-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-5707] Add ARRAY_CONTAINS function (enabled in Spark library) #3207
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
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,75 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you 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 org.apache.calcite.sql.type; | ||
|
|
||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.sql.SqlCallBinding; | ||
| import org.apache.calcite.sql.SqlNode; | ||
| import org.apache.calcite.sql.SqlOperandCountRange; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
|
|
||
| import static org.apache.calcite.sql.type.NonNullableAccessors.getComponentTypeOrThrow; | ||
| import static org.apache.calcite.util.Static.RESOURCE; | ||
|
|
||
| /** | ||
| * Parameter type-checking strategy where types must be Array and Array element type. | ||
| */ | ||
| public class ArrayElementOperandTypeChecker implements SqlOperandTypeChecker { | ||
| //~ Methods ---------------------------------------------------------------- | ||
|
|
||
| @Override public boolean checkOperandTypes( | ||
| SqlCallBinding callBinding, | ||
| boolean throwOnFailure) { | ||
| final SqlNode op0 = callBinding.operand(0); | ||
| if (!OperandTypes.ARRAY.checkSingleOperandType( | ||
| callBinding, | ||
| op0, | ||
| 0, | ||
| throwOnFailure)) { | ||
| return false; | ||
| } | ||
|
|
||
| RelDataType arrayComponentType = | ||
| getComponentTypeOrThrow(SqlTypeUtil.deriveType(callBinding, op0)); | ||
| final SqlNode op1 = callBinding.operand(1); | ||
| RelDataType aryType1 = SqlTypeUtil.deriveType(callBinding, op1); | ||
liuyongvs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| RelDataType biggest = | ||
| callBinding.getTypeFactory().leastRestrictive( | ||
| ImmutableList.of(arrayComponentType, aryType1)); | ||
| if (biggest == null) { | ||
| if (throwOnFailure) { | ||
| throw callBinding.newError( | ||
| RESOURCE.typeNotComparable( | ||
| arrayComponentType.toString(), aryType1.toString())); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override public SqlOperandCountRange getOperandCountRange() { | ||
| return SqlOperandCountRanges.of(2); | ||
| } | ||
|
|
||
| @Override public String getAllowedSignatures(SqlOperator op, String opName) { | ||
| return "<ARRAY> " + opName + " <ARRAY>"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5364,6 +5364,33 @@ private static void checkIf(SqlOperatorFixture f) { | |
| f.checkNull("array_concat(cast(null as integer array), array[1])"); | ||
| } | ||
|
|
||
| /** Tests {@code ARRAY_CONTAINS} function from Spark. */ | ||
| @Test void testArrayContainsFunc() { | ||
| final SqlOperatorFixture f0 = fixture(); | ||
| f0.setFor(SqlLibraryOperators.ARRAY_CONTAINS); | ||
| f0.checkFails("^array_contains(array[1, 2], 1)^", | ||
| "No match found for function signature " | ||
| + "ARRAY_CONTAINS\\(<INTEGER ARRAY>, <NUMERIC>\\)", false); | ||
|
|
||
| final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK); | ||
|
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. again curious what the behavior is/should be if you search an array of type X for a value of type Y, obviously it would return false but should it be allowed in the first place?
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. Good point, @tanclary. The validator should give an error if you - say - search for a BOOLEAN in a DATE ARRAY. We should add a test case to this test method.
Contributor
Author
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. hi @julianhyde @tanclary , there is the unit test in the end |
||
| f.checkScalar("array_contains(array[1, 2], 1)", true, | ||
| "BOOLEAN NOT NULL"); | ||
| f.checkScalar("array_contains(array[1], 1)", true, | ||
| "BOOLEAN NOT NULL"); | ||
| f.checkScalar("array_contains(array(), 1)", false, | ||
| "BOOLEAN NOT NULL"); | ||
| f.checkScalar("array_contains(array[array[1, 2], array[3, 4]], array[1, 2])", true, | ||
| "BOOLEAN NOT NULL"); | ||
| f.checkScalar("array_contains(array[map[1, 'a'], map[2, 'b']], map[1, 'a'])", true, | ||
| "BOOLEAN NOT NULL"); | ||
|
Comment on lines
+5384
to
+5385
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.
Contributor
Author
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. Due to implementation limitation, currently Spark can't compare or do equality check between map types. As a result, map values can't appear in EQUAL or comparison expressions, can't be grouping key, etc.() |
||
| f.checkNull("array_contains(cast(null as integer array), 1)"); | ||
| f.checkType("array_contains(cast(null as integer array), 1)", "BOOLEAN"); | ||
| f.checkNull("array_contains(array[1, null], cast(null as integer))"); | ||
| f.checkType("array_contains(array[1, null], cast(null as integer))", "BOOLEAN"); | ||
| f.checkFails("^array_contains(array[1, 2], true)^", | ||
| "INTEGER is not comparable to BOOLEAN", false); | ||
| } | ||
liuyongvs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** Tests {@code ARRAY_DISTINCT} function from Spark. */ | ||
| @Test void testArrayDistinctFunc() { | ||
| final SqlOperatorFixture f0 = fixture(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many codes are same as
MultisetOperandTypeChecker, Can we extract them?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes refer it.
do not find a good idea to abstract it.
MultisetOperandTypeChecker for two Multiset
ArrayElementOperandTypeChecker for Array and element type