From 56177e18493dd4fea0a55b665489ffaa3930a2aa Mon Sep 17 00:00:00 2001 From: wpan Date: Tue, 27 Jul 2021 10:55:44 +0800 Subject: [PATCH] fix mem leak --- src/client/src/tscSQLParser.c | 45 ++++---- src/common/src/texpr.c | 30 +++--- src/query/inc/qFilter.h | 5 +- src/query/src/qExecutor.c | 9 +- src/query/src/qFilter.c | 188 +++++++++++++++++++++++++++------- src/query/src/qSqlParser.c | 4 +- src/query/src/queryMain.c | 3 + src/util/src/hash.c | 3 +- 8 files changed, 203 insertions(+), 84 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 919dc392ed..da7f4d5750 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -3424,8 +3424,8 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, retVal = tVariantDump(&pRight->value, (char*)pColumnFilter->pz, colType, false); } else if (colType == TSDB_DATA_TYPE_NCHAR) { - // pRight->value.nLen + 1 is larger than the actual nchar string length - pColumnFilter->pz = (int64_t)calloc(1, bufLen * TSDB_NCHAR_SIZE); + // bufLen + 1 is larger than the actual nchar string length + pColumnFilter->pz = (int64_t)calloc(1, (bufLen + 1) * TSDB_NCHAR_SIZE); retVal = tVariantDump(&pRight->value, (char*)pColumnFilter->pz, colType, false); size_t len = twcslen((wchar_t*)pColumnFilter->pz); pColumnFilter->len = len * TSDB_NCHAR_SIZE; @@ -3652,14 +3652,10 @@ static int32_t getColQueryCondExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlEx } tExprNode* p = NULL; - //SFilterInfo colFilter = {0}; - - SArray* colList = taosArrayInit(10, sizeof(SColIndex)); - ret = exprTreeFromSqlExpr(pCmd, &p, p1, pQueryInfo, colList, NULL); - //if (ret == TSDB_CODE_SUCCESS) { - // ret = filterInitFromTree(p, &colFilter, (int32_t)taosArrayGetSize(colList)); - //} + SArray* colList = taosArrayInit(10, sizeof(SColIndex)); + ret = exprTreeFromSqlExpr(pCmd, &p, p1, pQueryInfo, colList, NULL); + taosArrayDestroy(colList); SBufferWriter bw = tbufInitWriter(NULL, false); @@ -5045,8 +5041,10 @@ static int32_t getQueryTimeRange(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr tExprNode* p = NULL; SFilterInfo *filter = NULL; - SArray* colList = taosArrayInit(10, sizeof(SColIndex)); + SArray* colList = taosArrayInit(10, sizeof(SColIndex)); ret = exprTreeFromSqlExpr(pCmd, &p, *pExpr, pQueryInfo, colList, NULL); + taosArrayDestroy(colList); + if (ret != TSDB_CODE_SUCCESS) { goto _ret; } @@ -5058,6 +5056,8 @@ static int32_t getQueryTimeRange(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tSqlExpr ret = filterGetTimeRange(filter, &pQueryInfo->window); + filterFreeInfo(filter); + _ret: tExprTreeDestroy(p, NULL); @@ -5100,7 +5100,7 @@ int32_t validateWhereNode(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSq #endif if ((ret = getQueryCondExpr(&pSql->cmd, pQueryInfo, pExpr, &condExpr, etype, &tbIdx, (*pExpr)->tokenId, &condExpr.pColumnCond, &condExpr.pTimewindow)) != TSDB_CODE_SUCCESS) { - return ret; + goto PARSE_WHERE_EXIT; } if (taosArrayGetSize(pQueryInfo->pUpstream) > 0 && condExpr.pTimewindow != NULL) { @@ -5115,21 +5115,21 @@ int32_t validateWhereNode(SQueryInfo* pQueryInfo, tSqlExpr** pExpr, SSqlObj* pSq // 1. check if it is a join query if ((ret = validateJoinExpr(&pSql->cmd, pQueryInfo, &condExpr)) != TSDB_CODE_SUCCESS) { - return ret; + goto PARSE_WHERE_EXIT; } // 2. get the query time range if ((ret = convertTimeRangeFromExpr(&pSql->cmd, pQueryInfo, condExpr.pTimewindow)) != TSDB_CODE_SUCCESS) { - return ret; + goto PARSE_WHERE_EXIT; } if ((ret = getQueryTimeRange(&pSql->cmd, pQueryInfo, &condExpr.pTimewindow)) != TSDB_CODE_SUCCESS) { - return ret; + goto PARSE_WHERE_EXIT; } // 3. get the tag query condition if ((ret = getTagQueryCondExpr(&pSql->cmd, pQueryInfo, &condExpr, pExpr)) != TSDB_CODE_SUCCESS) { - return ret; + goto PARSE_WHERE_EXIT; } // 4. get the table name query condition @@ -8613,12 +8613,15 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS } else if (pSqlExpr->tokenId == TK_SET) { int32_t colType = -1; STableMeta* pTableMeta = tscGetMetaInfo(pQueryInfo, pQueryInfo->curTableIdx)->pTableMeta; - size_t colSize = taosArrayGetSize(pCols); - if (pCols != NULL && colSize > 0) { - SColIndex* idx = taosArrayGet(pCols, colSize - 1); - SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, idx->colIndex); - if (pSchema != NULL) { - colType = pSchema->type; + if (pCols != NULL) { + size_t colSize = taosArrayGetSize(pCols); + + if (colSize > 0) { + SColIndex* idx = taosArrayGet(pCols, colSize - 1); + SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, idx->colIndex); + if (pSchema != NULL) { + colType = pSchema->type; + } } } tVariant *pVal; diff --git a/src/common/src/texpr.c b/src/common/src/texpr.c index 4ba2b35f3e..8414314301 100644 --- a/src/common/src/texpr.c +++ b/src/common/src/texpr.c @@ -513,7 +513,8 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t tVariant tmpVar = {0}; size_t t = 0; int32_t sz = tbufReadInt32(&br); - void *pvar = NULL; + void *pvar = NULL; + int64_t val = 0; int32_t bufLen = 0; if (IS_NUMERIC_TYPE(sType)) { bufLen = 60; // The maximum length of string that a number is converted to. @@ -528,21 +529,21 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_UTINYINT: case TSDB_DATA_TYPE_TINYINT: { - uint8_t val = (uint8_t)tbufReadInt64(&br); + *(uint8_t *)&val = (uint8_t)tbufReadInt64(&br); t = sizeof(val); pvar = &val; break; } case TSDB_DATA_TYPE_USMALLINT: case TSDB_DATA_TYPE_SMALLINT: { - uint16_t val = (uint16_t)tbufReadInt64(&br); + *(uint16_t *)&val = (uint16_t)tbufReadInt64(&br); t = sizeof(val); pvar = &val; break; } case TSDB_DATA_TYPE_UINT: case TSDB_DATA_TYPE_INT: { - uint32_t val = (uint32_t)tbufReadInt64(&br); + *(uint32_t *)&val = (uint32_t)tbufReadInt64(&br); t = sizeof(val); pvar = &val; break; @@ -550,31 +551,29 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_UBIGINT: case TSDB_DATA_TYPE_BIGINT: { - uint64_t val = (uint64_t)tbufReadInt64(&br); + *(uint64_t *)&val = (uint64_t)tbufReadInt64(&br); t = sizeof(val); pvar = &val; break; } case TSDB_DATA_TYPE_DOUBLE: { - double val = tbufReadDouble(&br); + *(double *)&val = tbufReadDouble(&br); t = sizeof(val); pvar = &val; break; } case TSDB_DATA_TYPE_FLOAT: { - float val = (float)tbufReadDouble(&br); + *(float *)&val = (float)tbufReadDouble(&br); t = sizeof(val); pvar = &val; break; } case TSDB_DATA_TYPE_BINARY: { - char *val = (char *)tbufReadBinary(&br, &t); - pvar = val; + pvar = (char *)tbufReadBinary(&br, &t); break; } case TSDB_DATA_TYPE_NCHAR: { - char *val = (char *)tbufReadBinary(&br, &t); - pvar = val; + pvar = (char *)tbufReadBinary(&br, &t); break; } default: @@ -594,7 +593,6 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_UTINYINT: case TSDB_DATA_TYPE_TINYINT: { - int8_t val = 0; if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } @@ -604,7 +602,6 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t } case TSDB_DATA_TYPE_USMALLINT: case TSDB_DATA_TYPE_SMALLINT: { - int16_t val = 0; if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } @@ -614,7 +611,6 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t } case TSDB_DATA_TYPE_UINT: case TSDB_DATA_TYPE_INT: { - int32_t val = 0; if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } @@ -625,7 +621,6 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_UBIGINT: case TSDB_DATA_TYPE_BIGINT: { - int64_t val = 0; if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } @@ -634,7 +629,6 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t break; } case TSDB_DATA_TYPE_DOUBLE: { - double val = 0; if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } @@ -643,7 +637,6 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t break; } case TSDB_DATA_TYPE_FLOAT: { - float val = 0; if (tVariantDump(&tmpVar, (char *)&val, tType, false)) { goto err_ret; } @@ -672,12 +665,15 @@ void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t } taosHashPut(pObj, (char *)pvar, t, &dummy, sizeof(dummy)); + tVariantDestroy(&tmpVar); + memset(&tmpVar, 0, sizeof(tmpVar)); } *q = (void *)pObj; pObj = NULL; err_ret: + tVariantDestroy(&tmpVar); taosHashCleanup(pObj); tfree(tmp); } diff --git a/src/query/inc/qFilter.h b/src/query/inc/qFilter.h index 4ed30853e8..d7edefd9cd 100644 --- a/src/query/inc/qFilter.h +++ b/src/query/inc/qFilter.h @@ -36,6 +36,7 @@ enum { FLD_TYPE_MAX = 3, FLD_DESC_NO_FREE = 4, FLD_DATA_NO_FREE = 8, + FLD_DATA_IS_HASH = 16, }; enum { @@ -64,6 +65,7 @@ enum { FI_STATUS_ALL = 1, FI_STATUS_EMPTY = 2, FI_STATUS_REWRITE = 4, + FI_STATUS_CLONED = 8, }; enum { @@ -187,7 +189,6 @@ typedef struct SFilterUnit { } SFilterUnit; typedef struct SFilterPCtx { - SHashObj *colHash; SHashObj *valHash; SHashObj *unitHash; } SFilterPCtx; @@ -232,7 +233,7 @@ typedef struct SFilterInfo { #define RESET_RANGE(ctx, r) do { (r)->next = (ctx)->rf; (ctx)->rf = r; } while (0) #define FREE_RANGE(ctx, r) do { if ((r)->prev) { (r)->prev->next = (r)->next; } else { (ctx)->rs = (r)->next;} if ((r)->next) { (r)->next->prev = (r)->prev; } RESET_RANGE(ctx, r); } while (0) -#define FREE_FROM_RANGE(ctx, r) do { if ((r)->prev) { (r)->prev->next = NULL; } else { (ctx)->rs = NULL;} while (r) {SFilterRangeNode *n = (r)->next; RESET_RANGE(ctx, r); r = n; } } while (0) +#define FREE_FROM_RANGE(ctx, r) do { SFilterRangeNode *_r = r; if ((_r)->prev) { (_r)->prev->next = NULL; } else { (ctx)->rs = NULL;} while (_r) {SFilterRangeNode *n = (_r)->next; RESET_RANGE(ctx, _r); _r = n; } } while (0) #define INSERT_RANGE(ctx, r, ra) do { SFilterRangeNode *n = filterNewRange(ctx, ra); n->prev = (r)->prev; if ((r)->prev) { (r)->prev->next = n; } else { (ctx)->rs = n; } (r)->prev = n; n->next = r; } while (0) #define APPEND_RANGE(ctx, r, ra) do { SFilterRangeNode *n = filterNewRange(ctx, ra); n->prev = (r); if (r) { (r)->next = n; } else { (ctx)->rs = n; } } while (0) diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 34896babe9..3e8f683008 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -2847,7 +2847,10 @@ int32_t loadDataBlockOnDemand(SQueryRuntimeEnv* pRuntimeEnv, STableScanInfo* pTa return terrno; } - doSetFilterColInfo(pQueryAttr->pFilters, pBlock); + if (pQueryAttr->pFilters != NULL) { + doSetFilterColInfo(pQueryAttr->pFilters, pBlock); + } + if (pQueryAttr->pFilters != NULL || pRuntimeEnv->pTsBuf != NULL) { filterColRowsInDataBlock(pRuntimeEnv, pBlock, ascQuery); } @@ -7575,6 +7578,8 @@ _cleanup_qinfo: tfree(pExprs); + filterFreeInfo(pFilters); + _cleanup: freeQInfo(pQInfo); return NULL; @@ -7930,6 +7935,8 @@ void freeQueryAttr(SQueryAttr* pQueryAttr) { taosArrayDestroy(pQueryAttr->pGroupbyExpr->columnInfo); tfree(pQueryAttr->pGroupbyExpr); } + + filterFreeInfo(pQueryAttr->pFilters); } } diff --git a/src/query/src/qFilter.c b/src/query/src/qFilter.c index 7ee19e88b3..0a7d7ac8e3 100644 --- a/src/query/src/qFilter.c +++ b/src/query/src/qFilter.c @@ -581,6 +581,13 @@ int32_t filterFreeRangeCtx(void* h) { r = rn; } + r = ctx->rf; + while (r) { + rn = r->next; + free(r); + r = rn; + } + free(ctx); return TSDB_CODE_SUCCESS; @@ -654,7 +661,7 @@ int32_t filterGetFiledByData(SFilterInfo *info, int32_t type, void *v, int32_t d } -int32_t filterAddField(SFilterInfo *info, void *desc, void *data, int32_t type, SFilterFieldId *fid, int32_t dataLen) { +int32_t filterAddField(SFilterInfo *info, void *desc, void **data, int32_t type, SFilterFieldId *fid, int32_t dataLen, bool freeIfExists) { int32_t idx = -1; uint16_t *num; @@ -663,8 +670,8 @@ int32_t filterAddField(SFilterInfo *info, void *desc, void *data, int32_t type, if (*num > 0) { if (type == FLD_TYPE_COLUMN) { idx = filterGetFiledByDesc(&info->fields[type], type, desc); - } else if (dataLen > 0 && FILTER_GET_FLAG(info->options, FI_OPTION_NEED_UNIQE)) { - idx = filterGetFiledByData(info, type, data, dataLen); + } else if (data && (*data) && dataLen > 0 && FILTER_GET_FLAG(info->options, FI_OPTION_NEED_UNIQE)) { + idx = filterGetFiledByData(info, type, *data, dataLen); } } @@ -677,15 +684,28 @@ int32_t filterAddField(SFilterInfo *info, void *desc, void *data, int32_t type, info->fields[type].fields[idx].flag = type; info->fields[type].fields[idx].desc = desc; - info->fields[type].fields[idx].data = data; + info->fields[type].fields[idx].data = data ? *data : NULL; + + if (type == FLD_TYPE_COLUMN) { + FILTER_SET_FLAG(info->fields[type].fields[idx].flag, FLD_DATA_NO_FREE); + } + ++(*num); - if (data && dataLen > 0 && FILTER_GET_FLAG(info->options, FI_OPTION_NEED_UNIQE)) { + if (data && (*data) && dataLen > 0 && FILTER_GET_FLAG(info->options, FI_OPTION_NEED_UNIQE)) { if (info->pctx.valHash == NULL) { info->pctx.valHash = taosHashInit(FILTER_DEFAULT_GROUP_SIZE * FILTER_DEFAULT_VALUE_SIZE, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false); } - taosHashPut(info->pctx.valHash, data, dataLen, &idx, sizeof(idx)); + taosHashPut(info->pctx.valHash, *data, dataLen, &idx, sizeof(idx)); + } + } else { + if (freeIfExists) { + tfree(desc); + } + + if (data && freeIfExists) { + tfree(*data); } } @@ -696,7 +716,7 @@ int32_t filterAddField(SFilterInfo *info, void *desc, void *data, int32_t type, } static FORCE_INLINE int32_t filterAddColFieldFromField(SFilterInfo *info, SFilterField *field, SFilterFieldId *fid) { - filterAddField(info, field->desc, field->data, FILTER_GET_TYPE(field->flag), fid, 0); + filterAddField(info, field->desc, &field->data, FILTER_GET_TYPE(field->flag), fid, 0, false); FILTER_SET_FLAG(field->flag, FLD_DESC_NO_FREE); FILTER_SET_FLAG(field->flag, FLD_DATA_NO_FREE); @@ -722,7 +742,7 @@ int32_t filterAddFieldFromNode(SFilterInfo *info, tExprNode *node, SFilterFieldI node->pVal = NULL; } - filterAddField(info, v, NULL, type, fid, 0); + filterAddField(info, v, NULL, type, fid, 0, true); return TSDB_CODE_SUCCESS; } @@ -829,7 +849,7 @@ int32_t filterAddGroupUnitFromNode(SFilterInfo *info, tExprNode* tree, SArray *g len = tDataTypes[type].bytes; } - filterAddField(info, NULL, fdata, FLD_TYPE_VALUE, &right, len); + filterAddField(info, NULL, &fdata, FLD_TYPE_VALUE, &right, len, true); filterAddUnit(info, TSDB_RELATION_EQUAL, &left, &right, &uidx); @@ -840,6 +860,8 @@ int32_t filterAddGroupUnitFromNode(SFilterInfo *info, tExprNode* tree, SArray *g p = taosHashIterate((SHashObj *)data, p); } + + taosHashCleanup(data); } else { filterAddFieldFromNode(info, tree->_node.pRight, &right); @@ -858,28 +880,34 @@ int32_t filterAddGroupUnitFromNode(SFilterInfo *info, tExprNode* tree, SArray *g int32_t filterAddUnitFromUnit(SFilterInfo *dst, SFilterInfo *src, SFilterUnit* u, uint16_t *uidx) { SFilterFieldId left, right, *pright = &right; int32_t type = FILTER_UNIT_DATA_TYPE(u); + uint16_t flag = FLD_DESC_NO_FREE; - filterAddField(dst, FILTER_UNIT_COL_DESC(src, u), NULL, FLD_TYPE_COLUMN, &left, 0); + filterAddField(dst, FILTER_UNIT_COL_DESC(src, u), NULL, FLD_TYPE_COLUMN, &left, 0, false); SFilterField *t = FILTER_UNIT_LEFT_FIELD(src, u); - FILTER_SET_FLAG(t->flag, FLD_DESC_NO_FREE); - + FILTER_SET_FLAG(t->flag, flag); + if (u->right.type == FLD_TYPE_VALUE) { void *data = FILTER_UNIT_VAL_DATA(src, u); if (IS_VAR_DATA_TYPE(type)) { if (FILTER_UNIT_OPTR(u) == TSDB_RELATION_IN) { - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, 0); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, 0, false); + + t = FILTER_GET_FIELD(dst, right); + + FILTER_SET_FLAG(t->flag, FLD_DATA_IS_HASH); } else { - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, varDataTLen(data)); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, varDataTLen(data), false); } } else { - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, false); } + + flag = FLD_DATA_NO_FREE; t = FILTER_UNIT_RIGHT_FIELD(src, u); - FILTER_SET_FLAG(t->flag, FLD_DATA_NO_FREE); + FILTER_SET_FLAG(t->flag, flag); } else { pright = NULL; } - return filterAddUnit(dst, FILTER_UNIT_OPTR(u), &left, pright, uidx); } @@ -926,7 +954,7 @@ int32_t filterAddGroupUnitFromCtx(SFilterInfo *dst, SFilterInfo *src, SFilterRan if (func(&ra->s, &ra->e) == 0) { void *data = malloc(sizeof(int64_t)); SIMPLE_COPY_VALUES(data, &ra->s); - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true); filterAddUnit(dst, TSDB_RELATION_EQUAL, &left, &right, &uidx); filterAddUnitToGroup(g, uidx); return TSDB_CODE_SUCCESS; @@ -936,7 +964,7 @@ int32_t filterAddGroupUnitFromCtx(SFilterInfo *dst, SFilterInfo *src, SFilterRan if (!FILTER_GET_FLAG(ra->sflag, RA_NULL)) { void *data = malloc(sizeof(int64_t)); SIMPLE_COPY_VALUES(data, &ra->s); - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true); filterAddUnit(dst, FILTER_GET_FLAG(ra->sflag, RA_EXCLUDE) ? TSDB_RELATION_GREATER : TSDB_RELATION_GREATER_EQUAL, &left, &right, &uidx); filterAddUnitToGroup(g, uidx); } @@ -944,7 +972,7 @@ int32_t filterAddGroupUnitFromCtx(SFilterInfo *dst, SFilterInfo *src, SFilterRan if (!FILTER_GET_FLAG(ra->eflag, RA_NULL)) { void *data = malloc(sizeof(int64_t)); SIMPLE_COPY_VALUES(data, &ra->e); - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true); filterAddUnit(dst, FILTER_GET_FLAG(ra->eflag, RA_EXCLUDE) ? TSDB_RELATION_LESS : TSDB_RELATION_LESS_EQUAL, &left, &right, &uidx); filterAddUnitToGroup(g, uidx); } @@ -990,7 +1018,7 @@ int32_t filterAddGroupUnitFromCtx(SFilterInfo *dst, SFilterInfo *src, SFilterRan if (func(&r->ra.s, &r->ra.e) == 0) { void *data = malloc(sizeof(int64_t)); SIMPLE_COPY_VALUES(data, &r->ra.s); - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true); filterAddUnit(dst, TSDB_RELATION_EQUAL, &left, &right, &uidx); filterAddUnitToGroup(g, uidx); @@ -1004,7 +1032,7 @@ int32_t filterAddGroupUnitFromCtx(SFilterInfo *dst, SFilterInfo *src, SFilterRan if (!FILTER_GET_FLAG(r->ra.sflag, RA_NULL)) { void *data = malloc(sizeof(int64_t)); SIMPLE_COPY_VALUES(data, &r->ra.s); - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true); filterAddUnit(dst, FILTER_GET_FLAG(r->ra.sflag, RA_EXCLUDE) ? TSDB_RELATION_GREATER : TSDB_RELATION_GREATER_EQUAL, &left, &right, &uidx); filterAddUnitToGroup(g, uidx); } @@ -1012,7 +1040,7 @@ int32_t filterAddGroupUnitFromCtx(SFilterInfo *dst, SFilterInfo *src, SFilterRan if (!FILTER_GET_FLAG(r->ra.eflag, RA_NULL)) { void *data = malloc(sizeof(int64_t)); SIMPLE_COPY_VALUES(data, &r->ra.e); - filterAddField(dst, NULL, data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes); + filterAddField(dst, NULL, &data, FLD_TYPE_VALUE, &right, tDataTypes[type].bytes, true); filterAddUnit(dst, FILTER_GET_FLAG(r->ra.eflag, RA_EXCLUDE) ? TSDB_RELATION_LESS : TSDB_RELATION_LESS_EQUAL, &left, &right, &uidx); filterAddUnitToGroup(g, uidx); } @@ -1031,11 +1059,13 @@ int32_t filterAddGroupUnitFromCtx(SFilterInfo *dst, SFilterInfo *src, SFilterRan static void filterFreeGroup(void *pItem) { - SFilterGroup* p = (SFilterGroup*) pItem; - if (p) { - tfree(p->unitIdxs); - tfree(p->unitFlags); + if (pItem == NULL) { + return; } + + SFilterGroup* p = (SFilterGroup*) pItem; + tfree(p->unitIdxs); + tfree(p->unitFlags); } @@ -1113,10 +1143,10 @@ void filterDumpInfoToString(SFilterInfo *info, const char *msg, int32_t options) if (var->nType == TSDB_DATA_TYPE_VALUE_ARRAY) { qDebug("VAL%d => [type:TS][val:[%" PRIi64"] - [%" PRId64 "]]", i, *(int64_t *)field->data, *(((int64_t *)field->data) + 1)); } else { - qDebug("VAL%d => [type:%d][val:%" PRIi64"]", i, var->nType, var->i64); //TODO + qDebug("VAL%d => [type:%d][val:%" PRIx64"]", i, var->nType, var->i64); //TODO } - } else { - qDebug("VAL%d => [type:NIL][val:0x%" PRIx64"]", i, *(int64_t *)field->data); //TODO + } else if (field->data) { + qDebug("VAL%d => [type:NIL][val:NIL]", i); //TODO } } @@ -1248,10 +1278,67 @@ void filterFreeGroupCtx(SFilterGroupCtx* gRes) { tfree(gRes); } +void filterFreeField(SFilterField* field, int32_t type) { + if (field == NULL) { + return; + } + + if (!FILTER_GET_FLAG(field->flag, FLD_DESC_NO_FREE)) { + if (type == FLD_TYPE_VALUE) { + tVariantDestroy(field->desc); + } + + tfree(field->desc); + } + + if (!FILTER_GET_FLAG(field->flag, FLD_DATA_NO_FREE)) { + if (FILTER_GET_FLAG(field->flag, FLD_DATA_IS_HASH)) { + taosHashCleanup(field->data); + } else { + tfree(field->data); + } + } +} + +void filterFreePCtx(SFilterPCtx *pctx) { + taosHashCleanup(pctx->valHash); + taosHashCleanup(pctx->unitHash); +} + void filterFreeInfo(SFilterInfo *info) { CHK_RETV(info == NULL); - //TODO + for (int32_t i = 0; i < FLD_TYPE_MAX; ++i) { + for (uint16_t f = 0; f < info->fields[i].num; ++f) { + filterFreeField(&info->fields[i].fields[f], i); + } + + tfree(info->fields[i].fields); + } + + for (int32_t i = 0; i < info->groupNum; ++i) { + filterFreeGroup(&info->groups[i]); + } + + tfree(info->groups); + + tfree(info->units); + + tfree(info->unitRes); + + tfree(info->unitFlags); + + for (uint16_t i = 0; i < info->colRangeNum; ++i) { + filterFreeRangeCtx(info->colRange[i]); + } + + tfree(info->colRange); + + filterFreePCtx(&info->pctx); + + if (!FILTER_GET_FLAG(info->status, FI_STATUS_CLONED)) { + tfree(info); + } } @@ -1281,14 +1368,16 @@ int32_t filterInitValFieldData(SFilterInfo *info) { if (unit->compare.optr == TSDB_RELATION_IN) { convertFilterSetFromBinary((void **)&fi->data, var->pz, var->nLen, type); CHK_LRET(fi->data == NULL, TSDB_CODE_QRY_APP_ERROR, "failed to convert in param"); - + + FILTER_SET_FLAG(fi->flag, FLD_DATA_IS_HASH); + continue; } if (type == TSDB_DATA_TYPE_BINARY) { - fi->data = calloc(1, (var->nLen + 1) * TSDB_NCHAR_SIZE); + fi->data = calloc(1, var->nLen + VARSTR_HEADER_SIZE); } else if (type == TSDB_DATA_TYPE_NCHAR) { - fi->data = calloc(1, (var->nLen + 1) * TSDB_NCHAR_SIZE); + fi->data = calloc(1, (var->nLen + VARSTR_HEADER_SIZE) * TSDB_NCHAR_SIZE); } else { if (var->nType == TSDB_DATA_TYPE_VALUE_ARRAY) { //TIME RANGE fi->data = calloc(var->nLen, tDataTypes[type].bytes); @@ -1488,7 +1577,9 @@ int32_t filterMergeGroupUnits(SFilterInfo *info, SFilterGroupCtx** gRes, int32_t colIdx[colIdxi++] = cidx; ++gRes[gResIdx]->colNum; } else { - FILTER_SET_FLAG(info->status, FI_STATUS_REWRITE); + if (!FILTER_NO_MERGE_DATA_TYPE(FILTER_UNIT_DATA_TYPE(u))) { + FILTER_SET_FLAG(info->status, FI_STATUS_REWRITE); + } } FILTER_PUSH_UNIT(gRes[gResIdx]->colInfo[cidx], u); @@ -1701,8 +1792,12 @@ int32_t filterMergeTwoGroups(SFilterInfo *info, SFilterGroupCtx** gRes1, SFilter _err_return: - if (colCtxs && taosArrayGetSize(colCtxs) > 0) { - taosArrayDestroyEx(colCtxs, filterFreeColCtx); + if (colCtxs) { + if (taosArrayGetSize(colCtxs) > 0) { + taosArrayDestroyEx(colCtxs, filterFreeColCtx); + } else { + taosArrayDestroy(colCtxs); + } } filterFreeRangeCtx(ctx); @@ -1823,6 +1918,8 @@ int32_t filterRewrite(SFilterInfo *info, SFilterGroupCtx** gRes, int32_t gResNum } SFilterInfo oinfo = *info; + + FILTER_SET_FLAG(oinfo.status, FI_STATUS_CLONED); SArray* group = taosArrayInit(FILTER_DEFAULT_GROUP_SIZE, sizeof(SFilterGroup)); SFilterGroupCtx *res = NULL; @@ -1831,6 +1928,7 @@ int32_t filterRewrite(SFilterInfo *info, SFilterGroupCtx** gRes, int32_t gResNum uint16_t uidx = 0; memset(info, 0, sizeof(*info)); + info->colRangeNum = oinfo.colRangeNum; info->colRange = oinfo.colRange; oinfo.colRangeNum = 0; @@ -1877,6 +1975,8 @@ int32_t filterRewrite(SFilterInfo *info, SFilterGroupCtx** gRes, int32_t gResNum taosArrayDestroy(group); + filterFreeInfo(&oinfo); + return TSDB_CODE_SUCCESS; } @@ -1961,6 +2061,7 @@ int32_t filterGenerateColRange(SFilterInfo *info, SFilterGroupCtx** gRes, int32_ } _err_return: + tfree(idxNum); tfree(idxs); return TSDB_CODE_SUCCESS; @@ -1990,12 +2091,13 @@ int32_t filterPreprocess(SFilterInfo *info) { if (FILTER_GET_FLAG(info->status, FI_STATUS_ALL)) { qInfo("Final - FilterInfo: [ALL]"); - return TSDB_CODE_SUCCESS; + goto _return; } + if (FILTER_GET_FLAG(info->status, FI_STATUS_EMPTY)) { qInfo("Final - FilterInfo: [EMPTY]"); - return TSDB_CODE_SUCCESS; + goto _return; } filterGenerateColRange(info, gRes, gResNum); @@ -2005,6 +2107,14 @@ int32_t filterPreprocess(SFilterInfo *info) { filterPostProcessRange(info); filterRewrite(info, gRes, gResNum); + +_return: + + for (int32_t i = 0; i < gResNum; ++i) { + filterFreeGroupCtx(gRes[i]); + } + + tfree(gRes); return TSDB_CODE_SUCCESS; } diff --git a/src/query/src/qSqlParser.c b/src/query/src/qSqlParser.c index 919ecdade8..bb5efdb123 100644 --- a/src/query/src/qSqlParser.c +++ b/src/query/src/qSqlParser.c @@ -459,9 +459,7 @@ static void doDestroySqlExprNode(tSqlExpr *pExpr) { return; } - if (pExpr->tokenId == TK_STRING) { - tVariantDestroy(&pExpr->value); - } + tVariantDestroy(&pExpr->value); tSqlExprListDestroy(pExpr->Expr.paramList); free(pExpr); diff --git a/src/query/src/queryMain.c b/src/query/src/queryMain.c index dd20716388..0bdf4d2ead 100644 --- a/src/query/src/queryMain.c +++ b/src/query/src/queryMain.c @@ -175,6 +175,7 @@ int32_t qCreateQueryInfo(void* tsdb, int32_t vgId, SQueryTableMsg* pQueryMsg, qi param.pSecExprs = NULL; param.pGroupbyExpr = NULL; param.pTagColumnInfo = NULL; + param.pFilters = NULL; if ((*pQInfo) == NULL) { code = TSDB_CODE_QRY_OUT_OF_MEMORY; @@ -200,6 +201,8 @@ int32_t qCreateQueryInfo(void* tsdb, int32_t vgId, SQueryTableMsg* pQueryMsg, qi freeColumnFilterInfo(column->flist.filterInfo, column->flist.numOfFilters); } + filterFreeInfo(param.pFilters); + //pQInfo already freed in initQInfo, but *pQInfo may not pointer to null; if (code != TSDB_CODE_SUCCESS) { *pQInfo = NULL; diff --git a/src/util/src/hash.c b/src/util/src/hash.c index c8bd79f118..c8e04a37b1 100644 --- a/src/util/src/hash.c +++ b/src/util/src/hash.c @@ -713,7 +713,8 @@ size_t taosHashGetMemSize(const SHashObj *pHashObj) { } FORCE_INLINE void *taosHashGetDataKey(SHashObj *pHashObj, void *data) { - return GET_HASH_NODE_KEY(GET_HASH_PNODE(data)); + SHashNode * node = GET_HASH_PNODE(data); + return GET_HASH_NODE_KEY(node); } FORCE_INLINE uint32_t taosHashGetDataKeyLen(SHashObj *pHashObj, void *data) {