feat(objects): add table statistics columns

This commit is contained in:
t8y2 committed 2026-06-19 14:35:32 +08:00
1 parent 4186c94dd4
commit d59997c090
23 files changed
+360 -19

No files matched your search

+38 -1
View File
@@ -13,7 +13,8 @@ use std::time::Instant;
use crate::models::connection::DatabaseType;
use crate::sql::starts_with_executable_sql_keyword;
use crate::types::{
ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, ObjectInfo, QueryResult, TableInfo, TriggerInfo,
ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, ObjectInfo, ObjectStatistics, QueryResult, TableInfo,
TriggerInfo,
};
use super::file_validator::validate_file_path;
@@ -103,6 +104,17 @@ fn get_opt_i32(row: &mysql_async::Row, name: &str) -> Option<i32> {
})
}
fn get_opt_i64(row: &mysql_async::Row, name: &str) -> Option<i64> {
row_get::<i64, _>(row, name)
.or_else(|| row_get::<u64, _>(row, name).and_then(|value| i64::try_from(value).ok()))
.or_else(|| row_get::<String, _>(row, name).and_then(|value| value.parse::<i64>().ok()))
.or_else(|| {
row_get::<Vec<u8>, _>(row, name)
.and_then(|b| String::from_utf8(b).ok())
.and_then(|value| value.parse::<i64>().ok())
})
}
#[cfg(test)]
fn mysql_datetime_to_string(value: NaiveDateTime) -> String {
value.to_string()
@@ -1195,6 +1207,31 @@ pub async fn list_objects(pool: &MySqlPool, database: &str) -> Result<Vec<Object
Ok(objects)
}
pub async fn list_object_statistics(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectStatistics>, String> {
let sql = format!(
"SELECT TABLE_NAME, TABLE_ROWS, COALESCE(DATA_LENGTH, 0) + COALESCE(INDEX_LENGTH, 0) AS TOTAL_BYTES \
FROM information_schema.TABLES \
WHERE TABLE_SCHEMA = {} AND TABLE_TYPE <> 'VIEW' \
ORDER BY TABLE_NAME",
quote_value(database),
);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.filter_map(|row| {
let name = get_str_by_name(row, "TABLE_NAME").trim().to_string();
(!name.is_empty()).then_some(ObjectStatistics {
name,
schema: Some(database.to_string()),
estimated_rows: get_opt_i64(row, "TABLE_ROWS"),
total_bytes: get_opt_i64(row, "TOTAL_BYTES"),
})
})
.collect())
}
pub async fn list_table_objects_show(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let (tables, routines) =
tokio::join!(list_tables_show_with_status(pool, database), list_routine_objects(pool, database));
+29 -2
View File
@@ -20,8 +20,8 @@ use tokio_postgres::{Row, SimpleQueryMessage};
use super::file_validator::validate_file_path;
use crate::sql::starts_with_executable_sql_keyword;
use crate::types::{
ColumnInfo, DatabaseInfo, ForeignKeyInfo, FunctionInfo, IndexInfo, ObjectInfo, OwnerInfo, QueryResult, RuleInfo,
SequenceInfo, TableInfo, TriggerInfo,
ColumnInfo, DatabaseInfo, ForeignKeyInfo, FunctionInfo, IndexInfo, ObjectInfo, ObjectStatistics, OwnerInfo,
QueryResult, RuleInfo, SequenceInfo, TableInfo, TriggerInfo,
};
fn pg_temporal_to_json_value(row: &Row, idx: usize) -> Option<serde_json::Value> {
@@ -1102,6 +1102,33 @@ pub async fn list_objects(pool: &Pool, schema: &str) -> Result<Vec<ObjectInfo>,
.collect())
}
pub async fn list_object_statistics(pool: &Pool, schema: &str) -> Result<Vec<ObjectStatistics>, String> {
let schema = if schema.is_empty() { "public" } else { schema };
let client = pool.get().await.map_err(|e| e.to_string())?;
let stmt = client
.prepare_cached(
"SELECT c.relname, \
GREATEST(c.reltuples, 0)::bigint AS estimated_rows, \
pg_catalog.pg_total_relation_size(c.oid)::bigint AS total_bytes \
FROM pg_catalog.pg_class c \
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
WHERE n.nspname = $1 AND c.relkind IN ('r','m','f','p') \
ORDER BY c.relname",
)
.await
.map_err(|e| e.to_string())?;
let rows = client.query(&stmt, &[&schema]).await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| ObjectStatistics {
name: row.get::<_, String>(0),
schema: Some(schema.to_string()),
estimated_rows: row.try_get::<_, i64>(1).ok(),
total_bytes: row.try_get::<_, i64>(2).ok(),
})
.collect())
}
pub async fn list_schemas(pool: &Pool) -> Result<Vec<String>, String> {
let client = pool.get().await.map_err(|e| e.to_string())?;
let stmt = client
+40 -1
View File
@@ -1,7 +1,8 @@
use crate::query::MAX_ROWS;
use crate::sql::starts_with_executable_sql_keyword;
use crate::types::{
ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, LinkedServerInfo, QueryResult, TableInfo, TriggerInfo,
ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, LinkedServerInfo, ObjectStatistics, QueryResult, TableInfo,
TriggerInfo,
};
use futures::{FutureExt, TryStreamExt};
use rust_decimal::Decimal;
@@ -929,6 +930,44 @@ fn sqlserver_list_objects_sql(schema: &str) -> String {
)
}
pub async fn list_object_statistics(
client: &mut SqlServerClient,
schema: &str,
) -> Result<Vec<ObjectStatistics>, String> {
let s = schema.replace('\'', "''");
let sql = format!(
"SELECT o.name, \
SUM(CASE WHEN ps.index_id IN (0, 1) THEN ps.row_count ELSE 0 END) AS estimated_rows, \
SUM(ps.reserved_page_count) * 8192 AS total_bytes \
FROM sys.objects o \
JOIN sys.schemas s ON s.schema_id = o.schema_id \
JOIN sys.dm_db_partition_stats ps ON ps.object_id = o.object_id \
WHERE s.name = '{s}' AND o.type = 'U' AND o.is_ms_shipped = 0 \
GROUP BY o.object_id, o.name \
ORDER BY o.name"
);
let stream = client.query(&*sql, &[]).await.map_err(|e| e.to_string())?;
let rows = stream.into_first_result().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| ObjectStatistics {
name: row.get::<&str, _>(0).unwrap_or("").to_string(),
schema: Some(schema.to_string()),
estimated_rows: row
.try_get::<i64, _>(1)
.ok()
.flatten()
.or_else(|| row.try_get::<i32, _>(1).ok().flatten().map(i64::from)),
total_bytes: row
.try_get::<i64, _>(2)
.ok()
.flatten()
.or_else(|| row.try_get::<i32, _>(2).ok().flatten().map(i64::from)),
})
.filter(|stat| !stat.name.is_empty())
.collect())
}
pub async fn get_columns(client: &mut SqlServerClient, schema: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
let sql = sqlserver_columns_sql(schema, table);
let stream = client.query(&*sql, &[]).await.map_err(|e| e.to_string())?;
+37
View File
@@ -1004,6 +1004,18 @@ pub async fn list_objects_core(
.await
}
pub async fn list_object_statistics_core(
state: &AppState,
connection_id: &str,
database: &str,
schema: &str,
) -> Result<Vec<db::ObjectStatistics>, String> {
retry_metadata_connection(state, connection_id, Some(database), || {
list_object_statistics_once(state, connection_id, database, schema)
})
.await
}
pub async fn list_completion_objects_core(
state: &AppState,
connection_id: &str,
@@ -1016,6 +1028,31 @@ pub async fn list_completion_objects_core(
.await
}
async fn list_object_statistics_once(
state: &AppState,
connection_id: &str,
database: &str,
schema: &str,
) -> Result<Vec<db::ObjectStatistics>, String> {
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
let db_config = connection_config(state, connection_id).await;
let connections = state.connections.read().await;
try_sqlserver!(connections, &pool_key, list_object_statistics, schema);
let pool = connections.get(&pool_key).ok_or("Pool not found")?;
match pool {
PoolKind::Mysql(p, mode) => {
if *mode == MysqlMode::OceanBaseOracle || db_config.as_ref().is_some_and(is_manticoresearch_config) {
Ok(vec![])
} else {
db::mysql::list_object_statistics(p, database).await
}
}
PoolKind::Postgres(p) if db_config.as_ref().is_some_and(is_questdb_config) => Ok(vec![]),
PoolKind::Postgres(p) => db::postgres::list_object_statistics(p, schema).await,
_ => Ok(vec![]),
}
}
async fn list_objects_once(
state: &AppState,
connection_id: &str,
+8
View File
@@ -34,6 +34,14 @@ pub struct ObjectInfo {
pub parent_name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectStatistics {
pub name: String,
pub schema: Option<String>,
pub estimated_rows: Option<i64>,
pub total_bytes: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ObjectSourceKind {