apache / apache/datafusion

INTERSECT ALL returns wrong number of records from RHS

Đang mở
#12,955 5 bình luận 1 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Rust
Star
9.3k
Fork
2.4k
Merge trung bình
3 ngày 11 giờ
Pull request đã merge (30 ngày)
360

Mô tả

### Describe the bug

According to the [SQL spec](https://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt), when returning duplicate records from INTERSECT ALL the minimum number of copies from either input should be returned. Specifically:
```
b) If a set operator is specified, then the result of applying
the set operator is a table containing the following rows:

i) Let R be a row that is a duplicate of some row in T1 or of
some row in T2 or both. Let m be the number of duplicates
of R in T1 and let n be the number of duplicates of R in
T2, where m � 0 and n � 0.

...

iii) If ALL is specified, then

Case:

1) If UNION is specified, then the number of duplicates of
R that T contains is (m + n).

2) If EXCEPT is specified, then the number of duplicates of
R that T contains is the maximum of (m - n) and 0.

3) If INTERSECT is specified, then the number of duplicates
of R that T contains is the minimum of m and n.
```

DataFusion currently returns ALL copies of duplicated records from the RHS.

### To Reproduce

The following query
```sql
➜ ~ datafusion-cli
DataFusion CLI v42.0.0

> SELECT * FROM VALUES ('a'), ('b'), ('b'), ('c'), ('c'), ('c')
INTERSECT ALL
SELECT * FROM VALUES ('b'), ('b'), ('b'), ('c'), ('c');
+---------+
| column1 |
+---------+
| b |
| b |
| c |
| c |
| c |
+---------+
```

returns 3 copies of the record `('c')` which does not match the expected behaviour based on the spec.

Note that only 2 copies of `('b')` are returned, so this only appears to affect the RHS.

### Expected behavior

The above query should return 2 copies of the record `('c')`

### Additional context

See DB Fiddle for Postgres which showcases the expected behaviour:
https://www.db-fiddle.com/f/ja4BG5CfyEvak5ScoBwCZr/0

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

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện truy vấn INTERSECT ALL đã được báo cáo trong DataFusion và lần theo đường dẫn thực thi xử lý các hàng trùng lặp trong đầu vào bên phải. Công việc được xem là hoàn tất khi trả về số lượng bản sao tối thiểu từ cả hai đầu vào cho mỗi hàng, với một bài kiểm thử hồi quy bao quát ví dụ b và c.

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

Đánh giá

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

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.