cloud-fan opened a new pull request, #60661:
URL: https://github.com/apache/spark/pull/60661
### What changes were proposed in this pull request?
`value IN (e1, e2, ...)` should return the same result as `value = e1 OR
value = e2 OR ...`. When the value is a CHAR column, each of those equalities
pads its two sides independently:
- Against a CHAR column of a different length, or against a string literal,
the shorter side is padded.
- Against a STRING or VARCHAR column, nothing is padded.
`ApplyCharTypePadding` handles an IN list differently. It pads the value and
all the elements together, and it gives up when the elements need different
padding:
- `char_col IN (other_char_col, string_col)` and `char_col IN
(other_char_col, varchar_col)` pad nothing.
`CharVarcharUtils.addPaddingInStringComparison` bails out when one element has
no CHAR raw type, or when CHAR and VARCHAR are mixed.
- `char_col IN (other_char_col, 'literal')` matches neither of the two IN
cases (all literals, or all columns), so it is not padded either.
- The same applies to structs with CHAR fields.
This PR adds a case to
`ApplyCharTypePaddingHelper.singleNodePaddingForStringComparison`. It groups
the IN list elements by how each one is padded against the value, and the
existing IN padding then pads each group as before. There are three kinds of
group:
- the string literals;
- the columns of each type that contains CHAR, ignoring CHAR/VARCHAR lengths;
- the elements that are not padded.
The groups are combined with OR. For example, `c IN (c5, s)` becomes
`rpad(c, 5) IN (c5) OR c IN (s)`. An IN whose elements are all padded the same
way is left unchanged.
Splitting the IN references the value more than once. When the value is an
outer reference, the two analyzers then disagree on the subquery's outer
attributes:
- The fixed-point analyzer collects a subquery's outer references after the
post-resolution rules (including `ApplyCharTypePadding`) have run on the
subquery plan.
- The single-pass resolver's `PlanRewriter` runs those rules on the subquery
plan but keeps the outer references it collected before.
This mismatch already exists on master when a struct outer reference is
padded, e.g. `EXISTS (SELECT 1 FROM t2 WHERE t1.struct_char2 =
t2.struct_char5)`. This PR makes `PlanRewriter` collect the outer references
again when the rules change a subquery plan, matching the fixed-point analyzer.
These related cases need different code and are left out of this PR:
- IN subqueries, e.g. `char_col IN (SELECT other_char_col FROM ...)`.
- A literal value with a list of CHAR columns, e.g. `'a' IN (char_col, ...)`.
- Outer references used as list elements. They are still compared unpadded,
as before.
### Why are the changes needed?
They fix wrong results: IN and NOT IN disagree with the equalities they are
made of.
```sql
CREATE TABLE t(c CHAR(2), c5 CHAR(5), s STRING, v VARCHAR(5)) USING parquet;
INSERT INTO t VALUES ('a', 'a', 'a', 'a');
SELECT c = c5, c IN (c5, s), c NOT IN (c5, s), c IN (c5, v), c IN (c5, 'x'),
c IN (s, 'a')
FROM t;
-- before: true, false, true, false, false, false
-- after: true, true, false, true, true, true
```
### Does this PR introduce _any_ user-facing change?
Yes.
- Queries comparing a CHAR value with an IN list that mixes CHAR columns
with STRING or VARCHAR columns, or with string literals, now return correct
results. Previously they could miss rows, and NOT IN could keep rows it should
drop.
- In the analyzed plan, such an IN becomes an OR of INs, one per padding
group, e.g. `rpad(c, 5) IN (c5) OR c IN (s)`.
### How was this patch tested?
Added the test `char type comparison: IN list mixing CHAR columns with other
elements` to `CharVarcharTestSuite`. It covers read-side padding enabled and
disabled, a correlated subquery whose IN value is an outer reference, and
structs with CHAR fields, including a padded struct outer reference. It also
checks every IN and NOT IN against the OR of its equalities. Without the fix,
the test fails in `FileSourceCharVarcharTestSuite` and
`DSV2CharVarcharTestSuite`.
Also ran:
- `FileSourceCharVarcharTestSuite`, `DSV2CharVarcharTestSuite` and
`BasicCharVarcharTestSuite`
- `SubquerySuite` and `RangeJoinSQLSuite`
- the suites under `org.apache.spark.sql.analysis.resolver`
- `SQLQueryTestSuite` filtered to `subquery` and `char`
`HiveCharVarcharTestSuite` could not run locally because of a classpath
issue in my environment (`NoClassDefFoundError:
org/apache/commons/logging/LogFactory`).
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (Claude Opus 5.5)
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]