Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect results in COUNT(*) queries involving LIMIT #8048

Closed
msirek opened this issue Nov 4, 2023 · 0 comments · Fixed by #8049
Closed

Incorrect results in COUNT(*) queries involving LIMIT #8048

msirek opened this issue Nov 4, 2023 · 0 comments · Fixed by #8049
Labels
bug Something isn't working

Comments

@msirek
Copy link
Contributor

msirek commented Nov 4, 2023

Describe the bug

While testing #8038, I ran into some incorrect results cases in LIMIT queries.

To Reproduce

❯ CREATE TABLE IF NOT EXISTS t1 (a INT) AS VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
0 rows in set. Query took 0.009 seconds.

❯ EXPLAIN
SELECT COUNT(*) FROM (SELECT a FROM t1 WHERE a > 3 LIMIT 3 OFFSET 6);
+---------------+---------------------------------------------------------------+
| plan_type     | plan                                                          |
+---------------+---------------------------------------------------------------+
| logical_plan  | Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1)) AS COUNT(*)]] |
|               |   Limit: skip=6, fetch=3                                      |
|               |     Filter: t1.a > Int32(3)                                   |
|               |       TableScan: t1 projection=[a]                            |
| physical_plan | ProjectionExec: expr=[9 as COUNT(*)]                          |
|               |   EmptyExec: produce_one_row=true                             |
|               |                                                               |
+---------------+---------------------------------------------------------------+
2 rows in set. Query took 0.015 seconds.

-- The correct answer is 1.SELECT COUNT(*) FROM (SELECT a FROM t1 WHERE a > 3 LIMIT 3 OFFSET 6);
+----------+
| COUNT(*) |
+----------+
| 9        |
+----------+

❯ CREATE EXTERNAL TABLE sales(customer_id VARCHAR, revenue BIGINT) STORED AS CSV location '/home/ms/git/arrow-datafusion/datafusion/core/tests/data/customer.csv';
0 rows in set. Query took 0.002 seconds.

-- The correct answer is 1.SELECT COUNT(*) FROM (SELECT customer_id FROM sales LIMIT 10000000000);
+-------------+
| COUNT(*)    |
+-------------+
| 10000000000 |
+-------------+
1 row in set. Query took 0.009 seconds.

❯ explain  SELECT COUNT(*) FROM (SELECT customer_id FROM sales LIMIT 10000000000);
+---------------+---------------------------------------------------------------+
| plan_type     | plan                                                          |
+---------------+---------------------------------------------------------------+
| logical_plan  | Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1)) AS COUNT(*)]] |
|               |   Limit: skip=0, fetch=10000000000                            |
|               |     TableScan: sales projection=[], fetch=10000000000         |
| physical_plan | ProjectionExec: expr=[10000000000 as COUNT(*)]                |
|               |   EmptyExec: produce_one_row=true                             |
|               |                                                               |
+---------------+---------------------------------------------------------------+
2 rows in set. Query took 0.015 seconds.

Expected behavior

Correct results from query execution

Additional context

No response

@msirek msirek added the bug Something isn't working label Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant