Skip to content

Commit 59d265e

Browse files
committed
Fixed cardinality inferrer using schema and not env
1 parent 662f17f commit 59d265e

5 files changed

Lines changed: 23 additions & 32 deletions

File tree

Sources/Compiler/Compiler.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ fileprivate struct CompilerWithSource {
111111
var typeChecker = StmtTypeChecker(schema: schema, pragmas: pragmas.featherPragmas)
112112
let (parameters, type) = typeChecker.signature(for: stmt)
113113

114-
var cardinalityInferer = CardinalityInferrer(schema: schema)
114+
// Note: Make sure to pass env from type checker to make sure all is imported
115+
var cardinalityInferer = CardinalityInferrer(env: typeChecker.env)
115116
let cardinality = cardinalityInferer.cardinality(for: stmt)
116117

117118
let uniqueParameters = uniquify(parameters: parameters)

Sources/Compiler/Environment.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct Environment {
110110
return detachedValues[named].count > 0
111111
}
112112

113-
subscript(function name: Substring, argCount argCount: Int) -> TypeScheme? {
113+
func resolve(function name: Substring, argCount: Int) -> TypeScheme? {
114114
// TODO: Move this out of the env
115115
guard let scheme = Builtins.functions[name],
116116
case let .fn(params, ret) = scheme.type else { return nil }
@@ -132,7 +132,7 @@ struct Environment {
132132
)
133133
}
134134

135-
subscript(prefix op: Operator) -> TypeScheme? {
135+
func resolve(prefix op: Operator) -> TypeScheme? {
136136
return switch op {
137137
case .plus: Builtins.pos
138138
case .minus: Builtins.negate
@@ -141,7 +141,7 @@ struct Environment {
141141
}
142142
}
143143

144-
subscript(infix op: Operator) -> TypeScheme? {
144+
func resolve(infix op: Operator) -> TypeScheme? {
145145
return switch op {
146146
case .in, .not(.in): Builtins.in
147147
case .plus, .minus, .multiply, .divide, .bitwuseOr,
@@ -161,7 +161,7 @@ struct Environment {
161161
}
162162
}
163163

164-
subscript(postfix op: Operator) -> TypeScheme? {
164+
func resolve(postfix op: Operator) -> TypeScheme? {
165165
return switch op {
166166
case .collate: Builtins.concatOp
167167
case .escape: Builtins.escape

Sources/Compiler/QualifiedName.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ public struct QualifiedName: Hashable, Sendable, CustomStringConvertible {
2020
self.schema = schema
2121
}
2222

23-
@_disfavoredOverload
24-
public init(name: Substring, schema: Substring?) {
25-
self.name = name
26-
self.schema = schema.map(SchemaName.init) ?? nil
27-
}
28-
2923
public var description: String {
3024
return if let schema {
3125
"\(schema).\(name)"

Sources/Compiler/Sema/CardinalityInferrer.swift

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public enum Cardinality: String {
1515
/// This allows the generation to be smart and set the
1616
/// return type to an Array only if needed.
1717
struct CardinalityInferrer {
18-
let schema: Schema
18+
let env: Environment
1919

2020
mutating func cardinality<S: StmtSyntax>(for syntax: borrowing S) -> Cardinality {
2121
return syntax.accept(visitor: &self)
@@ -31,13 +31,6 @@ struct CardinalityInferrer {
3131
let didFilterByPrimaryKey = !table.primaryKey.contains{ !filteredPrimaryKeys.contains($0) }
3232
return didFilterByPrimaryKey ? .single : .many
3333
}
34-
35-
private func qualifiedName(
36-
for name: IdentifierSyntax,
37-
in schema: IdentifierSyntax?
38-
) -> QualifiedName {
39-
return QualifiedName(name: name.value, schema: schema?.value)
40-
}
4134
}
4235

4336
/// Returns `true` if the query/statement will only return one value.
@@ -56,9 +49,10 @@ extension CardinalityInferrer: StmtSyntaxVisitor {
5649
// No filtering, update is to full table
5750
guard let whereExpr = stmt.whereExpr else { return .many }
5851

59-
let tableName = qualifiedName(for: stmt.tableName.tableName.name, in: stmt.tableName.tableName.schema)
60-
61-
guard let table = schema[tableName] else {
52+
guard let table = env.resolve(
53+
table: stmt.tableName.tableName.name.value,
54+
schema: stmt.tableName.tableName.schema?.value
55+
).value else {
6256
// Upstream will have emitted diag
6357
return .many
6458
}
@@ -95,11 +89,12 @@ extension CardinalityInferrer: StmtSyntaxVisitor {
9589
// If its not against a table we cannot infer it.
9690
guard case let .table(table) = join.tableOrSubquery.kind else { return .many }
9791

98-
let tableName = qualifiedName(for: table.name, in: table.schema)
99-
10092
// If we cannot find the table something upstream will have already emitted
10193
// diagnositic so just exit
102-
guard let t = schema[tableName] else { return .many }
94+
guard let t = env.resolve(
95+
table: table.name.value,
96+
schema: table.schema?.value
97+
).value else { return .many }
10398

10499
// If they had filtering on all primary keys we can assume a single
105100
// result will be returned.
@@ -124,9 +119,10 @@ extension CardinalityInferrer: StmtSyntaxVisitor {
124119
mutating func visit(_ stmt: borrowing DeleteStmtSyntax) -> Cardinality {
125120
guard let filter = stmt.whereExpr else { return .many }
126121

127-
let tableName = qualifiedName(for: stmt.table.tableName.name, in: stmt.table.tableName.schema)
128-
129-
guard let table = schema[tableName] else {
122+
guard let table = env.resolve(
123+
table: stmt.table.tableName.name.value,
124+
schema: stmt.table.tableName.schema?.value
125+
).value else {
130126
// Upstream will have emitted diag
131127
return .many
132128
}

Sources/Compiler/Sema/ExprTypeChecker.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ extension ExprTypeChecker: ExprSyntaxVisitor {
151151
mutating func visit(_ expr: borrowing PrefixExprSyntax) -> Type {
152152
let rhs = expr.rhs.accept(visitor: &self)
153153

154-
guard let scheme = env[prefix: expr.operator.operator] else {
154+
guard let scheme = env.resolve(prefix: expr.operator.operator) else {
155155
diagnostics.add(.init(
156156
"'\(expr.operator.operator)' is not a valid prefix operator",
157157
at: expr.operator.location
@@ -169,7 +169,7 @@ extension ExprTypeChecker: ExprSyntaxVisitor {
169169
let lTy = expr.lhs.accept(visitor: &self)
170170
let rTy = expr.rhs.accept(visitor: &self)
171171

172-
guard let scheme = env[infix: expr.operator.operator] else {
172+
guard let scheme = env.resolve(infix: expr.operator.operator) else {
173173
diagnostics.add(.init(
174174
"'\(expr.operator.operator)' is not a valid infix operator",
175175
at: expr.operator.location
@@ -190,7 +190,7 @@ extension ExprTypeChecker: ExprSyntaxVisitor {
190190
mutating func visit(_ expr: borrowing PostfixExprSyntax) -> Type {
191191
let lhs = expr.lhs.accept(visitor: &self)
192192

193-
guard let scheme = env[postfix: expr.operator.operator] else {
193+
guard let scheme = env.resolve(postfix: expr.operator.operator) else {
194194
diagnostics.add(.init(
195195
"'\(expr.operator.operator)' is not a valid postfix operator",
196196
at: expr.operator.location
@@ -220,7 +220,7 @@ extension ExprTypeChecker: ExprSyntaxVisitor {
220220
mutating func visit(_ expr: borrowing FunctionExprSyntax) -> Type {
221221
let argTys = typeCheck(expr.args)
222222

223-
guard let scheme = env[function: expr.name.value, argCount: argTys.count] else {
223+
guard let scheme = env.resolve(function: expr.name.value, argCount: argTys.count) else {
224224
diagnostics.add(.init("No such function '\(expr.name)' exits", at: expr.location))
225225
return .error
226226
}

0 commit comments

Comments
 (0)