ibzib commented on a change in pull request #14476:
URL: https://github.com/apache/beam/pull/14476#discussion_r613553067



##########
File path: 
sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/BeamCalcRelType.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.sql.zetasql;
+
+import org.apache.beam.sdk.extensions.sql.impl.CalcRelSplitter;
+import org.apache.beam.sdk.extensions.sql.impl.rel.BeamCalcRel;
+import org.apache.beam.sdk.extensions.sql.impl.rel.BeamLogicalConvention;
+import 
org.apache.beam.sdk.extensions.sql.zetasql.translation.ZetaSqlScalarFunctionImpl;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.adapter.enumerable.CallImplementor;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.adapter.enumerable.RexImpTable;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.plan.RelOptCluster;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.plan.RelOptRule;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.plan.RelTraitSet;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelNode;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexCall;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexDynamicParam;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexFieldAccess;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexLiteral;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexLocalRef;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexNode;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexProgram;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlOperator;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.tools.RelBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@link org.apache.beam.sdk.extensions.sql.impl.CalcRelSplitter.RelType} for 
{@link BeamCalcRel}.
+ */
+class BeamCalcRelType extends CalcRelSplitter.RelType {
+  private static final Logger LOG = 
LoggerFactory.getLogger(BeamCalcRelType.class);
+
+  BeamCalcRelType(String name) {
+    super(name);
+  }
+
+  @Override
+  protected boolean canImplement(RexFieldAccess field) {
+    // Don't implement freestanding field accesses. Only implement field 
accesses that are operands
+    // to UDF calls.
+    return false;
+  }
+
+  @Override
+  protected boolean canImplement(RexLiteral literal) {
+    // Don't implement freestanding literals. Only implement literals that are 
operands to UDF
+    // calls.
+    return false;
+  }
+
+  @Override
+  protected boolean canImplement(RexDynamicParam param) {
+    return false;
+  }
+
+  @Override
+  protected boolean canImplement(RexCall call) {
+    final SqlOperator operator = call.getOperator();
+
+    CallImplementor implementor = RexImpTable.INSTANCE.get(operator);
+    if (implementor == null) {
+      // Reject methods with no implementation
+      return false;
+    }
+
+    if (operator instanceof SqlUserDefinedFunction) {
+      SqlUserDefinedFunction udf = (SqlUserDefinedFunction) call.op;
+      if (udf.function instanceof ZetaSqlScalarFunctionImpl) {
+        ZetaSqlScalarFunctionImpl scalarFunction = (ZetaSqlScalarFunctionImpl) 
udf.function;
+        if (!scalarFunction.functionGroup.equals(
+            BeamZetaSqlCatalog.USER_DEFINED_JAVA_SCALAR_FUNCTIONS)) {
+          // Reject ZetaSQL Builtin Scalar Functions
+          return false;
+        }
+        for (RexNode operand : call.getOperands()) {
+          if (operand instanceof RexLocalRef) {
+            if (!(BeamJavaUdfCalcRule.udfSupportsLiteralType(operand.getType())

Review comment:
       > We know that some types work as inputs but not literals
   
   Which ones?
   
   I'm not sure we need to validate operand types here at all, since we do that 
during query parsing now (#14464).

##########
File path: 
sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/BeamJavaUdfCalcRule.java
##########
@@ -122,7 +122,7 @@ public RelNode convert(RelNode rel) {
    * Returns true only if the literal can be correctly implemented by {@link
    * org.apache.beam.sdk.extensions.sql.impl.rel.BeamCalcRel} in ZetaSQL.
    */
-  private static boolean udfSupportsLiteralType(RelDataType type) {
+  static boolean udfSupportsLiteralType(RelDataType type) {

Review comment:
       I agree we can find a better way to share all the logic between the two.

##########
File path: 
sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/BeamCalcRelType.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.sql.zetasql;
+
+import org.apache.beam.sdk.extensions.sql.impl.CalcRelSplitter;
+import org.apache.beam.sdk.extensions.sql.impl.rel.BeamCalcRel;
+import org.apache.beam.sdk.extensions.sql.impl.rel.BeamLogicalConvention;
+import 
org.apache.beam.sdk.extensions.sql.zetasql.translation.ZetaSqlScalarFunctionImpl;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.adapter.enumerable.CallImplementor;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.adapter.enumerable.RexImpTable;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.plan.RelOptCluster;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.plan.RelOptRule;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.plan.RelTraitSet;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelNode;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexCall;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexDynamicParam;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexFieldAccess;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexLiteral;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexLocalRef;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexNode;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rex.RexProgram;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlOperator;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.tools.RelBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@link org.apache.beam.sdk.extensions.sql.impl.CalcRelSplitter.RelType} for 
{@link BeamCalcRel}.
+ */
+class BeamCalcRelType extends CalcRelSplitter.RelType {
+  private static final Logger LOG = 
LoggerFactory.getLogger(BeamCalcRelType.class);
+
+  BeamCalcRelType(String name) {
+    super(name);
+  }
+
+  @Override
+  protected boolean canImplement(RexFieldAccess field) {
+    // Don't implement freestanding field accesses. Only implement field 
accesses that are operands

Review comment:
       I should probably clarify what I mean here. There is a distinction 
between "implementing" an expression and "referencing" an expression. For 
example, for `udf_call(field1)`, field1 is selected to be implemented or not in 
`canImplement(RexFieldAccess)`. But then it can be referenced (i.e. appear as a 
`RexLocalRef`) in `canImplement(RexCall)` as an operand to `udf_call`. I took 
the cautious approach here and said don't implement anything except the UDF 
calls themselves.
   
   But note that CalcRelSplitter treats literals as a special case, so 
BeamCalcRel actually does end up implementing literals that are arguments to 
UDFs. 
https://github.com/apache/beam/blob/93c174408c5f6827f3dab79431060871c1038773/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/CalcRelSplitter.java#L181-L183

##########
File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/CalcRelSplitter.java
##########
@@ -0,0 +1,898 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.extensions.sql.impl;

Review comment:
       Done.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to