wuchong commented on code in PR #22166: URL: https://github.com/apache/flink/pull/22166#discussion_r1144158121
########## flink-table/flink-table-calcite-bridge/src/main/java/org/apache/flink/table/calcite/bridge/CalciteContext.java: ########## @@ -0,0 +1,69 @@ +/* + * 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.calcite.bridge; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.api.TableConfig; +import org.apache.flink.table.catalog.FunctionCatalog; +import org.apache.flink.table.delegation.DialectFactory; + +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptTable; +import org.apache.calcite.prepare.CalciteCatalogReader; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.tools.FrameworkConfig; +import org.apache.calcite.tools.RelBuilder; + +/** + * To provide calcite context which is mainly used to create Calcite's RelNode. The context is + * provided by Flink's table planner. + */ +@Internal +public interface CalciteContext extends DialectFactory.Context { + + /** Create a {@link CalciteCatalogReader} . */ + CalciteCatalogReader createCatalogReader(boolean lenientCaseSensitivity); + + /** Return the RelOptCluster provided by Flink's table planner. */ + RelOptCluster getCluster(); + + /** Create {@link FrameworkConfig} customized by Flink's table planner. */ + FrameworkConfig createFrameworkConfig(); + + /** Return {@link RelDataTypeFactory} customized by Flink's table planner. */ + RelDataTypeFactory getTypeFactory(); + + /** Create a builder for relational expressions provided by Flink's table planner. */ + RelBuilder createRelBuilder(); + + /** Return the {@link TableConfig}. */ + TableConfig getTableConfig(); + + /** Return the {@link ClassLoader}. */ + ClassLoader getClassLoader(); + + /** Return the {@link FunctionCatalog}. */ + FunctionCatalog getFunctionCatalog(); + + /** + * Create the {@link RelOptTable.ToRelContext} used t0 convert a table into a relational Review Comment: ```suggestion * Create a new instance of {@link RelOptTable.ToRelContext} used to convert a table into a relational ``` ########## flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/delegation/DialectFactory.java: ########## @@ -36,52 +34,22 @@ * <p>The {@link #factoryIdentifier()} is identified by matching it against {@link * TableConfigOptions#TABLE_SQL_DIALECT}. */ -@Internal +@PublicEvolving public interface DialectFactory extends Factory { - /** Creates a new parser. */ Parser create(Context context); + /** Create an extended operation executor. */ default ExtendedOperationExecutor createExtendedOperationExecutor(Context context) { Review Comment: I'm thinking of removing the `ExtendedOperationExecutor` because we have introduced `ExecutableOperation` which is a self-contained way to support the operation execution. In this way, we can rename `DialectFactory` back to `ParserFactory` to align with the original FLIP proposal. ########## flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/service/result/ResultFetcher.java: ########## @@ -134,9 +138,22 @@ private ResultFetcher( } public static ResultFetcher fromTableResult( - OperationHandle operationHandle, - TableResultInternal tableResult, - boolean isQueryResult) { + OperationHandle operationHandle, TableResult tableResult, boolean isQueryResult) { + CloseableIterator<RowData> resultRows; + RowDataToStringConverter rowDataToStringConverter; + if (tableResult instanceof TableResultInternal) { + TableResultInternal tableResultInternal = (TableResultInternal) tableResult; + resultRows = tableResultInternal.collectInternal(); + rowDataToStringConverter = tableResultInternal.getRowDataToStringConverter(); + } else { + // sometimes, the tableResult maybe not an instance of TableResultInternal + // in the case of pluggable dialect implements method + // ExtendedOperationExecutor#executeOperation, and it doesn't return TableResultInternal Review Comment: Which case doesn't return `TableResultInternal`? ########## flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/delegation/PlannerBase.scala: ########## @@ -169,17 +171,53 @@ abstract class PlannerBase( override def getParser: Parser = { if (parser == null || getTableConfig.getSqlDialect != currentDialect) { dialectFactory = getDialectFactory - parser = - dialectFactory.create(new DefaultParserContext(catalogManager, plannerContext, executor)) + parser = dialectFactory.create( + new DefaultCalciteContext(catalogManager, getOperationTreeBuilder, plannerContext)) } parser } + private def getOperationTreeBuilder: OperationTreeBuilder = { + OperationTreeBuilderImpl.create( + tableConfig, + classLoader, + functionCatalog.asLookup(f => getParser.parseIdentifier(f)), + catalogManager.getDataTypeFactory, + (path: String) => getTableReferenceExpression(path), + (s: String, inputRowType: RowType, outputType) => + getParser.parseSqlExpression(s, inputRowType, outputType), + isStreamingMode + ) + } Review Comment: The creation of OperationTreeBuilderImpl is super complex, and the logic is duplicated. This is difficult to maintain logic consistency in the future. Besides, the extracted `OperationTreeBuilder` seems incomplete (e.g, join) and is never used. I would prefer to introduce the `OperationTreeBuilder` when we indeed need this, otherwise, it only increases the overhead of maintenance. What do you think? ########## flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/TableEnvironmentInternal.java: ########## @@ -74,15 +75,15 @@ public interface TableEnvironmentInternal extends TableEnvironment { * @param operations The operations to be executed. * @return the affected row counts (-1 means unknown). */ - TableResultInternal executeInternal(List<ModifyOperation> operations); + TableResult executeInternal(List<ModifyOperation> operations); /** * Execute the given operation and return the execution result. * * @param operation The operation to be executed. * @return the content of the execution result. */ - TableResultInternal executeInternal(Operation operation); + TableResult executeInternal(Operation operation); Review Comment: Why do we need to change this? -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
