pyarrow
FileIO implementation for reading and writing table files that uses pyarrow.fs.
This file contains a FileIO implementation that relies on the filesystem interface provided
by PyArrow. It relies on PyArrow's from_uri
method that infers the correct filesystem
type to use. Theoretically, this allows the supported storage types to grow naturally
with the pyarrow library.
ArrowScan
¶
Source code in pyiceberg/io/pyarrow.py
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 |
|
to_record_batches(tasks)
¶
Scan the Iceberg table and return an Iterator[pa.RecordBatch].
Returns an Iterator of pa.RecordBatch with data from the Iceberg table by resolving the right columns that match the current table schema. Only data that matches the provided row_filter expression is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tasks
|
Iterable[FileScanTask]
|
FileScanTasks representing the data files and delete files to read from. |
required |
Returns:
Type | Description |
---|---|
Iterator[RecordBatch]
|
An Iterator of PyArrow RecordBatches. |
Iterator[RecordBatch]
|
Total number of rows will be capped if specified. |
Raises:
Type | Description |
---|---|
ResolveError
|
When a required field cannot be found in the file |
ValueError
|
When a field type in the file cannot be projected to the schema type |
Source code in pyiceberg/io/pyarrow.py
to_table(tasks)
¶
Scan the Iceberg table and return a pa.Table.
Returns a pa.Table with data from the Iceberg table by resolving the right columns that match the current table schema. Only data that matches the provided row_filter expression is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tasks
|
Iterable[FileScanTask]
|
FileScanTasks representing the data files and delete files to read from. |
required |
Returns:
Type | Description |
---|---|
Table
|
A PyArrow table. Total number of rows will be capped if specified. |
Raises:
Type | Description |
---|---|
ResolveError
|
When a required field cannot be found in the file |
ValueError
|
When a field type in the file cannot be projected to the schema type |
Source code in pyiceberg/io/pyarrow.py
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 |
|
PyArrowFile
¶
Bases: InputFile
, OutputFile
A combined InputFile and OutputFile implementation that uses a pyarrow filesystem to generate pyarrow.lib.NativeFile instances.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
location
|
str
|
A URI or a path to a local file. |
required |
Attributes:
Name | Type | Description |
---|---|---|
location(str) |
The URI or path to a local file for a PyArrowFile instance. |
Examples:
>>> from pyiceberg.io.pyarrow import PyArrowFile
>>> # input_file = PyArrowFile("s3://foo/bar.txt")
>>> # Read the contents of the PyArrowFile instance
>>> # Make sure that you have permissions to read/write
>>> # file_content = input_file.open().read()
>>> # output_file = PyArrowFile("s3://baz/qux.txt")
>>> # Write bytes to a file
>>> # Make sure that you have permissions to read/write
>>> # output_file.create().write(b'foobytes')
Source code in pyiceberg/io/pyarrow.py
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 |
|
__len__()
¶
create(overwrite=False)
¶
Create a writable pyarrow.lib.NativeFile for this PyArrowFile's location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
overwrite
|
bool
|
Whether to overwrite the file if it already exists. |
False
|
Returns:
Type | Description |
---|---|
OutputStream
|
pyarrow.lib.NativeFile: A NativeFile instance for the file located at self.location. |
Raises:
Type | Description |
---|---|
FileExistsError
|
If the file already exists at |
Note
This retrieves a pyarrow NativeFile by opening an output stream. If overwrite is set to False, a check is first performed to verify that the file does not exist. This is not thread-safe and a possibility does exist that the file can be created by a concurrent process after the existence check yet before the output stream is created. In such a case, the default pyarrow behavior will truncate the contents of the existing file when opening the output stream.
Source code in pyiceberg/io/pyarrow.py
exists()
¶
open(seekable=True)
¶
Open the location using a PyArrow FileSystem inferred from the location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seekable
|
bool
|
If the stream should support seek, or if it is consumed sequential. |
True
|
Returns:
Type | Description |
---|---|
InputStream
|
pyarrow.lib.NativeFile: A NativeFile instance for the file located at |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the file at self.location does not exist. |
PermissionError
|
If the file at self.location cannot be accessed due to a permission error such as an AWS error code 15. |
Source code in pyiceberg/io/pyarrow.py
to_input_file()
¶
Return a new PyArrowFile for the location of an existing PyArrowFile instance.
This method is included to abide by the OutputFile abstract base class. Since this implementation uses a single PyArrowFile class (as opposed to separate InputFile and OutputFile implementations), this method effectively returns a copy of the same instance.
Source code in pyiceberg/io/pyarrow.py
PyArrowFileIO
¶
Bases: FileIO
Source code in pyiceberg/io/pyarrow.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
|
__getstate__()
¶
Create a dictionary of the PyArrowFileIO fields used when pickling.
__setstate__(state)
¶
delete(location)
¶
Delete the file at the given location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
location
|
Union[str, InputFile, OutputFile]
|
The URI to the file--if an InputFile instance or an OutputFile instance is provided, the location attribute for that instance is used as the location to delete. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
When the file at the provided location does not exist. |
PermissionError
|
If the file at the provided location cannot be accessed due to a permission error such as an AWS error code 15. |
Source code in pyiceberg/io/pyarrow.py
new_input(location)
¶
Get a PyArrowFile instance to read bytes from the file at the given location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
location
|
str
|
A URI or a path to a local file. |
required |
Returns:
Name | Type | Description |
---|---|---|
PyArrowFile |
PyArrowFile
|
A PyArrowFile instance for the given location. |
Source code in pyiceberg/io/pyarrow.py
new_output(location)
¶
Get a PyArrowFile instance to write bytes to the file at the given location.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
location
|
str
|
A URI or a path to a local file. |
required |
Returns:
Name | Type | Description |
---|---|---|
PyArrowFile |
PyArrowFile
|
A PyArrowFile instance for the given location. |
Source code in pyiceberg/io/pyarrow.py
parse_location(location)
staticmethod
¶
Return the path without the scheme.
Source code in pyiceberg/io/pyarrow.py
PyArrowSchemaVisitor
¶
Bases: Generic[T]
, ABC
Source code in pyiceberg/io/pyarrow.py
after_field(field)
¶
after_list_element(element)
¶
Override this method to perform an action immediately after visiting an element within a ListType.
after_map_key(key)
¶
after_map_value(value)
¶
before_field(field)
¶
before_list_element(element)
¶
Override this method to perform an action immediately before visiting an element within a ListType.
before_map_key(key)
¶
before_map_value(value)
¶
field(field, field_result)
abstractmethod
¶
list(list_type, element_result)
abstractmethod
¶
map(map_type, key_result, value_result)
abstractmethod
¶
primitive(primitive)
abstractmethod
¶
schema(schema, struct_result)
abstractmethod
¶
compute_statistics_plan(schema, table_properties)
¶
Compute the statistics plan for all columns.
The resulting list is assumed to have the same length and same order as the columns in the pyarrow table. This allows the list to map from the column index to the Iceberg column ID. For each element, the desired metrics collection that was provided by the user in the configuration is computed and then adjusted according to the data type of the column. For nested columns the minimum and maximum values are not computed. And truncation is only applied to text of binary strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_properties
|
from pyiceberg.table.metadata.TableMetadata
|
The Iceberg table metadata properties. They are required to compute the mapping of column position to iceberg schema type id. It's also used to set the mode for column metrics collection |
required |
Source code in pyiceberg/io/pyarrow.py
data_file_statistics_from_parquet_metadata(parquet_metadata, stats_columns, parquet_column_mapping)
¶
Compute and return DataFileStatistics that includes the following.
- record_count
- column_sizes
- value_counts
- null_value_counts
- nan_value_counts
- column_aggregates
- split_offsets
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parquet_metadata
|
FileMetaData
|
A pyarrow metadata object. |
required |
stats_columns
|
Dict[int, StatisticsCollector]
|
The statistics gathering plan. It is required to set the mode for column metrics collection |
required |
parquet_column_mapping
|
Dict[str, int]
|
The mapping of the parquet file name to the field ID |
required |
Source code in pyiceberg/io/pyarrow.py
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 |
|
parquet_path_to_id_mapping(schema)
¶
Compute the mapping of parquet column path to Iceberg ID.
For each column, the parquet file metadata has a path_in_schema attribute that follows a specific naming scheme for nested columnds. This function computes a mapping of the full paths to the corresponding Iceberg IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Schema
|
The current table schema. |
required |
Source code in pyiceberg/io/pyarrow.py
project_batches(tasks, table_metadata, io, row_filter, projected_schema, case_sensitive=True, limit=None)
¶
Resolve the right columns based on the identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tasks
|
Iterable[FileScanTask]
|
A URI or a path to a local file. |
required |
table_metadata
|
TableMetadata
|
The table metadata of the table that's being queried |
required |
io
|
FileIO
|
A FileIO to open streams to the object store |
required |
row_filter
|
BooleanExpression
|
The expression for filtering rows. |
required |
projected_schema
|
Schema
|
The output schema. |
required |
case_sensitive
|
bool
|
Case sensitivity when looking up column names. |
True
|
limit
|
Optional[int]
|
Limit the number of records. |
None
|
Raises:
Type | Description |
---|---|
ResolveError
|
When an incompatible query is done. |
Source code in pyiceberg/io/pyarrow.py
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 |
|
project_table(tasks, table_metadata, io, row_filter, projected_schema, case_sensitive=True, limit=None)
¶
Resolve the right columns based on the identifier.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tasks
|
Iterable[FileScanTask]
|
A URI or a path to a local file. |
required |
table_metadata
|
TableMetadata
|
The table metadata of the table that's being queried |
required |
io
|
FileIO
|
A FileIO to open streams to the object store |
required |
row_filter
|
BooleanExpression
|
The expression for filtering rows. |
required |
projected_schema
|
Schema
|
The output schema. |
required |
case_sensitive
|
bool
|
Case sensitivity when looking up column names. |
True
|
limit
|
Optional[int]
|
Limit the number of records. |
None
|
Raises:
Type | Description |
---|---|
ResolveError
|
When an incompatible query is done. |
Source code in pyiceberg/io/pyarrow.py
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 |
|
visit_pyarrow(obj, visitor)
¶
Apply a pyarrow schema visitor to any point within a schema.
The function traverses the schema in post-order fashion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Union[DataType, Schema]
|
An instance of a Schema or an IcebergType. |
required |
visitor
|
PyArrowSchemaVisitor[T]
|
An instance of an implementation of the generic PyarrowSchemaVisitor base class. |
required |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If attempting to visit an unrecognized object type. |