An array judgment function performs logic judgment on the input array data and returns a Boolean value. OceanBase Database supports the array judgment functions array_contains(), array_contains_all(), and array_overlaps() and the ANY() operator.
array_contains()
The array_contains() function verifies whether a specific element is included in an array. The syntax is as follows:
array_contains(arr1, element)
The arguments are described as follows:
- The data passed in by using the
arr1argument must be of the ARRAY type. - The element specified by the
elementargument can be of any type.
The returned value 1 indicates that the specified element is included in the array, and 0 indicates that the element is not included in the array.
Here are some examples:
SELECT array_contains([1,2,3], 2);
+----------------------------+
| array_contains([1,2,3], 2) |
+----------------------------+
| 1 |
+----------------------------+
1 row in set
SELECT array_contains([1,2,3], 6);
+----------------------------+
| array_contains([1,2,3], 6) |
+----------------------------+
| 0 |
+----------------------------+
1 row in set
SELECT array_contains(["hello", "hi"], "hel");
+----------------------------------------+
| array_contains(["hello", "hi"], "hel") |
+----------------------------------------+
| 0 |
+----------------------------------------+
1 row in set
SELECT array_contains(["hello", "hi"], "hi");
+---------------------------------------+
| array_contains(["hello", "hi"], "hi") |
+---------------------------------------+
| 1 |
+---------------------------------------+
1 row in set
SELECT array_contains([[1,2],[3,4]], [3.0,4.0]);
+------------------------------------------+
| array_contains([[1,2],[3,4]], [3.0,4.0]) |
+------------------------------------------+
| 1 |
+------------------------------------------+
1 row in set
array_contains_all
The array_contains_all() function judges whether the specified array contains all elements of another array. The syntax is as follows:
array_contains_all(arr1, arr2)
The parameters are described as follows:
- The
arr1parameter must be of an array type. - The
arr2parameter must be of an array type.
The return value is 1 if the specified array contains all elements of another array, and 0 otherwise.
The arr1 and arr2 parameters must have the same (or convertible) element types. Otherwise, the function returns an error.
Here are some examples:
SELECT array_contains_all([1,2,3], [2]);
+----------------------------------+
| array_contains_all([1,2,3], [2]) |
+----------------------------------+
| 1 |
+----------------------------------+
1 row in set
SELECT array_contains_all([2], [1,2,3]);
+----------------------------------+
| array_contains_all([2], [1,2,3]) |
+----------------------------------+
| 0 |
+----------------------------------+
1 row in set
SELECT array_contains_all([1,2,3], null);
+-----------------------------------+
| array_contains_all([1,2,3], null) |
+-----------------------------------+
| NULL |
+-----------------------------------+
1 row in set
SELECT array_contains_all([1,2,3], [2.0]);
+------------------------------------+
| array_contains_all([1,2,3], [2.0]) |
+------------------------------------+
| 1 |
+------------------------------------+
1 row in set
SELECT array_contains_all(["hello", "hi"], ["hi"]);
+---------------------------------------------+
| array_contains_all(["hello", "hi"], ["hi"]) |
+---------------------------------------------+
| 1 |
+---------------------------------------------+
1 row in set
SELECT array_contains_all([[1,2],[3,4]], [[3.0,4.0]]);
+------------------------------------------------+
| array_contains_all([[1,2],[3,4]], [[3.0,4.0]]) |
+------------------------------------------------+
| 1 |
+------------------------------------------------+
1 row in set
SELECT array_contains_all([["hello", "world"], ["hi", "what"], ["are you?"]], [["are you?"]]);
+----------------------------------------------------------------------------------------+
| array_contains_all([["hello", "world"], ["hi", "what"], ["are you?"]], [["are you?"]]) |
+----------------------------------------------------------------------------------------+
| 1 |
+----------------------------------------------------------------------------------------+
1 row in set
array_overlaps
The array_overlaps() function judges whether two arrays have an intersection. The syntax is as follows:
array_overlaps(arr1, arr2)
The parameters are described as follows:
- The
arr1parameter must be of an array type. - The
arr2parameter must be of an array type.
The return value is 1 if the two arrays have an intersection, and 0 otherwise.
The arr1 and arr2 parameters must have the same (or convertible) element types. Otherwise, the function returns an error.
Here are some examples:
SELECT array_overlaps([1,2,3], [2]);
+------------------------------+
| array_overlaps([1,2,3], [2]) |
+------------------------------+
| 1 |
+------------------------------+
1 row in set
SELECT array_overlaps([1,2,3], null);
+-------------------------------+
| array_overlaps([1,2,3], null) |
+-------------------------------+
| NULL |
+-------------------------------+
1 row in set
SELECT array_overlaps([1,2,3], [2.0]);
+--------------------------------+
| array_overlaps([1,2,3], [2.0]) |
+--------------------------------+
| 1 |
+--------------------------------+
1 row in set
SELECT array_overlaps([1.1,2.2,3.3], [2.2]);
+--------------------------------------+
| array_overlaps([1.1,2.2,3.3], [2.2]) |
+--------------------------------------+
| 1 |
+--------------------------------------+
1 row in set
SELECT array_overlaps(["hello", "hi"], ["hi"]);
+-----------------------------------------+
| array_overlaps(["hello", "hi"], ["hi"]) |
+-----------------------------------------+
| 1 |
+-----------------------------------------+
1 row in set
SELECT array_overlaps([[1,2],[3,4]], [[3,4]]);
+----------------------------------------+
| array_overlaps([[1,2],[3,4]], [[3,4]]) |
+----------------------------------------+
| 1 |
+----------------------------------------+
1 row in set
SELECT array_overlaps([[1,2],[3,4]], [[3.0,4.0]]);
+--------------------------------------------+
| array_overlaps([[1,2],[3,4]], [[3.0,4.0]]) |
+--------------------------------------------+
| 1 |
+--------------------------------------------+
1 row in set
SELECT array_overlaps([["hello", "world"], ["hi", "what"], ["are you?"]], [["are you?"]]);
+------------------------------------------------------------------------------------+
| array_overlaps([["hello", "world"], ["hi", "what"], ["are you?"]], [["are you?"]]) |
+------------------------------------------------------------------------------------+
| 1 |
+------------------------------------------------------------------------------------+
1 row in set
ANY
The functionality, input arguments, and return values of the ANY() operator are the same as those of the array_contains() function. The difference between the two lies in the syntax.
element = ANY(arr1)
Here is an example:
SELECT 2 = ANY([1,2,3]);
SELECT 6 = ANY([1,2,3]);
SELECT "hel" = ANY(["hello", "hi"]);
SELECT "hi" = ANY(["hello", "hi"]);
SELECT [3.0,4.0] = ANY([[1,2],[3,4]]);