Repository navigation
[Spark] List partition columns last in the schema of non-Delta tables - #2232
Conversation
Spark's file sources read and write a table as its data columns followed by its partition columns. UCProxy.loadV1Table built the schema in UC's declared column order, so for a Parquet, ORC, JSON or CSV table whose partition column was not declared last, DESCRIBE TABLE and the catalog API listed the columns in a different order than SELECT * and INSERT ... VALUES use. Values written in the order DESCRIBE showed landed in other columns. Non-Delta tables now list the data columns in UC order followed by the partition columns in partition order, as Spark's own catalog stores them. Delta tables keep UC's order since Delta takes its schema from the Delta log. Signed-off-by: Maha Rana Yadavalli <[email protected]>
|
/review |
yili-db
left a comment
There was a problem hiding this comment.
[AI Assisted] The reorder matches Spark's own catalog, so the approach is right. Two inline asks: limit the reorder to file-source formats, and fold the new test class into ParquetExternalTableReadWriteTest. The docs wording is a nit.
Limit the partition-last schema to Parquet, ORC, JSON, CSV, Avro and text tables so Iceberg and any future format keep the declared order. Fold the tests into ParquetExternalTableReadWriteTest, DeltaExternalTableReadWriteTest and UCProxySuite instead of a separate server-backed class. Signed-off-by: Maha Rana Yadavalli <[email protected]>
|
/review |
yili-db
left a comment
There was a problem hiding this comment.
[AI Assisted] Approving, with one non-blocking docs note inline.
|
|
||
| See this page to learn more about the difference between external and managed Unity Catalog tables. | ||
|
|
||
| In a partitioned table of a file-source format (Parquet, ORC, JSON, CSV, Avro or text), Spark puts the partition columns |
There was a problem hiding this comment.
[AI Assisted] nit: Could the docs also say that UC keeps the declared column order (what the REST API and other engines see), while Spark lists partition columns last for file-source tables, matching Spark's own catalog? A table created as (a, p, b) then shows a, p, b through REST and a, b, p in Spark.
There was a problem hiding this comment.
Added in 60e7ff8, with the (a, p, b) example showing a, p, b through REST and a, b, p in Spark.
Signed-off-by: Maha Rana Yadavalli <[email protected]>
|
/review |
yili-db
left a comment
There was a problem hiding this comment.
[AI Assisted] Follow-up only: the approval above stands.
|
|
||
| See this page to learn more about the difference between external and managed Unity Catalog tables. | ||
|
|
||
| In a partitioned table of a file-source format (Parquet, ORC, JSON, CSV, Avro or text), Spark puts the partition columns |
There was a problem hiding this comment.
[AI Assisted] This paragraph sits inside the HTML comment that starts at line 781 and ends at line 971, so the published page will not show it. Could we move it outside that block, for example near the partitioned CREATE TABLE material above line 772?
…section (#2238) **PR Checklist** - [x] A description of the changes is added to the description of this PR. - [x] If there is a related issue, make sure it is linked to this PR. - [ ] If you've fixed a bug or added code that should be tested, add tests! (docs only) - [x] If you've added or modified a feature, documentation in `docs` is updated **Description of changes** Follow-up to #2232. The note on partition column order for file-source tables landed inside the HTML comment that hides the draft sections of `docs/integrations/unity-catalog-spark.md` (lines 781 to 971), so the published page does not show it. This moves the paragraph unchanged to the end of the visible "Create New Table" section, right before "Insert New Rows into Table", since it explains what `DESCRIBE TABLE`, `SELECT *` and `INSERT INTO ... VALUES` do on those tables. Signed-off-by: Maha Rana Yadavalli <[email protected]> Co-authored-by: Maha Rana Yadavalli <[email protected]>
PR Checklist
docsis updatedDescription of changes
For a partitioned table of a file-source format (Parquet, ORC, JSON, CSV, Avro or text) whose partition column is not declared last,
DESCRIBE TABLEshows a different column order than the one Spark reads and writes. AfterCREATE TABLE t (a INT, p INT, b INT) USING parquet PARTITIONED BY (p) LOCATION '...',DESCRIBE TABLE tlistsa, p, b, whileSELECT *returnsa, b, pandINSERT INTO t VALUES (...)takes the values in that order too. A user who writesINSERT INTO t VALUES (1, 2, 3)followingDESCRIBEgetsp = 3andb = 2, in partition directoryp=3, with no error. When the swapped columns have types that can't be cast to each other, the insert fails withCANNOT_SAFELY_CASTinstead, which is confusing given whatDESCRIBEshows.UCProxy.loadV1Tablebuilds theCatalogTableschema in UC's declared column order and also setspartitionColumnNames. Spark's file sources treat the schema as data columns followed by partition columns, and Spark's own catalog stores it that way (it moves the partition columns last at CREATE), so the relation Spark scans and writes isa, b, pwhile the schema the catalog returns, whichDESCRIBEshows, isa, p, b.For tables of those file-source formats the connector now builds the schema from the non-partition columns in UC order followed by the partition columns in partition index order, so with
PARTITIONED BY (q, p)the schema ends inq, pwhatever the declared order. Column comments, defaults and other metadata stay on each field, since the fields are built the same way and only reordered. Tables of other formats keep the declared order. Delta takes its schema from the Delta log, and Iceberg or any format added later is left alone so it does not change by default.This matches Spark's built-in catalog for the same DDL. On Spark 4.0 and 4.1,
CREATE TABLE spark_catalog.default.t (a INT, p INT, b STRING) USING parquet PARTITIONED BY (p)also describes asa, b, p, andSELECT *andINSERT ... VALUESuse that order. UC still stores and returns the columns in the declared order, so the REST API, the CLI and other engines see no change, and a table created through the REST API withpartition_indexset is reordered the same way when Spark loads it.Backward compatibility: no data written before this change is affected, since
SELECT *andINSERT ... VALUESalready used the data-then-partition order. What changes is the column orderDESCRIBE TABLEand the loaded table's schema report for these tables. CTAS into a file-source UC table fails before and after this change, and no table is left behind. Before, Spark rejected it because the table does not support batch append. Now, when the partition column is not last in the query, the positional cast check runs first and reportsINCOMPATIBLE_DATA_FOR_TABLE.CANNOT_SAFELY_CASTinstead, the same as anINSERT ... VALUESin that order would.PR #2228 also edited
loadV1Table(comment and table properties for non-Delta tables). It has merged, and this branch merges main without conflicts.Tests:
ParquetExternalTableReadWriteTest.testLoadTableKeepsColumnMetadataAndListsPartitionColumnsLastruns against a real UC server. It creates a Parquet table with the partition column in the middle, writes with a positionalINSERT ... VALUESand with a column list that leaves out a column with aDEFAULT, and checks theDESCRIBE TABLErows (including the comment of the moved partition column) andSELECT *. It does the same for a table with two partition columnsPARTITIONED BY (q, p)declared aspbeforeq, which covers the partition index sort.DeltaExternalTableReadWriteTest.testPartitionedTableKeepsDeclaredColumnOrderchecks that a partitioned Delta table keeps its declared order.UCProxySuiteloads a table as it comes from the REST API withpartition_indexset, without a server, and checks the loaded schema, the partition order, and that a moved column keeps its comment and default for each of the six file-source formats, and that Delta and Iceberg keep the declared order.I ran these on Linux (GitHub Actions on my fork) against Spark 4.1.0 with Delta 4.4.1. With the fix reverted,
testLoadTableKeepsColumnMetadataAndListsPartitionColumnsLastand all six file-source cases oftestLoadFileSourceTablePutsPartitionColumnsLastfail on the column order, while the Delta and Iceberg cases pass. I also checked locally that with the earlier version of this PR, which reordered every format except Delta, the Iceberg case oftestLoadNonFileSourceTableKeepsUCColumnOrderfails. With the fix, all of them pass, and the fullspark/testsuite passes (293 tests, 0 failures). Spark 4.0, 4.2 and 4.3 are left to the CI matrix.