-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Add
count-distinct
aggregation (#3455)
# Overview Add a count-distinct operation. - counts the unique number of elements inside of a column - will *not* count any `NULL`s, should they appear ## Example ```py df = daft.from_pydict({'a': [1,2,3,None,4]}) df.agg(daft.col('a').count_distinct()).show() ``` Will result in `4` being outputted. ## Implementation ### Uni-partitioned - calls `Series::count_distinct` which performs: - list_agg - list_unique_count ### Multi-partitioned - transforms into: - list_agg (stage 1) - list_concat (stage 2) - list_unique_count (final projection)
- Loading branch information
Raunak Bhagat
authored
Dec 3, 2024
1 parent
830f8b7
commit 75ad85a
Showing
18 changed files
with
261 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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,54 @@ | ||
use std::any::Any; | ||
|
||
use common_error::{DaftError, DaftResult}; | ||
use daft_core::{ | ||
prelude::{DataType, Field, Schema}, | ||
series::Series, | ||
}; | ||
use daft_dsl::{ | ||
functions::{ScalarFunction, ScalarUDF}, | ||
ExprRef, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||
pub struct ListUniqueCount; | ||
|
||
#[typetag::serde] | ||
impl ScalarUDF for ListUniqueCount { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &'static str { | ||
"list_unique_count" | ||
} | ||
|
||
fn to_field(&self, inputs: &[ExprRef], schema: &Schema) -> DaftResult<Field> { | ||
match inputs { | ||
[input] => { | ||
let field = input.to_field(schema)?; | ||
Ok(Field::new(field.name, DataType::UInt64)) | ||
} | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input arg, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series]) -> DaftResult<Series> { | ||
match inputs { | ||
[input] => input.list_unique_count(), | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 1 input arg, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn list_unique_count(expr: ExprRef) -> ExprRef { | ||
ScalarFunction::new(ListUniqueCount, vec![expr]).into() | ||
} |
This file contains 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 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 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.