|
23 | 23 | current_date, |
24 | 24 | current_time, |
25 | 25 | current_timestamp, |
| 26 | + dense_rank, |
26 | 27 | desc, |
27 | 28 | get, |
28 | 29 | is_null, |
@@ -514,6 +515,133 @@ def test_rank(session): |
514 | 515 | ) |
515 | 516 |
|
516 | 517 |
|
| 518 | +def test_dense_rank(session): |
| 519 | + df = session.create_dataframe( |
| 520 | + [ |
| 521 | + ("A", 1), |
| 522 | + ("A", 1), |
| 523 | + ("A", 2), |
| 524 | + ("A", 3), |
| 525 | + ("B", 2), |
| 526 | + ("B", 3), |
| 527 | + ], |
| 528 | + ["cat", "val"], |
| 529 | + ) |
| 530 | + window_spec = Window.partition_by(col("cat")).order_by(col("val").asc()) |
| 531 | + result = df.with_column("dense_rank", dense_rank().over(window_spec)) |
| 532 | + Utils.check_answer( |
| 533 | + result, |
| 534 | + [ |
| 535 | + Row("A", 1, 1), |
| 536 | + Row("A", 1, 1), |
| 537 | + Row("A", 2, 2), |
| 538 | + Row("A", 3, 3), |
| 539 | + Row("B", 2, 1), |
| 540 | + Row("B", 3, 2), |
| 541 | + ], |
| 542 | + ) |
| 543 | + |
| 544 | + |
| 545 | +def test_dense_rank_null_partition(session): |
| 546 | + """dense_rank() should handle NULL partition values (SNOW-3244422).""" |
| 547 | + df = session.create_dataframe( |
| 548 | + [ |
| 549 | + ("A", 1), |
| 550 | + ("A", 2), |
| 551 | + (None, 3), |
| 552 | + (None, 1), |
| 553 | + ("B", 5), |
| 554 | + ], |
| 555 | + ["cat", "val"], |
| 556 | + ) |
| 557 | + window_spec = Window.partition_by(col("cat")).order_by(col("val").asc()) |
| 558 | + result = df.with_column("dense_rank", dense_rank().over(window_spec)) |
| 559 | + Utils.check_answer( |
| 560 | + result, |
| 561 | + [ |
| 562 | + Row("A", 1, 1), |
| 563 | + Row("A", 2, 2), |
| 564 | + Row("B", 5, 1), |
| 565 | + Row(None, 1, 1), |
| 566 | + Row(None, 3, 2), |
| 567 | + ], |
| 568 | + ) |
| 569 | + |
| 570 | + |
| 571 | +def test_rank_null_partition(session): |
| 572 | + """rank() should handle NULL partition values.""" |
| 573 | + df = session.create_dataframe( |
| 574 | + [ |
| 575 | + ("A", 1), |
| 576 | + ("A", 1), |
| 577 | + (None, 2), |
| 578 | + (None, 3), |
| 579 | + (None, 3), |
| 580 | + ], |
| 581 | + ["cat", "val"], |
| 582 | + ) |
| 583 | + window_spec = Window.partition_by(col("cat")).order_by(col("val").asc()) |
| 584 | + result = df.with_column("rank", rank().over(window_spec)) |
| 585 | + Utils.check_answer( |
| 586 | + result, |
| 587 | + [ |
| 588 | + Row("A", 1, 1), |
| 589 | + Row("A", 1, 1), |
| 590 | + Row(None, 2, 1), |
| 591 | + Row(None, 3, 2), |
| 592 | + Row(None, 3, 2), |
| 593 | + ], |
| 594 | + ) |
| 595 | + |
| 596 | + |
| 597 | +def test_row_number_null_partition(session): |
| 598 | + """row_number() should handle NULL partition values.""" |
| 599 | + df = session.create_dataframe( |
| 600 | + [ |
| 601 | + ("A", 1), |
| 602 | + ("A", 2), |
| 603 | + (None, 3), |
| 604 | + (None, 1), |
| 605 | + ], |
| 606 | + ["cat", "val"], |
| 607 | + ) |
| 608 | + window_spec = Window.partition_by(col("cat")).order_by(col("val").asc()) |
| 609 | + result = df.with_column("row_num", row_number().over(window_spec)) |
| 610 | + Utils.check_answer( |
| 611 | + result, |
| 612 | + [ |
| 613 | + Row("A", 1, 1), |
| 614 | + Row("A", 2, 2), |
| 615 | + Row(None, 1, 1), |
| 616 | + Row(None, 3, 2), |
| 617 | + ], |
| 618 | + ) |
| 619 | + |
| 620 | + |
| 621 | +def test_window_agg_null_partition(session): |
| 622 | + """Aggregate window functions should handle NULL partition values.""" |
| 623 | + df = session.create_dataframe( |
| 624 | + [ |
| 625 | + ("A", 1), |
| 626 | + ("A", 3), |
| 627 | + (None, 2), |
| 628 | + (None, 4), |
| 629 | + ], |
| 630 | + ["cat", "val"], |
| 631 | + ) |
| 632 | + window_spec = Window.partition_by(col("cat")) |
| 633 | + result = df.with_column("total", sum("val").over(window_spec)) |
| 634 | + Utils.check_answer( |
| 635 | + result, |
| 636 | + [ |
| 637 | + Row("A", 1, 4), |
| 638 | + Row("A", 3, 4), |
| 639 | + Row(None, 2, 6), |
| 640 | + Row(None, 4, 6), |
| 641 | + ], |
| 642 | + ) |
| 643 | + |
| 644 | + |
517 | 645 | def test_window_indexing(session): |
518 | 646 | df = session.create_dataframe( |
519 | 647 | [ |
|
0 commit comments