-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add array_contains_all function #23202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||||
| { | ||||
|
jainavi17 marked this conversation as resolved.
|
||||
| TypedSet firstSet = new TypedSet(elementType, firstArray.getPositionCount(), "arrayContainsAll"); | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 presto/presto-main/src/main/java/com/facebook/presto/operator/scalar/ArrayDistinctFunction.java Line 72 in 6f28f37
|
||||
| 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; | ||||
| } | ||||
| } | ||||
|
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 | ||
|
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() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.