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);
}
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations