Skip to content

Commit 446b321

Browse files
committed
fix optimization for bulk updates
- don't optimize if parameters are not supplied yet - check the key type before binding parameters
1 parent d07ea57 commit 446b321

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Looking for more help?
7676
.. _contribution docs: CONTRIBUTING.rst
7777
.. _Crate.io: http://crate.io/
7878
.. _CrateDB: https://github.com/crate/crate
79-
.. _DB API 2.0: http://www.python.org/dev/peps/pep-0249/>
79+
.. _DB API 2.0: http://www.python.org/dev/peps/pep-0249/
8080
.. _developer docs: DEVELOP.rst
8181
.. _paid support: https://crate.io/pricing/
8282
.. _pip: https://pypi.python.org/pypi/pip

src/crate/client/sqlalchemy/compiler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ def rewrite_update(clauseelement, multiparams, params):
4444
The update statement is only rewritten if an item of the MutableDict was
4545
changed.
4646
"""
47-
4847
newmultiparams = []
4948
_multiparams = multiparams[0]
49+
if len(_multiparams) == 0:
50+
return clauseelement, multiparams, params
5051
for _params in _multiparams:
5152
newparams = {}
5253
for key, val in _params.items():
@@ -316,7 +317,7 @@ def visit_update(self, update_stmt, **kw):
316317
set_clauses.append(clause)
317318

318319
for k, v in update_stmt.parameters.items():
319-
if '[' in k:
320+
if type(k) is str and '[' in k:
320321
bindparam = sa.sql.bindparam(k, v)
321322
set_clauses.append(k + ' = ' + self.process(bindparam))
322323

src/crate/client/sqlalchemy/tests/compiler_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from __future__ import absolute_import
2323
from unittest import TestCase
24+
2425
from crate.client.sqlalchemy.compiler import crate_before_execute
2526

2627
import sqlalchemy as sa
@@ -55,3 +56,17 @@ def test_crate_update_rewritten(self):
5556
)
5657

5758
assert hasattr(clauseelement, '_crate_specific') is True
59+
60+
61+
def test_bulk_update_on_builtin_type(self):
62+
"""
63+
The "before_execute" hook in the compiler doesn't get
64+
access to the parameters in case of a bulk update. It
65+
should not try to optimize any parameters.
66+
"""
67+
data = ({},)
68+
clauseelement, multiparams, params = crate_before_execute(
69+
self.crate_engine, self.update, data, None
70+
)
71+
72+
assert hasattr(clauseelement, '_crate_specific') is False

0 commit comments

Comments
 (0)