Convert an Arrow Schema to a Fory Schema. This is for compatibility with code that uses PyArrow schemas. Args: arrow_schema: A PyArrow Schema object. Returns: A Fory Schema object with the same structure. Raises: ImportError: If pyarrow is not available.
(arrow_schema)
| 255 | |
| 256 | |
| 257 | def from_arrow_schema(arrow_schema) -> Schema: |
| 258 | """Convert an Arrow Schema to a Fory Schema. |
| 259 | |
| 260 | This is for compatibility with code that uses PyArrow schemas. |
| 261 | |
| 262 | Args: |
| 263 | arrow_schema: A PyArrow Schema object. |
| 264 | |
| 265 | Returns: |
| 266 | A Fory Schema object with the same structure. |
| 267 | |
| 268 | Raises: |
| 269 | ImportError: If pyarrow is not available. |
| 270 | """ |
| 271 | try: |
| 272 | from pyarrow import types as pa_types |
| 273 | except ImportError: |
| 274 | raise ImportError("pyarrow is required for Arrow schema conversion") |
| 275 | |
| 276 | def convert_type(arrow_type) -> DataType: |
| 277 | if pa_types.is_boolean(arrow_type): |
| 278 | return boolean() |
| 279 | elif pa_types.is_int8(arrow_type): |
| 280 | return int8() |
| 281 | elif pa_types.is_int16(arrow_type): |
| 282 | return int16() |
| 283 | elif pa_types.is_int32(arrow_type): |
| 284 | return int32() |
| 285 | elif pa_types.is_int64(arrow_type): |
| 286 | return int64() |
| 287 | elif pa_types.is_float32(arrow_type): |
| 288 | return float32() |
| 289 | elif pa_types.is_float64(arrow_type): |
| 290 | return float64() |
| 291 | elif pa_types.is_string(arrow_type) or pa_types.is_large_string(arrow_type): |
| 292 | return utf8() |
| 293 | elif pa_types.is_binary(arrow_type) or pa_types.is_large_binary(arrow_type): |
| 294 | return binary() |
| 295 | elif pa_types.is_date32(arrow_type): |
| 296 | return date32() |
| 297 | elif pa_types.is_timestamp(arrow_type): |
| 298 | return timestamp() |
| 299 | elif pa_types.is_list(arrow_type) or pa_types.is_large_list(arrow_type): |
| 300 | return list_(convert_type(arrow_type.value_type)) |
| 301 | elif pa_types.is_map(arrow_type): |
| 302 | return map_(convert_type(arrow_type.key_type), convert_type(arrow_type.item_type)) |
| 303 | elif pa_types.is_struct(arrow_type): |
| 304 | fields = [] |
| 305 | for i in range(arrow_type.num_fields): |
| 306 | f = arrow_type.field(i) |
| 307 | fields.append(field(f.name, convert_type(f.type), nullable=f.nullable)) |
| 308 | return struct(fields) |
| 309 | else: |
| 310 | raise TypeError(f"Unsupported Arrow type for Fory conversion: {arrow_type}") |
| 311 | |
| 312 | fory_fields = [] |
| 313 | for i in range(len(arrow_schema)): |
| 314 | f = arrow_schema.field(i) |