-
Notifications
You must be signed in to change notification settings - Fork 112
feature: Set table name from ctx functions #260
New issue
Have a question about this 8000 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
Merged
andygrove
merged 2 commits into
apache:main
from
simicd:feature/table-name-in-from-ctx-functions
Mar 7, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -276,23 +276,29 @@ impl PySessionContext { | |
fn create_dataframe( | ||
&mut self, | ||
partitions: PyArrowType<Vec<Vec<RecordBatch>>>, | ||
name: Option<&str>, | ||
py: Python, | ||
) -> PyResult<PyDataFrame> { | ||
let schema = partitions.0[0][0].schema(); | ||
let table = MemTable::try_new(schema, partitions.0).map_err(DataFusionError::from)?; | ||
|
||
// generate a random (unique) name for this table | ||
// generate a random (unique) name for this table if none is provided | ||
// table name cannot start with numeric digit | ||
let name = "c".to_owned() | ||
+ Uuid::new_v4() | ||
.simple() | ||
.encode_lower(&mut Uuid::encode_buffer()); | ||
let table_name = match name { | ||
Some(val) => val.to_owned(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
None => { | ||
"c".to_owned() | ||
+ Uuid::new_v4() | ||
.simple() | ||
.encode_lower(&mut Uuid::encode_buffer()) | ||
} | ||
}; | ||
|
||
self.ctx | ||
.register_table(&*name, Arc::new(table)) | ||
.register_table(&*table_name, Arc::new(table)) | ||
.map_err(DataFusionError::from)?; | ||
|
||
let table = wait_for_future(py, self._table(&name)).map_err(DataFusionError::from)?; | ||
let table = wait_for_future(py, self._table(&table_name)).map_err(DataFusionError::from)?; | ||
|
||
let df = PyDataFrame::new(table); | ||
Ok(df) | ||
|
@@ -305,37 +311,52 @@ impl PySessionContext { | |
|
||
/// Construct datafusion dataframe from Python list | ||
#[allow(clippy::wrong_self_convention)] | ||
fn from_pylist(&mut self, data: PyObject, _py: Python) -> PyResult<PyDataFrame> { | ||
fn from_pylist( | ||
&mut self, | ||
data: PyObject, | ||
name: Option<&str>, | ||
_py: Python, | ||
) -> PyResult<PyDataFrame> { | ||
Python::with_gil(|py| { | ||
// Instantiate pyarrow Table object & convert to Arrow Table | ||
let table_class = py.import("pyarrow")?.getattr("Table")?; | ||
let args = PyTuple::new(py, &[data]); | ||
let table = table_class.call_method1("from_pylist", args)?.into(); | ||
|
||
// Convert Arrow Table to datafusion DataFrame | ||
let df = self.from_arrow_table(table, py)?; | ||
let df = self.from_arrow_table(table, name, py)?; | ||
Ok(df) | ||
}) | ||
} | ||
|
||
/// Construct datafusion dataframe from Python dictionary | ||
#[allow(clippy::wrong_self_convention)] | ||
fn from_pydict(&mut self, data: PyObject, _py: Python) -> PyResult<PyDataFrame> { | ||
fn from_pydict( | ||
&mut self, | ||
data: PyObject, | ||
name: Option<&str>, | ||
_py: Python, | ||
) -> PyResult<PyDataFrame> { | ||
Python::with_gil(|py| { | ||
// Instantiate pyarrow Table object & convert to Arrow Table | ||
let table_class = py.import("pyarrow")?.getattr("Table")?; | ||
let args = PyTuple::new(py, &[data]); | ||
let table = table_class.call_method1("from_pydict", args)?.into(); | ||
|
||
// Convert Arrow Table to datafusion DataFrame | ||
let df = self.from_arrow_table(table, py)?; | ||
let df = self.from_arrow_table(table, name, py)?; | ||
Ok(df) | ||
}) | ||
} | ||
|
||
/// Construct datafusion dataframe from Arrow Table | ||
#[allow(clippy::wrong_self_convention)] | ||
fn from_arrow_table(&mut self, data: PyObject, _py: Python) -> PyResult<PyDataFrame> { | ||
fn from_arrow_table( | ||
&mut self, | ||
data: PyObject, | ||
name: Option<&str>, | ||
_py: Python, | ||
) -> PyResult<PyDataFrame> { | ||
Python::with_gil(|py| { | ||
// Instantiate pyarrow Table object & convert to batches | ||
let table = data.call_method0(py, "to_batches")?; | ||
|
@@ -345,34 +366,44 @@ impl PySessionContext { | |
// here we need to wrap the vector of record batches in an additional vector | ||
let batches = table.extract::<PyArrowType<Vec<RecordBatch>>>(py)?; | ||
let list_of_batches = PyArrowType::try_from(vec![batches.0])?; | ||
self.create_dataframe(list_of_batches, py) | ||
self.create_dataframe(list_of_batches, name, py) | ||
}) | ||
} | ||
|
||
/// Construct datafusion dataframe from pandas | ||
#[allow(clippy::wrong_self_convention)] | ||
fn from_pandas(&mut self, data: PyObject, _py: Python) -> PyResult<PyDataFrame> { | ||
fn from_pandas( | ||
&mut self, | ||
data: PyObject, | ||
name: Option<&str>, | ||
_py: Python, | ||
) -> PyResult<PyDataFrame> { | ||
Python::with_gil(|py| { | ||
// Instantiate pyarrow Table object & convert to Arrow Table | ||
let table_class = py.import("pyarrow")?.getattr("Table")?; | ||
let args = PyTuple::new(py, &[data]); | ||
let table = table_class.call_method1("from_pandas", args)?.into(); | ||
|
||
// Convert Arrow Table to datafusion DataFrame | ||
let df = self.from_arrow_table(table, py)?; | ||
let df = self.from_arrow_table(table, name, py)?; | ||
Ok(df) | ||
}) | ||
} | ||
|
||
/// Construct datafusion dataframe from polars | ||
#[allow(clippy::wrong_self_convention)] | ||
fn from_polars(&mut self, data: PyObject, _py: Python) -> PyResult<PyDataFrame> { | ||
fn from_polars( | ||
&mut self, | ||
data: PyObject, | ||
name: Option<&str>, | ||
_py: Python, | ||
) -> PyResult<PyDataFrame> { | ||
Python::with_gil(|py| { | ||
// Convert Polars dataframe to Arrow Table | ||
let table = data.call_method0(py, "to_arrow")?; | ||
|
||
// Convert Arrow Table to datafusion DataFrame | ||
let df = self.from_arrow_table(table, py)?; | ||
let df = self.from_arrow_table(table, name, py)?; | ||
Ok(df) | ||
}) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
nice!