Skip to content

Commit

Permalink
feat: support filter with windowed sort (#4960)
Browse files Browse the repository at this point in the history
* feat: support windowed sort with where condition

Signed-off-by: Ruihang Xia <[email protected]>

* fix split logic

Signed-off-by: Ruihang Xia <[email protected]>

* modify fuzz test to reflect logic change

Signed-off-by: Ruihang Xia <[email protected]>

* feat: handle sort that wont preserving partition

Signed-off-by: Ruihang Xia <[email protected]>

* clean up

Signed-off-by: Ruihang Xia <[email protected]>

* fix typo

Signed-off-by: Ruihang Xia <[email protected]>

* fix test case and add more cases

Signed-off-by: Ruihang Xia <[email protected]>

---------

Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia authored Nov 8, 2024
1 parent fcd0cee commit 8efbafa
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 45 deletions.
38 changes: 30 additions & 8 deletions src/query/src/optimizer/windowed_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use datafusion::physical_plan::coalesce_batches::CoalesceBatchesExec;
use datafusion::physical_plan::coalesce_partitions::CoalescePartitionsExec;
use datafusion::physical_plan::repartition::RepartitionExec;
use datafusion::physical_plan::sorts::sort::SortExec;
use datafusion::physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;
use datafusion::physical_plan::ExecutionPlan;
use datafusion_common::tree_node::{Transformed, TreeNode};
use datafusion_common::Result as DataFusionResult;
Expand Down Expand Up @@ -67,10 +68,12 @@ impl WindowedSortPhysicalRule {
.transform_down(|plan| {
if let Some(sort_exec) = plan.as_any().downcast_ref::<SortExec>() {
// TODO: support multiple expr in windowed sort
if !sort_exec.preserve_partitioning() || sort_exec.expr().len() != 1 {
if sort_exec.expr().len() != 1 {
return Ok(Transformed::no(plan));
}

let preserve_partitioning = sort_exec.preserve_partitioning();

let Some(scanner_info) = fetch_partition_range(sort_exec.input().clone())?
else {
return Ok(Transformed::no(plan));
Expand Down Expand Up @@ -111,11 +114,23 @@ impl WindowedSortPhysicalRule {
new_input,
)?;

return Ok(Transformed {
data: Arc::new(windowed_sort_exec),
transformed: true,
tnr: datafusion_common::tree_node::TreeNodeRecursion::Stop,
});
if !preserve_partitioning {
let order_preserving_merge = SortPreservingMergeExec::new(
sort_exec.expr().to_vec(),
Arc::new(windowed_sort_exec),
);
return Ok(Transformed {
data: Arc::new(order_preserving_merge),
transformed: true,
tnr: datafusion_common::tree_node::TreeNodeRecursion::Stop,
});
} else {
return Ok(Transformed {
data: Arc::new(windowed_sort_exec),
transformed: true,
tnr: datafusion_common::tree_node::TreeNodeRecursion::Stop,
});
}
}

Ok(Transformed::no(plan))
Expand All @@ -126,6 +141,7 @@ impl WindowedSortPhysicalRule {
}
}

#[derive(Debug)]
struct ScannerInfo {
partition_ranges: Vec<Vec<PartitionRange>>,
time_index: String,
Expand All @@ -136,25 +152,31 @@ fn fetch_partition_range(input: Arc<dyn ExecutionPlan>) -> DataFusionResult<Opti
let mut partition_ranges = None;
let mut time_index = None;
let mut tag_columns = None;
let mut is_batch_coalesced = false;

input.transform_up(|plan| {
// Unappliable case, reset the state.
if plan.as_any().is::<RepartitionExec>()
|| plan.as_any().is::<CoalesceBatchesExec>()
|| plan.as_any().is::<CoalescePartitionsExec>()
|| plan.as_any().is::<SortExec>()
|| plan.as_any().is::<WindowedSortExec>()
{
partition_ranges = None;
}

if plan.as_any().is::<CoalesceBatchesExec>() {
is_batch_coalesced = true;
}

if let Some(region_scan_exec) = plan.as_any().downcast_ref::<RegionScanExec>() {
partition_ranges = Some(region_scan_exec.get_uncollapsed_partition_ranges());
time_index = Some(region_scan_exec.time_index());
tag_columns = Some(region_scan_exec.tag_columns());

// set distinguish_partition_ranges to true, this is an incorrect workaround
region_scan_exec.with_distinguish_partition_range(true);
if !is_batch_coalesced {
region_scan_exec.with_distinguish_partition_range(true);
}
}

Ok(Transformed::no(plan))
Expand Down
Loading

0 comments on commit 8efbafa

Please sign in to comment.