Skip to main content
Version: Java (Groovy)

Create static tables

Deephaven is often used to read table data from Parquet, Kafka, or other external sources, but it can also generate static or ticking tables from scratch. There are two functions for creating static tables: emptyTable and newTable. This guide will show you how to use these functions to create static tables and columns, and how to add data to those tables.

emptyTable

The emptyTable function takes a single argument - an int representing the number of rows in the new table. The resulting table has no columns and the specified number of rows. In the following example, we create a table with 10 rows and no columns:

table = emptyTable(10)

Calling emptyTable on its own generates a table with no data, but it can easily be populated with columns and data using update or another selection method. This can be done in the same line that creates the table, or at any time afterward.

In the following example, we create a table with 10 rows and a single column X with values 0 through 9 by using the special variable i to represent the row index. Then, the table is updated again to add a column Y with values equal to X squared:

table = emptyTable(10).update("X = i")

table = table.update("Y = X * X")

Deephaven's update and other selection methods can take user-defined functions as arguments and harness the power of the Deephaven Query Language to handle complex data transformations. For more information, see the select, view, and update guide.

DQL supports logical operators, Java functions, user-defined functions, and more. In the following example, we'll create a table with 100 rows, then create four columns:

source = emptyTable(100).update(
// mathematical operations are supported
"X = 0.1 * i",
// many built-in functions are provided to cover common operations
"SinX = sin(X)",
// in-line logical operations and comparison operators are supported
"PositiveSinX = SinX > 0 ? true : false",
// and they can all be combined
"TransformedX = PositiveSinX == true ? 5 * X : 0",
)

DQL is a powerful, versatile tool for table transformations. For more information, see the formula documentation.

newTable

Deephaven's newTable function allows you to create a new table and manually populate it with data. newTable accepts a list of Deephaven column objects. The following query creates a new table with a string column and an int column.

result = newTable(
stringCol("NameOfStringCol", "Data String 1", "Data String 2", "Data String 3"),
intCol("NameOfIntCol", 4, 5, 6),
)

Here, we create an example with two integer columns. Then, we update the table to add a new column X via a formula that uses a variable, a user-defined function, an auto-imported Java function, and various operators:

var = 3

f = { a, b -> a + b }
source = newTable(intCol("A", 1, 2, 3, 4, 5), intCol("B", 10, 20, 30, 40, 50))

result = source.update("X = A + 3 * sqrt(B) + var + (int)f(A, B)")

Create new columns in a table

Here, we will go into detail on creating new columns in your tables.

Selection methods -- such as select, view, update, updateView, and lazyUpdate -- and formulas are used to create new columns:

  • The selection method determines which columns will be in the output table and how the values are computed.
  • The formulas are the recipes for computing the cell values.

In the following examples, we use a table of student test results. Using update, we create a new Total column containing the sum of each student's math, science, and art scores. Notice that update includes the columns from the source table in the output table.

scores = newTable(
stringCol("Name", "James", "Lauren", "Zoey"),
intCol("Math", 95, 72, 100),
intCol("Science", 100, 78, 98),
intCol("Art", 90, 92, 96),
)

total = scores.update("Total = Math + Science + Art")

Now we make the example a little more complicated by adding a column of average test scores.

average = scores.update("Average = (Math + Science + Art) / 3 ")

For the next example, we have the students' test results in various subjects and the class averages. We want to see which students scored higher than the class average. We can use the select method to create a table containing the Name and Subject columns from the source table, plus a new column indicating if the score is above average.

classAverage = newTable(
stringCol("Name", "James", "James", "James", "Lauren", "Lauren", "Lauren", "Zoey", "Zoey", "Zoey"),
stringCol("Subject", "Math", "Science", "Art", "Math", "Science", "Art", "Math", "Science", "Art"),
intCol("StudentAverage", 95, 100, 90, 72, 78, 92, 100, 98, 96),
intCol("ClassAverage", 86, 90, 95, 86, 90, 95, 86, 90, 95),
)

aboveAverage = classAverage.select("Name", "Subject", "AboveAverage = StudentAverage > ClassAverage")

Column types

Deephaven supports the following column types:

Data TypeMethod
booleanbooleanCol
bytebyteCol
charcharCol
java.time.InstantinstantCol
doubledoubleCol
floatfloatCol
intintCol
longlongCol
shortshortCol
StringstringCol