Copilot commented on code in PR #823:
URL: https://github.com/apache/sedona-db/pull/823#discussion_r3206862508
##########
python/sedonadb/python/sedonadb/expr/expression.py:
##########
@@ -152,6 +153,87 @@ def negate(self) -> "Expr":
"""
return Expr(self._impl.negate())
+ # Arithmetic operators -------------------------------------------------
+ #
+ # Each binary dunder routes through the shared `_binary` helper, which
+ # coerces plain Python values to literal Exprs via `_to_expr` and then
+ # calls into the single Rust factory `expr_binary` with a string opcode.
+ # The reflected variants (`__radd__`, `__rsub__`, ...) make
+ # `1 - col("x")` work the same as `col("x") - 1`.
+
+ def __add__(self, other: Any) -> "Expr":
+ return _binary("+", self, other)
+
+ def __radd__(self, other: Any) -> "Expr":
+ return _binary("+", other, self)
+
+ def __sub__(self, other: Any) -> "Expr":
+ return _binary("-", self, other)
+
+ def __rsub__(self, other: Any) -> "Expr":
+ return _binary("-", other, self)
+
+ def __mul__(self, other: Any) -> "Expr":
+ return _binary("*", self, other)
+
+ def __rmul__(self, other: Any) -> "Expr":
+ return _binary("*", other, self)
+
+ def __truediv__(self, other: Any) -> "Expr":
+ return _binary("/", self, other)
+
+ def __rtruediv__(self, other: Any) -> "Expr":
+ return _binary("/", other, self)
+
+ def __neg__(self) -> "Expr":
+ return self.negate()
+
+ # Comparison operators -------------------------------------------------
+
+ def __eq__(self, other: Any) -> "Expr": # type: ignore[override]
+ return _binary("==", self, other)
+
+ def __ne__(self, other: Any) -> "Expr": # type: ignore[override]
+ return _binary("!=", self, other)
+
+ def __lt__(self, other: Any) -> "Expr":
+ return _binary("<", self, other)
+
+ def __le__(self, other: Any) -> "Expr":
+ return _binary("<=", self, other)
+
+ def __gt__(self, other: Any) -> "Expr":
+ return _binary(">", self, other)
+
+ def __ge__(self, other: Any) -> "Expr":
+ return _binary(">=", self, other)
+
+ # Boolean operators ----------------------------------------------------
+ #
+ # `&` / `|` / `~` rather than `and` / `or` / `not` because Python does
+ # not allow overloading the keyword forms — they always coerce to bool.
+
Review Comment:
`Expr` instances currently have default Python truthiness, so constructs
like `if col("x") > 0:` or accidentally using `and`/`or` will silently treat
the expression as `True` rather than raising. With operator overloads now
returning `Expr`, it's important to add `Expr.__bool__` (and optionally
`__len__`) that raises `TypeError` with guidance to use `&`/`|`/`~` for boolean
composition and to pass the expression into `DataFrame.filter()` / similar
consumers.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]