cloud-fan commented on a change in pull request #35779: URL: https://github.com/apache/spark/pull/35779#discussion_r822388005
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/DistinctKeyVisitor.scala ########## @@ -0,0 +1,140 @@ +/* + * 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, Expression, ExpressionSet, NamedExpression} +import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys +import org.apache.spark.sql.catalyst.plans.{Inner, LeftOuter, LeftSemiOrAnti, RightOuter} + +/** + * A visitor pattern for traversing a [[LogicalPlan]] tree and propagate the distinct attributes. + */ +object DistinctKeyVisitor extends LogicalPlanVisitor[Set[ExpressionSet]] { + + private def projectDistinctKeys( + keys: Set[ExpressionSet], projectList: Seq[NamedExpression]): Set[ExpressionSet] = { + val outputSet = ExpressionSet(projectList.map(_.toAttribute)) + val distinctKeys = keys.filter(_.subsetOf(outputSet)) + val aliases = projectList.filter(_.isInstanceOf[Alias]) + if (aliases.isEmpty) { + distinctKeys + } else { + 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) + } ++ distinctKeys + }.filter(_.nonEmpty) + } + + override def default(p: LogicalPlan): Set[ExpressionSet] = Set.empty[ExpressionSet] + + override def visitAggregate(p: Aggregate): Set[ExpressionSet] = { + val groupingExps = ExpressionSet(p.groupingExpressions) // handle group by a, a + projectDistinctKeys(Set(groupingExps), p.aggregateExpressions) + } + + override def visitDistinct(p: Distinct): Set[ExpressionSet] = Set(ExpressionSet(p.output)) + + override def visitExcept(p: Except): Set[ExpressionSet] = + if (!p.isAll && p.deterministic) Set(ExpressionSet(p.output)) else default(p) + + override def visitExpand(p: Expand): Set[ExpressionSet] = default(p) + + override def visitFilter(p: Filter): Set[ExpressionSet] = p.child.distinctKeys + + override def visitGenerate(p: Generate): Set[ExpressionSet] = default(p) + + override def visitGlobalLimit(p: GlobalLimit): Set[ExpressionSet] = { + p.maxRows match { + case Some(value) if value <= 1 => Set(ExpressionSet(p.output)) + case _ => p.child.distinctKeys + } + } + + override def visitIntersect(p: Intersect): Set[ExpressionSet] = { + if (!p.isAll && p.deterministic) Set(ExpressionSet(p.output)) else default(p) + } + + override def visitJoin(p: Join): Set[ExpressionSet] = { + p match { + case Join(_, _, LeftSemiOrAnti(_), _, _) => + p.left.distinctKeys + case ExtractEquiJoinKeys(joinType, leftKeys, rightKeys, _, _, left, right, _) + if left.distinctKeys.nonEmpty || right.distinctKeys.nonEmpty => + val rightJoinKeySet = ExpressionSet(rightKeys) + val leftJoinKeySet = ExpressionSet(leftKeys) + joinType match { + case Inner if left.distinctKeys.exists(_.subsetOf(leftJoinKeySet)) && + right.distinctKeys.exists(_.subsetOf(rightJoinKeySet)) => + left.distinctKeys ++ right.distinctKeys + case Inner | LeftOuter if right.distinctKeys.exists(_.subsetOf(rightJoinKeySet)) => + p.left.distinctKeys + case Inner | RightOuter if left.distinctKeys.exists(_.subsetOf(leftJoinKeySet)) => + p.right.distinctKeys + case _ => + default(p) + } + case _ => default(p) + } + } + + override def visitLocalLimit(p: LocalLimit): Set[ExpressionSet] = p.child.distinctKeys + + override def visitPivot(p: Pivot): Set[ExpressionSet] = default(p) + + override def visitProject(p: Project): Set[ExpressionSet] = { + if (p.child.distinctKeys.nonEmpty) { + projectDistinctKeys(p.child.distinctKeys.map(ExpressionSet(_)), p.projectList) Review comment: isn't `p.child.distinctKeys` already a `Set[ExpressionSet]`? -- 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]
