dynamobi/dynamobi — Issue #381: [FRG-396] Optimize WHERE clause by internally wrapping condition in '... IS TRUE'
Author: dynamobi-build
Created: 2011-12-30 03:11:39+00:00
State: open
Labels: FRG, Optimization rules

[reporter="jhyde", created="Wed, 12 May 2010 04:00:50 -0500 (GMT-05:00)"]
SQL boolean expressions use 3-valued logic (TRUE, FALSE, UNKNOWN aka NULL) but a WHERE clause has only two outcomes. WHERE drops the row if the boolean expression evaluates to FALSE or UNKNOWN. Knowing this, we can evaluate the boolean expression in less detail.

A pragmatic way to achieve these optimizations is to wrap the expression in '... IS TRUE' (a predicate that only ever returns TRUE or FALSE) and then add rewrite rules to simplify.

From email thread http://farrago-developers.1374669.n2.nabble.com/Re-Eigenbase-perforce-change-13608-for-review-tc5015243.html

>> Julian Hyde wrote:

>> That said, changing the rex-to-calc generator is probably not the right way

>> to achieve that. The most elegant way to express that optimization would be

>> to internally wrap the WHERE clause condition in "... IS TRUE" and let some

>> rewrites do their work. So, the optimization would already have been done

>> before the rex-to-calc received the program.

> John Sichi wrote:

>

> Yeah, this would apply to the Java calc as well; currently the IS TRUE

> is being inserted right at the point where we generate code (in

> IterCalcRel.implementAbstractTupleIter):

>

> if (program.getCondition() != null) {

> // TODO jvs 8-Oct-2006: move condition to its own

> // method if big, as below for project exprs.

>condBody = new StatementList();

> RexNode rexIsTrue =

> rel.getCluster().getRexBuilder().makeCall(

> SqlStdOperatorTable.isTrueOperator,

> program.getCondition());

> Expression conditionExp =

> translator.translateRexNode(rexIsTrue);

> calcStmts.add(new IfStatement(conditionExp, condBody));

> } else {

> condBody = calcStmts;

> }