From 0be1c3dc8f89a0b7ea9c0c89d879cdc15b14acbc Mon Sep 17 00:00:00 2001 From: Gagan Goel Date: Wed, 19 Apr 2023 21:11:06 +0000 Subject: [PATCH] MCOL-5429 Fix high memory consumption in GROUP_CONCAT() processing. 1. Input and output RowGroup's used in GROUP_CONCAT classes are currently allocating a raw memory buffer of size equal to the actual width of the string datatype. As an example, for the following query: SELECT col1, GROUP_CONCAT(col2) FROM t GROUP BY col1; If col2 is a TEXT field with default width, the input RowGroup containing the target rows to be concatenated will assign 64kb of memory for every input row in the RowGroup. This is wasteful as actual field values in real workloads would be much smaller. We fix this by enabling the RowGroup to use the StringStore when the RowGroup contains long strings. 2. RowAggregation::initialize() allocates a memory buffer for a NULL row. The size of this buffer is equal to the row size for the output RowGroup. For the above scenario, using the default group_concat_max_len (which is a server variable that sets the maximum length of the GROUP_CONCAT string) value of 1mb, the buffer size would be (1mb + 64kb + some additional metadata). If the user sets group_concat_max_len to a higher value, say 3gb, this buffer size would be ~3gb. Now if the runtime initiates several instances of RowAggregation, total memory consumption by PrimProc could exceed the hardware memory limits causing the OS OOM to kill the process. We fix this problem by again enabling the StringStore for the NULL row allocation. 3. In the plugin code in buildAggregateColumn(), there is an integer overflow when the server group_concat_max_len variable (which is an uint32_t) is set to a value > INT32_MAX (such as 3gb) and is assigned to CalpontSystemCatalog::ColType::colWidth (which is an int32_t). As a short term fix, we saturate the assigned value to colWidth to INT32_MAX. Proper fix would be to upgrade CalpontSystemCatalog::ColType::colWidth to an uint32_t. --- dbcon/joblist/groupconcat.cpp | 32 ++++++++++++++++++++++++++----- dbcon/joblist/groupconcat.h | 2 ++ dbcon/mysql/ha_mcs_execplan.cpp | 19 +++++++++++++++++- utils/rowgroup/rowaggregation.cpp | 28 +++++++++++++++++++++------ utils/rowgroup/rowaggregation.h | 6 ++++-- utils/rowgroup/rowgroup.h | 5 +++++ 6 files changed, 78 insertions(+), 14 deletions(-) diff --git a/dbcon/joblist/groupconcat.cpp b/dbcon/joblist/groupconcat.cpp index 9c7cb2e78..a90598cf2 100644 --- a/dbcon/joblist/groupconcat.cpp +++ b/dbcon/joblist/groupconcat.cpp @@ -267,6 +267,14 @@ void GroupConcatInfo::mapColumns(const RowGroup& projRG) (*k)->fRowGroup = RowGroup(oids.size(), pos, oids, keys, types, csNums, scale, precision, projRG.getStringTableThreshold(), false); + + // MCOL-5429 Use stringstore if the datatype of the groupconcat + // field is a long string. + if ((*k)->fRowGroup.hasLongString()) + { + (*k)->fRowGroup.setUseStringTable(true); + } + (*k)->fMapping = makeMapping(projRG, (*k)->fRowGroup); } } @@ -318,10 +326,24 @@ void GroupConcatAgUM::initialize() fConcator->initialize(fGroupConcat); - fGroupConcat->fRowGroup.initRow(&fRow, true); - fData.reset(new uint8_t[fRow.getSize()]); - - fRow.setData(rowgroup::Row::Pointer(fData.get())); + // MCOL-5429 Use stringstore if the datatype of the groupconcat + // field is a long string. + if (fGroupConcat->fRowGroup.hasLongString()) + { + fRowGroup = fGroupConcat->fRowGroup; + fRowGroup.setUseStringTable(true); + fRowRGData.reinit(fRowGroup, 1); + fRowGroup.setData(&fRowRGData); + fRowGroup.resetRowGroup(0); + fRowGroup.initRow(&fRow); + fRowGroup.getRow(0, &fRow); + } + else + { + fGroupConcat->fRowGroup.initRow(&fRow, true); + fData.reset(new uint8_t[fRow.getSize()]); + fRow.setData(rowgroup::Row::Pointer(fData.get())); + } } void GroupConcatAgUM::processRow(const rowgroup::Row& inRow) @@ -392,7 +414,7 @@ GroupConcator::~GroupConcator() void GroupConcator::initialize(const rowgroup::SP_GroupConcat& gcc) { // MCOL-901 This value comes from the Server and it is - // too high(3MB) to allocate it for every instance. + // too high(1MB or 3MB by default) to allocate it for every instance. fGroupConcatLen = gcc->fSize; size_t sepSize = gcc->fSeparator.size(); fCurrentLength -= sepSize; // XXX Yet I have to find out why spearator has c_str() as nullptr here. diff --git a/dbcon/joblist/groupconcat.h b/dbcon/joblist/groupconcat.h index fc5c605d7..9f9ba9e0e 100644 --- a/dbcon/joblist/groupconcat.h +++ b/dbcon/joblist/groupconcat.h @@ -92,6 +92,8 @@ class GroupConcatAgUM : public rowgroup::GroupConcatAg boost::scoped_ptr fConcator; boost::scoped_array fData; rowgroup::Row fRow; + rowgroup::RGData fRowRGData; + rowgroup::RowGroup fRowGroup; bool fNoOrder; }; diff --git a/dbcon/mysql/ha_mcs_execplan.cpp b/dbcon/mysql/ha_mcs_execplan.cpp index 9bd7f2908..4aa7f0370 100644 --- a/dbcon/mysql/ha_mcs_execplan.cpp +++ b/dbcon/mysql/ha_mcs_execplan.cpp @@ -5348,7 +5348,24 @@ ReturnedColumn* buildAggregateColumn(Item* item, gp_walk_info& gwi) // Item_func_group_concat* gc = (Item_func_group_concat*)isp; CalpontSystemCatalog::ColType ct; ct.colDataType = CalpontSystemCatalog::VARCHAR; - ct.colWidth = isp->max_length; + + // MCOL-5429 CalpontSystemCatalog::ColType::colWidth is currently + // stored as an int32_t (see calpontsystemcatalog.h). However, + // Item_sum::max_length is an uint32_t. This means there will be an + // integer overflow when Item_sum::max_length > colWidth. This ultimately + // causes an array index out of bound in GroupConcator::swapStreamWithStringAndReturnBuf() + // in groupconcat.cpp when ExeMgr processes groupconcat. As a temporary + // fix, we cap off the max groupconcat length to std::numeric_limits::max(). + // The proper fix would be to change colWidth type to uint32_t. + if (isp->max_length <= std::numeric_limits::max()) + { + ct.colWidth = isp->max_length; + } + else + { + ct.colWidth = std::numeric_limits::max(); + } + ct.precision = 0; ac->resultType(ct); } diff --git a/utils/rowgroup/rowaggregation.cpp b/utils/rowgroup/rowaggregation.cpp index ec3b42ed8..4a716497d 100644 --- a/utils/rowgroup/rowaggregation.cpp +++ b/utils/rowgroup/rowaggregation.cpp @@ -656,7 +656,7 @@ void RowAggregation::resetUDAF(RowUDAFFunctionCol* rowUDAF, uint64_t funcColsIdx // Initilalize the data members to meaningful values, setup the hashmap. // The fRowGroupOut must have a valid data pointer before this. //------------------------------------------------------------------------------ -void RowAggregation::initialize() +void RowAggregation::initialize(bool hasGroupConcat) { // Calculate the length of the hashmap key. fAggMapKeyCount = fGroupByCols.size(); @@ -694,9 +694,25 @@ void RowAggregation::initialize() makeAggFieldsNull(fRow); // Keep a copy of the null row to initialize new map entries. - fRowGroupOut->initRow(&fNullRow, true); - fNullRowData.reset(new uint8_t[fNullRow.getSize()]); - fNullRow.setData(rowgroup::Row::Pointer(fNullRowData.get())); + // MCOL-5429 Use stringstore if the datatype of the groupconcat + // field is a long string. + if (hasGroupConcat && fRowGroupOut->hasLongString()) + { + fNullRowGroup = *fRowGroupOut; + fNullRowGroup.setUseStringTable(true); + fNullRowRGData.reinit(fNullRowGroup, 1); + fNullRowGroup.setData(&fNullRowRGData); + fNullRowGroup.resetRowGroup(0); + fNullRowGroup.initRow(&fNullRow); + fNullRowGroup.getRow(0, &fNullRow); + } + else + { + fRowGroupOut->initRow(&fNullRow, true); + fNullRowData.reset(new uint8_t[fNullRow.getSize()]); + fNullRow.setData(rowgroup::Row::Pointer(fNullRowData.get())); + } + copyRow(fRow, &fNullRow); // Lazy approach w/o a mapping b/w fFunctionCols idx and fRGContextColl idx @@ -2413,7 +2429,7 @@ void RowAggregationUM::endOfInput() //------------------------------------------------------------------------------ // Initilalize the Group Concat data //------------------------------------------------------------------------------ -void RowAggregationUM::initialize() +void RowAggregationUM::initialize(bool hasGroupConcat) { if (fGroupConcat.size() > 0) fFunctionColGc = fFunctionCols; @@ -2423,7 +2439,7 @@ void RowAggregationUM::initialize() fKeyRG = fRowGroupIn.truncate(fGroupByCols.size()); } - RowAggregation::initialize(); + RowAggregation::initialize(fGroupConcat.size() > 0); } //------------------------------------------------------------------------------ diff --git a/utils/rowgroup/rowaggregation.h b/utils/rowgroup/rowaggregation.h index 295ed4712..0f10a6271 100644 --- a/utils/rowgroup/rowaggregation.h +++ b/utils/rowgroup/rowaggregation.h @@ -529,7 +529,7 @@ class RowAggregation : public messageqcpp::Serializeable } protected: - virtual void initialize(); + virtual void initialize(bool hasGroupConcat = false); virtual void initMapData(const Row& row); virtual void attachGroupConcatAg(); @@ -580,6 +580,8 @@ class RowAggregation : public messageqcpp::Serializeable Row fNullRow; Row* tmpRow; // used by the hashers & eq functors boost::scoped_array fNullRowData; + rowgroup::RGData fNullRowRGData; + rowgroup::RowGroup fNullRowGroup; std::unique_ptr fRowAggStorage; @@ -724,7 +726,7 @@ class RowAggregationUM : public RowAggregation protected: // virtual methods from base - void initialize() override; + void initialize(bool hasGroupConcat = false) override; void attachGroupConcatAg() override; void updateEntry(const Row& row, std::vector* rgContextColl = nullptr) override; diff --git a/utils/rowgroup/rowgroup.h b/utils/rowgroup/rowgroup.h index 44bf7f653..694631ab5 100644 --- a/utils/rowgroup/rowgroup.h +++ b/utils/rowgroup/rowgroup.h @@ -1544,6 +1544,11 @@ class RowGroup : public messageqcpp::Serializeable inline bool usesStringTable() const; inline void setUseStringTable(bool); + bool hasLongString() const + { + return hasLongStringField; + } + void serializeRGData(messageqcpp::ByteStream&) const; inline uint32_t getStringTableThreshold() const;