Search in sources :

Example 1 with SamzaSqlJavaTypeFactoryImpl

use of org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl in project samza by apache.

the class SamzaSqlQueryParser method createPlanner.

private static Planner createPlanner() {
    Connection connection;
    SchemaPlus rootSchema;
    try {
        JavaTypeFactory typeFactory = new SamzaSqlJavaTypeFactoryImpl();
        SamzaSqlDriver driver = new SamzaSqlDriver(typeFactory);
        DriverManager.deregisterDriver(DriverManager.getDriver("jdbc:calcite:"));
        DriverManager.registerDriver(driver);
        connection = driver.connect("jdbc:calcite:", new Properties());
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        rootSchema = calciteConnection.getRootSchema();
    } catch (SQLException e) {
        throw new SamzaException(e);
    }
    final List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelCollationTraitDef.INSTANCE);
    FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.JAVA).build()).defaultSchema(rootSchema).operatorTable(SqlStdOperatorTable.instance()).traitDefs(traitDefs).context(Contexts.EMPTY_CONTEXT).costFactory(null).build();
    return Frameworks.getPlanner(frameworkConfig);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SamzaSqlDriver(org.apache.samza.sql.interfaces.SamzaSqlDriver) Properties(java.util.Properties) SamzaException(org.apache.samza.SamzaException) RelTraitDef(org.apache.calcite.plan.RelTraitDef) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SamzaSqlJavaTypeFactoryImpl(org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl)

Example 2 with SamzaSqlJavaTypeFactoryImpl

use of org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl in project samza by apache.

the class RexToJavaCompiler method compile.

/**
 * Compiles a relational expression to a instance of {@link Expression}
 *
 * for e.g.
 *    Query : select id from profile
 *      where profile table has relational schema with id(NUMBER) and name(VARCHAR) columns.
 *    This query will result in the following relational plan
 *      LogicalProject(id=[$1])
 *        LogicalTableScan(table=[[profile]])
 *
 *    And the corresponding expressions are
 *       inputs : EnumerableTableScan (Which is the output of LogicalTableScan)
 *       nodes : [$1] Which essentially means take pick the first column from the input
 *
 *    This function converts the LogicalProject expression "[$1]" with input RexNode which is an output of TableScan
 *    to a java code that implements the interface {@link Expression}
 *
 * @param inputs Input relations/time-varying relations for this row expression
 * @param nodes relational expressions that needs to be converted to java code.
 * @return compiled expression of type {@link org.apache.samza.sql.data.Expression}
 */
public org.apache.samza.sql.data.Expression compile(List<RelNode> inputs, List<RexNode> nodes) {
    /*
     *  In case there are multiple input relations, we build a single input row type combining types of all the inputs.
     */
    final RelDataTypeFactory.FieldInfoBuilder fieldBuilder = rexBuilder.getTypeFactory().builder();
    for (RelNode input : inputs) {
        fieldBuilder.addAll(input.getRowType().getFieldList());
    }
    final RelDataType inputRowType = fieldBuilder.build();
    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 sqlContext = Expressions.parameter(SamzaSqlExecutionContext.class, "sqlContext");
    final ParameterExpression context = Expressions.parameter(Context.class, "context");
    final ParameterExpression root = DataContext.ROOT;
    final ParameterExpression inputValues = Expressions.parameter(Object[].class, "inputValues");
    final ParameterExpression outputValues = Expressions.parameter(Object[].class, "outputValues");
    final JavaTypeFactoryImpl javaTypeFactory = new SamzaSqlJavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
    // public void execute(Object[] inputValues, Object[] outputValues)
    final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.of(Expressions.variable(Object[].class, "inputValues"), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
    final List<org.apache.calcite.linq4j.tree.Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, SqlConformanceEnum.DEFAULT, builder, null, DataContext.ROOT, inputGetter, null);
    for (int i = 0; i < list.size(); i++) {
        builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues, Expressions.constant(i)), list.get(i))));
    }
    return createSamzaExpressionFromCalcite(sqlContext, context, root, inputValues, outputValues, builder.toBlock());
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) RelDataType(org.apache.calcite.rel.type.RelDataType) RelNode(org.apache.calcite.rel.RelNode) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) SamzaSqlJavaTypeFactoryImpl(org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) RexNode(org.apache.calcite.rex.RexNode) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder) SamzaSqlJavaTypeFactoryImpl(org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl)

Aggregations

SamzaSqlJavaTypeFactoryImpl (org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 RexToLixTranslator (org.apache.calcite.adapter.enumerable.RexToLixTranslator)1 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)1 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)1 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)1 RelTraitDef (org.apache.calcite.plan.RelTraitDef)1 RelNode (org.apache.calcite.rel.RelNode)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RexNode (org.apache.calcite.rex.RexNode)1 RexProgram (org.apache.calcite.rex.RexProgram)1 RexProgramBuilder (org.apache.calcite.rex.RexProgramBuilder)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)1