From b97b313c6eb7f2fe4b98d011c292de4d258c508c Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 7 Mar 2023 09:03:07 -0500 Subject: resolve select to NULLTYPE if no columns Fixed regression where the :func:`_sql.select` construct would not be able to render if it were given no columns and then used in the context of an EXISTS, raising an internal exception instead. While an empty "SELECT" is not typically valid SQL, in the context of EXISTS databases such as PostgreSQL allow it, and in any case the condition now no longer raises an internal exception. For this case, also add an extra whitespace trim step for the unusual case that there are no columns to render. This is done in such a way as to not interfere with other test cases that are involving custom compilation schemes. Fixes: #9440 Change-Id: If65ba9ce15d371f09b4342ad0669143b7b082a78 --- test/sql/test_select.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'test/sql') diff --git a/test/sql/test_select.py b/test/sql/test_select.py index ad4b4db95..7979fd200 100644 --- a/test/sql/test_select.py +++ b/test/sql/test_select.py @@ -9,6 +9,7 @@ from sqlalchemy import select from sqlalchemy import String from sqlalchemy import Table from sqlalchemy import testing +from sqlalchemy import true from sqlalchemy import tuple_ from sqlalchemy import union from sqlalchemy.sql import column @@ -77,6 +78,29 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): "WHERE mytable.myid = myothertable.otherid", ) + @testing.combinations( + ( + lambda tbl: select().select_from(tbl).where(tbl.c.id == 123), + "SELECT FROM tbl WHERE tbl.id = :id_1", + ), + (lambda tbl: select().where(true()), "SELECT WHERE 1 = 1"), + ( + lambda tbl: select() + .select_from(tbl) + .where(tbl.c.id == 123) + .exists(), + "EXISTS (SELECT FROM tbl WHERE tbl.id = :id_1)", + ), + ) + def test_select_no_columns(self, stmt, expected): + """test #9440""" + + tbl = table("tbl", column("id")) + + stmt = testing.resolve_lambda(stmt, tbl=tbl) + + self.assert_compile(stmt, expected) + def test_new_calling_style_clauseelement_thing_that_has_iter(self): class Thing: def __clause_element__(self): -- cgit v1.2.1