Added support for the SQL-standard function :class:`.array_agg`,
which automatically returns an :class:`.Array` of the correct type
- and supports index / slice operations. As arrays are only
+ and supports index / slice operations, as well as
+ :func:`.postgresql.array_agg`, which returns a :class:`.postgresql.ARRAY`
+ with additional comparison features. As arrays are only
supported on Postgresql at the moment, only actually works on
Postgresql. Also added a new construct
:class:`.postgresql.aggregate_order_by` in support of PG's
from sqlalchemy import func
stmt = select([func.array_agg(table.c.value)])
-A Postgresql element for an aggreagte ORDER BY is also added via
+A Postgresql element for an aggregate ORDER BY is also added via
:class:`.postgresql.aggregate_order_by`::
from sqlalchemy.dialects.postgresql import aggregate_order_by
SELECT array_agg(table1.a ORDER BY table1.b DESC) AS array_agg_1 FROM table1
+The PG dialect itself also provides an :func:`.postgresql.array_agg` wrapper to
+ensure the :class:`.postgresql.ARRAY` type::
+
+ from sqlalchemy.dialects.postgresql import array_agg
+ stmt = select([array_agg(table.c.value).contains('foo')])
+
+
Additionally, functions like ``percentile_cont()``, ``percentile_disc()``,
``rank()``, ``dense_rank()`` and others that require an ordering via
``WITHIN GROUP (ORDER BY <expr>)`` are now available via the
.. autoclass:: ARRAY
:members: __init__, Comparator
+.. autofunction:: array_agg
.. autofunction:: Any
from .hstore import HSTORE, hstore
from .json import JSON, JSONB
from .array import array, ARRAY, Any, All
-from .ext import aggregate_order_by, ExcludeConstraint
+from .ext import aggregate_order_by, ExcludeConstraint, array_agg
from .ranges import INT4RANGE, INT8RANGE, NUMRANGE, DATERANGE, TSRANGE, \
TSTZRANGE
'INTERVAL', 'ARRAY', 'ENUM', 'dialect', 'array', 'HSTORE',
'hstore', 'INT4RANGE', 'INT8RANGE', 'NUMRANGE', 'DATERANGE',
'TSRANGE', 'TSTZRANGE', 'json', 'JSON', 'JSONB', 'Any', 'All',
- 'DropEnumType', 'CreateEnumType', 'ExcludeConstraint', 'aggregate_order_by'
+ 'DropEnumType', 'CreateEnumType', 'ExcludeConstraint',
+ 'aggregate_order_by', 'array_agg'
)
.. seealso::
- :func:`.expression.any`
+ :func:`.expression.any_`
"""
.. seealso::
- :func:`.expression.all`
+ :func:`.expression.all_`
"""
* :class:`.postgresql.array` - array literal
+* :func:`.postgresql.array_agg` - ARRAY_AGG SQL function
+
+* :class:`.postgresql.aggregate_order_by` - helper for PG's ORDER BY aggregate
+ function syntax.
+
JSON Types
----------
from ...sql import expression
from ...sql import elements
+from ...sql import functions
from ...sql.schema import ColumnCollectionConstraint
+from .array import ARRAY
class aggregate_order_by(expression.ColumnElement):
initially=self.initially)
c.dispatch._update(self.dispatch)
return c
+
+
+def array_agg(*arg, **kw):
+ """Postgresql-specific form of :class:`.array_agg`, ensures
+ return type is :class:`.postgresql.ARRAY` and not
+ the plain :class:`.types.Array`.
+
+ .. versionadded:: 1.1
+
+ """
+ kw['type_'] = ARRAY(functions._type_from_args(arg))
+ return functions.func.array_agg(*arg, **kw)
The ``func.array_agg(expr)`` construct returns an expression of
type :class:`.Array`.
- e.g.
+ e.g.::
stmt = select([func.array_agg(table.c.values)[2:5]])
.. versionadded:: 1.1
+ .. seealso::
+
+ :func:`.postgresql.array_agg` - PostgreSQL-specific version that
+ returns :class:`.ARRAY`, which has PG-specific operators added.
+
"""
+ type = sqltypes.Array
+
def __init__(self, *args, **kwargs):
args = [_literal_as_binds(c) for c in args]
- kwargs.setdefault('type_', sqltypes.Array(_type_from_args(args)))
+ kwargs.setdefault('type_', self.type(_type_from_args(args)))
kwargs['_parsed_args'] = args
super(array_agg, self).__init__(*args, **kwargs)
"ARRAY[%(param_4)s, %(param_5)s, %(param_6)s]))[%(param_7)s]"
)
+ def test_array_agg_generic(self):
+ expr = func.array_agg(column('q', Integer))
+ is_(expr.type.__class__, types.Array)
+ is_(expr.type.item_type.__class__, Integer)
+
+ def test_array_agg_specific(self):
+ from sqlalchemy.dialects.postgresql import array_agg
+ expr = array_agg(column('q', Integer))
+ is_(expr.type.__class__, postgresql.ARRAY)
+ is_(expr.type.item_type.__class__, Integer)
+
class ArrayRoundTripTest(fixtures.TablesTest, AssertsExecutionResults):