@@ -416,14 +416,30 @@ function buildCustomFilteringContext(
416416
417417 if ( ! isInBy ) {
418418 statsSpecificFunctionsToIgnore . push (
419- ...getFunctionsToIgnoreForStats ( command , finalCommandArgIndex ) ,
420- ...( isAggFunctionUsedAlready ( command , finalCommandArgIndex )
421- ? getAllFunctions ( { type : FunctionDefinitionTypes . AGG } ) . map ( ( { name } ) => name )
422- : [ ] ) ,
423- ...( isTimeseriesAggUsedAlready ( command , finalCommandArgIndex )
424- ? getAllFunctions ( { type : FunctionDefinitionTypes . TIME_SERIES_AGG } ) . map ( ( { name } ) => name )
425- : [ ] )
419+ ...getFunctionsToIgnoreForStats ( command , finalCommandArgIndex )
426420 ) ;
421+
422+ // The "no nested aggregations" rule applies unless the cursor's param
423+ // explicitly expects an aggregation (e.g. SPARKLINE's first arg has
424+ // hint.kind === 'aggregation'), in which case nesting is required, not forbidden.
425+ const expectsAggregation = basicContext . paramDefinitions . some (
426+ ( p ) => p . hint ?. kind === 'aggregation'
427+ ) ;
428+
429+ if ( ! expectsAggregation ) {
430+ if ( isAggFunctionUsedAlready ( command , finalCommandArgIndex ) ) {
431+ statsSpecificFunctionsToIgnore . push (
432+ ...getAllFunctions ( { type : FunctionDefinitionTypes . AGG } ) . map ( ( { name } ) => name )
433+ ) ;
434+ }
435+ if ( isTimeseriesAggUsedAlready ( command , finalCommandArgIndex ) ) {
436+ statsSpecificFunctionsToIgnore . push (
437+ ...getAllFunctions ( { type : FunctionDefinitionTypes . TIME_SERIES_AGG } ) . map (
438+ ( { name } ) => name
439+ )
440+ ) ;
441+ }
442+ }
427443 }
428444
429445 return {
0 commit comments