• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4988

16 Mar 2026 12:26PM UTC coverage: 75.821% (+1.9%) from 73.883%
#4988

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

274 of 464 new or added lines in 29 files covered. (59.05%)

4404 existing lines in 23 files now uncovered.

337108 of 444611 relevant lines covered (75.82%)

146708292.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.01
/source/libs/parser/src/parAstCreater.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15
#ifndef TD_ASTRA
16
#include <uv.h>
17
#endif
18

19
#include <regex.h>
20
#include <stddef.h>
21
#include <stdio.h>
22
#include <string.h>
23

24
#include "nodes.h"
25
#include "parAst.h"
26
#include "parUtil.h"
27
#include "tglobal.h"
28
#include "ttime.h"
29
#include "cmdnodes.h"
30
#include "osMemory.h"
31
#include "osString.h"
32
#include "parToken.h"
33
#include "tdef.h"
34
#include "tmsg.h"
35
#include "ttokendef.h"
36

37
#define CHECK_MAKE_NODE(p) \
38
  do {                     \
39
    if (NULL == (p)) {     \
40
      goto _err;           \
41
    }                      \
42
  } while (0)
43

44
#define CHECK_OUT_OF_MEM(p)   \
45
  do {                        \
46
    if (NULL == (p)) {        \
47
      pCxt->errCode = terrno; \
48
      goto _err;              \
49
    }                         \
50
  } while (0)
51

52
#define CHECK_PARSER_STATUS(pCxt)             \
53
  do {                                        \
54
    if (TSDB_CODE_SUCCESS != pCxt->errCode) { \
55
      goto _err;                              \
56
    }                                         \
57
  } while (0)
58

59
#define CHECK_NAME(p) \
60
  do {                \
61
    if (!p) {         \
62
      goto _err;      \
63
    }                 \
64
  } while (0)
65

66
#define COPY_STRING_FORM_ID_TOKEN(buf, pToken) strncpy(buf, (pToken)->z, TMIN((pToken)->n, sizeof(buf) - 1))
67
#define TRIM_STRING_FORM_ID_TOKEN(buf, pToken)                      \
68
  do {                                                              \
69
    if (pToken->z[0] == '`') {                                      \
70
      (void)trimString(pToken->z, pToken->n, buf, sizeof(buf) - 1); \
71
    } else {                                                        \
72
      COPY_STRING_FORM_ID_TOKEN(buf, pToken);                       \
73
    }                                                               \
74
  } while (0)
75
#define COPY_STRING_FORM_STR_TOKEN(buf, pToken)                              \
76
  do {                                                                       \
77
    if ((pToken)->n > 2) {                                                   \
78
      strncpy(buf, (pToken)->z + 1, TMIN((pToken)->n - 2, sizeof(buf) - 1)); \
79
    }                                                                        \
80
  } while (0)
81

82
#define COPY_COW_STR_FROM_ID_TOKEN(cow, pToken)                  \
83
  do {                                                           \
84
    if (pToken->z[0] == '`') {                                   \
85
      (cow) = xCreateCowStr((pToken)->n - 2, (pToken)->z + 1, true); \
86
    } else {                                                     \
87
      (cow) = xCreateCowStr((pToken)->n, (pToken)->z, true); \
88
    }                                                            \
89
  } while (0)
90
#define COPY_COW_STR_FROM_STR_TOKEN(cow, pToken)                     \
91
  do {                                                               \
92
    if ((pToken)->n > 2) {                                           \
93
      (cow) = xCreateCowStr((pToken)->n - 2, (pToken)->z + 1, true); \
94
    }                                                                \
95
  } while (0)
96
SToken nil_token = {.type = TK_NK_NIL, .n = 0, .z = NULL};
97

98
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
875,609,468✔
99
  memset(pCxt, 0, sizeof(SAstCreateContext));
875,609,468✔
100
  pCxt->pQueryCxt = pParseCxt;
875,609,468✔
101
  pCxt->msgBuf.buf = pParseCxt->pMsg;
875,572,136✔
102
  pCxt->msgBuf.len = pParseCxt->msgLen;
875,577,648✔
103
  pCxt->notSupport = false;
875,600,608✔
104
  pCxt->pRootNode = NULL;
875,592,668✔
105
  pCxt->placeholderNo = 0;
875,579,412✔
106
  pCxt->pPlaceholderValues = NULL;
875,575,794✔
107
  pCxt->errCode = TSDB_CODE_SUCCESS;
875,579,536✔
108
}
875,576,822✔
109

110
static void trimEscape(SAstCreateContext* pCxt, SToken* pName, bool trimStar) {
2,147,483,647✔
111
  // todo need to deal with `ioo``ii` -> ioo`ii: done
112
  if (NULL != pName && pName->n > 1 && TS_ESCAPE_CHAR == pName->z[0]) {
2,147,483,647✔
113
    if (!pCxt->pQueryCxt->hasDupQuoteChar) {
22,635,502✔
114
      pName->z += 1;
22,423,636✔
115
      pName->n -= 2;
22,423,918✔
116
      // * is forbidden as an identifier name
117
      if (pName->z[0] == '*' && trimStar && pName->n == 1) {
22,423,906✔
118
        pName->z[0] = '\0';
×
119
        pName->n = 0;
×
120
      }
121
    } else {
122
      int32_t i = 1, j = 0;
211,854✔
123
      for (; i < pName->n - 1; ++i) {
1,296,420✔
124
        if ((pName->z[i] == TS_ESCAPE_CHAR) && (pName->z[i + 1] == TS_ESCAPE_CHAR)) {
1,084,566✔
125
          pName->z[j++] = TS_ESCAPE_CHAR;
511,190✔
126
          ++i;
511,190✔
127
        } else {
128
          pName->z[j++] = pName->z[i];
573,376✔
129
        }
130
      }
131
      pName->n = j;
211,854✔
132
    }
133
  }
134
}
2,147,483,647✔
135

136
static bool checkUserName(SAstCreateContext* pCxt, SToken* pUserName) {
1,466,964✔
137
  if (NULL == pUserName) {
1,466,964✔
138
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
139
  } else {
140
    if (pUserName->n >= TSDB_USER_LEN) {
1,466,964✔
141
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
4,024✔
142
    }
143
  }
144
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
1,466,964✔
145
    trimEscape(pCxt, pUserName, true);
1,462,940✔
146
  }
147
  return TSDB_CODE_SUCCESS == pCxt->errCode;
1,466,964✔
148
}
149

150

151

152
static bool isValidPassword(SAstCreateContext* pCxt, const char* password, bool imported) {
272,874✔
153
  if (imported) {
272,874✔
154
    return strlen(password) == TSDB_PASSWORD_LEN;
×
155
  }
156
  // leave actual password validation to server side as client/server may have different
157
  // 'enableStrongPassword' and 'enableAdvancedSecurity' settings, and client-side
158
  // validation may cause confusions.
159
  return true;
272,874✔
160
}
161

162

163

164
static int32_t parsePort(SAstCreateContext* pCxt, const char* p, int32_t* pPort) {
321,070✔
165
  *pPort = taosStr2Int32(p, NULL, 10);
321,070✔
166
  if (*pPort >= UINT16_MAX || *pPort <= 0) {
321,070✔
167
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
×
168
  }
169
  return TSDB_CODE_SUCCESS;
321,070✔
170
}
171

172
static int32_t parseEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
324,950✔
173
  if (pEp->n >= (NULL == pPort ? (TSDB_FQDN_LEN + 1 + 5) : TSDB_FQDN_LEN)) {  // format 'fqdn:port' or 'fqdn'
324,950✔
174
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
175
  }
176

177
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
324,950✔
178
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
324,950✔
179
  (void)strdequote(ep);
324,950✔
180
  (void)strtrim(ep);
324,950✔
181
  if (NULL == pPort) {
324,950✔
182
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
316,068✔
183
    return TSDB_CODE_SUCCESS;
316,068✔
184
  }
185
  char* pColon = strrchr(ep, ':');
8,882✔
186
  if (NULL == pColon) {
8,882✔
187
    *pPort = tsServerPort;
3,880✔
188
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
3,880✔
189
    return TSDB_CODE_SUCCESS;
3,880✔
190
  }
191
  strncpy(pFqdn, ep, pColon - ep);
5,002✔
192
  return parsePort(pCxt, pColon + 1, pPort);
5,002✔
193
}
194

195
static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, const SToken* pPortToken, char* pFqdn,
324,950✔
196
                                  int32_t* pPort) {
197
  if (NULL == pEp) {
324,950✔
198
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
199
    return false;
×
200
  }
201

202
  if (NULL != pPortToken) {
324,950✔
203
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
316,068✔
204
  }
205

206
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
324,950✔
207
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
324,950✔
208
  }
209

210
  return TSDB_CODE_SUCCESS == pCxt->errCode;
324,950✔
211
}
212

213
static bool checkObjName(SAstCreateContext* pCxt, SToken* pObjName, bool need) {
1,760,944✔
214
  if (NULL == pObjName || TK_NK_NIL == pObjName->type) {
1,760,944✔
215
    if (need) {
20,062✔
216
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME);
×
217
    }
218
  } else {
219
    trimEscape(pCxt, pObjName, true);
1,740,882✔
220
    if (pObjName->n >= TSDB_OBJ_NAME_LEN || pObjName->n == 0) {
1,740,882✔
221
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pObjName->z);
×
222
    }
223
  }
224
  return TSDB_CODE_SUCCESS == pCxt->errCode;
1,760,944✔
225
}
226

227
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
1,766,384,656✔
228
  if (NULL == pDbName) {
1,766,384,656✔
229
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
1,228,568,676✔
230
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
9,840✔
231
    }
232
  } else {
233
    trimEscape(pCxt, pDbName, true);
537,815,980✔
234
    if (pDbName->n >= TSDB_DB_NAME_LEN || (demandDb && (pDbName->n == 0))) {
537,872,322✔
235
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
29,128✔
236
    }
237
  }
238
  return TSDB_CODE_SUCCESS == pCxt->errCode;
1,766,427,338✔
239
}
240

241
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
2,147,483,647✔
242
  trimEscape(pCxt, pTableName, true);
2,147,483,647✔
243
  if (NULL != pTableName && pTableName->type != TK_NK_NIL &&
2,147,483,647✔
244
      (pTableName->n >= TSDB_TABLE_NAME_LEN || pTableName->n == 0)) {
2,147,483,647✔
245
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
43,586✔
246
    return false;
8,300✔
247
  }
248
  return true;
2,147,483,647✔
249
}
250

251
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
2,147,483,647✔
252
  trimEscape(pCxt, pColumnName, true);
2,147,483,647✔
253
  if (NULL != pColumnName && pColumnName->type != TK_NK_NIL &&
2,147,483,647✔
254
      (pColumnName->n >= TSDB_COL_NAME_LEN || pColumnName->n == 0)) {
2,147,483,647✔
255
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
2,582✔
256
    return false;
9,088✔
257
  }
258
  return true;
2,147,483,647✔
259
}
260

261
static bool checkIndexName(SAstCreateContext* pCxt, SToken* pIndexName) {
20,860✔
262
  trimEscape(pCxt, pIndexName, true);
20,860✔
263
  if (NULL != pIndexName && (pIndexName->n >= TSDB_INDEX_NAME_LEN || pIndexName->n == 0)) {
20,860✔
264
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pIndexName->z);
×
265
    return false;
×
266
  }
267
  return true;
20,860✔
268
}
269

270
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
611,402✔
271
  trimEscape(pCxt, pTopicName, true);
611,402✔
272
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN || pTopicName->n == 0) {
611,402✔
273
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
1,540✔
274
    return false;
1,540✔
275
  }
276
  return true;
609,862✔
277
}
278

279
static bool checkCGroupName(SAstCreateContext* pCxt, SToken* pCGroup) {
3,270✔
280
  trimEscape(pCxt, pCGroup, true);
3,270✔
281
  if (pCGroup->n >= TSDB_CGROUP_LEN || pCGroup->n == 0) {
3,270✔
282
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pCGroup->z);
×
283
    return false;
×
284
  }
285
  return true;
3,270✔
286
}
287

288
static bool checkViewName(SAstCreateContext* pCxt, SToken* pViewName) {
762,492✔
289
  trimEscape(pCxt, pViewName, true);
762,492✔
290
  if (pViewName->n >= TSDB_VIEW_NAME_LEN || pViewName->n == 0) {
762,492✔
291
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pViewName->z);
1,546✔
292
    return false;
1,546✔
293
  }
294
  return true;
760,946✔
295
}
296

297
static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
855,996✔
298
  trimEscape(pCxt, pStreamName, true);
855,996✔
299
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN || pStreamName->n == 0) {
855,996✔
300
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
404✔
301
    return false;
404✔
302
  }
303
  return true;
855,592✔
304
}
305

306
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
106,276✔
307
  if (NULL == pCommentToken) {
106,276✔
308
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
×
309
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
106,276✔
310
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
17,238✔
311
  }
312
  return TSDB_CODE_SUCCESS == pCxt->errCode;
106,276✔
313
}
314

315
static bool checkRsmaName(SAstCreateContext* pCxt, SToken* pRsmaToken) {
225,612✔
316
  trimEscape(pCxt, pRsmaToken, true);
225,612✔
317
  if (NULL == pRsmaToken) {
225,612✔
318
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
319
  } else if (pRsmaToken->n >= TSDB_TABLE_NAME_LEN) {
225,612✔
320
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
×
321
  } else if (pRsmaToken->n == 0) {
225,612✔
322
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pRsmaToken->z);
×
323
  }
324
  return pCxt->errCode == TSDB_CODE_SUCCESS;
225,612✔
325
}
326

327
static bool checkTsmaName(SAstCreateContext* pCxt, SToken* pTsmaToken) {
14,656✔
328
  trimEscape(pCxt, pTsmaToken, true);
14,656✔
329
  if (NULL == pTsmaToken) {
14,656✔
330
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
331
  } else if (pTsmaToken->n >= TSDB_TABLE_NAME_LEN - strlen(TSMA_RES_STB_POSTFIX)) {
14,656✔
332
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
468✔
333
  } else if (pTsmaToken->n == 0) {
14,188✔
334
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTsmaToken->z);
×
335
  }
336
  return pCxt->errCode == TSDB_CODE_SUCCESS;
14,656✔
337
}
338

339
static bool checkMountPath(SAstCreateContext* pCxt, SToken* pMountPath) {
3,234✔
340
  trimEscape(pCxt, pMountPath, true);
3,234✔
341
  if (pMountPath->n >= TSDB_MOUNT_PATH_LEN || pMountPath->n == 0) {
3,234✔
342
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pMountPath->z);
×
343
    return false;
×
344
  }
345
  return true;
3,234✔
346
}
347

348
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
2,147,483,647✔
349
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
350
  SRawExprNode* target = NULL;
2,147,483,647✔
351
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
2,147,483,647✔
352
  CHECK_MAKE_NODE(target);
2,147,483,647✔
353
  target->p = pToken->z;
2,147,483,647✔
354
  target->n = pToken->n;
2,147,483,647✔
355
  target->pNode = pNode;
2,147,483,647✔
356
  return (SNode*)target;
2,147,483,647✔
357
_err:
11,940✔
358
  nodesDestroyNode(pNode);
11,940✔
359
  return NULL;
11,940✔
360
}
361

362
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
2,147,483,647✔
363
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
364
  SRawExprNode* target = NULL;
2,147,483,647✔
365
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
2,147,483,647✔
366
  CHECK_MAKE_NODE(target);
2,147,483,647✔
367
  target->p = pStart->z;
2,147,483,647✔
368
  target->n = (pEnd->z + pEnd->n) - pStart->z;
2,147,483,647✔
369
  target->pNode = pNode;
2,147,483,647✔
370
  return (SNode*)target;
2,147,483,647✔
371
_err:
×
372
  nodesDestroyNode(pNode);
×
373
  return NULL;
×
374
}
375

376
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
186,211,870✔
377
  CHECK_PARSER_STATUS(pCxt);
186,211,870✔
378
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
186,211,870✔
379
    return pNode;
×
380
  }
381
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
186,215,516✔
382
  return pNode;
186,212,896✔
383
_err:
×
384
  nodesDestroyNode(pNode);
×
385
  return NULL;
×
386
}
387

388
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
2,147,483,647✔
389
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
390
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,147,483,647✔
391
  SNode*        pRealizedExpr = pRawExpr->pNode;
2,147,483,647✔
392
  if (nodesIsExprNode(pRealizedExpr)) {
2,147,483,647✔
393
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
2,147,483,647✔
394
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
2,147,483,647✔
395
      tstrncpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
2,147,483,647✔
396
      tstrncpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
2,147,483,647✔
397
    } else if (pRawExpr->isPseudoColumn) {
2,147,483,647✔
398
      // all pseudo column are translate to function with same name
399
      tstrncpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
163,428,272✔
400
      if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_column") == 0) {
163,425,008✔
401
        SValueNode* pColId = (SValueNode*)nodesListGetNode(((SFunctionNode*)pExpr)->pParameterList, 0);
95,312✔
402
        snprintf(pExpr->userAlias, sizeof(pExpr->userAlias), "%%%%%s", pColId->literal);
95,312✔
403
      } else if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_tbname") == 0) {
163,331,328✔
404
        tstrncpy(pExpr->userAlias, "%%tbname", TSDB_COL_NAME_LEN);
82,168✔
405
      } else {
406
        tstrncpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
163,246,540✔
407
      }
408
    } else {
409
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
2,147,483,647✔
410

411
      // See TS-3398.
412
      // Len of pRawExpr->p could be larger than len of aliasName[TSDB_COL_NAME_LEN].
413
      // If aliasName is truncated, hash value of aliasName could be the same.
414
      uint64_t hashVal = MurmurHash3_64(pRawExpr->p, pRawExpr->n);
2,147,483,647✔
415
      snprintf(pExpr->aliasName, TSDB_COL_NAME_LEN, "%" PRIu64, hashVal);
2,147,483,647✔
416
      strncpy(pExpr->userAlias, pRawExpr->p, len);
2,147,483,647✔
417
      pExpr->userAlias[len] = 0;
2,147,483,647✔
418
    }
419
  }
420
  pRawExpr->pNode = NULL;
2,147,483,647✔
421
  nodesDestroyNode(pNode);
2,147,483,647✔
422
  return pRealizedExpr;
2,147,483,647✔
423
_err:
388✔
424
  nodesDestroyNode(pNode);
388✔
425
  return NULL;
388✔
426
}
427

428
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
2,147,483,647✔
429
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
2,147,483,647✔
430
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1,024✔
431
    return nil_token;
×
432
  }
433
  SRawExprNode* target = (SRawExprNode*)pNode;
2,147,483,647✔
434
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
2,147,483,647✔
435
  return t;
2,147,483,647✔
436
}
437

438
SNodeList* createColsFuncParamNodeList(SAstCreateContext* pCxt, SNode* pNode, SNodeList* pNodeList, SToken* pAlias) {
2,188,672✔
439
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,188,672✔
440
  SNode*        pFuncNode = pRawExpr->pNode;
2,188,672✔
441
  CHECK_PARSER_STATUS(pCxt);
2,188,672✔
442
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
2,188,672✔
443
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
444
  }
445
  CHECK_PARSER_STATUS(pCxt);
2,188,672✔
446
  if (pFuncNode->type != QUERY_NODE_FUNCTION) {
2,188,672✔
447
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
448
  }
449
  CHECK_PARSER_STATUS(pCxt);
2,188,672✔
450
  SNodeList* list = NULL;
2,188,672✔
451
  pCxt->errCode = nodesMakeList(&list);
2,188,672✔
452
  CHECK_MAKE_NODE(list);
2,188,672✔
453
  pCxt->errCode = nodesListAppend(list, pFuncNode);
2,188,672✔
454
  CHECK_PARSER_STATUS(pCxt);
2,188,672✔
455
  pCxt->errCode = nodesListAppendList(list, pNodeList);
2,188,672✔
456
  CHECK_PARSER_STATUS(pCxt);
2,188,672✔
457
  return list;
2,188,672✔
458

459
_err:
×
460
  nodesDestroyNode(pFuncNode);
×
461
  nodesDestroyList(pNodeList);
×
462
  return NULL;
×
463
}
464

465
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
2,147,483,647✔
466
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
467
  SNodeList* list = NULL;
2,147,483,647✔
468
  pCxt->errCode = nodesMakeList(&list);
2,147,483,647✔
469
  CHECK_MAKE_NODE(list);
2,147,483,647✔
470
  pCxt->errCode = nodesListAppend(list, pNode);
2,147,483,647✔
471
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
2,147,483,647✔
472
    nodesDestroyList(list);
×
473
    return NULL;
×
474
  }
475
  return list;
2,147,483,647✔
476
_err:
15,168✔
477
  nodesDestroyNode(pNode);
15,168✔
478
  return NULL;
15,168✔
479
}
480

481
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
2,147,483,647✔
482
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
483
  pCxt->errCode = nodesListAppend(pList, pNode);
2,147,483,647✔
484
  return pList;
2,147,483,647✔
485
_err:
3,566✔
486
  nodesDestroyNode(pNode);
3,566✔
487
  nodesDestroyList(pList);
3,566✔
488
  return NULL;
3,566✔
489
}
490

491
SPrivSetArgs privArgsAdd(SAstCreateContext* pCxt, SPrivSetArgs arg1, SPrivSetArgs arg2) {
29,960✔
492
  CHECK_PARSER_STATUS(pCxt);
29,960✔
493
  if (arg1.nPrivArgs == 0 || arg2.nPrivArgs == 0) {
29,960✔
494
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
495
                                            "Invalid privilege types: Unknown privilege type");
496
    CHECK_PARSER_STATUS(pCxt);
×
497
  }
498

499
  SPrivSetArgs merged = arg1;
29,960✔
500
  merged.nPrivArgs += arg2.nPrivArgs;
29,960✔
501
  if (merged.nPrivArgs > TSDB_PRIV_MAX_INPUT_ARGS) {
29,960✔
502
    pCxt->errCode =
×
503
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
504
                                "Invalid privilege types: exceed max privilege number:%d", TSDB_PRIV_MAX_INPUT_ARGS);
505
    CHECK_PARSER_STATUS(pCxt);
×
506
  }
507
  for (int32_t i = 0; i < PRIV_GROUP_CNT; ++i) {
149,800✔
508
    if (arg2.privSet.set[i]) {
119,840✔
509
      merged.privSet.set[i] |= arg2.privSet.set[i];
28,410✔
510
    }
511
  }
512
  if (merged.selectCols) {
29,960✔
513
    if (arg2.selectCols) {
2,790✔
514
      pCxt->errCode = nodesListAppendList((SNodeList*)merged.selectCols, (SNodeList*)arg2.selectCols);
×
515
      CHECK_PARSER_STATUS(pCxt);
×
516
    }
517
  } else if (arg2.selectCols) {
27,170✔
518
    merged.selectCols = arg2.selectCols;
×
519
  }
520
  if (LIST_LENGTH((SNodeList*)merged.selectCols) > TSDB_MAX_COLUMNS) {
29,960✔
521
    pCxt->errCode =
×
522
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
523
                                "Invalid privilege columns: SELECT exceed max columns number:%d", TSDB_MAX_COLUMNS);
524
    CHECK_PARSER_STATUS(pCxt);
×
525
  }
526
  if (merged.insertCols) {
29,960✔
527
    if (arg2.insertCols) {
1,550✔
528
      pCxt->errCode = nodesListAppendList((SNodeList*)merged.insertCols, (SNodeList*)arg2.insertCols);
×
529
      CHECK_PARSER_STATUS(pCxt);
×
530
    }
531
  } else if (arg2.insertCols) {
28,410✔
532
    merged.insertCols = arg2.insertCols;
1,550✔
533
  }
534
  if (LIST_LENGTH((SNodeList*)merged.insertCols) > TSDB_MAX_COLUMNS) {
29,960✔
535
    pCxt->errCode =
×
536
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
537
                                "Invalid privilege columns: INSERT exceed max columns number:%d", TSDB_MAX_COLUMNS);
538
    CHECK_PARSER_STATUS(pCxt);
×
539
  }
540
  if (merged.updateCols) {
29,960✔
541
    if (arg2.updateCols) {
×
542
      pCxt->errCode = nodesListAppendList((SNodeList*)merged.updateCols, (SNodeList*)arg2.updateCols);
×
543
      CHECK_PARSER_STATUS(pCxt);
×
544
    }
545
  } else if (arg2.updateCols) {
29,960✔
546
    merged.updateCols = arg2.updateCols;
×
547
  }
548
  if (LIST_LENGTH((SNodeList*)merged.updateCols) > TSDB_MAX_COLUMNS) {
29,960✔
549
    pCxt->errCode =
×
550
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
551
                                "Invalid privilege columns: UPDATE exceed max columns number:%d", TSDB_MAX_COLUMNS);
552
    CHECK_PARSER_STATUS(pCxt);
×
553
  }
554
_err:
29,960✔
555
  return merged;
29,960✔
556
}
557

558
SPrivSetArgs privArgsSetType(SAstCreateContext* pCxt, EPrivType type) {
×
559
  CHECK_PARSER_STATUS(pCxt);
×
560
  SPrivSetArgs args = {.nPrivArgs = 1};
×
561
  privAddType(&args.privSet, type);
562
_err:
×
563
  return args;
×
564
}
565

566
SPrivSetArgs privArgsSetCols(SAstCreateContext* pCxt, SNodeList* selectCols, SNodeList* insertCols,
×
567
                             SNodeList* updateCols) {
568
  CHECK_PARSER_STATUS(pCxt);
×
569
  SPrivSetArgs args = {.nPrivArgs = 1, .selectCols = selectCols, .insertCols = insertCols, .updateCols = updateCols};
×
570
_err:
×
571
  return args;
×
572
}
573

574
/**
575
 * @brief set privilege args from tokens as to decrease the definition of keywords
576
 *
577
 * @param pCxt
578
 * @param type 0 alter, 1 read, 2 show, 3 set user
579
 * @param t1
580
 * @param t2
581
 * @return SPrivSetArgs
582
 */
583
SPrivSetArgs privArgsSet(SAstCreateContext* pCxt, int32_t type, SToken* t1, SToken* t2) {
13,200✔
584
  CHECK_PARSER_STATUS(pCxt);
13,200✔
585
  SPrivSetArgs args = {0};
13,200✔
586
  if (!t1) goto _err;
13,200✔
587
  if (type == 0) { // alter
13,200✔
588
    if (t1->n == 4) {
3,432✔
589
      if (taosStrncasecmp(t1->z, TSDB_WORD_SELF, 4) == 0) {
264✔
590
        if (t2 && t2->n == 4 && taosStrncasecmp(t2->z, TSDB_WORD_PASS, 4) == 0) {
264✔
591
          return PRIV_SET_TYPE(PRIV_PASS_ALTER_SELF);
264✔
592
        }
593
      }
594
    } else if (t1->n == 5) {
3,168✔
595
      if (taosStrncasecmp(t1->z, TSDB_WORD_DEBUG, 5) == 0) {
1,584✔
596
        if (t2 && t2->n == 8 && taosStrncasecmp(t2->z, TSDB_WORD_VARIABLE, 8) == 0) {
792✔
597
          return PRIV_SET_TYPE(PRIV_VAR_DEBUG_ALTER);
792✔
598
        }
599
      } else if (taosStrncasecmp(t1->z, TSDB_WORD_AUDIT, 5) == 0) {
792✔
600
        if (t2 && t2->n == 8 && taosStrncasecmp(t2->z, TSDB_WORD_VARIABLE, 8) == 0) {
792✔
601
          return PRIV_SET_TYPE(PRIV_VAR_AUDIT_ALTER);
792✔
602
        }
603
      }
604
    } else if (t1->n == 6) {
1,584✔
605
      if (taosStrncasecmp(t1->z, TSDB_WORD_SYSTEM, 6) == 0) {
792✔
606
        if (t2 && t2->n == 8 && taosStrncasecmp(t2->z, TSDB_WORD_VARIABLE, 8) == 0) {
792✔
607
          return PRIV_SET_TYPE(PRIV_VAR_SYSTEM_ALTER);
792✔
608
        }
609
      }
610
    } else if (t1->n == 8) {
792✔
611
      if (taosStrncasecmp(t1->z, TSDB_WORD_SECURITY, 8) == 0) {
792✔
612
        if (t2 && t2->n == 8 && taosStrncasecmp(t2->z, TSDB_WORD_VARIABLE, 8) == 0)
792✔
613
          return PRIV_SET_TYPE(PRIV_VAR_SECURITY_ALTER);
792✔
614
      }
615
    }
616
  } else if (type == 1) { // read
9,768✔
617
    if (t1->n == 18) {
6,336✔
618
      if (taosStrncasecmp(t1->z, TSDB_INFORMATION_SCHEMA_DB, 18) == 0) {
6,336✔
619
        if (!t2) goto _err;
4,224✔
620
        if (t2->n == 5) {
4,224✔
621
          if (taosStrncasecmp(t2->z, TSDB_WORD_BASIC, 5) == 0) {
1,760✔
622
            return PRIV_SET_TYPE(PRIV_INFO_SCHEMA_READ_BASIC);
1,232✔
623
          } else if (taosStrncasecmp(t2->z, TSDB_WORD_AUDIT, 5) == 0) {
528✔
624
            return PRIV_SET_TYPE(PRIV_INFO_SCHEMA_READ_AUDIT);
528✔
625
          }
626
        } else if ((t2->n == 8) && taosStrncasecmp(t2->z, TSDB_WORD_SECURITY, 8) == 0) {
2,464✔
627
          return PRIV_SET_TYPE(PRIV_INFO_SCHEMA_READ_SEC);
1,232✔
628
        } else if ((t2->n == 10) && taosStrncasecmp(t2->z, TSDB_WORD_PRIVILEGED, 10) == 0) {
1,232✔
629
          return PRIV_SET_TYPE(PRIV_INFO_SCHEMA_READ_PRIVILEGED);
1,232✔
630
        }
631
      } else if (taosStrncasecmp(t1->z, TSDB_PERFORMANCE_SCHEMA_DB, 18) == 0) {
2,112✔
632
        if (!t2) goto _err;
2,112✔
633
        if (t2->n == 5) {
2,112✔
634
          if (taosStrncasecmp(t2->z, TSDB_WORD_BASIC, 5) == 0) {
1,584✔
635
            return PRIV_SET_TYPE(PRIV_PERF_SCHEMA_READ_BASIC);
1,584✔
636
          }
637
        } else if ((t2->n == 10) && taosStrncasecmp(t2->z, TSDB_WORD_PRIVILEGED, 10) == 0) {
528✔
638
          return PRIV_SET_TYPE(PRIV_PERF_SCHEMA_READ_PRIVILEGED);
528✔
639
        }
640
      }
641
    }
642
  } else if (type == 2) { // show
3,432✔
643
    if (t1->n == 5) {
3,168✔
644
      if (taosStrncasecmp(t1->z, TSDB_WORD_DEBUG, 5) == 0) {
1,584✔
645
        if (t2 && t2->n == 9 && taosStrncasecmp(t2->z, TSDB_WORD_VARIABLES, 9) == 0) {
792✔
646
          return PRIV_SET_TYPE(PRIV_VAR_DEBUG_SHOW);
792✔
647
        }
648
      } else if (taosStrncasecmp(t1->z, TSDB_WORD_AUDIT, 5) == 0) {
792✔
649
        if (t2 && t2->n == 9 && taosStrncasecmp(t2->z, TSDB_WORD_VARIABLES, 9) == 0) {
792✔
650
          return PRIV_SET_TYPE(PRIV_VAR_AUDIT_SHOW);
792✔
651
        }
652
      }
653
    } else if (t1->n == 6) {
1,584✔
654
      if (taosStrncasecmp(t1->z, TSDB_WORD_SYSTEM, 6) == 0) {
792✔
655
        if (t2 && t2->n == 9 && taosStrncasecmp(t2->z, TSDB_WORD_VARIABLES, 9) == 0) {
792✔
656
          return PRIV_SET_TYPE(PRIV_VAR_SYSTEM_SHOW);
792✔
657
        }
658
      }
659
    } else if (t1->n == 8) {
792✔
660
      if (taosStrncasecmp(t1->z, TSDB_WORD_SECURITY, 8) == 0) {
792✔
661
        if (t2 && t2->n == 9 && taosStrncasecmp(t2->z, TSDB_WORD_VARIABLES, 9) == 0)
792✔
662
          return PRIV_SET_TYPE(PRIV_VAR_SECURITY_SHOW);
792✔
663
      }
664
    }
665
  } else if (type == 3) { // set user
264✔
666
    if (t1->n == 5) {
×
667
      if (taosStrncasecmp(t1->z, TSDB_WORD_BASIC, 5) == 0) {
×
668
        if (t2 && t2->n == 11 && taosStrncasecmp(t2->z, TSDB_WORD_INFORMATION, 11) == 0) {
×
669
          return PRIV_SET_TYPE(PRIV_USER_SET_BASIC);
×
670
        }
671
      } else if (taosStrncasecmp(t1->z, TSDB_WORD_AUDIT, 5) == 0) {
×
672
        if (t2 && t2->n == 11 && taosStrncasecmp(t2->z, TSDB_WORD_INFORMATION, 11) == 0) {
×
673
          return PRIV_SET_TYPE(PRIV_USER_SET_AUDIT);
×
674
        }
675
      }
676
    } else if ((t1->n == 8) && taosStrncasecmp(t1->z, TSDB_WORD_SECURITY, 8) == 0) {
×
677
      if (t2 && t2->n == 11 && taosStrncasecmp(t2->z, TSDB_WORD_INFORMATION, 11) == 0) {
×
678
        return PRIV_SET_TYPE(PRIV_USER_SET_SECURITY);
×
679
      }
680
    }
681
  } else if (type == 4) { // show users
264✔
682
    if (t1->n == 8) {
264✔
683
      if (taosStrncasecmp(t1->z, TSDB_WORD_SECURITY, 8) == 0) {
264✔
684
        if (t2 && t2->n == 11 && taosStrncasecmp(t2->z, TSDB_WORD_INFORMATION, 11) == 0) {
264✔
685
          return PRIV_SET_TYPE(PRIV_USER_SHOW_SECURITY);
264✔
686
        }
687
      }
688
    }
689
  }
690

691
_err:
×
692
  return args;
×
693
}
694

695
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
2,147,483,647✔
696
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
697
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
2,147,483,647✔
698
    return NULL;
766✔
699
  }
700
  SColumnNode* col = NULL;
2,147,483,647✔
701
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
2,147,483,647✔
702
  CHECK_MAKE_NODE(col);
2,147,483,647✔
703
  if (NULL != pTableAlias) {
2,147,483,647✔
704
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
499,051,456✔
705
  }
706
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
2,147,483,647✔
707
  return (SNode*)col;
2,147,483,647✔
708
_err:
×
709
  return NULL;
×
710
}
711

712
/**
713
 * @param type: 1 with mask; 0 without mask
714
 */
715
SNode* createColumnNodeExt(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName, int8_t type) {
264✔
716
  SNode* result = createColumnNode(pCxt, pTableAlias, pColumnName);
264✔
717
  if (result != NULL) {
264✔
718
    if (type == 1) ((SColumnNode*)result)->hasMask = 1;
264✔
719
  }
720
  return result;
264✔
721
}
722

723
SNode* createPlaceHolderColumnNode(SAstCreateContext* pCxt, SNode* pColId) {
95,312✔
724
  CHECK_PARSER_STATUS(pCxt);
95,312✔
725
  SFunctionNode* pFunc = NULL;
95,312✔
726
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
95,312✔
727
  CHECK_PARSER_STATUS(pCxt);
95,312✔
728
  tstrncpy(pFunc->functionName, "_placeholder_column", TSDB_FUNC_NAME_LEN);
95,312✔
729
  ((SValueNode*)pColId)->notReserved = true;
95,312✔
730
  pCxt->errCode = nodesListMakeAppend(&pFunc->pParameterList, pColId);
95,312✔
731
  CHECK_PARSER_STATUS(pCxt);
95,312✔
732
  pFunc->tz = pCxt->pQueryCxt->timezone;
95,312✔
733
  pFunc->charsetCxt = pCxt->pQueryCxt->charsetCxt;
95,312✔
734
  return (SNode*)pFunc;
95,312✔
735
_err:
×
736
  return NULL;
×
737
}
738

739
static void copyValueTrimEscape(char* buf, int32_t bufLen, const SToken* pToken, bool trim) {
1,007,048,772✔
740
  int32_t len = TMIN(pToken->n, bufLen - 1);
1,007,048,772✔
741
  if (trim && (pToken->z[0] == TS_ESCAPE_CHAR)) {
1,007,047,924✔
742
    int32_t i = 1, j = 0;
57,970✔
743
    for (; i < len - 1; ++i) {
326,740✔
744
      buf[j++] = pToken->z[i];
268,770✔
745
      if (pToken->z[i] == TS_ESCAPE_CHAR) {
268,770✔
746
        if (pToken->z[i + 1] == TS_ESCAPE_CHAR) ++i;
135,966✔
747
      }
748
    }
749
    buf[j] = 0;
57,970✔
750
  } else {
751
    tstrncpy(buf, pToken->z, len + 1);
1,006,989,954✔
752
  }
753
}
1,007,051,828✔
754

755
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
1,007,037,966✔
756
  CHECK_PARSER_STATUS(pCxt);
1,007,037,966✔
757
  SValueNode* val = NULL;
1,007,038,320✔
758
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
1,007,039,210✔
759
  CHECK_MAKE_NODE(val);
1,007,067,170✔
760
  if (!(val->literal = taosMemoryMalloc(pLiteral->n + 1))) {
1,007,067,170✔
761
    pCxt->errCode = terrno;
×
762
    nodesDestroyNode((SNode*)val);
×
763
    return NULL;
×
764
  }
765
  copyValueTrimEscape(val->literal, pLiteral->n + 1, pLiteral,
1,007,056,906✔
766
                      pCxt->pQueryCxt->hasDupQuoteChar && (TK_NK_ID == pLiteral->type));
1,007,054,454✔
767
  if (TK_NK_STRING == pLiteral->type) {
1,007,054,452✔
768
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
124,584,660✔
769
  }
770
  val->node.resType.type = dataType;
1,007,058,186✔
771
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
1,007,054,674✔
772
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
1,007,051,714✔
773
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
29,716✔
774
  }
775
  val->translate = false;
1,007,051,714✔
776
  val->tz = pCxt->pQueryCxt->timezone;
1,007,048,622✔
777
  val->charsetCxt = pCxt->pQueryCxt->charsetCxt;
1,007,051,762✔
778
  return (SNode*)val;
1,007,048,290✔
779
_err:
×
780
  return NULL;
×
781
}
782

783
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
284,494,072✔
784
  CHECK_PARSER_STATUS(pCxt);
284,494,072✔
785
  SValueNode* val = NULL;
284,487,236✔
786
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
284,508,010✔
787
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
284,536,876✔
788
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
789
    goto _exit;
×
790
  }
791
  if (pLiteral) {
284,469,280✔
792
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
283,343,508✔
793
    if (!val->literal) {
283,389,734✔
794
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
795
      goto _exit;
×
796
    }
797
  } else if (pNode) {
1,125,772✔
798
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
1,125,772✔
799
    if (!nodesIsExprNode(pRawExpr->pNode)) {
1,125,772✔
800
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
801
      goto _exit;
×
802
    }
803
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
1,125,772✔
804
    if (!val->literal) {
1,125,772✔
805
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
806
      goto _exit;
×
807
    }
808
  } else {
809
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
810
    goto _exit;
×
811
  }
812
  if (!val->literal) {
284,494,868✔
813
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
814
    goto _exit;
×
815
  }
816

817
  val->node.resType.type = dataType;
284,485,050✔
818
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
284,483,280✔
819
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
284,490,488✔
820
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
821
  }
822
_exit:
284,490,488✔
823
  nodesDestroyNode(pNode);
284,495,596✔
824
  if (pCxt->errCode != 0) {
284,467,548✔
825
    nodesDestroyNode((SNode*)val);
×
826
    return NULL;
×
827
  }
828
  return (SNode*)val;
284,478,912✔
829
_err:
×
830
  nodesDestroyNode(pNode);
×
831
  return NULL;
×
832
}
833

834
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
100,028✔
835
                             SNode* pRight) {
836
  SValueNode* val = NULL;
100,028✔
837
  CHECK_PARSER_STATUS(pCxt);
100,028✔
838

839
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
100,028✔
840
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
100,028✔
841
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
842
    goto _exit;
×
843
  }
844
  if (pLiteral) {
100,028✔
845
    if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) {
100,028✔
846
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
847
      goto _exit;
×
848
    }
849
  } else {
850
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
851
    goto _exit;
×
852
  }
853

854
  val->node.resType.type = dataType;
100,028✔
855
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
100,028✔
856
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
100,028✔
857
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
858
  }
859
_exit:
100,028✔
860
  nodesDestroyNode(pLeft);
100,028✔
861
  nodesDestroyNode(pRight);
100,028✔
862
  CHECK_PARSER_STATUS(pCxt);
100,028✔
863
  return (SNode*)val;
100,028✔
864
_err:
×
865
  nodesDestroyNode((SNode*)val);
×
866
  nodesDestroyNode(pLeft);
×
867
  nodesDestroyNode(pRight);
×
868
  return NULL;
×
869
}
870

871
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
17,073,732✔
872
  if (!pHintList) return false;
17,073,732✔
873
  SNode* pNode;
874
  FOREACH(pNode, pHintList) {
2,760✔
875
    SHintNode* pHint = (SHintNode*)pNode;
2,760✔
876
    if (pHint->option == hint) {
2,760✔
877
      return true;
2,760✔
878
    }
879
  }
880
  return false;
×
881
}
882

883
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
17,081,646✔
884
                       int32_t paramNum) {
885
  void* value = NULL;
17,081,646✔
886
  switch (opt) {
17,081,646✔
887
    case HINT_SKIP_TSMA:
7,914✔
888
    case HINT_BATCH_SCAN:
889
    case HINT_NO_BATCH_SCAN: {
890
      if (paramNum > 0) {
7,914✔
891
        return true;
1,536✔
892
      }
893
      break;
6,378✔
894
    }
895
    case HINT_SORT_FOR_GROUP:
112,240✔
896
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
112,240✔
897
      break;
111,320✔
898
    case HINT_PARTITION_FIRST:
3,680✔
899
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
3,680✔
900
      break;
1,840✔
901
    case HINT_PARA_TABLES_SORT:
16,956,256✔
902
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
16,956,256✔
903
      break;
16,956,256✔
904
    case HINT_SMALLDATA_TS_SORT:
×
905
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
906
      break;
×
907
    case HINT_HASH_JOIN:
1,556✔
908
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
1,556✔
909
      break;
1,556✔
910
    case HINT_WIN_OPTIMIZE_BATCH:
×
911
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_BATCH)) return true;
×
912
      break;
×
913
    case HINT_WIN_OPTIMIZE_SINGLE:
×
914
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_SINGLE)) return true;
×
915
      break;
×
916
    default:
×
917
      return true;
×
918
  }
919

920
  SHintNode* hint = NULL;
17,077,350✔
921
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
17,077,350✔
922
  if (!hint) {
17,077,350✔
923
    return true;
×
924
  }
925
  hint->option = opt;
17,077,350✔
926
  hint->value = value;
17,077,350✔
927

928
  if (NULL == *ppHintList) {
17,077,350✔
929
    pCxt->errCode = nodesMakeList(ppHintList);
17,075,814✔
930
    if (!*ppHintList) {
17,075,814✔
931
      nodesDestroyNode((SNode*)hint);
×
932
      return true;
×
933
    }
934
  }
935

936
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
17,077,350✔
937
  if (pCxt->errCode) {
17,077,350✔
938
    return true;
×
939
  }
940

941
  return false;
17,077,350✔
942
}
943

944
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
1,390,826,372✔
945
  CHECK_PARSER_STATUS(pCxt);
1,390,826,372✔
946
  if (NULL == pLiteral || pLiteral->n <= 5) {
1,390,825,470✔
947
    return NULL;
1,373,747,426✔
948
  }
949
  SNodeList* pHintList = NULL;
17,078,148✔
950
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
17,078,118✔
951
  if (!hint) return NULL;
17,078,118✔
952
  int32_t     i = 0;
17,078,118✔
953
  bool        quit = false;
17,078,118✔
954
  bool        inParamList = false;
17,078,118✔
955
  bool        lastComma = false;
17,078,118✔
956
  EHintOption opt = 0;
17,078,118✔
957
  int32_t     paramNum = 0;
17,078,118✔
958
  SToken      paramList[10];
17,078,118✔
959
  while (!quit) {
102,480,442✔
960
    SToken t0 = {0};
102,475,378✔
961
    if (hint[i] == 0) {
102,475,378✔
962
      break;
17,073,054✔
963
    }
964
    t0.n = tGetToken(&hint[i], &t0.type, NULL);
85,402,324✔
965
    t0.z = hint + i;
85,402,324✔
966
    i += t0.n;
85,402,324✔
967

968
    switch (t0.type) {
85,402,324✔
969
      case TK_BATCH_SCAN:
4,608✔
970
        lastComma = false;
4,608✔
971
        if (0 != opt || inParamList) {
4,608✔
972
          quit = true;
×
973
          break;
×
974
        }
975
        opt = HINT_BATCH_SCAN;
4,608✔
976
        break;
4,608✔
977
      case TK_NO_BATCH_SCAN:
3,072✔
978
        lastComma = false;
3,072✔
979
        if (0 != opt || inParamList) {
3,072✔
980
          quit = true;
×
981
          break;
×
982
        }
983
        opt = HINT_NO_BATCH_SCAN;
3,072✔
984
        break;
3,072✔
985
      case TK_SORT_FOR_GROUP:
112,240✔
986
        lastComma = false;
112,240✔
987
        if (0 != opt || inParamList) {
112,240✔
988
          quit = true;
×
989
          break;
×
990
        }
991
        opt = HINT_SORT_FOR_GROUP;
112,240✔
992
        break;
112,240✔
993
      case TK_PARTITION_FIRST:
3,680✔
994
        lastComma = false;
3,680✔
995
        if (0 != opt || inParamList) {
3,680✔
996
          quit = true;
×
997
          break;
×
998
        }
999
        opt = HINT_PARTITION_FIRST;
3,680✔
1000
        break;
3,680✔
1001
      case TK_PARA_TABLES_SORT:
16,956,256✔
1002
        lastComma = false;
16,956,256✔
1003
        if (0 != opt || inParamList) {
16,956,256✔
1004
          quit = true;
×
1005
          break;
×
1006
        }
1007
        opt = HINT_PARA_TABLES_SORT;
16,956,256✔
1008
        break;
16,956,256✔
1009
      case TK_SMALLDATA_TS_SORT:
×
1010
        lastComma = false;
×
1011
        if (0 != opt || inParamList) {
×
1012
          quit = true;
×
1013
          break;
×
1014
        }
1015
        opt = HINT_SMALLDATA_TS_SORT;
×
1016
        break;
×
1017
      case TK_HASH_JOIN:
1,556✔
1018
        lastComma = false;
1,556✔
1019
        if (0 != opt || inParamList) {
1,556✔
1020
          quit = true;
×
1021
          break;
×
1022
        }
1023
        opt = HINT_HASH_JOIN;
1,556✔
1024
        break;
1,556✔
1025
      case TK_SKIP_TSMA:
234✔
1026
        lastComma = false;
234✔
1027
        if (0 != opt || inParamList) {
234✔
1028
          quit = true;
×
1029
          break;
×
1030
        }
1031
        opt = HINT_SKIP_TSMA;
234✔
1032
        break;
234✔
1033
      case TK_WIN_OPTIMIZE_BATCH:
×
1034
        lastComma = false;
×
1035
        if (0 != opt || inParamList) {
×
1036
          quit = true;
×
1037
          break;
×
1038
        }
1039
        opt = HINT_WIN_OPTIMIZE_BATCH;
×
1040
        break;
×
1041
      case TK_WIN_OPTIMIZE_SINGLE:
×
1042
        lastComma = false;
×
1043
        if (0 != opt || inParamList) {
×
1044
          quit = true;
×
1045
          break;
×
1046
        }
1047
        opt = HINT_WIN_OPTIMIZE_SINGLE;
×
1048
        break;
×
1049
      case TK_NK_LP:
17,081,646✔
1050
        lastComma = false;
17,081,646✔
1051
        if (0 == opt || inParamList) {
17,081,646✔
1052
          quit = true;
×
1053
        }
1054
        inParamList = true;
17,081,646✔
1055
        break;
17,081,646✔
1056
      case TK_NK_RP:
17,081,646✔
1057
        lastComma = false;
17,081,646✔
1058
        if (0 == opt || !inParamList) {
17,081,646✔
1059
          quit = true;
×
1060
        } else {
1061
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
17,081,646✔
1062
          inParamList = false;
17,081,646✔
1063
          paramNum = 0;
17,081,646✔
1064
          opt = 0;
17,081,646✔
1065
        }
1066
        break;
17,081,646✔
1067
      case TK_NK_ID:
2,304✔
1068
        lastComma = false;
2,304✔
1069
        if (0 == opt || !inParamList) {
2,304✔
1070
          quit = true;
768✔
1071
        } else {
1072
          paramList[paramNum++] = t0;
1,536✔
1073
        }
1074
        break;
2,304✔
1075
      case TK_NK_COMMA:
1,536✔
1076
        if (lastComma) {
1,536✔
1077
          quit = true;
×
1078
        }
1079
        lastComma = true;
1,536✔
1080
        break;
1,536✔
1081
      case TK_NK_SPACE:
34,153,546✔
1082
        break;
34,153,546✔
1083
      default:
×
1084
        lastComma = false;
×
1085
        quit = true;
×
1086
        break;
×
1087
    }
1088
  }
1089

1090
  taosMemoryFree(hint);
17,078,118✔
1091
  return pHintList;
17,078,118✔
1092
_err:
×
1093
  return NULL;
×
1094
}
1095

1096
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
2,883,200✔
1097
  trimEscape(pCxt, pLiteral, false);
2,883,200✔
1098
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
2,883,200✔
1099
}
1100

1101
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
144,727,332✔
1102
  CHECK_PARSER_STATUS(pCxt);
144,727,332✔
1103
  SValueNode* val = NULL;
144,725,528✔
1104
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
144,724,712✔
1105
  CHECK_MAKE_NODE(val);
144,745,268✔
1106
  if (pLiteral->type == TK_NK_STRING) {
144,745,268✔
1107
    // like '100s' or "100d"
1108
    // check format: ^[0-9]+[smwbauhdny]$'
1109
    if (pLiteral->n < 4) {
33,458✔
1110
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
1,484✔
1111
      return NULL;
1,484✔
1112
    }
1113
    char unit = pLiteral->z[pLiteral->n - 2];
31,974✔
1114
    switch (unit) {
31,974✔
1115
      case 'a':
26,746✔
1116
      case 'b':
1117
      case 'd':
1118
      case 'h':
1119
      case 'm':
1120
      case 's':
1121
      case 'u':
1122
      case 'w':
1123
      case 'y':
1124
      case 'n':
1125
        break;
26,746✔
1126
      default:
5,228✔
1127
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
5,228✔
1128
        return NULL;
5,228✔
1129
    }
1130
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
96,528✔
1131
      if (!isdigit(pLiteral->z[i])) {
74,234✔
1132
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
4,452✔
1133
        return NULL;
4,452✔
1134
      }
1135
    }
1136
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
22,294✔
1137
  } else {
1138
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
144,706,398✔
1139
  }
1140
  if (!val->literal) {
144,734,436✔
1141
    nodesDestroyNode((SNode*)val);
×
1142
    pCxt->errCode = terrno;
×
1143
    return NULL;
×
1144
  }
1145
  val->flag |= VALUE_FLAG_IS_DURATION;
144,730,184✔
1146
  val->translate = false;
144,730,184✔
1147
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
144,729,196✔
1148
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
144,730,012✔
1149
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
144,733,620✔
1150
  return (SNode*)val;
144,730,184✔
1151
_err:
×
1152
  return NULL;
×
1153
}
1154

1155
SNode* createTimeOffsetValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
2,551,968✔
1156
  CHECK_PARSER_STATUS(pCxt);
2,551,968✔
1157
  SValueNode* val = NULL;
2,551,968✔
1158
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
2,551,968✔
1159
  CHECK_MAKE_NODE(val);
2,551,968✔
1160
  if (pLiteral->type == TK_NK_STRING) {
2,551,968✔
1161
    // like '100s' or "100d"
1162
    // check format: ^[0-9]+[smwbauhdny]$'
1163
    if (pLiteral->n < 4) {
×
1164
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
1165
      return NULL;
×
1166
    }
1167
    char unit = pLiteral->z[pLiteral->n - 2];
×
1168
    switch (unit) {
×
1169
      case 'a':
×
1170
      case 'b':
1171
      case 'd':
1172
      case 'h':
1173
      case 'm':
1174
      case 's':
1175
      case 'u':
1176
      case 'w':
1177
        break;
×
1178
      default:
×
1179
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
1180
        return NULL;
×
1181
    }
1182
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
×
1183
      if (!isdigit(pLiteral->z[i])) {
×
1184
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
1185
        return NULL;
×
1186
      }
1187
    }
1188
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
×
1189
  } else {
1190
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
2,551,968✔
1191
  }
1192
  if (!val->literal) {
2,551,968✔
1193
    nodesDestroyNode((SNode*)val);
×
1194
    pCxt->errCode = terrno;
×
1195
    return NULL;
×
1196
  }
1197
  val->flag |= VALUE_FLAG_IS_TIME_OFFSET;
2,551,968✔
1198
  val->translate = false;
2,551,968✔
1199
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
2,551,968✔
1200
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
2,551,968✔
1201
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
2,551,968✔
1202
  return (SNode*)val;
2,551,968✔
1203
_err:
×
1204
  return NULL;
×
1205
}
1206

1207
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
1,877,946✔
1208
  CHECK_PARSER_STATUS(pCxt);
1,877,946✔
1209
  if (NULL == pCxt->pQueryCxt->db) {
1,877,946✔
1210
    return NULL;
9,102✔
1211
  }
1212

1213
  SValueNode* val = NULL;
1,868,844✔
1214
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
1,868,844✔
1215
  CHECK_MAKE_NODE(val);
1,868,844✔
1216
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
1,868,844✔
1217
  if (!val->literal) {
1,868,844✔
1218
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
1219
    nodesDestroyNode((SNode*)val);
×
1220
    return NULL;
×
1221
  }
1222
  val->translate = false;
1,868,844✔
1223
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
1,868,844✔
1224
  val->node.resType.bytes = strlen(val->literal);
1,868,844✔
1225
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
1,868,844✔
1226
  return (SNode*)val;
1,868,844✔
1227
_err:
×
1228
  return NULL;
×
1229
}
1230

1231
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
32,940✔
1232
  CHECK_PARSER_STATUS(pCxt);
32,940✔
1233
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
32,940✔
1234
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
776✔
1235
    return NULL;
776✔
1236
  }
1237
  SValueNode* val = NULL;
32,164✔
1238
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
32,164✔
1239
  CHECK_MAKE_NODE(val);
32,164✔
1240
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
32,164✔
1241
  if (!val->literal) {
32,164✔
1242
    pCxt->errCode = terrno;
×
1243
    nodesDestroyNode((SNode*)val);
×
1244
    return NULL;
×
1245
  }
1246
  val->placeholderNo = ++pCxt->placeholderNo;
32,164✔
1247
  if (NULL == pCxt->pPlaceholderValues) {
32,164✔
1248
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
25,948✔
1249
    if (NULL == pCxt->pPlaceholderValues) {
25,948✔
1250
      nodesDestroyNode((SNode*)val);
×
1251
      return NULL;
×
1252
    }
1253
  }
1254
  if (NULL == taosArrayPush(pCxt->pPlaceholderValues, &val)) {
64,328✔
1255
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
1256
    nodesDestroyNode((SNode*)val);
×
1257
    taosArrayDestroy(pCxt->pPlaceholderValues);
×
1258
    return NULL;
×
1259
  }
1260
  return (SNode*)val;
32,164✔
1261
_err:
×
1262
  return NULL;
×
1263
}
1264

1265
SNode* createDurationPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
1,152✔
1266
  SNode* pNode = createPlaceholderValueNode(pCxt, pLiteral);
1,152✔
1267
  if (pNode != NULL) {
1,152✔
1268
    SValueNode* val = (SValueNode*)pNode;
1,152✔
1269
    val->flag |= VALUE_FLAG_IS_DURATION;
1,152✔
1270
    val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
1,152✔
1271
    val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
1,152✔
1272
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
1,152✔
1273
  }
1274
  return pNode;
1,152✔
1275
}
1276

1277
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
382,008,822✔
1278
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
382,008,822✔
1279
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
54,936,610✔
1280
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
54,875,050✔
1281
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
54,875,050✔
1282
    nodesDestroyNode(pParam);
54,875,050✔
1283
    return code;
54,875,050✔
1284
  } else {
1285
    return nodesListAppend(pCond->pParameterList, pParam);
327,134,588✔
1286
  }
1287
}
1288

1289
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
191,326,310✔
1290
  CHECK_PARSER_STATUS(pCxt);
191,326,310✔
1291
  SLogicConditionNode* cond = NULL;
191,329,098✔
1292
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
191,329,098✔
1293
  CHECK_MAKE_NODE(cond);
191,340,936✔
1294
  cond->condType = type;
191,340,936✔
1295
  cond->pParameterList = NULL;
191,341,752✔
1296
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
191,342,568✔
1297
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
191,341,284✔
1298
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
191,336,608✔
1299
  }
1300
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
191,346,220✔
1301
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
190,678,390✔
1302
  }
1303
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
191,344,396✔
1304
    nodesDestroyNode((SNode*)cond);
×
1305
    return NULL;
×
1306
  }
1307
  return (SNode*)cond;
191,339,800✔
1308
_err:
×
1309
  nodesDestroyNode(pParam1);
×
1310
  nodesDestroyNode(pParam2);
×
1311
  return NULL;
130✔
1312
}
1313

1314
static uint8_t getMinusDataType(uint8_t orgType) {
58,023,334✔
1315
  switch (orgType) {
58,023,334✔
1316
    case TSDB_DATA_TYPE_UTINYINT:
41,093,800✔
1317
    case TSDB_DATA_TYPE_USMALLINT:
1318
    case TSDB_DATA_TYPE_UINT:
1319
    case TSDB_DATA_TYPE_UBIGINT:
1320
      return TSDB_DATA_TYPE_BIGINT;
41,093,800✔
1321
    default:
16,930,322✔
1322
      break;
16,930,322✔
1323
  }
1324
  return orgType;
16,930,322✔
1325
}
1326

1327
SNode* setNodeQuantifyType(SAstCreateContext* pCxt, SNode* pNode, EQuantifyType type) {
112,039,264✔
1328
  CHECK_PARSER_STATUS(pCxt);
112,039,264✔
1329

1330
  switch (nodeType(pNode)) {
112,039,264✔
1331
    case QUERY_NODE_SELECT_STMT: {
111,674,104✔
1332
      SSelectStmt* pSelect = (SSelectStmt*)pNode;
111,674,104✔
1333
      pSelect->quantify = type;
111,674,104✔
1334
      pSelect->subQType = E_SUB_QUERY_COLUMN;
111,674,104✔
1335
      break;
111,674,104✔
1336
    }  
1337
    case QUERY_NODE_SET_OPERATOR:{
366,624✔
1338
      SSetOperator* pSet = (SSetOperator*)pNode;
366,624✔
1339
      pSet->quantify = type;
366,624✔
1340
      pSet->subQType = E_SUB_QUERY_COLUMN;
366,624✔
1341
      break;
366,624✔
1342
    }
1343
    default:
×
1344
      pCxt->errCode = TSDB_CODE_PAR_INVALID_EXPR_SUBQ;
×
1345
      CHECK_PARSER_STATUS(pCxt);
×
1346
      break;
×
1347
  }
1348

1349
  return pNode;
112,039,264✔
1350

1351
_err:
×
1352
  return NULL;
×
1353
}
1354

1355
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
1,156,508,514✔
1356
  CHECK_PARSER_STATUS(pCxt);
1,156,508,514✔
1357
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
1,156,507,526✔
1358
    SValueNode* pVal = (SValueNode*)pLeft;
58,024,252✔
1359
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
58,024,252✔
1360
    if (!pNewLiteral) {
58,024,120✔
1361
      pCxt->errCode = terrno;
×
1362
      goto _err;
×
1363
    }
1364
    if ('+' == pVal->literal[0]) {
58,024,120✔
1365
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal + 1);
×
1366
    } else if ('-' == pVal->literal[0]) {
58,024,074✔
1367
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "%s", pVal->literal + 1);
9,396✔
1368
    } else {
1369
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal);
58,014,656✔
1370
    }
1371
    taosMemoryFree(pVal->literal);
58,024,074✔
1372
    pVal->literal = pNewLiteral;
58,023,992✔
1373
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
58,023,992✔
1374
    return pLeft;
58,023,992✔
1375
  }
1376
  if ((OP_TYPE_IN == type || OP_TYPE_NOT_IN == type) && pRight) {
1,098,483,320✔
1377
    if (QUERY_NODE_SELECT_STMT == nodeType(pRight)) {
44,644,406✔
1378
      ((SSelectStmt*)pRight)->subQType= E_SUB_QUERY_COLUMN;
32,576,036✔
1379
    } else if (QUERY_NODE_SET_OPERATOR == nodeType(pRight)) {
12,068,370✔
1380
      ((SSetOperator*)pRight)->subQType= E_SUB_QUERY_COLUMN;
1,535,824✔
1381
    }
1382
  }
1383
  if ((OP_TYPE_EXISTS == type || OP_TYPE_NOT_EXISTS == type) && pLeft) {
1,098,483,320✔
1384
    if (QUERY_NODE_SELECT_STMT == nodeType(pLeft)) {
8,884,224✔
1385
      ((SSelectStmt*)pLeft)->subQType= E_SUB_QUERY_ROWNUM;
8,884,224✔
1386
    } else if (QUERY_NODE_SET_OPERATOR == nodeType(pLeft)) {
×
1387
      ((SSetOperator*)pLeft)->subQType= E_SUB_QUERY_ROWNUM;
×
1388
    }
1389
  }
1390
  if (pLeft && QUERY_NODE_VALUE == nodeType(pLeft)) {
1,098,483,320✔
1391
    SValueNode* pVal = (SValueNode*)pLeft;
74,608,524✔
1392
    pVal->tz = pCxt->pQueryCxt->timezone;
74,608,524✔
1393
  }
1394
  if (pRight && QUERY_NODE_VALUE == nodeType(pRight)) {
1,098,486,652✔
1395
    SValueNode* pVal = (SValueNode*)pRight;
485,890,428✔
1396
    pVal->tz = pCxt->pQueryCxt->timezone;
485,890,428✔
1397
  }
1398

1399
  SOperatorNode* op = NULL;
1,098,478,294✔
1400
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
1,098,475,858✔
1401
  CHECK_MAKE_NODE(op);
1,098,537,832✔
1402
  op->opType = type;
1,098,537,832✔
1403
  op->pLeft = pLeft;
1,098,536,096✔
1404
  op->pRight = pRight;
1,098,531,990✔
1405
  op->tz = pCxt->pQueryCxt->timezone;
1,098,533,250✔
1406
  op->charsetCxt = pCxt->pQueryCxt->charsetCxt;
1,098,530,656✔
1407
  return (SNode*)op;
1,098,528,772✔
1408
_err:
×
1409
  nodesDestroyNode(pLeft);
×
1410
  nodesDestroyNode(pRight);
×
1411
  return NULL;
×
1412
}
1413

1414
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
39,178,466✔
1415
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
39,178,466✔
1416
  CHECK_PARSER_STATUS(pCxt);
39,177,478✔
1417
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
39,175,502✔
1418
  CHECK_PARSER_STATUS(pCxt);
39,182,468✔
1419
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
39,181,824✔
1420
  CHECK_PARSER_STATUS(pCxt);
39,183,434✔
1421
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
39,183,262✔
1422
  CHECK_PARSER_STATUS(pCxt);
39,185,872✔
1423
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
39,185,056✔
1424
  CHECK_PARSER_STATUS(pCxt);
39,179,542✔
1425
  return pRet;
39,181,690✔
1426
_err:
×
1427
  nodesDestroyNode(pNew);
×
1428
  nodesDestroyNode(pGE);
×
1429
  nodesDestroyNode(pLE);
×
1430
  nodesDestroyNode(pExpr);
×
1431
  nodesDestroyNode(pLeft);
×
1432
  nodesDestroyNode(pRight);
×
1433
  return NULL;
442✔
1434
}
1435

1436
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
6,903,148✔
1437
  SNode *pNew = NULL, *pLT = NULL, *pGT = NULL;
6,903,148✔
1438
  CHECK_PARSER_STATUS(pCxt);
6,903,148✔
1439
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
6,903,148✔
1440
  CHECK_PARSER_STATUS(pCxt);
6,903,148✔
1441
  pLT = createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft);
6,903,148✔
1442
  CHECK_PARSER_STATUS(pCxt);
6,903,148✔
1443
  pGT = createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pNew, pRight);
6,903,148✔
1444
  CHECK_PARSER_STATUS(pCxt);
6,903,148✔
1445
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, pLT, pGT);
6,903,148✔
1446
  CHECK_PARSER_STATUS(pCxt);
6,903,148✔
1447
  return pRet;
6,903,148✔
1448
_err:
×
1449
  nodesDestroyNode(pNew);
×
1450
  nodesDestroyNode(pGT);
×
1451
  nodesDestroyNode(pLT);
×
1452
  nodesDestroyNode(pExpr);
×
1453
  nodesDestroyNode(pLeft);
×
1454
  nodesDestroyNode(pRight);
×
1455
  return NULL;
×
1456
}
1457

1458
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
155,560,050✔
1459
  CHECK_PARSER_STATUS(pCxt);
155,560,050✔
1460
  SColumnNode* pCol = NULL;
155,558,650✔
1461
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
155,558,650✔
1462
  CHECK_MAKE_NODE(pCol);
155,574,264✔
1463
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
155,574,264✔
1464
  if (NULL == pFuncName) {
155,572,460✔
1465
    tstrncpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME, TSDB_COL_NAME_LEN);
132,785,436✔
1466
  } else {
1467
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
22,787,024✔
1468
  }
1469
  pCol->isPrimTs = true;
155,574,196✔
1470
  return (SNode*)pCol;
155,568,852✔
1471
_err:
×
1472
  return NULL;
×
1473
}
1474

1475
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
937,094,926✔
1476
  CHECK_PARSER_STATUS(pCxt);
937,094,926✔
1477
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
937,093,828✔
1478
    return createPrimaryKeyCol(pCxt, pFuncName);
22,767,614✔
1479
  }
1480
  SFunctionNode* func = NULL;
914,332,544✔
1481
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
914,332,506✔
1482
  CHECK_MAKE_NODE(func);
914,345,104✔
1483
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
914,345,104✔
1484
  func->pParameterList = pParameterList;
914,343,030✔
1485
  func->tz = pCxt->pQueryCxt->timezone;
914,339,504✔
1486
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
914,340,962✔
1487
  return (SNode*)func;
914,342,750✔
1488
_err:
×
1489
  nodesDestroyList(pParameterList);
×
1490
  return NULL;
×
1491
}
1492

1493
SNode* createPHTbnameFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
82,168✔
1494
  CHECK_PARSER_STATUS(pCxt);
82,168✔
1495
  SFunctionNode* func = NULL;
82,168✔
1496
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
82,168✔
1497
  CHECK_MAKE_NODE(func);
82,168✔
1498
  tstrncpy(func->functionName, "_placeholder_tbname", TSDB_FUNC_NAME_LEN);
82,168✔
1499
  func->pParameterList = pParameterList;
82,168✔
1500
  func->tz = pCxt->pQueryCxt->timezone;
82,168✔
1501
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
82,168✔
1502
  return (SNode*)func;
82,168✔
1503
_err:
×
1504
  nodesDestroyList(pParameterList);
×
1505
  return NULL;
×
1506
}
1507

1508
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
206,618,552✔
1509
  SFunctionNode* func = NULL;
206,618,552✔
1510
  CHECK_PARSER_STATUS(pCxt);
206,618,552✔
1511
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
206,618,552✔
1512
  CHECK_MAKE_NODE(func);
206,621,512✔
1513
  tstrncpy(func->functionName, "cast", TSDB_FUNC_NAME_LEN);
206,621,512✔
1514
  func->node.resType = dt;
206,621,512✔
1515
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
206,621,512✔
1516
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
161,011,460✔
1517
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
45,610,052✔
1518
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
7,555,362✔
1519
  }
1520
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
206,621,512✔
1521
  CHECK_PARSER_STATUS(pCxt);
206,621,722✔
1522
  func->tz = pCxt->pQueryCxt->timezone;
206,621,722✔
1523
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
206,621,722✔
1524

1525
  return (SNode*)func;
206,621,722✔
1526
_err:
×
1527
  nodesDestroyNode((SNode*)func);
×
1528
  nodesDestroyNode(pExpr);
×
1529
  return NULL;
314✔
1530
}
1531

1532
SNode* createPositionFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
236,924✔
1533
  SFunctionNode* func = NULL;
236,924✔
1534
  CHECK_PARSER_STATUS(pCxt);
236,924✔
1535
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
236,924✔
1536
  CHECK_MAKE_NODE(func);
236,924✔
1537
  tstrncpy(func->functionName, "position", TSDB_FUNC_NAME_LEN);
236,924✔
1538
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
236,924✔
1539
  CHECK_PARSER_STATUS(pCxt);
236,924✔
1540
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
236,924✔
1541
  CHECK_PARSER_STATUS(pCxt);
236,924✔
1542
  return (SNode*)func;
236,924✔
1543
_err:
×
1544
  nodesDestroyNode((SNode*)func);
×
1545
  nodesDestroyNode(pExpr);
×
1546
  nodesDestroyNode(pExpr2);
×
1547
  return NULL;
×
1548
}
1549

1550
SNode* createTrimFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, ETrimType type) {
44,142✔
1551
  SFunctionNode* func = NULL;
44,142✔
1552
  CHECK_PARSER_STATUS(pCxt);
44,142✔
1553
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
44,142✔
1554
  CHECK_MAKE_NODE(func);
44,142✔
1555
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
44,142✔
1556
  func->trimType = type;
44,142✔
1557
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
44,142✔
1558
  CHECK_PARSER_STATUS(pCxt);
44,142✔
1559
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
44,142✔
1560
  return (SNode*)func;
44,142✔
1561
_err:
×
1562
  nodesDestroyNode((SNode*)func);
×
1563
  nodesDestroyNode(pExpr);
×
1564
  return NULL;
×
1565
}
1566

1567
SNode* createTrimFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, ETrimType type) {
123,276✔
1568
  SFunctionNode* func = NULL;
123,276✔
1569
  CHECK_PARSER_STATUS(pCxt);
123,276✔
1570
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
123,276✔
1571
  CHECK_MAKE_NODE(func);
123,276✔
1572
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
123,276✔
1573
  func->trimType = type;
123,276✔
1574
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
123,276✔
1575
  CHECK_PARSER_STATUS(pCxt);
123,276✔
1576
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
123,276✔
1577
  CHECK_PARSER_STATUS(pCxt);
123,276✔
1578
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
123,276✔
1579
  return (SNode*)func;
123,276✔
1580
_err:
×
1581
  nodesDestroyNode((SNode*)func);
×
1582
  nodesDestroyNode(pExpr);
×
1583
  nodesDestroyNode(pExpr2);
×
1584
  return NULL;
×
1585
}
1586

1587
SNode* createSubstrFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
19,984✔
1588
  SFunctionNode* func = NULL;
19,984✔
1589
  CHECK_PARSER_STATUS(pCxt);
19,984✔
1590
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
19,984✔
1591
  CHECK_MAKE_NODE(func);
19,984✔
1592
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
19,984✔
1593
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
19,984✔
1594
  CHECK_PARSER_STATUS(pCxt);
19,984✔
1595
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
19,984✔
1596
  CHECK_PARSER_STATUS(pCxt);
19,984✔
1597
  return (SNode*)func;
19,984✔
1598
_err:
×
1599
  nodesDestroyNode((SNode*)func);
×
1600
  nodesDestroyNode(pExpr);
×
1601
  nodesDestroyNode(pExpr2);
×
1602
  return NULL;
×
1603
}
1604

1605
SNode* createSubstrFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, SNode* pExpr3) {
43,928✔
1606
  SFunctionNode* func = NULL;
43,928✔
1607
  CHECK_PARSER_STATUS(pCxt);
43,928✔
1608
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
43,928✔
1609
  CHECK_MAKE_NODE(func);
43,928✔
1610
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
43,928✔
1611
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
43,928✔
1612
  CHECK_PARSER_STATUS(pCxt);
43,928✔
1613
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
43,928✔
1614
  CHECK_PARSER_STATUS(pCxt);
43,928✔
1615
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr3);
43,928✔
1616
  CHECK_PARSER_STATUS(pCxt);
43,928✔
1617
  return (SNode*)func;
43,928✔
1618
_err:
×
1619
  nodesDestroyNode((SNode*)func);
×
1620
  nodesDestroyNode(pExpr);
×
1621
  nodesDestroyNode(pExpr2);
×
1622
  nodesDestroyNode(pExpr3);
×
1623
  return NULL;
×
1624
}
1625

1626
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
14,188,340✔
1627
  SNodeListNode* list = NULL;
14,188,340✔
1628
  CHECK_PARSER_STATUS(pCxt);
14,188,340✔
1629
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
14,188,340✔
1630
  CHECK_MAKE_NODE(list);
14,188,340✔
1631
  list->pNodeList = pList;
14,188,340✔
1632
  return (SNode*)list;
14,188,340✔
1633
_err:
×
1634
  nodesDestroyList(pList);
×
1635
  return NULL;
×
1636
}
1637

1638
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
12,416✔
1639
  SNodeListNode* list = NULL;
12,416✔
1640
  CHECK_PARSER_STATUS(pCxt);
12,416✔
1641
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
12,416✔
1642
  CHECK_MAKE_NODE(list);
12,416✔
1643
  pCxt->errCode = nodesListMakeStrictAppend(&list->pNodeList, p1);
12,416✔
1644
  CHECK_PARSER_STATUS(pCxt);
12,416✔
1645
  pCxt->errCode = nodesListStrictAppend(list->pNodeList, p2);
12,416✔
1646
  CHECK_PARSER_STATUS(pCxt);
12,416✔
1647
  return (SNode*)list;
12,416✔
1648
_err:
×
1649
  nodesDestroyNode((SNode*)list);
×
1650
  nodesDestroyNode(p1);
×
1651
  nodesDestroyNode(p2);
×
1652
  return NULL;
×
1653
}
1654

1655
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
1,585,461,556✔
1656
  CHECK_PARSER_STATUS(pCxt);
1,585,461,556✔
1657
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
1,585,459,078✔
1658
  CHECK_NAME(checkTableName(pCxt, pTableName));
1,585,484,808✔
1659
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
1,585,455,030✔
1660
  SRealTableNode* realTable = NULL;
1,585,466,656✔
1661
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
1,585,468,358✔
1662
  CHECK_MAKE_NODE(realTable);
1,585,523,822✔
1663
  if (NULL != pDbName) {
1,585,523,822✔
1664
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
357,606,926✔
1665
  } else {
1666
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
1,227,916,896✔
1667
  }
1668
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
1,585,480,246✔
1669
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
166,332,280✔
1670
  } else {
1671
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
1,419,149,914✔
1672
  }
1673
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
1,585,499,180✔
1674
  return (SNode*)realTable;
1,585,469,432✔
1675
_err:
18,140✔
1676
  return NULL;
18,140✔
1677
}
1678

1679
SNode* createPlaceHolderTableNode(SAstCreateContext* pCxt, EStreamPlaceholder type, SToken* pTableAlias) {
279,604✔
1680
  CHECK_PARSER_STATUS(pCxt);
279,604✔
1681

1682
  SPlaceHolderTableNode* phTable = NULL;
279,604✔
1683
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PLACE_HOLDER_TABLE, (SNode**)&phTable);
279,604✔
1684
  CHECK_MAKE_NODE(phTable);
279,604✔
1685

1686
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
279,604✔
1687
    COPY_STRING_FORM_ID_TOKEN(phTable->table.tableAlias, pTableAlias);
5,806✔
1688
  }
1689

1690
  phTable->placeholderType = type;
279,604✔
1691
  return (SNode*)phTable;
279,604✔
1692
_err:
×
1693
  return NULL;
×
1694
}
1695

1696
SNode* createStreamNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pStreamName) {
855,996✔
1697
  CHECK_PARSER_STATUS(pCxt);
855,996✔
1698
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
855,996✔
1699
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
855,996✔
1700
  SStreamNode* pStream = NULL;
855,592✔
1701
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM, (SNode**)&pStream);
855,592✔
1702
  CHECK_MAKE_NODE(pStream);
855,592✔
1703
  if (NULL != pDbName) {
855,592✔
1704
    COPY_STRING_FORM_ID_TOKEN(pStream->dbName, pDbName);
568,710✔
1705
  } else {
1706
    snprintf(pStream->dbName, sizeof(pStream->dbName), "%s", pCxt->pQueryCxt->db);
286,882✔
1707
  }
1708
  COPY_STRING_FORM_ID_TOKEN(pStream->streamName, pStreamName);
855,592✔
1709
  return (SNode*)pStream;
855,592✔
1710
_err:
404✔
1711
  return NULL;
404✔
1712
}
1713

1714
SNode* createRecalcRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
23,472✔
1715
  SStreamCalcRangeNode* pRange = NULL;
23,472✔
1716
  CHECK_PARSER_STATUS(pCxt);
23,472✔
1717
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_CALC_RANGE, (SNode**)&pRange);
23,472✔
1718
  CHECK_MAKE_NODE(pRange);
23,472✔
1719
  if (NULL == pStart && NULL == pEnd) {
23,472✔
1720
    pRange->calcAll = true;
×
1721
  } else {
1722
    pRange->calcAll = false;
23,472✔
1723
    pRange->pStart = pStart;
23,472✔
1724
    pRange->pEnd = pEnd;
23,472✔
1725
  }
1726

1727
  return (SNode*)pRange;
23,472✔
1728
_err:
×
1729
  nodesDestroyNode((SNode*)pRange);
×
1730
  nodesDestroyNode(pStart);
×
1731
  nodesDestroyNode(pEnd);
×
1732
  return NULL;
×
1733
}
1734

1735
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
63,592,946✔
1736
  CHECK_PARSER_STATUS(pCxt);
63,592,946✔
1737
  if (!checkTableName(pCxt, pTableAlias)) {
63,592,946✔
1738
    return NULL;
×
1739
  }
1740
  STempTableNode* tempTable = NULL;
63,592,946✔
1741
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
63,592,946✔
1742
  CHECK_MAKE_NODE(tempTable);
63,592,946✔
1743
  tempTable->pSubquery = pSubquery;
63,592,946✔
1744
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
63,592,946✔
1745
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
2,081,460✔
1746
  } else {
1747
    taosRandStr(tempTable->table.tableAlias, 32);
61,511,486✔
1748
  }
1749
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
63,592,946✔
1750
    tstrncpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
55,164,782✔
1751
    ((SSelectStmt*)pSubquery)->isSubquery = true;
55,164,782✔
1752
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
8,428,164✔
1753
    tstrncpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
8,428,164✔
1754
  }
1755
  return (SNode*)tempTable;
63,592,946✔
1756
_err:
×
1757
  nodesDestroyNode(pSubquery);
×
1758
  return NULL;
×
1759
}
1760

1761
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, EJoinSubType stype, SNode* pLeft, SNode* pRight,
85,310,326✔
1762
                           SNode* pJoinCond) {
1763
  CHECK_PARSER_STATUS(pCxt);
85,310,326✔
1764
  SJoinTableNode* joinTable = NULL;
85,310,326✔
1765
  pCxt->errCode = nodesMakeNode(QUERY_NODE_JOIN_TABLE, (SNode**)&joinTable);
85,310,326✔
1766
  CHECK_MAKE_NODE(joinTable);
85,317,250✔
1767
  joinTable->joinType = type;
85,317,250✔
1768
  joinTable->subType = stype;
85,317,250✔
1769
  joinTable->pLeft = pLeft;
85,317,250✔
1770
  joinTable->pRight = pRight;
85,317,250✔
1771
  joinTable->pOnCond = pJoinCond;
85,317,250✔
1772
  return (SNode*)joinTable;
85,317,250✔
1773
_err:
×
1774
  nodesDestroyNode(pLeft);
×
1775
  nodesDestroyNode(pRight);
×
1776
  nodesDestroyNode(pJoinCond);
×
1777
  return NULL;
×
1778
}
1779

1780
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
763,300✔
1781
  CHECK_PARSER_STATUS(pCxt);
763,300✔
1782
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
763,300✔
1783
  CHECK_NAME(checkViewName(pCxt, pViewName));
762,492✔
1784
  SViewNode* pView = NULL;
760,946✔
1785
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
760,946✔
1786
  CHECK_MAKE_NODE(pView);
760,946✔
1787
  if (NULL != pDbName) {
760,946✔
1788
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
375,500✔
1789
  } else {
1790
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
385,446✔
1791
  }
1792
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
760,946✔
1793
  return (SNode*)pView;
760,946✔
1794
_err:
2,354✔
1795
  return NULL;
2,354✔
1796
}
1797

1798
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) {
57,834,024✔
1799
  CHECK_PARSER_STATUS(pCxt);
57,834,024✔
1800
  SLimitNode* limitNode = NULL;
57,834,024✔
1801
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
57,834,024✔
1802
  CHECK_MAKE_NODE(limitNode);
57,835,202✔
1803
  limitNode->limit = (SValueNode*)pLimit;
57,835,202✔
1804
  if (NULL != pOffset) {
57,835,202✔
1805
    limitNode->offset = (SValueNode*)pOffset;
13,807,086✔
1806
  }
1807
  return (SNode*)limitNode;
57,835,202✔
1808
_err:
×
1809
  return NULL;
×
1810
}
1811

1812
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
339,059,874✔
1813
  CHECK_PARSER_STATUS(pCxt);
339,059,874✔
1814
  SOrderByExprNode* orderByExpr = NULL;
339,058,890✔
1815
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
339,059,874✔
1816
  CHECK_MAKE_NODE(orderByExpr);
339,089,282✔
1817
  orderByExpr->pExpr = pExpr;
339,089,282✔
1818
  orderByExpr->order = order;
339,090,266✔
1819
  if (NULL_ORDER_DEFAULT == nullOrder) {
339,089,278✔
1820
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
338,645,984✔
1821
  }
1822
  orderByExpr->nullOrder = nullOrder;
339,089,278✔
1823
  return (SNode*)orderByExpr;
339,089,278✔
1824
_err:
×
1825
  nodesDestroyNode(pExpr);
×
1826
  return NULL;
×
1827
}
1828

1829
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
26,672,806✔
1830
  CHECK_PARSER_STATUS(pCxt);
26,672,806✔
1831
  SSessionWindowNode* session = NULL;
26,672,806✔
1832
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
26,672,806✔
1833
  CHECK_MAKE_NODE(session);
26,679,032✔
1834
  session->pCol = (SColumnNode*)pCol;
26,679,032✔
1835
  session->pGap = (SValueNode*)pGap;
26,679,032✔
1836
  return (SNode*)session;
26,679,032✔
1837
_err:
×
1838
  nodesDestroyNode(pCol);
×
1839
  nodesDestroyNode(pGap);
×
1840
  return NULL;
×
1841
}
1842

1843
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr, SNodeList* pOptions, SNode* pTrueForLimit) {
10,558,140✔
1844
  SStateWindowNode* state = NULL;
10,558,140✔
1845
  CHECK_PARSER_STATUS(pCxt);
10,558,140✔
1846
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
10,557,752✔
1847
  CHECK_MAKE_NODE(state);
10,559,628✔
1848
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
10,559,628✔
1849
  CHECK_MAKE_NODE(state->pCol);
10,559,232✔
1850
  state->pExpr = pExpr;
10,559,232✔
1851
  state->pTrueForLimit = pTrueForLimit;
10,559,232✔
1852
  if (pOptions != NULL) {
10,559,232✔
1853
    if (pOptions->length >= 1) {
732,802✔
1854
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 0), &state->pExtend);
732,802✔
1855
      CHECK_MAKE_NODE(state->pExtend);
732,802✔
1856
    }
1857
    if (pOptions->length == 2) {
732,802✔
1858
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 1), &state->pZeroth);
×
1859
      CHECK_MAKE_NODE(state->pZeroth);
×
1860
    }
1861
    nodesDestroyList(pOptions);
732,802✔
1862
  }
1863
  return (SNode*)state;
10,557,054✔
1864
_err:
388✔
1865
  nodesDestroyNode((SNode*)state);
388✔
1866
  nodesDestroyNode(pExpr);
388✔
1867
  nodesDestroyNode(pTrueForLimit);
388✔
1868
  nodesDestroyList(pOptions);
388✔
1869
  return NULL;
1,086✔
1870
}
1871

1872
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond, SNode* pTrueForLimit) {
9,571,546✔
1873
  SEventWindowNode* pEvent = NULL;
9,571,546✔
1874
  CHECK_PARSER_STATUS(pCxt);
9,571,546✔
1875
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
9,571,546✔
1876
  CHECK_MAKE_NODE(pEvent);
9,573,856✔
1877
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
9,573,856✔
1878
  CHECK_MAKE_NODE(pEvent->pCol);
9,572,888✔
1879
  pEvent->pStartCond = pStartCond;
9,572,888✔
1880
  pEvent->pEndCond = pEndCond;
9,572,888✔
1881
  pEvent->pTrueForLimit = pTrueForLimit;
9,572,888✔
1882
  return (SNode*)pEvent;
9,572,888✔
1883
_err:
×
1884
  nodesDestroyNode((SNode*)pEvent);
×
1885
  nodesDestroyNode(pStartCond);
×
1886
  nodesDestroyNode(pEndCond);
×
1887
  nodesDestroyNode(pTrueForLimit);
×
1888
  return NULL;
968✔
1889
}
1890

1891
SNode* createTrueForCountNode(SAstCreateContext* pCxt, const SToken* pCount) {
33,938✔
1892
  STrueForNode* pTrueFor = NULL;
33,938✔
1893
  CHECK_PARSER_STATUS(pCxt);
33,938✔
1894
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRUE_FOR, (SNode**)&pTrueFor);
33,938✔
1895
  CHECK_MAKE_NODE(pTrueFor);
33,938✔
1896
  pTrueFor->trueForType = TRUE_FOR_COUNT_ONLY;
33,938✔
1897
  pTrueFor->pDuration = NULL;
33,938✔
1898
  pTrueFor->count = taosStr2Int32(pCount->z, NULL, 10);
33,938✔
1899
  if (pTrueFor->count < 0) {
33,938✔
1900
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_TRUE_FOR_COUNT);
388✔
1901
    goto _err;
388✔
1902
  }
1903
  return (SNode*)pTrueFor;
33,550✔
1904
_err:
388✔
1905
  nodesDestroyNode((SNode*)pTrueFor);
388✔
1906
  return NULL;
388✔
1907
}
1908

1909
SNode* createTrueForAndNode(SAstCreateContext* pCxt, SNode* pDuration, const SToken* pCount) {
32,386✔
1910
  STrueForNode* pTrueFor = NULL;
32,386✔
1911
  CHECK_PARSER_STATUS(pCxt);
32,386✔
1912
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRUE_FOR, (SNode**)&pTrueFor);
32,386✔
1913
  CHECK_MAKE_NODE(pTrueFor);
32,386✔
1914
  pTrueFor->trueForType = TRUE_FOR_AND;
32,386✔
1915
  pTrueFor->pDuration = pDuration;
32,386✔
1916
  pDuration = NULL;
32,386✔
1917
  pTrueFor->count = taosStr2Int32(pCount->z, NULL, 10);
32,386✔
1918
  return (SNode*)pTrueFor;
32,386✔
1919
_err:
×
1920
  nodesDestroyNode((SNode*)pTrueFor);
×
1921
  nodesDestroyNode(pDuration);
×
1922
  return NULL;
×
1923
}
1924

1925
SNode* createTrueForOrNode(SAstCreateContext* pCxt, SNode* pDuration, const SToken* pCount) {
32,386✔
1926
  STrueForNode* pTrueFor = NULL;
32,386✔
1927
  CHECK_PARSER_STATUS(pCxt);
32,386✔
1928
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRUE_FOR, (SNode**)&pTrueFor);
32,386✔
1929
  CHECK_MAKE_NODE(pTrueFor);
32,386✔
1930
  pTrueFor->trueForType = TRUE_FOR_OR;
32,386✔
1931
  pTrueFor->pDuration = pDuration;
32,386✔
1932
  pDuration = NULL;
32,386✔
1933
  pTrueFor->count = taosStr2Int32(pCount->z, NULL, 10);
32,386✔
1934
  return (SNode*)pTrueFor;
32,386✔
1935
_err:
×
1936
  nodesDestroyNode((SNode*)pTrueFor);
×
1937
  nodesDestroyNode(pDuration);
×
1938
  return NULL;
×
1939
}
1940

1941
SNode* createCountWindowNode(SAstCreateContext* pCxt, const SToken* pCountToken, const SToken* pSlidingToken,
×
1942
                             SNodeList* pColList) {
1943
  SCountWindowNode* pCount = NULL;
×
1944
  CHECK_PARSER_STATUS(pCxt);
×
1945
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
×
1946
  CHECK_MAKE_NODE(pCount);
×
1947
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1948
  CHECK_MAKE_NODE(pCount->pCol);
×
1949
  pCount->windowCount = taosStr2Int64(pCountToken->z, NULL, 10);
×
1950
  if (pSlidingToken == NULL) {
×
1951
    pCount->windowSliding = taosStr2Int64(pSlidingToken->z, NULL, 10);
×
1952
  } else {
1953
    pCount->windowSliding = taosStr2Int64(pCountToken->z, NULL, 10);
×
1954
  }
1955
  pCount->pColList = pColList;
×
1956
  return (SNode*)pCount;
×
1957
_err:
×
1958
  nodesDestroyNode((SNode*)pCount);
×
1959
  return NULL;
×
1960
}
1961

1962
SNode* createCountWindowNodeFromArgs(SAstCreateContext* pCxt, SNode* arg) {
8,931,758✔
1963
  SCountWindowArgs* args = (SCountWindowArgs*)arg;
8,931,758✔
1964
  SCountWindowNode* pCount = NULL;
8,931,758✔
1965
  CHECK_PARSER_STATUS(pCxt);
8,931,758✔
1966
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
8,931,758✔
1967
  CHECK_MAKE_NODE(pCount);
8,933,282✔
1968
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
8,933,282✔
1969
  CHECK_MAKE_NODE(pCount->pCol);
8,933,020✔
1970
  pCount->windowCount = args->count;
8,933,020✔
1971
  pCount->windowSliding = args->sliding;
8,933,020✔
1972
  pCount->pColList = args->pColList;
8,933,020✔
1973
  args->pColList = NULL;
8,933,020✔
1974
  nodesDestroyNode(arg);
8,933,020✔
1975
  return (SNode*)pCount;
8,932,542✔
1976
_err:
×
1977
  nodesDestroyNode((SNode*)pCount);
×
1978
  return NULL;
×
1979
}
1980

1981
SNode* createCountWindowArgs(SAstCreateContext* pCxt, const SToken* countToken, const SToken* slidingToken,
8,940,460✔
1982
                             SNodeList* colList) {
1983
  CHECK_PARSER_STATUS(pCxt);
8,940,460✔
1984

1985
  SCountWindowArgs* args = NULL;
8,940,460✔
1986
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW_ARGS, (SNode**)&args);
8,940,460✔
1987
  CHECK_MAKE_NODE(args);
8,942,300✔
1988
  args->count = taosStr2Int64(countToken->z, NULL, 10);
8,942,300✔
1989
  if (slidingToken && slidingToken->type == TK_NK_INTEGER) {
8,941,348✔
1990
    args->sliding = taosStr2Int64(slidingToken->z, NULL, 10);
49,222✔
1991
  } else {
1992
    args->sliding = taosStr2Int64(countToken->z, NULL, 10);
8,892,126✔
1993
  }
1994
  args->pColList = colList;
8,942,170✔
1995
  return (SNode*)args;
8,942,170✔
1996
_err:
×
1997
  return NULL;
×
1998
}
1999

2000
SNode* createAnomalyWindowNode(SAstCreateContext* pCxt, SNodeList* pExprList) {
×
2001
  SAnomalyWindowNode* pAnomaly = NULL;
×
2002
  CHECK_PARSER_STATUS(pCxt);
×
2003
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ANOMALY_WINDOW, (SNode**)&pAnomaly);
×
2004
  CHECK_MAKE_NODE(pAnomaly);
×
2005
  
2006
  pAnomaly->pCol = createPrimaryKeyCol(pCxt, NULL);
×
2007
  CHECK_MAKE_NODE(pAnomaly->pCol);
×
2008

2009
  pAnomaly->pExpr = pExprList;
×
2010

2011
  int32_t len = LIST_LENGTH(pExprList);
×
2012
  if (len == 1) {
×
2013
    tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
2014
  } else {
2015
    SNode* pNode = nodesListGetNode(pExprList, len - 1);
×
2016
    if (pNode != NULL && nodeType(pNode) == QUERY_NODE_VALUE) {
×
2017
      tstrncpy(pAnomaly->anomalyOpt, ((SValueNode*)pNode)->literal, TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
2018

2019
      SListCell* pCell = nodesListGetCell(pExprList, len - 1);
×
2020
      (void)nodesListErase(pExprList, pCell);
×
2021
      nodesDestroyNode(pNode);
×
2022
    } else {
2023
      tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
2024
    }
2025
  }
2026

2027
  return (SNode*)pAnomaly;
×
2028

2029
_err:
×
2030
  nodesDestroyNode((SNode*)pAnomaly);
×
2031
  return NULL;
×
2032
}
2033

2034
SNode* createIntervalWindowNodeExt(SAstCreateContext* pCxt, SNode* pInter, SNode* pSliding) {
295,372✔
2035
  SIntervalWindowNode* pInterval = NULL;
295,372✔
2036
  CHECK_PARSER_STATUS(pCxt);
295,372✔
2037
  if (pInter) {
295,372✔
2038
    pInterval = (SIntervalWindowNode*)pInter;
238,630✔
2039
  } else {
2040
    pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&pInterval);
56,742✔
2041
    CHECK_MAKE_NODE(pInterval);
56,742✔
2042
  }
2043
  pInterval->pCol = createPrimaryKeyCol(pCxt, NULL);
295,372✔
2044
  CHECK_MAKE_NODE(pInterval->pCol);
295,372✔
2045
  pInterval->pSliding = ((SSlidingWindowNode*)pSliding)->pSlidingVal;
295,372✔
2046
  pInterval->pSOffset = ((SSlidingWindowNode*)pSliding)->pOffset;
295,372✔
2047
  return (SNode*)pInterval;
295,372✔
2048
_err:
×
2049
  nodesDestroyNode((SNode*)pInter);
×
2050
  nodesDestroyNode((SNode*)pInterval);
×
2051
  nodesDestroyNode((SNode*)pSliding);
×
2052
  return NULL;
×
2053
}
2054

2055
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
77,875,864✔
2056
                                SNode* pFill) {
2057
  SIntervalWindowNode* interval = NULL;
77,875,864✔
2058
  CHECK_PARSER_STATUS(pCxt);
77,875,864✔
2059
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
77,875,864✔
2060
  CHECK_MAKE_NODE(interval);
77,894,818✔
2061
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
77,894,818✔
2062
  CHECK_MAKE_NODE(interval->pCol);
77,885,896✔
2063
  interval->pInterval = pInterval;
77,885,896✔
2064
  interval->pOffset = pOffset;
77,885,896✔
2065
  interval->pSliding = pSliding;
77,885,896✔
2066
  interval->pFill = pFill;
77,885,896✔
2067
  TAOS_SET_OBJ_ALIGNED(&interval->timeRange, TSWINDOW_INITIALIZER);
77,885,896✔
2068
  interval->timezone = pCxt->pQueryCxt->timezone;
77,885,896✔
2069
  return (SNode*)interval;
77,885,896✔
2070
_err:
×
2071
  nodesDestroyNode((SNode*)interval);
×
2072
  nodesDestroyNode(pInterval);
×
2073
  nodesDestroyNode(pOffset);
×
2074
  nodesDestroyNode(pSliding);
×
2075
  nodesDestroyNode(pFill);
×
2076
  return NULL;
4,870✔
2077
}
2078

2079
SNode* createPeriodWindowNode(SAstCreateContext* pCxt, SNode* pPeriodTime, SNode* pOffset) {
61,758✔
2080
  SPeriodWindowNode* pPeriod = NULL;
61,758✔
2081
  CHECK_PARSER_STATUS(pCxt);
61,758✔
2082
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PERIOD_WINDOW, (SNode**)&pPeriod);
61,758✔
2083
  CHECK_MAKE_NODE(pPeriod);
61,758✔
2084
  pPeriod->pOffset = pOffset;
61,758✔
2085
  pPeriod->pPeroid = pPeriodTime;
61,758✔
2086
  return (SNode*)pPeriod;
61,758✔
2087
_err:
×
2088
  nodesDestroyNode((SNode*)pOffset);
×
2089
  nodesDestroyNode((SNode*)pPeriodTime);
×
2090
  nodesDestroyNode((SNode*)pPeriod);
×
2091
  return NULL;
×
2092
}
2093

2094
SNode* createWindowOffsetNode(SAstCreateContext* pCxt, SNode* pStartOffset, SNode* pEndOffset) {
1,269,856✔
2095
  SWindowOffsetNode* winOffset = NULL;
1,269,856✔
2096
  CHECK_PARSER_STATUS(pCxt);
1,269,856✔
2097
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WINDOW_OFFSET, (SNode**)&winOffset);
1,269,856✔
2098
  CHECK_MAKE_NODE(winOffset);
1,269,856✔
2099
  winOffset->pStartOffset = pStartOffset;
1,269,856✔
2100
  winOffset->pEndOffset = pEndOffset;
1,269,856✔
2101
  return (SNode*)winOffset;
1,269,856✔
2102
_err:
×
2103
  nodesDestroyNode((SNode*)winOffset);
×
2104
  nodesDestroyNode(pStartOffset);
×
2105
  nodesDestroyNode(pEndOffset);
×
2106
  return NULL;
×
2107
}
2108

2109
SNode* createSurroundNode(SAstCreateContext* pCxt, SNode* pSurroundingTime,
159,888✔
2110
                          SNode* pValues) {
2111
  SSurroundNode* pSurround = NULL;
159,888✔
2112
  CHECK_PARSER_STATUS(pCxt);
159,888✔
2113
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SURROUND, (SNode**)&pSurround);
159,888✔
2114
  CHECK_MAKE_NODE(pSurround);
159,888✔
2115
  pSurround->pSurroundingTime = pSurroundingTime;
159,888✔
2116
  pSurround->pValues = pValues;
159,888✔
2117
  return (SNode*)pSurround;
159,888✔
2118
_err:
×
2119
  nodesDestroyNode((SNode*)pSurround);
×
2120
  nodesDestroyNode(pSurroundingTime);
×
2121
  nodesDestroyNode(pValues);
×
2122
  return NULL;
×
2123
}
2124

2125
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
8,265,916✔
2126
  return createFillNodeWithSurroundingTime(pCxt, mode, pValues, NULL);
8,265,916✔
2127
}
2128

2129
SNode* createFillNodeWithSurroundNode(SAstCreateContext* pCxt, EFillMode mode,
3,903,028✔
2130
                                      SNode* pSurroundNode) {
2131
  if (pSurroundNode == NULL) {
3,903,028✔
2132
    return createFillNode(pCxt, mode, NULL);
3,743,140✔
2133
  }
2134

2135
  SSurroundNode* pSurround = (SSurroundNode*)pSurroundNode;
159,888✔
2136
  SNode* pSurroundingTime = pSurround->pSurroundingTime;
159,888✔
2137
  SNode* pValues = pSurround->pValues;
159,888✔
2138

2139
  /* set surround node to NULL to avoid freeing before using */
2140
  pSurround->pSurroundingTime = NULL;
159,888✔
2141
  pSurround->pValues = NULL;
159,888✔
2142
  nodesDestroyNode((SNode*)pSurround);
159,888✔
2143

2144
  return createFillNodeWithSurroundingTime(pCxt, mode, pValues,
159,888✔
2145
                                           pSurroundingTime);
2146
}
2147

2148
SNode* createFillNodeWithSurroundingTime(SAstCreateContext* pCxt,
8,427,608✔
2149
                                         EFillMode mode, SNode* pValues,
2150
                                         SNode* pSurroundingTime) {
2151
  SFillNode* fill = NULL;
8,427,608✔
2152
  CHECK_PARSER_STATUS(pCxt);
8,428,424✔
2153
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
8,424,816✔
2154
  CHECK_MAKE_NODE(fill);
8,428,424✔
2155
  fill->mode = mode;
8,428,424✔
2156
  fill->pValues = pValues;
8,427,608✔
2157
  fill->pSurroundingTime = pSurroundingTime;
8,426,792✔
2158
  fill->pWStartTs = NULL;
8,426,620✔
2159
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION,
8,450,762✔
2160
                                (SNode**)&(fill->pWStartTs));
8,423,012✔
2161
  CHECK_MAKE_NODE(fill->pWStartTs);
8,428,424✔
2162
  tstrncpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart",
8,426,792✔
2163
           TSDB_FUNC_NAME_LEN);
2164
  return (SNode*)fill;
8,426,620✔
2165
_err:
×
2166
  nodesDestroyNode((SNode*)fill);
×
2167
  nodesDestroyNode(pValues);
×
2168
  nodesDestroyNode(pSurroundingTime);
×
2169
  return NULL;
×
2170
}
2171

2172
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
111,526,758✔
2173
  SGroupingSetNode* groupingSet = NULL;
111,526,758✔
2174
  CHECK_PARSER_STATUS(pCxt);
111,526,758✔
2175
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
111,526,758✔
2176
  CHECK_MAKE_NODE(groupingSet);
111,537,358✔
2177
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
111,537,358✔
2178
  groupingSet->pParameterList = NULL;
111,537,358✔
2179
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
111,537,358✔
2180
  CHECK_PARSER_STATUS(pCxt);
111,537,372✔
2181
  return (SNode*)groupingSet;
111,537,372✔
2182
_err:
×
2183
  nodesDestroyNode((SNode*)groupingSet);
×
2184
  nodesDestroyNode(pNode);
×
2185
  return NULL;
658✔
2186
}
2187

2188
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
7,058,108✔
2189
  CHECK_PARSER_STATUS(pCxt);
7,058,108✔
2190
  if (isSubQueryNode(pStart) || isSubQueryNode(pEnd) || isSubQueryNode(pInterval)) {
7,058,924✔
2191
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
1,472✔
2192
    CHECK_PARSER_STATUS(pCxt);
1,472✔
2193
  }
2194

2195
  if (NULL == pInterval) {
7,053,372✔
2196
    if (pEnd && nodeType(pEnd) == QUERY_NODE_VALUE && ((SValueNode*)pEnd)->flag & VALUE_FLAG_IS_DURATION) {
6,815,956✔
2197
      return createInterpTimeAround(pCxt, pStart, NULL, pEnd);
2,810,340✔
2198
    }
2199
    return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
4,006,432✔
2200
  }
2201

2202
  return createInterpTimeAround(pCxt, pStart, pEnd, pInterval);
237,416✔
2203

2204
_err:
1,472✔
2205

2206
  nodesDestroyNode(pStart);
1,472✔
2207
  nodesDestroyNode(pEnd);
1,472✔
2208
  nodesDestroyNode(pInterval);
1,472✔
2209
  return NULL;
1,472✔
2210
}
2211

2212
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
3,071,552✔
2213
  CHECK_PARSER_STATUS(pCxt);
3,071,552✔
2214
  if (isSubQueryNode(pPoint)) {
3,070,564✔
2215
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
736✔
2216
    CHECK_PARSER_STATUS(pCxt);
736✔
2217
  }
2218

2219
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
3,069,828✔
2220
_err:
736✔
2221
  nodesDestroyNode(pPoint);
736✔
2222
  return NULL;
736✔
2223
}
2224

2225
SNode* createInterpTimeAround(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
3,049,560✔
2226
  CHECK_PARSER_STATUS(pCxt);
3,049,560✔
2227
  SRangeAroundNode* pAround = NULL;
3,044,792✔
2228
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RANGE_AROUND, (SNode**)&pAround);
3,045,780✔
2229
  CHECK_PARSER_STATUS(pCxt);
3,049,560✔
2230
  if (NULL == pEnd) {
3,047,584✔
2231
    pAround->pRange = createInterpTimePoint(pCxt, pStart);
2,810,168✔
2232
  } else {
2233
    pAround->pRange = createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
237,416✔
2234
  }
2235
  pAround->pInterval = pInterval;
3,046,940✔
2236
  CHECK_PARSER_STATUS(pCxt);
3,043,976✔
2237
  return (SNode*)pAround;
3,048,572✔
2238
_err:
×
2239
  return NULL;
×
2240
}
2241

2242
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
63,949,880✔
2243
  CHECK_PARSER_STATUS(pCxt);
63,949,880✔
2244
  SWhenThenNode* pWhenThen = NULL;
63,949,880✔
2245
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
63,949,880✔
2246
  CHECK_MAKE_NODE(pWhenThen);
63,960,066✔
2247
  pWhenThen->pWhen = pWhen;
63,960,066✔
2248
  pWhenThen->pThen = pThen;
63,960,066✔
2249
  return (SNode*)pWhenThen;
63,960,066✔
2250
_err:
×
2251
  nodesDestroyNode(pWhen);
×
2252
  nodesDestroyNode(pThen);
×
2253
  return NULL;
×
2254
}
2255

2256
static int32_t debugPrintNode(SNode* pNode) {
×
2257
  char*   pStr = NULL;
×
2258
  int32_t code = nodesNodeToString(pNode, false, &pStr, NULL);
×
2259
  if (TSDB_CODE_SUCCESS == code) {
×
2260
    (void)printf("%s\n", pStr);
×
2261
    taosMemoryFree(pStr);
×
2262
  }
2263
  return code;
×
2264
}
2265

2266
SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
62,227,744✔
2267
  CHECK_PARSER_STATUS(pCxt);
62,227,744✔
2268
  SCaseWhenNode* pCaseWhen = NULL;
62,227,744✔
2269
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CASE_WHEN, (SNode**)&pCaseWhen);
62,227,744✔
2270
  CHECK_MAKE_NODE(pCaseWhen);
62,239,926✔
2271
  pCaseWhen->pCase = pCase;
62,239,926✔
2272
  pCaseWhen->pWhenThenList = pWhenThenList;
62,239,926✔
2273
  pCaseWhen->pElse = pElse;
62,239,926✔
2274
  pCaseWhen->tz = pCxt->pQueryCxt->timezone;
62,239,926✔
2275
  pCaseWhen->charsetCxt = pCxt->pQueryCxt->charsetCxt;
62,239,926✔
2276
  // debugPrintNode((SNode*)pCaseWhen);
2277
  return (SNode*)pCaseWhen;
62,239,926✔
2278
_err:
×
2279
  nodesDestroyNode(pCase);
×
2280
  nodesDestroyList(pWhenThenList);
×
2281
  nodesDestroyNode(pElse);
×
2282
  return NULL;
×
2283
}
2284

2285
SNode* createNullIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
6,096✔
2286
  SNode *    pCase = NULL, *pEqual = NULL, *pThen = NULL;
6,096✔
2287
  SNode *    pWhenThenNode = NULL, *pElse = NULL;
6,096✔
2288
  SNodeList* pWhenThenList = NULL;
6,096✔
2289
  SNode*     pCaseWhen = NULL;
6,096✔
2290

2291
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2292
  pEqual = createOperatorNode(pCxt, OP_TYPE_EQUAL, pExpr1, pExpr2);
6,096✔
2293
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2294
  SToken nullToken = {
6,096✔
2295
      .n = 4,
2296
      .type = TK_NULL,
2297
      .z = "null",
2298
  };
2299
  pThen = createValueNode(pCxt, TSDB_DATA_TYPE_NULL, &nullToken);
6,096✔
2300
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2301
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pThen);
6,096✔
2302
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2303
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
6,096✔
2304
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2305
  pCxt->errCode = nodesCloneNode(pExpr1, &pElse);
6,096✔
2306
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2307
  pCaseWhen = createCaseWhenNode(pCxt, pCase, pWhenThenList, pElse);
6,096✔
2308
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2309
  // debugPrintNode((SNode*)pCaseWhen);
2310
  return (SNode*)pCaseWhen;
6,096✔
2311
_err:
×
2312
  nodesDestroyNode(pCase);
×
2313
  nodesDestroyNode(pEqual);
×
2314
  nodesDestroyNode(pThen);
×
2315
  nodesDestroyNode(pWhenThenNode);
×
2316
  nodesDestroyNode(pElse);
×
2317
  nodesDestroyList(pWhenThenList);
×
2318
  return NULL;
×
2319
}
2320

2321
SNode* createIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
135,684✔
2322
  SNode*     pCase = NULL;
135,684✔
2323
  SNode*     pWhenThenNode = NULL;
135,684✔
2324
  SNodeList* pWhenThenList = NULL;
135,684✔
2325
  SNode*     pCaseWhen = NULL;
135,684✔
2326

2327
  CHECK_PARSER_STATUS(pCxt);
135,684✔
2328
  pWhenThenNode = createWhenThenNode(pCxt, pExpr1, pExpr2);
135,684✔
2329
  CHECK_PARSER_STATUS(pCxt);
135,684✔
2330
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
135,684✔
2331
  CHECK_PARSER_STATUS(pCxt);
135,684✔
2332
  pCaseWhen = createCaseWhenNode(pCxt, pCase, pWhenThenList, pExpr3);
135,684✔
2333
  CHECK_PARSER_STATUS(pCxt);
135,684✔
2334
  // debugPrintNode((SNode*)pCaseWhen);
2335
  return (SNode*)pCaseWhen;
135,684✔
2336
_err:
×
2337
  nodesDestroyNode(pCase);
×
2338
  nodesDestroyNode(pWhenThenNode);
×
2339
  nodesDestroyList(pWhenThenList);
×
2340
  return NULL;
×
2341
}
2342

2343
SNode* createNvlNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
21,680✔
2344
  SNode *    pThen = NULL, *pEqual = NULL;
21,680✔
2345
  SNode*     pWhenThenNode = NULL;
21,680✔
2346
  SNodeList* pWhenThenList = NULL;
21,680✔
2347
  SNode*     pCaseWhen = NULL;
21,680✔
2348

2349
  CHECK_PARSER_STATUS(pCxt);
21,680✔
2350
  pEqual = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr1, NULL);
21,680✔
2351
  CHECK_PARSER_STATUS(pCxt);
21,680✔
2352
  pCxt->errCode = nodesCloneNode(pExpr1, &pThen);
21,680✔
2353
  CHECK_PARSER_STATUS(pCxt);
21,680✔
2354
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pThen);
21,680✔
2355
  CHECK_PARSER_STATUS(pCxt);
21,680✔
2356
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
21,680✔
2357
  CHECK_PARSER_STATUS(pCxt);
21,680✔
2358
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, pExpr2);
21,680✔
2359
  CHECK_PARSER_STATUS(pCxt);
21,680✔
2360
  // debugPrintNode((SNode*)pCaseWhen);
2361
  return (SNode*)pCaseWhen;
21,680✔
2362
_err:
×
2363
  nodesDestroyNode(pEqual);
×
2364
  nodesDestroyNode(pThen);
×
2365
  nodesDestroyNode(pWhenThenNode);
×
2366
  nodesDestroyList(pWhenThenList);
×
2367
  return NULL;
×
2368
}
2369

2370
SNode* createNvl2Node(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
6,096✔
2371
  SNode *    pEqual = NULL, *pWhenThenNode = NULL;
6,096✔
2372
  SNodeList* pWhenThenList = NULL;
6,096✔
2373
  SNode*     pCaseWhen = NULL;
6,096✔
2374

2375
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2376
  pEqual = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr1, NULL);
6,096✔
2377
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2378
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pExpr2);
6,096✔
2379
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2380
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
6,096✔
2381
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2382
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, pExpr3);
6,096✔
2383
  CHECK_PARSER_STATUS(pCxt);
6,096✔
2384
  // debugPrintNode((SNode*)pCaseWhen);
2385
  return (SNode*)pCaseWhen;
6,096✔
2386
_err:
×
2387
  nodesDestroyNode(pEqual);
×
2388
  nodesDestroyNode(pWhenThenNode);
×
2389
  nodesDestroyList(pWhenThenList);
×
2390
  return NULL;
×
2391
}
2392

2393
SNode* createCoalesceNode(SAstCreateContext* pCxt, SNodeList* pParamList) {
3,756✔
2394
  int32_t    sizeParam = LIST_LENGTH(pParamList);
3,756✔
2395
  SNode *    pNotNullCond = NULL, *pWhenThenNode = NULL, *pExpr = NULL;
3,756✔
2396
  SNodeList* pWhenThenList = NULL;
3,756✔
2397
  SNode *    pCaseWhen = NULL, *pThen = NULL;
3,756✔
2398

2399
  CHECK_PARSER_STATUS(pCxt);
3,756✔
2400

2401
  for (int i = 0; i < sizeParam; ++i) {
13,146✔
2402
    pExpr = nodesListGetNode(pParamList, i);
9,390✔
2403

2404
    pCxt->errCode = nodesCloneNode(pExpr, &pThen);
9,390✔
2405
    CHECK_PARSER_STATUS(pCxt);
9,390✔
2406

2407
    pNotNullCond = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr, NULL);
9,390✔
2408
    CHECK_PARSER_STATUS(pCxt);
9,390✔
2409

2410
    pWhenThenNode = createWhenThenNode(pCxt, pNotNullCond, pThen);
9,390✔
2411
    CHECK_PARSER_STATUS(pCxt);
9,390✔
2412

2413
    if (!pWhenThenList) {
9,390✔
2414
      pWhenThenList = createNodeList(pCxt, pWhenThenNode);
3,756✔
2415
    } else {
2416
      pCxt->errCode = nodesListAppend(pWhenThenList, pWhenThenNode);
5,634✔
2417
    }
2418
    CHECK_PARSER_STATUS(pCxt);
9,390✔
2419
  }
2420

2421
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, NULL);
3,756✔
2422
  CHECK_PARSER_STATUS(pCxt);
3,756✔
2423
  // debugPrintNode((SNode*)pCaseWhen);
2424
  return (SNode*)pCaseWhen;
3,756✔
2425
_err:
×
2426
  nodesDestroyNode(pExpr);
×
2427
  nodesDestroyNode(pNotNullCond);
×
2428
  nodesDestroyNode(pThen);
×
2429
  nodesDestroyNode(pWhenThenNode);
×
2430
  nodesDestroyList(pWhenThenList);
×
2431
  return NULL;
×
2432
}
2433

2434
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
181,934,134✔
2435
  CHECK_PARSER_STATUS(pCxt);
181,934,134✔
2436
  trimEscape(pCxt, pAlias, false);
181,934,134✔
2437
  SExprNode* pExpr = (SExprNode*)pNode;
181,935,716✔
2438
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
181,935,716✔
2439
  strncpy(pExpr->aliasName, pAlias->z, len);
181,935,716✔
2440
  pExpr->aliasName[len] = '\0';
181,935,716✔
2441
  strncpy(pExpr->userAlias, pAlias->z, len);
181,935,716✔
2442
  pExpr->userAlias[len] = '\0';
181,935,716✔
2443
  pExpr->asAlias = true;
181,935,716✔
2444
  return pNode;
181,935,716✔
2445
_err:
×
2446
  nodesDestroyNode(pNode);
×
2447
  return NULL;
×
2448
}
2449

2450
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
1,363,039,972✔
2451
  CHECK_PARSER_STATUS(pCxt);
1,363,039,972✔
2452
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,363,035,902✔
2453
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
1,363,080,698✔
2454
  }
2455
  return pStmt;
1,363,025,414✔
2456
_err:
2,444✔
2457
  nodesDestroyNode(pStmt);
2,444✔
2458
  nodesDestroyNode(pWhere);
2,444✔
2459
  return NULL;
2,444✔
2460
}
2461

2462
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
1,363,033,170✔
2463
  CHECK_PARSER_STATUS(pCxt);
1,363,033,170✔
2464
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,363,029,842✔
2465
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
1,363,069,970✔
2466
  }
2467
  return pStmt;
1,363,020,792✔
2468
_err:
2,444✔
2469
  nodesDestroyNode(pStmt);
2,444✔
2470
  nodesDestroyList(pPartitionByList);
2,444✔
2471
  return NULL;
2,444✔
2472
}
2473

2474
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
1,363,025,204✔
2475
  CHECK_PARSER_STATUS(pCxt);
1,363,025,204✔
2476
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,363,024,838✔
2477
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
1,363,062,286✔
2478
  }
2479
  return pStmt;
1,363,013,458✔
2480
_err:
2,444✔
2481
  nodesDestroyNode(pStmt);
2,444✔
2482
  nodesDestroyNode(pWindow);
2,444✔
2483
  return NULL;
2,444✔
2484
}
2485

2486
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
1,363,027,344✔
2487
  CHECK_PARSER_STATUS(pCxt);
1,363,027,344✔
2488
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,363,024,396✔
2489
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
1,363,058,430✔
2490
  }
2491
  return pStmt;
1,363,022,454✔
2492
_err:
2,444✔
2493
  nodesDestroyNode(pStmt);
2,444✔
2494
  nodesDestroyList(pGroupByList);
2,444✔
2495
  return NULL;
2,444✔
2496
}
2497

2498
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
1,363,022,136✔
2499
  CHECK_PARSER_STATUS(pCxt);
1,363,022,136✔
2500
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,363,015,704✔
2501
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
1,363,060,960✔
2502
  }
2503
  return pStmt;
1,363,018,540✔
2504
_err:
2,444✔
2505
  nodesDestroyNode(pStmt);
2,444✔
2506
  nodesDestroyNode(pHaving);
2,444✔
2507
  return NULL;
2,444✔
2508
}
2509

2510
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
1,297,253,780✔
2511
  CHECK_PARSER_STATUS(pCxt);
1,297,253,780✔
2512
  if (NULL == pOrderByList) {
1,297,253,710✔
2513
    return pStmt;
1,028,175,232✔
2514
  }
2515
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
269,078,478✔
2516
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
241,874,724✔
2517
  } else {
2518
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
27,203,754✔
2519
  }
2520
  return pStmt;
269,076,674✔
2521
_err:
2,444✔
2522
  nodesDestroyNode(pStmt);
2,444✔
2523
  nodesDestroyList(pOrderByList);
2,444✔
2524
  return NULL;
2,444✔
2525
}
2526

2527
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
1,297,258,560✔
2528
  CHECK_PARSER_STATUS(pCxt);
1,297,258,560✔
2529
  if (NULL == pSlimit) {
1,297,257,862✔
2530
    return pStmt;
1,292,628,538✔
2531
  }
2532
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
4,647,390✔
2533
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
4,651,394✔
2534
  }
2535
  return pStmt;
4,647,390✔
2536
_err:
2,444✔
2537
  nodesDestroyNode(pStmt);
2,444✔
2538
  nodesDestroyNode(pSlimit);
2,444✔
2539
  return NULL;
2,444✔
2540
}
2541

2542
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
1,297,238,102✔
2543
  CHECK_PARSER_STATUS(pCxt);
1,297,238,102✔
2544
  if (NULL == pLimit) {
1,297,237,866✔
2545
    return pStmt;
1,244,519,558✔
2546
  }
2547
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
52,718,308✔
2548
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
50,601,996✔
2549
  } else {
2550
    ((SSetOperator*)pStmt)->pLimit = pLimit;
2,139,852✔
2551
  }
2552
  return pStmt;
52,718,308✔
2553
_err:
2,444✔
2554
  nodesDestroyNode(pStmt);
2,444✔
2555
  nodesDestroyNode(pLimit);
2,444✔
2556
  return NULL;
2,444✔
2557
}
2558

2559
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
1,363,015,742✔
2560
  CHECK_PARSER_STATUS(pCxt);
1,363,015,742✔
2561
  SSelectStmt* pSelect = (SSelectStmt*)pStmt;
1,363,014,952✔
2562
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,363,014,952✔
2563
    if (pRange && nodeType(pRange) == QUERY_NODE_RANGE_AROUND) {
1,363,059,926✔
2564
      pSelect->pRangeAround = pRange;
3,032,660✔
2565
      SRangeAroundNode* pAround = (SRangeAroundNode*)pRange;
3,035,448✔
2566
      TSWAP(pSelect->pRange, pAround->pRange);
3,035,448✔
2567
    } else {
2568
      pSelect->pRange = pRange;
1,360,026,450✔
2569
    }
2570
  }
2571
  return pStmt;
1,363,010,422✔
2572
_err:
2,444✔
2573
  nodesDestroyNode(pStmt);
2,444✔
2574
  nodesDestroyNode(pRange);
2,444✔
2575
  return NULL;
2,444✔
2576
}
2577

2578
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
1,363,019,412✔
2579
  CHECK_PARSER_STATUS(pCxt);
1,363,019,412✔
2580
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,363,021,706✔
2581
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
1,363,059,902✔
2582
  }
2583
  return pStmt;
1,363,019,530✔
2584
_err:
2,444✔
2585
  nodesDestroyNode(pStmt);
2,444✔
2586
  nodesDestroyNode(pEvery);
2,444✔
2587
  return NULL;
2,444✔
2588
}
2589

2590
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
1,363,024,474✔
2591
  CHECK_PARSER_STATUS(pCxt);
1,363,024,474✔
2592
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
1,363,027,288✔
2593
    SFillNode* pFillClause = (SFillNode*)pFill;
7,327,812✔
2594
    nodesDestroyNode(pFillClause->pWStartTs);
7,327,812✔
2595
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
7,328,156✔
2596
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
7,328,972✔
2597
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
7,328,972✔
2598
  }
2599
  return pStmt;
1,363,021,432✔
2600
_err:
2,444✔
2601
  nodesDestroyNode(pStmt);
2,444✔
2602
  nodesDestroyNode(pFill);
2,444✔
2603
  return NULL;
2,444✔
2604
}
2605

2606
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
69,633,032✔
2607
  CHECK_PARSER_STATUS(pCxt);
69,633,032✔
2608
  if (NULL == pJLimit) {
69,633,032✔
2609
    return pJoin;
69,194,596✔
2610
  }
2611
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
438,436✔
2612
  pJoinNode->pJLimit = pJLimit;
438,436✔
2613

2614
  return pJoin;
438,436✔
2615
_err:
×
2616
  nodesDestroyNode(pJoin);
×
2617
  nodesDestroyNode(pJLimit);
×
2618
  return NULL;
×
2619
}
2620

2621
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
69,632,066✔
2622
  CHECK_PARSER_STATUS(pCxt);
69,632,066✔
2623
  if (NULL == pWinOffset) {
69,632,066✔
2624
    return pJoin;
68,383,886✔
2625
  }
2626
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
1,248,570✔
2627
  pJoinNode->pWindowOffset = pWinOffset;
1,248,570✔
2628

2629
  return pJoin;
1,248,570✔
2630
_err:
×
2631
  nodesDestroyNode(pJoin);
×
2632
  nodesDestroyNode(pWinOffset);
×
2633
  return NULL;
×
2634
}
2635

2636
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
1,363,059,782✔
2637
                        SNodeList* pHint) {
2638
  CHECK_PARSER_STATUS(pCxt);
1,363,059,782✔
2639
  SNode* select = NULL;
1,363,054,912✔
2640
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
1,363,054,962✔
2641
  CHECK_MAKE_NODE(select);
1,363,051,292✔
2642
  return select;
1,363,051,292✔
2643
_err:
2,444✔
2644
  nodesDestroyList(pProjectionList);
2,444✔
2645
  nodesDestroyNode(pTable);
2,444✔
2646
  nodesDestroyList(pHint);
2,444✔
2647
  return NULL;
18,320✔
2648
}
2649

2650
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
1,363,049,838✔
2651
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,363,049,838✔
2652
    if (pCxt->pQueryCxt->biMode) {
1,363,086,916✔
2653
      ((SSelectStmt*)pStmt)->tagScan = true;
14,348✔
2654
    } else {
2655
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
1,363,064,828✔
2656
    }
2657
  }
2658
  return pStmt;
1,363,041,290✔
2659
}
2660

2661
static void setSubquery(SNode* pStmt) {
162,555,688✔
2662
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
162,555,688✔
2663
    ((SSelectStmt*)pStmt)->isSubquery = true;
161,993,266✔
2664
  }
2665
}
162,555,688✔
2666

2667
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
81,276,426✔
2668
  CHECK_PARSER_STATUS(pCxt);
81,276,426✔
2669
  SSetOperator* setOp = NULL;
81,276,426✔
2670
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
81,276,426✔
2671
  CHECK_MAKE_NODE(setOp);
81,279,908✔
2672
  setOp->opType = type;
81,279,908✔
2673
  setOp->pLeft = pLeft;
81,279,908✔
2674
  setSubquery(setOp->pLeft);
81,279,908✔
2675
  setOp->pRight = pRight;
81,280,418✔
2676
  setSubquery(setOp->pRight);
81,280,418✔
2677
  snprintf(setOp->stmtName, TSDB_TABLE_NAME_LEN, "%p", setOp);
81,281,486✔
2678
  return (SNode*)setOp;
81,281,486✔
2679
_err:
×
2680
  nodesDestroyNode(pLeft);
×
2681
  nodesDestroyNode(pRight);
×
2682
  return NULL;
132✔
2683
}
2684

2685
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
4,042,846✔
2686
  if (!pOptions->walRetentionPeriodIsSet) {
4,042,846✔
2687
    pOptions->walRetentionPeriod =
4,022,640✔
2688
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
2689
  }
2690
  if (!pOptions->walRetentionSizeIsSet) {
4,042,660✔
2691
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
4,041,586✔
2692
  }
2693
  if (!pOptions->walRollPeriodIsSet) {
4,043,032✔
2694
    pOptions->walRollPeriod =
4,043,032✔
2695
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
2696
  }
2697
}
4,042,660✔
2698

2699
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
2,999,116✔
2700
  CHECK_PARSER_STATUS(pCxt);
2,999,116✔
2701
  SDatabaseOptions* pOptions = NULL;
2,999,116✔
2702
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
2,999,116✔
2703
  CHECK_MAKE_NODE(pOptions);
2,998,930✔
2704
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
2,998,930✔
2705
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
2,998,930✔
2706
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
2,998,930✔
2707
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
2,998,930✔
2708
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
2,999,116✔
2709
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
2,998,930✔
2710
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
2,999,116✔
2711
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
2,998,930✔
2712
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
2,999,116✔
2713
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
2,998,930✔
2714
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
2,998,930✔
2715
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
2,998,930✔
2716
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
2,999,116✔
2717
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
2,999,116✔
2718
  pOptions->precision = TSDB_DEFAULT_PRECISION;
2,998,930✔
2719
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
2,999,116✔
2720
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
2,999,116✔
2721
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
2,998,930✔
2722
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
2,999,116✔
2723
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
2,998,930✔
2724
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
2,999,116✔
2725
  updateWalOptionsDefault(pOptions);
2,999,116✔
2726
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
2,998,930✔
2727
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
2,998,930✔
2728
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
2,998,930✔
2729
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
2,998,930✔
2730
  pOptions->ssChunkSize = TSDB_DEFAULT_SS_CHUNK_SIZE;
2,999,116✔
2731
  pOptions->ssKeepLocal = TSDB_DEFAULT_SS_KEEP_LOCAL;
2,999,116✔
2732
  pOptions->ssCompact = TSDB_DEFAULT_SS_COMPACT;
2,998,930✔
2733
  pOptions->withArbitrator = TSDB_DEFAULT_DB_WITH_ARBITRATOR;
2,999,116✔
2734
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
2,998,930✔
2735
  pOptions->dnodeListStr[0] = 0;
2,998,930✔
2736
  pOptions->compactInterval = TSDB_DEFAULT_COMPACT_INTERVAL;
2,998,930✔
2737
  pOptions->compactStartTime = TSDB_DEFAULT_COMPACT_START_TIME;
2,999,116✔
2738
  pOptions->compactEndTime = TSDB_DEFAULT_COMPACT_END_TIME;
2,999,116✔
2739
  pOptions->compactTimeOffset = TSDB_DEFAULT_COMPACT_TIME_OFFSET;
2,999,116✔
2740
  pOptions->encryptAlgorithmStr[0] = 0;
2,998,930✔
2741
  pOptions->isAudit = 0;
2,999,116✔
2742
  pOptions->secureDelete = 0;
2,999,116✔
2743
  return (SNode*)pOptions;
2,998,930✔
UNCOV
2744
_err:
×
2745
  return NULL;
×
2746
}
2747

2748
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
424,742✔
2749
  CHECK_PARSER_STATUS(pCxt);
424,742✔
2750
  SDatabaseOptions* pOptions = NULL;
424,742✔
2751
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
424,742✔
2752
  CHECK_MAKE_NODE(pOptions);
424,742✔
2753
  pOptions->buffer = -1;
424,742✔
2754
  pOptions->cacheModel = -1;
424,742✔
2755
  pOptions->cacheLastSize = -1;
424,742✔
2756
  pOptions->compressionLevel = -1;
424,742✔
2757
  pOptions->daysPerFile = -1;
424,742✔
2758
  pOptions->fsyncPeriod = -1;
424,742✔
2759
  pOptions->maxRowsPerBlock = -1;
424,742✔
2760
  pOptions->minRowsPerBlock = -1;
424,742✔
2761
  pOptions->keep[0] = -1;
424,742✔
2762
  pOptions->keep[1] = -1;
424,742✔
2763
  pOptions->keep[2] = -1;
424,742✔
2764
  pOptions->pages = -1;
424,742✔
2765
  pOptions->pagesize = -1;
424,742✔
2766
  pOptions->tsdbPageSize = -1;
424,742✔
2767
  pOptions->precision = -1;
424,742✔
2768
  pOptions->replica = -1;
424,742✔
2769
  pOptions->strict = -1;
424,742✔
2770
  pOptions->walLevel = -1;
424,742✔
2771
  pOptions->numOfVgroups = -1;
424,742✔
2772
  pOptions->singleStable = -1;
424,742✔
2773
  pOptions->schemaless = -1;
424,742✔
2774
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
424,742✔
2775
  pOptions->walRetentionSize = -2;    // -1 is a valid value
424,742✔
2776
  pOptions->walRollPeriod = -1;
424,742✔
2777
  pOptions->walSegmentSize = -1;
424,742✔
2778
  pOptions->sstTrigger = -1;
424,742✔
2779
  pOptions->tablePrefix = -1;
424,742✔
2780
  pOptions->tableSuffix = -1;
424,742✔
2781
  pOptions->ssChunkSize = -1;
424,742✔
2782
  pOptions->ssKeepLocal = -1;
424,742✔
2783
  pOptions->ssCompact = -1;
424,742✔
2784
  pOptions->withArbitrator = -1;
424,742✔
2785
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
424,742✔
2786
  pOptions->dnodeListStr[0] = 0;
424,742✔
2787
  pOptions->compactInterval = -1;
424,742✔
2788
  pOptions->compactStartTime = -1;
424,742✔
2789
  pOptions->compactEndTime = -1;
424,742✔
2790
  pOptions->compactTimeOffset = -1;
424,742✔
2791
  pOptions->encryptAlgorithmStr[0] = 0;
424,742✔
2792
  pOptions->isAudit = -1;
424,742✔
2793
  pOptions->allowDrop = -1;
424,742✔
2794
  pOptions->secureDelete = -1;
424,742✔
2795
  return (SNode*)pOptions;
424,742✔
UNCOV
2796
_err:
×
UNCOV
2797
  return NULL;
×
2798
}
2799

2800
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
4,855,250✔
2801
                                    bool alter) {
2802
  CHECK_PARSER_STATUS(pCxt);
4,855,250✔
2803
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
4,855,250✔
2804
  switch (type) {
4,855,250✔
2805
    case DB_OPTION_BUFFER:
117,316✔
2806
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
117,316✔
2807
      break;
117,316✔
2808
    case DB_OPTION_CACHEMODEL:
136,990✔
2809
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
136,990✔
2810
      break;
136,990✔
2811
    case DB_OPTION_CACHESIZE:
27,838✔
2812
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
27,838✔
2813
      break;
27,838✔
2814
    case DB_OPTION_COMP:
34,026✔
2815
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
34,026✔
2816
      break;
34,026✔
2817
    case DB_OPTION_DAYS: {
608,432✔
2818
      SToken* pToken = pVal;
608,432✔
2819
      if (TK_NK_INTEGER == pToken->type) {
608,432✔
2820
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
489,146✔
2821
      } else {
2822
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
119,286✔
2823
      }
2824
      break;
608,432✔
2825
    }
2826
    case DB_OPTION_FSYNC:
68,506✔
2827
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
68,506✔
2828
      break;
68,506✔
2829
    case DB_OPTION_MAXROWS:
63,266✔
2830
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
63,266✔
2831
      break;
63,266✔
2832
    case DB_OPTION_MINROWS:
73,610✔
2833
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
73,610✔
2834
      break;
73,610✔
2835
    case DB_OPTION_KEEP:
410,202✔
2836
      pDbOptions->pKeep = pVal;
410,202✔
2837
      break;
410,202✔
2838
    case DB_OPTION_PAGES:
31,142✔
2839
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
31,142✔
2840
      break;
31,142✔
2841
    case DB_OPTION_PAGESIZE:
12,180✔
2842
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
12,180✔
2843
      break;
12,180✔
2844
    case DB_OPTION_TSDB_PAGESIZE:
9,418✔
2845
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
9,418✔
2846
      break;
9,418✔
2847
    case DB_OPTION_PRECISION:
237,778✔
2848
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
237,778✔
2849
      break;
237,778✔
2850
    case DB_OPTION_REPLICA:
1,067,530✔
2851
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
1,067,530✔
2852
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
1,067,344✔
2853
      if (!alter) {
1,067,530✔
2854
        updateWalOptionsDefault(pDbOptions);
1,043,916✔
2855
      }
2856
      break;
1,067,530✔
UNCOV
2857
    case DB_OPTION_STRICT:
×
UNCOV
2858
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
2859
      break;
×
2860
    case DB_OPTION_WAL:
95,910✔
2861
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
95,910✔
2862
      break;
95,910✔
2863
    case DB_OPTION_VGROUPS:
1,052,408✔
2864
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
1,052,408✔
2865
      break;
1,052,408✔
2866
    case DB_OPTION_SINGLE_STABLE:
15,032✔
2867
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
15,032✔
2868
      break;
15,032✔
2869
    case DB_OPTION_RETENTIONS:
5,432✔
2870
      pDbOptions->pRetentions = pVal;
5,432✔
2871
      break;
5,432✔
2872
    case DB_OPTION_WAL_RETENTION_PERIOD:
177,898✔
2873
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
177,898✔
2874
      pDbOptions->walRetentionPeriodIsSet = true;
177,898✔
2875
      break;
177,898✔
2876
    case DB_OPTION_WAL_RETENTION_SIZE:
37,034✔
2877
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
37,034✔
2878
      pDbOptions->walRetentionSizeIsSet = true;
37,034✔
2879
      break;
37,034✔
2880
    case DB_OPTION_WAL_ROLL_PERIOD:
776✔
2881
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
776✔
2882
      pDbOptions->walRollPeriodIsSet = true;
776✔
2883
      break;
776✔
2884
    case DB_OPTION_WAL_SEGMENT_SIZE:
922✔
2885
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
922✔
2886
      break;
922✔
2887
    case DB_OPTION_STT_TRIGGER:
85,718✔
2888
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
85,718✔
2889
      break;
85,718✔
2890
    case DB_OPTION_TABLE_PREFIX: {
34,470✔
2891
      SValueNode* pNode = (SValueNode*)pVal;
34,470✔
2892
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
34,470✔
2893
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
34,470✔
2894
      } else {
UNCOV
2895
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
UNCOV
2896
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2897
      }
2898
      nodesDestroyNode((SNode*)pNode);
34,470✔
2899
      break;
34,470✔
2900
    }
2901
    case DB_OPTION_TABLE_SUFFIX: {
33,338✔
2902
      SValueNode* pNode = (SValueNode*)pVal;
33,338✔
2903
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
33,338✔
2904
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
33,338✔
2905
      } else {
UNCOV
2906
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
UNCOV
2907
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2908
      }
2909
      nodesDestroyNode((SNode*)pNode);
33,338✔
2910
      break;
33,338✔
2911
    }
2912
    case DB_OPTION_SS_CHUNKPAGES:
9,946✔
2913
      pDbOptions->ssChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
9,946✔
2914
      break;
9,946✔
2915
    case DB_OPTION_SS_KEEPLOCAL: {
11,172✔
2916
      SToken* pToken = pVal;
11,172✔
2917
      if (TK_NK_INTEGER == pToken->type) {
11,172✔
2918
        pDbOptions->ssKeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
2,396✔
2919
      } else {
2920
        pDbOptions->ssKeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
8,776✔
2921
      }
2922
      break;
11,172✔
2923
    }
2924
    case DB_OPTION_SS_COMPACT:
11,732✔
2925
      pDbOptions->ssCompact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
11,732✔
2926
      break;
11,732✔
2927
    case DB_OPTION_KEEP_TIME_OFFSET:
23,276✔
2928
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
23,276✔
2929
        pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
13,576✔
2930
      } else {
2931
        pDbOptions->pKeepTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
9,700✔
2932
      }
2933
      break;
23,276✔
2934
    case DB_OPTION_ENCRYPT_ALGORITHM:
21,046✔
2935
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
21,046✔
2936
      if (strlen(pDbOptions->encryptAlgorithmStr) == 0) pDbOptions->encryptAlgorithm = -1;
21,046✔
2937
      break;
21,046✔
2938
    case DB_OPTION_DNODES:
54,856✔
2939
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
54,856✔
2940
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
1,490✔
2941
                 TSDB_DNODE_LIST_LEN);
2942
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1,490✔
2943
      } else {
2944
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
53,366✔
2945
      }
2946
      break;
54,856✔
2947
    case DB_OPTION_COMPACT_INTERVAL:
64,242✔
2948
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
64,242✔
2949
        pDbOptions->compactInterval = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
9,788✔
2950
      } else {
2951
        pDbOptions->pCompactIntervalNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
54,454✔
2952
      }
2953
      break;
64,242✔
2954
    case DB_OPTION_COMPACT_TIME_RANGE:
99,818✔
2955
      pDbOptions->pCompactTimeRangeList = pVal;
99,818✔
2956
      break;
99,818✔
2957
    case DB_OPTION_COMPACT_TIME_OFFSET:
59,016✔
2958
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
59,016✔
2959
        pDbOptions->compactTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
28,658✔
2960
      } else {
2961
        pDbOptions->pCompactTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
30,358✔
2962
      }
2963
      break;
59,016✔
2964
    case DB_OPTION_IS_AUDIT:
9,034✔
2965
      pDbOptions->isAudit = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
9,034✔
2966
      break;
9,034✔
UNCOV
2967
    case DB_OPTION_ALLOW_DROP:
×
UNCOV
2968
      pDbOptions->allowDrop = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
×
2969
      break;
×
2970
    case DB_OPTION_SECURE_DELETE:
13,012✔
2971
      pDbOptions->secureDelete = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
13,012✔
2972
      break;
13,012✔
2973
    default:
40,928✔
2974
      break;
40,928✔
2975
  }
2976
  return pOptions;
4,855,250✔
UNCOV
2977
_err:
×
UNCOV
2978
  nodesDestroyNode(pOptions);
×
UNCOV
2979
  return NULL;
×
2980
}
2981

2982
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
4,392,752✔
2983
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
4,392,752✔
2984
}
2985

2986
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
462,498✔
2987
  CHECK_PARSER_STATUS(pCxt);
462,498✔
2988
  switch (pAlterOption->type) {
462,498✔
2989
    case DB_OPTION_KEEP:
139,064✔
2990
    case DB_OPTION_RETENTIONS:
2991
    case DB_OPTION_COMPACT_TIME_RANGE:
2992
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
139,064✔
2993
    default:
323,434✔
2994
      break;
323,434✔
2995
  }
2996
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
323,434✔
UNCOV
2997
_err:
×
UNCOV
2998
  nodesDestroyNode(pOptions);
×
UNCOV
2999
  nodesDestroyList(pAlterOption->pList);
×
UNCOV
3000
  return NULL;
×
3001
}
3002

3003
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
2,929,942✔
3004
  CHECK_PARSER_STATUS(pCxt);
2,929,942✔
3005
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
2,928,452✔
3006
  SCreateDatabaseStmt* pStmt = NULL;
2,926,112✔
3007
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
2,926,112✔
3008
  CHECK_MAKE_NODE(pStmt);
2,926,112✔
3009
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
2,926,112✔
3010
  pStmt->ignoreExists = ignoreExists;
2,926,112✔
3011
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
2,926,112✔
3012
  return (SNode*)pStmt;
2,925,928✔
3013
_err:
3,830✔
3014
  nodesDestroyNode(pOptions);
3,830✔
3015
  return NULL;
3,830✔
3016
}
3017

3018
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName, bool force) {
2,418,074✔
3019
  CHECK_PARSER_STATUS(pCxt);
2,418,074✔
3020
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
2,418,074✔
3021
  SDropDatabaseStmt* pStmt = NULL;
2,418,074✔
3022
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
2,418,074✔
3023
  CHECK_MAKE_NODE(pStmt);
2,418,074✔
3024
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
2,418,074✔
3025
  pStmt->ignoreNotExists = ignoreNotExists;
2,418,074✔
3026
  pStmt->force = force;
2,418,074✔
3027
  return (SNode*)pStmt;
2,418,074✔
UNCOV
3028
_err:
×
UNCOV
3029
  return NULL;
×
3030
}
3031

3032
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
415,722✔
3033
  CHECK_PARSER_STATUS(pCxt);
415,722✔
3034
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
415,722✔
3035
  SAlterDatabaseStmt* pStmt = NULL;
415,722✔
3036
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
415,722✔
3037
  CHECK_MAKE_NODE(pStmt);
415,722✔
3038
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
415,722✔
3039
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
415,722✔
3040
  return (SNode*)pStmt;
415,722✔
UNCOV
3041
_err:
×
UNCOV
3042
  nodesDestroyNode(pOptions);
×
UNCOV
3043
  return NULL;
×
3044
}
3045

3046
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
3,683,776✔
3047
  CHECK_PARSER_STATUS(pCxt);
3,683,776✔
3048
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
3,683,776✔
3049
  SFlushDatabaseStmt* pStmt = NULL;
3,683,802✔
3050
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
3,683,802✔
3051
  CHECK_MAKE_NODE(pStmt);
3,683,802✔
3052
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
3,683,802✔
3053
  return (SNode*)pStmt;
3,683,802✔
UNCOV
3054
_err:
×
UNCOV
3055
  return NULL;
×
3056
}
3057

3058
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
19,120✔
3059
  CHECK_PARSER_STATUS(pCxt);
19,120✔
3060
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
19,120✔
3061
  STrimDatabaseStmt* pStmt = NULL;
19,120✔
3062
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
19,120✔
3063
  CHECK_MAKE_NODE(pStmt);
19,120✔
3064
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
19,120✔
3065
  pStmt->maxSpeed = maxSpeed;
19,120✔
3066
  return (SNode*)pStmt;
19,120✔
UNCOV
3067
_err:
×
UNCOV
3068
  return NULL;
×
3069
}
3070

3071
SNode* createTrimDbWalStmt(SAstCreateContext* pCxt, SToken* pDbName) {
2,272✔
3072
  CHECK_PARSER_STATUS(pCxt);
2,272✔
3073
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
2,272✔
3074
  STrimDbWalStmt* pStmt = NULL;
2,272✔
3075
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_WAL_STMT, (SNode**)&pStmt);
2,272✔
3076
  CHECK_MAKE_NODE(pStmt);
2,272✔
3077
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
2,272✔
3078
  return (SNode*)pStmt;
2,272✔
UNCOV
3079
_err:
×
UNCOV
3080
  return NULL;
×
3081
}
3082

3083
SNode* createSsMigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
6,054✔
3084
  CHECK_PARSER_STATUS(pCxt);
6,054✔
3085
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
6,054✔
3086
  SSsMigrateDatabaseStmt* pStmt = NULL;
6,054✔
3087
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SSMIGRATE_DATABASE_STMT, (SNode**)&pStmt);
6,054✔
3088
  CHECK_MAKE_NODE(pStmt);
6,054✔
3089
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
6,054✔
3090
  return (SNode*)pStmt;
6,054✔
UNCOV
3091
_err:
×
UNCOV
3092
  return NULL;
×
3093
}
3094

3095
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd, bool metaOnly,
60,560✔
3096
                         bool force) {
3097
  CHECK_PARSER_STATUS(pCxt);
60,560✔
3098
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
60,560✔
3099
  SCompactDatabaseStmt* pStmt = NULL;
60,560✔
3100
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
60,560✔
3101
  CHECK_MAKE_NODE(pStmt);
60,560✔
3102
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
60,560✔
3103
  pStmt->pStart = pStart;
60,560✔
3104
  pStmt->pEnd = pEnd;
60,560✔
3105
  pStmt->metaOnly = metaOnly;
60,560✔
3106
  pStmt->force = force;
60,560✔
3107
  return (SNode*)pStmt;
60,560✔
UNCOV
3108
_err:
×
UNCOV
3109
  nodesDestroyNode(pStart);
×
UNCOV
3110
  nodesDestroyNode(pEnd);
×
UNCOV
3111
  return NULL;
×
3112
}
3113

3114
SNode* createCreateMountStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pMountName, SToken* pDnodeId,
3,234✔
3115
                             SToken* pMountPath) {
3116
#ifdef USE_MOUNT
3117
  CHECK_PARSER_STATUS(pCxt);
3,234✔
3118
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
3,234✔
3119
  CHECK_NAME(checkMountPath(pCxt, pMountPath));
3,234✔
3120
  SCreateMountStmt* pStmt = NULL;
3,234✔
3121
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MOUNT_STMT, (SNode**)&pStmt);
3,234✔
3122
  CHECK_MAKE_NODE(pStmt);
3,234✔
3123
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
3,234✔
3124
  COPY_STRING_FORM_STR_TOKEN(pStmt->mountPath, pMountPath);
3,234✔
3125
  pStmt->ignoreExists = ignoreExists;
3,234✔
3126
  if (TK_NK_INTEGER == pDnodeId->type) {
3,234✔
3127
    pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
3,234✔
3128
  } else {
UNCOV
3129
    goto _err;
×
3130
  }
3131
  return (SNode*)pStmt;
3,234✔
UNCOV
3132
_err:
×
UNCOV
3133
  nodesDestroyNode((SNode*)pStmt);
×
3134
  return NULL;
×
3135
#else
3136
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
3137
  return NULL;
3138
#endif
3139
}
3140

3141
SNode* createDropMountStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pMountName) {
1,080✔
3142
#ifdef USE_MOUNT
3143
  CHECK_PARSER_STATUS(pCxt);
1,080✔
3144
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
1,080✔
3145
  SDropMountStmt* pStmt = NULL;
1,080✔
3146
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_MOUNT_STMT, (SNode**)&pStmt);
1,080✔
3147
  CHECK_MAKE_NODE(pStmt);
1,080✔
3148
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
1,080✔
3149
  pStmt->ignoreNotExists = ignoreNotExists;
1,080✔
3150
  return (SNode*)pStmt;
1,080✔
UNCOV
3151
_err:
×
UNCOV
3152
  return NULL;
×
3153
#else
3154
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
3155
  return NULL;
3156
#endif
3157
}
3158

3159
SNode* createCompactVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
9,060✔
3160
                                SNode* pEnd, bool metaOnly, bool force) {
3161
  CHECK_PARSER_STATUS(pCxt);
9,060✔
3162
  if (NULL == pDbName) {
9,060✔
3163
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
1,510✔
3164
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
1,510✔
3165
    CHECK_PARSER_STATUS(pCxt);
1,510✔
3166
  }
3167
  SCompactVgroupsStmt* pStmt = NULL;
7,550✔
3168
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_VGROUPS_STMT, (SNode**)&pStmt);
7,550✔
3169
  CHECK_MAKE_NODE(pStmt);
7,550✔
3170
  pStmt->pDbName = pDbName;
7,550✔
3171
  pStmt->vgidList = vgidList;
7,550✔
3172
  pStmt->pStart = pStart;
7,550✔
3173
  pStmt->pEnd = pEnd;
7,550✔
3174
  pStmt->metaOnly = metaOnly;
7,550✔
3175
  pStmt->force = force;
7,550✔
3176
  return (SNode*)pStmt;
7,550✔
3177
_err:
1,510✔
3178
  nodesDestroyNode(pDbName);
1,510✔
3179
  nodesDestroyList(vgidList);
1,510✔
3180
  nodesDestroyNode(pStart);
1,510✔
3181
  nodesDestroyNode(pEnd);
1,510✔
3182
  return NULL;
1,510✔
3183
}
3184

3185
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
102,186,066✔
3186
  CHECK_PARSER_STATUS(pCxt);
102,186,066✔
3187
  STableOptions* pOptions = NULL;
102,162,070✔
3188
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
102,184,030✔
3189
  CHECK_MAKE_NODE(pOptions);
102,181,994✔
3190
  pOptions->maxDelay1 = -1;
102,181,994✔
3191
  pOptions->maxDelay2 = -1;
102,179,156✔
3192
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
102,167,638✔
3193
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
102,173,748✔
3194
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
102,146,808✔
3195
  pOptions->keep = -1;
102,149,362✔
3196
  pOptions->virtualStb = false;
102,141,672✔
3197
  pOptions->commentNull = true;  // mark null
102,168,768✔
3198
  pOptions->secureDelete = 0;
102,150,572✔
3199
  return (SNode*)pOptions;
102,156,418✔
UNCOV
3200
_err:
×
UNCOV
3201
  return NULL;
×
3202
}
3203

3204
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
146,628✔
3205
  CHECK_PARSER_STATUS(pCxt);
146,628✔
3206
  STableOptions* pOptions = NULL;
146,628✔
3207
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
146,628✔
3208
  CHECK_MAKE_NODE(pOptions);
146,628✔
3209
  pOptions->ttl = -1;
146,628✔
3210
  pOptions->commentNull = true;  // mark null
146,628✔
3211
  pOptions->keep = -1;
146,628✔
3212
  pOptions->secureDelete = -1;
146,628✔
3213
  return (SNode*)pOptions;
146,628✔
UNCOV
3214
_err:
×
UNCOV
3215
  return NULL;
×
3216
}
3217

3218
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
1,443,690✔
3219
  CHECK_PARSER_STATUS(pCxt);
1,443,690✔
3220
  switch (type) {
1,443,690✔
3221
    case TABLE_OPTION_COMMENT:
106,276✔
3222
      if (checkComment(pCxt, (SToken*)pVal, true)) {
106,276✔
3223
        ((STableOptions*)pOptions)->commentNull = false;
89,038✔
3224
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
89,038✔
3225
      }
3226
      break;
106,276✔
3227
    case TABLE_OPTION_MAXDELAY:
2,328✔
3228
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
2,328✔
3229
      break;
2,328✔
3230
    case TABLE_OPTION_WATERMARK:
2,328✔
3231
      ((STableOptions*)pOptions)->pWatermark = pVal;
2,328✔
3232
      break;
2,328✔
3233
    case TABLE_OPTION_ROLLUP:
3,880✔
3234
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
3,880✔
3235
      break;
3,880✔
3236
    case TABLE_OPTION_TTL: {
219,516✔
3237
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
219,516✔
3238
      if (ttl > INT32_MAX) {
219,516✔
3239
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
1,490✔
3240
      } else {
3241
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
3242
        ((STableOptions*)pOptions)->ttl = ttl;
218,026✔
3243
      }
3244
      break;
219,516✔
3245
    }
3246
    case TABLE_OPTION_SMA:
850,242✔
3247
      ((STableOptions*)pOptions)->pSma = pVal;
850,242✔
3248
      break;
850,242✔
3249
    case TABLE_OPTION_DELETE_MARK:
776✔
3250
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
776✔
3251
      break;
776✔
3252
    case TABLE_OPTION_KEEP:
150,360✔
3253
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
150,360✔
3254
        ((STableOptions*)pOptions)->keep = taosStr2Int32(((SToken*)pVal)->z, NULL, 10) * 1440;
24,192✔
3255
      } else {
3256
        ((STableOptions*)pOptions)->pKeepNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
126,168✔
3257
      }
3258
      break;
150,360✔
3259
    case TABLE_OPTION_VIRTUAL: {
97,456✔
3260
      int64_t virtualStb = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
97,456✔
3261
      if (virtualStb != 0 && virtualStb != 1) {
97,456✔
UNCOV
3262
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
3263
      } else {
3264
        ((STableOptions*)pOptions)->virtualStb = virtualStb;
97,456✔
3265
      }
3266
      break;
97,456✔
3267
    }
3268
    case TABLE_OPTION_SECURE_DELETE: {
10,528✔
3269
      int64_t secureDelete = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
10,528✔
3270
      if (secureDelete != 0 && secureDelete != 1) {
10,528✔
NEW
3271
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
3272
      } else {
3273
        ((STableOptions*)pOptions)->secureDelete = (int8_t)secureDelete;
10,528✔
3274
      }
3275
      break;
10,528✔
3276
    }
3277
    default:
×
3278
      break;
×
3279
  }
3280
  return pOptions;
1,443,690✔
UNCOV
3281
_err:
×
UNCOV
3282
  nodesDestroyNode(pOptions);
×
UNCOV
3283
  return NULL;
×
3284
}
3285

3286
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
826,849,456✔
3287
  CHECK_PARSER_STATUS(pCxt);
826,849,456✔
3288
  SColumnOptions* pOptions = NULL;
826,849,456✔
3289
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
826,849,456✔
3290
  CHECK_MAKE_NODE(pOptions);
826,849,456✔
3291
  pOptions->commentNull = true;
826,849,456✔
3292
  pOptions->bPrimaryKey = false;
826,849,456✔
3293
  pOptions->hasRef = false;
826,849,456✔
3294
  return (SNode*)pOptions;
826,849,456✔
UNCOV
3295
_err:
×
UNCOV
3296
  return NULL;
×
3297
}
3298

3299
EColumnOptionType getColumnOptionType(const char* optionType) {
4,375,918✔
3300
  if (0 == strcasecmp(optionType, "ENCODE")) {
4,375,918✔
3301
    return COLUMN_OPTION_ENCODE;
1,176,566✔
3302
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
3,199,352✔
3303
    return COLUMN_OPTION_COMPRESS;
1,608,632✔
3304
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
1,590,720✔
3305
    return COLUMN_OPTION_LEVEL;
1,586,400✔
3306
  }
3307
  return 0;
4,320✔
3308
}
3309

3310
SNode* setColumnReference(SAstCreateContext* pCxt, SNode* pOptions, SNode* pRef) {
181,664,762✔
3311
  CHECK_PARSER_STATUS(pCxt);
181,664,762✔
3312

3313
  ((SColumnOptions*)pOptions)->hasRef = true;
181,664,762✔
3314
  tstrncpy(((SColumnOptions*)pOptions)->refDb, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
181,664,762✔
3315
  tstrncpy(((SColumnOptions*)pOptions)->refTable, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
181,664,762✔
3316
  tstrncpy(((SColumnOptions*)pOptions)->refColumn, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
181,664,762✔
3317
  return pOptions;
181,664,762✔
UNCOV
3318
_err:
×
UNCOV
3319
  nodesDestroyNode(pOptions);
×
UNCOV
3320
  return NULL;
×
3321
}
3322

3323
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
486,532✔
3324
  CHECK_PARSER_STATUS(pCxt);
486,532✔
3325
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
486,532✔
3326
  return pOptions;
486,532✔
UNCOV
3327
_err:
×
UNCOV
3328
  nodesDestroyNode(pOptions);
×
UNCOV
3329
  return NULL;
×
3330
}
3331

3332
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
4,375,918✔
3333
  CHECK_PARSER_STATUS(pCxt);
4,375,918✔
3334
  char optionType[TSDB_CL_OPTION_LEN];
4,339,858✔
3335

3336
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
4,375,918✔
3337
  strncpy(optionType, pVal1->z, TMIN(pVal1->n, TSDB_CL_OPTION_LEN));
4,375,918✔
3338
  if (0 == strlen(optionType)) {
4,375,918✔
UNCOV
3339
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
3340
    return pOptions;
×
3341
  }
3342
  EColumnOptionType type = getColumnOptionType(optionType);
4,375,918✔
3343
  switch (type) {
4,375,918✔
3344
    case COLUMN_OPTION_ENCODE:
1,176,566✔
3345
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
1,176,566✔
3346
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
1,176,566✔
3347
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
1,176,566✔
UNCOV
3348
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
3349
      }
3350
      break;
1,176,566✔
3351
    case COLUMN_OPTION_COMPRESS:
1,608,632✔
3352
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
1,608,632✔
3353
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
1,608,632✔
3354
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
1,608,632✔
3355
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
3356
      }
3357
      break;
1,608,632✔
3358
    case COLUMN_OPTION_LEVEL:
1,586,400✔
3359
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
1,586,400✔
3360
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
1,586,400✔
3361
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
1,586,400✔
UNCOV
3362
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
3363
      }
3364
      break;
1,586,400✔
3365
    default:
4,320✔
3366
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
4,320✔
3367
      break;
4,320✔
3368
  }
3369
  return pOptions;
4,375,918✔
UNCOV
3370
_err:
×
3371
  nodesDestroyNode(pOptions);
×
UNCOV
3372
  return NULL;
×
3373
}
3374

3375
SNode* createColumnRefNodeByNode(SAstCreateContext* pCxt, SToken* pColName, SNode* pRef) {
109,654,300✔
3376
  CHECK_PARSER_STATUS(pCxt);
109,654,300✔
3377
  CHECK_NAME(checkColumnName(pCxt, pColName));
109,654,300✔
3378

3379
  SColumnRefNode* pCol = NULL;
109,654,300✔
3380
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
109,654,300✔
3381
  CHECK_MAKE_NODE(pCol);
109,654,300✔
3382
  if (pColName) {
109,654,300✔
3383
    COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
109,654,300✔
3384
  }
3385
  tstrncpy(pCol->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
109,654,300✔
3386
  tstrncpy(pCol->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
109,654,300✔
3387
  tstrncpy(pCol->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
109,654,300✔
3388
  return (SNode*)pCol;
109,654,300✔
UNCOV
3389
_err:
×
UNCOV
3390
  return NULL;
×
3391
}
3392

3393
STokenTriplet* createTokenTriplet(SAstCreateContext* pCxt, SToken pName) {
292,992,384✔
3394
  CHECK_PARSER_STATUS(pCxt);
292,992,384✔
3395

3396
  STokenTriplet* pTokenTri = taosMemoryMalloc(sizeof(STokenTriplet));
292,992,384✔
3397
  CHECK_OUT_OF_MEM(pTokenTri);
292,992,384✔
3398
  pTokenTri->name[0] = pName;
292,992,384✔
3399
  pTokenTri->numOfName = 1;
292,992,384✔
3400

3401
  return pTokenTri;
292,992,384✔
UNCOV
3402
_err:
×
UNCOV
3403
  return NULL;
×
3404
}
3405

3406
STokenTriplet* setColumnName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri, SToken pName) {
293,825,656✔
3407
  CHECK_PARSER_STATUS(pCxt);
293,825,656✔
3408

3409
  if (pTokenTri->numOfName >= 3) {
293,825,656✔
UNCOV
3410
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
3411
    goto _err;
×
3412
  }
3413

3414
  pTokenTri->name[pTokenTri->numOfName] = pName;
293,825,656✔
3415
  pTokenTri->numOfName++;
293,825,656✔
3416
  return pTokenTri;
293,825,656✔
UNCOV
3417
_err:
×
3418
  return NULL;
×
3419
}
3420

3421
SNode* createColumnRefNodeByName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri) {
292,992,384✔
3422
  SColumnRefNode* pCol = NULL;
292,992,384✔
3423
  CHECK_PARSER_STATUS(pCxt);
292,992,384✔
3424
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
292,992,384✔
3425
  CHECK_MAKE_NODE(pCol);
292,992,384✔
3426

3427
  switch (pTokenTri->numOfName) {
292,992,384✔
3428
    case 2: {
292,159,112✔
3429
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[0]));
292,159,112✔
3430
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[1]));
292,159,112✔
3431
      snprintf(pCol->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
292,159,112✔
3432
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[0]);
292,159,112✔
3433
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[1]);
292,159,112✔
3434
      break;
292,159,112✔
3435
    }
3436
    case 3: {
833,272✔
3437
      CHECK_NAME(checkDbName(pCxt, &pTokenTri->name[0], true));
833,272✔
3438
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[1]));
833,272✔
3439
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[2]));
833,272✔
3440
      COPY_STRING_FORM_ID_TOKEN(pCol->refDbName, &pTokenTri->name[0]);
833,272✔
3441
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[1]);
833,272✔
3442
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[2]);
833,272✔
3443
      break;
833,272✔
3444
    }
UNCOV
3445
    default: {
×
UNCOV
3446
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
3447
      goto _err;
×
3448
    }
3449
  }
3450

3451
  taosMemFree(pTokenTri);
292,992,384✔
3452
  return (SNode*)pCol;
292,992,384✔
UNCOV
3453
_err:
×
UNCOV
3454
  taosMemFree(pTokenTri);
×
UNCOV
3455
  nodesDestroyNode((SNode*)pCol);
×
UNCOV
3456
  return NULL;
×
3457
}
3458

3459
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
824,074,698✔
3460
  CHECK_PARSER_STATUS(pCxt);
824,074,698✔
3461
  CHECK_NAME(checkColumnName(pCxt, pColName));
824,074,698✔
3462
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
824,069,708✔
3463
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
7,780✔
3464
    CHECK_PARSER_STATUS(pCxt);
7,780✔
3465
  }
3466
  SColumnDefNode* pCol = NULL;
824,061,928✔
3467
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
824,061,928✔
3468
  CHECK_MAKE_NODE(pCol);
824,061,928✔
3469
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
824,061,928✔
3470
  pCol->dataType = dataType;
824,061,928✔
3471
  pCol->pOptions = pNode;
824,061,928✔
3472
  pCol->sma = true;
824,061,928✔
3473
  return (SNode*)pCol;
824,061,928✔
3474
_err:
12,770✔
3475
  nodesDestroyNode(pNode);
12,770✔
3476
  return NULL;
12,770✔
3477
}
3478

3479
SDataType createDataType(uint8_t type) {
829,468,132✔
3480
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
829,468,132✔
3481
  return dt;
829,468,132✔
3482
}
3483

3484
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
219,024,972✔
3485
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
219,024,972✔
3486
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
219,024,972✔
3487
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
219,024,972✔
3488
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
219,024,972✔
3489
  return dt;
219,024,972✔
3490
}
3491

3492
SDataType createDecimalDataType(uint8_t type, const SToken* pPrecisionToken, const SToken* pScaleToken) {
473,506✔
3493
  SDataType dt = {0};
473,506✔
3494
  dt.precision = taosStr2UInt8(pPrecisionToken->z, NULL, 10);
473,506✔
3495
  dt.scale = pScaleToken ? taosStr2Int32(pScaleToken->z, NULL, 10) : 0;
473,506✔
3496
  dt.type = decimalTypeFromPrecision(dt.precision);
473,506✔
3497
  dt.bytes = tDataTypes[dt.type].bytes;
473,506✔
3498
  return dt;
473,506✔
3499
}
3500

3501
SNode* createCreateVTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols) {
351,464✔
3502
  SCreateVTableStmt* pStmt = NULL;
351,464✔
3503
  CHECK_PARSER_STATUS(pCxt);
351,464✔
3504
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
351,464✔
3505
  CHECK_MAKE_NODE(pStmt);
351,464✔
3506
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
351,464✔
3507
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
351,464✔
3508
  pStmt->ignoreExists = ignoreExists;
351,464✔
3509
  pStmt->pCols = pCols;
351,464✔
3510
  nodesDestroyNode(pRealTable);
351,464✔
3511
  return (SNode*)pStmt;
351,464✔
UNCOV
3512
_err:
×
UNCOV
3513
  nodesDestroyNode(pRealTable);
×
UNCOV
3514
  nodesDestroyList(pCols);
×
UNCOV
3515
  return NULL;
×
3516
}
3517

3518
SNode* createCreateVSubTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable,
561,894✔
3519
                                 SNodeList* pSpecificColRefs, SNodeList* pColRefs, SNode* pUseRealTable,
3520
                                 SNodeList* pSpecificTags, SNodeList* pValsOfTags) {
3521
  CHECK_PARSER_STATUS(pCxt);
561,894✔
3522
  SCreateVSubTableStmt* pStmt = NULL;
561,894✔
3523
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT, (SNode**)&pStmt);
561,894✔
3524
  CHECK_MAKE_NODE(pStmt);
561,894✔
3525
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
561,894✔
3526
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
561,894✔
3527
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
561,894✔
3528
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
561,894✔
3529
  pStmt->ignoreExists = ignoreExists;
561,894✔
3530
  pStmt->pSpecificTags = pSpecificTags;
561,894✔
3531
  pStmt->pValsOfTags = pValsOfTags;
561,894✔
3532
  pStmt->pSpecificColRefs = pSpecificColRefs;
561,894✔
3533
  pStmt->pColRefs = pColRefs;
561,894✔
3534
  nodesDestroyNode(pRealTable);
561,894✔
3535
  nodesDestroyNode(pUseRealTable);
561,894✔
3536
  return (SNode*)pStmt;
561,894✔
UNCOV
3537
_err:
×
UNCOV
3538
  nodesDestroyNode(pRealTable);
×
UNCOV
3539
  nodesDestroyNode(pUseRealTable);
×
UNCOV
3540
  nodesDestroyList(pSpecificTags);
×
UNCOV
3541
  nodesDestroyList(pValsOfTags);
×
UNCOV
3542
  nodesDestroyList(pSpecificColRefs);
×
UNCOV
3543
  nodesDestroyList(pColRefs);
×
UNCOV
3544
  return NULL;
×
3545
}
3546

3547
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
17,184,442✔
3548
                             SNodeList* pTags, SNode* pOptions) {
3549
  CHECK_PARSER_STATUS(pCxt);
17,184,442✔
3550
  SCreateTableStmt* pStmt = NULL;
17,181,764✔
3551
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
17,181,764✔
3552
  CHECK_MAKE_NODE(pStmt);
17,181,764✔
3553
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
17,181,764✔
3554
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
17,181,764✔
3555
  pStmt->ignoreExists = ignoreExists;
17,181,764✔
3556
  pStmt->pCols = pCols;
17,181,764✔
3557
  pStmt->pTags = pTags;
17,181,764✔
3558
  pStmt->pOptions = (STableOptions*)pOptions;
17,181,764✔
3559
  nodesDestroyNode(pRealTable);
17,181,764✔
3560
  return (SNode*)pStmt;
17,181,764✔
3561
_err:
2,678✔
3562
  nodesDestroyNode(pRealTable);
2,678✔
3563
  nodesDestroyList(pCols);
2,678✔
3564
  nodesDestroyList(pTags);
2,678✔
3565
  nodesDestroyNode(pOptions);
2,678✔
3566
  return NULL;
2,678✔
3567
}
3568

3569
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
84,980,682✔
3570
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
3571
  CHECK_PARSER_STATUS(pCxt);
84,980,682✔
3572
  SCreateSubTableClause* pStmt = NULL;
84,956,352✔
3573
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
84,964,790✔
3574
  CHECK_MAKE_NODE(pStmt);
85,002,520✔
3575
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
85,002,520✔
3576
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
84,959,664✔
3577
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
84,976,924✔
3578
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
84,957,344✔
3579
  pStmt->ignoreExists = ignoreExists;
84,929,276✔
3580
  pStmt->pSpecificTags = pSpecificTags;
84,964,892✔
3581
  pStmt->pValsOfTags = pValsOfTags;
84,952,972✔
3582
  pStmt->pOptions = (STableOptions*)pOptions;
84,931,938✔
3583
  nodesDestroyNode(pRealTable);
84,924,082✔
3584
  nodesDestroyNode(pUseRealTable);
84,968,632✔
3585
  return (SNode*)pStmt;
84,990,116✔
UNCOV
3586
_err:
×
UNCOV
3587
  nodesDestroyNode(pRealTable);
×
UNCOV
3588
  nodesDestroyNode(pOptions);
×
UNCOV
3589
  nodesDestroyNode(pUseRealTable);
×
UNCOV
3590
  nodesDestroyList(pSpecificTags);
×
UNCOV
3591
  nodesDestroyList(pValsOfTags);
×
UNCOV
3592
  return NULL;
×
3593
}
3594

3595
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
2,762✔
3596
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
3597
  CHECK_PARSER_STATUS(pCxt);
2,762✔
3598
  SCreateSubTableFromFileClause* pStmt = NULL;
2,762✔
3599
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
2,762✔
3600
  CHECK_MAKE_NODE(pStmt);
2,762✔
3601
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2,762✔
3602
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2,762✔
3603
  pStmt->ignoreExists = ignoreExists;
2,762✔
3604
  pStmt->pSpecificTags = pSpecificTags;
2,762✔
3605
  if (TK_NK_STRING == pFilePath->type) {
2,762✔
3606
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
2,762✔
3607
  } else {
3608
    strncpy(pStmt->filePath, pFilePath->z, pFilePath->n);
×
3609
  }
3610

3611
  nodesDestroyNode(pUseRealTable);
2,762✔
3612
  return (SNode*)pStmt;
2,762✔
UNCOV
3613
_err:
×
UNCOV
3614
  nodesDestroyNode(pUseRealTable);
×
UNCOV
3615
  nodesDestroyList(pSpecificTags);
×
UNCOV
3616
  return NULL;
×
3617
}
3618

3619
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
71,100,084✔
3620
  CHECK_PARSER_STATUS(pCxt);
71,100,084✔
3621
  SCreateMultiTablesStmt* pStmt = NULL;
71,041,924✔
3622
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
71,080,208✔
3623
  CHECK_MAKE_NODE(pStmt);
71,098,780✔
3624
  pStmt->pSubTables = pSubTables;
71,098,780✔
3625
  return (SNode*)pStmt;
71,106,498✔
UNCOV
3626
_err:
×
UNCOV
3627
  nodesDestroyList(pSubTables);
×
UNCOV
3628
  return NULL;
×
3629
}
3630

3631
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
4,641,156✔
3632
  CHECK_PARSER_STATUS(pCxt);
4,641,156✔
3633
  SDropTableClause* pStmt = NULL;
4,640,752✔
3634
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
4,640,752✔
3635
  CHECK_MAKE_NODE(pStmt);
4,640,752✔
3636
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
4,640,752✔
3637
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
4,640,752✔
3638
  pStmt->ignoreNotExists = ignoreNotExists;
4,640,752✔
3639
  nodesDestroyNode(pRealTable);
4,640,752✔
3640
  return (SNode*)pStmt;
4,640,752✔
3641
_err:
404✔
3642
  nodesDestroyNode(pRealTable);
404✔
3643
  return NULL;
404✔
3644
}
3645

3646
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
4,425,148✔
3647
  CHECK_PARSER_STATUS(pCxt);
4,425,148✔
3648
  SDropTableStmt* pStmt = NULL;
4,424,744✔
3649
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
4,424,744✔
3650
  CHECK_MAKE_NODE(pStmt);
4,424,744✔
3651
  pStmt->pTables = pTables;
4,424,744✔
3652
  pStmt->withOpt = withOpt;
4,424,744✔
3653
  return (SNode*)pStmt;
4,424,744✔
3654
_err:
404✔
3655
  nodesDestroyList(pTables);
404✔
3656
  return NULL;
404✔
3657
}
3658

3659
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
167,586✔
3660
  CHECK_PARSER_STATUS(pCxt);
167,586✔
3661
  SDropSuperTableStmt* pStmt = NULL;
167,586✔
3662
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
167,586✔
3663
  CHECK_MAKE_NODE(pStmt);
167,586✔
3664
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
167,586✔
3665
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
167,586✔
3666
  pStmt->ignoreNotExists = ignoreNotExists;
167,586✔
3667
  pStmt->withOpt = withOpt;
167,586✔
3668
  nodesDestroyNode(pRealTable);
167,586✔
3669
  return (SNode*)pStmt;
167,586✔
UNCOV
3670
_err:
×
UNCOV
3671
  nodesDestroyNode(pRealTable);
×
UNCOV
3672
  return NULL;
×
3673
}
3674

3675
SNode* createDropVirtualTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
147,876✔
3676
  CHECK_PARSER_STATUS(pCxt);
147,876✔
3677
  SDropVirtualTableStmt* pStmt = NULL;
147,876✔
3678
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
147,876✔
3679
  CHECK_MAKE_NODE(pStmt);
147,876✔
3680
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
147,876✔
3681
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
147,876✔
3682
  pStmt->ignoreNotExists = ignoreNotExists;
147,876✔
3683
  pStmt->withOpt = withOpt;
147,876✔
3684
  nodesDestroyNode(pRealTable);
147,876✔
3685
  return (SNode*)pStmt;
147,876✔
3686
_err:
×
3687
  nodesDestroyNode(pRealTable);
×
3688
  return NULL;
×
3689
}
3690

3691
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
39,406,098✔
3692
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
39,406,098✔
3693
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
39,406,098✔
3694
  nodesDestroyNode(pRealTable);
39,406,098✔
3695
  return (SNode*)pStmt;
39,406,098✔
3696
}
3697

3698
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
146,628✔
3699
  CHECK_PARSER_STATUS(pCxt);
146,628✔
3700
  SAlterTableStmt* pStmt = NULL;
130,578✔
3701
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
130,578✔
3702
  CHECK_MAKE_NODE(pStmt);
130,578✔
3703
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
130,578✔
3704
  pStmt->pOptions = (STableOptions*)pOptions;
130,578✔
3705
  return createAlterTableStmtFinalize(pRealTable, pStmt);
130,578✔
3706
_err:
16,050✔
3707
  nodesDestroyNode(pRealTable);
16,050✔
3708
  nodesDestroyNode(pOptions);
16,050✔
3709
  return NULL;
16,050✔
3710
}
3711

3712
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
5,415,692✔
3713
                                    SDataType dataType) {
3714
  CHECK_PARSER_STATUS(pCxt);
5,415,692✔
3715
  CHECK_NAME(checkColumnName(pCxt, pColName));
5,415,692✔
3716
  SAlterTableStmt* pStmt = NULL;
5,415,288✔
3717
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
5,415,288✔
3718
  CHECK_MAKE_NODE(pStmt);
5,415,288✔
3719
  pStmt->alterType = alterType;
5,415,288✔
3720
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
5,415,288✔
3721
  pStmt->dataType = dataType;
5,415,288✔
3722
  return createAlterTableStmtFinalize(pRealTable, pStmt);
5,415,288✔
3723
_err:
404✔
3724
  nodesDestroyNode(pRealTable);
404✔
3725
  return NULL;
404✔
3726
}
3727

3728
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
12,739,306✔
3729
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
3730
  SAlterTableStmt* pStmt = NULL;
12,739,306✔
3731
  CHECK_PARSER_STATUS(pCxt);
12,739,306✔
3732
  CHECK_NAME(checkColumnName(pCxt, pColName));
12,734,986✔
3733
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
12,734,582✔
3734
  CHECK_MAKE_NODE(pStmt);
12,734,582✔
3735
  pStmt->alterType = alterType;
12,734,582✔
3736
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
12,734,582✔
3737
  pStmt->dataType = dataType;
12,734,582✔
3738
  pStmt->pColOptions = (SColumnOptions*)pOptions;
12,734,582✔
3739

3740
  if (pOptions != NULL) {
12,734,582✔
3741
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
12,734,582✔
3742
    if (pOption->hasRef) {
12,734,582✔
3743
      if (!pOption->commentNull || pOption->bPrimaryKey || 0 != strcmp(pOption->compress, "") ||
72,936✔
3744
          0 != strcmp(pOption->encode, "") || 0 != strcmp(pOption->compressLevel, "")) {
72,936✔
UNCOV
3745
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
×
3746
      }
3747
      pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF;
72,936✔
3748
      tstrncpy(pStmt->refDbName, pOption->refDb, TSDB_DB_NAME_LEN);
72,936✔
3749
      tstrncpy(pStmt->refTableName, pOption->refTable, TSDB_TABLE_NAME_LEN);
72,936✔
3750
      tstrncpy(pStmt->refColName, pOption->refColumn, TSDB_COL_NAME_LEN);
72,936✔
3751
      CHECK_PARSER_STATUS(pCxt);
72,936✔
3752
    } else if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
12,661,646✔
3753
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
12,656,182✔
3754
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
544,240✔
3755
      } else {
3756
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
3757
        //                                         "not support alter column with option except compress");
3758
        // return NULL;
3759
      }
3760
    } else {
3761
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
5,464✔
3762
                                              "not support alter column with option except compress");
3763
      CHECK_PARSER_STATUS(pCxt);
5,464✔
3764
    }
3765
  }
3766
  return createAlterTableStmtFinalize(pRealTable, pStmt);
12,729,118✔
3767
_err:
10,188✔
3768
  nodesDestroyNode(pOptions);
10,188✔
3769
  nodesDestroyNode((SNode*)pStmt);
10,188✔
3770
  nodesDestroyNode(pRealTable);
10,188✔
3771
  return NULL;
10,188✔
3772
}
3773

3774
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
222,500✔
3775
                                           SToken* pColName, SNode* pOptions) {
3776
  CHECK_PARSER_STATUS(pCxt);
222,500✔
3777
  CHECK_NAME(checkColumnName(pCxt, pColName));
222,500✔
3778
  SAlterTableStmt* pStmt = NULL;
222,500✔
3779
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
222,500✔
3780
  CHECK_MAKE_NODE(pStmt);
222,500✔
3781
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
222,500✔
3782
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
222,500✔
3783
  pStmt->pColOptions = (SColumnOptions*)pOptions;
222,500✔
3784
  return createAlterTableStmtFinalize(pRealTable, pStmt);
222,500✔
UNCOV
3785
_err:
×
UNCOV
3786
  nodesDestroyNode(pOptions);
×
UNCOV
3787
  nodesDestroyNode(pRealTable);
×
UNCOV
3788
  return NULL;
×
3789
}
3790

3791
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
3,532,784✔
3792
  CHECK_PARSER_STATUS(pCxt);
3,532,784✔
3793
  CHECK_NAME(checkColumnName(pCxt, pColName));
3,532,784✔
3794
  SAlterTableStmt* pStmt = NULL;
3,531,976✔
3795
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
3,531,976✔
3796
  CHECK_MAKE_NODE(pStmt);
3,531,976✔
3797
  pStmt->alterType = alterType;
3,531,976✔
3798
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
3,531,976✔
3799
  return createAlterTableStmtFinalize(pRealTable, pStmt);
3,531,976✔
3800
_err:
808✔
3801
  nodesDestroyNode(pRealTable);
808✔
3802
  return NULL;
808✔
3803
}
3804

3805
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
401,118✔
3806
                                 SToken* pNewColName) {
3807
  CHECK_PARSER_STATUS(pCxt);
401,118✔
3808
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
401,118✔
3809
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
401,118✔
3810
  SAlterTableStmt* pStmt = NULL;
399,402✔
3811
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
399,402✔
3812
  CHECK_MAKE_NODE(pStmt);
399,402✔
3813
  pStmt->alterType = alterType;
399,402✔
3814
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
399,402✔
3815
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
399,402✔
3816
  return createAlterTableStmtFinalize(pRealTable, pStmt);
399,402✔
3817
_err:
1,716✔
3818
  nodesDestroyNode(pRealTable);
1,716✔
3819
  return NULL;
1,716✔
3820
}
3821

3822
SNode* createAlterTableAlterColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
231,360✔
3823
                                   SNode* pRef) {
3824
  CHECK_PARSER_STATUS(pCxt);
231,360✔
3825
  CHECK_NAME(checkColumnName(pCxt, pColName));
231,360✔
3826
  SAlterTableStmt* pStmt = NULL;
231,360✔
3827
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
231,360✔
3828
  CHECK_MAKE_NODE(pStmt);
231,360✔
3829
  pStmt->alterType = alterType;
231,360✔
3830
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
231,360✔
3831
  tstrncpy(pStmt->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
231,360✔
3832
  tstrncpy(pStmt->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
231,360✔
3833
  tstrncpy(pStmt->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
231,360✔
3834
  return createAlterTableStmtFinalize(pRealTable, pStmt);
231,360✔
UNCOV
3835
_err:
×
UNCOV
3836
  nodesDestroyNode(pRealTable);
×
UNCOV
3837
  return NULL;
×
3838
}
3839

3840
SNode* createAlterTableRemoveColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
137,046✔
3841
                                    const SToken* pLiteral) {
3842
  CHECK_PARSER_STATUS(pCxt);
137,046✔
3843
  CHECK_NAME(checkColumnName(pCxt, pColName));
137,046✔
3844
  SAlterTableStmt* pStmt = NULL;
137,046✔
3845
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
137,046✔
3846
  CHECK_MAKE_NODE(pStmt);
137,046✔
3847
  pStmt->alterType = alterType;
137,046✔
3848
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
137,046✔
3849
  return createAlterTableStmtFinalize(pRealTable, pStmt);
137,046✔
UNCOV
3850
_err:
×
3851
  nodesDestroyNode(pRealTable);
×
3852
  return NULL;
×
3853
}
3854

3855
SNode* createAlterSingleTagColumnNode(SAstCreateContext* pCtx, SToken* pTagName, SNode* pVal) {
16,681,426✔
3856
  CHECK_PARSER_STATUS(pCtx);
16,681,426✔
3857
  SAlterTableStmt* pStmt = NULL;
16,681,426✔
3858
  pCtx->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
16,681,426✔
3859
  CHECK_MAKE_NODE(pStmt);
16,681,426✔
3860
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
16,681,426✔
3861
  CHECK_NAME(checkColumnName(pCtx, pTagName));
16,681,426✔
3862
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
16,681,426✔
3863
  pStmt->pVal = (SValueNode*)pVal;
16,681,426✔
3864
  pStmt->pNodeListTagValue = NULL;
16,681,426✔
3865
  return (SNode*)pStmt;
16,681,426✔
3866
_err:
×
3867
  return NULL;
×
3868
}
3869

UNCOV
3870
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
×
UNCOV
3871
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
3872
  CHECK_NAME(checkColumnName(pCxt, pTagName));
×
UNCOV
3873
  SAlterTableStmt* pStmt = NULL;
×
UNCOV
3874
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
UNCOV
3875
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
3876
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
×
UNCOV
3877
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
×
UNCOV
3878
  pStmt->pVal = (SValueNode*)pVal;
×
UNCOV
3879
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
UNCOV
3880
_err:
×
UNCOV
3881
  nodesDestroyNode(pVal);
×
3882
  nodesDestroyNode(pRealTable);
×
3883
  return NULL;
×
3884
}
3885

3886
SNode* createAlterTableSetMultiTagValue(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pList) {
16,608,830✔
3887
  CHECK_PARSER_STATUS(pCxt);
16,608,830✔
3888
  SAlterTableStmt* pStmt = NULL;
16,608,830✔
3889
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
16,608,830✔
3890

3891
  CHECK_MAKE_NODE(pStmt);
16,608,830✔
3892
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL;
16,608,830✔
3893
  pStmt->pNodeListTagValue = pList;
16,608,830✔
3894
  return createAlterTableStmtFinalize(pRealTable, pStmt);
16,608,830✔
3895
_err:
×
3896
  return NULL;
×
3897
}
3898

3899
SNode* setAlterSuperTableType(SNode* pStmt) {
1,048,630✔
3900
  if (!pStmt) return NULL;
1,048,630✔
3901
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
1,047,418✔
3902
  return pStmt;
1,047,418✔
3903
}
3904

3905
SNode* setAlterVirtualTableType(SNode* pStmt) {
787,808✔
3906
  if (!pStmt) return NULL;
787,808✔
3907
  setNodeType(pStmt, QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT);
787,808✔
3908
  return pStmt;
787,808✔
3909
}
3910

3911
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
167,802,946✔
3912
  CHECK_PARSER_STATUS(pCxt);
167,802,946✔
3913
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
167,805,338✔
3914
  SUseDatabaseStmt* pStmt = NULL;
167,810,272✔
3915
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
167,810,272✔
3916
  CHECK_MAKE_NODE(pStmt);
167,803,186✔
3917
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
167,803,186✔
3918
  return (SNode*)pStmt;
167,803,104✔
UNCOV
3919
_err:
×
UNCOV
3920
  return NULL;
×
3921
}
3922

3923
static bool needDbShowStmt(ENodeType type) {
3,337,344✔
3924
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
2,814,170✔
3925
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
1,856,988✔
3926
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
488,536✔
3927
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type ||
459,572✔
3928
         QUERY_NODE_SHOW_USAGE_STMT == type || QUERY_NODE_SHOW_VTABLES_STMT == type ||
6,151,514✔
3929
         QUERY_NODE_SHOW_STREAMS_STMT == type;
3930
}
3931

3932
SNode* createShowStmtWithLike(SAstCreateContext* pCxt, ENodeType type, SNode* pLikePattern) {
47,382✔
3933
  CHECK_PARSER_STATUS(pCxt);
47,382✔
3934
  SShowStmt* pStmt = NULL;
47,382✔
3935
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
47,382✔
3936
  CHECK_MAKE_NODE(pStmt);
47,382✔
3937
  pStmt->withFull = false;
47,382✔
3938
  pStmt->pTbName = pLikePattern;
47,382✔
3939
  if (pLikePattern) {
47,382✔
3940
    pStmt->tableCondType = OP_TYPE_LIKE;
9,592✔
3941
  }
3942
  return (SNode*)pStmt;
47,382✔
UNCOV
3943
_err:
×
UNCOV
3944
  nodesDestroyNode(pLikePattern);
×
UNCOV
3945
  return NULL;
×
3946
}
3947

3948
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
2,531,146✔
3949
  CHECK_PARSER_STATUS(pCxt);
2,531,146✔
3950
  SShowStmt* pStmt = NULL;
2,531,146✔
3951
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,531,146✔
3952
  CHECK_MAKE_NODE(pStmt);
2,531,146✔
3953
  pStmt->withFull = false;
2,531,146✔
3954
  return (SNode*)pStmt;
2,531,146✔
UNCOV
3955
_err:
×
UNCOV
3956
  return NULL;
×
3957
}
3958

3959
SNode* createShowXnodeStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pWhere) {
13,294✔
3960
  CHECK_PARSER_STATUS(pCxt);
13,294✔
3961
  SShowStmt* pStmt = NULL;
13,294✔
3962
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
13,294✔
3963
  CHECK_MAKE_NODE(pStmt);
13,294✔
3964
  pStmt->pWhere = pWhere;
13,294✔
3965
  return (SNode*)pStmt;
13,294✔
UNCOV
3966
_err:
×
UNCOV
3967
  return NULL;
×
3968
}
3969

3970
SNode* createShowXNodeResourcesWhereStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
11,204✔
3971
  CHECK_PARSER_STATUS(pCxt);
11,204✔
3972
  SShowStmt* pStmt = NULL;
11,204✔
3973
  ENodeType  type;
3974
  switch (resourceType) {
11,204✔
3975
    case XNODE_TASK:
4,380✔
3976
      type = QUERY_NODE_SHOW_XNODE_TASKS_STMT;
4,380✔
3977
      break;
4,380✔
3978
    case XNODE_JOB:
3,940✔
3979
      type = QUERY_NODE_SHOW_XNODE_JOBS_STMT;
3,940✔
3980
      break;
3,940✔
3981
    case XNODE_AGENT:
2,884✔
3982
      type = QUERY_NODE_SHOW_XNODE_AGENTS_STMT;
2,884✔
3983
      break;
2,884✔
UNCOV
3984
    default:
×
UNCOV
3985
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
3986
                                              "Xnode not support show xnode resource type");
UNCOV
3987
      goto _err;
×
3988
  }
3989

3990
  pStmt = (SShowStmt*)createShowXnodeStmtWithCond(pCxt, type, pWhere);
11,204✔
3991
  CHECK_MAKE_NODE(pStmt);
11,204✔
3992

3993
  return (SNode*)pStmt;
11,204✔
UNCOV
3994
_err:
×
UNCOV
3995
  return NULL;
×
3996
}
3997

3998
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
26,536✔
3999
  CHECK_PARSER_STATUS(pCxt);
26,536✔
4000
  SShowStmt* pStmt = NULL;
26,536✔
4001
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
26,536✔
4002
  CHECK_MAKE_NODE(pStmt);
26,536✔
4003
  pStmt->withFull = true;
26,536✔
4004
  return (SNode*)pStmt;
26,536✔
UNCOV
4005
_err:
×
UNCOV
4006
  return NULL;
×
4007
}
4008

4009
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
685,444✔
4010
  CHECK_PARSER_STATUS(pCxt);
685,444✔
4011
  SShowCompactsStmt* pStmt = NULL;
685,444✔
4012
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
685,444✔
4013
  CHECK_MAKE_NODE(pStmt);
685,444✔
4014
  return (SNode*)pStmt;
685,444✔
UNCOV
4015
_err:
×
UNCOV
4016
  return NULL;
×
4017
}
4018

4019
SNode* createShowSsMigratesStmt(SAstCreateContext* pCxt, ENodeType type) {
175,820✔
4020
  CHECK_PARSER_STATUS(pCxt);
175,820✔
4021
  SShowSsMigratesStmt* pStmt = NULL;
175,820✔
4022
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
175,820✔
4023
  CHECK_MAKE_NODE(pStmt);
175,820✔
4024
  return (SNode*)pStmt;
175,820✔
UNCOV
4025
_err:
×
UNCOV
4026
  return NULL;
×
4027
}
4028

4029
SNode* createShowTokensStmt(SAstCreateContext* pCxt, ENodeType type) {
3,368✔
4030
  CHECK_PARSER_STATUS(pCxt);
3,368✔
4031
  SShowTokensStmt* pStmt = NULL;
3,368✔
4032
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,368✔
4033
  CHECK_MAKE_NODE(pStmt);
3,368✔
4034
  return (SNode*)pStmt;
3,368✔
UNCOV
4035
_err:
×
UNCOV
4036
  return NULL;
×
4037
}
4038

4039
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
1,472,010✔
4040
  if (pStmt == NULL) {
1,472,010✔
4041
    return NULL;
×
4042
  }
4043
  SShowStmt* pShow = (SShowStmt*)pStmt;
1,472,010✔
4044
  pShow->showKind = showKind;
1,472,010✔
4045
  return pStmt;
1,472,010✔
4046
}
4047

4048
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
3,110,078✔
4049
                              EOperatorType tableCondType) {
4050
  CHECK_PARSER_STATUS(pCxt);
3,110,078✔
4051
  if (needDbShowStmt(type) && NULL == pDbName) {
3,110,078✔
4052
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
2,210✔
4053
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2,210✔
4054
    CHECK_PARSER_STATUS(pCxt);
2,210✔
4055
  }
4056
  SShowStmt* pStmt = NULL;
3,107,868✔
4057
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,107,868✔
4058
  CHECK_MAKE_NODE(pStmt);
3,107,868✔
4059
  pStmt->pDbName = pDbName;
3,107,868✔
4060
  pStmt->pTbName = pTbName;
3,107,868✔
4061
  pStmt->tableCondType = tableCondType;
3,107,868✔
4062
  return (SNode*)pStmt;
3,107,868✔
4063
_err:
2,210✔
4064
  nodesDestroyNode(pDbName);
2,210✔
4065
  nodesDestroyNode(pTbName);
2,210✔
4066
  return NULL;
2,210✔
4067
}
4068

4069
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
523,174✔
4070
                            EOperatorType tableCondType) {
4071
  CHECK_PARSER_STATUS(pCxt);
523,174✔
4072
  SNode* pDbName = NULL;
523,174✔
4073
  if (option.dbName.type == TK_NK_NIL) {
523,174✔
4074
    pDbName = createDefaultDatabaseCondValue(pCxt);
184,096✔
4075
  } else {
4076
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
339,078✔
4077
  }
4078

4079
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
523,174✔
UNCOV
4080
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
4081
    return NULL;
×
4082
  }
4083

4084
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
523,174✔
4085
  CHECK_PARSER_STATUS(pCxt);
523,174✔
4086
  (void)setShowKind(pCxt, pStmt, option.kind);
520,992✔
4087
  return pStmt;
520,992✔
4088
_err:
2,182✔
4089
  nodesDestroyNode(pTbName);
2,182✔
4090
  return NULL;
2,182✔
4091
}
4092

4093
SNode* createShowVTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
121,254✔
4094
                             EOperatorType tableCondType) {
4095
  CHECK_PARSER_STATUS(pCxt);
121,254✔
4096
  SNode* pDbName = NULL;
121,254✔
4097
  if (option.dbName.type == TK_NK_NIL) {
121,254✔
4098
    pDbName = createDefaultDatabaseCondValue(pCxt);
7,784✔
4099
  } else {
4100
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
113,470✔
4101
  }
4102

4103
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
121,254✔
UNCOV
4104
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
4105
    return NULL;
×
4106
  }
4107

4108
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_VTABLES_STMT, pDbName, pTbName, tableCondType);
121,254✔
4109
  CHECK_PARSER_STATUS(pCxt);
121,254✔
4110
  (void)setShowKind(pCxt, pStmt, option.kind);
121,254✔
4111
  return pStmt;
121,254✔
UNCOV
4112
_err:
×
UNCOV
4113
  nodesDestroyNode(pTbName);
×
UNCOV
4114
  return NULL;
×
4115
}
4116

4117
SNode* createShowSTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
458,472✔
4118
                             EOperatorType tableCondType) {
4119
  CHECK_PARSER_STATUS(pCxt);
458,472✔
4120
  SNode* pDbName = NULL;
458,472✔
4121
  if (option.dbName.type == TK_NK_NIL) {
458,472✔
4122
    pDbName = createDefaultDatabaseCondValue(pCxt);
127,556✔
4123
  } else {
4124
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
330,916✔
4125
  }
4126

4127
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_VIRTUAL &&
458,472✔
4128
      option.kind != SHOW_KIND_ALL) {
455,936✔
4129
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4130
    return NULL;
×
4131
  }
4132

4133
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_STABLES_STMT, pDbName, pTbName, tableCondType);
458,472✔
4134
  CHECK_PARSER_STATUS(pCxt);
458,472✔
4135
  (void)setShowKind(pCxt, pStmt, option.kind);
458,472✔
4136
  return pStmt;
458,472✔
UNCOV
4137
_err:
×
UNCOV
4138
  nodesDestroyNode(pTbName);
×
UNCOV
4139
  return NULL;
×
4140
}
4141

4142
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
131,428✔
4143
  CHECK_PARSER_STATUS(pCxt);
131,428✔
4144
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
131,428✔
4145
  SShowCreateDatabaseStmt* pStmt = NULL;
131,428✔
4146
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
131,428✔
4147
  CHECK_MAKE_NODE(pStmt);
131,428✔
4148
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
131,428✔
4149
  return (SNode*)pStmt;
131,428✔
UNCOV
4150
_err:
×
UNCOV
4151
  return NULL;
×
4152
}
4153

4154
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
9,878✔
4155
  CHECK_PARSER_STATUS(pCxt);
9,878✔
4156
  SToken  dbToken = {0};
9,878✔
4157
  SToken* pDbToken = NULL;
9,878✔
4158

4159
  if (pNode) {
9,878✔
4160
    SValueNode* pDbName = (SValueNode*)pNode;
3,202✔
4161
    if (pDbName->literal) {
3,202✔
4162
      dbToken.z = pDbName->literal;
3,202✔
4163
      dbToken.n = strlen(pDbName->literal);
3,202✔
4164
      pDbToken = &dbToken;
3,202✔
4165
    }
4166
  }
4167

4168
  if (pDbToken) {
9,878✔
4169
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
3,202✔
4170
  }
4171

4172
  SShowAliveStmt* pStmt = NULL;
9,878✔
4173
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
9,878✔
4174
  CHECK_PARSER_STATUS(pCxt);
9,878✔
4175

4176
  if (pDbToken) {
9,878✔
4177
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
3,202✔
4178
  }
4179
  if (pNode) {
9,878✔
4180
    nodesDestroyNode(pNode);
3,202✔
4181
  }
4182

4183
  return (SNode*)pStmt;
9,878✔
UNCOV
4184
_err:
×
UNCOV
4185
  nodesDestroyNode(pNode);
×
UNCOV
4186
  return NULL;
×
4187
}
4188

4189
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
172,368✔
4190
  CHECK_PARSER_STATUS(pCxt);
172,368✔
4191
  SShowCreateTableStmt* pStmt = NULL;
171,560✔
4192
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
171,560✔
4193
  CHECK_MAKE_NODE(pStmt);
171,560✔
4194
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
171,560✔
4195
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
171,560✔
4196
  nodesDestroyNode(pRealTable);
171,560✔
4197
  return (SNode*)pStmt;
171,560✔
4198
_err:
808✔
4199
  nodesDestroyNode(pRealTable);
808✔
4200
  return NULL;
808✔
4201
}
4202

4203
SNode* createShowCreateVTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
14,350✔
4204
  CHECK_PARSER_STATUS(pCxt);
14,350✔
4205
  SShowCreateTableStmt* pStmt = NULL;
14,350✔
4206
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
14,350✔
4207
  CHECK_MAKE_NODE(pStmt);
14,350✔
4208
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
14,350✔
4209
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
14,350✔
4210
  nodesDestroyNode(pRealTable);
14,350✔
4211
  return (SNode*)pStmt;
14,350✔
UNCOV
4212
_err:
×
UNCOV
4213
  nodesDestroyNode(pRealTable);
×
UNCOV
4214
  return NULL;
×
4215
}
4216

4217
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
16,704✔
4218
  CHECK_PARSER_STATUS(pCxt);
16,704✔
4219
  SShowCreateViewStmt* pStmt = NULL;
16,704✔
4220
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
16,704✔
4221
  CHECK_MAKE_NODE(pStmt);
16,704✔
4222
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
16,704✔
4223
  tstrncpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
16,704✔
4224
  nodesDestroyNode(pRealTable);
16,704✔
4225
  return (SNode*)pStmt;
16,704✔
UNCOV
4226
_err:
×
UNCOV
4227
  nodesDestroyNode(pRealTable);
×
4228
  return NULL;
×
4229
}
4230

4231
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
7,518✔
4232
  CHECK_PARSER_STATUS(pCxt);
7,518✔
4233
  SShowTableDistributedStmt* pStmt = NULL;
7,518✔
4234
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
7,518✔
4235
  CHECK_MAKE_NODE(pStmt);
7,518✔
4236
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
7,518✔
4237
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
7,518✔
4238
  nodesDestroyNode(pRealTable);
7,518✔
4239
  return (SNode*)pStmt;
7,518✔
UNCOV
4240
_err:
×
UNCOV
4241
  nodesDestroyNode(pRealTable);
×
4242
  return NULL;
×
4243
}
4244

4245
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
73,252✔
4246
  CHECK_PARSER_STATUS(pCxt);
73,252✔
4247
  SShowDnodeVariablesStmt* pStmt = NULL;
73,252✔
4248
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
73,252✔
4249
  CHECK_MAKE_NODE(pStmt);
73,252✔
4250
  pStmt->pDnodeId = pDnodeId;
73,252✔
4251
  pStmt->pLikePattern = pLikePattern;
73,252✔
4252
  return (SNode*)pStmt;
73,252✔
UNCOV
4253
_err:
×
UNCOV
4254
  nodesDestroyNode(pDnodeId);
×
UNCOV
4255
  nodesDestroyNode(pLikePattern);
×
4256
  return NULL;
×
4257
}
4258

4259
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
10,686✔
4260
  CHECK_PARSER_STATUS(pCxt);
10,686✔
4261
  SShowVnodesStmt* pStmt = NULL;
10,686✔
4262
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
10,686✔
4263
  CHECK_MAKE_NODE(pStmt);
10,686✔
4264
  pStmt->pDnodeId = pDnodeId;
10,686✔
4265
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
10,686✔
4266
  return (SNode*)pStmt;
10,686✔
UNCOV
4267
_err:
×
UNCOV
4268
  nodesDestroyNode(pDnodeId);
×
4269
  nodesDestroyNode(pDnodeEndpoint);
×
4270
  return NULL;
×
4271
}
4272

4273
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
13,118✔
4274
  CHECK_PARSER_STATUS(pCxt);
13,118✔
4275
  if (NULL == pDbName) {
13,118✔
UNCOV
4276
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
UNCOV
4277
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
4278
    CHECK_PARSER_STATUS(pCxt);
×
4279
  }
4280
  SShowTableTagsStmt* pStmt = NULL;
13,118✔
4281
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
13,118✔
4282
  CHECK_MAKE_NODE(pStmt);
13,118✔
4283
  pStmt->pDbName = pDbName;
13,118✔
4284
  pStmt->pTbName = pTbName;
13,118✔
4285
  pStmt->pTags = pTags;
13,118✔
4286
  return (SNode*)pStmt;
13,118✔
UNCOV
4287
_err:
×
UNCOV
4288
  nodesDestroyNode(pTbName);
×
UNCOV
4289
  nodesDestroyNode(pDbName);
×
UNCOV
4290
  nodesDestroyList(pTags);
×
UNCOV
4291
  return NULL;
×
4292
}
4293

4294
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
60,764✔
4295
  CHECK_PARSER_STATUS(pCxt);
60,764✔
4296
  SShowCompactDetailsStmt* pStmt = NULL;
60,764✔
4297
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
60,764✔
4298
  CHECK_MAKE_NODE(pStmt);
60,764✔
4299
  pStmt->pId = pCompactId;
60,764✔
4300
  return (SNode*)pStmt;
60,764✔
UNCOV
4301
_err:
×
UNCOV
4302
  nodesDestroyNode(pCompactId);
×
4303
  return NULL;
×
4304
}
4305

4306
SNode* createShowRetentionDetailsStmt(SAstCreateContext* pCxt, SNode* pId) {
4,470✔
4307
  CHECK_PARSER_STATUS(pCxt);
4,470✔
4308
  SShowRetentionDetailsStmt* pStmt = NULL;
4,470✔
4309
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_RETENTION_DETAILS_STMT, (SNode**)&pStmt);
4,470✔
4310
  CHECK_MAKE_NODE(pStmt);
4,470✔
4311
  pStmt->pId = pId;
4,470✔
4312
  return (SNode*)pStmt;
4,470✔
UNCOV
4313
_err:
×
UNCOV
4314
  nodesDestroyNode(pId);
×
UNCOV
4315
  return NULL;
×
4316
}
4317

4318
SNode* createShowTransactionDetailsStmt(SAstCreateContext* pCxt, SNode* pTransactionIdNode) {
584✔
4319
  CHECK_PARSER_STATUS(pCxt);
584✔
4320
  SShowTransactionDetailsStmt* pStmt = NULL;
584✔
4321
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT, (SNode**)&pStmt);
584✔
4322
  CHECK_MAKE_NODE(pStmt);
584✔
4323
  pStmt->pTransactionId = pTransactionIdNode;
584✔
4324
  return (SNode*)pStmt;
584✔
UNCOV
4325
_err:
×
UNCOV
4326
  nodesDestroyNode(pTransactionIdNode);
×
UNCOV
4327
  return NULL;
×
4328
}
4329

4330

4331

4332
static bool parseIp(const char* strIp, SIpRange* pIpRange) {
10,504✔
4333
  if (strchr(strIp, ':') == NULL) {
10,504✔
4334
    struct in_addr ip4;
10,504✔
4335
    if (inet_pton(AF_INET, strIp, &ip4) == 1) {
10,504✔
4336
      pIpRange->type = 0;
8,732✔
4337
      memcpy(&pIpRange->ipV4.ip, &ip4.s_addr, sizeof(ip4.s_addr));
8,732✔
4338
      return true;
8,732✔
4339
    }
4340
  } else {
4341
    struct in6_addr ip6;
×
4342
    if (inet_pton(AF_INET6, strIp, &ip6) == 1) {
×
4343
      pIpRange->type = 1;
×
UNCOV
4344
      memcpy(&pIpRange->ipV6.addr[0], ip6.s6_addr, 8);
×
UNCOV
4345
      memcpy(&pIpRange->ipV6.addr[1], ip6.s6_addr + 8, 8);
×
UNCOV
4346
      return true;
×
4347
    }
4348
  }
4349

4350
  return false;
1,772✔
4351
}
4352

4353

4354

4355
SIpRangeNode* parseIpRange(SAstCreateContext* pCxt, const SToken* token) {
10,504✔
4356
  CHECK_PARSER_STATUS(pCxt);
10,504✔
4357

4358
#ifdef TD_ASTRA
4359
  return NULL;
4360
#else
4361

4362
  SIpRangeNode* node = NULL;
10,504✔
4363
  int32_t code = nodesMakeNode(QUERY_NODE_IP_RANGE, (SNode**)&node);
10,504✔
4364
  if (node == NULL) {
10,504✔
UNCOV
4365
    goto _err;
×
4366
  }
4367

4368
  char buf[64];
10,504✔
4369
  if (token->n >= sizeof(buf)) {
10,504✔
UNCOV
4370
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
UNCOV
4371
    goto _err;
×
4372
  }
4373
  memcpy(buf, token->z, token->n);
10,504✔
4374
  buf[token->n] = '\0';
10,504✔
4375
  (void)strdequote(buf);
10,504✔
4376

4377
  char* slash = strchr(buf, '/');
10,504✔
4378
  if (slash) {
10,504✔
4379
    *slash = '\0';
3,720✔
4380
  }
4381

4382
  if (!parseIp(buf, &node->range)) {
10,504✔
4383
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
1,772✔
4384
    goto _err;
1,772✔
4385
  }
4386

4387
  int32_t mask = 0;
8,732✔
4388
  if (!slash) {
8,732✔
4389
    mask = node->range.type == 0 ? 32 : 128;
5,344✔
4390
  } else if (taosStr2int32(slash + 1, &mask) != TSDB_CODE_SUCCESS) {
3,388✔
UNCOV
4391
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
UNCOV
4392
    goto _err;
×
4393
  }
4394

4395
  code = tIpRangeSetMask(&node->range, mask);
8,732✔
4396
  if (code != TSDB_CODE_SUCCESS) {
8,732✔
4397
    goto _err;
908✔
4398
  }
4399

4400
  return node;
7,824✔
4401

4402
_err:
2,680✔
4403
  pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
2,680✔
4404
  nodesDestroyNode((SNode*)node);
2,680✔
4405
  return NULL;
2,680✔
4406

4407
#endif
4408
}
4409

4410

4411

4412
SDateTimeRangeNode* parseDateTimeRange(SAstCreateContext* pCxt, const SToken* token) {
9,792✔
4413
  CHECK_PARSER_STATUS(pCxt);
9,792✔
4414

4415
  SDateTimeRangeNode* node = NULL;
9,792✔
4416
  int32_t code = nodesMakeNode(QUERY_NODE_DATE_TIME_RANGE, (SNode**)&node);
9,792✔
4417
  if (code != TSDB_CODE_SUCCESS) {
9,792✔
UNCOV
4418
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
×
UNCOV
4419
    goto _err;
×
4420
  }
4421

4422
  char buf[128];
9,792✔
4423
  if (token->n >= sizeof(buf)) {
9,792✔
UNCOV
4424
    code = TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG;
×
UNCOV
4425
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Date time range string is too long");
×
UNCOV
4426
    goto _err;
×
4427
  }
4428
  memcpy(buf, token->z, token->n);
9,792✔
4429
  buf[token->n] = '\0';
9,792✔
4430
  (void)strdequote(buf);
9,792✔
4431

4432
  code = TSDB_CODE_PAR_INVALID_OPTION_VALUE;
9,792✔
4433
  int32_t year = 0, month = 0, day = 0, hour = 0, minute = 0, duration = 0;
9,792✔
4434
  if (buf[0] >= '1' && buf[0] <= '9') {
14,976✔
4435
    // format: YYYY-MM-DD HH:MM duration
4436
    int ret = sscanf(buf, "%d-%d-%d %d:%d %d", &year, &month, &day, &hour, &minute, &duration);
7,488✔
4437
    if (ret != 6) {
7,488✔
4438
      goto _err;
1,728✔
4439
    }
4440
    if (month < 1 || month > 12) {
5,760✔
4441
      goto _err;
576✔
4442
    }
4443
  } else {
4444
    // format: WEEKDAY HH:MM duration
4445
    char weekday[4];
2,304✔
4446
    int ret = sscanf(buf, "%3s %d:%d %d", weekday, &hour, &minute, &duration);
2,304✔
4447
    if (ret != 4) {
2,304✔
4448
      goto _err;
576✔
4449
    }
4450
    day = taosParseShortWeekday(weekday);
1,728✔
4451
    if (day < 0 || day > 6) {
1,728✔
UNCOV
4452
      goto _err;
×
4453
    }
4454
    month = -1;
1,728✔
4455
  }
4456

4457
  node->range.year = (int16_t)year;
6,912✔
4458
  node->range.month = (int8_t)month;
6,912✔
4459
  node->range.day = (int8_t)day;
6,912✔
4460
  node->range.hour = (int8_t)hour;
6,912✔
4461
  node->range.minute = (int8_t)minute;
6,912✔
4462
  node->range.duration = duration;
6,912✔
4463
  if (!isValidDateTimeRange(&node->range)) {
6,912✔
UNCOV
4464
    goto _err;
×
4465
  }
4466

4467
  return node;
6,912✔
4468

4469
_err:
2,880✔
4470
  if (code == TSDB_CODE_PAR_INVALID_OPTION_VALUE) { // other error types have been set above
2,880✔
4471
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid date time range");
2,880✔
4472
  }
4473
  nodesDestroyNode((SNode*)node);
2,880✔
4474
  return NULL;
2,880✔
4475
}
4476

4477

4478
SUserOptions* createDefaultUserOptions(SAstCreateContext* pCxt) {
361,728✔
4479
  SUserOptions* pOptions = NULL;
361,728✔
4480
  int32_t code = nodesMakeNode(QUERY_NODE_USER_OPTIONS, (SNode**)&pOptions);
361,728✔
4481
  if (pOptions == NULL) {
361,728✔
UNCOV
4482
    pCxt->errCode = code;
×
UNCOV
4483
    return NULL;
×
4484
  }
4485

4486
  pOptions->enable = 1;
361,728✔
4487
  pOptions->sysinfo = 1;
361,728✔
4488
  pOptions->createdb = 0;
361,728✔
4489
  pOptions->isImport = 0;
361,728✔
4490
  pOptions->changepass = 2;
361,728✔
4491

4492
  pOptions->sessionPerUser = TSDB_USER_SESSION_PER_USER_DEFAULT;
361,728✔
4493
  pOptions->connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
361,728✔
4494
  pOptions->connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
361,728✔
4495
  pOptions->callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
361,728✔
4496
  pOptions->vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
361,728✔
4497
  pOptions->failedLoginAttempts = TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
361,728✔
4498
  pOptions->passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
361,728✔
4499
  pOptions->passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
361,728✔
4500
  pOptions->passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
361,728✔
4501
  pOptions->passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
361,728✔
4502
  pOptions->passwordGraceTime = TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
361,728✔
4503
  pOptions->inactiveAccountTime = TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
361,728✔
4504
  pOptions->allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
361,728✔
4505

4506
  return pOptions;
361,728✔
4507
}
4508

4509

4510

4511
SUserOptions* mergeUserOptions(SAstCreateContext* pCxt, SUserOptions* a, SUserOptions* b) {
429,550✔
4512
  if (a == NULL && b == NULL) {
429,550✔
4513
      return createDefaultUserOptions(pCxt);
361,728✔
4514
  }
4515
  if (b == NULL) {
67,822✔
4516
    return a;
42,018✔
4517
  }
4518
  if (a == NULL) {
25,804✔
UNCOV
4519
    return b;
×
4520
  }
4521

4522
  if (b->hasPassword) {
25,804✔
UNCOV
4523
    if (a->hasPassword) {
×
UNCOV
4524
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASS");
×
4525
    } else {
UNCOV
4526
      a->hasPassword = true;
×
UNCOV
4527
      tstrncpy(a->password, b->password, sizeof(a->password));
×
4528
    }
4529
  }
4530

4531
  if (b->hasTotpseed) {
25,804✔
UNCOV
4532
    if (a->hasTotpseed) {
×
UNCOV
4533
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TOTPSEED");
×
4534
    } else {
4535
      a->hasTotpseed = true;
×
UNCOV
4536
      tstrncpy(a->totpseed, b->totpseed, sizeof(a->totpseed));
×
4537
    }
4538
  }
4539

4540
  if (b->hasEnable) {
25,804✔
UNCOV
4541
    if (a->hasEnable) {
×
4542
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE/ACCOUNT LOCK/ACCOUNT UNLOCK");
×
4543
    } else {
UNCOV
4544
      a->hasEnable = true;
×
UNCOV
4545
      a->enable = b->enable;
×
4546
    }
4547
  }
4548

4549
  if (b->hasSysinfo) {
25,804✔
4550
    if (a->hasSysinfo) {
332✔
4551
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SYSINFO");
×
4552
    } else {
4553
      a->hasSysinfo = true;
332✔
4554
      a->sysinfo = b->sysinfo;
332✔
4555
    }
4556
  }
4557

4558
  if (b->hasCreatedb) {
25,804✔
4559
    if (a->hasCreatedb) {
4,084✔
4560
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CREATEDB");
×
4561
    } else {
4562
      a->hasCreatedb = true;
4,084✔
4563
      a->createdb = b->createdb;
4,084✔
4564
    }
4565
  }
4566

4567
  if (b->hasChangepass) {
25,804✔
UNCOV
4568
    if (a->hasChangepass) {
×
UNCOV
4569
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CHANGEPASS");
×
4570
    } else {
UNCOV
4571
      a->hasChangepass = true;
×
UNCOV
4572
      a->changepass = b->changepass;
×
4573
    }
4574
  }
4575

4576
  if (b->hasSessionPerUser) {
25,804✔
UNCOV
4577
    if (a->hasSessionPerUser) {
×
UNCOV
4578
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SESSION_PER_USER");
×
4579
    } else {
UNCOV
4580
      a->hasSessionPerUser = true;
×
UNCOV
4581
      a->sessionPerUser = b->sessionPerUser;
×
4582
    }
4583
  }
4584

4585
  if (b->hasConnectTime) {
25,804✔
4586
    if (a->hasConnectTime) {
576✔
4587
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_TIME");
×
4588
    } else {
4589
      a->hasConnectTime = true;
576✔
4590
      a->connectTime = b->connectTime;
576✔
4591
    }
4592
  }
4593

4594
  if (b->hasConnectIdleTime) {
25,804✔
4595
    if (a->hasConnectIdleTime) {
576✔
4596
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_IDLE_TIME");
×
4597
    } else {
4598
      a->hasConnectIdleTime = true;
576✔
4599
      a->connectIdleTime = b->connectIdleTime;
576✔
4600
    }
4601
  }
4602

4603
  if (b->hasCallPerSession) {
25,804✔
4604
    if (a->hasCallPerSession) {
576✔
UNCOV
4605
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CALLS_PER_SESSION");
×
4606
    } else {
4607
      a->hasCallPerSession = true;
576✔
4608
      a->callPerSession = b->callPerSession;
576✔
4609
    }
4610
  }
4611

4612
  if (b->hasVnodePerCall) {
25,804✔
4613
    if (a->hasVnodePerCall) {
864✔
UNCOV
4614
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "VNODES_PER_CALL");
×
4615
    } else {
4616
      a->hasVnodePerCall = true;
864✔
4617
      a->vnodePerCall = b->vnodePerCall;
864✔
4618
    }
4619
  }
4620

4621
  if (b->hasFailedLoginAttempts) {
25,804✔
4622
    if (a->hasFailedLoginAttempts) {
576✔
UNCOV
4623
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "FAILED_LOGIN_ATTEMPTS");
×
4624
    } else {
4625
      a->hasFailedLoginAttempts = true;
576✔
4626
      a->failedLoginAttempts = b->failedLoginAttempts;
576✔
4627
    }
4628
  }
4629

4630
  if (b->hasPasswordLifeTime) {
25,804✔
4631
    if (a->hasPasswordLifeTime) {
576✔
UNCOV
4632
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LIFE_TIME");
×
4633
    } else {
4634
      a->hasPasswordLifeTime = true;
576✔
4635
      a->passwordLifeTime = b->passwordLifeTime;
576✔
4636
    }
4637
  }
4638

4639
  if (b->hasPasswordReuseTime) {
25,804✔
4640
    if (a->hasPasswordReuseTime) {
3,988✔
UNCOV
4641
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_TIME");
×
4642
    } else {
4643
      a->hasPasswordReuseTime = true;
3,988✔
4644
      a->passwordReuseTime = b->passwordReuseTime;
3,988✔
4645
    }
4646
  }
4647

4648
  if (b->hasPasswordReuseMax) {
25,804✔
4649
    if (a->hasPasswordReuseMax) {
3,648✔
UNCOV
4650
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_MAX");
×
4651
    } else {
4652
      a->hasPasswordReuseMax = true;
3,648✔
4653
      a->passwordReuseMax = b->passwordReuseMax;
3,648✔
4654
    }
4655
  }
4656

4657
  if (b->hasPasswordLockTime) {
25,804✔
4658
    if (a->hasPasswordLockTime) {
576✔
UNCOV
4659
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LOCK_TIME");
×
4660
    } else {
4661
      a->hasPasswordLockTime = true;
576✔
4662
      a->passwordLockTime = b->passwordLockTime;
576✔
4663
    }
4664
  }
4665

4666
  if (b->hasPasswordGraceTime) {
25,804✔
4667
    if (a->hasPasswordGraceTime) {
576✔
UNCOV
4668
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_GRACE_TIME");
×
4669
    } else {
4670
      a->hasPasswordGraceTime = true;
576✔
4671
      a->passwordGraceTime = b->passwordGraceTime;
576✔
4672
    }
4673
  }
4674

4675
  if (b->hasInactiveAccountTime) {
25,804✔
4676
    if (a->hasInactiveAccountTime) {
576✔
UNCOV
4677
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "INACTIVE_ACCOUNT_TIME");
×
4678
    } else {
4679
      a->hasInactiveAccountTime = true;
576✔
4680
      a->inactiveAccountTime = b->inactiveAccountTime;
576✔
4681
    }
4682
  }
4683

4684
  if (b->hasAllowTokenNum) {
25,804✔
4685
    if (a->hasAllowTokenNum) {
576✔
UNCOV
4686
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ALLOW_TOKEN_NUM");
×
4687
    } else {
4688
      a->hasAllowTokenNum = true;
576✔
4689
      a->allowTokenNum = b->allowTokenNum;
576✔
4690
    }
4691
  }
4692

4693
  if (b->pIpRanges != NULL) {
25,804✔
4694
    if (a->pIpRanges == NULL) {
2,952✔
4695
      a->pIpRanges = b->pIpRanges;
2,376✔
4696
    } else {
4697
      int32_t code = nodesListAppendList(a->pIpRanges, b->pIpRanges);
576✔
4698
      if (code != TSDB_CODE_SUCCESS) {
576✔
UNCOV
4699
        pCxt->errCode = code;
×
4700
      }
4701
    }
4702
    b->pIpRanges = NULL;
2,952✔
4703
  }
4704

4705
  if (b->pDropIpRanges != NULL) {
25,804✔
4706
    if (a->pDropIpRanges == NULL) {
288✔
UNCOV
4707
      a->pDropIpRanges = b->pDropIpRanges;
×
4708
    } else {
4709
      int32_t code = nodesListAppendList(a->pDropIpRanges, b->pDropIpRanges);
288✔
4710
      if (code != TSDB_CODE_SUCCESS) {
288✔
UNCOV
4711
        pCxt->errCode = code;
×
4712
      }
4713
    }
4714
    b->pDropIpRanges = NULL;
288✔
4715
  }
4716

4717
  if (b->pTimeRanges != NULL) {
25,804✔
4718
    if (a->pTimeRanges == NULL) {
1,728✔
4719
      a->pTimeRanges = b->pTimeRanges;
1,152✔
4720
    } else {
4721
      int32_t code = nodesListAppendList(a->pTimeRanges, b->pTimeRanges);
576✔
4722
      if (code != TSDB_CODE_SUCCESS) {
576✔
4723
        pCxt->errCode = code;
×
4724
      }
4725
    }
4726
    b->pTimeRanges = NULL;
1,728✔
4727
  }
4728

4729
  if (b->pDropTimeRanges != NULL) {
25,804✔
4730
    if (a->pDropTimeRanges == NULL) {
288✔
UNCOV
4731
      a->pDropTimeRanges = b->pDropTimeRanges;
×
4732
    } else {
4733
      int32_t code = nodesListAppendList(a->pDropTimeRanges, b->pDropTimeRanges);
288✔
4734
      if (code != TSDB_CODE_SUCCESS) {
288✔
UNCOV
4735
        pCxt->errCode = code;
×
4736
      }
4737
    }
4738
    b->pDropTimeRanges = NULL;
288✔
4739
  }
4740

4741
  nodesDestroyNode((SNode*)b);
25,804✔
4742
  return a;
25,804✔
4743
}
4744

4745

4746

4747
void setUserOptionsTotpseed(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pTotpseed) {
×
UNCOV
4748
  pUserOptions->hasTotpseed = true;
×
4749

UNCOV
4750
  if (pTotpseed == NULL) { // clear TOTP secret
×
4751
    memset(pUserOptions->totpseed, 0, sizeof(pUserOptions->totpseed));
×
UNCOV
4752
    return;
×
4753
  }
4754

UNCOV
4755
  if (pTotpseed->n >= sizeof(pUserOptions->totpseed) * 2) {
×
UNCOV
4756
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
UNCOV
4757
    return;
×
4758
  }
4759

UNCOV
4760
  char buf[sizeof(pUserOptions->totpseed) * 2 + 1];
×
UNCOV
4761
  memcpy(buf, pTotpseed->z, pTotpseed->n);
×
UNCOV
4762
  buf[pTotpseed->n] = 0;
×
4763
  (void)strdequote(buf);
×
4764
  size_t len = strtrim(buf);
×
4765

4766
  if (len >= sizeof(pUserOptions->totpseed)) {
×
4767
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
4768
  } else if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
UNCOV
4769
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT, "TOTPSEED", TSDB_USER_TOTPSEED_MIN_LEN);
×
4770
  } else {
4771
    tstrncpy(pUserOptions->totpseed, buf, sizeof(pUserOptions->totpseed));
×
4772
  }
4773
}
4774

4775

4776

4777
void setUserOptionsPassword(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pPassword) {
293,632✔
4778
  pUserOptions->hasPassword = true;
293,632✔
4779

4780
  if (pPassword->n >= sizeof(pUserOptions->password) * 2) {
293,632✔
UNCOV
4781
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
4782
    return;
×
4783
  }
4784

4785
  char buf[sizeof(pUserOptions->password) * 2 + 1];
288,366✔
4786
  memcpy(buf, pPassword->z, pPassword->n);
293,632✔
4787
  buf[pPassword->n] = 0;
293,632✔
4788
  (void)strdequote(buf);
293,632✔
4789
  size_t len = strtrim(buf);
293,632✔
4790

4791
  if (len >= sizeof(pUserOptions->password)) {
293,632✔
4792
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
1,340✔
4793
  } else if (len < TSDB_PASSWORD_MIN_LEN) {
292,292✔
4794
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
10,846✔
4795
  } else {
4796
    tstrncpy(pUserOptions->password, buf, sizeof(pUserOptions->password));
281,446✔
4797
  }
4798
}
4799

4800

4801

4802
static bool isValidUserOptions(SAstCreateContext* pCxt, const SUserOptions* opts) {
316,938✔
4803
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
316,938✔
4804
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
2,420✔
4805
    return false;
2,420✔
4806
  }
4807

4808
  if (opts->hasSysinfo && (opts->sysinfo < 0 || opts->sysinfo > 1)) {
314,518✔
4809
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SYSINFO");
4,476✔
4810
    return false;
4,476✔
4811
  }
4812

4813
  if (opts->hasIsImport && (opts->isImport < 0 || opts->isImport > 1)) {
310,042✔
UNCOV
4814
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "IS_IMPORT");
×
UNCOV
4815
    return false;
×
4816
  }
4817

4818
  if (opts->hasCreatedb && (opts->createdb < 0 || opts->createdb > 1)) {
310,042✔
4819
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CREATEDB");
2,420✔
4820
    return false;
2,420✔
4821
  }
4822

4823
  if (opts->hasTotpseed && opts->totpseed[0] != 0 && !taosIsComplexString(opts->totpseed)) {
307,622✔
UNCOV
4824
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TOTPSEED");
×
UNCOV
4825
    return false;
×
4826
  }
4827

4828
  if (opts->hasPassword && !isValidPassword(pCxt, opts->password, opts->hasIsImport && opts->isImport)) {
307,622✔
UNCOV
4829
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
×
4830
    return false;
×
4831
  }
4832

4833
  if (opts->hasChangepass && (opts->changepass < 0 || opts->changepass > 2)) {
307,622✔
4834
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CHANGEPASS");
288✔
4835
    return false;
288✔
4836
  }
4837

4838
  if (opts->hasSessionPerUser && (opts->sessionPerUser < -1 || opts->sessionPerUser == 0)) {
307,334✔
UNCOV
4839
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SESSION_PER_USER");
×
4840
    return false;
×
4841
  }
4842

4843
  if (opts->hasConnectTime && (opts->connectTime < -1 || opts->connectTime == 0)) {
307,334✔
UNCOV
4844
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_TIME");
×
4845
    return false;
×
4846
  }
4847

4848
  if (opts->hasConnectIdleTime && (opts->connectIdleTime < -1 || opts->connectIdleTime == 0)) {
307,334✔
UNCOV
4849
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_IDLE_TIME");
×
UNCOV
4850
    return false;
×
4851
  }
4852

4853
  if (opts->hasCallPerSession && (opts->callPerSession < -1 || opts->callPerSession == 0)) {
307,334✔
UNCOV
4854
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CALLS_PER_SESSION");
×
4855
    return false;
×
4856
  }
4857

4858
  if (opts->hasVnodePerCall && (opts->vnodePerCall < -1 || opts->vnodePerCall == 0)) {
307,334✔
4859
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "VNODES_PER_CALL");
288✔
4860
    return false;
288✔
4861
  }
4862

4863
  if (opts->hasFailedLoginAttempts && (opts->failedLoginAttempts < -1 || opts->failedLoginAttempts == 0)) {
307,046✔
4864
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "FAILED_LOGIN_ATTEMPTS");
288✔
4865
    return false;
288✔
4866
  }
4867

4868
  if (opts->hasPasswordLockTime && (opts->passwordLockTime < -1 || opts->passwordLockTime == 0)) {
306,758✔
4869
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LOCK_TIME");
288✔
4870
    return false;
288✔
4871
  }
4872

4873
  if (opts->hasPasswordLifeTime && opts->passwordLifeTime != -1 && opts->passwordLifeTime < TSDB_USER_PASSWORD_LIFE_TIME_MIN) {
306,470✔
4874
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LIFE_TIME");
288✔
4875
    return false;
288✔
4876
  }
4877

4878
  if (opts->hasPasswordGraceTime && (opts->passwordGraceTime < -1)) {
306,182✔
UNCOV
4879
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_GRACE_TIME");
×
UNCOV
4880
    return false;
×
4881
  }
4882

4883
  if (opts->hasPasswordReuseTime && (opts->passwordReuseTime < 0 || opts->passwordReuseTime > TSDB_USER_PASSWORD_REUSE_TIME_MAX)) {
306,182✔
4884
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_TIME");
288✔
4885
    return false;
288✔
4886
  }
4887

4888
  if (opts->hasPasswordReuseMax && (opts->passwordReuseMax < 0 || opts->passwordReuseMax > TSDB_USER_PASSWORD_REUSE_MAX_MAX)) {
305,894✔
4889
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_MAX");
288✔
4890
    return false;
288✔
4891
  }
4892

4893
  if (opts->hasInactiveAccountTime && (opts->inactiveAccountTime < -1 || opts->inactiveAccountTime == 0)) {
305,606✔
4894
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "INACTIVE_ACCOUNT_TIME");
288✔
4895
    return false;
288✔
4896
  }
4897

4898
  if (opts->hasAllowTokenNum && opts->allowTokenNum < -1) {
305,318✔
UNCOV
4899
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ALLOW_TOKEN_NUM");
×
UNCOV
4900
    return false;
×
4901
  }
4902

4903
  // ip ranges and date time ranges has been validated during parsing
4904

4905
  return true;
305,318✔
4906
}
4907

4908

4909

4910
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* opts, bool ignoreExists) {
221,990✔
4911
  SCreateUserStmt* pStmt = NULL;
221,990✔
4912

4913
  CHECK_PARSER_STATUS(pCxt);
221,990✔
4914
  CHECK_NAME(checkUserName(pCxt, pUserName));
208,100✔
4915

4916
  if (!isValidUserOptions(pCxt, opts)) {
207,508✔
4917
    goto _err;
4,724✔
4918
  }
4919

4920
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
202,784✔
4921
  CHECK_MAKE_NODE(pStmt);
202,784✔
4922

4923
  pStmt->hasSessionPerUser = opts->hasSessionPerUser;
202,784✔
4924
  pStmt->hasConnectTime = opts->hasConnectTime;
202,784✔
4925
  pStmt->hasConnectIdleTime = opts->hasConnectIdleTime;
202,784✔
4926
  pStmt->hasCallPerSession = opts->hasCallPerSession;
202,784✔
4927
  pStmt->hasVnodePerCall = opts->hasVnodePerCall;
202,784✔
4928
  pStmt->hasFailedLoginAttempts = opts->hasFailedLoginAttempts;
202,784✔
4929
  pStmt->hasPasswordLifeTime = opts->hasPasswordLifeTime;
202,784✔
4930
  pStmt->hasPasswordReuseTime = opts->hasPasswordReuseTime;
202,784✔
4931
  pStmt->hasPasswordReuseMax = opts->hasPasswordReuseMax;
202,784✔
4932
  pStmt->hasPasswordLockTime = opts->hasPasswordLockTime;
202,784✔
4933
  pStmt->hasPasswordGraceTime = opts->hasPasswordGraceTime;
202,784✔
4934
  pStmt->hasInactiveAccountTime = opts->hasInactiveAccountTime;
202,784✔
4935
  pStmt->hasAllowTokenNum = opts->hasAllowTokenNum;
202,784✔
4936

4937
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
202,784✔
4938
  tstrncpy(pStmt->password, opts->password, sizeof(pStmt->password));
202,784✔
4939
  tstrncpy(pStmt->totpseed, opts->totpseed, sizeof(pStmt->totpseed));
202,784✔
4940

4941
  pStmt->ignoreExists = ignoreExists;
202,784✔
4942
  pStmt->sysinfo = opts->sysinfo;
202,784✔
4943
  pStmt->createDb = opts->createdb;
202,784✔
4944
  pStmt->isImport = opts->isImport;
202,784✔
4945
  pStmt->changepass = opts->changepass;
202,784✔
4946
  pStmt->enable = opts->enable;
202,784✔
4947

4948
  pStmt->sessionPerUser = opts->sessionPerUser;
202,784✔
4949
  pStmt->connectTime = opts->connectTime;
202,784✔
4950
  pStmt->connectIdleTime = opts->connectIdleTime;
202,784✔
4951
  pStmt->callPerSession = opts->callPerSession;
202,784✔
4952
  pStmt->vnodePerCall = opts->vnodePerCall;
202,784✔
4953
  pStmt->failedLoginAttempts = opts->failedLoginAttempts;
202,784✔
4954
  pStmt->passwordLifeTime = opts->passwordLifeTime;
202,784✔
4955
  pStmt->passwordReuseTime = opts->passwordReuseTime;
202,784✔
4956
  pStmt->passwordReuseMax = opts->passwordReuseMax;
202,784✔
4957
  pStmt->passwordLockTime = opts->passwordLockTime;
202,784✔
4958
  pStmt->passwordGraceTime = opts->passwordGraceTime;
202,784✔
4959
  pStmt->inactiveAccountTime = opts->inactiveAccountTime;
202,784✔
4960
  pStmt->allowTokenNum = opts->allowTokenNum;
202,784✔
4961

4962
  pStmt->numIpRanges = LIST_LENGTH(opts->pIpRanges);
202,784✔
4963
  pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpRange));
202,784✔
4964
  CHECK_OUT_OF_MEM(pStmt->pIpRanges);
202,784✔
4965
  int i = 0;
202,784✔
4966
  SNode* pNode = NULL;
202,784✔
4967
  FOREACH(pNode, opts->pIpRanges) {
208,072✔
4968
    SIpRangeNode* node = (SIpRangeNode*)(pNode);
5,288✔
4969
    pStmt->pIpRanges[i++] = node->range;
5,288✔
4970
  }
4971

4972
  pStmt->numTimeRanges = LIST_LENGTH(opts->pTimeRanges);
202,784✔
4973
  pStmt->pTimeRanges = taosMemoryMalloc(pStmt->numTimeRanges * sizeof(SDateTimeRange));
202,784✔
4974
  CHECK_OUT_OF_MEM(pStmt->pTimeRanges);
202,784✔
4975
  i = 0;
202,784✔
4976
  pNode = NULL;
202,784✔
4977
  FOREACH(pNode, opts->pTimeRanges) {
207,392✔
4978
    SDateTimeRangeNode* node = (SDateTimeRangeNode*)(pNode);
4,608✔
4979
    pStmt->pTimeRanges[i++] = node->range;
4,608✔
4980
  }
4981

4982
  nodesDestroyNode((SNode*)opts);
202,784✔
4983
  return (SNode*)pStmt;
202,784✔
4984

4985
_err:
19,206✔
4986
  nodesDestroyNode((SNode*)pStmt);
19,206✔
4987
  nodesDestroyNode((SNode*)opts);
19,042✔
4988
  return NULL;
19,206✔
4989
}
4990

4991

4992

4993
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* pUserOptions) {
113,286✔
4994
  SAlterUserStmt* pStmt = NULL;
113,286✔
4995
  CHECK_PARSER_STATUS(pCxt);
113,286✔
4996
  CHECK_NAME(checkUserName(pCxt, pUserName));
109,430✔
4997
  if (!isValidUserOptions(pCxt, pUserOptions)) {
109,430✔
4998
    goto _err;
6,896✔
4999
  }
5000

5001
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
102,534✔
5002
  CHECK_MAKE_NODE(pStmt);
102,534✔
5003
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
102,534✔
5004
  pStmt->pUserOptions = pUserOptions;
102,534✔
5005
  return (SNode*)pStmt;
102,534✔
5006

5007
_err:
10,752✔
5008
  nodesDestroyNode((SNode*)pStmt);
10,752✔
5009
  nodesDestroyNode((SNode*)pUserOptions);
10,752✔
5010
  return NULL;
10,752✔
5011
}
5012

5013

5014

5015
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName, bool ignoreNotExists) {
97,988✔
5016
  CHECK_PARSER_STATUS(pCxt);
97,988✔
5017
  CHECK_NAME(checkUserName(pCxt, pUserName));
97,988✔
5018
  SDropUserStmt* pStmt = NULL;
97,988✔
5019
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
97,988✔
5020
  CHECK_MAKE_NODE(pStmt);
97,988✔
5021
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
97,988✔
5022
  pStmt->ignoreNotExists = ignoreNotExists;
97,988✔
5023
  return (SNode*)pStmt;
97,988✔
UNCOV
5024
_err:
×
UNCOV
5025
  return NULL;
×
5026
}
5027

5028
static bool checkRoleName(SAstCreateContext* pCxt, SToken* pName, bool checkSysName) {
1,803,672✔
5029
  if (NULL == pName) {
1,803,672✔
UNCOV
5030
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5031
  } else {
5032
    if (pName->n >= TSDB_ROLE_LEN) {
1,803,672✔
UNCOV
5033
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
5034
    }
5035
  }
5036
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
1,803,672✔
5037
    trimEscape(pCxt, pName, true);
1,803,672✔
5038
  }
5039
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
1,803,672✔
5040
    if (checkSysName && taosStrncasecmp(pName->z, "sys", 3) == 0) {  // system reserved role name prefix
1,803,672✔
5041
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
792✔
5042
                                              "Cannot create/drop/alter roles with reserved prefix 'sys'");
5043
    }
5044
  }
5045
  return TSDB_CODE_SUCCESS == pCxt->errCode;
1,803,672✔
5046
}
5047

5048

5049
STokenOptions* createDefaultTokenOptions(SAstCreateContext* pCxt) {
53,140✔
5050
  STokenOptions* pOptions = NULL;
53,140✔
5051
  int32_t code = nodesMakeNode(QUERY_NODE_TOKEN_OPTIONS, (SNode**)&pOptions);
53,140✔
5052
  if (pOptions == NULL) {
53,140✔
UNCOV
5053
    pCxt->errCode = code;
×
UNCOV
5054
    return NULL;
×
5055
  }
5056

5057
  pOptions->enable = 1;
53,140✔
5058
  pOptions->ttl = 0;
53,140✔
5059
  return pOptions;
53,140✔
5060
}
5061

5062

5063

5064
STokenOptions* mergeTokenOptions(SAstCreateContext* pCxt, STokenOptions* a, STokenOptions* b) {
16,200✔
5065
  if (a == NULL && b == NULL) {
16,200✔
5066
      return createDefaultTokenOptions(pCxt);
13,212✔
5067
  }
5068
  if (b == NULL) {
2,988✔
5069
    return a;
×
5070
  }
5071
  if (a == NULL) {
2,988✔
UNCOV
5072
    return b;
×
5073
  }
5074

5075
  if (b->hasEnable) {
2,988✔
UNCOV
5076
    if (a->hasEnable) {
×
UNCOV
5077
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE");
×
5078
    } else {
UNCOV
5079
      a->hasEnable = true;
×
UNCOV
5080
      a->enable = b->enable;
×
5081
    }
5082
  }
5083

5084
  if (b->hasTtl) {
2,988✔
5085
    if (a->hasTtl) {
1,328✔
UNCOV
5086
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TTL");
×
5087
    } else {
5088
      a->hasTtl = true;
1,328✔
5089
      a->ttl = b->ttl;
1,328✔
5090
    }
5091
  }
5092

5093
  if (b->hasProvider) {
2,988✔
5094
    if (a->hasProvider) {
996✔
5095
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PROVIDER");
×
5096
    } else {
5097
      a->hasProvider = true;
996✔
5098
      tstrncpy(a->provider, b->provider, sizeof(a->provider));
996✔
5099
    }
5100
  }
5101

5102
  if (b->hasExtraInfo) {
2,988✔
5103
    if (a->hasExtraInfo) {
664✔
UNCOV
5104
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "EXTRA_INFO");
×
5105
    } else {
5106
      a->hasExtraInfo = true;
664✔
5107
      tstrncpy(a->extraInfo, b->extraInfo, sizeof(a->extraInfo));
664✔
5108
    }
5109
  }
5110
  nodesDestroyNode((SNode*)b);
2,988✔
5111
  return a;
2,988✔
5112
}
5113

5114

5115

5116
void setTokenOptionsProvider(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pProvider) {
1,992✔
5117
  pTokenOptions->hasProvider = true;
1,992✔
5118

5119
  if (pProvider->n >= sizeof(pTokenOptions->provider) * 2) {
1,992✔
5120
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
×
UNCOV
5121
    return;
×
5122
  }
5123

5124
  char buf[sizeof(pTokenOptions->provider) * 2 + 1];
1,992✔
5125
  memcpy(buf, pProvider->z, pProvider->n);
1,992✔
5126
  buf[pProvider->n] = 0;
1,992✔
5127
  (void)strdequote(buf);
1,992✔
5128
  size_t len = strtrim(buf);
1,992✔
5129

5130
  if (len >= sizeof(pTokenOptions->provider)) {
1,992✔
5131
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
664✔
5132
  } else {
5133
    tstrncpy(pTokenOptions->provider, buf, sizeof(pTokenOptions->provider));
1,328✔
5134
  }
5135
}
5136

5137

5138

5139
void setTokenOptionsExtraInfo(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pExtraInfo) {
1,992✔
5140
  pTokenOptions->hasExtraInfo = true;
1,992✔
5141

5142
  if (pExtraInfo->n >= sizeof(pTokenOptions->extraInfo) * 2) {
1,992✔
UNCOV
5143
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
×
UNCOV
5144
    return;
×
5145
  }
5146

5147
  char buf[sizeof(pTokenOptions->extraInfo) * 2 + 1];
1,992✔
5148
  memcpy(buf, pExtraInfo->z, pExtraInfo->n);
1,992✔
5149
  buf[pExtraInfo->n] = 0;
1,992✔
5150
  (void)strdequote(buf);
1,992✔
5151
  size_t len = strtrim(buf);
1,992✔
5152

5153
  if (len >= sizeof(pTokenOptions->extraInfo)) {
1,992✔
5154
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
664✔
5155
  } else {
5156
    tstrncpy(pTokenOptions->extraInfo, buf, sizeof(pTokenOptions->extraInfo));
1,328✔
5157
  }
5158
}
5159

5160

5161

5162
static bool isValidTokenOptions(SAstCreateContext* pCxt, const STokenOptions* opts) {
8,896✔
5163
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
8,896✔
5164
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
664✔
5165
    return false;
664✔
5166
  }
5167

5168
  if (opts->hasTtl && (opts->ttl < 0)) {
8,232✔
UNCOV
5169
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TTL");
×
UNCOV
5170
    return false;
×
5171
  }
5172

5173
  return true;
8,232✔
5174
}
5175

5176

5177

5178
static bool checkTokenName(SAstCreateContext* pCxt, SToken* pTokenName) {
54,400✔
5179
  if (NULL == pTokenName) {
54,400✔
UNCOV
5180
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5181
  } else {
5182
    if (pTokenName->n >= TSDB_TOKEN_NAME_LEN) {
54,400✔
5183
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOKEN_NAME", TSDB_TOKEN_NAME_LEN);
664✔
5184
    }
5185
  }
5186
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
54,400✔
5187
    trimEscape(pCxt, pTokenName, true);
53,736✔
5188
  }
5189
  return TSDB_CODE_SUCCESS == pCxt->errCode;
54,400✔
5190
}
5191

5192
SNode* createCreateRoleStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pName) {
4,006✔
5193
  CHECK_PARSER_STATUS(pCxt);
4,006✔
5194
  CHECK_NAME(checkRoleName(pCxt, pName, true));
4,006✔
5195
  SCreateRoleStmt* pStmt = NULL;
4,006✔
5196
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ROLE_STMT, (SNode**)&pStmt);
4,006✔
5197
  CHECK_MAKE_NODE(pStmt);
4,006✔
5198
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
4,006✔
5199
  pStmt->ignoreExists = ignoreExists;
4,006✔
5200
  return (SNode*)pStmt;
4,006✔
UNCOV
5201
_err:
×
UNCOV
5202
  return NULL;
×
5203
}
5204

5205
SNode* createDropRoleStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pName) {
3,696✔
5206
  CHECK_PARSER_STATUS(pCxt);
3,696✔
5207
  CHECK_NAME(checkRoleName(pCxt, pName, true));
3,696✔
5208
  SDropRoleStmt* pStmt = NULL;
2,904✔
5209
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ROLE_STMT, (SNode**)&pStmt);
2,904✔
5210
  CHECK_MAKE_NODE(pStmt);
2,904✔
5211
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
2,904✔
5212
  pStmt->ignoreNotExists = ignoreNotExists;
2,904✔
5213
  return (SNode*)pStmt;
2,904✔
5214
_err:
792✔
5215
  return NULL;
792✔
5216
}
5217

5218
/**
5219
 * used by user and role
5220
 */
5221
SNode* createAlterRoleStmt(SAstCreateContext* pCxt, SToken* pName, int8_t alterType, void* pAlterInfo) {
792✔
5222
  SAlterRoleStmt* pStmt = NULL;
792✔
5223
  CHECK_PARSER_STATUS(pCxt);
792✔
5224
  CHECK_NAME(checkUserName(pCxt, pName));
792✔
5225
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ROLE_STMT, (SNode**)&pStmt);
792✔
5226
  CHECK_MAKE_NODE(pStmt);
792✔
5227
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
792✔
5228
  pStmt->alterType = alterType;
792✔
5229
  switch (alterType) {
792✔
5230
    case TSDB_ALTER_ROLE_LOCK: {
792✔
5231
      SToken* pVal = pAlterInfo;
792✔
5232
      pStmt->lock = taosStr2Int8(pVal->z, NULL, 10);
792✔
5233
      break;
792✔
5234
    }
UNCOV
5235
    default:
×
UNCOV
5236
      break;
×
5237
  }
5238
  return (SNode*)pStmt;
792✔
UNCOV
5239
_err:
×
UNCOV
5240
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
5241
  return NULL;
×
5242
}
5243

5244

5245
SNode* createCreateTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, SToken* pUserName, STokenOptions* opts, bool ignoreExists) {
44,576✔
5246
  SCreateTokenStmt* pStmt = NULL;
44,576✔
5247

5248
  CHECK_PARSER_STATUS(pCxt);
44,576✔
5249
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
43,912✔
5250
  CHECK_NAME(checkUserName(pCxt, pUserName));
43,248✔
5251

5252
  if (opts == NULL) {
43,248✔
5253
    opts = createDefaultTokenOptions(pCxt);
39,928✔
5254
  } else if (!isValidTokenOptions(pCxt, opts)) {
3,320✔
5255
    goto _err;
332✔
5256
  }
5257

5258
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOKEN_STMT, (SNode**)&pStmt);
42,916✔
5259
  CHECK_MAKE_NODE(pStmt);
42,916✔
5260

5261
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
42,916✔
5262
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
42,916✔
5263
  pStmt->enable = opts->enable;
42,916✔
5264
  pStmt->ignoreExists = ignoreExists;
42,916✔
5265
  pStmt->ttl = opts->ttl;
42,916✔
5266
  tstrncpy(pStmt->provider, opts->provider, sizeof(pStmt->provider));
42,916✔
5267
  tstrncpy(pStmt->extraInfo, opts->extraInfo, sizeof(pStmt->extraInfo));
42,916✔
5268
  nodesDestroyNode((SNode*)opts);
42,916✔
5269
  return (SNode*)pStmt;
42,916✔
5270

5271
_err:
1,660✔
5272
  nodesDestroyNode((SNode*)pStmt);
1,660✔
5273
  nodesDestroyNode((SNode*)opts);
1,660✔
5274
  return NULL;
1,660✔
5275
}
5276

5277

5278

5279
SNode* createAlterTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, STokenOptions* opts) {
6,240✔
5280
  SAlterTokenStmt* pStmt = NULL;
6,240✔
5281

5282
  CHECK_PARSER_STATUS(pCxt);
6,240✔
5283
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
5,576✔
5284
  if (!isValidTokenOptions(pCxt, opts)) {
5,576✔
5285
    goto _err;
332✔
5286
  }
5287

5288
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TOKEN_STMT, (SNode**)&pStmt);
5,244✔
5289
  CHECK_MAKE_NODE(pStmt);
5,244✔
5290

5291
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
5,244✔
5292
  pStmt->pTokenOptions = opts;
5,244✔
5293
  return (SNode*)pStmt;
5,244✔
5294

5295
_err:
996✔
5296
  nodesDestroyNode((SNode*)pStmt);
996✔
5297
  nodesDestroyNode((SNode*)opts);
996✔
5298
  return NULL;
996✔
5299
}
5300

5301

5302

5303
SNode* createDropTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, bool ignoreNotExists) {
4,912✔
5304
  SDropTokenStmt* pStmt = NULL;
4,912✔
5305

5306
  CHECK_PARSER_STATUS(pCxt);
4,912✔
5307
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
4,912✔
5308

5309
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOKEN_STMT, (SNode**)&pStmt);
4,912✔
5310
  CHECK_MAKE_NODE(pStmt);
4,912✔
5311

5312
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
4,912✔
5313
  pStmt->ignoreNotExists = ignoreNotExists;
4,912✔
5314
  return (SNode*)pStmt;
4,912✔
5315

UNCOV
5316
_err:
×
UNCOV
5317
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
5318
  return NULL;
×
5319
}
5320

5321
SNode* createCreateTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
35,530✔
5322
  SCreateTotpSecretStmt* pStmt = NULL;
35,530✔
5323

5324
  CHECK_PARSER_STATUS(pCxt);
35,530✔
5325
  CHECK_NAME(checkUserName(pCxt, pUserName));
35,530✔
5326

5327
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOTP_SECRET_STMT, (SNode**)&pStmt);
35,218✔
5328
  CHECK_MAKE_NODE(pStmt);
35,218✔
5329

5330
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
35,218✔
5331
  return (SNode*)pStmt;
35,218✔
5332

5333
_err:
312✔
5334
  nodesDestroyNode((SNode*)pStmt);
312✔
5335
  return NULL;
312✔
5336
}
5337

5338

5339
SNode* createDropTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
16,224✔
5340
  SDropTotpSecretStmt* pStmt = NULL;
16,224✔
5341

5342
  CHECK_PARSER_STATUS(pCxt);
16,224✔
5343
  CHECK_NAME(checkUserName(pCxt, pUserName));
16,224✔
5344

5345
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOTP_SECRET_STMT, (SNode**)&pStmt);
13,104✔
5346
  CHECK_MAKE_NODE(pStmt);
13,104✔
5347

5348
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
13,104✔
5349
  return (SNode*)pStmt;
13,104✔
5350

5351
_err:
3,120✔
5352
  nodesDestroyNode((SNode*)pStmt);
3,120✔
5353
  return NULL;
3,120✔
5354
}
5355

5356

UNCOV
5357
SNode* createDropEncryptAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId) {
×
UNCOV
5358
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
5359
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
UNCOV
5360
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
UNCOV
5361
    goto _err;
×
5362
  }
UNCOV
5363
  SDropEncryptAlgrStmt* pStmt = NULL;
×
UNCOV
5364
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ENCRYPT_ALGR_STMT, (SNode**)&pStmt);
×
UNCOV
5365
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
5366
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
UNCOV
5367
  return (SNode*)pStmt;
×
UNCOV
5368
_err:
×
UNCOV
5369
  return NULL;
×
5370
}
5371

5372
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
320,294✔
5373
  CHECK_PARSER_STATUS(pCxt);
320,294✔
5374
  SCreateDnodeStmt* pStmt = NULL;
320,294✔
5375
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
320,294✔
5376
  CHECK_MAKE_NODE(pStmt);
320,294✔
5377
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
320,294✔
UNCOV
5378
    nodesDestroyNode((SNode*)pStmt);
×
5379
    return NULL;
×
5380
  }
5381
  return (SNode*)pStmt;
320,294✔
5382
_err:
×
5383
  return NULL;
×
5384
}
5385

5386
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
28,532✔
5387
  CHECK_PARSER_STATUS(pCxt);
28,532✔
5388
  SDropDnodeStmt* pStmt = NULL;
28,532✔
5389
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
28,532✔
5390
  CHECK_MAKE_NODE(pStmt);
28,532✔
5391
  if (TK_NK_INTEGER == pDnode->type) {
28,532✔
5392
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
23,876✔
5393
  } else {
5394
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
4,656✔
5395
      nodesDestroyNode((SNode*)pStmt);
×
UNCOV
5396
      return NULL;
×
5397
    }
5398
  }
5399
  pStmt->force = force;
28,532✔
5400
  pStmt->unsafe = unsafe;
28,532✔
5401
  return (SNode*)pStmt;
28,532✔
UNCOV
5402
_err:
×
UNCOV
5403
  return NULL;
×
5404
}
5405

5406
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
224,176✔
5407
                            const SToken* pValue) {
5408
  CHECK_PARSER_STATUS(pCxt);
224,176✔
5409
  SAlterDnodeStmt* pStmt = NULL;
224,176✔
5410
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
224,176✔
5411
  CHECK_MAKE_NODE(pStmt);
224,176✔
5412
  if (NULL != pDnode) {
224,176✔
5413
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
132,248✔
5414
  } else {
5415
    pStmt->dnodeId = -1;
91,928✔
5416
  }
5417
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
224,176✔
5418
  if (NULL != pValue) {
224,176✔
5419
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
103,734✔
5420
  }
5421
  return (SNode*)pStmt;
224,176✔
UNCOV
5422
_err:
×
UNCOV
5423
  return NULL;
×
5424
}
5425

UNCOV
5426
SNode* createCreateAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId, const SToken* name, const SToken* desc,
×
5427
                            const SToken* type, const SToken* osslAlgrName) {
UNCOV
5428
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
5429
  SCreateEncryptAlgrStmt* pStmt = NULL;
×
UNCOV
5430
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ENCRYPT_ALGORITHMS_STMT, (SNode**)&pStmt);
×
UNCOV
5431
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
5432
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
UNCOV
5433
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
UNCOV
5434
    goto _err;
×
5435
  }
UNCOV
5436
  if (name->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
UNCOV
5437
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_NAME_TOO_LONG);
×
5438
    goto _err;
×
5439
  }
UNCOV
5440
  if (desc->n >= TSDB_ENCRYPT_ALGR_DESC_LEN) {
×
UNCOV
5441
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_DESC_TOO_LONG);
×
5442
    goto _err;
×
5443
  }
5444
  if (type->n >= TSDB_ENCRYPT_ALGR_TYPE_LEN) {
×
5445
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_TYPE_TOO_LONG);
×
5446
    goto _err;
×
5447
  }
5448
  if (osslAlgrName->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5449
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_OSSL_NAME_TOO_LONG);
×
5450
    goto _err;
×
5451
  }
5452
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
5453
  (void)trimString(name->z, name->n, pStmt->name, sizeof(pStmt->name));
×
5454
  (void)trimString(desc->z, desc->n, pStmt->desc, sizeof(pStmt->desc));
×
UNCOV
5455
  (void)trimString(type->z, type->n, pStmt->algrType, sizeof(pStmt->algrType));
×
5456
  (void)trimString(osslAlgrName->z, osslAlgrName->n, pStmt->osslAlgrName, sizeof(pStmt->osslAlgrName));
×
5457
  return (SNode*)pStmt;
×
5458
_err:
×
UNCOV
5459
  return NULL;
×
5460
}
5461

5462
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
×
UNCOV
5463
  CHECK_PARSER_STATUS(pCxt);
×
5464
  SCreateAnodeStmt* pStmt = NULL;
×
5465
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
×
5466
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
5467
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
×
5468
  return (SNode*)pStmt;
×
5469
_err:
×
5470
  return NULL;
×
5471
}
5472

5473
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
×
5474
  CHECK_PARSER_STATUS(pCxt);
×
5475
  SUpdateAnodeStmt* pStmt = NULL;
×
UNCOV
5476
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
×
UNCOV
5477
  CHECK_MAKE_NODE(pStmt);
×
5478
  if (NULL != pAnode) {
×
5479
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5480
  } else {
5481
    pStmt->anodeId = -1;
×
5482
  }
5483
  return (SNode*)pStmt;
×
5484
_err:
×
5485
  return NULL;
×
5486
}
5487

UNCOV
5488
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
5489
  CHECK_PARSER_STATUS(pCxt);
×
5490
  SUpdateAnodeStmt* pStmt = NULL;
×
5491
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
5492
  CHECK_MAKE_NODE(pStmt);
×
5493
  if (NULL != pAnode) {
×
5494
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5495
  } else {
UNCOV
5496
    pStmt->anodeId = -1;
×
5497
  }
UNCOV
5498
  return (SNode*)pStmt;
×
5499
_err:
×
5500
  return NULL;
×
5501
}
5502

5503
SNode* createCreateBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId, SNode* pOptions) {
50,408✔
5504
  CHECK_PARSER_STATUS(pCxt);
50,408✔
5505
  SCreateBnodeStmt* pStmt = NULL;
50,408✔
5506
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_BNODE_STMT, (SNode**)&pStmt);
50,408✔
5507
  CHECK_MAKE_NODE(pStmt);
50,408✔
5508
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
50,408✔
5509

5510
  pStmt->pOptions = (SBnodeOptions*)pOptions;
50,408✔
5511

5512
  return (SNode*)pStmt;
50,408✔
UNCOV
5513
_err:
×
5514
  return NULL;
×
5515
}
5516

5517
SNode* createDropBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId) {
59,732✔
5518
  CHECK_PARSER_STATUS(pCxt);
59,732✔
5519
  SUpdateBnodeStmt* pStmt = NULL;
59,732✔
5520
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_BNODE_STMT, (SNode**)&pStmt);
59,732✔
5521
  CHECK_MAKE_NODE(pStmt);
59,732✔
5522
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
59,732✔
5523

5524
  return (SNode*)pStmt;
59,732✔
UNCOV
5525
_err:
×
UNCOV
5526
  return NULL;
×
5527
}
5528

5529
SNode* createDefaultBnodeOptions(SAstCreateContext* pCxt) {
50,408✔
5530
  CHECK_PARSER_STATUS(pCxt);
50,408✔
5531
  SBnodeOptions* pOptions = NULL;
50,408✔
5532
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BNODE_OPTIONS, (SNode**)&pOptions);
50,408✔
5533
  CHECK_MAKE_NODE(pOptions);
50,408✔
5534

5535
  tstrncpy(pOptions->protoStr, TSDB_BNODE_OPT_PROTO_DFT_STR, TSDB_BNODE_OPT_PROTO_STR_LEN);
50,408✔
5536
  pOptions->proto = TSDB_BNODE_OPT_PROTO_DEFAULT;
50,408✔
5537

5538
  return (SNode*)pOptions;
50,408✔
UNCOV
5539
_err:
×
UNCOV
5540
  return NULL;
×
5541
}
5542

UNCOV
5543
static SNode* setBnodeOptionImpl(SAstCreateContext* pCxt, SNode* pBodeOptions, EBnodeOptionType type, void* pVal,
×
5544
                                 bool alter) {
UNCOV
5545
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
5546
  SBnodeOptions* pOptions = (SBnodeOptions*)pBodeOptions;
×
UNCOV
5547
  switch (type) {
×
UNCOV
5548
    case BNODE_OPTION_PROTOCOL:
×
UNCOV
5549
      COPY_STRING_FORM_STR_TOKEN(pOptions->protoStr, (SToken*)pVal);
×
UNCOV
5550
      break;
×
UNCOV
5551
    default:
×
UNCOV
5552
      break;
×
5553
  }
5554

5555
  return pBodeOptions;
×
5556
_err:
×
UNCOV
5557
  nodesDestroyNode(pBodeOptions);
×
UNCOV
5558
  return NULL;
×
5559
}
5560

5561
SNode* setBnodeOption(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pOption, void* pVal) {
×
5562
  if (0 == strncasecmp(pOption->z, "protocol", 8)) {
×
5563
    return setBnodeOptionImpl(pCxt, pOptions, BNODE_OPTION_PROTOCOL, pVal, false);
×
5564
  } else {
5565
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5566
    return pOptions;
×
5567
  }
5568
}
5569

5570
SNode* createCreateXnodeWithTokenStmt(SAstCreateContext* pCxt, const SToken* pUrl, SToken* pToken) {
88✔
5571
  CHECK_PARSER_STATUS(pCxt);
88✔
5572
  SCreateXnodeStmt* pStmt = NULL;
88✔
5573
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
88✔
5574
  CHECK_MAKE_NODE(pStmt);
88✔
5575

5576
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
88✔
5577

5578
  if (pToken != NULL) {
88✔
5579
    if (pToken->n <= 2) {
88✔
UNCOV
5580
      pCxt->errCode =
×
5581
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode token should not be empty");
×
5582
      goto _err;
×
5583
    }
5584
    if (pToken->n > TSDB_TOKEN_LEN + 2) {
88✔
UNCOV
5585
      pCxt->errCode =
×
UNCOV
5586
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode token length is illegal");
×
UNCOV
5587
      goto _err;
×
5588
    }
5589
    strncpy(pStmt->token, pToken->z + 1, pToken->n - 2);
88✔
5590
    pStmt->token[sizeof(pStmt->token) - 1] = '\0';
88✔
5591
  }
5592
  return (SNode*)pStmt;
88✔
UNCOV
5593
_err:
×
UNCOV
5594
  return NULL;
×
5595
}
5596

5597
SNode* createCreateXnodeWithUserPassStmt(SAstCreateContext* pCxt, const SToken* pUrl, SToken* pUser,
3,072✔
5598
                                         const SToken* pPass) {
5599
  CHECK_PARSER_STATUS(pCxt);
3,072✔
5600
  SCreateXnodeStmt* pStmt = NULL;
3,072✔
5601
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
3,072✔
5602
  CHECK_MAKE_NODE(pStmt);
3,072✔
5603

5604
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
3,072✔
5605

5606
  if (pUser != NULL) {
3,072✔
5607
    CHECK_NAME(checkUserName(pCxt, pUser));
3,072✔
5608
    COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUser);
3,072✔
5609
  }
5610
  if (pPass != NULL) {
3,072✔
5611
    if (pPass->n <= 2) {
3,072✔
UNCOV
5612
      pCxt->errCode =
×
UNCOV
5613
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode password should not be empty");
×
UNCOV
5614
      goto _err;
×
5615
    }
5616
    strncpy(pStmt->pass, pPass->z + 1, pPass->n - 2);
3,072✔
5617
    pStmt->pass[sizeof(pStmt->pass) - 1] = '\0';
3,072✔
5618
  }
5619
  return (SNode*)pStmt;
3,072✔
UNCOV
5620
_err:
×
UNCOV
5621
  return NULL;
×
5622
}
5623
SNode* createCreateXnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
4,008✔
5624
  CHECK_PARSER_STATUS(pCxt);
4,008✔
5625
  SCreateXnodeStmt* pStmt = NULL;
4,008✔
5626
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
4,008✔
5627
  CHECK_MAKE_NODE(pStmt);
4,008✔
5628
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
4,008✔
5629
  return (SNode*)pStmt;
4,008✔
5630
_err:
×
UNCOV
5631
  return NULL;
×
5632
}
5633

5634
SNode* createDropXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode, bool force) {
12,092✔
5635
  if (NULL == pXnode) {
12,092✔
5636
    pCxt->errCode =
×
5637
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
UNCOV
5638
    goto _err;
×
5639
  }
5640
  CHECK_PARSER_STATUS(pCxt);
12,092✔
5641
  SDropXnodeStmt* pStmt = NULL;
12,092✔
5642
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_STMT, (SNode**)&pStmt);
12,092✔
5643
  CHECK_MAKE_NODE(pStmt);
12,092✔
5644

5645
  pStmt->force = force;
12,092✔
5646
  if (pXnode->type == TK_NK_STRING) {
12,092✔
5647
    if (pXnode->n <= 2) {
7,112✔
UNCOV
5648
      pCxt->errCode =
×
UNCOV
5649
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode url should not be all be NULL");
×
UNCOV
5650
        goto _err;
×
5651
    }
5652
    COPY_STRING_FORM_STR_TOKEN(pStmt->url, pXnode);
7,112✔
5653
  } else if(pXnode->type == TK_NK_INTEGER) {
4,980✔
5654
    pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
4,980✔
5655
  } else {
UNCOV
5656
    pCxt->errCode =
×
UNCOV
5657
      generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id or url should not be all be NULL");
×
5658
  }
5659

5660
  return (SNode*)pStmt;
12,092✔
UNCOV
5661
_err:
×
UNCOV
5662
  return NULL;
×
5663
}
5664

5665
SNode* createDrainXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode) {
2,516✔
5666
  CHECK_PARSER_STATUS(pCxt);
2,516✔
5667
  if (NULL == pXnode) {
2,516✔
UNCOV
5668
    pCxt->errCode =
×
UNCOV
5669
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
UNCOV
5670
    goto _err;
×
5671
  }
5672
  if (pXnode->type != TK_NK_INTEGER) {
2,516✔
5673
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be an integer");
×
UNCOV
5674
    goto _err;
×
5675
  }
5676

5677
  SDrainXnodeStmt* pStmt = NULL;
2,516✔
5678
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DRAIN_XNODE_STMT, (SNode**)&pStmt);
2,516✔
5679
  CHECK_MAKE_NODE(pStmt);
2,516✔
5680
  pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
2,516✔
5681
  if (pStmt->xnodeId <= 0) {
2,516✔
UNCOV
5682
    pCxt->errCode =
×
UNCOV
5683
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be greater than 0");
×
5684
    goto _err;
×
5685
  }
5686

5687
  return (SNode*)pStmt;
2,516✔
UNCOV
5688
_err:
×
5689
  return NULL;
×
5690
}
5691

5692
SNode* createAlterXnodeStmt(SAstCreateContext* pCxt, const SToken* pToken, const SToken* pUser, const SToken* pPass) {
528✔
5693
  CHECK_PARSER_STATUS(pCxt);
528✔
5694
  SAlterXnodeStmt* pStmt = NULL;
528✔
5695
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_STMT, (SNode**)&pStmt);
528✔
5696
  CHECK_MAKE_NODE(pStmt);
528✔
5697
  if (pToken != NULL) {
528✔
5698
    pStmt->token = xCreateCowStr(pToken->n - 2, pToken->z + 1, true);
176✔
5699
  }
5700
  if (pUser != NULL && pPass != NULL) {
528✔
5701
    if (pUser->n <= 2) {
352✔
UNCOV
5702
      pCxt->errCode =
×
UNCOV
5703
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode user should not be NULL or empty");
×
5704
      goto _err;
×
5705
    }
5706
    if (pPass->n <= 2) {
352✔
UNCOV
5707
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5708
                                              "xnode password should not be NULL or empty");
UNCOV
5709
      goto _err;
×
5710
    }
5711
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
352✔
5712
    COPY_COW_STR_FROM_ID_TOKEN(pStmt->user, pUser);
352✔
5713
    pStmt->pass = xCreateCowStr(pPass->n - 2, pPass->z + 1, true);
352✔
5714
  }
5715

5716
  return (SNode*)pStmt;
528✔
UNCOV
5717
_err:
×
5718
  nodesDestroyNode((SNode*)pStmt);
×
5719
  return NULL;
×
5720
}
5721

5722
EXnodeResourceType setXnodeResourceType(SAstCreateContext* pCxt, const SToken* pResourceId) {
96,916✔
5723
  CHECK_PARSER_STATUS(pCxt);
96,916✔
5724
  const size_t TASK_LEN = 4;
96,916✔
5725
  const size_t TASKS_LEN = 5;
96,916✔
5726
  const size_t AGENT_LEN = 5;
96,916✔
5727
  const size_t AGENTS_LEN = 6;
96,916✔
5728
  const size_t JOB_LEN = 3;
96,916✔
5729
  const size_t JOBS_LEN = 4;
96,916✔
5730

5731
  if (pResourceId->z[0] == '`') {
96,916✔
UNCOV
5732
    if (strncmp(pResourceId->z + 1, "task", TASK_LEN) == 0 || strncmp(pResourceId->z + 1, "tasks", TASKS_LEN) == 0) {
×
5733
      return XNODE_TASK;
×
5734
    }
5735
    if (strncmp(pResourceId->z + 1, "agent", AGENT_LEN) == 0 ||
×
UNCOV
5736
        strncmp(pResourceId->z + 1, "agents", AGENTS_LEN) == 0) {
×
UNCOV
5737
      return XNODE_AGENT;
×
5738
    }
UNCOV
5739
    if (strncmp(pResourceId->z + 1, "job", JOB_LEN) == 0 || strncmp(pResourceId->z + 1, "jobs", JOBS_LEN) == 0) {
×
UNCOV
5740
      return XNODE_JOB;
×
5741
    }
5742

UNCOV
5743
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5744
                                            "Invalid xnode resource type (task/agent) at: %s", pResourceId->z);
×
UNCOV
5745
    goto _err;
×
5746
  }
5747
  if (strncmp(pResourceId->z, "task", TASK_LEN) == 0 || strncmp(pResourceId->z, "tasks", TASKS_LEN) == 0) {
96,916✔
5748
    return XNODE_TASK;
60,700✔
5749
  }
5750
  if (strncmp(pResourceId->z, "agent", AGENT_LEN) == 0 || strncmp(pResourceId->z, "agents", AGENTS_LEN) == 0) {
36,216✔
5751
    return XNODE_AGENT;
7,372✔
5752
  }
5753
  if (strncmp(pResourceId->z, "job", JOB_LEN) == 0 || strncmp(pResourceId->z, "jobs", JOBS_LEN) == 0) {
28,844✔
5754
    return XNODE_JOB;
28,844✔
5755
  }
5756
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5757
                                          "Invalid xnode resource type (task/agent/job) at: %s", pResourceId->z);
×
UNCOV
5758
  goto _err;
×
5759

5760
_err:
×
5761
  return XNODE_UNKNOWN;
×
5762
}
5763
SNode* createXnodeSourceAsDsn(SAstCreateContext* pCxt, const SToken* pToken) {
22,880✔
5764
  SXTaskSource* pSource = NULL;
22,880✔
5765
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
22,880✔
5766
  CHECK_MAKE_NODE(pSource);
22,880✔
5767
  if (pToken == NULL || pToken->n <= 0) {
22,880✔
UNCOV
5768
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5769
                                            "xnode source dsn should not be NULL or empty");
UNCOV
5770
    goto _err;
×
5771
  }
5772
  if (pToken->n > TSDB_XNODE_TASK_SOURCE_LEN) {
22,880✔
5773
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5774
                                            "Invalid xnode source dsn length: %d, max length: %d", pToken->n,
×
5775
                                            TSDB_XNODE_TASK_SOURCE_LEN);
5776
    goto _err;
×
5777
  }
5778
  pSource->source.type = XNODE_TASK_SOURCE_DSN;
22,880✔
5779
  COPY_COW_STR_FROM_STR_TOKEN(pSource->source.cstr, pToken);
22,880✔
5780
  return (SNode*)pSource;
22,880✔
UNCOV
5781
_err:
×
UNCOV
5782
  return NULL;
×
5783
}
5784
SNode* createXnodeSourceAsDatabase(SAstCreateContext* pCxt, const SToken* pToken) {
880✔
5785
  SXTaskSource* pSource = NULL;
880✔
5786
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
880✔
5787
  CHECK_MAKE_NODE(pSource);
880✔
5788
  if (pToken == NULL || pToken->n <= 0) {
880✔
5789
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5790
                                            "xnode source database should not be NULL or empty");
UNCOV
5791
    goto _err;
×
5792
  }
5793
  if (pToken->n > TSDB_XNODE_TASK_SOURCE_LEN) {
880✔
UNCOV
5794
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5795
                                            "Invalid xnode source database length: %d, max length: %d", pToken->n,
×
5796
                                            TSDB_XNODE_TASK_SOURCE_LEN);
5797
    goto _err;
×
5798
  }
5799
  pSource->source.type = XNODE_TASK_SOURCE_DATABASE;
880✔
5800
  COPY_COW_STR_FROM_ID_TOKEN(pSource->source.cstr, pToken);
880✔
5801
  return (SNode*)pSource;
880✔
UNCOV
5802
_err:
×
UNCOV
5803
  return NULL;
×
5804
}
5805
SNode* createXnodeSourceAsTopic(SAstCreateContext* pCxt, const SToken* pToken) {
4,400✔
5806
  SXTaskSource* pSource = NULL;
4,400✔
5807
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
4,400✔
5808
  CHECK_MAKE_NODE(pSource);
4,400✔
5809
  if (pToken == NULL || pToken->n <= 0) {
4,400✔
5810
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5811
                                            "xnode source dsn should not be NULL or empty");
UNCOV
5812
    goto _err;
×
5813
  }
5814
  if (pToken->n > TSDB_TOPIC_NAME_LEN) {
4,400✔
UNCOV
5815
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5816
                                            "Invalid xnode source topic length: %d, max length: %d", pToken->n,
×
5817
                                            TSDB_TOPIC_NAME_LEN);
5818
    goto _err;
×
5819
  }
5820
  pSource->source.type = XNODE_TASK_SOURCE_TOPIC;
4,400✔
5821
  COPY_COW_STR_FROM_STR_TOKEN(pSource->source.cstr, pToken);
4,400✔
5822
  return (SNode*)pSource;
4,400✔
UNCOV
5823
_err:
×
UNCOV
5824
  return NULL;
×
5825
}
5826
SNode* createXnodeSinkAsDsn(SAstCreateContext* pCxt, const SToken* pToken) {
11,440✔
5827
  SXTaskSink* pSink = NULL;
11,440✔
5828
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SINK_OPT, (SNode**)&pSink);
11,440✔
5829
  CHECK_MAKE_NODE(pSink);
11,440✔
5830
  if (pToken == NULL || pToken->n <= 0) {
11,440✔
5831
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5832
                                            "xnode sink dsn should not be NULL or empty");
UNCOV
5833
    goto _err;
×
5834
  }
5835
  if (pToken->n > TSDB_XNODE_TASK_SINK_LEN) {
11,440✔
UNCOV
5836
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5837
                                            "Invalid xnode sink dsn length: %d, max length: %d", pToken->n,
×
5838
                                            TSDB_XNODE_TASK_SINK_LEN);
5839
    goto _err;
×
5840
  }
5841
  pSink->sink.type = XNODE_TASK_SINK_DSN;
11,440✔
5842
  COPY_COW_STR_FROM_STR_TOKEN(pSink->sink.cstr, pToken);
11,440✔
5843
  return (SNode*)pSink;
11,440✔
UNCOV
5844
_err:
×
UNCOV
5845
  return NULL;
×
5846
}
5847
SNode* createXnodeSinkAsDatabase(SAstCreateContext* pCxt, const SToken* pToken) {
17,600✔
5848
  SXTaskSink* pSink = NULL;
17,600✔
5849
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SINK_OPT, (SNode**)&pSink);
17,600✔
5850
  CHECK_MAKE_NODE(pSink);
17,600✔
5851
  if (pToken == NULL || pToken->n <= 0) {
17,600✔
5852
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5853
                                            "Xnode sink database should not be NULL or empty");
UNCOV
5854
    goto _err;
×
5855
  }
5856
  if (pToken->n > TSDB_XNODE_TASK_SINK_LEN) {
17,600✔
UNCOV
5857
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5858
                                            "Invalid xnode sink database length: %d, max length: %d", pToken->n,
×
5859
                                            TSDB_XNODE_TASK_SINK_LEN);
5860
    goto _err;
×
5861
  }
5862
  pSink->sink.type = XNODE_TASK_SINK_DATABASE;
17,600✔
5863
  if (pToken->type == TK_NK_STRING) {
17,600✔
UNCOV
5864
    COPY_COW_STR_FROM_STR_TOKEN(pSink->sink.cstr, pToken);
×
5865
  } else if (pToken->type == TK_NK_ID) {
17,600✔
5866
    COPY_COW_STR_FROM_ID_TOKEN(pSink->sink.cstr, pToken);
17,600✔
5867
  } else {
5868
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5869
                                            "Invalid xnode sink database type: %d", pToken->type);
×
5870
    goto _err;
×
5871
  }
5872

5873
  return (SNode*)pSink;
17,600✔
5874
_err:
×
UNCOV
5875
  return NULL;
×
5876
}
5877

5878
SNode* createXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pSource,
27,280✔
5879
                                          SNode* pSink, SNode* pNode) {
5880
  SNode* pStmt = NULL;
27,280✔
5881
  if (pResourceName == NULL) {
27,280✔
UNCOV
5882
    pCxt->errCode =
×
UNCOV
5883
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name should not be NULL");
×
5884
    goto _err;
×
5885
  }
5886
  if (pSource == NULL || pSink == NULL) {
27,280✔
UNCOV
5887
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5888
                                            "Xnode task source and sink should not be NULL");
UNCOV
5889
    goto _err;
×
5890
  }
5891
  if (nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT || nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT) {
27,280✔
UNCOV
5892
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5893
                                            "Xnode task source and sink should be valid nodes");
UNCOV
5894
    goto _err;
×
5895
  }
5896
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_TASK_STMT, (SNode**)&pStmt);
27,280✔
5897
  CHECK_MAKE_NODE(pStmt);
27,280✔
5898
  SCreateXnodeTaskStmt* pTaskStmt = (SCreateXnodeTaskStmt*)pStmt;
27,280✔
5899
  if (pResourceName->type == TK_NK_STRING) {
27,280✔
5900
    if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
27,280✔
5901
      pCxt->errCode =
880✔
5902
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
880✔
5903
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_TASK_NAME_LEN);
5904
      goto _err;
880✔
5905
    }
5906
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
26,400✔
UNCOV
5907
  } else if (pResourceName->type == TK_NK_ID) {
×
5908
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
×
5909
  } else {
5910
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode name type: %d",
×
UNCOV
5911
                                            pResourceName->type);
×
UNCOV
5912
    goto _err;
×
5913
  }
5914

5915
  if (pSource != NULL) {
26,400✔
5916
    SXTaskSource* source = (SXTaskSource*)(pSource);
26,400✔
5917
    pTaskStmt->source = source;
26,400✔
5918
  }
5919
  if (pSink != NULL) {
26,400✔
5920
    SXTaskSink* sink = (SXTaskSink*)(pSink);
26,400✔
5921
    pTaskStmt->sink = sink;
26,400✔
5922
  }
5923
  if (pNode != NULL) {
26,400✔
5924
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
26,400✔
5925
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
26,400✔
5926
      pTaskStmt->options = options;
26,400✔
5927
    }
5928
  }
5929
  return (SNode*)pTaskStmt;
26,400✔
5930
_err:
880✔
5931
  if (pStmt != NULL) {
880✔
5932
    nodesDestroyNode(pStmt);
880✔
5933
  }
5934
  if (pNode != NULL) {
880✔
5935
    nodesDestroyNode(pNode);
880✔
5936
  }
5937
  return NULL;
880✔
5938
}
5939

5940
SNode* createXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pOptions) {
1,672✔
5941
  SNode* pStmt = NULL;
1,672✔
5942
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_AGENT_STMT, (SNode**)&pStmt);
1,672✔
5943
  CHECK_MAKE_NODE(pStmt);
1,672✔
5944
  SCreateXnodeAgentStmt* pAgentStmt = (SCreateXnodeAgentStmt*)pStmt;
1,672✔
5945

5946
  if (pOptions != NULL) {
1,672✔
5947
    if (nodeType(pOptions) == QUERY_NODE_XNODE_TASK_OPTIONS) {
1,672✔
5948
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pOptions);
1,672✔
5949
      pAgentStmt->options = options;
1,672✔
5950
    }
5951
  }
5952

5953
  if (pResourceName->type == TK_NK_STRING && pResourceName->n > 2) {
1,672✔
5954
    if (pResourceName->n > TSDB_XNODE_AGENT_NAME_LEN + 2) {
1,672✔
UNCOV
5955
      pCxt->errCode =
×
UNCOV
5956
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5957
                                  "Xnode agent name should be less than %d characters", TSDB_XNODE_AGENT_NAME_LEN);
UNCOV
5958
      goto _err;
×
5959
    }
5960
    COPY_STRING_FORM_STR_TOKEN(pAgentStmt->name, pResourceName);
1,672✔
5961
  } else {
UNCOV
5962
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5963
                                            "Invalid xnode agent name type: %d", pResourceName->type);
×
UNCOV
5964
    goto _err;
×
5965
  }
5966

5967
  return (SNode*)pAgentStmt;
1,672✔
UNCOV
5968
_err:
×
UNCOV
5969
  if (pStmt != NULL) {
×
UNCOV
5970
    nodesDestroyNode(pStmt);
×
5971
  }
5972
  return NULL;
×
5973
}
5974

5975
SNode* createXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResourceName,
28,952✔
5976
                                  SNode* pSource, SNode* pSink, SNode* pOptions) {
5977
  CHECK_PARSER_STATUS(pCxt);
28,952✔
5978

5979
  switch (resourceType) {
28,952✔
5980
    case XNODE_TASK: {
27,280✔
5981
      SNode* rs = createXnodeTaskWithOptionsDirectly(pCxt, pResourceName, pSource, pSink, pOptions);
27,280✔
5982
      if (rs == NULL) {
27,280✔
5983
        goto _err;
880✔
5984
      }
5985
      return rs;
26,400✔
5986
    }
5987
    case XNODE_AGENT: {
1,672✔
5988
      SNode* rs = createXnodeAgentWithOptionsDirectly(pCxt, pResourceName, pOptions);
1,672✔
5989
      if (rs == NULL) {
1,672✔
UNCOV
5990
        goto _err;
×
5991
      }
5992
      return rs;
1,672✔
5993
    }
UNCOV
5994
    default:
×
UNCOV
5995
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5996
                                              "Invalid xnode resource type: %d", resourceType);
UNCOV
5997
      goto _err;
×
5998
  }
5999
_err:
880✔
6000
  nodesDestroyNode(pSource);
880✔
6001
  nodesDestroyNode(pSink);
880✔
6002
  nodesDestroyNode(pOptions);
880✔
6003
  return NULL;
880✔
6004
}
6005

6006
SNode* createStartXnodeTaskStmt(SAstCreateContext* pCxt, const EXnodeResourceType resourceType, SToken* pIdOrName) {
880✔
6007
  SNode* pStmt = NULL;
880✔
6008
  CHECK_PARSER_STATUS(pCxt);
880✔
6009
  if (resourceType != XNODE_TASK) {
880✔
6010
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6011
                                            "Invalid xnode resource type: %d", resourceType);
UNCOV
6012
    goto _err;
×
6013
  }
6014
  if (pIdOrName == NULL || (pIdOrName != NULL && pIdOrName->type != TK_NK_INTEGER && pIdOrName->type != TK_NK_STRING)) {
880✔
UNCOV
6015
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6016
                                            "Xnode task id or name should be an integer or string");
UNCOV
6017
    goto _err;
×
6018
  }
6019

6020
  pCxt->errCode = nodesMakeNode(QUERY_NODE_START_XNODE_TASK_STMT, (SNode**)&pStmt);
880✔
6021
  CHECK_MAKE_NODE(pStmt);
880✔
6022
  SStartXnodeTaskStmt* pTaskStmt = (SStartXnodeTaskStmt*)pStmt;
880✔
6023
  if (pIdOrName->type == TK_NK_INTEGER) {
880✔
UNCOV
6024
    pTaskStmt->tid = taosStr2Int32(pIdOrName->z, NULL, 10);
×
UNCOV
6025
    if (pTaskStmt->tid <= 0) {
×
6026
      pCxt->errCode =
×
UNCOV
6027
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Task id should be greater than 0");
×
6028
      goto _err;
×
6029
    }
6030
  } else {
6031
    if (pIdOrName->n > TSDB_XNODE_RESOURCE_NAME_LEN + 2) {
880✔
UNCOV
6032
      pCxt->errCode =
×
6033
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6034
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_RESOURCE_NAME_LEN);
UNCOV
6035
      goto _err;
×
6036
    }
6037
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
880✔
6038
    COPY_STRING_FORM_STR_TOKEN(buf, pIdOrName);
880✔
6039
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
880✔
6040
  }
6041

6042
  return (SNode*)pTaskStmt;
880✔
6043
_err:
×
6044
  if (pStmt != NULL) {
×
UNCOV
6045
    nodesDestroyNode(pStmt);
×
6046
  }
UNCOV
6047
  return NULL;
×
6048
}
6049

6050
SNode* createStopXnodeTaskStmt(SAstCreateContext* pCxt, const EXnodeResourceType resourceType, SToken* pIdOrName) {
880✔
6051
  SNode* pStmt = NULL;
880✔
6052
  CHECK_PARSER_STATUS(pCxt);
880✔
6053
  if (resourceType != XNODE_TASK) {
880✔
UNCOV
6054
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6055
                                            "Only support stop task, invalid resource type: %d", resourceType);
UNCOV
6056
    goto _err;
×
6057
  }
6058
  if (pIdOrName != NULL && pIdOrName->type != TK_NK_INTEGER && pIdOrName->type != TK_NK_STRING) {
880✔
6059
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6060
                                            "Xnode task id or name should be an integer or string");
6061
    goto _err;
×
6062
  }
6063
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STOP_XNODE_TASK_STMT, (SNode**)&pStmt);
880✔
6064
  CHECK_MAKE_NODE(pStmt);
880✔
6065
  SStopXnodeTaskStmt* pTaskStmt = (SStopXnodeTaskStmt*)pStmt;
880✔
6066
  if (pIdOrName->type == TK_NK_INTEGER) {
880✔
UNCOV
6067
    pTaskStmt->tid = taosStr2Int32(pIdOrName->z, NULL, 10);
×
UNCOV
6068
    if (pTaskStmt->tid <= 0) {
×
UNCOV
6069
      pCxt->errCode =
×
6070
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Task id should be greater than 0");
×
UNCOV
6071
      goto _err;
×
6072
    }
6073
  } else {
6074
    if (pIdOrName->n > TSDB_XNODE_RESOURCE_NAME_LEN + 2) {
880✔
6075
      pCxt->errCode =
×
UNCOV
6076
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6077
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_RESOURCE_NAME_LEN);
UNCOV
6078
      goto _err;
×
6079
    }
6080
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
880✔
6081
    COPY_STRING_FORM_STR_TOKEN(buf, pIdOrName);
880✔
6082
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
880✔
6083
  }
6084

6085
  return (SNode*)pTaskStmt;
880✔
6086
_err:
×
6087
  if (pStmt != NULL) {
×
UNCOV
6088
    nodesDestroyNode(pStmt);
×
6089
  }
UNCOV
6090
  return NULL;
×
6091
}
6092

6093
SNode* rebalanceXnodeJobWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceId, SNode* pNode) {
968✔
6094
  SNode* pStmt = NULL;
968✔
6095
  if (pResourceId == NULL) {
968✔
UNCOV
6096
    pCxt->errCode =
×
UNCOV
6097
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should not be NULL");
×
UNCOV
6098
    goto _err;
×
6099
  }
6100
  if (pNode == NULL) {
968✔
UNCOV
6101
    pCxt->errCode =
×
6102
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job options should not be NULL");
×
6103
    goto _err;
×
6104
  }
6105
  if (pResourceId->type != TK_NK_INTEGER) {
968✔
6106
    pCxt->errCode =
×
UNCOV
6107
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should be an integer");
×
UNCOV
6108
    goto _err;
×
6109
  }
6110
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_STMT, (SNode**)&pStmt);
968✔
6111
  CHECK_MAKE_NODE(pStmt);
968✔
6112

6113
  SRebalanceXnodeJobStmt* pJobStmt = (SRebalanceXnodeJobStmt*)pStmt;
968✔
6114
  char                    buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
968✔
6115
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceId);
968✔
6116
  pJobStmt->jid = atoi(buf);
968✔
6117

6118
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
968✔
6119
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
968✔
6120
    // printXnodeTaskOptions(&options->opts);
6121
    pJobStmt->options = options;
968✔
6122
  }
6123
  return (SNode*)pJobStmt;
968✔
6124
_err:
×
UNCOV
6125
  return NULL;
×
6126
}
6127

6128
SNode* createRebalanceXnodeJobStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* resourceId,
968✔
6129
                                   SNode* pNodeOptions) {
6130
  CHECK_PARSER_STATUS(pCxt);
968✔
6131

6132
  switch (resourceType) {
968✔
6133
    case XNODE_JOB: {
968✔
6134
      return rebalanceXnodeJobWithOptionsDirectly(pCxt, resourceId, pNodeOptions);
968✔
6135
    }
UNCOV
6136
    default:
×
UNCOV
6137
      pCxt->errCode =
×
UNCOV
6138
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6139
                                  "Invalid xnode resource type: %d, rebalance only support job", resourceType);
6140
      goto _err;
×
6141
  }
UNCOV
6142
_err:
×
UNCOV
6143
  return NULL;
×
6144
}
6145

6146
SNode* rebalanceXnodeJobWhereDirectly(SAstCreateContext* pCxt, SNode* pWhere) {
1,320✔
6147
  int32_t code = 0;
1,320✔
6148
  SNode*  pStmt = NULL;
1,320✔
6149

6150
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_WHERE_STMT, (SNode**)&pStmt);
1,320✔
6151
  CHECK_MAKE_NODE(pStmt);
1,320✔
6152

6153
  SRebalanceXnodeJobWhereStmt* pJobStmt = (SRebalanceXnodeJobWhereStmt*)pStmt;
1,320✔
6154
  pJobStmt->pWhere = pWhere;
1,320✔
6155

6156
  return (SNode*)pJobStmt;
1,320✔
UNCOV
6157
_err:
×
6158
  return NULL;
×
6159
}
6160

6161
SNode* createRebalanceXnodeJobWhereStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
1,320✔
6162
  CHECK_PARSER_STATUS(pCxt);
1,320✔
6163

6164
  switch (resourceType) {
1,320✔
6165
    case XNODE_JOB: {
1,320✔
6166
      return rebalanceXnodeJobWhereDirectly(pCxt, pWhere);
1,320✔
6167
    }
UNCOV
6168
    default:
×
UNCOV
6169
      pCxt->errCode =
×
UNCOV
6170
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6171
                                  "Invalid xnode resource type: %d, rebalance only support job", resourceType);
UNCOV
6172
      goto _err;
×
6173
  }
6174
_err:
×
UNCOV
6175
  return NULL;
×
6176
}
6177

6178
SNode* updateXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pSource,
3,520✔
6179
                                          SNode* pSink, SNode* pNode) {
6180
  SNode* pStmt = NULL;
3,520✔
6181

6182
  if ((pSource != NULL && nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT) ||
3,520✔
6183
      (pSink != NULL && nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT)) {
1,760✔
6184
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6185
                                            "Xnode task source and sink should be valid nodes");
6186
    goto _err;
×
6187
  }
6188
  if (pSource == NULL && pSink == NULL && pNode == NULL) {
3,520✔
UNCOV
6189
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6190
                                            "Xnode task source, sink, and with options can't all be NULL");
6191
    goto _err;
×
6192
  }
6193

6194
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_XNODE_TASK_STMT, (SNode**)&pStmt);
3,520✔
6195
  CHECK_MAKE_NODE(pStmt);
3,520✔
6196
  SUpdateXnodeTaskStmt* pTaskStmt = (SUpdateXnodeTaskStmt*)pStmt;
3,520✔
6197
  if (pResIdOrName->type == TK_NK_INTEGER) {
3,520✔
6198
    pTaskStmt->tid = taosStr2Int32(pResIdOrName->z, NULL, 10);
880✔
6199
  } else {
6200
    if (pResIdOrName->n <= 2) {
2,640✔
UNCOV
6201
      pCxt->errCode =
×
6202
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name can't be empty string");
×
UNCOV
6203
      goto _err;
×
6204
    }
6205
    char buf[TSDB_XNODE_TASK_NAME_LEN] = {0};
2,640✔
6206
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
2,640✔
6207
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
2,640✔
6208
  }
6209

6210
  if (pSource != NULL) {
3,520✔
6211
    SXTaskSource* source = (SXTaskSource*)(pSource);
880✔
6212
    pTaskStmt->source = source;
880✔
6213
  }
6214
  if (pSink != NULL) {
3,520✔
6215
    SXTaskSink* sink = (SXTaskSink*)(pSink);
1,760✔
6216
    pTaskStmt->sink = sink;
1,760✔
6217
  }
6218
  if (pNode != NULL) {
3,520✔
6219
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
3,520✔
6220
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
3,520✔
6221
      pTaskStmt->options = options;
3,520✔
6222
    }
6223
  }
6224
  return (SNode*)pTaskStmt;
3,520✔
UNCOV
6225
_err:
×
UNCOV
6226
  if (pStmt != NULL) {
×
UNCOV
6227
    nodesDestroyNode(pStmt);
×
6228
  }
UNCOV
6229
  return NULL;
×
6230
}
6231

6232
SNode* alterXnodeJobWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pNode) {
88✔
6233
  SNode* pStmt = NULL;
88✔
6234
  if (pResourceName == NULL) {
88✔
UNCOV
6235
    pCxt->errCode =
×
UNCOV
6236
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should not be NULL");
×
UNCOV
6237
    goto _err;
×
6238
  }
6239
  if (pNode == NULL) {
88✔
UNCOV
6240
    pCxt->errCode =
×
6241
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job options should not be NULL");
×
6242
    goto _err;
×
6243
  }
6244
  if (pResourceName->type != TK_NK_INTEGER) {
88✔
6245
    pCxt->errCode =
×
UNCOV
6246
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should be integer");
×
UNCOV
6247
    goto _err;
×
6248
  }
6249
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_JOB_STMT, (SNode**)&pStmt);
88✔
6250
  CHECK_MAKE_NODE(pStmt);
88✔
6251

6252
  SAlterXnodeJobStmt* pJobStmt = (SAlterXnodeJobStmt*)pStmt;
88✔
6253
  char                buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
88✔
6254
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
88✔
6255
  pJobStmt->jid = atoi(buf);
88✔
6256

6257
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
88✔
6258
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
88✔
6259
    pJobStmt->options = options;
88✔
6260
  }
6261
  return (SNode*)pJobStmt;
88✔
6262
_err:
×
6263
  if (pStmt != NULL) {
×
UNCOV
6264
    nodesDestroyNode(pStmt);
×
6265
  }
UNCOV
6266
  return NULL;
×
6267
}
6268

6269
SNode* alterXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pNode) {
264✔
6270
  SNode* pStmt = NULL;
264✔
6271
  if (NULL == pNode) {
264✔
UNCOV
6272
    pCxt->errCode =
×
UNCOV
6273
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode alter agent options can't be null");
×
UNCOV
6274
    goto _err;
×
6275
  }
6276

6277
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_AGENT_STMT, (SNode**)&pStmt);
264✔
6278
  CHECK_MAKE_NODE(pStmt);
264✔
6279
  SAlterXnodeAgentStmt* pAgentStmt = (SAlterXnodeAgentStmt*)pStmt;
264✔
6280
  if (pResIdOrName->type == TK_NK_INTEGER) {
264✔
UNCOV
6281
    pAgentStmt->id = taosStr2Int32(pResIdOrName->z, NULL, 10);
×
6282
  } else {
6283
    if (pResIdOrName->n <= 2) {
264✔
UNCOV
6284
      pCxt->errCode =
×
UNCOV
6285
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode alter agent name can't be empty string");
×
UNCOV
6286
      goto _err;
×
6287
    }
6288
    char buf[TSDB_XNODE_AGENT_NAME_LEN] = {0};
264✔
6289
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
264✔
6290
    pAgentStmt->name = xCreateCowStr(strlen(buf), buf, true);
264✔
6291
  }
6292

6293
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
264✔
6294
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
264✔
6295
    pAgentStmt->options = options;
264✔
6296
  }
6297

6298
  return (SNode*)pAgentStmt;
264✔
UNCOV
6299
_err:
×
6300
  if (pStmt != NULL) {
×
6301
    nodesDestroyNode(pStmt);
×
6302
  }
UNCOV
6303
  return NULL;
×
6304
}
6305

6306
SNode* alterXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResIdOrName,
3,872✔
6307
                                 SNode* pSource, SNode* pSink, SNode* pNode) {
6308
  CHECK_PARSER_STATUS(pCxt);
3,872✔
6309

6310
  switch (resourceType) {
3,872✔
6311
    case XNODE_TASK: {
3,520✔
6312
      return updateXnodeTaskWithOptionsDirectly(pCxt, pResIdOrName, pSource, pSink, pNode);
3,520✔
6313
    }
6314
    case XNODE_AGENT: {
264✔
6315
      return alterXnodeAgentWithOptionsDirectly(pCxt, pResIdOrName, pNode);
264✔
6316
    }
6317
    case XNODE_JOB: {
88✔
6318
      return alterXnodeJobWithOptionsDirectly(pCxt, pResIdOrName, pNode);
88✔
6319
    }
UNCOV
6320
    default:
×
UNCOV
6321
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6322
                                              "Invalid xnode resource type: %d", resourceType);
UNCOV
6323
      goto _err;
×
6324
  }
UNCOV
6325
_err:
×
UNCOV
6326
  return NULL;
×
6327
}
6328

6329
SNode* dropXnodeResource(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SToken* pResourceName) {
26,752✔
6330
  SNode* pStmt = NULL;
26,752✔
6331
  char   buf[TSDB_XNODE_TASK_NAME_LEN + 1] = {0};
26,752✔
6332

6333
  CHECK_PARSER_STATUS(pCxt);
26,752✔
6334
  if (pResourceName == NULL || pResourceName->n <= 0) {
26,752✔
UNCOV
6335
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6336
                                            "Xnode resource name should not be NULL or empty");
6337
    goto _err;
×
6338
  }
6339
  if (pResourceName->n > TSDB_XNODE_RESOURCE_NAME_LEN) {
26,752✔
6340
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
880✔
6341
                                            "Invalid xnode resource name length: %d, max length: %d", pResourceName->n,
6342
                                            TSDB_XNODE_RESOURCE_NAME_LEN);
6343
    goto _err;
880✔
6344
  }
6345
  switch (resourceType) {
25,872✔
6346
    case XNODE_TASK:
22,880✔
6347
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
22,880✔
6348
      CHECK_MAKE_NODE(pStmt);
22,880✔
6349
      SDropXnodeTaskStmt* pTaskStmt = (SDropXnodeTaskStmt*)pStmt;
22,880✔
6350

6351
      if (pResourceName->type == TK_NK_STRING) {
22,880✔
6352
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
22,880✔
6353
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6354
                                                  "Invalid xnode task name length: %d, max length: %d",
6355
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
UNCOV
6356
          goto _err;
×
6357
        }
6358
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
22,880✔
6359
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
22,880✔
UNCOV
6360
      } else if (pResourceName->type == TK_NK_ID) {
×
UNCOV
6361
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
UNCOV
6362
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
×
UNCOV
6363
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
UNCOV
6364
        pTaskStmt->id = taosStr2Int32(pResourceName->z, NULL, 10);
×
6365
      } else {
UNCOV
6366
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6367
                                                "Invalid xnode job id type: %d", pResourceName->type);
UNCOV
6368
        goto _err;
×
6369
      }
6370
      break;
22,880✔
6371
    case XNODE_AGENT:
2,552✔
6372
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
2,552✔
6373
      CHECK_MAKE_NODE(pStmt);
2,552✔
6374
      SDropXnodeAgentStmt* pDropAgent = (SDropXnodeAgentStmt*)pStmt;
2,552✔
6375

6376
      if (pResourceName->type == TK_NK_STRING) {
2,552✔
6377
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
2,552✔
6378
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6379
                                                  "Invalid xnode task name length: %d, max length: %d",
6380
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
UNCOV
6381
          goto _err;
×
6382
        }
6383
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
2,552✔
6384
        pDropAgent->name = taosStrndupi(buf, sizeof(buf));
2,552✔
UNCOV
6385
      } else if (pResourceName->type == TK_NK_ID) {
×
UNCOV
6386
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
UNCOV
6387
        pDropAgent->name = taosStrndupi(buf, sizeof(buf));
×
UNCOV
6388
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
UNCOV
6389
        pDropAgent->id = taosStr2Int32(pResourceName->z, NULL, 10);
×
6390
      } else {
UNCOV
6391
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6392
                                                "Invalid xnode agent id type: %d", pResourceName->type);
UNCOV
6393
        goto _err;
×
6394
      }
6395
      break;
2,552✔
6396
    case XNODE_JOB:
440✔
6397
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_JOB_STMT, (SNode**)&pStmt);
440✔
6398
      CHECK_MAKE_NODE(pStmt);
440✔
6399
      SDropXnodeJobStmt* pJobStmt = (SDropXnodeJobStmt*)pStmt;
440✔
6400

6401
      if (pResourceName->type == TK_NK_STRING) {
440✔
6402
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
×
6403
      } else if (pResourceName->type == TK_NK_INTEGER) {
440✔
6404
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
440✔
6405
      } else {
UNCOV
6406
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6407
                                                "Invalid xnode job id type: %d", pResourceName->type);
UNCOV
6408
        goto _err;
×
6409
      }
6410
      break;
440✔
UNCOV
6411
    default:
×
UNCOV
6412
      break;
×
6413
  }
6414
  return (SNode*)pStmt;
25,872✔
6415
_err:
880✔
6416
  if (pStmt != NULL) {
880✔
UNCOV
6417
    nodesDestroyNode(pStmt);
×
6418
  }
6419
  return NULL;
880✔
6420
}
6421

6422
SNode* dropXnodeResourceWhere(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
14,520✔
6423
  CHECK_PARSER_STATUS(pCxt);
14,520✔
6424
  SDropXnodeJobStmt* pStmt = NULL;
14,520✔
6425
  switch (resourceType) {
14,520✔
UNCOV
6426
    case XNODE_TASK:
×
6427
    case XNODE_AGENT:
6428
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6429
                                              "Xnode only drop xnode job where ... support");
UNCOV
6430
      goto _err;
×
6431
    case XNODE_JOB:
14,520✔
6432
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_JOB_STMT, (SNode**)&pStmt);
14,520✔
6433
      CHECK_MAKE_NODE(pStmt);
14,520✔
6434
      pStmt->pWhere = pWhere;
14,520✔
6435
      break;
14,520✔
UNCOV
6436
    default:
×
UNCOV
6437
      break;
×
6438
  }
6439
  return (SNode*)pStmt;
14,520✔
UNCOV
6440
_err:
×
UNCOV
6441
  nodesDestroyNode(pWhere);
×
6442
  return NULL;
×
6443
}
6444

6445
SNode* createDefaultXnodeTaskOptions(SAstCreateContext* pCxt) {
41,360✔
6446
  CHECK_PARSER_STATUS(pCxt);
41,360✔
6447
  SXnodeTaskOptions* pOptions = NULL;
41,360✔
6448
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_OPTIONS, (SNode**)&pOptions);
41,360✔
6449
  CHECK_MAKE_NODE(pOptions);
41,360✔
6450
  return (SNode*)pOptions;
41,360✔
UNCOV
6451
_err:
×
6452
  return NULL;
×
6453
}
6454

6455
static char   TRIGGER[8] = "trigger";
6456
static SToken TRIGGER_TOKEN = {
6457
    .n = 7,
6458
    .type = TK_NK_ID,
6459
    .z = TRIGGER,
6460
};
UNCOV
6461
SToken* createTriggerToken() { return &TRIGGER_TOKEN; }
×
6462

6463
SNode*  setXnodeTaskOption(SAstCreateContext* pCxt, SNode* pTaskOptions, SToken* pKey, SToken* pVal) {
112,464✔
6464
  CHECK_PARSER_STATUS(pCxt);
112,464✔
6465
  if (pTaskOptions == NULL) {
112,464✔
6466
    pTaskOptions = createDefaultXnodeTaskOptions(pCxt);
41,360✔
6467
    if (pTaskOptions == NULL) {
41,360✔
6468
      pCxt->errCode =
×
UNCOV
6469
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task options should not be NULL");
×
UNCOV
6470
      goto _err;
×
6471
    }
6472
  }
6473
  SXnodeTaskOptions* pOptions = (SXnodeTaskOptions*)pTaskOptions;
112,464✔
6474
  char               key[TSDB_COL_NAME_LEN] = {0};
112,464✔
6475
  if (pKey == NULL) {
112,464✔
UNCOV
6476
    pCxt->errCode =
×
6477
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
UNCOV
6478
    goto _err;
×
6479
  }
6480
  TRIM_STRING_FORM_ID_TOKEN(key, pKey);
112,464✔
6481

6482
  if (strlen(key) == 0) {
112,464✔
UNCOV
6483
    pCxt->errCode =
×
6484
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
6485
    goto _err;
×
6486
  }
6487
  char via[TSDB_COL_NAME_LEN] = {0};
112,464✔
6488
  char buf[TSDB_XNODE_TASK_OPTIONS_MAX_NUM] = {0};
112,464✔
6489
  if (strcmp(key, "trigger") == 0) {
112,464✔
6490
    if (pVal->type == TK_NK_STRING) {
21,384✔
6491
      (void)trimString(pVal->z, pVal->n, pOptions->trigger, sizeof(pOptions->trigger));
21,384✔
6492
      pOptions->triggerLen = pVal->n == 2 ? 1 : pVal->n - 2;
21,384✔
6493
    } else {
6494
      pCxt->errCode =
×
UNCOV
6495
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option trigger must be string");
×
UNCOV
6496
      goto _err;
×
6497
    }
6498
  } else if (strcmp(key, "parser") == 0 || strcmp(key, "transform") == 0) {
91,080✔
6499
    if (pVal->type == TK_NK_STRING) {
20,240✔
6500
      if (pVal->n > TSDB_XNODE_TASK_PARSER_LEN + 2) {
20,240✔
6501
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_MND_XNODE_TASK_PARSER_TOO_LONG,
×
6502
                                                "Option parser must be string with length <= %d",
6503
                                                TSDB_XNODE_TASK_PARSER_LEN);
UNCOV
6504
        goto _err;
×
6505
      }
6506
      if (pOptions->parser) {
20,240✔
UNCOV
6507
        taosMemFreeClear(pOptions->parser);
×
6508
      }
6509
      pOptions->parserLen = pVal->n == 2 ? 1 : pVal->n - 2;
20,240✔
6510
      pOptions->parser = taosMemoryCalloc(1, pOptions->parserLen + 1);
20,240✔
6511
      (void)trimString(pVal->z, pVal->n, pOptions->parser, pOptions->parserLen + 1);
20,240✔
6512
    } else {
UNCOV
6513
      pCxt->errCode =
×
UNCOV
6514
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option parser must be string");
×
UNCOV
6515
      goto _err;
×
6516
    }
6517
  } else if (strcmp(key, "health") == 0) {
70,840✔
UNCOV
6518
    if (pVal->type == TK_NK_STRING) {
×
UNCOV
6519
      (void)trimString(pVal->z, pVal->n, pOptions->health, sizeof(pOptions->health));
×
6520
      pOptions->healthLen = pVal->n == 2 ? 1 : pVal->n - 2;
×
6521
    } else {
UNCOV
6522
      pCxt->errCode =
×
6523
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option health must be string");
×
UNCOV
6524
      goto _err;
×
6525
    }
6526
  } else if (strcmp(key, "via") == 0) {
70,840✔
6527
    switch (pVal->type) {
3,520✔
UNCOV
6528
      case TK_NK_STRING:
×
6529
        (void)trimString(pVal->z, pVal->n, via, sizeof(via));
×
6530
        pOptions->via = taosStr2Int32(via, NULL, 10);
×
6531
        if (pOptions->via <= 0) {
×
UNCOV
6532
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6533
                                                   "Invalid xnode task option via: %s", pVal->z);
6534
          goto _err;
×
6535
        }
6536
        break;
×
6537
      case TK_NK_INTEGER:
3,520✔
6538
        pOptions->via = taosStr2Int32(pVal->z, NULL, 10);
3,520✔
6539
        break;
3,520✔
6540
      default:
×
UNCOV
6541
        pCxt->errCode =
×
UNCOV
6542
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode task option: %s", key);
×
6543
    }
6544
  } else {
6545
    if (pOptions->optionsNum < TSDB_XNODE_TASK_OPTIONS_MAX_NUM) {
67,320✔
6546
      char* pKeyVal = NULL;
67,320✔
6547
      if (pVal != NULL) {
67,320✔
6548
        parserDebug("key value length expected: %d, actual: %d\n", pKey->n + pVal->n + 2, pKey->n + pVal->n);
67,320✔
6549
        pKeyVal = taosMemoryMalloc(pKey->n + pVal->n + 2);
67,320✔
6550
        memset(pKeyVal, 0, pKey->n + pVal->n + 2);
67,320✔
6551

6552
        CHECK_OUT_OF_MEM(pKeyVal);
67,320✔
6553
        size_t pos = strlen(key);
67,320✔
6554
        memcpy(pKeyVal, key, pos);
67,320✔
6555
        pKeyVal[pos] = '=';  // Add '=' after the key
67,320✔
6556
        pos++;
67,320✔
6557

6558
        if (pVal->type == TK_NK_STRING) {
67,320✔
6559
          (void)trimString(pVal->z, pVal->n, pKeyVal + pos, pVal->n + 1);
39,688✔
6560
        } else {
6561
          strncpy(pKeyVal + pos, pVal->z, TMIN(pVal->n, pKey->n + pVal->n + 2 - pos - 1));
27,632✔
6562
          pKeyVal[pos + pVal->n] = '\0';
27,632✔
6563
        }
6564
      } else {
UNCOV
6565
        size_t keyLen = strlen(key);
×
UNCOV
6566
        pKeyVal = taosMemoryMalloc(keyLen + 1);
×
UNCOV
6567
        memset(pKeyVal, 0, keyLen + 1);
×
UNCOV
6568
        CHECK_OUT_OF_MEM(pKeyVal);
×
UNCOV
6569
        memcpy(pKeyVal, key, keyLen);
×
6570
      }
6571
      pOptions->options[pOptions->optionsNum] = pKeyVal;
67,320✔
6572
      pOptions->optionsNum++;
67,320✔
6573
    } else {
UNCOV
6574
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6575
                                               "reaches max options number(%d) %s", pOptions->optionsNum, key);
UNCOV
6576
      goto _err;
×
6577
    }
6578
  }
6579
  return pTaskOptions;
112,464✔
UNCOV
6580
_err:
×
6581
  nodesDestroyNode(pTaskOptions);
×
6582
  return NULL;
×
6583
}
6584

6585
SNode* createXnodeTaskJobWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pTidToken,
7,568✔
6586
                                     SNode* pNodeOptions) {
6587
  CHECK_PARSER_STATUS(pCxt);
7,568✔
6588
  SNode* pStmt = NULL;
7,568✔
6589

6590
  switch (resourceType) {
7,568✔
6591
    case XNODE_JOB: {
7,568✔
6592
      if (pTidToken == NULL || pTidToken->n <= 0) {
7,568✔
UNCOV
6593
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6594
                                                "Xnode job task id should not be NULL or empty");
UNCOV
6595
        goto _err;
×
6596
      }
6597
      if (pNodeOptions == NULL || nodeType(pNodeOptions) != QUERY_NODE_XNODE_TASK_OPTIONS) {
7,568✔
6598
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6599
                                                "Xnode job options should not be NULL or empty");
UNCOV
6600
        goto _err;
×
6601
      }
6602
      pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_JOB_STMT, &pStmt);
7,568✔
6603
      CHECK_MAKE_NODE(pStmt);
7,568✔
6604
      SCreateXnodeJobStmt* pJobStmt = (SCreateXnodeJobStmt*)pStmt;
7,568✔
6605
      pJobStmt->options = (SXnodeTaskOptions*)pNodeOptions;
7,568✔
6606
      pJobStmt->tid = pTidToken->type == TK_NK_STRING ? atoi(pTidToken->z) : taosStr2Int32(pTidToken->z, NULL, 10);
7,568✔
6607
      break;
7,568✔
6608
    }
6609
    default:
×
UNCOV
6610
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6611
                                              "Invalid xnode resource type: %d with ON clause", resourceType);
UNCOV
6612
      goto _err;
×
6613
  }
6614
  return pStmt;
7,568✔
UNCOV
6615
_err:
×
6616
  if (pStmt != NULL) {
×
UNCOV
6617
    nodesDestroyNode(pStmt);
×
6618
  }
UNCOV
6619
  return NULL;
×
6620
}
6621

6622
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
3,476✔
6623
  SToken config;
3,476✔
6624
  config.type = TK_NK_STRING;
3,476✔
6625
  config.z = "\"encrypt_key\"";
3,476✔
6626
  config.n = strlen(config.z);
3,476✔
6627
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
3,476✔
6628
}
6629

6630
SNode* createAlterEncryptKeyStmt(SAstCreateContext* pCxt, int8_t keyType, const SToken* pValue) {
568✔
6631
  CHECK_PARSER_STATUS(pCxt);
568✔
6632
  SAlterEncryptKeyStmt* pStmt = NULL;
568✔
6633
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ENCRYPT_KEY_STMT, (SNode**)&pStmt);
568✔
6634
  CHECK_MAKE_NODE(pStmt);
568✔
6635

6636
  pStmt->keyType = keyType;
568✔
6637
  if (NULL != pValue) {
568✔
6638
    (void)trimString(pValue->z, pValue->n, pStmt->newKey, sizeof(pStmt->newKey));
568✔
6639
  }
6640

6641
  return (SNode*)pStmt;
568✔
UNCOV
6642
_err:
×
UNCOV
6643
  return NULL;
×
6644
}
6645

UNCOV
6646
SNode* createAlterKeyExpirationStmt(SAstCreateContext* pCxt, const SToken* pDays, const SToken* pStrategy) {
×
UNCOV
6647
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
6648
  SAlterKeyExpirationStmt* pStmt = NULL;
×
UNCOV
6649
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_KEY_EXPIRATION_STMT, (SNode**)&pStmt);
×
UNCOV
6650
  CHECK_MAKE_NODE(pStmt);
×
6651

UNCOV
6652
  if (NULL != pDays) {
×
UNCOV
6653
    pStmt->days = taosStr2Int32(pDays->z, NULL, 10);
×
6654
  }
UNCOV
6655
  if (NULL != pStrategy) {
×
UNCOV
6656
    (void)trimString(pStrategy->z, pStrategy->n, pStmt->strategy, sizeof(pStmt->strategy));
×
6657
  }
6658

6659
  return (SNode*)pStmt;
×
UNCOV
6660
_err:
×
UNCOV
6661
  return NULL;
×
6662
}
6663

6664
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
20,860✔
6665
  if (!checkIndexName(pCxt, pIndexName)) {
20,860✔
6666
    return NULL;
×
6667
  }
6668
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
20,860✔
6669
}
6670

6671
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
42,944✔
6672
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
6673
  CHECK_PARSER_STATUS(pCxt);
42,944✔
6674
  SCreateIndexStmt* pStmt = NULL;
42,944✔
6675
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
42,944✔
6676
  CHECK_MAKE_NODE(pStmt);
42,944✔
6677
  pStmt->indexType = type;
42,944✔
6678
  pStmt->ignoreExists = ignoreExists;
42,944✔
6679

6680
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
42,944✔
6681
  if (strlen(pFullTable->table.dbName) == 0) {
42,944✔
6682
    // no db specified,
UNCOV
6683
    if (pCxt->pQueryCxt->db == NULL) {
×
UNCOV
6684
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
UNCOV
6685
      CHECK_PARSER_STATUS(pCxt);
×
6686
    } else {
UNCOV
6687
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
6688
    }
6689
  } else {
6690
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
42,944✔
6691
  }
6692
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
42,944✔
6693
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
42,944✔
6694
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
42,944✔
6695
  nodesDestroyNode(pIndexName);
42,944✔
6696
  nodesDestroyNode(pRealTable);
42,944✔
6697
  pStmt->pCols = pCols;
42,944✔
6698
  pStmt->pOptions = (SIndexOptions*)pOptions;
42,944✔
6699
  return (SNode*)pStmt;
42,944✔
6700
_err:
×
6701
  nodesDestroyNode(pIndexName);
×
UNCOV
6702
  nodesDestroyNode(pRealTable);
×
6703
  nodesDestroyNode(pOptions);
×
UNCOV
6704
  nodesDestroyList(pCols);
×
UNCOV
6705
  return NULL;
×
6706
}
6707

UNCOV
6708
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
×
6709
                         SNode* pStreamOptions) {
UNCOV
6710
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
6711
  SIndexOptions* pOptions = NULL;
×
UNCOV
6712
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
×
UNCOV
6713
  CHECK_MAKE_NODE(pOptions);
×
UNCOV
6714
  pOptions->pFuncs = pFuncs;
×
UNCOV
6715
  pOptions->pInterval = pInterval;
×
6716
  pOptions->pOffset = pOffset;
×
6717
  pOptions->pSliding = pSliding;
×
6718
  pOptions->pStreamOptions = pStreamOptions;
×
6719
  return (SNode*)pOptions;
×
6720
_err:
×
6721
  nodesDestroyNode(pInterval);
×
UNCOV
6722
  nodesDestroyNode(pOffset);
×
UNCOV
6723
  nodesDestroyNode(pSliding);
×
6724
  nodesDestroyNode(pStreamOptions);
×
UNCOV
6725
  return NULL;
×
6726
}
6727

6728
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
20,860✔
6729
  CHECK_PARSER_STATUS(pCxt);
20,860✔
6730
  SDropIndexStmt* pStmt = NULL;
20,860✔
6731
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
20,860✔
6732
  CHECK_MAKE_NODE(pStmt);
20,860✔
6733
  pStmt->ignoreNotExists = ignoreNotExists;
20,860✔
6734
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
20,860✔
6735
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
20,860✔
6736
  nodesDestroyNode(pIndexName);
20,860✔
6737
  return (SNode*)pStmt;
20,860✔
6738
_err:
×
6739
  nodesDestroyNode(pIndexName);
×
6740
  return NULL;
×
6741
}
6742

6743
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
214,698✔
6744
  CHECK_PARSER_STATUS(pCxt);
214,698✔
6745
  SCreateComponentNodeStmt* pStmt = NULL;
214,698✔
6746
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
214,698✔
6747
  CHECK_MAKE_NODE(pStmt);
214,698✔
6748
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
214,698✔
6749
  return (SNode*)pStmt;
214,698✔
UNCOV
6750
_err:
×
UNCOV
6751
  return NULL;
×
6752
}
6753

6754
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
103,636✔
6755
  CHECK_PARSER_STATUS(pCxt);
103,636✔
6756
  SDropComponentNodeStmt* pStmt = NULL;
103,636✔
6757
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
103,636✔
6758
  CHECK_MAKE_NODE(pStmt);
103,636✔
6759
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
103,636✔
6760
  return (SNode*)pStmt;
103,636✔
UNCOV
6761
_err:
×
UNCOV
6762
  return NULL;
×
6763
}
6764

6765
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
3,404✔
6766
  CHECK_PARSER_STATUS(pCxt);
3,404✔
6767
  SRestoreComponentNodeStmt* pStmt = NULL;
3,404✔
6768
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,404✔
6769
  CHECK_MAKE_NODE(pStmt);
3,404✔
6770
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
3,404✔
6771
  return (SNode*)pStmt;
3,404✔
UNCOV
6772
_err:
×
UNCOV
6773
  return NULL;
×
6774
}
6775

6776
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery, bool reload) {
290,762✔
6777
  CHECK_PARSER_STATUS(pCxt);
290,762✔
6778
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
290,762✔
6779
  SCreateTopicStmt* pStmt = NULL;
289,626✔
6780
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
289,626✔
6781
  CHECK_MAKE_NODE(pStmt);
289,626✔
6782
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
289,626✔
6783
  pStmt->ignoreExists = ignoreExists;
289,626✔
6784
  pStmt->pQuery = pQuery;
289,626✔
6785
  pStmt->reload = reload;
289,626✔
6786
  return (SNode*)pStmt;
289,626✔
6787
_err:
1,136✔
6788
  nodesDestroyNode(pQuery);
1,136✔
6789
  return NULL;
1,136✔
6790
}
6791

6792
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
54,726✔
6793
                                  int8_t withMeta) {
6794
  CHECK_PARSER_STATUS(pCxt);
54,726✔
6795
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
54,726✔
6796
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
54,726✔
6797
  SCreateTopicStmt* pStmt = NULL;
54,726✔
6798
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
54,726✔
6799
  CHECK_MAKE_NODE(pStmt);
54,726✔
6800
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
54,726✔
6801
  pStmt->ignoreExists = ignoreExists;
54,726✔
6802
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
54,726✔
6803
  pStmt->withMeta = withMeta;
54,726✔
6804
  return (SNode*)pStmt;
54,726✔
UNCOV
6805
_err:
×
UNCOV
6806
  return NULL;
×
6807
}
6808

6809
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
31,444✔
6810
                                     int8_t withMeta, SNode* pWhere) {
6811
  CHECK_PARSER_STATUS(pCxt);
31,444✔
6812
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
31,444✔
6813
  SCreateTopicStmt* pStmt = NULL;
31,444✔
6814
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
31,444✔
6815
  CHECK_MAKE_NODE(pStmt);
31,444✔
6816
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
31,444✔
6817
  pStmt->ignoreExists = ignoreExists;
31,444✔
6818
  pStmt->withMeta = withMeta;
31,444✔
6819
  pStmt->pWhere = pWhere;
31,444✔
6820

6821
  tstrncpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
31,444✔
6822
  tstrncpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
31,444✔
6823
  nodesDestroyNode(pRealTable);
31,444✔
6824
  return (SNode*)pStmt;
31,444✔
UNCOV
6825
_err:
×
UNCOV
6826
  nodesDestroyNode(pRealTable);
×
UNCOV
6827
  nodesDestroyNode(pWhere);
×
UNCOV
6828
  return NULL;
×
6829
}
6830

6831
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName, bool force) {
231,200✔
6832
  CHECK_PARSER_STATUS(pCxt);
231,200✔
6833
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
231,200✔
6834
  SDropTopicStmt* pStmt = NULL;
230,796✔
6835
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
230,796✔
6836
  CHECK_MAKE_NODE(pStmt);
230,796✔
6837
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
230,796✔
6838
  pStmt->ignoreNotExists = ignoreNotExists;
230,796✔
6839
  pStmt->force = force;
230,796✔
6840
  return (SNode*)pStmt;
230,796✔
6841
_err:
404✔
6842
  return NULL;
404✔
6843
}
6844

6845
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName,
3,270✔
6846
                            bool force) {
6847
  CHECK_PARSER_STATUS(pCxt);
3,270✔
6848
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
3,270✔
6849
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
3,270✔
6850
  SDropCGroupStmt* pStmt = NULL;
3,270✔
6851
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
3,270✔
6852
  CHECK_MAKE_NODE(pStmt);
3,270✔
6853
  pStmt->ignoreNotExists = ignoreNotExists;
3,270✔
6854
  pStmt->force = force;
3,270✔
6855
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
3,270✔
6856
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
3,270✔
6857
  return (SNode*)pStmt;
3,270✔
UNCOV
6858
_err:
×
UNCOV
6859
  return NULL;
×
6860
}
6861

6862
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
1,848✔
6863
  CHECK_PARSER_STATUS(pCxt);
1,848✔
6864
  SAlterClusterStmt* pStmt = NULL;
1,848✔
6865
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
1,848✔
6866
  CHECK_MAKE_NODE(pStmt);
1,848✔
6867
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
1,848✔
6868
  if (NULL != pValue) {
1,848✔
6869
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
1,848✔
6870
  }
6871
  return (SNode*)pStmt;
1,848✔
UNCOV
6872
_err:
×
UNCOV
6873
  return NULL;
×
6874
}
6875

6876
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
871,500✔
6877
  CHECK_PARSER_STATUS(pCxt);
871,500✔
6878
  SAlterLocalStmt* pStmt = NULL;
871,500✔
6879
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
871,500✔
6880
  CHECK_MAKE_NODE(pStmt);
871,500✔
6881
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
871,500✔
6882
  if (NULL != pValue) {
871,500✔
6883
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
860,596✔
6884
  }
6885
  return (SNode*)pStmt;
871,500✔
UNCOV
6886
_err:
×
UNCOV
6887
  return NULL;
×
6888
}
6889

6890
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
190,293,310✔
6891
  CHECK_PARSER_STATUS(pCxt);
190,293,310✔
6892
  SExplainOptions* pOptions = NULL;
190,293,310✔
6893
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
190,293,310✔
6894
  CHECK_MAKE_NODE(pOptions);
190,300,384✔
6895
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
190,300,384✔
6896
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
190,300,384✔
6897
  return (SNode*)pOptions;
190,300,384✔
UNCOV
6898
_err:
×
UNCOV
6899
  return NULL;
×
6900
}
6901

6902
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
104,675,410✔
6903
  CHECK_PARSER_STATUS(pCxt);
104,675,410✔
6904
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
104,675,410✔
6905
  return pOptions;
104,675,410✔
UNCOV
6906
_err:
×
UNCOV
6907
  return NULL;
×
6908
}
6909

6910
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
3,134,410✔
6911
  CHECK_PARSER_STATUS(pCxt);
3,134,410✔
6912
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
3,134,410✔
6913
  return pOptions;
3,134,410✔
6914
_err:
×
6915
  return NULL;
×
6916
}
6917

6918
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
174,025,882✔
6919
  CHECK_PARSER_STATUS(pCxt);
174,025,882✔
6920
  SExplainStmt* pStmt = NULL;
174,025,882✔
6921
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
174,025,882✔
6922
  CHECK_MAKE_NODE(pStmt);
174,062,800✔
6923
  pStmt->analyze = analyze;
174,062,800✔
6924
  pStmt->pOptions = (SExplainOptions*)pOptions;
174,062,800✔
6925
  pStmt->pQuery = pQuery;
174,062,800✔
6926
  return (SNode*)pStmt;
174,062,800✔
UNCOV
6927
_err:
×
UNCOV
6928
  nodesDestroyNode(pOptions);
×
UNCOV
6929
  nodesDestroyNode(pQuery);
×
6930
  return NULL;
3,058✔
6931
}
6932

6933
SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
1,064,934✔
6934
  CHECK_PARSER_STATUS(pCxt);
1,064,934✔
6935
  SDescribeStmt* pStmt = NULL;
1,064,986✔
6936
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT, (SNode**)&pStmt);
1,064,986✔
6937
  CHECK_MAKE_NODE(pStmt);
1,064,986✔
6938
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
1,064,986✔
6939
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
1,064,986✔
6940
  nodesDestroyNode(pRealTable);
1,064,986✔
6941
  return (SNode*)pStmt;
1,064,986✔
UNCOV
6942
_err:
×
6943
  nodesDestroyNode(pRealTable);
×
6944
  return NULL;
×
6945
}
6946

6947
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
4,187,154✔
6948
  CHECK_PARSER_STATUS(pCxt);
4,187,154✔
6949
  SNode* pStmt = NULL;
4,187,154✔
6950
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
4,187,154✔
6951
  CHECK_MAKE_NODE(pStmt);
4,187,154✔
6952
  return pStmt;
4,187,154✔
UNCOV
6953
_err:
×
UNCOV
6954
  return NULL;
×
6955
}
6956

6957
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
30,498✔
6958
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
30,498✔
6959
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
22,938✔
6960
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
7,560✔
6961
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
7,560✔
6962
  } else {
UNCOV
6963
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6964
                                            "udf programming language supports c and python");
6965
  }
6966
  return pCxt->errCode;
30,498✔
6967
}
6968

6969
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
30,498✔
6970
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
6971
                                bool orReplace) {
6972
  CHECK_PARSER_STATUS(pCxt);
30,498✔
6973
  if (pLibPath->n <= 2) {
30,498✔
UNCOV
6974
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
6975
    CHECK_PARSER_STATUS(pCxt);
×
6976
  }
6977
  int8_t language = 0;
30,498✔
6978
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
30,498✔
6979
  CHECK_PARSER_STATUS(pCxt);
30,498✔
6980
  SCreateFunctionStmt* pStmt = NULL;
30,498✔
6981
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
30,498✔
6982
  CHECK_MAKE_NODE(pStmt);
30,498✔
6983
  pStmt->orReplace = orReplace;
30,498✔
6984
  pStmt->ignoreExists = ignoreExists;
30,498✔
6985
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
30,498✔
6986
  pStmt->isAgg = aggFunc;
30,498✔
6987
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
30,498✔
6988
  pStmt->outputDt = dataType;
30,498✔
6989
  pStmt->bufSize = bufSize;
30,498✔
6990
  pStmt->language = language;
30,498✔
6991
  return (SNode*)pStmt;
30,498✔
UNCOV
6992
_err:
×
UNCOV
6993
  return NULL;
×
6994
}
6995

6996
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
17,504✔
6997
  CHECK_PARSER_STATUS(pCxt);
17,504✔
6998
  SDropFunctionStmt* pStmt = NULL;
17,504✔
6999
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
17,504✔
7000
  CHECK_MAKE_NODE(pStmt);
17,504✔
7001
  pStmt->ignoreNotExists = ignoreNotExists;
17,504✔
7002
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
17,504✔
7003
  return (SNode*)pStmt;
17,504✔
UNCOV
7004
_err:
×
UNCOV
7005
  return NULL;
×
7006
}
7007

7008
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
430,962✔
7009
  SCreateViewStmt* pStmt = NULL;
430,962✔
7010
  CHECK_PARSER_STATUS(pCxt);
430,962✔
7011
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
430,962✔
7012
  CHECK_MAKE_NODE(pStmt);
430,962✔
7013
  int32_t i = pAs->n;
430,962✔
7014
  while (isspace(*(pAs->z + i))) {
861,924✔
7015
    ++i;
430,962✔
7016
  }
7017
  pStmt->pQuerySql = tstrdup(pAs->z + i);
430,962✔
7018
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
430,962✔
7019
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
430,962✔
7020
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
430,962✔
7021
  nodesDestroyNode(pView);
430,962✔
7022
  pStmt->orReplace = orReplace;
430,962✔
7023
  pStmt->pQuery = pQuery;
430,962✔
7024
  return (SNode*)pStmt;
430,962✔
UNCOV
7025
_err:
×
UNCOV
7026
  nodesDestroyNode(pView);
×
UNCOV
7027
  nodesDestroyNode(pQuery);
×
UNCOV
7028
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
7029
  return NULL;
×
7030
}
7031

7032
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
323,232✔
7033
  CHECK_PARSER_STATUS(pCxt);
323,232✔
7034
  SDropViewStmt* pStmt = NULL;
322,424✔
7035
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
322,424✔
7036
  CHECK_MAKE_NODE(pStmt);
322,424✔
7037
  pStmt->ignoreNotExists = ignoreNotExists;
322,424✔
7038
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
322,424✔
7039
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
322,424✔
7040
  nodesDestroyNode(pView);
322,424✔
7041
  return (SNode*)pStmt;
322,424✔
7042
_err:
808✔
7043
  nodesDestroyNode(pView);
808✔
7044
  return NULL;
808✔
7045
}
7046

7047
SNode* createStreamOutTableNode(SAstCreateContext* pCxt, SNode* pIntoTable, SNode* pOutputSubTable, SNodeList* pColList,
751,854✔
7048
                                SNodeList* pTagList) {
7049
  SStreamOutTableNode* pOutTable = NULL;
751,854✔
7050
  CHECK_PARSER_STATUS(pCxt);
751,854✔
7051
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OUT_TABLE, (SNode**)&pOutTable);
751,450✔
7052
  CHECK_MAKE_NODE(pOutTable);
751,450✔
7053
  pOutTable->pOutTable = pIntoTable;
751,450✔
7054
  pOutTable->pSubtable = pOutputSubTable;
751,450✔
7055
  pOutTable->pCols = pColList;
751,450✔
7056
  pOutTable->pTags = pTagList;
751,450✔
7057
  return (SNode*)pOutTable;
751,450✔
7058

7059
_err:
404✔
7060
  nodesDestroyNode((SNode*)pOutTable);
404✔
7061
  nodesDestroyNode(pIntoTable);
404✔
7062
  nodesDestroyNode(pOutputSubTable);
404✔
7063
  nodesDestroyList(pColList);
404✔
7064
  nodesDestroyList(pTagList);
404✔
7065
  return NULL;
404✔
7066
}
7067

7068
SNode* createStreamTriggerNode(SAstCreateContext* pCxt, SNode* pTriggerWindow, SNode* pTriggerTable,
764,250✔
7069
                               SNodeList* pPartitionList, SNode* pOptions, SNode* pNotification) {
7070
  SStreamTriggerNode* pTrigger = NULL;
764,250✔
7071
  CHECK_PARSER_STATUS(pCxt);
764,250✔
7072
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER, (SNode**)&pTrigger);
764,094✔
7073
  CHECK_MAKE_NODE(pTrigger);
764,094✔
7074

7075
  pTrigger->pOptions = pOptions;
764,094✔
7076
  pTrigger->pNotify = pNotification;
764,094✔
7077
  pTrigger->pTrigerTable = pTriggerTable;
764,094✔
7078
  pTrigger->pPartitionList = pPartitionList;
764,094✔
7079
  pTrigger->pTriggerWindow = pTriggerWindow;
764,094✔
7080
  return (SNode*)pTrigger;
764,094✔
7081

7082
_err:
156✔
7083
  nodesDestroyNode((SNode*)pTrigger);
156✔
7084
  nodesDestroyNode(pTriggerWindow);
156✔
7085
  nodesDestroyNode(pTriggerTable);
156✔
7086
  nodesDestroyNode(pOptions);
156✔
7087
  nodesDestroyNode(pNotification);
156✔
7088
  nodesDestroyList(pPartitionList);
156✔
7089
  return NULL;
156✔
7090
}
7091

7092
SNode* createSlidingWindowNode(SAstCreateContext* pCxt, SNode* pSlidingVal, SNode* pOffset) {
295,372✔
7093
  SSlidingWindowNode* pSliding = NULL;
295,372✔
7094
  CHECK_PARSER_STATUS(pCxt);
295,372✔
7095
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SLIDING_WINDOW, (SNode**)&pSliding);
295,372✔
7096
  CHECK_MAKE_NODE(pSliding);
295,372✔
7097
  pSliding->pSlidingVal = pSlidingVal;
295,372✔
7098
  pSliding->pOffset = pOffset;
295,372✔
7099
  return (SNode*)pSliding;
295,372✔
UNCOV
7100
_err:
×
UNCOV
7101
  nodesDestroyNode(pSlidingVal);
×
UNCOV
7102
  nodesDestroyNode(pOffset);
×
UNCOV
7103
  nodesDestroyNode((SNode*)pSliding);
×
UNCOV
7104
  return NULL;
×
7105
}
7106

7107
SNode* createStreamTriggerOptions(SAstCreateContext* pCxt) {
262,408✔
7108
  SStreamTriggerOptions* pOptions = NULL;
262,408✔
7109
  CHECK_PARSER_STATUS(pCxt);
262,408✔
7110
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER_OPTIONS, (SNode**)&pOptions);
262,408✔
7111
  CHECK_MAKE_NODE(pOptions);
262,408✔
7112
  pOptions->pPreFilter = NULL;
262,408✔
7113
  pOptions->pWaterMark = NULL;
262,408✔
7114
  pOptions->pMaxDelay = NULL;
262,408✔
7115
  pOptions->pExpiredTime = NULL;
262,408✔
7116
  pOptions->pFillHisStartTime = NULL;
262,408✔
7117
  pOptions->pEventType = EVENT_NONE;
262,408✔
7118
  pOptions->calcNotifyOnly = false;
262,408✔
7119
  pOptions->deleteOutputTable = false;
262,408✔
7120
  pOptions->deleteRecalc = false;
262,408✔
7121
  pOptions->fillHistory = false;
262,408✔
7122
  pOptions->fillHistoryFirst = false;
262,408✔
7123
  pOptions->lowLatencyCalc = false;
262,408✔
7124
  pOptions->forceOutput = false;
262,408✔
7125
  pOptions->ignoreDisorder = false;
262,408✔
7126
  pOptions->ignoreNoDataTrigger = false;
262,408✔
7127
  return (SNode*)pOptions;
262,408✔
UNCOV
7128
_err:
×
UNCOV
7129
  nodesDestroyNode((SNode*)pOptions);
×
UNCOV
7130
  return NULL;
×
7131
}
7132

7133
SNode* createStreamTagDefNode(SAstCreateContext* pCxt, SToken* pTagName, SDataType dataType, SNode* tagExpression) {
69,400✔
7134
  SStreamTagDefNode* pTagDef = NULL;
69,400✔
7135
  CHECK_PARSER_STATUS(pCxt);
69,400✔
7136
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TAG_DEF, (SNode**)&pTagDef);
69,400✔
7137
  CHECK_MAKE_NODE(pTagDef);
69,400✔
7138
  COPY_STRING_FORM_ID_TOKEN(pTagDef->tagName, pTagName);
69,400✔
7139
  int32_t nameLen = strdequote(pTagDef->tagName);
69,400✔
7140
  pTagDef->tagName[nameLen] = '\0';
69,400✔
7141
  pTagDef->dataType = dataType;
69,400✔
7142
  pTagDef->pTagExpr = tagExpression;
69,400✔
7143
  return (SNode*)pTagDef;
69,400✔
7144
_err:
×
7145
  nodesDestroyNode(tagExpression);
×
7146
  nodesDestroyNode((SNode*)pTagDef);
×
UNCOV
7147
  return NULL;
×
7148
}
7149

7150
SNode* setStreamTriggerOptions(SAstCreateContext* pCxt, SNode* pOptions, SStreamTriggerOption* pOptionUnit) {
332,984✔
7151
  CHECK_PARSER_STATUS(pCxt);
332,984✔
7152
  SStreamTriggerOptions* pStreamOptions = (SStreamTriggerOptions*)pOptions;
332,984✔
7153
  switch (pOptionUnit->type) {
332,984✔
7154
    case STREAM_TRIGGER_OPTION_CALC_NOTIFY_ONLY:
4,480✔
7155
      if (pStreamOptions->calcNotifyOnly) {
4,480✔
UNCOV
7156
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7157
                                                "CALC_NOTIFY_ONLY specified multiple times");
UNCOV
7158
        goto _err;
×
7159
      }
7160
      pStreamOptions->calcNotifyOnly = true;
4,480✔
7161
      break;
4,480✔
7162
    case STREAM_TRIGGER_OPTION_DELETE_OUTPUT_TABLE:
1,310✔
7163
      if (pStreamOptions->deleteOutputTable) {
1,310✔
UNCOV
7164
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7165
                                                "DELETE_OUTPUT_TABLE specified multiple times");
UNCOV
7166
        goto _err;
×
7167
      }
7168
      pStreamOptions->deleteOutputTable = true;
1,310✔
7169
      break;
1,310✔
7170
    case STREAM_TRIGGER_OPTION_DELETE_RECALC:
19,980✔
7171
      if (pStreamOptions->deleteRecalc) {
19,980✔
7172
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7173
                                                "DELETE_RECALC specified multiple times");
7174
        goto _err;
×
7175
      }
7176
      pStreamOptions->deleteRecalc = true;
19,980✔
7177
      break;
19,980✔
7178
    case STREAM_TRIGGER_OPTION_EXPIRED_TIME:
16,242✔
7179
      if (pStreamOptions->pExpiredTime != NULL) {
16,242✔
7180
        pCxt->errCode =
×
UNCOV
7181
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EXPIRED_TIME specified multiple times");
×
7182
        goto _err;
×
7183
      }
7184
      pStreamOptions->pExpiredTime = pOptionUnit->pNode;
16,242✔
7185
      break;
16,242✔
7186
    case STREAM_TRIGGER_OPTION_FORCE_OUTPUT:
8,694✔
7187
      if (pStreamOptions->forceOutput) {
8,694✔
7188
        pCxt->errCode =
×
UNCOV
7189
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FORCE_OUTPUT specified multiple times");
×
7190
        goto _err;
×
7191
      }
7192
      pStreamOptions->forceOutput = true;
8,694✔
7193
      break;
8,694✔
7194
    case STREAM_TRIGGER_OPTION_FILL_HISTORY:
80,920✔
7195
      if (pStreamOptions->fillHistoryFirst) {
80,920✔
7196
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7197
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
7198
        goto _err;
×
7199
      }
7200
      if (pStreamOptions->pFillHisStartTime != NULL) {
80,920✔
UNCOV
7201
        pCxt->errCode =
×
UNCOV
7202
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FILL_HISTORY specified multiple times");
×
UNCOV
7203
        goto _err;
×
7204
      }
7205
      pStreamOptions->fillHistory = true;
80,920✔
7206
      if (pOptionUnit->pNode == NULL) {
80,920✔
7207
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
38,046✔
7208
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
38,046✔
7209
      } else {
7210
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
42,874✔
7211
      }
7212
      break;
80,920✔
7213
    case STREAM_TRIGGER_OPTION_FILL_HISTORY_FIRST:
7,224✔
7214
      if (pStreamOptions->fillHistory) {
7,224✔
7215
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
1,332✔
7216
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
7217
        goto _err;
1,332✔
7218
      }
7219
      if (pStreamOptions->pFillHisStartTime != NULL) {
5,892✔
UNCOV
7220
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7221
                                                "FILL_HISTORY_FIRST specified multiple times");
UNCOV
7222
        goto _err;
×
7223
      }
7224
      pStreamOptions->fillHistoryFirst = true;
5,892✔
7225
      if (pOptionUnit->pNode == NULL) {
5,892✔
7226
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
944✔
7227
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
944✔
7228
      } else {
7229
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
4,948✔
7230
      }
7231
      break;
5,892✔
7232
    case STREAM_TRIGGER_OPTION_IGNORE_DISORDER:
46,164✔
7233
      if (pStreamOptions->ignoreDisorder) {
46,164✔
7234
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
388✔
7235
                                                "IGNORE_DISORDER specified multiple times");
7236
        goto _err;
388✔
7237
      }
7238
      pStreamOptions->ignoreDisorder = true;
45,776✔
7239
      break;
45,776✔
7240
    case STREAM_TRIGGER_OPTION_LOW_LATENCY_CALC:
18,464✔
7241
      if (pStreamOptions->lowLatencyCalc) {
18,464✔
UNCOV
7242
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7243
                                                "LOW_LATENCY_CALC specified multiple times");
UNCOV
7244
        goto _err;
×
7245
      }
7246
      pStreamOptions->lowLatencyCalc = true;
18,464✔
7247
      break;
18,464✔
7248
    case STREAM_TRIGGER_OPTION_MAX_DELAY:
16,792✔
7249
      if (pStreamOptions->pMaxDelay != NULL) {
16,792✔
UNCOV
7250
        pCxt->errCode =
×
UNCOV
7251
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "MAX_DELAY specified multiple times");
×
UNCOV
7252
        goto _err;
×
7253
      }
7254
      pStreamOptions->pMaxDelay = pOptionUnit->pNode;
16,792✔
7255
      break;
16,792✔
7256
    case STREAM_TRIGGER_OPTION_WATERMARK:
19,506✔
7257
      if (pStreamOptions->pWaterMark != NULL) {
19,506✔
7258
        pCxt->errCode =
×
UNCOV
7259
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "WATERMARK specified multiple times");
×
7260
        goto _err;
×
7261
      }
7262
      pStreamOptions->pWaterMark = pOptionUnit->pNode;
19,506✔
7263
      break;
19,506✔
7264
    case STREAM_TRIGGER_OPTION_PRE_FILTER:
36,852✔
7265
      if (pStreamOptions->pPreFilter != NULL) {
36,852✔
7266
        pCxt->errCode =
388✔
7267
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "PRE_FILTER specified multiple times");
388✔
7268
        goto _err;
388✔
7269
      }
7270
      pStreamOptions->pPreFilter = pOptionUnit->pNode;
36,464✔
7271
      break;
36,464✔
7272
    case STREAM_TRIGGER_OPTION_EVENT_TYPE:
18,364✔
7273
      if (pStreamOptions->pEventType != EVENT_NONE) {
18,364✔
7274
        pCxt->errCode =
×
7275
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EVENT_TYPE specified multiple times");
×
7276
        goto _err;
×
7277
      }
7278
      pStreamOptions->pEventType = pOptionUnit->flag;
18,364✔
7279
      break;
18,364✔
7280
    case STREAM_TRIGGER_OPTION_IGNORE_NODATA_TRIGGER:
37,992✔
7281
      if (pStreamOptions->ignoreNoDataTrigger) {
37,992✔
UNCOV
7282
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7283
                                                "IGNORE_NODATA_TRIGGER specified multiple times");
UNCOV
7284
        goto _err;
×
7285
      }
7286
      pStreamOptions->ignoreNoDataTrigger = true;
37,992✔
7287
      break;
37,992✔
UNCOV
7288
    default:
×
UNCOV
7289
      break;
×
7290
  }
7291
  return pOptions;
330,876✔
7292
_err:
2,108✔
7293
  nodesDestroyNode(pOptionUnit->pNode);
2,108✔
7294
  nodesDestroyNode(pOptions);
2,108✔
7295
  return NULL;
2,108✔
7296
}
7297

7298
static bool validateNotifyUrl(const char* url) {
129,836✔
7299
  const char* prefix[] = {"ws://", "wss://"};
129,836✔
7300
  const char* host = NULL;
129,836✔
7301

7302
  if (!url || *url == '\0') return false;
129,836✔
7303

7304
  for (int32_t i = 0; i < ARRAY_SIZE(prefix); ++i) {
130,148✔
7305
    if (taosStrncasecmp(url, prefix[i], strlen(prefix[i])) == 0) {
129,992✔
7306
      host = url + strlen(prefix[i]);
129,680✔
7307
      break;
129,680✔
7308
    }
7309
  }
7310

7311
  return (host != NULL) && (*host != '\0') && (*host != '/');
129,836✔
7312
}
7313

7314
SNode* createStreamNotifyOptions(SAstCreateContext* pCxt, SNodeList* pAddrUrls, int64_t eventType, SNode* pWhere,
127,896✔
7315
                                 int64_t notifyType) {
7316
  SNode* pNode = NULL;
127,896✔
7317
  CHECK_PARSER_STATUS(pCxt);
127,896✔
7318

7319
  if (LIST_LENGTH(pAddrUrls) == 0) {
127,896✔
UNCOV
7320
    pCxt->errCode =
×
UNCOV
7321
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "notification address cannot be empty");
×
UNCOV
7322
    goto _err;
×
7323
  }
7324

7325
  FOREACH(pNode, pAddrUrls) {
257,576✔
7326
    char* url = ((SValueNode*)pNode)->literal;
129,836✔
7327
    if (strlen(url) >= TSDB_STREAM_NOTIFY_URL_LEN) {
129,836✔
UNCOV
7328
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7329
                                              "notification address \"%s\" exceed maximum length %d", url,
7330
                                              TSDB_STREAM_NOTIFY_URL_LEN);
UNCOV
7331
      goto _err;
×
7332
    }
7333
    if (!validateNotifyUrl(url)) {
129,836✔
7334
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
156✔
7335
                                              "invalid notification address \"%s\"", url);
7336
      goto _err;
156✔
7337
    }
7338
  }
7339

7340
  SStreamNotifyOptions* pNotifyOptions = NULL;
127,740✔
7341
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_NOTIFY_OPTIONS, (SNode**)&pNotifyOptions);
127,740✔
7342
  CHECK_MAKE_NODE(pNotifyOptions);
127,740✔
7343
  pNotifyOptions->pAddrUrls = pAddrUrls;
127,740✔
7344
  pNotifyOptions->pWhere = pWhere;
127,740✔
7345
  pNotifyOptions->eventType = eventType;
127,740✔
7346
  pNotifyOptions->notifyType = notifyType;
127,740✔
7347
  return (SNode*)pNotifyOptions;
127,740✔
7348
_err:
156✔
7349
  nodesDestroyList(pAddrUrls);
156✔
7350
  return NULL;
156✔
7351
}
7352

7353
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pStream, SNode* pTrigger,
747,480✔
7354
                              SNode* pOutTable, SNode* pQuery) {
7355
  SCreateStreamStmt* pStmt = NULL;
747,480✔
7356
  CHECK_PARSER_STATUS(pCxt);
747,480✔
7357
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
746,672✔
7358
  CHECK_MAKE_NODE(pStmt);
746,672✔
7359

7360
  if (pOutTable && ((SStreamOutTableNode*)pOutTable)->pOutTable) {
746,672✔
7361
    tstrncpy(pStmt->targetDbName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.dbName,
739,410✔
7362
             TSDB_DB_NAME_LEN);
7363
    tstrncpy(pStmt->targetTabName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.tableName,
739,410✔
7364
             TSDB_TABLE_NAME_LEN);
7365
  }
7366

7367
  if (pStream) {
746,672✔
7368
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
746,672✔
7369
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
746,672✔
7370
  } else {
UNCOV
7371
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7372
    goto _err;
×
7373
  }
7374
  nodesDestroyNode(pStream);
746,672✔
7375

7376
  pStmt->ignoreExists = ignoreExists;
746,672✔
7377
  pStmt->pTrigger = pTrigger;
746,672✔
7378
  pStmt->pQuery = pQuery;
746,672✔
7379
  pStmt->pTags = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pTags : NULL;
746,672✔
7380
  pStmt->pSubtable = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pSubtable : NULL;
746,672✔
7381
  pStmt->pCols = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pCols : NULL;
746,672✔
7382
  return (SNode*)pStmt;
746,672✔
7383
_err:
808✔
7384
  nodesDestroyNode(pOutTable);
808✔
7385
  nodesDestroyNode(pQuery);
808✔
7386
  nodesDestroyNode(pTrigger);
808✔
7387
  nodesDestroyNode(pQuery);
808✔
7388
  return NULL;
808✔
7389
}
7390

7391
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNodeList* pStreamList) {
30,278✔
7392
  CHECK_PARSER_STATUS(pCxt);
30,278✔
7393
  SDropStreamStmt* pStmt = NULL;
30,278✔
7394
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
30,278✔
7395
  CHECK_MAKE_NODE(pStmt);
30,278✔
7396

7397
  if (pStreamList) {
30,278✔
7398
    pStmt->pStreamList = pStreamList;
30,278✔
7399
  } else {
UNCOV
7400
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7401
    goto _err;
×
7402
  }
7403

7404
  pStmt->ignoreNotExists = ignoreNotExists;
30,278✔
7405
  return (SNode*)pStmt;
30,278✔
UNCOV
7406
_err:
×
UNCOV
7407
  nodesDestroyList(pStreamList);
×
UNCOV
7408
  return NULL;
×
7409
}
7410

7411
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pStream) {
5,868✔
7412
  CHECK_PARSER_STATUS(pCxt);
5,868✔
7413
  SPauseStreamStmt* pStmt = NULL;
5,868✔
7414
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
5,868✔
7415
  CHECK_MAKE_NODE(pStmt);
5,868✔
7416
  if (pStream) {
5,868✔
7417
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
5,868✔
7418
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
5,868✔
7419
  } else {
UNCOV
7420
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7421
    goto _err;
×
7422
  }
7423
  nodesDestroyNode(pStream);
5,868✔
7424
  pStmt->ignoreNotExists = ignoreNotExists;
5,868✔
7425
  return (SNode*)pStmt;
5,868✔
UNCOV
7426
_err:
×
UNCOV
7427
  return NULL;
×
7428
}
7429

7430
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated, SNode* pStream) {
5,164✔
7431
  CHECK_PARSER_STATUS(pCxt);
5,164✔
7432
  SResumeStreamStmt* pStmt = NULL;
5,164✔
7433
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
5,164✔
7434
  CHECK_MAKE_NODE(pStmt);
5,164✔
7435
  if (pStream) {
5,164✔
7436
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
5,164✔
7437
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
5,164✔
7438
  } else {
UNCOV
7439
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7440
    goto _err;
×
7441
  }
7442
  nodesDestroyNode(pStream);
5,164✔
7443
  pStmt->ignoreNotExists = ignoreNotExists;
5,164✔
7444
  pStmt->ignoreUntreated = ignoreUntreated;
5,164✔
7445
  return (SNode*)pStmt;
5,164✔
UNCOV
7446
_err:
×
UNCOV
7447
  return NULL;
×
7448
}
7449

7450
SNode* createRecalcStreamStmt(SAstCreateContext* pCxt, SNode* pStream, SNode* pRange) {
23,472✔
7451
  CHECK_PARSER_STATUS(pCxt);
23,472✔
7452
  SRecalcStreamStmt* pStmt = NULL;
23,472✔
7453
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RECALCULATE_STREAM_STMT, (SNode**)&pStmt);
23,472✔
7454
  CHECK_MAKE_NODE(pStmt);
23,472✔
7455
  if (pStream) {
23,472✔
7456
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
23,472✔
7457
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
23,472✔
7458
  } else {
UNCOV
7459
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7460
    goto _err;
×
7461
  }
7462
  pStmt->pRange = pRange;
23,472✔
7463
  return (SNode*)pStmt;
23,472✔
UNCOV
7464
_err:
×
UNCOV
7465
  return NULL;
×
7466
}
7467

7468
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
5,160✔
7469
  CHECK_PARSER_STATUS(pCxt);
5,160✔
7470
  SKillStmt* pStmt = NULL;
5,160✔
7471
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
5,160✔
7472
  CHECK_MAKE_NODE(pStmt);
5,160✔
7473
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
5,160✔
7474
  return (SNode*)pStmt;
5,160✔
7475
_err:
×
7476
  return NULL;
×
7477
}
7478

7479
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
264✔
7480
  CHECK_PARSER_STATUS(pCxt);
264✔
7481
  SKillQueryStmt* pStmt = NULL;
264✔
7482
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
264✔
7483
  CHECK_MAKE_NODE(pStmt);
264✔
7484
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
264✔
7485
  return (SNode*)pStmt;
264✔
UNCOV
7486
_err:
×
UNCOV
7487
  return NULL;
×
7488
}
7489

7490
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
24,246✔
7491
  CHECK_PARSER_STATUS(pCxt);
24,246✔
7492
  SBalanceVgroupStmt* pStmt = NULL;
24,246✔
7493
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
24,246✔
7494
  CHECK_MAKE_NODE(pStmt);
24,246✔
7495
  return (SNode*)pStmt;
24,246✔
UNCOV
7496
_err:
×
UNCOV
7497
  return NULL;
×
7498
}
7499

7500
SNode* createAssignLeaderStmt(SAstCreateContext* pCxt) {
34✔
7501
  CHECK_PARSER_STATUS(pCxt);
34✔
7502
  SAssignLeaderStmt* pStmt = NULL;
34✔
7503
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ASSIGN_LEADER_STMT, (SNode**)&pStmt);
34✔
7504
  CHECK_MAKE_NODE(pStmt);
34✔
7505
  return (SNode*)pStmt;
34✔
UNCOV
7506
_err:
×
UNCOV
7507
  return NULL;
×
7508
}
7509

7510
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
5,636✔
7511
  CHECK_PARSER_STATUS(pCxt);
5,636✔
7512
  SBalanceVgroupLeaderStmt* pStmt = NULL;
5,636✔
7513
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
5,636✔
7514
  CHECK_MAKE_NODE(pStmt);
5,636✔
7515
  if (NULL != pVgId && NULL != pVgId->z) {
5,636✔
7516
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
2✔
7517
  }
7518
  return (SNode*)pStmt;
5,636✔
UNCOV
7519
_err:
×
UNCOV
7520
  return NULL;
×
7521
}
7522

7523
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
218✔
7524
  CHECK_PARSER_STATUS(pCxt);
218✔
7525
  SBalanceVgroupLeaderStmt* pStmt = NULL;
218✔
7526
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
218✔
7527
  CHECK_MAKE_NODE(pStmt);
218✔
7528
  if (NULL != pDbName) {
218✔
7529
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
218✔
7530
  }
7531
  return (SNode*)pStmt;
218✔
UNCOV
7532
_err:
×
UNCOV
7533
  return NULL;
×
7534
}
7535

7536
SNode* createSetVgroupKeepVersionStmt(SAstCreateContext* pCxt, const SToken* pVgId, const SToken* pKeepVersion) {
2,268✔
7537
  CHECK_PARSER_STATUS(pCxt);
2,268✔
7538
  SSetVgroupKeepVersionStmt* pStmt = NULL;
2,268✔
7539
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_VGROUP_KEEP_VERSION_STMT, (SNode**)&pStmt);
2,268✔
7540
  CHECK_MAKE_NODE(pStmt);
2,268✔
7541
  if (NULL != pVgId && NULL != pVgId->z) {
2,268✔
7542
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
2,268✔
7543
  }
7544
  if (NULL != pKeepVersion && NULL != pKeepVersion->z) {
2,268✔
7545
    pStmt->keepVersion = taosStr2Int64(pKeepVersion->z, NULL, 10);
2,268✔
7546
  }
7547
  return (SNode*)pStmt;
2,268✔
7548
_err:
×
7549
  return NULL;
×
7550
}
7551

UNCOV
7552
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
×
UNCOV
7553
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
7554
  SMergeVgroupStmt* pStmt = NULL;
×
UNCOV
7555
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
×
UNCOV
7556
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
7557
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
×
UNCOV
7558
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
×
UNCOV
7559
  return (SNode*)pStmt;
×
UNCOV
7560
_err:
×
UNCOV
7561
  return NULL;
×
7562
}
7563

7564
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
93,236✔
7565
  CHECK_PARSER_STATUS(pCxt);
93,236✔
7566
  SRedistributeVgroupStmt* pStmt = NULL;
93,236✔
7567
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
93,236✔
7568
  CHECK_MAKE_NODE(pStmt);
93,236✔
7569
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
93,236✔
7570
  pStmt->pDnodes = pDnodes;
93,236✔
7571
  return (SNode*)pStmt;
93,236✔
7572
_err:
×
7573
  nodesDestroyList(pDnodes);
×
7574
  return NULL;
×
7575
}
7576

7577
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, bool force) {
36,190✔
7578
  CHECK_PARSER_STATUS(pCxt);
36,190✔
7579
  SSplitVgroupStmt* pStmt = NULL;
36,190✔
7580
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
36,190✔
7581
  CHECK_MAKE_NODE(pStmt);
36,190✔
7582
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
36,190✔
7583
  pStmt->force = force;
36,190✔
7584
  return (SNode*)pStmt;
36,190✔
UNCOV
7585
_err:
×
UNCOV
7586
  return NULL;
×
7587
}
7588

7589
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
7590
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
7591
  SNode* pStmt = NULL;
×
UNCOV
7592
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
UNCOV
7593
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
7594
  return pStmt;
×
UNCOV
7595
_err:
×
UNCOV
7596
  return NULL;
×
7597
}
7598

7599
SNode* createGrantStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
1,773,260✔
7600
                       SNode* pCond, int8_t optrType) {
7601
  CHECK_PARSER_STATUS(pCxt);
1,773,260✔
7602
  CHECK_NAME(checkRoleName(pCxt, pPrincipal, false));
1,773,260✔
7603
  SGrantStmt* pStmt = NULL;
1,773,260✔
7604
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
1,773,260✔
7605
  CHECK_MAKE_NODE(pStmt);
1,773,260✔
7606
  pStmt->optrType = optrType;
1,773,260✔
7607
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
1,773,260✔
7608
  switch (optrType) {
1,773,260✔
7609
    case TSDB_ALTER_ROLE_LOCK:
×
7610
      break;
×
7611
    case TSDB_ALTER_ROLE_PRIVILEGES: {
1,760,944✔
7612
      CHECK_NAME(checkObjName(pCxt, &pPrivLevel->first, false));
1,760,944✔
7613
      CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
1,760,944✔
7614
      pStmt->privileges = *(SPrivSetArgs*)resouces;
1,760,944✔
7615
      if(pStmt->privileges.nPrivArgs <= 0) {
1,760,944✔
UNCOV
7616
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Unknown privilege type");
×
UNCOV
7617
        goto _err;
×
7618
      }
7619
      if (TK_NK_NIL != pPrivLevel->first.type) {
1,760,944✔
7620
        COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
1,740,882✔
7621
      }
7622
      if (TK_NK_NIL != pPrivLevel->second.type) {
1,760,944✔
7623
        COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
1,169,404✔
7624
      }
7625
      pStmt->privileges.objType = pPrivLevel->objType;
1,760,944✔
7626
      pStmt->pCond = pCond;
1,760,944✔
7627
      break;
1,760,944✔
7628
    }
7629
    case TSDB_ALTER_ROLE_ROLE: {
12,316✔
7630
      SToken* pRole = (SToken*)resouces;
12,316✔
7631
      CHECK_NAME(checkRoleName(pCxt, pRole, false));
12,316✔
7632
      COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
12,316✔
7633
      break;
12,316✔
7634
    }
UNCOV
7635
    default:
×
UNCOV
7636
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported grant type");
×
UNCOV
7637
      goto _err;
×
7638
  }
7639
  return (SNode*)pStmt;
1,773,260✔
UNCOV
7640
_err:
×
UNCOV
7641
  nodesDestroyNode(pCond);
×
UNCOV
7642
  return NULL;
×
7643
}
7644

7645
SNode* createRevokeStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
952,580✔
7646
                        SNode* pCond, int8_t optrType) {
7647
  CHECK_PARSER_STATUS(pCxt);
952,580✔
7648
  CHECK_NAME(checkUserName(pCxt, pPrincipal));
952,580✔
7649
  SRevokeStmt* pStmt = NULL;
952,580✔
7650
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
952,580✔
7651
  CHECK_MAKE_NODE(pStmt);
952,580✔
7652
  pStmt->optrType = optrType;
952,580✔
7653
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
952,580✔
7654
  if (optrType == TSDB_ALTER_ROLE_PRIVILEGES) {
952,580✔
7655
    CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
942,186✔
7656
    CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
942,186✔
7657
    pStmt->privileges = *(SPrivSetArgs*)resouces;
942,186✔
7658
    if (TK_NK_NIL != pPrivLevel->first.type) {
942,186✔
7659
      COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
931,758✔
7660
    }
7661
    if (TK_NK_NIL != pPrivLevel->second.type) {
942,186✔
7662
      COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
836,544✔
7663
    }
7664
    pStmt->privileges.objType = pPrivLevel->objType;
942,186✔
7665
    pStmt->pCond = pCond;
942,186✔
7666
  } else if (optrType == TSDB_ALTER_ROLE_ROLE) {
10,394✔
7667
    SToken* pRole = (SToken*)resouces;
10,394✔
7668
    CHECK_NAME(checkRoleName(pCxt, pRole, false));
10,394✔
7669
    COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
10,394✔
7670
  } else {
UNCOV
7671
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported revoke type");
×
UNCOV
7672
    goto _err;
×
7673
  }
7674

7675
  return (SNode*)pStmt;
952,580✔
UNCOV
7676
_err:
×
UNCOV
7677
  nodesDestroyNode(pCond);
×
UNCOV
7678
  return NULL;
×
7679
}
7680

7681
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
10,884,150✔
7682
  SFunctionNode* pFunc = NULL;
10,884,150✔
7683
  CHECK_PARSER_STATUS(pCxt);
10,884,150✔
7684
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
10,884,150✔
7685
  CHECK_MAKE_NODE(pFunc);
10,884,150✔
7686
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
10,884,150✔
7687
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
10,884,150✔
7688
  CHECK_MAKE_NODE(pCol);
10,884,150✔
7689
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
10,884,150✔
7690
  CHECK_PARSER_STATUS(pCxt);
10,884,150✔
7691
  return (SNode*)pFunc;
10,884,150✔
7692
_err:
×
7693
  nodesDestroyNode((SNode*)pFunc);
×
7694
  return NULL;
×
7695
}
7696

7697
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
3,628,050✔
7698
  SDeleteStmt* pStmt = NULL;
3,628,050✔
7699
  CHECK_PARSER_STATUS(pCxt);
3,628,050✔
7700
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
3,628,050✔
7701
  CHECK_MAKE_NODE(pStmt);
3,628,024✔
7702
  pStmt->pFromTable = pTable;
3,628,024✔
7703
  pStmt->pWhere = pWhere;
3,628,024✔
7704
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
3,628,024✔
7705
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
3,628,050✔
7706
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
3,628,050✔
7707
  CHECK_MAKE_NODE(pStmt->pCountFunc);
3,628,050✔
7708
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
3,628,050✔
7709
  CHECK_MAKE_NODE(pStmt->pLastFunc);
3,628,050✔
7710
  pStmt->secureDelete = 0;
3,628,050✔
7711
  return (SNode*)pStmt;
3,628,050✔
UNCOV
7712
_err:
×
UNCOV
7713
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
7714
  nodesDestroyNode(pTable);
×
UNCOV
7715
  nodesDestroyNode(pWhere);
×
UNCOV
7716
  return NULL;
×
7717
}
7718

7719
SNode* createSecureDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
2,700✔
7720
  SNode* pNode = createDeleteStmt(pCxt, pTable, pWhere);
2,700✔
7721
  if (pNode) {
2,700✔
7722
    ((SDeleteStmt*)pNode)->secureDelete = 1;
2,700✔
7723
  }
7724
  return pNode;
2,700✔
7725
}
7726

7727
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
689,492✔
7728
  CHECK_PARSER_STATUS(pCxt);
689,492✔
7729
  SInsertStmt* pStmt = NULL;
689,492✔
7730
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
689,492✔
7731
  CHECK_MAKE_NODE(pStmt);
689,492✔
7732
  pStmt->pTable = pTable;
689,492✔
7733
  pStmt->pCols = pCols;
689,492✔
7734
  pStmt->pQuery = pQuery;
689,492✔
7735
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
689,492✔
7736
    tstrncpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
689,298✔
7737
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
194✔
7738
    tstrncpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
194✔
7739
  }
7740
  return (SNode*)pStmt;
689,492✔
7741
_err:
×
UNCOV
7742
  nodesDestroyNode(pTable);
×
UNCOV
7743
  nodesDestroyNode(pQuery);
×
UNCOV
7744
  nodesDestroyList(pCols);
×
UNCOV
7745
  return NULL;
×
7746
}
7747

7748
SNode* createCreateRsmaStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* rsmaName, SNode* pRealTable,
225,612✔
7749
                            SNodeList* pFuncs, SNodeList* pIntervals) {
7750
  SCreateRsmaStmt* pStmt = NULL;
225,612✔
7751

7752
  CHECK_PARSER_STATUS(pCxt);
225,612✔
7753
  CHECK_NAME(checkRsmaName(pCxt, rsmaName));
225,612✔
7754
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_RSMA_STMT, (SNode**)&pStmt);
225,612✔
7755
  CHECK_MAKE_NODE(pStmt);
225,612✔
7756

7757
  pStmt->ignoreExists = ignoreExists;
225,612✔
7758
  pStmt->pFuncs = pFuncs;
225,612✔
7759
  pStmt->pIntervals = pIntervals;
225,612✔
7760
  COPY_STRING_FORM_ID_TOKEN(pStmt->rsmaName, rsmaName);
225,612✔
7761

7762
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
225,612✔
7763
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
225,612✔
7764
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
225,612✔
7765
  nodesDestroyNode(pRealTable);
225,612✔
7766

7767
  return (SNode*)pStmt;
225,612✔
7768
_err:
×
7769
  nodesDestroyNode((SNode*)pStmt);
×
7770
  nodesDestroyList(pFuncs);
×
UNCOV
7771
  nodesDestroyNode(pRealTable);
×
UNCOV
7772
  nodesDestroyList(pIntervals);
×
UNCOV
7773
  return NULL;
×
7774
}
7775

7776
SNode* createDropRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
20,426✔
7777
  CHECK_PARSER_STATUS(pCxt);
20,426✔
7778
  SDropRsmaStmt* pStmt = NULL;
20,426✔
7779
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_RSMA_STMT, (SNode**)&pStmt);
20,426✔
7780
  CHECK_MAKE_NODE(pStmt);
20,426✔
7781

7782
  pStmt->ignoreNotExists = ignoreNotExists;
20,426✔
7783
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
20,426✔
7784

7785
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
20,426✔
7786
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
20,426✔
7787

7788
  nodesDestroyNode(pRealTable);
20,426✔
7789
  return (SNode*)pStmt;
20,426✔
UNCOV
7790
_err:
×
UNCOV
7791
  nodesDestroyNode(pRealTable);
×
UNCOV
7792
  return NULL;
×
7793
}
7794

7795
SNode* createAlterRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRsma, int8_t alterType,
41,814✔
7796
                           void* alterInfo) {
7797
  CHECK_PARSER_STATUS(pCxt);
41,814✔
7798
  SAlterRsmaStmt* pStmt = NULL;
41,814✔
7799
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_RSMA_STMT, (SNode**)&pStmt);
41,814✔
7800
  CHECK_MAKE_NODE(pStmt);
41,814✔
7801
  pStmt->ignoreNotExists = ignoreNotExists;
41,814✔
7802
  SRealTableNode* pTableNode = (SRealTableNode*)pRsma;
41,814✔
7803

7804
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
41,814✔
7805
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
41,814✔
7806
  nodesDestroyNode(pRsma);
41,814✔
7807

7808
  pStmt->alterType = alterType;
41,814✔
7809
  switch (alterType) {
41,814✔
7810
    case TSDB_ALTER_RSMA_FUNCTION: {
41,814✔
7811
      pStmt->pFuncs = (SNodeList*)alterInfo;
41,814✔
7812
      break;
41,814✔
7813
    }
UNCOV
7814
    default:
×
7815
      break;
×
7816
  }
7817
  return (SNode*)pStmt;
41,814✔
UNCOV
7818
_err:
×
UNCOV
7819
  return NULL;
×
7820
}
7821

7822
SNode* createShowCreateRsmaStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
6,318✔
7823
  CHECK_PARSER_STATUS(pCxt);
6,318✔
7824
  SShowCreateRsmaStmt* pStmt = NULL;
6,318✔
7825
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
6,318✔
7826
  CHECK_MAKE_NODE(pStmt);
6,318✔
7827
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
6,318✔
7828
  tstrncpy(pStmt->rsmaName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->rsmaName));
6,318✔
7829
  nodesDestroyNode(pRealTable);
6,318✔
7830
  return (SNode*)pStmt;
6,318✔
UNCOV
7831
_err:
×
UNCOV
7832
  nodesDestroyNode(pRealTable);
×
UNCOV
7833
  return NULL;
×
7834
}
7835

7836
SNode* createRollupStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
19,898✔
7837
  CHECK_PARSER_STATUS(pCxt);
19,898✔
7838
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
19,898✔
7839
  SRollupDatabaseStmt* pStmt = NULL;
19,898✔
7840
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_DATABASE_STMT, (SNode**)&pStmt);
19,898✔
7841
  CHECK_MAKE_NODE(pStmt);
19,898✔
7842
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
19,898✔
7843
  pStmt->pStart = pStart;
19,898✔
7844
  pStmt->pEnd = pEnd;
19,898✔
7845
  return (SNode*)pStmt;
19,898✔
UNCOV
7846
_err:
×
UNCOV
7847
  nodesDestroyNode(pStart);
×
UNCOV
7848
  nodesDestroyNode(pEnd);
×
UNCOV
7849
  return NULL;
×
7850
}
7851

7852
SNode* createRollupVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
8,940✔
7853
                                SNode* pEnd) {
7854
  CHECK_PARSER_STATUS(pCxt);
8,940✔
7855
  if (NULL == pDbName) {
8,940✔
7856
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
7857
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
7858
    CHECK_PARSER_STATUS(pCxt);
×
7859
  }
7860
  SRollupVgroupsStmt* pStmt = NULL;
8,940✔
7861
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_VGROUPS_STMT, (SNode**)&pStmt);
8,940✔
7862
  CHECK_MAKE_NODE(pStmt);
8,940✔
7863
  pStmt->pDbName = pDbName;
8,940✔
7864
  pStmt->vgidList = vgidList;
8,940✔
7865
  pStmt->pStart = pStart;
8,940✔
7866
  pStmt->pEnd = pEnd;
8,940✔
7867
  return (SNode*)pStmt;
8,940✔
UNCOV
7868
_err:
×
UNCOV
7869
  nodesDestroyNode(pDbName);
×
UNCOV
7870
  nodesDestroyList(vgidList);
×
7871
  nodesDestroyNode(pStart);
×
7872
  nodesDestroyNode(pEnd);
×
7873
  return NULL;
×
7874
}
7875

7876
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
14,656✔
7877
                            SNode* pRealTable, SNode* pInterval) {
7878
  SCreateTSMAStmt* pStmt = NULL;
14,656✔
7879

7880
  CHECK_PARSER_STATUS(pCxt);
14,656✔
7881
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
14,656✔
7882
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
14,188✔
7883
  CHECK_MAKE_NODE(pStmt);
14,188✔
7884

7885
  pStmt->ignoreExists = ignoreExists;
14,188✔
7886
  if (!pOptions) {
14,188✔
7887
    // recursive tsma
7888
    pStmt->pOptions = NULL;
2,106✔
7889
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
2,106✔
7890
    CHECK_MAKE_NODE(pStmt->pOptions);
2,106✔
7891
    pStmt->pOptions->recursiveTsma = true;
2,106✔
7892
  } else {
7893
    pStmt->pOptions = (STSMAOptions*)pOptions;
12,082✔
7894
  }
7895
  pStmt->pOptions->pInterval = pInterval;
14,188✔
7896
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
14,188✔
7897

7898
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
14,188✔
7899
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
14,188✔
7900
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
14,188✔
7901
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
14,188✔
7902
  nodesDestroyNode(pRealTable);
14,188✔
7903

7904
  return (SNode*)pStmt;
14,188✔
7905
_err:
468✔
7906
  nodesDestroyNode((SNode*)pStmt);
468✔
7907
  nodesDestroyNode(pOptions);
468✔
7908
  nodesDestroyNode(pRealTable);
468✔
7909
  nodesDestroyNode(pInterval);
468✔
7910
  return NULL;
468✔
7911
}
7912

7913
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
13,720✔
7914
  CHECK_PARSER_STATUS(pCxt);
13,720✔
7915
  STSMAOptions* pOptions = NULL;
13,720✔
7916
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
13,720✔
7917
  CHECK_MAKE_NODE(pOptions);
13,720✔
7918
  pOptions->pFuncs = pFuncs;
13,720✔
7919
  return (SNode*)pOptions;
13,720✔
UNCOV
7920
_err:
×
UNCOV
7921
  nodesDestroyList(pFuncs);
×
UNCOV
7922
  return NULL;
×
7923
}
7924

UNCOV
7925
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
UNCOV
7926
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
7927
  STSMAOptions* pOptions = NULL;
×
UNCOV
7928
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
UNCOV
7929
  CHECK_MAKE_NODE(pOptions);
×
UNCOV
7930
  return (SNode*)pOptions;
×
UNCOV
7931
_err:
×
UNCOV
7932
  return NULL;
×
7933
}
7934

7935
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
5,238✔
7936
  CHECK_PARSER_STATUS(pCxt);
5,238✔
7937
  SDropTSMAStmt* pStmt = NULL;
5,238✔
7938
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
5,238✔
7939
  CHECK_MAKE_NODE(pStmt);
5,238✔
7940

7941
  pStmt->ignoreNotExists = ignoreNotExists;
5,238✔
7942
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
5,238✔
7943

7944
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
5,238✔
7945
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
5,238✔
7946

7947
  nodesDestroyNode(pRealTable);
5,238✔
7948
  return (SNode*)pStmt;
5,238✔
UNCOV
7949
_err:
×
7950
  nodesDestroyNode(pRealTable);
×
7951
  return NULL;
×
7952
}
7953

7954
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
264✔
7955
  CHECK_PARSER_STATUS(pCxt);
264✔
7956

7957
  SShowStmt* pStmt = NULL;
264✔
7958
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
264✔
7959
  CHECK_MAKE_NODE(pStmt);
264✔
7960

7961
  pStmt->pDbName = dbName;
264✔
7962
  return (SNode*)pStmt;
264✔
UNCOV
7963
_err:
×
UNCOV
7964
  nodesDestroyNode(dbName);
×
UNCOV
7965
  return NULL;
×
7966
}
7967
SNode* createShowDiskUsageStmt(SAstCreateContext* pCxt, SNode* dbName, ENodeType type) {
648✔
7968
  CHECK_PARSER_STATUS(pCxt);
648✔
7969
  if (NULL == dbName) {
648✔
UNCOV
7970
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
UNCOV
7971
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
7972
    CHECK_PARSER_STATUS(pCxt);
×
7973
  }
7974

7975
  SShowStmt* pStmt = NULL;
648✔
7976
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
648✔
7977
  CHECK_MAKE_NODE(pStmt);
648✔
7978

7979
  pStmt->pDbName = dbName;
648✔
7980
  return (SNode*)pStmt;
648✔
UNCOV
7981
_err:
×
UNCOV
7982
  nodesDestroyNode(dbName);
×
UNCOV
7983
  return NULL;
×
7984
}
7985

7986
SNode* createShowStreamsStmt(SAstCreateContext* pCxt, SNode* pDbName, ENodeType type) {
227,266✔
7987
  CHECK_PARSER_STATUS(pCxt);
227,266✔
7988

7989
  if (needDbShowStmt(type) && NULL == pDbName) {
227,266✔
7990
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
468✔
7991
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
468✔
7992
    CHECK_PARSER_STATUS(pCxt);
468✔
7993
  }
7994

7995
  SShowStmt* pStmt = NULL;
226,798✔
7996
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
226,798✔
7997
  CHECK_MAKE_NODE(pStmt);
226,798✔
7998
  pStmt->withFull = false;
226,798✔
7999
  pStmt->pDbName = pDbName;
226,798✔
8000

8001
  return (SNode*)pStmt;
226,798✔
8002

8003
_err:
468✔
8004
  nodesDestroyNode(pDbName);
468✔
8005
  return NULL;
468✔
8006
}
8007

8008
SNode* createScanStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
1,776✔
8009
  CHECK_PARSER_STATUS(pCxt);
1,776✔
8010
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,776✔
8011
  SScanDatabaseStmt* pStmt = NULL;
1,776✔
8012
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_DATABASE_STMT, (SNode**)&pStmt);
1,776✔
8013
  CHECK_MAKE_NODE(pStmt);
1,776✔
8014
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,776✔
8015
  pStmt->pStart = pStart;
1,776✔
8016
  pStmt->pEnd = pEnd;
1,776✔
8017
  return (SNode*)pStmt;
1,776✔
UNCOV
8018
_err:
×
UNCOV
8019
  nodesDestroyNode(pStart);
×
UNCOV
8020
  nodesDestroyNode(pEnd);
×
UNCOV
8021
  return NULL;
×
8022
}
8023

8024
SNode* createScanVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart, SNode* pEnd) {
576✔
8025
  CHECK_PARSER_STATUS(pCxt);
576✔
8026
  if (NULL == pDbName) {
576✔
8027
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
96✔
8028
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
96✔
8029
    CHECK_PARSER_STATUS(pCxt);
96✔
8030
  }
8031
  SScanVgroupsStmt* pStmt = NULL;
480✔
8032
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_VGROUPS_STMT, (SNode**)&pStmt);
480✔
8033
  CHECK_MAKE_NODE(pStmt);
480✔
8034
  pStmt->pDbName = pDbName;
480✔
8035
  pStmt->vgidList = vgidList;
480✔
8036
  pStmt->pStart = pStart;
480✔
8037
  pStmt->pEnd = pEnd;
480✔
8038
  return (SNode*)pStmt;
480✔
8039
_err:
96✔
8040
  nodesDestroyNode(pDbName);
96✔
8041
  nodesDestroyList(vgidList);
96✔
8042
  nodesDestroyNode(pStart);
96✔
8043
  nodesDestroyNode(pEnd);
96✔
8044
  return NULL;
96✔
8045
}
8046

8047
SNode* createShowScansStmt(SAstCreateContext* pCxt, ENodeType type) {
4,320✔
8048
  CHECK_PARSER_STATUS(pCxt);
4,320✔
8049
  SShowScansStmt* pStmt = NULL;
4,320✔
8050
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
4,320✔
8051
  CHECK_MAKE_NODE(pStmt);
4,320✔
8052
  return (SNode*)pStmt;
4,320✔
UNCOV
8053
_err:
×
UNCOV
8054
  return NULL;
×
8055
}
8056

8057
SNode* createShowScanDetailsStmt(SAstCreateContext* pCxt, SNode* pScanIdNode) {
2,880✔
8058
  CHECK_PARSER_STATUS(pCxt);
2,880✔
8059
  SShowScanDetailsStmt* pStmt = NULL;
2,880✔
8060
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_SCAN_DETAILS_STMT, (SNode**)&pStmt);
2,880✔
8061
  CHECK_MAKE_NODE(pStmt);
2,880✔
8062
  pStmt->pScanId = pScanIdNode;
2,880✔
8063
  return (SNode*)pStmt;
2,880✔
UNCOV
8064
_err:
×
UNCOV
8065
  nodesDestroyNode(pScanIdNode);
×
UNCOV
8066
  return NULL;
×
8067
}
8068

UNCOV
8069
SNode* createAlterAllDnodeTLSStmt(SAstCreateContext* pCxt, SToken* alterName) {
×
UNCOV
8070
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
8071
  SAlterDnodeStmt* pStmt = NULL;
×
8072
  static char*     tls = "TLS";
UNCOV
8073
  if (NULL == alterName || alterName->n <= 0) {
×
UNCOV
8074
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter name is empty");
×
UNCOV
8075
    goto _err;
×
8076
  }
8077

8078
  if (alterName->n == strlen(tls) && taosStrncasecmp(alterName->z, tls, alterName->n) == 0) {
×
8079
    pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODES_RELOAD_TLS_STMT, (SNode**)&pStmt);
×
8080

UNCOV
8081
    memcpy(pStmt->config, "reload", strlen("reload"));
×
UNCOV
8082
    memcpy(pStmt->value, "tls", strlen("tls"));
×
UNCOV
8083
    pStmt->dnodeId = -1;
×
8084
  } else {
UNCOV
8085
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter is not supported");
×
UNCOV
8086
    goto _err;
×
8087
  }
8088

8089
  CHECK_MAKE_NODE(pStmt);
×
8090

8091
  return (SNode*)pStmt;
×
UNCOV
8092
_err:
×
UNCOV
8093
  return NULL;
×
8094
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc