Skip to content

Commit

Permalink
fix(py): fix incorrect description in cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li committed Feb 27, 2025
1 parent e0240e6 commit 04b354f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
52 changes: 31 additions & 21 deletions bindings/python/src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Arc;

use databend_driver::SchemaRef;
use pyo3::exceptions::{PyAttributeError, PyException, PyStopIteration};
use pyo3::types::{PyList, PyTuple};
use pyo3::{prelude::*, IntoPyObjectExt};
Expand Down Expand Up @@ -200,6 +201,7 @@ pub struct BlockingDatabendCursor {
rows: Option<Arc<Mutex<databend_driver::RowIterator>>>,
// buffer is used to store only the first row after execute
buffer: Vec<Row>,
schema: Option<SchemaRef>,
}

impl BlockingDatabendCursor {
Expand All @@ -208,6 +210,7 @@ impl BlockingDatabendCursor {
conn: Arc::new(conn),
rows: None,
buffer: Vec::new(),
schema: None,
}
}
}
Expand All @@ -216,35 +219,41 @@ impl BlockingDatabendCursor {
fn reset(&mut self) {
self.rows = None;
self.buffer.clear();
self.schema = None;
}
}

#[pymethods]
impl BlockingDatabendCursor {
#[getter]
pub fn description<'p>(&'p self, py: Python<'p>) -> PyResult<PyObject> {
match self.rows {
None => Ok(py.None()),
Some(ref rows) => {
let schema = wait_for_future(py, async move {
let rows = rows.lock().await;
rows.schema()
});
let mut fields = vec![];
for field in schema.fields() {
let field = (
field.name.clone(), // name
field.data_type.to_string(), // type_code
None::<i64>, // display_size
None::<i64>, // internal_size
None::<i64>, // precision
None::<i64>, // scale
None::<bool>, // null_ok
);
fields.push(field.into_pyobject(py)?);
}
PyList::new(py, fields)?.into_py_any(py)
if let Some(ref schema) = self.schema {
let mut fields = vec![];
for field in schema.fields() {
let field = (
field.name.clone(), // name
field.data_type.to_string(), // type_code
None::<i64>, // display_size
None::<i64>, // internal_size
None::<i64>, // precision
None::<i64>, // scale
None::<bool>, // null_ok
);
fields.push(field.into_pyobject(py)?);
}
PyList::new(py, fields)?.into_py_any(py)
} else {
Ok(py.None())
}
}

fn set_schema(&mut self, py: Python) {
if let Some(ref rows) = self.rows {
let schema = wait_for_future(py, async move {
let rows = rows.lock().await;
rows.schema()
});
self.schema = Some(schema)
}
}

Expand Down Expand Up @@ -291,6 +300,7 @@ impl BlockingDatabendCursor {
self.buffer.push(Row::new(first));
}
self.rows = Some(Arc::new(Mutex::new(rows)));
self.set_schema(py);
Ok(py.None())
}

Expand Down
3 changes: 3 additions & 0 deletions bindings/python/tests/cursor/steps/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def _(context):
ret.append(row.values())
assert ret == expected, f"ret: {ret}"

desc = context.cursor.description
assert desc != None

# fetchmany
context.cursor.execute("SELECT * FROM test")
rows = context.cursor.fetchmany(3)
Expand Down

0 comments on commit 04b354f

Please sign in to comment.