|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Behavioral tests for <c>CROSS APPLY</c> and <c>OUTER APPLY</c>: the right |
| 8 | +/// side is a correlated derived table re-executed per left-side row, with |
| 9 | +/// the outer row's columns visible inside the inner SELECT's WHERE / |
| 10 | +/// projection. CROSS APPLY drops outer rows whose lateral plan yields zero |
| 11 | +/// rows; OUTER APPLY null-fills the right side. The shape EF Core 10 emits |
| 12 | +/// for <c>SelectMany</c> over a filtered correlated child collection. |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class ApplyTests |
| 16 | +{ |
| 17 | + private static DbConnection SeededBlogsPosts() |
| 18 | + { |
| 19 | + var connection = new Simulation().CreateOpenConnection(); |
| 20 | + _ = connection.CreateCommand("create table blogs (id int, title varchar(20))").ExecuteNonQuery(); |
| 21 | + _ = connection.CreateCommand("create table posts (id int, blog_id int, title varchar(20), score int)").ExecuteNonQuery(); |
| 22 | + _ = connection.CreateCommand("insert into blogs values (1, 'A'), (2, 'B'), (3, 'C')").ExecuteNonQuery(); |
| 23 | + // Blog 1: 3 posts. Blog 2: 1 low-score post. Blog 3: no posts. |
| 24 | + _ = connection.CreateCommand( |
| 25 | + "insert into posts values " + |
| 26 | + "(1, 1, 'P1', 10), (2, 1, 'P2', 20), (3, 1, 'P3', 30), " + |
| 27 | + "(4, 2, 'P4', 5)").ExecuteNonQuery(); |
| 28 | + return connection; |
| 29 | + } |
| 30 | + |
| 31 | + // === CROSS APPLY === |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void CrossApply_FilteredChild_EmitsMatchingPairs() |
| 35 | + { |
| 36 | + // The shape EF Core 10 emits for SelectMany of a filtered nav. |
| 37 | + using var connection = SeededBlogsPosts(); |
| 38 | + using var reader = connection.CreateCommand( |
| 39 | + "select p0.bt, p0.pt from blogs as b " + |
| 40 | + "cross apply (select b.title as bt, p.title as pt from posts as p " + |
| 41 | + " where b.id = p.blog_id and p.score > 10) as p0").ExecuteReader(); |
| 42 | + var rows = new List<(string, string)>(); |
| 43 | + while (reader.Read()) |
| 44 | + rows.Add((reader.GetString(0), reader.GetString(1))); |
| 45 | + CollectionAssert.AreEquivalent(new[] { ("A", "P2"), ("A", "P3") }, rows); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void CrossApply_ZeroMatches_DropsOuterRow() |
| 50 | + { |
| 51 | + // Blog 3 has no posts; CROSS APPLY drops it (INNER-style). |
| 52 | + using var connection = SeededBlogsPosts(); |
| 53 | + var ids = new List<int>(); |
| 54 | + using var reader = connection.CreateCommand( |
| 55 | + "select b.id from blogs as b " + |
| 56 | + "cross apply (select 1 as marker from posts as p where p.blog_id = b.id) as p0").ExecuteReader(); |
| 57 | + while (reader.Read()) ids.Add(reader.GetInt32(0)); |
| 58 | + CollectionAssert.AreEquivalent(new[] { 1, 1, 1, 2 }, ids); |
| 59 | + } |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public void CrossApply_ExpressionOverOuterColumn() |
| 63 | + { |
| 64 | + // Probe #9 shape: lateral WHERE references outer column in arithmetic. |
| 65 | + using var connection = SeededBlogsPosts(); |
| 66 | + var rows = new List<(string, int)>(); |
| 67 | + using var reader = connection.CreateCommand( |
| 68 | + "select b.title, p0.score from blogs as b " + |
| 69 | + "cross apply (select p.score from posts as p " + |
| 70 | + " where b.id = p.blog_id and p.score >= b.id * 5) as p0").ExecuteReader(); |
| 71 | + while (reader.Read()) |
| 72 | + rows.Add((reader.GetString(0), reader.GetInt32(1))); |
| 73 | + // Blog 1 (id=1, threshold=5): 10, 20, 30. Blog 2 (id=2, threshold=10): 5 fails. |
| 74 | + CollectionAssert.AreEquivalent(new[] { ("A", 10), ("A", 20), ("A", 30) }, rows); |
| 75 | + } |
| 76 | + |
| 77 | + [TestMethod] |
| 78 | + public void CrossApply_TopN_PerOuterRow() |
| 79 | + { |
| 80 | + // Top-2 posts by score per blog. |
| 81 | + using var connection = SeededBlogsPosts(); |
| 82 | + var rows = new List<(int, string)>(); |
| 83 | + using var reader = connection.CreateCommand( |
| 84 | + "select b.id, p0.title from blogs as b " + |
| 85 | + "cross apply (select top(2) p.title, p.score from posts as p " + |
| 86 | + " where p.blog_id = b.id order by p.score desc) as p0").ExecuteReader(); |
| 87 | + while (reader.Read()) |
| 88 | + rows.Add((reader.GetInt32(0), reader.GetString(1))); |
| 89 | + CollectionAssert.AreEquivalent(new[] { (1, "P3"), (1, "P2"), (2, "P4") }, rows); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public void CrossApply_NoOnPredicateAccepted() |
| 94 | + { |
| 95 | + // APPLY explicitly forbids ON; the inner WHERE provides correlation. |
| 96 | + // This test just confirms that APPLY without ON parses and runs. |
| 97 | + using var connection = SeededBlogsPosts(); |
| 98 | + using var reader = connection.CreateCommand( |
| 99 | + "select b.id, p0.score from blogs as b " + |
| 100 | + "cross apply (select p.score from posts as p where p.blog_id = b.id) as p0 " + |
| 101 | + "order by b.id, p0.score").ExecuteReader(); |
| 102 | + var rows = new List<(int, int)>(); |
| 103 | + while (reader.Read()) rows.Add((reader.GetInt32(0), reader.GetInt32(1))); |
| 104 | + CollectionAssert.AreEqual(new[] { (1, 10), (1, 20), (1, 30), (2, 5) }, rows); |
| 105 | + } |
| 106 | + |
| 107 | + // === OUTER APPLY === |
| 108 | + |
| 109 | + [TestMethod] |
| 110 | + public void OuterApply_ZeroMatches_NullFillsRight() |
| 111 | + { |
| 112 | + // Blog 3 has no posts; OUTER APPLY emits one NULL row for it. |
| 113 | + using var connection = SeededBlogsPosts(); |
| 114 | + var rows = new List<(int Id, int? Score)>(); |
| 115 | + using var reader = connection.CreateCommand( |
| 116 | + "select b.id, p0.score from blogs as b " + |
| 117 | + "outer apply (select p.score from posts as p where p.blog_id = b.id) as p0 " + |
| 118 | + "order by b.id, p0.score").ExecuteReader(); |
| 119 | + while (reader.Read()) |
| 120 | + rows.Add((reader.GetInt32(0), reader.IsDBNull(1) ? null : reader.GetInt32(1))); |
| 121 | + var expected = new List<(int Id, int? Score)> { (1, 10), (1, 20), (1, 30), (2, 5), (3, null) }; |
| 122 | + CollectionAssert.AreEqual(expected, rows); |
| 123 | + } |
| 124 | + |
| 125 | + [TestMethod] |
| 126 | + public void OuterApply_AllOuterRowsAppearAtLeastOnce() |
| 127 | + { |
| 128 | + using var connection = SeededBlogsPosts(); |
| 129 | + var ids = new HashSet<int>(); |
| 130 | + using var reader = connection.CreateCommand( |
| 131 | + "select b.id from blogs as b " + |
| 132 | + "outer apply (select p.id from posts as p where p.blog_id = b.id and p.score > 999) as p0").ExecuteReader(); |
| 133 | + while (reader.Read()) _ = ids.Add(reader.GetInt32(0)); |
| 134 | + CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, ids.ToArray()); |
| 135 | + } |
| 136 | + |
| 137 | + // === Chained APPLY === |
| 138 | + |
| 139 | + [TestMethod] |
| 140 | + public void CrossApply_MultipleChained_CorrelateLeftToRight() |
| 141 | + { |
| 142 | + // Two APPLYs in a row: the second references both b and p0. |
| 143 | + // Blog 1 (scores 10, 20, 30): p0=10 → p1∈{10,20,30}; p0=20 → p1∈{20,30}; p0=30 → p1∈{30}. |
| 144 | + // Blog 2 (score 5): p0=5 → p1∈{5}. Blog 3 has no posts → no rows. |
| 145 | + using var connection = SeededBlogsPosts(); |
| 146 | + var triples = new List<(int, int, int)>(); |
| 147 | + using var reader = connection.CreateCommand( |
| 148 | + "select b.id, p0.score, p1.score from blogs as b " + |
| 149 | + "cross apply (select p.score from posts as p where p.blog_id = b.id) as p0 " + |
| 150 | + "cross apply (select q.score from posts as q where q.blog_id = b.id and q.score >= p0.score) as p1").ExecuteReader(); |
| 151 | + while (reader.Read()) |
| 152 | + triples.Add((reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2))); |
| 153 | + CollectionAssert.AreEquivalent( |
| 154 | + new[] { (1, 10, 10), (1, 10, 20), (1, 10, 30), (1, 20, 20), (1, 20, 30), (1, 30, 30), (2, 5, 5) }, |
| 155 | + triples); |
| 156 | + } |
| 157 | + |
| 158 | + // === Parser rejections === |
| 159 | + |
| 160 | + [TestMethod] |
| 161 | + public void Apply_RejectsOnClause() |
| 162 | + { |
| 163 | + // CROSS APPLY ... ON ... is not valid syntax (no ON for APPLY). |
| 164 | + using var connection = SeededBlogsPosts(); |
| 165 | + _ = Throws<DbException>(() => |
| 166 | + _ = connection.CreateCommand( |
| 167 | + "select 1 from blogs as b " + |
| 168 | + "cross apply (select 1 from posts as p where p.blog_id = b.id) as p0 " + |
| 169 | + "on b.id = p0.id").ExecuteScalar()); |
| 170 | + } |
| 171 | + |
| 172 | + [TestMethod] |
| 173 | + public void Apply_RequiresParenthesizedSelect() |
| 174 | + { |
| 175 | + using var connection = SeededBlogsPosts(); |
| 176 | + _ = Throws<DbException>(() => |
| 177 | + _ = connection.CreateCommand( |
| 178 | + "select 1 from blogs as b cross apply posts as p").ExecuteScalar()); |
| 179 | + } |
| 180 | + |
| 181 | + [TestMethod] |
| 182 | + public void OuterApply_AsLeadingKeyword_RequiresApply() |
| 183 | + { |
| 184 | + // OUTER alone (without LEFT/RIGHT/FULL preceding) only forms OUTER APPLY. |
| 185 | + using var connection = SeededBlogsPosts(); |
| 186 | + _ = Throws<DbException>(() => |
| 187 | + _ = connection.CreateCommand( |
| 188 | + "select 1 from blogs as b outer join posts as p on b.id = p.blog_id").ExecuteScalar()); |
| 189 | + } |
| 190 | +} |
0 commit comments