Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify SimplifyRedundantCase rule #23846

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ public Optional<Expression> apply(Expression expression, Session session, Map<Sy
return Optional.empty();
}

return transformRecursive(0, caseTerm.whenClauses(), defaultValue)
.or(() -> Optional.<Expression>of(FALSE));
return Optional.of(transformRecursive(0, caseTerm.whenClauses(), defaultValue));
}

private Optional<Expression> transformRecursive(int start, List<WhenClause> clauses, Expression defaultExpression)
private Expression transformRecursive(int start, List<WhenClause> clauses, Expression defaultExpression)
{
// An expression such as:
// CASE
Expand Down Expand Up @@ -107,19 +106,17 @@ private Optional<Expression> transformRecursive(int start, List<WhenClause> clau
if (end < clauses.size()) {
List<Expression> terms = new ArrayList<>();
terms.add(new Comparison(Comparison.Operator.IDENTICAL, clauses.get(end).getOperand(), TRUE));
transformRecursive(end + 1, clauses, defaultExpression).ifPresent(terms::add);
terms.add(transformRecursive(end + 1, clauses, defaultExpression));

return Optional.of(IrUtils.and(
return IrUtils.and(
ImmutableList.<Expression>builder()
.addAll(falseTerms)
.add(IrUtils.or(terms))
.build()));
.build());
}
else if (defaultExpression.equals(TRUE)) {
return Optional.of(IrUtils.and(falseTerms));
}
else {
return Optional.empty();
return IrUtils.and(falseTerms);
}
return FALSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void test()
new Case(
ImmutableList.of(new WhenClause(new Reference(BOOLEAN, "x"), TRUE)),
FALSE)))
.isEqualTo(Optional.of(new Comparison(IDENTICAL, new Reference(BOOLEAN, "x"), TRUE)));
.isEqualTo(Optional.of(IrUtils.or(new Comparison(IDENTICAL, new Reference(BOOLEAN, "x"), TRUE), FALSE)));

assertThat(optimize(
new Case(
Expand All @@ -82,7 +82,7 @@ void test()
new Case(
ImmutableList.of(new WhenClause(new Comparison(EQUAL, new Reference(BIGINT, "x"), new Constant(BIGINT, 1L)), TRUE)),
FALSE)))
.isEqualTo(Optional.of(new Comparison(IDENTICAL, new Comparison(EQUAL, new Reference(BIGINT, "x"), new Constant(BIGINT, 1L)), TRUE)));
.isEqualTo(Optional.of(IrUtils.or(new Comparison(IDENTICAL, new Comparison(EQUAL, new Reference(BIGINT, "x"), new Constant(BIGINT, 1L)), TRUE), FALSE)));

assertThat(optimize(
new Case(
Expand All @@ -102,7 +102,7 @@ void test()
new Comparison(IDENTICAL, new Reference(BOOLEAN, "x"), TRUE),
IrUtils.and(
IrExpressions.not(PLANNER_CONTEXT.getMetadata(), new Comparison(IDENTICAL, new Reference(BOOLEAN, "y"), TRUE)),
new Comparison(IDENTICAL, new Reference(BOOLEAN, "z"), TRUE)))));
IrUtils.or(new Comparison(IDENTICAL, new Reference(BOOLEAN, "z"), TRUE), FALSE)))));

assertThat(optimize(
new Case(
Expand Down Expand Up @@ -158,7 +158,7 @@ void test()
new WhenClause(new Reference(BOOLEAN, "a2"), FALSE),
new WhenClause(new Reference(BOOLEAN, "a3"), FALSE)),
FALSE)))
.isEqualTo(Optional.of(new Comparison(IDENTICAL, new Reference(BOOLEAN, "a1"), TRUE)));
.isEqualTo(Optional.of(IrUtils.or(new Comparison(IDENTICAL, new Reference(BOOLEAN, "a1"), TRUE), FALSE)));
}

@Test
Expand Down
Loading