org.apache.calcite.linq4j.tree.Blocks Java Examples

The following examples show how to use org.apache.calcite.linq4j.tree.Blocks. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testFactorOutBinaryAddSurvivesMultipleOptimizations() {
  assertThat(
      optimize(
          optimizeExpression(
              Expressions.new_(Runnable.class,
                  Collections.emptyList(),
                  Expressions.methodDecl(0,
                      int.class,
                      "test",
                      Collections.emptyList(),
                      Blocks.toFunctionBlock(Expressions.add(ONE, TWO)))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$1_2;\n"
          + "      }\n"
          + "\n"
          + "      static final int $L4J$C$1_2 = 1 + 2;\n"
          + "    };\n"
          + "}\n"));
}
 
Example #2
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testDeterministicClassDefaultMethod() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(null,
                          Types.lookupMethod(TestDeterministicClass.class,
                              "deterministic", int.class),
                          ONE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$org_apache_calcite_linq4j_test_DeterministicTest_TestDeterminis9de610da;\n"
          + "      }\n"
          + "\n"
          + "      static final int $L4J$C$org_apache_calcite_linq4j_test_DeterministicTest_TestDeterminis9de610da = org.apache.calcite.linq4j.test.DeterministicTest.TestDeterministicClass.deterministic(1);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #3
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testNonDeterministicMethodCall() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(null,
                          Types.lookupMethod(TestClass.class,
                              "nonDeterministic", int.class),
                          ONE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return org.apache.calcite.linq4j.test.DeterministicTest.TestClass.nonDeterministic(1);\n"
          + "      }\n"
          + "\n"
          + "    };\n"
          + "}\n"));
}
 
Example #4
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testDeterministicMethodCall() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(null,
                          Types.lookupMethod(TestClass.class,
                              "deterministic", int.class),
                          ONE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$org_apache_calcite_linq4j_test_DeterministicTest_TestClass_dete33e8af1c;\n"
          + "      }\n"
          + "\n"
          + "      static final int $L4J$C$org_apache_calcite_linq4j_test_DeterministicTest_TestClass_dete33e8af1c = org.apache.calcite.linq4j.test.DeterministicTest.TestClass.deterministic(1);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #5
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testDeterministicClassNonDeterministicMethod() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(null,
                          Types.lookupMethod(TestDeterministicClass.class,
                              "nonDeterministic", int.class),
                          ONE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return org.apache.calcite.linq4j.test.DeterministicTest.TestDeterministicClass.nonDeterministic(1);\n"
          + "      }\n"
          + "\n"
          + "    };\n"
          + "}\n"));
}
 
Example #6
Source File: LixToRelTranslator.java    From calcite with Apache License 2.0 6 votes vote down vote up
private List<RexNode> toRex(
    RelNode child, FunctionExpression expression) {
  RexBuilder rexBuilder = cluster.getRexBuilder();
  List<RexNode> list =
      Collections.singletonList(
          rexBuilder.makeRangeReference(child));
  CalcitePrepareImpl.ScalarTranslator translator =
      CalcitePrepareImpl.EmptyScalarTranslator
          .empty(rexBuilder)
          .bind(expression.parameterList, list);
  final List<RexNode> rexList = new ArrayList<>();
  final Expression simple = Blocks.simple(expression.body);
  for (Expression expression1 : fieldExpressions(simple)) {
    rexList.add(translator.toRex(expression1));
  }
  return rexList;
}
 
Example #7
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testIntegerValueOfZeroComplexTest() {
  // Integer.valueOf(0) is optimized in complex expressions
  assertThat(
      optimize(
          Expressions.new_(Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(0, int.class, "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(
                          getMethod(Integer.class, "valueOf", int.class),
                          Expressions.constant(0)))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$Integer_valueOf_0_;\n"
          + "      }\n"
          + "\n"
          + "      static final Integer $L4J$C$Integer_valueOf_0_ = Integer.valueOf(0);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #8
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testInstanceofComplexTest() {
  // instanceof is optimized in complex expressions
  assertThat(
      optimize(
          Expressions.new_(Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(0, int.class, "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.orElse(
                          Expressions.typeIs(ONE, Boolean.class),
                          Expressions.typeIs(TWO, Integer.class)))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$1_instanceof_Boolean_2_instanceof_Integer;\n"
          + "      }\n"
          + "\n"
          + "      static final boolean "
          + "$L4J$C$1_instanceof_Boolean_2_instanceof_Integer = 1 instanceof "
          + "Boolean || 2 instanceof Integer;\n"
          + "    };\n"
          + "}\n"));
}
 
Example #9
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testInstanceofTest() {
  // Single instanceof is not optimized
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0, int.class, "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.typeIs(ONE, Boolean.class))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return 1 instanceof Boolean;\n"
          + "      }\n"
          + "\n"
          + "    };\n"
          + "}\n"));
}
 
Example #10
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testNewBigInteger() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0, int.class, "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.new_(BigInteger.class,
                          Expressions.constant("42")))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$new_java_math_BigInteger_42_;\n"
          + "      }\n"
          + "\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$new_java_math_BigInteger_42_ = new java.math.BigInteger(\n"
          + "        \"42\");\n"
          + "    };\n"
          + "}\n"));
}
 
Example #11
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testFactorOutBinaryAddMul() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.multiply(Expressions.add(ONE, TWO),
                          THREE))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$1_2_3;\n"
          + "      }\n"
          + "\n"
          + "      static final int $L4J$C$1_2 = 1 + 2;\n"
          + "      static final int $L4J$C$1_2_3 = $L4J$C$1_2 * 3;\n"
          + "    };\n"
          + "}\n"));
}
 
Example #12
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testFactorOutBinaryAddNameCollision() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.multiply(Expressions.add(ONE, TWO),
                          Expressions.subtract(ONE, TWO)))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$1_2_1_20;\n"
          + "      }\n"
          + "\n"
          + "      static final int $L4J$C$1_2 = 1 + 2;\n"
          + "      static final int $L4J$C$1_20 = 1 - 2;\n"
          + "      static final int $L4J$C$1_2_1_20 = $L4J$C$1_2 * $L4J$C$1_20;\n"
          + "    };\n"
          + "}\n"));
}
 
Example #13
Source File: DeterministicTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testFactorOutBinaryAdd() {
  assertThat(
      optimize(
          Expressions.new_(
              Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(
                  0,
                  int.class,
                  "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(Expressions.add(ONE, TWO))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return $L4J$C$1_2;\n"
          + "      }\n"
          + "\n"
          + "      static final int $L4J$C$1_2 = 1 + 2;\n"
          + "    };\n"
          + "}\n"));
}
 
Example #14
Source File: CsvTableScan.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
  PhysType physType =
      PhysTypeImpl.of(
          implementor.getTypeFactory(),
          getRowType(),
          pref.preferArray());

  if (table instanceof JsonTable) {
    return implementor.result(
        physType,
        Blocks.toBlock(
            Expressions.call(table.getExpression(JsonTable.class),
                "enumerable")));
  }
  return implementor.result(
      physType,
      Blocks.toBlock(
          Expressions.call(table.getExpression(CsvTranslatableTable.class),
              "project", implementor.getRootExpression(),
              Expressions.constant(fields))));
}
 
Example #15
Source File: ProcessorStatusTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

    return implementor.result(physType, Blocks.toBlock(
        Expressions.call(table.getExpression(ProcessorStatusTable.class), "project", Expressions.constant(fields))));
}
 
Example #16
Source File: EnumerableTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
  // Note that representation is ARRAY. This assumes that the table
  // returns a Object[] for each record. Actually a Table<T> can
  // return any type T. And, if it is a JdbcTable, we'd like to be
  // able to generate alternate accessors that return e.g. synthetic
  // records {T0 f0; T1 f1; ...} and don't box every primitive value.
  final PhysType physType =
      PhysTypeImpl.of(
          implementor.getTypeFactory(),
          getRowType(),
          format());
  final Expression expression = getExpression(physType);
  return implementor.result(physType, Blocks.toBlock(expression));
}
 
Example #17
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static List<Expression> simpleList(BlockStatement statement) {
  Expression simple = Blocks.simple(statement);
  if (simple instanceof NewExpression) {
    NewExpression newExpression = (NewExpression) simple;
    return newExpression.arguments;
  } else {
    return Collections.singletonList(simple);
  }
}
 
Example #18
Source File: FlowFileTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

    return implementor.result(physType, Blocks.toBlock(
        Expressions.call(table.getExpression(FlowFileTable.class), "project", Expressions.constant(fields))));
}
 
Example #19
Source File: ProvenanceTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

    return implementor.result(physType, Blocks.toBlock(
        Expressions.call(table.getExpression(ProvenanceTable.class), "project", Expressions.constant(fields))));
}
 
Example #20
Source File: ConnectionStatusPredictionsTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

    return implementor.result(physType, Blocks.toBlock(
        Expressions.call(table.getExpression(ConnectionStatusPredictionsTable.class), "project", Expressions.constant(fields))));
}
 
Example #21
Source File: ConnectionStatusTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

    return implementor.result(physType, Blocks.toBlock(
        Expressions.call(table.getExpression(ConnectionStatusTable.class), "project", Expressions.constant(fields))));
}
 
Example #22
Source File: JvmMetricsTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

    return implementor.result(physType, Blocks.toBlock(
        Expressions.call(table.getExpression(JvmMetricsTable.class), "project", Expressions.constant(fields))));
}
 
Example #23
Source File: ProcessGroupStatusTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

    return implementor.result(physType, Blocks.toBlock(
        Expressions.call(table.getExpression(ProcessGroupStatusTable.class), "project", Expressions.constant(fields))));
}
 
Example #24
Source File: BulletinTableScan.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());

    return implementor.result(physType, Blocks.toBlock(
        Expressions.call(table.getExpression(BulletinTable.class), "project", Expressions.constant(fields))));
}
 
Example #25
Source File: OLAPJoinRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

    context.setReturnTupleInfo(rowType, columnRowType);

    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());
    RelOptTable factTable = context.firstTableScan.getTable();
    MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeOLAPQuery",
            implementor.getRootExpression(), Expressions.constant(context.id));
    return implementor.result(physType, Blocks.toBlock(exprCall));
}
 
Example #26
Source File: DeterministicTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testBigIntegerValueOf() {
  // instanceof is optimized in complex expressions
  assertThat(
      optimize(
          Expressions.new_(Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(0, int.class, "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(
                          Expressions.call(null,
                              Types.lookupMethod(BigInteger.class, "valueOf",
                                  long.class),
                              Expressions.constant(42L)),
                          "add",
                          Expressions.call(null,
                              Types.lookupMethod(BigInteger.class, "valueOf",
                                  long.class),
                              Expressions.constant(42L))))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return "
          + "$L4J$C$java_math_BigInteger_valueOf_42L_add_java_math_BigInteger_valued8d57d69;\n"
          + "      }\n"
          + "\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$java_math_BigInteger_valueOf_42L_ = java.math.BigInteger"
          + ".valueOf(42L);\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$java_math_BigInteger_valueOf_42L_add_java_math_BigInteger_valued8d57d69 = $L4J$C$java_math_BigInteger_valueOf_42L_.add($L4J$C$java_math_BigInteger_valueOf_42L_);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #27
Source File: DeterministicTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testStaticField() {
  // instanceof is optimized in complex expressions
  assertThat(
      optimize(
          Expressions.new_(Runnable.class,
              Collections.emptyList(),
              Expressions.methodDecl(0, int.class, "test",
                  Collections.emptyList(),
                  Blocks.toFunctionBlock(
                      Expressions.call(
                          Expressions.field(null, BigInteger.class, "ONE"),
                          "add",
                          Expressions.call(null,
                              Types.lookupMethod(BigInteger.class, "valueOf",
                                  long.class),
                              Expressions.constant(42L))))))),
      equalTo("{\n"
          + "  return new Runnable(){\n"
          + "      int test() {\n"
          + "        return "
          + "$L4J$C$java_math_BigInteger_ONE_add_java_math_BigInteger_valueOf_42L_;\n"
          + "      }\n"
          + "\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$java_math_BigInteger_valueOf_42L_ = java.math.BigInteger"
          + ".valueOf(42L);\n"
          + "      static final java.math.BigInteger "
          + "$L4J$C$java_math_BigInteger_ONE_add_java_math_BigInteger_valueOf_42L_ = java.math.BigInteger.ONE.add($L4J$C$java_math_BigInteger_valueOf_42L_);\n"
          + "    };\n"
          + "}\n"));
}
 
Example #28
Source File: FileTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
  PhysType physType =
      PhysTypeImpl.of(
          implementor.getTypeFactory(),
          getRowType(),
          pref.preferArray());

  return implementor.result(
      physType,
      Blocks.toBlock(
          Expressions.call(table.getExpression(FileTable.class), "project",
              Expressions.constant(fields))));
}
 
Example #29
Source File: OLAPTableScan.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

    context.setReturnTupleInfo(rowType, columnRowType);
    String execFunction = genExecFunc();

    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.ARRAY);
    MethodCallExpression exprCall = Expressions.call(table.getExpression(OLAPTable.class), execFunction,
            implementor.getRootExpression(), Expressions.constant(context.id));
    return implementor.result(physType, Blocks.toBlock(exprCall));
}
 
Example #30
Source File: OLAPJoinRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {

    context.setReturnTupleInfo(rowType, columnRowType);

    PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), pref.preferArray());
    RelOptTable factTable = context.firstTableScan.getTable();
    MethodCallExpression exprCall = Expressions.call(factTable.getExpression(OLAPTable.class), "executeOLAPQuery",
            implementor.getRootExpression(), Expressions.constant(context.id));
    return implementor.result(physType, Blocks.toBlock(exprCall));
}