Skip to content

Commit

Permalink
fix(py): fix incorrect description in cursor (#602)
Browse files Browse the repository at this point in the history
* fix(py): fix incorrect description in cursor

* fix(py): bump chrono
  • Loading branch information
sundy-li authored Feb 27, 2025
1 parent e0240e6 commit 27b1fe8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ databend-driver-core = { path = "sql", version = "0.26.1" }
databend-driver-macros = { path = "macros", version = "0.26.1" }

tokio-stream = "0.1"
chrono = { version = "0.4.35", default-features = false, features = ["clock"] }
chrono = { version = "=0.4.39", default-features = false, features = ["clock"] }
arrow = { version = "53.0" }
arrow-array = { version = "53.0" }
arrow-schema = { version = "53.0" }
Expand Down
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
2 changes: 1 addition & 1 deletion driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ databend-common-ast = "0.2.1"
derive-visitor = { version = "0.4.0", features = ["std-types-drive"] }

async-trait = "0.1"
chrono = { version = "0.4.35", default-features = false, features = ["clock"] }
chrono = { version = "=0.4.39", default-features = false, features = ["clock"] }
csv = "1.3"
glob = "0.3"
log = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ databend-client = { workspace = true }
tokio-stream = { workspace = true }
tonic = { workspace = true, optional = true }

chrono = { version = "0.4.35", default-features = false }
chrono = { version = "=0.4.39", default-features = false }
geozero = { version = "0.14.0", features = ["with-wkb"] }
glob = "0.3"
hex = "0.4.3"
Expand Down

0 comments on commit 27b1fe8

Please sign in to comment.