Skip to content

Commit f351f3a

Browse files
committed
Add uuid type support in core engine.
1 parent c3a7e50 commit f351f3a

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ log = "0.4.26"
2525
regex = "1.11.1"
2626
serde = { version = "1.0.219", features = ["derive"] }
2727
serde_json = "1.0.140"
28-
sqlx = { version = "0.8.3", features = ["chrono", "postgres", "runtime-tokio"] }
28+
sqlx = { version = "0.8.3", features = ["chrono", "postgres", "runtime-tokio", "uuid"] }
2929
tokio = { version = "1.44.1", features = [
3030
"macros",
3131
"rt-multi-thread",
@@ -89,3 +89,4 @@ rustls = { version = "0.23.25" }
8989
http-body-util = "0.1.3"
9090
yaml-rust2 = "0.10.0"
9191
urlencoding = "2.1.3"
92+
uuid = "1.16.0"

src/base/json_schema.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ impl ToJsonSchema for schema::BasicValueType {
2626
schema::BasicValueType::Float32 | schema::BasicValueType::Float64 => {
2727
schema.instance_type = Some(SingleOrVec::Single(Box::new(InstanceType::Number)));
2828
}
29-
schema::BasicValueType::Json => {
30-
// Can be any value. No type constraint.
31-
}
3229
schema::BasicValueType::Range => {
3330
schema.instance_type = Some(SingleOrVec::Single(Box::new(InstanceType::Array)));
3431
schema.array = Some(Box::new(ArrayValidation {
@@ -51,6 +48,13 @@ impl ToJsonSchema for schema::BasicValueType {
5148
.description =
5249
Some("A range, start pos (inclusive), end pos (exclusive).".to_string());
5350
}
51+
schema::BasicValueType::Uuid => {
52+
schema.instance_type = Some(SingleOrVec::Single(Box::new(InstanceType::String)));
53+
schema.format = Some("uuid".to_string());
54+
}
55+
schema::BasicValueType::Json => {
56+
// Can be any value. No type constraint.
57+
}
5458
schema::BasicValueType::Vector(s) => {
5559
schema.instance_type = Some(SingleOrVec::Single(Box::new(InstanceType::Array)));
5660
schema.array = Some(Box::new(ArrayValidation {

src/base/schema.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub enum BasicValueType {
3535
/// A range, with a start offset and a length.
3636
Range,
3737

38+
/// A UUID.
39+
Uuid,
40+
3841
/// A JSON value.
3942
Json,
4043

@@ -52,6 +55,7 @@ impl std::fmt::Display for BasicValueType {
5255
BasicValueType::Float32 => write!(f, "float32"),
5356
BasicValueType::Float64 => write!(f, "float64"),
5457
BasicValueType::Range => write!(f, "range"),
58+
BasicValueType::Uuid => write!(f, "uuid"),
5559
BasicValueType::Json => write!(f, "json"),
5660
BasicValueType::Vector(s) => write!(
5761
f,

src/base/value.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use serde::{
99
Deserialize, Serialize,
1010
};
1111
use std::{collections::BTreeMap, ops::Deref, sync::Arc};
12+
use uuid::Uuid;
1213

1314
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
1415
pub struct RangeValue {
@@ -76,6 +77,7 @@ pub enum KeyValue {
7677
Bool(bool),
7778
Int64(i64),
7879
Range(RangeValue),
80+
Uuid(Uuid),
7981
Struct(Vec<KeyValue>),
8082
}
8183

@@ -141,6 +143,7 @@ impl std::fmt::Display for KeyValue {
141143
KeyValue::Bool(v) => write!(f, "{}", v),
142144
KeyValue::Int64(v) => write!(f, "{}", v),
143145
KeyValue::Range(v) => write!(f, "[{}, {})", v.start, v.end),
146+
KeyValue::Uuid(v) => write!(f, "{}", v),
144147
KeyValue::Struct(v) => {
145148
write!(
146149
f,
@@ -205,6 +208,7 @@ impl KeyValue {
205208
output.push(v.start.to_string());
206209
output.push(v.end.to_string());
207210
}
211+
KeyValue::Uuid(v) => output.push(v.to_string()),
208212
KeyValue::Struct(v) => {
209213
for part in v {
210214
part.parts_to_strs(output);
@@ -235,6 +239,7 @@ impl KeyValue {
235239
KeyValue::Bool(_) => "bool",
236240
KeyValue::Int64(_) => "int64",
237241
KeyValue::Range { .. } => "range",
242+
KeyValue::Uuid(_) => "uuid",
238243
KeyValue::Struct(_) => "struct",
239244
}
240245
}
@@ -299,6 +304,7 @@ pub enum BasicValue {
299304
Float32(f32),
300305
Float64(f64),
301306
Range(RangeValue),
307+
Uuid(Uuid),
302308
Json(Arc<serde_json::Value>),
303309
Vector(Arc<[BasicValue]>),
304310
}
@@ -373,6 +379,7 @@ impl BasicValue {
373379
BasicValue::Bool(v) => KeyValue::Bool(v),
374380
BasicValue::Int64(v) => KeyValue::Int64(v),
375381
BasicValue::Range(v) => KeyValue::Range(v),
382+
BasicValue::Uuid(v) => KeyValue::Uuid(v),
376383
BasicValue::Float32(_)
377384
| BasicValue::Float64(_)
378385
| BasicValue::Json(_)
@@ -388,6 +395,7 @@ impl BasicValue {
388395
BasicValue::Bool(v) => KeyValue::Bool(*v),
389396
BasicValue::Int64(v) => KeyValue::Int64(*v),
390397
BasicValue::Range(v) => KeyValue::Range(*v),
398+
BasicValue::Uuid(v) => KeyValue::Uuid(*v),
391399
BasicValue::Float32(_)
392400
| BasicValue::Float64(_)
393401
| BasicValue::Json(_)
@@ -405,6 +413,7 @@ impl BasicValue {
405413
BasicValue::Float32(_) => "float32",
406414
BasicValue::Float64(_) => "float64",
407415
BasicValue::Range(_) => "range",
416+
BasicValue::Uuid(_) => "uuid",
408417
BasicValue::Json(_) => "json",
409418
BasicValue::Vector(_) => "vector",
410419
}
@@ -436,6 +445,7 @@ impl From<KeyValue> for Value {
436445
KeyValue::Bool(v) => Value::Basic(BasicValue::Bool(v)),
437446
KeyValue::Int64(v) => Value::Basic(BasicValue::Int64(v)),
438447
KeyValue::Range(v) => Value::Basic(BasicValue::Range(v)),
448+
KeyValue::Uuid(v) => Value::Basic(BasicValue::Uuid(v)),
439449
KeyValue::Struct(v) => Value::Struct(FieldValues {
440450
fields: v.into_iter().map(Value::from).collect(),
441451
}),
@@ -734,6 +744,7 @@ impl serde::Serialize for BasicValue {
734744
BasicValue::Float32(v) => serializer.serialize_f32(*v),
735745
BasicValue::Float64(v) => serializer.serialize_f64(*v),
736746
BasicValue::Range(v) => v.serialize(serializer),
747+
BasicValue::Uuid(v) => serializer.serialize_str(&v.to_string()),
737748
BasicValue::Json(v) => v.serialize(serializer),
738749
BasicValue::Vector(v) => v.serialize(serializer),
739750
}

src/execution/query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl SimpleSemanticsQueryHandler {
8787
| value::BasicValue::Str(_)
8888
| value::BasicValue::Bool(_)
8989
| value::BasicValue::Range(_)
90+
| value::BasicValue::Uuid(_)
9091
| value::BasicValue::Json(_)
9192
| value::BasicValue::Vector(_) => {
9293
bail!("Query results is not a vector of number")

src/ops/storages/postgres.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use serde::Serialize;
2121
use sqlx::postgres::types::PgRange;
2222
use sqlx::postgres::PgRow;
2323
use sqlx::{PgPool, Row};
24+
use uuid::Uuid;
2425

2526
#[derive(Debug, Deserialize)]
2627
pub struct Spec {
@@ -78,6 +79,9 @@ fn bind_key_field<'arg>(
7879
end: Bound::Excluded(v.end as i64),
7980
});
8081
}
82+
KeyValue::Uuid(v) => {
83+
builder.push_bind(v);
84+
}
8185
KeyValue::Struct(fields) => {
8286
builder.push_bind(sqlx::types::Json(fields));
8387
}
@@ -110,15 +114,18 @@ fn bind_value_field<'arg>(
110114
BasicValue::Float64(v) => {
111115
builder.push_bind(v);
112116
}
113-
BasicValue::Json(v) => {
114-
builder.push_bind(sqlx::types::Json(&**v));
115-
}
116117
BasicValue::Range(v) => {
117118
builder.push_bind(PgRange {
118119
start: Bound::Included(v.start as i64),
119120
end: Bound::Excluded(v.end as i64),
120121
});
121122
}
123+
BasicValue::Uuid(v) => {
124+
builder.push_bind(v);
125+
}
126+
BasicValue::Json(v) => {
127+
builder.push_bind(sqlx::types::Json(&**v));
128+
}
122129
BasicValue::Vector(v) => match &field_schema.value_type.typ {
123130
ValueType::Basic(BasicValueType::Vector(vs)) if convertible_to_pgvector(vs) => {
124131
let vec = v
@@ -186,6 +193,9 @@ fn from_pg_value(row: &PgRow, field_idx: usize, typ: &ValueType) -> Result<Value
186193
_ => anyhow::bail!("invalid range value"),
187194
})
188195
.transpose()?,
196+
BasicValueType::Uuid => row
197+
.try_get::<Option<Uuid>, _>(field_idx)?
198+
.map(BasicValue::Uuid),
189199
BasicValueType::Json => row
190200
.try_get::<Option<serde_json::Value>, _>(field_idx)?
191201
.map(|v| BasicValue::Json(Arc::from(v))),
@@ -655,6 +665,7 @@ fn to_column_type_sql(column_type: &ValueType) -> Cow<'static, str> {
655665
BasicValueType::Float32 => "real".into(),
656666
BasicValueType::Float64 => "double precision".into(),
657667
BasicValueType::Range => "int8range".into(),
668+
BasicValueType::Uuid => "uuid".into(),
658669
BasicValueType::Json => "jsonb".into(),
659670
BasicValueType::Vector(vec_schema) => {
660671
if convertible_to_pgvector(vec_schema) {

0 commit comments

Comments
 (0)