Execute SQL queries in Deephaven
Structured Query Language (SQL) is the most popular programming language for database management and access. Its popularity can be attributed to a number of factors, including its simplicity, readability, and interoperability. Deephaven supports executing SQL queries against in-memory Deephaven tables using the Sql and SqlAdapter Java classes.
This guide will show you how to execute SQL queries on Deephaven tables in your script session.
The Sql class
The Sql class provides high-level methods for executing SQL queries against tables in your script session. It offers two main methods:
evaluate- Executes a SQL query and returns a Deephaven table.dryRun- Parses a SQL query into aTableSpecwithout executing it. Useful for validation.
These methods automatically use tables from your script session's query scope as the catalog for SQL queries.
Execute a query with evaluate
The simplest way to execute SQL against Deephaven tables is with evaluate():
The query references tables by their variable names in the script session (source and other). The result is a new Deephaven table.
Supported SQL features
Deephaven's SQL implementation supports:
- SELECT statements with column selection and aliasing.
- WHERE clauses for filtering.
- JOIN operations (INNER, LEFT, RIGHT, FULL).
- GROUP BY and aggregate functions (SUM, COUNT, AVG, MIN, MAX).
- ORDER BY for sorting.
- LIMIT and OFFSET for pagination.
- Standard SQL expressions and operators.
Validate queries with dryRun
Use dryRun to validate SQL syntax and parse the query without executing it:
This is useful for:
- Validating SQL syntax before execution.
- Validating a query quickly without the overhead of execution.
- Inspecting the query plan.
- Building tools that work with SQL queries.
Advanced usage with SqlAdapter
For more control over SQL parsing, use SqlAdapter.parseSql(). This method allows you to specify an explicit Scope (catalog) instead of using the script session's query scope.
This approach is useful when:
- Building custom SQL tooling.
- Integrating SQL parsing into other systems.
- You need full control over the catalog/scope.
Choose the right method
Each of the above methods serves different use cases.
evaluate: This is the simplest and most direct way to run SQL queries against tables in your script session. Use it if you don't need control over the scope.dryRun: Use this when you want to validate SQL syntax without executing it. It's a lightweight way to check your queries.parseSql: Use this for advanced use cases requiring explicit scope control or custom SQL tooling, or if you want to use tables not in your script session.