on master help sign in
Add a `rebind` step to `alignWith` (#213)

`Rel8.Tabulate.alignWith` uses `theseTable` to recover the key, which translates to a mess of nested `CASE` expressions. This becomes exponential with repeated uses of `alignWith`. Adding a `rebind` step to `alignWith` to rewrite this `key` expression each time makes this linear.

commitfd0b449c8c1ca73e79efb8d25912b2c4801cf5c9 authorShane <shane.obrien@circuithub.com> dateTue 1 Nov 2022, 14:34 (4 years ago) parent38cbebc
1 file changed+2−1
src/Rel8/Tabulate.hs+2−1
src/Rel8/Tabulate.hs+2−1
@@ -93,6 +93,7 @@ import Rel8.Query.Filter ( where_ )
9393import Rel8.Query.List ( catNonEmptyTable )
9494import qualified Rel8.Query.Maybe as Q ( optional )
9595import Rel8.Query.Opaleye ( mapOpaleye, unsafePeekQuery )
96+import Rel8.Query.Rebind ( rebind )
9697import Rel8.Query.These ( alignBy )
9798import Rel8.Table ( Table, fromColumns, toColumns )
9899import Rel8.Table.Aggregate ( hgroupBy, listAgg, nonEmptyAgg )
@@ -508,8 +509,8 @@ alignWith :: EqTable k
508509 -> Tabulation k a -> Tabulation k b -> Tabulation k c
509510alignWith f (Tabulation as) (Tabulation bs) = Tabulation $ \p -> do
510511 tkab <- liftF2 (alignBy condition) as bs p
512+ k <- traverse (rebind "key") $ recover $ bimap fst fst tkab
511513 let
512− k = recover $ bimap fst fst tkab
513514 tab = bimap snd snd tkab
514515 pure (k, f tab)
515516 where