-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlib.rs
354 lines (300 loc) · 9.46 KB
/
lib.rs
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use core::ffi::CStr;
use inner_ulid::Ulid as InnerUlid;
use pg_sys::Datum as SysDatum;
use pgrx::callconv::{ArgAbi, BoxRet};
use pgrx::datum::Datum;
use pgrx::{
pg_shmem_init, pg_sys::Oid, prelude::*, rust_regtypein, shmem::*, PgLwLock, StringInfo, Uuid,
};
use std::time::{Duration, SystemTime};
::pgrx::pg_module_magic!();
static SHARED_ULID: PgLwLock<u128> = PgLwLock::new();
#[pg_guard]
pub extern "C" fn _PG_init() {
pg_shmem_init!(SHARED_ULID);
}
#[allow(non_camel_case_types)]
#[derive(
PostgresType, PostgresEq, PostgresHash, PostgresOrd, Debug, PartialEq, PartialOrd, Eq, Hash, Ord,
)]
#[inoutfuncs]
#[bikeshed_postgres_type_manually_impl_from_into_datum]
pub struct ulid(u128);
impl InOutFuncs for ulid {
#[inline]
fn input(input: &CStr) -> Self
where
Self: Sized,
{
let val = input.to_str().unwrap();
match InnerUlid::from_string(val) {
Ok(inner) => ulid(inner.0),
Err(err) => {
ereport!(
ERROR,
PgSqlErrorCode::ERRCODE_INVALID_TEXT_REPRESENTATION,
format!("invalid input syntax for type ulid: \"{val}\": {err}")
);
}
}
}
#[inline]
fn output(&self, buffer: &mut StringInfo) {
buffer.push_str(&InnerUlid(self.0).to_string())
}
}
impl IntoDatum for ulid {
#[inline]
fn into_datum(self) -> Option<SysDatum> {
self.0.to_ne_bytes().into_datum()
}
#[inline]
fn type_oid() -> Oid {
rust_regtypein::<Self>()
}
}
impl FromDatum for ulid {
#[inline]
unsafe fn from_polymorphic_datum(datum: SysDatum, is_null: bool, typoid: Oid) -> Option<Self>
where
Self: Sized,
{
let bytes: &[u8] = FromDatum::from_polymorphic_datum(datum, is_null, typoid)?;
let mut len_bytes = [0u8; 16];
len_bytes.copy_from_slice(bytes);
Some(ulid(u128::from_ne_bytes(len_bytes)))
}
}
unsafe impl<'fcx> ArgAbi<'fcx> for ulid
where
Self: 'fcx,
{
unsafe fn unbox_arg_unchecked(arg: ::pgrx::callconv::Arg<'_, 'fcx>) -> Self {
unsafe { arg.unbox_arg_using_from_datum().unwrap() }
}
}
unsafe impl BoxRet for ulid {
unsafe fn box_into<'fcx>(self, fcinfo: &mut pgrx::callconv::FcInfo<'fcx>) -> Datum<'fcx> {
unsafe { fcinfo.return_raw_datum(self.into_datum().unwrap()) }
}
}
#[pg_extern]
fn gen_monotonic_ulid() -> ulid {
let mut shared_bytes = SHARED_ULID.exclusive();
let shared_ulid = InnerUlid::from(*shared_bytes);
let new_ulid = if shared_ulid.is_nil()
|| SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or(Duration::ZERO)
.as_millis()
> u128::from(shared_ulid.timestamp_ms())
{
InnerUlid::new()
} else {
shared_ulid.increment().unwrap()
};
*shared_bytes = u128::from(new_ulid);
ulid(*shared_bytes)
}
#[pg_extern]
fn gen_ulid() -> ulid {
ulid(InnerUlid::new().0)
}
#[pg_extern(immutable, parallel_safe)]
fn ulid_from_uuid(input: Uuid) -> ulid {
let mut bytes = *input.as_bytes();
bytes.reverse();
ulid(u128::from_ne_bytes(bytes))
}
#[pg_extern(immutable, parallel_safe)]
fn ulid_to_uuid(input: ulid) -> Uuid {
let mut bytes = input.0.to_ne_bytes();
bytes.reverse();
Uuid::from_bytes(bytes)
}
#[pg_extern(immutable, parallel_safe)]
fn ulid_to_bytea(input: ulid) -> Vec<u8> {
let mut bytes = input.0.to_ne_bytes();
bytes.reverse();
bytes.to_vec()
}
#[pg_extern(immutable, parallel_safe)]
fn ulid_to_timestamp(input: ulid) -> Timestamp {
let inner_seconds = (InnerUlid(input.0).timestamp_ms() as f64) / 1000.0;
to_timestamp(inner_seconds).into()
}
#[pg_extern(immutable, parallel_safe)]
fn timestamp_to_ulid(input: Timestamp) -> ulid {
let epoch: f64 = input
.extract_part(DateTimeParts::Epoch)
.unwrap()
.try_into()
.unwrap();
let milliseconds = (epoch * 1000.0) as u64;
let inner = InnerUlid::from_parts(milliseconds, 0);
ulid(inner.0)
}
#[pg_extern(immutable, parallel_safe)]
fn timestamptz_to_ulid(input: TimestampWithTimeZone) -> ulid {
let epoch: f64 = input
.extract_part(DateTimeParts::Epoch)
.unwrap()
.try_into()
.unwrap();
let milliseconds = (epoch * 1000.0) as u64;
let inner = InnerUlid::from_parts(milliseconds, 0);
ulid(inner.0)
}
extension_sql!(
r#"
CREATE CAST (uuid AS ulid) WITH FUNCTION ulid_from_uuid(uuid) AS IMPLICIT;
CREATE CAST (ulid AS uuid) WITH FUNCTION ulid_to_uuid(ulid) AS IMPLICIT;
CREATE CAST (ulid AS bytea) WITH FUNCTION ulid_to_bytea(ulid) AS IMPLICIT;
CREATE CAST (ulid AS timestamp) WITH FUNCTION ulid_to_timestamp(ulid) AS IMPLICIT;
CREATE CAST (timestamp AS ulid) WITH FUNCTION timestamp_to_ulid(timestamp) AS IMPLICIT;
CREATE CAST (timestamptz AS ulid) WITH FUNCTION timestamptz_to_ulid(timestamptz) AS IMPLICIT;
"#,
name = "ulid_casts",
requires = [
ulid_from_uuid,
ulid_to_uuid,
ulid_to_bytea,
ulid_to_timestamp,
timestamp_to_ulid,
timestamptz_to_ulid
]
);
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use super::*;
const INT: u128 = 2029121117734015635515926905565997019;
const TEXT: &str = "01GV5PA9EQG7D82Q3Y4PKBZSYV";
const UUID: &str = "0186cb65-25d7-81da-815c-7e25a6bfe7db";
const BYTEA: &[u8] = &[
1, 134, 203, 101, 37, 215, 129, 218, 129, 92, 126, 37, 166, 191, 231, 219,
];
const TIMESTAMP: &str = "2023-03-10 12:00:49.111";
#[pg_test]
fn test_null_to_ulid() {
let result = Spi::get_one::<ulid>("SELECT NULL::ulid;").unwrap();
assert_eq!(None, result);
}
#[pg_test]
fn test_string_to_ulid() {
let result = Spi::get_one::<ulid>(&format!("SELECT '{TEXT}'::ulid;")).unwrap();
assert_eq!(Some(ulid(INT)), result);
}
#[pg_test]
fn test_ulid_to_string() {
let result = Spi::get_one::<&str>(&format!("SELECT '{TEXT}'::ulid::text;")).unwrap();
assert_eq!(Some(TEXT), result);
}
#[pg_test]
fn test_string_to_ulid_lowercase() {
let result = Spi::get_one::<ulid>(&format!("SELECT LOWER('{TEXT}')::ulid;")).unwrap();
assert_eq!(Some(ulid(INT)), result);
}
#[pg_test]
#[should_panic = "invalid input syntax for type ulid: \"01GV5PA9EQG7D82Q3Y4PKBZSY\": invalid length"]
fn test_string_to_ulid_invalid_length() {
let _ = Spi::get_one::<ulid>("SELECT '01GV5PA9EQG7D82Q3Y4PKBZSY'::ulid;");
}
#[pg_test]
#[should_panic = "invalid input syntax for type ulid: \"01GV5PA9EQG7D82Q3Y4PKBZSYU\": invalid character"]
fn test_string_to_ulid_invalid_char() {
let _ = Spi::get_one::<ulid>("SELECT '01GV5PA9EQG7D82Q3Y4PKBZSYU'::ulid;");
}
#[pg_test]
fn test_ulid_to_timestamp() {
let result = Spi::get_one::<&str>(&format!(
"SET TIMEZONE TO 'UTC'; SELECT '{TEXT}'::ulid::timestamp::text;"
))
.unwrap();
assert_eq!(Some(TIMESTAMP), result);
}
#[pg_test]
fn test_timestamp_to_ulid() {
let result = Spi::get_one::<&str>(&format!(
"SET TIMEZONE TO 'UTC'; SELECT '{TIMESTAMP}'::timestamp::ulid::text;"
))
.unwrap();
assert_eq!(Some("01GV5PA9EQ0000000000000000"), result);
}
#[pg_test]
fn test_timestamptz_to_ulid() {
let result = Spi::get_one::<&str>(&format!(
"SET TIMEZONE TO 'UTC'; SELECT '{TIMESTAMP}'::timestamptz::ulid::text;"
))
.unwrap();
assert_eq!(Some("01GV5PA9EQ0000000000000000"), result);
}
#[pg_test]
fn test_ulid_to_uuid() {
let result = Spi::get_one::<&str>(&format!("SELECT '{TEXT}'::ulid::uuid::text;")).unwrap();
assert_eq!(Some(UUID), result);
}
#[pg_test]
fn test_ulid_to_bytea() {
let result = Spi::get_one::<&[u8]>(&format!("SELECT '{TEXT}'::ulid::bytea;")).unwrap();
assert_eq!(Some(BYTEA), result);
}
#[pg_test]
fn test_uuid_to_ulid() {
let result = Spi::get_one::<ulid>(&format!("SELECT '{UUID}'::uuid::ulid;")).unwrap();
assert_eq!(Some(ulid(INT)), result);
}
#[pg_test]
fn test_generate() {
let result = Spi::get_one::<ulid>("SELECT gen_ulid();").unwrap();
assert!(result.is_some());
}
#[pg_test]
fn test_hash() {
Spi::run(
"CREATE TABLE foo (
id ulid,
data TEXT
);
CREATE TABLE bar (
id ulid,
foo_id ulid
);
INSERT INTO foo DEFAULT VALUES;
INSERT INTO bar DEFAULT VALUES;
SELECT *
FROM bar
JOIN foo ON bar.id = foo.id;",
)
.unwrap();
}
#[pg_test]
fn test_commutator() {
Spi::run(
"CREATE TABLE foo (
id ulid,
data TEXT
);
CREATE TABLE bar (
id ulid
);
SELECT *
FROM bar
JOIN foo ON bar.id = foo.id;",
)
.unwrap();
}
}
/// This module is required by `cargo pgrx test` invocations.
/// It must be visible at the root of your extension crate.
#[cfg(test)]
pub mod pg_test {
pub fn setup(_options: Vec<&str>) {
// perform one-off initialization when the pg_test framework starts
}
#[must_use]
pub fn postgresql_conf_options() -> Vec<&'static str> {
// return any postgresql.conf settings that are required for your tests
vec![]
}
}