Python variables in query strings
The ability to use your own custom Python variables, functions, and classes in Deephaven query strings is one of its most powerful features. The use of Python variables in query strings follows some basic rules, which are outlined in this guide.
Python variables in query strings
There are three types of Python variables supported in the Deephaven Query Language (DQL). The use of variables in queries follows query scope rules similar to Python's LEGB ordering. Additionally, since the Deephaven engine is built on Java, the Python-Java boundary is an important consideration when using Python variables in query strings.
Scalars
Scalars are single values, such as numbers or booleans. They can be used directly in query strings without special syntax. The following example shows the basic use of Python variables in query strings to create new columns in a table:
Strings
Like scalars, Python strings can be used in query strings with no special syntax. The following example creates a table with two string columns:
Sequences
In Python, sequences include lists, NumPy arrays, and more. These can be used in query strings as well.
Extracting Python sequence elements in the query language is also supported. The Deephaven engine is not able to infer data types from extracted list elements, so be sure to use type casts to ensure the correct resultant column types.
It's common to use jpy to convert Python sequence variables to Java primitive arrays before using them in query strings. This is often more efficient than using the Python sequence directly.