2018-12-13 10:26:15 +03:00
|
|
|
module Hasura.RQL.DML.Count
|
|
|
|
( CountQueryP1 (..),
|
|
|
|
validateCountQWith,
|
|
|
|
validateCountQ,
|
|
|
|
runCount,
|
|
|
|
countQToTx,
|
|
|
|
)
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Data.Aeson
|
2020-11-02 14:50:40 +03:00
|
|
|
import Data.ByteString.Builder qualified as BB
|
|
|
|
import Data.Sequence qualified as DS
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
import Database.PG.Query qualified as PG
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.Backends.Postgres.Connection.MonadTx
|
|
|
|
import Hasura.Backends.Postgres.Execute.Types
|
2020-11-02 14:50:40 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.DML qualified as S
|
2020-10-27 16:53:49 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
2020-11-02 14:50:40 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.BoolExp
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2020-08-27 19:36:39 +03:00
|
|
|
import Hasura.Prelude
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.DML.Internal
|
2020-11-12 12:25:48 +03:00
|
|
|
import Hasura.RQL.DML.Types
|
|
|
|
import Hasura.RQL.IR.BoolExp
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Metadata
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.SQL.Types
|
2021-01-09 02:09:15 +03:00
|
|
|
import Hasura.Session
|
2023-05-17 11:53:31 +03:00
|
|
|
import Hasura.Table.Cache
|
2020-12-28 15:56:00 +03:00
|
|
|
import Hasura.Tracing qualified as Tracing
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
data CountQueryP1 = CountQueryP1
|
2022-08-01 12:32:04 +03:00
|
|
|
{ cqp1Table :: QualifiedTable,
|
|
|
|
cqp1Where :: (AnnBoolExpSQL ('Postgres 'Vanilla), Maybe (AnnBoolExpSQL ('Postgres 'Vanilla))),
|
|
|
|
cqp1Distinct :: Maybe [PGCol]
|
2020-10-22 23:42:27 +03:00
|
|
|
}
|
|
|
|
deriving (Eq)
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
mkSQLCount ::
|
|
|
|
CountQueryP1 -> S.Select
|
|
|
|
mkSQLCount (CountQueryP1 tn (permFltr, mWc) mDistCols) =
|
|
|
|
S.mkSelect
|
2018-11-14 15:59:59 +03:00
|
|
|
{ S.selExtr = [S.Extractor S.countStar Nothing],
|
2018-06-27 16:11:32 +03:00
|
|
|
S.selFrom =
|
2023-05-24 16:51:56 +03:00
|
|
|
Just
|
|
|
|
$ S.FromExp
|
2018-06-27 16:11:32 +03:00
|
|
|
[S.mkSelFromExp False innerSel $ TableName "r"]
|
|
|
|
}
|
|
|
|
where
|
|
|
|
finalWC =
|
2023-05-24 16:51:56 +03:00
|
|
|
toSQLBoolExp (S.QualTable tn)
|
|
|
|
$ maybe permFltr (andAnnBoolExps permFltr) mWc
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
innerSel =
|
|
|
|
partSel
|
|
|
|
{ S.selFrom = Just $ S.mkSimpleFromExp tn,
|
|
|
|
S.selWhere = S.WhereFrag <$> Just finalWC
|
|
|
|
}
|
|
|
|
|
|
|
|
partSel = case mDistCols of
|
|
|
|
Just distCols ->
|
|
|
|
let extrs = flip map distCols $ \c -> S.Extractor (S.mkSIdenExp c) Nothing
|
|
|
|
in S.mkSelect
|
|
|
|
{ S.selDistinct = Just S.DistinctSimple,
|
|
|
|
S.selExtr = extrs
|
2021-09-24 01:56:37 +03:00
|
|
|
}
|
2018-06-27 16:11:32 +03:00
|
|
|
Nothing ->
|
|
|
|
S.mkSelect
|
2020-01-16 07:53:28 +03:00
|
|
|
{ S.selExtr = [S.Extractor (S.SEStar Nothing) Nothing]
|
2018-06-27 16:11:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
-- SELECT count(*) FROM (SELECT DISTINCT c1, .. cn FROM .. WHERE ..) r;
|
|
|
|
-- SELECT count(*) FROM (SELECT * FROM .. WHERE ..) r;
|
2018-12-13 10:26:15 +03:00
|
|
|
validateCountQWith ::
|
2021-04-22 00:44:37 +03:00
|
|
|
(UserInfoM m, QErrM m, TableInfoRM ('Postgres 'Vanilla) m) =>
|
Specialize `RQL.DML` to postgres.
### Description
When generalizing the code, back in late 2020, we over-eagerly generalized parts of the code that are specific to RQL's DML. This was in part due to the fact that, at the time, the DML types were all mixed alongside other types in `RQL.Types`. As a result, a lot of `RQL.DML.Internal` was generic over the backend type, instead of being specialized to `'Postgres 'Vanilla`.
A consequence of this is that, before this PR, `DML.Internal` ended up having a dependency on non-Postgres backends, due to the use of `annBoolExp`, which requires a `BackendMetadata` instance. Since the code was written in a generic manner, `DML.Internal` in turn depended on having the metadata instances in scope... This PR changes that to, instead, explicitly import the Postgres instance.
(Note that this module didn't import `RQL.Types.Metadata.Instances`, but depends on a module that imports it, and **orphan instances are transitively imported**, as evidenced by the need for that explicit import in #4568.)
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4573
GitOrigin-RevId: 7b82b5d7c23c03654518a1816802d400f37c3c64
2022-05-27 21:22:06 +03:00
|
|
|
SessionVariableBuilder m ->
|
2021-04-22 00:44:37 +03:00
|
|
|
(ColumnType ('Postgres 'Vanilla) -> Value -> m S.SQLExp) ->
|
2018-06-27 16:11:32 +03:00
|
|
|
CountQuery ->
|
|
|
|
m CountQueryP1
|
2021-01-07 12:04:22 +03:00
|
|
|
validateCountQWith sessVarBldr prepValBldr (CountQuery qt _ mDistCols mWhere) = do
|
2022-04-26 18:12:47 +03:00
|
|
|
tableInfo <- askTableInfoSource qt
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Check if select is allowed
|
|
|
|
selPerm <-
|
2023-05-24 16:51:56 +03:00
|
|
|
modifyErr (<> selNecessaryMsg)
|
|
|
|
$ askSelPermInfo tableInfo
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
let colInfoMap = _tciFieldInfoMap $ _tiCoreInfo tableInfo
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
forM_ mDistCols $ \distCols -> do
|
|
|
|
let distColAsrns =
|
|
|
|
[ checkSelOnCol selPerm,
|
2021-09-09 14:54:19 +03:00
|
|
|
assertColumnExists colInfoMap relInDistColsErr
|
|
|
|
]
|
2018-06-27 16:11:32 +03:00
|
|
|
withPathK "distinct" $ verifyAsrns distColAsrns distCols
|
|
|
|
|
|
|
|
-- convert the where clause
|
|
|
|
annSQLBoolExp <- forM mWhere $ \be ->
|
2023-05-24 16:51:56 +03:00
|
|
|
withPathK "where"
|
|
|
|
$ convBoolExp colInfoMap selPerm be sessVarBldr colInfoMap (valueParserWithCollectableType prepValBldr)
|
2019-04-17 12:48:41 +03:00
|
|
|
|
|
|
|
resolvedSelFltr <-
|
2023-05-24 16:51:56 +03:00
|
|
|
convAnnBoolExpPartialSQL sessVarBldr
|
|
|
|
$ spiFilter selPerm
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
return
|
|
|
|
$ CountQueryP1
|
2018-06-27 16:11:32 +03:00
|
|
|
qt
|
2019-04-17 12:48:41 +03:00
|
|
|
(resolvedSelFltr, annSQLBoolExp)
|
2018-06-27 16:11:32 +03:00
|
|
|
mDistCols
|
|
|
|
where
|
|
|
|
selNecessaryMsg =
|
|
|
|
"; \"count\" is only allowed if the role "
|
|
|
|
<> "has \"select\" permissions on the table"
|
|
|
|
relInDistColsErr =
|
|
|
|
"Relationships can't be used in \"distinct\"."
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
validateCountQ ::
|
2019-11-20 21:21:30 +03:00
|
|
|
(QErrM m, UserInfoM m, CacheRM m) =>
|
2021-01-07 12:04:22 +03:00
|
|
|
CountQuery ->
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
m (CountQueryP1, DS.Seq PG.PrepArg)
|
2021-01-07 12:04:22 +03:00
|
|
|
validateCountQ query = do
|
|
|
|
let source = cqSource query
|
2022-04-26 18:12:47 +03:00
|
|
|
tableCache :: TableCache ('Postgres 'Vanilla) <- fold <$> askTableCache source
|
2023-05-24 16:51:56 +03:00
|
|
|
flip runTableCacheRT tableCache
|
|
|
|
$ runDMLP1T
|
|
|
|
$ validateCountQWith sessVarFromCurrentSetting binRHSBuilder query
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
countQToTx ::
|
2022-10-06 12:07:14 +03:00
|
|
|
(MonadTx m) =>
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
(CountQueryP1, DS.Seq PG.PrepArg) ->
|
2019-03-18 19:22:21 +03:00
|
|
|
m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
countQToTx (u, p) = do
|
2018-11-16 15:40:23 +03:00
|
|
|
qRes <-
|
2023-05-24 16:51:56 +03:00
|
|
|
liftTx
|
|
|
|
$ PG.rawQE
|
2018-11-16 15:40:23 +03:00
|
|
|
dmlTxErrorHandler
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
(PG.fromBuilder countSQL)
|
2018-11-16 15:40:23 +03:00
|
|
|
(toList p)
|
|
|
|
True
|
2019-03-18 19:22:21 +03:00
|
|
|
return $ encJFromBuilder $ encodeCount qRes
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
|
|
|
countSQL = toSQL $ mkSQLCount u
|
Import `pg-client-hs` as `PG`
Result of executing the following commands:
```shell
# replace "as Q" imports with "as PG" (in retrospect this didn't need a regex)
git grep -lE 'as Q($|[^a-zA-Z])' -- '*.hs' | xargs sed -i -E 's/as Q($|[^a-zA-Z])/as PG\1/'
# replace " Q." with " PG."
git grep -lE ' Q\.' -- '*.hs' | xargs sed -i 's/ Q\./ PG./g'
# replace "(Q." with "(PG."
git grep -lE '\(Q\.' -- '*.hs' | xargs sed -i 's/(Q\./(PG./g'
# ditto, but for [, |, { and !
git grep -lE '\[Q\.' -- '*.hs' | xargs sed -i 's/\[Q\./\[PG./g'
git grep -l '|Q\.' -- '*.hs' | xargs sed -i 's/|Q\./|PG./g'
git grep -l '{Q\.' -- '*.hs' | xargs sed -i 's/{Q\./{PG./g'
git grep -l '!Q\.' -- '*.hs' | xargs sed -i 's/!Q\./!PG./g'
```
(Doing the `grep -l` before the `sed`, instead of `sed` on the entire codebase, reduces the number of `mtime` updates, and so reduces how many times a file gets recompiled while checking intermediate results.)
Finally, I manually removed a broken and unused `Arbitrary` instance in `Hasura.RQL.Network`. (It used an `import Test.QuickCheck.Arbitrary as Q` statement, which was erroneously caught by the first find-replace command.)
After this PR, `Q` is no longer used as an import qualifier. That was not the goal of this PR, but perhaps it's a useful fact for future efforts.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5933
GitOrigin-RevId: 8c84c59d57789111d40f5d3322c5a885dcfbf40e
2022-09-20 22:54:43 +03:00
|
|
|
encodeCount (PG.SingleRow (Identity c)) =
|
2018-06-27 16:11:32 +03:00
|
|
|
BB.byteString "{\"count\":" <> BB.intDec c <> BB.char7 '}'
|
|
|
|
|
2023-01-25 10:12:53 +03:00
|
|
|
-- TODO: What does this do?
|
2018-12-13 10:26:15 +03:00
|
|
|
runCount ::
|
2020-12-28 15:56:00 +03:00
|
|
|
( QErrM m,
|
|
|
|
UserInfoM m,
|
|
|
|
CacheRM m,
|
|
|
|
MonadIO m,
|
|
|
|
MonadBaseControl IO m,
|
2021-03-02 07:26:31 +03:00
|
|
|
Tracing.MonadTrace m,
|
|
|
|
MetadataM m
|
2021-01-07 12:04:22 +03:00
|
|
|
) =>
|
|
|
|
CountQuery ->
|
|
|
|
m EncJSON
|
|
|
|
runCount q = do
|
2021-04-22 00:44:37 +03:00
|
|
|
sourceConfig <- askSourceConfig @('Postgres 'Vanilla) (cqSource q)
|
2023-01-25 10:12:53 +03:00
|
|
|
validateCountQ q >>= runTxWithCtx (_pscExecCtx sourceConfig) (Tx PG.ReadOnly Nothing) LegacyRQLQuery . countQToTx
|