rahil-c commented on code in PR #18432:
URL: https://github.com/apache/hudi/pull/18432#discussion_r3037483196
##########
hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/analysis/HoodieSparkBaseAnalysis.scala:
##########
@@ -310,6 +327,61 @@ case class ResolveReferences(spark: SparkSession) extends
Rule[LogicalPlan]
sparkAdapter.getCatalystPlanUtils.unapplyMergeIntoTable(plan)
}
+ /**
+ * Resolves a table reference to a DataFrame. Accepts either a table
identifier
+ * (including multi-part identifiers like catalog.db.table) or a file path.
+ */
+ private def resolveTableToDf(table: String): DataFrame = {
+ try {
+ if (table.contains(StoragePath.SEPARATOR)) {
+ spark.read.format("hudi").load(table)
+ } else {
+ spark.table(table)
+ }
+ } catch {
+ case e: Exception => throw new HoodieAnalysisException(
+ s"hudi_vector_search: unable to resolve table '$table':
${e.getMessage}")
+ }
+ }
+
+ private def evaluateQueryVector(expr: Expression): Array[Double] = {
+ if (!expr.foldable) {
+ throw new HoodieAnalysisException(
+ s"Function '${HoodieVectorSearchTableValuedFunction.FUNC_NAME}': " +
+ "query vector must be a constant expression (e.g., ARRAY(1.0, 2.0,
3.0))")
+ }
+ val value = expr.eval(null)
+ if (value == null) {
+ throw new HoodieAnalysisException(
+ s"Function '${HoodieVectorSearchTableValuedFunction.FUNC_NAME}': query
vector cannot be null")
+ }
+
+ val arrayData = value.asInstanceOf[ArrayData]
+ val numElements = arrayData.numElements()
+ val elementType = expr.dataType.asInstanceOf[ArrayType].elementType
+
+ // Resolve element extractor once, before the loop.
+ // Spark SQL infers untyped decimal literals (e.g. ARRAY(1.0, 0.5)) as
DecimalType,
+ // not DoubleType, so DecimalType is accepted and converted.
+ val getElement: Int => Double = elementType match {
+ case DoubleType => i => arrayData.getDouble(i)
+ case FloatType => i => arrayData.getFloat(i).toDouble
+ case IntegerType => i => arrayData.getInt(i).toDouble
+ case LongType => i => arrayData.getLong(i).toDouble
+ case d: DecimalType => i => arrayData.getDecimal(i, d.precision,
d.scale).toDouble
+ case other => throw new HoodieAnalysisException(
+ s"Function '${HoodieVectorSearchTableValuedFunction.FUNC_NAME}': " +
+ s"query vector element type $other not supported, expected numeric
array")
+ }
+
+ (0 until numElements).map { i =>
+ if (arrayData.isNullAt(i)) throw new HoodieAnalysisException(
+ s"Function '${HoodieVectorSearchTableValuedFunction.FUNC_NAME}': " +
+ s"query vector element at index $i is null")
+ getElement(i)
+ }.toArray
+ }
Review Comment:
ok
--
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]