Skip to content

[Spark] List partition columns last in the schema of non-Delta tables - #2232

Merged
yili-db merged 4 commits into
unitycatalog:mainfrom
maharanay22:fix-spark-partition-column-order
Nov 14, 2026
Merged

yili-db merged 4 commits into
unitycatalog:mainfrom
maharanay22:fix-spark-partition-column-order

Conversation

@maharanay22

@maharanay22 maharanay22 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

  • A description of the changes is added to the description of this PR.
  • 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!
  • If you've added or modified a feature, documentation in docs is updated

Description 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 TABLE shows a different column order than the one Spark reads and writes. After CREATE TABLE t (a INT, p INT, b INT) USING parquet PARTITIONED BY (p) LOCATION '...', DESCRIBE TABLE t lists a, p, b, while SELECT * returns a, b, p and INSERT INTO t VALUES (...) takes the values in that order too. A user who writes INSERT INTO t VALUES (1, 2, 3) following DESCRIBE gets p = 3 and b = 2, in partition directory p=3, with no error. When the swapped columns have types that can't be cast to each other, the insert fails with CANNOT_SAFELY_CAST instead, which is confusing given what DESCRIBE shows.

UCProxy.loadV1Table builds the CatalogTable schema in UC's declared column order and also sets partitionColumnNames. 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 is a, b, p while the schema the catalog returns, which DESCRIBE shows, is a, 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 in q, p whatever 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 as a, b, p, and SELECT * and INSERT ... VALUES use 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 with partition_index set is reordered the same way when Spark loads it.

Backward compatibility: no data written before this change is affected, since SELECT * and INSERT ... VALUES already used the data-then-partition order. What changes is the column order DESCRIBE TABLE and 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 reports INCOMPATIBLE_DATA_FOR_TABLE.CANNOT_SAFELY_CAST instead, the same as an INSERT ... VALUES in 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.testLoadTableKeepsColumnMetadataAndListsPartitionColumnsLast runs against a real UC server. It creates a Parquet table with the partition column in the middle, writes with a positional INSERT ... VALUES and with a column list that leaves out a column with a DEFAULT, and checks the DESCRIBE TABLE rows (including the comment of the moved partition column) and SELECT *. It does the same for a table with two partition columns PARTITIONED BY (q, p) declared as p before q, which covers the partition index sort. DeltaExternalTableReadWriteTest.testPartitionedTableKeepsDeclaredColumnOrder checks that a partitioned Delta table keeps its declared order. UCProxySuite loads a table as it comes from the REST API with partition_index set, 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, testLoadTableKeepsColumnMetadataAndListsPartitionColumnsLast and all six file-source cases of testLoadFileSourceTablePutsPartitionColumnsLast fail 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 of testLoadNonFileSourceTableKeepsUCColumnOrder fails. With the fix, all of them pass, and the full spark/test suite passes (293 tests, 0 failures). Spark 4.0, 4.2 and 4.3 are left to the CI matrix.

build/sbt "spark/testOnly io.unitycatalog.spark.ParquetExternalTableReadWriteTest io.unitycatalog.spark.DeltaExternalTableReadWriteTest io.unitycatalog.spark.UCProxySuite"
build/sbt spark/test

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]>
@yili-db

yili-db commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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]>
@yili-db

yili-db commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 60e7ff8, with the (a, p, b) example showing a, p, b through REST and a, b, p in Spark.

@yili-db

yili-db commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

/review

@yili-db yili-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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?

@maharanay22 maharanay22 Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yili-db, thanks for catching that. #2238 moves the paragraph out of the hidden block, to the end of the Create New Table section.

@yili-db
yili-db merged commit bd1b65b into unitycatalog:main Nov 14, 2026
19 checks passed
yili-db pushed a commit that referenced this pull request Nov 15, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants