Search in sources :

Example 61 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class GeodeToEnumerableConverter method implement.

/**
 * {@inheritDoc}
 *
 * @param implementor GeodeImplementContext
 */
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    // travers all relations form this to the scan leaf
    final GeodeImplementContext geodeImplementContext = new GeodeImplementContext();
    ((GeodeRel) getInput()).implement(geodeImplementContext);
    // PhysType is Enumerable Adapter class that maps SQL types (getRowType)
    // with physical Java types (getJavaTypes())
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY));
    final List<Class> physFieldClasses = new AbstractList<Class>() {

        public Class get(int index) {
            return physType.fieldClass(index);
        }

        public int size() {
            return rowType.getFieldCount();
        }
    };
    // Expression meta-program for calling the GeodeTable.GeodeQueryable#query
    // method form the generated code
    final BlockBuilder blockBuilder = new BlockBuilder().append(Expressions.call(geodeImplementContext.table.getExpression(GeodeTable.GeodeQueryable.class), GEODE_QUERY_METHOD, constantArrayList(Pair.zip(geodeFieldNames(rowType), physFieldClasses), Pair.class), // physical fields
    constantArrayList(toListMapPairs(geodeImplementContext.selectFields), Pair.class), // selected fields
    constantArrayList(toListMapPairs(geodeImplementContext.oqlAggregateFunctions), Pair.class), constantArrayList(geodeImplementContext.groupByFields, String.class), constantArrayList(geodeImplementContext.whereClause, String.class), constantArrayList(geodeImplementContext.orderByFields, String.class), Expressions.constant(geodeImplementContext.limitValue)));
    Hook.QUERY_PLAN.run(geodeImplementContext);
    return implementor.result(physType, blockBuilder.toBlock());
}
Also used : AbstractList(java.util.AbstractList) PhysType(org.apache.calcite.adapter.enumerable.PhysType) GeodeImplementContext(org.apache.calcite.adapter.geode.rel.GeodeRel.GeodeImplementContext) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 62 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class SplunkTableScan method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    Map map = ImmutableMap.builder().put("search", search).put("earliest", Util.first(earliest, "")).put("latest", Util.first(latest, "")).put("fieldList", fieldList).build();
    if (CalcitePrepareImpl.DEBUG) {
        System.out.println("Splunk: " + map);
    }
    Hook.QUERY_PLAN.run(map);
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferCustom());
    final BlockBuilder builder = new BlockBuilder();
    return implementor.result(physType, builder.append(Expressions.call(table.getExpression(SplunkTable.SplunkTableQueryable.class), METHOD, Expressions.constant(search), Expressions.constant(earliest), Expressions.constant(latest), fieldList == null ? Expressions.constant(null) : constantStringList(fieldList))).toBlock());
}
Also used : PhysType(org.apache.calcite.adapter.enumerable.PhysType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 63 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder 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)

Example 64 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class ExpressionTest method testBlockBuilder3.

@Test
public void testBlockBuilder3() {
    /*
    int a = 1;
    int b = a + 2;
    int c = a + 3;
    int d = a + 4;
    int e = {
      int b = a + 3;
      foo(b);
    }
    bar(a, b, c, d, e);
*/
    BlockBuilder builder0 = new BlockBuilder();
    final Expression a = builder0.append("_a", Expressions.constant(1));
    final Expression b = builder0.append("_b", Expressions.add(a, Expressions.constant(2)));
    final Expression c = builder0.append("_c", Expressions.add(a, Expressions.constant(3)));
    final Expression d = builder0.append("_d", Expressions.add(a, Expressions.constant(4)));
    BlockBuilder builder1 = new BlockBuilder();
    final Expression b1 = builder1.append("_b", Expressions.add(a, Expressions.constant(3)));
    builder1.add(Expressions.statement(Expressions.call(ExpressionTest.class, "foo", b1)));
    final Expression e = builder0.append("e", builder1.toBlock());
    builder0.add(Expressions.statement(Expressions.call(ExpressionTest.class, "bar", a, b, c, d, e)));
    // With the bug in BlockBuilder.append(String, BlockExpression),
    // bar(1, _b, _c, _d, foo(_d));
    // Correct result is
    // bar(1, _b, _c, _d, foo(_c));
    // because _c has the same expression (a + 3) as inner b.
    BlockStatement expression = builder0.toBlock();
    assertEquals("{\n" + "  final int _b = 1 + 2;\n" + "  final int _c = 1 + 3;\n" + "  final int _d = 1 + 4;\n" + "  org.apache.calcite.linq4j.test.ExpressionTest.bar(1, _b, _c, _d, org.apache.calcite.linq4j.test.ExpressionTest.foo(_c));\n" + "}\n", 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) Test(org.junit.Test)

Example 65 with BlockBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.linq4j.tree.BlockBuilder in project calcite by apache.

the class ExpressionTest method testFor.

@Test
public void testFor() throws NoSuchFieldException {
    final BlockBuilder builder = new BlockBuilder();
    final ParameterExpression i_ = Expressions.parameter(int.class, "i");
    builder.add(Expressions.for_(Expressions.declare(0, i_, Expressions.constant(0)), Expressions.lessThan(i_, Expressions.constant(10)), Expressions.postIncrementAssign(i_), Expressions.block(Expressions.statement(Expressions.call(Expressions.field(null, System.class.getField("out")), "println", i_)))));
    assertEquals("{\n" + "  for (int i = 0; i < 10; i++) {\n" + "    System.out.println(i);\n" + "  }\n" + "}\n", Expressions.toString(builder.toBlock()));
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)67 Expression (org.apache.calcite.linq4j.tree.Expression)56 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)40 RelDataType (org.apache.calcite.rel.type.RelDataType)15 PhysType (org.apache.calcite.adapter.enumerable.PhysType)14 ArrayList (java.util.ArrayList)12 MethodCallExpression (org.apache.calcite.linq4j.tree.MethodCallExpression)12 Test (org.junit.Test)10 BlockStatement (org.apache.calcite.linq4j.tree.BlockStatement)9 Type (java.lang.reflect.Type)8 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)8 MemberDeclaration (org.apache.calcite.linq4j.tree.MemberDeclaration)7 RexToLixTranslator (org.apache.calcite.adapter.enumerable.RexToLixTranslator)6 NewExpression (org.apache.calcite.linq4j.tree.NewExpression)6 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)5 BinaryExpression (org.apache.calcite.linq4j.tree.BinaryExpression)5 FunctionExpression (org.apache.calcite.linq4j.tree.FunctionExpression)5 RexNode (org.apache.calcite.rex.RexNode)5 EnumerableRelImplementor (org.apache.calcite.adapter.enumerable.EnumerableRelImplementor)4 Function1 (org.apache.calcite.linq4j.function.Function1)4