fix: route StringReplace through codegen dispatcher to fix empty-search divergence#4537
Open
andygrove wants to merge 2 commits into
Open
fix: route StringReplace through codegen dispatcher to fix empty-search divergence#4537andygrove wants to merge 2 commits into
andygrove wants to merge 2 commits into
Conversation
…pty literal [skip ci] DataFusion's `replace` inserts the replacement between every character and at both boundaries when the search string is empty, but Spark short-circuits and returns the source unchanged. The new `CometStringReplace` serde detects an empty literal search and routes through `CometScalaUDF.emitJvmCodegenDispatch` so Spark's own `doGenCode` handles the case. Users can opt back into the native (divergent) path with `spark.comet.expression.StringReplace.allowIncompatible=true`. Closes apache#4497.
…owIncompatible [skip ci] The empty-search divergence affects column-valued empty strings too, not just literal empty searches. Drop the literal-only plan-time check and route every StringReplace through `CometScalaUDF.emitJvmCodegenDispatch` by default, mirroring the pattern used by `CometInitCap`. Users can opt back into the native path with `spark.comet.expression.StringReplace.allowIncompatible=true`. Test data now includes a row with an empty search column value to cover the non-literal case.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #4497.
Rationale for this change
replace(str, search, replacement)diverges from Spark when the search string is empty. Spark short-circuits and returns the source unchanged (UTF8String.replacecheckssearch.numBytes == 0). DataFusion'sreplaceinstead inserts the replacement between every character and at both boundaries, producing e.g.replace('hello', '', 'x')->xhxexlxlxox. The bug applies to both literal empty searches and column-valued empty strings at runtime.What changes are included in this PR?
CometStringReplaceserde inspark/src/main/scala/org/apache/comet/serde/strings.scala. By default it routes throughCometScalaUDF.emitJvmCodegenDispatchso Spark's owndoGenCoderuns inside the Comet pipeline. Users can opt back into the native (divergent) path withspark.comet.expression.StringReplace.allowIncompatible=true. The pattern mirrorsCometInitCap.QueryPlanSerdenow wiresStringReplacetoCometStringReplaceinstead of the inlineCometScalarFunction("replace").string_replace.sqlremoves thequery ignore(#3344)directive on the empty-literal case, adds rows for empty-source / NULL-source / NULL-replacement, and adds a row('hello', '', 'x')to the test table to exercise the column-valued empty-search case.Are these changes tested?
Yes.
CometSqlFileTestSuiteruns the updatedstring_replace.sqland passes on Spark 3.5. Compile verified for Spark 3.4 (Java 11), Spark 3.5 (Java 11), and Spark 4.0 (Java 17).Are there any user-facing changes?
The default behavior of
replacebecomes Spark-compatible for all empty-search cases. Plans that previously executedreplacenatively now route through the JVM codegen dispatcher whenspark.comet.exec.scalaUDF.codegen.enabled=true(the default), which is slightly slower than the native path but matches Spark exactly. The native path remains available behindspark.comet.expression.StringReplace.allowIncompatible=true.