From 94e71a2162461eb5be5a9bea82bae2cb21c4ca05 Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Mon, 4 Dec 2023 13:18:16 +0100 Subject: [PATCH] Fix writing Constant integer value to a block SqlToRowExpressionTranslator.visitSubscriptExpression creates a constant with integer index that gets assigned to a Object field which makes it boxed Integer class instance. Then when serialized, Integer is casted to a primitive long, which fails at runtime. --- .../io/trino/sql/gen/DereferenceCodeGenerator.java | 11 ++++++++++- .../src/main/java/io/trino/spi/type/TypeUtils.java | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/core/trino-main/src/main/java/io/trino/sql/gen/DereferenceCodeGenerator.java b/core/trino-main/src/main/java/io/trino/sql/gen/DereferenceCodeGenerator.java index 6f5736356791..9c0f5da61a9e 100644 --- a/core/trino-main/src/main/java/io/trino/sql/gen/DereferenceCodeGenerator.java +++ b/core/trino-main/src/main/java/io/trino/sql/gen/DereferenceCodeGenerator.java @@ -44,7 +44,7 @@ public DereferenceCodeGenerator(SpecialForm specialForm) returnType = specialForm.getType(); checkArgument(specialForm.getArguments().size() == 2); base = specialForm.getArguments().get(0); - index = (int) ((ConstantExpression) specialForm.getArguments().get(1)).getValue(); + index = toIntValue(((ConstantExpression) specialForm.getArguments().get(1)).getValue()); } @Override @@ -93,4 +93,13 @@ public BytecodeNode generateExpression(BytecodeGeneratorContext generator) return block; } + + private static int toIntValue(Object value) + { + if (value instanceof Number number) { + return number.intValue(); + } + + return (int) value; + } } diff --git a/core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java b/core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java index 7e5cd97a39ed..acca8963a919 100644 --- a/core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java +++ b/core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java @@ -83,7 +83,7 @@ else if (type.getJavaType() == double.class) { type.writeDouble(blockBuilder, ((double) value)); } else if (type.getJavaType() == long.class) { - type.writeLong(blockBuilder, ((long) value)); + type.writeLong(blockBuilder, castToLong(value)); } else if (type.getJavaType() == Slice.class) { Slice slice; @@ -117,6 +117,15 @@ public static boolean isFloatingPointNaN(Type type, Object value) return false; } + private static long castToLong(Object value) + { + if (value instanceof Number number) { + return number.longValue(); + } + + return (long) value; + } + static void checkElementNotNull(boolean isNull, String errorMsg) { if (isNull) {