pola-rs/polars · error · TypeError
Unexpected UnaryOp
Error message
Unexpected UnaryOp: {a} What it means
When converting a pyarrow filter predicate, a UnaryOp (unary operator) node is only supported if it is `Invert` (`~`), which maps to pyiceberg's Not. Any other unary operator (e.g. unary minus/plus, `not` handled elsewhere) reaches this branch and raises TypeError('Unexpected UnaryOp: ...').
Solutions
- Use only `~` for negation in predicates; invert the comparison instead (e.g. `~(a == b)` or `a != b`)
- Replace unary arithmetic on fields with comparisons against literal values (e.g. `field('x') < 0` instead of `-field('x') > 0`)
- Express the predicate directly as a pyiceberg expression if it needs shapes pyarrow filters can't encode
Example fix
// before
pc.field('x') == -pc.field('y') # Unexpected UnaryOp
// after
pc.field('x') + pc.field('y') == 0 # or restructure; better: pc.field('x') < 0 if intent was negation Defensive patterns
Strategy: validation
Validate before calling
def unary_ops_ok(expr) -> bool:
import ast
return all(isinstance(n.op, ast.Invert)
for n in ast.walk(expr) if isinstance(n, ast.UnaryOp)) Try / catch
try:
lf = pl.scan_iceberg(src, filter=pred)
except TypeError as e:
if 'Unexpected UnaryOp' in str(e):
lf = pl.scan_iceberg(src).filter(polars_equivalent) Prevention
- Use `~` only for logical negation in pyarrow filters
- Never apply unary minus/plus to fields inside Iceberg predicates
- Invert comparisons (a != b) instead of negating values
- Test predicates against scan_iceberg in CI
When it happens
Trigger: A pyarrow.compute expression passed to scan_iceberg containing a unary operator other than `~`, e.g. `pc.field('x') == -pc.field('y')` or `-field('a') == 0`, causing the AST walker to hit an unsupported UnaryOp.
Common situations: Writing predicates with negated numeric fields or unary plus; assuming arbitrary Python operators are translatable to Iceberg expressions (Iceberg predicates are boolean comparisons/boolean algebra only).
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/dff199bca543e490.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/io/iceberg/_utils.py:258
@_convert_predicate.register(Constant)
def _(a: Constant) -> Any:
return a.value
@_convert_predicate.register(Name)
def _(a: Name) -> Any:
return a.id
@_convert_predicate.register(UnaryOp)
def _(a: UnaryOp) -> Any:
if isinstance(a.op, Invert):
operand = _ensure_boolean_expression(_convert_predicate(a.operand))
return pyiceberg.expressions.Not(operand)
else:
msg = f"Unexpected UnaryOp: {a}"
raise TypeError(msg)
@_convert_predicate.register(Call)
def _(a: Call) -> Any:
args = [_convert_predicate(arg) for arg in a.args]
f = _convert_predicate(a.func)
if f == "field":
return args
elif f == "scalar":
return args[0]
elif f in _temporal_conversions:
# convert from polars-native i64 to ISO8601 string
return _temporal_conversions[f](*args).isoformat()
else:
ref = _convert_predicate(a.func.value)[0] # type: ignore[attr-defined]
if f == "isin":
return pyiceberg.expressions.In(ref, args[0]) # type: ignore[misc, call-arg]
elif f == "is_null":View on GitHub (pinned to fe841f959e)