withKeys
The withKeys method returns a table that shares the underlying data and schema with the source table, but with the specified columns marked as its key columns.
Key columns are metadata used by consumers such as the Deephaven UI to identify which rows represent the same real-world entity. They do not affect the table's data. Multiple rows may share the same key column values. To additionally assert that each combination of key values identifies exactly one row, use withUniqueKeys instead.
Syntax
Parameters
| Parameter | Type | Description |
|---|---|---|
| columns | String... | The key column name(s). Must name at least one existing column in the source table. |
Returns
A table that shares the underlying data and schema with the source table, with the keyColumns attribute set to the specified column names. If the source table already has this exact set of key columns, the source table itself may be returned.
Note
withKeys only sets keyColumns; it does not clear an existing uniqueKeys attribute. If the source table was previously marked with withUniqueKeys, uniqueKeys remains true after calling withKeys, even though the new key columns may no longer be unique. To fully convert such a table, also remove the attribute with withoutAttributes: table.withKeys(columns).withoutAttributes(["uniqueKeys"]), where columns is the same key column name(s) passed to withKeys.
Preserved through table operations
The keyColumns and uniqueKeys attributes are preserved by:
where,whereIn, andwhereNotInsortandsortDescendingreverseflattenupdateViewandlazyUpdatenaturalJoinandexactJoin
Other operations that build a new result from the table's data — including select, update, view, join, and dropColumns — clear both attributes. Call withKeys or withUniqueKeys again on the result to restore them.
withAttributes and withoutAttributes are a separate case: they preserve every attribute they aren't explicitly asked to add or remove, so keyColumns and uniqueKeys survive them unless you target those specific keys. retainingAttributes works the other way around — it drops every attribute except the ones you name, so keyColumns and uniqueKeys only survive it if you explicitly include them.
Examples
In this example, Key1 and Key2 together form the key column set for keyedTable. Because the same combination of values repeats across rows, selecting one of them in the Deephaven UI selects every row that shares that combination.
