Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: vdavydov@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 09/10] sql: fix quote() function
Date: Fri, 13 Aug 2021 06:17:25 +0300	[thread overview]
Message-ID: <a3338ba39806fe44f923c8c78d6e2a3deede5080.1628824421.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1628824421.git.imeevma@gmail.com>

Ater this patch SQL built-in function quite will return the same number
in case it receives number as an argument. If the argument is not
number, string representation of the argument will be returned.

Part of #6105
---
 src/box/sql/func.c                       | 18 ++-------
 test/sql-tap/built-in-functions.test.lua | 47 +++++++++++++++++++++++-
 test/sql-tap/trigger5.test.lua           |  3 +-
 test/sql/types.result                    | 14 +++----
 4 files changed, 59 insertions(+), 23 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 8d9c5a7d9..98f7169c0 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1092,26 +1092,13 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv)
 	assert(argc == 1);
 	UNUSED_PARAMETER(argc);
 	switch (argv[0]->type) {
-	case MEM_TYPE_DOUBLE:{
-			double r1, r2;
-			char zBuf[50];
-			r1 = mem_get_double_unsafe(argv[0]);
-			sql_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
-			sqlAtoF(zBuf, &r2, 20);
-			if (r1 != r2) {
-				sql_snprintf(sizeof(zBuf), zBuf, "%!.20e",
-						 r1);
-			}
-			sql_result_text(context, zBuf, -1,
-					    SQL_TRANSIENT);
-			break;
-		}
 	case MEM_TYPE_UUID: {
 		char buf[UUID_STR_LEN + 1];
 		tt_uuid_to_string(&argv[0]->u.uuid, &buf[0]);
 		sql_result_text(context, buf, UUID_STR_LEN, SQL_TRANSIENT);
 		break;
 	}
+	case MEM_TYPE_DOUBLE:
 	case MEM_TYPE_UINT:
 	case MEM_TYPE_INT: {
 			sql_result_value(context, argv[0]);
@@ -2094,6 +2081,9 @@ static struct sql_func_definition definitions[] = {
 	 FIELD_TYPE_INTEGER, position_func, NULL},
 	{"PRINTF", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, printfFunc, 
 	 NULL},
+	{"QUOTE", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, quoteFunc, NULL},
+	{"QUOTE", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, quoteFunc, NULL},
+	{"QUOTE", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, quoteFunc, NULL},
 	{"QUOTE", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_STRING, quoteFunc, NULL},
 	{"RANDOM", 0, {}, FIELD_TYPE_INTEGER, randomFunc, NULL},
 	{"RANDOMBLOB", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_VARBINARY,
diff --git a/test/sql-tap/built-in-functions.test.lua b/test/sql-tap/built-in-functions.test.lua
index 6fae811dc..5fae8f357 100755
--- a/test/sql-tap/built-in-functions.test.lua
+++ b/test/sql-tap/built-in-functions.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 local test = require("sqltester")
-test:plan(52)
+test:plan(56)
 
 --
 -- Make sure that number of arguments check is checked properly for SQL built-in
@@ -545,4 +545,49 @@ test:do_test(
         {name = "COLUMN_2", type = "scalar"},
     })
 
+--
+-- Make sure QUOTE() returns the same number if it takes a number as an
+-- argument, otherwise it returns a string.
+--
+test:do_execsql_test(
+    "builtins-4.1",
+    [[
+        SELECT QUOTE(1), 1 = QUOTE(1);
+    ]],
+    {
+        1, true
+    }
+)
+
+test:do_execsql_test(
+    "builtins-4.2",
+    [[
+        SELECT QUOTE(1.5), 1.5 = QUOTE(1.5);
+    ]],
+    {
+        1.5, true
+    }
+)
+
+test:do_execsql_test(
+    "builtins-4.3",
+    [[
+        SELECT QUOTE(CAST(1 AS NUMBER)),
+        CAST(1 AS NUMBER) = QUOTE(CAST(1 AS NUMBER));
+    ]],
+    {
+        1, true
+    }
+)
+
+test:do_execsql_test(
+    "builtins-4.4",
+    [[
+        SELECT QUOTE('1'), QUOTE(x'31'), QUOTE(true);
+    ]],
+    {
+        "'1'","X'31'","TRUE"
+    }
+)
+
 test:finish_test()
diff --git a/test/sql-tap/trigger5.test.lua b/test/sql-tap/trigger5.test.lua
index 8336cdcf2..7f3762126 100755
--- a/test/sql-tap/trigger5.test.lua
+++ b/test/sql-tap/trigger5.test.lua
@@ -31,7 +31,8 @@ test:do_execsql_test(
           INSERT INTO Undo VALUES
              ((SELECT coalesce(max(id),0) + 1 FROM Undo),
               (SELECT 'INSERT INTO Item (a,b,c) VALUES (' || CAST(coalesce(old.a,'NULL') AS TEXT)
-                  || ',' || quote(old.b) || ',' || CAST(old.c AS TEXT) || ');'));
+                  || ',' || CAST(quote(old.b) AS STRING) || ',' ||
+                  CAST(old.c AS TEXT) || ');'));
         END;
         DELETE FROM Item WHERE a = 1;
         SELECT * FROM Undo;
diff --git a/test/sql/types.result b/test/sql/types.result
index 3017793c6..ec80dfc14 100644
--- a/test/sql/types.result
+++ b/test/sql/types.result
@@ -847,7 +847,7 @@ box.execute("SELECT quote(i) FROM t;")
 ---
 - metadata:
   - name: COLUMN_1
-    type: string
+    type: integer
   rows:
   - [18446744073709551613]
 ...
@@ -863,7 +863,7 @@ box.execute("SELECT quote(i) FROM t;")
 ---
 - metadata:
   - name: COLUMN_1
-    type: string
+    type: integer
   rows:
   - [18446744073709551613]
 ...
@@ -1883,12 +1883,12 @@ box.execute("SELECT quote(d) FROM t;")
 ---
 - metadata:
   - name: COLUMN_1
-    type: string
+    type: double
   rows:
-  - ['10.0']
-  - ['-2.0']
-  - ['3.3']
-  - ['1.8e+19']
+  - [10]
+  - [-2]
+  - [3.3]
+  - [18000000000000000000]
 ...
 box.execute("SELECT LEAST(d, 0) FROM t;")
 ---
-- 
2.25.1


  parent reply	other threads:[~2021-08-13  3:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-13  3:17 [Tarantool-patches] [PATCH v1 00/10] Check types of SQL built-in functions arguments Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 01/10] sql: modify signature of TRIM() Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 02/10] sql: rework SQL built-in functions hash table Mergen Imeev via Tarantool-patches
2021-08-16 13:53   ` Vladimir Davydov via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 03/10] sql: check number of arguments during parsing Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 04/10] sql: static type check for SQL built-in functions Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 05/10] sql: runtime " Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 06/10] sql: enable types checking for some functions Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 07/10] sql: fix result type of min() and max() functions Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 08/10] sql: check argument types of sum(), avg(), total() Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` Mergen Imeev via Tarantool-patches [this message]
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 10/10] sql: arguments check for string value functions Mergen Imeev via Tarantool-patches
2021-08-19 11:49 ` [Tarantool-patches] [PATCH v1 00/10] Check types of SQL built-in functions arguments Vladimir Davydov via Tarantool-patches

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=a3338ba39806fe44f923c8c78d6e2a3deede5080.1628824421.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 09/10] sql: fix quote() function' \
    /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