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

feat: binding interval convert to python timedelta #567

Merged
merged 3 commits into from
Jan 7, 2025
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
29 changes: 15 additions & 14 deletions bindings/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,21 @@ asyncio.run(main())

### General Data Types

| Databend | Python |
| ----------- | ------------------- |
| `BOOLEAN` | `bool` |
| `TINYINT` | `int` |
| `SMALLINT` | `int` |
| `INT` | `int` |
| `BIGINT` | `int` |
| `FLOAT` | `float` |
| `DOUBLE` | `float` |
| `DECIMAL` | `decimal.Decimal` |
| `DATE` | `datetime.date` |
| `TIMESTAMP` | `datetime.datetime` |
| `VARCHAR` | `str` |
| `BINARY` | `bytes` |
| Databend | Python |
|-------------|----------------------|
| `BOOLEAN` | `bool` |
| `TINYINT` | `int` |
| `SMALLINT` | `int` |
| `INT` | `int` |
| `BIGINT` | `int` |
| `FLOAT` | `float` |
| `DOUBLE` | `float` |
| `DECIMAL` | `decimal.Decimal` |
| `DATE` | `datetime.date` |
| `TIMESTAMP` | `datetime.datetime` |
| `INTERVAL` | `datetime.timedelta` |
| `VARCHAR` | `str` |
| `BINARY` | `bytes` |

### Semi-Structured Data Types

Expand Down
11 changes: 9 additions & 2 deletions bindings/python/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::sync::Arc;

use chrono::{NaiveDate, NaiveDateTime};
use chrono::{Duration, NaiveDate, NaiveDateTime};
use once_cell::sync::Lazy;
use pyo3::exceptions::{PyAttributeError, PyException, PyStopAsyncIteration, PyStopIteration};
use pyo3::sync::GILOnceCell;
Expand Down Expand Up @@ -102,7 +102,14 @@ impl<'py> IntoPyObject<'py> for Value {
databend_driver::Value::Variant(s) => s.into_bound_py_any(py)?,
databend_driver::Value::Geometry(s) => s.into_bound_py_any(py)?,
databend_driver::Value::Geography(s) => s.into_bound_py_any(py)?,
databend_driver::Value::Interval(s) => s.into_bound_py_any(py)?,
databend_driver::Value::Interval(s) => {
let value = databend_driver::Interval::from_string(&s).unwrap();
let total_micros = (value.months as i64) * 30 * 86400000000
+ (value.days as i64) * 86400000000
+ value.micros;
let s = Duration::microseconds(total_micros);
s.into_bound_py_any(py)?
}
};
Ok(val)
}
Expand Down
6 changes: 5 additions & 1 deletion bindings/python/tests/asyncio/steps/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import os
from datetime import datetime, date
from datetime import datetime, date, timedelta
from decimal import Decimal

from behave import given, when, then
Expand Down Expand Up @@ -66,6 +66,10 @@ async def _(context):
row = await context.conn.query_row("select to_binary('xyz')")
assert row.values() == (b"xyz",), f"Binary: {row.values()}"

# Interval
row = await context.conn.query_row("select to_interval('1 hours')")
assert row.values() == (timedelta(hours=1),), f"Interval: {row.values()}"

# Decimal
row = await context.conn.query_row("SELECT 15.7563::Decimal(8,4), 2.0+3.0")
assert row.values() == (
Expand Down
6 changes: 5 additions & 1 deletion bindings/python/tests/blocking/steps/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import os
from datetime import datetime, date
from datetime import datetime, date, timedelta
from decimal import Decimal

from behave import given, when, then
Expand Down Expand Up @@ -61,6 +61,10 @@ async def _(context):
row = context.conn.query_row("select to_binary('xyz')")
assert row.values() == (b"xyz",), f"Binary: {row.values()}"

# Interval
row = context.conn.query_row("select to_interval('1 microseconds')")
assert row.values() == (timedelta(microseconds=1),), f"Interval: {row.values()}"

# Decimal
row = context.conn.query_row("SELECT 15.7563::Decimal(8,4), 2.0+3.0")
assert row.values() == (
Expand Down
7 changes: 6 additions & 1 deletion bindings/python/tests/cursor/steps/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import os
from datetime import datetime, date
from datetime import datetime, date, timedelta
from decimal import Decimal

from behave import given, when, then
Expand Down Expand Up @@ -62,6 +62,11 @@ async def _(context):
row = context.cursor.fetchone()
assert row[0] == b"xyz", f"Binary: {row.values()}"

# Interval
context.cursor.execute("select to_interval('1 days')")
row = context.cursor.fetchone()
assert row.values() == (timedelta(1),), f"Interval: {row.values()}"

# Decimal
context.cursor.execute("SELECT 15.7563::Decimal(8,4), 2.0+3.0")
row = context.cursor.fetchone()
Expand Down
Loading