1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//! Wrappers is a development framework for Postgres Foreign Data Wrappers ([FDW](https://wiki.postgresql.org/wiki/Foreign_data_wrappers)) based on [pgrx](https://github.com/tcdi/pgrx).
//!
//! Its goal is to make Postgres FDW development easier while keeping Rust language's modern capabilities, such as high performance, strong types, and safety.
//!
//! # Usage
//!
//! Wrappers is a pgrx extension, please follow the [installation steps](https://github.com/tcdi/pgrx#system-requirements) to install `pgrx` first.
//!
//! After pgrx is installed, create your project using command like below,
//!
//! ```bash
//! $ cargo pgrx new my_project
//! ```
//!
//! And then change default Postgres version to `pg14` or `pg15` and add below dependencies to your project's `Cargo.toml`,
//!
//! ```toml
//! [features]
//! default = ["pg15"]
//! ...
//!
//! [dependencies]
//! pgrx = "=0.8.3"
//! supabase-wrappers = "0.1"
//! ```
//!
//! # Supported Types
//!
//! For simplicity purpose, only a subset of [pgrx types](https://github.com/tcdi/pgrx#mapping-of-postgres-types-to-rust) are supported currently. For example,
//!
//! - bool
//! - f64
//! - i64
//! - String
//! - Timestamp
//! - JsonB
//!
//! See the full supported types list in [`interface::Cell`]. More types will be added in the future if needed or you can [raise a request](https://github.com/supabase/wrappers/issues) to us.
//!
//! # Developing a FDW
//!
//! The core interface is the [`interface::ForeignDataWrapper`] trait which provides callback functions to be called by Postgres during different querying phases. For example,
//!
//! - Query planning phase
//!   - [get_rel_size()](`interface::ForeignDataWrapper#method.get_rel_size`)
//! - Scan phase
//!   - [begin_scan()](`interface::ForeignDataWrapper#tymethod.begin_scan`) *required*
//!   - [iter_scan()](`interface::ForeignDataWrapper#tymethod.iter_scan`) *required*
//!   - [re_scan()](`interface::ForeignDataWrapper#method.re_scan`)
//!   - [end_scan()](`interface::ForeignDataWrapper#tymethod.end_scan`) *required*
//! - Modify phase
//!   - [begin_modify()](`interface::ForeignDataWrapper#method.begin_modify`)
//!   - [insert()](`interface::ForeignDataWrapper#method.insert`)
//!   - [update()](`interface::ForeignDataWrapper#method.update`)
//!   - [delete()](`interface::ForeignDataWrapper#method.delete`)
//!   - [end_modify()](`interface::ForeignDataWrapper#method.end_modify`)
//!
//! To give different functionalities to your FDW, you can choose different callback functions to implement. The required ones are `begin_scan`, `iter_scan` and `end_scan`, all the others are optional. See [Postgres FDW document](https://www.postgresql.org/docs/current/fdw-callbacks.html) for more details about FDW development.
//!
//! The FDW implements [`interface::ForeignDataWrapper`] trait must use [`wrappers_fdw`] macro and implement a `new()` initialization function. For example,
//!
//! ```rust,no_run
//! use supabase_wrappers::prelude::*;
//!
//! #[wrappers_fdw(
//!    version = "0.1.0",
//!    author = "Supabase",
//!    website = "https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/helloworld_fdw"
//! )]
//! pub struct HelloWorldFdw {
//!     //row counter
//!     row_cnt: i64,
//!
//!     // target column name list
//!     tgt_cols: Vec<Column>,
//! }
//!
//! impl ForeignDataWrapper for HelloWorldFdw {
//!     pub fn new(options: &HashMap<String, String>) -> Self {
//!         // 'options' is the key-value pairs defined in `CREATE SERVER` SQL, for example,
//!         //
//!         // create server my_helloworld_server
//!         //   foreign data wrapper wrappers_helloworld
//!         //   options (
//!         //     foo 'bar'
//!         // );
//!         //
//!         // 'options' passed here will be a hashmap { 'foo' -> 'bar' }.
//!         //
//!         // You can do any initalization in this new() function, like saving connection
//!         // info or API url in an variable, but don't do heavy works like database
//!         // connection or API call.
//!         Self {
//!             row_cnt: 0,
//!             tgt_cols: Vec::new(),
//!         }
//!     }
//! }
//! ```
//!
//! To develop a simple FDW supports basic query `SELECT`, you need to implement `begin_scan`, `iter_scan` and `end_scan`.
//!
//! - `begin_scan` - called once at the beginning of `SELECT`
//! - `iter_scan` - called for each row to be returned to Postgres, return `None` to stop the scan
//! - `end_scan` - called once at the end of `SELECT`
//!
//! Suppose the foreign table DDL is like below,
//!
//! ```sql
//! create foreign table hello (
//!   id bigint,
//!   col text
//! )
//!   server my_helloworld_server
//!   options (
//!     foo 'bar'
//!   );
//! ```
//!
//! Then we can implement [`interface::ForeignDataWrapper`] trait like below,
//!
//! ```rust,no_run
//! impl ForeignDataWrapper for HelloWorldFdw {
//!     fn begin_scan(
//!         &mut self,
//!         _quals: &[Qual],
//!         columns: &[Column],
//!         _sorts: &[Sort],
//!         _limit: &Option<Limit>,
//!         _options: &HashMap<String, String>,
//!     ) {
//!         // reset row count
//!         self.row_cnt = 0;
//!
//!         // save a copy of target columns
//!         self.tgt_cols = columns.to_vec();
//!     }
//!
//!     fn iter_scan(&mut self, row: &mut Row) -> Option<()> {
//!         // this is called on each row and we only return one row here
//!         if self.row_cnt < 1 {
//!             // add values to row if they are in target column list
//!             for tgt_col in &self.tgt_cols {
//!                 match tgt_col.as_str() {
//!                     "id" => row.push("id", Some(Cell::I64(self.row_cnt))),
//!                     "col" => row.push("col", Some(Cell::String("Hello world".to_string()))),
//!                     _ => {}
//!                 }
//!             }
//!
//!             self.row_cnt += 1;
//!
//!             // return the 'Some(())' to Postgres and continue data scan
//!             return Some(());
//!         }
//!
//!         // return 'None' to stop data scan
//!         None
//!     }
//!
//!     fn end_scan(&mut self) {
//!         // we do nothing here, but you can do things like resource cleanup and etc.
//!     }
//! }
//! ```
//!
//! And that's it. Now your FDW is ready to run,
//!
//! ```bash
//! $ cargo pgrx run
//! ```
//!
//! Then create the FDW and foreign table, and make a query on it,
//!
//! ```sql
//! create extension my_project;
//!
//! create foreign data wrapper helloworld_wrapper
//!   handler hello_world_fdw_handler
//!   validator hello_world_fdw_validator;
//!
//! create server my_helloworld_server
//!   foreign data wrapper helloworld_wrapper;
//!
//! create foreign table hello (
//!   id bigint,
//!   col text
//! )
//!   server my_helloworld_server;
//!
//! select * from hello;
//!
//!  id |    col
//! ----+-------------
//!   0 | Hello world
//! (1 row)
//! ```
//!
//! ### Pro Tips
//!
//! You can use `EXPLAIN` to check what have been pushed down. For example,
//!
//! ```sql
//! explain select * from hello where id = 1 order by col limit 1;
//!
//!                                                        QUERY PLAN
//! --------------------------------------------------------------------------------------------------------------------------
//!  Limit  (cost=1.01..1.01 rows=1 width=40)
//!    ->  Sort  (cost=1.01..1.01 rows=1 width=40)
//!          Sort Key: col
//!          ->  Foreign Scan on hello  (cost=0.00..1.00 rows=1 width=0)
//!                Filter: (id = 1)
//!                Wrappers: quals = [Qual { field: "id", operator: "=", value: Cell(I32(1)), use_or: false, param: None }]
//!                Wrappers: tgts = [Column { name: "id", num: 1, type_oid: 20 }, Column { name: "col", num: 2, type_oid: 25 }]
//!                Wrappers: sorts = [Sort { field: "col", field_no: 2, reversed: false, nulls_first: false, collate: None }]
//!                Wrappers: limit = Some(Limit { count: 1, offset: 0 })
//! (9 rows)
//! ```
//!
//! ### More FDW Examples
//!
//! See more FDW examples which interact with RDBMS or RESTful API.
//! - [HelloWorld](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/helloworld_fdw): A demo FDW to show how to develop a baisc FDW.
//! - [BigQuery](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/bigquery_fdw): A FDW for Google [BigQuery](https://cloud.google.com/bigquery) which supports data read and modify.
//! - [Clickhouse](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/clickhouse_fdw): A FDW for [ClickHouse](https://clickhouse.com/) which supports data read and modify.
//! - [Stripe](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/stripe_fdw): A FDW for [Stripe](https://stripe.com/) API which supports data read and modify.
//! - [Firebase](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/firebase_fdw): A FDW for Google [Firebase](https://firebase.google.com/) which supports data read only.
//! - [Airtable](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/airtable_fdw): A FDW for [Airtable](https://airtable.com/) API which supports data read only.
//! - [S3](https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/s3_fdw): A FDW for [AWS S3](https://aws.amazon.com/s3/) which supports data read only.

pub mod interface;
pub mod utils;

/// The prelude includes all necessary imports to make Wrappers work
pub mod prelude {
    pub use crate::interface::*;
    pub use crate::utils::*;
    pub use crate::wrappers_fdw;
    pub use ::tokio::runtime::Runtime;
}

use pgrx::prelude::*;
use pgrx::AllocatedByPostgres;

mod instance;
mod limit;
mod memctx;
mod modify;
mod polyfill;
mod qual;
mod scan;
mod sort;

/// PgBox'ed `FdwRoutine`, used in [`fdw_routine`](interface::ForeignDataWrapper::fdw_routine)
pub type FdwRoutine<A = AllocatedByPostgres> = PgBox<pg_sys::FdwRoutine, A>;

pub use supabase_wrappers_macros::wrappers_fdw;