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
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 1525 1526 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 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 |
|
_limit = limit
instance-attribute
¶
Scan the Iceberg Table and create an Arrow construct.
Attributes:
Name | Type | Description |
---|---|---|
_table_metadata |
Current table metadata of the Iceberg table |
|
_io |
PyIceberg FileIO implementation from which to fetch the io properties |
|
_projected_schema |
Iceberg Schema to project onto the data files |
|
_bound_row_filter |
Schema bound row expression to filter the data with |
|
_case_sensitive |
Case sensitivity when looking up column names |
|
_limit |
Limit the number of records. |
_projected_field_ids
property
¶
Set of field IDs that should be projected from the data files.
_use_large_types
property
¶
Whether to represent data as large arrow types.
Defaults to True.
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
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 |
|
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
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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
|
__len__()
¶
_file_info()
¶
Retrieve a pyarrow.fs.FileInfo object for the location.
Raises:
Type | Description |
---|---|
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
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
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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
|
__getstate__()
¶
Create a dictionary of the PyArrowFileIO fields used when pickling.
__setstate__(state)
¶
_initialize_fs(scheme, netloc=None)
¶
Initialize FileSystem for different scheme.
Source code in pyiceberg/io/pyarrow.py
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
¶
UnsupportedPyArrowTypeException
¶
Bases: Exception
Cannot convert PyArrow type to corresponding Iceberg type.
Source code in pyiceberg/io/pyarrow.py
_ConvertToIceberg
¶
Bases: PyArrowSchemaVisitor[Union[IcebergType, Schema]]
Converts PyArrowSchema to Iceberg Schema. Applies the IDs from name_mapping if provided.
Source code in pyiceberg/io/pyarrow.py
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 |
|
_ConvertToIcebergWithoutIDs
¶
Bases: _ConvertToIceberg
Converts PyArrowSchema to Iceberg Schema with all -1 ids.
The schema generated through this visitor should always be
used in conjunction with new_table_metadata
function to
assign new field ids in order. This is currently used only
when creating an Iceberg Schema from a PyArrow schema when
creating a new Iceberg table.
Source code in pyiceberg/io/pyarrow.py
_NullNaNUnmentionedTermsCollector
¶
Bases: BoundBooleanExpressionVisitor[None]
Source code in pyiceberg/io/pyarrow.py
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
|
_handle_explicit_is_nan_or_not(term)
¶
Handle the predicate case where either is_nan or is_not_nan is included.
Source code in pyiceberg/io/pyarrow.py
_handle_explicit_is_null_or_not(term)
¶
Handle the predicate case where either is_null or is_not_null is included.
Source code in pyiceberg/io/pyarrow.py
_handle_nan_unmentioned(term)
¶
Handle the predicate case where neither is_nan or is_not_nan is included.
_handle_null_unmentioned(term)
¶
Handle the predicate case where neither is_null or is_not_null is included.
collect(expr)
¶
Collect the bound references categorized by having at least one is_null or is_not_null in the expr and the remaining.
_check_pyarrow_schema_compatible(requested_schema, provided_schema, downcast_ns_timestamp_to_us=False)
¶
Check if the requested_schema
is compatible with provided_schema
.
Two schemas are considered compatible when they are equal in terms of the Iceberg Schema type.
Raises:
Type | Description |
---|---|
ValueError
|
If the schemas are not compatible. |
Source code in pyiceberg/io/pyarrow.py
_dataframe_to_data_files(table_metadata, df, io, write_uuid=None, counter=None)
¶
Convert a PyArrow table into a DataFile.
Returns:
Type | Description |
---|---|
Iterable[DataFile]
|
An iterable that supplies datafiles that represent the table. |
Source code in pyiceberg/io/pyarrow.py
_determine_partitions(spec, schema, arrow_table)
¶
Based on the iceberg table partition spec, filter the arrow table into partitions with their keys.
Example: Input: An arrow table with partition key of ['n_legs', 'year'] and with data of {'year': [2020, 2022, 2022, 2021, 2022, 2022, 2022, 2019, 2021], 'n_legs': [2, 2, 2, 4, 4, 4, 4, 5, 100], 'animal': ["Flamingo", "Parrot", "Parrot", "Dog", "Horse", "Horse", "Horse","Brittle stars", "Centipede"]}. The algorithm: - We determine the set of unique partition keys - Then we produce a set of partitions by filtering on each of the combinations - We combine the chunks to create a copy to avoid GIL congestion on the original table
Source code in pyiceberg/io/pyarrow.py
_expression_to_complementary_pyarrow(expr)
¶
Complementary filter conversion function of expression_to_pyarrow.
Could not use expression_to_pyarrow(Not(expr)) to achieve this complementary effect because ~ in pyarrow.compute.Expression does not handle null.
Source code in pyiceberg/io/pyarrow.py
_get_column_projection_values(file, projected_schema, partition_spec, file_project_field_ids)
¶
Apply Column Projection rules to File Schema.
Source code in pyiceberg/io/pyarrow.py
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
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 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 |
|
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
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. |