-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathencoder.rs
More file actions
308 lines (280 loc) · 10.9 KB
/
encoder.rs
File metadata and controls
308 lines (280 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use crate::pg_server::PgError;
use pgwire::api::portal::Format;
use pgwire::api::results::{DataRowEncoder, FieldInfo};
use pgwire::api::Type;
use spacetimedb_lib::sats::satn::{PsqlChars, PsqlPrintFmt, PsqlType, TypedWriter};
use spacetimedb_lib::sats::{satn, ValueWithType};
use spacetimedb_lib::{
ser, AlgebraicType, AlgebraicValue, ProductType, ProductTypeElement, ProductValue, TimeDuration, Timestamp,
};
use std::borrow::Cow;
use std::sync::Arc;
pub(crate) fn row_desc(schema: &ProductType, format: &Format) -> Arc<Vec<FieldInfo>> {
Arc::new(
schema
.elements
.iter()
.enumerate()
.map(|(pos, ty)| {
let field_name = ty.name.clone().map(Into::into).unwrap_or_else(|| format!("col_{pos}"));
let field_type = type_of(schema, ty);
FieldInfo::new(field_name, None, None, field_type, format.format_for(pos))
})
.collect(),
)
}
pub(crate) fn type_of(schema: &ProductType, ty: &ProductTypeElement) -> Type {
let format = PsqlPrintFmt::use_fmt(schema, ty, ty.name());
match &ty.algebraic_type {
AlgebraicType::String => Type::VARCHAR,
AlgebraicType::Bool => Type::BOOL,
AlgebraicType::U8 | AlgebraicType::I8 | AlgebraicType::I16 => Type::INT2,
AlgebraicType::U16 | AlgebraicType::I32 => Type::INT4,
AlgebraicType::U32 | AlgebraicType::I64 => Type::INT8,
AlgebraicType::U64 | AlgebraicType::I128 | AlgebraicType::U128 | AlgebraicType::I256 | AlgebraicType::U256 => {
Type::NUMERIC
}
AlgebraicType::F32 => Type::FLOAT4,
AlgebraicType::F64 => Type::FLOAT8,
AlgebraicType::Array(ty) => match *ty.elem_ty {
AlgebraicType::String => Type::VARCHAR_ARRAY,
AlgebraicType::Bool => Type::BOOL_ARRAY,
AlgebraicType::U8 => Type::BYTEA,
AlgebraicType::I8 | AlgebraicType::I16 => Type::INT2_ARRAY,
AlgebraicType::U16 | AlgebraicType::I32 => Type::INT4_ARRAY,
AlgebraicType::U32 | AlgebraicType::I64 => Type::INT8_ARRAY,
AlgebraicType::U64
| AlgebraicType::I128
| AlgebraicType::U128
| AlgebraicType::I256
| AlgebraicType::U256 => Type::NUMERIC_ARRAY,
_ => Type::ANYARRAY,
},
AlgebraicType::Product(_) => match format {
PsqlPrintFmt::Hex => Type::BYTEA_ARRAY,
PsqlPrintFmt::Timestamp => Type::TIMESTAMP,
PsqlPrintFmt::Duration => Type::INTERVAL,
_ => Type::JSON,
},
AlgebraicType::Sum(sum) if sum.is_simple_enum() => Type::ANYENUM,
AlgebraicType::Sum(_) => Type::JSON,
_ => Type::UNKNOWN,
}
}
impl ser::Error for PgError {
fn custom<T: std::fmt::Display>(msg: T) -> Self {
PgError::Other(anyhow::anyhow!(msg.to_string()))
}
}
pub(crate) struct PsqlFormatter<'a> {
pub(crate) encoder: &'a mut DataRowEncoder,
}
impl TypedWriter for PsqlFormatter<'_> {
type Error = PgError;
fn write<W: std::fmt::Display>(&mut self, value: W) -> Result<(), Self::Error> {
self.encoder.encode_field(&value.to_string())?;
Ok(())
}
fn write_bool(&mut self, value: bool) -> Result<(), Self::Error> {
self.encoder.encode_field(&value)?;
Ok(())
}
fn write_string(&mut self, value: &str) -> Result<(), Self::Error> {
self.encoder.encode_field(&value)?;
Ok(())
}
fn write_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> {
self.encoder.encode_field(&value)?;
Ok(())
}
fn write_hex(&mut self, value: &[u8]) -> Result<(), Self::Error> {
self.encoder.encode_field(&value)?;
Ok(())
}
fn write_timestamp(&mut self, value: Timestamp) -> Result<(), Self::Error> {
self.encoder.encode_field(&value.to_rfc3339()?)?;
Ok(())
}
fn write_duration(&mut self, value: TimeDuration) -> Result<(), Self::Error> {
self.encoder.encode_field(&value.to_iso8601())?;
Ok(())
}
fn write_alt_record(
&mut self,
ty: &PsqlType,
value: &ValueWithType<'_, ProductValue>,
) -> Result<bool, Self::Error> {
let json = satn::PsqlWrapper { ty: ty.clone(), value }.to_string();
self.encoder.encode_field(&json)?;
Ok(true)
}
fn write_record(
&mut self,
_fields: Vec<(Cow<str>, PsqlType, ValueWithType<AlgebraicValue>)>,
) -> Result<(), Self::Error> {
unreachable!("Use `write_alt_record` for records in PSQL format");
}
fn write_variant(
&mut self,
tag: u8,
ty: PsqlType,
name: Option<&str>,
value: ValueWithType<AlgebraicValue>,
) -> Result<(), Self::Error> {
if let AlgebraicType::Sum(sum) = &ty.field.algebraic_type {
if sum.is_option() {
if *value.value() == AlgebraicValue::unit() {
self.write("")?;
} else {
self.write(satn::PsqlWrapper { ty, value })?;
}
return Ok(());
}
if sum.is_simple_enum() {
if let Some(variant_name) = name {
self.encoder.encode_field(&variant_name)?;
return Ok(());
}
}
}
let PsqlChars { start, sep, end, quote } = ty.client.format_chars();
let name = name.map(Cow::from).unwrap_or_else(|| Cow::from(tag.to_string()));
let json = format!(
"{start}{quote}{name}{quote}{sep} {}{end}",
satn::PsqlWrapper { ty, value }
);
self.encoder.encode_field(&json)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pg_server::to_rows;
use futures::StreamExt;
use spacetimedb_client_api_messages::http::SqlStmtResult;
use spacetimedb_lib::sats::algebraic_value::Packed;
use spacetimedb_lib::sats::{i256, product, u256, AlgebraicType, ProductType, SumTypeVariant};
use spacetimedb_lib::{ConnectionId, Identity};
async fn run(schema: ProductType, row: ProductValue) -> String {
let header = row_desc(&schema, &Format::UnifiedText);
let stmt = SqlStmtResult {
schema,
rows: vec![row],
total_duration_micros: 0,
stats: Default::default(),
};
let mut stream = to_rows(stmt, header).unwrap();
let mut result = String::new();
if let Some(row) = stream.next().await {
result = String::from_utf8_lossy(row.unwrap().data.freeze().as_ref()).to_string();
}
result
}
#[tokio::test]
async fn test_primitives() {
let schema = ProductType::from([
AlgebraicType::U8,
AlgebraicType::I8,
AlgebraicType::I16,
AlgebraicType::U16,
AlgebraicType::I32,
AlgebraicType::U32,
AlgebraicType::I64,
AlgebraicType::U64,
AlgebraicType::I128,
AlgebraicType::U128,
AlgebraicType::I256,
AlgebraicType::U256,
AlgebraicType::F32,
AlgebraicType::F64,
AlgebraicType::String,
AlgebraicType::Bool,
]);
let value = product![
1u8,
-1i8,
-2i16,
3u16,
-4i32,
5u32,
-6i64,
7u64,
Packed::from(-8i128),
Packed::from(9u128),
i256::from(-10),
u256::from(11u128),
12.34f32,
56.78f64,
"test".to_string(),
true,
];
let row = run(schema, value).await;
assert_eq!(row, "\0\0\0\u{1}1\0\0\0\u{2}-1\0\0\0\u{2}-2\0\0\0\u{1}3\0\0\0\u{2}-4\0\0\0\u{1}5\0\0\0\u{2}-6\0\0\0\u{1}7\0\0\0\u{2}-8\0\0\0\u{1}9\0\0\0\u{3}-10\0\0\0\u{2}11\0\0\0\u{5}12.34\0\0\0\u{5}56.78\0\0\0\u{4}test\0\0\0\u{1}t");
}
#[tokio::test]
async fn test_enum() {
let some = AlgebraicType::option(AlgebraicType::I64);
let schema = ProductType::from([some.clone(), some]);
let value = product![
AlgebraicValue::sum(0, AlgebraicValue::I64(1)), // Some(1)
AlgebraicValue::sum(1, AlgebraicValue::unit()), // None
];
let row = run(schema, value).await;
assert_eq!(row, "\0\0\0\u{b}{\"some\": 1}\0\0\0\u{c}{\"none\": {}}");
let color = AlgebraicType::Sum([SumTypeVariant::new_named(AlgebraicType::I64, "Gray")].into());
let nested = AlgebraicType::option(color.clone());
let schema = ProductType::from([color, nested]);
// {"Gray": 1}, {"some": {"Gray": 2}}
let value = product![
AlgebraicValue::sum(0, AlgebraicValue::I64(1)), // Gray(1)
AlgebraicValue::sum(0, AlgebraicValue::sum(0, AlgebraicValue::I64(2))), // Some(Gray(2))
];
let row = run(schema.clone(), value.clone()).await;
assert_eq!(row, "\0\0\0\u{b}{\"Gray\": 1}\0\0\0\u{15}{\"some\": {\"Gray\": 2}}");
// Now nested product
let product = AlgebraicType::product([
ProductTypeElement::new(AlgebraicType::Product(schema), Some("x".into())),
ProductTypeElement::new(AlgebraicType::String, Some("y".into())),
]);
let schema = ProductType::from([product.clone()]);
let value = product![AlgebraicValue::product(vec![
value.into(),
AlgebraicValue::String("a".into()),
])];
let row = run(schema, value).await;
assert_eq!(
row,
"\0\0\0G{\"x\": {\"col_0\": {\"Gray\": 1}, \"col_1\": {\"some\": {\"Gray\": 2}}}, \"y\": \"a\"}"
);
// Now a simple enum
let names = AlgebraicType::simple_enum(["A", "B", "C"].into_iter());
let schema = ProductType::from([names.clone(), names.clone(), names]);
let value = product![
AlgebraicValue::enum_simple(0), // A
AlgebraicValue::enum_simple(1), // B
AlgebraicValue::enum_simple(2), // C
];
let row = run(schema, value).await;
assert_eq!(row, "\0\0\0\u{1}A\0\0\0\u{1}B\0\0\0\u{1}C");
}
#[tokio::test]
async fn test_special_types() {
let schema = ProductType::from([
AlgebraicType::identity(),
AlgebraicType::connection_id(),
AlgebraicType::time_duration(),
AlgebraicType::timestamp(),
AlgebraicType::bytes(),
]);
let value = product![
Identity::ZERO,
ConnectionId::ZERO,
TimeDuration::from_micros(0),
Timestamp::from_micros_since_unix_epoch(1622545800000),
AlgebraicValue::Bytes("test".as_bytes().into()),
];
let row = run(schema, value).await;
assert_eq!(row, "\0\0\0B\\x0000000000000000000000000000000000000000000000000000000000000000\0\0\0\"\\x00000000000000000000000000000000\0\0\0\u{3}P0D\0\0\0\u{1d}1970-01-19T18:42:25.800+00:00\0\0\0\n\\x74657374");
}
}