From 583a9c75bbb35387169d4f0cf763ef566d899954 Mon Sep 17 00:00:00 2001 From: Josh Rosen Date: Fri, 3 Jun 2022 18:31:07 -0700 Subject: [PATCH] Fix source and binary incompatibilities --- .../catalyst/optimizer/finishAnalysis.scala | 2 +- .../spark/sql/catalyst/plans/QueryPlan.scala | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala index 242c799dd226..a33069051d9d 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/finishAnalysis.scala @@ -84,7 +84,7 @@ object ComputeCurrentTime extends Rule[LogicalPlan] { treePatternbits.containsPattern(CURRENT_LIKE) } - plan.transformDownWithSubqueries(transformCondition) { + plan.transformDownWithSubqueriesAndPruning(transformCondition) { case subQuery => subQuery.transformAllExpressionsWithPruning(transformCondition) { case cd: CurrentDate => diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala index d0283f4d3672..cc62c81b101b 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/QueryPlan.scala @@ -454,7 +454,7 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] * to rewrite the whole plan, include its subqueries, in one go. */ def transformWithSubqueries(f: PartialFunction[PlanType, PlanType]): PlanType = - transformDownWithSubqueries(AlwaysProcess.fn, UnknownRuleId)(f) + transformDownWithSubqueries(f) /** * Returns a copy of this node where the given partial function has been recursively applied @@ -479,10 +479,20 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] * first to this node, then this node's subqueries and finally this node's children. * When the partial function does not apply to a given node, it is left unchanged. */ - def transformDownWithSubqueries( - cond: TreePatternBits => Boolean = AlwaysProcess.fn, ruleId: RuleId = UnknownRuleId) - (f: PartialFunction[PlanType, PlanType]) -: PlanType = { + def transformDownWithSubqueries(f: PartialFunction[PlanType, PlanType]): PlanType = { + transformDownWithSubqueriesAndPruning(AlwaysProcess.fn, UnknownRuleId)(f) + } + + /** + * This method is the top-down (pre-order) counterpart of transformUpWithSubqueries. + * Returns a copy of this node where the given partial function has been recursively applied + * first to this node, then this node's subqueries and finally this node's children. + * When the partial function does not apply to a given node, it is left unchanged. + */ + def transformDownWithSubqueriesAndPruning( + cond: TreePatternBits => Boolean, + ruleId: RuleId = UnknownRuleId) + (f: PartialFunction[PlanType, PlanType]): PlanType = { val g: PartialFunction[PlanType, PlanType] = new PartialFunction[PlanType, PlanType] { override def isDefinedAt(x: PlanType): Boolean = true @@ -490,7 +500,7 @@ abstract class QueryPlan[PlanType <: QueryPlan[PlanType]] val transformed = f.applyOrElse[PlanType, PlanType](plan, identity) transformed transformExpressionsDown { case planExpression: PlanExpression[PlanType] => - val newPlan = planExpression.plan.transformDownWithSubqueries(cond, ruleId)(f) + val newPlan = planExpression.plan.transformDownWithSubqueriesAndPruning(cond, ruleId)(f) planExpression.withNewPlan(newPlan) } }