Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ extension QueryExecutionCoordinator {
}
// One entry per result in the order `batchResultSets` places them; a count result has no rows to build.
var shapes: [Bool] = []
var superseded: Set<Int> = []
var sources: [(sql: String?, result: QueryResult)] = []
var sourceCandidates: [Int] = []
for (mapped, output) in zip(queries, outputs) {
Expand All @@ -689,6 +690,7 @@ extension QueryExecutionCoordinator {
continue
}
// Result sets the batch let go still count toward the run's results, and are never shown.
superseded.formUnion(shapes.count..<(shapes.count + output.supersededResultSetCount))
shapes += repeatElement(true, count: output.supersededResultSetCount)
let pairs: [(sql: String?, result: QueryResult)] = mapped.map { mapped in
zip(mapped, output.resultSets).map { (sql: $0.sql as String?, result: $1) }
Expand All @@ -699,7 +701,11 @@ extension QueryExecutionCoordinator {
sources.append(pair)
}
}
let results = RunResultPlan(producesRows: shapes)
let results = RunResultPlan(
producesRows: shapes,
keepingRowResults: AppSettingsManager.shared.dataGrid.resultSetLimitPerRun,
unavailable: superseded
)
let shown = zip(sources, sourceCandidates).map { source, candidate in
results.showsRows(ofStatement: candidate) ? source : nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ private struct ExecutedStatementHistory {
let results: [QueryResult]
let parameters: [QueryParameter]
let connection: DatabaseConnection
// Read once per run: the rows built off the main actor and the results shown take the same plan.
let rowResultLimit: Int?

func keeping(_ kept: [QueryResult]) -> ExecutedStatementHistory {
ExecutedStatementHistory(prepared: prepared, results: kept, parameters: parameters, connection: connection)
ExecutedStatementHistory(
prepared: prepared, results: kept, parameters: parameters, connection: connection, rowResultLimit: rowResultLimit
)
}
}

Expand Down Expand Up @@ -470,7 +474,8 @@ extension QueryExecutionCoordinator {
case .completed(let ran), .cancelled(let ran), .failed(let ran, _, _):
results = ran
}
let resultPlan = RunResultPlan(producesRows: results.map { !$0.columns.isEmpty })
let rowResultLimit = AppSettingsManager.shared.dataGrid.resultSetLimitPerRun
let resultPlan = RunResultPlan(producesRows: results.map { !$0.columns.isEmpty }, keepingRowResults: rowResultLimit)
let built = await buildStatementRows(
zip(sent, results).enumerated().map { index, pair in
resultPlan.showsRows(ofStatement: index) ? (sql: pair.0.originalSQL as String?, result: pair.1) : nil
Expand All @@ -479,7 +484,7 @@ extension QueryExecutionCoordinator {
)

let history = ExecutedStatementHistory(
prepared: sent, results: results, parameters: parameters, connection: conn
prepared: sent, results: results, parameters: parameters, connection: conn, rowResultLimit: rowResultLimit
)
guard parent.tabExecution.settle(claim) || reportsUnknownCommitAfterStop(outcome, claim: claim) else {
keepStatementsAfterStop(
Expand Down Expand Up @@ -773,7 +778,7 @@ extension QueryExecutionCoordinator {
let firstNumber = parent.tabManager.tabs.first { $0.id == tabId }?.display.pinnedResultCount ?? 0
let reading = parent.tableNameReading
let ran = Array(zip(history.prepared, history.results))
let plan = RunResultPlan(producesRows: ran.map { !$0.1.columns.isEmpty })
let plan = RunResultPlan(producesRows: ran.map { !$0.1.columns.isEmpty }, keepingRowResults: history.rowResultLimit)
let resultSets = plan.entries.enumerated().map { position, entry -> ResultSet in
switch entry {
case .summary(let indices):
Expand Down
24 changes: 13 additions & 11 deletions apple/TablePro/Core/Coordinators/RunResultPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import TableProPluginKit

// Which statements of a run become results of their own. Two or more statements that return no
// rows share one summary, placed where the last of them ran, so the result of the last statement
// stays the last result. Only the last `maxRowResults` results that return rows are kept.
// stays the last result. With a limit, only that many of the newest results that return rows are kept.
struct RunResultPlan: Equatable, Sendable {
static let maxRowResults = 100

enum Entry: Equatable, Sendable {
case statement(Int)
case summary([Int])
Expand All @@ -25,9 +23,11 @@ struct RunResultPlan: Equatable, Sendable {

var droppedRowResultCount: Int { rowResultCount - keptRowResultCount }

init(producesRows: [Bool], maxRowResults: Int = Self.maxRowResults) {
// `unavailable` are results that return rows but are already gone, so they count as dropped whatever the limit.
init(producesRows: [Bool], keepingRowResults limit: Int?, unavailable: Set<Int> = []) {
let rowIndices = producesRows.indices.filter { producesRows[$0] }
let kept = Set(rowIndices.suffix(max(0, maxRowResults)))
let available = rowIndices.filter { !unavailable.contains($0) }
let kept = Set(limit.map { available.suffix(max(1, $0)) } ?? available[...])
let summarized = producesRows.indices.filter { !producesRows[$0] }
let collapses = summarized.count >= 2

Expand Down Expand Up @@ -71,12 +71,14 @@ struct RunResultPlan: Equatable, Sendable {
}

var droppedNotice: String? {
guard droppedRowResultCount > 0 else { return nil }
return String(
format: String(localized: "Showing the last %1$@ of %2$@ results that returned rows."),
keptRowResultCount.formatted(),
rowResultCount.formatted()
)
switch droppedRowResultCount {
case ...0:
return nil
case 1:
return String(localized: "1 earlier result was dropped.")
default:
return String(format: String(localized: "%@ earlier results were dropped."), droppedRowResultCount.formatted())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ internal struct AgentTransactionGuard: Sendable {
throw error
}
let pendingMatters = commitMode == .manual && writes
guard pendingMatters || startState.holdsUserTransaction else { return (value, nil) }
// A Teradata ANSI session holds every change in a transaction it opens, which then refuses the next statement.
let mayHoldWrite = writes && !controls && startState == .idle
&& family.failedStatementAbortsTransaction && !family.takesBackAFailedStatement
guard pendingMatters || startState.holdsUserTransaction || mayHoldWrite else { return (value, nil) }
let endState = await driver.heldSessionTransactionState()
let notice = Self.notice(
startState: startState, endState: endState, pendingMatters: pendingMatters, openedHold: opened
Expand All @@ -142,7 +145,8 @@ internal struct AgentTransactionGuard: Sendable {
openedHold: Bool
) -> String? {
if startState.holdsUserTransaction || openedHold, endState == .idle { return ManualCommit.transactionEndedNotice }
return pendingMatters && endState.holdsUserTransaction ? ManualCommit.pendingNotice : nil
if pendingMatters { return endState.holdsUserTransaction ? ManualCommit.pendingNotice : nil }
return startState == .idle && !openedHold && endState.holdsUserTransaction ? ManualCommit.statementOpenedNotice : nil
}

nonisolated private func savepointed<T>(
Expand Down
14 changes: 9 additions & 5 deletions apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,9 @@ internal actor DatabaseAccessBridge {
)
}

// One approved INSERT per row, on one write lease, each with the values it was approved with.
internal func runInserts(_ operation: ApprovedOperation) async throws -> Int {
// One approved INSERT per row, on one write lease, each with the values it was approved with. The notice says the
// rows are pending in a transaction.
internal func runInserts(_ operation: ApprovedOperation) async throws -> (affected: Int, notice: String?) {
let reach = DriverReach()
do {
return try await runApprovedInserts(operation, reach: reach)
Expand All @@ -567,7 +568,10 @@ internal actor DatabaseAccessBridge {
}
}

private func runApprovedInserts(_ operation: ApprovedOperation, reach: DriverReach) async throws -> Int {
private func runApprovedInserts(
_ operation: ApprovedOperation,
reach: DriverReach
) async throws -> (affected: Int, notice: String?) {
guard operation.kind == .writeQuery, operation.unit == .statement, !operation.statements.isEmpty
else { throw Self.notApproved }
let scope = operation.scope
Expand All @@ -590,7 +594,7 @@ internal actor DatabaseAccessBridge {
scope: scope,
route: route,
cancellation: .protectedWrite
) { driver -> Int in
) { driver -> (affected: Int, notice: String?) in
reach.mark()
// The hold and savepoint execute_query gets, so a Manual Commit insert waits for Commit too.
let ran = try await agentGuard.running(
Expand All @@ -607,7 +611,7 @@ internal actor DatabaseAccessBridge {
}
return affected
}
return ran.value
return (ran.value, ran.notice)
}
}

Expand Down
10 changes: 7 additions & 3 deletions apple/TablePro/Core/MCP/MCPConnectionBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,15 @@ public actor MCPConnectionBridge {

func insertRows(_ operation: ApprovedOperation, table: String) async throws -> JsonValue {
let inserted = try await access.runInserts(operation)
return .object([
var reply: [String: JsonValue] = [
"table": .string(table),
"rows_submitted": .int(operation.statements.count),
"rows_affected": .int(inserted)
])
"rows_affected": .int(inserted.affected)
]
if let notice = inserted.notice {
reply["status_message"] = .string(notice)
}
return .object(reply)
}

func transactionControl(_ operation: ApprovedOperation, action: String) async throws -> JsonValue {
Expand Down
3 changes: 2 additions & 1 deletion apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ public struct InsertRowsTool: MCPToolImplementation {
properties: [
"table": MCPToolSchema.string(String(localized: "Table that received the rows")),
"rows_submitted": MCPToolSchema.integer(String(localized: "Rows sent")),
"rows_affected": MCPToolSchema.integer(String(localized: "Rows the engine reported inserting"))
"rows_affected": MCPToolSchema.integer(String(localized: "Rows the engine reported inserting")),
"status_message": MCPToolSchema.string(String(localized: "What happened to the open transaction, when it matters"))
],
required: ["table", "rows_submitted", "rows_affected"]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ internal enum BatchTransactionPolicy {
return SqlBlockStructure.beginStartsTransaction(followedBy: cursor.next()?.word)
case "START":
return cursor.next()?.word == "TRANSACTION"
case "BT":
return family == .teradata
case "XA":
let following = cursor.next()?.word
return following == "START" || following == "BEGIN"
Expand Down
11 changes: 10 additions & 1 deletion apple/TablePro/Core/Services/Execution/ManualCommit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,14 @@ internal enum ManualCommit {
grammar: SQLLexicalGrammar
) -> Bool {
if startsTransaction(statements, databaseType: databaseType, grammar: grammar) { return true }
let isTeradata = TransactionEngineFamily.of(databaseType) == .teradata
return statements.contains { statement in
if CatalogChangeClassifier.effect(of: statement, databaseType: databaseType, grammar: grammar).endsTransaction {
return true
}
return transactionControlKeywords.contains(QueryClassifier.leadingKeyword(of: statement, grammar: grammar))
let keyword = QueryClassifier.leadingKeyword(of: statement, grammar: grammar)
// Teradata's short form of END TRANSACTION.
return transactionControlKeywords.contains(keyword) || (isTeradata && keyword == "ET")
}
}

Expand All @@ -326,6 +329,12 @@ internal enum ManualCommit {
String(localized: "The open transaction on this connection ended while this statement ran.")
}

internal static var statementOpenedNotice: String {
String(
localized: "This change is pending in a transaction the statement opened. Other statements are refused until it is committed or rolled back."
)
}

internal static var pendingNotice: String {
String(localized: "Manual Commit is on for this connection, so this change is pending until the user commits it in TablePro.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ internal enum TransactionEngineFamily: String, Hashable, Sendable, CaseIterable
// After a failed statement the block takes nothing but a rollback, so one error costs every pending change.
internal var failedStatementAbortsTransaction: Bool {
switch self {
case .postgres, .redshift, .cockroach, .duckdb:
// Teradata mode rolls back the whole transaction on any failed request, ANSI mode on a failure such as a
// deadlock; the family cannot see the session's mode, so it takes the stricter one.
case .postgres, .redshift, .cockroach, .duckdb, .teradata:
return true
// Teradata mode rolls back the whole transaction on a failed request and ANSI mode only the request; the
// family cannot see the session's mode, so Teradata keeps the answer it had before it had a family.
case .mysql, .sqlite, .sqlServer, .oracle, .redis, .teradata, .other:
case .mysql, .sqlite, .sqlServer, .oracle, .redis, .other:
return false
}
}
Expand Down
15 changes: 13 additions & 2 deletions apple/TablePro/Models/Settings/AppSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ struct DataGridSettings: Codable, Equatable {
var defaultSortDirection: SortDirection
var rowCopyFormat: RowCopyFormat
var showInvisibleCharacters: Bool
// 0 keeps every result a run returns.
var resultSetsKeptPerRun: Int

static let `default` = DataGridSettings(
rowHeight: .normal,
Expand All @@ -140,7 +142,8 @@ struct DataGridSettings: Codable, Equatable {
defaultSortBehavior: .none,
defaultSortDirection: .ascending,
rowCopyFormat: .rows,
showInvisibleCharacters: true
showInvisibleCharacters: true,
resultSetsKeptPerRun: 0
)

init(
Expand All @@ -158,7 +161,8 @@ struct DataGridSettings: Codable, Equatable {
defaultSortBehavior: DefaultSortBehavior = .none,
defaultSortDirection: SortDirection = .ascending,
rowCopyFormat: RowCopyFormat = .rows,
showInvisibleCharacters: Bool = true
showInvisibleCharacters: Bool = true,
resultSetsKeptPerRun: Int = 0
) {
self.rowHeight = rowHeight
self.dateFormat = dateFormat
Expand All @@ -175,6 +179,7 @@ struct DataGridSettings: Codable, Equatable {
self.defaultSortDirection = defaultSortDirection
self.rowCopyFormat = rowCopyFormat
self.showInvisibleCharacters = showInvisibleCharacters
self.resultSetsKeptPerRun = resultSetsKeptPerRun
}

init(from decoder: Decoder) throws {
Expand Down Expand Up @@ -202,6 +207,7 @@ struct DataGridSettings: Codable, Equatable {
showInvisibleCharacters = container.decodeSetting(
.showInvisibleCharacters, default: fallback.showInvisibleCharacters
)
resultSetsKeptPerRun = container.decodeSetting(.resultSetsKeptPerRun, default: fallback.resultSetsKeptPerRun)
}

// MARK: - Validated Properties
Expand Down Expand Up @@ -244,6 +250,11 @@ struct DataGridSettings: Codable, Equatable {
return nil
}

// Nil keeps every result.
var resultSetLimitPerRun: Int? {
resultSetsKeptPerRun > 0 ? resultSetsKeptPerRun : nil
}

var validatedQueryResultRowCap: Int {
if queryResultRowCap == 0 { return 0 }
return queryResultRowCap.clamped(to: SettingsValidationRules.queryResultRowCapRange)
Expand Down
21 changes: 21 additions & 0 deletions apple/TablePro/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -3473,6 +3473,9 @@
"%@ during the Bolt handshake. If the server requires TLS, turn on SSL." : {
"extractionState" : "manual",
"generatesSymbol" : false
},
"%@ earlier results were dropped." : {

},
"%@ ends on %@ and does not renew." : {
"localizations" : {
Expand Down Expand Up @@ -13622,6 +13625,9 @@
}
}
}
},
"1 earlier result was dropped." : {

},
"1 field" : {

Expand Down Expand Up @@ -16988,6 +16994,9 @@
}
}
}
},
"A run that returns more results with rows keeps only the newest. Pinned results always stay." : {

},
"a running query" : {

Expand Down Expand Up @@ -113469,6 +113478,9 @@
}
}
}
},
"Multiple Results" : {

},
"Multiple statements are not permitted for this client" : {
"localizations" : {
Expand Down Expand Up @@ -152680,6 +152692,9 @@
}
}
}
},
"Results kept per run:" : {

},
"Resume" : {
"localizations" : {
Expand Down Expand Up @@ -197879,6 +197894,9 @@
},
"This change does not apply to this kind of type." : {

},
"This change is pending in a transaction the statement opened. Other statements are refused until it is committed or rolled back." : {

},
"This change needs %@ to be recreated, and it cannot be recreated from the definition the database stored for it. Virtual tables, such as full-text search tables, are the usual reason." : {
"localizations" : {
Expand Down Expand Up @@ -216228,6 +216246,9 @@
}
}
}
},
"What happened to the open transaction, when it matters" : {

},
"What is switched on here is ignored when two structures are compared. These drift between environments by design." : {
"localizations" : {
Expand Down
Loading
Loading