Search in sources :

Example 1 with ConversionException

use of io.crate.exceptions.ConversionException in project crate by crate.

the class SubQueryAndParamBinder method convert.

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);
    }
    try {
        return Literal.ofUnchecked(type, type.implicitCast(value));
    } catch (ClassCastException | IllegalArgumentException e) {
        throw new ConversionException(value, type);
    }
}
Also used : ConversionException(io.crate.exceptions.ConversionException)

Example 2 with ConversionException

use of io.crate.exceptions.ConversionException 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 3 with ConversionException

use of io.crate.exceptions.ConversionException in project crate by crate.

the class ValueNormalizer method normalizeInputForReference.

/**
 * normalize and validate given value according to the corresponding {@link io.crate.metadata.Reference}
 *
 * @param valueSymbol the value to normalize, might be anything from {@link Scalar} to {@link io.crate.expression.symbol.Literal}
 * @param reference   the reference to which the value has to comply in terms of type-compatibility
 * @return the normalized Symbol, should be a literal
 * @throws io.crate.exceptions.ColumnValidationException
 */
public static Symbol normalizeInputForReference(Symbol valueSymbol, Reference reference, TableInfo tableInfo, Function<Symbol, Symbol> normalizer) {
    assert valueSymbol != null : "valueSymbol must not be null";
    DataType<?> targetType = getTargetType(valueSymbol, reference);
    try {
        valueSymbol = normalizer.apply(valueSymbol.cast(reference.valueType()));
    } catch (PgArrayParsingException | ConversionException e) {
        throw new ColumnValidationException(reference.column().name(), tableInfo.ident(), String.format(Locale.ENGLISH, "Cannot cast expression `%s` of type `%s` to `%s`", valueSymbol, valueSymbol.valueType().getName(), reference.valueType().getName()));
    }
    if (!(valueSymbol instanceof Literal)) {
        return valueSymbol.cast(targetType);
    }
    Object value = ((Literal<?>) valueSymbol).value();
    if (value == null) {
        return valueSymbol;
    }
    try {
        if (targetType.id() == ObjectType.ID) {
            // noinspection unchecked
            normalizeObjectValue((Map) value, reference, tableInfo);
        } else if (isObjectArray(targetType)) {
            normalizeObjectArrayValue((List<Map<String, Object>>) value, reference, tableInfo);
        }
    } catch (PgArrayParsingException | ConversionException e) {
        throw new ColumnValidationException(reference.column().name(), tableInfo.ident(), Symbols.format("\"%s\" has a type that can't be implicitly cast to that of \"%s\" (" + reference.valueType().getName() + ")", valueSymbol, reference));
    }
    return valueSymbol;
}
Also used : ConversionException(io.crate.exceptions.ConversionException) Literal(io.crate.expression.symbol.Literal) List(java.util.List) ColumnValidationException(io.crate.exceptions.ColumnValidationException) PgArrayParsingException(io.crate.protocols.postgres.parser.PgArrayParsingException)

Example 4 with ConversionException

use of io.crate.exceptions.ConversionException in project crate by crate.

the class ValueNormalizer method normalizeInputForReference.

/**
     * normalize and validate given value according to the corresponding {@link io.crate.metadata.Reference}
     *
     * @param valueSymbol the value to normalize, might be anything from {@link io.crate.metadata.Scalar} to {@link io.crate.analyze.symbol.Literal}
     * @param reference   the reference to which the value has to comply in terms of type-compatibility
     * @return the normalized Symbol, should be a literal
     * @throws io.crate.exceptions.ColumnValidationException
     */
public Symbol normalizeInputForReference(Symbol valueSymbol, Reference reference) {
    assert valueSymbol != null : "valueSymbol must not be null";
    DataType<?> targetType = getTargetType(valueSymbol, reference);
    if (!(valueSymbol instanceof Literal)) {
        return ExpressionAnalyzer.castIfNeededOrFail(valueSymbol, targetType);
    }
    Literal literal = (Literal) valueSymbol;
    try {
        literal = Literal.convert(literal, reference.valueType());
    } catch (ConversionException e) {
        throw new ColumnValidationException(reference.ident().columnIdent().name(), String.format(Locale.ENGLISH, "%s cannot be cast to type %s", SymbolPrinter.INSTANCE.printSimple(valueSymbol), reference.valueType().getName()));
    }
    Object value = literal.value();
    if (value == null) {
        return literal;
    }
    try {
        if (targetType == DataTypes.OBJECT) {
            //noinspection unchecked
            normalizeObjectValue((Map) value, reference);
        } else if (isObjectArray(targetType)) {
            normalizeObjectArrayValue((Object[]) value, reference);
        }
    } catch (ConversionException e) {
        throw new ColumnValidationException(reference.ident().columnIdent().name(), SymbolFormatter.format("\"%s\" has a type that can't be implicitly cast to that of \"%s\" (" + reference.valueType().getName() + ")", literal, reference));
    }
    return literal;
}
Also used : ConversionException(io.crate.exceptions.ConversionException) Literal(io.crate.analyze.symbol.Literal) ColumnValidationException(io.crate.exceptions.ColumnValidationException)

Example 5 with ConversionException

use of io.crate.exceptions.ConversionException in project crate by crate.

the class ImplicitCastFunction method evaluate.

@Override
public Object evaluate(TransactionContext txnCtx, NodeContext nodeCtx, Input<Object>[] args) {
    var targetTypeSignature = parseTypeSignature((String) args[1].value());
    var targetType = targetTypeSignature.createType();
    try {
        return targetType.implicitCast(args[0].value());
    } catch (ConversionException e) {
        throw e;
    } catch (ClassCastException | IllegalArgumentException e) {
        throw new ConversionException(args[0].value(), targetType);
    }
}
Also used : ConversionException(io.crate.exceptions.ConversionException)

Aggregations

ConversionException (io.crate.exceptions.ConversionException)6 ColumnValidationException (io.crate.exceptions.ColumnValidationException)2 Literal (io.crate.analyze.symbol.Literal)1 Input (io.crate.data.Input)1 SubscriptFunction (io.crate.expression.scalar.SubscriptFunction)1 SubscriptObjectFunction (io.crate.expression.scalar.SubscriptObjectFunction)1 SubscriptRecordFunction (io.crate.expression.scalar.SubscriptRecordFunction)1 ArrayFunction (io.crate.expression.scalar.arithmetic.ArrayFunction)1 ExplicitCastFunction (io.crate.expression.scalar.cast.ExplicitCastFunction)1 ImplicitCastFunction (io.crate.expression.scalar.cast.ImplicitCastFunction)1 TryCastFunction (io.crate.expression.scalar.cast.TryCastFunction)1 CurrentSchemaFunction (io.crate.expression.scalar.systeminformation.CurrentSchemaFunction)1 CurrentSchemasFunction (io.crate.expression.scalar.systeminformation.CurrentSchemasFunction)1 CurrentTimeFunction (io.crate.expression.scalar.timestamp.CurrentTimeFunction)1 CurrentTimestampFunction (io.crate.expression.scalar.timestamp.CurrentTimestampFunction)1 Literal (io.crate.expression.symbol.Literal)1 Symbol (io.crate.expression.symbol.Symbol)1 PgArrayParsingException (io.crate.protocols.postgres.parser.PgArrayParsingException)1 ArrayType (io.crate.types.ArrayType)1 ArrayList (java.util.ArrayList)1