|
| 1 | +using System.Data.Common; |
| 2 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Behavioral tests for the <c>PIVOT</c> / <c>UNPIVOT</c> table operators. |
| 8 | +/// PIVOT desugars to grouped conditional aggregation (grouping key = every |
| 9 | +/// inner column except the FOR column and the aggregate argument), each IN |
| 10 | +/// value becoming <c>agg(CASE forCol WHEN value THEN argCol END)</c>; UNPIVOT |
| 11 | +/// unfolds each row into one row per non-NULL IN column. Both attach as a |
| 12 | +/// postfix to a FROM source and behave like a derived table downstream. |
| 13 | +/// Behavior probed against SQL Server 2025. |
| 14 | +/// </summary> |
| 15 | +[TestClass] |
| 16 | +public sealed class PivotTests |
| 17 | +{ |
| 18 | + private static DbConnection SeededSales() |
| 19 | + { |
| 20 | + var connection = new Simulation().CreateOpenConnection(); |
| 21 | + _ = connection.CreateCommand( |
| 22 | + "create table sales (Region varchar(10), Yr int, Amount decimal(10,2), Note varchar(20))").ExecuteNonQuery(); |
| 23 | + _ = connection.CreateCommand( |
| 24 | + "insert sales values " + |
| 25 | + "('East', 2020, 100.00, 'a'), ('East', 2020, 50.00, 'a'), " + |
| 26 | + "('East', 2021, 200.00, 'b'), ('West', 2020, 10.00, 'a'), " + |
| 27 | + "('West', 2021, 20.00, 'b'), ('West', 2022, 5.00, 'c')").ExecuteNonQuery(); |
| 28 | + return connection; |
| 29 | + } |
| 30 | + |
| 31 | + // === PIVOT === |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + public void Pivot_DerivedTable_GroupsByRemainingColumn() |
| 35 | + { |
| 36 | + // Inner projection drops Note, so the grouping key is Region alone: |
| 37 | + // one row per region, each year folded into its own column. |
| 38 | + using var connection = SeededSales(); |
| 39 | + const string pivot = |
| 40 | + "(select Region, Yr, Amount from sales) src " + |
| 41 | + "pivot (sum(Amount) for Yr in ([2020],[2021],[2022])) as p"; |
| 42 | + AreEqual(2, connection.CreateCommand($"select count(*) from {pivot}").ExecuteScalar()); |
| 43 | + AreEqual(150.00m, connection.CreateCommand($"select [2020] from {pivot} where Region = 'East'").ExecuteScalar()); |
| 44 | + AreEqual(200.00m, connection.CreateCommand($"select [2021] from {pivot} where Region = 'East'").ExecuteScalar()); |
| 45 | + AreEqual(5.00m, connection.CreateCommand($"select [2022] from {pivot} where Region = 'West'").ExecuteScalar()); |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void Pivot_StrayColumn_SplitsGroups() |
| 50 | + { |
| 51 | + // SELECT * over the bare table keeps Note, so (Region, Note) is the |
| 52 | + // implicit grouping key — East splits into the 'a' and 'b' groups. |
| 53 | + using var connection = SeededSales(); |
| 54 | + AreEqual(5, connection.CreateCommand( |
| 55 | + "select count(*) from " + |
| 56 | + "(select Region, Note, Yr, Amount from sales) src " + |
| 57 | + "pivot (sum(Amount) for Yr in ([2020],[2021],[2022])) as p").ExecuteScalar()); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void Pivot_EmptyGroup_SumIsNullCountIsZero() |
| 62 | + { |
| 63 | + using var connection = SeededSales(); |
| 64 | + // East has no 2022 row for SUM → NULL... |
| 65 | + AreEqual(DBNull.Value, connection.CreateCommand( |
| 66 | + "select [2022] from (select Region, Yr, Amount from sales) src " + |
| 67 | + "pivot (sum(Amount) for Yr in ([2022])) as p where Region = 'East'").ExecuteScalar()); |
| 68 | + // ...but COUNT over an empty group is 0, not NULL. |
| 69 | + AreEqual(0, connection.CreateCommand( |
| 70 | + "select [2022] from (select Region, Yr from sales) src " + |
| 71 | + "pivot (count(Yr) for Yr in ([2022])) as p where Region = 'East'").ExecuteScalar()); |
| 72 | + } |
| 73 | + |
| 74 | + [TestMethod] |
| 75 | + public void Pivot_ValueNotInSource_ColumnOfNulls() |
| 76 | + { |
| 77 | + using var connection = SeededSales(); |
| 78 | + AreEqual(DBNull.Value, connection.CreateCommand( |
| 79 | + "select [2099] from (select Region, Yr, Amount from sales) src " + |
| 80 | + "pivot (sum(Amount) for Yr in ([2099])) as p where Region = 'East'").ExecuteScalar()); |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + public void Pivot_StringForColumn() |
| 85 | + { |
| 86 | + using var connection = SeededSales(); |
| 87 | + AreEqual(150.00m, connection.CreateCommand( |
| 88 | + "select [East] from (select Yr, Region, Amount from sales) src " + |
| 89 | + "pivot (sum(Amount) for Region in ([East],[West])) as p where Yr = 2020").ExecuteScalar()); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public void Pivot_AvgAggregate_WidensScale() |
| 94 | + { |
| 95 | + // AVG(decimal(10,2)) → decimal(38,6): East's 2020 avg of 100 and 50. |
| 96 | + using var connection = SeededSales(); |
| 97 | + AreEqual(75.000000m, connection.CreateCommand( |
| 98 | + "select [2020] from (select Region, Yr, Amount from sales) src " + |
| 99 | + "pivot (avg(Amount) for Yr in ([2020])) as p where Region = 'East'").ExecuteScalar()); |
| 100 | + } |
| 101 | + |
| 102 | + [TestMethod] |
| 103 | + public void Pivot_WhereAndOrderByApplyToOutput() |
| 104 | + { |
| 105 | + using var connection = SeededSales(); |
| 106 | + using var reader = connection.CreateCommand( |
| 107 | + "select Region from (select Region, Yr, Amount from sales) src " + |
| 108 | + "pivot (sum(Amount) for Yr in ([2020],[2021])) as p " + |
| 109 | + "where [2020] > 50 order by [2020] desc").ExecuteReader(); |
| 110 | + var regions = new List<string>(); |
| 111 | + while (reader.Read()) regions.Add(reader.GetString(0)); |
| 112 | + CollectionAssert.AreEqual(new[] { "East" }, regions); |
| 113 | + } |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void Pivot_ComputedForColumn_AdventureWorksShape() |
| 117 | + { |
| 118 | + // The shape of Sales.vSalesPersonSalesByFiscalYears: a derived table |
| 119 | + // projects YEAR(OrderDate) as the FOR column plus passthrough name |
| 120 | + // columns, pivoting SUM(SubTotal) across fiscal years. |
| 121 | + var connection = new Simulation().CreateOpenConnection(); |
| 122 | + _ = connection.CreateCommand( |
| 123 | + "create table orders (SalesPersonId int, FullName varchar(20), OrderDate date, SubTotal decimal(10,2))").ExecuteNonQuery(); |
| 124 | + _ = connection.CreateCommand( |
| 125 | + "insert orders values " + |
| 126 | + "(1, 'Amy', '2002-03-01', 100.00), (1, 'Amy', '2002-09-01', 50.00), " + |
| 127 | + "(1, 'Amy', '2003-01-01', 200.00), (2, 'Bob', '2004-06-01', 7.00)").ExecuteNonQuery(); |
| 128 | + const string pivot = |
| 129 | + "(select SalesPersonId, FullName, year(OrderDate) as FiscalYear, SubTotal from orders) soh " + |
| 130 | + "pivot (sum(SubTotal) for FiscalYear in ([2002],[2003],[2004])) as pvt"; |
| 131 | + AreEqual(150.00m, connection.CreateCommand( |
| 132 | + $"select [2002] from {pivot} where SalesPersonId = 1").ExecuteScalar()); |
| 133 | + AreEqual(7.00m, connection.CreateCommand( |
| 134 | + $"select [2004] from {pivot} where FullName = 'Bob'").ExecuteScalar()); |
| 135 | + AreEqual(DBNull.Value, connection.CreateCommand( |
| 136 | + $"select [2003] from {pivot} where FullName = 'Bob'").ExecuteScalar()); |
| 137 | + connection.Dispose(); |
| 138 | + } |
| 139 | + |
| 140 | + // === PIVOT error paths === |
| 141 | + |
| 142 | + [TestMethod] |
| 143 | + public void Pivot_CountStar_Rejected() |
| 144 | + => _ = new Simulation().AssertSqlError( |
| 145 | + "create table t (a int, b int); " + |
| 146 | + "select * from (select a, b from t) s pivot (count(*) for b in ([1])) p", 102); |
| 147 | + |
| 148 | + [TestMethod] |
| 149 | + public void Pivot_TwoAggregates_Rejected() |
| 150 | + => _ = new Simulation().AssertSqlError( |
| 151 | + "create table t (a int, b int, c int); " + |
| 152 | + "select * from (select a, b, c from t) s pivot (sum(c), count(c) for b in ([1])) p", 102); |
| 153 | + |
| 154 | + [TestMethod] |
| 155 | + public void Pivot_MissingAlias_Rejected() |
| 156 | + => _ = new Simulation().AssertSqlError( |
| 157 | + "create table t (a int, b int, c int); " + |
| 158 | + "select * from (select a, b, c from t) s pivot (sum(c) for b in ([1]))", 102); |
| 159 | + |
| 160 | + [TestMethod] |
| 161 | + public void Pivot_UnknownForColumn_Msg207() |
| 162 | + => _ = new Simulation().AssertSqlError( |
| 163 | + "create table t (a int, b int, c int); " + |
| 164 | + "select * from (select a, b, c from t) s pivot (sum(c) for nope in ([1])) p", 207); |
| 165 | + |
| 166 | + [TestMethod] |
| 167 | + public void Pivot_DuplicateInValue_Msg8156() |
| 168 | + { |
| 169 | + var ex = new Simulation().AssertSqlError( |
| 170 | + "create table t (a int, b int, c int); " + |
| 171 | + "select * from (select a, b, c from t) s pivot (sum(c) for b in ([1],[1])) p", 8156); |
| 172 | + Contains("was specified multiple times for 'p'", ex.Message); |
| 173 | + } |
| 174 | + |
| 175 | + // === UNPIVOT === |
| 176 | + |
| 177 | + private static DbConnection SeededQuarters() |
| 178 | + { |
| 179 | + var connection = new Simulation().CreateOpenConnection(); |
| 180 | + _ = connection.CreateCommand( |
| 181 | + "create table q (ProductId int, Q1 int, Q2 int, Q3 int, Q4 int)").ExecuteNonQuery(); |
| 182 | + _ = connection.CreateCommand( |
| 183 | + "insert q values (1, 10, 20, null, 40), (2, null, null, null, null), (3, 5, 6, 7, 8)").ExecuteNonQuery(); |
| 184 | + return connection; |
| 185 | + } |
| 186 | + |
| 187 | + [TestMethod] |
| 188 | + public void Unpivot_ExcludesNullsAndAllNullRows() |
| 189 | + { |
| 190 | + using var connection = SeededQuarters(); |
| 191 | + using var reader = connection.CreateCommand( |
| 192 | + "select ProductId, Quarter, Sales from q " + |
| 193 | + "unpivot (Sales for Quarter in (Q1, Q2, Q3, Q4)) as u " + |
| 194 | + "order by ProductId, Quarter").ExecuteReader(); |
| 195 | + var rows = new List<(int, string, int)>(); |
| 196 | + while (reader.Read()) |
| 197 | + rows.Add((reader.GetInt32(0), reader.GetString(1), reader.GetInt32(2))); |
| 198 | + // Product 2 is all-NULL (vanishes); Product 1 drops its Q3 NULL. |
| 199 | + CollectionAssert.AreEqual( |
| 200 | + new[] |
| 201 | + { |
| 202 | + (1, "Q1", 10), (1, "Q2", 20), (1, "Q4", 40), |
| 203 | + (3, "Q1", 5), (3, "Q2", 6), (3, "Q3", 7), (3, "Q4", 8), |
| 204 | + }, |
| 205 | + rows); |
| 206 | + } |
| 207 | + |
| 208 | + [TestMethod] |
| 209 | + public void Unpivot_SelectStar_ValueThenNameColumn() |
| 210 | + { |
| 211 | + // SELECT * order: passthrough, then value column, then name column. |
| 212 | + using var connection = SeededQuarters(); |
| 213 | + using var reader = connection.CreateCommand( |
| 214 | + "select * from q unpivot (Sales for Quarter in (Q1, Q2, Q3, Q4)) as u").ExecuteReader(); |
| 215 | + AreEqual("ProductId", reader.GetName(0)); |
| 216 | + AreEqual("Sales", reader.GetName(1)); |
| 217 | + AreEqual("Quarter", reader.GetName(2)); |
| 218 | + } |
| 219 | + |
| 220 | + [TestMethod] |
| 221 | + public void Unpivot_TypeConflict_Msg8167() |
| 222 | + { |
| 223 | + var ex = new Simulation().AssertSqlError( |
| 224 | + "create table m (Id int, A bigint, B int); insert m values (1, 100, 2); " + |
| 225 | + "select Id, Col, Val from m unpivot (Val for Col in (A, B)) as u", 8167); |
| 226 | + Contains("conflicts with the type of other columns", ex.Message); |
| 227 | + } |
| 228 | + |
| 229 | + [TestMethod] |
| 230 | + public void Unpivot_MissingAlias_Rejected() |
| 231 | + => _ = new Simulation().AssertSqlError( |
| 232 | + "create table q (ProductId int, Q1 int, Q2 int); " + |
| 233 | + "select * from q unpivot (Sales for Quarter in (Q1, Q2))", 102); |
| 234 | + |
| 235 | + [TestMethod] |
| 236 | + public void Unpivot_UnknownColumn_Msg207() |
| 237 | + => _ = new Simulation().AssertSqlError( |
| 238 | + "create table q (ProductId int, Q1 int, Q2 int); " + |
| 239 | + "select * from q unpivot (Sales for Quarter in (Q1, Nope)) as u", 207); |
| 240 | +} |
0 commit comments