Skip to content

Commit d966086

Browse files
scwfmarmbrus
authored andcommitted
[SQL][Minor] Fix foreachUp of treenode
`foreachUp` should runs the given function recursively on [[children]] then on this node(just like transformUp). The current implementation does not follow this. This will leads to checkanalysis do not check from bottom of logical tree. Author: scwf <[email protected]> Author: Fei Wang <[email protected]> Closes apache#5518 from scwf/patch-1 and squashes the following commits: 18e28b2 [scwf] added a test case 1ccbfa8 [Fei Wang] fix foreachUp
1 parent 6183b5e commit d966086

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/trees/TreeNode.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ abstract class TreeNode[BaseType <: TreeNode[BaseType]] {
8585
* @param f the function to be applied to each node in the tree.
8686
*/
8787
def foreachUp(f: BaseType => Unit): Unit = {
88-
children.foreach(_.foreach(f))
88+
children.foreach(_.foreachUp(f))
8989
f(this)
9090
}
9191

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/trees/TreeNodeSuite.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,17 @@ class TreeNodeSuite extends FunSuite {
117117
assert(transformed.origin.startPosition.isDefined)
118118
}
119119

120+
test("foreach up") {
121+
val actual = new ArrayBuffer[String]()
122+
val expected = Seq("1", "2", "3", "4", "-", "*", "+")
123+
val expression = Add(Literal(1), Multiply(Literal(2), Subtract(Literal(3), Literal(4))))
124+
expression foreachUp {
125+
case b: BinaryExpression => actual.append(b.symbol);
126+
case l: Literal => actual.append(l.toString);
127+
}
128+
129+
assert(expected === actual)
130+
}
131+
120132

121133
}

0 commit comments

Comments
 (0)