Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Apache Druid Sql Parsing And Time Floor Generation

From Leeroopedia


Knowledge Sources
Domains Web_Console, SQL_Processing
Last Updated 2026-02-10 10:00 GMT

Overview

Provides SQL parsing, formatting, query extraction, and time-floor expression generation utilities for the Druid web console.

Description

The SQL Utils module contains functions for pretty-printing SQL AST nodes, converting time format identifiers (auto, iso, posix, millis, micro, nano, or custom Joda patterns) to SQL TIME_PARSE/MILLIS_TO_TIMESTAMP expressions, extracting GROUP BY expressions from aggregation functions, and finding all SQL queries within a multi-query text editor buffer. The `findAllSqlQueriesInText` function identifies individual SELECT, INSERT, REPLACE, WITH, SET, and EXPLAIN statements with their start/end offsets. A `smartTimeFloor` utility generates TIME_FLOOR expressions that align sub-day granularities to day boundaries.

Usage

Used in the query workbench to parse multi-statement SQL text, highlight individual queries, provide query-at-cursor functionality, convert time format specifications during ingestion setup, and generate smart time floor expressions for the explore view.

Code Reference

Source Location

Signature

export function prettyPrintSql(b: SqlBase): string;
export function timeFormatToSql(timeFormat: string): SqlExpression | undefined;
export function convertToGroupByExpression(ex: SqlExpression): SqlExpression | undefined;
export function findSqlQueryPrefix(text: string): string | undefined;
export function cleanSqlQueryPrefix(text: string): string;

export interface QuerySlice {
  index: number;
  startOffset: number;
  startRowColumn: RowColumn;
  endOffset: number;
  endRowColumn: RowColumn;
  sql: string;
}

export function findAllSqlQueriesInText(text: string): QuerySlice[];
export function smartTimeFloor(ex: SqlExpression, granularity: string, isUTC: boolean): SqlExpression;

Import

import { findAllSqlQueriesInText, prettyPrintSql, smartTimeFloor } from './utils/sql';

I/O Contract

Inputs

Name Type Required Description
b SqlBase Yes A SQL AST node from druid-query-toolkit for pretty-printing
timeFormat string Yes A time format identifier: 'auto', 'iso', 'posix', 'millis', 'micro', 'nano', or a Joda pattern
ex SqlExpression Yes A SQL expression (e.g., an aggregate function or a column reference)
text string Yes A multi-statement SQL text to scan for individual queries
granularity string Yes An ISO 8601 duration string for TIME_FLOOR granularity
isUTC boolean Yes Whether the timezone is UTC (affects origin alignment in smartTimeFloor)

Outputs

Name Type Description
(return) string Pretty-printed SQL string trimmed to 50 characters
(return) undefined SQL expression for the given time format, or undefined if unrecognized
(return) QuerySlice[] Array of discovered SQL queries with offsets and row/column positions

Usage Examples

Extract all queries from editor text

import { findAllSqlQueriesInText } from './utils/sql';

const text = 'SELECT * FROM wikipedia\nSELECT COUNT(*) FROM wikipedia';
const queries = findAllSqlQueriesInText(text);
// queries[0].sql === 'SELECT * FROM wikipedia'
// queries[1].sql === 'SELECT COUNT(*) FROM wikipedia'

Convert a time format to SQL expression

import { timeFormatToSql } from './utils/sql';

const autoExpr = timeFormatToSql('auto');
// Produces: TIME_PARSE(TRIM(?))

const posixExpr = timeFormatToSql('posix');
// Produces: MILLIS_TO_TIMESTAMP((?) * 1000)

Smart time floor with day alignment

import { smartTimeFloor } from './utils/sql';
import { C } from 'druid-query-toolkit';

const floored = smartTimeFloor(C('__time'), 'PT6H', false);
// Produces: TIME_FLOOR(__time, 'PT6H', TIME_FLOOR(__time, 'P1D'))

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment