Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.paimon.flink;
import org.apache.paimon.data.BinaryString;
import org.apache.paimon.predicate.Predicate;
import org.apache.paimon.predicate.PredicateBuilder;
import org.apache.paimon.utils.TypeUtils;
import org.apache.flink.table.data.conversion.DataStructureConverters;
import org.apache.flink.table.expressions.CallExpression;
import org.apache.flink.table.expressions.Expression;
import org.apache.flink.table.expressions.ExpressionVisitor;
import org.apache.flink.table.expressions.FieldReferenceExpression;
import org.apache.flink.table.expressions.ResolvedExpression;
import org.apache.flink.table.expressions.TypeLiteralExpression;
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.LogicalType;
import org.apache.flink.table.types.logical.LogicalTypeFamily;
import org.apache.flink.table.types.logical.RowType;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.flink.table.types.logical.utils.LogicalTypeCasts.supportsImplicitCast;
import static org.apache.paimon.flink.LogicalTypeConversion.toDataType;
/**
* Convert {@link Expression} to {@link Predicate}.
*
*
For {@link FieldReferenceExpression}, please use name instead of index, if the project
* pushdown is before and the filter pushdown is after, the index of the filter will be projected.
*/
public class PredicateConverter implements ExpressionVisitor {
private final PredicateBuilder builder;
public PredicateConverter(RowType type) {
this(new PredicateBuilder(toDataType(type)));
}
public PredicateConverter(PredicateBuilder builder) {
this.builder = builder;
}
/** Accepts simple LIKE patterns like "abc%". */
private static final Pattern BEGIN_PATTERN = Pattern.compile("([^%]+)%");
@Override
public Predicate visit(CallExpression call) {
FunctionDefinition func = call.getFunctionDefinition();
List children = call.getChildren();
if (func == BuiltInFunctionDefinitions.AND) {
return PredicateBuilder.and(children.get(0).accept(this), children.get(1).accept(this));
} else if (func == BuiltInFunctionDefinitions.OR) {
return PredicateBuilder.or(children.get(0).accept(this), children.get(1).accept(this));
} else if (func == BuiltInFunctionDefinitions.EQUALS) {
return visitBiFunction(children, builder::equal, builder::equal);
} else if (func == BuiltInFunctionDefinitions.NOT_EQUALS) {
return visitBiFunction(children, builder::notEqual, builder::notEqual);
} else if (func == BuiltInFunctionDefinitions.GREATER_THAN) {
return visitBiFunction(children, builder::greaterThan, builder::lessThan);
} else if (func == BuiltInFunctionDefinitions.GREATER_THAN_OR_EQUAL) {
return visitBiFunction(children, builder::greaterOrEqual, builder::lessOrEqual);
} else if (func == BuiltInFunctionDefinitions.LESS_THAN) {
return visitBiFunction(children, builder::lessThan, builder::greaterThan);
} else if (func == BuiltInFunctionDefinitions.LESS_THAN_OR_EQUAL) {
return visitBiFunction(children, builder::lessOrEqual, builder::greaterOrEqual);
} else if (func == BuiltInFunctionDefinitions.IN) {
FieldReferenceExpression fieldRefExpr =
extractFieldReference(children.get(0)).orElseThrow(UnsupportedExpression::new);
List