fsk119 commented on code in PR #27039:
URL: https://github.com/apache/flink/pull/27039#discussion_r2428816931


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/sql/ml/SqlVectorSearchTableFunction.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.flink.table.planner.functions.utils.SqlValidatorUtils;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.utils.LogicalTypeCasts;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeFieldImpl;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCallBinding;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperandCountRange;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlOperatorBinding;
+import org.apache.calcite.sql.SqlTableFunction;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlOperandCountRanges;
+import org.apache.calcite.sql.type.SqlOperandMetadata;
+import org.apache.calcite.sql.type.SqlReturnTypeInference;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.validate.SqlNameMatcher;
+import org.apache.calcite.util.Util;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+import static 
org.apache.flink.table.planner.calcite.FlinkTypeFactory.toLogicalType;
+
+/**
+ * {@link SqlVectorSearchTableFunction} implements an operator for search.
+ *
+ * <p>It allows four parameters:
+ *
+ * <ol>
+ *   <li>a table
+ *   <li>a descriptor to provide a column name from the input table
+ *   <li>a query column from the left table
+ *   <li>a literal value for top k
+ * </ol>
+ */
+public class SqlVectorSearchTableFunction extends SqlFunction implements 
SqlTableFunction {
+
+    private static final String PARAM_SEARCH_TABLE = "SEARCH_TABLE";
+    private static final String PARAM_COLUMN_TO_SEARCH = "COLUMN_TO_SEARCH";
+    private static final String PARAM_COLUMN_TO_QUERY = "COLUMN_TO_QUERY";
+    private static final String PARAM_TOP_K = "TOP_K";
+
+    private static final String OUTPUT_SCORE = "score";
+
+    public SqlVectorSearchTableFunction() {
+        super(
+                "VECTOR_SEARCH",
+                SqlKind.OTHER_FUNCTION,
+                ReturnTypes.CURSOR,
+                null,
+                new OperandMetadataImpl(),
+                SqlFunctionCategory.SYSTEM);
+    }
+
+    @Override
+    public SqlReturnTypeInference getRowTypeInference() {
+        return new SqlReturnTypeInference() {
+            @Override
+            public @Nullable RelDataType inferReturnType(SqlOperatorBinding 
opBinding) {
+                final RelDataTypeFactory typeFactory = 
opBinding.getTypeFactory();
+                final RelDataType inputRowType = opBinding.getOperandType(0);
+
+                return typeFactory
+                        .builder()
+                        .kind(inputRowType.getStructKind())
+                        .addAll(inputRowType.getFieldList())
+                        .addAll(
+                                SqlValidatorUtils.makeOutputUnique(
+                                        inputRowType.getFieldList(),
+                                        Collections.singletonList(
+                                                new RelDataTypeFieldImpl(
+                                                        OUTPUT_SCORE,
+                                                        0,
+                                                        
typeFactory.createSqlType(
+                                                                
SqlTypeName.DOUBLE)))))
+                        .build();
+            }
+        };
+    }
+
+    @Override
+    public boolean argumentMustBeScalar(int ordinal) {
+        return ordinal != 0;
+    }
+
+    private static class OperandMetadataImpl implements SqlOperandMetadata {
+
+        private static final List<String> PARAMETERS =
+                Collections.unmodifiableList(
+                        Arrays.asList(
+                                PARAM_SEARCH_TABLE,
+                                PARAM_COLUMN_TO_SEARCH,
+                                PARAM_COLUMN_TO_QUERY,
+                                PARAM_TOP_K));

Review Comment:
   Of course, the document will be added when all tasks are finished. The 
current API doesn't support paging, I think we can leave this as the future 
work. 



-- 
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]

Reply via email to