lirui-apache commented on a change in pull request #15253: URL: https://github.com/apache/flink/pull/15253#discussion_r604833563
########## File path: flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/planner/delegation/hive/SqlFunctionConverter.java ########## @@ -0,0 +1,191 @@ +/* + * 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.delegation.hive; + +import org.apache.flink.connectors.hive.FlinkHiveException; +import org.apache.flink.table.planner.delegation.hive.copy.HiveParserBetween; +import org.apache.flink.table.planner.delegation.hive.copy.HiveParserSqlFunctionConverter; +import org.apache.flink.table.planner.functions.sql.SqlCurrentTimestampFunction; +import org.apache.flink.util.Preconditions; + +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexFieldCollation; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexOver; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.rex.RexWindow; +import org.apache.calcite.sql.SqlAggFunction; +import org.apache.calcite.sql.SqlFunction; +import org.apache.calcite.sql.SqlFunctionCategory; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.SqlOperatorTable; +import org.apache.calcite.sql.SqlSyntax; +import org.apache.calcite.sql.fun.SqlCastFunction; +import org.apache.calcite.sql.validate.SqlNameMatcher; +import org.apache.hadoop.hive.ql.parse.SemanticException; +import org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc; +import org.apache.hadoop.hive.ql.session.SessionState; + +import java.lang.reflect.Field; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; + +/** A RexShuttle that converts Hive function calls so that Flink recognizes them. */ +public class SqlFunctionConverter extends RexShuttle { + + // need reflection to get some fields from RexWindow + private static final Field REX_WINDOW_PART_KEYS; + private static final Field REX_WINDOW_ORDER_KEYS; + + static { + try { + REX_WINDOW_PART_KEYS = RexWindow.class.getDeclaredField("partitionKeys"); + REX_WINDOW_ORDER_KEYS = RexWindow.class.getDeclaredField("orderKeys"); + } catch (Exception e) { + throw new FlinkHiveException("Failed to init RexWindow fields", e); + } + } + + protected final RelOptCluster cluster; + protected final RexBuilder builder; + private final SqlOperatorTable opTable; + private final SqlNameMatcher nameMatcher; + + public SqlFunctionConverter( + RelOptCluster cluster, SqlOperatorTable opTable, SqlNameMatcher nameMatcher) { + this.cluster = cluster; + this.builder = cluster.getRexBuilder(); + this.opTable = opTable; + this.nameMatcher = nameMatcher; + } + + @Override + public RexNode visitCall(RexCall call) { + SqlOperator operator = call.getOperator(); + List<RexNode> operands = call.getOperands(); + SqlOperator convertedOp = convertOperator(operator); + final boolean[] update = null; + if (convertedOp instanceof SqlCastFunction) { + RelDataType type = call.getType(); + return builder.makeCall(type, convertedOp, visitList(operands, update)); + } else { + if (convertedOp instanceof SqlCurrentTimestampFunction) { + // flink's current_timestamp has different type from hive's, convert it to a literal Review comment: Unfortunately hive connector doesn't support `TIMESTAMP LTZ` type, so we can't use Flink's function. -- 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