Search in sources :

Example 11 with ColumnIdent

use of io.crate.metadata.ColumnIdent in project crate by crate.

the class PartitionPropertiesAnalyzer method toPartitionName.

public static PartitionName toPartitionName(DocTableInfo tableInfo, List<Assignment> partitionProperties, Row parameters) {
    Preconditions.checkArgument(tableInfo.isPartitioned(), "table '%s' is not partitioned", tableInfo.ident().fqn());
    Preconditions.checkArgument(partitionProperties.size() == tableInfo.partitionedBy().size(), "The table \"%s\" is partitioned by %s columns but the PARTITION clause contains %s columns", tableInfo.ident().fqn(), tableInfo.partitionedBy().size(), partitionProperties.size());
    Map<ColumnIdent, Object> properties = assignmentsToMap(partitionProperties, parameters);
    BytesRef[] values = new BytesRef[properties.size()];
    for (Map.Entry<ColumnIdent, Object> entry : properties.entrySet()) {
        Object value = entry.getValue();
        int idx = tableInfo.partitionedBy().indexOf(entry.getKey());
        try {
            Reference reference = tableInfo.partitionedByColumns().get(idx);
            Object converted = reference.valueType().value(value);
            values[idx] = converted == null ? null : DataTypes.STRING.value(converted);
        } catch (IndexOutOfBoundsException ex) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "\"%s\" is no known partition column", entry.getKey().sqlFqn()));
        }
    }
    return new PartitionName(tableInfo.ident(), Arrays.asList(values));
}
Also used : PartitionName(io.crate.metadata.PartitionName) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) BytesRef(org.apache.lucene.util.BytesRef)

Example 12 with ColumnIdent

use of io.crate.metadata.ColumnIdent in project crate by crate.

the class AnalyzedColumnDefinition method name.

public void name(String name) {
    validateName(name);
    this.name = name;
    if (this.parent != null) {
        this.ident = ColumnIdent.getChild(this.parent.ident, name);
    } else {
        this.ident = new ColumnIdent(name);
    }
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent)

Example 13 with ColumnIdent

use of io.crate.metadata.ColumnIdent 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 14 with ColumnIdent

use of io.crate.metadata.ColumnIdent in project crate by crate.

the class AbstractTableRelation method checkNestedArray.

protected Reference checkNestedArray(ColumnIdent ci, Reference reference) {
    // TODO: build type correctly as array when the tableInfo is created and remove the conversion here
    DataType dataType = null;
    ColumnIdent tmpCI = ci;
    Reference tmpRI = reference;
    while (!tmpCI.isColumn() && hasMatchingParent(tmpRI, IS_OBJECT_ARRAY)) {
        if (DataTypes.isCollectionType(reference.valueType())) {
            // TODO: remove this limitation with next type refactoring
            throw new UnsupportedOperationException("cannot query for arrays inside object arrays explicitly");
        }
        // return references of primitive types as array
        if (dataType == null) {
            dataType = new ArrayType(reference.valueType());
            if (hasNestedObjectReference(tmpRI))
                break;
        } else {
            dataType = new ArrayType(dataType);
        }
        tmpCI = tmpCI.getParent();
        tmpRI = tableInfo.getReference(tmpCI);
    }
    if (dataType != null) {
        return new Reference(reference.ident(), reference.granularity(), dataType, reference.columnPolicy(), reference.indexType(), reference.isNullable());
    } else {
        return reference;
    }
}
Also used : ArrayType(io.crate.types.ArrayType) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) DataType(io.crate.types.DataType)

Example 15 with ColumnIdent

use of io.crate.metadata.ColumnIdent in project crate by crate.

the class AbstractTableRelation method getField.

@Nullable
public Field getField(Path path) {
    ColumnIdent ci = toColumnIdent(path);
    Reference reference = tableInfo.getReference(ci);
    if (reference == null) {
        return null;
    }
    reference = checkNestedArray(ci, reference);
    return allocate(ci, reference);
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) Nullable(javax.annotation.Nullable)

Aggregations

ColumnIdent (io.crate.metadata.ColumnIdent)34 Reference (io.crate.metadata.Reference)15 CrateUnitTest (io.crate.test.integration.CrateUnitTest)12 Test (org.junit.Test)12 Map (java.util.Map)5 ColumnUnknownException (io.crate.exceptions.ColumnUnknownException)4 GeneratedReference (io.crate.metadata.GeneratedReference)4 NestedObjectExpression (io.crate.operation.reference.NestedObjectExpression)3 TestingHelpers.mapToSortedString (io.crate.testing.TestingHelpers.mapToSortedString)3 ArrayType (io.crate.types.ArrayType)3 HashMap (java.util.HashMap)3 Field (io.crate.analyze.symbol.Field)2 Symbol (io.crate.analyze.symbol.Symbol)2 ColumnValidationException (io.crate.exceptions.ColumnValidationException)2 DocTableInfo (io.crate.metadata.doc.DocTableInfo)2 FileUriCollectPhase (io.crate.planner.node.dql.FileUriCollectPhase)2 DataType (io.crate.types.DataType)2 BytesRef (org.apache.lucene.util.BytesRef)2 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1