Skip to content

Commit 117b9fc

Browse files
Merge branch 'master' into users/kundadebdatta/5047_enable_read_hedging_with_ppaf
2 parents 4a99a78 + a627e89 commit 117b9fc

5 files changed

Lines changed: 368 additions & 53 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: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,33 @@ public static HybridSearchQueryResult Create(CosmosElement document)
4949
throw new ArgumentException($"{FieldNames.Rid} must exist.");
5050
}
5151

52-
if (!cosmosObject.TryGetValue(FieldNames.Payload, out CosmosObject outerPayload))
53-
{
54-
throw new ArgumentException($"{FieldNames.Payload} must exist.");
55-
}
52+
bool outerPayloadExists = cosmosObject.TryGetValue(FieldNames.Payload, out CosmosObject outerPayload);
5653

57-
if (!outerPayload.TryGetValue(FieldNames.Payload, out CosmosElement innerPayload))
54+
HybridSearchQueryResult result;
55+
if (outerPayloadExists && outerPayload.TryGetValue(FieldNames.ComponentScores, out CosmosArray componentScores))
5856
{
59-
innerPayload = CosmosUndefined.Create();
60-
}
57+
// Using the older format where the payload is nested.
58+
if (!outerPayload.TryGetValue(FieldNames.Payload, out CosmosElement innerPayload))
59+
{
60+
innerPayload = CosmosUndefined.Create();
61+
}
6162

62-
if (!outerPayload.TryGetValue(FieldNames.ComponentScores, out CosmosArray componentScores))
63+
result = new HybridSearchQueryResult(rid, componentScores, innerPayload);
64+
}
65+
else
6366
{
64-
throw new ArgumentException($"{FieldNames.ComponentScores} must exist.");
67+
// Using the newer format where the payload is not nested.
68+
if (!cosmosObject.TryGetValue(FieldNames.ComponentScores, out componentScores))
69+
{
70+
throw new ArgumentException($"{FieldNames.ComponentScores} must exist.");
71+
}
72+
73+
CosmosElement payload = outerPayloadExists ? outerPayload : CosmosUndefined.Create();
74+
75+
result = new HybridSearchQueryResult(rid, componentScores, payload);
6576
}
6677

67-
return new HybridSearchQueryResult(rid, componentScores, innerPayload);
78+
return result;
6879
}
6980

7081
private static class FieldNames

Microsoft.Azure.Cosmos/src/RequestOptions/QueryRequestOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public ConsistencyLevel? ConsistencyLevel
208208

209209
internal FeedRange FeedRange { get; set; }
210210

211-
internal bool IsHybridSearchQueryPlanOptimizationDisabled { get; set; } = ConfigurationManager.IsHybridSearchQueryPlanOptimizationDisabled(defaultValue: false);
211+
internal bool IsHybridSearchQueryPlanOptimizationDisabled { get; set; } = ConfigurationManager.IsHybridSearchQueryPlanOptimizationDisabled(defaultValue: true);
212212

213213
// This is a temporary flag to enable the distributed query gateway mode.
214214
// This flag will be removed once we have a way for the client to determine

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/Query/NonStreamingOrderByQueryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static async Task ImplementationAsync(Container container, IReadOnlyList<CosmosO
111111
MakeQueryFeaturesTest(
112112
environmentVariable: null,
113113
requestOption: null,
114-
expectHybridSearchOptimizationDisabled: false),
114+
expectHybridSearchOptimizationDisabled: true),
115115
MakeQueryFeaturesTest(
116116
environmentVariable: true,
117117
requestOption: null,

0 commit comments

Comments
 (0)