-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-35378][SQL] Eagerly execute commands in QueryExecution instead of caller sides #32513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 31 commits
dad709c
0338a23
40cea6b
a82ed76
bf296fb
babe0d0
803a12a
4c9b3cf
e3b8454
6516cc4
ddbb5bb
bde1062
7a7bc55
2ad5391
33f0297
309fb0f
8e9277d
d006b2a
fc3afe3
fde3c31
b58c0ea
4dcc759
f47dda6
2a61f23
cd7d39c
6c74474
26c2341
d11b00d
a61c267
8fe6c06
b20b000
b60e04e
94ba930
0ed9485
911e081
ecfe9ba
cfeadd1
6a80674
c81b082
ee1e84a
9c95570
219abb4
f39f920
1d10b61
2bcdddd
5d9f7ee
6011bbe
0905d84
3f6cb85
1d821e0
ddbc5c4
d545d9b
4b730d4
de55034
1a3ce51
ccf8ba3
35ea747
2b2caf7
1db52b9
83d2710
8054799
d15e166
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, UnsafeProjection} | ||
| import org.apache.spark.sql.catalyst.plans.QueryPlan | ||
| import org.apache.spark.sql.execution.metric.SQLMetrics | ||
|
|
||
| /** | ||
| * Physical plan node for holding data from a command. | ||
| * | ||
| * `rows` may not be serializable and ideally we should not send `rows` to the executors. | ||
| * Thus marking it as transient. | ||
| */ | ||
| case class CommandResultExec( | ||
|
cloud-fan marked this conversation as resolved.
|
||
| output: Seq[Attribute], | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. override def output: Seq[Attribute] = commandPhysicalPlan.output?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we obtain the output in |
||
| commandPhysicalPlan: SparkPlan, | ||
| @transient rows: Seq[InternalRow]) extends LeafExecNode { | ||
|
|
||
| override lazy val metrics = Map( | ||
| "numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output rows")) | ||
|
|
||
| override def innerChildren: Seq[QueryPlan[_]] = Seq(commandPhysicalPlan) | ||
|
|
||
| @transient private lazy val unsafeRows: Array[InternalRow] = { | ||
|
cloud-fan marked this conversation as resolved.
|
||
| if (rows.isEmpty) { | ||
| Array.empty | ||
| } else { | ||
| val proj = UnsafeProjection.create(output, output) | ||
| rows.map(r => proj(r).copy()).toArray | ||
| } | ||
| } | ||
|
|
||
| @transient private lazy val rdd: RDD[InternalRow] = { | ||
| if (rows.isEmpty) { | ||
| sqlContext.sparkContext.emptyRDD | ||
| } else { | ||
| val numSlices = math.min( | ||
| unsafeRows.length, sqlContext.sparkSession.leafNodeDefaultParallelism) | ||
| sqlContext.sparkContext.parallelize(unsafeRows, numSlices) | ||
| } | ||
| } | ||
|
|
||
| override def doExecute(): RDD[InternalRow] = { | ||
| val numOutputRows = longMetric("numOutputRows") | ||
| rdd.map { r => | ||
| numOutputRows += 1 | ||
| r | ||
| } | ||
| } | ||
|
|
||
| override protected def stringArgs: Iterator[Any] = { | ||
| if (unsafeRows.isEmpty) { | ||
| Iterator("<empty>", output) | ||
| } else { | ||
| Iterator(output) | ||
| } | ||
| } | ||
|
|
||
| override def executeCollect(): Array[InternalRow] = { | ||
| longMetric("numOutputRows").add(unsafeRows.size) | ||
| unsafeRows | ||
| } | ||
|
|
||
| override def executeTake(limit: Int): Array[InternalRow] = { | ||
| val taken = unsafeRows.take(limit) | ||
| longMetric("numOutputRows").add(taken.size) | ||
| taken | ||
| } | ||
|
|
||
| override def executeTail(limit: Int): Array[InternalRow] = { | ||
| val taken: Seq[InternalRow] = unsafeRows.takeRight(limit) | ||
| longMetric("numOutputRows").add(taken.size) | ||
| taken.toArray | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,7 @@ import org.apache.spark.sql.catalyst.analysis.UnsupportedOperationChecker | |
| import org.apache.spark.sql.catalyst.expressions.SubqueryExpression | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.ByteCodeStats | ||
| import org.apache.spark.sql.catalyst.plans.QueryPlan | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, ReturnAnswer} | ||
| import org.apache.spark.sql.catalyst.plans.logical.{Command, LogicalPlan, ReturnAnswer} | ||
| import org.apache.spark.sql.catalyst.rules.{PlanChangeLogger, Rule} | ||
| import org.apache.spark.sql.catalyst.util.StringUtils.PlanStringConcat | ||
| import org.apache.spark.sql.catalyst.util.truncatedString | ||
|
|
@@ -40,6 +40,7 @@ import org.apache.spark.sql.execution.bucketing.{CoalesceBucketsInJoin, DisableU | |
| import org.apache.spark.sql.execution.dynamicpruning.PlanDynamicPruningFilters | ||
| import org.apache.spark.sql.execution.exchange.{EnsureRequirements, ReuseExchange} | ||
| import org.apache.spark.sql.execution.streaming.{IncrementalExecution, OffsetSeqMetadata} | ||
| import org.apache.spark.sql.expressions.CommandResult | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.streaming.OutputMode | ||
| import org.apache.spark.util.Utils | ||
|
|
@@ -54,7 +55,8 @@ import org.apache.spark.util.Utils | |
| class QueryExecution( | ||
| val sparkSession: SparkSession, | ||
| val logical: LogicalPlan, | ||
| val tracker: QueryPlanningTracker = new QueryPlanningTracker) extends Logging { | ||
| val tracker: QueryPlanningTracker = new QueryPlanningTracker, | ||
| val isExecutingCommand: Boolean = false) extends Logging { | ||
|
|
||
| val id: Long = QueryExecution.nextExecutionId | ||
|
|
||
|
|
@@ -74,12 +76,33 @@ class QueryExecution( | |
| sparkSession.sessionState.analyzer.executeAndCheck(logical, tracker) | ||
| } | ||
|
|
||
| // SPARK-35378: Commands should be executed eagerly so that `sql("INSERT ...")` can trigger the | ||
| // table insertion immediately without a `.collect()`. We also need to eagerly execute non-root | ||
| // commands, because many commands return `GenericInternalRow` and can't be put in a query plan | ||
| // directly, otherwise the query engine may cast `GenericInternalRow` to `UnsafeRow` and fail. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now it seems better to put the doc in |
||
| lazy val commandExecuted: LogicalPlan = if (isExecutingCommand) { | ||
| analyzed.mapChildren(eagerlyExecuteCommands) | ||
| } else { | ||
| eagerlyExecuteCommands(analyzed) | ||
| } | ||
|
|
||
| private def eagerlyExecuteCommands(p: LogicalPlan) = p transformDown { | ||
| case c: Command => | ||
| val qe = sparkSession.sessionState.executePlan(c, true) | ||
| CommandResult( | ||
| qe.analyzed.output, | ||
| qe.commandExecuted, | ||
| qe.executedPlan, | ||
| SQLExecution.withNewExecutionId(qe, Some("command"))(qe.executedPlan.executeCollect())) | ||
| case other => other | ||
| } | ||
|
|
||
| lazy val withCachedData: LogicalPlan = sparkSession.withActive { | ||
| assertAnalyzed() | ||
| assertSupported() | ||
| // clone the plan to avoid sharing the plan instance between different stages like analyzing, | ||
| // optimizing and planning. | ||
| sparkSession.sharedState.cacheManager.useCachedData(analyzed.clone()) | ||
| sparkSession.sharedState.cacheManager.useCachedData(commandExecuted.clone()) | ||
| } | ||
|
|
||
| lazy val optimizedPlan: LogicalPlan = executePhase(QueryPlanningTracker.OPTIMIZATION) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make sure we don't include command execution time in the optimization phase, it's better to follow
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * 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.expressions | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a public package which makes
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.catalyst.plans.QueryPlan | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan} | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
|
|
||
| /** | ||
| * Logical plan node for holding data from a command. | ||
| * | ||
| * `rows` may not be serializable and ideally we should not send `rows` to the executors. | ||
| * Thus marking it as transient. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto, them |
||
| */ | ||
| case class CommandResult( | ||
| output: Seq[Attribute], | ||
| commandLogicalPlan: LogicalPlan, | ||
| commandPhysicalPlan: SparkPlan, | ||
| @transient rows: Seq[InternalRow]) extends LeafNode { | ||
| override def innerChildren: Seq[QueryPlan[_]] = Seq(commandLogicalPlan) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it -> them