Search in sources :

Example 1 with ColumnUnknownException

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

the class InsertFromSubQueryAnalyzer method resolveTargetColumns.

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

Example 2 with ColumnUnknownException

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

the class AnalyzedTableElements method changeToPartitionedByColumn.

void changeToPartitionedByColumn(ColumnIdent partitionedByIdent, boolean skipIfNotFound) {
    Preconditions.checkArgument(!partitionedByIdent.name().startsWith("_"), "Cannot use system columns in PARTITIONED BY clause");
    // need to call primaryKeys() before the partition column is removed from the columns list
    if (!primaryKeys().isEmpty() && !primaryKeys().contains(partitionedByIdent.fqn())) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot use non primary key column '%s' in PARTITIONED BY clause if primary key is set on table", partitionedByIdent.sqlFqn()));
    }
    AnalyzedColumnDefinition columnDefinition = columnDefinitionByIdent(partitionedByIdent);
    if (columnDefinition == null) {
        if (skipIfNotFound) {
            return;
        }
        throw new ColumnUnknownException(partitionedByIdent.sqlFqn());
    }
    DataType columnType = DataTypes.ofMappingNameSafe(columnDefinition.dataType());
    if (!DataTypes.isPrimitive(columnType)) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot use column %s of type %s in PARTITIONED BY clause", columnDefinition.ident().sqlFqn(), columnDefinition.dataType()));
    }
    if (columnDefinition.isArrayOrInArray()) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot use array column %s in PARTITIONED BY clause", columnDefinition.ident().sqlFqn()));
    }
    if (columnDefinition.indexConstraint().equals("analyzed")) {
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot use column %s with fulltext index in PARTITIONED BY clause", columnDefinition.ident().sqlFqn()));
    }
    columnIdents.remove(columnDefinition.ident());
    columnDefinition.indexConstraint(Reference.IndexType.NO.toString());
    partitionedByColumns.add(columnDefinition);
}
Also used : ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) DataType(io.crate.types.DataType)

Example 3 with ColumnUnknownException

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

the class ValueNormalizer method normalizeObjectValue.

@SuppressWarnings("unchecked")
private void normalizeObjectValue(Map<String, Object> value, Reference info) {
    for (Map.Entry<String, Object> entry : value.entrySet()) {
        AnalyzedColumnDefinition.validateName(entry.getKey());
        ColumnIdent nestedIdent = ColumnIdent.getChild(info.ident().columnIdent(), entry.getKey());
        TableInfo tableInfo = schemas.getTableInfo(info.ident().tableIdent());
        Reference nestedInfo = tableInfo.getReference(nestedIdent);
        if (nestedInfo == null) {
            if (info.columnPolicy() == ColumnPolicy.IGNORED) {
                continue;
            }
            DynamicReference dynamicReference = null;
            if (tableInfo instanceof DocTableInfo) {
                dynamicReference = ((DocTableInfo) tableInfo).getDynamic(nestedIdent, true);
            }
            if (dynamicReference == null) {
                throw new ColumnUnknownException(nestedIdent.sqlFqn());
            }
            DataType type = DataTypes.guessType(entry.getValue());
            if (type == null) {
                throw new ColumnValidationException(info.ident().columnIdent().sqlFqn(), "Invalid value");
            }
            dynamicReference.valueType(type);
            nestedInfo = dynamicReference;
        } else {
            if (entry.getValue() == null) {
                continue;
            }
        }
        if (nestedInfo.valueType() == DataTypes.OBJECT && entry.getValue() instanceof Map) {
            normalizeObjectValue((Map<String, Object>) entry.getValue(), nestedInfo);
        } else if (isObjectArray(nestedInfo.valueType()) && entry.getValue() instanceof Object[]) {
            normalizeObjectArrayValue((Object[]) entry.getValue(), nestedInfo);
        } else {
            entry.setValue(normalizePrimitiveValue(entry.getValue(), nestedInfo));
        }
    }
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) DocTableInfo(io.crate.metadata.doc.DocTableInfo) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) DynamicReference(io.crate.analyze.symbol.DynamicReference) Reference(io.crate.metadata.Reference) DynamicReference(io.crate.analyze.symbol.DynamicReference) DataType(io.crate.types.DataType) DocTableInfo(io.crate.metadata.doc.DocTableInfo) TableInfo(io.crate.metadata.table.TableInfo) ColumnValidationException(io.crate.exceptions.ColumnValidationException) Map(java.util.Map)

Example 4 with ColumnUnknownException

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

the class FullQualifiedNameFieldProvider method resolveField.

@Override
public Symbol resolveField(QualifiedName qualifiedName, @Nullable List<String> path, Operation operation, boolean errorOnUnknownObjectKey) {
    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;
    Symbol lastField = null;
    for (var entry : sources.entrySet()) {
        RelationName relName = entry.getKey();
        String sourceSchema = relName.schema();
        String sourceTableOrAlias = relName.name();
        if (columnSchema != null && !columnSchema.equals(sourceSchema)) {
            continue;
        }
        schemaMatched = true;
        if (columnTableName != null && !sourceTableOrAlias.equals(columnTableName)) {
            continue;
        }
        tableNameMatched = true;
        AnalyzedRelation sourceRelation = entry.getValue();
        Symbol newField = sourceRelation.getField(columnIdent, operation, errorOnUnknownObjectKey);
        if (newField != null) {
            if (lastField != null) {
                if (errorOnUnknownObjectKey == false) {
                    /* ex) CREATE TABLE c1 (obj object as (x int));
                         *     CREATE TABLE c2 (obj object as (y int));
                         *     select obj['x'] from c1, c2;
                         *     --> ambiguous because c2.obj['x'] is another candidate with errorOnUnknownObjectKey = false
                         */
                    return resolveField(qualifiedName, path, operation, true);
                }
                throw new AmbiguousColumnException(columnIdent, newField);
            }
            lastField = newField;
        }
    }
    if (lastField == null) {
        if (!schemaMatched || !tableNameMatched) {
            String schema = columnSchema == null ? defaultSchema : columnSchema;
            raiseUnsupportedFeatureIfInParentScope(columnSchema, columnTableName, schema);
            RelationName relationName = new RelationName(schema, columnTableName);
            throw new RelationUnknown(relationName);
        }
        RelationName relationName = sources.entrySet().iterator().next().getKey();
        throw new ColumnUnknownException(columnIdent.sqlFqn(), relationName);
    }
    return lastField;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) RelationUnknown(io.crate.exceptions.RelationUnknown) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Symbol(io.crate.expression.symbol.Symbol) RelationName(io.crate.metadata.RelationName) AmbiguousColumnException(io.crate.exceptions.AmbiguousColumnException)

Example 5 with ColumnUnknownException

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

the class ValueNormalizer method normalizeObjectValue.

@SuppressWarnings("unchecked")
private static void normalizeObjectValue(Map<String, Object> value, Reference info, TableInfo tableInfo) {
    for (Map.Entry<String, Object> entry : value.entrySet()) {
        ColumnIdent nestedIdent = ColumnIdent.getChildSafe(info.column(), entry.getKey());
        Reference nestedInfo = tableInfo.getReference(nestedIdent);
        if (nestedInfo == null) {
            if (info.columnPolicy() == ColumnPolicy.IGNORED) {
                continue;
            }
            DynamicReference dynamicReference = null;
            if (tableInfo instanceof DocTableInfo) {
                dynamicReference = ((DocTableInfo) tableInfo).getDynamic(nestedIdent, true, true);
            }
            if (dynamicReference == null) {
                throw new ColumnUnknownException(nestedIdent.sqlFqn(), tableInfo.ident());
            }
            DataType type = DataTypes.guessType(entry.getValue());
            if (type == null) {
                throw new ColumnValidationException(info.column().sqlFqn(), tableInfo.ident(), "Invalid value");
            }
            dynamicReference.valueType(type);
            nestedInfo = dynamicReference;
        } else {
            if (entry.getValue() == null) {
                continue;
            }
        }
        if (nestedInfo.valueType().id() == ObjectType.ID && entry.getValue() instanceof Map) {
            normalizeObjectValue((Map<String, Object>) entry.getValue(), nestedInfo, tableInfo);
        } else if (isObjectArray(nestedInfo.valueType()) && entry.getValue() instanceof List) {
            normalizeObjectArrayValue((List<Map<String, Object>>) entry.getValue(), nestedInfo, tableInfo);
        } else {
            entry.setValue(normalizePrimitiveValue(entry.getValue(), nestedInfo));
        }
    }
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) DocTableInfo(io.crate.metadata.doc.DocTableInfo) ColumnUnknownException(io.crate.exceptions.ColumnUnknownException) Reference(io.crate.metadata.Reference) DynamicReference(io.crate.expression.symbol.DynamicReference) DynamicReference(io.crate.expression.symbol.DynamicReference) DataType(io.crate.types.DataType) List(java.util.List) ColumnValidationException(io.crate.exceptions.ColumnValidationException) Map(java.util.Map)

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