fix(mysql): use database-specific catalog switching
This commit is contained in:
1 parent
9571519179
commit
2152f8cde8
31 files changed
+818
-182
No files matched your search
@@ -82,6 +82,16 @@ fn oceanbase_mysql_setup_queries(config: &ConnectionConfig) -> Vec<String> {
|
||||
oceanbase_mysql_query_timeout_sql(config, config.query_timeout_secs).into_iter().collect()
|
||||
}
|
||||
|
||||
fn mysql_pool_setup_queries(config: &ConnectionConfig, url: &str) -> Vec<String> {
|
||||
let mut queries = oceanbase_mysql_setup_queries(config);
|
||||
if let Some(dialect) = db::mysql::mysql_catalog_dialect(config.db_type, config.driver_profile.as_deref()) {
|
||||
if let Some(query) = db::mysql::catalog_setup_query_for_url(dialect, url) {
|
||||
queries.push(query);
|
||||
}
|
||||
}
|
||||
queries
|
||||
}
|
||||
|
||||
pub enum PoolKind {
|
||||
Mysql(db::mysql::MySqlPool, MysqlMode),
|
||||
Postgres(deadpool_postgres::Pool),
|
||||
@@ -363,7 +373,7 @@ pub async fn connect_mysql_metadata_pool(
|
||||
) -> Result<(db::mysql::MySqlPool, MysqlMode), String> {
|
||||
let url = connection_url_for_endpoint(db_config, host, port);
|
||||
let idle_timeout_secs = Some(db_config.idle_timeout_secs);
|
||||
let extra_setup_queries = oceanbase_mysql_setup_queries(db_config);
|
||||
let extra_setup_queries = mysql_pool_setup_queries(db_config, &url);
|
||||
if db_config.needs_bare_mysql() {
|
||||
return match connect_bare_mysql_pool_with_setup(
|
||||
db_config,
|
||||
@@ -475,7 +485,7 @@ pub async fn connect_bare_metadata_pool(
|
||||
max_connections: usize,
|
||||
) -> Result<db::mysql::MySqlPool, String> {
|
||||
let url = connection_url_for_endpoint(db_config, host, port);
|
||||
let extra_setup_queries = oceanbase_mysql_setup_queries(db_config);
|
||||
let extra_setup_queries = mysql_pool_setup_queries(db_config, &url);
|
||||
if db_config.effective_database().is_none() {
|
||||
return connect_bare_mysql_pool_with_setup(
|
||||
db_config,
|
||||
@@ -1220,7 +1230,7 @@ impl AppState {
|
||||
&url,
|
||||
connect_timeout,
|
||||
mysql_pool_max_connections,
|
||||
&oceanbase_mysql_setup_queries(&db_config),
|
||||
&mysql_pool_setup_queries(&db_config, &url),
|
||||
)
|
||||
.await?
|
||||
};
|
||||
@@ -3872,12 +3882,12 @@ async fn detect_ob_oracle_mode(config: &ConnectionConfig, pool: &db::mysql::MySq
|
||||
mod tests {
|
||||
use super::{
|
||||
agent_connect_timeout, connection_remote_endpoint, connection_url_for_endpoint, database_connection_config,
|
||||
metadata_connection_config, mysql_metadata_fallback_url, oceanbase_mysql_query_timeout_sql,
|
||||
oceanbase_mysql_setup_queries, prestosql_jdbc_config_for_endpoint, redacted_connection_url_for_endpoint,
|
||||
redis_sentinel_transport_id, redis_sentinel_transport_prefix, sqlserver_legacy_agent_config,
|
||||
sqlserver_legacy_driver_error, sqlserver_uses_legacy_driver, task_client_session_id, uses_bare_mysql_pool,
|
||||
uses_tcp_probe, validate_connection_url_params, validate_h2_database_path, AppState, MysqlMode, PoolKind,
|
||||
PRESTOSQL_JDBC_DRIVER_CLASS,
|
||||
metadata_connection_config, mysql_metadata_fallback_url, mysql_pool_setup_queries,
|
||||
oceanbase_mysql_query_timeout_sql, oceanbase_mysql_setup_queries, prestosql_jdbc_config_for_endpoint,
|
||||
redacted_connection_url_for_endpoint, redis_sentinel_transport_id, redis_sentinel_transport_prefix,
|
||||
sqlserver_legacy_agent_config, sqlserver_legacy_driver_error, sqlserver_uses_legacy_driver,
|
||||
task_client_session_id, uses_bare_mysql_pool, uses_tcp_probe, validate_connection_url_params,
|
||||
validate_h2_database_path, AppState, MysqlMode, PoolKind, PRESTOSQL_JDBC_DRIVER_CLASS,
|
||||
};
|
||||
use crate::agent_connection::{
|
||||
agent_connect_params, mongo_legacy_error_with_auth_hint, mongo_uses_legacy_driver,
|
||||
@@ -4182,6 +4192,28 @@ mod tests {
|
||||
assert!(oceanbase_mysql_setup_queries(&config).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doris_pool_setup_uses_switch_for_configured_catalog() {
|
||||
let mut config = mysql_config(Some("bi"));
|
||||
config.db_type = DatabaseType::Doris;
|
||||
|
||||
assert_eq!(
|
||||
mysql_pool_setup_queries(&config, "mysql://root:secret@localhost:9030/bi?catalog=paimon%5Fcatalog"),
|
||||
vec!["SWITCH `paimon_catalog`"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starrocks_pool_setup_uses_set_catalog_for_configured_catalog() {
|
||||
let mut config = mysql_config(Some("bi"));
|
||||
config.db_type = DatabaseType::StarRocks;
|
||||
|
||||
assert_eq!(
|
||||
mysql_pool_setup_queries(&config, "mysql://root:secret@localhost:9030/bi?catalog=paimon%5Fcatalog"),
|
||||
vec!["SET CATALOG `paimon_catalog`"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mongo_legacy_retry_covers_old_server_handshake_eof() {
|
||||
let err = r#"MongoDB connection failed: Kind: Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: db.example.com:27017, Type: Unknown, Error: Kind: I/O error: unexpected end of file } ] }"#;
|
||||
|
||||
+114
-52
@@ -26,6 +26,27 @@ use super::file_validator::validate_file_path;
|
||||
pub type MySqlPool = mysql_async::Pool;
|
||||
const MYSQL_TCP_KEEPALIVE_MS: u32 = 30_000;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum MySqlCatalogDialect {
|
||||
Doris,
|
||||
StarRocks,
|
||||
}
|
||||
|
||||
pub(crate) fn mysql_catalog_dialect(
|
||||
db_type: DatabaseType,
|
||||
driver_profile: Option<&str>,
|
||||
) -> Option<MySqlCatalogDialect> {
|
||||
match db_type {
|
||||
DatabaseType::Doris => Some(MySqlCatalogDialect::Doris),
|
||||
DatabaseType::StarRocks => Some(MySqlCatalogDialect::StarRocks),
|
||||
_ => match driver_profile.map(str::to_ascii_lowercase).as_deref() {
|
||||
Some("doris" | "selectdb") => Some(MySqlCatalogDialect::Doris),
|
||||
Some("starrocks") => Some(MySqlCatalogDialect::StarRocks),
|
||||
_ => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct MySqlQueryDialect {
|
||||
supports_admin_show_results: bool,
|
||||
@@ -1012,7 +1033,6 @@ fn mysql_setup_queries_for_database_with_mode(
|
||||
setup_mode: MySqlSetupMode,
|
||||
) -> Vec<String> {
|
||||
let charset = mysql_connection_charset(url).unwrap_or("utf8mb4");
|
||||
let catalog = mysql_connection_catalog(url);
|
||||
let database = setup_database.map(ToOwned::to_owned).or_else(|| mysql_connection_database(url));
|
||||
let mut queries = Vec::new();
|
||||
if let Some(database) = database.as_deref() {
|
||||
@@ -1031,22 +1051,51 @@ fn mysql_setup_queries_for_database_with_mode(
|
||||
if let Some(query) = setup_mode.group_concat_max_len_query() {
|
||||
queries.push(query);
|
||||
}
|
||||
// StarRocks/Doris expose external storage (Paimon, Hive, ...) through a
|
||||
// catalog. `SET catalog` must run *before* `USE <database>` (the database
|
||||
// lives in the external catalog and is unknown to the default one).
|
||||
// mysql_async drains the setup list back-to-front (Vec::pop), so push it
|
||||
// last to make it execute first. The handshake does not send the database
|
||||
// as schema (see `mysql_async_url`, which strips the path when a catalog is
|
||||
// configured), so the connection establishes in the default catalog and
|
||||
// this setup query is what switches it. The pool re-runs these queries
|
||||
// after every connection reset, so the catalog stays current.
|
||||
if let Some(catalog) = catalog.as_deref() {
|
||||
queries.push(format!("SET catalog = {}", quote_identifier(catalog)));
|
||||
}
|
||||
queries.extend(extra_setup_queries.iter().cloned());
|
||||
queries
|
||||
}
|
||||
|
||||
fn catalog_switch_query(dialect: MySqlCatalogDialect, catalog: &str) -> String {
|
||||
let catalog = quote_identifier(catalog);
|
||||
match dialect {
|
||||
MySqlCatalogDialect::Doris => format!("SWITCH {catalog}"),
|
||||
MySqlCatalogDialect::StarRocks => format!("SET CATALOG {catalog}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn catalog_setup_query_for_url(dialect: MySqlCatalogDialect, url: &str) -> Option<String> {
|
||||
mysql_connection_catalog(url).map(|catalog| catalog_switch_query(dialect, &catalog))
|
||||
}
|
||||
|
||||
pub(crate) fn catalog_database_context_queries(
|
||||
dialect: Option<MySqlCatalogDialect>,
|
||||
catalog: Option<&str>,
|
||||
database: &str,
|
||||
) -> Result<Vec<String>, String> {
|
||||
let Some(catalog) = catalog.filter(|value| !value.trim().is_empty()) else {
|
||||
return Ok(Vec::new());
|
||||
};
|
||||
let dialect = dialect.ok_or("Catalog selection is only supported for Doris and StarRocks")?;
|
||||
let mut queries = Vec::with_capacity(2);
|
||||
queries.push(catalog_switch_query(dialect, catalog));
|
||||
if !database.trim().is_empty() {
|
||||
queries.push(format!("USE {}", quote_identifier(database)));
|
||||
}
|
||||
Ok(queries)
|
||||
}
|
||||
|
||||
pub(crate) async fn apply_catalog_database_context(
|
||||
conn: &mut mysql_async::Conn,
|
||||
dialect: Option<MySqlCatalogDialect>,
|
||||
catalog: Option<&str>,
|
||||
database: &str,
|
||||
) -> Result<(), String> {
|
||||
for query in catalog_database_context_queries(dialect, catalog, database)? {
|
||||
conn.query_drop(&query).await.map_err(|error| format!("Failed to select query catalog/database: {error}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn should_enable_explicit_timestamp_defaults(sql: &str) -> bool {
|
||||
if !starts_with_executable_sql_keyword(sql, &["CREATE", "ALTER"]) {
|
||||
return false;
|
||||
@@ -1125,8 +1174,8 @@ fn mysql_connection_database(url: &str) -> Option<String> {
|
||||
|
||||
/// Extracts an opt-in `catalog=<name>` URL parameter. dbx strips it from the
|
||||
/// URL before handing it to mysql_async (see `is_dbx_handled_mysql_url_param`)
|
||||
/// and instead emits `SET catalog = <name>` during connection setup. This is
|
||||
/// how StarRocks/Doris connections reach an external catalog such as Paimon.
|
||||
/// and emits the database-specific catalog switch during connection setup.
|
||||
/// This is how StarRocks/Doris connections reach an external catalog such as Paimon.
|
||||
fn mysql_connection_catalog(url: &str) -> Option<String> {
|
||||
let (_, query) = url.split_once('?')?;
|
||||
let query = query.split('#').next().unwrap_or(query);
|
||||
@@ -1594,7 +1643,7 @@ fn mysql_url_param_value_is_true(value: &str) -> bool {
|
||||
/// Strips the database path from a `mysql://[user[:pass]@]host[:port][/path]`
|
||||
/// URL, returning only the scheme and authority. Used so mysql_async does not
|
||||
/// send the database as the schema during the MySQL handshake (StarRocks would
|
||||
/// reject an external-catalog database before `SET catalog` runs in setup).
|
||||
/// reject an external-catalog database before the catalog switch runs in setup).
|
||||
fn strip_mysql_url_path(base: &str) -> &str {
|
||||
let Some(rest) = base.strip_prefix("mysql://") else {
|
||||
return base;
|
||||
@@ -4111,6 +4160,32 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::db::connection_timeout;
|
||||
use mysql_async::consts::ColumnFlags;
|
||||
#[test]
|
||||
fn catalog_database_context_uses_database_specific_syntax_before_database() {
|
||||
assert_eq!(
|
||||
catalog_database_context_queries(Some(MySqlCatalogDialect::Doris), Some("paimon`catalog"), "bi").unwrap(),
|
||||
vec!["SWITCH `paimon``catalog`", "USE `bi`"]
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_database_context_queries(Some(MySqlCatalogDialect::StarRocks), Some("paimon`catalog"), "bi")
|
||||
.unwrap(),
|
||||
vec!["SET CATALOG `paimon``catalog`", "USE `bi`"]
|
||||
);
|
||||
assert_eq!(
|
||||
catalog_database_context_queries(Some(MySqlCatalogDialect::Doris), None, "").unwrap(),
|
||||
Vec::<String>::new()
|
||||
);
|
||||
assert!(catalog_database_context_queries(None, Some("paimon_catalog"), "bi").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catalog_dialect_supports_native_and_profile_connections() {
|
||||
assert_eq!(mysql_catalog_dialect(DatabaseType::Doris, None), Some(MySqlCatalogDialect::Doris));
|
||||
assert_eq!(mysql_catalog_dialect(DatabaseType::StarRocks, None), Some(MySqlCatalogDialect::StarRocks));
|
||||
assert_eq!(mysql_catalog_dialect(DatabaseType::Mysql, Some("selectdb")), Some(MySqlCatalogDialect::Doris));
|
||||
assert_eq!(mysql_catalog_dialect(DatabaseType::Mysql, Some("STARROCKS")), Some(MySqlCatalogDialect::StarRocks));
|
||||
assert_eq!(mysql_catalog_dialect(DatabaseType::Mysql, None), None);
|
||||
}
|
||||
|
||||
fn mysql_test_object(name: &str, object_type: &str) -> ObjectInfo {
|
||||
ObjectInfo {
|
||||
@@ -5316,7 +5391,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_compatible_setup_queries_keep_catalog_and_extra_queries() {
|
||||
fn mysql_compatible_setup_queries_leave_catalog_to_database_specific_setup() {
|
||||
let extra = vec!["SET ob_query_timeout = 30000000".to_string()];
|
||||
let queries = mysql_setup_queries_with_mode(
|
||||
"mysql://root:secret@localhost:9030/clip?catalog=paimon_catalog",
|
||||
@@ -5324,15 +5399,19 @@ mod tests {
|
||||
MySqlSetupMode::Compatible,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
queries,
|
||||
vec![
|
||||
"USE `clip`",
|
||||
"SET NAMES utf8mb4",
|
||||
"SET catalog = `paimon_catalog`",
|
||||
"SET ob_query_timeout = 30000000"
|
||||
]
|
||||
assert_eq!(queries, vec!["USE `clip`", "SET NAMES utf8mb4", "SET ob_query_timeout = 30000000"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_appends_database_specific_catalog_for_reverse_execution() {
|
||||
let extra = vec!["SWITCH `paimon_catalog`".to_string()];
|
||||
let queries = mysql_setup_queries_with_mode(
|
||||
"mysql://root:secret@localhost:9030/clip?catalog=paimon_catalog",
|
||||
&extra,
|
||||
MySqlSetupMode::Compatible,
|
||||
);
|
||||
|
||||
assert_eq!(queries, vec!["USE `clip`", "SET NAMES utf8mb4", "SWITCH `paimon_catalog`"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -5703,38 +5782,21 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_switch_catalog_when_present() {
|
||||
// `SET catalog` is pushed last so mysql_async's back-to-front setup
|
||||
// execution (Vec::pop) runs it before `USE <database>`.
|
||||
fn catalog_setup_query_for_url_uses_database_specific_syntax() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/clip?catalog=paimon_catalog", &[]),
|
||||
vec![
|
||||
"USE `clip`",
|
||||
"SET NAMES utf8mb4",
|
||||
"SET SESSION group_concat_max_len = 1048576",
|
||||
"SET catalog = `paimon_catalog`"
|
||||
]
|
||||
catalog_setup_query_for_url(MySqlCatalogDialect::Doris, "mysql://host:3306/clip?catalog=paimon_catalog"),
|
||||
Some("SWITCH `paimon_catalog`".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_switch_catalog_without_database() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/?catalog=paimon_catalog", &[]),
|
||||
vec!["SET NAMES utf8mb4", "SET SESSION group_concat_max_len = 1048576", "SET catalog = `paimon_catalog`"]
|
||||
catalog_setup_query_for_url(
|
||||
MySqlCatalogDialect::StarRocks,
|
||||
"mysql://host:3306/clip?catalog=paimon_catalog"
|
||||
),
|
||||
Some("SET CATALOG `paimon_catalog`".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_decodes_catalog_parameter() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?catalog=my%5Fcatalog", &[]),
|
||||
vec![
|
||||
"USE `db`",
|
||||
"SET NAMES utf8mb4",
|
||||
"SET SESSION group_concat_max_len = 1048576",
|
||||
"SET catalog = `my_catalog`"
|
||||
]
|
||||
catalog_setup_query_for_url(MySqlCatalogDialect::Doris, "mysql://host:3306/db?catalog=my%5Fcatalog"),
|
||||
Some("SWITCH `my_catalog`".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+151
-48
@@ -221,6 +221,25 @@ async fn connection_mysql_query_dialect(state: &AppState, connection_id: &str) -
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
async fn connection_mysql_catalog_dialect(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
) -> Option<db::mysql::MySqlCatalogDialect> {
|
||||
let configs = state.configs.read().await;
|
||||
configs
|
||||
.get(connection_id)
|
||||
.and_then(|config| db::mysql::mysql_catalog_dialect(config.db_type, config.driver_profile.as_deref()))
|
||||
}
|
||||
|
||||
async fn connection_mysql_catalog_dialect_for_pool_key(
|
||||
state: &AppState,
|
||||
pool_key: &str,
|
||||
) -> Option<db::mysql::MySqlCatalogDialect> {
|
||||
let configs = state.configs.read().await;
|
||||
crate::connection::config_for_pool_key(pool_key, &configs)
|
||||
.and_then(|config| db::mysql::mysql_catalog_dialect(config.db_type, config.driver_profile.as_deref()))
|
||||
}
|
||||
|
||||
async fn connection_database_type_for_pool_key(state: &AppState, pool_key: &str) -> Option<DatabaseType> {
|
||||
let configs = state.configs.read().await;
|
||||
configs
|
||||
@@ -401,6 +420,8 @@ pub struct QueryExecutionOptions {
|
||||
pub max_rows: Option<usize>,
|
||||
pub fetch_size: Option<usize>,
|
||||
pub page_size: Option<usize>,
|
||||
/// Doris / StarRocks catalog selected for this query tab.
|
||||
pub catalog: Option<String>,
|
||||
pub result_session_id: Option<String>,
|
||||
pub client_session_id: Option<String>,
|
||||
/// Query timeout in seconds. `None` uses the default (30s).
|
||||
@@ -1245,6 +1266,14 @@ fn resolve_query_timeout(timeout_secs: Option<u64>) -> Option<Duration> {
|
||||
}
|
||||
}
|
||||
|
||||
fn query_pool_database<'a>(database: &'a str, catalog: Option<&str>) -> Option<&'a str> {
|
||||
if database.is_empty() || catalog.is_some() {
|
||||
None
|
||||
} else {
|
||||
Some(database)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn operation_budget_for_pool_key(
|
||||
state: &AppState,
|
||||
pool_key: &str,
|
||||
@@ -1317,6 +1346,7 @@ pub async fn do_execute(
|
||||
crate::query_execution_sql::check_read_only(sql, &name, database_type)?;
|
||||
}
|
||||
let pool_db_type = connection_database_type_for_pool_key(state, pool_key).await;
|
||||
let mysql_catalog_dialect = connection_mysql_catalog_dialect_for_pool_key(state, pool_key).await;
|
||||
let connections = state.connections.read().await;
|
||||
let pool = connections.get(pool_key).ok_or("Connection not found")?;
|
||||
|
||||
@@ -1424,6 +1454,17 @@ pub async fn do_execute(
|
||||
});
|
||||
}
|
||||
apply_oceanbase_mysql_session_timeout(state, pool_key, &mut conn, options.timeout_secs).await?;
|
||||
wait_for_result_opt(
|
||||
cancel_token.clone(),
|
||||
query_timeout,
|
||||
db::mysql::apply_catalog_database_context(
|
||||
&mut conn,
|
||||
mysql_catalog_dialect,
|
||||
options.catalog.as_deref(),
|
||||
database.unwrap_or_default(),
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
wait_for_query_opt(
|
||||
cancel_token,
|
||||
query_timeout,
|
||||
@@ -1826,17 +1867,11 @@ pub async fn execute_sql_statement_with_options(
|
||||
// When a query tab has a client session, keep even database-less execution
|
||||
// on that tab-scoped pool so connection-level state (for example MySQL @vars)
|
||||
// survives across runs.
|
||||
let pool_key = if database.is_empty() {
|
||||
state
|
||||
.get_or_create_pool_for_session(connection_id, None, options.client_session_id.as_deref())
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?
|
||||
} else {
|
||||
state
|
||||
.get_or_create_pool_for_session(connection_id, Some(database), options.client_session_id.as_deref())
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?
|
||||
};
|
||||
let pool_database = query_pool_database(database, options.catalog.as_deref());
|
||||
let pool_key = state
|
||||
.get_or_create_pool_for_session(connection_id, pool_database, options.client_session_id.as_deref())
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;
|
||||
|
||||
if is_canceled(&cancel_token) {
|
||||
return Err(canceled_error());
|
||||
@@ -1853,9 +1888,9 @@ pub async fn execute_sql_statement_with_options(
|
||||
let action = result.as_ref().err().map(|e| query_pool_error_action(db_type, sql, e));
|
||||
match action {
|
||||
Some(PoolErrorAction::ReconnectAndRetry) if !is_canceled(&cancel_token) => {
|
||||
let db_opt = if database.is_empty() { None } else { Some(database) };
|
||||
let pool_database = query_pool_database(database, options.catalog.as_deref());
|
||||
let new_key = state
|
||||
.reconnect_pool_for_session(connection_id, db_opt, options.client_session_id.as_deref())
|
||||
.reconnect_pool_for_session(connection_id, pool_database, options.client_session_id.as_deref())
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;
|
||||
with_sql_context(
|
||||
@@ -1952,12 +1987,10 @@ pub async fn close_query_session(
|
||||
database: &str,
|
||||
session_id: &str,
|
||||
client_session_id: Option<&str>,
|
||||
catalog: Option<&str>,
|
||||
) -> Result<bool, String> {
|
||||
let pool_key = if database.is_empty() {
|
||||
state.get_or_create_pool_for_session(connection_id, None, client_session_id).await?
|
||||
} else {
|
||||
state.get_or_create_pool_for_session(connection_id, Some(database), client_session_id).await?
|
||||
};
|
||||
let pool_database = query_pool_database(database, catalog);
|
||||
let pool_key = state.get_or_create_pool_for_session(connection_id, pool_database, client_session_id).await?;
|
||||
|
||||
let connections = state.connections.read().await;
|
||||
let pool = connections.get(&pool_key).ok_or("Connection not found")?;
|
||||
@@ -2050,22 +2083,16 @@ pub async fn execute_multi_core_with_options_for_client_and_progress(
|
||||
options: QueryExecutionOptions,
|
||||
progress: Option<ExecuteMultiProgressCallback>,
|
||||
) -> Result<Vec<ExecuteMultiResult>, String> {
|
||||
let pool_database = query_pool_database(database, options.catalog.as_deref());
|
||||
// Reject MongoDB queries that fall through to the generic executor.
|
||||
if connection_is_mongodb(state, connection_id).await {
|
||||
return Err(MONGO_SHELL_COMMAND_HINT.to_string());
|
||||
}
|
||||
|
||||
let pool_key = if database.is_empty() {
|
||||
state
|
||||
.get_or_create_pool_for_session(connection_id, None, options.client_session_id.as_deref())
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?
|
||||
} else {
|
||||
state
|
||||
.get_or_create_pool_for_session(connection_id, Some(database), options.client_session_id.as_deref())
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?
|
||||
};
|
||||
let pool_key = state
|
||||
.get_or_create_pool_for_session(connection_id, pool_database, options.client_session_id.as_deref())
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql))?;
|
||||
if let Some(execution_id) = options.execution_id.as_deref() {
|
||||
state.running_queries.set_pool_key(execution_id, pool_key.clone());
|
||||
}
|
||||
@@ -2111,7 +2138,15 @@ pub async fn execute_multi_core_with_options_for_client_and_progress(
|
||||
// When use_transaction is explicitly true and we have multiple statements,
|
||||
// route through the transaction wrapper instead of the sequential auto-commit loop.
|
||||
if options.use_transaction == Some(true) && statements.len() > 1 {
|
||||
let result = execute_statements_in_transaction(state, connection_id, database, &statements, schema).await?;
|
||||
let result = execute_statements_in_transaction(
|
||||
state,
|
||||
connection_id,
|
||||
database,
|
||||
&statements,
|
||||
schema,
|
||||
options.catalog.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
return Ok(vec![result.into()]);
|
||||
}
|
||||
|
||||
@@ -2142,6 +2177,7 @@ pub async fn execute_multi_core_with_options_for_client_and_progress(
|
||||
// Read-only check for MySQL batch path
|
||||
check_read_only_for_connection_multi(state, &pool_key, &statements).await?;
|
||||
let mysql_dialect = connection_mysql_query_dialect(state, connection_id).await;
|
||||
let mysql_catalog_dialect = connection_mysql_catalog_dialect(state, connection_id).await;
|
||||
return execute_multi_mysql(
|
||||
state,
|
||||
&pool_key,
|
||||
@@ -2149,6 +2185,8 @@ pub async fn execute_multi_core_with_options_for_client_and_progress(
|
||||
&pool,
|
||||
mode,
|
||||
mysql_dialect,
|
||||
mysql_catalog_dialect,
|
||||
database,
|
||||
&statements,
|
||||
cancel_token,
|
||||
options,
|
||||
@@ -2269,6 +2307,7 @@ where
|
||||
(results, None)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn execute_multi_mysql(
|
||||
state: &AppState,
|
||||
pool_key: &str,
|
||||
@@ -2276,6 +2315,8 @@ async fn execute_multi_mysql(
|
||||
pool: &db::mysql::MySqlPool,
|
||||
mode: crate::connection::MysqlMode,
|
||||
dialect: db::mysql::MySqlQueryDialect,
|
||||
catalog_dialect: Option<db::mysql::MySqlCatalogDialect>,
|
||||
database: &str,
|
||||
statements: &[String],
|
||||
cancel_token: Option<CancellationToken>,
|
||||
options: QueryExecutionOptions,
|
||||
@@ -2304,6 +2345,12 @@ async fn execute_multi_mysql(
|
||||
}
|
||||
};
|
||||
apply_oceanbase_mysql_session_timeout(state, pool_key, &mut conn, options.timeout_secs).await?;
|
||||
wait_for_result_opt(
|
||||
cancel_token.clone(),
|
||||
query_timeout,
|
||||
db::mysql::apply_catalog_database_context(&mut conn, catalog_dialect, options.catalog.as_deref(), database),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let mut executor = MysqlBatchConnection {
|
||||
conn: &mut conn,
|
||||
@@ -2796,7 +2843,9 @@ pub async fn execute_schema_diff_deploy(
|
||||
};
|
||||
let atomicity = classify_schema_diff_atomicity(db_type, &parsed, has_transactional_path);
|
||||
|
||||
match execute_statements_in_transaction_on_pool(state, &pool_key, connection_id, database, &parsed, schema).await {
|
||||
match execute_statements_in_transaction_on_pool(state, &pool_key, connection_id, database, &parsed, schema, None)
|
||||
.await
|
||||
{
|
||||
Ok(result) => SchemaDiffDeployResult {
|
||||
transaction_id: tx_id,
|
||||
status: crate::two_phase_commit::TransactionStatus::Committed.as_str().to_string(),
|
||||
@@ -2852,18 +2901,17 @@ pub async fn execute_statements_in_transaction(
|
||||
database: &str,
|
||||
statements: &[String],
|
||||
schema: Option<&str>,
|
||||
catalog: Option<&str>,
|
||||
) -> Result<db::QueryResult, String> {
|
||||
let sql_ctx = statements.first().map(|s| s.as_str()).unwrap_or("");
|
||||
let pool_key = if database.is_empty() {
|
||||
connection_id.to_string()
|
||||
} else {
|
||||
state
|
||||
.get_or_create_pool(connection_id, Some(database))
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql_ctx))?
|
||||
};
|
||||
let pool_database = query_pool_database(database, catalog);
|
||||
let pool_key = state
|
||||
.get_or_create_pool(connection_id, pool_database)
|
||||
.await
|
||||
.map_err(|e| query_error_with_omitted_sql_context(&e, sql_ctx))?;
|
||||
|
||||
execute_statements_in_transaction_on_pool(state, &pool_key, connection_id, database, statements, schema).await
|
||||
execute_statements_in_transaction_on_pool(state, &pool_key, connection_id, database, statements, schema, catalog)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Execute multiple SQL statements transactionally on an already-resolved pool.
|
||||
@@ -2875,12 +2923,14 @@ pub async fn execute_statements_in_transaction_on_pool(
|
||||
database: &str,
|
||||
statements: &[String],
|
||||
schema: Option<&str>,
|
||||
catalog: Option<&str>,
|
||||
) -> Result<db::QueryResult, String> {
|
||||
// Read-only check: intercept all transaction paths before dispatching
|
||||
check_read_only_for_connection_multi(state, pool_key, statements).await?;
|
||||
|
||||
let start = std::time::Instant::now();
|
||||
let db_type = connection_database_type(state, connection_id).await;
|
||||
let mysql_catalog_dialect = connection_mysql_catalog_dialect(state, connection_id).await;
|
||||
let operation_budget = configured_operation_budget_for_pool_key(state, pool_key).await;
|
||||
|
||||
// Clone the pool handle within the lock, then drop it before any async work.
|
||||
@@ -2926,7 +2976,18 @@ pub async fn execute_statements_in_transaction_on_pool(
|
||||
exec_tx_pg_inner(pool, statements, schema, start, operation_budget.clone(), cancel_context).await
|
||||
}
|
||||
Some(TxPath::Mysql(pool, _bare)) => {
|
||||
exec_tx_mysql_inner(state, pool_key, pool, statements, start, operation_budget.clone()).await
|
||||
exec_tx_mysql_inner(
|
||||
state,
|
||||
pool_key,
|
||||
pool,
|
||||
statements,
|
||||
start,
|
||||
operation_budget.clone(),
|
||||
mysql_catalog_dialect,
|
||||
catalog,
|
||||
database,
|
||||
)
|
||||
.await
|
||||
}
|
||||
Some(TxPath::Sqlite(pool)) => exec_tx_sqlite_inner(pool, statements, start).await,
|
||||
Some(TxPath::CloudflareD1(client)) => {
|
||||
@@ -3068,9 +3129,13 @@ async fn exec_tx_mysql_inner(
|
||||
statements: &[String],
|
||||
start: std::time::Instant,
|
||||
budget: DbOperationBudget,
|
||||
catalog_dialect: Option<db::mysql::MySqlCatalogDialect>,
|
||||
catalog: Option<&str>,
|
||||
database: &str,
|
||||
) -> Result<db::QueryResult, String> {
|
||||
let mut conn = db::mysql::get_conn_with_health_check_with_timeout(&pool, budget.checkout_timeout).await?;
|
||||
apply_oceanbase_mysql_session_timeout(state, pool_key, &mut conn, None).await?;
|
||||
db::mysql::apply_catalog_database_context(&mut conn, catalog_dialect, catalog, database).await?;
|
||||
mysql_query_drop_with_timeout(
|
||||
&mut conn,
|
||||
"START TRANSACTION",
|
||||
@@ -3308,8 +3373,9 @@ pub async fn begin_manual_transaction(
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
schema: Option<&str>,
|
||||
catalog: Option<&str>,
|
||||
) -> Result<String, String> {
|
||||
begin_transaction_session(state, connection_id, database, schema, false).await
|
||||
begin_transaction_session(state, connection_id, database, schema, catalog, false).await
|
||||
}
|
||||
|
||||
/// Start a read-only, repeatable snapshot for a database backup.
|
||||
@@ -3318,7 +3384,7 @@ pub async fn begin_database_backup_snapshot(
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
) -> Result<String, String> {
|
||||
begin_transaction_session(state, connection_id, database, None, true).await
|
||||
begin_transaction_session(state, connection_id, database, None, None, true).await
|
||||
}
|
||||
|
||||
fn postgres_transaction_begin_sql(consistent_snapshot: bool) -> &'static str {
|
||||
@@ -3354,13 +3420,12 @@ async fn begin_transaction_session(
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
schema: Option<&str>,
|
||||
catalog: Option<&str>,
|
||||
consistent_snapshot: bool,
|
||||
) -> Result<String, String> {
|
||||
let pool_key = if database.is_empty() {
|
||||
connection_id.to_string()
|
||||
} else {
|
||||
state.get_or_create_pool(connection_id, Some(database)).await?
|
||||
};
|
||||
let mysql_catalog_dialect = connection_mysql_catalog_dialect(state, connection_id).await;
|
||||
let pool_database = query_pool_database(database, catalog);
|
||||
let pool_key = state.get_or_create_pool(connection_id, pool_database).await?;
|
||||
|
||||
// Clone the pool handle under a brief read lock, then drop the lock before
|
||||
// any async I/O — same pattern as do_execute throughout this file.
|
||||
@@ -3397,6 +3462,7 @@ async fn begin_transaction_session(
|
||||
}
|
||||
TxnPoolHandle::Mysql(mysql_pool) => {
|
||||
let mut conn = mysql_pool.get_conn().await.map_err(|e| format!("Failed to get MySQL connection: {e}"))?;
|
||||
db::mysql::apply_catalog_database_context(&mut conn, mysql_catalog_dialect, catalog, database).await?;
|
||||
if let Some(isolation_sql) = mysql_transaction_isolation_sql(consistent_snapshot) {
|
||||
conn.query_drop(isolation_sql).await.map_err(|e| format!("SET TRANSACTION failed: {e}"))?;
|
||||
}
|
||||
@@ -3889,6 +3955,43 @@ mod tests {
|
||||
};
|
||||
use crate::storage::Storage;
|
||||
|
||||
#[test]
|
||||
fn external_catalog_queries_do_not_bind_database_during_pool_creation() {
|
||||
assert_eq!(query_pool_database("bi", Some("paimon_catalog")), None);
|
||||
assert_eq!(query_pool_database("bi", None), Some("bi"));
|
||||
assert_eq!(query_pool_database("", None), None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn query_and_transaction_paths_resolve_catalog_dialect_from_connection() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-catalog-dialect-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let storage = Storage::open(&dir.join("storage.db")).await.unwrap();
|
||||
let state = AppState::new(storage);
|
||||
|
||||
let mut doris = test_connection_config(DatabaseType::Doris);
|
||||
doris.id = "doris".to_string();
|
||||
let mut starrocks = test_connection_config(DatabaseType::StarRocks);
|
||||
starrocks.id = "starrocks".to_string();
|
||||
{
|
||||
let mut configs = state.configs.write().await;
|
||||
configs.insert(doris.id.clone(), doris);
|
||||
configs.insert(starrocks.id.clone(), starrocks);
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
connection_mysql_catalog_dialect_for_pool_key(&state, "doris:bi").await,
|
||||
Some(db::mysql::MySqlCatalogDialect::Doris)
|
||||
);
|
||||
assert_eq!(
|
||||
connection_mysql_catalog_dialect(&state, "starrocks").await,
|
||||
Some(db::mysql::MySqlCatalogDialect::StarRocks)
|
||||
);
|
||||
|
||||
drop(state);
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn schema_diff_atomicity_marks_mysql_ddl_as_partial() {
|
||||
let atomicity = classify_schema_diff_atomicity(
|
||||
|
||||
@@ -588,6 +588,7 @@ pub async fn export_query_result_core(
|
||||
&request.database,
|
||||
&session_id,
|
||||
request.client_session_id.as_deref(),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -582,6 +582,7 @@ async fn close_table_export_cursor_if_open(
|
||||
&request.database,
|
||||
&session_id,
|
||||
Some(&client_session_id),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -3744,6 +3744,7 @@ async fn execute_import_transaction(
|
||||
database,
|
||||
statements,
|
||||
(!schema.trim().is_empty()).then_some(schema),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
*db_write_ms += started_at.elapsed().as_millis();
|
||||
|
||||
Reference in new issue
Block a user