manuzhang commented on code in PR #14984:
URL: https://github.com/apache/iceberg/pull/14984#discussion_r3740910623


##########
spark/v4.2/spark-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/IcebergAlterV2ViewUnsetPropertiesExec.scala:
##########
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.spark.sql.execution.datasources.v2
+
+import org.apache.iceberg.spark.Spark3Util
+import org.apache.iceberg.spark.source.SparkView
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.analysis.IcebergAnalysisException
+import org.apache.spark.sql.catalyst.analysis.ResolvedIdentifier
+import org.apache.spark.sql.catalyst.analysis.ViewUtil
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.connector.catalog.Identifier
+import org.apache.spark.sql.connector.catalog.ViewCatalog
+import org.apache.spark.sql.execution.command.CommandUtils
+import scala.jdk.CollectionConverters._
+
+/**
+ * Executes ALTER VIEW UNSET TBLPROPERTIES for Spark V2 views.
+ *
+ * Uses a custom command instead of Spark's built-in implementation so Iceberg 
catalogs commit
+ * property-only metadata updates and reject changes to reserved view 
properties.
+ */
+case class IcebergAlterV2ViewUnsetPropertiesExec(
+    catalog: ViewCatalog,
+    ident: Identifier,
+    propertyKeys: Seq[String],
+    ifExists: Boolean)
+    extends LeafV2CommandExec {
+
+  override lazy val output: Seq[Attribute] = Nil
+
+  override protected def run(): Seq[InternalRow] = {
+    val viewInfo = catalog.loadView(ident)
+    val properties = viewInfo.properties.asScala.toMap
+
+    if (!ifExists) {
+      propertyKeys.filterNot(properties.contains).foreach { property =>
+        throw new IcebergAnalysisException(s"Cannot remove property that is 
not set: '$property'")
+      }
+    }
+
+    propertyKeys.foreach(verifyNonReservedPropertyIsUnset)
+
+    val icebergViewCatalog =
+      ViewUtil
+        .icebergViewCatalog(catalog, ident)
+        .getOrElse(
+          throw new IllegalStateException(
+            s"Cannot load underlying Iceberg view catalog for view: $ident"))
+    val view = 
icebergViewCatalog.loadView(Spark3Util.identifierToTableIdentifier(ident))
+    val update = view.updateProperties()
+    propertyKeys.foreach(update.remove)
+    CommandUtils.uncacheTableOrView(session, ResolvedIdentifier(catalog, 
ident))
+    update.commit()
+
+    Nil
+  }
+
+  override def simpleString(maxFields: Int): String = {
+    s"IcebergAlterV2ViewUnsetProperties: ${ident}"
+  }
+
+  private def verifyNonReservedPropertyIsUnset(property: String): Unit = {
+    if (SparkView.isReservedProperty(property)) {
+      throw new UnsupportedOperationException(s"Cannot unset reserved 
property: '$property'")

Review Comment:
   I’m retaining `UnsupportedOperationException` to preserve the Spark 
4.1/Iceberg behavior for reserved-property mutations. 
`IcebergAnalysisException` remains specific to missing user properties.



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