@@ -508,7 +508,7 @@ object NullPropagation extends Rule[LogicalPlan] {
508508/**
509509 * Replace attributes with aliases of the original foldable expressions if possible.
510510 * Other optimizations will take advantage of the propagated foldable expressions. For example,
511- * This rule can optimize
511+ * this rule can optimize
512512 * {{{
513513 * SELECT 1.0 x, 'abc' y, Now() z ORDER BY x, y, 3
514514 * }}}
@@ -535,7 +535,7 @@ object FoldablePropagation extends Rule[LogicalPlan] {
535535 } else {
536536 CleanupAliases (plan.transformUp {
537537 // We can only propagate foldables for a subset of unary nodes.
538- case u : UnaryNode if canPropagateFoldables(u) =>
538+ case u : UnaryNode if foldableMap.nonEmpty && canPropagateFoldables(u) =>
539539 u.transformExpressions(replaceFoldable)
540540
541541 // Join derives the output attributes from its child while they are actually not the
@@ -544,7 +544,7 @@ object FoldablePropagation extends Rule[LogicalPlan] {
544544 // propagating the foldable expressions.
545545 // TODO(cloud-fan): It seems more reasonable to use new attributes as the output attributes
546546 // of outer join.
547- case j @ Join (left, right, joinType, _) =>
547+ case j @ Join (left, right, joinType, _) if foldableMap.nonEmpty =>
548548 val newJoin = j.transformExpressions(replaceFoldable)
549549 val missDerivedAttrsSet : AttributeSet = AttributeSet (joinType match {
550550 case _ : InnerLike | LeftExistence (_) => Nil
@@ -557,22 +557,16 @@ object FoldablePropagation extends Rule[LogicalPlan] {
557557 }.toSeq)
558558 newJoin
559559
560- // Similar to Join, Expand also miss-derives output attributes from child attributes, we
561- // should exclude them when propagating.
562- // TODO(hvanhovell): Expand should use new attributes as the output attributes.
563- case expand : Expand =>
564- val newExpand = expand.copy(projections = expand.projections.map { projection =>
560+ // We can not replace the attributes in `Expand.output`. If there are other non-leaf
561+ // operators that have the `output` field, we should put them here too.
562+ case expand : Expand if foldableMap.nonEmpty =>
563+ expand.copy(projections = expand.projections.map { projection =>
565564 projection.map(_.transform(replaceFoldable))
566565 })
567- val missDerivedAttrsSet = expand.child.outputSet
568- foldableMap = AttributeMap (foldableMap.baseMap.values.filterNot {
569- case (attr, _) => missDerivedAttrsSet.contains(attr)
570- }.toSeq)
571- newExpand
572566
573567 // For other plans, they are not safe to apply foldable propagation, and they should not
574568 // propagate foldable expressions from children.
575- case other =>
569+ case other if foldableMap.nonEmpty =>
576570 val childrenOutputSet = AttributeSet (other.children.flatMap(_.output))
577571 foldableMap = AttributeMap (foldableMap.baseMap.values.filterNot {
578572 case (attr, _) => childrenOutputSet.contains(attr)
0 commit comments