Skip to content

Commit 83fb024

Browse files
authored
feat: add placeholder table/functions for pg_description queries (#116)
1 parent f079c9d commit 83fb024

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

datafusion-postgres/src/pg_catalog.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const PG_CATALOG_TABLE_PG_DATABASE: &str = "pg_database";
3030
const PG_CATALOG_TABLE_PG_AM: &str = "pg_am";
3131
const PG_CATALOG_TABLE_PG_RANGE: &str = "pg_range";
3232
const PG_CATALOG_TABLE_PG_ENUM: &str = "pg_enum";
33+
const PG_CATALOG_TABLE_PG_DESCRIPTION: &str = "pg_description";
3334

3435
/// Determine PostgreSQL table type (relkind) from DataFusion TableProvider
3536
fn get_table_type(table: &Arc<dyn TableProvider>) -> &'static str {
@@ -72,6 +73,7 @@ pub const PG_CATALOG_TABLES: &[&str] = &[
7273
PG_CATALOG_TABLE_PG_AM,
7374
PG_CATALOG_TABLE_PG_RANGE,
7475
PG_CATALOG_TABLE_PG_ENUM,
76+
PG_CATALOG_TABLE_PG_DESCRIPTION,
7577
];
7678

7779
// Data structure to hold pg_type table data
@@ -281,6 +283,7 @@ impl SchemaProvider for PgCatalogSchemaProvider {
281283
PG_CATALOG_TABLE_PG_PROC => Ok(Some(self.create_pg_proc_table())),
282284
PG_CATALOG_TABLE_PG_RANGE => Ok(Some(self.create_pg_range_table())),
283285
PG_CATALOG_TABLE_PG_ENUM => Ok(Some(self.create_pg_enum_table())),
286+
PG_CATALOG_TABLE_PG_DESCRIPTION => Ok(Some(self.create_pg_description_table())),
284287
_ => Ok(None),
285288
}
286289
}
@@ -782,6 +785,18 @@ impl PgCatalogSchemaProvider {
782785
Arc::new(provider)
783786
}
784787

788+
/// Create a mock empty table for pg_description
789+
fn create_pg_description_table(&self) -> Arc<dyn TableProvider> {
790+
let schema = Arc::new(Schema::new(vec![
791+
Field::new("objoid", DataType::Int32, false), // Oid
792+
Field::new("classoid", DataType::Int32, false), // Oid of the obj class
793+
Field::new("objsubid", DataType::Int32, false), // subid
794+
Field::new("description", DataType::Utf8, false),
795+
]));
796+
let provider = MemTable::try_new(schema, vec![vec![]]).unwrap();
797+
Arc::new(provider)
798+
}
799+
785800
/// Create a populated pg_proc table with standard PostgreSQL functions
786801
fn create_pg_proc_table(&self) -> Arc<dyn TableProvider> {
787802
// Define complete schema for pg_proc (matching PostgreSQL)
@@ -1927,13 +1942,16 @@ pub fn create_has_table_privilege_3param_udf() -> ScalarUDF {
19271942
// Define the function implementation for 3-parameter version
19281943
let func = move |args: &[ColumnarValue]| {
19291944
let args = ColumnarValue::values_to_arrays(args)?;
1930-
let _user = &args[0]; // User (can be name or OID)
1945+
let user = &args[0]; // User (can be name or OID)
19311946
let _table = &args[1]; // Table (can be name or OID)
19321947
let _privilege = &args[2]; // Privilege type (SELECT, INSERT, etc.)
19331948

19341949
// For now, always return true (full access)
1935-
let mut builder = BooleanArray::builder(1);
1936-
builder.append_value(true);
1950+
let mut builder = BooleanArray::builder(user.len());
1951+
for _ in 0..user.len() {
1952+
builder.append_value(true);
1953+
}
1954+
19371955
let array: ArrayRef = Arc::new(builder.finish());
19381956

19391957
Ok(ColumnarValue::Array(array))
@@ -1953,12 +1971,14 @@ pub fn create_has_table_privilege_2param_udf() -> ScalarUDF {
19531971
// Define the function implementation for 2-parameter version (current user, table, privilege)
19541972
let func = move |args: &[ColumnarValue]| {
19551973
let args = ColumnarValue::values_to_arrays(args)?;
1956-
let _table = &args[0]; // Table (can be name or OID)
1974+
let table = &args[0]; // Table (can be name or OID)
19571975
let _privilege = &args[1]; // Privilege type (SELECT, INSERT, etc.)
19581976

19591977
// For now, always return true (full access for current user)
1960-
let mut builder = BooleanArray::builder(1);
1961-
builder.append_value(true);
1978+
let mut builder = BooleanArray::builder(table.len());
1979+
for _ in 0..table.len() {
1980+
builder.append_value(true);
1981+
}
19621982
let array: ArrayRef = Arc::new(builder.finish());
19631983

19641984
Ok(ColumnarValue::Array(array))
@@ -1974,6 +1994,32 @@ pub fn create_has_table_privilege_2param_udf() -> ScalarUDF {
19741994
)
19751995
}
19761996

1997+
pub fn create_format_type_udf() -> ScalarUDF {
1998+
let func = move |args: &[ColumnarValue]| {
1999+
let args = ColumnarValue::values_to_arrays(args)?;
2000+
let type_oids = &args[0]; // Table (can be name or OID)
2001+
let _type_mods = &args[1]; // Privilege type (SELECT, INSERT, etc.)
2002+
2003+
// For now, always return true (full access for current user)
2004+
let mut builder = StringBuilder::new();
2005+
for _ in 0..type_oids.len() {
2006+
builder.append_value("???");
2007+
}
2008+
2009+
let array: ArrayRef = Arc::new(builder.finish());
2010+
2011+
Ok(ColumnarValue::Array(array))
2012+
};
2013+
2014+
create_udf(
2015+
"format_type",
2016+
vec![DataType::Int32, DataType::Int32],
2017+
DataType::Utf8,
2018+
Volatility::Stable,
2019+
Arc::new(func),
2020+
)
2021+
}
2022+
19772023
/// Install pg_catalog and postgres UDFs to current `SessionContext`
19782024
pub fn setup_pg_catalog(
19792025
session_context: &SessionContext,
@@ -1995,6 +2041,7 @@ pub fn setup_pg_catalog(
19952041
session_context.register_udf(create_pg_get_userbyid_udf());
19962042
session_context.register_udf(create_has_table_privilege_2param_udf());
19972043
session_context.register_udf(create_pg_table_is_visible());
2044+
session_context.register_udf(create_format_type_udf());
19982045

19992046
Ok(())
20002047
}

0 commit comments

Comments
 (0)