Calculates the average of all numeric elements in an array. The function skips null values and non-numeric elements in the array, only calculating the average for valid numeric elements.
array_avg(ARRAY<T> arr)
arr:ARRAY type, the array for which to calculate the average. Supports column names or constant values.Supported types for T:
Return type: Automatically selected based on input type
Return value meaning:
Usage notes:
CREATE TABLE array_avg_test ( id INT, int_array ARRAY<INT>, double_array ARRAY<DOUBLE>, mixed_array ARRAY<STRING> ) DUPLICATE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 3 PROPERTIES ( "replication_num" = "1" ); INSERT INTO array_avg_test VALUES (1, [1, 2, 3, 4, 5], [1.1, 2.2, 3.3, 4.4, 5.5], ['1', '2', '3', '4', '5']), (2, [10, 20, 30], [10.5, 20.5, 30.5], ['10', '20', '30']), (3, [], [], []), (4, NULL, NULL, NULL), (5, [1, null, 3, null, 5], [1.1, null, 3.3, null, 5.5], ['1', null, '3', null, '5']);
Query Examples:
Calculate the average of double_array:
SELECT array_avg(double_array) FROM array_avg_test WHERE id = 1; +-------------------------+ | array_avg(double_array) | +-------------------------+ | 3.3 | +-------------------------+
Calculate the average of a mixed-type array, strings will be converted to numeric:
SELECT array_avg(mixed_array) FROM array_avg_test WHERE id = 1; +------------------------+ | array_avg(mixed_array) | +------------------------+ | 3 | +------------------------+
Empty array returns NULL:
SELECT array_avg(int_array) FROM array_avg_test WHERE id = 3; +----------------------+ | array_avg(int_array) | +----------------------+ | NULL | +----------------------+
NULL array returns NULL:
SELECT array_avg(int_array) FROM array_avg_test WHERE id = 4; +----------------------+ | array_avg(int_array) | +----------------------+ | NULL | +----------------------+
Array containing null values, null elements are not included in calculation:
SELECT array_avg(int_array) FROM array_avg_test WHERE id = 5; +----------------------+ | array_avg(int_array) | +----------------------+ | 3 | +----------------------+
Complex type examples:
Nested array types are not supported, will error:
SELECT array_avg([[1,2,3]]); ERROR 1105 (HY000): errCode = 2, detailMessage = array_avg([[1, 2, 3]]) does not support type: ARRAY<TINYINT>
Map types are not supported, will error:
SELECT array_avg([{'k':1},{'k':2}]); ERROR 1105 (HY000): errCode = 2, detailMessage = array_avg([map('k', 1), map('k', 2)]) does not support type: MAP<VARCHAR(1),TINYINT>
Error when parameter count is wrong:
SELECT array_avg([1,2,3], [4,5,6]); ERROR 1105 (HY000): errCode = 2, detailMessage = Can not found function 'array_avg' which has 2 arity. Candidate functions are: [array_avg(Expression)]
Error when passing non-array type:
SELECT array_avg('not_an_array'); ERROR 1105 (HY000): errCode = 2, detailMessage = Can not find the compatibility function signature: array_avg(VARCHAR(12))
Array is NULL, will return type conversion error
mysql> SELECT array_max(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, AVG, ARRAY_AVG