use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class OptimizerTest method testConditionalIfBoolTrue.
@Test
public void testConditionalIfBoolTrue() {
// if (bool) {return 1} else if (true) {return 2}
Expression bool = Expressions.parameter(boolean.class, "bool");
assertEquals("{\n" + " if (bool) {\n" + " return 1;\n" + " } else {\n" + " return 2;\n" + " }\n" + "}\n", optimize(Expressions.ifThenElse(bool, Expressions.return_(null, ONE), TRUE, Expressions.return_(null, TWO))));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class OptimizerTest method testConditionalIfBoolFalseTrue.
@Test
public void testConditionalIfBoolFalseTrue() {
// if (bool) {1} else if (false) {2} if (true) {4} else {5}
Expression bool = Expressions.parameter(boolean.class, "bool");
assertEquals("{\n" + " if (bool) {\n" + " return 1;\n" + " } else {\n" + " return 4;\n" + " }\n" + "}\n", optimize(Expressions.ifThenElse(bool, Expressions.return_(null, ONE), FALSE, Expressions.return_(null, TWO), TRUE, Expressions.return_(null, FOUR), Expressions.return_(null, Expressions.constant(5)))));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class OptimizerTest method testConditionalIfBoolTrueElse.
@Test
public void testConditionalIfBoolTrueElse() {
// if (bool) {return 1} else if (true) {return 2} else {return 3}
Expression bool = Expressions.parameter(boolean.class, "bool");
assertEquals("{\n" + " if (bool) {\n" + " return 1;\n" + " } else {\n" + " return 2;\n" + " }\n" + "}\n", optimize(Expressions.ifThenElse(bool, Expressions.return_(null, ONE), TRUE, Expressions.return_(null, TWO), Expressions.return_(null, THREE))));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class PhysTypeImpl method generateComparator.
public Expression generateComparator(RelCollation collation) {
// int c;
// c = Utilities.compare(v0, v1);
// if (c != 0) return c; // or -c if descending
// ...
// return 0;
BlockBuilder body = new BlockBuilder();
final Type javaRowClass = Primitive.box(this.javaRowClass);
final ParameterExpression parameterV0 = Expressions.parameter(javaRowClass, "v0");
final ParameterExpression parameterV1 = Expressions.parameter(javaRowClass, "v1");
final ParameterExpression parameterC = Expressions.parameter(int.class, "c");
final int mod = collation.getFieldCollations().size() == 1 ? Modifier.FINAL : 0;
body.add(Expressions.declare(mod, parameterC, null));
for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
final int index = fieldCollation.getFieldIndex();
Expression arg0 = fieldReference(parameterV0, index);
Expression arg1 = fieldReference(parameterV1, index);
switch(Primitive.flavor(fieldClass(index))) {
case OBJECT:
arg0 = Types.castIfNecessary(Comparable.class, arg0);
arg1 = Types.castIfNecessary(Comparable.class, arg1);
}
final boolean nullsFirst = fieldCollation.nullDirection == RelFieldCollation.NullDirection.FIRST;
final boolean descending = fieldCollation.getDirection() == RelFieldCollation.Direction.DESCENDING;
body.add(Expressions.statement(Expressions.assign(parameterC, Expressions.call(Utilities.class, fieldNullable(index) ? (nullsFirst != descending ? "compareNullsFirst" : "compareNullsLast") : "compare", arg0, arg1))));
body.add(Expressions.ifThen(Expressions.notEqual(parameterC, Expressions.constant(0)), Expressions.return_(null, descending ? Expressions.negate(parameterC) : parameterC)));
}
body.add(Expressions.return_(null, Expressions.constant(0)));
final List<MemberDeclaration> memberDeclarations = Expressions.<MemberDeclaration>list(Expressions.methodDecl(Modifier.PUBLIC, int.class, "compare", ImmutableList.of(parameterV0, parameterV1), body.toBlock()));
if (EnumerableRules.BRIDGE_METHODS) {
final ParameterExpression parameterO0 = Expressions.parameter(Object.class, "o0");
final ParameterExpression parameterO1 = Expressions.parameter(Object.class, "o1");
BlockBuilder bridgeBody = new BlockBuilder();
bridgeBody.add(Expressions.return_(null, Expressions.call(Expressions.parameter(Comparable.class, "this"), BuiltInMethod.COMPARATOR_COMPARE.method, Expressions.convert_(parameterO0, javaRowClass), Expressions.convert_(parameterO1, javaRowClass))));
memberDeclarations.add(overridingMethodDecl(BuiltInMethod.COMPARATOR_COMPARE.method, ImmutableList.of(parameterO0, parameterO1), bridgeBody.toBlock()));
}
return Expressions.new_(Comparator.class, Collections.<Expression>emptyList(), memberDeclarations);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.Expression in project calcite by apache.
the class PhysTypeImpl method generateCollationKey.
public Pair<Expression, Expression> generateCollationKey(final List<RelFieldCollation> collations) {
final Expression selector;
if (collations.size() == 1) {
RelFieldCollation collation = collations.get(0);
ParameterExpression parameter = Expressions.parameter(javaRowClass, "v");
selector = Expressions.lambda(Function1.class, fieldReference(parameter, collation.getFieldIndex()), parameter);
return Pair.<Expression, Expression>of(selector, Expressions.call(BuiltInMethod.NULLS_COMPARATOR.method, Expressions.constant(collation.nullDirection == RelFieldCollation.NullDirection.FIRST), Expressions.constant(collation.getDirection() == RelFieldCollation.Direction.DESCENDING)));
}
selector = Expressions.call(BuiltInMethod.IDENTITY_SELECTOR.method);
// int c;
// c = Utilities.compare(v0, v1);
// if (c != 0) return c; // or -c if descending
// ...
// return 0;
BlockBuilder body = new BlockBuilder();
final ParameterExpression parameterV0 = Expressions.parameter(javaRowClass, "v0");
final ParameterExpression parameterV1 = Expressions.parameter(javaRowClass, "v1");
final ParameterExpression parameterC = Expressions.parameter(int.class, "c");
final int mod = collations.size() == 1 ? Modifier.FINAL : 0;
body.add(Expressions.declare(mod, parameterC, null));
for (RelFieldCollation collation : collations) {
final int index = collation.getFieldIndex();
Expression arg0 = fieldReference(parameterV0, index);
Expression arg1 = fieldReference(parameterV1, index);
switch(Primitive.flavor(fieldClass(index))) {
case OBJECT:
arg0 = Types.castIfNecessary(Comparable.class, arg0);
arg1 = Types.castIfNecessary(Comparable.class, arg1);
}
final boolean nullsFirst = collation.nullDirection == RelFieldCollation.NullDirection.FIRST;
final boolean descending = collation.getDirection() == RelFieldCollation.Direction.DESCENDING;
final Method method = (fieldNullable(index) ? (nullsFirst ^ descending ? BuiltInMethod.COMPARE_NULLS_FIRST : BuiltInMethod.COMPARE_NULLS_LAST) : BuiltInMethod.COMPARE).method;
body.add(Expressions.statement(Expressions.assign(parameterC, Expressions.call(method.getDeclaringClass(), method.getName(), arg0, arg1))));
body.add(Expressions.ifThen(Expressions.notEqual(parameterC, Expressions.constant(0)), Expressions.return_(null, descending ? Expressions.negate(parameterC) : parameterC)));
}
body.add(Expressions.return_(null, Expressions.constant(0)));
final List<MemberDeclaration> memberDeclarations = Expressions.<MemberDeclaration>list(Expressions.methodDecl(Modifier.PUBLIC, int.class, "compare", ImmutableList.of(parameterV0, parameterV1), body.toBlock()));
if (EnumerableRules.BRIDGE_METHODS) {
final ParameterExpression parameterO0 = Expressions.parameter(Object.class, "o0");
final ParameterExpression parameterO1 = Expressions.parameter(Object.class, "o1");
BlockBuilder bridgeBody = new BlockBuilder();
bridgeBody.add(Expressions.return_(null, Expressions.call(Expressions.parameter(Comparable.class, "this"), BuiltInMethod.COMPARATOR_COMPARE.method, Expressions.convert_(parameterO0, javaRowClass), Expressions.convert_(parameterO1, javaRowClass))));
memberDeclarations.add(overridingMethodDecl(BuiltInMethod.COMPARATOR_COMPARE.method, ImmutableList.of(parameterO0, parameterO1), bridgeBody.toBlock()));
}
return Pair.<Expression, Expression>of(selector, Expressions.new_(Comparator.class, Collections.<Expression>emptyList(), memberDeclarations));
}
Aggregations