From 43895b6f28cfbd0c2861b0acd5bbfe494093230b Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Mon, 19 Jun 2017 18:16:44 +0300 Subject: [PATCH] MXS-1249: Add json functions The json function are treated as read-only when the server version is >= 10.2.3. https://mariadb.com/kb/en/mariadb/json-functions/ --- .../qc_sqlite/builtin_functions.c | 61 ++++++++++++++++++- .../qc_sqlite/builtin_functions.h | 20 +++--- query_classifier/qc_sqlite/qc_sqlite.c | 5 +- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/query_classifier/qc_sqlite/builtin_functions.c b/query_classifier/qc_sqlite/builtin_functions.c index 37fde1477..b157c31e9 100644 --- a/query_classifier/qc_sqlite/builtin_functions.c +++ b/query_classifier/qc_sqlite/builtin_functions.c @@ -1,5 +1,14 @@ /* - * @licence@ + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. */ #include "builtin_functions.h" @@ -368,6 +377,42 @@ static const char* BUILTIN_FUNCTIONS[] = const size_t N_BUILTIN_FUNCTIONS = sizeof(BUILTIN_FUNCTIONS) / sizeof(BUILTIN_FUNCTIONS[0]); +// The functions have been taken from: +// https://mariadb.com/kb/en/mariadb/json-functions + +static const char* BUILTIN_10_2_3_FUNCTIONS[] = +{ + "json_array", + "json_array_append", + "json_array_insert", + "json_compact", + "json_contains", + "json_contains_path", + "json_depth", + "json_detailed", + "json_exists", + "json_extract", + "json_insert", + "json_keys", + "json_length", + "json_loose", + "json_merge", + "json_object", + "json_query", + "json_quote", + "json_remove" + "json_replace", + "json_search", + "json_set", + "json_type", + "json_unquote", + "json_valid", + "json_value" +}; + +const size_t N_BUILTIN_10_2_3_FUNCTIONS = + sizeof(BUILTIN_10_2_3_FUNCTIONS) / sizeof(BUILTIN_10_2_3_FUNCTIONS[0]); + // NOTE: sort_compare and search_compare are not identical, so don't // NOTE: optimize either of them away. static int sort_compare(const void* key, const void* value) @@ -389,6 +434,7 @@ void init_builtin_functions() ss_dassert(!unit.inited); qsort(BUILTIN_FUNCTIONS, N_BUILTIN_FUNCTIONS, sizeof(char*), sort_compare); + qsort(BUILTIN_10_2_3_FUNCTIONS, N_BUILTIN_10_2_3_FUNCTIONS, sizeof(char*), sort_compare); unit.inited = true; } @@ -399,11 +445,22 @@ void finish_builtin_functions() unit.inited = false; } -bool is_builtin_readonly_function(const char* key) +bool is_builtin_readonly_function(const char* key, uint32_t major, uint32_t minor, uint32_t patch) { ss_dassert(unit.inited); char* value = bsearch(key, BUILTIN_FUNCTIONS, N_BUILTIN_FUNCTIONS, sizeof(char*), search_compare); + if (!value) + { + if ((major > 10) || + ((major == 10) && (minor > 2)) || + ((major == 10) && (minor == 2) && (patch >= 3))) + { + value = bsearch(key, BUILTIN_10_2_3_FUNCTIONS, N_BUILTIN_10_2_3_FUNCTIONS, + sizeof(char*), search_compare); + } + } + return value ? true : false; } diff --git a/query_classifier/qc_sqlite/builtin_functions.h b/query_classifier/qc_sqlite/builtin_functions.h index 9c0da9e47..49768aee8 100644 --- a/query_classifier/qc_sqlite/builtin_functions.h +++ b/query_classifier/qc_sqlite/builtin_functions.h @@ -1,11 +1,19 @@ -#ifndef BUILTIN_FUNCTIONS_H -#define BUILTIN_FUNCTIONS_H -/** - * @LICENCE@ +#pragma once +/* + * Copyright (c) 2016 MariaDB Corporation Ab * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. */ #include +#include #ifdef __cplusplus extern "C" { @@ -14,10 +22,8 @@ extern "C" { void init_builtin_functions(); void finish_builtin_functions(); -bool is_builtin_readonly_function(const char* zToken); +bool is_builtin_readonly_function(const char* zToken, uint32_t major, uint32_t minor, uint32_t patch); #ifdef __cplusplus } #endif - -#endif diff --git a/query_classifier/qc_sqlite/qc_sqlite.c b/query_classifier/qc_sqlite/qc_sqlite.c index 63bea623b..0cfe41610 100644 --- a/query_classifier/qc_sqlite/qc_sqlite.c +++ b/query_classifier/qc_sqlite/qc_sqlite.c @@ -1105,7 +1105,10 @@ static void update_field_infos(QC_SQLITE_INFO* info, { info->type_mask |= (QUERY_TYPE_READ | QUERY_TYPE_MASTER_READ); } - else if (!is_builtin_readonly_function(zToken)) + else if (!is_builtin_readonly_function(zToken, + this_thread.version_major, + this_thread.version_minor, + this_thread.version_patch)) { info->type_mask |= QUERY_TYPE_WRITE; }