|
4 | 4 | # Distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. |
5 | 5 |
|
6 | 6 |
|
| 7 | +from typing import Any |
| 8 | +from typing import Dict |
7 | 9 | from typing import Optional |
8 | 10 |
|
9 | 11 | from orso.schema import RelationSchema |
10 | 12 |
|
| 13 | +from opteryx.managers.expression import NodeType |
11 | 14 | from opteryx.models import RelationStatistics |
| 15 | +from opteryx.shared.stats_cache import StatsCache |
| 16 | +from opteryx.third_party.cyan4973.xxhash import hash_bytes |
12 | 17 |
|
13 | 18 |
|
14 | 19 | class Statistics: |
15 | 20 | def __init__(self, statistics: dict, **kwargs): |
| 21 | + self.stats_cache = StatsCache() |
16 | 22 | self.relation_statistics = RelationStatistics() |
17 | 23 |
|
| 24 | + def read_blob_statistics( |
| 25 | + self, blob_name: str, blob_bytes: bytes = None, decoder=None |
| 26 | + ) -> Optional[Dict[str, Any]]: |
| 27 | + key = hex(hash_bytes(blob_name.encode())).encode() |
| 28 | + cached_stats = self.stats_cache.get(key) |
| 29 | + if cached_stats is not None: |
| 30 | + # If statistics are cached, return them |
| 31 | + return cached_stats |
| 32 | + |
| 33 | + cached_stats = decoder(blob_bytes, just_statistics=True) |
| 34 | + self.stats_cache.set(key, cached_stats) |
| 35 | + return cached_stats |
| 36 | + |
| 37 | + def prefilter_blobs(self, blob_names: list[str], query_statistics, selection) -> list[str]: |
| 38 | + new_blob_names = [] |
| 39 | + for blob_name in blob_names: |
| 40 | + key = hex(hash_bytes(blob_name.encode())).encode() |
| 41 | + cached_stats = self.stats_cache.get(key) |
| 42 | + if cached_stats is None: |
| 43 | + # we have no stats so we can't make a decision |
| 44 | + new_blob_names.append(blob_name) |
| 45 | + query_statistics.no_stats += 1 |
| 46 | + continue |
| 47 | + |
| 48 | + skip_blob = False |
| 49 | + |
| 50 | + for condition in selection: |
| 51 | + if condition.left.node_type != NodeType.IDENTIFIER: |
| 52 | + continue |
| 53 | + if condition.right.node_type != NodeType.LITERAL: |
| 54 | + continue |
| 55 | + |
| 56 | + column_name = condition.left.source_column |
| 57 | + literal_value = condition.right.value |
| 58 | + max_value = cached_stats.upper_bounds.get(column_name, None) |
| 59 | + min_value = cached_stats.lower_bounds.get(column_name, None) |
| 60 | + |
| 61 | + if max_value is None or min_value is None: |
| 62 | + continue |
| 63 | + |
| 64 | + if condition.value == "Eq": # noqa: SIM102 |
| 65 | + # value must be within [min, max] |
| 66 | + if literal_value < min_value or literal_value > max_value: |
| 67 | + query_statistics.blobs_pruned += 1 |
| 68 | + skip_blob = True |
| 69 | + break |
| 70 | + |
| 71 | + elif condition.value == "NotEq": # noqa: SIM102 |
| 72 | + # only prune if min == max == literal (i.e., column only contains this value) |
| 73 | + if min_value == max_value == literal_value: |
| 74 | + query_statistics.blobs_pruned += 1 |
| 75 | + skip_blob = True |
| 76 | + break |
| 77 | + |
| 78 | + elif condition.value == "Gt": # noqa: SIM102 |
| 79 | + # value must be less than max to potentially match |
| 80 | + if max_value <= literal_value: |
| 81 | + query_statistics.blobs_pruned += 1 |
| 82 | + skip_blob = True |
| 83 | + break |
| 84 | + |
| 85 | + elif condition.value == "GtEq": # noqa: SIM102 |
| 86 | + # value must be less than or equal to max to potentially match |
| 87 | + if max_value < literal_value: |
| 88 | + query_statistics.blobs_pruned += 1 |
| 89 | + skip_blob = True |
| 90 | + break |
| 91 | + |
| 92 | + elif condition.value == "Lt": # noqa: SIM102 |
| 93 | + # value must be greater than min to potentially match |
| 94 | + if min_value >= literal_value: |
| 95 | + query_statistics.blobs_pruned += 1 |
| 96 | + skip_blob = True |
| 97 | + break |
| 98 | + |
| 99 | + elif condition.value == "LtEq": # noqa: SIM102 |
| 100 | + # value must be greater than or equal to min to potentially match |
| 101 | + if min_value > literal_value: |
| 102 | + query_statistics.blobs_pruned += 1 |
| 103 | + skip_blob = True |
| 104 | + break |
| 105 | + |
| 106 | + if not skip_blob: |
| 107 | + new_blob_names.append(blob_name) |
| 108 | + |
| 109 | + return new_blob_names |
| 110 | + |
18 | 111 | def map_statistics( |
19 | 112 | self, statistics: Optional[RelationStatistics], schema: RelationSchema |
20 | 113 | ) -> RelationSchema: |
|
0 commit comments