Search in sources :

Example 1 with HashTable

use of io.prestosql.util.JsonUtil.HashTable in project hetu-core by openlookeng.

the class TestJsonHashTable method testBigint.

@Test
public void testBigint() {
    Random rand = new Random(SEED);
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, NUM_ROUNDS * (NUM_RANDOM_VALUES_IN_ROUND + NUM_EXISTING_VALUES_IN_ROUND));
    HashTable hashTable = new HashTable(BIGINT, blockBuilder);
    Set<Long> valueSet = new HashSet<>();
    List<Long> valueList = new ArrayList<>();
    for (int i = 0; i < NUM_ROUNDS; i++) {
        for (int j = 0; j < NUM_RANDOM_VALUES_IN_ROUND; j++) {
            long value = rand.nextLong();
            BIGINT.writeLong(blockBuilder, value);
            if (valueSet.contains(value)) {
                assertTrue(hashTable.contains(blockBuilder.getPositionCount() - 1));
                assertFalse(hashTable.addIfAbsent(blockBuilder.getPositionCount() - 1));
            } else {
                valueSet.add(value);
                valueList.add(value);
                assertFalse(hashTable.contains(blockBuilder.getPositionCount() - 1));
                assertTrue(hashTable.addIfAbsent(blockBuilder.getPositionCount() - 1));
            }
        }
        for (int j = 0; j < NUM_EXISTING_VALUES_IN_ROUND; j++) {
            // Randomly pick a existing value
            long value = valueList.get(rand.nextInt(valueList.size()));
            BIGINT.writeLong(blockBuilder, value);
            assertTrue(hashTable.contains(blockBuilder.getPositionCount() - 1));
            assertFalse(hashTable.addIfAbsent(blockBuilder.getPositionCount() - 1));
        }
    }
}
Also used : HashTable(io.prestosql.util.JsonUtil.HashTable) Random(java.util.Random) ArrayList(java.util.ArrayList) BlockBuilder(io.prestosql.spi.block.BlockBuilder) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 2 with HashTable

use of io.prestosql.util.JsonUtil.HashTable in project hetu-core by openlookeng.

the class TestJsonHashTable method testVarchar.

@Test
public void testVarchar() {
    Random rand = new Random(SEED);
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, NUM_ROUNDS * (NUM_RANDOM_VALUES_IN_ROUND + NUM_EXISTING_VALUES_IN_ROUND));
    HashTable hashTable = new HashTable(VARCHAR, blockBuilder);
    Set<Slice> valueSet = new HashSet<>();
    List<Slice> valueList = new ArrayList<>();
    for (int i = 0; i < NUM_ROUNDS; i++) {
        for (int j = 0; j < NUM_RANDOM_VALUES_IN_ROUND; j++) {
            Slice value = generateRandomSlice(rand, 10);
            VARCHAR.writeSlice(blockBuilder, value);
            if (valueSet.contains(value)) {
                assertTrue(hashTable.contains(blockBuilder.getPositionCount() - 1));
                assertFalse(hashTable.addIfAbsent(blockBuilder.getPositionCount() - 1));
            } else {
                valueSet.add(value);
                valueList.add(value);
                assertFalse(hashTable.contains(blockBuilder.getPositionCount() - 1));
                assertTrue(hashTable.addIfAbsent(blockBuilder.getPositionCount() - 1));
            }
        }
        for (int j = 0; j < NUM_EXISTING_VALUES_IN_ROUND; j++) {
            // randomly pick a existing value
            Slice value = valueList.get(rand.nextInt(valueList.size()));
            VARCHAR.writeSlice(blockBuilder, value);
            assertTrue(hashTable.contains(blockBuilder.getPositionCount() - 1));
            assertFalse(hashTable.addIfAbsent(blockBuilder.getPositionCount() - 1));
        }
    }
}
Also used : HashTable(io.prestosql.util.JsonUtil.HashTable) Random(java.util.Random) Slice(io.airlift.slice.Slice) ArrayList(java.util.ArrayList) BlockBuilder(io.prestosql.spi.block.BlockBuilder) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 3 with HashTable

use of io.prestosql.util.JsonUtil.HashTable 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)

Aggregations

BlockBuilder (io.prestosql.spi.block.BlockBuilder)3 HashTable (io.prestosql.util.JsonUtil.HashTable)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Random (java.util.Random)2 Test (org.testng.annotations.Test)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 Slice (io.airlift.slice.Slice)1 PrestoException (io.prestosql.spi.PrestoException)1 UsedByGeneratedCode (io.prestosql.spi.annotation.UsedByGeneratedCode)1 JsonCastException (io.prestosql.util.JsonCastException)1 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)1