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
34 changes: 17 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "pgx_ulid"
publish = false
version = "0.2.2"
version = "0.2.3"

edition = "2021"
rust-version = "1.85.0"
rust-version = "1.90.0"

[lib]
crate-type = ["cdylib", "lib"]
Expand All @@ -24,11 +24,11 @@ pg_test = []

[dependencies]
inner_ulid = { package = "ulid", version = "1.1.3" }
pgrx = "^0.16.1"
pgrx = "^0.17.0"
serde = { version = "1.0", features = ["derive"] }

[dev-dependencies]
pgrx-tests = "^0.16.1"
pgrx-tests = "^0.17.0"

[profile.dev]
panic = "unwind"
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ RUN chown postgres:postgres /home/postgres
USER postgres

RUN \
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --profile minimal --default-toolchain 1.85.0 && \
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --profile minimal --default-toolchain 1.90.0 && \
rustup --version && \
rustc --version && \
cargo --version

# pgrx
RUN cargo install cargo-pgrx --version 0.16.1 --locked
RUN cargo install cargo-pgrx --version 0.17.0 --locked

RUN cargo pgrx init --pg${PG_MAJOR} $(which pg_config)

Expand Down
119 changes: 115 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ 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, PgLwLock, PgSharedMemoryInitialization,
StringInfo, Uuid,
};
use pgrx::{pg_shmem_init, pg_sys::Oid, prelude::*, rust_regtypein, PgLwLock, StringInfo, Uuid};
use serde::{Deserialize, Serialize};

::pgrx::pg_module_magic!();
Expand Down Expand Up @@ -185,6 +182,74 @@ fn timestamptz_to_ulid(input: TimestampWithTimeZone) -> ulid {
ulid(inner.0)
}

#[derive(Debug, AggregateName)]
#[aggregate_name = "min"]
struct UlidMin;

#[pg_aggregate]
impl Aggregate<UlidMin> for UlidMin {
type Args = ulid;
type State = ulid;

fn state(
current: Self::State,
arg: Self::Args,
_fcinfo: pg_sys::FunctionCallInfo,
) -> Self::State {
if current <= arg {
current
} else {
arg
}
}

fn combine(
first: Self::State,
second: Self::State,
_fcinfo: pg_sys::FunctionCallInfo,
) -> Self::State {
if first <= second {
first
} else {
second
}
}
}

#[derive(Debug, AggregateName)]
#[aggregate_name = "max"]
struct UlidMax;

#[pg_aggregate]
impl Aggregate<UlidMax> for UlidMax {
type Args = ulid;
type State = ulid;

fn state(
current: Self::State,
arg: Self::Args,
_fcinfo: pg_sys::FunctionCallInfo,
) -> Self::State {
if current >= arg {
current
} else {
arg
}
}

fn combine(
first: Self::State,
second: Self::State,
_fcinfo: pg_sys::FunctionCallInfo,
) -> Self::State {
if first >= second {
first
} else {
second
}
}
}

extension_sql!(
r#"
CREATE CAST (uuid AS ulid) WITH FUNCTION ulid_from_uuid(uuid) AS IMPLICIT;
Expand Down Expand Up @@ -360,6 +425,52 @@ mod tests {
.unwrap();
}

#[pg_test]
fn test_min_ulid_aggregate() {
let result = Spi::get_one::<String>(
"SELECT min(id)::text FROM (VALUES ('01GV5PA9EQG7D82Q3Y4PKBZSYV'::ulid), ('01GV5PA9EQ0000000000000000'::ulid)) AS t(id);",
)
.unwrap();
assert_eq!(Some("01GV5PA9EQ0000000000000000".to_string()), result);
}

#[pg_test]
fn test_max_ulid_aggregate() {
let result = Spi::get_one::<String>(
"SELECT max(id)::text FROM (VALUES ('01GV5PA9EQG7D82Q3Y4PKBZSYV'::ulid), ('01GV5PA9EQ0000000000000000'::ulid)) AS t(id);",
)
.unwrap();
assert_eq!(Some("01GV5PA9EQG7D82Q3Y4PKBZSYV".to_string()), result);
}

#[pg_test]
fn test_min_max_ulid_group_by_order_by() {
Spi::run(
r#"
CREATE TABLE contacts (
id ulid,
bucket int
);
INSERT INTO contacts (id, bucket) VALUES
('01GV5PA9EQ0000000000000000'::ulid, 1),
('01GV5PA9EQG7D82Q3Y4PKBZSYV'::ulid, 1),
('01GV5PA9EQG7D82Q3Y4PKBZSYW'::ulid, 2);
"#,
)
.unwrap();

let result = Spi::get_one::<String>(
"SELECT max(id)::text FROM contacts GROUP BY bucket ORDER BY max(id) DESC LIMIT 1;",
)
.unwrap();
assert_eq!(Some("01GV5PA9EQG7D82Q3Y4PKBZSYW".to_string()), result);
let result_min = Spi::get_one::<String>(
"SELECT min(id)::text FROM contacts GROUP BY bucket ORDER BY min(id) LIMIT 1;",
)
.unwrap();
assert_eq!(Some("01GV5PA9EQ0000000000000000".to_string()), result_min);
}

// TODO: Add tests for binary protocol
}

Expand Down