steFaiz commented on code in PR #7550: URL: https://github.com/apache/paimon/pull/7550#discussion_r3008880277
########## paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/procedure/VectorSearchProcedure.java: ########## @@ -0,0 +1,280 @@ +/* + * 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.paimon.flink.procedure; + +import org.apache.paimon.casting.CastExecutor; +import org.apache.paimon.casting.CastExecutors; +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.data.BinaryString; +import org.apache.paimon.data.InternalArray; +import org.apache.paimon.data.InternalMap; +import org.apache.paimon.data.InternalRow; +import org.apache.paimon.data.InternalVector; +import org.apache.paimon.globalindex.GlobalIndexResult; +import org.apache.paimon.reader.RecordReader; +import org.apache.paimon.table.Table; +import org.apache.paimon.table.source.ReadBuilder; +import org.apache.paimon.table.source.TableScan; +import org.apache.paimon.types.ArrayType; +import org.apache.paimon.types.DataField; +import org.apache.paimon.types.DataType; +import org.apache.paimon.types.DataTypeRoot; +import org.apache.paimon.types.DataTypes; +import org.apache.paimon.types.MapType; +import org.apache.paimon.types.RowType; +import org.apache.paimon.types.VectorType; +import org.apache.paimon.utils.InternalRowUtils; +import org.apache.paimon.utils.JsonSerdeUtil; +import org.apache.paimon.utils.StringUtils; + +import org.apache.flink.table.annotation.ArgumentHint; +import org.apache.flink.table.annotation.DataTypeHint; +import org.apache.flink.table.annotation.ProcedureHint; +import org.apache.flink.table.procedure.ProcedureContext; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Vector search procedure. This procedure takes one vector and searches for topK nearest vectors. + * Usage: + * + * <pre><code> + * CALL sys.vector_search( + * `table` => 'tableId', + * vector_column => 'v', + * query_vector => '1.0,2.0,3.0', + * top_k => 5 + * ) + * + * -- with projection and options + * CALL sys.vector_search( + * `table` => 'tableId', + * vector_column => 'v', + * query_vector => '1.0,2.0,3.0', + * top_k => 5, + * projection => 'id,name', + * options => 'k1=v1;k2=v2' + * ) + * </code></pre> + */ +public class VectorSearchProcedure extends ProcedureBase { + + public static final String IDENTIFIER = "vector_search"; + + private static final Base64.Encoder BASE64_ENCODER = Base64.getEncoder(); + + @ProcedureHint( + argument = { + @ArgumentHint(name = "table", type = @DataTypeHint("STRING")), + @ArgumentHint(name = "vector_column", type = @DataTypeHint("STRING")), + @ArgumentHint(name = "query_vector", type = @DataTypeHint("STRING")), + @ArgumentHint(name = "top_k", type = @DataTypeHint("INT")), + @ArgumentHint( + name = "projection", + type = @DataTypeHint("STRING"), + isOptional = true), + @ArgumentHint(name = "options", type = @DataTypeHint("STRING"), isOptional = true) + }) + public String[] call( + ProcedureContext procedureContext, + String tableId, + String vectorColumn, + String queryVectorStr, + Integer topK, + String projection, + String options) + throws Exception { + Table table = catalog.getTable(Identifier.fromString(tableId)); + + Map<String, String> optionsMap = optionalConfigMap(options); + if (!optionsMap.isEmpty()) { + table = table.copy(optionsMap); + } + + float[] queryVector = parseVector(queryVectorStr); + + GlobalIndexResult result = + table.newVectorSearchBuilder() + .withVector(queryVector) + .withVectorColumn(vectorColumn) + .withLimit(topK) + .executeLocal(); + + RowType tableRowType = table.rowType(); + int[] projectionIndices = parseProjection(projection, tableRowType); + + ReadBuilder readBuilder = table.newReadBuilder(); + if (projectionIndices != null) { + readBuilder.withProjection(projectionIndices); + } + + TableScan.Plan plan = readBuilder.newScan().withGlobalIndexResult(result).plan(); + + RowType readType = + projectionIndices != null + ? buildProjectedRowType(tableRowType, projectionIndices) + : tableRowType; + + List<String> rows = new ArrayList<>(); + try (RecordReader<InternalRow> reader = readBuilder.newRead().createReader(plan)) { + reader.forEachRemaining( + row -> { + try { + rows.add(convertRowToJsonString(row, readType)); + } catch (Exception e) { + throw new RuntimeException("Failed to convert row to JSON string", e); + } + }); + } + + return rows.toArray(new String[0]); + } + + private static float[] parseVector(String vectorStr) { + String[] parts = vectorStr.split(","); + float[] vector = new float[parts.length]; + for (int i = 0; i < parts.length; i++) { + vector[i] = Float.parseFloat(parts[i].trim()); + } + return vector; + } + + private static int[] parseProjection(String projection, RowType rowType) { + if (StringUtils.isNullOrWhitespaceOnly(projection)) { + return null; + } + String[] columns = projection.split(","); + int[] indices = new int[columns.length]; + List<String> fieldNames = rowType.getFieldNames(); + for (int i = 0; i < columns.length; i++) { Review Comment: Thanks! modified. -- 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]
