loom.etl.backends.polars¶
Polars backend for the Loom ETL framework.
- class loom.etl.backends.polars.PolarsFileWriter[source]¶
Bases:
objectWrite FILE targets with Polars DataFrame writers.
- class loom.etl.backends.polars.PolarsPhysicalSchema(schema, partition_columns=())[source]¶
Bases:
objectPhysical schema snapshot for a resolved Polars target.
- class loom.etl.backends.polars.PolarsSourceReader(locator, *, route_resolver=None, file_locator=None, mongo_reader=None, clickhouse_reader=None, dynamodb_reader=None)[source]¶
Bases:
SourceReaderRead table, file, and MongoDB sources using Polars.
- Parameters:
locator (str | TableLocator)
route_resolver (TableRouteResolver | None)
file_locator (FileLocator | None)
mongo_reader (MongoSourceReader | None)
clickhouse_reader (ClickHouseSourceReader | None)
dynamodb_reader (DynamoDbSourceReader | None)
- read(spec, params_instance, /)[source]¶
Read source spec and return lazy frame.
- Parameters:
spec (TableSourceSpec | FileSourceSpec | TempSourceSpec | MongoSourceSpec | ClickHouseSourceSpec | DynamoDbSourceSpec)
params_instance (Any)
- Return type:
polars.LazyFrame
- read_streaming(spec, params_instance, /)[source]¶
Route streaming reads to the matching capability-aware sub-reader.
Only
ClickHouseSourceSpecis supported today; other kinds raiseTypeError. We refuse to silently fall back toread()because a step that opts into streaming for memory safety must fail loudly rather than risk OOM on a non-streaming path.- Parameters:
spec (TableSourceSpec | FileSourceSpec | TempSourceSpec | MongoSourceSpec | ClickHouseSourceSpec | DynamoDbSourceSpec) – Compiled source specification.
params_instance (Any) – Concrete params for current run.
- Returns:
Lazy frame whose
collect(engine="streaming")is memory-bounded.- Raises:
TypeError – When spec is not a ClickHouse spec, or the matching sub-reader was not configured at construction time.
- Return type:
polars.LazyFrame
- class loom.etl.backends.polars.PolarsTargetWriter(locator, *, route_resolver=None, missing_table_policy=MissingTablePolicy.SCHEMA_MODE, file_locator=None, audit_config=None)[source]¶
Bases:
_WritePolicy[LazyFrame,DataFrame,PolarsPhysicalSchema]Polars target writer using delta-rs for Delta tables.
- Parameters:
locator (str | TableLocator)
route_resolver (TableRouteResolver | None)
missing_table_policy (MissingTablePolicy)
file_locator (FileLocator | None)
audit_config (AuditConfig | None)
- loom.etl.backends.polars.decode_json_columns(frame, json_columns)[source]¶
Decode JSON string columns the way
PolarsSourceReaderdoes at read time.Each column named in json_columns holds JSON text and is replaced by the value decoded into its declared
LoomType. Rows are first normalised to that type, so a field that is a string in some rows and an object in others decodes instead of raisingpolars.exceptions.ComputeError. A null or unparseable value decodes to null. Other columns pass through unchanged.Use it to reproduce in a test what a step receives from a source declared with
parse_json.- Parameters:
frame (FrameT) – Eager or lazy frame holding the JSON text columns.
json_columns (Iterable[JsonColumnSpec]) – Column declarations, e.g.
FromTable(...).json_columnsorFromFile(...).json_columns.
- Returns:
A frame of the same kind as frame, or frame itself when json_columns is empty.
- Return type:
FrameT
Example:
from loom.etl.backends.polars import decode_json_columns events = FromTable("raw.events").parse_json("payload", Payload) decoded = decode_json_columns(raw_frame, events.json_columns)
- loom.etl.backends.polars.loom_type_to_polars(lt)[source]¶
Recursively convert a
LoomTypeto a concrete Polars DataType.Returns an instance (not a class) so it can be used directly in
pl.col(...).cast(...)andpl.lit(None).cast(...).- Parameters:
lt (LoomDtype | ListType | ArrayType | StructType | DecimalType | DatetimeType | DurationType | CategoricalType | EnumType) – Any
LoomTypevalue.- Returns:
Concrete Polars DataType instance.
- Raises:
TypeError – If lt is a coarse structural
LoomDtype(e.g.LoomDtype.LIST) whose inner type is unknown. UseListType,StructType, etc. instead.- Return type:
polars.DataType
- loom.etl.backends.polars.polars_to_loom_type(dtype)[source]¶
Recursively convert a Polars DataType to its
LoomType.Handles arbitrarily nested types —
List[Struct[...]],Struct[List[...]],Decimal(18,4),Datetime("ns","UTC"), etc.For primitive types the result is the corresponding
LoomDtypemember (which is part of theLoomTypeunion).- Parameters:
dtype (pl.DataType | DataTypeClass) – Polars DataType instance.
- Returns:
Matching
LoomTypevalue.- Return type:
- loom.etl.backends.polars.read_delta_physical_schema(uri, storage_options=None)[source]¶
Read physical schema directly from a Delta table URI.
- Parameters:
- Returns:
Physical schema when the table exists, else
None.- Return type:
PolarsPhysicalSchema | None