Commit f3d2f477 authored by Donald H. (Donnie) Pinkston, III's avatar Donald H. (Donnie) Pinkston, III
Browse files

Simplify/flatten composite Boolean expressions

The new ANTLRv4 parser generates left-deep nested Boolean AND/OR
expressions, which need to be flattened where appropriate.  The
BooleanOperator.simplify() method takes care of this operation.

Introducing this flattening/simplification everywhere all at once seemed
to be inadvisable, so currently it is only applied in the query AST when
schemas are computed.  Additionally, only WHERE predicates, HAVING
predicates, and ON clauses are simplified/flattened.  (NATURAL JOIN and
USING join predicates are generated programmatically, not by the parser,
and are already flattened.)
parent c142f9d5
Showing with 14 additions and 2 deletions
+14 -2
......@@ -72,7 +72,7 @@ public class BooleanOperator extends Expression {
throw new NullPointerException("type cannot be null");
this.type = type;
this.terms = new ArrayList<Expression>();
this.terms = new ArrayList<>();
if (terms != null) {
for (Expression term : terms)
......@@ -492,9 +492,12 @@ public class BooleanOperator extends Expression {
// that are also BooleanOperators of the same type, since
// the new nodes will now be at index i.
}
else {
// Some other kind of term - skip it.
i++;
}
}
return this;
}
......
......@@ -665,6 +665,7 @@ public class FromClause {
}
joinOnExpr = expr;
joinOnExpr = joinOnExpr.simplify();
}
......
......@@ -452,6 +452,10 @@ public class SelectClause {
resolveExpressionRefs("WHERE clause", whereExpr, fromSchema,
/* checkParentQueries */ true);
// Simplify the expression
whereExpr = whereExpr.simplify();
// Compute the schemas of subqueries embedded in the WHERE clause.
whereExpr.traverse(subquerySchemaComputer);
}
......@@ -470,6 +474,10 @@ public class SelectClause {
resolveExpressionRefs("HAVING clause", havingExpr, fromSchema,
/* checkParentQueries */ true);
// Simplify the expression
havingExpr = havingExpr.simplify();
// Compute the schemas of subqueries embedded in the HAVING clause
havingExpr.traverse(subquerySchemaComputer);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment