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: COUNT() makes a function resolution in WHERE clause to fail #41951

Closed
imotov opened this issue May 8, 2019 · 7 comments
Closed

SQL: COUNT() makes a function resolution in WHERE clause to fail #41951

imotov opened this issue May 8, 2019 · 7 comments
Labels

Comments

@imotov
Copy link
Contributor

imotov commented May 8, 2019

A single COUNT(*) in the select portion prevents the field attribute resolution in WHERE clause

DELETE test
PUT test/_doc/1
{
  "int": 10
}

POST _sql?format=txt
{
  "query": "SELECT COUNT(*) FROM test WHERE ABS(int) > 0 GROUP BY int"
}

The query above fails wtth the following error:

{
  "error": {
    "root_cause": [
      {
        "type": "verification_exception",
        "reason": "Found 1 problem(s)\nline 1:55: Unknown column [int], did you mean [int]?"
      }
    ],
    "type": "verification_exception",
    "reason": "Found 1 problem(s)\nline 1:55: Unknown column [int], did you mean [int]?"
  },
  "status": 400
}

However if we add something besides COUNT(*) to the select clause, it works:

POST _sql?format=txt
{
  "query": "SELECT COUNT(*), MAX(int) FROM test WHERE ABS(int) > 0 GROUP BY int"
}

Removing the function for the WHERE clause also works:

POST _sql?format=txt
{
  "query": "SELECT COUNT(*) FROM test WHERE int > 0 GROUP BY int"
}

This issue might be related to #41413 but the use case seems to be quite different since it requires calling a function in WHERE and doesn't use a literal.

@imotov imotov added >bug :Analytics/SQL SQL querying labels May 8, 2019
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search

@codebird
Copy link
Contributor

codebird commented May 8, 2019

@imotov this is fixed with my proposed solution.

curl -XPOST -H 'Content-Type: application/json' -u elastic-admin:elastic-password "http://localhost:9200/_sql?format=txt" -d'
{
  "query": "SELECT COUNT(*) FROM test WHERE ABS(int) > 0 GROUP BY int"
}'
   COUNT(*)    
---------------
2              

but the issue #41413, is not solved, there is still something I am missing!

@imotov
Copy link
Contributor Author

imotov commented May 8, 2019

@codebird is there a PR with your proposed solution?

@codebird
Copy link
Contributor

codebird commented May 8, 2019

nope didn't do a PR, because it doesn't solve #41413, I just wanted some input on the approach.

https://github.com/elastic/elasticsearch/compare/master...codebird:issue%2341413?expand=1

Here's the code if you want to check it.

@imotov
Copy link
Contributor Author

imotov commented May 8, 2019

@codebird thanks! I think it would greatly simplify the feedback process if you could sign CLA and open a PR for this fix (assuming that you want it to be eventually merged into elasticsearch).

@codebird
Copy link
Contributor

codebird commented May 8, 2019

Oh I have already signed the CLA. I'll make a pull request and reference this issue and 41413 as they are closely related

@matriv matriv closed this as completed in 45b8580 Feb 15, 2020
matriv pushed a commit that referenced this issue Feb 15, 2020
Translate to an agg query even if only literals are selected,
so that the correct number of rows is returned (number of buckets).

Fix issue with key only in GROUP BY (not in select) and WHERE clause:
Resolve aggregates and groupings based on the child plan which holds
the info info for all the fields of the underlying table.

Fixes: #41951
Fixes: #41413
(cherry picked from commit 45b8580)
matriv pushed a commit that referenced this issue Feb 15, 2020
Translate to an agg query even if only literals are selected,
so that the correct number of rows is returned (number of buckets).

Fix issue with key only in GROUP BY (not in select) and WHERE clause:
Resolve aggregates and groupings based on the child plan which holds
the info info for all the fields of the underlying table.

Fixes: #41951
Fixes: #41413
(cherry picked from commit 45b8580)
@matriv
Copy link
Contributor

matriv commented Feb 15, 2020

master : 45b8580
7.x : 5b32d11
7.6 : 967f702

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

No branches or pull requests

4 participants