Search in sources :

Example 1 with FrameworkConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig 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).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 2 with FrameworkConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig in project storm by apache.

the class StormSqlImpl method execute.

@Override
public void execute(Iterable<String> statements, ChannelHandler result) throws Exception {
    Map<String, DataSource> dataSources = new HashMap<>();
    for (String sql : statements) {
        StormParser parser = new StormParser(sql);
        SqlNode node = parser.impl().parseSqlStmtEof();
        if (node instanceof SqlCreateTable) {
            handleCreateTable((SqlCreateTable) node, dataSources);
        } else if (node instanceof SqlCreateFunction) {
            handleCreateFunction((SqlCreateFunction) node);
        } else {
            FrameworkConfig config = buildFrameWorkConfig();
            Planner planner = Frameworks.getPlanner(config);
            SqlNode parse = planner.parse(sql);
            SqlNode validate = planner.validate(parse);
            RelNode tree = planner.convert(validate);
            PlanCompiler compiler = new PlanCompiler(typeFactory);
            AbstractValuesProcessor proc = compiler.compile(tree);
            proc.initialize(dataSources, result);
        }
    }
}
Also used : HashMap(java.util.HashMap) AbstractValuesProcessor(org.apache.storm.sql.runtime.AbstractValuesProcessor) SqlCreateTable(org.apache.storm.sql.parser.SqlCreateTable) ISqlTridentDataSource(org.apache.storm.sql.runtime.ISqlTridentDataSource) DataSource(org.apache.storm.sql.runtime.DataSource) PlanCompiler(org.apache.storm.sql.compiler.backends.standalone.PlanCompiler) RelNode(org.apache.calcite.rel.RelNode) SqlCreateFunction(org.apache.storm.sql.parser.SqlCreateFunction) Planner(org.apache.calcite.tools.Planner) QueryPlanner(org.apache.storm.sql.planner.trident.QueryPlanner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) StormParser(org.apache.storm.sql.parser.StormParser) SqlNode(org.apache.calcite.sql.SqlNode)

Example 3 with FrameworkConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig in project calcite by apache.

the class SqlDdlNodes method populate.

/**
 * Populates the table called {@code name} by executing {@code query}.
 */
protected static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) {
    // Generate, prepare and execute an "INSERT INTO table query" statement.
    // (It's a bit inefficient that we convert from SqlNode to SQL and back
    // again.)
    final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(context.getRootSchema().plus()).build();
    final Planner planner = Frameworks.getPlanner(config);
    try {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        final SqlPrettyWriter w = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, false, pw);
        pw.print("INSERT INTO ");
        name.unparse(w, 0, 0);
        pw.print(" ");
        query.unparse(w, 0, 0);
        pw.flush();
        final String sql = sw.toString();
        final SqlNode query1 = planner.parse(sql);
        final SqlNode query2 = planner.validate(query1);
        final RelRoot r = planner.rel(query2);
        final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
        int rowCount = prepare.executeUpdate();
        Util.discard(rowCount);
        prepare.close();
    } catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SQLException(java.sql.SQLException) RelRoot(org.apache.calcite.rel.RelRoot) PreparedStatement(java.sql.PreparedStatement) RelConversionException(org.apache.calcite.tools.RelConversionException) StringWriter(java.io.StringWriter) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) PrintWriter(java.io.PrintWriter) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with FrameworkConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig in project calcite by apache.

the class TraitPropagationTest method run.

// Created so that we can control when the TraitDefs are defined (e.g.
// before the cluster is created).
private static RelNode run(PropAction action, RuleSet rules) throws Exception {
    FrameworkConfig config = Frameworks.newConfigBuilder().ruleSets(rules).build();
    final Properties info = new Properties();
    final Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
    final CalciteServerStatement statement = connection.createStatement().unwrap(CalciteServerStatement.class);
    final CalcitePrepare.Context prepareContext = statement.createPrepareContext();
    final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
    CalciteCatalogReader catalogReader = new CalciteCatalogReader(prepareContext.getRootSchema(), prepareContext.getDefaultSchemaPath(), typeFactory, prepareContext.config());
    final RexBuilder rexBuilder = new RexBuilder(typeFactory);
    final RelOptPlanner planner = new VolcanoPlanner(config.getCostFactory(), config.getContext());
    // set up rules before we generate cluster
    planner.clearRelTraitDefs();
    planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
    planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
    planner.clear();
    for (RelOptRule r : rules) {
        planner.addRule(r);
    }
    final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
    return action.apply(cluster, catalogReader, prepareContext.getRootSchema().plus());
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) Connection(java.sql.Connection) CalcitePrepare(org.apache.calcite.jdbc.CalcitePrepare) Properties(java.util.Properties) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelOptRule(org.apache.calcite.plan.RelOptRule) CalciteCatalogReader(org.apache.calcite.prepare.CalciteCatalogReader) CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig)

Example 5 with FrameworkConfig

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.FrameworkConfig in project calcite by apache.

the class RelToSqlConverterTest method getPlanner.

private static Planner getPlanner(List<RelTraitDef> traitDefs, SqlParser.Config parserConfig, CalciteAssert.SchemaSpec schemaSpec, SqlToRelConverter.Config sqlToRelConf, Program... programs) {
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(parserConfig).defaultSchema(CalciteAssert.addSchema(rootSchema, schemaSpec)).traitDefs(traitDefs).sqlToRelConverterConfig(sqlToRelConf).programs(programs).build();
    return Frameworks.getPlanner(config);
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig)

Aggregations

FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)24 Planner (org.apache.calcite.tools.Planner)14 RelNode (org.apache.calcite.rel.RelNode)12 SqlNode (org.apache.calcite.sql.SqlNode)12 SchemaPlus (org.apache.calcite.schema.SchemaPlus)10 ArrayList (java.util.ArrayList)6 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)6 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)5 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)5 ChainedSqlOperatorTable (org.apache.calcite.sql.util.ChainedSqlOperatorTable)5 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)4 StreamableTable (org.apache.calcite.schema.StreamableTable)4 Table (org.apache.calcite.schema.Table)4 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)4 Test (org.junit.Test)4 Connection (java.sql.Connection)3 SQLException (java.sql.SQLException)3 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)3 RelBuilder (org.apache.calcite.tools.RelBuilder)3 HashMap (java.util.HashMap)2