-
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] connect: add modulus operator and withColumns support (#3351)
- Add `%` operator and `sum` function to unresolved functions - Implement withColumns transformation - Add test coverage for group by with modulus operation
- Loading branch information
1 parent
86523a0
commit 8de0101
Showing
4 changed files
with
83 additions
and
1 deletion.
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
30 changes: 30 additions & 0 deletions
30
src/daft-connect/src/translation/logical_plan/with_columns.rs
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,30 @@ | ||
use eyre::bail; | ||
use spark_connect::{expression::ExprType, Expression}; | ||
|
||
use crate::translation::{to_daft_expr, to_logical_plan, Plan}; | ||
|
||
pub fn with_columns(with_columns: spark_connect::WithColumns) -> eyre::Result<Plan> { | ||
let spark_connect::WithColumns { input, aliases } = with_columns; | ||
|
||
let Some(input) = input else { | ||
bail!("input is required"); | ||
}; | ||
|
||
let mut plan = to_logical_plan(*input)?; | ||
|
||
let daft_exprs: Vec<_> = aliases | ||
.into_iter() | ||
.map(|alias| { | ||
let expression = Expression { | ||
common: None, | ||
expr_type: Some(ExprType::Alias(Box::new(alias))), | ||
}; | ||
|
||
to_daft_expr(&expression) | ||
}) | ||
.try_collect()?; | ||
|
||
plan.builder = plan.builder.with_columns(daft_exprs)?; | ||
|
||
Ok(plan) | ||
} |
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,33 @@ | ||
from __future__ import annotations | ||
|
||
from pyspark.sql.functions import col | ||
|
||
|
||
def test_group_by(spark_session): | ||
# Create DataFrame from range(10) | ||
df = spark_session.range(10) | ||
|
||
# Add a column that will have repeated values for grouping | ||
df = df.withColumn("group", col("id") % 3) | ||
|
||
# Group by the new column and sum the ids in each group | ||
df_grouped = df.groupBy("group").sum("id") | ||
|
||
# Convert to pandas to verify the sums | ||
df_grouped_pandas = df_grouped.toPandas() | ||
|
||
# Sort by group to ensure consistent order for comparison | ||
df_grouped_pandas = df_grouped_pandas.sort_values("group").reset_index(drop=True) | ||
|
||
# Verify the expected sums for each group | ||
# group id | ||
# 0 2 15 | ||
# 1 1 12 | ||
# 2 0 18 | ||
expected = { | ||
"group": [0, 1, 2], | ||
"id": [18, 12, 15], # todo(correctness): should this be "id" for value here? | ||
} | ||
|
||
assert df_grouped_pandas["group"].tolist() == expected["group"] | ||
assert df_grouped_pandas["id"].tolist() == expected["id"] |