wangshuo128 commented on a change in pull request #31968:
URL: https://github.com/apache/spark/pull/31968#discussion_r607812378



##########
File path: 
sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkSQLDriver.scala
##########
@@ -64,9 +65,15 @@ private[hive] class SparkSQLDriver(val context: SQLContext = 
SparkSQLEnv.sqlCont
         new VariableSubstitution().substitute(command)
       }
       context.sparkContext.setJobDescription(substitutorCommand)
-      val execution = 
context.sessionState.executePlan(context.sql(command).logicalPlan)
-      hiveResponse = SQLExecution.withNewExecutionId(execution) {

Review comment:
       `toDF` only produces a `executionId` by 
`SQLExecution.withNewExecutionId`, but the `command`  with side effects would 
run only once.  It happens as below:
   1. when run `sql("show tables")`, Spark generates a `QueryExecution` object. 
The `QueryExecution`'s `LogicalPlan` is eagerly executed when we pass the  
`QueryExecution` to a Dataset and initialize `Dataset.logicalPlan`
   2. when run `show`,  the code goes to 
`Dataset.showString`https://github.com/apache/spark/blob/3b634f66c3e4a942178a1e322ae65ce82779625d/sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala#L340
   and then call `Dataset.getRows`
   
https://github.com/apache/spark/blob/3b634f66c3e4a942178a1e322ae65ce82779625d/sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala#L291
   we new a `Dataset` with the same `QueryExecution` object in _**step1**_
   
https://github.com/apache/spark/blob/3b634f66c3e4a942178a1e322ae65ce82779625d/sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala#L454
   thus, the `command`  would be wrapped in `SQLExecution.withNewExecutionId` 
when initializing the `df.logicalPlan`  , however,  the `QueryExecution` has 
already been optimized and planned, won't execute the underlying command any 
more, see `ExecutedCommandExec.sideEffectResult`
   
https://github.com/apache/spark/blob/3b634f66c3e4a942178a1e322ae65ce82779625d/sql/core/src/main/scala/org/apache/spark/sql/execution/command/commands.scala#L68
   




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

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