Search in sources :

Example 1 with FilterableTable

use of org.apache.calcite.schema.FilterableTable 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 FilterableTable

use of org.apache.calcite.schema.FilterableTable 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 FilterableTable

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

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

the class ScannableTableTest method testFilterableTableCooperative.

/**
 * A filter on a {@link FilterableTable} with two columns (cooperative).
 */
@Test
public void testFilterableTableCooperative() throws Exception {
    final StringBuilder buf = new StringBuilder();
    final Table table = new BeatlesFilterableTable(buf, true);
    final String explain = "PLAN=" + "EnumerableInterpreter\n" + "  BindableTableScan(table=[[s, beatles]], filters=[[=($0, 4)]])";
    CalciteAssert.that().with(newSchema("s", "beatles", table)).query("select * from \"s\".\"beatles\" where \"i\" = 4").explainContains(explain).returnsUnordered("i=4; j=John; k=1940", "i=4; j=Paul; k=1942");
    // Only 2 rows came out of the table. If the value is 4, it means that the
    // planner did not pass the filter down.
    assertThat(buf.toString(), is("returnCount=2, filter=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)

Example 5 with FilterableTable

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

the class ScannableTableTest method testFilterableTableNonCooperative.

/**
 * A filter on a {@link FilterableTable} with two columns (noncooperative).
 */
@Test
public void testFilterableTableNonCooperative() throws Exception {
    final StringBuilder buf = new StringBuilder();
    final Table table = new BeatlesFilterableTable(buf, false);
    final String explain = "PLAN=" + "EnumerableInterpreter\n" + "  BindableTableScan(table=[[s, beatles2]], filters=[[=($0, 4)]])";
    CalciteAssert.that().with(newSchema("s", "beatles2", table)).query("select * from \"s\".\"beatles2\" where \"i\" = 4").explainContains(explain).returnsUnordered("i=4; j=John; k=1940", "i=4; j=Paul; k=1942");
    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

FilterableTable (org.apache.calcite.schema.FilterableTable)5 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)5 ScannableTable (org.apache.calcite.schema.ScannableTable)5 QueryableTable (org.apache.calcite.schema.QueryableTable)3 RelOptTable (org.apache.calcite.plan.RelOptTable)2 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 RelOptCluster (org.apache.calcite.plan.RelOptCluster)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 StreamableTable (org.apache.calcite.schema.StreamableTable)1 TranslatableTable (org.apache.calcite.schema.TranslatableTable)1