Commit 316a3cc
committed
[SPARK-57773][SQL] Strip leaked internal metadata in DataSourceV2Relation.create while preserving column IDs
### What changes were proposed in this pull request?
`DataSourceV2Relation.create` builds the relation output schema directly from `table.columns().asSchema` (after char/varchar replacement), without removing internal metadata. This PR makes `create` strip internal metadata (the keys in `INTERNAL_METADATA_KEYS`, via `removeInternalMetadata`) from the schema before it becomes the relation's output, **while preserving column IDs**.
`removeInternalMetadata` gains a `keepFieldIds: Boolean = false` parameter that skips `FIELD_ID_METADATA_KEY` in the same removal pass (the default preserves behavior for existing callers). `create` calls it with `keepFieldIds = true`:
```scala
val schema = removeInternalMetadata(
CharVarcharUtils.replaceCharVarcharWithStringInSchema(table.columns.asSchema),
keepFieldIds = true)
```
### Why are the changes needed?
`INTERNAL_METADATA_KEYS` exists so that internal-only metadata keys (e.g. `__metadata_col`, `__qualified_access_only`) do not surface to users, and `removeInternalMetadata` is already applied on other schema-producing paths. But `DataSourceV2Relation.create` never applied it, so any internal metadata key that a v2 source attaches to its columns leaks straight onto the relation output and `df.schema`. This hardens `create` so internal-only keys stay internal, consistent with the other paths.
Column IDs need special handling: `FIELD_ID_METADATA_KEY` is listed in `INTERNAL_METADATA_KEYS` so that other paths drop it, but it is also deliberately surfaced onto the relation's output (see SPARK-57544). Removing it in `create` would defeat that, so the new `keepFieldIds` flag preserves it in the same pass that strips the rest.
### Does this PR introduce _any_ user-facing change?
No. It removes internal-only metadata keys (which were never meant to be user-visible) from the v2 relation output, and preserves the column IDs that are intentionally surfaced.
### How was this patch tested?
New unit test in `DataSourceV2RelationSuite` that builds a table whose column carries both a column ID and a leaked internal metadata key, and asserts that `create` strips the internal key from the relation output while preserving the column ID. Verified the test fails without the source change.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)
Closes #56887 from cloud-fan/SPARK-57544-followup.
Authored-by: Wenchen Fan <wenchen@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>1 parent 5ebf000 commit 316a3cc
3 files changed
Lines changed: 54 additions & 5 deletions
File tree
- sql/catalyst/src
- main/scala/org/apache/spark/sql
- catalyst/util
- execution/datasources/v2
- test/scala/org/apache/spark/sql/execution/datasources/v2
Lines changed: 6 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
254 | | - | |
| 254 | + | |
255 | 255 | | |
256 | 256 | | |
257 | 257 | | |
258 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
259 | 263 | | |
260 | 264 | | |
261 | 265 | | |
| |||
Lines changed: 8 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
332 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
333 | 339 | | |
334 | 340 | | |
335 | 341 | | |
| |||
Lines changed: 40 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
23 | 28 | | |
24 | | - | |
| 29 | + | |
| 30 | + | |
25 | 31 | | |
26 | 32 | | |
27 | 33 | | |
| |||
107 | 113 | | |
108 | 114 | | |
109 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
110 | 149 | | |
0 commit comments