fix(sql): preserve duplicate OceanBase Oracle projections when paging

Closes #10722
This commit is contained in:
t8y2 committed 2026-10-02 18:40:46 +00:00
1 parent c30b198455
commit 6d1c3c8373
10 files changed
+702 -18

No files matched your search

@@ -339,6 +339,19 @@ public final class JdbcExecutor {
return executePage(conn, sql, schema, setSchemaSql, () -> "", options, valueReader, StatementMessageReader.NONE, tableReadSessions); return executePage(conn, sql, schema, setSchemaSql, () -> "", options, valueReader, StatementMessageReader.NONE, tableReadSessions);
} }
public QueryPageResult executeBoundedPage(
Connection conn,
String sql,
String schema,
Function<String, String> setSchemaSql,
Supplier<String> resetSchemaSql,
QueryPageOptions options,
ResultValueReader valueReader
) {
return executePage(conn, sql, schema, setSchemaSql, resetSchemaSql, options, valueReader,
StatementMessageReader.NONE, sessions, false, true);
}
public QueryPageResult startTableRead( public QueryPageResult startTableRead(
Connection conn, Connection conn,
String sql, String sql,
@@ -387,6 +400,23 @@ public final class JdbcExecutor {
StatementMessageReader statementMessageReader, StatementMessageReader statementMessageReader,
ConcurrentHashMap<String, QuerySession> targetSessions, ConcurrentHashMap<String, QuerySession> targetSessions,
boolean advancePastUpdateCounts boolean advancePastUpdateCounts
) {
return executePage(conn, sql, schema, setSchemaSql, resetSchemaSql, options, valueReader,
statementMessageReader, targetSessions, advancePastUpdateCounts, false);
}
private QueryPageResult executePage(
Connection conn,
String sql,
String schema,
Function<String, String> setSchemaSql,
Supplier<String> resetSchemaSql,
QueryPageOptions options,
ResultValueReader valueReader,
StatementMessageReader statementMessageReader,
ConcurrentHashMap<String, QuerySession> targetSessions,
boolean advancePastUpdateCounts,
boolean boundStatementRows
) { ) {
return unchecked(() -> { return unchecked(() -> {
expireIdleSessions(targetSessions, System.currentTimeMillis(), QUERY_SESSION_IDLE_TIMEOUT_MILLIS); expireIdleSessions(targetSessions, System.currentTimeMillis(), QUERY_SESSION_IDLE_TIMEOUT_MILLIS);
@@ -402,9 +432,14 @@ public final class JdbcExecutor {
QuerySession createdSession = null; QuerySession createdSession = null;
activeStatements.add(stmt); activeStatements.add(stmt);
try { try {
if (boundStatementRows) {
stmt.setMaxRows(statementMaxRows(options.getMaxRows()));
}
applyQueryTimeout(stmt, options.getTimeoutSecs()); applyQueryTimeout(stmt, options.getTimeoutSecs());
if (options.getFetchSize() != null && options.getFetchSize() > 0) { if (options.getFetchSize() != null && options.getFetchSize() > 0) {
stmt.setFetchSize(options.getFetchSize()); stmt.setFetchSize(options.getFetchSize());
} else if (boundStatementRows) {
stmt.setFetchSize(Math.max(1, Math.min(options.getPageSize(), Math.max(options.getMaxRows(), 1))));
} }
// Keep script transaction-control statements in the SQL stream. // Keep script transaction-control statements in the SQL stream.
// JDBC transaction APIs are reserved for executeTransaction. // JDBC transaction APIs are reserved for executeTransaction.
@@ -0,0 +1,194 @@
package com.dbx.agent;
import org.junit.jupiter.api.Test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class JdbcBoundedCursorTest {
@Test
void boundsTheWholeCursorAndPreservesDuplicateColumnsAndLookahead() {
for (int max : new int[]{0, 1, 3, 10000, Integer.MAX_VALUE}) {
for (int count : new int[]{0, 1, 2, 3, 4, 5}) {
Fixture f = new Fixture(count);
QueryPageResult page = f.start(new QueryPageOptions(1, null, max));
List<List<Object>> rows = new ArrayList<>(page.getRows());
while (page.getHas_more()) {
page = f.executor.fetchPage(page.getSession_id(), 1);
rows.addAll(page.getRows());
}
int expected = Math.min(count, Math.max(max, 1));
assertEquals(expected, rows.size());
for (int i = 0; i < expected; i++) assertEquals(List.of(i + 1, i + 1), rows.get(i));
assertEquals(List.of("NAME", "NAME"), page.getColumns());
assertEquals(count > expected, page.getTruncated());
assertEquals(JdbcExecutor.statementMaxRows(max), f.statementLimit);
assertEquals(1, f.fetchSize);
f.assertClosed();
}
}
}
@Test
void usesPositiveDefaultFetchSizeAndPreservesExplicitFetchSize() {
for (Integer fetch : new Integer[]{null, 0, -1, 7}) {
Fixture f = new Fixture(5);
f.start(new QueryPageOptions(0, fetch, 3));
assertEquals(fetch != null && fetch > 0 ? fetch : 1, f.fetchSize);
f.executor.closeAllQuerySessions();
f.assertClosed();
}
Fixture defaults = new Fixture(1);
defaults.start(new QueryPageOptions());
assertEquals(10001, defaults.statementLimit);
assertEquals(100, defaults.fetchSize);
defaults.assertClosed();
}
@Test
void leavesOrdinaryCursorPolicyUnchanged() {
Fixture f = new Fixture(5);
f.executor.executePage(f.connection, "SELECT name, name FROM people", null, schema -> "",
new QueryPageOptions(1, null, 3));
assertEquals(0, f.statementLimit);
assertEquals(0, f.fetchSize);
f.executor.cancelActiveStatements();
f.assertClosed();
}
@Test
void cleansUpLimitFetchExecuteAndReadFailures() {
for (String failure : List.of("setMaxRows", "setFetchSize", "execute", "next")) {
Fixture f = new Fixture(5);
f.failure = failure;
RuntimeException error = assertThrows(RuntimeException.class, () -> f.start(new QueryPageOptions()));
assertTrue(error.toString().contains("fixture " + failure));
f.assertClosed();
}
Fixture f = new Fixture(5);
QueryPageResult page = f.start(new QueryPageOptions(1, null, 3));
f.failure = "next";
assertThrows(RuntimeException.class, () -> f.executor.fetchPage(page.getSession_id(), 1));
f.assertClosed();
}
@Test
void cancelsActiveExecutionAndIdleBoundedCursor() {
Fixture executing = new Fixture(5);
executing.cancelOnExecute = true;
assertThrows(RuntimeException.class, () -> executing.start(new QueryPageOptions()));
assertEquals(1, executing.cancels);
executing.assertClosed();
Fixture idle = new Fixture(5);
idle.start(new QueryPageOptions(1, null, 3));
idle.executor.cancelActiveStatements();
assertEquals(1, idle.cancels);
idle.assertClosed();
Fixture fetching = new Fixture(5);
QueryPageResult page = fetching.start(new QueryPageOptions(1, null, 3));
fetching.cancelOnNext = true;
assertThrows(RuntimeException.class, () -> fetching.executor.fetchPage(page.getSession_id(), 1));
assertEquals(1, fetching.cancels);
fetching.assertClosed();
}
private static final class Fixture {
final JdbcExecutor executor = new JdbcExecutor();
final int count;
int row;
int statementLimit;
int fetchSize;
int closes;
int cancels;
String failure;
boolean cancelOnExecute;
boolean cancelOnNext;
final ResultSetMetaData metadata = proxy(ResultSetMetaData.class, (object, method, args) -> {
switch (method.getName()) {
case "getColumnCount": return 2;
case "getColumnLabel": return "NAME";
case "getColumnType": return Types.INTEGER;
case "getColumnTypeName": return "NUMBER";
default: return zero(method.getReturnType());
}
});
final ResultSet result = proxy(ResultSet.class, (object, method, args) -> {
fail(method.getName());
switch (method.getName()) {
case "getMetaData": return metadata;
case "next": return nextRow();
case "getInt": case "getObject": return row;
default: return zero(method.getReturnType());
}
});
final Statement statement = proxy(Statement.class, (object, method, args) -> {
fail(method.getName());
switch (method.getName()) {
case "hashCode": return System.identityHashCode(object);
case "equals": return object == args[0];
case "setMaxRows": statementLimit = (Integer) args[0]; return null;
case "setFetchSize": fetchSize = (Integer) args[0]; return null;
case "execute":
if (cancelOnExecute) {
executor.cancelActiveStatements();
throw new SQLException("fixture canceled");
}
return true;
case "getResultSet": return result;
case "close": closes++; return null;
case "cancel": cancels++; return null;
default: return zero(method.getReturnType());
}
});
final Connection connection = proxy(Connection.class, (object, method, args) ->
"createStatement".equals(method.getName()) ? statement : zero(method.getReturnType()));
Fixture(int count) { this.count = count; }
boolean nextRow() throws SQLException {
if (cancelOnNext) {
executor.cancelActiveStatements();
throw new SQLException("fixture canceled during fetch");
}
return ++row <= count && (statementLimit == 0 || row <= statementLimit);
}
void fail(String method) throws SQLException {
if (method.equals(failure)) throw new SQLException("fixture " + method);
}
QueryPageResult start(QueryPageOptions options) {
return executor.executeBoundedPage(connection, "SELECT name, name FROM people", null,
schema -> "", () -> "", options, executor::defaultResultValue);
}
void assertClosed() {
assertTrue(closes > 0);
assertFalse(executor.hasActiveStatements());
assertFalse(executor.hasOpenSessions());
}
}
private static Object zero(Class<?> type) {
if (type == boolean.class) return false;
if (type == int.class) return 0;
if (type == long.class) return 0L;
return null;
}
private static <T> T proxy(Class<T> type, InvocationHandler handler) {
return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, handler));
}
}
@@ -100,7 +100,13 @@ public final class OceanBaseOracleAgent extends ConfiguredJdbcAgent {
@Override @Override
public QueryPageResult executeQueryPage(String sql, String schema, QueryPageOptions options) { public QueryPageResult executeQueryPage(String sql, String schema, QueryPageOptions options) {
try (QueryTiming timing = QueryTiming.begin()) { try (QueryTiming timing = QueryTiming.begin()) {
QueryPageResult result = super.executeQueryPage(sql, schema, options); long prepareStarted = System.nanoTime();
Connection connection = requireConnected();
uncheckedVoid(() -> beforeQueryExecution(connection, options.getTimeoutSecs()));
QueryTiming.record("session_prepare", prepareStarted);
QueryPageResult result = JdbcExecutor.current().executeBoundedPage(
connection, sql, schema, this::setSchemaSQL, this::resetSchemaSQL, options, resultValueReader()
);
result.setQuery_timings_ms(timing.finish()); result.setQuery_timings_ms(timing.finish());
return result; return result;
} }
@@ -147,7 +147,8 @@ class OceanBaseOracleAgentTest {
List<String> auditSql = new ArrayList<>(); List<String> auditSql = new ArrayList<>();
Connection auditConnection = auditTimingConnection(1, 2, false, auditSql, new ArrayList<>(), auditLimits); Connection auditConnection = auditTimingConnection(1, 2, false, auditSql, new ArrayList<>(), auditLimits);
int[] row = {-1}; int[] row = {-1};
int[] queryLimit = {0}; // JDBC's default, independent of the client-side cap. int[] queryLimit = {0};
int[] queryFetchSize = {0};
boolean[] queryStarted = {false}; boolean[] queryStarted = {false};
boolean[] resultClosed = {false}; boolean[] resultClosed = {false};
boolean[] statementClosed = {false}; boolean[] statementClosed = {false};
@@ -167,6 +168,7 @@ class OceanBaseOracleAgentTest {
}); });
Statement statement = proxy(Statement.class, (method, args) -> { Statement statement = proxy(Statement.class, (method, args) -> {
if ("setMaxRows".equals(method.getName())) queryLimit[0] = (Integer) args[0]; if ("setMaxRows".equals(method.getName())) queryLimit[0] = (Integer) args[0];
if ("setFetchSize".equals(method.getName())) queryFetchSize[0] = (Integer) args[0];
if ("execute".equals(method.getName())) { if ("execute".equals(method.getName())) {
queryStarted[0] = !String.valueOf(args[0]).startsWith("ALTER SESSION"); queryStarted[0] = !String.valueOf(args[0]).startsWith("ALTER SESSION");
statementClosed[0] = false; statementClosed[0] = false;
@@ -198,7 +200,8 @@ class OceanBaseOracleAgentTest {
Assertions.assertFalse(result.getHas_more()); Assertions.assertFalse(result.getHas_more());
Assertions.assertEquals(truncated, result.getTruncated()); Assertions.assertEquals(truncated, result.getTruncated());
Assertions.assertEquals(truncated ? List.of(List.of(1)) : List.of(List.of(1), List.of(2)), rows); Assertions.assertEquals(truncated ? List.of(List.of(1)) : List.of(List.of(1), List.of(2)), rows);
Assertions.assertEquals(0, queryLimit[0], "the paging cap must not change the JDBC statement limit"); Assertions.assertEquals(maxRows + 1, queryLimit[0]);
Assertions.assertTrue(queryFetchSize[0] > 0);
Assertions.assertTrue(auditSql.isEmpty(), "completed queries must not read trace or audit records"); Assertions.assertTrue(auditSql.isEmpty(), "completed queries must not read trace or audit records");
Assertions.assertTrue(auditLimits.isEmpty()); Assertions.assertTrue(auditLimits.isEmpty());
Assertions.assertNull(result.getServer_execute_time_us()); Assertions.assertNull(result.getServer_execute_time_us());
@@ -0,0 +1,78 @@
package com.dbx.agent.oceanbaseoracle;
import com.dbx.agent.JdbcExecutor;
import com.dbx.agent.QueryPageOptions;
import com.oceanbase.jdbc.OceanBaseConnection;
import com.oceanbase.jdbc.UrlParser;
import com.oceanbase.jdbc.internal.com.read.dao.Results;
import com.oceanbase.jdbc.internal.protocol.MasterProtocol;
import com.oceanbase.jdbc.internal.util.pool.GlobalStateInfo;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import static org.junit.jupiter.api.Assertions.*;
class OceanBaseOracleCursorTransportTest {
@Test
void connectorKeepsSqlAndAppliesCumulativeLimitIncludingLookahead() throws Exception {
for (String sql : List.of("SELECT a.name, a.* FROM people a", "SELECT name, name FROM people",
"SELECT a.*, b.* FROM people a JOIN people b ON a.id=b.id")) {
for (int max : new int[]{0, 1, 10000, Integer.MAX_VALUE}) {
for (boolean fallback : new boolean[]{false, true}) {
Capture protocol = new Capture(fallback);
OceanBaseConnection connection = new OceanBaseConnection(protocol);
JdbcExecutor executor = new JdbcExecutor();
RuntimeException error = assertThrows(RuntimeException.class, () -> executor.executeBoundedPage(
connection, sql, null, schema -> "", () -> "", new QueryPageOptions(2, null, max),
executor::defaultResultValue));
assertTrue(error.toString().contains("STOP_BEFORE_NETWORK"));
assertEquals("set @@_ORACLE_SQL_SELECT_LIMIT = " + JdbcExecutor.statementMaxRows(max), protocol.sent.get(0));
if (fallback) assertEquals("set @@SQL_SELECT_LIMIT = " + JdbcExecutor.statementMaxRows(max), protocol.sent.get(1));
assertEquals(sql, protocol.sent.get(protocol.sent.size() - 1));
assertFalse(executor.hasActiveStatements());
assertFalse(executor.hasOpenSessions());
protocol.sent.clear();
try (Statement statement = connection.createStatement()) {
assertThrows(SQLException.class, () -> statement.execute("SELECT 1 FROM DUAL"));
}
assertEquals("set @@_ORACLE_SQL_SELECT_LIMIT = DEFAULT", protocol.sent.get(0));
if (fallback) assertEquals("set @@SQL_SELECT_LIMIT = DEFAULT", protocol.sent.get(1));
}
}
}
}
private static final class Capture extends MasterProtocol {
final List<String> sent = new ArrayList<>();
final boolean fallback;
Capture(boolean fallback) throws Exception {
super(UrlParser.parse("jdbc:oceanbase://localhost:1/test"), new GlobalStateInfo(), new ReentrantLock(), null);
this.fallback = fallback;
connected = true;
}
@Override
public boolean isOracleMode() { return true; }
@Override
public boolean versionGreaterOrEqual(int major, int minor, int patch) { return false; }
@Override
public void executeQuery(String sql) throws SQLException {
sent.add(sql);
if (fallback && sql.contains("_ORACLE_SQL_SELECT_LIMIT")) throw new SQLException("unsupported variable fixture");
}
@Override
public void executeQuery(boolean master, Results results, String sql) throws SQLException {
sent.add(sql);
throw new SQLException("STOP_BEFORE_NETWORK", "ZZZZZ");
}
}
}
+1
View File
@@ -539,6 +539,7 @@ export interface QueryPaginationExecutionPlan {
exactQueryRowBound?: number; exactQueryRowBound?: number;
useAgentResultSession: boolean; useAgentResultSession: boolean;
paginationRowNumberColumn?: string; paginationRowNumberColumn?: string;
paginationError?: string;
} }
export type QuerySortDirection = "asc" | "desc"; export type QuerySortDirection = "asc" | "desc";
@@ -1039,12 +1039,12 @@ describe("queryStore hidden primary key editing", () => {
expect(store.tabs.find((tab) => tab.id === tabId)?.result?.large_value_cells).toEqual([{ row_index: 0, column_index: 1, original_bytes: 81920 }]); expect(store.tabs.find((tab) => tab.id === tabId)?.result?.large_value_cells).toEqual([{ row_index: 0, column_index: 1, original_bytes: 81920 }]);
}); });
it("continues the Oracle cursor for page 2 in a manual transaction", async () => { it.each(["oracle", "oceanbase-oracle"])("continues the %s cursor for page 2 in a manual transaction", async (databaseType) => {
getConnectionConfig.mockReturnValue({ id: "oracle-1", name: "Oracle", db_type: "oracle", database: "ORCL", query_timeout_secs: 30 }); getConnectionConfig.mockReturnValue({ id: "oracle-1", name: "Oracle", db_type: databaseType, database: "ORCL", query_timeout_secs: 30 });
analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "complex-query" }); analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "complex-query" });
prepareQueryPaginationExecutionPlan.mockImplementation(async (options) => ({ prepareQueryPaginationExecutionPlan.mockImplementation(async (options) => ({
sqlToExecute: options.sql, sqlToExecute: options.sql,
pageSql: options.sql, pageSql: databaseType === "oceanbase-oracle" ? undefined : options.sql,
pageLimit: options.pagination.limit, pageLimit: options.pagination.limit,
pageOffset: options.pagination.offset, pageOffset: options.pagination.offset,
countSql: undefined, countSql: undefined,
@@ -2049,6 +2049,94 @@ describe("queryStore hidden primary key editing", () => {
); );
}); });
it("advances an OceanBase duplicate projection cursor through an offset jump and next page", async () => {
getConnectionConfig.mockReturnValue({ id: "ob-1", name: "OceanBase", db_type: "oceanbase-oracle", database: "app", query_timeout_secs: 30 });
analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "complex-query" });
prepareQueryPaginationExecutionPlan.mockImplementation(async (options) => ({
sqlToExecute: options.sql,
pageSql: undefined,
pageLimit: options.pagination.limit,
pageOffset: options.pagination.offset,
countSql: undefined,
useAgentResultSession: true,
}));
let row = 0;
executeMulti.mockImplementation(async () => [
{
columns: ["NAME", "NAME"],
rows: Array.from({ length: 2 }, () => [++row, row]),
affected_rows: 0,
execution_time_ms: 1,
session_id: "ob-cursor",
has_more: row < 8,
},
]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("ob-1", "app", "Query");
const sql = "SELECT a.name, a.* FROM people a ORDER BY a.id";
await store.executeTabSql(tabId, sql, { pagination: { limit: 2, offset: 4 } });
const tab = store.tabs.find((item) => item.id === tabId)!;
expect(tab.result?.rows).toEqual([
[5, 5],
[6, 6],
]);
expect(executeMulti).toHaveBeenCalledTimes(3);
expect(executeMulti.mock.calls[0]![5]).toEqual(expect.objectContaining({ pageSize: 2, fetchSize: 2, resultSessionId: undefined }));
expect(executeMulti.mock.calls[0]![5].maxRows).toBeGreaterThan(2);
expect(executeMulti.mock.calls[1]![5].resultSessionId).toBe("ob-cursor");
await store.executeTabSql(tabId, sql, { pagination: { limit: 2, offset: 6, sessionId: "ob-cursor" } });
expect(executeMulti).toHaveBeenCalledTimes(4);
expect(executeMulti.mock.calls[3]![2]).toBe(sql);
expect(tab.result?.rows).toEqual([
[7, 7],
[8, 8],
]);
expect(tab.result?.columns).toEqual(["NAME", "NAME"]);
});
it("does not execute an OceanBase offset plan rejected without a cursor", async () => {
getConnectionConfig.mockReturnValue({ id: "ob-1", name: "OceanBase", db_type: "oceanbase-oracle", database: "app", query_timeout_secs: 30 });
analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "complex-query" });
prepareQueryPaginationExecutionPlan.mockImplementation(async () => ({
sqlToExecute: "",
pageSql: undefined,
pageLimit: undefined,
pageOffset: undefined,
countSql: undefined,
useAgentResultSession: false,
paginationError: "This query requires an Agent result session for offset pagination",
}));
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("ob-1", "app", "Query");
await store.executeTabSql(tabId, "SELECT name, name FROM people", { pagination: { limit: 2, offset: 4 } });
expect(JSON.stringify(store.tabs.find((item) => item.id === tabId)?.result)).toContain("requires an Agent result session");
expect(executeMulti).not.toHaveBeenCalled();
expect(executeInManualTransaction).not.toHaveBeenCalled();
});
it("rejects a fresh OceanBase manual-transaction offset instead of repeating the first page", async () => {
getConnectionConfig.mockReturnValue({ id: "ob-1", name: "OceanBase", db_type: "oceanbase-oracle", database: "app", query_timeout_secs: 30 });
analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "complex-query" });
prepareQueryPaginationExecutionPlan.mockImplementation(async (options) => ({
sqlToExecute: options.sql,
pageSql: undefined,
pageLimit: options.pagination.limit,
pageOffset: options.pagination.offset,
countSql: undefined,
useAgentResultSession: true,
}));
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("ob-1", "app", "Query");
store.setAutoCommit(tabId, false);
await store.executeTabSql(tabId, "SELECT name, name FROM people", { pagination: { limit: 2, offset: 4 } });
expect(JSON.stringify(store.tabs.find((item) => item.id === tabId)?.result)).toContain("requires an existing result session");
expect(executeMulti).not.toHaveBeenCalled();
expect(executeInManualTransaction).not.toHaveBeenCalled();
});
it("stops appending when a SQL Server query has no bounded next-page plan", async () => { it("stops appending when a SQL Server query has no bounded next-page plan", async () => {
getConnectionConfig.mockReturnValue({ id: "sqlserver-1", name: "SQL Server", db_type: "sqlserver", database: "app", query_timeout_secs: 30 }); getConnectionConfig.mockReturnValue({ id: "sqlserver-1", name: "SQL Server", db_type: "sqlserver", database: "app", query_timeout_secs: 30 });
analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "complex-query" }); analyzeEditableQueryEditability.mockResolvedValue({ editable: false, reason: "complex-query" });
+4
View File
@@ -7710,6 +7710,10 @@ export const useQueryStore = defineStore("query", () => {
firstPageUsesActualSql: hiddenPrimaryKeys.length > 0, firstPageUsesActualSql: hiddenPrimaryKeys.length > 0,
}); });
const canPaginateSqlServerUseScript = !!sqlServerUseScript && !!plan.pageSql && typeof plan.pageLimit === "number" && typeof plan.pageOffset === "number"; const canPaginateSqlServerUseScript = !!sqlServerUseScript && !!plan.pageSql && typeof plan.pageLimit === "number" && typeof plan.pageOffset === "number";
if (plan.paginationError) throw new Error(plan.paginationError);
if (effectiveDbType === "oceanbase-oracle" && tab.autoCommit === false && plan.useAgentResultSession && !plan.pageSql && pagination.offset > 0 && !pagination.sessionId) {
throw new Error("This query requires an existing result session for offset pagination in a manual transaction");
}
if (sqlServerUseScript && !canPaginateSqlServerUseScript) { if (sqlServerUseScript && !canPaginateSqlServerUseScript) {
sqlToExecute = sqlBeforePagination; sqlToExecute = sqlBeforePagination;
} else { } else {
@@ -999,6 +999,9 @@ async fn export_query_result_core_inner(
use_agent_cursor: request.use_agent_cursor, use_agent_cursor: request.use_agent_cursor,
first_page_uses_actual_sql: true, first_page_uses_actual_sql: true,
}); });
if let Some(error) = plan.pagination_error {
return Err(error);
}
let Some(plan_limit) = plan.page_limit else { let Some(plan_limit) = plan.page_limit else {
return Err("Failed to build query pagination plan for export".to_string()); return Err("Failed to build query pagination plan for export".to_string());
}; };
@@ -2598,6 +2601,24 @@ mod tests {
assert_eq!(streaming_pagination_preflight_error(&req, 100, false, false), None); assert_eq!(streaming_pagination_preflight_error(&req, 100, false, false), None);
} }
#[test]
fn oceanbase_duplicate_projection_export_requires_cursor_and_follows_its_end() {
let mut req = request("csv", None, None);
req.database_type = DatabaseType::OceanbaseOracle;
req.sql = "SELECT a.name, a.* FROM people a ORDER BY a.id".into();
req.query_base_sql = req.sql.clone();
req.use_agent_cursor = true;
assert!(!supports_streaming_offset_pagination(&req, 100));
assert_eq!(streaming_pagination_preflight_error(&req, 100, false, false), None);
assert!(should_fetch_next_page(true, true, 100, 100, 100));
assert!(!should_fetch_next_page(true, false, 100, 100, 100));
req.use_agent_cursor = false;
assert_eq!(
streaming_pagination_preflight_error(&req, 100, false, false),
Some(STREAMING_PAGINATION_UNSUPPORTED_ERROR)
);
}
#[test] #[test]
fn csv_unlimited_export_has_no_effective_row_limit() { fn csv_unlimited_export_has_no_effective_row_limit() {
assert_eq!(effective_row_limit(&request("csv", None, None)), None); assert_eq!(effective_row_limit(&request("csv", None, None)), None);
+266 -12
View File
@@ -63,6 +63,8 @@ pub struct QueryPaginationExecutionPlan {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub exact_query_row_bound: Option<usize>, pub exact_query_row_bound: Option<usize>,
pub use_agent_result_session: bool, pub use_agent_result_session: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pagination_error: Option<String>,
/// Trailing helper column added by DBX's ROWNUM pagination wrapper. /// Trailing helper column added by DBX's ROWNUM pagination wrapper.
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub pagination_row_number_column: Option<String>, pub pagination_row_number_column: Option<String>,
@@ -152,6 +154,7 @@ pub fn build_query_pagination_execution_plan(
count_sql: None, count_sql: None,
exact_query_row_bound, exact_query_row_bound,
use_agent_result_session: false, use_agent_result_session: false,
pagination_error: None,
pagination_row_number_column: None, pagination_row_number_column: None,
single_execution: false, single_execution: false,
}; };
@@ -175,19 +178,21 @@ pub fn build_query_pagination_execution_plan(
return plan; return plan;
} }
if sql_server_cte { if options.database_type == Some(DatabaseType::OceanbaseOracle)
&& (!rownum_wrapper_projection_is_safe(&options.sql) || has_top_level_rownum(&options.sql))
{
if options.use_agent_cursor {
plan.page_limit = Some(options.pagination.limit.max(1));
plan.page_offset = Some(options.pagination.offset);
plan.use_agent_result_session = true;
} else if options.pagination.offset > 0 {
plan.pagination_error = Some("This query requires an Agent result session for offset pagination".into());
plan.sql_to_execute.clear();
}
return plan; return plan;
} }
// Existing top-level ROWNUM predicates/projections cannot be wrapped without if sql_server_cte {
// changing their semantics. Keep one cursor so later pages advance it.
if options.database_type == Some(DatabaseType::OceanbaseOracle)
&& options.use_agent_cursor
&& has_top_level_rownum(&options.sql)
{
plan.page_limit = Some(options.pagination.limit);
plan.page_offset = Some(options.pagination.offset);
plan.use_agent_result_session = true;
return plan; return plan;
} }
@@ -309,7 +314,14 @@ pub fn build_paginated_query_sql(options: PaginatedQuerySqlOptions) -> QuerySqlB
TablePaginationStrategy::Db2FetchFirst | TablePaginationStrategy::FetchFirst => { TablePaginationStrategy::Db2FetchFirst | TablePaginationStrategy::FetchFirst => {
ok(add_fetch_first_limit(&statement, safe_limit, safe_offset)) ok(add_fetch_first_limit(&statement, safe_limit, safe_offset))
} }
TablePaginationStrategy::Rownum => ok(add_rownum_limit(&statement, safe_limit, safe_offset)), TablePaginationStrategy::Rownum => {
if options.database_type == Some(DatabaseType::OceanbaseOracle)
&& !rownum_wrapper_projection_is_safe(&statement)
{
return err("unsupported");
}
ok(add_rownum_limit(&statement, safe_limit, safe_offset))
}
TablePaginationStrategy::AgentMaxRows | TablePaginationStrategy::Unbounded => ok(format!("{statement};")), TablePaginationStrategy::AgentMaxRows | TablePaginationStrategy::Unbounded => ok(format!("{statement};")),
TablePaginationStrategy::IrisTop => ok(add_iris_top_limit(&statement, safe_limit)), TablePaginationStrategy::IrisTop => ok(add_iris_top_limit(&statement, safe_limit)),
TablePaginationStrategy::LimitOffset => { TablePaginationStrategy::LimitOffset => {
@@ -454,6 +466,29 @@ pub fn build_sorted_query_sql(options: SortedQuerySqlOptions) -> QuerySqlBuildRe
return err("not_select"); return err("not_select");
} }
if options.database_type == Some(DatabaseType::OceanbaseOracle) && !rownum_wrapper_projection_is_safe(statement) {
let Ok(statements) = Parser::parse_sql(&GenericDialect {}, statement) else {
return err("unsupported");
};
let [Statement::Query(query)] = statements.as_slice() else {
return err("unsupported");
};
if query.limit_clause.is_some()
|| query.fetch.is_some()
|| !query.locks.is_empty()
|| has_top_level_rownum(statement)
|| has_top_level_top(statement)
|| options.column_index >= options.result_columns.len()
{
return err("unsupported");
}
let end = find_top_level_trailing_order_by(statement).unwrap_or(statement.len());
return ok(append_sql_suffix(
statement[..end].trim_end(),
&format!("ORDER BY {} {};", options.column_index + 1, options.direction.as_sql()),
));
}
let uses_hive_subquery_syntax = let uses_hive_subquery_syntax =
matches!(options.database_type, Some(DatabaseType::Argo | DatabaseType::Hive | DatabaseType::Impala)); matches!(options.database_type, Some(DatabaseType::Argo | DatabaseType::Hive | DatabaseType::Impala));
if uses_hive_subquery_syntax { if uses_hive_subquery_syntax {
@@ -1908,6 +1943,52 @@ fn add_firebird_rows_limit(statement: &str, limit: usize, offset: usize) -> Stri
append_sql_suffix(statement, &format!("{rows};")) append_sql_suffix(statement, &format!("{rows};"))
} }
fn rownum_wrapper_projection_is_safe(statement: &str) -> bool {
let Ok(statements) = Parser::parse_sql(&GenericDialect {}, statement) else {
return false;
};
let [Statement::Query(query)] = statements.as_slice() else {
return false;
};
fn safe_projection(body: &SetExpr, allow_wildcard: bool) -> bool {
match body {
SetExpr::Query(query) => safe_projection(&query.body, allow_wildcard && query.with.is_none()),
SetExpr::SetOperation { left, .. } => safe_projection(left, allow_wildcard),
SetExpr::Select(select) => {
let mut names = HashSet::new();
select.projection.iter().all(|item| {
let name = match item {
SelectItem::ExprWithAlias { alias, .. } => alias,
SelectItem::UnnamedExpr(Expr::Identifier(name)) => name,
SelectItem::UnnamedExpr(Expr::CompoundIdentifier(parts)) => {
let Some(name) = parts.last() else {
return false;
};
name
}
SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _) => {
return allow_wildcard
&& select.projection.len() == 1
&& select.from.len() == 1
&& select.from[0].joins.is_empty()
&& matches!(&select.from[0].relation, TableFactor::Table { args: None, alias, .. }
if alias.as_ref().is_none_or(|alias| alias.columns.is_empty()));
}
_ => return select.projection.len() == 1,
};
names.insert(if name.quote_style.is_some() {
name.value.clone()
} else {
name.value.to_uppercase()
})
})
}
_ => false,
}
}
safe_projection(&query.body, query.with.is_none())
}
fn add_rownum_limit(statement: &str, limit: usize, offset: usize) -> String { fn add_rownum_limit(statement: &str, limit: usize, offset: usize) -> String {
if has_top_level_rownum(statement) { if has_top_level_rownum(statement) {
return format!("{statement};"); return format!("{statement};");
@@ -1915,7 +1996,7 @@ fn add_rownum_limit(statement: &str, limit: usize, offset: usize) -> String {
if offset == 0 { if offset == 0 {
return derived_table_sql("SELECT * FROM", statement, &format!("WHERE ROWNUM <= {limit};")); return derived_table_sql("SELECT * FROM", statement, &format!("WHERE ROWNUM <= {limit};"));
} }
let end = offset + limit; let end = offset.saturating_add(limit);
let column = rownum_pagination_column_name(statement); let column = rownum_pagination_column_name(statement);
let inner = derived_table_sql( let inner = derived_table_sql(
&format!("SELECT dbx_inner.*, ROWNUM AS \"{column}\" FROM"), &format!("SELECT dbx_inner.*, ROWNUM AS \"{column}\" FROM"),
@@ -2473,6 +2554,179 @@ fn fallback_alias(index: usize) -> String {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
fn oceanbase_plan(sql: &str, limit: usize, offset: usize, cursor: bool) -> QueryPaginationExecutionPlan {
build_query_pagination_execution_plan(QueryPaginationExecutionPlanOptions {
sql: sql.into(),
query_base_sql: sql.into(),
database_type: Some(DatabaseType::OceanbaseOracle),
pagination: QueryPagination { limit, offset, session_id: None },
use_agent_cursor: cursor,
first_page_uses_actual_sql: true,
})
}
#[test]
fn oceanbase_cursor_fallback_preserves_limits_and_rejects_unavailable_offsets() {
for sql in [
"SELECT a.name, a.* FROM people a ORDER BY a.id",
"SELECT name, name FROM people WHERE ROWNUM <= 9",
"SELECT name, name FROM people ORDER BY name FETCH FIRST 9 ROWS ONLY",
"SELECT * FROM people WHERE ROWNUM <= 9",
"SELECT * FROM (SELECT name, name FROM people) p",
"SELECT a.id AS label, a.name AS \"LABEL\" FROM people a",
"SELECT a.name || a.name, a.name || a.name FROM people a",
] {
for limit in [0, 1, 100, i32::MAX as usize] {
for offset in [0, 1, 100, 500] {
let plan = oceanbase_plan(sql, limit, offset, true);
assert_eq!(plan.sql_to_execute, sql);
assert!(plan.use_agent_result_session, "{sql}");
assert_eq!(plan.page_limit, Some(limit.max(1)));
assert_eq!(plan.page_offset, Some(offset));
assert!(plan.pagination_error.is_none());
let plan = oceanbase_plan(sql, limit, offset, false);
assert!(!plan.use_agent_result_session);
assert!(plan.page_sql.is_none());
if offset == 0 {
assert_eq!(plan.sql_to_execute, sql);
assert!(plan.pagination_error.is_none());
} else {
assert!(plan.pagination_error.is_some());
assert!(plan.sql_to_execute.is_empty());
}
}
}
}
}
#[test]
fn oceanbase_safe_projections_keep_bounded_sql_at_every_offset() {
for sql in [
"SELECT * FROM people",
"SELECT a.* FROM people a",
"SELECT a.id, a.name FROM people a",
"SELECT a.id AS left_id, b.id AS right_id FROM people a JOIN people b ON a.id=b.id",
"SELECT a.id AS label, a.name AS \"label\" FROM people a",
"SELECT name AS label, name AS other_label FROM people ORDER BY name FETCH FIRST 9 ROWS ONLY",
"SELECT name AS label, COUNT(*) AS n FROM people GROUP BY name",
] {
for cursor in [false, true] {
for limit in [0, 1, 100, i32::MAX as usize] {
for offset in [0, 100, 500] {
let plan = oceanbase_plan(sql, limit, offset, cursor);
assert!(!plan.use_agent_result_session, "{sql}");
assert!(plan.pagination_error.is_none());
assert!(plan.sql_to_execute.contains(sql));
assert!(plan.sql_to_execute.contains(&format!("ROWNUM <= {}", offset + limit.max(1))));
assert_eq!(plan.pagination_row_number_column.is_some(), offset > 0);
}
}
}
}
}
#[test]
fn oceanbase_existing_session_wins_over_projection_and_cursor_availability() {
let sql = "SELECT a.name, a.* FROM people a";
let plan = build_query_pagination_execution_plan(QueryPaginationExecutionPlanOptions {
sql: sql.into(),
query_base_sql: sql.into(),
database_type: Some(DatabaseType::OceanbaseOracle),
pagination: QueryPagination { limit: 10, offset: 50, session_id: Some("cursor".into()) },
use_agent_cursor: false,
first_page_uses_actual_sql: false,
});
assert!(plan.use_agent_result_session);
assert_eq!(plan.sql_to_execute, sql);
assert_eq!(plan.page_offset, Some(50));
assert!(plan.pagination_error.is_none());
}
#[test]
fn oceanbase_duplicate_projection_sort_preserves_text_and_count_preserves_cardinality() {
for sql in [
"SELECT /*+ FULL(a) */ a.name, a.* FROM people a ORDER BY a.id",
"SELECT DISTINCT name, name FROM people ORDER BY name",
"SELECT name, name, COUNT(*) FROM people GROUP BY name ORDER BY name",
"SELECT name, name FROM people UNION ALL SELECT name, name FROM old_people ORDER BY 1",
] {
let sorted = build_sorted_query_sql(SortedQuerySqlOptions {
original_sql: sql.into(),
database_type: Some(DatabaseType::OceanbaseOracle),
result_columns: vec!["NAME".into(), "NAME".into()],
column_index: 1,
column: "NAME".into(),
direction: QuerySortDirection::Desc,
});
assert!(sorted.ok, "{sql}");
let sorted = sorted.sql.unwrap();
assert_eq!(sorted, format!("{} ORDER BY 2 DESC;", &sql[..sql.rfind(" ORDER BY").unwrap()]));
assert!(oceanbase_plan(&sorted, 10, 0, true).use_agent_result_session);
let count = build_count_query_sql(CountQuerySqlOptions {
original_sql: sql.into(),
database_type: Some(DatabaseType::OceanbaseOracle),
});
assert!(count.ok);
let count = count.sql.unwrap();
assert!(count.starts_with("SELECT COUNT(*) AS dbx_total_rows FROM ("));
assert!(count.contains(sql));
}
}
#[test]
fn oceanbase_sort_does_not_move_ordering_across_user_limits_or_locks() {
for suffix in ["WHERE ROWNUM <= 9", "ORDER BY name FETCH FIRST 9 ROWS ONLY", "FOR UPDATE"] {
let sorted = build_sorted_query_sql(SortedQuerySqlOptions {
original_sql: format!("SELECT name, name FROM people {suffix}"),
database_type: Some(DatabaseType::OceanbaseOracle),
result_columns: vec!["NAME".into(), "NAME".into()],
column_index: 0,
column: "NAME".into(),
direction: QuerySortDirection::Asc,
});
assert!(!sorted.ok);
}
}
#[test]
fn oceanbase_unsafe_projection_uses_cursor_at_every_offset() {
for sql in [
"SELECT a.name, a.* FROM people a ORDER BY a.id",
"SELECT a.name, a.name FROM people a",
"SELECT a.id, b.id FROM people a JOIN people b ON a.id = b.id",
"SELECT * FROM people a JOIN people b ON a.id = b.id",
"SELECT a.*, b.* FROM people a JOIN people b ON a.id = b.id",
"SELECT a.id AS label, a.name AS LABEL FROM people a",
"SELECT DISTINCT a.name, a.name FROM people a",
"SELECT a.name, a.name, COUNT(*) FROM people a GROUP BY a.name",
"WITH p AS (SELECT * FROM people) SELECT p.name, p.* FROM p",
"SELECT name, name FROM people UNION ALL SELECT name, name FROM people",
] {
for offset in [0, 100, 500] {
let plan = super::build_query_pagination_execution_plan(super::QueryPaginationExecutionPlanOptions {
sql: sql.into(),
query_base_sql: sql.into(),
database_type: Some(super::DatabaseType::OceanbaseOracle),
pagination: super::QueryPagination { limit: 100, offset, session_id: None },
use_agent_cursor: true,
first_page_uses_actual_sql: true,
});
assert!(plan.use_agent_result_session, "{sql} offset {offset}");
assert_eq!(plan.sql_to_execute, sql);
assert_eq!(plan.page_offset, Some(offset));
assert!(plan.pagination_row_number_column.is_none());
assert!(plan.page_sql.is_none());
let rewritten = super::build_paginated_query_sql(super::PaginatedQuerySqlOptions {
original_sql: sql.into(),
database_type: Some(super::DatabaseType::OceanbaseOracle),
limit: 100,
offset,
});
assert!(!rewritten.ok, "{sql}");
}
}
}
use super::*; use super::*;
/// Query shape from issue #7832: a MySQL GROUP BY over a LEFT JOIN with an /// Query shape from issue #7832: a MySQL GROUP BY over a LEFT JOIN with an