diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index fbe9172329c847ee9558cdd78c94ee21cc0f8b1e..548a280d9e5c565d015e5cf19ef9e5f0fcd284fc 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2820,6 +2820,32 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col pExpr = tscExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pCmd), interResult, false); tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_DOUBLE, sizeof(double)); + } else if (functionId == TSDB_FUNC_MAVG || functionId == TSDB_FUNC_SAMPLE) { + tVariantDump(pVariant, val, TSDB_DATA_TYPE_BIGINT, true); + + int64_t numRowsSelected = GET_INT32_VAL(val); + if (numRowsSelected <= 0 || numRowsSelected > 1000) { // todo use macro + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg12); + } + + // todo REFACTOR + // set the first column ts for top/bottom query + SColumnIndex index1 = {index.tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX}; + pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &index1, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, getNewResColId(pCmd), + 0, false); + tstrncpy(pExpr->base.aliasName, aAggs[TSDB_FUNC_TS_DUMMY].name, sizeof(pExpr->base.aliasName)); + + const int32_t TS_COLUMN_INDEX = PRIMARYKEY_TIMESTAMP_COL_INDEX; + SColumnList ids = createColumnList(1, index.tableIndex, TS_COLUMN_INDEX); + insertResultField(pQueryInfo, colIndex, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, + aAggs[TSDB_FUNC_TS_DUMMY].name, pExpr); + + colIndex += 1; // the first column is ts + + getResultDataInfo(pSchema->type, pSchema->bytes, functionId, numRowsSelected, &resultType, &resultSize, &interResult, 0, false, + pUdfInfo); + pExpr = tscExprAppend(pQueryInfo, functionId, &index, resultType, resultSize, getNewResColId(pCmd), interResult, false); + tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, sizeof(int64_t)); } else { tVariantDump(pVariant, val, TSDB_DATA_TYPE_BIGINT, true); @@ -2829,7 +2855,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } // todo REFACTOR - // set the first column ts for top/bottom/mavg/sample query + // set the first column ts for top/bottom query SColumnIndex index1 = {index.tableIndex, PRIMARYKEY_TIMESTAMP_COL_INDEX}; pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS, &index1, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, getNewResColId(pCmd), 0, false); diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index 886803d51adcb58a95ab205230c7c74d7c261830..3e01f7ed5abbb3f5b138db1f3cd46f55d85c6fa7 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -4678,7 +4678,7 @@ static void assignResultSample(SSampleFuncInfo *pInfo, int32_t index, int64_t ts tVariantCreateFromBinary(pInfo->values + index, pData, bytes, type); } - *(pInfo->timeStamps + pInfo->numSampled) = ts; + *(pInfo->timeStamps + index) = ts; return; } @@ -4691,7 +4691,6 @@ static void do_reservoir_sample(SSampleFuncInfo *pInfo, int32_t samplesK, int64_ int32_t j = rand() % (pInfo->totalPoints); if (j < samplesK) { assignResultSample(pInfo, j, ts, pData, bytes, type); - *(pInfo->timeStamps + j) = ts; } } }