From 6353a3a676ea8b7e77fc70f1ed5eea07347096a5 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja.osipov@gmail.com>
Date: Mon, 14 Mar 2011 16:28:33 +0300
Subject: [PATCH] Fixes and test cases for Bug#729758, Bug#729879

A fix and a test case for

https://bugs.launchpad.net/tarantool/+bug/729758
"SELECT fails with a disjunct and small LIMIT"

and

https://bugs.launchpad.net/tarantool/+bug/729879
"Zero limit is treated the same as no limit"

These were simple coding bugs in SELECT main loop.
---
 mod/silverbox/box.c     | 21 +++++++++++++--------
 test/box_big/sql.result | 35 ++++++++++++++++++++++++++++++++++-
 test/box_big/sql.test   | 23 ++++++++++++++++++++++-
 3 files changed, 69 insertions(+), 10 deletions(-)

diff --git a/mod/silverbox/box.c b/mod/silverbox/box.c
index 069cd7a339..0432b436a9 100644
--- a/mod/silverbox/box.c
+++ b/mod/silverbox/box.c
@@ -558,6 +558,11 @@ process_select(struct box_txn *txn, u32 limit, u32 offset, struct tbuf *data)
 
 	if (txn->index->type == TREE) {
 		for (u32 i = 0; i < count; i++) {
+
+			/* End the loop if reached the limit. */
+			if (limit == *found)
+				goto end;
+
 			u32 key_len = read_u32(data);
 			void *key = read_field(data);
 
@@ -578,17 +583,19 @@ process_select(struct box_txn *txn, u32 limit, u32 offset, struct tbuf *data)
 					continue;
 				}
 
-				(*found)++;
 				tuple_add_iov(txn, tuple);
 
-				if (--limit == 0)
+				if (limit == ++(*found))
 					break;
 			}
-			if (limit == 0)
-				break;
 		}
 	} else {
 		for (u32 i = 0; i < count; i++) {
+
+			/* End the loop if reached the limit. */
+			if (limit == *found)
+				goto end;
+
 			u32 key_len = read_u32(data);
 			if (key_len != 1)
 				box_raise(ERR_CODE_ILLEGAL_PARAMS,
@@ -603,17 +610,15 @@ process_select(struct box_txn *txn, u32 limit, u32 offset, struct tbuf *data)
 				continue;
 			}
 
-			(*found)++;
 			tuple_add_iov(txn, tuple);
-
-			if (--limit == 0)
-				break;
+			(*found)++;
 		}
 	}
 
 	if (data->len != 0)
 		box_raise(ERR_CODE_ILLEGAL_PARAMS, "can't unpack request");
 
+end:
 	return ERR_CODE_OK;
 }
 
diff --git a/test/box_big/sql.result b/test/box_big/sql.result
index 5e9737fdf0..0aacd2ae1b 100644
--- a/test/box_big/sql.result
+++ b/test/box_big/sql.result
@@ -1,3 +1,8 @@
+
+A test case for Bug#729758
+"SELECT fails with a disjunct and small LIMIT"
+https://bugs.launchpad.net/tarantool/+bug/729758
+
 insert into t0 values ('Doe', 'Richard')
 Insert OK, 1 row affected
 insert into t0 values ('Roe', 'Richard')
@@ -15,4 +20,32 @@ Insert OK, 1 row affected
 insert into t0 values ('Callaghan', 'Tomas')
 Insert OK, 1 row affected
 select * from t0 where k1='Richard' or k1='Tomas' or k1='Tomas' limit 5
-An error occurred: ERR_CODE_ILLEGAL_PARAMS, 'Illegal parameters'
+Found 5 tuples:
+['Doe', 'Richard']
+['Roe', 'Richard']
+['Woe', 'Richard']
+['Major', 'Tomas']
+['Kytes', 'Tomas']
+
+A test case for Bug#729879
+"Zero limit is treated the same as no limit"
+https://bugs.launchpad.net/tarantool/+bug/729879
+
+select * from t0 where k1='Richard' or k1='Tomas' limit 0
+No match
+delete from t0 where k0='Doe'
+Delete OK, 1 row affected
+delete from t0 where k0='Roe'
+Delete OK, 1 row affected
+delete from t0 where k0='Woe'
+Delete OK, 1 row affected
+delete from t0 where k0='Major'
+Delete OK, 1 row affected
+delete from t0 where k0='Kytes'
+Delete OK, 1 row affected
+delete from t0 where k0='Stiles'
+Delete OK, 1 row affected
+delete from t0 where k0='Wales'
+Delete OK, 1 row affected
+delete from t0 where k0='Callaghan'
+Delete OK, 1 row affected
diff --git a/test/box_big/sql.test b/test/box_big/sql.test
index 8fc4b33135..5d94e8911d 100644
--- a/test/box_big/sql.test
+++ b/test/box_big/sql.test
@@ -1,4 +1,10 @@
 # encoding: tarantool
+#
+print """
+A test case for Bug#729758
+"SELECT fails with a disjunct and small LIMIT"
+https://bugs.launchpad.net/tarantool/+bug/729758
+"""
 
 exec sql "insert into t0 values ('Doe', 'Richard')"
 exec sql "insert into t0 values ('Roe', 'Richard')"
@@ -8,7 +14,22 @@ exec sql "insert into t0 values ('Kytes', 'Tomas')"
 exec sql "insert into t0 values ('Stiles', 'Tomas')"
 exec sql "insert into t0 values ('Wales', 'Tomas')"
 exec sql "insert into t0 values ('Callaghan', 'Tomas')"
-# xxx: bug
 exec sql "select * from t0 where k1='Richard' or k1='Tomas' or k1='Tomas' limit 5"
 
+print """
+A test case for Bug#729879
+"Zero limit is treated the same as no limit"
+https://bugs.launchpad.net/tarantool/+bug/729879
+"""
+exec sql "select * from t0 where k1='Richard' or k1='Tomas' limit 0"
+
+# Cleanup
+exec sql "delete from t0 where k0='Doe'"
+exec sql "delete from t0 where k0='Roe'"
+exec sql "delete from t0 where k0='Woe'"
+exec sql "delete from t0 where k0='Major'"
+exec sql "delete from t0 where k0='Kytes'"
+exec sql "delete from t0 where k0='Stiles'"
+exec sql "delete from t0 where k0='Wales'"
+exec sql "delete from t0 where k0='Callaghan'"
 # vim: syntax=python
-- 
GitLab