KurtYoung commented on a change in pull request #10098: [FLINK-14326][FLINK-14324][table] Support to apply watermark assigner in planner URL: https://github.com/apache/flink/pull/10098#discussion_r343981291
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/calcite/SqlExprToRexConverterImpl.java ########## @@ -0,0 +1,160 @@ +/* + * 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.calcite; + +import org.apache.flink.table.planner.plan.FlinkCalciteCatalogReader; + +import org.apache.calcite.config.CalciteConnectionConfig; +import org.apache.calcite.config.CalciteConnectionConfigImpl; +import org.apache.calcite.config.CalciteConnectionProperty; +import org.apache.calcite.jdbc.CalciteSchema; +import org.apache.calcite.jdbc.CalciteSchemaBuilder; +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.prepare.CalciteCatalogReader; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.logical.LogicalProject; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.schema.Table; +import org.apache.calcite.schema.impl.AbstractSchema; +import org.apache.calcite.schema.impl.AbstractTable; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.tools.FrameworkConfig; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; + +/** + * Standard implementation of {@link SqlExprToRexConverter}. + */ +public class SqlExprToRexConverterImpl implements SqlExprToRexConverter { + + private static final String TEMPORARY_TABLE_NAME = "__temp_table__"; + private static final String QUERY_FORMAT = "SELECT %s FROM " + TEMPORARY_TABLE_NAME; + + private final FlinkPlannerImpl planner; + + public SqlExprToRexConverterImpl( + FrameworkConfig config, + FlinkTypeFactory typeFactory, + RelOptCluster cluster, + RelDataType tableRowType) { + this.planner = new FlinkPlannerImpl( + config, + (isLenient) -> createSingleTableCatalogReader(isLenient, config, typeFactory, tableRowType), + typeFactory, + cluster + ); + } + + @Override + public RexNode convertToRexNode(String expr) { + return convertToRexNodes(new String[]{expr})[0]; + } + + @Override + public RexNode[] convertToRexNodes(String[] exprs) { + String query = String.format(QUERY_FORMAT, String.join(",", exprs)); + SqlNode parsed = planner.parser().parse(query); + SqlNode validated = planner.validate(parsed); + RelNode rel = planner.rel(validated).rel; + if (rel instanceof LogicalProject) { Review comment: Check this `LogicalProject` is based on table scan. I'm not sure whether the planner will give us a RelNode like ``` LogicalProject LogicalProject TableScan ``` If this happened, we will end up with a very weird error. ---------------------------------------------------------------- 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
