Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .zed/rules
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ src/orcapod/
arrow_data_utils.py — System tag manipulation, source info, column helpers
arrow_utils.py — Arrow table utilities
schema_utils.py — Schema extraction, union, intersection, compatibility
lazy_module.py — LazyModule for deferred heavy imports
lazy_module.py — LazyModule for modules `import orcapod` does not load

tests/
test_core/
Expand Down Expand Up @@ -1078,8 +1078,12 @@ generation of hashing; rows of the current data context are the current entries.

### Key patterns

- LazyModule("pyarrow") — deferred import for heavy deps. Used in
if TYPE_CHECKING: / else: blocks.
- Imports: a module that `import orcapod` loads anyway (pyarrow, pyarrow.compute, polars,
numpy, pandas, asyncio) is imported directly (ITL-1349: every LazyModule attribute read is
a __getattr__ call, 42.9 million in one warm job.run()). LazyModule (if TYPE_CHECKING: /
else: blocks) only for a module `import orcapod` never loads (networkx, deltalake,
pyarrow.fs, pyarrow.dataset, git) and the optional extras (psycopg, ray, spiral);
tests/test_utils/test_lazy_module_use.py checks it.
- Argument symmetry — operators return frozenset (commutative) or tuple (ordered).
- StaticOutputPod.process() → DynamicPodStream — wraps static_process() with staleness
detection and automatic recomputation.
Expand Down
13 changes: 10 additions & 3 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ src/orcapod/
├── arrow_data_utils.py # System tag manipulation, source info, column helpers
├── arrow_utils.py # Arrow table utilities
├── schema_utils.py # Schema extraction, union, intersection, compatibility
└── lazy_module.py # LazyModule for deferred heavy imports
└── lazy_module.py # LazyModule for modules `import orcapod` does not load

tests/
├── test_core/
Expand Down Expand Up @@ -1120,8 +1120,15 @@ and `as_table()` methods. `all_info=True` sets everything to True.

### Key patterns

- **`LazyModule("pyarrow")`** — deferred import for heavy deps (pyarrow, polars). Used in
`if TYPE_CHECKING:` / `else:` blocks at module level.
- **Imports: direct, or `LazyModule` only for a module `import orcapod` does not load**
(developer, November 22, 2026, ITL-1349). Every attribute read through a `LazyModule` is
a `__getattr__` call (42.9 million in one warm workshop-scale `job.run()`), so a module
that `import orcapod` loads anyway (pyarrow, `pyarrow.compute`, polars, numpy, pandas,
asyncio) is imported directly: `import pyarrow as pa`. `LazyModule` (in an
`if TYPE_CHECKING:` / `else:` block at module level) stays only for a module a plain
`import orcapod` never loads (networkx, deltalake, `pyarrow.fs`, `pyarrow.dataset`, git)
and for the optional extras (psycopg, ray, spiral).
`tests/test_utils/test_lazy_module_use.py` checks it.
- **Argument symmetry** — each operator declares `argument_symmetry(streams)` returning
`frozenset` (commutative) or `tuple` (ordered). Determines how upstream hashes combine.
- **`StaticOutputPod.process()` → `DynamicPodStream`** — wraps `static_process()` output
Expand Down
3 changes: 3 additions & 0 deletions changelog.d/ITL-1349.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Changed

- **Less overhead per row.** Orcapod imports pyarrow, `pyarrow.compute`, polars, numpy and pandas directly instead of through a lazy module wrapper, which cost a Python call on every attribute access (millions in one job run). `import orcapod` already loaded these modules, so import time does not change. Modules that `import orcapod` does not load (networkx, deltalake, the optional database clients) stay deferred. Results, hashes and stored tables are unchanged. (ITL-1349)
13 changes: 4 additions & 9 deletions src/orcapod/contexts/stored.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
import warnings
from collections.abc import Collection
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, NamedTuple, Protocol, cast
from typing import Any, NamedTuple, Protocol, cast

import pyarrow as pa
import pyarrow.compute as pc

from orcapod.errors import (
InputValidationError,
Expand All @@ -49,14 +52,6 @@
TRACKING_DB_SCHEMA_VERSION,
constants,
)
from orcapod.utils.lazy_module import LazyModule

if TYPE_CHECKING:
import pyarrow as pa
import pyarrow.compute as pc
else:
pa = LazyModule("pyarrow")
pc = LazyModule("pyarrow.compute")

logger = logging.getLogger(__name__)

Expand Down
7 changes: 1 addition & 6 deletions src/orcapod/core/data_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import typing
from typing import TYPE_CHECKING, Any, ClassVar, Generic, Literal, TypeVar

import pyarrow as pa
from uuid_utils import uuid7

from orcapod.config import OrcapodConfig
Expand Down Expand Up @@ -46,7 +47,6 @@
from orcapod.utils import schema_utils
from orcapod.utils.canonical_type_names import stabilize_type_names
from orcapod.utils.git_utils import get_git_info_for_python_object
from orcapod.utils.lazy_module import LazyModule
from orcapod.utils.tensor_conversions import describe, tensor_conversions, warn_once

from orcapod.protocols.observability_protocols import DataExecutionLoggerProtocol
Expand All @@ -55,11 +55,6 @@
from orcapod.logical_types.protocols import (
TypeConverterProtocol as LogicalTypeConverterProtocol,
)
import pyarrow as pa
import pyarrow.compute as pc
else:
pa = LazyModule("pyarrow")
pc = LazyModule("pyarrow.compute")

module_logger = logging.getLogger(__name__)

Expand Down
8 changes: 2 additions & 6 deletions src/orcapod/core/data_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@
from contextvars import ContextVar
from typing import TYPE_CHECKING, Any, Literal

import pyarrow as pa

from orcapod.errors import InputValidationError
from orcapod.types import Schema, SourceInfoValue

from orcapod.utils.lazy_module import LazyModule

if TYPE_CHECKING:
import pyarrow as pa

from orcapod.protocols.core_protocols import DataProtocol, TagProtocol
else:
pa = LazyModule("pyarrow")

# Diagnostic emitted when a structural operator materializes a data-tag (section 7.4).
# ``"warn"`` emits a ``DataTagMaterializationWarning``; ``"silent"`` emits nothing. The
Expand Down
9 changes: 2 additions & 7 deletions src/orcapod/core/datagrams/datagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import logging
import uuid
from collections.abc import Collection, Iterator, Mapping
from typing import TYPE_CHECKING, Any, Self, cast
from typing import Any, Self, cast

import pyarrow as pa
from uuid_utils import uuid7

from orcapod.hashing.orcapod_table import OrcapodTable
Expand All @@ -38,12 +39,6 @@
from orcapod.system_constants import constants
from orcapod.types import ColumnConfig, DataType, DataValue, Schema, SchemaLike
from orcapod.utils import arrow_utils
from orcapod.utils.lazy_module import LazyModule

if TYPE_CHECKING:
import pyarrow as pa
else:
pa = LazyModule("pyarrow")

logger = logging.getLogger(__name__)

Expand Down
7 changes: 2 additions & 5 deletions src/orcapod/core/datagrams/tag_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, Any, Self

import pyarrow as pa

from orcapod import contexts
from orcapod.core.datagrams.datagram import Datagram, context_key_field
from orcapod.semantic_types import infer_python_schema_from_pylist_data
Expand All @@ -35,14 +37,9 @@
SourceInfoValue,
)
from orcapod.utils import arrow_utils
from orcapod.utils.lazy_module import LazyModule

if TYPE_CHECKING:
import pyarrow as pa

from orcapod.protocols.core_protocols import DataProtocol
else:
pa = LazyModule("pyarrow")

logger = logging.getLogger(__name__)

Expand Down
8 changes: 2 additions & 6 deletions src/orcapod/core/function_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from functools import update_wrapper, wraps
from typing import TYPE_CHECKING, Any, Literal, Protocol, cast

import pyarrow as pa

from orcapod import contexts
from orcapod.channels import ReadableChannel, WritableChannel
from orcapod.config import OrcapodConfig
Expand Down Expand Up @@ -59,7 +61,6 @@
resolve_concurrency,
)
from orcapod.utils import arrow_utils, schema_utils
from orcapod.utils.lazy_module import LazyModule

from datetime import datetime, timezone

Expand All @@ -75,12 +76,7 @@
logger = logging.getLogger(__name__)

if TYPE_CHECKING:
import polars as pl
import pyarrow as pa
from orcapod.protocols.observability_protocols import ExecutionObserverProtocol
else:
pa = LazyModule("pyarrow")
pl = LazyModule("polars")


def _executor_supports_concurrent(
Expand Down
13 changes: 4 additions & 9 deletions src/orcapod/core/nodes/function_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
from abc import abstractmethod
from collections import Counter
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
from typing import TYPE_CHECKING, Any, Literal, NamedTuple, cast
from typing import Any, Literal, NamedTuple, cast

import polars as pl
import pyarrow as pa

from orcapod import contexts
from orcapod.core.function_pod import (
Expand Down Expand Up @@ -116,17 +119,9 @@
Schema,
)
from orcapod.utils import arrow_utils, schema_utils
from orcapod.utils.lazy_module import LazyModule

logger = logging.getLogger(__name__)

if TYPE_CHECKING:
import polars as pl
import pyarrow as pa
else:
pa = LazyModule("pyarrow")
pl = LazyModule("polars")

# Pipeline entry ID column name used when fetching records from the pipeline
# database. Always present in the table returned by _fetch_joined_records.
_PIPELINE_ENTRY_ID_COL = "__pipeline_entry_id"
Expand Down
10 changes: 3 additions & 7 deletions src/orcapod/core/nodes/operator_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from collections.abc import Collection, Iterator, Mapping, Sequence
from typing import TYPE_CHECKING, Any, Literal, cast

import pyarrow as pa
import pyarrow.compute as pc

from orcapod import contexts
from orcapod.core.data_tags import (
mark_data_tags,
Expand Down Expand Up @@ -82,18 +85,11 @@
from orcapod.system_constants import OPERATOR_DB_SCHEMA_VERSION, constants
from orcapod.types import CacheMode, ColumnConfig, ContentHash, Schema
from orcapod.utils import arrow_utils
from orcapod.utils.lazy_module import LazyModule

logger = logging.getLogger(__name__)

if TYPE_CHECKING:
import pyarrow as pa
import pyarrow.compute as pc

from orcapod.protocols.observability_protocols import ExecutionObserverProtocol
else:
pa = LazyModule("pyarrow")
pc = LazyModule("pyarrow.compute")


async def _discard(channel: ReadableChannel[Any]) -> None:
Expand Down
7 changes: 2 additions & 5 deletions src/orcapod/core/nodes/source_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from datetime import datetime
from typing import TYPE_CHECKING, Any

import pyarrow as pa

from orcapod import contexts
from orcapod.config import OrcapodConfig
from orcapod.core.base import TraceableBase
Expand Down Expand Up @@ -46,21 +48,16 @@
from orcapod.types import ColumnConfig, ContentHash, Schema
from orcapod.utils import arrow_utils
from orcapod.utils.arrow_utils import system_tag_column_names
from orcapod.utils.lazy_module import LazyModule
from orcapod.utils.schema_utils import (
arrow_schema_identity,
compute_source_schema_hash,
declared_arrow_schema,
)

if TYPE_CHECKING:
import pyarrow as pa

from orcapod.channels import WritableChannel
from orcapod.protocols.core_protocols import StreamProtocol
from orcapod.protocols.observability_protocols import ExecutionObserverProtocol
else:
pa = LazyModule("pyarrow")

logger = logging.getLogger(__name__)

Expand Down
7 changes: 2 additions & 5 deletions src/orcapod/core/operators/add_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, ClassVar

import pyarrow as pa

from orcapod.channels import ReadableChannel, WritableChannel
from orcapod.core.data_tags import stream_data_tags
from orcapod.core.datagrams import Tag
Expand All @@ -19,14 +21,9 @@
from orcapod.system_constants import constants
from orcapod.types import ColumnConfig, ContentHash, Schema
from orcapod.utils import schema_utils
from orcapod.utils.lazy_module import LazyModule

if TYPE_CHECKING:
import pyarrow as pa

from orcapod.protocols.semantic_types_protocols import TypeConverterProtocol
else:
pa = LazyModule("pyarrow")

TagConstant = bool | int | float | str | bytes
"""A value ``AddTags`` accepts: its exact Python type is the tag's declared type."""
Expand Down
17 changes: 5 additions & 12 deletions src/orcapod/core/operators/arrow_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,11 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, cast

from orcapod.utils.lazy_module import LazyModule

if TYPE_CHECKING:
import numpy as np
import polars as pl
import pyarrow as pa
else:
np = LazyModule("numpy")
pl = LazyModule("polars")
pa = LazyModule("pyarrow")
from typing import Any, cast

import numpy as np
import polars as pl
import pyarrow as pa


def is_list_kind(arrow_type: "pa.DataType") -> bool:
Expand Down
13 changes: 4 additions & 9 deletions src/orcapod/core/operators/batch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import Any, ClassVar, TYPE_CHECKING, cast
from typing import Any, ClassVar, cast

import numpy as np
import pyarrow as pa

from orcapod.core.operators.meta_columns import WithoutMetaWriter
from orcapod.channels import ReadableChannel, WritableChannel
Expand All @@ -12,14 +15,6 @@
from orcapod.system_constants import constants
from orcapod.types import ColumnConfig, ContentHash
from orcapod.utils import arrow_utils
from orcapod.utils.lazy_module import LazyModule

if TYPE_CHECKING:
import numpy as np
import pyarrow as pa
else:
np = LazyModule("numpy")
pa = LazyModule("pyarrow")

from orcapod.types import Schema

Expand Down
8 changes: 1 addition & 7 deletions src/orcapod/core/operators/column_selection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from abc import ABC, abstractmethod
from collections.abc import Collection, Sequence
from typing import TYPE_CHECKING, Any, ClassVar
from typing import Any, ClassVar

from orcapod.core.operators.meta_columns import WithoutMetaWriter
from orcapod.channels import ReadableChannel, WritableChannel
Expand All @@ -12,14 +12,8 @@
from orcapod.protocols.core_protocols import DataProtocol, StreamProtocol, TagProtocol
from orcapod.system_constants import constants
from orcapod.types import ColumnConfig, ContentHash, Schema
from orcapod.utils.lazy_module import LazyModule
from orcapod.utils.schema_utils import column_names

if TYPE_CHECKING:
import pyarrow as pa
else:
pa = LazyModule("pyarrow")

logger = logging.getLogger(__name__)


Expand Down
Loading
Loading