-
Notifications
You must be signed in to change notification settings - Fork 14
Use parquet stats for pruning reads #2687 #2689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
8e32bb8
dbea018
c0aff54
31e2d5a
2a362d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,17 +4,111 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Dict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from orso.schema import RelationSchema | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from opteryx.managers.expression import NodeType | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from opteryx.models import RelationStatistics | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from opteryx.shared.stats_cache import StatsCache | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from opteryx.third_party.cyan4973.xxhash import hash_bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Statistics: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, statistics: dict, **kwargs): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.stats_cache = StatsCache() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.relation_statistics = RelationStatistics() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def read_blob_statistics( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, blob_name: str, blob_bytes: bytes = None, decoder=None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> Optional[Dict[str, Any]]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key = hex(hash_bytes(blob_name.encode())).encode() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cached_stats = self.stats_cache.get(key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if cached_stats is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # If statistics are cached, return them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return cached_stats | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cached_stats = decoder(blob_bytes, just_statistics=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if cached_stats is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.stats_cache.set(key, cached_stats) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return cached_stats | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def prefilter_blobs(self, blob_names: list[str], query_statistics, selection) -> list[str]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def prefilter_blobs(self, blob_names: list[str], query_statistics, selection) -> list[str]: | |
| def prefilter_blobs( | |
| self, blob_names: list[str], query_statistics: RelationStatistics, selection: list[NodeType] | |
| ) -> list[str]: |
Copilot
AI
Jul 23, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The repetitive if-elif chain for condition types could be refactored into a more maintainable approach using a dictionary mapping or strategy pattern.
| if condition.value == "Eq": # noqa: SIM102 | |
| # value must be within [min, max] | |
| if literal_value < min_value or literal_value > max_value: | |
| query_statistics.blobs_pruned += 1 | |
| skip_blob = True | |
| break | |
| elif condition.value == "NotEq": # noqa: SIM102 | |
| # only prune if min == max == literal (i.e., column only contains this value) | |
| if min_value == max_value == literal_value: | |
| query_statistics.blobs_pruned += 1 | |
| skip_blob = True | |
| break | |
| elif condition.value == "Gt": # noqa: SIM102 | |
| # value must be less than max to potentially match | |
| if max_value <= literal_value: | |
| query_statistics.blobs_pruned += 1 | |
| skip_blob = True | |
| break | |
| elif condition.value == "GtEq": # noqa: SIM102 | |
| # value must be less than or equal to max to potentially match | |
| if max_value < literal_value: | |
| query_statistics.blobs_pruned += 1 | |
| skip_blob = True | |
| break | |
| elif condition.value == "Lt": # noqa: SIM102 | |
| # value must be greater than min to potentially match | |
| if min_value >= literal_value: | |
| query_statistics.blobs_pruned += 1 | |
| skip_blob = True | |
| break | |
| elif condition.value == "LtEq": # noqa: SIM102 | |
| # value must be greater than or equal to min to potentially match | |
| if min_value > literal_value: | |
| query_statistics.blobs_pruned += 1 | |
| skip_blob = True | |
| break | |
| if not skip_blob: | |
| new_blob_names.append(blob_name) | |
| condition_handlers = { | |
| "Eq": self._handle_eq, | |
| "NotEq": self._handle_not_eq, | |
| "Gt": self._handle_gt, | |
| "GtEq": self._handle_gt_eq, | |
| "Lt": self._handle_lt, | |
| "LtEq": self._handle_lt_eq, | |
| } | |
| handler = condition_handlers.get(condition.value) | |
| if handler: | |
| skip_blob = handler( | |
| query_statistics, | |
| cached_stats, | |
| column_name, | |
| literal_value, | |
| ) | |
| if skip_blob: | |
| break | |
| if not skip_blob: | |
| new_blob_names.append(blob_name) | |
| new_blob_names.append(blob_name) |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,109 @@ | ||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||
| # you may not use this file except in compliance with the License. | ||||||||
| # See the License at http://www.apache.org/licenses/LICENSE-2.0 | ||||||||
| # Distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. | ||||||||
|
|
||||||||
| """ | ||||||||
| Global Blob Statistics Cache. | ||||||||
|
|
||||||||
| This module implements a global statistics cache using an LRU-K2 policy to determine | ||||||||
| which items to keep and which to evict. The cache is used to store and retrieve | ||||||||
| table or query statistics efficiently, and is shared across all connections and cursors. | ||||||||
|
|
||||||||
| The cache is limited by the number of items (MAX_STATISTICS_CACHE_ITEMS), not by memory volume. | ||||||||
| Eviction occurs when the item count exceeds the configured maximum. | ||||||||
| """ | ||||||||
|
|
||||||||
| from typing import Optional | ||||||||
|
|
||||||||
| from opteryx.config import MAX_STATISTICS_CACHE_ITEMS | ||||||||
| from opteryx.models import RelationStatistics | ||||||||
| from opteryx.utils.lru_2 import LRU2 | ||||||||
|
|
||||||||
|
|
||||||||
| class _StatsCache: | ||||||||
| """ | ||||||||
| Implements a statistics cache using an LRU-K2 eviction policy. | ||||||||
| Stores serialized statistics objects, keyed by bytes. | ||||||||
| """ | ||||||||
|
|
||||||||
| slots = "_lru" | ||||||||
|
|
||||||||
| def __init__(self): | ||||||||
| self._lru = LRU2() | ||||||||
|
|
||||||||
| def get(self, key: bytes) -> Optional[RelationStatistics]: | ||||||||
| """ | ||||||||
| Retrieve a statistics object from the cache by key. | ||||||||
| Returns the deserialized object if found, or None if not present. | ||||||||
| """ | ||||||||
| cached_stats = self._lru.get(key) | ||||||||
| if cached_stats is not None: | ||||||||
| return RelationStatistics.from_bytes(cached_stats) | ||||||||
| return None | ||||||||
|
|
||||||||
| def delete(self, key: bytes): | ||||||||
| """ | ||||||||
| Remove a statistics object from the cache by key. | ||||||||
| """ | ||||||||
| self._lru.delete(key) | ||||||||
|
|
||||||||
| def set(self, key: bytes, value: RelationStatistics) -> Optional[str]: | ||||||||
|
||||||||
| """ | ||||||||
| Store a statistics object in the cache, serializing it to bytes. | ||||||||
| If the cache exceeds the maximum allowed items, evict the least recently used item. | ||||||||
|
|
||||||||
| Args: | ||||||||
| key: The key associated with the statistics object. | ||||||||
| value: The statistics object to store (will be serialized). | ||||||||
|
|
||||||||
| Returns: | ||||||||
| The key of the evicted item if eviction occurred, otherwise None. | ||||||||
| """ | ||||||||
| cached_stats = value.to_bytes() | ||||||||
|
|
||||||||
| # Update LRU cache with the new key and memory pool key if commit succeeds | ||||||||
| self._lru.set(key, cached_stats) | ||||||||
| if self._lru.size > MAX_STATISTICS_CACHE_ITEMS: | ||||||||
|
||||||||
| if self._lru.size > MAX_STATISTICS_CACHE_ITEMS: | |
| current_size = len(self._lru) # Use len() to get the number of items in the cache | |
| if current_size > MAX_STATISTICS_CACHE_ITEMS: |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,6 +48,8 @@ def __init__(self, k=2): | |||||||
| self.evictions = 0 | ||||||||
| self.inserts = 0 | ||||||||
|
|
||||||||
| self.size = 0 | ||||||||
|
|
||||||||
| def __len__(self): | ||||||||
| return len(self.slots) | ||||||||
|
|
||||||||
|
|
@@ -62,6 +64,7 @@ def get(self, key: bytes): | |||||||
|
|
||||||||
| def set(self, key: bytes, value): | ||||||||
| self.inserts += 1 | ||||||||
| self.size += 1 | ||||||||
|
||||||||
| self.size += 1 | |
| if key not in self.slots: | |
| self.size += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parameters 'query_statistics' and 'selection' lack type hints, making the API unclear. Consider adding proper type annotations for these parameters to improve code clarity and IDE support.