fsk119 commented on code in PR #26644: URL: https://github.com/apache/flink/pull/26644#discussion_r2148969047
########## flink-table/flink-table-common/src/main/java/org/apache/flink/table/ml/TaskType.java: ########## @@ -0,0 +1,57 @@ +/* + * 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.ml; + +import org.apache.flink.annotation.PublicEvolving; + +import java.util.Arrays; + +/** + * Enum representing different types of machine learning tasks. Each task type has a corresponding + * name that can be used for identification. + */ +@PublicEvolving +public enum TaskType { + REGRESSION("regression"), + CLUSTERING("clustering"), + CLASSIFICATION("classification"), + EMBEDDING("embedding"), + TEXT_GENERATION("text_generation"); + + private final String name; + + TaskType(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static TaskType fromName(String name) { + return Arrays.stream(values()) + .filter(taskType -> taskType.name.equalsIgnoreCase(name)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Unknown task type: " + name)); + } + + public static boolean isValidTaskType(String name) { Review Comment: I think flink is case-sensitve. ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/ml/SqlMLTableFunction.java: ########## @@ -103,4 +126,193 @@ public SqlReturnTypeInference getRowTypeInference() { } protected abstract RelDataType inferRowType(SqlOperatorBinding opBinding); + + protected static Optional<RuntimeException> checkModelSignature( + SqlCallBinding callBinding, int inputDescriptorIndex, int outputDescriptorIndex) { + SqlValidator validator = callBinding.getValidator(); + + // Check second operand is SqlModelCall + if (!(callBinding.operand(1) instanceof SqlModelCall)) { + return Optional.of( + new ValidationException("Second operand must be a model identifier.")); + } + + // Get input descriptor columns + SqlCall descriptorCall = (SqlCall) callBinding.operand(inputDescriptorIndex); + List<SqlNode> descriptCols = descriptorCall.getOperandList(); + + // Get model input size + SqlModelCall modelCall = (SqlModelCall) callBinding.operand(1); + RelDataType modelInputType = modelCall.getInputType(validator); + + // Check sizes match + if (descriptCols.size() != modelInputType.getFieldCount()) { + return Optional.of( + new ValidationException( + String.format( + "Number of input descriptor columns (%d) does not match model input size (%d).", + descriptCols.size(), modelInputType.getFieldCount()))); + } + + // Check input types match + final RelDataType tableType = validator.getValidatedNodeType(callBinding.operand(0)); + final SqlNameMatcher matcher = validator.getCatalogReader().nameMatcher(); + for (int i = 0; i < descriptCols.size(); i++) { + Tuple3<Boolean, LogicalType, LogicalType> result = + checkModelDescriptorType( + tableType, + modelInputType.getFieldList().get(i).getType(), + descriptCols.get(i), + matcher); + if (!result.f0) { + return Optional.of( + new ValidationException( + String.format( + "Input descriptor column type %s cannot be assigned to model input type %s at position %d.", + result.f1, result.f2, i))); + } + } + + // Check output + if (outputDescriptorIndex > 0) { + descriptorCall = (SqlCall) callBinding.operand(outputDescriptorIndex); + descriptCols = descriptorCall.getOperandList(); + if (descriptCols.size() != 1) { + return Optional.of( + new ValidationException( + "Label descriptor must have exactly one column for evaluation.")); + } + RelDataType modelOutputType = modelCall.getOutputType(validator); + if (modelOutputType.getFieldCount() != 1) { + return Optional.of( + new ValidationException( + "Model output must have exactly one field for evaluation.")); + } + + Tuple3<Boolean, LogicalType, LogicalType> result = + checkModelDescriptorType( + tableType, + modelOutputType.getFieldList().get(0).getType(), + descriptCols.get(0), + matcher); + if (!result.f0) { + return Optional.of( + new ValidationException( + String.format( + "Label descriptor column type %s cannot be assigned to model output type %s for evaluation.", + result.f1, result.f2))); + } + } + + return Optional.empty(); + } + + private static Tuple3<Boolean, LogicalType, LogicalType> checkModelDescriptorType( + RelDataType tableType, + RelDataType modelType, + SqlNode descriptorNode, + SqlNameMatcher matcher) { + SqlIdentifier columnName = (SqlIdentifier) descriptorNode; + String descriptColName = Review Comment: descriptorColName ########## flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/ml/SqlMLTableFunction.java: ########## @@ -103,4 +126,193 @@ public SqlReturnTypeInference getRowTypeInference() { } protected abstract RelDataType inferRowType(SqlOperatorBinding opBinding); + + protected static Optional<RuntimeException> checkModelSignature( + SqlCallBinding callBinding, int inputDescriptorIndex, int outputDescriptorIndex) { + SqlValidator validator = callBinding.getValidator(); + + // Check second operand is SqlModelCall + if (!(callBinding.operand(1) instanceof SqlModelCall)) { + return Optional.of( + new ValidationException("Second operand must be a model identifier.")); + } + + // Get input descriptor columns + SqlCall descriptorCall = (SqlCall) callBinding.operand(inputDescriptorIndex); + List<SqlNode> descriptCols = descriptorCall.getOperandList(); + + // Get model input size + SqlModelCall modelCall = (SqlModelCall) callBinding.operand(1); + RelDataType modelInputType = modelCall.getInputType(validator); + + // Check sizes match + if (descriptCols.size() != modelInputType.getFieldCount()) { + return Optional.of( + new ValidationException( + String.format( + "Number of input descriptor columns (%d) does not match model input size (%d).", + descriptCols.size(), modelInputType.getFieldCount()))); + } + + // Check input types match + final RelDataType tableType = validator.getValidatedNodeType(callBinding.operand(0)); + final SqlNameMatcher matcher = validator.getCatalogReader().nameMatcher(); + for (int i = 0; i < descriptCols.size(); i++) { + Tuple3<Boolean, LogicalType, LogicalType> result = + checkModelDescriptorType( + tableType, + modelInputType.getFieldList().get(i).getType(), + descriptCols.get(i), + matcher); + if (!result.f0) { + return Optional.of( + new ValidationException( + String.format( + "Input descriptor column type %s cannot be assigned to model input type %s at position %d.", + result.f1, result.f2, i))); + } + } + + // Check output + if (outputDescriptorIndex > 0) { Review Comment: Actually, only ML_EVALUTE need to check this? It's better we can mode ML_EVALUTE logic to subclass. -- 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]
