Skip to content

Commit 7808121

Browse files
Fokkokevinjqliu
andauthored
Add support for write.data.path (#1611)
Relates to #1492 --------- Co-authored-by: Kevin Liu <[email protected]>
1 parent b47af2d commit 7808121

File tree

4 files changed

+58
-22
lines changed

4 files changed

+58
-22
lines changed

mkdocs/docs/configuration.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,19 @@ Iceberg tables support table properties to configure table behavior.
5454

5555
### Write options
5656

57-
| Key | Options | Default | Description |
58-
|------------------------------------------|-----------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
59-
| `write.parquet.compression-codec` | `{uncompressed,zstd,gzip,snappy}` | zstd | Sets the Parquet compression coddec. |
60-
| `write.parquet.compression-level` | Integer | null | Parquet compression level for the codec. If not set, it is up to PyIceberg |
61-
| `write.parquet.row-group-limit` | Number of rows | 1048576 | The upper bound of the number of entries within a single row group |
62-
| `write.parquet.page-size-bytes` | Size in bytes | 1MB | Set a target threshold for the approximate encoded size of data pages within a column chunk |
63-
| `write.parquet.page-row-limit` | Number of rows | 20000 | Set a target threshold for the maximum number of rows within a column chunk |
64-
| `write.parquet.dict-size-bytes` | Size in bytes | 2MB | Set the dictionary page size limit per row group |
65-
| `write.metadata.previous-versions-max` | Integer | 100 | The max number of previous version metadata files to keep before deleting after commit. |
66-
| `write.object-storage.enabled` | Boolean | True | Enables the [`ObjectStoreLocationProvider`](configuration.md#object-store-location-provider) that adds a hash component to file paths. Note: the default value of `True` differs from Iceberg's Java implementation |
67-
| `write.object-storage.partitioned-paths` | Boolean | True | Controls whether [partition values are included in file paths](configuration.md#partition-exclusion) when object storage is enabled |
68-
| `write.py-location-provider.impl` | String of form `module.ClassName` | null | Optional, [custom `LocationProvider`](configuration.md#loading-a-custom-location-provider) implementation |
57+
| Key | Options | Default | Description |
58+
|------------------------------------------|------------------------------------|----------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
59+
| `write.parquet.compression-codec` | `{uncompressed,zstd,gzip,snappy}` | zstd | Sets the Parquet compression coddec. |
60+
| `write.parquet.compression-level` | Integer | null | Parquet compression level for the codec. If not set, it is up to PyIceberg |
61+
| `write.parquet.row-group-limit` | Number of rows | 1048576 | The upper bound of the number of entries within a single row group |
62+
| `write.parquet.page-size-bytes` | Size in bytes | 1MB | Set a target threshold for the approximate encoded size of data pages within a column chunk |
63+
| `write.parquet.page-row-limit` | Number of rows | 20000 | Set a target threshold for the maximum number of rows within a column chunk |
64+
| `write.parquet.dict-size-bytes` | Size in bytes | 2MB | Set the dictionary page size limit per row group |
65+
| `write.metadata.previous-versions-max` | Integer | 100 | The max number of previous version metadata files to keep before deleting after commit. |
66+
| `write.object-storage.enabled` | Boolean | True | Enables the [`ObjectStoreLocationProvider`](configuration.md#object-store-location-provider) that adds a hash component to file paths. Note: the default value of `True` differs from Iceberg's Java implementation |
67+
| `write.object-storage.partitioned-paths` | Boolean | True | Controls whether [partition values are included in file paths](configuration.md#partition-exclusion) when object storage is enabled |
68+
| `write.py-location-provider.impl` | String of form `module.ClassName` | null | Optional, [custom `LocationProvider`](configuration.md#loading-a-custom-location-provider) implementation |
69+
| `write.data.path` | String pointing to location | `{metadata.location}/data` | Sets the location under which data is written. |
6970

7071
### Table behavior options
7172

@@ -210,8 +211,8 @@ file paths that are optimized for object storage.
210211

211212
### Simple Location Provider
212213

213-
The `SimpleLocationProvider` places a table's file names underneath a `data` directory in the table's base storage
214-
location (this is `table.metadata.location` - see the [Iceberg table specification](https://iceberg.apache.org/spec/#table-metadata)).
214+
The `SimpleLocationProvider` provides paths prefixed by `{location}/data/`, where `location` comes from the [table metadata](https://iceberg.apache.org/spec/#table-metadata-fields). This can be overridden by setting [`write.data.path` table configuration](#write-options).
215+
215216
For example, a non-partitioned table might have a data file with location:
216217

217218
```txt
@@ -239,9 +240,9 @@ When several files are stored under the same prefix, cloud object stores such as
239240
resulting in slowdowns. The `ObjectStoreLocationProvider` counteracts this by injecting deterministic hashes, in the form of binary directories,
240241
into file paths, to distribute files across a larger number of object store prefixes.
241242

242-
Paths still contain partitions just before the file name, in Hive-style, and a `data` directory beneath the table's location,
243-
in a similar manner to the [`SimpleLocationProvider`](configuration.md#simple-location-provider). For example, a table
244-
partitioned over a string column `category` might have a data file with location: (note the additional binary directories)
243+
Paths are prefixed by `{location}/data/`, where `location` comes from the [table metadata](https://iceberg.apache.org/spec/#table-metadata-fields), in a similar manner to the [`SimpleLocationProvider`](configuration.md#simple-location-provider). This can be overridden by setting [`write.data.path` table configuration](#write-options).
244+
245+
For example, a table partitioned over a string column `category` might have a data file with location: (note the additional binary directories)
245246

246247
```txt
247248
s3://bucket/ns/table/data/0101/0110/1001/10110010/category=orders/0000-0-5affc076-96a4-48f2-9cd2-d5efbc9f0c94-00001.parquet

pyiceberg/table/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ class TableProperties:
196196
WRITE_OBJECT_STORE_PARTITIONED_PATHS = "write.object-storage.partitioned-paths"
197197
WRITE_OBJECT_STORE_PARTITIONED_PATHS_DEFAULT = True
198198

199+
WRITE_DATA_PATH = "write.data.path"
200+
199201
DELETE_MODE = "write.delete.mode"
200202
DELETE_MODE_COPY_ON_WRITE = "copy-on-write"
201203
DELETE_MODE_MERGE_ON_READ = "merge-on-read"

pyiceberg/table/locations.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ class LocationProvider(ABC):
4040
table_location: str
4141
table_properties: Properties
4242

43+
data_path: str
44+
4345
def __init__(self, table_location: str, table_properties: Properties):
4446
self.table_location = table_location
4547
self.table_properties = table_properties
4648

49+
if path := table_properties.get(TableProperties.WRITE_DATA_PATH):
50+
self.data_path = path.rstrip("/")
51+
else:
52+
self.data_path = f"{self.table_location.rstrip('/')}/data"
53+
4754
@abstractmethod
4855
def new_data_location(self, data_file_name: str, partition_key: Optional[PartitionKey] = None) -> str:
4956
"""Return a fully-qualified data file location for the given filename.
@@ -62,8 +69,11 @@ def __init__(self, table_location: str, table_properties: Properties):
6269
super().__init__(table_location, table_properties)
6370

6471
def new_data_location(self, data_file_name: str, partition_key: Optional[PartitionKey] = None) -> str:
65-
prefix = f"{self.table_location}/data"
66-
return f"{prefix}/{partition_key.to_path()}/{data_file_name}" if partition_key else f"{prefix}/{data_file_name}"
72+
return (
73+
f"{self.data_path}/{partition_key.to_path()}/{data_file_name}"
74+
if partition_key
75+
else f"{self.data_path}/{data_file_name}"
76+
)
6777

6878

6979
class ObjectStoreLocationProvider(LocationProvider):
@@ -85,13 +95,12 @@ def new_data_location(self, data_file_name: str, partition_key: Optional[Partiti
8595
if self._include_partition_paths and partition_key:
8696
return self.new_data_location(f"{partition_key.to_path()}/{data_file_name}")
8797

88-
prefix = f"{self.table_location}/data"
8998
hashed_path = self._compute_hash(data_file_name)
9099

91100
return (
92-
f"{prefix}/{hashed_path}/{data_file_name}"
101+
f"{self.data_path}/{hashed_path}/{data_file_name}"
93102
if self._include_partition_paths
94-
else f"{prefix}/{hashed_path}-{data_file_name}"
103+
else f"{self.data_path}/{hashed_path}-{data_file_name}"
95104
)
96105

97106
@staticmethod

tests/table/test_locations.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from pyiceberg.partitioning import PartitionField, PartitionFieldValue, PartitionKey, PartitionSpec
2222
from pyiceberg.schema import Schema
23+
from pyiceberg.table import TableProperties
2324
from pyiceberg.table.locations import LocationProvider, load_location_provider
2425
from pyiceberg.transforms import IdentityTransform
2526
from pyiceberg.typedef import EMPTY_DICT
@@ -133,3 +134,26 @@ def test_hash_injection(data_file_name: str, expected_hash: str) -> None:
133134
provider = load_location_provider(table_location="table_location", table_properties=EMPTY_DICT)
134135

135136
assert provider.new_data_location(data_file_name) == f"table_location/data/{expected_hash}/{data_file_name}"
137+
138+
139+
def test_object_location_provider_write_data_path() -> None:
140+
provider = load_location_provider(
141+
table_location="s3://table-location/table",
142+
table_properties={TableProperties.WRITE_DATA_PATH: "s3://table-location/custom/data/path"},
143+
)
144+
145+
assert (
146+
provider.new_data_location("file.parquet") == "s3://table-location/custom/data/path/0010/1111/0101/11011101/file.parquet"
147+
)
148+
149+
150+
def test_simple_location_provider_write_data_path() -> None:
151+
provider = load_location_provider(
152+
table_location="table_location",
153+
table_properties={
154+
TableProperties.WRITE_DATA_PATH: "s3://table-location/custom/data/path",
155+
"write.object-storage.enabled": "false",
156+
},
157+
)
158+
159+
assert provider.new_data_location("file.parquet") == "s3://table-location/custom/data/path/file.parquet"

0 commit comments

Comments
 (0)