-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
SimpleQuerySqlServerTest.cs
831 lines (718 loc) · 25.5 KB
/
SimpleQuerySqlServerTest.cs
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ReSharper disable InconsistentNaming
using Microsoft.Data.SqlClient;
namespace Microsoft.EntityFrameworkCore.Query;
public class SimpleQuerySqlServerTest : SimpleQueryRelationalTestBase
{
protected override ITestStoreFactory TestStoreFactory
=> SqlServerTestStoreFactory.Instance;
#region 27427
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Muliple_occurrences_of_FromSql_in_group_by_aggregate(bool async)
{
var contextFactory = await InitializeAsync<Context27427>();
using var context = contextFactory.CreateContext();
var query = context.DemoEntities
.FromSqlRaw("SELECT * FROM DemoEntities WHERE Id = {0}", new SqlParameter { Value = 1 })
.Select(e => e.Id);
var query2 = context.DemoEntities
.Where(e => query.Contains(e.Id))
.GroupBy(e => e.Id)
.Select(g => new { g.Key, Aggregate = g.Count() });
if (async)
{
await query2.ToListAsync();
}
else
{
query2.ToList();
}
AssertSql(
"""
p0='1'
SELECT [d].[Id] AS [Key], COUNT(*) AS [Aggregate]
FROM [DemoEntities] AS [d]
WHERE [d].[Id] IN (
SELECT [m].[Id]
FROM (
SELECT * FROM DemoEntities WHERE Id = @p0
) AS [m]
)
GROUP BY [d].[Id]
""");
}
protected class Context27427 : DbContext
{
public Context27427(DbContextOptions options)
: base(options)
{
}
public DbSet<DemoEntity> DemoEntities { get; set; }
}
protected class DemoEntity
{
public int Id { get; set; }
}
#endregion
#region 30478
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task TemporalAsOf_with_json_basic_query(bool async)
{
var contextFactory = await InitializeAsync<Context30478>(seed: x => x.Seed());
using var context = contextFactory.CreateContext();
var query = context.Entities.TemporalAsOf(new DateTime(2010, 1, 1));
var result = async
? await query.ToListAsync()
: query.ToList();
Assert.Equal(2, result.Count);
Assert.True(result.All(x => x.Reference != null));
Assert.True(result.All(x => x.Collection.Count > 0));
AssertSql(
"""
SELECT [e].[Id], [e].[Name], [e].[PeriodEnd], [e].[PeriodStart], [e].[Collection], [e].[Reference]
FROM [Entities] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [e]
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task TemporalAll_with_json_basic_query(bool async)
{
var contextFactory = await InitializeAsync<Context30478>(seed: x => x.Seed());
using var context = contextFactory.CreateContext();
var query = context.Entities.TemporalAll();
var result = async
? await query.ToListAsync()
: query.ToList();
Assert.Equal(2, result.Count);
Assert.True(result.All(x => x.Reference != null));
Assert.True(result.All(x => x.Collection.Count > 0));
AssertSql(
"""
SELECT [e].[Id], [e].[Name], [e].[PeriodEnd], [e].[PeriodStart], [e].[Collection], [e].[Reference]
FROM [Entities] FOR SYSTEM_TIME ALL AS [e]
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task TemporalAsOf_project_json_entity_reference(bool async)
{
var contextFactory = await InitializeAsync<Context30478>(seed: x => x.Seed());
using var context = contextFactory.CreateContext();
var query = context.Entities.TemporalAsOf(new DateTime(2010, 1, 1)).Select(x => x.Reference);
var result = async
? await query.ToListAsync()
: query.ToList();
Assert.Equal(2, result.Count);
Assert.True(result.All(x => x != null));
AssertSql(
"""
SELECT [e].[Reference], [e].[Id]
FROM [Entities] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [e]
""");
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task TemporalAsOf_project_json_entity_collection(bool async)
{
var contextFactory = await InitializeAsync<Context30478>(seed: x => x.Seed());
using var context = contextFactory.CreateContext();
var query = context.Entities.TemporalAsOf(new DateTime(2010, 1, 1)).Select(x => x.Collection);
var result = async
? await query.ToListAsync()
: query.ToList();
Assert.Equal(2, result.Count);
Assert.True(result.All(x => x.Count > 0));
AssertSql(
"""
SELECT [e].[Collection], [e].[Id]
FROM [Entities] FOR SYSTEM_TIME AS OF '2010-01-01T00:00:00.0000000' AS [e]
""");
}
protected class Context30478 : DbContext
{
public Context30478(DbContextOptions options)
: base(options)
{
}
public DbSet<Entity30478> Entities { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Entity30478>().Property(x => x.Id).ValueGeneratedNever();
modelBuilder.Entity<Entity30478>().ToTable("Entities", tb => tb.IsTemporal());
modelBuilder.Entity<Entity30478>().OwnsOne(x => x.Reference, nb =>
{
nb.ToJson();
nb.OwnsOne(x => x.Nested);
});
modelBuilder.Entity<Entity30478>().OwnsMany(x => x.Collection, nb =>
{
nb.ToJson();
nb.OwnsOne(x => x.Nested);
});
}
public void Seed()
{
var e1 = new Entity30478
{
Id = 1,
Name = "e1",
Reference = new Json30478
{
Name = "r1",
Nested = new JsonNested30478 { Number = 1 }
},
Collection = new List<Json30478>
{
new Json30478
{
Name = "c11",
Nested = new JsonNested30478 { Number = 11 }
},
new Json30478
{
Name = "c12",
Nested = new JsonNested30478 { Number = 12 }
},
new Json30478
{
Name = "c13",
Nested = new JsonNested30478 { Number = 12 }
}
}
};
var e2 = new Entity30478
{
Id = 2,
Name = "e2",
Reference = new Json30478
{
Name = "r2",
Nested = new JsonNested30478 { Number = 2 }
},
Collection = new List<Json30478>
{
new Json30478
{
Name = "c21",
Nested = new JsonNested30478 { Number = 21 }
},
new Json30478
{
Name = "c22",
Nested = new JsonNested30478 { Number = 22 }
},
}
};
AddRange(e1, e2);
SaveChanges();
RemoveRange(e1, e2);
SaveChanges();
Database.ExecuteSqlRaw($"ALTER TABLE [Entities] SET (SYSTEM_VERSIONING = OFF)");
Database.ExecuteSqlRaw($"ALTER TABLE [Entities] DROP PERIOD FOR SYSTEM_TIME");
Database.ExecuteSqlRaw($"UPDATE [EntitiesHistory] SET PeriodStart = '2000-01-01T01:00:00.0000000Z'");
Database.ExecuteSqlRaw($"UPDATE [EntitiesHistory] SET PeriodEnd = '2020-07-01T07:00:00.0000000Z'");
Database.ExecuteSqlRaw($"ALTER TABLE [Entities] ADD PERIOD FOR SYSTEM_TIME ([PeriodStart], [PeriodEnd])");
Database.ExecuteSqlRaw($"ALTER TABLE [Entities] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[EntitiesHistory]))");
}
}
protected class Entity30478
{
public int Id { get; set; }
public string Name { get; set; }
public Json30478 Reference { get; set; }
public List<Json30478> Collection { get; set; }
}
protected class Json30478
{
public string Name { get; set; }
public JsonNested30478 Nested { get; set; }
}
protected class JsonNested30478
{
public int Number { get; set; }
}
#endregion
public override async Task Multiple_nested_reference_navigations(bool async)
{
await base.Multiple_nested_reference_navigations(async);
AssertSql(
"""
@__p_0='3'
SELECT TOP(1) [s].[Id], [s].[Email], [s].[Logon], [s].[ManagerId], [s].[Name], [s].[SecondaryManagerId]
FROM [Staff] AS [s]
WHERE [s].[Id] = @__p_0
""",
//
"""
@__id_0='1'
SELECT TOP(2) [a].[Id], [a].[Complete], [a].[Deleted], [a].[PeriodEnd], [a].[PeriodStart], [a].[StaffId], [s].[Id], [s].[Email], [s].[Logon], [s].[ManagerId], [s].[Name], [s].[SecondaryManagerId], [s0].[Id], [s0].[Email], [s0].[Logon], [s0].[ManagerId], [s0].[Name], [s0].[SecondaryManagerId], [s1].[Id], [s1].[Email], [s1].[Logon], [s1].[ManagerId], [s1].[Name], [s1].[SecondaryManagerId]
FROM [Appraisals] AS [a]
INNER JOIN [Staff] AS [s] ON [a].[StaffId] = [s].[Id]
LEFT JOIN [Staff] AS [s0] ON [s].[ManagerId] = [s0].[Id]
LEFT JOIN [Staff] AS [s1] ON [s].[SecondaryManagerId] = [s1].[Id]
WHERE [a].[Id] = @__id_0
""");
}
public override async Task Comparing_enum_casted_to_byte_with_int_parameter(bool async)
{
await base.Comparing_enum_casted_to_byte_with_int_parameter(async);
AssertSql(
"""
@__bitterTaste_0='1'
SELECT [i].[IceCreamId], [i].[Name], [i].[Taste]
FROM [IceCreams] AS [i]
WHERE [i].[Taste] = @__bitterTaste_0
""");
}
public override async Task Comparing_enum_casted_to_byte_with_int_constant(bool async)
{
await base.Comparing_enum_casted_to_byte_with_int_constant(async);
AssertSql(
"""
SELECT [i].[IceCreamId], [i].[Name], [i].[Taste]
FROM [IceCreams] AS [i]
WHERE [i].[Taste] = 1
""");
}
public override async Task Comparing_byte_column_to_enum_in_vb_creating_double_cast(bool async)
{
await base.Comparing_byte_column_to_enum_in_vb_creating_double_cast(async);
AssertSql(
"""
SELECT [f].[Id], [f].[Taste]
FROM [Food] AS [f]
WHERE [f].[Taste] = CAST(1 AS tinyint)
""");
}
public override async Task Null_check_removal_in_ternary_maintain_appropriate_cast(bool async)
{
await base.Null_check_removal_in_ternary_maintain_appropriate_cast(async);
AssertSql(
"""
SELECT CAST([f].[Taste] AS tinyint) AS [Bar]
FROM [Food] AS [f]
""");
}
public override async Task Bool_discriminator_column_works(bool async)
{
await base.Bool_discriminator_column_works(async);
AssertSql(
"""
SELECT [a].[Id], [a].[BlogId], [b].[Id], [b].[IsPhotoBlog], [b].[Title], [b].[NumberOfPhotos]
FROM [Authors] AS [a]
LEFT JOIN [Blog] AS [b] ON [a].[BlogId] = [b].[Id]
""");
}
public override async Task Count_member_over_IReadOnlyCollection_works(bool async)
{
await base.Count_member_over_IReadOnlyCollection_works(async);
AssertSql(
"""
SELECT (
SELECT COUNT(*)
FROM [Books] AS [b]
WHERE [a].[AuthorId] = [b].[AuthorId]) AS [BooksCount]
FROM [Authors] AS [a]
""");
}
public override async Task Multiple_different_entity_type_from_different_namespaces(bool async)
{
await base.Multiple_different_entity_type_from_different_namespaces(async);
AssertSql(
"""
SELECT cast(null as int) AS MyValue
""");
}
public override async Task Unwrap_convert_node_over_projection_when_translating_contains_over_subquery(bool async)
{
await base.Unwrap_convert_node_over_projection_when_translating_contains_over_subquery(async);
AssertSql(
"""
@__currentUserId_0='1'
SELECT CASE
WHEN [u].[Id] IN (
SELECT [u0].[Id]
FROM [Memberships] AS [m]
INNER JOIN [Users] AS [u0] ON [m].[UserId] = [u0].[Id]
WHERE [m].[GroupId] IN (
SELECT [m0].[GroupId]
FROM [Memberships] AS [m0]
WHERE [m0].[UserId] = @__currentUserId_0
)
) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END AS [HasAccess]
FROM [Users] AS [u]
""");
}
public override async Task Unwrap_convert_node_over_projection_when_translating_contains_over_subquery_2(bool async)
{
await base.Unwrap_convert_node_over_projection_when_translating_contains_over_subquery_2(async);
AssertSql(
"""
@__currentUserId_0='1'
SELECT CASE
WHEN [u].[Id] IN (
SELECT [u0].[Id]
FROM [Memberships] AS [m]
INNER JOIN [Groups] AS [g] ON [m].[GroupId] = [g].[Id]
INNER JOIN [Users] AS [u0] ON [m].[UserId] = [u0].[Id]
WHERE [g].[Id] IN (
SELECT [g0].[Id]
FROM [Memberships] AS [m0]
INNER JOIN [Groups] AS [g0] ON [m0].[GroupId] = [g0].[Id]
WHERE [m0].[UserId] = @__currentUserId_0
)
) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END AS [HasAccess]
FROM [Users] AS [u]
""");
}
public override async Task Unwrap_convert_node_over_projection_when_translating_contains_over_subquery_3(bool async)
{
await base.Unwrap_convert_node_over_projection_when_translating_contains_over_subquery_3(async);
AssertSql(
"""
@__currentUserId_0='1'
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [Memberships] AS [m]
INNER JOIN [Users] AS [u0] ON [m].[UserId] = [u0].[Id]
WHERE [m].[GroupId] IN (
SELECT [m0].[GroupId]
FROM [Memberships] AS [m0]
WHERE [m0].[UserId] = @__currentUserId_0
) AND [u0].[Id] = [u].[Id]) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END AS [HasAccess]
FROM [Users] AS [u]
""");
}
public override async Task GroupBy_aggregate_on_right_side_of_join(bool async)
{
await base.GroupBy_aggregate_on_right_side_of_join(async);
AssertSql(
"""
@__orderId_0='123456'
SELECT [o].[Id], [o].[CancellationDate], [o].[OrderId], [o].[ShippingDate]
FROM [OrderItems] AS [o]
INNER JOIN (
SELECT [o0].[OrderId] AS [Key], MAX(CASE
WHEN [o0].[ShippingDate] IS NULL AND [o0].[CancellationDate] IS NULL THEN [o0].[OrderId]
ELSE [o0].[OrderId] - 10000000
END) AS [IsPending]
FROM [OrderItems] AS [o0]
WHERE [o0].[OrderId] = @__orderId_0
GROUP BY [o0].[OrderId]
) AS [t] ON [o].[OrderId] = [t].[Key]
WHERE [o].[OrderId] = @__orderId_0
ORDER BY [o].[OrderId]
""");
}
public override async Task Enum_with_value_converter_matching_take_value(bool async)
{
await base.Enum_with_value_converter_matching_take_value(async);
AssertSql(
"""
@__orderItemType_1='MyType1' (Nullable = false) (Size = 4000)
@__p_0='1'
SELECT [o1].[Id], COALESCE((
SELECT TOP(1) [o2].[Price]
FROM [OrderItems] AS [o2]
WHERE [o1].[Id] = [o2].[Order26472Id] AND [o2].[Type] = @__orderItemType_1), 0.0E0) AS [SpecialSum]
FROM (
SELECT TOP(@__p_0) [o].[Id]
FROM [Orders] AS [o]
WHERE EXISTS (
SELECT 1
FROM [OrderItems] AS [o0]
WHERE [o].[Id] = [o0].[Order26472Id])
ORDER BY [o].[Id]
) AS [t]
INNER JOIN [Orders] AS [o1] ON [t].[Id] = [o1].[Id]
ORDER BY [t].[Id]
""");
}
public override async Task GroupBy_Aggregate_over_navigations_repeated(bool async)
{
await base.GroupBy_Aggregate_over_navigations_repeated(async);
AssertSql(
"""
SELECT (
SELECT MIN([o].[HourlyRate])
FROM [TimeSheets] AS [t0]
LEFT JOIN [Order] AS [o] ON [t0].[OrderId] = [o].[Id]
WHERE [t0].[OrderId] IS NOT NULL AND [t].[OrderId] = [t0].[OrderId]) AS [HourlyRate], (
SELECT MIN([c].[Id])
FROM [TimeSheets] AS [t1]
INNER JOIN [Project] AS [p] ON [t1].[ProjectId] = [p].[Id]
INNER JOIN [Customers] AS [c] ON [p].[CustomerId] = [c].[Id]
WHERE [t1].[OrderId] IS NOT NULL AND [t].[OrderId] = [t1].[OrderId]) AS [CustomerId], (
SELECT MIN([c0].[Name])
FROM [TimeSheets] AS [t2]
INNER JOIN [Project] AS [p0] ON [t2].[ProjectId] = [p0].[Id]
INNER JOIN [Customers] AS [c0] ON [p0].[CustomerId] = [c0].[Id]
WHERE [t2].[OrderId] IS NOT NULL AND [t].[OrderId] = [t2].[OrderId]) AS [CustomerName]
FROM [TimeSheets] AS [t]
WHERE [t].[OrderId] IS NOT NULL
GROUP BY [t].[OrderId]
""");
}
public override async Task Aggregate_over_subquery_in_group_by_projection(bool async)
{
await base.Aggregate_over_subquery_in_group_by_projection(async);
AssertSql(
"""
SELECT [o].[CustomerId], (
SELECT MIN([o0].[HourlyRate])
FROM [Order] AS [o0]
WHERE [o0].[CustomerId] = [o].[CustomerId]) AS [CustomerMinHourlyRate], MIN([o].[HourlyRate]) AS [HourlyRate], COUNT(*) AS [Count]
FROM [Order] AS [o]
WHERE [o].[Number] <> N'A1' OR [o].[Number] IS NULL
GROUP BY [o].[CustomerId], [o].[Number]
""");
}
public override async Task Aggregate_over_subquery_in_group_by_projection_2(bool async)
{
await base.Aggregate_over_subquery_in_group_by_projection_2(async);
AssertSql(
"""
SELECT [t].[Value] AS [A], (
SELECT MAX([t0].[Id])
FROM [Table] AS [t0]
WHERE [t0].[Value] = MAX([t].[Id]) * 6 OR ([t0].[Value] IS NULL AND MAX([t].[Id]) IS NULL)) AS [B]
FROM [Table] AS [t]
GROUP BY [t].[Value]
""");
}
public override async Task Group_by_aggregate_in_subquery_projection_after_group_by(bool async)
{
await base.Group_by_aggregate_in_subquery_projection_after_group_by(async);
AssertSql(
"""
SELECT [t].[Value] AS [A], COALESCE(SUM([t].[Id]), 0) AS [B], COALESCE((
SELECT TOP(1) COALESCE(SUM([t].[Id]), 0) + COALESCE(SUM([t0].[Id]), 0)
FROM [Table] AS [t0]
GROUP BY [t0].[Value]
ORDER BY (SELECT 1)), 0) AS [C]
FROM [Table] AS [t]
GROUP BY [t].[Value]
""");
}
public override async Task Group_by_multiple_aggregate_joining_different_tables(bool async)
{
await base.Group_by_multiple_aggregate_joining_different_tables(async);
AssertSql(
"""
SELECT (
SELECT COUNT(*)
FROM (
SELECT DISTINCT [c].[Value1]
FROM (
SELECT [p0].[Id], [p0].[Child1Id], [p0].[Child2Id], [p0].[ChildFilter1Id], [p0].[ChildFilter2Id], 1 AS [Key]
FROM [Parents] AS [p0]
) AS [t1]
LEFT JOIN [Child1] AS [c] ON [t1].[Child1Id] = [c].[Id]
WHERE [t].[Key] = [t1].[Key]
) AS [t0]) AS [Test1], (
SELECT COUNT(*)
FROM (
SELECT DISTINCT [c0].[Value2]
FROM (
SELECT [p1].[Id], [p1].[Child1Id], [p1].[Child2Id], [p1].[ChildFilter1Id], [p1].[ChildFilter2Id], 1 AS [Key]
FROM [Parents] AS [p1]
) AS [t3]
LEFT JOIN [Child2] AS [c0] ON [t3].[Child2Id] = [c0].[Id]
WHERE [t].[Key] = [t3].[Key]
) AS [t2]) AS [Test2]
FROM (
SELECT 1 AS [Key]
FROM [Parents] AS [p]
) AS [t]
GROUP BY [t].[Key]
""");
}
public override async Task Group_by_multiple_aggregate_joining_different_tables_with_query_filter(bool async)
{
await base.Group_by_multiple_aggregate_joining_different_tables_with_query_filter(async);
AssertSql(
"""
SELECT (
SELECT COUNT(*)
FROM (
SELECT DISTINCT [t2].[Value1]
FROM (
SELECT [p0].[Id], [p0].[Child1Id], [p0].[Child2Id], [p0].[ChildFilter1Id], [p0].[ChildFilter2Id], 1 AS [Key]
FROM [Parents] AS [p0]
) AS [t0]
LEFT JOIN (
SELECT [c].[Id], [c].[Filter1], [c].[Value1]
FROM [ChildFilter1] AS [c]
WHERE [c].[Filter1] = N'Filter1'
) AS [t2] ON [t0].[ChildFilter1Id] = [t2].[Id]
WHERE [t].[Key] = [t0].[Key]
) AS [t1]) AS [Test1], (
SELECT COUNT(*)
FROM (
SELECT DISTINCT [t5].[Value2]
FROM (
SELECT [p1].[Id], [p1].[Child1Id], [p1].[Child2Id], [p1].[ChildFilter1Id], [p1].[ChildFilter2Id], 1 AS [Key]
FROM [Parents] AS [p1]
) AS [t4]
LEFT JOIN (
SELECT [c0].[Id], [c0].[Filter2], [c0].[Value2]
FROM [ChildFilter2] AS [c0]
WHERE [c0].[Filter2] = N'Filter2'
) AS [t5] ON [t4].[ChildFilter2Id] = [t5].[Id]
WHERE [t].[Key] = [t4].[Key]
) AS [t3]) AS [Test2]
FROM (
SELECT 1 AS [Key]
FROM [Parents] AS [p]
) AS [t]
GROUP BY [t].[Key]
""");
}
public override async Task Subquery_first_member_compared_to_null(bool async)
{
await base.Subquery_first_member_compared_to_null(async);
AssertSql(
"""
SELECT (
SELECT TOP(1) [c1].[SomeOtherNullableDateTime]
FROM [Child26744] AS [c1]
WHERE [p].[Id] = [c1].[ParentId] AND [c1].[SomeNullableDateTime] IS NULL
ORDER BY [c1].[SomeInteger])
FROM [Parents] AS [p]
WHERE EXISTS (
SELECT 1
FROM [Child26744] AS [c]
WHERE [p].[Id] = [c].[ParentId] AND [c].[SomeNullableDateTime] IS NULL) AND (
SELECT TOP(1) [c0].[SomeOtherNullableDateTime]
FROM [Child26744] AS [c0]
WHERE [p].[Id] = [c0].[ParentId] AND [c0].[SomeNullableDateTime] IS NULL
ORDER BY [c0].[SomeInteger]) IS NOT NULL
""");
}
public override async Task SelectMany_where_Select(bool async)
{
await base.SelectMany_where_Select(async);
AssertSql(
"""
SELECT [t0].[SomeNullableDateTime]
FROM [Parents] AS [p]
INNER JOIN (
SELECT [t].[ParentId], [t].[SomeNullableDateTime], [t].[SomeOtherNullableDateTime]
FROM (
SELECT [c].[ParentId], [c].[SomeNullableDateTime], [c].[SomeOtherNullableDateTime], ROW_NUMBER() OVER(PARTITION BY [c].[ParentId] ORDER BY [c].[SomeInteger]) AS [row]
FROM [Child26744] AS [c]
WHERE [c].[SomeNullableDateTime] IS NULL
) AS [t]
WHERE [t].[row] <= 1
) AS [t0] ON [p].[Id] = [t0].[ParentId]
WHERE [t0].[SomeOtherNullableDateTime] IS NOT NULL
""");
}
public override async Task StoreType_for_UDF_used(bool async)
{
await base.StoreType_for_UDF_used(async);
AssertSql(
"""
@__date_0='2012-12-12T00:00:00.0000000' (DbType = DateTime)
SELECT [m].[Id], [m].[SomeDate]
FROM [MyEntities] AS [m]
WHERE [m].[SomeDate] = @__date_0
""",
//
"""
@__date_0='2012-12-12T00:00:00.0000000' (DbType = DateTime)
SELECT [m].[Id], [m].[SomeDate]
FROM [MyEntities] AS [m]
WHERE [dbo].[ModifyDate]([m].[SomeDate]) = @__date_0
""");
}
public override async Task Pushdown_does_not_add_grouping_key_to_projection_when_distinct_is_applied(bool async)
{
await base.Pushdown_does_not_add_grouping_key_to_projection_when_distinct_is_applied(async);
AssertSql(
"""
@__p_0='123456'
SELECT TOP(@__p_0) [t].[JSON]
FROM [TableData] AS [t]
INNER JOIN (
SELECT DISTINCT [i].[Parcel]
FROM [IndexData] AS [i]
WHERE [i].[Parcel] = N'some condition'
GROUP BY [i].[Parcel], [i].[RowId]
HAVING COUNT(*) = 1
) AS [t0] ON [t].[ParcelNumber] = [t0].[Parcel]
WHERE [t].[TableId] = 123
ORDER BY [t].[ParcelNumber]
""");
}
public override async Task Hierarchy_query_with_abstract_type_sibling(bool async)
{
await base.Hierarchy_query_with_abstract_type_sibling(async);
AssertSql(
"""
SELECT [a].[Id], [a].[Discriminator], [a].[Species], [a].[Name], [a].[EdcuationLevel], [a].[FavoriteToy]
FROM [Animals] AS [a]
WHERE [a].[Discriminator] IN (N'Cat', N'Dog') AND [a].[Species] LIKE N'F%'
""");
}
public override async Task Hierarchy_query_with_abstract_type_sibling_TPT(bool async)
{
await base.Hierarchy_query_with_abstract_type_sibling_TPT(async);
AssertSql(
"""
SELECT [a].[Id], [a].[Species], [p].[Name], [c].[EdcuationLevel], [d].[FavoriteToy], CASE
WHEN [d].[Id] IS NOT NULL THEN N'Dog'
WHEN [c].[Id] IS NOT NULL THEN N'Cat'
END AS [Discriminator]
FROM [Animals] AS [a]
LEFT JOIN [Pets] AS [p] ON [a].[Id] = [p].[Id]
LEFT JOIN [Cats] AS [c] ON [a].[Id] = [c].[Id]
LEFT JOIN [Dogs] AS [d] ON [a].[Id] = [d].[Id]
WHERE ([d].[Id] IS NOT NULL OR [c].[Id] IS NOT NULL) AND [a].[Species] LIKE N'F%'
""");
}
public override async Task Hierarchy_query_with_abstract_type_sibling_TPC(bool async)
{
await base.Hierarchy_query_with_abstract_type_sibling_TPC(async);
AssertSql(
"""
SELECT [t].[Id], [t].[Species], [t].[Name], [t].[EdcuationLevel], [t].[FavoriteToy], [t].[Discriminator]
FROM (
SELECT [c].[Id], [c].[Species], [c].[Name], [c].[EdcuationLevel], NULL AS [FavoriteToy], N'Cat' AS [Discriminator]
FROM [Cats] AS [c]
UNION ALL
SELECT [d].[Id], [d].[Species], [d].[Name], NULL AS [EdcuationLevel], [d].[FavoriteToy], N'Dog' AS [Discriminator]
FROM [Dogs] AS [d]
) AS [t]
WHERE [t].[Species] LIKE N'F%'
""");
}
public override async Task Filter_on_nested_DTO_with_interface_gets_simplified_correctly(bool async)
{
await base.Filter_on_nested_DTO_with_interface_gets_simplified_correctly(async);
AssertSql(
"""
SELECT [c].[Id], [c].[CompanyId], CASE
WHEN [c0].[Id] IS NOT NULL THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END, [c0].[Id], [c0].[CompanyName], [c0].[CountryId], [c1].[Id], [c1].[CountryName]
FROM [Customers] AS [c]
LEFT JOIN [Companies] AS [c0] ON [c].[CompanyId] = [c0].[Id]
LEFT JOIN [Countries] AS [c1] ON [c0].[CountryId] = [c1].[Id]
WHERE CASE
WHEN [c0].[Id] IS NOT NULL THEN [c1].[CountryName]
ELSE NULL
END = N'COUNTRY'
""");
}
}