Search in sources :

Example 26 with ArrayType

use of io.crate.types.ArrayType in project crate by crate.

the class Function method castArrayElements.

private Symbol castArrayElements(DataType<?> newDataType, CastMode... modes) {
    DataType<?> innerType = ((ArrayType<?>) newDataType).innerType();
    ArrayList<Symbol> newArgs = new ArrayList<>(arguments.size());
    for (Symbol arg : arguments) {
        try {
            newArgs.add(arg.cast(innerType, modes));
        } catch (ConversionException e) {
            throw new ConversionException(returnType, newDataType);
        }
    }
    return new Function(signature, newArgs, newDataType, null);
}
Also used : ArrayType(io.crate.types.ArrayType) ConversionException(io.crate.exceptions.ConversionException) SubscriptObjectFunction(io.crate.expression.scalar.SubscriptObjectFunction) CurrentTimestampFunction(io.crate.expression.scalar.timestamp.CurrentTimestampFunction) SubscriptRecordFunction(io.crate.expression.scalar.SubscriptRecordFunction) CurrentSchemaFunction(io.crate.expression.scalar.systeminformation.CurrentSchemaFunction) ArrayFunction(io.crate.expression.scalar.arithmetic.ArrayFunction) CurrentTimeFunction(io.crate.expression.scalar.timestamp.CurrentTimeFunction) ImplicitCastFunction(io.crate.expression.scalar.cast.ImplicitCastFunction) ExplicitCastFunction(io.crate.expression.scalar.cast.ExplicitCastFunction) SubscriptFunction(io.crate.expression.scalar.SubscriptFunction) TryCastFunction(io.crate.expression.scalar.cast.TryCastFunction) CurrentSchemasFunction(io.crate.expression.scalar.systeminformation.CurrentSchemasFunction) ArrayList(java.util.ArrayList)

Example 27 with ArrayType

use of io.crate.types.ArrayType in project crate by crate.

the class ArraySumFunction method register.

public static void register(ScalarFunctionModule module) {
    module.register(Signature.scalar(NAME, new ArrayType(DataTypes.NUMERIC).getTypeSignature(), DataTypes.NUMERIC.getTypeSignature()), ArraySumFunction::new);
    for (var supportedType : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
        DataType inputDependantOutputType = DataTypes.LONG;
        if (supportedType == DataTypes.FLOAT || supportedType == DataTypes.DOUBLE) {
            inputDependantOutputType = supportedType;
        }
        module.register(Signature.scalar(NAME, new ArrayType(supportedType).getTypeSignature(), inputDependantOutputType.getTypeSignature()), ArraySumFunction::new);
    }
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType)

Example 28 with ArrayType

use of io.crate.types.ArrayType in project crate by crate.

the class ArrayAvgFunction method register.

public static void register(ScalarFunctionModule module) {
    // All types except float and double have numeric average
    // https://www.postgresql.org/docs/13/functions-aggregate.html
    module.register(Signature.scalar(NAME, new ArrayType(DataTypes.NUMERIC).getTypeSignature(), DataTypes.NUMERIC.getTypeSignature()), (signature, boundSignature) -> new UnaryScalar<>(signature, boundSignature, new ArrayType(DataTypes.NUMERIC), Operations.NUMERIC.getFunction()));
    module.register(Signature.scalar(NAME, new ArrayType(DataTypes.FLOAT).getTypeSignature(), DataTypes.FLOAT.getTypeSignature()), (signature, boundSignature) -> new UnaryScalar<>(signature, boundSignature, new ArrayType(DataTypes.FLOAT), Operations.FLOAT.getFunction()));
    module.register(Signature.scalar(NAME, new ArrayType(DataTypes.DOUBLE).getTypeSignature(), DataTypes.DOUBLE.getTypeSignature()), (signature, boundSignature) -> new UnaryScalar<>(signature, boundSignature, new ArrayType(DataTypes.DOUBLE), Operations.DOUBLE.getFunction()));
    for (var supportedType : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
        if (supportedType != DataTypes.FLOAT && supportedType != DataTypes.DOUBLE) {
            module.register(Signature.scalar(NAME, new ArrayType(supportedType).getTypeSignature(), DataTypes.NUMERIC.getTypeSignature()), (signature, boundSignature) -> new UnaryScalar<>(signature, boundSignature, new ArrayType(supportedType), Operations.INTEGRAL_PRIMITIVE.getFunction()));
        }
    }
}
Also used : ArrayType(io.crate.types.ArrayType)

Example 29 with ArrayType

use of io.crate.types.ArrayType in project crate by crate.

the class DocIndexMetadata method internalExtractColumnDefinitions.

/**
 * extracts index definitions as well
 */
@SuppressWarnings("unchecked")
private void internalExtractColumnDefinitions(@Nullable ColumnIdent columnIdent, @Nullable Map<String, Object> propertiesMap) {
    if (propertiesMap == null) {
        return;
    }
    var columns = propertiesMap.entrySet().stream().sorted(SORT_BY_POSITION_THEN_NAME).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
    for (Map.Entry<String, Object> columnEntry : columns.entrySet()) {
        Map<String, Object> columnProperties = (Map) columnEntry.getValue();
        DataType columnDataType = getColumnDataType(columnProperties);
        ColumnIdent newIdent = childIdent(columnIdent, columnEntry.getKey());
        boolean nullable = !notNullColumns.contains(newIdent);
        columnProperties = furtherColumnProperties(columnProperties);
        int position = columnPosition((int) columnProperties.getOrDefault("position", 0));
        String defaultExpression = (String) columnProperties.getOrDefault("default_expr", null);
        Reference.IndexType columnIndexType = getColumnIndexType(columnProperties);
        StorageSupport storageSupport = columnDataType.storageSupport();
        assert storageSupport != null : "DataType used in table definition must have storage support: " + columnDataType;
        boolean docValuesDefault = storageSupport.getComputedDocValuesDefault(columnIndexType);
        boolean hasDocValues = Booleans.parseBoolean(columnProperties.getOrDefault(DOC_VALUES, docValuesDefault).toString());
        if (columnDataType == DataTypes.GEO_SHAPE) {
            String geoTree = (String) columnProperties.get("tree");
            String precision = (String) columnProperties.get("precision");
            Integer treeLevels = (Integer) columnProperties.get("tree_levels");
            Double distanceErrorPct = (Double) columnProperties.get("distance_error_pct");
            addGeoReference(position, newIdent, geoTree, precision, treeLevels, distanceErrorPct);
        } else if (columnDataType.id() == ObjectType.ID || (columnDataType.id() == ArrayType.ID && ((ArrayType) columnDataType).innerType().id() == ObjectType.ID)) {
            ColumnPolicy columnPolicy = ColumnPolicies.decodeMappingValue(columnProperties.get("dynamic"));
            add(position, newIdent, columnDataType, defaultExpression, columnPolicy, Reference.IndexType.NONE, nullable, hasDocValues);
            if (columnProperties.get("properties") != null) {
                // walk nested
                internalExtractColumnDefinitions(newIdent, (Map<String, Object>) columnProperties.get("properties"));
            }
        } else if (columnDataType != DataTypes.NOT_SUPPORTED) {
            List<String> copyToColumns = Maps.get(columnProperties, "copy_to");
            // extract columns this column is copied to, needed for indices
            if (copyToColumns != null) {
                for (String copyToColumn : copyToColumns) {
                    ColumnIdent targetIdent = ColumnIdent.fromPath(copyToColumn);
                    IndexReference.Builder builder = getOrCreateIndexBuilder(targetIdent);
                    builder.addColumn(newInfo(position, newIdent, columnDataType, defaultExpression, ColumnPolicy.DYNAMIC, columnIndexType, false, hasDocValues));
                }
            }
            // is it an index?
            if (indicesMap.containsKey(newIdent.fqn())) {
                IndexReference.Builder builder = getOrCreateIndexBuilder(newIdent);
                builder.indexType(columnIndexType).analyzer((String) columnProperties.get("analyzer"));
            } else {
                add(position, newIdent, columnDataType, defaultExpression, ColumnPolicy.DYNAMIC, columnIndexType, nullable, hasDocValues);
            }
        }
    }
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) RelationName(io.crate.metadata.RelationName) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) FieldProvider(io.crate.analyze.relations.FieldProvider) TableReferenceResolver(io.crate.analyze.expressions.TableReferenceResolver) ArrayType(io.crate.types.ArrayType) Settings(org.elasticsearch.common.settings.Settings) Locale(java.util.Locale) Map(java.util.Map) StorageSupport(io.crate.types.StorageSupport) MapBuilder(io.crate.common.collections.MapBuilder) NodeContext(io.crate.metadata.NodeContext) GeneratedReference(io.crate.metadata.GeneratedReference) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Lists2(io.crate.common.collections.Lists2) CheckConstraint(io.crate.sql.tree.CheckConstraint) Booleans(io.crate.common.Booleans) Objects(java.util.Objects) List(java.util.List) Version(org.elasticsearch.Version) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata) Symbol(io.crate.expression.symbol.Symbol) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) DataTypes(io.crate.types.DataTypes) ReferenceIdent(io.crate.metadata.ReferenceIdent) GeoReference(io.crate.metadata.GeoReference) Expression(io.crate.sql.tree.Expression) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) ExpressionAnalyzer(io.crate.analyze.expressions.ExpressionAnalyzer) NumberOfReplicas(io.crate.analyze.NumberOfReplicas) HashMap(java.util.HashMap) ObjectType(io.crate.types.ObjectType) Operation(io.crate.metadata.table.Operation) IndexReference(io.crate.metadata.IndexReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ColumnPolicy(io.crate.sql.tree.ColumnPolicy) SqlParser(io.crate.sql.parser.SqlParser) Nullable(javax.annotation.Nullable) ColumnPolicies(io.crate.metadata.table.ColumnPolicies) StringType(io.crate.types.StringType) ExpressionAnalysisContext(io.crate.analyze.expressions.ExpressionAnalysisContext) Maps(io.crate.common.collections.Maps) DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) ColumnIdent(io.crate.metadata.ColumnIdent) Reference(io.crate.metadata.Reference) DataType(io.crate.types.DataType) BitStringFieldMapper(org.elasticsearch.index.mapper.BitStringFieldMapper) IOException(java.io.IOException) DOC_VALUES(org.elasticsearch.index.mapper.TypeParsers.DOC_VALUES) RowGranularity(io.crate.metadata.RowGranularity) BitStringType(io.crate.types.BitStringType) Comparator(java.util.Comparator) Collections(java.util.Collections) ColumnPolicy(io.crate.sql.tree.ColumnPolicy) GeneratedReference(io.crate.metadata.GeneratedReference) GeoReference(io.crate.metadata.GeoReference) IndexReference(io.crate.metadata.IndexReference) Reference(io.crate.metadata.Reference) CheckConstraint(io.crate.sql.tree.CheckConstraint) ArrayType(io.crate.types.ArrayType) ColumnIdent(io.crate.metadata.ColumnIdent) StorageSupport(io.crate.types.StorageSupport) DataType(io.crate.types.DataType) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IndexReference(io.crate.metadata.IndexReference)

Example 30 with ArrayType

use of io.crate.types.ArrayType in project crate by crate.

the class DocIndexMetadata method getColumnDataType.

/**
 * extract dataType from given columnProperties
 *
 * @param columnProperties map of String to Object containing column properties
 * @return dataType of the column with columnProperties
 */
public static DataType<?> getColumnDataType(Map<String, Object> columnProperties) {
    DataType<?> type;
    String typeName = (String) columnProperties.get("type");
    if (typeName == null || ObjectType.NAME.equals(typeName)) {
        Map<String, Object> innerProperties = (Map<String, Object>) columnProperties.get("properties");
        if (innerProperties != null) {
            ObjectType.Builder builder = ObjectType.builder();
            for (Map.Entry<String, Object> entry : innerProperties.entrySet()) {
                builder.setInnerType(entry.getKey(), getColumnDataType((Map<String, Object>) entry.getValue()));
            }
            type = builder.build();
        } else {
            type = Objects.requireNonNullElse(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
        }
    } else if (typeName.equalsIgnoreCase("array")) {
        Map<String, Object> innerProperties = Maps.get(columnProperties, "inner");
        DataType<?> innerType = getColumnDataType(innerProperties);
        type = new ArrayType<>(innerType);
    } else {
        typeName = typeName.toLowerCase(Locale.ENGLISH);
        switch(typeName) {
            case DateFieldMapper.CONTENT_TYPE:
                Boolean ignoreTimezone = (Boolean) columnProperties.get("ignore_timezone");
                if (ignoreTimezone != null && ignoreTimezone) {
                    return DataTypes.TIMESTAMP;
                } else {
                    return DataTypes.TIMESTAMPZ;
                }
            case KeywordFieldMapper.CONTENT_TYPE:
                Integer lengthLimit = (Integer) columnProperties.get("length_limit");
                return lengthLimit != null ? StringType.of(lengthLimit) : DataTypes.STRING;
            case BitStringFieldMapper.CONTENT_TYPE:
                Integer length = (Integer) columnProperties.get("length");
                assert length != null : "Length is required for bit string type";
                return new BitStringType(length);
            default:
                type = Objects.requireNonNullElse(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
        }
    }
    return type;
}
Also used : BitStringType(io.crate.types.BitStringType) ArrayType(io.crate.types.ArrayType) ObjectType(io.crate.types.ObjectType) DataType(io.crate.types.DataType) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ArrayType (io.crate.types.ArrayType)76 Test (org.junit.Test)53 DataType (io.crate.types.DataType)35 CrateUnitTest (io.crate.test.integration.CrateUnitTest)20 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)15 Symbol (io.crate.expression.symbol.Symbol)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 BytesRef (org.apache.lucene.util.BytesRef)8 DocTableInfo (io.crate.metadata.doc.DocTableInfo)7 Literal (io.crate.analyze.symbol.Literal)6 Literal (io.crate.expression.symbol.Literal)6 ColumnIdent (io.crate.metadata.ColumnIdent)6 Input (io.crate.data.Input)5 HashMap (java.util.HashMap)5 Function (io.crate.analyze.symbol.Function)4 Reference (io.crate.metadata.Reference)4 DataTypes (io.crate.types.DataTypes)4 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 TransactionContext (io.crate.metadata.TransactionContext)3