From 04b26bd1fce0fd95db95efe7ec70d35c9a64fa39 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 08:33:15 +0700 Subject: [PATCH 1/2] fix(macos): read Teradata's BT and ET as transaction control only in its own grammar --- .../TableProSQLGrammar/SQLCatalogEffect.swift | 15 +++++++++++---- .../TableProSQLGrammar/SQLLexicalGrammar.swift | 3 +++ .../TableProSQLGrammar/SQLLexicalProfile.swift | 4 +++- .../TableProSQLGrammar/SQLLexicalReadings.swift | 2 +- .../SQLCatalogEffectClassifierTests.swift | 11 +++++++++++ .../Services/Execution/BatchCommitStatement.swift | 3 +-- .../Execution/BatchTransactionPolicy.swift | 2 +- .../Core/Services/Execution/ManualCommit.swift | 5 +---- .../Execution/BatchCommitStatementTests.swift | 4 +++- .../Plugins/SQLLexicalFeatureMappingTests.swift | 3 ++- 10 files changed, 37 insertions(+), 15 deletions(-) diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift index bb1b393ae0..bb2f0f4ea6 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift @@ -73,7 +73,7 @@ public enum SQLCatalogEffectClassifier { return .opaque } let tokens = leadingTokens(of: trimmed, grammar: grammar) - let leading = leadingKeywordEffect(tokens: tokens, trimmed: trimmed) + let leading = leadingKeywordEffect(tokens: tokens, trimmed: trimmed, grammar: grammar) guard leading.kinds.isEmpty else { return leading } return leading.union(embeddedDefinitionEffect(tokens: tokens)) } @@ -89,9 +89,17 @@ public enum SQLCatalogEffectClassifier { return SQLCatalogEffect(kinds: definedKinds(after: tokens[(index + 1)...]), endsTransaction: false) } - private static func leadingKeywordEffect(tokens: [String], trimmed: String) -> SQLCatalogEffect { + private static func leadingKeywordEffect( + tokens: [String], + trimmed: String, + grammar: SQLLexicalGrammar + ) -> SQLCatalogEffect { guard let keyword = tokens.first else { return .none } + if grammar.contains(.transactionShortForms), tokens.count == 1 { + if keyword == "ET" { return SQLCatalogEffect(kinds: [], endsTransaction: true) } + if keyword == "BT" { return .none } + } if transactionEndKeywords.contains(keyword) { let rollsBackToSavepoint = keyword == "ROLLBACK" && tokens.dropFirst().prefix(2).contains("TO") return rollsBackToSavepoint ? .none : SQLCatalogEffect(kinds: [], endsTransaction: true) @@ -160,8 +168,7 @@ public enum SQLCatalogEffectClassifier { sql.range(of: #"\bINTO\b"#, options: [.regularExpression, .caseInsensitive]) != nil } - // ET is Teradata's END TRANSACTION. - private static let transactionEndKeywords: Set = ["COMMIT", "END", "ROLLBACK", "ABORT", "ET"] + private static let transactionEndKeywords: Set = ["COMMIT", "END", "ROLLBACK", "ABORT"] // PostgreSQL's `DISCARD TEMP` and `DISCARD ALL` drop the session's temporary tables. private static let discardedTableTargets: Set = ["TEMP", "TEMPORARY", "ALL"] diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalGrammar.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalGrammar.swift index 187eb5dd2b..04b79e747e 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalGrammar.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalGrammar.swift @@ -164,6 +164,9 @@ public struct SQLLexicalGrammar: OptionSet, Hashable, Sendable { /// PostgreSQL 17.11. That pass reads quotes its own way, so DuckDB's profile also doubts this fact. Curated only. public static let nonASCIISpacesEndNames = SQLLexicalGrammar(rawValue: 1 << 42) + // `BT` and `ET` begin and end a transaction, as Teradata reads them; anywhere else they are names. Curated only. + public static let transactionShortForms = SQLLexicalGrammar(rawValue: 1 << 43) + /// Standard SQL: quotes close only on a doubled quote, `--` and flat `/* */` are the only comments. public static let ansi: SQLLexicalGrammar = [] diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift index eb5701d9aa..9415934219 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift @@ -82,6 +82,8 @@ public struct SQLLexicalProfile: Sendable, Hashable { .plsqlBlocks, .delimiterDirective, .unterminatedStatements, .terminatedMergeStatements, .sqlScriptBlocks, .cqlBatches, .atomicRoutineBodies, .triggerBodies, .statementsAsValues, .bracketedStatementSources, .pipeOperatorChains, .psqlScripts, + // An unknown engine reads `BT` and `ET` as names, not as the end of a transaction. + .transactionShortForms, ]) if seen.insert(unitless).inserted { result.append(unitless) @@ -326,7 +328,7 @@ public struct SQLLexicalProfile: Sendable, Hashable { "Trino": trinoFamily, "Presto": trinoFamily, "Teradata": SQLLexicalProfile( - grammar: .ansi, + grammar: [.transactionShortForms], undetermined: [.nestedBlockComments, .carriageReturnEndsLineComments] ), "SAP HANA": SQLLexicalProfile( diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalReadings.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalReadings.swift index be5805d4a9..0e7e9e7c52 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalReadings.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalReadings.swift @@ -138,7 +138,7 @@ enum SQLLexicalRelevance { var facts: SQLLexicalGrammar = [ .plsqlBlocks, .delimiterDirective, .unterminatedStatements, .terminatedMergeStatements, .sqlScriptBlocks, .cqlBatches, .atomicRoutineBodies, .triggerBodies, .statementsAsValues, - .pipeOperatorChains, .databendTokens, .psqlScripts, + .pipeOperatorChains, .databendTokens, .psqlScripts, .transactionShortForms, ] var blockCommentOpeners = 0 var index = 0 diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift index b1b960672e..9b27290def 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift @@ -59,6 +59,17 @@ struct SQLCatalogEffectClassifierTests { #expect(SQLCatalogEffectClassifier.effect(of: "ET", grammar: grammar("Teradata")).endsTransaction) } + // Only Teradata reads BT and ET as transaction control; elsewhere a lone word is a statement nothing knows. + @Test("BT and ET are transaction control in Teradata's grammar alone") + func transactionShortFormsAreTeradatas() { + #expect(SQLCatalogEffectClassifier.effect(of: "et", grammar: grammar("Teradata")).endsTransaction) + #expect(SQLCatalogEffectClassifier.effect(of: "BT", grammar: grammar("Teradata")) == .none) + for engine in ["PostgreSQL", "MySQL", "SQL Server", "Oracle", "Nonesuch"] { + #expect(!SQLCatalogEffectClassifier.effect(of: "ET", grammar: grammar(engine)).endsTransaction, "\(engine)") + } + #expect(SQLLexicalProfile.everyKnownReading.allSatisfy { !$0.contains(.transactionShortForms) }) + } + @Test("Code the text cannot show refreshes every kind") func opaqueCode() { #expect(kinds("DO $$ BEGIN EXECUTE 'CREATE TABLE t (id int)'; END $$") == .everything) diff --git a/apple/TablePro/Core/Services/Execution/BatchCommitStatement.swift b/apple/TablePro/Core/Services/Execution/BatchCommitStatement.swift index 22f4f1dd9e..3fad4836c7 100644 --- a/apple/TablePro/Core/Services/Execution/BatchCommitStatement.swift +++ b/apple/TablePro/Core/Services/Execution/BatchCommitStatement.swift @@ -35,8 +35,7 @@ internal enum BatchCommitStatement { case "END": guard let follower = cursor.next()?.word else { return true } return transactionNouns.contains(follower) - // Teradata's abbreviation of END TRANSACTION. - case "ET": + case "ET" where grammar.contains(.transactionShortForms): return cursor.next() == nil default: return false diff --git a/apple/TablePro/Core/Services/Execution/BatchTransactionPolicy.swift b/apple/TablePro/Core/Services/Execution/BatchTransactionPolicy.swift index fed3e0fdf3..b53e8cfb5f 100644 --- a/apple/TablePro/Core/Services/Execution/BatchTransactionPolicy.swift +++ b/apple/TablePro/Core/Services/Execution/BatchTransactionPolicy.swift @@ -82,7 +82,7 @@ internal enum BatchTransactionPolicy { case "START": return cursor.next()?.word == "TRANSACTION" case "BT": - return family == .teradata + return grammar.contains(.transactionShortForms) case "XA": let following = cursor.next()?.word return following == "START" || following == "BEGIN" diff --git a/apple/TablePro/Core/Services/Execution/ManualCommit.swift b/apple/TablePro/Core/Services/Execution/ManualCommit.swift index 3c15baeb50..fc3cf36993 100644 --- a/apple/TablePro/Core/Services/Execution/ManualCommit.swift +++ b/apple/TablePro/Core/Services/Execution/ManualCommit.swift @@ -298,14 +298,11 @@ 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 } - let keyword = QueryClassifier.leadingKeyword(of: statement, grammar: grammar) - // Teradata's short form of END TRANSACTION. - return transactionControlKeywords.contains(keyword) || (isTeradata && keyword == "ET") + return transactionControlKeywords.contains(QueryClassifier.leadingKeyword(of: statement, grammar: grammar)) } } diff --git a/apple/TableProTests/Core/Services/Execution/BatchCommitStatementTests.swift b/apple/TableProTests/Core/Services/Execution/BatchCommitStatementTests.swift index f0abccfd27..5c29a89a4a 100644 --- a/apple/TableProTests/Core/Services/Execution/BatchCommitStatementTests.swift +++ b/apple/TableProTests/Core/Services/Execution/BatchCommitStatementTests.swift @@ -43,9 +43,11 @@ struct BatchCommitStatementTests { #expect(Self.matches(sql, type: .teradata)) } - @Test("ET followed by anything is not ET") + @Test("ET followed by anything is not ET, and ET is only Teradata's") func etWithAFollowerIsNotACommitPoint() { #expect(Self.matches("ET AL", type: .teradata) == false) + #expect(Self.matches("ET") == false) + #expect(Self.matches("ET", type: .mysql) == false) } /// A block terminator is never a transaction's end, whichever engine wrote it. diff --git a/apple/TableProTests/Plugins/SQLLexicalFeatureMappingTests.swift b/apple/TableProTests/Plugins/SQLLexicalFeatureMappingTests.swift index e469284b29..83b5bc8a96 100644 --- a/apple/TableProTests/Plugins/SQLLexicalFeatureMappingTests.swift +++ b/apple/TableProTests/Plugins/SQLLexicalFeatureMappingTests.swift @@ -30,9 +30,10 @@ struct SQLLexicalFeatureMappingTests { .backslashEscapesInAngleQuotes, .escapeBytesPrefix, .exponentSignsNeedNoDigits, .formFeedEndsLineComments, .atomicRoutineBodies, .triggerBodies, .statementsAsValues, .bracketedStatementSources, .pipeOperatorChains, .withoutDashComments, .escapeStringContinuations, .databendTokens, .psqlScripts, .nonASCIISpacesEndNames, + .transactionShortForms, ] #expect(grammarBits.isDisjoint(with: curatedOnly)) - #expect(grammarBits.union(curatedOnly).rawValue == (1 << 43) - 1) + #expect(grammarBits.union(curatedOnly).rawValue == (1 << 44) - 1) } @Test("SAP HANA's SQLScript blocks stay in the app's curated grammar and never reach the kit") From 1b63cb2000448a3e5ef85aecbe5c9708ff2d980c Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 08:46:40 +0700 Subject: [PATCH 2/2] fix(macos): word the unknown-engine note on BT and ET as transaction control --- .../Sources/TableProSQLGrammar/SQLLexicalProfile.swift | 2 +- .../SQLCatalogEffectClassifierTests.swift | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift index 9415934219..caae688aab 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLLexicalProfile.swift @@ -82,7 +82,7 @@ public struct SQLLexicalProfile: Sendable, Hashable { .plsqlBlocks, .delimiterDirective, .unterminatedStatements, .terminatedMergeStatements, .sqlScriptBlocks, .cqlBatches, .atomicRoutineBodies, .triggerBodies, .statementsAsValues, .bracketedStatementSources, .pipeOperatorChains, .psqlScripts, - // An unknown engine reads `BT` and `ET` as names, not as the end of a transaction. + // An unknown engine reads `BT` and `ET` as names, not as transaction control. .transactionShortForms, ]) if seen.insert(unitless).inserted { diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift index 9b27290def..1e235f5f0f 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift @@ -59,7 +59,6 @@ struct SQLCatalogEffectClassifierTests { #expect(SQLCatalogEffectClassifier.effect(of: "ET", grammar: grammar("Teradata")).endsTransaction) } - // Only Teradata reads BT and ET as transaction control; elsewhere a lone word is a statement nothing knows. @Test("BT and ET are transaction control in Teradata's grammar alone") func transactionShortFormsAreTeradatas() { #expect(SQLCatalogEffectClassifier.effect(of: "et", grammar: grammar("Teradata")).endsTransaction)