fix(tdsql): list only logical partitioned tables

Closes #6660
This commit is contained in:
t8y2 committed 2026-08-19 07:55:10 +00:00
1 parent 68d93c9427
commit bf6c8d4d2b
2 files changed
+84 -14

No files matched your search

+66 -9
View File
@@ -2816,7 +2816,7 @@ async fn list_table_names_show(pool: &MySqlPool, database: &str) -> Result<Vec<T
list_table_names_show_filtered(pool, database, None, &[]).await list_table_names_show_filtered(pool, database, None, &[]).await
} }
fn shardingsphere_show_full_tables_sql(database: &str) -> String { fn logical_show_full_tables_sql(database: &str) -> String {
if database.trim().is_empty() { if database.trim().is_empty() {
"SHOW FULL TABLES".to_string() "SHOW FULL TABLES".to_string()
} else { } else {
@@ -2840,16 +2840,32 @@ fn table_infos_from_show_rows(rows: &[mysql_async::Row]) -> Vec<TableInfo> {
tables tables
} }
pub async fn list_shardingsphere_tables(pool: &MySqlPool, database: &str) -> Result<Vec<TableInfo>, String> { pub async fn list_logical_tables_show(pool: &MySqlPool, database: &str) -> Result<Vec<TableInfo>, String> {
// ShardingSphere's logical names are authoritative here. Do not add SHOW TABLE STATUS: // Proxy logical names are authoritative here. Do not add SHOW TABLE STATUS: this hot
// this hot path must replace the information_schema lookup with one metadata request. // path must replace the information_schema lookup with one metadata request.
let sql = shardingsphere_show_full_tables_sql(database); let sql = logical_show_full_tables_sql(database);
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?; let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter(&sql).await.map_err(|error| error.to_string())?; let result = conn.query_iter(&sql).await.map_err(|error| error.to_string())?;
let rows = result.collect_and_drop::<mysql_async::Row>().await.map_err(|error| error.to_string())?; let rows = result.collect_and_drop::<mysql_async::Row>().await.map_err(|error| error.to_string())?;
Ok(table_infos_from_show_rows(&rows)) Ok(table_infos_from_show_rows(&rows))
} }
async fn list_logical_table_objects_show_filtered(
pool: &MySqlPool,
database: &str,
object_types: Option<&[String]>,
limit: Option<usize>,
offset: Option<usize>,
) -> Result<Vec<ObjectInfo>, String> {
let tables = list_logical_tables_show(pool, database).await?;
Ok(filter_table_objects_fallback(
table_infos_to_objects(tables, &HashMap::new(), database),
object_types,
limit,
offset,
))
}
async fn list_table_names_show_filtered( async fn list_table_names_show_filtered(
pool: &MySqlPool, pool: &MySqlPool,
database: &str, database: &str,
@@ -3264,6 +3280,14 @@ fn object_query_supports_paging(object_types: Option<&[String]>) -> bool {
all_types_supported && uses_table_source != uses_routine_source all_types_supported && uses_table_source != uses_routine_source
} }
fn logical_table_supplemental_types(object_types: Option<&[String]>) -> Vec<String> {
["PROCEDURE", "FUNCTION", "TRIGGER", "EVENT"]
.into_iter()
.filter(|object_type| requested_object_type(object_types, object_type))
.map(str::to_string)
.collect()
}
pub async fn list_objects( pub async fn list_objects(
pool: &MySqlPool, pool: &MySqlPool,
database: &str, database: &str,
@@ -3368,6 +3392,29 @@ pub async fn list_objects(
Ok(PagedObjectList { objects, paging_applied }) Ok(PagedObjectList { objects, paging_applied })
} }
pub async fn list_objects_with_logical_tables(
pool: &MySqlPool,
database: &str,
object_types: Option<&[String]>,
limit: Option<usize>,
offset: Option<usize>,
) -> Result<PagedObjectList, String> {
if !wants_table_objects(object_types) {
return list_objects(pool, database, object_types, limit, offset).await;
}
let paging_applied = limit.is_some() && object_query_supports_paging(object_types);
let (query_limit, query_offset) = if paging_applied { (limit, offset) } else { (None, None) };
let mut objects =
list_logical_table_objects_show_filtered(pool, database, object_types, query_limit, query_offset).await?;
let supplemental_types = logical_table_supplemental_types(object_types);
if !supplemental_types.is_empty() {
objects.extend(list_objects(pool, database, Some(&supplemental_types), None, None).await?.objects);
}
Ok(PagedObjectList { objects, paging_applied })
}
pub async fn list_object_statistics(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectStatistics>, String> { pub async fn list_object_statistics(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectStatistics>, String> {
let sql = format!( let sql = format!(
"SELECT TABLE_NAME, TABLE_ROWS, COALESCE(DATA_LENGTH, 0) + COALESCE(INDEX_LENGTH, 0) AS TOTAL_BYTES \ "SELECT TABLE_NAME, TABLE_ROWS, COALESCE(DATA_LENGTH, 0) + COALESCE(INDEX_LENGTH, 0) AS TOTAL_BYTES \
@@ -6159,13 +6206,13 @@ mod tests {
} }
#[test] #[test]
fn shardingsphere_show_full_tables_is_one_exact_statement() { fn logical_show_full_tables_is_one_exact_statement() {
assert_eq!(shardingsphere_show_full_tables_sql("app"), "SHOW FULL TABLES FROM `app`"); assert_eq!(logical_show_full_tables_sql("app"), "SHOW FULL TABLES FROM `app`");
assert_eq!(shardingsphere_show_full_tables_sql(""), "SHOW FULL TABLES"); assert_eq!(logical_show_full_tables_sql(""), "SHOW FULL TABLES");
} }
#[test] #[test]
fn shardingsphere_show_rows_keep_logical_names_and_types_without_comments() { fn proxy_show_rows_keep_logical_names_and_types_without_comments() {
let rows = vec![ let rows = vec![
mysql_test_row(vec![Value::Bytes(b"normal_table".to_vec()), Value::Bytes(b"BASE TABLE".to_vec())]), mysql_test_row(vec![Value::Bytes(b"normal_table".to_vec()), Value::Bytes(b"BASE TABLE".to_vec())]),
mysql_test_row(vec![Value::Bytes(b"t_order".to_vec()), Value::Bytes(b"BASE TABLE".to_vec())]), mysql_test_row(vec![Value::Bytes(b"t_order".to_vec()), Value::Bytes(b"BASE TABLE".to_vec())]),
@@ -6474,6 +6521,16 @@ mod tests {
assert!(!object_query_supports_paging(None)); assert!(!object_query_supports_paging(None));
} }
#[test]
fn logical_table_objects_keep_only_requested_non_table_sources() {
assert_eq!(logical_table_supplemental_types(None), ["PROCEDURE", "FUNCTION", "TRIGGER", "EVENT"]);
assert!(logical_table_supplemental_types(Some(&["TABLE".to_string(), "VIEW".to_string()])).is_empty());
assert_eq!(
logical_table_supplemental_types(Some(&["TABLE".to_string(), "PROCEDURE".to_string()])),
["PROCEDURE"]
);
}
#[test] #[test]
fn mysql_completion_triggers_sql_lists_database_triggers() { fn mysql_completion_triggers_sql_lists_database_triggers() {
let sql = list_completion_triggers_sql("app"); let sql = list_completion_triggers_sql("app");
+18 -5
View File
@@ -2278,7 +2278,7 @@ async fn list_tables_once(
let tables = db::ob_oracle::list_tables(p, schema).await?; let tables = db::ob_oracle::list_tables(p, schema).await?;
Ok(filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter)) Ok(filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter))
} else if mysql_table_list_source_for_config(db_config.as_ref()) == MysqlTableListSource::ShowFullTables { } else if mysql_table_list_source_for_config(db_config.as_ref()) == MysqlTableListSource::ShowFullTables {
db::mysql::list_shardingsphere_tables(p, mysql_table_metadata_catalog(database, schema)) db::mysql::list_logical_tables_show(p, mysql_table_metadata_catalog(database, schema))
.await .await
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter)) .map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter))
} else { } else {
@@ -2739,10 +2739,11 @@ fn is_shardingsphere_proxy_version(version: &str) -> bool {
} }
fn mysql_table_list_source_for_config(config: Option<&ConnectionConfig>) -> MysqlTableListSource { fn mysql_table_list_source_for_config(config: Option<&ConnectionConfig>) -> MysqlTableListSource {
if config if config.is_some_and(db::tdsql_mysql::is_config)
.and_then(|config| config.database_info.as_ref()) || config
.and_then(|info| info.product_version.as_deref()) .and_then(|config| config.database_info.as_ref())
.is_some_and(is_shardingsphere_proxy_version) .and_then(|info| info.product_version.as_deref())
.is_some_and(is_shardingsphere_proxy_version)
{ {
MysqlTableListSource::ShowFullTables MysqlTableListSource::ShowFullTables
} else { } else {
@@ -3859,6 +3860,14 @@ for line in sys.stdin:
assert_eq!(mysql_table_list_source_for_config(None), MysqlTableListSource::InformationSchema); assert_eq!(mysql_table_list_source_for_config(None), MysqlTableListSource::InformationSchema);
} }
#[test]
fn tdsql_profile_uses_logical_show_full_tables_source() {
let mut config = test_connection_config(DatabaseType::Mysql);
config.driver_profile = Some("TDSQL".to_string());
assert_eq!(mysql_table_list_source_for_config(Some(&config)), MysqlTableListSource::ShowFullTables);
}
#[test] #[test]
fn shardingsphere_logical_tables_keep_local_constraints() { fn shardingsphere_logical_tables_keep_local_constraints() {
let tables = vec![ let tables = vec![
@@ -5422,6 +5431,10 @@ async fn list_objects_once(
db::starrocks::list_table_objects(p, database).await.map(unpaged_object_list) db::starrocks::list_table_objects(p, database).await.map(unpaged_object_list)
} else if db_config.as_ref().is_some_and(db::mysql_compatible::uses_show_metadata) { } else if db_config.as_ref().is_some_and(db::mysql_compatible::uses_show_metadata) {
db::mysql::list_table_objects_show(p, database).await.map(unpaged_object_list) db::mysql::list_table_objects_show(p, database).await.map(unpaged_object_list)
} else if mysql_table_list_source_for_config(db_config.as_ref()) == MysqlTableListSource::ShowFullTables {
db::mysql::list_objects_with_logical_tables(p, database, object_types, mysql_limit, mysql_offset)
.await
.map(|result| ObjectListOutcome { objects: result.objects, paging_applied: result.paging_applied })
} else { } else {
db::mysql::list_objects(p, database, object_types, mysql_limit, mysql_offset) db::mysql::list_objects(p, database, object_types, mysql_limit, mysql_offset)
.await .await