From 9ae8c965e8a029defce638fc181c4690deaaa8d2 Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Fri, 14 Jan 2022 02:10:25 +0900 Subject: [PATCH] Fix bad parameter count in code generator Avoid BLOCK_POSITION when there a lot of function arguments --- .../io/prestosql/sql/gen/BytecodeUtils.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/presto-main/src/main/java/io/prestosql/sql/gen/BytecodeUtils.java b/presto-main/src/main/java/io/prestosql/sql/gen/BytecodeUtils.java index c4bc484599d7..d32fa29bde08 100644 --- a/presto-main/src/main/java/io/prestosql/sql/gen/BytecodeUtils.java +++ b/presto-main/src/main/java/io/prestosql/sql/gen/BytecodeUtils.java @@ -248,16 +248,7 @@ public static BytecodeNode generateFullInvocation( } else { BytecodeNode argument = argumentCompilers.get(i).apply(Optional.empty()); - if (argument instanceof InputReferenceNode) { - argumentConventions.add(BLOCK_POSITION); - } - else if (functionMetadata.getArgumentDefinitions().get(i).isNullable()) { - // a Java function can only have 255 arguments, so if the count is high use boxed nullable instead of the more efficient null flag - argumentConventions.add(argumentCompilers.size() > 100 ? BOXED_NULLABLE : NULL_FLAG); - } - else { - argumentConventions.add(NEVER_NULL); - } + argumentConventions.add(getPreferredArgumentConvention(argument, argumentCompilers.size(), functionMetadata.getArgumentDefinitions().get(i).isNullable())); arguments.add(argument); } } @@ -355,6 +346,21 @@ else if (type == ConnectorSession.class) { return block; } + private static InvocationArgumentConvention getPreferredArgumentConvention(BytecodeNode argument, int argumentCount, boolean nullable) + { + // a Java function can only have 255 arguments, so if the count is low use block position or boxed nullable as they are more efficient + if (argumentCount <= 100) { + if (argument instanceof InputReferenceNode) { + return BLOCK_POSITION; + } + if (nullable) { + return NULL_FLAG; + } + } + + return nullable ? BOXED_NULLABLE : NEVER_NULL; + } + public static BytecodeBlock unboxPrimitiveIfNecessary(Scope scope, Class boxedType) { BytecodeBlock block = new BytecodeBlock();