From 256e044aa7727843c8f239d162f1193c999111bb Mon Sep 17 00:00:00 2001 From: Jethro Nederhof Date: Thu, 3 Feb 2022 20:24:12 +1100 Subject: [PATCH 1/4] fix: POSTCOMPILE expansion in SQLAlchemy 1.4.27+ Handle the new double underscore prefix for POSTCOMPILE variables introduced in this version. --- sqlalchemy_bigquery/base.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sqlalchemy_bigquery/base.py b/sqlalchemy_bigquery/base.py index 136758ab..1676825f 100644 --- a/sqlalchemy_bigquery/base.py +++ b/sqlalchemy_bigquery/base.py @@ -341,16 +341,19 @@ def group_by_clause(self, select, **kw): __sqlalchemy_version_info = tuple(map(int, sqlalchemy.__version__.split("."))) - __expandng_text = ( + __expanding_text = ( "EXPANDING" if __sqlalchemy_version_info < (1, 4) else "POSTCOMPILE" ) + # https://github.com/sqlalchemy/sqlalchemy/commit/f79df12bd6d99b8f6f09d4bf07722638c4b4c159 + __expanding_conflict = "" if __sqlalchemy_version_info < (1, 4, 27) else "__" + __in_expanding_bind = _helpers.substitute_string_re_method( fr""" \sIN\s\( # ' IN (' ( - \[ # Expanding placeholder - {__expandng_text} # e.g. [EXPANDING_foo_1] + {__expanding_conflict}\[ # Expanding placeholder + {__expanding_text} # e.g. [EXPANDING_foo_1] _[^\]]+ # \] (:[A-Z0-9]+)? # type marker (e.g. ':INT64' @@ -431,7 +434,9 @@ def visit_notendswith_op_binary(self, binary, operator, **kw): __placeholder = re.compile(r"%\(([^\]:]+)(:[^\]:]+)?\)s$").match - __expanded_param = re.compile(fr"\(\[" fr"{__expandng_text}" fr"_[^\]]+\]\)$").match + __expanded_param = re.compile( + fr"\({__expanding_conflict}\[" fr"{__expanding_text}" fr"_[^\]]+\]\)$" + ).match __remove_type_parameter = _helpers.substitute_string_re_method( r""" From ed93927a96475917a378bc390fac53e272170b92 Mon Sep 17 00:00:00 2001 From: Jethro Nederhof Date: Fri, 18 Feb 2022 09:28:43 +1100 Subject: [PATCH 2/4] build: Widen sqlalchemy version support to include 1.4.27 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 4da104e7..0189da96 100644 --- a/setup.py +++ b/setup.py @@ -90,7 +90,7 @@ def readme(): # https://github.com/googleapis/python-bigquery-sqlalchemy/issues/386 # and # https://github.com/googleapis/python-bigquery-sqlalchemy/issues/385 - "sqlalchemy>=1.2.0,<=1.4.26", + "sqlalchemy>=1.2.0,<=1.4.27", "future", ], extras_require=extras, From 9133c2092fe6bfe9d13e4daeadf575c0efac7f34 Mon Sep 17 00:00:00 2001 From: Jethro Nederhof Date: Fri, 18 Feb 2022 16:30:11 +1100 Subject: [PATCH 3/4] fix: IN expansion compliance test --- sqlalchemy_bigquery/base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sqlalchemy_bigquery/base.py b/sqlalchemy_bigquery/base.py index 1676825f..cbf185d9 100644 --- a/sqlalchemy_bigquery/base.py +++ b/sqlalchemy_bigquery/base.py @@ -528,7 +528,8 @@ def visit_bindparam( if bindparam.expanding: assert_(self.__expanded_param(param), f"Unexpected param: {param}") - param = param.replace(")", f":{bq_type})") + if self.__sqlalchemy_version_info < (1, 4, 27): + param = param.replace(")", f":{bq_type})") else: m = self.__placeholder(param) From 5724486763726ca9f4db2768abe97fadeae05fe6 Mon Sep 17 00:00:00 2001 From: Jethro Nederhof Date: Sat, 19 Feb 2022 10:24:19 +1100 Subject: [PATCH 4/4] fix(coverage): skip expansion coverage checks --- sqlalchemy_bigquery/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlalchemy_bigquery/base.py b/sqlalchemy_bigquery/base.py index cbf185d9..ca1772a0 100644 --- a/sqlalchemy_bigquery/base.py +++ b/sqlalchemy_bigquery/base.py @@ -526,7 +526,7 @@ def visit_bindparam( assert_(param != "%s", f"Unexpected param: {param}") - if bindparam.expanding: + if bindparam.expanding: # pragma: NO COVER assert_(self.__expanded_param(param), f"Unexpected param: {param}") if self.__sqlalchemy_version_info < (1, 4, 27): param = param.replace(")", f":{bq_type})")