Skip to content

Commit

Permalink
nexus: support uuid arrays (#2316)
Browse files Browse the repository at this point in the history
fixes #2315
  • Loading branch information
serprex authored Dec 4, 2024
1 parent 8b66146 commit 1917c81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nexus/peer-postgres/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ fn values_from_row(row: &Row) -> Vec<Value> {
let uuid: Option<Uuid> = row.get(i);
uuid.map(Value::Uuid).unwrap_or(Value::Null)
}
&Type::UUID_ARRAY => {
let uuid: Option<Vec<Uuid>> = row.get(i);
uuid.map(ArrayValue::Uuid)
.map(Value::Array)
.unwrap_or(Value::Null)
}
&Type::INET | &Type::CIDR => {
let s: Option<MaskedIpAddr> = row.get(i);
s.map(Value::IpAddr).unwrap_or(Value::Null)
Expand Down
16 changes: 16 additions & 0 deletions nexus/value/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bytes::{BufMut, Bytes, BytesMut};
use chrono::{DateTime, NaiveDate, NaiveTime, Utc};
use pgwire::types::ToSqlText;
use postgres_types::{IsNull, ToSql, Type};
use uuid::{Uuid, fmt::Hyphenated};

#[derive(Debug, PartialEq, Clone)]
pub enum ArrayValue {
Expand All @@ -21,6 +22,7 @@ pub enum ArrayValue {
Text(Vec<String>),
Binary(Vec<Bytes>),
VarBinary(Vec<Bytes>),
Uuid(Vec<Uuid>),
Date(Vec<NaiveDate>),
Time(Vec<NaiveTime>),
TimeWithTimeZone(Vec<NaiveTime>),
Expand Down Expand Up @@ -94,6 +96,11 @@ impl ArrayValue {
.map(|v| serde_json::Value::String(hex::encode(v)))
.collect(),
),
ArrayValue::Uuid(arr) => serde_json::Value::Array(
arr.iter()
.map(|v| serde_json::Value::String(v.to_string()))
.collect(),
),
ArrayValue::Date(arr) => serde_json::Value::Array(
arr.iter()
.map(|&v| serde_json::Value::String(v.to_string()))
Expand Down Expand Up @@ -151,6 +158,7 @@ impl ToSql for ArrayValue {
ArrayValue::Text(arr) => arr.to_sql(ty, out)?,
ArrayValue::Binary(_arr) => todo!("support encoding array of binary"),
ArrayValue::VarBinary(_arr) => todo!("support encoding array of varbinary"),
ArrayValue::Uuid(arr) => arr.to_sql(ty, out)?,
ArrayValue::Date(arr) => arr.to_sql(ty, out)?,
ArrayValue::Time(arr) => arr.to_sql(ty, out)?,
ArrayValue::TimeWithTimeZone(arr) => arr.to_sql(ty, out)?,
Expand Down Expand Up @@ -229,6 +237,14 @@ impl ToSqlText for ArrayValue {
ArrayValue::Text(arr) => array_to_sql_text!(arr, ty, out),
ArrayValue::Binary(_arr) => todo!("implement encoding array of binary"),
ArrayValue::VarBinary(_arr) => todo!("implement encoding array of varbinary"),
ArrayValue::Uuid(arr) => {
let mut buf = [0u8; Hyphenated::LENGTH];
for v in arr {
out.put_slice(b"'");
out.put_slice(v.hyphenated().encode_lower(&mut buf).as_bytes());
out.put_slice(b"',");
}
}
ArrayValue::Date(arr) => array_to_sql_text!(arr, ty, out),
ArrayValue::Time(arr) => array_to_sql_text!(arr, ty, out),
ArrayValue::TimeWithTimeZone(arr) => array_to_sql_text!(arr, ty, out),
Expand Down

0 comments on commit 1917c81

Please sign in to comment.