-
Notifications
You must be signed in to change notification settings - Fork 213
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
feat(storage): support range-filter scan by sort key #644
Merged
Merged
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 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 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 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 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 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Copyright 2022 RisingLight Project Authors. Licensed under Apache-2.0. | ||
|
||
use std::borrow::Borrow; | ||
use std::path::PathBuf; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
|
@@ -14,8 +15,10 @@ use super::{path_of_data_column, path_of_index_column, RowSetIterator}; | |
use crate::binder::BoundExpr; | ||
use crate::catalog::ColumnCatalog; | ||
use crate::storage::secondary::column::ColumnReadableFile; | ||
use crate::storage::secondary::encode::PrimitiveFixedWidthEncode; | ||
use crate::storage::secondary::DeleteVector; | ||
use crate::storage::{StorageColumnRef, StorageResult}; | ||
use crate::types::DataValue; | ||
|
||
/// Represents a column in Secondary. | ||
/// | ||
|
@@ -120,8 +123,19 @@ impl DiskRowset { | |
dvs: Vec<Arc<DeleteVector>>, | ||
seek_pos: ColumnSeekPosition, | ||
expr: Option<BoundExpr>, | ||
begin_keys: &[DataValue], | ||
end_keys: &[DataValue], | ||
) -> StorageResult<RowSetIterator> { | ||
RowSetIterator::new(self.clone(), column_refs, dvs, seek_pos, expr).await | ||
RowSetIterator::new( | ||
self.clone(), | ||
column_refs, | ||
dvs, | ||
seek_pos, | ||
expr, | ||
begin_keys, | ||
end_keys, | ||
) | ||
.await | ||
} | ||
|
||
pub fn on_disk_size(&self) -> u64 { | ||
|
@@ -131,10 +145,52 @@ impl DiskRowset { | |
.sum1() | ||
.unwrap_or(0) | ||
} | ||
|
||
/// Get the start row id to begin with for later table scanning. | ||
/// If `begin_keys` is empty, we return `ColumnSeekPosition::RowId(0)` to indicate scanning | ||
/// from the beginning, otherwise we scan the rowsets' first column indexes, find the first | ||
/// block who contains data greater than or equal to `begin_key` and return the row id of | ||
/// the block's first key. Currently, only the first column of the rowsets can be used to get | ||
/// the start row id and this column should be primary key. | ||
/// If `begin_key` is greater than all blocks' `first_key`, we return the `first_key` of the | ||
/// last block. | ||
/// Todo: support multi sort-keys range filter | ||
pub async fn start_rowid(&self, begin_keys: &[DataValue]) -> ColumnSeekPosition { | ||
if begin_keys.is_empty() { | ||
return ColumnSeekPosition::RowId(0); | ||
} | ||
|
||
// for now, we only use the first column to get the start row id, which means the length | ||
// of `begin_keys` can only be 0 or 1. | ||
let begin_key = begin_keys[0].borrow(); | ||
let column = self.column(0); | ||
let column_index = column.index(); | ||
|
||
let start_row_id = match *begin_key { | ||
DataValue::Int32(begin_val) => { | ||
let mut pre_block_first_key = 0; | ||
for index in column_index.indexes() { | ||
let mut first_key: &[u8] = &index.first_key; | ||
let first_val: i32 = PrimitiveFixedWidthEncode::decode(&mut first_key); | ||
|
||
if first_val > begin_val { | ||
break; | ||
} | ||
pre_block_first_key = index.first_rowid; | ||
} | ||
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. One optimization can be done in the future PRs:
|
||
pre_block_first_key | ||
} | ||
// Todo: support ohter type | ||
_ => panic!("for now support range-filter scan by sort key type of int32"), | ||
}; | ||
ColumnSeekPosition::RowId(start_row_id) | ||
skyzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod tests { | ||
use std::borrow::Borrow; | ||
|
||
use tempfile::TempDir; | ||
|
||
use super::*; | ||
|
@@ -270,11 +326,98 @@ pub mod tests { | |
.unwrap() | ||
} | ||
|
||
pub async fn helper_build_rowset_with_first_key_recorded(tempdir: &TempDir) -> DiskRowset { | ||
let columns = vec![ | ||
ColumnCatalog::new( | ||
0, | ||
DataTypeKind::Int(None) | ||
.not_null() | ||
.to_column_primary_key("v1".to_string()), | ||
), | ||
ColumnCatalog::new( | ||
1, | ||
DataTypeKind::Int(None) | ||
.not_null() | ||
.to_column("v2".to_string()), | ||
), | ||
ColumnCatalog::new( | ||
2, | ||
DataTypeKind::Int(None) | ||
.not_null() | ||
.to_column("v3".to_string()), | ||
), | ||
]; | ||
|
||
let mut builder = RowsetBuilder::new( | ||
columns.clone().into(), | ||
ColumnBuilderOptions::record_first_key_test(), | ||
); | ||
let mut key = 0; | ||
for _ in 0..10 { | ||
let mut array0 = vec![]; | ||
let mut array1 = vec![]; | ||
let mut array2 = vec![]; | ||
for _ in 0..28 { | ||
array0.push(key); | ||
array1.push(key + 1); | ||
array2.push(key + 2); | ||
key += 1; | ||
} | ||
builder.append( | ||
[ | ||
ArrayImpl::new_int32(array0.clone().into_iter().collect()), | ||
ArrayImpl::new_int32(array1.clone().into_iter().collect()), | ||
ArrayImpl::new_int32(array2.clone().into_iter().collect()), | ||
] | ||
.into_iter() | ||
.collect(), | ||
); | ||
} | ||
|
||
let backend = IOBackend::in_memory(); | ||
|
||
let writer = RowsetWriter::new(tempdir.path(), backend.clone()); | ||
writer.flush(builder.finish()).await.unwrap(); | ||
|
||
DiskRowset::open( | ||
tempdir.path().to_path_buf(), | ||
columns.into(), | ||
Cache::new(2333), | ||
0, | ||
backend, | ||
) | ||
.await | ||
.unwrap() | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_block() { | ||
let tempdir = tempfile::tempdir().unwrap(); | ||
let rowset = helper_build_rowset(&tempdir, true, 1000).await; | ||
let column = rowset.column(0); | ||
column.get_block(0).await.unwrap(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_get_start_id() { | ||
let tempdir = tempfile::tempdir().unwrap(); | ||
let rowset = helper_build_rowset_with_first_key_recorded(&tempdir).await; | ||
let start_keys = vec![DataValue::Int32(222)]; | ||
|
||
{ | ||
let start_rid = match rowset.start_rowid(start_keys.borrow()).await { | ||
ColumnSeekPosition::RowId(x) => x, | ||
_ => panic!("Unable to reach the branch"), | ||
}; | ||
assert_eq!(start_rid, 196_u32); | ||
} | ||
{ | ||
let start_keys = vec![DataValue::Int32(10000)]; | ||
let start_rid = match rowset.start_rowid(start_keys.borrow()).await { | ||
ColumnSeekPosition::RowId(x) => x, | ||
_ => panic!("Unable to reach the branch"), | ||
}; | ||
assert_eq!(start_rid, 252_u32); | ||
} | ||
} | ||
} |
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.
So we are assuming that the first column of the RowSet is pk, and pk is non-nullable. Should enforce this constraint in binder in later PRs.