Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions presto-docs/src/main/sphinx/functions/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,13 @@ Array Functions

.. function:: find_first(array(E), function(T,boolean)) -> E

Returns the first element of ``array`` which returns true for ``function(T,boolean)``. Returns ``NULL`` if no such element exists.
Returns the first element of ``array`` which returns true for ``function(T,boolean)``, throws exception if the returned element is NULL.
Returns ``NULL`` if no such element exists.

.. function:: find_first(array(E), index, function(T,boolean)) -> E

Returns the first element of ``array`` which returns true for ``function(T,boolean)``. Returns ``NULL`` if no such element exists.
Returns the first element of ``array`` which returns true for ``function(T,boolean)``, throws exception if the returned element is NULL.
Returns ``NULL`` if no such element exists.
If ``index`` > 0, the search for element starts at position ``index`` until the end of array.
If ``index`` < 0, the search for element starts at position ``abs(index)`` counting from last, until the start of array. ::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
import com.facebook.presto.operator.scalar.ArrayEqualOperator;
import com.facebook.presto.operator.scalar.ArrayExceptFunction;
import com.facebook.presto.operator.scalar.ArrayFilterFunction;
import com.facebook.presto.operator.scalar.ArrayFindFirstFirstFunction;
import com.facebook.presto.operator.scalar.ArrayFindFirstFunction;
import com.facebook.presto.operator.scalar.ArrayFindFirstIndexFunction;
import com.facebook.presto.operator.scalar.ArrayFindFirstIndexWithOffsetFunction;
import com.facebook.presto.operator.scalar.ArrayFindFirstWithOffsetFunction;
Expand Down Expand Up @@ -810,7 +810,7 @@ private List<? extends SqlFunction> getBuildInFunctions(FeaturesConfig featuresC
.scalar(ArrayNgramsFunction.class)
.scalar(ArrayAllMatchFunction.class)
.scalar(ArrayAnyMatchFunction.class)
.scalar(ArrayFindFirstFirstFunction.class)
.scalar(ArrayFindFirstFunction.class)
.scalar(ArrayFindFirstWithOffsetFunction.class)
.scalar(ArrayFindFirstIndexFunction.class)
.scalar(ArrayFindFirstIndexWithOffsetFunction.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

@Description("Return the first element which matches the given predicate, null if no match")
@ScalarFunction(value = "find_first", deterministic = true)
public final class ArrayFindFirstFirstFunction
public final class ArrayFindFirstFunction
extends ArrayFindFirstWithOffsetFunction
{
private ArrayFindFirstFirstFunction() {}
private ArrayFindFirstFunction() {}

@TypeParameter("T")
@SqlType("T")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.airlift.slice.Slice;

import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
import static com.facebook.presto.util.Failures.checkCondition;
import static java.lang.Boolean.TRUE;
import static java.lang.Math.toIntExact;

Expand Down Expand Up @@ -112,6 +113,9 @@ public static Block findBlockUtil(
}
Boolean match = function.apply(element);
if (TRUE.equals(match)) {
if (element == null) {
checkCondition(false, INVALID_FUNCTION_ARGUMENT, "FIND_FIRST finds NULL as match, which is not supported.");
}
return element;
}
}
Expand All @@ -136,6 +140,9 @@ public static Slice findSliceUtil(
}
Boolean match = function.apply(element);
if (TRUE.equals(match)) {
if (element == null) {
checkCondition(false, INVALID_FUNCTION_ARGUMENT, "FIND_FIRST finds NULL as match, which is not supported.");
}
return element;
}
}
Expand All @@ -160,6 +167,9 @@ public static Long findLongUtil(
}
Boolean match = function.apply(element);
if (TRUE.equals(match)) {
if (element == null) {
checkCondition(false, INVALID_FUNCTION_ARGUMENT, "FIND_FIRST finds NULL as match, which is not supported.");
}
return element;
}
}
Expand All @@ -184,6 +194,9 @@ public static Double findDoubleUtil(
}
Boolean match = function.apply(element);
if (TRUE.equals(match)) {
if (element == null) {
checkCondition(false, INVALID_FUNCTION_ARGUMENT, "FIND_FIRST finds NULL as match, which is not supported.");
}
return element;
}
}
Expand All @@ -208,6 +221,9 @@ public static Boolean findBooleanUtil(
}
Boolean match = function.apply(element);
if (TRUE.equals(match)) {
if (element == null) {
checkCondition(false, INVALID_FUNCTION_ARGUMENT, "FIND_FIRST finds NULL as match, which is not supported.");
}
return element;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public void testEmpty()
@Test
public void testNullArray()
{
assertFunction("find_first(ARRAY [NULL], x -> x IS NULL)", UNKNOWN, null);
assertInvalidFunction("find_first(ARRAY [NULL], x -> x IS NULL)", "FIND_FIRST finds NULL as match, which is not supported.");
assertFunction("find_first(ARRAY [NULL], x -> x IS NOT NULL)", UNKNOWN, null);
assertFunction("find_first(ARRAY [CAST (NULL AS INTEGER)], x -> x IS NULL)", INTEGER, null);
assertInvalidFunction("find_first(ARRAY [CAST (NULL AS INTEGER)], x -> x IS NULL)", "FIND_FIRST finds NULL as match, which is not supported.");
}

@Test
Expand Down