Skip to content

Commit 3aa22c9

Browse files
committed
small fixes
1 parent 603dfea commit 3aa22c9

12 files changed

Lines changed: 176 additions & 10 deletions

.swiftformat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
--swiftversion 60.
22
--disable unusedArguments, opaqueGenericParameters, redundantSelf, trailingSpace, redundantReturn, spaceAroundOperators
3+
--ifdef no-indent

Sources/Compiler/Driver.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ public actor Driver {
109109
// The output path contains the file we are writing too
110110
// so removing the last component gives us just the directory.
111111
var directory = path.split(separator: "/")
112-
directory.removeLast()
113-
try fileSystem.create(directory: directory.joined(separator: "/"))
112+
113+
if directory.count > 1 {
114+
directory.removeLast()
115+
try fileSystem.create(directory: directory.joined(separator: "/"))
116+
}
117+
114118
try file.write(toFile: path, atomically: true, encoding: .utf8)
115119
} else {
116120
// No output path, default to stdout.

Sources/Compiler/Sema/Builtins.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ enum Builtins {
117117
returning: .text,
118118
overloads: [Function.Overload(.text, .integer, .integer, returning: .text)]
119119
),
120+
"sqrt": Function(.var(.integer(0)), returning: .var(.integer(0))),
120121
"trim": Function(
121122
.text,
122123
returning: .text,

Sources/Compiler/Sema/CardinalityInferrer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct CardinalityInferrer {
2727
_ expr: any ExprSyntax,
2828
for table: Table
2929
) -> Cardinality {
30+
guard !table.primaryKey.isEmpty else { return .many }
3031
let filteredPrimaryKeys = expr.accept(visitor: &self)
3132
let didFilterByPrimaryKey = !table.primaryKey.contains{ !filteredPrimaryKeys.contains($0) }
3233
return didFilterByPrimaryKey ? .single : .many

Sources/Compiler/Sema/InferenceState.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ struct InferenceState {
135135
case let .alias(ty, alias):
136136
return .alias(solution(for: ty, defaultIfTyVar: true), alias)
137137
case let .row(row):
138-
return .row(row.mapTypes { solution(for: $0, defaultIfTyVar: true) })
138+
if let type = row.first, row.count == 1, !row.isUnknown {
139+
return solution(for: type, defaultIfTyVar: true)
140+
} else {
141+
return .row(row.mapTypes { solution(for: $0, defaultIfTyVar: true) })
142+
}
139143
default:
140144
return result
141145
}

Sources/Compiler/Sema/StmtTypeChecker.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ extension StmtTypeChecker {
446446
// Type check limit before since it does not have access
447447
// to any selected columns
448448
if let limit = select.limit {
449-
_ = typeCheck(limit.expr)
449+
let (type, name) = typeCheck(limit.expr)
450+
nameInferrer.suggest(name: "limit", for: name)
451+
inferenceState.unify(type, with: .integer, at: limit.expr.location)
450452
}
451453

452454
let resultColumns = typeCheck(

Sources/Compiler/Sema/Type.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public enum Type: Equatable, CustomStringConvertible, Sendable {
7777
}
7878
}
7979

80+
var isUnknown: Bool {
81+
guard case .unknown = self else { return false }
82+
return true
83+
}
84+
8085
func apply(_ s: Substitution) -> Row {
8186
return switch self {
8287
case let .fixed(v): .fixed(v.map { $0.apply(s) })

Tests/CompilerTests/Compiler/CompileIsSingleResult.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ CREATE TABLE employee (
99
PRIMARY KEY(companyId, userId)
1010
);
1111

12+
CREATE TABLE noPk (value INTEGER);
13+
1214
-- CHECK: SINGLE
1315
SELECT * FROM user WHERE id = 1;
1416

@@ -29,3 +31,6 @@ SELECT * FROM employee WHERE companyId = 1 AND userId = 1;
2931

3032
-- CHECK: MANY
3133
SELECT * FROM employee WHERE companyId = 1 OR userId = 1;
34+
35+
-- CHECK: MANY
36+
SELECT * FROM noPk WHERE value = ?;

Tests/CompilerTests/Compiler/CompileSimpleSelects.sql

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ SELECT id, ? AS value FROM bar;
184184
-- CHECK: CHUNK
185185
-- CHECK: OUTPUT
186186
-- CHECK: id INTEGER
187-
-- CHECK: bestValue (INTEGER)
188-
-- CHECK: secondBestValue (INTEGER)
189-
-- CHECK: thirdBestValue (INTEGER)
187+
-- CHECK: bestValue INTEGER
188+
-- CHECK: secondBestValue INTEGER
189+
-- CHECK: thirdBestValue INTEGER
190190
-- CHECK: TABLES
191191
-- CHECK: bar
192192
SELECT id,
@@ -234,11 +234,17 @@ SELECT -1 AS value FROM foo
234234
WHERE value = 1 AND value NOTNULL AND value NOT NULL AND value ISNULL ORDER BY value;
235235

236236
-- CHECK: SIGNATURE
237+
-- CHECK: PARAMETERS
238+
-- CHECK: PARAMETER
239+
-- CHECK: TYPE INTEGER
240+
-- CHECK: INDEX 1
241+
-- CHECK: NAME limit
237242
-- CHECK: OUTPUT_CHUNKS
238243
-- CHECK: CHUNK
239244
-- CHECK: OUTPUT
240245
-- CHECK: value INTEGER
241246
-- CHECK: TABLES
242247
-- CHECK: foo
243248
SELECT 1 AS value FROM foo
244-
WHERE EXISTS (SELECT * FROM foo) value;
249+
WHERE EXISTS (SELECT * FROM foo)
250+
LIMIT ?;

Tests/CompilerTests/TypeCheckerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ class TypeCheckerTests: XCTestCase {
192192
}
193193

194194
func testExprInParens() {
195-
XCTAssertEqual(.row(.fixed([.integer])), try check("(1 + 1) + 1"))
195+
XCTAssertEqual(.integer, try check("(1 + 1) + 1"))
196196
}
197197

198198
func scope(table: String, schema: String) throws -> Environment {

0 commit comments

Comments
 (0)