diff --git a/AGENTS.md b/AGENTS.md index 7493fdc6f2..fe2174a27b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -52,6 +52,19 @@ lives under `pyiceberg/`, organized by concern rather than by engine: - Use domain-specific exceptions from pyiceberg/exceptions.py for Iceberg-specific error conditions. For invalid arguments/values, ValueError is acceptable. Don't raise bare Exception. - Comments should be succinct and follow the same style of comments found in the rest of the codebase. +### Visitors + +Prefer the existing visitor pattern over hand-written recursion (e.g. `isinstance` chains over `StructType`/`ListType`/`MapType` or `And`/`Or`/`Not`) when traversing schemas or expressions. Subclass the matching base visitor and look at the existing subclasses for examples. + +- **Iceberg schemas** (`pyiceberg/schema.py`): + - `SchemaVisitor` with `visit()`: post-order traversal; `SchemaVisitorPerPrimitiveType` adds one method per primitive type. + - `PreOrderSchemaVisitor` with `pre_order_visit()`: when the parent decides whether/how to visit children (e.g. `_SchemaCompatibilityVisitor`). + - `SchemaWithPartnerVisitor` + `PartnerAccessor` with `visit_with_partner()`: walk a schema alongside a partner structure (e.g. `ArrowProjectionVisitor`/`ArrowAccessor` in `io/pyarrow.py`, `SchemaPartnerAccessor` in `avro/resolver.py`). +- **Arrow schemas** (`pyiceberg/io/pyarrow.py`): `PyArrowSchemaVisitor` with `visit_pyarrow()` (e.g. `_ConvertToIceberg`, `_HasIds`). Iceberg-to-Arrow conversion uses `_ConvertToArrowSchema`. +- **Expressions** (`pyiceberg/expressions/visitors.py`): + - `BooleanExpressionVisitor` with `visit()`: unbound expressions (e.g. `BindVisitor`, `_RewriteNotVisitor`). + - `BoundBooleanExpressionVisitor`: bound predicates, with one method per predicate type (e.g. `_ExpressionEvaluationVisitor`, `_InclusiveMetricsEvaluationVisitor`, `_ConvertToArrowExpression` in `io/pyarrow.py`). + ### Spec Compliance - The [Iceberg spec](https://github.com/apache/iceberg/blob/main/format/spec.md) is the source of truth. Check changes to metadata, manifests, schemas, partitioning, transforms, or the commit path against it, and cite the relevant section when behavior is non-obvious.