|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +import json |
| 18 | +from os import path |
| 19 | + |
| 20 | +import pytest |
| 21 | +from datasketches import compact_theta_sketch, update_theta_sketch |
| 22 | + |
| 23 | +from pyiceberg.table.puffin import MAGIC_BYTES, PuffinFile |
| 24 | +from pyiceberg.table.theta_sketch import ThetaSketch, theta_sketches_from_puffin_file |
| 25 | + |
| 26 | + |
| 27 | +def _open_fixture(file: str) -> bytes: |
| 28 | + cur_dir = path.dirname(path.realpath(__file__)) |
| 29 | + with open(f"{cur_dir}/puffin/v1/{file}", "rb") as f: |
| 30 | + return f.read() |
| 31 | + |
| 32 | + |
| 33 | +def _make_sketch(values: list[int]) -> compact_theta_sketch: |
| 34 | + ts = update_theta_sketch() |
| 35 | + for v in values: |
| 36 | + ts.update(v) |
| 37 | + return ts.compact() |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def empty_sketch_bytes() -> bytes: |
| 42 | + return update_theta_sketch().compact().serialize() |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def three_value_sketch_bytes() -> bytes: |
| 47 | + return _make_sketch([1, 2, 3]).serialize() |
| 48 | + |
| 49 | + |
| 50 | +def test_empty_sketch(empty_sketch_bytes: bytes) -> None: |
| 51 | + sketch = compact_theta_sketch.deserialize(empty_sketch_bytes) |
| 52 | + ts = ThetaSketch(field_id=1, sketch=sketch) |
| 53 | + |
| 54 | + assert ts.is_empty() |
| 55 | + assert ts.get_estimate() == 0.0 |
| 56 | + |
| 57 | + |
| 58 | +def test_sketch_estimate(three_value_sketch_bytes: bytes) -> None: |
| 59 | + sketch = compact_theta_sketch.deserialize(three_value_sketch_bytes) |
| 60 | + ts = ThetaSketch(field_id=1, sketch=sketch) |
| 61 | + |
| 62 | + assert not ts.is_empty() |
| 63 | + assert ts.get_estimate() == pytest.approx(3.0) |
| 64 | + assert not ts.is_estimation_mode() |
| 65 | + |
| 66 | + |
| 67 | +def test_sketch_bounds_exact_mode(three_value_sketch_bytes: bytes) -> None: |
| 68 | + sketch = compact_theta_sketch.deserialize(three_value_sketch_bytes) |
| 69 | + ts = ThetaSketch(field_id=1, sketch=sketch) |
| 70 | + |
| 71 | + assert ts.get_lower_bound(1) == pytest.approx(3.0) |
| 72 | + assert ts.get_upper_bound(1) == pytest.approx(3.0) |
| 73 | + |
| 74 | + |
| 75 | +def test_sketch_field_id() -> None: |
| 76 | + sketch = _make_sketch([10, 20, 30]) |
| 77 | + ts = ThetaSketch(field_id=42, sketch=sketch) |
| 78 | + |
| 79 | + assert ts.field_id == 42 |
| 80 | + |
| 81 | + |
| 82 | +def test_sketch_property() -> None: |
| 83 | + sketch = _make_sketch([1, 2]) |
| 84 | + ts = ThetaSketch(field_id=1, sketch=sketch) |
| 85 | + |
| 86 | + assert ts.sketch is sketch |
| 87 | + |
| 88 | + |
| 89 | +def test_estimation_mode() -> None: |
| 90 | + ts_builder = update_theta_sketch(lg_k=5) |
| 91 | + for i in range(100): |
| 92 | + ts_builder.update(i) |
| 93 | + sketch = ts_builder.compact() |
| 94 | + ts = ThetaSketch(field_id=1, sketch=sketch) |
| 95 | + |
| 96 | + assert ts.is_estimation_mode() |
| 97 | + assert ts.get_estimate() > 0 |
| 98 | + assert ts.get_lower_bound(1) <= ts.get_estimate() |
| 99 | + assert ts.get_upper_bound(1) >= ts.get_estimate() |
| 100 | + |
| 101 | + |
| 102 | +def _build_puffin_file(blob_bytes: bytes, field_ids: list[int], snapshot_id: int = 1) -> bytes: |
| 103 | + # Puffin layout: magic(4) + blobs + footer_json + footer_size(4) + flags(4) + magic(4) |
| 104 | + # Blob offsets are file-absolute; first blob starts immediately after the 4-byte magic. |
| 105 | + blob_offset = 4 |
| 106 | + footer = { |
| 107 | + "blobs": [ |
| 108 | + { |
| 109 | + "type": "apache-datasketches-theta-v1", |
| 110 | + "snapshot-id": snapshot_id, |
| 111 | + "sequence-number": 1, |
| 112 | + "fields": field_ids, |
| 113 | + "offset": blob_offset, |
| 114 | + "length": len(blob_bytes), |
| 115 | + } |
| 116 | + ], |
| 117 | + "properties": {}, |
| 118 | + } |
| 119 | + footer_json = json.dumps(footer, separators=(",", ":")).encode("utf-8") |
| 120 | + footer_size_bytes = len(footer_json).to_bytes(4, byteorder="little") |
| 121 | + flags = b"\x00\x00\x00\x00" |
| 122 | + return MAGIC_BYTES + blob_bytes + footer_json + footer_size_bytes + flags + MAGIC_BYTES |
| 123 | + |
| 124 | + |
| 125 | +def test_theta_sketches_from_puffin_file_single_field(three_value_sketch_bytes: bytes) -> None: |
| 126 | + puffin_bytes = _build_puffin_file(three_value_sketch_bytes, field_ids=[5]) |
| 127 | + puffin_file = PuffinFile(puffin_bytes) |
| 128 | + |
| 129 | + sketches = theta_sketches_from_puffin_file(puffin_file) |
| 130 | + |
| 131 | + assert len(sketches) == 1 |
| 132 | + assert sketches[0].field_id == 5 |
| 133 | + assert sketches[0].get_estimate() == pytest.approx(3.0) |
| 134 | + |
| 135 | + |
| 136 | +def test_theta_sketches_from_puffin_file_multiple_fields(three_value_sketch_bytes: bytes) -> None: |
| 137 | + puffin_bytes = _build_puffin_file(three_value_sketch_bytes, field_ids=[1, 2, 3]) |
| 138 | + puffin_file = PuffinFile(puffin_bytes) |
| 139 | + |
| 140 | + sketches = theta_sketches_from_puffin_file(puffin_file) |
| 141 | + |
| 142 | + assert len(sketches) == 3 |
| 143 | + assert [s.field_id for s in sketches] == [1, 2, 3] |
| 144 | + for sketch in sketches: |
| 145 | + assert sketch.get_estimate() == pytest.approx(3.0) |
| 146 | + |
| 147 | + |
| 148 | +def test_theta_sketches_from_puffin_file_empty_sketch(empty_sketch_bytes: bytes) -> None: |
| 149 | + puffin_bytes = _build_puffin_file(empty_sketch_bytes, field_ids=[7]) |
| 150 | + puffin_file = PuffinFile(puffin_bytes) |
| 151 | + |
| 152 | + sketches = theta_sketches_from_puffin_file(puffin_file) |
| 153 | + |
| 154 | + assert len(sketches) == 1 |
| 155 | + assert sketches[0].is_empty() |
| 156 | + assert sketches[0].get_estimate() == 0.0 |
| 157 | + |
| 158 | + |
| 159 | +def test_theta_sketches_from_trino_written_puffin_file() -> None: |
| 160 | + puffin_file = PuffinFile(_open_fixture("theta-sketches.puffin")) |
| 161 | + sketches = theta_sketches_from_puffin_file(puffin_file) |
| 162 | + |
| 163 | + assert len(sketches) == 3 |
| 164 | + assert [s.field_id for s in sketches] == [1, 2, 3] |
| 165 | + for sketch in sketches: |
| 166 | + assert sketch.get_estimate() == pytest.approx(5.0) |
0 commit comments