Composed query with groupBy clause in subquery fails

Đang mở
#746 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
fsharp, sql
Lĩnh vực
databases

Hướng nghiên cứu

Bắt đầu bằng cách theo dõi quá trình SQLProvider dịch các truy vấn F# kết hợp có mệnh đề groupBy và kiểm tra truy vấn con được tạo cho trường hợp tái hiện. Hoàn tất khi truy vấn con IN chỉ chọn cột aggregate được yêu cầu, trong khi việc chọn tường minh khóa nhóm vẫn bao gồm khóa đó; thêm hoặc chạy một regression test bao quát cả hai trường hợp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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:

  1. 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
  1. Run the query, and you get the SqlException described above.
  2. 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]))
)
  1. Basically SQLProvider is including [a].[UserId] as '[a].[UserId]' in the select clause of the subquery. The IN clause 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
Ngôn ngữ chính
F#
Star
627
Fork
147
Merge trung bình
2 giờ 2 phút
Pull request đã merge (30 ngày)
1

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của fsprojects/SQLProvider

Tất cả issue của fsprojects/SQLProvider

Issue tương tự

Thêm issue về Databases

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.