2022-04-22 16:38:35 +03:00
|
|
|
-- | This module defines the top-level translation functions pertaining to
|
|
|
|
-- queries that use aggregation (i.e., /not/ so-called "simple" selects) into
|
|
|
|
-- Postgres AST.
|
|
|
|
module Hasura.Backends.Postgres.Translate.Select.Aggregate
|
|
|
|
( mkAggregateSelect,
|
|
|
|
selectAggregateQuerySQL,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Monad.Writer.Strict (runWriter)
|
|
|
|
import Database.PG.Query (Query, fromBuilder)
|
|
|
|
import Hasura.Backends.Postgres.SQL.DML (BoolExp (BELit), Select)
|
2022-09-16 17:59:23 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.RenameIdentifiers (renameIdentifiers)
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types (IsIdentifier (toIdentifier))
|
2022-04-22 16:38:35 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Select.AnnotatedFieldJSON
|
2022-04-22 20:18:20 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Select.Internal.GenerateSelect (generateSQLSelectFromArrayNode)
|
|
|
|
import Hasura.Backends.Postgres.Translate.Select.Internal.Process (processAnnAggregateSelect)
|
2022-09-16 17:59:23 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Types (MultiRowSelectNode (MultiRowSelectNode), SelectNode (SelectNode), SourcePrefixes (SourcePrefixes))
|
2022-04-22 16:38:35 +03:00
|
|
|
import Hasura.Prelude
|
2022-09-16 17:59:23 +03:00
|
|
|
import Hasura.RQL.IR.Select (AnnAggregateSelect, AnnSelectG (_asnStrfyNum))
|
2022-04-22 16:38:35 +03:00
|
|
|
import Hasura.RQL.Types.Backend (Backend)
|
|
|
|
import Hasura.RQL.Types.Common (FieldName (FieldName))
|
|
|
|
import Hasura.SQL.Backend (BackendType (Postgres))
|
|
|
|
import Hasura.SQL.Types (ToSQL (toSQL))
|
|
|
|
|
|
|
|
-- | Translates IR to Postgres queries for aggregated SELECTs.
|
|
|
|
--
|
|
|
|
-- See 'mkAggregateSelect' for the Postgres AST.
|
|
|
|
selectAggregateQuerySQL ::
|
|
|
|
forall pgKind.
|
|
|
|
(Backend ('Postgres pgKind), PostgresAnnotatedFieldJSON pgKind) =>
|
|
|
|
AnnAggregateSelect ('Postgres pgKind) ->
|
|
|
|
Query
|
2022-09-16 17:59:23 +03:00
|
|
|
selectAggregateQuerySQL = fromBuilder . toSQL . mkAggregateSelect
|
2022-04-22 16:38:35 +03:00
|
|
|
|
|
|
|
mkAggregateSelect ::
|
|
|
|
forall pgKind.
|
2022-09-16 17:59:23 +03:00
|
|
|
(Backend ('Postgres pgKind), PostgresAnnotatedFieldJSON pgKind) =>
|
2022-04-22 16:38:35 +03:00
|
|
|
AnnAggregateSelect ('Postgres pgKind) ->
|
|
|
|
Select
|
|
|
|
mkAggregateSelect annAggSel =
|
|
|
|
let ((selectSource, nodeExtractors, topExtractor), joinTree) =
|
|
|
|
runWriter $
|
|
|
|
flip runReaderT strfyNum $
|
|
|
|
processAnnAggregateSelect sourcePrefixes rootFieldName annAggSel
|
|
|
|
selectNode = SelectNode nodeExtractors joinTree
|
|
|
|
arrayNode = MultiRowSelectNode [topExtractor] selectNode
|
2022-07-22 18:27:42 +03:00
|
|
|
in renameIdentifiers $
|
2022-04-22 16:38:35 +03:00
|
|
|
generateSQLSelectFromArrayNode selectSource arrayNode $ BELit True
|
|
|
|
where
|
|
|
|
strfyNum = _asnStrfyNum annAggSel
|
|
|
|
rootFieldName = FieldName "root"
|
|
|
|
rootIdentifier = toIdentifier rootFieldName
|
|
|
|
sourcePrefixes = SourcePrefixes rootIdentifier rootIdentifier
|