|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Behavioral tests for SQL Server's set operators: <c>UNION</c> / |
| 8 | +/// <c>UNION ALL</c> / <c>INTERSECT</c> / <c>EXCEPT</c>. Covers dedup |
| 9 | +/// semantics (NULL-equals-NULL, opposite of <c>=</c>'s tri-state |
| 10 | +/// behavior), type promotion across branches, the precedence rule |
| 11 | +/// (INTERSECT binds tighter than UNION/EXCEPT), Msg 205 on column-count |
| 12 | +/// mismatch, Msg 156 on per-branch ORDER BY, and the top-level ORDER BY |
| 13 | +/// that applies post-chain. Sourced from probes against SQL Server 2025. |
| 14 | +/// </summary> |
| 15 | +[TestClass] |
| 16 | +public sealed class SetOperationTests |
| 17 | +{ |
| 18 | + private static List<int> ReadInts(DbCommand command) |
| 19 | + { |
| 20 | + using var reader = command.ExecuteReader(); |
| 21 | + var values = new List<int>(); |
| 22 | + while (reader.Read()) |
| 23 | + values.Add(reader.GetInt32(0)); |
| 24 | + return values; |
| 25 | + } |
| 26 | + |
| 27 | + // === UNION / UNION ALL === |
| 28 | + |
| 29 | + [TestMethod] |
| 30 | + public void Union_Dedupes() |
| 31 | + { |
| 32 | + var values = ReadInts(new Simulation().CreateCommand("select 1 union select 2 union select 1")); |
| 33 | + CollectionAssert.AreEquivalent(new[] { 1, 2 }, values); |
| 34 | + } |
| 35 | + |
| 36 | + [TestMethod] |
| 37 | + public void UnionAll_PreservesDuplicates() |
| 38 | + { |
| 39 | + var values = ReadInts(new Simulation().CreateCommand("select 1 union all select 2 union all select 1")); |
| 40 | + CollectionAssert.AreEqual(new[] { 1, 2, 1 }, values); |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + public void Union_NullsCompareEqual_DedupedToSingleRow() |
| 45 | + { |
| 46 | + // SET ops treat NULLs as equal — opposite of `=` operator's UNKNOWN. |
| 47 | + using var connection = new Simulation().CreateOpenConnection(); |
| 48 | + using var reader = connection.CreateCommand( |
| 49 | + "select cast(null as int) union select cast(null as int)").ExecuteReader(); |
| 50 | + var rows = 0; |
| 51 | + while (reader.Read()) |
| 52 | + rows++; |
| 53 | + AreEqual(1, rows); |
| 54 | + } |
| 55 | + |
| 56 | + // === INTERSECT === |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void Intersect_KeepsCommonRows() |
| 60 | + { |
| 61 | + var values = ReadInts(new Simulation().CreateCommand("select 1 intersect select 1")); |
| 62 | + CollectionAssert.AreEqual(new[] { 1 }, values); |
| 63 | + } |
| 64 | + |
| 65 | + [TestMethod] |
| 66 | + public void Intersect_NoOverlap_Empty() |
| 67 | + { |
| 68 | + var values = ReadInts(new Simulation().CreateCommand("select 1 intersect select 2")); |
| 69 | + IsEmpty(values); |
| 70 | + } |
| 71 | + |
| 72 | + [TestMethod] |
| 73 | + public void Intersect_NullsMatch() |
| 74 | + { |
| 75 | + using var connection = new Simulation().CreateOpenConnection(); |
| 76 | + using var reader = connection.CreateCommand( |
| 77 | + "select cast(null as int) intersect select cast(null as int)").ExecuteReader(); |
| 78 | + var rows = 0; |
| 79 | + while (reader.Read()) rows++; |
| 80 | + AreEqual(1, rows); |
| 81 | + } |
| 82 | + |
| 83 | + // === EXCEPT === |
| 84 | + |
| 85 | + [TestMethod] |
| 86 | + public void Except_RemovesRightSide() |
| 87 | + { |
| 88 | + var values = ReadInts(new Simulation().CreateCommand("select 1 except select 2")); |
| 89 | + CollectionAssert.AreEqual(new[] { 1 }, values); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public void Except_AllRemoved_Empty() |
| 94 | + { |
| 95 | + var values = ReadInts(new Simulation().CreateCommand("select 1 except select 1")); |
| 96 | + IsEmpty(values); |
| 97 | + } |
| 98 | + |
| 99 | + [TestMethod] |
| 100 | + public void Except_DedupesLeftBeforeFiltering() |
| 101 | + { |
| 102 | + var simulation = new Simulation(); |
| 103 | + _ = simulation.ExecuteNonQuery("create table t (v int)"); |
| 104 | + _ = simulation.ExecuteNonQuery("insert into t values (1), (1), (2)"); |
| 105 | + |
| 106 | + using var connection = simulation.CreateOpenConnection(); |
| 107 | + var values = ReadInts(connection.CreateCommand("select v from t except select 99")); |
| 108 | + // INTERSECT/EXCEPT both dedupe their left side (probe-confirmed). |
| 109 | + CollectionAssert.AreEquivalent(new[] { 1, 2 }, values); |
| 110 | + } |
| 111 | + |
| 112 | + // === Type promotion / column count === |
| 113 | + |
| 114 | + [TestMethod] |
| 115 | + public void TypePromotion_IntPlusDecimal_ProducesDecimal() |
| 116 | + { |
| 117 | + using var connection = new Simulation().CreateOpenConnection(); |
| 118 | + using var reader = connection.CreateCommand("select 1 union select 2.5").ExecuteReader(); |
| 119 | + var values = new List<decimal>(); |
| 120 | + while (reader.Read()) |
| 121 | + values.Add(reader.GetDecimal(0)); |
| 122 | + CollectionAssert.AreEquivalent(new[] { 1m, 2.5m }, values); |
| 123 | + } |
| 124 | + |
| 125 | + [TestMethod] |
| 126 | + public void MismatchedColumnCount_RaisesMsg205() |
| 127 | + { |
| 128 | + var ex = Throws<DbException>(() => |
| 129 | + _ = new Simulation().ExecuteScalar("select 1, 2 union select 3")); |
| 130 | + AreEqual("205", ex.Data["HelpLink.EvtID"]); |
| 131 | + AreEqual("All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.", ex.Message); |
| 132 | + } |
| 133 | + |
| 134 | + // === Precedence / chaining === |
| 135 | + |
| 136 | + [TestMethod] |
| 137 | + public void Intersect_BindsTighterThanUnion() |
| 138 | + { |
| 139 | + // `1 union 2 intersect 2` should parse as `1 union (2 intersect 2)` = {1, 2}. |
| 140 | + var values = ReadInts(new Simulation().CreateCommand("select 1 union select 2 intersect select 2")); |
| 141 | + CollectionAssert.AreEquivalent(new[] { 1, 2 }, values); |
| 142 | + } |
| 143 | + |
| 144 | + [TestMethod] |
| 145 | + public void ThreeBranchUnion_LeftAssociative() |
| 146 | + { |
| 147 | + var values = ReadInts(new Simulation().CreateCommand("select 1 union select 2 union select 3")); |
| 148 | + CollectionAssert.AreEquivalent(new[] { 1, 2, 3 }, values); |
| 149 | + } |
| 150 | + |
| 151 | + [TestMethod] |
| 152 | + public void UnionAllAfterUnion_PreservesDupAtEnd() |
| 153 | + { |
| 154 | + // `(1 union 2) union all 1` = {1, 2} ++ {1} = {1, 2, 1}. |
| 155 | + var values = ReadInts(new Simulation().CreateCommand("select 1 union select 2 union all select 1")); |
| 156 | + CollectionAssert.AreEqual(new[] { 1, 2, 1 }, values); |
| 157 | + } |
| 158 | + |
| 159 | + // === ORDER BY interaction === |
| 160 | + |
| 161 | + [TestMethod] |
| 162 | + public void TopLevelOrderBy_AppliesToCombinedResult() |
| 163 | + { |
| 164 | + // ORDER BY at the very end applies to the combined result and |
| 165 | + // can reference the first branch's column name. |
| 166 | + using var connection = new Simulation().CreateOpenConnection(); |
| 167 | + using var reader = connection.CreateCommand( |
| 168 | + "select 1 as v union select 2 union select 3 order by v desc").ExecuteReader(); |
| 169 | + var values = new List<int>(); |
| 170 | + while (reader.Read()) |
| 171 | + values.Add(reader.GetInt32(0)); |
| 172 | + CollectionAssert.AreEqual(new[] { 3, 2, 1 }, values); |
| 173 | + } |
| 174 | + |
| 175 | + [TestMethod] |
| 176 | + public void PerBranchOrderBy_RaisesMsg156() |
| 177 | + { |
| 178 | + var ex = Throws<DbException>(() => |
| 179 | + _ = new Simulation().ExecuteScalar("select 1 order by 1 union select 2")); |
| 180 | + AreEqual("156", ex.Data["HelpLink.EvtID"]); |
| 181 | + } |
| 182 | + |
| 183 | + [TestMethod] |
| 184 | + public void SingleSelect_OrderByNonProjectedSource_StillWorks() |
| 185 | + { |
| 186 | + // The set-op refactor must not break the existing branch-internal |
| 187 | + // ORDER BY path: a non-set-op SELECT can still ORDER BY a source |
| 188 | + // column that's not in the projection list. |
| 189 | + var simulation = new Simulation(); |
| 190 | + _ = simulation.ExecuteNonQuery("create table t (a int, b int)"); |
| 191 | + _ = simulation.ExecuteNonQuery("insert into t values (3, 30), (1, 10), (2, 20)"); |
| 192 | + |
| 193 | + using var connection = simulation.CreateOpenConnection(); |
| 194 | + using var reader = connection.CreateCommand("select b from t order by a").ExecuteReader(); |
| 195 | + var values = new List<int>(); |
| 196 | + while (reader.Read()) |
| 197 | + values.Add(reader.GetInt32(0)); |
| 198 | + // a=1,2,3 → b=10,20,30 in that order. |
| 199 | + CollectionAssert.AreEqual(new[] { 10, 20, 30 }, values); |
| 200 | + } |
| 201 | + |
| 202 | + // === Tabled-source set ops === |
| 203 | + |
| 204 | + [TestMethod] |
| 205 | + public void Union_AcrossTwoTables_Dedupes() |
| 206 | + { |
| 207 | + var simulation = new Simulation(); |
| 208 | + _ = simulation.ExecuteNonQuery("create table left_t (v int)"); |
| 209 | + _ = simulation.ExecuteNonQuery("create table right_t (v int)"); |
| 210 | + _ = simulation.ExecuteNonQuery("insert into left_t values (1), (2), (3)"); |
| 211 | + _ = simulation.ExecuteNonQuery("insert into right_t values (3), (4), (5)"); |
| 212 | + |
| 213 | + using var connection = simulation.CreateOpenConnection(); |
| 214 | + var values = ReadInts(connection.CreateCommand( |
| 215 | + "select v from left_t union select v from right_t")); |
| 216 | + CollectionAssert.AreEquivalent(new[] { 1, 2, 3, 4, 5 }, values); |
| 217 | + } |
| 218 | + |
| 219 | + [TestMethod] |
| 220 | + public void Intersect_AcrossTwoTables_Common() |
| 221 | + { |
| 222 | + var simulation = new Simulation(); |
| 223 | + _ = simulation.ExecuteNonQuery("create table left_t (v int)"); |
| 224 | + _ = simulation.ExecuteNonQuery("create table right_t (v int)"); |
| 225 | + _ = simulation.ExecuteNonQuery("insert into left_t values (1), (2), (3)"); |
| 226 | + _ = simulation.ExecuteNonQuery("insert into right_t values (3), (4), (5)"); |
| 227 | + |
| 228 | + using var connection = simulation.CreateOpenConnection(); |
| 229 | + var values = ReadInts(connection.CreateCommand( |
| 230 | + "select v from left_t intersect select v from right_t")); |
| 231 | + CollectionAssert.AreEqual(new[] { 3 }, values); |
| 232 | + } |
| 233 | + |
| 234 | + [TestMethod] |
| 235 | + public void Except_AcrossTwoTables_LeftMinusRight() |
| 236 | + { |
| 237 | + var simulation = new Simulation(); |
| 238 | + _ = simulation.ExecuteNonQuery("create table left_t (v int)"); |
| 239 | + _ = simulation.ExecuteNonQuery("create table right_t (v int)"); |
| 240 | + _ = simulation.ExecuteNonQuery("insert into left_t values (1), (2), (3)"); |
| 241 | + _ = simulation.ExecuteNonQuery("insert into right_t values (3), (4), (5)"); |
| 242 | + |
| 243 | + using var connection = simulation.CreateOpenConnection(); |
| 244 | + var values = ReadInts(connection.CreateCommand( |
| 245 | + "select v from left_t except select v from right_t")); |
| 246 | + CollectionAssert.AreEquivalent(new[] { 1, 2 }, values); |
| 247 | + } |
| 248 | +} |
0 commit comments