Search in sources :

Example 1 with PgArrayParsingException

use of io.crate.protocols.postgres.parser.PgArrayParsingException 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)

Aggregations

ColumnValidationException (io.crate.exceptions.ColumnValidationException)1 ConversionException (io.crate.exceptions.ConversionException)1 Literal (io.crate.expression.symbol.Literal)1 PgArrayParsingException (io.crate.protocols.postgres.parser.PgArrayParsingException)1 List (java.util.List)1