You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/09/16 07:50:52 UTC

(datafusion-comet) 02/02: test: expand replace compatibility regression coverage (#5409)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-comet.git

commit 8c229a703ccb024a8b5b1b849a56ceef0b66a4bd
Author: sam-1112 <75...@users.noreply.github.com>
AuthorDate: Wed Sep 16 03:28:24 2026 +0000

    test: expand replace compatibility regression coverage (#5409)
    
    * test: expand replace compatibility regression coverage
    
    * test: make replace dispatcher assertion robust
    
    ---------
    
    Co-authored-by: Hung <hu...@Hungs-Mac-mini.local>
    Co-authored-by: Andy Grove <ag...@apache.org>
---
 .../expressions/string/string_replace.sql          |  22 +++-
 .../scala/org/apache/comet/CometCodegenSuite.scala | 118 +++++++++++++++++++++
 2 files changed, 139 insertions(+), 1 deletion(-)

diff --git a/spark/src/test/resources/sql-tests/expressions/string/string_replace.sql b/spark/src/test/resources/sql-tests/expressions/string/string_replace.sql
index ad09525b6d..db4800ca57 100644
--- a/spark/src/test/resources/sql-tests/expressions/string/string_replace.sql
+++ b/spark/src/test/resources/sql-tests/expressions/string/string_replace.sql
@@ -19,7 +19,7 @@ statement
 CREATE TABLE test_str_replace(s string, search string, replace string) USING parquet
 
 statement
-INSERT INTO test_str_replace VALUES ('hello world', 'world', 'there'), ('aaa', 'a', 'bb'), ('hello', 'xyz', 'abc'), ('', 'a', 'b'), (NULL, 'a', 'b'), ('hello', '', 'x')
+INSERT INTO test_str_replace VALUES ('hello world', 'world', 'there'), ('aaa', 'a', 'bb'), ('hello', 'xyz', 'abc'), ('', 'a', 'b'), (NULL, 'a', 'b'), ('hello', '', 'x'), ('aaaa', 'aa', 'x'), ('你好你好', '你好', 'X'), ('😀a😀', '😀', 'x')
 
 query
 SELECT replace(s, search, replace) FROM test_str_replace
@@ -41,6 +41,26 @@ SELECT replace(NULL, '', 'x')
 query
 SELECT replace('hello', '', NULL)
 
+-- Overlapping candidates: Spark replaces non-overlapping left-to-right
+-- ('aaaa' + 'aa' -> 'xx'). Compatibility coverage for overlapping candidates.
+query
+SELECT replace(s, 'aa', 'x') FROM test_str_replace WHERE s = 'aaaa'
+
+-- Multi-byte UTF-8 values. Compatibility coverage for multi-byte UTF-8 values.
+query
+SELECT replace(s, '你好', 'X') FROM test_str_replace WHERE s = '你好你好'
+
+query
+SELECT replace(s, '😀', 'x') FROM test_str_replace WHERE s = '😀a😀'
+
+-- Multi-byte replacement.
+query
+SELECT replace(s, '你好', '世界') FROM test_str_replace WHERE s = '你好你好'
+
+-- Replacement contains the search string; replacement is not applied recursively.
+query
+SELECT replace(s, 'aa', 'aaa') FROM test_str_replace WHERE s = 'aaaa'
+
 -- column + literal + literal
 query
 SELECT replace(s, 'world', 'there') FROM test_str_replace
diff --git a/spark/src/test/scala/org/apache/comet/CometCodegenSuite.scala b/spark/src/test/scala/org/apache/comet/CometCodegenSuite.scala
index 403007bca0..149ee7a145 100644
--- a/spark/src/test/scala/org/apache/comet/CometCodegenSuite.scala
+++ b/spark/src/test/scala/org/apache/comet/CometCodegenSuite.scala
@@ -139,6 +139,29 @@ class CometCodegenSuite
     }
   }
 
+  /**
+   * Whether `replace` was routed through the JVM codegen dispatcher. Inspects the expression
+   * collection rather than formatted explain text: `rollUpInfoMessages` concatenates sibling
+   * names alphabetically (`cast, divide, ..., replace`), so a substring `"JVM codegen dispatcher:
+   * replace"` misses the case where `replace` is not first.
+   */
+  private def replaceIsDispatched(
+      plan: org.apache.spark.sql.execution.SparkPlan): (Seq[String], String) = {
+    val info = new ExtendedExplainInfo()
+    (info.getCodegenDispatchExpressions(plan), info.generateExtendedInfo(plan))
+  }
+
+  private def assertReplaceDispatch(
+      df: org.apache.spark.sql.DataFrame,
+      expectDispatcher: Boolean,
+      clue: String): Unit = {
+    checkSparkAnswerAndOperator(df)
+    val (dispatched, explain) = replaceIsDispatched(df.queryExecution.executedPlan)
+    assert(
+      dispatched.contains("replace") == expectDispatcher,
+      s"$clue, got dispatched expressions: $dispatched\n$explain")
+  }
+
   test("codegen kernel round-trips CalendarIntervalType") {
     val input = new IntervalMonthDayNanoVector("in", CometArrowAllocator)
     val field =
@@ -568,6 +591,101 @@ class CometCodegenSuite
     }
   }
 
+  test("replace compatibility boundary cases stay on JVM codegen dispatcher") {
+    withSQLConf(
+      CometConf.COMET_SCALA_UDF_CODEGEN_ENABLED.key -> "true",
+      CometConf.COMET_EXPLAIN_CODEGEN_ENABLED.key -> "true",
+      CometConf.COMET_EXEC_PROJECT_ENABLED.key -> "true",
+      CometConf.COMET_EXTENDED_EXPLAIN_FORMAT.key ->
+        CometConf.COMET_EXTENDED_EXPLAIN_FORMAT_VERBOSE) {
+
+      // Malformed search: CometLiteral would normalize 0xFF to U+FFFD, incorrectly matching
+      // a well-formed U+FFFD in the source.
+      withTable("t") {
+        sql("CREATE TABLE t (s STRING) USING parquet")
+        sql("INSERT INTO t VALUES ('\uFFFD'), ('ok')")
+        assertReplaceDispatch(
+          sql("SELECT replace(s, CAST(X'FF' AS STRING), 'x') FROM t"),
+          expectDispatcher = true,
+          "expected dispatcher path for malformed search literal")
+      }
+
+      // Malformed replacement has the same serialization hazard.
+      withTable("t") {
+        sql("CREATE TABLE t (s STRING) USING parquet")
+        sql("INSERT INTO t VALUES ('a'), ('b')")
+        assertReplaceDispatch(
+          sql("SELECT replace(s, 'a', CAST(X'FF' AS STRING)) FROM t"),
+          expectDispatcher = true,
+          "expected dispatcher path for malformed replacement literal")
+      }
+
+      // Spark skips replacement evaluation when src is NULL; native evaluates every child.
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+        withTable("t") {
+          sql("CREATE TABLE t (s STRING, n INT) USING parquet")
+          sql("INSERT INTO t VALUES (NULL, 0), ('a', 1)")
+          assertReplaceDispatch(
+            sql("SELECT replace(s, 'a', CAST(1 / n AS STRING)) FROM t"),
+            expectDispatcher = true,
+            "expected dispatcher path for throwing replacement expression")
+        }
+      }
+
+      // A 256 KiB scalar replacement overflows Arrow Utf8 offsets when broadcast to 8192 rows.
+      withTable("t") {
+        sql("CREATE TABLE t (s STRING) USING parquet")
+        sql("INSERT INTO t VALUES ('hello')")
+        assertReplaceDispatch(
+          sql("SELECT replace(s, 'notfound', repeat('x', 262144)) FROM t"),
+          expectDispatcher = true,
+          "expected dispatcher path for oversized replacement literal")
+      }
+
+      // Source is not on the whitelist: Spark short-circuits inside substring when s is NULL.
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+        withTable("t") {
+          sql("CREATE TABLE t (s STRING, n INT) USING parquet")
+          sql("INSERT INTO t VALUES (NULL, 0), ('a', 1)")
+          assertReplaceDispatch(
+            sql("SELECT replace(substring(s, 1, CAST(1 / n AS INT)), 'a', 'x') FROM t"),
+            expectDispatcher = true,
+            "expected dispatcher path for throwing expression nested in source")
+        }
+      }
+
+      // Malformed source literal: same CometLiteral byte-normalization as search/replacement.
+      withTable("t") {
+        sql("CREATE TABLE t (r STRING) USING parquet")
+        sql("INSERT INTO t VALUES ('x')")
+        assertReplaceDispatch(
+          sql("SELECT replace(CAST(X'FF' AS STRING), 'a', r) FROM t"),
+          expectDispatcher = true,
+          "expected dispatcher path for malformed source literal")
+      }
+
+      // Malformed literal nested under concat is still in the source tree.
+      withTable("t") {
+        sql("CREATE TABLE t (r STRING) USING parquet")
+        sql("INSERT INTO t VALUES ('x')")
+        assertReplaceDispatch(
+          sql("SELECT replace(concat(CAST(X'FF' AS STRING), r), 'a', 'x') FROM t"),
+          expectDispatcher = true,
+          "expected dispatcher path for malformed literal nested in source")
+      }
+
+      // Oversized source literal has the same broadcast / offset-overflow hazard.
+      withTable("t") {
+        sql("CREATE TABLE t (r STRING) USING parquet")
+        sql("INSERT INTO t VALUES ('x')")
+        assertReplaceDispatch(
+          sql("SELECT replace(repeat('x', 262144), 'notfound', r) FROM t"),
+          expectDispatcher = true,
+          "expected dispatcher path for oversized source literal")
+      }
+    }
+  }
+
   test("codegen dispatch fallback reasons name the expression") {
     // Flag-off short-circuit tags the expression `<name>: <reason>` so distinct expressions
     // don't collapse in the `Set[String]` roll-up.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org