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
28 changes: 17 additions & 11 deletions apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apple/TablePro/Views/Import/ImportDialog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 40 additions & 0 deletions apple/TableProTests/Core/Utilities/SQL/SQLFileParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions apple/changelog.d/4279.fixed.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A read error while previewing or importing a SQL file quit TablePro instead of failing the import.
1 change: 1 addition & 0 deletions apple/changelog.d/4279.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Importing a large SQL file held memory the size of the file while counting its statements or loading COPY rows.
Loading