Skip to content

fix:Add support for TimeDelta type in Python and Rust #368

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/docs/core/data_types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This is the list of all basic types supported by CocoIndex:
| Time | | `datetime.time` | `datetime.time` |
| LocalDatetime | Date and time without timezone | `cocoindex.typing.LocalDateTime` | `datetime.datetime` |
| OffsetDatetime | Date and time with a timezone offset | `cocoindex.typing.OffsetDateTime` | `datetime.datetime` |
| TimeDelta | A duration of time | `cocoindex.typing.TimeDelta` | `datetime.timedelta` |
| Vector[*type*, *N*?] | |`Annotated[list[type], cocoindex.typing.Vector(dim=N)]` | `list[type]` |
| Json | | `cocoindex.typing.Json` | Any type convertible to JSON by `json` package |

Expand Down
3 changes: 3 additions & 0 deletions python/cocoindex/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, key: str, value: Any):
Json = Annotated[Any, TypeKind('Json')]
LocalDateTime = Annotated[datetime.datetime, TypeKind('LocalDateTime')]
OffsetDateTime = Annotated[datetime.datetime, TypeKind('OffsetDateTime')]
TimeDelta = Annotated[datetime.timedelta, TypeKind('TimeDelta')]

COLLECTION_TYPES = ('Table', 'List')

Expand Down Expand Up @@ -142,6 +143,8 @@ def analyze_type_info(t) -> AnalyzedTypeInfo:
kind = 'Time'
elif t is datetime.datetime:
kind = 'OffsetDateTime'
elif t is datetime.timedelta:
kind = 'TimeDelta'
else:
raise ValueError(f"type unsupported yet: {t}")

Expand Down
4 changes: 4 additions & 0 deletions src/base/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub enum BasicValueType {
/// Date and time with timezone.
OffsetDateTime,

/// A time duration.
TimeDelta,

/// A JSON value.
Json,

Expand All @@ -72,6 +75,7 @@ impl std::fmt::Display for BasicValueType {
BasicValueType::Time => write!(f, "time"),
BasicValueType::LocalDateTime => write!(f, "local_datetime"),
BasicValueType::OffsetDateTime => write!(f, "offset_datetime"),
BasicValueType::TimeDelta => write!(f, "timedelta"),
BasicValueType::Json => write!(f, "json"),
BasicValueType::Vector(s) => write!(
f,
Expand Down
13 changes: 13 additions & 0 deletions src/base/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::Result;
use base64::prelude::*;
use chrono::Offset;
use log::warn;
use pyo3::pyclass;
use serde::{
de::{SeqAccess, Visitor},
ser::{SerializeMap, SerializeSeq, SerializeTuple},
Expand Down Expand Up @@ -354,6 +355,7 @@ pub enum BasicValue {
Time(chrono::NaiveTime),
LocalDateTime(chrono::NaiveDateTime),
OffsetDateTime(chrono::DateTime<chrono::FixedOffset>),
TimeDelta(chrono::Duration),
Json(Arc<serde_json::Value>),
Vector(Arc<[BasicValue]>),
}
Expand Down Expand Up @@ -436,6 +438,12 @@ impl From<chrono::DateTime<chrono::FixedOffset>> for BasicValue {
}
}

impl From<chrono::Duration> for BasicValue {
fn from(value: chrono::Duration) -> Self {
BasicValue::TimeDelta(value)
}
}

impl From<serde_json::Value> for BasicValue {
fn from(value: serde_json::Value) -> Self {
BasicValue::Json(Arc::from(value))
Expand Down Expand Up @@ -465,6 +473,7 @@ impl BasicValue {
| BasicValue::Time(_)
| BasicValue::LocalDateTime(_)
| BasicValue::OffsetDateTime(_)
| BasicValue::TimeDelta(_)
| BasicValue::Json(_)
| BasicValue::Vector(_) => api_bail!("invalid key value type"),
};
Expand All @@ -485,6 +494,7 @@ impl BasicValue {
| BasicValue::Time(_)
| BasicValue::LocalDateTime(_)
| BasicValue::OffsetDateTime(_)
| BasicValue::TimeDelta(_)
| BasicValue::Json(_)
| BasicValue::Vector(_) => api_bail!("invalid key value type"),
};
Expand All @@ -505,6 +515,7 @@ impl BasicValue {
BasicValue::Time(_) => "time",
BasicValue::LocalDateTime(_) => "local_datetime",
BasicValue::OffsetDateTime(_) => "offset_datetime",
BasicValue::TimeDelta(_) => "timedelta",
BasicValue::Json(_) => "json",
BasicValue::Vector(_) => "vector",
}
Expand Down Expand Up @@ -860,6 +871,7 @@ impl serde::Serialize for BasicValue {
BasicValue::OffsetDateTime(v) => {
serializer.serialize_str(&v.to_rfc3339_opts(chrono::SecondsFormat::AutoSi, true))
}
BasicValue::TimeDelta(v) => serializer.serialize_str(&v.to_string()),
BasicValue::Json(v) => v.serialize(serializer),
BasicValue::Vector(v) => v.serialize(serializer),
}
Expand Down Expand Up @@ -912,6 +924,7 @@ impl BasicValue {
}
}
}
(v, BasicValueType::TimeDelta) => BasicValue::TimeDelta(v.as_duration()?),
(v, BasicValueType::Json) => BasicValue::Json(Arc::from(v)),
(
serde_json::Value::Array(v),
Expand Down
7 changes: 7 additions & 0 deletions src/ops/storages/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ fn bind_value_field<'arg>(
BasicValue::OffsetDateTime(v) => {
builder.push_bind(v);
}
BasicValue::TimeDelta(v) => {
builder.push_bind(v);
}
BasicValue::Json(v) => {
builder.push_bind(sqlx::types::Json(&**v));
}
Expand Down Expand Up @@ -219,6 +222,9 @@ fn from_pg_value(row: &PgRow, field_idx: usize, typ: &ValueType) -> Result<Value
BasicValueType::OffsetDateTime => row
.try_get::<Option<chrono::DateTime<chrono::FixedOffset>>, _>(field_idx)?
.map(BasicValue::OffsetDateTime),
BasicValueType::TimeDelta => row
.try_get::<Option<chrono::Duration>, _>(field_idx)?
.map(BasicValue::TimeDelta),
BasicValueType::Json => row
.try_get::<Option<serde_json::Value>, _>(field_idx)?
.map(|v| BasicValue::Json(Arc::from(v))),
Expand Down Expand Up @@ -701,6 +707,7 @@ fn to_column_type_sql(column_type: &ValueType) -> Cow<'static, str> {
BasicValueType::Time => "time".into(),
BasicValueType::LocalDateTime => "timestamp".into(),
BasicValueType::OffsetDateTime => "timestamp with time zone".into(),
BasicValueType::TimeDelta => "interval".into(),
BasicValueType::Json => "jsonb".into(),
BasicValueType::Vector(vec_schema) => {
if convertible_to_pgvector(vec_schema) {
Expand Down
11 changes: 11 additions & 0 deletions src/py/convert.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bytes::Bytes;
use pyo3::types::{PyList, PyTuple};
use pyo3::types::{PyDateAccess, PyDateTime, PyDelta, PyTimeAccess, PyTzInfoAccess};
use pyo3::IntoPyObjectExt;
use pyo3::{exceptions::PyException, prelude::*};
use pythonize::{depythonize, pythonize};
Expand Down Expand Up @@ -70,6 +71,9 @@ fn basic_value_to_py_object<'py>(
value::BasicValue::Time(v) => v.into_bound_py_any(py)?,
value::BasicValue::LocalDateTime(v) => v.into_bound_py_any(py)?,
value::BasicValue::OffsetDateTime(v) => v.into_bound_py_any(py)?,
value::BasicValue::TimeDelta(v) => {
PyDelta::new(py, 0, v.num_seconds() as i32, v.subsec_micros())?.into_bound_py_any(py)?
}
value::BasicValue::Json(v) => pythonize(py, v).into_py_result()?,
value::BasicValue::Vector(v) => v
.iter()
Expand Down Expand Up @@ -143,6 +147,13 @@ fn basic_value_from_py_object<'py>(
schema::BasicValueType::OffsetDateTime => {
value::BasicValue::OffsetDateTime(v.extract::<chrono::DateTime<chrono::FixedOffset>>()?)
}
schema::BasicValueType::TimeDelta => {
let delta = v.extract::<&PyDelta>()?;
value::BasicValue::TimeDelta(
chrono::Duration::seconds(delta.get_days() as i64 * 86400 + delta.get_seconds() as i64)
+ chrono::Duration::microseconds(delta.get_microseconds() as i64),
)
}
schema::BasicValueType::Json => {
value::BasicValue::Json(Arc::from(depythonize::<serde_json::Value>(v)?))
}
Expand Down
Loading