Skip to content

Commit

Permalink
Addressed comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
adityasa committed Jan 28, 2024
1 parent 510034c commit f70b19f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 54 deletions.
48 changes: 25 additions & 23 deletions Microsoft.Azure.Cosmos/src/Linq/CosmosLinqQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Azure.Cosmos.Linq
using Microsoft.Azure.Cosmos.Serializer;
using Microsoft.Azure.Cosmos.Tracing;
using Newtonsoft.Json;
using Debug = System.Diagnostics.Debug;

/// <summary>
/// This is the entry point for LINQ query creation/execution, it generate query provider, implements IOrderedQueryable.
Expand Down Expand Up @@ -109,11 +110,10 @@ public IEnumerator<T> GetEnumerator()
}

FeedIterator<T> localFeedIterator = this.CreateFeedIterator(false, out ClientOperation clientOperation);

if (clientOperation != ClientOperation.None)
{
throw new InvalidOperationException($"Linq expression cannot be converted to query directly since it involves client side operation : {clientOperation}");
}
Debug.Assert(
clientOperation == ClientOperation.None,
"CosmosLinqQuery Assert!",
$"Unexpected client operation. Expected 'None', Received '{clientOperation}'");

while (localFeedIterator.HasMoreResults)
{
Expand Down Expand Up @@ -151,33 +151,33 @@ public override string ToString()
public QueryDefinition ToQueryDefinition(IDictionary<object, string> parameters = null)
{
LinqQuery linqQuery = DocumentQueryEvaluator.Evaluate(this.Expression, this.linqSerializationOptions, parameters);

if (linqQuery.ClientOperation != ClientOperation.None)
{
throw new InvalidOperationException($"Linq expression cannot be converted to query directly since it involves client side operation : {linqQuery.ClientOperation}");
}
ClientOperation clientOperation = linqQuery.ClientOperation;
Debug.Assert(
clientOperation == ClientOperation.None,
"CosmosLinqQuery Assert!",
$"Unexpected client operation. Expected 'None', Received '{clientOperation}'");

return QueryDefinition.CreateFromQuerySpec(linqQuery.SqlQuerySpec);
}

public FeedIterator<T> ToFeedIterator()
{
FeedIterator<T> iterator = this.CreateFeedIterator(true, out ClientOperation clientOperation);
if (clientOperation != ClientOperation.None)
{
throw new InvalidOperationException($"This operation does not support LINQ expression that contains client operation {clientOperation}");
}
Debug.Assert(
clientOperation == ClientOperation.None,
"CosmosLinqQuery Assert!",
$"Unexpected client operation. Expected 'None', Received '{clientOperation}'");

return new FeedIteratorInlineCore<T>(iterator, this.container.ClientContext);
}

public FeedIterator ToStreamIterator()
{
FeedIterator iterator = this.CreateStreamIterator(true, out ClientOperation clientOperation);
if (clientOperation != ClientOperation.None)
{
throw new InvalidOperationException($"This operation does not support LINQ expression that contains client operation {clientOperation}");
}
Debug.Assert(
clientOperation == ClientOperation.None,
"CosmosLinqQuery Assert!",
$"Unexpected client operation. Expected 'None', Received '{clientOperation}'");

return new FeedIteratorInlineCore(iterator, this.container.ClientContext);
}
Expand All @@ -203,10 +203,10 @@ internal async Task<Response<T>> AggregateResultAsync(CancellationToken cancella
Headers headers = new Headers();

FeedIteratorInlineCore<T> localFeedIterator = this.CreateFeedIterator(isContinuationExpected: false, clientOperation: out ClientOperation clientOperation);
if (clientOperation != ClientOperation.None)
{
throw new InvalidOperationException($"This operation does not support LINQ expression that contains client operation {clientOperation}");
}
Debug.Assert(
clientOperation == ClientOperation.None,
"CosmosLinqQuery Assert!",
$"Unexpected client operation. Expected 'None', Received '{clientOperation}'");

ITrace rootTrace;
using (rootTrace = Trace.GetRootTrace("Aggregate LINQ Operation"))
Expand Down Expand Up @@ -250,7 +250,9 @@ internal T ExecuteScalar()
System.Diagnostics.Debug.Assert(result.Count <= 1, "CosmosLinqQuery Assert!", "At most one result is expected!");
return result.FirstOrDefault();

// None typically gets returned during invocation of this method when aggregates are called and evaluates to FirstOrDefault.
// ExecuteScalar gets called when (sync) aggregates such as Max, Min, Sum are invoked on the IQueryable.
// Since query fully supprots these operations, there is no client operation involved.
// In these cases we return FirstOrDefault which handles empty/undefined/null result set from the backend.
case ClientOperation.None:
return result.FirstOrDefault();

Expand Down
29 changes: 0 additions & 29 deletions Microsoft.Azure.Cosmos/src/Linq/LinqQueryFeedIterator{T}.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected SqlQuerySpec QuerySpec

if (linqQuery.ClientOperation != ClientOperation.None)
{
throw new InvalidOperationException($"This operation does not support support LINQ expression that contains client operation {linqQuery.ClientOperation}");
throw new NotSupportedException($"This operation does not support the supplied LINQ expression since it involves client side operation : {linqQuery.ClientOperation}");
}

this.querySpec = linqQuery.SqlQuerySpec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.Azure.Cosmos.Services.Management.Tests.LinqProviderTests
[TestClass]
public class LinqCleanupTests
{
[Ignore]
// [Ignore]
[TestMethod]
public async Task CleanupLinqTestDatabases()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Azure.Cosmos.Services.Management.Tests.LinqProviderTests
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.Cosmos.Linq;

/// <summary>
/// LINQ tests for Non aggregate scalar functions such as FirstOrDefault
Expand Down

0 comments on commit f70b19f

Please sign in to comment.