Repository navigation
[SPARK-62675][SQL] Pad CHAR columns compared with a subquery that returns a constant string - #61186
Open
linhongliu-db wants to merge 3 commits into
Open
linhongliu-db wants to merge 3 commits into
linhongliu-db wants to merge 3 commits into
Conversation
…urns a constant string A CHAR column compared with a string literal is padded, but compared with an uncorrelated subquery that returns a constant string, such as `(SELECT 'US')`, a constant read through a CTE, an IN list with such a subquery element, or `IN (SELECT 'US')`, it is not, so padded rows stop matching. Pad such subqueries like the constant they return. Co-authored-by: Isaac <no-reply@databricks.com>
Follow the output column of the subquery through any chain of projections, subquery aliases and CTE references over a one-row relation, so that e.g. `c = (SELECT 'US' FROM (SELECT 1) s)` is padded too. Test that the literal elements of a mixed IN list are padded. Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
linhongliu-db
marked this pull request as ready for review
December 4, 2026 22:48
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?
When a CHAR(n) column is compared with a string literal,
ApplyCharTypePaddingpads the shorter side to the longer length, soc = 'US'matches a stored'US '. This PR pads the same way when the other side is an uncorrelated subquery that returns a constant string:c = (SELECT 'US'), and<>,<, ... on either side of the comparisonWITH k AS (SELECT 'US' AS v) ... c = (SELECT v FROM k)orc = (SELECT 'US' FROM (SELECT 1) s)c IN ('XX', (SELECT 'US'))c IN (SELECT 'US')andc NOT IN (SELECT 'US')In
ApplyCharTypePaddingHelper, a newConstantStringextractor matches a foldable expression, as before, or an uncorrelated scalar subquery whose plan returns exactly one row (a chain ofProject,SubqueryAlias,WithCTEand non-recursiveCTERelationRefnodes over aOneRowRelation; CTE definitions are looked up in the plan) and whose single output column, followed down through the chain, is a foldable STRING. The constant's value gives the length, and the existing literal logic pads the column and the subquery. A newInSubquerycase pads the value, and wraps the list query in a projection that pads its output when the constant is shorter.IN_SUBQUERYis added to the pruning patterns. If the constant fails to evaluate, it is left unpadded rather than failing the analysis.Subqueries that return a column (
c = (SELECT string_col FROM t)) are still compared without padding, likec = string_col.Both the fixed-point analyzer and the single-pass resolver apply padding through the
ApplyCharTypePaddingpost-hoc rule, so both are covered.This is related to SPARK-61978 (#60661), which reworks the IN-list cases of the same function and leaves IN subqueries out. This change does not depend on it. If both land, a constant subquery element of an IN list should be grouped with the literals.
Why are the changes needed?
Without padding, padded rows silently stop matching:
=andINdrop them, and<>andNOT INkeep them. The optimizer later inlines(SELECT 'US')into the literal'US', but padding was already decided during analysis, so the final plan compares the padded column with an unpadded constant.Does this PR introduce any user-facing change?
Yes, it fixes a correctness bug. Comparisons of a CHAR column with a subquery that returns a constant string now return the same rows as the comparison with the string literal. Comparisons with non-constant STRING subqueries and columns are unchanged.
How was this patch tested?
New test in
CharVarcharTestSuite, which runs for the file source, file source scan, DSv2 and Hive variants. It covers=,<>,<, a longer constant, a NULL constant, CTE constants, constants projected over a one-row subquery, IN lists that mix literals with subquery elements (checking that the literals are padded too), IN and NOT IN subqueries, with and without read-side padding. Each result is checked against the literal form, and a negative case checks that STRING-column subqueries stay unpadded. The test fails on master and passes with this change.New
ApplyCharTypePaddingHelperSuitein catalyst runs the padding rewrite on resolved plans. It checks the exact rewritten expressions for scalar subqueries (both sides,<, a longer constant, and with the column padded too), constants read through one-row projections, subquery aliases and CTEs, mixed IN lists, and IN subqueries. It also checks the plans that must stay unchanged: subqueries returning a column, a constant over a relation with any number of rows, correlated subqueries, a NULL constant, and non-CHAR columns. On master its four padding tests fail.Also ran
ResolveSubquerySuite,AnalysisSuite,FileSourceCharVarcharTestSuite,FileSourceCharVarcharScanTestSuite,DSV2CharVarcharTestSuite,BasicCharVarcharTestSuite,CharVarcharScanNormalizationSuite,HiveCharVarcharTestSuite,CharVarcharReadNormalizationSuite, andSQLQueryTestSuite -z char/-z subquery.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
This pull request and its description were written by Isaac.