SEPURI-SAI-KRISHNA opened a new pull request, #60242:
URL: https://github.com/apache/spark/pull/60242
### What changes were proposed in this pull request?
When the key of a map subscript (`m[key]`, `col("m")(key)`, `getItem`) has
an integral type that is wider than the integral key type of the map, the
implicit type coercion now casts the key to the map key type with `TRY` instead
of with a regular cast. A key out of the range of the map key type then becomes
NULL, and the lookup returns NULL, because such a key is not in the map. A key
in that range is cast to the same value as before.
The change is a new case for `GetMapValue` in
`TypeCoercionHelper.ImplicitTypeCoercion`, which is shared by the default and
the ANSI type coercion and by the single-pass resolver. Other keys are coerced
as before.
### Why are the changes needed?
The implicit cast of the key to the map key type overflows. With ANSI mode
off, it wraps around, so the lookup returns the value of a different key. With
ANSI mode on, it fails the query, although the key is simply not in the map:
```sql
CREATE TABLE t USING parquet AS
SELECT id, map(CAST(44 AS TINYINT), 'v') AS m, map(44, 'iv') AS mi FROM
range(1);
SET spark.sql.ansi.enabled=false;
SELECT m[300], mi[4294967340L], element_at(m, 300), element_at(mi,
4294967340L) FROM t;
-- v iv NULL NULL (expected NULL for all four)
SET spark.sql.ansi.enabled=true;
SELECT m[300] FROM t;
-- [CAST_OVERFLOW] The value 300 of the type "INT" cannot be cast to
"TINYINT" due to an overflow.
```
300 wraps to 44 in `TINYINT`, and 4294967340 wraps to 44 in `INT`. The same
happens with a non-literal key, such as an `INT` column used as the key of a
`MAP<TINYINT, ...>` column, and with the DataFrame API. `element_at` widens the
map keys instead and returns NULL.
Casting the key with `TRY` gives the same results as `element_at` without
casting the map: a map lookup with a key that fits keeps its plan (a literal
key such as `m[44]` is still folded to a `TINYINT` literal), and the
optimizations that match `GetMapValue` on a map column or a `CreateMap` (nested
column pruning, `SimplifyExtractValueOps`) keep applying.
### Does this PR introduce _any_ user-facing change?
Yes. A map subscript whose integral key is out of the range of the integral
map key type returns NULL, instead of the value of a different key (ANSI mode
off) or a `CAST_OVERFLOW` error (ANSI mode on). Keys in range return the same
values as before. In the analyzed plan, the cast of such a key is shown as
`try_cast`.
### How was this patch tested?
- A new test in `TypeCoercionSuiteBase` (run by `TypeCoercionSuite` and
`AnsiTypeCoercionSuite`): a wider integral key is cast with `TRY`, and other
keys are cast as before.
- A new test in `DataFrameComplexTypeSuite`: lookups in `TINYINT`,
`SMALLINT` and `INT` keyed maps read from a Parquet table, with `BIGINT` and
`INT` column keys and with literal keys, in SQL and the DataFrame API, with
ANSI mode on and off, with and without codegen. The out-of-range keys are
chosen so that they wrap around to a key of the map.
- New queries in `map.sql`, with regenerated golden files (ANSI and non-ANSI
results and analyzer results). Column names are unchanged.
- All of these tests fail without this change: with `CAST_OVERFLOW` with
ANSI mode on, and with the value of the key 44 instead of NULL with ANSI mode
off.
- Ran `TypeCoercionSuite`, `AnsiTypeCoercionSuite`,
`CollationTypeCoercionSuite`, `AnalysisSuite`, `AnalysisErrorSuite`,
`ComplexTypeSuite`, `SelectedFieldSuite`, `ComplexTypesSuite`, the suites in
`org.apache.spark.sql.catalyst.analysis.resolver`, `DataFrameComplexTypeSuite`,
`DataFrameFunctionsSuite`, `ColumnExpressionSuite`,
`QueryExecutionAnsiErrorsSuite`, `ParquetV1SchemaPruningSuite`,
`ParquetV2SchemaPruningSuite`, `OrcV1SchemaPruningSuite`,
`WholeStageCodegenSuite`, `SubquerySuite`, `SQLQuerySuite`, `DataFrameSuite`,
and `SQLQueryTestSuite` for the inputs that use map subscripts.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.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]