fix(db2): defer LOB loading in query results

Refs: #10723
This commit is contained in:
t8y2 committed 2026-10-03 23:55:40 +08:00
1 parent aa2fe831b6
commit da1094df93
11 files changed
+505 -35

No files matched your search

@@ -6,19 +6,25 @@ public final class ExecuteQueryOptions {
private int maxRows; private int maxRows;
private Integer fetchSize; private Integer fetchSize;
private int timeoutSecs; private int timeoutSecs;
private boolean deferLobs;
public ExecuteQueryOptions() { public ExecuteQueryOptions() {
this(JdbcExecutor.DEFAULT_MAX_ROWS, null, 0); this(JdbcExecutor.DEFAULT_MAX_ROWS, null, 0, false);
} }
public ExecuteQueryOptions(int maxRows, Integer fetchSize) { public ExecuteQueryOptions(int maxRows, Integer fetchSize) {
this(maxRows, fetchSize, 0); this(maxRows, fetchSize, 0, false);
} }
public ExecuteQueryOptions(int maxRows, Integer fetchSize, int timeoutSecs) { public ExecuteQueryOptions(int maxRows, Integer fetchSize, int timeoutSecs) {
this(maxRows, fetchSize, timeoutSecs, false);
}
public ExecuteQueryOptions(int maxRows, Integer fetchSize, int timeoutSecs, boolean deferLobs) {
this.maxRows = maxRows; this.maxRows = maxRows;
this.fetchSize = fetchSize; this.fetchSize = fetchSize;
this.timeoutSecs = timeoutSecs; this.timeoutSecs = timeoutSecs;
this.deferLobs = deferLobs;
} }
public int getMaxRows() { public int getMaxRows() {
@@ -33,6 +39,10 @@ public final class ExecuteQueryOptions {
return timeoutSecs; return timeoutSecs;
} }
public boolean getDeferLobs() {
return deferLobs;
}
public void setMaxRows(int maxRows) { public void setMaxRows(int maxRows) {
this.maxRows = maxRows; this.maxRows = maxRows;
} }
@@ -45,6 +55,10 @@ public final class ExecuteQueryOptions {
this.timeoutSecs = timeoutSecs; this.timeoutSecs = timeoutSecs;
} }
public void setDeferLobs(boolean deferLobs) {
this.deferLobs = deferLobs;
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (this == other) return true; if (this == other) return true;
@@ -52,16 +66,17 @@ public final class ExecuteQueryOptions {
ExecuteQueryOptions that = (ExecuteQueryOptions) other; ExecuteQueryOptions that = (ExecuteQueryOptions) other;
return maxRows == that.maxRows return maxRows == that.maxRows
&& timeoutSecs == that.timeoutSecs && timeoutSecs == that.timeoutSecs
&& deferLobs == that.deferLobs
&& Objects.equals(fetchSize, that.fetchSize); && Objects.equals(fetchSize, that.fetchSize);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(maxRows, fetchSize, timeoutSecs); return Objects.hash(maxRows, fetchSize, timeoutSecs, deferLobs);
} }
@Override @Override
public String toString() { public String toString() {
return "ExecuteQueryOptions(maxRows=" + maxRows + ", fetchSize=" + fetchSize + ", timeoutSecs=" + timeoutSecs + ")"; return "ExecuteQueryOptions(maxRows=" + maxRows + ", fetchSize=" + fetchSize + ", timeoutSecs=" + timeoutSecs + ", deferLobs=" + deferLobs + ")";
} }
} }
@@ -294,7 +294,8 @@ public final class JsonRpcServer {
new ExecuteQueryOptions( new ExecuteQueryOptions(
intOrDefault(params, "maxRows", JdbcExecutor.DEFAULT_MAX_ROWS), intOrDefault(params, "maxRows", JdbcExecutor.DEFAULT_MAX_ROWS),
intOrNull(params, "fetchSize"), intOrNull(params, "fetchSize"),
intOrDefault(params, "timeoutSecs", 0) intOrDefault(params, "timeoutSecs", 0),
booleanOrDefault(params, "deferLobs", false)
) )
); );
} }
@@ -306,7 +307,8 @@ public final class JsonRpcServer {
intOrDefault(params, "pageSize", 100), intOrDefault(params, "pageSize", 100),
intOrNull(params, "fetchSize"), intOrNull(params, "fetchSize"),
intOrDefault(params, "maxRows", JdbcExecutor.DEFAULT_MAX_ROWS), intOrDefault(params, "maxRows", JdbcExecutor.DEFAULT_MAX_ROWS),
intOrDefault(params, "timeoutSecs", 0) intOrDefault(params, "timeoutSecs", 0),
booleanOrDefault(params, "deferLobs", false)
) )
); );
} }
@@ -327,7 +329,8 @@ public final class JsonRpcServer {
intOrDefault(params, "pageSize", 100), intOrDefault(params, "pageSize", 100),
intOrNull(params, "fetchSize"), intOrNull(params, "fetchSize"),
intOrDefault(params, "maxRows", JdbcExecutor.DEFAULT_MAX_ROWS), intOrDefault(params, "maxRows", JdbcExecutor.DEFAULT_MAX_ROWS),
intOrDefault(params, "timeoutSecs", 0) intOrDefault(params, "timeoutSecs", 0),
booleanOrDefault(params, "deferLobs", false)
) )
); );
} }
@@ -556,6 +559,11 @@ public final class JsonRpcServer {
return element.getAsInt(); return element.getAsInt();
} }
private static boolean booleanOrDefault(JsonObject object, String key, boolean defaultValue) {
JsonElement element = object.get(key);
return element == null || element instanceof JsonNull ? defaultValue : element.getAsBoolean();
}
private MetadataListConstraints metadataListConstraints(JsonObject params) { private MetadataListConstraints metadataListConstraints(JsonObject params) {
return new MetadataListConstraints( return new MetadataListConstraints(
stringOrNull(params, "filter"), stringOrNull(params, "filter"),
@@ -7,20 +7,26 @@ public final class QueryPageOptions {
private Integer fetchSize; private Integer fetchSize;
private int maxRows; private int maxRows;
private int timeoutSecs; private int timeoutSecs;
private boolean deferLobs;
public QueryPageOptions() { public QueryPageOptions() {
this(100, null, JdbcExecutor.DEFAULT_MAX_ROWS, 0); this(100, null, JdbcExecutor.DEFAULT_MAX_ROWS, 0, false);
} }
public QueryPageOptions(int pageSize, Integer fetchSize, int maxRows) { public QueryPageOptions(int pageSize, Integer fetchSize, int maxRows) {
this(pageSize, fetchSize, maxRows, 0); this(pageSize, fetchSize, maxRows, 0, false);
} }
public QueryPageOptions(int pageSize, Integer fetchSize, int maxRows, int timeoutSecs) { public QueryPageOptions(int pageSize, Integer fetchSize, int maxRows, int timeoutSecs) {
this(pageSize, fetchSize, maxRows, timeoutSecs, false);
}
public QueryPageOptions(int pageSize, Integer fetchSize, int maxRows, int timeoutSecs, boolean deferLobs) {
this.pageSize = pageSize; this.pageSize = pageSize;
this.fetchSize = fetchSize; this.fetchSize = fetchSize;
this.maxRows = maxRows; this.maxRows = maxRows;
this.timeoutSecs = timeoutSecs; this.timeoutSecs = timeoutSecs;
this.deferLobs = deferLobs;
} }
public int getPageSize() { public int getPageSize() {
@@ -39,6 +45,10 @@ public final class QueryPageOptions {
return timeoutSecs; return timeoutSecs;
} }
public boolean getDeferLobs() {
return deferLobs;
}
public void setPageSize(int pageSize) { public void setPageSize(int pageSize) {
this.pageSize = pageSize; this.pageSize = pageSize;
} }
@@ -55,6 +65,10 @@ public final class QueryPageOptions {
this.timeoutSecs = timeoutSecs; this.timeoutSecs = timeoutSecs;
} }
public void setDeferLobs(boolean deferLobs) {
this.deferLobs = deferLobs;
}
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (this == other) return true; if (this == other) return true;
@@ -63,12 +77,13 @@ public final class QueryPageOptions {
return pageSize == that.pageSize return pageSize == that.pageSize
&& maxRows == that.maxRows && maxRows == that.maxRows
&& timeoutSecs == that.timeoutSecs && timeoutSecs == that.timeoutSecs
&& deferLobs == that.deferLobs
&& Objects.equals(fetchSize, that.fetchSize); && Objects.equals(fetchSize, that.fetchSize);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(pageSize, fetchSize, maxRows, timeoutSecs); return Objects.hash(pageSize, fetchSize, maxRows, timeoutSecs, deferLobs);
} }
@Override @Override
@@ -77,6 +92,7 @@ public final class QueryPageOptions {
+ ", fetchSize=" + fetchSize + ", fetchSize=" + fetchSize
+ ", maxRows=" + maxRows + ", maxRows=" + maxRows
+ ", timeoutSecs=" + timeoutSecs + ", timeoutSecs=" + timeoutSecs
+ ", deferLobs=" + deferLobs
+ ")"; + ")";
} }
} }
@@ -436,6 +436,19 @@ class CommonJavaCompatibilityTest {
assertTrue(row.get(2).getAsJsonPrimitive().isNumber()); assertTrue(row.get(2).getAsJsonPrimitive().isNumber());
} }
@Test
void jsonRpcServerDispatchesDeferredLobOption() {
MinimalAgent agent = new MinimalAgent();
JsonRpcServer server = new JsonRpcServer(agent);
String response = server.handleRequest(
"{\"jsonrpc\":\"2.0\",\"id\":9,\"method\":\"" + AgentProtocol.METHOD_EXECUTE_QUERY + "\",\"params\":{\"sql\":\"select payload from documents\",\"deferLobs\":true}}"
);
assertTrue(JsonParser.parseString(response).getAsJsonObject().has("result"));
assertTrue(agent.lastExecuteOptions.getDeferLobs());
}
@Test @Test
void jsonRpcServerReconnectsWhenStoredJdbcConnectionIsStale() { void jsonRpcServerReconnectsWhenStoredJdbcConnectionIsStale() {
ReconnectingAgent agent = new ReconnectingAgent(); ReconnectingAgent agent = new ReconnectingAgent();
@@ -524,14 +537,14 @@ class CommonJavaCompatibilityTest {
JsonRpcServer server = new JsonRpcServer(agent); JsonRpcServer server = new JsonRpcServer(agent);
String startResponse = server.handleRequest( String startResponse = server.handleRequest(
"{\"jsonrpc\":\"2.0\",\"id\":11,\"method\":\"" + AgentProtocol.METHOD_START_TABLE_READ + "\",\"params\":{\"sql\":\"select * from orders\",\"schema\":\"public\",\"pageSize\":2,\"fetchSize\":8,\"maxRows\":20,\"timeoutSecs\":3}}" "{\"jsonrpc\":\"2.0\",\"id\":11,\"method\":\"" + AgentProtocol.METHOD_START_TABLE_READ + "\",\"params\":{\"sql\":\"select * from orders\",\"schema\":\"public\",\"pageSize\":2,\"fetchSize\":8,\"maxRows\":20,\"timeoutSecs\":3,\"deferLobs\":true}}"
); );
JsonObject startJson = JsonParser.parseString(startResponse).getAsJsonObject(); JsonObject startJson = JsonParser.parseString(startResponse).getAsJsonObject();
assertTrue(startJson.has("result")); assertTrue(startJson.has("result"));
assertEquals("select * from orders", agent.lastSql); assertEquals("select * from orders", agent.lastSql);
assertEquals("public", agent.lastSchema); assertEquals("public", agent.lastSchema);
assertEquals(new QueryPageOptions(2, 8, 20, 3), agent.lastOptions); assertEquals(new QueryPageOptions(2, 8, 20, 3, true), agent.lastOptions);
assertEquals("table-session", startJson.getAsJsonObject("result").get("session_id").getAsString()); assertEquals("table-session", startJson.getAsJsonObject("result").get("session_id").getAsString());
String fetchResponse = server.handleRequest( String fetchResponse = server.handleRequest(
@@ -586,7 +599,9 @@ class CommonJavaCompatibilityTest {
assertEquals(true, page.getHas_more()); assertEquals(true, page.getHas_more());
assertEquals(JdbcExecutor.DEFAULT_MAX_ROWS, new ExecuteQueryOptions().getMaxRows()); assertEquals(JdbcExecutor.DEFAULT_MAX_ROWS, new ExecuteQueryOptions().getMaxRows());
assertFalse(new ExecuteQueryOptions().getDeferLobs());
assertEquals(100, new QueryPageOptions().getPageSize()); assertEquals(100, new QueryPageOptions().getPageSize());
assertFalse(new QueryPageOptions().getDeferLobs());
assertNotNull(JdbcExecutor.INSTANCE); assertNotNull(JdbcExecutor.INSTANCE);
} }
@@ -885,6 +900,8 @@ class CommonJavaCompatibilityTest {
} }
private static class MinimalAgent implements DatabaseAgent { private static class MinimalAgent implements DatabaseAgent {
private ExecuteQueryOptions lastExecuteOptions;
@Override @Override
public void connect(ConnectParams params) { public void connect(ConnectParams params) {
} }
@@ -934,6 +951,7 @@ class CommonJavaCompatibilityTest {
@Override @Override
public QueryResult executeQuery(String sql, String schema, ExecuteQueryOptions options) { public QueryResult executeQuery(String sql, String schema, ExecuteQueryOptions options) {
lastExecuteOptions = options;
return new QueryResult(Collections.emptyList(), Collections.emptyList(), 0L, 0L); return new QueryResult(Collections.emptyList(), Collections.emptyList(), 0L, 0L);
} }
@@ -14,9 +14,13 @@ import com.dbx.agent.MetadataListConstraints;
import com.dbx.agent.MetadataSqlSupport; import com.dbx.agent.MetadataSqlSupport;
import com.dbx.agent.ObjectInfo; import com.dbx.agent.ObjectInfo;
import com.dbx.agent.ObjectSource; import com.dbx.agent.ObjectSource;
import com.dbx.agent.QueryPageOptions;
import com.dbx.agent.QueryPageResult;
import com.dbx.agent.QueryResult; import com.dbx.agent.QueryResult;
import com.dbx.agent.TableInfo; import com.dbx.agent.TableInfo;
import com.dbx.agent.TriggerInfo; import com.dbx.agent.TriggerInfo;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.Types; import java.sql.Types;
@@ -25,13 +29,18 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public final class Db2Agent extends AbstractJdbcAgent { public final class Db2Agent extends AbstractJdbcAgent {
private static final String LARGE_VALUE_BYTES_COLUMN_PREFIX = "__DBX_LARGE_VALUE_BYTES_";
private static final String DEFERRED_LOB_MARKER = "D:1";
private static final Set<String> NUMERIC_PRECISION_TYPES = Set.of( private static final Set<String> NUMERIC_PRECISION_TYPES = Set.of(
"DECIMAL", "NUMERIC", "INTEGER", "SMALLINT", "BIGINT", "REAL", "DOUBLE", "FLOAT" "DECIMAL", "NUMERIC", "INTEGER", "SMALLINT", "BIGINT", "REAL", "DOUBLE", "FLOAT"
); );
private static final Set<String> NUMERIC_SCALE_TYPES = Set.of("DECIMAL", "NUMERIC"); private static final Set<String> NUMERIC_SCALE_TYPES = Set.of("DECIMAL", "NUMERIC");
private static final Set<String> CHARACTER_LENGTH_TYPES = Set.of("VARCHAR", "CHAR", "CLOB", "GRAPHIC", "VARGRAPHIC"); private static final Set<String> CHARACTER_LENGTH_TYPES = Set.of("VARCHAR", "CHAR", "CLOB", "GRAPHIC", "VARGRAPHIC");
private final Set<String> deferredQuerySessions = ConcurrentHashMap.newKeySet();
private final Set<String> deferredTableReadSessions = ConcurrentHashMap.newKeySet();
@Override @Override
protected String driverClass() { protected String driverClass() {
@@ -55,6 +64,12 @@ public final class Db2Agent extends AbstractJdbcAgent {
return properties; return properties;
} }
@Override
protected void afterDisconnect() {
deferredQuerySessions.clear();
deferredTableReadSessions.clear();
}
@Override @Override
public List<DatabaseInfo> listDatabases() { public List<DatabaseInfo> listDatabases() {
return unchecked(() -> { return unchecked(() -> {
@@ -383,7 +398,11 @@ public final class Db2Agent extends AbstractJdbcAgent {
@Override @Override
public QueryResult executeQuery(String sql, String schema, ExecuteQueryOptions options) { public QueryResult executeQuery(String sql, String schema, ExecuteQueryOptions options) {
return JdbcExecutor.current().execute( boolean deferLobs = shouldDeferLobs(sql, options.getDeferLobs());
JdbcExecutor.ResultValueReader valueReader = deferLobs
? (JdbcExecutor.ColumnAwareResultValueReader) this::deferredResultValue
: this::resultValue;
QueryResult result = JdbcExecutor.current().execute(
requireConnected(), requireConnected(),
sql, sql,
schema, schema,
@@ -391,8 +410,72 @@ public final class Db2Agent extends AbstractJdbcAgent {
options.getMaxRows(), options.getMaxRows(),
options.getFetchSize(), options.getFetchSize(),
options.getTimeoutSecs(), options.getTimeoutSecs(),
this::resultValue valueReader
); );
return deferLobs ? addDeferredLobMarkers(result) : result;
}
@Override
public QueryPageResult executeQueryPage(String sql, String schema, QueryPageOptions options) {
boolean deferLobs = shouldDeferLobs(sql, options.getDeferLobs());
JdbcExecutor.ResultValueReader valueReader = deferLobs
? (JdbcExecutor.ColumnAwareResultValueReader) this::deferredResultValue
: this::resultValue;
QueryPageResult result = JdbcExecutor.current().executePage(
requireConnected(),
sql,
schema,
this::setSchemaSQL,
this::resetSchemaSQL,
options,
valueReader,
advancePastUpdateCounts()
);
rememberDeferredSession(deferredQuerySessions, result, deferLobs);
return deferLobs ? addDeferredLobMarkers(result) : result;
}
@Override
public QueryPageResult fetchQueryPage(String sessionId, int pageSize) {
QueryPageResult result = JdbcExecutor.current().fetchPage(sessionId, pageSize);
return finishDeferredPage(deferredQuerySessions, sessionId, result);
}
@Override
public boolean closeQuerySession(String sessionId) {
deferredQuerySessions.remove(sessionId);
return super.closeQuerySession(sessionId);
}
@Override
public QueryPageResult startTableRead(String sql, String schema, QueryPageOptions options) {
boolean deferLobs = shouldDeferLobs(sql, options.getDeferLobs());
JdbcExecutor.ResultValueReader valueReader = deferLobs
? (JdbcExecutor.ColumnAwareResultValueReader) this::deferredResultValue
: this::resultValue;
QueryPageResult result = JdbcExecutor.current().startTableRead(
requireConnected(),
sql,
schema,
this::setSchemaSQL,
this::resetSchemaSQL,
options,
valueReader
);
rememberDeferredSession(deferredTableReadSessions, result, deferLobs);
return deferLobs ? addDeferredLobMarkers(result) : result;
}
@Override
public QueryPageResult fetchTableReadPage(String sessionId, int pageSize) {
QueryPageResult result = JdbcExecutor.current().fetchTableReadPage(sessionId, pageSize);
return finishDeferredPage(deferredTableReadSessions, sessionId, result);
}
@Override
public boolean closeTableReadSession(String sessionId) {
deferredTableReadSessions.remove(sessionId);
return super.closeTableReadSession(sessionId);
} }
@Override @Override
@@ -411,6 +494,138 @@ public final class Db2Agent extends AbstractJdbcAgent {
}); });
} }
Object deferredResultValue(ResultSet rs, int index, int sqlType, String typeName) {
String lobType = deferredLobType(sqlType, typeName);
if (lobType == null) {
return resultValue(rs, index, sqlType);
}
return unchecked(() -> {
if ("BLOB".equals(lobType)) {
Blob value = rs.getBlob(index);
return value == null || rs.wasNull() ? null : "<BLOB>";
}
Clob value = rs.getClob(index);
return value == null || rs.wasNull() ? null : "<" + lobType + ">";
});
}
static QueryResult addDeferredLobMarkers(QueryResult result) {
DeferredLobResult deferred = deferredLobResult(result.getColumns(), result.getColumn_types(), result.getRows());
if (deferred == null) {
return result;
}
result.setColumns(deferred.columns());
result.setColumn_types(deferred.columnTypes());
result.setRows(deferred.rows());
return result;
}
static QueryPageResult addDeferredLobMarkers(QueryPageResult result) {
DeferredLobResult deferred = deferredLobResult(result.getColumns(), result.getColumn_types(), result.getRows());
if (deferred == null) {
return result;
}
result.setColumns(deferred.columns());
result.setColumn_types(deferred.columnTypes());
result.setRows(deferred.rows());
return result;
}
private static DeferredLobResult deferredLobResult(
List<String> columns,
List<String> columnTypes,
List<List<Object>> rows
) {
if (columns.size() != columnTypes.size()
|| columns.stream().anyMatch(column -> column.toUpperCase(Locale.ROOT).startsWith(LARGE_VALUE_BYTES_COLUMN_PREFIX))) {
return null;
}
List<String> deferredTypes = new ArrayList<>(columnTypes.size());
boolean hasDeferredLob = false;
for (String columnType : columnTypes) {
String lobType = deferredLobType(Types.OTHER, columnType);
deferredTypes.add(lobType);
hasDeferredLob |= lobType != null;
}
if (!hasDeferredLob) {
return null;
}
List<String> expandedColumns = new ArrayList<>(columns.size() + deferredTypes.size());
List<String> expandedColumnTypes = new ArrayList<>(columnTypes.size() + deferredTypes.size());
for (int index = 0; index < columns.size(); index++) {
expandedColumns.add(columns.get(index));
expandedColumnTypes.add(columnTypes.get(index));
String lobType = deferredTypes.get(index);
if (lobType != null) {
expandedColumns.add(LARGE_VALUE_BYTES_COLUMN_PREFIX + markerAliasKind(lobType) + "_" + index);
expandedColumnTypes.add("VARCHAR");
}
}
List<List<Object>> expandedRows = new ArrayList<>(rows.size());
for (List<Object> row : rows) {
List<Object> expandedRow = new ArrayList<>(expandedColumns.size());
for (int index = 0; index < columns.size(); index++) {
Object value = index < row.size() ? row.get(index) : null;
expandedRow.add(value);
if (deferredTypes.get(index) != null) {
expandedRow.add(value == null ? null : DEFERRED_LOB_MARKER);
}
}
expandedRows.add(expandedRow);
}
return new DeferredLobResult(expandedColumns, expandedColumnTypes, expandedRows);
}
private static String deferredLobType(int sqlType, String typeName) {
String normalized = typeName == null ? "" : typeName.trim().toUpperCase(Locale.ROOT);
int parameterStart = normalized.indexOf('(');
if (parameterStart >= 0) {
normalized = normalized.substring(0, parameterStart).trim();
}
if (sqlType == Types.BLOB || "BLOB".equals(normalized)) {
return "BLOB";
}
if ("DBCLOB".equals(normalized)) {
return "DBCLOB";
}
if (sqlType == Types.CLOB || sqlType == Types.NCLOB || "CLOB".equals(normalized) || "NCLOB".equals(normalized)) {
return "CLOB";
}
return null;
}
private static String markerAliasKind(String lobType) {
return "BLOB".equals(lobType) ? "L" : "C";
}
private static boolean shouldDeferLobs(String sql, boolean requested) {
return requested && !sql.toUpperCase(Locale.ROOT).contains(LARGE_VALUE_BYTES_COLUMN_PREFIX);
}
private static void rememberDeferredSession(Set<String> sessions, QueryPageResult result, boolean deferLobs) {
if (deferLobs && result.getHas_more() && result.getSession_id() != null) {
sessions.add(result.getSession_id());
}
}
private static QueryPageResult finishDeferredPage(Set<String> sessions, String sessionId, QueryPageResult result) {
if (!sessions.contains(sessionId)) {
return result;
}
if (!result.getHas_more()) {
sessions.remove(sessionId);
}
return addDeferredLobMarkers(result);
}
private record DeferredLobResult(
List<String> columns,
List<String> columnTypes,
List<List<Object>> rows
) {}
static String buildUrl(ConnectParams params) { static String buildUrl(ConnectParams params) {
if (!params.getConnection_string().trim().isEmpty()) { if (!params.getConnection_string().trim().isEmpty()) {
return params.getConnection_string(); return params.getConnection_string();
@@ -5,6 +5,7 @@ import com.dbx.agent.ConnectParams;
import com.dbx.agent.MetadataListConstraints; import com.dbx.agent.MetadataListConstraints;
import com.dbx.agent.ObjectInfo; import com.dbx.agent.ObjectInfo;
import com.dbx.agent.ObjectSource; import com.dbx.agent.ObjectSource;
import com.dbx.agent.QueryResult;
import com.dbx.agent.TableInfo; import com.dbx.agent.TableInfo;
import com.dbx.agent.test.JdbcFakeExecutionBehaviorTest; import com.dbx.agent.test.JdbcFakeExecutionBehaviorTest;
import com.dbx.agent.test.JdbcMetadataSqlFake; import com.dbx.agent.test.JdbcMetadataSqlFake;
@@ -15,6 +16,7 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Blob;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.Statement; import java.sql.Statement;
@@ -24,6 +26,7 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -92,6 +95,59 @@ class Db2AgentTest extends JdbcFakeExecutionBehaviorTest {
assertEquals("0x89504e", agent.resultValue(resultSet, 1, Types.LONGVARBINARY)); assertEquals("0x89504e", agent.resultValue(resultSet, 1, Types.LONGVARBINARY));
} }
@Test
void defersBlobMaterializationWithoutCallingGetBytes() {
Db2Agent agent = new Db2Agent();
AtomicInteger getBytesCalls = new AtomicInteger();
Blob blob = proxy(Blob.class, (method, args) -> defaultValue(method.getReturnType()));
ResultSet resultSet = proxy(ResultSet.class, (method, args) -> {
if ("getBlob".equals(method.getName())) {
return blob;
}
if ("getBytes".equals(method.getName())) {
getBytesCalls.incrementAndGet();
return new byte[] {1};
}
if ("wasNull".equals(method.getName())) {
return false;
}
return defaultValue(method.getReturnType());
});
assertEquals("<BLOB>", agent.deferredResultValue(resultSet, 1, Types.BLOB, "BLOB"));
assertEquals(0, getBytesCalls.get());
}
@Test
void addsHiddenMarkersForDeferredLobColumns() {
QueryResult result = new QueryResult(
Arrays.asList("MAFAPPDATAID", "APP", "DESCRIPTION"),
Arrays.asList("BIGINT", "BLOB", "CLOB(1M)"),
Arrays.asList(
Arrays.asList(1, "<BLOB>", "<CLOB>"),
Arrays.asList(2, null, null)
),
0L,
1L,
false
);
Db2Agent.addDeferredLobMarkers(result);
assertEquals(
Arrays.asList("MAFAPPDATAID", "APP", "__DBX_LARGE_VALUE_BYTES_L_1", "DESCRIPTION", "__DBX_LARGE_VALUE_BYTES_C_2"),
result.getColumns()
);
assertEquals(Arrays.asList("BIGINT", "BLOB", "VARCHAR", "CLOB(1M)", "VARCHAR"), result.getColumn_types());
assertEquals(
Arrays.asList(
Arrays.asList(1, "<BLOB>", "D:1", "<CLOB>", "D:1"),
Arrays.asList(2, null, null, null, null)
),
result.getRows()
);
}
@Test @Test
void listsAllCatalogSchemasWithoutOwnerTypeFiltering() { void listsAllCatalogSchemasWithoutOwnerTypeFiltering() {
Db2Agent agent = new Db2Agent(); Db2Agent agent = new Db2Agent();
@@ -48,6 +48,10 @@ describe("data grid large-value metadata", () => {
columnTypes: ["bigint", "longtext"], columnTypes: ["bigint", "longtext"],
largeValuePreviewSize: TABLE_DATA_CELL_PREVIEW_SIZE, largeValuePreviewSize: TABLE_DATA_CELL_PREVIEW_SIZE,
}); });
expect(tableDataLargeValuePreviewOptions("db2", [columns[0]!, column("payload", "blob")], ["id"], 100)).toEqual({
columnTypes: ["bigint", "blob"],
largeValuePreviewSize: TABLE_DATA_CELL_PREVIEW_SIZE,
});
expect(tableDataLargeValuePreviewOptions("sqlite", columns, ["id"], 100)).toEqual({}); expect(tableDataLargeValuePreviewOptions("sqlite", columns, ["id"], 100)).toEqual({});
expect(tableDataLargeValuePreviewOptions("mysql", columns, [], 100)).toEqual({}); expect(tableDataLargeValuePreviewOptions("mysql", columns, [], 100)).toEqual({});
expect(tableDataLargeValuePreviewOptions("mysql", [columns[0]!], ["id"], 100)).toEqual({}); expect(tableDataLargeValuePreviewOptions("mysql", [columns[0]!], ["id"], 100)).toEqual({});
@@ -124,6 +128,8 @@ describe("data grid large-value metadata", () => {
expect(isTableDataVisiblePreviewColumn("postgres", "jsonb")).toBe(true); expect(isTableDataVisiblePreviewColumn("postgres", "jsonb")).toBe(true);
expect(isTableDataVisiblePreviewColumn("postgres", "text[]")).toBe(false); expect(isTableDataVisiblePreviewColumn("postgres", "text[]")).toBe(false);
expect(isTableDataVisiblePreviewColumn("postgres", "vector(1536)")).toBe(false); expect(isTableDataVisiblePreviewColumn("postgres", "vector(1536)")).toBe(false);
expect(isTableDataVisiblePreviewColumn("db2", "clob")).toBe(true);
expect(isTableDataVisiblePreviewColumn("db2", "blob")).toBe(false);
expect(isTableDataVisiblePreviewColumn("oracle", "clob")).toBe(false); expect(isTableDataVisiblePreviewColumn("oracle", "clob")).toBe(false);
}); });
@@ -135,6 +135,9 @@ export function isTableDataVisiblePreviewColumn(databaseType: DatabaseType | und
if (databaseType === "postgres") { if (databaseType === "postgres") {
return !normalized.includes("[") && (base === "char" || base === "character" || base === "varchar" || base === "text" || base === "citext" || base === "name" || base === "xml" || base === "json" || base === "jsonb" || base === "tsvector" || normalized.startsWith("character varying")); return !normalized.includes("[") && (base === "char" || base === "character" || base === "varchar" || base === "text" || base === "citext" || base === "name" || base === "xml" || base === "json" || base === "jsonb" || base === "tsvector" || normalized.startsWith("character varying"));
} }
if (databaseType === "db2") {
return base === "clob" || base === "dbclob" || base === "char" || base === "character" || base === "varchar" || base === "graphic" || base === "vargraphic";
}
return false; return false;
} }
@@ -175,7 +178,11 @@ function matchesMysqlUnboundedLargeValueType(base: string): boolean {
} }
export function canUseTableDataLargeValuePreview(databaseType: DatabaseType | undefined, columns: readonly ColumnInfo[], primaryKeys: readonly string[]): boolean { export function canUseTableDataLargeValuePreview(databaseType: DatabaseType | undefined, columns: readonly ColumnInfo[], primaryKeys: readonly string[]): boolean {
return (databaseType === "mysql" || databaseType === "postgres") && columns.length > 0 && primaryKeys.length > 0 && !columns.some((column) => column.name.toLocaleUpperCase().startsWith(TABLE_DATA_LARGE_VALUE_MARKER_PREFIX)); return supportsTableDataLargeValuePreview(databaseType) && columns.length > 0 && primaryKeys.length > 0 && !columns.some((column) => column.name.toLocaleUpperCase().startsWith(TABLE_DATA_LARGE_VALUE_MARKER_PREFIX));
}
export function supportsTableDataLargeValuePreview(databaseType: DatabaseType | undefined): boolean {
return databaseType === "mysql" || databaseType === "postgres" || databaseType === "db2";
} }
/** /**
@@ -1356,6 +1356,71 @@ describe("queryStore hidden primary key editing", () => {
expect(executeMulti).toHaveBeenCalledWith("oracle-1", "ORCL", 'SELECT PAYLOAD, "ID" AS "__DBX_PK_0" FROM APP.DOCUMENTS', undefined, expect.any(String), expect.objectContaining({ tableDataPreview: true, timeoutSecs: 30 })); expect(executeMulti).toHaveBeenCalledWith("oracle-1", "ORCL", 'SELECT PAYLOAD, "ID" AS "__DBX_PK_0" FROM APP.DOCUMENTS', undefined, expect.any(String), expect.objectContaining({ tableDataPreview: true, timeoutSecs: 30 }));
}); });
it("defers DB2 BLOB materialization for a stable-key star query", async () => {
getConnectionConfig.mockReturnValue({ id: "db2-1", name: "DB2", db_type: "db2", database: "MAXIMO", query_timeout_secs: 60 });
getColumns.mockResolvedValue([
{ name: "MAFAPPDATAID", data_type: "BIGINT", is_nullable: false, column_default: null, is_primary_key: true, extra: null },
{ name: "APP", data_type: "BLOB", is_nullable: true, column_default: null, is_primary_key: false, extra: null },
]);
listIndexes.mockResolvedValue([{ name: "MAFAPPDATA_PK", columns: ["MAFAPPDATAID"], is_unique: true, is_primary: true }]);
analyzeEditableQueryEditability.mockResolvedValue({
editable: true,
analysis: {
schema: undefined,
tableName: "MAFAPPDATA",
tableAlias: "mf",
selectStar: true,
columns: [],
},
});
executeMulti.mockResolvedValue([
{
columns: ["MAFAPPDATAID", "APP"],
rows: [[1, "<BLOB>"]],
affected_rows: 0,
execution_time_ms: 57,
large_value_cells: [{ row_index: 0, column_index: 1, original_bytes: 64 * 1024 * 1024 }],
},
]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("db2-1", "MAXIMO", "Query", "query", "MAXIMO");
store.setAutoCommit(tabId, true);
await store.executeTabSql(tabId, "select * from MAFAPPDATA mf");
expect(executeMulti).toHaveBeenCalledWith("db2-1", "MAXIMO", "select * from MAFAPPDATA mf", "MAXIMO", expect.any(String), expect.objectContaining({ tableDataPreview: true, timeoutSecs: 60 }));
expect(store.tabs.find((tab) => tab.id === tabId)?.result?.large_value_cells).toEqual([{ row_index: 0, column_index: 1, original_bytes: 64 * 1024 * 1024 }]);
});
it("keeps deferred DB2 LOB handling disabled without a stable key", async () => {
getConnectionConfig.mockReturnValue({ id: "db2-1", name: "DB2", db_type: "db2", database: "MAXIMO", query_timeout_secs: 60 });
getColumns.mockResolvedValue([{ name: "APP", data_type: "BLOB", is_nullable: true, column_default: null, is_primary_key: false, extra: null }]);
listIndexes.mockResolvedValue([]);
analyzeEditableQueryEditability.mockResolvedValue({
editable: true,
analysis: {
schema: undefined,
tableName: "MAFAPPDATA",
tableAlias: "mf",
selectStar: true,
columns: [],
},
});
executeMulti.mockResolvedValue([{ columns: ["APP"], rows: [["0x01"]], affected_rows: 0, execution_time_ms: 1 }]);
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("db2-1", "MAXIMO", "Query", "query", "MAXIMO");
store.setAutoCommit(tabId, true);
await store.executeTabSql(tabId, "select * from MAFAPPDATA mf");
expect(executeMulti).toHaveBeenCalledOnce();
expect(executeMulti.mock.calls[0]?.[5]).not.toHaveProperty("tableDataPreview");
});
it("keeps deferred Oracle LOBs disabled for views", async () => { it("keeps deferred Oracle LOBs disabled for views", async () => {
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: "oracle", database: "ORCL", query_timeout_secs: 30 });
getColumns.mockResolvedValue([ getColumns.mockResolvedValue([
@@ -714,6 +714,59 @@ describe("queryStore table data refresh", () => {
); );
}); });
it("enables bounded DB2 BLOB previews for table refreshes", async () => {
mocks.getConnectionConfig.mockReturnValue({
id: "db2-1",
name: "DB2",
db_type: "db2",
database: "MAXIMO",
query_timeout_secs: 60,
});
mocks.buildTableSelectSql.mockResolvedValue('SELECT "MAFAPPDATAID", SUBSTR("APP", 1, 8193) AS "APP" FROM "MAXIMO"."MAFAPPDATA";');
const { useQueryStore } = await import("@/stores/queryStore");
const store = useQueryStore();
const tabId = store.createTab("db2-1", "MAXIMO", "MAFAPPDATA", "data", "MAXIMO");
store.setTableMeta(tabId, {
database: "MAXIMO",
schema: "MAXIMO",
tableName: "MAFAPPDATA",
tableType: "TABLE",
columns: [
{ name: "MAFAPPDATAID", data_type: "BIGINT", is_nullable: false, column_default: null, is_primary_key: true, extra: null },
{ name: "APP", data_type: "BLOB", is_nullable: true, column_default: null, is_primary_key: false, extra: null },
],
primaryKeys: ["MAFAPPDATAID"],
});
const tab = store.tabs.find((candidate) => candidate.id === tabId)!;
tab.resultPageLimit = 100;
tab.resultPageOffset = 0;
await expect(store.refreshDataTab(tabId)).resolves.toBe(true);
expect(mocks.buildTableSelectSql).toHaveBeenCalledWith(
expect.objectContaining({
databaseType: "db2",
columnTypes: ["BIGINT", "BLOB"],
largeValuePreviewSize: 8 * 1024,
}),
);
expect(mocks.executeMulti).toHaveBeenCalledWith(
"db2-1",
"MAXIMO",
expect.any(String),
undefined,
expect.any(String),
expect.objectContaining({
maxRows: 100,
fetchSize: 100,
maxResultBytes: 32 * 1024 * 1024,
resultKeyColumns: ["MAFAPPDATAID"],
tableDataPreview: true,
timeoutSecs: 60,
}),
);
});
it("keeps a MySQL table refresh unqualified in the selected database context", async () => { it("keeps a MySQL table refresh unqualified in the selected database context", async () => {
mocks.getConnectionConfig.mockReturnValue({ mocks.getConnectionConfig.mockReturnValue({
id: "mysql-1", id: "mysql-1",
+31 -20
View File
@@ -95,7 +95,7 @@ import { queryResultNameFromPreamble, queryResultSourceLabel, queryResultSourceN
import { sqlServerCountUsesLocalTempTable } from "@/lib/query/queryResultCountSession"; import { sqlServerCountUsesLocalTempTable } from "@/lib/query/queryResultCountSession";
import { stripPaginationRowNumber } from "@/lib/query/queryPaginationResult"; import { stripPaginationRowNumber } from "@/lib/query/queryPaginationResult";
import { beginDataGridNativeSelectionBlock, finishDataGridNativeSelectionBlock } from "@/lib/dataGrid/dataGridNativeSelection"; import { beginDataGridNativeSelectionBlock, finishDataGridNativeSelectionBlock } from "@/lib/dataGrid/dataGridNativeSelection";
import { appendLargeValueCells, canUseTableDataLargeValuePreview, remapLargeValueCells, tableDataLargeValuePreviewOptions, TABLE_DATA_RESULT_MAX_BYTES } from "@/lib/dataGrid/dataGridLargeValues"; import { appendLargeValueCells, canUseTableDataLargeValuePreview, remapLargeValueCells, supportsTableDataLargeValuePreview, tableDataLargeValuePreviewOptions, TABLE_DATA_RESULT_MAX_BYTES } from "@/lib/dataGrid/dataGridLargeValues";
import { simpleDataGridOrderByReferencesMissingColumn, sortDataGridRowIndexes, type DataGridSortDirection } from "@/lib/dataGrid/dataGridSort"; import { simpleDataGridOrderByReferencesMissingColumn, sortDataGridRowIndexes, type DataGridSortDirection } from "@/lib/dataGrid/dataGridSort";
import { normalizeResultPageSize } from "@/lib/dataGrid/paginationPageSize"; import { normalizeResultPageSize } from "@/lib/dataGrid/paginationPageSize";
import { agentProtocolQueryResultMaxRows, capQueryResultTotal, effectiveQueryResultMaxRows, limitQueryPagination, queryResultLimitReached } from "@/lib/dataGrid/queryResultRowLimit"; import { agentProtocolQueryResultMaxRows, capQueryResultTotal, effectiveQueryResultMaxRows, limitQueryPagination, queryResultLimitReached } from "@/lib/dataGrid/queryResultRowLimit";
@@ -148,6 +148,7 @@ const QUERY_SURFACE_ACTIVATION_EVENT = "dbx:activate-query-surface";
const ORACLE_LIKE_METADATA_TYPES = new Set<string>(["oracle", "dameng", "oceanbase-oracle"]); const ORACLE_LIKE_METADATA_TYPES = new Set<string>(["oracle", "dameng", "oceanbase-oracle"]);
const ORACLE_DEFERRED_LOB_TYPES = new Set<string>(["CLOB", "NCLOB", "BLOB", "BFILE", "XMLTYPE", "SYS.XMLTYPE"]); const ORACLE_DEFERRED_LOB_TYPES = new Set<string>(["CLOB", "NCLOB", "BLOB", "BFILE", "XMLTYPE", "SYS.XMLTYPE"]);
const DB2_DEFERRED_LOB_TYPES = new Set<string>(["CLOB", "DBCLOB", "BLOB"]);
// Bounded concurrency for grouped-query display column loads, scoped per // Bounded concurrency for grouped-query display column loads, scoped per
// connection so different connections never block each other. Matches the // connection so different connections never block each other. Matches the
@@ -163,7 +164,7 @@ const UPPERCASE_FOLDED_METADATA_TYPES = new Set<string>([...ORACLE_LIKE_METADATA
// OceanBase Oracle mode ride the Oracle adapter and HighGo rides the PostgreSQL one // OceanBase Oracle mode ride the Oracle adapter and HighGo rides the PostgreSQL one
// (issue #10233: without this, a query that omits the primary key stays read-only // (issue #10233: without this, a query that omits the primary key stays read-only
// even though the table has one). // even though the table has one).
const HIDDEN_QUERY_KEY_DATABASE_TYPES = new Set<DatabaseType>(["mysql", "postgres", "sqlserver", "oracle", "xugu", "dameng", "highgo", "oceanbase-oracle"]); const HIDDEN_QUERY_KEY_DATABASE_TYPES = new Set<DatabaseType>(["mysql", "postgres", "sqlserver", "oracle", "xugu", "dameng", "highgo", "oceanbase-oracle", "db2"]);
const QUERY_RESULT_EXPORT_UNSUPPORTED_ERROR = "Streaming export is unsupported for this query. Simplify it or use a supported driver."; const QUERY_RESULT_EXPORT_UNSUPPORTED_ERROR = "Streaming export is unsupported for this query. Simplify it or use a supported driver.";
const BACKGROUND_CLIENT_SESSION_SUFFIXES = ["count", "explain", "export"] as const; const BACKGROUND_CLIENT_SESSION_SUFFIXES = ["count", "explain", "export"] as const;
const CANCEL_QUERY_TIMEOUT_MS = 10_000; const CANCEL_QUERY_TIMEOUT_MS = 10_000;
@@ -838,14 +839,23 @@ function projectsAllColumnsForSource(analysis: EditableQueryInfo, sourceKey: str
return analysis.selectStar || analysis.columns.some((column) => column.star && (!column.sourceKey || column.sourceKey === sourceKey)); return analysis.selectStar || analysis.columns.some((column) => column.star && (!column.sourceKey || column.sourceKey === sourceKey));
} }
function oracleQueryProjectsDeferredLob(analysis: EditableQueryInfo, sourceKey: string, columns: readonly { name: string; data_type: string }[]): boolean { function queryProjectsDeferredLob(databaseType: DatabaseType, analysis: EditableQueryInfo, sourceKey: string, columns: readonly { name: string; data_type: string }[]): boolean {
const deferredColumns = new Set(columns.filter((column) => ORACLE_DEFERRED_LOB_TYPES.has(column.data_type.trim().toUpperCase())).map((column) => column.name.toLowerCase())); const deferredTypes = databaseType === "oracle" ? ORACLE_DEFERRED_LOB_TYPES : databaseType === "db2" ? DB2_DEFERRED_LOB_TYPES : undefined;
if (!deferredTypes) return false;
const deferredColumns = new Set(
columns
.filter((column) => {
const normalized = column.data_type.trim().toUpperCase();
return deferredTypes.has(normalized) || deferredTypes.has(normalized.split("(", 1)[0]!.trim());
})
.map((column) => column.name.toLowerCase()),
);
if (deferredColumns.size === 0) return false; if (deferredColumns.size === 0) return false;
if (projectsAllColumnsForSource(analysis, sourceKey)) return true; if (projectsAllColumnsForSource(analysis, sourceKey)) return true;
return analysis.columns.some((column) => column.sourceName && column.sourceKey === sourceKey && deferredColumns.has(column.sourceName.toLowerCase())); return analysis.columns.some((column) => column.sourceName && column.sourceKey === sourceKey && deferredColumns.has(column.sourceName.toLowerCase()));
} }
function oracleColumnsAllowDeferredLobMarkers(columns: readonly { name: string }[]): boolean { function columnsAllowDeferredLobMarkers(columns: readonly { name: string }[]): boolean {
return !columns.some((column) => column.name.toUpperCase().startsWith("__DBX_LARGE_VALUE_BYTES_")); return !columns.some((column) => column.name.toUpperCase().startsWith("__DBX_LARGE_VALUE_BYTES_"));
} }
@@ -5961,7 +5971,7 @@ export const useQueryStore = defineStore("query", () => {
sql: string; sql: string;
metadataSql: string; metadataSql: string;
hiddenPrimaryKeys: HiddenPrimaryKeyProjection[]; hiddenPrimaryKeys: HiddenPrimaryKeyProjection[];
oracleLobPreview: boolean; largeValuePreview: boolean;
} }
function oracleCompletionTableType(tab: QueryTab, metadataDbType: string, database: string, schema: string, tableName: string, catalog?: string): string | undefined { function oracleCompletionTableType(tab: QueryTab, metadataDbType: string, database: string, schema: string, tableName: string, catalog?: string): string | undefined {
@@ -6215,8 +6225,9 @@ export const useQueryStore = defineStore("query", () => {
function buildHiddenPrimaryKeyPreparation(tab: QueryTab, sql: string, databaseType: DatabaseType, loaded: LoadedEditableSource, primaryKeys: string[], declaredPrimaryKeys: string[], traceId: string, elapsed: () => string): EditableQueryExecutionPreparation { function buildHiddenPrimaryKeyPreparation(tab: QueryTab, sql: string, databaseType: DatabaseType, loaded: LoadedEditableSource, primaryKeys: string[], declaredPrimaryKeys: string[], traceId: string, elapsed: () => string): EditableQueryExecutionPreparation {
const metadataAnalysis = expandStarProjectionColumnsForSource(bindColumnsForSource(databaseType, loaded.analysis, loaded.source, loaded.tableMeta.columns), loaded.source, loaded.tableMeta.columns); const metadataAnalysis = expandStarProjectionColumnsForSource(bindColumnsForSource(databaseType, loaded.analysis, loaded.source, loaded.tableMeta.columns), loaded.source, loaded.tableMeta.columns);
const oracleLobPreview = databaseType === "oracle" && primaryKeys.length > 0 && oracleRowIdIsSafeForQuery(tab, loaded) && oracleColumnsAllowDeferredLobMarkers(loaded.tableMeta.columns) && oracleQueryProjectsDeferredLob(metadataAnalysis, loaded.source.key, loaded.tableMeta.columns); const stableLobSource = databaseType === "oracle" ? oracleRowIdIsSafeForQuery(tab, loaded) : databaseType === "db2";
const unchanged = { sql, metadataSql: sql, hiddenPrimaryKeys: [], oracleLobPreview }; const largeValuePreview = (databaseType === "oracle" || databaseType === "db2") && primaryKeys.length > 0 && stableLobSource && columnsAllowDeferredLobMarkers(loaded.tableMeta.columns) && queryProjectsDeferredLob(databaseType, metadataAnalysis, loaded.source.key, loaded.tableMeta.columns);
const unchanged = { sql, metadataSql: sql, hiddenPrimaryKeys: [], largeValuePreview };
const missingPrimaryKeys = const missingPrimaryKeys =
declaredPrimaryKeys.length === 0 declaredPrimaryKeys.length === 0
? primaryKeys.filter((primaryKey) => !(databaseType === "oracle" && primaryKey === DBX_ROWID_COLUMN && metadataAnalysis.columns.some((column) => column.sourceKey === loaded.source.key && !column.sourceNameQuoted && column.sourceName?.toUpperCase() === "ROWID"))) ? primaryKeys.filter((primaryKey) => !(databaseType === "oracle" && primaryKey === DBX_ROWID_COLUMN && metadataAnalysis.columns.some((column) => column.sourceKey === loaded.source.key && !column.sourceNameQuoted && column.sourceName?.toUpperCase() === "ROWID")))
@@ -6240,11 +6251,11 @@ export const useQueryStore = defineStore("query", () => {
keyCount: rewritten.projections.length, keyCount: rewritten.projections.length,
elapsed: elapsed(), elapsed: elapsed(),
}); });
return { sql: rewritten.sql, metadataSql: rewritten.sql, hiddenPrimaryKeys: rewritten.projections, oracleLobPreview }; return { sql: rewritten.sql, metadataSql: rewritten.sql, hiddenPrimaryKeys: rewritten.projections, largeValuePreview };
} }
async function prepareEditableQueryExecution(tab: QueryTab, sql: string, conn: ConnectionConfig | undefined, databaseType: DatabaseType | undefined, executionDatabase: string, traceId: string, elapsed: () => string): Promise<EditableQueryExecutionPreparation> { async function prepareEditableQueryExecution(tab: QueryTab, sql: string, conn: ConnectionConfig | undefined, databaseType: DatabaseType | undefined, executionDatabase: string, traceId: string, elapsed: () => string): Promise<EditableQueryExecutionPreparation> {
const unchanged = { sql, metadataSql: sql, hiddenPrimaryKeys: [], oracleLobPreview: false }; const unchanged = { sql, metadataSql: sql, hiddenPrimaryKeys: [], largeValuePreview: false };
if (!databaseType || !HIDDEN_QUERY_KEY_DATABASE_TYPES.has(databaseType) || !tab.connectionId) return unchanged; if (!databaseType || !HIDDEN_QUERY_KEY_DATABASE_TYPES.has(databaseType) || !tab.connectionId) return unchanged;
try { try {
@@ -6257,10 +6268,10 @@ export const useQueryStore = defineStore("query", () => {
const wholeSourceProjected = projectsAllColumnsForSource(analysis, source.key); const wholeSourceProjected = projectsAllColumnsForSource(analysis, source.key);
const hasDirectSourceProjection = analysis.columns.some((column) => Boolean(column.sourceName) && (!column.sourceKey || column.sourceKey === source.key)); const hasDirectSourceProjection = analysis.columns.some((column) => Boolean(column.sourceName) && (!column.sourceKey || column.sourceKey === source.key));
if (!wholeSourceProjected && !hasDirectSourceProjection) return unchanged; if (!wholeSourceProjected && !hasDirectSourceProjection) return unchanged;
// Whole-source projections already include declared primary keys. Only // Whole-source projections already include declared primary keys. Oracle
// Oracle and Xugu need preflight metadata here to add their synthetic // and Xugu may need a synthetic key, while DB2 needs column metadata to
// row key for a keyless base table. // decide whether LOB materialization can be deferred safely.
if (databaseType !== "oracle" && databaseType !== "xugu" && wholeSourceProjected) return unchanged; if (databaseType !== "oracle" && databaseType !== "xugu" && databaseType !== "db2" && wholeSourceProjected) return unchanged;
const target = resolveEditableSourceMetadataTarget(tab, analysis, source, conn, databaseType, executionDatabase); const target = resolveEditableSourceMetadataTarget(tab, analysis, source, conn, databaseType, executionDatabase);
const cached = getCachedTableMetadata(target.request); const cached = getCachedTableMetadata(target.request);
@@ -6951,7 +6962,7 @@ export const useQueryStore = defineStore("query", () => {
let resultSortedSql = options?.resultSortedSql; let resultSortedSql = options?.resultSortedSql;
let queryMetadataSql = queryBaseSql; let queryMetadataSql = queryBaseSql;
let hiddenPrimaryKeys: HiddenPrimaryKeyProjection[] = []; let hiddenPrimaryKeys: HiddenPrimaryKeyProjection[] = [];
let useOracleLobPreview = false; let useLargeValuePreview = false;
let pageSql: string | undefined; let pageSql: string | undefined;
let requestedPageLimit: number | undefined; let requestedPageLimit: number | undefined;
let pageLimit: number | undefined; let pageLimit: number | undefined;
@@ -7703,7 +7714,7 @@ export const useQueryStore = defineStore("query", () => {
// does not turn an otherwise editable result into a complex read-only one. // does not turn an otherwise editable result into a complex read-only one.
queryMetadataSql = options?.resultSortedSql && !options?.querySort ? queryBaseSql : prepared.metadataSql; queryMetadataSql = options?.resultSortedSql && !options?.querySort ? queryBaseSql : prepared.metadataSql;
hiddenPrimaryKeys = prepared.hiddenPrimaryKeys; hiddenPrimaryKeys = prepared.hiddenPrimaryKeys;
useOracleLobPreview = prepared.oracleLobPreview; useLargeValuePreview = prepared.largeValuePreview;
if (options?.querySort) { if (options?.querySort) {
const sorted = await api.buildSortedQuerySql({ const sorted = await api.buildSortedQuerySql({
originalSql: sqlToExecute, originalSql: sqlToExecute,
@@ -7816,14 +7827,14 @@ export const useQueryStore = defineStore("query", () => {
: { maxRows: agentProtocolQueryResultMaxRows(queryResultMaxRows) }), : { maxRows: agentProtocolQueryResultMaxRows(queryResultMaxRows) }),
...(useJdbcDriverRowOffset && typeof pageOffset === "number" && pageOffset > 0 ? { rowOffset: pageOffset } : {}), ...(useJdbcDriverRowOffset && typeof pageOffset === "number" && pageOffset > 0 ? { rowOffset: pageOffset } : {}),
...(executionClientSessionId ? { clientSessionId: executionClientSessionId } : {}), ...(executionClientSessionId ? { clientSessionId: executionClientSessionId } : {}),
...(tab.mode === "data" && (effectiveDbType === "mysql" || effectiveDbType === "postgres") ...(tab.mode === "data" && supportsTableDataLargeValuePreview(effectiveDbType)
? { ? {
maxResultBytes: TABLE_DATA_RESULT_MAX_BYTES, maxResultBytes: TABLE_DATA_RESULT_MAX_BYTES,
resultKeyColumns: dataTabMeta?.primaryKeys ?? [], resultKeyColumns: dataTabMeta?.primaryKeys ?? [],
tableDataPreview: useTableDataPreview, tableDataPreview: useTableDataPreview,
} }
: {}), : {}),
...(useOracleLobPreview ? { tableDataPreview: true } : {}), ...(useLargeValuePreview ? { tableDataPreview: true } : {}),
timeoutSecs: queryTimeoutSecs, timeoutSecs: queryTimeoutSecs,
catalog: executionCatalog, catalog: executionCatalog,
continueOnError: continueOnBatchError, continueOnError: continueOnBatchError,
@@ -7953,8 +7964,8 @@ export const useQueryStore = defineStore("query", () => {
// strand it after returning the first page. // strand it after returning the first page.
const executeInTransaction = (sessionId: string) => const executeInTransaction = (sessionId: string) =>
useAgentResultSession && !isOffsetJumpPage useAgentResultSession && !isOffsetJumpPage
? api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, agentProtocolQueryResultMaxRows(queryResultMaxRows), useOracleLobPreview, pageLimit, options?.pagination?.sessionId, classificationSql) ? api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, agentProtocolQueryResultMaxRows(queryResultMaxRows), useLargeValuePreview, pageLimit, options?.pagination?.sessionId, classificationSql)
: api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, pageLimit ?? agentProtocolQueryResultMaxRows(queryResultMaxRows), useOracleLobPreview, undefined, undefined, classificationSql); : api.executeInManualTransaction(sessionId, sqlToExecute, executionDatabase, executionSchema, pageLimit ?? agentProtocolQueryResultMaxRows(queryResultMaxRows), useLargeValuePreview, undefined, undefined, classificationSql);
try { try {
return await executeInTransaction(txnSessionId); return await executeInTransaction(txnSessionId);
} catch (error) { } catch (error) {