-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Handle dicts for distinct count #15871
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
Changes from all commits
9004db2
8c5b623
9a0dad6
06819dc
1dbe235
d2e61e8
e51f766
933f19d
47fc9b5
2978e7a
161c1bb
a6f9507
e7e8cef
c8c94d1
8ed4259
dc7d508
d4f2a0c
1c44c57
f5b09b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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::array::{ArrayRef, BooleanArray}; | ||
use arrow::downcast_dictionary_array; | ||
use datafusion_common::{arrow_datafusion_err, ScalarValue}; | ||
use datafusion_common::{internal_err, DataFusionError}; | ||
use datafusion_expr_common::accumulator::Accumulator; | ||
|
||
#[derive(Debug)] | ||
pub struct DictionaryCountAccumulator { | ||
inner: Box<dyn Accumulator>, | ||
} | ||
|
||
impl DictionaryCountAccumulator { | ||
pub fn new(inner: Box<dyn Accumulator>) -> Self { | ||
Self { inner } | ||
} | ||
} | ||
|
||
impl Accumulator for DictionaryCountAccumulator { | ||
fn update_batch(&mut self, values: &[ArrayRef]) -> datafusion_common::Result<()> { | ||
let values: Vec<_> = values | ||
.iter() | ||
.map(|dict| { | ||
downcast_dictionary_array! { | ||
dict => { | ||
let buff: BooleanArray = dict.occupancy().into(); | ||
arrow::compute::filter( | ||
dict.values(), | ||
&buff | ||
).map_err(|e| arrow_datafusion_err!(e)) | ||
}, | ||
_ => internal_err!("DictionaryCountAccumulator only supports dictionary arrays") | ||
} | ||
}) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
self.inner.update_batch(values.as_slice()) | ||
} | ||
|
||
fn evaluate(&mut self) -> datafusion_common::Result<ScalarValue> { | ||
self.inner.evaluate() | ||
} | ||
|
||
fn size(&self) -> usize { | ||
self.inner.size() | ||
} | ||
|
||
fn state(&mut self) -> datafusion_common::Result<Vec<ScalarValue>> { | ||
self.inner.state() | ||
} | ||
|
||
fn merge_batch(&mut self, states: &[ArrayRef]) -> datafusion_common::Result<()> { | ||
self.inner.merge_batch(states) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we really want to juice performance, we would also implement a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, for sure - I think we can do it on top of this one? |
||
} |
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.
TIL:
occupancy