Arrays
This guide shows you how to work with arrays in query strings.
Arrays are an invaluable tool for grouping related data. They provide an easy way to access previous and future values in time series datasets. Support for arrays is built into the Deephaven Query Language (DQL).
Array column types
Array columns fall into one of three categories of data type.
Array columns
Array columns are Java arrays of primitive types. For example, the following query creates a table with a single row containing an array of primitive integers.
You can also use Python functions to create Java primitive array columns:
The Deephaven engine can seamlessly work with these column types.
Vector columns
Vector columns arise from common table operations including grouping. These vector columns are used in dedicated aggregations, combined aggregations, update_by, and more. The following example creates a vector column by calling group_by:
PyListWrapper columns
An org.jpy.PyListWrapper is jpy's implementation of an array of Python objects. It is a generic object for an arbitrary Python sequence (list, NumPy array, tuple, etc.) whose type cannot be accurately inferred. It is less fully featured than the other array types.
This column type arises when Python sequences are used in query strings without explicit type information. For example:
source_meta shows that the resultant column type is org.jpy.PyListWrapper. This is fine in certain cases. However, this column type is the array equivalent of an org.jpy.PyObject data type, which is not performant and does not support many basic operations. For this reason, it's recommended to convert Python objects to their Java equivalents using either jpy or the Deephaven array method.
Using jpy:
Using deephaven.dtypes.array:
You can access individual elements from Python lists in query strings using standard indexing syntax. However, since Deephaven can't automatically determine the data type of these elements, you must use explicit type casts to ensure proper types:
Convert between arrays and vectors
Since Deephaven tables commonly use both Java primitive arrays and Deephaven vectors, it's useful to convert between the two. The following example converts between both vector and array columns:
Create array columns
By grouping
Arrays can be created using the group_by method to group data.
With aggregations
Certain aggregations create array columns. For example, the following operations create array columns:
The following example calls rolling_group_tick to create an array column:
Using the underscore operator
Every column in a table has an associated array variable, which can be accessed using the underscore (_) operator. This operator is specific to Deephaven. For example, a column called X can be accessed as an array by using the column name X_:
Get array length
The len method returns the length of the given input. This is useful in query strings where you need to get the size of a Vector or a Java array.
Access array elements
The square bracket operators [] are used to access elements in array columns. The following example uses these operators to access the previous and next elements in the array, as well as print the size of the array:
Note
The first row of column A is null because there is no previous element at the zeroth array index. The last row of column B is null because there is no next element at the last array index.
Additionally, you can access specific array elements directly using indexes.
Slice arrays
You can slice arrays into subarrays with subVector. The first input is the index at which the slice starts, while the second is the index at which the slice ends. They are inclusive and exclusive, respectively.
The following example slices the array column, then grabs specific elements from the subarray.
Functions with array arguments
Built-in query language functions
Caution
Dedicated aggregations, Combined aggregations, and update_by are always more performant than group_by followed by manual calculations when used on ticking tables.
Many built-in query language methods take arrays as input. The following example uses the sum and avg functions on a column containing arrays.
Python functions
Python functions that take arrays as input are also supported in query strings. The following example calls a Python function that takes a NumPy array as input: