diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 2ff8fa4707c3b4062d415db37e61b693566032a4..96c944243ef9d9179886c80498cb28ebf18e2088 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2592,7 +2592,8 @@ static int32_t setExprInfoForFunctions(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SS return TSDB_CODE_SUCCESS; } -void setResultColName(char* name, tSqlExprItem* pItem, int32_t functionId, SStrToken* pToken, bool multiCols) { +void setResultColName(char* name, bool finalResult, tSqlExprItem* pItem, int32_t functionId, SStrToken* pToken, + bool multiCols) { if (pItem->aliasName != NULL) { tstrncpy(name, pItem->aliasName, TSDB_COL_NAME_LEN); } else { @@ -2600,7 +2601,7 @@ void setResultColName(char* name, tSqlExprItem* pItem, int32_t functionId, SStrT int32_t len = MIN(pToken->n + 1, TSDB_COL_NAME_LEN); tstrncpy(uname, pToken->z, len); - if (tsKeepOriginalColumnName) { // keep the original column name + if (finalResult && tsKeepOriginalColumnName) { // keep the original column name tstrncpy(name, uname, TSDB_COL_NAME_LEN); } else if (multiCols) { if (!TSDB_FUNC_IS_SCALAR(functionId)) { @@ -2617,7 +2618,7 @@ void setResultColName(char* name, tSqlExprItem* pItem, int32_t functionId, SStrT tstrncpy(name, tmp, TSDB_COL_NAME_LEN); } - } else { // use the user-input result column name + } else { // use the user-input result column name len = MIN(pItem->pNode->exprToken.n + 1, TSDB_COL_NAME_LEN); tstrncpy(name, pItem->pNode->exprToken.z, len); } @@ -2977,7 +2978,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col for (int32_t j = 0; j < tscGetNumOfColumns(pTableMetaInfo->pTableMeta); ++j) { index.columnIndex = j; SStrToken t = {.z = pSchema[j].name, .n = (uint32_t)strnlen(pSchema[j].name, TSDB_COL_NAME_LEN)}; - setResultColName(name, pItem, cvtFunc.originFuncId, &t, true); + setResultColName(name, finalResult, pItem, cvtFunc.originFuncId, &t, true); if (setExprInfoForFunctions(pCmd, pQueryInfo, &pSchema[j], cvtFunc, name, colIndex++, &index, finalResult, pUdfInfo) != 0) { @@ -3005,7 +3006,8 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex); bool multiColOutput = taosArrayGetSize(pItem->pNode->Expr.paramList) > 1; - setResultColName(name, pItem, cvtFunc.originFuncId, &pParamElem->pNode->columnName, multiColOutput); + setResultColName(name, finalResult, pItem, cvtFunc.originFuncId, &pParamElem->pNode->columnName, + multiColOutput); if (setExprInfoForFunctions(pCmd, pQueryInfo, pSchema, cvtFunc, name, colIndex++, &index, finalResult, pUdfInfo) != 0) { return TSDB_CODE_TSC_INVALID_OPERATION; @@ -3030,7 +3032,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col char name[TSDB_COL_NAME_LEN] = {0}; SStrToken t = {.z = pSchema[i].name, .n = (uint32_t)strnlen(pSchema[i].name, TSDB_COL_NAME_LEN)}; - setResultColName(name, pItem, cvtFunc.originFuncId, &t, true); + setResultColName(name, finalResult, pItem, cvtFunc.originFuncId, &t, true); if (setExprInfoForFunctions(pCmd, pQueryInfo, &pSchema[index.columnIndex], cvtFunc, name, colIndex, &index, finalResult, pUdfInfo) != 0) { @@ -10462,17 +10464,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS *pExpr = calloc(1, sizeof(tExprNode)); (*pExpr)->nodeType = TSQL_NODE_COL; (*pExpr)->pSchema = calloc(1, sizeof(SSchema)); - if (tsKeepOriginalColumnName && // TD-14196, tsKeepOriginalColumnName params makes logic special - (pSqlExpr->functionId == TSDB_FUNC_FIRST || - pSqlExpr->functionId == TSDB_FUNC_LAST || - pSqlExpr->functionId == TSDB_FUNC_SPREAD || - pSqlExpr->functionId == TSDB_FUNC_LAST_ROW || - pSqlExpr->functionId == TSDB_FUNC_INTERP)) { - tSqlExprItem* pParamElem = taosArrayGet(pSqlExpr->Expr.paramList, 0); - strncpy((*pExpr)->pSchema->name, pParamElem->pNode->columnName.z, pParamElem->pNode->columnName.n); - }else{ - strncpy((*pExpr)->pSchema->name, pSqlExpr->exprToken.z, pSqlExpr->exprToken.n); - } + strncpy((*pExpr)->pSchema->name, pSqlExpr->exprToken.z, pSqlExpr->exprToken.n); // set the input column data byte and type. size_t size = taosArrayGetSize(pQueryInfo->exprList); diff --git a/tests/pytest/functions/function_first.py b/tests/pytest/functions/function_first.py index f1a916b168271e67d0001c5d4444966f8c07a2d1..c1f7692dfdfd4dbd29f2873aa607dcb4a7dc1c8b 100644 --- a/tests/pytest/functions/function_first.py +++ b/tests/pytest/functions/function_first.py @@ -142,6 +142,29 @@ class TDTestCase: # TD-2607 first,last + where none exist condition + interval tdSql.query("select first(*),last(*) from test1 where ts < 23 interval(1s)") tdSql.checkRows(0) + + # TS-1601 + tdSql.execute("create database test") + tdSql.execute("use test") + tdSql.execute("create table tb01(ts timestamp, c1 double, c2 int)") + tdSql.execute("insert into tb01 values(now, 2.3987401, 20)(now + 2s, 4.58123, 11)") + + r = os.popen("taos -s 'select first(c1) + last(c1) from test.tb01'") + text = r.read() + r.close() + result = float(text.split("|")[1].split("\n")[2]) + + tdSql.query("select first(c1) + last(c1) from tb01") + tdSql.checkData(0, 0, result) + + r = os.popen("taos -s 'select first(c1) - last(c1) from test.tb01'") + text = r.read() + r.close() + result = float(text.split("|")[1].split("\n")[2]) + tdSql.query("select first(c1) - last(c1) from tb01") + tdSql.checkData(0, 0, result) + + def stop(self): tdSql.close() diff --git a/tests/pytest/functions/function_last.py b/tests/pytest/functions/function_last.py index 991ac96a800803440a2e662c163622af95c556e3..1b89e2f5e4df83c338221b92cc35a367a7358335 100644 --- a/tests/pytest/functions/function_last.py +++ b/tests/pytest/functions/function_last.py @@ -127,6 +127,10 @@ class TDTestCase: tdSql.checkRows(1) tdSql.checkData(0, 0, 9.1) + tdSql.query("select last(col6)/10 from test1") + tdSql.checkRows(1) + tdSql.checkData(0, 0, 0.91) + tdSql.query("select last(col7) from test1") tdSql.checkRows(1) tdSql.checkData(0, 0, True)