use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.
the class PlannerTest method checkBushy.
/**
* Checks that a query returns a particular plan, using a planner with
* MultiJoinOptimizeBushyRule enabled.
*/
private void checkBushy(String sql, String expected) throws Exception {
final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(SqlParser.Config.DEFAULT).defaultSchema(CalciteAssert.addSchema(rootSchema, CalciteAssert.SchemaSpec.CLONE_FOODMART)).traitDefs((List<RelTraitDef>) null).programs(Programs.heuristicJoinOrder(Programs.RULE_SET, true, 2)).build();
Planner planner = Frameworks.getPlanner(config);
SqlNode parse = planner.parse(sql);
SqlNode validate = planner.validate(parse);
RelNode convert = planner.rel(validate).project();
RelTraitSet traitSet = planner.getEmptyTraitSet().replace(EnumerableConvention.INSTANCE);
RelNode transform = planner.transform(0, traitSet, convert);
assertThat(toString(transform), containsString(expected));
}
use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.
the class RelSet method addAbstractConverters.
private void addAbstractConverters(VolcanoPlanner planner, RelOptCluster cluster, RelSubset subset, boolean subsetToOthers) {
// we can convert. No point adding converters if it is not possible.
for (RelSubset other : subsets) {
assert other.getTraitSet().size() == subset.getTraitSet().size();
if ((other == subset) || (subsetToOthers && !subset.getConvention().useAbstractConvertersForConversion(subset.getTraitSet(), other.getTraitSet())) || (!subsetToOthers && !other.getConvention().useAbstractConvertersForConversion(other.getTraitSet(), subset.getTraitSet()))) {
continue;
}
final ImmutableList<RelTrait> difference = subset.getTraitSet().difference(other.getTraitSet());
boolean addAbstractConverter = true;
int numTraitNeedConvert = 0;
for (RelTrait curOtherTrait : difference) {
RelTraitDef traitDef = curOtherTrait.getTraitDef();
RelTrait curRelTrait = subset.getTraitSet().getTrait(traitDef);
assert curRelTrait.getTraitDef() == traitDef;
if (curRelTrait == null) {
addAbstractConverter = false;
break;
}
boolean canConvert = false;
boolean needConvert = false;
if (subsetToOthers) {
// We can convert from subset to other. So, add converter with subset as child and
// traitset as the other's traitset.
canConvert = traitDef.canConvert(cluster.getPlanner(), curRelTrait, curOtherTrait, subset);
needConvert = !curRelTrait.satisfies(curOtherTrait);
} else {
// We can convert from others to subset.
canConvert = traitDef.canConvert(cluster.getPlanner(), curOtherTrait, curRelTrait, other);
needConvert = !curOtherTrait.satisfies(curRelTrait);
}
if (!canConvert) {
addAbstractConverter = false;
break;
}
if (needConvert) {
numTraitNeedConvert++;
}
}
if (addAbstractConverter && numTraitNeedConvert > 0) {
if (subsetToOthers) {
final AbstractConverter converter = new AbstractConverter(cluster, subset, null, other.getTraitSet());
planner.register(converter, other);
} else {
final AbstractConverter converter = new AbstractConverter(cluster, other, null, subset.getTraitSet());
planner.register(converter, subset);
}
}
}
}
use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.
the class VolcanoPlanner method addRule.
public boolean addRule(RelOptRule rule) {
if (locked) {
return false;
}
if (ruleSet.contains(rule)) {
// Rule already exists.
return false;
}
final boolean added = ruleSet.add(rule);
assert added;
final String ruleName = rule.toString();
if (ruleNames.put(ruleName, rule.getClass())) {
Set<Class> x = ruleNames.get(ruleName);
if (x.size() > 1) {
throw new RuntimeException("Rule description '" + ruleName + "' is not unique; classes: " + x);
}
}
mapRuleDescription(rule);
// it.
for (RelOptRuleOperand operand : rule.getOperands()) {
for (Class<? extends RelNode> subClass : subClasses(operand.getMatchedClass())) {
classOperands.put(subClass, operand);
}
}
// with the trait.
if (rule instanceof ConverterRule) {
ConverterRule converterRule = (ConverterRule) rule;
final RelTrait ruleTrait = converterRule.getInTrait();
final RelTraitDef ruleTraitDef = ruleTrait.getTraitDef();
if (traitDefs.contains(ruleTraitDef)) {
ruleTraitDef.registerConverterRule(this, converterRule);
}
}
return true;
}
use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.
the class VolcanoPlanner method completeConversion.
/**
* Converts traits using well-founded induction. We don't require that
* each conversion preserves all traits that have previously been converted,
* but if it changes "locked in" traits we'll try some other conversion.
*
* @param rel Relational expression
* @param allowInfiniteCostConverters Whether to allow infinite converters
* @param toTraits Target trait set
* @param usedTraits Traits that have been locked in
* @return Converted relational expression
*/
private RelNode completeConversion(RelNode rel, boolean allowInfiniteCostConverters, RelTraitSet toTraits, Expressions.FluentList<RelTraitDef> usedTraits) {
if (true) {
return rel;
}
for (RelTrait trait : rel.getTraitSet()) {
if (toTraits.contains(trait)) {
// We're already a match on this trait type.
continue;
}
final RelTraitDef traitDef = trait.getTraitDef();
RelNode rel2 = traitDef.convert(this, rel, toTraits.getTrait(traitDef), allowInfiniteCostConverters);
// heading for a cycle.
for (RelTraitDef usedTrait : usedTraits) {
if (!rel2.getTraitSet().contains(usedTrait)) {
continue;
}
}
// recursive call, to convert one more trait
rel = completeConversion(rel2, allowInfiniteCostConverters, toTraits, usedTraits.append(traitDef));
if (rel != null) {
return rel;
}
}
assert rel.getTraitSet().equals(toTraits);
return rel;
}
use of org.apache.calcite.plan.RelTraitDef in project beam by apache.
the class CalciteQueryPlanner method defaultConfig.
public FrameworkConfig defaultConfig(JdbcConnection connection, Collection<RuleSet> ruleSets) {
final CalciteConnectionConfig config = connection.config();
final SqlParser.ConfigBuilder parserConfig = SqlParser.configBuilder().setQuotedCasing(config.quotedCasing()).setUnquotedCasing(config.unquotedCasing()).setQuoting(config.quoting()).setConformance(config.conformance()).setCaseSensitive(config.caseSensitive());
final SqlParserImplFactory parserFactory = config.parserFactory(SqlParserImplFactory.class, null);
if (parserFactory != null) {
parserConfig.setParserFactory(parserFactory);
}
final SchemaPlus schema = connection.getRootSchema();
final SchemaPlus defaultSchema = connection.getCurrentSchemaPlus();
final ImmutableList<RelTraitDef> traitDefs = ImmutableList.of(ConventionTraitDef.INSTANCE);
final CalciteCatalogReader catalogReader = new CalciteCatalogReader(CalciteSchema.from(schema), ImmutableList.of(defaultSchema.getName()), connection.getTypeFactory(), connection.config());
final SqlOperatorTable opTab0 = connection.config().fun(SqlOperatorTable.class, SqlStdOperatorTable.instance());
return Frameworks.newConfigBuilder().parserConfig(parserConfig.build()).defaultSchema(defaultSchema).traitDefs(traitDefs).context(Contexts.of(connection.config())).ruleSets(ruleSets.toArray(new RuleSet[0])).costFactory(BeamCostModel.FACTORY).typeSystem(connection.getTypeFactory().getTypeSystem()).operatorTable(SqlOperatorTables.chain(opTab0, catalogReader)).build();
}
Aggregations