Skip to content

Commit cbc4759

Browse files
committed
refactor: Simplify PySessionConfig and PySessionContext by removing unnecessary display_config handling
- Removed display_config from PySessionConfig, streamlining its structure. - Updated PySessionContext to directly manage display_config, ensuring consistent access across methods. - Adjusted methods in PySessionContext to utilize the new display_config handling, enhancing clarity and maintainability. - Cleaned up code in PyDataFrame to ensure it correctly references the updated display_config.
1 parent 7fa2c7c commit cbc4759

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

src/context.rs

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,11 @@ impl Default for PyDataframeDisplayConfig {
127127
#[derive(Clone, Default)]
128128
pub struct PySessionConfig {
129129
pub config: SessionConfig,
130-
pub display_config: PyDataframeDisplayConfig,
131130
}
132131

133132
impl From<SessionConfig> for PySessionConfig {
134133
fn from(config: SessionConfig) -> Self {
135-
Self {
136-
config,
137-
display_config: PyDataframeDisplayConfig::default(),
138-
}
134+
Self { config }
139135
}
140136
}
141137

@@ -151,10 +147,7 @@ impl PySessionConfig {
151147
}
152148
}
153149

154-
Self {
155-
config,
156-
display_config: PyDataframeDisplayConfig::default(),
157-
}
150+
Self { config }
158151
}
159152

160153
fn with_create_default_catalog_and_schema(&self, enabled: bool) -> Self {
@@ -213,12 +206,6 @@ impl PySessionConfig {
213206
Self::from(self.config.clone().with_repartition_file_min_size(size))
214207
}
215208

216-
fn with_dataframe_display_config(&self, display_config: PyDataframeDisplayConfig) -> Self {
217-
let mut config = self.clone();
218-
config.display_config = display_config;
219-
config
220-
}
221-
222209
fn with_parquet_pruning(&self, enabled: bool) -> Self {
223210
Self::from(self.config.clone().with_parquet_pruning(enabled))
224211
}
@@ -332,6 +319,7 @@ impl PySQLOptions {
332319
#[derive(Clone)]
333320
pub struct PySessionContext {
334321
pub ctx: SessionContext,
322+
pub display_config: PyDataframeDisplayConfig,
335323
}
336324

337325
#[pymethods]
@@ -341,6 +329,7 @@ impl PySessionContext {
341329
pub fn new(
342330
config: Option<PySessionConfig>,
343331
runtime: Option<PyRuntimeEnvBuilder>,
332+
display_config: Option<PyDataframeDisplayConfig>,
344333
) -> PyDataFusionResult<Self> {
345334
let config = if let Some(c) = config {
346335
c.config
@@ -358,22 +347,33 @@ impl PySessionContext {
358347
.with_runtime_env(runtime)
359348
.with_default_features()
360349
.build();
350+
361351
Ok(PySessionContext {
362352
ctx: SessionContext::new_with_state(session_state),
353+
display_config: display_config.unwrap_or_default(),
363354
})
364355
}
365356

366357
pub fn enable_url_table(&self) -> PyResult<Self> {
367358
Ok(PySessionContext {
368359
ctx: self.ctx.clone().enable_url_table(),
360+
display_config: self.display_config.clone(),
369361
})
370362
}
371363

364+
pub fn with_display_config(&self, display_config: PyDataframeDisplayConfig) -> Self {
365+
Self {
366+
ctx: self.ctx.clone(),
367+
display_config,
368+
}
369+
}
370+
372371
#[classmethod]
373372
#[pyo3(signature = ())]
374373
fn global_ctx(_cls: &Bound<'_, PyType>) -> PyResult<Self> {
375374
Ok(Self {
376375
ctx: get_global_ctx().clone(),
376+
display_config: PyDataframeDisplayConfig::default(),
377377
})
378378
}
379379

@@ -457,7 +457,7 @@ impl PySessionContext {
457457
pub fn sql(&mut self, query: &str, py: Python) -> PyDataFusionResult<PyDataFrame> {
458458
let result = self.ctx.sql(query);
459459
let df = wait_for_future(py, result)?;
460-
Ok(PyDataFrame::new(df, self.ctx.display_config.clone()))
460+
Ok(PyDataFrame::new(df, self.display_config.clone()))
461461
}
462462

463463
#[pyo3(signature = (query, options=None))]
@@ -474,7 +474,7 @@ impl PySessionContext {
474474
};
475475
let result = self.ctx.sql_with_options(query, options);
476476
let df = wait_for_future(py, result)?;
477-
Ok(PyDataFrame::new(df, self.ctx.display_config.clone()))
477+
Ok(PyDataFrame::new(df, self.display_config.clone()))
478478
}
479479

480480
#[pyo3(signature = (partitions, name=None, schema=None))]
@@ -509,15 +509,15 @@ impl PySessionContext {
509509

510510
let table = wait_for_future(py, self._table(&table_name))?;
511511

512-
let df = PyDataFrame::new(table, self.ctx.display_config.clone());
512+
let df = PyDataFrame::new(table, self.display_config.clone());
513513
Ok(df)
514514
}
515515

516516
/// Create a DataFrame from an existing logical plan
517517
pub fn create_dataframe_from_logical_plan(&mut self, plan: PyLogicalPlan) -> PyDataFrame {
518518
PyDataFrame::new(
519519
DataFrame::new(self.ctx.state(), plan.plan.as_ref().clone()),
520-
self.ctx.display_config.clone(),
520+
self.display_config.clone(),
521521
)
522522
}
523523

@@ -886,18 +886,18 @@ impl PySessionContext {
886886
pub fn table(&self, name: &str, py: Python) -> PyResult<PyDataFrame> {
887887
let x = wait_for_future(py, self.ctx.table(name))
888888
.map_err(|e| PyKeyError::new_err(e.to_string()))?;
889-
Ok(PyDataFrame::new(x, self.ctx.display_config.clone()))
889+
Ok(PyDataFrame::new(x, self.display_config.clone()))
890890
}
891891

892892
pub fn table_exist(&self, name: &str) -> PyDataFusionResult<bool> {
893893
Ok(self.ctx.table_exist(name)?)
894894
}
895895

896896
pub fn empty_table(&self) -> PyDataFusionResult<PyDataFrame> {
897-
Ok(
898-
PyDataFrame::new(self.ctx.read_empty()?),
899-
self.ctx.display_config.clone(),
900-
)
897+
Ok(PyDataFrame::new(
898+
self.ctx.read_empty()?,
899+
self.display_config.clone(),
900+
))
901901
}
902902

903903
pub fn session_id(&self) -> String {
@@ -932,7 +932,7 @@ impl PySessionContext {
932932
let result = self.ctx.read_json(path, options);
933933
wait_for_future(py, result)?
934934
};
935-
Ok(PyDataFrame::new(df, self.ctx.display_config.clone()))
935+
Ok(PyDataFrame::new(df, self.display_config.clone()))
936936
}
937937

938938
#[allow(clippy::too_many_arguments)]
@@ -977,18 +977,12 @@ impl PySessionContext {
977977
let paths = path.extract::<Vec<String>>()?;
978978
let paths = paths.iter().map(|p| p as &str).collect::<Vec<&str>>();
979979
let result = self.ctx.read_csv(paths, options);
980-
let df = PyDataFrame::new(
981-
wait_for_future(py, result)?,
982-
self.ctx.display_config.clone(),
983-
);
980+
let df = PyDataFrame::new(wait_for_future(py, result)?, self.display_config.clone());
984981
Ok(df)
985982
} else {
986983
let path = path.extract::<String>()?;
987984
let result = self.ctx.read_csv(path, options);
988-
let df = PyDataFrame::new(
989-
wait_for_future(py, result)?,
990-
self.ctx.display_config.clone(),
991-
);
985+
let df = PyDataFrame::new(wait_for_future(py, result)?, self.display_config.clone());
992986
Ok(df)
993987
}
994988
}
@@ -1026,10 +1020,7 @@ impl PySessionContext {
10261020
.collect();
10271021

10281022
let result = self.ctx.read_parquet(path, options);
1029-
let df = PyDataFrame::new(
1030-
wait_for_future(py, result)?,
1031-
self.ctx.display_config.clone(),
1032-
);
1023+
let df = PyDataFrame::new(wait_for_future(py, result)?, self.display_config.clone());
10331024
Ok(df)
10341025
}
10351026

@@ -1054,12 +1045,12 @@ impl PySessionContext {
10541045
let read_future = self.ctx.read_avro(path, options);
10551046
wait_for_future(py, read_future)?
10561047
};
1057-
Ok(PyDataFrame::new(df, self.ctx.display_config.clone()))
1048+
Ok(PyDataFrame::new(df, self.display_config.clone()))
10581049
}
10591050

10601051
pub fn read_table(&self, table: &PyTable) -> PyDataFusionResult<PyDataFrame> {
10611052
let df = self.ctx.read_table(table.table())?;
1062-
Ok(PyDataFrame::new(df, self.ctx.display_config.clone()))
1053+
Ok(PyDataFrame::new(df, self.display_config.clone()))
10631054
}
10641055

10651056
fn __repr__(&self) -> PyResult<String> {
@@ -1175,6 +1166,9 @@ impl From<PySessionContext> for SessionContext {
11751166

11761167
impl From<SessionContext> for PySessionContext {
11771168
fn from(ctx: SessionContext) -> PySessionContext {
1178-
PySessionContext { ctx }
1169+
PySessionContext {
1170+
ctx,
1171+
display_config: PyDataframeDisplayConfig::default(),
1172+
}
11791173
}
11801174
}

src/dataframe.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ impl PyDataFrame {
118118
}
119119

120120
fn __repr__(&self, py: Python) -> PyDataFusionResult<String> {
121-
// Get display configuration values
122-
let min_rows = self.display_config.min_table_rows;
123-
let max_rows = self.display_config.max_table_rows_in_repr;
124-
let max_bytes = self.display_config.max_table_bytes;
125-
126121
// Collect record batches for display
127122
let (batches, has_more) = wait_for_future(
128123
py,
@@ -605,7 +600,7 @@ impl PyDataFrame {
605600
/// Calculate the exception of two `DataFrame`s. The two `DataFrame`s must have exactly the same schema
606601
fn except_all(&self, py_df: PyDataFrame) -> PyDataFusionResult<Self> {
607602
let new_df = self.df.as_ref().clone().except(py_df.df.as_ref().clone())?;
608-
Ok(Self::new(new_df))
603+
Ok(Self::new(new_df. self.display_config))
609604
}
610605

611606
/// Write a `DataFrame` to a CSV file.

0 commit comments

Comments
 (0)