summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/selectable.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy/sql/selectable.py')
-rw-r--r--lib/sqlalchemy/sql/selectable.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py
index ac955a60f..e62aa1e8e 100644
--- a/lib/sqlalchemy/sql/selectable.py
+++ b/lib/sqlalchemy/sql/selectable.py
@@ -183,6 +183,51 @@ def lateral(selectable, name=None):
return 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'
@@ -474,6 +519,21 @@ class FromClause(Selectable):
"""
return Lateral(self, name)
+ def tablesample(self, sampling, name=None, seed=None):
+ """Return a TABLESAMPLE alias of this :class:`.FromClause`.
+
+ The return value is the :class:`.TableSample` construct also
+ provided by the top-level :func:`~.expression.tablesample` function.
+
+ .. versionadded:: 1.1
+
+ .. seealso::
+
+ :func:`~.expression.tablesample` - usage guidelines and parameters
+
+ """
+ return TableSample(self, sampling, name, seed)
+
def is_derived_from(self, fromclause):
"""Return True if this FromClause is 'derived' from the given
FromClause.
@@ -1268,6 +1328,38 @@ class Lateral(Alias):
__visit_name__ = 'lateral'
+class TableSample(Alias):
+ """Represent a TABLESAMPLE clause.
+
+ This object is constructed from the :func:`~.expression.tablesample` module
+ level function as well as the :meth:`.FromClause.tablesample` method available
+ on all :class:`.FromClause` subclasses.
+
+ .. versionadded:: 1.1
+
+ .. seealso::
+
+ :func:`~.expression.tablesample`
+
+ """
+
+ __visit_name__ = 'tablesample'
+
+ def __init__(self, selectable, sampling,
+ name=None,
+ seed=None):
+ self.sampling = sampling
+ self.seed = seed
+ super(TableSample, self).__init__(selectable, name=name)
+
+ @util.dependencies("sqlalchemy.sql.functions")
+ def _get_method(self, functions):
+ if isinstance(self.sampling, functions.Function):
+ return self.sampling
+ else:
+ return functions.func.system(self.sampling)
+
+
class CTE(Generative, HasSuffixes, Alias):
"""Represent a Common Table Expression.