EnricoMi commented on code in PR #37525:
URL: https://github.com/apache/spark/pull/37525#discussion_r1090613525
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -435,6 +435,16 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val EXPRESSION_PROJECTION_CANDIDATE_LIMIT =
+ buildConf("spark.sql.optimizer.expressionProjectionCandidateLimit")
+ .doc("The maximum number of the candidate of output expressions whose
alias are replaced." +
+ " It can preserve the output partitioning and ordering." +
+ " Negative value means disable this optimization.")
+ .internal()
+ .version("3.4.0")
Review Comment:
This PR targets master, which is `3.5.0`. Is this going to be merged into
`branch-3.4`, which is feature-freeze? If not, this line should be adjusted.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/AliasAwareOutputExpression.scala:
##########
@@ -16,52 +16,42 @@
*/
package org.apache.spark.sql.execution
-import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
NamedExpression, SortOrder}
-import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning,
Partitioning, PartitioningCollection, UnknownPartitioning}
+import scala.collection.mutable
-/**
- * A trait that provides functionality to handle aliases in the
`outputExpressions`.
- */
-trait AliasAwareOutputExpression extends UnaryExecNode {
- protected def outputExpressions: Seq[NamedExpression]
-
- private lazy val aliasMap = outputExpressions.collect {
- case a @ Alias(child, _) => child.canonicalized -> a.toAttribute
- }.toMap
-
- protected def hasAlias: Boolean = aliasMap.nonEmpty
-
- protected def normalizeExpression(exp: Expression): Expression = {
- exp.transformDown {
- case e: Expression => aliasMap.getOrElse(e.canonicalized, e)
- }
- }
-}
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.catalyst.plans.{AliasAwareOutputExpression,
AliasAwareQueryOutputOrdering}
+import org.apache.spark.sql.catalyst.plans.physical.{Partitioning,
PartitioningCollection, UnknownPartitioning}
/**
* A trait that handles aliases in the `outputExpressions` to produce
`outputPartitioning` that
* satisfies distribution requirements.
*/
-trait AliasAwareOutputPartitioning extends AliasAwareOutputExpression {
+trait PartitioningPreservingUnaryExecNode extends UnaryExecNode
+ with AliasAwareOutputExpression {
final override def outputPartitioning: Partitioning = {
- val normalizedOutputPartitioning = if (hasAlias) {
- child.outputPartitioning match {
+ if (hasAlias) {
+ flattenPartitioning(child.outputPartitioning).flatMap {
case e: Expression =>
- normalizeExpression(e).asInstanceOf[Partitioning]
- case other => other
+ // We need unique partitionings but if the input partitioning is
+ // `HashPartitioning(Seq(id + id))` and we have `id -> a` and `id ->
b` aliases then after
+ // the projection we have 4 partitionings:
+ // `HashPartitioning(Seq(a + a))`, `HashPartitioning(Seq(a + b))`,
+ // `HashPartitioning(Seq(b + a))`, `HashPartitioning(Seq(b + b))`,
but
+ // `HashPartitioning(Seq(a + b))` is the same as
`HashPartitioning(Seq(b + a))`.
+ val partitioningSet = mutable.Set.empty[Expression]
+ projectExpression(e)
+ .filter(e => partitioningSet.add(e.canonicalized))
+ .take(aliasCandidateLimit)
Review Comment:
Scala 2.13 allows to simplify this. Its a shame...
```suggestion
projectExpression(e)
.distinctBy(_.canonicalized)
.take(aliasCandidateLimit)
```
--
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]