Search in sources :

Example 81 with Expression

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

the class JaninoRexCompiler method baz.

/**
 * Given a method that implements {@link Scalar#execute(Context, Object[])},
 * adds a bridge method that implements {@link Scalar#execute(Context)}, and
 * compiles.
 */
static Scalar baz(ParameterExpression context_, ParameterExpression outputValues_, BlockStatement block) {
    final List<MemberDeclaration> declarations = Lists.newArrayList();
    // public void execute(Context, Object[] outputValues)
    declarations.add(Expressions.methodDecl(Modifier.PUBLIC, void.class, BuiltInMethod.SCALAR_EXECUTE2.method.getName(), ImmutableList.of(context_, outputValues_), block));
    // public Object execute(Context)
    final BlockBuilder builder = new BlockBuilder();
    final Expression values_ = builder.append("values", Expressions.newArrayBounds(Object.class, 1, Expressions.constant(1)));
    builder.add(Expressions.statement(Expressions.call(Expressions.parameter(Scalar.class, "this"), BuiltInMethod.SCALAR_EXECUTE2.method, context_, values_)));
    builder.add(Expressions.return_(null, Expressions.arrayIndex(values_, Expressions.constant(0))));
    declarations.add(Expressions.methodDecl(Modifier.PUBLIC, Object.class, BuiltInMethod.SCALAR_EXECUTE1.method.getName(), ImmutableList.of(context_), builder.toBlock()));
    final ClassDeclaration classDeclaration = Expressions.classDecl(Modifier.PUBLIC, "Buzz", null, ImmutableList.<Type>of(Scalar.class), declarations);
    String s = Expressions.toString(declarations, "\n", false);
    if (CalcitePrepareImpl.DEBUG) {
        Util.debugCode(System.out, s);
    }
    try {
        return getScalar(classDeclaration, s);
    } catch (CompileException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassDeclaration(org.apache.calcite.linq4j.tree.ClassDeclaration) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) MemberDeclaration(org.apache.calcite.linq4j.tree.MemberDeclaration) CompileException(org.codehaus.commons.compiler.CompileException) IOException(java.io.IOException) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 82 with Expression

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

the class Schemas method subSchemaExpression.

/**
 * Returns the expression for a sub-schema.
 */
public static Expression subSchemaExpression(SchemaPlus schema, String name, Class type) {
    // (Type) schemaExpression.getSubSchema("name")
    final Expression schemaExpression = expression(schema);
    Expression call = Expressions.call(schemaExpression, BuiltInMethod.SCHEMA_GET_SUB_SCHEMA.method, Expressions.constant(name));
    // noinspection unchecked
    if (false && type != null && !type.isAssignableFrom(Schema.class)) {
        return unwrap(call, type);
    }
    return call;
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression)

Example 83 with Expression

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

the class DeterministicTest method testConstantIsConstant.

@Test
public void testConstantIsConstant() {
    // Small expressions are atomic.
    assertThat(isAtomic(Expressions.constant(0)), is(true));
    assertThat(isAtomic(Expressions.constant("xxx")), is(true));
    assertThat(isAtomic(Expressions.constant(null)), is(true));
    Expression e = Expressions.call(getMethod(Integer.class, "valueOf", int.class), Expressions.constant(-100));
    assertThat(isAtomic(e), is(false));
    assertThat(isConstant(e), is(true));
    e = Expressions.call(Integer.class, "valueOf", Expressions.constant(0));
    assertThat(isAtomic(e), is(false));
    assertThat(isConstant(e), is(true));
    e = Expressions.call(Expressions.constant("xxx"), "length");
    assertThat(isAtomic(e), is(false));
    assertThat(isConstant(e), is(true));
}
Also used : BigInteger(java.math.BigInteger) Expression(org.apache.calcite.linq4j.tree.Expression) BlockBuilderBase.optimizeExpression(org.apache.calcite.linq4j.test.BlockBuilderBase.optimizeExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Test(org.junit.Test)

Example 84 with Expression

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

the class ExpressionTest method testConstantExpression.

@Test
public void testConstantExpression() {
    final Expression constant = Expressions.constant(new Object[] { 1, new Object[] { (byte) 1, (short) 2, (int) 3, (long) 4, (float) 5, (double) 6, (char) 7, true, "string", null }, new AllType(true, (byte) 100, (char) 101, (short) 102, 103, (long) 104, (float) 105, (double) 106, new BigDecimal(107), new BigInteger("108"), "109", null) });
    assertEquals("new Object[] {\n" + "  1,\n" + "  new Object[] {\n" + "    (byte)1,\n" + "    (short)2,\n" + "    3,\n" + "    4L,\n" + "    5.0F,\n" + "    6.0D,\n" + "    (char)7,\n" + "    true,\n" + "    \"string\",\n" + "    null},\n" + "  new org.apache.calcite.linq4j.test.ExpressionTest.AllType(\n" + "    true,\n" + "    (byte)100,\n" + "    (char)101,\n" + "    (short)102,\n" + "    103,\n" + "    104L,\n" + "    105.0F,\n" + "    106.0D,\n" + "    new java.math.BigDecimal(107L),\n" + "    new java.math.BigInteger(\"108\"),\n" + "    \"109\",\n" + "    null)}", constant.toString());
    constant.accept(new Shuttle());
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BigInteger(java.math.BigInteger) Shuttle(org.apache.calcite.linq4j.tree.Shuttle) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 85 with Expression

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

the class ExpressionTest method checkBlockBuilder.

public void checkBlockBuilder(boolean optimizing, String expected) {
    BlockBuilder statements = new BlockBuilder(optimizing);
    Expression one = statements.append("one", Expressions.constant(1));
    Expression two = statements.append("two", Expressions.constant(2));
    Expression three = statements.append("three", Expressions.add(one, two));
    Expression six = statements.append("six", Expressions.multiply(three, two));
    Expression nine = statements.append("nine", Expressions.multiply(three, three));
    Expression eighteen = statements.append("eighteen", Expressions.add(Expressions.add(three, six), nine));
    statements.add(Expressions.return_(null, eighteen));
    BlockStatement expression = statements.toBlock();
    assertEquals(expected, Expressions.toString(expression));
    expression.accept(new Shuttle());
}
Also used : Expression(org.apache.calcite.linq4j.tree.Expression) NewExpression(org.apache.calcite.linq4j.tree.NewExpression) FunctionExpression(org.apache.calcite.linq4j.tree.FunctionExpression) MethodCallExpression(org.apache.calcite.linq4j.tree.MethodCallExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Shuttle(org.apache.calcite.linq4j.tree.Shuttle) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Aggregations

Expression (org.apache.calcite.linq4j.tree.Expression)91 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)64 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)56 ArrayList (java.util.ArrayList)22 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)19 RelDataType (org.apache.calcite.rel.type.RelDataType)19 ConstantExpression (org.apache.calcite.linq4j.tree.ConstantExpression)16 Test (org.junit.Test)16 Type (java.lang.reflect.Type)11 PhysType (org.apache.calcite.adapter.enumerable.PhysType)11 UnaryExpression (org.apache.calcite.linq4j.tree.UnaryExpression)11 RexNode (org.apache.calcite.rex.RexNode)10 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)9 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)8 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)8 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)8 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)7 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)7 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5