[Tarantool-patches] [PATCH v1 2/3] sql: fix uuid behaviour in least() and greatest()

imeevma at tarantool.org imeevma at tarantool.org
Mon Jul 5 18:06:15 MSK 2021


Prior to this patch, the built-in SQL functions greatest() and least()
could return incorrect results if their arguments contained at least one
uuid and at least one value of a different type. These values are now
sorted according to the rules of SCALAR comparison, where UUID values
are larger than any other scalar values.

Part of #6164
---
 src/box/sql/mem.c                             |  4 ++--
 test/sql-tap/gh-6164-uuid-follow-ups.test.lua | 21 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 6f3bf52e5..330ddfd8e 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -2480,8 +2480,8 @@ sqlMemCompare(const Mem * pMem1, const Mem * pMem2, const struct coll * pColl)
 		if (mem_cmp_uuid(pMem1, pMem2, &res) == 0)
 			return res;
 		if (type1 != MEM_TYPE_UUID)
-			return +1;
-		return -1;
+			return -1;
+		return +1;
 	}
 
 	/* At least one of the two values is a number
diff --git a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua
index d8fa700ea..8872f9f23 100755
--- a/test/sql-tap/gh-6164-uuid-follow-ups.test.lua
+++ b/test/sql-tap/gh-6164-uuid-follow-ups.test.lua
@@ -1,6 +1,8 @@
 #!/usr/bin/env tarantool
 local test = require("sqltester")
-test:plan(1)
+test:plan(3)
+
+local uuid = require('uuid').fromstr('11111111-1111-1111-1111-111111111111')
 
 box.execute([[select quote(cast('11111111-1111-1111-1111-111111111111' as uuid));]])
 
@@ -13,4 +15,21 @@ test:do_execsql_test(
         '11111111-1111-1111-1111-111111111111'
     })
 
+-- Make sure that functions greatest() and least() can properly work with uuid.
+test:do_execsql_test(
+    "gh-6164-2",
+    [[
+        SELECT GREATEST(true, 1, x'33', cast('11111111-1111-1111-1111-111111111111' as uuid), 1e10);
+    ]], {
+        uuid
+    })
+
+test:do_execsql_test(
+    "gh-6164-3",
+    [[
+        SELECT LEAST(true, 1, x'33', cast('11111111-1111-1111-1111-111111111111' as uuid), 1e10);
+    ]], {
+        true
+    })
+
 test:finish_test()
-- 
2.25.1



More information about the Tarantool-patches mailing list