maropu commented on a change in pull request #32298:
URL: https://github.com/apache/spark/pull/32298#discussion_r626976124
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/subquery.scala
##########
@@ -267,6 +268,33 @@ object ScalarSubquery {
}
}
+/**
+ * A subquery that is capable to return multiple scalar values.
+ */
+case class MultiScalarSubquery(
+ plan: LogicalPlan,
+ exprId: ExprId = NamedExpression.newExprId)
+ extends SubqueryExpression(plan, Seq.empty, exprId) with
LeafLike[Expression] with Unevaluable {
+ override def dataType: DataType = {
+ assert(plan.schema.nonEmpty, "Multi-column scalar subquery should have
columns")
Review comment:
`assert(plan.schema.size > 1, ...)` ?
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
##########
@@ -1353,6 +1353,14 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val SCALAR_SUBQUERY_MERGE_ENABLED =
+ buildConf("spark.sql.scalarSubqueyMerge.enabled")
Review comment:
`spark.sql.optimizer.scalarSubqueyMerging.enabled` (or
`spark.sql.optimizer.mergeScalarSubqueries.enabled`) instead?
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
##########
@@ -1353,6 +1353,14 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val SCALAR_SUBQUERY_MERGE_ENABLED =
+ buildConf("spark.sql.scalarSubqueyMerge.enabled")
Review comment:
I think we need to describe more, e.g., To enable this
feature,`spark.sql.execution.reuseSubquery` needs to be true, etc.
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.catalyst.optimizer
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LeafNode,
LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{MULTI_SCALAR_SUBQUERY,
SCALAR_SUBQUERY}
+
+/**
+ * This rule tries to merge multiple non-correlated [[ScalarSubquery]]s into a
+ * [[MultiScalarSubquery]] to compute multiple scalar values once.
+ *
+ * The process is the following:
+ * - While traversing through the plan each [[ScalarSubquery]] plan is tried
to merge into the cache
+ * of already seen subquery plans. If merge is possible then cache is
updated with the merged
+ * subquery plan, if not then the new subquery plan is added to the cache.
+ * - The original [[ScalarSubquery]] expression is replaced to a reference
pointing to its cached
+ * version in this form:
`GetStructField(MultiScalarSubquery(SubqueryReference(...)))`.
+ * - A second traversal checks if a [[SubqueryReference]] is pointing to a
subquery plan that
+ * returns multiple values and either replaces only [[SubqueryReference]] to
the cached plan or
+ * restores the whole expression to its original [[ScalarSubquery]] form.
+ * - [[ReuseSubquery]] rule makes sure that merged subqueries are computed
once.
+ *
+ * Eg. the following query:
+ *
+ * SELECT
+ * (SELECT avg(a) FROM t GROUP BY b),
+ * (SELECT sum(b) FROM t GROUP BY b)
+ *
+ * is optimized from:
+ *
+ * Project [scalar-subquery#231 [] AS scalarsubquery()#241,
+ * scalar-subquery#232 [] AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#240], [sum(b#240) AS sum(b)#238L]
+ * : +- Project [b#240]
+ * : +- Relation default.t[a#239,b#240] parquet
+ * +- OneRowRelation
+ *
+ * to:
+ *
+ * Project [multi-scalar-subquery#231.avg(a) AS scalarsubquery()#241,
+ * multi-scalar-subquery#232.sum(b) AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : : +- Project [a#233, b#234]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : +- Project [a#233, b#234]
+ * : +- Relation default.t[a#233,b#234] parquet
+ * +- OneRowRelation
+ */
+object MergeScalarSubqueries extends Rule[LogicalPlan] with PredicateHelper {
+ def apply(plan: LogicalPlan): LogicalPlan = {
+ if (conf.scalarSubqueryMergeEabled && conf.subqueryReuseEnabled) {
+ val mergedSubqueries = ArrayBuffer.empty[LogicalPlan]
+ removeReferences(mergeAndInsertReferences(plan, mergedSubqueries),
mergedSubqueries)
+ } else {
+ plan
+ }
+ }
+
+ private def mergeAndInsertReferences(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): LogicalPlan = {
+
plan.transformAllExpressionsWithPruning(_.containsAnyPattern(SCALAR_SUBQUERY),
ruleId) {
+ case s: ScalarSubquery if s.children.isEmpty =>
+ val (mergedPlan, ordinal) = mergeAndGetReference(s.plan,
mergedSubqueries)
+ GetStructField(MultiScalarSubquery(mergedPlan, s.exprId), ordinal)
+ }
+ }
+
+ case class SubqueryReference(
+ index: Int,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]) extends LeafNode {
+ override def stringArgs: Iterator[Any] = Iterator(index)
+
+ override def output: Seq[Attribute] = mergedSubqueries(index).output
+ }
+
+ private def mergeAndGetReference(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): (SubqueryReference, Int) = {
+ mergedSubqueries.zipWithIndex.collectFirst {
+ Function.unlift { case (s, i) => mergePlans(plan, s).map(_ -> i) }
+ }.map { case ((mergedPlan, outputMap), i) =>
+ mergedSubqueries(i) = mergedPlan
+ SubqueryReference(i, mergedSubqueries) ->
+ mergedPlan.output.indexOf(outputMap(plan.output.head))
+ }.getOrElse {
+ mergedSubqueries += plan
+ SubqueryReference(mergedSubqueries.length - 1, mergedSubqueries) -> 0
+ }
+ }
+
+ private def mergePlans(
+ newPlan: LogicalPlan,
+ existingPlan: LogicalPlan): Option[(LogicalPlan,
AttributeMap[Attribute])] = {
+ (newPlan, existingPlan) match {
+ case (np, ep) if np.canonicalized == ep.canonicalized =>
+ Some(ep -> AttributeMap(np.output.zip(ep.output)))
+ case (np: Project, ep: Project) =>
+ mergePlans(np.child, ep.child).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.projectList ++ newProjectList),
mergedChild) ->
+ newOutputMap
+ }
+ case (np, ep: Project) =>
+ mergePlans(np, ep.child).map { case (mergedChild, outputMap) =>
+ Project(distinctExpressions(ep.projectList ++ outputMap.values),
mergedChild) -> outputMap
+ }
+ case (np: Project, ep) =>
+ mergePlans(np.child, ep).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.output ++ newProjectList),
mergedChild) -> newOutputMap
+ }
Review comment:
Which kind of queries does this case handle? (this PR already has any
test for this code path?) Is it safe to accept any plan node if the other side
is `Project`?
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.catalyst.optimizer
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LeafNode,
LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{MULTI_SCALAR_SUBQUERY,
SCALAR_SUBQUERY}
+
+/**
+ * This rule tries to merge multiple non-correlated [[ScalarSubquery]]s into a
+ * [[MultiScalarSubquery]] to compute multiple scalar values once.
+ *
+ * The process is the following:
+ * - While traversing through the plan each [[ScalarSubquery]] plan is tried
to merge into the cache
+ * of already seen subquery plans. If merge is possible then cache is
updated with the merged
+ * subquery plan, if not then the new subquery plan is added to the cache.
+ * - The original [[ScalarSubquery]] expression is replaced to a reference
pointing to its cached
+ * version in this form:
`GetStructField(MultiScalarSubquery(SubqueryReference(...)))`.
+ * - A second traversal checks if a [[SubqueryReference]] is pointing to a
subquery plan that
+ * returns multiple values and either replaces only [[SubqueryReference]] to
the cached plan or
+ * restores the whole expression to its original [[ScalarSubquery]] form.
+ * - [[ReuseSubquery]] rule makes sure that merged subqueries are computed
once.
+ *
+ * Eg. the following query:
+ *
+ * SELECT
+ * (SELECT avg(a) FROM t GROUP BY b),
+ * (SELECT sum(b) FROM t GROUP BY b)
+ *
+ * is optimized from:
+ *
+ * Project [scalar-subquery#231 [] AS scalarsubquery()#241,
+ * scalar-subquery#232 [] AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#240], [sum(b#240) AS sum(b)#238L]
+ * : +- Project [b#240]
+ * : +- Relation default.t[a#239,b#240] parquet
+ * +- OneRowRelation
+ *
+ * to:
+ *
+ * Project [multi-scalar-subquery#231.avg(a) AS scalarsubquery()#241,
+ * multi-scalar-subquery#232.sum(b) AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : : +- Project [a#233, b#234]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : +- Project [a#233, b#234]
+ * : +- Relation default.t[a#233,b#234] parquet
+ * +- OneRowRelation
+ */
+object MergeScalarSubqueries extends Rule[LogicalPlan] with PredicateHelper {
+ def apply(plan: LogicalPlan): LogicalPlan = {
+ if (conf.scalarSubqueryMergeEabled && conf.subqueryReuseEnabled) {
+ val mergedSubqueries = ArrayBuffer.empty[LogicalPlan]
+ removeReferences(mergeAndInsertReferences(plan, mergedSubqueries),
mergedSubqueries)
+ } else {
+ plan
+ }
+ }
+
+ private def mergeAndInsertReferences(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): LogicalPlan = {
+
plan.transformAllExpressionsWithPruning(_.containsAnyPattern(SCALAR_SUBQUERY),
ruleId) {
+ case s: ScalarSubquery if s.children.isEmpty =>
+ val (mergedPlan, ordinal) = mergeAndGetReference(s.plan,
mergedSubqueries)
+ GetStructField(MultiScalarSubquery(mergedPlan, s.exprId), ordinal)
+ }
+ }
+
+ case class SubqueryReference(
+ index: Int,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]) extends LeafNode {
+ override def stringArgs: Iterator[Any] = Iterator(index)
+
+ override def output: Seq[Attribute] = mergedSubqueries(index).output
+ }
+
+ private def mergeAndGetReference(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): (SubqueryReference, Int) = {
+ mergedSubqueries.zipWithIndex.collectFirst {
+ Function.unlift { case (s, i) => mergePlans(plan, s).map(_ -> i) }
+ }.map { case ((mergedPlan, outputMap), i) =>
+ mergedSubqueries(i) = mergedPlan
+ SubqueryReference(i, mergedSubqueries) ->
+ mergedPlan.output.indexOf(outputMap(plan.output.head))
+ }.getOrElse {
+ mergedSubqueries += plan
+ SubqueryReference(mergedSubqueries.length - 1, mergedSubqueries) -> 0
+ }
+ }
+
+ private def mergePlans(
+ newPlan: LogicalPlan,
+ existingPlan: LogicalPlan): Option[(LogicalPlan,
AttributeMap[Attribute])] = {
+ (newPlan, existingPlan) match {
+ case (np, ep) if np.canonicalized == ep.canonicalized =>
+ Some(ep -> AttributeMap(np.output.zip(ep.output)))
+ case (np: Project, ep: Project) =>
+ mergePlans(np.child, ep.child).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.projectList ++ newProjectList),
mergedChild) ->
+ newOutputMap
+ }
+ case (np, ep: Project) =>
+ mergePlans(np, ep.child).map { case (mergedChild, outputMap) =>
+ Project(distinctExpressions(ep.projectList ++ outputMap.values),
mergedChild) -> outputMap
+ }
+ case (np: Project, ep) =>
+ mergePlans(np.child, ep).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.output ++ newProjectList),
mergedChild) -> newOutputMap
+ }
+ case (np: Aggregate, ep: Aggregate) =>
Review comment:
We can always assume merging two (or more) aggregates makes performance
better? For example, we have two aggregates in a plan, one side is a
hash-aggregate and the other side is an object hash-aggregate. In this case,
the merged plan node seems to be an object-hash aggregate. If this is true,
this rewrite can easily cause high memory pressure.
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.catalyst.optimizer
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LeafNode,
LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{MULTI_SCALAR_SUBQUERY,
SCALAR_SUBQUERY}
+
+/**
+ * This rule tries to merge multiple non-correlated [[ScalarSubquery]]s into a
+ * [[MultiScalarSubquery]] to compute multiple scalar values once.
+ *
+ * The process is the following:
+ * - While traversing through the plan each [[ScalarSubquery]] plan is tried
to merge into the cache
+ * of already seen subquery plans. If merge is possible then cache is
updated with the merged
+ * subquery plan, if not then the new subquery plan is added to the cache.
+ * - The original [[ScalarSubquery]] expression is replaced to a reference
pointing to its cached
+ * version in this form:
`GetStructField(MultiScalarSubquery(SubqueryReference(...)))`.
+ * - A second traversal checks if a [[SubqueryReference]] is pointing to a
subquery plan that
+ * returns multiple values and either replaces only [[SubqueryReference]] to
the cached plan or
+ * restores the whole expression to its original [[ScalarSubquery]] form.
+ * - [[ReuseSubquery]] rule makes sure that merged subqueries are computed
once.
+ *
+ * Eg. the following query:
+ *
+ * SELECT
+ * (SELECT avg(a) FROM t GROUP BY b),
+ * (SELECT sum(b) FROM t GROUP BY b)
+ *
+ * is optimized from:
+ *
+ * Project [scalar-subquery#231 [] AS scalarsubquery()#241,
+ * scalar-subquery#232 [] AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#240], [sum(b#240) AS sum(b)#238L]
+ * : +- Project [b#240]
+ * : +- Relation default.t[a#239,b#240] parquet
+ * +- OneRowRelation
+ *
+ * to:
+ *
+ * Project [multi-scalar-subquery#231.avg(a) AS scalarsubquery()#241,
+ * multi-scalar-subquery#232.sum(b) AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : : +- Project [a#233, b#234]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : +- Project [a#233, b#234]
+ * : +- Relation default.t[a#233,b#234] parquet
+ * +- OneRowRelation
+ */
+object MergeScalarSubqueries extends Rule[LogicalPlan] with PredicateHelper {
+ def apply(plan: LogicalPlan): LogicalPlan = {
+ if (conf.scalarSubqueryMergeEabled && conf.subqueryReuseEnabled) {
+ val mergedSubqueries = ArrayBuffer.empty[LogicalPlan]
+ removeReferences(mergeAndInsertReferences(plan, mergedSubqueries),
mergedSubqueries)
+ } else {
+ plan
+ }
+ }
+
+ private def mergeAndInsertReferences(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): LogicalPlan = {
+
plan.transformAllExpressionsWithPruning(_.containsAnyPattern(SCALAR_SUBQUERY),
ruleId) {
+ case s: ScalarSubquery if s.children.isEmpty =>
+ val (mergedPlan, ordinal) = mergeAndGetReference(s.plan,
mergedSubqueries)
+ GetStructField(MultiScalarSubquery(mergedPlan, s.exprId), ordinal)
+ }
+ }
+
+ case class SubqueryReference(
+ index: Int,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]) extends LeafNode {
+ override def stringArgs: Iterator[Any] = Iterator(index)
+
+ override def output: Seq[Attribute] = mergedSubqueries(index).output
+ }
+
+ private def mergeAndGetReference(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): (SubqueryReference, Int) = {
+ mergedSubqueries.zipWithIndex.collectFirst {
+ Function.unlift { case (s, i) => mergePlans(plan, s).map(_ -> i) }
+ }.map { case ((mergedPlan, outputMap), i) =>
+ mergedSubqueries(i) = mergedPlan
+ SubqueryReference(i, mergedSubqueries) ->
+ mergedPlan.output.indexOf(outputMap(plan.output.head))
+ }.getOrElse {
+ mergedSubqueries += plan
+ SubqueryReference(mergedSubqueries.length - 1, mergedSubqueries) -> 0
+ }
+ }
+
+ private def mergePlans(
+ newPlan: LogicalPlan,
+ existingPlan: LogicalPlan): Option[(LogicalPlan,
AttributeMap[Attribute])] = {
+ (newPlan, existingPlan) match {
+ case (np, ep) if np.canonicalized == ep.canonicalized =>
+ Some(ep -> AttributeMap(np.output.zip(ep.output)))
+ case (np: Project, ep: Project) =>
+ mergePlans(np.child, ep.child).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.projectList ++ newProjectList),
mergedChild) ->
+ newOutputMap
+ }
+ case (np, ep: Project) =>
+ mergePlans(np, ep.child).map { case (mergedChild, outputMap) =>
+ Project(distinctExpressions(ep.projectList ++ outputMap.values),
mergedChild) -> outputMap
+ }
+ case (np: Project, ep) =>
+ mergePlans(np.child, ep).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.output ++ newProjectList),
mergedChild) -> newOutputMap
+ }
+ case (np: Aggregate, ep: Aggregate) =>
+ mergePlans(np.child, ep.child).flatMap { case (mergedChild, outputMap)
=>
+ val newGroupingExpression =
replaceAttributes(np.groupingExpressions, outputMap)
+ if (ExpressionSet(newGroupingExpression) ==
ExpressionSet(ep.groupingExpressions)) {
+ val newAggregateExpressions =
replaceAttributes(np.aggregateExpressions, outputMap)
+ val newOutputMap = createOutputMap(np.aggregateExpressions,
newAggregateExpressions)
Review comment:
In the tests added in this PR, it seems this map always has the same
exprId mapping, e.g., `sum(a)#3` -> `sum(a)#3`. So, could you add more tests?
##########
File path: sql/core/src/main/scala/org/apache/spark/sql/execution/subquery.scala
##########
@@ -107,6 +107,53 @@ case class ScalarSubquery(
}
}
+/**
+ * A subquery that is capable to return multiple scalar values.
+ */
+case class MultiScalarSubqueryExec(
Review comment:
Could we define a new class, `BaseScalarSubqueryExec` and share common
methods between `MultiScalarSubqueryExec` and `ScalarSubquery`?
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.catalyst.optimizer
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LeafNode,
LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{MULTI_SCALAR_SUBQUERY,
SCALAR_SUBQUERY}
+
+/**
+ * This rule tries to merge multiple non-correlated [[ScalarSubquery]]s into a
+ * [[MultiScalarSubquery]] to compute multiple scalar values once.
+ *
+ * The process is the following:
+ * - While traversing through the plan each [[ScalarSubquery]] plan is tried
to merge into the cache
+ * of already seen subquery plans. If merge is possible then cache is
updated with the merged
+ * subquery plan, if not then the new subquery plan is added to the cache.
+ * - The original [[ScalarSubquery]] expression is replaced to a reference
pointing to its cached
+ * version in this form:
`GetStructField(MultiScalarSubquery(SubqueryReference(...)))`.
+ * - A second traversal checks if a [[SubqueryReference]] is pointing to a
subquery plan that
+ * returns multiple values and either replaces only [[SubqueryReference]] to
the cached plan or
+ * restores the whole expression to its original [[ScalarSubquery]] form.
+ * - [[ReuseSubquery]] rule makes sure that merged subqueries are computed
once.
+ *
+ * Eg. the following query:
+ *
+ * SELECT
+ * (SELECT avg(a) FROM t GROUP BY b),
+ * (SELECT sum(b) FROM t GROUP BY b)
+ *
+ * is optimized from:
+ *
+ * Project [scalar-subquery#231 [] AS scalarsubquery()#241,
+ * scalar-subquery#232 [] AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#240], [sum(b#240) AS sum(b)#238L]
+ * : +- Project [b#240]
+ * : +- Relation default.t[a#239,b#240] parquet
+ * +- OneRowRelation
+ *
+ * to:
+ *
+ * Project [multi-scalar-subquery#231.avg(a) AS scalarsubquery()#241,
+ * multi-scalar-subquery#232.sum(b) AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : : +- Project [a#233, b#234]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : +- Project [a#233, b#234]
+ * : +- Relation default.t[a#233,b#234] parquet
+ * +- OneRowRelation
+ */
+object MergeScalarSubqueries extends Rule[LogicalPlan] with PredicateHelper {
+ def apply(plan: LogicalPlan): LogicalPlan = {
+ if (conf.scalarSubqueryMergeEabled && conf.subqueryReuseEnabled) {
+ val mergedSubqueries = ArrayBuffer.empty[LogicalPlan]
+ removeReferences(mergeAndInsertReferences(plan, mergedSubqueries),
mergedSubqueries)
+ } else {
+ plan
+ }
+ }
+
+ private def mergeAndInsertReferences(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): LogicalPlan = {
+
plan.transformAllExpressionsWithPruning(_.containsAnyPattern(SCALAR_SUBQUERY),
ruleId) {
+ case s: ScalarSubquery if s.children.isEmpty =>
+ val (mergedPlan, ordinal) = mergeAndGetReference(s.plan,
mergedSubqueries)
+ GetStructField(MultiScalarSubquery(mergedPlan, s.exprId), ordinal)
+ }
+ }
+
+ case class SubqueryReference(
+ index: Int,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]) extends LeafNode {
+ override def stringArgs: Iterator[Any] = Iterator(index)
+
+ override def output: Seq[Attribute] = mergedSubqueries(index).output
+ }
+
+ private def mergeAndGetReference(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): (SubqueryReference, Int) = {
+ mergedSubqueries.zipWithIndex.collectFirst {
+ Function.unlift { case (s, i) => mergePlans(plan, s).map(_ -> i) }
+ }.map { case ((mergedPlan, outputMap), i) =>
+ mergedSubqueries(i) = mergedPlan
+ SubqueryReference(i, mergedSubqueries) ->
+ mergedPlan.output.indexOf(outputMap(plan.output.head))
+ }.getOrElse {
+ mergedSubqueries += plan
+ SubqueryReference(mergedSubqueries.length - 1, mergedSubqueries) -> 0
+ }
+ }
+
+ private def mergePlans(
+ newPlan: LogicalPlan,
+ existingPlan: LogicalPlan): Option[(LogicalPlan,
AttributeMap[Attribute])] = {
+ (newPlan, existingPlan) match {
+ case (np, ep) if np.canonicalized == ep.canonicalized =>
+ Some(ep -> AttributeMap(np.output.zip(ep.output)))
+ case (np: Project, ep: Project) =>
+ mergePlans(np.child, ep.child).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.projectList ++ newProjectList),
mergedChild) ->
+ newOutputMap
+ }
+ case (np, ep: Project) =>
+ mergePlans(np, ep.child).map { case (mergedChild, outputMap) =>
+ Project(distinctExpressions(ep.projectList ++ outputMap.values),
mergedChild) -> outputMap
+ }
+ case (np: Project, ep) =>
+ mergePlans(np.child, ep).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.output ++ newProjectList),
mergedChild) -> newOutputMap
+ }
Review comment:
btw, could we use a white-list here to check if it can merge plans or
not?
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.catalyst.optimizer
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LeafNode,
LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{MULTI_SCALAR_SUBQUERY,
SCALAR_SUBQUERY}
+
+/**
+ * This rule tries to merge multiple non-correlated [[ScalarSubquery]]s into a
+ * [[MultiScalarSubquery]] to compute multiple scalar values once.
+ *
+ * The process is the following:
+ * - While traversing through the plan each [[ScalarSubquery]] plan is tried
to merge into the cache
+ * of already seen subquery plans. If merge is possible then cache is
updated with the merged
+ * subquery plan, if not then the new subquery plan is added to the cache.
+ * - The original [[ScalarSubquery]] expression is replaced to a reference
pointing to its cached
+ * version in this form:
`GetStructField(MultiScalarSubquery(SubqueryReference(...)))`.
+ * - A second traversal checks if a [[SubqueryReference]] is pointing to a
subquery plan that
+ * returns multiple values and either replaces only [[SubqueryReference]] to
the cached plan or
+ * restores the whole expression to its original [[ScalarSubquery]] form.
+ * - [[ReuseSubquery]] rule makes sure that merged subqueries are computed
once.
+ *
+ * Eg. the following query:
+ *
+ * SELECT
+ * (SELECT avg(a) FROM t GROUP BY b),
+ * (SELECT sum(b) FROM t GROUP BY b)
+ *
+ * is optimized from:
+ *
+ * Project [scalar-subquery#231 [] AS scalarsubquery()#241,
+ * scalar-subquery#232 [] AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#240], [sum(b#240) AS sum(b)#238L]
+ * : +- Project [b#240]
+ * : +- Relation default.t[a#239,b#240] parquet
+ * +- OneRowRelation
+ *
+ * to:
+ *
+ * Project [multi-scalar-subquery#231.avg(a) AS scalarsubquery()#241,
+ * multi-scalar-subquery#232.sum(b) AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : : +- Project [a#233, b#234]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : +- Project [a#233, b#234]
+ * : +- Relation default.t[a#233,b#234] parquet
+ * +- OneRowRelation
+ */
+object MergeScalarSubqueries extends Rule[LogicalPlan] with PredicateHelper {
+ def apply(plan: LogicalPlan): LogicalPlan = {
+ if (conf.scalarSubqueryMergeEabled && conf.subqueryReuseEnabled) {
+ val mergedSubqueries = ArrayBuffer.empty[LogicalPlan]
+ removeReferences(mergeAndInsertReferences(plan, mergedSubqueries),
mergedSubqueries)
+ } else {
+ plan
+ }
+ }
+
+ private def mergeAndInsertReferences(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): LogicalPlan = {
+
plan.transformAllExpressionsWithPruning(_.containsAnyPattern(SCALAR_SUBQUERY),
ruleId) {
+ case s: ScalarSubquery if s.children.isEmpty =>
+ val (mergedPlan, ordinal) = mergeAndGetReference(s.plan,
mergedSubqueries)
+ GetStructField(MultiScalarSubquery(mergedPlan, s.exprId), ordinal)
+ }
+ }
+
+ case class SubqueryReference(
+ index: Int,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]) extends LeafNode {
+ override def stringArgs: Iterator[Any] = Iterator(index)
+
+ override def output: Seq[Attribute] = mergedSubqueries(index).output
+ }
+
+ private def mergeAndGetReference(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): (SubqueryReference, Int) = {
+ mergedSubqueries.zipWithIndex.collectFirst {
+ Function.unlift { case (s, i) => mergePlans(plan, s).map(_ -> i) }
+ }.map { case ((mergedPlan, outputMap), i) =>
+ mergedSubqueries(i) = mergedPlan
+ SubqueryReference(i, mergedSubqueries) ->
+ mergedPlan.output.indexOf(outputMap(plan.output.head))
+ }.getOrElse {
+ mergedSubqueries += plan
+ SubqueryReference(mergedSubqueries.length - 1, mergedSubqueries) -> 0
+ }
+ }
+
+ private def mergePlans(
+ newPlan: LogicalPlan,
+ existingPlan: LogicalPlan): Option[(LogicalPlan,
AttributeMap[Attribute])] = {
+ (newPlan, existingPlan) match {
+ case (np, ep) if np.canonicalized == ep.canonicalized =>
+ Some(ep -> AttributeMap(np.output.zip(ep.output)))
+ case (np: Project, ep: Project) =>
+ mergePlans(np.child, ep.child).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.projectList ++ newProjectList),
mergedChild) ->
+ newOutputMap
+ }
+ case (np, ep: Project) =>
+ mergePlans(np, ep.child).map { case (mergedChild, outputMap) =>
+ Project(distinctExpressions(ep.projectList ++ outputMap.values),
mergedChild) -> outputMap
+ }
+ case (np: Project, ep) =>
+ mergePlans(np.child, ep).map { case (mergedChild, outputMap) =>
+ val newProjectList = replaceAttributes(np.projectList, outputMap)
+ val newOutputMap = createOutputMap(np.projectList, newProjectList)
+ Project(distinctExpressions(ep.output ++ newProjectList),
mergedChild) -> newOutputMap
+ }
+ case (np: Aggregate, ep: Aggregate) =>
Review comment:
IMHO, since this rewrite itself is not an optimization, but a
pre-process to reuse sub-queries, so it might be better to implement this logic
inside the `ReuseSubquery` side, and merge them if physical plans are the same.
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.catalyst.optimizer
+
+import scala.collection.mutable.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LeafNode,
LogicalPlan, Project}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{MULTI_SCALAR_SUBQUERY,
SCALAR_SUBQUERY}
+
+/**
+ * This rule tries to merge multiple non-correlated [[ScalarSubquery]]s into a
+ * [[MultiScalarSubquery]] to compute multiple scalar values once.
+ *
+ * The process is the following:
+ * - While traversing through the plan each [[ScalarSubquery]] plan is tried
to merge into the cache
+ * of already seen subquery plans. If merge is possible then cache is
updated with the merged
+ * subquery plan, if not then the new subquery plan is added to the cache.
+ * - The original [[ScalarSubquery]] expression is replaced to a reference
pointing to its cached
+ * version in this form:
`GetStructField(MultiScalarSubquery(SubqueryReference(...)))`.
+ * - A second traversal checks if a [[SubqueryReference]] is pointing to a
subquery plan that
+ * returns multiple values and either replaces only [[SubqueryReference]] to
the cached plan or
+ * restores the whole expression to its original [[ScalarSubquery]] form.
+ * - [[ReuseSubquery]] rule makes sure that merged subqueries are computed
once.
+ *
+ * Eg. the following query:
+ *
+ * SELECT
+ * (SELECT avg(a) FROM t GROUP BY b),
+ * (SELECT sum(b) FROM t GROUP BY b)
+ *
+ * is optimized from:
+ *
+ * Project [scalar-subquery#231 [] AS scalarsubquery()#241,
+ * scalar-subquery#232 [] AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#240], [sum(b#240) AS sum(b)#238L]
+ * : +- Project [b#240]
+ * : +- Relation default.t[a#239,b#240] parquet
+ * +- OneRowRelation
+ *
+ * to:
+ *
+ * Project [multi-scalar-subquery#231.avg(a) AS scalarsubquery()#241,
+ * multi-scalar-subquery#232.sum(b) AS scalarsubquery()#242L]
+ * : :- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : : +- Project [a#233, b#234]
+ * : : +- Relation default.t[a#233,b#234] parquet
+ * : +- Aggregate [b#234], [avg(a#233) AS avg(a)#236, sum(b#234) AS
sum(b)#238L]
+ * : +- Project [a#233, b#234]
+ * : +- Relation default.t[a#233,b#234] parquet
+ * +- OneRowRelation
+ */
+object MergeScalarSubqueries extends Rule[LogicalPlan] with PredicateHelper {
+ def apply(plan: LogicalPlan): LogicalPlan = {
+ if (conf.scalarSubqueryMergeEabled && conf.subqueryReuseEnabled) {
+ val mergedSubqueries = ArrayBuffer.empty[LogicalPlan]
+ removeReferences(mergeAndInsertReferences(plan, mergedSubqueries),
mergedSubqueries)
+ } else {
+ plan
+ }
+ }
+
+ private def mergeAndInsertReferences(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): LogicalPlan = {
+
plan.transformAllExpressionsWithPruning(_.containsAnyPattern(SCALAR_SUBQUERY),
ruleId) {
+ case s: ScalarSubquery if s.children.isEmpty =>
+ val (mergedPlan, ordinal) = mergeAndGetReference(s.plan,
mergedSubqueries)
+ GetStructField(MultiScalarSubquery(mergedPlan, s.exprId), ordinal)
+ }
+ }
+
+ case class SubqueryReference(
+ index: Int,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]) extends LeafNode {
+ override def stringArgs: Iterator[Any] = Iterator(index)
+
+ override def output: Seq[Attribute] = mergedSubqueries(index).output
+ }
+
+ private def mergeAndGetReference(
+ plan: LogicalPlan,
+ mergedSubqueries: ArrayBuffer[LogicalPlan]): (SubqueryReference, Int) = {
+ mergedSubqueries.zipWithIndex.collectFirst {
+ Function.unlift { case (s, i) => mergePlans(plan, s).map(_ -> i) }
+ }.map { case ((mergedPlan, outputMap), i) =>
+ mergedSubqueries(i) = mergedPlan
+ SubqueryReference(i, mergedSubqueries) ->
+ mergedPlan.output.indexOf(outputMap(plan.output.head))
+ }.getOrElse {
+ mergedSubqueries += plan
+ SubqueryReference(mergedSubqueries.length - 1, mergedSubqueries) -> 0
+ }
+ }
+
+ private def mergePlans(
Review comment:
`mergePlans` -> `tryMergePlans`?
--
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]