use of org.apache.calcite.schema.CustomColumnResolvingTable 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.CustomColumnResolvingTable in project calcite by apache.
the class SqlValidatorUtil method getTargetField.
/**
* Resolve a target column name in the target table.
*
* @return the target field or null if the name cannot be resolved
* @param rowType the target row type
* @param id the target column identifier
* @param table the target table or null if it is not a RelOptTable instance
*/
public static RelDataTypeField getTargetField(RelDataType rowType, RelDataTypeFactory typeFactory, SqlIdentifier id, SqlValidatorCatalogReader catalogReader, RelOptTable table) {
final Table t = table == null ? null : table.unwrap(Table.class);
if (!(t instanceof CustomColumnResolvingTable)) {
final SqlNameMatcher nameMatcher = catalogReader.nameMatcher();
return nameMatcher.field(rowType, id.getSimple());
}
final List<Pair<RelDataTypeField, List<String>>> entries = ((CustomColumnResolvingTable) t).resolveColumn(rowType, typeFactory, id.names);
switch(entries.size()) {
case 1:
if (!entries.get(0).getValue().isEmpty()) {
return null;
}
return entries.get(0).getKey();
default:
return null;
}
}
Aggregations