/** * Returns whether this program is in canonical form. * * @param fail Whether to throw an assertion error if not in canonical * form * @param rexBuilder Rex builder * @return whether in canonical form */ public boolean isNormalized(boolean fail, RexBuilder rexBuilder) { final RexProgram normalizedProgram = RexProgramBuilder.normalize(rexBuilder, this); String normalized = normalizedProgram.toString(); String string = toString(); if (!normalized.equals(string)) { assert !fail : "Program is not normalized:\n" + "program: " + string + "\n" + "normalized: " + normalized + "\n"; return false; } return true; }
/** * Returns whether this program is in canonical form. * * @param fail Whether to throw an assertion error if not in canonical form * @param rexBuilder Rex builder * @return whether in canonical form */ public boolean isNormalized(boolean fail, RexBuilder rexBuilder) { final RexProgram normalizedProgram = RexProgramBuilder.normalize(rexBuilder, this); String normalized = normalizedProgram.toString(); String string = toString(); if (!normalized.equals(string)) { assert !fail : "Program is not normalized:\n" + "program: " + string + "\n" + "normalized: " + normalized + "\n"; return false; } return true; }
/** * Returns whether the fields on the leading edge of the project list are * the input fields. * * @param fail Whether to throw an assert failure if does not project * identity */ public boolean projectsIdentity(final boolean fail) { final int fieldCount = inputRowType.getFieldCount(); if (projects.size() < fieldCount) { assert !fail : "program '" + toString() + "' does not project identity for input row type '" + inputRowType + "'"; return false; } for (int i = 0; i < fieldCount; i++) { RexLocalRef project = projects.get(i); if (project.index != i) { assert !fail : "program " + toString() + "' does not project identity for input row type '" + inputRowType + "', field #" + i; return false; } } return true; }
/** * Returns whether the fields on the leading edge of the project list are * the input fields. * * @param fail Whether to throw an assert failure if does not project * identity */ public boolean projectsIdentity(final boolean fail) { final int fieldCount = inputRowType.getFieldCount(); if (projects.size() < fieldCount) { assert !fail : "program '" + toString() + "' does not project identity for input row type '" + inputRowType + "'"; return false; } for (int i = 0; i < fieldCount; i++) { RexLocalRef project = projects.get(i); if (project.index != i) { assert !fail : "program " + toString() + "' does not project identity for input row type '" + inputRowType + "', field #" + i; return false; } } return true; }
PrintWriter traceWriter = new PrintWriter(traceMsg); traceWriter.println("FarragoAutoCalcRule result expressions for: "); traceWriter.println(program.toString());
PrintWriter traceWriter = new PrintWriter(traceMsg); traceWriter.println("FarragoAutoCalcRule result expressions for: "); traceWriter.println(program.toString());
/** * 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); }
/** * 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); }
/** * Tests that AND(x, x) is translated to x. */ @Test public void testDuplicateAnd() { 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); }
/** * 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 = RexProgramBuilder.normalize( rexBuilder, program); 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); }
/** * 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 = RexProgramBuilder.normalize( rexBuilder, program); 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); }
/** * 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 dups (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); }
/** * 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 dups (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); }
/** * 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)], " + "expr#8=[AND($t7, $t7)], expr#9=[AND($t8, $t7)], " + "a=[$t5], b=[$t6], $condition=[$t9])", program); }