osscm commented on code in PR #9830:
URL: https://github.com/apache/iceberg/pull/9830#discussion_r3617790901


##########
spark/v3.5/spark-extensions/src/main/scala/org/apache/spark/sql/execution/datasources/v2/CreateMaterializedViewExec.scala:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.catalog.Namespace
+import org.apache.iceberg.catalog.TableIdentifier
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap
+import org.apache.iceberg.spark.MaterializedViewUtil
+import org.apache.iceberg.spark.Spark3Util
+import org.apache.iceberg.spark.SparkCatalog
+import org.apache.iceberg.spark.SparkSchemaUtil
+import org.apache.iceberg.spark.source.SparkView
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.analysis.ViewAlreadyExistsException
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.connector.catalog.Identifier
+import org.apache.spark.sql.connector.catalog.View
+import org.apache.spark.sql.connector.catalog.ViewCatalog
+import org.apache.spark.sql.connector.expressions.Transform
+import org.apache.spark.sql.types.StructType
+import scala.collection.JavaConverters._
+
+case class CreateMaterializedViewExec(
+    catalog: ViewCatalog,
+    ident: Identifier,
+    queryText: String,
+    viewSchema: StructType,
+    columnAliases: Seq[String],
+    columnComments: Seq[Option[String]],
+    queryColumnNames: Seq[String],
+    comment: Option[String],
+    properties: Map[String, String],
+    allowExisting: Boolean,
+    replace: Boolean,
+    storageTableIdentifier: Option[String])
+    extends LeafV2CommandExec {
+
+  override def output: Seq[Attribute] = Nil
+
+  override protected def run(): Seq[InternalRow] = {
+
+    // Check if storageTableIdentifier is provided. If not, generate a default 
identifier.
+    val sparkStorageTableIdentifier = storageTableIdentifier match {
+      case Some(identifier) => {
+        val catalogAndIdentifier = Spark3Util.catalogAndIdentifier(session, 
identifier)
+        val storageTableCatalogName = catalogAndIdentifier.catalog().name()
+        Preconditions.checkState(
+          storageTableCatalogName.equals(catalog.name()),
+          "Storage table identifier must be in the same catalog as the view." +
+            " Found storage table in catalog: %s, expected: %s.",
+          Array[Object](storageTableCatalogName, catalog.name()))
+        catalogAndIdentifier.identifier()
+      }
+      case None => 
MaterializedViewUtil.getDefaultMaterializedViewStorageTableIdentifier(ident)
+    }
+
+    // Step 1: Create the storage table BEFORE the MV view metadata.
+    // Per spec: "The storage table must exist and be accessible before the
+    // materialized view metadata is committed."
+    // A newly created MV has a storage table with no snapshots until a 
refresh is performed.
+    catalog

Review Comment:
   Curious about the ordering here — could this cause an issue with `IF NOT 
EXISTS / OR REPLACE` when the MV already exists?
   
     Step 1 creates the storage table unconditionally (deterministic name 
<view>__storage), before replace/allowExisting are checked, and it's outside 
the `try/catch` that wraps step 2. 
   
   Step 2 : is where `createView` actually handles allowExisting/replace. So if 
the MV already exists, wouldn't step 1 hit a `TableAlreadyExistsException` on 
the storage table before ever reaching that
     handling in step 2?
     
     Noting that the Trino side ran into something similar and fixed it 
(trinodb/trino#29481, "Avoid recreating storage table on `CREATE OR REPLACE 
MATERIALIZED VIEW`"), so wanted to flag it here too. Also don't see a test in 
`TestMaterializedViews` for `IF NOT EXISTS or REPLACE` against an existing MV — 
might be worth adding if this turns out to be a real gap.



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