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

View file

@ -161,6 +161,19 @@ to be.
> return $ App i es > return $ App i es
> _ -> return $ AggregateApp i d es (fromMaybe [] od) > _ -> 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 :: P ScalarExpr
> scase = > scase =
@ -308,7 +321,7 @@ postgresql handles this
> ,extract > ,extract
> ,subquery > ,subquery
> ,prefixUnaryOp > ,prefixUnaryOp
> ,try app > ,(try app) >>= windowSuffix
> ,try dottedIden > ,try dottedIden
> ,identifier > ,identifier
> ,sparens] > ,sparens]

View file

@ -39,6 +39,15 @@ back into SQL source text. It attempts to format the output nicely.
> <+> commaSep (map scalarExpr es) > <+> commaSep (map scalarExpr es)
> <+> orderBy od) > <+> 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"] = > scalarExpr (SpecialOp nm [a,b,c]) | nm `elem` ["between", "not between"] =
> sep [scalarExpr a > sep [scalarExpr a
> ,text nm <+> scalarExpr b > ,text nm <+> scalarExpr b

View file

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

View file

@ -184,14 +184,20 @@
> windowFunctions :: TestItem > windowFunctions :: TestItem
> windowFunctions = Group "windowFunctions" $ map (uncurry TestScalarExpr) > windowFunctions = Group "windowFunctions" $ map (uncurry TestScalarExpr)
> [{-("max(a) over ()", NumLit "1") > [("max(a) over ()", WindowApp "max" [Iden "a"] [] [])
> ,("count(*) over ()", NumLit "1") > ,("count(*) over ()", WindowApp "count" [Star] [] [])
> ,("max(a) over (partition by b)", NumLit "1") > ,("max(a) over (partition by b)"
> ,("sum(a) over (order by b)", NumLit "1") > ,WindowApp "max" [Iden "a"] [Iden "b"] [])
> ,("sum(a) over (partition by b order by c)", NumLit "1") > ,("max(a) over (partition by b,c)"
> ,("sum(a) over (partition by b order by c)", NumLit "1") > ,WindowApp "max" [Iden "a"] [Iden "b",Iden "c"] [])
> ,("sum(a) over (order by b)"
> ,WindowApp "sum" [Iden "a"] [] [(Iden "b", Asc)])
> ,("sum(a) over (order by b desc,c)"
> ,WindowApp "sum" [Iden "a"] [] [(Iden "b", Desc)
> ,(Iden "c", Asc)])
> ,("sum(a) over (partition by b order by c)"
> ,WindowApp "sum" [Iden "a"] [Iden "b"] [(Iden "c", Asc)])
> -- todo: check order by options, add frames > -- todo: check order by options, add frames
> -}
> ] > ]
> parens :: TestItem > parens :: TestItem