rahil-c commented on code in PR #18432:
URL: https://github.com/apache/hudi/pull/18432#discussion_r3019184183


##########
hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/hudi/functional/TestHoodieVectorSearchFunction.scala:
##########
@@ -0,0 +1,1441 @@
+/*
+ * 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.hudi.functional
+
+import org.apache.hudi.DataSourceWriteOptions._
+import org.apache.hudi.common.schema.HoodieSchema
+import org.apache.hudi.testutils.HoodieSparkClientTestBase
+
+import org.apache.spark.sql.{Row, SaveMode, SparkSession}
+import org.apache.spark.sql.types._
+import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
+import org.junit.jupiter.api.Assertions._
+
+/**
+ * End-to-end tests for the hudi_vector_search table-valued function.
+ * Tests both single-query and batch-query modes with Spark SQL and DataFrame 
API.
+ */
+class TestHoodieVectorSearchFunction extends HoodieSparkClientTestBase {
+
+  var spark: SparkSession = null
+  private val corpusPath = "corpus"
+  private val corpusViewName = "corpus_view"
+
+  // Test corpus: 5 unit-ish vectors in 3D for easy manual verification
+  // doc_1: [1, 0, 0] - x-axis
+  // doc_2: [0, 1, 0] - y-axis
+  // doc_3: [0, 0, 1] - z-axis
+  // doc_4: [0.707, 0.707, 0] - 45 degrees in xy-plane (normalized)
+  // doc_5: [0.577, 0.577, 0.577] - equal in all 3 dims (normalized)
+  private val corpusData = Seq(
+    ("doc_1", Seq(1.0f, 0.0f, 0.0f), "x-axis"),
+    ("doc_2", Seq(0.0f, 1.0f, 0.0f), "y-axis"),
+    ("doc_3", Seq(0.0f, 0.0f, 1.0f), "z-axis"),
+    ("doc_4", Seq(0.70710678f, 0.70710678f, 0.0f), "xy-diagonal"),
+    ("doc_5", Seq(0.57735027f, 0.57735027f, 0.57735027f), "xyz-diagonal")
+  )
+
+  @BeforeEach override def setUp(): Unit = {
+    initPath()
+    initSparkContexts()
+    spark = sqlContext.sparkSession
+    initTestDataGenerator()
+    initHoodieStorage()
+    createCorpusTable()
+  }
+
+  @AfterEach override def tearDown(): Unit = {
+    spark.catalog.dropTempView(corpusViewName)
+    cleanupSparkContexts()
+    cleanupTestDataGenerator()
+    cleanupFileSystem()
+  }
+
+  private def createCorpusTable(): Unit = {
+    val metadata = new MetadataBuilder()
+      .putString(HoodieSchema.TYPE_METADATA_FIELD, "VECTOR(3)")
+      .build()
+
+    val schema = StructType(Seq(
+      StructField("id", StringType, nullable = false),
+      StructField("embedding", ArrayType(FloatType, containsNull = false),
+        nullable = false, metadata),
+      StructField("label", StringType, nullable = true)
+    ))
+
+    val rows = corpusData.map { case (id, emb, label) =>
+      Row(id, emb, label)
+    }
+
+    val df = spark.createDataFrame(
+      spark.sparkContext.parallelize(rows),
+      schema
+    )
+
+    df.write.format("hudi")
+      .option(RECORDKEY_FIELD.key, "id")
+      .option(PRECOMBINE_FIELD.key, "id")
+      .option(TABLE_NAME.key, "vector_search_corpus")
+      .option(TABLE_TYPE.key, "COPY_ON_WRITE")
+      .mode(SaveMode.Overwrite)
+      .save(basePath + "/" + corpusPath)
+
+    spark.read.format("hudi").load(basePath + "/" + corpusPath)
+      .createOrReplaceTempView(corpusViewName)
+  }
+
+  // ==================== Single Query Mode: Spark SQL ====================
+
+  @Test
+  def testSingleQueryCosineDistance(): Unit = {
+    // Query vector [1, 0, 0] should be closest to doc_1, then doc_4, then 
doc_5
+    val result = spark.sql(
+      s"""
+         |SELECT id, label, _distance
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  ARRAY(1.0, 0.0, 0.0),
+         |  3,
+         |  'cosine'
+         |)
+         |ORDER BY _distance
+         |""".stripMargin
+    ).collect()
+
+    assertEquals(3, result.length)
+
+    // doc_1 [1,0,0]: cosine distance to [1,0,0] = 1 - 1.0 = 0.0
+    assertEquals("doc_1", result(0).getAs[String]("id"))
+    assertEquals(0.0, result(0).getAs[Double]("_distance"), 1e-5)
+
+    // doc_4 [0.707,0.707,0]: cosine distance to [1,0,0] = 1 - 0.707 ~= 0.293
+    assertEquals("doc_4", result(1).getAs[String]("id"))
+    assertEquals(1.0 - 0.70710678, result(1).getAs[Double]("_distance"), 1e-4)
+
+    // doc_5 [0.577,0.577,0.577]: cosine distance to [1,0,0] = 1 - 0.577 ~= 
0.423
+    assertEquals("doc_5", result(2).getAs[String]("id"))
+    assertEquals(1.0 - 0.57735027, result(2).getAs[Double]("_distance"), 1e-4)
+  }
+
+  @Test
+  def testSingleQueryL2Distance(): Unit = {
+    // Query [1, 0, 0] with L2
+    val result = spark.sql(
+      s"""
+         |SELECT id, _distance
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  ARRAY(1.0, 0.0, 0.0),
+         |  3,
+         |  'l2'
+         |)
+         |ORDER BY _distance
+         |""".stripMargin
+    ).collect()
+
+    assertEquals(3, result.length)
+
+    // doc_1: L2 = 0.0
+    assertEquals("doc_1", result(0).getAs[String]("id"))
+    assertEquals(0.0, result(0).getAs[Double]("_distance"), 1e-5)
+
+    // doc_4: L2 = sqrt((1-0.707)^2 + (0-0.707)^2 + 0) = sqrt(0.086 + 0.5) ~= 
0.765
+    assertEquals("doc_4", result(1).getAs[String]("id"))
+    val expectedL2Doc4 = math.sqrt(
+      math.pow(1.0 - 0.70710678, 2) + math.pow(0.70710678, 2))
+    assertEquals(expectedL2Doc4, result(1).getAs[Double]("_distance"), 1e-4)
+  }
+
+  @Test
+  def testSingleQueryDotProduct(): Unit = {
+    // Query [1, 0, 0] with dot_product (negated: lower = more similar)
+    val result = spark.sql(
+      s"""
+         |SELECT id, _distance
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  ARRAY(1.0, 0.0, 0.0),
+         |  3,
+         |  'dot_product'
+         |)
+         |ORDER BY _distance
+         |""".stripMargin
+    ).collect()
+
+    assertEquals(3, result.length)
+
+    // doc_1: -dot = -1.0 (most similar)
+    assertEquals("doc_1", result(0).getAs[String]("id"))
+    assertEquals(-1.0, result(0).getAs[Double]("_distance"), 1e-5)
+
+    // doc_4: -dot = -0.707
+    assertEquals("doc_4", result(1).getAs[String]("id"))
+    assertEquals(-0.70710678, result(1).getAs[Double]("_distance"), 1e-4)
+  }
+
+  @Test
+  def testSingleQueryDefaultMetric(): Unit = {
+    // Omit metric arg, should default to cosine
+    val result = spark.sql(
+      s"""
+         |SELECT id, _distance
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  ARRAY(1.0, 0.0, 0.0),
+         |  3
+         |)
+         |ORDER BY _distance
+         |""".stripMargin
+    ).collect()
+
+    assertEquals(3, result.length)
+    // Should match cosine: doc_1 first with distance ~0
+    assertEquals("doc_1", result(0).getAs[String]("id"))
+    assertEquals(0.0, result(0).getAs[Double]("_distance"), 1e-5)
+  }
+
+  @Test
+  def testSingleQueryReturnsAllCorpusColumns(): Unit = {
+    val result = spark.sql(
+      s"""
+         |SELECT *
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  ARRAY(1.0, 0.0, 0.0),
+         |  2
+         |)
+         |""".stripMargin
+    )
+
+    // Should have the _distance column plus original corpus columns 
(embedding is dropped)
+    assertTrue(result.columns.contains("_distance"))
+    assertTrue(result.columns.contains("id"))
+    assertTrue(result.columns.contains("label"))
+    assertFalse(result.columns.contains("embedding"))
+    assertEquals(2, result.count())
+  }
+
+  @Test
+  def testKGreaterThanCorpus(): Unit = {
+    // k=100, corpus has 5 rows -> should return all 5
+    val result = spark.sql(
+      s"""
+         |SELECT id, _distance
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  ARRAY(1.0, 0.0, 0.0),
+         |  100
+         |)
+         |""".stripMargin
+    ).collect()
+
+    assertEquals(5, result.length)
+  }
+
+  // ==================== Single Query Mode: Composability ====================
+
+  @Test
+  def testVectorSearchWithWhereClause(): Unit = {
+    val result = spark.sql(
+      s"""
+         |SELECT id, _distance
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  ARRAY(1.0, 0.0, 0.0),
+         |  5,
+         |  'cosine'
+         |)
+         |WHERE _distance < 0.5
+         |ORDER BY _distance
+         |""".stripMargin
+    ).collect()
+
+    // doc_1 (distance ~0) and doc_4 (distance ~0.29) should pass; doc_5 
(~0.42) should pass too
+    // doc_2 and doc_3 have distance = 1.0 and should be filtered out
+    assertTrue(result.length >= 2)
+    assertTrue(result.forall(_.getAs[Double]("_distance") < 0.5))
+  }
+
+  @Test
+  def testVectorSearchAsSubquery(): Unit = {
+    val result = spark.sql(
+      s"""
+         |SELECT sub.id, sub.label, sub._distance
+         |FROM (
+         |  SELECT *
+         |  FROM hudi_vector_search(
+         |    '$corpusViewName',
+         |    'embedding',
+         |    ARRAY(0.0, 1.0, 0.0),
+         |    3
+         |  )
+         |) sub
+         |WHERE sub.label != 'y-axis'
+         |ORDER BY sub._distance
+         |""".stripMargin
+    ).collect()
+
+    // doc_2 (y-axis) is filtered out
+    assertTrue(result.forall(_.getAs[String]("id") != "doc_2"))
+  }
+
+  // ==================== Batch Query Mode ====================
+
+  @Test
+  def testBatchQueryMode(): Unit = {
+    // Create a query table with 2 queries
+    val querySchema = StructType(Seq(
+      StructField("query_name", StringType, nullable = false),
+      StructField("query_embedding", ArrayType(FloatType, containsNull = 
false), nullable = false)
+    ))
+
+    val queryRows = Seq(
+      Row("q_x", Seq(1.0f, 0.0f, 0.0f)),     // should find doc_1 closest
+      Row("q_y", Seq(0.0f, 1.0f, 0.0f))      // should find doc_2 closest
+    )
+
+    spark.createDataFrame(
+      spark.sparkContext.parallelize(queryRows), querySchema
+    ).createOrReplaceTempView("queries_view")
+
+    val result = spark.sql(
+      s"""
+         |SELECT *
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  'queries_view',
+         |  'query_embedding',
+         |  2,
+         |  'cosine'
+         |)
+         |""".stripMargin
+    ).collect()
+
+    // 2 queries x 2 results each = 4 rows
+    assertEquals(4, result.length)
+
+    // Check that _distance column exists
+    val columns = result.head.schema.fieldNames
+    assertTrue(columns.contains("_distance"))
+    assertTrue(columns.contains("_query_id"))
+
+    spark.catalog.dropTempView("queries_view")
+  }
+
+  @Test
+  def testBatchQueryResultsPerQuery(): Unit = {
+    val querySchema = StructType(Seq(
+      StructField("qid", StringType, nullable = false),
+      StructField("qvec", ArrayType(FloatType, containsNull = false), nullable 
= false)
+    ))
+
+    val queryRows = Seq(
+      Row("q1", Seq(1.0f, 0.0f, 0.0f)),
+      Row("q2", Seq(0.0f, 0.0f, 1.0f))
+    )
+
+    spark.createDataFrame(
+      spark.sparkContext.parallelize(queryRows), querySchema
+    ).createOrReplaceTempView("batch_queries")
+
+    val resultDf = spark.sql(
+      s"""
+         |SELECT *
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  'batch_queries',
+         |  'qvec',
+         |  2,
+         |  'cosine'
+         |)
+         |""".stripMargin
+    )
+
+    // Each query should get 2 results
+    val resultsByQuery = resultDf.groupBy("_query_id").count().collect()
+    assertEquals(2, resultsByQuery.length)
+    resultsByQuery.foreach { row =>
+      assertEquals(2, row.getLong(1))
+    }
+
+    spark.catalog.dropTempView("batch_queries")
+  }
+
+  @Test
+  def testBatchQuerySameEmbeddingColumnName(): Unit = {
+    // Both corpus and query use the column name "embedding" — previously 
caused ambiguity error
+    val querySchema = StructType(Seq(
+      StructField("query_name", StringType, nullable = false),
+      StructField("embedding", ArrayType(FloatType, containsNull = false), 
nullable = false)
+    ))
+
+    spark.createDataFrame(
+      spark.sparkContext.parallelize(Seq(
+        Row("q_x", Seq(1.0f, 0.0f, 0.0f)),
+        Row("q_y", Seq(0.0f, 1.0f, 0.0f))
+      )), querySchema
+    ).createOrReplaceTempView("same_col_queries")
+
+    val result = spark.sql(
+      s"""
+         |SELECT *
+         |FROM hudi_vector_search(
+         |  '$corpusViewName',
+         |  'embedding',
+         |  'same_col_queries',
+         |  'embedding',
+         |  2,
+         |  'cosine'
+         |)
+         |""".stripMargin
+    ).collect()
+
+    // 2 queries x 2 results each = 4 rows; should not throw AnalysisException
+    assertEquals(4, result.length)
+    assertTrue(result.head.schema.fieldNames.contains("_distance"))
+
+    spark.catalog.dropTempView("same_col_queries")
+  }
+
+  // ==================== DataFrame API ====================

Review Comment:
   remove comment



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