-
Notifications
You must be signed in to change notification settings - Fork 492
fix(mito2): schema-safe skipping index pruning #8122
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
Open
fengys1996
wants to merge
4
commits into
GreptimeTeam:main
Choose a base branch
from
fengys1996:fix/skip-index-compat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ use std::time::Instant; | |||||||
|
|
||||||||
| use common_base::range_read::RangeReader; | ||||||||
| use common_telemetry::{tracing, warn}; | ||||||||
| use datatypes::data_type::ConcreteDataType; | ||||||||
| use index::bloom_filter::applier::{BloomFilterApplier, InListPredicate}; | ||||||||
| use index::bloom_filter::reader::{ | ||||||||
| BloomFilterReadMetrics, BloomFilterReader, BloomFilterReaderImpl, | ||||||||
|
|
@@ -30,6 +31,7 @@ use object_store::ObjectStore; | |||||||
| use puffin::puffin_manager::cache::PuffinMetadataCacheRef; | ||||||||
| use puffin::puffin_manager::{PuffinManager, PuffinReader}; | ||||||||
| use snafu::ResultExt; | ||||||||
| use store_api::metadata::RegionMetadataRef; | ||||||||
| use store_api::region_request::PathType; | ||||||||
| use store_api::storage::ColumnId; | ||||||||
|
|
||||||||
|
|
@@ -133,10 +135,16 @@ pub struct BloomFilterIndexApplier { | |||||||
|
|
||||||||
| /// Bloom filter predicates. | ||||||||
| /// For each column, the value will be retained only if it contains __all__ predicates. | ||||||||
| predicates: Arc<BTreeMap<ColumnId, Vec<InListPredicate>>>, | ||||||||
| predicates: BTreeMap<ColumnId, Vec<InListPredicate>>, | ||||||||
|
|
||||||||
| /// Predicate key. Used to identify the predicate and fetch result from cache. | ||||||||
| predicate_key: PredicateKey, | ||||||||
| /// Expected predicate column types from the latest region metadata. | ||||||||
| expected_predicate_col_types: BTreeMap<ColumnId, ConcreteDataType>, | ||||||||
| } | ||||||||
|
|
||||||||
| #[derive(Clone)] | ||||||||
| pub(crate) struct SstApplyPlan { | ||||||||
| pub predicate_key: PredicateKey, | ||||||||
| pub predicates: Arc<BTreeMap<ColumnId, Vec<InListPredicate>>>, | ||||||||
| } | ||||||||
|
|
||||||||
| impl BloomFilterIndexApplier { | ||||||||
|
|
@@ -149,8 +157,8 @@ impl BloomFilterIndexApplier { | |||||||
| object_store: ObjectStore, | ||||||||
| puffin_manager_factory: PuffinManagerFactory, | ||||||||
| predicates: BTreeMap<ColumnId, Vec<InListPredicate>>, | ||||||||
| expected_predicate_col_types: BTreeMap<ColumnId, ConcreteDataType>, | ||||||||
| ) -> Self { | ||||||||
| let predicates = Arc::new(predicates); | ||||||||
| Self { | ||||||||
| table_dir, | ||||||||
| path_type, | ||||||||
|
|
@@ -159,8 +167,8 @@ impl BloomFilterIndexApplier { | |||||||
| puffin_manager_factory, | ||||||||
| puffin_metadata_cache: None, | ||||||||
| bloom_filter_index_cache: None, | ||||||||
| predicate_key: PredicateKey::new_bloom(predicates.clone()), | ||||||||
| predicates, | ||||||||
| expected_predicate_col_types, | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -207,6 +215,7 @@ impl BloomFilterIndexApplier { | |||||||
| &self, | ||||||||
| file_id: RegionIndexId, | ||||||||
| file_size_hint: Option<u64>, | ||||||||
| predicates: &BTreeMap<ColumnId, Vec<InListPredicate>>, | ||||||||
| row_groups: impl Iterator<Item = (usize, bool)>, | ||||||||
| mut metrics: Option<&mut BloomFilterIndexApplyMetrics>, | ||||||||
| ) -> Result<Vec<(usize, Vec<Range<usize>>)>> { | ||||||||
|
|
@@ -230,7 +239,7 @@ impl BloomFilterIndexApplier { | |||||||
| .map(|(i, range)| (*i, vec![range.clone()])) | ||||||||
| .collect::<Vec<_>>(); | ||||||||
|
|
||||||||
| for (column_id, predicates) in self.predicates.iter() { | ||||||||
| for (column_id, predicates) in predicates { | ||||||||
| let blob = match self | ||||||||
| .blob_reader(file_id, *column_id, file_size_hint, metrics.as_deref_mut()) | ||||||||
| .await? | ||||||||
|
|
@@ -439,8 +448,31 @@ impl BloomFilterIndexApplier { | |||||||
| } | ||||||||
|
|
||||||||
| /// Returns the predicate key. | ||||||||
| pub fn predicate_key(&self) -> &PredicateKey { | ||||||||
| &self.predicate_key | ||||||||
| pub fn plan_for_sst(&self, sst_metadata: &RegionMetadataRef) -> Option<SstApplyPlan> { | ||||||||
| let mut compatible_predicates = BTreeMap::new(); | ||||||||
|
|
||||||||
| for (col_id, expected) in &self.expected_predicate_col_types { | ||||||||
| if let Some(sst_col) = sst_metadata.column_by_id(*col_id) | ||||||||
| && sst_col.column_schema.data_type != *expected | ||||||||
| { | ||||||||
| continue; | ||||||||
| } | ||||||||
|
fengys1996 marked this conversation as resolved.
|
||||||||
|
|
||||||||
| if let Some(predicates) = self.predicates.get(col_id) { | ||||||||
| compatible_predicates.insert(*col_id, predicates.clone()); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| if compatible_predicates.is_empty() { | ||||||||
| return None; | ||||||||
| } | ||||||||
|
Comment on lines
+466
to
+468
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use a flag greptimedb/src/mito2/src/sst/index/inverted_index/applier.rs Lines 387 to 389 in f1ad472
|
||||||||
|
|
||||||||
| let predicates = Arc::new(compatible_predicates); | ||||||||
| let predicate_key = PredicateKey::new_bloom(predicates.clone()); | ||||||||
| Some(SstApplyPlan { | ||||||||
| predicate_key, | ||||||||
| predicates, | ||||||||
| }) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -496,8 +528,15 @@ mod tests { | |||||||
| ); | ||||||||
|
|
||||||||
| let applier = builder.build(&exprs).unwrap().unwrap(); | ||||||||
| let plan = applier.plan_for_sst(&Arc::new(metadata.clone())).unwrap(); | ||||||||
| applier | ||||||||
| .apply(file_id, None, row_groups.into_iter(), None) | ||||||||
| .apply( | ||||||||
| file_id, | ||||||||
| None, | ||||||||
| &plan.predicates, | ||||||||
| row_groups.into_iter(), | ||||||||
| None, | ||||||||
| ) | ||||||||
| .await | ||||||||
| .unwrap() | ||||||||
| .into_iter() | ||||||||
|
|
||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
tests/cases/standalone/common/alter/change_col_type_skipping_index.result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| -- Regression test for skip index with column type change. | ||
| CREATE TABLE monitoring_data_skip ( | ||
| host STRING SKIPPING INDEX, | ||
| `region` STRING, | ||
| cpu_usage DOUBLE SKIPPING INDEX, | ||
| `timestamp` TIMESTAMP TIME INDEX | ||
| ) WITH ('append_mode'='true'); | ||
|
|
||
| Affected Rows: 0 | ||
|
|
||
| INSERT INTO monitoring_data_skip (host, region, cpu_usage, `timestamp`) VALUES | ||
| ('web-01', 'us-east', 12.5, '2026-05-06 10:00:00'), | ||
| ('web-01', 'us-east', 15.2, '2026-05-06 10:01:00'), | ||
| ('web-02', 'us-east', 23.7, '2026-05-06 10:01:00'), | ||
| ('db-01', 'us-east', 45.0, '2026-05-06 10:02:00'), | ||
| ('db-02', 'us-west', 82.2, '2026-05-06 10:02:00'), | ||
| ('cache-01', 'eu-central', 55.4, '2026-05-06 10:02:00'), | ||
| ('queue-01', 'ap-south', 99.1, '2026-05-06 10:02:00'); | ||
|
|
||
| Affected Rows: 7 | ||
|
|
||
| ADMIN FLUSH_TABLE('monitoring_data_skip'); | ||
|
|
||
| +-------------------------------------------+ | ||
| | ADMIN FLUSH_TABLE('monitoring_data_skip') | | ||
| +-------------------------------------------+ | ||
| | 0 | | ||
| +-------------------------------------------+ | ||
|
|
||
| ALTER TABLE monitoring_data_skip | ||
| MODIFY COLUMN cpu_usage STRING; | ||
|
|
||
| Affected Rows: 0 | ||
|
|
||
| SELECT host, region, cpu_usage, `timestamp` FROM monitoring_data_skip | ||
| WHERE cpu_usage = '23.7'; | ||
|
|
||
| +--------+---------+-----------+---------------------+ | ||
| | host | region | cpu_usage | timestamp | | ||
| +--------+---------+-----------+---------------------+ | ||
| | web-02 | us-east | 23.7 | 2026-05-06T10:01:00 | | ||
| +--------+---------+-----------+---------------------+ | ||
|
|
||
| DROP TABLE monitoring_data_skip; | ||
|
|
||
| Affected Rows: 0 | ||
|
|
26 changes: 26 additions & 0 deletions
26
tests/cases/standalone/common/alter/change_col_type_skipping_index.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| -- Regression test for skip index with column type change. | ||
| CREATE TABLE monitoring_data_skip ( | ||
| host STRING SKIPPING INDEX, | ||
| `region` STRING, | ||
| cpu_usage DOUBLE SKIPPING INDEX, | ||
| `timestamp` TIMESTAMP TIME INDEX | ||
| ) WITH ('append_mode'='true'); | ||
|
|
||
| INSERT INTO monitoring_data_skip (host, region, cpu_usage, `timestamp`) VALUES | ||
| ('web-01', 'us-east', 12.5, '2026-05-06 10:00:00'), | ||
| ('web-01', 'us-east', 15.2, '2026-05-06 10:01:00'), | ||
| ('web-02', 'us-east', 23.7, '2026-05-06 10:01:00'), | ||
| ('db-01', 'us-east', 45.0, '2026-05-06 10:02:00'), | ||
| ('db-02', 'us-west', 82.2, '2026-05-06 10:02:00'), | ||
| ('cache-01', 'eu-central', 55.4, '2026-05-06 10:02:00'), | ||
| ('queue-01', 'ap-south', 99.1, '2026-05-06 10:02:00'); | ||
|
|
||
| ADMIN FLUSH_TABLE('monitoring_data_skip'); | ||
|
|
||
| ALTER TABLE monitoring_data_skip | ||
| MODIFY COLUMN cpu_usage STRING; | ||
|
|
||
| SELECT host, region, cpu_usage, `timestamp` FROM monitoring_data_skip | ||
| WHERE cpu_usage = '23.7'; | ||
|
|
||
| DROP TABLE monitoring_data_skip; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 comment is outdated.