question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Having clause in correlated subquery cannot reference outer query attribute

See original GitHub issue

Consider the following query:

SELECT * FROM customer WHERE EXISTS (
    SELECT COUNT(*) FROM transaction 
    GROUP BY transaction.customer_id 
    HAVING transaction.customer_id = customer.id);

This is giving the following error: Query 20190619_162521_08871_c8pm8 failed: line 1:172: '(transaction_sequence.customer_id = customer_sequence.id)' must be an aggregate expression or appear in GROUP BY clause

This example works for MariaDB/MySQL, PostgreSQL, MS SQL Server, and Oracle 12c. It is surprising that it fails for Presto.

The error is due to customer_sequence.id, which is an attribute referenced from the outer query. If I replace it with a constant, it works. Also, if I move the filter to the WHERE clause, it also works:

SELECT * FROM customer WHERE EXISTS (
    SELECT COUNT(*) FROM transaction
    WHERE transaction.customer_id = customer.id
    GROUP BY transaction.customer_id;

So it seems like there is a problem in handling of outer query attribute references in the having clause.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bgedikcommented, Jun 19, 2019

@mbasmanova I would have liked to spend time on it, but unfortunately it is not possible at this time. This issue is not blocking me (I have a workaround), but I wanted to report it for the benefit of the community.

I am sure the issue can be fixed easily by someone who is knowledgeable about the Presto code base. Based on a quick look, it seems to me that this is a problem with the validation that is being done in AggregationAnalyzer.java.

0reactions
stale[bot]commented, Apr 16, 2022

This issue has been automatically marked as stale because it has not had any activity in the last 2 years. If you feel that this issue is important, just comment and the stale tag will be removed; otherwise it will be closed in 7 days. This is an attempt to ensure that our open issues remain valuable and relevant so that we can keep track of what needs to be done and prioritize the right things.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Referencing outer query's tables in a subquery - Stack Overflow
Is it possible to reference an outer query in a subquery with MySQL? I know there are some cases where this is possible:...
Read more >
SQL Correlated Subqueries - GeeksforGeeks
A correlated subquery is evaluated once for each row processed by the parent statement. The parent statement can be a SELECT, UPDATE, or...
Read more >
bad item ref from correlated subquery to outer distinct table
Hi Sinisa! I agree with you that all "outer queries are seen by their subqueries" and that whether temporary tables are used should...
Read more >
Correlated Subqueries - an overview | ScienceDirect Topics
A correlated subquery is a subquery that references columns in the tables of its ... When you get to the WHERE clause, and...
Read more >
Subquery gives no error for a non-existing column with the ...
Because of this "feature" you are able to write correlated subqueries and reference the fields from outer query in the inner query.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found