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(bindings/python): support convert Date,Timestamp to datetime types #389

Merged
merged 7 commits into from
Apr 17, 2024
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
3 changes: 2 additions & 1 deletion bindings/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ name = "databend_driver"
doc = false

[dependencies]
chrono = { version = "0.4.35", default-features = false }
ctor = "0.2.5"
databend-driver = { workspace = true, features = ["rustls", "flight-sql"] }
once_cell = "1.18"
pyo3 = { version = "0.21", features = ["abi3-py37"] }
pyo3 = { version = "0.21", features = ["abi3-py37", "chrono"] }
pyo3-asyncio = { version = "0.21", features = ["tokio-runtime"] }
tokio = "1.34"
tokio-stream = "0.1"
9 changes: 5 additions & 4 deletions bindings/python/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::sync::Arc;

use chrono::{NaiveDate, NaiveDateTime};
use once_cell::sync::Lazy;
use pyo3::exceptions::{PyException, PyStopAsyncIteration, PyStopIteration};
use pyo3::intern;
Expand Down Expand Up @@ -68,12 +69,12 @@ impl IntoPy<PyObject> for Value {
v.into_py(py)
}
databend_driver::Value::Timestamp(_) => {
let s = self.0.to_string();
s.into_py(py)
let t = NaiveDateTime::try_from(self.0).unwrap();
t.into_py(py)
}
databend_driver::Value::Date(_) => {
let s = self.0.to_string();
s.into_py(py)
let d = NaiveDate::try_from(self.0).unwrap();
d.into_py(py)
}
databend_driver::Value::Array(inner) => {
let list = PyList::new_bound(py, inner.into_iter().map(|v| Value(v).into_py(py)));
Expand Down
45 changes: 27 additions & 18 deletions bindings/python/tests/asyncio/steps/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

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

from behave import given, when, then
Expand All @@ -24,7 +25,8 @@
@async_run_until_complete
async def _(context):
dsn = os.getenv(
"TEST_DATABEND_DSN", "databend+http://root:root@localhost:8000/?sslmode=disable"
"TEST_DATABEND_DSN",
"databend+http://root:root@localhost:8000/?sslmode=disable",
)
client = databend_driver.AsyncDatabendClient(dsn)
context.conn = await client.get_conn()
Expand Down Expand Up @@ -54,33 +56,40 @@ async def _(context):
async def _(context, input, output):
row = await context.conn.query_row(f"SELECT '{input}'")
value = row.values()[0]
assert output == value
assert output == value, f"output: {output}"


@then("Select types should be expected native types")
@async_run_until_complete
async def _(context):
# NumberValue::Decimal
row = await context.conn.query_row("SELECT 15.7563::Decimal(8,4), 2.0+3.0")
assert row.values() == (Decimal("15.7563"), Decimal("5.0"))
assert row.values() == (
Decimal("15.7563"),
Decimal("5.0"),
), f"Decimal: {row.values()}"

# Binary
row = await context.conn.query_row("select to_binary('xyz')")
assert row.values() == (b"xyz",)
assert row.values() == (b"xyz",), f"Binary: {row.values()}"

# Array
row = await context.conn.query_row("select [10::Decimal(15,2), 1.1+2.3]")
assert row.values() == ([Decimal("10.00"), Decimal("3.40")],)
assert row.values() == (
[Decimal("10.00"), Decimal("3.40")],
), f"Array: {row.values()}"

# Map
row = await context.conn.query_row("select {'xx':to_date('2020-01-01')}")
assert row.values() == ({"xx": "2020-01-01"},)
assert row.values() == ({"xx": date(2020, 1, 1)},), f"Map: {row.values()}"

# Tuple
row = await context.conn.query_row(
"select (10, '20', to_datetime('2024-04-16 12:34:56.789'))"
)
assert row.values() == ((10, "20", "2024-04-16 12:34:56.789"),)
assert row.values() == (
(10, "20", datetime(2024, 4, 16, 12, 34, 56, 789000)),
), f"Tuple: {row.values()}"


@then("Select numbers should iterate all rows")
Expand All @@ -91,7 +100,7 @@ async def _(context):
async for row in rows:
ret.append(row.values()[0])
expected = [0, 1, 2, 3, 4]
assert ret == expected
assert ret == expected, f"ret: {ret}"


@then("Insert and Select should be equal")
Expand All @@ -110,11 +119,11 @@ async def _(context):
async for row in rows:
ret.append(row.values())
expected = [
(-1, 1, 1.0, "1", "1", "2011-03-06", "2011-03-06 06:20:00"),
(-2, 2, 2.0, "2", "2", "2012-05-31", "2012-05-31 11:20:00"),
(-3, 3, 3.0, "3", "2", "2016-04-04", "2016-04-04 11:30:00"),
(-1, 1, 1.0, "1", "1", date(2011, 3, 6), datetime(2011, 3, 6, 6, 20)),
(-2, 2, 2.0, "2", "2", date(2012, 5, 31), datetime(2012, 5, 31, 11, 20)),
(-3, 3, 3.0, "3", "2", date(2016, 4, 4), datetime(2016, 4, 4, 11, 30)),
]
assert ret == expected
assert ret == expected, f"ret: {ret}"


@then("Stream load and Select should be equal")
Expand All @@ -126,16 +135,16 @@ async def _(context):
["-3", "3", "3.0", "3", "2", "2016-04-04", "2016-04-04T11:30:00Z"],
]
progress = await context.conn.stream_load("INSERT INTO test VALUES", values)
assert progress.write_rows == 3
assert progress.write_bytes == 185
assert progress.write_rows == 3, f"progress.write_rows: {progress.write_rows}"
assert progress.write_bytes == 185, f"progress.write_bytes: {progress.write_bytes}"

rows = await context.conn.query_iter("SELECT * FROM test")
ret = []
async for row in rows:
ret.append(row.values())
expected = [
(-1, 1, 1.0, "1", "1", "2011-03-06", "2011-03-06 06:20:00"),
(-2, 2, 2.0, "2", "2", "2012-05-31", "2012-05-31 11:20:00"),
(-3, 3, 3.0, "3", "2", "2016-04-04", "2016-04-04 11:30:00"),
(-1, 1, 1.0, "1", "1", date(2011, 3, 6), datetime(2011, 3, 6, 6, 20)),
(-2, 2, 2.0, "2", "2", date(2012, 5, 31), datetime(2012, 5, 31, 11, 20)),
(-3, 3, 3.0, "3", "2", date(2016, 4, 4), datetime(2016, 4, 4, 11, 30)),
]
assert ret == expected
assert ret == expected, f"ret: {ret}"
45 changes: 27 additions & 18 deletions bindings/python/tests/blocking/steps/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

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

from behave import given, when, then
Expand All @@ -22,7 +23,8 @@
@given("A new Databend Driver Client")
def _(context):
dsn = os.getenv(
"TEST_DATABEND_DSN", "databend+http://root:root@localhost:8000/?sslmode=disable"
"TEST_DATABEND_DSN",
"databend+http://root:root@localhost:8000/?sslmode=disable",
)
client = databend_driver.BlockingDatabendClient(dsn)
context.conn = client.get_conn()
Expand Down Expand Up @@ -50,32 +52,39 @@ def _(context):
def _(context, input, output):
row = context.conn.query_row(f"SELECT '{input}'")
value = row.values()[0]
assert output == value
assert output == value, f"output: {output}"


@then("Select types should be expected native types")
async def _(context):
# NumberValue::Decimal
row = context.conn.query_row("SELECT 15.7563::Decimal(8,4), 2.0+3.0")
assert row.values() == (Decimal("15.7563"), Decimal("5.0"))
assert row.values() == (
Decimal("15.7563"),
Decimal("5.0"),
), f"Decimal: {row.values()}"

# Binary
row = context.conn.query_row("select to_binary('xyz')")
assert row.values() == (b"xyz",)
assert row.values() == (b"xyz",), f"Binary: {row.values()}"

# Array
row = context.conn.query_row("select [10::Decimal(15,2), 1.1+2.3]")
assert row.values() == ([Decimal("10.00"), Decimal("3.40")],)
assert row.values() == (
[Decimal("10.00"), Decimal("3.40")],
), f"Array: {row.values()}"

# Map
row = context.conn.query_row("select {'xx':to_date('2020-01-01')}")
assert row.values() == ({"xx": "2020-01-01"},)
assert row.values() == ({"xx": date(2020, 1, 1)},), f"Map: {row.values()}"

# Tuple
row = context.conn.query_row(
"select (10, '20', to_datetime('2024-04-16 12:34:56.789'))"
)
assert row.values() == ((10, "20", "2024-04-16 12:34:56.789"),)
assert row.values() == (
(10, "20", datetime(2024, 4, 16, 12, 34, 56, 789000)),
), f"Tuple: {row.values()}"


@then("Select numbers should iterate all rows")
Expand All @@ -85,7 +94,7 @@ def _(context):
for row in rows:
ret.append(row.values()[0])
expected = [0, 1, 2, 3, 4]
assert ret == expected
assert ret == expected, f"ret: {ret}"


@then("Insert and Select should be equal")
Expand All @@ -103,11 +112,11 @@ def _(context):
for row in rows:
ret.append(row.values())
expected = [
(-1, 1, 1.0, "1", "1", "2011-03-06", "2011-03-06 06:20:00"),
(-2, 2, 2.0, "2", "2", "2012-05-31", "2012-05-31 11:20:00"),
(-3, 3, 3.0, "3", "2", "2016-04-04", "2016-04-04 11:30:00"),
(-1, 1, 1.0, "1", "1", date(2011, 3, 6), datetime(2011, 3, 6, 6, 20)),
(-2, 2, 2.0, "2", "2", date(2012, 5, 31), datetime(2012, 5, 31, 11, 20)),
(-3, 3, 3.0, "3", "2", date(2016, 4, 4), datetime(2016, 4, 4, 11, 30)),
]
assert ret == expected
assert ret == expected, f"ret: {ret}"


@then("Stream load and Select should be equal")
Expand All @@ -118,16 +127,16 @@ def _(context):
["-3", "3", "3.0", "3", "2", "2016-04-04", "2016-04-04T11:30:00Z"],
]
progress = context.conn.stream_load("INSERT INTO test VALUES", values)
assert progress.write_rows == 3
assert progress.write_bytes == 185
assert progress.write_rows == 3, f"progress.write_rows: {progress.write_rows}"
assert progress.write_bytes == 185, f"progress.write_bytes: {progress.write_bytes}"

rows = context.conn.query_iter("SELECT * FROM test")
ret = []
for row in rows:
ret.append(row.values())
expected = [
(-1, 1, 1.0, "1", "1", "2011-03-06", "2011-03-06 06:20:00"),
(-2, 2, 2.0, "2", "2", "2012-05-31", "2012-05-31 11:20:00"),
(-3, 3, 3.0, "3", "2", "2016-04-04", "2016-04-04 11:30:00"),
(-1, 1, 1.0, "1", "1", date(2011, 3, 6), datetime(2011, 3, 6, 6, 20)),
(-2, 2, 2.0, "2", "2", date(2012, 5, 31), datetime(2012, 5, 31, 11, 20)),
(-3, 3, 3.0, "3", "2", date(2016, 4, 4), datetime(2016, 4, 4, 11, 30)),
]
assert ret == expected
assert ret == expected, f"ret: {ret}"
15 changes: 6 additions & 9 deletions sql/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ impl TryFrom<(&DataType, &str)> for Value {
Ok(Self::Number(d))
}
DataType::Timestamp => Ok(Self::Timestamp(
chrono::NaiveDateTime::parse_from_str(v, "%Y-%m-%d %H:%M:%S%.6f")?
NaiveDateTime::parse_from_str(v, "%Y-%m-%d %H:%M:%S%.6f")?
.and_utc()
.timestamp_micros(),
)),
DataType::Date => Ok(Self::Date(
chrono::NaiveDate::parse_from_str(v, "%Y-%m-%d")?.num_days_from_ce() - DAYS_FROM_CE,
NaiveDate::parse_from_str(v, "%Y-%m-%d")?.num_days_from_ce() - DAYS_FROM_CE,
)),
DataType::Bitmap => Ok(Self::Bitmap(v.to_string())),
DataType::Variant => Ok(Self::Variant(v.to_string())),
Expand Down Expand Up @@ -540,8 +540,7 @@ impl TryFrom<Value> for NaiveDateTime {
Value::Timestamp(i) => {
let secs = i / 1_000_000;
let nanos = ((i % 1_000_000) * 1000) as u32;
let t = DateTime::from_timestamp(secs, nanos);
match t {
match DateTime::from_timestamp(secs, nanos) {
Some(t) => Ok(t.naive_utc()),
None => Err(ConvertError::new("NaiveDateTime", "".to_string()).into()),
}
Expand All @@ -557,8 +556,7 @@ impl TryFrom<Value> for NaiveDate {
match val {
Value::Date(i) => {
let days = i + DAYS_FROM_CE;
let d = NaiveDate::from_num_days_from_ce_opt(days);
match d {
match NaiveDate::from_num_days_from_ce_opt(days) {
Some(d) => Ok(d),
None => Err(ConvertError::new("NaiveDate", "".to_string()).into()),
}
Expand Down Expand Up @@ -1102,16 +1100,15 @@ impl ValueDecoder {
let mut buf = Vec::new();
reader.read_quoted_text(&mut buf, b'\'')?;
let v = unsafe { std::str::from_utf8_unchecked(&buf) };
let days =
chrono::NaiveDate::parse_from_str(v, "%Y-%m-%d")?.num_days_from_ce() - DAYS_FROM_CE;
let days = NaiveDate::parse_from_str(v, "%Y-%m-%d")?.num_days_from_ce() - DAYS_FROM_CE;
Ok(Value::Date(days))
}

fn read_timestamp<R: AsRef<[u8]>>(&self, reader: &mut Cursor<R>) -> Result<Value> {
let mut buf = Vec::new();
reader.read_quoted_text(&mut buf, b'\'')?;
let v = unsafe { std::str::from_utf8_unchecked(&buf) };
let ts = chrono::NaiveDateTime::parse_from_str(v, "%Y-%m-%d %H:%M:%S%.6f")?
let ts = NaiveDateTime::parse_from_str(v, "%Y-%m-%d %H:%M:%S%.6f")?
.and_utc()
.timestamp_micros();
Ok(Value::Timestamp(ts))
Expand Down
Loading