Search in sources :

Example 1 with CustomColumnResolvingTable

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);
                }
            }
        }
    }
}
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 2 with CustomColumnResolvingTable

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;
    }
}
Also used : CustomColumnResolvingTable(org.apache.calcite.schema.CustomColumnResolvingTable) Table(org.apache.calcite.schema.Table) ExtensibleTable(org.apache.calcite.schema.ExtensibleTable) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelOptTable(org.apache.calcite.plan.RelOptTable) CustomColumnResolvingTable(org.apache.calcite.schema.CustomColumnResolvingTable) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) Pair(org.apache.calcite.util.Pair)

Aggregations

CustomColumnResolvingTable (org.apache.calcite.schema.CustomColumnResolvingTable)2 Table (org.apache.calcite.schema.Table)2 Pair (org.apache.calcite.util.Pair)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 RelOptTable (org.apache.calcite.plan.RelOptTable)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)1 ExtensibleTable (org.apache.calcite.schema.ExtensibleTable)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 SqlOperatorTable (org.apache.calcite.sql.SqlOperatorTable)1 SqlStdOperatorTable (org.apache.calcite.sql.fun.SqlStdOperatorTable)1