Skip to content

Commit f085b49

Browse files
committed
[HSTACK] - expose unparser plan_sql on DF object
1 parent 4ddfe7a commit f085b49

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

python/datafusion/dataframe.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,9 @@ def count(self) -> int:
808808
def distributed_plan(self):
809809
return self.df.distributed_plan()
810810

811+
def plan_sql(self):
812+
return self.df.plan_sql()
813+
811814
@deprecated("Use :py:func:`unnest_columns` instead.")
812815
def unnest_column(self, column: str, preserve_nulls: bool = True) -> DataFrame:
813816
"""See :py:func:`unnest_columns`."""

src/dataframe.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ use datafusion::execution::SendableRecordBatchStream;
4141
use datafusion::parquet::basic::{BrotliLevel, Compression, GzipLevel, ZstdLevel};
4242
use datafusion::physical_plan::{ExecutionPlan, ExecutionPlanProperties};
4343
use datafusion::prelude::*;
44+
use datafusion::sql::unparser::plan_to_sql;
4445
use datafusion_proto::physical_plan::AsExecutionPlan;
4546
use datafusion_proto::protobuf::PhysicalPlanNode;
4647
use prost::Message;
4748
use pyo3::exceptions::PyValueError;
4849
use pyo3::prelude::*;
4950
use pyo3::pybacked::PyBackedStr;
50-
use pyo3::types::{PyBytes, PyCapsule, PyDict, PyTuple, PyTupleMethods};
51+
use pyo3::types::{PyBytes, PyCapsule, PyDict, PyString, PyTuple, PyTupleMethods};
5152
use tokio::task::JoinHandle;
5253

5354
use crate::catalog::PyTable;
@@ -713,6 +714,13 @@ impl PyDataFrame {
713714
let future_plan = DistributedPlan::try_new(self.df.as_ref());
714715
wait_for_future(py, future_plan).map_err(py_datafusion_err)
715716
}
717+
718+
fn plan_sql(&self, py: Python<'_>) -> PyResult<PyObject> {
719+
let logical_plan = self.df.logical_plan();
720+
721+
let sql = plan_to_sql(logical_plan).map_err(py_datafusion_err)?;
722+
Ok(PyString::new(py, sql.to_string().as_ref()).into())
723+
}
716724
}
717725

718726
#[pyclass(get_all)]

0 commit comments

Comments
 (0)