gianm commented on code in PR #16306:
URL: https://github.com/apache/druid/pull/16306#discussion_r1570105949
##########
docs/querying/math-expr.md:
##########
@@ -176,24 +176,25 @@ See javadoc of java.lang.Math for detailed explanation
for each function.
## Array functions
-| function | description |
-| --- | --- |
-| array(expr1,expr ...) | constructs an array from the expression arguments,
using the type of the first argument as the output array type |
-| array_length(arr) | returns length of array expression |
-| array_offset(arr,long) | returns the array element at the 0 based index
supplied, or null for an out of range index|
-| array_ordinal(arr,long) | returns the array element at the 1 based index
supplied, or null for an out of range index |
-| array_contains(arr,expr) | returns 1 if the array contains the element
specified by expr, or contains all elements specified by expr if expr is an
array, else 0 |
-| array_overlap(arr1,arr2) | returns 1 if arr1 and arr2 have any elements in
common, else 0 |
-| array_offset_of(arr,expr) | returns the 0 based index of the first
occurrence of expr in the array, or `null` or `-1` if
`druid.generic.useDefaultValueForNull=true` (deprecated legacy mode) if no
matching elements exist in the array. |
-| array_ordinal_of(arr,expr) | returns the 1 based index of the first
occurrence of expr in the array, or `null` or `-1` if
`druid.generic.useDefaultValueForNull=true` (deprecated legacy mode) if no
matching elements exist in the array. |
-| array_prepend(expr,arr) | adds expr to arr at the beginning, the resulting
array type determined by the type of the array |
-| array_append(arr,expr) | appends expr to arr, the resulting array type
determined by the type of the first array |
-| array_concat(arr1,arr2) | concatenates 2 arrays, the resulting array type
determined by the type of the first array |
-| array_set_add(arr,expr) | adds expr to arr and converts the array to a new
array composed of the unique set of elements. The resulting array type
determined by the type of the array |
-| array_set_add_all(arr1,arr2) | combines the unique set of elements of 2
arrays, the resulting array type determined by the type of the first array |
-| array_slice(arr,start,end) | return the subarray of arr from the 0 based
index start(inclusive) to end(exclusive), or `null`, if start is less than 0,
greater than length of arr or less than end|
-| array_to_string(arr,str) | joins all elements of arr by the delimiter
specified by str |
-| string_to_array(str1,str2) | splits str1 into an array on the delimiter
specified by str2, which is a regular expression |
+| function | description
|
Review Comment:
Please don't format these markdown tables like this- it makes diffs hard to
read because of the whitespace-only changes. Please turn off the feature in
your IDE; if you're using IntelliJ, I think the code style file in the repo
should do it.
##########
docs/querying/math-expr.md:
##########
@@ -176,24 +176,25 @@ See javadoc of java.lang.Math for detailed explanation
for each function.
## Array functions
-| function | description |
-| --- | --- |
-| array(expr1,expr ...) | constructs an array from the expression arguments,
using the type of the first argument as the output array type |
-| array_length(arr) | returns length of array expression |
-| array_offset(arr,long) | returns the array element at the 0 based index
supplied, or null for an out of range index|
-| array_ordinal(arr,long) | returns the array element at the 1 based index
supplied, or null for an out of range index |
-| array_contains(arr,expr) | returns 1 if the array contains the element
specified by expr, or contains all elements specified by expr if expr is an
array, else 0 |
-| array_overlap(arr1,arr2) | returns 1 if arr1 and arr2 have any elements in
common, else 0 |
-| array_offset_of(arr,expr) | returns the 0 based index of the first
occurrence of expr in the array, or `null` or `-1` if
`druid.generic.useDefaultValueForNull=true` (deprecated legacy mode) if no
matching elements exist in the array. |
-| array_ordinal_of(arr,expr) | returns the 1 based index of the first
occurrence of expr in the array, or `null` or `-1` if
`druid.generic.useDefaultValueForNull=true` (deprecated legacy mode) if no
matching elements exist in the array. |
-| array_prepend(expr,arr) | adds expr to arr at the beginning, the resulting
array type determined by the type of the array |
-| array_append(arr,expr) | appends expr to arr, the resulting array type
determined by the type of the first array |
-| array_concat(arr1,arr2) | concatenates 2 arrays, the resulting array type
determined by the type of the first array |
-| array_set_add(arr,expr) | adds expr to arr and converts the array to a new
array composed of the unique set of elements. The resulting array type
determined by the type of the array |
-| array_set_add_all(arr1,arr2) | combines the unique set of elements of 2
arrays, the resulting array type determined by the type of the first array |
-| array_slice(arr,start,end) | return the subarray of arr from the 0 based
index start(inclusive) to end(exclusive), or `null`, if start is less than 0,
greater than length of arr or less than end|
-| array_to_string(arr,str) | joins all elements of arr by the delimiter
specified by str |
-| string_to_array(str1,str2) | splits str1 into an array on the delimiter
specified by str2, which is a regular expression |
+| function | description
|
+|------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| array(expr1,expr ...) | constructs an array from the expression
arguments, using the type of the first argument as the output array type
|
+| array_length(arr) | returns length of array expression
|
+| array_offset(arr,long) | returns the array element at the 0 based
index supplied, or null for an out of range index
|
+| array_ordinal(arr,long) | returns the array element at the 1 based
index supplied, or null for an out of range index
|
+| array_contains(arr,expr) | returns 1 if the array contains the element
specified by expr, or contains all elements specified by expr if expr is an
array, else 0
|
+| array_overlap(arr1,arr2) | returns 1 if arr1 and arr2 have any elements
in common, else 0
|
+| scalar_in(expr,arr) | returns 1 if the array contains the scalar
specified by expr, else 0.
|
|
Review Comment:
`scalar_in_array` may be a better name. Wondering what you / others think?
Should also add a reference to the function in the SQL docs.
##########
sql/src/main/java/org/apache/druid/sql/calcite/expression/builtin/ScalarInOperatorConversion.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.druid.sql.calcite.expression.builtin;
+
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.druid.sql.calcite.expression.DirectOperatorConversion;
+import org.apache.druid.sql.calcite.expression.OperatorConversions;
+
+public class ScalarInOperatorConversion extends DirectOperatorConversion
+{
+ private static final SqlFunction SQL_FUNCTION = OperatorConversions
+ .operatorBuilder("SCALAR_IN")
+ .operandTypeChecker(
+ OperandTypes.sequence(
+ "'SCALAR_IN(expr, array)'",
+ OperandTypes.or(
+ OperandTypes.family(SqlTypeFamily.CHARACTER),
+ OperandTypes.family(SqlTypeFamily.NUMERIC)
+ ),
+ OperandTypes.or(
Review Comment:
No reason for an `or` with just one family.
##########
processing/src/main/java/org/apache/druid/math/expr/Function.java:
##########
@@ -3724,6 +3724,44 @@ ExprEval doApply(ExprEval arrayExpr, ExprEval scalarExpr)
}
}
+ class ArrayScalarInFunction extends ArrayScalarFunction
+ {
+ @Override
+ public String name()
+ {
+ return "scalar_in";
+ }
+
+ @Nullable
+ @Override
+ public ExpressionType getOutputType(Expr.InputBindingInspector inspector,
List<Expr> args)
+ {
+ return ExpressionType.LONG;
+ }
+
+ @Override
+ Expr getScalarArgument(List<Expr> args)
+ {
+ return args.get(0);
+ }
+
+ @Override
+ Expr getArrayArgument(List<Expr> args)
+ {
+ return args.get(1);
+ }
+
+ @Override
+ ExprEval doApply(ExprEval arrayExpr, ExprEval scalarExpr)
Review Comment:
You don't need to do this now, but as a follow up you could add an
`asSingleThreaded` impl that creates a Set to avoid the
`Arrays.asList(array).contains`, similar to the technique used in
`OverlapConstantArray`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]