Skip to content

Commit 0821679

Browse files
committed
refactor: simplify with_column method by using with_column directly
1 parent 0a28da5 commit 0821679

2 files changed

Lines changed: 4 additions & 16 deletions

File tree

python/datafusion/context.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,12 @@ def from_polars(self, data: pl.DataFrame, name: str | None = None) -> DataFrame:
731731
"""
732732
return DataFrame(self.ctx.from_polars(data, name))
733733

734-
# https://github.com/apache/datafusion-python/pull/1016#discussion_r1983239116
735-
# is the discussion on how we arrived at adding register_view
736734
def register_view(self, name: str, df: DataFrame) -> None:
737735
"""Register a :py:class: `~datafusion.detaframe.DataFrame` as a view.
738736
737+
The DataFrame is converted into a view before registration so it can be
738+
referenced from SQL without materializing results immediately.
739+
739740
Args:
740741
name (str): The name to register the view under.
741742
df (DataFrame): The DataFrame to be converted into a view and registered.

src/dataframe.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::collections::HashMap;
1919
use std::ffi::CString;
2020
use std::sync::Arc;
2121

22-
use crate::datafusion_expr::Expr;
2322
use arrow::array::{new_null_array, RecordBatch, RecordBatchIterator, RecordBatchReader};
2423
use arrow::compute::can_cast_types;
2524
use arrow::error::ArrowError;
@@ -474,19 +473,7 @@ impl PyDataFrame {
474473
}
475474

476475
fn with_column(&self, name: &str, expr: PyExpr) -> PyDataFusionResult<Self> {
477-
let expr: Expr = expr.into();
478-
let aliased = expr.alias(name);
479-
480-
let df_schema = self.df.as_ref().schema().clone();
481-
let mut proj_exprs: Vec<Expr> = df_schema
482-
.fields()
483-
.iter()
484-
.filter(|field| field.name() != name)
485-
.map(|field| col(field.name()))
486-
.collect();
487-
proj_exprs.push(aliased);
488-
489-
let df = self.df.as_ref().clone().select(proj_exprs)?;
476+
let df = self.df.as_ref().clone().with_column(name, expr.into())?;
490477
Ok(Self::new(df))
491478
}
492479

0 commit comments

Comments
 (0)