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;
}
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));
}
}
}
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);
}
}
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();
}
}
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);
}
Aggregations