Skip to content

fix(python)!: raise on bool(Expr) instead of silently dropping predicates - #4682

Merged
Xuanwo merged 3 commits into
lancedb:mainfrom
avanish-garg:fix/expr-bool-ambiguous
Oct 27, 2026
Merged

Xuanwo merged 3 commits into
lancedb:mainfrom
avanish-garg:fix/expr-bool-ambiguous

Conversation

@avanish-garg

Copy link
Copy Markdown
Contributor

Python's and / or / not and chained comparisons call bool() on an Expr. Expr had no
__bool__, so that was always True and the filter silently lost a predicate: where(lit(2) < col("id") < lit(5)) returned ids 0..4 (lower bound dropped), (col("id") > 7) and (col("g") == 0)
returned all even ids, and (a) or (b) kept only a (see the issue for the repro and output).

Changes (python/python/lancedb/expr.py, python/python/lancedb/query.py):

  • Expr.__bool__ raises TypeError("The truth value of an Expr is ambiguous. ...") with a hint to
    use &, |, ~ with parentheses and Expr.isin() for membership. TypeError rather than
    ValueError because the object does not support truthiness at all (polars Expr and SQLAlchemy
    clauses raise TypeError; the codebase raises TypeError for unsupported lit() values).
    pandas, numpy and pyarrow Expression use ValueError.
  • LanceHybridQueryBuilder tested if self._where:, which would have started raising for every
    .where(Expr) on a hybrid query. It is now isinstance(self._where, Expr) or self._where, so
    an Expr is always applied and an empty SQL string is still skipped as before.
  • Notes section in the Expr docstring.

Not changed: no other code in python/python/lancedb tests the truthiness of a filter or of an
Expr (checked where/filter/predicate/condition handling in query.py, table.py,
remote/, merge.py, functions.py, pydantic.py: they use is None / isinstance). Cross-SDK:
nodejs has no Expr class on main yet, and JavaScript cannot overload && / || / !
either, so there is nothing equivalent to port; the Rust core is not affected.

Behaviour changes to be aware of: code that already used and / or / not / chained
comparisons on an Expr (which returned wrong rows) now raises TypeError. x in [expr, ...],
any([...]) and all([...]) on Exprs, and == between two Query objects whose filter are
different Expr objects (pydantic compares the fields), now raise instead of returning a meaningless
answer; Expr stays unhashable and isinstance / is None checks are unaffected.

Tests (python/python/tests/test_expr.py): TestExprTruthiness (bool, and, or, not,
chained comparison, if, list membership, any/all raise with a helpful message; &, |, ~
still combine) and TestExprWhereBuilders (plain, vector, FTS, hybrid and async .where(Expr)
return the right rows, hybrid with an SQL string and with an empty string is unchanged, chained
comparison is rejected before any query runs). With the source change stashed 11 of the new tests
fail; with only __bool__ added and the hybrid change reverted the hybrid test fails with the
new TypeError, so it guards the second change. test_expr.py: 144 passed (123 before this change).

Verification caveat: main's Python layer is newer than the newest published wheel, so the tests
were run against the 0.41.0b14 native module (python/python/lancedb/expr.py and query.py are
identical to the wheel); the full suite (pytest python/tests --ignore=python/tests/docs -m "not slow and not s3_test")
has the identical 25 failures before and after (wheel/main skew in remote options, remote db,
index and table tests; none in expr/query tests): 2604 passed before, 2625 passed after (the 21
new tests). ruff format --check python and ruff check python are clean. Not run: Rust build, Node tests, remote (cloud)
tests.

Fixes #4681

Expr had no __bool__, so Python's and/or/not and chained comparisons
(1 < col('x') < 5) always saw True and kept a single operand, silently
dropping the other predicate and returning wrong rows. bool(Expr) now
raises TypeError pointing to &, | and ~.

The hybrid query builder tested if self._where:, which would have raised
for every .where(Expr); it now checks for an Expr explicitly.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added bug Something isn't working Python Python SDK labels Oct 26, 2026
lancedb-gatekeeper[bot]

This comment was marked as outdated.

@lancedb-gatekeeper lancedb-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 26, 2026
@Xuanwo Xuanwo added the breaking-change A breaking change in our public APIs label Oct 27, 2026
@github-actions github-actions Bot removed the breaking-change A breaking change in our public APIs label Oct 27, 2026
@Xuanwo Xuanwo changed the title fix(python): raise on bool(Expr) instead of silently dropping predicates fix(python)!: raise on bool(Expr) instead of silently dropping predicates Oct 27, 2026
@github-actions github-actions Bot added the breaking-change A breaking change in our public APIs label Oct 27, 2026
@lancedb-gatekeeper lancedb-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 27, 2026

@lancedb-gatekeeper lancedb-gatekeeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Gate recommendation: approve.

Expression truth tests now fail before predicates can be silently discarded. The hybrid change preserves valid .where(Expr) filters and the existing empty-string behavior.

@lancedb-gatekeeper lancedb-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 27, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expression truthiness guard fixes silent predicate loss, and the explicit hybrid handling preserves Expr filters. The regression coverage exercises the affected public query paths; all applicable checks and Gatekeeper pass on this commit.

@Xuanwo
Xuanwo merged commit 8887c82 into lancedb:main Oct 27, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change A breaking change in our public APIs bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance. Python Python SDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants