Search in sources :

Example 1 with RelTraitDef

use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.

the class VolcanoPlanner method changeTraitsUsingConverters.

private RelNode changeTraitsUsingConverters(RelNode rel, RelTraitSet toTraits, boolean allowAbstractConverters) {
    final RelTraitSet fromTraits = rel.getTraitSet();
    assert fromTraits.size() >= toTraits.size();
    final boolean allowInfiniteCostConverters = SaffronProperties.INSTANCE.allowInfiniteCostConverters().get();
    // Traits may build on top of another...for example a collation trait
    // would typically come after a distribution trait since distribution
    // destroys collation; so when doing the conversion below we use
    // fromTraits as the trait of the just previously converted RelNode.
    // Also, toTraits may have fewer traits than fromTraits, excess traits
    // will be left as is.  Finally, any null entries in toTraits are
    // ignored.
    RelNode converted = rel;
    for (int i = 0; (converted != null) && (i < toTraits.size()); i++) {
        RelTrait fromTrait = converted.getTraitSet().getTrait(i);
        final RelTraitDef traitDef = fromTrait.getTraitDef();
        RelTrait toTrait = toTraits.getTrait(i);
        if (toTrait == null) {
            continue;
        }
        assert traitDef == toTrait.getTraitDef();
        // if (fromTrait.subsumes(toTrait)) {
        if (fromTrait.equals(toTrait)) {
            // No need to convert; it's already correct.
            continue;
        }
        rel = traitDef.convert(this, converted, toTrait, allowInfiniteCostConverters);
        if (rel != null) {
            assert rel.getTraitSet().getTrait(traitDef).satisfies(toTrait);
            rel = completeConversion(rel, allowInfiniteCostConverters, toTraits, Expressions.list(traitDef));
            if (rel != null) {
                register(rel, converted);
            }
        }
        if ((rel == null) && allowAbstractConverters) {
            RelTraitSet stepTraits = converted.getTraitSet().replace(toTrait);
            rel = getSubset(converted, stepTraits);
        }
        converted = rel;
    }
    // make sure final converted traitset subsumes what was required
    if (converted != null) {
        assert converted.getTraitSet().satisfies(toTraits);
    }
    return converted;
}
Also used : RelTrait(org.apache.calcite.plan.RelTrait) RelNode(org.apache.calcite.rel.RelNode) RelTraitDef(org.apache.calcite.plan.RelTraitDef) RelTraitSet(org.apache.calcite.plan.RelTraitSet)

Example 2 with RelTraitDef

use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.

the class VolcanoPlanner method removeRule.

public boolean removeRule(RelOptRule rule) {
    if (!ruleSet.remove(rule)) {
        // Rule was not present.
        return false;
    }
    // Remove description.
    unmapRuleDescription(rule);
    // Remove operands.
    for (Iterator<RelOptRuleOperand> iter = classOperands.values().iterator(); iter.hasNext(); ) {
        RelOptRuleOperand entry = iter.next();
        if (entry.getRule().equals(rule)) {
            iter.remove();
        }
    }
    // graph.)
    if (rule instanceof ConverterRule) {
        ConverterRule converterRule = (ConverterRule) rule;
        final RelTrait ruleTrait = converterRule.getInTrait();
        final RelTraitDef ruleTraitDef = ruleTrait.getTraitDef();
        if (traitDefs.contains(ruleTraitDef)) {
            ruleTraitDef.deregisterConverterRule(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 3 with RelTraitDef

use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.

the class PlannerImpl method ready.

private void ready() {
    switch(state) {
        case STATE_0_CLOSED:
            reset();
    }
    ensure(State.STATE_1_RESET);
    Frameworks.withPlanner(new Frameworks.PlannerAction<Void>() {

        public Void apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            // use our own defaultSchema
            Util.discard(rootSchema);
            typeFactory = (JavaTypeFactory) cluster.getTypeFactory();
            planner = cluster.getPlanner();
            planner.setExecutor(executor);
            return null;
        }
    }, config);
    state = State.STATE_2_READY;
    // then, register the trait def specified in traitDefs.
    if (this.traitDefs != null) {
        planner.clearRelTraitDefs();
        for (RelTraitDef def : this.traitDefs) {
            planner.addRelTraitDef(def);
        }
    }
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelOptSchema(org.apache.calcite.plan.RelOptSchema) RelTraitDef(org.apache.calcite.plan.RelTraitDef) Frameworks(org.apache.calcite.tools.Frameworks) SchemaPlus(org.apache.calcite.schema.SchemaPlus) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory)

Example 4 with RelTraitDef

use of org.apache.calcite.plan.RelTraitDef in project calcite by apache.

the class FrameworksTest method testUpdate.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2039">[CALCITE-2039]
 * AssertionError when pushing project to ProjectableFilterableTable</a>
 * using UPDATE via {@link Frameworks}.
 */
@Test
public void testUpdate() throws Exception {
    Table table = new TableImpl();
    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
    SchemaPlus schema = rootSchema.add("x", new AbstractSchema());
    schema.add("MYTABLE", table);
    List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelDistributionTraitDef.INSTANCE);
    SqlParser.Config parserConfig = SqlParser.configBuilder(SqlParser.Config.DEFAULT).setCaseSensitive(false).build();
    final FrameworkConfig config = Frameworks.newConfigBuilder().parserConfig(parserConfig).defaultSchema(schema).traitDefs(traitDefs).ruleSets(RuleSets.ofList(AbstractConverter.ExpandConversionRule.INSTANCE)).programs(Programs.ofRules(Programs.RULE_SET)).build();
    executeQuery(config, " UPDATE MYTABLE set id=7 where id=1", CalcitePrepareImpl.DEBUG);
}
Also used : RelOptAbstractTable(org.apache.calcite.plan.RelOptAbstractTable) Table(org.apache.calcite.schema.Table) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) RelTraitDef(org.apache.calcite.plan.RelTraitDef) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SqlParser(org.apache.calcite.sql.parser.SqlParser) Test(org.junit.Test)

Example 5 with RelTraitDef

use of org.apache.calcite.plan.RelTraitDef in project samza by apache.

the class SamzaSqlQueryParser method createPlanner.

private static Planner createPlanner() {
    Connection connection;
    SchemaPlus rootSchema;
    try {
        JavaTypeFactory typeFactory = new SamzaSqlJavaTypeFactoryImpl();
        SamzaSqlDriver driver = new SamzaSqlDriver(typeFactory);
        DriverManager.deregisterDriver(DriverManager.getDriver("jdbc:calcite:"));
        DriverManager.registerDriver(driver);
        connection = driver.connect("jdbc:calcite:", new Properties());
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        rootSchema = calciteConnection.getRootSchema();
    } catch (SQLException e) {
        throw new SamzaException(e);
    }
    final List<RelTraitDef> traitDefs = new ArrayList<>();
    traitDefs.add(ConventionTraitDef.INSTANCE);
    traitDefs.add(RelCollationTraitDef.INSTANCE);
    FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.JAVA).build()).defaultSchema(rootSchema).operatorTable(SqlStdOperatorTable.instance()).traitDefs(traitDefs).context(Contexts.EMPTY_CONTEXT).costFactory(null).build();
    return Frameworks.getPlanner(frameworkConfig);
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) SamzaSqlDriver(org.apache.samza.sql.interfaces.SamzaSqlDriver) Properties(java.util.Properties) SamzaException(org.apache.samza.SamzaException) RelTraitDef(org.apache.calcite.plan.RelTraitDef) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SamzaSqlJavaTypeFactoryImpl(org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl)

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