From: mike bayer Date: Wed, 15 Jun 2016 19:17:10 +0000 (-0400) Subject: Merge "Add TABLESAMPLE clause support." X-Git-Tag: rel_1_1_0b1~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c80400ec7c5631d251bac43342637637b9c8214;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Merge "Add TABLESAMPLE clause support." --- 5c80400ec7c5631d251bac43342637637b9c8214 diff --cc lib/sqlalchemy/sql/selectable.py index 741aa9bead,e62aa1e8e9..9770b11bbf --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@@ -180,9 -180,54 +180,54 @@@ def lateral(selectable, name=None) :ref:`lateral_selects` - overview of usage. """ - return selectable.lateral(name=name) + return _interpret_as_from(selectable).lateral(name=name) + def tablesample(selectable, sampling, name=None, seed=None): + """Return a :class:`.TableSample` object. + + :class:`.TableSample` is an :class:`.Alias` subclass that represents + a table with the TABLESAMPLE clause applied to it. + :func:`~.expression.tablesample` + is also available from the :class:`.FromClause` class via the + :meth:`.FromClause.tablesample` method. + + The TABLESAMPLE clause allows selecting a randomly selected approximate + percentage of rows from a table. It supports multiple sampling methods, + most commonly BERNOULLI and SYSTEM. + + e.g.:: + + from sqlalchemy import func + + selectable = people.tablesample( + func.bernoulli(1), + name='alias', + seed=func.random()) + stmt = select([selectable.c.people_id]) + + Assuming ``people`` with a column ``people_id``, the above + statement would render as:: + + SELECT alias.people_id FROM + people AS alias TABLESAMPLE bernoulli(:bernoulli_1) + REPEATABLE (random()) + + .. versionadded:: 1.1 + + :param sampling: a ``float`` percentage between 0 and 100 or + :class:`.functions.Function`. + + :param name: optional alias name + + :param seed: any real-valued SQL expression. When specified, the + REPEATABLE sub-clause is also rendered. + + """ + return _interpret_as_from(selectable).tablesample( + sampling, name=name, seed=seed) + + class Selectable(ClauseElement): """mark a class as being selectable""" __visit_name__ = 'selectable'