ARRAY
ARRAY is an ordered sequence of same-type elements that supports index access, slicing, set operations, and higher-order functions. It is well-suited for storing tag lists, score sequences, log paths, and similar data.
Type selection reference for complex types:
| Type | Structure | Best for |
|---|---|---|
ARRAY<T> | Ordered list, uniform element type | Tags, scores, ID lists |
MAP<K,V> | Key-value pairs, uniform key type | Dynamic attributes, name-to-value mappings |
STRUCT<f:T,...> | Named fields, heterogeneous types | Fixed-structure objects such as user information |
Syntax and Definition
Type declaration
Construction methods
Literal type inference: integer literals infer to array<int>, decimals infer to array<decimal(p,s)>:
Element access: index starts at 0; out-of-bounds or negative indices return NULL:
Creating Tables and Writing Data
Querying basic information:
| id | tags | first_tag | tag_count |
|---|---|---|---|
| 1 | ["read","write","admin"] | read | 3 |
| 2 | ["read"] | read | 1 |
| 3 | ["read","write"] | read | 2 |
Common Functions
| Function | Description | Example Result |
|---|---|---|
size(arr) / cardinality(arr) | Number of elements | size([1,2,3]) → 3 |
array_contains(arr, val) | Whether a value is present | array_contains([1,2,3], 2) → true |
array_position(arr, val) | First occurrence position (1-based); returns 0 if not found | array_position([10,20,30,20], 20) → 2 |
array_append(arr, val) | Append to end | array_append([1,2], 3) → [1,2,3] |
array_prepend(arr, val) | Prepend to start | array_prepend([1,2], 0) → [0,1,2] |
array_remove(arr, val) | Remove all matching elements (returns NULL when val is NULL) | array_remove([1,2,3,2], 2) → [1,3] |
array_distinct(arr) | Deduplicate, preserving first-occurrence order | array_distinct([1,2,2,3]) → [1,2,3] |
array_sort(arr) | Sort ascending, NULLs at end | array_sort([3,1,NULL,2]) → [1,2,3,null] |
sort_array(arr, asc) | Specify ascending/descending; NULLs always at end | sort_array([3,1,2], false) → [3,2,1] |
array_max(arr) | Maximum value (ignores NULL) | array_max([3,1,5,2]) → 5 |
array_min(arr) | Minimum value (ignores NULL) | array_min([3,1,5,2]) → 1 |
array_union(arr1, arr2) | Merge and deduplicate | array_union([1,2], [2,3]) → [1,2,3] |
array_intersect(arr1, arr2) | Intersection | array_intersect([1,2,3], [2,3,4]) → [2,3] |
array_except(arr1, arr2) | Difference (elements in arr1 but not arr2) | array_except([1,2,3], [2]) → [1,3] |
flatten(arr) | Flatten one level of nesting | flatten([[1,2],[3,4]]) → [1,2,3,4] |
slice(arr, start, length) | Slice; start is 1-based, supports negative to count from end | slice([10,20,30,40], 2, 2) → [20,30] |
arrays_zip(arr1, arr2, ...) | Merge element-by-element into a STRUCT array | See example below |
array_join(arr, sep) | Convert elements to strings and concatenate | array_join([1,2,3], ',') → '1,2,3' |
array_join(arr, sep, nullReplacement) | Fill NULL elements with replacement string | array_join(['a',NULL,'c'], ',', 'N/A') → 'a,N/A,c' |
Slice Example
arrays_zip Example
arrays_zip merges multiple arrays element-by-element into a STRUCT array; field names are auto-generated as "0", "1", "2":
Higher-Order Functions
TRANSFORM and FILTER support lambda expressions for element-by-element processing:
Combined example — filter then transform:
Aggregation and Expansion
ARRAY_AGG — Rows to Array
Aggregate multiple row values into a single array; supports ORDER BY and DISTINCT (cannot be used together):
| dept | members |
|---|---|
| Eng | ["Alice","Bob"] |
| HR | ["Carol","Dave"] |
EXPLODE — Array to Rows
EXPLODE expands an array into multiple rows; use with LATERAL VIEW to retain other fields from the original row:
| id | tag |
|---|---|
| 1 | admin |
| 1 | read |
| 1 | write |
| 2 | read |
| 3 | read |
| 3 | write |
POSEXPLODE additionally returns the element's position in the array (0-based):
| id | pos | tag |
|---|---|---|
| 1 | 0 | read |
| 1 | 1 | write |
| 1 | 2 | admin |
Type Conversion
Difference between CAST to STRING and TO_JSON:
NULL Handling
Notes
- Index starts at
0;slicestart position starts at1. These are inconsistent — be careful to distinguish them. - ARRAY supports
=equality comparison but does not support>,<, or other ordering comparisons, and cannot be used as a GROUP BY key or JOIN key. TRANSFORMandFILTERare lambda higher-order functions;AGGREGATE/REDUCEare not yet supported.ARRAY_AGG'sDISTINCTandORDER BYcannot be used together.array_positionreturns a 1-based position; returns0when not found, which differs from index access (0-based).
Related Documents
- MAP: key-value pair type
- STRUCT: named structure with multiple fields and types
- Data Type Conversion: complex type conversion rules
