sujun1020 commented on a change in pull request #13306:
URL: https://github.com/apache/flink/pull/13306#discussion_r484235277



##########
File path: 
flink-formats/flink-orc/src/main/java/org/apache/flink/orc/OrcFilters.java
##########
@@ -0,0 +1,372 @@
+/*
+ * 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.flink.orc;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.api.java.tuple.Tuple3;
+import org.apache.flink.table.expressions.CallExpression;
+import org.apache.flink.table.expressions.Expression;
+import org.apache.flink.table.expressions.FieldReferenceExpression;
+import org.apache.flink.table.expressions.ValueLiteralExpression;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.FunctionDefinition;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+
+import org.apache.flink.shaded.curator4.com.google.common.collect.ImmutableMap;
+
+import org.apache.hadoop.hive.ql.io.sarg.PredicateLeaf;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.util.Optional;
+import java.util.function.Function;
+
+/**
+ * Utility class that provides helper methods to work with Orc Filter PushDown.
+ */
+public class OrcFilters {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(OrcFileSystemFormatFactory.class);
+
+       private static final ImmutableMap<FunctionDefinition, 
Function<CallExpression, OrcSplitReader.Predicate>> FILTERS =
+                       new ImmutableMap.Builder<FunctionDefinition, 
Function<CallExpression, OrcSplitReader.Predicate>>()
+                                       
.put(BuiltInFunctionDefinitions.IS_NULL, createIsNullPredicateConverter())
+                                       
.put(BuiltInFunctionDefinitions.IS_NOT_NULL, 
createIsNotNullPredicateConverter())
+                                       .put(BuiltInFunctionDefinitions.NOT, 
createNotPredicateConverter())
+                                       .put(BuiltInFunctionDefinitions.OR, 
createOrPredicateConverter())
+                                       .put(BuiltInFunctionDefinitions.EQUALS, 
createEqualsPredicateConverter())
+                                       
.put(BuiltInFunctionDefinitions.NOT_EQUALS, createNotEqualsPredicateConverter())
+                                       
.put(BuiltInFunctionDefinitions.GREATER_THAN, 
createGreaterThanPredicateConverter())
+                                       
.put(BuiltInFunctionDefinitions.GREATER_THAN_OR_EQUAL, 
createGreaterThanEqualsPredicateConverter())
+                                       
.put(BuiltInFunctionDefinitions.LESS_THAN, createLessThanPredicateConverter())
+                                       
.put(BuiltInFunctionDefinitions.LESS_THAN_OR_EQUAL, 
createLessThanEqualsPredicateConverter())
+                                       .build();
+
+       private static boolean isUnaryValid(CallExpression callExpression) {
+               return callExpression.getChildren().size() == 1 && 
callExpression.getChildren().get(0) instanceof FieldReferenceExpression;
+       }
+
+       private static boolean isBinaryValid(CallExpression callExpression) {
+               return callExpression.getChildren().size() == 2 && 
((callExpression.getChildren().get(0) instanceof FieldReferenceExpression && 
callExpression.getChildren().get(1) instanceof ValueLiteralExpression) ||
+                               (callExpression.getChildren().get(0) instanceof 
ValueLiteralExpression && callExpression.getChildren().get(1) instanceof 
FieldReferenceExpression));
+       }
+
+       private static Tuple2<String, PredicateLeaf.Type> 
getTuple2Args(CallExpression callExp){
+               if (!isUnaryValid(callExp)) {
+                       // not a valid predicate
+                       LOG.debug("Unsupported predicate [{}] cannot be pushed 
into OrcFileSystemFormatFactory.", callExp);
+                       return null;
+               }
+
+               PredicateLeaf.Type colType = 
toOrcType(((FieldReferenceExpression) 
callExp.getChildren().get(0)).getOutputDataType());
+               if (colType == null) {
+                       // unsupported type
+                       LOG.debug("Unsupported predicate [{}] cannot be pushed 
into OrcFileSystemFormatFactory.", callExp);
+                       return null;
+               }
+
+               String colName = getColumnName(callExp);
+
+               return Tuple2.of(colName, colType);
+       }
+
+       private static Tuple3<String, PredicateLeaf.Type, Serializable> 
getTuple3Args(CallExpression callExp){
+               if (!isBinaryValid(callExp)) {
+                       // not a valid predicate
+                       LOG.debug("Unsupported predicate [{}] cannot be pushed 
into OrcFileSystemFormatFactory.", callExp);
+                       return null;
+               }
+
+               PredicateLeaf.Type litType = getLiteralType(callExp);
+               if (litType == null) {
+                       // unsupported literal type
+                       LOG.debug("Unsupported predicate [{}] cannot be pushed 
into OrcFileSystemFormatFactory.", callExp);
+                       return null;
+               }
+
+               String colName = getColumnName(callExp);
+
+               // fetch literal and ensure it is serializable
+               Object literalObj = getLiteral(callExp).get();
+               Serializable literal;
+               // validate that literal is serializable
+               if (literalObj instanceof Serializable) {
+                       literal = (Serializable) literalObj;
+               } else {
+                       LOG.warn("Encountered a non-serializable literal of 
type {}. " +
+                                                       "Cannot push predicate 
[{}] into OrcFileSystemFormatFactory. " +
+                                                       "This is a bug and 
should be reported.",
+                                       
literalObj.getClass().getCanonicalName(), callExp);
+                       return null;
+               }
+
+               return Tuple3.of(colName, litType, literal);
+       }
+
+       private static Function<CallExpression, OrcSplitReader.Predicate> 
createIsNullPredicateConverter(){
+               return callExp -> {
+                               Tuple2<String, PredicateLeaf.Type> tuple2 = 
getTuple2Args(callExp);
+
+                               if (tuple2 == null){
+                                       return null;
+                               }
+
+                               return new OrcSplitReader.IsNull(tuple2.f0, 
tuple2.f1);
+               };
+       }
+
+       private static Function<CallExpression, OrcSplitReader.Predicate> 
createIsNotNullPredicateConverter(){
+               return callExp -> {
+
+                       Tuple2<String, PredicateLeaf.Type> tuple2 = 
getTuple2Args(callExp);
+
+                       if (tuple2 == null){
+                               return null;
+                       }
+
+                       return new OrcSplitReader.Not(
+                                       new OrcSplitReader.IsNull(tuple2.f0, 
tuple2.f1));
+               };
+       }
+
+       private static Function<CallExpression, OrcSplitReader.Predicate> 
createNotPredicateConverter(){
+               return callExp -> {
+                       if (callExp.getChildren().size() != 1) {
+                               // not a valid predicate
+                               LOG.debug("Unsupported predicate [{}] cannot be 
pushed into OrcFileSystemFormatFactory.", callExp);
+                               return null;
+                       }
+
+                       OrcSplitReader.Predicate c = 
toOrcPredicate(callExp.getChildren().get(0));
+                       if (c == null) {
+                               return null;
+                       } else {
+                               return new OrcSplitReader.Not(c);
+                       }
+               };
+       }
+
+       private static Function<CallExpression, OrcSplitReader.Predicate> 
createOrPredicateConverter(){
+               return callExp -> {
+                       if (callExp.getChildren().size() < 2) {
+                               return null;
+                       }
+                       Expression left = callExp.getChildren().get(0);
+                       Expression right = callExp.getChildren().get(1);
+
+                       OrcSplitReader.Predicate c1 = toOrcPredicate(left);
+                       OrcSplitReader.Predicate c2 = toOrcPredicate(right);
+                       if (c1 == null || c2 == null) {
+                               return null;
+                       } else {
+                               return new OrcSplitReader.Or(c1, c2);
+                       }
+               };
+       }
+
+       private static Function<CallExpression, OrcSplitReader.Predicate> 
createEqualsPredicateConverter(){
+               return callExp -> {
+                       Tuple3<String, PredicateLeaf.Type, Serializable> tuple3 
= getTuple3Args(callExp);
+
+                       if (tuple3 == null){
+                               return null;
+                       }
+                       return new OrcSplitReader.Equals(tuple3.f0, tuple3.f1, 
tuple3.f2);
+               };
+       }
+
+       private static Function<CallExpression, OrcSplitReader.Predicate> 
createNotEqualsPredicateConverter(){

Review comment:
       I got it, Thanks a lot




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to