Skip to content

Conversation

@EmilyMatt
Copy link
Contributor

@EmilyMatt EmilyMatt commented Oct 28, 2025

Which issue does this PR close?

Rationale for this change

Adds more detailed metrics, so it is easier to identify which part of the aggregate streams are actually slow.

What changes are included in this PR?

Added a metrics struct, and used it in the functions common to the aggregate streams.

Are these changes tested?

Yes, added some tests to verify the metrics are actually updated and can be retrieved.

I've also ran the groupby benchmarks to ensure we don't create timers in a way that could impact performance, and it seems ok, all the changes are within what I'd expect as std variation on a local machine.

Comparing main and agg-metrics
--------------------
Benchmark h2o.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃       main ┃ agg-metrics ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1     │ 1252.42 ms │  1196.62 ms │     no change │
│ QQuery 2     │ 3976.62 ms │  3392.89 ms │ +1.17x faster │
│ QQuery 3     │ 3448.29 ms │  2918.47 ms │ +1.18x faster │
│ QQuery 4     │ 1909.15 ms │  1632.98 ms │ +1.17x faster │
│ QQuery 5     │ 3056.36 ms │  2831.82 ms │ +1.08x faster │
│ QQuery 6     │ 2663.13 ms │  2594.64 ms │     no change │
│ QQuery 7     │ 2802.28 ms │  2592.43 ms │ +1.08x faster │
│ QQuery 8     │ 4489.29 ms │  4199.00 ms │ +1.07x faster │
│ QQuery 9     │ 7001.75 ms │  6622.98 ms │ +1.06x faster │
│ QQuery 10    │ 4725.80 ms │  4619.37 ms │     no change │
└──────────────┴────────────┴─────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary          ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (main)          │ 35325.09ms │
│ Total Time (agg-metrics)   │ 32601.19ms │
│ Average Time (main)        │  3532.51ms │
│ Average Time (agg-metrics) │  3260.12ms │
│ Queries Faster             │          7 │
│ Queries Slower             │          0 │
│ Queries with No Change     │          3 │
│ Queries with Failure       │          0 │
└────────────────────────────┴────────────┘

Are there any user-facing changes?

Nothing that is direct to the user, additional metrics will now be available, but no breaking changes.

@github-actions github-actions bot added the physical-plan Changes to the physical-plan crate label Oct 28, 2025
}

/// Evaluates expressions against a record batch.
pub fn evaluate_many(
Copy link
Member

@rluvaton rluvaton Oct 28, 2025

Choose a reason for hiding this comment

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

this is a breaking change as you changed the visibility and the arguments.

you should create a new function and leave this function as is

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, that is mentioned in the PR

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should change that function even when it is a utility

Copy link
Contributor Author

@EmilyMatt EmilyMatt Oct 28, 2025

Choose a reason for hiding this comment

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

I think this is a sure way to end up with lots of dead code because "someone surely uses it", instead, it was simply a mistake to have such a generic utility in this file made public, and the earlier it is fixed the less people are affected.

Copy link
Member

@rluvaton rluvaton Oct 28, 2025

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then this function should have been moved somewhere else, there are multiple copies of the code that does exactly that all over the project, keeping this specific instance pub is super random, and slightly disruptive, will work around it to not modify the visibility in this PR's scope and will open an issue to change that

evaluate_optional(&self.filter_expressions, &batch)?
};

let aggregation_timer = self.group_by_metrics.aggregation_time.timer();
Copy link
Member

Choose a reason for hiding this comment

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

Observation: the aggregation time also include finding the group indices which I don't think it

however a possible solution is to move inside the loop but I'm not a fan of it as it can be a hot loop

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 that was a conscious choice to avoid running this once per group, because current_group_indices is actually a scratch space, it can't organically be separated into two loops(although creating one scratch space per group_by is also valid)

Copy link
Member

Choose a reason for hiding this comment

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

the loop also contain building and searching the hash table self.group_values.intern which can be expensive (e.g. when GroupValuesRows is used)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a metric for it, I think this is a mistake tho

baseline_metrics: BaselineMetrics,

/// Aggregation-specific metrics
group_by_metrics: GroupByMetrics,
Copy link
Member

@rluvaton rluvaton Oct 28, 2025

Choose a reason for hiding this comment

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

Can you also add a timer for building and searching (single timer) the the hash table (group_values.intern)

or if you prefer we can leave that for later

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 think this will become a performance hit, I'd rather keep metrics concrete to avoid syscalls

pub struct GroupByMetrics {
pub aggregate_arguments_time: Time,
pub aggregation_time: Time,
pub emitting_time: Time,
Copy link
Member

@rluvaton rluvaton Oct 28, 2025

Choose a reason for hiding this comment

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

Can you add few subset timers from this:

  1. building aggregate expressions (how much time calling all accumulators update_batch/merge_batch)
  2. emitting aggregate expressions (how much time calling all accumulators emit/state)
  3. Emitting grouping columns (self.group_values.emit in row_hash is an example of that emission)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as previous comments, I think using clocks here is going to start encumbering the system

Co-authored-by: Raz Luvaton <16746759+rluvaton@users.noreply.github.com>
@rluvaton
Copy link
Member

Can you please update the PR description after your changes?

Copy link
Member

@rluvaton rluvaton left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @EmilyMatt !

@rluvaton rluvaton added this pull request to the merge queue Oct 28, 2025
Merged via the queue into apache:main with commit 6cc73fa Oct 28, 2025
32 checks passed
tobixdev pushed a commit to tobixdev/datafusion that referenced this pull request Nov 2, 2025
## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes apache#18323 .

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

Adds more detailed metrics, so it is easier to identify which part of
the aggregate streams are actually slow.

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

Added a metrics struct, and used it in the functions common to the
aggregate streams.

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

Yes, added some tests to verify the metrics are actually updated and can
be retrieved.

I've also ran the groupby benchmarks to ensure we don't create timers in
a way that could impact performance, and it seems ok, all the changes
are within what I'd expect as std variation on a local machine.
```
Comparing main and agg-metrics
--------------------
Benchmark h2o.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃       main ┃ agg-metrics ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1     │ 1252.42 ms │  1196.62 ms │     no change │
│ QQuery 2     │ 3976.62 ms │  3392.89 ms │ +1.17x faster │
│ QQuery 3     │ 3448.29 ms │  2918.47 ms │ +1.18x faster │
│ QQuery 4     │ 1909.15 ms │  1632.98 ms │ +1.17x faster │
│ QQuery 5     │ 3056.36 ms │  2831.82 ms │ +1.08x faster │
│ QQuery 6     │ 2663.13 ms │  2594.64 ms │     no change │
│ QQuery 7     │ 2802.28 ms │  2592.43 ms │ +1.08x faster │
│ QQuery 8     │ 4489.29 ms │  4199.00 ms │ +1.07x faster │
│ QQuery 9     │ 7001.75 ms │  6622.98 ms │ +1.06x faster │
│ QQuery 10    │ 4725.80 ms │  4619.37 ms │     no change │
└──────────────┴────────────┴─────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary          ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (main)          │ 35325.09ms │
│ Total Time (agg-metrics)   │ 32601.19ms │
│ Average Time (main)        │  3532.51ms │
│ Average Time (agg-metrics) │  3260.12ms │
│ Queries Faster             │          7 │
│ Queries Slower             │          0 │
│ Queries with No Change     │          3 │
│ Queries with Failure       │          0 │
└────────────────────────────┴────────────┘

```

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

Nothing that is direct to the user, additional metrics will now be
available, but no breaking changes.

---------

Co-authored-by: Raz Luvaton <16746759+rluvaton@users.noreply.github.com>
Co-authored-by: Eshed Schacham <ashdnazg@gmail.com>
codetyri0n pushed a commit to codetyri0n/datafusion that referenced this pull request Nov 11, 2025
## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes apache#123` indicates that this PR will close issue apache#123.
-->

- Closes apache#18323 .

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

Adds more detailed metrics, so it is easier to identify which part of
the aggregate streams are actually slow.

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

Added a metrics struct, and used it in the functions common to the
aggregate streams.

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

Yes, added some tests to verify the metrics are actually updated and can
be retrieved.

I've also ran the groupby benchmarks to ensure we don't create timers in
a way that could impact performance, and it seems ok, all the changes
are within what I'd expect as std variation on a local machine.
```
Comparing main and agg-metrics
--------------------
Benchmark h2o.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃       main ┃ agg-metrics ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1     │ 1252.42 ms │  1196.62 ms │     no change │
│ QQuery 2     │ 3976.62 ms │  3392.89 ms │ +1.17x faster │
│ QQuery 3     │ 3448.29 ms │  2918.47 ms │ +1.18x faster │
│ QQuery 4     │ 1909.15 ms │  1632.98 ms │ +1.17x faster │
│ QQuery 5     │ 3056.36 ms │  2831.82 ms │ +1.08x faster │
│ QQuery 6     │ 2663.13 ms │  2594.64 ms │     no change │
│ QQuery 7     │ 2802.28 ms │  2592.43 ms │ +1.08x faster │
│ QQuery 8     │ 4489.29 ms │  4199.00 ms │ +1.07x faster │
│ QQuery 9     │ 7001.75 ms │  6622.98 ms │ +1.06x faster │
│ QQuery 10    │ 4725.80 ms │  4619.37 ms │     no change │
└──────────────┴────────────┴─────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Benchmark Summary          ┃            ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ Total Time (main)          │ 35325.09ms │
│ Total Time (agg-metrics)   │ 32601.19ms │
│ Average Time (main)        │  3532.51ms │
│ Average Time (agg-metrics) │  3260.12ms │
│ Queries Faster             │          7 │
│ Queries Slower             │          0 │
│ Queries with No Change     │          3 │
│ Queries with Failure       │          0 │
└────────────────────────────┴────────────┘

```

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

Nothing that is direct to the user, additional metrics will now be
available, but no breaking changes.

---------

Co-authored-by: Raz Luvaton <16746759+rluvaton@users.noreply.github.com>
Co-authored-by: Eshed Schacham <ashdnazg@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve metrics for Aggregate streams

3 participants