Skip to content

bug(python): Expr has no __bool__, so "and" / "or" / chained comparisons silently drop a filter predicate #4681

Closed

Description

@avanish-garg

LanceDB version

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.

Are there known steps to reproduce?

import tempfile, lancedb
from lancedb.expr import col, lit

db = lancedb.connect(tempfile.mkdtemp())
t = db.create_table("t", [{"id": i, "g": i % 2, "vector": [float(i), 1.0]} for i in range(12)])
ids = lambda where: sorted(r["id"] for r in t.search().where(where).limit(100).to_list())

print(ids("id > 2 AND id < 5"))                         # [3, 4]
print(ids((col("id") > 2) & (col("id") < 5)))           # [3, 4]
print(ids(lit(2) < col("id") < lit(5)))                 # [0, 1, 2, 3, 4]   <- lower bound dropped
print(ids((col("id") > 7) and (col("g") == 0)))         # [0, 2, 4, 6, 8, 10]  <- first predicate dropped
print(ids((col("id") > 7) or (col("g") == 0)))          # [8, 9, 10, 11]    <- second predicate dropped
print(bool(col("id") == 1))                             # True

Output on main (0.41.0b14 native module):

[3, 4]
[3, 4]
[0, 1, 2, 3, 4]
[0, 2, 4, 6, 8, 10]
[8, 9, 10, 11]
True

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

  1. added
    bug
    on Oct 26, 2026
  2. added a commit that references this issue on Oct 27, 2026
    8887c82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions