@@ -19,7 +19,7 @@ import {
1919 CreateSourceParams
2020} from './BucketSource.js' ;
2121import { SqlRuleError } from './errors.js' ;
22- import { GetQuerierOptions } from './index.js' ;
22+ import { BucketDataSourceDefinition , GetQuerierOptions } from './index.js' ;
2323import { SourceTableInterface } from './SourceTableInterface.js' ;
2424import { AvailableTable , SqlTools } from './sql_filters.js' ;
2525import { checkUnsupportedFeatures , isClauseError } from './sql_support.js' ;
@@ -44,6 +44,7 @@ import {
4444} from './types.js' ;
4545import { filterJsonRow , getBucketId , isJsonValue , isSelectStatement , normalizeParameterValue } from './utils.js' ;
4646import { DetectRequestParameters } from './validators.js' ;
47+ import { HydrationState , resolveHydrationState } from './HydrationState.js' ;
4748
4849export interface SqlParameterQueryOptions {
4950 sourceTable : TablePattern ;
@@ -59,6 +60,7 @@ export interface SqlParameterQueryOptions {
5960 bucketParameters : string [ ] ;
6061 queryId : string ;
6162 tools : SqlTools ;
63+ querierDataSource : BucketDataSourceDefinition ;
6264 errors ?: SqlRuleError [ ] ;
6365}
6466
@@ -75,7 +77,8 @@ export class SqlParameterQuery
7577 descriptorName : string ,
7678 sql : string ,
7779 options : QueryParseOptions ,
78- queryId : string
80+ queryId : string ,
81+ querierDataSource : BucketDataSourceDefinition
7982 ) : SqlParameterQuery | StaticSqlParameterQuery | TableValuedFunctionSqlParameterQuery {
8083 const parsed = parse ( sql , { locationTracking : true } ) ;
8184 const schema = options ?. schema ;
@@ -91,7 +94,7 @@ export class SqlParameterQuery
9194
9295 if ( q . from == null ) {
9396 // E.g. SELECT token_parameters.user_id as user_id WHERE token_parameters.is_admin
94- return StaticSqlParameterQuery . fromSql ( descriptorName , sql , q , options , queryId ) ;
97+ return StaticSqlParameterQuery . fromSql ( descriptorName , sql , q , options , queryId , querierDataSource ) ;
9598 }
9699
97100 let errors : SqlRuleError [ ] = [ ] ;
@@ -102,7 +105,15 @@ export class SqlParameterQuery
102105 throw new SqlRuleError ( 'Must SELECT from a single table' , sql , q . from ?. [ 0 ] . _location ) ;
103106 } else if ( q . from [ 0 ] . type == 'call' ) {
104107 const from = q . from [ 0 ] ;
105- return TableValuedFunctionSqlParameterQuery . fromSql ( descriptorName , sql , from , q , options , queryId ) ;
108+ return TableValuedFunctionSqlParameterQuery . fromSql (
109+ descriptorName ,
110+ sql ,
111+ from ,
112+ q ,
113+ options ,
114+ queryId ,
115+ querierDataSource
116+ ) ;
106117 } else if ( q . from [ 0 ] . type == 'statement' ) {
107118 throw new SqlRuleError ( 'Subqueries are not supported yet' , sql , q . from ?. [ 0 ] . _location ) ;
108119 }
@@ -203,6 +214,7 @@ export class SqlParameterQuery
203214 bucketParameters,
204215 queryId,
205216 tools,
217+ querierDataSource,
206218 errors
207219 } ) ;
208220
@@ -297,6 +309,8 @@ export class SqlParameterQuery
297309 readonly queryId : string ;
298310 readonly tools : SqlTools ;
299311
312+ readonly querierDataSource : BucketDataSourceDefinition ;
313+
300314 readonly errors : SqlRuleError [ ] ;
301315
302316 constructor ( options : SqlParameterQueryOptions ) {
@@ -314,6 +328,7 @@ export class SqlParameterQuery
314328 this . queryId = options . queryId ;
315329 this . tools = options . tools ;
316330 this . errors = options . errors ?? [ ] ;
331+ this . querierDataSource = options . querierDataSource ;
317332 }
318333
319334 public get defaultLookupName ( ) : string {
@@ -333,15 +348,18 @@ export class SqlParameterQuery
333348 }
334349
335350 createParameterQuerierSource ( params : CreateSourceParams ) : BucketParameterQuerierSource {
351+ const hydrationState = resolveHydrationState ( params ) ;
352+ const bucketPrefix = hydrationState . getBucketSourceState ( this . querierDataSource ) . bucketPrefix ;
336353 return {
337354 pushBucketParameterQueriers : ( result : PendingQueriers , options : GetQuerierOptions ) => {
338- const q = this . getBucketParameterQuerier ( options . globalParameters , [ 'default' ] , params . bucketIdTransformer ) ;
355+ const q = this . getBucketParameterQuerier ( options . globalParameters , [ 'default' ] , bucketPrefix ) ;
339356 result . queriers . push ( q ) ;
340357 }
341358 } ;
342359 }
343360
344361 createParameterLookupSource ( params : CreateSourceParams ) : BucketParameterLookupSource {
362+ // FIXME: Use HydrationState for lookups.
345363 return {
346364 evaluateParameterRow : ( sourceTable : SourceTableInterface , row : SqliteRow ) : EvaluatedParametersResult [ ] => {
347365 if ( this . tableSyncsParameters ( sourceTable ) ) {
@@ -403,7 +421,7 @@ export class SqlParameterQuery
403421 resolveBucketDescriptions (
404422 bucketParameters : SqliteJsonRow [ ] ,
405423 parameters : RequestParameters ,
406- transformer : BucketIdTransformer
424+ bucketPrefix : string
407425 ) : BucketDescription [ ] {
408426 // Filters have already been applied and gotten us the set of bucketParameters - don't attempt to filter again.
409427 // We _do_ need to evaluate the output columns here, using a combination of precomputed bucketParameters,
@@ -428,7 +446,7 @@ export class SqlParameterQuery
428446 }
429447
430448 return {
431- bucket : getBucketId ( this . descriptorName , this . bucketParameters , result , transformer ) ,
449+ bucket : getBucketId ( bucketPrefix , this . bucketParameters , result ) ,
432450 priority : this . priority
433451 } ;
434452 } )
@@ -514,7 +532,7 @@ export class SqlParameterQuery
514532 getBucketParameterQuerier (
515533 requestParameters : RequestParameters ,
516534 reasons : BucketInclusionReason [ ] ,
517- transformer : BucketIdTransformer
535+ bucketPrefix : string
518536 ) : BucketParameterQuerier {
519537 const lookups = this . getLookups ( requestParameters ) ;
520538 if ( lookups . length == 0 ) {
@@ -534,7 +552,7 @@ export class SqlParameterQuery
534552 parameterQueryLookups : lookups ,
535553 queryDynamicBucketDescriptions : async ( source : ParameterLookupSource ) => {
536554 const bucketParameters = await source . getParameterSets ( lookups ) ;
537- return this . resolveBucketDescriptions ( bucketParameters , requestParameters , transformer ) . map ( ( bucket ) => ( {
555+ return this . resolveBucketDescriptions ( bucketParameters , requestParameters , bucketPrefix ) . map ( ( bucket ) => ( {
538556 ...bucket ,
539557 definition : this . descriptorName ,
540558 inclusion_reasons : reasons
0 commit comments