You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug(python): Expr has no __bool__, so "and" / "or" / chained comparisons silently drop a filter predicate #4681
0.41.0b14 (also checked against main @ 180eaaf by reading the code: python/python/lancedb/expr.py and query.py are identical to the wheel)
What happened?
lancedb.expr.Expr overloads ==, <, >, ... to build expressions, and combines them with &, |, ~ (module and class docstrings). It does not define __bool__, so bool(expr) is
always True. Python's and, or, not and chained comparisons call bool() on an operand and
keep only one operand, so a predicate is silently dropped and the query returns wrong rows, with
no error and no warning:
where(lit(2) < col("id") < lit(5)) is evaluated by Python as (2 < id) and (id < 5), which
keeps only id < 5: it returns ids 0..4 (the lower bound is lost) instead of 3, 4.
where((col("id") > 7) and (col("g") == 0)) keeps only col("g") == 0: the id > 7 half
vanishes (returns every even id).
where((col("id") > 7) or (col("g") == 0)) keeps only col("id") > 7.
bool(col("id") == 1) is True.
Expected: an error, like pandas, polars, pyarrow and SQLAlchemy raise for their lazy expressions
when bool() is called on them, telling the user to use &, |, ~ (a wrong result that looks
right is worse than an exception). Expr already sets __hash__ = None for the same "operator
returns an Expr" reason.
I have a fix (an Expr.__bool__ that raises TypeError with a hint to use &, |, ~, plus
the one place in the hybrid query builder that tested the truthiness of the filter) and will open
a PR.
Activity
fix(python)!: raise on bool(Expr) instead of silently dropping predic…