1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Query.LinqToEntities
{
using System.Data.Entity.TestModels.ArubaModel;
using System.Linq;
using Xunit;
public class GroupByOptimizationTests : FunctionalTestBase
{
[Fact]
public void GroupBy_is_optimized_when_projecting_group_key()
{
var expectedSql =
@"SELECT
[Distinct1].[FirstName] AS [FirstName]
FROM ( SELECT DISTINCT
[Extent1].[FirstName] AS [FirstName]
FROM [dbo].[ArubaOwners] AS [Extent1]
) AS [Distinct1]";
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(o => o.FirstName).Select(g => g.Key);
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(o => o.FirstName).Select(g => g.Key).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o == i);
}
}
[Fact]
public void GroupBy_is_optimized_when_projecting_group_count()
{
var expectedSql =
@"SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
[Extent1].[FirstName] AS [K1],
COUNT(1) AS [A1]
FROM [dbo].[ArubaOwners] AS [Extent1]
GROUP BY [Extent1].[FirstName]
) AS [GroupBy1]";
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(o => o.FirstName).Select(g => g.Count());
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(o => o.FirstName).Select(g => g.Count()).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o == i);
}
}
[Fact]
public void GroupBy_is_optimized_when_projecting_expression_containing_group_key()
{
var expectedSql =
@"SELECT
[Extent1].[Id] * 2 AS [C1]
FROM [dbo].[ArubaOwners] AS [Extent1]";
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(o => o.Id).Select(g => g.Key * 2);
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(o => o.Id).Select(g => g.Key * 2).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o == i);
}
}
[Fact]
public void GroupBy_is_optimized_when_projecting_aggregate_on_the_group()
{
var expectedSql =
@"SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
[Extent1].[FirstName] AS [K1],
MAX([Extent1].[Id]) AS [A1]
FROM [dbo].[ArubaOwners] AS [Extent1]
GROUP BY [Extent1].[FirstName]
) AS [GroupBy1]";
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(o => o.FirstName).Select(g => g.Max(p => p.Id));
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(o => o.FirstName).Select(g => g.Max(p => p.Id)).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o == i);
}
}
[Fact]
public void GroupBy_is_optimized_when_projecting_anonymous_type_containing_group_key_and_group_aggregate()
{
var expectedSql =
@"SELECT
1 AS [C1],
[GroupBy1].[K1] AS [FirstName],
[GroupBy1].[A1] AS [C2]
FROM ( SELECT
[Extent1].[FirstName] AS [K1],
MAX([Extent1].[Id]) AS [A1]
FROM [dbo].[ArubaOwners] AS [Extent1]
GROUP BY [Extent1].[FirstName]
) AS [GroupBy1]";
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(o => o.FirstName).Select(g => new { Key = g.Key, Aggregate = g.Max(p => p.Id) });
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(o => o.FirstName).Select(g => new { Key = g.Key, Aggregate = g.Max(p => p.Id) }).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.Key == i.Key && o.Aggregate == i.Aggregate);
}
}
[Fact]
public void GroupBy_is_optimized_when_projecting_anonymous_type_containing_group_key_and_multiple_group_aggregates()
{
var expectedSql =
@"SELECT
1 AS [C1],
[GroupBy1].[K1] AS [FirstName],
[GroupBy1].[A1] AS [C2],
[GroupBy1].[A2] AS [C3]
FROM ( SELECT
[Extent1].[K1] AS [K1],
MAX([Extent1].[A1]) AS [A1],
MIN([Extent1].[A2]) AS [A2]
FROM ( SELECT
[Extent1].[FirstName] AS [K1],
[Extent1].[Id] AS [A1],
[Extent1].[Id] + 2 AS [A2]
FROM [dbo].[ArubaOwners] AS [Extent1]
) AS [Extent1]
GROUP BY [K1]
) AS [GroupBy1]";
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(o => o.FirstName).Select(g => new { key1 = g.Key, key2 = g.Key, max = g.Max(p => p.Id), min = g.Min(s => s.Id + 2) });
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(o => o.FirstName).Select(g => new { key1 = g.Key, key2 = g.Key, max = g.Max(p => p.Id), min = g.Min(s => s.Id + 2) }).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.key1 == i.key1 && o.key2 == i.key2 && o.max == i.max && o.min == i.min);
}
}
[Fact]
public void GroupBy_is_optimized_when_projecting_conditional_expression_containing_group_key()
{
var expectedSql =
@"SELECT
1 AS [C1],
CASE WHEN ([Distinct1].[FirstName] IS NULL) THEN N'is null' ELSE N'not null' END AS [C2],
CASE WHEN (((@p__linq__0 = 1) AND (@p__linq__1 = 1)) OR ((@p__linq__2 = 1) AND (@p__linq__3 = 1))) THEN cast(1 as bit) WHEN ( NOT (((@p__linq__0 = 1) AND (@p__linq__1 = 1)) OR ((@p__linq__2 = 1) AND (@p__linq__3 = 1)))) THEN cast(0 as bit) END AS [C3]
FROM ( SELECT DISTINCT
[Extent1].[FirstName] AS [FirstName]
FROM [dbo].[ArubaOwners] AS [Extent1]
) AS [Distinct1]
/*
Boolean p__linq__0 = True
Boolean p__linq__1 = False
Boolean p__linq__2 = False
Boolean p__linq__3 = True
*/";
bool a = true;
bool b = false;
bool c = true;
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(o => o.FirstName).Select(g => new { keyIsNull = g.Key == null ? "is null" : "not null", logicExpression = (a && b || b && c) });
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(o => o.FirstName).Select(g => new { keyIsNull = g.Key == null ? "is null" : "not null", logicExpression = (a && b || b && c) }).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.keyIsNull == i.keyIsNull && o.logicExpression == i.logicExpression);
}
}
[Fact]
public void GroupBy_is_optimized_when_filerting_and_projecting_anonymous_type_with_group_key_and_function_aggregate()
{
var expectedSql =
@"SELECT
1 AS [C1],
[GroupBy1].[K1] AS [FirstName],
[GroupBy1].[A1] AS [C2]
FROM ( SELECT
[Extent1].[FirstName] AS [K1],
AVG( CAST( [Extent1].[Id] AS float)) AS [A1]
FROM [dbo].[ArubaOwners] AS [Extent1]
WHERE [Extent1].[Id] > 5
GROUP BY [Extent1].[FirstName]
) AS [GroupBy1]";
using (var context = new ArubaContext())
{
var query = context.Owners.Where(o => o.Id > 5).GroupBy(o => o.FirstName).Select(g => new { FirstName = g.Key, AverageId = g.Average(p => p.Id) });
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList().OrderBy(r => r.AverageId).ToList();
var expected = context.Owners.ToList().Where(o => o.Id > 5).GroupBy(o => o.FirstName).Select(g => new { FirstName = g.Key, AverageId = g.Average(p => p.Id) }).OrderBy(r => r.AverageId).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.FirstName == i.FirstName && o.AverageId == i.AverageId);
}
}
[Fact]
public void GroupBy_is_optimized_when_projecting_function_aggregate_with_expression()
{
var expectedSql =
@"SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
[Extent1].[K1] AS [K1],
MAX([Extent1].[A1]) AS [A1]
FROM ( SELECT
[Extent1].[FirstName] AS [K1],
[Extent1].[Id] * 2 AS [A1]
FROM [dbo].[ArubaOwners] AS [Extent1]
) AS [Extent1]
GROUP BY [K1]
) AS [GroupBy1]";
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(p => p.FirstName).Select(g => g.Max(p => p.Id * 2));
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(p => p.FirstName).Select(g => g.Max(p => p.Id * 2)).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o == i);
}
}
[Fact]
public void GroupBy_is_optimized_when_projecting_expression_with_multiple_function_aggregates()
{
var expectedSql =
@"SELECT
1 AS [C1],
[GroupBy1].[A1] - [GroupBy1].[A2] AS [C2]
FROM ( SELECT
[Extent1].[FirstName] AS [K1],
MAX([Extent1].[Id]) AS [A1],
MIN([Extent1].[Id]) AS [A2]
FROM [dbo].[ArubaOwners] AS [Extent1]
GROUP BY [Extent1].[FirstName]
) AS [GroupBy1]";
using (var context = new ArubaContext())
{
var query = context.Owners.GroupBy(o => o.FirstName).Select(g => new { maxMinusMin = g.Max(p => p.Id) - g.Min(s => s.Id) });
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().GroupBy(o => o.FirstName).Select(g => new { maxMinusMin = g.Max(p => p.Id) - g.Min(s => s.Id) }).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o.maxMinusMin == i.maxMinusMin);
}
}
[Fact]
public void GroupBy_is_optimized_when_grouping_by_row_and_projecting_column_of_the_key_row()
{
var expectedSql =
@"SELECT
[Distinct1].[FirstName] AS [FirstName]
FROM ( SELECT DISTINCT
[Extent1].[FirstName] AS [FirstName]
FROM [dbo].[ArubaOwners] AS [Extent1]
WHERE [Extent1].[Id] < 4
) AS [Distinct1]";
using (var context = new ArubaContext())
{
var query = context.Owners.Where(o => o.Id < 4).GroupBy(g => new { g.FirstName }).Select(g => g.Key.FirstName);
QueryTestHelpers.VerifyDbQuery(query, expectedSql);
var results = query.ToList();
var expected = context.Owners.ToList().Where(o => o.Id < 4).GroupBy(g => new { g.FirstName }).Select(g => g.Key.FirstName).ToList();
QueryTestHelpers.VerifyQueryResult(expected, results, (o, i) => o == i);
}
}
}
}
|