{"record":{"id":"dff199bca543e490","repo":"pola-rs/polars","slug":"unexpected-unaryop-a","errorCode":null,"errorMessage":"Unexpected UnaryOp: {a}","messagePattern":"Unexpected UnaryOp: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/iceberg/_utils.py","lineNumber":258,"sourceCode":"\n@_convert_predicate.register(Constant)\ndef _(a: Constant) -> Any:\n    return a.value\n\n\n@_convert_predicate.register(Name)\ndef _(a: Name) -> Any:\n    return a.id\n\n\n@_convert_predicate.register(UnaryOp)\ndef _(a: UnaryOp) -> Any:\n    if isinstance(a.op, Invert):\n        operand = _ensure_boolean_expression(_convert_predicate(a.operand))\n        return pyiceberg.expressions.Not(operand)\n    else:\n        msg = f\"Unexpected UnaryOp: {a}\"\n        raise TypeError(msg)\n\n\n@_convert_predicate.register(Call)\ndef _(a: Call) -> Any:\n    args = [_convert_predicate(arg) for arg in a.args]\n    f = _convert_predicate(a.func)\n    if f == \"field\":\n        return args\n    elif f == \"scalar\":\n        return args[0]\n    elif f in _temporal_conversions:\n        # convert from polars-native i64 to ISO8601 string\n        return _temporal_conversions[f](*args).isoformat()\n    else:\n        ref = _convert_predicate(a.func.value)[0]  # type: ignore[attr-defined]\n        if f == \"isin\":\n            return pyiceberg.expressions.In(ref, args[0])  # type: ignore[misc, call-arg]\n        elif f == \"is_null\":","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/iceberg/_utils.py#L240-L276","documentation":"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: ...').","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"// before\npc.field('x') == -pc.field('y')  # Unexpected UnaryOp\n// after\npc.field('x') + pc.field('y') == 0  # or restructure; better: pc.field('x') < 0 if intent was negation","handlingStrategy":"validation","validationCode":"def unary_ops_ok(expr) -> bool:\n    import ast\n    return all(isinstance(n.op, ast.Invert)\n               for n in ast.walk(expr) if isinstance(n, ast.UnaryOp))","typeGuard":null,"tryCatchPattern":"try:\n    lf = pl.scan_iceberg(src, filter=pred)\nexcept TypeError as e:\n    if 'Unexpected UnaryOp' in str(e):\n        lf = pl.scan_iceberg(src).filter(polars_equivalent)","preventionTips":["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"],"tags":["iceberg","predicate","pyarrow","unary-operator"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}