From d48ba502176984c0d56e56ca861a9e03e0d3c522 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 23 Apr 2021 17:55:52 +0800 Subject: [PATCH] [td-225] fix bug found by regression test. --- src/client/inc/tsclient.h | 41 -------------------------- src/client/src/tscSQLParser.c | 19 ++++++++---- src/client/src/tscSubquery.c | 12 ++++++-- src/query/src/qSqlParser.c | 2 +- tests/script/general/parser/having.sim | 2 -- 5 files changed, 23 insertions(+), 53 deletions(-) diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 7fa2242d8e..74db411226 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -496,47 +496,6 @@ int32_t tscSQLSyntaxErrMsg(char* msg, const char* additionalInfo, const char* s int32_t tscToSQLCmd(SSqlObj *pSql, struct SSqlInfo *pInfo); -static FORCE_INLINE void tscGetResultColumnChr(SSqlRes* pRes, SFieldInfo* pFieldInfo, int32_t columnIndex, int32_t offset) { - SInternalField* pInfo = (SInternalField*) TARRAY_GET_ELEM(pFieldInfo->internalField, columnIndex); - - int32_t type = pInfo->field.type; - int32_t bytes = pInfo->field.bytes; - - char* pData = pRes->data + (int32_t)(offset * pRes->numOfRows + bytes * pRes->row); - UNUSED(pData); - -// user defined constant value output columns - if (pInfo->pExpr != NULL && TSDB_COL_IS_UD_COL(pInfo->pExpr->base.colInfo.flag)) { - if (type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_BINARY) { - pData = pInfo->pExpr->base.param[1].pz; - pRes->length[columnIndex] = pInfo->pExpr->base.param[1].nLen; - pRes->tsrow[columnIndex] = (pInfo->pExpr->base.param[1].nType == TSDB_DATA_TYPE_NULL) ? NULL : (unsigned char*)pData; - } else { - assert(bytes == tDataTypes[type].bytes); - - pRes->tsrow[columnIndex] = isNull(pData, type) ? NULL : (unsigned char*)&pInfo->pExpr->base.param[1].i64; - pRes->length[columnIndex] = bytes; - } - } else { - if (type == TSDB_DATA_TYPE_NCHAR || type == TSDB_DATA_TYPE_BINARY) { - int32_t realLen = varDataLen(pData); - assert(realLen <= bytes - VARSTR_HEADER_SIZE); - - pRes->tsrow[columnIndex] = (isNull(pData, type)) ? NULL : (unsigned char*)((tstr *)pData)->data; - if (realLen < pInfo->pExpr->base.resBytes - VARSTR_HEADER_SIZE) { // todo refactor - *(pData + realLen + VARSTR_HEADER_SIZE) = 0; - } - - pRes->length[columnIndex] = realLen; - } else { - assert(bytes == tDataTypes[type].bytes); - - pRes->tsrow[columnIndex] = isNull(pData, type) ? NULL : (unsigned char*)pData; - pRes->length[columnIndex] = bytes; - } - } -} - extern int32_t sentinel; extern SHashObj *tscVgroupMap; extern SHashObj *tscTableMetaInfo; diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 3b75b8f636..421d99c892 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -6801,12 +6801,16 @@ int32_t tscGetExprFilters(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelect tSqlExprItem* pItem = taosArrayGet(pSelectNodeList, i); if (tSqlExprCompare(pItem->pNode, pSqlExpr) == 0) { // exists, not added it, - int32_t functionId = isValidFunction(pSqlExpr->operand.z, pSqlExpr->operand.n); - tSqlExprItem* pParamElem = taosArrayGet(pSqlExpr->pParam, 0); - SStrToken* pToken = &pParamElem->pNode->colInfo; - SColumnIndex index = COLUMN_INDEX_INITIALIZER; - getColumnIndexByName(pCmd, pToken, pQueryInfo, &index); + int32_t functionId = pSqlExpr->functionId; + if (pSqlExpr->pParam == NULL) { + index.columnIndex = 0; + index.tableIndex = 0; + } else { + tSqlExprItem* pParamElem = taosArrayGet(pSqlExpr->pParam, 0); + SStrToken* pToken = &pParamElem->pNode->colInfo; + getColumnIndexByName(pCmd, pToken, pQueryInfo, &index); + } size_t numOfNodeInSel = tscSqlExprNumOfExprs(pQueryInfo); for(int32_t k = 0; k < numOfNodeInSel; ++k) { @@ -6843,6 +6847,10 @@ int32_t tscGetExprFilters(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelect size_t n = tscSqlExprNumOfExprs(pQueryInfo); *pExpr = tscSqlExprGet(pQueryInfo, (int32_t)n - 1); + + SInternalField* pField = taosArrayGet(pQueryInfo->fieldsInfo.internalField, n - 1); + pField->visible = false; + return TSDB_CODE_SUCCESS; } @@ -6920,7 +6928,6 @@ static int32_t handleExprInHavingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, S } return TSDB_CODE_SUCCESS; -// return genExprFilter(pInfo->pFieldFilters); } int32_t getHavingExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SArray* pSelectNodeList, tSqlExpr* pExpr, int32_t parentOptr) { diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index ebfaa5b961..a6fe3fa077 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -3403,20 +3403,26 @@ TAOS_ROW doSetResultRowData(SSqlObj *pSql) { SQueryInfo *pQueryInfo = tscGetQueryInfo(pCmd, pCmd->clauseIndex); size_t size = tscNumOfFields(pQueryInfo); + + int32_t j = 0; for (int i = 0; i < size; ++i) { SInternalField* pInfo = (SInternalField*)TARRAY_GET_ELEM(pQueryInfo->fieldsInfo.internalField, i); + if (!pInfo->visible) { + continue; + } int32_t type = pInfo->field.type; int32_t bytes = pInfo->field.bytes; if (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR) { - pRes->tsrow[i] = isNull(pRes->urow[i], type) ? NULL : pRes->urow[i]; + pRes->tsrow[j] = isNull(pRes->urow[i], type) ? NULL : pRes->urow[i]; } else { - pRes->tsrow[i] = isNull(pRes->urow[i], type) ? NULL : varDataVal(pRes->urow[i]); - pRes->length[i] = varDataLen(pRes->urow[i]); + pRes->tsrow[j] = isNull(pRes->urow[i], type) ? NULL : varDataVal(pRes->urow[i]); + pRes->length[j] = varDataLen(pRes->urow[i]); } ((char**) pRes->urow)[i] += bytes; + j += 1; } pRes->row++; // index increase one-step diff --git a/src/query/src/qSqlParser.c b/src/query/src/qSqlParser.c index aa9921d93e..83e1ff6bf8 100644 --- a/src/query/src/qSqlParser.c +++ b/src/query/src/qSqlParser.c @@ -358,7 +358,7 @@ int32_t tSqlExprCompare(tSqlExpr *left, tSqlExpr *right) { for (int32_t i = 0; i < size; i++) { tSqlExprItem* pLeftElem = taosArrayGet(left->pParam, i); tSqlExpr* pSubLeft = pLeftElem->pNode; - tSqlExprItem* pRightElem = taosArrayGet(left->pParam, i); + tSqlExprItem* pRightElem = taosArrayGet(right->pParam, i); tSqlExpr* pSubRight = pRightElem->pNode; if (tSqlExprCompare(pSubLeft, pSubRight)) { diff --git a/tests/script/general/parser/having.sim b/tests/script/general/parser/having.sim index 2e95664d31..ddafdd7329 100644 --- a/tests/script/general/parser/having.sim +++ b/tests/script/general/parser/having.sim @@ -141,9 +141,7 @@ if $data30 != 4 then endi sql_error select top(f1,2) from st2 group by f1 having count(f2) > 0; - sql_error select top(f1,2) from st2 group by f1 having count(f2) > 0; - sql_error select top(f1,2) from st2 group by f1 having avg(f1) > 0; sql select avg(f1),count(f1) from st2 group by f1 having avg(f1) > 2; -- GitLab