From 1a6f5f40add20284a47e05fa3b5dd89e2e3ef453 Mon Sep 17 00:00:00 2001 From: Murat Dursun Date: Wed, 15 Jul 2026 16:36:06 +0200 Subject: [PATCH] fix: current_database() returns the configured catalog name, not the literal "datafusion" create_current_database_udf hardcoded "datafusion" regardless of the catalog the session was set up with. setup_pg_catalog already has the catalog name in scope; thread it through so SELECT current_database() reflects the database the client connected to, per PostgreSQL semantics. Co-Authored-By: Claude Opus 4.8 (1M context) --- datafusion-pg-catalog/src/pg_catalog.rs | 40 +++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/datafusion-pg-catalog/src/pg_catalog.rs b/datafusion-pg-catalog/src/pg_catalog.rs index 2776bdb..e4f051e 100644 --- a/datafusion-pg-catalog/src/pg_catalog.rs +++ b/datafusion-pg-catalog/src/pg_catalog.rs @@ -1080,12 +1080,16 @@ pub fn create_current_schema_udf() -> ScalarUDF { ) } -pub fn create_current_database_udf() -> ScalarUDF { +pub fn create_current_database_udf(database_name: &str) -> ScalarUDF { + // `current_database()` is fixed for the life of a connection in + // PostgreSQL, so capture the catalog name the session was set up + // with and return it verbatim. + let database_name = database_name.to_string(); // Define the function implementation let func = move |_args: &[ColumnarValue]| { // Create a UTF8 array with a single value let mut builder = StringBuilder::new(); - builder.append_value("datafusion"); + builder.append_value(&database_name); let array: ArrayRef = Arc::new(builder.finish()); Ok(ColumnarValue::Array(array)) @@ -1459,7 +1463,7 @@ where })? .register_schema("pg_catalog", pg_catalog.clone())?; - session_context.register_udf(create_current_database_udf()); + session_context.register_udf(create_current_database_udf(catalog_name)); session_context.register_udf(create_current_schema_udf()); session_context.register_udf(create_current_schemas_udf()); // session_context.register_udf(create_version_udf()); @@ -1528,6 +1532,36 @@ where mod test { use super::*; + #[tokio::test] + async fn current_database_returns_the_configured_catalog_name() { + use crate::pg_catalog::context::EmptyContextProvider; + use datafusion::arrow::array::StringArray; + use datafusion::prelude::{SessionConfig, SessionContext}; + + // Regression: `current_database()` used to return the hardcoded + // literal "datafusion" regardless of the catalog the session was + // set up with. It must reflect the configured catalog name. + let config = SessionConfig::new().with_default_catalog_and_schema("my_database", "public"); + let ctx = SessionContext::new_with_config(config); + setup_pg_catalog(&ctx, "my_database", EmptyContextProvider).unwrap(); + + let batches = ctx + .sql("SELECT current_database()") + .await + .unwrap() + .collect() + .await + .unwrap(); + + let value = batches[0] + .column(0) + .as_any() + .downcast_ref::() + .expect("current_database() returns a Utf8 column") + .value(0); + assert_eq!(value, "my_database"); + } + #[test] fn test_load_arrow_data() { let table = ArrowTable::from_ipc_data(