org.apache.calcite.adapter.enumerable.RexToLixTranslator Java Examples
The following examples show how to use
org.apache.calcite.adapter.enumerable.RexToLixTranslator.
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: TableFunctionImpl.java From calcite with Apache License 2.0 | 6 votes |
private static CallImplementor createImplementor(final Method method) { return RexImpTable.createImplementor( new ReflectiveCallNotNullImplementor(method) { public Expression implement(RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { Expression expr = super.implement(translator, call, translatedOperands); final Class<?> returnType = method.getReturnType(); if (QueryableTable.class.isAssignableFrom(returnType)) { Expression queryable = Expressions.call( Expressions.convert_(expr, QueryableTable.class), BuiltInMethod.QUERYABLE_TABLE_AS_QUERYABLE.method, Expressions.call(DataContext.ROOT, BuiltInMethod.DATA_CONTEXT_GET_QUERY_PROVIDER.method), Expressions.constant(null, SchemaPlus.class), Expressions.constant(call.getOperator().getName(), String.class)); expr = Expressions.call(queryable, BuiltInMethod.QUERYABLE_AS_ENUMERABLE.method); } else { expr = Expressions.call(expr, BuiltInMethod.SCANNABLE_TABLE_SCAN.method, DataContext.ROOT); } return expr; } }, NullPolicy.NONE, false); }
Example #2
Source File: HiveRexExecutorImpl.java From marble with Apache License 2.0 | 6 votes |
public Expression field(BlockBuilder list, int index, Type storageType) { MethodCallExpression recFromCtx = Expressions.call( DataContext.ROOT, BuiltInMethod.DATA_CONTEXT_GET.method, Expressions.constant("inputRecord")); Expression recFromCtxCasted = RexToLixTranslator.convert(recFromCtx, Object[].class); IndexExpression recordAccess = Expressions.arrayIndex(recFromCtxCasted, Expressions.constant(index)); if (storageType == null) { final RelDataType fieldType = rowType.getFieldList().get(index).getType(); storageType = ((JavaTypeFactory) typeFactory).getJavaClass(fieldType); } return RexToLixTranslator.convert(recordAccess, storageType); }
Example #3
Source File: HiveRexExecutorImpl.java From marble with Apache License 2.0 | 5 votes |
private String compile(RexBuilder rexBuilder, List<RexNode> constExps, RexToLixTranslator.InputGetter getter, RelDataType rowType) { final RexProgramBuilder programBuilder = new RexProgramBuilder(rowType, rexBuilder); for (RexNode node : constExps) { programBuilder.addProject( node, "c" + programBuilder.getProjectList().size()); } final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem()); final BlockBuilder blockBuilder = new BlockBuilder(); final ParameterExpression root0_ = Expressions.parameter(Object.class, "root0"); final ParameterExpression root_ = DataContext.ROOT; blockBuilder.add( Expressions.declare( Modifier.FINAL, root_, Expressions.convert_(root0_, DataContext.class))); final SqlConformance conformance = SqlConformanceEnum.HIVE; final RexProgram program = programBuilder.getProgram(); final List<Expression> expressions = RexToLixTranslator.translateProjects(program, javaTypeFactory, conformance, blockBuilder, null, root_, getter, null); blockBuilder.add( Expressions.return_(null, Expressions.newArrayInit(Object[].class, expressions))); final MethodDeclaration methodDecl = Expressions.methodDecl(Modifier.PUBLIC, Object[].class, BuiltInMethod.FUNCTION1_APPLY.method.getName(), ImmutableList.of(root0_), blockBuilder.toBlock()); String code = Expressions.toString(methodDecl); if (CalcitePrepareImpl.DEBUG) { Util.debugCode(System.out, code); } return code; }
Example #4
Source File: RexExecutorImpl.java From calcite with Apache License 2.0 | 5 votes |
private static String compile(RexBuilder rexBuilder, List<RexNode> constExps, RexToLixTranslator.InputGetter getter, RelDataType rowType) { final RexProgramBuilder programBuilder = new RexProgramBuilder(rowType, rexBuilder); for (RexNode node : constExps) { programBuilder.addProject( node, "c" + programBuilder.getProjectList().size()); } final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem()); final BlockBuilder blockBuilder = new BlockBuilder(); final ParameterExpression root0_ = Expressions.parameter(Object.class, "root0"); final ParameterExpression root_ = DataContext.ROOT; blockBuilder.add( Expressions.declare( Modifier.FINAL, root_, Expressions.convert_(root0_, DataContext.class))); final SqlConformance conformance = SqlConformanceEnum.DEFAULT; final RexProgram program = programBuilder.getProgram(); final List<Expression> expressions = RexToLixTranslator.translateProjects(program, javaTypeFactory, conformance, blockBuilder, null, root_, getter, null); blockBuilder.add( Expressions.return_(null, Expressions.newArrayInit(Object[].class, expressions))); final MethodDeclaration methodDecl = Expressions.methodDecl(Modifier.PUBLIC, Object[].class, BuiltInMethod.FUNCTION1_APPLY.method.getName(), ImmutableList.of(root0_), blockBuilder.toBlock()); String code = Expressions.toString(methodDecl); if (CalciteSystemProperty.DEBUG.value()) { Util.debugCode(System.out, code); } return code; }
Example #5
Source File: MongoRules.java From calcite with Apache License 2.0 | 5 votes |
@Override public String visitLiteral(RexLiteral literal) { if (literal.getValue() == null) { return "null"; } return "{$literal: " + RexToLixTranslator.translateLiteral(literal, literal.getType(), typeFactory, RexImpTable.NullAs.NOT_POSSIBLE) + "}"; }
Example #6
Source File: ElasticsearchRules.java From calcite with Apache License 2.0 | 5 votes |
@Override public String visitLiteral(RexLiteral literal) { if (literal.getValue() == null) { return "null"; } return "\"literal\":\"" + RexToLixTranslator.translateLiteral(literal, literal.getType(), typeFactory, RexImpTable.NullAs.NOT_POSSIBLE) + "\""; }
Example #7
Source File: RexNodeToJavaCodeCompiler.java From streamline with Apache License 2.0 | 5 votes |
private BlockBuilder compileToBlock(final RexProgram program, ParameterExpression context_, ParameterExpression outputValues_) { RelDataType inputRowType = program.getInputRowType(); final BlockBuilder builder = new BlockBuilder(); final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem()); final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl( ImmutableList.of( Pair.<Expression, PhysType>of( Expressions.field(context_, BuiltInMethod.CONTEXT_VALUES.field), 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 Expression root = Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field); final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, null, root, inputGetter, correlates); for (int i = 0; i < list.size(); i++) { builder.add( Expressions.statement( Expressions.assign( Expressions.arrayIndex(outputValues_, Expressions.constant(i)), list.get(i)))); } return builder; }
Example #8
Source File: ExpressionCompiler.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * 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 #9
Source File: HiveRexExecutorImpl.java From marble with Apache License 2.0 | 5 votes |
/** * Creates an {@link RexExecutable} that allows to apply the * generated code during query processing (filter, projection). * * @param rexBuilder Rex builder * @param exps Expressions * @param rowType describes the structure of the input row. */ public RexExecutable getExecutable(RexBuilder rexBuilder, List<RexNode> exps, RelDataType rowType) { final JavaTypeFactoryImpl typeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem()); final RexToLixTranslator.InputGetter getter = new DataContextInputGetter(rowType, typeFactory); final String code = compile(rexBuilder, exps, getter, rowType); return new RexExecutable(code, "generated Rex code"); }
Example #10
Source File: SparkRules.java From calcite with Apache License 2.0 | 4 votes |
public Result implementSpark(Implementor implementor) { /* return Linq4j.asSpark( new Object[][] { new Object[] {1, 2}, new Object[] {3, 4} }); */ final JavaTypeFactory typeFactory = (JavaTypeFactory) getCluster().getTypeFactory(); final BlockBuilder builder = new BlockBuilder(); final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.CUSTOM); final Type rowClass = physType.getJavaRowType(); final List<Expression> expressions = new ArrayList<>(); final List<RelDataTypeField> fields = rowType.getFieldList(); for (List<RexLiteral> tuple : tuples) { final List<Expression> literals = new ArrayList<>(); for (Pair<RelDataTypeField, RexLiteral> pair : Pair.zip(fields, tuple)) { literals.add( RexToLixTranslator.translateLiteral( pair.right, pair.left.getType(), typeFactory, RexImpTable.NullAs.NULL)); } expressions.add(physType.record(literals)); } builder.add( Expressions.return_(null, Expressions.call(SparkMethod.ARRAY_TO_RDD.method, Expressions.call(SparkMethod.GET_SPARK_CONTEXT.method, implementor.getRootExpression()), Expressions.newArrayInit(Primitive.box(rowClass), expressions)))); return implementor.result(physType, builder.toBlock()); }
Example #11
Source File: SparkRules.java From calcite with Apache License 2.0 | 4 votes |
public Result implementSpark(Implementor implementor) { final JavaTypeFactory typeFactory = implementor.getTypeFactory(); final BlockBuilder builder = new BlockBuilder(); final SparkRel child = (SparkRel) getInput(); final Result result = implementor.visitInput(this, 0, child); final PhysType physType = PhysTypeImpl.of( typeFactory, getRowType(), JavaRowFormat.CUSTOM); // final RDD<Employee> inputRdd = <<child adapter>>; // return inputRdd.flatMap( // new FlatMapFunction<Employee, X>() { // public List<X> call(Employee e) { // if (!(e.empno < 10)) { // return Collections.emptyList(); // } // return Collections.singletonList( // new X(...))); // } // }) Type outputJavaType = physType.getJavaRowType(); final Type rddType = Types.of( JavaRDD.class, outputJavaType); Type inputJavaType = result.physType.getJavaRowType(); final Expression inputRdd_ = builder.append( "inputRdd", result.block); BlockBuilder builder2 = new BlockBuilder(); final ParameterExpression e_ = Expressions.parameter(inputJavaType, "e"); if (program.getCondition() != null) { Expression condition = RexToLixTranslator.translateCondition( program, typeFactory, builder2, new RexToLixTranslator.InputGetterImpl( Collections.singletonList( Pair.of((Expression) e_, result.physType))), null, implementor.getConformance()); builder2.add( Expressions.ifThen( Expressions.not(condition), Expressions.return_(null, Expressions.call( BuiltInMethod.COLLECTIONS_EMPTY_LIST.method)))); } final SqlConformance conformance = SqlConformanceEnum.DEFAULT; List<Expression> expressions = RexToLixTranslator.translateProjects( program, typeFactory, conformance, builder2, null, DataContext.ROOT, new RexToLixTranslator.InputGetterImpl( Collections.singletonList( Pair.of((Expression) e_, result.physType))), null); builder2.add( Expressions.return_(null, Expressions.convert_( Expressions.call( BuiltInMethod.COLLECTIONS_SINGLETON_LIST.method, physType.record(expressions)), List.class))); final BlockStatement callBody = builder2.toBlock(); builder.add( Expressions.return_( null, Expressions.call( inputRdd_, SparkMethod.RDD_FLAT_MAP.method, Expressions.lambda( SparkRuntime.CalciteFlatMapFunction.class, callBody, e_)))); return implementor.result(physType, builder.toBlock()); }
Example #12
Source File: HiveUDFImplementor.java From marble with Apache License 2.0 | 4 votes |
@Override public Expression implement(RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { try { SqlOperator operator = call.getOperator(); int hiveUdfId = HiveUDFInstanceCollecterPerSqlQuery.get().getSizeOfStashedHiveUDFInstance(); String udfInstanceName = "udfInstance_" + hiveUdfId; HiveUDFInstanceCollecterPerSqlQuery.get().incrementSizeOfStashedHiveUDFInstance(); ParameterExpression udfInstanceVariableExpr = Expressions .parameter(Types.of(GenericUDF.class, Object.class), "hiveUDFInstanceHolder." + udfInstanceName); ParameterExpression udfInstanceVariableLocalExpr = Expressions .parameter(Types.of(GenericUDF.class, Object.class), udfInstanceName); HiveUDFInstanceCollecterPerSqlQuery.get() .getStashedFieldsForHiveUDFInstanceHolder() .add(generateUdfInstanceDeclaration(operator.getName(), operator.getSyntax(), udfInstanceName)); Expression argTypeArrayExpr = generateArgsTypeExpr(call, translatedOperands); Expression argsExpr = new NewArrayExpression(Object.class, 1, null, translatedOperands); String outputOiName = "udfOutputOi_" + hiveUdfId; ParameterExpression udfOutputOiVariableExpr = Expressions .parameter(Types.of(GenericUDF.class, Object.class), "hiveUDFInstanceHolder." + outputOiName); HiveUDFInstanceCollecterPerSqlQuery.get() .getStashedFieldsForHiveUDFInstanceHolder() .add(generateUdfOutputOIDeclaration(udfInstanceVariableLocalExpr, argTypeArrayExpr, outputOiName)); Expression callExpr = Expressions.call(CALL_GENERIC_UDF_METHOD, Arrays.asList(udfInstanceVariableExpr, argsExpr, argTypeArrayExpr, udfOutputOiVariableExpr)); String castMethodName = TypeConvertUtil.CALCITE_SQL_TYPE_2_CAST_METHOD.get( call.type.getSqlTypeName()); Method castMethod = TypeConvertUtil.class.getMethod(castMethodName, Object.class); Expression castExpr = Expressions.call(castMethod, callExpr); return castExpr; } catch (Exception e) { throw new RuntimeException(e); } }
Example #13
Source File: CalcitePrepareImpl.java From calcite with Apache License 2.0 | 4 votes |
public RexNode toRex(Expression expression) { switch (expression.getNodeType()) { case MemberAccess: // Case-sensitive name match because name was previously resolved. return rexBuilder.makeFieldAccess( toRex( ((MemberExpression) expression).expression), ((MemberExpression) expression).field.getName(), true); case GreaterThan: return binary(expression, SqlStdOperatorTable.GREATER_THAN); case LessThan: return binary(expression, SqlStdOperatorTable.LESS_THAN); case Parameter: return parameter((ParameterExpression) expression); case Call: MethodCallExpression call = (MethodCallExpression) expression; SqlOperator operator = RexToLixTranslator.JAVA_TO_SQL_METHOD_MAP.get(call.method); if (operator != null) { return rexBuilder.makeCall( type(call), operator, toRex( Expressions.<Expression>list() .appendIfNotNull(call.targetExpression) .appendAll(call.expressions))); } throw new RuntimeException( "Could translate call to method " + call.method); case Constant: final ConstantExpression constant = (ConstantExpression) expression; Object value = constant.value; if (value instanceof Number) { Number number = (Number) value; if (value instanceof Double || value instanceof Float) { return rexBuilder.makeApproxLiteral( BigDecimal.valueOf(number.doubleValue())); } else if (value instanceof BigDecimal) { return rexBuilder.makeExactLiteral((BigDecimal) value); } else { return rexBuilder.makeExactLiteral( BigDecimal.valueOf(number.longValue())); } } else if (value instanceof Boolean) { return rexBuilder.makeLiteral((Boolean) value); } else { return rexBuilder.makeLiteral(constant.toString()); } default: throw new UnsupportedOperationException( "unknown expression type " + expression.getNodeType() + " " + expression); } }
Example #14
Source File: WinAggAddContextImpl.java From calcite with Apache License 2.0 | 4 votes |
public final RexToLixTranslator rowTranslator() { return rowTranslator( computeIndex(Expressions.constant(0), WinAggImplementor.SeekType.AGG_INDEX)); }
Example #15
Source File: WinAggResultContextImpl.java From calcite with Apache License 2.0 | 4 votes |
public RexToLixTranslator rowTranslator(Expression rowIndex) { return getFrame().rowTranslator(rowIndex) .setNullable(currentNullables()); }
Example #16
Source File: RexExecutorImpl.java From calcite with Apache License 2.0 | 4 votes |
private static String compile(RexBuilder rexBuilder, List<RexNode> constExps, RexToLixTranslator.InputGetter getter) { final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory(); final RelDataType emptyRowType = typeFactory.builder().build(); return compile(rexBuilder, constExps, getter, emptyRowType); }
Example #17
Source File: HiveRexExecutorImpl.java From marble with Apache License 2.0 | 4 votes |
private String compile(RexBuilder rexBuilder, List<RexNode> constExps, RexToLixTranslator.InputGetter getter) { final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory(); final RelDataType emptyRowType = typeFactory.builder().build(); return compile(rexBuilder, constExps, getter, emptyRowType); }
Example #18
Source File: JaninoRexCompiler.java From calcite with Apache License 2.0 | 4 votes |
public Scalar compile(List<RexNode> nodes, RelDataType inputRowType) { final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder); for (RexNode node : nodes) { programBuilder.addProject(node, null); } final RexProgram program = programBuilder.getProgram(); final BlockBuilder builder = new BlockBuilder(); final ParameterExpression context_ = Expressions.parameter(Context.class, "context"); final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues"); final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem()); // public void execute(Context, Object[] outputValues) final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl( ImmutableList.of( Pair.of( Expressions.field(context_, BuiltInMethod.CONTEXT_VALUES.field), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false)))); final Function1<String, RexToLixTranslator.InputGetter> correlates = a0 -> { throw new UnsupportedOperationException(); }; final Expression root = Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field); final SqlConformance conformance = SqlConformanceEnum.DEFAULT; // TODO: get this from implementor final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, conformance, builder, null, root, inputGetter, correlates); for (int i = 0; i < list.size(); i++) { builder.add( Expressions.statement( Expressions.assign( Expressions.arrayIndex(outputValues_, Expressions.constant(i)), list.get(i)))); } return baz(context_, outputValues_, builder.toBlock()); }