Java Code Examples for io.crate.types.DataType#value()

The following examples show how to use io.crate.types.DataType#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InsertFromValues.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Row cast(Row row, List<Reference> columnReferences, DocTableInfo tableInfo) {
    if (row == null) {
        return null;
    }
    Object[] cells = new Object[row.numColumns()];
    for (int i = 0; i < cells.length; i++) {
        Reference reference = columnReferences.get(i);
        DataType<?> targetType = reference.valueType();
        Object value = row.get(i);
        try {
            cells[i] = targetType.value(value);
        } catch (IllegalArgumentException | ClassCastException e) {
            throw new ColumnValidationException(
                reference.column().name(),
                tableInfo.ident(),
                "Invalid value '" + value + "' for type '" + targetType + "'");
        }
    }
    return new RowN(cells);
}
 
Example 2
Source File: PercentileAggregationTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllTypesReturnSameResult() throws Exception {
    for (DataType valueType : DataTypes.NUMERIC_PRIMITIVE_TYPES) {
        List<Double> fractions = Arrays.asList(0.5, 0.8);
        Object[][] rowsWithSingleFraction = new Object[10][];
        Object[][] rowsWithFractionsArray = new Object[10][];
        for (int i = 0; i < rowsWithSingleFraction.length; i++) {
            rowsWithSingleFraction[i] = new Object[]{ valueType.value(i), fractions.get(0) };
            rowsWithFractionsArray[i] = new Object[]{ valueType.value(i), fractions };
        }
        Object result = execSingleFractionPercentile(valueType, rowsWithSingleFraction);
        assertEquals(4.5, result);
        result = execArrayFractionPercentile(valueType, rowsWithFractionsArray);
        assertThat(result, instanceOf(List.class));
        assertEquals(2, ((List) result).size());
        assertEquals(4.5, ((List) result).get(0));
        assertEquals(7.5, ((List) result).get(1));
    }
}
 
Example 3
Source File: LiteralTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedArrayLiteral() throws Exception {
    for (DataType<?> type : DataTypes.PRIMITIVE_TYPES) {
        DataType<?> nestedType = new ArrayType<>(new ArrayType<>(type));
        Object value;

        if (type.id() == BooleanType.ID) {
            value = true;
        } else if (type.id() == DataTypes.IP.id()) {
            value = type.value("123.34.243.23");
        } else if (type.id() == DataTypes.INTERVAL.id()) {
            value = type.value(new Period().withSeconds(100));
        } else {
            value = type.value("0");
        }
        var nestedValue = List.of(List.of(value));
        Literal nestedLiteral = Literal.ofUnchecked(nestedType, nestedValue);
        assertThat(nestedLiteral.valueType(), is(nestedType));
        assertThat(nestedLiteral.value(), is(nestedValue));
    }
}
 
Example 4
Source File: PolyglotValuesConverter.java    From crate with Apache License 2.0 5 votes vote down vote up
static Object toCrateObject(Value value, DataType<?> type) {
    if (value == null) {
        return null;
    }
    switch (type.id()) {
        case ArrayType.ID:
            ArrayList<Object> items = new ArrayList<>((int) value.getArraySize());
            for (int idx = 0; idx < value.getArraySize(); idx++) {
                var item = toCrateObject(value.getArrayElement(idx), ((ArrayType) type).innerType());
                items.add(idx, item);
            }
            return type.value(items);
        case ObjectType.ID:
            return type.value(value.as(MAP_TYPE_LITERAL));
        case GeoPointType.ID:
            if (value.hasArrayElements()) {
                return type.value(toCrateObject(value, DataTypes.DOUBLE_ARRAY));
            } else {
                return type.value(value.asString());
            }
        case GeoShapeType.ID:
            if (value.isString()) {
                return type.value(value.asString());
            } else {
                return type.value(value.as(MAP_TYPE_LITERAL));
            }
        default:
            final Object polyglotValue;
            if (value.isNumber()) {
                polyglotValue = value.as(NUMBER_TYPE_LITERAL);
            } else if (value.isString()) {
                polyglotValue = value.asString();
            } else if (value.isBoolean()) {
                polyglotValue = value.asBoolean();
            } else {
                polyglotValue = value.asString();
            }
            return type.value(polyglotValue);
    }
}
 
Example 5
Source File: ValueExtractors.java    From crate with Apache License 2.0 4 votes vote down vote up
public static Function<Map<String, Object>, Object> fromMap(ColumnIdent column, DataType<?> type) {
    return map -> type.value(fromMap(map, column));
}
 
Example 6
Source File: DataTypeTestingTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Test
public void testDataGeneratorReturnValidValues() throws Exception {
    for (DataType type : DataTypeTesting.ALL_TYPES_EXCEPT_ARRAYS) {
        type.value(DataTypeTesting.getDataGenerator(type).get());
    }
}