Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

[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
apache:masterfrom
linhongliu-db:fix-char-constant-subquery-padding
Open

linhongliu-db wants to merge 3 commits into
apache:masterfrom
linhongliu-db:fix-char-constant-subquery-padding

Conversation

@linhongliu-db

@linhongliu-db linhongliu-db commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

When a CHAR(n) column is compared with a string literal, ApplyCharTypePadding pads the shorter side to the longer length, so c = '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 comparison
  • a constant read through a CTE, a subquery alias or a one-row projection, e.g. WITH k AS (SELECT 'US' AS v) ... c = (SELECT v FROM k) or c = (SELECT 'US' FROM (SELECT 1) s)
  • an IN list whose elements are literals or such subqueries, e.g. c IN ('XX', (SELECT 'US'))
  • c IN (SELECT 'US') and c NOT IN (SELECT 'US')

In ApplyCharTypePaddingHelper, a new ConstantString extractor matches a foldable expression, as before, or an uncorrelated scalar subquery whose plan returns exactly one row (a chain of Project, SubqueryAlias, WithCTE and non-recursive CTERelationRef nodes over a OneRowRelation; 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 new InSubquery case pads the value, and wraps the list query in a projection that pads its output when the constant is shorter. IN_SUBQUERY is 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, like c = string_col.

Both the fixed-point analyzer and the single-pass resolver apply padding through the ApplyCharTypePadding post-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: = and IN drop them, and <> and NOT IN keep 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.

CREATE TABLE customers (id INT, name STRING, country CHAR(5)) USING parquet;
INSERT INTO customers VALUES (1, 'alice', 'US'), (2, 'bob', 'DE'), (3, 'carol', 'US');
SELECT name FROM customers WHERE country = 'US';            -- alice, carol
SELECT name FROM customers WHERE country = (SELECT 'US');   -- before: no rows; after: alice, carol

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 ApplyCharTypePaddingHelperSuite in 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, and SQLQueryTestSuite -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.

…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>
@github-actions github-actions Bot added the SQL label Dec 4, 2026
linhongliu-db and others added 2 commits December 4, 2026 22:05
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
linhongliu-db marked this pull request as ready for review December 4, 2026 22:48

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant