fix(kingbase): quote MySQL-compatible identifiers
This commit is contained in:
39 files changed
+373
-23
No files matched your search
@@ -39,6 +39,7 @@
|
||||
"connect",
|
||||
"test_connection",
|
||||
"validate_connection",
|
||||
"connection_info",
|
||||
"list_databases",
|
||||
"list_schemas",
|
||||
"list_tables",
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
"cancel_session",
|
||||
"test_connection",
|
||||
"validate_connection",
|
||||
"connection_info",
|
||||
"list_databases",
|
||||
"list_schemas",
|
||||
"list_tables",
|
||||
|
||||
@@ -2317,6 +2317,35 @@ impl AppState {
|
||||
keys
|
||||
}
|
||||
|
||||
pub async fn connection_identifier_quote(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
database: Option<&str>,
|
||||
) -> Result<Option<String>, String> {
|
||||
let config = self
|
||||
.configs
|
||||
.read()
|
||||
.await
|
||||
.get(connection_id)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("Connection config not found: {connection_id}"))?;
|
||||
if !database_capabilities::is_agent_type(&config.db_type) {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let pool_key = self.get_or_create_pool(connection_id, database).await?;
|
||||
let client = {
|
||||
let connections = self.connections.read().await;
|
||||
match connections.get(&pool_key) {
|
||||
Some(PoolKind::Agent(client)) => client.clone(),
|
||||
_ => return Ok(None),
|
||||
}
|
||||
};
|
||||
let mut agent = client.lock().await;
|
||||
let info = agent.connection_info(Some(db::connection_timeout())).await?;
|
||||
Ok(Some(info.identifier_quote))
|
||||
}
|
||||
|
||||
pub async fn reset_connection_transport(&self, connection_id: &str) {
|
||||
let layer_count = {
|
||||
let configs = self.configs.read().await;
|
||||
|
||||
@@ -217,6 +217,8 @@ pub struct DataGridCountSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identifier_quote: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub catalog: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
@@ -721,12 +723,21 @@ pub fn build_data_grid_column_distinct_values_sql(options: DataGridColumnDistinc
|
||||
}
|
||||
|
||||
pub fn build_data_grid_count_sql(options: DataGridCountSqlOptions) -> String {
|
||||
let table = crate::sql_dialect::qualified_table_name_with_catalog(
|
||||
options.database_type,
|
||||
options.catalog.as_deref(),
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
);
|
||||
let table = if options.database_type == Some(DatabaseType::Kingbase) {
|
||||
crate::sql_dialect::table_data_qualified_table_name(
|
||||
options.database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
)
|
||||
} else {
|
||||
crate::sql_dialect::qualified_table_name_with_catalog(
|
||||
options.database_type,
|
||||
options.catalog.as_deref(),
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
)
|
||||
};
|
||||
let predicate = crate::sql_dialect::normalize_where_input(options.where_input.as_deref());
|
||||
let where_clause = if predicate.is_empty() { String::new() } else { format!(" WHERE ({predicate})") };
|
||||
format!("SELECT COUNT(*) AS cnt FROM {table}{where_clause}")
|
||||
@@ -3060,6 +3071,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_count_sql(DataGridCountSqlOptions {
|
||||
database_type: Some(DatabaseType::Postgres),
|
||||
identifier_quote: None,
|
||||
catalog: None,
|
||||
schema: Some("public".to_string()),
|
||||
table_name: "users".to_string(),
|
||||
@@ -3070,6 +3082,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_count_sql(DataGridCountSqlOptions {
|
||||
database_type: Some(DatabaseType::Doris),
|
||||
identifier_quote: None,
|
||||
catalog: Some("iceberg_catalog".to_string()),
|
||||
schema: Some("sales".to_string()),
|
||||
table_name: "orders".to_string(),
|
||||
@@ -3080,6 +3093,7 @@ mod tests {
|
||||
assert_eq!(
|
||||
build_data_grid_count_sql(DataGridCountSqlOptions {
|
||||
database_type: Some(DatabaseType::StarRocks),
|
||||
identifier_quote: None,
|
||||
catalog: Some("hive_catalog".to_string()),
|
||||
schema: None,
|
||||
table_name: "orders".to_string(),
|
||||
@@ -3087,6 +3101,17 @@ mod tests {
|
||||
}),
|
||||
"SELECT COUNT(*) AS cnt FROM `hive_catalog`.`orders`"
|
||||
);
|
||||
assert_eq!(
|
||||
build_data_grid_count_sql(DataGridCountSqlOptions {
|
||||
database_type: Some(DatabaseType::Kingbase),
|
||||
identifier_quote: Some("`".to_string()),
|
||||
catalog: None,
|
||||
schema: Some("cqbq_ls".to_string()),
|
||||
table_name: "ANALYZE".to_string(),
|
||||
where_input: None,
|
||||
}),
|
||||
"SELECT COUNT(*) AS cnt FROM `cqbq_ls`.`ANALYZE`"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -373,6 +373,7 @@ pub enum AgentMethod {
|
||||
CancelSession,
|
||||
TestConnection,
|
||||
ValidateConnection,
|
||||
ConnectionInfo,
|
||||
ListDatabases,
|
||||
ListSchemas,
|
||||
ListTables,
|
||||
@@ -400,7 +401,7 @@ pub enum AgentMethod {
|
||||
}
|
||||
|
||||
impl AgentMethod {
|
||||
pub const ALL: [Self; 32] = [
|
||||
pub const ALL: [Self; 33] = [
|
||||
Self::Handshake,
|
||||
Self::Connect,
|
||||
Self::OpenSession,
|
||||
@@ -409,6 +410,7 @@ impl AgentMethod {
|
||||
Self::CancelSession,
|
||||
Self::TestConnection,
|
||||
Self::ValidateConnection,
|
||||
Self::ConnectionInfo,
|
||||
Self::ListDatabases,
|
||||
Self::ListSchemas,
|
||||
Self::ListTables,
|
||||
@@ -445,6 +447,7 @@ impl AgentMethod {
|
||||
Self::CancelSession => "cancel_session",
|
||||
Self::TestConnection => "test_connection",
|
||||
Self::ValidateConnection => "validate_connection",
|
||||
Self::ConnectionInfo => "connection_info",
|
||||
Self::ListDatabases => "list_databases",
|
||||
Self::ListSchemas => "list_schemas",
|
||||
Self::ListTables => "list_tables",
|
||||
@@ -473,6 +476,12 @@ impl AgentMethod {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentConnectionInfo {
|
||||
pub identifier_quote: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentTableReadStartParams {
|
||||
@@ -910,6 +919,10 @@ impl AgentDriverClient {
|
||||
self.call_method_with_timeout(AgentMethod::ValidateConnection, serde_json::json!({}), timeout_duration).await
|
||||
}
|
||||
|
||||
pub async fn connection_info(&mut self, timeout_duration: Option<Duration>) -> Result<AgentConnectionInfo, String> {
|
||||
self.call_method_with_timeout(AgentMethod::ConnectionInfo, serde_json::json!({}), timeout_duration).await
|
||||
}
|
||||
|
||||
pub async fn disconnect(&mut self) -> Result<Value, String> {
|
||||
if self.shared_runtime.is_some() {
|
||||
let session_id = self.agent_session_id.as_ref().ok_or("Shared Agent session id is missing")?.clone();
|
||||
|
||||
@@ -14,5 +14,6 @@ pub use identifiers::{
|
||||
normalize_where_input, qualified_table_name, qualified_table_name_with_catalog, quote_table_identifier,
|
||||
};
|
||||
pub(crate) use identifiers::{parse_sqlserver_linked_schema_ref, qualified_transfer_table, quote_transfer_identifier};
|
||||
pub(crate) use table_select::table_data_qualified_table_name;
|
||||
pub use table_select::{build_count_table_sql, build_table_data_select_sql, build_table_select_sql};
|
||||
pub use types::*;
|
||||
@@ -21,12 +21,21 @@ pub fn build_table_data_select_sql(options: TableDataSelectSqlOptions) -> String
|
||||
}
|
||||
|
||||
// Doris / StarRocks multi-catalog: prefix the catalog for external-catalog tables.
|
||||
let table = qualified_table_name_with_catalog(
|
||||
database_type,
|
||||
options.catalog.as_deref(),
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
);
|
||||
let table = if database_type == Some(DatabaseType::Kingbase) {
|
||||
table_data_qualified_table_name(
|
||||
database_type,
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
)
|
||||
} else {
|
||||
qualified_table_name_with_catalog(
|
||||
database_type,
|
||||
options.catalog.as_deref(),
|
||||
options.schema.as_deref(),
|
||||
&options.table_name,
|
||||
)
|
||||
};
|
||||
let predicate = normalize_where_input(options.where_input.as_deref());
|
||||
let where_clause = if predicate.is_empty() { String::new() } else { format!(" WHERE ({predicate})") };
|
||||
let default_order_by = if database_type == Some(DatabaseType::InfluxDb) {
|
||||
@@ -143,6 +152,40 @@ pub fn build_table_data_select_sql(options: TableDataSelectSqlOptions) -> String
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn table_data_qualified_table_name(
|
||||
database_type: Option<DatabaseType>,
|
||||
schema: Option<&str>,
|
||||
table_name: &str,
|
||||
identifier_quote: Option<&str>,
|
||||
) -> String {
|
||||
if database_type != Some(DatabaseType::Kingbase) {
|
||||
return qualified_table_name(database_type, schema, table_name);
|
||||
}
|
||||
let table = quote_table_data_identifier(database_type, table_name, identifier_quote);
|
||||
schema
|
||||
.map(str::trim)
|
||||
.filter(|schema| !schema.is_empty())
|
||||
.map(|schema| format!("{}.{}", quote_table_data_identifier(database_type, schema, identifier_quote), table))
|
||||
.unwrap_or(table)
|
||||
}
|
||||
|
||||
fn quote_table_data_identifier(
|
||||
database_type: Option<DatabaseType>,
|
||||
name: &str,
|
||||
identifier_quote: Option<&str>,
|
||||
) -> String {
|
||||
if database_type != Some(DatabaseType::Kingbase) {
|
||||
return quote_table_identifier(database_type, name);
|
||||
}
|
||||
let Some(quote) = identifier_quote else {
|
||||
return quote_table_identifier(database_type, name);
|
||||
};
|
||||
if quote.is_empty() {
|
||||
return name.to_string();
|
||||
}
|
||||
format!("{quote}{}{quote}", name.replace(quote, &format!("{quote}{quote}")))
|
||||
}
|
||||
|
||||
fn is_view_table_type(table_type: Option<&str>) -> bool {
|
||||
table_type.is_some_and(|value| value.to_ascii_uppercase().contains("VIEW"))
|
||||
}
|
||||
@@ -418,6 +461,7 @@ mod tests {
|
||||
fn opts(database_type: DatabaseType, catalog: Option<&str>, table: &str) -> TableDataSelectSqlOptions {
|
||||
TableDataSelectSqlOptions {
|
||||
database_type: Some(database_type),
|
||||
identifier_quote: None,
|
||||
schema: None,
|
||||
table_name: table.to_string(),
|
||||
catalog: catalog.map(|c| c.to_string()),
|
||||
|
||||
@@ -21,6 +21,11 @@ fn quotes_identifiers_by_database_type() {
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::StarRocks), "user`name"), "`user``name`");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::SqlServer), "user]name"), "[user]]name]");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Postgres), "user\"name"), "\"user\"\"name\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Kingbase), "cqbq_ls"), "\"cqbq_ls\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Kingbase), "actionlogs"), "\"actionlogs\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Kingbase), "order"), "\"order\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Kingbase), "MixedCase"), "\"MixedCase\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Kingbase), "order detail"), "\"order detail\"");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Informix), "users_1"), "users_1");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Jdbc), "users_1"), "users_1");
|
||||
assert_eq!(quote_table_identifier(Some(DatabaseType::Jdbc), "user name"), "user name");
|
||||
@@ -31,6 +36,10 @@ fn quotes_identifiers_by_database_type() {
|
||||
fn qualifies_schema_only_for_schema_aware_databases() {
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Postgres), Some("public"), "users"), "\"public\".\"users\"");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Kwdb), Some("public"), "users"), "\"public\".\"users\"");
|
||||
assert_eq!(
|
||||
qualified_table_name(Some(DatabaseType::Kingbase), Some("cqbq_ls"), "actionlogs"),
|
||||
"\"cqbq_ls\".\"actionlogs\""
|
||||
);
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Mysql), Some("public"), "users"), "`public`.`users`");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::Goldendb), Some("public"), "users"), "`public`.`users`");
|
||||
assert_eq!(qualified_table_name(Some(DatabaseType::StarRocks), Some("warehouse"), "users"), "`warehouse`.`users`");
|
||||
@@ -244,6 +253,10 @@ fn builds_select_sql_with_limit_syntax_for_database_type() {
|
||||
|
||||
#[test]
|
||||
fn builds_table_data_where_and_schema_queries() {
|
||||
assert_eq!(
|
||||
build_count_table_sql(Some(DatabaseType::Kingbase), Some("cqbq_ls"), "actionlogs"),
|
||||
"SELECT COUNT(*) AS row_count FROM \"cqbq_ls\".\"actionlogs\""
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
@@ -298,6 +311,36 @@ fn builds_table_data_where_and_schema_queries() {
|
||||
}),
|
||||
"SELECT * FROM \"public\".\"orders\" WHERE (amount > 10) LIMIT 50 OFFSET 100;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::Kingbase),
|
||||
identifier_quote: Some("`".to_string()),
|
||||
schema: Some("cqbq_ls".to_string()),
|
||||
table_name: "actionlogs".to_string(),
|
||||
table_type: None,
|
||||
primary_keys: Vec::new(),
|
||||
columns: Vec::new(),
|
||||
fallback_order_columns: Vec::new(),
|
||||
order_by: None,
|
||||
limit: Some(100),
|
||||
offset: None,
|
||||
where_input: None,
|
||||
include_row_id: false,
|
||||
..Default::default()
|
||||
}),
|
||||
"SELECT * FROM `cqbq_ls`.`actionlogs` LIMIT 100;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::Kingbase),
|
||||
identifier_quote: Some("\"".to_string()),
|
||||
schema: Some("App Schema".to_string()),
|
||||
table_name: "ANALYZE".to_string(),
|
||||
limit: Some(100),
|
||||
..Default::default()
|
||||
}),
|
||||
"SELECT * FROM \"App Schema\".\"ANALYZE\" LIMIT 100;"
|
||||
);
|
||||
assert_eq!(
|
||||
build_table_data_select_sql(TableDataSelectSqlOptions {
|
||||
database_type: Some(DatabaseType::Xugu),
|
||||
|
||||
@@ -22,6 +22,8 @@ pub struct TableDataSelectSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identifier_quote: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
pub table_name: String,
|
||||
/// Doris / StarRocks multi-catalog: when set to a non-`internal` catalog,
|
||||
|
||||
Reference in new issue
Block a user