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

Four arithmetic operations between aggregation functions are not supported #320

Open
Lvnszn opened this issue Apr 27, 2023 · 12 comments
Open

Comments

@Lvnszn
Copy link

Lvnszn commented Apr 27, 2023

select sum(1)/count(1) from dual
select sum(1)/7 from dual
select sum(1)*3 from dual

The sql parser cannot parse this kind of sql

@cube2222
Copy link
Owner

Hey! This is indeed a limitation right now. You need to use a common table expression or subquery.

@Lvnszn
Copy link
Author

Lvnszn commented May 6, 2023

Will you upgrade sqlparser?I understand this limitation is because of low version of sqlparser.

@cube2222
Copy link
Owner

cube2222 commented May 7, 2023

This is unrelated to sqlparser and is just a limitation of the current SQL AST -> logical plan translation step. It would have to be written in a way that gets all leaf aggregations in each expression and then constructs a GroupBy (all aggregations) -> Map (operations on aggregations).

@Lvnszn
Copy link
Author

Lvnszn commented May 8, 2023

You mean that the parse is actually successful, but there is a problem when the ast is converted into a logical plan

This is unrelated to sqlparser and is just a limitation of the current SQL AST -> logical plan translation step. 

When I parsed this sql, he had a problem. The sum(1)/7 would not be divided into two args 【sum(1) and 7 】.
I always thought that the vitess you introduced at that time was too old and did not parse this logic.

@Lvnszn
Copy link
Author

Lvnszn commented May 10, 2023

demo 1
SELECT SUM(_1)/SUM(_1) as \"_5\" from student_new0.student_new as a join sc_inner_new1.sc_inner_new as b on a._3 = b._1 limit 1

response1

{"error": "couldn't parse query: invalid argument syntax error at position 20"}

demo 2
SELECT SUM(_1)/100 as \"\" from student_new0.student_new as a join sc_inner_new1.sc_inner_new as b on a._3 = b._1 limit 10

response 2
{"error": "couldn't parse query: invalid argument syntax error at position 23 near 'as'"}

@Lvnszn
Copy link
Author

Lvnszn commented May 10, 2023

demo3
SELECT floor(float(sum((_1)))) as \"_5\" from student_new0.student_new as a join sc_inner_new1.sc_inner_new as b on a._3 = b._1 limit 10

response3
{"error": "typecheck error: unknown function: sum(Int)"}

@cube2222
Copy link
Owner

There's a bug, which is in fact in the parser, where sometimes division isn't parsed correctly. This is because the parser supports just writing out file paths as tables names (FROM my/file.json) but this means there's ambiguity that's hard to solve. That's why you're getting the syntax errors, you can fix that by writing it as /(a, b).

@cube2222
Copy link
Owner

You mean that the parse is actually successful, but there is a problem when the ast is converted into a logical plan

Yeah, you can see that in demo3. It tries to process the sum as a function, not as an aggregate.

@Lvnszn
Copy link
Author

Lvnszn commented May 12, 2023

There's a bug, which is in fact in the parser, where sometimes division isn't parsed correctly. This is because the parser supports just writing out file paths as tables names (FROM my/file.json) but this means there's ambiguity that's hard to solve. That's why you're getting the syntax errors, you can fix that by writing it as /(a, b).

Thank you for your reply, can u give me some example about /(a,b). I try this sql and not work.

SELECT /(sum(_1), sum(_3)) as \"_1\" from student_new0.student_new as a  outer join sc_inner_new1.sc_inner_new as b on a._3 = b._1 limit 1

@cube2222
Copy link
Owner

Oh, it's indeed broken, but now I found the right way: leave a space between / and the operands. So sum(_1) / sum(_3) and it parses correctly (it will still not work since it reads it as a function, but it will parse).

@Lvnszn
Copy link
Author

Lvnszn commented May 15, 2023

yes, the error show as below.

{"error": "typecheck error: unknown function: sum(NULL | Int)"}

@Lvnszn
Copy link
Author

Lvnszn commented May 17, 2023

hi, how can I fix this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants