@@ -11,7 +11,7 @@ import {
11
11
import { renderChartConfig } from '@/renderChartConfig' ;
12
12
import type { ChartConfig , ChartConfigWithDateRange , TSource } from '@/types' ;
13
13
14
- const DEFAULT_SAMPLE_SIZE = 1e6 ;
14
+ export const DEFAULT_MAX_ROWS_TO_READ = 1e6 ;
15
15
16
16
export class MetadataCache {
17
17
private cache = new Map < string , any > ( ) ;
@@ -268,7 +268,7 @@ export class Metadata {
268
268
query_params : sql . params ,
269
269
connectionId,
270
270
clickhouse_settings : {
271
- max_rows_to_read : DEFAULT_SAMPLE_SIZE ,
271
+ max_rows_to_read : DEFAULT_MAX_ROWS_TO_READ ,
272
272
read_overflow_mode : 'break' ,
273
273
} ,
274
274
} )
@@ -341,7 +341,7 @@ export class Metadata {
341
341
query_params : sql . params ,
342
342
connectionId,
343
343
clickhouse_settings : {
344
- max_rows_to_read : DEFAULT_SAMPLE_SIZE ,
344
+ max_rows_to_read : DEFAULT_MAX_ROWS_TO_READ ,
345
345
read_overflow_mode : 'break' ,
346
346
} ,
347
347
} )
@@ -438,36 +438,41 @@ export class Metadata {
438
438
limit ?: number ;
439
439
disableRowLimit ?: boolean ;
440
440
} ) {
441
- const sql = await renderChartConfig (
442
- {
443
- ...chartConfig ,
444
- select : keys
445
- . map ( ( k , i ) => `groupUniqArray(${ limit } )(${ k } ) AS param${ i } ` )
446
- . join ( ', ' ) ,
441
+ return this . cache . getOrFetch (
442
+ `${ chartConfig . from . databaseName } .${ keys . join ( ',' ) } .${ chartConfig . dateRange . toString ( ) } .${ disableRowLimit } .values` ,
443
+ async ( ) => {
444
+ const sql = await renderChartConfig (
445
+ {
446
+ ...chartConfig ,
447
+ select : keys
448
+ . map ( ( k , i ) => `groupUniqArray(${ limit } )(${ k } ) AS param${ i } ` )
449
+ . join ( ', ' ) ,
450
+ } ,
451
+ this ,
452
+ ) ;
453
+
454
+ const json = await this . clickhouseClient
455
+ . query < 'JSON' > ( {
456
+ query : sql . sql ,
457
+ query_params : sql . params ,
458
+ connectionId : chartConfig . connection ,
459
+ clickhouse_settings : ! disableRowLimit
460
+ ? {
461
+ max_rows_to_read : DEFAULT_MAX_ROWS_TO_READ ,
462
+ read_overflow_mode : 'break' ,
463
+ }
464
+ : undefined ,
465
+ } )
466
+ . then ( res => res . json < any > ( ) ) ;
467
+
468
+ // TODO: Fix type issues mentioned in HDX-1548. value is not acually a
469
+ // string[], sometimes it's { [key: string]: string; }
470
+ return Object . entries ( json ?. data ?. [ 0 ] ) . map ( ( [ key , value ] ) => ( {
471
+ key : keys [ parseInt ( key . replace ( 'param' , '' ) ) ] ,
472
+ value : ( value as string [ ] ) ?. filter ( Boolean ) , // remove nulls
473
+ } ) ) ;
447
474
} ,
448
- this ,
449
475
) ;
450
-
451
- const json = await this . clickhouseClient
452
- . query < 'JSON' > ( {
453
- query : sql . sql ,
454
- query_params : sql . params ,
455
- connectionId : chartConfig . connection ,
456
- clickhouse_settings : ! disableRowLimit
457
- ? {
458
- max_rows_to_read : DEFAULT_SAMPLE_SIZE ,
459
- read_overflow_mode : 'break' ,
460
- }
461
- : undefined ,
462
- } )
463
- . then ( res => res . json < any > ( ) ) ;
464
-
465
- // TODO: Fix type issues mentioned in HDX-1548. value is not acually a
466
- // string[], sometimes it's { [key: string]: string; }
467
- return Object . entries ( json . data [ 0 ] ) . map ( ( [ key , value ] ) => ( {
468
- key : keys [ parseInt ( key . replace ( 'param' , '' ) ) ] ,
469
- value : ( value as string [ ] ) ?. filter ( Boolean ) , // remove nulls
470
- } ) ) ;
471
476
}
472
477
}
473
478
0 commit comments