elastic / elasticsearch

Free and Open Source, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
69.63k stars 24.64k forks source link

SQL: inconsistent behaviour with GROUP BY constant, aggregations and false conditions #84490

Open luigidellaquila opened 2 years ago

luigidellaquila commented 2 years ago
SELECT  count(*), 'x'  c FROM person WHERE name = 'doesNotExist' group by c;

returns one row

{
    "columns": [
        {
            "name": "count(*)",
            "type": "long"
        },
        {
            "name": "c",
            "type": "keyword"
        }
    ],
    "rows": [
        [
            0,
            "x"
        ]
    ]
}

While

SELECT  count(*), 'x'  c FROM person WHERE 'xyz' = 'doesNotExist' group by c;

returns zero rows

{
    "columns": [
        {
            "name": "count(*)",
            "type": "long"
        },
        {
            "name": "c",
            "type": "keyword"
        }
    ],
    "rows": []
}

The two queries should return the same result, since in both cases the WHERE condition evaluates to FALSE for all the records. The only difference is that the first query evaluates the condition on the data, while the second one evaluates it locally (name = 'doesNotExist', that could still be true vs. 'xyz' = 'doesNotExist', that is always false).

This issue could be related to #74064

Steps to reproduce

POST localhost:9200/_bulk

{"index": {"_index":"person"}}
{"name" : "foo", "surname":"bar"}
{"index": {"_index":"person"}}
{"name" : "foo", "surname":"baz"}

GET localhost:9200/_sql

{
 "query":"select  count(*), 'x'  c from person where name = 'doesNotExist' group by c"
}

GET localhost:9200/_sql

{
 "query":"select  count(*), 'x'  c from person where 'xyz' = 'doesNotExist' group by c"
}
elasticmachine commented 2 years ago

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

elasticsearchmachine commented 8 months ago

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