Merge pull request #11516 from taosdata/feature/TD-14243
feat(query): support now()/today() function in select clause
This commit is contained in:
commit
4a47b1a37b
|
@ -181,9 +181,9 @@
|
|||
#define TK_NULL 163
|
||||
#define TK_FIRST 164
|
||||
#define TK_LAST 165
|
||||
#define TK_CAST 166
|
||||
#define TK_NOW 167
|
||||
#define TK_TODAY 168
|
||||
#define TK_NOW 166
|
||||
#define TK_TODAY 167
|
||||
#define TK_CAST 168
|
||||
#define TK_ROWTS 169
|
||||
#define TK_TBNAME 170
|
||||
#define TK_QSTARTTS 171
|
||||
|
|
|
@ -78,6 +78,8 @@ int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam *
|
|||
int32_t toUnixtimestampFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput);
|
||||
int32_t timeTruncateFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput);
|
||||
int32_t timeDiffFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput);
|
||||
int32_t nowFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput);
|
||||
int32_t todayFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput);
|
||||
|
||||
bool getTimePseudoFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv);
|
||||
|
||||
|
|
|
@ -782,6 +782,26 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
|
|||
.sprocessFunc = timeDiffFunction,
|
||||
.finalizeFunc = NULL
|
||||
},
|
||||
{
|
||||
.name = "now",
|
||||
.type = FUNCTION_TYPE_NOW,
|
||||
.classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_DATETIME_FUNC,
|
||||
.translateFunc = translateTimePseudoColumn,
|
||||
.getEnvFunc = NULL,
|
||||
.initFunc = NULL,
|
||||
.sprocessFunc = nowFunction,
|
||||
.finalizeFunc = NULL
|
||||
},
|
||||
{
|
||||
.name = "today",
|
||||
.type = FUNCTION_TYPE_TODAY,
|
||||
.classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_DATETIME_FUNC,
|
||||
.translateFunc = translateTimePseudoColumn,
|
||||
.getEnvFunc = NULL,
|
||||
.initFunc = NULL,
|
||||
.sprocessFunc = todayFunction,
|
||||
.finalizeFunc = NULL
|
||||
},
|
||||
{
|
||||
.name = "_rowts",
|
||||
.type = FUNCTION_TYPE_ROWTS,
|
||||
|
@ -851,16 +871,6 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
|
|||
.initFunc = NULL,
|
||||
.sprocessFunc = winDurFunction,
|
||||
.finalizeFunc = NULL
|
||||
},
|
||||
{
|
||||
.name = "now",
|
||||
.type = FUNCTION_TYPE_NOW,
|
||||
.classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_DATETIME_FUNC,
|
||||
.translateFunc = translateTimePseudoColumn,
|
||||
.getEnvFunc = getTimePseudoFuncEnv,
|
||||
.initFunc = NULL,
|
||||
.sprocessFunc = winDurFunction,
|
||||
.finalizeFunc = NULL
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -92,6 +92,7 @@ SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pL
|
|||
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight);
|
||||
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight);
|
||||
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList);
|
||||
SNode* createFunctionNodeNoParam(SAstCreateContext* pCxt, const SToken* pFuncName);
|
||||
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt);
|
||||
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList);
|
||||
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2);
|
||||
|
|
|
@ -503,6 +503,8 @@ column_name(A) ::= NK_ID(B).
|
|||
function_name(A) ::= NK_ID(B). { A = B; }
|
||||
function_name(A) ::= FIRST(B). { A = B; }
|
||||
function_name(A) ::= LAST(B). { A = B; }
|
||||
function_name(A) ::= NOW(B). { A = B; }
|
||||
function_name(A) ::= TODAY(B). { A = B; }
|
||||
|
||||
%type table_alias { SToken }
|
||||
%destructor table_alias { }
|
||||
|
@ -535,6 +537,7 @@ expression(A) ::= pseudo_column(B).
|
|||
expression(A) ::= column_reference(B). { A = B; }
|
||||
expression(A) ::= function_name(B) NK_LP expression_list(C) NK_RP(D). { A = createRawExprNodeExt(pCxt, &B, &D, createFunctionNode(pCxt, &B, C)); }
|
||||
expression(A) ::= function_name(B) NK_LP NK_STAR(C) NK_RP(D). { A = createRawExprNodeExt(pCxt, &B, &D, createFunctionNode(pCxt, &B, createNodeList(pCxt, createColumnNode(pCxt, NULL, &C)))); }
|
||||
expression(A) ::= function_name(B) NK_LP NK_RP(D). { A = createRawExprNodeExt(pCxt, &B, &D, createFunctionNodeNoParam(pCxt, &B)); }
|
||||
expression(A) ::= CAST(B) NK_LP expression(C) AS type_name(D) NK_RP(E). { A = createRawExprNodeExt(pCxt, &B, &E, createCastFunctionNode(pCxt, releaseRawExprNode(pCxt, C), D)); }
|
||||
//expression(A) ::= case_expression(B). { A = B; }
|
||||
expression(A) ::= subquery(B). { A = B; }
|
||||
|
@ -581,8 +584,8 @@ expression_list(A) ::= expression_list(B) NK_COMMA expression(C).
|
|||
column_reference(A) ::= column_name(B). { A = createRawExprNode(pCxt, &B, createColumnNode(pCxt, NULL, &B)); }
|
||||
column_reference(A) ::= table_name(B) NK_DOT column_name(C). { A = createRawExprNodeExt(pCxt, &B, &C, createColumnNode(pCxt, &B, &C)); }
|
||||
|
||||
pseudo_column(A) ::= NOW(B). { A = createRawExprNode(pCxt, &B, createFunctionNode(pCxt, &B, NULL)); }
|
||||
pseudo_column(A) ::= TODAY(B). { A = createRawExprNode(pCxt, &B, createFunctionNode(pCxt, &B, NULL)); }
|
||||
//pseudo_column(A) ::= NOW(B). { A = createRawExprNode(pCxt, &B, createFunctionNode(pCxt, &B, NULL)); }
|
||||
//pseudo_column(A) ::= TODAY(B). { A = createRawExprNode(pCxt, &B, createFunctionNode(pCxt, &B, NULL)); }
|
||||
pseudo_column(A) ::= ROWTS(B). { A = createRawExprNode(pCxt, &B, createFunctionNode(pCxt, &B, NULL)); }
|
||||
pseudo_column(A) ::= TBNAME(B). { A = createRawExprNode(pCxt, &B, createFunctionNode(pCxt, &B, NULL)); }
|
||||
pseudo_column(A) ::= QSTARTTS(B). { A = createRawExprNode(pCxt, &B, createFunctionNode(pCxt, &B, NULL)); }
|
||||
|
|
|
@ -360,6 +360,39 @@ SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNod
|
|||
return (SNode*)func;
|
||||
}
|
||||
|
||||
SNode* createFunctionNodeNoParam(SAstCreateContext* pCxt, const SToken* pFuncName) {
|
||||
SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
|
||||
CHECK_OUT_OF_MEM(func);
|
||||
char buf[64] = {0};
|
||||
|
||||
int32_t dataType;
|
||||
switch (pFuncName->type) {
|
||||
case TK_NOW: {
|
||||
int64_t ts = taosGetTimestamp(TSDB_TIME_PRECISION_MILLI);
|
||||
snprintf(buf, sizeof(buf), "%"PRId64, ts);
|
||||
dataType = TSDB_DATA_TYPE_BIGINT;
|
||||
break;
|
||||
}
|
||||
case TK_TODAY: {
|
||||
int64_t ts = taosGetTimestampToday(TSDB_TIME_PRECISION_MILLI);
|
||||
snprintf(buf, sizeof(buf), "%"PRId64, ts);
|
||||
dataType = TSDB_DATA_TYPE_BIGINT;
|
||||
break;
|
||||
}
|
||||
//case TK_TIMEZONE: {
|
||||
// strncpy(buf, tsTimezoneStr, strlen(tsTimezoneStr));
|
||||
// dataType = TSDB_DATA_TYPE_BINARY;
|
||||
// break;
|
||||
//}
|
||||
}
|
||||
SToken token = {.type = pFuncName->type, .n = strlen(buf), .z = buf};
|
||||
|
||||
SNodeList *pParameterList = createNodeList(pCxt, createValueNode(pCxt, dataType, &token));
|
||||
strncpy(func->functionName, pFuncName->z, pFuncName->n);
|
||||
func->pParameterList = pParameterList;
|
||||
return (SNode*)func;
|
||||
}
|
||||
|
||||
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
|
||||
SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
|
||||
CHECK_OUT_OF_MEM(func);
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1259,6 +1259,22 @@ int32_t timeDiffFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *p
|
|||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t nowFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
|
||||
if (inputNum != 1) {
|
||||
return TSDB_CODE_FAILED;
|
||||
}
|
||||
colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 0));
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t todayFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
|
||||
if (inputNum != 1) {
|
||||
return TSDB_CODE_FAILED;
|
||||
}
|
||||
colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 0));
|
||||
return TSDB_CODE_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t atanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
|
||||
return doScalarFunctionUnique(pInput, inputNum, pOutput, atan);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue