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



##########
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:
       nit: Possibly put this in `org.apache.beam.sdk.extensions.sql.impl.rel`?

##########
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:
       It appears `BeamJavaUdfCalcRule`/`BeamZetaSqlCalcRule` are out of sync 
with their respective `CalcRelSplitter.RelType`. The logic is mostly duplicated 
between the two, but it is slightly different. Can you make a abstract rule 
that takes a single `CalcRelSplitter.RelType` and replace the old CalcRules 
with that?

##########
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:
       Just looking at the type without looking at what the `RexLocalRef` is 
referencing seems like a problem here. We know that some types work as inputs 
but not literals, this doesn't appear to enforce that distinction. (This goes 
back to my concern above.)

##########
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 am concerned about this only applying to freestanding field accesses 
(same thing with literals below). I would expect this to also prevent the field 
access from a RexCall.




-- 
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