[Tarantool-patches] [PATCH v6 12/22] sql: check args of round()

imeevma at tarantool.org imeevma at tarantool.org
Thu Jul 16 17:46:26 MSK 2020


After this patch, the argument types of the round() function will be
checked properly.

Part of #4159
---
 src/box/sql/func.c          | 24 ++++++++---------
 test/sql-tap/func.test.lua  |  6 ++---
 test/sql-tap/func5.test.lua | 51 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index a52b9a103..d2b968702 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -789,21 +789,17 @@ roundFunc(sql_context * context, int argc, sql_value ** argv)
 		return;
 	}
 	if (argc == 2) {
-		if (sql_value_is_null(argv[1]))
-			return;
+		enum mp_type precision_type = sql_value_type(argv[1]);
+		if (precision_type == MP_NIL)
+			return sql_result_null(context);
+		assert(precision_type == MP_UINT);
 		n = sql_value_int(argv[1]);
-		if (n < 0)
-			n = 0;
+		assert(n >= 0);
 	}
-	if (sql_value_is_null(argv[0]))
-		return;
 	enum mp_type mp_type = sql_value_type(argv[0]);
-	if (mp_type_is_bloblike(mp_type)) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
-			 sql_value_to_diag_str(argv[0]), "numeric");
-		context->is_aborted = true;
-		return;
-	}
+	if (mp_type == MP_NIL)
+		return sql_result_null(context);
+	assert(mp_type == MP_DOUBLE);
 	r = sql_value_double(argv[0]);
 	/* If Y==0 and X will fit in a 64-bit int,
 	 * handle the rounding directly,
@@ -2763,8 +2759,8 @@ static struct {
 	}, {
 	 .name = "ROUND",
 	 .param_count = -1,
-	 .first_arg = FIELD_TYPE_ANY,
-	 .args = FIELD_TYPE_ANY,
+	 .first_arg = FIELD_TYPE_DOUBLE,
+	 .args = FIELD_TYPE_UNSIGNED,
 	 .is_blob_like_str = false,
 	 .returns = FIELD_TYPE_INTEGER,
 	 .aggregate = FUNC_AGGREGATE_NONE,
diff --git a/test/sql-tap/func.test.lua b/test/sql-tap/func.test.lua
index 2ae36fe64..9d8bf94ed 100755
--- a/test/sql-tap/func.test.lua
+++ b/test/sql-tap/func.test.lua
@@ -502,13 +502,13 @@ test:do_execsql_test(
         -- </func-4.12>
     })
 
-test:do_execsql_test(
+test:do_catchsql_test(
     "func-4.13",
     [[
         SELECT round(t1,2) FROM tbl1
     ]], {
         -- <func-4.13>
-        0.0, 0.0, 0.0, 0.0, 0.0
+        1, "Type mismatch: can not convert this to double"
         -- </func-4.13>
     })
 
@@ -2918,7 +2918,7 @@ test:do_catchsql_test(
         SELECT ROUND(X'FF')
     ]], {
         -- <func-76.1>
-        1, "Type mismatch: can not convert varbinary to numeric"
+        1, "Type mismatch: can not convert varbinary to double"
         -- </func-76.1>
     })
 
diff --git a/test/sql-tap/func5.test.lua b/test/sql-tap/func5.test.lua
index bf7a338a1..3351e398b 100755
--- a/test/sql-tap/func5.test.lua
+++ b/test/sql-tap/func5.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 test = require("sqltester")
-test:plan(120)
+test:plan(127)
 
 --!./tcltestrunner.lua
 -- 2010 August 27
@@ -983,4 +983,53 @@ test:do_execsql_test(
         "34"
     })
 
+test:do_execsql_test(
+    "func-5-6.15.1", [[
+        SELECT round(NULL, NULL);
+    ]],{
+        ""
+    })
+
+test:do_execsql_test(
+    "func-5-6.15.2", [[
+        SELECT round(123, 3);
+    ]], {
+        123
+    })
+
+test:do_catchsql_test(
+    "func-5-6.15.3", [[
+        SELECT round(-123, -3);
+    ]], {
+        1, "Type mismatch: can not convert -3 to unsigned"
+    })
+
+test:do_execsql_test(
+    "func-5-6.15.4", [[
+        SELECT round(-5.5, 2.5);
+    ]], {
+        -5.5
+    })
+
+test:do_catchsql_test(
+    "func-5-6.15.5", [[
+        SELECT round('-123', '-123');
+    ]], {
+        1, "Type mismatch: can not convert -123 to double"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.15.6", [[
+        SELECT round(false, true);
+    ]], {
+        1, "Type mismatch: can not convert FALSE to double"
+    })
+
+test:do_catchsql_test(
+    "func-5-6.15.7", [[
+        SELECT round(X'3334', X'35');
+    ]], {
+        1, "Type mismatch: can not convert varbinary to double"
+    })
+
 test:finish_test()
-- 
2.25.1



More information about the Tarantool-patches mailing list