Groovy variables in query strings
The ability to use your own custom Groovy variables, closures, and classes in Deephaven query strings is one of its most powerful features. The use of Groovy variables in query strings follows some basic rules, which are outlined in this guide.
There are three types of Groovy variables supported in the Deephaven Query Language (DQL). The use of variables in queries follows Groovy's scoping rules and Deephaven's query scope mechanism.
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 Groovy variables in query strings to create new columns in a table:
a = 4
b = 3.14
c = -1.91e7
source = emptyTable(1).update("A = a", "B = b", "C = c + 1")
sourceMeta = source.meta()
Strings
Like scalars, Groovy strings can be used in query strings with no special syntax. The following example creates a table with two string columns:
myFirstString = "Hello, world!"
mySecondString = "Coding is fun."
source = emptyTable(1).update("FirstString = myFirstString", "SecondString = mySecondString")
sourceMeta = source.meta()
Sequences
In Groovy, sequences include lists, arrays, and more. These can be used in query strings as well.
myList = [1, 2, 3]
sourceList = emptyTable(1).update("ListColumn = myList")
sourceListMeta = sourceList.meta()
Extracting Groovy 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.
myList = [1, 2, 3]
source = emptyTable(1).update(
"FirstElement = (int)myList[0]",
"SecondElement = (int)myList[1]",
"ThirdElement = (int)myList[2]",
)
sourceMeta = source.meta()