Commit Graph

453 Commits

Author SHA1 Message Date
Shane O'Brien
08792f26f8
Expand use of QualifiedName to types, composites, enums 2023-07-15 13:38:33 +01:00
Shane
cdf0c761d3
Export materialize (#260)
The main reason I wasn't happy with this before is that there was nothing stopping you from writing `materialize query pure`. This returned query would produce invalid SQL if you actually tried to use it, because it would attempt to reference a common table expression outside the scope of the `WITH` statement.

The "solution" here is just to throw a `rebind` around the result such that `materialize` incurs an extra `Table Expr b` constraint, which means that you can't return `Query (Query a)` because `Query a` can't satisfy a `Table Expr` constraint.
2023-07-15 11:25:04 +00:00
Shane
9f372dc649
Support selecting from table-returning functions with queryFunction (#241)
This fixes #71.
2023-07-11 14:32:24 +01:00
Shane
bf63d70ff3
Unify function and nullaryFunction (#258)
This does away with the weird variadic arguments thing we had going on with `function`.

Functions with no arguments are now written as:

```haskell
now :: Expr UTCTime
now = function "now" ()
```

Functions with multiple arguments are now written as:

```haskell
quot :: Sql DBIntegral a => Expr a -> Expr a -> Expr a
quot n d = function "div" (n, d)
```

Single-argument functions are written exactly as before.
2023-07-11 14:05:02 +01:00
Shane
c778ac1763
Introduce QualifiedName (fixes #228) (#257)
This adds a new type `QualifiedName` for named PostgreSQL objects (tables, views, functions and sequences) that can optionally be qualified by a schema. Previously only `TableSchema` could be qualified in this way.

`QualifiedName` has an `IsString` instance so the common case (where the schema is `Nothing`) doesn't have to care about schemas (if `OverloadedStrings` is enabled).

This also refactors `TableSchema` to use `QualifiedName` for its `name` field and drops its `schema` field.

Thanks to @elldritch for the bug report and the inspiration.
2023-07-11 12:06:36 +00:00
Ollie Charles
8cec776fa6
Set up scriv and add pending changelog entries (#255) 2023-07-11 11:49:53 +00:00
Ollie Charles
6554cfc841
Switch to Opaleye 0.9.7.0 (#259)
We actually require this now, so this updates the lower bound, and builds from Hackage rather than a `source-repository-package`.
2023-07-08 22:58:24 +01:00
Ollie Charles
5d07aa549d
Hide Rel8.materialise (#256) 2023-07-07 18:22:36 +00:00
Ollie Charles
8b50ce5f1b
Use base-compat instead of CPP (#254) 2023-07-07 17:48:21 +00:00
Ollie Charles
044b9189ec
Switch to Nix flakes (#253) 2023-07-07 18:25:55 +01:00
Shane
3c0b67f99e
Statements overhaul (support for statement-level WITH) (#250)
The motivation behind this PR is to add support for PostreSQL's `WITH` syntax at the statement level, which gives the ability to, e.g., delete some rows from a table and then re-insert those deleted rows into another table, without any round-trips between the application and the database.

To support this, this PR introduces a new type called `Statement`, which represents a single PostgreSQL statement. It has a `Monad` instance which allows sub-statements (such as `DELETE` and `INSERT` statements) to be composed together and their results bound to values that can be referenced in subsequent sub-statements. These "compound" statements are then rendered as a `WITH` statement.

`select`, `insert`, `update` and `delete` have all been altered to produce the `Statement` type described above instead of the `Hasql.Statement` type.

Some changes were necessary to the `Returning` type. `Returning` previously bundled two different concepts together: whether or not to generate a `RETURNING` clause in the SQL for a manipulation statement, and how to decode the returned rows (if any). It was necessary to break these concepts apart because with `WITH` we need the ability to generate manipulation statements with `RETURNING` clauses that are never actually decoded at all (the results just get passed to the next statement without touching the application).

Now, the `Returning` type is only concerned with whether or not to generate a `RETURNING` clause, and the question of how to decode the returned the result of the statement is handled by the `run` functions. `run` converts a `Statement` into a runnable `Hasql.Statement`, decoding the result of the statement as a list of rows. The other variations, `run_`, `runN`, `run1`, `runMaybe` and `runVector` can be used when you want to decode as something other than a list of rows.

This also gains us support for decoding the result of a query directly to a `Vector` for the first time, which brings a performance improvement over lists for those who need it.
2023-07-07 11:29:15 +01:00
Shane
0357176c7b
Add head and last functions for extracting elements of ListTable (#245) 2023-06-24 11:27:56 +01:00
Shane
3e282eebf5
Bypass unneccessary toColumns/fromColumns in extract (#244) 2023-06-18 22:19:58 +01:00
Shane
2230452b19
Support "rank 2" catListTable (by "parsing" anonymous record) (#243)
This is another possible "fix" to #168 (as opposed to #242). It doesn't really fix the problem, but it allows us to use two levels of `catListTable` instead of only one. Instead of trying to use Postgres's broken `.f1` syntax, we cast the anonymous record to text, remove the parentheses and quotes and unescape any escaped quotes or backslashes, and then cast the resulting text back to the appropriate type. The reason this only works one level deep is that if the type we cast the text back to is itself an anonymous record, then PostgreSQL doesn't know how to parse the text.

It's kind of ugly and hacky but it does work and otherwise maintains the status quo. Comparison operators on nested lists continue to work as before and we don't need to burden `DBType` with parsing nonsense.
2023-06-18 21:57:00 +01:00
Shane
2eca87772f
Aggregation overhaul — return to Profunctor and semi-aggregations (#235)
This PR makes a number of changes to how aggregation works in Rel8.

The biggest change is that we drop the `Aggregate` context and we return to the `Profunctor`-based `Aggregator` that Opaleye uses (as in #37). While working with `Profunctor`s is more awkward for many common use-cases, it's ultimately more powerful. The big thing it gives you that we don't currently have is the ability to "post-map" on the result of an aggregation function. Pretend for a moment that Postgres does not have the `avg` function built-in. With the previous Rel8, there is no way to directly write `sum(x) / count(x)`. The best you could do would something like:

```haskell
fmap (\(total, size) -> total / fromIntegral size) $ aggregate $ do
  foo <- each fooSchema
  pure (sum foo.x, count foo.x)
```

The key thing is that the mapping can only happen after `aggregate` is called. Whereas with the `Profunctor`-based `Aggregator` this is just `(/) <$> sum <*> fmap fromIntegral count`. This isn't too bad if the only thing you want to do is computing the average, but if you're doing a complicated aggregation with several things happening at once then you might need to do several unrelated post-processings after the `aggregate`. We really want a way to bundle up the postmapping with the aggregation itself and have that as a singular composable unit. Another example is the `listAggExpr` function. The only reason Rel8 exports this is because it can't be directly expressed in terms of `listAgg`. With the `Profunctor`-based `Aggregator` it can be, it's just `(id $*) <$> listAgg`, it no longer needs to be a special case.

The original attempt in #37 recognised that it can be awkward to have to write `lmap (.x) sum`, so instead of sum having the type signature `Aggregator (Expr a) (Expr a)`, it had the type signature `(i -> Expr a) -> Aggregator i (Expr a)`, so that you wouldn't have to use `lmap`, you could just type `sum (.x)`. However, there are many ways to compose `Aggregator`s — for example, if you wanted to use combinators from `product-profunctor` to combine aggregators, then you'd rather type `sum ***! count` than `sum id ***! count id`. So in this PR we keep the type of `sum` as `Aggregator (Expr a) (Expr a)`, but we also export `sumOn`, which has the bundled `lmap`.

The other major change is that this PR introduces two forms of aggregation — "semi"-aggregation and "full"-aggregation. Up until now, all aggregation in Rel8 was "semi"-aggregation, but "full"-aggregation feels a bit more natural and Haskelly.

Up until now, the `aggrgegate` combinator in Rel8 would return zero rows if given a query that itself returned zero rows, even if the aggregation functions that comprised it had identity values. So it was very common to see code like `fmap (fromMaybeTable 0) $ optional $ aggregate $ sum <$> _`. Again, we "know" that `0` is the identity value for `sum` and we really want some way to bundle those together and to say "return the identity value if there are zero rows". Rel8 now has this ability — it has both `Aggregator` and `Aggregator1`, with the former having identity values and the latter not. The `aggregate` function now takes an `Aggregator` and returns the identity value when encountering zero rows, whereas the `aggregate1` function takes an `Aggregator1` and behaves as before. `count`, `sum`, `and`, `or`, `listAgg` are `Aggregator`s (with the identity values `0`, `0`, `true`, `false` and `listTable []` respectively) and `groupBy`, `max` and `min` are `Aggregator1`s.

This also means that `many` is now just `aggregate listAgg` instead of `fmap (fromMaybeTable (listTable [])) . optional . aggregate . fmap listAgg`.

It should also be noted that these functions are actually polymorphic — `sum` will actually give you an `Aggregator'` that can be used as either `Aggregator` or `Aggregator1` without needing to explicitly convert between them. Similarly `aggregate1` can take either an `Aggegator` or an `Aggregator1` (though it won't use the identity value of the former).

Aggregation in Rel8 now supports more of the features of PostgresSQL supports. Three new combinators are introduced — `distinctAggregate`, `filterWhere` and `orderAggregateBy`.

Opaleye itself already supported `distinctAggregate` and indeed we used this to implement `countDistinct` as a special case, but we now support using `DISTINCT` on arbitrary aggregation functions.

`filterWhere` is new to both Rel8 and Opaleye. It corresponds to PostgreSQL's `FILTER (WHERE ...)` syntax in aggregations. It also uses the identity value of an `Aggregator` in the case where the given predicate returns zero rows. There is also `filterWhereOptional` which can be used with `Aggregator1`s.

`orderAggregateBy` allows the values within an aggregation to be ordered using a given ordering, mainly non-commutative aggregation functions like `listAgg`.
2023-06-18 20:05:00 +00:00
Shane
baaabe7ced
Fix #219 (#240)
We need to `rebind` immediately after calling `UNNEST`, before we call `fromColumns`.
2023-06-17 23:04:34 +00:00
Shane
e2a77b7310
Passing pure to withExplicit is invalid; materialize needs to take a function (#231) 2023-04-24 12:03:01 +01:00
Jonathan Lorimer
ecf34ae506
Add inet DBType (#227) 2023-04-14 09:56:03 +00:00
Shane
bbc97eaa45
Fix build on GHC 9.2 (#230)
PR #229 broke on GHC versions before 9.4 because `~` was not exported from `Data.Type.Equality` then.
2023-04-13 12:51:05 +01:00
Shane
e7cf7b2a34
Add support for two forms of PostgreSQL WITH syntax; loop and materialize (#180) 2023-03-28 17:50:29 +00:00
Shane
fef9064f91
Fix warnings on latest GHC (#229)
These are due to `~` no longer being built-in syntax.
2023-03-23 17:07:27 +00:00
Ollie Charles
20ff61fe37
Release 1.4.1.0 (#226) 2023-01-20 11:05:37 +00:00
Ollie Charles
408e353de0
Support GHC 9.4 (#199) 2023-01-20 10:59:39 +00:00
tomjaguarpaw
6c038ecb1d
Use less Opaleye internals in aggregation (#204) 2022-11-30 17:47:24 +00:00
Shane
42c1e1c853
Call quote_ident before nextval (#217)
It turns out you can't do `nextval('FooBar_id_seq')` in PostgreSQL, you need to write `nextval('"FooBar_id_seq"')`.
2022-11-22 17:05:36 +00:00
Shane
ed4b5bb0ac
Add supported for polymorphic nested Rel8ables (#215)
```haskell
data Nest t u f = Nest
  { foo :: t f
  , bar :: u f
  }
  deriving (Generic, Rel8able)
``
2022-11-08 13:47:12 +00:00
Gary Pamparà
42e38c70cc
Expose the createOrReplaceView function in the Rel8 module (#212) 2022-11-04 18:35:55 +00:00
Shane
36c2b1ef0c
Restore zero-based indexed (#214)
In #182 we introduced window functions, and also refactored `indexed` to use the new window function support via `rowNumber`.

However, previously `indexed` was zero-based, whereas PostgreSQL's `row_number()` function counts from one, so this change silently changed the behaviour of `indexed`.

This commit restores the previous behaviour.
2022-11-02 18:35:48 +00:00
Shane
fd0b449c8c
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.
2022-11-01 14:34:20 +00:00
Gary Pamparà
38cbebc7d0
Allow for view creation or the replacing of an existing views (#209) 2022-10-18 18:35:16 +00:00
Shane
d2f05bdcfd
Add Monoid instance for Query (#207) 2022-10-17 12:00:40 +01:00
Shane
5eb5689110
Fix insertion of default values (#206)
At some point, possibly due to a change in Opaleye, insertion of default values (using `unsafeDefault`) stopped working in Rel8.

In general, PostgreSQL only allows insertion of default values in insert statements that have the form `INSERT INTO table VALUES (DEFAULT)`. The way Opaleye typically generates SQL, the Rel8 equivalent to that code would come out looking more like `INSERT INTO table SELECT * FROM (VALUES (DEFAULT)) q`. Because the `VALUES` is wrapped in a `SELECT`, the `DEFAULT` becomes invalud PostgreSQL syntax.

To get around this, we special case `VALUES` when generating SQL for INSERT statements. However, something in the Opaleye representation changed and this special case was no longer firing. This commit fixes that.
2022-10-17 11:04:36 +01:00
Shane
2e82fccb02
Window functions (#182) 2022-10-14 02:06:07 +01:00
Shane
c69504ecc5
Upgrade Haskell.nix (#205) 2022-10-14 02:00:10 +01:00
tomjaguarpaw
44d5d0a3b7
Remove unused toOrderExprs (#202) 2022-08-30 08:14:32 +01:00
Marco Perone
227cd33886
Correcct DeriveAnyType into DeriveAnyClass (#200) 2022-08-22 11:05:57 +01:00
Ollie Charles
5686bd8794
Release 1.4 (#198) 2022-08-17 14:53:06 +01:00
Ollie Charles
38a92887d6
Support hasql-1.6 (#195)
Co-authored-by: Shane <shane.obrien@circuithub.com>
2022-08-16 17:50:39 +01:00
tomjaguarpaw
6791677f58
Use Opaleye adaptors (#190)
* Add htraverseP

* Add traverseFieldP

* Define and use fromOpaleyespec

* Use traverseFieldP

* Add htraversePWithField and use it in valuesspec

* Use aggregatorApply

* Update HTable.hs

Co-authored-by: Ollie Charles <ollie@ocharles.org.uk>
Co-authored-by: Shane <shane.obrien@circuithub.com>
2022-08-16 17:34:22 +01:00
Ollie Charles
961e1baa09
Upgrade Haskell.nix (#194) 2022-08-16 16:18:46 +00:00
Shane
c060f6bb3c
Add alignMaybeTable (#196) 2022-08-12 14:37:45 +00:00
tomjaguarpaw
71392dc7b9
Depend on Opaleye.Table rather than Opaleye.Internal.Table (#191) 2022-07-26 10:31:12 +01:00
Shane
58a7ecdc4e
Add AltTable and AlternativeTable instances for NullTable (#187) 2022-07-01 10:38:26 +00:00
Marco Perone
df1a27153e
Add example for groupBy (#184) 2022-06-25 08:50:43 +00:00
Shane
2a31fe984a
greatest and least were unfortunately incorrect, their implementations were the wrong way around. greatestExpr and leastExpr were unaffected by this. (#183) 2022-06-20 14:56:28 +01:00
Shane
4b4cce9c44
Add fromMaybeTable (#179)
A little helper function for `MaybeTable`s analogous to `Data.Maybe.fromMaybe`.
2022-06-09 13:59:53 +00:00
Shane
bdcfd29623
Optimize <|>: implementation of Tabulation (#178)
`Tabulation` has an instance of `AltTable`, which is intended to uphold the following law:

```haskell
fromQuery a <|>: fromQuery b = fromQuery (a <|>: b)
```

The previous implementation was not actually defined in terms of `Query`'s `<|>:` (i.e., `UNION ALL`), because not every `Tabulation` can be safely `toQuery`'d. Instead it used a combination of `alignWith`, `catNonEmptyTable` and `some` that worked even on "magic" `Tabulation`s.

However, using `unsafePeekQuery`, we can actually "statically" determine if a `Tabulation` is "magic" or not, which means we can selectively switch the implementation to use `Query`'s `<|>:` where possible. This produces a simpler and usually faster query.
2022-06-09 13:54:33 +00:00
Chris Done
6183b23b12
Write up some haddocks for ADTs based on Shane's notes (#177)
Co-authored-by: Ollie Charles <ollie@ocharles.org.uk>
2022-06-07 08:16:26 +00:00
Shane
99e868b84b
Add NullTable (#173)
This is an alternative to `MaybeTable` that doesn't use a tag columns. It's less flexible (no `Functor` or `Applicative` instance) and is meaningless when used with a table that has no non-nullable columns (so nesting `NullTable` is redundant). But in situations where the underlying `Table` does have non-nullable columns, it can losslessly converted to and from `MaybeTable`.

It is useful for embedding into a base table when you don't want to store the extra tag column in your schema.

Co-authored-by: Ollie Charles <ollie@ocharles.org.uk>
2022-05-20 12:34:45 +01:00
Kraig McKernan
d0a54b7244
Update cookbook to use some over listAgg (#154) 2022-03-12 11:48:45 +00:00