ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.63k stars 245 forks source link

Tuple comparison with ">=" is broken #663

Open nielstron opened 2 years ago

nielstron commented 2 years ago

In the case if ">=", the translation of tuple comparison is wrong. The correct translation of (a, b) >= (c, d) is a __>__ c OR (a = c AND b > d). However, currently it is a __>=__ c OR (a = c AND b > d).

Consider the following sample program:

from pony import orm as pony

DB_NAME = "test.sqlite"
db = pony.Database()

class Test(db.Entity):
    a = pony.Required(int)
    b = pony.Required(int)

db.bind(provider="sqlite", filename=DB_NAME, create_db=True)
db.generate_mapping(create_tables=True)
pony.set_sql_debug(True)

with pony.db_session as ses:
    _ = pony.get(f for f in Test if (f.a, f.b) >= (1, 2))

with this output:

GET NEW CONNECTION
SWITCH TO AUTOCOMMIT MODE
SELECT "f"."id", "f"."a", "f"."b"
FROM "Test" "f"
WHERE ("f"."a" >= 1 OR "f"."a" = 1 AND "f"."b" >= 2)
LIMIT 2

RELEASE CONNECTION