Composed query with groupBy clause in subquery fails
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start by tracing SQLProvider's translation of composed F# queries with a groupBy clause and inspect the generated subquery for the reproduction. Done means the IN subquery selects only the requested aggregate column, while explicitly selecting the group key still includes it; add or run a regression test covering both cases.
Written by the indexing model from the issue text.
Description
Describe the bug
SqlException "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS." returned when using a composed query when the subquery has a groupBy clause. This is because SQLProvider includes the groupBy key in the select clause even if you haven't asked for it.
To Reproduce
Steps to reproduce the behavior:
- Take a query that uses a subquery with a groupBy clause, e.g.:
let test =
let lastAddresses =
query {
for a in dbContext.Address do
where (a.Deleted.IsNone)
groupBy a.UserId into g
select (g.Max(fun a -> a.Id))
}
let result =
query {
for a in dbContext.Address do
where (lastAddresses.Contains(a.Id))
select a
}
result
- Run the query, and you get the SqlException described above.
- Inspecting the SQL produced, you can see the problem:
SELECT *
FROM [address] as [a]
WHERE (([a].[Id] IN (
SELECT [a].[UserId] as '[a].[UserId]', MAX([a].[Id]) as '[a].[MAX_Id]'
FROM [address] as [a]
WHERE (([a].[Deleted] IS NOT NULL))
GROUP BY [a].[UserId]))
)
- Basically SQLProvider is including
[a].[UserId] as '[a].[UserId]'in the select clause of the subquery. TheINclause is only expecting a single column in the subquery so fails.
Expected behavior
SQLProvider should only return the columns requested in the query. The SQL produced should be:
SELECT *
FROM [address] as [a]
WHERE (([a].[Id] IN (
SELECT MAX([a].[Id]) as '[a].[MAX_Id]'
FROM [address] as [a]
WHERE (([a].[Deleted] IS NOT NULL))
GROUP BY [a].[UserId]))
)
Additional context
If we wanted the groupBy key included in the select statement, we could do so by requesting it explicitly, so there is no reason why SQLProvider should do this for us unsolicitedly:
let test =
let lastAddresses =
query {
for a in dbContext.Address do
where (a.Deleted.IsNone)
groupBy a.UserId into g
select (g.Key, g.Max(fun a -> a.Id))
}
let result =
query {
for a in dbContext.Address do
where (lastAddresses.Contains(a.Id))
select a
}
result
- Dominant language
- F#
- Stars
- 627
- Forks
- 147
- Avg merge
- 2h 2m
- Merged PRs (30d)
- 1
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fsprojects/SQLProvider
-
enhancement
Difficulty 3/5 1-2 days Newbie friendliness 55/100
fsprojects/SQLProvider#872 · 2 comments ·
-
Repo Assist? Openenhancement
Difficulty 5/5 Over a week Newbie friendliness 10/100
fsprojects/SQLProvider#870 · 1 comment ·
-
postgresql
Difficulty 4/5 3-5 days Newbie friendliness 58/100
fsprojects/SQLProvider#869 · 2 comments ·
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 42/100
fsprojects/SQLProvider#868 · 2 comments ·
-
sql server
Difficulty 4/5 3-5 days Newbie friendliness 45/100
fsprojects/SQLProvider#851 · 1 comment ·
All issues in fsprojects/SQLProvider
Similar issues
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
[BUG] A column whose default is the empty string is drawn in the ER diagram as having no default Openbug database-provider good first issue hacktoberfest
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
libredb/libredb-studio#1030 · 6 comments ·
-
comp-datalake
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
ClickHouse/ClickHouse#121222 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug redshift
Difficulty 2/5 1-3 hours Newbie friendliness 88/100