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: Cannot use HAVING clause with aggregates and fields #71350

Open
costin opened this issue Apr 6, 2021 · 3 comments
Open

SQL: Cannot use HAVING clause with aggregates and fields #71350

costin opened this issue Apr 6, 2021 · 3 comments
Assignees
Labels
:Analytics/SQL SQL querying >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo)

Comments

@costin
Copy link
Member

costin commented Apr 6, 2021

Currently the HAVING clause translation takes into account only aggregations - however when dealing with subqueries, fields can be used as well:

SELECT * FROM (
  SELECT languages, COUNT(*) AS c FROM test_emp 
)
WHERE c > 0 OR languages IS NOT NULL

Due to the disjunction languages > 0 cannot be pushed down and the execution will fail due to the generated script being incorrect:

"source" : "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.or(InternalQlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.gt(params.a0,params.v0)),InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.isNotNull(InternalQlScriptUtils.docValue(doc,params.v2)))",
@costin costin added >bug :Analytics/SQL SQL querying labels Apr 6, 2021
@elasticmachine elasticmachine added the Team:QL (Deprecated) Meta label for query languages team label Apr 6, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-ql (Team:QL)

@Luegg
Copy link
Contributor

Luegg commented Jul 6, 2021

I don't see an easy way to support this atm. As mentioned in #36853 and #32692 the script cannot access the bucket key from the composite aggregation.

(currently generated query for the SQL SELECT * FROM (SELECT COUNT(*) c, languages FROM test_emp GROUP BY languages) WHERE c > 0 OR languages IS NOT NULL:

{
  "size": 0,
  "_source": false,
  "aggregations": {
    "groupby": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "8831d512": {
              "terms": {
                "field": "languages",
                "missing_bucket": true,
                "order": "asc"
              }
            }
          }
        ]
      },
      "aggregations": {
        "having.having.f143ef6a_|_having.3cf5abc8": {
          "bucket_selector": {
            "buckets_path": {
              "a0": "_count"
            },
            "script": {
              "source": "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.or(InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0)),InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.isNotNull(InternalQlScriptUtils.docValue(doc,params.v1)))))",
              "lang": "painless",
              "params": {
                "v0": 0,
                "v1": "languages"
              }
            },
            "gap_policy": "skip"
          }
        }
      }
    }
  }
}

)

@wchaparro wchaparro removed the Team:QL (Deprecated) Meta label for query languages team label Jan 17, 2024
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Jan 17, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/SQL SQL querying >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo)
Projects
None yet
Development

No branches or pull requests

5 participants