2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
-- Here are the tests for the group by component of query exprs
|
|
|
|
|
2024-01-10 08:40:24 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2024-01-09 01:07:47 +01:00
|
|
|
module Language.SQL.SimpleSQL.GroupBy (groupByTests) where
|
|
|
|
|
|
|
|
import Language.SQL.SimpleSQL.TestTypes
|
|
|
|
import Language.SQL.SimpleSQL.Syntax
|
2024-02-04 17:00:59 +01:00
|
|
|
import Language.SQL.SimpleSQL.TestRunners
|
|
|
|
import Data.Text (Text)
|
2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
groupByTests :: TestItem
|
|
|
|
groupByTests = Group "groupByTests"
|
|
|
|
[simpleGroupBy
|
|
|
|
,newGroupBy
|
|
|
|
,randomGroupBy
|
|
|
|
]
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
q :: HasCallStack => Text -> QueryExpr -> TestItem
|
|
|
|
q src a = testQueryExpr ansi2011 src a
|
|
|
|
|
|
|
|
p :: HasCallStack => Text -> TestItem
|
|
|
|
p src = testParseQueryExpr ansi2011 src
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-01-09 01:07:47 +01:00
|
|
|
simpleGroupBy :: TestItem
|
2024-02-04 17:00:59 +01:00
|
|
|
simpleGroupBy = Group "simpleGroupBy"
|
|
|
|
[q "select a,sum(b) from t group by a"
|
|
|
|
$ toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)
|
2024-01-09 01:07:47 +01:00
|
|
|
,(App [Name Nothing "sum"] [Iden [Name Nothing "b"]],Nothing)]
|
2024-01-11 15:45:20 +01:00
|
|
|
,msFrom = [TRSimple [Name Nothing "t"]]
|
|
|
|
,msGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]]
|
2024-02-04 17:00:59 +01:00
|
|
|
}
|
2024-01-09 01:07:47 +01:00
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,q "select a,b,sum(c) from t group by a,b"
|
|
|
|
$ toQueryExpr $ makeSelect {msSelectList = [(Iden [Name Nothing "a"],Nothing)
|
2024-01-09 01:07:47 +01:00
|
|
|
,(Iden [Name Nothing "b"],Nothing)
|
|
|
|
,(App [Name Nothing "sum"] [Iden [Name Nothing "c"]],Nothing)]
|
2024-01-11 15:45:20 +01:00
|
|
|
,msFrom = [TRSimple [Name Nothing "t"]]
|
|
|
|
,msGroupBy = [SimpleGroup $ Iden [Name Nothing "a"]
|
2024-01-09 01:07:47 +01:00
|
|
|
,SimpleGroup $ Iden [Name Nothing "b"]]
|
2024-02-04 17:00:59 +01:00
|
|
|
}
|
2024-01-09 01:07:47 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
{-
|
|
|
|
test the new group by (), grouping sets, cube and rollup syntax (not
|
|
|
|
sure which sql version they were introduced, 1999 or 2003 I think).
|
|
|
|
-}
|
|
|
|
|
|
|
|
newGroupBy :: TestItem
|
2024-02-04 17:00:59 +01:00
|
|
|
newGroupBy = Group "newGroupBy"
|
|
|
|
[q "select * from t group by ()" $ ms [GroupingParens []]
|
|
|
|
,q "select * from t group by grouping sets ((), (a))"
|
|
|
|
$ ms [GroupingSets [GroupingParens []
|
|
|
|
,GroupingParens [SimpleGroup $ Iden [Name Nothing "a"]]]]
|
|
|
|
,q "select * from t group by cube(a,b)"
|
|
|
|
$ ms [Cube [SimpleGroup $ Iden [Name Nothing "a"], SimpleGroup $ Iden [Name Nothing "b"]]]
|
|
|
|
,q "select * from t group by rollup(a,b)"
|
|
|
|
$ ms [Rollup [SimpleGroup $ Iden [Name Nothing "a"], SimpleGroup $ Iden [Name Nothing "b"]]]
|
2024-01-09 01:07:47 +01:00
|
|
|
]
|
|
|
|
where
|
2024-01-11 15:45:20 +01:00
|
|
|
ms g = toQueryExpr $ makeSelect {msSelectList = [(Star,Nothing)]
|
|
|
|
,msFrom = [TRSimple [Name Nothing "t"]]
|
|
|
|
,msGroupBy = g}
|
2024-01-09 01:07:47 +01:00
|
|
|
|
|
|
|
randomGroupBy :: TestItem
|
2024-02-04 17:00:59 +01:00
|
|
|
randomGroupBy = Group "randomGroupBy"
|
|
|
|
[p "select * from t GROUP BY a"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a))"
|
|
|
|
,p "select * from t GROUP BY a,b,c"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b,c))"
|
|
|
|
,p "select * from t GROUP BY ROLLUP(a,b)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(a),\n\
|
|
|
|
\() )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY ROLLUP(b,a)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((b,a),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(b),\n\
|
|
|
|
\() )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY CUBE(a,b,c)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b,c),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(a,b),\n\
|
|
|
|
\(a,c),\n\
|
|
|
|
\(b,c),\n\
|
|
|
|
\(a),\n\
|
|
|
|
\(b),\n\
|
|
|
|
\(c),\n\
|
|
|
|
\() )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY ROLLUP(Province, County, City)"
|
|
|
|
,p "select * from t GROUP BY ROLLUP(Province, (County, City))"
|
|
|
|
,p "select * from t GROUP BY ROLLUP(Province, (County, City))"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((Province, County, City),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(Province),\n\
|
|
|
|
\() )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY GROUPING SETS((Province, County, City),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(Province, County),\n\
|
|
|
|
\(Province),\n\
|
|
|
|
\() )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY a, ROLLUP(b,c)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b,c),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(a,b),\n\
|
|
|
|
\(a) )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY a, b, ROLLUP(c,d)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b,c,d),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(a,b,c),\n\
|
|
|
|
\(a,b) )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY ROLLUP(a), ROLLUP(b,c)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b,c),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(a,b),\n\
|
|
|
|
\(a),\n\
|
|
|
|
\(b,c),\n\
|
|
|
|
\(b),\n\
|
|
|
|
\() )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY ROLLUP(a), CUBE(b,c)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b,c),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(a,b),\n\
|
|
|
|
\(a,c),\n\
|
|
|
|
\(a),\n\
|
|
|
|
\(b,c),\n\
|
|
|
|
\(b),\n\
|
|
|
|
\(c),\n\
|
|
|
|
\() )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY CUBE(a,b), ROLLUP(c,d)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b,c,d),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(a,b,c),\n\
|
|
|
|
\(a,b),\n\
|
|
|
|
\(a,c,d),\n\
|
|
|
|
\(a,c),\n\
|
|
|
|
\(a),\n\
|
|
|
|
\(b,c,d),\n\
|
|
|
|
\(b,c),\n\
|
|
|
|
\(b),\n\
|
|
|
|
\(c,d),\n\
|
|
|
|
\(c),\n\
|
|
|
|
\() )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY a, ROLLUP(a,b)"
|
|
|
|
,p "select * from t GROUP BY GROUPING SETS((a,b),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\(a) )"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY Region,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\ROLLUP(Sales_Person, WEEK(Sales_Date)),\n\
|
|
|
|
\CUBE(YEAR(Sales_Date), MONTH (Sales_Date))"
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "select * from t GROUP BY ROLLUP (Region, Sales_Person, WEEK(Sales_Date),\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\YEAR(Sales_Date), MONTH(Sales_Date) )"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT WEEK(SALES_DATE) AS WEEK,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\DAYOFWEEK(SALES_DATE) AS DAY_WEEK,\n\
|
|
|
|
\SALES_PERSON, SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\WHERE WEEK(SALES_DATE) = 13\n\
|
|
|
|
\GROUP BY WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE), SALES_PERSON\n\
|
|
|
|
\ORDER BY WEEK, DAY_WEEK, SALES_PERSON"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT WEEK(SALES_DATE) AS WEEK,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\DAYOFWEEK(SALES_DATE) AS DAY_WEEK,\n\
|
|
|
|
\SALES_PERSON, SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\WHERE WEEK(SALES_DATE) = 13\n\
|
|
|
|
\GROUP BY GROUPING SETS ( (WEEK(SALES_DATE), SALES_PERSON),\n\
|
|
|
|
\(DAYOFWEEK(SALES_DATE), SALES_PERSON))\n\
|
|
|
|
\ORDER BY WEEK, DAY_WEEK, SALES_PERSON"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT WEEK(SALES_DATE) AS WEEK,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\DAYOFWEEK(SALES_DATE) AS DAY_WEEK,\n\
|
|
|
|
\SALES_PERSON, SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\WHERE WEEK(SALES_DATE) = 13\n\
|
|
|
|
\GROUP BY ROLLUP ( WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE), SALES_PERSON )\n\
|
|
|
|
\ORDER BY WEEK, DAY_WEEK, SALES_PERSON"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT WEEK(SALES_DATE) AS WEEK,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\DAYOFWEEK(SALES_DATE) AS DAY_WEEK,\n\
|
|
|
|
\SALES_PERSON, SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\WHERE WEEK(SALES_DATE) = 13\n\
|
|
|
|
\GROUP BY CUBE ( WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE), SALES_PERSON )\n\
|
|
|
|
\ORDER BY WEEK, DAY_WEEK, SALES_PERSON"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT SALES_PERSON,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\MONTH(SALES_DATE) AS MONTH,\n\
|
|
|
|
\SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\GROUP BY GROUPING SETS ( (SALES_PERSON, MONTH(SALES_DATE)),\n\
|
|
|
|
\()\n\
|
|
|
|
\)\n\
|
|
|
|
\ORDER BY SALES_PERSON, MONTH"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT WEEK(SALES_DATE) AS WEEK,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\DAYOFWEEK(SALES_DATE) AS DAY_WEEK,\n\
|
|
|
|
\SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\GROUP BY ROLLUP ( WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE) )\n\
|
|
|
|
\ORDER BY WEEK, DAY_WEEK"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT MONTH(SALES_DATE) AS MONTH,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\REGION,\n\
|
|
|
|
\SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\GROUP BY ROLLUP ( MONTH(SALES_DATE), REGION )\n\
|
|
|
|
\ORDER BY MONTH, REGION"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT WEEK(SALES_DATE) AS WEEK,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\DAYOFWEEK(SALES_DATE) AS DAY_WEEK,\n\
|
|
|
|
\MONTH(SALES_DATE) AS MONTH,\n\
|
|
|
|
\REGION,\n\
|
|
|
|
\SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\GROUP BY GROUPING SETS ( ROLLUP( WEEK(SALES_DATE), DAYOFWEEK(SALES_DATE) ),\n\
|
|
|
|
\ROLLUP( MONTH(SALES_DATE), REGION ) )\n\
|
|
|
|
\ORDER BY WEEK, DAY_WEEK, MONTH, REGION"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT R1, R2,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\WEEK(SALES_DATE) AS WEEK,\n\
|
|
|
|
\DAYOFWEEK(SALES_DATE) AS DAY_WEEK,\n\
|
|
|
|
\MONTH(SALES_DATE) AS MONTH,\n\
|
|
|
|
\REGION, SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES,(VALUES('GROUP 1','GROUP 2')) AS X(R1,R2)\n\
|
|
|
|
\GROUP BY GROUPING SETS ((R1, ROLLUP(WEEK(SALES_DATE),\n\
|
|
|
|
\DAYOFWEEK(SALES_DATE))),\n\
|
|
|
|
\(R2,ROLLUP( MONTH(SALES_DATE), REGION ) ))\n\
|
|
|
|
\ORDER BY WEEK, DAY_WEEK, MONTH, REGION"
|
|
|
|
|
2024-02-04 17:00:59 +01:00
|
|
|
{-,p "SELECT COALESCE(R1,R2) AS GROUP,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\WEEK(SALES_DATE) AS WEEK,\n\
|
|
|
|
\DAYOFWEEK(SALES_DATE) AS DAY_WEEK,\n\
|
|
|
|
\MONTH(SALES_DATE) AS MONTH,\n\
|
|
|
|
\REGION, SUM(SALES) AS UNITS_SOLD\n\
|
|
|
|
\FROM SALES,(VALUES('GROUP 1','GROUP 2')) AS X(R1,R2)\n\
|
|
|
|
\GROUP BY GROUPING SETS ((R1, ROLLUP(WEEK(SALES_DATE),\n\
|
|
|
|
\DAYOFWEEK(SALES_DATE))),\n\
|
|
|
|
\(R2,ROLLUP( MONTH(SALES_DATE), REGION ) ))\n\
|
|
|
|
\ORDER BY GROUP, WEEK, DAY_WEEK, MONTH, REGION"-}
|
|
|
|
-- as group - needs more subtle keyword blacklisting
|
|
|
|
|
|
|
|
-- decimal as a function not allowed due to the reserved keyword
|
|
|
|
-- handling: todo, review if this is ansi standard function or
|
|
|
|
-- if there are places where reserved keywords can still be used
|
2024-02-04 17:00:59 +01:00
|
|
|
,p "SELECT MONTH(SALES_DATE) AS MONTH,\n\
|
2024-01-09 01:07:47 +01:00
|
|
|
\REGION,\n\
|
|
|
|
\SUM(SALES) AS UNITS_SOLD,\n\
|
|
|
|
\MAX(SALES) AS BEST_SALE,\n\
|
|
|
|
\CAST(ROUND(AVG(DECIMALx(SALES)),2) AS DECIMAL(5,2)) AS AVG_UNITS_SOLD\n\
|
|
|
|
\FROM SALES\n\
|
|
|
|
\GROUP BY CUBE(MONTH(SALES_DATE),REGION)\n\
|
|
|
|
\ORDER BY MONTH, REGION"
|
|
|
|
|
|
|
|
]
|