Skip to content
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

Do not sort rows in FirstValueAccumulator #14402

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

blaginin
Copy link
Contributor

@blaginin blaginin commented Feb 1, 2025

Which issue does this PR close?

Closes #14215

Rationale for this change

Right now, merging / updating batches in first_value / last_value we sometimes sort input to find just one max / min row. In some cases, we request to sort only top-1 value but even that produces extra index buffers...

What changes are included in this PR?

Instead of sorting arrays, just finding min / max directly.

Are these changes tested?

Yes, by aggregate.slt. Also added new benches to illustrate the speed improvement

Are there any user-facing changes?

No

@github-actions github-actions bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) functions labels Feb 1, 2025
@blaginin
Copy link
Contributor Author

blaginin commented Feb 1, 2025

group                                                main                                    new_lexcmp
first_last_ignore_nulls                              2.02      4.3±0.23ms        ? ?/sec     1.00      2.1±0.08ms        ? ?/sec
first_last_many_columns                              1.12      2.3±0.12ms        ? ?/sec     1.00      2.0±0.08ms        ? ?/sec
first_last_one_column                                1.20  1907.2±49.72µs        ? ?/sec     1.00  1584.4±47.60µs        ? ?/sec

Comment on lines -641 to +618
let indices = lexsort_to_indices(&sort_cols, None)?;
take_arrays(&filtered_states, &indices, None)?
};
let comparator = LexicographicalComparator::try_new(&sort_columns)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In lexsort_to_indices there are some additional optimizations when there's just one column - so I also wrote a version which reuses lexsort_to_indices - but the speed increase there is actually 20% smaller than here

@@ -3003,7 +3003,7 @@ SELECT FIRST_VALUE(amount ORDER BY ts ASC) AS fv1,
LAST_VALUE(amount ORDER BY ts ASC) AS fv2
FROM sales_global
----
30 100
30 80
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO, it actually makes more sense because that line has the same ORDER BY value but appears last in the table.

@blaginin
Copy link
Contributor Author

blaginin commented Feb 1, 2025

fyi @jayzhan211

@jayzhan211 jayzhan211 requested a review from korowa February 2, 2025 07:07
}
let comparator = LexicographicalComparator::try_new(&sort_columns)?;
let best = (0..value.len())
.filter(|&index| !(self.ignore_nulls && value.is_null(index)))
Copy link
Contributor

@jayzhan211 jayzhan211 Feb 2, 2025

Choose a reason for hiding this comment

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

If we make 'ignore nulls' a constant generic, it might be faster than we can avoid filter logic for null if we don't ignore nulls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate functions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FirstValue / LastValue performance optimisations
2 participants