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);
}
}
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;
}
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));
}
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());
}
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());
}
Aggregations