Implementation:Apache Druid Values Query
| Knowledge Sources | |
|---|---|
| Domains | Web_Console, SQL_Generation |
| Last Updated | 2026-02-10 10:00 GMT |
Overview
Converts a Druid QueryResult into a SQL VALUES query that reproduces the result set as an inline table.
Description
The Values Query module transforms a druid-query-toolkit QueryResult (with typed header columns and rows) into a SQL SELECT ... FROM (VALUES ...) query. It handles type-specific serialization: COMPLEX<json> values are wrapped in PARSE_JSON, array columns are encoded with a separator and decoded via STRING_TO_ARRAY, COMPLEX binary types use DECODE_BASE64_COMPLEX, and standard types receive appropriate CAST expressions. The module also corrects for a legacy Druid behavior where ARRAY types may be described inconsistently in column metadata.
Usage
Used when exporting query results as SQL format in the download utilities, and in other scenarios where query results need to be materialized as inline SQL VALUES expressions.
Code Reference
Source Location
- Repository: Apache Druid
- File: web-console/src/utils/values-query.tsx
- Lines: 1-122
Signature
export function queryResultToValuesQuery(sample: QueryResult): SqlQuery;
Import
import { queryResultToValuesQuery } from './utils/values-query';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sample | QueryResult |
Yes | A druid-query-toolkit QueryResult containing typed header columns and row data |
Outputs
| Name | Type | Description |
|---|---|---|
| (return) | SqlQuery |
A SqlQuery AST representing SELECT ... FROM (VALUES (...), (...)) AS t(c1, c2, ...) with appropriate casts and type conversions per column |
Usage Examples
Convert query results to a VALUES query
import { queryResultToValuesQuery } from './utils/values-query';
const valuesQuery = queryResultToValuesQuery(queryResult);
const sql = valuesQuery.toString();
// Produces: SELECT CAST("c1" AS VARCHAR) AS "channel", CAST("c2" AS BIGINT) AS "count"
// FROM (VALUES ('en', 100), ('de', 50)) AS "t" ("c1", "c2")
Use with download utilities
import { queryResultsToString } from './utils/download';
const sqlString = queryResultsToString(queryResult, 'sql');
// Internally calls queryResultToValuesQuery and converts to string