forked from apache/iceberg-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sorting.py
More file actions
171 lines (129 loc) · 6.47 KB
/
test_sorting.py
File metadata and controls
171 lines (129 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint:disable=redefined-outer-name,eval-used
import json
from typing import Any
import pytest
from pyiceberg.exceptions import ValidationError
from pyiceberg.schema import Schema
from pyiceberg.table.metadata import TableMetadataUtil
from pyiceberg.table.sorting import (
UNSORTED_SORT_ORDER,
NullOrder,
SortDirection,
SortField,
SortOrder,
)
from pyiceberg.transforms import BucketTransform, IdentityTransform, VoidTransform, YearTransform
from pyiceberg.types import IntegerType, NestedField, StructType
@pytest.fixture
def sort_order() -> SortOrder:
return SortOrder(
SortField(source_id=19, transform=IdentityTransform(), null_order=NullOrder.NULLS_FIRST),
SortField(source_id=25, transform=BucketTransform(4), direction=SortDirection.DESC),
SortField(source_id=22, transform=VoidTransform(), direction=SortDirection.ASC),
order_id=22,
)
def test_serialize_sort_order_default() -> None:
assert (
SortOrder(SortField(source_id=19)).model_dump_json()
== '{"order-id":1,"fields":[{"source-id":19,"transform":"identity","direction":"asc","null-order":"nulls-first"}]}'
)
def test_serialize_sort_order_unsorted() -> None:
assert UNSORTED_SORT_ORDER.model_dump_json() == '{"order-id":0,"fields":[]}'
def test_serialize_sort_order(sort_order: SortOrder) -> None:
expected = (
'{"order-id":22,"fields":['
'{"source-id":19,"transform":"identity","direction":"asc","null-order":"nulls-first"},'
'{"source-id":25,"transform":"bucket[4]","direction":"desc","null-order":"nulls-last"},'
'{"source-id":22,"transform":"void","direction":"asc","null-order":"nulls-first"}]}'
)
assert sort_order.model_dump_json() == expected
def test_deserialize_sort_order(sort_order: SortOrder) -> None:
payload = (
'{"order-id": 22, "fields": ['
'{"source-id": 19, "transform": "identity", "direction": "asc", "null-order": "nulls-first"}, '
'{"source-id": 25, "transform": "bucket[4]", "direction": "desc", "null-order": "nulls-last"}, '
'{"source-id": 22, "transform": "void", "direction": "asc", "null-order": "nulls-first"}]}'
)
assert SortOrder.model_validate_json(payload) == sort_order
def test_sorting_schema(example_table_metadata_v2: dict[str, Any]) -> None:
table_metadata = TableMetadataUtil.parse_raw(json.dumps(example_table_metadata_v2))
assert table_metadata.sort_orders == [
SortOrder(
SortField(2, IdentityTransform(), SortDirection.ASC, null_order=NullOrder.NULLS_FIRST),
SortField(
3,
BucketTransform(4),
direction=SortDirection.DESC,
null_order=NullOrder.NULLS_LAST,
),
order_id=3,
)
]
def test_sorting_to_string(sort_order: SortOrder) -> None:
expected = """[
19 ASC NULLS FIRST
bucket[4](25) DESC NULLS LAST
void(22) ASC NULLS FIRST
]"""
assert str(sort_order) == expected
def test_sorting_to_repr(sort_order: SortOrder) -> None:
expected = (
"SortOrder("
"SortField(source_id=19, transform=IdentityTransform(), "
"direction=SortDirection.ASC, null_order=NullOrder.NULLS_FIRST), "
"SortField(source_id=25, transform=BucketTransform(num_buckets=4), "
"direction=SortDirection.DESC, null_order=NullOrder.NULLS_LAST), "
"SortField(source_id=22, transform=VoidTransform(), "
"direction=SortDirection.ASC, null_order=NullOrder.NULLS_FIRST), "
"order_id=22)"
)
assert repr(sort_order) == expected
def test_unsorting_to_repr() -> None:
expected = """SortOrder(order_id=0)"""
assert repr(UNSORTED_SORT_ORDER) == expected
def test_sorting_repr(sort_order: SortOrder) -> None:
"""To make sure that the repr converts back to the original object"""
assert sort_order == eval(repr(sort_order))
def test_serialize_sort_field_v2() -> None:
expected = SortField(source_id=19, transform=IdentityTransform(), null_order=NullOrder.NULLS_FIRST)
payload = '{"source-id":19,"transform":"identity","direction":"asc","null-order":"nulls-first"}'
assert SortField.model_validate_json(payload) == expected
def test_serialize_sort_field_v3() -> None:
expected = SortField(source_id=19, transform=IdentityTransform(), null_order=NullOrder.NULLS_FIRST)
payload = '{"source-ids":[19],"transform":"identity","direction":"asc","null-order":"nulls-first"}'
assert SortField.model_validate_json(payload) == expected
def test_incompatible_source_column_not_found(sort_order: SortOrder) -> None:
schema = Schema(NestedField(1, "foo", IntegerType()), NestedField(2, "bar", IntegerType()))
with pytest.raises(ValidationError) as exc:
sort_order.check_compatible(schema)
assert "Cannot find source column for sort field: 19 ASC NULLS FIRST" in str(exc.value)
def test_incompatible_non_primitive_type() -> None:
schema = Schema(NestedField(1, "foo", StructType()), NestedField(2, "bar", IntegerType()))
sort_order = SortOrder(SortField(source_id=1, transform=IdentityTransform(), null_order=NullOrder.NULLS_FIRST))
with pytest.raises(ValidationError) as exc:
sort_order.check_compatible(schema)
assert "Cannot sort by non-primitive source field: 1: foo: optional struct<>" in str(exc.value)
def test_incompatible_transform_source_type() -> None:
schema = Schema(NestedField(1, "foo", IntegerType()), NestedField(2, "bar", IntegerType()))
sort_order = SortOrder(
SortField(source_id=1, transform=YearTransform(), null_order=NullOrder.NULLS_FIRST),
)
with pytest.raises(ValidationError) as exc:
sort_order.check_compatible(schema)
assert "Invalid source field foo with type int for transform: year" in str(exc.value)