use of org.apache.calcite.rex.RexProgramBuilder in project calcite by apache.
the class FilterToCalcRule method onMatch.
// ~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
final LogicalFilter filter = call.rel(0);
final RelNode rel = filter.getInput();
// Create a program containing a filter.
final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
final RelDataType inputRowType = rel.getRowType();
final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
programBuilder.addIdentity();
programBuilder.addCondition(filter.getCondition());
final RexProgram program = programBuilder.getProgram();
final LogicalCalc calc = LogicalCalc.create(rel, program);
call.transformTo(calc);
}
use of org.apache.calcite.rex.RexProgramBuilder in project calcite by apache.
the class RexProgramTest method testBuildProgram.
/**
* Tests construction of a RexProgram.
*/
@Test
public void testBuildProgram() {
final RexProgramBuilder builder = createProg(0);
final RexProgram program = builder.getProgram(false);
final String programString = program.toString();
TestUtil.assertEqualsVerbose("(expr#0..1=[{inputs}], expr#2=[+($0, 1)], expr#3=[77], " + "expr#4=[+($0, $1)], expr#5=[+($0, $0)], expr#6=[+($t4, $t2)], " + "a=[$t6], b=[$t5])", programString);
// Normalize the program using the RexProgramBuilder.normalize API.
// Note that unused expression '77' is eliminated, input refs (e.g. $0)
// become local refs (e.g. $t0), and constants are assigned to locals.
final RexProgram normalizedProgram = program.normalize(rexBuilder, null);
final String normalizedProgramString = normalizedProgram.toString();
TestUtil.assertEqualsVerbose("(expr#0..1=[{inputs}], expr#2=[+($t0, $t1)], expr#3=[1], " + "expr#4=[+($t0, $t3)], expr#5=[+($t2, $t4)], " + "expr#6=[+($t0, $t0)], a=[$t5], b=[$t6])", normalizedProgramString);
}
use of org.apache.calcite.rex.RexProgramBuilder in project calcite by apache.
the class RexProgramTest method testNormalize.
/**
* Tests construction and normalization of a RexProgram.
*/
@Test
public void testNormalize() {
final RexProgramBuilder builder = createProg(0);
final String program = builder.getProgram(true).toString();
TestUtil.assertEqualsVerbose("(expr#0..1=[{inputs}], expr#2=[+($t0, $t1)], expr#3=[1], " + "expr#4=[+($t0, $t3)], expr#5=[+($t2, $t4)], " + "expr#6=[+($t0, $t0)], a=[$t5], b=[$t6])", program);
}
use of org.apache.calcite.rex.RexProgramBuilder in project calcite by apache.
the class RexProgramTest method testDuplicateAnd.
/**
* Checks translation of AND(x, x).
*/
@Test
public void testDuplicateAnd() {
// RexProgramBuilder used to translate AND(x, x) to x.
// Now it translates it to AND(x, x).
// The optimization of AND(x, x) => x occurs at a higher level.
final RexProgramBuilder builder = createProg(2);
final String program = builder.getProgram(true).toString();
TestUtil.assertEqualsVerbose("(expr#0..1=[{inputs}], expr#2=[+($t0, $t1)], expr#3=[1], " + "expr#4=[+($t0, $t3)], expr#5=[+($t2, $t4)], " + "expr#6=[+($t0, $t0)], expr#7=[>($t2, $t0)], " + "a=[$t5], b=[$t6], $condition=[$t7])", program);
}
use of org.apache.calcite.rex.RexProgramBuilder in project calcite by apache.
the class RexProgramTest method testElimDups.
/**
* Tests construction and normalization of a RexProgram.
*/
@Test
public void testElimDups() {
final RexProgramBuilder builder = createProg(1);
final String unnormalizedProgram = builder.getProgram(false).toString();
TestUtil.assertEqualsVerbose("(expr#0..1=[{inputs}], expr#2=[+($0, 1)], expr#3=[77], " + "expr#4=[+($0, $1)], expr#5=[+($0, 1)], expr#6=[+($0, $t5)], " + "expr#7=[+($t4, $t2)], a=[$t7], b=[$t6])", unnormalizedProgram);
// normalize eliminates duplicates (specifically "+($0, $1)")
final RexProgramBuilder builder2 = createProg(1);
final String program2 = builder2.getProgram(true).toString();
TestUtil.assertEqualsVerbose("(expr#0..1=[{inputs}], expr#2=[+($t0, $t1)], expr#3=[1], " + "expr#4=[+($t0, $t3)], expr#5=[+($t2, $t4)], " + "expr#6=[+($t0, $t4)], a=[$t5], b=[$t6])", program2);
}
Aggregations