use of io.prestosql.spi.type.RowType 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;
}
use of io.prestosql.spi.type.RowType in project pulsar by apache.
the class PulsarAvroColumnDecoder method serializeRow.
private static Block serializeRow(BlockBuilder parentBlockBuilder, Object value, Type type, String columnName) {
if (value == null) {
checkState(parentBlockBuilder != null, "parent block builder is null");
parentBlockBuilder.appendNull();
return null;
}
BlockBuilder blockBuilder;
if (parentBlockBuilder != null) {
blockBuilder = parentBlockBuilder;
} else {
blockBuilder = type.createBlockBuilder(null, 1);
}
BlockBuilder singleRowBuilder = blockBuilder.beginBlockEntry();
GenericRecord record = (GenericRecord) value;
List<Field> fields = ((RowType) type).getFields();
for (Field field : fields) {
checkState(field.getName().isPresent(), "field name not found");
serializeObject(singleRowBuilder, record.get(field.getName().get()), field.getType(), columnName);
}
blockBuilder.closeEntry();
if (parentBlockBuilder == null) {
return blockBuilder.getObject(0, Block.class);
}
return null;
}
use of io.prestosql.spi.type.RowType in project pulsar by yahoo.
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);
}
use of io.prestosql.spi.type.RowType 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);
}
}
}
use of io.prestosql.spi.type.RowType in project pulsar by yahoo.
the class PulsarAvroColumnDecoder method serializeRow.
private static Block serializeRow(BlockBuilder parentBlockBuilder, Object value, Type type, String columnName) {
if (value == null) {
checkState(parentBlockBuilder != null, "parent block builder is null");
parentBlockBuilder.appendNull();
return null;
}
BlockBuilder blockBuilder;
if (parentBlockBuilder != null) {
blockBuilder = parentBlockBuilder;
} else {
blockBuilder = type.createBlockBuilder(null, 1);
}
BlockBuilder singleRowBuilder = blockBuilder.beginBlockEntry();
GenericRecord record = (GenericRecord) value;
List<Field> fields = ((RowType) type).getFields();
for (Field field : fields) {
checkState(field.getName().isPresent(), "field name not found");
serializeObject(singleRowBuilder, record.get(field.getName().get()), field.getType(), columnName);
}
blockBuilder.closeEntry();
if (parentBlockBuilder == null) {
return blockBuilder.getObject(0, Block.class);
}
return null;
}
Aggregations