@@ -233,66 +233,33 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
233233 }
234234
235235 protected queryResponse ( query : string , values : unknown [ ] ) : Promise < ResponseJSON < Record < string , unknown > > > {
236- const queryResponseCompat = process . env . CUBE_CLICKHOUSE_QUERY_COMPAT === 'true' ;
237- const isInsert = query . trim ( ) . match ( / ^ I N S E R T / i) ;
238-
239236 console . log ( 'ClickHouse queryResponse call' , query ) ;
240237
241238 const formattedQuery = sqlstring . format ( query , values ) ;
242239
243240 console . log ( 'ClickHouse queryResponse prepared' , formattedQuery ) ;
244241
245- return this . withCancel ( async ( connection , queryId ) => {
246- // if (formattedQuery.startsWith("CREATE TABLE")) {
247- //
248- // }
249-
250- if ( queryResponseCompat && isInsert ) {
251- // TODO why do we even have inserts coming to query? is it accepted contract, or just tests misbehaviour?
252- // INSERT queries does not work with `query` method due to query preparation (like adding FORMAT clause)
253- // And `insert` wants to construct query on its own
254- await connection . command ( {
255- query : formattedQuery ,
256- query_id : queryId ,
257- clickhouse_settings : this . config . clickhouseSettings ,
258- } ) ;
259-
260- return {
261- data : [ ] ,
262- } ;
263- }
242+ return this . withCancel ( async ( connection , queryId , signal ) => {
243+ const format = 'JSON' ;
264244
265245 const resultSet = await connection . query ( {
266246 query : formattedQuery ,
267247 query_id : queryId ,
268- format : 'JSON' ,
248+ format,
269249 clickhouse_settings : this . config . clickhouseSettings ,
270250 abort_signal : signal ,
271251 } ) ;
272252 console . log ( "queryResponse resultSet" , query , resultSet . query_id , resultSet . response_headers ) ;
273253
274- if ( queryResponseCompat && resultSet . response_headers [ 'x-clickhouse-format' ] !== 'JSON' ) {
275- // TODO why do we even have CREATE TABLE coming to query? is it accepted contract, or just tests misbehaviour?
276- // Result set for query like CREATE TABLE would be empty, and would not even send x-clickhouse-format header field
277- // Parsing response like that as JSON would fail
278- // CREATE TABLE response header fields:
279- // 'content-type': 'text/plain; charset=UTF-8',
280- // SELECT response header fields:
281- // 'content-type': 'application/json; charset=UTF-8',
282- // 'x-clickhouse-format': 'JSON',
283- const results = await resultSet . text ( ) ;
284- console . log ( "queryResponse text results" , query , results ) ;
285- console . log ( "queryResponse text results JSON" , JSON . stringify ( results ) ) ;
286- return {
287- data : [ ] ,
288- } ;
289- } else {
290- // We used format JSON, so we expect each row to be Record with column names as keys
291- const results = await resultSet . json < Record < string , unknown > > ( ) ;
292- console . log ( "queryResponse json results" , query , results ) ;
293- console . log ( "queryResponse json results JSON" , JSON . stringify ( results ) ) ;
294- return results ;
254+ if ( resultSet . response_headers [ 'x-clickhouse-format' ] !== format ) {
255+ throw new Error ( `Unexpected x-clickhouse-format in response: expected ${ format } , received ${ resultSet . response_headers [ 'x-clickhouse-format' ] } ` ) ;
295256 }
257+
258+ // We used format JSON, so we expect each row to be Record with column names as keys
259+ const results = await resultSet . json < Record < string , unknown > > ( ) ;
260+ console . log ( "queryResponse json results" , query , results ) ;
261+ console . log ( "queryResponse json results JSON" , JSON . stringify ( results ) ) ;
262+ return results ;
296263 } ) ;
297264 }
298265
@@ -376,12 +343,19 @@ export class ClickHouseDriver extends BaseDriver implements DriverInterface {
376343
377344 console . log ( 'ClickHouse stream prepared' , formattedQuery ) ;
378345
346+ const format = 'JSONCompactEachRowWithNamesAndTypes' ;
347+
379348 const resultSet = await client . query ( {
380349 query : formattedQuery ,
381350 query_id : uuidv4 ( ) ,
382- format : 'JSONCompactEachRowWithNamesAndTypes' ,
351+ format,
383352 clickhouse_settings : this . config . clickhouseSettings ,
384353 } ) ;
354+
355+ if ( resultSet . response_headers [ 'x-clickhouse-format' ] !== format ) {
356+ throw new Error ( `Unexpected x-clickhouse-format in response: expected ${ format } , received ${ resultSet . response_headers [ 'x-clickhouse-format' ] } ` ) ;
357+ }
358+
385359 // Array<unknown> is okay, because we use fixed JSONCompactEachRowWithNamesAndTypes format
386360 // And each row after first two will look like this: [42, "hello", [0,1]]
387361 // https://clickhouse.com/docs/en/interfaces/formats#jsoncompacteachrowwithnamesandtypes
0 commit comments