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 @@ -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))
}
Expand All @@ -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)
Expand Down Expand Up @@ -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<String> = ["COMMIT", "END", "ROLLBACK", "ABORT", "ET"]
private static let transactionEndKeywords: Set<String> = ["COMMIT", "END", "ROLLBACK", "ABORT"]

// PostgreSQL's `DISCARD TEMP` and `DISCARD ALL` drop the session's temporary tables.
private static let discardedTableTargets: Set<String> = ["TEMP", "TEMPORARY", "ALL"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 transaction control.
.transactionShortForms,
])
if seen.insert(unitless).inserted {
result.append(unitless)
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ struct SQLCatalogEffectClassifierTests {
#expect(SQLCatalogEffectClassifier.effect(of: "ET", grammar: grammar("Teradata")).endsTransaction)
}

@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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 1 addition & 4 deletions apple/TablePro/Core/Services/Execution/ManualCommit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading