summaryrefslogtreecommitdiff
path: root/sql/item_jsonfunc.h
diff options
context:
space:
mode:
authortanruixiang <819464715@qq.com>2022-06-16 15:05:35 +0800
committertanruixiang <819464715@qq.com>2022-09-06 01:35:26 +0800
commitb8eb48ed933b1418c51c4e7e194a82ed1082a6c2 (patch)
tree34daabb65f145fba43af4f85ad61142cecdb67e9 /sql/item_jsonfunc.h
parent12c236415930c789a422b313c3bd9b9b5fe494af (diff)
downloadmariadb-git-bb-10.11-MDEV-26182-json_intersect.tar.gz
MDEV-26182: Implement JSON_INTERSECT()bb-10.11-MDEV-26182-json_intersect
First we introduce the compare_whole parameter. The intersection results of different types of values vary according to compare_whole. If compare_whole is true (When the array or object or scalar is not at the outermost layer, compare_whole is true.): it means that the two jsons being compared, whether they are scalars, arrays or objects, must be exactly equal to have an intersection. Different types indicate that there is no intersection between jsons. If two scalars are compared, there is an intersection between them, which means that the two scalars are exactly the same. If two arrays are compared, there is an intersection between them, which means that the values at all indices of the two arrays are equal. If two objects are compared, there is an intersection between them, which means that all KV pairs of the two objects are equal. if compare_whole is false: When a value is a subset of another value, there is an intersection. For example, taking the intersection of two objects is taking their common KV pairs. The intersection of array and array is to take their common elements. An object and a scalar take an intersection with an array. As long as the object or scalar exists in the array, the intersection is the object or the scalar. We put a json into the hash, scan another json, and efficiently get the intersection by reading and updating the hash information.
Diffstat (limited to 'sql/item_jsonfunc.h')
-rw-r--r--sql/item_jsonfunc.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/sql/item_jsonfunc.h b/sql/item_jsonfunc.h
index 82efddb5a5a..1ba9a70648b 100644
--- a/sql/item_jsonfunc.h
+++ b/sql/item_jsonfunc.h
@@ -59,7 +59,48 @@ int json_find_overlap_with_array(json_engine_t *js,
json_engine_t *value,
bool compare_whole);
+struct LEX_CSTRING_KEYVALUE
+{
+ LEX_CSTRING key;
+ LEX_CSTRING value;
+ int count= 0;
+
+ static uchar *
+ get_hash_key(const uchar *data, size_t *len_ret,
+ my_bool __attribute__((unused)))
+ {
+ LEX_CSTRING_KEYVALUE *e= (LEX_CSTRING_KEYVALUE *) data;
+ *len_ret= e->key.length;
+ return (uchar *) e->key.str;
+ }
+
+ static void hash_free(void *ptr)
+ {
+ my_free(ptr);
+ }
+};
+bool check_intersect(String *str, json_engine_t *js,
+ json_engine_t *value, bool compare_whole);
+bool json_find_intersect_with_object(String *str, json_engine_t *js,
+ json_engine_t *value, bool compare_whole);
+bool check_unique_key_in_object(json_engine_t *js);
+bool json_find_intersect_with_array(String *str, json_engine_t *js,
+ json_engine_t *value, bool compare_whole);
+bool check_unique_key_in_js(json_engine_t *js);
+bool get_hash_from_json(json_engine_t *value, HASH &property_hash);
+bool get_object_hash_from_json(json_engine_t *value, HASH &property_hash);
+bool get_array_hash_from_json(json_engine_t *value, HASH &property_hash);
+bool create_kv_pair_and_search_in_hash(LEX_CSTRING_KEYVALUE *&new_entry,
+ HASH &property_hash, uchar *&search_result,
+ const uchar *value_start, size_t value_len,
+ const uchar *key_start, size_t key_len);
+bool get_value_from_json(json_engine_t *js, const uchar *&value_start,
+ size_t &value_len);
+bool json_intersect_arr_and_obj(String *str, json_engine_t *js,
+ json_engine_t *value);
+bool json_intersect_between_arrays(String *str, json_engine_t *js,
+ json_engine_t *value);
class Json_engine_scan: public json_engine_t
{
@@ -791,4 +832,24 @@ public:
{ return get_item_copy<Item_func_json_overlaps>(thd, this); }
};
+class Item_func_json_intersect: public Item_str_func
+{
+protected:
+ String tmp_js1, tmp_js2;
+public:
+ Item_func_json_intersect(THD *thd, Item *a, Item *b):
+ Item_str_func(thd, a, b) {}
+ String *val_str(String *) override;
+ bool fix_length_and_dec(THD *thd) override;
+ LEX_CSTRING func_name_cstring() const override
+ {
+ static LEX_CSTRING name= {STRING_WITH_LEN("json_intersect") };
+ return name;
+ }
+ Item *get_copy(THD *thd) override
+ { return get_item_copy<Item_func_json_intersect>(thd, this); }
+};
+
+
+
#endif /* ITEM_JSONFUNC_INCLUDED */