You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "heyihong (via GitHub)" <gi...@apache.org> on 2025/07/30 15:24:12 UTC

[PR] [SPARK-52451][CONNECT] Make WriteOperation in SparkConnectPlanner side effect free [spark]

heyihong opened a new pull request, #51727:
URL: https://github.com/apache/spark/pull/51727

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'common/utils/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
   
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "heyihong (via GitHub)" <gi...@apache.org>.
heyihong commented on code in PR #51727:
URL: https://github.com/apache/spark/pull/51727#discussion_r2245459671


##########
sql/core/src/main/scala/org/apache/spark/sql/classic/DataFrameWriter.scala:
##########
@@ -551,17 +551,31 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) extends sql.DataFram
           case _ => // OK
         }
 
-        // Drop the existing table
-        catalog.dropTable(qualifiedIdent, ignoreIfNotExists = true, purge = false)
-        createTable(qualifiedIdent)
-        // Refresh the cache of the table in the catalog.
-        catalog.refreshTable(qualifiedIdent)
+        val dropTablePlan = DropTableCommand(
+          qualifiedIdent,
+          ifExists = true,
+          isView = tableRelation.isInstanceOf[org.apache.spark.sql.catalyst.plans.logical.View],
+          purge = false)
+
+        val createTablePlan = createTableLogicalPlan(qualifiedIdent)
+
+        val refreshTablePlan = RefreshTableCommand(qualifiedIdent)
+
+        CompoundBody(

Review Comment:
   Yes, CompoundBody is actually executed during [the analysis phase](https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/QueryExecution.scala#L104). But the track doesn't seem to get updated correctly. So I made a small fix in this pr



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #51727:
URL: https://github.com/apache/spark/pull/51727#issuecomment-3138664812

   The idea LGTM, we can simplify it futher in the future by using a simple logical plan for each `DataFrameWriter` API. Then Spark Connect can just use that logical plan, instead of calling `DataFrameWriter` to generate the logical plan.


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "heyihong (via GitHub)" <gi...@apache.org>.
heyihong commented on PR #51727:
URL: https://github.com/apache/spark/pull/51727#issuecomment-3137074816

   @HyukjinKwon @cloud-fan @hvanhovell 


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "heyihong (via GitHub)" <gi...@apache.org>.
heyihong commented on code in PR #51727:
URL: https://github.com/apache/spark/pull/51727#discussion_r2245459671


##########
sql/core/src/main/scala/org/apache/spark/sql/classic/DataFrameWriter.scala:
##########
@@ -551,17 +551,31 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) extends sql.DataFram
           case _ => // OK
         }
 
-        // Drop the existing table
-        catalog.dropTable(qualifiedIdent, ignoreIfNotExists = true, purge = false)
-        createTable(qualifiedIdent)
-        // Refresh the cache of the table in the catalog.
-        catalog.refreshTable(qualifiedIdent)
+        val dropTablePlan = DropTableCommand(
+          qualifiedIdent,
+          ifExists = true,
+          isView = tableRelation.isInstanceOf[org.apache.spark.sql.catalyst.plans.logical.View],
+          purge = false)
+
+        val createTablePlan = createTableLogicalPlan(qualifiedIdent)
+
+        val refreshTablePlan = RefreshTableCommand(qualifiedIdent)
+
+        CompoundBody(

Review Comment:
   Yes, CompoundBody is actually executed during [the analysis phase](https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/QueryExecution.scala#L104). But the track doesn't seem to get updated correctly.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #51727:
URL: https://github.com/apache/spark/pull/51727#discussion_r2250898281


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ddl.scala:
##########
@@ -956,6 +956,64 @@ case class AlterTableSetLocationCommand(
   }
 }
 
+/**
+ * A command that saves a query as a V1 table.
+ */
+private[sql] case class SaveAsV1TableCommand(
+    tableDesc: CatalogTable,
+    mode: SaveMode,
+    query: LogicalPlan) extends LeafRunnableCommand {
+  override def run(sparkSession: SparkSession): Seq[Row] = {
+    val catalog = sparkSession.sessionState.catalog
+    val qualifiedIdent = catalog.qualifyIdentifier(tableDesc.identifier)
+    val tableDescWithQualifiedIdent = tableDesc.copy(identifier = qualifiedIdent)
+    val tableExists = catalog.tableExists(qualifiedIdent)
+
+    (tableExists, mode) match {
+      case (true, SaveMode.Ignore) =>
+        // Do nothing
+
+      case (true, SaveMode.ErrorIfExists) =>
+        throw QueryCompilationErrors.tableAlreadyExistsError(qualifiedIdent)
+
+      case (true, SaveMode.Overwrite) =>
+        // Get all input data source or hive relations of the query.
+        val srcRelations = query.collect {
+          case l: LogicalRelation => l.relation
+          case relation: HiveTableRelation => relation.tableMeta.identifier
+        }
+
+        val tableRelation = sparkSession.table(qualifiedIdent).queryExecution.analyzed
+        EliminateSubqueryAliases(tableRelation) match {
+          // check if the table is a data source table (the relation is a BaseRelation).
+          case l: LogicalRelation if srcRelations.contains(l.relation) =>
+            throw QueryCompilationErrors.cannotOverwriteTableThatIsBeingReadFromError(
+              qualifiedIdent)
+          // check hive table relation when overwrite mode
+          case relation: HiveTableRelation
+              if srcRelations.contains(relation.tableMeta.identifier) =>
+            throw QueryCompilationErrors.cannotOverwriteTableThatIsBeingReadFromError(
+              qualifiedIdent)
+          case _ => // OK
+        }
+
+        // Drop the existing table
+        catalog.dropTable(qualifiedIdent, ignoreIfNotExists = true, purge = false)
+        runCommand(sparkSession)(CreateTable(tableDescWithQualifiedIdent, mode, Some(query)))
+        // Refresh the cache of the table in the catalog.
+        catalog.refreshTable(qualifiedIdent)
+
+      case _ =>
+        runCommand(sparkSession)(CreateTable(tableDescWithQualifiedIdent, mode, Some(query)))
+    }
+    Seq.empty[Row]
+  }
+
+  private def runCommand(session: SparkSession)(command: LogicalPlan): Unit = {

Review Comment:
   ```suggestion
     private def runCommand(session: SparkSession, command: LogicalPlan): Unit = {
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #51727: [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free
URL: https://github.com/apache/spark/pull/51727


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #51727:
URL: https://github.com/apache/spark/pull/51727#issuecomment-3151371860

   thanks, merging to master!


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #51727:
URL: https://github.com/apache/spark/pull/51727#discussion_r2244430748


##########
sql/core/src/main/scala/org/apache/spark/sql/classic/DataFrameWriter.scala:
##########
@@ -551,17 +551,31 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) extends sql.DataFram
           case _ => // OK
         }
 
-        // Drop the existing table
-        catalog.dropTable(qualifiedIdent, ignoreIfNotExists = true, purge = false)
-        createTable(qualifiedIdent)
-        // Refresh the cache of the table in the catalog.
-        catalog.refreshTable(qualifiedIdent)
+        val dropTablePlan = DropTableCommand(
+          qualifiedIdent,
+          ifExists = true,
+          isView = tableRelation.isInstanceOf[org.apache.spark.sql.catalyst.plans.logical.View],
+          purge = false)
+
+        val createTablePlan = createTableLogicalPlan(qualifiedIdent)
+
+        val refreshTablePlan = RefreshTableCommand(qualifiedIdent)
+
+        CompoundBody(

Review Comment:
   Does this really work with `runCommand`?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #51727:
URL: https://github.com/apache/spark/pull/51727#discussion_r2247794830


##########
sql/core/src/main/scala/org/apache/spark/sql/classic/DataFrameWriter.scala:
##########
@@ -551,17 +551,31 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) extends sql.DataFram
           case _ => // OK
         }
 
-        // Drop the existing table
-        catalog.dropTable(qualifiedIdent, ignoreIfNotExists = true, purge = false)
-        createTable(qualifiedIdent)
-        // Refresh the cache of the table in the catalog.
-        catalog.refreshTable(qualifiedIdent)
+        val dropTablePlan = DropTableCommand(
+          qualifiedIdent,
+          ifExists = true,
+          isView = tableRelation.isInstanceOf[org.apache.spark.sql.catalyst.plans.logical.View],
+          purge = false)
+
+        val createTablePlan = createTableLogicalPlan(qualifiedIdent)
+
+        val refreshTablePlan = RefreshTableCommand(qualifiedIdent)
+
+        CompoundBody(

Review Comment:
   I'm a bit worried about using `CompoundBody` outside of SQL script execution, and the fix in https://github.com/apache/spark/pull/51727/files#diff-a3fb0a56a7d2d08dc87434eff5b43aba0b006b59ed2f25e29bfb6fb4f81ec0c4R164 is a bit suspicious.
   
   Can we create a new command `SaveAsTableCommand` to do these operations? 



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "heyihong (via GitHub)" <gi...@apache.org>.
heyihong commented on code in PR #51727:
URL: https://github.com/apache/spark/pull/51727#discussion_r2245459671


##########
sql/core/src/main/scala/org/apache/spark/sql/classic/DataFrameWriter.scala:
##########
@@ -551,17 +551,31 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) extends sql.DataFram
           case _ => // OK
         }
 
-        // Drop the existing table
-        catalog.dropTable(qualifiedIdent, ignoreIfNotExists = true, purge = false)
-        createTable(qualifiedIdent)
-        // Refresh the cache of the table in the catalog.
-        catalog.refreshTable(qualifiedIdent)
+        val dropTablePlan = DropTableCommand(
+          qualifiedIdent,
+          ifExists = true,
+          isView = tableRelation.isInstanceOf[org.apache.spark.sql.catalyst.plans.logical.View],
+          purge = false)
+
+        val createTablePlan = createTableLogicalPlan(qualifiedIdent)
+
+        val refreshTablePlan = RefreshTableCommand(qualifiedIdent)
+
+        CompoundBody(

Review Comment:
   Yes, CompoundBody is actually executed during [the analysis phase](https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/QueryExecution.scala#L104)



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-52451][CONNECT][SQL] Make WriteOperation in SparkConnectPlanner side effect free [spark]

Posted by "heyihong (via GitHub)" <gi...@apache.org>.
heyihong commented on code in PR #51727:
URL: https://github.com/apache/spark/pull/51727#discussion_r2245459671


##########
sql/core/src/main/scala/org/apache/spark/sql/classic/DataFrameWriter.scala:
##########
@@ -551,17 +551,31 @@ final class DataFrameWriter[T] private[sql](ds: Dataset[T]) extends sql.DataFram
           case _ => // OK
         }
 
-        // Drop the existing table
-        catalog.dropTable(qualifiedIdent, ignoreIfNotExists = true, purge = false)
-        createTable(qualifiedIdent)
-        // Refresh the cache of the table in the catalog.
-        catalog.refreshTable(qualifiedIdent)
+        val dropTablePlan = DropTableCommand(
+          qualifiedIdent,
+          ifExists = true,
+          isView = tableRelation.isInstanceOf[org.apache.spark.sql.catalyst.plans.logical.View],
+          purge = false)
+
+        val createTablePlan = createTableLogicalPlan(qualifiedIdent)
+
+        val refreshTablePlan = RefreshTableCommand(qualifiedIdent)
+
+        CompoundBody(

Review Comment:
   Yes, CompoundBody is actually executed during [the analysis phase](https://github.com/apache/spark/blob/master/sql/core/src/main/scala/org/apache/spark/sql/execution/QueryExecution.scala#L104). But the tracker doesn't seem to get updated correctly. So I made a small fix in this pr



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org