Skip to content

armgit / dbx

Public Imported from GitHub
0

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。

fix(mysql): drop the paged extra-row probe

Acd-kk2c8c27089251
diff --git a/apps/desktop/src/lib/query/__tests__/batchQueryPagination.spec.ts b/apps/desktop/src/lib/query/__tests__/batchQueryPagination.spec.ts
index 0a25bea294..49cc3b615e 100644
--- a/apps/desktop/src/lib/query/__tests__/batchQueryPagination.spec.ts
+++ b/apps/desktop/src/lib/query/__tests__/batchQueryPagination.spec.ts
@@ -75,6 +75,13 @@ describe("prepareBatchQueryPagination", () => {
expect(result!.sql).toContain("LIMIT 21 OFFSET 0");
});
+ it("keeps mysql batches at the requested page size when no probe is offered (#13211)", async () => {
+ const prepare = vi.fn().mockResolvedValue(plan({ pageProbeSql: undefined }));
+ const result = await prepareBatchQueryPagination(options({ databaseType: "mysql" }), prepare);
+ expect(result?.statements).toHaveLength(2);
+ expect(result?.sql).toBe("SELECT id FROM t LIMIT 20 OFFSET 0; SELECT id FROM t LIMIT 20 OFFSET 0");
+ });
+
it("skips cursor sessions, sqlserver batches, and single statements", async () => {
const prepare = vi.fn();
expect(await prepareBatchQueryPagination(options({ useAgentCursor: true }), prepare)).toBeUndefined();
diff --git a/apps/desktop/src/stores/__tests__/queryStore.tableDataRefresh.spec.ts b/apps/desktop/src/stores/__tests__/queryStore.tableDataRefresh.spec.ts
index 4bf4f2fc33..dca636d6c9 100644
--- a/apps/desktop/src/stores/__tests__/queryStore.tableDataRefresh.spec.ts
+++ b/apps/desktop/src/stores/__tests__/queryStore.tableDataRefresh.spec.ts
@@ -4,6 +4,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { clearDataGridStructuredFilterStates, loadDataGridStructuredFilterState, saveDataGridStructuredFilterState } from "@/lib/dataGrid/dataGridFilterBuilderPersistence";
import { clearDataGridSortBuilderMemoryCache, useDataGridSortBuilder } from "@/composables/useDataGridSortBuilder";
import { resetRowIdLocatorStateForTests, rowIdLocatorUnsupported } from "@/lib/table/tableRowIdSupport";
+import { canGoNextDataGridPage } from "@/lib/dataGrid/dataGridPagination";
const mocks = vi.hoisted(() => ({
buildTableSelectSql: vi.fn(),
@@ -18,6 +19,7 @@ const mocks = vi.hoisted(() => ({
}));
vi.mock("@/lib/backend/api", () => ({
+ analyzeEditableQueryEditability: vi.fn().mockResolvedValue({ editable: false, reason: "complex-query" }),
buildTableSelectSql: mocks.buildTableSelectSql,
closeClientConnectionSession: mocks.closeClientConnectionSession,
closeQuerySession: mocks.closeQuerySession,
@@ -130,6 +132,49 @@ describe("queryStore table data refresh", () => {
expect(tab.resultTotalRowCount).toBe(hasMore ? undefined : offset + 55);
});
+ it.each(["query", "data"] as const)("keeps MySQL %s pages bounded when no lookahead is offered (#13211)", async (mode) => {
+ mocks.getConnectionConfig.mockReturnValue({ id: "mysql-1", db_type: "mysql", database: "app" });
+ const { useQueryStore } = await import("@/stores/queryStore");
+ const store = useQueryStore();
+ const id = store.createTab("mysql-1", "app", "dates", mode);
+ const baseSql = "SELECT * FROM table1 a, table2 b WHERE a.id = b.id AND REPLACE(SUBSTR(b.ymd, 1, 7), '-', '') < a.ym";
+ let returnedRows = 1000;
+ let offset = 0;
+ mocks.prepareQueryPaginationExecutionPlan.mockImplementation(async () => ({
+ sqlToExecute: `${baseSql} LIMIT 1000${offset ? ` OFFSET ${offset}` : ""};`,
+ pageSql: `${baseSql} LIMIT 1000${offset ? ` OFFSET ${offset}` : ""};`,
+ pageLimit: 1000,
+ pageOffset: offset,
+ useAgentResultSession: false,
+ }));
+ mocks.executeMulti.mockImplementation(async () => [
+ {
+ columns: ["id"],
+ rows: Array.from({ length: returnedRows }, (_, index) => [offset + index + 1]),
+ affected_rows: 0,
+ execution_time_ms: 1,
+ has_more: false,
+ },
+ ]);
+
+ await store.executeTabSql(id, mode === "data" ? `${baseSql} LIMIT 1000;` : baseSql, { pagination: { limit: 1000, offset } });
+
+ expect(mocks.executeMulti).toHaveBeenLastCalledWith("mysql-1", "app", `${baseSql} LIMIT 1000;`, undefined, expect.any(String), expect.objectContaining({ maxRows: 1000 }));
+ const tab = store.tabs.find((candidate) => candidate.id === id)!;
+ expect(tab.result?.rows).toHaveLength(1000);
+ expect(tab.result?.page_has_more).toBeUndefined();
+ expect(tab.resultTotalRowCount).toBeUndefined();
+ expect(canGoNextDataGridPage({ rowCount: tab.result!.rows.length, pageSize: 1000, pageHasMore: tab.result?.page_has_more, totalRowCount: tab.resultTotalRowCount })).toBe(true);
+
+ offset = 1000;
+ returnedRows = 0;
+ await store.executeTabSql(id, mode === "data" ? `${baseSql} LIMIT 1000 OFFSET 1000;` : baseSql, { pagination: { limit: 1000, offset } });
+
+ expect(mocks.executeMulti.mock.calls[1]?.[2]).toBe(`${baseSql} LIMIT 1000 OFFSET 1000;`);
+ expect(tab.result?.rows).toHaveLength(0);
+ expect(canGoNextDataGridPage({ rowCount: tab.result!.rows.length, pageSize: 1000, pageOffset: offset, pageHasMore: tab.result?.page_has_more, totalRowCount: tab.resultTotalRowCount })).toBe(false);
+ });
+
it("loads a duplicated data tab with its filter, database sort and page without sharing results", async () => {
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
diff --git a/crates/dbx-sql-data/src/query_result_sql.rs b/crates/dbx-sql-data/src/query_result_sql.rs
index d30bd9a1a5..a67ce99b5c 100644
--- a/crates/dbx-sql-data/src/query_result_sql.rs
+++ b/crates/dbx-sql-data/src/query_result_sql.rs
@@ -1,7 +1,6 @@
use std::collections::HashSet;
use std::ops::ControlFlow;
-use dbx_sql_core::sql_risk::{prove_read_only_for_database, ReadProof};
use serde::{Deserialize, Serialize};
use crate::models::connection::DatabaseType;
@@ -60,8 +59,8 @@ pub struct QueryPaginationExecutionPlanOptions {
#[serde(rename_all = "camelCase")]
pub struct QueryPaginationExecutionPlan {
pub sql_to_execute: String,
- /// Optional native MySQL page with one extra row. Only offered for queries
- /// accepted by the strict read-only proof; callers must trim the probe row.
+ /// Legacy optional MySQL lookahead, retained for wire compatibility.
+ /// New plans leave this unset to avoid scanning beyond the requested page.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub page_probe_sql: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
@@ -283,34 +282,10 @@ pub fn build_query_pagination_execution_plan(
plan.page_sql = page_sql;
plan.page_limit = Some(options.pagination.limit);
plan.page_offset = Some(options.pagination.offset);
- // A SELECT can mutate session state or invoke a user function. Do not
- // evaluate an extra row unless both the original and rewritten query
- // satisfy the same strict proof used for manual-transaction reads.
- if options.database_type == Some(DatabaseType::Mysql)
- && !options.use_agent_cursor
- && options.pagination.limit > 0
- && prove_read_only_for_database(&options.sql, DatabaseType::Mysql) == ReadProof::ProvenReadOnly
- && (options.sql == options.query_base_sql
- || prove_read_only_for_database(&options.query_base_sql, DatabaseType::Mysql)
- == ReadProof::ProvenReadOnly)
- {
- if let Some(probe_limit) =
- options.pagination.limit.checked_add(1).filter(|limit| *limit <= i32::MAX as usize)
- {
- let probe = build_paginated_query_sql(PaginatedQuerySqlOptions {
- original_sql: options.sql.clone(),
- database_type: options.database_type,
- limit: probe_limit,
- offset: options.pagination.offset,
- });
- if probe.ok {
- plan.page_probe_sql = probe.sql.map(|sql| match execution_hint {
- Some(hint) => inject_execution_hint(&sql, hint),
- None => sql,
- });
- }
- }
- }
+ // Do not request an extra row to prove that another page exists.
+ // Finding one more match in a filtered query can scan the entire
+ // remaining input even when the requested page is ready (#13211).
+ // A full page with unknown total keeps the frontend's next-page option.
if options.pagination.offset > 0
&& pagination_strategy(options.database_type, PaginationContext::UserQuery)
== TablePaginationStrategy::Rownum
@@ -5311,6 +5286,57 @@ WHERE u.id = picked.id;
assert!(!plan.use_agent_result_session);
}
+ #[test]
+ fn mysql_pagination_does_not_look_past_the_requested_page() {
+ for sql in [
+ "SELECT * FROM table1 a, table2 b WHERE a.id = b.id AND REPLACE(SUBSTR(b.ymd, 1, 7), '-', '') < a.ym",
+ "SELECT id FROM people WHERE active = 1",
+ "SELECT id FROM people",
+ ] {
+ for offset in [0, 1000] {
+ let plan = build_query_pagination_execution_plan(QueryPaginationExecutionPlanOptions {
+ sql: sql.to_string(),
+ query_base_sql: sql.to_string(),
+ database_type: Some(DatabaseType::Mysql),
+ pagination: QueryPagination { limit: 1000, offset, session_id: None },
+ use_agent_cursor: false,
+ first_page_uses_actual_sql: false,
+ execution_hint: None,
+ });
+ let expected_sql = if offset == 0 {
+ format!("{sql} LIMIT 1000;")
+ } else {
+ format!("{sql} LIMIT 1000 OFFSET {offset};")
+ };
+
+ assert_eq!(plan.sql_to_execute, expected_sql);
+ assert_eq!(plan.page_sql.as_deref(), Some(expected_sql.as_str()));
+ assert_eq!(plan.page_limit, Some(1000));
+ assert_eq!(plan.page_offset, Some(offset));
+ assert!(plan.page_probe_sql.is_none(), "must not wait for a 1001st match: {sql}");
+ assert!(serde_json::to_value(&plan).unwrap().get("pageProbeSql").is_none());
+ }
+ }
+ }
+
+ #[test]
+ fn mysql_pagination_keeps_the_user_limit_without_a_probe() {
+ let sql = "SELECT id FROM people LIMIT 10";
+ let plan = build_query_pagination_execution_plan(QueryPaginationExecutionPlanOptions {
+ sql: sql.to_string(),
+ query_base_sql: sql.to_string(),
+ database_type: Some(DatabaseType::Mysql),
+ pagination: QueryPagination { limit: 1000, offset: 0, session_id: None },
+ use_agent_cursor: false,
+ first_page_uses_actual_sql: false,
+ execution_hint: None,
+ });
+
+ assert_eq!(plan.sql_to_execute, "SELECT id FROM people LIMIT 10;");
+ assert_eq!(plan.exact_query_row_bound, Some(10));
+ assert!(plan.page_probe_sql.is_none());
+ }
+
#[test]
fn mysql_count_preserves_leading_ampersand_routing_hint() {
let result = build_count_query_sql(CountQuerySqlOptions {