Skip to content

Commit

Permalink
Flatten coalesce expressions
Browse files Browse the repository at this point in the history
Flatten coalesce(colA, coalesce(colB, colC)) to coalesce(colA, colB, colC).
  • Loading branch information
Praveen2112 committed Jan 25, 2019
1 parent 8b1e53f commit c51fa6b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ protected Object visitCoalesceExpression(CoalesceExpression node, Object context
List<Object> values = node.getOperands().stream()
.map(value -> processWithExceptionHandling(value, context))
.filter(Objects::nonNull)
.flatMap(expression -> {
if (expression instanceof CoalesceExpression) {
return ((CoalesceExpression) expression).getOperands().stream();
}
return Stream.of(expression);
})
.collect(Collectors.toList());

if ((!values.isEmpty() && !(values.get(0) instanceof Expression)) || values.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,11 @@ public void testCoalesce()
assertOptimizedEquals("coalesce(6, unbound_long2, unbound_long, unbound_long3)", "6");
assertOptimizedEquals("coalesce(2 * 3, unbound_long2, unbound_long, unbound_long3)", "6");
assertOptimizedMatches("coalesce(random(), random(), 5)", "coalesce(random(), random(), 5E0)");
assertOptimizedMatches("coalesce(unbound_long, coalesce(unbound_long, 1))", "coalesce(unbound_long, BIGINT '1')");
assertOptimizedMatches("coalesce(coalesce(unbound_long, coalesce(unbound_long, 1)), unbound_long2)", "coalesce(unbound_long, BIGINT '1')");
assertOptimizedMatches("coalesce(unbound_long, 2, coalesce(unbound_long, 1))", "coalesce(unbound_long, BIGINT '2')");
assertOptimizedMatches("coalesce(coalesce(unbound_long, coalesce(unbound_long2, unbound_long3)), 1)", "coalesce(unbound_long, unbound_long2, unbound_long3, BIGINT '1')");
assertOptimizedMatches("coalesce(unbound_double, coalesce(random(), unbound_double))", "coalesce(unbound_double, random())");
}

@Test
Expand Down

0 comments on commit c51fa6b

Please sign in to comment.