@@ -55,14 +55,17 @@ private sealed class InitializationParameters
5555
5656 public int MaxConcurrency { get ; }
5757
58+ public bool NonStreamingOrderBy { get ; }
59+
5860 public InitializationParameters (
5961 IDocumentContainer documentContainer ,
6062 SqlQuerySpec sqlQuerySpec ,
6163 IReadOnlyList < FeedRangeEpk > targetRanges ,
6264 PartitionKey ? partitionKey ,
6365 IReadOnlyList < OrderByColumn > orderByColumns ,
6466 QueryPaginationOptions queryPaginationOptions ,
65- int maxConcurrency )
67+ int maxConcurrency ,
68+ bool nonStreamingOrderBy )
6669 {
6770 this . DocumentContainer = documentContainer ?? throw new ArgumentNullException ( nameof ( documentContainer ) ) ;
6871 this . SqlQuerySpec = sqlQuerySpec ?? throw new ArgumentNullException ( nameof ( sqlQuerySpec ) ) ;
@@ -71,6 +74,7 @@ public InitializationParameters(
7174 this . OrderByColumns = orderByColumns ?? throw new ArgumentNullException ( nameof ( orderByColumns ) ) ;
7275 this . QueryPaginationOptions = queryPaginationOptions ?? throw new ArgumentNullException ( nameof ( queryPaginationOptions ) ) ;
7376 this . MaxConcurrency = maxConcurrency ;
77+ this . NonStreamingOrderBy = nonStreamingOrderBy ;
7478 }
7579 }
7680
@@ -181,6 +185,7 @@ public static TryCatch<IQueryPipelineStage> MonadicCreate(
181185 IReadOnlyList < OrderByColumn > orderByColumns ,
182186 QueryPaginationOptions queryPaginationOptions ,
183187 int maxConcurrency ,
188+ bool nonStreamingOrderBy ,
184189 CosmosElement continuationToken )
185190 {
186191 if ( documentContainer == null )
@@ -233,24 +238,28 @@ public static TryCatch<IQueryPipelineStage> MonadicCreate(
233238 partitionKey ,
234239 orderByColumns ,
235240 queryPaginationOptions ,
236- maxConcurrency ) ;
241+ maxConcurrency ,
242+ nonStreamingOrderBy ) ;
237243
238244 return TryCatch < IQueryPipelineStage > . FromResult ( new OrderByCrossPartitionQueryPipelineStage ( init ) ) ;
239245 }
240246
241- private static async ValueTask < ( TryCatch < IQueryPipelineStage > , Queue < QueryPage > ) > MoveNextAsync_InitializeAsync ( InitializationParameters init , ITrace trace , CancellationToken cancellationToken )
247+ private static async ValueTask < ( TryCatch < IQueryPipelineStage > , Queue < QueryPage > ) > MoveNextAsync_InitializeAsync (
248+ InitializationParameters parameters ,
249+ ITrace trace ,
250+ CancellationToken cancellationToken )
242251 {
243252 SqlQuerySpec rewrittenQueryForOrderBy = new SqlQuerySpec (
244- init . SqlQuerySpec . QueryText . Replace ( oldValue : FormatPlaceHolder , newValue : TrueFilter ) ,
245- init . SqlQuerySpec . Parameters ) ;
253+ parameters . SqlQuerySpec . QueryText . Replace ( oldValue : FormatPlaceHolder , newValue : TrueFilter ) ,
254+ parameters . SqlQuerySpec . Parameters ) ;
246255
247- List < OrderByQueryPartitionRangePageAsyncEnumerator > uninitializedEnumerators = init . TargetRanges
256+ List < OrderByQueryPartitionRangePageAsyncEnumerator > uninitializedEnumerators = parameters . TargetRanges
248257 . Select ( range => OrderByQueryPartitionRangePageAsyncEnumerator . Create (
249- init . DocumentContainer ,
258+ parameters . DocumentContainer ,
250259 rewrittenQueryForOrderBy ,
251260 new FeedRangeState < QueryState > ( range , state : default ) ,
252- init . PartitionKey ,
253- init . QueryPaginationOptions ,
261+ parameters . PartitionKey ,
262+ parameters . QueryPaginationOptions ,
254263 TrueFilter ,
255264 PrefetchPolicy . PrefetchSinglePage ) )
256265 . ToList ( ) ;
@@ -259,13 +268,12 @@ public static TryCatch<IQueryPipelineStage> MonadicCreate(
259268 uninitializedEnumerators
260269 . Select ( x => ( x , ( OrderByContinuationToken ) null ) ) ) ;
261270
262- await ParallelPrefetch . PrefetchInParallelAsync ( uninitializedEnumerators , init . MaxConcurrency , trace , cancellationToken ) ;
271+ await ParallelPrefetch . PrefetchInParallelAsync ( uninitializedEnumerators , parameters . MaxConcurrency , trace , cancellationToken ) ;
263272
264- IReadOnlyList < SortOrder > sortOrders = init . OrderByColumns . Select ( column => column . SortOrder ) . ToList ( ) ;
273+ IReadOnlyList < SortOrder > sortOrders = parameters . OrderByColumns . Select ( column => column . SortOrder ) . ToList ( ) ;
265274 PriorityQueue < OrderByQueryPartitionRangePageAsyncEnumerator > initializedEnumerators = new PriorityQueue < OrderByQueryPartitionRangePageAsyncEnumerator > ( new OrderByEnumeratorComparer ( sortOrders ) ) ;
266275 Queue < ( OrderByQueryPartitionRangePageAsyncEnumerator enumerator , OrderByContinuationToken token ) > enumeratorsAndTokens = new Queue < ( OrderByQueryPartitionRangePageAsyncEnumerator enumerator , OrderByContinuationToken token ) > ( ) ;
267276
268- bool nonStreaming = false ;
269277 Queue < QueryPage > bufferedPages = new Queue < QueryPage > ( ) ;
270278 QueryPageParameters queryPageParameters = null ;
271279 while ( uninitializedEnumeratorsAndTokens . Count != 0 )
@@ -278,7 +286,7 @@ public static TryCatch<IQueryPipelineStage> MonadicCreate(
278286 if ( IsSplitException ( enumerator . Current . Exception ) )
279287 {
280288 await MoveNextAsync_InitializeAsync_HandleSplitAsync (
281- init . DocumentContainer ,
289+ parameters . DocumentContainer ,
282290 uninitializedEnumeratorsAndTokens ,
283291 enumerator ,
284292 token ,
@@ -307,9 +315,6 @@ await MoveNextAsync_InitializeAsync_HandleSplitAsync(
307315 additionalHeaders : page . AdditionalHeaders ) ;
308316 }
309317
310- // For backwards compatibility the default value of streaming for ORDER BY is _true_
311- nonStreaming = nonStreaming || ( ! page . Streaming . GetValueOrDefault ( true ) && ( page . State != null ) ) ;
312-
313318 if ( enumerator . Current . Result . Enumerator . MoveNext ( ) )
314319 {
315320 // the page is non-empty then we need to enqueue the enumerator in the PriorityQueue
@@ -335,7 +340,7 @@ await MoveNextAsync_InitializeAsync_HandleSplitAsync(
335340 }
336341
337342 IQueryPipelineStage pipelineStage ;
338- if ( nonStreaming )
343+ if ( parameters . NonStreamingOrderBy )
339344 {
340345 Queue < OrderByQueryPartitionRangePageAsyncEnumerator > orderbyEnumerators = new Queue < OrderByQueryPartitionRangePageAsyncEnumerator > ( ) ;
341346 foreach ( ( OrderByQueryPartitionRangePageAsyncEnumerator enumerator , OrderByContinuationToken _ ) in enumeratorsAndTokens )
@@ -350,10 +355,10 @@ await MoveNextAsync_InitializeAsync_HandleSplitAsync(
350355 orderbyEnumerators . Enqueue ( bufferedEnumerator ) ;
351356 }
352357
353- await ParallelPrefetch . PrefetchInParallelAsync ( orderbyEnumerators , init . MaxConcurrency , trace , cancellationToken ) ;
358+ await ParallelPrefetch . PrefetchInParallelAsync ( orderbyEnumerators , parameters . MaxConcurrency , trace , cancellationToken ) ;
354359
355360 pipelineStage = await NonStreamingOrderByPipelineStage . CreateAsync (
356- init . QueryPaginationOptions ,
361+ parameters . QueryPaginationOptions ,
357362 sortOrders ,
358363 orderbyEnumerators ,
359364 queryPageParameters ,
@@ -363,12 +368,12 @@ await MoveNextAsync_InitializeAsync_HandleSplitAsync(
363368 else
364369 {
365370 pipelineStage = StreamingOrderByCrossPartitionQueryPipelineStage . Create (
366- init . DocumentContainer ,
371+ parameters . DocumentContainer ,
367372 sortOrders ,
368373 initializedEnumerators ,
369374 enumeratorsAndTokens ,
370- init . QueryPaginationOptions ,
371- init . MaxConcurrency ) ;
375+ parameters . QueryPaginationOptions ,
376+ parameters . MaxConcurrency ) ;
372377 }
373378
374379 return ( TryCatch < IQueryPipelineStage > . FromResult ( pipelineStage ) , bufferedPages ) ;
0 commit comments