-
Notifications
You must be signed in to change notification settings - Fork 22
[ISSUE-153] Add blocking poll into python bindings #154
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,9 @@ | |
| use crate::TOKIO_RUNTIME; | ||
| use crate::*; | ||
| use arrow::array::RecordBatch; | ||
| use arrow_pyarrow::FromPyArrow; | ||
| use arrow_pyarrow::{FromPyArrow, ToPyArrow}; | ||
| use fluss::client::EARLIEST_OFFSET; | ||
| use fluss::record::to_arrow_schema; | ||
| use fluss::rpc::message::OffsetSpec; | ||
| use pyo3_async_runtimes::tokio::future_into_py; | ||
| use std::sync::Arc; | ||
|
|
@@ -321,6 +322,59 @@ impl LogScanner { | |
| Ok(df) | ||
| } | ||
|
|
||
| /// Poll for new records with the specified timeout | ||
| /// | ||
| /// Args: | ||
| /// timeout_ms: Timeout in milliseconds to wait for records | ||
| /// | ||
| /// Returns: | ||
| /// PyArrow Table containing the polled records | ||
| /// | ||
| /// Note: | ||
| /// - Returns an empty table (with correct schema) if no records are available | ||
| /// - When timeout expires, returns an empty table (NOT an error) | ||
| fn poll(&self, py: Python, timeout_ms: i64) -> PyResult<Py<PyAny>> { | ||
| use std::time::Duration; | ||
|
||
|
|
||
| if timeout_ms < 0 { | ||
| return Err(FlussError::new_err(format!( | ||
| "timeout_ms must be non-negative, got: {timeout_ms}" | ||
| ))); | ||
| } | ||
|
|
||
| let timeout = Duration::from_millis(timeout_ms as u64); | ||
| let scan_records = py | ||
| .detach(|| TOKIO_RUNTIME.block_on(async { self.inner.poll(timeout).await })) | ||
|
Contributor
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. actually, I'm thinking can we use
Contributor
Author
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. makes sense, thank you! |
||
| .map_err(|e| FlussError::new_err(e.to_string()))?; | ||
|
|
||
| // Convert records to Arrow batches per bucket | ||
| let mut arrow_batches = Vec::new(); | ||
| for (_bucket, records) in scan_records.into_records_by_buckets() { | ||
| let mut batches = Utils::convert_scan_records_to_arrow(records); | ||
| arrow_batches.append(&mut batches); | ||
| } | ||
| if arrow_batches.is_empty() { | ||
| return self.create_empty_table(py); | ||
| } | ||
|
|
||
| Utils::combine_batches_to_table(py, arrow_batches) | ||
| } | ||
|
|
||
| /// Create an empty PyArrow table with the correct schema | ||
| fn create_empty_table(&self, py: Python) -> PyResult<Py<PyAny>> { | ||
| let arrow_schema = to_arrow_schema(self.table_info.get_row_type()); | ||
| let py_schema = arrow_schema | ||
| .to_pyarrow(py) | ||
| .map_err(|e| FlussError::new_err(format!("Failed to convert schema: {e}")))?; | ||
|
|
||
| let pyarrow = py.import("pyarrow")?; | ||
| let empty_table = pyarrow | ||
| .getattr("Table")? | ||
| .call_method1("from_batches", (vec![] as Vec<Py<PyAny>>, py_schema))?; | ||
|
|
||
| Ok(empty_table.into()) | ||
| } | ||
|
|
||
| fn __repr__(&self) -> String { | ||
| format!("LogScanner(table={})", self.table_info.table_path) | ||
| } | ||
|
|
||
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.
The example calls
subscribe()again before polling, but there's no explanation of why this is necessary or whether it resets the scanner state. Consider adding a comment explaining this step, similar to line 163 which says '# Reset subscription'.