Skip to content
Open
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: 5 additions & 1 deletion presto-docs/src/main/sphinx/functions/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Array Functions
Returns the average of all non-null elements of the ``array``. If there is no non-null elements, returns
``null``.

.. function:: array_contains_all(x, y) -> boolean

Returns true if the array ``x`` contains all the elements of array ``y`` including ``nulls``.

.. function:: array_cum_sum(array(T)) -> array(T)

Returns the array whose elements are the cumulative sum of the input array, i.e. result[i] = input[1]+input[2]+...+input[i].
Expand Down Expand Up @@ -78,7 +82,7 @@ Array Functions
.. function:: array_has_duplicates(array(T)) -> boolean

Returns a boolean: whether ``array`` has any elements that occur more than once.
Throws an exception if any of the elements are rows or arrays that contain nulls.
Throws an exception if any of the elements are rows or arrays that contain nulls.
Comment thread
jainavi17 marked this conversation as resolved.

SELECT array_has_duplicates(ARRAY[1, 2, null, 1, null, 3]) -- true
Comment thread
steveburnett marked this conversation as resolved.
SELECT array_has_duplicates(ARRAY[ROW(1, null), ROW(1, null)]) -- "map key cannot be null or contain nulls"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
import com.facebook.presto.operator.scalar.ArrayCardinalityFunction;
import com.facebook.presto.operator.scalar.ArrayCombinationsFunction;
import com.facebook.presto.operator.scalar.ArrayContains;
import com.facebook.presto.operator.scalar.ArrayContainsAllFunction;
import com.facebook.presto.operator.scalar.ArrayCumSum;
import com.facebook.presto.operator.scalar.ArrayDistinctFromOperator;
import com.facebook.presto.operator.scalar.ArrayDistinctFunction;
Expand Down Expand Up @@ -854,6 +855,7 @@ private List<? extends SqlFunction> getBuiltInFunctions(FeaturesConfig featuresC
.scalars(DataSizeFunctions.class)
.scalar(ArrayCardinalityFunction.class)
.scalar(ArrayContains.class)
.scalar(ArrayContainsAllFunction.class)
.scalar(ArrayFilterFunction.class)
.scalar(ArrayPositionFunction.class)
.scalar(ArrayPositionWithIndexFunction.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.operator.scalar;

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.operator.aggregation.TypedSet;
import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.SqlNullable;
import com.facebook.presto.spi.function.SqlType;
import com.facebook.presto.spi.function.TypeParameter;

@ScalarFunction("array_contains_all")
@Description("Returns true if all elements of the second array are present in the first array")
public final class ArrayContainsAllFunction
{
private ArrayContainsAllFunction() {}

@TypeParameter("T")
@SqlType(StandardTypes.BOOLEAN)
@SqlNullable
public static Boolean containsAll(
@TypeParameter("T") Type elementType,
@SqlType("array(T)") Block firstArray,
@SqlType("array(T)") Block secondArray)
{
Comment thread
jainavi17 marked this conversation as resolved.
TypedSet firstSet = new TypedSet(elementType, firstArray.getPositionCount(), "arrayContainsAll");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you benchmark this version vs. using OptimizedTypeSet and checking the cardinality of the intersection of the two sets? I expect that using OptimizedTypedSet would be faster except for the case where a large second array is able to short circuit very early.

Copy link
Copy Markdown
Contributor

@kaikalur kaikalur Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually curious - how about CONTAINS? @rschlussel did you fix that as well to be IS DISTINCT FROM? This function should behave like contains for that part

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contains fails for all null values. If we want this to fail for all nulls, we can do that as well, but we shouldn't have failures only for nulls nested in complex types, but otherwise compare nulls as equal (which is what typedset does by default)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jainavi17 did you have a chance to benchmark vs. a version using OptimizedTypeSet?

Copy link
Copy Markdown
Contributor

@rschlussel rschlussel Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use distinct semantics here so that we don't throw an exception for arrays of arrays with nulls? You pass in the "distinct" function handle to the typedSet. You can follow how it's done for array_distinct here: You can use "distinct comparison" for the typedset following what we do for array_distinct see

TypedSet typedSet = new TypedSet(type, elementIsDistinctFrom, array.getPositionCount(), "array_distinct");

for (int i = 0; i < firstArray.getPositionCount(); i++) {
firstSet.add(firstArray, i);
}

for (int i = 0; i < secondArray.getPositionCount(); i++) {
if (!firstSet.contains(secondArray, i)) {
return false;
}
}
return true;
}
}
Comment thread
jainavi17 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.type;

import com.facebook.presto.Session;
import com.facebook.presto.common.type.BooleanType;
import com.facebook.presto.operator.scalar.AbstractTestFunctions;
import com.facebook.presto.operator.scalar.FunctionAssertions;
import com.facebook.presto.sql.analyzer.FeaturesConfig;
import com.facebook.presto.sql.analyzer.SemanticErrorCode;
import com.facebook.presto.sql.analyzer.SemanticException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static com.facebook.presto.SystemSessionProperties.FIELD_NAMES_IN_JSON_CAST_ENABLED;
import static com.facebook.presto.common.type.UnknownType.UNKNOWN;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

public class TestArrayContainsAll
Comment thread
jainavi17 marked this conversation as resolved.
extends AbstractTestFunctions
{
private static FunctionAssertions fieldNameInJsonCastEnabled;

public TestArrayContainsAll() {}

@BeforeClass
public void setUp()
{
registerScalar(getClass());
fieldNameInJsonCastEnabled = new FunctionAssertions(
Session.builder(session)
.setSystemProperty(FIELD_NAMES_IN_JSON_CAST_ENABLED, "true")
.build(),
new FeaturesConfig());
}

@AfterClass(alwaysRun = true)
public final void tearDown()
{
fieldNameInJsonCastEnabled.close();
fieldNameInJsonCastEnabled = null;
}

@Test
public void testOverlappingArrays()
{
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, 2, 3], ARRAY [2])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, 2, 3], ARRAY [2, 3])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, 2, 3], ARRAY [1, 2, 3])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, 2, 3], ARRAY [1, 1, 2, 3])", BooleanType.BOOLEAN, true);
}

@Test
public void testDisjointArrays()
{
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, 2, 3], ARRAY [4])", BooleanType.BOOLEAN, false);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, 2, 3], ARRAY [4, 5])", BooleanType.BOOLEAN, false);
}

@Test
public void testEmptyArrays()
{
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [], ARRAY [])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, 2, 3], ARRAY [])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [], ARRAY [1, 2, 3])", BooleanType.BOOLEAN, false);
}

@Test
public void testDifferentDataTypes()
{
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1.0, 2.0, 3.0], ARRAY [2.0])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1.0, 2.0, 3.0], ARRAY [4.0])", BooleanType.BOOLEAN, false);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY ['a', 'b', 'c'], ARRAY ['b'])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY ['a', 'b', 'c'], ARRAY ['d'])", BooleanType.BOOLEAN, false);
}

@Test
public void testNulls()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test for array of arrays/rows containing nulls

{
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [NULL, 2, 3], ARRAY [2])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, NULL, 3], ARRAY [NULL])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1, 2, 3], ARRAY [NULL])", BooleanType.BOOLEAN, false);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [NULL, NULL], ARRAY [NULL])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(NULL, ARRAY [1])", BooleanType.BOOLEAN, null);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY [1], NULL)", BooleanType.BOOLEAN, null);
assertFunction("ARRAY_CONTAINS_ALL(NULL, NULL)", BooleanType.BOOLEAN, null);
}

@Test
public void testArrayOfArrays()
{
assertFunction("ARRAY_CONTAINS_ALL(ARRAY[ARRAY[1, 2], ARRAY[3, 4]], ARRAY[ARRAY[1, 2]])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY[ARRAY[1, 2], ARRAY[3, 4]], ARRAY[ARRAY[3, 4], ARRAY[5, 6]])", BooleanType.BOOLEAN, false);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY[ARRAY[1, 2], ARRAY[3, 4]], ARRAY[ARRAY[5, 6]])", BooleanType.BOOLEAN, false);
}

@Test
public void testArrayOfRows()
{
assertFunction("ARRAY_CONTAINS_ALL(ARRAY[ROW(1, 'a'), ROW(2, 'b')], ARRAY[ROW(1, 'a')])", BooleanType.BOOLEAN, true);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY[ROW(1, 'a'), ROW(2, 'b')], ARRAY[ROW(2, 'b'), ROW(3, 'c')])", BooleanType.BOOLEAN, false);
assertFunction("ARRAY_CONTAINS_ALL(ARRAY[ROW(1, 'a'), ROW(2, 'b')], ARRAY[ROW(3, 'c')])", BooleanType.BOOLEAN, false);
}

@Override
public void assertInvalidFunction(String projection, SemanticErrorCode errorCode)
{
try {
assertFunction(projection, UNKNOWN, null);
fail("Expected error " + errorCode + " from " + projection);
}
catch (SemanticException e) {
assertEquals(e.getCode(), errorCode);
}
}
}