cloud-fan commented on a change in pull request #33404: URL: https://github.com/apache/spark/pull/33404#discussion_r736291811
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DistinctAttributesVisitor.scala ########## @@ -0,0 +1,100 @@ +/* + * 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.logical + +import org.apache.spark.sql.catalyst.expressions.{Alias, ExpressionSet} +import org.apache.spark.sql.catalyst.plans.{LeftAnti, LeftSemi} + +/** + * A visitor pattern for traversing a [[LogicalPlan]] tree and propagate the distinct attributes. + */ +object DistinctAttributesVisitor extends LogicalPlanVisitor[Set[ExpressionSet]] { + override def default(p: LogicalPlan): Set[ExpressionSet] = { + Set.empty[ExpressionSet] + } + + override def visitAggregate(p: Aggregate): Set[ExpressionSet] = { Review comment: I'd prefer this ``` /** * Add a new ExpressionSet S into distinctKeys D. * To minimize the size of D: * 1. If there is a subset of S in D, return D. * 2. Otherwise, remove all the ExpressionSet containing S from D, and add the new one. */ private def addDistinctKey( keys: DistinctKeys, newExpressionSet: ExpressionSet): DistinctKeys = { if (keys.exists(_.subsetOf(newExpressionSet))) { keys } else { keys.filterNot(s => newExpressionSet.subsetOf(s)) + newExpressionSet } } /** * Propagate distinct keys with projectList. * For each alias in project list, replace the corresponding expression in distinctKeys. * To minimize the size of distinctKeys, remove all ExpressionSet that not subset of projectList. */ private def projectDistinctKeys( keys: DistinctKeys, projectList: Seq[NamedExpression]): DistinctKeys = { val outputSet = ExpressionSet(projectList.map(_.toAttribute)) val aliases = projectList.filter(_.isInstanceOf[Alias]) if (aliases.isEmpty) return keys.filter(_.subsetOf(outputSet)) val aliasedDistinctKeys = keys.map { expressionSet => expressionSet.map { expression => expression transform { case expr: Expression => aliases .collectFirst { case a: Alias if a.child.semanticEquals(expr) => a.toAttribute } .getOrElse(expr) } } } aliasedDistinctKeys.collect { case es: ExpressionSet if es.subsetOf(outputSet) => ExpressionSet(es) } } override def visitAggregate(p: Aggregate): Set[ExpressionSet] = { val distinctKeysWithGrouping = addDistinctKey(p.child.distinctKeys, ExpressionSet(p.groupingExpressions)) projectDistinctKeys(distinctKeysWithGrouping, p.aggregateExpressions) } ``` -- 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]
