Skip to content

Commit c986c08

Browse files
committed
Fix array_top_n not outputting null
1 parent d260ff5 commit c986c08

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

presto-main/src/main/java/com/facebook/presto/operator/scalar/sql/ArraySqlFunctions.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static String removeNulls()
163163
@SqlParameters({@SqlParameter(name = "input", type = "array(T)"), @SqlParameter(name = "n", type = "int")})
164164
@SqlType("array<T>")
165165
public static String arrayTopN()
166-
{ return "RETURN IF(n < 0, fail('Parameter n: ' || cast(n as varchar) || ' to ARRAY_TOP_N is negative'), SLICE(ARRAY_SORT_DESC(input), 1, n))"; }
166+
{ return "RETURN IF(n < 0, NULL, SLICE(ARRAY_SORT_DESC(input), 1, n))"; }
167167

168168
@SqlInvokedScalarFunction(value = "array_top_n", deterministic = true, calledOnNullInput = true)
169169
@Description("Returns the top N values of the given map sorted using the provided lambda comparator.")
@@ -172,6 +172,6 @@ public static String arrayTopN()
172172
@SqlType("array<T>")
173173
public static String arrayTopNComparator()
174174
{
175-
return "RETURN IF(n < 0, fail('Parameter n: ' || cast(n as varchar) || ' to ARRAY_TOP_N is negative'), SLICE(REVERSE(ARRAY_SORT(input, f)), 1, n))";
175+
return "RETURN IF(n < 0, NULL, SLICE(REVERSE(ARRAY_SORT(input, f)), 1, n))";
176176
}
177177
}

presto-main/src/test/java/com/facebook/presto/operator/scalar/sql/TestArraySqlFunctions.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,21 @@ public void testArrayTopNEdgeAndErrorCase()
393393
// Test exceptions
394394
assertInvalidFunction("ARRAY_TOP_N(ARRAY [ROW('a', 1), ROW('a', null), null, ROW('a', 0)], 2)", StandardErrorCode.INVALID_FUNCTION_ARGUMENT);
395395
assertInvalidFunction("ARRAY_TOP_N(ARRAY [MAP(ARRAY['foo', 'bar'], ARRAY[1, 2]), MAP(ARRAY['foo', 'bar'], ARRAY[0, 3])], 2)", SemanticErrorCode.FUNCTION_NOT_FOUND);
396-
assertInvalidFunction("ARRAY_TOP_N(ARRAY ['a', 'a', 'd', 'a', 'a', 'a'], -1)", StandardErrorCode.GENERIC_USER_ERROR, "Parameter n: -1 to ARRAY_TOP_N is negative");
397396

398397
// Test edge cases
399398
assertFunction("ARRAY_TOP_N(ARRAY [null, null], 3)", new ArrayType(UNKNOWN), asList(null, null));
400399
assertFunction("ARRAY_TOP_N(ARRAY [3, 5, 1, 2], 0)", new ArrayType(INTEGER), emptyList());
401400
assertFunction("ARRAY_TOP_N(ARRAY [], 3)", new ArrayType(UNKNOWN), emptyList());
402401
assertFunction("ARRAY_TOP_N(ARRAY [1, 4], 3)", new ArrayType(INTEGER), ImmutableList.of(4, 1));
403402
}
403+
404+
@Test
405+
public void testArrayTopNNegativeParameter()
406+
{
407+
assertFunction("ARRAY_TOP_N(ARRAY ['a', 'a', 'd', 'a', 'a', 'a'], -1)", new ArrayType(createVarcharType(1)), null);
408+
assertFunction("ARRAY_TOP_N(ARRAY [1,2,3,4,5,6], -5)", new ArrayType(INTEGER), null);
409+
assertFunction("ARRAY_TOP_N(ARRAY [DOUBLE '1.0', 100, 2, DOUBLE '5.0', DOUBLE '3.0'], -3)", new ArrayType(DOUBLE), null);
410+
assertFunction("ARRAY_TOP_N(ARRAY [true, true, false, true, false], -4)", new ArrayType(BOOLEAN), null);
411+
assertFunction("ARRAY_TOP_N(ARRAY [null, null], -3)", new ArrayType(UNKNOWN), null);
412+
}
404413
}

0 commit comments

Comments
 (0)