Search in sources :

Example 26 with UsedByGeneratedCode

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

the class MapToJsonCast method toJson.

@UsedByGeneratedCode
public static Slice toJson(ObjectKeyProvider provider, JsonGeneratorWriter writer, Block block) {
    try {
        Map<String, Integer> orderedKeyToValuePosition = new TreeMap<>();
        for (int i = 0; i < block.getPositionCount(); i += 2) {
            String objectKey = provider.getObjectKey(block, i);
            orderedKeyToValuePosition.put(objectKey, i + 1);
        }
        SliceOutput output = new DynamicSliceOutput(40);
        try (JsonGenerator jsonGenerator = createJsonGenerator(JSON_FACTORY, output)) {
            jsonGenerator.writeStartObject();
            for (Map.Entry<String, Integer> entry : orderedKeyToValuePosition.entrySet()) {
                jsonGenerator.writeFieldName(entry.getKey());
                writer.writeJsonValue(jsonGenerator, block, entry.getValue());
            }
            jsonGenerator.writeEndObject();
        }
        return output.slice();
    } catch (IOException e) {
        throwIfUnchecked(e);
        throw new RuntimeException(e);
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonUtil.createJsonGenerator(io.prestosql.util.JsonUtil.createJsonGenerator) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 27 with UsedByGeneratedCode

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

the class JsonToMapCast method toMap.

@UsedByGeneratedCode
public static Block toMap(MapType mapType, BlockBuilderAppender keyAppender, BlockBuilderAppender valueAppender, 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_OBJECT) {
            throw new JsonCastException(format("Expected a json object, but got %s", jsonParser.getText()));
        }
        BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
        BlockBuilder singleMapBlockBuilder = mapBlockBuilder.beginBlockEntry();
        HashTable hashTable = new HashTable(mapType.getKeyType(), singleMapBlockBuilder);
        int position = 0;
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            keyAppender.append(jsonParser, singleMapBlockBuilder);
            jsonParser.nextToken();
            valueAppender.append(jsonParser, singleMapBlockBuilder);
            // For example: CAST(JSON '{"1": 1, "01": 2}' AS MAP<INTEGER, INTEGER>).
            if (!hashTable.addIfAbsent(position)) {
                throw new JsonCastException("Duplicate keys are not allowed");
            }
            position += 2;
        }
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        mapBlockBuilder.closeEntry();
        return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
    } catch (PrestoException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", mapType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", mapType, truncateIfNecessaryForErrorMessage(json)), e);
    }
}
Also used : HashTable(io.prestosql.util.JsonUtil.HashTable) 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 28 with UsedByGeneratedCode

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

the class JsonToRowCast method toRow.

@UsedByGeneratedCode
public static Block toRow(RowType rowType, BlockBuilderAppender[] fieldAppenders, Optional<Map<String, Integer>> fieldNameToIndex, 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 && jsonParser.getCurrentToken() != START_OBJECT) {
            throw new JsonCastException(format("Expected a json array or object, but got %s", jsonParser.getText()));
        }
        BlockBuilder rowBlockBuilder = rowType.createBlockBuilder(null, 1);
        parseJsonToSingleRowBlock(jsonParser, (SingleRowBlockWriter) rowBlockBuilder.beginBlockEntry(), fieldAppenders, fieldNameToIndex);
        rowBlockBuilder.closeEntry();
        if (jsonParser.nextToken() != null) {
            throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
        }
        return rowType.getObject(rowBlockBuilder, 0);
    } catch (PrestoException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", rowType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
    } catch (Exception e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", rowType, 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 29 with UsedByGeneratedCode

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

the class MapElementAtFunction method elementAt.

@UsedByGeneratedCode
public static Object elementAt(MethodHandle keyEqualsMethod, Type keyType, Type valueType, Block map, Object key) {
    SingleMapBlock mapBlock = (SingleMapBlock) map;
    int valuePosition = mapBlock.seekKeyExact((Block) key);
    if (valuePosition == -1) {
        return null;
    }
    return readNativeValue(valueType, mapBlock, valuePosition);
}
Also used : SingleMapBlock(io.prestosql.spi.block.SingleMapBlock) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 30 with UsedByGeneratedCode

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

the class MapElementAtFunction method elementAt.

@UsedByGeneratedCode
public static Object elementAt(MethodHandle keyEqualsMethod, Type keyType, Type valueType, Block map, boolean key) {
    SingleMapBlock mapBlock = (SingleMapBlock) map;
    int valuePosition = mapBlock.seekKeyExact(key);
    if (valuePosition == -1) {
        return null;
    }
    return readNativeValue(valueType, mapBlock, valuePosition);
}
Also used : SingleMapBlock(io.prestosql.spi.block.SingleMapBlock) 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