11import R from 'ramda' ;
2+ import crypto from 'crypto' ;
23import { getEnv , MaybeCancelablePromise } from '@cubejs-backend/shared' ;
34import {
45 cancelCombinator ,
@@ -33,7 +34,7 @@ type IndexesSql = { sql: [string, unknown[]], indexName: string }[];
3334type QueryKey = [ QueryWithParams , IndexesSql , InvalidationKeys ] | [ QueryWithParams , InvalidationKeys ] ;
3435
3536type QueryOptions = {
36- queryKey : QueryKey ;
37+ queryKeyMd5 : QueryKey | string ;
3738 newVersionEntry : VersionEntry ;
3839 query : string ;
3940 values : unknown [ ] ;
@@ -47,6 +48,29 @@ function nowTimestamp(client: DriverInterface) {
4748 return client . nowTimestamp ?.( ) ?? new Date ( ) . getTime ( ) ;
4849}
4950
51+ /**
52+ * Computes MD5 hash of the query key.
53+ * Special handling for FETCH_TABLES_FOR queries - returns the original string instead of hash.
54+ */
55+ function queryKeyMd5 ( queryKey : QueryKey ) : string {
56+ const jsonStr = JSON . stringify ( queryKey ) ;
57+
58+ // Special case: if query contains FETCH_TABLES_FOR, return the string representation
59+ if ( jsonStr . includes ( 'FETCH_TABLES_FOR' ) ) {
60+ // Extract the SQL string from the query key if it's a string
61+ if ( Array . isArray ( queryKey ) && queryKey . length > 0 ) {
62+ const firstElement = queryKey [ 0 ] ;
63+ if ( Array . isArray ( firstElement ) && typeof firstElement [ 0 ] === 'string' ) {
64+ return firstElement [ 0 ] ;
65+ }
66+ }
67+ return jsonStr ;
68+ }
69+
70+ // Otherwise, return MD5 hash
71+ return crypto . createHash ( 'md5' ) . update ( jsonStr ) . digest ( 'hex' ) ;
72+ }
73+
5074export class PreAggregationLoader {
5175 private preAggregations : PreAggregations ;
5276
@@ -482,7 +506,7 @@ export class PreAggregationLoader {
482506
483507 protected queryOptions ( invalidationKeys : InvalidationKeys , query : string , params : unknown [ ] , targetTableName : string , newVersionEntry : VersionEntry ) {
484508 return {
485- queryKey : this . preAggregationQueryKey ( invalidationKeys ) ,
509+ queryKeyMd5 : queryKeyMd5 ( this . preAggregationQueryKey ( invalidationKeys ) ) ,
486510 query,
487511 values : params ,
488512 targetTableName,
0 commit comments