11// TSQL - Type-Safe SQL Query Language for ClickHouse
22// Originally derived from PostHog's HogQL (see NOTICE.md for attribution)
33
4- import { CharStreams , CommonTokenStream } from "antlr4ts" ;
54import type { ANTLRErrorListener , RecognitionException , Recognizer } from "antlr4ts" ;
5+ import { CharStreams , CommonTokenStream } from "antlr4ts" ;
66import type { Token } from "antlr4ts/Token" ;
77import { TSQLLexer } from "./grammar/TSQLLexer.js" ;
88import { TSQLParser } from "./grammar/TSQLParser.js" ;
9+ import type { Expression , SelectQuery , SelectSetQuery } from "./query/ast.js" ;
10+ import { SyntaxError as TSQLSyntaxError } from "./query/errors.js" ;
911import { TSQLParseTreeConverter } from "./query/parser.js" ;
10- import type { SelectQuery , SelectSetQuery , Expression } from "./query/ast.js" ;
11- import { SyntaxError } from "./query/errors.js" ;
12- import {
13- createSchemaRegistry ,
14- type TableSchema ,
15- type FieldMappings ,
16- type RequiredFilter ,
17- } from "./query/schema.js" ;
18- import { createPrinterContext , type QuerySettings } from "./query/printer_context.js" ;
1912import { printToClickHouse , type PrintResult } from "./query/printer.js" ;
13+ import { createPrinterContext , type QuerySettings } from "./query/printer_context.js" ;
14+ import { createSchemaRegistry , type FieldMappings , type TableSchema } from "./query/schema.js" ;
2015
2116/**
2217 * Simple error listener that captures syntax errors
@@ -45,65 +40,65 @@ export * from "./query/errors.js";
4540// Re-export escape utilities
4641export {
4742 escapeClickHouseIdentifier ,
48- escapeTSQLIdentifier ,
4943 escapeClickHouseString ,
44+ escapeTSQLIdentifier ,
5045 escapeTSQLString ,
5146 getClickHouseType ,
5247} from "./query/escape.js" ;
5348
5449// Re-export function definitions
5550export {
56- TSQL_CLICKHOUSE_FUNCTIONS ,
57- TSQL_AGGREGATIONS ,
58- TSQL_COMPARISON_MAPPING ,
5951 findTSQLAggregation ,
6052 findTSQLFunction ,
6153 getAllExposedFunctionNames ,
54+ TSQL_AGGREGATIONS ,
55+ TSQL_CLICKHOUSE_FUNCTIONS ,
56+ TSQL_COMPARISON_MAPPING ,
6257 type TSQLFunctionMeta ,
6358} from "./query/functions.js" ;
6459
6560// Re-export schema types and functions
6661export {
67- type TableSchema ,
68- type ColumnSchema ,
69- type TenantColumnConfig ,
70- type RequiredFilter ,
71- type SchemaRegistry ,
72- type ClickHouseType ,
73- type OutputColumnMetadata ,
74- type FieldMappings ,
62+ column ,
7563 createSchemaRegistry ,
76- findTable ,
7764 findColumn ,
78- validateTable ,
79- validateSelectColumn ,
80- validateFilterColumn ,
81- validateSortColumn ,
82- validateGroupColumn ,
83- column ,
65+ findTable ,
66+ getAllowedUserValues ,
67+ getExternalValue ,
68+ getInternalValue ,
69+ getInternalValueFromMapping ,
70+ getInternalValueFromMappingCaseInsensitive ,
8471 // Value mapping utilities
8572 getUserFriendlyValue ,
86- getInternalValue ,
87- getAllowedUserValues ,
88- isValidUserValue ,
89- // Virtual column utilities
90- isVirtualColumn ,
9173 getVirtualColumnExpression ,
9274 // Field mapping utilities (runtime dynamic mappings)
9375 hasFieldMapping ,
94- getExternalValue ,
95- getInternalValueFromMapping ,
96- getInternalValueFromMappingCaseInsensitive ,
76+ isValidUserValue ,
77+ // Virtual column utilities
78+ isVirtualColumn ,
79+ validateFilterColumn ,
80+ validateGroupColumn ,
81+ validateSelectColumn ,
82+ validateSortColumn ,
83+ validateTable ,
84+ type ClickHouseType ,
85+ type ColumnSchema ,
86+ type FieldMappings ,
87+ type OutputColumnMetadata ,
88+ type RequiredFilter ,
89+ type SchemaRegistry ,
90+ type TableSchema ,
91+ type TenantColumnConfig ,
9792} from "./query/schema.js" ;
9893
9994// Re-export printer context
10095export {
101- PrinterContext ,
10296 createPrinterContext ,
97+ DEFAULT_QUERY_SETTINGS ,
98+ PrinterContext ,
10399 type PrinterContextOptions ,
104- type QuerySettings ,
105100 type QueryNotice ,
106- DEFAULT_QUERY_SETTINGS ,
101+ type QuerySettings ,
107102} from "./query/printer_context.js" ;
108103
109104// Re-export printer
@@ -115,15 +110,15 @@ export { TSQLParseTreeConverter } from "./query/parser.js";
115110// Re-export validator
116111export {
117112 validateQuery ,
118- type ValidationResult ,
119113 type ValidationIssue ,
114+ type ValidationResult ,
120115 type ValidationSeverity ,
121116} from "./query/validator.js" ;
122117
123118// Re-export result transformation utilities
124119export {
125- transformResults ,
126120 createResultTransformer ,
121+ transformResults ,
127122 type TransformResultsOptions ,
128123} from "./query/results.js" ;
129124
@@ -132,7 +127,7 @@ export {
132127 *
133128 * @param query - The TSQL query string to parse
134129 * @returns The parsed AST (SelectQuery or SelectSetQuery)
135- * @throws SyntaxError if the query is invalid
130+ * @throws TSQLSyntaxError if the query is invalid
136131 *
137132 * @example
138133 * ```typescript
@@ -153,19 +148,19 @@ export function parseTSQLSelect(query: string): SelectQuery | SelectSetQuery {
153148 const parseTree = parser . select ( ) ;
154149
155150 if ( errorListener . error ) {
156- throw new SyntaxError ( errorListener . error ) ;
151+ throw new TSQLSyntaxError ( errorListener . error ) ;
157152 }
158153
159154 const converter = new TSQLParseTreeConverter ( ) ;
160155 const ast = converter . visit ( parseTree ) ;
161156
162157 // Validate the result is a select query
163158 if ( typeof ast === "string" || ! ( "expression_type" in ast ) ) {
164- throw new SyntaxError ( "Failed to parse SELECT query" ) ;
159+ throw new TSQLSyntaxError ( "Failed to parse SELECT query" ) ;
165160 }
166161
167162 if ( ast . expression_type !== "select_query" && ast . expression_type !== "select_set_query" ) {
168- throw new SyntaxError ( `Expected SELECT query, got ${ ast . expression_type } ` ) ;
163+ throw new TSQLSyntaxError ( `Expected SELECT query, got ${ ast . expression_type } ` ) ;
169164 }
170165
171166 return ast as SelectQuery | SelectSetQuery ;
@@ -176,7 +171,7 @@ export function parseTSQLSelect(query: string): SelectQuery | SelectSetQuery {
176171 *
177172 * @param expr - The TSQL expression string to parse
178173 * @returns The parsed expression AST
179- * @throws SyntaxError if the expression is invalid
174+ * @throws TSQLSyntaxError if the expression is invalid
180175 *
181176 * @example
182177 * ```typescript
@@ -197,7 +192,7 @@ export function parseTSQLExpr(expr: string): Expression {
197192 const parseTree = parser . columnExpr ( 0 ) ;
198193
199194 if ( errorListener . error ) {
200- throw new SyntaxError ( errorListener . error ) ;
195+ throw new TSQLSyntaxError ( errorListener . error ) ;
201196 }
202197
203198 const converter = new TSQLParseTreeConverter ( ) ;
@@ -244,7 +239,7 @@ export interface CompileTSQLOptions {
244239 * @param query - The TSQL query string to compile
245240 * @param options - Compilation options including tenant IDs and schema
246241 * @returns The compiled SQL and parameters
247- * @throws SyntaxError if the query is invalid
242+ * @throws TSQLSyntaxError if the query is invalid
248243 * @throws QueryError if tables/columns are not allowed
249244 *
250245 * @example
0 commit comments