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

ORDER BY min(x) results in error Plan("No field named 'foo.x'. Valid fields are 'MIN(foo.x)'.") #1479

Closed
alamb opened this issue Dec 23, 2021 · 2 comments · Fixed by #1506
Closed
Labels
bug Something isn't working datafusion Changes in the datafusion crate good first issue Good for newcomers

Comments

@alamb
Copy link
Contributor

alamb commented Dec 23, 2021

Describe the bug
You can not refer to an aggregate directly in the ORDER BY clause

To Reproduce

echo "1" > /tmp/foo.csv
cargo run datafusion-cli
DataFusion CLI v5.1.0

❯ create external table foo(x int) stored as CSV location '/tmp/foo.csv';
0 rows in set. Query took 0.033 seconds.

❯ select min(x) from foo order by min(x);
Plan("No field named 'foo.x'. Valid fields are 'MIN(foo.x)'.")

Expected behavior
The query should produce a single column, with a single row 1

Note there are several workarounds to express this query that do work:

Refer to the item by index:

select min(x) from foo order by 1;
+------------+
| MIN(foo.x) |
+------------+
| 1          |
+------------+
1 row in set. Query took 0.045 seconds.

Given the query an alias:

select min(x) as min_x from foo order by min_x;
+-------+
| min_x |
+-------+
| 1     |
+-------+
1 row in set. Query took 0.010 seconds.

Additional context
Add any other context about the problem here.

@alamb alamb added bug Something isn't working good first issue Good for newcomers datafusion Changes in the datafusion crate labels Dec 23, 2021
@houqp
Copy link
Member

houqp commented Dec 24, 2021

I think there is a typo in your reproduction example? It should be the following right?

❯ select min(x) from foo order by min(x);
Plan("No field named 'foo.x'. Valid fields are 'MIN(foo.x)'.")

@alamb
Copy link
Contributor Author

alamb commented Dec 26, 2021

Yes sorry @houqp -- I have fixed that in the description

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working datafusion Changes in the datafusion crate good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants