@@ -21,14 +21,14 @@ import {
2121 TableStructure ,
2222 UnloadOptions ,
2323} from '@cubejs-backend/base-driver' ;
24+
25+ import { ClickHouseClient , createClient } from '@clickhouse/client' ;
2426import genericPool , { Pool } from 'generic-pool' ;
2527import { v4 as uuidv4 } from 'uuid' ;
2628import sqlstring from 'sqlstring' ;
2729
2830import { HydrationStream , transformRow } from './HydrationStream' ;
2931
30- const ClickHouse = require ( '@cubejs-backend/apla-clickhouse' ) ;
31-
3232const ClickhouseTypeToGeneric : Record < string , string > = {
3333 enum : 'text' ,
3434 string : 'text' ,
@@ -86,7 +86,7 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
8686 return 5 ;
8787 }
8888
89- protected readonly pool : Pool < any > ;
89+ protected readonly pool : Pool < ClickHouseClient > ;
9090
9191 protected readonly readOnlyMode : boolean ;
9292
@@ -122,6 +122,7 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
122122 config . dataSource ||
123123 assertDataSource ( 'default' ) ;
124124
125+ // TODO recheck everything in config for new driver
125126 this . config = {
126127 host : getEnv ( 'dbHost' , { dataSource } ) ,
127128 port : getEnv ( 'dbPort' , { dataSource } ) ,
@@ -149,9 +150,17 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
149150 getEnv ( 'clickhouseReadOnly' , { dataSource } ) === 'true' ;
150151
151152 this . pool = genericPool . createPool ( {
152- create : async ( ) => new ClickHouse ( {
153+ create : async ( ) => createClient ( {
153154 ...this . config ,
154- queryOptions : {
155+
156+ // TODO proper value here, with proper back compat
157+ url : `https://${ this . config . host } :${ this . config . port } ` ,
158+ username : getEnv ( 'dbUser' , { dataSource } ) ,
159+ password : getEnv ( 'dbPass' , { dataSource } ) ,
160+
161+ database : this . config . queryOptions . database ,
162+ session_id : uuidv4 ( ) ,
163+ clickhouse_settings : {
155164 //
156165 //
157166 // If ClickHouse user's permissions are restricted with "readonly = 1",
@@ -160,9 +169,7 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
160169 //
161170 //
162171 ...( this . readOnlyMode ? { } : { join_use_nulls : 1 } ) ,
163- session_id : uuidv4 ( ) ,
164- ...this . config . queryOptions ,
165- }
172+ } ,
166173 } ) ,
167174 destroy : ( ) => Promise . resolve ( )
168175 } , {
@@ -178,34 +185,31 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
178185 } ) ;
179186 }
180187
181- protected withConnection ( fn : ( con : any , queryId : string ) => Promise < any > ) {
188+ protected withConnection ( fn : ( con : ClickHouseClient , queryId : string ) => Promise < any > ) {
182189 const self = this ;
183190 const connectionPromise = this . pool . acquire ( ) ;
184191 const queryId = uuidv4 ( ) ;
185192
186193 let cancelled = false ;
187194 const cancelObj : any = { } ;
188195
189- const promise : any = connectionPromise . then ( ( connection : any ) => {
196+ const promise : any = connectionPromise . then ( ( connection : ClickHouseClient ) => {
190197 cancelObj . cancel = async ( ) => {
191198 cancelled = true ;
192199 await self . withConnection ( async conn => {
193- await conn . querying ( `KILL QUERY WHERE query_id = '${ queryId } '` ) ;
200+ await conn . command ( {
201+ query : `KILL QUERY WHERE query_id = '${ queryId } '` ,
202+ } ) ;
194203 } ) ;
195204 } ;
196205 return fn ( connection , queryId )
197- . then ( res => this . pool . release ( connection ) . then ( ( ) => {
206+ . finally ( ( ) => this . pool . release ( connection ) )
207+ . then ( ( res ) => {
198208 if ( cancelled ) {
199209 throw new Error ( 'Query cancelled' ) ;
200210 }
201211 return res ;
202- } ) )
203- . catch ( ( err ) => this . pool . release ( connection ) . then ( ( ) => {
204- if ( cancelled ) {
205- throw new Error ( 'Query cancelled' ) ;
206- }
207- throw err ;
208- } ) ) ;
212+ } ) ;
209213 } ) ;
210214 promise . cancel = ( ) => cancelObj . cancel ( ) ;
211215
@@ -229,10 +233,10 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
229233 protected queryResponse ( query : string , values : unknown [ ] ) {
230234 const formattedQuery = sqlstring . format ( query , values ) ;
231235
232- return this . withConnection ( ( connection , queryId ) => connection . querying ( formattedQuery , {
233- dataObjects : true ,
234- queryOptions : {
235- query_id : queryId ,
236+ return this . withConnection ( ( connection , queryId ) => connection . query ( {
237+ query : formattedQuery ,
238+ query_id : queryId ,
239+ clickhouse_settings : {
236240 //
237241 //
238242 // If ClickHouse user's permissions are restricted with "readonly = 1",
@@ -241,7 +245,7 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
241245 //
242246 //
243247 ...( this . readOnlyMode ? { } : { join_use_nulls : 1 } ) ,
244- }
248+ } ,
245249 } ) ) ;
246250 }
247251
0 commit comments