You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "lyne7-sc (via GitHub)" <gi...@apache.org> on 2026/08/23 05:34:32 UTC
[PR] bench: add window aggregate filter benchmarks [datafusion]
lyne7-sc opened a new pull request, #24589:
URL: https://github.com/apache/datafusion/pull/24589
## 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 #123` indicates that this PR will close issue #123.
-->
- Refs #24508.
## 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.
Please explain the problem you are trying to solve in terms of the user-visible
behavior, rather than the implementation.
For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the
implementation. "COUNT(DISTINCT) returns wrong results when the column contains
nulls" is the user-visible problem.
-->
Add benchmark coverage for evaluating the performance impact of the window aggregate filter argument evaluation changes in #24508.
## 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.
-->
Adds a Criterion benchmark for window aggregates with `FILTER` over pre-ordered input.
It covers:
- Cumulative and sliding frames through `BoundedWindowAggExec`.
- Whole-partition frames through `WindowAggExec`.
- Column, division, and `power` arguments.
- Different representative `FILTER` selectivities.
This PR only adds benchmark coverage and does not change behavior.
## 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.
## 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.
-->
No.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3888414871
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,291 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: Option<usize>) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| {
+ filter_percent
+ .map(|percent| (i * percent) % 100 < percent)
+ .unwrap_or(true)
+ })
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: Option<usize>,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ match filter_percent {
+ Some(_) => format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ None => format!("sum({})", argument_kind.name()),
+ },
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ filter_percent.map(|_| col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+ sample_size: usize,
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
+ group.sample_size(sample_size);
+
+ let plan = make_window_plan(None, ArgumentKind::Column, window_frame.clone());
Review Comment:
The baseline is fixed to `ArgumentKind::Column`, so `divide` and `power_udf` have no unfiltered run at their own shape. The plan evaluates the argument expression once per batch before the filter runs, so `power_udf` carries a base cost that `column/no_filter` does not show. Would it work to loop the baseline over `argument_kinds`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3849357956
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,254 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
Review Comment:
Thanks. I've reduced the sample size to 10.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal merged PR #24589:
URL: https://github.com/apache/datafusion/pull/24589
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3849355823
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,254 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
+
+ for &filter_percent in filter_percents {
+ for &argument_kind in argument_kinds {
+ let plan =
+ make_window_plan(filter_percent, argument_kind, window_frame.clone());
+ let task_ctx = Arc::new(TaskContext::default());
+ group.bench_function(
+ BenchmarkId::new(
+ argument_kind.name(),
+ format!("{filter_percent}_percent"),
+ ),
+ |b| {
+ b.iter(|| {
+ let batches = runtime
+ .block_on(collect(Arc::clone(&plan), Arc::clone(&task_ctx)))
+ .unwrap();
+ black_box(batches);
+ })
+ },
+ );
+ }
+ }
+
+ group.finish();
+}
+
+fn window_filter_benchmark(c: &mut Criterion) {
+ let runtime = tokio::runtime::Runtime::new().unwrap();
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_cumulative",
+ &cumulative_frame(),
+ &[
+ ArgumentKind::Column,
+ ArgumentKind::Divide,
+ ArgumentKind::Power,
+ ],
+ &[10, 30, 50],
+ );
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_sliding_10_rows",
+ &sliding_frame(),
+ &[ArgumentKind::Column, ArgumentKind::Power],
+ &[30],
+ );
+ benchmark_window_case(
Review Comment:
Thanks for the suggestion. I think this is a reasonable balance. I've updated the benchmark coverage.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3879252822
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
Review Comment:
Makes sense. added no-filter case per group as a baseline
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3879271516
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+ sample_size: usize,
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
+ group.sample_size(sample_size);
+
+ for &filter_percent in filter_percents {
+ for &argument_kind in argument_kinds {
+ let plan =
+ make_window_plan(filter_percent, argument_kind, window_frame.clone());
+ let task_ctx = Arc::new(TaskContext::default());
+ group.bench_function(
+ BenchmarkId::new(
+ argument_kind.name(),
+ format!("{filter_percent}_percent"),
+ ),
+ |b| {
+ b.iter(|| {
+ let batches = runtime
+ .block_on(collect(Arc::clone(&plan), Arc::clone(&task_ctx)))
+ .unwrap();
+ black_box(batches);
+ })
+ },
+ );
+ }
+ }
+
+ group.finish();
+}
+
+fn window_filter_benchmark(c: &mut Criterion) {
+ let runtime = tokio::runtime::Runtime::new().unwrap();
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_cumulative",
+ &cumulative_frame(),
+ &[
+ ArgumentKind::Column,
+ ArgumentKind::Divide,
+ ArgumentKind::Power,
+ ],
+ &[10, 30, 50],
+ 10,
+ );
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_sliding_10_rows",
+ &sliding_frame(),
Review Comment:
Added `Divide` and aligned the sliding filter selectivities with the other groups.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3889541672
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
Review Comment:
Updated. The filter mask now uses a fixed-seed shuffle to mix isolated and consecutive selected rows.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#issuecomment-5384510670
## [Codecov](https://app.codecov.io/gh/apache/datafusion/pull/24589?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 81.38%. Comparing base ([`dbdc627`](https://app.codecov.io/gh/apache/datafusion/commit/dbdc627deb98778dbf23cf7ccc2df7c66e1d9f55?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`9e87eaa`](https://app.codecov.io/gh/apache/datafusion/commit/9e87eaaa1f80fadad95b01a1ecc1dfc53686c0bf?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 39 commits behind head on main.
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## main #24589 +/- ##
==========================================
+ Coverage 81.31% 81.38% +0.07%
==========================================
Files 1117 1116 -1
Lines 395911 397960 +2049
Branches 395911 397960 +2049
==========================================
+ Hits 321918 323871 +1953
+ Misses 55177 55129 -48
- Partials 18816 18960 +144
```
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion/pull/24589?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3874118598
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
Review Comment:
Every case here passes a filter, so the run has no baseline without FILTER at the same shape. That makes it hard to read the cost of the filter machinery apart from the base window cost. Would it work to add one case per group that passes `None` here?
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
Review Comment:
At 10, 30 and 50 percent this expression never selects two rows in a row, so every selected run is one row long. The whole-partition case filters the full batch, and that shape is the most
expensive one for `arrow::compute::filter`. `i % 100 < filter_percent` gives the same selectivity with clustered runs. Would it be worth a second pattern with clustered runs, so the numbers show
the range rather than one end of it?
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+ sample_size: usize,
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
+ group.sample_size(sample_size);
+
+ for &filter_percent in filter_percents {
+ for &argument_kind in argument_kinds {
+ let plan =
+ make_window_plan(filter_percent, argument_kind, window_frame.clone());
+ let task_ctx = Arc::new(TaskContext::default());
+ group.bench_function(
+ BenchmarkId::new(
+ argument_kind.name(),
+ format!("{filter_percent}_percent"),
+ ),
+ |b| {
+ b.iter(|| {
+ let batches = runtime
+ .block_on(collect(Arc::clone(&plan), Arc::clone(&task_ctx)))
+ .unwrap();
+ black_box(batches);
+ })
+ },
+ );
+ }
+ }
+
+ group.finish();
+}
+
+fn window_filter_benchmark(c: &mut Criterion) {
+ let runtime = tokio::runtime::Runtime::new().unwrap();
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_cumulative",
+ &cumulative_frame(),
+ &[
+ ArgumentKind::Column,
+ ArgumentKind::Divide,
+ ArgumentKind::Power,
+ ],
+ &[10, 30, 50],
+ 10,
+ );
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_sliding_10_rows",
+ &sliding_frame(),
Review Comment:
The sliding frame filters twice per row, once for the update slice and once for the retract slice. That makes it the heaviest filter path of the three groups, but it skips `divide`. Would you add it here too?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3888413260
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
Review Comment:
yes, keep the benchmark on the window FILTER path, and the mixed distribution addresses my concern.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3889541143
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,291 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: Option<usize>) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| {
+ filter_percent
+ .map(|percent| (i * percent) % 100 < percent)
+ .unwrap_or(true)
+ })
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: Option<usize>,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ match filter_percent {
+ Some(_) => format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ None => format!("sum({})", argument_kind.name()),
+ },
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ filter_percent.map(|_| col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+ sample_size: usize,
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
+ group.sample_size(sample_size);
+
+ let plan = make_window_plan(None, ArgumentKind::Column, window_frame.clone());
Review Comment:
Updated. Each argument kind now has its own unfiltered baseline.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3852700317
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,254 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
Review Comment:
Updated. the bounded groups use a sample size of 10, while the whole-partition group uses default of 100.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3879299151
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
Review Comment:
Thanks again for the feedback. I took a closer look, and whether the selected rows are clustered or scattered does not seem to change the window-specific evaluation or accumulator work. The layout can still affect Arrow's filter kernel, but that feels like a separate dimension from what this benchmark is intended to isolate.
Should we keep this benchmark focused on the window FILTER path? I think we can adjust the existing include distribution so that it has a mix of isolated and consecutive selected rows. Would that address your concern?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "lyne7-sc (via GitHub)" <gi...@apache.org>.
lyne7-sc commented on PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#issuecomment-5477603513
Thank you @kumarUjjawal
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3850951301
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,255 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
+ group.sample_size(10);
+
+ for &filter_percent in filter_percents {
+ for &argument_kind in argument_kinds {
+ let plan =
+ make_window_plan(filter_percent, argument_kind, window_frame.clone());
+ let task_ctx = Arc::new(TaskContext::default());
+ group.bench_function(
+ BenchmarkId::new(
+ argument_kind.name(),
+ format!("{filter_percent}_percent"),
+ ),
+ |b| {
+ b.iter(|| {
+ let batches = runtime
+ .block_on(collect(Arc::clone(&plan), Arc::clone(&task_ctx)))
+ .unwrap();
+ black_box(batches);
+ })
+ },
+ );
+ }
+ }
+
+ group.finish();
+}
+
+fn window_filter_benchmark(c: &mut Criterion) {
+ let runtime = tokio::runtime::Runtime::new().unwrap();
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_cumulative",
+ &cumulative_frame(),
+ &[ArgumentKind::Column, ArgumentKind::Power],
+ &[30],
Review Comment:
Could we restore `divide` here, and the 10 and 50 percent points with it?
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,254 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
Review Comment:
What I had in mind was setting this per group: 10 for the millisecond-scale bounded frames, and leaving whole-partition at the default.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] bench: add window aggregate filter benchmarks [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #24589:
URL: https://github.com/apache/datafusion/pull/24589#discussion_r3841475629
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,254 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
Review Comment:
`bounded_window` sets `sample_size(10)` on its group, should we set it here too, per group so the microsecond-scale whole-partition cases can keep a higher count?
##########
datafusion/physical-plan/benches/window_filter.rs:
##########
@@ -0,0 +1,254 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Microbenchmark for window aggregates with `FILTER`. The benchmark uses
+//! pre-ordered input to exclude sorting and query planning, but executes the
+//! physical window plan so both stateful and whole-partition evaluation paths
+//! are represented.
+
+use std::hint::black_box;
+use std::sync::Arc;
+
+use arrow::array::{BooleanArray, Float64Array, UInt64Array};
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use arrow::record_batch::RecordBatch;
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use datafusion_common::{ScalarValue, config::ConfigOptions};
+use datafusion_execution::TaskContext;
+use datafusion_expr::{
+ Operator, WindowFrame, WindowFrameBound, WindowFrameUnits, WindowFunctionDefinition,
+};
+use datafusion_functions::math::power;
+use datafusion_functions_aggregate::sum::sum_udaf;
+use datafusion_physical_expr::expressions::{BinaryExpr, col, lit};
+use datafusion_physical_expr::{
+ LexOrdering, PhysicalExpr, PhysicalSortExpr, ScalarFunctionExpr,
+};
+use datafusion_physical_plan::test::TestMemoryExec;
+use datafusion_physical_plan::windows::{
+ BoundedWindowAggExec, WindowAggExec, create_window_expr,
+};
+use datafusion_physical_plan::{ExecutionPlan, InputOrderMode, collect};
+
+const BATCH_SIZE: usize = 8192;
+const NUM_BATCHES: usize = 4;
+
+#[derive(Clone, Copy)]
+enum ArgumentKind {
+ Column,
+ Divide,
+ Power,
+}
+
+impl ArgumentKind {
+ fn name(self) -> &'static str {
+ match self {
+ Self::Column => "column",
+ Self::Divide => "divide",
+ Self::Power => "power_udf",
+ }
+ }
+}
+
+fn schema() -> SchemaRef {
+ Arc::new(Schema::new(vec![
+ Field::new("id", DataType::UInt64, false),
+ Field::new("value", DataType::Float64, false),
+ Field::new("include", DataType::Boolean, false),
+ ]))
+}
+
+fn make_batches(filter_percent: usize) -> Vec<RecordBatch> {
+ (0..NUM_BATCHES)
+ .map(|batch_index| {
+ let start = batch_index * BATCH_SIZE;
+ let end = start + BATCH_SIZE;
+ let id = UInt64Array::from_iter_values((start..end).map(|i| i as u64));
+ let value = Float64Array::from_iter_values((start..end).map(|i| i as f64));
+ let include = BooleanArray::from(
+ (start..end)
+ .map(|i| (i * filter_percent) % 100 < filter_percent)
+ .collect::<Vec<_>>(),
+ );
+
+ RecordBatch::try_new(
+ schema(),
+ vec![Arc::new(id), Arc::new(value), Arc::new(include)],
+ )
+ .unwrap()
+ })
+ .collect()
+}
+
+fn window_argument(kind: ArgumentKind, schema: &Schema) -> Arc<dyn PhysicalExpr> {
+ let column = col("value", schema).unwrap();
+ match kind {
+ ArgumentKind::Column => column,
+ ArgumentKind::Divide => {
+ Arc::new(BinaryExpr::new(column, Operator::Divide, lit(10.0_f64)))
+ }
+ ArgumentKind::Power => Arc::new(
+ ScalarFunctionExpr::try_new(
+ power(),
+ vec![column, lit(1.5_f64)],
+ schema,
+ Arc::new(ConfigOptions::default()),
+ )
+ .unwrap(),
+ ),
+ }
+}
+
+fn cumulative_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn sliding_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(Some(10))),
+ WindowFrameBound::CurrentRow,
+ )
+}
+
+fn whole_partition_frame() -> WindowFrame {
+ WindowFrame::new_bounds(
+ WindowFrameUnits::Rows,
+ WindowFrameBound::Preceding(ScalarValue::UInt64(None)),
+ WindowFrameBound::Following(ScalarValue::UInt64(None)),
+ )
+}
+
+fn make_window_plan(
+ filter_percent: usize,
+ argument_kind: ArgumentKind,
+ window_frame: WindowFrame,
+) -> Arc<dyn ExecutionPlan> {
+ let schema = schema();
+ let order_by = vec![PhysicalSortExpr {
+ expr: col("id", &schema).unwrap(),
+ options: Default::default(),
+ }];
+ let window_expr = create_window_expr(
+ &WindowFunctionDefinition::AggregateUDF(sum_udaf()),
+ format!("sum({}) FILTER (WHERE include)", argument_kind.name()),
+ &[window_argument(argument_kind, &schema)],
+ &[],
+ &order_by,
+ Arc::new(window_frame),
+ Arc::clone(&schema),
+ false,
+ false,
+ Some(col("include", &schema).unwrap()),
+ )
+ .unwrap();
+
+ let source = TestMemoryExec::try_new(&[make_batches(filter_percent)], schema, None)
+ .unwrap()
+ .try_with_sort_information(LexOrdering::new(order_by).into_iter().collect())
+ .unwrap();
+ let input: Arc<dyn ExecutionPlan> =
+ Arc::new(TestMemoryExec::update_cache(&Arc::new(source)));
+
+ if window_expr.uses_bounded_memory() {
+ Arc::new(
+ BoundedWindowAggExec::try_new(
+ vec![window_expr],
+ input,
+ InputOrderMode::Sorted,
+ false,
+ )
+ .unwrap(),
+ )
+ } else {
+ Arc::new(WindowAggExec::try_new(vec![window_expr], input, false).unwrap())
+ }
+}
+
+fn benchmark_window_case(
+ c: &mut Criterion,
+ runtime: &tokio::runtime::Runtime,
+ name: &str,
+ window_frame: &WindowFrame,
+ argument_kinds: &[ArgumentKind],
+ filter_percents: &[usize],
+) {
+ let mut group = c.benchmark_group(format!("window_aggregate_filter/{name}"));
+
+ for &filter_percent in filter_percents {
+ for &argument_kind in argument_kinds {
+ let plan =
+ make_window_plan(filter_percent, argument_kind, window_frame.clone());
+ let task_ctx = Arc::new(TaskContext::default());
+ group.bench_function(
+ BenchmarkId::new(
+ argument_kind.name(),
+ format!("{filter_percent}_percent"),
+ ),
+ |b| {
+ b.iter(|| {
+ let batches = runtime
+ .block_on(collect(Arc::clone(&plan), Arc::clone(&task_ctx)))
+ .unwrap();
+ black_box(batches);
+ })
+ },
+ );
+ }
+ }
+
+ group.finish();
+}
+
+fn window_filter_benchmark(c: &mut Criterion) {
+ let runtime = tokio::runtime::Runtime::new().unwrap();
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_cumulative",
+ &cumulative_frame(),
+ &[
+ ArgumentKind::Column,
+ ArgumentKind::Divide,
+ ArgumentKind::Power,
+ ],
+ &[10, 30, 50],
+ );
+ benchmark_window_case(
+ c,
+ &runtime,
+ "bounded_sliding_10_rows",
+ &sliding_frame(),
+ &[ArgumentKind::Column, ArgumentKind::Power],
+ &[30],
+ );
+ benchmark_window_case(
Review Comment:
A whole-partition frame is constant in partition and does a single `update_batch`, while the cumulative and sliding frames walk row by row and spend their time in range calculation rather than argument evaluation. Would it be worth giving this group all three argument kinds and all three selectivities and trimming the cumulative group back?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org