use of org.apache.calcite.rex.RexProgram 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.RexProgram in project calcite by apache.
the class RexProgramTest method testSimplifyCondition.
/**
* Tests how the condition is simplified.
*/
@Test
public void testSimplifyCondition() {
final RexProgram program = createProg(3).getProgram(false);
assertThat(program.toString(), is("(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)], expr#8=[5], expr#9=[>($t2, $t8)], " + "expr#10=[true], expr#11=[IS NOT NULL($t5)], expr#12=[false], " + "expr#13=[null], expr#14=[CASE($t9, $t10, $t11, $t12, $t13)], " + "expr#15=[NOT($t14)], a=[$t7], b=[$t6], $condition=[$t15])"));
assertThat(program.normalize(rexBuilder, simplify).toString(), is("(expr#0..1=[{inputs}], expr#2=[+($t0, $t1)], expr#3=[1], " + "expr#4=[+($t0, $t3)], expr#5=[+($t2, $t4)], " + "expr#6=[+($t0, $t4)], expr#7=[5], expr#8=[>($t4, $t7)], " + "expr#9=[CAST($t8):BOOLEAN], expr#10=[IS FALSE($t9)], " + "a=[$t5], b=[$t6], $condition=[$t10])"));
}
use of org.apache.calcite.rex.RexProgram in project flink by apache.
the class PushFilterPastChangelogNormalizeRule method onMatch.
@Override
public void onMatch(RelOptRuleCall call) {
final StreamPhysicalCalc calc = call.rel(0);
final StreamPhysicalChangelogNormalize changelogNormalize = call.rel(1);
final RexProgram program = calc.getProgram();
final RexNode condition = RexUtil.toCnf(call.builder().getRexBuilder(), program.expandLocalRef(program.getCondition()));
final Set<Integer> primaryKeyIndices = IntStream.of(changelogNormalize.uniqueKeys()).boxed().collect(Collectors.toSet());
// Determine which filters can be pushed (= involve only primary key columns)
final List<RexNode> primaryKeyPredicates = new ArrayList<>();
final List<RexNode> otherPredicates = new ArrayList<>();
partitionPrimaryKeyPredicates(RelOptUtil.conjunctions(condition), primaryKeyIndices, primaryKeyPredicates, otherPredicates);
// Construct a new ChangelogNormalize which has primary key filters pushed into it
final StreamPhysicalChangelogNormalize newChangelogNormalize = pushFiltersThroughChangelogNormalize(call, primaryKeyPredicates);
// Retain only filters which haven't been pushed
transformWithRemainingPredicates(call, newChangelogNormalize, otherPredicates);
}
use of org.apache.calcite.rex.RexProgram in project flink by apache.
the class PushFilterPastChangelogNormalizeRule method projectIdentityWithConditions.
/**
* Returns a {@link StreamPhysicalCalc} with the given {@param conditions} and an identity
* projection.
*/
private StreamPhysicalCalc projectIdentityWithConditions(RelBuilder relBuilder, RelNode newInput, List<RexNode> conditions) {
final RexProgramBuilder programBuilder = new RexProgramBuilder(newInput.getRowType(), relBuilder.getRexBuilder());
programBuilder.addIdentity();
final RexNode condition = relBuilder.and(conditions);
if (!condition.isAlwaysTrue()) {
programBuilder.addCondition(condition);
}
final RexProgram newProgram = programBuilder.getProgram();
return new StreamPhysicalCalc(newInput.getCluster(), newInput.getTraitSet(), newInput, newProgram, newProgram.getOutputRowType());
}
use of org.apache.calcite.rex.RexProgram in project flink by apache.
the class PushFilterPastChangelogNormalizeRule method projectWith.
/**
* Returns a {@link StreamPhysicalCalc} which is a copy of {@param calc}, but with the
* projections applied from {@param projectFromCalc}.
*/
private StreamPhysicalCalc projectWith(RelBuilder relBuilder, StreamPhysicalCalc projectFromCalc, StreamPhysicalCalc calc) {
final RexProgramBuilder programBuilder = new RexProgramBuilder(calc.getRowType(), relBuilder.getRexBuilder());
if (calc.getProgram().getCondition() != null) {
programBuilder.addCondition(calc.getProgram().expandLocalRef(calc.getProgram().getCondition()));
}
for (Pair<RexLocalRef, String> projectRef : projectFromCalc.getProgram().getNamedProjects()) {
final RexNode project = projectFromCalc.getProgram().expandLocalRef(projectRef.left);
programBuilder.addProject(project, projectRef.right);
}
final RexProgram newProgram = programBuilder.getProgram();
return (StreamPhysicalCalc) calc.copy(calc.getTraitSet(), calc.getInput(), newProgram);
}
Aggregations