提交 72495356 编写于 作者: D dapan1121

fix udf issues and case issues

上级 f96fbcb6
......@@ -3112,10 +3112,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col
memset(pExpr->base.aliasName, 0, tListLen(pExpr->base.aliasName));
getColumnName(pItem, pExpr->base.aliasName, pExpr->base.token, sizeof(pExpr->base.aliasName) - 1);
SSchema s = {0};
s.type = (uint8_t)resType;
s.bytes = bytes;
s.colId = pExpr->base.colInfo.colId;
SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex);
uint64_t uid = pTableMetaInfo->pTableMeta->id.uid;
SColumnList ids = createColumnList(1, index.tableIndex, index.columnIndex);
......@@ -3123,7 +3120,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col
insertResultField(pQueryInfo, colIndex, &ids, pUdfInfo->resBytes, pUdfInfo->resType, pExpr->base.aliasName, pExpr);
} else {
for (int32_t i = 0; i < ids.num; ++i) {
tscColumnListInsert(pQueryInfo->colList, index.columnIndex, uid, &s);
tscColumnListInsert(pQueryInfo->colList, index.columnIndex, uid, pSchema);
}
}
tscInsertPrimaryTsSourceColumn(pQueryInfo, pTableMetaInfo->pTableMeta->id.uid);
......@@ -4227,7 +4224,11 @@ static int32_t validateSQLExpr(SSqlCmd* pCmd, tSqlExpr* pExpr, SQueryInfo* pQuer
// Append the sqlExpr into exprList of pQueryInfo structure sequentially
pExpr->functionId = isValidFunction(pExpr->Expr.operand.z, pExpr->Expr.operand.n);
if (pExpr->functionId < 0) {
return TSDB_CODE_TSC_INVALID_OPERATION;
SUdfInfo* pUdfInfo = NULL;
pUdfInfo = isValidUdf(pQueryInfo->pUdfInfo, pExpr->Expr.operand.z, pExpr->Expr.operand.n);
if (pUdfInfo == NULL) {
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "invalid function name");
}
}
if (addExprAndResultField(pCmd, pQueryInfo, outputIndex, &item, false, NULL) != TSDB_CODE_SUCCESS) {
......@@ -7298,6 +7299,11 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char*
}
if (f < 0) {
SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, -1 * f - 1);
if (pUdfInfo->funcType == TSDB_UDF_TYPE_SCALAR) {
return invalidOperationMsg(msg, msg1);
}
continue;
}
......@@ -7320,6 +7326,10 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, char*
return invalidOperationMsg(msg, msg1);
}
if (IS_SCALAR_FUNCTION(aAggs[f].status)) {
return invalidOperationMsg(msg, msg1);
}
if (f == TSDB_FUNC_COUNT && pExpr->base.colInfo.colIndex == TSDB_TBNAME_COLUMN_INDEX) {
return invalidOperationMsg(msg, msg1);
}
......@@ -8560,15 +8570,30 @@ int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo) {
info.name = strndup(t->z, t->n);
if (pQueryInfo->pUdfInfo == NULL) {
pQueryInfo->pUdfInfo = taosArrayInit(4, sizeof(struct SUdfInfo));
} else if (taosArrayGetSize(pQueryInfo->pUdfInfo) > 0) {
int32_t usize = (int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo);
int32_t exist = 0;
for (int32_t j = 0; j < usize; ++j) {
SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, j);
int32_t len = strlen(pUdfInfo->name);
if (len == t->n && strncasecmp(info.name, pUdfInfo->name, t->n) == 0) {
exist = 1;
break;
}
}
if (exist) {
continue;
}
}
info.functionId = (int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo) * (-1) - 1;;
taosArrayPush(pQueryInfo->pUdfInfo, &info);
if ((int32_t)taosArrayGetSize(pQueryInfo->pUdfInfo) > 1) {
code = tscInvalidOperationMsg(tscGetErrorMsgPayload(pCmd), "only one udf allowed in one sql", NULL);
if (taosArrayGetSize(pQueryInfo->pUdfInfo) > 1) {
code = tscInvalidOperationMsg(pCmd->payload, "only one udf allowed", NULL);
goto _end;
}
}
}
}
}
......
......@@ -1102,6 +1102,11 @@ int tscBuildQueryMsg(SSqlObj *pSql, SSqlInfo *pInfo) {
// support only one udf
if (pQueryInfo->pUdfInfo != NULL && taosArrayGetSize(pQueryInfo->pUdfInfo) > 0) {
if (taosArrayGetSize(pQueryInfo->pUdfInfo) > 1) {
code = tscInvalidOperationMsg(pCmd->payload, "only one udf allowed", NULL);
goto _end;
}
pQueryMsg->udfContentOffset = htonl((int32_t) (pMsg - pCmd->payload));
for(int32_t i = 0; i < taosArrayGetSize(pQueryInfo->pUdfInfo); ++i) {
SUdfInfo* pUdfInfo = taosArrayGet(pQueryInfo->pUdfInfo, i);
......
......@@ -1279,14 +1279,17 @@ void handleDownstreamOperator(SSqlObj** pSqlObjList, int32_t numOfUpstream, SQue
int32_t functionId = pExprInfo->base.functionId;
if (functionId < 0) {
if (pUdfInfo) {
pSql->res.code = tscInvalidOperationMsg(pSql->cmd.payload, "only one udf allowed", NULL);
return;
}
pUdfInfo = taosArrayGet(px->pUdfInfo, -1 * functionId - 1);
int32_t code = initUdfInfo(pUdfInfo);
if (code != TSDB_CODE_SUCCESS) {
pSql->res.code = code;
return;
}
break;
}
}
......
......@@ -233,6 +233,7 @@ int32_t isValidFunction(const char* name, int32_t len);
#define IS_MULTIOUTPUT(x) (((x)&TSDB_FUNCSTATE_MO) != 0)
#define IS_SINGLEOUTPUT(x) (((x)&TSDB_FUNCSTATE_SO) != 0)
#define IS_OUTER_FORWARD(x) (((x)&TSDB_FUNCSTATE_OF) != 0)
#define IS_SCALAR_FUNCTION(x) (((x)&TSDB_FUNCSTATE_SCALAR) != 0)
// determine the real data need to calculated the result
enum {
......
......@@ -5434,7 +5434,7 @@ SAggFunctionInfo aAggs[] = {{
"ceil",
TSDB_FUNC_CEIL,
TSDB_FUNC_CEIL,
TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
function_setup,
ceil_function,
doFinalizer,
......@@ -5445,7 +5445,7 @@ SAggFunctionInfo aAggs[] = {{
"floor",
TSDB_FUNC_FLOOR,
TSDB_FUNC_FLOOR,
TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
function_setup,
floor_function,
doFinalizer,
......@@ -5456,7 +5456,7 @@ SAggFunctionInfo aAggs[] = {{
"round",
TSDB_FUNC_ROUND,
TSDB_FUNC_ROUND,
TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
TSDB_FUNCSTATE_SO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
function_setup,
round_function,
doFinalizer,
......
......@@ -8202,7 +8202,7 @@ int32_t createQueryFunc(SQueriedTableInfo* pTableInfo, int32_t numOfOutput, SExp
}
int32_t param = (int32_t)pExprs[i].base.param[0].i64;
if (pExprs[i].base.functionId != TSDB_FUNC_ARITHM &&
if (pExprs[i].base.functionId > 0 && pExprs[i].base.functionId != TSDB_FUNC_ARITHM &&
(type != pExprs[i].base.colType || bytes != pExprs[i].base.colBytes)) {
tfree(pExprs);
return TSDB_CODE_QRY_INVALID_MSG;
......
......@@ -210,10 +210,10 @@ class TDTestCase:
tdSql.query("select max(id) + 5 from tb1")
tdSql.query("select max(id) + avg(val) from st")
tdSql.query("select max(id) + avg(val) from tb1")
tdSql.error("select abs_max(number) + 5 from st")
tdSql.error("select abs_max(number) + 5 from tb1")
tdSql.query("select abs_max(number) + 5 from st")
tdSql.query("select abs_max(number) + 5 from tb1")
tdSql.error("select abs_max(number) + max(id) from st")
tdSql.error("select abs_max(number)*abs_max(val) from st")
tdSql.query("select abs_max(number)*abs_max(val) from st")
tdLog.info("======= UDF Nested query test =======")
tdSql.query("select sum(id) from (select id from st)")
......
......@@ -1154,6 +1154,63 @@ if $data61 != 22 then
return -1
endi
sql_error select sum_double(f1),add_one(f1) from tb1 where ts>="2021-03-23 17:00:00.000" and ts<="2021-03-24 20:00:00.000" interval (1h) sliding (30m);
sql select add_one(f1) from (select * from tb1);
if $rows != 7 then
return -1
endi
if $data00 != 2 then
return -1
endi
if $data10 != 3 then
return -1
endi
if $data20 != 4 then
return -1
endi
if $data30 != 5 then
return -1
endi
if $data40 != 6 then
return -1
endi
if $data50 != 7 then
return -1
endi
if $data60 != 8 then
return -1
endi
sql select add_one(ff1) from (select add_one(f1) as ff1 from tb1);
if $rows != 7 then
return -1
endi
if $data00 != 3 then
return -1
endi
if $data10 != 4 then
return -1
endi
if $data20 != 5 then
return -1
endi
if $data30 != 6 then
return -1
endi
if $data40 != 7 then
return -1
endi
if $data50 != 8 then
return -1
endi
if $data60 != 9 then
return -1
endi
sql_error select add_one(f1),sub_one(f1) from tb1;
system sh/exec.sh -n dnode1 -s stop -x SIGINT
......
......@@ -415,4 +415,8 @@ cd ../../../debug; make
./test.sh -f general/parser/last_cache.sim
./test.sh -f unique/big/balance.sim
./test.sh -f general/parser/udf.sim
./test.sh -f general/parser/udf_dll.sim
./test.sh -f general/parser/udf_dll_stable.sim
#======================b7-end===============
......@@ -9,5 +9,6 @@ touch /tmp/normal
gcc -g -O0 -fPIC -shared sh/sum_double.c -o /tmp/sum_double.so
gcc -g -O0 -fPIC -shared sh/add_one.c -o /tmp/add_one.so
gcc -g -O0 -fPIC -shared sh/sub_one.c -o /tmp/sub_one.so
gcc -g -O0 -fPIC -shared sh/demo.c -o /tmp/demo.so
gcc -g -O0 -fPIC -shared sh/abs_max.c -o /tmp/abs_max.so
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct SUdfInit{
int maybe_null; /* 1 if function can return NULL */
int decimals; /* for real functions */
long long length; /* For string functions */
char *ptr; /* free pointer for function data */
int const_item; /* 0 if result is independent of arguments */
} SUdfInit;
void sub_one(char* data, short itype, short ibytes, int numOfRows, long long* ts, char* dataOutput, char* interBUf, char* tsOutput,
int* numOfOutput, short otype, short obytes, SUdfInit* buf) {
int i;
int r = 0;
printf("sub_one input data:%p, type:%d, rows:%d, ts:%p,%lld, dataoutput:%p, tsOutput:%p, numOfOutput:%p, buf:%p\n", data, itype, numOfRows, ts, *ts, dataOutput, tsOutput, numOfOutput, buf);
if (itype == 4) {
for(i=0;i<numOfRows;++i) {
printf("input %d - %d", i, *((int *)data + i));
*((int *)dataOutput+i)=*((int *)data + i) - 1;
printf(", output %d\n", *((int *)dataOutput+i));
if (tsOutput) {
*(long long*)tsOutput=1000000;
}
}
*numOfOutput=numOfRows;
printf("sub_one out, numOfOutput:%d\n", *numOfOutput);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册