HAVING in sql semijoins cannot see aggregated field
- Dominant language
- Java
- Stars
- 14.1k
- Forks
- 3.8k
- Avg merge
- 2d 58m
- Merged PRs (30d)
- 233
Description
### Affected Version
0.16.1 (and prior versions)
### Description
Example SQL:
```
SELECT "field1"
FROM table1
WHERE "field2" IN
(SELECT "field3"
FROM
(SELECT SUM("value") as "abc",
"field3"
FROM table2
GROUP BY "field3"
HAVING "abc" > 100))
```
Any query of this form will invariably see an exception that looks something like:
`org.apache.calcite.tools.ValidationException: org.apache.calcite.runtime.CalciteContextException: From line 1, column 400 to line 1, column 404: Column 'abc' not found in any table`
I've no idea if this is a bug, oversight, or unimplemented feature, but it would be great if queries like this worked. For the record, the HAVING clause appears to work with ordinary table columns, just not ones that are aggregated at query time.
Contributor guide
Research direction
Start by running the SQL example against Apache Druid 0.16.1 or a current build and confirm the validation error that column 'abc' is not found. Investigate how the nested semijoin's HAVING clause resolves the aggregated alias; done means this query validates and executes while HAVING on ordinary columns continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100