alamb commented on code in PR #8849: URL: https://github.com/apache/arrow-datafusion/pull/8849#discussion_r1468846650
########## datafusion/physical-expr/src/aggregate/count_distinct/strings.rs: ########## @@ -0,0 +1,487 @@ +// 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. + +//! Specialized implementation of `COUNT DISTINCT` for `StringArray` and `LargeStringArray` + +use ahash::RandomState; +use arrow_array::cast::AsArray; +use arrow_array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait}; +use arrow_buffer::{BufferBuilder, OffsetBuffer, ScalarBuffer}; +use datafusion_common::cast::as_list_array; +use datafusion_common::hash_utils::create_hashes; +use datafusion_common::utils::array_into_list_array; +use datafusion_common::ScalarValue; +use datafusion_execution::memory_pool::proxy::RawTableAllocExt; +use datafusion_expr::Accumulator; +use std::fmt::Debug; +use std::mem; +use std::ops::Range; +use std::sync::Arc; + +#[derive(Debug)] +pub(super) struct StringDistinctCountAccumulator<O: OffsetSizeTrait>(SSOStringHashSet<O>); +impl<O: OffsetSizeTrait> StringDistinctCountAccumulator<O> { + pub(super) fn new() -> Self { + Self(SSOStringHashSet::<O>::new()) + } +} + +impl<O: OffsetSizeTrait> Accumulator for StringDistinctCountAccumulator<O> { + fn state(&mut self) -> datafusion_common::Result<Vec<ScalarValue>> { + // take the state out of the string set and replace with default + let set = std::mem::take(&mut self.0); + let arr = set.into_state(); + let list = Arc::new(array_into_list_array(arr)); + Ok(vec![ScalarValue::List(list)]) + } + + fn update_batch(&mut self, values: &[ArrayRef]) -> datafusion_common::Result<()> { + if values.is_empty() { + return Ok(()); + } + + self.0.insert(values[0].clone()); Review Comment: Yes, you are right -- I tried did this in f5e268d07 Thank you ########## datafusion/physical-expr/src/aggregate/count_distinct/strings.rs: ########## @@ -0,0 +1,487 @@ +// 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. + +//! Specialized implementation of `COUNT DISTINCT` for `StringArray` and `LargeStringArray` + +use ahash::RandomState; +use arrow_array::cast::AsArray; +use arrow_array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait}; +use arrow_buffer::{BufferBuilder, OffsetBuffer, ScalarBuffer}; +use datafusion_common::cast::as_list_array; +use datafusion_common::hash_utils::create_hashes; +use datafusion_common::utils::array_into_list_array; +use datafusion_common::ScalarValue; +use datafusion_execution::memory_pool::proxy::RawTableAllocExt; +use datafusion_expr::Accumulator; +use std::fmt::Debug; +use std::mem; +use std::ops::Range; +use std::sync::Arc; + +#[derive(Debug)] +pub(super) struct StringDistinctCountAccumulator<O: OffsetSizeTrait>(SSOStringHashSet<O>); +impl<O: OffsetSizeTrait> StringDistinctCountAccumulator<O> { + pub(super) fn new() -> Self { + Self(SSOStringHashSet::<O>::new()) + } +} + +impl<O: OffsetSizeTrait> Accumulator for StringDistinctCountAccumulator<O> { + fn state(&mut self) -> datafusion_common::Result<Vec<ScalarValue>> { + // take the state out of the string set and replace with default + let set = std::mem::take(&mut self.0); + let arr = set.into_state(); + let list = Arc::new(array_into_list_array(arr)); + Ok(vec![ScalarValue::List(list)]) + } + + fn update_batch(&mut self, values: &[ArrayRef]) -> datafusion_common::Result<()> { + if values.is_empty() { + return Ok(()); + } + + self.0.insert(values[0].clone()); Review Comment: Yes, you are right -- I did this in f5e268d07 Thank you -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
