Skip to content

Add support for adaptive partial aggregation#20979

Merged
prithvip merged 1 commit intoprestodb:masterfrom
prithvip:adapative-partial-agg
Nov 27, 2023
Merged

Add support for adaptive partial aggregation#20979
prithvip merged 1 commit intoprestodb:masterfrom
prithvip:adapative-partial-agg

Conversation

@prithvip
Copy link
Contributor

@prithvip prithvip commented Sep 28, 2023

When partial aggregation is not effectively reducing cardinality, instead send raw input rows directly to the final aggregation step.

Port of github.com/trinodb/trino/pull/11011 and github.com/trinodb/trino/pull/17143

Description

This change adds support for adaptive partial aggregation.

Motivation and Context

Partial Aggregation can be inefficient when the number of unique values is high and the buffer is too small to effectively reduce cardinality. In these cases, the wasted CPU from hashing exceeds the benefit of transmitting less rows over the network. Partial aggregation should be disabled in this case.

Impact

Benchmarks results on TPCH:

Screenshot 2023-11-27 at 1 43 00 PM

Test Plan

  1. Unit tests
  2. Ran Presto cluster on extended shadow with this feature enabled
  3. Verifier run on production workload

Contributor checklist

  • Please make sure your submission complies with our development, formatting, commit message, and attribution guidelines.
  • PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced.
  • Documented new properties (with its default value), SQL syntax, functions, or other functionality.
  • If release notes are required, they follow the release notes guidelines.
  • Adequate tests were added if applicable.
  • CI passed.

Release Notes

== RELEASE NOTES ==
General Changes
* Improve efficiency of partial aggregation step. 
  This can be enabled by setting the ``adaptive_partial_aggregation`` session property to true. 

@prithvip prithvip requested a review from a team as a code owner September 28, 2023 02:17
@prithvip prithvip requested a review from presto-oss September 28, 2023 02:17
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Sep 28, 2023

CLA Not Signed

@prithvip prithvip requested review from MnO2 and ajaygeorge September 28, 2023 03:35
Copy link
Contributor

Choose a reason for hiding this comment

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

are these configs same as the ones used in Trino ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes they are

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add a comment on what a null result means ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Will hash agg ever produce non unique rows ? ie, Is the word unique redundant ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it will always be unique, but I think its clearer to call it unique to understand the logic, instead of something more generic like outputRowsProduced

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Add a comment on what an empty uniquerowsproduced means and how it differs from 0 unique rows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need any special handling in this clause wrt to spillEnabled ?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. looking at the code with spillEnabled we are bypassing it and going with Skip . Would that cause local OOMs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whether spill is enabled or not, if partial aggregation is not effective, we should use SkipAggregationBuilder instead. It would not cause local OOM since SkipAggregationBuilder doesn't use any additional memory other than the already allocated partial agg output buffer.

Copy link
Contributor

Choose a reason for hiding this comment

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

is this comment superfluous ?

Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. But will note that there are tons of places where we have no op as single line comment

Copy link
Contributor

Choose a reason for hiding this comment

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

fillConsecutive ? or Range to borrow the python name ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed to fillConsecutive

Copy link
Contributor

Choose a reason for hiding this comment

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

Q: What is the policy b/w the feature config name and the session prop name agreeing on the prefix experimental ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no policy - session prop name is totally different from feature config name and session props do not have prefixes unto themselves. When they do have prefix, it's to refer to the catalog to which the session prop should be applied.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why should the partial agg disabling look at the byte size instead of just the input and output row cardinalities ?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. It would be nice to clarify this in a comment

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I would refactor this check totalBytesProcessed >= maxPartialMemory.toBytes() * ENABLE_AGGREGATION_BUFFER_SIZE_TO_INPUT_BYTES_RATIO) to a shouldEnablePartialAggregation similar to the disable method below.

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've added a comment clarifying why we look at # bytes as the floor instead of # of rows.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you think of more tests for this ?

For example, anything to verify that the "vacous group-by" results output by the partial agg into the output buffer are consumable by the final agg for different data types ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do test several data types in the unit tests. For comprehensiveness, I think this is better suited for verifier testing, which will test wide range of data types

@ajaygeorge
Copy link
Contributor

Starting to take a look today.
Please mention the properties need to enable this feature in Release notes section.

@steveburnett
Copy link
Contributor

Starting to take a look today. Please mention the properties need to enable this feature in Release notes section.

Should this be documented, possibly in Aggregate Functions?

Copy link
Contributor

@ajaygeorge ajaygeorge left a comment

Choose a reason for hiding this comment

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

Looks mostly good.
Adding some comments

Copy link
Contributor

Choose a reason for hiding this comment

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

nit : maxPartialAggregationMemory

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed to maxPartialAggregationMemorySize

Comment on lines 25 to 26
Copy link
Contributor

Choose a reason for hiding this comment

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

nit considering to disable -> considering disabling

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. It would be nice to clarify this in a comment

Copy link
Contributor

Choose a reason for hiding this comment

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

same as above. why bytes is important to look at?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment should address this here too, it is the same reason.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. looking at the code with spillEnabled we are bypassing it and going with Skip . Would that cause local OOMs?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I would refactor this check totalBytesProcessed >= maxPartialMemory.toBytes() * ENABLE_AGGREGATION_BUFFER_SIZE_TO_INPUT_BYTES_RATIO) to a shouldEnablePartialAggregation similar to the disable method below.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove.

Copy link
Contributor

Choose a reason for hiding this comment

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

add requireNonNull(accumulatorFactories, "accumulatorFactories is null"); before this line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

nit. prefer functional style. (inputHashChannel.map(size -> 1).orElse(0))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

feels clearer to me to leave as is

Copy link
Contributor

Choose a reason for hiding this comment

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

can you help me understand why were are using NOOP here.? Is it because we are not doing any aggregation and just returning the flat rows.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes exactly, there is no memory used by this builder

Copy link
Contributor

@ajaygeorge ajaygeorge left a comment

Choose a reason for hiding this comment

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

LGTM % a few comments.

When partial aggregation is not effectively reducing cardinality, instead send
raw input rows directly to the final aggregation step.

Port of:
github.com/trinodb/trino/pull/11011
github.com/trinodb/trino/pull/17143

Co-authored-by: Lukasz Stec <lukasz.s.stec@gmail.com>
Co-authored-by: Karol Sobczak <sopel39@users.noreply.github.com>
@prithvip prithvip force-pushed the adapative-partial-agg branch from e1ede28 to c2312bb Compare November 27, 2023 21:47
@prithvip prithvip merged commit 588660e into prestodb:master Nov 27, 2023
@prithvip
Copy link
Contributor Author

Starting to take a look today. Please mention the properties need to enable this feature in Release notes section.

I've added session property info to enable this in Release Notes section

@wanglinsong wanglinsong mentioned this pull request Feb 12, 2024
64 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants