Skip to content
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

Implement GroupColumn Decimal128Array #13505

Open
Tracked by #12680
alamb opened this issue Nov 20, 2024 · 7 comments · May be fixed by #13564
Open
Tracked by #12680

Implement GroupColumn Decimal128Array #13505

alamb opened this issue Nov 20, 2024 · 7 comments · May be fixed by #13564
Assignees

Comments

@alamb
Copy link
Contributor

alamb commented Nov 20, 2024

Is your feature request related to a problem or challenge?

In #12269 @jayzhan211 made significant improvements to how group values are stored in multi-column aggregations.

Specifically for queries like

SELECT ... FROM ... GROUP BY col1, ... colN

The improvement relies on implementing specialized versions of GroupColumn for the types of col1, colN

We have implemented the primitive types and Strings/StringViews now, but we have not implemented all types

This means queries like

SELECT ... FROM ... GROUP BY int_cl, decimal_col

Will fall back to the slower (but general) GroupValuesRows:

/// representation.
pub struct GroupValuesRows {

Describe the solution you'd like

Implement GroupColumn for Decimal128 types.

You can see how to do this here:

macro_rules! downcast_helper {
($t:ty, $d:ident) => {
return Ok(Box::new(GroupValuesPrimitive::<$t>::new($d.clone())))
};
}

@jonathanc-n also made a really nice PR here

and the make sure there are tests for each of those types in queries that group on multiple columns

Describe alternatives you've considered

No response

Additional context

Here is an example for how this was done for Strings: #12809

@alamb
Copy link
Contributor Author

alamb commented Nov 20, 2024

BTW we can verify that this is working as expected after merging

Then you can do

cd datafusion-cli
RUST_LOG=debug cargo run -- -c "create or replace table foo(x decimal(10,3), y int) as values (10.0, 100), (21.2, 200), (33.0, 300); select count(*) from foo group by x, y";

You should not see any lines about Creating GroupValuesRows . Here is what is printed out on main

[2024-11-20T22:08:58Z DEBUG datafusion_physical_plan::aggregates::group_values::row] Creating GroupValuesRows for schema: Field { name: "x", data_type: Decimal128(10, 3), nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "y", data_type: Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }

@jonathanc-n
Copy link
Contributor

take

@jonathanc-n
Copy link
Contributor

jonathanc-n commented Nov 21, 2024

@alamb For this pr, will it need its own custom column implementation for decimal128 instead of instantiate_primitive!, similar to how byte, byteview, stringview, etc. are dealt with? I am thinking that due to the parameters

@alamb
Copy link
Contributor Author

alamb commented Nov 22, 2024

@alamb For this pr, will it need its own custom column implementation for decimal128 instead of instantiate_primitive!, similar to how byte, byteview, stringview, etc. are dealt with? I am thinking that due to the parameters

I think you can use Vec<u128> for the underlying storage (aka use the PrimtiveGroup struct), for the underlying primitive type, but then call this function to:

pub fn adjust_output_array(data_type: &DataType, array: ArrayRef) -> Result<ArrayRef> {

To adjust the type at the end

For example like this:

let vals = adjust_output_array(&self.data_type, vals).expect("Type is incorrect");

So the idea is that you keep the actual group values as native types (u128 in this case) as we are only comparing their values

Does that make sense?

@jonathanc-n
Copy link
Contributor

Yep, thanks!

@jayzhan211
Copy link
Contributor

@alamb For this pr, will it need its own custom column implementation for decimal128 instead of instantiate_primitive!, similar to how byte, byteview, stringview, etc. are dealt with? I am thinking that due to the parameters

I think you can use Vec<u128> for the underlying storage (aka use the PrimtiveGroup struct), for the underlying primitive type, but then call this function to:

pub fn adjust_output_array(data_type: &DataType, array: ArrayRef) -> Result<ArrayRef> {

To adjust the type at the end

For example like this:

let vals = adjust_output_array(&self.data_type, vals).expect("Type is incorrect");

So the idea is that you keep the actual group values as native types (u128 in this case) as we are only comparing their values

Does that make sense?

It seems adjust_output_array clone the array, is it better to use with_data_type to avoid the clone?

Ok(vec![Arc::new(array.with_data_type(self.data_type.clone()))])

@alamb
Copy link
Contributor Author

alamb commented Nov 23, 2024

It seems adjust_output_array clone the array, is it better to use with_data_type to avoid the clone?

I think they are roughly equivalent - if we can avoid cloning the array that certainly seems better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants