Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted all aggregate linq queries to return Response<T> #776

Merged
merged 2 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 57 additions & 46 deletions Microsoft.Azure.Cosmos/src/Linq/CosmosLinqExtensions.cs

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions Microsoft.Azure.Cosmos/src/Linq/CosmosLinqQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.Azure.Cosmos.Linq
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Query;
using Newtonsoft.Json;

/// <summary>
Expand Down Expand Up @@ -169,16 +170,25 @@ Task<DocumentFeedResponse<dynamic>> IDocumentQuery<T>.ExecuteNextAsync(Cancellat
throw new NotImplementedException();
}

internal async Task<IList<T>> AggregateResultAsync(CancellationToken cancellationToken = default(CancellationToken))
internal async Task<Response<T>> AggregateResultAsync(CancellationToken cancellationToken = default(CancellationToken))
{
List<T> result = new List<T>();
CosmosDiagnosticsAggregate cosmosDiagnostics = new CosmosDiagnosticsAggregate();
Headers headers = new Headers();
FeedIterator<T> localFeedIterator = this.CreateFeedIterator(false);
while (localFeedIterator.HasMoreResults)
{
FeedResponse<T> response = await localFeedIterator.ReadNextAsync();
headers.RequestCharge += response.RequestCharge;
cosmosDiagnostics.Diagnostics.Add(response.Diagnostics);
result.AddRange(response);
}
return result;

return new ItemResponse<T>(
System.Net.HttpStatusCode.OK,
headers,
result.FirstOrDefault(),
cosmosDiagnostics);
}

private FeedIterator CreateStreamIterator(bool isContinuationExcpected)
Expand Down
5 changes: 2 additions & 3 deletions Microsoft.Azure.Cosmos/src/Linq/CosmosLinqQueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public object Execute(Expression expression)
return cosmosLINQQuery.ToList().FirstOrDefault();
}

public async Task<TResult> ExecuteAggregateAsync<TResult>(
public async Task<Response<TResult>> ExecuteAggregateAsync<TResult>(
Expression expression,
CancellationToken cancellationToken = default(CancellationToken))
{
Expand All @@ -123,8 +123,7 @@ public async Task<TResult> ExecuteAggregateAsync<TResult>(
expression,
this.allowSynchronousQueryExecution,
this.serializationOptions);
IList<TResult> result = await cosmosLINQQuery.AggregateResultAsync();
return result.FirstOrDefault();
return await cosmosLINQQuery.AggregateResultAsync();
}
}
}
19 changes: 19 additions & 0 deletions Microsoft.Azure.Cosmos/src/Query/CosmosDiagnosticsAggregate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Query
{
using System.Collections.Generic;
using Newtonsoft.Json;

internal class CosmosDiagnosticsAggregate : CosmosDiagnostics
{
public IList<CosmosDiagnostics> Diagnostics = new List<CosmosDiagnostics>();

public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,65 +267,125 @@ public async Task QueryableExtentionFunctionsTest()
int count = await linqQueryable.CountAsync();
Assert.AreEqual(10, count);

int intSum = await linqQueryable.Select(item => item.taskNum).SumAsync();
Response<int> intSum = await linqQueryable.Select(item => item.taskNum).SumAsync();
Assert.AreEqual(420, intSum.Resource);
Assert.IsTrue(intSum.RequestCharge > 0);
string diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<int?> intNullableSum = await linqQueryable.Select(item => (int?)item.taskNum).SumAsync();
Assert.AreEqual<int?>(420, intNullableSum);
Assert.IsTrue(intNullableSum.RequestCharge > 0);
diagnostics = intNullableSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<float> floatSum = await linqQueryable.Select(item => (float)item.taskNum).SumAsync();
Assert.AreEqual(420, intSum);

int? intNullableSum = await linqQueryable.Select(item => item.taskNum).SumAsync();
Assert.AreEqual(420, intNullableSum);

float floatSum = await linqQueryable.Select(item => (float)item.taskNum).SumAsync();
Assert.AreEqual(420, intSum);

float? floatNullableSum = await linqQueryable.Select(item => (float?)item.taskNum).SumAsync();
Assert.AreEqual(420, intNullableSum);

double doubleSum = await linqQueryable.Select(item => (double)item.taskNum).SumAsync();
Assert.AreEqual(420, doubleSum);

double? doubleNullableSum = await linqQueryable.Select(item => (double?)item.taskNum).SumAsync();
Assert.AreEqual(420, doubleNullableSum);

long longSum = await linqQueryable.Select(item => (long)item.taskNum).SumAsync();
Assert.AreEqual(420, longSum);

long? longNullableSum = await linqQueryable.Select(item => (long?)item.taskNum).SumAsync();
Assert.AreEqual(420, longNullableSum);

decimal decimalSum = await linqQueryable.Select(item => (decimal)item.taskNum).SumAsync();
Assert.AreEqual(420, decimalSum);

decimal? decimalNullableSum = await linqQueryable.Select(item => (decimal?)item.taskNum).SumAsync();
Assert.AreEqual(420, decimalNullableSum);

double intToDoubleAvg = await linqQueryable.Select(item => item.taskNum).AverageAsync();
Assert.AreEqual(42, intToDoubleAvg);

double? intToDoubleNulableAvg = await linqQueryable.Select(item => item.taskNum).AverageAsync();
Assert.AreEqual(42, intToDoubleNulableAvg);

float floatAvg = await linqQueryable.Select(item => (float)item.taskNum).AverageAsync();
Assert.AreEqual(42, floatAvg);

float? floatNullableAvg = await linqQueryable.Select(item => (float?)item.taskNum).AverageAsync();
Assert.AreEqual(42, floatNullableAvg);

double doubleAvg = await linqQueryable.Select(item => (double)item.taskNum).AverageAsync();
Assert.AreEqual(42, doubleAvg);

double? doubleNullableAvg = await linqQueryable.Select(item => (double?)item.taskNum).AverageAsync();
Assert.AreEqual(42, doubleNullableAvg);

double longToDoubleAvg = await linqQueryable.Select(item => (long)item.taskNum).AverageAsync();
Assert.AreEqual(42, longToDoubleAvg);

double? longToNullableDoubleAvg = await linqQueryable.Select(item => (long?)item.taskNum).AverageAsync();
Assert.AreEqual(42, longToNullableDoubleAvg);

decimal decimalAvg = await linqQueryable.Select(item => (decimal)item.taskNum).AverageAsync();
Assert.AreEqual(42, decimalAvg);

decimal? decimalNullableAvg = await linqQueryable.Select(item => (decimal?)item.taskNum).AverageAsync();
Assert.AreEqual(42, decimalNullableAvg);
Assert.IsTrue(floatSum.RequestCharge > 0);
diagnostics = floatSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<float?> floatNullableSum = await linqQueryable.Select(item => (float?)item.taskNum).SumAsync();
Assert.AreEqual<float?>(420, intNullableSum);
Assert.IsTrue(intSum.RequestCharge > 0);
diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<double> doubleSum = await linqQueryable.Select(item => (double)item.taskNum).SumAsync();
Assert.AreEqual(420.0, doubleSum);
Assert.IsTrue(intSum.RequestCharge > 0);
diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<double?> doubleNullableSum = await linqQueryable.Select(item => (double?)item.taskNum).SumAsync();
Assert.AreEqual<double?>(420.0, doubleNullableSum);
Assert.IsTrue(intSum.RequestCharge > 0);
diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<long> longSum = await linqQueryable.Select(item => (long)item.taskNum).SumAsync();
Assert.AreEqual<long>(420, longSum);
Assert.IsTrue(intSum.RequestCharge > 0);
diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<long?> longNullableSum = await linqQueryable.Select(item => (long?)item.taskNum).SumAsync();
Assert.AreEqual<long?>(420, longNullableSum);
Assert.IsTrue(intSum.RequestCharge > 0);
diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<decimal> decimalSum = await linqQueryable.Select(item => (decimal)item.taskNum).SumAsync();
Assert.AreEqual<decimal>(420, decimalSum);
Assert.IsTrue(intSum.RequestCharge > 0);
diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<decimal?> decimalNullableSum = await linqQueryable.Select(item => (decimal?)item.taskNum).SumAsync();
Assert.AreEqual<decimal?>(420, decimalNullableSum);
Assert.IsTrue(intSum.RequestCharge > 0);
diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<double> intToDoubleAvg = await linqQueryable.Select(item => item.taskNum).AverageAsync();
Assert.AreEqual<double>(42, intToDoubleAvg);
Assert.IsTrue(intSum.RequestCharge > 0);
diagnostics = intSum.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<double?> intToDoubleNulableAvg = await linqQueryable.Select(item => (double?)item.taskNum).AverageAsync();
Assert.AreEqual<double?>(42, intToDoubleNulableAvg);
Assert.IsTrue(intToDoubleNulableAvg.RequestCharge > 0);
diagnostics = intToDoubleNulableAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<float> floatAvg = await linqQueryable.Select(item => (float)item.taskNum).AverageAsync();
Assert.AreEqual<float>(42, floatAvg);
Assert.IsTrue(floatAvg.RequestCharge > 0);
diagnostics = floatAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<float?> floatNullableAvg = await linqQueryable.Select(item => (float?)item.taskNum).AverageAsync();
Assert.AreEqual<float?>(42, floatNullableAvg);
Assert.IsTrue(floatNullableAvg.RequestCharge > 0);
diagnostics = floatNullableAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<double> doubleAvg = await linqQueryable.Select(item => (double)item.taskNum).AverageAsync();
Assert.AreEqual<double>(42, doubleAvg);
Assert.IsTrue(doubleAvg.RequestCharge > 0);
diagnostics = doubleAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<double?> doubleNullableAvg = await linqQueryable.Select(item => (double?)item.taskNum).AverageAsync();
Assert.AreEqual<double?>(42, doubleNullableAvg);
Assert.IsTrue(doubleNullableAvg.RequestCharge > 0);
diagnostics = doubleNullableAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<double> longToDoubleAvg = await linqQueryable.Select(item => (long)item.taskNum).AverageAsync();
Assert.AreEqual<double>(42, longToDoubleAvg);
Assert.IsTrue(longToDoubleAvg.RequestCharge > 0);
diagnostics = longToDoubleAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<double?> longToNullableDoubleAvg = await linqQueryable.Select(item => (long?)item.taskNum).AverageAsync();
Assert.AreEqual<double?>(42, longToNullableDoubleAvg);
Assert.IsTrue(longToNullableDoubleAvg.RequestCharge > 0);
diagnostics = longToNullableDoubleAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<decimal> decimalAvg = await linqQueryable.Select(item => (decimal)item.taskNum).AverageAsync();
Assert.AreEqual<decimal>(42, decimalAvg);
Assert.IsTrue(decimalAvg.RequestCharge > 0);
diagnostics = decimalAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

Response<decimal?> decimalNullableAvg = await linqQueryable.Select(item => (decimal?)item.taskNum).AverageAsync();
Assert.AreEqual<decimal?>(42, decimalNullableAvg);
Assert.IsTrue(decimalNullableAvg.RequestCharge > 0);
diagnostics = decimalNullableAvg.Diagnostics.ToString();
Assert.IsTrue(diagnostics.Contains("queryMetrics"));

//Adding more items to test min and max function
ToDoActivity toDoActivity = ToDoActivity.CreateRandomToDoActivity();
Expand All @@ -336,10 +396,10 @@ public async Task QueryableExtentionFunctionsTest()
toDoActivity.id = "maxTaskNum";
await this.Container.CreateItemAsync(toDoActivity, new PartitionKey(toDoActivity.status));

int minTaskNum = await linqQueryable.Select(item => item.taskNum).MinAsync();
Response<int> minTaskNum = await linqQueryable.Select(item => item.taskNum).MinAsync();
Assert.AreEqual(20, minTaskNum);

int maxTaskNum = await linqQueryable.Select(item => item.taskNum).MaxAsync();
Response<int> maxTaskNum = await linqQueryable.Select(item => item.taskNum).MaxAsync();
Assert.AreEqual(100, maxTaskNum);
}

Expand Down
Loading