fix(databricks): qualify Unity Catalog table previews

Closes #10399
This commit is contained in:
t8y2 committed 2026-09-27 15:56:52 +00:00
1 parent 20c50550cf
commit ba3872abef
3 files changed
+63 -13

No files matched your search

@@ -72,9 +72,12 @@ pub fn qualified_table_name(database_type: Option<DatabaseType>, schema: Option<
} }
/// Like `qualified_table_name`, but also supports 3-part names for SQL Server /// Like `qualified_table_name`, but also supports 3-part names for SQL Server
/// (`<database>.<schema>.<table>`) and Doris/StarRocks external catalogs /// (`<database>.<schema>.<table>`), Databricks Unity Catalog
/// (`<catalog>.<schema>.<table>`), and Doris/StarRocks external catalogs
/// (`<catalog>.<database>.<table>`). SQL parsing stores the first segment of a /// (`<catalog>.<database>.<table>`). SQL parsing stores the first segment of a
/// 3-part source in `catalog`; for SQL Server that segment is its database. /// 3-part source in `catalog`; for SQL Server that segment is its database.
/// The desktop metadata tree stores a Databricks JDBC catalog in `database`,
/// so Databricks accepts that as a fallback when `catalog` is absent.
/// Doris/StarRocks use `schema` as the middle segment when present, otherwise /// Doris/StarRocks use `schema` as the middle segment when present, otherwise
/// `database`, and ignore their built-in `internal` catalog. /// `database`, and ignore their built-in `internal` catalog.
pub fn qualified_table_name_with_catalog( pub fn qualified_table_name_with_catalog(
@@ -85,6 +88,19 @@ pub fn qualified_table_name_with_catalog(
table_name: &str, table_name: &str,
) -> String { ) -> String {
let catalog = catalog.map(str::trim).filter(|catalog| !catalog.is_empty()); let catalog = catalog.map(str::trim).filter(|catalog| !catalog.is_empty());
if database_type == Some(DatabaseType::Databricks) {
let catalog = catalog.or_else(|| database.map(str::trim).filter(|database| !database.is_empty()));
let schema = schema.map(str::trim).filter(|schema| !schema.is_empty());
if let (Some(catalog), Some(schema)) = (catalog, schema) {
return format!(
"{}.{}.{}",
quote_table_identifier(database_type, catalog),
quote_table_identifier(database_type, schema),
quote_table_identifier(database_type, table_name)
);
}
return qualified_table_name(database_type, schema, table_name);
}
match (catalog, database_type) { match (catalog, database_type) {
(Some(database), Some(DatabaseType::SqlServer)) => { (Some(database), Some(DatabaseType::SqlServer)) => {
let table = qualified_table_name(database_type, schema, table_name); let table = qualified_table_name(database_type, schema, table_name);
@@ -1080,7 +1080,34 @@ mod tests {
} }
#[test] #[test]
fn databricks_table_select_uses_backtick_identifiers() { fn databricks_table_select_uses_unity_catalog_three_part_name() {
let explicit_catalog = TableDataSelectSqlOptions {
database_type: Some(DatabaseType::Databricks),
catalog: Some("analytics".to_string()),
database: Some("ignored_tree_catalog".to_string()),
schema: Some("sales".to_string()),
table_name: "orders".to_string(),
limit: Some(100),
..Default::default()
};
assert_eq!(
build_table_data_select_sql(explicit_catalog),
"SELECT * FROM `analytics`.`sales`.`orders` LIMIT 100;"
);
let tree_catalog = TableDataSelectSqlOptions {
database_type: Some(DatabaseType::Databricks),
database: Some("analytics".to_string()),
schema: Some("sales".to_string()),
table_name: "orders".to_string(),
limit: Some(100),
..Default::default()
};
assert_eq!(build_table_data_select_sql(tree_catalog), "SELECT * FROM `analytics`.`sales`.`orders` LIMIT 100;");
}
#[test]
fn databricks_table_select_without_catalog_keeps_two_part_name() {
assert_eq!( assert_eq!(
build_table_data_select_sql(TableDataSelectSqlOptions { build_table_data_select_sql(TableDataSelectSqlOptions {
database_type: Some(DatabaseType::Databricks), database_type: Some(DatabaseType::Databricks),
@@ -1225,13 +1252,19 @@ mod tests {
} }
#[test] #[test]
fn external_catalog_is_ignored_for_non_doris_engines() { fn catalog_qualification_does_not_change_other_dialects() {
// Postgres does not support the 3-part catalog naming; the catalog // Postgres does not support the 3-part catalog naming; the catalog
// must be ignored to avoid emitting an invalid qualified name. // must be ignored to avoid emitting an invalid qualified name.
let sql = let sql = build_table_data_select_sql(TableDataSelectSqlOptions {
build_table_data_select_sql(opts(DatabaseType::Postgres, Some("iceberg_catalog"), Some("sales"), "orders")); database_type: Some(DatabaseType::Postgres),
assert!(!sql.contains("iceberg_catalog"), "sql was: {sql}"); catalog: Some("analytics".to_string()),
assert!(sql.contains("orders"), "sql was: {sql}"); database: Some("warehouse".to_string()),
schema: Some("sales".to_string()),
table_name: "orders".to_string(),
limit: Some(10),
..Default::default()
});
assert_eq!(sql, "SELECT * FROM \"sales\".\"orders\" LIMIT 10;");
} }
#[test] #[test]
@@ -28,14 +28,15 @@ pub struct TableDataSelectSqlOptions {
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub schema: Option<String>, pub schema: Option<String>,
pub table_name: String, pub table_name: String,
/// Doris / StarRocks multi-catalog: when set to a non-`internal` catalog, /// Catalog namespace used in three-part table names. Databricks emits
/// the FROM clause is prefixed with the catalog /// `<catalog>.<schema>.<table>`; Doris / StarRocks external catalogs emit
/// (`<catalog>.<database>.<table>`). /// `<catalog>.<database>.<table>`.
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub catalog: Option<String>, pub catalog: Option<String>,
/// Doris / StarRocks multi-catalog: the database under the external /// Database namespace. For Databricks table nodes this carries the JDBC
/// catalog, used as the middle segment of the 3-part qualified name when /// catalog when the explicit `catalog` field is unavailable. For Doris /
/// `schema` is absent (Doris/StarRocks have no separate schema concept). /// StarRocks it is the database under an external catalog and is used as
/// the middle segment when `schema` is absent.
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub database: Option<String>, pub database: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]