on master help sign in
unnest support
commita3b32bde12856fca630413f94e9fa9ab28f99637 authorShane O'Brien <shane.obrien@circuithub.com> dateMon 14 Jun 2021, 17:23 (5 years ago) parentd08428c
5 files changed+74−23
src/Rel8.hs+4 src/Rel8/Query/List.hs+54−18 src/Rel8/Schema/HTable/Vectorize.hs+2−2 src/Rel8/Type/Array.hs+7−1 tests/Main.hs+7−2
src/Rel8.hs+4
@@ -100,6 +100,8 @@ module Rel8
100100 , nameListTable
101101 , many
102102 , manyExpr
103+ , catListTable
104+ , catList
103105
104106 -- ** @NonEmptyTable@
105107 , NonEmptyTable
@@ -108,6 +110,8 @@ module Rel8
108110 , nameNonEmptyTable
109111 , some
110112 , someExpr
113+ , catNonEmptyTable
114+ , catNonEmpty
111115
112116 -- ** @ADT@
113117 , ADT, ADTable, fromADT, toADT
src/Rel8/Query/List.hs+54−18
@@ -1,29 +1,52 @@
11{-# language FlexibleContexts #-}
2−{-# language MonoLocalBinds #-}
2+{-# language GADTs #-}
3+{-# language NamedFieldPuns #-}
34
45module Rel8.Query.List
56 ( many, some
67 , manyExpr, someExpr
8+ , catListTable, catNonEmptyTable
9+ , catList, catNonEmpty
710 )
811where
912
1013-- base
14+import Data.Functor.Identity ( runIdentity )
1115import Data.List.NonEmpty ( NonEmpty )
1216import Prelude
1317
18+-- opaleye
19+import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
20+
1421-- rel8
15−import Rel8.Expr ( Expr )
22+import Rel8.Expr ( Col( E, unE ), Expr )
1623import Rel8.Expr.Aggregate ( listAggExpr, nonEmptyAggExpr )
24+import Rel8.Expr.Opaleye ( mapPrimExpr )
1725import Rel8.Query ( Query )
1826import Rel8.Query.Aggregate ( aggregate )
1927import Rel8.Query.Maybe ( optional )
20−import Rel8.Schema.Null ( Sql )
21−import Rel8.Table ( Table )
28+import Rel8.Schema.HTable.Vectorize ( hunvectorize )
29+import Rel8.Schema.Null ( Sql, Unnullify )
30+import Rel8.Schema.Spec ( SSpec( SSpec, info ) )
31+import Rel8.Table ( Table, fromColumns )
2232import Rel8.Table.Aggregate ( listAgg, nonEmptyAgg )
23−import Rel8.Table.List ( ListTable )
33+import Rel8.Table.List ( ListTable( ListTable ) )
2434import Rel8.Table.Maybe ( maybeTable )
25−import Rel8.Table.NonEmpty ( NonEmptyTable )
26−import Rel8.Type ( DBType )
35+import Rel8.Table.NonEmpty ( NonEmptyTable( NonEmptyTable ) )
36+import Rel8.Type ( DBType, typeInformation )
37+import Rel8.Type.Array ( extractArrayElement )
38+import Rel8.Type.Information ( TypeInformation )
39+
40+
41+-- | Aggregate a 'Query' into a 'ListTable'. If the supplied query returns 0
42+-- rows, this function will produce a 'Query' that returns one row containing
43+-- the empty @ListTable@. If the supplied @Query@ does return rows, @many@ will
44+-- return exactly one row, with a @ListTable@ collecting all returned rows.
45+--
46+-- @many@ is analogous to 'Control.Applicative.many' from
47+-- @Control.Applicative@.
48+many :: Table Expr a => Query a -> Query (ListTable a)
49+many = fmap (maybeTable mempty id) . optional . aggregate . fmap listAgg
2750
2851
2952-- | Aggregate a 'Query' into a 'NonEmptyTable'. If the supplied query returns
@@ -38,17 +61,6 @@ some :: Table Expr a => Query a -> Query (NonEmptyTable a)
3861some = aggregate . fmap nonEmptyAgg
3962
4063
41−-- | Aggregate a 'Query' into a 'ListTable'. If the supplied query returns 0
42−-- rows, this function will produce a 'Query' that returns one row containing
43−-- the empty @ListTable@. If the supplied @Query@ does return rows, @many@ will
44−-- return exactly one row, with a @ListTable@ collecting all returned rows.
45−--
46−-- @many@ is analogous to 'Control.Applicative.many' from
47−-- @Control.Applicative@.
48−many :: Table Expr a => Query a -> Query (ListTable a)
49−many = fmap (maybeTable mempty id) . optional . aggregate . fmap listAgg
50−
51−
5264-- | A version of 'many' specialised to single expressions.
5365manyExpr :: Sql DBType a => Query (Expr a) -> Query (Expr [a])
5466manyExpr = fmap (maybeTable mempty id) . optional . aggregate . fmap listAggExpr
@@ -57,3 +69,27 @@ manyExpr = fmap (maybeTable mempty id) . optional . aggregate . fmap listAggExpr
5769-- | A version of 'many' specialised to single expressions.
5870someExpr :: Sql DBType a => Query (Expr a) -> Query (Expr (NonEmpty a))
5971someExpr = aggregate . fmap nonEmptyAggExpr
72+
73+
74+catListTable :: Table Expr a => ListTable a -> Query a
75+catListTable (ListTable as) = pure $ fromColumns $ runIdentity $
76+ hunvectorize (\SSpec {info} -> pure . E . sunnest info . unE) as
77+
78+
79+catNonEmptyTable :: Table Expr a => NonEmptyTable a -> Query a
80+catNonEmptyTable (NonEmptyTable as) = pure $ fromColumns $ runIdentity $
81+ hunvectorize (\SSpec {info} -> pure . E . sunnest info . unE) as
82+
83+
84+catList :: Sql DBType a => Expr [a] -> Query (Expr a)
85+catList = pure . sunnest typeInformation
86+
87+
88+catNonEmpty :: Sql DBType a => Expr (NonEmpty a) -> Query (Expr a)
89+catNonEmpty = pure . sunnest typeInformation
90+
91+
92+sunnest :: TypeInformation (Unnullify a) -> Expr (list a) -> Expr a
93+sunnest info = mapPrimExpr $
94+ extractArrayElement info .
95+ Opaleye.UnExpr (Opaleye.UnOpOther "UNNEST")
src/Rel8/Schema/HTable/Vectorize.hs+2−2
@@ -46,7 +46,7 @@ import Rel8.Type.Array ( listTypeInformation, nonEmptyTypeInformation )
4646import Rel8.Type.Information ( TypeInformation )
4747
4848-- semialign
49−import Data.Zip ( Unzip, Repeat, Zippy(..) )
49+import Data.Zip ( Unzip, Zip, Zippy(..) )
5050import Rel8.FCF
5151import Rel8.Schema.HTable.MapTable
5252import GHC.Generics (Generic)
@@ -106,7 +106,7 @@ hvectorize vectorizer as = HVectorize $ htabulate $ \(HMapTableField field) ->
106106{-# INLINABLE hvectorize #-}
107107
108108
109−hunvectorize :: (HTable t, Repeat f, Vector list)
109+hunvectorize :: (HTable t, Zip f, Vector list)
110110 => (forall labels necessity a. ()
111111 => SSpec ('Spec labels necessity a)
112112 -> context ('Spec labels 'Required (list a))
src/Rel8/Type/Array.hs+7−1
@@ -5,7 +5,7 @@
55{-# language ViewPatterns #-}
66
77module Rel8.Type.Array
8− ( array, encodeArrayElement
8+ ( array, encodeArrayElement, extractArrayElement
99 , listTypeInformation
1010 , nonEmptyTypeInformation
1111 )
@@ -92,3 +92,9 @@ encodeArrayElement :: TypeInformation a -> Opaleye.PrimExpr -> Opaleye.PrimExpr
9292encodeArrayElement info
9393 | isArray info = Opaleye.UnExpr (Opaleye.UnOpOther "ROW")
9494 | otherwise = id
95+
96+
97+extractArrayElement :: TypeInformation a -> Opaleye.PrimExpr -> Opaleye.PrimExpr
98+extractArrayElement info
99+ | isArray info = flip Opaleye.CompositeExpr "f1"
100+ | otherwise = id
tests/Main.hs+7−2
@@ -694,11 +694,16 @@ testSelectArray = databasePropertyTest "Can SELECT Arrays (with aggregation)" \t
694694 rows <- forAll $ Gen.list (Range.linear 1 10) Gen.bool
695695
696696 transaction \connection -> do
697− selected <- liftIO $ Rel8.select connection $ Rel8.aggregate do
698− Rel8.listAgg <$> Rel8.values (map Rel8.lit rows)
697+ selected <- liftIO $ Rel8.select connection do
698+ Rel8.many $ Rel8.values (map Rel8.lit rows)
699699
700700 selected === [foldMap pure rows]
701701
702+ selected' <- liftIO $ Rel8.select connection $ Rel8.catListTable =<< do
703+ Rel8.many $ Rel8.values (map Rel8.lit rows)
704+
705+ selected' === rows
706+
702707
703708data NestedMaybeTable f = NestedMaybeTable
704709 { nmt1 :: Rel8.Column f Bool