From bf4d0dcfe2d30b42861293af995a3b8d5c4acfb1 Mon Sep 17 00:00:00 2001 From: Michael Okoko Date: Fri, 30 Jul 2021 06:55:17 +0100 Subject: [PATCH] implement parse and serialize for histogram json --- sql/sql_statistics.cc | 36 ++++++++++++++++++++++++++++-------- sql/sql_statistics.h | 6 ++++-- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc index 9e22c82d920..8bd181e1939 100644 --- a/sql/sql_statistics.cc +++ b/sql/sql_statistics.cc @@ -1261,12 +1261,6 @@ bool Histogram_binary::parse(MEM_ROOT *mem_root, Histogram_type type_arg, const */ void Histogram_binary::serialize(Field *field) { - if (get_type() == JSON) - { - field->store((char*)get_values(), strlen((char*)get_values()), - &my_charset_bin); - } - else field->store((char*)get_values(), get_width(), &my_charset_bin); } @@ -1287,6 +1281,32 @@ void Histogram_json::init_for_collection(MEM_ROOT *mem_root, Histogram_type htyp size = (uint8) size_arg; } +bool Histogram_json::parse(MEM_ROOT *mem_root, Histogram_type type_arg, const uchar *ptr, uint size_arg) +{ + size = (uint8) size_arg; + type = type_arg; + // I think we could use memcpy here, but not sure about how to get the right size + // since we can't depend on size_arg (it's zero for json histograms) + // also, does it make sense to cast here? or we can modify json_get_array_items + // to accept uchar* + const char *json = (char *)ptr; + int vt; + bool result = json_get_array_items(json, json + strlen(json), &vt, hist_buckets); + fprintf(stderr,"==============\n"); + fprintf(stderr,"histogram: %s\n", json); + fprintf(stderr, "json_get_array_items() returned %s\n", result ? "true" : "false"); + fprintf(stderr, "value type after json_get_array_items() is %d\n", vt); + fprintf(stderr, " JSV_BAD_JSON=%d, JSON_VALUE_ARRAY=%d\n", (int)JSV_BAD_JSON, (int)JSON_VALUE_ARRAY); + fprintf(stderr, "hist_buckets.size()=%zu\n", hist_buckets.size()); + return false; +} + +void Histogram_json::serialize(Field *field) +{ + field->store((char*)get_values(), strlen((char*)get_values()), + &my_charset_bin); +} + /* An object of the class Index_stat is created to read statistical data on tables from the statistical table table_stat, to update @@ -1987,9 +2007,9 @@ public: @brief Get the pointer to the histogram built for table_field */ - Histogram_binary *get_histogram() + Histogram_base *get_histogram() { - return dynamic_cast(table_field->collected_stats->histogram_); + return table_field->collected_stats->histogram_; } }; diff --git a/sql/sql_statistics.h b/sql/sql_statistics.h index 76ab8694789..3e4e917dc46 100644 --- a/sql/sql_statistics.h +++ b/sql/sql_statistics.h @@ -16,6 +16,7 @@ #ifndef SQL_STATISTICS_H #define SQL_STATISTICS_H +#include /* For COMPLEMENTARY_FOR_QUERIES and PREFERABLY_FOR_QUERIES they are similar to the COMPLEMENTARY and PREFERABLY respectively except that @@ -341,11 +342,12 @@ private: Histogram_type type; uint8 size; /* Number of elements in the histogram*/ uchar *values; + std::vector hist_buckets; public: - bool parse(MEM_ROOT *mem_root, Histogram_type type_arg, const uchar *ptr, uint size) override {return false;} + bool parse(MEM_ROOT *mem_root, Histogram_type type_arg, const uchar *ptr, uint size) override; - void serialize(Field *to_field) override{} + void serialize(Field *field) override; // returns number of buckets in the histogram uint get_width() override