Calculates the minimum value in an array. The function iterates through all elements in the array, finds the minimum value and returns it.
array_min(ARRAY<T> arr)
arr:ARRAY type, the array for which to calculate the minimum value.Supported types for T:
Return type: T
Return value meaning:
Usage notes:
Query Examples:
Calculate the minimum value of a floating-point array:
SELECT array_min([5.5, 2.2, 8.8, 1.1, 9.9, 3.3]); +-------------------------------------------+ | array_min([5.5, 2.2, 8.8, 1.1, 9.9, 3.3]) | +-------------------------------------------+ | 1.1 | +-------------------------------------------+
Calculate the minimum value of a string array (lexicographically):
SELECT array_min(['zebra', 'apple', 'banana', 'cherry']); +---------------------------------------------------+ | array_min(['zebra', 'apple', 'banana', 'cherry']) | +---------------------------------------------------+ | apple | +---------------------------------------------------+
Calculate the minimum value of an array containing null values, null elements are not included in comparison:
SELECT array_min([5, null, 2, null, 8, 1]); +-------------------------------------+ | array_min([5, null, 2, null, 8, 1]) | +-------------------------------------+ | 1 | +-------------------------------------+
Empty array returns NULL:
SELECT array_min([]); +------------------+ | array_min([]) | +------------------+ | NULL | +------------------+
Array with all null elements returns NULL:
SELECT array_min([null, null, null]); +----------------------------------+ | array_min([null, null, null]) | +----------------------------------+ | NULL | +----------------------------------+
Minimum value of a date array:
SELECT array_min(cast(['2023-01-01', '2022-12-31', '2023-06-15'] as array<datetime>)); +--------------------------------------------------------------------------------+ | array_min(cast(['2023-01-01', '2022-12-31', '2023-06-15'] as array<datetime>)) | +--------------------------------------------------------------------------------+ | 2022-12-31 00:00:00 | +--------------------------------------------------------------------------------+
Minimum value of an IP address array:
SELECT array_min(CAST(['192.168.1.100', '192.168.1.1', '192.168.1.50'] AS ARRAY<IPV4>)); +----------------------------------------------------------------------------------+ | array_min(CAST(['192.168.1.100', '192.168.1.1', '192.168.1.50'] AS ARRAY<IPV4>)) | +----------------------------------------------------------------------------------+ | 192.168.1.1 | +----------------------------------------------------------------------------------+ SELECT array_min(CAST(['2001:db8::1', '2001:db8::2', '2001:db8::0'] AS ARRAY<IPV6>)); +-------------------------------------------------------------------------------+ | array_min(CAST(['2001:db8::1', '2001:db8::2', '2001:db8::0'] AS ARRAY<IPV6>)) | +-------------------------------------------------------------------------------+ | 2001:db8:: | +-------------------------------------------------------------------------------+
Complex type examples:
Nested array types are not supported, will error:
SELECT array_min([[1,2],[3,4],[5,6]]); ERROR 1105 (HY000): errCode = 2, detailMessage = array_min does not support complex types: array_min([[1, 2], [3, 4], [5, 6]])
Map types are not supported, will error:
SELECT array_min([{'k':1},{'k':2},{'k':3}]); ERROR 1105 (HY000): errCode = 2, detailMessage = array_min does not support complex types: array_min([map('k', 1), map('k', 2), map('k', 3)])
Error when parameter count is wrong:
SELECT array_min([1,2,3], [4,5,6]); ERROR 1105 (HY000): errCode = 2, detailMessage = Can not found function 'array_min' which has 2 arity. Candidate functions are: [array_min(Expression)]
Error when passing non-array type:
SELECT array_min('not_an_array'); ERROR 1105 (HY000): errCode = 2, detailMessage = class org.apache.doris.nereids.types.VarcharType cannot be cast to class org.apache.doris.nereids.types.ArrayType (org.apache.doris.nereids.types.VarcharType and org.apache.doris.nereids.types.ArrayType are in unnamed module of loader 'app')
Array is NULL, will return type conversion error:
mysql> SELECT array_min(NULL); ERROR 1105 (HY000): errCode = 2, detailMessage = class org.apache.doris.nereids.types.NullType cannot be cast to class org.apache.doris.nereids.types.ArrayType (org.apache.doris.nereids.types.NullType and org.apache.doris.nereids.types.ArrayType are in unnamed module of loader 'app')
ARRAY, MIN, ARRAY_MIN