Java Code Examples for org.apache.calcite.linq4j.Linq4j#getMethod()

The following examples show how to use org.apache.calcite.linq4j.Linq4j#getMethod() . 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: OptimizerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test 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"));
}
 
Example 2
Source File: OptimizerTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test 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"));
}