From f146a81d262e6a1e4e4edb0d30d06c51e179da74 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Thu, 7 Jan 2021 22:30:03 +0800 Subject: [PATCH] [TD-2627]: fix crash in apercentile query. --- src/query/src/qHistogram.c | 3 ++ src/query/tests/histogramTest.cpp | 87 ++++++++++++++++++------------- 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/query/src/qHistogram.c b/src/query/src/qHistogram.c index 0d31948e59..98b825c11f 100644 --- a/src/query/src/qHistogram.c +++ b/src/query/src/qHistogram.c @@ -143,6 +143,9 @@ SHistogramInfo* tHistogramCreateFrom(void* pBuf, int32_t numOfBins) { SHistogramInfo* pHisto = (SHistogramInfo*)pBuf; pHisto->elems = (SHistBin*)((char*)pBuf + sizeof(SHistogramInfo)); + for(int32_t i = 0; i < numOfBins; ++i) { + pHisto->elems[i].val = -DBL_MAX; + } pHisto->maxEntries = numOfBins; diff --git a/src/query/tests/histogramTest.cpp b/src/query/tests/histogramTest.cpp index 82fd4bcf99..3088d6f807 100644 --- a/src/query/tests/histogramTest.cpp +++ b/src/query/tests/histogramTest.cpp @@ -4,42 +4,9 @@ #include #include "taos.h" -#include "tsdb.h" - -#include "tstoken.h" -#include "tutil.h" - #include "qHistogram.h" - -/* test validate the names for table/database */ -TEST(testCase, histogram_binary_search) { - SHistogramInfo* pHisto = tHistogramCreate(MAX_HISTOGRAM_BIN); - - pHisto->numOfEntries = 10; - for (int32_t i = 0; i < 10; ++i) { - pHisto->elems[i].num = 1; - pHisto->elems[i].val = i; - } - - int32_t idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 1); - assert(idx == 1); - - idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 9); - assert(idx == 9); - - idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 20); - assert(idx == 10); - - idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, -1); - assert(idx == 0); - - idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 3.9); - assert(idx == 4); - - free(pHisto); -} - -TEST(testCase, histogram_add) { +namespace { +void doHistogramAddTest() { SHistogramInfo* pHisto = NULL; /** @@ -99,6 +66,56 @@ TEST(testCase, histogram_add) { tHistogramDestroy(&pRes); free(res); } +void doHistogramRepeatTest() { + SHistogramInfo* pHisto = NULL; + struct timeval systemTime; + + gettimeofday(&systemTime, NULL); + int64_t st = + (int64_t)systemTime.tv_sec * 1000L + (uint64_t)systemTime.tv_usec / 1000; + + for (int32_t i = 0; i < 1000; ++i) { + tHistogramAdd(&pHisto, -24 + i); + // tHistogramPrint(pHisto); + } + + tHistogramDestroy(&pHisto); + +} +} + +/* test validate the names for table/database */ +TEST(testCase, histogram_binary_search) { + SHistogramInfo* pHisto = tHistogramCreate(MAX_HISTOGRAM_BIN); + + pHisto->numOfEntries = 10; + for (int32_t i = 0; i < 10; ++i) { + pHisto->elems[i].num = 1; + pHisto->elems[i].val = i; + } + + int32_t idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 1); + assert(idx == 1); + + idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 9); + assert(idx == 9); + + idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 20); + assert(idx == 10); + + idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, -1); + assert(idx == 0); + + idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 3.9); + assert(idx == 4); + + free(pHisto); +} + +TEST(testCase, histogram_add) { + doHistogramAddTest(); + doHistogramRepeatTest(); +} TEST(testCase, heapsort) { // int32_t num = 20; -- GitLab