From a8e382b3d3d891386cd60f2c5530f3746f3fd575 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 22:43:55 +0700 Subject: [PATCH 1/2] perf(macos): drain the SQL import parser's autorelease pool per chunk, statement and COPY piece --- .../Core/Utilities/SQL/SQLFileParser.swift | 28 ++++++++----- .../TablePro/Views/Import/ImportDialog.swift | 2 +- .../Utilities/SQL/SQLFileParserTests.swift | 40 +++++++++++++++++++ 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift b/apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift index 3e7573c572..047b9e3885 100644 --- a/apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift +++ b/apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift @@ -1186,21 +1186,25 @@ extension SQLFileParser { return ctx.sawBatchSeparator } + // A MySQL count keeps each statement's text, so the cut drains per statement as well as per chunk. private func readNextStatement() throws { - processBuffer() - guard ctx.collected.isEmpty else { return } - guard !ctx.atEndOfInput else { - emitTrailingStatement() - finished = true - closeFile() - return + try autoreleasepool { + processBuffer() + guard ctx.collected.isEmpty else { return } + guard !ctx.atEndOfInput else { + emitTrailingStatement() + finished = true + closeFile() + return + } + try readChunk() } - try readChunk() } private func readChunk() throws { let handle = try openFileIfNeeded() - let rawData = handle.readData(ofLength: limits.chunkSize) + // A count, a GO scan and a driver's COPY read chunk after chunk with nothing draining the autorelease pool. + let rawData = try autoreleasepool { try handle.read(upToCount: limits.chunkSize) } ?? Data() if !rawData.isEmpty, let onRead { bytesRead += Int64(rawData.count) onRead(bytesRead, fileSize) @@ -1337,8 +1341,10 @@ extension SQLFileParser { private func nextRowsPiece(generation: Int) throws -> Data? { do { - guard let piece = try nextRowsRange(generation: generation) else { return nil } - return Data(nsBuffer.substring(with: piece).utf8) + return try autoreleasepool { + guard let piece = try nextRowsRange(generation: generation) else { return nil } + return Data(nsBuffer.substring(with: piece).utf8) + } } catch { rowsFailure = error copyRows = nil diff --git a/apple/TablePro/Views/Import/ImportDialog.swift b/apple/TablePro/Views/Import/ImportDialog.swift index 3c89d80f34..51662e3b6d 100644 --- a/apple/TablePro/Views/Import/ImportDialog.swift +++ b/apple/TablePro/Views/Import/ImportDialog.swift @@ -437,7 +437,7 @@ struct ImportDialog: View { } let maxPreviewSize = 5 * 1_024 * 1_024 - let previewData = handle.readData(ofLength: maxPreviewSize) + let previewData = try handle.read(upToCount: maxPreviewSize) ?? Data() var decoder = SQLChunkDecoder(encoding: selectedEncoding.encoding) if let preview = decoder.decode(previewData) { diff --git a/apple/TableProTests/Core/Utilities/SQL/SQLFileParserTests.swift b/apple/TableProTests/Core/Utilities/SQL/SQLFileParserTests.swift index 796c73cbb8..f2be88f9fb 100644 --- a/apple/TableProTests/Core/Utilities/SQL/SQLFileParserTests.swift +++ b/apple/TableProTests/Core/Utilities/SQL/SQLFileParserTests.swift @@ -415,6 +415,46 @@ struct SQLFileParserTests { #expect(count == statementCount) } + @Test("An empty file holds no statement, parsed or counted") + func emptyFile() async throws { + let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString + ".sql") + try Data().write(to: url) + defer { try? FileManager.default.removeItem(at: url) } + + let parser = SQLFileParser() + var statements: [String] = [] + for try await (statement, _) in parser.parseFile(url: url, encoding: .utf8, grammar: TestGrammar.postgres) { + statements.append(statement) + } + #expect(statements.isEmpty) + #expect(try await parser.countStatements(url: url, encoding: .utf8, grammar: TestGrammar.postgres) == 0) + } + + @Test("A last statement with no ; arrives when the file ends on a full read chunk", arguments: [1, 6, 9, 18]) + func lastStatementEndsOnChunkBoundary(chunkSize: Int) async throws { + let sql = "SELECT 1;\nSELECT 2" + try #require(sql.utf8.count % chunkSize == 0) + let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString + ".sql") + try Data(sql.utf8).write(to: url) + defer { try? FileManager.default.removeItem(at: url) } + + let parser = SQLFileParser(chunkSize: chunkSize) + var statements: [String] = [] + for try await (statement, _) in parser.parseFile(url: url, encoding: .utf8, grammar: TestGrammar.postgres) { + statements.append(statement) + } + #expect(statements == ["SELECT 1", "SELECT 2"]) + #expect(try await parser.countStatements(url: url, encoding: .utf8, grammar: TestGrammar.postgres) == 2) + } + + @Test("A UTF-8 file cut inside its last character fails rather than dropping the bytes") + func fileCutInsideItsLastCharacter() async throws { + let cut = Data(Data("SELECT 'é".utf8).dropLast()) + await #expect(throws: DecompressionError.self) { + _ = try await Self.parse(cut, encoding: .utf8) + } + } + @Test("Dialect.from maps known database type ids") func dialect_from_database_type_id() { #expect(SqlDialect.from(databaseTypeId: "PostgreSQL") == .postgres) From 7a4dd3cbce3ef0e85727cd6dfd8c3b60f8ff33aa Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 22:44:32 +0700 Subject: [PATCH 2/2] docs(changelog): add fragments for #4279 --- apple/changelog.d/4279.fixed.1.md | 1 + apple/changelog.d/4279.fixed.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 apple/changelog.d/4279.fixed.1.md create mode 100644 apple/changelog.d/4279.fixed.md diff --git a/apple/changelog.d/4279.fixed.1.md b/apple/changelog.d/4279.fixed.1.md new file mode 100644 index 0000000000..b52a9b633b --- /dev/null +++ b/apple/changelog.d/4279.fixed.1.md @@ -0,0 +1 @@ +A read error while previewing or importing a SQL file quit TablePro instead of failing the import. diff --git a/apple/changelog.d/4279.fixed.md b/apple/changelog.d/4279.fixed.md new file mode 100644 index 0000000000..8c95d60df1 --- /dev/null +++ b/apple/changelog.d/4279.fixed.md @@ -0,0 +1 @@ +Importing a large SQL file held memory the size of the file while counting its statements or loading COPY rows.