-
Notifications
You must be signed in to change notification settings - Fork 181
feat(arrow): Allow record batches output from read_sql #819
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
Open
chitralverma
wants to merge
3
commits into
sfu-db:main
Choose a base branch
from
chitralverma:allow-record-batches
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−21
Open
Changes from 2 commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,76 @@ use connectorx::{prelude::*, sql::CXQuery}; | |
use fehler::throws; | ||
use libc::uintptr_t; | ||
use pyo3::prelude::*; | ||
use pyo3::pyclass; | ||
use pyo3::{PyAny, Python}; | ||
use std::convert::TryFrom; | ||
use std::sync::Arc; | ||
|
||
/// Python-exposed RecordBatch wrapper | ||
#[pyclass] | ||
pub struct PyRecordBatch(RecordBatch); | ||
|
||
/// Python-exposed iterator over RecordBatches | ||
#[pyclass(unsendable, module = "connectorx")] | ||
pub struct PyRecordBatchIterator(Box<dyn RecordBatchIterator>); | ||
|
||
#[pymethods] | ||
impl PyRecordBatch { | ||
pub fn num_rows(&self) -> usize { | ||
self.0.num_rows() | ||
} | ||
|
||
pub fn num_columns(&self) -> usize { | ||
self.0.num_columns() | ||
} | ||
|
||
#[throws(ConnectorXPythonError)] | ||
pub fn to_ptrs<'py>(&self, py: Python<'py>) -> Bound<'py, PyAny> { | ||
let ptrs = py.allow_threads( | ||
|| -> Result<(Vec<String>, Vec<Vec<(uintptr_t, uintptr_t)>>), ConnectorXPythonError> { | ||
let rbs = vec![self.0.clone()]; | ||
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. is this okay or do you suggest any workarounds?
|
||
Ok(to_ptrs(rbs)) | ||
}, | ||
)?; | ||
let obj: PyObject = ptrs.into_py(py); | ||
obj.into_bound(py) | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyRecordBatchIterator { | ||
#[throws(ConnectorXPythonError)] | ||
fn schema_ptr<'py>(&self, py: Python<'py>) -> Bound<'py, PyAny> { | ||
let (rb, _) = self.0.get_schema(); | ||
let ptrs = py.allow_threads( | ||
|| -> Result<(Vec<String>, Vec<Vec<(uintptr_t, uintptr_t)>>), ConnectorXPythonError> { | ||
let rbs = vec![rb]; | ||
Ok(to_ptrs(rbs)) | ||
}, | ||
)?; | ||
let obj: PyObject = ptrs.into_py(py); | ||
obj.into_bound(py) | ||
} | ||
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { | ||
slf | ||
} | ||
|
||
fn __next__<'py>( | ||
mut slf: PyRefMut<'py, Self>, | ||
py: Python<'py>, | ||
) -> PyResult<Option<Py<PyRecordBatch>>> { | ||
match slf.0.next_batch() { | ||
Some(rb) => { | ||
let wrapped = PyRecordBatch(rb); | ||
let py_obj = Py::new(py, wrapped)?; | ||
Ok(Some(py_obj)) | ||
} | ||
|
||
None => Ok(None), | ||
} | ||
} | ||
} | ||
|
||
#[throws(ConnectorXPythonError)] | ||
pub fn write_arrow<'py>( | ||
py: Python<'py>, | ||
|
@@ -28,6 +94,30 @@ pub fn write_arrow<'py>( | |
obj.into_bound(py) | ||
} | ||
|
||
#[throws(ConnectorXPythonError)] | ||
pub fn get_arrow_rb_iter<'py>( | ||
py: Python<'py>, | ||
source_conn: &SourceConn, | ||
origin_query: Option<String>, | ||
queries: &[CXQuery<String>], | ||
pre_execution_queries: Option<&[String]>, | ||
batch_size: usize, | ||
) -> Bound<'py, PyAny> { | ||
let mut arrow_iter: Box<dyn RecordBatchIterator> = new_record_batch_iter( | ||
source_conn, | ||
origin_query, | ||
queries, | ||
batch_size, | ||
pre_execution_queries, | ||
); | ||
|
||
arrow_iter.prepare(); | ||
let py_rb_iter = PyRecordBatchIterator(arrow_iter); | ||
|
||
let obj: PyObject = py_rb_iter.into_py(py); | ||
obj.into_bound(py) | ||
} | ||
|
||
pub fn to_ptrs(rbs: Vec<RecordBatch>) -> (Vec<String>, Vec<Vec<(uintptr_t, uintptr_t)>>) { | ||
if rbs.is_empty() { | ||
return (vec![], vec![]); | ||
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returns a pyarrow
RecordBatchReader
instead of an iterator/ generator ofRecordBatch
. I guess this will be useful for users who want to get the pyarrowSchema
since RecordBatchReader has it.