toTuple

Syntax

toTuple(X)

Details

Use with GROUP BY to group by and convert X into a tuple. The function does not work when used alone.

The toArray function creates an array vector whose length cannot be changed in subsequent updates, whereas toTuple has no such restriction. This makes toTuple suitable for data grouping scenarios where the length may change, and is particularly well-suited for data processing that requires frequent updates or involves dynamic lengths.

Parameters

X is a column name or an expression on column(s).

Returns

A tuple.

Examples

t = table(1 1 3 4 as id, 10.1 10.3 9.5 9.6 as price)
select toTuple(price) as newPrice from t group by id

Simulate portfolio holding data: portfolio ID, stock symbol, and position size (dynamically changing).

t = table([`P1, `P1, `P1, `P2, `P2, `P3, `P3, `P3, `P3] as portfolioId, 
          [`AAPL, `GOOG, `MSFT, `AAPL, `TSLA, `GOOG, `GOOG, `AMZN, `NVDA] as symbol,
          [100, 50, 200, 150, 75, 80, 120, 90, 60] as position)

// Use toTuple to group by portfolio and aggregate all held stocks and quantities into tuples (with variable length).
result = select toTuple(symbol) as symbols, toTuple(position) as positions from t group by portfolioId
result
portfolioId symbols positions
P1 ['AAPL', 'GOOG', 'MSFT'] [100, 50, 200]
P2 ['AAPL', 'TSLA'] [150, 75]
P3 ['GOOG', 'GOOG', 'AMZN', 'NVDA'] [80, 120, 90, 60]

Related Functions: toArray, toColumnarTuple