From eb131d61df50d18649bc3ec3cf2d4f7cfefadb01 Mon Sep 17 00:00:00 2001 From: Yuya Ebihara Date: Wed, 12 Jan 2022 19:47:19 +0900 Subject: [PATCH 1/2] Fix query failure when accessing numeric types in BigQuery --- .../io/trino/plugin/bigquery/BigQueryResultPageSource.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryResultPageSource.java b/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryResultPageSource.java index 681a891d36e0..3bdb499e5367 100644 --- a/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryResultPageSource.java +++ b/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryResultPageSource.java @@ -27,6 +27,7 @@ import io.trino.spi.type.ArrayType; import io.trino.spi.type.DecimalType; import io.trino.spi.type.Decimals; +import io.trino.spi.type.Int128; import io.trino.spi.type.LongTimestampWithTimeZone; import io.trino.spi.type.RowType; import io.trino.spi.type.Type; @@ -190,7 +191,7 @@ else if (type.equals(TIME_MICROS)) { else if (javaType == double.class) { type.writeDouble(output, ((Number) value).doubleValue()); } - else if (javaType == Slice.class) { + else if (javaType == Slice.class || type.getJavaType() == Int128.class) { writeSlice(output, type, value); } else if (javaType == LongTimestampWithTimeZone.class) { From ebd83b938dd68ae5508891976bcae1005c000279 Mon Sep 17 00:00:00 2001 From: Yuya Ebihara Date: Wed, 12 Jan 2022 19:53:50 +0900 Subject: [PATCH 2/2] Extract writeObject method in BigQueryResultPageSource --- .../bigquery/BigQueryResultPageSource.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryResultPageSource.java b/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryResultPageSource.java index 3bdb499e5367..df894a10c228 100644 --- a/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryResultPageSource.java +++ b/plugin/trino-bigquery/src/main/java/io/trino/plugin/bigquery/BigQueryResultPageSource.java @@ -191,7 +191,10 @@ else if (type.equals(TIME_MICROS)) { else if (javaType == double.class) { type.writeDouble(output, ((Number) value).doubleValue()); } - else if (javaType == Slice.class || type.getJavaType() == Int128.class) { + else if (type.getJavaType() == Int128.class) { + writeObject(output, type, value); + } + else if (javaType == Slice.class) { writeSlice(output, type, value); } else if (javaType == LongTimestampWithTimeZone.class) { @@ -218,12 +221,6 @@ private static void writeSlice(BlockBuilder output, Type type, Object value) if (type instanceof VarcharType) { type.writeSlice(output, utf8Slice(((Utf8) value).toString())); } - else if (type instanceof DecimalType) { - verify(isLongDecimal(type), "The type should be long decimal"); - DecimalType decimalType = (DecimalType) type; - BigDecimal decimal = DECIMAL_CONVERTER.convert(decimalType.getPrecision(), decimalType.getScale(), value); - type.writeObject(output, Decimals.encodeScaledValue(decimal, decimalType.getScale())); - } else if (type instanceof VarbinaryType) { if (value instanceof ByteBuffer) { type.writeSlice(output, Slices.wrappedBuffer((ByteBuffer) value)); @@ -237,6 +234,19 @@ else if (type instanceof VarbinaryType) { } } + private static void writeObject(BlockBuilder output, Type type, Object value) + { + if (type instanceof DecimalType) { + verify(isLongDecimal(type), "The type should be long decimal"); + DecimalType decimalType = (DecimalType) type; + BigDecimal decimal = DECIMAL_CONVERTER.convert(decimalType.getPrecision(), decimalType.getScale(), value); + type.writeObject(output, Decimals.encodeScaledValue(decimal, decimalType.getScale())); + } + else { + throw new TrinoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Object: " + type.getTypeSignature()); + } + } + private void writeBlock(BlockBuilder output, Type type, Object value) { if (type instanceof ArrayType && value instanceof List) {