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

fix: Use python print for show method #1511

Merged
merged 3 commits into from
Aug 8, 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
2 changes: 2 additions & 0 deletions py-glaredb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod error;
mod logical_plan;
mod runtime;
mod session;
mod util;

use environment::PyEnvironmentReader;
use error::PyGlareDbError;
use futures::lock::Mutex;
Expand Down
5 changes: 3 additions & 2 deletions py-glaredb/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::util::pyprint;
use anyhow::Result;
use arrow_util::pretty::pretty_format_batches;
use datafusion::arrow::record_batch::RecordBatch;
Expand Down Expand Up @@ -143,8 +144,8 @@ fn print_batch(result: &mut ExecutionResult, py: Python<'_>) -> PyResult<()> {

let disp = pretty_format_batches(&batches, None, None, None)
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
println!("{disp}");
Ok(())

pyprint(disp, py)
}
_ => Err(PyRuntimeError::new_err("Not able to show executed result")),
}
Expand Down
11 changes: 11 additions & 0 deletions py-glaredb/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use pyo3::prelude::*;
use pyo3::types::PyDict;
use std::fmt::Display;

/// Use python's builtin `print` to display an item.
pub fn pyprint(item: impl Display, py: Python) -> PyResult<()> {
let locals = PyDict::new(py);
locals.set_item("__display_item", item.to_string())?;
py.run("print(__display_item)", None, Some(locals))?;
Ok(())
}
9 changes: 9 additions & 0 deletions py-glaredb/tests/test_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import glaredb
import pytest


def test_show():
con = glaredb.connect()
# Just make sure we don't error right now. We could error if we reference
# the incorrect local during the print.
con.sql("select 1").show()