[tarantool-patches] [PATCH 8/9] sql: make LIKE predicate return boolean result

Nikita Pettik korablev at tarantool.org
Sun Apr 14 18:04:06 MSK 2019


According to ANSI, LIKE predicate should return boolean result.
This patch changes type of return value of LIKE predicate.

Part of #3723
---
 src/box/sql/func.c                                 | 12 ++--
 src/box/sql/sqlInt.h                               |  3 +
 src/box/sql/vdbeInt.h                              |  4 ++
 src/box/sql/vdbeapi.c                              |  6 ++
 src/box/sql/vdbemem.c                              |  8 +++
 .../gh-3251-string-pattern-comparison.test.lua     | 76 +++++++++++-----------
 6 files changed, 65 insertions(+), 44 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 860cd8920..baa739ba4 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -974,7 +974,7 @@ likeFunc(sql_context *context, int argc, sql_value **argv)
 		sql_result_error(context, err_msg, -1);
 		return;
 	}
-	sql_result_int(context, res == MATCH);
+	sql_result_boolean(context, res == MATCH);
 }
 
 /*
@@ -1781,9 +1781,9 @@ sqlRegisterLikeFunctions(sql *db, int is_case_insensitive)
 	 * supplied pattern and FALSE otherwise.
 	 */
 	int *is_like_ci = SQL_INT_TO_PTR(is_case_insensitive);
-	sqlCreateFunc(db, "LIKE", FIELD_TYPE_INTEGER, 2, 0,
+	sqlCreateFunc(db, "LIKE", FIELD_TYPE_BOOLEAN, 2, 0,
 			  is_like_ci, likeFunc, 0, 0, 0);
-	sqlCreateFunc(db, "LIKE", FIELD_TYPE_INTEGER, 3, 0,
+	sqlCreateFunc(db, "LIKE", FIELD_TYPE_BOOLEAN, 3, 0,
 			  is_like_ci, likeFunc, 0, 0, 0);
 	setLikeOptFlag(db, "LIKE",
 		       !(is_case_insensitive) ? (SQL_FUNC_LIKE |
@@ -1838,11 +1838,11 @@ sqlRegisterBuiltinFunctions(void)
 		FUNCTION(soundex, 1, 0, 0, soundexFunc),
 #endif
 		FUNCTION2(unlikely, 1, 0, 0, noopFunc, SQL_FUNC_UNLIKELY,
-			  FIELD_TYPE_INTEGER),
+			  FIELD_TYPE_BOOLEAN),
 		FUNCTION2(likelihood, 2, 0, 0, noopFunc, SQL_FUNC_UNLIKELY,
-			  FIELD_TYPE_INTEGER),
+			  FIELD_TYPE_BOOLEAN),
 		FUNCTION2(likely, 1, 0, 0, noopFunc, SQL_FUNC_UNLIKELY,
-			  FIELD_TYPE_INTEGER),
+			  FIELD_TYPE_BOOLEAN),
 		FUNCTION_COLL(ltrim, 1, 1, 0, trimFunc),
 		FUNCTION_COLL(ltrim, 2, 1, 0, trimFunc),
 		FUNCTION_COLL(rtrim, 1, 2, 0, trimFunc),
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 5bb8a8921..10bdb0597 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -492,6 +492,9 @@ sql_result_error_code(sql_context *, int);
 void
 sql_result_int(sql_context *, int);
 
+void
+sql_result_boolean(sql_context *ctx, bool value);
+
 void
 sql_result_int64(sql_context *, sql_int64);
 
diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h
index 5251e76ab..65d262038 100644
--- a/src/box/sql/vdbeInt.h
+++ b/src/box/sql/vdbeInt.h
@@ -495,6 +495,10 @@ void sqlVdbeMemMove(Mem *, Mem *);
 int sqlVdbeMemNulTerminate(Mem *);
 int sqlVdbeMemSetStr(Mem *, const char *, int, u8, void (*)(void *));
 void sqlVdbeMemSetInt64(Mem *, i64);
+
+void
+vdbe_mem_set_boolean(struct Mem *mem, bool value);
+
 #ifdef SQL_OMIT_FLOATING_POINT
 #define sqlVdbeMemSetDouble sqlVdbeMemSetInt64
 #else
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index e1302afc0..3164871cc 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -386,6 +386,12 @@ sql_result_int(sql_context * pCtx, int iVal)
 	sqlVdbeMemSetInt64(pCtx->pOut, (i64) iVal);
 }
 
+void
+sql_result_boolean(sql_context *ctx, bool value)
+{
+	vdbe_mem_set_boolean(ctx->pOut, value);
+}
+
 void
 sql_result_int64(sql_context * pCtx, i64 iVal)
 {
diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index 4fed0eefe..349f5a13e 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -797,6 +797,14 @@ sqlVdbeMemSetInt64(Mem * pMem, i64 val)
 	}
 }
 
+void
+vdbe_mem_set_boolean(struct Mem *mem, bool value)
+{
+	sqlVdbeMemSetNull(mem);
+	mem->u.b = value;
+	mem->flags = MEM_Bool;
+}
+
 #ifndef SQL_OMIT_FLOATING_POINT
 /*
  * Delete any previous value and set the value stored in *pMem to val,
diff --git a/test/sql-tap/gh-3251-string-pattern-comparison.test.lua b/test/sql-tap/gh-3251-string-pattern-comparison.test.lua
index dec3cce91..861ac8ba2 100755
--- a/test/sql-tap/gh-3251-string-pattern-comparison.test.lua
+++ b/test/sql-tap/gh-3251-string-pattern-comparison.test.lua
@@ -8,118 +8,118 @@ local like_test_cases =
 {
     {"1.1",
         "SELECT 'AB' LIKE '_B';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.2",
         "SELECT 'CD' LIKE '_B';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.3",
         "SELECT '' LIKE '_B';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.4",
         "SELECT 'AB' LIKE '%B';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.5",
         "SELECT 'CD' LIKE '%B';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.6",
         "SELECT '' LIKE '%B';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.7",
         "SELECT 'AB' LIKE 'A__';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.8",
         "SELECT 'CD' LIKE 'A__';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.9",
         "SELECT '' LIKE 'A__';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.10",
         "SELECT 'AB' LIKE 'A_';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.11",
         "SELECT 'CD' LIKE 'A_';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.12",
         "SELECT '' LIKE 'A_';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.13",
         "SELECT 'AB' LIKE 'A';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.14",
         "SELECT 'CD' LIKE 'A';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.15",
         "SELECT '' LIKE 'A';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.16",
         "SELECT 'AB' LIKE '_';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.17",
         "SELECT 'CD' LIKE '_';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.18",
         "SELECT '' LIKE '_';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.19",
         "SELECT 'AB' LIKE '__';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.20",
         "SELECT 'CD' LIKE '__';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.21",
         "SELECT '' LIKE '__';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.22",
         "SELECT 'AB' LIKE '%A';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.23",
         "SELECT 'AB' LIKE '%C';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.24",
         "SELECT 'Ñ‘Ñ„' LIKE '%Å“Ø´';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.25",
         "SELECT 'Ñ‘Ñ„â„«Å’Ø´' LIKE '%Å“Ø´';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.26",
         "SELECT 'â„«Å’Ø´' LIKE '%Å“Ø´';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.27",
         "SELECT 'Ñ‘Ñ„' LIKE 'Ñ‘_';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.28",
         "SELECT 'Ñ‘Ñ„â„«Å’Ø´' LIKE 'Ñ‘_';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.29",
         "SELECT 'â„«Å’Ø´' LIKE 'Ñ‘_';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.30",
         "SELECT 'Ñ‘Ñ„' LIKE 'Ñ‘Ñ„%';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.31",
         "SELECT 'Ñ‘Ñ„â„«Å’Ø´' LIKE 'Ñ‘Ñ„%';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.32",
         "SELECT 'â„«Å’Ø´' LIKE 'Ñ‘Ñ„%';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.33",
         "SELECT 'Ñ‘Ñ„' LIKE 'Ñ‘Ñ„â„«%';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.34",
         "SELECT 'Ñ‘Ñ„â„«Å’Ø´' LIKE 'Ñ‘Ñ„â„«%';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.35",
         "SELECT 'â„«Å’Ø´' LIKE 'Ñ‘Ñ„Ø´%';",
-        {0, {0}} },
+        {0, {false}} },
     {"1.36",
         "SELECT 'Ñ‘Ñ„' LIKE 'Ñ‘_%';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.37",
         "SELECT 'Ñ‘Ñ„â„«Å’Ø´' LIKE 'Ñ‘_%';",
-        {0, {1}} },
+        {0, {true}} },
     {"1.38",
         "SELECT 'â„«Å’Ø´' LIKE 'Ñ‘_%';",
-        {0, {0}} },
+        {0, {false}} },
 }
 
 test:do_catchsql_set_test(like_test_cases, prefix)
-- 
2.15.1





More information about the Tarantool-patches mailing list