-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: optimize grouping and introduced unparsing and substrait support #16161
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
Open
chenkovsky
wants to merge
28
commits into
apache:main
Choose a base branch
from
chenkovsky:feature/optimize_grouping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
d8d30c5
grouping udf
chenkovsky 6c540e7
update
chenkovsky 7bbc616
add test
chenkovsky fe6b56d
WIP grouping optimization
chenkovsky 501bb0d
update grouping
chenkovsky 8482046
fix bug
chenkovsky 90c4f93
Merge branch 'main' into feature/optimize_grouping
chenkovsky 7b79382
merge master
chenkovsky c723433
update
chenkovsky 516d255
fix test
chenkovsky 0c7184a
update test
chenkovsky a9d4ffc
fmt
chenkovsky ee4cf29
unparse
chenkovsky 208f6a6
fix bug
chenkovsky fc3d2fa
update
chenkovsky 12ad681
clippy
chenkovsky fe431e8
update doc
chenkovsky 28103db
update doc
chenkovsky 1911941
Merge branch 'main' into feature/optimize_grouping
chenkovsky 3373cd0
update
chenkovsky 275393b
fix dependency
chenkovsky f9a1def
Merge branch 'main' into feature/optimize_grouping
chenkovsky bf3a290
plan to sql
chenkovsky e9982f7
format
chenkovsky 4fde772
Merge branch 'main' into feature/optimize_grouping
chenkovsky 1117599
update
chenkovsky e929e3a
Merge branch 'main' into feature/optimize_grouping
chenkovsky 60cbf21
substrait support
chenkovsky 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,119 @@ | ||
// 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 std::{any::Any, sync::Arc}; | ||
|
||
use arrow::{ | ||
array::{Array, Int32Array}, | ||
datatypes::DataType, | ||
}; | ||
use datafusion_common::{internal_err, Result, ScalarValue}; | ||
use datafusion_expr_common::{ | ||
accumulator::Accumulator, | ||
signature::{Signature, Volatility}, | ||
}; | ||
use datafusion_functions_aggregate_common::accumulator::AccumulatorArgs; | ||
|
||
use crate::{ | ||
expr::{AggregateFunction, ScalarFunction}, | ||
utils::grouping_set_to_exprlist, | ||
Aggregate, AggregateUDF, AggregateUDFImpl, Expr, | ||
}; | ||
|
||
// To avoid adding datafusion-functions-aggregate dependency, implement a DummyGroupingUDAF here | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct DummyGroupingUDAF { | ||
signature: Signature, | ||
} | ||
|
||
impl Default for DummyGroupingUDAF { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl DummyGroupingUDAF { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::variadic_any(Volatility::Immutable), | ||
} | ||
} | ||
|
||
pub fn from_scalar_function( | ||
func: &ScalarFunction, | ||
agg: &Aggregate, | ||
) -> Result<AggregateFunction> { | ||
if func.args.len() != 1 && func.args.len() != 2 { | ||
return internal_err!("Grouping function must have one or two arguments"); | ||
} | ||
let grouping_expr = grouping_set_to_exprlist(&agg.group_expr)?; | ||
let args = if func.args.len() == 1 { | ||
grouping_expr.iter().map(|e| (*e).clone()).collect() | ||
} else if let Expr::Literal(ScalarValue::List(list), _) = &func.args[1] { | ||
if list.len() != 1 { | ||
return internal_err!("The second argument of grouping function must be a list with exactly one element"); | ||
} | ||
|
||
let grouping_expr = grouping_expr.into_iter().rev().collect::<Vec<_>>(); | ||
let values = list | ||
.value(0) | ||
.as_any() | ||
.downcast_ref::<Int32Array>() | ||
.unwrap() | ||
.values() | ||
.to_vec(); | ||
values | ||
.iter() | ||
.map(|i: &i32| grouping_expr[*i as usize].clone()) | ||
.collect() | ||
} else { | ||
return internal_err!( | ||
"The second argument of grouping function must be a list" | ||
); | ||
}; | ||
Ok(AggregateFunction::new_udf( | ||
Arc::new(AggregateUDF::from(Self::new())), | ||
args, | ||
false, | ||
None, | ||
vec![], | ||
None, | ||
)) | ||
} | ||
} | ||
|
||
impl AggregateUDFImpl for DummyGroupingUDAF { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"grouping" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
Ok(DataType::Int32) | ||
} | ||
|
||
fn accumulator(&self, _acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> { | ||
todo!() | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
currently, I created a dummy udaf. maybe we can move grouping udaf to datafusion-expr module directly. because grouping udaf is a pseudo udaf itself.