Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,135 @@ struct DerivedTableParser {
return target.reference
}

// MARK: - Join scope

struct JoinSource: Equatable {
let table: String
let schema: String?
let alias: String?
// Whether the name the statement exposes it by, the alias or else the table, was quoted.
let isQuoted: Bool
}

private struct FromItemNames {
let sources: [JoinSource]
// One stored table named directly, not a join group, a derived table or a table function.
let isTable: Bool
let end: Int
}

// Walking back from an ON, a clause word means it belongs to no FROM list, as in CREATE INDEX, GRANT or a trigger.
private static let joinScopeBreakers: Set<String> = [
"SELECT", "WHERE", "GROUP", "HAVING", "ORDER", "LIMIT", "OFFSET", "FETCH", "SET", "VALUES", "RETURNING", "INTO",
"UNION", "INTERSECT", "EXCEPT", "MINUS", "WINDOW", "QUALIFY", "INSERT", "DELETE", "MERGE", "CREATE", "ALTER",
"DROP", "GRANT", "REVOKE"
]

// Set only when the ON is the token right before `cursor` and follows a table joined by JOIN. The earlier tables
// are the ones before it since the last comma of its FROM list or join group, which is all such an ON may name.
func joinScope(in stream: SQLTokenStream, cursor: Int) -> (joined: JoinSource, earlier: [JoinSource])? {
guard let on = stream.tokens.lastIndex(where: { $0.range.location < cursor }), stream.isWord(on, "ON"),
stream.kind(on - 1) != .comma, !stream.isWord(on - 1, in: Self.joinWords),
let start = joinListStart(before: on, cursor: cursor, in: stream) else { return nil }
var earlier: [JoinSource] = []
var isJoined = false
var index = start
while index < on {
let item = fromItemNames(at: index, to: on, in: stream)
let next = nextFromItem(after: item.end, to: on, in: stream).index
guard next < on else {
guard isJoined, item.isTable, let joined = item.sources.first,
firstTopLevel(["ON"], from: item.end, to: on, in: stream) == nil else { return nil }
return (joined, Array(earlier.reversed()))
}
if stream.kind(next - 1) == .comma {
earlier = []
isJoined = false
} else {
earlier += item.sources
isJoined = stream.isWord(next - 1, "JOIN") || stream.isWord(next - 1, "STRAIGHT_JOIN")
}
index = next
}
return nil
}

// The first item of the FROM list holding `on` at the cursor's query level, or of the join group around it.
private func joinListStart(before on: Int, cursor: Int, in stream: SQLTokenStream) -> Int? {
var floor = 0
if let open = stream.enclosingParens(before: cursor).last {
guard isStatementBody(open, in: stream) else {
return opensFromItem(open, in: stream) ? open + 1 : nil
}
floor = open + 1
}
var index = on - 1
while index >= floor {
let token = stream.tokens[index]
if token.kind == .closeParen, token.partner >= floor {
index = token.partner - 1
continue
}
// After a dot a word is a name, as in `a.set`.
if token.kind == .word, stream.kind(index - 1) != .dot {
switch token.upper {
case "FROM" where !stream.isWord(index - 1, "DISTINCT"), "UPDATE":
return index + 1
case "USING" where stream.kind(index + 1) != .openParen:
return index + 1
case let word where Self.joinScopeBreakers.contains(word):
return nil
default:
break
}
}
index -= 1
}
return nil
}

private func fromItemNames(at start: Int, to end: Int, in stream: SQLTokenStream) -> FromItemNames {
var index = start
while stream.isWord(index, "LATERAL") || stream.isWord(index, "ONLY") { index += 1 }
guard index < end else { return FromItemNames(sources: [], isTable: false, end: end) }
if stream.kind(index) == .openParen {
let close = stream.after(index) - 1
guard !isQueryBody(index, in: stream) else {
return FromItemNames(sources: [], isTable: false, end: alias(after: close, in: stream)?.end ?? close + 1)
}
return FromItemNames(sources: groupNames(from: index + 1, to: min(close, end), in: stream), isTable: false, end: close + 1)
}
guard let path = path(at: index, to: end, in: stream) else {
return FromItemNames(sources: [], isTable: false, end: index + 1)
}
index = path.end
if stream.kind(index) == .openParen {
let close = stream.after(index) - 1
return FromItemNames(sources: [], isTable: false, end: alias(after: close, in: stream)?.end ?? close + 1)
}
if stream.kind(index) == .star { index += 1 }
var source = JoinSource(table: path.table, schema: path.schema, alias: nil, isQuoted: path.isQuoted)
if let alias = alias(after: index - 1, in: stream) {
let aliasIndex = stream.isWord(index, "AS") ? index + 1 : index
source = JoinSource(
table: path.table, schema: path.schema, alias: alias.name, isQuoted: stream.kind(aliasIndex) == .quoted
)
index = alias.end
}
return FromItemNames(sources: [source], isTable: true, end: index)
}

private func groupNames(from start: Int, to end: Int, in stream: SQLTokenStream) -> [JoinSource] {
var names: [JoinSource] = []
var index = start
while index < end {
let item = fromItemNames(at: index, to: end, in: stream)
names += item.sources
index = nextFromItem(after: item.end, to: end, in: stream).index
}
return names
}

// MARK: - Definitions

// One query level. A CTE of its WITH list is in scope from the next definition on, and only a level that holds the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ public protocol SQLCompletionCatalog: Sendable {
func allowedValues(forColumn column: String, in references: [TableReference]) async -> [String]
func allColumnsInScope(for references: [TableReference]) async -> [SQLCompletionItem]
func allColumnsFromCachedTables() async -> [SQLCompletionItem]
// The table's own keys, none pointing outside its schema.
func foreignKeys(of tableName: String, schema: String?) async -> [SQLCompletionForeignKey]
// For a surface that spells nothing after ranking; nil leaves the name as stored and written.
func writtenText(of identifier: SQLCompletionIdentifier) -> String?
}

public extension SQLCompletionCatalog {
func tableCompletionItems(inSchema schema: String, isQuoted: Bool) async -> [SQLCompletionItem] {
await tableCompletionItems(inSchema: schema)
}

func foreignKeys(of tableName: String, schema: String?) async -> [SQLCompletionForeignKey] { [] }

func writtenText(of identifier: SQLCompletionIdentifier) -> String? { nil }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum SQLCompletionKind: String, CaseIterable, Sendable {
case alias
case `operator`
case favorite // Saved SQL favorite (keyword expansion)
case joinCondition

public var iconName: String {
switch self {
Expand All @@ -22,12 +23,14 @@ public enum SQLCompletionKind: String, CaseIterable, Sendable {
case .alias: return "a.circle.fill"
case .operator: return "equal.circle.fill"
case .favorite: return "star.circle.fill"
case .joinCondition: return "link.circle.fill"
}
}

/// Lower sorts first within the same context.
public var basePriority: Int {
switch self {
case .joinCondition: return 0
case .favorite: return 50
case .column: return 100
case .table: return 200
Expand Down Expand Up @@ -57,6 +60,8 @@ public struct SQLCompletionItem: Identifiable, Hashable, Sendable {
public let caseFolding: SQLCompletionCaseFolding
// Set on a name the catalog stores, so the connection's identifier rules can spell it.
public let identifier: SQLCompletionIdentifier?
// The `a.x = b.y` pairs of a join condition, so each name is spelled like any other.
public let columnPairs: [SQLCompletionColumnPair]
public var matchedRanges: [Range<Int>] = []
public var fuzzyPenalty: Int = 0
// `o.total` names a table and a column; a column literally named `order.total` is one name.
Expand All @@ -71,7 +76,8 @@ public struct SQLCompletionItem: Identifiable, Hashable, Sendable {
sortPriority: Int? = nil,
filterText: String? = nil,
caseFolding: SQLCompletionCaseFolding = .fixed,
identifier: SQLCompletionIdentifier? = nil
identifier: SQLCompletionIdentifier? = nil,
columnPairs: [SQLCompletionColumnPair] = []
) {
self.id = UUID()
self.label = label
Expand All @@ -83,6 +89,7 @@ public struct SQLCompletionItem: Identifiable, Hashable, Sendable {
self.filterText = filterText ?? label.lowercased()
self.caseFolding = caseFolding
self.identifier = identifier
self.columnPairs = columnPairs
}

private init(recasing item: SQLCompletionItem, label: String, insertText: String) {
Expand All @@ -96,6 +103,7 @@ public struct SQLCompletionItem: Identifiable, Hashable, Sendable {
self.filterText = item.filterText
self.caseFolding = item.caseFolding
self.identifier = item.identifier
self.columnPairs = item.columnPairs
self.matchedRanges = item.matchedRanges
self.fuzzyPenalty = item.fuzzyPenalty
self.isQualifiedReference = item.isQualifiedReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,27 +258,9 @@ public final class SQLCompletionProvider {
}

case .on:
items += await joinConditionItems(for: context.joinScope)
items += await allowedValueItems(for: context)
items += await columnItems(for: context.tableReferences)
for ref in context.tableReferences {
let qualifier = ref.alias ?? ref.tableName
let cols = await catalog?.columnCompletionItems(for: ref.tableName, schema: ref.schema) ?? []
for col in cols {
var item = SQLCompletionItem(
label: "\(qualifier).\(col.label)",
kind: .column,
insertText: "\(qualifier).\(col.label)",
detail: col.detail,
documentation: "Column from \(ref.tableName)",
sortPriority: 80,
identifier: col.identifier.map {
SQLCompletionIdentifier([Self.qualifierPart(for: ref)] + $0.parts)
}
)
item.isQualifiedReference = true
items.append(item)
}
}
items += await qualifiedColumnItems(for: context.tableReferences)
items += SQLKeywords.operatorItems()
items += dialectOperatorItems()
items += filterKeywords([
Expand Down Expand Up @@ -578,6 +560,85 @@ public final class SQLCompletionProvider {
return await catalog?.allColumnsInScope(for: resolved) ?? []
}

// A catalog that already qualifies a column once two tables are in scope keeps its own item, so the name is
// offered once and with the catalog's spelling.
private func qualifiedColumnItems(for references: [TableReference]) async -> [SQLCompletionItem] {
var items = await columnItems(for: references)
var indexByLabel: [String: Int] = [:]
for (index, item) in items.enumerated() where item.kind == .column && indexByLabel[item.label] == nil {
indexByLabel[item.label] = index
}
for ref in references {
let cols = await catalog?.columnCompletionItems(for: ref.tableName, schema: ref.schema) ?? []
for col in cols {
let label = "\(ref.identifier).\(col.label)"
if let index = indexByLabel[label] {
items[index].sortPriority = min(items[index].sortPriority, Self.qualifiedColumnPriority)
continue
}
let parts = [Self.qualifierPart(for: ref)] + (col.identifier?.parts ?? [.catalog(col.label)])
var item = SQLCompletionItem(
label: label,
kind: .column,
insertText: catalog?.writtenText(of: SQLCompletionIdentifier(parts)) ?? "\(ref.identifier).\(col.insertText)",
detail: col.detail,
documentation: "Column from \(ref.tableName)",
sortPriority: Self.qualifiedColumnPriority,
identifier: col.identifier.map { SQLCompletionIdentifier([Self.qualifierPart(for: ref)] + $0.parts) }
)
item.isQualifiedReference = true
indexByLabel[label] = items.count
items.append(item)
}
}
return items
}

private static let qualifiedColumnPriority = 80

private func joinConditionItems(for scope: SQLJoinScope?) async -> [SQLCompletionItem] {
guard let scope, let catalog else { return [] }
let joined = await joinSide(scope.joined, in: catalog)
var earlier: [SQLJoinConditions.Side] = []
for reference in scope.earlier {
earlier.append(await joinSide(reference, in: catalog))
}
var seen = Set<String>()
return SQLJoinConditions.conditions(joined: joined, earlier: earlier).compactMap { condition in
let item = joinConditionItem(condition, joined: scope.joined, catalog: catalog)
return seen.insert(item.label).inserted ? item : nil
}
}

private func joinSide(_ reference: TableReference, in catalog: any SQLCompletionCatalog) async -> SQLJoinConditions.Side {
SQLJoinConditions.Side(
reference: reference,
keys: await catalog.foreignKeys(of: reference.tableName, schema: reference.schema),
columns: await catalog.columnCompletionItems(for: reference.tableName, schema: reference.schema).map(\.label)
)
}

private func joinConditionItem(
_ condition: SQLJoinConditions.Condition,
joined: TableReference,
catalog: any SQLCompletionCatalog
) -> SQLCompletionItem {
let pairs = condition.columns.map { columns in
SQLCompletionColumnPair(
left: SQLCompletionIdentifier([Self.qualifierPart(for: condition.earlier), .catalog(columns.earlier)]),
right: SQLCompletionIdentifier([Self.qualifierPart(for: joined), .catalog(columns.joined)])
)
}
let written = { (name: SQLCompletionIdentifier) in catalog.writtenText(of: name) ?? name.plainText }
return SQLCompletionItem(
label: SQLCompletionColumnPair.condition(pairs.map { (left: $0.left.plainText, right: $0.right.plainText) }),
kind: .joinCondition,
insertText: SQLCompletionColumnPair.condition(pairs.map { (left: written($0.left), right: written($0.right)) }),
detail: condition.constraintName.isEmpty ? nil : condition.constraintName,
columnPairs: pairs
)
}

// ORDER BY sorts by an output alias before a source column of the same name, so the alias leads.
private func outputAliasItems(
_ spellings: [String], besides columns: [SQLCompletionItem], insideQuotes: Bool
Expand Down Expand Up @@ -843,7 +904,7 @@ public final class SQLCompletionProvider {
}
case .select, .where_, .and, .on, .having, .groupBy, .orderBy,
.returning, .using, .window:
if item.kind == .column {
if item.kind == .column || item.kind == .joinCondition {
score -= 200
}
case .set, .insertColumns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public struct SQLContext: Sendable {
public private(set) var quotedName: SQLQuotedName?
public let outputAliases: [String] // SELECT-list aliases an ORDER BY at the cursor may name, as written
public let targetTable: TableReference? // The table the INSERT, UPDATE or MERGE at the cursor writes
public let joinScope: SQLJoinScope? // Right after the ON of a join

public init(
clauseType: SQLClauseType,
Expand All @@ -148,7 +149,8 @@ public struct SQLContext: Sendable {
comparisonColumn: String? = nil,
quotedName: SQLQuotedName? = nil,
outputAliases: [String] = [],
targetTable: TableReference? = nil
targetTable: TableReference? = nil,
joinScope: SQLJoinScope? = nil
) {
self.clauseType = clauseType
self.prefix = prefix
Expand All @@ -167,6 +169,7 @@ public struct SQLContext: Sendable {
self.quotedName = quotedName
self.outputAliases = outputAliases
self.targetTable = targetTable
self.joinScope = joinScope
}

public func replacingTableReferences(_ references: [TableReference]) -> SQLContext {
Expand Down Expand Up @@ -390,6 +393,11 @@ public final class SQLContextAnalyzer {
writtenTable($0, among: tableReferences)
}
: nil
let joinScope = resolution.clause == .on && dotPrefix == nil && quotedName == nil && !isCastTarget
? Self.derivedTableParser.joinScope(in: tokens, cursor: clauseEnd).flatMap {
SQLJoinScope($0, references: tableReferences, cteNames: cteNames)
}
: nil

return SQLContext(
clauseType: isCastTarget ? .castTarget : resolution.clause,
Expand All @@ -408,7 +416,8 @@ public final class SQLContextAnalyzer {
comparisonColumn: comparisonColumn,
quotedName: quotedName,
outputAliases: outputAliases,
targetTable: targetTable
targetTable: targetTable,
joinScope: joinScope
)
}

Expand Down
Loading
Loading