pola-rs/polars · error · TypeError

Unknown comparison

Error message

Unknown comparison: {op}

What it means

Compare nodes in the pyarrow predicate AST are translated to pyiceberg comparisons; the handler covers Lt, LtE (and sibling ops earlier in the chain: Gt, GtE, Eq, NotEq). A comparison operator outside the mapped set reaches the fallback and raises TypeError('Unknown comparison: {op}').

Solutions

  1. Use single, explicit comparisons: ==, !=, <, <=, >, >=, and pc/field is_null/is_nan calls
  2. Split chained comparisons into separate &-combined predicates
  3. For membership tests use pyiceberg's In expression or an equivalent `|`-chain of equality checks
  4. Upgrade polars if the operator is newly supported upstream

Example fix

// before
pc.field('a') < pc.field('b') < pc.field('c')  # chained compare
// after
(pc.field('a') < pc.field('b')) & (pc.field('b') < pc.field('c'))
Defensive patterns

Strategy: validation

Validate before calling

def comparisons_supported(expr) -> bool:
    import ast
    ok = {ast.Lt, ast.LtE, ast.Gt, ast.GtE, ast.Eq, ast.NotEq}
    return all(isinstance(n.ops[0], ok) for n in ast.walk(expr) if isinstance(n, ast.Compare))

Try / catch

try:
    lf = pl.scan_iceberg(src, filter=pred)
except TypeError as e:
    if 'Unknown comparison' in str(e):
        lf = pl.scan_iceberg(src).filter(downstream_condition)

Prevention

When it happens

Trigger: A pyarrow.compute filter with a comparison operator the converter doesn't map — e.g. `pc.field('a') % 2 == 0` producing non-compare nodes is handled elsewhere, but operators like `In` via compare, chained comparisons (`a < b < c` producing multiple ops), or an exotic op reaching this branch with an unmapped ast.cmpop.

Common situations: Chained Python comparisons in filters; using operators like `is`/`in` through the compare path; polars/pyarrow version drift where an op isn't in the mapping.

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/78c6df7b7ea1e3a3. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/iceberg/_utils.py:323

@_convert_predicate.register(Compare)
def _(a: Compare) -> Any:
    op = a.ops[0]
    lhs = _convert_predicate(a.left)[0]
    rhs = _convert_predicate(a.comparators[0])

    if isinstance(op, Gt):
        return pyiceberg.expressions.GreaterThan(lhs, rhs)  # type: ignore[misc, call-arg]
    if isinstance(op, GtE):
        return pyiceberg.expressions.GreaterThanOrEqual(lhs, rhs)  # type: ignore[misc, call-arg]
    if isinstance(op, Eq):
        return pyiceberg.expressions.EqualTo(lhs, rhs)  # type: ignore[misc, call-arg]
    if isinstance(op, Lt):
        return pyiceberg.expressions.LessThan(lhs, rhs)  # type: ignore[misc, call-arg]
    if isinstance(op, LtE):
        return pyiceberg.expressions.LessThanOrEqual(lhs, rhs)  # type: ignore[misc, call-arg]
    else:
        msg = f"Unknown comparison: {op}"
        raise TypeError(msg)


@_convert_predicate.register(List)
def _(a: List) -> Any:
    return [_convert_predicate(e) for e in a.elts]


def extract_field_initial_default(field: NestedField) -> pl.Series | None:
    from pyiceberg.types import (
        UUIDType,
    )

    if field.initial_default is None:
        return None

    value = field.initial_default

    if isinstance(field.field_type, UUIDType):

View on GitHub (pinned to fe841f959e)