Repository navigation
Commit e46e643
authored
perf: avoid quadratic metadata merging in union schemas (apache#26790)
Related to apache#17261. Complements the fast path for equal schemas added in
Benchmark coverage is reviewed in apache#26791. This PR is stacked on its
benchmark-only commit `cfc6594b5`; the [optimization-only
diff](apache/datafusion@cfc6594...c5f9d3e)
contains the schema change and regression test.
Constructing a union with unequal input schemas can take quadratic time
in the number of children. The fallback merges every input's field
metadata before checking whether that candidate field is nullable. With
non-nullable fields, the same full merge is repeated for every child.
Select the first nullable field (or the first field when none are
nullable) before merging metadata. This makes the fallback linear in
child count for a fixed number of fields and metadata entries per input.
The shared helper also serves `InterleaveExec`.
- Merge metadata once per output field, preserving the selected field,
leftmost name, and existing metadata conflict precedence.
- Add a test that constructs and executes unions with non-nullable
inputs, first/middle/last nullable inputs, conflicting and distinct
metadata keys, and checks output rows.
Benchmarks use upstream `c18e5d974` plus the benchmark extension, Rust
1.99.1, `release-nonlto`, and an Apple M5 Pro. The table averages two
interleaved runs per binary, with 20 Criterion samples per run. Schemas
have 10 fields and two metadata entries per field.
| Non-nullable schema case | Children | Upstream | Patched | Speedup |
| --- | ---: | ---: | ---: | ---: |
| Last child's metadata differs | 10 | 141 µs | 62 µs | 2.3× |
| Last child's metadata differs | 100 | 9.24 ms | 0.66 ms | 14.1× |
| Last child's metadata differs | 1,000 | 877 ms | 7.40 ms | 118.6× |
| Field names differ | 10 | 140 µs | 60 µs | 2.3× |
| Field names differ | 100 | 9.38 ms | 0.66 ms | 14.3× |
| Field names differ | 1,000 | 866 ms | 7.36 ms | 117.6× |
The controls with nullable fields or equal schemas at 4,000 children
varied from -0.8% to +6.3% on this shared machine. Background builds
affected some measurements, so the larger SQL cases with sorted unions
were also checked in a tighter alternating sequence, with four runs per
binary:
| SQL planning case | Upstream | Patched | Change |
| --- | ---: | ---: | ---: |
| 50 columns, Int64 | 38.61 ms | 38.40 ms | -0.5% |
| 50 columns, UInt64 | 139.48 ms | 139.63 ms | +0.1% |
These results establish the constructor improvement independently of
workloads. The original extended logical-planning benchmark only calls
logical optimization and does not exercise this physical schema helper.
To run the benchmark cases from apache#26791:
```sh
cargo bench --profile release-nonlto -p datafusion-physical-plan --bench union_schema -- union_exec_try_new_non_nullable
```
Faster construction of unions and interleaves with unequal schemas and
non-nullable fields. Schema behavior and public APIs are preserved.
AI assistance: OpenAI Codex.
(cherry picked from commit 7ca68cf)
Ported to branch-55 (arrow 59): arrow::datatypes::Metadata does not exist there, so the
new test builds field metadata as a HashMap<String, String>, and the unused Field import
is dropped.
(cherry picked from commit 0c03aae)
(cherry picked from commit 83e6630)1 parent b516542 commit e46e643
2 files changed
Lines changed: 110 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
| |||
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
48 | | - | |
| 51 | + | |
49 | 52 | | |
50 | 53 | | |
51 | | - | |
| 54 | + | |
52 | 55 | | |
53 | 56 | | |
54 | 57 | | |
| |||
120 | 123 | | |
121 | 124 | | |
122 | 125 | | |
123 | | - | |
| 126 | + | |
124 | 127 | | |
| 128 | + | |
125 | 129 | | |
126 | 130 | | |
127 | 131 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
918 | 918 | | |
919 | 919 | | |
920 | 920 | | |
921 | | - | |
922 | | - | |
| 921 | + | |
| 922 | + | |
923 | 923 | | |
924 | 924 | | |
925 | 925 | | |
| |||
951 | 951 | | |
952 | 952 | | |
953 | 953 | | |
954 | | - | |
955 | | - | |
956 | | - | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
957 | 958 | | |
958 | 959 | | |
959 | | - | |
960 | | - | |
961 | | - | |
962 | | - | |
963 | | - | |
964 | | - | |
965 | | - | |
966 | | - | |
967 | | - | |
968 | | - | |
969 | | - | |
970 | | - | |
971 | | - | |
972 | | - | |
973 | | - | |
974 | | - | |
975 | | - | |
976 | | - | |
977 | | - | |
978 | | - | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
979 | 977 | | |
980 | 978 | | |
981 | 979 | | |
| |||
1067 | 1065 | | |
1068 | 1066 | | |
1069 | 1067 | | |
1070 | | - | |
| 1068 | + | |
1071 | 1069 | | |
1072 | 1070 | | |
1073 | 1071 | | |
| |||
1741 | 1739 | | |
1742 | 1740 | | |
1743 | 1741 | | |
| 1742 | + | |
| 1743 | + | |
| 1744 | + | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
| 1762 | + | |
| 1763 | + | |
| 1764 | + | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
| 1768 | + | |
| 1769 | + | |
| 1770 | + | |
| 1771 | + | |
| 1772 | + | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
| 1776 | + | |
| 1777 | + | |
| 1778 | + | |
| 1779 | + | |
| 1780 | + | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
| 1791 | + | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
| 1801 | + | |
| 1802 | + | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
1744 | 1820 | | |
1745 | 1821 | | |
1746 | 1822 | | |
| |||
0 commit comments