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

The following examples show how to use io.crate.types.DataType#equals() . 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: SubscriptObjectFunction.java    From crate with Apache License 2.0 6 votes vote down vote up
static Symbol tryToInferReturnTypeFromObjectTypeAndArguments(Function func) {
    if (!func.valueType().equals(DataTypes.UNDEFINED)) {
        return func;
    }
    var arguments = func.arguments();
    ObjectType objectType = (ObjectType) arguments.get(0).valueType();
    List<String> path = maybeCreatePath(arguments);
    if (path == null) {
        return func;
    } else {
        DataType<?> returnType = objectType.resolveInnerType(path);
        return returnType.equals(DataTypes.UNDEFINED)
            ? func
            : new Function(
                func.signature(),
                func.arguments(),
                returnType
        );
    }
}
 
Example 2
Source File: SignatureBinder.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public SolverReturnStatus update(BoundVariables.Builder bindings) {
    if (!bindings.containsTypeVariable(typeParameter)) {
        bindings.setTypeVariable(typeParameter, actualType);
        return SolverReturnStatus.CHANGED;
    }
    DataType<?> originalType = bindings.getTypeVariable(typeParameter);
    DataType<?> commonType = getCommonType(originalType, actualType);
    if (commonType == null) {
        return SolverReturnStatus.UNSOLVABLE;
    }
    if (commonType.equals(originalType)) {
        return SolverReturnStatus.UNCHANGED_SATISFIED;
    }
    bindings.setTypeVariable(typeParameter, commonType);
    return SolverReturnStatus.CHANGED;
}
 
Example 3
Source File: ArrayDifferenceFunction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public FunctionImplementation<Function> getForTypes(List<DataType> dataTypes) throws IllegalArgumentException {
    Preconditions.checkArgument(dataTypes.size() == 2, "array_difference function requires 2 arguments");

    for (int i = 0; i < dataTypes.size(); i++) {
        Preconditions.checkArgument(dataTypes.get(i) instanceof ArrayType, String.format(Locale.ENGLISH,
                "Argument %d of the array_difference function cannot be converted to array", i + 1));
    }

    DataType innerType0 = ((ArrayType) dataTypes.get(0)).innerType();
    DataType innerType1 = ((ArrayType) dataTypes.get(1)).innerType();

    Preconditions.checkArgument(!innerType0.equals(DataTypes.UNDEFINED) || !innerType1.equals(DataTypes.UNDEFINED),
            "One of the arguments of the array_difference function can be of undefined inner type, but not both");

    if(!innerType0.equals(DataTypes.UNDEFINED)){
        Preconditions.checkArgument(innerType1.isConvertableTo(innerType0),
                String.format(Locale.ENGLISH,
                        "Second argument's inner type (%s) of the array_difference function cannot be converted to the first argument's inner type (%s)",
                        innerType1,innerType0));
    }

    return new ArrayDifferenceFunction(createInfo(dataTypes), null);
}
 
Example 4
Source File: ArrayCatFunction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public FunctionImplementation<Function> getForTypes(List<DataType> dataTypes) throws IllegalArgumentException {
    Preconditions.checkArgument(dataTypes.size() == 2, "array_cat function requires 2 arguments");

    for (int i = 0; i < dataTypes.size(); i++) {
        Preconditions.checkArgument(dataTypes.get(i) instanceof ArrayType, String.format(Locale.ENGLISH,
                "Argument %d of the array_cat function cannot be converted to array", i + 1));
    }

    DataType innerType0 = ((ArrayType) dataTypes.get(0)).innerType();
    DataType innerType1 = ((ArrayType) dataTypes.get(1)).innerType();

    Preconditions.checkArgument(!innerType0.equals(DataTypes.UNDEFINED) || !innerType1.equals(DataTypes.UNDEFINED),
            "One of the arguments of the array_cat function can be of undefined inner type, but not both");

    if(!innerType0.equals(DataTypes.UNDEFINED)){
        Preconditions.checkArgument(innerType1.isConvertableTo(innerType0),
                String.format(Locale.ENGLISH,
                        "Second argument's inner type (%s) of the array_cat function cannot be converted to the first argument's inner type (%s)",
                        innerType1,innerType0));
    }

    return new ArrayCatFunction(createInfo(dataTypes));
}
 
Example 5
Source File: AnalyzedTableElements.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void processGeneratedExpression(ExpressionAnalyzer expressionAnalyzer,
                                        SymbolPrinter symbolPrinter,
                                        AnalyzedColumnDefinition columnDefinition,
                                        ExpressionAnalysisContext expressionAnalysisContext) {
    // validate expression
    Symbol function = expressionAnalyzer.convert(columnDefinition.generatedExpression(), expressionAnalysisContext);

    String formattedExpression;
    DataType valueType = function.valueType();
    DataType definedType = columnDefinition.dataType() == null ? null : DataTypes.ofMappingNameSafe(columnDefinition.dataType());

    // check for optional defined type and add `cast` to expression if possible
    if (definedType != null && !definedType.equals(valueType)) {
        Preconditions.checkArgument(valueType.isConvertableTo(definedType),
                "generated expression value type '%s' not supported for conversion to '%s'", valueType, definedType.getName());

        Function castFunction = new Function(CastFunctionResolver.functionInfo(valueType, definedType, false), Lists.newArrayList(function));
        formattedExpression = symbolPrinter.print(castFunction, SymbolPrinter.Style.PARSEABLE_NOT_QUALIFIED); // no full qualified references here
    } else {
        columnDefinition.dataType(function.valueType().getName());
        formattedExpression = symbolPrinter.print(function, SymbolPrinter.Style.PARSEABLE_NOT_QUALIFIED); // no full qualified references here
    }

    columnDefinition.formattedGeneratedExpression(formattedExpression);
}
 
Example 6
Source File: SignatureBinder.java    From crate with Apache License 2.0 6 votes vote down vote up
private static boolean satisfiesCoercion(CoercionType coercionType,
                                         DataType<?> fromType,
                                         TypeSignature toTypeSignature) {
    switch (coercionType) {
        case FULL:
            return fromType.isConvertableTo(toTypeSignature.createType(), false);
        case PRECEDENCE_ONLY:
            var toType = toTypeSignature.createType();
            return fromType.equals(toType)
                   || (fromType.isConvertableTo(toTypeSignature.createType(), false)
                      && toType.precedes(fromType));
        case NONE:
        default:
            return fromType.getTypeSignature().equals(toTypeSignature);
    }
}
 
Example 7
Source File: TableInfo.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * This is like {@link #getReference(ColumnIdent)},
 * except that the type is adjusted via {@link #getReadType(ColumnIdent)}
 */
@Nullable
default Reference getReadReference(ColumnIdent columnIdent) {
    Reference ref = getReference(columnIdent);
    if (ref == null) {
        return null;
    }
    DataType<?> readType = getReadType(columnIdent);
    if (readType.equals(ref.valueType())) {
        return ref;
    } else {
        return new Reference(
            ref.ident(),
            ref.granularity(),
            readType,
            ref.columnPolicy(),
            ref.indexType(),
            ref.isNullable(),
            ref.isColumnStoreDisabled(),
            ref.position(),
            ref.defaultExpression()
        );
    }
}
 
Example 8
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 9
Source File: SubQueryAndParamBinder.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Symbol convert(ParameterSymbol parameterSymbol, Row params) {
    DataType<?> type = parameterSymbol.valueType();
    Object value;
    try {
        value = params.get(parameterSymbol.index());
    } catch (IndexOutOfBoundsException e) {
        throw new IllegalArgumentException(String.format(
            Locale.ENGLISH,
            "The query contains a parameter placeholder $%d, but there are only %d parameter values",
            (parameterSymbol.index() + 1),
            params.numColumns()
        ));
    }
    if (type.equals(DataTypes.UNDEFINED)) {
        type = DataTypes.guessType(value);
    }
    return Literal.ofUnchecked(type, type.value(value));
}
 
Example 10
Source File: EqOperator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public FunctionImplementation<Function> getForTypes(List<DataType> dataTypes) throws IllegalArgumentException {
    Preconditions.checkArgument(dataTypes.size() == 2, "EqOperator must have 2 arguments");
    DataType leftType = dataTypes.get(0);
    DataType rightType = dataTypes.get(1);

    FunctionInfo info = createInfo(dataTypes);
    if (DataTypes.isCollectionType(leftType) && DataTypes.isCollectionType(rightType)) {
        return new ArrayEqOperator(info);
    }
    if (leftType.equals(DataTypes.OBJECT) && rightType.equals(DataTypes.OBJECT)) {
        return new ObjectEqOperator(info);
    }
    return new EqOperator(info);
}
 
Example 11
Source File: ArrayArgumentValidators.java    From crate with Apache License 2.0 5 votes vote down vote up
public static void ensureInnerTypeIsNotUndefined(List<DataType<?>> dataTypes, String functionName) {
    DataType<?> innerType = ((ArrayType<?>) dataTypes.get(0)).innerType();
    if (innerType.equals(DataTypes.UNDEFINED)) {
        throw new IllegalArgumentException(String.format(
            Locale.ENGLISH,
            "The inner type of the array argument `%s` function cannot be undefined",
            functionName));
    }
}
 
Example 12
Source File: ArrayArgumentValidators.java    From crate with Apache License 2.0 5 votes vote down vote up
public static void ensureBothInnerTypesAreNotUndefined(List<DataType<?>> dataTypes, String functionName) {
    DataType<?> innerType0 = ((ArrayType<?>) dataTypes.get(0)).innerType();
    DataType<?> innerType1 = ((ArrayType<?>) dataTypes.get(1)).innerType();

    if (innerType0.equals(DataTypes.UNDEFINED) || innerType1.equals(DataTypes.UNDEFINED)) {
        throw new IllegalArgumentException(
            "One of the arguments of the `" + functionName +
            "` function can be of undefined inner type, but not both");
    }
}
 
Example 13
Source File: AnalyzedTableElements.java    From crate with Apache License 2.0 5 votes vote down vote up
private static void validateAndFormatExpression(Symbol function,
                                                AnalyzedColumnDefinition<Symbol> columnDefinitionWithExpressionSymbols,
                                                AnalyzedColumnDefinition<Object> columnDefinitionEvaluated,
                                                Consumer<String> formattedExpressionConsumer) {
    String formattedExpression;
    DataType<?> valueType = function.valueType();
    DataType<?> definedType = columnDefinitionWithExpressionSymbols.dataType();

    // check for optional defined type and add `cast` to expression if possible
    if (definedType != null && !definedType.equals(valueType)) {
        final DataType<?> columnDataType;
        if (ArrayType.NAME.equals(columnDefinitionWithExpressionSymbols.collectionType())) {
            columnDataType = new ArrayType<>(definedType);
        } else {
            columnDataType = definedType;
        }
        if (!valueType.isConvertableTo(columnDataType, false)) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                "expression value type '%s' not supported for conversion to '%s'",
                valueType, columnDataType.getName())
            );
        }

        Symbol castFunction = CastFunctionResolver
            .generateCastFunction(function, columnDataType);
        formattedExpression = castFunction.toString(Style.UNQUALIFIED);
    } else {
        if (valueType instanceof ArrayType) {
            columnDefinitionEvaluated.collectionType(ArrayType.NAME);
            columnDefinitionEvaluated.dataType(ArrayType.unnest(valueType).getName());
        } else {
            columnDefinitionEvaluated.dataType(valueType.getName());
        }
        formattedExpression = function.toString(Style.UNQUALIFIED);
    }
    formattedExpressionConsumer.accept(formattedExpression);
}
 
Example 14
Source File: GroupingProjector.java    From crate with Apache License 2.0 5 votes vote down vote up
private static void ensureAllTypesSupported(Iterable<? extends Symbol> keys) {
    for (Symbol key : keys) {
        DataType type = key.valueType();
        if (type instanceof ArrayType || type.equals(DataTypes.UNDEFINED)) {
            throw new UnsupportedOperationException("Cannot GROUP BY type: " + type);
        }
    }
}
 
Example 15
Source File: QueryBuilderHelper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static QueryBuilderHelper forType(DataType dataType) {
    while (dataType instanceof CollectionType) {
        dataType = ((CollectionType) dataType).innerType();
    }
    if (dataType.equals(DataTypes.BOOLEAN)) {
        return booleanQueryBuilder;
    }
    if (dataType.equals(DataTypes.BYTE)) {
        return intQueryBuilder;
    }
    if (dataType.equals(DataTypes.SHORT)) {
        return intQueryBuilder;
    }
    if (dataType.equals(DataTypes.INTEGER)) {
        return intQueryBuilder;
    }
    if (dataType.equals(DataTypes.TIMESTAMP) || dataType.equals(DataTypes.LONG)) {
        return longQueryBuilder;
    }
    if (dataType.equals(DataTypes.FLOAT)) {
        return floatQueryBuilder;
    }
    if (dataType.equals(DataTypes.DOUBLE)) {
        return doubleQueryBuilder;
    }
    if (dataType.equals(DataTypes.IP)) {
        return ipQueryBuilder;
    }
    if (dataType.equals(DataTypes.STRING)) {
        return stringQueryBuilder;
    }
    throw new UnsupportedOperationException(String.format(Locale.ENGLISH, "type %s not supported", dataType));
}
 
Example 16
Source File: Literal.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static boolean typeMatchesValue(DataType type, Object value) {
    if (value == null) {
        return true;
    }
    if (type.equals(DataTypes.STRING) && (value instanceof BytesRef || value instanceof String)) {
        return true;
    }
    if (type instanceof ArrayType) {
        DataType innerType = ((ArrayType) type).innerType();
        while (innerType instanceof ArrayType && value.getClass().isArray()) {
            type = innerType;
            innerType = ((ArrayType) innerType).innerType();
            value = ((Object[])value)[0];
        }
        if (innerType.equals(DataTypes.STRING)) {
            for (Object o : ((Object[]) value)) {
                if (o != null && !(o instanceof String || o instanceof BytesRef)) {
                    return false;
                }
            }
            return true;
        } else {
            return Arrays.equals((Object[]) value, ((ArrayType)type).value(value));
        }
    }
    // types like GeoPoint are represented as arrays
    if (value.getClass().isArray() && Arrays.equals((Object[]) value, (Object[]) type.value(value))) {
        return true;
    }
    return type.value(value).equals(value);
}
 
Example 17
Source File: ConcatFunction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public FunctionImplementation<Function> getForTypes(List<DataType> dataTypes) throws IllegalArgumentException {
    if (dataTypes.size() < 2) {
        throw new IllegalArgumentException("concat function requires at least 2 arguments");
    } else if (dataTypes.size() == 2 && dataTypes.get(0).equals(DataTypes.STRING) && dataTypes.get(1).equals(DataTypes.STRING)) {
        return new StringConcatFunction(new FunctionInfo(new FunctionIdent(NAME, dataTypes), DataTypes.STRING));
    } else if(dataTypes.size() == 2 && dataTypes.get(0) instanceof ArrayType && dataTypes.get(1) instanceof ArrayType){

        DataType innerType0 = ((ArrayType) dataTypes.get(0)).innerType();
        DataType innerType1 = ((ArrayType) dataTypes.get(1)).innerType();

        Preconditions.checkArgument(!innerType0.equals(DataTypes.UNDEFINED) || !innerType1.equals(DataTypes.UNDEFINED),
                "When concatenating arrays, one of the two arguments can be of undefined inner type, but not both");

        if(!innerType0.equals(DataTypes.UNDEFINED)){
            Preconditions.checkArgument(innerType1.isConvertableTo(innerType0),
                    String.format(Locale.ENGLISH,
                            "Second argument's inner type (%s) of the array_cat function cannot be converted to the first argument's inner type (%s)",
                            innerType1, innerType0));
        }

        return new ArrayCatFunction(ArrayCatFunction.createInfo(dataTypes));
    } else {
        for (int i = 0; i < dataTypes.size(); i++) {
            if (!dataTypes.get(i).isConvertableTo(DataTypes.STRING)) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                        "Argument %d of the concat function can't be converted to string", i));
            }
        }
        return new GenericConcatFunction(new FunctionInfo(new FunctionIdent(NAME, dataTypes), DataTypes.STRING));
    }
}
 
Example 18
Source File: ArrayArgumentValidators.java    From crate with Apache License 2.0 5 votes vote down vote up
public static void ensureSingleArgumentArrayInnerTypeIsNotUndefined(List<DataType<?>> dataTypes) {
    DataType<?> innerType = ((ArrayType<?>) dataTypes.get(0)).innerType();
    if (innerType.equals(DataTypes.UNDEFINED)) {
        throw new IllegalArgumentException(
            "When used with only one argument, the inner type of the array argument cannot be undefined");
    }
}
 
Example 19
Source File: DistanceFunction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Symbol normalizeSymbol(Function symbol) {
    Symbol arg1 = symbol.arguments().get(0);
    Symbol arg2 = symbol.arguments().get(1);
    DataType arg1Type = arg1.valueType();
    DataType arg2Type = arg2.valueType();

    boolean arg1IsReference = true;
    boolean literalConverted = false;
    short numLiterals = 0;

    if (arg1.symbolType().isValueSymbol()) {
        numLiterals++;
        arg1IsReference = false;
        if (!arg1Type.equals(DataTypes.GEO_POINT)) {
            literalConverted = true;
            arg1 = Literal.convert(arg1, DataTypes.GEO_POINT);
        }
    } else {
        validateType(arg1, arg1Type);
    }

    if (arg2.symbolType().isValueSymbol()) {
        numLiterals++;
        if (!arg2Type.equals(DataTypes.GEO_POINT)) {
            literalConverted = true;
            arg2 = Literal.convert(arg2, DataTypes.GEO_POINT);
        }
    } else {
        validateType(arg2, arg2Type);
    }

    if (numLiterals == 2) {
        return Literal.newLiteral(evaluate((Input) arg1, (Input) arg2));
    }

    // ensure reference is the first argument.
    if (!arg1IsReference) {
        return new Function(geoPointInfo, Arrays.asList(arg2, arg1));
    }
    if (literalConverted) {
        return new Function(geoPointInfo, Arrays.asList(arg1, arg2));
    }
    return symbol;
}
 
Example 20
Source File: Symbol.java    From crate with Apache License 2.0 3 votes vote down vote up
/**
 * Casts this Symbol to a new {@link DataType} by wrapping an implicit cast
 * function around it if no {@link CastMode} modes are provided.
 * <p>
 * Subclasses of this class may provide another cast methods.
 *
 * @param targetType The resulting data type after applying the cast
 * @param modes      One of the {@link CastMode} types.
 * @return An instance of {@link Function} which casts this symbol.
 */
public Symbol cast(DataType<?> targetType, CastMode... modes) {
    if (targetType.equals(valueType())) {
        return this;
    } else if (ArrayType.unnest(targetType).equals(DataTypes.UNTYPED_OBJECT)
               && valueType().id() == targetType.id()) {
        return this;
    }
    return CastFunctionResolver.generateCastFunction(this, targetType, modes);
}