未验证 提交 5a5c20bd 编写于 作者: D dapan1121 提交者: GitHub

Merge pull request #9059 from taosdata/fix/TD-11970

[TD-11970]<fix>: support timestamp arithmetic operation like "now + 1h/s/d" on timestamp tags
...@@ -39,7 +39,9 @@ typedef struct tVariant { ...@@ -39,7 +39,9 @@ typedef struct tVariant {
bool tVariantIsValid(tVariant *pVar); bool tVariantIsValid(tVariant *pVar);
void tVariantCreate(tVariant *pVar, SStrToken *token, bool needRmquoteEscape); void tVariantCreate(tVariant *pVar, SStrToken *token);
void tVariantCreateExt(tVariant *pVar, SStrToken *token, int32_t optrType, bool needRmquoteEscape);
void tVariantCreateFromBinary(tVariant *pVar, const char *pz, size_t len, uint32_t type); void tVariantCreateFromBinary(tVariant *pVar, const char *pz, size_t len, uint32_t type);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "hash.h" #include "hash.h"
#include "taos.h" #include "taos.h"
#include "taoserror.h"
#include "taosdef.h" #include "taosdef.h"
#include "ttoken.h" #include "ttoken.h"
#include "ttokendef.h" #include "ttokendef.h"
...@@ -30,7 +31,11 @@ ...@@ -30,7 +31,11 @@
assert(0); \ assert(0); \
} while (0) } while (0)
void tVariantCreate(tVariant *pVar, SStrToken *token, bool needRmquoteEscape) { void tVariantCreate(tVariant *pVar, SStrToken *token) {
tVariantCreateExt(pVar, token, TK_ID, true);
}
void tVariantCreateExt(tVariant *pVar, SStrToken *token, int32_t optrType, bool needRmquoteEscape) {
int32_t ret = 0; int32_t ret = 0;
int32_t type = token->type; int32_t type = token->type;
...@@ -54,7 +59,7 @@ void tVariantCreate(tVariant *pVar, SStrToken *token, bool needRmquoteEscape) { ...@@ -54,7 +59,7 @@ void tVariantCreate(tVariant *pVar, SStrToken *token, bool needRmquoteEscape) {
case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_BIGINT:
case TSDB_DATA_TYPE_INT:{ case TSDB_DATA_TYPE_INT:{
ret = tStrToInteger(token->z, token->type, token->n, &pVar->i64, true); ret = tStrToInteger(token->z, token->type, token->n, &pVar->i64, true);
if (ret != 0) { if (ret != TSDB_CODE_SUCCESS) {
SStrToken t = {0}; SStrToken t = {0};
tGetToken(token->z, &t.type); tGetToken(token->z, &t.type);
if (t.type == TK_MINUS) { // it is a signed number which is greater than INT64_MAX or less than INT64_MIN if (t.type == TK_MINUS) { // it is a signed number which is greater than INT64_MAX or less than INT64_MIN
...@@ -64,7 +69,7 @@ void tVariantCreate(tVariant *pVar, SStrToken *token, bool needRmquoteEscape) { ...@@ -64,7 +69,7 @@ void tVariantCreate(tVariant *pVar, SStrToken *token, bool needRmquoteEscape) {
// data overflow, try unsigned parse the input number // data overflow, try unsigned parse the input number
ret = tStrToInteger(token->z, token->type, token->n, &pVar->i64, false); ret = tStrToInteger(token->z, token->type, token->n, &pVar->i64, false);
if (ret != 0) { if (ret != TSDB_CODE_SUCCESS) {
pVar->nType = -1; // -1 means error type pVar->nType = -1; // -1 means error type
return; return;
} }
...@@ -85,7 +90,21 @@ void tVariantCreate(tVariant *pVar, SStrToken *token, bool needRmquoteEscape) { ...@@ -85,7 +90,21 @@ void tVariantCreate(tVariant *pVar, SStrToken *token, bool needRmquoteEscape) {
break; break;
} }
case TSDB_DATA_TYPE_TIMESTAMP: { case TSDB_DATA_TYPE_TIMESTAMP: {
if (optrType == TK_NOW) {
pVar->i64 = taosGetTimestamp(TSDB_TIME_PRECISION_NANO); pVar->i64 = taosGetTimestamp(TSDB_TIME_PRECISION_NANO);
} else if (optrType == TK_PLUS || optrType == TK_MINUS) {
char unit = 0;
ret = parseAbsoluteDuration(token->z, token->n, &pVar->i64, &unit, TSDB_TIME_PRECISION_NANO);
if (ret != TSDB_CODE_SUCCESS) {
pVar->nType = -1; // -1 means error type
return;
}
if (optrType == TK_PLUS) {
pVar->i64 += taosGetTimestamp(TSDB_TIME_PRECISION_NANO);
} else {
pVar->i64 = taosGetTimestamp(TSDB_TIME_PRECISION_NANO) - pVar->i64;
}
}
break; break;
} }
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#ifndef TDENGINE_TTOKENDEF_H #ifndef TDENGINE_TTOKENDEF_H
#define TDENGINE_TTOKENDEF_H #define TDENGINE_TTOKENDEF_H
#define TK_ID 1 #define TK_ID 1
#define TK_BOOL 2 #define TK_BOOL 2
#define TK_TINYINT 3 #define TK_TINYINT 3
...@@ -139,12 +138,12 @@ ...@@ -139,12 +138,12 @@
#define TK_USING 120 #define TK_USING 120
#define TK_NULL 121 #define TK_NULL 121
#define TK_NOW 122 #define TK_NOW 122
#define TK_SELECT 123 #define TK_VARIABLE 123
#define TK_UNION 124 #define TK_SELECT 124
#define TK_ALL 125 #define TK_UNION 125
#define TK_DISTINCT 126 #define TK_ALL 126
#define TK_FROM 127 #define TK_DISTINCT 127
#define TK_VARIABLE 128 #define TK_FROM 128
#define TK_RANGE 129 #define TK_RANGE 129
#define TK_INTERVAL 130 #define TK_INTERVAL 130
#define TK_EVERY 131 #define TK_EVERY 131
...@@ -219,7 +218,6 @@ ...@@ -219,7 +218,6 @@
#define TK_FILE 200 #define TK_FILE 200
#define TK_SPACE 300 #define TK_SPACE 300
#define TK_COMMENT 301 #define TK_COMMENT 301
#define TK_ILLEGAL 302 #define TK_ILLEGAL 302
......
...@@ -253,7 +253,7 @@ acct_optr(Y) ::= pps(C) tseries(D) storage(P) streams(F) qtime(Q) dbs(E) users(K ...@@ -253,7 +253,7 @@ acct_optr(Y) ::= pps(C) tseries(D) storage(P) streams(F) qtime(Q) dbs(E) users(K
intitemlist(A) ::= intitemlist(X) COMMA intitem(Y). { A = tVariantListAppend(X, &Y, -1); } intitemlist(A) ::= intitemlist(X) COMMA intitem(Y). { A = tVariantListAppend(X, &Y, -1); }
intitemlist(A) ::= intitem(X). { A = tVariantListAppend(NULL, &X, -1); } intitemlist(A) ::= intitem(X). { A = tVariantListAppend(NULL, &X, -1); }
intitem(A) ::= INTEGER(X). { toTSDBType(X.type); tVariantCreate(&A, &X, true); } intitem(A) ::= INTEGER(X). { toTSDBType(X.type); tVariantCreate(&A, &X); }
%type keep {SArray*} %type keep {SArray*}
%destructor keep {taosArrayDestroy($$);} %destructor keep {taosArrayDestroy($$);}
...@@ -438,39 +438,49 @@ column(A) ::= ids(X) typename(Y). { ...@@ -438,39 +438,49 @@ column(A) ::= ids(X) typename(Y). {
tagitemlist(A) ::= tagitemlist(X) COMMA tagitem(Y). { A = tVariantListAppend(X, &Y, -1); } tagitemlist(A) ::= tagitemlist(X) COMMA tagitem(Y). { A = tVariantListAppend(X, &Y, -1); }
tagitemlist(A) ::= tagitem(X). { A = tVariantListAppend(NULL, &X, -1); } tagitemlist(A) ::= tagitem(X). { A = tVariantListAppend(NULL, &X, -1); }
tagitem(A) ::= INTEGER(X). { toTSDBType(X.type); tVariantCreate(&A, &X, true); } tagitem(A) ::= INTEGER(X). { toTSDBType(X.type); tVariantCreate(&A, &X); }
tagitem(A) ::= FLOAT(X). { toTSDBType(X.type); tVariantCreate(&A, &X, true); } tagitem(A) ::= FLOAT(X). { toTSDBType(X.type); tVariantCreate(&A, &X); }
tagitem(A) ::= STRING(X). { toTSDBType(X.type); tVariantCreate(&A, &X, true); } tagitem(A) ::= STRING(X). { toTSDBType(X.type); tVariantCreate(&A, &X); }
tagitem(A) ::= BOOL(X). { toTSDBType(X.type); tVariantCreate(&A, &X, true); } tagitem(A) ::= BOOL(X). { toTSDBType(X.type); tVariantCreate(&A, &X); }
tagitem(A) ::= NULL(X). { X.type = 0; tVariantCreate(&A, &X, true); } tagitem(A) ::= NULL(X). { X.type = 0; tVariantCreate(&A, &X); }
tagitem(A) ::= NOW(X). { X.type = TSDB_DATA_TYPE_TIMESTAMP; tVariantCreate(&A, &X, true);} tagitem(A) ::= NOW(X). { X.type = TSDB_DATA_TYPE_TIMESTAMP; tVariantCreateExt(&A, &X, TK_NOW, true);}
tagitem(A) ::= NOW PLUS VARIABLE(X).{
X.type = TSDB_DATA_TYPE_TIMESTAMP;
tVariantCreateExt(&A, &X, TK_PLUS, true);
}
tagitem(A) ::= NOW MINUS VARIABLE(X).{
X.type = TSDB_DATA_TYPE_TIMESTAMP;
tVariantCreateExt(&A, &X, TK_MINUS, true);
}
tagitem(A) ::= MINUS(X) INTEGER(Y).{ tagitem(A) ::= MINUS(X) INTEGER(Y).{
X.n += Y.n; X.n += Y.n;
X.type = Y.type; X.type = Y.type;
toTSDBType(X.type); toTSDBType(X.type);
tVariantCreate(&A, &X, true); tVariantCreate(&A, &X);
} }
tagitem(A) ::= MINUS(X) FLOAT(Y). { tagitem(A) ::= MINUS(X) FLOAT(Y). {
X.n += Y.n; X.n += Y.n;
X.type = Y.type; X.type = Y.type;
toTSDBType(X.type); toTSDBType(X.type);
tVariantCreate(&A, &X, true); tVariantCreate(&A, &X);
} }
tagitem(A) ::= PLUS(X) INTEGER(Y). { tagitem(A) ::= PLUS(X) INTEGER(Y). {
X.n += Y.n; X.n += Y.n;
X.type = Y.type; X.type = Y.type;
toTSDBType(X.type); toTSDBType(X.type);
tVariantCreate(&A, &X, true); tVariantCreate(&A, &X);
} }
tagitem(A) ::= PLUS(X) FLOAT(Y). { tagitem(A) ::= PLUS(X) FLOAT(Y). {
X.n += Y.n; X.n += Y.n;
X.type = Y.type; X.type = Y.type;
toTSDBType(X.type); toTSDBType(X.type);
tVariantCreate(&A, &X, true); tVariantCreate(&A, &X);
} }
//////////////////////// The SELECT statement ///////////////////////////////// //////////////////////// The SELECT statement /////////////////////////////////
...@@ -609,7 +619,7 @@ fill_opt(N) ::= . { N = 0; } ...@@ -609,7 +619,7 @@ fill_opt(N) ::= . { N = 0; }
fill_opt(N) ::= FILL LP ID(Y) COMMA tagitemlist(X) RP. { fill_opt(N) ::= FILL LP ID(Y) COMMA tagitemlist(X) RP. {
tVariant A = {0}; tVariant A = {0};
toTSDBType(Y.type); toTSDBType(Y.type);
tVariantCreate(&A, &Y, true); tVariantCreate(&A, &Y);
tVariantListInsert(X, &A, -1, 0); tVariantListInsert(X, &A, -1, 0);
N = X; N = X;
...@@ -652,12 +662,12 @@ sortlist(A) ::= arrow(Y) sortorder(Z). { ...@@ -652,12 +662,12 @@ sortlist(A) ::= arrow(Y) sortorder(Z). {
%type item {tVariant} %type item {tVariant}
item(A) ::= ID(X). { item(A) ::= ID(X). {
toTSDBType(X.type); toTSDBType(X.type);
tVariantCreate(&A, &X, true); tVariantCreate(&A, &X);
} }
item(A) ::= ID(X) DOT ID(Y). { item(A) ::= ID(X) DOT ID(Y). {
toTSDBType(X.type); toTSDBType(X.type);
X.n += (1+Y.n); X.n += (1+Y.n);
tVariantCreate(&A, &X, true); tVariantCreate(&A, &X);
} }
%type sortorder {int} %type sortorder {int}
......
...@@ -143,14 +143,14 @@ tSqlExpr *tSqlExprCreateIdValue(SSqlInfo* pInfo, SStrToken *pToken, int32_t optr ...@@ -143,14 +143,14 @@ tSqlExpr *tSqlExprCreateIdValue(SSqlInfo* pInfo, SStrToken *pToken, int32_t optr
if (optrType == TK_NULL) { if (optrType == TK_NULL) {
if (pToken){ if (pToken){
pToken->type = TSDB_DATA_TYPE_NULL; pToken->type = TSDB_DATA_TYPE_NULL;
tVariantCreate(&pSqlExpr->value, pToken, true); tVariantCreate(&pSqlExpr->value, pToken);
} }
pSqlExpr->tokenId = optrType; pSqlExpr->tokenId = optrType;
pSqlExpr->type = SQL_NODE_VALUE; pSqlExpr->type = SQL_NODE_VALUE;
} else if (optrType == TK_INTEGER || optrType == TK_STRING || optrType == TK_FLOAT || optrType == TK_BOOL) { } else if (optrType == TK_INTEGER || optrType == TK_STRING || optrType == TK_FLOAT || optrType == TK_BOOL) {
if (pToken) { if (pToken) {
toTSDBType(pToken->type); toTSDBType(pToken->type);
tVariantCreate(&pSqlExpr->value, pToken, true); tVariantCreate(&pSqlExpr->value, pToken);
} }
pSqlExpr->tokenId = optrType; pSqlExpr->tokenId = optrType;
pSqlExpr->type = SQL_NODE_VALUE; pSqlExpr->type = SQL_NODE_VALUE;
...@@ -211,7 +211,7 @@ tSqlExpr *tSqlExprCreateTimestamp(SStrToken *pToken, int32_t optrType) { ...@@ -211,7 +211,7 @@ tSqlExpr *tSqlExprCreateTimestamp(SStrToken *pToken, int32_t optrType) {
if (optrType == TK_INTEGER || optrType == TK_STRING) { if (optrType == TK_INTEGER || optrType == TK_STRING) {
if (pToken) { if (pToken) {
toTSDBType(pToken->type); toTSDBType(pToken->type);
tVariantCreate(&pSqlExpr->value, pToken, true); tVariantCreate(&pSqlExpr->value, pToken);
} }
pSqlExpr->tokenId = optrType; pSqlExpr->tokenId = optrType;
pSqlExpr->type = SQL_NODE_VALUE; pSqlExpr->type = SQL_NODE_VALUE;
...@@ -599,7 +599,7 @@ SArray *tVariantListAppendToken(SArray *pList, SStrToken *pToken, uint8_t order, ...@@ -599,7 +599,7 @@ SArray *tVariantListAppendToken(SArray *pList, SStrToken *pToken, uint8_t order,
if (pToken) { if (pToken) {
tVariantListItem item; tVariantListItem item;
tVariantCreate(&item.pVar, pToken, needRmquoteEscape); tVariantCreateExt(&item.pVar, pToken, TK_ID, needRmquoteEscape);
item.sortOrder = order; item.sortOrder = order;
taosArrayPush(pList, &item); taosArrayPush(pList, &item);
......
此差异已折叠。
...@@ -238,3 +238,4 @@ run general/parser/tbname_escape.sim ...@@ -238,3 +238,4 @@ run general/parser/tbname_escape.sim
run general/parser/columnName_escape.sim run general/parser/columnName_escape.sim
run general/parser/tagName_escape.sim run general/parser/tagName_escape.sim
run general/parser/interp_blocks.sim run general/parser/interp_blocks.sim
run general/parser/create_tb_with_timestamp_tag.sim
system sh/stop_dnodes.sh
system sh/deploy.sh -n dnode1 -i 1
system sh/cfg.sh -n dnode1 -c walLevel -v 1
system sh/cfg.sh -n dnode1 -c maxtablesPerVnode -v 2
system sh/exec.sh -n dnode1 -s start
sleep 100
sql connect
print ======================== dnode1 start
$db = testdb
sql create database $db precision 'ns'
sql use $db
sql create stable st1 (ts timestamp , c1 int) tags(t1 timestamp, t2 int)
sql create table t1_0 using st1 tags(now, 0)
#nanoseconds
sql create table t1_1 using st1 tags(now + 1b, 0)
#microseconds
sql create table t1_2 using st1 tags(now + 1u, 0)
#milliseconds
sql create table t1_3 using st1 tags(now + 1a, 0)
#seconds
sql create table t1_4 using st1 tags(now + 1s, 0)
#minutes
sql create table t1_5 using st1 tags(now + 1m, 0)
#hours
sql create table t1_6 using st1 tags(now + 1h, 0)
#days
sql create table t1_7 using st1 tags(now + 1d, 0)
#weeks
sql create table t1_8 using st1 tags(now + 1w, 0)
#months(not supported)
sql_error create table t1_9 using st1 tags(now + 1n, 0)
#years(not supported)
sql_error create table t1_10 using st1 tags(now + 1y, 0)
sql create stable st2 (ts timestamp , c1 int) tags(t1 timestamp, t2 int)
sql create table t2_0 using st2 tags(now, 0)
#nanoseconds
sql create table t2_1 using st2 tags(now - 1b, 0)
#microseconds
sql create table t2_2 using st2 tags(now - 1u, 0)
#milliseconds
sql create table t2_3 using st2 tags(now - 1a, 0)
#seconds
sql create table t2_4 using st2 tags(now - 1s, 0)
#minutes
sql create table t2_5 using st2 tags(now - 1m, 0)
#hours
sql create table t2_6 using st2 tags(now - 1h, 0)
#days
sql create table t2_7 using st2 tags(now - 1d, 0)
#weeks
sql create table t2_8 using st2 tags(now - 1w, 0)
#months(not supported)
sql_error create table t2_9 using st2 tags(now - 1n, 0)
#years(not supported)
sql_error create table t2_10 using st2 tags(now - 1y, 0)
sql insert into t1_0 values (now, 0)
sql insert into t1_1 values (now, 1)
sql insert into t1_2 values (now, 2)
sql insert into t1_3 values (now, 3)
sql insert into t1_4 values (now, 4)
sql insert into t1_5 values (now, 5)
sql insert into t1_6 values (now, 6)
sql insert into t1_7 values (now, 7)
sql insert into t1_8 values (now, 8)
sql insert into t2_0 values (now, 0)
sql insert into t2_1 values (now, 1)
sql insert into t2_2 values (now, 2)
sql insert into t2_3 values (now, 3)
sql insert into t2_4 values (now, 4)
sql insert into t2_5 values (now, 5)
sql insert into t2_6 values (now, 6)
sql insert into t2_7 values (now, 7)
sql insert into t2_8 values (now, 8)
sql select * from st1
if $rows != 9 then
return -1
endi
sql select * from st2
if $rows != 9 then
return -1
endi
sql create stable st3 (ts timestamp , c1 int) tags (t1 timestamp, t2 timestamp, t3 timestamp, t4 timestamp, t5 timestamp, t6 timestamp, t7 timestamp, t8 timestamp, t9 timestamp)
sql create table t3 using st3 tags(now, now + 1b, now + 1u, now + 1a, now + 1s, now + 1m, now + 1h, now + 1d, now + 1w)
sql insert into t3 values (now, 1)
sql select * from st3
if $rows != 1 then
return -1
endi
sql create stable st4 (ts timestamp , c1 int) tags (t1 timestamp, t2 timestamp, t3 timestamp, t4 timestamp, t5 timestamp, t6 timestamp, t7 timestamp, t8 timestamp, t9 timestamp)
sql create table t4 using st4 tags(now, now - 1b, now - 1u, now - 1a, now - 1s, now - 1m, now - 1h, now - 1d, now - 1w)
sql insert into t4 values (now, 1)
sql select * from st4
if $rows != 1 then
return -1
endi
system sh/exec.sh -n dnode1 -s stop -x SIGINT
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册