Skip to main content

Export: Polars

Converts the data contract into a Python module that defines a Polars pl.Schema for each schema. Polars does not need to be installed to run the export.

datacontract export polars orders.odcs.yaml --output orders_schema.py

Running this against the example orders contract produces:

import polars as pl

orders = pl.Schema(
{
"order_id": pl.String,
"order_timestamp": pl.Datetime(time_unit="us"),
"customer_id": pl.String,
"order_total": pl.Int64,
"status": pl.String,
}
)

line_items = pl.Schema(
{
"line_item_id": pl.String,
"order_id": pl.String,
"sku": pl.String,
"quantity": pl.Int64,
}
)

Types​

The physicalType decides when it names a width or a precision, then the format of an integer or number, then the logicalType.

ContractPolars type
int16Int16
int32Int32
bigint, int64Int64
hugeint, int128Int128
uint8 to uint128, and the unsigned MySQL and DuckDB integer typesUInt8 to UInt128
float16Float16
float32Float32
double, float64Float64
decimal(p,s), numeric(p,s), number(p,s)Decimal(precision=p, scale=s)
decimal, numericDecimal(), whose precision Polars chooses and whose scale is 0
timestamp(n), timestamp_ntz(n)Datetime in milliseconds for n up to 3, microseconds up to 6, nanoseconds above it, and microseconds without n
timestamp_tz, timestamp with time zone, or logicalType: timestamp with timezone: trueDatetime in the defaultTimezone, or in UTC
logicalType: integer with format i8 to i128 or u8 to u128Int8 to Int128, UInt8 to UInt128
logicalType: number with format f32 or f64Float32, Float64
logicalType: integer, number, string, boolean, date, timeInt64, Float64, String, Boolean, Date, Time
logicalType: array, objectList, Struct
logicalType: mapList of Struct with key and value, as Polars reads Arrow maps
logicalType: vectorArray of the elementType with dimensions elements; binary elements are Boolean, bfloat16 ones Float32

Some names have no width of their own, so the logicalType decides: int8 is a 64-bit integer in Postgres and DuckDB and an 8-bit one in ClickHouse, tinyint, smallint, int and integer are 64-bit in BigQuery and NUMBER(38,0) in Snowflake, and float, float4 and real are 64-bit in Snowflake. A decimal with more than 38 digits fails the export, since a Polars decimal cannot hold it. A property with neither a logicalType nor a physicalType is Null.

Polars 1.0 has no Int128, UInt128 or Float16. The module uses them only for a contract that declares those widths.

All options: datacontract export polars.