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

HAVING clause is handled incorrectly when it references a name that matches both a column and an alias. #7082

Closed
nicktobey opened this issue Dec 1, 2023 · 0 comments · Fixed by dolthub/go-mysql-server#2174
Labels
analyzer sql Issue with SQL

Comments

@nicktobey
Copy link
Contributor

CREATE TABLE t(pk int PRIMARY KEY, col int, g int);
 
-- each of the following statements throws an error
SELECT col AS col FROM t HAVING AVG(col) > 0;
SELECT pk AS col FROM t HAVING AVG(col) > 0;
SELECT col AS col FROM t GROUP BY g HAVING AVG(col) > 0;
SELECT pk AS col FROM t GROUP BY g HAVING AVG(col) > 0;

The error is of the form unable to find field with index 6 in row of 3 columns.

The alias is necessary. SELECT col FROM t HAVING AVG(col) > 0; succeeds.

It looks like the analyzer creates GetField instances for the expressions that appear in the statement, including both the col alias, and the AVG(col) expression. Every GetField is assigned a column id which is also used as the field index. Thus, these GetFields which correspond to the expressions have column ids (and thus field indexes) which are greater than the total number of fields in the table. When evaluating the HAVING expression, the analyzer treats these ids as field indexes, sees that they're outside the acceptable range, and returns an error.

Of the ~350 remaining sqllogictest failures, this accounts for 73 of them.

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

Successfully merging a pull request may close this issue.

1 participant