public BlockStatement compileToBlock(final RexProgram program) { final ParameterExpression context_ = Expressions.parameter(Context.class, "context"); final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues"); return compileToBlock(program, context_, outputValues_).toBlock(); }
public String compile(final RexProgram program, String className) { final ParameterExpression context_ = Expressions.parameter(Context.class, "context"); final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues"); BlockBuilder builder = compileToBlock(program, context_, outputValues_); return baz(context_, outputValues_, builder.toBlock(), className); }
Expressions.statement( Expressions.call( Expressions.parameter(ExecutableExpression.class, "this"), StormBuiltInMethod.EXPR_EXECUTE2.method, context, values_))); builder.add(
public BlockStatement compileToBlock(final RexProgram program) { final ParameterExpression context_ = Expressions.parameter(Context.class, "context"); final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues"); return compileToBlock(program, context_, outputValues_).toBlock(); }
public String compile(final RexProgram program, String className) { final ParameterExpression context_ = Expressions.parameter(Context.class, "context"); final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues"); BlockBuilder builder = compileToBlock(program, context_, outputValues_); return baz(context_, outputValues_, builder.toBlock(), className); }
/** * Creates an expression that declares and initializes a variable. No * type is required; it is assumed that the variable is the same type as * the initializer. You can retrieve the {@link ParameterExpression} from * the {@link DeclarationStatement#parameter} field of the result. */ public static DeclarationStatement declare(int modifiers, String name, Expression initializer) { assert initializer != null : "empty initializer for variable declaration with name '" + name + "', modifiers " + modifiers + ". Please use declare(int, ParameterExpression, initializer) instead"; return declare(modifiers, parameter(initializer.getType(), name), initializer); }
/** * Creates final static field to hold the given expression. * The method might reuse existing declarations if appropriate. * * @param expression expression to store in final field * @return expression for the given input expression */ protected Expression createField(Expression expression) { ParameterExpression pe = findDeclaredExpression(expression); if (pe != null) { return pe; } String name = inventFieldName(expression); pe = Expressions.parameter(expression.getType(), name); FieldDeclaration decl = Expressions.fieldDecl(Modifier.FINAL | Modifier.STATIC, pe, expression); dedup.put(expression, pe); addedDeclarations.add(decl); constants.put(pe, true); fieldsByName.put(name, pe); return pe; }
@Override public CallImplementor getImplementor() { return RexImpTable.createImplementor((translator, call, translatedOperands) -> { final Expression context = Expressions.parameter(SamzaSqlExecutionContext.class, "context"); final Expression getUdfInstance = Expressions.call(ScalarUdf.class, context, getUdfMethod, Expressions.constant(udfMethod.getDeclaringClass().getName()), Expressions.constant(udfName)); final Expression callExpression = Expressions.convert_(Expressions.call(Expressions.convert_(getUdfInstance, udfMethod.getDeclaringClass()), udfMethod, translatedOperands), Object.class); // The Janino compiler which is used to compile the expressions doesn't seem to understand the Type of the ScalarUdf.execute // because it is a generic. To work around that we are explicitly casting it to the return type. return Expressions.convert_(callExpression, udfMethod.getReturnType()); }, NullPolicy.NONE, false); }
@Override public CallImplementor getImplementor() { return RexImpTable.createImplementor((translator, call, translatedOperands) -> { final Expression context = Expressions.parameter(SamzaSqlExecutionContext.class, "context"); final Expression getUdfInstance = Expressions.call(ScalarUdf.class, context, getUdfMethod, Expressions.constant(udfMethod.getDeclaringClass().getName()), Expressions.constant(udfName)); final Expression callExpression = Expressions.convert_(Expressions.call(Expressions.convert_(getUdfInstance, udfMethod.getDeclaringClass()), udfMethod, translatedOperands), Object.class); // The Janino compiler which is used to compile the expressions doesn't seem to understand the Type of the ScalarUdf.execute // because it is a generic. To work around that we are explicitly casting it to the return type. return Expressions.convert_(callExpression, udfMethod.getReturnType()); }, NullPolicy.NONE, false); }
public Expression convertTo(Expression exp, PhysType targetPhysType) { final JavaRowFormat targetFormat = targetPhysType.getFormat(); if (format == targetFormat) { return exp; } final ParameterExpression o_ = Expressions.parameter(javaRowClass, "o"); final int fieldCount = rowType.getFieldCount(); return Expressions.call(exp, BuiltInMethod.SELECT.method, generateSelector(o_, Util.range(fieldCount), targetFormat)); }
public Expression convertTo(Expression exp, PhysType targetPhysType) { final JavaRowFormat targetFormat = targetPhysType.getFormat(); if (format == targetFormat) { return exp; } final ParameterExpression o_ = Expressions.parameter(javaRowClass, "o"); final int fieldCount = rowType.getFieldCount(); return Expressions.call(exp, BuiltInMethod.SELECT.method, generateSelector(o_, Util.range(fieldCount), targetFormat)); }
private Expression toRows(PhysType physType, Expression expression) { if (physType.getFormat() == JavaRowFormat.SCALAR && Object[].class.isAssignableFrom(elementType) && getRowType().getFieldCount() == 1 && (table.unwrap(ScannableTable.class) != null || table.unwrap(FilterableTable.class) != null || table.unwrap(ProjectableFilterableTable.class) != null)) { return Expressions.call(BuiltInMethod.SLICE0.method, expression); } JavaRowFormat oldFormat = format(); if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) { return expression; } final ParameterExpression row_ = Expressions.parameter(elementType, "row"); final int fieldCount = table.getRowType().getFieldCount(); List<Expression> expressionList = new ArrayList<>(fieldCount); for (int i = 0; i < fieldCount; i++) { expressionList.add(fieldExpression(row_, i, physType, oldFormat)); } return Expressions.call(expression, BuiltInMethod.SELECT.method, Expressions.lambda(Function1.class, physType.record(expressionList), row_)); }
private Expression toRows(PhysType physType, Expression expression) { if (physType.getFormat() == JavaRowFormat.SCALAR && Object[].class.isAssignableFrom(elementType) && getRowType().getFieldCount() == 1 && (table.unwrap(ScannableTable.class) != null || table.unwrap(FilterableTable.class) != null || table.unwrap(ProjectableFilterableTable.class) != null)) { return Expressions.call(BuiltInMethod.SLICE0.method, expression); } JavaRowFormat oldFormat = format(); if (physType.getFormat() == oldFormat && !hasCollectionField(rowType)) { return expression; } final ParameterExpression row_ = Expressions.parameter(elementType, "row"); final int fieldCount = table.getRowType().getFieldCount(); List<Expression> expressionList = new ArrayList<>(fieldCount); for (int i = 0; i < fieldCount; i++) { expressionList.add(fieldExpression(row_, i, physType, oldFormat)); } return Expressions.call(expression, BuiltInMethod.SELECT.method, Expressions.lambda(Function1.class, physType.record(expressionList), row_)); }
Expression predicate(EnumerableRelImplementor implementor, BlockBuilder builder, PhysType leftPhysType, PhysType rightPhysType, RexNode condition) { 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(), getCluster().getRexBuilder()); program.addCondition(condition); builder.add( Expressions.return_(null, RexToLixTranslator.translateCondition(program.getProgram(), implementor.getTypeFactory(), builder, new RexToLixTranslator.InputGetterImpl( ImmutableList.of(Pair.of((Expression) left_, leftPhysType), Pair.of((Expression) right_, rightPhysType))), implementor.allCorrelateVariables, implementor.getConformance()))); return Expressions.lambda(Predicate2.class, builder.toBlock(), left_, right_); } }
@Test public void testQueryProviderSingleColumn() throws Exception { Connection connection = CalciteAssert .that(CalciteAssert.Config.REGULAR).connect(); QueryProvider queryProvider = connection.unwrap(QueryProvider.class); ParameterExpression e = Expressions.parameter(Employee.class, "e"); // "Enumerable<T> asEnumerable(final T[] ts)" List<Integer> list = queryProvider.createQuery( Expressions.call( Expressions.call( Types.of(Enumerable.class, Employee.class), null, LINQ4J_AS_ENUMERABLE_METHOD, Expressions.constant(new JdbcTest.HrSchema().emps)), "asQueryable"), Employee.class) .select( Expressions.<Function1<Employee, Integer>>lambda( Expressions.field(e, "empid"), e)) .toList(); assertEquals(Arrays.asList(100, 200, 150, 110), list); }
@Test public void testQueryProviderSingleColumn() throws Exception { Connection connection = CalciteAssert .that(CalciteAssert.Config.REGULAR).connect(); QueryProvider queryProvider = connection.unwrap(QueryProvider.class); ParameterExpression e = Expressions.parameter(Employee.class, "e"); // "Enumerable<T> asEnumerable(final T[] ts)" List<Integer> list = queryProvider.createQuery( Expressions.call( Expressions.call( Types.of(Enumerable.class, Employee.class), null, LINQ4J_AS_ENUMERABLE_METHOD, Expressions.constant(new JdbcTest.HrSchema().emps)), "asQueryable"), Employee.class) .select( Expressions.<Function1<Employee, Integer>>lambda( Expressions.field(e, "empid"), e)) .toList(); assertEquals(Arrays.asList(100, 200, 150, 110), list); }
public Expression implementResult(AggContext info, AggResultContext result) { WinAggResultContext winResult = (WinAggResultContext) result; List<RexNode> rexArgs = winResult.rexArguments(); ParameterExpression res = Expressions.parameter(0, info.returnType(), result.currentBlock().newName("nth")); RexToLixTranslator currentRowTranslator = winResult.rowTranslator( winResult.computeIndex(Expressions.constant(0), SeekType.START)); Expression dstIndex = winResult.computeIndex( Expressions.subtract( currentRowTranslator.translate(rexArgs.get(1), int.class), Expressions.constant(1)), SeekType.START); Expression rowInRange = winResult.rowInPartition(dstIndex); BlockBuilder thenBlock = result.nestBlock(); Expression nthValue = winResult.rowTranslator(dstIndex) .translate(rexArgs.get(0), res.type); thenBlock.add(Expressions.statement(Expressions.assign(res, nthValue))); result.exitBlock(); BlockStatement thenBranch = thenBlock.toBlock(); Expression defaultValue = getDefaultValue(res.type); result.currentBlock().add(Expressions.declare(0, res, null)); result.currentBlock().add( Expressions.ifThenElse(rowInRange, thenBranch, Expressions.statement(Expressions.assign(res, defaultValue)))); return res; } }
public Expression implementResult(AggContext info, AggResultContext result) { WinAggResultContext winResult = (WinAggResultContext) result; List<RexNode> rexArgs = winResult.rexArguments(); ParameterExpression res = Expressions.parameter(0, info.returnType(), result.currentBlock().newName("nth")); RexToLixTranslator currentRowTranslator = winResult.rowTranslator( winResult.computeIndex(Expressions.constant(0), SeekType.START)); Expression dstIndex = winResult.computeIndex( Expressions.subtract( currentRowTranslator.translate(rexArgs.get(1), int.class), Expressions.constant(1)), SeekType.START); Expression rowInRange = winResult.rowInPartition(dstIndex); BlockBuilder thenBlock = result.nestBlock(); Expression nthValue = winResult.rowTranslator(dstIndex) .translate(rexArgs.get(0), res.type); thenBlock.add(Expressions.statement(Expressions.assign(res, nthValue))); result.exitBlock(); BlockStatement thenBranch = thenBlock.toBlock(); Expression defaultValue = getDefaultValue(res.type); result.currentBlock().add(Expressions.declare(0, res, null)); result.currentBlock().add( Expressions.ifThenElse(rowInRange, thenBranch, Expressions.statement(Expressions.assign(res, defaultValue)))); return res; } }
@Test public void testTableWhere() throws SQLException, ClassNotFoundException { final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect(); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); ParameterExpression c = Expressions.parameter(JdbcTest.Customer.class, "c"); String s = Schemas.queryable(Schemas.createDataContext(connection, rootSchema), rootSchema.getSubSchema("foodmart"), JdbcTest.Customer.class, "customer") .where( Expressions.lambda( Expressions.lessThan( Expressions.field(c, "customer_id"), Expressions.constant(5)), c)) .toList() .toString(); Util.discard(s); } }
@Test public void testTableWhere() throws SQLException, ClassNotFoundException { final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect(); final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); ParameterExpression c = Expressions.parameter(JdbcTest.Customer.class, "c"); String s = Schemas.queryable(Schemas.createDataContext(connection, rootSchema), rootSchema.getSubSchema("foodmart"), JdbcTest.Customer.class, "customer") .where( Expressions.lambda( Expressions.lessThan( Expressions.field(c, "customer_id"), Expressions.constant(5)), c)) .toList() .toString(); Util.discard(s); } }