Search in sources :

Example 76 with Table

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

the class RelOptTableImpl method create.

public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType, final CalciteSchema.TableEntry tableEntry, Double rowCount) {
    final Table table = tableEntry.getTable();
    Function<Class, Expression> expressionFunction = getClassExpressionFunction(tableEntry, table);
    return new RelOptTableImpl(schema, rowType, tableEntry.path(), table, expressionFunction, rowCount);
}
Also used : TranslatableTable(org.apache.calcite.schema.TranslatableTable) Table(org.apache.calcite.schema.Table) ModifiableTable(org.apache.calcite.schema.ModifiableTable) StreamableTable(org.apache.calcite.schema.StreamableTable) RelOptTable(org.apache.calcite.plan.RelOptTable) QueryableTable(org.apache.calcite.schema.QueryableTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) FilterableTable(org.apache.calcite.schema.FilterableTable) Expression(org.apache.calcite.linq4j.tree.Expression)

Example 77 with Table

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

the class EmptyScope method resolve_.

private void resolve_(final CalciteSchema rootSchema, List<String> names, List<String> schemaNames, SqlNameMatcher nameMatcher, Path path, Resolved resolved) {
    final List<String> concat = ImmutableList.<String>builder().addAll(schemaNames).addAll(names).build();
    CalciteSchema schema = rootSchema;
    SqlValidatorNamespace namespace = null;
    List<String> remainingNames = concat;
    for (String schemaName : concat) {
        if (schema == rootSchema && nameMatcher.matches(schemaName, schema.name)) {
            remainingNames = Util.skip(remainingNames);
            continue;
        }
        final CalciteSchema subSchema = schema.getSubSchema(schemaName, nameMatcher.isCaseSensitive());
        if (subSchema != null) {
            path = path.plus(null, -1, subSchema.name, StructKind.NONE);
            remainingNames = Util.skip(remainingNames);
            schema = subSchema;
            namespace = new SchemaNamespace(validator, ImmutableList.copyOf(path.stepNames()));
            continue;
        }
        CalciteSchema.TableEntry entry = schema.getTable(schemaName, nameMatcher.isCaseSensitive());
        if (entry == null) {
            entry = schema.getTableBasedOnNullaryFunction(schemaName, nameMatcher.isCaseSensitive());
        }
        if (entry != null) {
            path = path.plus(null, -1, entry.name, StructKind.NONE);
            remainingNames = Util.skip(remainingNames);
            final Table table = entry.getTable();
            SqlValidatorTable table2 = null;
            if (table instanceof Wrapper) {
                table2 = ((Wrapper) table).unwrap(Prepare.PreparingTable.class);
            }
            if (table2 == null) {
                final RelOptSchema relOptSchema = validator.catalogReader.unwrap(RelOptSchema.class);
                final RelDataType rowType = table.getRowType(validator.typeFactory);
                table2 = RelOptTableImpl.create(relOptSchema, rowType, entry, null);
            }
            namespace = new TableNamespace(validator, table2);
            resolved.found(namespace, false, this, path, remainingNames);
            return;
        }
        // neither sub-schema nor table
        if (namespace != null && !remainingNames.equals(names)) {
            resolved.found(namespace, false, this, path, remainingNames);
        }
        return;
    }
}
Also used : Wrapper(org.apache.calcite.schema.Wrapper) Table(org.apache.calcite.schema.Table) RelOptSchema(org.apache.calcite.plan.RelOptSchema) RelDataType(org.apache.calcite.rel.type.RelDataType) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema)

Example 78 with Table

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

the class ScannableTableTest method testPFPushDownProjectFilterAggregateNested.

@Test
public void testPFPushDownProjectFilterAggregateNested() {
    final StringBuilder buf = new StringBuilder();
    final String sql = "select \"k\", count(*) as c\n" + "from (\n" + "  select \"k\", \"i\" from \"s\".\"beatles\" group by \"k\", \"i\") t\n" + "where \"k\" = 1940\n" + "group by \"k\"";
    final Table table = new BeatlesProjectableFilterableTable(buf, false);
    final String explain = "PLAN=" + "EnumerableAggregate(group=[{0}], C=[COUNT()])\n" + "  EnumerableAggregate(group=[{0, 1}])\n" + "    EnumerableInterpreter\n" + "      BindableTableScan(table=[[s, beatles]], " + "filters=[[=($2, 1940)]], projects=[[2, 0]])";
    CalciteAssert.that().with(newSchema("s", "beatles", table)).query(sql).explainContains(explain).returnsUnordered("k=1940; C=2");
}
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 79 with Table

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

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

Table (org.apache.calcite.schema.Table)104 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)38 RelOptTable (org.apache.calcite.plan.RelOptTable)33 Test (org.junit.Test)27 RelDataType (org.apache.calcite.rel.type.RelDataType)22 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)22 SchemaPlus (org.apache.calcite.schema.SchemaPlus)20 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)17 ScannableTable (org.apache.calcite.schema.ScannableTable)17 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)16 FilterableTable (org.apache.calcite.schema.FilterableTable)15 AbstractTable (org.apache.calcite.schema.impl.AbstractTable)15 StreamableTable (org.apache.calcite.schema.StreamableTable)14 ArrayList (java.util.ArrayList)13 ModifiableViewTable (org.apache.calcite.schema.impl.ModifiableViewTable)13 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)12 RelNode (org.apache.calcite.rel.RelNode)12 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 BitString (org.apache.calcite.util.BitString)10 ImmutableMap (com.google.common.collect.ImmutableMap)9