Search in sources :

Example 41 with RowType

use of io.prestosql.spi.type.RowType in project incubator-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) DecimalType(io.prestosql.spi.type.DecimalType) 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 42 with RowType

use of io.prestosql.spi.type.RowType in project incubator-pulsar by apache.

the class ProtobufNativeDecoderTestUtil method checkArrayValues.

public void checkArrayValues(Block block, Type type, Object value) {
    assertNotNull(type, "Type is null");
    assertTrue(type instanceof ArrayType, "Unexpected type");
    assertNotNull(block, "Block is null");
    assertNotNull(value, "Value is null");
    List<?> list = (List<?>) value;
    assertEquals(block.getPositionCount(), list.size());
    Type elementType = ((ArrayType) type).getElementType();
    if (elementType instanceof ArrayType) {
        for (int index = 0; index < block.getPositionCount(); index++) {
            if (block.isNull(index)) {
                assertNull(list.get(index));
                continue;
            }
            Block arrayBlock = block.getObject(index, Block.class);
            checkArrayValues(arrayBlock, elementType, list.get(index));
        }
    } else if (elementType instanceof MapType) {
        for (int index = 0; index < block.getPositionCount(); index++) {
            if (block.isNull(index)) {
                assertNull(list.get(index));
                continue;
            }
            Block mapBlock = block.getObject(index, Block.class);
            checkMapValues(mapBlock, elementType, list.get(index));
        }
    } else if (elementType instanceof RowType) {
        for (int index = 0; index < block.getPositionCount(); index++) {
            if (block.isNull(index)) {
                assertNull(list.get(index));
                continue;
            }
            Block rowBlock = block.getObject(index, Block.class);
            checkRowValues(rowBlock, elementType, list.get(index));
        }
    } else {
        for (int index = 0; index < block.getPositionCount(); index++) {
            checkPrimitiveValue(getObjectValue(elementType, block, index), list.get(index));
        }
    }
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) 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) List(java.util.List) MapType(io.prestosql.spi.type.MapType)

Example 43 with RowType

use of io.prestosql.spi.type.RowType in project incubator-pulsar by apache.

the class ProtobufNativeDecoderTestUtil method checkRowValues.

public void checkRowValues(Block block, Type type, Object value) {
    assertNotNull(type, "Type is null");
    assertTrue(type instanceof RowType, "Unexpected type");
    assertNotNull(block, "Block is null");
    assertNotNull(value, "Value is null");
    DynamicMessage record = (DynamicMessage) value;
    RowType rowType = (RowType) type;
    assertEquals(record.getAllFields().size(), rowType.getFields().size(), "Protobuf field size mismatch");
    assertEquals(block.getPositionCount(), rowType.getFields().size(), "Presto type field size mismatch");
    for (int fieldIndex = 0; fieldIndex < rowType.getFields().size(); fieldIndex++) {
        RowType.Field rowField = rowType.getFields().get(fieldIndex);
        Object expectedValue = record.getField(((DynamicMessage) value).getDescriptorForType().findFieldByName(rowField.getName().get()));
        if (block.isNull(fieldIndex)) {
            assertNull(expectedValue);
            continue;
        }
        checkField(block, rowField.getType(), fieldIndex, expectedValue);
    }
}
Also used : RowType(io.prestosql.spi.type.RowType) DynamicMessage(com.google.protobuf.DynamicMessage)

Example 44 with RowType

use of io.prestosql.spi.type.RowType in project incubator-pulsar by apache.

the class TestPulsarRecordCursor method testTopics.

@Test(singleThreaded = true)
public void testTopics() throws Exception {
    for (Map.Entry<TopicName, PulsarRecordCursor> entry : pulsarRecordCursors.entrySet()) {
        log.info("!------ topic %s ------!", entry.getKey());
        setup();
        List<PulsarColumnHandle> fooColumnHandles = topicsToColumnHandles.get(entry.getKey());
        PulsarRecordCursor pulsarRecordCursor = entry.getValue();
        PulsarSqlSchemaInfoProvider pulsarSqlSchemaInfoProvider = mock(PulsarSqlSchemaInfoProvider.class);
        when(pulsarSqlSchemaInfoProvider.getSchemaByVersion(any())).thenReturn(completedFuture(topicsToSchemas.get(entry.getKey().getSchemaName())));
        pulsarRecordCursor.setPulsarSqlSchemaInfoProvider(pulsarSqlSchemaInfoProvider);
        TopicName topicName = entry.getKey();
        int count = 0;
        while (pulsarRecordCursor.advanceNextPosition()) {
            List<String> columnsSeen = new LinkedList<>();
            for (int i = 0; i < fooColumnHandles.size(); i++) {
                if (pulsarRecordCursor.isNull(i)) {
                    columnsSeen.add(fooColumnHandles.get(i).getName());
                } else {
                    if (fooColumnHandles.get(i).getName().equals("field1")) {
                        assertEquals(pulsarRecordCursor.getLong(i), ((Integer) fooFunctions.get("field1").apply(count)).longValue());
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("field2")) {
                        assertEquals(pulsarRecordCursor.getSlice(i).getBytes(), ((String) fooFunctions.get("field2").apply(count)).getBytes());
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("field3")) {
                        assertEquals(pulsarRecordCursor.getLong(i), Float.floatToIntBits((Float) fooFunctions.get("field3").apply(count)));
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("field4")) {
                        assertEquals(pulsarRecordCursor.getDouble(i), ((Double) fooFunctions.get("field4").apply(count)).doubleValue());
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("field5")) {
                        assertEquals(pulsarRecordCursor.getBoolean(i), ((Boolean) fooFunctions.get("field5").apply(count)).booleanValue());
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("field6")) {
                        assertEquals(pulsarRecordCursor.getLong(i), ((Long) fooFunctions.get("field6").apply(count)).longValue());
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("timestamp")) {
                        pulsarRecordCursor.getLong(i);
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("time")) {
                        pulsarRecordCursor.getLong(i);
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("date")) {
                        pulsarRecordCursor.getLong(i);
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("bar")) {
                        assertTrue(fooColumnHandles.get(i).getType() instanceof RowType);
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("field7")) {
                        assertEquals(pulsarRecordCursor.getSlice(i).getBytes(), fooFunctions.get("field7").apply(count).toString().getBytes());
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else if (fooColumnHandles.get(i).getName().equals("decimal")) {
                        Type type = fooColumnHandles.get(i).getType();
                        // In JsonDecoder, decimal trans to varcharType
                        if (type instanceof VarcharType) {
                            assertEquals(new String(pulsarRecordCursor.getSlice(i).getBytes()), fooFunctions.get("decimal").apply(count).toString());
                        } else {
                            DecimalType decimalType = (DecimalType) fooColumnHandles.get(i).getType();
                            assertEquals(BigDecimal.valueOf(pulsarRecordCursor.getLong(i), decimalType.getScale()), fooFunctions.get("decimal").apply(count));
                        }
                        columnsSeen.add(fooColumnHandles.get(i).getName());
                    } else {
                        if (PulsarInternalColumn.getInternalFieldsMap().containsKey(fooColumnHandles.get(i).getName())) {
                            columnsSeen.add(fooColumnHandles.get(i).getName());
                        }
                    }
                }
            }
            assertEquals(columnsSeen.size(), fooColumnHandles.size());
            count++;
        }
        assertEquals(count, topicsToNumEntries.get(topicName.getSchemaName()).longValue());
        assertEquals(pulsarRecordCursor.getCompletedBytes(), completedBytes);
        cleanup();
        pulsarRecordCursor.close();
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) RowType(io.prestosql.spi.type.RowType) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedList(java.util.LinkedList) TopicName(org.apache.pulsar.common.naming.TopicName) DecimalType(io.prestosql.spi.type.DecimalType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) SchemaType(org.apache.pulsar.common.schema.SchemaType) KeyValueEncodingType(org.apache.pulsar.common.schema.KeyValueEncodingType) VarcharType(io.prestosql.spi.type.VarcharType) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) DecimalType(io.prestosql.spi.type.DecimalType) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 45 with RowType

use of io.prestosql.spi.type.RowType in project incubator-pulsar by apache.

the class TestAvroDecoder method testRow.

@Test
public void testRow() {
    DecoderTestMessage message = new DecoderTestMessage();
    message.stringField = "message_2";
    DecoderTestMessage.TestRow testRow = new DecoderTestMessage.TestRow();
    message.rowField = testRow;
    testRow.intField = 22;
    testRow.stringField = "message_2_testRow";
    DecoderTestMessage.NestedRow nestedRow = new DecoderTestMessage.NestedRow();
    nestedRow.longField = 222L;
    nestedRow.stringField = "message_2_nestedRow";
    testRow.nestedRow = nestedRow;
    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("rowField");
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
    RowType columnType = RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("intField", INTEGER)).add(RowType.field("nestedRow", RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("longField", BIGINT)).add(RowType.field("stringField", VARCHAR)).build()))).add(RowType.field("stringField", VARCHAR)).build());
    PulsarColumnHandle columnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "rowField", columnType, false, false, "rowField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkRowValues(getBlock(decodedRow, columnHandle), columnHandle.getType(), fieldValue);
}
Also used : FieldValueProvider(io.prestosql.decoder.FieldValueProvider) RowType(io.prestosql.spi.type.RowType) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) Test(org.testng.annotations.Test)

Aggregations

RowType (io.prestosql.spi.type.RowType)76 ArrayType (io.prestosql.spi.type.ArrayType)38 Type (io.prestosql.spi.type.Type)35 MapType (io.prestosql.spi.type.MapType)29 Test (org.testng.annotations.Test)25 List (java.util.List)19 ImmutableList (com.google.common.collect.ImmutableList)18 VarcharType (io.prestosql.spi.type.VarcharType)17 Block (io.prestosql.spi.block.Block)16 BlockBuilder (io.prestosql.spi.block.BlockBuilder)15 Map (java.util.Map)12 BigintType (io.prestosql.spi.type.BigintType)11 DecimalType (io.prestosql.spi.type.DecimalType)11 DoubleType (io.prestosql.spi.type.DoubleType)11 BooleanType (io.prestosql.spi.type.BooleanType)10 IntegerType (io.prestosql.spi.type.IntegerType)10 RealType (io.prestosql.spi.type.RealType)10 SmallintType (io.prestosql.spi.type.SmallintType)10 TimestampType (io.prestosql.spi.type.TimestampType)10 VarbinaryType (io.prestosql.spi.type.VarbinaryType)10