|
4 | 4 | namespace SqlServerSimulator; |
5 | 5 |
|
6 | 6 | /// <summary> |
7 | | -/// Behavioral tests for <c>ROW_NUMBER() OVER(PARTITION BY ... ORDER BY ...)</c>, |
8 | | -/// the single shape EF Core 10 emits for <c>Take</c>-per-group / <c>Skip+Take</c>-per-group |
9 | | -/// patterns. The function's value depends on the entire row stream |
10 | | -/// (sorted within partition), so the executor buffers post-WHERE tuples |
11 | | -/// before binding per-row results. |
| 7 | +/// Behavioral tests for ranking windows (<c>ROW_NUMBER</c> / <c>RANK</c> / |
| 8 | +/// <c>DENSE_RANK</c> / <c>NTILE</c>) and value windows (<c>LAG</c> / |
| 9 | +/// <c>LEAD</c> / <c>FIRST_VALUE</c>). All require ORDER BY inside OVER and |
| 10 | +/// share the post-WHERE buffer + per-partition sort path; the ranking |
| 11 | +/// family additionally tracks tie behavior (RANK skips, DENSE_RANK doesn't, |
| 12 | +/// ROW_NUMBER assigns arbitrary order within ties), and value functions |
| 13 | +/// re-resolve the operand against another row's tuple. <c>LAST_VALUE</c> |
| 14 | +/// isn't covered — its implicit-frame semantic (current row, not partition |
| 15 | +/// last) requires explicit-frame support which the simulator doesn't model. |
12 | 16 | /// </summary> |
13 | 17 | [TestClass] |
14 | 18 | public sealed class WindowFunctionTests |
@@ -158,4 +162,245 @@ public void RowNumber_CombinedWithGroupBy_NotSupported() |
158 | 162 | _ = connection.CreateCommand( |
159 | 163 | "select blog_id, row_number() over(order by blog_id), count(*) from posts group by blog_id").ExecuteScalar()); |
160 | 164 | } |
| 165 | + |
| 166 | + // === RANK / DENSE_RANK === |
| 167 | + |
| 168 | + /// <summary> |
| 169 | + /// Seed for tie-sensitive tests: scores=10,20,20,30 in blog 1; 5,5,50 in blog 2. |
| 170 | + /// </summary> |
| 171 | + private static DbConnection SeededTies() |
| 172 | + { |
| 173 | + var connection = new Simulation().CreateOpenConnection(); |
| 174 | + _ = connection.CreateCommand(""" |
| 175 | + create table t (id int, grp int, v int); |
| 176 | + insert t values (1, 1, 10), (2, 1, 20), (3, 1, 20), (4, 1, 30), (5, 2, 5), (6, 2, 5), (7, 2, 50) |
| 177 | + """).ExecuteNonQuery(); |
| 178 | + return connection; |
| 179 | + } |
| 180 | + |
| 181 | + [TestMethod] |
| 182 | + public void Rank_TiesShareRank_NextRankSkipsAhead() |
| 183 | + { |
| 184 | + // RANK with ties: 10 → 1, 20 (tie) → 2, 20 (tie) → 2, 30 → 4 (skips 3). |
| 185 | + using var connection = SeededTies(); |
| 186 | + using var reader = connection.CreateCommand( |
| 187 | + "select id, rank() over(order by v) from t where grp = 1").ExecuteReader(); |
| 188 | + var byId = new Dictionary<int, long>(); |
| 189 | + while (reader.Read()) |
| 190 | + byId[reader.GetInt32(0)] = reader.GetInt64(1); |
| 191 | + AreEqual(1L, byId[1]); |
| 192 | + AreEqual(2L, byId[2]); |
| 193 | + AreEqual(2L, byId[3]); |
| 194 | + AreEqual(4L, byId[4]); |
| 195 | + } |
| 196 | + |
| 197 | + [TestMethod] |
| 198 | + public void DenseRank_TiesShareRank_NextRankDoesNotSkip() |
| 199 | + { |
| 200 | + // DENSE_RANK with ties: 10 → 1, 20 (tie) → 2, 20 (tie) → 2, 30 → 3 (no gap). |
| 201 | + using var connection = SeededTies(); |
| 202 | + using var reader = connection.CreateCommand( |
| 203 | + "select id, dense_rank() over(order by v) from t where grp = 1").ExecuteReader(); |
| 204 | + var byId = new Dictionary<int, long>(); |
| 205 | + while (reader.Read()) |
| 206 | + byId[reader.GetInt32(0)] = reader.GetInt64(1); |
| 207 | + AreEqual(1L, byId[1]); |
| 208 | + AreEqual(2L, byId[2]); |
| 209 | + AreEqual(2L, byId[3]); |
| 210 | + AreEqual(3L, byId[4]); |
| 211 | + } |
| 212 | + |
| 213 | + [TestMethod] |
| 214 | + public void Rank_PartitionedResetsPerPartition() |
| 215 | + { |
| 216 | + using var connection = SeededTies(); |
| 217 | + using var reader = connection.CreateCommand( |
| 218 | + "select id, rank() over(partition by grp order by v) from t").ExecuteReader(); |
| 219 | + var byId = new Dictionary<int, long>(); |
| 220 | + while (reader.Read()) |
| 221 | + byId[reader.GetInt32(0)] = reader.GetInt64(1); |
| 222 | + // Group 1: 1→1, 2→2, 3→2, 4→4. Group 2: 5→1, 6→1, 7→3. |
| 223 | + AreEqual(1L, byId[5]); |
| 224 | + AreEqual(1L, byId[6]); |
| 225 | + AreEqual(3L, byId[7]); |
| 226 | + } |
| 227 | + |
| 228 | + [TestMethod] |
| 229 | + public void Rank_RequiresOrderByInsideOver() |
| 230 | + { |
| 231 | + using var connection = SeededTies(); |
| 232 | + _ = Throws<DbException>(() => |
| 233 | + _ = connection.CreateCommand("select rank() over() from t").ExecuteScalar()); |
| 234 | + } |
| 235 | + |
| 236 | + // === NTILE === |
| 237 | + |
| 238 | + [TestMethod] |
| 239 | + public void NTile_DistributesRowsEvenly() |
| 240 | + { |
| 241 | + // 6 rows / 3 buckets = 2 rows per bucket. |
| 242 | + using var connection = SeededTies(); |
| 243 | + using var reader = connection.CreateCommand( |
| 244 | + "select id, ntile(3) over(order by id) from t where id <= 6").ExecuteReader(); |
| 245 | + var byId = new Dictionary<int, int>(); |
| 246 | + while (reader.Read()) |
| 247 | + byId[reader.GetInt32(0)] = reader.GetInt32(1); |
| 248 | + AreEqual(1, byId[1]); |
| 249 | + AreEqual(1, byId[2]); |
| 250 | + AreEqual(2, byId[3]); |
| 251 | + AreEqual(2, byId[4]); |
| 252 | + AreEqual(3, byId[5]); |
| 253 | + AreEqual(3, byId[6]); |
| 254 | + } |
| 255 | + |
| 256 | + [TestMethod] |
| 257 | + public void NTile_UnevenDistribution_FirstBucketsLarger() |
| 258 | + { |
| 259 | + // 7 rows / 3 buckets → smaller=2, remainder=1 → first bucket has 3, rest have 2. |
| 260 | + using var connection = SeededTies(); |
| 261 | + using var reader = connection.CreateCommand( |
| 262 | + "select id, ntile(3) over(order by id) from t").ExecuteReader(); |
| 263 | + var byId = new Dictionary<int, int>(); |
| 264 | + while (reader.Read()) |
| 265 | + byId[reader.GetInt32(0)] = reader.GetInt32(1); |
| 266 | + // Bucket sizes: [3, 2, 2] → IDs 1,2,3 → bucket 1; 4,5 → 2; 6,7 → 3. |
| 267 | + AreEqual(1, byId[1]); |
| 268 | + AreEqual(1, byId[2]); |
| 269 | + AreEqual(1, byId[3]); |
| 270 | + AreEqual(2, byId[4]); |
| 271 | + AreEqual(2, byId[5]); |
| 272 | + AreEqual(3, byId[6]); |
| 273 | + AreEqual(3, byId[7]); |
| 274 | + } |
| 275 | + |
| 276 | + [TestMethod] |
| 277 | + public void NTile_BucketsExceedRows_OneRowPerBucketThenEmpty() |
| 278 | + { |
| 279 | + // 3 rows / 5 buckets → first 3 buckets get 1 row each, last 2 buckets are empty. |
| 280 | + using var connection = SeededTies(); |
| 281 | + using var reader = connection.CreateCommand( |
| 282 | + "select id, ntile(5) over(order by id) from t where id <= 3").ExecuteReader(); |
| 283 | + var byId = new Dictionary<int, int>(); |
| 284 | + while (reader.Read()) |
| 285 | + byId[reader.GetInt32(0)] = reader.GetInt32(1); |
| 286 | + AreEqual(1, byId[1]); |
| 287 | + AreEqual(2, byId[2]); |
| 288 | + AreEqual(3, byId[3]); |
| 289 | + } |
| 290 | + |
| 291 | + [TestMethod] |
| 292 | + public void NTile_NonPositiveBucketCount_Raises9819() |
| 293 | + { |
| 294 | + using var connection = SeededTies(); |
| 295 | + var ex = Throws<DbException>(() => |
| 296 | + _ = connection.CreateCommand("select ntile(0) over(order by id) from t").ExecuteScalar()); |
| 297 | + AreEqual("9819", ex.Data["HelpLink.EvtID"]); |
| 298 | + } |
| 299 | + |
| 300 | + // === LAG / LEAD === |
| 301 | + |
| 302 | + [TestMethod] |
| 303 | + public void Lag_DefaultOffsetOne_PartitionBoundaryNull() |
| 304 | + { |
| 305 | + using var connection = SeededTies(); |
| 306 | + using var reader = connection.CreateCommand( |
| 307 | + "select id, lag(v) over(order by id) from t").ExecuteReader(); |
| 308 | + var byId = new Dictionary<int, int?>(); |
| 309 | + while (reader.Read()) |
| 310 | + byId[reader.GetInt32(0)] = reader.IsDBNull(1) ? null : reader.GetInt32(1); |
| 311 | + IsNull(byId[1]); // First row: no predecessor → NULL. |
| 312 | + AreEqual(10, byId[2]); |
| 313 | + AreEqual(20, byId[3]); |
| 314 | + AreEqual(20, byId[4]); |
| 315 | + AreEqual(30, byId[5]); |
| 316 | + } |
| 317 | + |
| 318 | + [TestMethod] |
| 319 | + public void Lag_ExplicitOffset_LooksBackFurther() |
| 320 | + { |
| 321 | + using var connection = SeededTies(); |
| 322 | + using var reader = connection.CreateCommand( |
| 323 | + "select id, lag(v, 2) over(order by id) from t").ExecuteReader(); |
| 324 | + var byId = new Dictionary<int, int?>(); |
| 325 | + while (reader.Read()) |
| 326 | + byId[reader.GetInt32(0)] = reader.IsDBNull(1) ? null : reader.GetInt32(1); |
| 327 | + IsNull(byId[1]); |
| 328 | + IsNull(byId[2]); |
| 329 | + AreEqual(10, byId[3]); |
| 330 | + AreEqual(20, byId[4]); |
| 331 | + } |
| 332 | + |
| 333 | + [TestMethod] |
| 334 | + public void Lag_WithDefaultExpression_SubstitutesAtBoundary() |
| 335 | + { |
| 336 | + using var connection = SeededTies(); |
| 337 | + using var reader = connection.CreateCommand( |
| 338 | + "select id, lag(v, 1, -99) over(order by id) from t").ExecuteReader(); |
| 339 | + var byId = new Dictionary<int, int>(); |
| 340 | + while (reader.Read()) |
| 341 | + byId[reader.GetInt32(0)] = reader.GetInt32(1); |
| 342 | + AreEqual(-99, byId[1]); |
| 343 | + AreEqual(10, byId[2]); |
| 344 | + } |
| 345 | + |
| 346 | + [TestMethod] |
| 347 | + public void Lead_MirrorsLagInOppositeDirection() |
| 348 | + { |
| 349 | + using var connection = SeededTies(); |
| 350 | + using var reader = connection.CreateCommand( |
| 351 | + "select id, lead(v) over(order by id) from t").ExecuteReader(); |
| 352 | + var byId = new Dictionary<int, int?>(); |
| 353 | + while (reader.Read()) |
| 354 | + byId[reader.GetInt32(0)] = reader.IsDBNull(1) ? null : reader.GetInt32(1); |
| 355 | + AreEqual(20, byId[1]); |
| 356 | + AreEqual(20, byId[2]); |
| 357 | + AreEqual(30, byId[3]); |
| 358 | + IsNull(byId[7]); // Last row: no successor → NULL. |
| 359 | + } |
| 360 | + |
| 361 | + [TestMethod] |
| 362 | + public void Lag_PartitionedDoesNotCrossPartitionBoundary() |
| 363 | + { |
| 364 | + using var connection = SeededTies(); |
| 365 | + using var reader = connection.CreateCommand( |
| 366 | + "select id, lag(v) over(partition by grp order by id) from t").ExecuteReader(); |
| 367 | + var byId = new Dictionary<int, int?>(); |
| 368 | + while (reader.Read()) |
| 369 | + byId[reader.GetInt32(0)] = reader.IsDBNull(1) ? null : reader.GetInt32(1); |
| 370 | + IsNull(byId[1]); // grp 1's first row. |
| 371 | + IsNull(byId[5]); // grp 2's first row — wouldn't be NULL if partition was ignored. |
| 372 | + } |
| 373 | + |
| 374 | + // === FIRST_VALUE === |
| 375 | + |
| 376 | + [TestMethod] |
| 377 | + public void FirstValue_ReturnsPartitionFirstAfterOrderBy() |
| 378 | + { |
| 379 | + using var connection = SeededTies(); |
| 380 | + using var reader = connection.CreateCommand( |
| 381 | + "select id, first_value(v) over(partition by grp order by v) from t").ExecuteReader(); |
| 382 | + var byId = new Dictionary<int, int>(); |
| 383 | + while (reader.Read()) |
| 384 | + byId[reader.GetInt32(0)] = reader.GetInt32(1); |
| 385 | + // grp 1: smallest v=10 → broadcast across rows 1,2,3,4. |
| 386 | + // grp 2: smallest v=5 → broadcast across rows 5,6,7. |
| 387 | + AreEqual(10, byId[1]); |
| 388 | + AreEqual(10, byId[4]); |
| 389 | + AreEqual(5, byId[5]); |
| 390 | + AreEqual(5, byId[7]); |
| 391 | + } |
| 392 | + |
| 393 | + [TestMethod] |
| 394 | + public void FirstValue_RespectsOrderByDirection() |
| 395 | + { |
| 396 | + // DESC ORDER BY → "first" is the max in each partition. |
| 397 | + using var connection = SeededTies(); |
| 398 | + using var reader = connection.CreateCommand( |
| 399 | + "select id, first_value(v) over(partition by grp order by v desc) from t").ExecuteReader(); |
| 400 | + var byId = new Dictionary<int, int>(); |
| 401 | + while (reader.Read()) |
| 402 | + byId[reader.GetInt32(0)] = reader.GetInt32(1); |
| 403 | + AreEqual(30, byId[1]); // grp 1's largest v. |
| 404 | + AreEqual(50, byId[5]); // grp 2's largest v. |
| 405 | + } |
161 | 406 | } |
0 commit comments