@@ -4,8 +4,8 @@ use std::sync::atomic::AtomicU32;
44
55use async_trait:: async_trait;
66use datafusion:: arrow:: array:: {
7- ArrayRef , AsArray , BooleanBuilder , Int32Builder , RecordBatch , StringArray , StringBuilder ,
8- as_boolean_array,
7+ ArrayRef , AsArray , BooleanArray , BooleanBuilder , Int32Builder , RecordBatch , StringArray ,
8+ StringBuilder , as_boolean_array,
99} ;
1010use datafusion:: arrow:: datatypes:: { DataType , Field , Int32Type , SchemaRef } ;
1111use datafusion:: arrow:: ipc:: reader:: FileReader ;
@@ -35,12 +35,16 @@ pub mod pg_attribute;
3535pub mod pg_class;
3636pub mod pg_database;
3737pub mod pg_get_expr_udf;
38+ pub mod pg_locks;
3839pub mod pg_namespace;
3940pub mod pg_replication_slot;
4041pub mod pg_roles;
4142pub mod pg_settings;
4243pub mod pg_stat_gssapi;
44+ pub mod pg_stat_ssl;
4345pub mod pg_tables;
46+ pub mod pg_tablespace;
47+ pub mod pg_timezone;
4448pub mod pg_views;
4549pub mod quote_ident_udf;
4650
@@ -103,6 +107,9 @@ const PG_CATALOG_TABLE_PG_STATISTIC_EXT_DATA: &str = "pg_statistic_ext_data";
103107const PG_CATALOG_TABLE_PG_SUBSCRIPTION : & str = "pg_subscription" ;
104108const PG_CATALOG_TABLE_PG_SUBSCRIPTION_REL : & str = "pg_subscription_rel" ;
105109const PG_CATALOG_TABLE_PG_TABLESPACE : & str = "pg_tablespace" ;
110+ const PG_CATALOG_TABLE_PG_LOCKS : & str = "pg_locks" ;
111+ const PG_CATALOG_VIEW_PG_TIMEZONE_NAMES : & str = "pg_timezone_names" ;
112+ const PG_CATALOG_VIEW_PG_TIMEZONE_ABBREVS : & str = "pg_timezone_abbrevs" ;
106113const PG_CATALOG_TABLE_PG_TRIGGER : & str = "pg_trigger" ;
107114const PG_CATALOG_TABLE_PG_USER_MAPPING : & str = "pg_user_mapping" ;
108115const PG_CATALOG_VIEW_PG_SETTINGS : & str = "pg_settings" ;
@@ -111,6 +118,7 @@ const PG_CATALOG_VIEW_PG_MATVIEWS: &str = "pg_matviews";
111118const PG_CATALOG_VIEW_PG_ROLES : & str = "pg_roles" ;
112119const PG_CATALOG_VIEW_PG_TABLES : & str = "pg_tables" ;
113120const PG_CATALOG_VIEW_PG_STAT_GSSAPI : & str = "pg_stat_gssapi" ;
121+ const PG_CATALOG_VIEW_PG_STAT_SSL : & str = "pg_stat_ssl" ;
114122const PG_CATALOG_VIEW_PG_STAT_USER_TABLES : & str = "pg_stat_user_tables" ;
115123const PG_CATALOG_VIEW_PG_REPLICATION_SLOTS : & str = "pg_replication_slots" ;
116124
@@ -174,16 +182,20 @@ pub const PG_CATALOG_TABLES: &[&str] = &[
174182 PG_CATALOG_TABLE_PG_SUBSCRIPTION ,
175183 PG_CATALOG_TABLE_PG_SUBSCRIPTION_REL ,
176184 PG_CATALOG_TABLE_PG_TABLESPACE ,
185+ PG_CATALOG_TABLE_PG_LOCKS ,
177186 PG_CATALOG_TABLE_PG_TRIGGER ,
178187 PG_CATALOG_TABLE_PG_USER_MAPPING ,
179188 PG_CATALOG_VIEW_PG_ROLES ,
180189 PG_CATALOG_VIEW_PG_SETTINGS ,
181190 PG_CATALOG_VIEW_PG_STAT_GSSAPI ,
191+ PG_CATALOG_VIEW_PG_STAT_SSL ,
182192 PG_CATALOG_VIEW_PG_TABLES ,
183193 PG_CATALOG_VIEW_PG_VIEWS ,
184194 PG_CATALOG_VIEW_PG_MATVIEWS ,
185195 PG_CATALOG_VIEW_PG_STAT_USER_TABLES ,
186196 PG_CATALOG_VIEW_PG_REPLICATION_SLOTS ,
197+ PG_CATALOG_VIEW_PG_TIMEZONE_NAMES ,
198+ PG_CATALOG_VIEW_PG_TIMEZONE_ABBREVS ,
187199] ;
188200
189201#[ derive( Debug , Hash , Eq , PartialEq , PartialOrd , Ord ) ]
@@ -364,7 +376,8 @@ impl<C: CatalogInfo, P: PgCatalogContextProvider> PgCatalogSchemaProvider<C, P>
364376 Ok ( Some ( self . static_tables . pg_subscription_rel . clone ( ) . into ( ) ) )
365377 }
366378 PG_CATALOG_TABLE_PG_TABLESPACE => {
367- Ok ( Some ( self . static_tables . pg_tablespace . clone ( ) . into ( ) ) )
379+ let table = Arc :: new ( pg_tablespace:: PgTablespaceTable :: new ( ) ) ;
380+ Ok ( Some ( PgCatalogTable :: Dynamic ( table) ) )
368381 }
369382 PG_CATALOG_TABLE_PG_TRIGGER => Ok ( Some ( self . static_tables . pg_trigger . clone ( ) . into ( ) ) ) ,
370383 PG_CATALOG_TABLE_PG_USER_MAPPING => {
@@ -416,6 +429,10 @@ impl<C: CatalogInfo, P: PgCatalogContextProvider> PgCatalogSchemaProvider<C, P>
416429 let table = Arc :: new ( pg_stat_gssapi:: PgStatGssApiTable :: new ( ) ) ;
417430 Ok ( Some ( PgCatalogTable :: Dynamic ( table) ) )
418431 }
432+ PG_CATALOG_VIEW_PG_STAT_SSL => {
433+ let table = Arc :: new ( pg_stat_ssl:: PgStatSslTable :: new ( ) ) ;
434+ Ok ( Some ( PgCatalogTable :: Dynamic ( table) ) )
435+ }
419436 PG_CATALOG_VIEW_PG_ROLES => {
420437 let table = Arc :: new ( pg_roles:: PgRolesTable :: new ( self . context_provider . clone ( ) ) ) ;
421438 Ok ( Some ( PgCatalogTable :: Dynamic ( table) ) )
@@ -427,6 +444,11 @@ impl<C: CatalogInfo, P: PgCatalogContextProvider> PgCatalogSchemaProvider<C, P>
427444 PG_CATALOG_VIEW_PG_REPLICATION_SLOTS => {
428445 Ok ( Some ( pg_replication_slot:: pg_replication_slots ( ) . into ( ) ) )
429446 }
447+ PG_CATALOG_TABLE_PG_LOCKS => Ok ( Some ( pg_locks:: pg_locks ( ) . into ( ) ) ) ,
448+ PG_CATALOG_VIEW_PG_TIMEZONE_NAMES => Ok ( Some ( pg_timezone:: pg_timezone_names ( ) . into ( ) ) ) ,
449+ PG_CATALOG_VIEW_PG_TIMEZONE_ABBREVS => {
450+ Ok ( Some ( pg_timezone:: pg_timezone_abbrevs ( ) . into ( ) ) )
451+ }
430452
431453 _ => Ok ( None ) ,
432454 }
@@ -588,7 +610,6 @@ pub struct PgCatalogStaticTables {
588610 pub pg_statistic_ext_data : Arc < ArrowTable > ,
589611 pub pg_subscription : Arc < ArrowTable > ,
590612 pub pg_subscription_rel : Arc < ArrowTable > ,
591- pub pg_tablespace : Arc < ArrowTable > ,
592613 pub pg_trigger : Arc < ArrowTable > ,
593614 pub pg_user_mapping : Arc < ArrowTable > ,
594615
@@ -977,13 +998,6 @@ impl PgCatalogStaticTables {
977998 ) )
978999 . to_vec ( ) ,
9791000 ) ?,
980- pg_tablespace : Self :: create_arrow_table (
981- include_bytes ! ( concat!(
982- env!( "CARGO_MANIFEST_DIR" ) ,
983- "/pg_catalog_arrow_exports/pg_tablespace.feather"
984- ) )
985- . to_vec ( ) ,
986- ) ?,
9871001 pg_trigger : Self :: create_arrow_table (
9881002 include_bytes ! ( concat!(
9891003 env!( "CARGO_MANIFEST_DIR" ) ,
@@ -1426,6 +1440,102 @@ pub fn create_pg_get_partition_ancestors_udf() -> ScalarUDF {
14261440 )
14271441}
14281442
1443+ pub fn create_pg_postmaster_start_time_udf ( ) -> ScalarUDF {
1444+ let func = move |_args : & [ ColumnarValue ] | {
1445+ let mut builder = datafusion:: arrow:: array:: TimestampNanosecondBuilder :: new ( )
1446+ . with_data_type ( DataType :: Timestamp (
1447+ datafusion:: arrow:: datatypes:: TimeUnit :: Nanosecond ,
1448+ Some ( Arc :: from ( "UTC" ) ) ,
1449+ ) ) ;
1450+ builder. append_value ( 1_700_000_000_000_000_000 ) ;
1451+ let array: ArrayRef = Arc :: new ( builder. finish ( ) ) ;
1452+ Ok ( ColumnarValue :: Array ( array) )
1453+ } ;
1454+
1455+ create_udf (
1456+ "pg_postmaster_start_time" ,
1457+ vec ! [ ] ,
1458+ DataType :: Timestamp (
1459+ datafusion:: arrow:: datatypes:: TimeUnit :: Nanosecond ,
1460+ Some ( Arc :: from ( "UTC" ) ) ,
1461+ ) ,
1462+ Volatility :: Stable ,
1463+ Arc :: new ( func) ,
1464+ )
1465+ }
1466+
1467+ pub fn create_pg_is_in_recovery_udf ( ) -> ScalarUDF {
1468+ let func = move |_args : & [ ColumnarValue ] | {
1469+ let array: ArrayRef = Arc :: new ( BooleanArray :: from ( vec ! [ false ] ) ) ;
1470+ Ok ( ColumnarValue :: Array ( array) )
1471+ } ;
1472+
1473+ create_udf (
1474+ "pg_is_in_recovery" ,
1475+ vec ! [ ] ,
1476+ DataType :: Boolean ,
1477+ Volatility :: Stable ,
1478+ Arc :: new ( func) ,
1479+ )
1480+ }
1481+
1482+ pub fn create_txid_current_udf ( ) -> ScalarUDF {
1483+ let func = move |_args : & [ ColumnarValue ] | {
1484+ let array: ArrayRef = Arc :: new ( datafusion:: arrow:: array:: Int64Array :: from ( vec ! [ 1i64 ] ) ) ;
1485+ Ok ( ColumnarValue :: Array ( array) )
1486+ } ;
1487+
1488+ create_udf (
1489+ "txid_current" ,
1490+ vec ! [ ] ,
1491+ DataType :: Int64 ,
1492+ Volatility :: Stable ,
1493+ Arc :: new ( func) ,
1494+ )
1495+ }
1496+
1497+ pub fn create_pg_tablespace_location_udf ( ) -> ScalarUDF {
1498+ let func = move |args : & [ ColumnarValue ] | {
1499+ let args = ColumnarValue :: values_to_arrays ( args) ?;
1500+ let input = & args[ 0 ] ;
1501+ let mut builder = StringBuilder :: new ( ) ;
1502+ for _ in 0 ..input. len ( ) {
1503+ builder. append_null ( ) ;
1504+ }
1505+ let array: ArrayRef = Arc :: new ( builder. finish ( ) ) ;
1506+ Ok ( ColumnarValue :: Array ( array) )
1507+ } ;
1508+
1509+ create_udf (
1510+ "pg_tablespace_location" ,
1511+ vec ! [ DataType :: Int32 ] ,
1512+ DataType :: Utf8 ,
1513+ Volatility :: Stable ,
1514+ Arc :: new ( func) ,
1515+ )
1516+ }
1517+
1518+ pub fn create_age_udf ( ) -> ScalarUDF {
1519+ let func = move |args : & [ ColumnarValue ] | {
1520+ let args = ColumnarValue :: values_to_arrays ( args) ?;
1521+ let input = & args[ 0 ] ;
1522+ let mut builder = datafusion:: arrow:: array:: Int64Builder :: new ( ) ;
1523+ for _ in 0 ..input. len ( ) {
1524+ builder. append_value ( 0 ) ;
1525+ }
1526+ let array: ArrayRef = Arc :: new ( builder. finish ( ) ) ;
1527+ Ok ( ColumnarValue :: Array ( array) )
1528+ } ;
1529+
1530+ create_udf (
1531+ "age" ,
1532+ vec ! [ DataType :: Utf8 ] ,
1533+ DataType :: Int64 ,
1534+ Volatility :: Stable ,
1535+ Arc :: new ( func) ,
1536+ )
1537+ }
1538+
14291539/// Install pg_catalog and postgres UDFs to current `SessionContext`
14301540pub fn setup_pg_catalog < P > (
14311541 session_context : & SessionContext ,
@@ -1484,6 +1594,11 @@ where
14841594 session_context. register_udf ( create_pg_get_partition_ancestors_udf ( ) ) ;
14851595 session_context. register_udf ( quote_ident_udf:: create_quote_ident_udf ( ) ) ;
14861596 session_context. register_udf ( quote_ident_udf:: create_parse_ident_udf ( ) ) ;
1597+ session_context. register_udf ( create_pg_postmaster_start_time_udf ( ) ) ;
1598+ session_context. register_udf ( create_pg_is_in_recovery_udf ( ) ) ;
1599+ session_context. register_udf ( create_txid_current_udf ( ) ) ;
1600+ session_context. register_udf ( create_pg_tablespace_location_udf ( ) ) ;
1601+ session_context. register_udf ( create_age_udf ( ) ) ;
14871602
14881603 Ok ( ( ) )
14891604}
@@ -1942,14 +2057,6 @@ mod test {
19422057 . to_vec ( ) ,
19432058 )
19442059 . expect ( "Failed to load ipc data" ) ;
1945- let _ = ArrowTable :: from_ipc_data (
1946- include_bytes ! ( concat!(
1947- env!( "CARGO_MANIFEST_DIR" ) ,
1948- "/pg_catalog_arrow_exports/pg_tablespace.feather"
1949- ) )
1950- . to_vec ( ) ,
1951- )
1952- . expect ( "Failed to load ipc data" ) ;
19532060 let _ = ArrowTable :: from_ipc_data (
19542061 include_bytes ! ( concat!(
19552062 env!( "CARGO_MANIFEST_DIR" ) ,
0 commit comments