Search in sources :

Example 1 with UsedByGeneratedCode

use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.

the class JsonToArrayCast method toArray.

@UsedByGeneratedCode
public static Block toArray(ArrayType arrayType, BlockBuilderAppender elementAppender, ConnectorSession connectorSession, Slice json) {
    try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
        jsonParser.nextToken();
        if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
            return null;
        }
        if (jsonParser.getCurrentToken() != START_ARRAY) {
            throw new JsonCastException(format("Expected a json array, but got %s", jsonParser.getText()));
        }
        BlockBuilder blockBuilder = arrayType.getElementType().createBlockBuilder(null, 20);
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            elementAppender.append(jsonParser, blockBuilder);
        }
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return blockBuilder.build();
    } catch (PrestoException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", arrayType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", arrayType, truncateIfNecessaryForErrorMessage(json)), e);
    }
}
Also used : JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) PrestoException(io.prestosql.spi.PrestoException) JsonCastException(io.prestosql.util.JsonCastException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) BlockBuilder(io.prestosql.spi.block.BlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 2 with UsedByGeneratedCode

use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.

the class MapConcatFunction method mapConcat.

@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Object state, Block[] maps) {
    int entries = 0;
    int lastMapIndex = maps.length - 1;
    int firstMapIndex = lastMapIndex;
    for (int i = 0; i < maps.length; i++) {
        entries += maps[i].getPositionCount();
        if (maps[i].getPositionCount() > 0) {
            lastMapIndex = i;
            firstMapIndex = min(firstMapIndex, i);
        }
    }
    if (lastMapIndex == firstMapIndex) {
        return maps[lastMapIndex];
    }
    PageBuilder pageBuilder = (PageBuilder) state;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    // TODO: we should move TypedSet into user state as well
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    TypedSet typedSet = new TypedSet(keyType, entries / 2, FUNCTION_NAME);
    BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    // the last map
    Block map = maps[lastMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        typedSet.add(map, i);
        keyType.appendTo(map, i, blockBuilder);
        valueType.appendTo(map, i + 1, blockBuilder);
    }
    // the map between the last and the first
    for (int idx = lastMapIndex - 1; idx > firstMapIndex; idx--) {
        map = maps[idx];
        for (int i = 0; i < map.getPositionCount(); i += 2) {
            if (!typedSet.contains(map, i)) {
                typedSet.add(map, i);
                keyType.appendTo(map, i, blockBuilder);
                valueType.appendTo(map, i + 1, blockBuilder);
            }
        }
    }
    // the first map
    map = maps[firstMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        if (!typedSet.contains(map, i)) {
            keyType.appendTo(map, i, blockBuilder);
            valueType.appendTo(map, i + 1, blockBuilder);
        }
    }
    mapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) TypedSet(io.prestosql.operator.aggregation.TypedSet) Block(io.prestosql.spi.block.Block) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 3 with UsedByGeneratedCode

use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.

the class DecimalCasts method varcharToShortDecimal.

@UsedByGeneratedCode
public static long varcharToShortDecimal(Slice value, long precision, long scale, long tenToScale) {
    BigDecimal result;
    String stringValue = value.toString(UTF_8);
    try {
        result = new BigDecimal(stringValue).setScale(DecimalConversions.intScale(scale), HALF_UP);
    } catch (NumberFormatException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value is not a number.", stringValue, precision, scale));
    }
    if (overflows(result, precision)) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value too large.", stringValue, precision, scale));
    }
    return result.unscaledValue().longValue();
}
Also used : PrestoException(io.prestosql.spi.PrestoException) BigDecimal(java.math.BigDecimal) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 4 with UsedByGeneratedCode

use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.

the class DecimalCasts method jsonToLongDecimal.

@UsedByGeneratedCode
public static Slice jsonToLongDecimal(Slice json, long precision, long scale, BigInteger tenToScale) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Slice result = currentTokenAsLongDecimal(parser, intPrecision(precision), DecimalConversions.intScale(scale));
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DECIMAL(%s,%s)", precision, scale);
        return result;
    } catch (IOException | NumberFormatException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
    }
}
Also used : Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 5 with UsedByGeneratedCode

use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.

the class DecimalCasts method varcharToLongDecimal.

@UsedByGeneratedCode
public static Slice varcharToLongDecimal(Slice value, long precision, long scale, BigInteger tenToScale) {
    BigDecimal result;
    String stringValue = value.toString(UTF_8);
    try {
        result = new BigDecimal(stringValue).setScale(DecimalConversions.intScale(scale), HALF_UP);
    } catch (NumberFormatException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value is not a number.", stringValue, precision, scale));
    }
    if (overflows(result, precision)) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value too large.", stringValue, precision, scale));
    }
    return encodeUnscaledValue(result.unscaledValue());
}
Also used : PrestoException(io.prestosql.spi.PrestoException) BigDecimal(java.math.BigDecimal) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Aggregations

UsedByGeneratedCode (io.prestosql.spi.annotation.UsedByGeneratedCode)38 BlockBuilder (io.prestosql.spi.block.BlockBuilder)20 PrestoException (io.prestosql.spi.PrestoException)12 SingleMapBlock (io.prestosql.spi.block.SingleMapBlock)10 JsonParser (com.fasterxml.jackson.core.JsonParser)5 JsonCastException (io.prestosql.util.JsonCastException)5 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)5 PageBuilder (io.prestosql.spi.PageBuilder)4 Block (io.prestosql.spi.block.Block)4 IOException (java.io.IOException)4 Slice (io.airlift.slice.Slice)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)2 SliceOutput (io.airlift.slice.SliceOutput)2 TypedSet (io.prestosql.operator.aggregation.TypedSet)2 Type (io.prestosql.spi.type.Type)2 JsonUtil.createJsonGenerator (io.prestosql.util.JsonUtil.createJsonGenerator)2 BigDecimal (java.math.BigDecimal)2 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)1 QueryState (io.prestosql.execution.QueryState)1