bmorck commented on code in PR #17499:
URL: https://github.com/apache/iceberg/pull/17499#discussion_r3755111612


##########
spark/v4.1/spark-extensions/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveViews.scala:
##########
@@ -111,16 +119,72 @@ case class ResolveViews(spark: SparkSession) extends 
Rule[LogicalPlan] with Look
 
     // Apply the field aliases and column comments
     // This logic differs from how Spark handles views in 
SessionCatalog.fromCatalogTable.
-    // This is more strict because it doesn't allow resolution by field name.
+    // BINDING is more strict because it doesn't allow resolution by field 
name. COMPENSATION and
+    // TYPE_EVOLUTION coerce as SessionCatalog.castColToType does for those 
modes. Every mode keeps
+    // the stored name and metadata; only the coercion differs.
+    val mode = viewSchemaMode
     val aliases = view.schema.fields.zipWithIndex.map { case (expected, pos) =>
       val attr = GetColumnByOrdinal(pos, expected.dataType)
-      Alias(UpCast(attr, expected.dataType), expected.name)(explicitMetadata =
-        Some(expected.metadata))
+      val coerced =
+        if (mode == SparkSQLProperties.VIEW_SCHEMA_MODE_COMPENSATION) {
+          Cast(attr, expected.dataType, ansiEnabled = true)
+        } else if (mode == SparkSQLProperties.VIEW_SCHEMA_MODE_TYPE_EVOLUTION) 
{
+          attr
+        } else {
+          UpCast(attr, expected.dataType)
+        }
+      Alias(coerced, expected.name)(explicitMetadata = Some(expected.metadata))
     }.toIndexedSeq
 
     SubqueryAlias(nameParts, Project(aliases, rewritten))
   }
 
+  /**
+   * How a view's stored schema is applied to the columns its SQL produces.
+   *
+   * Read on every resolution rather than cached, so that SET takes effect 
within a session.
+   */
+  private def viewSchemaMode: String = {
+    spark.conf.getOption(SparkSQLProperties.VIEW_SCHEMA_BINDING_MODE) match {
+      case Some(mode) =>
+        parseSchemaBindingMode(mode)
+      case None =>
+        // Mirror SessionCatalog.castColToType: turning binding mode off 
selects SchemaUnsupported,
+        // which compensates with an ANSI cast unless compensation is turned 
off as well. Neither conf
+        // can select TYPE_EVOLUTION: in Spark that mode is requested per 
view, with
+        // CREATE or ALTER VIEW ... WITH SCHEMA TYPE EVOLUTION, and stored on 
the view itself.
+        if (isExplicitlyFalse(sparkViewSchemaBindingMode) &&
+          !isExplicitlyFalse(sparkViewSchemaCompensation)) {
+          SparkSQLProperties.VIEW_SCHEMA_MODE_COMPENSATION

Review Comment:
   Thanks for catching this. This wouldn't be backwards compatible since this 
would override the default spark.sql.iceberg.view.schema-binding-mode=BINDING, 
and spark.sql.legacy.viewSchemaBindingMode currently has no effect on V2 views. 
So if someone had been using `spark.sql.legacy.viewSchemaBindingMode` for their 
v1 session catalog views in spark and was expecting it to have no impact on 
their v2 views, this would break that assumption. 
   
   After thinking a bit more about this, I think we should remove 
`spark.sql.legacy.viewSchemaBindingMode` and 
`spark.sql.legacy.viewSchemaCompensation` since they were originally intended 
only for Spark's V1 session catalog views and this would overload their 
original intent.
   
   Went ahead and removed them



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to