diff --git a/include/libs/qcom/query.h b/include/libs/qcom/query.h index c95a5f1318..2a7436f404 100644 --- a/include/libs/qcom/query.h +++ b/include/libs/qcom/query.h @@ -319,9 +319,11 @@ extern int32_t (*queryProcessMsgRsp[TDMT_MAX])(void* output, char* msg, int32_t (NO_RET_REDIRECT_ERROR(_code) || SYNC_UNKNOWN_LEADER_REDIRECT_ERROR(_code) || \ SYNC_SELF_LEADER_REDIRECT_ERROR(_code) || SYNC_OTHER_LEADER_REDIRECT_ERROR(_code)) +#define IS_VIEW_REQUEST(_type) ((_type) == TDMT_MND_CREATE_VIEW || (_type) == TDMT_MND_DROP_VIEW) + #define NEED_CLIENT_RM_TBLMETA_REQ(_type) \ ((_type) == TDMT_VND_CREATE_TABLE || (_type) == TDMT_MND_CREATE_STB || (_type) == TDMT_VND_DROP_TABLE || \ - (_type) == TDMT_MND_DROP_STB) + (_type) == TDMT_MND_DROP_STB || (_type) == TDMT_MND_CREATE_VIEW || (_type) == TDMT_MND_DROP_VIEW) #define NEED_SCHEDULER_REDIRECT_ERROR(_code) \ (SYNC_UNKNOWN_LEADER_REDIRECT_ERROR(_code) || SYNC_SELF_LEADER_REDIRECT_ERROR(_code) || \ diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index a3e02a582f..288c4ed18c 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -407,7 +407,7 @@ void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResu int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest); int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList); void doAsyncQuery(SRequestObj* pRequest, bool forceUpdateMeta); -int32_t removeMeta(STscObj* pTscObj, SArray* tbList); +int32_t removeMeta(STscObj* pTscObj, SArray* tbList, bool isView); int32_t handleAlterTbExecRes(void* res, struct SCatalog* pCatalog); int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog); bool qnodeRequired(SRequestObj* pRequest); diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 00780191ac..bc7e0c07de 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -1009,7 +1009,7 @@ void schedulerExecCb(SExecResult* pResult, void* param, int32_t code) { tscDebug("schedulerExecCb request type %s", TMSG_INFO(pRequest->type)); if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) { - removeMeta(pTscObj, pRequest->targetTableList); + removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); } pRequest->metric.execCostUs = taosGetTimestampUs() - pRequest->metric.execStart; @@ -1097,7 +1097,7 @@ SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQue } if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) { - removeMeta(pRequest->pTscObj, pRequest->targetTableList); + removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); } handleQueryExecRsp(pRequest); @@ -1281,7 +1281,7 @@ int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest) { return code; } -int32_t removeMeta(STscObj* pTscObj, SArray* tbList) { +int32_t removeMeta(STscObj* pTscObj, SArray* tbList, bool isView) { SCatalog* pCatalog = NULL; int32_t tbNum = taosArrayGetSize(tbList); int32_t code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &pCatalog); @@ -1289,9 +1289,18 @@ int32_t removeMeta(STscObj* pTscObj, SArray* tbList) { return code; } - for (int32_t i = 0; i < tbNum; ++i) { - SName* pTbName = taosArrayGet(tbList, i); - catalogRemoveTableMeta(pCatalog, pTbName); + if (isView) { + for (int32_t i = 0; i < tbNum; ++i) { + SName* pViewName = taosArrayGet(tbList, i); + char dbFName[TSDB_DB_FNAME_LEN]; + tNameGetFullDbName(pViewName, dbFName); + catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0); + } + } else { + for (int32_t i = 0; i < tbNum; ++i) { + SName* pTbName = taosArrayGet(tbList, i); + catalogRemoveTableMeta(pCatalog, pTbName); + } } return TSDB_CODE_SUCCESS; diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index 8027a61b8f..693efbc364 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -35,7 +35,7 @@ int32_t genericRspCallback(void* param, SDataBuf* pMsg, int32_t code) { setErrno(pRequest, code); if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type)) { - removeMeta(pRequest->pTscObj, pRequest->targetTableList); + removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); } taosMemoryFree(pMsg->pEpSet); diff --git a/source/client/src/clientRawBlockWrite.c b/source/client/src/clientRawBlockWrite.c index e7ba30d78c..63306214af 100644 --- a/source/client/src/clientRawBlockWrite.c +++ b/source/client/src/clientRawBlockWrite.c @@ -993,7 +993,7 @@ static int32_t taosCreateTable(TAOS* taos, void* meta, int32_t metaLen) { launchQueryImpl(pRequest, pQuery, true, NULL); if (pRequest->code == TSDB_CODE_SUCCESS) { - removeMeta(pTscObj, pRequest->tableList); + removeMeta(pTscObj, pRequest->tableList, false); } code = pRequest->code; @@ -1139,7 +1139,7 @@ static int32_t taosDropTable(TAOS* taos, void* meta, int32_t metaLen) { launchQueryImpl(pRequest, pQuery, true, NULL); if (pRequest->code == TSDB_CODE_SUCCESS) { - removeMeta(pTscObj, pRequest->tableList); + removeMeta(pTscObj, pRequest->tableList, false); } code = pRequest->code; diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index d57ae99e1d..a8ccb893ee 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -7455,6 +7455,11 @@ static int32_t translateCreateView(STranslateContext* pCxt, SCreateViewStmt* pSt if (TSDB_CODE_SUCCESS == code) { code = (*pCxt->pParseCxt->parseSqlFp)(pCxt->pParseCxt->parseSqlParam, pStmt->pQuerySql, false, &res); } + if (TSDB_CODE_SUCCESS == code) { + SName name; + toName(pCxt->pParseCxt->acctId, pStmt->dbName, pStmt->viewName, &name); + code = collectUseTable(&name, pCxt->pTargetTables); + } if (TSDB_CODE_SUCCESS == code) { pStmt->createReq.precision = res.schemaRes.precision; pStmt->createReq.numOfCols = res.schemaRes.numOfCols; @@ -7490,6 +7495,13 @@ static int32_t translateDropView(STranslateContext* pCxt, SDropViewStmt* pStmt) return TSDB_CODE_OUT_OF_MEMORY; } dropReq.igNotExists = pStmt->ignoreNotExists; + + toName(pCxt->pParseCxt->acctId, pStmt->dbName, pStmt->viewName, &name); + int32_t code = collectUseTable(&name, pCxt->pTargetTables); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + return buildCmdMsg(pCxt, TDMT_MND_DROP_VIEW, (FSerializeFunc)tSerializeSCMDropViewReq, &dropReq); } diff --git a/tests/script/tsim/view/create_drop_view.sim b/tests/script/tsim/view/create_drop_view.sim new file mode 100644 index 0000000000..5b859f3cda --- /dev/null +++ b/tests/script/tsim/view/create_drop_view.sim @@ -0,0 +1,20 @@ +sql connect +sql use test; + +sql create view view1 as select * from st; +sql drop view view1; + +sql create or replace view view2 as select f from ct1; +sql drop view if exists view2; +sql drop view if exists view3; +sql_error drop view view2; +sql_error drop view view3; + +sql create view view3 as select avg(f) from st2; +sql create or replace view view3 as select f fa from st; +sql_error create view view3 as select * from st2; +sql create view view4 as select * from view3; +sql create or replace view view4 as select fa from view3; +sql drop view view3; +sql_error create view view5 as select * from view3; +sql drop view view4; diff --git a/tests/script/tsim/view/query_view.sim b/tests/script/tsim/view/query_view.sim new file mode 100644 index 0000000000..21cfe35656 --- /dev/null +++ b/tests/script/tsim/view/query_view.sim @@ -0,0 +1,152 @@ +system sh/stop_dnodes.sh +system sh/deploy.sh -n dnode1 -i 1 +system sh/exec.sh -n dnode1 -s start +sql connect +sql drop database if exists test +sql create database test; +sql use test; + +sql create table st(ts timestamp, f int) tags (t int); +sql insert into ct1 using st tags(1) values(now, 1); +sql insert into ct2 using st tags(2) values(now, 2); +sql insert into ct3 using st tags(3) values(now, 3); +sql insert into ct4 using st tags(4) values(now, 4); + +sql create table st2(ts timestamp, f int) tags (t int); +sql insert into ct21 using st2 tags(1) values(now, 1); +sql insert into ct22 using st2 tags(2) values(now, 2); +sql insert into ct23 using st2 tags(3) values(now, 3); +sql insert into ct24 using st2 tags(4) values(now, 4); + +sql create view view1 as select * from st; +sql drop view view1; +sql create or replace view view2 as select f from ct1; +sql drop view if exists view2; +sql drop view if exists view3; +sql_error drop view view2; +sql_error drop view view3; + + +sql select tbname, 1 from st group by tbname order by tbname; +print $rows $data00 $data10 $data20 +if $rows != 4 then + return -1 +endi +if $data00 != @ct1@ then + return -1 +endi +if $data10 != @ct2@ then + return -1 +endi +sql select tbname, 1 from st group by tbname slimit 0, 1; +print $rows +if $rows != 1 then + return -1 +endi +sql select tbname, 1 from st group by tbname slimit 2, 2; +print $rows $data00 $data10 +if $rows != 2 then + return -1 +endi +sql select tbname, 1 from st group by tbname order by tbname slimit 0, 1; +print $rows $data00 $data10 $data20 +if $rows != 4 then + return -1 +endi + +sql create table stt1(ts timestamp, f int) tags (t int, b varchar(10)); +sql insert into ctt11 using stt1 tags(1, '1aa') values(now, 1); +sql insert into ctt12 using stt1 tags(2, '1bb') values(now, 2); +sql insert into ctt13 using stt1 tags(3, '1cc') values(now, 3); +sql insert into ctt14 using stt1 tags(4, '1dd') values(now, 4); +sql insert into ctt14 values(now, 5); + +sql create table stt2(ts timestamp, f int) tags (t int, b varchar(10)); +sql insert into ctt21 using stt2 tags(1, '2aa') values(now, 1); +sql insert into ctt22 using stt2 tags(2, '2bb') values(now, 2); +sql insert into ctt23 using stt2 tags(3, '2cc') values(now, 3); +sql insert into ctt24 using stt2 tags(4, '2dd') values(now, 4); + +sql select tags t, b from stt1 order by t +print $rows +print $data00 $data01 $data10 $data11 $data20 $data21 $data30 $data31 +if $rows != 4 then + return -1 +endi +if $data31 != @1dd@ then + return -1 +endi + +sql select tags t, b from stt2 order by t +print $rows +print $data00 $data01 $data10 $data11 $data20 $data21 $data30 $data31 +if $rows != 4 then + return -1 +endi +if $data31 != @2dd@ then + return -1 +endi + +sql select tags t,b,f from stt1 order by t +print $rows +print $data00 $data01 $data02 $data10 $data11 $data12 $data20 $data21 $data22 $data30 $data31 $data32 $data40 $data41 $data42 +if $rows != 5 then + return -1 +endi +if $data42 != 5 then + return -1 +endi + +sql select tags tbname,t,b from stt1 order by t +print $rows +print $data00 $data01 $data02 $data10 $data11 $data12 $data20 $data21 $data22 $data30 $data31 $data32 +if $rows != 4 then + return -1 +endi +if $data30 != @ctt14@ then + return -1 +endi +if $data32 != @1dd@ then + return -1 +endi + +sql select tags t,b from stt1 where t=1 +print $rows +print $data00 $data01 +if $rows != 1 then + return -1 +endi +if $data00 != @1@ then + return -1 +endi +if $data01 != @1aa@ then + return -1 +endi + +sql select tags t,b from stt1 where tbname='ctt11' +print $rows +print $data00 $data01 +if $rows != 1 then + return -1 +endi +if $data00 != @1@ then + return -1 +endi +if $data01 != @1aa@ then + return -1 +endi + +sql select tags t,b from ctt11 +print $rows +print $data00 $data01 +if $rows != 1 then + return -1 +endi +if $data00 != @1@ then + return -1 +endi +if $data01 != @1aa@ then + return -1 +endi + +system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/script/tsim/view/show_desc_view.sim b/tests/script/tsim/view/show_desc_view.sim new file mode 100644 index 0000000000..329750b136 --- /dev/null +++ b/tests/script/tsim/view/show_desc_view.sim @@ -0,0 +1,39 @@ +system sh/stop_dnodes.sh +system sh/deploy.sh -n dnode1 -i 1 +system sh/exec.sh -n dnode1 -s start +sql connect +sql drop database if exists test +sql create database test; +sql use test; + +sql create table st(ts timestamp, f int) tags (t int); +sql insert into ct1 using st tags(1) values(now, 1); +sql insert into ct2 using st tags(2) values(now, 2); +sql insert into ct3 using st tags(3) values(now, 3); +sql insert into ct4 using st tags(4) values(now, 4); + +sql create table st2(ts timestamp, f int) tags (t int); +sql insert into ct21 using st2 tags(1) values(now, 1); +sql insert into ct22 using st2 tags(2) values(now, 2); +sql insert into ct23 using st2 tags(3) values(now, 3); +sql insert into ct24 using st2 tags(4) values(now, 4); + +sql create view view1 as select * from st; +sql drop view view1; + +sql create or replace view view2 as select f from ct1; +sql drop view if exists view2; +sql drop view if exists view3; +sql_error drop view view2; +sql_error drop view view3; + +sql create view view3 as select avg(f) from st2; +sql create or replace view view3 as select f fa from st; +sql_error create view view3 as select * from st2; +sql create view view4 as select * from view3; +sql create or replace view view4 as select fa from view3; +sql drop view view3; +sql_error create view view5 as select * from view3; +sql drop view view4; + +system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/script/tsim/view/view.sim b/tests/script/tsim/view/view.sim new file mode 100644 index 0000000000..55aeafd571 --- /dev/null +++ b/tests/script/tsim/view/view.sim @@ -0,0 +1,30 @@ +system sh/stop_dnodes.sh +system sh/deploy.sh -n dnode1 -i 1 +system sh/exec.sh -n dnode1 -s start +sql connect +sql drop database if exists test +sql create database test; +sql use test; + +sql create table st(ts timestamp, f int) tags (t int); +sql insert into ct1 using st tags(1) values(now, 1); +sql insert into ct2 using st tags(2) values(now, 2); +sql insert into ct3 using st tags(3) values(now, 3); +sql insert into ct4 using st tags(4) values(now, 4); + +sql create table st2(ts timestamp, f int) tags (t int); +sql insert into ct21 using st2 tags(1) values(now, 1); +sql insert into ct22 using st2 tags(2) values(now, 2); +sql insert into ct23 using st2 tags(3) values(now, 3); +sql insert into ct24 using st2 tags(4) values(now, 4); + +run tsim/view/create_drop_view.sim + +print ================== restart server to commit data into disk +system sh/exec.sh -n dnode1 -s stop -x SIGINT +system sh/exec.sh -n dnode1 -s start +print ================== server restart completed + +run tsim/view/create_drop_view.sim + +system sh/exec.sh -n dnode1 -s stop -x SIGINT