-
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] (WIP) connect: createDataFrame
- Loading branch information
1 parent
df7ffd5
commit 421d94b
Showing
4 changed files
with
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use eyre::{bail, WrapErr}; | ||
|
||
use crate::translation::to_logical_plan; | ||
|
||
pub fn to_df(to_df: spark_connect::ToDf) -> eyre::Result<daft_logical_plan::LogicalPlanBuilder> { | ||
let spark_connect::ToDf { | ||
input, | ||
column_names, | ||
} = to_df; | ||
|
||
let Some(input) = input else { | ||
bail!("Input is required"); | ||
}; | ||
|
||
let plan = to_logical_plan(*input) | ||
.wrap_err_with(|| format!("Failed to translate relation to logical plan: {input:?}"))?; | ||
|
||
let column_names: Vec<_> = column_names.iter().map(daft_dsl::col).collect(); | ||
|
||
let plan = plan | ||
.with_columns(column_names) | ||
.wrap_err_with(|| format!("Failed to add columns to logical plan: {column_names:?}"))?; | ||
|
||
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,12 @@ | ||
from __future__ import annotations | ||
|
||
|
||
def test_create_df(spark_session): | ||
# Create simple DataFrame | ||
data = [(1,), (2,), (3,)] | ||
df = spark_session.createDataFrame(data, ["id"]) | ||
|
||
# Convert to pandas | ||
df_pandas = df.toPandas() | ||
assert len(df_pandas) == 3, "DataFrame should have 3 rows" | ||
assert list(df_pandas["id"]) == [1, 2, 3], "DataFrame should contain expected values" |
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,17 @@ | ||
from __future__ import annotations | ||
|
||
from pyspark.sql.functions import col | ||
|
||
|
||
def test_distinct(spark_session): | ||
# Create DataFrame with duplicates | ||
data = [(1,), (1,), (2,), (2,), (3,)] | ||
df = spark_session.createDataFrame(data, ["id"]) | ||
|
||
# Get distinct rows | ||
df_distinct = df.distinct() | ||
|
||
# Verify distinct operation removed duplicates | ||
df_distinct_pandas = df_distinct.toPandas() | ||
assert len(df_distinct_pandas) == 3, "Distinct should remove duplicates" | ||
assert set(df_distinct_pandas["id"]) == {1, 2, 3}, "Distinct values should be preserved" |