Skip to content

Commit 523d8d8

Browse files
authored
Query: Fixes non streaming order by queries to not be tagged as passthrough queries (#5273)
## Description Since the addition of non-streaming order by, it is no longer safe to treat single partition order by queries as passthrough, as it can lead to incorrect results for `ORDER BY VectorDistance` and `ORDER BY RANK` queries. This PR fixes the issue by adding a check for non-streaming order by queries in the passthrough query creation logic. ## Type of change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue)
1 parent 243a3ca commit 523d8d8

5 files changed

Lines changed: 462 additions & 248 deletions

File tree

Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CosmosQueryExecutionContextFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ private static async Task<TryCatch<IQueryPipelineStage>> TryCreateCoreContextAsy
205205
bool hasDistinct = sqlQuery.SelectClause.HasDistinct;
206206
bool hasGroupBy = sqlQuery.GroupByClause != default;
207207
bool hasAggregates = AggregateProjectionDetector.HasAggregate(sqlQuery.SelectClause.SelectSpec);
208-
bool createPassthroughQuery = !hasAggregates && !hasDistinct && !hasGroupBy;
208+
bool hasOrderBy = sqlQuery.OrderByClause != default;
209+
bool createPassthroughQuery = !hasAggregates && !hasDistinct && !hasGroupBy && !hasOrderBy;
209210

210211
if (createPassthroughQuery)
211212
{
@@ -305,6 +306,7 @@ private static async Task<TryCatch<IQueryPipelineStage>> TryCreateFromPartitione
305306
&& partitionedQueryExecutionInfo.QueryRanges[0].IsSingleValue);
306307
bool serverStreamingQuery = !partitionedQueryExecutionInfo.QueryInfo.HasAggregates
307308
&& !partitionedQueryExecutionInfo.QueryInfo.HasDistinct
309+
&& !partitionedQueryExecutionInfo.QueryInfo.HasNonStreamingOrderBy
308310
&& !partitionedQueryExecutionInfo.QueryInfo.HasGroupBy;
309311
bool streamingSinglePartitionQuery = singleLogicalPartitionKeyQuery && serverStreamingQuery;
310312

Microsoft.Azure.Cosmos/src/Query/Core/Pipeline/CrossPartition/HybridSearch/HybridSearchCrossPartitionQueryPipelineStage.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public static TryCatch<IQueryPipelineStage> MonadicCreate(
9696
{
9797
TryCatch<IQueryPipelineStage> ComponentPipelineFactory(QueryInfo rewrittenQueryInfo)
9898
{
99+
HybridSearchDebugTraceHelpers.TraceQuerySpec(sqlQuerySpec);
100+
99101
return PipelineFactory.MonadicCreate(
100102
documentContainer,
101103
sqlQuerySpec,
@@ -289,6 +291,8 @@ private async ValueTask<bool> MoveNextAsync_DrainSingletonComponentAsync(ITrace
289291

290292
QueryPage page = sourceStage.Current.Result;
291293

294+
HybridSearchDebugTraceHelpers.TraceQueryResultTSVHeader(1);
295+
292296
List<CosmosElement> documents = new List<CosmosElement>(page.Documents.Count);
293297
foreach (CosmosElement cosmosElement in page.Documents)
294298
{
@@ -950,6 +954,19 @@ private static class HybridSearchDebugTraceHelpers
950954
private const bool Enabled = false;
951955
#pragma warning disable CS0162 // Unreachable code detected
952956

957+
[Conditional("DEBUG")]
958+
public static void TraceQuerySpec(SqlQuerySpec querySpec)
959+
{
960+
if (Enabled)
961+
{
962+
CosmosSerializerCore serializerCore = new CosmosSerializerCore();
963+
System.IO.Stream stream = serializerCore.ToStreamSqlQuerySpec(querySpec, ResourceType.Document);
964+
string content = new System.IO.StreamReader(stream).ReadToEnd();
965+
System.Diagnostics.Trace.WriteLine(content);
966+
System.Diagnostics.Trace.WriteLine("\n");
967+
}
968+
}
969+
953970
[Conditional("DEBUG")]
954971
public static void TraceQueryResults(IReadOnlyList<HybridSearchQueryResult> queryResults, int componentCount)
955972
{
@@ -1026,7 +1043,7 @@ private static StringBuilder AppendQueryResult(HybridSearchQueryResult queryResu
10261043
return builder;
10271044
}
10281045

1029-
private static void TraceQueryResultTSVHeader(int componentCount)
1046+
public static void TraceQueryResultTSVHeader(int componentCount)
10301047
{
10311048
StringBuilder builder = new StringBuilder();
10321049
builder.Append("_rid");

0 commit comments

Comments
 (0)