2021-08-04 14:42:24 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2021-09-24 01:56:37 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
-- | Convert the simple AST to an SQL query, ready to be passed
|
|
|
|
-- to the mysql package's query/exec functions.
|
|
|
|
module Hasura.Backends.MySQL.ToQuery
|
2021-09-24 01:56:37 +03:00
|
|
|
( Printer,
|
|
|
|
toQueryPretty,
|
|
|
|
fromSelect,
|
|
|
|
toQueryFlat,
|
|
|
|
Query (..),
|
2021-10-22 02:50:18 +03:00
|
|
|
renderBuilderPretty,
|
|
|
|
runBuilderPretty,
|
2021-08-04 14:42:24 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.ByteString (ByteString)
|
2021-10-22 02:50:18 +03:00
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.List (intersperse)
|
|
|
|
import Data.String
|
|
|
|
import Data.Text qualified as T
|
2021-10-22 02:50:18 +03:00
|
|
|
import Data.Text.Encoding qualified as T
|
|
|
|
import Data.Text.Lazy.Builder qualified as LT
|
|
|
|
import Data.Tuple (swap)
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.Backends.MySQL.Types
|
|
|
|
import Hasura.Prelude hiding (GT, LT)
|
2021-08-04 14:42:24 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
newtype Query = Query {unQuery :: ByteString} deriving (Show, Eq, Monoid, Semigroup)
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
data Printer
|
|
|
|
= SeqPrinter [Printer]
|
|
|
|
| SepByPrinter Printer [Printer]
|
|
|
|
| NewlinePrinter
|
|
|
|
| QueryPrinter Query
|
|
|
|
| IndentPrinter Int Printer
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
instance IsString Printer where
|
|
|
|
fromString = QueryPrinter . Query . fromString
|
|
|
|
|
|
|
|
(<+>) :: Printer -> Printer -> Printer
|
2021-09-24 01:56:37 +03:00
|
|
|
(<+>) x y = SeqPrinter [x, y]
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
-- Printer generators
|
|
|
|
|
|
|
|
fromExpression :: Expression -> Printer
|
|
|
|
fromExpression =
|
|
|
|
\case
|
|
|
|
ValueExpression value -> QueryPrinter (fromScalarType value)
|
|
|
|
AndExpression xs ->
|
|
|
|
case xs of
|
2021-10-22 02:50:18 +03:00
|
|
|
[] -> truePrinter
|
2021-08-04 14:42:24 +03:00
|
|
|
_ ->
|
|
|
|
SepByPrinter
|
|
|
|
(NewlinePrinter <+> "AND ")
|
|
|
|
(fmap (\x -> "(" <+> fromExpression x <+> ")") (toList xs))
|
|
|
|
OrExpression xs ->
|
|
|
|
case xs of
|
|
|
|
[] -> falsePrinter
|
|
|
|
_ ->
|
|
|
|
SepByPrinter
|
|
|
|
(NewlinePrinter <+> "OR ")
|
|
|
|
(fmap (\x -> "(" <+> fromExpression x <+> ")") (toList xs))
|
|
|
|
NotExpression expression -> "NOT " <+> fromExpression expression
|
2021-09-24 01:56:37 +03:00
|
|
|
ExistsExpression sel -> "EXISTS (" <+> fromSelect sel <+> ")"
|
2021-10-22 02:50:18 +03:00
|
|
|
InExpression x xs ->
|
|
|
|
fromExpression x <+> " IN " <+> SeqPrinter (fmap fromExpression xs)
|
2021-08-04 14:42:24 +03:00
|
|
|
ColumnExpression fieldName -> fromFieldName fieldName
|
|
|
|
MethodExpression field method args ->
|
2021-09-24 01:56:37 +03:00
|
|
|
fromExpression field <+> "."
|
|
|
|
<+> fromString (T.unpack method)
|
|
|
|
<+> "("
|
|
|
|
<+> SeqPrinter (map fromExpression args)
|
|
|
|
<+> ")"
|
2021-08-04 14:42:24 +03:00
|
|
|
OpExpression op x y ->
|
2021-09-24 01:56:37 +03:00
|
|
|
"("
|
|
|
|
<+> fromExpression x
|
|
|
|
<+> ") "
|
|
|
|
<+> fromOp op
|
|
|
|
<+> " ("
|
|
|
|
<+> fromExpression y
|
|
|
|
<+> ")"
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromScalarType :: ScalarValue -> Query
|
2021-10-22 02:50:18 +03:00
|
|
|
fromScalarType = \case
|
|
|
|
BigValue v -> Query $ fromString $ show v
|
|
|
|
BinaryValue v -> Query $ fromString $ show v
|
|
|
|
BitValue v -> Query $ fromString $ show v
|
|
|
|
BlobValue v -> Query $ fromString $ show v
|
|
|
|
CharValue v -> Query $ fromString $ show v
|
|
|
|
DatetimeValue v -> Query $ fromString $ show v
|
|
|
|
DateValue v -> Query $ fromString $ show v
|
|
|
|
DecimalValue v -> Query $ fromString $ show v
|
|
|
|
DoubleValue v -> Query $ fromString $ show v
|
|
|
|
EnumValue v -> Query $ fromString $ show v
|
|
|
|
FloatValue v -> Query $ fromString $ show v
|
|
|
|
GeometrycollectionValue v -> Query $ fromString $ show v
|
|
|
|
GeometryValue v -> Query $ fromString $ show v
|
|
|
|
IntValue v -> Query $ fromString $ show v
|
|
|
|
JsonValue v -> Query $ fromString $ show v
|
|
|
|
LinestringValue v -> Query $ fromString $ show v
|
|
|
|
MediumValue v -> Query $ fromString $ show v
|
|
|
|
MultilinestringValue v -> Query $ fromString $ show v
|
|
|
|
MultipointValue v -> Query $ fromString $ show v
|
|
|
|
MultipolygonValue v -> Query $ fromString $ show v
|
|
|
|
NullValue -> Query $ fromString "NULL"
|
|
|
|
NumericValue v -> Query $ fromString $ show v
|
|
|
|
PointValue v -> Query $ fromString $ show v
|
|
|
|
PolygonValue v -> Query $ fromString $ show v
|
|
|
|
SmallValue v -> Query $ fromString $ show v
|
|
|
|
TextValue v -> Query $ fromString $ show v
|
|
|
|
TimestampValue v -> Query $ fromString $ show v
|
|
|
|
TimeValue v -> Query $ fromString $ show v
|
|
|
|
TinyValue v -> Query $ fromString $ show v
|
|
|
|
VarbinaryValue v -> Query $ fromString $ show v
|
|
|
|
VarcharValue v -> Query $ fromString $ show v
|
|
|
|
YearValue v -> Query $ fromString $ show v
|
|
|
|
other -> error $ "fromscalartype: not implemented " <> show other
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromOp :: Op -> Printer
|
|
|
|
fromOp =
|
|
|
|
\case
|
2021-09-24 01:56:37 +03:00
|
|
|
LT -> "<"
|
|
|
|
GT -> ">"
|
|
|
|
GTE -> ">="
|
|
|
|
LTE -> "<="
|
|
|
|
IN -> "IN"
|
|
|
|
NIN -> "NOT IN"
|
|
|
|
LIKE -> "LIKE"
|
2021-08-04 14:42:24 +03:00
|
|
|
NLIKE -> "NOT LIKE"
|
2021-09-24 01:56:37 +03:00
|
|
|
EQ' -> "="
|
|
|
|
NEQ' -> "!="
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromFieldName :: FieldName -> Printer
|
|
|
|
fromFieldName (FieldName {..}) =
|
|
|
|
fromNameText fNameEntity <+> "." <+> fromNameText fName
|
|
|
|
|
|
|
|
fromSelect :: Select -> Printer
|
2021-10-22 02:50:18 +03:00
|
|
|
fromSelect Select {..} =
|
|
|
|
SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
$ [ "SELECT "
|
|
|
|
<+> IndentPrinter
|
|
|
|
7
|
|
|
|
( SepByPrinter
|
|
|
|
("," <+> NewlinePrinter)
|
|
|
|
(map fromProjection (toList selectProjections))
|
|
|
|
),
|
|
|
|
"FROM " <+> IndentPrinter 5 (fromFrom selectFrom),
|
|
|
|
fromWhere selectWhere,
|
|
|
|
fromOrderBys selectOrderBy,
|
|
|
|
fromOffsetAndLimit selectSqlTop selectSqlOffset
|
|
|
|
]
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
-- https://dev.mysql.com/doc/refman/5.7/en/select.html
|
2021-10-22 02:50:18 +03:00
|
|
|
fromOffsetAndLimit :: Top -> Maybe Int -> Printer
|
2021-08-04 14:42:24 +03:00
|
|
|
fromOffsetAndLimit NoTop Nothing = ""
|
|
|
|
fromOffsetAndLimit NoTop (Just offset) =
|
|
|
|
SeqPrinter
|
|
|
|
[ "LIMIT " <+> fromString (show (maxBound :: Int)),
|
2021-10-22 02:50:18 +03:00
|
|
|
IndentPrinter 9 (SepByPrinter NewlinePrinter [" OFFSET " <+> fromString (show offset)])
|
2021-08-04 14:42:24 +03:00
|
|
|
]
|
|
|
|
fromOffsetAndLimit (Top val) Nothing = SeqPrinter ["LIMIT " <+> fromString (show val)]
|
|
|
|
fromOffsetAndLimit (Top val) (Just offset) =
|
|
|
|
SeqPrinter
|
|
|
|
[ "LIMIT " <+> fromString (show val),
|
2021-10-22 02:50:18 +03:00
|
|
|
IndentPrinter 9 (SepByPrinter NewlinePrinter [" OFFSET " <+> fromString (show offset)])
|
2021-08-04 14:42:24 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
fromOrderBys ::
|
2021-10-22 02:50:18 +03:00
|
|
|
Maybe (NonEmpty OrderBy) -> Printer
|
|
|
|
fromOrderBys Nothing = ""
|
|
|
|
fromOrderBys morderBys =
|
2021-08-04 14:42:24 +03:00
|
|
|
SeqPrinter
|
2021-09-24 01:56:37 +03:00
|
|
|
[ "ORDER BY ",
|
|
|
|
IndentPrinter
|
2021-08-04 14:42:24 +03:00
|
|
|
9
|
2021-09-24 01:56:37 +03:00
|
|
|
( SepByPrinter
|
|
|
|
NewlinePrinter
|
|
|
|
[ case morderBys of
|
|
|
|
Nothing -> ""
|
|
|
|
Just orderBys ->
|
|
|
|
SepByPrinter
|
|
|
|
("," <+> NewlinePrinter)
|
2021-10-22 02:50:18 +03:00
|
|
|
(concatMap fromOrderBy (toList orderBys))
|
2021-09-24 01:56:37 +03:00
|
|
|
]
|
|
|
|
)
|
2021-08-04 14:42:24 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
fromOrderBy :: OrderBy -> [Printer]
|
|
|
|
fromOrderBy OrderBy {..} =
|
2021-09-24 01:56:37 +03:00
|
|
|
[ fromNullsOrder orderByFieldName orderByNullsOrder,
|
2021-08-04 14:42:24 +03:00
|
|
|
-- Above: This doesn't do anything when using text, ntext or image
|
|
|
|
-- types. See below on CAST commentary.
|
2021-09-24 01:56:37 +03:00
|
|
|
wrapNullHandling (fromFieldName orderByFieldName)
|
|
|
|
<+> " "
|
|
|
|
<+> fromOrder orderByOrder
|
2021-08-04 14:42:24 +03:00
|
|
|
]
|
|
|
|
where
|
|
|
|
wrapNullHandling inner = inner
|
|
|
|
|
|
|
|
fromOrder :: Order -> Printer
|
|
|
|
fromOrder =
|
|
|
|
\case
|
2021-09-24 01:56:37 +03:00
|
|
|
Asc -> "ASC"
|
2021-08-04 14:42:24 +03:00
|
|
|
Desc -> "DESC"
|
|
|
|
|
2021-10-22 02:50:18 +03:00
|
|
|
-- Source <https://gregrs-uk.github.io/2011-02-02/mysql-order-by-with-nulls-first-or-last/>
|
2021-08-04 14:42:24 +03:00
|
|
|
fromNullsOrder :: FieldName -> NullsOrder -> Printer
|
|
|
|
fromNullsOrder fieldName =
|
|
|
|
\case
|
|
|
|
NullsAnyOrder -> ""
|
2021-10-22 02:50:18 +03:00
|
|
|
-- ISNULL(NULL)=1, ISNULL(_) = 0 -- therefore we need DESC to put
|
|
|
|
-- nulls first.
|
|
|
|
NullsFirst -> "ISNULL(" <+> fromFieldName fieldName <+> ") DESC"
|
|
|
|
NullsLast -> "ISNULL(" <+> fromFieldName fieldName <+> ") ASC"
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromProjection :: Projection -> Printer
|
|
|
|
fromProjection =
|
|
|
|
\case
|
|
|
|
ExpressionProjection aliasedExpression ->
|
|
|
|
fromAliased (fmap fromExpression aliasedExpression)
|
|
|
|
FieldNameProjection aliasedFieldName ->
|
|
|
|
fromAliased (fmap fromFieldName aliasedFieldName)
|
|
|
|
AggregateProjection aliasedAggregate ->
|
|
|
|
fromAliased (fmap fromAggregate aliasedAggregate)
|
2021-10-22 02:50:18 +03:00
|
|
|
AggregateProjections aliasedAggregates ->
|
|
|
|
fromAliased
|
|
|
|
( fmap
|
|
|
|
( \aggs ->
|
|
|
|
"STRUCT("
|
|
|
|
<+> IndentPrinter
|
|
|
|
7
|
|
|
|
( SepByPrinter
|
|
|
|
", "
|
|
|
|
(fmap (fromAliased . fmap fromAggregate) (toList aggs))
|
|
|
|
)
|
|
|
|
<+> ")"
|
|
|
|
)
|
|
|
|
aliasedAggregates
|
|
|
|
)
|
2021-08-04 14:42:24 +03:00
|
|
|
StarProjection -> "*"
|
2021-10-22 02:50:18 +03:00
|
|
|
EntityProjection aliasedEntity ->
|
|
|
|
fromAliased
|
|
|
|
( fmap
|
|
|
|
( \(fields :: [(FieldName, FieldOrigin)]) ->
|
|
|
|
-- Example:
|
|
|
|
-- STRUCT(
|
|
|
|
-- IFNULL(
|
|
|
|
-- `aa_articles1`.`aggregate`,
|
|
|
|
-- STRUCT(0 as count, struct(null as id) as sum)
|
|
|
|
-- ) as aggregate
|
|
|
|
-- ) AS `articles_aggregate`
|
|
|
|
--
|
|
|
|
-- The (AS `articles_aggregate`) part at the end is rendered by 'fromAliased' evaluating
|
|
|
|
-- at the root of this branch, and not by anything below
|
|
|
|
"STRUCT("
|
|
|
|
<+> ( SepByPrinter
|
|
|
|
", "
|
|
|
|
( fields
|
|
|
|
<&> \(fieldName@FieldName {..}, fieldOrigin :: FieldOrigin) ->
|
|
|
|
"IFNULL(" <+> fromFieldName fieldName <+> ", " <+> fromFieldOrigin fieldOrigin
|
|
|
|
<+> ") AS "
|
|
|
|
<+> fromNameText fName
|
|
|
|
)
|
|
|
|
)
|
|
|
|
<+> ")"
|
|
|
|
)
|
|
|
|
aliasedEntity
|
|
|
|
)
|
|
|
|
ArrayEntityProjection entityAlias aliasedEntity ->
|
|
|
|
fromAliased
|
|
|
|
( fmap
|
|
|
|
( \aggs ->
|
|
|
|
"ARRAY(SELECT AS STRUCT "
|
|
|
|
<+> IndentPrinter
|
|
|
|
7
|
|
|
|
(SepByPrinter ", " (fmap fromFieldNameNaked (toList aggs)))
|
|
|
|
<+> " FROM "
|
|
|
|
<+> fromNameText (entityAliasText entityAlias)
|
|
|
|
<+> ".agg)"
|
|
|
|
)
|
|
|
|
aliasedEntity
|
|
|
|
)
|
|
|
|
where
|
|
|
|
fromFieldNameNaked :: FieldName -> Printer
|
|
|
|
fromFieldNameNaked (FieldName {..}) =
|
|
|
|
fromNameText fName
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromAggregate :: Aggregate -> Printer
|
|
|
|
fromAggregate =
|
|
|
|
\case
|
|
|
|
CountAggregate countable -> "COUNT(" <+> fromCountable countable <+> ")"
|
|
|
|
OpAggregate text args ->
|
2021-09-24 01:56:37 +03:00
|
|
|
QueryPrinter (Query $ fromString $ show text)
|
|
|
|
<+> "("
|
|
|
|
<+> SepByPrinter ", " (map fromExpression (toList args))
|
|
|
|
<+> ")"
|
2021-08-04 14:42:24 +03:00
|
|
|
TextAggregate text -> fromExpression (ValueExpression (TextValue text))
|
|
|
|
|
|
|
|
fromCountable :: Countable FieldName -> Printer
|
|
|
|
fromCountable =
|
|
|
|
\case
|
|
|
|
StarCountable -> "*"
|
|
|
|
NonNullFieldCountable fields ->
|
|
|
|
SepByPrinter ", " (map fromFieldName (toList fields))
|
|
|
|
DistinctCountable fields ->
|
2021-09-24 01:56:37 +03:00
|
|
|
"DISTINCT "
|
|
|
|
<+> SepByPrinter ", " (map fromFieldName (toList fields))
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromWhere :: Where -> Printer
|
|
|
|
fromWhere =
|
|
|
|
\case
|
|
|
|
Where [] -> ""
|
|
|
|
Where expressions ->
|
2021-09-24 01:56:37 +03:00
|
|
|
"WHERE "
|
|
|
|
<+> IndentPrinter 6 (fromExpression (AndExpression expressions))
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromFrom :: From -> Printer
|
|
|
|
fromFrom =
|
|
|
|
\case
|
|
|
|
FromQualifiedTable aliasedQualifiedTableName ->
|
|
|
|
fromAliased (fmap fromTableName aliasedQualifiedTableName)
|
2021-10-22 02:50:18 +03:00
|
|
|
FromSelect select -> fromAliased (fmap (parens . fromSelect) select)
|
2021-08-04 14:42:24 +03:00
|
|
|
|
2021-10-22 02:50:18 +03:00
|
|
|
parens :: Printer -> Printer
|
|
|
|
parens x = "(" <+> IndentPrinter 1 x <+> ")"
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromTableName :: TableName -> Printer
|
|
|
|
fromTableName TableName {name, schema} =
|
2021-10-23 14:42:20 +03:00
|
|
|
maybe "" ((<+> ".") . fromNameText) schema <+> fromNameText name
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromAliased :: Aliased Printer -> Printer
|
|
|
|
fromAliased Aliased {..} =
|
2021-09-24 01:56:37 +03:00
|
|
|
aliasedThing
|
|
|
|
<+> ((" AS " <+>) . fromNameText) aliasedAlias
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
fromNameText :: Text -> Printer
|
|
|
|
fromNameText t = QueryPrinter (Query . fromString . T.unpack $ t)
|
|
|
|
|
2021-10-22 02:50:18 +03:00
|
|
|
truePrinter :: Printer
|
|
|
|
truePrinter = "TRUE"
|
2021-08-04 14:42:24 +03:00
|
|
|
|
2021-10-22 02:50:18 +03:00
|
|
|
falsePrinter :: Printer
|
|
|
|
falsePrinter = "FALSE"
|
2021-08-04 14:42:24 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Basic printing API
|
|
|
|
|
|
|
|
toQueryFlat :: Printer -> Query
|
|
|
|
toQueryFlat = go 0
|
|
|
|
where
|
|
|
|
go level =
|
|
|
|
\case
|
|
|
|
QueryPrinter q -> q
|
|
|
|
SeqPrinter xs -> mconcat (filter notEmpty (map (go level) xs))
|
|
|
|
SepByPrinter x xs ->
|
|
|
|
mconcat
|
|
|
|
(intersperse (go level x) (filter notEmpty (map (go level) xs)))
|
|
|
|
NewlinePrinter -> Query " "
|
|
|
|
IndentPrinter n p -> go (level + n) p
|
|
|
|
notEmpty = (/= mempty)
|
|
|
|
|
|
|
|
toQueryPretty :: Printer -> Query
|
|
|
|
toQueryPretty = go 0
|
|
|
|
where
|
|
|
|
go level =
|
|
|
|
\case
|
|
|
|
QueryPrinter q -> q
|
|
|
|
SeqPrinter xs -> mconcat (filter notEmpty (map (go level) xs))
|
|
|
|
SepByPrinter x xs ->
|
|
|
|
mconcat
|
|
|
|
(intersperse (go level x) (filter notEmpty (map (go level) xs)))
|
|
|
|
NewlinePrinter -> Query $ fromString $ show $ "\n" <> indentation level
|
|
|
|
IndentPrinter n p -> go (level + n) p
|
|
|
|
indentation n = T.replicate n " "
|
|
|
|
notEmpty = (/= mempty)
|
2021-10-22 02:50:18 +03:00
|
|
|
|
|
|
|
-- | Produces a query with holes, and a mapping for each
|
|
|
|
renderBuilderPretty :: Printer -> (LT.Builder, InsOrdHashMap Int ScalarValue)
|
|
|
|
renderBuilderPretty =
|
|
|
|
second (OMap.fromList . map swap . OMap.toList) . flip runState mempty
|
|
|
|
. runBuilderPretty
|
|
|
|
|
|
|
|
runBuilderPretty :: Printer -> State (InsOrdHashMap ScalarValue Int) LT.Builder
|
|
|
|
runBuilderPretty = go 0
|
|
|
|
where
|
|
|
|
go level =
|
|
|
|
\case
|
|
|
|
SeqPrinter xs -> fmap (mconcat . filter notEmpty) (mapM (go level) xs)
|
|
|
|
SepByPrinter x xs -> do
|
|
|
|
i <- go level x
|
|
|
|
fmap (mconcat . intersperse i . filter notEmpty) (mapM (go level) xs)
|
|
|
|
NewlinePrinter -> pure ("\n" <> indentation level)
|
|
|
|
IndentPrinter n p -> go (level + n) p
|
|
|
|
QueryPrinter Query {unQuery = q} -> pure . LT.fromText . T.decodeUtf8 $ q
|
|
|
|
indentation n = LT.fromText (T.replicate n " ")
|
|
|
|
notEmpty = (/= mempty)
|
|
|
|
|
|
|
|
fromFieldOrigin :: FieldOrigin -> Printer
|
|
|
|
fromFieldOrigin = \case
|
|
|
|
NoOrigin -> "NULL"
|
|
|
|
AggregateOrigin aliasedAggregates ->
|
|
|
|
"STRUCT("
|
|
|
|
<+>
|
|
|
|
-- Example: "0 AS count, STRUCT(NULL AS id) AS sum"
|
|
|
|
SepByPrinter ", " (fromAliased . fmap fromNullAggregate <$> aliasedAggregates)
|
|
|
|
<+> ")"
|
|
|
|
|
|
|
|
fromNullAggregate :: Aggregate -> Printer
|
|
|
|
fromNullAggregate = \case
|
|
|
|
CountAggregate _ -> "0"
|
|
|
|
OpAggregate _text _exp -> "NULL"
|
|
|
|
TextAggregate _text -> "NULL"
|