-
Notifications
You must be signed in to change notification settings - Fork 147
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
[FEAT]: hash expr #2398
Merged
universalmind303
merged 2 commits into
Eventual-Inc:main
from
universalmind303:hash-expr
Jun 20, 2024
Merged
[FEAT]: hash expr #2398
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use common_error::{DaftError, DaftResult}; | ||
use daft_core::{ | ||
datatypes::{Field, UInt64Array}, | ||
schema::Schema, | ||
DataType, IntoSeries, Series, | ||
}; | ||
|
||
use crate::{ | ||
functions::{FunctionEvaluator, FunctionExpr}, | ||
Expr, ExprRef, | ||
}; | ||
|
||
pub(super) struct HashEvaluator {} | ||
|
||
impl FunctionEvaluator for HashEvaluator { | ||
fn fn_name(&self) -> &'static str { | ||
"hash" | ||
} | ||
|
||
fn to_field(&self, inputs: &[ExprRef], schema: &Schema, _: &FunctionExpr) -> DaftResult<Field> { | ||
match inputs { | ||
[input] | [input, _] => match input.to_field(schema) { | ||
Ok(field) => Ok(Field::new(field.name, DataType::UInt64)), | ||
e => e, | ||
}, | ||
_ => Err(DaftError::SchemaMismatch(format!( | ||
"Expected 2 input arg, got {}", | ||
inputs.len() | ||
))), | ||
} | ||
} | ||
|
||
fn evaluate(&self, inputs: &[Series], _: &FunctionExpr) -> DaftResult<Series> { | ||
match inputs { | ||
[input] => input.hash(None).map(|s| s.into_series()), | ||
[input, seed] => { | ||
match seed.len() { | ||
1 => { | ||
let seed = seed.cast(&DataType::UInt64)?; | ||
// There's no way to natively extend the array, so we extract the element and repeat it. | ||
let seed = seed.u64().unwrap(); | ||
let seed = seed.get(0).unwrap(); | ||
let seed = UInt64Array::from_iter( | ||
"seed", | ||
std::iter::repeat(Some(seed)).take(input.len()), | ||
); | ||
input.hash(Some(&seed)).map(|s| s.into_series()) | ||
} | ||
_ if seed.len() == input.len() => { | ||
let seed = seed.cast(&DataType::UInt64)?; | ||
let seed = seed.u64().unwrap(); | ||
|
||
input.hash(Some(seed)).map(|s| s.into_series()) | ||
} | ||
_ => Err(DaftError::ValueError( | ||
"Seed must be a single value or the same length as the input".to_string(), | ||
)), | ||
} | ||
} | ||
_ => Err(DaftError::ValueError("Expected 2 input arg".to_string())), | ||
} | ||
} | ||
} | ||
|
||
pub fn hash(input: ExprRef, seed: Option<ExprRef>) -> ExprRef { | ||
let inputs = match seed { | ||
Some(seed) => vec![input, seed], | ||
None => vec![input], | ||
}; | ||
|
||
Expr::Function { | ||
func: FunctionExpr::Hash, | ||
inputs, | ||
} | ||
.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
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,44 @@ | ||
import daft | ||
from daft import col | ||
|
||
|
||
def test_table_expr_hash(): | ||
df = daft.from_pydict( | ||
{ | ||
"utf8": ["foo", "bar"], | ||
"int": [1, None], | ||
} | ||
) | ||
expected = { | ||
"utf8": [12352915711150947722, 15304296276065178466], | ||
"int": [3439722301264460078, 3244421341483603138], | ||
} | ||
result = df.select(col("utf8").hash(), col("int").hash()) | ||
assert result.to_pydict() == expected | ||
|
||
|
||
def test_table_expr_hash_with_seed(): | ||
df = daft.from_pydict( | ||
{ | ||
"utf8": ["foo", "bar"], | ||
"int": [1, None], | ||
} | ||
) | ||
expected = { | ||
"utf8": [15221504070560512414, 2671805001252040144], | ||
"int": [16405722695416140795, 3244421341483603138], | ||
} | ||
result = df.select(col("utf8").hash(seed=42), col("int").hash(seed=42)) | ||
assert result.to_pydict() == expected | ||
|
||
|
||
def test_table_expr_hash_with_seed_array(): | ||
df = daft.from_pydict( | ||
{ | ||
"utf8": ["foo", "bar"], | ||
"seed": [1, 1000], | ||
} | ||
) | ||
expected = {"utf8": [6076897603942036120, 15438169081903732554]} | ||
result = df.select(col("utf8").hash(seed=col("seed"))) | ||
assert result.to_pydict() == expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It would also be good to run some of these hash tests that we run on the series on the expression as well!
https://github.com/Eventual-Inc/Daft/blob/main/tests/series/test_hash.py
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.
I was thinking that too, but didn't see anywhere else the exprs were being tested like that.
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.
We have tests like these here in
table
https://github.com/Eventual-Inc/Daft/blob/main/tests/table/utf8/test_ilike.py where we test the expression.You can create a new file like
test_hash.py
and test the hash expression on atable