diff --git a/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayMaxFunction.java b/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayMaxFunction.java index 94debbaa2ac06..85fe8e3f02685 100644 --- a/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayMaxFunction.java +++ b/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayMaxFunction.java @@ -95,8 +95,12 @@ public static Block blockArrayMax( return null; } + if (block.isNull(0)) { + return null; + } + Block selectedValue = (Block) elementType.getObject(block, 0); - for (int i = 0; i < block.getPositionCount(); i++) { + for (int i = 1; i < block.getPositionCount(); i++) { if (block.isNull(i)) { return null; } diff --git a/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayMinFunction.java b/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayMinFunction.java index b605a0bfb731d..30a7ddec1ba00 100644 --- a/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayMinFunction.java +++ b/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayMinFunction.java @@ -95,8 +95,12 @@ public static Block blockArrayMin( return null; } + if (block.isNull(0)) { + return null; + } + Block selectedValue = (Block) elementType.getObject(block, 0); - for (int i = 0; i < block.getPositionCount(); i++) { + for (int i = 1; i < block.getPositionCount(); i++) { if (block.isNull(i)) { return null; } diff --git a/presto-main/src/test/java/com/facebook/presto/type/TestArrayOperators.java b/presto-main/src/test/java/com/facebook/presto/type/TestArrayOperators.java index 3f7b795895f35..9566a68f7ab83 100644 --- a/presto-main/src/test/java/com/facebook/presto/type/TestArrayOperators.java +++ b/presto-main/src/test/java/com/facebook/presto/type/TestArrayOperators.java @@ -696,6 +696,30 @@ public void testArrayMin() assertDecimalFunction("ARRAY_MIN(ARRAY [2.22222222222222222, 2.3])", decimal("2.22222222222222222")); } + @Test + public void testArrayMinWithNullsInBothArraysNotComparedFirstIsMin() + { + assertFunction("ARRAY_MIN(ARRAY [ARRAY[1, NULL], ARRAY[2, NULL]])", new ArrayType(INTEGER), Lists.newArrayList(1, null)); + } + + @Test + public void testArrayMinWithNullsInBothArraysNotComparedSecondIsMin() + { + assertFunction("ARRAY_MIN(ARRAY [ARRAY[2, NULL], ARRAY[1, NULL]])", new ArrayType(INTEGER), Lists.newArrayList(1, null)); + } + + @Test + public void testArrayMinWithNullInFirstArrayIsCompared() + { + assertInvalidFunction("ARRAY_MIN(ARRAY [ARRAY[1, NULL], ARRAY[1, 2]])", NOT_SUPPORTED); + } + + @Test + public void testArrayMinWithNullInSecondArrayIsCompared() + { + assertInvalidFunction("ARRAY_MIN(ARRAY [ARRAY[1, 2], ARRAY[1, NULL]])", NOT_SUPPORTED); + } + @Test public void testArrayMax() { @@ -726,6 +750,30 @@ public void testArrayMax() assertDecimalFunction("ARRAY_MAX(ARRAY [2.22222222222222222, 2.3])", decimal("2.30000000000000000")); } + @Test + public void testArrayMaxWithNullsInBothArraysNotComparedSecondIsMax() + { + assertFunction("ARRAY_MAX(ARRAY [ARRAY[1, NULL], ARRAY[2, NULL]])", new ArrayType(INTEGER), Lists.newArrayList(2, null)); + } + + @Test + public void testArrayMaxWithNullsInBothArraysNotComparedFirstIsMax() + { + assertFunction("ARRAY_MAX(ARRAY [ARRAY[2, NULL], ARRAY[1, NULL]])", new ArrayType(INTEGER), Lists.newArrayList(2, null)); + } + + @Test + public void testArrayMaxWithNullInFirstArrayIsCompared() + { + assertInvalidFunction("ARRAY_MAX(ARRAY [ARRAY[1, NULL], ARRAY[1, 2]])", NOT_SUPPORTED); + } + + @Test + public void testArrayMaxWithNullInSecondArrayIsCompared() + { + assertInvalidFunction("ARRAY_MAX(ARRAY [ARRAY[1, 2], ARRAY[1, NULL]])", NOT_SUPPORTED); + } + @Test public void testArrayPosition() {