|
93 | 93 | Union = expr_internal.Union |
94 | 94 | Unnest = expr_internal.Unnest |
95 | 95 | UnnestExpr = expr_internal.UnnestExpr |
96 | | -Window = expr_internal.Window |
| 96 | +WindowExpr = expr_internal.WindowExpr |
97 | 97 |
|
98 | 98 | __all__ = [ |
99 | 99 | "Expr", |
|
155 | 155 | "Partitioning", |
156 | 156 | "Repartition", |
157 | 157 | "Window", |
| 158 | + "WindowExpr", |
158 | 159 | "WindowFrame", |
159 | 160 | "WindowFrameBound", |
160 | 161 | ] |
@@ -527,32 +528,25 @@ def window_frame(self, window_frame: WindowFrame) -> ExprFuncBuilder: |
527 | 528 | """ |
528 | 529 | return ExprFuncBuilder(self.expr.window_frame(window_frame.window_frame)) |
529 | 530 |
|
530 | | - def over( |
531 | | - self, |
532 | | - partition_by: Optional[list[Expr]] = None, |
533 | | - window_frame: Optional[WindowFrame] = None, |
534 | | - order_by: Optional[list[SortExpr | Expr]] = None, |
535 | | - null_treatment: Optional[NullTreatment] = None, |
536 | | - ) -> Expr: |
| 531 | + def over(self, window: Window) -> Expr: |
537 | 532 | """Turn an aggregate function into a window function. |
538 | 533 |
|
539 | 534 | This function turns any aggregate function into a window function. With the |
540 | 535 | exception of ``partition_by``, how each of the parameters is used is determined |
541 | 536 | by the underlying aggregate function. |
542 | 537 |
|
543 | 538 | Args: |
544 | | - partition_by: Expressions to partition the window frame on |
545 | | - window_frame: Specify the window frame parameters |
546 | | - order_by: Set ordering within the window frame |
547 | | - null_treatment: Set how to handle null values |
| 539 | + window: Window definition |
548 | 540 | """ |
549 | | - partition_by_raw = expr_list_to_raw_expr_list(partition_by) |
550 | | - order_by_raw = sort_list_to_raw_sort_list(order_by) |
| 541 | + partition_by_raw = expr_list_to_raw_expr_list(window._partition_by) |
| 542 | + order_by_raw = sort_list_to_raw_sort_list(window._order_by) |
551 | 543 | window_frame_raw = ( |
552 | | - window_frame.window_frame if window_frame is not None else None |
| 544 | + window._window_frame.window_frame |
| 545 | + if window._window_frame is not None |
| 546 | + else None |
553 | 547 | ) |
554 | 548 | null_treatment_raw = ( |
555 | | - null_treatment.value if null_treatment is not None else None |
| 549 | + window._null_treatment.value if window._null_treatment is not None else None |
556 | 550 | ) |
557 | 551 |
|
558 | 552 | return Expr( |
@@ -606,6 +600,30 @@ def build(self) -> Expr: |
606 | 600 | return Expr(self.builder.build()) |
607 | 601 |
|
608 | 602 |
|
| 603 | +class Window: |
| 604 | + """Define reusable window parameters.""" |
| 605 | + |
| 606 | + def __init__( |
| 607 | + self, |
| 608 | + partition_by: Optional[list[Expr]] = None, |
| 609 | + window_frame: Optional[WindowFrame] = None, |
| 610 | + order_by: Optional[list[SortExpr | Expr]] = None, |
| 611 | + null_treatment: Optional[NullTreatment] = None, |
| 612 | + ) -> None: |
| 613 | + """Construct a window definition. |
| 614 | +
|
| 615 | + Args: |
| 616 | + partition_by: Partitions for window operation |
| 617 | + window_frame: Define the start and end bounds of the window frame |
| 618 | + order_by: Set ordering |
| 619 | + null_treatment: Indicate how nulls are to be treated |
| 620 | + """ |
| 621 | + self._partition_by = partition_by |
| 622 | + self._window_frame = window_frame |
| 623 | + self._order_by = order_by |
| 624 | + self._null_treatment = null_treatment |
| 625 | + |
| 626 | + |
609 | 627 | class WindowFrame: |
610 | 628 | """Defines a window frame for performing window operations.""" |
611 | 629 |
|
|
0 commit comments