Search in sources :

Example 21 with Table

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

the class DelegatingScope method resolveInNamespace.

/**
 * If a record type allows implicit references to fields, recursively looks
 * into the fields. Otherwise returns immediately.
 */
void resolveInNamespace(SqlValidatorNamespace ns, boolean nullable, List<String> names, SqlNameMatcher nameMatcher, Path path, Resolved resolved) {
    if (names.isEmpty()) {
        resolved.found(ns, nullable, this, path, null);
        return;
    }
    final RelDataType rowType = ns.getRowType();
    if (rowType.isStruct()) {
        SqlValidatorTable validatorTable = ns.getTable();
        if (validatorTable instanceof Prepare.PreparingTable) {
            Table t = ((Prepare.PreparingTable) validatorTable).unwrap(Table.class);
            if (t instanceof CustomColumnResolvingTable) {
                final List<Pair<RelDataTypeField, List<String>>> entries = ((CustomColumnResolvingTable) t).resolveColumn(rowType, validator.getTypeFactory(), names);
                for (Pair<RelDataTypeField, List<String>> entry : entries) {
                    final RelDataTypeField field = entry.getKey();
                    final List<String> remainder = entry.getValue();
                    final SqlValidatorNamespace ns2 = new FieldNamespace(validator, field.getType());
                    final Step path2 = path.plus(rowType, field.getIndex(), field.getName(), StructKind.FULLY_QUALIFIED);
                    resolveInNamespace(ns2, nullable, remainder, nameMatcher, path2, resolved);
                }
                return;
            }
        }
        final String name = names.get(0);
        final RelDataTypeField field0 = nameMatcher.field(rowType, name);
        if (field0 != null) {
            final SqlValidatorNamespace ns2 = ns.lookupChild(field0.getName());
            final Step path2 = path.plus(rowType, field0.getIndex(), field0.getName(), StructKind.FULLY_QUALIFIED);
            resolveInNamespace(ns2, nullable, names.subList(1, names.size()), nameMatcher, path2, resolved);
        } else {
            for (RelDataTypeField field : rowType.getFieldList()) {
                switch(field.getType().getStructKind()) {
                    case PEEK_FIELDS:
                    case PEEK_FIELDS_DEFAULT:
                    case PEEK_FIELDS_NO_EXPAND:
                        final Step path2 = path.plus(rowType, field.getIndex(), field.getName(), field.getType().getStructKind());
                        final SqlValidatorNamespace ns2 = ns.lookupChild(field.getName());
                        resolveInNamespace(ns2, nullable, names, nameMatcher, path2, resolved);
                }
            }
        }
    }
}
Also used : Table(org.apache.calcite.schema.Table) CustomColumnResolvingTable(org.apache.calcite.schema.CustomColumnResolvingTable) RelDataType(org.apache.calcite.rel.type.RelDataType) CustomColumnResolvingTable(org.apache.calcite.schema.CustomColumnResolvingTable) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) Pair(org.apache.calcite.util.Pair)

Example 22 with Table

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

the class ScannableTableTest method testProjectableFilterableWithProjectFilterNonCooperative.

/**
 * A filter on a {@link org.apache.calcite.schema.ProjectableFilterableTable}
 * with two columns, and a project in the query (NonCooperative).
 */
@Test
public void testProjectableFilterableWithProjectFilterNonCooperative() throws Exception {
    final StringBuilder buf = new StringBuilder();
    final Table table = new BeatlesProjectableFilterableTable(buf, false);
    final String explain = "PLAN=" + "EnumerableInterpreter\n" + "  BindableTableScan(table=[[s, beatles]], filters=[[>($2, 1941)]], " + "projects=[[0, 2]])";
    CalciteAssert.that().with(newSchema("s", "beatles", table)).query("select \"i\",\"k\" from \"s\".\"beatles\" where \"k\" > 1941").explainContains(explain).returnsUnordered("i=4; k=1942", "i=6; k=1943");
    assertThat(buf.toString(), is("returnCount=4, projects=[0, 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 23 with Table

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

the class ScannableTableTest method testProjectableFilterableCooperative.

/**
 * A filter on a {@link org.apache.calcite.schema.ProjectableFilterableTable}
 * with two columns (cooperative).
 */
@Test
public void testProjectableFilterableCooperative() throws Exception {
    final StringBuilder buf = new StringBuilder();
    final Table table = new BeatlesProjectableFilterableTable(buf, true);
    final String explain = "PLAN=" + "EnumerableInterpreter\n" + "  BindableTableScan(table=[[s, beatles]], filters=[[=($0, 4)]], projects=[[1]])";
    CalciteAssert.that().with(newSchema("s", "beatles", table)).query("select \"j\" from \"s\".\"beatles\" where \"i\" = 4").explainContains(explain).returnsUnordered("j=John", "j=Paul");
    // 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, projects=[1]"));
}
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 24 with Table

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

the class ScannableTableTest method testPFTableRefusesFilterCooperative.

/**
 * A filter and project on a
 * {@link org.apache.calcite.schema.ProjectableFilterableTable}. The table
 * refuses to execute the filter, so Calcite should add a pull up and
 * transform the filter (projecting the column needed by the filter).
 */
@Test
public void testPFTableRefusesFilterCooperative() throws Exception {
    final StringBuilder buf = new StringBuilder();
    final Table table = new BeatlesProjectableFilterableTable(buf, false);
    final String explain = "PLAN=EnumerableInterpreter\n" + "  BindableTableScan(table=[[s, beatles2]], filters=[[=($0, 4)]], projects=[[2]])";
    CalciteAssert.that().with(newSchema("s", "beatles2", table)).query("select \"k\" from \"s\".\"beatles2\" where \"i\" = 4").explainContains(explain).returnsUnordered("k=1940", "k=1942");
    assertThat(buf.toString(), is("returnCount=4, projects=[2, 0]"));
}
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 25 with Table

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

the class ScannableTableTest method testProjectableFilterableWithProjectAndFilter.

/**
 * A filter on a {@link org.apache.calcite.schema.ProjectableFilterableTable}
 * with two columns, and a project in the query. (Cooperative)
 */
@Test
public void testProjectableFilterableWithProjectAndFilter() throws Exception {
    final StringBuilder buf = new StringBuilder();
    final Table table = new BeatlesProjectableFilterableTable(buf, true);
    final String explain = "PLAN=" + "EnumerableInterpreter\n" + "  BindableTableScan(table=[[s, beatles]], filters=[[=($0, 4)]], projects=[[2, 1]]";
    CalciteAssert.that().with(newSchema("s", "beatles", table)).query("select \"k\",\"j\" from \"s\".\"beatles\" where \"i\" = 4").explainContains(explain).returnsUnordered("k=1940; j=John", "k=1942; j=Paul");
    assertThat(buf.toString(), is("returnCount=2, filter=4, projects=[2, 1]"));
}
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