kennknowles commented on code in PR #34827:
URL: https://github.com/apache/beam/pull/34827#discussion_r2075351067


##########
sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/FilterUtils.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * 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.io.iceberg;
+
+import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;
+import static 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
+
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.BiFunction;
+import java.util.stream.Collectors;
+import 
org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlBasicCall;
+import 
org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlIdentifier;
+import org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind;
+import 
org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlLiteral;
+import org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode;
+import 
org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNodeList;
+import 
org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperator;
+import 
org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException;
+import 
org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.expressions.Evaluator;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.Expression.Operation;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.types.Type.TypeID;
+import org.apache.iceberg.util.DateTimeUtil;
+import org.apache.iceberg.util.NaNUtil;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+/**
+ * Utilities that convert between a SQL filter expression and an Iceberg 
{@link Expression}. Uses
+ * Apache Calcite semantics.
+ *
+ * <p>Note: Only supports top-level fields (i.e. cannot reference nested 
fields).
+ */
+class FilterUtils {
+  private static final Map<SqlKind, Operation> FILTERS =
+      ImmutableMap.<SqlKind, Operation>builder()
+          .put(SqlKind.IS_NULL, Operation.IS_NULL)
+          .put(SqlKind.IS_NOT_NULL, Operation.NOT_NULL)
+          .put(SqlKind.LESS_THAN, Operation.LT)
+          .put(SqlKind.LESS_THAN_OR_EQUAL, Operation.LT_EQ)
+          .put(SqlKind.GREATER_THAN, Operation.GT)
+          .put(SqlKind.GREATER_THAN_OR_EQUAL, Operation.GT_EQ)
+          .put(SqlKind.EQUALS, Operation.EQ)
+          .put(SqlKind.NOT_EQUALS, Operation.NOT_EQ)
+          .put(SqlKind.IN, Operation.IN)
+          .put(SqlKind.NOT_IN, Operation.NOT_IN)
+          .put(SqlKind.AND, Operation.AND)
+          .put(SqlKind.OR, Operation.OR)
+          .build();
+
+  static FilterFunction filterOn(Expression expression, Schema schema) {
+    return new FilterFunction(expression, schema);
+  }
+
+  static class FilterFunction {
+    private final Evaluator evaluator;
+
+    FilterFunction(Expression expression, Schema schema) {
+      this.evaluator = new Evaluator(schema.asStruct(), expression);
+    }
+
+    boolean filter(Record rec) {
+      return evaluator.eval(rec);
+    }
+  }
+
+  static Expression convert(@Nullable String filter, Schema schema) {
+    if (filter == null) {
+      return Expressions.alwaysTrue();
+    }
+
+    SqlParser parser = SqlParser.create(filter);
+    try {
+      SqlNode expression = parser.parseExpression();
+      return convert(expression, schema);
+    } catch (Exception exception) {
+      throw new RuntimeException(
+          String.format("Encountered an error when parsing filter: '%s'", 
filter), exception);
+    }
+  }
+
+  private static Expression convert(SqlNode expression, Schema schema) throws 
SqlParseException {

Review Comment:
   OK cool, makes sense. Extra credit project: write to d...@iceberg.apache.org 
pointing to ours, Flink's, and Spark's code that converts Calcite into Iceberg 
expression, and suggest they adopt it.



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

To unsubscribe, e-mail: github-unsubscr...@beam.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to