Search in sources :

Example 21 with Field

use of io.crate.analyze.symbol.Field in project crate by crate.

the class FetchFieldExtractor method process.

public static Set<Field> process(List<Symbol> symbols, Multimap<AnalyzedRelation, Symbol> subOutputs) {
    HashSet<Field> canBeFetched = new HashSet<>();
    Context ctx = new Context(subOutputs, canBeFetched);
    for (Symbol symbol : symbols) {
        FieldVisitor.INSTANCE.process(symbol, ctx);
    }
    return canBeFetched;
}
Also used : Field(io.crate.analyze.symbol.Field) Symbol(io.crate.analyze.symbol.Symbol) HashSet(java.util.HashSet)

Example 22 with Field

use of io.crate.analyze.symbol.Field in project crate by crate.

the class FullQualifedNameFieldProvider method resolveField.

public Field resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation) {
    List<String> parts = qualifiedName.getParts();
    String columnSchema = null;
    String columnTableName = null;
    ColumnIdent columnIdent = new ColumnIdent(parts.get(parts.size() - 1), path);
    switch(parts.size()) {
        case 1:
            break;
        case 2:
            columnTableName = parts.get(0);
            break;
        case 3:
            columnSchema = parts.get(0);
            columnTableName = parts.get(1);
            break;
        default:
            throw new IllegalArgumentException("Column reference \"%s\" has too many parts. " + "A column reference can have at most 3 parts and must have one of the following formats:  " + "\"<column>\", \"<table>.<column>\" or \"<schema>.<table>.<column>\"");
    }
    boolean schemaMatched = false;
    boolean tableNameMatched = false;
    Field lastField = null;
    for (Map.Entry<QualifiedName, AnalyzedRelation> entry : sources.entrySet()) {
        List<String> sourceParts = entry.getKey().getParts();
        String sourceSchema = null;
        String sourceTableOrAlias;
        if (sourceParts.size() == 1) {
            sourceTableOrAlias = sourceParts.get(0);
        } else if (sourceParts.size() == 2) {
            sourceSchema = sourceParts.get(0);
            sourceTableOrAlias = sourceParts.get(1);
        } else {
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "sources key (QualifiedName) must have 1 or 2 parts, not %d", sourceParts.size()));
        }
        AnalyzedRelation sourceRelation = entry.getValue();
        if (columnSchema != null && sourceSchema != null && !columnSchema.equals(sourceSchema)) {
            continue;
        }
        schemaMatched = true;
        if (columnTableName != null && !sourceTableOrAlias.equals(columnTableName)) {
            continue;
        }
        tableNameMatched = true;
        Field newField = sourceRelation.getField(columnIdent, operation);
        if (newField != null) {
            if (lastField != null) {
                throw new AmbiguousColumnException(columnIdent);
            }
            lastField = newField;
        }
    }
    if (lastField == null) {
        if (!schemaMatched || !tableNameMatched) {
            throw RelationUnknownException.of(columnSchema, columnTableName);
        }
        throw new ColumnUnknownException(columnIdent.sqlFqn());
    }
    return lastField;
}
Also used : QualifiedName(io.crate.sql.tree.QualifiedName) ColumnIdent(io.crate.metadata.ColumnIdent) Field(io.crate.analyze.symbol.Field) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) AmbiguousColumnException(io.crate.exceptions.AmbiguousColumnException) Map(java.util.Map)

Example 23 with Field

use of io.crate.analyze.symbol.Field in project crate by crate.

the class NameFieldProvider method resolveField.

public Field resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation) {
    List<String> parts = qualifiedName.getParts();
    ColumnIdent columnIdent = new ColumnIdent(parts.get(parts.size() - 1), path);
    if (parts.size() != 1) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Column reference \"%s\" has too many parts. " + "A column must not have a schema or a table here.", qualifiedName));
    }
    Field field = relation.getField(columnIdent, operation);
    if (field == null) {
        throw new ColumnUnknownException(columnIdent.sqlFqn());
    }
    return field;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Field(io.crate.analyze.symbol.Field) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException)

Example 24 with Field

use of io.crate.analyze.symbol.Field in project crate by crate.

the class ResultToXContentBuilder method colTypes.

ResultToXContentBuilder colTypes(List<Field> fields) throws IOException {
    builder.startArray(FIELDS.COLUMN_TYPES);
    for (Field field : fields) {
        toXContentNestedDataType(builder, field.valueType());
    }
    builder.endArray();
    return this;
}
Also used : Field(io.crate.analyze.symbol.Field)

Aggregations

Field (io.crate.analyze.symbol.Field)24 CrateUnitTest (io.crate.test.integration.CrateUnitTest)14 Test (org.junit.Test)14 DummyRelation (io.crate.testing.DummyRelation)10 QualifiedName (io.crate.sql.tree.QualifiedName)8 Symbol (io.crate.analyze.symbol.Symbol)3 FullQualifedNameFieldProvider (io.crate.analyze.relations.FullQualifedNameFieldProvider)2 Function (io.crate.analyze.symbol.Function)2 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)2 ColumnIdent (io.crate.metadata.ColumnIdent)2 SymbolMatchers.isField (io.crate.testing.SymbolMatchers.isField)2 SessionContext (io.crate.action.sql.SessionContext)1 Analysis (io.crate.analyze.Analysis)1 QuerySpec (io.crate.analyze.QuerySpec)1 RelationSource (io.crate.analyze.RelationSource)1 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 TableRelation (io.crate.analyze.relations.TableRelation)1 AmbiguousColumnException (io.crate.exceptions.AmbiguousColumnException)1 TableInfo (io.crate.metadata.table.TableInfo)1 PGType (io.crate.protocols.postgres.types.PGType)1