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(export): stream Oracle result exports through manual transactions

zipg79ba3d1a67d1
diff --git a/crates/dbx-core/src/data/query_result_export.rs b/crates/dbx-core/src/data/query_result_export.rs
index f7a6c571b2..76292b682e 100644
--- a/crates/dbx-core/src/data/query_result_export.rs
+++ b/crates/dbx-core/src/data/query_result_export.rs
@@ -23,13 +23,13 @@ use crate::database_export::{
};
use crate::models::connection::DatabaseType;
use crate::query::{
- await_stream_with_progress_timeout, canceled_error, close_query_session,
+ await_stream_with_progress_timeout, canceled_error, close_manual_transaction_query_session, close_query_session,
execute_in_manual_transaction_with_options, execute_sql_statement_with_options, operation_budget_for_pool_key,
ManualTransactionExecutionOptions, QueryExecutionOptions, StreamProgressClock, QUERY_CANCELED,
};
use crate::query_result_sql::{
- build_query_pagination_execution_plan, has_top_level_top, top_level_top_row_count, QueryPagination,
- QueryPaginationExecutionPlanOptions,
+ build_query_pagination_execution_plan, has_top_level_top, top_level_rownum_row_count, top_level_top_row_count,
+ QueryPagination, QueryPaginationExecutionPlanOptions,
};
use crate::table_export::TableExportProgress;
use crate::transfer::keyset_pagination_sql_with_identifier_quote;
@@ -801,6 +801,39 @@ fn supports_streaming_offset_pagination(request: &QueryResultExportRequest, page
&& !first_sql.trim().eq_ignore_ascii_case(second_sql.trim())
}
+/// Whether the export may open/continue an Agent result-session cursor.
+///
+/// The request decides whether the driver has an Agent cursor at all; inside a
+/// manual transaction the cursor additionally requires the transaction's own
+/// connection to be an Agent connection (native/external transaction
+/// connections only page with SQL). Kept separate from the lookup so the export
+/// policy stays unit-testable without an AppState.
+fn export_can_use_agent_cursor(request: &QueryResultExportRequest, txn_supports_agent_cursor: Option<bool>) -> bool {
+ request.use_agent_cursor && txn_supports_agent_cursor.unwrap_or(true)
+}
+
+/// Execution options for one export page read through the open manual
+/// transaction. Cursor pages must forward the page size and the result-session
+/// id, otherwise the transaction would return only the first page and later
+/// pages could never be reached; non-cursor pages stay bounded by their own
+/// page limit.
+fn manual_transaction_page_options(
+ request: &QueryResultExportRequest,
+ plan_limit: usize,
+ use_agent_result_session: bool,
+ agent_max_rows: usize,
+ session_id: Option<&str>,
+) -> ManualTransactionExecutionOptions {
+ ManualTransactionExecutionOptions {
+ max_rows: Some(if use_agent_result_session { agent_max_rows } else { plan_limit }),
+ page_size: use_agent_result_session.then_some(plan_limit),
+ result_session_id: if use_agent_result_session { session_id.map(str::to_owned) } else { None },
+ execution_id: request.execution_id.clone(),
+ timeout_secs: request.timeout_secs,
+ ..Default::default()
+ }
+}
+
fn streaming_pagination_preflight_error(
request: &QueryResultExportRequest,
page_size: usize,
@@ -819,21 +852,28 @@ fn streaming_pagination_preflight_error(
/// Enforceable in-memory row bound for a single-execution export, or `None`
/// when the query cannot be safely streamed in one shot without an Agent
-/// cursor. Kingbase SQL Server compatibility mode TOP queries cannot be
-/// rewritten with LIMIT/OFFSET; a concrete `TOP n` bounds the result and the
-/// user's export row limit caps it further. Percentage TOP / `WITH TIES` have
-/// no concrete row bound, so they are only single-execution-capable when a row
-/// limit is configured.
+/// cursor.
+///
+/// Two statement shapes bound their own rows: Kingbase SQL Server compatibility
+/// mode `TOP n` (which cannot be rewritten with LIMIT/OFFSET) and Oracle-family
+/// queries that already filter with `ROWNUM < n` / `ROWNUM <= n` (which the
+/// ROWNUM wrapper must not page again). Both can be exported with one bounded
+/// execution. Percentage TOP / `WITH TIES` have no concrete row bound, so they
+/// are only single-execution-capable when the export row limit caps them.
fn single_execution_row_bound(request: &QueryResultExportRequest) -> Option<usize> {
- if !has_top_level_top(&request.sql) {
- return None;
- }
- match (top_level_top_row_count(&request.sql), request.row_limit) {
- (Some(top), Some(row_limit)) => Some(top.min(row_limit)),
- (Some(top), None) => Some(top),
- (None, Some(row_limit)) => Some(row_limit),
- (None, None) => None,
- }
+ let explicit_bound = if has_top_level_top(&request.sql) {
+ match top_level_top_row_count(&request.sql) {
+ Some(top) => Some(top),
+ // TOP without a concrete literal is unbounded by itself; the export
+ // row limit is the only enforceable cap for such statements.
+ None => request.row_limit,
+ }
+ } else if matches!(request.database_type, DatabaseType::Oracle | DatabaseType::OceanbaseOracle) {
+ top_level_rownum_row_count(&request.sql)
+ } else {
+ None
+ };
+ explicit_bound.map(|bound| request.row_limit.map_or(bound, |row_limit| bound.min(row_limit)))
}
/// Maps the frozen export header onto the column list a page reported.
@@ -1053,17 +1093,29 @@ async fn export_query_result_with_cleanup(
Ok(())
};
if let Some(session_id) = session_id {
- let cursor_close = close_query_session(
- state,
- &request.connection_id,
- &request.database,
- &session_id,
- request.client_session_id.as_deref(),
- None,
- )
- .await;
+ let txn_session_id = request.txn_session_id.as_deref().map(str::trim).filter(|id| !id.is_empty());
+ // A cursor opened through the manual transaction belongs to that
+ // transaction's connection. Closing it through the normal session pool
+ // cannot find it (and could disturb the transaction's own session).
+ let cursor_close = match txn_session_id {
+ Some(txn_session_id) => {
+ close_manual_transaction_query_session(state, &request.connection_id, txn_session_id, &session_id)
+ .await
+ .map(|_| ())
+ }
+ None => close_query_session(
+ state,
+ &request.connection_id,
+ &request.database,
+ &session_id,
+ request.client_session_id.as_deref(),
+ None,
+ )
+ .await
+ .map(|_| ()),
+ };
if report_completion {
- cleanup = cleanup.and(cursor_close.map(|_| ()));
+ cleanup = cleanup.and(cursor_close);
}
}
if let Some(client_session_id) = request.client_session_id.as_deref() {
@@ -1106,9 +1158,21 @@ async fn export_query_result_core_inner(
// The streaming fast paths below check out their own pooled connection, which
// cannot see an open manual transaction. Inside a transaction the export pages
// through the transaction's connection instead, the same way the result grid
- // pages, and result-session cursors are not opened there.
+ // pages. An Agent transaction connection hosts the Agent result session as
+ // well, so keep the cursor there: Oracle 11g cannot offset-paginate a query
+ // whose ROWNUM wrapper is unsafe (an existing ROWNUM bound, wildcards,
+ // duplicate projections, ...), and disabling the cursor for transactional tabs
+ // made those exports fail with "streaming export is unsupported" even though
+ // the grid could page them. Native/external transaction connections have no
+ // cursor, so they keep SQL-only pagination.
let txn_session_id = request.txn_session_id.as_deref().map(str::trim).filter(|id| !id.is_empty());
- let use_agent_cursor = request.use_agent_cursor && txn_session_id.is_none();
+ let txn_supports_agent_cursor = match txn_session_id {
+ Some(txn_session_id) => {
+ Some(crate::query::manual_transaction_supports_agent_cursor(state, txn_session_id).await)
+ }
+ None => None,
+ };
+ let use_agent_cursor = export_can_use_agent_cursor(request, txn_supports_agent_cursor);
if txn_session_id.is_none() {
if try_export_postgres_query_result_stream(
@@ -1250,6 +1314,11 @@ async fn export_query_result_core_inner(
(String::new(), this_page, false, false)
} else if let Some(plan) = keyset_plan.as_ref() {
(build_keyset_export_sql(plan, request, this_page), this_page, false, false)
+ } else if let Some(bound) = single_execution_bound {
+ // The statement already bounds its own rows (Kingbase TOP or an
+ // Oracle ROWNUM predicate), so one execution covers the export: no
+ // LIMIT/OFFSET rewrite and no cursor session are needed.
+ (request.sql.clone(), bound, false, true)
} else {
let plan = build_query_pagination_execution_plan(QueryPaginationExecutionPlanOptions {
sql: request.sql.clone(),
@@ -1302,12 +1371,13 @@ async fn export_query_result_core_inner(
&sql_to_execute,
&request.database,
request.schema.as_deref(),
- ManualTransactionExecutionOptions {
- max_rows: Some(plan_limit),
- execution_id: request.execution_id.clone(),
- timeout_secs: request.timeout_secs,
- ..Default::default()
- },
+ manual_transaction_page_options(
+ request,
+ plan_limit,
+ use_agent_result_session,
+ agent_max_rows,
+ session_id.as_deref(),
+ ),
)
.await
.and_then(|results| {
@@ -3125,6 +3195,121 @@ mod tests {
}
}
+ #[test]
+ fn oracle_manual_transaction_export_keeps_the_agent_cursor() {
+ // Regression: exports inside a manual transaction page through the
+ // transaction's connection. An Agent transaction connection also hosts
+ // the result session, so disabling the cursor made Oracle 11g exports fail
+ // with the streaming-unsupported error whenever the ROWNUM wrapper cannot
+ // paginate the statement (here an existing `ROWNUM <` bound, as in "export
+ // current result set" for a grid whose query already bounds its rows).
+ let mut req = request("xlsx", None, None);
+ req.database_type = DatabaseType::Oracle;
+ req.sql = "SELECT FJFY, JCBW FROM T_YL_JCZD01 WHERE ROWNUM < 2000;".to_string();
+ req.query_base_sql = req.sql.clone();
+ req.use_agent_cursor = true;
+ req.txn_session_id = Some("oracle-txn-1".to_string());
+
+ let cursor = export_can_use_agent_cursor(&req, Some(true));
+ assert!(cursor);
+ assert!(!supports_streaming_offset_pagination(&req, 1000));
+ assert_eq!(streaming_pagination_preflight_error(&req, 1000, false, false, cursor), None);
+
+ // The pre-fix policy disabled the cursor for every transactional export,
+ // which is exactly how the user's query produced the reported error.
+ let pre_fix_use_agent_cursor = req.use_agent_cursor && req.txn_session_id.is_none();
+ assert_eq!(
+ streaming_pagination_preflight_error(&req, 1000, false, false, pre_fix_use_agent_cursor),
+ Some(STREAMING_PAGINATION_UNSUPPORTED_ERROR)
+ );
+
+ // A native/external transaction connection has no result cursor: keep the
+ // honest error instead of silently exporting only the first page.
+ let cursor = export_can_use_agent_cursor(&req, Some(false));
+ assert!(!cursor);
+ assert_eq!(
+ streaming_pagination_preflight_error(&req, 1000, false, false, cursor),
+ Some(STREAMING_PAGINATION_UNSUPPORTED_ERROR)
+ );
+
+ // Drivers without an Agent cursor still get the honest error.
+ req.use_agent_cursor = false;
+ let cursor = export_can_use_agent_cursor(&req, None);
+ assert!(!cursor);
+ assert_eq!(
+ streaming_pagination_preflight_error(&req, 1000, false, false, cursor),
+ Some(STREAMING_PAGINATION_UNSUPPORTED_ERROR)
+ );
+ }
+
+ #[test]
+ fn manual_transaction_export_pages_forward_the_cursor_session() {
+ let mut req = request("xlsx", None, None);
+ req.use_agent_cursor = true;
+ req.txn_session_id = Some("oracle-txn-1".to_string());
+
+ let first_cursor_page = manual_transaction_page_options(&req, 500, true, 10_000, None);
+ assert_eq!(first_cursor_page.page_size, Some(500));
+ assert_eq!(first_cursor_page.max_rows, Some(10_000));
+ assert!(first_cursor_page.result_session_id.is_none());
+
+ let cursor_page = manual_transaction_page_options(&req, 500, true, 10_000, Some("cursor-1"));
+ assert_eq!(cursor_page.page_size, Some(500));
+ assert_eq!(cursor_page.result_session_id.as_deref(), Some("cursor-1"));
+ assert_eq!(cursor_page.max_rows, Some(10_000));
+
+ // Server-side paginated pages keep their own bounded row count and must
+ // not open a cursor.
+ let bounded_page = manual_transaction_page_options(&req, 500, false, 10_000, None);
+ assert_eq!(bounded_page.max_rows, Some(500));
+ assert!(bounded_page.page_size.is_none());
+ assert!(bounded_page.result_session_id.is_none());
+ }
+
+ #[test]
+ fn oracle_rownum_bounded_export_streams_a_single_bounded_execution() {
+ // Regression for "export current result set" on Oracle 11g: a query that
+ // already bounds itself with ROWNUM cannot be ROWNUM-wrapped for offset
+ // pagination, and a manual transaction has no cursor page to fall back
+ // on. One bounded execution is enough, exactly like Kingbase TOP.
+ let mut req = request("xlsx", None, None);
+ req.database_type = DatabaseType::Oracle;
+ req.sql = "SELECT FJFY, JCBW FROM T_YL_JCZD01 WHERE ROWNUM < 2000;".to_string();
+ req.query_base_sql = req.sql.clone();
+ req.use_agent_cursor = false;
+ req.txn_session_id = Some("oracle-txn-1".to_string());
+
+ assert_eq!(single_execution_row_bound(&req), Some(1999));
+ assert_eq!(single_execution_page_limit(&req, 2000), Some(1999));
+ assert!(supports_single_execution_export(&req, 2000));
+ assert_eq!(streaming_pagination_preflight_error(&req, 2000, false, true, false), None);
+
+ // A page size smaller than the bound keeps the normal pagination path
+ // (and the honest error when neither cursor nor rewrite is available).
+ assert_eq!(single_execution_page_limit(&req, 1000), None);
+ assert_eq!(
+ streaming_pagination_preflight_error(&req, 1000, false, false, false),
+ Some(STREAMING_PAGINATION_UNSUPPORTED_ERROR)
+ );
+
+ // The export row limit caps the single execution.
+ req.row_limit = Some(500);
+ assert_eq!(single_execution_row_bound(&req), Some(500));
+ }
+
+ #[test]
+ fn rownum_bounds_only_apply_to_oracle_family_requests() {
+ let mut req = request("csv", None, None);
+ req.sql = "SELECT id FROM users WHERE ROWNUM < 10".to_string();
+ req.query_base_sql = req.sql.clone();
+ assert_eq!(single_execution_row_bound(&req), None);
+
+ for database_type in [DatabaseType::Oracle, DatabaseType::OceanbaseOracle] {
+ req.database_type = database_type;
+ assert_eq!(single_execution_row_bound(&req), Some(9));
+ }
+ }
+
#[test]
fn csv_unlimited_export_has_no_effective_row_limit() {
assert_eq!(effective_row_limit(&request("csv", None, None)), None);
diff --git a/crates/dbx-core/src/query/mod.rs b/crates/dbx-core/src/query/mod.rs
index 9ac19ce2ca..0b4978906d 100644
--- a/crates/dbx-core/src/query/mod.rs
+++ b/crates/dbx-core/src/query/mod.rs
@@ -3721,6 +3721,21 @@ pub async fn close_manual_transaction_query_session(
}
}
+/// Whether an open manual transaction can page a query through an Agent
+/// result-session cursor. Only Agent transaction connections host cursors;
+/// native (PostgreSQL/MySQL/SQL Server) and external-driver transactions page
+/// with SQL only.
+pub async fn manual_transaction_supports_agent_cursor(state: &AppState, txn_session_id: &str) -> bool {
+ let sessions = state.transaction_sessions.read().await;
+ let Some(session) = sessions.get(txn_session_id) else {
+ return false;
+ };
+ let Ok(connection) = session.connection.try_lock() else {
+ return false;
+ };
+ matches!(&*connection, TxnConnection::Agent { .. })
+}
+
pub async fn execute_multi_core(
state: &AppState,
connection_id: &str,
@@ -12769,6 +12784,40 @@ for line in sys.stdin:
cursor_ids.discard(cursor_id)
print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": closed}}), flush=True)
continue
+ if method == "execute_query_page":
+ # First cursor page: two rows and an open result session on this
+ # transaction connection.
+ params = request.get("params", {{}})
+ page_size = int(params.get("pageSize") or 1)
+ rows = [["page-row-" + str(index)] for index in range(1, min(page_size, 2) + 1)]
+ result = {{
+ "columns": ["val"],
+ "column_types": [],
+ "column_sortables": [],
+ "rows": rows,
+ "affected_rows": 0,
+ "execution_time_ms": 1,
+ "truncated": False,
+ "session_id": "cursor-1",
+ "has_more": True
+ }}
+ print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": result}}), flush=True)
+ continue
+ if method == "fetch_query_page":
+ # Continuation page of the same cursor: one last row.
+ result = {{
+ "columns": ["val"],
+ "column_types": [],
+ "column_sortables": [],
+ "rows": [["page-row-3"]],
+ "affected_rows": 0,
+ "execution_time_ms": 1,
+ "truncated": False,
+ "session_id": "cursor-1",
+ "has_more": False
+ }}
+ print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "result": result}}), flush=True)
+ continue
if method == "execute_query":
params = request.get("params", {{}})
sql = params.get("sql", "")
@@ -12875,6 +12924,115 @@ for line in sys.stdin:
let _ = std::fs::remove_dir_all(dir);
}
+ #[tokio::test]
+ async fn manual_transaction_agent_cursor_support_is_agent_only() {
+ let (state, txn_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;
+ // The fake Oracle transaction holds an Agent connection, so exports may
+ // page it through an Agent result session.
+ assert!(manual_transaction_supports_agent_cursor(&state, &txn_id).await);
+ assert!(!manual_transaction_supports_agent_cursor(&state, "missing-txn").await);
+
+ // A native transaction connection only pages with SQL.
+ let mysql_state = AppState::new(crate::persistence::test_storage::open(&dir.join("mysql.db")).await.unwrap());
+ let mysql_txn_id = uuid::Uuid::new_v4().to_string();
+ mysql_state.transaction_sessions.write().await.insert(
+ mysql_txn_id.clone(),
+ TransactionSession {
+ sqlserver: None,
+ connection: Arc::new(tokio::sync::Mutex::new(TxnConnection::Mysql(None))),
+ pool_key: "mysql-conn".to_string(),
+ last_activity: std::time::Instant::now(),
+ idle_timeout: std::time::Duration::from_secs(60),
+ busy: false,
+ snapshot_rotation_safe: false,
+ connection_id: "mysql-conn".to_string(),
+ database: String::new(),
+ schema: None,
+ },
+ );
+ assert!(!manual_transaction_supports_agent_cursor(&mysql_state, &mysql_txn_id).await);
+
+ rollback_manual_transaction(&state, &txn_id).await.unwrap();
+ drop(mysql_state);
+ drop(state);
+ let _ = std::fs::remove_dir_all(dir);
+ }
+
+ /// Builds a query-result export request for the fake Agent transaction above.
+ fn manual_transaction_export_request(
+ txn_id: &str,
+ file_path: &std::path::Path,
+ sql: &str,
+ page_size: usize,
+ ) -> crate::data::query_result_export::QueryResultExportRequest {
+ serde_json::from_value(serde_json::json!({
+ "exportId": "export-1",
+ "connectionId": "agent-conn",
+ "database": "ORCL",
+ "schema": "APP",
+ "sql": sql,
+ "queryBaseSql": sql,
+ "databaseType": "oracle",
+ "useAgentCursor": true,
+ "filePath": file_path.to_string_lossy(),
+ "format": "csv",
+ "pageSize": page_size,
+ "txnSessionId": txn_id,
+ }))
+ .unwrap()
+ }
+
+ #[tokio::test]
+ async fn oracle_query_result_export_pages_through_the_manual_transaction_cursor() {
+ let (state, txn_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;
+ let csv_path = dir.join("cursor-export.csv");
+ // Wildcard projection: no safe ROWNUM wrapper, so the export needs the
+ // Agent result session to reach the second page — inside the transaction.
+ let request = manual_transaction_export_request(&txn_id, &csv_path, "SELECT * FROM APP.EVENTS", 2);
+ let progress = Arc::new(std::sync::Mutex::new(Vec::new()));
+ let progress_sink = progress.clone();
+ crate::data::query_result_export::export_query_result_core(&state, &request, None, move |update| {
+ progress_sink.lock().unwrap().push(update);
+ })
+ .await
+ .expect("manual-transaction export pages through the transaction cursor");
+
+ let csv = std::fs::read_to_string(&csv_path).unwrap();
+ for row in ["page-row-1", "page-row-2", "page-row-3"] {
+ assert!(csv.contains(row), "missing {row} in {csv}");
+ }
+ let terminal = progress.lock().unwrap().last().cloned().unwrap();
+ assert!(matches!(terminal.status, crate::database_export::ExportStatus::Done));
+ assert_eq!(terminal.rows_exported, 3);
+
+ rollback_manual_transaction(&state, &txn_id).await.unwrap();
+ drop(state);
+ let _ = std::fs::remove_dir_all(dir);
+ }
+
+ #[tokio::test]
+ async fn oracle_rownum_bounded_export_runs_once_inside_the_manual_transaction() {
+ let (state, txn_id, dir) = manual_transaction_test_state(DatabaseType::Oracle).await;
+ let csv_path = dir.join("bounded-export.csv");
+ let sql = "SELECT FJFY, JCBW FROM T_YL_JCZD01 WHERE ROWNUM < 2000;";
+ let request = manual_transaction_export_request(&txn_id, &csv_path, sql, 2000);
+ crate::data::query_result_export::export_query_result_core(&state, &request, None, |_| {})
+ .await
+ .expect("ROWNUM-bounded export runs as one bounded transaction execution");
+
+ let csv = std::fs::read_to_string(&csv_path).unwrap();
+ // The fake transaction connection answers the statement itself (no
+ // cursor page fetch), proving the single-execution path was taken. The
+ // transaction executor strips the trailing semicolon, hence the prefix
+ // assertion rather than a byte-for-byte match.
+ assert!(csv.contains("FJFY") && csv.contains("ROWNUM < 2000"), "unexpected export content: {csv}");
+ assert!(!csv.contains("page-row"), "bounded export must not open a cursor: {csv}");
+
+ rollback_manual_transaction(&state, &txn_id).await.unwrap();
+ drop(state);
+ let _ = std::fs::remove_dir_all(dir);
+ }
+
fn manual_sql_file_request(session_id: &str) -> crate::sql::SqlFileRequest {
crate::sql::SqlFileRequest {
txn_session_id: Some(session_id.to_string()),
diff --git a/crates/dbx-core/tests/live_oracle_query_result_export_transaction.rs b/crates/dbx-core/tests/live_oracle_query_result_export_transaction.rs
new file mode 100644
index 0000000000..9fa4eece86
--- /dev/null
+++ b/crates/dbx-core/tests/live_oracle_query_result_export_transaction.rs
@@ -0,0 +1,234 @@
+use dbx_core::connection::AppState;
+use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
+use dbx_core::query::{
+ begin_manual_transaction, execute_in_manual_transaction, execute_sql_statement, rollback_manual_transaction,
+};
+use dbx_core::query_result_export::{export_query_result_core, QueryResultExportRequest};
+use std::sync::Arc;
+
+// More rows than one export page, so the export cannot be served by a single page.
+const ROWS: usize = 650;
+
+fn live_oracle_config(id: &str) -> ConnectionConfig {
+ let host = std::env::var("DBX_LIVE_ORACLE_HOST").expect("DBX_LIVE_ORACLE_HOST");
+ let port = std::env::var("DBX_LIVE_ORACLE_PORT").ok().and_then(|value| value.parse::<u16>().ok()).unwrap_or(1521);
+ let service = std::env::var("DBX_LIVE_ORACLE_SERVICE").expect("DBX_LIVE_ORACLE_SERVICE");
+ let username = std::env::var("DBX_LIVE_ORACLE_USER").expect("DBX_LIVE_ORACLE_USER");
+ let password = std::env::var("DBX_LIVE_ORACLE_PASSWORD").expect("DBX_LIVE_ORACLE_PASSWORD");
+
+ serde_json::from_value(serde_json::json!({
+ "id": id,
+ "name": id,
+ "db_type": DatabaseType::Oracle,
+ "host": host,
+ "port": port,
+ "username": username,
+ "password": password,
+ "database": service,
+ "connect_timeout_secs": 10,
+ "query_timeout_secs": 60,
+ "idle_timeout_secs": 60,
+ "keepalive_interval_secs": 0
+ }))
+ .expect("live Oracle export config should deserialize")
+}
+
+fn export_request(
+ connection_id: &str,
+ database: &str,
+ sql: &str,
+ file_path: &str,
+ page_size: usize,
+ client_session_id: &str,
+ txn_session_id: Option<&str>,
+) -> QueryResultExportRequest {
+ serde_json::from_value(serde_json::json!({
+ "exportId": uuid::Uuid::new_v4().to_string(),
+ "connectionId": connection_id,
+ "database": database,
+ "sql": sql,
+ "queryBaseSql": sql,
+ "databaseType": DatabaseType::Oracle,
+ "useAgentCursor": true,
+ "filePath": file_path,
+ "format": "csv",
+ "pageSize": page_size,
+ "clientSessionId": client_session_id,
+ "txnSessionId": txn_session_id,
+ }))
+ .expect("export request should deserialize")
+}
+
+fn csv_data_row_count(path: &std::path::Path) -> usize {
+ std::fs::read_to_string(path).unwrap().lines().skip(1).filter(|line| !line.trim().is_empty()).count()
+}
+
+fn csv_rows_with_value(path: &std::path::Path, value: &str) -> usize {
+ let text = std::fs::read_to_string(path).unwrap();
+ text.lines()
+ .skip(1)
+ .filter(|line| line.trim_end().rsplit(',').next().is_some_and(|field| field.trim_matches('"') == value))
+ .count()
+}
+
+async fn committed_rows_with_v1(state: &AppState, connection_id: &str, database: &str, table: &str) -> String {
+ let result = execute_sql_statement(
+ state,
+ connection_id,
+ database,
+ &format!("SELECT COUNT(*) FROM {table} WHERE V = 1"),
+ None,
+ None,
+ )
+ .await
+ .unwrap();
+ result.rows[0][0].as_str().map(str::to_string).unwrap_or_else(|| result.rows[0][0].to_string())
+}
+
+async fn seed_table(state: &AppState, connection_id: &str, database: &str, table: &str) {
+ let _ =
+ execute_sql_statement(state, connection_id, database, &format!("DROP TABLE {table} PURGE"), None, None).await;
+ execute_sql_statement(
+ state,
+ connection_id,
+ database,
+ &format!("CREATE TABLE {table} (ID NUMBER PRIMARY KEY, V NUMBER NOT NULL)"),
+ None,
+ None,
+ )
+ .await
+ .unwrap();
+ // Oracle 11g has no `VALUES` table constructor or `generate_series`; CONNECT BY
+ // generates the synthetic 650-row fixture in one statement.
+ execute_sql_statement(
+ state,
+ connection_id,
+ database,
+ &format!("INSERT INTO {table} (ID, V) SELECT LEVEL, 0 FROM DUAL CONNECT BY LEVEL <= {ROWS}"),
+ None,
+ None,
+ )
+ .await
+ .unwrap();
+}
+
+async fn live_state(id: &str) -> (Arc<AppState>, std::path::PathBuf) {
+ let dir = std::env::temp_dir().join(format!("dbx-live-oracle-export-txn-{id}"));
+ std::fs::create_dir_all(&dir).unwrap();
+ let storage = dbx_core::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
+ (Arc::new(AppState::new(storage)), dir)
+}
+
+fn unique_id() -> String {
+ uuid::Uuid::new_v4().simple().to_string()[..8].to_uppercase()
+}
+
+#[tokio::test]
+#[ignore = "requires a disposable Oracle 11g/18c+ endpoint (DBX_LIVE_ORACLE_*)"]
+async fn live_oracle_query_result_export_pages_through_open_manual_transaction() {
+ let suffix = unique_id();
+ let connection_id = format!("live-oracle-export-txn-{suffix}");
+ let table = format!("DBX_EXPORT_TXN_{suffix}");
+ let service = std::env::var("DBX_LIVE_ORACLE_SERVICE").unwrap();
+ let (state, dir) = live_state(&suffix).await;
+ state.configs.write().await.insert(connection_id.clone(), live_oracle_config(&connection_id));
+ seed_table(&state, &connection_id, &service, &table).await;
+
+ let txn = begin_manual_transaction(&state, &connection_id, &service, None, None).await.unwrap();
+ execute_in_manual_transaction(&state, &txn, &format!("UPDATE {table} SET V = 1"), &service, None, None)
+ .await
+ .unwrap();
+
+ // The user-reported statement shape: an existing ROWNUM predicate makes the
+ // Oracle ROWNUM wrapper unsafe, so the export needs the Agent cursor. With a
+ // page size below the ROWNUM bound it must stream several cursor pages.
+ let sql = format!("SELECT ID, V FROM {table} WHERE ROWNUM < 2000");
+ let with_txn_path = dir.join("with-txn.csv");
+ let request = export_request(
+ &connection_id,
+ &service,
+ &sql,
+ with_txn_path.to_str().unwrap(),
+ 100,
+ &format!("tab:export:{suffix}-a"),
+ Some(&txn),
+ );
+ let with_txn_result = export_query_result_core(&state, &request, None, |_| {}).await;
+
+ let without_txn_path = dir.join("without-txn.csv");
+ let request = export_request(
+ &connection_id,
+ &service,
+ &sql,
+ without_txn_path.to_str().unwrap(),
+ 100,
+ &format!("tab:export:{suffix}-b"),
+ None,
+ );
+ let without_txn_result = export_query_result_core(&state, &request, None, |_| {}).await;
+
+ // The export must leave the transaction open: rolling it back discards the update.
+ let rollback_result = rollback_manual_transaction(&state, &txn).await;
+ let committed_after_rollback = committed_rows_with_v1(&state, &connection_id, &service, &table).await;
+
+ let with_txn_rows = csv_data_row_count(&with_txn_path);
+ let without_txn_rows = csv_data_row_count(&without_txn_path);
+ let with_txn_updated = csv_rows_with_value(&with_txn_path, "1");
+ let without_txn_updated = csv_rows_with_value(&without_txn_path, "1");
+ let _ =
+ execute_sql_statement(&state, &connection_id, &service, &format!("DROP TABLE {table} PURGE"), None, None).await;
+ let _ = std::fs::remove_dir_all(&dir);
+
+ with_txn_result.unwrap();
+ without_txn_result.unwrap();
+ rollback_result.unwrap();
+ assert_eq!(with_txn_rows, ROWS, "export inside the transaction should export every row");
+ assert_eq!(with_txn_updated, ROWS, "export inside the transaction should see its uncommitted update");
+ assert_eq!(without_txn_rows, ROWS, "export without the transaction should still export every row");
+ assert_eq!(without_txn_updated, 0, "export without the transaction reads committed data only");
+ assert_eq!(committed_after_rollback, "0", "export must not commit the manual transaction");
+}
+
+#[tokio::test]
+#[ignore = "requires a disposable Oracle 11g/18c+ endpoint (DBX_LIVE_ORACLE_*)"]
+async fn live_oracle_rownum_bounded_export_runs_once_inside_manual_transaction() {
+ let suffix = unique_id();
+ let connection_id = format!("live-oracle-export-txn-{suffix}");
+ let table = format!("DBX_EXPORT_TXN_{suffix}");
+ let service = std::env::var("DBX_LIVE_ORACLE_SERVICE").unwrap();
+ let (state, dir) = live_state(&suffix).await;
+ state.configs.write().await.insert(connection_id.clone(), live_oracle_config(&connection_id));
+ seed_table(&state, &connection_id, &service, &table).await;
+
+ let txn = begin_manual_transaction(&state, &connection_id, &service, None, None).await.unwrap();
+ execute_in_manual_transaction(&state, &txn, &format!("UPDATE {table} SET V = 1"), &service, None, None)
+ .await
+ .unwrap();
+
+ // Default export page size (2000) is above the ROWNUM bound (1999), so the
+ // export can run as one bounded statement instead of paging a cursor.
+ let sql = format!("SELECT ID, V FROM {table} WHERE ROWNUM < 2000");
+ let path = dir.join("bounded.csv");
+ let request = export_request(
+ &connection_id,
+ &service,
+ &sql,
+ path.to_str().unwrap(),
+ 2000,
+ &format!("tab:export:{suffix}-c"),
+ Some(&txn),
+ );
+ let result = export_query_result_core(&state, &request, None, |_| {}).await;
+
+ let rollback_result = rollback_manual_transaction(&state, &txn).await;
+ let rows = csv_data_row_count(&path);
+ let updated = csv_rows_with_value(&path, "1");
+ let _ =
+ execute_sql_statement(&state, &connection_id, &service, &format!("DROP TABLE {table} PURGE"), None, None).await;
+ let _ = std::fs::remove_dir_all(&dir);
+
+ result.unwrap();
+ rollback_result.unwrap();
+ assert_eq!(rows, ROWS, "ROWNUM-bounded export should export every row");
+ assert_eq!(updated, ROWS, "ROWNUM-bounded export should see the transaction's uncommitted update");
+}
diff --git a/crates/dbx-sql-data/src/query_result_sql.rs b/crates/dbx-sql-data/src/query_result_sql.rs
index 3605d6d3fd..c5f208c2a5 100644
--- a/crates/dbx-sql-data/src/query_result_sql.rs
+++ b/crates/dbx-sql-data/src/query_result_sql.rs
@@ -10,10 +10,10 @@ use crate::sql_dialect::{
quote_table_identifier, PaginationContext, TablePaginationStrategy,
};
use sqlparser::ast::{
- visit_expressions, Expr, ForClause, GroupByExpr, LimitClause, ObjectNamePart, OrderByKind, Select, SelectItem,
- SelectModifiers, SetExpr, Statement, TableFactor, Value, ValueWithSpan,
+ visit_expressions, BinaryOperator, Expr, ForClause, GroupByExpr, LimitClause, ObjectNamePart, OrderByKind, Select,
+ SelectItem, SelectModifiers, SetExpr, Statement, TableFactor, Value, ValueWithSpan,
};
-use sqlparser::dialect::{ClickHouseDialect, GenericDialect, MsSqlDialect, MySqlDialect};
+use sqlparser::dialect::{ClickHouseDialect, GenericDialect, MsSqlDialect, MySqlDialect, OracleDialect};
use sqlparser::parser::Parser;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
@@ -2088,6 +2088,67 @@ fn has_top_level_rownum(sql: &str) -> bool {
top_level_sql_tokens(sql).iter().any(|token| token.text == "ROWNUM")
}
+/// Exact upper bound on the rows a query can return when it already filters on
+/// Oracle's `ROWNUM` pseudo-column with a literal comparison (`ROWNUM < n`,
+/// `ROWNUM <= n`, in either operand order, optionally combined through `AND`).
+///
+/// Such a statement bounds itself, so it can be exported in one execution
+/// without a LIMIT/OFFSET rewrite (which Oracle 11g does not have) or an Agent
+/// cursor page — the predicate keeps the buffered result below the export page
+/// size. Anything ambiguous (expressions, placeholders, `OR`, ROWNUM inside a
+/// subquery) yields `None` so the caller keeps its normal pagination path.
+pub fn top_level_rownum_row_count(sql: &str) -> Option<usize> {
+ let statements = Parser::parse_sql(&OracleDialect {}, sql).ok()?;
+ let [Statement::Query(query)] = statements.as_slice() else {
+ return None;
+ };
+ let SetExpr::Select(select) = query.body.as_ref() else {
+ return None;
+ };
+ rownum_bound_from_expr(select.selection.as_ref()?)
+}
+
+fn rownum_bound_from_expr(expr: &Expr) -> Option<usize> {
+ match expr {
+ Expr::Nested(inner) => rownum_bound_from_expr(inner),
+ Expr::BinaryOp { left, op, right } if *op == BinaryOperator::And => {
+ match (rownum_bound_from_expr(left), rownum_bound_from_expr(right)) {
+ (Some(left_bound), Some(right_bound)) => Some(left_bound.min(right_bound)),
+ (Some(bound), None) | (None, Some(bound)) => Some(bound),
+ (None, None) => None,
+ }
+ }
+ Expr::BinaryOp { left, op, right } => {
+ let (rownum, literal, inclusive) = match op {
+ BinaryOperator::Lt => (left, right, false),
+ BinaryOperator::LtEq => (left, right, true),
+ // `2000 > ROWNUM` / `2000 >= ROWNUM` bound ROWNUM from above too.
+ BinaryOperator::Gt => (right, left, false),
+ BinaryOperator::GtEq => (right, left, true),
+ _ => return None,
+ };
+ if !is_rownum_column(rownum) {
+ return None;
+ }
+ let bound = literal_row_count(literal)?;
+ Some(if inclusive { bound } else { bound.saturating_sub(1) })
+ }
+ _ => None,
+ }
+}
+
+fn is_rownum_column(expr: &Expr) -> bool {
+ matches!(expr, Expr::Identifier(ident) if ident.value.eq_ignore_ascii_case("ROWNUM"))
+}
+
+fn literal_row_count(expr: &Expr) -> Option<usize> {
+ match expr {
+ Expr::Value(ValueWithSpan { value: Value::Number(number, _), .. }) => number.parse().ok(),
+ Expr::Nested(inner) => literal_row_count(inner),
+ _ => None,
+ }
+}
+
fn has_top_level_offset_fetch_next(sql: &str) -> bool {
let tokens = top_level_sql_tokens(sql);
let has_offset = tokens.iter().any(|token| token.text == "OFFSET");
@@ -5179,6 +5240,30 @@ WHERE u.id = picked.id;
}
}
+ #[test]
+ fn top_level_rownum_row_count_bounds_rownum_filtered_queries() {
+ for (sql, expected) in [
+ ("SELECT FJFY, JCBW FROM T_YL_JCZD01 WHERE ROWNUM < 2000;", Some(1999)),
+ ("SELECT FJFY, JCBW FROM T_YL_JCZD01 WHERE ROWNUM <= 2000;", Some(2000)),
+ ("SELECT id FROM users WHERE ROWNUM < 10 AND active = 1", Some(9)),
+ ("SELECT id FROM users WHERE active = 1 AND ROWNUM <= 100", Some(100)),
+ ("SELECT id FROM users WHERE 2000 > ROWNUM", Some(1999)),
+ ("SELECT id FROM users WHERE (ROWNUM <= 50)", Some(50)),
+ ("SELECT id FROM users WHERE ROWNUM < 1", Some(0)),
+ // Ambiguous shapes must keep the normal pagination path.
+ ("SELECT id FROM users WHERE ROWNUM < 2000 OR active = 1", None),
+ ("SELECT id FROM users WHERE ROWNUM < :limit", None),
+ ("SELECT id FROM users WHERE ROWNUM > 10", None),
+ ("SELECT id FROM (SELECT id FROM users WHERE ROWNUM < 10) t WHERE ROWNUM <= 5", Some(5)),
+ ("SELECT id FROM (SELECT id FROM users WHERE ROWNUM < 10) t", None),
+ ("SELECT ROWNUM, id FROM users", None),
+ ("SELECT id FROM users", None),
+ ("SELECT id FROM users; SELECT 1", None),
+ ] {
+ assert_eq!(top_level_rownum_row_count(sql), expected, "{sql}");
+ }
+ }
+
#[test]
fn oceanbase_oracle_pagination_wraps_with_rownum() {
let result = build_paginated_query_sql(PaginatedQuerySqlOptions {