|
92 | 92 | Union = expr_internal.Union |
93 | 93 | Unnest = expr_internal.Unnest |
94 | 94 | UnnestExpr = expr_internal.UnnestExpr |
95 | | -Window = expr_internal.Window |
| 95 | +WindowExpr = expr_internal.WindowExpr |
96 | 96 |
|
97 | 97 | __all__ = [ |
98 | 98 | "Expr", |
|
154 | 154 | "Partitioning", |
155 | 155 | "Repartition", |
156 | 156 | "Window", |
| 157 | + "WindowExpr", |
157 | 158 | "WindowFrame", |
158 | 159 | "WindowFrameBound", |
159 | 160 | ] |
@@ -542,32 +543,25 @@ def window_frame(self, window_frame: WindowFrame) -> ExprFuncBuilder: |
542 | 543 | """ |
543 | 544 | return ExprFuncBuilder(self.expr.window_frame(window_frame.window_frame)) |
544 | 545 |
|
545 | | - def over( |
546 | | - self, |
547 | | - partition_by: Optional[list[Expr]] = None, |
548 | | - window_frame: Optional[WindowFrame] = None, |
549 | | - order_by: Optional[list[SortExpr | Expr]] = None, |
550 | | - null_treatment: Optional[NullTreatment] = None, |
551 | | - ) -> Expr: |
| 546 | + def over(self, window: Window) -> Expr: |
552 | 547 | """Turn an aggregate function into a window function. |
553 | 548 |
|
554 | 549 | This function turns any aggregate function into a window function. With the |
555 | 550 | exception of ``partition_by``, how each of the parameters is used is determined |
556 | 551 | by the underlying aggregate function. |
557 | 552 |
|
558 | 553 | Args: |
559 | | - partition_by: Expressions to partition the window frame on |
560 | | - window_frame: Specify the window frame parameters |
561 | | - order_by: Set ordering within the window frame |
562 | | - null_treatment: Set how to handle null values |
| 554 | + window: Window definition |
563 | 555 | """ |
564 | | - partition_by_raw = expr_list_to_raw_expr_list(partition_by) |
565 | | - order_by_raw = sort_list_to_raw_sort_list(order_by) |
| 556 | + partition_by_raw = expr_list_to_raw_expr_list(window._partition_by) |
| 557 | + order_by_raw = sort_list_to_raw_sort_list(window._order_by) |
566 | 558 | window_frame_raw = ( |
567 | | - window_frame.window_frame if window_frame is not None else None |
| 559 | + window._window_frame.window_frame |
| 560 | + if window._window_frame is not None |
| 561 | + else None |
568 | 562 | ) |
569 | 563 | null_treatment_raw = ( |
570 | | - null_treatment.value if null_treatment is not None else None |
| 564 | + window._null_treatment.value if window._null_treatment is not None else None |
571 | 565 | ) |
572 | 566 |
|
573 | 567 | return Expr( |
@@ -621,6 +615,30 @@ def build(self) -> Expr: |
621 | 615 | return Expr(self.builder.build()) |
622 | 616 |
|
623 | 617 |
|
| 618 | +class Window: |
| 619 | + """Define reusable window parameters.""" |
| 620 | + |
| 621 | + def __init__( |
| 622 | + self, |
| 623 | + partition_by: Optional[list[Expr]] = None, |
| 624 | + window_frame: Optional[WindowFrame] = None, |
| 625 | + order_by: Optional[list[SortExpr | Expr]] = None, |
| 626 | + null_treatment: Optional[NullTreatment] = None, |
| 627 | + ) -> None: |
| 628 | + """Construct a window definition. |
| 629 | +
|
| 630 | + Args: |
| 631 | + partition_by: Partitions for window operation |
| 632 | + window_frame: Define the start and end bounds of the window frame |
| 633 | + order_by: Set ordering |
| 634 | + null_treatment: Indicate how nulls are to be treated |
| 635 | + """ |
| 636 | + self._partition_by = partition_by |
| 637 | + self._window_frame = window_frame |
| 638 | + self._order_by = order_by |
| 639 | + self._null_treatment = null_treatment |
| 640 | + |
| 641 | + |
624 | 642 | class WindowFrame: |
625 | 643 | """Defines a window frame for performing window operations.""" |
626 | 644 |
|
|
0 commit comments