Skip to content

Commit 955ed13

Browse files
committed
Validate range partition split ordering
1 parent 0fd0a4e commit 955ed13

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

‎tests/test_partition_distribution_protocol.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,58 @@ def test_distribution_records_reject_ambiguous_shapes() -> None:
113113
names=["id", "extra"],
114114
),
115115
)
116+
117+
118+
@pytest.mark.parametrize(
119+
("points", "direction", "null_order"),
120+
[
121+
([20, 10], OrderByDirection.ASC, OrderByNullOrder.NULLS_LAST),
122+
([10, 20], OrderByDirection.DESC, OrderByNullOrder.NULLS_LAST),
123+
([10, 10], OrderByDirection.ASC, OrderByNullOrder.NULLS_LAST),
124+
([None, 10], OrderByDirection.ASC, OrderByNullOrder.NULLS_LAST),
125+
([10, None], OrderByDirection.ASC, OrderByNullOrder.NULLS_FIRST),
126+
],
127+
)
128+
def test_range_split_points_must_be_strictly_ordered(
129+
points: list[int | None],
130+
direction: OrderByDirection,
131+
null_order: OrderByNullOrder,
132+
) -> None:
133+
"""Duplicate, reversed, and incorrectly null-ordered boundaries fail closed."""
134+
key = RangeKey(
135+
transform=encoded(identity("id")),
136+
direction=direction,
137+
null_order=null_order,
138+
)
139+
with pytest.raises(ValueError, match="strictly ordered"):
140+
RangePartitioning(
141+
keys=[encoded(key)],
142+
split_points=pa.record_batch([pa.array(points, type=pa.int64())], names=["id"]),
143+
)
144+
145+
146+
def test_range_split_points_accept_lexicographic_descending_and_null_order() -> None:
147+
"""The validator applies every key's direction and null placement lexicographically."""
148+
first = RangeKey(
149+
transform=encoded(identity("group")),
150+
direction=OrderByDirection.ASC,
151+
null_order=OrderByNullOrder.NULLS_FIRST,
152+
)
153+
second = RangeKey(
154+
transform=encoded(identity("value")),
155+
direction=OrderByDirection.DESC,
156+
null_order=OrderByNullOrder.NULLS_LAST,
157+
)
158+
159+
ranges = RangePartitioning(
160+
keys=[encoded(first), encoded(second)],
161+
split_points=pa.record_batch(
162+
[
163+
pa.array([None, 1, 1, 2], type=pa.int64()),
164+
pa.array([None, 20, 10, 99], type=pa.int64()),
165+
],
166+
names=["group", "value"],
167+
),
168+
)
169+
170+
assert ranges.split_points.num_rows == 4

‎vgi/protocol.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,41 @@ class RangePartitioning(ArrowSerializableDataclass):
603603
def __post_init__(self) -> None:
604604
if not self.keys:
605605
raise ValueError("range partitioning must contain at least one key")
606-
for value in self.keys:
607-
RangeKey.deserialize_from_bytes(value)
606+
decoded_keys = [RangeKey.deserialize_from_bytes(value) for value in self.keys]
608607
if self.split_points.num_columns != len(self.keys):
609608
raise ValueError("range split-point width must match the range key width")
609+
for row in range(1, self.split_points.num_rows):
610+
if _compare_range_rows(self.split_points, row - 1, row, decoded_keys) >= 0:
611+
raise ValueError("range split points must be strictly ordered by the declared comparator")
612+
613+
614+
def _compare_range_rows(
615+
batch: pa.RecordBatch,
616+
left_row: int,
617+
right_row: int,
618+
keys: list[RangeKey],
619+
) -> int:
620+
"""Compare two split-point rows using the protocol's lexicographic order."""
621+
for column, key in zip(batch.columns, keys, strict=True):
622+
left = column[left_row]
623+
right = column[right_row]
624+
if not left.is_valid or not right.is_valid:
625+
if not left.is_valid and not right.is_valid:
626+
continue
627+
left_first = key.null_order == OrderByNullOrder.NULLS_FIRST
628+
return (-1 if left_first else 1) if not left.is_valid else (1 if left_first else -1)
629+
try:
630+
if pc.equal(left, right).as_py() is True:
631+
continue
632+
less = pc.less(left, right).as_py() is True
633+
greater = pc.greater(left, right).as_py() is True
634+
except (pa.ArrowInvalid, pa.ArrowNotImplementedError, TypeError) as error:
635+
raise ValueError("range split points contain values that cannot be ordered") from error
636+
if not less and not greater:
637+
raise ValueError("range split points contain unordered values")
638+
result = -1 if less else 1
639+
return -result if key.direction == OrderByDirection.DESC else result
640+
return 0
610641

611642

612643
@dataclass(frozen=True, slots=True, kw_only=True)

0 commit comments

Comments
 (0)