[tarantool-patches] [PATCH v2 04/12] sql: rework LIKE case-insensitive mode

Kirill Shcherbatov kshcherbatov at tarantool.org
Wed Jul 10 14:01:03 MSK 2019


The case_sensitive_like pragma used to process a strange way: on
each call a new instance of function having appropriate
properties was created. Reworked legacy code with user session
object to define this property there. The new approach allows
to rework sql builtins machinery with further patches.

Needed for #4113, #2200, #2233
---
 src/box/sql/func.c   | 41 +++++------------------------------------
 src/box/sql/pragma.c |  8 --------
 src/box/sql/sqlInt.h |  3 ---
 3 files changed, 5 insertions(+), 47 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 580571709..ae75a1c3c 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -39,6 +39,7 @@
 #include "version.h"
 #include "coll/coll.h"
 #include "tarantoolInt.h"
+#include "box/session.h"
 #include <unicode/ustring.h>
 #include <unicode/ucasemap.h>
 #include <unicode/ucnv.h>
@@ -885,7 +886,8 @@ likeFunc(sql_context *context, int argc, sql_value **argv)
 	u32 escape = SQL_END_OF_STRING;
 	int nPat;
 	sql *db = sql_context_db_handle(context);
-	int is_like_ci = SQL_PTR_TO_INT(sql_user_data(context));
+	bool is_like_ci =
+		(current_session()->sql_flags & LIKE_CASE_SENS_FLAG) == 0;
 	int rhs_type = sql_value_type(argv[0]);
 	int lhs_type = sql_value_type(argv[1]);
 
@@ -1704,40 +1706,6 @@ groupConcatFinalize(sql_context * context)
 	}
 }
 
-/*
- * Set the LIKEOPT flag on the 2-argument function with the given name.
- */
-static void
-setLikeOptFlag(sql * db, const char *zName, u8 flagVal)
-{
-	FuncDef *pDef;
-	pDef = sqlFindFunction(db, zName, 2, 0);
-	if (ALWAYS(pDef)) {
-		pDef->funcFlags |= flagVal;
-	}
-}
-
-/**
- * Register the built-in LIKE function.
- */
-void
-sqlRegisterLikeFunctions(sql *db, int is_case_insensitive)
-{
-	/*
-	 * FIXME: after introducing type <BOOLEAN> LIKE must
-	 * return that type: TRUE if the string matches the
-	 * supplied pattern and FALSE otherwise.
-	 */
-	int *is_like_ci = SQL_INT_TO_PTR(is_case_insensitive);
-	sqlCreateFunc(db, "LIKE", FIELD_TYPE_BOOLEAN, 2, 0,
-			  is_like_ci, likeFunc, 0, 0, 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 |
-		       SQL_FUNC_CASE) : SQL_FUNC_LIKE);
-}
-
 int
 sql_is_like_func(struct sql *db, struct Expr *expr, int *is_like_ci)
 {
@@ -1749,7 +1717,8 @@ sql_is_like_func(struct sql *db, struct Expr *expr, int *is_like_ci)
 	assert(func != NULL);
 	if ((func->funcFlags & SQL_FUNC_LIKE) == 0)
 		return 0;
-	*is_like_ci = (func->funcFlags & SQL_FUNC_CASE) == 0;
+	*is_like_ci =
+		(current_session()->sql_flags & LIKE_CASE_SENS_FLAG) == 0;
 	return 1;
 }
 
diff --git a/src/box/sql/pragma.c b/src/box/sql/pragma.c
index 53524b617..1fd74c0a3 100644
--- a/src/box/sql/pragma.c
+++ b/src/box/sql/pragma.c
@@ -484,17 +484,9 @@ sqlPragma(Parse * pParse, Token * pId,	/* First part of [schema.]id field */
 					sqlParserTrace(0, 0);
 			}
 #endif
-			/*
-			 * Reinstall the LIKE and functions. The
-			 * variant of LIKE * used will be case
-			 * sensitive or not depending on the RHS.
-			 */
-			if (mask == LIKE_CASE_SENS_FLAG)
-				sqlRegisterLikeFunctions(db, !is_pragma_set);
 		}
 		break;
 	}
-
 	case PragTyp_TABLE_INFO:
 		sql_pragma_table_info(pParse, zRight);
 		break;
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 9f439fb07..7bcbb4eb1 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -1271,7 +1271,6 @@ struct FuncDestructor {
  *     SQL_FUNC_CONSTANT  ==  sql_DETERMINISTIC from the API
  */
 #define SQL_FUNC_LIKE     0x0004	/* Candidate for the LIKE optimization */
-#define SQL_FUNC_CASE     0x0008	/* Case-sensitive LIKE-type function */
 #define SQL_FUNC_EPHEM    0x0010	/* Ephemeral.  Delete with VDBE */
 #define SQL_FUNC_NEEDCOLL 0x0020	/* sqlGetFuncCollSeq() might be called.
 					 * The flag is set when the collation
@@ -4262,8 +4261,6 @@ sql_key_info_unref(struct sql_key_info *key_info);
 struct key_def *
 sql_key_info_to_key_def(struct sql_key_info *key_info);
 
-void sqlRegisterLikeFunctions(sql *, int);
-
 /**
  * Check if the function implements LIKE-style comparison & if it
  * is appropriate to apply a LIKE query optimization.
-- 
2.21.0





More information about the Tarantool-patches mailing list