yihua opened a new pull request, #20860:
URL: https://github.com/apache/hudi/pull/20860

   ### Describe the issue this Pull Request addresses
   
   Closes #20859.
   
   A Spark SQL MERGE INTO whose source key column has another integral type 
than the record key column is refused, for example a BIGINT source key (common 
with `spark.range`) against an INT record key: "Primary key data type mismatch" 
on a table with a record key, and "can't cast ... (of LongType) to IntegerType" 
on a keyless table.
   
   ### Summary and Changelog
   
   `MergeIntoHoodieTableCommand` (Spark 3 and Spark 4 copies) now accepts a 
source key of another integral type for an integral key column and casts it to 
the column type with an ANSI cast. A narrower source type is accepted on every 
Spark version; a wider one needs Spark 3.5 or later, whose assignment 
resolution also ANSI-casts the inserted value.
   
   A wider source key renders the record key from its own value, so a value the 
key column cannot hold would be written under a record key that differs from 
the stored value (for example `4294967297` with an explicit `cast(s.id as int)` 
in the insert). The command therefore filters the source through an ANSI cast 
of each such key column to the key type, which fails the statement with a cast 
overflow on that value. The check is a filter rather than a projection, so the 
optimizer cannot drop it as a no-op. It covers every record key field with a 
source column, including one the merge condition does not match on, and no 
other field.
   
   Tests: a new case in `TestMergeIntoTable2` (COW and MOR, keyed and keyless) 
merges BIGINT keys into an INT key column and INT keys into a BIGINT one, and 
checks that an out-of-range key fails with an overflow for `INSERT *`, an 
explicit `cast(s.id as int)`, a renamed source key, a matched-only update, a 
`spark.range` source, and a composite key whose second field is not in the 
merge condition, leaving the table unchanged. It fails on master with the 
errors above. `TestTableColumnTypeMismatch` now uses a DOUBLE source key for 
its key mismatch case, since a BIGINT one is accepted.
   
   ### Impact
   
   MERGE INTO accepts a source key of another integral type than the record key 
column. A source key value out of the range of the key column fails the 
statement, including in a matched-only MERGE, where nothing could match it.
   
   ### Risk Level
   
   low
   
   Only MERGE INTO statements whose source and target key types differ, which 
failed before, take the new path.
   
   It conflicts textually with #20843 and #20855 in `TestMergeIntoTable2` (all 
three append tests there); the changes are independent, so whichever merges 
later needs a rebase.
   
   ### Documentation Update
   
   none
   
   ### Contributor's checklist
   
   - [x] Read through [contributor's 
guide](https://hudi.apache.org/contribute/how-to-contribute)
   - [x] Enough context is provided in the sections above
   - [x] Adequate tests were added if applicable
   


-- 
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]

Reply via email to