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

SQL: HAVING without GROUP BY #37051

Closed
astefan opened this issue Dec 31, 2018 · 2 comments · Fixed by #46709
Closed

SQL: HAVING without GROUP BY #37051

astefan opened this issue Dec 31, 2018 · 2 comments · Fixed by #46709
Assignees
Labels

Comments

@astefan
Copy link
Contributor

astefan commented Dec 31, 2018

sql> select 1 having count(*) > 0;
Bad request [Found 1 problem(s)
line 1:17: Cannot use WHERE filtering on aggregate function [COUNT(1)], use HAVING instead]

The error message is confusing.

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search

@astefan
Copy link
Contributor Author

astefan commented Apr 12, 2019

For a query like select count(1) from calcs group by str0 having count(1) > 0 we do generate an ES query, which seems valid and does work:

{
    "size": 0,
    "_source": false,
    "stored_fields": "_none_",
    "aggregations": {
        "groupby": {
            "composite": {
                "size": 1000,
                "sources": [
                    {
                        "53415": {
                            "terms": {
                                "field": "str0",
                                "missing_bucket": true,
                                "order": "asc"
                            }
                        }
                    }
                ]
            },
            "aggregations": {
                "having.53424": {
                    "bucket_selector": {
                        "buckets_path": {
                            "a0": "_count"
                        },
                        "script": {
                            "source": "InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a0,params.v0))",
                            "lang": "painless",
                            "params": {
                                "v0": 0
                            }
                        },
                        "gap_policy": "skip"
                    }
                }
            }
        }
    }
}

@astefan astefan self-assigned this Apr 18, 2019
@costin costin assigned costin and unassigned astefan Sep 11, 2019
costin added a commit to costin/elasticsearch that referenced this issue Sep 13, 2019
Handle queries with implicit GROUP BY where the aggregation is not in
the projection/SELECT but inside the filter/HAVING such as:

SELECT 1 FROM x HAVING COUNT(*) > 0

The engine now properly identifies the case

Fix elastic#37051
costin added a commit that referenced this issue Sep 17, 2019
Handle queries with implicit GROUP BY where the aggregation is not in
the projection/SELECT but inside the filter/HAVING such as:

SELECT 1 FROM x HAVING COUNT(*) > 0

The engine now properly identifies the case and handles it accordingly.

Fix #37051
costin added a commit that referenced this issue Sep 17, 2019
Handle queries with implicit GROUP BY where the aggregation is not in
the projection/SELECT but inside the filter/HAVING such as:

SELECT 1 FROM x HAVING COUNT(*) > 0

The engine now properly identifies the case and handles it accordingly.

Fix #37051

(cherry picked from commit fa53ca0)
costin added a commit that referenced this issue Sep 17, 2019
Handle queries with implicit GROUP BY where the aggregation is not in
the projection/SELECT but inside the filter/HAVING such as:

SELECT 1 FROM x HAVING COUNT(*) > 0

The engine now properly identifies the case and handles it accordingly.

Fix #37051

(cherry picked from commit fa53ca0)
(cherry picked from commit 683b5fd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants