Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Improve string representation of datafusion classes #159

Merged
merged 6 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ impl PyCatalog {
))),
}
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"Catalog(schema_names=[{}])",
self.names().join(";")
))
}
}

#[pymethods]
Expand All @@ -97,6 +104,13 @@ impl PyDatabase {
}
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"Database(table_names=[{}])",
Vec::from_iter(self.names()).join(";")
))
}

// register_table
// deregister_table
}
Expand All @@ -119,6 +133,11 @@ impl PyTable {
}
}

fn __repr__(&self) -> PyResult<String> {
let kind = self.kind();
Ok(format!("Table(kind={kind})"))
}

// fn scan
// fn statistics
// fn has_exact_statistics
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ impl PyConfig {
}
Ok(dict.into())
}

fn __repr__(&mut self, py: Python) -> PyResult<String> {
let dict = self.get_all(py);
match dict {
Ok(result) => Ok(format!("Config({result})")),
Err(err) => Ok(format!("Error: {:?}", err.to_string())),
}
}
}

/// Convert a python object to a ScalarValue
Expand Down
8 changes: 8 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,14 @@ impl PySessionContext {
};
Ok(PyDataFrame::new(df))
}

fn __repr__(&self) -> PyResult<String> {
let id = self.session_id();
match id {
Ok(value) => Ok(format!("SessionContext(session_id={value})")),
Err(err) => Ok(format!("Error: {:?}", err.to_string())),
}
}
}

impl PySessionContext {
Expand Down
10 changes: 10 additions & 0 deletions src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ impl PyDataFrame {
})
}

fn __repr__(&self, py: Python) -> PyResult<String> {
let df = self.df.as_ref().clone().limit(0, Some(10))?;
let batches = wait_for_future(py, df.collect())?;
let batches_as_string = pretty::pretty_format_batches(&batches);
match batches_as_string {
Ok(batch) => Ok(format!("DataFrame()\n{batch}")),
Err(err) => Ok(format!("Error: {:?}", err.to_string())),
}
}

/// Returns the schema from the logical plan
fn schema(&self) -> PyArrowType<Schema> {
PyArrowType(self.df.schema().into())
Expand Down
4 changes: 2 additions & 2 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl PyExpr {
expr.into()
}

fn __str__(&self) -> PyResult<String> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed since redundant if implemented the same way, e.g. str(literal(5)) will fall back to __repr__ if __str__ is not implemented)

Ok(format!("{}", self.expr))
fn __repr__(&self) -> PyResult<String> {
Ok(format!("Expr({})", self.expr))
}

fn __add__(&self, rhs: PyExpr) -> PyResult<PyExpr> {
Expand Down
4 changes: 4 additions & 0 deletions src/udaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,8 @@ impl PyAggregateUDF {
let args = args.iter().map(|e| e.expr.clone()).collect();
Ok(self.function.call(args).into())
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("AggregateUDF({})", self.function.name))
}
}
4 changes: 4 additions & 0 deletions src/udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ impl PyScalarUDF {
let args = args.iter().map(|e| e.expr.clone()).collect();
Ok(self.function.call(args).into())
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!("ScalarUDF({})", self.function.name))
}
}