fsk119 commented on code in PR #26553: URL: https://github.com/apache/flink/pull/26553#discussion_r2101708436
########## flink-table/flink-table-planner/src/main/java/org/apache/calcite/sql/validate/ProcedureNamespace.java: ########## @@ -61,7 +62,7 @@ public RelDataType validateImpl(RelDataType targetRowType) { final SqlOperator operator = call.getOperator(); final SqlCallBinding callBinding = new FlinkSqlCallBinding(validator, scope, call); final SqlCall permutedCall = callBinding.permutedCall(); - if (operator instanceof SqlWindowTableFunction) { + if (operator instanceof SqlWindowTableFunction || operator instanceof SqlMLTableFunction) { Review Comment: Calcite have already validated this call at `SqlValidatorImpl#validateCall`, I think we don't need to validate it again. ########## flink-table/flink-table-planner/src/main/java/org/apache/calcite/sql/SqlModelCall.java: ########## @@ -0,0 +1,81 @@ +/* + * 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.calcite.sql; + +import org.apache.flink.table.planner.catalog.CatalogSchemaModel; + +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.sql.validate.SqlValidatorScope; + +import static org.apache.calcite.util.Static.RESOURCE; + +/** SqlModelCall to fetch and reference model based on identifier. */ +public class SqlModelCall extends SqlBasicCall { + + private final CatalogSchemaModel model; + + public SqlModelCall(SqlExplicitModelCall modelCall, CatalogSchemaModel model) { + super( + new SqlModelOperator(model), + modelCall.getOperandList(), + modelCall.getParserPosition(), + modelCall.getFunctionQuantifier()); + this.model = model; + } + + @Override + public void validate(SqlValidator validator, SqlValidatorScope scope) { + SqlIdentifier modelIdentifier = (SqlIdentifier) getOperandList().get(0); + if (model == null) { Review Comment: Remove this check. It is useless now. ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/ml/SqlMLTableFunction.java: ########## @@ -0,0 +1,113 @@ +/* + * 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.functions.sql.ml; + +import org.apache.flink.table.api.ValidationException; + +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlFunction; +import org.apache.calcite.sql.SqlFunctionCategory; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlOperatorBinding; +import org.apache.calcite.sql.SqlSelect; +import org.apache.calcite.sql.SqlTableFunction; +import org.apache.calcite.sql.type.ReturnTypes; +import org.apache.calcite.sql.type.SqlOperandMetadata; +import org.apache.calcite.sql.type.SqlReturnTypeInference; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.sql.validate.SqlValidatorScope; + +import java.util.List; + +/** + * Base class for a table-valued function that works with models. Examples include {@code + * ML_PREDICT}. + */ +public abstract class SqlMLTableFunction extends SqlFunction implements SqlTableFunction { + + private static final String TABLE_INPUT_ERROR = + "SqlMLTableFunction must have only one table as first operand."; + + protected static final String PARAM_INPUT = "INPUT"; + protected static final String PARAM_MODEL = "MODEL"; + protected static final String PARAM_COLUMN = "ARGS"; + protected static final String PARAM_CONFIG = "CONFIG"; + + public SqlMLTableFunction(String name, SqlOperandMetadata operandMetadata) { + super( + name, + SqlKind.OTHER_FUNCTION, + ReturnTypes.CURSOR, + null, + operandMetadata, + SqlFunctionCategory.SYSTEM); + } + + @Override + public void validateCall( + SqlCall call, + SqlValidator validator, + SqlValidatorScope scope, + SqlValidatorScope operandScope) { + assert call.getOperator() == this; + final List<SqlNode> operandList = call.getOperandList(); + + // ML table function should take only one table as input and use descriptor to reference + // columns in the table. The scope for descriptor validation should be the input table. + // Since the input table will be rewritten as select query. We get the select query's scope + // and use it for descriptor validation. + SqlValidatorScope selectScope = null; Review Comment: Why don't validate the operand type in PredictOperandMetadata#checkOperandTypes? According to the javadoc, I think we should delay the check later. ``` /** * Validates a call to this operator. * * <p>This method should not perform type-derivation or perform validation * related related to types. That is done later, by * {@link #deriveType(SqlValidator, SqlValidatorScope, SqlCall)}. This method * should focus on structural validation. * * <p>A typical implementation of this method first validates the operands, * then performs some operator-specific logic. The default implementation * just validates the operands. * * <p>This method is the default implementation of {@link SqlCall#validate}; * but note that some sub-classes of {@link SqlCall} never call this method. * * @param call the call to this operator * @param validator the active validator * @param scope validator scope * @param operandScope validator scope in which to validate operands to this * call; usually equal to scope, but not always because * some operators introduce new scopes * @see SqlNode#validateExpr(SqlValidator, SqlValidatorScope) * @see #deriveType(SqlValidator, SqlValidatorScope, SqlCall) */ public void validateCall( SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope) { assert call.getOperator() == this; for (SqlNode operand : call.getOperandList()) { operand.validateExpr(validator, operandScope); } } ``` I think we don't need to override this method. -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org