use of org.apache.calcite.rel.RelRoot in project beam by apache.
the class ThreeTablesSchema method transform.
private RelNode transform(String sql, RuleSet prepareRules) throws Exception {
final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
final SchemaPlus defSchema = rootSchema.add("tt", new ThreeTablesSchema());
final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.Config.DEFAULT).defaultSchema(defSchema).traitDefs(ConventionTraitDef.INSTANCE, RelCollationTraitDef.INSTANCE).programs(Programs.of(prepareRules)).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);
return planner.transform(0, desiredTraits, planBefore);
}
use of org.apache.calcite.rel.RelRoot in project druid by druid-io.
the class DruidPlanner method planWithDruidConvention.
/**
* Construct a {@link PlannerResult} for a {@link RelNode} that is directly translatable to a native Druid query.
*/
private PlannerResult planWithDruidConvention(final RelRoot root, @Nullable final SqlExplain explain, @Nullable final SqlInsert insert) throws ValidationException, RelConversionException {
final RelRoot possiblyLimitedRoot = possiblyWrapRootWithOuterLimitFromContext(root);
final QueryMaker queryMaker = buildQueryMaker(root, insert);
plannerContext.setQueryMaker(queryMaker);
RelNode parameterized = rewriteRelDynamicParameters(possiblyLimitedRoot.rel);
final DruidRel<?> druidRel = (DruidRel<?>) planner.transform(Rules.DRUID_CONVENTION_RULES, planner.getEmptyTraitSet().replace(DruidConvention.instance()).plus(root.collation), parameterized);
if (explain != null) {
return planExplanation(druidRel, explain, true);
} else {
final Supplier<Sequence<Object[]>> resultsSupplier = () -> {
// sanity check
final Set<ResourceAction> readResourceActions = plannerContext.getResourceActions().stream().filter(action -> action.getAction() == Action.READ).collect(Collectors.toSet());
Preconditions.checkState(readResourceActions.isEmpty() == druidRel.getDataSourceNames().isEmpty() || // them with InlineDataSource of empty rows.
readResourceActions.size() >= druidRel.getDataSourceNames().size(), "Authorization sanity check failed");
return druidRel.runQuery();
};
return new PlannerResult(resultsSupplier, queryMaker.getResultType());
}
}
use of org.apache.calcite.rel.RelRoot in project druid by druid-io.
the class DruidPlanner method prepare.
/**
* Prepare an SQL query for execution, including some initial parsing and validation and any dynamic parameter type
* resolution, to support prepared statements via JDBC.
*
* In some future this could perhaps re-use some of the work done by {@link #validate()}
* instead of repeating it, but that day is not today.
*/
public PrepareResult prepare() throws SqlParseException, ValidationException, RelConversionException {
resetPlanner();
final ParsedNodes parsed = ParsedNodes.create(planner.parse(plannerContext.getSql()));
final SqlNode validatedQueryNode = planner.validate(parsed.getQueryNode());
final RelRoot rootQueryRel = planner.rel(validatedQueryNode);
final SqlValidator validator = getValidator();
final RelDataTypeFactory typeFactory = rootQueryRel.rel.getCluster().getTypeFactory();
final RelDataType parameterTypes = validator.getParameterRowType(validator.validate(validatedQueryNode));
final RelDataType returnedRowType;
if (parsed.getExplainNode() != null) {
returnedRowType = getExplainStructType(typeFactory);
} else {
returnedRowType = buildQueryMaker(rootQueryRel, parsed.getInsertNode()).getResultType();
}
return new PrepareResult(returnedRowType, parameterTypes);
}
use of org.apache.calcite.rel.RelRoot in project druid by druid-io.
the class DruidPlanner method plan.
public PlannerResult plan(final String sql) throws SqlParseException, ValidationException, RelConversionException {
SqlExplain explain = null;
SqlNode parsed = planner.parse(sql);
if (parsed.getKind() == SqlKind.EXPLAIN) {
explain = (SqlExplain) parsed;
parsed = explain.getExplicandum();
}
final SqlNode validated = planner.validate(parsed);
final RelRoot root = planner.rel(validated);
try {
return planWithDruidConvention(explain, root);
} catch (RelOptPlanner.CannotPlanException e) {
// Try again with BINDABLE convention. Used for querying Values, metadata tables, and fallback.
try {
return planWithBindableConvention(explain, root);
} catch (Exception e2) {
e.addSuppressed(e2);
throw e;
}
}
}
use of org.apache.calcite.rel.RelRoot in project drill by axbaretto.
the class SqlConverter method toRel.
public RelRoot toRel(final SqlNode validatedNode) {
if (planner == null) {
planner = new VolcanoPlanner(costFactory, settings);
planner.setExecutor(new DrillConstExecutor(functions, util, settings));
planner.clearRelTraitDefs();
planner.addRelTraitDef(ConventionTraitDef.INSTANCE);
planner.addRelTraitDef(DrillDistributionTraitDef.INSTANCE);
planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
}
if (cluster == null) {
initCluster();
}
final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(new Expander(), validator, catalog, cluster, DrillConvertletTable.INSTANCE, sqlToRelConverterConfig);
/*
* Sets value to false to avoid simplifying project expressions
* during creating new projects since it may cause changing data mode
* which causes to assertion errors during type validation
*/
Hook.REL_BUILDER_SIMPLIFY.add(Hook.property(false));
// To avoid unexpected column errors set a value of top to false
final RelRoot rel = sqlToRelConverter.convertQuery(validatedNode, false, false);
final RelRoot rel2 = rel.withRel(sqlToRelConverter.flattenTypes(rel.rel, true));
final RelRoot rel3 = rel2.withRel(RelDecorrelator.decorrelateQuery(rel2.rel));
return rel3;
}
Aggregations