cdm_data_loaders.converters.emitters.pyarrow ¶
Emit PyArrow schema objects from immutable typed schema nodes.
PyArrowEmitterError ¶
Bases: ConversionError
A typed schema cannot be represented by the configured Arrow policy.
PyArrowEmitter
pydantic-model
¶
Bases: _TabularEmitterBase[DataType]
Convert typed schema nodes into PyArrow schemas and empty tables.
Fields:
-
treat_unknown_as_string(bool) -
emit_unions_as_json(bool) -
extra_metadata_keywords(ExtraMetadataKeywords) -
max_nesting(PositiveInt) -
format_map(frozendict[str, DataType])
Validators:
-
_merge_with_builtin_format_map→format_map
Source code in src/cdm_data_loaders/converters/emitters/pyarrow.py
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
emit ¶
Emit a table schema, requiring the root to be a struct.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
SchemaDocument
|
the schema document to convert |
required |
Returns:
| Type | Description |
|---|---|
pa.Schema
|
the Arrow schema |
Raises:
| Type | Description |
|---|---|
PyArrowEmitterError
|
if the root does not resolve to a struct |
Source code in src/cdm_data_loaders/converters/emitters/pyarrow.py
emit_table ¶
Emit an empty table with the document's schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
SchemaDocument
|
the schema document to convert |
required |
Returns:
| Type | Description |
|---|---|
pa.Table
|
a zero-row table |
Raises:
| Type | Description |
|---|---|
PyArrowEmitterError
|
if the root does not resolve to a struct |
Source code in src/cdm_data_loaders/converters/emitters/pyarrow.py
emit_python ¶
emit_python(
document: SchemaDocument | Mapping[str, SchemaDocument],
*,
docstring: str | None = None
) -> str
Render a static Python module defining PyArrow struct components and schemas.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
SchemaDocument | Mapping[str, SchemaDocument]
|
A single |
required |
docstring
|
str | None
|
Optional module docstring header. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Formatted Python source code defining |
Raises:
| Type | Description |
|---|---|
PyArrowEmitterError
|
If any document root does not resolve to a struct. |
Source code in src/cdm_data_loaders/converters/emitters/pyarrow.py
emit_python_file ¶
emit_python_file(
document: SchemaDocument | Mapping[str, SchemaDocument],
output: str | Path,
*,
docstring: str | None = None
) -> Path
Render and write a static Python schema module to output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
SchemaDocument | Mapping[str, SchemaDocument]
|
A single |
required |
output
|
str | Path
|
Destination file path for the generated Python module. |
required |
docstring
|
str | None
|
Optional module docstring header. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Resolved output |
Raises:
| Type | Description |
|---|---|
PyArrowEmitterError
|
If any document root does not resolve to a struct. |
Source code in src/cdm_data_loaders/converters/emitters/pyarrow.py
build_metadata ¶
build_metadata(
node: TypedNode,
ctx: ConversionContext,
field: Field | None = None,
) -> dict[str, str]
Select field metadata and encode it as Arrow's string key/value pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
TypedNode
|
the node whose constraints, annotations and extensions are searched |
required |
ctx
|
ConversionContext
|
draft-specific metadata context |
required |
field
|
Field | None
|
the owning field, whose annotations and extensions take precedence |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
|
Source code in src/cdm_data_loaders/converters/emitters/pyarrow.py
merge_format_map ¶
Validate format overrides and merge them into the built-in mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
a mapping from JSON Schema format names to PyArrow types |
required |
Returns:
| Type | Description |
|---|---|
object
|
the merged mapping, or |
Raises:
| Type | Description |
|---|---|
ValueError
|
if a key is not a string or a value is not a PyArrow |
Source code in src/cdm_data_loaders/converters/emitters/pyarrow.py
assemble_pyarrow_schema ¶
assemble_pyarrow_schema(
component: StructType | Sequence[Field],
*,
metadata: Mapping[str, str | bytes] | None = None
) -> pa.Schema
Assemble a PyArrow table schema from a pa.StructType component or sequence of pa.Fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component
|
StructType | Sequence[Field]
|
struct component or sequence of PyArrow fields |
required |
metadata
|
Mapping[str, str | bytes] | None
|
optional schema-level metadata |
None
|
Returns:
| Type | Description |
|---|---|
pa.Schema
|
assembled PyArrow schema |
Raises:
| Type | Description |
|---|---|
PyArrowEmitterError
|
if |
Source code in src/cdm_data_loaders/converters/emitters/pyarrow.py
infer_type_from_enum ¶
Map homogeneous JSON enum values to Arrow scalar types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
list[object]
|
the enum members |
required |
Returns:
| Type | Description |
|---|---|
pa.DataType
|
|