-
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.
- Loading branch information
1 parent
5238279
commit e9777ee
Showing
3 changed files
with
61 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use eyre::bail; | ||
|
||
use crate::translation::{to_logical_plan, Plan}; | ||
|
||
pub async fn drop(drop: spark_connect::Drop) -> eyre::Result<Plan> { | ||
let spark_connect::Drop { | ||
input, | ||
columns, | ||
column_names, | ||
} = drop; | ||
|
||
let Some(input) = input else { | ||
bail!("input is required"); | ||
}; | ||
|
||
if !columns.is_empty() { | ||
bail!("columns is not supported; use column_names instead"); | ||
} | ||
|
||
let mut plan = Box::pin(to_logical_plan(*input)).await?; | ||
|
||
// Get all column names from the schema | ||
let all_columns = plan.builder.schema().names(); | ||
|
||
// Create a set of columns to drop for efficient lookup | ||
let columns_to_drop: std::collections::HashSet<_> = column_names.iter().collect(); | ||
|
||
// Create expressions for all columns except the ones being dropped | ||
let to_select = all_columns | ||
.iter() | ||
.filter(|col_name| !columns_to_drop.contains(*col_name)) | ||
.map(|col_name| daft_dsl::col(col_name.clone())) | ||
.collect(); | ||
|
||
// Use select to keep only the columns we want | ||
plan.builder = plan.builder.select(to_select)?; | ||
|
||
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,16 @@ | ||
from __future__ import annotations | ||
|
||
|
||
def test_drop(spark_session): | ||
# Create DataFrame from range(10) | ||
df = spark_session.range(10) | ||
|
||
# Drop the 'id' column | ||
df_dropped = df.drop("id") | ||
|
||
# Verify the drop was successful | ||
assert "id" not in df_dropped.columns, "Column 'id' should be dropped" | ||
assert len(df_dropped.columns) == len(df.columns) - 1, "Should have one less column after drop" | ||
|
||
# Verify the DataFrame has no columns after dropping all columns" | ||
assert len(df_dropped.columns) == 0, "DataFrame should have no columns after dropping 'id'" |