shivadarshan-devadiga opened a new pull request, #60750:
URL: https://github.com/apache/spark/pull/60750
### What changes were proposed in this pull request?
When a string literal is compared with a CHAR column, `ApplyCharTypePadding`
pads the shorter side to the longer length, so `c = 'a'` on a `CHAR(3)` column
becomes `c = rpad('a', 3, ' ')`. It only does this for a column
(`AttrOrOuterRef`), though, so a literal compared with a CHAR field of a struct
column is left unpadded. The field itself is padded when it's read
(`readSidePadding(s.c, 3)`), so `s.c = 'a'` compares `'a '` with `'a'` and
never matches:
```
Filter (s#23.c = A)
+- Project [id#19, CASE WHEN isnull(s#20) THEN null ELSE named_struct(c,
readSidePadding(s#20.c, 3)) END AS s#23]
+- Relation nested_char[id#19,s#20] parquet
```
The column's metadata already records its full raw type, such as `struct<c:
char(3)>`. This PR adds `RawTypedRef`, which matches a column or a chain of
`GetStructField` over one and returns the raw type of that column or field, by
struct ordinal. The literal comparison and the `IN` list of literals cases use
it, so they pad struct fields the same way as columns. For a column,
`RawTypedRef` returns the same raw type as before.
Comparisons between two fields, or a field and a column, still go through
`CharVarcharUtils.addPaddingInStringComparison`, which pads whole attributes.
This PR doesn't change them.
### Why are the changes needed?
A filter on a CHAR field of a struct returns no rows where the same filter
on a top-level CHAR column matches:
```sql
CREATE TABLE nested_char (id INT, s STRUCT<c: CHAR(3)>) USING PARQUET;
INSERT INTO nested_char VALUES (1, named_struct('c', 'A')), (2,
named_struct('c', 'B'));
SELECT id FROM nested_char WHERE s.c = 'A';
```
It returns no rows; it should return `1`.
### Does this PR introduce _any_ user-facing change?
Yes. Comparing a CHAR field of a struct with a string literal, with `=`,
`<=>`, `<`, `>` or `IN`, now pads as for a CHAR column, so the comparisons in
the example match.
### How was this patch tested?
New test `SPARK-61970: char type comparison: struct field and string
literal` in `CharVarcharTestSuite`. It covers `=` both ways, a literal with
trailing spaces, `>`, `IN`, a literal longer than the CHAR length, `<=>` with
`NULL`, and a field of a nested struct. It fails without this change and runs
in both `BasicCharVarcharTestSuite` and `FileSourceCharVarcharTestSuite`.
`CharVarcharTestSuite` (192 passed, 1 canceled by its existing `assume`) and
the `charvarchar` tests of `SQLQueryTestSuite` pass, and `scalastyle` is clean
for `catalyst` and `sql`.
### Was this patch authored or co-authored using generative AI tooling?
Co-authored by: Claude Sonnet 4.5
--
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]