Search in sources :

Example 1 with ConstantExpression

use of org.apache.calcite.linq4j.tree.ConstantExpression in project calcite by apache.

the class Linq4jTest method testApproxConstant.

/**
 * We use BigDecimal to represent literals of float and double using
 * BigDecimal, because we want an exact representation.
 */
@Test
public void testApproxConstant() {
    ConstantExpression c;
    c = Expressions.constant(new BigDecimal("3.1"), float.class);
    assertThat(Expressions.toString(c), equalTo("3.1F"));
    c = Expressions.constant(new BigDecimal("-5.156"), float.class);
    assertThat(Expressions.toString(c), equalTo("-5.156F"));
    c = Expressions.constant(new BigDecimal("-51.6"), Float.class);
    assertThat(Expressions.toString(c), equalTo("Float.valueOf(-51.6F)"));
    c = Expressions.constant(new BigDecimal(Float.MAX_VALUE), Float.class);
    assertThat(Expressions.toString(c), equalTo("Float.valueOf(Float.intBitsToFloat(2139095039))"));
    c = Expressions.constant(new BigDecimal(Float.MIN_VALUE), Float.class);
    assertThat(Expressions.toString(c), equalTo("Float.valueOf(Float.intBitsToFloat(1))"));
    c = Expressions.constant(new BigDecimal("3.1"), double.class);
    assertThat(Expressions.toString(c), equalTo("3.1D"));
    c = Expressions.constant(new BigDecimal("-5.156"), double.class);
    assertThat(Expressions.toString(c), equalTo("-5.156D"));
    c = Expressions.constant(new BigDecimal("-51.6"), Double.class);
    assertThat(Expressions.toString(c), equalTo("Double.valueOf(-51.6D)"));
    c = Expressions.constant(new BigDecimal(Double.MAX_VALUE), Double.class);
    assertThat(Expressions.toString(c), equalTo("Double.valueOf(Double.longBitsToDouble(9218868437227405311L))"));
    c = Expressions.constant(new BigDecimal(Double.MIN_VALUE), Double.class);
    assertThat(Expressions.toString(c), equalTo("Double.valueOf(Double.longBitsToDouble(1L))"));
}
Also used : ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with ConstantExpression

use of org.apache.calcite.linq4j.tree.ConstantExpression in project calcite by apache.

the class OptimizerTest method testAssign.

@Test
public void testAssign() {
    // long x = 0;
    // final long y = System.currentTimeMillis();
    // if (System.nanoTime() > 0) {
    // x = y;
    // }
    // System.out.println(x);
    // 
    // In bug https://github.com/julianhyde/linq4j/issues/27, this was
    // incorrectly optimized to
    // 
    // if (System.nanoTime() > 0L) {
    // System.currentTimeMillis();
    // }
    // System.out.println(0L);
    final ParameterExpression x_ = Expressions.parameter(long.class, "x");
    final ParameterExpression y_ = Expressions.parameter(long.class, "y");
    final Method mT = Linq4j.getMethod("java.lang.System", "currentTimeMillis");
    final Method mNano = Linq4j.getMethod("java.lang.System", "nanoTime");
    final ConstantExpression zero = Expressions.constant(0L);
    assertThat(optimize(Expressions.block(Expressions.declare(0, x_, zero), Expressions.declare(Modifier.FINAL, y_, Expressions.call(mT)), Expressions.ifThen(Expressions.greaterThan(Expressions.call(mNano), zero), Expressions.statement(Expressions.assign(x_, y_))), Expressions.statement(Expressions.call(Expressions.field(null, System.class, "out"), "println", x_)))), equalTo("{\n" + "  long x = 0L;\n" + "  if (System.nanoTime() > 0L) {\n" + "    x = System.currentTimeMillis();\n" + "  }\n" + "  System.out.println(x);\n" + "}\n"));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 3 with ConstantExpression

use of org.apache.calcite.linq4j.tree.ConstantExpression in project calcite by apache.

the class OptimizerTest method testAssign2.

@Test
public void testAssign2() {
    // long x = 0;
    // final long y = System.currentTimeMillis();
    // if (System.currentTimeMillis() > 0) {
    // x = y;
    // }
    // 
    // Make sure we don't fold two calls to System.currentTimeMillis into one.
    final ParameterExpression x_ = Expressions.parameter(long.class, "x");
    final ParameterExpression y_ = Expressions.parameter(long.class, "y");
    final Method mT = Linq4j.getMethod("java.lang.System", "currentTimeMillis");
    final ConstantExpression zero = Expressions.constant(0L);
    assertThat(optimize(Expressions.block(Expressions.declare(0, x_, zero), Expressions.declare(Modifier.FINAL, y_, Expressions.call(mT)), Expressions.ifThen(Expressions.greaterThan(Expressions.call(mT), zero), Expressions.statement(Expressions.assign(x_, y_))))), equalTo("{\n" + "  long x = 0L;\n" + "  if (System.currentTimeMillis() > 0L) {\n" + "    x = System.currentTimeMillis();\n" + "  }\n" + "}\n"));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 4 with ConstantExpression

use of org.apache.calcite.linq4j.tree.ConstantExpression in project calcite by apache.

the class EnumerableRelImplementor method classDecl.

private ClassDeclaration classDecl(JavaTypeFactoryImpl.SyntheticRecordType type) {
    ClassDeclaration classDeclaration = Expressions.classDecl(Modifier.PUBLIC | Modifier.STATIC, type.getName(), null, ImmutableList.<Type>of(Serializable.class), new ArrayList<MemberDeclaration>());
    // ...
    for (Types.RecordField field : type.getRecordFields()) {
        classDeclaration.memberDeclarations.add(Expressions.fieldDecl(field.getModifiers(), Expressions.parameter(field.getType(), field.getName()), null));
    }
    // Constructor:
    // Foo(T0 f0, ...) { this.f0 = f0; ... }
    final BlockBuilder blockBuilder = new BlockBuilder();
    final List<ParameterExpression> parameters = new ArrayList<>();
    final ParameterExpression thisParameter = Expressions.parameter(type, "this");
    // Here a constructor without parameter is used because the generated
    // code could cause error if number of fields is too large.
    classDeclaration.memberDeclarations.add(Expressions.constructorDecl(Modifier.PUBLIC, type, parameters, blockBuilder.toBlock()));
    // equals method():
    // public boolean equals(Object o) {
    // if (this == o) return true;
    // if (!(o instanceof MyClass)) return false;
    // final MyClass that = (MyClass) o;
    // return this.f0 == that.f0
    // && equal(this.f1, that.f1)
    // ...
    // }
    final BlockBuilder blockBuilder2 = new BlockBuilder();
    final ParameterExpression thatParameter = Expressions.parameter(type, "that");
    final ParameterExpression oParameter = Expressions.parameter(Object.class, "o");
    blockBuilder2.add(Expressions.ifThen(Expressions.equal(thisParameter, oParameter), Expressions.return_(null, Expressions.constant(true))));
    blockBuilder2.add(Expressions.ifThen(Expressions.not(Expressions.typeIs(oParameter, type)), Expressions.return_(null, Expressions.constant(false))));
    blockBuilder2.add(Expressions.declare(Modifier.FINAL, thatParameter, Expressions.convert_(oParameter, type)));
    final List<Expression> conditions = new ArrayList<>();
    for (Types.RecordField field : type.getRecordFields()) {
        conditions.add(Primitive.is(field.getType()) ? Expressions.equal(Expressions.field(thisParameter, field.getName()), Expressions.field(thatParameter, field.getName())) : Expressions.call(BuiltInMethod.OBJECTS_EQUAL.method, Expressions.field(thisParameter, field.getName()), Expressions.field(thatParameter, field.getName())));
    }
    blockBuilder2.add(Expressions.return_(null, Expressions.foldAnd(conditions)));
    classDeclaration.memberDeclarations.add(Expressions.methodDecl(Modifier.PUBLIC, boolean.class, "equals", Collections.singletonList(oParameter), blockBuilder2.toBlock()));
    // hashCode method:
    // public int hashCode() {
    // int h = 0;
    // h = hash(h, f0);
    // ...
    // return h;
    // }
    final BlockBuilder blockBuilder3 = new BlockBuilder();
    final ParameterExpression hParameter = Expressions.parameter(int.class, "h");
    final ConstantExpression constantZero = Expressions.constant(0);
    blockBuilder3.add(Expressions.declare(0, hParameter, constantZero));
    for (Types.RecordField field : type.getRecordFields()) {
        final Method method = BuiltInMethod.HASH.method;
        blockBuilder3.add(Expressions.statement(Expressions.assign(hParameter, Expressions.call(method.getDeclaringClass(), method.getName(), ImmutableList.of(hParameter, Expressions.field(thisParameter, field))))));
    }
    blockBuilder3.add(Expressions.return_(null, hParameter));
    classDeclaration.memberDeclarations.add(Expressions.methodDecl(Modifier.PUBLIC, int.class, "hashCode", Collections.<ParameterExpression>emptyList(), blockBuilder3.toBlock()));
    // compareTo method:
    // public int compareTo(MyClass that) {
    // int c;
    // c = compare(this.f0, that.f0);
    // if (c != 0) return c;
    // ...
    // return 0;
    // }
    final BlockBuilder blockBuilder4 = new BlockBuilder();
    final ParameterExpression cParameter = Expressions.parameter(int.class, "c");
    final int mod = type.getRecordFields().size() == 1 ? Modifier.FINAL : 0;
    blockBuilder4.add(Expressions.declare(mod, cParameter, null));
    final ConditionalStatement conditionalStatement = Expressions.ifThen(Expressions.notEqual(cParameter, constantZero), Expressions.return_(null, cParameter));
    for (Types.RecordField field : type.getRecordFields()) {
        MethodCallExpression compareCall;
        try {
            final Method method = (field.nullable() ? BuiltInMethod.COMPARE_NULLS_LAST : BuiltInMethod.COMPARE).method;
            compareCall = Expressions.call(method.getDeclaringClass(), method.getName(), Expressions.field(thisParameter, field), Expressions.field(thatParameter, field));
        } catch (RuntimeException e) {
            if (e.getCause() instanceof NoSuchMethodException) {
                // In those cases it is fine if we skip the problematic fields.
                continue;
            }
            throw e;
        }
        blockBuilder4.add(Expressions.statement(Expressions.assign(cParameter, compareCall)));
        blockBuilder4.add(conditionalStatement);
    }
    blockBuilder4.add(Expressions.return_(null, constantZero));
    classDeclaration.memberDeclarations.add(Expressions.methodDecl(Modifier.PUBLIC, int.class, "compareTo", Collections.singletonList(thatParameter), blockBuilder4.toBlock()));
    // toString method:
    // public String toString() {
    // return "{f0=" + f0
    // + ", f1=" + f1
    // ...
    // + "}";
    // }
    final BlockBuilder blockBuilder5 = new BlockBuilder();
    Expression expression5 = null;
    for (Types.RecordField field : type.getRecordFields()) {
        if (expression5 == null) {
            expression5 = Expressions.constant("{" + field.getName() + "=");
        } else {
            expression5 = Expressions.add(expression5, Expressions.constant(", " + field.getName() + "="));
        }
        expression5 = Expressions.add(expression5, Expressions.field(thisParameter, field.getName()));
    }
    expression5 = expression5 == null ? Expressions.constant("{}") : Expressions.add(expression5, Expressions.constant("}"));
    blockBuilder5.add(Expressions.return_(null, expression5));
    classDeclaration.memberDeclarations.add(Expressions.methodDecl(Modifier.PUBLIC, String.class, "toString", Collections.<ParameterExpression>emptyList(), blockBuilder5.toBlock()));
    return classDeclaration;
}
Also used : Types(org.apache.calcite.linq4j.tree.Types) Serializable(java.io.Serializable) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ConditionalStatement(org.apache.calcite.linq4j.tree.ConditionalStatement) ArrayList(java.util.ArrayList) BuiltInMethod(org.apache.calcite.util.BuiltInMethod) Method(java.lang.reflect.Method) ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) NewArrayExpression(org.apache.calcite.linq4j.tree.NewArrayExpression) Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 5 with ConstantExpression

use of org.apache.calcite.linq4j.tree.ConstantExpression in project calcite by apache.

the class RexToLixTranslator method handleNull.

/**
 * Adapts an expression with "normal" result to one that adheres to
 * this particular policy. Wraps the result expression into a new
 * parameter if need be.
 *
 * @param input Expression
 * @param nullAs If false, if expression is definitely not null at
 *   runtime. Therefore we can optimize. For example, we can cast to int
 *   using x.intValue().
 * @return Translated expression
 */
public Expression handleNull(Expression input, RexImpTable.NullAs nullAs) {
    final Expression nullHandled = nullAs.handle(input);
    // If we get ConstantExpression, just return it (i.e. primitive false)
    if (nullHandled instanceof ConstantExpression) {
        return nullHandled;
    }
    // then we can just reuse it
    if (nullHandled == input) {
        return input;
    }
    // If nullHandled is different, then it might be unsafe to compute
    // early (i.e. unbox of null value should not happen _before_ ternary).
    // Thus we wrap it into brand-new ParameterExpression,
    // and we are guaranteed that ParameterExpression will not be shared
    String unboxVarName = "v_unboxed";
    if (input instanceof ParameterExpression) {
        unboxVarName = ((ParameterExpression) input).name + "_unboxed";
    }
    ParameterExpression unboxed = Expressions.parameter(nullHandled.getType(), list.newName(unboxVarName));
    list.add(Expressions.declare(Modifier.FINAL, unboxed, nullHandled));
    return unboxed;
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) UnaryExpression(org.apache.calcite.linq4j.tree.UnaryExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ConstantExpression(org.apache.calcite.linq4j.tree.ConstantExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ByteString(org.apache.calcite.avatica.util.ByteString)

Aggregations

ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)6 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)4 Method (java.lang.reflect.Method)3 Test (org.junit.Test)3 Expression (org.apache.calcite.linq4j.tree.Expression)2 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)2 BuiltInMethod (org.apache.calcite.util.BuiltInMethod)2 Serializable (java.io.Serializable)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)1 ClassDeclaration (org.apache.calcite.linq4j.tree.ClassDeclaration)1 ConditionalStatement (org.apache.calcite.linq4j.tree.ConditionalStatement)1 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)1 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)1 NewArrayExpression (org.apache.calcite.linq4j.tree.NewArrayExpression)1 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)1 Types (org.apache.calcite.linq4j.tree.Types)1 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)1