Search in sources :

Example 11 with RelTraitDef

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));
}
Also used : RelNode(org.apache.calcite.rel.RelNode) RelTraitDef(org.apache.calcite.plan.RelTraitDef) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SqlNode(org.apache.calcite.sql.SqlNode)

Example 12 with RelTraitDef

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);
            }
        }
    }
}
Also used : RelTrait(org.apache.calcite.plan.RelTrait) RelTraitDef(org.apache.calcite.plan.RelTraitDef)

Example 13 with RelTraitDef

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;
}
Also used : RelOptRuleOperand(org.apache.calcite.plan.RelOptRuleOperand) ConverterRule(org.apache.calcite.rel.convert.ConverterRule) RelTrait(org.apache.calcite.plan.RelTrait) RelTraitDef(org.apache.calcite.plan.RelTraitDef)

Example 14 with RelTraitDef

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;
}
Also used : RelTrait(org.apache.calcite.plan.RelTrait) RelNode(org.apache.calcite.rel.RelNode) RelTraitDef(org.apache.calcite.plan.RelTraitDef)

Example 15 with RelTraitDef

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();
}
Also used : RuleSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet) CalciteCatalogReader(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.CalciteCatalogReader) CalciteConnectionConfig(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig) RelTraitDef(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitDef) SqlOperatorTable(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable) SqlParser(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus) SqlParserImplFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserImplFactory)

Aggregations

RelTraitDef (org.apache.calcite.plan.RelTraitDef)13 SchemaPlus (org.apache.calcite.schema.SchemaPlus)6 ArrayList (java.util.ArrayList)5 RelTrait (org.apache.calcite.plan.RelTrait)5 RelNode (org.apache.calcite.rel.RelNode)5 Test (org.junit.Test)4 RelTraitDef (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitDef)3 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)3 RuleSet (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RuleSet)3 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)3 RelTraitSet (org.apache.calcite.plan.RelTraitSet)3 SqlNode (org.apache.calcite.sql.SqlNode)3 Connection (java.sql.Connection)2 CalciteConnectionConfig (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.config.CalciteConnectionConfig)2 CalciteCatalogReader (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.prepare.CalciteCatalogReader)2 SqlOperatorTable (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlOperatorTable)2 SqlParser (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser)2 SqlParserImplFactory (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParserImplFactory)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)2