Convert a Fory Schema to an Arrow Schema. This is for compatibility with ArrowWriter which requires Arrow schemas. Args: fory_schema: A Fory Schema object. Returns: An Arrow Schema object with the same structure. Raises: ImportError: If pyarrow is not avai
(fory_schema: Schema)
| 317 | |
| 318 | |
| 319 | def to_arrow_schema(fory_schema: Schema): |
| 320 | """Convert a Fory Schema to an Arrow Schema. |
| 321 | |
| 322 | This is for compatibility with ArrowWriter which requires Arrow schemas. |
| 323 | |
| 324 | Args: |
| 325 | fory_schema: A Fory Schema object. |
| 326 | |
| 327 | Returns: |
| 328 | An Arrow Schema object with the same structure. |
| 329 | |
| 330 | Raises: |
| 331 | ImportError: If pyarrow is not available. |
| 332 | """ |
| 333 | try: |
| 334 | import pyarrow as pa |
| 335 | except ImportError: |
| 336 | raise ImportError("pyarrow is required for Arrow schema conversion") |
| 337 | |
| 338 | def convert_type(fory_type: DataType): |
| 339 | type_id = fory_type.id |
| 340 | if type_id == TypeId.BOOL: |
| 341 | return pa.bool_() |
| 342 | elif type_id == TypeId.INT8: |
| 343 | return pa.int8() |
| 344 | elif type_id == TypeId.INT16: |
| 345 | return pa.int16() |
| 346 | elif type_id == TypeId.INT32: |
| 347 | return pa.int32() |
| 348 | elif type_id == TypeId.INT64: |
| 349 | return pa.int64() |
| 350 | elif type_id == TypeId.FLOAT32: |
| 351 | return pa.float32() |
| 352 | elif type_id == TypeId.FLOAT64: |
| 353 | return pa.float64() |
| 354 | elif type_id == TypeId.STRING: |
| 355 | return pa.string() |
| 356 | elif type_id == TypeId.BINARY: |
| 357 | return pa.binary() |
| 358 | elif type_id == TypeId.DATE: |
| 359 | return pa.date32() |
| 360 | elif type_id == TypeId.TIMESTAMP: |
| 361 | return pa.timestamp("us") |
| 362 | elif type_id == TypeId.LIST: |
| 363 | return pa.list_(convert_type(fory_type.value_type)) |
| 364 | elif type_id == TypeId.MAP: |
| 365 | return pa.map_(convert_type(fory_type.key_type), convert_type(fory_type.item_type)) |
| 366 | elif type_id == TypeId.STRUCT: |
| 367 | fields = [] |
| 368 | for i in range(fory_type.num_fields): |
| 369 | f = fory_type.field(i) |
| 370 | fields.append(pa.field(f.name, convert_type(f.type), nullable=f.nullable)) |
| 371 | return pa.struct(fields) |
| 372 | else: |
| 373 | raise TypeError(f"Unsupported Fory type for Arrow conversion: {fory_type}") |
| 374 | |
| 375 | arrow_fields = [] |
| 376 | for i in range(fory_schema.num_fields): |