Search in sources :

Example 51 with DataType

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

the class ArrayMaxFunctionTest method test_array_returns_max_element.

@Test
public void test_array_returns_max_element() {
    List<DataType> typesToTest = new ArrayList(DataTypes.PRIMITIVE_TYPES);
    typesToTest.add(DataTypes.NUMERIC);
    for (DataType type : typesToTest) {
        var valuesToTest = TestingHelpers.getRandomsOfType(2, 10, type);
        var optional = valuesToTest.stream().filter(o -> o != null).max((o1, o2) -> type.compare(o1, o2));
        var expected = optional.orElse(null);
        String expression = String.format(Locale.ENGLISH, "array_max(?::%s[])", type.getName());
        assertEvaluate(expression, expected, Literal.of(valuesToTest, new ArrayType<>(type)));
    }
}
Also used : Asserts.assertThrowsMatches(io.crate.testing.Asserts.assertThrowsMatches) List(java.util.List) TestingHelpers(io.crate.testing.TestingHelpers) Literal(io.crate.expression.symbol.Literal) DataTypes(io.crate.types.DataTypes) Locale(java.util.Locale) DataType(io.crate.types.DataType) Test(org.junit.Test) ArrayType(io.crate.types.ArrayType) ArrayList(java.util.ArrayList) ArrayType(io.crate.types.ArrayType) ArrayList(java.util.ArrayList) DataType(io.crate.types.DataType) Test(org.junit.Test)

Example 52 with DataType

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

the class DocIndexMetadataTest method testCreateTableMappingGenerationAndParsingArrayInsideObject.

@Test
public void testCreateTableMappingGenerationAndParsingArrayInsideObject() throws Exception {
    DocIndexMetadata md = getDocIndexMetadataFromStatement("create table t1 (" + "id int primary key," + "details object as (names array(string))" + ") with (number_of_replicas=0)");
    DataType type = md.references().get(new ColumnIdent("details", "names")).valueType();
    assertThat(type, Matchers.equalTo(new ArrayType(DataTypes.STRING)));
}
Also used : ArrayType(io.crate.types.ArrayType) ColumnIdent(io.crate.metadata.ColumnIdent) DataType(io.crate.types.DataType) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 53 with DataType

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

the class InsertFromSubQueryAnalyzedStatement method rewriteNestedInputToSubscript.

private Symbol rewriteNestedInputToSubscript(ColumnIdent columnIdent, Symbol inputSymbol) {
    Reference reference = tableInfo().getReference(columnIdent);
    Symbol symbol = inputSymbol;
    Iterator<String> pathIt = columnIdent.path().iterator();
    while (pathIt.hasNext()) {
        // rewrite object access to subscript scalar
        String key = pathIt.next();
        DataType returnType = DataTypes.OBJECT;
        if (!pathIt.hasNext()) {
            returnType = reference.valueType();
        }
        FunctionIdent functionIdent = new FunctionIdent(SubscriptObjectFunction.NAME, ImmutableList.<DataType>of(DataTypes.OBJECT, DataTypes.STRING));
        symbol = new Function(new FunctionInfo(functionIdent, returnType), Arrays.asList(symbol, Literal.of(key)));
    }
    return symbol;
}
Also used : SubscriptObjectFunction(io.crate.operation.scalar.SubscriptObjectFunction) DataType(io.crate.types.DataType)

Example 54 with DataType

use of io.crate.types.DataType 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) {
        if (columnProperties.containsKey("properties")) {
            type = DataTypes.OBJECT;
        } else {
            return DataTypes.NOT_SUPPORTED;
        }
    } else if (typeName.equalsIgnoreCase("array")) {
        Map<String, Object> innerProperties = getNested(columnProperties, "inner");
        DataType innerType = getColumnDataType(innerProperties);
        type = new ArrayType(innerType);
    } else {
        typeName = typeName.toLowerCase(Locale.ENGLISH);
        type = MoreObjects.firstNonNull(DataTypes.ofMappingName(typeName), DataTypes.NOT_SUPPORTED);
    }
    return type;
}
Also used : ArrayType(io.crate.types.ArrayType) DataType(io.crate.types.DataType)

Example 55 with DataType

use of io.crate.types.DataType 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;
    }
    for (Map.Entry<String, Object> columnEntry : propertiesMap.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);
        Reference.IndexType columnIndexType = getColumnIndexType(columnProperties);
        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(newIdent, geoTree, precision, treeLevels, distanceErrorPct);
        } else if (columnDataType == DataTypes.OBJECT || (columnDataType.id() == ArrayType.ID && ((ArrayType) columnDataType).innerType() == DataTypes.OBJECT)) {
            ColumnPolicy columnPolicy = ColumnPolicy.of(columnProperties.get("dynamic"));
            add(newIdent, columnDataType, columnPolicy, Reference.IndexType.NO, false, nullable);
            if (columnProperties.get("properties") != null) {
                // walk nested
                internalExtractColumnDefinitions(newIdent, (Map<String, Object>) columnProperties.get("properties"));
            }
        } else if (columnDataType != DataTypes.NOT_SUPPORTED) {
            List<String> copyToColumns = getNested(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(newIdent, columnDataType, ColumnPolicy.DYNAMIC, columnIndexType, false));
                }
            }
            // is it an index?
            if (indicesMap.containsKey(newIdent.fqn())) {
                IndexReference.Builder builder = getOrCreateIndexBuilder(newIdent);
                builder.indexType(columnIndexType).analyzer((String) columnProperties.get("analyzer"));
            } else {
                add(newIdent, columnDataType, columnIndexType, nullable);
            }
        }
    }
}
Also used : ColumnPolicy(io.crate.metadata.table.ColumnPolicy) DataType(io.crate.types.DataType)

Aggregations

DataType (io.crate.types.DataType)95 ArrayType (io.crate.types.ArrayType)35 Test (org.junit.Test)33 ArrayList (java.util.ArrayList)17 Map (java.util.Map)17 CrateUnitTest (io.crate.test.integration.CrateUnitTest)14 List (java.util.List)12 Symbol (io.crate.expression.symbol.Symbol)11 Literal (io.crate.expression.symbol.Literal)9 ColumnIdent (io.crate.metadata.ColumnIdent)9 HashMap (java.util.HashMap)9 FunctionIdent (io.crate.metadata.FunctionIdent)8 NodeContext (io.crate.metadata.NodeContext)8 Reference (io.crate.metadata.Reference)8 Row (io.crate.data.Row)7 Symbols (io.crate.expression.symbol.Symbols)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)7 Locale (java.util.Locale)7 Lists2 (io.crate.common.collections.Lists2)6 FunctionInfo (io.crate.metadata.FunctionInfo)6