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
2 changes: 1 addition & 1 deletion apple/Packages/TableProCore/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ let package = Package(
),
.testTarget(
name: "TableProWeaviateClientTests",
dependencies: ["TableProWeaviateClient", "TableProTLSTestFixtures"],
dependencies: ["TableProWeaviateClient", "TableProWeaviateCore", "TableProTLSTestFixtures"],
path: "Tests/TableProWeaviateClientTests"
),
.testTarget(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,11 @@ extension SQLBareNameCalls {
blocks[body].sources.append(input.named(variable))
}
}
expression(
(index + 1)..<close, block: body, commonTables: commonTables, ordersBy: false,
readsAfterAs: word == "MATCH_RECOGNIZE"
)
if word == "MATCH_RECOGNIZE" {
rowPatternClause((index + 1)..<close, block: body, commonTables: commonTables)
} else {
expression((index + 1)..<close, block: body, commonTables: commonTables, ordersBy: false)
}
index = close + 1
}
state = .afterTable
Expand Down Expand Up @@ -533,6 +534,59 @@ extension SQLBareNameCalls {
return (last + 1, append(.table(table, alias: nil), to: block))
}

// As Oracle 23ai runs MATCH_RECOGNIZE (measured): PARTITION BY, ORDER BY, each MEASURES expression and each DEFINE
// condition hold names. A MEASURES alias, a DEFINE variable, the row pattern, SUBSET and the clause's own words
// are not. A clause that does not read in Oracle's order is read whole, every name after AS included.
private mutating func rowPatternClause(_ range: Range<Int>, block: Int, commonTables: [String: CommonTable]) {
guard let first = range.first,
let clause = SQLRowPatternClause.read(tokens, body: range, text: code.code as NSString)
else {
expression(range, block: block, commonTables: commonTables, ordersBy: false, readsAfterAs: true)
return
}
let depth = tokens[first].depth
for list in [clause.partitionBy, clause.orderBy].compactMap({ $0 }) {
expression(list, block: block, commonTables: commonTables, ordersBy: false)
}
for item in clause.measures.map({ items($0, depth: depth) }) ?? [] {
let read = measureExpression(item, depth: depth)
expression(read, block: block, commonTables: commonTables, ordersBy: false, inRowPattern: true)
}
for item in items(clause.define, depth: depth) {
let read = definedCondition(item, depth: depth)
expression(read, block: block, commonTables: commonTables, ordersBy: false, inRowPattern: true)
}
}

private func items(_ range: Range<Int>, depth: Int) -> [Range<Int>] {
var items: [Range<Int>] = []
var start = range.lowerBound
for index in range.lowerBound...range.upperBound
where index == range.upperBound || (tokens[index].kind == .comma && tokens[index].depth == depth) {
if start < index { items.append(start..<index) }
start = index + 1
}
return items
}

// `expr [AS] alias`: the alias names the measure.
private func measureExpression(_ item: Range<Int>, depth: Int) -> Range<Int> {
if let alias = item.first(where: { tokens[$0].depth == depth && word(at: $0) == "AS" }) {
return item.lowerBound..<alias
}
let last = item.upperBound - 1
guard last > item.lowerBound, identifier(at: last) != nil, endsOperand(at: last - 1) else { return item }
return item.lowerBound..<last
}

// `variable AS condition`.
private func definedCondition(_ item: Range<Int>, depth: Int) -> Range<Int> {
guard let condition = item.first(where: { tokens[$0].depth == depth && word(at: $0) == "AS" }) else {
return item
}
return (condition + 1)..<item.upperBound
}

// The names `PATTERN (...)` and `SUBSET x = (...)` give.
private func patternVariables(_ range: Range<Int>) -> [String] {
var names: [String] = []
Expand Down Expand Up @@ -586,13 +640,15 @@ extension SQLBareNameCalls {

// MARK: - Expressions

// `readsAfterAs`: MATCH_RECOGNIZE's `DEFINE v AS <condition>`, where AS leads an expression, not a type.
// `readsAfterAs`: a MATCH_RECOGNIZE clause read whole, where AS also leads a DEFINE condition. `inRowPattern`: a
// MATCH_RECOGNIZE measure or condition, where RUNNING and FINAL before a call are the clause's own words.
private mutating func expression(
_ range: Range<Int>,
block: Int,
commonTables: [String: CommonTable],
ordersBy: Bool,
readsAfterAs: Bool = false
readsAfterAs: Bool = false,
inRowPattern: Bool = false
) {
var index = range.lowerBound
let end = range.upperBound
Expand All @@ -605,7 +661,10 @@ extension SQLBareNameCalls {
if startsQuery(inner) {
_ = queryExpression(inner, parent: block, commonTables: commonTables)
} else {
expression(inner, block: block, commonTables: commonTables, ordersBy: false, readsAfterAs: readsAfterAs)
expression(
inner, block: block, commonTables: commonTables, ordersBy: false,
readsAfterAs: readsAfterAs, inRowPattern: inRowPattern
)
}
index = close + 1
afterAs = false
Expand All @@ -616,6 +675,11 @@ extension SQLBareNameCalls {
index += 1
continue
}
if inRowPattern, token.word == "RUNNING" || token.word == "FINAL", index + 2 < end,
identifier(at: index + 1) != nil, tokens[index + 2].kind == .open {
index += 1
continue
}
guard let name = identifier(at: index) else {
afterAs = false
index += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public enum SQLReadOnlyFunctionCalls {
"BOOL_AND", "BOOL_OR", "EVERY", "POW", "LOG10", "PI", "RANDOM",
]

// The engines here that read MATCH_RECOGNIZE; DM8 answers it with a syntax error. Measured on Oracle 23ai: in its
// MEASURES and DEFINE these call the clause's own navigation, never a user function of that name, and
// `PATTERN (...)` is a row pattern. Elsewhere, a query nested in the clause included, each is a user's name.
static let rowPatternEngines: Set<String> = ["Oracle"]
static let rowPatternFunctions: Set<String> = ["PREV", "NEXT", "FIRST", "LAST", "CLASSIFIER", "MATCH_NUMBER"]

private static let oracleFamily: Set<String> = ["Oracle", "Dameng"]
private static let mysqlFamily: Set<String> = ["MySQL", "MariaDB", "OceanBase", "TiDB", "StarRocks", "Databend"]
private static let postgresFamily: Set<String> = ["PostgreSQL", "CockroachDB", "PGlite", "KingbaseES", "Redshift"]
Expand Down Expand Up @@ -180,14 +186,21 @@ public enum SQLReadOnlyFunctionCalls {
) -> Bool {
let typeNames = Set(typeNames.map { $0.uppercased() })
let known = builtins(forDatabaseTypeId: databaseTypeId)
let readsRowPatterns = rowPatternEngines.contains(databaseTypeId)
let tiering = SQLStatementTiering(databaseTypeId: databaseTypeId, readings: readings, request: nil)
return readings.all.contains { grammar in
SQLStatementClassifier.statements(of: sql, grammar: grammar).contains { statement in
// SurrealQL's built-ins are namespaced, `time::now()`; only a stored body can write.
if databaseTypeId == SQLStatementTiering.surrealDB {
return tiering.callsSurrealStoredCode(StatementCode(statement, grammar: grammar))
}
return statementCallsUnlistedFunction(statement, grammar: grammar, builtins: known, typeNames: typeNames)
return statementCallsUnlistedFunction(
statement,
grammar: grammar,
builtins: known,
typeNames: typeNames,
readsRowPatterns: readsRowPatterns
)
}
}
}
Expand All @@ -196,7 +209,8 @@ public enum SQLReadOnlyFunctionCalls {
_ statement: String,
grammar: SQLLexicalGrammar,
builtins: Set<String>,
typeNames: Set<String>
typeNames: Set<String>,
readsRowPatterns: Bool
) -> Bool {
// A plain EXPLAIN plans its statement without running it.
if let explain = SQLExplainStatement.read(statement, grammar: grammar) {
Expand All @@ -209,8 +223,19 @@ public enum SQLReadOnlyFunctionCalls {
}
let source = statement as NSString
let code = SQLCodeProjection.code(of: statement, grammar: grammar) as NSString
let calls = SQLFunctionCallScanner.calls(in: source, code: code)
var quoted: [Range<Int>]?
return SQLFunctionCallScanner.calls(in: source, code: code).contains { call in
func site(of call: SQLFunctionCallScanner.Call, nameStart: Int) -> CallSite {
let spans = quoted ?? quotedSpans(in: source, grammar: grammar)
quoted = spans
return CallSite(code: code, quotedSpans: spans, nameStart: nameStart, parenthesis: call.parenthesis)
}
let rowPatterns = readsRowPatterns
? RowPatternClauses(source: source, code: code, grammar: grammar, calls: calls) {
site(of: $0, nameStart: $1).matches(.afterOperand)
}
: nil
return calls.contains { call in
guard case .named(let name, let start) = call.callee,
!quoteOpeners.contains(source.character(at: start))
else { return true }
Expand All @@ -224,11 +249,86 @@ public enum SQLReadOnlyFunctionCalls {
break
}
if builtins.contains(word) || typeNames.contains(word) || reservedWords.contains(word) { return false }
if rowPatterns?.ownsCall(named: word, at: start) == true { return false }
guard let rules = syntax[word] else { return true }
let spans = quoted ?? quotedSpans(in: source, grammar: grammar)
quoted = spans
let site = CallSite(code: code, quotedSpans: spans, nameStart: start, parenthesis: call.parenthesis)
return !rules.contains { site.matches($0) }
let callSite = site(of: call, nameStart: start)
return !rules.contains { callSite.matches($0) }
}
}

// What each MATCH_RECOGNIZE clause holds: its row pattern, its MEASURES and DEFINE, where navigation is the clause's
// own, and the queries nested in it, which that navigation does not reach. A clause `SQLRowPatternClause` cannot read
// owns nothing.
private struct RowPatternClauses {
private enum Kind {
case clause
case navigation
case pattern
case query
}

private var groups: [(range: Range<Int>, kind: Kind)] = []
private var owned: Set<Int> = []

init(
source: NSString,
code: NSString,
grammar: SQLLexicalGrammar,
calls: [SQLFunctionCallScanner.Call],
opensClause: (SQLFunctionCallScanner.Call, Int) -> Bool
) {
let openers = calls.filter { call in
guard case .named(let name, let start) = call.callee else { return false }
return name.uppercased() == "MATCH_RECOGNIZE" && opensClause(call, start)
}
guard !openers.isEmpty else { return }
let tokens = TSQLTokens.read(source, code: code, grammar: grammar)
for call in openers {
guard case .named(_, let start) = call.callee,
let open = tokens.firstIndex(where: { $0.kind == .open && $0.location == call.parenthesis }),
let close = Self.close(of: open, in: tokens),
let clause = SQLRowPatternClause.read(tokens, body: (open + 1)..<close, text: code)
else { continue }
owned.insert(start)
owned.insert(tokens[clause.patternKeyword].location)
let end = tokens[close].location
groups.append(((call.parenthesis + 1)..<end, .clause))
let patternOpen = tokens[clause.patternKeyword + 1].location
groups.append(((patternOpen + 1)..<tokens[clause.pattern.upperBound].location, .pattern))
for section in [clause.measures, clause.define].compactMap({ $0 }) where !section.isEmpty {
let stop = section.upperBound < close ? tokens[section.upperBound].location : end
groups.append((tokens[section.lowerBound].location..<stop, .navigation))
}
}
// `((SELECT ...) UNION ALL SELECT ...)` is a query too, so a run of parentheses is skipped first.
for (index, token) in tokens.enumerated() where token.kind == .open {
let first = tokens[index...].firstIndex { $0.kind != .open }
guard let word = first.flatMap({ tokens[$0].word }), word == "SELECT" || word == "WITH",
let close = Self.close(of: index, in: tokens)
else { continue }
groups.append(((token.location + 1)..<tokens[close].location, .query))
}
}

// The clause itself, its `PATTERN (`, anything in its row pattern, and navigation in MEASURES or DEFINE.
func ownsCall(named word: String, at start: Int) -> Bool {
if owned.contains(start) { return true }
switch kind(at: start) {
case .pattern:
return true
case .navigation:
return SQLReadOnlyFunctionCalls.rowPatternFunctions.contains(word)
case .clause, .query, nil:
return false
}
}

private func kind(at position: Int) -> Kind? {
groups.filter { $0.range.contains(position) }.max { $0.range.lowerBound < $1.range.lowerBound }?.kind
}

private static func close(of open: Int, in tokens: [TSQLTokens.Token]) -> Int? {
((open + 1)..<tokens.count).first { tokens[$0].kind == .close && tokens[$0].depth == tokens[open].depth }
}
}

Expand Down
Loading
Loading