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

The following examples show how to use io.crate.types.DataType#id() . 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: InsertAnalyzer.java    From crate with Apache License 2.0 6 votes vote down vote up
private static void checkSourceAndTargetColsForLengthAndTypesCompatibility(
    List<Reference> targetColumns, List<Symbol> sources) {
    if (targetColumns.size() != sources.size()) {
        Collector<CharSequence, ?, String> commaJoiner = Collectors.joining(", ");
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
            "Number of target columns (%s) of insert statement doesn't match number of source columns (%s)",
            targetColumns.stream().map(r -> r.column().sqlFqn()).collect(commaJoiner),
            sources.stream().map(Symbol::toString).collect(commaJoiner)));
    }

    for (int i = 0; i < targetColumns.size(); i++) {
        Reference targetCol = targetColumns.get(i);
        Symbol source = sources.get(i);
        DataType<?> targetType = targetCol.valueType();
        if (targetType.id() == DataTypes.UNDEFINED.id() || source.valueType().isConvertableTo(targetType, false)) {
            continue;
        }
        throw new IllegalArgumentException(String.format(Locale.ENGLISH,
            "The type '%s' of the insert source '%s' is not convertible to the type '%s' of target column '%s'",
            source.valueType(),
            source,
            targetType,
            targetCol.column().fqn()
        ));
    }
}
 
Example 2
Source File: NullSentinelValues.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * @return a sentinel value that is used to indicate a `null` value.
 *         The returned value here is **not** necessarily compatible with the value type of the given `type`.
 *         This can be used for in places where {@link org.apache.lucene.search.ScoreDoc} is used,
 *         which for example uses LONG also for columns of type INTEGER.
 */
public static Object nullSentinelForScoreDoc(DataType<?> type, boolean reverseFlag, Boolean nullFirst) {
    boolean min = reverseFlag ^ (nullFirst != null ? nullFirst : reverseFlag);
    switch (type.id()) {
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
        case BooleanType.ID:
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return min ? Long.MIN_VALUE : Long.MAX_VALUE;

        case FloatType.ID:
            return min ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;

        case DoubleType.ID:
            return min ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;

        default:
            return null;
    }
}
 
Example 3
Source File: Literal.java    From crate with Apache License 2.0 6 votes vote down vote up
private static <T> boolean typeMatchesValue(DataType<T> type, T value) {
    if (value == null) {
        return true;
    }
    if (type.id() == ObjectType.ID) {
        //noinspection unchecked
        Map<String, Object> mapValue = (Map<String, Object>) value;
        ObjectType objectType = ((ObjectType) type);
        for (String key : mapValue.keySet()) {
            DataType<?> innerType = objectType.innerType(key);
            //noinspection unchecked
            if (typeMatchesValue((DataType<Object>) innerType, mapValue.get(key)) == false) {
                return false;
            }
        }
        // lets do the expensive "deep" map value conversion only after everything else succeeded
        Map<String, Object> safeValue = objectType.value(value);
        return safeValue.size() == mapValue.size();
    }

    return Objects.equals(type.value(value), value);
}
 
Example 4
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 5
Source File: DocIndexMetaData.java    From crate with Apache License 2.0 6 votes vote down vote up
private ImmutableMap<ColumnIdent, String> getAnalyzers(ColumnIdent columnIdent, Map<String, Object> propertiesMap) {
    ImmutableMap.Builder<ColumnIdent, String> builder = ImmutableMap.builder();
    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());
        columnProperties = furtherColumnProperties(columnProperties);
        if (columnDataType.id() == ObjectType.ID
            || (columnDataType.id() == ArrayType.ID
                && ((ArrayType) columnDataType).innerType().id() == ObjectType.ID)) {
            if (columnProperties.get("properties") != null) {
                builder.putAll(getAnalyzers(newIdent, (Map<String, Object>) columnProperties.get("properties")));
            }
        }
        String analyzer = (String) columnProperties.get("analyzer");
        if (analyzer != null) {
            builder.put(newIdent, analyzer);
        }
    }
    return builder.build();
}
 
Example 6
Source File: NegateLiterals.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Literal visitLiteral(Literal symbol, Void context) {
    Object value = symbol.value();
    if (value == null) {
        return symbol;
    }
    DataType valueType = symbol.valueType();
    switch (valueType.id()) {
        case DoubleType.ID:
            return Literal.of(valueType, (Double) value * -1);
        case FloatType.ID:
            return Literal.of(valueType, (Double) value * -1);
        case ShortType.ID:
            return Literal.of(valueType, (Short) value * -1);
        case IntegerType.ID:
            return Literal.of(valueType, (Integer) value * -1);
        case LongType.ID:
            return Literal.of(valueType, (Long) value * -1);
        default:
            throw new UnsupportedOperationException(Symbols.format(
                "Cannot negate %s. You may need to add explicit type casts", symbol));
    }
}
 
Example 7
Source File: GroupByMaps.java    From crate with Apache License 2.0 6 votes vote down vote up
public static <K, V> Supplier<Map<K, V>> mapForType(DataType<K> type) {
    switch (type.id()) {
        case ByteType.ID:
            return () -> (Map) new PrimitiveMapWithNulls<>(new ByteObjectHashMap<>());
        case ShortType.ID:
            return () -> (Map) new PrimitiveMapWithNulls<>(new ShortObjectHashMap<>());
        case IntegerType.ID:
            return () -> (Map) new PrimitiveMapWithNulls<>(new IntObjectHashMap<>());

        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return () -> (Map) new PrimitiveMapWithNulls<>(new LongObjectHashMap<>());

        default:
            return HashMap::new;
    }
}
 
Example 8
Source File: MoveReferenceCastToLiteralCastOnAnyOperatorsWhenRightIsReference.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Symbol apply(Function operator,
                    Captures captures,
                    Functions functions) {
    var literal = operator.arguments().get(0);
    var castFunction = captures.get(castCapture);
    var reference = castFunction.arguments().get(0);
    DataType<?> targetType = reference.valueType();
    if (targetType.id() != ArrayType.ID) {
        return null;
    }
    targetType = ((ArrayType<?>) targetType).innerType();

    var operatorName = operator.name();
    if (List.of(AnyOperators.Type.EQ.opName(), AnyOperators.Type.NEQ.opName()).contains(operatorName) == false
        && literal.valueType().id() == ArrayType.ID) {
        // this is not supported and will fail later on with more verbose error than a cast error
        return null;
    }

    return functionResolver.apply(
        operator.name(),
        List.of(literal.cast(targetType), reference)
    );
}
 
Example 9
Source File: InsertFromSubQueryPlanner.java    From crate with Apache License 2.0 6 votes vote down vote up
@Nullable
private static EvalProjection createCastProjection(List<Reference> targetCols, List<Symbol> sourceCols) {
    ArrayList<Symbol> casts = new ArrayList<>(targetCols.size());
    boolean requiresCasts = false;
    for (int i = 0; i < sourceCols.size(); i++) {
        Symbol output = sourceCols.get(i);
        Reference targetCol = targetCols.get(i);
        InputColumn inputColumn = new InputColumn(i, output.valueType());
        DataType<?> targetType = targetCol.valueType();
        if (targetType.id() == DataTypes.UNDEFINED.id() || targetType.equals(output.valueType())) {
            casts.add(inputColumn);
        } else {
            requiresCasts = true;
            casts.add(inputColumn.cast(targetType));
        }
    }
    return requiresCasts ? new EvalProjection(casts) : null;
}
 
Example 10
Source File: DocIndexMetaData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private ImmutableMap<ColumnIdent, String> getAnalyzers(ColumnIdent columnIdent, Map<String, Object> propertiesMap) {
    ImmutableMap.Builder<ColumnIdent, String> builder = ImmutableMap.builder();
    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());
        columnProperties = furtherColumnProperties(columnProperties);
        if (columnDataType == DataTypes.OBJECT
            || (columnDataType.id() == ArrayType.ID
                && ((ArrayType) columnDataType).innerType() == DataTypes.OBJECT)) {
            if (columnProperties.get("properties") != null) {
                builder.putAll(getAnalyzers(newIdent, (Map<String, Object>) columnProperties.get("properties")));
            }
        }
        String analyzer = (String) columnProperties.get("analyzer");
        if (analyzer != null) {
            builder.put(newIdent, analyzer);
        }
    }
    return builder.build();
}
 
Example 11
Source File: ArithmeticOperatorsFactory.java    From crate with Apache License 2.0 5 votes vote down vote up
static BiFunction getSubtractFunction(DataType<?> fstArgDataType, DataType<?> sndArgDataType) {
    switch (fstArgDataType.id()) {
        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            if (IntervalType.ID == sndArgDataType.id()) {
                var signature = IntervalTimestampArithmeticScalar.signatureFor(
                    fstArgDataType,
                    ArithmeticFunctions.Names.SUBTRACT
                );
                return new IntervalTimestampArithmeticScalar(
                    "-",
                    signature,
                    signature
                );
            }
            return SUB_LONG_FUNCTION;
        case DoubleType.ID:
            return SUB_DOUBLE_FUNCTION;
        case FloatType.ID:
            return SUB_FLOAT_FUNCTION;
        case ByteType.ID:
        case ShortType.ID:
        case IntegerType.ID:
            return SUB_INTEGER_FUNCTION;
        default:
            throw new UnsupportedOperationException(
                "Cannot create subtract function for data type " + fstArgDataType.getName());
    }
}
 
Example 12
Source File: Unnest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static void validateTypes(List<? extends DataType> argumentTypes) {
    ListIterator<? extends DataType> it = argumentTypes.listIterator();
    if (!it.hasNext()) {
        throw new IllegalArgumentException("unnest expects at least 1 argument of type array. Got 0");
    }
    while (it.hasNext()) {
        DataType dataType = it.next();
        if (dataType.id() != ArrayType.ID) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                    "unnest expects arguments of type array. " +
                    "Got an argument of type '%s' at position %d instead.", dataType, it.previousIndex()));
        }
    }
}
 
Example 13
Source File: SignatureBinder.java    From crate with Apache License 2.0 5 votes vote down vote up
private boolean appendConstraintSolvers(List<TypeConstraintSolver> resultBuilder,
                                        TypeSignature formalTypeSignature,
                                        TypeSignature actualTypeSignature,
                                        CoercionType coercionType) {
    if (formalTypeSignature.getParameters().isEmpty()) {
        TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(formalTypeSignature.getBaseTypeName());
        if (typeVariableConstraint == null) {
            return true;
        }
        resultBuilder.add(new TypeParameterSolver(formalTypeSignature.getBaseTypeName(), actualTypeSignature.createType()));
        return true;
    }

    DataType<?> actualType = actualTypeSignature.createType();

    List<TypeSignature> actualTypeParametersTypeSignature;
    if (UndefinedType.ID == actualType.id()) {
        actualTypeParametersTypeSignature = Collections.nCopies(formalTypeSignature.getParameters().size(),
                                                                        UndefinedType.INSTANCE.getTypeSignature());
    } else {
        actualTypeParametersTypeSignature = Lists2.map(
            actualType.getTypeParameters(),
            DataType::getTypeSignature
        );
    }

    return appendConstraintSolvers(
        resultBuilder,
        Collections.unmodifiableList(formalTypeSignature.getParameters()),
        actualTypeParametersTypeSignature,
        coercionType);
}
 
Example 14
Source File: SizeEstimatorFactory.java    From crate with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> SizeEstimator<T> create(DataType<?> type) {
    switch (type.id()) {
        case UndefinedType.ID:
            return (SizeEstimator<T>) new ConstSizeEstimator(UNKNOWN_DEFAULT_RAM_BYTES_USED);

        case ObjectType.ID:
        case GeoShapeType.ID:
            return (SizeEstimator<T>) new SamplingSizeEstimator<>(SAMPLE_EVERY_NTH, MapSizeEstimator.INSTANCE);

        case StringType.ID:
        case IpType.ID:
            return (SizeEstimator<T>) StringSizeEstimator.INSTANCE;

        case ArrayType.ID:
            var innerEstimator = create(((ArrayType<?>) type).innerType());
            return (SizeEstimator<T>) ArraySizeEstimator.create(innerEstimator);

        case OidVectorType.ID:
            return (SizeEstimator<T>) ArraySizeEstimator.create(create(DataTypes.INTEGER));

        case RowType.ID:
            return (SizeEstimator<T>) new RecordSizeEstimator(Lists2.map(((RowType) type).fieldTypes(), SizeEstimatorFactory::create));

        case RegprocType.ID:
            return (SizeEstimator<T>) RegprocSizeEstimator.INSTANCE;

        default:
            if (type instanceof FixedWidthType) {
                return (SizeEstimator<T>) new ConstSizeEstimator(((FixedWidthType) type).fixedSize());
            }
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot get SizeEstimator for type %s", type));
    }
}
 
Example 15
Source File: AnyLikeOperator.java    From crate with Apache License 2.0 5 votes vote down vote up
public AnyLikeOperator(Signature signature,
                       Signature boundSignature,
                       TriPredicate<String, String, Integer> matcher,
                       int patternMatchingFlags) {
    this.signature = signature;
    this.boundSignature = boundSignature;
    this.matcher = matcher;
    this.patternMatchingFlags = patternMatchingFlags;
    DataType<?> innerType = ((ArrayType<?>) boundSignature.getArgumentDataTypes().get(1)).innerType();
    if (innerType.id() == ObjectType.ID) {
        throw new IllegalArgumentException("ANY on object arrays is not supported");
    }
}
 
Example 16
Source File: SizeEstimatorFactory.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> SizeEstimator<T> create(DataType type) {
    switch (type.id()) {
        case StringType.ID:
        case IpType.ID:
            return (SizeEstimator<T>)new BytesRefSizeEstimator();
        default:
            if (type instanceof FixedWidthType) {
                return (SizeEstimator<T>) new ConstSizeEstimator(((FixedWidthType) type).fixedSize());
            }
            throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "Cannot get SizeEstimator for type %s", type));
    }
}
 
Example 17
Source File: ArrayLengthQueryTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayLengthWithAllSupportedTypes() throws Exception {
    for (DataType<?> type : DataTypeTesting.ALL_TYPES_EXCEPT_ARRAYS) {
        // This is temporary as long as interval is not fully implemented
        if(DataTypes.STORAGE_UNSUPPORTED.contains(type)) {
            continue;
        }
        Supplier dataGenerator = DataTypeTesting.getDataGenerator(type);
        Object val1 = dataGenerator.get();
        Object val2 = dataGenerator.get();
        Object[] arr = {val1, val2};
        Object[] values = new Object[] {
            arr
        };

        // ensure the test is operating on a fresh, empty cluster state (no tables)
        resetClusterService();

        try (QueryTester tester = new QueryTester.Builder(
            createTempDir(),
            THREAD_POOL,
            clusterService,
            Version.CURRENT,
            "create table \"t_"+ type.getName() + "\" (xs array(\"" + type.getName() + "\"))"
        ).indexValues("xs", values).build()) {
            System.out.println(type);
            List<Object> result = tester.runQuery("xs", "array_length(xs, 1) >= 2");
            assertThat(result.size(), is(1));
            ArrayType arrayType = new ArrayType<>(type);
            // Object compareValueTo does type-guessing which might result in
            // double/float conversions which are not fully accurate, so we skip that here
            // having the result size check should be sufficient anyway
            if (type.id() != ObjectType.ID) {
                assertThat(arrayType.compare((List) result.get(0), Arrays.asList(arr)), is(0));
            }
        }
    }
}
 
Example 18
Source File: NullSentinelValues.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Similar to {@link #nullSentinelForScoreDoc(DataType, boolean, Boolean)}
 * but returns a value that is compatible with the value type of the given DataType.
 */
@Nullable
public static Object nullSentinel(DataType<?> dataType, NullValueOrder nullValueOrder, boolean reversed) {
    boolean min = nullValueOrder == NullValueOrder.FIRST ^ reversed;
    switch (dataType.id()) {
        case ByteType.ID:
            return min ? Byte.MIN_VALUE : Byte.MAX_VALUE;

        case ShortType.ID:
            return min ? Short.MIN_VALUE : Short.MAX_VALUE;

        case IntegerType.ID:
            return min ? Integer.MIN_VALUE : Integer.MAX_VALUE;

        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return min ? Long.MIN_VALUE : Long.MAX_VALUE;

        case FloatType.ID:
            return min ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;

        case DoubleType.ID:
            return min ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;

        default:
            return null;
    }
}
 
Example 19
Source File: DataTypeTesting.java    From crate with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Supplier<T> getDataGenerator(DataType<T> type) {
    Random random = RandomizedContext.current().getRandom();
    switch (type.id()) {
        case ByteType.ID:
            return () -> (T) (Byte) (byte) random.nextInt(Byte.MAX_VALUE);
        case BooleanType.ID:
            return () -> (T) (Boolean) random.nextBoolean();

        case StringType.ID:
            return () -> (T) RandomizedTest.randomAsciiLettersOfLength(random.nextInt(10));

        case IpType.ID:
            return () -> {
                if (random.nextBoolean()) {
                    return (T) randomIPv4Address(random);
                } else {
                    return (T) randomIPv6Address(random);
                }
            };

        case DoubleType.ID:
            return () -> (T) (Double) random.nextDouble();

        case FloatType.ID:
            return () -> (T) (Float) random.nextFloat();

        case ShortType.ID:
            return () -> (T) (Short) (short) random.nextInt(Short.MAX_VALUE);

        case IntegerType.ID:
            return () -> (T) (Integer) random.nextInt();

        case LongType.ID:
        case TimestampType.ID_WITH_TZ:
        case TimestampType.ID_WITHOUT_TZ:
            return () -> (T) (Long) random.nextLong();

        case GeoPointType.ID:
            return () -> (T) new PointImpl(
                BiasedNumbers.randomDoubleBetween(random, -180, 180),
                BiasedNumbers.randomDoubleBetween(random, -90, 90),
                JtsSpatialContext.GEO
            );

        case GeoShapeType.ID:
            return () -> {
                // Can't use immutable Collections.singletonMap; insert-analyzer mutates the map
                Map<String, Object> geoShape = new HashMap<>(2);
                geoShape.put("coordinates", Arrays.asList(10.2d, 32.2d));
                geoShape.put("type", "Point");
                return (T) geoShape;
            };

        case ObjectType.ID:
            Supplier<?> innerValueGenerator = getDataGenerator(randomType());
            return () -> {
                // Can't use immutable Collections.singletonMap; insert-analyzer mutates the map
                HashMap<String, Object> map = new HashMap<>();
                map.put("x", innerValueGenerator.get());
                return (T) map;
            };
        case IntervalType.ID:
            return () -> {
                return (T) new Period().withSeconds(RandomNumbers.randomIntBetween(random, 0, Integer.MAX_VALUE));
            };

    }

    throw new AssertionError("No data generator for type " + type.getName());
}
 
Example 20
Source File: ValueNormalizer.java    From crate with Apache License 2.0 4 votes vote down vote up
private static boolean isObjectArray(DataType type) {
    return type.id() == ArrayType.ID && ((ArrayType) type).innerType().id() == ObjectType.ID;
}