Java Code Examples for org.apache.calcite.linq4j.tree.Expressions#lambda()

The following examples show how to use org.apache.calcite.linq4j.tree.Expressions#lambda() . 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: TypeFinderTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testFunctionExpression2() {
  FunctionExpression expr = Expressions.lambda(Function1.class,
      Expressions.block(
          Expressions.return_(null, Expressions.constant(1L, Long.class))),
      Expressions.parameter(String.class, "input"));
  assertJavaCodeContains("new org.apache.calcite.linq4j.function.Function1() {\n"
      + "  public Long apply(String input) {\n"
      + "    return Long.valueOf(1L);\n"
      + "  }\n"
      + "  public Object apply(Object input) {\n"
      + "    return apply(\n"
      + "      (String) input);\n"
      + "  }\n"
      + "}\n", expr);
  assertTypeContains(Arrays.asList(String.class, Long.class), expr);
}
 
Example 2
Source File: TypeFinderTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testFunctionExpression1() {
  ParameterExpression param = Expressions.parameter(String.class, "input");
  FunctionExpression expr = Expressions.lambda(Function1.class,
      Expressions.block(
          Expressions.return_(null, param)),
      param);
  assertJavaCodeContains("new org.apache.calcite.linq4j.function.Function1() {\n"
      + "  public String apply(String input) {\n"
      + "    return input;\n"
      + "  }\n"
      + "  public Object apply(Object input) {\n"
      + "    return apply(\n"
      + "      (String) input);\n"
      + "  }\n"
      + "}\n", expr);
  assertTypeContains(String.class, expr);
}
 
Example 3
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Expression generateSelector(
    ParameterExpression parameter,
    List<Integer> fields,
    JavaRowFormat targetFormat) {
  // Optimize target format
  switch (fields.size()) {
  case 0:
    targetFormat = JavaRowFormat.LIST;
    break;
  case 1:
    targetFormat = JavaRowFormat.SCALAR;
    break;
  }
  final PhysType targetPhysType =
      project(fields, targetFormat);
  switch (format) {
  case SCALAR:
    return Expressions.call(BuiltInMethod.IDENTITY_SELECTOR.method);
  default:
    return Expressions.lambda(Function1.class,
        targetPhysType.record(fieldReferences(parameter, fields)), parameter);
  }
}
 
Example 4
Source File: ExpressionTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testLambdaCallsTwoArgMethod() throws NoSuchMethodException {
  // A parameter for the lambda expression.
  ParameterExpression paramS =
      Expressions.parameter(String.class, "s");
  ParameterExpression paramBegin =
      Expressions.parameter(Integer.TYPE, "begin");
  ParameterExpression paramEnd =
      Expressions.parameter(Integer.TYPE, "end");

  // This expression represents a lambda expression
  // that adds 1 to the parameter value.
  FunctionExpression lambdaExpr =
      Expressions.lambda(
          Expressions.call(
              paramS,
              String.class.getMethod(
                  "substring", Integer.TYPE, Integer.TYPE),
              paramBegin,
              paramEnd), paramS, paramBegin, paramEnd);

  // Compile and run the lambda expression.
  String s =
      (String) lambdaExpr.compile().dynamicInvoke("hello world", 3, 7);

  assertEquals("lo w", s);
}
 
Example 5
Source File: ExpressionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLambdaPrimitiveTwoArgs() {
  // Parameters for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(int.class, "key");
  ParameterExpression param2Expr =
      Expressions.parameter(int.class, "key2");

  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.block(
          (Type) null,
          Expressions.return_(
              null, paramExpr)),
      Arrays.asList(paramExpr, param2Expr));

  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals("new org.apache.calcite.linq4j.function.Function2() {\n"
          + "  public int apply(int key, int key2) {\n"
          + "    return key;\n"
          + "  }\n"
          + "  public Integer apply(Integer key, Integer key2) {\n"
          + "    return apply(\n"
          + "      key.intValue(),\n"
          + "      key2.intValue());\n"
          + "  }\n"
          + "  public Integer apply(Object key, Object key2) {\n"
          + "    return apply(\n"
          + "      (Integer) key,\n"
          + "      (Integer) key2);\n"
          + "  }\n"
          + "}\n",
      s);
}
 
Example 6
Source File: EnumUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns a predicate expression based on a join condition. **/
static Expression generatePredicate(
    EnumerableRelImplementor implementor,
    RexBuilder rexBuilder,
    RelNode left,
    RelNode right,
    PhysType leftPhysType,
    PhysType rightPhysType,
    RexNode condition) {
  final BlockBuilder builder = new BlockBuilder();
  final ParameterExpression left_ =
      Expressions.parameter(leftPhysType.getJavaRowType(), "left");
  final ParameterExpression right_ =
      Expressions.parameter(rightPhysType.getJavaRowType(), "right");
  final RexProgramBuilder program =
      new RexProgramBuilder(
          implementor.getTypeFactory().builder()
              .addAll(left.getRowType().getFieldList())
              .addAll(right.getRowType().getFieldList())
              .build(),
          rexBuilder);
  program.addCondition(condition);
  builder.add(
      Expressions.return_(null,
          RexToLixTranslator.translateCondition(program.getProgram(),
              implementor.getTypeFactory(),
              builder,
              new RexToLixTranslator.InputGetterImpl(
                  ImmutableList.of(Pair.of(left_, leftPhysType),
                      Pair.of(right_, rightPhysType))),
              implementor.allCorrelateVariables,
              implementor.getConformance())));
  return Expressions.lambda(Predicate2.class, builder.toBlock(), left_, right_);
}
 
Example 7
Source File: PhysTypeImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Expression convertTo(Expression exp, JavaRowFormat targetFormat) {
  if (format == targetFormat) {
    return exp;
  }
  final ParameterExpression o_ =
      Expressions.parameter(javaRowClass, "o");
  final int fieldCount = rowType.getFieldCount();
  // The conversion must be strict so optimizations of the targetFormat should not be performed
  // by the code that follows. If necessary the target format can be optimized before calling
  // this method.
  PhysType targetPhysType = PhysTypeImpl.of(typeFactory, rowType, targetFormat, false);
  final Expression selector = Expressions.lambda(Function1.class,
      targetPhysType.record(fieldReferences(o_, Util.range(fieldCount))), o_);
  return Expressions.call(exp, BuiltInMethod.SELECT.method, selector);
}
 
Example 8
Source File: ExpressionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLambdaCallsBinaryOpMixDoubleType() {
  // A parameter for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(Double.TYPE, "arg");

  // This expression represents a lambda expression
  // that adds 10.1d to the parameter value.
  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.add(
          paramExpr,
          Expressions.constant(10.1d)),
      Arrays.asList(paramExpr));
  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals(
      "new org.apache.calcite.linq4j.function.Function1() {\n"
          + "  public double apply(double arg) {\n"
          + "    return arg + 10.1D;\n"
          + "  }\n"
          + "  public Object apply(Double arg) {\n"
          + "    return apply(\n"
          + "      arg.doubleValue());\n"
          + "  }\n"
          + "  public Object apply(Object arg) {\n"
          + "    return apply(\n"
          + "      (Double) arg);\n"
          + "  }\n"
          + "}\n",
      s);

  // Compile and run the lambda expression.
  // The value of the parameter is 5.0f.
  double n = (Double) lambdaExpr.compile().dynamicInvoke(5.0f);

  // This code example produces the following output:
  //
  // arg => (arg +10.1d)
  // 15.1d
  assertEquals(15.1d, n, 0d);
}
 
Example 9
Source File: ExpressionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLambdaCallsBinaryOpMixType() {
  // A parameter for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(Long.TYPE, "arg");

  // This expression represents a lambda expression
  // that adds (int)10 to the parameter value.
  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.add(
          paramExpr,
          Expressions.constant(10)),
      Arrays.asList(paramExpr));
  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals(
      "new org.apache.calcite.linq4j.function.Function1() {\n"
          + "  public long apply(long arg) {\n"
          + "    return arg + 10;\n"
          + "  }\n"
          + "  public Object apply(Long arg) {\n"
          + "    return apply(\n"
          + "      arg.longValue());\n"
          + "  }\n"
          + "  public Object apply(Object arg) {\n"
          + "    return apply(\n"
          + "      (Long) arg);\n"
          + "  }\n"
          + "}\n",
      s);

  // Compile and run the lambda expression.
  // The value of the parameter is 5L.
  long n = (Long) lambdaExpr.compile().dynamicInvoke(5L);

  // This code example produces the following output:
  //
  // arg => (arg +10)
  // 15
  assertEquals(15L, n, 0d);
}
 
Example 10
Source File: ExpressionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLambdaCallsBinaryOpFloat() {
  // A parameter for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(Float.TYPE, "arg");

  // This expression represents a lambda expression
  // that adds 1f to the parameter value.
  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.add(
          paramExpr,
          Expressions.constant(2.0f)),
      Arrays.asList(paramExpr));
  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals(
      "new org.apache.calcite.linq4j.function.Function1() {\n"
          + "  public float apply(float arg) {\n"
          + "    return arg + 2.0F;\n"
          + "  }\n"
          + "  public Object apply(Float arg) {\n"
          + "    return apply(\n"
          + "      arg.floatValue());\n"
          + "  }\n"
          + "  public Object apply(Object arg) {\n"
          + "    return apply(\n"
          + "      (Float) arg);\n"
          + "  }\n"
          + "}\n",
      s);

  // Compile and run the lambda expression.
  // The value of the parameter is 1f
  float n = (Float) lambdaExpr.compile().dynamicInvoke(1f);

  // This code example produces the following output:
  //
  // arg => (arg +2)
  // 3.0
  assertEquals(3.0f, n, 0f);
}
 
Example 11
Source File: ExpressionTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLambdaCallsBinaryOpLong() {
  // A parameter for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(Long.TYPE, "arg");

  // This expression represents a lambda expression
  // that adds 1L to the parameter value.
  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.add(
          paramExpr,
          Expressions.constant(2L)),
      Arrays.asList(paramExpr));
  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals(
      "new org.apache.calcite.linq4j.function.Function1() {\n"
          + "  public long apply(long arg) {\n"
          + "    return arg + 2L;\n"
          + "  }\n"
          + "  public Object apply(Long arg) {\n"
          + "    return apply(\n"
          + "      arg.longValue());\n"
          + "  }\n"
          + "  public Object apply(Object arg) {\n"
          + "    return apply(\n"
          + "      (Long) arg);\n"
          + "  }\n"
          + "}\n",
      s);

  // Compile and run the lambda expression.
  // The value of the parameter is 1L.
  long n = (Long) lambdaExpr.compile().dynamicInvoke(1L);

  // This code example produces the following output:
  //
  // arg => (arg +2)
  // 3
  assertEquals(3L, n, 0d);
}
 
Example 12
Source File: ExpressionTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testLambdaCallsBinaryOpInt() {
  // A parameter for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(Integer.TYPE, "arg");

  // This expression represents a lambda expression
  // that adds 1 to the parameter value.
  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.add(
          paramExpr,
          Expressions.constant(2)),
      Arrays.asList(paramExpr));

  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals(
      "new org.apache.calcite.linq4j.function.Function1() {\n"
          + "  public int apply(int arg) {\n"
          + "    return arg + 2;\n"
          + "  }\n"
          + "  public Object apply(Integer arg) {\n"
          + "    return apply(\n"
          + "      arg.intValue());\n"
          + "  }\n"
          + "  public Object apply(Object arg) {\n"
          + "    return apply(\n"
          + "      (Integer) arg);\n"
          + "  }\n"
          + "}\n",
      s);

  // Compile and run the lambda expression.
  // The value of the parameter is 1
  Integer n = (Integer) lambdaExpr.compile().dynamicInvoke(1);

  // This code example produces the following output:
  //
  // arg => (arg +2)
  // 3
  assertEquals(3, n, 0);
}
 
Example 13
Source File: EnumUtils.java    From calcite with Apache License 2.0 4 votes vote down vote up
static Expression joinSelector(JoinRelType joinType, PhysType physType,
    List<PhysType> inputPhysTypes) {
  // A parameter for each input.
  final List<ParameterExpression> parameters = new ArrayList<>();

  // Generate all fields.
  final List<Expression> expressions = new ArrayList<>();
  final int outputFieldCount = physType.getRowType().getFieldCount();
  for (Ord<PhysType> ord : Ord.zip(inputPhysTypes)) {
    final PhysType inputPhysType =
        ord.e.makeNullable(joinType.generatesNullsOn(ord.i));
    // If input item is just a primitive, we do not generate specialized
    // primitive apply override since it won't be called anyway
    // Function<T> always operates on boxed arguments
    final ParameterExpression parameter =
        Expressions.parameter(Primitive.box(inputPhysType.getJavaRowType()),
            EnumUtils.LEFT_RIGHT.get(ord.i));
    parameters.add(parameter);
    if (expressions.size() == outputFieldCount) {
      // For instance, if semi-join needs to return just the left inputs
      break;
    }
    final int fieldCount = inputPhysType.getRowType().getFieldCount();
    for (int i = 0; i < fieldCount; i++) {
      Expression expression =
          inputPhysType.fieldReference(parameter, i,
              physType.getJavaFieldType(expressions.size()));
      if (joinType.generatesNullsOn(ord.i)) {
        expression =
            Expressions.condition(
                Expressions.equal(parameter, Expressions.constant(null)),
                Expressions.constant(null),
                expression);
      }
      expressions.add(expression);
    }
  }
  return Expressions.lambda(
      Function2.class,
      physType.record(expressions),
      parameters);
}
 
Example 14
Source File: ExpressionTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testLambdaCallsBinaryOpDouble() {
  // A parameter for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(Double.TYPE, "arg");

  // This expression represents a lambda expression
  // that adds 1 to the parameter value.
  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.add(
          paramExpr,
          Expressions.constant(2d)),
      Arrays.asList(paramExpr));

  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals(
      "new org.apache.calcite.linq4j.function.Function1() {\n"
          + "  public double apply(double arg) {\n"
          + "    return arg + 2.0D;\n"
          + "  }\n"
          + "  public Object apply(Double arg) {\n"
          + "    return apply(\n"
          + "      arg.doubleValue());\n"
          + "  }\n"
          + "  public Object apply(Object arg) {\n"
          + "    return apply(\n"
          + "      (Double) arg);\n"
          + "  }\n"
          + "}\n",
      s);

  // Compile and run the lambda expression.
  // The value of the parameter is 1.5.
  double n = (Double) lambdaExpr.compile().dynamicInvoke(1.5d);

  // This code example produces the following output:
  //
  // arg => (arg +2)
  // 3.5
  assertEquals(3.5D, n, 0d);
}
 
Example 15
Source File: EnumUtils.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a window selector which appends attribute of the window based on
 * the parameters.
 *
 * Note that it only works for batch scenario. E.g. all data is known and there is no late data.
 */
static Expression tumblingWindowSelector(
    PhysType inputPhysType,
    PhysType outputPhysType,
    Expression wmColExpr,
    Expression intervalExpr) {
  // Generate all fields.
  final List<Expression> expressions = new ArrayList<>();
  // If input item is just a primitive, we do not generate specialized
  // primitive apply override since it won't be called anyway
  // Function<T> always operates on boxed arguments
  final ParameterExpression parameter =
      Expressions.parameter(Primitive.box(inputPhysType.getJavaRowType()), "_input");
  final int fieldCount = inputPhysType.getRowType().getFieldCount();
  for (int i = 0; i < fieldCount; i++) {
    Expression expression =
        inputPhysType.fieldReference(parameter, i,
            outputPhysType.getJavaFieldType(expressions.size()));
    expressions.add(expression);
  }
  final Expression wmColExprToLong = EnumUtils.convert(wmColExpr, long.class);
  final Expression shiftExpr = Expressions.constant(1, long.class);

  // Find the fixed window for a timestamp given a window size and return the window start.
  // Fixed windows counts from timestamp 0.
  // wmMillis / intervalMillis * intervalMillis
  expressions.add(
      Expressions.multiply(
          Expressions.divide(
              wmColExprToLong,
              intervalExpr),
          intervalExpr));

  // Find the fixed window for a timestamp given a window size and return the window end.
  // Fixed windows counts from timestamp 0.

  // (wmMillis / sizeMillis + 1L) * sizeMillis;
  expressions.add(
      Expressions.multiply(
          Expressions.add(
              Expressions.divide(
                  wmColExprToLong,
                  intervalExpr),
              shiftExpr),
          intervalExpr));

  return Expressions.lambda(
      Function1.class,
      outputPhysType.record(expressions),
      parameter);
}
 
Example 16
Source File: ExpressionTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testLambdaCallsBinaryOpByte() {
  // A parameter for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(Byte.TYPE, "arg");

  // This expression represents a lambda expression
  // that adds 1 to the parameter value.
  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.add(
          paramExpr,
          Expressions.constant(Byte.valueOf("2"))),
      Arrays.asList(paramExpr));

  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals(
      "new org.apache.calcite.linq4j.function.Function1() {\n"
          + "  public int apply(byte arg) {\n"
          + "    return arg + (byte)2;\n"
          + "  }\n"
          + "  public Object apply(Byte arg) {\n"
          + "    return apply(\n"
          + "      arg.byteValue());\n"
          + "  }\n"
          + "  public Object apply(Object arg) {\n"
          + "    return apply(\n"
          + "      (Byte) arg);\n"
          + "  }\n"
          + "}\n",
      s);

  // Compile and run the lambda expression.
  // The value of the parameter is 1.
  Integer n = (Integer) lambdaExpr.compile().dynamicInvoke(Byte.valueOf("1"));

  // This code example produces the following output:
  //
  // arg => (arg +2)
  // 3
  assertEquals(3, n, 0);
}
 
Example 17
Source File: ExpressionTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testLambdaCallsBinaryOpShort() {
  // A parameter for the lambda expression.
  ParameterExpression paramExpr =
      Expressions.parameter(Short.TYPE, "arg");

  // This expression represents a lambda expression
  // that adds 1 to the parameter value.
  Short a = 2;
  FunctionExpression lambdaExpr = Expressions.lambda(
      Expressions.add(
          paramExpr,
          Expressions.constant(a)),
      Arrays.asList(paramExpr));

  // Print out the expression.
  String s = Expressions.toString(lambdaExpr);
  assertEquals(
      "new org.apache.calcite.linq4j.function.Function1() {\n"
          + "  public int apply(short arg) {\n"
          + "    return arg + (short)2;\n"
          + "  }\n"
          + "  public Object apply(Short arg) {\n"
          + "    return apply(\n"
          + "      arg.shortValue());\n"
          + "  }\n"
          + "  public Object apply(Object arg) {\n"
          + "    return apply(\n"
          + "      (Short) arg);\n"
          + "  }\n"
          + "}\n",
      s);

  // Compile and run the lambda expression.
  // The value of the parameter is 1.
  Short b = 1;
  Integer n = (Integer) lambdaExpr.compile().dynamicInvoke(b);

  // This code example produces the following output:
  //
  // arg => (arg +2)
  // 3
  assertEquals(3, n, 0);
}