From a1cc30b572faa8f6c2cd2dfce040431ac2c02409 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Tue, 11 Feb 2020 15:49:10 +0100 Subject: [PATCH 1/4] SQL: [Docs] Add limitation for sorting on aggs Add a sections to point out that when ordering by an aggregate certain restrictions are applied to the columns used in the `ORDER BY` clause. Only the exact same aggregate function(s) as in the `SELECT` clause and/or the exact same grouping keys must be used. Fixes: #52204 --- docs/reference/sql/limitations.asciidoc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/reference/sql/limitations.asciidoc b/docs/reference/sql/limitations.asciidoc index 328c11e1ae2f3..be06121d3ea5f 100644 --- a/docs/reference/sql/limitations.asciidoc +++ b/docs/reference/sql/limitations.asciidoc @@ -118,6 +118,22 @@ SELECT * FROM test GROUP BY age ORDER BY COUNT(*) LIMIT 100; It is possible to run the same queries without a `LIMIT` however in that case if the maximum size (*10000*) is passed, an exception will be returned as {es-sql} is unable to track (and sort) all the results returned. +Moreover, if there are aggregation(s) in the `ORDER BY`, they must be the exact the same exact aggregation +expressions(s), as used in the `SELECT` clause. The same applies for the fields that are defined the `GROUP BY` clause: +they must be the exact same expressions in the `ORDER BY` as in the `GROUP BY`. No operators or scalar functions can be +used on top or removed from those expressions. Here are some examples of queries that are not allowed: + +[source, sql] +-------------------------------------------------- +SELECT age, MAX(salary) FROM test GROUP BY gender ORDER BY age % 10, MAX(salary); + +SELECT age, MAX(salary) FROM test GROUP BY gender ORDER BY age, CAST(MAX(salary) AS FLOAT); + +SELECT age % 10, MAX(salary) FROM test GROUP BY gender ORDER BY age, MAX(salary); + +SELECT age, MAX(salary) / 12 AS max_salary FROM test GROUP BY ORDER BY age, MAX(salary); +-------------------------------------------------- + [float] === Using aggregation functions on top of scalar functions From 5191624cf139adc65beb7d537849b62926a5cd51 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Tue, 11 Feb 2020 20:46:59 +0100 Subject: [PATCH 2/4] fix limitation --- docs/reference/sql/limitations.asciidoc | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/docs/reference/sql/limitations.asciidoc b/docs/reference/sql/limitations.asciidoc index be06121d3ea5f..146ee365ac7ce 100644 --- a/docs/reference/sql/limitations.asciidoc +++ b/docs/reference/sql/limitations.asciidoc @@ -118,20 +118,15 @@ SELECT * FROM test GROUP BY age ORDER BY COUNT(*) LIMIT 100; It is possible to run the same queries without a `LIMIT` however in that case if the maximum size (*10000*) is passed, an exception will be returned as {es-sql} is unable to track (and sort) all the results returned. -Moreover, if there are aggregation(s) in the `ORDER BY`, they must be the exact the same exact aggregation -expressions(s), as used in the `SELECT` clause. The same applies for the fields that are defined the `GROUP BY` clause: -they must be the exact same expressions in the `ORDER BY` as in the `GROUP BY`. No operators or scalar functions can be -used on top or removed from those expressions. Here are some examples of queries that are not allowed: +Moreover, the aggregation(s) used in the `ORDER BY`, must be only plain aggregate functions. No scalar +functions or operators can be used, and therefore no complex columns that combine two ore more aggregate +functions can be used for ordering. Here are some examples of queries that are *not allowed*: [source, sql] -------------------------------------------------- -SELECT age, MAX(salary) FROM test GROUP BY gender ORDER BY age % 10, MAX(salary); +SELECT age, ROUND(AVG(salary)) AS avg FROM test GROUP BY gender ORDER BY avg; -SELECT age, MAX(salary) FROM test GROUP BY gender ORDER BY age, CAST(MAX(salary) AS FLOAT); - -SELECT age % 10, MAX(salary) FROM test GROUP BY gender ORDER BY age, MAX(salary); - -SELECT age, MAX(salary) / 12 AS max_salary FROM test GROUP BY ORDER BY age, MAX(salary); +SELECT age, MAX(salary) - MIN(salary) AS diff FROM test GROUP BY gender ORDER BY diff; -------------------------------------------------- [float] From 94b28283844b020101f073c72c9775ec811ce06c Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Tue, 11 Feb 2020 20:52:47 +0100 Subject: [PATCH 3/4] gender -> age --- docs/reference/sql/limitations.asciidoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/reference/sql/limitations.asciidoc b/docs/reference/sql/limitations.asciidoc index 146ee365ac7ce..7135dae9e6152 100644 --- a/docs/reference/sql/limitations.asciidoc +++ b/docs/reference/sql/limitations.asciidoc @@ -124,9 +124,9 @@ functions can be used for ordering. Here are some examples of queries that are * [source, sql] -------------------------------------------------- -SELECT age, ROUND(AVG(salary)) AS avg FROM test GROUP BY gender ORDER BY avg; +SELECT age, ROUND(AVG(salary)) AS avg FROM test GROUP BY age ORDER BY avg; -SELECT age, MAX(salary) - MIN(salary) AS diff FROM test GROUP BY gender ORDER BY diff; +SELECT age, MAX(salary) - MIN(salary) AS diff FROM test GROUP BY age ORDER BY diff; -------------------------------------------------- [float] From 969b929f455b6fa607272057f58888e7bd896a29 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Wed, 12 Feb 2020 12:40:27 +0100 Subject: [PATCH 4/4] address comments --- docs/reference/sql/limitations.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/sql/limitations.asciidoc b/docs/reference/sql/limitations.asciidoc index 7135dae9e6152..acbbfeb88b676 100644 --- a/docs/reference/sql/limitations.asciidoc +++ b/docs/reference/sql/limitations.asciidoc @@ -118,7 +118,7 @@ SELECT * FROM test GROUP BY age ORDER BY COUNT(*) LIMIT 100; It is possible to run the same queries without a `LIMIT` however in that case if the maximum size (*10000*) is passed, an exception will be returned as {es-sql} is unable to track (and sort) all the results returned. -Moreover, the aggregation(s) used in the `ORDER BY`, must be only plain aggregate functions. No scalar +Moreover, the aggregation(s) used in the `ORDER BY must be only plain aggregate functions. No scalar functions or operators can be used, and therefore no complex columns that combine two ore more aggregate functions can be used for ordering. Here are some examples of queries that are *not allowed*: