Skip to main content
Version: Python

Functions

Python, Groovy, and Java functions can be used inside query strings. These functions can be from libraries, or they can be user-defined.

The list of default Java imports can be reviewed in the deephaven-core repository on GitHub.

Examples

User-defined function

In the following example, a custom, user-defined function is used inside a query string to compute new column values.

from deephaven import new_table
from deephaven.column import int_col

source = new_table([
int_col("X", [2, 4, 6]),
int_col("Y", [8, 10, 12])
])

def f(a, b):
return a * b

result = source.update(formulas=["X", "Y", "Product = f(X, Y)"])

Passing tables to functions

In the following example, the compute function uses the source table and the input parameter a.

from deephaven import new_table
from deephaven.column import int_col

def f(a, b):
return a * b

def compute(source, a):
globals()["var_a"]= a
return source.update(formulas=["X = (int) f(var_a, A)"])

source = new_table([int_col("A", [1, 2, 3])])

result1 = compute(source, 10)
result2 = compute(source, 3)

Function from a library

In the following example, a function from a library is called.

from deephaven import empty_table
from os import getenv

result = empty_table(2).update(formulas=["Home = getenv(`HOME`)"])

Imported Java methods

In the following example, imported Java methods are used inside the query string.

from deephaven import new_table
from deephaven.column import int_col

source = new_table([
int_col("X", [2, 4, 6])
])
result = source.update(formulas=["Z = sqrt(X)"])