crv.io.write
Experimental API
crv.io.write
Append-only writer for canonical CRV tables.
Overview - Computes/ensures tick-bucket partitioning (bucket = tick // IoSettings.tick_bucket_size). - Validates frames against crv.core.tables descriptors (source of truth). - Writes Parquet parts with atomic tmp → ready rename and embeds version/table/run_id metadata. - Updates per-table manifest.json with per-part and per-bucket aggregates.
Source of truth - Canonical table names: crv.core.grammar.TableName (lower_snake). - Table descriptors and partitioning discipline: crv.core.tables (partitioning=["bucket"]). - Schema version metadata: crv.core.versioning.SCHEMA_V (embedded in Parquet key-value metadata). - Domain errors (naming/combination rules): crv.core.schema / crv.core.errors. - IO-layer errors: crv.io.errors.IoSchemaError, IoWriteError, IoManifestError.
Notes - Single-writer semantics initially (no inter-process locking). - File protocol baseline; remote backends (e.g., fsspec) can be layered later.
crv.io.write.append
append(
settings: crv.io.config.IoSettings,
run_id: str,
table: crv.core.grammar.TableName | str,
df: polars.DataFrame,
*,
validate_schema: bool = True,
validate_rows: bool = False
) -> dict[str, Any]
Append a DataFrame to a canonical table with atomic semantics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
crv.io.config.IoSettings
|
IO configuration (root_dir, compression, etc.). |
required |
run_id
|
str
|
Run identifier used to construct dataset paths (see crv.core.ids.RunId; stored as string on disk). |
required |
table
|
crv.core.grammar.TableName | str
|
Canonical table name (enum or lower_snake string). |
required |
df
|
polars.DataFrame
|
Polars DataFrame to append. Must include 'tick'; 'bucket' is computed as tick // IoSettings.tick_bucket_size. |
required |
validate_schema
|
bool
|
Validate against crv.core.tables descriptor (default True). |
True
|
validate_rows
|
bool
|
Reserved for future row-level validation (unused). |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, typing.Any]
|
dict[str, Any]: Summary with keys: - table (str) - run_id (str) - parts (list[dict]): Per-part {"bucket_id","path","rows","bytes","tick_min","tick_max"} - rows (int): Total rows written - buckets (list[int]): Buckets touched |
Raises:
| Type | Description |
|---|---|
crv.io.errors.IoSchemaError
|
Frame failed validation against core descriptor. |
crv.io.errors.IoWriteError
|
Parquet write/fsync/atomic-rename failed. |
crv.io.errors.IoManifestError
|
Manifest write failed. |
Notes
- Parquet parts embed metadata: b"crv_schema_version" = f"{SCHEMA_V.major}.{SCHEMA_V.minor}@{SCHEMA_V.date}" b"crv_table_name" = tname b"crv_run_id" = run_id
- Single-writer semantics initially; no inter-process locking.
Source code in src/crv/io/write.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |