@@ -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,18 +122,29 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
122122 config . dataSource ||
123123 assertDataSource ( 'default' ) ;
124124
125+ // TODO recheck everything in config for new driver
126+ const host = getEnv ( 'dbHost' , { dataSource } ) ;
127+ const port = getEnv ( 'dbPort' , { dataSource } ) ;
128+ // TODO proper value here, with proper back compat, and treating protocol
129+ const url = `https://${ host } :${ port } ` ;
130+
131+ const username = getEnv ( 'dbUser' , { dataSource } ) ;
132+ const password = getEnv ( 'dbPass' , { dataSource } ) ;
125133 this . config = {
126- host : getEnv ( 'dbHost' , { dataSource } ) ,
127- port : getEnv ( 'dbPort' , { dataSource } ) ,
128- auth :
129- getEnv ( 'dbUser' , { dataSource } ) ||
130- getEnv ( 'dbPass' , { dataSource } )
131- ? `${
132- getEnv ( 'dbUser' , { dataSource } )
133- } :${
134- getEnv ( 'dbPass' , { dataSource } )
135- } `
136- : '' ,
134+ // host: getEnv('dbHost', { dataSource }),
135+ // port: getEnv('dbPort', { dataSource }),
136+ url,
137+ // auth:
138+ // getEnv('dbUser', { dataSource }) ||
139+ // getEnv('dbPass', { dataSource })
140+ // ? `${
141+ // getEnv('dbUser', { dataSource })
142+ // }:${
143+ // getEnv('dbPass', { dataSource })
144+ // }`
145+ // : '',
146+ username,
147+ password,
137148 protocol : getEnv ( 'dbSsl' , { dataSource } ) ? 'https:' : 'http:' ,
138149 queryOptions : {
139150 database :
@@ -149,9 +160,15 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
149160 getEnv ( 'clickhouseReadOnly' , { dataSource } ) === 'true' ;
150161
151162 this . pool = genericPool . createPool ( {
152- create : async ( ) => new ClickHouse ( {
163+ create : async ( ) => createClient ( {
153164 ...this . config ,
154- queryOptions : {
165+
166+ username : getEnv ( 'dbUser' , { dataSource } ) ,
167+ password : getEnv ( 'dbPass' , { dataSource } ) ,
168+
169+ database : this . config . queryOptions . database ,
170+ session_id : uuidv4 ( ) ,
171+ clickhouse_settings : {
155172 //
156173 //
157174 // If ClickHouse user's permissions are restricted with "readonly = 1",
@@ -160,9 +177,7 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
160177 //
161178 //
162179 ...( this . readOnlyMode ? { } : { join_use_nulls : 1 } ) ,
163- session_id : uuidv4 ( ) ,
164- ...this . config . queryOptions ,
165- }
180+ } ,
166181 } ) ,
167182 destroy : ( ) => Promise . resolve ( )
168183 } , {
@@ -178,34 +193,31 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
178193 } ) ;
179194 }
180195
181- protected withConnection ( fn : ( con : any , queryId : string ) => Promise < any > ) {
196+ protected withConnection ( fn : ( con : ClickHouseClient , queryId : string ) => Promise < any > ) {
182197 const self = this ;
183198 const connectionPromise = this . pool . acquire ( ) ;
184199 const queryId = uuidv4 ( ) ;
185200
186201 let cancelled = false ;
187202 const cancelObj : any = { } ;
188203
189- const promise : any = connectionPromise . then ( ( connection : any ) => {
204+ const promise : any = connectionPromise . then ( ( connection : ClickHouseClient ) => {
190205 cancelObj . cancel = async ( ) => {
191206 cancelled = true ;
192207 await self . withConnection ( async conn => {
193- await conn . querying ( `KILL QUERY WHERE query_id = '${ queryId } '` ) ;
208+ await conn . command ( {
209+ query : `KILL QUERY WHERE query_id = '${ queryId } '` ,
210+ } ) ;
194211 } ) ;
195212 } ;
196213 return fn ( connection , queryId )
197- . then ( res => this . pool . release ( connection ) . then ( ( ) => {
214+ . finally ( ( ) => this . pool . release ( connection ) )
215+ . then ( ( res ) => {
198216 if ( cancelled ) {
199217 throw new Error ( 'Query cancelled' ) ;
200218 }
201219 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- } ) ) ;
220+ } ) ;
209221 } ) ;
210222 promise . cancel = ( ) => cancelObj . cancel ( ) ;
211223
@@ -229,10 +241,10 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
229241 protected queryResponse ( query : string , values : unknown [ ] ) {
230242 const formattedQuery = sqlstring . format ( query , values ) ;
231243
232- return this . withConnection ( ( connection , queryId ) => connection . querying ( formattedQuery , {
233- dataObjects : true ,
234- queryOptions : {
235- query_id : queryId ,
244+ return this . withConnection ( ( connection , queryId ) => connection . query ( {
245+ query : formattedQuery ,
246+ query_id : queryId ,
247+ clickhouse_settings : {
236248 //
237249 //
238250 // If ClickHouse user's permissions are restricted with "readonly = 1",
@@ -241,7 +253,7 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
241253 //
242254 //
243255 ...( this . readOnlyMode ? { } : { join_use_nulls : 1 } ) ,
244- }
256+ } ,
245257 } ) ) ;
246258 }
247259
0 commit comments