on dev help sign in
add more window functions

with arguments

commit3da5789f6f187f34ceed79caf72517b3bd6fc11e authorEitan Chatav <eitan.chatav@gmail.com> dateSat 12 Jan 2019, 15:38 (8 years ago) parentaa082a3
1 file changed+82
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+82
squeal-postgresql/src/Squeal/PostgreSQL/Expression.hs+82
@@ -152,6 +152,12 @@ module Squeal.PostgreSQL.Expression
152152 , denseRank
153153 , percentRank
154154 , cumeDist
155+ , ntile
156+ , lag
157+ , lead
158+ , firstValue
159+ , lastValue
160+ , nthValue
155161 -- * Sorting
156162 , SortExpression (..)
157163 , OrderBy (..)
@@ -1921,3 +1927,79 @@ cume_dist()
19211927-}
19221928cumeDist :: WindowFunction commons schemas from grp params ('NotNull 'PGfloat8)
19231929cumeDist = UnsafeWindowFunction "cume_dist()"
1930+
1931+{- | integer ranging from 1 to the argument value,
1932+dividing the partition as equally as possible
1933+-}
1934+ntile
1935+ :: Expression commons schemas from grp params ('NotNull 'PGint4)
1936+ -- ^ numuckets
1937+ -> WindowFunction commons schemas from grp params ('NotNull 'PGint4)
1938+ntile numBuckets = UnsafeWindowFunction $ "ntile"
1939+ <> parenthesized (renderSQL numBuckets)
1940+
1941+{- | returns value evaluated at the row that is offset rows before the current
1942+row within the partition; if there is no such row, instead return default
1943+(which must be of the same type as value). Both offset and default are
1944+evaluated with respect to the current row.
1945+-}
1946+lag
1947+ :: Expression commons schemas from grp params ty
1948+ -- ^ value
1949+ -> Expression commons schemas from grp params ('NotNull 'PGint4)
1950+ -- ^ offset
1951+ -> Expression commons schemas from grp params ty
1952+ -- ^ default
1953+ -> WindowFunction commons schemas from grp params ty
1954+lag value offset def = UnsafeWindowFunction $ "lag"
1955+ <> parenthesized
1956+ (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
1957+
1958+{- | returns value evaluated at the row that is offset rows after the current
1959+row within the partition; if there is no such row, instead return default
1960+(which must be of the same type as value). Both offset and default are
1961+evaluated with respect to the current row.
1962+-}
1963+lead
1964+ :: Expression commons schemas from grp params ty
1965+ -- ^ value
1966+ -> Expression commons schemas from grp params ('NotNull 'PGint4)
1967+ -- ^ offset
1968+ -> Expression commons schemas from grp params ty
1969+ -- ^ default
1970+ -> WindowFunction commons schemas from grp params ty
1971+lead value offset def = UnsafeWindowFunction $ "lag"
1972+ <> parenthesized
1973+ (commaSeparated ([renderSQL value, renderSQL offset, renderSQL def]))
1974+
1975+{- | returns value evaluated at the row that is the
1976+first row of the window frame
1977+-}
1978+firstValue
1979+ :: Expression commons schemas from grp params ty
1980+ -- ^ value
1981+ -> WindowFunction commons schemas from grp params ty
1982+firstValue value = UnsafeWindowFunction $ "first_value"
1983+ <> parenthesized (renderSQL value)
1984+
1985+{- | returns value evaluated at the row that is the
1986+last row of the window frame
1987+-}
1988+lastValue
1989+ :: Expression commons schemas from grp params ty
1990+ -- ^ value
1991+ -> WindowFunction commons schemas from grp params ty
1992+lastValue value = UnsafeWindowFunction $ "last_value"
1993+ <> parenthesized (renderSQL value)
1994+
1995+{- | returns value evaluated at the row that is the nth
1996+row of the window frame (counting from 1); null if no such row
1997+-}
1998+nthValue
1999+ :: Expression commons schemas from grp params (nullity ty)
2000+ -- ^ value
2001+ -> Expression commons schemas from grp params ('NotNull 'PGint4)
2002+ -- ^ nth
2003+ -> WindowFunction commons schemas from grp params ('Null ty)
2004+nthValue value nth = UnsafeWindowFunction $ "nth_value"
2005+ <> parenthesized (commaSeparated [renderSQL value, renderSQL nth])