import { type IntervalUnit } from './values.js'; import { type ClickHouseColumn } from './types.js'; import { ClickHouseError } from './values.js'; export { ClickHouseError }; export type Expr = { k: 'lit'; v: unknown; type: string; text: string; } | { k: 'id'; parts: string[]; } | { k: 'star'; qualifier?: string; } | { k: 'fn'; name: string; args: Expr[]; params?: Expr[]; distinct?: boolean; } | { k: 'lambda'; params: string[]; body: Expr; } | { k: 'bin'; op: string; a: Expr; b: Expr; } | { k: 'not'; a: Expr; } | { k: 'neg'; a: Expr; } | { k: 'isnull'; a: Expr; not: boolean; } | { k: 'between'; a: Expr; lo: Expr; hi: Expr; not: boolean; } | { k: 'case'; operand?: Expr; whens: Array<[Expr, Expr]>; else?: Expr; } | { k: 'alias'; name: string; e: Expr; } | { k: 'subquery'; q: Query; } | { k: 'array'; items: Expr[]; } | { k: 'tuple'; items: Expr[]; } | { k: 'index'; a: Expr; i: Expr; } | { k: 'interval'; n: Expr; unit: IntervalUnit; } | { k: 'cast'; e: Expr; type: string; } /** A ClickHouse query parameter `{name: Type}` (what `tb_secret` renders at publish). */ | { k: 'param'; name: string; type: string; }; type JoinKind = 'inner' | 'left' | 'right' | 'full' | 'cross'; type Strictness = 'all' | 'any' | 'semi' | 'anti'; export type TableSource = { kind: 'table'; name: string; } | { kind: 'sub'; query: Query; }; export type FromItem = { source: TableSource; final: boolean; alias?: string; join?: { kind: JoinKind; strictness: Strictness; on?: Expr; using?: string[]; }; }; export type OrderItem = { e: Expr; desc: boolean; nullsFirst: boolean; }; export type WithItem = { name: string; expr?: Expr; query?: Query; }; export type Select = { with: WithItem[]; distinct: boolean; items: Expr[]; from: FromItem[]; prewhere?: Expr; where?: Expr; groupBy: Expr[]; having?: Expr; orderBy: OrderItem[]; limit?: Expr; offset?: Expr; }; export type Query = { selects: Select[]; format?: string; }; /** Parse one SQL statement (a SELECT, possibly with UNION ALL and a trailing FORMAT). */ export declare function parseSql(sql: string): Query; /** A relation; `declared` marks a Data Source's own columns (their types are schema, not inference). */ export type Relation = { columns: ClickHouseColumn[]; rows: unknown[][]; declared?: boolean; }; /** Where a FROM name resolves: pipe nodes, Data Sources, CTEs. */ export interface Catalog { /** A named relation, or undefined when the name is unknown. `final` requests FINAL semantics. */ table(name: string, final: boolean): Relation | undefined; } type Source = { alias?: string; name?: string; columns: ClickHouseColumn[]; index: Map; declared?: boolean; }; type JRow = unknown[][]; type Scope = { sources: Source[]; usingKeys: Set; aliases: Map; withExprs: Map; withCache: Map; exec: Executor; subCache: Map; outer?: Scope; }; type Env = { scope: Scope; row: JRow; group?: JRow[]; aliasCache: Map; evaluating: Set; lambda?: Map; aj?: Map; }; export declare function evalExpr(e: Expr, env: Env): unknown; /** * `groupArraySample(max_size[, seed])(x)`: "Creates an array of sample argument values. The size of the resulting array * is limited to max_size elements. Argument values are selected and added to the array randomly" * (https://clickhouse.com/docs/sql-reference/aggregate-functions/reference/grouparraysample): a reservoir sample. With * no seed, ClickHouse seeds from a thread-local generator; a World must replay byte for byte, so the twin seeds from what * the World holds (R9: a stable hash of the values sampled and the size asked), and a given `seed` seeds it instead. */ /** mulberry32: a small seeded generator of uniform numbers in [0, 1), the same numbers for the same seed on every * machine. */ export declare function mulberry32(seed: number): () => number; export declare function isKnownFunction(name: string): boolean; export declare class Executor { readonly catalog: Catalog; readonly nowMs: number; readonly validating: boolean; rowsRead: number; bytesRead: number; /** `validating`: publish-time analysis — subqueries in expressions are analyzed even when no row reaches them. */ constructor(catalog: Catalog, nowMs: number, validating?: boolean); /** Resolve a FROM name: CTEs in scope first, then the catalog. */ table(name: string, final: boolean, scope?: Scope): Relation; run(q: Query, outer?: Scope): Relation; /** Run and also report rows_before_limit (for the response envelope). */ runTop(q: Query): { rel: Relation; rowsBeforeLimit?: number; }; private select; /** The analyzer pass: every expression of the SELECT resolves (see "static checking"). */ private check; private checkExpr; private orderValue; private defaultRow; private relationOf; private from; private combine; } export type QueryResult = { meta: ClickHouseColumn[]; data: Array>; rows: number; rowsBeforeLimitAtLeast?: number; rowsRead: number; bytesRead: number; }; /** Execute a parsed query against a catalog and shape Tinybird's result envelope. */ export declare function executeSql(q: Query, catalog: Catalog, nowMs: number, opts?: { validate?: boolean; }): QueryResult; /** Run a query to a Relation (for materialized views and node chaining). */ export declare function runSql(q: Query, catalog: Catalog, nowMs: number, opts?: { validate?: boolean; }): Relation; /** Every table name a query reads (FROM / JOIN / IN-subqueries), excluding its own CTEs. */ export declare function usedTables(q: Query): string[]; /** The left-most table of the first SELECT — a materialized view's trigger table. */ export declare function primaryTable(q: Query): string | undefined; export declare function queryStatistics(result: QueryResult): { elapsed: number; rows_read: number; bytes_read: number; }; /** * Render a result in one of the modeled text formats (`.json` is handled by the caller): * ClickHouse's JSONEachRow and CSV / CSVWithNames, every row (and the header) ending in `\n`. */ export declare function renderFormat(format: 'ndjson' | 'csv' | 'csvwithnames', result: QueryResult): string; export type QueryPlan = Query; /** Parse a SELECT and apply the checks ClickHouse makes before reading any data. */ export declare function parseQuery(sql: string): Query; /** Run a query over ONE table's stored rows (any FROM name resolves to it). */ export declare function executeQuery(q: Query, columns: readonly ClickHouseColumn[], rows: ReadonlyArray>, nowMs?: number): QueryResult;