Commit 04c01bb
feat: add TableSchemaBuilder and store partition columns as Fields (apache#22496)
## Which issue does this PR close?
- No separate issue. Follows up on apache#22372 (panic fix in
`TableSchema::with_table_partition_cols`) and the API discussion it
spawned, and is informed by apache#22026 (which adds a third column group,
virtual columns, to `TableSchema`).
## Rationale for this change
`TableSchema` has one required input (the file schema) and a growing set
of *optional* column groups: partition columns today, virtual columns in
apache#22026. The current API expresses this awkwardly:
- `new(file_schema, partition_cols)` privileges partition columns with a
positional slot while virtual columns only get a builder method — an
asymmetry that grows with every new column kind.
- `TableSchema` eagerly recomputes and caches the concatenated table
schema on *every* incremental setter call, so
`from_file_schema(s).with_table_partition_cols(p)` rebuilds it twice
(three times once virtual columns are added). This is exactly why
`new()`'s docs told callers to avoid the builder-style chain.
- The setter mutated an inner `Arc<Vec<FieldRef>>` in place, which is
what caused the shared-`Arc` panic fixed in apache#22372.
A dedicated builder addresses all three, and mirrors the existing
`FileScanConfigBuilder` (the type that *owns* a `TableSchema`).
## What changes are included in this PR?
- **`TableSchemaBuilder`**: `new(file_schema)` →
`.with_table_partition_cols(impl Into<Fields>)` → `.build()`. The
concatenated table schema is computed exactly **once**, in `build()`.
The setter takes `impl Into<Fields>`, so an existing schema's `Fields`
is accepted zero-copy.
- **Partition columns are now stored as `arrow::datatypes::Fields`** (an
immutable `Arc<[FieldRef]>`) instead of `Arc<Vec<FieldRef>>`: one fewer
indirection, shareable zero-copy, and — being immutable — the
shared-`Arc` mutation panic is structurally impossible.
- **`TableSchema::table_partition_cols()` and the delegating
`FileScanConfig::table_partition_cols()` now return `&Fields`.**
`Fields` derefs to `&[FieldRef]`, so iteration/indexing/`len`/`is_empty`
are unchanged; only the arrow `FileFormat` path needed `.to_vec()`.
- **`TableSchema::with_table_partition_cols` is deprecated** in favor of
the builder. It now **replaces** rather than appends. (Note: `main`
currently *appends* here — the replace change in apache#22372 was not captured
by that PR's squash merge — so this also restores the intended replace
semantics.)
- `new` / `from_file_schema` are kept as conveniences that route through
the builder.
- Documented in the 54.0.0 upgrade guide.
This intentionally leaves virtual columns out; apache#22026 should extend the
builder with `with_virtual_columns` once it lands.
## Are these changes tested?
Yes. New unit tests cover building with partition columns,
replace-on-repeat, zero-copy `Fields` input, and the deprecated setter's
behavior; existing `TableSchema` / `FileScanConfig` tests and doctests
pass. `cargo clippy --all-targets -- -D warnings` is clean across the
datasource/proto/arrow/parquet/catalog-listing crates.
## Are there any user-facing changes?
Yes — please apply the `api change` label:
- `TableSchema::table_partition_cols()` /
`FileScanConfig::table_partition_cols()` return `&Fields` instead of
`&Vec<FieldRef>` (source-compatible for most uses via `Deref`).
- `TableSchema::with_table_partition_cols` is deprecated (use the
builder) and now replaces rather than appends.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>1 parent 58b94f6 commit 04c01bb
18 files changed
Lines changed: 357 additions & 230 deletions
File tree
- datafusion
- catalog-listing/src
- core
- src
- datasource
- file_format
- physical_plan
- test
- tests/physical_optimizer
- datasource-arrow/src
- datasource-parquet/src
- opener
- datasource/src
- file_scan_config
- file_stream
- proto
- src/physical_plan
- tests/cases
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
332 | 333 | | |
333 | 334 | | |
334 | 335 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| |||
223 | 223 | | |
224 | 224 | | |
225 | 225 | | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
230 | 233 | | |
231 | 234 | | |
232 | 235 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | | - | |
| 125 | + | |
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
197 | | - | |
| 197 | + | |
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
| |||
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
268 | | - | |
| 268 | + | |
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
| |||
335 | 335 | | |
336 | 336 | | |
337 | 337 | | |
338 | | - | |
| 338 | + | |
339 | 339 | | |
340 | 340 | | |
341 | 341 | | |
| |||
371 | 371 | | |
372 | 372 | | |
373 | 373 | | |
374 | | - | |
| 374 | + | |
375 | 375 | | |
376 | 376 | | |
377 | 377 | | |
| |||
400 | 400 | | |
401 | 401 | | |
402 | 402 | | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
407 | 410 | | |
408 | 411 | | |
409 | 412 | | |
| |||
508 | 511 | | |
509 | 512 | | |
510 | 513 | | |
511 | | - | |
| 514 | + | |
512 | 515 | | |
513 | 516 | | |
514 | 517 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| |||
1642 | 1642 | | |
1643 | 1643 | | |
1644 | 1644 | | |
1645 | | - | |
1646 | | - | |
1647 | | - | |
| 1645 | + | |
| 1646 | + | |
1648 | 1647 | | |
1649 | 1648 | | |
1650 | 1649 | | |
| |||
1655 | 1654 | | |
1656 | 1655 | | |
1657 | 1656 | | |
1658 | | - | |
1659 | | - | |
| 1657 | + | |
| 1658 | + | |
1660 | 1659 | | |
1661 | 1660 | | |
1662 | 1661 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | | - | |
| 106 | + | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| |||
Lines changed: 8 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
1574 | 1574 | | |
1575 | 1575 | | |
1576 | 1576 | | |
1577 | | - | |
1578 | | - | |
1579 | | - | |
1580 | | - | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
| 1583 | + | |
1581 | 1584 | | |
1582 | 1585 | | |
1583 | 1586 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
114 | | - | |
| 114 | + | |
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | 40 | | |
42 | 41 | | |
43 | 42 | | |
44 | 43 | | |
45 | 44 | | |
46 | 45 | | |
47 | 46 | | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| |||
197 | 197 | | |
198 | 198 | | |
199 | 199 | | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
204 | 203 | | |
205 | 204 | | |
206 | 205 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1415 | 1415 | | |
1416 | 1416 | | |
1417 | 1417 | | |
1418 | | - | |
| 1418 | + | |
1419 | 1419 | | |
1420 | 1420 | | |
1421 | 1421 | | |
| |||
1495 | 1495 | | |
1496 | 1496 | | |
1497 | 1497 | | |
1498 | | - | |
| 1498 | + | |
1499 | 1499 | | |
1500 | 1500 | | |
1501 | 1501 | | |
| |||
1882 | 1882 | | |
1883 | 1883 | | |
1884 | 1884 | | |
1885 | | - | |
1886 | | - | |
1887 | | - | |
1888 | | - | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
1889 | 1892 | | |
1890 | 1893 | | |
1891 | 1894 | | |
| |||
1951 | 1954 | | |
1952 | 1955 | | |
1953 | 1956 | | |
1954 | | - | |
1955 | | - | |
1956 | | - | |
1957 | | - | |
| 1957 | + | |
| 1958 | + | |
| 1959 | + | |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
1958 | 1964 | | |
1959 | 1965 | | |
1960 | 1966 | | |
| |||
2023 | 2029 | | |
2024 | 2030 | | |
2025 | 2031 | | |
2026 | | - | |
2027 | | - | |
2028 | | - | |
2029 | | - | |
| 2032 | + | |
| 2033 | + | |
| 2034 | + | |
| 2035 | + | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
2030 | 2039 | | |
2031 | 2040 | | |
2032 | 2041 | | |
| |||
2104 | 2113 | | |
2105 | 2114 | | |
2106 | 2115 | | |
2107 | | - | |
2108 | | - | |
2109 | | - | |
2110 | | - | |
| 2116 | + | |
| 2117 | + | |
| 2118 | + | |
| 2119 | + | |
| 2120 | + | |
| 2121 | + | |
| 2122 | + | |
2111 | 2123 | | |
2112 | 2124 | | |
2113 | 2125 | | |
| |||
0 commit comments