|
31 | 31 | IdentityTransform, |
32 | 32 | MonthTransform, |
33 | 33 | TruncateTransform, |
| 34 | + VoidTransform, |
34 | 35 | YearTransform, |
35 | 36 | ) |
36 | 37 | from pyiceberg.typedef import Record |
@@ -194,6 +195,89 @@ def test_partition_spec_to_path_dropped_source_id() -> None: |
194 | 195 | assert spec.partition_to_path(record, schema) == "my%23str%25bucket=my%2Bstr/other+str%2Bbucket=%28+%29/my%21int%3Abucket=10" |
195 | 196 |
|
196 | 197 |
|
| 198 | +def test_partition_from_path_identity() -> None: |
| 199 | + schema = Schema( |
| 200 | + NestedField(field_id=1, name="foo", field_type=StringType(), required=False), |
| 201 | + NestedField(field_id=2, name="baz", field_type=IntegerType(), required=True), |
| 202 | + ) |
| 203 | + spec = PartitionSpec( |
| 204 | + PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="foo"), |
| 205 | + PartitionField(source_id=2, field_id=1001, transform=IdentityTransform(), name="baz"), |
| 206 | + spec_id=0, |
| 207 | + ) |
| 208 | + |
| 209 | + assert spec.partition_from_path("s3://bucket/table/data/foo=hello/baz=123/00000-0.parquet", schema) == Record("hello", 123) |
| 210 | + |
| 211 | + |
| 212 | +def test_partition_from_path_unpartitioned() -> None: |
| 213 | + schema = Schema(NestedField(field_id=1, name="foo", field_type=StringType(), required=False)) |
| 214 | + assert UNPARTITIONED_PARTITION_SPEC.partition_from_path("s3://bucket/table/data/00000-0.parquet", schema) is None |
| 215 | + |
| 216 | + |
| 217 | +def test_partition_from_path_not_hive_style() -> None: |
| 218 | + schema = Schema(NestedField(field_id=1, name="foo", field_type=StringType(), required=False)) |
| 219 | + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="foo"), spec_id=0) |
| 220 | + |
| 221 | + assert spec.partition_from_path("s3://bucket/table/data/00000-0.parquet", schema) is None |
| 222 | + |
| 223 | + |
| 224 | +def test_partition_from_path_field_name_mismatch() -> None: |
| 225 | + schema = Schema(NestedField(field_id=1, name="foo", field_type=StringType(), required=False)) |
| 226 | + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="foo"), spec_id=0) |
| 227 | + |
| 228 | + assert spec.partition_from_path("s3://bucket/table/data/wrong=hello/00000-0.parquet", schema) is None |
| 229 | + |
| 230 | + |
| 231 | +def test_partition_from_path_bucket_transform() -> None: |
| 232 | + schema = Schema(NestedField(field_id=1, name="int", field_type=IntegerType(), required=True)) |
| 233 | + spec = PartitionSpec( |
| 234 | + PartitionField(source_id=1, field_id=1000, transform=BucketTransform(num_buckets=3), name="int_bucket"), spec_id=0 |
| 235 | + ) |
| 236 | + |
| 237 | + assert spec.partition_from_path("s3://bucket/table/data/int_bucket=1/00000-0.parquet", schema) == Record(1) |
| 238 | + |
| 239 | + |
| 240 | +def test_partition_from_path_truncate_transform() -> None: |
| 241 | + schema = Schema(NestedField(field_id=1, name="str", field_type=StringType(), required=False)) |
| 242 | + spec = PartitionSpec( |
| 243 | + PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=3), name="str_trunc"), spec_id=0 |
| 244 | + ) |
| 245 | + |
| 246 | + assert spec.partition_from_path("s3://bucket/table/data/str_trunc=abc/00000-0.parquet", schema) == Record("abc") |
| 247 | + |
| 248 | + |
| 249 | +def test_partition_from_path_truncate_binary_transform_unsupported() -> None: |
| 250 | + schema = Schema(NestedField(field_id=1, name="bin", field_type=BinaryType(), required=False)) |
| 251 | + spec = PartitionSpec( |
| 252 | + PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=3), name="bin_trunc"), spec_id=0 |
| 253 | + ) |
| 254 | + |
| 255 | + # base64-encoded path value, not decodable back to bytes |
| 256 | + assert spec.partition_from_path("s3://bucket/table/data/bin_trunc=YWJj/00000-0.parquet", schema) is None |
| 257 | + |
| 258 | + |
| 259 | +def test_partition_from_path_time_transform_unsupported() -> None: |
| 260 | + schema = Schema(NestedField(field_id=1, name="date", field_type=DateType(), required=False)) |
| 261 | + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=MonthTransform(), name="date_month"), spec_id=0) |
| 262 | + |
| 263 | + # calendar string in path, not the raw int |
| 264 | + assert spec.partition_from_path("s3://bucket/table/data/date_month=2024-03/00000-0.parquet", schema) is None |
| 265 | + |
| 266 | + |
| 267 | +def test_partition_from_path_void_transform_unsupported() -> None: |
| 268 | + schema = Schema(NestedField(field_id=1, name="foo", field_type=StringType(), required=False)) |
| 269 | + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=VoidTransform(), name="foo_null"), spec_id=0) |
| 270 | + |
| 271 | + assert spec.partition_from_path("s3://bucket/table/data/foo_null=null/00000-0.parquet", schema) is None |
| 272 | + |
| 273 | + |
| 274 | +def test_partition_from_path_url_encoded_value() -> None: |
| 275 | + schema = Schema(NestedField(field_id=1, name="foo", field_type=StringType(), required=False)) |
| 276 | + spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="foo"), spec_id=0) |
| 277 | + |
| 278 | + assert spec.partition_from_path("s3://bucket/table/data/foo=a%2Bb/00000-0.parquet", schema) == Record("a+b") |
| 279 | + |
| 280 | + |
197 | 281 | def test_partition_type(table_schema_simple: Schema) -> None: |
198 | 282 | spec = PartitionSpec( |
199 | 283 | PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=19), name="str_truncate"), |
|
0 commit comments