Search in sources :

Example 1 with ProjectableFilterableTable

use of org.apache.calcite.schema.ProjectableFilterableTable in project calcite by apache.

the class TableScanNode method create.

/**
 * Creates a TableScanNode.
 *
 * <p>Tries various table SPIs, and negotiates with the table which filters
 * and projects it can implement. Adds to the Enumerable implementations of
 * any filters and projects that cannot be implemented by the table.
 */
static TableScanNode create(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects) {
    final RelOptTable relOptTable = rel.getTable();
    final ProjectableFilterableTable pfTable = relOptTable.unwrap(ProjectableFilterableTable.class);
    if (pfTable != null) {
        return createProjectableFilterable(compiler, rel, filters, projects, pfTable);
    }
    final FilterableTable filterableTable = relOptTable.unwrap(FilterableTable.class);
    if (filterableTable != null) {
        return createFilterable(compiler, rel, filters, projects, filterableTable);
    }
    final ScannableTable scannableTable = relOptTable.unwrap(ScannableTable.class);
    if (scannableTable != null) {
        return createScannable(compiler, rel, filters, projects, scannableTable);
    }
    // noinspection unchecked
    final Enumerable<Row> enumerable = relOptTable.unwrap(Enumerable.class);
    if (enumerable != null) {
        return createEnumerable(compiler, rel, enumerable, null, filters, projects);
    }
    final QueryableTable queryableTable = relOptTable.unwrap(QueryableTable.class);
    if (queryableTable != null) {
        return createQueryable(compiler, rel, filters, projects, queryableTable);
    }
    throw new AssertionError("cannot convert table " + relOptTable + " to enumerable");
}
Also used : QueryableTable(org.apache.calcite.schema.QueryableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ScannableTable(org.apache.calcite.schema.ScannableTable)

Example 2 with ProjectableFilterableTable

use of org.apache.calcite.schema.ProjectableFilterableTable in project calcite by apache.

the class EnumerableTableScan method deduceElementType.

public static Class deduceElementType(Table table) {
    if (table instanceof QueryableTable) {
        final QueryableTable queryableTable = (QueryableTable) table;
        final Type type = queryableTable.getElementType();
        if (type instanceof Class) {
            return (Class) type;
        } else {
            return Object[].class;
        }
    } else if (table instanceof ScannableTable || table instanceof FilterableTable || table instanceof ProjectableFilterableTable || table instanceof StreamableTable) {
        return Object[].class;
    } else {
        return Object.class;
    }
}
Also used : QueryableTable(org.apache.calcite.schema.QueryableTable) RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) StreamableTable(org.apache.calcite.schema.StreamableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable)

Example 3 with ProjectableFilterableTable

use of org.apache.calcite.schema.ProjectableFilterableTable 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 4 with ProjectableFilterableTable

use of org.apache.calcite.schema.ProjectableFilterableTable in project calcite by apache.

the class RelOptTableImpl method toRel.

public RelNode toRel(ToRelContext context) {
    // RelOptTable by replacing with immutable RelRecordType using the same field list.
    if (this.getRowType().isDynamicStruct()) {
        final RelDataType staticRowType = new RelRecordType(getRowType().getFieldList());
        final RelOptTable relOptTable = this.copy(staticRowType);
        return relOptTable.toRel(context);
    }
    // If there are any virtual columns, create a copy of this table without
    // those virtual columns.
    final List<ColumnStrategy> strategies = getColumnStrategies();
    if (strategies.contains(ColumnStrategy.VIRTUAL)) {
        final RelDataTypeFactory.Builder b = context.getCluster().getTypeFactory().builder();
        for (RelDataTypeField field : rowType.getFieldList()) {
            if (strategies.get(field.getIndex()) != ColumnStrategy.VIRTUAL) {
                b.add(field.getName(), field.getType());
            }
        }
        final RelOptTable relOptTable = new RelOptTableImpl(this.schema, b.build(), this.names, this.table, this.expressionFunction, this.rowCount) {

            @Override
            public <T> T unwrap(Class<T> clazz) {
                if (clazz.isAssignableFrom(InitializerExpressionFactory.class)) {
                    return clazz.cast(NullInitializerExpressionFactory.INSTANCE);
                }
                return super.unwrap(clazz);
            }
        };
        return relOptTable.toRel(context);
    }
    if (table instanceof TranslatableTable) {
        return ((TranslatableTable) table).toRel(context, this);
    }
    final RelOptCluster cluster = context.getCluster();
    if (Hook.ENABLE_BINDABLE.get(false)) {
        return LogicalTableScan.create(cluster, this);
    }
    if (CalcitePrepareImpl.ENABLE_ENUMERABLE && table instanceof QueryableTable) {
        return EnumerableTableScan.create(cluster, this);
    }
    if (table instanceof ScannableTable || table instanceof FilterableTable || table instanceof ProjectableFilterableTable) {
        return LogicalTableScan.create(cluster, this);
    }
    if (CalcitePrepareImpl.ENABLE_ENUMERABLE) {
        return EnumerableTableScan.create(cluster, this);
    }
    throw new AssertionError();
}
Also used : ColumnStrategy(org.apache.calcite.schema.ColumnStrategy) RelOptCluster(org.apache.calcite.plan.RelOptCluster) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) RelDataType(org.apache.calcite.rel.type.RelDataType) RelRecordType(org.apache.calcite.rel.type.RelRecordType) QueryableTable(org.apache.calcite.schema.QueryableTable) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) TranslatableTable(org.apache.calcite.schema.TranslatableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) ScannableTable(org.apache.calcite.schema.ScannableTable)

Example 5 with ProjectableFilterableTable

use of org.apache.calcite.schema.ProjectableFilterableTable in project calcite by apache.

the class ScannableTableTest method testCannotPushProject.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2039">[CALCITE-2039]
 * AssertionError when pushing project to ProjectableFilterableTable</a>.
 * Cannot push down a project if it is not a permutation of columns; in this
 * case, it contains a literal.
 */
@Test
public void testCannotPushProject() throws Exception {
    final StringBuilder buf = new StringBuilder();
    final Table table = new BeatlesProjectableFilterableTable(buf, true);
    final String explain = "PLAN=" + "EnumerableCalc(expr#0..2=[{inputs}], expr#3=[3], k=[$t2], j=[$t1], " + "i=[$t0], EXPR$3=[$t3])\n" + "  EnumerableInterpreter\n" + "    BindableTableScan(table=[[s, beatles]])";
    CalciteAssert.that().with(newSchema("s", "beatles", table)).query("select \"k\",\"j\",\"i\",3 from \"s\".\"beatles\"").explainContains(explain).returnsUnordered("k=1940; j=John; i=4; EXPR$3=3", "k=1940; j=Ringo; i=5; EXPR$3=3", "k=1942; j=Paul; i=4; EXPR$3=3", "k=1943; j=George; i=6; EXPR$3=3");
    assertThat(buf.toString(), is("returnCount=4"));
}
Also used : ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) Table(org.apache.calcite.schema.Table) FilterableTable(org.apache.calcite.schema.FilterableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) Test(org.junit.Test)

Aggregations

ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)5 FilterableTable (org.apache.calcite.schema.FilterableTable)4 ScannableTable (org.apache.calcite.schema.ScannableTable)4 RelOptTable (org.apache.calcite.plan.RelOptTable)3 QueryableTable (org.apache.calcite.schema.QueryableTable)3 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Table (org.apache.calcite.schema.Table)2 AbstractTable (org.apache.calcite.schema.impl.AbstractTable)2 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)2 Test (org.junit.Test)2 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 RelOptAbstractTable (org.apache.calcite.plan.RelOptAbstractTable)1 RelOptCluster (org.apache.calcite.plan.RelOptCluster)1 RelTraitDef (org.apache.calcite.plan.RelTraitDef)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 RelRecordType (org.apache.calcite.rel.type.RelRecordType)1 ColumnStrategy (org.apache.calcite.schema.ColumnStrategy)1 ModifiableTable (org.apache.calcite.schema.ModifiableTable)1