@@ -6,9 +6,11 @@ import {
66 createClient ,
77 type ResultSet ,
88 type Row ,
9+ type BaseQueryParams ,
10+ type InsertResult ,
911} from "@clickhouse/client" ;
1012import { recordSpanError , Span , startSpan , trace , Tracer } from "@internal/tracing" ;
11- import { flattenAttributes , tryCatch } from "@trigger.dev/core/v3" ;
13+ import { flattenAttributes , tryCatch , type Result } from "@trigger.dev/core/v3" ;
1214import { z } from "zod" ;
1315import { InsertError , QueryError } from "./errors.js" ;
1416import type {
@@ -797,6 +799,105 @@ export class ClickhouseClient implements ClickhouseReader, ClickhouseWriter {
797799 } ) ;
798800 } ;
799801 }
802+
803+ public insertCompactRaw ( req : {
804+ name : string ;
805+ table : string ;
806+ columns : readonly string [ ] ;
807+ settings ?: ClickHouseSettings ;
808+ } ) : (
809+ events : readonly any [ ] [ ] | any [ ] ,
810+ options ?: {
811+ attributes ?: Record < string , string | number | boolean > ;
812+ params ?: BaseQueryParams ;
813+ }
814+ ) => Promise < Result < InsertResult , InsertError > > {
815+ return async ( events , options ) => {
816+ const queryId = randomUUID ( ) ;
817+
818+ return await startSpan ( this . tracer , "insert" , async ( span ) => {
819+ // Check if events is a single row (array) or multiple rows (array of arrays)
820+ // If first element is not an array, treat as single row
821+ const isSingleRow = events . length > 0 && ! Array . isArray ( events [ 0 ] ) ;
822+ const eventsArray : readonly any [ ] [ ] = isSingleRow
823+ ? [ events as any [ ] ]
824+ : ( events as readonly any [ ] [ ] ) ;
825+
826+ this . logger . debug ( "Inserting into clickhouse (compact raw)" , {
827+ clientName : this . name ,
828+ name : req . name ,
829+ table : req . table ,
830+ events : eventsArray . length ,
831+ settings : req . settings ,
832+ attributes : options ?. attributes ,
833+ options,
834+ queryId,
835+ } ) ;
836+
837+ span . setAttributes ( {
838+ "clickhouse.clientName" : this . name ,
839+ "clickhouse.tableName" : req . table ,
840+ "clickhouse.operationName" : req . name ,
841+ "clickhouse.queryId" : queryId ,
842+ "clickhouse.format" : "JSONCompactEachRowWithNames" ,
843+ ...flattenAttributes ( req . settings , "clickhouse.settings" ) ,
844+ ...flattenAttributes ( options ?. attributes ) ,
845+ } ) ;
846+
847+ // Build compact format: [columns, ...rows]
848+ // Data is already in array format, no conversion needed
849+ const compactData : any [ ] = [ Array . from ( req . columns ) , ...eventsArray ] ;
850+
851+ const [ clickhouseError , result ] = await tryCatch (
852+ this . client . insert ( {
853+ table : req . table ,
854+ format : "JSONCompactEachRowWithNames" ,
855+ values : compactData ,
856+ query_id : queryId ,
857+ ...options ?. params ,
858+ clickhouse_settings : {
859+ ...req . settings ,
860+ ...options ?. params ?. clickhouse_settings ,
861+ } ,
862+ } )
863+ ) ;
864+
865+ if ( clickhouseError ) {
866+ this . logger . error ( "Error inserting into clickhouse" , {
867+ name : req . name ,
868+ error : clickhouseError ,
869+ table : req . table ,
870+ } ) ;
871+
872+ recordClickhouseError ( span , clickhouseError ) ;
873+ return [ new InsertError ( clickhouseError . message ) , null ] ;
874+ }
875+
876+ this . logger . debug ( "Inserted into clickhouse" , {
877+ clientName : this . name ,
878+ name : req . name ,
879+ table : req . table ,
880+ result,
881+ queryId,
882+ } ) ;
883+
884+ span . setAttributes ( {
885+ "clickhouse.query_id" : result . query_id ,
886+ "clickhouse.executed" : result . executed ,
887+ "clickhouse.summary.read_rows" : result . summary ?. read_rows ,
888+ "clickhouse.summary.read_bytes" : result . summary ?. read_bytes ,
889+ "clickhouse.summary.written_rows" : result . summary ?. written_rows ,
890+ "clickhouse.summary.written_bytes" : result . summary ?. written_bytes ,
891+ "clickhouse.summary.total_rows_to_read" : result . summary ?. total_rows_to_read ,
892+ "clickhouse.summary.result_rows" : result . summary ?. result_rows ,
893+ "clickhouse.summary.result_bytes" : result . summary ?. result_bytes ,
894+ "clickhouse.summary.elapsed_ns" : result . summary ?. elapsed_ns ,
895+ } ) ;
896+
897+ return [ null , result ] ;
898+ } ) ;
899+ } ;
900+ }
800901}
801902
802903function recordClickhouseError ( span : Span , error : Error ) {
0 commit comments