summaryrefslogtreecommitdiff
path: root/test/sql/test_external_traversal.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-02-23 13:37:18 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2020-03-06 11:01:51 -0500
commit851fb8f5a661c66ee76308181118369c8c4df9e0 (patch)
treeb6c786e78e090752f5c0922d1f09d277ab94e365 /test/sql/test_external_traversal.py
parentd72bda5ed23a46bcbf31d40684200dcb79012a33 (diff)
downloadsqlalchemy-851fb8f5a661c66ee76308181118369c8c4df9e0.tar.gz
Decouple compiler state from DML objects; make cacheable
Targeting select / insert / update / delete, the goal is to minimize overhead of construction and generative methods so that only the raw arguments passed are handled. An interim stage that converts the raw state into more compiler-ready state is added, which is analogous to the ORM QueryContext which will also be rolled in to be a similar concept, as is currently being prototyped in I19e05b3424b07114cce6c439b05198ac47f7ac10. the ORM update/delete BulkUD concept is also going to be rolled onto this idea. So while the compiler-ready state object, here called DMLState, looks a little thin, it's the base of a bigger pattern that will allow for ORM functionality to embed itself directly into the compiler, execution context, and result set objects. This change targets the DML objects, primarily focused on the values() method which is the most complex process. The work done by values() is minimized as much as possible while still being able to create a cache key. Additional computation is then offloaded to a new object ValuesState that is handled by the compiler. Architecturally, a big change here is that insert.values() and update.values() will generate BindParameter objects for the values now, which are then carefully received by crud.py so that they generate the expected names. This is so that the values() portion of these constructs is cacheable. for the "multi-values" version of Insert, this is all skipped and the plan right now is that a multi-values insert is not worth caching (can always be revisited). Using the coercions system in values() also gets us nicer validation for free, we can remove the NotAClauseElement thing from schema, and we also now require scalar_subquery() is called for an insert/update that uses a SELECT as a column value, 1.x deprecation path is added. The traversal system is then applied to the DML objects including tests so that they have traversal, cloning, and cache key support. cloning is not a use case for DML however having it present allows better validation of the structure within the tests. Special per-dialect DML is explicitly not cacheable at the moment, more as a proof of concept that third party DML constructs can exist as gracefully not-cacheable rather than producing an incomplete cache key. A few selected performance improvements have been added as well, simplifying the immutabledict.union() method and adding a new SQLCompiler function that can generate delimeter-separated clauses like WHERE and ORDER BY without having to build a ClauseList object at all. The use of ClauseList will be removed from Select in an upcoming commit. Overall, ClaustList is unnecessary for internal use and only adds overhead to statement construction and will likely be removed as much as possible except for explcit use of conjunctions like and_() and or_(). Change-Id: I408e0b8be91fddd77cf279da97f55020871f75a9
Diffstat (limited to 'test/sql/test_external_traversal.py')
-rw-r--r--test/sql/test_external_traversal.py141
1 files changed, 100 insertions, 41 deletions
diff --git a/test/sql/test_external_traversal.py b/test/sql/test_external_traversal.py
index 84d99d886..2a82c2cc1 100644
--- a/test/sql/test_external_traversal.py
+++ b/test/sql/test_external_traversal.py
@@ -7,6 +7,7 @@ from sqlalchemy import extract
from sqlalchemy import ForeignKey
from sqlalchemy import func
from sqlalchemy import Integer
+from sqlalchemy import literal
from sqlalchemy import literal_column
from sqlalchemy import MetaData
from sqlalchemy import select
@@ -37,7 +38,6 @@ from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_
from sqlalchemy.testing import is_not_
-
A = B = t1 = t2 = t3 = table1 = table2 = table3 = table4 = None
@@ -1961,29 +1961,53 @@ class ValuesBaseTest(fixtures.TestBase, AssertsCompiledSQL):
def test_add_kwarg(self):
i = t1.insert()
- eq_(i.parameters, None)
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+ eq_(compile_state._dict_parameters, None)
i = i.values(col1=5)
- eq_(i.parameters, {"col1": 5})
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+ self._compare_param_dict(compile_state._dict_parameters, {"col1": 5})
i = i.values(col2=7)
- eq_(i.parameters, {"col1": 5, "col2": 7})
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+ self._compare_param_dict(
+ compile_state._dict_parameters, {"col1": 5, "col2": 7}
+ )
def test_via_tuple_single(self):
i = t1.insert()
- eq_(i.parameters, None)
+
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+ eq_(compile_state._dict_parameters, None)
+
i = i.values((5, 6, 7))
- eq_(i.parameters, {"col1": 5, "col2": 6, "col3": 7})
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+
+ self._compare_param_dict(
+ compile_state._dict_parameters, {"col1": 5, "col2": 6, "col3": 7},
+ )
def test_kw_and_dict_simultaneously_single(self):
i = t1.insert()
- i = i.values({"col1": 5}, col2=7)
- eq_(i.parameters, {"col1": 5, "col2": 7})
+ assert_raises_message(
+ exc.ArgumentError,
+ r"Can't pass positional and kwargs to values\(\) simultaneously",
+ i.values,
+ {"col1": 5},
+ col2=7,
+ )
def test_via_tuple_multi(self):
i = t1.insert()
- eq_(i.parameters, None)
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+ eq_(compile_state._dict_parameters, None)
+
i = i.values([(5, 6, 7), (8, 9, 10)])
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
eq_(
- i.parameters,
+ compile_state._dict_parameters, {"col1": 5, "col2": 6, "col3": 7},
+ )
+ eq_(compile_state._has_multi_parameters, True)
+ eq_(
+ compile_state._multi_parameters,
[
{"col1": 5, "col2": 6, "col3": 7},
{"col1": 8, "col2": 9, "col3": 10},
@@ -1992,58 +2016,92 @@ class ValuesBaseTest(fixtures.TestBase, AssertsCompiledSQL):
def test_inline_values_single(self):
i = t1.insert(values={"col1": 5})
- eq_(i.parameters, {"col1": 5})
- is_(i._has_multi_parameters, False)
+
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+
+ self._compare_param_dict(compile_state._dict_parameters, {"col1": 5})
+ is_(compile_state._has_multi_parameters, False)
def test_inline_values_multi(self):
i = t1.insert(values=[{"col1": 5}, {"col1": 6}])
- eq_(i.parameters, [{"col1": 5}, {"col1": 6}])
- is_(i._has_multi_parameters, True)
+
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+
+ # multiparams are not converted to bound parameters
+ eq_(compile_state._dict_parameters, {"col1": 5})
+
+ # multiparams are not converted to bound parameters
+ eq_(compile_state._multi_parameters, [{"col1": 5}, {"col1": 6}])
+ is_(compile_state._has_multi_parameters, True)
+
+ def _compare_param_dict(self, a, b):
+ if list(a) != list(b):
+ return False
+
+ from sqlalchemy.types import NullType
+
+ for a_k, a_i in a.items():
+ b_i = b[a_k]
+
+ # compare BindParameter on the left to
+ # literal value on the right
+ assert a_i.compare(literal(b_i, type_=NullType()))
def test_add_dictionary(self):
i = t1.insert()
- eq_(i.parameters, None)
+
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+
+ eq_(compile_state._dict_parameters, None)
i = i.values({"col1": 5})
- eq_(i.parameters, {"col1": 5})
- is_(i._has_multi_parameters, False)
+
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+
+ self._compare_param_dict(compile_state._dict_parameters, {"col1": 5})
+ is_(compile_state._has_multi_parameters, False)
i = i.values({"col1": 6})
# note replaces
- eq_(i.parameters, {"col1": 6})
- is_(i._has_multi_parameters, False)
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+
+ self._compare_param_dict(compile_state._dict_parameters, {"col1": 6})
+ is_(compile_state._has_multi_parameters, False)
i = i.values({"col2": 7})
- eq_(i.parameters, {"col1": 6, "col2": 7})
- is_(i._has_multi_parameters, False)
+ compile_state = i._compile_state_cls(i, None, isinsert=True)
+ self._compare_param_dict(
+ compile_state._dict_parameters, {"col1": 6, "col2": 7}
+ )
+ is_(compile_state._has_multi_parameters, False)
def test_add_kwarg_disallowed_multi(self):
i = t1.insert()
i = i.values([{"col1": 5}, {"col1": 7}])
+ i = i.values(col2=7)
assert_raises_message(
exc.InvalidRequestError,
- "This construct already has multiple parameter sets.",
- i.values,
- col2=7,
+ "Can't mix single and multiple VALUES formats",
+ i.compile,
)
def test_cant_mix_single_multi_formats_dict_to_list(self):
i = t1.insert().values(col1=5)
+ i = i.values([{"col1": 6}])
assert_raises_message(
- exc.ArgumentError,
- "Can't mix single-values and multiple values "
- "formats in one statement",
- i.values,
- [{"col1": 6}],
+ exc.InvalidRequestError,
+ "Can't mix single and multiple VALUES "
+ "formats in one INSERT statement",
+ i.compile,
)
def test_cant_mix_single_multi_formats_list_to_dict(self):
i = t1.insert().values([{"col1": 6}])
+ i = i.values({"col1": 5})
assert_raises_message(
- exc.ArgumentError,
- "Can't mix single-values and multiple values "
- "formats in one statement",
- i.values,
- {"col1": 5},
+ exc.InvalidRequestError,
+ "Can't mix single and multiple VALUES "
+ "formats in one INSERT statement",
+ i.compile,
)
def test_erroneous_multi_args_dicts(self):
@@ -2072,7 +2130,7 @@ class ValuesBaseTest(fixtures.TestBase, AssertsCompiledSQL):
i = t1.insert()
assert_raises_message(
exc.ArgumentError,
- "Can't pass kwargs and multiple parameter sets simultaneously",
+ r"Can't pass positional and kwargs to values\(\) simultaneously",
i.values,
[{"col1": 5}],
col2=7,
@@ -2080,17 +2138,18 @@ class ValuesBaseTest(fixtures.TestBase, AssertsCompiledSQL):
def test_update_no_support_multi_values(self):
u = t1.update()
+ u = u.values([{"col1": 5}, {"col1": 7}])
assert_raises_message(
exc.InvalidRequestError,
- "This construct does not support multiple parameter sets.",
- u.values,
- [{"col1": 5}, {"col1": 7}],
+ "UPDATE construct does not support multiple parameter sets.",
+ u.compile,
)
def test_update_no_support_multi_constructor(self):
+ stmt = t1.update(values=[{"col1": 5}, {"col1": 7}])
+
assert_raises_message(
exc.InvalidRequestError,
- "This construct does not support multiple parameter sets.",
- t1.update,
- values=[{"col1": 5}, {"col1": 7}],
+ "UPDATE construct does not support multiple parameter sets.",
+ stmt.compile,
)