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);
}
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);
}
}
}
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);
}
}
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());
}
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);
}
Aggregations