• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

taosdata / TDengine / #5034

24 Apr 2026 11:25AM UTC coverage: 73.058%. Remained the same
#5034

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1336 of 1975 new or added lines in 48 files covered. (67.65%)

14149 existing lines in 164 files now uncovered.

275896 of 377640 relevant lines covered (73.06%)

132944440.29 hits per line

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

73.44
/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
#include "tcompare.h"
37

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

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

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

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

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

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

100
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
498,436,160✔
101
  memset(pCxt, 0, sizeof(SAstCreateContext));
498,436,160✔
102
  pCxt->pQueryCxt = pParseCxt;
498,436,160✔
103
  pCxt->msgBuf.buf = pParseCxt->pMsg;
498,437,169✔
104
  pCxt->msgBuf.len = pParseCxt->msgLen;
498,425,952✔
105
  pCxt->notSupport = false;
498,441,377✔
106
  pCxt->pRootNode = NULL;
498,435,842✔
107
  pCxt->placeholderNo = 0;
498,436,244✔
108
  pCxt->pPlaceholderValues = NULL;
498,434,066✔
109
  pCxt->errCode = TSDB_CODE_SUCCESS;
498,441,976✔
110
}
498,441,433✔
111

112
static void trimEscape(SAstCreateContext* pCxt, SToken* pName, bool trimStar) {
2,147,483,647✔
113
  // todo need to deal with `ioo``ii` -> ioo`ii: done
114
  if (NULL != pName && pName->n > 1 && TS_ESCAPE_CHAR == pName->z[0]) {
2,147,483,647✔
115
    if (!pCxt->pQueryCxt->hasDupQuoteChar) {
14,292,647✔
116
      pName->z += 1;
14,180,081✔
117
      pName->n -= 2;
14,180,571✔
118
      // * is forbidden as an identifier name
119
      if (pName->z[0] == '*' && trimStar && pName->n == 1) {
14,180,408✔
120
        pName->z[0] = '\0';
×
121
        pName->n = 0;
×
122
      }
123
    } else {
124
      int32_t i = 1, j = 0;
111,957✔
125
      for (; i < pName->n - 1; ++i) {
685,110✔
126
        if ((pName->z[i] == TS_ESCAPE_CHAR) && (pName->z[i + 1] == TS_ESCAPE_CHAR)) {
573,153✔
127
          pName->z[j++] = TS_ESCAPE_CHAR;
270,145✔
128
          ++i;
270,145✔
129
        } else {
130
          pName->z[j++] = pName->z[i];
303,008✔
131
        }
132
      }
133
      pName->n = j;
111,957✔
134
    }
135
  }
136
}
2,147,483,647✔
137

138
static bool checkUserName(SAstCreateContext* pCxt, SToken* pUserName) {
814,497✔
139
  if (NULL == pUserName) {
814,497✔
140
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
141
  } else {
142
    if (pUserName->n >= TSDB_USER_LEN) {
814,497✔
143
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
2,284✔
144
    }
145
  }
146
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
814,497✔
147
    trimEscape(pCxt, pUserName, true);
812,213✔
148
  }
149
  return TSDB_CODE_SUCCESS == pCxt->errCode;
814,497✔
150
}
151

152

153

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

164

165

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

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

179
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
182,093✔
180
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
182,093✔
181
  (void)strdequote(ep);
182,093✔
182
  (void)strtrim(ep);
182,093✔
183
  if (NULL == pPort) {
182,093✔
184
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
173,884✔
185
    return TSDB_CODE_SUCCESS;
173,884✔
186
  }
187
  char* pColon = strrchr(ep, ':');
8,209✔
188
  if (NULL == pColon) {
8,209✔
189
    *pPort = tsServerPort;
3,980✔
190
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
3,980✔
191
    return TSDB_CODE_SUCCESS;
3,980✔
192
  }
193
  TAOS_STRNCPY(pFqdn, ep, pColon - ep);
4,229✔
194
  return parsePort(pCxt, pColon + 1, pPort);
4,229✔
195
}
196

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

204
  if (NULL != pPortToken) {
182,093✔
205
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
173,884✔
206
  }
207

208
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
182,093✔
209
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
182,093✔
210
  }
211

212
  return TSDB_CODE_SUCCESS == pCxt->errCode;
182,093✔
213
}
214

215
static bool checkObjName(SAstCreateContext* pCxt, SToken* pObjName, bool need) {
964,507✔
216
  if (NULL == pObjName || TK_NK_NIL == pObjName->type) {
964,507✔
217
    if (need) {
12,442✔
218
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME);
×
219
    }
220
  } else {
221
    trimEscape(pCxt, pObjName, true);
952,065✔
222
    if (pObjName->n >= TSDB_OBJ_NAME_LEN || pObjName->n == 0) {
952,065✔
223
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pObjName->z);
×
224
    }
225
  }
226
  return TSDB_CODE_SUCCESS == pCxt->errCode;
964,507✔
227
}
228

229
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
1,019,455,459✔
230
  if (NULL == pDbName) {
1,019,455,459✔
231
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
721,499,670✔
232
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
9,605✔
233
    }
234
  } else {
235
    trimEscape(pCxt, pDbName, true);
297,955,789✔
236
    if (pDbName->n >= TSDB_DB_NAME_LEN || (demandDb && (pDbName->n == 0))) {
297,993,301✔
237
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
23,394✔
238
    }
239
  }
240
  return TSDB_CODE_SUCCESS == pCxt->errCode;
1,019,470,864✔
241
}
242

243
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
2,147,483,647✔
244
  trimEscape(pCxt, pTableName, true);
2,147,483,647✔
245
  if (NULL != pTableName && pTableName->type != TK_NK_NIL &&
2,147,483,647✔
246
      (pTableName->n >= TSDB_TABLE_NAME_LEN || pTableName->n == 0)) {
1,452,742,147✔
247
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
77,751✔
248
    return false;
33,280✔
249
  }
250
  return true;
2,147,483,647✔
251
}
252

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

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

272
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
340,972✔
273
  trimEscape(pCxt, pTopicName, true);
340,972✔
274
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN || pTopicName->n == 0) {
340,972✔
275
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
824✔
276
    return false;
824✔
277
  }
278
  return true;
340,148✔
279
}
280

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

290
static bool checkViewName(SAstCreateContext* pCxt, SToken* pViewName) {
407,884✔
291
  trimEscape(pCxt, pViewName, true);
407,884✔
292
  if (pViewName->n >= TSDB_VIEW_NAME_LEN || pViewName->n == 0) {
407,884✔
293
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pViewName->z);
831✔
294
    return false;
831✔
295
  }
296
  return true;
407,053✔
297
}
298

299
static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
606,129✔
300
  trimEscape(pCxt, pStreamName, true);
606,129✔
301
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN || pStreamName->n == 0) {
606,129✔
302
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
218✔
303
    return false;
218✔
304
  }
305
  return true;
605,911✔
306
}
307

308
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
58,506✔
309
  if (NULL == pCommentToken) {
58,506✔
310
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
×
311
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
58,506✔
312
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
9,152✔
313
  }
314
  return TSDB_CODE_SUCCESS == pCxt->errCode;
58,506✔
315
}
316

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

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

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

350
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
1,902,416,255✔
351
  CHECK_PARSER_STATUS(pCxt);
1,902,416,255✔
352
  SRawExprNode* target = NULL;
1,902,411,557✔
353
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
1,902,413,760✔
354
  CHECK_MAKE_NODE(target);
1,902,464,906✔
355
  target->p = pToken->z;
1,902,464,906✔
356
  target->n = pToken->n;
1,902,462,790✔
357
  target->pNode = pNode;
1,902,461,521✔
358
  return (SNode*)target;
1,902,461,511✔
359
_err:
7,290✔
360
  nodesDestroyNode(pNode);
7,290✔
361
  return NULL;
7,290✔
362
}
363

364
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
2,053,602,226✔
365
  CHECK_PARSER_STATUS(pCxt);
2,053,602,226✔
366
  SRawExprNode* target = NULL;
2,053,604,668✔
367
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
2,053,606,438✔
368
  CHECK_MAKE_NODE(target);
2,053,682,487✔
369
  target->p = pStart->z;
2,053,682,487✔
370
  target->n = (pEnd->z + pEnd->n) - pStart->z;
2,053,681,191✔
371
  target->pNode = pNode;
2,053,681,384✔
372
  return (SNode*)target;
2,053,680,991✔
373
_err:
×
374
  nodesDestroyNode(pNode);
×
375
  return NULL;
×
376
}
377

378
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
105,663,370✔
379
  CHECK_PARSER_STATUS(pCxt);
105,663,370✔
380
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
105,665,250✔
381
    return pNode;
×
382
  }
383
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
105,666,106✔
384
  return pNode;
105,666,018✔
385
_err:
×
386
  nodesDestroyNode(pNode);
×
387
  return NULL;
×
388
}
389

390
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
2,147,483,647✔
391
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
392
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,147,483,647✔
393
  SNode*        pRealizedExpr = pRawExpr->pNode;
2,147,483,647✔
394
  if (nodesIsExprNode(pRealizedExpr)) {
2,147,483,647✔
395
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
2,147,483,647✔
396
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
2,147,483,647✔
397
      tstrncpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
1,483,412,895✔
398
      tstrncpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
1,483,412,684✔
399
    } else if (pRawExpr->isPseudoColumn) {
2,147,483,647✔
400
      // all pseudo column are translate to function with same name
401
      tstrncpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
91,855,112✔
402
      if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_column") == 0) {
91,854,023✔
403
        SValueNode* pColId = (SValueNode*)nodesListGetNode(((SFunctionNode*)pExpr)->pParameterList, 0);
51,902✔
404
        snprintf(pExpr->userAlias, sizeof(pExpr->userAlias), "%%%%%s", pColId->literal);
51,902✔
405
      } else if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_tbname") == 0) {
91,800,618✔
406
        tstrncpy(pExpr->userAlias, "%%tbname", TSDB_COL_NAME_LEN);
44,971✔
407
      } else {
408
        tstrncpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
91,755,735✔
409
      }
410
    } else {
411
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
2,147,483,647✔
412

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

430
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
1,311,889,325✔
431
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
1,311,889,325✔
432
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
501✔
433
    return nil_token;
×
434
  }
435
  SRawExprNode* target = (SRawExprNode*)pNode;
1,311,906,760✔
436
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
1,311,906,760✔
437
  return t;
1,311,908,088✔
438
}
439

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

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

467
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
1,648,116,543✔
468
  CHECK_PARSER_STATUS(pCxt);
1,648,116,543✔
469
  SNodeList* list = NULL;
1,648,106,648✔
470
  pCxt->errCode = nodesMakeList(&list);
1,648,111,344✔
471
  CHECK_MAKE_NODE(list);
1,648,184,467✔
472
  pCxt->errCode = nodesListAppend(list, pNode);
1,648,184,467✔
473
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
1,648,193,314✔
474
    nodesDestroyList(list);
×
475
    return NULL;
×
476
  }
477
  return list;
1,648,202,262✔
478
_err:
9,882✔
479
  nodesDestroyNode(pNode);
9,882✔
480
  return NULL;
9,882✔
481
}
482

483
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
1,214,056,674✔
484
  CHECK_PARSER_STATUS(pCxt);
1,214,056,674✔
485
  pCxt->errCode = nodesListAppend(pList, pNode);
1,214,059,752✔
486
  return pList;
1,214,074,091✔
487
_err:
1,901✔
488
  nodesDestroyNode(pNode);
1,901✔
489
  nodesDestroyList(pList);
1,901✔
490
  return NULL;
1,901✔
491
}
492

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

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

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

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

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

UNCOV
697
_err:
×
UNCOV
698
  return args;
×
699
}
700

701
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
1,600,120,699✔
702
  CHECK_PARSER_STATUS(pCxt);
1,600,120,699✔
703
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
1,600,120,452✔
704
    return NULL;
408✔
705
  }
706
  SColumnNode* col = NULL;
1,600,150,862✔
707
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
1,600,153,218✔
708
  CHECK_MAKE_NODE(col);
1,600,165,963✔
709
  if (NULL != pTableAlias) {
1,600,165,963✔
710
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
280,791,262✔
711
  }
712
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
1,600,165,963✔
713
  return (SNode*)col;
1,600,162,701✔
UNCOV
714
_err:
×
UNCOV
715
  return NULL;
×
716
}
717

718
/**
719
 * @param type: 1 with mask; 0 without mask
720
 */
721
SNode* createColumnNodeExt(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName, int8_t type) {
163✔
722
  SNode* result = createColumnNode(pCxt, pTableAlias, pColumnName);
163✔
723
  if (result != NULL) {
163✔
724
    if (type == 1) ((SColumnNode*)result)->hasMask = 1;
163✔
725
  }
726
  return result;
163✔
727
}
728

729
SNode* createPlaceHolderColumnNode(SAstCreateContext* pCxt, SNode* pColId) {
51,902✔
730
  CHECK_PARSER_STATUS(pCxt);
51,902✔
731
  SFunctionNode* pFunc = NULL;
51,902✔
732
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
51,902✔
733
  CHECK_PARSER_STATUS(pCxt);
51,902✔
734
  tstrncpy(pFunc->functionName, "_placeholder_column", TSDB_FUNC_NAME_LEN);
51,902✔
735
  ((SValueNode*)pColId)->notReserved = true;
51,902✔
736
  pCxt->errCode = nodesListMakeAppend(&pFunc->pParameterList, pColId);
51,902✔
737
  CHECK_PARSER_STATUS(pCxt);
51,902✔
738
  pFunc->tz = pCxt->pQueryCxt->timezone;
51,902✔
739
  pFunc->charsetCxt = pCxt->pQueryCxt->charsetCxt;
51,902✔
740
  return (SNode*)pFunc;
51,902✔
UNCOV
741
_err:
×
UNCOV
742
  return NULL;
×
743
}
744

745
static void copyValueTrimEscape(char* buf, int32_t bufLen, const SToken* pToken, bool trim) {
569,004,749✔
746
  int32_t len = TMIN(pToken->n, bufLen - 1);
569,004,749✔
747
  if (trim && (pToken->z[0] == TS_ESCAPE_CHAR)) {
569,005,733✔
748
    int32_t i = 1, j = 0;
30,635✔
749
    for (; i < len - 1; ++i) {
172,670✔
750
      buf[j++] = pToken->z[i];
142,035✔
751
      if (pToken->z[i] == TS_ESCAPE_CHAR) {
142,035✔
752
        if (pToken->z[i + 1] == TS_ESCAPE_CHAR) ++i;
71,853✔
753
      }
754
    }
755
    buf[j] = 0;
30,635✔
756
  } else {
757
    tstrncpy(buf, pToken->z, len + 1);
568,975,098✔
758
  }
759
}
569,007,355✔
760

761
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
568,998,311✔
762
  CHECK_PARSER_STATUS(pCxt);
568,998,311✔
763
  SValueNode* val = NULL;
568,999,919✔
764
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
569,000,367✔
765
  CHECK_MAKE_NODE(val);
569,012,856✔
766
  if (!(val->literal = taosMemoryMalloc(pLiteral->n + 1))) {
569,012,856✔
UNCOV
767
    pCxt->errCode = terrno;
×
UNCOV
768
    nodesDestroyNode((SNode*)val);
×
UNCOV
769
    return NULL;
×
770
  }
771
  copyValueTrimEscape(val->literal, pLiteral->n + 1, pLiteral,
569,008,535✔
772
                      pCxt->pQueryCxt->hasDupQuoteChar && (TK_NK_ID == pLiteral->type));
569,008,548✔
773
  if (TK_NK_STRING == pLiteral->type) {
569,004,308✔
774
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
69,847,338✔
775
  }
776
  val->node.resType.type = dataType;
569,010,571✔
777
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
569,008,912✔
778
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
569,007,623✔
779
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
19,473✔
780
  }
781
  val->translate = false;
569,007,623✔
782
  val->tz = pCxt->pQueryCxt->timezone;
569,009,491✔
783
  val->charsetCxt = pCxt->pQueryCxt->charsetCxt;
569,010,992✔
784
  return (SNode*)val;
569,008,153✔
UNCOV
785
_err:
×
UNCOV
786
  return NULL;
×
787
}
788

789
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
153,321,254✔
790
  CHECK_PARSER_STATUS(pCxt);
153,321,254✔
791
  SValueNode* val = NULL;
153,325,193✔
792
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
153,326,561✔
793
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
153,340,200✔
794
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
12✔
UNCOV
795
    goto _exit;
×
796
  }
797
  if (pLiteral) {
153,328,087✔
798
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
152,725,996✔
799
    if (!val->literal) {
152,732,340✔
UNCOV
800
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
UNCOV
801
      goto _exit;
×
802
    }
803
  } else if (pNode) {
602,091✔
804
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
602,091✔
805
    if (!nodesIsExprNode(pRawExpr->pNode)) {
602,091✔
UNCOV
806
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
UNCOV
807
      goto _exit;
×
808
    }
809
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
602,091✔
810
    if (!val->literal) {
602,053✔
811
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
UNCOV
812
      goto _exit;
×
813
    }
814
  } else {
815
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
816
    goto _exit;
×
817
  }
818
  if (!val->literal) {
153,328,980✔
819
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
820
    goto _exit;
×
821
  }
822

823
  val->node.resType.type = dataType;
153,323,907✔
824
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
153,330,425✔
825
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
153,324,823✔
UNCOV
826
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
827
  }
828
_exit:
153,324,823✔
829
  nodesDestroyNode(pNode);
153,323,657✔
830
  if (pCxt->errCode != 0) {
153,314,345✔
UNCOV
831
    nodesDestroyNode((SNode*)val);
×
UNCOV
832
    return NULL;
×
833
  }
834
  return (SNode*)val;
153,322,942✔
835
_err:
×
836
  nodesDestroyNode(pNode);
×
UNCOV
837
  return NULL;
×
838
}
839

840
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
52,879✔
841
                             SNode* pRight) {
842
  SValueNode* val = NULL;
52,879✔
843
  CHECK_PARSER_STATUS(pCxt);
52,879✔
844

845
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
52,879✔
846
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
52,879✔
UNCOV
847
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
UNCOV
848
    goto _exit;
×
849
  }
850
  if (pLiteral) {
52,879✔
851
    if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) {
52,879✔
852
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
UNCOV
853
      goto _exit;
×
854
    }
855
  } else {
856
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
857
    goto _exit;
×
858
  }
859

860
  val->node.resType.type = dataType;
52,879✔
861
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
52,879✔
862
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
52,879✔
UNCOV
863
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
864
  }
865
_exit:
52,879✔
866
  nodesDestroyNode(pLeft);
52,879✔
867
  nodesDestroyNode(pRight);
52,879✔
868
  CHECK_PARSER_STATUS(pCxt);
52,879✔
869
  return (SNode*)val;
52,879✔
UNCOV
870
_err:
×
UNCOV
871
  nodesDestroyNode((SNode*)val);
×
UNCOV
872
  nodesDestroyNode(pLeft);
×
UNCOV
873
  nodesDestroyNode(pRight);
×
874
  return NULL;
×
875
}
876

877
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
9,276,192✔
878
  if (!pHintList) return false;
9,276,192✔
879
  SNode* pNode;
880
  FOREACH(pNode, pHintList) {
1,536✔
881
    SHintNode* pHint = (SHintNode*)pNode;
1,536✔
882
    if (pHint->option == hint) {
1,536✔
883
      return true;
1,536✔
884
    }
885
  }
UNCOV
886
  return false;
×
887
}
888

889
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
9,280,981✔
890
                       int32_t paramNum) {
891
  void* value = NULL;
9,280,981✔
892
  switch (opt) {
9,280,981✔
893
    case HINT_SKIP_TSMA:
4,789✔
894
    case HINT_BATCH_SCAN:
895
    case HINT_NO_BATCH_SCAN: {
896
      if (paramNum > 0) {
4,789✔
897
        return true;
820✔
898
      }
899
      break;
3,969✔
900
    }
901
    case HINT_SORT_FOR_GROUP:
62,464✔
902
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
62,464✔
903
      break;
61,952✔
904
    case HINT_PARTITION_FIRST:
2,048✔
905
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
2,048✔
906
      break;
1,024✔
907
    case HINT_PARA_TABLES_SORT:
9,210,320✔
908
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
9,210,320✔
909
      break;
9,210,320✔
UNCOV
910
    case HINT_SMALLDATA_TS_SORT:
×
UNCOV
911
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
UNCOV
912
      break;
×
913
    case HINT_HASH_JOIN:
1,360✔
914
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
1,360✔
915
      break;
1,360✔
916
    case HINT_WIN_OPTIMIZE_BATCH:
×
UNCOV
917
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_BATCH)) return true;
×
UNCOV
918
      break;
×
UNCOV
919
    case HINT_WIN_OPTIMIZE_SINGLE:
×
920
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_SINGLE)) return true;
×
921
      break;
×
922
    default:
×
923
      return true;
×
924
  }
925

926
  SHintNode* hint = NULL;
9,278,625✔
927
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
9,278,625✔
928
  if (!hint) {
9,278,625✔
UNCOV
929
    return true;
×
930
  }
931
  hint->option = opt;
9,278,625✔
932
  hint->value = value;
9,278,625✔
933

934
  if (NULL == *ppHintList) {
9,278,625✔
935
    pCxt->errCode = nodesMakeList(ppHintList);
9,277,805✔
936
    if (!*ppHintList) {
9,277,805✔
UNCOV
937
      nodesDestroyNode((SNode*)hint);
×
UNCOV
938
      return true;
×
939
    }
940
  }
941

942
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
9,278,625✔
943
  if (pCxt->errCode) {
9,278,625✔
UNCOV
944
    return true;
×
945
  }
946

947
  return false;
9,278,625✔
948
}
949

950
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
801,492,456✔
951
  CHECK_PARSER_STATUS(pCxt);
801,492,456✔
952
  if (NULL == pLiteral || pLiteral->n <= 5) {
801,491,874✔
953
    return NULL;
792,211,663✔
954
  }
955
  SNodeList* pHintList = NULL;
9,280,211✔
956
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
9,279,035✔
957
  if (!hint) return NULL;
9,279,035✔
958
  int32_t     i = 0;
9,279,035✔
959
  bool        quit = false;
9,279,035✔
960
  bool        inParamList = false;
9,279,035✔
961
  bool        lastComma = false;
9,279,035✔
962
  EHintOption opt = 0;
9,279,035✔
963
  int32_t     paramNum = 0;
9,279,035✔
964
  SToken      paramList[10];
9,279,035✔
965
  while (!quit) {
55,680,609✔
966
    SToken t0 = {0};
55,677,843✔
967
    if (hint[i] == 0) {
55,677,843✔
968
      break;
9,276,269✔
969
    }
970
    t0.n = tGetToken(&hint[i], &t0.type, NULL);
46,401,574✔
971
    t0.z = hint + i;
46,401,574✔
972
    i += t0.n;
46,401,574✔
973

974
    switch (t0.type) {
46,401,574✔
975
      case TK_BATCH_SCAN:
2,460✔
976
        lastComma = false;
2,460✔
977
        if (0 != opt || inParamList) {
2,460✔
UNCOV
978
          quit = true;
×
UNCOV
979
          break;
×
980
        }
981
        opt = HINT_BATCH_SCAN;
2,460✔
982
        break;
2,460✔
983
      case TK_NO_BATCH_SCAN:
2,172✔
984
        lastComma = false;
2,172✔
985
        if (0 != opt || inParamList) {
2,172✔
UNCOV
986
          quit = true;
×
UNCOV
987
          break;
×
988
        }
989
        opt = HINT_NO_BATCH_SCAN;
2,172✔
990
        break;
2,172✔
991
      case TK_SORT_FOR_GROUP:
62,464✔
992
        lastComma = false;
62,464✔
993
        if (0 != opt || inParamList) {
62,464✔
UNCOV
994
          quit = true;
×
UNCOV
995
          break;
×
996
        }
997
        opt = HINT_SORT_FOR_GROUP;
62,464✔
998
        break;
62,464✔
999
      case TK_PARTITION_FIRST:
2,048✔
1000
        lastComma = false;
2,048✔
1001
        if (0 != opt || inParamList) {
2,048✔
UNCOV
1002
          quit = true;
×
UNCOV
1003
          break;
×
1004
        }
1005
        opt = HINT_PARTITION_FIRST;
2,048✔
1006
        break;
2,048✔
1007
      case TK_PARA_TABLES_SORT:
9,210,320✔
1008
        lastComma = false;
9,210,320✔
1009
        if (0 != opt || inParamList) {
9,210,320✔
UNCOV
1010
          quit = true;
×
UNCOV
1011
          break;
×
1012
        }
1013
        opt = HINT_PARA_TABLES_SORT;
9,210,320✔
1014
        break;
9,210,320✔
1015
      case TK_SMALLDATA_TS_SORT:
×
UNCOV
1016
        lastComma = false;
×
UNCOV
1017
        if (0 != opt || inParamList) {
×
UNCOV
1018
          quit = true;
×
1019
          break;
×
1020
        }
1021
        opt = HINT_SMALLDATA_TS_SORT;
×
1022
        break;
×
1023
      case TK_HASH_JOIN:
1,360✔
1024
        lastComma = false;
1,360✔
1025
        if (0 != opt || inParamList) {
1,360✔
1026
          quit = true;
×
UNCOV
1027
          break;
×
1028
        }
1029
        opt = HINT_HASH_JOIN;
1,360✔
1030
        break;
1,360✔
1031
      case TK_SKIP_TSMA:
157✔
1032
        lastComma = false;
157✔
1033
        if (0 != opt || inParamList) {
157✔
UNCOV
1034
          quit = true;
×
UNCOV
1035
          break;
×
1036
        }
1037
        opt = HINT_SKIP_TSMA;
157✔
1038
        break;
157✔
1039
      case TK_WIN_OPTIMIZE_BATCH:
×
UNCOV
1040
        lastComma = false;
×
UNCOV
1041
        if (0 != opt || inParamList) {
×
UNCOV
1042
          quit = true;
×
1043
          break;
×
1044
        }
1045
        opt = HINT_WIN_OPTIMIZE_BATCH;
×
1046
        break;
×
1047
      case TK_WIN_OPTIMIZE_SINGLE:
×
UNCOV
1048
        lastComma = false;
×
1049
        if (0 != opt || inParamList) {
×
1050
          quit = true;
×
1051
          break;
×
1052
        }
1053
        opt = HINT_WIN_OPTIMIZE_SINGLE;
×
1054
        break;
×
1055
      case TK_NK_LP:
9,280,981✔
1056
        lastComma = false;
9,280,981✔
1057
        if (0 == opt || inParamList) {
9,280,981✔
1058
          quit = true;
×
1059
        }
1060
        inParamList = true;
9,280,981✔
1061
        break;
9,280,981✔
1062
      case TK_NK_RP:
9,280,981✔
1063
        lastComma = false;
9,280,981✔
1064
        if (0 == opt || !inParamList) {
9,280,981✔
UNCOV
1065
          quit = true;
×
1066
        } else {
1067
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
9,280,981✔
1068
          inParamList = false;
9,280,981✔
1069
          paramNum = 0;
9,280,981✔
1070
          opt = 0;
9,280,981✔
1071
        }
1072
        break;
9,280,981✔
1073
      case TK_NK_ID:
1,230✔
1074
        lastComma = false;
1,230✔
1075
        if (0 == opt || !inParamList) {
1,230✔
1076
          quit = true;
410✔
1077
        } else {
1078
          paramList[paramNum++] = t0;
820✔
1079
        }
1080
        break;
1,230✔
1081
      case TK_NK_COMMA:
820✔
1082
        if (lastComma) {
820✔
UNCOV
1083
          quit = true;
×
1084
        }
1085
        lastComma = true;
820✔
1086
        break;
820✔
1087
      case TK_NK_SPACE:
18,556,581✔
1088
        break;
18,556,581✔
UNCOV
1089
      default:
×
UNCOV
1090
        lastComma = false;
×
UNCOV
1091
        quit = true;
×
UNCOV
1092
        break;
×
1093
    }
1094
  }
1095

1096
  taosMemoryFree(hint);
9,279,035✔
1097
  return pHintList;
9,279,035✔
UNCOV
1098
_err:
×
UNCOV
1099
  return NULL;
×
1100
}
1101

1102
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
1,520,375✔
1103
  trimEscape(pCxt, pLiteral, false);
1,520,375✔
1104
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
1,520,375✔
1105
}
1106

1107
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
84,124,322✔
1108
  CHECK_PARSER_STATUS(pCxt);
84,124,322✔
1109
  SValueNode* val = NULL;
84,123,875✔
1110
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
84,123,338✔
1111
  CHECK_MAKE_NODE(val);
84,130,371✔
1112
  if (pLiteral->type == TK_NK_STRING) {
84,130,371✔
1113
    // like '100s' or "100d"
1114
    // check format: ^[0-9]+[smwbauhdny]$'
1115
    if (pLiteral->n < 4) {
19,093✔
1116
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
814✔
1117
      return NULL;
814✔
1118
    }
1119
    char unit = pLiteral->z[pLiteral->n - 2];
18,279✔
1120
    switch (unit) {
18,279✔
1121
      case 'a':
15,041✔
1122
      case 'b':
1123
      case 'd':
1124
      case 'h':
1125
      case 'm':
1126
      case 's':
1127
      case 'u':
1128
      case 'w':
1129
      case 'y':
1130
      case 'n':
1131
        break;
15,041✔
1132
      default:
3,238✔
1133
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
3,238✔
1134
        return NULL;
3,238✔
1135
    }
1136
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
53,688✔
1137
      if (!isdigit(pLiteral->z[i])) {
41,089✔
1138
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
2,442✔
1139
        return NULL;
2,442✔
1140
      }
1141
    }
1142
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
12,599✔
1143
  } else {
1144
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
84,108,862✔
1145
  }
1146
  if (!val->literal) {
84,122,505✔
UNCOV
1147
    nodesDestroyNode((SNode*)val);
×
UNCOV
1148
    pCxt->errCode = terrno;
×
1149
    return NULL;
741✔
1150
  }
1151
  val->flag |= VALUE_FLAG_IS_DURATION;
84,120,985✔
1152
  val->translate = false;
84,121,609✔
1153
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
84,122,417✔
1154
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
84,122,954✔
1155
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
84,121,523✔
1156
  return (SNode*)val;
84,121,970✔
UNCOV
1157
_err:
×
UNCOV
1158
  return NULL;
×
1159
}
1160

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

1213
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
994,051✔
1214
  CHECK_PARSER_STATUS(pCxt);
994,051✔
1215
  if (NULL == pCxt->pQueryCxt->db) {
994,051✔
1216
    return NULL;
5,230✔
1217
  }
1218

1219
  SValueNode* val = NULL;
988,821✔
1220
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
988,821✔
1221
  CHECK_MAKE_NODE(val);
988,821✔
1222
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
988,821✔
1223
  if (!val->literal) {
988,821✔
UNCOV
1224
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1225
    nodesDestroyNode((SNode*)val);
×
UNCOV
1226
    return NULL;
×
1227
  }
1228
  val->translate = false;
988,821✔
1229
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
988,821✔
1230
  val->node.resType.bytes = strlen(val->literal);
988,821✔
1231
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
988,821✔
1232
  return (SNode*)val;
988,821✔
UNCOV
1233
_err:
×
UNCOV
1234
  return NULL;
×
1235
}
1236

1237
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
26,106✔
1238
  CHECK_PARSER_STATUS(pCxt);
26,106✔
1239
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
26,106✔
1240
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
796✔
1241
    return NULL;
796✔
1242
  }
1243
  SValueNode* val = NULL;
25,310✔
1244
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
25,310✔
1245
  CHECK_MAKE_NODE(val);
25,310✔
1246
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
25,310✔
1247
  if (!val->literal) {
25,310✔
UNCOV
1248
    pCxt->errCode = terrno;
×
UNCOV
1249
    nodesDestroyNode((SNode*)val);
×
UNCOV
1250
    return NULL;
×
1251
  }
1252
  val->placeholderNo = ++pCxt->placeholderNo;
25,310✔
1253
  if (NULL == pCxt->pPlaceholderValues) {
25,310✔
1254
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
19,149✔
1255
    if (NULL == pCxt->pPlaceholderValues) {
19,149✔
UNCOV
1256
      nodesDestroyNode((SNode*)val);
×
UNCOV
1257
      return NULL;
×
1258
    }
1259
  }
1260
  if (NULL == taosArrayPush(pCxt->pPlaceholderValues, &val)) {
50,620✔
1261
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1262
    nodesDestroyNode((SNode*)val);
×
UNCOV
1263
    taosArrayDestroy(pCxt->pPlaceholderValues);
×
UNCOV
1264
    return NULL;
×
1265
  }
1266
  return (SNode*)val;
25,310✔
1267
_err:
×
1268
  return NULL;
×
1269
}
1270

1271
SNode* createDurationPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
1,176✔
1272
  SNode* pNode = createPlaceholderValueNode(pCxt, pLiteral);
1,176✔
1273
  if (pNode != NULL) {
1,176✔
1274
    SValueNode* val = (SValueNode*)pNode;
1,176✔
1275
    val->flag |= VALUE_FLAG_IS_DURATION;
1,176✔
1276
    val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
1,176✔
1277
    val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
1,176✔
1278
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
1,176✔
1279
  }
1280
  return pNode;
1,176✔
1281
}
1282

1283
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
213,759,586✔
1284
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
213,759,586✔
1285
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
30,073,387✔
1286
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
30,040,447✔
1287
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
30,040,447✔
1288
    nodesDestroyNode(pParam);
30,040,447✔
1289
    return code;
30,040,447✔
1290
  } else {
1291
    return nodesListAppend(pCond->pParameterList, pParam);
183,720,111✔
1292
  }
1293
}
1294

1295
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
107,061,150✔
1296
  CHECK_PARSER_STATUS(pCxt);
107,061,150✔
1297
  SLogicConditionNode* cond = NULL;
107,061,238✔
1298
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
107,061,150✔
1299
  CHECK_MAKE_NODE(cond);
107,066,475✔
1300
  cond->condType = type;
107,066,475✔
1301
  cond->pParameterList = NULL;
107,064,955✔
1302
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
107,064,507✔
1303
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
107,066,067✔
1304
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
107,064,514✔
1305
  }
1306
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
107,066,279✔
1307
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
106,698,033✔
1308
  }
1309
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
107,065,919✔
UNCOV
1310
    nodesDestroyNode((SNode*)cond);
×
UNCOV
1311
    return NULL;
×
1312
  }
1313
  return (SNode*)cond;
107,064,412✔
1314
_err:
×
1315
  nodesDestroyNode(pParam1);
×
UNCOV
1316
  nodesDestroyNode(pParam2);
×
1317
  return NULL;
274✔
1318
}
1319

1320
static uint8_t getMinusDataType(uint8_t orgType) {
35,059,919✔
1321
  switch (orgType) {
35,059,919✔
1322
    case TSDB_DATA_TYPE_UTINYINT:
24,703,483✔
1323
    case TSDB_DATA_TYPE_USMALLINT:
1324
    case TSDB_DATA_TYPE_UINT:
1325
    case TSDB_DATA_TYPE_UBIGINT:
1326
      return TSDB_DATA_TYPE_BIGINT;
24,703,483✔
1327
    default:
10,356,782✔
1328
      break;
10,356,782✔
1329
  }
1330
  return orgType;
10,356,782✔
1331
}
1332

1333
SNode* setNodeQuantifyType(SAstCreateContext* pCxt, SNode* pNode, EQuantifyType type) {
62,318,128✔
1334
  CHECK_PARSER_STATUS(pCxt);
62,318,128✔
1335

1336
  switch (nodeType(pNode)) {
62,318,128✔
1337
    case QUERY_NODE_SELECT_STMT: {
62,124,861✔
1338
      SSelectStmt* pSelect = (SSelectStmt*)pNode;
62,124,861✔
1339
      pSelect->quantify = type;
62,124,861✔
1340
      pSelect->subQType = E_SUB_QUERY_COLUMN;
62,124,861✔
1341
      break;
62,124,861✔
1342
    }  
1343
    case QUERY_NODE_SET_OPERATOR:{
193,914✔
1344
      SSetOperator* pSet = (SSetOperator*)pNode;
193,914✔
1345
      pSet->quantify = type;
193,914✔
1346
      pSet->subQType = E_SUB_QUERY_COLUMN;
193,914✔
1347
      break;
193,914✔
1348
    }
UNCOV
1349
    default:
×
UNCOV
1350
      pCxt->errCode = TSDB_CODE_PAR_INVALID_EXPR_SUBQ;
×
UNCOV
1351
      CHECK_PARSER_STATUS(pCxt);
×
UNCOV
1352
      break;
×
1353
  }
1354

1355
  return pNode;
62,318,128✔
1356

UNCOV
1357
_err:
×
UNCOV
1358
  return NULL;
×
1359
}
1360

1361
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
658,007,206✔
1362
  CHECK_PARSER_STATUS(pCxt);
658,007,206✔
1363
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
658,004,803✔
1364
    SValueNode* pVal = (SValueNode*)pLeft;
35,060,309✔
1365
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
35,060,309✔
1366
    if (!pNewLiteral) {
35,060,623✔
UNCOV
1367
      pCxt->errCode = terrno;
×
UNCOV
1368
      goto _err;
×
1369
    }
1370
    if ('+' == pVal->literal[0]) {
35,060,623✔
1371
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal + 1);
×
1372
    } else if ('-' == pVal->literal[0]) {
35,060,587✔
1373
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "%s", pVal->literal + 1);
5,004✔
1374
    } else {
1375
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal);
35,055,543✔
1376
    }
1377
    taosMemoryFree(pVal->literal);
35,060,625✔
1378
    pVal->literal = pNewLiteral;
35,060,481✔
1379
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
35,060,481✔
1380
    return pLeft;
35,060,174✔
1381
  }
1382
  if ((OP_TYPE_IN == type || OP_TYPE_NOT_IN == type) && pRight) {
622,944,494✔
1383
    if (QUERY_NODE_SELECT_STMT == nodeType(pRight)) {
24,757,603✔
1384
      ((SSelectStmt*)pRight)->subQType= E_SUB_QUERY_COLUMN;
18,010,103✔
1385
    } else if (QUERY_NODE_SET_OPERATOR == nodeType(pRight)) {
6,747,500✔
1386
      ((SSetOperator*)pRight)->subQType= E_SUB_QUERY_COLUMN;
916,576✔
1387
    }
1388
  }
1389
  if ((OP_TYPE_EXISTS == type || OP_TYPE_NOT_EXISTS == type) && pLeft) {
622,944,494✔
1390
    if (QUERY_NODE_SELECT_STMT == nodeType(pLeft)) {
4,853,134✔
1391
      ((SSelectStmt*)pLeft)->subQType= E_SUB_QUERY_ROWNUM;
4,853,134✔
UNCOV
1392
    } else if (QUERY_NODE_SET_OPERATOR == nodeType(pLeft)) {
×
UNCOV
1393
      ((SSetOperator*)pLeft)->subQType= E_SUB_QUERY_ROWNUM;
×
1394
    }
1395
  }
1396
  if (pLeft && QUERY_NODE_VALUE == nodeType(pLeft)) {
622,944,494✔
1397
    SValueNode* pVal = (SValueNode*)pLeft;
40,427,520✔
1398
    pVal->tz = pCxt->pQueryCxt->timezone;
40,427,520✔
1399
  }
1400
  if (pRight && QUERY_NODE_VALUE == nodeType(pRight)) {
622,945,493✔
1401
    SValueNode* pVal = (SValueNode*)pRight;
278,990,944✔
1402
    pVal->tz = pCxt->pQueryCxt->timezone;
278,990,944✔
1403
  }
1404

1405
  SOperatorNode* op = NULL;
622,945,326✔
1406
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
622,946,181✔
1407
  CHECK_MAKE_NODE(op);
622,967,537✔
1408
  op->opType = type;
622,967,537✔
1409
  op->pLeft = pLeft;
622,967,076✔
1410
  op->pRight = pRight;
622,963,404✔
1411
  op->tz = pCxt->pQueryCxt->timezone;
622,963,562✔
1412
  op->charsetCxt = pCxt->pQueryCxt->charsetCxt;
622,963,751✔
1413
  return (SNode*)op;
622,963,377✔
UNCOV
1414
_err:
×
UNCOV
1415
  nodesDestroyNode(pLeft);
×
UNCOV
1416
  nodesDestroyNode(pRight);
×
UNCOV
1417
  return NULL;
×
1418
}
1419

1420
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
21,843,165✔
1421
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
21,843,165✔
1422
  CHECK_PARSER_STATUS(pCxt);
21,843,701✔
1423
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
21,843,341✔
1424
  CHECK_PARSER_STATUS(pCxt);
21,842,091✔
1425
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
21,842,179✔
1426
  CHECK_PARSER_STATUS(pCxt);
21,842,899✔
1427
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
21,844,779✔
1428
  CHECK_PARSER_STATUS(pCxt);
21,845,036✔
1429
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
21,843,068✔
1430
  CHECK_PARSER_STATUS(pCxt);
21,841,998✔
1431
  return pRet;
21,843,517✔
UNCOV
1432
_err:
×
UNCOV
1433
  nodesDestroyNode(pNew);
×
UNCOV
1434
  nodesDestroyNode(pGE);
×
UNCOV
1435
  nodesDestroyNode(pLE);
×
1436
  nodesDestroyNode(pExpr);
×
1437
  nodesDestroyNode(pLeft);
×
1438
  nodesDestroyNode(pRight);
×
1439
  return NULL;
×
1440
}
1441

1442
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
3,615,232✔
1443
  SNode *pNew = NULL, *pLT = NULL, *pGT = NULL;
3,615,232✔
1444
  CHECK_PARSER_STATUS(pCxt);
3,615,232✔
1445
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
3,615,232✔
1446
  CHECK_PARSER_STATUS(pCxt);
3,615,232✔
1447
  pLT = createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft);
3,615,232✔
1448
  CHECK_PARSER_STATUS(pCxt);
3,615,232✔
1449
  pGT = createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pNew, pRight);
3,615,232✔
1450
  CHECK_PARSER_STATUS(pCxt);
3,615,232✔
1451
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, pLT, pGT);
3,615,232✔
1452
  CHECK_PARSER_STATUS(pCxt);
3,615,232✔
1453
  return pRet;
3,615,232✔
UNCOV
1454
_err:
×
UNCOV
1455
  nodesDestroyNode(pNew);
×
UNCOV
1456
  nodesDestroyNode(pGT);
×
UNCOV
1457
  nodesDestroyNode(pLT);
×
1458
  nodesDestroyNode(pExpr);
×
1459
  nodesDestroyNode(pLeft);
×
1460
  nodesDestroyNode(pRight);
×
1461
  return NULL;
×
1462
}
1463

1464
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
92,475,189✔
1465
  CHECK_PARSER_STATUS(pCxt);
92,475,189✔
1466
  SColumnNode* pCol = NULL;
92,477,244✔
1467
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
92,477,780✔
1468
  CHECK_MAKE_NODE(pCol);
92,480,530✔
1469
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
92,480,530✔
1470
  if (NULL == pFuncName) {
92,480,442✔
1471
    tstrncpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME, TSDB_COL_NAME_LEN);
78,666,721✔
1472
  } else {
1473
    tstrncpy(pCol->colName, pFuncName->z, pFuncName->n + 1);
13,813,721✔
1474
  }
1475
  pCol->isPrimTs = true;
92,479,634✔
1476
  return (SNode*)pCol;
92,479,186✔
UNCOV
1477
_err:
×
UNCOV
1478
  return NULL;
×
1479
}
1480

1481
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
535,989,642✔
1482
  CHECK_PARSER_STATUS(pCxt);
535,989,642✔
1483
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
535,990,832✔
1484
    return createPrimaryKeyCol(pCxt, pFuncName);
13,799,605✔
1485
  }
1486
  SFunctionNode* func = NULL;
522,193,712✔
1487
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
522,194,353✔
1488
  CHECK_MAKE_NODE(func);
522,197,297✔
1489
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
522,197,297✔
1490
  func->pParameterList = pParameterList;
522,195,778✔
1491
  func->tz = pCxt->pQueryCxt->timezone;
522,195,702✔
1492
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
522,198,180✔
1493
  return (SNode*)func;
522,194,646✔
UNCOV
1494
_err:
×
UNCOV
1495
  nodesDestroyList(pParameterList);
×
UNCOV
1496
  return NULL;
×
1497
}
1498

1499
SNode* createPHTbnameFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
44,971✔
1500
  CHECK_PARSER_STATUS(pCxt);
44,971✔
1501
  SFunctionNode* func = NULL;
44,971✔
1502
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
44,971✔
1503
  CHECK_MAKE_NODE(func);
44,971✔
1504
  tstrncpy(func->functionName, "_placeholder_tbname", TSDB_FUNC_NAME_LEN);
44,971✔
1505
  func->pParameterList = pParameterList;
44,971✔
1506
  func->tz = pCxt->pQueryCxt->timezone;
44,971✔
1507
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
44,971✔
1508
  return (SNode*)func;
44,971✔
UNCOV
1509
_err:
×
UNCOV
1510
  nodesDestroyList(pParameterList);
×
UNCOV
1511
  return NULL;
×
1512
}
1513

1514
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
114,393,632✔
1515
  SFunctionNode* func = NULL;
114,393,632✔
1516
  CHECK_PARSER_STATUS(pCxt);
114,393,632✔
1517
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
114,393,632✔
1518
  CHECK_MAKE_NODE(func);
114,394,791✔
1519
  tstrncpy(func->functionName, "cast", TSDB_FUNC_NAME_LEN);
114,394,791✔
1520
  func->node.resType = dt;
114,394,791✔
1521
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
114,394,791✔
1522
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
88,786,474✔
1523
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
25,608,317✔
1524
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
4,269,461✔
1525
  }
1526
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
114,394,791✔
1527
  CHECK_PARSER_STATUS(pCxt);
114,394,881✔
1528
  func->tz = pCxt->pQueryCxt->timezone;
114,394,881✔
1529
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
114,394,881✔
1530

1531
  return (SNode*)func;
114,394,881✔
UNCOV
1532
_err:
×
UNCOV
1533
  nodesDestroyNode((SNode*)func);
×
UNCOV
1534
  nodesDestroyNode(pExpr);
×
1535
  return NULL;
184✔
1536
}
1537

1538
SNode* createPositionFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
130,464✔
1539
  SFunctionNode* func = NULL;
130,464✔
1540
  CHECK_PARSER_STATUS(pCxt);
130,464✔
1541
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
130,464✔
1542
  CHECK_MAKE_NODE(func);
130,464✔
1543
  tstrncpy(func->functionName, "position", TSDB_FUNC_NAME_LEN);
130,464✔
1544
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
130,464✔
1545
  CHECK_PARSER_STATUS(pCxt);
130,464✔
1546
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
130,464✔
1547
  CHECK_PARSER_STATUS(pCxt);
130,464✔
1548
  return (SNode*)func;
130,464✔
UNCOV
1549
_err:
×
UNCOV
1550
  nodesDestroyNode((SNode*)func);
×
UNCOV
1551
  nodesDestroyNode(pExpr);
×
UNCOV
1552
  nodesDestroyNode(pExpr2);
×
1553
  return NULL;
×
1554
}
1555

1556
SNode* createTrimFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, ETrimType type) {
25,630✔
1557
  SFunctionNode* func = NULL;
25,630✔
1558
  CHECK_PARSER_STATUS(pCxt);
25,630✔
1559
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
25,630✔
1560
  CHECK_MAKE_NODE(func);
25,630✔
1561
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
25,630✔
1562
  func->trimType = type;
25,630✔
1563
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
25,630✔
1564
  CHECK_PARSER_STATUS(pCxt);
25,630✔
1565
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
25,630✔
1566
  return (SNode*)func;
25,630✔
UNCOV
1567
_err:
×
UNCOV
1568
  nodesDestroyNode((SNode*)func);
×
UNCOV
1569
  nodesDestroyNode(pExpr);
×
UNCOV
1570
  return NULL;
×
1571
}
1572

1573
SNode* createTrimFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, ETrimType type) {
68,130✔
1574
  SFunctionNode* func = NULL;
68,130✔
1575
  CHECK_PARSER_STATUS(pCxt);
68,130✔
1576
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
68,130✔
1577
  CHECK_MAKE_NODE(func);
68,130✔
1578
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
68,130✔
1579
  func->trimType = type;
68,130✔
1580
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
68,130✔
1581
  CHECK_PARSER_STATUS(pCxt);
68,130✔
1582
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
68,130✔
1583
  CHECK_PARSER_STATUS(pCxt);
68,130✔
1584
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
68,130✔
1585
  return (SNode*)func;
68,130✔
UNCOV
1586
_err:
×
UNCOV
1587
  nodesDestroyNode((SNode*)func);
×
UNCOV
1588
  nodesDestroyNode(pExpr);
×
UNCOV
1589
  nodesDestroyNode(pExpr2);
×
1590
  return NULL;
×
1591
}
1592

1593
SNode* createSubstrFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
10,655✔
1594
  SFunctionNode* func = NULL;
10,655✔
1595
  CHECK_PARSER_STATUS(pCxt);
10,655✔
1596
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
10,655✔
1597
  CHECK_MAKE_NODE(func);
10,655✔
1598
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
10,655✔
1599
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
10,655✔
1600
  CHECK_PARSER_STATUS(pCxt);
10,655✔
1601
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
10,655✔
1602
  CHECK_PARSER_STATUS(pCxt);
10,655✔
1603
  return (SNode*)func;
10,655✔
UNCOV
1604
_err:
×
UNCOV
1605
  nodesDestroyNode((SNode*)func);
×
UNCOV
1606
  nodesDestroyNode(pExpr);
×
UNCOV
1607
  nodesDestroyNode(pExpr2);
×
1608
  return NULL;
×
1609
}
1610

1611
SNode* createSubstrFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, SNode* pExpr3) {
23,422✔
1612
  SFunctionNode* func = NULL;
23,422✔
1613
  CHECK_PARSER_STATUS(pCxt);
23,422✔
1614
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
23,422✔
1615
  CHECK_MAKE_NODE(func);
23,422✔
1616
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
23,422✔
1617
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
23,422✔
1618
  CHECK_PARSER_STATUS(pCxt);
23,422✔
1619
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
23,422✔
1620
  CHECK_PARSER_STATUS(pCxt);
23,422✔
1621
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr3);
23,422✔
1622
  CHECK_PARSER_STATUS(pCxt);
23,422✔
1623
  return (SNode*)func;
23,422✔
UNCOV
1624
_err:
×
UNCOV
1625
  nodesDestroyNode((SNode*)func);
×
UNCOV
1626
  nodesDestroyNode(pExpr);
×
UNCOV
1627
  nodesDestroyNode(pExpr2);
×
1628
  nodesDestroyNode(pExpr3);
×
1629
  return NULL;
×
1630
}
1631

1632
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
7,853,650✔
1633
  SNodeListNode* list = NULL;
7,853,650✔
1634
  CHECK_PARSER_STATUS(pCxt);
7,853,650✔
1635
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
7,855,170✔
1636
  CHECK_MAKE_NODE(list);
7,854,634✔
1637
  list->pNodeList = pList;
7,854,634✔
1638
  return (SNode*)list;
7,854,186✔
UNCOV
1639
_err:
×
UNCOV
1640
  nodesDestroyList(pList);
×
UNCOV
1641
  return NULL;
×
1642
}
1643

1644
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
12,736✔
1645
  SNodeListNode* list = NULL;
12,736✔
1646
  CHECK_PARSER_STATUS(pCxt);
12,736✔
1647
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
12,736✔
1648
  CHECK_MAKE_NODE(list);
12,736✔
1649
  pCxt->errCode = nodesListMakeStrictAppend(&list->pNodeList, p1);
12,736✔
1650
  CHECK_PARSER_STATUS(pCxt);
12,736✔
1651
  pCxt->errCode = nodesListStrictAppend(list->pNodeList, p2);
12,736✔
1652
  CHECK_PARSER_STATUS(pCxt);
12,736✔
1653
  return (SNode*)list;
12,736✔
UNCOV
1654
_err:
×
UNCOV
1655
  nodesDestroyNode((SNode*)list);
×
UNCOV
1656
  nodesDestroyNode(p1);
×
UNCOV
1657
  nodesDestroyNode(p2);
×
1658
  return NULL;
×
1659
}
1660

1661
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
911,945,808✔
1662
  CHECK_PARSER_STATUS(pCxt);
911,945,808✔
1663
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
911,948,881✔
1664
  CHECK_NAME(checkTableName(pCxt, pTableName));
911,966,341✔
1665
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
911,930,338✔
1666
  SRealTableNode* realTable = NULL;
911,935,570✔
1667
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
911,936,854✔
1668
  CHECK_MAKE_NODE(realTable);
911,909,528✔
1669
  if (NULL != pDbName) {
911,909,528✔
1670
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
190,841,258✔
1671
  } else {
1672
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
721,068,270✔
1673
  }
1674
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
911,920,012✔
1675
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
95,147,730✔
1676
  } else {
1677
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
816,772,956✔
1678
  }
1679
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
911,916,135✔
1680
  return (SNode*)realTable;
911,905,046✔
1681
_err:
42,885✔
1682
  return NULL;
42,885✔
1683
}
1684

1685
SNode* createPlaceHolderTableNode(SAstCreateContext* pCxt, EStreamPlaceholder type, SToken* pTableAlias) {
183,654✔
1686
  CHECK_PARSER_STATUS(pCxt);
183,654✔
1687

1688
  SPlaceHolderTableNode* phTable = NULL;
183,654✔
1689
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PLACE_HOLDER_TABLE, (SNode**)&phTable);
183,654✔
1690
  CHECK_MAKE_NODE(phTable);
183,654✔
1691

1692
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
183,654✔
1693
    COPY_STRING_FORM_ID_TOKEN(phTable->table.tableAlias, pTableAlias);
3,120✔
1694
  }
1695

1696
  phTable->placeholderType = type;
183,654✔
1697
  return (SNode*)phTable;
183,654✔
UNCOV
1698
_err:
×
UNCOV
1699
  return NULL;
×
1700
}
1701

1702
SNode* createStreamNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pStreamName) {
606,129✔
1703
  CHECK_PARSER_STATUS(pCxt);
606,129✔
1704
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
606,129✔
1705
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
606,129✔
1706
  SStreamNode* pStream = NULL;
605,911✔
1707
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM, (SNode**)&pStream);
605,911✔
1708
  CHECK_MAKE_NODE(pStream);
605,911✔
1709
  if (NULL != pDbName) {
605,911✔
1710
    COPY_STRING_FORM_ID_TOKEN(pStream->dbName, pDbName);
430,697✔
1711
  } else {
1712
    snprintf(pStream->dbName, sizeof(pStream->dbName), "%s", pCxt->pQueryCxt->db);
175,214✔
1713
  }
1714
  COPY_STRING_FORM_ID_TOKEN(pStream->streamName, pStreamName);
605,911✔
1715
  return (SNode*)pStream;
605,911✔
1716
_err:
218✔
1717
  return NULL;
218✔
1718
}
1719

1720
SNode* createRecalcRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
12,749✔
1721
  SStreamCalcRangeNode* pRange = NULL;
12,749✔
1722
  CHECK_PARSER_STATUS(pCxt);
12,749✔
1723
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_CALC_RANGE, (SNode**)&pRange);
12,749✔
1724
  CHECK_MAKE_NODE(pRange);
12,749✔
1725
  if (NULL == pStart && NULL == pEnd) {
12,749✔
UNCOV
1726
    pRange->calcAll = true;
×
1727
  } else {
1728
    pRange->calcAll = false;
12,749✔
1729
    pRange->pStart = pStart;
12,749✔
1730
    pRange->pEnd = pEnd;
12,749✔
1731
  }
1732

1733
  return (SNode*)pRange;
12,749✔
UNCOV
1734
_err:
×
UNCOV
1735
  nodesDestroyNode((SNode*)pRange);
×
UNCOV
1736
  nodesDestroyNode(pStart);
×
UNCOV
1737
  nodesDestroyNode(pEnd);
×
1738
  return NULL;
×
1739
}
1740

1741
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
34,021,792✔
1742
  CHECK_PARSER_STATUS(pCxt);
34,021,792✔
1743
  if (!checkTableName(pCxt, pTableAlias)) {
34,022,223✔
UNCOV
1744
    return NULL;
×
1745
  }
1746
  STempTableNode* tempTable = NULL;
34,021,792✔
1747
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
34,021,792✔
1748
  CHECK_MAKE_NODE(tempTable);
34,022,223✔
1749
  tempTable->pSubquery = pSubquery;
34,022,223✔
1750
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
34,022,223✔
1751
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
1,176,702✔
1752
  } else {
1753
    taosRandStr(tempTable->table.tableAlias, 32);
32,845,521✔
1754
  }
1755
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
34,021,792✔
1756
    tstrncpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
29,459,103✔
1757
    ((SSelectStmt*)pSubquery)->isSubquery = true;
29,458,672✔
1758
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
4,562,689✔
1759
    tstrncpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
4,563,120✔
1760
  }
1761
  return (SNode*)tempTable;
34,021,361✔
UNCOV
1762
_err:
×
UNCOV
1763
  nodesDestroyNode(pSubquery);
×
UNCOV
1764
  return NULL;
×
1765
}
1766

1767
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, EJoinSubType stype, SNode* pLeft, SNode* pRight,
48,696,389✔
1768
                           SNode* pJoinCond) {
1769
  CHECK_PARSER_STATUS(pCxt);
48,696,389✔
1770
  SJoinTableNode* joinTable = NULL;
48,696,389✔
1771
  pCxt->errCode = nodesMakeNode(QUERY_NODE_JOIN_TABLE, (SNode**)&joinTable);
48,696,389✔
1772
  CHECK_MAKE_NODE(joinTable);
48,700,741✔
1773
  joinTable->joinType = type;
48,700,741✔
1774
  joinTable->subType = stype;
48,700,741✔
1775
  joinTable->pLeft = pLeft;
48,700,741✔
1776
  joinTable->pRight = pRight;
48,700,741✔
1777
  joinTable->pOnCond = pJoinCond;
48,700,741✔
1778
  return (SNode*)joinTable;
48,700,741✔
UNCOV
1779
_err:
×
UNCOV
1780
  nodesDestroyNode(pLeft);
×
UNCOV
1781
  nodesDestroyNode(pRight);
×
UNCOV
1782
  nodesDestroyNode(pJoinCond);
×
1783
  return NULL;
×
1784
}
1785

1786
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
408,320✔
1787
  CHECK_PARSER_STATUS(pCxt);
408,320✔
1788
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
408,320✔
1789
  CHECK_NAME(checkViewName(pCxt, pViewName));
407,884✔
1790
  SViewNode* pView = NULL;
407,053✔
1791
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
407,053✔
1792
  CHECK_MAKE_NODE(pView);
407,053✔
1793
  if (NULL != pDbName) {
407,053✔
1794
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
201,107✔
1795
  } else {
1796
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
205,946✔
1797
  }
1798
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
407,053✔
1799
  return (SNode*)pView;
407,053✔
1800
_err:
1,267✔
1801
  return NULL;
1,267✔
1802
}
1803

1804
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) {
32,569,752✔
1805
  CHECK_PARSER_STATUS(pCxt);
32,569,752✔
1806
  SLimitNode* limitNode = NULL;
32,569,752✔
1807
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
32,569,752✔
1808
  CHECK_MAKE_NODE(limitNode);
32,570,281✔
1809
  limitNode->limit = (SValueNode*)pLimit;
32,570,281✔
1810
  if (NULL != pOffset) {
32,570,281✔
1811
    limitNode->offset = (SValueNode*)pOffset;
7,309,671✔
1812
  }
1813
  return (SNode*)limitNode;
32,570,281✔
UNCOV
1814
_err:
×
UNCOV
1815
  return NULL;
×
1816
}
1817

1818
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
198,521,557✔
1819
  CHECK_PARSER_STATUS(pCxt);
198,521,557✔
1820
  SOrderByExprNode* orderByExpr = NULL;
198,522,419✔
1821
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
198,522,419✔
1822
  CHECK_MAKE_NODE(orderByExpr);
198,531,062✔
1823
  orderByExpr->pExpr = pExpr;
198,531,062✔
1824
  orderByExpr->order = order;
198,531,150✔
1825
  if (NULL_ORDER_DEFAULT == nullOrder) {
198,531,062✔
1826
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
198,264,755✔
1827
  }
1828
  orderByExpr->nullOrder = nullOrder;
198,531,062✔
1829
  return (SNode*)orderByExpr;
198,531,062✔
UNCOV
1830
_err:
×
UNCOV
1831
  nodesDestroyNode(pExpr);
×
UNCOV
1832
  return NULL;
×
1833
}
1834

1835
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
15,819,974✔
1836
  CHECK_PARSER_STATUS(pCxt);
15,819,974✔
1837
  SSessionWindowNode* session = NULL;
15,819,974✔
1838
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
15,819,974✔
1839
  CHECK_MAKE_NODE(session);
15,822,019✔
1840
  session->pCol = (SColumnNode*)pCol;
15,822,019✔
1841
  session->pGap = (SValueNode*)pGap;
15,822,019✔
1842
  return (SNode*)session;
15,822,019✔
UNCOV
1843
_err:
×
UNCOV
1844
  nodesDestroyNode(pCol);
×
UNCOV
1845
  nodesDestroyNode(pGap);
×
UNCOV
1846
  return NULL;
×
1847
}
1848

1849
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr, SNodeList* pOptions, SNode* pTrueForLimit) {
6,198,578✔
1850
  SStateWindowNode* state = NULL;
6,198,578✔
1851
  CHECK_PARSER_STATUS(pCxt);
6,198,578✔
1852
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
6,198,180✔
1853
  CHECK_MAKE_NODE(state);
6,198,598✔
1854
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
6,198,598✔
1855
  CHECK_MAKE_NODE(state->pCol);
6,198,607✔
1856
  state->pExpr = pExpr;
6,198,607✔
1857
  state->pTrueForLimit = pTrueForLimit;
6,198,607✔
1858
  if (pOptions != NULL) {
6,198,607✔
1859
    if (pOptions->length >= 1) {
392,333✔
1860
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 0), &state->pExtend);
392,333✔
1861
      CHECK_MAKE_NODE(state->pExtend);
392,333✔
1862
    }
1863
    if (pOptions->length == 2) {
392,333✔
UNCOV
1864
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 1), &state->pZeroth);
×
UNCOV
1865
      CHECK_MAKE_NODE(state->pZeroth);
×
1866
    }
1867
    nodesDestroyList(pOptions);
392,333✔
1868
  }
1869
  return (SNode*)state;
6,198,364✔
1870
_err:
398✔
1871
  nodesDestroyNode((SNode*)state);
398✔
1872
  nodesDestroyNode(pExpr);
398✔
1873
  nodesDestroyNode(pTrueForLimit);
398✔
1874
  nodesDestroyList(pOptions);
398✔
1875
  return NULL;
475✔
1876
}
1877

1878
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond, SNode* pTrueForLimit) {
5,647,213✔
1879
  SEventWindowNode* pEvent = NULL;
5,647,213✔
1880
  CHECK_PARSER_STATUS(pCxt);
5,647,213✔
1881
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
5,647,213✔
1882
  CHECK_MAKE_NODE(pEvent);
5,648,157✔
1883
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
5,648,157✔
1884
  CHECK_MAKE_NODE(pEvent->pCol);
5,647,730✔
1885
  pEvent->pStartCond = pStartCond;
5,647,730✔
1886
  pEvent->pEndCond = pEndCond;
5,647,730✔
1887
  pEvent->pTrueForLimit = pTrueForLimit;
5,647,730✔
1888
  return (SNode*)pEvent;
5,647,730✔
UNCOV
1889
_err:
×
UNCOV
1890
  nodesDestroyNode((SNode*)pEvent);
×
UNCOV
1891
  nodesDestroyNode(pStartCond);
×
UNCOV
1892
  nodesDestroyNode(pEndCond);
×
1893
  nodesDestroyNode(pTrueForLimit);
×
1894
  return NULL;
426✔
1895
}
1896

1897
SNode* createTrueForCountNode(SAstCreateContext* pCxt, const SToken* pCount) {
18,888✔
1898
  STrueForNode* pTrueFor = NULL;
18,888✔
1899
  CHECK_PARSER_STATUS(pCxt);
18,888✔
1900
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRUE_FOR, (SNode**)&pTrueFor);
18,888✔
1901
  CHECK_MAKE_NODE(pTrueFor);
18,888✔
1902
  pTrueFor->trueForType = TRUE_FOR_COUNT_ONLY;
18,888✔
1903
  pTrueFor->pDuration = NULL;
18,888✔
1904
  pTrueFor->count = taosStr2Int32(pCount->z, NULL, 10);
18,888✔
1905
  if (pTrueFor->count < 0) {
18,888✔
1906
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_TRUE_FOR_COUNT);
398✔
1907
    goto _err;
398✔
1908
  }
1909
  return (SNode*)pTrueFor;
18,490✔
1910
_err:
398✔
1911
  nodesDestroyNode((SNode*)pTrueFor);
398✔
1912
  return NULL;
398✔
1913
}
1914

1915
SNode* createTrueForAndNode(SAstCreateContext* pCxt, SNode* pDuration, const SToken* pCount) {
17,296✔
1916
  STrueForNode* pTrueFor = NULL;
17,296✔
1917
  CHECK_PARSER_STATUS(pCxt);
17,296✔
1918
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRUE_FOR, (SNode**)&pTrueFor);
17,296✔
1919
  CHECK_MAKE_NODE(pTrueFor);
17,296✔
1920
  pTrueFor->trueForType = TRUE_FOR_AND;
17,296✔
1921
  pTrueFor->pDuration = pDuration;
17,296✔
1922
  pDuration = NULL;
17,296✔
1923
  pTrueFor->count = taosStr2Int32(pCount->z, NULL, 10);
17,296✔
1924
  return (SNode*)pTrueFor;
17,296✔
UNCOV
1925
_err:
×
UNCOV
1926
  nodesDestroyNode((SNode*)pTrueFor);
×
UNCOV
1927
  nodesDestroyNode(pDuration);
×
UNCOV
1928
  return NULL;
×
1929
}
1930

1931
SNode* createTrueForOrNode(SAstCreateContext* pCxt, SNode* pDuration, const SToken* pCount) {
17,296✔
1932
  STrueForNode* pTrueFor = NULL;
17,296✔
1933
  CHECK_PARSER_STATUS(pCxt);
17,296✔
1934
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRUE_FOR, (SNode**)&pTrueFor);
17,296✔
1935
  CHECK_MAKE_NODE(pTrueFor);
17,296✔
1936
  pTrueFor->trueForType = TRUE_FOR_OR;
17,296✔
1937
  pTrueFor->pDuration = pDuration;
17,296✔
1938
  pDuration = NULL;
17,296✔
1939
  pTrueFor->count = taosStr2Int32(pCount->z, NULL, 10);
17,296✔
1940
  return (SNode*)pTrueFor;
17,296✔
UNCOV
1941
_err:
×
UNCOV
1942
  nodesDestroyNode((SNode*)pTrueFor);
×
UNCOV
1943
  nodesDestroyNode(pDuration);
×
UNCOV
1944
  return NULL;
×
1945
}
1946

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

1968
SNode* createCountWindowNodeFromArgs(SAstCreateContext* pCxt, SNode* arg) {
5,317,161✔
1969
  SCountWindowArgs* args = (SCountWindowArgs*)arg;
5,317,161✔
1970
  SCountWindowNode* pCount = NULL;
5,317,161✔
1971
  CHECK_PARSER_STATUS(pCxt);
5,317,161✔
1972
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
5,317,161✔
1973
  CHECK_MAKE_NODE(pCount);
5,317,786✔
1974
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
5,317,786✔
1975
  CHECK_MAKE_NODE(pCount->pCol);
5,317,602✔
1976
  pCount->windowCount = args->count;
5,317,602✔
1977
  pCount->windowSliding = args->sliding;
5,317,602✔
1978
  pCount->pColList = args->pColList;
5,317,602✔
1979
  args->pColList = NULL;
5,317,602✔
1980
  nodesDestroyNode(arg);
5,317,602✔
1981
  return (SNode*)pCount;
5,317,603✔
UNCOV
1982
_err:
×
UNCOV
1983
  nodesDestroyNode((SNode*)pCount);
×
UNCOV
1984
  return NULL;
×
1985
}
1986

1987
SNode* createCountWindowArgs(SAstCreateContext* pCxt, const SToken* countToken, const SToken* slidingToken,
5,321,891✔
1988
                             SNodeList* colList) {
1989
  CHECK_PARSER_STATUS(pCxt);
5,321,891✔
1990

1991
  SCountWindowArgs* args = NULL;
5,321,891✔
1992
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW_ARGS, (SNode**)&args);
5,321,891✔
1993
  CHECK_MAKE_NODE(args);
5,322,516✔
1994
  args->count = taosStr2Int64(countToken->z, NULL, 10);
5,322,516✔
1995
  if (slidingToken && slidingToken->type == TK_NK_INTEGER) {
5,322,004✔
1996
    args->sliding = taosStr2Int64(slidingToken->z, NULL, 10);
38,739✔
1997
  } else {
1998
    args->sliding = taosStr2Int64(countToken->z, NULL, 10);
5,283,265✔
1999
  }
2000
  args->pColList = colList;
5,322,611✔
2001
  return (SNode*)args;
5,322,611✔
UNCOV
2002
_err:
×
UNCOV
2003
  return NULL;
×
2004
}
2005

2006
SNode* createAnomalyWindowNode(SAstCreateContext* pCxt, SNodeList* pExprList) {
×
2007
  SAnomalyWindowNode* pAnomaly = NULL;
×
UNCOV
2008
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
2009
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ANOMALY_WINDOW, (SNode**)&pAnomaly);
×
2010
  CHECK_MAKE_NODE(pAnomaly);
×
2011
  
2012
  pAnomaly->pCol = createPrimaryKeyCol(pCxt, NULL);
×
2013
  CHECK_MAKE_NODE(pAnomaly->pCol);
×
2014

UNCOV
2015
  pAnomaly->pExpr = pExprList;
×
2016

2017
  int32_t len = LIST_LENGTH(pExprList);
×
UNCOV
2018
  if (len == 1) {
×
2019
    tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
2020
  } else {
2021
    SNode* pNode = nodesListGetNode(pExprList, len - 1);
×
2022
    if (pNode != NULL && nodeType(pNode) == QUERY_NODE_VALUE) {
×
2023
      tstrncpy(pAnomaly->anomalyOpt, ((SValueNode*)pNode)->literal, TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
2024

2025
      SListCell* pCell = nodesListGetCell(pExprList, len - 1);
×
2026
      (void)nodesListErase(pExprList, pCell);
×
2027
      nodesDestroyNode(pNode);
×
2028
    } else {
2029
      tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
2030
    }
2031
  }
2032

2033
  return (SNode*)pAnomaly;
×
2034

UNCOV
2035
_err:
×
UNCOV
2036
  nodesDestroyNode((SNode*)pAnomaly);
×
2037
  return NULL;
×
2038
}
2039

2040
SNode* createIntervalWindowNodeExt(SAstCreateContext* pCxt, SNode* pInter, SNode* pSliding) {
239,660✔
2041
  SIntervalWindowNode* pInterval = NULL;
239,660✔
2042
  CHECK_PARSER_STATUS(pCxt);
239,660✔
2043
  if (pInter) {
239,660✔
2044
    pInterval = (SIntervalWindowNode*)pInter;
184,868✔
2045
  } else {
2046
    pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&pInterval);
54,792✔
2047
    CHECK_MAKE_NODE(pInterval);
54,792✔
2048
  }
2049
  pInterval->pCol = createPrimaryKeyCol(pCxt, NULL);
239,660✔
2050
  CHECK_MAKE_NODE(pInterval->pCol);
239,660✔
2051
  pInterval->pSliding = ((SSlidingWindowNode*)pSliding)->pSlidingVal;
239,660✔
2052
  pInterval->pSOffset = ((SSlidingWindowNode*)pSliding)->pOffset;
239,660✔
2053
  return (SNode*)pInterval;
239,660✔
UNCOV
2054
_err:
×
UNCOV
2055
  nodesDestroyNode((SNode*)pInter);
×
UNCOV
2056
  nodesDestroyNode((SNode*)pInterval);
×
UNCOV
2057
  nodesDestroyNode((SNode*)pSliding);
×
2058
  return NULL;
×
2059
}
2060

2061
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
47,123,232✔
2062
                                SNode* pFill) {
2063
  SIntervalWindowNode* interval = NULL;
47,123,232✔
2064
  CHECK_PARSER_STATUS(pCxt);
47,123,232✔
2065
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
47,123,232✔
2066
  CHECK_MAKE_NODE(interval);
47,128,194✔
2067
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
47,128,194✔
2068
  CHECK_MAKE_NODE(interval->pCol);
47,125,628✔
2069
  interval->pInterval = pInterval;
47,125,628✔
2070
  interval->pOffset = pOffset;
47,125,628✔
2071
  interval->pSliding = pSliding;
47,125,628✔
2072
  interval->pFill = pFill;
47,125,628✔
2073
  TAOS_SET_OBJ_ALIGNED(&interval->timeRange, TSWINDOW_INITIALIZER);
47,125,628✔
2074
  interval->timezone = pCxt->pQueryCxt->timezone;
47,125,628✔
2075
  return (SNode*)interval;
47,125,628✔
UNCOV
2076
_err:
×
UNCOV
2077
  nodesDestroyNode((SNode*)interval);
×
UNCOV
2078
  nodesDestroyNode(pInterval);
×
UNCOV
2079
  nodesDestroyNode(pOffset);
×
2080
  nodesDestroyNode(pSliding);
×
2081
  nodesDestroyNode(pFill);
×
2082
  return NULL;
1,909✔
2083
}
2084

2085
SNode* createPeriodWindowNode(SAstCreateContext* pCxt, SNode* pPeriodTime, SNode* pOffset) {
46,318✔
2086
  SPeriodWindowNode* pPeriod = NULL;
46,318✔
2087
  CHECK_PARSER_STATUS(pCxt);
46,318✔
2088
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PERIOD_WINDOW, (SNode**)&pPeriod);
46,318✔
2089
  CHECK_MAKE_NODE(pPeriod);
46,318✔
2090
  pPeriod->pOffset = pOffset;
46,318✔
2091
  pPeriod->pPeroid = pPeriodTime;
46,318✔
2092
  return (SNode*)pPeriod;
46,318✔
UNCOV
2093
_err:
×
UNCOV
2094
  nodesDestroyNode((SNode*)pOffset);
×
UNCOV
2095
  nodesDestroyNode((SNode*)pPeriodTime);
×
UNCOV
2096
  nodesDestroyNode((SNode*)pPeriod);
×
2097
  return NULL;
×
2098
}
2099

2100
SNode* createWindowOffsetNode(SAstCreateContext* pCxt, SNode* pStartOffset, SNode* pEndOffset) {
681,418✔
2101
  SWindowOffsetNode* winOffset = NULL;
681,418✔
2102
  CHECK_PARSER_STATUS(pCxt);
681,418✔
2103
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WINDOW_OFFSET, (SNode**)&winOffset);
681,418✔
2104
  CHECK_MAKE_NODE(winOffset);
681,418✔
2105
  winOffset->pStartOffset = pStartOffset;
681,418✔
2106
  winOffset->pEndOffset = pEndOffset;
681,418✔
2107
  return (SNode*)winOffset;
681,418✔
UNCOV
2108
_err:
×
UNCOV
2109
  nodesDestroyNode((SNode*)winOffset);
×
UNCOV
2110
  nodesDestroyNode(pStartOffset);
×
UNCOV
2111
  nodesDestroyNode(pEndOffset);
×
2112
  return NULL;
×
2113
}
2114

2115
SNode* createSurroundNode(SAstCreateContext* pCxt, SNode* pSurroundingTime,
87,780✔
2116
                          SNode* pValues) {
2117
  SSurroundNode* pSurround = NULL;
87,780✔
2118
  CHECK_PARSER_STATUS(pCxt);
87,780✔
2119
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SURROUND, (SNode**)&pSurround);
87,780✔
2120
  CHECK_MAKE_NODE(pSurround);
87,780✔
2121
  pSurround->pSurroundingTime = pSurroundingTime;
87,780✔
2122
  pSurround->pValues = pValues;
87,780✔
2123
  return (SNode*)pSurround;
87,780✔
UNCOV
2124
_err:
×
UNCOV
2125
  nodesDestroyNode((SNode*)pSurround);
×
UNCOV
2126
  nodesDestroyNode(pSurroundingTime);
×
UNCOV
2127
  nodesDestroyNode(pValues);
×
2128
  return NULL;
×
2129
}
2130

2131
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
4,648,641✔
2132
  return createFillNodeWithSurroundingTime(pCxt, mode, pValues, NULL);
4,648,641✔
2133
}
2134

2135
SNode* createFillNodeWithSurroundNode(SAstCreateContext* pCxt, EFillMode mode,
2,161,344✔
2136
                                      SNode* pSurroundNode) {
2137
  if (pSurroundNode == NULL) {
2,161,344✔
2138
    return createFillNode(pCxt, mode, NULL);
2,073,116✔
2139
  }
2140

2141
  SSurroundNode* pSurround = (SSurroundNode*)pSurroundNode;
88,228✔
2142
  SNode* pSurroundingTime = pSurround->pSurroundingTime;
88,228✔
2143
  SNode* pValues = pSurround->pValues;
87,780✔
2144

2145
  /* set surround node to NULL to avoid freeing before using */
2146
  pSurround->pSurroundingTime = NULL;
87,780✔
2147
  pSurround->pValues = NULL;
87,780✔
2148
  nodesDestroyNode((SNode*)pSurround);
87,780✔
2149

2150
  return createFillNodeWithSurroundingTime(pCxt, mode, pValues,
87,780✔
2151
                                           pSurroundingTime);
2152
}
2153

2154
SNode* createFillNodeWithSurroundingTime(SAstCreateContext* pCxt,
4,736,421✔
2155
                                         EFillMode mode, SNode* pValues,
2156
                                         SNode* pSurroundingTime) {
2157
  SFillNode* fill = NULL;
4,736,421✔
2158
  CHECK_PARSER_STATUS(pCxt);
4,736,869✔
2159
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
4,736,510✔
2160
  CHECK_MAKE_NODE(fill);
4,736,869✔
2161
  fill->mode = mode;
4,736,869✔
2162
  fill->pValues = pValues;
4,737,405✔
2163
  fill->pSurroundingTime = pSurroundingTime;
4,736,509✔
2164
  fill->pWStartTs = NULL;
4,737,045✔
2165
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION,
4,754,211✔
2166
                                (SNode**)&(fill->pWStartTs));
4,735,885✔
2167
  CHECK_MAKE_NODE(fill->pWStartTs);
4,737,941✔
2168
  tstrncpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart",
4,735,525✔
2169
           TSDB_FUNC_NAME_LEN);
2170
  return (SNode*)fill;
4,737,406✔
UNCOV
2171
_err:
×
UNCOV
2172
  nodesDestroyNode((SNode*)fill);
×
UNCOV
2173
  nodesDestroyNode(pValues);
×
UNCOV
2174
  nodesDestroyNode(pSurroundingTime);
×
2175
  return NULL;
×
2176
}
2177

2178
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
63,140,672✔
2179
  SGroupingSetNode* groupingSet = NULL;
63,140,672✔
2180
  CHECK_PARSER_STATUS(pCxt);
63,140,672✔
2181
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
63,140,672✔
2182
  CHECK_MAKE_NODE(groupingSet);
63,143,651✔
2183
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
63,143,651✔
2184
  groupingSet->pParameterList = NULL;
63,143,651✔
2185
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
63,143,651✔
2186
  CHECK_PARSER_STATUS(pCxt);
63,143,321✔
2187
  return (SNode*)groupingSet;
63,143,321✔
UNCOV
2188
_err:
×
UNCOV
2189
  nodesDestroyNode((SNode*)groupingSet);
×
UNCOV
2190
  nodesDestroyNode(pNode);
×
2191
  return NULL;
689✔
2192
}
2193

2194
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
3,847,580✔
2195
  CHECK_PARSER_STATUS(pCxt);
3,847,580✔
2196
  if (isSubQueryNode(pStart) || isSubQueryNode(pEnd) || isSubQueryNode(pInterval)) {
3,847,580✔
2197
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
760✔
2198
    CHECK_PARSER_STATUS(pCxt);
760✔
2199
  }
2200

2201
  if (NULL == pInterval) {
3,846,820✔
2202
    if (pEnd && nodeType(pEnd) == QUERY_NODE_VALUE && ((SValueNode*)pEnd)->flag & VALUE_FLAG_IS_DURATION) {
3,721,350✔
2203
      return createInterpTimeAround(pCxt, pStart, NULL, pEnd);
1,527,294✔
2204
    }
2205
    return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
2,196,112✔
2206
  }
2207

2208
  return createInterpTimeAround(pCxt, pStart, pEnd, pInterval);
125,470✔
2209

2210
_err:
760✔
2211

2212
  nodesDestroyNode(pStart);
760✔
2213
  nodesDestroyNode(pEnd);
760✔
2214
  nodesDestroyNode(pInterval);
760✔
2215
  return NULL;
760✔
2216
}
2217

2218
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
1,668,909✔
2219
  CHECK_PARSER_STATUS(pCxt);
1,668,909✔
2220
  if (isSubQueryNode(pPoint)) {
1,668,909✔
2221
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
380✔
2222
    CHECK_PARSER_STATUS(pCxt);
380✔
2223
  }
2224

2225
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
1,669,601✔
2226
_err:
380✔
2227
  nodesDestroyNode(pPoint);
380✔
2228
  return NULL;
380✔
2229
}
2230

2231
SNode* createInterpTimeAround(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
1,653,300✔
2232
  CHECK_PARSER_STATUS(pCxt);
1,653,300✔
2233
  SRangeAroundNode* pAround = NULL;
1,653,748✔
2234
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RANGE_AROUND, (SNode**)&pAround);
1,653,300✔
2235
  CHECK_PARSER_STATUS(pCxt);
1,654,820✔
2236
  if (NULL == pEnd) {
1,653,748✔
2237
    pAround->pRange = createInterpTimePoint(pCxt, pStart);
1,528,278✔
2238
  } else {
2239
    pAround->pRange = createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
125,470✔
2240
  }
2241
  pAround->pInterval = pInterval;
1,654,372✔
2242
  CHECK_PARSER_STATUS(pCxt);
1,652,852✔
2243
  return (SNode*)pAround;
1,653,388✔
UNCOV
2244
_err:
×
UNCOV
2245
  return NULL;
×
2246
}
2247

2248
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
37,745,232✔
2249
  CHECK_PARSER_STATUS(pCxt);
37,745,232✔
2250
  SWhenThenNode* pWhenThen = NULL;
37,745,232✔
2251
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
37,745,232✔
2252
  CHECK_MAKE_NODE(pWhenThen);
37,748,123✔
2253
  pWhenThen->pWhen = pWhen;
37,748,123✔
2254
  pWhenThen->pThen = pThen;
37,748,123✔
2255
  return (SNode*)pWhenThen;
37,748,123✔
UNCOV
2256
_err:
×
UNCOV
2257
  nodesDestroyNode(pWhen);
×
UNCOV
2258
  nodesDestroyNode(pThen);
×
UNCOV
2259
  return NULL;
×
2260
}
2261

2262
static int32_t debugPrintNode(SNode* pNode) {
×
2263
  char*   pStr = NULL;
×
UNCOV
2264
  int32_t code = nodesNodeToString(pNode, false, &pStr, NULL);
×
UNCOV
2265
  if (TSDB_CODE_SUCCESS == code) {
×
2266
    (void)printf("%s\n", pStr);
×
2267
    taosMemoryFree(pStr);
×
2268
  }
2269
  return code;
×
2270
}
2271

2272
SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
36,840,647✔
2273
  CHECK_PARSER_STATUS(pCxt);
36,840,647✔
2274
  SCaseWhenNode* pCaseWhen = NULL;
36,840,647✔
2275
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CASE_WHEN, (SNode**)&pCaseWhen);
36,840,647✔
2276
  CHECK_MAKE_NODE(pCaseWhen);
36,845,863✔
2277
  pCaseWhen->pCase = pCase;
36,845,863✔
2278
  pCaseWhen->pWhenThenList = pWhenThenList;
36,845,863✔
2279
  pCaseWhen->pElse = pElse;
36,845,863✔
2280
  pCaseWhen->tz = pCxt->pQueryCxt->timezone;
36,845,863✔
2281
  pCaseWhen->charsetCxt = pCxt->pQueryCxt->charsetCxt;
36,845,863✔
2282
  // debugPrintNode((SNode*)pCaseWhen);
2283
  return (SNode*)pCaseWhen;
36,845,863✔
UNCOV
2284
_err:
×
UNCOV
2285
  nodesDestroyNode(pCase);
×
UNCOV
2286
  nodesDestroyList(pWhenThenList);
×
UNCOV
2287
  nodesDestroyNode(pElse);
×
2288
  return NULL;
×
2289
}
2290

2291
SNode* createNullIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
5,642✔
2292
  SNode *    pCase = NULL, *pEqual = NULL, *pThen = NULL;
5,642✔
2293
  SNode *    pWhenThenNode = NULL, *pElse = NULL;
5,642✔
2294
  SNodeList* pWhenThenList = NULL;
5,642✔
2295
  SNode*     pCaseWhen = NULL;
5,642✔
2296

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

2327
SNode* createIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
75,795✔
2328
  SNode*     pCase = NULL;
75,795✔
2329
  SNode*     pWhenThenNode = NULL;
75,795✔
2330
  SNodeList* pWhenThenList = NULL;
75,795✔
2331
  SNode*     pCaseWhen = NULL;
75,795✔
2332

2333
  CHECK_PARSER_STATUS(pCxt);
75,795✔
2334
  pWhenThenNode = createWhenThenNode(pCxt, pExpr1, pExpr2);
75,795✔
2335
  CHECK_PARSER_STATUS(pCxt);
75,795✔
2336
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
75,795✔
2337
  CHECK_PARSER_STATUS(pCxt);
75,795✔
2338
  pCaseWhen = createCaseWhenNode(pCxt, pCase, pWhenThenList, pExpr3);
75,795✔
2339
  CHECK_PARSER_STATUS(pCxt);
75,795✔
2340
  // debugPrintNode((SNode*)pCaseWhen);
2341
  return (SNode*)pCaseWhen;
75,795✔
UNCOV
2342
_err:
×
UNCOV
2343
  nodesDestroyNode(pCase);
×
UNCOV
2344
  nodesDestroyNode(pWhenThenNode);
×
UNCOV
2345
  nodesDestroyList(pWhenThenList);
×
2346
  return NULL;
×
2347
}
2348

2349
SNode* createNvlNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
15,605✔
2350
  SNode *    pThen = NULL, *pEqual = NULL;
15,605✔
2351
  SNode*     pWhenThenNode = NULL;
15,605✔
2352
  SNodeList* pWhenThenList = NULL;
15,605✔
2353
  SNode*     pCaseWhen = NULL;
15,605✔
2354

2355
  CHECK_PARSER_STATUS(pCxt);
15,605✔
2356
  pEqual = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr1, NULL);
15,605✔
2357
  CHECK_PARSER_STATUS(pCxt);
15,605✔
2358
  pCxt->errCode = nodesCloneNode(pExpr1, &pThen);
15,605✔
2359
  CHECK_PARSER_STATUS(pCxt);
15,605✔
2360
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pThen);
15,605✔
2361
  CHECK_PARSER_STATUS(pCxt);
15,605✔
2362
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
15,605✔
2363
  CHECK_PARSER_STATUS(pCxt);
15,605✔
2364
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, pExpr2);
15,605✔
2365
  CHECK_PARSER_STATUS(pCxt);
15,605✔
2366
  // debugPrintNode((SNode*)pCaseWhen);
2367
  return (SNode*)pCaseWhen;
15,605✔
UNCOV
2368
_err:
×
UNCOV
2369
  nodesDestroyNode(pEqual);
×
UNCOV
2370
  nodesDestroyNode(pThen);
×
UNCOV
2371
  nodesDestroyNode(pWhenThenNode);
×
2372
  nodesDestroyList(pWhenThenList);
×
2373
  return NULL;
×
2374
}
2375

2376
SNode* createNvl2Node(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
5,638✔
2377
  SNode *    pEqual = NULL, *pWhenThenNode = NULL;
5,638✔
2378
  SNodeList* pWhenThenList = NULL;
5,638✔
2379
  SNode*     pCaseWhen = NULL;
5,638✔
2380

2381
  CHECK_PARSER_STATUS(pCxt);
5,638✔
2382
  pEqual = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr1, NULL);
5,638✔
2383
  CHECK_PARSER_STATUS(pCxt);
5,638✔
2384
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pExpr2);
5,638✔
2385
  CHECK_PARSER_STATUS(pCxt);
5,638✔
2386
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
5,638✔
2387
  CHECK_PARSER_STATUS(pCxt);
5,638✔
2388
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, pExpr3);
5,638✔
2389
  CHECK_PARSER_STATUS(pCxt);
5,638✔
2390
  // debugPrintNode((SNode*)pCaseWhen);
2391
  return (SNode*)pCaseWhen;
5,638✔
UNCOV
2392
_err:
×
UNCOV
2393
  nodesDestroyNode(pEqual);
×
UNCOV
2394
  nodesDestroyNode(pWhenThenNode);
×
UNCOV
2395
  nodesDestroyList(pWhenThenList);
×
2396
  return NULL;
×
2397
}
2398

2399
SNode* createCoalesceNode(SAstCreateContext* pCxt, SNodeList* pParamList) {
3,204✔
2400
  int32_t    sizeParam = LIST_LENGTH(pParamList);
3,204✔
2401
  SNode *    pNotNullCond = NULL, *pWhenThenNode = NULL, *pExpr = NULL;
3,204✔
2402
  SNodeList* pWhenThenList = NULL;
3,204✔
2403
  SNode *    pCaseWhen = NULL, *pThen = NULL;
3,204✔
2404

2405
  CHECK_PARSER_STATUS(pCxt);
3,204✔
2406

2407
  for (int i = 0; i < sizeParam; ++i) {
10,613✔
2408
    pExpr = nodesListGetNode(pParamList, i);
7,409✔
2409

2410
    pCxt->errCode = nodesCloneNode(pExpr, &pThen);
7,409✔
2411
    CHECK_PARSER_STATUS(pCxt);
7,409✔
2412

2413
    pNotNullCond = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr, NULL);
7,409✔
2414
    CHECK_PARSER_STATUS(pCxt);
7,409✔
2415

2416
    pWhenThenNode = createWhenThenNode(pCxt, pNotNullCond, pThen);
7,409✔
2417
    CHECK_PARSER_STATUS(pCxt);
7,409✔
2418

2419
    if (!pWhenThenList) {
7,409✔
2420
      pWhenThenList = createNodeList(pCxt, pWhenThenNode);
3,204✔
2421
    } else {
2422
      pCxt->errCode = nodesListAppend(pWhenThenList, pWhenThenNode);
4,205✔
2423
    }
2424
    CHECK_PARSER_STATUS(pCxt);
7,409✔
2425
  }
2426

2427
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, NULL);
3,204✔
2428
  CHECK_PARSER_STATUS(pCxt);
3,204✔
2429
  // debugPrintNode((SNode*)pCaseWhen);
2430
  return (SNode*)pCaseWhen;
3,204✔
UNCOV
2431
_err:
×
UNCOV
2432
  nodesDestroyNode(pExpr);
×
UNCOV
2433
  nodesDestroyNode(pNotNullCond);
×
UNCOV
2434
  nodesDestroyNode(pThen);
×
2435
  nodesDestroyNode(pWhenThenNode);
×
2436
  nodesDestroyList(pWhenThenList);
×
2437
  return NULL;
×
2438
}
2439

2440
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
100,986,415✔
2441
  CHECK_PARSER_STATUS(pCxt);
100,986,415✔
2442
  trimEscape(pCxt, pAlias, false);
100,986,415✔
2443
  SExprNode* pExpr = (SExprNode*)pNode;
100,987,405✔
2444
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
100,987,405✔
2445
  tstrncpy(pExpr->aliasName, pAlias->z, len + 1);
100,987,405✔
2446
  tstrncpy(pExpr->userAlias, pAlias->z, len + 1);
100,987,405✔
2447
  pExpr->asAlias = true;
100,987,405✔
2448
  return pNode;
100,987,405✔
UNCOV
2449
_err:
×
UNCOV
2450
  nodesDestroyNode(pNode);
×
UNCOV
2451
  return NULL;
×
2452
}
2453

2454
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
786,868,624✔
2455
  CHECK_PARSER_STATUS(pCxt);
786,868,624✔
2456
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
786,869,529✔
2457
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
786,903,161✔
2458
  }
2459
  return pStmt;
786,869,962✔
2460
_err:
1,358✔
2461
  nodesDestroyNode(pStmt);
1,358✔
2462
  nodesDestroyNode(pWhere);
1,358✔
2463
  return NULL;
1,358✔
2464
}
2465

2466
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
786,880,077✔
2467
  CHECK_PARSER_STATUS(pCxt);
786,880,077✔
2468
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
786,879,594✔
2469
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
786,898,694✔
2470
  }
2471
  return pStmt;
786,880,488✔
2472
_err:
1,358✔
2473
  nodesDestroyNode(pStmt);
1,358✔
2474
  nodesDestroyList(pPartitionByList);
1,358✔
2475
  return NULL;
1,358✔
2476
}
2477

2478
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
786,871,146✔
2479
  CHECK_PARSER_STATUS(pCxt);
786,871,146✔
2480
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
786,872,810✔
2481
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
786,884,026✔
2482
  }
2483
  return pStmt;
786,864,078✔
2484
_err:
1,358✔
2485
  nodesDestroyNode(pStmt);
1,358✔
2486
  nodesDestroyNode(pWindow);
1,358✔
2487
  return NULL;
1,358✔
2488
}
2489

2490
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
786,865,347✔
2491
  CHECK_PARSER_STATUS(pCxt);
786,865,347✔
2492
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
786,865,261✔
2493
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
786,884,372✔
2494
  }
2495
  return pStmt;
786,864,782✔
2496
_err:
1,358✔
2497
  nodesDestroyNode(pStmt);
1,358✔
2498
  nodesDestroyList(pGroupByList);
1,358✔
2499
  return NULL;
1,358✔
2500
}
2501

2502
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
786,858,605✔
2503
  CHECK_PARSER_STATUS(pCxt);
786,858,605✔
2504
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
786,859,396✔
2505
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
786,884,297✔
2506
  }
2507
  return pStmt;
786,859,631✔
2508
_err:
1,358✔
2509
  nodesDestroyNode(pStmt);
1,358✔
2510
  nodesDestroyNode(pHaving);
1,358✔
2511
  return NULL;
1,358✔
2512
}
2513

2514
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
747,504,234✔
2515
  CHECK_PARSER_STATUS(pCxt);
747,504,234✔
2516
  if (NULL == pOrderByList) {
747,504,014✔
2517
    return pStmt;
590,826,748✔
2518
  }
2519
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
156,677,266✔
2520
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
141,133,983✔
2521
  } else {
2522
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
15,543,283✔
2523
  }
2524
  return pStmt;
156,676,818✔
2525
_err:
1,358✔
2526
  nodesDestroyNode(pStmt);
1,358✔
2527
  nodesDestroyList(pOrderByList);
1,358✔
2528
  return NULL;
1,358✔
2529
}
2530

2531
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
747,507,307✔
2532
  CHECK_PARSER_STATUS(pCxt);
747,507,307✔
2533
  if (NULL == pSlimit) {
747,509,857✔
2534
    return pStmt;
744,955,285✔
2535
  }
2536
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
2,561,856✔
2537
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
2,564,326✔
2538
  }
2539
  return pStmt;
2,561,856✔
2540
_err:
1,358✔
2541
  nodesDestroyNode(pStmt);
1,358✔
2542
  nodesDestroyNode(pSlimit);
1,358✔
2543
  return NULL;
1,358✔
2544
}
2545

2546
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
747,503,456✔
2547
  CHECK_PARSER_STATUS(pCxt);
747,503,456✔
2548
  if (NULL == pLimit) {
747,504,421✔
2549
    return pStmt;
717,746,154✔
2550
  }
2551
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
29,758,267✔
2552
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
28,607,679✔
2553
  } else {
2554
    ((SSetOperator*)pStmt)->pLimit = pLimit;
1,160,772✔
2555
  }
2556
  return pStmt;
29,758,267✔
2557
_err:
1,358✔
2558
  nodesDestroyNode(pStmt);
1,358✔
2559
  nodesDestroyNode(pLimit);
1,358✔
2560
  return NULL;
1,358✔
2561
}
2562

2563
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
786,849,570✔
2564
  CHECK_PARSER_STATUS(pCxt);
786,849,570✔
2565
  SSelectStmt* pSelect = (SSelectStmt*)pStmt;
786,847,990✔
2566
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
786,847,990✔
2567
    if (pRange && nodeType(pRange) == QUERY_NODE_RANGE_AROUND) {
786,880,353✔
2568
      pSelect->pRangeAround = pRange;
1,647,497✔
2569
      SRangeAroundNode* pAround = (SRangeAroundNode*)pRange;
1,647,049✔
2570
      TSWAP(pSelect->pRange, pAround->pRange);
1,647,049✔
2571
    } else {
2572
      pSelect->pRange = pRange;
785,229,992✔
2573
    }
2574
  }
2575
  return pStmt;
786,851,653✔
2576
_err:
1,358✔
2577
  nodesDestroyNode(pStmt);
1,358✔
2578
  nodesDestroyNode(pRange);
1,358✔
2579
  return NULL;
1,358✔
2580
}
2581

2582
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
786,863,244✔
2583
  CHECK_PARSER_STATUS(pCxt);
786,863,244✔
2584
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
786,863,047✔
2585
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
786,868,684✔
2586
  }
2587
  return pStmt;
786,860,277✔
2588
_err:
1,358✔
2589
  nodesDestroyNode(pStmt);
1,358✔
2590
  nodesDestroyNode(pEvery);
1,358✔
2591
  return NULL;
1,358✔
2592
}
2593

2594
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
786,853,387✔
2595
  CHECK_PARSER_STATUS(pCxt);
786,853,387✔
2596
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
786,851,142✔
2597
    SFillNode* pFillClause = (SFillNode*)pFill;
3,996,707✔
2598
    nodesDestroyNode(pFillClause->pWStartTs);
3,996,707✔
2599
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
3,996,708✔
2600
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
3,997,243✔
2601
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
3,996,259✔
2602
  }
2603
  return pStmt;
786,853,433✔
2604
_err:
1,358✔
2605
  nodesDestroyNode(pStmt);
1,358✔
2606
  nodesDestroyNode(pFill);
1,358✔
2607
  return NULL;
1,358✔
2608
}
2609

2610
SNode* createExternalWindowClause(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pAlias, SNode* pFill) {
188,445✔
2611
  SExternalWindowNode* pExtWin = NULL;
188,445✔
2612
  CHECK_PARSER_STATUS(pCxt);
188,445✔
2613
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXTERNAL_WINDOW, (SNode**)&pExtWin);
188,445✔
2614
  CHECK_MAKE_NODE(pExtWin);
188,445✔
2615

2616
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
188,445✔
2617
    ((SSelectStmt*)pSubquery)->subQType= E_SUB_QUERY_TABLE;
188,027✔
2618
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
418✔
2619
    ((SSetOperator*)pSubquery)->subQType= E_SUB_QUERY_TABLE;
418✔
2620
  }
2621

2622
  pExtWin->pCol = createPrimaryKeyCol(pCxt, NULL);
188,445✔
2623
  CHECK_MAKE_NODE(pExtWin->pCol);
188,445✔
2624

2625
  if (NULL != pFill) {
188,445✔
2626
    SFillNode* pFillClause = (SFillNode*)pFill;
38,476✔
2627
    nodesDestroyNode(pFillClause->pWStartTs);
38,476✔
2628
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
38,476✔
2629
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
38,476✔
2630
  }
2631

2632
  // Attach subquery and optional fill node
2633
  pExtWin->pSubquery = pSubquery;
188,445✔
2634
  pExtWin->pFill = pFill;
188,445✔
2635

2636
  // Set alias if provided; enforce length constraint (report error if too long)
2637
  pExtWin->aliasName[0] = '\0';
188,445✔
2638
  if (pAlias && pAlias->type != TK_NK_NIL) {
188,445✔
2639
    trimEscape(pCxt, pAlias, false);
188,445✔
2640
    if (pAlias->n >= TSDB_COL_NAME_LEN || pAlias->n == 0) {
188,445✔
UNCOV
2641
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pAlias->z);
×
UNCOV
2642
      goto _err;
×
2643
    }
2644
    int32_t len = pAlias->n;
188,445✔
2645
    strncpy(pExtWin->aliasName, pAlias->z, len);
188,445✔
2646
    pExtWin->aliasName[len] = '\0';
188,445✔
2647
  }
2648

2649
  return (SNode*)pExtWin;
188,445✔
UNCOV
2650
_err:
×
UNCOV
2651
  if (pExtWin) {
×
UNCOV
2652
    pExtWin->pSubquery = NULL;
×
UNCOV
2653
    pExtWin->pFill = NULL;
×
2654
    nodesDestroyNode((SNode*)pExtWin);
×
2655
  }
2656
  nodesDestroyNode(pSubquery);
×
2657
  nodesDestroyNode(pFill);
×
2658
  return NULL;
×
2659
}
2660

2661
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
40,214,834✔
2662
  CHECK_PARSER_STATUS(pCxt);
40,214,834✔
2663
  if (NULL == pJLimit) {
40,214,834✔
2664
    return pJoin;
39,979,942✔
2665
  }
2666
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
234,892✔
2667
  pJoinNode->pJLimit = pJLimit;
234,892✔
2668

2669
  return pJoin;
234,892✔
UNCOV
2670
_err:
×
UNCOV
2671
  nodesDestroyNode(pJoin);
×
UNCOV
2672
  nodesDestroyNode(pJLimit);
×
UNCOV
2673
  return NULL;
×
2674
}
2675

2676
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
40,213,914✔
2677
  CHECK_PARSER_STATUS(pCxt);
40,213,914✔
2678
  if (NULL == pWinOffset) {
40,213,914✔
2679
    return pJoin;
39,545,322✔
2680
  }
2681
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
669,891✔
2682
  pJoinNode->pWindowOffset = pWinOffset;
669,891✔
2683

2684
  return pJoin;
669,891✔
UNCOV
2685
_err:
×
UNCOV
2686
  nodesDestroyNode(pJoin);
×
UNCOV
2687
  nodesDestroyNode(pWinOffset);
×
UNCOV
2688
  return NULL;
×
2689
}
2690

2691
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
786,884,588✔
2692
                        SNodeList* pHint) {
2693
  CHECK_PARSER_STATUS(pCxt);
786,884,588✔
2694
  SNode* select = NULL;
786,885,853✔
2695
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
786,885,216✔
2696
  CHECK_MAKE_NODE(select);
786,893,269✔
2697
  return select;
786,893,269✔
2698
_err:
1,358✔
2699
  nodesDestroyList(pProjectionList);
1,358✔
2700
  nodesDestroyNode(pTable);
1,358✔
2701
  nodesDestroyList(pHint);
1,358✔
2702
  return NULL;
10,574✔
2703
}
2704

2705
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
786,884,779✔
2706
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
786,884,779✔
2707
    if (pCxt->pQueryCxt->biMode) {
786,907,382✔
2708
      ((SSelectStmt*)pStmt)->tagScan = true;
8,388✔
2709
    } else {
2710
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
786,894,576✔
2711
    }
2712
  }
2713
  return pStmt;
786,879,944✔
2714
}
2715

2716
static void setSubquery(SNode* pStmt) {
95,206,144✔
2717
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
95,206,144✔
2718
    ((SSelectStmt*)pStmt)->isSubquery = true;
94,894,143✔
2719
  }
2720
}
95,206,144✔
2721

2722
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
47,602,599✔
2723
  CHECK_PARSER_STATUS(pCxt);
47,602,599✔
2724
  SSetOperator* setOp = NULL;
47,602,599✔
2725
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
47,602,599✔
2726
  CHECK_MAKE_NODE(setOp);
47,604,560✔
2727
  setOp->opType = type;
47,604,560✔
2728
  setOp->pLeft = pLeft;
47,604,560✔
2729
  setSubquery(setOp->pLeft);
47,604,560✔
2730
  setOp->pRight = pRight;
47,604,385✔
2731
  setSubquery(setOp->pRight);
47,604,385✔
2732
  snprintf(setOp->stmtName, TSDB_TABLE_NAME_LEN, "%p", setOp);
47,604,613✔
2733
  return (SNode*)setOp;
47,604,613✔
UNCOV
2734
_err:
×
UNCOV
2735
  nodesDestroyNode(pLeft);
×
UNCOV
2736
  nodesDestroyNode(pRight);
×
2737
  return NULL;
463✔
2738
}
2739

2740
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
2,294,506✔
2741
  if (!pOptions->walRetentionPeriodIsSet) {
2,294,506✔
2742
    pOptions->walRetentionPeriod =
2,283,824✔
2743
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
2744
  }
2745
  if (!pOptions->walRetentionSizeIsSet) {
2,294,506✔
2746
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
2,293,731✔
2747
  }
2748
  if (!pOptions->walRollPeriodIsSet) {
2,294,506✔
2749
    pOptions->walRollPeriod =
2,294,506✔
2750
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
2751
  }
2752
}
2,294,506✔
2753

2754
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
1,729,553✔
2755
  CHECK_PARSER_STATUS(pCxt);
1,729,553✔
2756
  SDatabaseOptions* pOptions = NULL;
1,729,553✔
2757
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
1,729,553✔
2758
  CHECK_MAKE_NODE(pOptions);
1,729,553✔
2759
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
1,729,553✔
2760
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
1,729,553✔
2761
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
1,729,553✔
2762
  pOptions->cacheLastShardBits = -1;  // -1 means auto-calculate
1,729,553✔
2763
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
1,729,553✔
2764
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
1,729,553✔
2765
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
1,729,553✔
2766
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
1,729,553✔
2767
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
1,729,553✔
2768
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
1,729,553✔
2769
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
1,729,553✔
2770
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
1,729,553✔
2771
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
1,729,553✔
2772
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
1,729,553✔
2773
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
1,729,553✔
2774
  pOptions->precision = TSDB_DEFAULT_PRECISION;
1,729,553✔
2775
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
1,729,553✔
2776
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
1,729,553✔
2777
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
1,729,553✔
2778
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
1,729,553✔
2779
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
1,729,553✔
2780
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
1,729,553✔
2781
  updateWalOptionsDefault(pOptions);
1,729,553✔
2782
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
1,729,553✔
2783
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
1,729,553✔
2784
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
1,729,553✔
2785
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
1,729,553✔
2786
  pOptions->ssChunkSize = TSDB_DEFAULT_SS_CHUNK_SIZE;
1,729,553✔
2787
  pOptions->ssKeepLocal = TSDB_DEFAULT_SS_KEEP_LOCAL;
1,729,553✔
2788
  pOptions->ssCompact = TSDB_DEFAULT_SS_COMPACT;
1,729,553✔
2789
  pOptions->withArbitrator = TSDB_DEFAULT_DB_WITH_ARBITRATOR;
1,729,553✔
2790
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
1,729,553✔
2791
  pOptions->dnodeListStr[0] = 0;
1,729,553✔
2792
  pOptions->compactInterval = TSDB_DEFAULT_COMPACT_INTERVAL;
1,729,553✔
2793
  pOptions->compactStartTime = TSDB_DEFAULT_COMPACT_START_TIME;
1,729,553✔
2794
  pOptions->compactEndTime = TSDB_DEFAULT_COMPACT_END_TIME;
1,729,553✔
2795
  pOptions->compactTimeOffset = TSDB_DEFAULT_COMPACT_TIME_OFFSET;
1,729,553✔
2796
  pOptions->encryptAlgorithmStr[0] = 0;
1,729,553✔
2797
  pOptions->isAudit = 0;
1,729,553✔
2798
  pOptions->secureDelete = 0;
1,729,553✔
2799
  pOptions->allowDrop = INT8_MIN;  // -1 means not set
1,729,553✔
2800
  pOptions->securityLevel = -1;  // -1 means "not specified"
1,729,553✔
2801
  return (SNode*)pOptions;
1,729,553✔
UNCOV
2802
_err:
×
UNCOV
2803
  return NULL;
×
2804
}
2805

2806
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
243,472✔
2807
  CHECK_PARSER_STATUS(pCxt);
243,472✔
2808
  SDatabaseOptions* pOptions = NULL;
243,472✔
2809
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
243,472✔
2810
  CHECK_MAKE_NODE(pOptions);
243,472✔
2811
  pOptions->buffer = -1;
243,472✔
2812
  pOptions->cacheModel = -1;
243,472✔
2813
  pOptions->cacheLastSize = -1;
243,472✔
2814
  pOptions->cacheLastShardBits = -2;  // -2 means not set, -1 means auto-calculate
243,472✔
2815
  pOptions->compressionLevel = -1;
243,472✔
2816
  pOptions->daysPerFile = -1;
243,472✔
2817
  pOptions->fsyncPeriod = -1;
243,472✔
2818
  pOptions->maxRowsPerBlock = -1;
243,472✔
2819
  pOptions->minRowsPerBlock = -1;
243,472✔
2820
  pOptions->keep[0] = -1;
243,472✔
2821
  pOptions->keep[1] = -1;
243,472✔
2822
  pOptions->keep[2] = -1;
243,472✔
2823
  pOptions->pages = -1;
243,472✔
2824
  pOptions->pagesize = -1;
243,472✔
2825
  pOptions->tsdbPageSize = -1;
243,472✔
2826
  pOptions->precision = -1;
243,472✔
2827
  pOptions->replica = -1;
243,472✔
2828
  pOptions->strict = -1;
243,472✔
2829
  pOptions->walLevel = -1;
243,472✔
2830
  pOptions->numOfVgroups = -1;
243,472✔
2831
  pOptions->singleStable = -1;
243,472✔
2832
  pOptions->schemaless = -1;
243,472✔
2833
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
243,472✔
2834
  pOptions->walRetentionSize = -2;    // -1 is a valid value
243,472✔
2835
  pOptions->walRollPeriod = -1;
243,472✔
2836
  pOptions->walSegmentSize = -1;
243,472✔
2837
  pOptions->sstTrigger = -1;
243,472✔
2838
  pOptions->tablePrefix = -1;
243,472✔
2839
  pOptions->tableSuffix = -1;
243,472✔
2840
  pOptions->ssChunkSize = -1;
243,472✔
2841
  pOptions->ssKeepLocal = -1;
243,472✔
2842
  pOptions->ssCompact = -1;
243,472✔
2843
  pOptions->withArbitrator = -1;
243,472✔
2844
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
243,472✔
2845
  pOptions->dnodeListStr[0] = 0;
243,472✔
2846
  pOptions->compactInterval = -1;
243,472✔
2847
  pOptions->compactStartTime = -1;
243,472✔
2848
  pOptions->compactEndTime = -1;
243,472✔
2849
  pOptions->compactTimeOffset = -1;
243,472✔
2850
  pOptions->encryptAlgorithmStr[0] = 0;
243,472✔
2851
  pOptions->isAudit = -1;
243,472✔
2852
  pOptions->allowDrop = -1;
243,472✔
2853
  pOptions->secureDelete = -1;
243,472✔
2854
  pOptions->securityLevel = -1;
243,472✔
2855
  return (SNode*)pOptions;
243,472✔
UNCOV
2856
_err:
×
UNCOV
2857
  return NULL;
×
2858
}
2859

2860
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
2,787,552✔
2861
                                    bool alter) {
2862
  CHECK_PARSER_STATUS(pCxt);
2,787,552✔
2863
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
2,787,552✔
2864
  switch (type) {
2,787,552✔
2865
    case DB_OPTION_BUFFER:
69,393✔
2866
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
69,393✔
2867
      break;
69,393✔
2868
    case DB_OPTION_CACHEMODEL:
88,431✔
2869
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
88,431✔
2870
      break;
88,431✔
2871
    case DB_OPTION_CACHESIZE:
29,031✔
2872
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
29,031✔
2873
      break;
29,031✔
2874
    case DB_OPTION_CACHESHARDBITS:
18,030✔
2875
      pDbOptions->cacheLastShardBits = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
18,030✔
2876
      break;
18,030✔
2877
    case DB_OPTION_COMP:
19,270✔
2878
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
19,270✔
2879
      break;
19,270✔
2880
    case DB_OPTION_DAYS: {
332,740✔
2881
      SToken* pToken = pVal;
332,740✔
2882
      if (TK_NK_INTEGER == pToken->type) {
332,740✔
2883
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
267,203✔
2884
      } else {
2885
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
65,537✔
2886
      }
2887
      break;
332,740✔
2888
    }
2889
    case DB_OPTION_FSYNC:
38,350✔
2890
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
38,350✔
2891
      break;
38,350✔
2892
    case DB_OPTION_MAXROWS:
35,517✔
2893
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
35,517✔
2894
      break;
35,517✔
2895
    case DB_OPTION_MINROWS:
42,130✔
2896
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
42,130✔
2897
      break;
42,130✔
2898
    case DB_OPTION_KEEP:
226,903✔
2899
      pDbOptions->pKeep = pVal;
226,903✔
2900
      break;
226,903✔
2901
    case DB_OPTION_PAGES:
18,564✔
2902
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
18,564✔
2903
      break;
18,564✔
2904
    case DB_OPTION_PAGESIZE:
6,636✔
2905
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
6,636✔
2906
      break;
6,636✔
2907
    case DB_OPTION_TSDB_PAGESIZE:
5,178✔
2908
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5,178✔
2909
      break;
5,178✔
2910
    case DB_OPTION_PRECISION:
152,140✔
2911
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
152,140✔
2912
      break;
152,140✔
2913
    case DB_OPTION_REPLICA:
578,407✔
2914
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
578,407✔
2915
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
578,407✔
2916
      if (!alter) {
578,407✔
2917
        updateWalOptionsDefault(pDbOptions);
564,953✔
2918
      }
2919
      break;
578,407✔
UNCOV
2920
    case DB_OPTION_STRICT:
×
UNCOV
2921
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
UNCOV
2922
      break;
×
2923
    case DB_OPTION_WAL:
52,931✔
2924
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
52,931✔
2925
      break;
52,931✔
2926
    case DB_OPTION_VGROUPS:
625,350✔
2927
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
625,350✔
2928
      break;
625,350✔
2929
    case DB_OPTION_SINGLE_STABLE:
8,139✔
2930
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
8,139✔
2931
      break;
8,139✔
2932
    case DB_OPTION_RETENTIONS:
5,572✔
2933
      pDbOptions->pRetentions = pVal;
5,572✔
2934
      break;
5,572✔
2935
    case DB_OPTION_WAL_RETENTION_PERIOD:
97,739✔
2936
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
97,739✔
2937
      pDbOptions->walRetentionPeriodIsSet = true;
97,739✔
2938
      break;
97,739✔
2939
    case DB_OPTION_WAL_RETENTION_SIZE:
21,160✔
2940
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
21,160✔
2941
      pDbOptions->walRetentionSizeIsSet = true;
21,160✔
2942
      break;
21,160✔
2943
    case DB_OPTION_WAL_ROLL_PERIOD:
796✔
2944
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
796✔
2945
      pDbOptions->walRollPeriodIsSet = true;
796✔
2946
      break;
796✔
2947
    case DB_OPTION_WAL_SEGMENT_SIZE:
876✔
2948
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
876✔
2949
      break;
876✔
2950
    case DB_OPTION_STT_TRIGGER:
47,308✔
2951
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
47,308✔
2952
      break;
47,308✔
2953
    case DB_OPTION_TABLE_PREFIX: {
18,389✔
2954
      SValueNode* pNode = (SValueNode*)pVal;
18,389✔
2955
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
18,389✔
2956
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
18,389✔
2957
      } else {
UNCOV
2958
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
UNCOV
2959
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2960
      }
2961
      nodesDestroyNode((SNode*)pNode);
18,389✔
2962
      break;
18,389✔
2963
    }
2964
    case DB_OPTION_TABLE_SUFFIX: {
17,787✔
2965
      SValueNode* pNode = (SValueNode*)pVal;
17,787✔
2966
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
17,787✔
2967
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
17,787✔
2968
      } else {
UNCOV
2969
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
UNCOV
2970
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2971
      }
2972
      nodesDestroyNode((SNode*)pNode);
17,787✔
2973
      break;
17,787✔
2974
    }
2975
    case DB_OPTION_SS_CHUNKPAGES:
5,068✔
2976
      pDbOptions->ssChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5,068✔
2977
      break;
5,068✔
2978
    case DB_OPTION_SS_KEEPLOCAL: {
5,731✔
2979
      SToken* pToken = pVal;
5,731✔
2980
      if (TK_NK_INTEGER == pToken->type) {
5,731✔
2981
        pDbOptions->ssKeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
1,283✔
2982
      } else {
2983
        pDbOptions->ssKeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
4,448✔
2984
      }
2985
      break;
5,731✔
2986
    }
2987
    case DB_OPTION_SS_COMPACT:
6,026✔
2988
      pDbOptions->ssCompact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
6,026✔
2989
      break;
6,026✔
2990
    case DB_OPTION_KEEP_TIME_OFFSET:
12,061✔
2991
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
12,061✔
2992
        pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
6,973✔
2993
      } else {
2994
        pDbOptions->pKeepTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
5,088✔
2995
      }
2996
      break;
12,061✔
2997
    case DB_OPTION_ENCRYPT_ALGORITHM:
11,186✔
2998
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
11,186✔
2999
      if (strlen(pDbOptions->encryptAlgorithmStr) == 0) pDbOptions->encryptAlgorithm = -1;
11,186✔
3000
      break;
11,186✔
3001
    case DB_OPTION_DNODES:
29,045✔
3002
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
29,045✔
3003
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
789✔
3004
                 TSDB_DNODE_LIST_LEN);
3005
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
789✔
3006
      } else {
3007
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
28,256✔
3008
      }
3009
      break;
29,045✔
3010
    case DB_OPTION_COMPACT_INTERVAL:
34,356✔
3011
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
34,356✔
3012
        pDbOptions->compactInterval = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5,286✔
3013
      } else {
3014
        pDbOptions->pCompactIntervalNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
29,070✔
3015
      }
3016
      break;
34,356✔
3017
    case DB_OPTION_COMPACT_TIME_RANGE:
53,572✔
3018
      pDbOptions->pCompactTimeRangeList = pVal;
53,572✔
3019
      break;
53,572✔
3020
    case DB_OPTION_COMPACT_TIME_OFFSET:
31,541✔
3021
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
31,541✔
3022
        pDbOptions->compactTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
15,459✔
3023
      } else {
3024
        pDbOptions->pCompactTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
16,082✔
3025
      }
3026
      break;
31,541✔
3027
    case DB_OPTION_IS_AUDIT:
4,955✔
3028
      pDbOptions->isAudit = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
4,955✔
3029
      break;
4,955✔
3030
    case DB_OPTION_ALLOW_DROP:
3,039✔
3031
      pDbOptions->allowDrop = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
3,039✔
3032
      if(pDbOptions->allowDrop != 0 && pDbOptions->allowDrop != 1) {
3,039✔
NEW
3033
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "Invalid value for allow_drop, should be 0 or 1");
×
NEW
3034
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3035
      }
3036
      break;
3,039✔
3037
    case DB_OPTION_SECURITY_LEVEL:
5,491✔
3038
      pDbOptions->securityLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
5,491✔
3039
      break;
5,491✔
3040
    case DB_OPTION_SECURE_DELETE:
6,729✔
3041
      pDbOptions->secureDelete = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
6,729✔
3042
      break;
6,729✔
3043
    default:
21,985✔
3044
      break;
21,985✔
3045
  }
3046
  return pOptions;
2,787,552✔
3047
_err:
×
3048
  nodesDestroyNode(pOptions);
×
UNCOV
3049
  return NULL;
×
3050
}
3051

3052
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
2,519,321✔
3053
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
2,519,321✔
3054
}
3055

3056
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
268,231✔
3057
  CHECK_PARSER_STATUS(pCxt);
268,231✔
3058
  switch (pAlterOption->type) {
268,231✔
3059
    case DB_OPTION_KEEP:
78,917✔
3060
    case DB_OPTION_RETENTIONS:
3061
    case DB_OPTION_COMPACT_TIME_RANGE:
3062
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
78,917✔
3063
    default:
189,314✔
3064
      break;
189,314✔
3065
  }
3066
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
189,314✔
UNCOV
3067
_err:
×
UNCOV
3068
  nodesDestroyNode(pOptions);
×
UNCOV
3069
  nodesDestroyList(pAlterOption->pList);
×
UNCOV
3070
  return NULL;
×
3071
}
3072

3073
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
1,693,039✔
3074
  CHECK_PARSER_STATUS(pCxt);
1,693,039✔
3075
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,692,250✔
3076
  SCreateDatabaseStmt* pStmt = NULL;
1,689,981✔
3077
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
1,689,981✔
3078
  CHECK_MAKE_NODE(pStmt);
1,689,981✔
3079
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,689,981✔
3080
  pStmt->ignoreExists = ignoreExists;
1,689,981✔
3081
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
1,689,981✔
3082
  return (SNode*)pStmt;
1,689,981✔
3083
_err:
3,058✔
3084
  nodesDestroyNode(pOptions);
3,058✔
3085
  return NULL;
3,058✔
3086
}
3087

3088
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName, bool force) {
1,433,800✔
3089
  CHECK_PARSER_STATUS(pCxt);
1,433,800✔
3090
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,433,800✔
3091
  SDropDatabaseStmt* pStmt = NULL;
1,433,800✔
3092
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
1,433,800✔
3093
  CHECK_MAKE_NODE(pStmt);
1,433,800✔
3094
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,433,800✔
3095
  pStmt->ignoreNotExists = ignoreNotExists;
1,433,800✔
3096
  pStmt->force = force;
1,433,800✔
3097
  return (SNode*)pStmt;
1,433,800✔
UNCOV
3098
_err:
×
UNCOV
3099
  return NULL;
×
3100
}
3101

3102
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
238,634✔
3103
  CHECK_PARSER_STATUS(pCxt);
238,634✔
3104
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
238,634✔
3105
  SAlterDatabaseStmt* pStmt = NULL;
238,634✔
3106
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
238,634✔
3107
  CHECK_MAKE_NODE(pStmt);
238,634✔
3108
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
238,634✔
3109
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
238,634✔
3110
  return (SNode*)pStmt;
238,634✔
UNCOV
3111
_err:
×
3112
  nodesDestroyNode(pOptions);
×
3113
  return NULL;
×
3114
}
3115

3116
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
1,994,935✔
3117
  CHECK_PARSER_STATUS(pCxt);
1,994,935✔
3118
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,994,935✔
3119
  SFlushDatabaseStmt* pStmt = NULL;
1,994,935✔
3120
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
1,994,935✔
3121
  CHECK_MAKE_NODE(pStmt);
1,994,935✔
3122
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,994,935✔
3123
  return (SNode*)pStmt;
1,994,935✔
UNCOV
3124
_err:
×
3125
  return NULL;
×
3126
}
3127

3128
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
11,101✔
3129
  CHECK_PARSER_STATUS(pCxt);
11,101✔
3130
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
11,101✔
3131
  STrimDatabaseStmt* pStmt = NULL;
11,101✔
3132
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
11,101✔
3133
  CHECK_MAKE_NODE(pStmt);
11,101✔
3134
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
11,101✔
3135
  pStmt->maxSpeed = maxSpeed;
11,101✔
3136
  return (SNode*)pStmt;
11,101✔
UNCOV
3137
_err:
×
3138
  return NULL;
×
3139
}
3140

3141
SNode* createTrimDbWalStmt(SAstCreateContext* pCxt, SToken* pDbName) {
1,202✔
3142
  CHECK_PARSER_STATUS(pCxt);
1,202✔
3143
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,202✔
3144
  STrimDbWalStmt* pStmt = NULL;
1,202✔
3145
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_WAL_STMT, (SNode**)&pStmt);
1,202✔
3146
  CHECK_MAKE_NODE(pStmt);
1,202✔
3147
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,202✔
3148
  return (SNode*)pStmt;
1,202✔
UNCOV
3149
_err:
×
UNCOV
3150
  return NULL;
×
3151
}
3152

3153
SNode* createSsMigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
3,185✔
3154
  CHECK_PARSER_STATUS(pCxt);
3,185✔
3155
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
3,185✔
3156
  SSsMigrateDatabaseStmt* pStmt = NULL;
3,185✔
3157
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SSMIGRATE_DATABASE_STMT, (SNode**)&pStmt);
3,185✔
3158
  CHECK_MAKE_NODE(pStmt);
3,185✔
3159
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
3,185✔
3160
  return (SNode*)pStmt;
3,185✔
UNCOV
3161
_err:
×
UNCOV
3162
  return NULL;
×
3163
}
3164

3165
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd, bool metaOnly,
35,750✔
3166
                         bool force) {
3167
  CHECK_PARSER_STATUS(pCxt);
35,750✔
3168
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
35,750✔
3169
  SCompactDatabaseStmt* pStmt = NULL;
35,750✔
3170
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
35,750✔
3171
  CHECK_MAKE_NODE(pStmt);
35,750✔
3172
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
35,750✔
3173
  pStmt->pStart = pStart;
35,750✔
3174
  pStmt->pEnd = pEnd;
35,750✔
3175
  pStmt->metaOnly = metaOnly;
35,750✔
3176
  pStmt->force = force;
35,750✔
3177
  return (SNode*)pStmt;
35,750✔
UNCOV
3178
_err:
×
UNCOV
3179
  nodesDestroyNode(pStart);
×
UNCOV
3180
  nodesDestroyNode(pEnd);
×
UNCOV
3181
  return NULL;
×
3182
}
3183

3184
SNode* createCreateMountStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pMountName, SToken* pDnodeId,
1,802✔
3185
                             SToken* pMountPath) {
3186
#ifdef USE_MOUNT
3187
  CHECK_PARSER_STATUS(pCxt);
1,802✔
3188
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
1,802✔
3189
  CHECK_NAME(checkMountPath(pCxt, pMountPath));
1,802✔
3190
  SCreateMountStmt* pStmt = NULL;
1,802✔
3191
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MOUNT_STMT, (SNode**)&pStmt);
1,802✔
3192
  CHECK_MAKE_NODE(pStmt);
1,802✔
3193
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
1,802✔
3194
  COPY_STRING_FORM_STR_TOKEN(pStmt->mountPath, pMountPath);
1,802✔
3195
  pStmt->ignoreExists = ignoreExists;
1,802✔
3196
  if (TK_NK_INTEGER == pDnodeId->type) {
1,802✔
3197
    pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1,802✔
3198
  } else {
UNCOV
3199
    goto _err;
×
3200
  }
3201
  return (SNode*)pStmt;
1,802✔
UNCOV
3202
_err:
×
UNCOV
3203
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
3204
  return NULL;
×
3205
#else
3206
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
3207
  return NULL;
3208
#endif
3209
}
3210

3211
SNode* createDropMountStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pMountName) {
596✔
3212
#ifdef USE_MOUNT
3213
  CHECK_PARSER_STATUS(pCxt);
596✔
3214
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
596✔
3215
  SDropMountStmt* pStmt = NULL;
596✔
3216
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_MOUNT_STMT, (SNode**)&pStmt);
596✔
3217
  CHECK_MAKE_NODE(pStmt);
596✔
3218
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
596✔
3219
  pStmt->ignoreNotExists = ignoreNotExists;
596✔
3220
  return (SNode*)pStmt;
596✔
UNCOV
3221
_err:
×
UNCOV
3222
  return NULL;
×
3223
#else
3224
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
3225
  return NULL;
3226
#endif
3227
}
3228

3229
SNode* createCompactVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
4,890✔
3230
                                SNode* pEnd, bool metaOnly, bool force) {
3231
  CHECK_PARSER_STATUS(pCxt);
4,890✔
3232
  if (NULL == pDbName) {
4,890✔
3233
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
815✔
3234
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
815✔
3235
    CHECK_PARSER_STATUS(pCxt);
815✔
3236
  }
3237
  SCompactVgroupsStmt* pStmt = NULL;
4,075✔
3238
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_VGROUPS_STMT, (SNode**)&pStmt);
4,075✔
3239
  CHECK_MAKE_NODE(pStmt);
4,075✔
3240
  pStmt->pDbName = pDbName;
4,075✔
3241
  pStmt->vgidList = vgidList;
4,075✔
3242
  pStmt->pStart = pStart;
4,075✔
3243
  pStmt->pEnd = pEnd;
4,075✔
3244
  pStmt->metaOnly = metaOnly;
4,075✔
3245
  pStmt->force = force;
4,075✔
3246
  return (SNode*)pStmt;
4,075✔
3247
_err:
815✔
3248
  nodesDestroyNode(pDbName);
815✔
3249
  nodesDestroyList(vgidList);
815✔
3250
  nodesDestroyNode(pStart);
815✔
3251
  nodesDestroyNode(pEnd);
815✔
3252
  return NULL;
815✔
3253
}
3254

3255
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
55,325,605✔
3256
  CHECK_PARSER_STATUS(pCxt);
55,325,605✔
3257
  STableOptions* pOptions = NULL;
55,328,405✔
3258
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
55,329,676✔
3259
  CHECK_MAKE_NODE(pOptions);
55,336,851✔
3260
  pOptions->maxDelay1 = -1;
55,336,851✔
3261
  pOptions->maxDelay2 = -1;
55,329,096✔
3262
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
55,329,094✔
3263
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
55,326,613✔
3264
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
55,323,548✔
3265
  pOptions->keep = -1;
55,330,058✔
3266
  pOptions->virtualStb = false;
55,325,394✔
3267
  pOptions->commentNull = true;  // mark null
55,313,859✔
3268
  pOptions->secureDelete = 0;
55,322,576✔
3269
  pOptions->securityLevel = -1;
55,330,106✔
3270
  return (SNode*)pOptions;
55,331,562✔
UNCOV
3271
_err:
×
UNCOV
3272
  return NULL;
×
3273
}
3274

3275
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
82,357✔
3276
  CHECK_PARSER_STATUS(pCxt);
82,357✔
3277
  STableOptions* pOptions = NULL;
82,357✔
3278
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
82,357✔
3279
  CHECK_MAKE_NODE(pOptions);
82,357✔
3280
  pOptions->ttl = -1;
82,357✔
3281
  pOptions->commentNull = true;  // mark null
82,357✔
3282
  pOptions->keep = -1;
82,357✔
3283
  pOptions->secureDelete = -1;
82,357✔
3284
  pOptions->securityLevel = -1;
82,357✔
3285
  return (SNode*)pOptions;
82,357✔
UNCOV
3286
_err:
×
3287
  return NULL;
×
3288
}
3289

3290
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
982,674✔
3291
  CHECK_PARSER_STATUS(pCxt);
982,674✔
3292
  switch (type) {
982,674✔
3293
    case TABLE_OPTION_COMMENT:
58,506✔
3294
      if (checkComment(pCxt, (SToken*)pVal, true)) {
58,506✔
3295
        ((STableOptions*)pOptions)->commentNull = false;
49,354✔
3296
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
49,354✔
3297
      }
3298
      break;
58,506✔
3299
    case TABLE_OPTION_MAXDELAY:
2,388✔
3300
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
2,388✔
3301
      break;
2,388✔
3302
    case TABLE_OPTION_WATERMARK:
2,388✔
3303
      ((STableOptions*)pOptions)->pWatermark = pVal;
2,388✔
3304
      break;
2,388✔
3305
    case TABLE_OPTION_ROLLUP:
3,980✔
3306
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
3,980✔
3307
      break;
3,980✔
3308
    case TABLE_OPTION_TTL: {
118,225✔
3309
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
118,225✔
3310
      if (ttl > INT32_MAX) {
118,225✔
3311
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
801✔
3312
      } else {
3313
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
3314
        ((STableOptions*)pOptions)->ttl = ttl;
117,424✔
3315
      }
3316
      break;
118,225✔
3317
    }
3318
    case TABLE_OPTION_SMA:
617,736✔
3319
      ((STableOptions*)pOptions)->pSma = pVal;
617,736✔
3320
      break;
617,736✔
3321
    case TABLE_OPTION_DELETE_MARK:
796✔
3322
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
796✔
3323
      break;
796✔
3324
    case TABLE_OPTION_KEEP:
80,842✔
3325
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
80,842✔
3326
        ((STableOptions*)pOptions)->keep = taosStr2Int32(((SToken*)pVal)->z, NULL, 10) * 1440;
12,906✔
3327
      } else {
3328
        ((STableOptions*)pOptions)->pKeepNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
67,936✔
3329
      }
3330
      break;
80,842✔
3331
    case TABLE_OPTION_VIRTUAL: {
85,840✔
3332
      int64_t virtualStb = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
85,840✔
3333
      if (virtualStb != 0 && virtualStb != 1) {
85,840✔
UNCOV
3334
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
3335
      } else {
3336
        ((STableOptions*)pOptions)->virtualStb = virtualStb;
85,840✔
3337
      }
3338
      break;
85,840✔
3339
    }
3340
    case TABLE_OPTION_SECURE_DELETE: {
5,904✔
3341
      int64_t secureDelete = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
5,904✔
3342
      if (secureDelete != 0 && secureDelete != 1) {
5,904✔
UNCOV
3343
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
3344
      } else {
3345
        ((STableOptions*)pOptions)->secureDelete = (int8_t)secureDelete;
5,904✔
3346
      }
3347
      break;
5,904✔
3348
    }
3349
    case TABLE_OPTION_SECURITY_LEVEL: {
6,069✔
3350
      int64_t securityLevel = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
6,069✔
3351
      if (securityLevel < TSDB_MIN_SECURITY_LEVEL || securityLevel > TSDB_MAX_SECURITY_LEVEL) {
6,069✔
NEW
3352
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
3353
      } else {
3354
        ((STableOptions*)pOptions)->securityLevel = securityLevel;
6,069✔
3355
      }
3356
      break;
6,069✔
3357
    }
3358
    default:
×
3359
      break;
×
3360
  }
3361
  return pOptions;
982,674✔
UNCOV
3362
_err:
×
UNCOV
3363
  nodesDestroyNode(pOptions);
×
UNCOV
3364
  return NULL;
×
3365
}
3366

3367
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
462,712,361✔
3368
  CHECK_PARSER_STATUS(pCxt);
462,712,361✔
3369
  SColumnOptions* pOptions = NULL;
462,712,361✔
3370
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
462,712,361✔
3371
  CHECK_MAKE_NODE(pOptions);
462,712,361✔
3372
  pOptions->commentNull = true;
462,712,361✔
3373
  pOptions->bPrimaryKey = false;
462,712,361✔
3374
  pOptions->hasRef = false;
462,712,325✔
3375
  return (SNode*)pOptions;
462,712,361✔
UNCOV
3376
_err:
×
3377
  return NULL;
×
3378
}
3379

3380
EColumnOptionType getColumnOptionType(const char* optionType) {
2,404,022✔
3381
  if (0 == strcasecmp(optionType, "ENCODE")) {
2,404,022✔
3382
    return COLUMN_OPTION_ENCODE;
649,025✔
3383
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
1,754,997✔
3384
    return COLUMN_OPTION_COMPRESS;
882,107✔
3385
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
872,890✔
3386
    return COLUMN_OPTION_LEVEL;
870,548✔
3387
  }
3388
  return 0;
2,342✔
3389
}
3390

3391
SNode* setColumnReference(SAstCreateContext* pCxt, SNode* pOptions, SNode* pRef) {
100,731,829✔
3392
  CHECK_PARSER_STATUS(pCxt);
100,731,829✔
3393

3394
  ((SColumnOptions*)pOptions)->hasRef = true;
100,731,829✔
3395
  tstrncpy(((SColumnOptions*)pOptions)->refDb, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
100,731,829✔
3396
  tstrncpy(((SColumnOptions*)pOptions)->refTable, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
100,731,829✔
3397
  tstrncpy(((SColumnOptions*)pOptions)->refColumn, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
100,731,829✔
3398
  return pOptions;
100,731,829✔
UNCOV
3399
_err:
×
UNCOV
3400
  nodesDestroyNode(pOptions);
×
3401
  return NULL;
×
3402
}
3403

3404
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
265,183✔
3405
  CHECK_PARSER_STATUS(pCxt);
265,183✔
3406
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
265,183✔
3407
  return pOptions;
265,183✔
UNCOV
3408
_err:
×
UNCOV
3409
  nodesDestroyNode(pOptions);
×
UNCOV
3410
  return NULL;
×
3411
}
3412

3413
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
2,404,022✔
3414
  CHECK_PARSER_STATUS(pCxt);
2,404,022✔
3415
  char optionType[TSDB_CL_OPTION_LEN];
2,384,948✔
3416

3417
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
2,404,022✔
3418
  tstrncpy(optionType, pVal1->z, pVal1->n < TSDB_CL_OPTION_LEN ? pVal1->n + 1 : TSDB_CL_OPTION_LEN);
2,404,022✔
3419

3420
  if (0 == strlen(optionType)) {
2,404,022✔
UNCOV
3421
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
3422
    return pOptions;
×
3423
  }
3424
  EColumnOptionType type = getColumnOptionType(optionType);
2,404,022✔
3425
  switch (type) {
2,404,022✔
3426
    case COLUMN_OPTION_ENCODE:
649,025✔
3427
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
649,025✔
3428
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
649,025✔
3429
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
649,025✔
UNCOV
3430
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
3431
      }
3432
      break;
649,025✔
3433
    case COLUMN_OPTION_COMPRESS:
882,107✔
3434
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
882,107✔
3435
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
882,107✔
3436
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
882,107✔
UNCOV
3437
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
3438
      }
3439
      break;
882,107✔
3440
    case COLUMN_OPTION_LEVEL:
870,548✔
3441
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
870,548✔
3442
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
870,548✔
3443
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
870,548✔
UNCOV
3444
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
3445
      }
3446
      break;
870,548✔
3447
    default:
2,342✔
3448
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2,342✔
3449
      break;
2,342✔
3450
  }
3451
  return pOptions;
2,404,022✔
UNCOV
3452
_err:
×
UNCOV
3453
  nodesDestroyNode(pOptions);
×
UNCOV
3454
  return NULL;
×
3455
}
3456

3457
SNode* createColumnRefNodeByNode(SAstCreateContext* pCxt, SToken* pColName, SNode* pRef) {
60,967,522✔
3458
  CHECK_PARSER_STATUS(pCxt);
60,967,522✔
3459
  CHECK_NAME(checkColumnName(pCxt, pColName));
60,967,522✔
3460

3461
  SColumnRefNode* pCol = NULL;
60,967,522✔
3462
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
60,967,522✔
3463
  CHECK_MAKE_NODE(pCol);
60,967,522✔
3464
  if (pColName) {
60,967,522✔
3465
    COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
60,967,522✔
3466
  }
3467
  tstrncpy(pCol->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
60,967,522✔
3468
  tstrncpy(pCol->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
60,967,522✔
3469
  tstrncpy(pCol->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
60,967,522✔
3470
  return (SNode*)pCol;
60,967,522✔
UNCOV
3471
_err:
×
UNCOV
3472
  return NULL;
×
3473
}
3474

3475
// Create a SColumnRefNode from db.table.col triplet tokens (for positional tag refs in vtags_literal)
UNCOV
3476
SNode* createColumnRefNodeFromTriplet(SAstCreateContext* pCxt, SToken* pDb, SToken* pTable, SToken* pCol) {
×
3477
  CHECK_PARSER_STATUS(pCxt);
×
3478
  CHECK_NAME(checkDbName(pCxt, pDb, true));
×
3479
  CHECK_NAME(checkTableName(pCxt, pTable));
×
UNCOV
3480
  CHECK_NAME(checkColumnName(pCxt, pCol));
×
3481

UNCOV
3482
  SColumnRefNode* pNode = NULL;
×
UNCOV
3483
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pNode);
×
UNCOV
3484
  CHECK_MAKE_NODE(pNode);
×
UNCOV
3485
  COPY_STRING_FORM_ID_TOKEN(pNode->refDbName, pDb);
×
UNCOV
3486
  COPY_STRING_FORM_ID_TOKEN(pNode->refTableName, pTable);
×
UNCOV
3487
  COPY_STRING_FORM_ID_TOKEN(pNode->refColName, pCol);
×
UNCOV
3488
  return (SNode*)pNode;
×
UNCOV
3489
_err:
×
UNCOV
3490
  return NULL;
×
3491
}
3492

3493
// Create a SColumnRefNode from table.col pair tokens (for positional tag refs in vtags_literal)
UNCOV
3494
SNode* createColumnRefNodeFromPair(SAstCreateContext* pCxt, SToken* pTable, SToken* pCol) {
×
UNCOV
3495
  CHECK_PARSER_STATUS(pCxt);
×
3496
  CHECK_NAME(checkTableName(pCxt, pTable));
×
3497
  CHECK_NAME(checkColumnName(pCxt, pCol));
×
3498

UNCOV
3499
  SColumnRefNode* pNode = NULL;
×
UNCOV
3500
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pNode);
×
3501
  CHECK_MAKE_NODE(pNode);
×
3502
  snprintf(pNode->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
×
3503
  COPY_STRING_FORM_ID_TOKEN(pNode->refTableName, pTable);
×
3504
  COPY_STRING_FORM_ID_TOKEN(pNode->refColName, pCol);
×
3505
  return (SNode*)pNode;
×
UNCOV
3506
_err:
×
3507
  return NULL;
×
3508
}
3509

3510
STokenTriplet* createTokenTriplet(SAstCreateContext* pCxt, SToken pName) {
162,595,029✔
3511
  CHECK_PARSER_STATUS(pCxt);
162,595,029✔
3512

3513
  STokenTriplet* pTokenTri = taosMemoryMalloc(sizeof(STokenTriplet));
162,595,029✔
3514
  CHECK_OUT_OF_MEM(pTokenTri);
162,595,029✔
3515
  pTokenTri->name[0] = pName;
162,595,029✔
3516
  pTokenTri->numOfName = 1;
162,595,029✔
3517

3518
  return pTokenTri;
162,595,029✔
3519
_err:
×
3520
  return NULL;
×
3521
}
3522

3523
STokenTriplet* setColumnName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri, SToken pName) {
163,228,996✔
3524
  CHECK_PARSER_STATUS(pCxt);
163,228,996✔
3525

3526
  if (pTokenTri->numOfName >= 3) {
163,228,996✔
3527
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3528
    goto _err;
×
3529
  }
3530

3531
  pTokenTri->name[pTokenTri->numOfName] = pName;
163,228,996✔
3532
  pTokenTri->numOfName++;
163,228,996✔
3533
  return pTokenTri;
163,228,996✔
UNCOV
3534
_err:
×
UNCOV
3535
  return NULL;
×
3536
}
3537

3538
SNode* createColumnRefNodeByName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri) {
162,595,029✔
3539
  SColumnRefNode* pCol = NULL;
162,595,029✔
3540
  CHECK_PARSER_STATUS(pCxt);
162,595,029✔
3541
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
162,595,029✔
3542
  CHECK_MAKE_NODE(pCol);
162,595,029✔
3543

3544
  switch (pTokenTri->numOfName) {
162,595,029✔
3545
    case 2: {
161,961,062✔
3546
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[0]));
161,961,062✔
3547
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[1]));
161,961,062✔
3548
      snprintf(pCol->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
161,961,062✔
3549
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[0]);
161,961,062✔
3550
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[1]);
161,961,062✔
3551
      break;
161,961,062✔
3552
    }
3553
    case 3: {
633,967✔
3554
      CHECK_NAME(checkDbName(pCxt, &pTokenTri->name[0], true));
633,967✔
3555
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[1]));
633,967✔
3556
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[2]));
633,967✔
3557
      COPY_STRING_FORM_ID_TOKEN(pCol->refDbName, &pTokenTri->name[0]);
633,967✔
3558
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[1]);
633,967✔
3559
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[2]);
633,967✔
3560
      break;
633,967✔
3561
    }
UNCOV
3562
    default: {
×
UNCOV
3563
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
3564
      goto _err;
×
3565
    }
3566
  }
3567

3568
  taosMemFree(pTokenTri);
162,595,029✔
3569
  return (SNode*)pCol;
162,595,029✔
UNCOV
3570
_err:
×
UNCOV
3571
  taosMemFree(pTokenTri);
×
UNCOV
3572
  nodesDestroyNode((SNode*)pCol);
×
UNCOV
3573
  return NULL;
×
3574
}
3575

3576
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
458,635,368✔
3577
  CHECK_PARSER_STATUS(pCxt);
458,635,368✔
3578
  CHECK_NAME(checkColumnName(pCxt, pColName));
458,635,368✔
3579
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
458,632,686✔
3580
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
4,075✔
3581
    CHECK_PARSER_STATUS(pCxt);
4,075✔
3582
  }
3583
  SColumnDefNode* pCol = NULL;
458,628,611✔
3584
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
458,628,611✔
3585
  CHECK_MAKE_NODE(pCol);
458,628,611✔
3586
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
458,628,611✔
3587
  pCol->dataType = dataType;
458,628,611✔
3588
  pCol->pOptions = pNode;
458,628,611✔
3589
  pCol->sma = true;
458,628,611✔
3590
  return (SNode*)pCol;
458,628,611✔
3591
_err:
6,757✔
3592
  nodesDestroyNode(pNode);
6,757✔
3593
  return NULL;
6,757✔
3594
}
3595

3596
SDataType createDataType(uint8_t type) {
464,078,723✔
3597
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
464,078,723✔
3598
  return dt;
464,078,723✔
3599
}
3600

3601
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
121,285,366✔
3602
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
121,285,366✔
3603
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
121,285,366✔
3604
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
121,285,366✔
3605
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
121,285,641✔
3606
  return dt;
121,285,641✔
3607
}
3608

3609
SDataType createDecimalDataType(uint8_t type, const SToken* pPrecisionToken, const SToken* pScaleToken) {
307,385✔
3610
  SDataType dt = {0};
307,385✔
3611
  dt.precision = taosStr2UInt8(pPrecisionToken->z, NULL, 10);
307,385✔
3612
  dt.scale = pScaleToken ? taosStr2Int32(pScaleToken->z, NULL, 10) : 0;
307,385✔
3613
  dt.type = decimalTypeFromPrecision(dt.precision);
307,385✔
3614
  dt.bytes = tDataTypes[dt.type].bytes;
307,385✔
3615
  return dt;
307,385✔
3616
}
3617

3618
SNode* createCreateVTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols) {
234,227✔
3619
  SCreateVTableStmt* pStmt = NULL;
234,227✔
3620
  CHECK_PARSER_STATUS(pCxt);
234,227✔
3621
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
234,227✔
3622
  CHECK_MAKE_NODE(pStmt);
234,227✔
3623
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
234,227✔
3624
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->tableName));
234,227✔
3625
  pStmt->ignoreExists = ignoreExists;
234,227✔
3626
  pStmt->pCols = pCols;
234,227✔
3627
  nodesDestroyNode(pRealTable);
234,227✔
3628
  return (SNode*)pStmt;
234,227✔
UNCOV
3629
_err:
×
UNCOV
3630
  nodesDestroyNode(pRealTable);
×
UNCOV
3631
  nodesDestroyList(pCols);
×
UNCOV
3632
  return NULL;
×
3633
}
3634

3635
SNode* createCreateVSubTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable,
424,847✔
3636
                                 SNodeList* pSpecificColRefs, SNodeList* pColRefs, SNode* pUseRealTable,
3637
                                 SNodeList* pSpecificTags, SNodeList* pValsOfTags,
3638
                                 SNodeList* pSpecificTagRefs, SNodeList* pTagRefs) {
3639
  CHECK_PARSER_STATUS(pCxt);
424,847✔
3640
  SCreateVSubTableStmt* pStmt = NULL;
424,847✔
3641
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT, (SNode**)&pStmt);
424,847✔
3642
  CHECK_MAKE_NODE(pStmt);
424,847✔
3643

3644
  if (pTagRefs != NULL) {
424,847✔
UNCOV
3645
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
3646
    goto _err;
×
3647
  }
3648
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
424,847✔
3649
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->tableName));
424,847✔
3650
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, sizeof(pStmt->useDbName));
424,847✔
3651
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, sizeof(pStmt->useTableName));
424,847✔
3652
  pStmt->ignoreExists = ignoreExists;
424,847✔
3653
  pStmt->pSpecificTags = pSpecificTags;
424,847✔
3654
  pStmt->pValsOfTags = pValsOfTags;
424,847✔
3655
  pStmt->pSpecificColRefs = pSpecificColRefs;
424,847✔
3656
  pStmt->pColRefs = pColRefs;
424,847✔
3657
  pStmt->pSpecificTagRefs = pSpecificTagRefs;
424,847✔
3658
  pStmt->pTagRefs = pTagRefs;
424,847✔
3659
  nodesDestroyNode(pRealTable);
424,847✔
3660
  nodesDestroyNode(pUseRealTable);
424,847✔
3661
  return (SNode*)pStmt;
424,847✔
UNCOV
3662
_err:
×
UNCOV
3663
  nodesDestroyNode(pRealTable);
×
UNCOV
3664
  nodesDestroyNode(pUseRealTable);
×
UNCOV
3665
  nodesDestroyList(pSpecificTags);
×
UNCOV
3666
  nodesDestroyList(pValsOfTags);
×
UNCOV
3667
  nodesDestroyList(pSpecificColRefs);
×
UNCOV
3668
  nodesDestroyList(pColRefs);
×
UNCOV
3669
  nodesDestroyList(pSpecificTagRefs);
×
3670
  nodesDestroyList(pTagRefs);
×
3671
  return NULL;
×
3672
}
3673

3674
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
9,760,850✔
3675
                             SNodeList* pTags, SNode* pOptions) {
3676
  CHECK_PARSER_STATUS(pCxt);
9,760,850✔
3677
  SCreateTableStmt* pStmt = NULL;
9,759,423✔
3678
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
9,759,423✔
3679
  CHECK_MAKE_NODE(pStmt);
9,759,423✔
3680
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
9,759,423✔
3681
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
9,759,423✔
3682
  pStmt->ignoreExists = ignoreExists;
9,759,423✔
3683
  pStmt->pCols = pCols;
9,759,423✔
3684
  pStmt->pTags = pTags;
9,759,423✔
3685
  pStmt->pOptions = (STableOptions*)pOptions;
9,759,423✔
3686
  nodesDestroyNode(pRealTable);
9,759,423✔
3687
  return (SNode*)pStmt;
9,759,423✔
3688
_err:
1,427✔
3689
  nodesDestroyNode(pRealTable);
1,427✔
3690
  nodesDestroyList(pCols);
1,427✔
3691
  nodesDestroyList(pTags);
1,427✔
3692
  nodesDestroyNode(pOptions);
1,427✔
3693
  return NULL;
1,427✔
3694
}
3695

3696
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
45,558,564✔
3697
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
3698
  CHECK_PARSER_STATUS(pCxt);
45,558,564✔
3699
  SCreateSubTableClause* pStmt = NULL;
45,550,144✔
3700
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
45,557,034✔
3701
  CHECK_MAKE_NODE(pStmt);
45,564,638✔
3702
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
45,564,638✔
3703
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
45,556,900✔
3704
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
45,556,093✔
3705
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
45,543,260✔
3706
  pStmt->ignoreExists = ignoreExists;
45,553,152✔
3707
  pStmt->pSpecificTags = pSpecificTags;
45,555,670✔
3708
  pStmt->pValsOfTags = pValsOfTags;
45,545,710✔
3709
  pStmt->pOptions = (STableOptions*)pOptions;
45,548,351✔
3710
  nodesDestroyNode(pRealTable);
45,554,096✔
3711
  nodesDestroyNode(pUseRealTable);
45,553,921✔
3712
  return (SNode*)pStmt;
45,562,862✔
UNCOV
3713
_err:
×
UNCOV
3714
  nodesDestroyNode(pRealTable);
×
UNCOV
3715
  nodesDestroyNode(pOptions);
×
UNCOV
3716
  nodesDestroyNode(pUseRealTable);
×
UNCOV
3717
  nodesDestroyList(pSpecificTags);
×
UNCOV
3718
  nodesDestroyList(pValsOfTags);
×
UNCOV
3719
  return NULL;
×
3720
}
3721

3722
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
1,458✔
3723
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
3724
  CHECK_PARSER_STATUS(pCxt);
1,458✔
3725
  SCreateSubTableFromFileClause* pStmt = NULL;
1,458✔
3726
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
1,458✔
3727
  CHECK_MAKE_NODE(pStmt);
1,458✔
3728
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
1,458✔
3729
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
1,458✔
3730
  pStmt->ignoreExists = ignoreExists;
1,458✔
3731
  pStmt->pSpecificTags = pSpecificTags;
1,458✔
3732
  if (TK_NK_STRING == pFilePath->type) {
1,458✔
3733
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
1,458✔
3734
  } else {
UNCOV
3735
    tstrncpy(pStmt->filePath, pFilePath->z,
×
3736
             pFilePath->n < sizeof(pStmt->filePath) ? pFilePath->n + 1 : sizeof(pStmt->filePath));
3737
  }
3738

3739
  nodesDestroyNode(pUseRealTable);
1,458✔
3740
  return (SNode*)pStmt;
1,458✔
3741
_err:
×
3742
  nodesDestroyNode(pUseRealTable);
×
3743
  nodesDestroyList(pSpecificTags);
×
3744
  return NULL;
×
3745
}
3746

3747
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
38,143,122✔
3748
  CHECK_PARSER_STATUS(pCxt);
38,143,122✔
3749
  SCreateMultiTablesStmt* pStmt = NULL;
38,134,857✔
3750
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
38,143,702✔
3751
  CHECK_MAKE_NODE(pStmt);
38,150,994✔
3752
  pStmt->pSubTables = pSubTables;
38,150,994✔
3753
  return (SNode*)pStmt;
38,150,655✔
UNCOV
3754
_err:
×
UNCOV
3755
  nodesDestroyList(pSubTables);
×
UNCOV
3756
  return NULL;
×
3757
}
3758

3759
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
2,877,115✔
3760
  CHECK_PARSER_STATUS(pCxt);
2,877,115✔
3761
  SDropTableClause* pStmt = NULL;
2,876,897✔
3762
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
2,876,897✔
3763
  CHECK_MAKE_NODE(pStmt);
2,876,897✔
3764
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2,876,897✔
3765
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2,876,897✔
3766
  pStmt->ignoreNotExists = ignoreNotExists;
2,876,897✔
3767
  nodesDestroyNode(pRealTable);
2,876,897✔
3768
  return (SNode*)pStmt;
2,876,897✔
3769
_err:
218✔
3770
  nodesDestroyNode(pRealTable);
218✔
3771
  return NULL;
218✔
3772
}
3773

3774
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
2,753,161✔
3775
  CHECK_PARSER_STATUS(pCxt);
2,753,161✔
3776
  SDropTableStmt* pStmt = NULL;
2,752,943✔
3777
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
2,752,943✔
3778
  CHECK_MAKE_NODE(pStmt);
2,752,943✔
3779
  pStmt->pTables = pTables;
2,752,943✔
3780
  pStmt->withOpt = withOpt;
2,752,943✔
3781
  return (SNode*)pStmt;
2,752,943✔
3782
_err:
218✔
3783
  nodesDestroyList(pTables);
218✔
3784
  return NULL;
218✔
3785
}
3786

3787
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
102,545✔
3788
  CHECK_PARSER_STATUS(pCxt);
102,545✔
3789
  SDropSuperTableStmt* pStmt = NULL;
102,545✔
3790
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
102,545✔
3791
  CHECK_MAKE_NODE(pStmt);
102,545✔
3792
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
102,545✔
3793
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
102,545✔
3794
  pStmt->ignoreNotExists = ignoreNotExists;
102,545✔
3795
  pStmt->withOpt = withOpt;
102,545✔
3796
  nodesDestroyNode(pRealTable);
102,545✔
3797
  return (SNode*)pStmt;
102,545✔
UNCOV
3798
_err:
×
UNCOV
3799
  nodesDestroyNode(pRealTable);
×
UNCOV
3800
  return NULL;
×
3801
}
3802

3803
SNode* createDropVirtualTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
78,734✔
3804
  CHECK_PARSER_STATUS(pCxt);
78,734✔
3805
  SDropVirtualTableStmt* pStmt = NULL;
78,734✔
3806
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
78,734✔
3807
  CHECK_MAKE_NODE(pStmt);
78,734✔
3808
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
78,734✔
3809
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
78,734✔
3810
  pStmt->ignoreNotExists = ignoreNotExists;
78,734✔
3811
  pStmt->withOpt = withOpt;
78,734✔
3812
  nodesDestroyNode(pRealTable);
78,734✔
3813
  return (SNode*)pStmt;
78,734✔
UNCOV
3814
_err:
×
UNCOV
3815
  nodesDestroyNode(pRealTable);
×
UNCOV
3816
  return NULL;
×
3817
}
3818

3819
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
15,168,273✔
3820
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
15,168,273✔
3821
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
15,168,273✔
3822
  nodesDestroyNode(pRealTable);
15,168,273✔
3823
  return (SNode*)pStmt;
15,168,273✔
3824
}
3825

3826
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
82,357✔
3827
  CHECK_PARSER_STATUS(pCxt);
82,357✔
3828
  SAlterTableStmt* pStmt = NULL;
73,831✔
3829
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
73,831✔
3830
  CHECK_MAKE_NODE(pStmt);
73,831✔
3831
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
73,831✔
3832
  pStmt->pOptions = (STableOptions*)pOptions;
73,831✔
3833
  return createAlterTableStmtFinalize(pRealTable, pStmt);
73,831✔
3834
_err:
8,526✔
3835
  nodesDestroyNode(pRealTable);
8,526✔
3836
  nodesDestroyNode(pOptions);
8,526✔
3837
  return NULL;
8,526✔
3838
}
3839

3840
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
2,880,965✔
3841
                                    SDataType dataType) {
3842
  CHECK_PARSER_STATUS(pCxt);
2,880,965✔
3843
  CHECK_NAME(checkColumnName(pCxt, pColName));
2,880,965✔
3844
  SAlterTableStmt* pStmt = NULL;
2,880,747✔
3845
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
2,880,747✔
3846
  CHECK_MAKE_NODE(pStmt);
2,880,747✔
3847
  pStmt->alterType = alterType;
2,880,747✔
3848
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
2,880,747✔
3849
  pStmt->dataType = dataType;
2,880,747✔
3850
  return createAlterTableStmtFinalize(pRealTable, pStmt);
2,880,747✔
3851
_err:
218✔
3852
  nodesDestroyNode(pRealTable);
218✔
3853
  return NULL;
218✔
3854
}
3855

3856
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
9,691,951✔
3857
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
3858
  SAlterTableStmt* pStmt = NULL;
9,691,951✔
3859
  CHECK_PARSER_STATUS(pCxt);
9,691,951✔
3860
  CHECK_NAME(checkColumnName(pCxt, pColName));
9,689,609✔
3861
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
9,689,391✔
3862
  CHECK_MAKE_NODE(pStmt);
9,689,391✔
3863
  pStmt->alterType = alterType;
9,689,391✔
3864
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
9,689,391✔
3865
  pStmt->dataType = dataType;
9,689,391✔
3866
  pStmt->pColOptions = (SColumnOptions*)pOptions;
9,689,391✔
3867

3868
  if (pOptions != NULL) {
9,689,391✔
3869
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
9,689,391✔
3870
    if (pOption->hasRef) {
9,689,391✔
3871
      if (!pOption->commentNull || pOption->bPrimaryKey || 0 != strcmp(pOption->compress, "") ||
40,758✔
3872
          0 != strcmp(pOption->encode, "") || 0 != strcmp(pOption->compressLevel, "")) {
40,758✔
UNCOV
3873
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
×
3874
      }
3875
      pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF;
40,758✔
3876
      tstrncpy(pStmt->refDbName, pOption->refDb, TSDB_DB_NAME_LEN);
40,758✔
3877
      tstrncpy(pStmt->refTableName, pOption->refTable, TSDB_TABLE_NAME_LEN);
40,758✔
3878
      tstrncpy(pStmt->refColName, pOption->refColumn, TSDB_COL_NAME_LEN);
40,758✔
3879
      CHECK_PARSER_STATUS(pCxt);
40,758✔
3880
    } else if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
9,648,633✔
3881
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
9,645,729✔
3882
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
294,606✔
3883
      } else {
3884
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
3885
        //                                         "not support alter column with option except compress");
3886
        // return NULL;
3887
      }
3888
    } else {
3889
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
2,904✔
3890
                                              "not support alter column with option except compress");
3891
      CHECK_PARSER_STATUS(pCxt);
2,904✔
3892
    }
3893
  }
3894
  return createAlterTableStmtFinalize(pRealTable, pStmt);
9,686,487✔
3895
_err:
5,464✔
3896
  nodesDestroyNode(pOptions);
5,464✔
3897
  nodesDestroyNode((SNode*)pStmt);
5,464✔
3898
  nodesDestroyNode(pRealTable);
5,464✔
3899
  return NULL;
5,464✔
3900
}
3901

3902
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
120,412✔
3903
                                           SToken* pColName, SNode* pOptions) {
3904
  CHECK_PARSER_STATUS(pCxt);
120,412✔
3905
  CHECK_NAME(checkColumnName(pCxt, pColName));
120,412✔
3906
  SAlterTableStmt* pStmt = NULL;
120,412✔
3907
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
120,412✔
3908
  CHECK_MAKE_NODE(pStmt);
120,412✔
3909
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
120,412✔
3910
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
120,412✔
3911
  pStmt->pColOptions = (SColumnOptions*)pOptions;
120,412✔
3912
  return createAlterTableStmtFinalize(pRealTable, pStmt);
120,412✔
UNCOV
3913
_err:
×
UNCOV
3914
  nodesDestroyNode(pOptions);
×
UNCOV
3915
  nodesDestroyNode(pRealTable);
×
UNCOV
3916
  return NULL;
×
3917
}
3918

3919
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
1,952,502✔
3920
  CHECK_PARSER_STATUS(pCxt);
1,952,502✔
3921
  CHECK_NAME(checkColumnName(pCxt, pColName));
1,952,502✔
3922
  SAlterTableStmt* pStmt = NULL;
1,952,066✔
3923
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
1,952,066✔
3924
  CHECK_MAKE_NODE(pStmt);
1,952,066✔
3925
  pStmt->alterType = alterType;
1,952,066✔
3926
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1,952,066✔
3927
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1,952,066✔
3928
_err:
436✔
3929
  nodesDestroyNode(pRealTable);
436✔
3930
  return NULL;
436✔
3931
}
3932

3933
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
225,391✔
3934
                                 SToken* pNewColName) {
3935
  CHECK_PARSER_STATUS(pCxt);
225,391✔
3936
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
225,391✔
3937
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
225,391✔
3938
  SAlterTableStmt* pStmt = NULL;
224,476✔
3939
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
224,476✔
3940
  CHECK_MAKE_NODE(pStmt);
224,476✔
3941
  pStmt->alterType = alterType;
224,476✔
3942
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
224,476✔
3943
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
224,476✔
3944
  return createAlterTableStmtFinalize(pRealTable, pStmt);
224,476✔
3945
_err:
915✔
3946
  nodesDestroyNode(pRealTable);
915✔
3947
  return NULL;
915✔
3948
}
3949

3950
SNode* createAlterTableAlterColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
124,524✔
3951
                                   SNode* pRef) {
3952
  CHECK_PARSER_STATUS(pCxt);
124,524✔
3953
  CHECK_NAME(checkColumnName(pCxt, pColName));
124,524✔
3954
  SAlterTableStmt* pStmt = NULL;
124,524✔
3955
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
124,524✔
3956
  CHECK_MAKE_NODE(pStmt);
124,524✔
3957
  pStmt->alterType = alterType;
124,524✔
3958
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
124,524✔
3959
  tstrncpy(pStmt->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
124,524✔
3960
  tstrncpy(pStmt->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
124,524✔
3961
  tstrncpy(pStmt->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
124,524✔
3962
  return createAlterTableStmtFinalize(pRealTable, pStmt);
124,524✔
UNCOV
3963
_err:
×
UNCOV
3964
  nodesDestroyNode(pRealTable);
×
UNCOV
3965
  return NULL;
×
3966
}
3967

3968
SNode* createAlterTableRemoveColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
72,999✔
3969
                                    const SToken* pLiteral) {
3970
  CHECK_PARSER_STATUS(pCxt);
72,999✔
3971
  CHECK_NAME(checkColumnName(pCxt, pColName));
72,999✔
3972
  SAlterTableStmt* pStmt = NULL;
72,999✔
3973
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
72,999✔
3974
  CHECK_MAKE_NODE(pStmt);
72,999✔
3975
  pStmt->alterType = alterType;
72,999✔
3976
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
72,999✔
3977
  return createAlterTableStmtFinalize(pRealTable, pStmt);
72,999✔
UNCOV
3978
_err:
×
UNCOV
3979
  nodesDestroyNode(pRealTable);
×
UNCOV
3980
  return NULL;
×
3981
}
3982

3983

3984

3985
SNode* createAlterTagValueNode(SAstCreateContext* pCxt, SToken* pTagName, SNode* pVal) {
9,145,830✔
3986
  CHECK_PARSER_STATUS(pCxt);
9,145,830✔
3987
  CHECK_NAME(checkColumnName(pCxt, pTagName));
9,145,830✔
3988
  SUpdateTagValueNode* pNode = NULL;
9,145,830✔
3989
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_TAG_VALUE, (SNode**)&pNode);
9,145,830✔
3990
  CHECK_MAKE_NODE(pNode);
9,145,830✔
3991
  COPY_STRING_FORM_ID_TOKEN(pNode->tagName, pTagName);
9,145,830✔
3992
  pNode->pVal = (SValueNode*)pVal;
9,145,830✔
3993
  return (SNode*)pNode;
9,145,830✔
3994

UNCOV
3995
_err:
×
UNCOV
3996
  nodesDestroyNode((SNode*)pNode);
×
UNCOV
3997
  return NULL;
×
3998
}
3999

4000

4001
// NOTE: this function only supports REGEXP_REPLACE for now, it should be extended
4002
// for full expression support in the future, and the prototype needs to be changed
4003
// accordingly.
4004
SNode* createAlterTagValueNodeWithExpression(SAstCreateContext* pCxt, SToken* pTagName, const SToken* pattern, const SToken* replacement) {
14,088✔
4005
  CHECK_PARSER_STATUS(pCxt);
14,088✔
4006
  CHECK_NAME(checkColumnName(pCxt, pTagName));
14,088✔
4007
  SUpdateTagValueNode* pNode = NULL;
14,088✔
4008
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_TAG_VALUE, (SNode**)&pNode);
14,088✔
4009
  CHECK_MAKE_NODE(pNode);
14,088✔
4010
  COPY_STRING_FORM_ID_TOKEN(pNode->tagName, pTagName);
14,088✔
4011

4012
  pNode->regexp = taosMemoryMalloc(pattern->n + 1);
14,088✔
4013
  if (pNode->regexp == NULL) {
14,088✔
UNCOV
4014
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
4015
    goto _err;
×
4016
  }
4017
  (void)trimString(pattern->z, pattern->n, pNode->regexp, pattern->n);
14,088✔
4018

4019
  int32_t code = checkRegexPattern(pNode->regexp);
14,088✔
4020
  if (code != 0) {
14,088✔
4021
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "'%s' is not a valid regular expression", pNode->regexp);
1,370✔
4022
    goto _err;
1,370✔
4023
  }
4024

4025
  pNode->replacement = taosMemoryMalloc(replacement->n + 1);
12,718✔
4026
  if (pNode->replacement == NULL) {
12,718✔
UNCOV
4027
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
4028
    goto _err;
×
4029
  }
4030
  (void)trimString(replacement->z, replacement->n, pNode->replacement, replacement->n);
12,718✔
4031

4032
  return (SNode*)pNode;
12,718✔
4033

4034
_err:
1,370✔
4035
  nodesDestroyNode((SNode*)pNode);
1,370✔
4036
  return NULL;
1,370✔
4037
}
4038

4039

4040

4041
SNode* createAlterTableUpdateTagValClause(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pTagList) {
9,038,929✔
4042
  CHECK_PARSER_STATUS(pCxt);
9,038,929✔
4043
  SAlterTableUpdateTagValClause* pClause = NULL;
9,038,929✔
4044
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_UPDATE_TAG_VAL_CLAUSE, (SNode**)&pClause);
9,038,929✔
4045

4046
  CHECK_MAKE_NODE(pClause);
9,038,929✔
4047
  pClause->pTagList = pTagList;
9,038,929✔
4048

4049
  tstrncpy(pClause->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pClause->dbName));
9,038,929✔
4050
  tstrncpy(pClause->tableName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pClause->tableName));
9,038,929✔
4051
  nodesDestroyNode(pRealTable);
9,038,929✔
4052

4053
  return (SNode*)pClause;
9,038,929✔
4054

UNCOV
4055
_err:
×
UNCOV
4056
  nodesDestroyNode((SNode*)pClause);
×
UNCOV
4057
  return NULL;
×
4058
}
4059

4060

4061

4062
SNode* createAlterMultiTableUpdateTagValStmt(SAstCreateContext* pCxt, SNodeList* pTableList) {
8,996,392✔
4063
  CHECK_PARSER_STATUS(pCxt);
8,996,392✔
4064
  SAlterTableStmt* pStmt = NULL;
8,996,392✔
4065
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
8,996,392✔
4066

4067
  CHECK_MAKE_NODE(pStmt);
8,996,392✔
4068
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL;
8,996,392✔
4069
  pStmt->pList = pTableList;
8,996,392✔
4070
  return (SNode*)pStmt;
8,996,392✔
4071

UNCOV
4072
_err:
×
UNCOV
4073
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
4074
  return NULL;
×
4075
}
4076

4077

4078

4079
SNode* createAlterChildTableUpdateTagValStmt(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pTagList, SNode* pWhere) {
32,731✔
4080
  CHECK_PARSER_STATUS(pCxt);
32,731✔
4081
  SAlterTableStmt* pStmt = NULL;
32,731✔
4082
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
32,731✔
4083

4084
  CHECK_MAKE_NODE(pStmt);
32,731✔
4085
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL;
32,731✔
4086
  pStmt->pList = pTagList;
32,731✔
4087
  pStmt->pWhere = pWhere;
32,731✔
4088
  return createAlterTableStmtFinalize(pRealTable, pStmt);
32,731✔
4089

UNCOV
4090
_err:
×
UNCOV
4091
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
4092
  return NULL;
×
4093
}
4094

4095

4096

4097
SNode* setAlterSuperTableType(SNode* pStmt) {
570,749✔
4098
  if (!pStmt) return NULL;
570,749✔
4099
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
570,095✔
4100
  return pStmt;
570,095✔
4101
}
4102

4103
SNode* setAlterVirtualTableType(SNode* pStmt) {
450,548✔
4104
  if (!pStmt) return NULL;
450,548✔
4105
  setNodeType(pStmt, QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT);
450,548✔
4106
  return pStmt;
450,548✔
4107
}
4108

4109
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
99,839,828✔
4110
  CHECK_PARSER_STATUS(pCxt);
99,839,828✔
4111
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
99,840,322✔
4112
  SUseDatabaseStmt* pStmt = NULL;
99,843,285✔
4113
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
99,843,285✔
4114
  CHECK_MAKE_NODE(pStmt);
99,843,217✔
4115
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
99,843,217✔
4116
  return (SNode*)pStmt;
99,843,222✔
4117
_err:
×
UNCOV
4118
  return NULL;
×
4119
}
4120

4121
static bool needDbShowStmt(ENodeType type) {
1,636,469✔
4122
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
4123
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
4124
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
4125
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type ||
4126
         QUERY_NODE_SHOW_USAGE_STMT == type || QUERY_NODE_SHOW_VTABLES_STMT == type ||
4127
         QUERY_NODE_SHOW_STREAMS_STMT == type || QUERY_NODE_SHOW_VALIDATE_VTABLE_STMT;
1,636,469✔
4128
}
4129

4130
SNode* createShowStmtWithLike(SAstCreateContext* pCxt, ENodeType type, SNode* pLikePattern) {
26,843✔
4131
  CHECK_PARSER_STATUS(pCxt);
26,843✔
4132
  SShowStmt* pStmt = NULL;
26,843✔
4133
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
26,843✔
4134
  CHECK_MAKE_NODE(pStmt);
26,843✔
4135
  pStmt->withFull = false;
26,843✔
4136
  pStmt->pTbName = pLikePattern;
26,843✔
4137
  if (pLikePattern) {
26,843✔
4138
    pStmt->tableCondType = OP_TYPE_LIKE;
5,197✔
4139
  }
4140
  return (SNode*)pStmt;
26,843✔
UNCOV
4141
_err:
×
4142
  nodesDestroyNode(pLikePattern);
×
4143
  return NULL;
×
4144
}
4145

4146
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
1,514,277✔
4147
  CHECK_PARSER_STATUS(pCxt);
1,514,277✔
4148
  SShowStmt* pStmt = NULL;
1,514,333✔
4149
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,514,333✔
4150
  CHECK_MAKE_NODE(pStmt);
1,514,333✔
4151
  pStmt->withFull = false;
1,514,333✔
4152
  return (SNode*)pStmt;
1,514,333✔
UNCOV
4153
_err:
×
UNCOV
4154
  return NULL;
×
4155
}
4156

4157
SNode* createShowXnodeStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pWhere) {
19,466✔
4158
  CHECK_PARSER_STATUS(pCxt);
19,466✔
4159
  SShowStmt* pStmt = NULL;
19,466✔
4160
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
19,466✔
4161
  CHECK_MAKE_NODE(pStmt);
19,466✔
4162
  pStmt->pWhere = pWhere;
19,466✔
4163
  return (SNode*)pStmt;
19,466✔
UNCOV
4164
_err:
×
UNCOV
4165
  return NULL;
×
4166
}
4167

4168
SNode* createShowXNodeResourcesWhereStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
17,067✔
4169
  CHECK_PARSER_STATUS(pCxt);
17,067✔
4170
  SShowStmt* pStmt = NULL;
17,067✔
4171
  ENodeType  type;
4172
  switch (resourceType) {
17,067✔
4173
    case XNODE_TASK:
7,334✔
4174
      type = QUERY_NODE_SHOW_XNODE_TASKS_STMT;
7,334✔
4175
      break;
7,334✔
4176
    case XNODE_JOB:
5,360✔
4177
      type = QUERY_NODE_SHOW_XNODE_JOBS_STMT;
5,360✔
4178
      break;
5,360✔
4179
    case XNODE_AGENT:
4,373✔
4180
      type = QUERY_NODE_SHOW_XNODE_AGENTS_STMT;
4,373✔
4181
      break;
4,373✔
UNCOV
4182
    default:
×
UNCOV
4183
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4184
                                              "Xnode not support show xnode resource type");
UNCOV
4185
      goto _err;
×
4186
  }
4187

4188
  pStmt = (SShowStmt*)createShowXnodeStmtWithCond(pCxt, type, pWhere);
17,067✔
4189
  CHECK_MAKE_NODE(pStmt);
17,067✔
4190

4191
  return (SNode*)pStmt;
17,067✔
UNCOV
4192
_err:
×
UNCOV
4193
  return NULL;
×
4194
}
4195

4196
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
15,116✔
4197
  CHECK_PARSER_STATUS(pCxt);
15,116✔
4198
  SShowStmt* pStmt = NULL;
15,116✔
4199
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
15,116✔
4200
  CHECK_MAKE_NODE(pStmt);
15,116✔
4201
  pStmt->withFull = true;
15,116✔
4202
  return (SNode*)pStmt;
15,116✔
UNCOV
4203
_err:
×
UNCOV
4204
  return NULL;
×
4205
}
4206

4207
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
748,808✔
4208
  CHECK_PARSER_STATUS(pCxt);
748,808✔
4209
  SShowCompactsStmt* pStmt = NULL;
748,808✔
4210
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
748,808✔
4211
  CHECK_MAKE_NODE(pStmt);
748,808✔
4212
  return (SNode*)pStmt;
748,808✔
UNCOV
4213
_err:
×
UNCOV
4214
  return NULL;
×
4215
}
4216

4217
SNode* createShowSsMigratesStmt(SAstCreateContext* pCxt, ENodeType type) {
92,430✔
4218
  CHECK_PARSER_STATUS(pCxt);
92,430✔
4219
  SShowSsMigratesStmt* pStmt = NULL;
92,430✔
4220
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
92,430✔
4221
  CHECK_MAKE_NODE(pStmt);
92,430✔
4222
  return (SNode*)pStmt;
92,430✔
UNCOV
4223
_err:
×
UNCOV
4224
  return NULL;
×
4225
}
4226

4227
SNode* createShowTokensStmt(SAstCreateContext* pCxt, ENodeType type) {
1,952✔
4228
  CHECK_PARSER_STATUS(pCxt);
1,952✔
4229
  SShowTokensStmt* pStmt = NULL;
1,952✔
4230
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,952✔
4231
  CHECK_MAKE_NODE(pStmt);
1,952✔
4232
  return (SNode*)pStmt;
1,952✔
UNCOV
4233
_err:
×
UNCOV
4234
  return NULL;
×
4235
}
4236

4237
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
797,638✔
4238
  if (pStmt == NULL) {
797,638✔
4239
    return NULL;
×
4240
  }
4241
  SShowStmt* pShow = (SShowStmt*)pStmt;
797,638✔
4242
  pShow->showKind = showKind;
797,638✔
4243
  return pStmt;
797,638✔
4244
}
4245

4246
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
1,636,469✔
4247
                              EOperatorType tableCondType) {
4248
  CHECK_PARSER_STATUS(pCxt);
1,636,469✔
4249
  if (needDbShowStmt(type) && NULL == pDbName) {
1,636,469✔
4250
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
1,161✔
4251
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1,161✔
4252
    CHECK_PARSER_STATUS(pCxt);
1,161✔
4253
  }
4254
  SShowStmt* pStmt = NULL;
1,635,308✔
4255
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,635,308✔
4256
  CHECK_MAKE_NODE(pStmt);
1,635,308✔
4257
  pStmt->pDbName = pDbName;
1,635,308✔
4258
  pStmt->pTbName = pTbName;
1,635,308✔
4259
  pStmt->tableCondType = tableCondType;
1,635,308✔
4260
  return (SNode*)pStmt;
1,635,308✔
4261
_err:
1,161✔
4262
  nodesDestroyNode(pDbName);
1,161✔
4263
  nodesDestroyNode(pTbName);
1,161✔
4264
  return NULL;
1,161✔
4265
}
4266

4267
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
286,594✔
4268
                            EOperatorType tableCondType) {
4269
  CHECK_PARSER_STATUS(pCxt);
286,594✔
4270
  SNode* pDbName = NULL;
286,594✔
4271
  if (option.dbName.type == TK_NK_NIL) {
286,594✔
4272
    pDbName = createDefaultDatabaseCondValue(pCxt);
101,480✔
4273
  } else {
4274
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
185,114✔
4275
  }
4276

4277
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
286,594✔
UNCOV
4278
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
4279
    return NULL;
×
4280
  }
4281

4282
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
286,594✔
4283
  CHECK_PARSER_STATUS(pCxt);
286,594✔
4284
  (void)setShowKind(pCxt, pStmt, option.kind);
285,448✔
4285
  return pStmt;
285,448✔
4286
_err:
1,146✔
4287
  nodesDestroyNode(pTbName);
1,146✔
4288
  return NULL;
1,146✔
4289
}
4290

4291
SNode* createShowVTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
64,462✔
4292
                             EOperatorType tableCondType) {
4293
  CHECK_PARSER_STATUS(pCxt);
64,462✔
4294
  SNode* pDbName = NULL;
64,462✔
4295
  if (option.dbName.type == TK_NK_NIL) {
64,462✔
4296
    pDbName = createDefaultDatabaseCondValue(pCxt);
4,139✔
4297
  } else {
4298
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
60,323✔
4299
  }
4300

4301
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
64,462✔
UNCOV
4302
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4303
    return NULL;
×
4304
  }
4305

4306
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_VTABLES_STMT, pDbName, pTbName, tableCondType);
64,462✔
4307
  CHECK_PARSER_STATUS(pCxt);
64,462✔
4308
  (void)setShowKind(pCxt, pStmt, option.kind);
64,462✔
4309
  return pStmt;
64,462✔
UNCOV
4310
_err:
×
UNCOV
4311
  nodesDestroyNode(pTbName);
×
UNCOV
4312
  return NULL;
×
4313
}
4314

4315
SNode* createShowSTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
245,074✔
4316
                             EOperatorType tableCondType) {
4317
  CHECK_PARSER_STATUS(pCxt);
245,074✔
4318
  SNode* pDbName = NULL;
245,074✔
4319
  if (option.dbName.type == TK_NK_NIL) {
245,074✔
4320
    pDbName = createDefaultDatabaseCondValue(pCxt);
69,926✔
4321
  } else {
4322
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
175,148✔
4323
  }
4324

4325
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_VIRTUAL &&
245,074✔
4326
      option.kind != SHOW_KIND_ALL) {
243,712✔
4327
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4328
    return NULL;
×
4329
  }
4330

4331
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_STABLES_STMT, pDbName, pTbName, tableCondType);
245,074✔
4332
  CHECK_PARSER_STATUS(pCxt);
245,074✔
4333
  (void)setShowKind(pCxt, pStmt, option.kind);
245,074✔
4334
  return pStmt;
245,074✔
4335
_err:
×
4336
  nodesDestroyNode(pTbName);
×
4337
  return NULL;
×
4338
}
4339

4340
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
74,462✔
4341
  CHECK_PARSER_STATUS(pCxt);
74,462✔
4342
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
74,462✔
4343
  SShowCreateDatabaseStmt* pStmt = NULL;
74,462✔
4344
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
74,462✔
4345
  CHECK_MAKE_NODE(pStmt);
74,462✔
4346
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
74,462✔
4347
  return (SNode*)pStmt;
74,462✔
UNCOV
4348
_err:
×
UNCOV
4349
  return NULL;
×
4350
}
4351

4352
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
5,222✔
4353
  CHECK_PARSER_STATUS(pCxt);
5,222✔
4354
  SToken  dbToken = {0};
5,222✔
4355
  SToken* pDbToken = NULL;
5,222✔
4356

4357
  if (pNode) {
5,222✔
4358
    SValueNode* pDbName = (SValueNode*)pNode;
1,728✔
4359
    if (pDbName->literal) {
1,728✔
4360
      dbToken.z = pDbName->literal;
1,728✔
4361
      dbToken.n = strlen(pDbName->literal);
1,728✔
4362
      pDbToken = &dbToken;
1,728✔
4363
    }
4364
  }
4365

4366
  if (pDbToken) {
5,222✔
4367
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
1,728✔
4368
  }
4369

4370
  SShowAliveStmt* pStmt = NULL;
5,222✔
4371
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
5,222✔
4372
  CHECK_PARSER_STATUS(pCxt);
5,222✔
4373

4374
  if (pDbToken) {
5,222✔
4375
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
1,728✔
4376
  }
4377
  if (pNode) {
5,222✔
4378
    nodesDestroyNode(pNode);
1,728✔
4379
  }
4380

4381
  return (SNode*)pStmt;
5,222✔
UNCOV
4382
_err:
×
UNCOV
4383
  nodesDestroyNode(pNode);
×
UNCOV
4384
  return NULL;
×
4385
}
4386

4387
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
95,673✔
4388
  CHECK_PARSER_STATUS(pCxt);
95,673✔
4389
  SShowCreateTableStmt* pStmt = NULL;
95,237✔
4390
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
95,237✔
4391
  CHECK_MAKE_NODE(pStmt);
95,237✔
4392
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
95,237✔
4393
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
95,237✔
4394
  nodesDestroyNode(pRealTable);
95,237✔
4395
  return (SNode*)pStmt;
95,237✔
4396
_err:
436✔
4397
  nodesDestroyNode(pRealTable);
436✔
4398
  return NULL;
436✔
4399
}
4400

4401
SNode* createShowCreateVTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
9,560✔
4402
  CHECK_PARSER_STATUS(pCxt);
9,560✔
4403
  SShowCreateTableStmt* pStmt = NULL;
9,560✔
4404
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
9,560✔
4405
  CHECK_MAKE_NODE(pStmt);
9,560✔
4406
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
9,560✔
4407
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
9,560✔
4408
  nodesDestroyNode(pRealTable);
9,560✔
4409
  return (SNode*)pStmt;
9,560✔
UNCOV
4410
_err:
×
UNCOV
4411
  nodesDestroyNode(pRealTable);
×
UNCOV
4412
  return NULL;
×
4413
}
4414

4415
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
8,895✔
4416
  CHECK_PARSER_STATUS(pCxt);
8,895✔
4417
  SShowCreateViewStmt* pStmt = NULL;
8,895✔
4418
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
8,895✔
4419
  CHECK_MAKE_NODE(pStmt);
8,895✔
4420
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
8,895✔
4421
  tstrncpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
8,895✔
4422
  nodesDestroyNode(pRealTable);
8,895✔
4423
  return (SNode*)pStmt;
8,895✔
UNCOV
4424
_err:
×
UNCOV
4425
  nodesDestroyNode(pRealTable);
×
UNCOV
4426
  return NULL;
×
4427
}
4428

4429
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
4,633✔
4430
  CHECK_PARSER_STATUS(pCxt);
4,633✔
4431
  SShowTableDistributedStmt* pStmt = NULL;
4,633✔
4432
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
4,633✔
4433
  CHECK_MAKE_NODE(pStmt);
4,633✔
4434
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
4,633✔
4435
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
4,633✔
4436
  nodesDestroyNode(pRealTable);
4,633✔
4437
  return (SNode*)pStmt;
4,633✔
UNCOV
4438
_err:
×
UNCOV
4439
  nodesDestroyNode(pRealTable);
×
UNCOV
4440
  return NULL;
×
4441
}
4442

4443
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
41,403✔
4444
  CHECK_PARSER_STATUS(pCxt);
41,403✔
4445
  SShowDnodeVariablesStmt* pStmt = NULL;
41,403✔
4446
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
41,403✔
4447
  CHECK_MAKE_NODE(pStmt);
41,403✔
4448
  pStmt->pDnodeId = pDnodeId;
41,403✔
4449
  pStmt->pLikePattern = pLikePattern;
41,403✔
4450
  return (SNode*)pStmt;
41,403✔
4451
_err:
×
UNCOV
4452
  nodesDestroyNode(pDnodeId);
×
UNCOV
4453
  nodesDestroyNode(pLikePattern);
×
UNCOV
4454
  return NULL;
×
4455
}
4456

4457
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
10,969✔
4458
  CHECK_PARSER_STATUS(pCxt);
10,969✔
4459
  SShowVnodesStmt* pStmt = NULL;
10,969✔
4460
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
10,969✔
4461
  CHECK_MAKE_NODE(pStmt);
10,969✔
4462
  pStmt->pDnodeId = pDnodeId;
10,969✔
4463
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
10,969✔
4464
  return (SNode*)pStmt;
10,969✔
4465
_err:
×
UNCOV
4466
  nodesDestroyNode(pDnodeId);
×
UNCOV
4467
  nodesDestroyNode(pDnodeEndpoint);
×
UNCOV
4468
  return NULL;
×
4469
}
4470

4471
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
8,256✔
4472
  CHECK_PARSER_STATUS(pCxt);
8,256✔
4473
  if (NULL == pDbName) {
8,256✔
UNCOV
4474
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
UNCOV
4475
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4476
    CHECK_PARSER_STATUS(pCxt);
×
4477
  }
4478
  SShowTableTagsStmt* pStmt = NULL;
8,256✔
4479
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
8,256✔
4480
  CHECK_MAKE_NODE(pStmt);
8,256✔
4481
  pStmt->pDbName = pDbName;
8,256✔
4482
  pStmt->pTbName = pTbName;
8,256✔
4483
  pStmt->pTags = pTags;
8,256✔
4484
  return (SNode*)pStmt;
8,256✔
UNCOV
4485
_err:
×
UNCOV
4486
  nodesDestroyNode(pTbName);
×
UNCOV
4487
  nodesDestroyNode(pDbName);
×
UNCOV
4488
  nodesDestroyList(pTags);
×
UNCOV
4489
  return NULL;
×
4490
}
4491

4492
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
34,425✔
4493
  CHECK_PARSER_STATUS(pCxt);
34,425✔
4494
  SShowCompactDetailsStmt* pStmt = NULL;
34,425✔
4495
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
34,425✔
4496
  CHECK_MAKE_NODE(pStmt);
34,425✔
4497
  pStmt->pId = pCompactId;
34,425✔
4498
  return (SNode*)pStmt;
34,425✔
4499
_err:
×
4500
  nodesDestroyNode(pCompactId);
×
4501
  return NULL;
×
4502
}
4503

4504
SNode* createShowRetentionDetailsStmt(SAstCreateContext* pCxt, SNode* pId) {
2,370✔
4505
  CHECK_PARSER_STATUS(pCxt);
2,370✔
4506
  SShowRetentionDetailsStmt* pStmt = NULL;
2,370✔
4507
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_RETENTION_DETAILS_STMT, (SNode**)&pStmt);
2,370✔
4508
  CHECK_MAKE_NODE(pStmt);
2,370✔
4509
  pStmt->pId = pId;
2,370✔
4510
  return (SNode*)pStmt;
2,370✔
4511
_err:
×
4512
  nodesDestroyNode(pId);
×
4513
  return NULL;
×
4514
}
4515

4516
SNode* createShowTransactionDetailsStmt(SAstCreateContext* pCxt, SNode* pTransactionIdNode) {
316✔
4517
  CHECK_PARSER_STATUS(pCxt);
316✔
4518
  SShowTransactionDetailsStmt* pStmt = NULL;
316✔
4519
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT, (SNode**)&pStmt);
316✔
4520
  CHECK_MAKE_NODE(pStmt);
316✔
4521
  pStmt->pTransactionId = pTransactionIdNode;
316✔
4522
  return (SNode*)pStmt;
316✔
UNCOV
4523
_err:
×
4524
  nodesDestroyNode(pTransactionIdNode);
×
4525
  return NULL;
×
4526
}
4527

4528
SNode* createShowValidateVirtualTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pVTable) {
3,864✔
4529
  CHECK_PARSER_STATUS(pCxt);
3,864✔
4530
  SShowValidateVirtualTable* pStmt = NULL;
3,864✔
4531
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,864✔
4532
  CHECK_MAKE_NODE(pStmt);
3,864✔
4533

4534
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pVTable)->table.dbName, TSDB_DB_NAME_LEN);
3,864✔
4535
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pVTable)->table.tableName, TSDB_TABLE_NAME_LEN);
3,864✔
4536
  nodesDestroyNode(pVTable);
3,864✔
4537
  return (SNode*)pStmt;
3,864✔
4538
_err:
×
UNCOV
4539
  nodesDestroyNode(pVTable);
×
UNCOV
4540
  return NULL;
×
4541
}
4542

4543
static bool parseIp(const char* strIp, SIpRange* pIpRange) {
6,557✔
4544
  if (strchr(strIp, ':') == NULL) {
6,557✔
4545
    struct in_addr ip4;
6,557✔
4546
    if (inet_pton(AF_INET, strIp, &ip4) == 1) {
6,557✔
4547
      pIpRange->type = 0;
5,412✔
4548
      memcpy(&pIpRange->ipV4.ip, &ip4.s_addr, sizeof(ip4.s_addr));
5,412✔
4549
      return true;
5,412✔
4550
    }
4551
  } else {
UNCOV
4552
    struct in6_addr ip6;
×
UNCOV
4553
    if (inet_pton(AF_INET6, strIp, &ip6) == 1) {
×
UNCOV
4554
      pIpRange->type = 1;
×
UNCOV
4555
      memcpy(&pIpRange->ipV6.addr[0], ip6.s6_addr, 8);
×
UNCOV
4556
      memcpy(&pIpRange->ipV6.addr[1], ip6.s6_addr + 8, 8);
×
UNCOV
4557
      return true;
×
4558
    }
4559
  }
4560

4561
  return false;
1,145✔
4562
}
4563

4564

4565

4566
SIpRangeNode* parseIpRange(SAstCreateContext* pCxt, const SToken* token) {
6,557✔
4567
  CHECK_PARSER_STATUS(pCxt);
6,557✔
4568

4569
#ifdef TD_ASTRA
4570
  return NULL;
4571
#else
4572

4573
  SIpRangeNode* node = NULL;
6,557✔
4574
  int32_t code = nodesMakeNode(QUERY_NODE_IP_RANGE, (SNode**)&node);
6,557✔
4575
  if (node == NULL) {
6,557✔
UNCOV
4576
    goto _err;
×
4577
  }
4578

4579
  char buf[64];
6,557✔
4580
  if (token->n >= sizeof(buf)) {
6,557✔
4581
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
4582
    goto _err;
×
4583
  }
4584
  memcpy(buf, token->z, token->n);
6,557✔
4585
  buf[token->n] = '\0';
6,557✔
4586
  (void)strdequote(buf);
6,557✔
4587

4588
  char* slash = strchr(buf, '/');
6,557✔
4589
  if (slash) {
6,557✔
4590
    *slash = '\0';
2,790✔
4591
  }
4592

4593
  if (!parseIp(buf, &node->range)) {
6,557✔
4594
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
1,145✔
4595
    goto _err;
1,145✔
4596
  }
4597

4598
  int32_t mask = 0;
5,412✔
4599
  if (!slash) {
5,412✔
4600
    mask = node->range.type == 0 ? 32 : 128;
2,947✔
4601
  } else if (taosStr2int32(slash + 1, &mask) != TSDB_CODE_SUCCESS) {
2,465✔
UNCOV
4602
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
UNCOV
4603
    goto _err;
×
4604
  }
4605

4606
  code = tIpRangeSetMask(&node->range, mask);
5,412✔
4607
  if (code != TSDB_CODE_SUCCESS) {
5,412✔
4608
    goto _err;
653✔
4609
  }
4610

4611
  return node;
4,759✔
4612

4613
_err:
1,798✔
4614
  pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
1,798✔
4615
  nodesDestroyNode((SNode*)node);
1,798✔
4616
  return NULL;
1,798✔
4617

4618
#endif
4619
}
4620

4621

4622

4623
SDateTimeRangeNode* parseDateTimeRange(SAstCreateContext* pCxt, const SToken* token) {
5,576✔
4624
  CHECK_PARSER_STATUS(pCxt);
5,576✔
4625

4626
  SDateTimeRangeNode* node = NULL;
5,576✔
4627
  int32_t code = nodesMakeNode(QUERY_NODE_DATE_TIME_RANGE, (SNode**)&node);
5,576✔
4628
  if (code != TSDB_CODE_SUCCESS) {
5,576✔
UNCOV
4629
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
×
UNCOV
4630
    goto _err;
×
4631
  }
4632

4633
  char buf[128];
5,576✔
4634
  if (token->n >= sizeof(buf)) {
5,576✔
UNCOV
4635
    code = TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG;
×
UNCOV
4636
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Date time range string is too long");
×
UNCOV
4637
    goto _err;
×
4638
  }
4639
  memcpy(buf, token->z, token->n);
5,576✔
4640
  buf[token->n] = '\0';
5,576✔
4641
  (void)strdequote(buf);
5,576✔
4642

4643
  code = TSDB_CODE_PAR_INVALID_OPTION_VALUE;
5,576✔
4644
  int32_t year = 0, month = 0, day = 0, hour = 0, minute = 0, duration = 0;
5,576✔
4645
  if (buf[0] >= '1' && buf[0] <= '9') {
8,528✔
4646
    // format: YYYY-MM-DD HH:MM duration
4647
    int ret = sscanf(buf, "%d-%d-%d %d:%d %d", &year, &month, &day, &hour, &minute, &duration);
4,264✔
4648
    if (ret != 6) {
4,264✔
4649
      goto _err;
984✔
4650
    }
4651
    if (month < 1 || month > 12) {
3,280✔
4652
      goto _err;
328✔
4653
    }
4654
  } else {
4655
    // format: WEEKDAY HH:MM duration
4656
    char weekday[4];
1,312✔
4657
    int ret = sscanf(buf, "%3s %d:%d %d", weekday, &hour, &minute, &duration);
1,312✔
4658
    if (ret != 4) {
1,312✔
4659
      goto _err;
328✔
4660
    }
4661
    day = taosParseShortWeekday(weekday);
984✔
4662
    if (day < 0 || day > 6) {
984✔
UNCOV
4663
      goto _err;
×
4664
    }
4665
    month = -1;
984✔
4666
  }
4667

4668
  node->range.year = (int16_t)year;
3,936✔
4669
  node->range.month = (int8_t)month;
3,936✔
4670
  node->range.day = (int8_t)day;
3,936✔
4671
  node->range.hour = (int8_t)hour;
3,936✔
4672
  node->range.minute = (int8_t)minute;
3,936✔
4673
  node->range.duration = duration;
3,936✔
4674
  if (!isValidDateTimeRange(&node->range)) {
3,936✔
UNCOV
4675
    goto _err;
×
4676
  }
4677

4678
  return node;
3,936✔
4679

4680
_err:
1,640✔
4681
  if (code == TSDB_CODE_PAR_INVALID_OPTION_VALUE) { // other error types have been set above
1,640✔
4682
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid date time range");
1,640✔
4683
  }
4684
  nodesDestroyNode((SNode*)node);
1,640✔
4685
  return NULL;
1,640✔
4686
}
4687

4688

4689
SUserOptions* createDefaultUserOptions(SAstCreateContext* pCxt) {
210,796✔
4690
  SUserOptions* pOptions = NULL;
210,796✔
4691
  int32_t code = nodesMakeNode(QUERY_NODE_USER_OPTIONS, (SNode**)&pOptions);
210,796✔
4692
  if (pOptions == NULL) {
210,796✔
UNCOV
4693
    pCxt->errCode = code;
×
UNCOV
4694
    return NULL;
×
4695
  }
4696

4697
  pOptions->enable = 1;
210,796✔
4698
  pOptions->sysinfo = 1;
210,796✔
4699
  pOptions->createdb = 0;
210,796✔
4700
  pOptions->isImport = 0;
210,796✔
4701
  pOptions->changepass = 2;
210,796✔
4702

4703
  pOptions->sessionPerUser = TSDB_USER_SESSION_PER_USER_DEFAULT;
210,796✔
4704
  pOptions->connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
210,796✔
4705
  pOptions->connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
210,796✔
4706
  pOptions->callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
210,796✔
4707
  pOptions->vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
210,796✔
4708
  pOptions->failedLoginAttempts = TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
210,796✔
4709
  pOptions->passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
210,796✔
4710
  pOptions->passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
210,796✔
4711
  pOptions->passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
210,796✔
4712
  pOptions->passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
210,796✔
4713
  pOptions->passwordGraceTime = TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
210,796✔
4714
  pOptions->inactiveAccountTime = TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
210,796✔
4715
  pOptions->allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
210,796✔
4716

4717
  return pOptions;
210,796✔
4718
}
4719

4720

4721

4722
SUserOptions* mergeUserOptions(SAstCreateContext* pCxt, SUserOptions* a, SUserOptions* b) {
249,883✔
4723
  if (a == NULL && b == NULL) {
249,883✔
4724
      return createDefaultUserOptions(pCxt);
210,796✔
4725
  }
4726
  if (b == NULL) {
39,087✔
4727
    return a;
24,845✔
4728
  }
4729
  if (a == NULL) {
14,242✔
UNCOV
4730
    return b;
×
4731
  }
4732

4733
  if (b->hasPassword) {
14,242✔
UNCOV
4734
    if (a->hasPassword) {
×
UNCOV
4735
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASS");
×
4736
    } else {
UNCOV
4737
      a->hasPassword = true;
×
UNCOV
4738
      tstrncpy(a->password, b->password, sizeof(a->password));
×
4739
    }
4740
  }
4741

4742
  if (b->hasTotpseed) {
14,242✔
UNCOV
4743
    if (a->hasTotpseed) {
×
UNCOV
4744
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TOTPSEED");
×
4745
    } else {
UNCOV
4746
      a->hasTotpseed = true;
×
UNCOV
4747
      tstrncpy(a->totpseed, b->totpseed, sizeof(a->totpseed));
×
4748
    }
4749
  }
4750

4751
  if (b->hasEnable) {
14,242✔
UNCOV
4752
    if (a->hasEnable) {
×
UNCOV
4753
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE/ACCOUNT LOCK/ACCOUNT UNLOCK");
×
4754
    } else {
4755
      a->hasEnable = true;
×
UNCOV
4756
      a->enable = b->enable;
×
4757
    }
4758
  }
4759

4760
  if (b->hasSysinfo) {
14,242✔
4761
    if (a->hasSysinfo) {
182✔
4762
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SYSINFO");
×
4763
    } else {
4764
      a->hasSysinfo = true;
182✔
4765
      a->sysinfo = b->sysinfo;
182✔
4766
    }
4767
  }
4768

4769
  if (b->hasCreatedb) {
14,242✔
4770
    if (a->hasCreatedb) {
2,189✔
4771
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CREATEDB");
×
4772
    } else {
4773
      a->hasCreatedb = true;
2,189✔
4774
      a->createdb = b->createdb;
2,189✔
4775
    }
4776
  }
4777

4778
  if (b->hasChangepass) {
14,242✔
UNCOV
4779
    if (a->hasChangepass) {
×
4780
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CHANGEPASS");
×
4781
    } else {
UNCOV
4782
      a->hasChangepass = true;
×
UNCOV
4783
      a->changepass = b->changepass;
×
4784
    }
4785
  }
4786

4787
  if (b->hasSessionPerUser) {
14,242✔
UNCOV
4788
    if (a->hasSessionPerUser) {
×
UNCOV
4789
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SESSION_PER_USER");
×
4790
    } else {
UNCOV
4791
      a->hasSessionPerUser = true;
×
UNCOV
4792
      a->sessionPerUser = b->sessionPerUser;
×
4793
    }
4794
  }
4795

4796
  if (b->hasConnectTime) {
14,242✔
4797
    if (a->hasConnectTime) {
328✔
UNCOV
4798
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_TIME");
×
4799
    } else {
4800
      a->hasConnectTime = true;
328✔
4801
      a->connectTime = b->connectTime;
328✔
4802
    }
4803
  }
4804

4805
  if (b->hasConnectIdleTime) {
14,242✔
4806
    if (a->hasConnectIdleTime) {
328✔
4807
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_IDLE_TIME");
×
4808
    } else {
4809
      a->hasConnectIdleTime = true;
328✔
4810
      a->connectIdleTime = b->connectIdleTime;
328✔
4811
    }
4812
  }
4813

4814
  if (b->hasCallPerSession) {
14,242✔
4815
    if (a->hasCallPerSession) {
328✔
4816
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CALLS_PER_SESSION");
×
4817
    } else {
4818
      a->hasCallPerSession = true;
328✔
4819
      a->callPerSession = b->callPerSession;
328✔
4820
    }
4821
  }
4822

4823
  if (b->hasVnodePerCall) {
14,242✔
4824
    if (a->hasVnodePerCall) {
492✔
UNCOV
4825
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "VNODES_PER_CALL");
×
4826
    } else {
4827
      a->hasVnodePerCall = true;
492✔
4828
      a->vnodePerCall = b->vnodePerCall;
492✔
4829
    }
4830
  }
4831

4832
  if (b->hasFailedLoginAttempts) {
14,242✔
4833
    if (a->hasFailedLoginAttempts) {
328✔
UNCOV
4834
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "FAILED_LOGIN_ATTEMPTS");
×
4835
    } else {
4836
      a->hasFailedLoginAttempts = true;
328✔
4837
      a->failedLoginAttempts = b->failedLoginAttempts;
328✔
4838
    }
4839
  }
4840

4841
  if (b->hasPasswordLifeTime) {
14,242✔
4842
    if (a->hasPasswordLifeTime) {
328✔
UNCOV
4843
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LIFE_TIME");
×
4844
    } else {
4845
      a->hasPasswordLifeTime = true;
328✔
4846
      a->passwordLifeTime = b->passwordLifeTime;
328✔
4847
    }
4848
  }
4849

4850
  if (b->hasPasswordReuseTime) {
14,242✔
4851
    if (a->hasPasswordReuseTime) {
2,173✔
UNCOV
4852
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_TIME");
×
4853
    } else {
4854
      a->hasPasswordReuseTime = true;
2,173✔
4855
      a->passwordReuseTime = b->passwordReuseTime;
2,173✔
4856
    }
4857
  }
4858

4859
  if (b->hasPasswordReuseMax) {
14,242✔
4860
    if (a->hasPasswordReuseMax) {
2,043✔
UNCOV
4861
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_MAX");
×
4862
    } else {
4863
      a->hasPasswordReuseMax = true;
2,043✔
4864
      a->passwordReuseMax = b->passwordReuseMax;
2,043✔
4865
    }
4866
  }
4867

4868
  if (b->hasPasswordLockTime) {
14,242✔
4869
    if (a->hasPasswordLockTime) {
328✔
UNCOV
4870
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LOCK_TIME");
×
4871
    } else {
4872
      a->hasPasswordLockTime = true;
328✔
4873
      a->passwordLockTime = b->passwordLockTime;
328✔
4874
    }
4875
  }
4876

4877
  if (b->hasPasswordGraceTime) {
14,242✔
4878
    if (a->hasPasswordGraceTime) {
328✔
UNCOV
4879
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_GRACE_TIME");
×
4880
    } else {
4881
      a->hasPasswordGraceTime = true;
328✔
4882
      a->passwordGraceTime = b->passwordGraceTime;
328✔
4883
    }
4884
  }
4885

4886
  if (b->hasInactiveAccountTime) {
14,242✔
4887
    if (a->hasInactiveAccountTime) {
328✔
UNCOV
4888
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "INACTIVE_ACCOUNT_TIME");
×
4889
    } else {
4890
      a->hasInactiveAccountTime = true;
328✔
4891
      a->inactiveAccountTime = b->inactiveAccountTime;
328✔
4892
    }
4893
  }
4894

4895
  if (b->hasAllowTokenNum) {
14,242✔
4896
    if (a->hasAllowTokenNum) {
328✔
UNCOV
4897
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ALLOW_TOKEN_NUM");
×
4898
    } else {
4899
      a->hasAllowTokenNum = true;
328✔
4900
      a->allowTokenNum = b->allowTokenNum;
328✔
4901
    }
4902
  }
4903

4904
  if (b->pIpRanges != NULL) {
14,242✔
4905
    if (a->pIpRanges == NULL) {
1,601✔
4906
      a->pIpRanges = b->pIpRanges;
1,273✔
4907
    } else {
4908
      int32_t code = nodesListAppendList(a->pIpRanges, b->pIpRanges);
328✔
4909
      if (code != TSDB_CODE_SUCCESS) {
328✔
UNCOV
4910
        pCxt->errCode = code;
×
4911
      }
4912
    }
4913
    b->pIpRanges = NULL;
1,601✔
4914
  }
4915

4916
  if (b->pDropIpRanges != NULL) {
14,242✔
4917
    if (a->pDropIpRanges == NULL) {
164✔
UNCOV
4918
      a->pDropIpRanges = b->pDropIpRanges;
×
4919
    } else {
4920
      int32_t code = nodesListAppendList(a->pDropIpRanges, b->pDropIpRanges);
164✔
4921
      if (code != TSDB_CODE_SUCCESS) {
164✔
4922
        pCxt->errCode = code;
×
4923
      }
4924
    }
4925
    b->pDropIpRanges = NULL;
164✔
4926
  }
4927

4928
  if (b->pTimeRanges != NULL) {
14,242✔
4929
    if (a->pTimeRanges == NULL) {
984✔
4930
      a->pTimeRanges = b->pTimeRanges;
656✔
4931
    } else {
4932
      int32_t code = nodesListAppendList(a->pTimeRanges, b->pTimeRanges);
328✔
4933
      if (code != TSDB_CODE_SUCCESS) {
328✔
UNCOV
4934
        pCxt->errCode = code;
×
4935
      }
4936
    }
4937
    b->pTimeRanges = NULL;
984✔
4938
  }
4939

4940
  if (b->pDropTimeRanges != NULL) {
14,242✔
4941
    if (a->pDropTimeRanges == NULL) {
164✔
UNCOV
4942
      a->pDropTimeRanges = b->pDropTimeRanges;
×
4943
    } else {
4944
      int32_t code = nodesListAppendList(a->pDropTimeRanges, b->pDropTimeRanges);
164✔
4945
      if (code != TSDB_CODE_SUCCESS) {
164✔
UNCOV
4946
        pCxt->errCode = code;
×
4947
      }
4948
    }
4949
    b->pDropTimeRanges = NULL;
164✔
4950
  }
4951

4952
  if (b->pSecurityLevels != NULL) {
14,242✔
NEW
4953
    if (a->pSecurityLevels == NULL) {
×
NEW
4954
      a->pSecurityLevels = b->pSecurityLevels;
×
NEW
4955
      b->pSecurityLevels = NULL;
×
4956
    } else {
NEW
4957
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SECURITY_LEVELS");
×
4958
    }
4959
  }
4960

4961
  nodesDestroyNode((SNode*)b);
14,242✔
4962
  return a;
14,242✔
4963
}
4964

4965

4966

UNCOV
4967
void setUserOptionsTotpseed(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pTotpseed) {
×
4968
  pUserOptions->hasTotpseed = true;
×
4969

UNCOV
4970
  if (pTotpseed == NULL) { // clear TOTP secret
×
UNCOV
4971
    memset(pUserOptions->totpseed, 0, sizeof(pUserOptions->totpseed));
×
UNCOV
4972
    return;
×
4973
  }
4974

UNCOV
4975
  if (pTotpseed->n >= sizeof(pUserOptions->totpseed) * 2) {
×
4976
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
UNCOV
4977
    return;
×
4978
  }
4979

4980
  char buf[sizeof(pUserOptions->totpseed) * 2 + 1];
×
UNCOV
4981
  memcpy(buf, pTotpseed->z, pTotpseed->n);
×
UNCOV
4982
  buf[pTotpseed->n] = 0;
×
UNCOV
4983
  (void)strdequote(buf);
×
UNCOV
4984
  size_t len = strtrim(buf);
×
4985

UNCOV
4986
  if (len >= sizeof(pUserOptions->totpseed)) {
×
4987
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
4988
  } else if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
4989
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT, "TOTPSEED", TSDB_USER_TOTPSEED_MIN_LEN);
×
4990
  } else {
4991
    tstrncpy(pUserOptions->totpseed, buf, sizeof(pUserOptions->totpseed));
×
4992
  }
4993
}
4994

4995

4996

4997
void setUserOptionsPassword(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pPassword) {
167,986✔
4998
  pUserOptions->hasPassword = true;
167,986✔
4999

5000
  if (pPassword->n >= sizeof(pUserOptions->password) * 2) {
167,986✔
5001
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
5002
    return;
×
5003
  }
5004

5005
  char buf[sizeof(pUserOptions->password) * 2 + 1];
165,139✔
5006
  memcpy(buf, pPassword->z, pPassword->n);
167,986✔
5007
  buf[pPassword->n] = 0;
167,986✔
5008
  (void)strdequote(buf);
167,986✔
5009
  size_t len = strtrim(buf);
167,986✔
5010

5011
  if (len >= sizeof(pUserOptions->password)) {
167,986✔
5012
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
713✔
5013
  } else if (len < TSDB_PASSWORD_MIN_LEN) {
167,273✔
5014
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
5,762✔
5015
  } else {
5016
    tstrncpy(pUserOptions->password, buf, sizeof(pUserOptions->password));
161,511✔
5017
  }
5018
}
5019

5020

5021

5022
static bool isValidUserOptions(SAstCreateContext* pCxt, const SUserOptions* opts) {
185,951✔
5023
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
185,951✔
5024
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
1,349✔
5025
    return false;
1,349✔
5026
  }
5027

5028
  if (opts->hasSysinfo && (opts->sysinfo < 0 || opts->sysinfo > 1)) {
184,602✔
5029
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SYSINFO");
2,503✔
5030
    return false;
2,503✔
5031
  }
5032

5033
  if (opts->hasIsImport && (opts->isImport < 0 || opts->isImport > 1)) {
182,099✔
UNCOV
5034
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "IS_IMPORT");
×
5035
    return false;
×
5036
  }
5037

5038
  if (opts->hasCreatedb && (opts->createdb < 0 || opts->createdb > 1)) {
182,099✔
5039
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CREATEDB");
1,349✔
5040
    return false;
1,349✔
5041
  }
5042

5043
  if (opts->hasTotpseed && opts->totpseed[0] != 0 && !taosIsComplexString(opts->totpseed)) {
180,750✔
UNCOV
5044
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TOTPSEED");
×
UNCOV
5045
    return false;
×
5046
  }
5047

5048
  if (opts->hasPassword && !isValidPassword(pCxt, opts->password, opts->hasIsImport && opts->isImport)) {
180,750✔
UNCOV
5049
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
×
UNCOV
5050
    return false;
×
5051
  }
5052

5053
  if (opts->hasChangepass && (opts->changepass < 0 || opts->changepass > 2)) {
180,750✔
5054
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CHANGEPASS");
164✔
5055
    return false;
164✔
5056
  }
5057

5058
  if (opts->hasSessionPerUser && (opts->sessionPerUser < -1 || opts->sessionPerUser == 0)) {
180,586✔
UNCOV
5059
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SESSION_PER_USER");
×
UNCOV
5060
    return false;
×
5061
  }
5062

5063
  if (opts->hasConnectTime && (opts->connectTime < -1 || opts->connectTime == 0)) {
180,586✔
UNCOV
5064
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_TIME");
×
UNCOV
5065
    return false;
×
5066
  }
5067

5068
  if (opts->hasConnectIdleTime && (opts->connectIdleTime < -1 || opts->connectIdleTime == 0)) {
180,586✔
5069
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_IDLE_TIME");
×
UNCOV
5070
    return false;
×
5071
  }
5072

5073
  if (opts->hasCallPerSession && (opts->callPerSession < -1 || opts->callPerSession == 0)) {
180,586✔
UNCOV
5074
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CALLS_PER_SESSION");
×
UNCOV
5075
    return false;
×
5076
  }
5077

5078
  if (opts->hasVnodePerCall && (opts->vnodePerCall < -1 || opts->vnodePerCall == 0)) {
180,586✔
5079
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "VNODES_PER_CALL");
164✔
5080
    return false;
164✔
5081
  }
5082

5083
  if (opts->hasFailedLoginAttempts && (opts->failedLoginAttempts < -1 || opts->failedLoginAttempts == 0)) {
180,422✔
5084
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "FAILED_LOGIN_ATTEMPTS");
164✔
5085
    return false;
164✔
5086
  }
5087

5088
  if (opts->hasPasswordLockTime && (opts->passwordLockTime < -1 || opts->passwordLockTime == 0)) {
180,258✔
5089
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LOCK_TIME");
164✔
5090
    return false;
164✔
5091
  }
5092

5093
  if (opts->hasPasswordLifeTime && opts->passwordLifeTime != -1 && opts->passwordLifeTime < TSDB_USER_PASSWORD_LIFE_TIME_MIN) {
180,094✔
5094
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LIFE_TIME");
164✔
5095
    return false;
164✔
5096
  }
5097

5098
  if (opts->hasPasswordGraceTime && (opts->passwordGraceTime < -1)) {
179,930✔
5099
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_GRACE_TIME");
×
UNCOV
5100
    return false;
×
5101
  }
5102

5103
  if (opts->hasPasswordReuseTime && (opts->passwordReuseTime < 0 || opts->passwordReuseTime > TSDB_USER_PASSWORD_REUSE_TIME_MAX)) {
179,930✔
5104
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_TIME");
164✔
5105
    return false;
164✔
5106
  }
5107

5108
  if (opts->hasPasswordReuseMax && (opts->passwordReuseMax < 0 || opts->passwordReuseMax > TSDB_USER_PASSWORD_REUSE_MAX_MAX)) {
179,766✔
5109
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_MAX");
164✔
5110
    return false;
164✔
5111
  }
5112

5113
  if (opts->hasInactiveAccountTime && (opts->inactiveAccountTime < -1 || opts->inactiveAccountTime == 0)) {
179,602✔
5114
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "INACTIVE_ACCOUNT_TIME");
164✔
5115
    return false;
164✔
5116
  }
5117

5118
  if (opts->hasAllowTokenNum && opts->allowTokenNum < -1) {
179,438✔
UNCOV
5119
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ALLOW_TOKEN_NUM");
×
UNCOV
5120
    return false;
×
5121
  }
5122

5123
  // ip ranges and date time ranges has been validated during parsing
5124

5125
  return true;
179,438✔
5126
}
5127

5128

5129

5130
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* opts, bool ignoreExists) {
128,563✔
5131
  SCreateUserStmt* pStmt = NULL;
128,563✔
5132

5133
  CHECK_PARSER_STATUS(pCxt);
128,563✔
5134
  CHECK_NAME(checkUserName(pCxt, pUserName));
120,723✔
5135

5136
  if (!isValidUserOptions(pCxt, opts)) {
120,386✔
5137
    goto _err;
2,661✔
5138
  }
5139

5140
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
117,725✔
5141
  CHECK_MAKE_NODE(pStmt);
117,725✔
5142

5143
  pStmt->hasSessionPerUser = opts->hasSessionPerUser;
117,725✔
5144
  pStmt->hasConnectTime = opts->hasConnectTime;
117,725✔
5145
  pStmt->hasConnectIdleTime = opts->hasConnectIdleTime;
117,725✔
5146
  pStmt->hasCallPerSession = opts->hasCallPerSession;
117,725✔
5147
  pStmt->hasVnodePerCall = opts->hasVnodePerCall;
117,725✔
5148
  pStmt->hasFailedLoginAttempts = opts->hasFailedLoginAttempts;
117,725✔
5149
  pStmt->hasPasswordLifeTime = opts->hasPasswordLifeTime;
117,725✔
5150
  pStmt->hasPasswordReuseTime = opts->hasPasswordReuseTime;
117,725✔
5151
  pStmt->hasPasswordReuseMax = opts->hasPasswordReuseMax;
117,725✔
5152
  pStmt->hasPasswordLockTime = opts->hasPasswordLockTime;
117,725✔
5153
  pStmt->hasPasswordGraceTime = opts->hasPasswordGraceTime;
117,725✔
5154
  pStmt->hasInactiveAccountTime = opts->hasInactiveAccountTime;
117,725✔
5155
  pStmt->hasAllowTokenNum = opts->hasAllowTokenNum;
117,725✔
5156

5157
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
117,725✔
5158
  tstrncpy(pStmt->password, opts->password, sizeof(pStmt->password));
117,725✔
5159
  tstrncpy(pStmt->totpseed, opts->totpseed, sizeof(pStmt->totpseed));
117,725✔
5160

5161
  pStmt->ignoreExists = ignoreExists;
117,725✔
5162
  pStmt->sysinfo = opts->sysinfo;
117,725✔
5163
  pStmt->createDb = opts->createdb;
117,725✔
5164
  pStmt->isImport = opts->isImport;
117,725✔
5165
  pStmt->changepass = opts->changepass;
117,725✔
5166
  pStmt->enable = opts->enable;
117,725✔
5167

5168
  pStmt->sessionPerUser = opts->sessionPerUser;
117,725✔
5169
  pStmt->connectTime = opts->connectTime;
117,725✔
5170
  pStmt->connectIdleTime = opts->connectIdleTime;
117,725✔
5171
  pStmt->callPerSession = opts->callPerSession;
117,725✔
5172
  pStmt->vnodePerCall = opts->vnodePerCall;
117,725✔
5173
  pStmt->failedLoginAttempts = opts->failedLoginAttempts;
117,725✔
5174
  pStmt->passwordLifeTime = opts->passwordLifeTime;
117,725✔
5175
  pStmt->passwordReuseTime = opts->passwordReuseTime;
117,725✔
5176
  pStmt->passwordReuseMax = opts->passwordReuseMax;
117,725✔
5177
  pStmt->passwordLockTime = opts->passwordLockTime;
117,725✔
5178
  pStmt->passwordGraceTime = opts->passwordGraceTime;
117,725✔
5179
  pStmt->inactiveAccountTime = opts->inactiveAccountTime;
117,725✔
5180
  pStmt->allowTokenNum = opts->allowTokenNum;
117,725✔
5181

5182
  pStmt->numIpRanges = LIST_LENGTH(opts->pIpRanges);
117,725✔
5183
  pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpRange));
117,725✔
5184
  CHECK_OUT_OF_MEM(pStmt->pIpRanges);
117,725✔
5185
  int i = 0;
117,725✔
5186
  SNode* pNode = NULL;
117,725✔
5187
  FOREACH(pNode, opts->pIpRanges) {
120,800✔
5188
    SIpRangeNode* node = (SIpRangeNode*)(pNode);
3,075✔
5189
    pStmt->pIpRanges[i++] = node->range;
3,075✔
5190
  }
5191

5192
  pStmt->numTimeRanges = LIST_LENGTH(opts->pTimeRanges);
117,725✔
5193
  pStmt->pTimeRanges = taosMemoryMalloc(pStmt->numTimeRanges * sizeof(SDateTimeRange));
117,725✔
5194
  CHECK_OUT_OF_MEM(pStmt->pTimeRanges);
117,725✔
5195
  i = 0;
117,725✔
5196
  pNode = NULL;
117,725✔
5197
  FOREACH(pNode, opts->pTimeRanges) {
120,349✔
5198
    SDateTimeRangeNode* node = (SDateTimeRangeNode*)(pNode);
2,624✔
5199
    pStmt->pTimeRanges[i++] = node->range;
2,624✔
5200
  }
5201
  pStmt->userOps = *opts;  // only for privilege checking
117,725✔
5202
  TSWAP(pStmt->pSecurityLevels, opts->pSecurityLevels);
117,725✔
5203

5204
  nodesDestroyNode((SNode*)opts);
117,725✔
5205
  return (SNode*)pStmt;
117,725✔
5206

5207
_err:
10,838✔
5208
  nodesDestroyNode((SNode*)pStmt);
10,838✔
5209
  nodesDestroyNode((SNode*)opts);
10,838✔
5210
  return NULL;
10,838✔
5211
}
5212

5213

5214

5215
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* pUserOptions) {
67,638✔
5216
  SAlterUserStmt* pStmt = NULL;
67,638✔
5217
  CHECK_PARSER_STATUS(pCxt);
67,638✔
5218
  CHECK_NAME(checkUserName(pCxt, pUserName));
65,565✔
5219
  if (!isValidUserOptions(pCxt, pUserOptions)) {
65,565✔
5220
    goto _err;
3,852✔
5221
  }
5222

5223
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
61,713✔
5224
  CHECK_MAKE_NODE(pStmt);
61,713✔
5225
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
61,713✔
5226
  pStmt->pUserOptions = pUserOptions;
61,713✔
5227
  return (SNode*)pStmt;
61,713✔
5228

5229
_err:
5,925✔
5230
  nodesDestroyNode((SNode*)pStmt);
5,925✔
5231
  nodesDestroyNode((SNode*)pUserOptions);
5,925✔
5232
  return NULL;
5,925✔
5233
}
5234

5235

5236

5237
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName, bool ignoreNotExists) {
59,796✔
5238
  CHECK_PARSER_STATUS(pCxt);
59,796✔
5239
  CHECK_NAME(checkUserName(pCxt, pUserName));
59,796✔
5240
  SDropUserStmt* pStmt = NULL;
59,796✔
5241
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
59,796✔
5242
  CHECK_MAKE_NODE(pStmt);
59,796✔
5243
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
59,796✔
5244
  pStmt->ignoreNotExists = ignoreNotExists;
59,796✔
5245
  return (SNode*)pStmt;
59,796✔
UNCOV
5246
_err:
×
UNCOV
5247
  return NULL;
×
5248
}
5249

5250
static bool checkRoleName(SAstCreateContext* pCxt, SToken* pName, bool checkSysName) {
998,719✔
5251
  if (NULL == pName) {
998,719✔
UNCOV
5252
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5253
  } else {
5254
    if (pName->n >= TSDB_ROLE_LEN) {
998,719✔
UNCOV
5255
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
5256
    }
5257
  }
5258
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
998,719✔
5259
    trimEscape(pCxt, pName, true);
998,719✔
5260
  }
5261
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
998,719✔
5262
    if (checkSysName && taosStrncasecmp(pName->z, "sys", 3) == 0) {  // system reserved role name prefix
998,719✔
5263
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
489✔
5264
                                              "Cannot create/drop/alter roles with reserved prefix 'sys'");
5265
    }
5266
  }
5267
  return TSDB_CODE_SUCCESS == pCxt->errCode;
998,719✔
5268
}
5269

5270

5271
STokenOptions* createDefaultTokenOptions(SAstCreateContext* pCxt) {
29,788✔
5272
  STokenOptions* pOptions = NULL;
29,788✔
5273
  int32_t code = nodesMakeNode(QUERY_NODE_TOKEN_OPTIONS, (SNode**)&pOptions);
29,788✔
5274
  if (pOptions == NULL) {
29,788✔
UNCOV
5275
    pCxt->errCode = code;
×
UNCOV
5276
    return NULL;
×
5277
  }
5278

5279
  pOptions->enable = 1;
29,788✔
5280
  pOptions->ttl = 0;
29,788✔
5281
  return pOptions;
29,788✔
5282
}
5283

5284

5285

5286
STokenOptions* mergeTokenOptions(SAstCreateContext* pCxt, STokenOptions* a, STokenOptions* b) {
8,833✔
5287
  if (a == NULL && b == NULL) {
8,833✔
5288
      return createDefaultTokenOptions(pCxt);
7,206✔
5289
  }
5290
  if (b == NULL) {
1,627✔
5291
    return a;
×
5292
  }
5293
  if (a == NULL) {
1,627✔
UNCOV
5294
    return b;
×
5295
  }
5296

5297
  if (b->hasEnable) {
1,627✔
UNCOV
5298
    if (a->hasEnable) {
×
UNCOV
5299
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE");
×
5300
    } else {
UNCOV
5301
      a->hasEnable = true;
×
UNCOV
5302
      a->enable = b->enable;
×
5303
    }
5304
  }
5305

5306
  if (b->hasTtl) {
1,627✔
5307
    if (a->hasTtl) {
717✔
UNCOV
5308
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TTL");
×
5309
    } else {
5310
      a->hasTtl = true;
717✔
5311
      a->ttl = b->ttl;
717✔
5312
    }
5313
  }
5314

5315
  if (b->hasProvider) {
1,627✔
5316
    if (a->hasProvider) {
546✔
UNCOV
5317
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PROVIDER");
×
5318
    } else {
5319
      a->hasProvider = true;
546✔
5320
      tstrncpy(a->provider, b->provider, sizeof(a->provider));
546✔
5321
    }
5322
  }
5323

5324
  if (b->hasExtraInfo) {
1,627✔
5325
    if (a->hasExtraInfo) {
364✔
UNCOV
5326
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "EXTRA_INFO");
×
5327
    } else {
5328
      a->hasExtraInfo = true;
364✔
5329
      tstrncpy(a->extraInfo, b->extraInfo, sizeof(a->extraInfo));
364✔
5330
    }
5331
  }
5332
  nodesDestroyNode((SNode*)b);
1,627✔
5333
  return a;
1,627✔
5334
}
5335

5336

5337

5338
void setTokenOptionsProvider(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pProvider) {
1,092✔
5339
  pTokenOptions->hasProvider = true;
1,092✔
5340

5341
  if (pProvider->n >= sizeof(pTokenOptions->provider) * 2) {
1,092✔
UNCOV
5342
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
×
UNCOV
5343
    return;
×
5344
  }
5345

5346
  char buf[sizeof(pTokenOptions->provider) * 2 + 1];
1,092✔
5347
  memcpy(buf, pProvider->z, pProvider->n);
1,092✔
5348
  buf[pProvider->n] = 0;
1,092✔
5349
  (void)strdequote(buf);
1,092✔
5350
  size_t len = strtrim(buf);
1,092✔
5351

5352
  if (len >= sizeof(pTokenOptions->provider)) {
1,092✔
5353
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
364✔
5354
  } else {
5355
    tstrncpy(pTokenOptions->provider, buf, sizeof(pTokenOptions->provider));
728✔
5356
  }
5357
}
5358

5359

5360

5361
void setTokenOptionsExtraInfo(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pExtraInfo) {
1,092✔
5362
  pTokenOptions->hasExtraInfo = true;
1,092✔
5363

5364
  if (pExtraInfo->n >= sizeof(pTokenOptions->extraInfo) * 2) {
1,092✔
UNCOV
5365
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
×
UNCOV
5366
    return;
×
5367
  }
5368

5369
  char buf[sizeof(pTokenOptions->extraInfo) * 2 + 1];
1,092✔
5370
  memcpy(buf, pExtraInfo->z, pExtraInfo->n);
1,092✔
5371
  buf[pExtraInfo->n] = 0;
1,092✔
5372
  (void)strdequote(buf);
1,092✔
5373
  size_t len = strtrim(buf);
1,092✔
5374

5375
  if (len >= sizeof(pTokenOptions->extraInfo)) {
1,092✔
5376
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
364✔
5377
  } else {
5378
    tstrncpy(pTokenOptions->extraInfo, buf, sizeof(pTokenOptions->extraInfo));
728✔
5379
  }
5380
}
5381

5382

5383

5384
static bool isValidTokenOptions(SAstCreateContext* pCxt, const STokenOptions* opts) {
4,851✔
5385
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
4,851✔
5386
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
364✔
5387
    return false;
364✔
5388
  }
5389

5390
  if (opts->hasTtl && (opts->ttl < 0)) {
4,487✔
UNCOV
5391
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TTL");
×
UNCOV
5392
    return false;
×
5393
  }
5394

5395
  return true;
4,487✔
5396
}
5397

5398

5399

5400
static bool checkTokenName(SAstCreateContext* pCxt, SToken* pTokenName) {
30,497✔
5401
  if (NULL == pTokenName) {
30,497✔
5402
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5403
  } else {
5404
    if (pTokenName->n >= TSDB_TOKEN_NAME_LEN) {
30,497✔
5405
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOKEN_NAME", TSDB_TOKEN_NAME_LEN);
364✔
5406
    }
5407
  }
5408
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
30,497✔
5409
    trimEscape(pCxt, pTokenName, true);
30,133✔
5410
  }
5411
  return TSDB_CODE_SUCCESS == pCxt->errCode;
30,497✔
5412
}
5413

5414
SNode* createCreateRoleStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pName) {
4,935✔
5415
  CHECK_PARSER_STATUS(pCxt);
4,935✔
5416
  CHECK_NAME(checkRoleName(pCxt, pName, true));
4,935✔
5417
  SCreateRoleStmt* pStmt = NULL;
4,935✔
5418
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ROLE_STMT, (SNode**)&pStmt);
4,935✔
5419
  CHECK_MAKE_NODE(pStmt);
4,935✔
5420
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
4,935✔
5421
  pStmt->ignoreExists = ignoreExists;
4,935✔
5422
  return (SNode*)pStmt;
4,935✔
UNCOV
5423
_err:
×
UNCOV
5424
  return NULL;
×
5425
}
5426

5427
SNode* createDropRoleStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pName) {
2,282✔
5428
  CHECK_PARSER_STATUS(pCxt);
2,282✔
5429
  CHECK_NAME(checkRoleName(pCxt, pName, true));
2,282✔
5430
  SDropRoleStmt* pStmt = NULL;
1,793✔
5431
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ROLE_STMT, (SNode**)&pStmt);
1,793✔
5432
  CHECK_MAKE_NODE(pStmt);
1,793✔
5433
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
1,793✔
5434
  pStmt->ignoreNotExists = ignoreNotExists;
1,793✔
5435
  return (SNode*)pStmt;
1,793✔
5436
_err:
489✔
5437
  return NULL;
489✔
5438
}
5439

5440
/**
5441
 * used by user and role
5442
 */
5443
SNode* createAlterRoleStmt(SAstCreateContext* pCxt, SToken* pName, int8_t alterType, void* pAlterInfo) {
489✔
5444
  SAlterRoleStmt* pStmt = NULL;
489✔
5445
  CHECK_PARSER_STATUS(pCxt);
489✔
5446
  CHECK_NAME(checkUserName(pCxt, pName));
489✔
5447
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ROLE_STMT, (SNode**)&pStmt);
489✔
5448
  CHECK_MAKE_NODE(pStmt);
489✔
5449
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
489✔
5450
  pStmt->alterType = alterType;
489✔
5451
  switch (alterType) {
489✔
5452
    case TSDB_ALTER_ROLE_LOCK: {
489✔
5453
      SToken* pVal = pAlterInfo;
489✔
5454
      pStmt->lock = taosStr2Int8(pVal->z, NULL, 10);
489✔
5455
      break;
489✔
5456
    }
UNCOV
5457
    default:
×
UNCOV
5458
      break;
×
5459
  }
5460
  return (SNode*)pStmt;
489✔
UNCOV
5461
_err:
×
UNCOV
5462
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
5463
  return NULL;
×
5464
}
5465

5466

5467
SNode* createCreateTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, SToken* pUserName, STokenOptions* opts, bool ignoreExists) {
25,119✔
5468
  SCreateTokenStmt* pStmt = NULL;
25,119✔
5469

5470
  CHECK_PARSER_STATUS(pCxt);
25,119✔
5471
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
24,755✔
5472
  CHECK_NAME(checkUserName(pCxt, pUserName));
24,391✔
5473

5474
  if (opts == NULL) {
24,391✔
5475
    opts = createDefaultTokenOptions(pCxt);
22,582✔
5476
  } else if (!isValidTokenOptions(pCxt, opts)) {
1,809✔
5477
    goto _err;
182✔
5478
  }
5479

5480
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOKEN_STMT, (SNode**)&pStmt);
24,209✔
5481
  CHECK_MAKE_NODE(pStmt);
24,209✔
5482

5483
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
24,209✔
5484
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
24,209✔
5485
  pStmt->enable = opts->enable;
24,209✔
5486
  pStmt->ignoreExists = ignoreExists;
24,209✔
5487
  pStmt->ttl = opts->ttl;
24,209✔
5488
  tstrncpy(pStmt->provider, opts->provider, sizeof(pStmt->provider));
24,209✔
5489
  tstrncpy(pStmt->extraInfo, opts->extraInfo, sizeof(pStmt->extraInfo));
24,209✔
5490
  nodesDestroyNode((SNode*)opts);
24,209✔
5491
  return (SNode*)pStmt;
24,209✔
5492

5493
_err:
910✔
5494
  nodesDestroyNode((SNode*)pStmt);
910✔
5495
  nodesDestroyNode((SNode*)opts);
910✔
5496
  return NULL;
910✔
5497
}
5498

5499

5500

5501
SNode* createAlterTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, STokenOptions* opts) {
3,406✔
5502
  SAlterTokenStmt* pStmt = NULL;
3,406✔
5503

5504
  CHECK_PARSER_STATUS(pCxt);
3,406✔
5505
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
3,042✔
5506
  if (!isValidTokenOptions(pCxt, opts)) {
3,042✔
5507
    goto _err;
182✔
5508
  }
5509

5510
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TOKEN_STMT, (SNode**)&pStmt);
2,860✔
5511
  CHECK_MAKE_NODE(pStmt);
2,860✔
5512

5513
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
2,860✔
5514
  pStmt->pTokenOptions = opts;
2,860✔
5515
  return (SNode*)pStmt;
2,860✔
5516

5517
_err:
546✔
5518
  nodesDestroyNode((SNode*)pStmt);
546✔
5519
  nodesDestroyNode((SNode*)opts);
546✔
5520
  return NULL;
546✔
5521
}
5522

5523

5524

5525
SNode* createDropTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, bool ignoreNotExists) {
2,700✔
5526
  SDropTokenStmt* pStmt = NULL;
2,700✔
5527

5528
  CHECK_PARSER_STATUS(pCxt);
2,700✔
5529
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
2,700✔
5530

5531
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOKEN_STMT, (SNode**)&pStmt);
2,700✔
5532
  CHECK_MAKE_NODE(pStmt);
2,700✔
5533

5534
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
2,700✔
5535
  pStmt->ignoreNotExists = ignoreNotExists;
2,700✔
5536
  return (SNode*)pStmt;
2,700✔
5537

UNCOV
5538
_err:
×
UNCOV
5539
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
5540
  return NULL;
×
5541
}
5542

5543
SNode* createCreateTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
19,514✔
5544
  SCreateTotpSecretStmt* pStmt = NULL;
19,514✔
5545

5546
  CHECK_PARSER_STATUS(pCxt);
19,514✔
5547
  CHECK_NAME(checkUserName(pCxt, pUserName));
19,514✔
5548

5549
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOTP_SECRET_STMT, (SNode**)&pStmt);
19,337✔
5550
  CHECK_MAKE_NODE(pStmt);
19,337✔
5551

5552
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
19,337✔
5553
  return (SNode*)pStmt;
19,337✔
5554

5555
_err:
177✔
5556
  nodesDestroyNode((SNode*)pStmt);
177✔
5557
  return NULL;
177✔
5558
}
5559

5560

5561
SNode* createDropTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
9,204✔
5562
  SDropTotpSecretStmt* pStmt = NULL;
9,204✔
5563

5564
  CHECK_PARSER_STATUS(pCxt);
9,204✔
5565
  CHECK_NAME(checkUserName(pCxt, pUserName));
9,204✔
5566

5567
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOTP_SECRET_STMT, (SNode**)&pStmt);
7,434✔
5568
  CHECK_MAKE_NODE(pStmt);
7,434✔
5569

5570
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
7,434✔
5571
  return (SNode*)pStmt;
7,434✔
5572

5573
_err:
1,770✔
5574
  nodesDestroyNode((SNode*)pStmt);
1,770✔
5575
  return NULL;
1,770✔
5576
}
5577

5578

UNCOV
5579
SNode* createDropEncryptAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId) {
×
UNCOV
5580
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
5581
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
UNCOV
5582
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
UNCOV
5583
    goto _err;
×
5584
  }
UNCOV
5585
  SDropEncryptAlgrStmt* pStmt = NULL;
×
UNCOV
5586
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ENCRYPT_ALGR_STMT, (SNode**)&pStmt);
×
UNCOV
5587
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
5588
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
UNCOV
5589
  return (SNode*)pStmt;
×
UNCOV
5590
_err:
×
UNCOV
5591
  return NULL;
×
5592
}
5593

5594
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
177,317✔
5595
  CHECK_PARSER_STATUS(pCxt);
177,317✔
5596
  SCreateDnodeStmt* pStmt = NULL;
177,317✔
5597
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
177,317✔
5598
  CHECK_MAKE_NODE(pStmt);
177,317✔
5599
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
177,317✔
UNCOV
5600
    nodesDestroyNode((SNode*)pStmt);
×
UNCOV
5601
    return NULL;
×
5602
  }
5603
  return (SNode*)pStmt;
177,317✔
UNCOV
5604
_err:
×
UNCOV
5605
  return NULL;
×
5606
}
5607

5608
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
18,846✔
5609
  CHECK_PARSER_STATUS(pCxt);
18,846✔
5610
  SDropDnodeStmt* pStmt = NULL;
18,846✔
5611
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
18,846✔
5612
  CHECK_MAKE_NODE(pStmt);
18,846✔
5613
  if (TK_NK_INTEGER == pDnode->type) {
18,846✔
5614
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
14,070✔
5615
  } else {
5616
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
4,776✔
5617
      nodesDestroyNode((SNode*)pStmt);
×
5618
      return NULL;
×
5619
    }
5620
  }
5621
  pStmt->force = force;
18,846✔
5622
  pStmt->unsafe = unsafe;
18,846✔
5623
  return (SNode*)pStmt;
18,846✔
5624
_err:
×
5625
  return NULL;
×
5626
}
5627

5628
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
130,497✔
5629
                            const SToken* pValue) {
5630
  CHECK_PARSER_STATUS(pCxt);
130,497✔
5631
  SAlterDnodeStmt* pStmt = NULL;
130,497✔
5632
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
130,497✔
5633
  CHECK_MAKE_NODE(pStmt);
130,497✔
5634
  if (NULL != pDnode) {
130,497✔
5635
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
72,905✔
5636
  } else {
5637
    pStmt->dnodeId = -1;
57,592✔
5638
  }
5639
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
130,497✔
5640
  if (NULL != pValue) {
130,497✔
5641
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
56,589✔
5642
  }
5643
  return (SNode*)pStmt;
130,497✔
UNCOV
5644
_err:
×
UNCOV
5645
  return NULL;
×
5646
}
5647

UNCOV
5648
SNode* createCreateAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId, const SToken* name, const SToken* desc,
×
5649
                            const SToken* type, const SToken* osslAlgrName) {
UNCOV
5650
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
5651
  SCreateEncryptAlgrStmt* pStmt = NULL;
×
UNCOV
5652
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ENCRYPT_ALGORITHMS_STMT, (SNode**)&pStmt);
×
5653
  CHECK_MAKE_NODE(pStmt);
×
5654
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
UNCOV
5655
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
UNCOV
5656
    goto _err;
×
5657
  }
UNCOV
5658
  if (name->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
UNCOV
5659
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_NAME_TOO_LONG);
×
5660
    goto _err;
×
5661
  }
UNCOV
5662
  if (desc->n >= TSDB_ENCRYPT_ALGR_DESC_LEN) {
×
UNCOV
5663
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_DESC_TOO_LONG);
×
UNCOV
5664
    goto _err;
×
5665
  }
UNCOV
5666
  if (type->n >= TSDB_ENCRYPT_ALGR_TYPE_LEN) {
×
UNCOV
5667
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_TYPE_TOO_LONG);
×
UNCOV
5668
    goto _err;
×
5669
  }
UNCOV
5670
  if (osslAlgrName->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
UNCOV
5671
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_OSSL_NAME_TOO_LONG);
×
UNCOV
5672
    goto _err;
×
5673
  }
UNCOV
5674
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
UNCOV
5675
  (void)trimString(name->z, name->n, pStmt->name, sizeof(pStmt->name));
×
UNCOV
5676
  (void)trimString(desc->z, desc->n, pStmt->desc, sizeof(pStmt->desc));
×
UNCOV
5677
  (void)trimString(type->z, type->n, pStmt->algrType, sizeof(pStmt->algrType));
×
UNCOV
5678
  (void)trimString(osslAlgrName->z, osslAlgrName->n, pStmt->osslAlgrName, sizeof(pStmt->osslAlgrName));
×
UNCOV
5679
  return (SNode*)pStmt;
×
5680
_err:
×
5681
  return NULL;
×
5682
}
5683

5684
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
×
UNCOV
5685
  CHECK_PARSER_STATUS(pCxt);
×
5686
  SCreateAnodeStmt* pStmt = NULL;
×
5687
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
×
5688
  CHECK_MAKE_NODE(pStmt);
×
5689
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
×
5690
  return (SNode*)pStmt;
×
5691
_err:
×
5692
  return NULL;
×
5693
}
5694

5695
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
×
5696
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
5697
  SUpdateAnodeStmt* pStmt = NULL;
×
5698
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
×
5699
  CHECK_MAKE_NODE(pStmt);
×
5700
  if (NULL != pAnode) {
×
UNCOV
5701
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5702
  } else {
5703
    pStmt->anodeId = -1;
×
5704
  }
UNCOV
5705
  return (SNode*)pStmt;
×
5706
_err:
×
5707
  return NULL;
×
5708
}
5709

5710
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
5711
  CHECK_PARSER_STATUS(pCxt);
×
5712
  SUpdateAnodeStmt* pStmt = NULL;
×
5713
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
5714
  CHECK_MAKE_NODE(pStmt);
×
5715
  if (NULL != pAnode) {
×
5716
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5717
  } else {
UNCOV
5718
    pStmt->anodeId = -1;
×
5719
  }
5720
  return (SNode*)pStmt;
×
5721
_err:
×
5722
  return NULL;
×
5723
}
5724

5725
SNode* createCreateBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId, SNode* pOptions) {
27,042✔
5726
  CHECK_PARSER_STATUS(pCxt);
27,042✔
5727
  SCreateBnodeStmt* pStmt = NULL;
27,042✔
5728
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_BNODE_STMT, (SNode**)&pStmt);
27,042✔
5729
  CHECK_MAKE_NODE(pStmt);
27,042✔
5730
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
27,042✔
5731

5732
  pStmt->pOptions = (SBnodeOptions*)pOptions;
27,042✔
5733

5734
  return (SNode*)pStmt;
27,042✔
5735
_err:
×
5736
  return NULL;
×
5737
}
5738

5739
SNode* createDropBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId) {
32,040✔
5740
  CHECK_PARSER_STATUS(pCxt);
32,040✔
5741
  SUpdateBnodeStmt* pStmt = NULL;
32,040✔
5742
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_BNODE_STMT, (SNode**)&pStmt);
32,040✔
5743
  CHECK_MAKE_NODE(pStmt);
32,040✔
5744
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
32,040✔
5745

5746
  return (SNode*)pStmt;
32,040✔
5747
_err:
×
5748
  return NULL;
×
5749
}
5750

5751
SNode* createDefaultBnodeOptions(SAstCreateContext* pCxt) {
27,042✔
5752
  CHECK_PARSER_STATUS(pCxt);
27,042✔
5753
  SBnodeOptions* pOptions = NULL;
27,042✔
5754
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BNODE_OPTIONS, (SNode**)&pOptions);
27,042✔
5755
  CHECK_MAKE_NODE(pOptions);
27,042✔
5756

5757
  tstrncpy(pOptions->protoStr, TSDB_BNODE_OPT_PROTO_DFT_STR, TSDB_BNODE_OPT_PROTO_STR_LEN);
27,042✔
5758
  pOptions->proto = TSDB_BNODE_OPT_PROTO_DEFAULT;
27,042✔
5759

5760
  return (SNode*)pOptions;
27,042✔
UNCOV
5761
_err:
×
UNCOV
5762
  return NULL;
×
5763
}
5764

UNCOV
5765
static SNode* setBnodeOptionImpl(SAstCreateContext* pCxt, SNode* pBodeOptions, EBnodeOptionType type, void* pVal,
×
5766
                                 bool alter) {
UNCOV
5767
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
5768
  SBnodeOptions* pOptions = (SBnodeOptions*)pBodeOptions;
×
UNCOV
5769
  switch (type) {
×
UNCOV
5770
    case BNODE_OPTION_PROTOCOL:
×
5771
      COPY_STRING_FORM_STR_TOKEN(pOptions->protoStr, (SToken*)pVal);
×
5772
      break;
×
UNCOV
5773
    default:
×
UNCOV
5774
      break;
×
5775
  }
5776

UNCOV
5777
  return pBodeOptions;
×
UNCOV
5778
_err:
×
UNCOV
5779
  nodesDestroyNode(pBodeOptions);
×
UNCOV
5780
  return NULL;
×
5781
}
5782

5783
SNode* setBnodeOption(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pOption, void* pVal) {
×
5784
  if (0 == strncasecmp(pOption->z, "protocol", 8)) {
×
UNCOV
5785
    return setBnodeOptionImpl(pCxt, pOptions, BNODE_OPTION_PROTOCOL, pVal, false);
×
5786
  } else {
UNCOV
5787
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
5788
    return pOptions;
×
5789
  }
5790
}
5791

5792
SNode* createCreateXnodeWithTokenStmt(SAstCreateContext* pCxt, const SToken* pUrl, SToken* pToken) {
141✔
5793
  CHECK_PARSER_STATUS(pCxt);
141✔
5794
  SCreateXnodeStmt* pStmt = NULL;
141✔
5795
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
141✔
5796
  CHECK_MAKE_NODE(pStmt);
141✔
5797

5798
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
141✔
5799

5800
  if (pToken != NULL) {
141✔
5801
    if (pToken->n <= 2) {
141✔
UNCOV
5802
      pCxt->errCode =
×
5803
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode token should not be empty");
×
5804
      goto _err;
×
5805
    }
5806
    if (pToken->n > TSDB_TOKEN_LEN + 2) {
141✔
5807
      pCxt->errCode =
×
5808
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode token length is illegal");
×
5809
      goto _err;
×
5810
    }
5811
    tstrncpy(pStmt->token, pToken->z + 1, TMIN(pToken->n - 2 + 1, sizeof(pStmt->token)));
141✔
5812
  }
5813
  return (SNode*)pStmt;
141✔
5814
_err:
×
5815
  return NULL;
×
5816
}
5817

5818
SNode* createCreateXnodeWithUserPassStmt(SAstCreateContext* pCxt, const SToken* pUrl, SToken* pUser,
1,269✔
5819
                                         const SToken* pPass) {
5820
  CHECK_PARSER_STATUS(pCxt);
1,269✔
5821
  SCreateXnodeStmt* pStmt = NULL;
1,269✔
5822
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
1,269✔
5823
  CHECK_MAKE_NODE(pStmt);
1,269✔
5824

5825
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
1,269✔
5826

5827
  if (pUser != NULL) {
1,269✔
5828
    CHECK_NAME(checkUserName(pCxt, pUser));
1,269✔
5829
    COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUser);
1,269✔
5830
  }
5831
  if (pPass != NULL) {
1,269✔
5832
    if (pPass->n <= 2) {
1,269✔
UNCOV
5833
      pCxt->errCode =
×
UNCOV
5834
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode password should not be empty");
×
UNCOV
5835
      goto _err;
×
5836
    }
5837
    tstrncpy(pStmt->pass, pPass->z + 1,
1,269✔
5838
             (pPass->n - 2) < sizeof(pStmt->pass) ? (pPass->n - 2) + 1 : sizeof(pStmt->pass));
5839
  }
5840
  return (SNode*)pStmt;
1,269✔
UNCOV
5841
_err:
×
UNCOV
5842
  return NULL;
×
5843
}
5844
SNode* createCreateXnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
1,410✔
5845
  CHECK_PARSER_STATUS(pCxt);
1,410✔
5846
  SCreateXnodeStmt* pStmt = NULL;
1,410✔
5847
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
1,410✔
5848
  CHECK_MAKE_NODE(pStmt);
1,410✔
5849
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
1,410✔
5850
  return (SNode*)pStmt;
1,410✔
5851
_err:
×
UNCOV
5852
  return NULL;
×
5853
}
5854

5855
SNode* createDropXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode, bool force) {
2,679✔
5856
  if (NULL == pXnode) {
2,679✔
UNCOV
5857
    pCxt->errCode =
×
UNCOV
5858
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
UNCOV
5859
    goto _err;
×
5860
  }
5861
  CHECK_PARSER_STATUS(pCxt);
2,679✔
5862
  SDropXnodeStmt* pStmt = NULL;
2,679✔
5863
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_STMT, (SNode**)&pStmt);
2,679✔
5864
  CHECK_MAKE_NODE(pStmt);
2,679✔
5865

5866
  pStmt->force = force;
2,679✔
5867
  if (pXnode->type == TK_NK_STRING) {
2,679✔
5868
    if (pXnode->n <= 2) {
2,115✔
5869
      pCxt->errCode =
×
5870
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode url should not be all be NULL");
×
5871
        goto _err;
×
5872
    }
5873
    COPY_STRING_FORM_STR_TOKEN(pStmt->url, pXnode);
2,115✔
5874
  } else if(pXnode->type == TK_NK_INTEGER) {
564✔
5875
    pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
564✔
5876
  } else {
5877
    pCxt->errCode =
×
5878
      generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id or url should not be all be NULL");
×
5879
  }
5880

5881
  return (SNode*)pStmt;
2,679✔
UNCOV
5882
_err:
×
UNCOV
5883
  return NULL;
×
5884
}
5885

5886
SNode* createDrainXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode) {
423✔
5887
  CHECK_PARSER_STATUS(pCxt);
423✔
5888
  if (NULL == pXnode) {
423✔
UNCOV
5889
    pCxt->errCode =
×
UNCOV
5890
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
UNCOV
5891
    goto _err;
×
5892
  }
5893
  if (pXnode->type != TK_NK_INTEGER) {
423✔
5894
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be an integer");
×
5895
    goto _err;
×
5896
  }
5897

5898
  SDrainXnodeStmt* pStmt = NULL;
423✔
5899
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DRAIN_XNODE_STMT, (SNode**)&pStmt);
423✔
5900
  CHECK_MAKE_NODE(pStmt);
423✔
5901
  pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
423✔
5902
  if (pStmt->xnodeId <= 0) {
423✔
UNCOV
5903
    pCxt->errCode =
×
UNCOV
5904
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be greater than 0");
×
5905
    goto _err;
×
5906
  }
5907

5908
  return (SNode*)pStmt;
423✔
UNCOV
5909
_err:
×
UNCOV
5910
  return NULL;
×
5911
}
5912

5913
SNode* createAlterXnodeStmt(SAstCreateContext* pCxt, const SToken* pToken, const SToken* pUser, const SToken* pPass) {
987✔
5914
  CHECK_PARSER_STATUS(pCxt);
987✔
5915
  SAlterXnodeStmt* pStmt = NULL;
987✔
5916
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_STMT, (SNode**)&pStmt);
987✔
5917
  CHECK_MAKE_NODE(pStmt);
987✔
5918
  if (pToken != NULL) {
987✔
5919
    pStmt->token = xCreateCowStr(pToken->n - 2, pToken->z + 1, true);
282✔
5920
  }
5921
  if (pUser != NULL && pPass != NULL) {
987✔
5922
    if (pUser->n <= 2) {
705✔
UNCOV
5923
      pCxt->errCode =
×
UNCOV
5924
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode user should not be NULL or empty");
×
5925
      goto _err;
×
5926
    }
5927
    if (pPass->n <= 2) {
705✔
UNCOV
5928
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5929
                                              "xnode password should not be NULL or empty");
5930
      goto _err;
×
5931
    }
5932
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
705✔
5933
    COPY_COW_STR_FROM_ID_TOKEN(pStmt->user, pUser);
705✔
5934
    pStmt->pass = xCreateCowStr(pPass->n - 2, pPass->z + 1, true);
705✔
5935
  }
5936

5937
  return (SNode*)pStmt;
987✔
UNCOV
5938
_err:
×
5939
  nodesDestroyNode((SNode*)pStmt);
×
5940
  return NULL;
×
5941
}
5942

5943
EXnodeResourceType setXnodeResourceType(SAstCreateContext* pCxt, const SToken* pResourceId) {
69,237✔
5944
  CHECK_PARSER_STATUS(pCxt);
69,237✔
5945
  const size_t TASK_LEN = 4;
69,237✔
5946
  const size_t TASKS_LEN = 5;
69,237✔
5947
  const size_t AGENT_LEN = 5;
69,237✔
5948
  const size_t AGENTS_LEN = 6;
69,237✔
5949
  const size_t JOB_LEN = 3;
69,237✔
5950
  const size_t JOBS_LEN = 4;
69,237✔
5951

5952
  if (pResourceId->z[0] == '`') {
69,237✔
UNCOV
5953
    if (strncmp(pResourceId->z + 1, "task", TASK_LEN) == 0 || strncmp(pResourceId->z + 1, "tasks", TASKS_LEN) == 0) {
×
UNCOV
5954
      return XNODE_TASK;
×
5955
    }
UNCOV
5956
    if (strncmp(pResourceId->z + 1, "agent", AGENT_LEN) == 0 ||
×
UNCOV
5957
        strncmp(pResourceId->z + 1, "agents", AGENTS_LEN) == 0) {
×
UNCOV
5958
      return XNODE_AGENT;
×
5959
    }
5960
    if (strncmp(pResourceId->z + 1, "job", JOB_LEN) == 0 || strncmp(pResourceId->z + 1, "jobs", JOBS_LEN) == 0) {
×
5961
      return XNODE_JOB;
×
5962
    }
5963

5964
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5965
                                            "Invalid xnode resource type (task/agent) at: %s", pResourceId->z);
×
5966
    goto _err;
×
5967
  }
5968
  if (strncmp(pResourceId->z, "task", TASK_LEN) == 0 || strncmp(pResourceId->z, "tasks", TASKS_LEN) == 0) {
69,237✔
5969
    return XNODE_TASK;
16,922✔
5970
  }
5971
  if (strncmp(pResourceId->z, "agent", AGENT_LEN) == 0 || strncmp(pResourceId->z, "agents", AGENTS_LEN) == 0) {
52,315✔
5972
    return XNODE_AGENT;
10,295✔
5973
  }
5974
  if (strncmp(pResourceId->z, "job", JOB_LEN) == 0 || strncmp(pResourceId->z, "jobs", JOBS_LEN) == 0) {
42,020✔
5975
    return XNODE_JOB;
42,020✔
5976
  }
UNCOV
5977
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5978
                                          "Invalid xnode resource type (task/agent/job) at: %s", pResourceId->z);
×
UNCOV
5979
  goto _err;
×
5980

UNCOV
5981
_err:
×
UNCOV
5982
  return XNODE_UNKNOWN;
×
5983
}
5984
SNode* createXnodeSourceAsDsn(SAstCreateContext* pCxt, const SToken* pToken) {
4,089✔
5985
  SXTaskSource* pSource = NULL;
4,089✔
5986
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
4,089✔
5987
  CHECK_MAKE_NODE(pSource);
4,089✔
5988
  if (pToken == NULL || pToken->n <= 0) {
4,089✔
5989
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5990
                                            "xnode source dsn should not be NULL or empty");
UNCOV
5991
    goto _err;
×
5992
  }
5993
  if (pToken->n > TSDB_XNODE_TASK_SOURCE_LEN) {
4,089✔
5994
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
5995
                                            "Invalid xnode source dsn length: %d, max length: %d", pToken->n,
×
5996
                                            TSDB_XNODE_TASK_SOURCE_LEN);
5997
    goto _err;
×
5998
  }
5999
  pSource->source.type = XNODE_TASK_SOURCE_DSN;
4,089✔
6000
  COPY_COW_STR_FROM_STR_TOKEN(pSource->source.cstr, pToken);
4,089✔
6001
  return (SNode*)pSource;
4,089✔
6002
_err:
×
UNCOV
6003
  return NULL;
×
6004
}
6005
SNode* createXnodeSourceAsDatabase(SAstCreateContext* pCxt, const SToken* pToken) {
141✔
6006
  SXTaskSource* pSource = NULL;
141✔
6007
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
141✔
6008
  CHECK_MAKE_NODE(pSource);
141✔
6009
  if (pToken == NULL || pToken->n <= 0) {
141✔
UNCOV
6010
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6011
                                            "xnode source database should not be NULL or empty");
UNCOV
6012
    goto _err;
×
6013
  }
6014
  if (pToken->n > TSDB_XNODE_TASK_SOURCE_LEN) {
141✔
6015
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
6016
                                            "Invalid xnode source database length: %d, max length: %d", pToken->n,
×
6017
                                            TSDB_XNODE_TASK_SOURCE_LEN);
6018
    goto _err;
×
6019
  }
6020
  pSource->source.type = XNODE_TASK_SOURCE_DATABASE;
141✔
6021
  COPY_COW_STR_FROM_ID_TOKEN(pSource->source.cstr, pToken);
141✔
6022
  return (SNode*)pSource;
141✔
UNCOV
6023
_err:
×
UNCOV
6024
  return NULL;
×
6025
}
6026
SNode* createXnodeSourceAsTopic(SAstCreateContext* pCxt, const SToken* pToken) {
705✔
6027
  SXTaskSource* pSource = NULL;
705✔
6028
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
705✔
6029
  CHECK_MAKE_NODE(pSource);
705✔
6030
  if (pToken == NULL || pToken->n <= 0) {
705✔
6031
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6032
                                            "xnode source dsn should not be NULL or empty");
6033
    goto _err;
×
6034
  }
6035
  if (pToken->n > TSDB_TOPIC_NAME_LEN) {
705✔
UNCOV
6036
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
6037
                                            "Invalid xnode source topic length: %d, max length: %d", pToken->n,
×
6038
                                            TSDB_TOPIC_NAME_LEN);
6039
    goto _err;
×
6040
  }
6041
  pSource->source.type = XNODE_TASK_SOURCE_TOPIC;
705✔
6042
  COPY_COW_STR_FROM_STR_TOKEN(pSource->source.cstr, pToken);
705✔
6043
  return (SNode*)pSource;
705✔
UNCOV
6044
_err:
×
UNCOV
6045
  return NULL;
×
6046
}
6047
SNode* createXnodeSinkAsDsn(SAstCreateContext* pCxt, const SToken* pToken) {
2,256✔
6048
  SXTaskSink* pSink = NULL;
2,256✔
6049
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SINK_OPT, (SNode**)&pSink);
2,256✔
6050
  CHECK_MAKE_NODE(pSink);
2,256✔
6051
  if (pToken == NULL || pToken->n <= 0) {
2,256✔
6052
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6053
                                            "xnode sink dsn should not be NULL or empty");
6054
    goto _err;
×
6055
  }
6056
  if (pToken->n > TSDB_XNODE_TASK_SINK_LEN) {
2,256✔
UNCOV
6057
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
6058
                                            "Invalid xnode sink dsn length: %d, max length: %d", pToken->n,
×
6059
                                            TSDB_XNODE_TASK_SINK_LEN);
6060
    goto _err;
×
6061
  }
6062
  pSink->sink.type = XNODE_TASK_SINK_DSN;
2,256✔
6063
  COPY_COW_STR_FROM_STR_TOKEN(pSink->sink.cstr, pToken);
2,256✔
6064
  return (SNode*)pSink;
2,256✔
UNCOV
6065
_err:
×
UNCOV
6066
  return NULL;
×
6067
}
6068
SNode* createXnodeSinkAsDatabase(SAstCreateContext* pCxt, const SToken* pToken) {
2,820✔
6069
  SXTaskSink* pSink = NULL;
2,820✔
6070
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SINK_OPT, (SNode**)&pSink);
2,820✔
6071
  CHECK_MAKE_NODE(pSink);
2,820✔
6072
  if (pToken == NULL || pToken->n <= 0) {
2,820✔
6073
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6074
                                            "Xnode sink database should not be NULL or empty");
6075
    goto _err;
×
6076
  }
6077
  if (pToken->n > TSDB_XNODE_TASK_SINK_LEN) {
2,820✔
UNCOV
6078
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
6079
                                            "Invalid xnode sink database length: %d, max length: %d", pToken->n,
×
6080
                                            TSDB_XNODE_TASK_SINK_LEN);
6081
    goto _err;
×
6082
  }
6083
  pSink->sink.type = XNODE_TASK_SINK_DATABASE;
2,820✔
6084
  if (pToken->type == TK_NK_STRING) {
2,820✔
UNCOV
6085
    COPY_COW_STR_FROM_STR_TOKEN(pSink->sink.cstr, pToken);
×
6086
  } else if (pToken->type == TK_NK_ID) {
2,820✔
6087
    COPY_COW_STR_FROM_ID_TOKEN(pSink->sink.cstr, pToken);
2,820✔
6088
  } else {
UNCOV
6089
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6090
                                            "Invalid xnode sink database type: %d", pToken->type);
×
UNCOV
6091
    goto _err;
×
6092
  }
6093

6094
  return (SNode*)pSink;
2,820✔
UNCOV
6095
_err:
×
6096
  return NULL;
×
6097
}
6098

6099
SNode* createXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pSource,
4,794✔
6100
                                          SNode* pSink, SNode* pNode) {
6101
  SNode* pStmt = NULL;
4,794✔
6102
  if (pResourceName == NULL) {
4,794✔
UNCOV
6103
    pCxt->errCode =
×
UNCOV
6104
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name should not be NULL");
×
UNCOV
6105
    goto _err;
×
6106
  }
6107
  if (pSource == NULL || pSink == NULL) {
4,794✔
UNCOV
6108
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6109
                                            "Xnode task source and sink should not be NULL");
UNCOV
6110
    goto _err;
×
6111
  }
6112
  if (nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT || nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT) {
4,794✔
UNCOV
6113
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6114
                                            "Xnode task source and sink should be valid nodes");
6115
    goto _err;
×
6116
  }
6117
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_TASK_STMT, (SNode**)&pStmt);
4,794✔
6118
  CHECK_MAKE_NODE(pStmt);
4,794✔
6119
  SCreateXnodeTaskStmt* pTaskStmt = (SCreateXnodeTaskStmt*)pStmt;
4,794✔
6120
  if (pResourceName->type == TK_NK_STRING) {
4,794✔
6121
    if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
4,794✔
6122
      pCxt->errCode =
141✔
6123
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
141✔
6124
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_TASK_NAME_LEN);
6125
      goto _err;
141✔
6126
    }
6127
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
4,653✔
UNCOV
6128
  } else if (pResourceName->type == TK_NK_ID) {
×
UNCOV
6129
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
×
6130
  } else {
6131
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode name type: %d",
×
6132
                                            pResourceName->type);
×
UNCOV
6133
    goto _err;
×
6134
  }
6135

6136
  if (pSource != NULL) {
4,653✔
6137
    SXTaskSource* source = (SXTaskSource*)(pSource);
4,653✔
6138
    pTaskStmt->source = source;
4,653✔
6139
  }
6140
  if (pSink != NULL) {
4,653✔
6141
    SXTaskSink* sink = (SXTaskSink*)(pSink);
4,653✔
6142
    pTaskStmt->sink = sink;
4,653✔
6143
  }
6144
  if (pNode != NULL) {
4,653✔
6145
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
4,653✔
6146
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
4,653✔
6147
      pTaskStmt->options = options;
4,653✔
6148
    }
6149
  }
6150
  return (SNode*)pTaskStmt;
4,653✔
6151
_err:
141✔
6152
  if (pStmt != NULL) {
141✔
6153
    nodesDestroyNode(pStmt);
141✔
6154
  }
6155
  if (pNode != NULL) {
141✔
6156
    nodesDestroyNode(pNode);
141✔
6157
  }
6158
  return NULL;
141✔
6159
}
6160

6161
SNode* createXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pOptions) {
2,679✔
6162
  SNode* pStmt = NULL;
2,679✔
6163
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_AGENT_STMT, (SNode**)&pStmt);
2,679✔
6164
  CHECK_MAKE_NODE(pStmt);
2,679✔
6165
  SCreateXnodeAgentStmt* pAgentStmt = (SCreateXnodeAgentStmt*)pStmt;
2,679✔
6166

6167
  if (pOptions != NULL) {
2,679✔
6168
    if (nodeType(pOptions) == QUERY_NODE_XNODE_TASK_OPTIONS) {
2,679✔
6169
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pOptions);
2,679✔
6170
      pAgentStmt->options = options;
2,679✔
6171
    }
6172
  }
6173

6174
  if (pResourceName->type == TK_NK_STRING && pResourceName->n > 2) {
2,679✔
6175
    if (pResourceName->n > TSDB_XNODE_AGENT_NAME_LEN + 2) {
2,679✔
UNCOV
6176
      pCxt->errCode =
×
UNCOV
6177
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6178
                                  "Xnode agent name should be less than %d characters", TSDB_XNODE_AGENT_NAME_LEN);
UNCOV
6179
      goto _err;
×
6180
    }
6181
    COPY_STRING_FORM_STR_TOKEN(pAgentStmt->name, pResourceName);
2,679✔
6182
  } else {
UNCOV
6183
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
UNCOV
6184
                                            "Invalid xnode agent name type: %d", pResourceName->type);
×
UNCOV
6185
    goto _err;
×
6186
  }
6187

6188
  return (SNode*)pAgentStmt;
2,679✔
UNCOV
6189
_err:
×
UNCOV
6190
  if (pStmt != NULL) {
×
UNCOV
6191
    nodesDestroyNode(pStmt);
×
6192
  }
UNCOV
6193
  return NULL;
×
6194
}
6195

6196
SNode* createXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResourceName,
7,473✔
6197
                                  SNode* pSource, SNode* pSink, SNode* pOptions) {
6198
  CHECK_PARSER_STATUS(pCxt);
7,473✔
6199

6200
  switch (resourceType) {
7,473✔
6201
    case XNODE_TASK: {
4,794✔
6202
      SNode* rs = createXnodeTaskWithOptionsDirectly(pCxt, pResourceName, pSource, pSink, pOptions);
4,794✔
6203
      if (rs == NULL) {
4,794✔
6204
        goto _err;
141✔
6205
      }
6206
      return rs;
4,653✔
6207
    }
6208
    case XNODE_AGENT: {
2,679✔
6209
      SNode* rs = createXnodeAgentWithOptionsDirectly(pCxt, pResourceName, pOptions);
2,679✔
6210
      if (rs == NULL) {
2,679✔
UNCOV
6211
        goto _err;
×
6212
      }
6213
      return rs;
2,679✔
6214
    }
6215
    default:
×
UNCOV
6216
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6217
                                              "Invalid xnode resource type: %d", resourceType);
UNCOV
6218
      goto _err;
×
6219
  }
6220
_err:
141✔
6221
  nodesDestroyNode(pSource);
141✔
6222
  nodesDestroyNode(pSink);
141✔
6223
  nodesDestroyNode(pOptions);
141✔
6224
  return NULL;
141✔
6225
}
6226

6227
SNode* createStartXnodeTaskStmt(SAstCreateContext* pCxt, const EXnodeResourceType resourceType, SToken* pIdOrName) {
141✔
6228
  SNode* pStmt = NULL;
141✔
6229
  CHECK_PARSER_STATUS(pCxt);
141✔
6230
  if (resourceType != XNODE_TASK) {
141✔
UNCOV
6231
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6232
                                            "Invalid xnode resource type: %d", resourceType);
UNCOV
6233
    goto _err;
×
6234
  }
6235
  if (pIdOrName == NULL || (pIdOrName != NULL && pIdOrName->type != TK_NK_INTEGER && pIdOrName->type != TK_NK_STRING)) {
141✔
UNCOV
6236
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6237
                                            "Xnode task id or name should be an integer or string");
UNCOV
6238
    goto _err;
×
6239
  }
6240

6241
  pCxt->errCode = nodesMakeNode(QUERY_NODE_START_XNODE_TASK_STMT, (SNode**)&pStmt);
141✔
6242
  CHECK_MAKE_NODE(pStmt);
141✔
6243
  SStartXnodeTaskStmt* pTaskStmt = (SStartXnodeTaskStmt*)pStmt;
141✔
6244
  if (pIdOrName->type == TK_NK_INTEGER) {
141✔
UNCOV
6245
    pTaskStmt->tid = taosStr2Int32(pIdOrName->z, NULL, 10);
×
UNCOV
6246
    if (pTaskStmt->tid <= 0) {
×
6247
      pCxt->errCode =
×
UNCOV
6248
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Task id should be greater than 0");
×
UNCOV
6249
      goto _err;
×
6250
    }
6251
  } else {
6252
    if (pIdOrName->n > TSDB_XNODE_RESOURCE_NAME_LEN + 2) {
141✔
UNCOV
6253
      pCxt->errCode =
×
6254
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6255
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_RESOURCE_NAME_LEN);
UNCOV
6256
      goto _err;
×
6257
    }
6258
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
141✔
6259
    COPY_STRING_FORM_STR_TOKEN(buf, pIdOrName);
141✔
6260
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
141✔
6261
  }
6262

6263
  return (SNode*)pTaskStmt;
141✔
UNCOV
6264
_err:
×
UNCOV
6265
  if (pStmt != NULL) {
×
UNCOV
6266
    nodesDestroyNode(pStmt);
×
6267
  }
UNCOV
6268
  return NULL;
×
6269
}
6270

6271
SNode* createStopXnodeTaskStmt(SAstCreateContext* pCxt, const EXnodeResourceType resourceType, SToken* pIdOrName) {
141✔
6272
  SNode* pStmt = NULL;
141✔
6273
  CHECK_PARSER_STATUS(pCxt);
141✔
6274
  if (resourceType != XNODE_TASK) {
141✔
UNCOV
6275
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6276
                                            "Only support stop task, invalid resource type: %d", resourceType);
UNCOV
6277
    goto _err;
×
6278
  }
6279
  if (pIdOrName != NULL && pIdOrName->type != TK_NK_INTEGER && pIdOrName->type != TK_NK_STRING) {
141✔
UNCOV
6280
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6281
                                            "Xnode task id or name should be an integer or string");
6282
    goto _err;
×
6283
  }
6284
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STOP_XNODE_TASK_STMT, (SNode**)&pStmt);
141✔
6285
  CHECK_MAKE_NODE(pStmt);
141✔
6286
  SStopXnodeTaskStmt* pTaskStmt = (SStopXnodeTaskStmt*)pStmt;
141✔
6287
  if (pIdOrName->type == TK_NK_INTEGER) {
141✔
UNCOV
6288
    pTaskStmt->tid = taosStr2Int32(pIdOrName->z, NULL, 10);
×
6289
    if (pTaskStmt->tid <= 0) {
×
6290
      pCxt->errCode =
×
UNCOV
6291
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Task id should be greater than 0");
×
6292
      goto _err;
×
6293
    }
6294
  } else {
6295
    if (pIdOrName->n > TSDB_XNODE_RESOURCE_NAME_LEN + 2) {
141✔
UNCOV
6296
      pCxt->errCode =
×
UNCOV
6297
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6298
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_RESOURCE_NAME_LEN);
UNCOV
6299
      goto _err;
×
6300
    }
6301
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
141✔
6302
    COPY_STRING_FORM_STR_TOKEN(buf, pIdOrName);
141✔
6303
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
141✔
6304
  }
6305

6306
  return (SNode*)pTaskStmt;
141✔
UNCOV
6307
_err:
×
UNCOV
6308
  if (pStmt != NULL) {
×
UNCOV
6309
    nodesDestroyNode(pStmt);
×
6310
  }
6311
  return NULL;
×
6312
}
6313

6314
SNode* rebalanceXnodeJobWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceId, SNode* pNode) {
282✔
6315
  SNode* pStmt = NULL;
282✔
6316
  if (pResourceId == NULL) {
282✔
UNCOV
6317
    pCxt->errCode =
×
6318
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should not be NULL");
×
UNCOV
6319
    goto _err;
×
6320
  }
6321
  if (pNode == NULL) {
282✔
UNCOV
6322
    pCxt->errCode =
×
UNCOV
6323
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job options should not be NULL");
×
6324
    goto _err;
×
6325
  }
6326
  if (pResourceId->type != TK_NK_INTEGER) {
282✔
6327
    pCxt->errCode =
×
6328
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should be an integer");
×
UNCOV
6329
    goto _err;
×
6330
  }
6331
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_STMT, (SNode**)&pStmt);
282✔
6332
  CHECK_MAKE_NODE(pStmt);
282✔
6333

6334
  SRebalanceXnodeJobStmt* pJobStmt = (SRebalanceXnodeJobStmt*)pStmt;
282✔
6335
  char                    buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
282✔
6336
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceId);
282✔
6337
  pJobStmt->jid = taosStr2Int32(buf, NULL, 10);
282✔
6338

6339
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
282✔
6340
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
282✔
6341
    // printXnodeTaskOptions(&options->opts);
6342
    pJobStmt->options = options;
282✔
6343
  }
6344
  return (SNode*)pJobStmt;
282✔
6345
_err:
×
UNCOV
6346
  return NULL;
×
6347
}
6348

6349
SNode* createRebalanceXnodeJobStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* resourceId,
282✔
6350
                                   SNode* pNodeOptions) {
6351
  CHECK_PARSER_STATUS(pCxt);
282✔
6352

6353
  switch (resourceType) {
282✔
6354
    case XNODE_JOB: {
282✔
6355
      return rebalanceXnodeJobWithOptionsDirectly(pCxt, resourceId, pNodeOptions);
282✔
6356
    }
UNCOV
6357
    default:
×
6358
      pCxt->errCode =
×
6359
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6360
                                  "Invalid xnode resource type: %d, rebalance only support job", resourceType);
UNCOV
6361
      goto _err;
×
6362
  }
6363
_err:
×
6364
  return NULL;
×
6365
}
6366

6367
SNode* rebalanceXnodeJobWhereDirectly(SAstCreateContext* pCxt, SNode* pWhere) {
846✔
6368
  int32_t code = 0;
846✔
6369
  SNode*  pStmt = NULL;
846✔
6370

6371
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_WHERE_STMT, (SNode**)&pStmt);
846✔
6372
  CHECK_MAKE_NODE(pStmt);
846✔
6373

6374
  SRebalanceXnodeJobWhereStmt* pJobStmt = (SRebalanceXnodeJobWhereStmt*)pStmt;
846✔
6375
  pJobStmt->pWhere = pWhere;
846✔
6376

6377
  return (SNode*)pJobStmt;
846✔
UNCOV
6378
_err:
×
UNCOV
6379
  return NULL;
×
6380
}
6381

6382
SNode* createRebalanceXnodeJobWhereStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
846✔
6383
  CHECK_PARSER_STATUS(pCxt);
846✔
6384

6385
  switch (resourceType) {
846✔
6386
    case XNODE_JOB: {
846✔
6387
      return rebalanceXnodeJobWhereDirectly(pCxt, pWhere);
846✔
6388
    }
UNCOV
6389
    default:
×
UNCOV
6390
      pCxt->errCode =
×
UNCOV
6391
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6392
                                  "Invalid xnode resource type: %d, rebalance only support job", resourceType);
6393
      goto _err;
×
6394
  }
6395
_err:
×
UNCOV
6396
  return NULL;
×
6397
}
6398

6399
SNode* updateXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pSource,
564✔
6400
                                          SNode* pSink, SNode* pNode) {
6401
  SNode* pStmt = NULL;
564✔
6402

6403
  if ((pSource != NULL && nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT) ||
564✔
6404
      (pSink != NULL && nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT)) {
282✔
UNCOV
6405
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6406
                                            "Xnode task source and sink should be valid nodes");
UNCOV
6407
    goto _err;
×
6408
  }
6409
  if (pSource == NULL && pSink == NULL && pNode == NULL) {
564✔
UNCOV
6410
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6411
                                            "Xnode task source, sink, and with options can't all be NULL");
UNCOV
6412
    goto _err;
×
6413
  }
6414

6415
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_XNODE_TASK_STMT, (SNode**)&pStmt);
564✔
6416
  CHECK_MAKE_NODE(pStmt);
564✔
6417
  SUpdateXnodeTaskStmt* pTaskStmt = (SUpdateXnodeTaskStmt*)pStmt;
564✔
6418
  if (pResIdOrName->type == TK_NK_INTEGER) {
564✔
6419
    pTaskStmt->tid = taosStr2Int32(pResIdOrName->z, NULL, 10);
141✔
6420
  } else {
6421
    if (pResIdOrName->n <= 2) {
423✔
UNCOV
6422
      pCxt->errCode =
×
UNCOV
6423
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name can't be empty string");
×
UNCOV
6424
      goto _err;
×
6425
    }
6426
    char buf[TSDB_XNODE_TASK_NAME_LEN] = {0};
423✔
6427
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
423✔
6428
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
423✔
6429
  }
6430

6431
  if (pSource != NULL) {
564✔
6432
    SXTaskSource* source = (SXTaskSource*)(pSource);
141✔
6433
    pTaskStmt->source = source;
141✔
6434
  }
6435
  if (pSink != NULL) {
564✔
6436
    SXTaskSink* sink = (SXTaskSink*)(pSink);
282✔
6437
    pTaskStmt->sink = sink;
282✔
6438
  }
6439
  if (pNode != NULL) {
564✔
6440
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
564✔
6441
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
564✔
6442
      pTaskStmt->options = options;
564✔
6443
    }
6444
  }
6445
  return (SNode*)pTaskStmt;
564✔
6446
_err:
×
UNCOV
6447
  if (pStmt != NULL) {
×
6448
    nodesDestroyNode(pStmt);
×
6449
  }
UNCOV
6450
  return NULL;
×
6451
}
6452

6453
SNode* alterXnodeJobWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pNode) {
141✔
6454
  SNode* pStmt = NULL;
141✔
6455
  if (pResourceName == NULL) {
141✔
UNCOV
6456
    pCxt->errCode =
×
UNCOV
6457
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should not be NULL");
×
6458
    goto _err;
×
6459
  }
6460
  if (pNode == NULL) {
141✔
UNCOV
6461
    pCxt->errCode =
×
UNCOV
6462
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job options should not be NULL");
×
UNCOV
6463
    goto _err;
×
6464
  }
6465
  if (pResourceName->type != TK_NK_INTEGER) {
141✔
UNCOV
6466
    pCxt->errCode =
×
UNCOV
6467
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should be integer");
×
UNCOV
6468
    goto _err;
×
6469
  }
6470
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_JOB_STMT, (SNode**)&pStmt);
141✔
6471
  CHECK_MAKE_NODE(pStmt);
141✔
6472

6473
  SAlterXnodeJobStmt* pJobStmt = (SAlterXnodeJobStmt*)pStmt;
141✔
6474
  char                buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
141✔
6475
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
141✔
6476
  pJobStmt->jid = taosStr2Int32(buf, NULL, 10);
141✔
6477

6478
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
141✔
6479
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
141✔
6480
    pJobStmt->options = options;
141✔
6481
  }
6482
  return (SNode*)pJobStmt;
141✔
6483
_err:
×
6484
  if (pStmt != NULL) {
×
UNCOV
6485
    nodesDestroyNode(pStmt);
×
6486
  }
UNCOV
6487
  return NULL;
×
6488
}
6489

6490
SNode* alterXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pNode) {
423✔
6491
  SNode* pStmt = NULL;
423✔
6492
  if (NULL == pNode) {
423✔
6493
    pCxt->errCode =
×
6494
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode alter agent options can't be null");
×
UNCOV
6495
    goto _err;
×
6496
  }
6497

6498
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_AGENT_STMT, (SNode**)&pStmt);
423✔
6499
  CHECK_MAKE_NODE(pStmt);
423✔
6500
  SAlterXnodeAgentStmt* pAgentStmt = (SAlterXnodeAgentStmt*)pStmt;
423✔
6501
  if (pResIdOrName->type == TK_NK_INTEGER) {
423✔
6502
    pAgentStmt->id = taosStr2Int32(pResIdOrName->z, NULL, 10);
×
6503
  } else {
6504
    if (pResIdOrName->n <= 2) {
423✔
UNCOV
6505
      pCxt->errCode =
×
UNCOV
6506
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode alter agent name can't be empty string");
×
UNCOV
6507
      goto _err;
×
6508
    }
6509
    char buf[TSDB_XNODE_AGENT_NAME_LEN] = {0};
423✔
6510
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
423✔
6511
    pAgentStmt->name = xCreateCowStr(strlen(buf), buf, true);
423✔
6512
  }
6513

6514
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
423✔
6515
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
423✔
6516
    pAgentStmt->options = options;
423✔
6517
  }
6518

6519
  return (SNode*)pAgentStmt;
423✔
6520
_err:
×
6521
  if (pStmt != NULL) {
×
UNCOV
6522
    nodesDestroyNode(pStmt);
×
6523
  }
UNCOV
6524
  return NULL;
×
6525
}
6526

6527
SNode* alterXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResIdOrName,
1,128✔
6528
                                 SNode* pSource, SNode* pSink, SNode* pNode) {
6529
  CHECK_PARSER_STATUS(pCxt);
1,128✔
6530

6531
  switch (resourceType) {
1,128✔
6532
    case XNODE_TASK: {
564✔
6533
      return updateXnodeTaskWithOptionsDirectly(pCxt, pResIdOrName, pSource, pSink, pNode);
564✔
6534
    }
6535
    case XNODE_AGENT: {
423✔
6536
      return alterXnodeAgentWithOptionsDirectly(pCxt, pResIdOrName, pNode);
423✔
6537
    }
6538
    case XNODE_JOB: {
141✔
6539
      return alterXnodeJobWithOptionsDirectly(pCxt, pResIdOrName, pNode);
141✔
6540
    }
6541
    default:
×
6542
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6543
                                              "Invalid xnode resource type: %d", resourceType);
UNCOV
6544
      goto _err;
×
6545
  }
UNCOV
6546
_err:
×
UNCOV
6547
  return NULL;
×
6548
}
6549

6550
SNode* dropXnodeResource(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SToken* pResourceName) {
6,768✔
6551
  SNode* pStmt = NULL;
6,768✔
6552
  char   buf[TSDB_XNODE_TASK_NAME_LEN + 1] = {0};
6,768✔
6553

6554
  CHECK_PARSER_STATUS(pCxt);
6,768✔
6555
  if (pResourceName == NULL || pResourceName->n <= 0) {
6,768✔
6556
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6557
                                            "Xnode resource name should not be NULL or empty");
6558
    goto _err;
×
6559
  }
6560
  if (pResourceName->n > TSDB_XNODE_RESOURCE_NAME_LEN) {
6,768✔
6561
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
141✔
6562
                                            "Invalid xnode resource name length: %d, max length: %d", pResourceName->n,
6563
                                            TSDB_XNODE_RESOURCE_NAME_LEN);
6564
    goto _err;
141✔
6565
  }
6566
  switch (resourceType) {
6,627✔
6567
    case XNODE_TASK:
3,807✔
6568
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
3,807✔
6569
      CHECK_MAKE_NODE(pStmt);
3,807✔
6570
      SDropXnodeTaskStmt* pTaskStmt = (SDropXnodeTaskStmt*)pStmt;
3,807✔
6571

6572
      if (pResourceName->type == TK_NK_STRING) {
3,807✔
6573
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
3,807✔
UNCOV
6574
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6575
                                                  "Invalid xnode task name length: %d, max length: %d",
6576
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
6577
          goto _err;
×
6578
        }
6579
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
3,807✔
6580
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
3,807✔
UNCOV
6581
      } else if (pResourceName->type == TK_NK_ID) {
×
6582
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
6583
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
×
UNCOV
6584
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
UNCOV
6585
        pTaskStmt->id = taosStr2Int32(pResourceName->z, NULL, 10);
×
6586
      } else {
UNCOV
6587
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6588
                                                "Invalid xnode job id type: %d", pResourceName->type);
UNCOV
6589
        goto _err;
×
6590
      }
6591
      break;
3,807✔
6592
    case XNODE_AGENT:
2,820✔
6593
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
2,820✔
6594
      CHECK_MAKE_NODE(pStmt);
2,820✔
6595
      SDropXnodeAgentStmt* pDropAgent = (SDropXnodeAgentStmt*)pStmt;
2,820✔
6596

6597
      if (pResourceName->type == TK_NK_STRING) {
2,820✔
6598
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
2,820✔
UNCOV
6599
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6600
                                                  "Invalid xnode task name length: %d, max length: %d",
6601
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
UNCOV
6602
          goto _err;
×
6603
        }
6604
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
2,820✔
6605
        pDropAgent->name = taosStrndupi(buf, sizeof(buf));
2,820✔
UNCOV
6606
      } else if (pResourceName->type == TK_NK_ID) {
×
UNCOV
6607
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
UNCOV
6608
        pDropAgent->name = taosStrndupi(buf, sizeof(buf));
×
UNCOV
6609
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
6610
        pDropAgent->id = taosStr2Int32(pResourceName->z, NULL, 10);
×
6611
      } else {
UNCOV
6612
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6613
                                                "Invalid xnode agent id type: %d", pResourceName->type);
UNCOV
6614
        goto _err;
×
6615
      }
6616
      break;
2,820✔
6617
    case XNODE_JOB:
×
6618
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_JOB_STMT, (SNode**)&pStmt);
×
6619
      CHECK_MAKE_NODE(pStmt);
×
6620
      SDropXnodeJobStmt* pJobStmt = (SDropXnodeJobStmt*)pStmt;
×
6621

UNCOV
6622
      if (pResourceName->type == TK_NK_STRING) {
×
6623
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
×
UNCOV
6624
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
6625
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
×
6626
      } else {
UNCOV
6627
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6628
                                                "Invalid xnode job id type: %d", pResourceName->type);
UNCOV
6629
        goto _err;
×
6630
      }
UNCOV
6631
      break;
×
UNCOV
6632
    default:
×
UNCOV
6633
      break;
×
6634
  }
6635
  return (SNode*)pStmt;
6,627✔
6636
_err:
141✔
6637
  if (pStmt != NULL) {
141✔
6638
    nodesDestroyNode(pStmt);
×
6639
  }
6640
  return NULL;
141✔
6641
}
6642

6643
SNode* dropXnodeResourceWhere(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
23,124✔
6644
  CHECK_PARSER_STATUS(pCxt);
23,124✔
6645
  SDropXnodeJobStmt* pStmt = NULL;
23,124✔
6646
  switch (resourceType) {
23,124✔
UNCOV
6647
    case XNODE_TASK:
×
6648
    case XNODE_AGENT:
UNCOV
6649
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6650
                                              "Xnode only drop xnode job where ... support");
UNCOV
6651
      goto _err;
×
6652
    case XNODE_JOB:
23,124✔
6653
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_JOB_STMT, (SNode**)&pStmt);
23,124✔
6654
      CHECK_MAKE_NODE(pStmt);
23,124✔
6655
      pStmt->pWhere = pWhere;
23,124✔
6656
      break;
23,124✔
UNCOV
6657
    default:
×
6658
      break;
×
6659
  }
6660
  return (SNode*)pStmt;
23,124✔
6661
_err:
×
UNCOV
6662
  nodesDestroyNode(pWhere);
×
6663
  return NULL;
×
6664
}
6665

6666
SNode* createDefaultXnodeTaskOptions(SAstCreateContext* pCxt) {
21,150✔
6667
  CHECK_PARSER_STATUS(pCxt);
21,150✔
6668
  SXnodeTaskOptions* pOptions = NULL;
21,150✔
6669
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_OPTIONS, (SNode**)&pOptions);
21,150✔
6670
  CHECK_MAKE_NODE(pOptions);
21,150✔
6671
  return (SNode*)pOptions;
21,150✔
UNCOV
6672
_err:
×
UNCOV
6673
  return NULL;
×
6674
}
6675

6676
static char   TRIGGER[8] = "trigger";
6677
static SToken TRIGGER_TOKEN = {
6678
    .n = 7,
6679
    .type = TK_NK_ID,
6680
    .z = TRIGGER,
6681
};
UNCOV
6682
SToken* createTriggerToken() { return &TRIGGER_TOKEN; }
×
6683

6684
SNode*  setXnodeTaskOption(SAstCreateContext* pCxt, SNode* pTaskOptions, SToken* pKey, SToken* pVal) {
52,170✔
6685
  CHECK_PARSER_STATUS(pCxt);
52,170✔
6686
  if (pTaskOptions == NULL) {
52,170✔
6687
    pTaskOptions = createDefaultXnodeTaskOptions(pCxt);
21,150✔
6688
    if (pTaskOptions == NULL) {
21,150✔
UNCOV
6689
      pCxt->errCode =
×
UNCOV
6690
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task options should not be NULL");
×
UNCOV
6691
      goto _err;
×
6692
    }
6693
  }
6694
  SXnodeTaskOptions* pOptions = (SXnodeTaskOptions*)pTaskOptions;
52,170✔
6695
  char               key[TSDB_COL_NAME_LEN] = {0};
52,170✔
6696
  if (pKey == NULL) {
52,170✔
6697
    pCxt->errCode =
×
6698
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
6699
    goto _err;
×
6700
  }
6701
  TRIM_STRING_FORM_ID_TOKEN(key, pKey);
52,170✔
6702

6703
  if (strlen(key) == 0) {
52,170✔
UNCOV
6704
    pCxt->errCode =
×
UNCOV
6705
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
UNCOV
6706
    goto _err;
×
6707
  }
6708
  char via[TSDB_COL_NAME_LEN] = {0};
52,170✔
6709
  char buf[TSDB_XNODE_TASK_OPTIONS_MAX_NUM] = {0};
52,170✔
6710
  if (strcmp(key, "trigger") == 0) {
52,170✔
6711
    if (pVal->type == TK_NK_STRING) {
3,807✔
6712
      (void)trimString(pVal->z, pVal->n, pOptions->trigger, sizeof(pOptions->trigger));
3,807✔
6713
      pOptions->triggerLen = pVal->n == 2 ? 1 : pVal->n - 2;
3,807✔
6714
    } else {
UNCOV
6715
      pCxt->errCode =
×
UNCOV
6716
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option trigger must be string");
×
UNCOV
6717
      goto _err;
×
6718
    }
6719
  } else if (strcmp(key, "parser") == 0 || strcmp(key, "transform") == 0) {
48,363✔
6720
    if (pVal->type == TK_NK_STRING) {
3,243✔
6721
      if (pVal->n > TSDB_XNODE_TASK_PARSER_LEN + 2) {
3,243✔
UNCOV
6722
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_MND_XNODE_TASK_PARSER_TOO_LONG,
×
6723
                                                "Option parser must be string with length <= %d",
6724
                                                TSDB_XNODE_TASK_PARSER_LEN);
6725
        goto _err;
×
6726
      }
6727
      if (pOptions->parser) {
3,243✔
UNCOV
6728
        taosMemFreeClear(pOptions->parser);
×
6729
      }
6730
      pOptions->parserLen = pVal->n == 2 ? 1 : pVal->n - 2;
3,243✔
6731
      pOptions->parser = taosMemoryCalloc(1, pOptions->parserLen + 1);
3,243✔
6732
      (void)trimString(pVal->z, pVal->n, pOptions->parser, pOptions->parserLen + 1);
3,243✔
6733
    } else {
6734
      pCxt->errCode =
×
6735
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option parser must be string");
×
UNCOV
6736
      goto _err;
×
6737
    }
6738
  } else if (strcmp(key, "health") == 0) {
45,120✔
UNCOV
6739
    if (pVal->type == TK_NK_STRING) {
×
6740
      (void)trimString(pVal->z, pVal->n, pOptions->health, sizeof(pOptions->health));
×
6741
      pOptions->healthLen = pVal->n == 2 ? 1 : pVal->n - 2;
×
6742
    } else {
UNCOV
6743
      pCxt->errCode =
×
UNCOV
6744
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option health must be string");
×
UNCOV
6745
      goto _err;
×
6746
    }
6747
  } else if (strcmp(key, "via") == 0) {
45,120✔
6748
    switch (pVal->type) {
705✔
UNCOV
6749
      case TK_NK_STRING:
×
UNCOV
6750
        (void)trimString(pVal->z, pVal->n, via, sizeof(via));
×
6751
        pOptions->via = taosStr2Int32(via, NULL, 10);
×
6752
        if (pOptions->via <= 0) {
×
6753
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6754
                                                   "Invalid xnode task option via: %s", pVal->z);
UNCOV
6755
          goto _err;
×
6756
        }
UNCOV
6757
        break;
×
6758
      case TK_NK_INTEGER:
705✔
6759
        pOptions->via = taosStr2Int32(pVal->z, NULL, 10);
705✔
6760
        break;
705✔
6761
      default:
×
UNCOV
6762
        pCxt->errCode =
×
UNCOV
6763
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode task option: %s", key);
×
6764
    }
6765
  } else {
6766
    if (pOptions->optionsNum < TSDB_XNODE_TASK_OPTIONS_MAX_NUM) {
44,415✔
6767
      char* pKeyVal = NULL;
44,415✔
6768
      if (pVal != NULL) {
44,415✔
6769
        parserDebug("key value length expected: %d, actual: %d\n", pKey->n + pVal->n + 2, pKey->n + pVal->n);
44,415✔
6770
        pKeyVal = taosMemoryMalloc(pKey->n + pVal->n + 2);
44,415✔
6771
        memset(pKeyVal, 0, pKey->n + pVal->n + 2);
44,415✔
6772

6773
        CHECK_OUT_OF_MEM(pKeyVal);
44,415✔
6774
        size_t pos = strlen(key);
44,415✔
6775
        memcpy(pKeyVal, key, pos);
44,415✔
6776
        pKeyVal[pos] = '=';  // Add '=' after the key
44,415✔
6777
        pos++;
44,415✔
6778

6779
        if (pVal->type == TK_NK_STRING) {
44,415✔
6780
          (void)trimString(pVal->z, pVal->n, pKeyVal + pos, pVal->n + 1);
27,495✔
6781
        } else {
6782
          TAOS_STRNCPY(pKeyVal + pos, pVal->z, TMIN(pVal->n, pKey->n + pVal->n + 2 - pos - 1));
16,920✔
6783
          pKeyVal[pos + pVal->n] = '\0';
16,920✔
6784
        }
6785
      } else {
6786
        size_t keyLen = strlen(key);
×
6787
        pKeyVal = taosMemoryMalloc(keyLen + 1);
×
6788
        memset(pKeyVal, 0, keyLen + 1);
×
6789
        CHECK_OUT_OF_MEM(pKeyVal);
×
UNCOV
6790
        memcpy(pKeyVal, key, keyLen);
×
6791
      }
6792
      pOptions->options[pOptions->optionsNum] = pKeyVal;
44,415✔
6793
      pOptions->optionsNum++;
44,415✔
6794
    } else {
UNCOV
6795
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6796
                                               "reaches max options number(%d) %s", pOptions->optionsNum, key);
6797
      goto _err;
×
6798
    }
6799
  }
6800
  return pTaskOptions;
52,170✔
UNCOV
6801
_err:
×
UNCOV
6802
  nodesDestroyNode(pTaskOptions);
×
UNCOV
6803
  return NULL;
×
6804
}
6805

6806
SNode* createXnodeTaskJobWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pTidToken,
12,267✔
6807
                                     SNode* pNodeOptions) {
6808
  CHECK_PARSER_STATUS(pCxt);
12,267✔
6809
  SNode* pStmt = NULL;
12,267✔
6810

6811
  switch (resourceType) {
12,267✔
6812
    case XNODE_JOB: {
12,267✔
6813
      if (pTidToken == NULL || pTidToken->n <= 0) {
12,267✔
UNCOV
6814
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6815
                                                "Xnode job task id should not be NULL or empty");
UNCOV
6816
        goto _err;
×
6817
      }
6818
      if (pNodeOptions == NULL || nodeType(pNodeOptions) != QUERY_NODE_XNODE_TASK_OPTIONS) {
12,267✔
UNCOV
6819
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6820
                                                "Xnode job options should not be NULL or empty");
UNCOV
6821
        goto _err;
×
6822
      }
6823
      pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_JOB_STMT, &pStmt);
12,267✔
6824
      CHECK_MAKE_NODE(pStmt);
12,267✔
6825
      SCreateXnodeJobStmt* pJobStmt = (SCreateXnodeJobStmt*)pStmt;
12,267✔
6826
      pJobStmt->options = (SXnodeTaskOptions*)pNodeOptions;
12,267✔
6827
      pJobStmt->tid = taosStr2Int32(pTidToken->z, NULL, 10);
12,267✔
6828
      break;
12,267✔
6829
    }
UNCOV
6830
    default:
×
6831
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6832
                                              "Invalid xnode resource type: %d with ON clause", resourceType);
6833
      goto _err;
×
6834
  }
6835
  return pStmt;
12,267✔
UNCOV
6836
_err:
×
6837
  if (pStmt != NULL) {
×
6838
    nodesDestroyNode(pStmt);
×
6839
  }
UNCOV
6840
  return NULL;
×
6841
}
6842

UNCOV
6843
EPrivType xnodeResourceToPrivType(SAstCreateContext* pCxt, SToken* pResourceId, EPrivType privType) {
×
UNCOV
6844
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
6845
  const size_t TASK_LEN = 4;
×
UNCOV
6846
  const size_t TASKS_LEN = 5;
×
6847

UNCOV
6848
  if (pResourceId->z[0] == '`') {
×
UNCOV
6849
    if ((pResourceId->n == (2 + TASK_LEN) && strncmp(pResourceId->z + 1, "task", TASK_LEN) == 0) ||
×
6850
        (pResourceId->n == (2 + TASKS_LEN) && strncmp(pResourceId->z + 1, "tasks", TASKS_LEN) == 0)) {
×
UNCOV
6851
      return privType;
×
6852
    }
UNCOV
6853
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6854
                                            "Syntax error: Invalid grant xnode resource type at: %s", pResourceId->z);
6855
    goto _err;
×
6856
  }
6857
  if ((pResourceId->n == TASK_LEN && strncmp(pResourceId->z, "task", TASK_LEN) == 0) ||
×
UNCOV
6858
      (pResourceId->n == TASKS_LEN && strncmp(pResourceId->z, "tasks", TASKS_LEN) == 0)) {
×
UNCOV
6859
    return privType;
×
6860
  }
UNCOV
6861
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6862
                                          "Syntax error: Invalid grant xnode resource type at: %s", pResourceId->z);
6863

UNCOV
6864
_err:
×
UNCOV
6865
  return PRIV_TYPE_UNKNOWN;
×
6866
}
6867

UNCOV
6868
SPrivLevelArgs xnodeTaskObjPrivLevelSet(SAstCreateContext* pCxt, SToken* resId, SPrivLevelArgs privLevelArgs) {
×
6869
  (void)xnodeResourceToPrivType(pCxt, resId, PRIV_TYPE_UNKNOWN);
×
UNCOV
6870
  CHECK_PARSER_STATUS(pCxt);
×
6871

6872
_err:
×
6873
  return privLevelArgs;
×
6874
}
6875

6876
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
1,823✔
6877
  SToken config;
1,823✔
6878
  config.type = TK_NK_STRING;
1,823✔
6879
  config.z = "\"encrypt_key\"";
1,823✔
6880
  config.n = strlen(config.z);
1,823✔
6881
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
1,823✔
6882
}
6883

6884
SNode* createAlterEncryptKeyStmt(SAstCreateContext* pCxt, int8_t keyType, const SToken* pValue) {
304✔
6885
  CHECK_PARSER_STATUS(pCxt);
304✔
6886
  SAlterEncryptKeyStmt* pStmt = NULL;
304✔
6887
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ENCRYPT_KEY_STMT, (SNode**)&pStmt);
304✔
6888
  CHECK_MAKE_NODE(pStmt);
304✔
6889

6890
  pStmt->keyType = keyType;
304✔
6891
  if (NULL != pValue) {
304✔
6892
    (void)trimString(pValue->z, pValue->n, pStmt->newKey, sizeof(pStmt->newKey));
304✔
6893
  }
6894

6895
  return (SNode*)pStmt;
304✔
UNCOV
6896
_err:
×
6897
  return NULL;
×
6898
}
6899

6900
SNode* createAlterKeyExpirationStmt(SAstCreateContext* pCxt, const SToken* pDays, const SToken* pStrategy) {
×
6901
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
6902
  SAlterKeyExpirationStmt* pStmt = NULL;
×
UNCOV
6903
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_KEY_EXPIRATION_STMT, (SNode**)&pStmt);
×
6904
  CHECK_MAKE_NODE(pStmt);
×
6905

6906
  if (NULL != pDays) {
×
UNCOV
6907
    pStmt->days = taosStr2Int32(pDays->z, NULL, 10);
×
6908
  }
6909
  if (NULL != pStrategy) {
×
UNCOV
6910
    (void)trimString(pStrategy->z, pStrategy->n, pStmt->strategy, sizeof(pStmt->strategy));
×
6911
  }
6912

UNCOV
6913
  return (SNode*)pStmt;
×
UNCOV
6914
_err:
×
UNCOV
6915
  return NULL;
×
6916
}
6917

6918
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
12,163✔
6919
  if (!checkIndexName(pCxt, pIndexName)) {
12,163✔
UNCOV
6920
    return NULL;
×
6921
  }
6922
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
12,163✔
6923
}
6924

6925
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
23,400✔
6926
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
6927
  CHECK_PARSER_STATUS(pCxt);
23,400✔
6928
  SCreateIndexStmt* pStmt = NULL;
23,400✔
6929
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
23,400✔
6930
  CHECK_MAKE_NODE(pStmt);
23,400✔
6931
  pStmt->indexType = type;
23,400✔
6932
  pStmt->ignoreExists = ignoreExists;
23,400✔
6933

6934
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
23,400✔
6935
  if (strlen(pFullTable->table.dbName) == 0) {
23,400✔
6936
    // no db specified,
6937
    if (pCxt->pQueryCxt->db == NULL) {
×
6938
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
6939
      CHECK_PARSER_STATUS(pCxt);
×
6940
    } else {
UNCOV
6941
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
6942
    }
6943
  } else {
6944
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
23,400✔
6945
  }
6946
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
23,400✔
6947
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
23,400✔
6948
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
23,400✔
6949
  nodesDestroyNode(pIndexName);
23,400✔
6950
  nodesDestroyNode(pRealTable);
23,400✔
6951
  pStmt->pCols = pCols;
23,400✔
6952
  pStmt->pOptions = (SIndexOptions*)pOptions;
23,400✔
6953
  return (SNode*)pStmt;
23,400✔
UNCOV
6954
_err:
×
UNCOV
6955
  nodesDestroyNode(pIndexName);
×
6956
  nodesDestroyNode(pRealTable);
×
UNCOV
6957
  nodesDestroyNode(pOptions);
×
UNCOV
6958
  nodesDestroyList(pCols);
×
UNCOV
6959
  return NULL;
×
6960
}
6961

UNCOV
6962
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
×
6963
                         SNode* pStreamOptions) {
UNCOV
6964
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
6965
  SIndexOptions* pOptions = NULL;
×
UNCOV
6966
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
×
UNCOV
6967
  CHECK_MAKE_NODE(pOptions);
×
UNCOV
6968
  pOptions->pFuncs = pFuncs;
×
UNCOV
6969
  pOptions->pInterval = pInterval;
×
UNCOV
6970
  pOptions->pOffset = pOffset;
×
UNCOV
6971
  pOptions->pSliding = pSliding;
×
UNCOV
6972
  pOptions->pStreamOptions = pStreamOptions;
×
6973
  return (SNode*)pOptions;
×
6974
_err:
×
6975
  nodesDestroyNode(pInterval);
×
UNCOV
6976
  nodesDestroyNode(pOffset);
×
6977
  nodesDestroyNode(pSliding);
×
UNCOV
6978
  nodesDestroyNode(pStreamOptions);
×
UNCOV
6979
  return NULL;
×
6980
}
6981

6982
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
12,163✔
6983
  CHECK_PARSER_STATUS(pCxt);
12,163✔
6984
  SDropIndexStmt* pStmt = NULL;
12,163✔
6985
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
12,163✔
6986
  CHECK_MAKE_NODE(pStmt);
12,163✔
6987
  pStmt->ignoreNotExists = ignoreNotExists;
12,163✔
6988
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
12,163✔
6989
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
12,163✔
6990
  nodesDestroyNode(pIndexName);
12,163✔
6991
  return (SNode*)pStmt;
12,163✔
6992
_err:
×
6993
  nodesDestroyNode(pIndexName);
×
6994
  return NULL;
×
6995
}
6996

6997
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
120,120✔
6998
  CHECK_PARSER_STATUS(pCxt);
120,120✔
6999
  SCreateComponentNodeStmt* pStmt = NULL;
120,120✔
7000
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
120,120✔
7001
  CHECK_MAKE_NODE(pStmt);
120,120✔
7002
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
120,120✔
7003
  return (SNode*)pStmt;
120,120✔
7004
_err:
×
7005
  return NULL;
×
7006
}
7007

7008
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
55,713✔
7009
  CHECK_PARSER_STATUS(pCxt);
55,713✔
7010
  SDropComponentNodeStmt* pStmt = NULL;
55,713✔
7011
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
55,713✔
7012
  CHECK_MAKE_NODE(pStmt);
55,713✔
7013
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
55,713✔
7014
  return (SNode*)pStmt;
55,713✔
7015
_err:
×
UNCOV
7016
  return NULL;
×
7017
}
7018

7019
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
2,521✔
7020
  CHECK_PARSER_STATUS(pCxt);
2,521✔
7021
  SRestoreComponentNodeStmt* pStmt = NULL;
2,521✔
7022
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,521✔
7023
  CHECK_MAKE_NODE(pStmt);
2,521✔
7024
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
2,521✔
7025
  pStmt->vgId = 0;
2,521✔
7026
  return (SNode*)pStmt;
2,521✔
UNCOV
7027
_err:
×
7028
  return NULL;
×
7029
}
7030

7031
SNode* createRestoreComponentNodeStmtWithVgId(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId, const SToken* pVgId) {
791✔
7032
  SRestoreComponentNodeStmt* pStmt = (SRestoreComponentNodeStmt*)createRestoreComponentNodeStmt(pCxt, type, pDnodeId);
791✔
7033
  if (pStmt == NULL) {
791✔
UNCOV
7034
    return NULL;
×
7035
  }
7036

7037
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
791✔
7038
  return (SNode*)pStmt;
791✔
7039
}
7040

7041
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery, bool reload) {
160,973✔
7042
  CHECK_PARSER_STATUS(pCxt);
160,973✔
7043
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
160,973✔
7044
  SCreateTopicStmt* pStmt = NULL;
160,367✔
7045
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
160,367✔
7046
  CHECK_MAKE_NODE(pStmt);
160,367✔
7047
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
160,367✔
7048
  pStmt->ignoreExists = ignoreExists;
160,367✔
7049
  pStmt->pQuery = pQuery;
160,367✔
7050
  pStmt->reload = reload;
160,367✔
7051
  return (SNode*)pStmt;
160,367✔
7052
_err:
606✔
7053
  nodesDestroyNode(pQuery);
606✔
7054
  return NULL;
606✔
7055
}
7056

7057
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
30,068✔
7058
                                  int8_t withMeta) {
7059
  CHECK_PARSER_STATUS(pCxt);
30,068✔
7060
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
30,068✔
7061
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
30,068✔
7062
  SCreateTopicStmt* pStmt = NULL;
30,068✔
7063
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
30,068✔
7064
  CHECK_MAKE_NODE(pStmt);
30,068✔
7065
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
30,068✔
7066
  pStmt->ignoreExists = ignoreExists;
30,068✔
7067
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
30,068✔
7068
  pStmt->withMeta = withMeta;
30,068✔
7069
  return (SNode*)pStmt;
30,068✔
7070
_err:
×
UNCOV
7071
  return NULL;
×
7072
}
7073

7074
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
19,642✔
7075
                                     int8_t withMeta, SNode* pWhere) {
7076
  CHECK_PARSER_STATUS(pCxt);
19,642✔
7077
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
19,642✔
7078
  SCreateTopicStmt* pStmt = NULL;
19,642✔
7079
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
19,642✔
7080
  CHECK_MAKE_NODE(pStmt);
19,642✔
7081
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
19,642✔
7082
  pStmt->ignoreExists = ignoreExists;
19,642✔
7083
  pStmt->withMeta = withMeta;
19,642✔
7084
  pStmt->pWhere = pWhere;
19,642✔
7085

7086
  tstrncpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
19,642✔
7087
  tstrncpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
19,642✔
7088
  nodesDestroyNode(pRealTable);
19,642✔
7089
  return (SNode*)pStmt;
19,642✔
UNCOV
7090
_err:
×
UNCOV
7091
  nodesDestroyNode(pRealTable);
×
UNCOV
7092
  nodesDestroyNode(pWhere);
×
UNCOV
7093
  return NULL;
×
7094
}
7095

7096
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName, bool force) {
127,782✔
7097
  CHECK_PARSER_STATUS(pCxt);
127,782✔
7098
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
127,782✔
7099
  SDropTopicStmt* pStmt = NULL;
127,564✔
7100
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
127,564✔
7101
  CHECK_MAKE_NODE(pStmt);
127,564✔
7102
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
127,564✔
7103
  pStmt->ignoreNotExists = ignoreNotExists;
127,564✔
7104
  pStmt->force = force;
127,564✔
7105
  return (SNode*)pStmt;
127,564✔
7106
_err:
218✔
7107
  return NULL;
218✔
7108
}
7109

7110
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName,
2,507✔
7111
                            bool force) {
7112
  CHECK_PARSER_STATUS(pCxt);
2,507✔
7113
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
2,507✔
7114
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
2,507✔
7115
  SDropCGroupStmt* pStmt = NULL;
2,507✔
7116
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
2,507✔
7117
  CHECK_MAKE_NODE(pStmt);
2,507✔
7118
  pStmt->ignoreNotExists = ignoreNotExists;
2,507✔
7119
  pStmt->force = force;
2,507✔
7120
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
2,507✔
7121
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
2,507✔
7122
  return (SNode*)pStmt;
2,507✔
UNCOV
7123
_err:
×
UNCOV
7124
  return NULL;
×
7125
}
7126

7127
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
4,580✔
7128
  CHECK_PARSER_STATUS(pCxt);
4,580✔
7129
  SAlterClusterStmt* pStmt = NULL;
4,580✔
7130
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
4,580✔
7131
  CHECK_MAKE_NODE(pStmt);
4,580✔
7132
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
4,580✔
7133
  if (NULL != pValue) {
4,580✔
7134
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
4,580✔
7135
  }
7136
  return (SNode*)pStmt;
4,580✔
UNCOV
7137
_err:
×
UNCOV
7138
  return NULL;
×
7139
}
7140

7141
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
576,159✔
7142
  CHECK_PARSER_STATUS(pCxt);
576,159✔
7143
  SAlterLocalStmt* pStmt = NULL;
576,159✔
7144
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
576,159✔
7145
  CHECK_MAKE_NODE(pStmt);
576,159✔
7146
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
576,159✔
7147
  if (NULL != pValue) {
576,159✔
7148
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
569,620✔
7149
  }
7150
  return (SNode*)pStmt;
576,159✔
UNCOV
7151
_err:
×
UNCOV
7152
  return NULL;
×
7153
}
7154

7155
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
110,387,420✔
7156
  CHECK_PARSER_STATUS(pCxt);
110,387,420✔
7157
  SExplainOptions* pOptions = NULL;
110,387,420✔
7158
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
110,387,420✔
7159
  CHECK_MAKE_NODE(pOptions);
110,389,263✔
7160
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
110,389,263✔
7161
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
110,389,263✔
7162
  return (SNode*)pOptions;
110,389,263✔
UNCOV
7163
_err:
×
UNCOV
7164
  return NULL;
×
7165
}
7166

7167
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
59,897,849✔
7168
  CHECK_PARSER_STATUS(pCxt);
59,897,849✔
7169
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
59,897,849✔
7170
  return pOptions;
59,897,849✔
UNCOV
7171
_err:
×
UNCOV
7172
  return NULL;
×
7173
}
7174

7175
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
100,532✔
7176
  CHECK_PARSER_STATUS(pCxt);
100,532✔
7177
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
100,532✔
7178
  return pOptions;
100,532✔
UNCOV
7179
_err:
×
UNCOV
7180
  return NULL;
×
7181
}
7182

7183
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
101,843,591✔
7184
  CHECK_PARSER_STATUS(pCxt);
101,843,591✔
7185
  SExplainStmt* pStmt = NULL;
101,843,591✔
7186
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
101,843,591✔
7187
  CHECK_MAKE_NODE(pStmt);
101,857,266✔
7188
  pStmt->analyze = analyze;
101,857,266✔
7189
  pStmt->pOptions = (SExplainOptions*)pOptions;
101,857,266✔
7190
  pStmt->pQuery = pQuery;
101,857,266✔
7191
  return (SNode*)pStmt;
101,857,266✔
UNCOV
7192
_err:
×
UNCOV
7193
  nodesDestroyNode(pOptions);
×
UNCOV
7194
  nodesDestroyNode(pQuery);
×
7195
  return NULL;
1✔
7196
}
7197

7198
SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
579,836✔
7199
  CHECK_PARSER_STATUS(pCxt);
579,836✔
7200
  SDescribeStmt* pStmt = NULL;
579,836✔
7201
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT, (SNode**)&pStmt);
579,836✔
7202
  CHECK_MAKE_NODE(pStmt);
579,873✔
7203
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
579,873✔
7204
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
579,836✔
7205
  nodesDestroyNode(pRealTable);
579,804✔
7206
  return (SNode*)pStmt;
579,804✔
7207
_err:
×
7208
  nodesDestroyNode(pRealTable);
×
UNCOV
7209
  return NULL;
×
7210
}
7211

7212
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
2,262,083✔
7213
  CHECK_PARSER_STATUS(pCxt);
2,262,083✔
7214
  SNode* pStmt = NULL;
2,262,083✔
7215
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
2,262,083✔
7216
  CHECK_MAKE_NODE(pStmt);
2,262,083✔
7217
  return pStmt;
2,262,083✔
UNCOV
7218
_err:
×
UNCOV
7219
  return NULL;
×
7220
}
7221

7222
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
16,661✔
7223
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
16,661✔
7224
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
12,420✔
7225
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
4,241✔
7226
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
4,241✔
7227
  } else {
7228
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7229
                                            "udf programming language supports c and python");
7230
  }
7231
  return pCxt->errCode;
16,661✔
7232
}
7233

7234
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
16,661✔
7235
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
7236
                                bool orReplace) {
7237
  CHECK_PARSER_STATUS(pCxt);
16,661✔
7238
  if (pLibPath->n <= 2) {
16,661✔
UNCOV
7239
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
7240
    CHECK_PARSER_STATUS(pCxt);
×
7241
  }
7242
  int8_t language = 0;
16,661✔
7243
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
16,661✔
7244
  CHECK_PARSER_STATUS(pCxt);
16,661✔
7245
  SCreateFunctionStmt* pStmt = NULL;
16,661✔
7246
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
16,661✔
7247
  CHECK_MAKE_NODE(pStmt);
16,661✔
7248
  pStmt->orReplace = orReplace;
16,661✔
7249
  pStmt->ignoreExists = ignoreExists;
16,661✔
7250
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
16,661✔
7251
  pStmt->isAgg = aggFunc;
16,661✔
7252
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
16,661✔
7253
  pStmt->outputDt = dataType;
16,661✔
7254
  pStmt->bufSize = bufSize;
16,661✔
7255
  pStmt->language = language;
16,661✔
7256
  return (SNode*)pStmt;
16,661✔
UNCOV
7257
_err:
×
UNCOV
7258
  return NULL;
×
7259
}
7260

7261
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
9,175✔
7262
  CHECK_PARSER_STATUS(pCxt);
9,175✔
7263
  SDropFunctionStmt* pStmt = NULL;
9,175✔
7264
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
9,175✔
7265
  CHECK_MAKE_NODE(pStmt);
9,175✔
7266
  pStmt->ignoreNotExists = ignoreNotExists;
9,175✔
7267
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
9,175✔
7268
  return (SNode*)pStmt;
9,175✔
UNCOV
7269
_err:
×
UNCOV
7270
  return NULL;
×
7271
}
7272

7273
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
230,475✔
7274
  SCreateViewStmt* pStmt = NULL;
230,475✔
7275
  CHECK_PARSER_STATUS(pCxt);
230,475✔
7276
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
230,475✔
7277
  CHECK_MAKE_NODE(pStmt);
230,475✔
7278
  int32_t i = pAs->n;
230,475✔
7279
  while (isspace(*(pAs->z + i))) {
460,950✔
7280
    ++i;
230,475✔
7281
  }
7282
  pStmt->pQuerySql = tstrdup(pAs->z + i);
230,475✔
7283
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
230,475✔
7284
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
230,475✔
7285
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
230,475✔
7286
  nodesDestroyNode(pView);
230,475✔
7287
  pStmt->orReplace = orReplace;
230,475✔
7288
  pStmt->pQuery = pQuery;
230,475✔
7289
  return (SNode*)pStmt;
230,475✔
UNCOV
7290
_err:
×
UNCOV
7291
  nodesDestroyNode(pView);
×
UNCOV
7292
  nodesDestroyNode(pQuery);
×
7293
  nodesDestroyNode((SNode*)pStmt);
×
7294
  return NULL;
×
7295
}
7296

7297
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
172,974✔
7298
  CHECK_PARSER_STATUS(pCxt);
172,974✔
7299
  SDropViewStmt* pStmt = NULL;
172,538✔
7300
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
172,538✔
7301
  CHECK_MAKE_NODE(pStmt);
172,538✔
7302
  pStmt->ignoreNotExists = ignoreNotExists;
172,538✔
7303
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
172,538✔
7304
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
172,538✔
7305
  nodesDestroyNode(pView);
172,538✔
7306
  return (SNode*)pStmt;
172,538✔
7307
_err:
436✔
7308
  nodesDestroyNode(pView);
436✔
7309
  return NULL;
436✔
7310
}
7311

7312
SNode* createStreamOutTableNode(SAstCreateContext* pCxt, SNode* pIntoTable, SNode* pOutputSubTable, SNodeList* pColList,
534,173✔
7313
                                SNodeList* pTagList, int32_t nodelayCreateSubtable) {
7314
  SStreamOutTableNode* pOutTable = NULL;
534,173✔
7315
  CHECK_PARSER_STATUS(pCxt);
534,173✔
7316
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OUT_TABLE, (SNode**)&pOutTable);
533,955✔
7317
  CHECK_MAKE_NODE(pOutTable);
533,955✔
7318
  pOutTable->pOutTable = pIntoTable;
533,955✔
7319
  pOutTable->pSubtable = pOutputSubTable;
533,955✔
7320
  pOutTable->pCols = pColList;
533,955✔
7321
  pOutTable->pTags = pTagList;
533,955✔
7322
  pOutTable->nodelayCreateSubtable = (nodelayCreateSubtable != 0) ? 1 : 0;
533,955✔
7323
  return (SNode*)pOutTable;
533,955✔
7324

7325
_err:
218✔
7326
  nodesDestroyNode((SNode*)pOutTable);
218✔
7327
  nodesDestroyNode(pIntoTable);
218✔
7328
  nodesDestroyNode(pOutputSubTable);
218✔
7329
  nodesDestroyList(pColList);
218✔
7330
  nodesDestroyList(pTagList);
218✔
7331
  return NULL;
218✔
7332
}
7333

7334
SNode* createStreamTriggerNode(SAstCreateContext* pCxt, SNode* pTriggerWindow, SNode* pTriggerTable,
544,456✔
7335
                               SNodeList* pPartitionList, SNode* pOptions, SNode* pNotification) {
7336
  SStreamTriggerNode* pTrigger = NULL;
544,456✔
7337
  CHECK_PARSER_STATUS(pCxt);
544,456✔
7338
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER, (SNode**)&pTrigger);
544,372✔
7339
  CHECK_MAKE_NODE(pTrigger);
544,372✔
7340

7341
  pTrigger->pOptions = pOptions;
544,372✔
7342
  pTrigger->pNotify = pNotification;
544,372✔
7343
  pTrigger->pTrigerTable = pTriggerTable;
544,372✔
7344
  pTrigger->pPartitionList = pPartitionList;
544,372✔
7345
  pTrigger->pTriggerWindow = pTriggerWindow;
544,372✔
7346
  return (SNode*)pTrigger;
544,372✔
7347

7348
_err:
84✔
7349
  nodesDestroyNode((SNode*)pTrigger);
84✔
7350
  nodesDestroyNode(pTriggerWindow);
84✔
7351
  nodesDestroyNode(pTriggerTable);
84✔
7352
  nodesDestroyNode(pOptions);
84✔
7353
  nodesDestroyNode(pNotification);
84✔
7354
  nodesDestroyList(pPartitionList);
84✔
7355
  return NULL;
84✔
7356
}
7357

7358
SNode* createSlidingWindowNode(SAstCreateContext* pCxt, SNode* pSlidingVal, SNode* pOffset) {
239,660✔
7359
  SSlidingWindowNode* pSliding = NULL;
239,660✔
7360
  CHECK_PARSER_STATUS(pCxt);
239,660✔
7361
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SLIDING_WINDOW, (SNode**)&pSliding);
239,660✔
7362
  CHECK_MAKE_NODE(pSliding);
239,660✔
7363
  pSliding->pSlidingVal = pSlidingVal;
239,660✔
7364
  pSliding->pOffset = pOffset;
239,660✔
7365
  return (SNode*)pSliding;
239,660✔
UNCOV
7366
_err:
×
UNCOV
7367
  nodesDestroyNode(pSlidingVal);
×
UNCOV
7368
  nodesDestroyNode(pOffset);
×
UNCOV
7369
  nodesDestroyNode((SNode*)pSliding);
×
UNCOV
7370
  return NULL;
×
7371
}
7372

7373
SNode* createStreamTriggerOptions(SAstCreateContext* pCxt) {
205,158✔
7374
  SStreamTriggerOptions* pOptions = NULL;
205,158✔
7375
  CHECK_PARSER_STATUS(pCxt);
205,158✔
7376
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER_OPTIONS, (SNode**)&pOptions);
205,158✔
7377
  CHECK_MAKE_NODE(pOptions);
205,158✔
7378
  pOptions->pPreFilter = NULL;
205,158✔
7379
  pOptions->pWaterMark = NULL;
205,158✔
7380
  pOptions->pMaxDelay = NULL;
205,158✔
7381
  pOptions->pExpiredTime = NULL;
205,158✔
7382
  pOptions->pFillHisStartTime = NULL;
205,158✔
7383
  pOptions->pIdleTimeout = NULL;
205,158✔
7384
  pOptions->pEventType = EVENT_NONE;
205,158✔
7385
  pOptions->calcNotifyOnly = false;
205,158✔
7386
  pOptions->deleteOutputTable = false;
205,158✔
7387
  pOptions->deleteRecalc = false;
205,158✔
7388
  pOptions->fillHistory = false;
205,158✔
7389
  pOptions->fillHistoryFirst = false;
205,158✔
7390
  pOptions->lowLatencyCalc = false;
205,158✔
7391
  pOptions->forceOutput = false;
205,158✔
7392
  pOptions->ignoreDisorder = false;
205,158✔
7393
  pOptions->ignoreNoDataTrigger = false;
205,158✔
7394
  return (SNode*)pOptions;
205,158✔
UNCOV
7395
_err:
×
UNCOV
7396
  nodesDestroyNode((SNode*)pOptions);
×
UNCOV
7397
  return NULL;
×
7398
}
7399

7400
SNode* createStreamTagDefNode(SAstCreateContext* pCxt, SToken* pTagName, SDataType dataType, SNode* tagExpression) {
43,518✔
7401
  SStreamTagDefNode* pTagDef = NULL;
43,518✔
7402
  CHECK_PARSER_STATUS(pCxt);
43,518✔
7403
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TAG_DEF, (SNode**)&pTagDef);
43,518✔
7404
  CHECK_MAKE_NODE(pTagDef);
43,518✔
7405
  COPY_STRING_FORM_ID_TOKEN(pTagDef->tagName, pTagName);
43,518✔
7406
  int32_t nameLen = strdequote(pTagDef->tagName);
43,518✔
7407
  pTagDef->tagName[nameLen] = '\0';
43,518✔
7408
  pTagDef->dataType = dataType;
43,518✔
7409
  pTagDef->pTagExpr = tagExpression;
43,518✔
7410
  return (SNode*)pTagDef;
43,518✔
UNCOV
7411
_err:
×
UNCOV
7412
  nodesDestroyNode(tagExpression);
×
UNCOV
7413
  nodesDestroyNode((SNode*)pTagDef);
×
UNCOV
7414
  return NULL;
×
7415
}
7416

7417
SNode* setStreamTriggerOptions(SAstCreateContext* pCxt, SNode* pOptions, SStreamTriggerOption* pOptionUnit) {
269,418✔
7418
  CHECK_PARSER_STATUS(pCxt);
269,418✔
7419
  SStreamTriggerOptions* pStreamOptions = (SStreamTriggerOptions*)pOptions;
269,418✔
7420
  switch (pOptionUnit->type) {
269,418✔
7421
    case STREAM_TRIGGER_OPTION_CALC_NOTIFY_ONLY:
2,804✔
7422
      if (pStreamOptions->calcNotifyOnly) {
2,804✔
UNCOV
7423
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7424
                                                "CALC_NOTIFY_ONLY specified multiple times");
UNCOV
7425
        goto _err;
×
7426
      }
7427
      pStreamOptions->calcNotifyOnly = true;
2,804✔
7428
      break;
2,804✔
7429
    case STREAM_TRIGGER_OPTION_DELETE_OUTPUT_TABLE:
796✔
7430
      if (pStreamOptions->deleteOutputTable) {
796✔
7431
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7432
                                                "DELETE_OUTPUT_TABLE specified multiple times");
7433
        goto _err;
×
7434
      }
7435
      pStreamOptions->deleteOutputTable = true;
796✔
7436
      break;
796✔
7437
    case STREAM_TRIGGER_OPTION_DELETE_RECALC:
13,978✔
7438
      if (pStreamOptions->deleteRecalc) {
13,978✔
UNCOV
7439
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7440
                                                "DELETE_RECALC specified multiple times");
UNCOV
7441
        goto _err;
×
7442
      }
7443
      pStreamOptions->deleteRecalc = true;
13,978✔
7444
      break;
13,978✔
7445
    case STREAM_TRIGGER_OPTION_EXPIRED_TIME:
10,519✔
7446
      if (pStreamOptions->pExpiredTime != NULL) {
10,519✔
7447
        pCxt->errCode =
×
7448
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EXPIRED_TIME specified multiple times");
×
7449
        goto _err;
×
7450
      }
7451
      pStreamOptions->pExpiredTime = pOptionUnit->pNode;
10,519✔
7452
      break;
10,519✔
7453
    case STREAM_TRIGGER_OPTION_FORCE_OUTPUT:
5,243✔
7454
      if (pStreamOptions->forceOutput) {
5,243✔
UNCOV
7455
        pCxt->errCode =
×
UNCOV
7456
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FORCE_OUTPUT specified multiple times");
×
UNCOV
7457
        goto _err;
×
7458
      }
7459
      pStreamOptions->forceOutput = true;
5,243✔
7460
      break;
5,243✔
7461
    case STREAM_TRIGGER_OPTION_FILL_HISTORY:
48,434✔
7462
      if (pStreamOptions->fillHistoryFirst) {
48,434✔
UNCOV
7463
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7464
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
UNCOV
7465
        goto _err;
×
7466
      }
7467
      if (pStreamOptions->pFillHisStartTime != NULL) {
48,434✔
UNCOV
7468
        pCxt->errCode =
×
7469
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FILL_HISTORY specified multiple times");
×
UNCOV
7470
        goto _err;
×
7471
      }
7472
      pStreamOptions->fillHistory = true;
48,434✔
7473
      if (pOptionUnit->pNode == NULL) {
48,434✔
7474
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
20,514✔
7475
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
20,514✔
7476
      } else {
7477
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
27,920✔
7478
      }
7479
      break;
48,434✔
7480
    case STREAM_TRIGGER_OPTION_FILL_HISTORY_FIRST:
4,478✔
7481
      if (pStreamOptions->fillHistory) {
4,478✔
7482
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
910✔
7483
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
7484
        goto _err;
910✔
7485
      }
7486
      if (pStreamOptions->pFillHisStartTime != NULL) {
3,568✔
UNCOV
7487
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7488
                                                "FILL_HISTORY_FIRST specified multiple times");
UNCOV
7489
        goto _err;
×
7490
      }
7491
      pStreamOptions->fillHistoryFirst = true;
3,568✔
7492
      if (pOptionUnit->pNode == NULL) {
3,568✔
7493
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
512✔
7494
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
512✔
7495
      } else {
7496
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
3,056✔
7497
      }
7498
      break;
3,568✔
7499
    case STREAM_TRIGGER_OPTION_IGNORE_DISORDER:
35,666✔
7500
      if (pStreamOptions->ignoreDisorder) {
35,666✔
7501
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
398✔
7502
                                                "IGNORE_DISORDER specified multiple times");
7503
        goto _err;
398✔
7504
      }
7505
      pStreamOptions->ignoreDisorder = true;
35,268✔
7506
      break;
35,268✔
7507
    case STREAM_TRIGGER_OPTION_LOW_LATENCY_CALC:
19,709✔
7508
      if (pStreamOptions->lowLatencyCalc) {
19,709✔
UNCOV
7509
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7510
                                                "LOW_LATENCY_CALC specified multiple times");
UNCOV
7511
        goto _err;
×
7512
      }
7513
      pStreamOptions->lowLatencyCalc = true;
19,709✔
7514
      break;
19,709✔
7515
    case STREAM_TRIGGER_OPTION_MAX_DELAY:
10,863✔
7516
      if (pStreamOptions->pMaxDelay != NULL) {
10,863✔
UNCOV
7517
        pCxt->errCode =
×
UNCOV
7518
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "MAX_DELAY specified multiple times");
×
UNCOV
7519
        goto _err;
×
7520
      }
7521
      pStreamOptions->pMaxDelay = pOptionUnit->pNode;
10,863✔
7522
      break;
10,863✔
7523
    case STREAM_TRIGGER_OPTION_WATERMARK:
11,369✔
7524
      if (pStreamOptions->pWaterMark != NULL) {
11,369✔
7525
        pCxt->errCode =
×
UNCOV
7526
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "WATERMARK specified multiple times");
×
UNCOV
7527
        goto _err;
×
7528
      }
7529
      pStreamOptions->pWaterMark = pOptionUnit->pNode;
11,369✔
7530
      break;
11,369✔
7531
    case STREAM_TRIGGER_OPTION_PRE_FILTER:
26,290✔
7532
      if (pStreamOptions->pPreFilter != NULL) {
26,290✔
7533
        pCxt->errCode =
398✔
7534
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "PRE_FILTER specified multiple times");
398✔
7535
        goto _err;
398✔
7536
      }
7537
      pStreamOptions->pPreFilter = pOptionUnit->pNode;
25,892✔
7538
      break;
25,892✔
7539
    case STREAM_TRIGGER_OPTION_EVENT_TYPE:
28,561✔
7540
      if (pStreamOptions->pEventType != EVENT_NONE) {
28,561✔
UNCOV
7541
        pCxt->errCode =
×
UNCOV
7542
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EVENT_TYPE specified multiple times");
×
UNCOV
7543
        goto _err;
×
7544
      }
7545
      pStreamOptions->pEventType = pOptionUnit->flag;
28,561✔
7546
      break;
28,561✔
7547
    case STREAM_TRIGGER_OPTION_IGNORE_NODATA_TRIGGER:
34,331✔
7548
      if (pStreamOptions->ignoreNoDataTrigger) {
34,331✔
UNCOV
7549
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7550
                                                "IGNORE_NODATA_TRIGGER specified multiple times");
UNCOV
7551
        goto _err;
×
7552
      }
7553
      pStreamOptions->ignoreNoDataTrigger = true;
34,331✔
7554
      break;
34,331✔
7555
    case STREAM_TRIGGER_OPTION_IDLE_TIMEOUT:
16,377✔
7556
      if (pStreamOptions->pIdleTimeout != NULL) {
16,377✔
UNCOV
7557
        pCxt->errCode =
×
UNCOV
7558
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "IDLE_TIMEOUT specified multiple times");
×
UNCOV
7559
        goto _err;
×
7560
      }
7561
      pStreamOptions->pIdleTimeout = pOptionUnit->pNode;
16,377✔
7562
      break;
16,377✔
7563
    default:
×
UNCOV
7564
      break;
×
7565
  }
7566
  return pOptions;
267,712✔
7567
_err:
1,706✔
7568
  nodesDestroyNode(pOptionUnit->pNode);
1,706✔
7569
  nodesDestroyNode(pOptions);
1,706✔
7570
  return NULL;
1,706✔
7571
}
7572

7573
static bool validateNotifyUrl(const char* url) {
125,800✔
7574
  const char* prefix[] = {"ws://", "wss://"};
125,800✔
7575
  const char* host = NULL;
125,800✔
7576

7577
  if (!url || *url == '\0') return false;
125,800✔
7578

7579
  for (int32_t i = 0; i < ARRAY_SIZE(prefix); ++i) {
125,968✔
7580
    if (taosStrncasecmp(url, prefix[i], strlen(prefix[i])) == 0) {
125,884✔
7581
      host = url + strlen(prefix[i]);
125,716✔
7582
      break;
125,716✔
7583
    }
7584
  }
7585

7586
  return (host != NULL) && (*host != '\0') && (*host != '/');
125,800✔
7587
}
7588

7589
SNode* createStreamNotifyOptions(SAstCreateContext* pCxt, SNodeList* pAddrUrls, int64_t eventType, SNode* pWhere,
122,616✔
7590
                                 int64_t notifyType) {
7591
  SNode* pNode = NULL;
122,616✔
7592
  CHECK_PARSER_STATUS(pCxt);
122,616✔
7593

7594
  if (LIST_LENGTH(pAddrUrls) == 0) {
122,616✔
7595
    pCxt->errCode =
×
UNCOV
7596
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "notification address cannot be empty");
×
UNCOV
7597
    goto _err;
×
7598
  }
7599

7600
  FOREACH(pNode, pAddrUrls) {
248,332✔
7601
    char* url = ((SValueNode*)pNode)->literal;
125,800✔
7602
    if (strlen(url) >= TSDB_STREAM_NOTIFY_URL_LEN) {
125,800✔
UNCOV
7603
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7604
                                              "notification address \"%s\" exceed maximum length %d", url,
7605
                                              TSDB_STREAM_NOTIFY_URL_LEN);
UNCOV
7606
      goto _err;
×
7607
    }
7608
    if (!validateNotifyUrl(url)) {
125,800✔
7609
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
84✔
7610
                                              "invalid notification address \"%s\"", url);
7611
      goto _err;
84✔
7612
    }
7613
  }
7614

7615
  SStreamNotifyOptions* pNotifyOptions = NULL;
122,532✔
7616
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_NOTIFY_OPTIONS, (SNode**)&pNotifyOptions);
122,532✔
7617
  CHECK_MAKE_NODE(pNotifyOptions);
122,532✔
7618
  pNotifyOptions->pAddrUrls = pAddrUrls;
122,532✔
7619
  pNotifyOptions->pWhere = pWhere;
122,532✔
7620
  pNotifyOptions->eventType = eventType;
122,532✔
7621
  pNotifyOptions->notifyType = notifyType;
122,532✔
7622
  return (SNode*)pNotifyOptions;
122,532✔
7623
_err:
84✔
7624
  nodesDestroyList(pAddrUrls);
84✔
7625
  return NULL;
84✔
7626
}
7627

7628
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pStream, SNode* pTrigger,
535,249✔
7629
                              SNode* pOutTable, SNode* pQuery) {
7630
  SCreateStreamStmt* pStmt = NULL;
535,249✔
7631
  CHECK_PARSER_STATUS(pCxt);
535,249✔
7632
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
534,813✔
7633
  CHECK_MAKE_NODE(pStmt);
534,813✔
7634

7635
  if (pOutTable && ((SStreamOutTableNode*)pOutTable)->pOutTable) {
534,813✔
7636
    tstrncpy(pStmt->targetDbName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.dbName,
527,471✔
7637
             TSDB_DB_NAME_LEN);
7638
    tstrncpy(pStmt->targetTabName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.tableName,
527,471✔
7639
             TSDB_TABLE_NAME_LEN);
7640
  }
7641

7642
  if (pStream) {
534,813✔
7643
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
534,813✔
7644
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
534,813✔
7645
  } else {
UNCOV
7646
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7647
    goto _err;
×
7648
  }
7649
  nodesDestroyNode(pStream);
534,813✔
7650

7651
  pStmt->ignoreExists = ignoreExists;
534,813✔
7652
  pStmt->pTrigger = pTrigger;
534,813✔
7653
  pStmt->pQuery = pQuery;
534,813✔
7654
  pStmt->pTags = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pTags : NULL;
534,813✔
7655
  pStmt->pSubtable = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pSubtable : NULL;
534,813✔
7656
  pStmt->pCols = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pCols : NULL;
534,813✔
7657
  pStmt->nodelayCreateSubtable = pOutTable ? ((SStreamOutTableNode*)pOutTable)->nodelayCreateSubtable : 0;
534,813✔
7658
  return (SNode*)pStmt;
534,813✔
7659
_err:
436✔
7660
  nodesDestroyNode(pOutTable);
436✔
7661
  nodesDestroyNode(pQuery);
436✔
7662
  nodesDestroyNode(pTrigger);
436✔
7663
  nodesDestroyNode(pQuery);
436✔
7664
  return NULL;
436✔
7665
}
7666

7667
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNodeList* pStreamList) {
21,368✔
7668
  CHECK_PARSER_STATUS(pCxt);
21,368✔
7669
  SDropStreamStmt* pStmt = NULL;
21,368✔
7670
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
21,368✔
7671
  CHECK_MAKE_NODE(pStmt);
21,368✔
7672

7673
  if (pStreamList) {
21,368✔
7674
    pStmt->pStreamList = pStreamList;
21,368✔
7675
  } else {
UNCOV
7676
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7677
    goto _err;
×
7678
  }
7679

7680
  pStmt->ignoreNotExists = ignoreNotExists;
21,368✔
7681
  return (SNode*)pStmt;
21,368✔
7682
_err:
×
7683
  nodesDestroyList(pStreamList);
×
UNCOV
7684
  return NULL;
×
7685
}
7686

7687
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pStream) {
3,243✔
7688
  CHECK_PARSER_STATUS(pCxt);
3,243✔
7689
  SPauseStreamStmt* pStmt = NULL;
3,243✔
7690
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
3,243✔
7691
  CHECK_MAKE_NODE(pStmt);
3,243✔
7692
  if (pStream) {
3,243✔
7693
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
3,243✔
7694
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
3,243✔
7695
  } else {
UNCOV
7696
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7697
    goto _err;
×
7698
  }
7699
  nodesDestroyNode(pStream);
3,243✔
7700
  pStmt->ignoreNotExists = ignoreNotExists;
3,243✔
7701
  return (SNode*)pStmt;
3,243✔
UNCOV
7702
_err:
×
UNCOV
7703
  return NULL;
×
7704
}
7705

7706
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated, SNode* pStream) {
2,857✔
7707
  CHECK_PARSER_STATUS(pCxt);
2,857✔
7708
  SResumeStreamStmt* pStmt = NULL;
2,857✔
7709
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
2,857✔
7710
  CHECK_MAKE_NODE(pStmt);
2,857✔
7711
  if (pStream) {
2,857✔
7712
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
2,857✔
7713
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
2,857✔
7714
  } else {
UNCOV
7715
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7716
    goto _err;
×
7717
  }
7718
  nodesDestroyNode(pStream);
2,857✔
7719
  pStmt->ignoreNotExists = ignoreNotExists;
2,857✔
7720
  pStmt->ignoreUntreated = ignoreUntreated;
2,857✔
7721
  return (SNode*)pStmt;
2,857✔
UNCOV
7722
_err:
×
UNCOV
7723
  return NULL;
×
7724
}
7725

7726
SNode* createRecalcStreamStmt(SAstCreateContext* pCxt, SNode* pStream, SNode* pRange) {
12,749✔
7727
  CHECK_PARSER_STATUS(pCxt);
12,749✔
7728
  SRecalcStreamStmt* pStmt = NULL;
12,749✔
7729
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RECALCULATE_STREAM_STMT, (SNode**)&pStmt);
12,749✔
7730
  CHECK_MAKE_NODE(pStmt);
12,749✔
7731
  if (pStream) {
12,749✔
7732
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
12,749✔
7733
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
12,749✔
7734
  } else {
UNCOV
7735
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
UNCOV
7736
    goto _err;
×
7737
  }
7738
  pStmt->pRange = pRange;
12,749✔
7739
  return (SNode*)pStmt;
12,749✔
UNCOV
7740
_err:
×
UNCOV
7741
  return NULL;
×
7742
}
7743

7744
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
2,718✔
7745
  CHECK_PARSER_STATUS(pCxt);
2,718✔
7746
  SKillStmt* pStmt = NULL;
2,718✔
7747
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,718✔
7748
  CHECK_MAKE_NODE(pStmt);
2,718✔
7749
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
2,718✔
7750
  return (SNode*)pStmt;
2,718✔
7751
_err:
×
7752
  return NULL;
×
7753
}
7754

7755
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
1,544✔
7756
  CHECK_PARSER_STATUS(pCxt);
1,544✔
7757
  SKillQueryStmt* pStmt = NULL;
1,544✔
7758
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
1,544✔
7759
  CHECK_MAKE_NODE(pStmt);
1,544✔
7760
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
1,544✔
7761
  return (SNode*)pStmt;
1,544✔
UNCOV
7762
_err:
×
UNCOV
7763
  return NULL;
×
7764
}
7765

7766
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
13,308✔
7767
  CHECK_PARSER_STATUS(pCxt);
13,308✔
7768
  SBalanceVgroupStmt* pStmt = NULL;
13,308✔
7769
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
13,308✔
7770
  CHECK_MAKE_NODE(pStmt);
13,308✔
7771
  return (SNode*)pStmt;
13,308✔
7772
_err:
×
UNCOV
7773
  return NULL;
×
7774
}
7775

7776
SNode* createAssignLeaderStmt(SAstCreateContext* pCxt) {
19✔
7777
  CHECK_PARSER_STATUS(pCxt);
19✔
7778
  SAssignLeaderStmt* pStmt = NULL;
19✔
7779
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ASSIGN_LEADER_STMT, (SNode**)&pStmt);
19✔
7780
  CHECK_MAKE_NODE(pStmt);
19✔
7781
  return (SNode*)pStmt;
19✔
UNCOV
7782
_err:
×
UNCOV
7783
  return NULL;
×
7784
}
7785

7786
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
5,136✔
7787
  CHECK_PARSER_STATUS(pCxt);
5,136✔
7788
  SBalanceVgroupLeaderStmt* pStmt = NULL;
5,136✔
7789
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
5,136✔
7790
  CHECK_MAKE_NODE(pStmt);
5,136✔
7791
  if (NULL != pVgId && NULL != pVgId->z) {
5,136✔
7792
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
91✔
7793
  }
7794
  return (SNode*)pStmt;
5,136✔
UNCOV
7795
_err:
×
UNCOV
7796
  return NULL;
×
7797
}
7798

7799
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
114✔
7800
  CHECK_PARSER_STATUS(pCxt);
114✔
7801
  SBalanceVgroupLeaderStmt* pStmt = NULL;
114✔
7802
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
114✔
7803
  CHECK_MAKE_NODE(pStmt);
114✔
7804
  if (NULL != pDbName) {
114✔
7805
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
114✔
7806
  }
7807
  return (SNode*)pStmt;
114✔
7808
_err:
×
7809
  return NULL;
×
7810
}
7811

7812
SNode* createSetVgroupKeepVersionStmt(SAstCreateContext* pCxt, const SToken* pVgId, const SToken* pKeepVersion) {
1,200✔
7813
  CHECK_PARSER_STATUS(pCxt);
1,200✔
7814
  SSetVgroupKeepVersionStmt* pStmt = NULL;
1,200✔
7815
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_VGROUP_KEEP_VERSION_STMT, (SNode**)&pStmt);
1,200✔
7816
  CHECK_MAKE_NODE(pStmt);
1,200✔
7817
  if (NULL != pVgId && NULL != pVgId->z) {
1,200✔
7818
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
1,200✔
7819
  }
7820
  if (NULL != pKeepVersion && NULL != pKeepVersion->z) {
1,200✔
7821
    pStmt->keepVersion = taosStr2Int64(pKeepVersion->z, NULL, 10);
1,200✔
7822
  }
7823
  return (SNode*)pStmt;
1,200✔
UNCOV
7824
_err:
×
UNCOV
7825
  return NULL;
×
7826
}
7827

UNCOV
7828
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
×
UNCOV
7829
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
7830
  SMergeVgroupStmt* pStmt = NULL;
×
7831
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
×
7832
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
7833
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
×
UNCOV
7834
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
×
UNCOV
7835
  return (SNode*)pStmt;
×
UNCOV
7836
_err:
×
UNCOV
7837
  return NULL;
×
7838
}
7839

7840
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
49,333✔
7841
  CHECK_PARSER_STATUS(pCxt);
49,333✔
7842
  SRedistributeVgroupStmt* pStmt = NULL;
49,333✔
7843
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
49,333✔
7844
  CHECK_MAKE_NODE(pStmt);
49,333✔
7845
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
49,333✔
7846
  pStmt->pDnodes = pDnodes;
49,333✔
7847
  return (SNode*)pStmt;
49,333✔
UNCOV
7848
_err:
×
UNCOV
7849
  nodesDestroyList(pDnodes);
×
UNCOV
7850
  return NULL;
×
7851
}
7852

7853
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, bool force) {
19,415✔
7854
  CHECK_PARSER_STATUS(pCxt);
19,415✔
7855
  SSplitVgroupStmt* pStmt = NULL;
19,415✔
7856
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
19,415✔
7857
  CHECK_MAKE_NODE(pStmt);
19,415✔
7858
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
19,415✔
7859
  pStmt->force = force;
19,415✔
7860
  return (SNode*)pStmt;
19,415✔
7861
_err:
×
UNCOV
7862
  return NULL;
×
7863
}
7864

7865
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
7866
  CHECK_PARSER_STATUS(pCxt);
×
7867
  SNode* pStmt = NULL;
×
7868
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
7869
  CHECK_MAKE_NODE(pStmt);
×
7870
  return pStmt;
×
7871
_err:
×
7872
  return NULL;
×
7873
}
7874

7875
SNode* createGrantStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
973,760✔
7876
                       SNode* pCond, int8_t optrType) {
7877
  CHECK_PARSER_STATUS(pCxt);
973,760✔
7878
  CHECK_NAME(checkRoleName(pCxt, pPrincipal, false));
973,760✔
7879
  SGrantStmt* pStmt = NULL;
973,760✔
7880
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
973,760✔
7881
  CHECK_MAKE_NODE(pStmt);
973,760✔
7882
  pStmt->optrType = optrType;
973,760✔
7883
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
973,760✔
7884
  switch (optrType) {
973,760✔
7885
    case TSDB_ALTER_ROLE_LOCK:
×
7886
      break;
×
7887
    case TSDB_ALTER_ROLE_PRIVILEGES: {
964,507✔
7888
      CHECK_NAME(checkObjName(pCxt, &pPrivLevel->first, false));
964,507✔
7889
      CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
964,507✔
7890
      pStmt->privileges = *(SPrivSetArgs*)resouces;
964,507✔
7891
      if(pStmt->privileges.nPrivArgs <= 0) {
964,507✔
UNCOV
7892
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Unknown privilege type");
×
UNCOV
7893
        goto _err;
×
7894
      }
7895
      if (TK_NK_NIL != pPrivLevel->first.type) {
964,507✔
7896
        COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
952,065✔
7897
      }
7898
      if (TK_NK_NIL != pPrivLevel->second.type) {
964,507✔
7899
        COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
638,837✔
7900
      }
7901
      pStmt->privileges.objType = pPrivLevel->objType;
964,507✔
7902
      pStmt->pCond = pCond;
964,507✔
7903
      break;
964,507✔
7904
    }
7905
    case TSDB_ALTER_ROLE_ROLE: {
9,253✔
7906
      SToken* pRole = (SToken*)resouces;
9,253✔
7907
      CHECK_NAME(checkRoleName(pCxt, pRole, false));
9,253✔
7908
      COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
9,253✔
7909
      break;
9,253✔
7910
    }
UNCOV
7911
    default:
×
UNCOV
7912
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported grant type");
×
UNCOV
7913
      goto _err;
×
7914
  }
7915
  return (SNode*)pStmt;
973,760✔
UNCOV
7916
_err:
×
UNCOV
7917
  nodesDestroyNode(pCond);
×
UNCOV
7918
  return NULL;
×
7919
}
7920

7921
SNode* createRevokeStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
513,546✔
7922
                        SNode* pCond, int8_t optrType) {
7923
  CHECK_PARSER_STATUS(pCxt);
513,546✔
7924
  CHECK_NAME(checkUserName(pCxt, pPrincipal));
513,546✔
7925
  SRevokeStmt* pStmt = NULL;
513,546✔
7926
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
513,546✔
7927
  CHECK_MAKE_NODE(pStmt);
513,546✔
7928
  pStmt->optrType = optrType;
513,546✔
7929
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
513,546✔
7930
  if (optrType == TSDB_ALTER_ROLE_PRIVILEGES) {
513,546✔
7931
    CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
505,057✔
7932
    CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
505,057✔
7933
    pStmt->privileges = *(SPrivSetArgs*)resouces;
505,057✔
7934
    if (TK_NK_NIL != pPrivLevel->first.type) {
505,057✔
7935
      COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
498,555✔
7936
    }
7937
    if (TK_NK_NIL != pPrivLevel->second.type) {
505,057✔
7938
      COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
447,327✔
7939
    }
7940
    pStmt->privileges.objType = pPrivLevel->objType;
505,057✔
7941
    pStmt->pCond = pCond;
505,057✔
7942
  } else if (optrType == TSDB_ALTER_ROLE_ROLE) {
8,489✔
7943
    SToken* pRole = (SToken*)resouces;
8,489✔
7944
    CHECK_NAME(checkRoleName(pCxt, pRole, false));
8,489✔
7945
    COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
8,489✔
7946
  } else {
7947
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported revoke type");
×
7948
    goto _err;
×
7949
  }
7950

7951
  return (SNode*)pStmt;
513,546✔
7952
_err:
×
7953
  nodesDestroyNode(pCond);
×
7954
  return NULL;
×
7955
}
7956

7957
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
5,925,621✔
7958
  SFunctionNode* pFunc = NULL;
5,925,621✔
7959
  CHECK_PARSER_STATUS(pCxt);
5,925,621✔
7960
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
5,925,621✔
7961
  CHECK_MAKE_NODE(pFunc);
5,925,607✔
7962
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
5,925,607✔
7963
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
5,925,607✔
7964
  CHECK_MAKE_NODE(pCol);
5,925,607✔
7965
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
5,925,607✔
7966
  CHECK_PARSER_STATUS(pCxt);
5,925,621✔
7967
  return (SNode*)pFunc;
5,925,621✔
UNCOV
7968
_err:
×
UNCOV
7969
  nodesDestroyNode((SNode*)pFunc);
×
UNCOV
7970
  return NULL;
×
7971
}
7972

7973
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
1,975,207✔
7974
  SDeleteStmt* pStmt = NULL;
1,975,207✔
7975
  CHECK_PARSER_STATUS(pCxt);
1,975,207✔
7976
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
1,975,207✔
7977
  CHECK_MAKE_NODE(pStmt);
1,975,207✔
7978
  pStmt->pFromTable = pTable;
1,975,207✔
7979
  pStmt->pWhere = pWhere;
1,975,207✔
7980
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
1,975,207✔
7981
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
1,975,207✔
7982
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
1,975,207✔
7983
  CHECK_MAKE_NODE(pStmt->pCountFunc);
1,975,207✔
7984
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
1,975,207✔
7985
  CHECK_MAKE_NODE(pStmt->pLastFunc);
1,975,207✔
7986
  pStmt->secureDelete = 0;
1,975,207✔
7987
  return (SNode*)pStmt;
1,975,207✔
7988
_err:
×
7989
  nodesDestroyNode((SNode*)pStmt);
×
7990
  nodesDestroyNode(pTable);
×
UNCOV
7991
  nodesDestroyNode(pWhere);
×
UNCOV
7992
  return NULL;
×
7993
}
7994

7995
SNode* createSecureDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
2,207✔
7996
  SNode* pNode = createDeleteStmt(pCxt, pTable, pWhere);
2,207✔
7997
  if (pNode) {
2,207✔
7998
    ((SDeleteStmt*)pNode)->secureDelete = 1;
2,207✔
7999
  }
8000
  return pNode;
2,207✔
8001
}
8002

8003
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
531,130✔
8004
  CHECK_PARSER_STATUS(pCxt);
531,130✔
8005
  SInsertStmt* pStmt = NULL;
531,130✔
8006
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
531,130✔
8007
  CHECK_MAKE_NODE(pStmt);
531,130✔
8008
  pStmt->pTable = pTable;
531,130✔
8009
  pStmt->pCols = pCols;
531,130✔
8010
  pStmt->pQuery = pQuery;
531,130✔
8011
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
531,130✔
8012
    tstrncpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
530,934✔
8013
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
196✔
8014
    tstrncpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
196✔
8015
  }
8016
  return (SNode*)pStmt;
531,130✔
UNCOV
8017
_err:
×
UNCOV
8018
  nodesDestroyNode(pTable);
×
UNCOV
8019
  nodesDestroyNode(pQuery);
×
UNCOV
8020
  nodesDestroyList(pCols);
×
UNCOV
8021
  return NULL;
×
8022
}
8023

8024
SNode* createCreateRsmaStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* rsmaName, SNode* pRealTable,
119,804✔
8025
                            SNodeList* pFuncs, SNodeList* pIntervals) {
8026
  SCreateRsmaStmt* pStmt = NULL;
119,804✔
8027

8028
  CHECK_PARSER_STATUS(pCxt);
119,804✔
8029
  CHECK_NAME(checkRsmaName(pCxt, rsmaName));
119,804✔
8030
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_RSMA_STMT, (SNode**)&pStmt);
119,804✔
8031
  CHECK_MAKE_NODE(pStmt);
119,804✔
8032

8033
  pStmt->ignoreExists = ignoreExists;
119,804✔
8034
  pStmt->pFuncs = pFuncs;
119,804✔
8035
  pStmt->pIntervals = pIntervals;
119,804✔
8036
  COPY_STRING_FORM_ID_TOKEN(pStmt->rsmaName, rsmaName);
119,804✔
8037

8038
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
119,804✔
8039
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
119,804✔
8040
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
119,804✔
8041
  nodesDestroyNode(pRealTable);
119,804✔
8042

8043
  return (SNode*)pStmt;
119,804✔
UNCOV
8044
_err:
×
UNCOV
8045
  nodesDestroyNode((SNode*)pStmt);
×
UNCOV
8046
  nodesDestroyList(pFuncs);
×
UNCOV
8047
  nodesDestroyNode(pRealTable);
×
UNCOV
8048
  nodesDestroyList(pIntervals);
×
UNCOV
8049
  return NULL;
×
8050
}
8051

8052
SNode* createDropRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
10,922✔
8053
  CHECK_PARSER_STATUS(pCxt);
10,922✔
8054
  SDropRsmaStmt* pStmt = NULL;
10,922✔
8055
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_RSMA_STMT, (SNode**)&pStmt);
10,922✔
8056
  CHECK_MAKE_NODE(pStmt);
10,922✔
8057

8058
  pStmt->ignoreNotExists = ignoreNotExists;
10,922✔
8059
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
10,922✔
8060

8061
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
10,922✔
8062
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
10,922✔
8063

8064
  nodesDestroyNode(pRealTable);
10,922✔
8065
  return (SNode*)pStmt;
10,922✔
UNCOV
8066
_err:
×
UNCOV
8067
  nodesDestroyNode(pRealTable);
×
UNCOV
8068
  return NULL;
×
8069
}
8070

8071
SNode* createAlterRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRsma, int8_t alterType,
22,308✔
8072
                           void* alterInfo) {
8073
  CHECK_PARSER_STATUS(pCxt);
22,308✔
8074
  SAlterRsmaStmt* pStmt = NULL;
22,308✔
8075
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_RSMA_STMT, (SNode**)&pStmt);
22,308✔
8076
  CHECK_MAKE_NODE(pStmt);
22,308✔
8077
  pStmt->ignoreNotExists = ignoreNotExists;
22,308✔
8078
  SRealTableNode* pTableNode = (SRealTableNode*)pRsma;
22,308✔
8079

8080
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
22,308✔
8081
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
22,308✔
8082
  nodesDestroyNode(pRsma);
22,308✔
8083

8084
  pStmt->alterType = alterType;
22,308✔
8085
  switch (alterType) {
22,308✔
8086
    case TSDB_ALTER_RSMA_FUNCTION: {
22,308✔
8087
      pStmt->pFuncs = (SNodeList*)alterInfo;
22,308✔
8088
      break;
22,308✔
8089
    }
UNCOV
8090
    default:
×
UNCOV
8091
      break;
×
8092
  }
8093
  return (SNode*)pStmt;
22,308✔
UNCOV
8094
_err:
×
UNCOV
8095
  return NULL;
×
8096
}
8097

8098
SNode* createShowCreateRsmaStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
3,511✔
8099
  CHECK_PARSER_STATUS(pCxt);
3,511✔
8100
  SShowCreateRsmaStmt* pStmt = NULL;
3,511✔
8101
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,511✔
8102
  CHECK_MAKE_NODE(pStmt);
3,511✔
8103
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
3,511✔
8104
  tstrncpy(pStmt->rsmaName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->rsmaName));
3,511✔
8105
  nodesDestroyNode(pRealTable);
3,511✔
8106
  return (SNode*)pStmt;
3,511✔
UNCOV
8107
_err:
×
UNCOV
8108
  nodesDestroyNode(pRealTable);
×
UNCOV
8109
  return NULL;
×
8110
}
8111

8112
SNode* createRollupStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
10,596✔
8113
  CHECK_PARSER_STATUS(pCxt);
10,596✔
8114
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
10,596✔
8115
  SRollupDatabaseStmt* pStmt = NULL;
10,596✔
8116
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_DATABASE_STMT, (SNode**)&pStmt);
10,596✔
8117
  CHECK_MAKE_NODE(pStmt);
10,596✔
8118
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
10,596✔
8119
  pStmt->pStart = pStart;
10,596✔
8120
  pStmt->pEnd = pEnd;
10,596✔
8121
  return (SNode*)pStmt;
10,596✔
UNCOV
8122
_err:
×
UNCOV
8123
  nodesDestroyNode(pStart);
×
UNCOV
8124
  nodesDestroyNode(pEnd);
×
UNCOV
8125
  return NULL;
×
8126
}
8127

8128
SNode* createRollupVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
4,740✔
8129
                                SNode* pEnd) {
8130
  CHECK_PARSER_STATUS(pCxt);
4,740✔
8131
  if (NULL == pDbName) {
4,740✔
UNCOV
8132
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
UNCOV
8133
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
UNCOV
8134
    CHECK_PARSER_STATUS(pCxt);
×
8135
  }
8136
  SRollupVgroupsStmt* pStmt = NULL;
4,740✔
8137
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_VGROUPS_STMT, (SNode**)&pStmt);
4,740✔
8138
  CHECK_MAKE_NODE(pStmt);
4,740✔
8139
  pStmt->pDbName = pDbName;
4,740✔
8140
  pStmt->vgidList = vgidList;
4,740✔
8141
  pStmt->pStart = pStart;
4,740✔
8142
  pStmt->pEnd = pEnd;
4,740✔
8143
  return (SNode*)pStmt;
4,740✔
8144
_err:
×
8145
  nodesDestroyNode(pDbName);
×
UNCOV
8146
  nodesDestroyList(vgidList);
×
UNCOV
8147
  nodesDestroyNode(pStart);
×
UNCOV
8148
  nodesDestroyNode(pEnd);
×
UNCOV
8149
  return NULL;
×
8150
}
8151

8152
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
9,622✔
8153
                            SNode* pRealTable, SNode* pInterval) {
8154
  SCreateTSMAStmt* pStmt = NULL;
9,622✔
8155

8156
  CHECK_PARSER_STATUS(pCxt);
9,622✔
8157
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
9,622✔
8158
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
9,308✔
8159
  CHECK_MAKE_NODE(pStmt);
9,308✔
8160

8161
  pStmt->ignoreExists = ignoreExists;
9,308✔
8162
  if (!pOptions) {
9,308✔
8163
    // recursive tsma
8164
    pStmt->pOptions = NULL;
1,413✔
8165
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
1,413✔
8166
    CHECK_MAKE_NODE(pStmt->pOptions);
1,413✔
8167
    pStmt->pOptions->recursiveTsma = true;
1,413✔
8168
  } else {
8169
    pStmt->pOptions = (STSMAOptions*)pOptions;
7,895✔
8170
  }
8171
  pStmt->pOptions->pInterval = pInterval;
9,308✔
8172
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
9,308✔
8173

8174
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
9,308✔
8175
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
9,308✔
8176
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
9,308✔
8177
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
9,308✔
8178
  nodesDestroyNode(pRealTable);
9,308✔
8179

8180
  return (SNode*)pStmt;
9,308✔
8181
_err:
314✔
8182
  nodesDestroyNode((SNode*)pStmt);
314✔
8183
  nodesDestroyNode(pOptions);
314✔
8184
  nodesDestroyNode(pRealTable);
314✔
8185
  nodesDestroyNode(pInterval);
314✔
8186
  return NULL;
314✔
8187
}
8188

8189
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
8,994✔
8190
  CHECK_PARSER_STATUS(pCxt);
8,994✔
8191
  STSMAOptions* pOptions = NULL;
8,994✔
8192
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
8,994✔
8193
  CHECK_MAKE_NODE(pOptions);
8,994✔
8194
  pOptions->pFuncs = pFuncs;
8,994✔
8195
  return (SNode*)pOptions;
8,994✔
UNCOV
8196
_err:
×
UNCOV
8197
  nodesDestroyList(pFuncs);
×
UNCOV
8198
  return NULL;
×
8199
}
8200

UNCOV
8201
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
UNCOV
8202
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
8203
  STSMAOptions* pOptions = NULL;
×
UNCOV
8204
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
UNCOV
8205
  CHECK_MAKE_NODE(pOptions);
×
UNCOV
8206
  return (SNode*)pOptions;
×
UNCOV
8207
_err:
×
UNCOV
8208
  return NULL;
×
8209
}
8210

8211
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
4,414✔
8212
  CHECK_PARSER_STATUS(pCxt);
4,414✔
8213
  SDropTSMAStmt* pStmt = NULL;
4,414✔
8214
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
4,414✔
8215
  CHECK_MAKE_NODE(pStmt);
4,414✔
8216

8217
  pStmt->ignoreNotExists = ignoreNotExists;
4,414✔
8218
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
4,414✔
8219

8220
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
4,414✔
8221
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
4,414✔
8222

8223
  nodesDestroyNode(pRealTable);
4,414✔
8224
  return (SNode*)pStmt;
4,414✔
UNCOV
8225
_err:
×
UNCOV
8226
  nodesDestroyNode(pRealTable);
×
UNCOV
8227
  return NULL;
×
8228
}
8229

8230
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
163✔
8231
  CHECK_PARSER_STATUS(pCxt);
163✔
8232

8233
  SShowStmt* pStmt = NULL;
163✔
8234
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
163✔
8235
  CHECK_MAKE_NODE(pStmt);
163✔
8236

8237
  pStmt->pDbName = dbName;
163✔
8238
  return (SNode*)pStmt;
163✔
8239
_err:
×
8240
  nodesDestroyNode(dbName);
×
8241
  return NULL;
×
8242
}
8243
SNode* createShowDiskUsageStmt(SAstCreateContext* pCxt, SNode* dbName, ENodeType type) {
364✔
8244
  CHECK_PARSER_STATUS(pCxt);
364✔
8245
  if (NULL == dbName) {
364✔
UNCOV
8246
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
UNCOV
8247
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
UNCOV
8248
    CHECK_PARSER_STATUS(pCxt);
×
8249
  }
8250

8251
  SShowStmt* pStmt = NULL;
364✔
8252
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
364✔
8253
  CHECK_MAKE_NODE(pStmt);
364✔
8254

8255
  pStmt->pDbName = dbName;
364✔
8256
  return (SNode*)pStmt;
364✔
UNCOV
8257
_err:
×
UNCOV
8258
  nodesDestroyNode(dbName);
×
UNCOV
8259
  return NULL;
×
8260
}
8261

8262
SNode* createShowStreamsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNode* pLike, ENodeType type) {
127,111✔
8263
  CHECK_PARSER_STATUS(pCxt);
127,111✔
8264

8265
  SShowStmt* pStmt = NULL;
127,111✔
8266
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
127,111✔
8267
  CHECK_MAKE_NODE(pStmt);
127,111✔
8268
  pStmt->withFull = false;
127,111✔
8269
  pStmt->pDbName = pDbName;
127,111✔
8270
  pStmt->pTbName = pLike;
127,111✔
8271
  pStmt->tableCondType = OP_TYPE_LIKE;
127,111✔
8272

8273
  return (SNode*)pStmt;
127,111✔
8274

8275
_err:
×
8276
  nodesDestroyNode(pDbName);
×
8277
  nodesDestroyNode(pLike);
×
UNCOV
8278
  return NULL;
×
8279
}
8280

8281
SNode* createScanStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
1,090✔
8282
  CHECK_PARSER_STATUS(pCxt);
1,090✔
8283
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,090✔
8284
  SScanDatabaseStmt* pStmt = NULL;
1,090✔
8285
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_DATABASE_STMT, (SNode**)&pStmt);
1,090✔
8286
  CHECK_MAKE_NODE(pStmt);
1,090✔
8287
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,090✔
8288
  pStmt->pStart = pStart;
1,090✔
8289
  pStmt->pEnd = pEnd;
1,090✔
8290
  return (SNode*)pStmt;
1,090✔
UNCOV
8291
_err:
×
UNCOV
8292
  nodesDestroyNode(pStart);
×
8293
  nodesDestroyNode(pEnd);
×
8294
  return NULL;
×
8295
}
8296

8297
SNode* createScanVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart, SNode* pEnd) {
336✔
8298
  CHECK_PARSER_STATUS(pCxt);
336✔
8299
  if (NULL == pDbName) {
336✔
8300
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
56✔
8301
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
56✔
8302
    CHECK_PARSER_STATUS(pCxt);
56✔
8303
  }
8304
  SScanVgroupsStmt* pStmt = NULL;
280✔
8305
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_VGROUPS_STMT, (SNode**)&pStmt);
280✔
8306
  CHECK_MAKE_NODE(pStmt);
280✔
8307
  pStmt->pDbName = pDbName;
280✔
8308
  pStmt->vgidList = vgidList;
280✔
8309
  pStmt->pStart = pStart;
280✔
8310
  pStmt->pEnd = pEnd;
280✔
8311
  return (SNode*)pStmt;
280✔
8312
_err:
56✔
8313
  nodesDestroyNode(pDbName);
56✔
8314
  nodesDestroyList(vgidList);
56✔
8315
  nodesDestroyNode(pStart);
56✔
8316
  nodesDestroyNode(pEnd);
56✔
8317
  return NULL;
56✔
8318
}
8319

8320
SNode* createShowScansStmt(SAstCreateContext* pCxt, ENodeType type) {
2,464✔
8321
  CHECK_PARSER_STATUS(pCxt);
2,464✔
8322
  SShowScansStmt* pStmt = NULL;
2,464✔
8323
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,464✔
8324
  CHECK_MAKE_NODE(pStmt);
2,464✔
8325
  return (SNode*)pStmt;
2,464✔
UNCOV
8326
_err:
×
8327
  return NULL;
×
8328
}
8329

8330
SNode* createShowScanDetailsStmt(SAstCreateContext* pCxt, SNode* pScanIdNode) {
1,904✔
8331
  CHECK_PARSER_STATUS(pCxt);
1,904✔
8332
  SShowScanDetailsStmt* pStmt = NULL;
1,904✔
8333
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_SCAN_DETAILS_STMT, (SNode**)&pStmt);
1,904✔
8334
  CHECK_MAKE_NODE(pStmt);
1,904✔
8335
  pStmt->pScanId = pScanIdNode;
1,904✔
8336
  return (SNode*)pStmt;
1,904✔
UNCOV
8337
_err:
×
UNCOV
8338
  nodesDestroyNode(pScanIdNode);
×
UNCOV
8339
  return NULL;
×
8340
}
8341

UNCOV
8342
SNode* createAlterAllDnodeTLSStmt(SAstCreateContext* pCxt, SToken* alterName) {
×
UNCOV
8343
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
8344
  SAlterDnodeStmt* pStmt = NULL;
×
8345
  static char*     tls = "TLS";
UNCOV
8346
  if (NULL == alterName || alterName->n <= 0) {
×
UNCOV
8347
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter name is empty");
×
UNCOV
8348
    goto _err;
×
8349
  }
8350

UNCOV
8351
  if (alterName->n == strlen(tls) && taosStrncasecmp(alterName->z, tls, alterName->n) == 0) {
×
UNCOV
8352
    pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODES_RELOAD_TLS_STMT, (SNode**)&pStmt);
×
8353

UNCOV
8354
    memcpy(pStmt->config, "reload", strlen("reload"));
×
UNCOV
8355
    memcpy(pStmt->value, "tls", strlen("tls"));
×
UNCOV
8356
    pStmt->dnodeId = -1;
×
8357
  } else {
UNCOV
8358
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter is not supported");
×
UNCOV
8359
    goto _err;
×
8360
  }
8361

8362
  CHECK_MAKE_NODE(pStmt);
×
8363

UNCOV
8364
  return (SNode*)pStmt;
×
UNCOV
8365
_err:
×
UNCOV
8366
  return NULL;
×
8367
}
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