]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- add logic to compiler such that if stack is empty, we just
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 2 Sep 2014 14:49:46 +0000 (10:49 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 2 Sep 2014 14:49:46 +0000 (10:49 -0400)
stringify a _label_reference() as is.
- add .key to _label_reference(), so that when _make_proxy()
is called, we don't call str() on it anyway.
- add a test to exercise Query's behavior of adding all the order_by
expressions to the columns list of the select, assert that things
work out when we have a _label_reference there, that it gets sucked
into the columns list and spit out on the other side, it's referred
to appropriately, etc.   _label_reference() could theoretically
be resolved at the point we iterate _raw_columns() but
it's better to just let things work as they already do (except
nicer, since we get "tablename.colname" instead of just "somename"
 in the columns list) so that we aren't adding a ton of overhead
to _columns_plus_names in the common case.

lib/sqlalchemy/sql/compiler.py
lib/sqlalchemy/sql/elements.py
lib/sqlalchemy/sql/selectable.py
test/orm/test_query.py
test/sql/test_text.py

index e4597dcd83d391948799a96be218ed0964dc7753..23e5456a7dbeea2c5189d5e697977c5773cc6d8f 100644 (file)
@@ -495,6 +495,12 @@ class SQLCompiler(Compiled):
         return "(" + grouping.element._compiler_dispatch(self, **kwargs) + ")"
 
     def visit_label_reference(self, element, **kwargs):
+        if not self.stack:
+            # compiling the element outside of the context of a SELECT
+            return self.process(
+                element._text_clause
+            )
+
         selectable = self.stack[-1]['selectable']
         try:
             col = selectable._inner_column_dict[element.text]
@@ -504,7 +510,7 @@ class SQLCompiler(Compiled):
                 "Can't resolve label reference %r; converting to text()",
                 util.ellipses_string(element.text))
             return self.process(
-                elements.TextClause._create_text(element.text)
+                element._text_clause
             )
         else:
             kwargs['render_label_as_label'] = col
index 0ea05fa0ec9ae2f98c1bafa7dad8da546d602724..984cfe0eea5de11bd5aa2f6ba4248f220cbc5a2d 100644 (file)
@@ -2292,7 +2292,11 @@ class _label_reference(ColumnElement):
     __visit_name__ = 'label_reference'
 
     def __init__(self, text):
-        self.text = text
+        self.text = self.key = text
+
+    @util.memoized_property
+    def _text_clause(self):
+        return TextClause._create_text(self.text)
 
 
 class UnaryExpression(ColumnElement):
index cf2c213d28d5f5c5e547b4a3ffc8a74e156f5f91..a494939959820e18e320be854f79324f8a83543a 100644 (file)
@@ -2976,6 +2976,7 @@ class Select(HasPrefixes, GenerativeSelect):
             def name_for_col(c):
                 if c._columns_clause_label is None:
                     return (None, c)
+
                 name = c._columns_clause_label
                 if name in names:
                     name = c.anon_label
index c0e9f9e1c4320d5d982c8250804b01aa8ed3b92d..cb67057e4343c812e6c5032366d6bf1a936dda10 100644 (file)
@@ -2276,7 +2276,7 @@ class HintsTest(QueryTest, AssertsCompiledSQL):
         )
 
 
-class TextTest(QueryTest):
+class TextTest(QueryTest, AssertsCompiledSQL):
     def test_fulltext(self):
         User = self.classes.User
 
@@ -2380,6 +2380,44 @@ class TextTest(QueryTest):
             [User(id=7), User(id=8), User(id=9), User(id=10)]
         )
 
+    def test_order_by_w_eager(self):
+        User = self.classes.User
+        Address = self.classes.Address
+        s = create_session()
+
+        # here, we are seeing how Query has to take the order by expressions
+        # of the query and then add them to the columns list, so that the
+        # outer subquery can order by that same label.  With the anonymous
+        # label, our column gets sucked up and restated again in the
+        # inner columns list!
+        # we could try to play games with making this "smarter" but it
+        # would add permanent overhead to Select._columns_plus_names,
+        # since that's where references would need to be resolved.
+        # so as it is, this query takes the _label_reference and makes a
+        # full blown proxy and all the rest of it.
+        self.assert_compile(
+            s.query(User).options(joinedload("addresses")).
+            order_by(desc("name")).limit(1),
+            "SELECT anon_1.users_id AS anon_1_users_id, "
+            "anon_1.users_name AS anon_1_users_name, "
+            "anon_1.anon_2 AS anon_1_anon_2, "
+            "addresses_1.id AS addresses_1_id, "
+            "addresses_1.user_id AS addresses_1_user_id, "
+            "addresses_1.email_address AS addresses_1_email_address "
+            "FROM (SELECT users.id AS users_id, users.name AS users_name, "
+            "users.name AS anon_2 FROM users ORDER BY users.name "
+            "DESC LIMIT ? OFFSET ?) AS anon_1 "
+            "LEFT OUTER JOIN addresses AS addresses_1 "
+            "ON anon_1.users_id = addresses_1.user_id "
+            "ORDER BY anon_1.anon_2 DESC, addresses_1.id"
+        )
+
+        eq_(
+            s.query(User).options(joinedload("addresses")).
+                order_by(desc("name")).first(),
+            User(name='jack', addresses=[Address()])
+        )
+
 
 class TextWarningTest(QueryTest, AssertsCompiledSQL):
     def _test(self, fn, arg, offending_clause, expected):
index 182c63624ab15cb12700935dbc0ca453f63e0972..e84a2907c7ff5591bae97fd35dd739b1f1b84658 100644 (file)
@@ -674,3 +674,8 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL):
             "SELECT foo(:foo_1) AS x UNION SELECT foo(:foo_2) AS y ORDER BY x"
         )
 
+    def test_standalone_units_stringable(self):
+        self.assert_compile(
+            desc("somelabel"),
+            "somelabel DESC"
+        )