Spark: Make the view stored-schema coercion configurable - #17453
Closed
bmorck wants to merge 2 commits into
Closed
Conversation
ResolveViews rebuilds a view's output from the stored schema, by position, wrapping each column in an UpCast. UpCast only widens, so a view whose stored type is narrower than what its SQL produces cannot be read at all, and there is no way to relax it. Add spark.sql.iceberg.view.schema-binding-mode, taking its mode names and coercions from Spark's ViewSchemaMode: BINDING (UpCast, the default and current behaviour), COMPENSATION (an ANSI cast, allowing narrowing) and TYPE_EVOLUTION (no cast, so the view reports the types its SQL produces). All three keep the stored column name and metadata. When the conf is unset, Spark's spark.sql.legacy.viewSchemaBindingMode and viewSchemaCompensation are honored instead, reproducing how SessionCatalog.castColToType treats SchemaUnsupported.
bmorck
marked this pull request as ready for review
July 31, 2026 16:23
Contributor
|
You should open a PR just for the latest version of Spark, to start with. Once that's merged then we can open PRs to backport to older versions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ResolveViewsrebuilds a view's output from the stored schema and wraps each column in anUpCast.UpCastonly widens, so a view whose stored type is narrower than what its SQL produces cannot be read at all and gets an error like the following:There is currently no way to relax that. Spark's has the
spark.sql.legacy.viewSchemaBindingModeandspark.sql.legacy.viewSchemaCompensationconfs to relax this behavior on the v1SessionCatalogview path, but this doesn't apply to v2 views.This adds
spark.sql.iceberg.view.schema-binding-mode, taking its mode names from Spark'sViewSchemaMode:BINDINGUpCast(col, storedType)— the current behaviour, and the defaultCOMPENSATIONCast(col, storedType, ansiEnabled = true)— narrowing allowedTYPE_EVOLUTIONThese mirror Spark's
SessionCatalog.castColToTypebehavior.TYPE_EVOLUTIONhere is the read-side coercion only and doesn't follow the methodology of Spark'sWITH SCHEMA TYPE EVOLUTION, which also syncs the corrected schema back to the catalog, throughViewSyncSchemaToMetaStore. This was an intentional choice since syncing the schema back to the catalog seems wrong to do from a session-level conf, since any reader would then mutate a shared view definition; it would belong with a per-view property, as Spark stores the mode on the view itself.When the conf is unset, Spark's two legacy confs above are honored instead, reproducing how Spark's
SessionCatalog.castColToTypetreatsSchemaUnsupported. The benefit of reading the Spark'sspark.sql.legacy.viewSchemaBindingModeis that it provides a single conf to relax the upcast across both v1 and v2 view implementations, but happy to drop the fallback if folks think it is best left out, the Iceberg conf is sufficient as is.Neither of Spark's confs can select
TYPE_EVOLUTION, since there it is a per-view clause rather than a session default. The Iceberg conf takes precedence when both are set. The mode is read on each view resolution rather than cached on the rule, soSETapplies within a session.Tests, in
TestViewsfor each Spark version, over a view whose stored schema declaresBIGINTfor a column its SQL produces asDOUBLE: the default still fails;COMPENSATIONreads the rows asLongType;TYPE_EVOLUTIONreads them asDoubleTypeand keeps the stored column name