Commit 8c478e9
Disallow positional struct casting when field names don’t overlap (#19955)
## Which issue does this PR close?
* Closes #19841.
## Rationale for this change
Struct-to-struct casting previously fell back to **positional mapping**
when there was **no field-name overlap** and the number of fields
matched. That behavior is ambiguous and can silently produce incorrect
results when source/target schemas have different field naming
conventions or ordering.
This PR makes struct casting **strictly name-based**: when there is no
overlap in field names between the source and target structs, the cast
is rejected with a clear planning error. This prevents accidental,
hard-to-detect data corruption and forces callers to provide explicit
field names (or align schemas) when casting.
## What changes are included in this PR?
* Removed the positional fallback logic for struct casting in
`cast_struct_column`; child fields are now resolved **only by name**.
* Updated `validate_struct_compatibility` to **error out** when there is
**no field name overlap**, instead of allowing positional compatibility
checks.
* Updated unit tests to reflect the new behavior (no-overlap casts now
fail with an appropriate error).
* Updated SQLLogicTest files to construct structs using **explicit field
names** (e.g. `{id: 1}` / `{a: 1, b: 'x'}` or `struct(… AS field)`),
avoiding reliance on positional behavior.
* Improved error messaging to explicitly mention the lack of field name
overlap.
## Are these changes tested?
Yes.
* Updated existing Rust unit tests in `nested_struct.rs` to assert the
new failure mode and error message.
* Updated SQLLogicTest coverage (`struct.slt`, `joins.slt`) to use named
struct literals so tests continue to validate struct behavior without
positional casting.
## Are there any user-facing changes?
Yes — behavioral change / potential breaking change.
* Casting between two `STRUCT`s with **no overlapping field names** now
fails (previously it could succeed via positional mapping if field
counts matched).
* Users must provide explicit field names (e.g. `{a: 1, b: 'x'}` or
`struct(expr AS a, expr AS b)`) or ensure schemas share field names.
* Error messages are more explicit: casts are rejected when there is “no
field name overlap”.
## LLM-generated code disclosure
This PR includes LLM-generated code and comments. All LLM-generated
content has been manually reviewed and tested.
---------
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>1 parent 96a6bd7 commit 8c478e9
6 files changed
Lines changed: 170 additions & 94 deletions
File tree
- datafusion
- common/src
- optimizer/src/simplify_expressions
- sqllogictest/test_files
- substrait/tests/cases
- docs/source/library-user-guide
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
| 34 | + | |
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
71 | | - | |
72 | 70 | | |
73 | 71 | | |
74 | 72 | | |
75 | 73 | | |
76 | | - | |
77 | | - | |
78 | | - | |
| 74 | + | |
| 75 | + | |
79 | 76 | | |
80 | 77 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
| 78 | + | |
| 79 | + | |
88 | 80 | | |
89 | 81 | | |
90 | 82 | | |
| |||
230 | 222 | | |
231 | 223 | | |
232 | 224 | | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
247 | 230 | | |
248 | 231 | | |
249 | 232 | | |
| |||
323 | 306 | | |
324 | 307 | | |
325 | 308 | | |
326 | | - | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
327 | 314 | | |
328 | 315 | | |
329 | 316 | | |
| |||
546 | 533 | | |
547 | 534 | | |
548 | 535 | | |
549 | | - | |
| 536 | + | |
550 | 537 | | |
551 | 538 | | |
552 | 539 | | |
| |||
556 | 543 | | |
557 | 544 | | |
558 | 545 | | |
559 | | - | |
| 546 | + | |
560 | 547 | | |
561 | 548 | | |
562 | 549 | | |
| |||
665 | 652 | | |
666 | 653 | | |
667 | 654 | | |
668 | | - | |
669 | | - | |
670 | | - | |
671 | | - | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
672 | 660 | | |
673 | | - | |
674 | | - | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
675 | 665 | | |
676 | 666 | | |
677 | 667 | | |
678 | 668 | | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
| 669 | + | |
683 | 670 | | |
684 | 671 | | |
685 | 672 | | |
| |||
948 | 935 | | |
949 | 936 | | |
950 | 937 | | |
951 | | - | |
| 938 | + | |
952 | 939 | | |
953 | 940 | | |
954 | 941 | | |
| |||
964 | 951 | | |
965 | 952 | | |
966 | 953 | | |
967 | | - | |
968 | | - | |
969 | | - | |
970 | | - | |
971 | | - | |
972 | | - | |
973 | | - | |
974 | | - | |
975 | | - | |
976 | | - | |
977 | | - | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
978 | 958 | | |
979 | 959 | | |
980 | 960 | | |
| |||
Lines changed: 10 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
657 | 658 | | |
658 | 659 | | |
659 | 660 | | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
660 | 666 | | |
661 | 667 | | |
662 | 668 | | |
| |||
5220 | 5226 | | |
5221 | 5227 | | |
5222 | 5228 | | |
5223 | | - | |
| 5229 | + | |
5224 | 5230 | | |
5225 | 5231 | | |
5226 | 5232 | | |
| |||
5237 | 5243 | | |
5238 | 5244 | | |
5239 | 5245 | | |
5240 | | - | |
| 5246 | + | |
5241 | 5247 | | |
5242 | | - | |
5243 | | - | |
5244 | | - | |
5245 | | - | |
| 5248 | + | |
5246 | 5249 | | |
5247 | | - | |
| 5250 | + | |
5248 | 5251 | | |
5249 | 5252 | | |
5250 | 5253 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | | - | |
| 60 | + | |
| 61 | + | |
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | | - | |
| 67 | + | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
42 | | - | |
43 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
397 | 397 | | |
398 | 398 | | |
399 | 399 | | |
400 | | - | |
| 400 | + | |
| 401 | + | |
401 | 402 | | |
402 | 403 | | |
403 | 404 | | |
| |||
408 | 409 | | |
409 | 410 | | |
410 | 411 | | |
411 | | - | |
| 412 | + | |
| 413 | + | |
412 | 414 | | |
413 | 415 | | |
414 | 416 | | |
| |||
442 | 444 | | |
443 | 445 | | |
444 | 446 | | |
445 | | - | |
446 | | - | |
447 | | - | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
448 | 450 | | |
449 | 451 | | |
450 | 452 | | |
451 | 453 | | |
452 | 454 | | |
453 | 455 | | |
454 | 456 | | |
455 | | - | |
456 | | - | |
| 457 | + | |
| 458 | + | |
457 | 459 | | |
458 | 460 | | |
459 | 461 | | |
| |||
501 | 503 | | |
502 | 504 | | |
503 | 505 | | |
504 | | - | |
505 | | - | |
506 | | - | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
507 | 509 | | |
508 | 510 | | |
509 | 511 | | |
| |||
528 | 530 | | |
529 | 531 | | |
530 | 532 | | |
531 | | - | |
532 | | - | |
533 | | - | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
534 | 536 | | |
535 | 537 | | |
536 | 538 | | |
| |||
553 | 555 | | |
554 | 556 | | |
555 | 557 | | |
556 | | - | |
557 | | - | |
| 558 | + | |
| 559 | + | |
558 | 560 | | |
559 | 561 | | |
560 | 562 | | |
| |||
568 | 570 | | |
569 | 571 | | |
570 | 572 | | |
571 | | - | |
| 573 | + | |
572 | 574 | | |
573 | 575 | | |
574 | 576 | | |
| |||
580 | 582 | | |
581 | 583 | | |
582 | 584 | | |
583 | | - | |
| 585 | + | |
584 | 586 | | |
585 | 587 | | |
586 | 588 | | |
| |||
602 | 604 | | |
603 | 605 | | |
604 | 606 | | |
605 | | - | |
| 607 | + | |
606 | 608 | | |
607 | 609 | | |
608 | 610 | | |
| |||
615 | 617 | | |
616 | 618 | | |
617 | 619 | | |
618 | | - | |
| 620 | + | |
619 | 621 | | |
620 | 622 | | |
621 | 623 | | |
| |||
823 | 825 | | |
824 | 826 | | |
825 | 827 | | |
826 | | - | |
| 828 | + | |
827 | 829 | | |
828 | | - | |
| 830 | + | |
829 | 831 | | |
830 | 832 | | |
831 | 833 | | |
| |||
859 | 861 | | |
860 | 862 | | |
861 | 863 | | |
862 | | - | |
863 | | - | |
864 | | - | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
865 | 867 | | |
866 | 868 | | |
867 | 869 | | |
| |||
0 commit comments