From 3d90958227c7e78124750d6b0d815f26ce5de50c Mon Sep 17 00:00:00 2001 From: Jamie Thomson <854953+jamiekt@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:10:01 +0000 Subject: [PATCH] rewrite basketWeeks expression 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. --- jstark/features/basket_periods.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/jstark/features/basket_periods.py b/jstark/features/basket_periods.py index 0d0cbb3..fe4b807 100644 --- a/jstark/features/basket_periods.py +++ b/jstark/features/basket_periods.py @@ -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)