Search in sources :

Example 26 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project nifi by apache.

the class FlowFileTable method getRowType.

@Override
public RelDataType getRowType(final RelDataTypeFactory typeFactory) {
    if (relDataType != null) {
        return relDataType;
    }
    RecordSchema schema;
    try (final InputStream in = session.read(flowFile)) {
        final RecordReader recordParser = recordParserFactory.createRecordReader(flowFile, in, logger);
        schema = recordParser.getSchema();
    } catch (final Exception e) {
        throw new ProcessException("Failed to determine schema of data records for " + flowFile, e);
    }
    final List<String> names = new ArrayList<>();
    final List<RelDataType> types = new ArrayList<>();
    final JavaTypeFactory javaTypeFactory = (JavaTypeFactory) typeFactory;
    for (final RecordField field : schema.getFields()) {
        names.add(field.getFieldName());
        types.add(getRelDataType(field.getDataType(), javaTypeFactory));
    }
    logger.debug("Found Schema: {}", new Object[] { schema });
    if (recordSchema == null) {
        recordSchema = schema;
    }
    relDataType = typeFactory.createStructType(Pair.zip(names, types));
    return relDataType;
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) RecordField(org.apache.nifi.serialization.record.RecordField) InputStream(java.io.InputStream) RecordReader(org.apache.nifi.serialization.RecordReader) ArrayList(java.util.ArrayList) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) ProcessException(org.apache.nifi.processor.exception.ProcessException)

Example 27 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project streamline by hortonworks.

the class TestRelNodeCompiler method testFilter.

@Test
public void testFilter() throws Exception {
    String sql = "SELECT ID + 1 FROM FOO WHERE ID > 3";
    TestCompilerUtils.CalciteState state = TestCompilerUtils.sqlOverDummyTable(sql);
    JavaTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    LogicalProject project = (LogicalProject) state.tree();
    LogicalFilter filter = (LogicalFilter) project.getInput();
    try (StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw)) {
        RelNodeCompiler compiler = new RelNodeCompiler(pw, typeFactory);
        // standalone mode doesn't use inputstreams argument
        compiler.visitFilter(filter, Collections.EMPTY_LIST);
        pw.flush();
        Assert.assertThat(sw.toString(), containsString("> 3"));
    }
    try (StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw)) {
        RelNodeCompiler compiler = new RelNodeCompiler(pw, typeFactory);
        // standalone mode doesn't use inputstreams argument
        compiler.visitProject(project, Collections.EMPTY_LIST);
        pw.flush();
        Assert.assertThat(sw.toString(), containsString(" + 1"));
    }
}
Also used : StringWriter(java.io.StringWriter) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RelNodeCompiler(com.hortonworks.streamline.streams.sql.compiler.RelNodeCompiler) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 28 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project streamline by hortonworks.

the class TestCompilerUtils method sqlOverDummyTable.

public static CalciteState sqlOverDummyTable(String sql) throws RelConversionException, ValidationException, SqlParseException {
    SchemaPlus schema = Frameworks.createRootSchema(true);
    JavaTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    StreamableTable streamableTable = new CompilerUtil.TableBuilderInfo(typeFactory).field("ID", SqlTypeName.INTEGER).field("NAME", typeFactory.createType(String.class)).field("ADDR", typeFactory.createType(String.class)).build();
    Table table = streamableTable.stream();
    schema.add("FOO", table);
    schema.add("BAR", table);
    schema.add("MYPLUS", ScalarFunctionImpl.create(MyPlus.class, "eval"));
    List<SqlOperatorTable> sqlOperatorTables = new ArrayList<>();
    sqlOperatorTables.add(SqlStdOperatorTable.instance());
    sqlOperatorTables.add(new CalciteCatalogReader(CalciteSchema.from(schema), false, Collections.<String>emptyList(), typeFactory));
    SqlOperatorTable chainedSqlOperatorTable = new ChainedSqlOperatorTable(sqlOperatorTables);
    FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(schema).operatorTable(chainedSqlOperatorTable).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(sql);
    SqlNode validate = planner.validate(parse);
    RelNode tree = planner.convert(validate);
    System.out.println(RelOptUtil.toString(tree, SqlExplainLevel.ALL_ATTRIBUTES));
    return new CalciteState(schema, tree);
}
Also used : SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) Table(org.apache.calcite.schema.Table) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) StreamableTable(org.apache.calcite.schema.StreamableTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) StreamableTable(org.apache.calcite.schema.StreamableTable) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) RelNode(org.apache.calcite.rel.RelNode) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Example 29 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project apex-malhar by apache.

the class ExpressionCompiler method getExpression.

/**
 * 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);
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) BlockStatement(org.apache.calcite.linq4j.tree.BlockStatement) Statement(org.apache.calcite.linq4j.tree.Statement) Function1(org.apache.calcite.linq4j.function.Function1) PhysType(org.apache.calcite.adapter.enumerable.PhysType) Expression(org.apache.calcite.linq4j.tree.Expression) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RexToLixTranslator(org.apache.calcite.adapter.enumerable.RexToLixTranslator) RexProgramBuilder(org.apache.calcite.rex.RexProgramBuilder) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 30 with JavaTypeFactory

use of org.apache.calcite.adapter.java.JavaTypeFactory in project storm by apache.

the class TestCompilerUtils method sqlOverDummyTable.

public static CalciteState sqlOverDummyTable(String sql) throws RelConversionException, ValidationException, SqlParseException {
    SchemaPlus schema = Frameworks.createRootSchema(true);
    JavaTypeFactory typeFactory = new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    StreamableTable streamableTable = new CompilerUtil.TableBuilderInfo(typeFactory).field("ID", SqlTypeName.INTEGER, new ColumnConstraint.PrimaryKey(SqlMonotonicity.MONOTONIC, SqlParserPos.ZERO)).field("NAME", typeFactory.createType(String.class)).field("ADDR", typeFactory.createType(String.class)).build();
    Table table = streamableTable.stream();
    schema.add("FOO", table);
    schema.add("BAR", table);
    schema.add("MYPLUS", ScalarFunctionImpl.create(MyPlus.class, "eval"));
    QueryPlanner queryPlanner = new QueryPlanner(schema);
    StreamsRel tree = queryPlanner.getPlan(sql);
    System.out.println(StormRelUtils.explain(tree, SqlExplainLevel.ALL_ATTRIBUTES));
    return new CalciteState(schema, tree);
}
Also used : StreamableTable(org.apache.calcite.schema.StreamableTable) Table(org.apache.calcite.schema.Table) StreamableTable(org.apache.calcite.schema.StreamableTable) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) SchemaPlus(org.apache.calcite.schema.SchemaPlus) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) QueryPlanner(org.apache.storm.sql.planner.streams.QueryPlanner) StreamsRel(org.apache.storm.sql.planner.streams.rel.StreamsRel)

Aggregations

JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)43 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)18 ArrayList (java.util.ArrayList)15 SchemaPlus (org.apache.calcite.schema.SchemaPlus)15 StreamableTable (org.apache.calcite.schema.StreamableTable)12 Table (org.apache.calcite.schema.Table)12 RelDataType (org.apache.calcite.rel.type.RelDataType)10 Expression (org.apache.calcite.linq4j.tree.Expression)9 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)8 SqlNode (org.apache.calcite.sql.SqlNode)7 Type (java.lang.reflect.Type)6 ParameterExpression (org.apache.calcite.linq4j.tree.ParameterExpression)6 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)6 CompilerUtil (org.apache.storm.sql.compiler.CompilerUtil)6 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)5 RelNode (org.apache.calcite.rel.RelNode)5 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)5 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)5 ImmutableList (com.google.common.collect.ImmutableList)4 CalcitePrepare (org.apache.calcite.jdbc.CalcitePrepare)4