Array type#

Arrays provide a convenient way to represent a sequence of values of the same type.

Basics#

Building an array#

from test_udf_array of rbc/tests/heavydb/test_howtos.py#
1from rbc.heavydb import Array
2
3@heavydb('Array<int64>(int32)')
4def arr_new(size):
5    arr = Array(size, dtype='int64')
6    for i in range(size):
7        arr[i] = 1
8    return arr
Example SQL Query
from test_geopoint of rbc/tests/heavydb/test_howtos.py#
1_, r = heavydb.sql_execute('SELECT arr_new(5);')
2assert list(r) == [([1, 1, 1, 1, 1],)]

Computing the length of an array#

from test_udf_array of rbc/tests/heavydb/test_howtos.py#
1@heavydb('int64(Array<int32>)')
2def my_length(arr):
3    return len(arr)
Example SQL Query
from test_geopoint of rbc/tests/heavydb/test_howtos.py#
1_, r = heavydb.sql_execute(f'SELECT my_length(i4) from {table_name}')
2assert list(r) == [(0,), (1,), (2,), (3,), (4,)]

Using the array_api#

RBC partially implements the array api standard. For a list of supported functions, check the API page.

from test_udf_array of rbc/tests/heavydb/test_howtos.py#
1from rbc.stdlib import array_api
2
3@heavydb('Array<int64>(int32)')
4def arr_new_ones(sz):
5    return array_api.ones(sz, dtype='int64')
Example SQL Query
from test_geopoint of rbc/tests/heavydb/test_howtos.py#
1_, r = heavydb.sql_execute('SELECT arr_new_ones(5);')
2assert list(r) == [([1, 1, 1, 1, 1],)]