Skip to content

Commit 5ebcc74

Browse files
committed
Union in subquery fix
1 parent 148a818 commit 5ebcc74

14 files changed

Lines changed: 214 additions & 78 deletions

File tree

‎.config/dotnet-tools.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"rollForward": false
4646
},
4747
"fsdocs-tool": {
48-
"version": "20.0.0",
48+
"version": "22.2.0",
4949
"commands": [
5050
"fsdocs"
5151
],

‎docs/RELEASE_NOTES.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
### 1.5.29 - 02.11.2026
22
- Fix: parameters of a sub-query nested in another sub-query are bound (IN, EXISTS, UNION and the like)
33
- Fix: groupBy keys with option or value-option columns, and with x.IsSome / x.IsNone in a tuple key
4-
- Fix: a grouped sub-query of IN / NOT IN that selects an aggregate selects it without the group keys, #746
4+
- Fix: a grouped sub-query of IN / NOT IN that selects an aggregate selects it without the group keys, on both sides of a union too, #746
5+
- IN / NOT IN sub-queries with a projection computed in .NET (e.g. `g.Max(...) + 1L`) fail with an error instead of comparing against the uncomputed column
6+
- Design time: a provider instance that goes away closes its connection instead of disposing it, as the cached types of other instances still use it ("Cannot access a disposed object")
57
- PostgreSQL: identity columns count as generated, so the typed ``Create(...)`` no longer takes them. Breaking for code that passed a value to a BY DEFAULT identity column: use Create() and set it, or Create(data). A saved ContextSchemaPath cache keeps the old signature until regenerated.
68

79
### 1.5.28 - 17.10.2026

‎docs/content/core/async.fsx‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ open System.Threading.Tasks
5757
open FSharp.Data.Sql
5858

5959
type MyWebServer() =
60-
member __.``Execute Business Logics`` (id : Guid) : Task<_> =
60+
member __.``Execute Business Logics`` (orderId : int64) : Task<_> =
6161
task {
6262
use transaction =
6363
new System.Transactions.TransactionScope(
@@ -67,14 +67,14 @@ type MyWebServer() =
6767
let context = TypeProviderConnection.GetDataContext cstr
6868
let! fetched =
6969
query {
70-
for t2 in context.MyDataBase.MyTable2 do
71-
join t1 in context.MyDataBase.MyTable1 on (t2.ForeignId = t1.Id)
72-
where (t2.Id = id)
73-
select t1
70+
for order in context.Main.Orders do
71+
join customer in context.Main.Customers on (order.CustomerId.Value = customer.CustomerId)
72+
where (order.OrderId = orderId)
73+
select order
7474
} |> Seq.executeQueryAsync
7575

76-
fetched |> Seq.iter (fun entity ->
77-
entity.SetColumn("Updated", DateTime.UtcNow |> box)
76+
fetched |> Seq.iter (fun order ->
77+
order.ShippedDate <- Some DateTime.UtcNow
7878
)
7979
do! context.SubmitUpdatesAsync()
8080

‎docs/content/core/composable.fsx‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type sql = SqlDataProvider<
1919
(*** hide ***)
2020
let ctx = sql.GetDataContext()
2121
(*** hide ***)
22-
let xs = [].AsQueryable()
22+
let xs = ([] : int list).AsQueryable()
2323

2424
(**
2525
# Composable Queries
@@ -82,10 +82,10 @@ In other words, SQLProvider queries typically follow this pattern:
8282
8383
*)
8484

85-
let (qry:IQueryable<'T>) =
85+
let pattern (xs:IQueryable<'T>) : IQueryable<'T> =
8686
query {
8787
//for is like C# foreach
88-
for x in (xs:IQueryable<'T>) do
88+
for x in xs do
8989
select x
9090
}
9191

@@ -125,7 +125,7 @@ let companyNameFilter inUse =
125125
126126
Then, you can create the main query
127127
*)
128-
let query1 =
128+
let mainQuery =
129129
query {
130130
for customers in ctx.Main.Customers do
131131
where (customers.ContactTitle = "USA")
@@ -136,7 +136,7 @@ let query1 =
136136
and now call you are able to call the second query like this
137137
*)
138138

139-
let res = companyNameFilter true query1 |> Seq.toArray
139+
let res = companyNameFilter true mainQuery |> Seq.toArray
140140

141141
(**
142142
### Generate composable queries by using FSharp.Linq.ComposableQuery
@@ -150,18 +150,18 @@ Example for using FSharpComposableQuery
150150
*)
151151

152152
let qry1 =
153-
query { for u in dbContext.Users do
154-
select (u.Id, u.Name, u.Email)
153+
query { for c in ctx.Main.Customers do
154+
select (c.CustomerId, c.CompanyName, c.City)
155155
}
156156

157157
let qry2 =
158-
query { for c in dbContext.Cars do
159-
select (c.UserId, c.Brand, c.Year)
158+
query { for o in ctx.Main.Orders do
159+
select (o.CustomerId, o.OrderId, o.Freight)
160160
}
161161

162162
query { for (i,n,e) in qry1 do
163163
join (u,b,y) in qry2 on (i = u)
164-
where (y > 2015)
164+
where (y > 100m)
165165
select (i,n,e,u,b,y)
166166
} |> Seq.toArray
167167

@@ -180,29 +180,31 @@ open System.Linq
180180

181181
let nestedQueryTest =
182182
let qry1 = query {
183-
for emp in ctx.Hr.Employees do
183+
for emp in ctx.Main.Employees do
184184
where (emp.FirstName.StartsWith("S"))
185185
select emp.FirstName
186186
}
187187
query {
188-
for emp in ctx.Hr.Employees do
188+
for emp in ctx.Main.Employees do
189189
where (qry1.Contains(emp.FirstName))
190190
select (emp.FirstName, emp.LastName)
191191
} |> Seq.toArray
192192

193193
(**
194194
195-
### Using non-strongly-typed __.GetColumn "name"
195+
### Using non-strongly-typed GetColumn "name"
196196
197-
All the entities inherit from SqlEntity which has GetColumn-method.
198-
So you can use non-strongly-typed columns like this:
197+
All the entities are SqlEntity objects, which implement `IColumnHolder` with a GetColumn method.
198+
So you can use non-strongly-typed columns, by the database's column name, like this:
199199
200200
*)
201+
open FSharp.Data.Sql.Common
202+
201203
let qry =
202204
query {
203205
for x in query1 do
204-
where ((x.GetColumn<string> "CustomerId") = "ALFKI")
205-
select (x.GetColumn<string> "CustomerId")
206+
where (((x :> IColumnHolder).GetColumn<string> "CustomerID") = "ALFKI")
207+
select ((x :> IColumnHolder).GetColumn<string> "CustomerID")
206208
} |> Seq.head
207209

208210
(**

‎docs/content/core/crud.fsx‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,15 @@ ctx.SubmitUpdates()
143143
*)
144144

145145
type Employee2 = {
146-
Id:int
146+
Id:int64
147147
FirstName:string
148148
LastName:string
149149
}
150150

151151
let updateEmployee (employee: Employee2) =
152152
let foundEmployeeMaybe = query {
153-
for p in ctx.Public.Employee2 do
154-
where (p.Id = employee.Id)
153+
for p in ctx.Main.Employees do
154+
where (p.EmployeeId = employee.Id)
155155
select (Some p)
156156
exactlyOneOrDefault
157157
}
@@ -164,8 +164,8 @@ let updateEmployee (employee: Employee2) =
164164

165165
let updateEmployee' (employee: Employee2) =
166166
query {
167-
for p in ctx.Public.Employee2 do
168-
where (p.Id = employee.Id)
167+
for p in ctx.Main.Employees do
168+
where (p.EmployeeId = employee.Id)
169169
}
170170
|> Seq.iter( fun e ->
171171
e.FirstName <- employee.FirstName
@@ -174,7 +174,7 @@ let updateEmployee' (employee: Employee2) =
174174
ctx.SubmitUpdates()
175175

176176
let john = {
177-
Id = 1
177+
Id = 1L
178178
FirstName = "John"
179179
LastName = "Doe" }
180180

@@ -188,7 +188,7 @@ output of .ColumnValues:
188188

189189
employees
190190
|> Seq.map (fun x ->
191-
employee.Create(x.ColumnValues)) // create twins
191+
ctx.Main.Employees.Create(x.ColumnValues)) // create twins
192192
|> Seq.toList
193193

194194
/// Retrieve the FSharp.Data.Sql.Common.SqlEntity objects
@@ -284,14 +284,12 @@ You can leverage this feature by setting the `OnConflict` property on a row obje
284284
Sql Server has a similar feature in the form of the MERGE statement. This is not yet supported.
285285
*)
286286

287-
let ctx = sql.GetDataContext()
288-
289-
let emp = ctx.Main.Employees.Create()
290-
emp.Id <- 1
291-
emp.FirstName <- "Jane"
292-
emp.LastName <- "Doe"
287+
let jane = ctx.Main.Employees.Create()
288+
jane.EmployeeId <- 1L
289+
jane.FirstName <- "Jane"
290+
jane.LastName <- "Doe"
293291

294-
emp.OnConflict <- FSharp.Data.Sql.Common.OnConflict.Update
292+
jane.OnConflict <- FSharp.Data.Sql.Common.OnConflict.Update
295293

296294
ctx.SubmitUpdates()
297295

@@ -321,7 +319,7 @@ There are 3 overrides of create.
321319
322320
*)
323321

324-
let emp = ctx.Main.Employees.``Create(FirstName, LastName)``("Don", "Syme")
322+
let don = ctx.Main.Employees.``Create(FirstName, LastName)``("Don", "Syme")
325323

326324
(**
327325

‎docs/content/core/mappers.fsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ If you want to use SQLProvider as code-generator and copy and paste the tables a
102102
103103
*)
104104

105-
sql.dataContext.DesignTimeCommands.TemplateAsRecord.``main.OrdersTemplate``
105+
// The template is a nested type: type its path and a dot in the editor
106+
// sql.dataContext.DesignTimeCommands.TemplateAsRecord.``main.OrdersTemplate``.
106107

107108
// intellisense will generate you code that you can copy and paste as template to create your own type:
108109
// ``type MainOrders = { CustomerId : String voption; EmployeeId : Int64 voption; Freight : Decimal voption; OrderDate : DateTime voption; OrderId : Int64; RequiredDate : DateTime voption; ShipAddress : String voption; ShipCity : String voption; ShipCountry : String voption; ShipName : String voption; ShipPostalCode : String voption; ShipRegion : String voption; ShippedDate : DateTime voption }``

‎docs/content/core/mysql.fsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ If you want to use the drivers in parallel, you need two resolution paths:
197197
*)
198198

199199
type HRFast = SqlDataProvider<Common.DatabaseProviderTypes.MYSQL, connString, ResolutionPath = @"c:\mysqlConnectorPath", Owner = "HR">
200-
type HRProcs = SqlDataProvider<Common.DatabaseProviderTypes.MYSQL, connString, ResolutionPath = @"creferencesOwner = "HR">
200+
type HRProcs = SqlDataProvider<Common.DatabaseProviderTypes.MYSQL, connString, ResolutionPath = @"c:\mysqlDataPath", Owner = "HR">
201201

202202
(**
203203
@@ -206,7 +206,7 @@ type HRProcs = SqlDataProvider<Common.DatabaseProviderTypes.MYSQL, connString, R
206206
One complex query:
207207
208208
```
209-
MySql.Data.dll: Real: 00:00:00.583, C0:00:00.484, GC gen0: 1, gen1: 0, gen2: 0
209+
MySql.Data.dll: Real: 00:00:00.583, CPU: 00:00:00.484, GC gen0: 1, gen1: 0, gen2: 0
210210
MySqlConnector.dll: Real: 00:00:00.173, CPU: 00:00:00.093, GC gen0: 1, gen1: 0, gen2: 0
211211
```
212212

‎docs/content/core/odbc.fsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let ctx = db.GetDataContext()
4848
4949
You don't need DSN for all data sources, e.g. MS Access can be used directly
5050
through the database file with a connection string like:
51-
`@"Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\Northwind.mdb"`
51+
<code>@"Driver={Microsoft Access Driver (&#42;.mdb, *.accdb)};Dbq=C:\Northwind.mdb"</code>
5252
5353
5454
### Quote Character

‎docs/content/core/querying.fsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ let xs2 =
103103
(**
104104
105105
F#'s own `Seq.contains` is not translated to SQL.
106+
107+
The sub-query selects one column that the database computes: a column, an aggregate of a groupBy
108+
(`select (g.Max(fun o -> o.OrderId))`, without the key), or arithmetic the provider translates. A projection
109+
computed in .NET, such as `g.Max(...) + 1L` or a call to your own F# function, fails with an error; select the
110+
column itself, or read the values first with `Seq.toArray`. A union (`xs.Union(ys).Contains x`) follows the same
111+
rule on both sides.
106112
- **One query for every case of a filter.** Compute flags outside the query and test them inside it, so the same
107113
query can serve "all" and "one":
108114
@@ -385,7 +391,7 @@ Math.Min(x,y) | SELECT(MIN) | LEAST | LEAST | LEAST | MIN | iif(x
385391
Math.Pow(x,y) | POWER(x,y) | POWER(x,y) | POWER(x,y) | POWER(x,y) | | x^y | POWER(x,y) | |
386392
(+) | + | + | + | + | + | + | + | |
387393
(-) | - | - | - | - | - | - | - | |
388-
(*) | * | * | * | * | * | * | * | |
394+
(&#42;) | * | * | * | * | * | * | * | |
389395
(/) | / | / | / | / | / | / | / | |
390396
(%) | % | % | % | % | % | % | % | |
391397
@@ -525,7 +531,7 @@ Notes and limitations:
525531
* `groupJoin ... into g` followed by `for x in g do` is supported and behaves as an inner join.
526532
Aggregating the group itself (e.g. `select (c, g.Count())`) is not supported - use `groupBy`
527533
for aggregates.
528-
* A `groupBy` after a `leftOuterJoin` follows SQL semantics: `COUNT(*)` counts the unmatched
534+
* A `groupBy` after a `leftOuterJoin` follows SQL semantics: <code>COUNT(&#42;)</code> counts the unmatched
529535
NULL row, so a customer with no orders gets count 1, not 0.
530536
* `DefaultIfEmpty(customDefaultValue)` is not supported; only the parameterless form is.
531537
* A filtered sub-query is not supported as a left-join destination (the filter would belong in

‎src/SQLProvider.Common/SqlRuntime.Common.fs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,9 @@ type SqlExp =
654654
| Distinct of SqlExp
655655
/// alias and column name, bool indicates ascending sort
656656
| OrderBy of alias * SqlColumnType * bool * SqlExp
657-
/// union type and subquery
658-
| Union of UnionType * string * seq<IDbDataParameter> * SqlExp
657+
/// union type and the subquery as SQL text with its parameters, rendered as the outer query is (true for a
658+
/// sub-query of IN / NOT IN), so that both sides select the same columns
659+
| Union of UnionType * (bool -> string * seq<IDbDataParameter>) * SqlExp
659660
| Skip of int * SqlExp
660661
| Take of int * SqlExp
661662
| Count of SqlExp
@@ -672,7 +673,7 @@ type SqlExp =
672673
| OrderBy(_,_,_,rest)
673674
| Skip(_,rest)
674675
| Take(_,rest)
675-
| Union(_,_,_,rest)
676+
| Union(_,_,rest)
676677
| Count(rest)
677678
| AggregateOp(_,_,rest) -> aux rest
678679
aux this
@@ -688,7 +689,7 @@ type SqlExp =
688689
| OrderBy(_,_,_,rest)
689690
| Skip(_,rest)
690691
| Take(_,rest)
691-
| Union(_,_,_,rest)
692+
| Union(_,_,rest)
692693
| Count(rest)
693694
| AggregateOp(_,_,rest) -> isGroupBy rest
694695
isGroupBy this
@@ -703,7 +704,7 @@ type SqlExp =
703704
| Distinct rest
704705
| Skip(_,rest)
705706
| Take(_,rest)
706-
| Union(_,_,_,rest)
707+
| Union(_,_,rest)
707708
| Count(rest)
708709
| AggregateOp(_,_,rest) -> isSortBy rest
709710
isSortBy this
@@ -736,7 +737,8 @@ type SqlQuery =
736737
static member Empty = { Filters = []; Links = []; Grouping = []; Aliases = Map.empty; Ordering = []; Count = false; AggregateOp = []; CrossJoins = []
737738
Projection = []; Distinct = false; UltimateChild = None; Skip = ValueNone; Take = ValueNone; Union = None; HavingFilters = [] }
738739

739-
static member ofSqlExp(exp,entityIndex: string ResizeArray) =
740+
/// The query of `exp`; `membershipSubquery` is true for a sub-query of IN / NOT IN, see SqlExp.Union.
741+
static member ofSqlExp(exp,entityIndex: string ResizeArray, membershipSubquery: bool) =
740742
let legaliseName (alias:alias) =
741743
if alias.StartsWith "_" then alias.TrimStart [|'_'|] else alias
742744

@@ -792,10 +794,12 @@ type SqlQuery =
792794
| Count(rest) ->
793795
if q.Count then failwith "count may only be specified once"
794796
else convert { q with Count = true } rest
795-
| Union(all,subquery, pars, rest) ->
797+
| Union(all, render, rest) ->
796798
if q.Union.IsSome then failwith "Union may only be specified once. However you can try: xs.Union(ys.Union(zs))"
797799
elif q.Take.IsSome then failwith "Union and take-limit is not yet supported as SQL-syntax varies."
798-
else convert { q with Union = Some(all,subquery,pars) } rest
800+
else
801+
let subquery, pars = render membershipSubquery
802+
convert { q with Union = Some(all,subquery,pars) } rest
799803
| AggregateOp(alias, operationWithKey, rest) ->
800804
convert { q with AggregateOp = (alias, operationWithKey)::q.AggregateOp } rest
801805
let sq = convert SqlQuery.Empty exp

0 commit comments

Comments
 (0)