Table/Row Function Manager#
The function managers in HeavyDB provide a convenient mechanism of handling the state of user-defined functions. They can perform various tasks such as allocate memory for output buffers, retrieve dictionary encoded strings in the dictionary proxy, and raise exceptions.
Table Function Manager#
Basic usage#
1@heavydb('int32(TableFunctionManager, Column<int64>, OutputColumn<int64>)')
2def table_copy(mgr, inp, out):
3 size = len(inp)
4 mgr.set_output_row_size(size)
5 for i in range(size):
6 out[i] = inp[i]
7 return size
Example SQL Query
1query = '''
2 SELECT * FROM TABLE(table_copy(
3 cursor(SELECT * FROM TABLE(generate_series(0, 4, 1)))
4 ))
5'''
6_, r = heavydb.sql_execute(query)
7assert list(r) == [(0,), (1,), (2,), (3,), (4,)]
Retrieving the dictionary string proxy#
When the Column has type TextEncodingDict
, users can access the dictionary
string proxy by calling the string_dict_proxy
attribute:
1@heavydb('int32(TableFunctionManager, Column<T>, OutputColumn<T> | input_id=args<0>)',
2 T=['TextEncodingDict'])
3def test_string_proxy(mgr, inp, out):
4 size = len(inp)
5 mgr.set_output_row_size(size)
6 for i in range(size):
7 s = inp.string_dict_proxy.get_string(inp[i])
8 id = out.string_dict_proxy.get_or_add_transient(s.title())
9 out[i] = id
10 return size
For additional information and references, please refer to the API page and the dedicated how-to page on the string dictionary proxy in HeavyDB.
Row Function Manager#
1@heavydb('TextEncodingDict(RowFunctionManager, TextEncodingDict)')
2def concat(mgr, text):
3 db_id: int = mgr.get_dict_db_id('concat', 0)
4 dict_id: int = mgr.get_dict_id('concat', 0)
5 s: str = mgr.get_string(db_id, dict_id, text)
6 s_concat = 'test: ' + s
7 return mgr.get_or_add_transient(mgr.TRANSIENT_DICT_DB_ID,
8 mgr.TRANSIENT_DICT_ID,
9 s_concat)
Example SQL Query
1query = f'''
2 SELECT concat(t1) FROM {table}
3'''
4_, r = heavydb.sql_execute(query)
5assert list(r) == [('test: fun',), ('test: bar',), ('test: foo',),
6 ('test: barr',), ('test: foooo',)]