Skip to content

Commit

Permalink
Fix writing Constant integer value to a block
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wendigo committed Dec 4, 2023
1 parent 19f9677 commit 94e71a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
11 changes: 10 additions & 1 deletion core/trino-spi/src/main/java/io/trino/spi/type/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 94e71a2

Please sign in to comment.