Skip to content

Commit 335d735

Browse files
committed
Add support for the optimized query plan that skips the order by rewrite
1 parent 45230b8 commit 335d735

3 files changed

Lines changed: 31 additions & 21 deletions

File tree

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,10 @@ private static IReadOnlyList<ComponentWeight> ExtractComponentWeights(HybridSear
404404
for (int index = 0; index < hybridSearchQueryInfo.ComponentQueryInfos.Count; ++index)
405405
{
406406
QueryInfo queryInfo = hybridSearchQueryInfo.ComponentQueryInfos[index];
407-
Debug.Assert(queryInfo.HasOrderBy, "The component query should have an order by");
408-
Debug.Assert(queryInfo.HasNonStreamingOrderBy, "The component query is a non streaming order by");
409-
Debug.Assert(queryInfo.OrderBy.Count == 1, "The component query should have exactly one order by expression");
407+
SortOrder sortOrder = queryInfo.HasOrderBy ? queryInfo.OrderBy[0] : SortOrder.Descending;
410408

411409
double componentWeight = useDefaultComponentWeight ? 1.0 : hybridSearchQueryInfo.ComponentWeights[index];
412-
result.Add(new ComponentWeight(componentWeight, queryInfo.OrderBy[0]));
410+
result.Add(new ComponentWeight(componentWeight, sortOrder));
413411
}
414412

415413
return result;
@@ -635,14 +633,19 @@ private static void ComputeRrfScores(
635633

636634
private static QueryInfo RewriteOrderByQueryInfo(QueryInfo queryInfo, GlobalFullTextSearchStatistics statistics, int componentCount)
637635
{
638-
Debug.Assert(queryInfo.HasOrderBy, "The component query should have an order by");
639-
Debug.Assert(queryInfo.HasNonStreamingOrderBy, "The component query is a non streaming order by");
636+
IReadOnlyList<string> rewrittenOrderByExpressions = queryInfo.OrderByExpressions;
640637

641-
List<string> rewrittenOrderByExpressions = new List<string>(queryInfo.OrderByExpressions.Count);
642-
foreach (string orderByExpression in queryInfo.OrderByExpressions)
638+
if (queryInfo.HasOrderBy)
643639
{
644-
string rewrittenOrderByExpression = FormatComponentQueryTextWorkaround(orderByExpression, statistics, componentCount);
645-
rewrittenOrderByExpressions.Add(rewrittenOrderByExpression);
640+
Debug.Assert(queryInfo.HasNonStreamingOrderBy, "The component query is a non streaming order by");
641+
List<string> orderByExpressions = new List<string>(queryInfo.OrderByExpressions.Count);
642+
foreach (string orderByExpression in queryInfo.OrderByExpressions)
643+
{
644+
string rewrittenOrderByExpression = FormatComponentQueryTextWorkaround(orderByExpression, statistics, componentCount);
645+
orderByExpressions.Add(rewrittenOrderByExpression);
646+
}
647+
648+
rewrittenOrderByExpressions = orderByExpressions;
646649
}
647650

648651
string rewrittenQuery = FormatComponentQueryTextWorkaround(queryInfo.RewrittenQuery, statistics, componentCount);
@@ -777,18 +780,15 @@ private static string FormatComponentQueryTextWorkaround(string format, GlobalFu
777780

778781
private class ComponentWeight
779782
{
780-
public SortOrder SortOrder { get; }
781-
782783
public double Weight { get; }
783784

784785
public Comparison<double> Comparison { get; }
785786

786787
public ComponentWeight(double weight, SortOrder sortOrder)
787788
{
788789
this.Weight = weight;
789-
this.SortOrder = sortOrder;
790790

791-
int comparisonFactor = (this.SortOrder == SortOrder.Ascending) ? 1 : -1;
791+
int comparisonFactor = (sortOrder == SortOrder.Ascending) ? 1 : -1;
792792
this.Comparison = (x, y) => comparisonFactor * x.CompareTo(y);
793793
}
794794
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,26 @@ public static HybridSearchQueryResult Create(CosmosElement document)
5454
throw new ArgumentException($"{FieldNames.Payload} must exist.");
5555
}
5656

57-
if (!outerPayload.TryGetValue(FieldNames.Payload, out CosmosObject innerPayload))
57+
if (outerPayload.TryGetValue(FieldNames.Payload, out CosmosObject innerPayload))
5858
{
59-
throw new ArgumentException($"{FieldNames.Payload} must exist nested within the outer payload field.");
59+
// Using the older format where the payload is nested.
60+
if (!outerPayload.TryGetValue(FieldNames.ComponentScores, out CosmosArray componentScores))
61+
{
62+
throw new ArgumentException($"{FieldNames.ComponentScores} must exist.");
63+
}
64+
65+
return new HybridSearchQueryResult(rid, componentScores, innerPayload);
6066
}
6167

62-
if (!outerPayload.TryGetValue(FieldNames.ComponentScores, out CosmosArray componentScores))
6368
{
64-
throw new ArgumentException($"{FieldNames.ComponentScores} must exist.");
65-
}
69+
// Using the newer format where the payload is not nested.
70+
if (!cosmosObject.TryGetValue(FieldNames.ComponentScores, out CosmosArray componentScores))
71+
{
72+
throw new ArgumentException($"{FieldNames.ComponentScores} must exist.");
73+
}
6674

67-
return new HybridSearchQueryResult(rid, componentScores, innerPayload);
75+
return new HybridSearchQueryResult(rid, componentScores, outerPayload);
76+
}
6877
}
6978

7079
private static class FieldNames

Microsoft.Azure.Cosmos/src/Query/Core/QueryPlan/QueryPlanRetriever.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ internal static class QueryPlanRetriever
3232
| QueryFeatures.NonStreamingOrderBy
3333
| QueryFeatures.CountIf
3434
| QueryFeatures.HybridSearch
35-
| QueryFeatures.WeightedRankFusion;
35+
| QueryFeatures.WeightedRankFusion
36+
| QueryFeatures.HybridSearchSkipOrderByRewrite;
3637

3738
private static readonly QueryFeatures SupportedQueryFeaturesWithoutNonStreamingOrderBy =
3839
SupportedQueryFeatures & (~QueryFeatures.NonStreamingOrderBy);

0 commit comments

Comments
 (0)