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
@@ -1,7 +1,7 @@
import Foundation

// DuckDB reads a file named as a table. A quoted identifier, in `"` or a backtick, counts only with a `/`, a `.` or a
// `\`.
// `\`. So does a dotted name ending in a file extension, and a table function DuckDB does not keep inside the database.
public enum SQLFileNamedAsTable {
private static let tableIntroducers: Set<String> = [
"FROM", "JOIN", "USING", "TABLE", "DESCRIBE", "DESC", "SHOW", "SUMMARIZE", "PIVOT", "UNPIVOT",
Expand All @@ -16,6 +16,23 @@ public enum SQLFileNamedAsTable {
// `extract(year FROM '2026-01-01'::DATE)` separates arguments with FROM.
private static let functionsTakingFrom: Set<String> = ["EXTRACT", "TRIM", "SUBSTRING", "OVERLAY"]

// Only a FROM or JOIN list holds table functions; PIVOT's and USING's parentheses hold expressions.
private static let callingIntroducers: Set<String> = ["FROM", "JOIN"]

// An allowlist: an extension DuckDB loads on its own brings table functions no list here knows.
static let tableFunctionsInsideDatabase: Set<String> = [
"RANGE", "GENERATE_SERIES", "UNNEST", "REPEAT", "JSON_EACH", "JSON_TREE", "DUCKDB_COLUMNS",
"DUCKDB_CONSTRAINTS", "DUCKDB_DATABASES", "DUCKDB_DEPENDENCIES", "DUCKDB_FUNCTIONS", "DUCKDB_INDEXES",
"DUCKDB_KEYWORDS", "DUCKDB_SCHEMAS", "DUCKDB_SEQUENCES", "DUCKDB_SETTINGS", "DUCKDB_TABLES", "DUCKDB_TYPES",
"DUCKDB_VARIABLES", "DUCKDB_VIEWS", "PRAGMA_DATABASE_SIZE", "PRAGMA_PLATFORM", "PRAGMA_SHOW",
"PRAGMA_STORAGE_INFO", "PRAGMA_TABLE_INFO", "PRAGMA_VERSION",
]

// DuckDB joins a dotted table name with `.` and reads it as a file in the working folder.
static let fileExtensions: Set<String> = [
"CSV", "TSV", "GZ", "ZST", "PARQUET", "JSON", "JSONL", "NDJSON", "GEOJSON", "GEOJSONL", "DB", "DDB", "DUCKDB",
]

private static let nameContinuations = Set(".:&".utf16)

private static let pathMarks = CharacterSet(charactersIn: "/.\\")
Expand All @@ -31,6 +48,8 @@ public enum SQLFileNamedAsTable {
var separatesArgumentsWithFrom = false
var listsTables = false
var expects = Expectation.nothing
var introducer = ""
var nameParts = 0
}

public static func appears(in statement: String, grammar: SQLLexicalGrammar) -> Bool {
Expand All @@ -46,32 +65,53 @@ public enum SQLFileNamedAsTable {
break
case .word(let word) where tableIntroducers.contains(word):
levels[top].expects = .table
levels[top].introducer = word
levels[top].nameParts = 0
if word == "FROM" { levels[top].listsTables = true }
case .word(let word) where tableListEnders.contains(word) && expected != .table:
levels[top].listsTables = false
case .word("ONLY") where expected == .table:
case .word("ONLY") where expected == .table, .word("LATERAL") where expected == .table:
levels[top].expects = .table
case .word, .name:
case .word(let word):
guard expected == .table else { break }
levels[top].nameParts += 1
if levels[top].nameParts > 1, fileExtensions.contains(word) { return true }
levels[top].expects = .restOfName
case .name:
if expected == .table { levels[top].expects = .restOfName }
case .quoted:
guard expected == .table else { break }
if namesFile(at: token.location, in: source, grammar: grammar) { return true }
levels[top].nameParts += 1
let quotedName = identifier(at: token.location, in: source, grammar: grammar)?.uppercased()
if levels[top].nameParts > 1, let quotedName, fileExtensions.contains(quotedName) { return true }
levels[top].expects = .restOfName
case .open:
if expected == .restOfName, callingIntroducers.contains(levels[top].introducer),
!callsTableFunctionInsideDatabase(before: index, in: tokens, source: source, grammar: grammar) {
return true
}
// `FROM (notes JOIN 'payroll.csv' USING (id))` groups tables, and any other `(` starts afresh.
levels.append(
Parentheses(
separatesArgumentsWithFrom: callsFunctionTakingFrom(before: index, in: tokens, source: source),
expects: expected == .table ? .table : .nothing
expects: expected == .table ? .table : .nothing,
introducer: expected == .table ? levels[top].introducer : ""
)
)
case .close:
if top > 0 { levels.removeLast() }
case .comma:
if levels[top].listsTables { levels[top].expects = .table }
if levels[top].listsTables {
levels[top].expects = .table
levels[top].nameParts = 0
}
case .other:
let continuesName = nameContinuations.contains(source.character(at: token.location))
if expected == .restOfName, continuesName { levels[top].expects = .table }
let mark = source.character(at: token.location)
if expected == .restOfName, nameContinuations.contains(mark) {
levels[top].expects = .table
if mark != period { levels[top].nameParts = 0 }
}
case .terminator:
break
}
Expand Down Expand Up @@ -106,6 +146,33 @@ public enum SQLFileNamedAsTable {
return tableIntroducers.first(where: word.hasSuffix).map { .word($0) } ?? token.kind
}

// A name the scanner cannot read counts as unknown.
private static func callsTableFunctionInsideDatabase(
before index: Int,
in tokens: [TSQLTokens.Token],
source: NSString,
grammar: SQLLexicalGrammar
) -> Bool {
guard index >= 1 else { return false }
let callee = tokens[index - 1]
let name: String? = switch callee.kind {
case .word(let word): word
case .quoted: identifier(at: callee.location, in: source, grammar: grammar)?.uppercased()
default: nil
}
guard let name else { return false }
return name == "VALUES" || tableFunctionsInsideDatabase.contains(name)
}

private static func identifier(at location: Int, in source: NSString, grammar: SQLLexicalGrammar) -> String? {
guard [SqlLexer.doubleQuote, SqlLexer.backtick].contains(source.character(at: location)),
let span = SQLNonCodeSpan.span(at: location, in: source, grammar: grammar)
else {
return nil
}
return source.substring(with: NSRange(location: location + 1, length: max(0, span.contentEnd - location - 1)))
}

private static func namesFile(at location: Int, in source: NSString, grammar: SQLLexicalGrammar) -> Bool {
guard [SqlLexer.doubleQuote, SqlLexer.backtick].contains(source.character(at: location)),
let span = SQLNonCodeSpan.span(at: location, in: source, grammar: grammar)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import Foundation
import TableProPluginKit
import Testing

struct PluginReadOnlyModeTests {
@Test("A new context asks for no Read-Only mode and no byte limit")
func contextDefaults() {
let context = PluginStatementContext()

#expect(!context.enforcesReadOnly)
#expect(context.resultByteLimit == nil)
#expect(context == PluginStatementContext(enforcesReadOnly: false, resultByteLimit: nil))
}

@Test("The initializer sets both fields, and equality reads them")
func contextInitializerSetsTheFields() {
let context = PluginStatementContext(enforcesReadOnly: true, resultByteLimit: 4_096)

#expect(context.enforcesReadOnly)
#expect(context.resultByteLimit == 4_096)
#expect(context != PluginStatementContext())
#expect(PluginStatementContext(resultByteLimit: 1) != PluginStatementContext(resultByteLimit: 2))
}

@Test("The Read-Only capability is bit 24 and shares no bit with another capability")
func capabilityBit() {
let others: [PluginCapabilities] = [
.materializedViews, .foreignTables, .storedProcedures, .userFunctions, .alterTableDDL, .foreignKeyToggle,
.truncateTable, .multiSchema, .parameterizedQueries, .cancelQuery, .batchExecute, .transactions,
.userManagement, .schemaCompare, .dataCompare, .resultSetBatches, .cellExpressions, .uuidCellText,
.temporaryTables, .rowWrites, .repeatingPrimaryKey, .readOnlyStatements, .statementPolicy,
.columnDefaultMarks,
]

#expect(PluginCapabilities.enforcesReadOnlyMode.rawValue == 1 << 24)
for other in others {
#expect(other.isDisjoint(with: .enforcesReadOnlyMode), "\(other.rawValue)")
}
}

@Test("The default bounded read with a context runs the context-free one for a plain read")
func defaultForwardsAPlainRead() async throws {
let driver = BoundedReadDriver()
let context = PluginStatementContext(resultByteLimit: 1_024)

let result = try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 5, context: context)

#expect(result?.rows.count == 1)
#expect(driver.calls == [5])
}

@Test(
"The default bounded read with a context hands back what the context-free one cannot honor",
arguments: Restriction.allCases
)
func defaultRefusesARestrictedRead(restriction: Restriction) async throws {
let driver = BoundedReadDriver()

let result = try await driver.executeBoundedQuery(query: "SELECT 1", rowCap: 5, context: restriction.context)

#expect(result == nil)
#expect(driver.calls.isEmpty)
}

enum Restriction: CaseIterable, Sendable {
case enforcesReadOnly
case readOnly
case readsCatalog
case refusesDestructive
case refusesServerCode

var context: PluginStatementContext {
var context = PluginStatementContext()
switch self {
case .enforcesReadOnly: context.enforcesReadOnly = true
case .readOnly: context.readOnly = true
case .readsCatalog: context.readsCatalog = true
case .refusesDestructive: context.allowsDestructive = false
case .refusesServerCode: context.allowsServerCode = false
}
return context
}
}
}

private final class BoundedReadDriver: PluginDatabaseDriver, @unchecked Sendable {
let supportsSchemas = false
let supportsTransactions = false
let currentSchema: String? = nil
let serverVersion: String? = nil

private let lock = NSLock()
private var rowCaps: [Int] = []

var calls: [Int] {
lock.withLock { rowCaps }
}

func connect() async throws {}
func disconnect() {}
func ping() async throws {}

func execute(query: String) async throws -> PluginQueryResult {
PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0)
}

func executeBoundedQuery(query: String, rowCap: Int) async throws -> PluginQueryResult? {
lock.withLock { rowCaps.append(rowCap) }
return PluginQueryResult(
columns: ["one"],
columnTypeNames: ["INTEGER"],
rows: [[.text("1")]],
rowsAffected: 0,
executionTime: 0
)
}

func fetchTables(schema: String?) async throws -> [PluginTableInfo] { [] }
func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { [] }
func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { [] }
func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { [] }
func fetchTableDDL(table: String, schema: String?) async throws -> String { "" }
func fetchViewDefinition(view: String, schema: String?) async throws -> String { "" }
func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata {
PluginTableMetadata(tableName: table)
}
func fetchDatabases() async throws -> [String] { [] }
func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata {
PluginDatabaseMetadata(name: database)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ struct SQLFileNamedAsTableTests {
#expect(namesFile(sql))
}

@Test("A table function DuckDB does not keep inside the database counts as one", arguments: [
"SELECT * FROM quack_serve('x')",
"SELECT * FROM odbc_query('DSN=x', 'DROP TABLE t')",
"SELECT * FROM notes, cloudformation_create_stack('x')",
"SELECT * FROM notes JOIN avro_metadata('x') ON true",
"SELECT * FROM notes, LATERAL quack_query('x')",
"SELECT * FROM (FROM quack_serve('x'))",
"SELECT * FROM system.main.quack_serve('x')",
"SELECT * FROM \"quack_serve\"('x')",
])
func unknownTableFunctions(sql: String) {
#expect(namesFile(sql))
}

@Test("A dotted name ending in a file extension is a file in the working folder", arguments: [
"SELECT * FROM finances.csv",
"SELECT * FROM notes.\"csv\"",
"SELECT * FROM backup.duckdb",
"SELECT * FROM notes, data.parquet",
])
func dottedFileNames(sql: String) {
#expect(namesFile(sql))
}

@Test("A FROM that runs on from a number or an operator still lists tables", arguments: [
"SELECT 1FROM 'payroll.csv'",
"SELECT 1e5FROM 'payroll.csv'",
Expand Down Expand Up @@ -122,6 +146,11 @@ struct SQLFileNamedAsTableTests {
"COMMENT ON TABLE notes IS 'a.csv'",
"CREATE TABLE notes (path VARCHAR DEFAULT 'a.csv')",
"SELECT _from, _desc, 'a.csv' FROM notes",
"SELECT * FROM main.range(1, 10)",
"SELECT * FROM duckdb_tables()",
"PIVOT notes ON year USING sum(salary)",
"SELECT * FROM notes JOIN tags ON f(tags.id) = notes.id",
"SELECT * FROM notes.csv_rows",
])
func valuesAndTableNames(sql: String) {
#expect(!namesFile(sql))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation
@testable import TableProSQLGrammar
import Testing

// The core keeps its own copy of these names for its MCP server, and both test suites compare theirs with one file.
struct SQLOutsideReachNamesTests {
private static let sharedNames = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("core/crates/core/tests/fixtures/outside_reach.json")

@Test("Each list names what the core lists")
func listsMatchTheSharedNames() throws {
let data = try Data(contentsOf: Self.sharedNames)
let shared = try JSONDecoder().decode([String: [String]].self, from: data)
.mapValues { Set($0.map { $0.uppercased() }) }
let duckDB = SQLStatementTiering.duckDB
let clickHouse = SQLStatementTiering.clickHouse
let listed: [String: Set<String>] = [
"filesystem_word_runs": Set(SQLStatementTiering.filesystemWordRuns.map { $0.joined(separator: " ") }),
"filesystem_names": SQLStatementTiering.filesystemNames,
"filesystem_or_code_keywords": SQLStatementTiering.filesystemOrCodeKeywords,
"filesystem_functions": SQLStatementTiering.filesystemFunctions,
"duckdb_functions": SQLStatementTiering.functionsReachingOutside[duckDB] ?? [],
"duckdb_families": Set(SQLStatementTiering.functionFamiliesReachingOutside[duckDB] ?? []),
"clickhouse_functions": SQLStatementTiering.functionsReachingOutside[clickHouse] ?? [],
"clickhouse_families": Set(SQLStatementTiering.functionFamiliesReachingOutside[clickHouse] ?? []),
]
#expect(listed == shared)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable {
public let scopes: Set<MCPScope>
public let connectionAccess: ConnectionAccess
public let metadata: MCPPrincipalMetadata
// Nil means every tool the scopes allow.
public let toolAllowlist: Set<String>?

public init(
tokenFingerprint: String,
tokenId: UUID,
scopes: Set<MCPScope>,
connectionAccess: ConnectionAccess = .all,
metadata: MCPPrincipalMetadata
metadata: MCPPrincipalMetadata,
toolAllowlist: Set<String>? = nil
) {
self.tokenFingerprint = tokenFingerprint
self.tokenId = tokenId
self.scopes = scopes
self.connectionAccess = connectionAccess
self.metadata = metadata
self.toolAllowlist = toolAllowlist
}

public static let inAppAssistantTokenId = UUID(uuid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xA1, 0x1A))
Expand Down Expand Up @@ -69,6 +73,10 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable {
scopes.contains(scope)
}

public func allowsTool(_ name: String) -> Bool {
toolAllowlist?.contains(name) ?? true
}

public func missingScopes(from required: Set<MCPScope>) -> Set<MCPScope> {
required.subtracting(scopes)
}
Expand All @@ -86,6 +94,7 @@ public struct MCPPrincipal: Sendable, Equatable, Hashable {
&& lhs.scopes == rhs.scopes
&& lhs.connectionAccess == rhs.connectionAccess
&& lhs.metadata == rhs.metadata
&& lhs.toolAllowlist == rhs.toolAllowlist
}

public func hash(into hasher: inout Hasher) {
Expand Down
Loading
Loading