use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class PlannerImpl method expandView.
@Override
public RelRoot expandView(RelDataType rowType, String queryString, List<String> schemaPath, @Nullable List<String> viewPath) {
RelOptPlanner planner = this.planner;
if (planner == null) {
ready();
planner = requireNonNull(this.planner, "planner");
}
SqlParser parser = SqlParser.create(queryString, parserConfig);
SqlNode sqlNode;
try {
sqlNode = parser.parseQuery();
} catch (SqlParseException e) {
throw new RuntimeException("parse failed", e);
}
final CalciteCatalogReader catalogReader = createCatalogReader().withSchemaPath(schemaPath);
final SqlValidator validator = createSqlValidator(catalogReader);
final RexBuilder rexBuilder = createRexBuilder();
final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
final SqlToRelConverter.Config config = sqlToRelConverterConfig.withTrimUnusedFields(false);
final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(this, validator, catalogReader, cluster, convertletTable, config);
final RelRoot root = sqlToRelConverter.convertQuery(sqlNode, true, false);
final RelRoot root2 = root.withRel(sqlToRelConverter.flattenTypes(root.rel, true));
final RelBuilder relBuilder = config.getRelBuilderFactory().create(cluster, null);
return root2.withRel(RelDecorrelator.decorrelateQuery(root.rel, relBuilder));
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class SqlToRelConverterTest method testLarge.
@Test
void testLarge() {
// Size factor used to be 400, but lambdas use a lot of stack
final int x = 300;
final SqlToRelFixture fixture = fixture();
SqlValidatorTest.checkLarge(x, input -> {
final RelRoot root = fixture.withSql(input).toRoot();
final String s = RelOptUtil.toString(root.project());
assertThat(s, notNullValue());
});
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class PlannerTest method testConvertWithoutValidateFails.
/**
* Tests that planner throws an error if you pass to
* {@link Planner#rel(org.apache.calcite.sql.SqlNode)}
* a {@link org.apache.calcite.sql.SqlNode} that has been parsed but not
* validated.
*/
@Test
void testConvertWithoutValidateFails() throws Exception {
Planner planner = getPlanner(null);
SqlNode parse = planner.parse("select * from \"emps\"");
try {
RelRoot rel = planner.rel(parse);
fail("expected error, got " + rel);
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("cannot move from STATE_3_PARSED to STATE_4_VALIDATED"));
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class ExtensionDdlExecutor 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(Objects.requireNonNull(Schemas.subSchema(context.getRootSchema(), context.getDefaultSchemaPath())).plus()).build();
final Planner planner = Frameworks.getPlanner(config);
try {
final StringBuilder buf = new StringBuilder();
final SqlPrettyWriter w = new SqlPrettyWriter(SqlPrettyWriter.config().withDialect(CalciteSqlDialect.DEFAULT).withAlwaysUseParentheses(false), buf);
buf.append("INSERT INTO ");
name.unparse(w, 0, 0);
buf.append(" ");
query.unparse(w, 0, 0);
final String sql = buf.toString();
final SqlNode query1 = planner.parse(sql);
final SqlNode query2 = planner.validate(query1);
final RelRoot r = planner.rel(query2);
final PreparedStatement prepare = context.getRelRunner().prepareStatement(r.rel);
int rowCount = prepare.executeUpdate();
Util.discard(rowCount);
prepare.close();
} catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
throw Util.throwAsRuntime(e);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot in project calcite by apache.
the class SortRemoveRuleTest method transform.
/**
* The default schema that is used in these tests provides tables sorted on the primary key. Due
* to this scan operators always come with a {@link org.apache.calcite.rel.RelCollation} trait.
*/
private RelNode transform(String sql, RuleSet prepareRules) throws Exception {
final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
final SchemaPlus defSchema = rootSchema.add("hr", new HrClusteredSchema());
final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.Config.DEFAULT).defaultSchema(defSchema).traitDefs(ConventionTraitDef.INSTANCE, RelCollationTraitDef.INSTANCE).programs(Programs.of(prepareRules), Programs.ofRules(CoreRules.SORT_REMOVE)).build();
Planner planner = Frameworks.getPlanner(config);
SqlNode parse = planner.parse(sql);
SqlNode validate = planner.validate(parse);
RelRoot planRoot = planner.rel(validate);
RelNode planBefore = planRoot.rel;
RelTraitSet desiredTraits = planBefore.getTraitSet().replace(EnumerableConvention.INSTANCE);
RelNode planAfter = planner.transform(0, desiredTraits, planBefore);
return planner.transform(1, desiredTraits, planAfter);
}
Aggregations