LiaCastaneda opened a new pull request, #27678:
URL: https://github.com/apache/datafusion/pull/27678
## Which issue does this PR close?
- Part of #27517 (the "`CAST` to a struct type" case).
## Rationale for this change
#27099 gives struct literals the field names of the struct column they are
combined with, because Substrait types carry no struct field names and the
consumer names a literal's fields `c0`, `c1`, .... A cast has the same problem:
its target type is a Substrait type too, so a cast to a struct type also
arrives with `c0`, `c1`, ... names.
DataFusion's own plans produce such casts. Union planning pads a `NULL`
input to the struct type of the other input, and when the `NULL` comes from
`VALUES` it's a `Null`-typed column, so the cast stays a cast instead of
becoming a typed `NULL` literal:
```sql
-- st: Struct{a, b}
SELECT st FROM t UNION ALL SELECT * FROM (VALUES (NULL)) AS v(st)
```
After a Substrait round trip, this fails with:
```
Incompatible inputs for Union: Previous inputs were of type Struct("a":
Int64, "b": Utf8), but got incompatible type Struct("c0": Int64, "c1": Utf8) on
column 'column1'
```
The same happens when such a cast is a function argument, for example in
`coalesce(st, v.st)`, which is simplified to a `CASE`: `Failed to coerce then
(Struct("a": Int64, "b": Utf8)) and else (Struct("c0": Int64, "c1": Utf8)) to
common types in CASE WHEN expression`.
## What changes are included in this PR?
A cast to a struct type whose input has no struct type, such as a `NULL`, a
`Null`-typed column or an empty map of non-struct types, is now treated like a
literal: its target type takes the field names of the struct it's combined
with, wherever literals already do (function arguments, `CASE` results, `IN`
lists, set operation inputs, ...).
Renaming can't change the result of such a cast: its input has no struct
fields for the target's fields to match. A cast from a struct is left
unchanged, because it matches fields by name.
Not covered, and still failing as before:
- A cast moved into a lower projection, so the expression combining it with
the struct column only sees a column reference (for example `coalesce(v.st,
st)`, where common subexpression elimination moves the cast). This is the "set
operation input whose top projection only references columns" family in #27517.
- A cast whose input contains a struct, such as `VALUES (map(['k'],
[NULL]))` next to a `Map<Utf8, Struct{a, b}>` column (a map's entries are a
struct). Renaming only the target would break the cast's by-name field matching.
## What is the testing strategy for this PR?
New round-trip tests in `roundtrip_logical_plan.rs`, in the same shape as
the struct literal tests from #27099. Each compares the output type and rows
before and after the round trip, and each fails on `main`:
- `roundtrip_struct_cast_union_with_values_null_second_input` /
`_first_input`: the query above, with the `VALUES` input second and first.
- `roundtrip_struct_cast_union_with_values_null_map_key`: the struct is a
map key.
- `roundtrip_struct_cast_in_coalesce`: the cast is a `coalesce` argument.
## Are there any user-facing changes?
No API changes. Plans with these casts now consume instead of failing.
--
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]