peter-toth commented on code in PR #37525: URL: https://github.com/apache/spark/pull/37525#discussion_r1090434607
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/AliasAwareOutputExpression.scala: ########## @@ -0,0 +1,116 @@ +/* + * 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.plans + +import scala.collection.mutable + +import org.apache.spark.sql.catalyst.SQLConfHelper +import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, AttributeSet, Empty2Null, Expression, NamedExpression, SortOrder} +import org.apache.spark.sql.internal.SQLConf + +/** + * A trait that provides functionality to handle aliases in the `outputExpressions`. + */ +trait AliasAwareOutputExpression extends SQLConfHelper { + protected val aliasCandidateLimit = conf.getConf(SQLConf.EXPRESSION_PROJECTION_CANDIDATE_LIMIT) + protected def outputExpressions: Seq[NamedExpression] + /** + * This method can be used to strip expression which does not affect the result, for example: + * strip the expression which is ordering agnostic for output ordering. + */ + protected def strip(expr: Expression): Expression = expr + + // Build an `Expression` -> `Attribute` alias map. + // There can be multiple alias defined for the same expressions but it doesn't make sense to store + // more than `aliasCandidateLimit` attributes for an expression. In those cases the old logic + // handled only the last alias so we need to make sure that we give precedence to that. + // If the `outputExpressions` contain simple attributes we need to add those too to the map. + private lazy val aliasMap = { + val aliases = mutable.Map[Expression, mutable.ArrayBuffer[Attribute]]() + outputExpressions.reverse.foreach { + case a @ Alias(child, _) => + val buffer = aliases.getOrElseUpdate(strip(child.canonicalized), mutable.ArrayBuffer.empty) + if (buffer.size < aliasCandidateLimit) { + buffer += a.toAttribute + } + case _ => + } + outputExpressions.foreach { + case a: Attribute if aliases.contains(a.canonicalized) => + val buffer = aliases(a.canonicalized) + if (buffer.size < aliasCandidateLimit) { + buffer += a + } + case _ => + } + aliases + } + + protected def hasAlias: Boolean = aliasMap.nonEmpty + + /** + * Return a set of Expression which normalize the original expression to the aliased. Review Comment: Done -- 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-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org