@@ -17,6 +17,9 @@ import {
1717import type { ClickhouseReader , QueryStats } from "./types.js" ;
1818import { QueryError } from "./errors.js" ;
1919import type { OutputColumnMetadata } from "@internal/tsql" ;
20+ import { Logger } from "@trigger.dev/core/logger" ;
21+
22+ const logger = new Logger ( "tsql" , "info" ) ;
2023
2124export type { QueryStats } ;
2225
@@ -105,6 +108,9 @@ export async function executeTSQL<TOut extends z.ZodSchema>(
105108) : Promise < TSQLQueryResult < z . output < TOut > > > {
106109 const shouldTransformValues = options . transformValues ?? true ;
107110
111+ let generatedSql : string | undefined ;
112+ let generatedParams : Record < string , unknown > | undefined ;
113+
108114 try {
109115 // 1. Compile the TSQL query to ClickHouse SQL
110116 const { sql, params, columns } = compileTSQL ( options . query , {
@@ -116,6 +122,9 @@ export async function executeTSQL<TOut extends z.ZodSchema>(
116122 fieldMappings : options . fieldMappings ,
117123 } ) ;
118124
125+ generatedSql = sql ;
126+ generatedParams = params ;
127+
119128 // 2. Execute the query with stats
120129 const queryFn = reader . queryWithStats ( {
121130 name : options . name ,
@@ -145,6 +154,17 @@ export async function executeTSQL<TOut extends z.ZodSchema>(
145154
146155 return [ null , { rows : rows ?? [ ] , columns, stats } ] ;
147156 } catch ( error ) {
157+ const errorMessage = error instanceof Error ? error . message : "Unknown error" ;
158+
159+ // Log TSQL compilation or unexpected errors
160+ logger . error ( "[TSQL] Query error" , {
161+ name : options . name ,
162+ error : errorMessage ,
163+ tsql : options . query ,
164+ generatedSql : generatedSql ?? "(compilation failed)" ,
165+ generatedParams : generatedParams ?? { } ,
166+ } ) ;
167+
148168 if ( error instanceof Error ) {
149169 return [ new QueryError ( error . message , { query : options . query } ) , null ] ;
150170 }
0 commit comments