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);
}
}
}
}
}
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]"));
}
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]"));
}
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]"));
}
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]"));
}
Aggregations