Repository navigation
fix(python)!: raise on bool(Expr) instead of silently dropping predicates - #4682
Merged
Merged
Conversation
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.
Xuanwo
approved these changes
Oct 27, 2026
Xuanwo
left a comment
Member
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Python's
and/or/notand chained comparisons callbool()on anExpr.Exprhad no__bool__, so that was alwaysTrueand 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 onlya(see the issue for the repro and output).Changes (
python/python/lancedb/expr.py,python/python/lancedb/query.py):Expr.__bool__raisesTypeError("The truth value of an Expr is ambiguous. ...")with a hint touse
&,|,~with parentheses andExpr.isin()for membership.TypeErrorrather thanValueErrorbecause the object does not support truthiness at all (polarsExprand SQLAlchemyclauses raise
TypeError; the codebase raisesTypeErrorfor unsupportedlit()values).pandas, numpy and pyarrow
ExpressionuseValueError.LanceHybridQueryBuildertestedif self._where:, which would have started raising for every.where(Expr)on a hybrid query. It is nowisinstance(self._where, Expr) or self._where, soan Expr is always applied and an empty SQL string is still skipped as before.
Exprdocstring.Not changed: no other code in
python/python/lancedbtests the truthiness of a filter or of anExpr(checkedwhere/filter/predicate/conditionhandling inquery.py,table.py,remote/,merge.py,functions.py,pydantic.py: they useis None/isinstance). Cross-SDK:nodejshas noExprclass onmainyet, 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/ chainedcomparisons on an
Expr(which returned wrong rows) now raisesTypeError.x in [expr, ...],any([...])andall([...])on Exprs, and==between twoQueryobjects whosefilteraredifferent Expr objects (pydantic compares the fields), now raise instead of returning a meaningless
answer;
Exprstays unhashable andisinstance/is Nonechecks are unaffected.Tests (
python/python/tests/test_expr.py):TestExprTruthiness(bool,and,or,not,chained comparison,
if, list membership,any/allraise 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 thenew
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 testswere run against the 0.41.0b14 native module (
python/python/lancedb/expr.pyandquery.pyareidentical 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 pythonandruff check pythonare clean. Not run: Rust build, Node tests, remote (cloud)tests.
Fixes #4681