From 6c125bae0e735d627743d1fc277347b677bc852c Mon Sep 17 00:00:00 2001 From: Bo Lu Date: Thu, 24 Oct 2024 18:59:15 +1100 Subject: [PATCH] feat: add `Interval` and `Bytea` data types support (#356) * 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 --- supabase-wrappers/src/interface.rs | 32 ++++++++++++++++++++++++++++-- wrappers/src/fdw/wasm_fdw/tests.rs | 6 +++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/supabase-wrappers/src/interface.rs b/supabase-wrappers/src/interface.rs index af6ca207..a53aa36d 100644 --- a/supabase-wrappers/src/interface.rs +++ b/supabase-wrappers/src/interface.rs @@ -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; @@ -47,7 +47,9 @@ pub enum Cell { Date(Date), Timestamp(Timestamp), Timestamptz(TimestampWithTimeZone), + Interval(Interval), Json(JsonB), + Bytea(*mut bytea), Uuid(Uuid), BoolArray(Vec>), I16Array(Vec>), @@ -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()), @@ -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::>() + .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), @@ -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(), @@ -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 @@ -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::())) + } PgOid::BuiltIn(PgBuiltInOids::UUIDOID) => { Uuid::from_datum(datum, is_null).map(Cell::Uuid) } diff --git a/wrappers/src/fdw/wasm_fdw/tests.rs b/wrappers/src/fdw/wasm_fdw/tests.rs index 26220bbc..9fa944ec 100644 --- a/wrappers/src/fdw/wasm_fdw/tests.rs +++ b/wrappers/src/fdw/wasm_fdw/tests.rs @@ -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/{}', @@ -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', @@ -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',