-
Notifications
You must be signed in to change notification settings - Fork 206
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: impl key partition rule #507
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c0474f5
impl key partition rule(draft).
Rachelint 854f7e0
refactor to more extensible and testable version.
Rachelint eca901e
add tests for key partition.
Rachelint 2b071dd
add df adapter and partition rule's building.
Rachelint 6a26d7d
address CR.
Rachelint 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
110 changes: 110 additions & 0 deletions
110
table_engine/src/partition/rule/df_adapter/extractor.rs
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0. | ||
|
||
//! Partition filter extractor | ||
|
||
use std::collections::HashSet; | ||
|
||
use common_types::datum::Datum; | ||
use datafusion_expr::{Expr, Operator}; | ||
use df_operator::visitor::find_columns_by_expr; | ||
|
||
use crate::partition::rule::filter::{PartitionCondition, PartitionFilter}; | ||
|
||
/// The datafusion filter exprs extractor | ||
/// | ||
/// It's used to extract the meaningful `Expr`s and convert them to | ||
/// [PartitionFilter](the inner filter type in ceresdb). | ||
/// | ||
/// NOTICE: When you implements [PartitionRule] for specific partition strategy, | ||
/// you should implement the corresponding [FilterExtractor], too. | ||
/// | ||
/// For example: [KeyRule] and [KeyExtractor]. | ||
/// If they are not related, [PartitionRule] may not take effect. | ||
pub trait FilterExtractor: Send + Sync + 'static { | ||
fn extract(&self, filters: &[Expr], columns: &[String]) -> Vec<PartitionFilter>; | ||
} | ||
pub struct KeyExtractor; | ||
|
||
impl FilterExtractor for KeyExtractor { | ||
fn extract(&self, filters: &[Expr], columns: &[String]) -> Vec<PartitionFilter> { | ||
if filters.is_empty() { | ||
return Vec::default(); | ||
} | ||
|
||
let mut target = Vec::with_capacity(filters.len()); | ||
for filter in filters { | ||
// If no target columns included in `filter`, ignore this `filter`. | ||
let columns_in_filter = find_columns_by_expr(filter) | ||
.into_iter() | ||
.collect::<HashSet<_>>(); | ||
let find_result = columns | ||
.iter() | ||
.find(|col| columns_in_filter.contains(col.as_str())); | ||
|
||
if find_result.is_none() { | ||
continue; | ||
} | ||
|
||
// If target columns included, now only the situation that only target column in | ||
// filter is supported. Once other type column found here, we ignore it. | ||
// TODO: support above situation. | ||
if columns_in_filter.len() != 1 { | ||
continue; | ||
} | ||
|
||
// Finally, we try to convert `filter` to `PartitionFilter`. | ||
// We just support the simple situation: "colum = value" now. | ||
// TODO: support "colum in [value list]". | ||
// TODO: we need to compare and check the datatype of column and value. | ||
// (Actually, there is type conversion on high-level, but when converted data | ||
// is overflow, it may take no effect). | ||
let partition_filter = match filter.clone() { | ||
Expr::BinaryExpr { left, op, right } => match (*left, op, *right) { | ||
(Expr::Column(col), Operator::Eq, Expr::Literal(val)) | ||
| (Expr::Literal(val), Operator::Eq, Expr::Column(col)) => { | ||
let datum_opt = Datum::from_scalar_value(&val); | ||
datum_opt.map(|d| PartitionFilter::new(col.name, PartitionCondition::Eq(d))) | ||
} | ||
_ => None, | ||
}, | ||
_ => None, | ||
}; | ||
|
||
if let Some(pf) = partition_filter { | ||
target.push(pf); | ||
} | ||
} | ||
|
||
target | ||
} | ||
} | ||
|
||
pub type FilterExtractorRef = Box<dyn FilterExtractor>; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use datafusion::scalar::ScalarValue; | ||
use datafusion_expr::col; | ||
|
||
use super::{FilterExtractor, *}; | ||
|
||
#[test] | ||
fn test_key_extractor_basic() { | ||
let extractor = KeyExtractor; | ||
|
||
// `Eq` expr will be accepted. | ||
let columns = vec!["col1".to_string()]; | ||
let accepted_expr = col("col1").eq(Expr::Literal(ScalarValue::Int32(Some(42)))); | ||
let partition_filter = extractor.extract(&[accepted_expr], &columns); | ||
let expected = PartitionFilter { | ||
column: "col1".to_string(), | ||
condition: PartitionCondition::Eq(Datum::Int32(42)), | ||
}; | ||
assert_eq!(partition_filter.get(0).unwrap(), &expected); | ||
|
||
// Other expr will be rejected now. | ||
let rejected_expr = col("col1").gt(Expr::Literal(ScalarValue::Int32(Some(42)))); | ||
let partition_filter = extractor.extract(&[rejected_expr], &columns); | ||
assert!(partition_filter.is_empty()); | ||
} | ||
} |
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.
Add one more newline above.