:ticket:`4020`
+
+.. change_3249:
+
+Support for GROUPING SETS, CUBE, ROLLUP
+---------------------------------------
+
+All three of GROUPING SETS, CUBE, ROLLUP are available via the
+:attr:`.func` namespace. In the case of CUBE and ROLLUP, these functions
+already work in previous versions, however for GROUPING SETS, a placeholder
+is added to the compiler to allow for the space. All three functions
+are named in the documentation now::
+
+ >>> from sqlalchemy import select, table, column, func, tuple_
+ >>> t = table('t',
+ ... column('value'), column('x'),
+ ... column('y'), column('z'), column('q'))
+ >>> stmt = select([func.sum(t.c.value)]).group_by(
+ ... func.grouping_sets(
+ ... tuple_(t.c.x, t.c.y),
+ ... tuple_(t.c.z, t.c.q),
+ ... )
+ ... )
+ >>> print(stmt)
+ SELECT sum(t.value) AS sum_1
+ FROM t GROUP BY GROUPING SETS((t.x, t.y), (t.z, t.q))
+
+:ticket:`3429`
+
Key Behavioral Changes - ORM
============================
functions.random: 'random%(expr)s',
functions.sysdate: 'sysdate',
functions.session_user: 'SESSION_USER',
- functions.user: 'USER'
+ functions.user: 'USER',
+ functions.cube: 'CUBE%(expr)s',
+ functions.rollup: 'ROLLUP%(expr)s',
+ functions.grouping_sets: 'GROUPING SETS%(expr)s',
}
EXTRACT_MAP = {
"""Represent a WITHIN GROUP (ORDER BY) clause.
This is a special operator against so-called
- so-called "ordered set aggregate" and "hypothetical
+ "ordered set aggregate" and "hypothetical
set aggregate" functions, including ``percentile_cont()``,
``rank()``, ``dense_rank()``, etc.
"""
type = sqltypes.Numeric()
+
+
+class cube(GenericFunction):
+ r"""Implement the ``CUBE`` grouping operation.
+
+ This function is used as part of the GROUP BY of a statement,
+ e.g. :meth:`.Select.group_by`::
+
+ stmt = select(
+ [func.sum(table.c.value), table.c.col_1, table.c.col_2]
+ ).group_by(func.cube(table.c.col_1, table.c.col_2))
+
+ .. versionadded:: 1.2
+
+ """
+
+
+class rollup(GenericFunction):
+ r"""Implement the ``ROLLUP`` grouping operation.
+
+ This function is used as part of the GROUP BY of a statement,
+ e.g. :meth:`.Select.group_by`::
+
+ stmt = select(
+ [func.sum(table.c.value), table.c.col_1, table.c.col_2]
+ ).group_by(func.rollup(table.c.col_1, table.c.col_2))
+
+ .. versionadded:: 1.2
+
+ """
+
+
+class grouping_sets(GenericFunction):
+ r"""Implement the ``GROUPING SETS`` grouping operation.
+
+ This function is used as part of the GROUP BY of a statement,
+ e.g. :meth:`.Select.group_by`::
+
+ stmt = select(
+ [func.sum(table.c.value), table.c.col_1, table.c.col_2]
+ ).group_by(func.grouping_sets(table.c.col_1, table.c.col_2))
+
+ In order to group by multiple sets, use the :func:`.tuple_` construct::
+
+ from sqlalchemy import tuple_
+
+ stmt = select(
+ [
+ func.sum(table.c.value),
+ table.c.col_1, table.c.col_2,
+ table.c.col_3]
+ ).group_by(
+ func.grouping_sets(
+ tuple_(table.c.col_1, table.c.col_2),
+ tuple_(table.c.value, table.c.col_3),
+ )
+ )
+
+
+ .. versionadded:: 1.2
+
+ """
]:
self.assert_compile(func.random(), ret, dialect=dialect)
+ def test_cube_operators(self):
+
+ t = table('t', column('value'),
+ column('x'), column('y'), column('z'), column('q'))
+
+ stmt = select([func.sum(t.c.value)])
+
+ self.assert_compile(
+ stmt.group_by(func.cube(t.c.x, t.c.y)),
+ "SELECT sum(t.value) AS sum_1 FROM t GROUP BY CUBE(t.x, t.y)"
+ )
+
+ self.assert_compile(
+ stmt.group_by(func.rollup(t.c.x, t.c.y)),
+ "SELECT sum(t.value) AS sum_1 FROM t GROUP BY ROLLUP(t.x, t.y)"
+ )
+
+ self.assert_compile(
+ stmt.group_by(
+ func.grouping_sets(t.c.x, t.c.y)
+ ),
+ "SELECT sum(t.value) AS sum_1 FROM t "
+ "GROUP BY GROUPING SETS(t.x, t.y)"
+ )
+
+ self.assert_compile(
+ stmt.group_by(
+ func.grouping_sets(
+ sql.tuple_(t.c.x, t.c.y),
+ sql.tuple_(t.c.z, t.c.q),
+ )
+ ),
+ "SELECT sum(t.value) AS sum_1 FROM t GROUP BY "
+ "GROUPING SETS((t.x, t.y), (t.z, t.q))"
+ )
+
def test_generic_annotation(self):
fn = func.coalesce('x', 'y')._annotate({"foo": "bar"})
self.assert_compile(