Skip to content

Commit

Permalink
feat: add Interval and Bytea data types support (#356)
Browse files Browse the repository at this point in the history
* feat: add Interval and Bytea data types support

* fix: wasm fdw test path

* temp: display current dir

* format code

* temp: display wasm file path

* temp: display wasm file path

* temp: display wasm file path

* temp: display wasm file path

* debug: upload wasm file

* temp: display wasm file path

* revert temp changes

* clean up comment
  • Loading branch information
burmecia authored Oct 24, 2024
1 parent 61950c7 commit 6c125ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
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

0 comments on commit 6c125ba

Please sign in to comment.