Search in sources :

Example 6 with ColumnUnknownException

use of io.crate.exceptions.ColumnUnknownException in project crate by crate.

the class InsertAnalyzer method resolveTargetColumns.

private static Collection<Reference> resolveTargetColumns(Collection<String> targetColumnNames, DocTableInfo targetTable) {
    if (targetColumnNames.isEmpty()) {
        return targetTable.columns();
    }
    LinkedHashSet<Reference> columns = new LinkedHashSet<>(targetColumnNames.size());
    for (String targetColumnName : targetColumnNames) {
        ColumnIdent columnIdent = ColumnIdent.fromPath(targetColumnName);
        Reference reference = targetTable.getReference(columnIdent);
        Reference targetReference;
        if (reference == null) {
            DynamicReference dynamicReference = targetTable.getDynamic(columnIdent, true, true);
            if (dynamicReference == null) {
                throw new ColumnUnknownException(targetColumnName, targetTable.ident());
            }
            targetReference = dynamicReference;
        } else {
            targetReference = reference;
        }
        if (!columns.add(targetReference)) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "reference '%s' repeated", targetColumnName));
        }
    }
    return columns;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ColumnIdent(io.crate.metadata.ColumnIdent) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference) DynamicReference(io.crate.expression.symbol.DynamicReference) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference) DynamicReference(io.crate.expression.symbol.DynamicReference)

Example 7 with ColumnUnknownException

use of io.crate.exceptions.ColumnUnknownException in project crate by crate.

the class InsertAnalyzer method verifyOnConflictTargets.

private static void verifyOnConflictTargets(CoordinatorTxnCtx txnCtx, ExpressionAnalyzer expressionAnalyzer, DocTableInfo docTable, Insert.DuplicateKeyContext<Expression> duplicateKeyContext) {
    List<Expression> constraintColumns = duplicateKeyContext.getConstraintColumns();
    if (constraintColumns.isEmpty()) {
        return;
    }
    List<ColumnIdent> pkColumnIdents = docTable.primaryKey();
    if (constraintColumns.size() != pkColumnIdents.size()) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Number of conflict targets (%s) did not match the number of primary key columns (%s)", constraintColumns, pkColumnIdents));
    }
    ExpressionAnalysisContext ctx = new ExpressionAnalysisContext(txnCtx.sessionContext());
    List<Symbol> conflictTargets = Lists2.map(constraintColumns, x -> {
        try {
            return expressionAnalyzer.convert(x, ctx);
        } catch (ColumnUnknownException e) {
            // Going through ExpressionAnalyzer again to still have a "column must exist" validation
            if (x instanceof QualifiedNameReference) {
                QualifiedName name = ((QualifiedNameReference) x).getName();
                Expression subscriptExpression = MetadataToASTNodeResolver.expressionFromColumn(ColumnIdent.fromPath(name.toString()));
                return expressionAnalyzer.convert(subscriptExpression, ctx);
            }
            throw e;
        }
    });
    for (Symbol conflictTarget : conflictTargets) {
        if (!pkColumnIdents.contains(Symbols.pathFromSymbol(conflictTarget))) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Conflict target (%s) did not match the primary key columns (%s)", conflictTarget, pkColumnIdents));
        }
    }
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Expression(io.crate.sql.tree.Expression) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Symbol(io.crate.expression.symbol.Symbol) QualifiedName(io.crate.sql.tree.QualifiedName) QualifiedNameReference(io.crate.sql.tree.QualifiedNameReference)

Example 8 with ColumnUnknownException

use of io.crate.exceptions.ColumnUnknownException in project crate by crate.

the class TableReferenceResolver method resolveField.

@Override
public Reference 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));
    }
    for (Reference reference : tableReferences) {
        if (reference.ident().columnIdent().equals(columnIdent)) {
            if (reference instanceof GeneratedReference) {
                throw new IllegalArgumentException("A generated column cannot be based on a generated column");
            }
            references.add(reference);
            return reference;
        }
    }
    throw new ColumnUnknownException(columnIdent.sqlFqn());
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) GeneratedReference(io.crate.metadata.GeneratedReference) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) GeneratedReference(io.crate.metadata.GeneratedReference) Reference(io.crate.metadata.Reference)

Example 9 with ColumnUnknownException

use of io.crate.exceptions.ColumnUnknownException 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 10 with ColumnUnknownException

use of io.crate.exceptions.ColumnUnknownException 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)

Aggregations

ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)12 ColumnIdent (io.crate.metadata.ColumnIdent)10 Reference (io.crate.metadata.Reference)5 DynamicReference (io.crate.expression.symbol.DynamicReference)3 Symbol (io.crate.expression.symbol.Symbol)3 GeneratedReference (io.crate.metadata.GeneratedReference)3 DataType (io.crate.types.DataType)3 Map (java.util.Map)3 DynamicReference (io.crate.analyze.symbol.DynamicReference)2 Field (io.crate.analyze.symbol.Field)2 AmbiguousColumnException (io.crate.exceptions.AmbiguousColumnException)2 ColumnValidationException (io.crate.exceptions.ColumnValidationException)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 QualifiedName (io.crate.sql.tree.QualifiedName)2 QualifiedNameReference (io.crate.sql.tree.QualifiedNameReference)2 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)1 RelationUnknown (io.crate.exceptions.RelationUnknown)1 VoidReference (io.crate.expression.symbol.VoidReference)1 IndexReference (io.crate.metadata.IndexReference)1 ReferenceIdent (io.crate.metadata.ReferenceIdent)1