diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index cd9853df03bab1a953ee8fd79e66a52e84ba157c..1611d3dbf950ef308069b7ea05651029e8004c73 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -107,14 +107,10 @@ int tsParseTime(SStrToken *pToken, int64_t *time, char **next, char *error, int1 return tscInvalidOperationMsg(error, "value expected in timestamp", sToken.z); } - if (parseAbsoluteDuration(valueToken.z, valueToken.n, &interval) != TSDB_CODE_SUCCESS) { + if (parseAbsoluteDuration(valueToken.z, valueToken.n, &interval, timePrec) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } - if (timePrec == TSDB_TIME_PRECISION_MILLI) { - interval /= 1000; - } - if (sToken.type == TK_PLUS) { useconds += interval; } else { diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index baa15382977dc90cac19088f531d559da255411b..20a85a17b7ef6f8f8000ea138cf0b2ffd7fa969a 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -832,18 +832,15 @@ int32_t validateIntervalNode(SSqlObj* pSql, SQueryInfo* pQueryInfo, SSqlNode* pS // interval is not null SStrToken *t = &pSqlNode->interval.interval; - if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.interval, &pQueryInfo->interval.intervalUnit) != TSDB_CODE_SUCCESS) { + if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.interval, + &pQueryInfo->interval.intervalUnit, tinfo.precision) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } if (pQueryInfo->interval.intervalUnit != 'n' && pQueryInfo->interval.intervalUnit != 'y') { - // if the unit of time window value is millisecond, change the value from microsecond - if (tinfo.precision == TSDB_TIME_PRECISION_MILLI) { - pQueryInfo->interval.interval = pQueryInfo->interval.interval / 1000; - } // interval cannot be less than 10 milliseconds - if (pQueryInfo->interval.interval < tsMinIntervalTime) { + if (convertTimePrecision(pQueryInfo->interval.interval, tinfo.precision, TSDB_TIME_PRECISION_MILLI) < tsMinIntervalTime) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } } @@ -919,6 +916,8 @@ int32_t validateSessionNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode * pS const char* msg3 = "invalid column name"; const char* msg4 = "invalid time window"; + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); // no session window if (!TPARSER_HAS_TOKEN(pSqlNode->sessionVal.gap)) { return TSDB_CODE_SUCCESS; @@ -928,7 +927,7 @@ int32_t validateSessionNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode * pS SStrToken* gap = &pSqlNode->sessionVal.gap; char timeUnit = 0; - if (parseNatualDuration(gap->z, gap->n, &pQueryInfo->sessionWindow.gap, &timeUnit) != TSDB_CODE_SUCCESS) { + if (parseNatualDuration(gap->z, gap->n, &pQueryInfo->sessionWindow.gap, &timeUnit, tinfo.precision) != TSDB_CODE_SUCCESS) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg4); } @@ -936,13 +935,6 @@ int32_t validateSessionNode(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SSqlNode * pS return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } - // if the unit of time window value is millisecond, change the value from microsecond - STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - if (tinfo.precision == TSDB_TIME_PRECISION_MILLI) { - pQueryInfo->sessionWindow.gap = pQueryInfo->sessionWindow.gap / 1000; - } - if (pQueryInfo->sessionWindow.gap != 0 && pQueryInfo->interval.interval != 0) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -979,7 +971,8 @@ int32_t parseIntervalOffset(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* of return TSDB_CODE_SUCCESS; } - if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.offset, &pQueryInfo->interval.offsetUnit) != TSDB_CODE_SUCCESS) { + if (parseNatualDuration(t->z, t->n, &pQueryInfo->interval.offset, + &pQueryInfo->interval.offsetUnit, tinfo.precision) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } @@ -988,10 +981,6 @@ int32_t parseIntervalOffset(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* of } if (pQueryInfo->interval.offsetUnit != 'n' && pQueryInfo->interval.offsetUnit != 'y') { - // if the unit of time window value is millisecond, change the value from microsecond - if (tinfo.precision == TSDB_TIME_PRECISION_MILLI) { - pQueryInfo->interval.offset = pQueryInfo->interval.offset / 1000; - } if (pQueryInfo->interval.intervalUnit != 'n' && pQueryInfo->interval.intervalUnit != 'y') { if (pQueryInfo->interval.offset >= pQueryInfo->interval.interval) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); @@ -1036,12 +1025,10 @@ int32_t parseSlidingClause(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SStrToken* pSl return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } - parseAbsoluteDuration(pSliding->z, pSliding->n, &pQueryInfo->interval.sliding); - if (tinfo.precision == TSDB_TIME_PRECISION_MILLI) { - pQueryInfo->interval.sliding /= 1000; - } + parseAbsoluteDuration(pSliding->z, pSliding->n, &pQueryInfo->interval.sliding, tinfo.precision); - if (pQueryInfo->interval.sliding < tsMinSlidingTime) { + if (pQueryInfo->interval.sliding < + convertTimePrecision(tsMinSlidingTime, TSDB_TIME_PRECISION_MILLI, tinfo.precision)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg0); } @@ -3189,8 +3176,9 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, return retVal; } } else if ((colType == TSDB_DATA_TYPE_TIMESTAMP) && (TSDB_DATA_TYPE_BIGINT == pRight->value.nType)) { - if ((timePrecision == TSDB_TIME_PRECISION_MILLI) && (pRight->flags & (1 << EXPR_FLAG_US_TIMESTAMP))) { - pRight->value.i64 /= 1000; + if (pRight->flags & (1 << EXPR_FLAG_NS_TIMESTAMP)) { + pRight->value.i64 = + convertTimePrecision(pRight->value.i64, TSDB_TIME_PRECISION_NANO, timePrecision); } } @@ -4707,7 +4695,7 @@ int32_t getTimeRange(STimeWindow* win, tSqlExpr* pRight, int32_t optr, int16_t t char* seg = strnchr(pRight->value.pz, '-', pRight->value.nLen, false); if (seg != NULL) { - if (taosParseTime(pRight->value.pz, &val, pRight->value.nLen, TSDB_TIME_PRECISION_MICRO, tsDaylight) == TSDB_CODE_SUCCESS) { + if (taosParseTime(pRight->value.pz, &val, pRight->value.nLen, timePrecision, tsDaylight) == TSDB_CODE_SUCCESS) { parsed = true; } else { return TSDB_CODE_TSC_INVALID_OPERATION; @@ -4720,18 +4708,6 @@ int32_t getTimeRange(STimeWindow* win, tSqlExpr* pRight, int32_t optr, int16_t t return TSDB_CODE_TSC_INVALID_OPERATION; } } - } else if (pRight->tokenId == TK_INTEGER && timePrecision == TSDB_TIME_PRECISION_MILLI) { - /* - * if the pRight->tokenId == TK_INTEGER/TK_FLOAT, the value is adaptive, we - * need the time precision in metermeta to transfer the value in MICROSECOND - * - * Additional check to avoid data overflow - */ - if (pRight->value.i64 <= INT64_MAX / 1000) { - pRight->value.i64 *= 1000; - } - } else if (pRight->tokenId == TK_FLOAT && timePrecision == TSDB_TIME_PRECISION_MILLI) { - pRight->value.dKey *= 1000; } if (!parsed) { @@ -4739,33 +4715,19 @@ int32_t getTimeRange(STimeWindow* win, tSqlExpr* pRight, int32_t optr, int16_t t * failed to parse timestamp in regular formation, try next * it may be a epoch time in string format */ - tVariantDump(&pRight->value, (char*)&val, TSDB_DATA_TYPE_BIGINT, true); - - /* - * transfer it into MICROSECOND format if it is a string, since for - * TK_INTEGER/TK_FLOAT the value has been transferred - * - * additional check to avoid data overflow - */ - if (pRight->tokenId == TK_STRING && timePrecision == TSDB_TIME_PRECISION_MILLI) { - if (val <= INT64_MAX / 1000) { - val *= 1000; - } + if (pRight->flags & (1 << EXPR_FLAG_NS_TIMESTAMP)) { + pRight->value.i64 = convertTimePrecision(pRight->value.i64, TSDB_TIME_PRECISION_NANO, timePrecision); } - } - - int32_t delta = 1; - /* for millisecond, delta is 1ms=1000us */ - if (timePrecision == TSDB_TIME_PRECISION_MILLI) { - delta *= 1000; + + tVariantDump(&pRight->value, (char*)&val, TSDB_DATA_TYPE_BIGINT, true); } if (optr == TK_LE) { win->ekey = val; } else if (optr == TK_LT) { - win->ekey = val - delta; + win->ekey = val - 1; } else if (optr == TK_GT) { - win->skey = val + delta; + win->skey = val + 1; } else if (optr == TK_GE) { win->skey = val; } else if (optr == TK_EQ) { @@ -5488,8 +5450,10 @@ int32_t setAlterTableInfo(SSqlObj* pSql, struct SSqlInfo* pInfo) { int32_t validateSqlFunctionInStreamSql(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { const char* msg0 = "sample interval can not be less than 10ms."; const char* msg1 = "functions not allowed in select clause"; - - if (pQueryInfo->interval.interval != 0 && pQueryInfo->interval.interval < 10 && + STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); + if (pQueryInfo->interval.interval != 0 && + convertTimePrecision(pQueryInfo->interval.interval, tinfo.precision, TSDB_TIME_PRECISION_MILLI)< 10 && pQueryInfo->interval.intervalUnit != 'n' && pQueryInfo->interval.intervalUnit != 'y') { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg0); @@ -5882,11 +5846,15 @@ static int32_t setTimePrecision(SSqlCmd* pCmd, SCreateDbMsg* pMsg, SCreateDbInfo } else if (strncmp(pToken->z, TSDB_TIME_PRECISION_MICRO_STR, pToken->n) == 0 && strlen(TSDB_TIME_PRECISION_MICRO_STR) == pToken->n) { pMsg->precision = TSDB_TIME_PRECISION_MICRO; + } else if (strncmp(pToken->z, TSDB_TIME_PRECISION_NANO_STR, pToken->n) == 0 && + strlen(TSDB_TIME_PRECISION_NANO_STR) == pToken->n) { + pMsg->precision = TSDB_TIME_PRECISION_NANO; } else { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } } + return TSDB_CODE_SUCCESS; } @@ -6514,9 +6482,10 @@ int32_t tscCheckCreateDbParams(SSqlCmd* pCmd, SCreateDbMsg* pCreate) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } - if (pCreate->precision != TSDB_TIME_PRECISION_MILLI && pCreate->precision != TSDB_TIME_PRECISION_MICRO) { - snprintf(msg, tListLen(msg), "invalid db option timePrecision: %d valid value: [%d, %d]", pCreate->precision, - TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO); + if (pCreate->precision != TSDB_TIME_PRECISION_MILLI && pCreate->precision != TSDB_TIME_PRECISION_MICRO && + pCreate->precision != TSDB_TIME_PRECISION_NANO) { + snprintf(msg, tListLen(msg), "invalid db option timePrecision: %d valid value: [%d, %d, %d]", pCreate->precision, + TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO, TSDB_TIME_PRECISION_NANO); return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } @@ -7640,12 +7609,6 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf if (validateWhereNode(pQueryInfo, &pSqlNode->pWhere, pSql) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } - - STableMeta* pTableMeta = tscGetMetaInfo(pQueryInfo, 0)->pTableMeta; - if (pTableMeta->tableInfo.precision == TSDB_TIME_PRECISION_MILLI) { - pQueryInfo->window.skey = pQueryInfo->window.skey / 1000; - pQueryInfo->window.ekey = pQueryInfo->window.ekey / 1000; - } } // validate the interval info @@ -7696,18 +7659,12 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf } // set where info - STableComInfo tinfo = tscGetTableInfo(pTableMetaInfo->pTableMeta); - if (pSqlNode->pWhere != NULL) { if (validateWhereNode(pQueryInfo, &pSqlNode->pWhere, pSql) != TSDB_CODE_SUCCESS) { return TSDB_CODE_TSC_INVALID_OPERATION; } pSqlNode->pWhere = NULL; - if (tinfo.precision == TSDB_TIME_PRECISION_MILLI) { - pQueryInfo->window.skey = pQueryInfo->window.skey / 1000; - pQueryInfo->window.ekey = pQueryInfo->window.ekey / 1000; - } } else { if (taosArrayGetSize(pSqlNode->from->list) > 1) { // Cross join not allowed yet return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "cross join not supported yet"); diff --git a/src/client/src/tscStream.c b/src/client/src/tscStream.c index 9b13cd2140dea3bc416f1a418d69b2ef0e187992..d51a277ffe0722627a747ac45e0297bfdbb40991 100644 --- a/src/client/src/tscStream.c +++ b/src/client/src/tscStream.c @@ -53,9 +53,7 @@ static int64_t tscGetRetryDelayTime(SSqlStream* pStream, int64_t slidingTime, in if (pStream->interval.intervalUnit != 'n' && pStream->interval.intervalUnit != 'y') { // change to ms - if (prec == TSDB_TIME_PRECISION_MICRO) { - slidingTime = slidingTime / 1000; - } + slidingTime = convertTimePrecision(slidingTime, pStream->precision, TSDB_TIME_PRECISION_MILLI); if (slidingTime < retryDelta) { return slidingTime; @@ -157,11 +155,7 @@ static void tscProcessStreamTimer(void *handle, void *tmrId) { pQueryInfo->window.skey = pStream->stime; int64_t etime = taosGetTimestamp(pStream->precision); // delay to wait all data in last time window - if (pStream->precision == TSDB_TIME_PRECISION_MICRO) { - etime -= tsMaxStreamComputDelay * 1000l; - } else { - etime -= tsMaxStreamComputDelay; - } + etime -= convertTimePrecision(tsMaxStreamComputDelay, TSDB_TIME_PRECISION_MILLI, pStream->precision); if (etime > pStream->etime) { etime = pStream->etime; } else if (pStream->interval.intervalUnit != 'y' && pStream->interval.intervalUnit != 'n') { @@ -178,8 +172,8 @@ static void tscProcessStreamTimer(void *handle, void *tmrId) { int64_t timer = pStream->interval.sliding; if (pStream->interval.intervalUnit == 'y' || pStream->interval.intervalUnit == 'n') { timer = 86400 * 1000l; - } else if (pStream->precision == TSDB_TIME_PRECISION_MICRO) { - timer /= 1000l; + } else { + timer = convertTimePrecision(timer, pStream->precision, TSDB_TIME_PRECISION_MILLI); } tscSetRetryTimer(pStream, pSql, timer); return; @@ -369,9 +363,8 @@ static void tscSetRetryTimer(SSqlStream *pStream, SSqlObj *pSql, int64_t timer) } static int64_t getLaunchTimeDelay(const SSqlStream* pStream) { - int64_t maxDelay = - (pStream->precision == TSDB_TIME_PRECISION_MICRO) ? tsMaxStreamComputDelay * 1000L : tsMaxStreamComputDelay; - + int64_t maxDelay = convertTimePrecision(tsMaxStreamComputDelay, TSDB_TIME_PRECISION_MILLI, pStream->precision); + int64_t delayDelta = maxDelay; if (pStream->interval.intervalUnit != 'n' && pStream->interval.intervalUnit != 'y') { delayDelta = (int64_t)(pStream->interval.sliding * tsStreamComputDelayRatio); @@ -438,16 +431,14 @@ static void tscSetNextLaunchTimer(SSqlStream *pStream, SSqlObj *pSql) { timer += getLaunchTimeDelay(pStream); - if (pStream->precision == TSDB_TIME_PRECISION_MICRO) { - timer = timer / 1000L; - } + timer = convertTimePrecision(timer, pStream->precision, TSDB_TIME_PRECISION_MILLI); tscSetRetryTimer(pStream, pSql, timer); } static int32_t tscSetSlidingWindowInfo(SSqlObj *pSql, SSqlStream *pStream) { int64_t minIntervalTime = - (pStream->precision == TSDB_TIME_PRECISION_MICRO) ? tsMinIntervalTime * 1000L : tsMinIntervalTime; + convertTimePrecision(tsMinIntervalTime, TSDB_TIME_PRECISION_MILLI, pStream->precision); SQueryInfo* pQueryInfo = tscGetQueryInfo(&pSql->cmd); @@ -471,7 +462,7 @@ static int32_t tscSetSlidingWindowInfo(SSqlObj *pSql, SSqlStream *pStream) { } int64_t minSlidingTime = - (pStream->precision == TSDB_TIME_PRECISION_MICRO) ? tsMinSlidingTime * 1000L : tsMinSlidingTime; + convertTimePrecision(tsMinSlidingTime, TSDB_TIME_PRECISION_MILLI, pStream->precision); if (pQueryInfo->interval.intervalUnit != 'n' && pQueryInfo->interval.intervalUnit!= 'y' && pQueryInfo->interval.sliding < minSlidingTime) { tscWarn("0x%"PRIx64" stream:%p, original sliding value:%" PRId64 " too small, reset to:%" PRId64, pSql->self, pStream, @@ -539,13 +530,12 @@ static int64_t tscGetLaunchTimestamp(const SSqlStream *pStream) { timer = pStream->stime - now; } - int64_t startDelay = - (pStream->precision == TSDB_TIME_PRECISION_MICRO) ? tsStreamCompStartDelay * 1000L : tsStreamCompStartDelay; - + int64_t startDelay = convertTimePrecision(tsStreamCompStartDelay, TSDB_TIME_PRECISION_MILLI, pStream->precision); + timer += getLaunchTimeDelay(pStream); timer += startDelay; - return (pStream->precision == TSDB_TIME_PRECISION_MICRO) ? timer / 1000L : timer; + return convertTimePrecision(timer, pStream->precision, TSDB_TIME_PRECISION_MILLI); } static void tscCreateStream(void *param, TAOS_RES *res, int code) { diff --git a/src/kit/shell/src/shellEngine.c b/src/kit/shell/src/shellEngine.c index d4176fca91cd412b4d2d20a9a6c72a998c443428..d5fa0a9e09f5ac10d4d6a64c48d4639697c49165 100644 --- a/src/kit/shell/src/shellEngine.c +++ b/src/kit/shell/src/shellEngine.c @@ -398,7 +398,10 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) { time_t tt; int32_t ms = 0; - if (precision == TSDB_TIME_PRECISION_MICRO) { + if (precision == TSDB_TIME_PRECISION_NANO) { + tt = (time_t)(val / 1000000000); + ms = val % 1000000000; + } else if (precision == TSDB_TIME_PRECISION_MICRO) { tt = (time_t)(val / 1000000); ms = val % 1000000; } else { @@ -419,7 +422,9 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) { #endif if (tt <= 0 && ms < 0) { tt--; - if (precision == TSDB_TIME_PRECISION_MICRO) { + if (precision == TSDB_TIME_PRECISION_NANO) { + ms += 1000000000; + } else if (precision == TSDB_TIME_PRECISION_MICRO) { ms += 1000000; } else { ms += 1000; @@ -427,9 +432,11 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) { } struct tm* ptm = localtime(&tt); - size_t pos = strftime(buf, 32, "%Y-%m-%d %H:%M:%S", ptm); + size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", ptm); - if (precision == TSDB_TIME_PRECISION_MICRO) { + if (precision == TSDB_TIME_PRECISION_NANO) { + sprintf(buf + pos, ".%09d", ms); + } else if (precision == TSDB_TIME_PRECISION_MICRO) { sprintf(buf + pos, ".%06d", ms); } else { sprintf(buf + pos, ".%03d", ms); @@ -778,6 +785,8 @@ static int calcColWidth(TAOS_FIELD* field, int precision) { case TSDB_DATA_TYPE_TIMESTAMP: if (args.is_raw_time) { return MAX(14, width); + } if (precision == TSDB_TIME_PRECISION_NANO) { + return MAX(29, width); } else if (precision == TSDB_TIME_PRECISION_MICRO) { return MAX(26, width); // '2020-01-01 00:00:00.000000' } else { diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index 6f5d407a7348b2fa7768f0266c5dcd79a6b4653f..c52f41ef8601758aa3521dfb86cac49339a8ff6a 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -1848,7 +1848,9 @@ static void printfQueryMeta() { static char* formatTimestamp(char* buf, int64_t val, int precision) { time_t tt; - if (precision == TSDB_TIME_PRECISION_MICRO) { + if (precision == TSDB_TIME_PRECISION_NANO) { + tt = (time_t)(val / 1000000000); + } else if (precision == TSDB_TIME_PRECISION_MICRO) { tt = (time_t)(val / 1000000); } else { tt = (time_t)(val / 1000); @@ -1869,7 +1871,9 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) { struct tm* ptm = localtime(&tt); size_t pos = strftime(buf, 32, "%Y-%m-%d %H:%M:%S", ptm); - if (precision == TSDB_TIME_PRECISION_MICRO) { + if (precision == TSDB_TIME_PRECISION_NANO) { + sprintf(buf + pos, ".%09d", (int)(val % 1000000000)); + } else if (precision == TSDB_TIME_PRECISION_MICRO) { sprintf(buf + pos, ".%06d", (int)(val % 1000000)); } else { sprintf(buf + pos, ".%03d", (int)(val % 1000)); @@ -6249,9 +6253,11 @@ static void startMultiThreadInsertData(int threads, char* db_name, if (0 != precision[0]) { if (0 == strncasecmp(precision, "ms", 2)) { timePrec = TSDB_TIME_PRECISION_MILLI; - } else if (0 == strncasecmp(precision, "us", 2)) { + } else if (0 == strncasecmp(precision, "us", 2)) { timePrec = TSDB_TIME_PRECISION_MICRO; - } else { + } else if (0 == strncasecmp(precision, "ns", 2)) { + timePrec = TSDB_TIME_PRECISION_NANO; + } else { errorPrint("Not support precision: %s\n", precision); exit(-1); } diff --git a/src/mnode/src/mnodeDb.c b/src/mnode/src/mnodeDb.c index b0ac1192bb8a5545c1e8f731fb92a0d69538eb18..ff36256b188c0540339ebc003f7e029a05bbed35 100644 --- a/src/mnode/src/mnodeDb.c +++ b/src/mnode/src/mnodeDb.c @@ -820,8 +820,13 @@ static int32_t mnodeRetrieveDbs(SShowObj *pShow, char *data, int32_t rows, void #endif pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - char *prec = (pDb->cfg.precision == TSDB_TIME_PRECISION_MILLI) ? TSDB_TIME_PRECISION_MILLI_STR - : TSDB_TIME_PRECISION_MICRO_STR; + char *prec = NULL; + switch (pDb->cfg.precision) { + case TSDB_TIME_PRECISION_MILLI: prec = TSDB_TIME_PRECISION_MILLI_STR; break; + case TSDB_TIME_PRECISION_MICRO: prec = TSDB_TIME_PRECISION_MICRO_STR; break; + case TSDB_TIME_PRECISION_NANO: prec = TSDB_TIME_PRECISION_NANO_STR; break; + default: assert(false); break; + } STR_WITH_SIZE_TO_VARSTR(pWrite, prec, 2); cols++; diff --git a/src/os/inc/osTime.h b/src/os/inc/osTime.h index 4743a8c160ce8228a2c564129f00ec2962038c56..f013a2f7d112c07e2b88b4329ff867e50c25ec0d 100644 --- a/src/os/inc/osTime.h +++ b/src/os/inc/osTime.h @@ -55,6 +55,13 @@ static FORCE_INLINE int64_t taosGetTimestampUs() { return (int64_t)systemTime.tv_sec * 1000000L + (int64_t)systemTime.tv_usec; } +//@return timestamp in nanosecond +static FORCE_INLINE int64_t taosGetTimestampNs() { + struct timespec systemTime = {0}; + clock_gettime(CLOCK_REALTIME, &systemTime); + return (int64_t)systemTime.tv_sec * 1000000000L + (int64_t)systemTime.tv_nsec; +} + /* * @return timestamp decided by global conf variable, tsTimePrecision * if precision == TSDB_TIME_PRECISION_MICRO, it returns timestamp in microsecond. @@ -63,7 +70,9 @@ static FORCE_INLINE int64_t taosGetTimestampUs() { static FORCE_INLINE int64_t taosGetTimestamp(int32_t precision) { if (precision == TSDB_TIME_PRECISION_MICRO) { return taosGetTimestampUs(); - } else { + } else if (precision == TSDB_TIME_PRECISION_NANO) { + return taosGetTimestampNs(); + }else { return taosGetTimestampMs(); } } @@ -88,12 +97,13 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision); int64_t taosTimeTruncate(int64_t t, const SInterval* pInterval, int32_t precision); int32_t taosTimeCountInterval(int64_t skey, int64_t ekey, int64_t interval, char unit, int32_t precision); -int32_t parseAbsoluteDuration(char* token, int32_t tokenlen, int64_t* ts); -int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit); +int32_t parseAbsoluteDuration(char* token, int32_t tokenlen, int64_t* ts, int32_t timePrecision); +int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision); int32_t taosParseTime(char* timestr, int64_t* time, int32_t len, int32_t timePrec, int8_t dayligth); void deltaToUtcInitOnce(); +int64_t convertTimePrecision(int64_t time, int32_t fromPrecision, int32_t toPrecision); #ifdef __cplusplus } #endif diff --git a/src/os/src/detail/osTime.c b/src/os/src/detail/osTime.c index 67e0c2642e42f66229c437e603f7062edf571f34..73122dc666a327c9e0cdabe557e68d6f51b0bcc1 100644 --- a/src/os/src/detail/osTime.c +++ b/src/os/src/detail/osTime.c @@ -14,7 +14,13 @@ */ #define _BSD_SOURCE + +#ifdef DARWIN #define _XOPEN_SOURCE +#else +#define _XOPEN_SOURCE 500 +#endif + #define _DEFAULT_SOURCE #include "os.h" @@ -119,8 +125,9 @@ int64_t parseFraction(char* str, char** end, int32_t timePrec) { const int32_t MILLI_SEC_FRACTION_LEN = 3; const int32_t MICRO_SEC_FRACTION_LEN = 6; + const int32_t NANO_SEC_FRACTION_LEN = 9; - int32_t factor[6] = {1, 10, 100, 1000, 10000, 100000}; + int32_t factor[9] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}; int32_t times = 1; while (str[i] >= '0' && str[i] <= '9') { @@ -140,12 +147,17 @@ int64_t parseFraction(char* str, char** end, int32_t timePrec) { } times = MILLI_SEC_FRACTION_LEN - i; - } else { - assert(timePrec == TSDB_TIME_PRECISION_MICRO); + } else if (timePrec == TSDB_TIME_PRECISION_MICRO) { if (i >= MICRO_SEC_FRACTION_LEN) { i = MICRO_SEC_FRACTION_LEN; } times = MICRO_SEC_FRACTION_LEN - i; + } else { + assert(timePrec == TSDB_TIME_PRECISION_NANO); + if (i >= NANO_SEC_FRACTION_LEN) { + i = NANO_SEC_FRACTION_LEN; + } + times = NANO_SEC_FRACTION_LEN - i; } fraction = strnatoi(str, i) * factor[times]; @@ -202,7 +214,9 @@ int32_t parseTimezone(char* str, int64_t* tzOffset) { * 2013-04-12T15:52:01.123+0800 */ int32_t parseTimeWithTz(char* timestr, int64_t* time, int32_t timePrec) { - int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : 1000000; + + int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : + (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000); int64_t tzOffset = 0; struct tm tm = {0}; @@ -287,7 +301,8 @@ int32_t parseLocaltime(char* timestr, int64_t* time, int32_t timePrec) { } } - int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : 1000000; + int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : + (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000); *time = factor * seconds + fraction; return 0; @@ -315,37 +330,50 @@ int32_t parseLocaltimeWithDst(char* timestr, int64_t* time, int32_t timePrec) { } } - int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : 1000000; + int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 : + (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000); *time = factor * seconds + fraction; return 0; } - -static int32_t getDurationInUs(int64_t val, char unit, int64_t* result) { - *result = val; - - int64_t factor = 1000L; +int64_t convertTimePrecision(int64_t time, int32_t fromPrecision, int32_t toPrecision) { + assert(fromPrecision == TSDB_TIME_PRECISION_MILLI || + fromPrecision == TSDB_TIME_PRECISION_MICRO || + fromPrecision == TSDB_TIME_PRECISION_NANO); + assert(toPrecision == TSDB_TIME_PRECISION_MILLI || + toPrecision == TSDB_TIME_PRECISION_MICRO || + toPrecision == TSDB_TIME_PRECISION_NANO); + static double factors[3][3] = { {1., 1000., 1000000.}, + {1.0 / 1000, 1., 1000.}, + {1.0 / 1000000, 1.0 / 1000, 1.} }; + return (int64_t)((double)time * factors[fromPrecision][toPrecision]); +} +static int32_t getDuration(int64_t val, char unit, int64_t* result, int32_t timePrecision) { switch (unit) { case 's': - (*result) *= MILLISECOND_PER_SECOND*factor; + (*result) = convertTimePrecision(val * MILLISECOND_PER_SECOND, TSDB_TIME_PRECISION_MILLI, timePrecision); break; case 'm': - (*result) *= MILLISECOND_PER_MINUTE*factor; + (*result) = convertTimePrecision(val * MILLISECOND_PER_MINUTE, TSDB_TIME_PRECISION_MILLI, timePrecision); break; case 'h': - (*result) *= MILLISECOND_PER_HOUR*factor; + (*result) = convertTimePrecision(val * MILLISECOND_PER_HOUR, TSDB_TIME_PRECISION_MILLI, timePrecision); break; case 'd': - (*result) *= MILLISECOND_PER_DAY*factor; + (*result) = convertTimePrecision(val * MILLISECOND_PER_DAY, TSDB_TIME_PRECISION_MILLI, timePrecision); break; case 'w': - (*result) *= MILLISECOND_PER_WEEK*factor; + (*result) = convertTimePrecision(val * MILLISECOND_PER_WEEK, TSDB_TIME_PRECISION_MILLI, timePrecision); break; case 'a': - (*result) *= factor; + (*result) = convertTimePrecision(val, TSDB_TIME_PRECISION_MILLI, timePrecision); break; case 'u': + (*result) = convertTimePrecision(val, TSDB_TIME_PRECISION_MICRO, timePrecision); + break; + case 'b': + (*result) = convertTimePrecision(val, TSDB_TIME_PRECISION_NANO, timePrecision); break; default: { return -1; @@ -357,6 +385,8 @@ static int32_t getDurationInUs(int64_t val, char unit, int64_t* result) { } /* + * b - nanoseconds; + * u - microseconds; * a - Millionseconds * s - Seconds * m - Minutes @@ -366,7 +396,7 @@ static int32_t getDurationInUs(int64_t val, char unit, int64_t* result) { * n - Months (30 days) * y - Years (365 days) */ -int32_t parseAbsoluteDuration(char* token, int32_t tokenlen, int64_t* duration) { +int32_t parseAbsoluteDuration(char* token, int32_t tokenlen, int64_t* duration, int32_t timePrecision) { errno = 0; char* endPtr = NULL; @@ -382,10 +412,10 @@ int32_t parseAbsoluteDuration(char* token, int32_t tokenlen, int64_t* duration) return -1; } - return getDurationInUs(timestamp, unit, duration); + return getDuration(timestamp, unit, duration, timePrecision); } -int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit) { +int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision) { errno = 0; /* get the basic numeric value */ @@ -399,7 +429,7 @@ int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* durati return 0; } - return getDurationInUs(*duration, *unit, duration); + return getDuration(*duration, *unit, duration, timePrecision); } int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) { diff --git a/src/plugins/http/inc/httpJson.h b/src/plugins/http/inc/httpJson.h index 4d182d0132528367a8e25b8f72329331be2cb5c5..3595ad926fa08a7ba87fc758c2b0472d34617488 100644 --- a/src/plugins/http/inc/httpJson.h +++ b/src/plugins/http/inc/httpJson.h @@ -64,8 +64,8 @@ void httpJsonOriginString(JsonBuf* buf, char* sVal, int32_t len); void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int32_t maxLen); void httpJsonInt64(JsonBuf* buf, int64_t num); void httpJsonUInt64(JsonBuf* buf, uint64_t num); -void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us); -void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us); +void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision); +void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision); void httpJsonInt(JsonBuf* buf, int32_t num); void httpJsonUInt(JsonBuf* buf, uint32_t num); void httpJsonFloat(JsonBuf* buf, float num); diff --git a/src/plugins/http/src/httpJson.c b/src/plugins/http/src/httpJson.c index 19166e720f0e517ccd1a793742836ae4bc245bd1..df4c4fbc63d469c22ea13273564bec49562f35a9 100644 --- a/src/plugins/http/src/httpJson.c +++ b/src/plugins/http/src/httpJson.c @@ -262,42 +262,92 @@ void httpJsonUInt64(JsonBuf* buf, uint64_t num) { buf->lst += snprintf(buf->lst, MAX_NUM_STR_SZ, "%" PRIu64, num); } -void httpJsonTimestamp(JsonBuf* buf, int64_t t, bool us) { +void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { char ts[35] = {0}; struct tm* ptm; - int32_t precision = 1000; - if (us) { - precision = 1000000; + + int32_t fractionLen; + char* format = NULL; + time_t quot = 0; + long mod = 0; + + switch (timePrecision) { + case TSDB_TIME_PRECISION_MILLI: { + quot = t / 1000; + fractionLen = 5; + format = ".%03" PRId64; + mod = t % 1000; + break; + } + + case TSDB_TIME_PRECISION_MICRO: { + quot = t / 1000000; + fractionLen = 8; + format = ".%06" PRId64; + mod = t % 1000000; + break; + } + + case TSDB_TIME_PRECISION_NANO: { + quot = t / 1000000000; + fractionLen = 11; + format = ".%09" PRId64; + mod = t % 1000000000; + break; + } + + default: + assert(false); } - time_t tt = t / precision; - ptm = localtime(&tt); + ptm = localtime("); int32_t length = (int32_t)strftime(ts, 35, "%Y-%m-%d %H:%M:%S", ptm); - if (us) { - length += snprintf(ts + length, 8, ".%06" PRId64, t % precision); - } else { - length += snprintf(ts + length, 5, ".%03" PRId64, t % precision); - } + length += snprintf(ts + length, fractionLen, format, mod); httpJsonString(buf, ts, length); } -void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, bool us) { +void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision) { char ts[40] = {0}; struct tm* ptm; - int32_t precision = 1000; - if (us) { - precision = 1000000; + + int32_t fractionLen; + char* format = NULL; + time_t quot = 0; + long mod = 0; + + switch (timePrecision) { + case TSDB_TIME_PRECISION_MILLI: { + quot = t / 1000; + fractionLen = 5; + format = ".%03" PRId64; + mod = t % 1000; + break; + } + + case TSDB_TIME_PRECISION_MICRO: { + quot = t / 1000000; + fractionLen = 8; + format = ".%06" PRId64; + mod = t % 1000000; + break; + } + + case TSDB_TIME_PRECISION_NANO: { + quot = t / 1000000000; + fractionLen = 11; + format = ".%09" PRId64; + mod = t % 1000000000; + break; + } + + default: + assert(false); } - time_t tt = t / precision; - ptm = localtime(&tt); + ptm = localtime("); int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", ptm); - if (us) { - length += snprintf(ts + length, 8, ".%06" PRId64, t % precision); - } else { - length += snprintf(ts + length, 5, ".%03" PRId64, t % precision); - } + length += snprintf(ts + length, fractionLen, format, mod); length += (int32_t)strftime(ts + length, 40 - length, "%z", ptm); httpJsonString(buf, ts, length); diff --git a/src/plugins/http/src/httpRestJson.c b/src/plugins/http/src/httpRestJson.c index 60c23e603e5107e858db2138d1aed87d5f8a5e1b..47f2d4ff5bcc513aafb8ea8f4e2a85db5a35b12a 100644 --- a/src/plugins/http/src/httpRestJson.c +++ b/src/plugins/http/src/httpRestJson.c @@ -186,13 +186,11 @@ bool restBuildSqlJson(HttpContext *pContext, HttpSqlCmd *cmd, TAOS_RES *result, break; case TSDB_DATA_TYPE_TIMESTAMP: if (timestampFormat == REST_TIMESTAMP_FMT_LOCAL_STRING) { - httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]), - taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO); + httpJsonTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result)); } else if (timestampFormat == REST_TIMESTAMP_FMT_TIMESTAMP) { httpJsonInt64(jsonBuf, *((int64_t *)row[i])); } else { - httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]), - taos_result_precision(result) == TSDB_TIME_PRECISION_MICRO); + httpJsonUtcTimestamp(jsonBuf, *((int64_t *)row[i]), taos_result_precision(result)); } break; default: diff --git a/src/query/inc/qSqlparser.h b/src/query/inc/qSqlparser.h index 652191bf0fb402ab1114a2797176b0379dd5dba4..c2a4e99b63b1a3d2b07ba11f79f0f8be66e529e2 100644 --- a/src/query/inc/qSqlparser.h +++ b/src/query/inc/qSqlparser.h @@ -46,7 +46,7 @@ enum SQL_NODE_FROM_TYPE { enum SQL_EXPR_FLAG { EXPR_FLAG_TS_ERROR = 1, - EXPR_FLAG_US_TIMESTAMP = 2, + EXPR_FLAG_NS_TIMESTAMP = 2, EXPR_FLAG_TIMESTAMP_VAR = 3, }; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 5ac19bba82508361a1b46ca58291648dc531e008..d15fd08100248b0c57bd60089eb67def073331b0 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -130,10 +130,10 @@ static void getNextTimeWindow(SQueryAttr* pQueryAttr, STimeWindow* tw) { return; } - int64_t key = tw->skey / 1000, interval = pQueryAttr->interval.interval; - if (pQueryAttr->precision == TSDB_TIME_PRECISION_MICRO) { - key /= 1000; - } + int64_t key = tw->skey, interval = pQueryAttr->interval.interval; + //convert key to second + key = convertTimePrecision(key, pQueryAttr->precision, TSDB_TIME_PRECISION_MILLI) / 1000; + if (pQueryAttr->interval.intervalUnit == 'y') { interval *= 12; } @@ -145,17 +145,13 @@ static void getNextTimeWindow(SQueryAttr* pQueryAttr, STimeWindow* tw) { int mon = (int)(tm.tm_year * 12 + tm.tm_mon + interval * factor); tm.tm_year = mon / 12; tm.tm_mon = mon % 12; - tw->skey = mktime(&tm) * 1000L; + tw->skey = convertTimePrecision(mktime(&tm) * 1000L, TSDB_TIME_PRECISION_MILLI, pQueryAttr->precision); mon = (int)(mon + interval); tm.tm_year = mon / 12; tm.tm_mon = mon % 12; - tw->ekey = mktime(&tm) * 1000L; + tw->ekey = convertTimePrecision(mktime(&tm) * 1000L, TSDB_TIME_PRECISION_MILLI, pQueryAttr->precision); - if (pQueryAttr->precision == TSDB_TIME_PRECISION_MICRO) { - tw->skey *= 1000L; - tw->ekey *= 1000L; - } tw->ekey -= 1; } diff --git a/src/query/src/qSqlParser.c b/src/query/src/qSqlParser.c index 2cf3463d6009f47802e89f0651257d6f6f803376..f725c7da2f6f6d8483d953b4022bd6b21aad051d 100644 --- a/src/query/src/qSqlParser.c +++ b/src/query/src/qSqlParser.c @@ -139,19 +139,20 @@ tSqlExpr *tSqlExprCreateIdValue(SStrToken *pToken, int32_t optrType) { pSqlExpr->tokenId = optrType; pSqlExpr->type = SQL_NODE_VALUE; } else if (optrType == TK_NOW) { - // use microsecond by default - pSqlExpr->value.i64 = taosGetTimestamp(TSDB_TIME_PRECISION_MICRO); + // use nanosecond by default TODO set value after getting database precision + pSqlExpr->value.i64 = taosGetTimestamp(TSDB_TIME_PRECISION_NANO); pSqlExpr->value.nType = TSDB_DATA_TYPE_BIGINT; pSqlExpr->tokenId = TK_TIMESTAMP; // TK_TIMESTAMP used to denote the time value is in microsecond pSqlExpr->type = SQL_NODE_VALUE; - pSqlExpr->flags |= 1 << EXPR_FLAG_US_TIMESTAMP; + pSqlExpr->flags |= 1 << EXPR_FLAG_NS_TIMESTAMP; } else if (optrType == TK_VARIABLE) { - int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSqlExpr->value.i64); + // use nanosecond by default TODO set value after getting database precision + int32_t ret = parseAbsoluteDuration(pToken->z, pToken->n, &pSqlExpr->value.i64, TSDB_TIME_PRECISION_NANO); if (ret != TSDB_CODE_SUCCESS) { terrno = TSDB_CODE_TSC_SQL_SYNTAX_ERROR; } - pSqlExpr->flags |= 1 << EXPR_FLAG_US_TIMESTAMP; + pSqlExpr->flags |= 1 << EXPR_FLAG_NS_TIMESTAMP; pSqlExpr->flags |= 1 << EXPR_FLAG_TIMESTAMP_VAR; pSqlExpr->value.nType = TSDB_DATA_TYPE_BIGINT; pSqlExpr->tokenId = TK_TIMESTAMP; diff --git a/src/util/src/ttokenizer.c b/src/util/src/ttokenizer.c index 7026fcda0f156c9edc6fb17dd192aae88e3042a3..3ab2aca67fac36dff1280d6fa35849f3222fb203 100644 --- a/src/util/src/ttokenizer.c +++ b/src/util/src/ttokenizer.c @@ -494,9 +494,9 @@ uint32_t tGetToken(char* z, uint32_t* tokenId) { } /* here is the 1u/1a/2s/3m/9y */ - if ((z[i] == 'u' || z[i] == 'a' || z[i] == 's' || z[i] == 'm' || z[i] == 'h' || z[i] == 'd' || z[i] == 'n' || + if ((z[i] == 'b' || z[i] == 'u' || z[i] == 'a' || z[i] == 's' || z[i] == 'm' || z[i] == 'h' || z[i] == 'd' || z[i] == 'n' || z[i] == 'y' || z[i] == 'w' || - z[i] == 'U' || z[i] == 'A' || z[i] == 'S' || z[i] == 'M' || z[i] == 'H' || z[i] == 'D' || z[i] == 'N' || + z[i] == 'B' || z[i] == 'U' || z[i] == 'A' || z[i] == 'S' || z[i] == 'M' || z[i] == 'H' || z[i] == 'D' || z[i] == 'N' || z[i] == 'Y' || z[i] == 'W') && (isIdChar[(uint8_t)z[i + 1]] == 0)) { *tokenId = TK_VARIABLE;