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

The following examples show how to use org.apache.calcite.linq4j.tree.Expressions#statement() . 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: InlinerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testInlineInTryCatchStatement() {
  final BlockBuilder builder = new BlockBuilder(true);
  final ParameterExpression t = Expressions.parameter(int.class, "t");
  builder.add(Expressions.declare(Modifier.FINAL, t, ONE));
  final ParameterExpression u = Expressions.parameter(int.class, "u");
  builder.add(Expressions.declare(Modifier.FINAL, u, null));
  Statement st = Expressions.statement(
      Expressions.assign(u,
          Expressions.makeBinary(ExpressionType.Add, t, TWO)));
  ParameterExpression e = Expressions.parameter(0, Exception.class, "e");
  CatchBlock cb = Expressions.catch_(e, Expressions.throw_(e));
  builder.add(Expressions.tryCatch(st, cb));
  builder.add(Expressions.return_(null, u));
  assertEquals(
      "{\n"
          + "  final int u;\n"
          + "  try {\n"
          + "    u = 1 + 2;\n"
          + "  } catch (Exception e) {\n"
          + "    throw e;\n"
          + "  }\n"
          + "  return u;\n"
          + "}\n",
      builder.toBlock().toString());
}
 
Example 2
Source File: ExpressionCompiler.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * Create quasi-Java expression from given {@link RexNode}
 *
 * @param node Expression in the form of {@link RexNode}
 * @param inputRowType Input Data type to expression in the form of {@link RelDataType}
 * @param outputRowType Output data type of expression in the form of {@link RelDataType}
 *
 * @return Returns quasi-Java expression
 */
public String getExpression(RexNode node, RelDataType inputRowType, RelDataType outputRowType)
{
  final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
  programBuilder.addProject(node, null);
  final RexProgram program = programBuilder.getProgram();

  final BlockBuilder builder = new BlockBuilder();
  final JavaTypeFactory javaTypeFactory = (JavaTypeFactory)rexBuilder.getTypeFactory();

  final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList
      .of(Pair.<Expression, PhysType>of(Expressions.variable(Object[].class, "inputValues"),
      PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
  final Function1<String, RexToLixTranslator.InputGetter> correlates =
      new Function1<String, RexToLixTranslator.InputGetter>()
    {
      public RexToLixTranslator.InputGetter apply(String a0)
      {
        throw new UnsupportedOperationException();
      }
    };

  final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder,
      PhysTypeImpl.of(javaTypeFactory, outputRowType, JavaRowFormat.ARRAY, false), null, inputGetter, correlates);

  for (int i = 0; i < list.size(); i++) {
    Statement statement = Expressions.statement(list.get(i));
    builder.add(statement);
  }

  return finalizeExpression(builder.toBlock(), inputRowType);
}
 
Example 3
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Handle checked Exceptions declared in Method. In such case,
 * method call should be wrapped in a try...catch block.
 * "
 *      final Type method_call;
 *      try {
 *        method_call = callExpr
 *      } catch (Exception e) {
 *        throw new RuntimeException(e);
 *      }
 * "
 */
Expression handleMethodCheckedExceptions(Expression callExpr) {
  // Try statement
  ParameterExpression methodCall = Expressions.parameter(
      callExpr.getType(), list.newName("method_call"));
  list.add(Expressions.declare(Modifier.FINAL, methodCall, null));
  Statement st = Expressions.statement(Expressions.assign(methodCall, callExpr));
  // Catch Block, wrap checked exception in unchecked exception
  ParameterExpression e = Expressions.parameter(0, Exception.class, "e");
  Expression uncheckedException = Expressions.new_(RuntimeException.class, e);
  CatchBlock cb = Expressions.catch_(e, Expressions.throw_(uncheckedException));
  list.add(Expressions.tryCatch(st, cb));
  return methodCall;
}