Repository navigation
[SPARK-58782][SQL][4.0] Fix DSv2 pushdown null literal serialization bug - #61177
Open
RonakkGovani wants to merge 1 commit into
Open
RonakkGovani wants to merge 1 commit into
RonakkGovani wants to merge 1 commit into
Conversation
This PR fixes a bug where null literals were incorrectly serialized as the string 'null' instead of the SQL keyword NULL when pushed down to external databases via DSv2 (JDBC aggregate/predicate pushdown). `LiteralValue.toString` - the only toString for DSv2's only concrete Literal, and the path every pushdown rendering (explain strings, generated SQL) ultimately goes through, matched on dataType even when the value was null, instead of checking for null first. This produced two distinct, user-visible bugs: * For StringType, a null value rendered as the quoted string 'null' instead of NULL. When pushed down (e.g. NULLIF(s, ''), which rewrites to If(s = '', null, s)), the empty-string row was grouped under the literal string "null" instead of collapsing into the real NULL group — the external database executed a different grouping key than intended, producing 3 result rows instead of 2. * For BinaryType, the branch ran assert(value.isInstanceOf[Array[Byte]]), which fails on null and threw an AssertionError when rendering a pushed expression containing a null binary literal, rather than merely producing bad SQL. Yes - Query results will now be correct when null literals are pushed down to external databases via JDBC. _Before the fix:_ A query with NULLIF(col, '') in a GROUP BY would push down CASE WHEN col = '' THEN 'null' ELSE col END to the database, creating a spurious 'null' string group instead of a NULL group. _After the fix:_ The same query pushes down CASE WHEN col = '' THEN NULL ELSE col END, correctly collapsing empty strings into the NULL group as intended. Null binary literals render as NULL without error. * Unit tests in LiteralValueSuite covering all data types * End-to-end regression test in JDBCV2Suite with H2 database * Updated existing test expectations that relied on the old (incorrect) behavior No Closes apache#58023 from pratham76/SPARK-58782. Authored-by: Pratham Manja <prathammanja76@gmail.com> Signed-off-by: Szehon Ho <szehon.apache@gmail.com> (cherry picked from commit 01b0663) Resolved conflicts for branch-4.0: kept its StringUtils.replace and Hex.encodeHexString helpers in LiteralValue.toString, and created the null_literal table in the existing JDBCV2Suite setup. Co-authored-by: Isaac <no-reply@databricks.com>
This branch has not been deployed
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.
What changes were proposed in this pull request?
Backport of #58023 to branch-4.0.
LiteralValue.toStringmatched on the data type even when the value was null, so a null literal rendered as'null'for strings and failed an assertion forBINARY. It now renders a null value asNULLfirst.The cherry-pick had conflicts on branch-4.0, which I resolved without other changes:
LiteralValue.toStringkeeps itsStringUtils.replaceandHex.encodeHexStringhelpers inside the new null check, andJDBCV2Suitecreates thenull_literaltable in its existing setup instead of the batched setup of master.Why are the changes needed?
Without it, branch-4.0 has both bugs that #58023 fixed on master, branch-4.x and branch-4.3. For example, SPARK-62633 reports that on 4.1.2 pushing down
bin_col IN (X'31', NULL)to a DSv2 source such as Iceberg fails withINTERNAL_ERRORfromjava.lang.AssertionErrorinLiteralValue.toString, and a pushed-downNULLIF(s, '')groups the empty string under a'null'string group.Does this PR introduce any user-facing change?
Yes. A null literal in a pushed-down DSv2 expression renders as
NULL, so pushing down a nullBINARYliteral no longer fails, and JDBC pushdown no longer compares with the string'null'.How was this patch tested?
Ran
LiteralValueSuite, including the nullBinaryTypecase, andJDBCV2Suiteon branch-4.0.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (Claude Opus 5.5), used to prepare this backport. The change itself is #58023 by @pratham76.
This pull request and its description were written by Isaac.