Skip to content

Commit

Permalink
rewrite basketWeeks expression
Browse files Browse the repository at this point in the history
Previous implementation wrapped each addition operation in an
unnecessary set of parentheses therefore the expression looked like
this:

[Column<'coalesce(
    (((((((((((((((((((((((((((((((((((((((((((((((((((((
    0 +
    CASE WHEN (coalesce(count(CASE WHEN ...)))) +
    CASE WHEN (coalesce(count(CASE WHEN ...)))) +
etc...

which was quite annoyinf. SO I've rewritten it so that doesn't happen.
  • Loading branch information
jamiekt committed Feb 25, 2024
1 parent b0a7ec4 commit 3d90958
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions jstark/features/basket_periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ class BasketPeriods(DerivedFeature):
"""BasketPeriods feature"""

def column_expression(self) -> Column:
expr = f.lit(0)
exprs = []
for period in range(self.feature_period.end, self.feature_period.start + 1):
expr = expr + f.when(
BasketCount(
as_at=self.as_at,
feature_period=FeaturePeriod(
self.feature_period.period_unit_of_measure, period, period
),
).column
> 0,
1,
).otherwise(0)
return expr
exprs.append(
f.when(
BasketCount(
as_at=self.as_at,
feature_period=FeaturePeriod(
self.feature_period.period_unit_of_measure, period, period
),
).column
> 0,
1,
).otherwise(0)
)
return sum(exprs)

def default_value(self) -> Column:
return f.lit(None)
Expand Down

0 comments on commit 3d90958

Please sign in to comment.