Skip to content

Conversation

@tustvold
Copy link
Contributor

@tustvold tustvold commented Apr 6, 2023

Which issue does this PR close?

Part of #5882

Rationale for this change

merge sorted i64        time:   [5.3681 ms 5.3730 ms 5.3784 ms]
                        change: [-43.762% -43.690% -43.621%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) high mild
  1 (1.00%) high severe

sort merge i64          time:   [5.6187 ms 5.6264 ms 5.6344 ms]
                        change: [-42.354% -42.259% -42.162%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

merge sorted f64        time:   [5.6240 ms 5.6307 ms 5.6386 ms]
                        change: [-40.730% -40.639% -40.548%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

sort merge f64          time:   [5.9045 ms 5.9124 ms 5.9207 ms]
                        change: [-39.173% -39.065% -38.945%] (p = 0.00 < 0.05)
                        Performance has improved.

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added the core Core DataFusion crate label Apr 6, 2023
offset: usize,
// If nulls first, the first non-null index
// Otherwise, the first null index
null_threshold: usize,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite pleased with this formulation of nulls, it avoids needing to consult the null mask at all 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason it is called null_threshold rather than null_index given it is a null index 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought null_index might be confused for the index of the first null, which is not always the case

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes sense.

options: SortOptions,
}

impl<T: ArrowNativeTypeOp> PrimitiveCursor<T> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The astute will notice this is parameterised on the underlying Native type, helping to reduce codegen by not forcing SortPreservingMergeStream to be generic over ArrowPrimitiveType

Comment on lines 60 to 61
arrow-schema = { workspace = true }
arrow-array = { workspace = true }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The downcast macros require these crates to be in scope

@tustvold tustvold marked this pull request as ready for review April 7, 2023 15:05
@alamb alamb changed the title Specialize Primitive Cursor Specialize Primitive Cursor -- make sorts / merges on a single column faster Apr 10, 2023
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tustvold -- this looks great. cc @jaylmiller and @ozankabak

offset: usize,
// If nulls first, the first non-null index
// Otherwise, the first null index
null_threshold: usize,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason it is called null_threshold rather than null_index given it is a null index 🤔

let s_v = self.value();
let o_v = other.value();

match self.options.descending {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be worth specializing on consts NULLS_FIRST and ASC/DESC as well to avoid what looks like runtime overhead in the hot path.

Maybe it doesn't make any practical difference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely something we could experiment, my expectation is that it won't make a huge difference given how branch heavy merging inherently is

batch_size: usize,
) -> Result<SendableRecordBatchStream> {
let streams = RowCursorStream::try_new(schema.as_ref(), expressions, streams)?;
if expressions.len() == 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is worth a comment here explaining the rationale for this for future readers. Something like

Suggested change
if expressions.len() == 1 {
// special case single column primitives to avoid overhead of runtime comparators
if expressions.len() == 1 {

}
}

pub struct PrimitiveCursorStream<T: ArrowPrimitiveType> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub struct PrimitiveCursorStream<T: ArrowPrimitiveType> {
/// Specialized stream for sorts on single primitive columns
pub struct PrimitiveCursorStream<T: ArrowPrimitiveType> {

@alamb alamb changed the title Specialize Primitive Cursor -- make sorts / merges on a single column faster Specialize Primitive Cursor -- make sorts / merges on a single primitive column faster Apr 10, 2023
@ozankabak
Copy link
Contributor

Thanks @tustvold

@tustvold tustvold merged commit 4675a58 into apache:main Apr 11, 2023
korowa pushed a commit to korowa/arrow-datafusion that referenced this pull request Apr 13, 2023
…ive column faster (apache#5897)

* Specialize PrimitiveCursor (apache#5882)

* Toml format

* Review feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants