Skip to content

Commit bf4d0dc

Browse files
idoqospetrunia
authored andcommitted
implement parse and serialize for histogram json
1 parent 9bba595 commit bf4d0dc

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

sql/sql_statistics.cc

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,12 +1261,6 @@ bool Histogram_binary::parse(MEM_ROOT *mem_root, Histogram_type type_arg, const
12611261
*/
12621262
void Histogram_binary::serialize(Field *field)
12631263
{
1264-
if (get_type() == JSON)
1265-
{
1266-
field->store((char*)get_values(), strlen((char*)get_values()),
1267-
&my_charset_bin);
1268-
}
1269-
else
12701264
field->store((char*)get_values(), get_width(), &my_charset_bin);
12711265
}
12721266

@@ -1287,6 +1281,32 @@ void Histogram_json::init_for_collection(MEM_ROOT *mem_root, Histogram_type htyp
12871281
size = (uint8) size_arg;
12881282
}
12891283

1284+
bool Histogram_json::parse(MEM_ROOT *mem_root, Histogram_type type_arg, const uchar *ptr, uint size_arg)
1285+
{
1286+
size = (uint8) size_arg;
1287+
type = type_arg;
1288+
// I think we could use memcpy here, but not sure about how to get the right size
1289+
// since we can't depend on size_arg (it's zero for json histograms)
1290+
// also, does it make sense to cast here? or we can modify json_get_array_items
1291+
// to accept uchar*
1292+
const char *json = (char *)ptr;
1293+
int vt;
1294+
bool result = json_get_array_items(json, json + strlen(json), &vt, hist_buckets);
1295+
fprintf(stderr,"==============\n");
1296+
fprintf(stderr,"histogram: %s\n", json);
1297+
fprintf(stderr, "json_get_array_items() returned %s\n", result ? "true" : "false");
1298+
fprintf(stderr, "value type after json_get_array_items() is %d\n", vt);
1299+
fprintf(stderr, " JSV_BAD_JSON=%d, JSON_VALUE_ARRAY=%d\n", (int)JSV_BAD_JSON, (int)JSON_VALUE_ARRAY);
1300+
fprintf(stderr, "hist_buckets.size()=%zu\n", hist_buckets.size());
1301+
return false;
1302+
}
1303+
1304+
void Histogram_json::serialize(Field *field)
1305+
{
1306+
field->store((char*)get_values(), strlen((char*)get_values()),
1307+
&my_charset_bin);
1308+
}
1309+
12901310
/*
12911311
An object of the class Index_stat is created to read statistical
12921312
data on tables from the statistical table table_stat, to update
@@ -1987,9 +2007,9 @@ class Count_distinct_field: public Sql_alloc
19872007
@brief
19882008
Get the pointer to the histogram built for table_field
19892009
*/
1990-
Histogram_binary *get_histogram()
2010+
Histogram_base *get_histogram()
19912011
{
1992-
return dynamic_cast<Histogram_binary *>(table_field->collected_stats->histogram_);
2012+
return table_field->collected_stats->histogram_;
19932013
}
19942014

19952015
};

sql/sql_statistics.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef SQL_STATISTICS_H
1717
#define SQL_STATISTICS_H
1818

19+
#include <vector>
1920
/*
2021
For COMPLEMENTARY_FOR_QUERIES and PREFERABLY_FOR_QUERIES they are
2122
similar to the COMPLEMENTARY and PREFERABLY respectively except that
@@ -341,11 +342,12 @@ class Histogram_json : public Histogram_base
341342
Histogram_type type;
342343
uint8 size; /* Number of elements in the histogram*/
343344
uchar *values;
345+
std::vector<std::string> hist_buckets;
344346

345347
public:
346-
bool parse(MEM_ROOT *mem_root, Histogram_type type_arg, const uchar *ptr, uint size) override {return false;}
348+
bool parse(MEM_ROOT *mem_root, Histogram_type type_arg, const uchar *ptr, uint size) override;
347349

348-
void serialize(Field *to_field) override{}
350+
void serialize(Field *field) override;
349351

350352
// returns number of buckets in the histogram
351353
uint get_width() override

0 commit comments

Comments
 (0)