-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
length{,1}
functions for getting the length of `{List,NonEmpty}…
…Table`s (#268)
- Loading branch information
1 parent
0e24745
commit 9e7a447
Showing
10 changed files
with
140 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
### Added | ||
|
||
- `Rel8.head`, `Rel8.headTable`, `Rel8.last`, `Rel8.lastExpr` for accessing the first/last elements of arrays and `ListTable`s. We have also added variants for non-empty arrays/`NonEmptyTable` with the `1` suffix (e.g., `head1`). ([#245](https://github.com/circuithub/rel8/pull/245)) | ||
- `Rel8.head`, `Rel8.headExpr`, `Rel8.last`, `Rel8.lastExpr` for accessing the first/last elements of `ListTable`s and arrays. We have also added variants for `NonEmptyTable`s/non-empty arrays with the `1` suffix (e.g., `head1`). ([#245](https://github.com/circuithub/rel8/pull/245)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### Added | ||
|
||
- `Rel8.length` and `Rel8.lengthExpr` for getting the length `ListTable`s and arrays. We have also added variants for `NonEmptyTable`s/non-empty arrays with the `1` suffix (e.g., `length1`). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,42 @@ | ||
{-# language FlexibleContexts #-} | ||
{-# language MonoLocalBinds #-} | ||
|
||
module Rel8.Expr.List ( | ||
headExpr, | ||
indexExpr, | ||
lastExpr, | ||
sheadExpr, | ||
slastExpr, | ||
lengthExpr, | ||
) where | ||
|
||
-- base | ||
import Data.Int (Int64) | ||
import Data.Int (Int32) | ||
import Prelude | ||
|
||
-- opaleye | ||
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye | ||
|
||
-- rel8 | ||
import Rel8.Expr (Expr) | ||
import Rel8.Expr.Opaleye (fromPrimExpr, toPrimExpr) | ||
import Rel8.Schema.Null (Nullify) | ||
import Rel8.Schema.Null (Nullify, Sql, Unnullify) | ||
import Rel8.Type (DBType, typeInformation) | ||
import Rel8.Type.Information (TypeInformation) | ||
import qualified Rel8.Type.Array as Prim | ||
|
||
|
||
headExpr :: Sql DBType a => Expr [a] -> Expr (Nullify a) | ||
headExpr = sheadExpr typeInformation | ||
|
||
|
||
lastExpr :: Sql DBType a => Expr [a] -> Expr (Nullify a) | ||
lastExpr = slastExpr typeInformation | ||
|
||
|
||
headExpr :: Expr [a] -> Expr (Nullify a) | ||
headExpr array = indexExpr array index | ||
where | ||
index = fromPrimExpr $ Opaleye.FunExpr "array_lower" [toPrimExpr array, one] | ||
where | ||
one = Opaleye.ConstExpr (Opaleye.IntegerLit 1) | ||
sheadExpr :: TypeInformation (Unnullify a) -> Expr [a] -> Expr (Nullify a) | ||
sheadExpr info = fromPrimExpr . Prim.head info . toPrimExpr | ||
|
||
|
||
indexExpr :: Expr [a] -> Expr Int64 -> Expr (Nullify a) | ||
indexExpr array index = | ||
fromPrimExpr (Opaleye.ArrayIndex (toPrimExpr array) (toPrimExpr index)) | ||
slastExpr :: TypeInformation (Unnullify a) -> Expr [a] -> Expr (Nullify a) | ||
slastExpr info = fromPrimExpr . Prim.last info . toPrimExpr | ||
|
||
|
||
lastExpr :: Expr [a] -> Expr (Nullify a) | ||
lastExpr array = indexExpr array index | ||
where | ||
index = fromPrimExpr $ Opaleye.FunExpr "array_upper" [toPrimExpr array, one] | ||
where | ||
one = Opaleye.ConstExpr (Opaleye.IntegerLit 1) | ||
lengthExpr :: Expr [a] -> Expr Int32 | ||
lengthExpr = fromPrimExpr . Prim.length . toPrimExpr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,43 @@ | ||
{-# language FlexibleContexts #-} | ||
{-# language MonoLocalBinds #-} | ||
|
||
module Rel8.Expr.NonEmpty ( | ||
head1Expr, | ||
index1Expr, | ||
last1Expr, | ||
shead1Expr, | ||
slast1Expr, | ||
length1Expr, | ||
) where | ||
|
||
-- base | ||
import Data.Int (Int64) | ||
import Data.Int (Int32) | ||
import Data.List.NonEmpty (NonEmpty) | ||
import Prelude | ||
|
||
-- opaleye | ||
import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye | ||
|
||
-- rel8 | ||
import Rel8.Expr (Expr) | ||
import Rel8.Expr.Opaleye (fromPrimExpr, toPrimExpr) | ||
import Rel8.Schema.Null (Nullify) | ||
import Rel8.Schema.Null (Sql, Unnullify) | ||
import Rel8.Type (DBType, typeInformation) | ||
import Rel8.Type.Information (TypeInformation) | ||
import qualified Rel8.Type.Array as Prim | ||
|
||
|
||
head1Expr :: Sql DBType a => Expr (NonEmpty a) -> Expr a | ||
head1Expr = shead1Expr typeInformation | ||
|
||
|
||
last1Expr :: Sql DBType a => Expr (NonEmpty a) -> Expr a | ||
last1Expr = slast1Expr typeInformation | ||
|
||
|
||
head1Expr :: Expr (NonEmpty a) -> Expr a | ||
head1Expr array = fromPrimExpr $ toPrimExpr $ index1Expr array index | ||
where | ||
index = fromPrimExpr $ Opaleye.FunExpr "array_lower" [toPrimExpr array, one] | ||
where | ||
one = Opaleye.ConstExpr (Opaleye.IntegerLit 1) | ||
shead1Expr :: TypeInformation (Unnullify a) -> Expr (NonEmpty a) -> Expr a | ||
shead1Expr info = fromPrimExpr . Prim.head info . toPrimExpr | ||
|
||
|
||
index1Expr :: Expr (NonEmpty a) -> Expr Int64 -> Expr (Nullify a) | ||
index1Expr array index = | ||
fromPrimExpr (Opaleye.ArrayIndex (toPrimExpr array) (toPrimExpr index)) | ||
slast1Expr :: TypeInformation (Unnullify a) -> Expr (NonEmpty a) -> Expr a | ||
slast1Expr info = fromPrimExpr . Prim.last info . toPrimExpr | ||
|
||
|
||
last1Expr :: Expr (NonEmpty a) -> Expr a | ||
last1Expr array = fromPrimExpr $ toPrimExpr $ index1Expr array index | ||
where | ||
index = fromPrimExpr $ Opaleye.FunExpr "array_upper" [toPrimExpr array, one] | ||
where | ||
one = Opaleye.ConstExpr (Opaleye.IntegerLit 1) | ||
length1Expr :: Expr (NonEmpty a) -> Expr Int32 | ||
length1Expr = fromPrimExpr . Prim.length . toPrimExpr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters