From 462ccd9ff18d2e428b20ec3f596391a275472140 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 15 Aug 2018 17:11:14 -0400 Subject: Add concept of "implicit boolean", treat as native Fixed issue that is closely related to :ticket:`3639` where an expression rendered in a boolean context on a non-native boolean backend would be compared to 1/0 even though it is already an implcitly boolean expression, when :meth:`.ColumnElement.self_group` were used. While this does not affect the user-friendly backends (MySQL, SQLite) it was not handled by Oracle (and possibly SQL Server). Whether or not the expression is implicitly boolean on any database is now determined up front as an additional check to not generate the integer comparison within the compliation of the statement. Fixes: #4320 Change-Id: Iae0a65e5c01bd576e64733c3651e1e1a1a1b240c --- test/sql/test_operators.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'test/sql') diff --git a/test/sql/test_operators.py b/test/sql/test_operators.py index f4b3cf584..00961a2e8 100644 --- a/test/sql/test_operators.py +++ b/test/sql/test_operators.py @@ -2105,9 +2105,41 @@ class NegationTest(fixtures.TestBase, testing.AssertsCompiledSQL): self.assert_compile( expr, - "NOT (mytable.myid = :myid_1 OR mytable.myid = :myid_2)" + "NOT (mytable.myid = :myid_1 OR mytable.myid = :myid_2)", + dialect=default.DefaultDialect(supports_native_boolean=False) ) + def test_negate_operator_self_group(self): + orig_expr = or_( + self.table1.c.myid == 1, self.table1.c.myid == 2).self_group() + expr = not_(orig_expr) + is_not_(expr, orig_expr) + + self.assert_compile( + expr, + "NOT (mytable.myid = :myid_1 OR mytable.myid = :myid_2)", + dialect=default.DefaultDialect(supports_native_boolean=False) + ) + + def test_implicitly_boolean(self): + # test for expressions that the database always considers as boolean + # even if there is no boolean datatype. + assert not self.table1.c.myid._is_implicitly_boolean + assert (self.table1.c.myid == 5)._is_implicitly_boolean + assert (self.table1.c.myid == 5).self_group()._is_implicitly_boolean + assert (self.table1.c.myid == 5).label('x')._is_implicitly_boolean + assert not_(self.table1.c.myid == 5)._is_implicitly_boolean + assert or_( + self.table1.c.myid == 5, self.table1.c.myid == 7 + )._is_implicitly_boolean + assert not column('x', Boolean)._is_implicitly_boolean + assert not (self.table1.c.myid + 5)._is_implicitly_boolean + assert not not_(column('x', Boolean))._is_implicitly_boolean + assert not select([self.table1.c.myid]).\ + as_scalar()._is_implicitly_boolean + assert not text("x = y")._is_implicitly_boolean + assert not literal_column("x = y")._is_implicitly_boolean + class LikeTest(fixtures.TestBase, testing.AssertsCompiledSQL): __dialect__ = 'default' -- cgit v1.2.1