feat(duckdb): add connection bootstrap and remote catalogs
This commit is contained in:
41 files changed
+893
-194
No files matched your search
+154
-11
@@ -74,7 +74,13 @@ pub fn duckdb_query_tables_in_database_with_attached(
|
||||
})
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(rows.filter_map(|r| r.ok()).collect())
|
||||
let tables: Vec<db::TableInfo> = rows.filter_map(|r| r.ok()).collect();
|
||||
if tables.is_empty() && duckdb_is_quack_catalog(con, &database) {
|
||||
if let Some(remote) = duckdb_quack_remote_tables(con, &database, schema) {
|
||||
return Ok(remote);
|
||||
}
|
||||
}
|
||||
Ok(tables)
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
@@ -85,7 +91,7 @@ pub fn duckdb_attach_database(con: &duckdb::Connection, name: &str, path: &str)
|
||||
return Err("DuckDB attached database name and path are required".to_string());
|
||||
}
|
||||
let sql = format!("ATTACH {} AS {}", duckdb_quote_string(path), duckdb_quote_ident(name));
|
||||
con.execute_batch(&sql).map_err(|e| e.to_string())
|
||||
con.execute_batch(&sql).map_err(|e| format!("Failed to attach database \"{name}\": {e}"))
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
@@ -127,7 +133,143 @@ pub fn duckdb_list_schemas_with_attached(
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rows = stmt.query_map([database.as_str()], |row| row.get::<_, String>(0)).map_err(|e| e.to_string())?;
|
||||
Ok(rows.filter_map(|r| r.ok()).collect())
|
||||
let schemas: Vec<String> = rows.filter_map(|r| r.ok()).collect();
|
||||
if schemas.is_empty() && duckdb_is_quack_catalog(con, &database) {
|
||||
if let Some(remote) = duckdb_quack_remote_schemas(con, &database) {
|
||||
return Ok(remote);
|
||||
}
|
||||
}
|
||||
Ok(schemas)
|
||||
}
|
||||
|
||||
/// Identifies quack catalogs by the storage-extension `type` that
|
||||
/// `duckdb_databases()` reports, rather than by the attach aliases parsed from
|
||||
/// the init script: `ATTACH 'quack:host:port'` without an `AS` alias gets a
|
||||
/// derived catalog name (e.g. `localhost:9494`) that no parser sees.
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
fn duckdb_is_quack_catalog(con: &duckdb::Connection, database: &str) -> bool {
|
||||
con.query_row("SELECT type FROM duckdb_databases() WHERE lower(database_name) = lower(?)", [database], |row| {
|
||||
row.get::<_, String>(0)
|
||||
})
|
||||
.map(|catalog_type| catalog_type.eq_ignore_ascii_case("quack"))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Quack-attached catalogs expose no metadata on the client side (the beta
|
||||
/// extension implements name resolution and streaming scans, but not catalog
|
||||
/// enumeration: `information_schema`, `duckdb_tables()` and `SHOW ALL TABLES`
|
||||
/// all skip the remote catalog). The extension's `quack_query_by_name` table
|
||||
/// function runs SQL on the remote server, so when a metadata query for an
|
||||
/// attached catalog comes back empty we ask the remote for its own
|
||||
/// information_schema. For non-quack catalogs (or when the extension is not
|
||||
/// loaded) the function call errors and the fallback quietly yields `None`.
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
fn duckdb_quack_remote_query<T>(
|
||||
con: &duckdb::Connection,
|
||||
alias: &str,
|
||||
remote_sql: &str,
|
||||
map_row: impl Fn(&duckdb::Row<'_>) -> Result<T, duckdb::Error>,
|
||||
) -> Option<Vec<T>> {
|
||||
let sql = format!(
|
||||
"SELECT * FROM quack_query_by_name({}, {})",
|
||||
duckdb_quote_string(alias),
|
||||
duckdb_quote_string(remote_sql)
|
||||
);
|
||||
let mut stmt = match con.prepare(&sql) {
|
||||
Ok(stmt) => stmt,
|
||||
Err(e) => {
|
||||
log::debug!("quack metadata fallback unavailable for catalog {alias}: {e}");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let rows = match stmt.query_map([], |row| map_row(row)) {
|
||||
Ok(rows) => rows,
|
||||
Err(e) => {
|
||||
log::debug!("quack metadata fallback query failed for catalog {alias}: {e}");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
Some(rows.filter_map(|r| r.ok()).collect())
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
fn duckdb_quack_remote_schemas(con: &duckdb::Connection, alias: &str) -> Option<Vec<String>> {
|
||||
duckdb_quack_remote_query(
|
||||
con,
|
||||
alias,
|
||||
"SELECT DISTINCT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema', 'pg_catalog') ORDER BY schema_name",
|
||||
|row| row.get::<_, String>(0),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
fn duckdb_quack_remote_tables(con: &duckdb::Connection, alias: &str, schema: &str) -> Option<Vec<db::TableInfo>> {
|
||||
let remote_sql = format!(
|
||||
"SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = {} ORDER BY table_name",
|
||||
duckdb_quote_string(schema)
|
||||
);
|
||||
duckdb_quack_remote_query(con, alias, &remote_sql, |row| {
|
||||
Ok(db::TableInfo {
|
||||
name: row.get::<_, String>(0)?,
|
||||
table_type: row.get::<_, String>(1)?,
|
||||
comment: None,
|
||||
parent_schema: None,
|
||||
parent_name: None,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
fn duckdb_quack_remote_columns(
|
||||
con: &duckdb::Connection,
|
||||
alias: &str,
|
||||
schema: &str,
|
||||
table: &str,
|
||||
) -> Option<Vec<db::ColumnInfo>> {
|
||||
let pk_sql = format!(
|
||||
"SELECT kcu.column_name
|
||||
FROM information_schema.table_constraints tc
|
||||
JOIN information_schema.key_column_usage kcu
|
||||
ON tc.constraint_name = kcu.constraint_name
|
||||
AND tc.table_schema = kcu.table_schema
|
||||
AND tc.table_name = kcu.table_name
|
||||
WHERE tc.constraint_type = 'PRIMARY KEY'
|
||||
AND tc.table_schema = {schema}
|
||||
AND tc.table_name = {table}
|
||||
ORDER BY kcu.ordinal_position",
|
||||
schema = duckdb_quote_string(schema),
|
||||
table = duckdb_quote_string(table),
|
||||
);
|
||||
let primary_keys: std::collections::HashSet<String> =
|
||||
duckdb_quack_remote_query(con, alias, &pk_sql, |row| row.get::<_, String>(0))
|
||||
.map(|names| names.into_iter().collect())
|
||||
.unwrap_or_default();
|
||||
|
||||
let columns_sql = format!(
|
||||
"SELECT column_name, data_type, is_nullable, column_default
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = {schema} AND table_name = {table}
|
||||
ORDER BY ordinal_position",
|
||||
schema = duckdb_quote_string(schema),
|
||||
table = duckdb_quote_string(table),
|
||||
);
|
||||
duckdb_quack_remote_query(con, alias, &columns_sql, |row| {
|
||||
let name = row.get::<_, String>(0)?;
|
||||
Ok(db::ColumnInfo {
|
||||
is_primary_key: primary_keys.contains(&name),
|
||||
name,
|
||||
data_type: row.get::<_, String>(1)?,
|
||||
is_nullable: row.get::<_, String>(2).unwrap_or_default() == "YES",
|
||||
column_default: row.get::<_, Option<String>>(3)?,
|
||||
extra: None,
|
||||
comment: None,
|
||||
numeric_precision: None,
|
||||
numeric_scale: None,
|
||||
character_maximum_length: None,
|
||||
enum_values: None,
|
||||
..Default::default()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
@@ -243,7 +385,13 @@ pub fn duckdb_query_columns_in_database_with_attached(
|
||||
})
|
||||
})
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(rows.filter_map(|r| r.ok()).collect())
|
||||
let columns: Vec<db::ColumnInfo> = rows.filter_map(|r| r.ok()).collect();
|
||||
if columns.is_empty() && duckdb_is_quack_catalog(con, &database) {
|
||||
if let Some(remote) = duckdb_quack_remote_columns(con, &database, schema, table) {
|
||||
return Ok(remote);
|
||||
}
|
||||
}
|
||||
Ok(columns)
|
||||
}
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
@@ -454,13 +602,7 @@ fn duckdb_completion_like_pattern(request: &db::CompletionAssistantRequest) -> S
|
||||
|
||||
#[cfg(feature = "duckdb-bundled")]
|
||||
async fn duckdb_attached_database_names(state: &AppState, connection_id: &str) -> Vec<String> {
|
||||
state
|
||||
.configs
|
||||
.read()
|
||||
.await
|
||||
.get(connection_id)
|
||||
.map(|config| config.attached_databases.iter().map(|database| database.name.clone()).collect())
|
||||
.unwrap_or_default()
|
||||
state.configs.read().await.get(connection_id).map(crate::db::duckdb_sql::config_attached_names).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn clickhouse_metadata_database<'a>(database: &'a str, schema: &'a str) -> &'a str {
|
||||
@@ -2326,6 +2468,7 @@ mod tests {
|
||||
visible_databases: None,
|
||||
visible_schemas: None,
|
||||
attached_databases: Vec::new(),
|
||||
init_script: None,
|
||||
color: None,
|
||||
transport_layers: Vec::new(),
|
||||
connect_timeout_secs: 5,
|
||||
|
||||
Reference in new issue
Block a user