JingsongLi commented on a change in pull request #9485: 
[FLINK-13775][table-planner-blink] Refactor 
ExpressionConverter(RexNodeConverter) in blink
URL: https://github.com/apache/flink/pull/9485#discussion_r319350369
 
 

 ##########
 File path: 
flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/expressions/converter/CustomizedConvertRule.java
 ##########
 @@ -0,0 +1,361 @@
+/*
+ * 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.table.planner.expressions.converter;
+
+import org.apache.flink.table.expressions.CallExpression;
+import org.apache.flink.table.expressions.Expression;
+import org.apache.flink.table.expressions.ExpressionUtils;
+import org.apache.flink.table.expressions.TableReferenceExpression;
+import org.apache.flink.table.expressions.TypeLiteralExpression;
+import org.apache.flink.table.expressions.ValueLiteralExpression;
+import org.apache.flink.table.expressions.utils.ApiExpressionUtils;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.FunctionDefinition;
+import org.apache.flink.table.operations.QueryOperation;
+import org.apache.flink.table.planner.calcite.FlinkRelBuilder;
+import org.apache.flink.table.planner.functions.InternalFunctionDefinitions;
+import org.apache.flink.table.planner.functions.sql.FlinkSqlOperatorTable;
+import org.apache.flink.table.planner.functions.sql.SqlThrowExceptionFunction;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.util.Preconditions;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexSubQuery;
+import org.apache.calcite.sql.fun.SqlTrimFunction;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.apache.calcite.sql.type.SqlTypeName.VARCHAR;
+import static 
org.apache.flink.table.planner.calcite.FlinkTypeFactory.toLogicalType;
+import static 
org.apache.flink.table.planner.expressions.converter.ExpressionConverter.extractValue;
+import static 
org.apache.flink.table.runtime.types.LogicalTypeDataTypeConverter.fromDataTypeToLogicalType;
+import static 
org.apache.flink.table.runtime.typeutils.TypeCheckUtils.isCharacterString;
+import static 
org.apache.flink.table.runtime.typeutils.TypeCheckUtils.isTemporal;
+import static 
org.apache.flink.table.runtime.typeutils.TypeCheckUtils.isTimeInterval;
+
+/**
+ * Customized {@link CallExpressionConvertRule}, Functions conversion here all 
require special logic,
+ * and there may be some special rules, such as needing get the literal values 
of inputs, such as
+ * converting to combinations of functions, to convert to RexNode of calcite.
+ */
+public class CustomizedConvertRule implements CallExpressionConvertRule {
+
+       private static final Map<FunctionDefinition, Conversion> 
DEFINITION_RULE_MAP = new HashMap<>();
+       static {
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.CAST, 
CustomizedConvertRule::convertCast);
+               
DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.REINTERPRET_CAST, 
CustomizedConvertRule::convertReinterpretCast);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.IN, 
CustomizedConvertRule::convertIn);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.GET, 
CustomizedConvertRule::convertGet);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.TRIM, 
CustomizedConvertRule::convertTrim);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.AS, 
CustomizedConvertRule::convertAs);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.IS_NULL, 
CustomizedConvertRule::convertIsNull);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.BETWEEN, 
CustomizedConvertRule::convertBetween);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.NOT_BETWEEN, 
CustomizedConvertRule::convertNotBetween);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.REPLACE, 
CustomizedConvertRule::convertReplace);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.PLUS, 
CustomizedConvertRule::convertPlus);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.CEIL, 
CustomizedConvertRule::convertCeil);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.FLOOR, 
CustomizedConvertRule::convertFloor);
+               
DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.TEMPORAL_OVERLAPS, 
CustomizedConvertRule::convertTemporalOverlaps);
+               
DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.TIMESTAMP_DIFF, 
CustomizedConvertRule::convertTimestampDiff);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.ARRAY, 
CustomizedConvertRule::convertArray);
+               
DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.ARRAY_ELEMENT, 
CustomizedConvertRule::convertArrayElement);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.MAP, 
CustomizedConvertRule::convertMap);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.ROW, 
CustomizedConvertRule::convertRow);
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.ORDER_ASC, 
CustomizedConvertRule::convertOrderAsc);
+
+               DEFINITION_RULE_MAP.put(BuiltInFunctionDefinitions.SQRT, 
(children, context) ->
+                       
context.getRelBuilder().call(FlinkSqlOperatorTable.POWER, context.toRexNodes(
+                               Arrays.asList(children.get(0), 
ApiExpressionUtils.valueLiteral(0.5)))));
+
+               // blink expression
+               
DEFINITION_RULE_MAP.put(InternalFunctionDefinitions.THROW_EXCEPTION, (children, 
context) -> {
+                       DataType type = ((TypeLiteralExpression) 
children.get(1)).getOutputDataType();
+                       SqlThrowExceptionFunction function = new 
SqlThrowExceptionFunction(
+                                       
context.getTypeFactory().createFieldTypeFromLogicalType(fromDataTypeToLogicalType(type)));
+                       return context.getRelBuilder().call(function, 
context.toRexNode(children.get(0)));
+               });
+       }
+
+       @Override
+       public Optional<RexNode> convert(CallExpression call, ConvertContext 
context) {
+               Conversion conversion = 
DEFINITION_RULE_MAP.get(call.getFunctionDefinition());
+               if (conversion == null) {
+                       return Optional.empty();
+               } else {
+                       return 
Optional.of(conversion.convert(call.getChildren(), context));
+               }
+       }
+
+       private static RexNode convertCast(List<Expression> children, 
ConvertContext context) {
+               RexNode child = context.toRexNode(children.get(0));
+               TypeLiteralExpression type = (TypeLiteralExpression) 
children.get(1);
+               return context.getRelBuilder().getRexBuilder().makeAbstractCast(
+                       context.getTypeFactory().createFieldTypeFromLogicalType(
+                               
type.getOutputDataType().getLogicalType().copy(child.getType().isNullable())),
+                       child);
+       }
+
+       private static RexNode convertArrayElement(List<Expression> children, 
ConvertContext context) {
+               List<RexNode> childrenRexNode = context.toRexNodes(children);
+               return 
context.getRelBuilder().call(FlinkSqlOperatorTable.ELEMENT, childrenRexNode);
 
 Review comment:
   I'll move 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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to