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: 6 additions & 0 deletions presto-docs/src/main/sphinx/functions/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ Array Functions

Returns the position of the first occurrence of the ``element`` in array ``x`` (or 0 if not found).

.. function:: array_position(x, element, instance) -> bigint

If ``instance > 0``, returns the position of the `instance`-th occurrence of the ``element`` in array ``x``. If
``instance < 0``, returns the position of the ``instance``-to-last occurrence of the ``element`` in array ``x``.
If no matching element instance is found, ``0`` is returned.

.. function:: array_remove(x, element) -> array

Remove all elements that equal ``element`` from array ``x``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import com.facebook.presto.operator.scalar.ArrayNormalizeFunction;
import com.facebook.presto.operator.scalar.ArrayNotEqualOperator;
import com.facebook.presto.operator.scalar.ArrayPositionFunction;
import com.facebook.presto.operator.scalar.ArrayPositionWithIndexFunction;
import com.facebook.presto.operator.scalar.ArrayRemoveFunction;
import com.facebook.presto.operator.scalar.ArrayReverseFunction;
import com.facebook.presto.operator.scalar.ArrayShuffleFunction;
Expand Down Expand Up @@ -711,6 +712,7 @@ private List<? extends SqlFunction> getBuildInFunctions(FeaturesConfig featuresC
.scalar(ArrayContains.class)
.scalar(ArrayFilterFunction.class)
.scalar(ArrayPositionFunction.class)
.scalar(ArrayPositionWithIndexFunction.class)
.scalars(CombineHashFunction.class)
.scalars(JsonOperators.class)
.scalar(JsonOperators.JsonDistinctFromOperator.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.OperatorDependency;
import com.facebook.presto.spi.function.ScalarFunction;
Expand All @@ -27,13 +26,11 @@
import java.lang.invoke.MethodHandle;

import static com.facebook.presto.common.function.OperatorType.EQUAL;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.facebook.presto.util.Failures.internalError;
import static com.google.common.base.Verify.verify;

@Description("Returns the position of the first occurrence of the given value in array (or 0 if not found)")
@ScalarFunction("array_position")
public final class ArrayPositionFunction
extends ArrayPositionWithIndexFunction
{
private ArrayPositionFunction() {}

Expand All @@ -45,23 +42,7 @@ public static long arrayPosition(
@SqlType("array(T)") Block array,
@SqlType("T") boolean element)
{
int size = array.getPositionCount();
for (int i = 0; i < size; i++) {
if (!array.isNull(i)) {
boolean arrayValue = type.getBoolean(array, i);
try {
Boolean result = (Boolean) equalMethodHandle.invokeExact(arrayValue, element);
verify(result != null, "Array element should not be null");
if (result) {
return i + 1; // result is 1-based (instead of 0)
}
}
catch (Throwable t) {
throw internalError(t);
}
}
}
return 0;
return arrayPositionWithIndex(type, equalMethodHandle, array, element, 1);
}

@TypeParameter("T")
Expand All @@ -72,23 +53,7 @@ public static long arrayPosition(
@SqlType("array(T)") Block array,
@SqlType("T") long element)
{
int size = array.getPositionCount();
for (int i = 0; i < size; i++) {
if (!array.isNull(i)) {
long arrayValue = type.getLong(array, i);
try {
Boolean result = (Boolean) equalMethodHandle.invokeExact(arrayValue, element);
verify(result != null, "Array element should not be null");
if (result) {
return i + 1; // result is 1-based (instead of 0)
}
}
catch (Throwable t) {
throw internalError(t);
}
}
}
return 0;
return arrayPositionWithIndex(type, equalMethodHandle, array, element, 1);
}

@TypeParameter("T")
Expand All @@ -99,23 +64,7 @@ public static long arrayPosition(
@SqlType("array(T)") Block array,
@SqlType("T") double element)
{
int size = array.getPositionCount();
for (int i = 0; i < size; i++) {
if (!array.isNull(i)) {
double arrayValue = type.getDouble(array, i);
try {
Boolean result = (Boolean) equalMethodHandle.invokeExact(arrayValue, element);
verify(result != null, "Array element should not be null");
if (result) {
return i + 1; // result is 1-based (instead of 0)
}
}
catch (Throwable t) {
throw internalError(t);
}
}
}
return 0;
return arrayPositionWithIndex(type, equalMethodHandle, array, element, 1);
}

@TypeParameter("T")
Expand All @@ -126,23 +75,7 @@ public static long arrayPosition(
@SqlType("array(T)") Block array,
@SqlType("T") Slice element)
{
int size = array.getPositionCount();
for (int i = 0; i < size; i++) {
if (!array.isNull(i)) {
Slice arrayValue = type.getSlice(array, i);
try {
Boolean result = (Boolean) equalMethodHandle.invokeExact(arrayValue, element);
verify(result != null, "Array element should not be nul");
if (result) {
return i + 1; // result is 1-based (instead of 0)
}
}
catch (Throwable t) {
throw internalError(t);
}
}
}
return 0;
return arrayPositionWithIndex(type, equalMethodHandle, array, element, 1);
}

@TypeParameter("T")
Expand All @@ -153,24 +86,6 @@ public static long arrayPosition(
@SqlType("array(T)") Block array,
@SqlType("T") Block element)
{
int size = array.getPositionCount();
for (int i = 0; i < size; i++) {
if (!array.isNull(i)) {
Object arrayValue = type.getObject(array, i);
try {
Boolean result = (Boolean) equalMethodHandle.invoke(arrayValue, element);
if (result == null) {
throw new PrestoException(NOT_SUPPORTED, "array_position does not support elements of complex types that contain null");
}
if (result) {
return i + 1; // result is 1-based (instead of 0)
}
}
catch (Throwable t) {
throw internalError(t);
}
}
}
return 0;
return arrayPositionWithIndex(type, equalMethodHandle, array, element, 1);
}
}
Loading