From 2aae37f93b413176d683259e0bc712728af931db Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 11 Jul 2021 19:40:59 -0400 Subject: [PATCH] represent tablesample.sampling as FunctionElement in all cases Fixed regression where the :func:`_sql.tablesample` construct would fail to be executable when constructed given a floating-point sampling value not embedded within a SQL function. Fixes: #6735 Change-Id: I557bcd4bdbffc4329ad69d5659ba99b1c8deb554 --- doc/build/changelog/unreleased_14/6735.rst | 7 +++++++ lib/sqlalchemy/sql/selectable.py | 12 ++++++------ test/sql/test_compare.py | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 doc/build/changelog/unreleased_14/6735.rst diff --git a/doc/build/changelog/unreleased_14/6735.rst b/doc/build/changelog/unreleased_14/6735.rst new file mode 100644 index 0000000000..9e8a3f29c4 --- /dev/null +++ b/doc/build/changelog/unreleased_14/6735.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, sql, regression + :tickets: 6735 + + Fixed regression where the :func:`_sql.tablesample` construct would fail to + be executable when constructed given a floating-point sampling value not + embedded within a SQL function. diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 235c74ea76..42cb6e5aef 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -1970,18 +1970,18 @@ class TableSample(AliasedReturnsRows): sampling, name=name, seed=seed ) + @util.preload_module("sqlalchemy.sql.functions") def _init(self, selectable, sampling, name=None, seed=None): + functions = util.preloaded.sql_functions + if not isinstance(sampling, functions.Function): + sampling = functions.func.system(sampling) + self.sampling = sampling self.seed = seed super(TableSample, self)._init(selectable, name=name) - @util.preload_module("sqlalchemy.sql.functions") def _get_method(self): - functions = util.preloaded.sql_functions - if isinstance(self.sampling, functions.Function): - return self.sampling - else: - return functions.func.system(self.sampling) + return self.sampling class CTE( diff --git a/test/sql/test_compare.py b/test/sql/test_compare.py index e96a47553b..365ed52b2a 100644 --- a/test/sql/test_compare.py +++ b/test/sql/test_compare.py @@ -504,6 +504,7 @@ class CoreFixtures(object): .union_all(select(table_a.c.b)), select(table_a.c.a).lateral(), select(table_a.c.a).lateral(name="bar"), + table_a.tablesample(0.75), table_a.tablesample(func.bernoulli(1)), table_a.tablesample(func.bernoulli(1), seed=func.random()), table_a.tablesample(func.bernoulli(1), seed=func.other_random()), -- 2.47.2