Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Interval and Bytea data types support #356

Merged
merged 13 commits into from
Oct 24, 2024
32 changes: 30 additions & 2 deletions supabase-wrappers/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
use crate::instance::ForeignServer;
use crate::FdwRoutine;
use pgrx::pg_sys::panic::ErrorReport;
use pgrx::prelude::{Date, Timestamp, TimestampWithTimeZone};
use pgrx::prelude::{Date, Interval, Timestamp, TimestampWithTimeZone};
use pgrx::{
datum::Uuid,
fcinfo,
pg_sys::{self, BuiltinOid, Datum, Oid},
pg_sys::{self, bytea, BuiltinOid, Datum, Oid},
AllocatedByRust, AnyNumeric, FromDatum, IntoDatum, JsonB, PgBuiltInOids, PgOid,
};
use std::collections::HashMap;
Expand Down Expand Up @@ -47,7 +47,9 @@ pub enum Cell {
Date(Date),
Timestamp(Timestamp),
Timestamptz(TimestampWithTimeZone),
Interval(Interval),
Json(JsonB),
Bytea(*mut bytea),
Uuid(Uuid),
BoolArray(Vec<Option<bool>>),
I16Array(Vec<Option<i16>>),
Expand All @@ -73,7 +75,9 @@ impl Clone for Cell {
Cell::Date(v) => Cell::Date(*v),
Cell::Timestamp(v) => Cell::Timestamp(*v),
Cell::Timestamptz(v) => Cell::Timestamptz(*v),
Cell::Interval(v) => Cell::Interval(*v),
Cell::Json(v) => Cell::Json(JsonB(v.0.clone())),
Cell::Bytea(v) => Cell::Bytea(*v),
Cell::Uuid(v) => Cell::Uuid(*v),
Cell::BoolArray(v) => Cell::BoolArray(v.clone()),
Cell::I16Array(v) => Cell::I16Array(v.clone()),
Expand Down Expand Up @@ -154,7 +158,21 @@ impl fmt::Display for Cell {
.expect("timestamptz should be a valid string")
)
},
Cell::Interval(v) => write!(f, "{}", v),
Cell::Json(v) => write!(f, "{:?}", v),
Cell::Bytea(v) => {
let byte_u8 = unsafe { pgrx::varlena::varlena_to_byte_slice(*v) };
let hex = byte_u8
.iter()
.map(|b| format!("{:02X}", b))
.collect::<Vec<String>>()
.join("");
if hex.is_empty() {
write!(f, "''")
} else {
write!(f, r#"'\x{}'"#, hex)
}
}
Cell::Uuid(v) => write!(f, "{}", v),
Cell::BoolArray(v) => write_array(v, f),
Cell::I16Array(v) => write_array(v, f),
Expand Down Expand Up @@ -182,7 +200,9 @@ impl IntoDatum for Cell {
Cell::Date(v) => v.into_datum(),
Cell::Timestamp(v) => v.into_datum(),
Cell::Timestamptz(v) => v.into_datum(),
Cell::Interval(v) => v.into_datum(),
Cell::Json(v) => v.into_datum(),
Cell::Bytea(v) => Some(Datum::from(v)),
Cell::Uuid(v) => v.into_datum(),
Cell::BoolArray(v) => v.into_datum(),
Cell::I16Array(v) => v.into_datum(),
Expand Down Expand Up @@ -212,7 +232,9 @@ impl IntoDatum for Cell {
|| other == pg_sys::DATEOID
|| other == pg_sys::TIMESTAMPOID
|| other == pg_sys::TIMESTAMPTZOID
|| other == pg_sys::INTERVALOID
|| other == pg_sys::JSONBOID
|| other == pg_sys::BYTEAOID
|| other == pg_sys::UUIDOID
|| other == pg_sys::BOOLARRAYOID
|| other == pg_sys::INT2ARRAYOID
Expand Down Expand Up @@ -265,9 +287,15 @@ impl FromDatum for Cell {
PgOid::BuiltIn(PgBuiltInOids::TIMESTAMPTZOID) => {
TimestampWithTimeZone::from_datum(datum, is_null).map(Cell::Timestamptz)
}
PgOid::BuiltIn(PgBuiltInOids::INTERVALOID) => {
Interval::from_datum(datum, is_null).map(Cell::Interval)
}
PgOid::BuiltIn(PgBuiltInOids::JSONBOID) => {
JsonB::from_datum(datum, is_null).map(Cell::Json)
}
PgOid::BuiltIn(PgBuiltInOids::BYTEAOID) => {
Some(Cell::Bytea(datum.cast_mut_ptr::<bytea>()))
}
PgOid::BuiltIn(PgBuiltInOids::UUIDOID) => {
Uuid::from_datum(datum, is_null).map(Cell::Uuid)
}
Expand Down
6 changes: 3 additions & 3 deletions wrappers/src/fdw/wasm_fdw/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod tests {
r#"CREATE SERVER snowflake_server
FOREIGN DATA WRAPPER wasm_wrapper
OPTIONS (
fdw_package_url 'file:///home/runner/work/wrappers/wrappers/wasm-wrappers/fdw/snowflake_fdw/target/wasm32-unknown-unknown/release/snowflake_fdw.wasm',
fdw_package_url 'file://../../../wasm-wrappers/fdw/snowflake_fdw/target/wasm32-unknown-unknown/release/snowflake_fdw.wasm',
fdw_package_name 'supabase:snowflake-fdw',
fdw_package_version '>=0.1.0',
api_url 'http://localhost:8096/snowflake/{}',
Expand Down Expand Up @@ -64,7 +64,7 @@ mod tests {
r#"CREATE SERVER paddle_server
FOREIGN DATA WRAPPER wasm_wrapper
OPTIONS (
fdw_package_url 'file:///home/runner/work/wrappers/wrappers/wasm-wrappers/fdw/paddle_fdw/target/wasm32-unknown-unknown/release/paddle_fdw.wasm',
fdw_package_url 'file://../../../wasm-wrappers/fdw/paddle_fdw/target/wasm32-unknown-unknown/release/paddle_fdw.wasm',
fdw_package_name 'supabase:paddle-fdw',
fdw_package_version '>=0.1.0',
api_url 'http://localhost:8096/paddle',
Expand Down Expand Up @@ -113,7 +113,7 @@ mod tests {
r#"CREATE SERVER notion_server
FOREIGN DATA WRAPPER wasm_wrapper
OPTIONS (
fdw_package_url 'file:///home/runner/work/wrappers/wrappers/wasm-wrappers/fdw/notion_fdw/target/wasm32-unknown-unknown/release/notion_fdw.wasm',
fdw_package_url 'file://../../../wasm-wrappers/fdw/notion_fdw/target/wasm32-unknown-unknown/release/notion_fdw.wasm',
fdw_package_name 'supabase:notion-fdw',
fdw_package_version '>=0.1.0',
api_url 'http://localhost:8096/notion',
Expand Down
Loading