25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(oceanbase): add Oracle-mode table statistics
efeirefa379f27371d
diff --git a/apps/desktop/src/components/objects/__tests__/ObjectBrowser.statisticsRefresh.component.spec.ts b/apps/desktop/src/components/objects/__tests__/ObjectBrowser.statisticsRefresh.component.spec.tsindex 5c8945a17c..c4c7c5e00f 100644--- a/apps/desktop/src/components/objects/__tests__/ObjectBrowser.statisticsRefresh.component.spec.ts+++ b/apps/desktop/src/components/objects/__tests__/ObjectBrowser.statisticsRefresh.component.spec.ts@@ -3,7 +3,7 @@import { createApp, defineComponent, h, ref, type App } from "vue";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import ObjectBrowser from "@/components/objects/ObjectBrowser.vue";-import { cacheObjectBrowserRows, createObjectBrowserRowsCacheWriteToken, invalidateObjectBrowserRowsCache } from "@/lib/table/objectBrowserRowsCache";+import { cacheObjectBrowserRows, createObjectBrowserRowsCacheWriteToken, getCachedObjectBrowserRows, invalidateObjectBrowserRowsCache } from "@/lib/table/objectBrowserRowsCache";import type { ObjectBrowserRow } from "@/lib/table/objectBrowserRows";import type { ConnectionConfig } from "@/types/database";@@ -111,10 +111,10 @@ afterEach(() => {invalidateObjectBrowserRowsCache({});});-async function mountBrowser() {+async function mountBrowser(schema = ref("public")) {const host = document.createElement("div");document.body.append(host);- const app = createApp({ setup: () => () => h(ObjectBrowser, { connection, database: "pgdemo", schema: "public" }) });+ const app = createApp({ setup: () => () => h(ObjectBrowser, { connection, database: "pgdemo", schema: schema.value }) });mountedApps.push({ app, host });app.mount(host);return host;@@ -125,6 +125,36 @@ function primeRowsCache(cachedAt: number) {}describe("ObjectBrowser table statistics on a cached scaffold (#10461)", () => {+ it("ignores late statistics from the previous schema", async () => {+ let resolveOld!: (value: unknown[]) => void;+ const oldStatistics = new Promise<unknown[]>((resolve) => {+ resolveOld = resolve;+ });+ mocks.listSchemas.mockResolvedValue(["public", "other"]);+ mocks.listObjectStatistics.mockImplementation((_id, _database, schema) => (schema === "public" ? oldStatistics : Promise.resolve([{ name: "orders", schema: "other", estimated_rows: 22, total_bytes: 2048 }])));+ primeRowsCache(Date.now() - 31_000);+ const schema = ref("public");+ const host = await mountBrowser(schema);+ await vi.waitFor(() => expect(mocks.listObjectStatistics).toHaveBeenCalledWith(connection.id, "pgdemo", "public"));+ schema.value = "other";+ await vi.waitFor(() => expect(getCachedObjectBrowserRows({ ...scope, schema: "other" })?.[0]).toMatchObject({ estimatedRows: 22, totalBytes: 2048 }));+ resolveOld([{ name: "orders", schema: "public", estimated_rows: 999999, total_bytes: 999999 }]);+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(getCachedObjectBrowserRows({ ...scope, schema: "other" })?.[0]).toMatchObject({ estimatedRows: 22, totalBytes: 2048 });+ expect(host.textContent).not.toContain("999,999");+ });++ it.each([null, 0])("replaces stale statistics with %s without confusing unknown and zero", async (value) => {+ mocks.listObjectStatistics.mockResolvedValue([{ name: "orders", schema: "public", estimated_rows: value, total_bytes: value }]);+ primeRowsCache(Date.now() - 31_000);+ const host = await mountBrowser();+ await vi.waitFor(() => {+ expect(getCachedObjectBrowserRows(scope)?.[0]).toMatchObject({ estimatedRows: value, totalBytes: value });+ });+ expect(host.textContent).not.toContain("24 KB");+ if (value === 0) expect(host.textContent).toContain("0 B");+ });+it("backfills row/size statistics when the restored scaffold is stale", async () => {primeRowsCache(Date.now() - 31_000);diff --git a/crates/dbx-core/src/schema/agent_metadata_fixture.py b/crates/dbx-core/src/schema/agent_metadata_fixture.pyindex 34945717a8..540a0062bc 100644--- a/crates/dbx-core/src/schema/agent_metadata_fixture.py+++ b/crates/dbx-core/src/schema/agent_metadata_fixture.py@@ -81,7 +81,7 @@ def handle(request):mode = 'connection' if failed.read_text() == session else 'success'if mode in ('sql', 'timeout', 'connection'):error = rpc_error(mode)- elif mode == 'fallback' and 'TABLE_USED_PAGES' in params['sql']:+ elif mode == 'fallback' and ('TABLE_USED_PAGES' in params['sql'] or 'DBA_OB_TABLET_REPLICAS' in params['sql']):error = rpc_error('sql')else:result = {'columns': ['TABLE_NAME', 'OWNER', 'NUM_ROWS', 'TOTAL_BYTES'],diff --git a/crates/dbx-core/src/schema/agent_metadata_routing_tests.rs b/crates/dbx-core/src/schema/agent_metadata_routing_tests.rsindex b03ad31e16..5aeb45eda0 100644--- a/crates/dbx-core/src/schema/agent_metadata_routing_tests.rs+++ b/crates/dbx-core/src/schema/agent_metadata_routing_tests.rs@@ -378,6 +378,30 @@ async fn statistics_fallback_reuses_ephemeral_session_and_closes_it() {fixture.shutdown().await;}+#[tokio::test]+async fn oceanbase_statistics_routes_through_agent_and_preserves_foreground() {+ for mode in ["success", "fallback", "sql"] {+ let fixture = AgentFixture::new(DatabaseType::OceanbaseOracle).await;+ let foreground = fixture.foreground().await;+ std::fs::write(fixture.control_path("statistics"), mode).unwrap();+ let result = list_object_statistics_core(&fixture.state, "conn", "configured", "APP").await;+ let queries = fixture.requests("execute_query");+ assert!(queries[0]["params"]["sql"].as_str().unwrap().contains("DBA_OB_TABLET_REPLICAS"));+ if mode == "sql" {+ assert!(result.is_err());+ assert_eq!(queries.len(), 2);+ } else {+ let rows = result.unwrap();+ assert_eq!(rows[0].estimated_rows, Some(12));+ assert_eq!(rows[0].total_bytes, if mode == "success" { Some(4096) } else { None });+ assert_eq!(queries.len(), if mode == "success" { 1 } else { 2 });+ }+ fixture.wait_for_requests("close_session", 1).await;+ fixture.assert_only_foreground_remains(&foreground).await;+ fixture.shutdown().await;+ }+}+#[tokio::test]async fn statistics_sql_error_and_timeout_clean_up_without_replaying() {for failure in ["sql", "timeout"] {diff --git a/crates/dbx-core/src/schema/mod.rs b/crates/dbx-core/src/schema/mod.rsindex 5a29795acb..ad4404e0cc 100644--- a/crates/dbx-core/src/schema/mod.rs+++ b/crates/dbx-core/src/schema/mod.rs@@ -1913,6 +1913,57 @@ fn oracle_object_statistics_query_plan(schema: &str) -> Vec<ObjectStatisticsAtte]}+fn oceanbase_oracle_object_statistics_sql(schema: &str) -> String {+ format!(+ r#"WITH locations AS (+ SELECT DISTINCT DATABASE_NAME, TABLE_NAME, TABLE_ID, TABLE_TYPE,+ DATA_TABLE_ID, TABLET_ID, LS_ID, SVR_IP, SVR_PORT, ROLE+ FROM SYS.DBA_OB_TABLE_LOCATIONS WHERE DATABASE_NAME = {owner}+), base_tables AS (+ SELECT DISTINCT DATABASE_NAME, TABLE_NAME, TABLE_ID FROM locations+ WHERE TABLE_TYPE = 'USER TABLE'+), owned_tablets AS (+ SELECT b.DATABASE_NAME, b.TABLE_NAME, l.TABLET_ID,+ MAX(CASE WHEN l.ROLE = 'LEADER' THEN l.LS_ID END) AS LS_ID,+ MAX(CASE WHEN l.ROLE = 'LEADER' THEN l.SVR_IP END) AS SVR_IP,+ MAX(CASE WHEN l.ROLE = 'LEADER' THEN l.SVR_PORT END) AS SVR_PORT,+ SUM(CASE WHEN l.ROLE = 'LEADER' THEN 1 ELSE 0 END) AS LEADER_COUNT+ FROM base_tables b JOIN locations l+ ON l.DATABASE_NAME = b.DATABASE_NAME+ AND (l.TABLE_ID = b.TABLE_ID OR (l.DATA_TABLE_ID = b.TABLE_ID+ AND l.TABLE_TYPE IN ('INDEX', 'LOB AUX TABLE')))+ GROUP BY b.DATABASE_NAME, b.TABLE_NAME, l.TABLET_ID+), tablet_sizes AS (+ SELECT o.DATABASE_NAME, o.TABLE_NAME, o.TABLET_ID,+ CASE WHEN o.LEADER_COUNT = 1 AND COUNT(r.TABLET_ID) = 1+ AND COUNT(r.REQUIRED_SIZE) = 1+ THEN MAX(r.REQUIRED_SIZE) ELSE NULL END AS TABLET_BYTES+ FROM owned_tablets o LEFT JOIN SYS.DBA_OB_TABLET_REPLICAS r+ ON r.TABLET_ID = o.TABLET_ID AND r.LS_ID = o.LS_ID+ AND r.SVR_IP = o.SVR_IP AND r.SVR_PORT = o.SVR_PORT+ GROUP BY o.DATABASE_NAME, o.TABLE_NAME, o.TABLET_ID, o.LEADER_COUNT+), sizes AS (+ SELECT DATABASE_NAME, TABLE_NAME,+ CASE WHEN COUNT(*) = COUNT(TABLET_BYTES)+ THEN SUM(TABLET_BYTES) ELSE NULL END AS TOTAL_BYTES+ FROM tablet_sizes GROUP BY DATABASE_NAME, TABLE_NAME+)+SELECT t.TABLE_NAME, t.OWNER, t.NUM_ROWS, s.TOTAL_BYTES+FROM ALL_TABLES t+LEFT JOIN sizes s ON s.DATABASE_NAME = t.OWNER AND s.TABLE_NAME = t.TABLE_NAME+WHERE t.OWNER = {owner} AND t.NESTED = 'NO'+ORDER BY t.TABLE_NAME"#,+ owner = oracle_owner_filter(schema),+ )+}++fn oceanbase_oracle_object_statistics_query_plan(schema: &str) -> Vec<ObjectStatisticsAttempt> {+ vec![+ ("tablet-replicas", oceanbase_oracle_object_statistics_sql(schema), true),+ ("rows-only", oracle_object_statistics_rows_only_sql(schema), true),+ ]+}+fn dameng_object_statistics_query_plan(schema: &str) -> Vec<ObjectStatisticsAttempt> {vec![("table-used-pages", dameng_object_statistics_sql(schema), true),@@ -2216,11 +2267,18 @@ async fn agent_list_object_statistics(#[derive(Clone, Copy, PartialEq, Eq, Debug)]enum ExternalDriverStatisticsDialect {Oracle,+ OceanbaseOracle,Dameng,Kingbase,}fn external_driver_statistics_dialect(config: &ConnectionConfig) -> Option<ExternalDriverStatisticsDialect> {+ if config.db_type == DatabaseType::OceanbaseOracle+ || crate::models::connection::jdbc_dialect_hint(config)+ == Some(crate::models::connection::JdbcDialectHint::OceanbaseOracle)+ {+ return Some(ExternalDriverStatisticsDialect::OceanbaseOracle);+ }// `is_oracle_external_driver_config` (added alongside the JDBC Oracle DDL// fix) checks both the `jdbc:oracle:` URL prefix and the driver class, so// it recognises more real-world configs than a URL-only sniff would.@@ -2250,6 +2308,7 @@ fn external_driver_statistics_query_plan() -> Vec<ObjectStatisticsAttempt> {match dialect {ExternalDriverStatisticsDialect::Oracle => oracle_object_statistics_query_plan(schema),+ ExternalDriverStatisticsDialect::OceanbaseOracle => oceanbase_oracle_object_statistics_query_plan(schema),ExternalDriverStatisticsDialect::Dameng => dameng_object_statistics_query_plan(schema),ExternalDriverStatisticsDialect::Kingbase => kingbase::object_statistics_query_plan(schema),}@@ -2258,6 +2317,7 @@ fn external_driver_statistics_query_plan(fn external_driver_statistics_vendor(dialect: ExternalDriverStatisticsDialect) -> &'static str {match dialect {ExternalDriverStatisticsDialect::Oracle => "Oracle",+ ExternalDriverStatisticsDialect::OceanbaseOracle => "OceanBase Oracle",ExternalDriverStatisticsDialect::Dameng => "Dameng",ExternalDriverStatisticsDialect::Kingbase => "Kingbase",}@@ -4300,6 +4360,12 @@ done#[test]fn external_driver_statistics_dialect_matches_jdbc_url_vendor() {let mut config = test_connection_config(DatabaseType::Jdbc);+ config.connection_string = Some(" JDBC:OceanBase:Oracle://localhost:2881/test ".to_string());+ assert_eq!(external_driver_statistics_dialect(&config), Some(ExternalDriverStatisticsDialect::OceanbaseOracle));+ config.connection_string = Some("jdbc:oceanbase:mysql://localhost:2881/test".to_string());+ assert_eq!(external_driver_statistics_dialect(&config), None);+ config.connection_string = Some("jdbc:oceanbase://localhost:2881/test".to_string());+ assert_eq!(external_driver_statistics_dialect(&config), None);config.connection_string = Some(" JDBC:Oracle:thin:@127.0.0.1:1521:XE ".to_string());assert_eq!(external_driver_statistics_dialect(&config), Some(ExternalDriverStatisticsDialect::Oracle));@@ -6510,6 +6576,65 @@ doneassert!(!rows_only_sql.contains("ALL_SEGMENTS"));}+ #[test]+ fn oceanbase_statistics_sql_requires_complete_unique_tablet_sizes() {+ for (case, mutation, expected) in [+ ("normal", "", Some(10_i64)),+ ("zero", "UPDATE SYS.DBA_OB_TABLET_REPLICAS SET REQUIRED_SIZE=0", Some(0)),+ ("missing-leader", "INSERT INTO SYS.DBA_OB_TABLE_LOCATIONS VALUES ('APP','T',1,'USER TABLE',0,2,1,'host',2882,'FOLLOWER')", None),+ ("multiple-leaders", "INSERT INTO SYS.DBA_OB_TABLE_LOCATIONS VALUES ('APP','T',1,'USER TABLE',0,1,2,'host2',2882,'LEADER')", None),+ ("duplicate-location", "INSERT INTO SYS.DBA_OB_TABLE_LOCATIONS SELECT * FROM SYS.DBA_OB_TABLE_LOCATIONS", Some(10)),+ ("missing-replica", "DELETE FROM SYS.DBA_OB_TABLET_REPLICAS", None),+ ("duplicate-replica", "INSERT INTO SYS.DBA_OB_TABLET_REPLICAS SELECT * FROM SYS.DBA_OB_TABLET_REPLICAS", None),+ ("null-size", "UPDATE SYS.DBA_OB_TABLET_REPLICAS SET REQUIRED_SIZE=NULL", None),+ ("index-and-lob", "INSERT INTO SYS.DBA_OB_TABLE_LOCATIONS VALUES ('APP','I',2,'INDEX',1,2,1,'host',2882,'LEADER'),('APP','L',3,'LOB AUX TABLE',1,3,1,'host',2882,'LEADER'); INSERT INTO SYS.DBA_OB_TABLET_REPLICAS VALUES (2,1,'host',2882,20),(3,1,'host',2882,30)", Some(60)),+ ] {+ let conn = rusqlite::Connection::open_in_memory().unwrap();+ conn.execute_batch(+ "ATTACH DATABASE ':memory:' AS SYS;+ CREATE TABLE ALL_TABLES(TABLE_NAME TEXT,OWNER TEXT,NUM_ROWS INTEGER,NESTED TEXT);+ INSERT INTO ALL_TABLES VALUES ('T','APP',31,'NO');+ CREATE TABLE SYS.DBA_OB_TABLE_LOCATIONS(DATABASE_NAME TEXT,TABLE_NAME TEXT,TABLE_ID INTEGER,TABLE_TYPE TEXT,DATA_TABLE_ID INTEGER,TABLET_ID INTEGER,LS_ID INTEGER,SVR_IP TEXT,SVR_PORT INTEGER,ROLE TEXT);+ INSERT INTO SYS.DBA_OB_TABLE_LOCATIONS VALUES ('APP','T',1,'USER TABLE',0,1,1,'host',2882,'LEADER');+ CREATE TABLE SYS.DBA_OB_TABLET_REPLICAS(TABLET_ID INTEGER,LS_ID INTEGER,SVR_IP TEXT,SVR_PORT INTEGER,REQUIRED_SIZE INTEGER);+ INSERT INTO SYS.DBA_OB_TABLET_REPLICAS VALUES (1,1,'host',2882,10);",+ ).unwrap();+ conn.execute_batch(mutation).unwrap();+ let actual: (i64, Option<i64>) = conn.query_row(+ &super::oceanbase_oracle_object_statistics_sql("APP"),+ [],+ |row| Ok((row.get(2)?, row.get(3)?)),+ ).unwrap();+ assert_eq!(actual, (31, expected), "{case}");+ }+ }++ #[tokio::test]+ async fn oceanbase_statistics_preserves_rows_when_capacity_is_unavailable() {+ let stats = super::object_statistics_from_query_plan(+ "OceanBase Oracle",+ "APP",+ super::oceanbase_oracle_object_statistics_query_plan("APP"),+ |sql| async move {+ if sql.contains("DBA_OB_TABLET_REPLICAS") {+ Err("insufficient privilege".to_string())+ } else {+ Ok(statistics_query_result(vec![vec![+ serde_json::json!("T"),+ serde_json::json!("APP"),+ serde_json::json!(31),+ serde_json::Value::Null,+ ]]))+ }+ },+ )+ .await+ .unwrap();+ assert_eq!(stats.len(), 1);+ assert_eq!(stats[0].estimated_rows, Some(31));+ assert_eq!(stats[0].total_bytes, None);+ }+#[test]fn dameng_object_statistics_sql_uses_bounded_table_size_function() {let sql = dameng_object_statistics_sql("app's");@@ -7424,6 +7549,22 @@ async fn list_object_statistics_once(if db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::MongoDb) {return crate::mongo_ops::mongo_agent_list_object_statistics(&client, database).await;}+ if db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::OceanbaseOracle) {+ let timeout_duration = agent_metadata_timeout(db_config.as_ref());+ return object_statistics_from_query_plan(+ "OceanBase Oracle",+ schema,+ oceanbase_oracle_object_statistics_query_plan(schema),+ |sql| {+ let client = client.clone();+ async move {+ let mut client = client.lock().await;+ agent_object_statistics_query(&mut client, database, schema, &sql, timeout_duration).await+ }+ },+ )+ .await;+ }if db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::Oracle) {return oracle_agent_list_object_statistics(client,@@ -7476,7 +7617,15 @@ async fn list_object_statistics_once(let pool = clone_metadata_pool(state, &pool_key).await.ok_or("Pool not found")?;match &pool {PoolKind::Mysql(p, mode) => {- if *mode == MysqlMode::OceanBaseOracle || db_config.as_ref().is_some_and(db::manticoresearch::is_config) {+ if *mode == MysqlMode::OceanBaseOracle {+ object_statistics_from_query_plan(+ "OceanBase Oracle",+ schema,+ oceanbase_oracle_object_statistics_query_plan(schema),+ |sql| async move { db::mysql::execute_query(p, &sql, true).await },+ )+ .await+ } else if db_config.as_ref().is_some_and(db::manticoresearch::is_config) {Ok(vec![])} else {let include_mysql_details = db_config.as_ref().is_some_and(|config| {