|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from narwhals._plan.common import Immutable |
| 6 | +from narwhals._plan.expr_parsing import parse_into_expr_ir |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from narwhals._plan.common import ExprIR, IntoExpr, Seq |
| 10 | + from narwhals._plan.dummy import DummyExpr |
| 11 | + from narwhals._plan.expr import Ternary |
| 12 | + |
| 13 | + |
| 14 | +class When(Immutable): |
| 15 | + __slots__ = ("condition",) |
| 16 | + |
| 17 | + condition: ExprIR |
| 18 | + |
| 19 | + def then(self, expr: IntoExpr, /) -> Then: |
| 20 | + return Then(condition=self.condition, statement=parse_into_expr_ir(expr)) |
| 21 | + |
| 22 | + @staticmethod |
| 23 | + def _from_expr(expr: DummyExpr, /) -> When: |
| 24 | + return When(condition=expr._ir) |
| 25 | + |
| 26 | + |
| 27 | +class Then(Immutable): |
| 28 | + __slots__ = ("condition", "statement") |
| 29 | + |
| 30 | + condition: ExprIR |
| 31 | + statement: ExprIR |
| 32 | + |
| 33 | + def when(self, condition: IntoExpr, /) -> ChainedWhen: |
| 34 | + return ChainedWhen( |
| 35 | + conditions=(self.condition, parse_into_expr_ir(condition)), |
| 36 | + statements=(self.statement,), |
| 37 | + ) |
| 38 | + |
| 39 | + def otherwise(self, statement: IntoExpr, /) -> DummyExpr: |
| 40 | + return ternary_expr( |
| 41 | + self.condition, self.condition, parse_into_expr_ir(statement) |
| 42 | + ).to_narwhals() |
| 43 | + |
| 44 | + |
| 45 | +class ChainedWhen(Immutable): |
| 46 | + __slots__ = ("conditions", "statements") |
| 47 | + |
| 48 | + conditions: Seq[ExprIR] |
| 49 | + statements: Seq[ExprIR] |
| 50 | + |
| 51 | + def then(self, statement: IntoExpr, /) -> ChainedThen: |
| 52 | + return ChainedThen( |
| 53 | + conditions=self.conditions, |
| 54 | + statements=(*self.statements, parse_into_expr_ir(statement)), |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +class ChainedThen(Immutable): |
| 59 | + """https://github.com/pola-rs/polars/blob/b9dd8cdbd6e6ec8373110536955ed5940b9460ec/crates/polars-plan/src/dsl/arity.rs#L89-L130.""" |
| 60 | + |
| 61 | + __slots__ = ("conditions", "statements") |
| 62 | + |
| 63 | + conditions: Seq[ExprIR] |
| 64 | + statements: Seq[ExprIR] |
| 65 | + |
| 66 | + def when(self, condition: IntoExpr, /) -> ChainedWhen: |
| 67 | + return ChainedWhen( |
| 68 | + conditions=(*self.conditions, parse_into_expr_ir(condition)), |
| 69 | + statements=self.statements, |
| 70 | + ) |
| 71 | + |
| 72 | + def otherwise(self, statement: IntoExpr, /) -> DummyExpr: |
| 73 | + otherwise = parse_into_expr_ir(statement) |
| 74 | + it_conditions = reversed(self.conditions) |
| 75 | + it_statements = reversed(self.statements) |
| 76 | + for e in it_conditions: |
| 77 | + otherwise = ternary_expr(e, next(it_statements), otherwise) |
| 78 | + return otherwise.to_narwhals() |
| 79 | + |
| 80 | + |
| 81 | +def ternary_expr(predicate: ExprIR, truthy: ExprIR, falsy: ExprIR, /) -> Ternary: |
| 82 | + from narwhals._plan.expr import Ternary |
| 83 | + |
| 84 | + return Ternary(predicate=predicate, truthy=truthy, falsy=falsy) |
0 commit comments