You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize SortPreservingMergeExec for single-column merge #13642
As shown in my flamegraph profile, there are code paths that invoke arrow::Row::cmp.
From what I understand, RowValues (which implements CursorValues and whose cmp method is called in is_gt) might come from RowCursorStream::convert_batch.
However, I’m not entirely sure if this is accurate or if it can explain for the lack of usage of RowConverter or SortFields.
EDIT: seems like the first SortExpr comes from PARTITION BY id6, than this may not be a single-column sort problem?
I've found that the sort isn't actually planned as a single-column sort. The expressions in this case contains multiple columns:
// SQL: row_number() over (partition by id6 order by v3 desc)LexOrdering{inner:[PhysicalSortExpr{expr:Column{name:"id6",index:0},options:SortOptions{descending:false,nulls_first:false}},PhysicalSortExpr{expr:Column{name:"v3",index:1},options:SortOptions{descending:true,nulls_first:true}}]}
I'm not sure whether this behavior is expected or if the plan should have used a single-column sort optimization in this case.
Activity
alan910127 commentedon Dec 4, 2024
take
comphead commentedon Dec 6, 2024
Hi @Dandandan does the
SortPreservingMergeStreamwork with row format? I checked quickly https://github.com/apache/datafusion/blob/main/datafusion/physical-plan/src/sorts/merge.rs#L44 I dont seeRowConverteror SortFields in there. Am I missing something?alan910127 commentedon Dec 6, 2024
As shown in my flamegraph profile, there are code paths that invoke
arrow::Row::cmp.From what I understand,
RowValues(which implementsCursorValuesand whosecmpmethod is called inis_gt) might come fromRowCursorStream::convert_batch.However, I’m not entirely sure if this is accurate or if it can explain for the lack of usage of
RowConverterorSortFields.comphead commentedon Dec 7, 2024
Thanks for the flamegraph, indeed looks like it happens when dealing with working with
PartitionedStream. namely inand
type CursorStream<C> = Box<dyn PartitionedStream<Output = Result<(C, RecordBatch)>>>;so it explainsjayzhan211 commentedon Dec 7, 2024
It seems single column case is optimized here
datafusion/datafusion/physical-plan/src/sorts/streaming_merge.rs
Lines 168 to 180 in 8404cd0
alan910127 commentedon Feb 4, 2025
EDIT: seems like the first SortExpr comes from
PARTITION BY id6, than this may not be a single-column sort problem?I've found that the sort isn't actually planned as a single-column sort. The
expressionsin this case contains multiple columns:I'm not sure whether this behavior is expected or if the plan should have used a single-column sort optimization in this case.
[-]Optimize SortPreservingMergeStream for single-column merge[/-][+]Optimize SortExec for single-column merge[/+][-]Optimize SortExec for single-column merge[/-][+]Optimize SortPreservingMergeExec for single-column merge[/+]Dandandan commentedon Feb 4, 2025
Hm I see the query has two
I am not totally sure where I found the single row still in
SortPreservingMergeStream.I do think however there is opportunity for
sort_batchinExternalSorter(datafusion/datafusion/physical-plan/src/sorts/sort.rs
Line 628 in ea788c7