Skip to content

Commit b2ab9c8

Browse files
Don't calculate the 'LoadResult.data' field value if Take < 0 (#468)
1 parent 1e36d48 commit b2ab9c8

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

net/DevExtreme.AspNet.Data.Tests/DataSourceLoaderTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,37 @@ public void Load_Select_NoAnonTypeLimits(bool? remoteSelect) {
475475
);
476476
}
477477

478+
[Theory]
479+
[InlineData(false, false)]
480+
[InlineData(false, true)]
481+
[InlineData(true, false)]
482+
[InlineData(true, true)]
483+
public void Load_NegativeTake(bool remoteGrouping, bool group) {
484+
var loadOptions = new SampleLoadOptions {
485+
Filter = new[] { "this", "<", "100" },
486+
RemoteGrouping = remoteGrouping,
487+
Take = -1,
488+
TotalSummary = new[] {
489+
new SummaryInfo { Selector = "this", SummaryType = "sum" }
490+
}
491+
};
492+
493+
if(group) {
494+
loadOptions.Group = new[] {
495+
new GroupingInfo { Selector = "any", IsExpanded = !remoteGrouping }
496+
};
497+
loadOptions.GroupSummary = new[] {
498+
new SummaryInfo { Selector = "any", SummaryType = "any" }
499+
};
500+
}
501+
502+
var loadResult = DataSourceLoader.Load(new[] { 1, 1, 2, 2, 100 }, loadOptions);
503+
Assert.Null(loadResult.data);
504+
Assert.Equal(6m, loadResult.summary[0]);
505+
506+
Assert.Single(loadOptions.ExpressionLog);
507+
}
508+
478509
[Theory]
479510
[InlineData(false, true)]
480511
[InlineData(false, false)]

net/DevExtreme.AspNet.Data/DataSourceLoadContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ partial class DataSourceLoadContext {
8888

8989
public IReadOnlyList<GroupingInfo> Group => _options.Group;
9090

91-
public bool HasGroups => !IsEmpty(Group);
91+
public bool HasGroups => Take > -1 && !IsEmpty(Group);
9292

9393
public bool ShouldEmptyGroups {
9494
get {
@@ -259,6 +259,8 @@ public bool ExpandLinqSumType {
259259
|| _providerInfo.IsXPO;
260260
}
261261
}
262+
263+
public bool IsRemoteTotalSummary => UseRemoteGrouping && !SummaryIsTotalCountOnly && HasSummary && !HasGroups;
262264
}
263265

264266
// Select

net/DevExtreme.AspNet.Data/DataSourceLoaderImpl.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public async Task<LoadResult> LoadAsync() {
4444
if(Context.IsCountQuery)
4545
return new LoadResult { totalCount = await ExecTotalCountAsync() };
4646

47+
if(Context.Take < 0)
48+
return await LoadAggregatesOnlyAsync();
49+
4750
var result = new LoadResult();
4851

4952
if(Context.UseRemoteGrouping && Context.ShouldEmptyGroups) {
@@ -116,6 +119,21 @@ await ExecExprAsync<S>(loadExpr),
116119
return result;
117120
}
118121

122+
async Task<LoadResult> LoadAggregatesOnlyAsync() {
123+
var result = new LoadResult();
124+
125+
if(Context.HasTotalSummary) {
126+
if(Context.IsRemoteTotalSummary) {
127+
await ContinueWithAggregationAsync<S>(null, null, result, false);
128+
} else {
129+
var data = await ExecExprAsync<S>(CreateBuilder().BuildLoadExpr(false));
130+
await ContinueWithAggregationAsync(data, new DefaultAccessor<S>(), result, false);
131+
}
132+
}
133+
134+
return result;
135+
}
136+
119137
async Task<IEnumerable<ExpandoObject>> ExecWithSelectAsync(Expression loadExpr) {
120138
if(Context.UseRemoteSelect)
121139
return SelectHelper.ConvertRemoteResult(await ExecExprAnonAsync(loadExpr), Context.FullSelect);
@@ -129,14 +147,14 @@ async Task ContinueWithGroupingAsync<R>(IEnumerable<R> loadResult, LoadResult re
129147
var groups = new GroupHelper<R>(accessor).Group(loadResult, Context.Group);
130148
if(Context.RequireGroupCount)
131149
result.groupCount = groups.Count;
132-
await ContinueWithAggregationAsync(groups, accessor, result);
150+
await ContinueWithAggregationAsync(groups, accessor, result, true);
133151
} else {
134-
await ContinueWithAggregationAsync(loadResult, accessor, result);
152+
await ContinueWithAggregationAsync(loadResult, accessor, result, true);
135153
}
136154
}
137155

138-
async Task ContinueWithAggregationAsync<R>(IEnumerable data, IAccessor<R> accessor, LoadResult result) {
139-
if(Context.UseRemoteGrouping && !Context.SummaryIsTotalCountOnly && Context.HasSummary && !Context.HasGroups) {
156+
async Task ContinueWithAggregationAsync<R>(IEnumerable data, IAccessor<R> accessor, LoadResult result, bool includeData) {
157+
if(Context.IsRemoteTotalSummary) {
140158
var totalsResult = await ExecRemoteTotalsAsync();
141159
result.totalCount = totalsResult.TotalCount;
142160
result.summary = totalsResult.Totals;
@@ -152,12 +170,14 @@ async Task ContinueWithAggregationAsync<R>(IEnumerable data, IAccessor<R> access
152170
if(Context.SummaryIsTotalCountOnly) {
153171
result.summary = Enumerable.Repeat((object)totalCount, Context.TotalSummary.Count).ToArray();
154172
} else if(Context.HasSummary) {
155-
data = Buffer<R>(data);
173+
if(includeData)
174+
data = Buffer<R>(data);
156175
result.summary = new AggregateCalculator<R>(data, accessor, Context.TotalSummary, Context.GroupSummary).Run();
157176
}
158177
}
159178

160-
result.data = data;
179+
if(includeData)
180+
result.data = data;
161181
}
162182

163183
Task<int> ExecCountAsync(Expression expr) {

0 commit comments

Comments
 (0)