Ted-Jiang commented on a change in pull request #1841: URL: https://github.com/apache/arrow-datafusion/pull/1841#discussion_r808701903
########## File path: datafusion/src/physical_plan/expressions/bitmap_distinct.rs ########## @@ -0,0 +1,233 @@ +// 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. + +//! Defines physical expressions that can evaluated at runtime during query execution + +use std::any::Any; +use std::borrow::Borrow; + +use std::fmt::Debug; +use std::ops::BitOrAssign; +use std::sync::Arc; + +use arrow::array::{ + Array, ArrayRef, BinaryArray, Int16Array, Int32Array, Int8Array, UInt16Array, + UInt32Array, UInt8Array, +}; +use arrow::datatypes::{DataType, Field}; +use croaring::Bitmap; +use log::info; + +use crate::error::{DataFusionError, Result}; +use crate::physical_plan::{Accumulator, AggregateExpr, PhysicalExpr}; +use crate::scalar::ScalarValue; + +use super::format_state_name; + +#[derive(Debug)] +pub struct BitMapDistinct { + name: String, + input_data_type: DataType, + expr: Arc<dyn PhysicalExpr>, +} + +impl BitMapDistinct { + /// Create a new bitmapDistinct aggregate function. + pub fn new( + expr: Arc<dyn PhysicalExpr>, + name: impl Into<String>, + input_data_type: DataType, + ) -> Self { + Self { + name: name.into(), + input_data_type, + expr, + } + } +} + +impl AggregateExpr for BitMapDistinct { + /// Return a reference to Any that can be used for downcasting + fn as_any(&self) -> &dyn Any { + self + } + + /// the field of the final result of this aggregation. + fn field(&self) -> Result<Field> { + Ok(Field::new(&self.name, DataType::UInt64, false)) + } + + fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> { + let accumulator: Box<dyn Accumulator> = match &self.input_data_type { + DataType::UInt8 + | DataType::UInt16 + | DataType::UInt32 + | DataType::Int8 + | DataType::Int16 + | DataType::Int32 => Box::new(BitmapDistinctCountAccumulator::try_new()), + other => { + return Err(DataFusionError::NotImplemented(format!( + "Support for 'bitmap_distinct' for data type {} is not implemented", + other + ))) + } + }; + Ok(accumulator) + } + + fn state_fields(&self) -> Result<Vec<Field>> { + Ok(vec![Field::new( + &format_state_name(&self.name, "bitmap_registers"), + DataType::Binary, + false, + )]) + } + + fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> { + vec![self.expr.clone()] + } + + fn name(&self) -> &str { + &self.name + } +} + +#[derive(Debug)] +struct BitmapDistinctCountAccumulator { + bitmap: croaring::bitmap::Bitmap, +} + +impl BitmapDistinctCountAccumulator { + fn try_new() -> Self { + Self { + bitmap: croaring::bitmap::Bitmap::create(), + } + } +} + +impl Accumulator for BitmapDistinctCountAccumulator { + //state() can be used by physical nodes to aggregate states together and send them over the network/threads, to combine values. + fn state(&self) -> Result<Vec<ScalarValue>> { + //maybe run optimized Review comment: IMHO, when meeting large amount data, run optimized may extremely reduce shuffle data between thread or process(ballista), it may reduce IO cost. -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org