Tarantool development patches archive
 help / color / mirror / Atom feed
From: Nikita Pettik <korablev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: v.shpilevoy@tarantool.org, Nikita Pettik <korablev@tarantool.org>
Subject: [tarantool-patches] [PATCH 2/2] sql: fix code generation for aggregate in HAVING clause
Date: Thu, 21 Feb 2019 21:01:35 +0300	[thread overview]
Message-ID: <750fa247185a20047e0ebd3242768ec81f12ad9f.1550768589.git.korablev@tarantool.org> (raw)
In-Reply-To: <cover.1550768589.git.korablev@tarantool.org>
In-Reply-To: <cover.1550768589.git.korablev@tarantool.org>

When we allowed using HAVING clause without GROUP BY (b40f2443a), one
possible combination was forgotten to be tested:

SELECT 1 FROM te40 HAVING SUM(s1) < 0;

In other words, resulting set contains no aggregates, but HAVING does
contain. In this case no byte-code related to aggregate execution is
emitted at all. Hence, query above equals to simple SELECT 1;
Unfortunately, result of such query is the same when condition under
HAVING clause is satisfied.  To fix this behaviour, it is enough to
indicate to byte-code generator that we should analyze aggregates not
only in ORDER BY clauses, but also in HAVING clause.

Closes #3932
Follow-up #2364
---
 src/box/sql/resolve.c         | 10 +++++++---
 test/sql-tap/select5.test.lua | 25 ++++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/src/box/sql/resolve.c b/src/box/sql/resolve.c
index bc208cc9d..e9a1b09f7 100644
--- a/src/box/sql/resolve.c
+++ b/src/box/sql/resolve.c
@@ -1290,12 +1290,16 @@ resolveSelectStep(Walker * pWalker, Select * p)
 				return WRC_Abort;
 		}
 
-		/* If there are no aggregate functions in the result-set, and no GROUP BY
-		 * expression, do not allow aggregates in any of the other expressions.
+		/*
+		 * If there are no aggregate functions in the
+		 * result-set, and no GROUP BY or HAVING
+		 * expression, do not allow aggregates in any
+		 * of the other expressions.
 		 */
 		assert((p->selFlags & SF_Aggregate) == 0);
 		pGroupBy = p->pGroupBy;
-		if (pGroupBy || (sNC.ncFlags & NC_HasAgg) != 0) {
+		if ((pGroupBy != NULL || p->pHaving != NULL) ||
+		    (sNC.ncFlags & NC_HasAgg) != 0) {
 			assert(NC_MinMaxAgg == SF_MinMaxAgg);
 			p->selFlags |=
 			    SF_Aggregate | (sNC.ncFlags & NC_MinMaxAgg);
diff --git a/test/sql-tap/select5.test.lua b/test/sql-tap/select5.test.lua
index 0d132dbf8..0e3efb5fa 100755
--- a/test/sql-tap/select5.test.lua
+++ b/test/sql-tap/select5.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 test = require("sqltester")
-test:plan(44)
+test:plan(46)
 
 --!./tcltestrunner.lua
 -- 2001 September 15
@@ -538,5 +538,28 @@ test:do_execsql_test(
     -- </select5-9.12>
 })
 
+-- gh-3932: bytecode is not emmited if aggregate is placed only
+-- in HAVING clause.
+--
+test:do_execsql_test(
+    "select5-9.13",
+    [[
+        SELECT 1 FROM te40 HAVING SUM(s1) < 0;
+    ]], {
+    -- <select5-9.13>
+    -- </select5-9.13>
+})
+
+test:do_execsql_test(
+    "select5-9.13.2",
+    [[
+            CREATE TABLE jj (s1 INT, s2 CHAR(1), PRIMARY KEY(s1));
+            INSERT INTO jj VALUES(1, 'A'), (2, 'a');
+            SELECT 1 FROM jj HAVING avg(s2) = 1 AND avg(s2) = 0;
+    ]], {
+    -- <select5-9.13.2>
+    -- </select5-9.13.2>
+})
+
 test:finish_test()
 
-- 
2.15.1

  parent reply	other threads:[~2019-02-21 18:01 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-21 18:01 [tarantool-patches] [PATCH 0/2] Add collation to built-in funcs and fix HAVING clause with aggregate Nikita Pettik
2019-02-21 18:01 ` [tarantool-patches] [PATCH 1/2] sql: derive collation for built-in functions Nikita Pettik
2019-02-25 12:58   ` [tarantool-patches] " Vladislav Shpilevoy
2019-02-25 18:32     ` n.pettik
2019-03-07 14:40       ` Vladislav Shpilevoy
2019-03-11  8:04         ` Konstantin Osipov
2019-02-21 18:01 ` Nikita Pettik [this message]
2019-02-25 12:58   ` [tarantool-patches] Re: [PATCH 2/2] sql: fix code generation for aggregate in HAVING clause Vladislav Shpilevoy
2019-02-25 18:33     ` n.pettik
2019-03-04 12:14       ` n.pettik
2019-03-04 12:52         ` Vladislav Shpilevoy
2019-03-07 14:40 ` [tarantool-patches] Re: [PATCH 0/2] Add collation to built-in funcs and fix HAVING clause with aggregate Vladislav Shpilevoy
2019-03-11 15:49 ` Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=750fa247185a20047e0ebd3242768ec81f12ad9f.1550768589.git.korablev@tarantool.org \
    --to=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH 2/2] sql: fix code generation for aggregate in HAVING clause' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox