提交 55a26990 编写于 作者: D dapan1121

feat: support case when cases

上级 05a39977
......@@ -131,7 +131,7 @@ static int32_t valueNodeCopy(const SValueNode* pSrc, SValueNode* pDst) {
COPY_SCALAR_FIELD(placeholderNo);
COPY_SCALAR_FIELD(typeData);
COPY_SCALAR_FIELD(unit);
if (!pSrc->translate) {
if (!pSrc->translate || pSrc->isNull) {
return TSDB_CODE_SUCCESS;
}
switch (pSrc->node.resType.type) {
......
......@@ -3011,7 +3011,7 @@ static int32_t valueNodeToJson(const void* pObj, SJson* pJson) {
if (TSDB_CODE_SUCCESS == code) {
code = tjsonAddBoolToObject(pJson, jkValueIsNull, pNode->isNull);
}
if (TSDB_CODE_SUCCESS == code && pNode->translate) {
if (TSDB_CODE_SUCCESS == code && pNode->translate && !pNode->isNull) {
code = datumToJson(pNode, pJson);
}
......@@ -3161,7 +3161,7 @@ static int32_t jsonToValueNode(const SJson* pJson, void* pObj) {
if (TSDB_CODE_SUCCESS == code) {
code = tjsonGetBoolValue(pJson, jkValueIsNull, &pNode->isNull);
}
if (TSDB_CODE_SUCCESS == code && pNode->translate) {
if (TSDB_CODE_SUCCESS == code && pNode->translate && !pNode->isNull) {
code = jsonToDatum(pJson, pNode);
}
......
......@@ -1604,7 +1604,7 @@ char* nodesGetStrValueFromNode(SValueNode* pNode) {
bool nodesIsExprNode(const SNode* pNode) {
ENodeType type = nodeType(pNode);
return (QUERY_NODE_COLUMN == type || QUERY_NODE_VALUE == type || QUERY_NODE_OPERATOR == type ||
QUERY_NODE_FUNCTION == type || QUERY_NODE_LOGIC_CONDITION == type);
QUERY_NODE_FUNCTION == type || QUERY_NODE_LOGIC_CONDITION == type || QUERY_NODE_CASE_WHEN == type);
}
bool nodesIsUnaryOp(const SOperatorNode* pOp) {
......
......@@ -1879,6 +1879,7 @@ static EDealRes translateCaseWhen(STranslateContext* pCxt, SCaseWhenNode* pCaseW
pWhenThen->pWhen = pIsTrue;
}
if (first) {
first = false;
pCaseWhen->node.resType = ((SExprNode*)pNode)->resType;
} else if (!dataTypeEqual(&pCaseWhen->node.resType, &((SExprNode*)pNode)->resType)) {
SNode* pCastFunc = NULL;
......
......@@ -66,7 +66,8 @@ static EDealRes doRewriteExpr(SNode** pNode, void* pContext) {
switch (nodeType(*pNode)) {
case QUERY_NODE_OPERATOR:
case QUERY_NODE_LOGIC_CONDITION:
case QUERY_NODE_FUNCTION: {
case QUERY_NODE_FUNCTION:
case QUERY_NODE_CASE_WHEN: {
SRewriteExprCxt* pCxt = (SRewriteExprCxt*)pContext;
SNode* pExpr;
int32_t index = 0;
......
......@@ -760,7 +760,8 @@ static EDealRes doRewritePrecalcExprs(SNode** pNode, void* pContext) {
return collectAndRewrite(pCxt, pNode);
}
case QUERY_NODE_OPERATOR:
case QUERY_NODE_LOGIC_CONDITION: {
case QUERY_NODE_LOGIC_CONDITION:
case QUERY_NODE_CASE_WHEN: {
return collectAndRewrite(pCxt, pNode);
}
case QUERY_NODE_FUNCTION: {
......
......@@ -53,7 +53,8 @@ static EDealRes doCreateColumn(SNode* pNode, void* pContext) {
}
case QUERY_NODE_OPERATOR:
case QUERY_NODE_LOGIC_CONDITION:
case QUERY_NODE_FUNCTION: {
case QUERY_NODE_FUNCTION:
case QUERY_NODE_CASE_WHEN: {
SExprNode* pExpr = (SExprNode*)pNode;
SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
if (NULL == pCol) {
......
......@@ -612,7 +612,7 @@ int32_t sclWalkCaseWhenList(SScalarCtx *ctx, SNodeList* pList, struct SListCell*
colDataAppend(output->columnData, rowIdx, NULL, true);
if (0 == rowIdx && totalRows > 1) {
if (0 == rowIdx && 1 == pCase->numOfRows && totalRows > 1) {
SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
*complete = true;
}
......@@ -625,7 +625,8 @@ _return:
SCL_RET(code);
}
int32_t sclWalkWhenList(SScalarCtx *ctx, SNodeList* pList, struct SListCell* pCell, SScalarParam *pElse, SScalarParam *output, int32_t rowIdx, int32_t totalRows, bool *complete) {
int32_t sclWalkWhenList(SScalarCtx *ctx, SNodeList* pList, struct SListCell* pCell, SScalarParam *pElse, SScalarParam *output,
int32_t rowIdx, int32_t totalRows, bool *complete, bool preSingle) {
SNode *node = NULL;
SWhenThenNode* pWhenThen = NULL;
SScalarParam *pWhen = NULL;
......@@ -645,7 +646,7 @@ int32_t sclWalkWhenList(SScalarCtx *ctx, SNodeList* pList, struct SListCell* pCe
if (*whenValue) {
colDataAppend(output->columnData, rowIdx, colDataGetData(pThen->columnData, (pThen->numOfRows > 1 ? rowIdx : 0)), colDataIsNull_s(pThen->columnData, (pThen->numOfRows > 1 ? rowIdx : 0)));
if (0 == rowIdx && 1 == pWhen->numOfRows && 1 == pThen->numOfRows && totalRows > 1) {
if (preSingle && 0 == rowIdx && 1 == pWhen->numOfRows && 1 == pThen->numOfRows && totalRows > 1) {
SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
*complete = true;
}
......@@ -660,7 +661,7 @@ int32_t sclWalkWhenList(SScalarCtx *ctx, SNodeList* pList, struct SListCell* pCe
if (pElse) {
colDataAppend(output->columnData, rowIdx, colDataGetData(pElse->columnData, (pElse->numOfRows > 1 ? rowIdx : 0)), colDataIsNull_s(pElse->columnData, (pElse->numOfRows > 1 ? rowIdx : 0)));
if (0 == rowIdx && 1 == pElse->numOfRows && totalRows > 1) {
if (preSingle && 0 == rowIdx && 1 == pElse->numOfRows && totalRows > 1) {
SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
*complete = true;
}
......@@ -670,7 +671,7 @@ int32_t sclWalkWhenList(SScalarCtx *ctx, SNodeList* pList, struct SListCell* pCe
colDataAppend(output->columnData, rowIdx, NULL, true);
if (0 == rowIdx && totalRows > 1) {
if (preSingle && 0 == rowIdx && totalRows > 1) {
SCL_ERR_JRET(sclExtendResRows(output, output, ctx->pBlockList));
*complete = true;
}
......@@ -905,7 +906,7 @@ int32_t sclExecCaseWhen(SCaseWhenNode *node, SScalarCtx *ctx, SScalarParam *outp
break;
}
} else {
SCL_ERR_JRET(sclWalkWhenList(ctx, node->pWhenThenList, node->pWhenThenList->pHead->pNext, pElse, output, i, rowNum, &complete));
SCL_ERR_JRET(sclWalkWhenList(ctx, node->pWhenThenList, node->pWhenThenList->pHead->pNext, pElse, output, i, rowNum, &complete, (pWhen->numOfRows == 1 && pThen->numOfRows == 1)));
if (complete) {
break;
}
......
system sh/stop_dnodes.sh
system sh/deploy.sh -n dnode1 -i 1
system sh/exec.sh -n dnode1 -s start
sql connect
print ======== step1
sql drop database if exists db1;
sql create database db1 vgroups 5;
sql use db1;
sql create stable sta (ts timestamp, f1 int, f2 binary(10), f3 bool) tags(t1 int, t2 bool, t3 binary(10));
sql create table tba1 using sta tags(0, false, '0');
sql create table tba2 using sta tags(1, true, '1');
sql create table tba3 using sta tags(null, null, '');
sql create table tba4 using sta tags(1, false, null);
sql create table tba5 using sta tags(3, true, 'aa');
sql insert into tba1 values ('2022-09-26 15:15:01', 0, "a", false);
sql insert into tba1 values ('2022-09-26 15:15:02', 1, "0", true);
sql insert into tba1 values ('2022-09-26 15:15:03', 5, "5", false);
sql insert into tba1 values ('2022-09-26 15:15:04', null, null, null);
sql insert into tba2 values ('2022-09-27 15:15:01', 0, "a", false);
sql insert into tba2 values ('2022-09-27 15:15:02', 1, "0", true);
sql insert into tba2 values ('2022-09-27 15:15:03', 5, "5", false);
sql insert into tba2 values ('2022-09-27 15:15:04', null, null, null);
sql insert into tba3 values ('2022-09-28 15:15:01', 0, "a", false);
sql insert into tba3 values ('2022-09-28 15:15:02', 1, "0", true);
sql insert into tba3 values ('2022-09-28 15:15:03', 5, "5", false);
sql insert into tba3 values ('2022-09-28 15:15:04', null, null, null);
sql insert into tba4 values ('2022-09-29 15:15:01', 0, "a", false);
sql insert into tba4 values ('2022-09-29 15:15:02', 1, "0", true);
sql insert into tba4 values ('2022-09-29 15:15:03', 5, "5", false);
sql insert into tba4 values ('2022-09-29 15:15:04', null, null, null);
sql insert into tba5 values ('2022-09-30 15:15:01', 0, "a", false);
sql insert into tba5 values ('2022-09-30 15:15:02', 1, "0", true);
sql insert into tba5 values ('2022-09-30 15:15:03', 5, "5", false);
sql insert into tba5 values ('2022-09-30 15:15:04', null, null, null);
sql select case when 3 then 4 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 4 then
return -1
endi
if $data10 != 4 then
return -1
endi
if $data20 != 4 then
return -1
endi
if $data30 != 4 then
return -1
endi
sql select case when 0 then 4 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != NULL then
return -1
endi
if $data10 != NULL then
return -1
endi
if $data20 != NULL then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when null then 4 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != NULL then
return -1
endi
if $data10 != NULL then
return -1
endi
if $data20 != NULL then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when 1 then 4+1 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 5.000000000 then
return -1
endi
if $data10 != 5.000000000 then
return -1
endi
if $data20 != 5.000000000 then
return -1
endi
if $data30 != 5.000000000 then
return -1
endi
sql select case when 1-1 then 0 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != NULL then
return -1
endi
if $data10 != NULL then
return -1
endi
if $data20 != NULL then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when 1+1 then 0 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 0 then
return -1
endi
if $data10 != 0 then
return -1
endi
if $data20 != 0 then
return -1
endi
if $data30 != 0 then
return -1
endi
sql select case when 1 then 1-1+2 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 2.000000000 then
return -1
endi
if $data10 != 2.000000000 then
return -1
endi
if $data20 != 2.000000000 then
return -1
endi
if $data30 != 2.000000000 then
return -1
endi
sql select case when 1 > 0 then 1 < 2 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 1 then
return -1
endi
if $data10 != 1 then
return -1
endi
if $data20 != 1 then
return -1
endi
if $data30 != 1 then
return -1
endi
sql select case when 1 > 2 then 1 < 2 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != NULL then
return -1
endi
if $data10 != NULL then
return -1
endi
if $data20 != NULL then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when abs(3) then abs(-1) end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 1 then
return -1
endi
if $data10 != 1 then
return -1
endi
if $data20 != 1 then
return -1
endi
if $data30 != 1 then
return -1
endi
sql select case when abs(1+1) then abs(-1)+abs(3) end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 4.000000000 then
return -1
endi
if $data10 != 4.000000000 then
return -1
endi
if $data20 != 4.000000000 then
return -1
endi
if $data30 != 4.000000000 then
return -1
endi
sql select case when 0 then 1 else 3 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 3 then
return -1
endi
if $data10 != 3 then
return -1
endi
if $data20 != 3 then
return -1
endi
if $data30 != 3 then
return -1
endi
sql select case when 0 then 1 when 1 then 0 else 3 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 0 then
return -1
endi
if $data10 != 0 then
return -1
endi
if $data20 != 0 then
return -1
endi
if $data30 != 0 then
return -1
endi
sql select case when 0 then 1 when 1 then 0 when 2 then 3 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 0 then
return -1
endi
if $data10 != 0 then
return -1
endi
if $data20 != 0 then
return -1
endi
if $data30 != 0 then
return -1
endi
sql select case when 'a' then 'b' when null then 0 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != NULL then
return -1
endi
if $data10 != NULL then
return -1
endi
if $data20 != NULL then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when '2' then 'b' when null then 0 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != b then
return -1
endi
if $data10 != b then
return -1
endi
if $data20 != b then
return -1
endi
if $data30 != b then
return -1
endi
sql select case when 0 then 'b' else null end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != NULL then
return -1
endi
if $data10 != NULL then
return -1
endi
if $data20 != NULL then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when 0 then 'b' else 2 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 2 then
return -1
endi
if $data10 != 2 then
return -1
endi
if $data20 != 2 then
return -1
endi
if $data30 != 2 then
return -1
endi
sql select case when sum(2) then sum(2)-sum(1) end from tba1;
if $rows != 1 then
return -1
endi
if $data00 != 4.000000000 then
return -1
endi
sql select case when sum(2) then abs(-2) end from tba1;
if $rows != 1 then
return -1
endi
if $data00 != 2 then
return -1
endi
sql select case when ts then ts end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != @22-09-26 15:15:01.000@ then
return -1
endi
if $data10 != @22-09-26 15:15:02.000@ then
return -1
endi
if $data20 != @22-09-26 15:15:03.000@ then
return -1
endi
if $data30 != @22-09-26 15:15:04.000@ then
return -1
endi
sql select case when f1 then ts end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != NULL then
return -1
endi
if $data10 != @22-09-26 15:15:02.000@ then
return -1
endi
if $data20 != @22-09-26 15:15:03.000@ then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when f1 then f1 when f1 + 1 then f1 + 1 else f1 is null end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 1 then
return -1
endi
if $data10 != 1 then
return -1
endi
if $data20 != 5 then
return -1
endi
if $data30 != 1 then
return -1
endi
sql select case when f1 then 3 when ts then ts end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 1664176501000 then
return -1
endi
if $data10 != 3 then
return -1
endi
if $data20 != 3 then
return -1
endi
if $data30 != 1664176504000 then
return -1
endi
sql select case when 3 then f1 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 0 then
return -1
endi
if $data10 != 1 then
return -1
endi
if $data20 != 5 then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when f1 then 3 when 1 then 2 end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != 2 then
return -1
endi
if $data10 != 3 then
return -1
endi
if $data20 != 3 then
return -1
endi
if $data30 != 2 then
return -1
endi
sql select case when sum(f1) then sum(f1)-abs(-1) end from tba1;
if $rows != 1 then
return -1
endi
if $data00 != 5.000000000 then
return -1
endi
sql select case when sum(f1) then sum(f1)-abs(f1) end from tba1;
if $rows != 4 then
return -1
endi
if $data00 != NULL then
return -1
endi
if $data10 != 0.000000000 then
return -1
endi
if $data20 != 0.000000000 then
return -1
endi
if $data30 != NULL then
return -1
endi
sql select case when f1 then sum(f1) when f1 is not null then 9 else 8 end from tba1 group by f1 order by f1;
if $rows != 4 then
return -1
endi
if $data00 != 8 then
return -1
endi
if $data10 != 9 then
return -1
endi
if $data20 != 1 then
return -1
endi
if $data30 != 5 then
return -1
endi
sql select f1 from tba1 where f1 > case when f1 then 0 else 3 end;
if $rows != 2 then
return -1
endi
if $data00 != 1 then
return -1
endi
if $data10 != 5 then
return -1
endi
sql select f1 from tba1 where ts > case when ts then ts end;
if $rows != 0 then
return -1
endi
sql select sum(f1),count(f1) from tba1 partition by case when f1 then f1 when 1 then 1 end;
if $rows != 2 then
return -1
endi
if $data00 != 1 then
return -1
endi
if $data01 != 2 then
return -1
endi
if $data10 != 5 then
return -1
endi
if $data11 != 1 then
return -1
endi
sql select case when f1 < 3 then 1 when f1 >= 3 then 2 else 3 end caseWhen, sum(f1),count(f1) from tba1 group by case when f1 < 3 then 1 when f1 >= 3 then 2 else 3 end
order by caseWhen;
if $rows != 3 then
return -1
endi
if $data00 != 1 then
return -1
endi
if $data01 != 1 then
return -1
endi
if $data02 != 2 then
return -1
endi
if $data10 != 2 then
return -1
endi
if $data11 != 5 then
return -1
endi
if $data12 != 1 then
return -1
endi
if $data20 != 3 then
return -1
endi
if $data21 != NULL then
return -1
endi
if $data22 != 0 then
return -1
endi
sql select f1 from tba1 order by case when f1 <= 0 then 3 when f1 = 1 then 4 when f1 >= 3 then 2 else 1 end desc;
if $rows != 4 then
return -1
endi
if $data00 != 1 then
return -1
endi
if $data10 != 0 then
return -1
endi
if $data20 != 5 then
return -1
endi
if $data30 != NULL then
return -1
endi
sql_error select case when sum(f1) then sum(f1)-abs(f1) end from tba1;
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.
先完成此消息的编辑!
想要评论请 注册