-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Single mode for multi column group by -- Almost 2x for ClickBench Q32 #11792
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
54003ff
convert to single
jayzhan211 c427e75
queries
jayzhan211 1abd5e1
reuse-hash benchmark
jayzhan211 5b5bf9d
low cardinality
jayzhan211 f55b639
v2
jayzhan211 40d6a0a
result
jayzhan211 6317a79
production ready
jayzhan211 c0e195f
production ready
jayzhan211 a203b57
rm test1
jayzhan211 b7e77f7
merge benchmark code
jayzhan211 02c79ba
fix test
jayzhan211 142d6ed
clippy
jayzhan211 747e925
Merge remote-tracking branch 'upstream/main' into single-mode-groupby
jayzhan211 d1ca792
fix test
jayzhan211 d7f3086
Merge remote-tracking branch 'upstream/main' into single-mode-groupby
jayzhan211 6fa6844
rm long running test
jayzhan211 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// 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. | ||
|
||
use arrow::{ | ||
datatypes::{DataType, Field, Schema}, | ||
record_batch::RecordBatch, | ||
}; | ||
use arrow_array::{Int64Array, StringArray}; | ||
use arrow_schema::SchemaRef; | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use datafusion::prelude::SessionContext; | ||
use datafusion::{datasource::MemTable, error::Result}; | ||
use futures::executor::block_on; | ||
use std::sync::Arc; | ||
use tokio::runtime::Runtime; | ||
|
||
async fn query(ctx: &mut SessionContext, sql: &str) { | ||
let rt = Runtime::new().unwrap(); | ||
|
||
// execute the query | ||
let df = rt.block_on(ctx.sql(sql)).unwrap(); | ||
criterion::black_box(rt.block_on(df.collect()).unwrap()); | ||
} | ||
|
||
fn high_cardinality_batches( | ||
array_len: usize, | ||
batch_size: usize, | ||
schema: SchemaRef, | ||
) -> Vec<RecordBatch> { | ||
(0..array_len / batch_size) | ||
.map(|i| { | ||
let data1 = (0..batch_size) | ||
.map(|j| (batch_size * i + j) as i64) | ||
.collect::<Vec<_>>(); | ||
let data2 = (0..batch_size) | ||
.map(|j| format!("a{}", (batch_size * i + j))) | ||
.collect::<Vec<_>>(); | ||
|
||
RecordBatch::try_new( | ||
schema.clone(), | ||
vec![ | ||
Arc::new(Int64Array::from(data1)), | ||
Arc::new(StringArray::from(data2)), | ||
], | ||
) | ||
.unwrap() | ||
}) | ||
.collect::<Vec<_>>() | ||
} | ||
|
||
fn low_cardinality_batches( | ||
array_len: usize, | ||
batch_size: usize, | ||
schema: SchemaRef, | ||
) -> Vec<RecordBatch> { | ||
(0..array_len / batch_size) | ||
.map(|i| { | ||
let data1 = (0..batch_size) | ||
.map(|j| ((batch_size * i + j) % 4 > 1) as i64) | ||
.collect::<Vec<_>>(); | ||
let data2 = (0..batch_size) | ||
.map(|j| format!("a{}", ((batch_size * i + j) % 2))) | ||
.collect::<Vec<_>>(); | ||
|
||
RecordBatch::try_new( | ||
schema.clone(), | ||
vec![ | ||
Arc::new(Int64Array::from(data1)), | ||
Arc::new(StringArray::from(data2)), | ||
], | ||
) | ||
.unwrap() | ||
}) | ||
.collect::<Vec<_>>() | ||
} | ||
|
||
fn create_context( | ||
batches: Vec<RecordBatch>, | ||
schema: SchemaRef, | ||
) -> Result<SessionContext> { | ||
let ctx = SessionContext::new(); | ||
|
||
// declare a table in memory. In spark API, this corresponds to createDataFrame(...). | ||
let provider = MemTable::try_new(schema, vec![batches])?; | ||
ctx.register_table("t", Arc::new(provider))?; | ||
|
||
Ok(ctx) | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let array_len = 2000000; // 2M rows | ||
let batch_size = 8192; | ||
let schema = Arc::new(Schema::new(vec![ | ||
Field::new("a", DataType::Int64, false), | ||
Field::new("b", DataType::Utf8, false), | ||
])); | ||
|
||
c.bench_function("benchmark high cardinality", |b| { | ||
let batches = high_cardinality_batches(array_len, batch_size, Arc::clone(&schema)); | ||
let mut ctx = create_context(batches, Arc::clone(&schema)).unwrap(); | ||
b.iter(|| block_on(query(&mut ctx, "select a, b, count(*) from t group by a, b order by count(*) desc limit 10"))) | ||
}); | ||
|
||
c.bench_function("benchmark low cardinality", |b| { | ||
let batches = low_cardinality_batches(array_len, batch_size, Arc::clone(&schema)); | ||
let mut ctx = create_context(batches, Arc::clone(&schema)).unwrap(); | ||
b.iter(|| block_on(query(&mut ctx, "select a, b, count(*) from t group by a, b order by count(*) desc limit 10"))) | ||
}); | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
// This can be any expression that returns a `Criterion` object. | ||
config = Criterion::default().sample_size(10); | ||
targets = criterion_benchmark | ||
} | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main branch
This PR has slightly regression for low cardinality but huge gain for high cardinality