rahil-c commented on code in PR #18432: URL: https://github.com/apache/hudi/pull/18432#discussion_r3033848990
########## hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/HoodieVectorSearchTableValuedFunction.scala: ########## @@ -0,0 +1,168 @@ +/* + * 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.spark.sql.catalyst.plans.logical + +import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression, Literal} +import org.apache.spark.sql.hudi.command.exception.HoodieAnalysisException +import org.apache.spark.sql.types.StringType + +object HoodieVectorSearchTableValuedFunction { + + val FUNC_NAME = "hudi_vector_search" + + object DistanceMetric extends Enumeration { + val COSINE, L2, DOT_PRODUCT = Value + + def fromString(s: String): Value = Option(s).map(_.toLowerCase).getOrElse("") match { + case "cosine" => COSINE + case "l2" | "euclidean" => L2 + case "dot_product" | "inner_product" => DOT_PRODUCT + case other => throw new HoodieAnalysisException( + s"Unsupported distance metric: '$other'. Supported: cosine, l2, dot_product") + } + } + + object SearchAlgorithm extends Enumeration { + val BRUTE_FORCE = Value + + def fromString(s: String): Value = Option(s).map(_.toLowerCase).getOrElse("") match { + case "brute_force" => BRUTE_FORCE + case other => throw new HoodieAnalysisException( + s"Unsupported search algorithm: '$other'. Supported: brute_force") + } + } + + case class ParsedArgs( + tableName: String, + embeddingCol: String, + queryVectorExpr: Expression, + k: Int, + metric: DistanceMetric.Value, + algorithm: SearchAlgorithm.Value + ) + + /** + * Parse arguments for the hudi_vector_search TVF (single-query mode). + * + * Signature (4–6 args): + * hudi_vector_search('table', 'embedding_col', ARRAY(1.0, 2.0, ...), k [, 'metric'] [, 'algorithm']) + * metric defaults to 'cosine'; algorithm defaults to 'brute_force'. + */ + def parseArgs(exprs: Seq[Expression]): ParsedArgs = { + if (exprs.size < 4 || exprs.size > 6) { + throw new HoodieAnalysisException( + s"Function '$FUNC_NAME' expects 4-6 arguments: " + + "(table, embedding_col, query_vector, k [, metric] [, algorithm]).") + } + + def requireStringLiteral(expr: Expression, argName: String): String = expr match { + case Literal(v, StringType) if v != null => v.toString + case _ => throw new HoodieAnalysisException( + s"Function '$FUNC_NAME': argument '$argName' must be a string literal, got: ${expr.sql}") + } + + val tableName = requireStringLiteral(exprs.head, "table") + val embeddingCol = requireStringLiteral(exprs(1), "embedding_col") + val queryVectorExpr = exprs(2) + val k = parseK(FUNC_NAME, exprs(3)) + val metric = if (exprs.size >= 5) DistanceMetric.fromString(exprs(4).eval().toString) + else DistanceMetric.COSINE + val algorithm = if (exprs.size >= 6) SearchAlgorithm.fromString(exprs(5).eval().toString) + else SearchAlgorithm.BRUTE_FORCE + ParsedArgs(tableName, embeddingCol, queryVectorExpr, k, metric, algorithm) + } + Review Comment: yes let me use those helpers -- 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]
