Search in sources :

Example 51 with DecoderColumnHandle

use of io.prestosql.decoder.DecoderColumnHandle in project pulsar by apache.

the class TestJsonDecoder method testPrimitiveType.

@Test
public void testPrimitiveType() {
    DecoderTestMessage message = new DecoderTestMessage();
    message.stringField = "message_1";
    message.intField = 22;
    message.floatField = 2.2f;
    message.doubleField = 22.20D;
    message.booleanField = true;
    message.longField = 222L;
    message.timestampField = System.currentTimeMillis();
    message.enumField = DecoderTestMessage.TestEnum.TEST_ENUM_2;
    LocalTime now = LocalTime.now(ZoneId.systemDefault());
    message.timeField = now.toSecondOfDay() * 1000;
    LocalDate localDate = LocalDate.now();
    LocalDate epoch = LocalDate.ofEpochDay(0);
    message.dateField = Math.toIntExact(ChronoUnit.DAYS.between(epoch, localDate));
    ByteBuf payload = io.netty.buffer.Unpooled.copiedBuffer(schema.encode(message));
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = pulsarRowDecoder.decodeRow(payload).get();
    PulsarColumnHandle stringFieldColumnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "stringField", VARCHAR, false, false, "stringField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkValue(decodedRow, stringFieldColumnHandle, message.stringField);
    PulsarColumnHandle intFieldColumnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "intField", INTEGER, false, false, "intField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkValue(decodedRow, intFieldColumnHandle, message.intField);
    PulsarColumnHandle floatFieldColumnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "floatField", REAL, false, false, "floatField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkValue(decodedRow, floatFieldColumnHandle, floatToIntBits(message.floatField));
    PulsarColumnHandle doubleFieldColumnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "doubleField", DOUBLE, false, false, "doubleField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkValue(decodedRow, doubleFieldColumnHandle, message.doubleField);
    PulsarColumnHandle booleanFieldColumnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "booleanField", BOOLEAN, false, false, "booleanField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkValue(decodedRow, booleanFieldColumnHandle, message.booleanField);
    PulsarColumnHandle longFieldColumnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "longField", BIGINT, false, false, "longField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkValue(decodedRow, longFieldColumnHandle, message.longField);
    PulsarColumnHandle enumFieldColumnHandle = new PulsarColumnHandle(getPulsarConnectorId().toString(), "enumField", VARCHAR, false, false, "enumField", null, null, PulsarColumnHandle.HandleKeyValueType.NONE);
    checkValue(decodedRow, enumFieldColumnHandle, message.enumField.toString());
}
Also used : LocalTime(java.time.LocalTime) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) ByteBuf(io.netty.buffer.ByteBuf) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) LocalDate(java.time.LocalDate) DecoderTestMessage(org.apache.pulsar.sql.presto.decoder.DecoderTestMessage) PulsarColumnHandle(org.apache.pulsar.sql.presto.PulsarColumnHandle) Test(org.testng.annotations.Test)

Example 52 with DecoderColumnHandle

use of io.prestosql.decoder.DecoderColumnHandle in project hetu-core by openlookeng.

the class JsonFieldDecoderTester method decode.

private FieldValueProvider decode(Optional<String> jsonValue, Type type) {
    String jsonField = "value";
    String json = jsonValue.map(value -> format("{\"%s\":%s}", jsonField, value)).orElse("{}");
    DecoderTestColumnHandle columnHandle = new DecoderTestColumnHandle(0, "some_column", type, jsonField, dataFormat.orElse(null), formatHint.orElse(null), false, false, false);
    RowDecoder rowDecoder = DECODER_FACTORY.create(emptyMap(), ImmutableSet.of(columnHandle));
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = rowDecoder.decodeRow(json.getBytes(UTF_8), null).orElseThrow(AssertionError::new);
    assertTrue(decodedRow.containsKey(columnHandle), format("column '%s' not found in decoded row", columnHandle.getName()));
    return decodedRow.get(columnHandle);
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) Slice(io.airlift.slice.Slice) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertEquals(org.testng.Assert.assertEquals) Preconditions.checkArgument(org.assertj.core.util.Preconditions.checkArgument) String.format(java.lang.String.format) ObjectMapperProvider(io.airlift.json.ObjectMapperProvider) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) RowDecoder(io.prestosql.decoder.RowDecoder) DecoderTestColumnHandle(io.prestosql.decoder.DecoderTestColumnHandle) Type(io.prestosql.spi.type.Type) Assert.assertFalse(org.testng.Assert.assertFalse) DecoderTestColumnHandle(io.prestosql.decoder.DecoderTestColumnHandle) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) RowDecoder(io.prestosql.decoder.RowDecoder) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle)

Example 53 with DecoderColumnHandle

use of io.prestosql.decoder.DecoderColumnHandle in project hetu-core by openlookeng.

the class TestJsonDecoder method testStringNumber.

@Test
public void testStringNumber() {
    byte[] json = "{\"a_number\":481516,\"a_string\":\"2342\"}".getBytes(StandardCharsets.UTF_8);
    DecoderTestColumnHandle column1 = new DecoderTestColumnHandle(0, "column1", createVarcharType(100), "a_number", null, null, false, false, false);
    DecoderTestColumnHandle column2 = new DecoderTestColumnHandle(1, "column2", BIGINT, "a_number", null, null, false, false, false);
    DecoderTestColumnHandle column3 = new DecoderTestColumnHandle(2, "column3", createVarcharType(100), "a_string", null, null, false, false, false);
    DecoderTestColumnHandle column4 = new DecoderTestColumnHandle(3, "column4", BIGINT, "a_string", null, null, false, false, false);
    Set<DecoderColumnHandle> columns = ImmutableSet.of(column1, column2, column3, column4);
    RowDecoder rowDecoder = DECODER_FACTORY.create(emptyMap(), columns);
    Optional<Map<DecoderColumnHandle, FieldValueProvider>> decodedRow = rowDecoder.decodeRow(json, null);
    assertTrue(decodedRow.isPresent());
    assertEquals(decodedRow.get().size(), columns.size());
    checkValue(decodedRow.get(), column1, "481516");
    checkValue(decodedRow.get(), column2, 481516);
    checkValue(decodedRow.get(), column3, "2342");
    checkValue(decodedRow.get(), column4, 2342);
}
Also used : DecoderTestColumnHandle(io.prestosql.decoder.DecoderTestColumnHandle) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) RowDecoder(io.prestosql.decoder.RowDecoder) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) Test(org.testng.annotations.Test)

Example 54 with DecoderColumnHandle

use of io.prestosql.decoder.DecoderColumnHandle in project hetu-core by openlookeng.

the class TestJsonDecoder method testSimple.

@Test
public void testSimple() throws Exception {
    byte[] json = ByteStreams.toByteArray(TestJsonDecoder.class.getResourceAsStream("/decoder/json/message.json"));
    DecoderTestColumnHandle column1 = new DecoderTestColumnHandle(0, "column1", createVarcharType(100), "source", null, null, false, false, false);
    DecoderTestColumnHandle column2 = new DecoderTestColumnHandle(1, "column2", createVarcharType(10), "user/screen_name", null, null, false, false, false);
    DecoderTestColumnHandle column3 = new DecoderTestColumnHandle(2, "column3", BIGINT, "id", null, null, false, false, false);
    DecoderTestColumnHandle column4 = new DecoderTestColumnHandle(3, "column4", BIGINT, "user/statuses_count", null, null, false, false, false);
    DecoderTestColumnHandle column5 = new DecoderTestColumnHandle(4, "column5", BOOLEAN, "user/geo_enabled", null, null, false, false, false);
    Set<DecoderColumnHandle> columns = ImmutableSet.of(column1, column2, column3, column4, column5);
    RowDecoder rowDecoder = DECODER_FACTORY.create(emptyMap(), columns);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = rowDecoder.decodeRow(json, null).orElseThrow(AssertionError::new);
    assertEquals(decodedRow.size(), columns.size());
    checkValue(decodedRow, column1, "<a href=\"http://twitterfeed.com\" rel=\"nofollow\">twitterfeed</a>");
    checkValue(decodedRow, column2, "EKentuckyN");
    checkValue(decodedRow, column3, 493857959588286460L);
    checkValue(decodedRow, column4, 7630);
    checkValue(decodedRow, column5, true);
}
Also used : DecoderTestColumnHandle(io.prestosql.decoder.DecoderTestColumnHandle) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) RowDecoder(io.prestosql.decoder.RowDecoder) Test(org.testng.annotations.Test)

Example 55 with DecoderColumnHandle

use of io.prestosql.decoder.DecoderColumnHandle in project hetu-core by openlookeng.

the class TestJsonDecoder method testNonExistent.

@Test
public void testNonExistent() {
    byte[] json = "{}".getBytes(StandardCharsets.UTF_8);
    DecoderTestColumnHandle column1 = new DecoderTestColumnHandle(0, "column1", createVarcharType(100), "very/deep/varchar", null, null, false, false, false);
    DecoderTestColumnHandle column2 = new DecoderTestColumnHandle(1, "column2", BIGINT, "no_bigint", null, null, false, false, false);
    DecoderTestColumnHandle column3 = new DecoderTestColumnHandle(2, "column3", DOUBLE, "double/is_missing", null, null, false, false, false);
    DecoderTestColumnHandle column4 = new DecoderTestColumnHandle(3, "column4", BOOLEAN, "hello", null, null, false, false, false);
    Set<DecoderColumnHandle> columns = ImmutableSet.of(column1, column2, column3, column4);
    RowDecoder rowDecoder = DECODER_FACTORY.create(emptyMap(), columns);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = rowDecoder.decodeRow(json, null).orElseThrow(AssertionError::new);
    assertEquals(decodedRow.size(), columns.size());
    checkIsNull(decodedRow, column1);
    checkIsNull(decodedRow, column2);
    checkIsNull(decodedRow, column3);
    checkIsNull(decodedRow, column4);
}
Also used : DecoderTestColumnHandle(io.prestosql.decoder.DecoderTestColumnHandle) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) RowDecoder(io.prestosql.decoder.RowDecoder) Test(org.testng.annotations.Test)

Aggregations

DecoderColumnHandle (io.prestosql.decoder.DecoderColumnHandle)92 FieldValueProvider (io.prestosql.decoder.FieldValueProvider)91 Test (org.testng.annotations.Test)80 ByteBuf (io.netty.buffer.ByteBuf)49 PulsarColumnHandle (org.apache.pulsar.sql.presto.PulsarColumnHandle)49 DecoderTestMessage (org.apache.pulsar.sql.presto.decoder.DecoderTestMessage)34 DecoderTestColumnHandle (io.prestosql.decoder.DecoderTestColumnHandle)33 ArrayType (io.prestosql.spi.type.ArrayType)20 RowType (io.prestosql.spi.type.RowType)15 GenericJsonRecord (org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord)15 RowDecoder (io.prestosql.decoder.RowDecoder)14 HashMap (java.util.HashMap)14 Type (io.prestosql.spi.type.Type)13 GenericAvroRecord (org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord)12 Map (java.util.Map)9 GenericProtobufNativeRecord (org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeRecord)9 ImmutableList (com.google.common.collect.ImmutableList)6 BigintType (io.prestosql.spi.type.BigintType)6 VarcharType (io.prestosql.spi.type.VarcharType)6 Timestamp (java.sql.Timestamp)6