这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[alias]
format = "fmt"
format-check = "fmt --check"
lint = "clippy --all-targets --all-features -- -D warnings"

[target.'cfg(target_os="macos")']
# Postgres symbols won't be available until runtime
rustflags = ["-Clink-arg=-Wl,-undefined,dynamic_lookup"]
11 changes: 4 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,20 @@ jobs:
name: Linting (fmt + clippy)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/rust-toolchain@1.85.0
with:
components: rustfmt, clippy
- name: Checkout
uses: actions/checkout@v3
- name: Format check
uses: actions-rs/cargo@v1
with:
command: fmt
args: -- --check
run: cargo format-check

lockfile:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/rust-toolchain@1.85.0
- name: Lockfile check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
timeout-minutes: 45
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/rust-toolchain@1.85.0
- name: Install dependencies
Expand Down
24 changes: 21 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use core::ffi::CStr;
use std::time::{Duration, SystemTime};

use inner_ulid::Ulid as InnerUlid;
use pg_sys::Datum as SysDatum;
use pgrx::callconv::{ArgAbi, BoxRet};
Expand All @@ -7,7 +9,6 @@ use pgrx::{
pg_shmem_init, pg_sys::Oid, prelude::*, rust_regtypein, PgLwLock, PgSharedMemoryInitialization,
StringInfo, Uuid,
};
use std::time::{Duration, SystemTime};

::pgrx::pg_module_magic!();

Expand Down Expand Up @@ -141,8 +142,7 @@ fn ulid_to_bytea(input: ulid) -> Vec<u8> {

#[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()
ulid_to_timestamptz(input).into()
}

#[pg_extern(immutable, parallel_safe)]
Expand All @@ -160,6 +160,12 @@ fn timestamp_to_ulid(input: Timestamp) -> ulid {
ulid(inner.0)
}

#[pg_extern(immutable, parallel_safe)]
fn ulid_to_timestamptz(input: ulid) -> TimestampWithTimeZone {
let inner_seconds = (InnerUlid(input.0).timestamp_ms() as f64) / 1000.0;
to_timestamp(inner_seconds)
}

#[pg_extern(immutable, parallel_safe)]
fn timestamptz_to_ulid(input: TimestampWithTimeZone) -> ulid {
let epoch: f64 = input
Expand All @@ -182,6 +188,7 @@ 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 (ulid AS timestamptz) WITH FUNCTION ulid_to_timestamptz(ulid) AS IMPLICIT;
CREATE CAST (timestamptz AS ulid) WITH FUNCTION timestamptz_to_ulid(timestamptz) AS IMPLICIT;
"#,
name = "ulid_casts",
Expand All @@ -191,6 +198,7 @@ CREATE CAST (timestamptz AS ulid) WITH FUNCTION timestamptz_to_ulid(timestamptz)
ulid_to_bytea,
ulid_to_timestamp,
timestamp_to_ulid,
ulid_to_timestamptz,
timestamptz_to_ulid
]
);
Expand All @@ -207,6 +215,7 @@ mod tests {
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";
const TIMESTAMPTZ: &str = "2023-03-10 12:00:49.111+00";

#[pg_test]
fn test_null_to_ulid() {
Expand Down Expand Up @@ -262,6 +271,15 @@ mod tests {
assert_eq!(Some("01GV5PA9EQ0000000000000000"), result);
}

#[pg_test]
fn test_ulid_to_timestamptz() {
let result = Spi::get_one::<&str>(&format!(
"SET TIMEZONE TO 'UTC'; SELECT '{TEXT}'::ulid::timestamptz::text;"
))
.unwrap();
assert_eq!(Some(TIMESTAMPTZ), result);
}

#[pg_test]
fn test_timestamptz_to_ulid() {
let result = Spi::get_one::<&str>(&format!(
Expand Down