Skip to content

Commit a3be5f1

Browse files
committed
Report ambiguous column names error based on output columns.
This is in constrast to the prior behavior, where as long as you didn't select an ambiguous column from an outer query, you were considered OK. This let you produce queries that spat ambiguous queries out to the consuming program source code, which was bad.
1 parent ced3b84 commit a3be5f1

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/Rezoom.SQL.Compiler/TypeChecker.fs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ type private TypeChecker(cxt : ITypeInferenceContext, scope : InferredSelectScop
227227
checker, Some texpr, None
228228
let columns = checker.ResultColumns(select.Columns, knownShape)
229229
let infoColumns =
230+
let used = HashSet()
230231
seq {
231232
for column in columns.Columns do
232233
match column.Case with
@@ -237,7 +238,9 @@ type private TypeChecker(cxt : ITypeInferenceContext, scope : InferredSelectScop
237238
ColumnName =
238239
match implicitAlias (expr.Value, alias) with
239240
| None -> failAt column.Source Error.expressionRequiresAlias
240-
| Some alias -> alias
241+
| Some alias ->
242+
if used.Add(alias) then alias
243+
else failAt column.Source (Error.ambiguousColumn alias)
241244
}
242245
// typechecker should've eliminated alternatives
243246
| _ -> bug "All wildcards must be expanded -- this is a typechecker bug"

src/Rezoom.SQL.Test/TestRoundTrip.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let ``select`` () =
2828
[<Test>]
2929
let ``fancy select`` () =
3030
roundtrip """
31-
select g.*, u.*
31+
select g.Id as GroupId, g.Name as GroupName, u.Id as UserId, u.Name as UserName
3232
from Users u
3333
left join UserGroupMaps gm on gm.UserId = u.Id
3434
left join Groups g on g.Id = gm.GroupId
@@ -38,7 +38,7 @@ let ``fancy select`` () =
3838
[<Test>]
3939
let ``fancy select with order by`` () =
4040
roundtrip """
41-
select g.*, u.*
41+
select g.Id as GroupId, g.Name as GroupName, u.Id as UserId, u.Name as UserName
4242
from Users u
4343
left join UserGroupMaps gm on gm.UserId = u.Id
4444
left join Groups g on g.Id = gm.GroupId
@@ -187,7 +187,7 @@ let ``date literals`` () =
187187
[<Test>]
188188
let ``join subqueries`` () =
189189
roundtrip """
190-
select * from
190+
select us.Id as UID, gs.Id as GID from
191191
(select u.Id from Users u) us
192192
join
193193
(select g.Id from Groups g) gs

src/Rezoom.SQL.Test/TestTypeErrors.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,14 @@ let ``cte is not in scope for update`` () =
120120
expectError (Error.objectNotATable "cte")
121121
"""
122122
with cte(x) as (select 1) update cte set x = 1;
123+
"""
124+
125+
[<Test>]
126+
let ``ambiguous columns`` () =
127+
expectError (Error.ambiguousColumn "Id")
128+
"""
129+
select *
130+
from users u
131+
join usergroupmaps ugm on ugm.userid = u.id
132+
join groups g on g.id = ugm.groupid
123133
"""

0 commit comments

Comments
 (0)