shivadarshan-devadiga opened a new pull request, #60756:
URL: https://github.com/apache/spark/pull/60756

   ### What changes were proposed in this pull request?
   
   A `Union` builds each output column from its first child 
(`Union.mergeChildOutputs`), metadata included, so the CHAR length of the 
output is the one of the first child. With the default read-side padding, the 
values of each child are padded to the CHAR length of their own table, and 
`ApplyCharTypePadding` pads a string literal compared with the output to the 
output's CHAR length. When the children have different CHAR lengths, the values 
of the other children have another length, and the comparison misses them. The 
rows that match depend on the order of the children:
   
   ```
   Filter (c#70 = rpad(a, 3,  ))           <- padded to the CHAR(3) of the 
first child
   +- Union false, false
      :- Project [id#64, readSidePadding(c#65, 3) AS c#70]   <- 'a  '
      +- Project [id#66, readSidePadding(c#67, 5) AS c#71]   <- 'a    ', never 
matches
   ```
   
   This PR makes `ApplyCharTypePadding` pad the children of a `Union` whose 
CHAR columns or fields have different lengths at the same position to the 
widest length, and gives the output that CHAR type, as the SQL standard does 
for the union of CHAR(n) and CHAR(m). It runs after the read-side padding and 
before the padding of comparisons, so comparisons pad to the widest length too.
   
   - `CharVarcharUtils.padToWiderCharLength` computes the raw type with the 
widest lengths and pads each child's value with the existing 
`padCharToTargetLength`. This also covers CHAR fields of structs and elements 
of arrays.
   - `ApplyCharTypePadding.padUnionChildren` adds the padding as a `Project` 
over the children that need it, through `transformUpWithNewOutput`. That method 
keeps the metadata of the references to the new outputs, so the rule then gives 
those references the new raw type, including in subqueries.
   
   Children that are not all CHAR types at a position, such as CHAR and STRING, 
or CHAR and VARCHAR, are left as before.
   
   ### Why are the changes needed?
   
   A filter above a `UNION ALL` of CHAR columns of different lengths drops 
matching rows, and reversing the children changes which rows it returns:
   
   ```sql
   CREATE TABLE t3 (id INT, c CHAR(3)) USING PARQUET;
   CREATE TABLE t5 (id INT, c CHAR(5)) USING PARQUET;
   INSERT INTO t3 VALUES (1, 'A');
   INSERT INTO t5 VALUES (2, 'A');
   SELECT id FROM (SELECT id, c FROM t3 UNION ALL SELECT id, c FROM t5) u WHERE 
c = 'A';  -- 1
   SELECT id FROM (SELECT id, c FROM t5 UNION ALL SELECT id, c FROM t3) u WHERE 
c = 'A';  -- 2
   ```
   
   Both should return 1 and 2.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The output of a `UNION` of CHAR columns or fields of different lengths 
now has the widest CHAR type, in either order of the children, and its values 
are padded to that length. The queries above return 1 and 2. `UNION` (distinct) 
treats `'A  '` and `'A    '` as the same value.
   
   ### How was this patch tested?
   
   New test `SPARK-61972: union of char columns of different lengths` in 
`CharVarcharTestSuite`, for both orders of the children: `UNION ALL` with a 
filter, the CHAR type of the output, `UNION`, and a union of structs with CHAR 
fields. It fails without this change, and runs in both 
`BasicCharVarcharTestSuite` and `FileSourceCharVarcharTestSuite`.
   
   These pass, and `scalastyle` is clean for `catalyst` and `sql` (main and 
test):
   - `CharVarcharTestSuite`: 192 passed, 1 canceled by its existing `assume`.
   - `*PlanStability*`: 322 passed, with no plan changes.
   
   ### 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]

Reply via email to