1
Fork 0

add window functions (without frames

This commit is contained in:
Jake Wheat 2013-12-13 23:31:36 +02:00
parent 81e7aa818b
commit 64eb5a5c9d
4 changed files with 37 additions and 8 deletions
Language/SQL/SimpleSQL

View file

@ -161,6 +161,19 @@ to be.
> return $ App i es
> _ -> return $ AggregateApp i d es (fromMaybe [] od)
> windowSuffix :: ScalarExpr -> P ScalarExpr
> windowSuffix e@(App f es) =
> choice [try (keyword_ "over")
> *> parens (WindowApp f es
> <$> option [] partitionBy
> <*> option [] orderBy)
> ,return e]
> where
> partitionBy = try (keyword_ "partition") >>
> keyword_ "by" >>
> commaSep1 scalarExpr'
> windowSuffix e = return e
> scase :: P ScalarExpr
> scase =
@ -308,7 +321,7 @@ postgresql handles this
> ,extract
> ,subquery
> ,prefixUnaryOp
> ,try app
> ,(try app) >>= windowSuffix
> ,try dottedIden
> ,identifier
> ,sparens]

View file

@ -39,6 +39,15 @@ back into SQL source text. It attempts to format the output nicely.
> <+> commaSep (map scalarExpr es)
> <+> orderBy od)
> scalarExpr (WindowApp f es pb od) =
> text f <> parens (commaSep $ map scalarExpr es)
> <+> text "over"
> <+> parens ((case pb of
> [] -> empty
> _ -> text "partition by"
> <+> nest 4 (commaSep $ map scalarExpr pb))
> <+> orderBy od)
> scalarExpr (SpecialOp nm [a,b,c]) | nm `elem` ["between", "not between"] =
> sep [scalarExpr a
> ,text nm <+> scalarExpr b

View file

@ -24,6 +24,7 @@
> | AggregateApp String (Maybe Duplicates)
> [ScalarExpr]
> [(ScalarExpr,Direction)]
> | WindowApp String [ScalarExpr] [ScalarExpr] [(ScalarExpr,Direction)]
> -- the binop, prefixop and postfix op
> -- are used for symbol and keyword operators
> -- these are used even for the multiple keyword