[ 
https://issues.apache.org/jira/browse/BEAM-8583?focusedWorklogId=342942&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-342942
 ]

ASF GitHub Bot logged work on BEAM-8583:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 13/Nov/19 21:59
            Start Date: 13/Nov/19 21:59
    Worklog Time Spent: 10m 
      Work Description: 11moon11 commented on pull request #10030: [BEAM-8583] 
[SQL] Big query filter push down
URL: https://github.com/apache/beam/pull/10030#discussion_r346024710
 
 

 ##########
 File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigquery/BigQueryFilter.java
 ##########
 @@ -0,0 +1,143 @@
+/*
+ * 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.meta.provider.bigquery;
+
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.AND;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.BETWEEN;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.CAST;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.COMPARISON;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.DIVIDE;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.LIKE;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.MINUS;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.MOD;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.OR;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.PLUS;
+import static 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind.TIMES;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.sdk.extensions.sql.meta.BeamSqlTableFilter;
+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.RexInputRef;
+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.RexNode;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.SqlKind;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.sql.type.SqlTypeName;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableSet;
+
+public class BigQueryFilter implements BeamSqlTableFilter {
+  private static final ImmutableSet<SqlKind> SUPPORTED_OPS =
+      ImmutableSet.<SqlKind>builder()
+          .add(COMPARISON.toArray(new SqlKind[0]))
+          // TODO: Check what other functions are supported and add support 
for them (ex: trim).
+          .add(PLUS, MINUS, MOD, DIVIDE, TIMES, LIKE, BETWEEN, CAST)
+          .build();
+  private List<RexNode> supported;
+  private List<RexNode> unsupported;
+
+  public BigQueryFilter(List<RexNode> predicateCNF) {
+    supported = new ArrayList<>();
+    unsupported = new ArrayList<>();
+
+    for (RexNode node : predicateCNF) {
+      if (!node.getType().getSqlTypeName().equals(SqlTypeName.BOOLEAN)) {
+        throw new RuntimeException(
+            "Predicate node '"
+                + node.getClass().getSimpleName()
+                + "' should be a boolean expression, but was: "
+                + node.getType().getSqlTypeName());
+      }
+
+      if (isSupported(node).getLeft()) {
+        supported.add(node);
+      } else {
+        unsupported.add(node);
+      }
+    }
+  }
+
+  @Override
+  public List<RexNode> getNotSupported() {
+    return unsupported;
+  }
+
+  public List<RexNode> getSupported() {
+    return supported;
+  }
+
+  @Override
+  public String toString() {
+    String supStr =
+        "supported{"
+            + 
supported.stream().map(RexNode::toString).collect(Collectors.joining())
+            + "}";
+    String unsupStr =
+        "unsupported{"
+            + 
unsupported.stream().map(RexNode::toString).collect(Collectors.joining())
+            + "}";
+
+    return "[" + supStr + ", " + unsupStr + "]";
+  }
+
+  /**
+   * Check whether a {@code RexNode} is supported. As of right now BigQuery 
supports: 1. Complex
+   * predicates (both conjunction and disjunction). 2. Comparison between a 
column and a literal.
+   *
+   * <p>TODO: Check if comparison between two columns is supported. Also over 
a boolean field.
+   *
+   * @param node A node to check for predicate push-down support.
+   * @return A pair containing a boolean whether an expression is supported 
and the number of input
+   *     references used by the expression.
+   */
+  private Pair<Boolean, Integer> isSupported(RexNode node) {
+    int numberOfInputRefs = 0;
+    boolean isSupported = true;
+
+    if (node instanceof RexCall) {
+      RexCall compositeNode = (RexCall) node;
+
+      // Only support comparisons in a predicate, some sql functions such as:
+      //  CAST, TRIM? and REVERSE? should be supported as well.
+      if (!node.getKind().belongsTo(SUPPORTED_OPS)) {
+        isSupported = false;
+      }
+
+      for (RexNode operand : compositeNode.getOperands()) {
+        // All operands must be supported for a parent node to be supported.
+        Pair<Boolean, Integer> childSupported = isSupported(operand);
+        // BigQuery supports complex combinations of both conjunctions (AND) 
and disjunctions (OR).
+        if (!node.getKind().belongsTo(ImmutableSet.of(AND, OR))) {
+          // Predicate functions, where more than one field is involved are 
unsupported.
+          numberOfInputRefs += childSupported.getRight();
+        }
+        isSupported = numberOfInputRefs < 2 && childSupported.getLeft();
+      }
+    } else if (node instanceof RexInputRef) {
+      numberOfInputRefs++;
 
 Review comment:
   I agree, updated.
 
----------------------------------------------------------------
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]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 342942)
    Time Spent: 1h  (was: 50m)

> [SQL] BigQuery should support predicate push-down in DIRECT_READ mode
> ---------------------------------------------------------------------
>
>                 Key: BEAM-8583
>                 URL: https://issues.apache.org/jira/browse/BEAM-8583
>             Project: Beam
>          Issue Type: Improvement
>          Components: dsl-sql
>            Reporter: Kirill Kozlov
>            Assignee: Kirill Kozlov
>            Priority: Major
>          Time Spent: 1h
>  Remaining Estimate: 0h
>
> * Add BigQuery Dialect with TypeTranslation (since it is not implemented in 
> Calcite 1.20.0, but is present in unreleased versions).
>  * Create a BigQueryFilter class.
>  * BigQueryTable#buildIOReader should translate supported filters into a Sql 
> string and pass it to BigQueryIO.
>  
> Potential improvements:
>  * After updating vendor Calcite, class 
> `BigQuerySqlDialectWithTypeTranslation` can be deleted and Calcite's 
> `BigQuerySqlDialect` can be utilized instead.
>  * Once BigQuery adds support for more filters, `BigQueryFilter#isSupported` 
> should be updated.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to