Search in sources :

Example 96 with ArrayType

use of io.prestosql.spi.type.ArrayType in project pulsar by apache.

the class TestAvroDecoder method testArray.

@Test
public void testArray() {
    DecoderTestMessage message = new DecoderTestMessage();
    message.arrayField = Arrays.asList("message_1", "message_2", "message_3");
    byte[] bytes = schema.encode(message);
    ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
    GenericAvroRecord genericRecord = (GenericAvroRecord) GenericAvroSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getAvroRecord().get("arrayField");
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
    ArrayType columnType = new ArrayType(VARCHAR);
    PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "arrayField", columnType, false, false, "arrayField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkArrayValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) Test(org.testng.annotations.Test)

Example 97 with ArrayType

use of io.prestosql.spi.type.ArrayType in project pulsar by apache.

the class PulsarAvroColumnDecoder method isSupportedType.

private boolean isSupportedType(Type type) {
    if (isSupportedPrimitive(type)) {
        return true;
    }
    if (type instanceof ArrayType) {
        checkArgument(type.getTypeParameters().size() == 1, "expecting exactly one type parameter for array");
        return isSupportedType(type.getTypeParameters().get(0));
    }
    if (type instanceof MapType) {
        List<Type> typeParameters = type.getTypeParameters();
        checkArgument(typeParameters.size() == 2, "expecting exactly two type parameters for map");
        checkArgument(typeParameters.get(0) instanceof VarcharType, "Unsupported column type '%s' for map key", typeParameters.get(0));
        return isSupportedType(type.getTypeParameters().get(1));
    }
    if (type instanceof RowType) {
        for (Type fieldType : type.getTypeParameters()) {
            if (!isSupportedType(fieldType)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) BigintType(io.prestosql.spi.type.BigintType) MapType(io.prestosql.spi.type.MapType) DoubleType(io.prestosql.spi.type.DoubleType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) SmallintType(io.prestosql.spi.type.SmallintType) IntegerType(io.prestosql.spi.type.IntegerType) TimeType(io.prestosql.spi.type.TimeType) RealType(io.prestosql.spi.type.RealType) ArrayType(io.prestosql.spi.type.ArrayType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TinyintType(io.prestosql.spi.type.TinyintType) TimestampType(io.prestosql.spi.type.TimestampType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) VarcharType(io.prestosql.spi.type.VarcharType) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType)

Example 98 with ArrayType

use of io.prestosql.spi.type.ArrayType in project pulsar by yahoo.

the class TestAvroDecoder method testArray.

@Test
public void testArray() {
    DecoderTestMessage message = new DecoderTestMessage();
    message.arrayField = Arrays.asList("message_1", "message_2", "message_3");
    byte[] bytes = schema.encode(message);
    ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
    GenericAvroRecord genericRecord = (GenericAvroRecord) GenericAvroSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getAvroRecord().get("arrayField");
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
    ArrayType columnType = new ArrayType(VARCHAR);
    PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "arrayField", columnType, false, false, "arrayField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkArrayValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) Test(org.testng.annotations.Test)

Example 99 with ArrayType

use of io.prestosql.spi.type.ArrayType in project pulsar by yahoo.

the class ProtobufNativeDecoderTestUtil method checkMapValues.

public void checkMapValues(Block block, Type type, Object value) {
    assertNotNull(type, "Type is null");
    assertTrue(type instanceof MapType, "Unexpected type");
    assertNotNull(block, "Block is null");
    assertNotNull(value, "Value is null");
    Map<?, ?> expected = PulsarProtobufNativeColumnDecoder.parseProtobufMap(value);
    assertEquals(block.getPositionCount(), expected.size() * 2);
    Type valueType = ((MapType) type).getValueType();
    // protobuf3 keyType only support integral or string type
    Type keyType = ((MapType) type).getKeyType();
    // check value
    if (valueType instanceof ArrayType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            Object actualKey = getObjectValue(keyType, block, index);
            assertTrue(expected.keySet().stream().anyMatch(e -> e.equals(actualKey)));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block arrayBlock = block.getObject(index + 1, Block.class);
            Object keyValue = expected.entrySet().stream().filter(e -> e.getKey().equals(actualKey)).findFirst().get().getValue();
            checkArrayValues(arrayBlock, valueType, keyValue);
        }
    } else if (valueType instanceof MapType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            Object actualKey = getObjectValue(keyType, block, index);
            assertTrue(expected.keySet().stream().anyMatch(e -> e.equals(actualKey)));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block mapBlock = block.getObject(index + 1, Block.class);
            Object keyValue = expected.entrySet().stream().filter(e -> e.getKey().equals(actualKey)).findFirst().get().getValue();
            checkMapValues(mapBlock, valueType, keyValue);
        }
    } else if (valueType instanceof RowType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            Object actualKey = getObjectValue(keyType, block, index);
            assertTrue(expected.keySet().stream().anyMatch(e -> e.equals(actualKey)));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block rowBlock = block.getObject(index + 1, Block.class);
            Object keyValue = expected.entrySet().stream().filter(e -> e.getKey().equals(actualKey)).findFirst().get().getValue();
            checkRowValues(rowBlock, valueType, keyValue);
        }
    } else {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            Object actualKey = getObjectValue(keyType, block, index);
            assertTrue(expected.keySet().stream().anyMatch(e -> e.equals(actualKey)));
            Object keyValue = expected.entrySet().stream().filter(e -> e.getKey().equals(actualKey)).findFirst().get().getValue();
            checkPrimitiveValue(getObjectValue(valueType, block, index + 1), keyValue);
        }
    }
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) EnumValue(com.google.protobuf.EnumValue) Assert.assertNull(org.testng.Assert.assertNull) DynamicMessage(com.google.protobuf.DynamicMessage) ArrayType(io.prestosql.spi.type.ArrayType) Assert.fail(org.testng.Assert.fail) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertNotNull(org.testng.Assert.assertNotNull) String.format(java.lang.String.format) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) ByteString(com.google.protobuf.ByteString) MapType(io.prestosql.spi.type.MapType) List(java.util.List) Map(java.util.Map) RowType(io.prestosql.spi.type.RowType) DecoderTestUtil(org.apache.pulsar.sql.presto.decoder.DecoderTestUtil) Assert.assertTrue(org.testng.Assert.assertTrue) Type(io.prestosql.spi.type.Type) Block(io.prestosql.spi.block.Block) ArrayType(io.prestosql.spi.type.ArrayType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) Block(io.prestosql.spi.block.Block) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType)

Example 100 with ArrayType

use of io.prestosql.spi.type.ArrayType in project pulsar by yahoo.

the class TestProtobufNativeDecoder method testArray.

@Test
public void testArray() {
    TestMsg.TestMessage testMessage = TestMsg.TestMessage.newBuilder().addRepeatedField("first").addRepeatedField("second").build();
    byte[] bytes = schema.encode(testMessage);
    ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(bytes);
    GenericProtobufNativeRecord genericRecord = (GenericProtobufNativeRecord) GenericProtobufNativeSchema.of(schemaInfo).decode(bytes);
    Object fieldValue = genericRecord.getProtobufRecord().getField(genericRecord.getProtobufRecord().getDescriptorForType().findFieldByName("repeatedField"));
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
    ArrayType columnType = new ArrayType(VARCHAR);
    PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "repeatedField", columnType, false, false, "repeatedField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkArrayValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) ByteBuf(io.netty.buffer.ByteBuf) GenericProtobufNativeRecord(org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) Test(org.testng.annotations.Test)

Aggregations

ArrayType (io.prestosql.spi.type.ArrayType)250 Test (org.testng.annotations.Test)182 RowType (io.prestosql.spi.type.RowType)100 Type (io.prestosql.spi.type.Type)99 List (java.util.List)86 ImmutableList (com.google.common.collect.ImmutableList)84 ArrayList (java.util.ArrayList)73 Arrays.asList (java.util.Arrays.asList)64 Collections.singletonList (java.util.Collections.singletonList)64 MessageType (org.apache.parquet.schema.MessageType)58 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)54 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)53 DecimalType.createDecimalType (io.prestosql.spi.type.DecimalType.createDecimalType)49 StructuralTestUtil.mapType (io.prestosql.tests.StructuralTestUtil.mapType)48 MapType (io.prestosql.spi.type.MapType)31 BigInteger (java.math.BigInteger)24 Map (java.util.Map)23 HashMap (java.util.HashMap)21 Block (io.prestosql.spi.block.Block)20 VarcharType (io.prestosql.spi.type.VarcharType)20