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

taosdata / TDengine / #4933

20 Jan 2026 10:44AM UTC coverage: 66.671% (+0.03%) from 66.646%
#4933

push

travis-ci

web-flow
merge: from main to 3.0 #34340

73 of 178 new or added lines in 9 files covered. (41.01%)

1199 existing lines in 124 files now uncovered.

203121 of 304663 relevant lines covered (66.67%)

132228377.94 hits per line

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

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

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

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

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

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

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

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

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

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

98
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
329,793,277✔
99
  memset(pCxt, 0, sizeof(SAstCreateContext));
329,793,277✔
100
  pCxt->pQueryCxt = pParseCxt;
329,793,277✔
101
  pCxt->msgBuf.buf = pParseCxt->pMsg;
329,787,445✔
102
  pCxt->msgBuf.len = pParseCxt->msgLen;
329,796,391✔
103
  pCxt->notSupport = false;
329,791,207✔
104
  pCxt->pRootNode = NULL;
329,792,132✔
105
  pCxt->placeholderNo = 0;
329,796,733✔
106
  pCxt->pPlaceholderValues = NULL;
329,791,904✔
107
  pCxt->errCode = TSDB_CODE_SUCCESS;
329,786,568✔
108
}
329,791,395✔
109

110
static void trimEscape(SAstCreateContext* pCxt, SToken* pName, bool trimStar) {
2,147,483,647✔
111
  // todo need to deal with `ioo``ii` -> ioo`ii: done
112
  if (NULL != pName && pName->n > 1 && TS_ESCAPE_CHAR == pName->z[0]) {
2,147,483,647✔
113
    if (!pCxt->pQueryCxt->hasDupQuoteChar) {
12,091,533✔
114
      pName->z += 1;
11,989,347✔
115
      pName->n -= 2;
11,989,225✔
116
      // * is forbidden as an identifier name
117
      if (pName->z[0] == '*' && trimStar && pName->n == 1) {
11,989,441✔
118
        pName->z[0] = '\0';
×
119
        pName->n = 0;
×
120
      }
121
    } else {
122
      int32_t i = 1, j = 0;
102,309✔
123
      for (; i < pName->n - 1; ++i) {
626,070✔
124
        if ((pName->z[i] == TS_ESCAPE_CHAR) && (pName->z[i + 1] == TS_ESCAPE_CHAR)) {
523,761✔
125
          pName->z[j++] = TS_ESCAPE_CHAR;
246,865✔
126
          ++i;
246,865✔
127
        } else {
128
          pName->z[j++] = pName->z[i];
276,896✔
129
        }
130
      }
131
      pName->n = j;
102,309✔
132
    }
133
  }
134
}
2,147,483,647✔
135

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

150

151

152
static bool isValidSimplePassword(const char* password) {
495✔
153
  for (char c = *password; c != 0; c = *(++password)) {
45,210✔
154
    if (c == ' ' || c == '\'' || c == '\"' || c == '`' || c == '\\') {
44,715✔
155
      return false;
×
156
    }
157
  }
158
  return true;
495✔
159
}
160

161

162

163
static bool isValidPassword(SAstCreateContext* pCxt, const char* password, bool imported) {
99,883✔
164
  if (imported) {
99,883✔
165
    return strlen(password) == TSDB_PASSWORD_LEN;
×
166
  }
167

168
  if (tsEnableStrongPassword) {
99,883✔
169
    return taosIsComplexString(password);
99,388✔
170
  }
171

172
  return isValidSimplePassword(password);
495✔
173
}
174

175

176

177
static int32_t parsePort(SAstCreateContext* pCxt, const char* p, int32_t* pPort) {
151,069✔
178
  *pPort = taosStr2Int32(p, NULL, 10);
151,069✔
179
  if (*pPort >= UINT16_MAX || *pPort <= 0) {
151,069✔
180
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
×
181
  }
182
  return TSDB_CODE_SUCCESS;
151,069✔
183
}
184

185
static int32_t parseEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
151,069✔
186
  if (pEp->n >= (NULL == pPort ? (TSDB_FQDN_LEN + 1 + 5) : TSDB_FQDN_LEN)) {  // format 'fqdn:port' or 'fqdn'
151,069✔
187
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
188
  }
189

190
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
151,069✔
191
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
151,069✔
192
  (void)strdequote(ep);
151,069✔
193
  (void)strtrim(ep);
151,069✔
194
  if (NULL == pPort) {
151,069✔
195
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
150,244✔
196
    return TSDB_CODE_SUCCESS;
150,244✔
197
  }
198
  char* pColon = strrchr(ep, ':');
825✔
199
  if (NULL == pColon) {
825✔
200
    *pPort = tsServerPort;
×
201
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
×
202
    return TSDB_CODE_SUCCESS;
×
203
  }
204
  strncpy(pFqdn, ep, pColon - ep);
825✔
205
  return parsePort(pCxt, pColon + 1, pPort);
825✔
206
}
207

208
static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, const SToken* pPortToken, char* pFqdn,
151,069✔
209
                                  int32_t* pPort) {
210
  if (NULL == pEp) {
151,069✔
211
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
212
    return false;
×
213
  }
214

215
  if (NULL != pPortToken) {
151,069✔
216
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
150,244✔
217
  }
218

219
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
151,069✔
220
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
151,069✔
221
  }
222

223
  return TSDB_CODE_SUCCESS == pCxt->errCode;
151,069✔
224
}
225

226
static bool checkObjName(SAstCreateContext* pCxt, SToken* pObjName, bool need) {
567,940✔
227
  if (NULL == pObjName || TK_NK_NIL == pObjName->type) {
567,940✔
228
    if (need) {
989✔
229
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME);
×
230
    }
231
  } else {
232
    trimEscape(pCxt, pObjName, true);
566,951✔
233
    if (pObjName->n >= TSDB_OBJ_NAME_LEN || pObjName->n == 0) {
566,951✔
234
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pObjName->z);
×
235
    }
236
  }
237
  return TSDB_CODE_SUCCESS == pCxt->errCode;
567,940✔
238
}
239

240
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
848,928,188✔
241
  if (NULL == pDbName) {
848,928,188✔
242
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
676,799,375✔
243
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
2,085✔
244
    }
245
  } else {
246
    trimEscape(pCxt, pDbName, true);
172,128,813✔
247
    if (pDbName->n >= TSDB_DB_NAME_LEN || (demandDb && (pDbName->n == 0))) {
172,131,780✔
248
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
1,532✔
249
    }
250
  }
251
  return TSDB_CODE_SUCCESS == pCxt->errCode;
848,933,646✔
252
}
253

254
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
2,147,483,647✔
255
  trimEscape(pCxt, pTableName, true);
2,147,483,647✔
256
  if (NULL != pTableName && pTableName->type != TK_NK_NIL &&
2,147,483,647✔
257
      (pTableName->n >= TSDB_TABLE_NAME_LEN || pTableName->n == 0)) {
1,361,720,194✔
258
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
9,171✔
259
    return false;
3,878✔
260
  }
261
  return true;
2,147,483,647✔
262
}
263

264
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
2,064,600,209✔
265
  trimEscape(pCxt, pColumnName, true);
2,064,600,209✔
266
  if (NULL != pColumnName && pColumnName->type != TK_NK_NIL &&
2,064,599,620✔
267
      (pColumnName->n >= TSDB_COL_NAME_LEN || pColumnName->n == 0)) {
2,064,601,607✔
268
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
3,875✔
269
    return false;
3,942✔
270
  }
271
  return true;
2,064,597,045✔
272
}
273

274
static bool checkIndexName(SAstCreateContext* pCxt, SToken* pIndexName) {
3,318✔
275
  trimEscape(pCxt, pIndexName, true);
3,318✔
276
  if (NULL != pIndexName && (pIndexName->n >= TSDB_INDEX_NAME_LEN || pIndexName->n == 0)) {
3,318✔
277
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pIndexName->z);
×
278
    return false;
×
279
  }
280
  return true;
3,318✔
281
}
282

283
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
278,707✔
284
  trimEscape(pCxt, pTopicName, true);
278,707✔
285
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN || pTopicName->n == 0) {
278,707✔
286
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
710✔
287
    return false;
710✔
288
  }
289
  return true;
277,997✔
290
}
291

292
static bool checkCGroupName(SAstCreateContext* pCxt, SToken* pCGroup) {
829✔
293
  trimEscape(pCxt, pCGroup, true);
829✔
294
  if (pCGroup->n >= TSDB_CGROUP_LEN || pCGroup->n == 0) {
829✔
295
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pCGroup->z);
×
296
    return false;
×
297
  }
298
  return true;
829✔
299
}
300

301
static bool checkViewName(SAstCreateContext* pCxt, SToken* pViewName) {
18,572✔
302
  trimEscape(pCxt, pViewName, true);
18,572✔
303
  if (pViewName->n >= TSDB_VIEW_NAME_LEN || pViewName->n == 0) {
18,572✔
304
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pViewName->z);
366✔
305
    return false;
366✔
306
  }
307
  return true;
18,206✔
308
}
309

310
static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
311,286✔
311
  trimEscape(pCxt, pStreamName, true);
311,286✔
312
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN || pStreamName->n == 0) {
311,286✔
313
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
183✔
314
    return false;
183✔
315
  }
316
  return true;
311,103✔
317
}
318

319
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
48,970✔
320
  if (NULL == pCommentToken) {
48,970✔
321
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
×
322
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
48,970✔
323
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
8,255✔
324
  }
325
  return TSDB_CODE_SUCCESS == pCxt->errCode;
48,970✔
326
}
327

328
static bool checkRsmaName(SAstCreateContext* pCxt, SToken* pRsmaToken) {
110,409✔
329
  trimEscape(pCxt, pRsmaToken, true);
110,409✔
330
  if (NULL == pRsmaToken) {
110,409✔
331
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
332
  } else if (pRsmaToken->n >= TSDB_TABLE_NAME_LEN) {
110,409✔
333
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
×
334
  } else if (pRsmaToken->n == 0) {
110,409✔
335
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pRsmaToken->z);
×
336
  }
337
  return pCxt->errCode == TSDB_CODE_SUCCESS;
110,409✔
338
}
339

340
static bool checkTsmaName(SAstCreateContext* pCxt, SToken* pTsmaToken) {
806✔
341
  trimEscape(pCxt, pTsmaToken, true);
806✔
342
  if (NULL == pTsmaToken) {
806✔
343
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
344
  } else if (pTsmaToken->n >= TSDB_TABLE_NAME_LEN - strlen(TSMA_RES_STB_POSTFIX)) {
806✔
345
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
×
346
  } else if (pTsmaToken->n == 0) {
806✔
347
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTsmaToken->z);
×
348
  }
349
  return pCxt->errCode == TSDB_CODE_SUCCESS;
806✔
350
}
351

352
static bool checkMountPath(SAstCreateContext* pCxt, SToken* pMountPath) {
1,386✔
353
  trimEscape(pCxt, pMountPath, true);
1,386✔
354
  if (pMountPath->n >= TSDB_MOUNT_PATH_LEN || pMountPath->n == 0) {
1,386✔
355
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pMountPath->z);
×
356
    return false;
×
357
  }
358
  return true;
1,386✔
359
}
360

361
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
1,729,894,337✔
362
  CHECK_PARSER_STATUS(pCxt);
1,729,894,337✔
363
  SRawExprNode* target = NULL;
1,729,889,873✔
364
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
1,729,890,248✔
365
  CHECK_MAKE_NODE(target);
1,729,899,095✔
366
  target->p = pToken->z;
1,729,899,095✔
367
  target->n = pToken->n;
1,729,896,711✔
368
  target->pNode = pNode;
1,729,895,970✔
369
  return (SNode*)target;
1,729,895,378✔
370
_err:
5,164✔
371
  nodesDestroyNode(pNode);
5,164✔
372
  return NULL;
5,164✔
373
}
374

375
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
1,940,579,214✔
376
  CHECK_PARSER_STATUS(pCxt);
1,940,579,214✔
377
  SRawExprNode* target = NULL;
1,940,578,500✔
378
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
1,940,578,410✔
379
  CHECK_MAKE_NODE(target);
1,940,581,907✔
380
  target->p = pStart->z;
1,940,581,907✔
381
  target->n = (pEnd->z + pEnd->n) - pStart->z;
1,940,578,927✔
382
  target->pNode = pNode;
1,940,579,399✔
383
  return (SNode*)target;
1,940,579,290✔
384
_err:
×
385
  nodesDestroyNode(pNode);
×
386
  return NULL;
×
387
}
388

389
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
92,199,970✔
390
  CHECK_PARSER_STATUS(pCxt);
92,199,970✔
391
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
92,200,352✔
392
    return pNode;
×
393
  }
394
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
92,200,352✔
395
  return pNode;
92,198,488✔
396
_err:
×
397
  nodesDestroyNode(pNode);
×
398
  return NULL;
×
399
}
400

401
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
2,147,483,647✔
402
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
403
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,147,483,647✔
404
  SNode*        pRealizedExpr = pRawExpr->pNode;
2,147,483,647✔
405
  if (nodesIsExprNode(pRealizedExpr)) {
2,147,483,647✔
406
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
2,147,483,647✔
407
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
2,147,483,647✔
408
      tstrncpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
1,320,650,855✔
409
      tstrncpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
1,320,652,925✔
410
    } else if (pRawExpr->isPseudoColumn) {
2,147,483,647✔
411
      // all pseudo column are translate to function with same name
412
      tstrncpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
82,127,487✔
413
      if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_column") == 0) {
82,125,996✔
414
        SValueNode* pColId = (SValueNode*)nodesListGetNode(((SFunctionNode*)pExpr)->pParameterList, 0);
35,683✔
415
        snprintf(pExpr->userAlias, sizeof(pExpr->userAlias), "%%%%%s", pColId->literal);
35,683✔
416
      } else if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_tbname") == 0) {
82,092,290✔
417
        tstrncpy(pExpr->userAlias, "%%tbname", TSDB_COL_NAME_LEN);
33,554✔
418
      } else {
419
        tstrncpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
82,053,508✔
420
      }
421
    } else {
422
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
2,147,483,647✔
423

424
      // See TS-3398.
425
      // Len of pRawExpr->p could be larger than len of aliasName[TSDB_COL_NAME_LEN].
426
      // If aliasName is truncated, hash value of aliasName could be the same.
427
      uint64_t hashVal = MurmurHash3_64(pRawExpr->p, pRawExpr->n);
2,147,483,647✔
428
      snprintf(pExpr->aliasName, TSDB_COL_NAME_LEN, "%" PRIu64, hashVal);
2,147,483,647✔
429
      strncpy(pExpr->userAlias, pRawExpr->p, len);
2,147,483,647✔
430
      pExpr->userAlias[len] = 0;
2,147,483,647✔
431
    }
432
  }
433
  pRawExpr->pNode = NULL;
2,147,483,647✔
434
  nodesDestroyNode(pNode);
2,147,483,647✔
435
  return pRealizedExpr;
2,147,483,647✔
436
_err:
×
437
  nodesDestroyNode(pNode);
×
438
  return NULL;
×
439
}
440

441
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
1,137,312,402✔
442
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
1,137,312,402✔
443
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1,157✔
444
    return nil_token;
×
445
  }
446
  SRawExprNode* target = (SRawExprNode*)pNode;
1,137,312,107✔
447
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
1,137,312,107✔
448
  return t;
1,137,315,846✔
449
}
450

451
SNodeList* createColsFuncParamNodeList(SAstCreateContext* pCxt, SNode* pNode, SNodeList* pNodeList, SToken* pAlias) {
1,042,565✔
452
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
1,042,565✔
453
  SNode*        pFuncNode = pRawExpr->pNode;
1,042,565✔
454
  CHECK_PARSER_STATUS(pCxt);
1,042,565✔
455
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
1,042,565✔
456
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
457
  }
458
  CHECK_PARSER_STATUS(pCxt);
1,042,565✔
459
  if (pFuncNode->type != QUERY_NODE_FUNCTION) {
1,042,565✔
460
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
461
  }
462
  CHECK_PARSER_STATUS(pCxt);
1,042,565✔
463
  SNodeList* list = NULL;
1,042,565✔
464
  pCxt->errCode = nodesMakeList(&list);
1,042,565✔
465
  CHECK_MAKE_NODE(list);
1,042,565✔
466
  pCxt->errCode = nodesListAppend(list, pFuncNode);
1,042,565✔
467
  CHECK_PARSER_STATUS(pCxt);
1,042,565✔
468
  pCxt->errCode = nodesListAppendList(list, pNodeList);
1,042,565✔
469
  CHECK_PARSER_STATUS(pCxt);
1,042,565✔
470
  return list;
1,042,565✔
471

472
_err:
×
473
  nodesDestroyNode(pFuncNode);
×
474
  nodesDestroyList(pNodeList);
×
475
  return NULL;
×
476
}
477

478
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
1,604,332,763✔
479
  CHECK_PARSER_STATUS(pCxt);
1,604,332,763✔
480
  SNodeList* list = NULL;
1,604,327,500✔
481
  pCxt->errCode = nodesMakeList(&list);
1,604,329,440✔
482
  CHECK_MAKE_NODE(list);
1,604,339,953✔
483
  pCxt->errCode = nodesListAppend(list, pNode);
1,604,339,953✔
484
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
1,604,334,901✔
485
    nodesDestroyList(list);
×
486
    return NULL;
×
487
  }
488
  return list;
1,604,327,230✔
489
_err:
7,217✔
490
  nodesDestroyNode(pNode);
7,217✔
491
  return NULL;
7,217✔
492
}
493

494
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
1,096,448,844✔
495
  CHECK_PARSER_STATUS(pCxt);
1,096,448,844✔
496
  pCxt->errCode = nodesListAppend(pList, pNode);
1,096,450,251✔
497
  return pList;
1,096,456,623✔
498
_err:
1,714✔
499
  nodesDestroyNode(pNode);
1,714✔
500
  nodesDestroyList(pList);
1,714✔
501
  return NULL;
1,714✔
502
}
503

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

565
SPrivSetArgs privArgsSetType(SAstCreateContext* pCxt, EPrivType type) {
×
566
  CHECK_PARSER_STATUS(pCxt);
×
567
  SPrivSetArgs args = {.nPrivArgs = 1};
×
568
  privAddType(&args.privSet, type);
569
_err:
×
570
  return args;
×
571
}
572

573
SPrivSetArgs privArgsSetCols(SAstCreateContext* pCxt, SNodeList* selectCols, SNodeList* insertCols,
×
574
                             SNodeList* updateCols) {
575
  CHECK_PARSER_STATUS(pCxt);
×
576
  SPrivSetArgs args = {.nPrivArgs = 1, .selectCols = selectCols, .insertCols = insertCols, .updateCols = updateCols};
×
577
_err:
×
578
  return args;
×
579
}
580

581
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
1,450,962,776✔
582
  CHECK_PARSER_STATUS(pCxt);
1,450,962,776✔
583
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
1,450,962,692✔
584
    return NULL;
×
585
  }
586
  SColumnNode* col = NULL;
1,450,962,132✔
587
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
1,450,961,864✔
588
  CHECK_MAKE_NODE(col);
1,450,965,288✔
589
  if (NULL != pTableAlias) {
1,450,965,288✔
590
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
278,133,746✔
591
  }
592
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
1,450,965,288✔
593
  return (SNode*)col;
1,450,961,251✔
594
_err:
×
595
  return NULL;
×
596
}
597

598
/**
599
 * @param type: 1 with mask; 0 without mask
600
 */
601
SNode* createColumnNodeExt(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName, int8_t type) {
×
602
  SNode* result = createColumnNode(pCxt, pTableAlias, pColumnName);
×
603
  if (result != NULL) {
×
604
    if (type == 1) ((SColumnNode*)result)->hasMask = 1;
×
605
  }
606
  return result;
×
607
}
608

609
SNode* createPlaceHolderColumnNode(SAstCreateContext* pCxt, SNode* pColId) {
35,683✔
610
  CHECK_PARSER_STATUS(pCxt);
35,683✔
611
  SFunctionNode* pFunc = NULL;
35,683✔
612
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
35,683✔
613
  CHECK_PARSER_STATUS(pCxt);
35,683✔
614
  tstrncpy(pFunc->functionName, "_placeholder_column", TSDB_FUNC_NAME_LEN);
35,683✔
615
  ((SValueNode*)pColId)->notReserved = true;
35,683✔
616
  pCxt->errCode = nodesListMakeAppend(&pFunc->pParameterList, pColId);
35,683✔
617
  CHECK_PARSER_STATUS(pCxt);
35,683✔
618
  pFunc->tz = pCxt->pQueryCxt->timezone;
35,683✔
619
  pFunc->charsetCxt = pCxt->pQueryCxt->charsetCxt;
35,683✔
620
  return (SNode*)pFunc;
35,683✔
621
_err:
×
622
  return NULL;
×
623
}
624

625
static void copyValueTrimEscape(char* buf, int32_t bufLen, const SToken* pToken, bool trim) {
545,342,314✔
626
  int32_t len = TMIN(pToken->n, bufLen - 1);
545,342,314✔
627
  if (trim && (pToken->z[0] == TS_ESCAPE_CHAR)) {
545,342,708✔
628
    int32_t i = 1, j = 0;
27,995✔
629
    for (; i < len - 1; ++i) {
157,790✔
630
      buf[j++] = pToken->z[i];
129,795✔
631
      if (pToken->z[i] == TS_ESCAPE_CHAR) {
129,795✔
632
        if (pToken->z[i + 1] == TS_ESCAPE_CHAR) ++i;
65,661✔
633
      }
634
    }
635
    buf[j] = 0;
27,995✔
636
  } else {
637
    tstrncpy(buf, pToken->z, len + 1);
545,314,713✔
638
  }
639
}
545,344,086✔
640

641
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
545,343,307✔
642
  CHECK_PARSER_STATUS(pCxt);
545,343,307✔
643
  SValueNode* val = NULL;
545,342,379✔
644
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
545,343,212✔
645
  CHECK_MAKE_NODE(val);
545,344,575✔
646
  if (!(val->literal = taosMemoryMalloc(pLiteral->n + 1))) {
545,344,575✔
647
    pCxt->errCode = terrno;
×
648
    nodesDestroyNode((SNode*)val);
×
649
    return NULL;
×
650
  }
651
  copyValueTrimEscape(val->literal, pLiteral->n + 1, pLiteral,
545,341,920✔
652
                      pCxt->pQueryCxt->hasDupQuoteChar && (TK_NK_ID == pLiteral->type));
545,342,317✔
653
  if (TK_NK_STRING == pLiteral->type) {
545,343,200✔
654
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
53,866,853✔
655
  }
656
  val->node.resType.type = dataType;
545,341,828✔
657
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
545,342,708✔
658
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
545,340,408✔
659
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
10,917✔
660
  }
661
  val->translate = false;
545,340,408✔
662
  val->tz = pCxt->pQueryCxt->timezone;
545,342,806✔
663
  val->charsetCxt = pCxt->pQueryCxt->charsetCxt;
545,341,391✔
664
  return (SNode*)val;
545,343,166✔
665
_err:
×
666
  return NULL;
×
667
}
668

669
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
124,333,651✔
670
  CHECK_PARSER_STATUS(pCxt);
124,333,651✔
671
  SValueNode* val = NULL;
124,332,500✔
672
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
124,333,956✔
673
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
124,343,597✔
UNCOV
674
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
675
    goto _exit;
×
676
  }
677
  if (pLiteral) {
124,338,821✔
678
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
124,168,127✔
679
    if (!val->literal) {
124,167,927✔
680
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
681
      goto _exit;
×
682
    }
683
  } else if (pNode) {
170,694✔
684
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
170,694✔
685
    if (!nodesIsExprNode(pRawExpr->pNode)) {
170,694✔
686
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
687
      goto _exit;
×
688
    }
689
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
170,694✔
690
    if (!val->literal) {
170,694✔
691
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
692
      goto _exit;
×
693
    }
694
  } else {
695
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
696
    goto _exit;
×
697
  }
698
  if (!val->literal) {
124,336,654✔
699
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
700
    goto _exit;
×
701
  }
702

703
  val->node.resType.type = dataType;
124,333,476✔
704
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
124,339,511✔
705
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
124,333,938✔
706
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
707
  }
708
_exit:
124,333,938✔
709
  nodesDestroyNode(pNode);
124,333,328✔
710
  if (pCxt->errCode != 0) {
124,325,777✔
711
    nodesDestroyNode((SNode*)val);
24✔
712
    return NULL;
×
713
  }
714
  return (SNode*)val;
124,335,418✔
715
_err:
×
716
  nodesDestroyNode(pNode);
×
717
  return NULL;
×
718
}
719

720
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
47,457✔
721
                             SNode* pRight) {
722
  SValueNode* val = NULL;
47,457✔
723
  CHECK_PARSER_STATUS(pCxt);
47,457✔
724

725
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
47,457✔
726
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
47,457✔
727
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
728
    goto _exit;
×
729
  }
730
  if (pLiteral) {
47,457✔
731
    if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) {
47,457✔
732
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
733
      goto _exit;
×
734
    }
735
  } else {
736
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
737
    goto _exit;
×
738
  }
739

740
  val->node.resType.type = dataType;
47,457✔
741
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
47,457✔
742
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
47,457✔
743
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
744
  }
745
_exit:
47,457✔
746
  nodesDestroyNode(pLeft);
47,457✔
747
  nodesDestroyNode(pRight);
47,457✔
748
  CHECK_PARSER_STATUS(pCxt);
47,457✔
749
  return (SNode*)val;
47,457✔
750
_err:
×
751
  nodesDestroyNode((SNode*)val);
×
752
  nodesDestroyNode(pLeft);
×
753
  nodesDestroyNode(pRight);
×
754
  return NULL;
×
755
}
756

757
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
8,211,574✔
758
  if (!pHintList) return false;
8,211,574✔
759
  SNode* pNode;
760
  FOREACH(pNode, pHintList) {
1,392✔
761
    SHintNode* pHint = (SHintNode*)pNode;
1,392✔
762
    if (pHint->option == hint) {
1,392✔
763
      return true;
1,392✔
764
    }
765
  }
766
  return false;
×
767
}
768

769
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
8,215,264✔
770
                       int32_t paramNum) {
771
  void* value = NULL;
8,215,264✔
772
  switch (opt) {
8,215,264✔
773
    case HINT_SKIP_TSMA:
3,690✔
774
    case HINT_BATCH_SCAN:
775
    case HINT_NO_BATCH_SCAN: {
776
      if (paramNum > 0) {
3,690✔
777
        return true;
738✔
778
      }
779
      break;
2,952✔
780
    }
781
    case HINT_SORT_FOR_GROUP:
56,608✔
782
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
56,608✔
783
      break;
56,144✔
784
    case HINT_PARTITION_FIRST:
1,856✔
785
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
1,856✔
786
      break;
928✔
787
    case HINT_PARA_TABLES_SORT:
8,152,364✔
788
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
8,152,364✔
789
      break;
8,152,364✔
790
    case HINT_SMALLDATA_TS_SORT:
×
791
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
792
      break;
×
793
    case HINT_HASH_JOIN:
746✔
794
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
746✔
795
      break;
746✔
796
    case HINT_WIN_OPTIMIZE_BATCH:
×
797
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_BATCH)) return true;
×
798
      break;
×
799
    case HINT_WIN_OPTIMIZE_SINGLE:
×
800
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_SINGLE)) return true;
×
801
      break;
×
802
    default:
×
803
      return true;
×
804
  }
805

806
  SHintNode* hint = NULL;
8,213,134✔
807
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
8,213,134✔
808
  if (!hint) {
8,213,134✔
809
    return true;
×
810
  }
811
  hint->option = opt;
8,213,134✔
812
  hint->value = value;
8,213,134✔
813

814
  if (NULL == *ppHintList) {
8,213,134✔
815
    pCxt->errCode = nodesMakeList(ppHintList);
8,212,396✔
816
    if (!*ppHintList) {
8,212,396✔
817
      nodesDestroyNode((SNode*)hint);
×
818
      return true;
×
819
    }
820
  }
821

822
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
8,213,134✔
823
  if (pCxt->errCode) {
8,213,134✔
824
    return true;
×
825
  }
826

827
  return false;
8,213,134✔
828
}
829

830
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
746,112,047✔
831
  CHECK_PARSER_STATUS(pCxt);
746,112,047✔
832
  if (NULL == pLiteral || pLiteral->n <= 5) {
746,112,413✔
833
    return NULL;
737,899,373✔
834
  }
835
  SNodeList* pHintList = NULL;
8,213,040✔
836
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
8,213,503✔
837
  if (!hint) return NULL;
8,213,503✔
838
  int32_t     i = 0;
8,213,503✔
839
  bool        quit = false;
8,213,503✔
840
  bool        inParamList = false;
8,213,503✔
841
  bool        lastComma = false;
8,213,503✔
842
  EHintOption opt = 0;
8,213,503✔
843
  int32_t     paramNum = 0;
8,213,503✔
844
  SToken      paramList[10];
8,213,503✔
845
  while (!quit) {
49,286,944✔
846
    SToken t0 = {0};
49,284,445✔
847
    if (hint[i] == 0) {
49,284,445✔
848
      break;
8,211,004✔
849
    }
850
    t0.n = tGetToken(&hint[i], &t0.type, NULL);
41,073,441✔
851
    t0.z = hint + i;
41,073,441✔
852
    i += t0.n;
41,073,441✔
853

854
    switch (t0.type) {
41,073,441✔
855
      case TK_BATCH_SCAN:
2,214✔
856
        lastComma = false;
2,214✔
857
        if (0 != opt || inParamList) {
2,214✔
858
          quit = true;
×
859
          break;
×
860
        }
861
        opt = HINT_BATCH_SCAN;
2,214✔
862
        break;
2,214✔
863
      case TK_NO_BATCH_SCAN:
1,476✔
864
        lastComma = false;
1,476✔
865
        if (0 != opt || inParamList) {
1,476✔
866
          quit = true;
×
867
          break;
×
868
        }
869
        opt = HINT_NO_BATCH_SCAN;
1,476✔
870
        break;
1,476✔
871
      case TK_SORT_FOR_GROUP:
56,608✔
872
        lastComma = false;
56,608✔
873
        if (0 != opt || inParamList) {
56,608✔
874
          quit = true;
×
875
          break;
×
876
        }
877
        opt = HINT_SORT_FOR_GROUP;
56,608✔
878
        break;
56,608✔
879
      case TK_PARTITION_FIRST:
1,856✔
880
        lastComma = false;
1,856✔
881
        if (0 != opt || inParamList) {
1,856✔
882
          quit = true;
×
883
          break;
×
884
        }
885
        opt = HINT_PARTITION_FIRST;
1,856✔
886
        break;
1,856✔
887
      case TK_PARA_TABLES_SORT:
8,152,364✔
888
        lastComma = false;
8,152,364✔
889
        if (0 != opt || inParamList) {
8,152,364✔
890
          quit = true;
×
891
          break;
×
892
        }
893
        opt = HINT_PARA_TABLES_SORT;
8,152,364✔
894
        break;
8,152,364✔
895
      case TK_SMALLDATA_TS_SORT:
×
896
        lastComma = false;
×
897
        if (0 != opt || inParamList) {
×
898
          quit = true;
×
899
          break;
×
900
        }
901
        opt = HINT_SMALLDATA_TS_SORT;
×
902
        break;
×
903
      case TK_HASH_JOIN:
746✔
904
        lastComma = false;
746✔
905
        if (0 != opt || inParamList) {
746✔
906
          quit = true;
×
907
          break;
×
908
        }
909
        opt = HINT_HASH_JOIN;
746✔
910
        break;
746✔
911
      case TK_SKIP_TSMA:
×
912
        lastComma = false;
×
913
        if (0 != opt || inParamList) {
×
914
          quit = true;
×
915
          break;
×
916
        }
917
        opt = HINT_SKIP_TSMA;
×
918
        break;
×
919
      case TK_WIN_OPTIMIZE_BATCH:
×
920
        lastComma = false;
×
921
        if (0 != opt || inParamList) {
×
922
          quit = true;
×
923
          break;
×
924
        }
925
        opt = HINT_WIN_OPTIMIZE_BATCH;
×
926
        break;
×
927
      case TK_WIN_OPTIMIZE_SINGLE:
×
928
        lastComma = false;
×
929
        if (0 != opt || inParamList) {
×
930
          quit = true;
×
931
          break;
×
932
        }
933
        opt = HINT_WIN_OPTIMIZE_SINGLE;
×
934
        break;
×
935
      case TK_NK_LP:
8,215,264✔
936
        lastComma = false;
8,215,264✔
937
        if (0 == opt || inParamList) {
8,215,264✔
938
          quit = true;
×
939
        }
940
        inParamList = true;
8,215,264✔
941
        break;
8,215,264✔
942
      case TK_NK_RP:
8,215,264✔
943
        lastComma = false;
8,215,264✔
944
        if (0 == opt || !inParamList) {
8,215,264✔
945
          quit = true;
×
946
        } else {
947
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
8,215,264✔
948
          inParamList = false;
8,215,264✔
949
          paramNum = 0;
8,215,264✔
950
          opt = 0;
8,215,264✔
951
        }
952
        break;
8,215,264✔
953
      case TK_NK_ID:
1,107✔
954
        lastComma = false;
1,107✔
955
        if (0 == opt || !inParamList) {
1,107✔
956
          quit = true;
369✔
957
        } else {
958
          paramList[paramNum++] = t0;
738✔
959
        }
960
        break;
1,107✔
961
      case TK_NK_COMMA:
738✔
962
        if (lastComma) {
738✔
963
          quit = true;
×
964
        }
965
        lastComma = true;
738✔
966
        break;
738✔
967
      case TK_NK_SPACE:
16,425,804✔
968
        break;
16,425,804✔
969
      default:
×
970
        lastComma = false;
×
971
        quit = true;
×
972
        break;
×
973
    }
974
  }
975

976
  taosMemoryFree(hint);
8,213,503✔
977
  return pHintList;
8,213,503✔
978
_err:
×
979
  return NULL;
×
980
}
981

982
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
1,359,035✔
983
  trimEscape(pCxt, pLiteral, false);
1,359,035✔
984
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
1,359,035✔
985
}
986

987
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
94,120,338✔
988
  CHECK_PARSER_STATUS(pCxt);
94,120,338✔
989
  SValueNode* val = NULL;
94,120,830✔
990
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
94,121,224✔
991
  CHECK_MAKE_NODE(val);
94,120,830✔
992
  if (pLiteral->type == TK_NK_STRING) {
94,120,830✔
993
    // like '100s' or "100d"
994
    // check format: ^[0-9]+[smwbauhdny]$'
995
    if (pLiteral->n < 4) {
15,488✔
996
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
712✔
997
      return NULL;
712✔
998
    }
999
    char unit = pLiteral->z[pLiteral->n - 2];
14,776✔
1000
    switch (unit) {
14,776✔
1001
      case 'a':
12,640✔
1002
      case 'b':
1003
      case 'd':
1004
      case 'h':
1005
      case 'm':
1006
      case 's':
1007
      case 'u':
1008
      case 'w':
1009
      case 'y':
1010
      case 'n':
1011
        break;
12,640✔
1012
      default:
2,136✔
1013
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
2,136✔
1014
        return NULL;
2,136✔
1015
    }
1016
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
45,748✔
1017
      if (!isdigit(pLiteral->z[i])) {
35,424✔
1018
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
2,316✔
1019
        return NULL;
2,316✔
1020
      }
1021
    }
1022
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
10,324✔
1023
  } else {
1024
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
94,103,478✔
1025
  }
1026
  if (!val->literal) {
94,115,174✔
1027
    nodesDestroyNode((SNode*)val);
×
1028
    pCxt->errCode = terrno;
×
1029
    return NULL;
×
1030
  }
1031
  val->flag |= VALUE_FLAG_IS_DURATION;
94,113,802✔
1032
  val->translate = false;
94,115,079✔
1033
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
94,112,519✔
1034
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
94,111,144✔
1035
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
94,115,568✔
1036
  return (SNode*)val;
94,113,799✔
1037
_err:
×
1038
  return NULL;
×
1039
}
1040

1041
SNode* createTimeOffsetValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
1,156,676✔
1042
  CHECK_PARSER_STATUS(pCxt);
1,156,676✔
1043
  SValueNode* val = NULL;
1,156,676✔
1044
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
1,156,676✔
1045
  CHECK_MAKE_NODE(val);
1,156,676✔
1046
  if (pLiteral->type == TK_NK_STRING) {
1,156,676✔
1047
    // like '100s' or "100d"
1048
    // check format: ^[0-9]+[smwbauhdny]$'
1049
    if (pLiteral->n < 4) {
×
1050
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
1051
      return NULL;
×
1052
    }
1053
    char unit = pLiteral->z[pLiteral->n - 2];
×
1054
    switch (unit) {
×
1055
      case 'a':
×
1056
      case 'b':
1057
      case 'd':
1058
      case 'h':
1059
      case 'm':
1060
      case 's':
1061
      case 'u':
1062
      case 'w':
1063
        break;
×
1064
      default:
×
1065
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
1066
        return NULL;
×
1067
    }
1068
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
×
1069
      if (!isdigit(pLiteral->z[i])) {
×
1070
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
1071
        return NULL;
×
1072
      }
1073
    }
1074
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
×
1075
  } else {
1076
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
1,156,676✔
1077
  }
1078
  if (!val->literal) {
1,156,676✔
1079
    nodesDestroyNode((SNode*)val);
×
1080
    pCxt->errCode = terrno;
×
1081
    return NULL;
×
1082
  }
1083
  val->flag |= VALUE_FLAG_IS_TIME_OFFSET;
1,156,676✔
1084
  val->translate = false;
1,156,676✔
1085
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
1,156,676✔
1086
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
1,156,676✔
1087
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
1,156,676✔
1088
  return (SNode*)val;
1,156,676✔
1089
_err:
×
1090
  return NULL;
×
1091
}
1092

1093
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
874,485✔
1094
  CHECK_PARSER_STATUS(pCxt);
874,485✔
1095
  if (NULL == pCxt->pQueryCxt->db) {
874,485✔
1096
    return NULL;
3,427✔
1097
  }
1098

1099
  SValueNode* val = NULL;
871,058✔
1100
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
871,058✔
1101
  CHECK_MAKE_NODE(val);
871,058✔
1102
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
871,058✔
1103
  if (!val->literal) {
871,058✔
1104
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
1105
    nodesDestroyNode((SNode*)val);
×
1106
    return NULL;
×
1107
  }
1108
  val->translate = false;
871,058✔
1109
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
871,058✔
1110
  val->node.resType.bytes = strlen(val->literal);
871,058✔
1111
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
871,058✔
1112
  return (SNode*)val;
871,058✔
1113
_err:
×
1114
  return NULL;
×
1115
}
1116

1117
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
9,823✔
1118
  CHECK_PARSER_STATUS(pCxt);
9,823✔
1119
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
9,823✔
1120
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
1121
    return NULL;
×
1122
  }
1123
  SValueNode* val = NULL;
9,823✔
1124
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
9,823✔
1125
  CHECK_MAKE_NODE(val);
9,823✔
1126
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
9,823✔
1127
  if (!val->literal) {
9,823✔
1128
    pCxt->errCode = terrno;
×
1129
    nodesDestroyNode((SNode*)val);
×
1130
    return NULL;
×
1131
  }
1132
  val->placeholderNo = ++pCxt->placeholderNo;
9,823✔
1133
  if (NULL == pCxt->pPlaceholderValues) {
9,823✔
1134
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
9,238✔
1135
    if (NULL == pCxt->pPlaceholderValues) {
9,238✔
1136
      nodesDestroyNode((SNode*)val);
×
1137
      return NULL;
×
1138
    }
1139
  }
1140
  if (NULL == taosArrayPush(pCxt->pPlaceholderValues, &val)) {
19,646✔
1141
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
1142
    nodesDestroyNode((SNode*)val);
×
1143
    taosArrayDestroy(pCxt->pPlaceholderValues);
×
1144
    return NULL;
×
1145
  }
1146
  return (SNode*)val;
9,823✔
1147
_err:
×
1148
  return NULL;
×
1149
}
1150

1151
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
206,694,914✔
1152
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
206,694,914✔
1153
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
26,501,091✔
1154
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
26,471,511✔
1155
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
26,471,511✔
1156
    nodesDestroyNode(pParam);
26,471,511✔
1157
    return code;
26,471,511✔
1158
  } else {
1159
    return nodesListAppend(pCond->pParameterList, pParam);
180,223,785✔
1160
  }
1161
}
1162

1163
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
103,420,464✔
1164
  CHECK_PARSER_STATUS(pCxt);
103,420,464✔
1165
  SLogicConditionNode* cond = NULL;
103,420,959✔
1166
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
103,420,956✔
1167
  CHECK_MAKE_NODE(cond);
103,419,981✔
1168
  cond->condType = type;
103,419,981✔
1169
  cond->pParameterList = NULL;
103,421,448✔
1170
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
103,419,092✔
1171
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
103,422,322✔
1172
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
103,421,448✔
1173
  }
1174
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
103,421,341✔
1175
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
103,276,610✔
1176
  }
1177
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
103,421,243✔
1178
    nodesDestroyNode((SNode*)cond);
×
1179
    return NULL;
×
1180
  }
1181
  return (SNode*)cond;
103,420,470✔
1182
_err:
×
1183
  nodesDestroyNode(pParam1);
×
1184
  nodesDestroyNode(pParam2);
×
1185
  return NULL;
×
1186
}
1187

1188
static uint8_t getMinusDataType(uint8_t orgType) {
33,703,244✔
1189
  switch (orgType) {
33,703,244✔
1190
    case TSDB_DATA_TYPE_UTINYINT:
23,329,345✔
1191
    case TSDB_DATA_TYPE_USMALLINT:
1192
    case TSDB_DATA_TYPE_UINT:
1193
    case TSDB_DATA_TYPE_UBIGINT:
1194
      return TSDB_DATA_TYPE_BIGINT;
23,329,345✔
1195
    default:
10,373,899✔
1196
      break;
10,373,899✔
1197
  }
1198
  return orgType;
10,373,899✔
1199
}
1200

1201
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
556,960,307✔
1202
  CHECK_PARSER_STATUS(pCxt);
556,960,307✔
1203
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
556,959,510✔
1204
    SValueNode* pVal = (SValueNode*)pLeft;
33,703,244✔
1205
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
33,703,244✔
1206
    if (!pNewLiteral) {
33,703,244✔
1207
      pCxt->errCode = terrno;
×
1208
      goto _err;
×
1209
    }
1210
    if ('+' == pVal->literal[0]) {
33,703,244✔
1211
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal + 1);
×
1212
    } else if ('-' == pVal->literal[0]) {
33,703,244✔
1213
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "%s", pVal->literal + 1);
1,521✔
1214
    } else {
1215
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal);
33,701,723✔
1216
    }
1217
    taosMemoryFree(pVal->literal);
33,703,244✔
1218
    pVal->literal = pNewLiteral;
33,703,203✔
1219
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
33,703,203✔
1220
    return pLeft;
33,703,244✔
1221
  }
1222
  if (pLeft && QUERY_NODE_VALUE == nodeType(pLeft)) {
523,256,266✔
1223
    SValueNode* pVal = (SValueNode*)pLeft;
20,405,967✔
1224
    pVal->tz = pCxt->pQueryCxt->timezone;
20,405,967✔
1225
  }
1226
  if (pRight && QUERY_NODE_VALUE == nodeType(pRight)) {
523,257,152✔
1227
    SValueNode* pVal = (SValueNode*)pRight;
271,877,222✔
1228
    pVal->tz = pCxt->pQueryCxt->timezone;
271,877,222✔
1229
  }
1230

1231
  SOperatorNode* op = NULL;
523,256,544✔
1232
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
523,255,184✔
1233
  CHECK_MAKE_NODE(op);
523,264,975✔
1234
  op->opType = type;
523,264,975✔
1235
  op->pLeft = pLeft;
523,263,007✔
1236
  op->pRight = pRight;
523,263,499✔
1237
  op->tz = pCxt->pQueryCxt->timezone;
523,262,616✔
1238
  op->charsetCxt = pCxt->pQueryCxt->charsetCxt;
523,263,010✔
1239
  return (SNode*)op;
523,261,045✔
1240
_err:
×
1241
  nodesDestroyNode(pLeft);
×
1242
  nodesDestroyNode(pRight);
×
1243
  return NULL;
36✔
1244
}
1245

1246
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
21,264,265✔
1247
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
21,264,265✔
1248
  CHECK_PARSER_STATUS(pCxt);
21,264,265✔
1249
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
21,263,382✔
1250
  CHECK_PARSER_STATUS(pCxt);
21,263,379✔
1251
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
21,263,379✔
1252
  CHECK_PARSER_STATUS(pCxt);
21,262,890✔
1253
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
21,261,912✔
1254
  CHECK_PARSER_STATUS(pCxt);
21,263,779✔
1255
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
21,263,287✔
1256
  CHECK_PARSER_STATUS(pCxt);
21,262,795✔
1257
  return pRet;
21,262,795✔
1258
_err:
×
1259
  nodesDestroyNode(pNew);
×
1260
  nodesDestroyNode(pGE);
×
1261
  nodesDestroyNode(pLE);
×
1262
  nodesDestroyNode(pExpr);
×
1263
  nodesDestroyNode(pLeft);
×
1264
  nodesDestroyNode(pRight);
×
1265
  return NULL;
×
1266
}
1267

1268
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
3,322,321✔
1269
  SNode *pNew = NULL, *pLT = NULL, *pGT = NULL;
3,322,321✔
1270
  CHECK_PARSER_STATUS(pCxt);
3,322,321✔
1271
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
3,322,321✔
1272
  CHECK_PARSER_STATUS(pCxt);
3,322,321✔
1273
  pLT = createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft);
3,322,321✔
1274
  CHECK_PARSER_STATUS(pCxt);
3,322,321✔
1275
  pGT = createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pNew, pRight);
3,322,321✔
1276
  CHECK_PARSER_STATUS(pCxt);
3,322,321✔
1277
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, pLT, pGT);
3,322,321✔
1278
  CHECK_PARSER_STATUS(pCxt);
3,322,321✔
1279
  return pRet;
3,322,321✔
1280
_err:
×
1281
  nodesDestroyNode(pNew);
×
1282
  nodesDestroyNode(pGT);
×
1283
  nodesDestroyNode(pLT);
×
1284
  nodesDestroyNode(pExpr);
×
1285
  nodesDestroyNode(pLeft);
×
1286
  nodesDestroyNode(pRight);
×
1287
  return NULL;
×
1288
}
1289

1290
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
99,526,820✔
1291
  CHECK_PARSER_STATUS(pCxt);
99,526,820✔
1292
  SColumnNode* pCol = NULL;
99,524,861✔
1293
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
99,526,236✔
1294
  CHECK_MAKE_NODE(pCol);
99,531,345✔
1295
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
99,531,345✔
1296
  if (NULL == pFuncName) {
99,528,106✔
1297
    tstrncpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME, TSDB_COL_NAME_LEN);
89,456,704✔
1298
  } else {
1299
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
10,071,402✔
1300
  }
1301
  pCol->isPrimTs = true;
99,531,333✔
1302
  return (SNode*)pCol;
99,530,352✔
1303
_err:
×
1304
  return NULL;
×
1305
}
1306

1307
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
540,845,443✔
1308
  CHECK_PARSER_STATUS(pCxt);
540,845,443✔
1309
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
540,844,094✔
1310
    return createPrimaryKeyCol(pCxt, pFuncName);
10,071,075✔
1311
  }
1312
  SFunctionNode* func = NULL;
530,772,435✔
1313
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
530,772,947✔
1314
  CHECK_MAKE_NODE(func);
530,773,696✔
1315
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
530,773,696✔
1316
  func->pParameterList = pParameterList;
530,774,647✔
1317
  func->tz = pCxt->pQueryCxt->timezone;
530,774,320✔
1318
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
530,775,063✔
1319
  return (SNode*)func;
530,774,629✔
1320
_err:
×
1321
  nodesDestroyList(pParameterList);
×
1322
  return NULL;
×
1323
}
1324

1325
SNode* createPHTbnameFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
33,554✔
1326
  CHECK_PARSER_STATUS(pCxt);
33,554✔
1327
  SFunctionNode* func = NULL;
33,554✔
1328
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
33,554✔
1329
  CHECK_MAKE_NODE(func);
33,554✔
1330
  tstrncpy(func->functionName, "_placeholder_tbname", TSDB_FUNC_NAME_LEN);
33,554✔
1331
  func->pParameterList = pParameterList;
33,554✔
1332
  func->tz = pCxt->pQueryCxt->timezone;
33,554✔
1333
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
33,554✔
1334
  return (SNode*)func;
33,554✔
1335
_err:
×
1336
  nodesDestroyList(pParameterList);
×
1337
  return NULL;
×
1338
}
1339

1340
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
103,249,259✔
1341
  SFunctionNode* func = NULL;
103,249,259✔
1342
  CHECK_PARSER_STATUS(pCxt);
103,249,259✔
1343
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
103,249,259✔
1344
  CHECK_MAKE_NODE(func);
103,249,259✔
1345
  tstrncpy(func->functionName, "cast", TSDB_FUNC_NAME_LEN);
103,249,259✔
1346
  func->node.resType = dt;
103,249,259✔
1347
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
103,249,259✔
1348
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
80,766,469✔
1349
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
22,482,790✔
1350
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
3,727,026✔
1351
  }
1352
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
103,249,259✔
1353
  CHECK_PARSER_STATUS(pCxt);
103,249,259✔
1354
  func->tz = pCxt->pQueryCxt->timezone;
103,249,259✔
1355
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
103,249,259✔
1356

1357
  return (SNode*)func;
103,249,259✔
1358
_err:
×
1359
  nodesDestroyNode((SNode*)func);
×
1360
  nodesDestroyNode(pExpr);
×
1361
  return NULL;
×
1362
}
1363

1364
SNode* createPositionFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
204,850✔
1365
  SFunctionNode* func = NULL;
204,850✔
1366
  CHECK_PARSER_STATUS(pCxt);
204,850✔
1367
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
204,850✔
1368
  CHECK_MAKE_NODE(func);
204,850✔
1369
  tstrncpy(func->functionName, "position", TSDB_FUNC_NAME_LEN);
204,850✔
1370
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
204,850✔
1371
  CHECK_PARSER_STATUS(pCxt);
204,850✔
1372
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
204,850✔
1373
  CHECK_PARSER_STATUS(pCxt);
204,850✔
1374
  return (SNode*)func;
204,850✔
1375
_err:
×
1376
  nodesDestroyNode((SNode*)func);
×
1377
  nodesDestroyNode(pExpr);
×
1378
  nodesDestroyNode(pExpr2);
×
1379
  return NULL;
×
1380
}
1381

1382
SNode* createTrimFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, ETrimType type) {
35,622✔
1383
  SFunctionNode* func = NULL;
35,622✔
1384
  CHECK_PARSER_STATUS(pCxt);
35,622✔
1385
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
35,622✔
1386
  CHECK_MAKE_NODE(func);
35,622✔
1387
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
35,622✔
1388
  func->trimType = type;
35,622✔
1389
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
35,622✔
1390
  CHECK_PARSER_STATUS(pCxt);
35,622✔
1391
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
35,622✔
1392
  return (SNode*)func;
35,622✔
1393
_err:
×
1394
  nodesDestroyNode((SNode*)func);
×
1395
  nodesDestroyNode(pExpr);
×
1396
  return NULL;
×
1397
}
1398

1399
SNode* createTrimFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, ETrimType type) {
111,724✔
1400
  SFunctionNode* func = NULL;
111,724✔
1401
  CHECK_PARSER_STATUS(pCxt);
111,724✔
1402
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
111,724✔
1403
  CHECK_MAKE_NODE(func);
111,724✔
1404
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
111,724✔
1405
  func->trimType = type;
111,724✔
1406
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
111,724✔
1407
  CHECK_PARSER_STATUS(pCxt);
111,724✔
1408
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
111,724✔
1409
  CHECK_PARSER_STATUS(pCxt);
111,724✔
1410
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
111,724✔
1411
  return (SNode*)func;
111,724✔
1412
_err:
×
1413
  nodesDestroyNode((SNode*)func);
×
1414
  nodesDestroyNode(pExpr);
×
1415
  nodesDestroyNode(pExpr2);
×
1416
  return NULL;
×
1417
}
1418

1419
SNode* createSubstrFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
18,639✔
1420
  SFunctionNode* func = NULL;
18,639✔
1421
  CHECK_PARSER_STATUS(pCxt);
18,639✔
1422
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
18,639✔
1423
  CHECK_MAKE_NODE(func);
18,639✔
1424
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
18,639✔
1425
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
18,639✔
1426
  CHECK_PARSER_STATUS(pCxt);
18,639✔
1427
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
18,639✔
1428
  CHECK_PARSER_STATUS(pCxt);
18,639✔
1429
  return (SNode*)func;
18,639✔
1430
_err:
×
1431
  nodesDestroyNode((SNode*)func);
×
1432
  nodesDestroyNode(pExpr);
×
1433
  nodesDestroyNode(pExpr2);
×
1434
  return NULL;
×
1435
}
1436

1437
SNode* createSubstrFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, SNode* pExpr3) {
40,898✔
1438
  SFunctionNode* func = NULL;
40,898✔
1439
  CHECK_PARSER_STATUS(pCxt);
40,898✔
1440
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
40,898✔
1441
  CHECK_MAKE_NODE(func);
40,898✔
1442
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
40,898✔
1443
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
40,898✔
1444
  CHECK_PARSER_STATUS(pCxt);
40,898✔
1445
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
40,898✔
1446
  CHECK_PARSER_STATUS(pCxt);
40,898✔
1447
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr3);
40,898✔
1448
  CHECK_PARSER_STATUS(pCxt);
40,898✔
1449
  return (SNode*)func;
40,898✔
1450
_err:
×
1451
  nodesDestroyNode((SNode*)func);
×
1452
  nodesDestroyNode(pExpr);
×
1453
  nodesDestroyNode(pExpr2);
×
1454
  nodesDestroyNode(pExpr3);
×
1455
  return NULL;
×
1456
}
1457

1458
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
6,717,560✔
1459
  SNodeListNode* list = NULL;
6,717,560✔
1460
  CHECK_PARSER_STATUS(pCxt);
6,717,560✔
1461
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
6,717,954✔
1462
  CHECK_MAKE_NODE(list);
6,717,071✔
1463
  list->pNodeList = pList;
6,717,071✔
1464
  return (SNode*)list;
6,717,465✔
1465
_err:
×
1466
  nodesDestroyList(pList);
×
1467
  return NULL;
×
1468
}
1469

1470
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
×
1471
  SNodeListNode* list = NULL;
×
1472
  CHECK_PARSER_STATUS(pCxt);
×
1473
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
×
1474
  CHECK_MAKE_NODE(list);
×
1475
  pCxt->errCode = nodesListMakeStrictAppend(&list->pNodeList, p1);
×
1476
  CHECK_PARSER_STATUS(pCxt);
×
1477
  pCxt->errCode = nodesListStrictAppend(list->pNodeList, p2);
×
1478
  CHECK_PARSER_STATUS(pCxt);
×
1479
  return (SNode*)list;
×
1480
_err:
×
1481
  nodesDestroyNode((SNode*)list);
×
1482
  nodesDestroyNode(p1);
×
1483
  nodesDestroyNode(p2);
×
1484
  return NULL;
×
1485
}
1486

1487
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
841,410,082✔
1488
  CHECK_PARSER_STATUS(pCxt);
841,410,082✔
1489
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
841,410,586✔
1490
  CHECK_NAME(checkTableName(pCxt, pTableName));
841,413,673✔
1491
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
841,409,231✔
1492
  SRealTableNode* realTable = NULL;
841,409,097✔
1493
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
841,408,976✔
1494
  CHECK_MAKE_NODE(realTable);
841,405,887✔
1495
  if (NULL != pDbName) {
841,405,887✔
1496
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
164,745,873✔
1497
  } else {
1498
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
676,660,014✔
1499
  }
1500
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
841,406,728✔
1501
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
96,747,568✔
1502
  } else {
1503
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
744,660,562✔
1504
  }
1505
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
841,408,238✔
1506
  return (SNode*)realTable;
841,405,920✔
1507
_err:
5,963✔
1508
  return NULL;
5,963✔
1509
}
1510

1511
SNode* createPlaceHolderTableNode(SAstCreateContext* pCxt, EStreamPlaceholder type, SToken* pTableAlias) {
121,015✔
1512
  CHECK_PARSER_STATUS(pCxt);
121,015✔
1513

1514
  SPlaceHolderTableNode* phTable = NULL;
121,015✔
1515
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PLACE_HOLDER_TABLE, (SNode**)&phTable);
121,015✔
1516
  CHECK_MAKE_NODE(phTable);
121,015✔
1517

1518
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
121,015✔
1519
    COPY_STRING_FORM_ID_TOKEN(phTable->table.tableAlias, pTableAlias);
2,577✔
1520
  }
1521

1522
  phTable->placeholderType = type;
121,015✔
1523
  return (SNode*)phTable;
121,015✔
1524
_err:
×
1525
  return NULL;
×
1526
}
1527

1528
SNode* createStreamNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pStreamName) {
311,286✔
1529
  CHECK_PARSER_STATUS(pCxt);
311,286✔
1530
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
311,286✔
1531
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
311,286✔
1532
  SStreamNode* pStream = NULL;
311,103✔
1533
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM, (SNode**)&pStream);
311,103✔
1534
  CHECK_MAKE_NODE(pStream);
311,103✔
1535
  if (NULL != pDbName) {
311,103✔
1536
    COPY_STRING_FORM_ID_TOKEN(pStream->dbName, pDbName);
195,572✔
1537
  } else {
1538
    snprintf(pStream->dbName, sizeof(pStream->dbName), "%s", pCxt->pQueryCxt->db);
115,531✔
1539
  }
1540
  COPY_STRING_FORM_ID_TOKEN(pStream->streamName, pStreamName);
311,103✔
1541
  return (SNode*)pStream;
311,103✔
1542
_err:
183✔
1543
  return NULL;
183✔
1544
}
1545

1546
SNode* createRecalcRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
10,911✔
1547
  SStreamCalcRangeNode* pRange = NULL;
10,911✔
1548
  CHECK_PARSER_STATUS(pCxt);
10,911✔
1549
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_CALC_RANGE, (SNode**)&pRange);
10,911✔
1550
  CHECK_MAKE_NODE(pRange);
10,911✔
1551
  if (NULL == pStart && NULL == pEnd) {
10,911✔
1552
    pRange->calcAll = true;
×
1553
  } else {
1554
    pRange->calcAll = false;
10,911✔
1555
    pRange->pStart = pStart;
10,911✔
1556
    pRange->pEnd = pEnd;
10,911✔
1557
  }
1558

1559
  return (SNode*)pRange;
10,911✔
1560
_err:
×
1561
  nodesDestroyNode((SNode*)pRange);
×
1562
  nodesDestroyNode(pStart);
×
1563
  nodesDestroyNode(pEnd);
×
1564
  return NULL;
×
1565
}
1566

1567
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
30,342,401✔
1568
  CHECK_PARSER_STATUS(pCxt);
30,342,401✔
1569
  if (!checkTableName(pCxt, pTableAlias)) {
30,342,401✔
1570
    return NULL;
×
1571
  }
1572
  STempTableNode* tempTable = NULL;
30,342,401✔
1573
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
30,342,783✔
1574
  CHECK_MAKE_NODE(tempTable);
30,342,401✔
1575
  tempTable->pSubquery = pSubquery;
30,342,401✔
1576
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
30,342,401✔
1577
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
719,820✔
1578
  } else {
1579
    taosRandStr(tempTable->table.tableAlias, 32);
29,622,581✔
1580
  }
1581
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
30,342,019✔
1582
    tstrncpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
26,293,353✔
1583
    ((SSelectStmt*)pSubquery)->isSubquery = true;
26,293,353✔
1584
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
4,048,666✔
1585
    tstrncpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
4,049,048✔
1586
  }
1587
  return (SNode*)tempTable;
30,342,401✔
1588
_err:
×
1589
  nodesDestroyNode(pSubquery);
×
1590
  return NULL;
×
1591
}
1592

1593
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, EJoinSubType stype, SNode* pLeft, SNode* pRight,
50,099,458✔
1594
                           SNode* pJoinCond) {
1595
  CHECK_PARSER_STATUS(pCxt);
50,099,458✔
1596
  SJoinTableNode* joinTable = NULL;
50,099,458✔
1597
  pCxt->errCode = nodesMakeNode(QUERY_NODE_JOIN_TABLE, (SNode**)&joinTable);
50,099,458✔
1598
  CHECK_MAKE_NODE(joinTable);
50,099,458✔
1599
  joinTable->joinType = type;
50,099,458✔
1600
  joinTable->subType = stype;
50,099,458✔
1601
  joinTable->pLeft = pLeft;
50,099,458✔
1602
  joinTable->pRight = pRight;
50,099,458✔
1603
  joinTable->pOnCond = pJoinCond;
50,099,458✔
1604
  return (SNode*)joinTable;
50,099,458✔
1605
_err:
×
1606
  nodesDestroyNode(pLeft);
×
1607
  nodesDestroyNode(pRight);
×
1608
  nodesDestroyNode(pJoinCond);
×
1609
  return NULL;
×
1610
}
1611

1612
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
18,938✔
1613
  CHECK_PARSER_STATUS(pCxt);
18,938✔
1614
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
18,938✔
1615
  CHECK_NAME(checkViewName(pCxt, pViewName));
18,572✔
1616
  SViewNode* pView = NULL;
18,206✔
1617
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
18,206✔
1618
  CHECK_MAKE_NODE(pView);
18,206✔
1619
  if (NULL != pDbName) {
18,206✔
1620
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
713✔
1621
  } else {
1622
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
17,493✔
1623
  }
1624
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
18,206✔
1625
  return (SNode*)pView;
18,206✔
1626
_err:
732✔
1627
  return NULL;
732✔
1628
}
1629

1630
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) {
37,503,197✔
1631
  CHECK_PARSER_STATUS(pCxt);
37,503,197✔
1632
  SLimitNode* limitNode = NULL;
37,503,197✔
1633
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
37,503,197✔
1634
  CHECK_MAKE_NODE(limitNode);
37,503,197✔
1635
  limitNode->limit = (SValueNode*)pLimit;
37,503,197✔
1636
  if (NULL != pOffset) {
37,503,197✔
1637
    limitNode->offset = (SValueNode*)pOffset;
6,648,874✔
1638
  }
1639
  return (SNode*)limitNode;
37,503,197✔
1640
_err:
×
1641
  return NULL;
×
1642
}
1643

1644
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
218,049,258✔
1645
  CHECK_PARSER_STATUS(pCxt);
218,049,258✔
1646
  SOrderByExprNode* orderByExpr = NULL;
218,048,864✔
1647
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
218,049,258✔
1648
  CHECK_MAKE_NODE(orderByExpr);
218,049,258✔
1649
  orderByExpr->pExpr = pExpr;
218,049,258✔
1650
  orderByExpr->order = order;
218,049,258✔
1651
  if (NULL_ORDER_DEFAULT == nullOrder) {
218,049,258✔
1652
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
217,808,733✔
1653
  }
1654
  orderByExpr->nullOrder = nullOrder;
218,049,258✔
1655
  return (SNode*)orderByExpr;
218,049,258✔
1656
_err:
×
1657
  nodesDestroyNode(pExpr);
×
1658
  return NULL;
×
1659
}
1660

1661
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
17,318,859✔
1662
  CHECK_PARSER_STATUS(pCxt);
17,318,859✔
1663
  SSessionWindowNode* session = NULL;
17,318,859✔
1664
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
17,318,859✔
1665
  CHECK_MAKE_NODE(session);
17,318,859✔
1666
  session->pCol = (SColumnNode*)pCol;
17,318,859✔
1667
  session->pGap = (SValueNode*)pGap;
17,318,859✔
1668
  return (SNode*)session;
17,318,859✔
1669
_err:
×
1670
  nodesDestroyNode(pCol);
×
1671
  nodesDestroyNode(pGap);
×
1672
  return NULL;
×
1673
}
1674

1675
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr, SNodeList* pOptions, SNode* pTrueForLimit) {
6,633,585✔
1676
  SStateWindowNode* state = NULL;
6,633,585✔
1677
  CHECK_PARSER_STATUS(pCxt);
6,633,585✔
1678
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
6,633,585✔
1679
  CHECK_MAKE_NODE(state);
6,633,585✔
1680
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
6,633,585✔
1681
  CHECK_MAKE_NODE(state->pCol);
6,633,585✔
1682
  state->pExpr = pExpr;
6,633,585✔
1683
  state->pTrueForLimit = pTrueForLimit;
6,633,585✔
1684
  if (pOptions != NULL) {
6,633,585✔
1685
    if (pOptions->length >= 1) {
352,035✔
1686
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 0), &state->pExtend);
352,035✔
1687
      CHECK_MAKE_NODE(state->pExtend);
352,035✔
1688
    }
1689
    if (pOptions->length == 2) {
352,035✔
1690
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 1), &state->pZeroth);
×
1691
      CHECK_MAKE_NODE(state->pZeroth);
×
1692
    }
1693
    nodesDestroyList(pOptions);
352,035✔
1694
  }
1695
  return (SNode*)state;
6,633,585✔
1696
_err:
×
1697
  nodesDestroyNode((SNode*)state);
×
1698
  nodesDestroyNode(pExpr);
×
1699
  nodesDestroyNode(pTrueForLimit);
×
1700
  nodesDestroyList(pOptions);
×
1701
  return NULL;
×
1702
}
1703

1704
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond, SNode* pTrueForLimit) {
5,872,636✔
1705
  SEventWindowNode* pEvent = NULL;
5,872,636✔
1706
  CHECK_PARSER_STATUS(pCxt);
5,872,636✔
1707
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
5,872,636✔
1708
  CHECK_MAKE_NODE(pEvent);
5,872,636✔
1709
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
5,872,636✔
1710
  CHECK_MAKE_NODE(pEvent->pCol);
5,872,636✔
1711
  pEvent->pStartCond = pStartCond;
5,872,636✔
1712
  pEvent->pEndCond = pEndCond;
5,872,636✔
1713
  pEvent->pTrueForLimit = pTrueForLimit;
5,872,636✔
1714
  return (SNode*)pEvent;
5,872,636✔
1715
_err:
×
1716
  nodesDestroyNode((SNode*)pEvent);
×
1717
  nodesDestroyNode(pStartCond);
×
1718
  nodesDestroyNode(pEndCond);
×
1719
  nodesDestroyNode(pTrueForLimit);
×
1720
  return NULL;
×
1721
}
1722

1723
SNode* createCountWindowNode(SAstCreateContext* pCxt, const SToken* pCountToken, const SToken* pSlidingToken,
×
1724
                             SNodeList* pColList) {
1725
  SCountWindowNode* pCount = NULL;
×
1726
  CHECK_PARSER_STATUS(pCxt);
×
1727
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
×
1728
  CHECK_MAKE_NODE(pCount);
×
1729
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1730
  CHECK_MAKE_NODE(pCount->pCol);
×
1731
  pCount->windowCount = taosStr2Int64(pCountToken->z, NULL, 10);
×
1732
  if (pSlidingToken == NULL) {
×
1733
    pCount->windowSliding = taosStr2Int64(pSlidingToken->z, NULL, 10);
×
1734
  } else {
1735
    pCount->windowSliding = taosStr2Int64(pCountToken->z, NULL, 10);
×
1736
  }
1737
  pCount->pColList = pColList;
×
1738
  return (SNode*)pCount;
×
1739
_err:
×
1740
  nodesDestroyNode((SNode*)pCount);
×
1741
  return NULL;
×
1742
}
1743

1744
SNode* createCountWindowNodeFromArgs(SAstCreateContext* pCxt, SNode* arg) {
5,856,900✔
1745
  SCountWindowArgs* args = (SCountWindowArgs*)arg;
5,856,900✔
1746
  SCountWindowNode* pCount = NULL;
5,856,900✔
1747
  CHECK_PARSER_STATUS(pCxt);
5,856,900✔
1748
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
5,856,900✔
1749
  CHECK_MAKE_NODE(pCount);
5,856,900✔
1750
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
5,856,900✔
1751
  CHECK_MAKE_NODE(pCount->pCol);
5,856,900✔
1752
  pCount->windowCount = args->count;
5,856,900✔
1753
  pCount->windowSliding = args->sliding;
5,856,900✔
1754
  pCount->pColList = args->pColList;
5,856,900✔
1755
  args->pColList = NULL;
5,856,900✔
1756
  nodesDestroyNode(arg);
5,856,900✔
1757
  return (SNode*)pCount;
5,856,900✔
1758
_err:
×
1759
  nodesDestroyNode((SNode*)pCount);
×
1760
  return NULL;
×
1761
}
1762

1763
SNode* createCountWindowArgs(SAstCreateContext* pCxt, const SToken* countToken, const SToken* slidingToken,
5,856,900✔
1764
                             SNodeList* colList) {
1765
  CHECK_PARSER_STATUS(pCxt);
5,856,900✔
1766

1767
  SCountWindowArgs* args = NULL;
5,856,900✔
1768
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW_ARGS, (SNode**)&args);
5,856,900✔
1769
  CHECK_MAKE_NODE(args);
5,856,900✔
1770
  args->count = taosStr2Int64(countToken->z, NULL, 10);
5,856,900✔
1771
  if (slidingToken && slidingToken->type == TK_NK_INTEGER) {
5,856,900✔
1772
    args->sliding = taosStr2Int64(slidingToken->z, NULL, 10);
20,651✔
1773
  } else {
1774
    args->sliding = taosStr2Int64(countToken->z, NULL, 10);
5,836,249✔
1775
  }
1776
  args->pColList = colList;
5,856,900✔
1777
  return (SNode*)args;
5,856,900✔
1778
_err:
×
1779
  return NULL;
×
1780
}
1781

1782
SNode* createAnomalyWindowNode(SAstCreateContext* pCxt, SNode* pExpr, const SToken* pFuncOpt) {
×
1783
  SAnomalyWindowNode* pAnomaly = NULL;
×
1784
  CHECK_PARSER_STATUS(pCxt);
×
1785
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ANOMALY_WINDOW, (SNode**)&pAnomaly);
×
1786
  CHECK_MAKE_NODE(pAnomaly);
×
1787
  pAnomaly->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1788
  CHECK_MAKE_NODE(pAnomaly->pCol);
×
1789
  pAnomaly->pExpr = pExpr;
×
1790
  if (pFuncOpt == NULL) {
×
1791
    tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
1792
  } else {
1793
    (void)trimString(pFuncOpt->z, pFuncOpt->n, pAnomaly->anomalyOpt, sizeof(pAnomaly->anomalyOpt));
×
1794
  }
1795
  return (SNode*)pAnomaly;
×
1796
_err:
×
1797
  nodesDestroyNode((SNode*)pAnomaly);
×
1798
  return NULL;
×
1799
}
1800

1801
SNode* createIntervalWindowNodeExt(SAstCreateContext* pCxt, SNode* pInter, SNode* pSliding) {
104,367✔
1802
  SIntervalWindowNode* pInterval = NULL;
104,367✔
1803
  CHECK_PARSER_STATUS(pCxt);
104,367✔
1804
  if (pInter) {
104,367✔
1805
    pInterval = (SIntervalWindowNode*)pInter;
82,491✔
1806
  } else {
1807
    pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&pInterval);
21,876✔
1808
    CHECK_MAKE_NODE(pInterval);
21,876✔
1809
  }
1810
  pInterval->pCol = createPrimaryKeyCol(pCxt, NULL);
104,367✔
1811
  CHECK_MAKE_NODE(pInterval->pCol);
104,367✔
1812
  pInterval->pSliding = ((SSlidingWindowNode*)pSliding)->pSlidingVal;
104,367✔
1813
  pInterval->pSOffset = ((SSlidingWindowNode*)pSliding)->pOffset;
104,367✔
1814
  return (SNode*)pInterval;
104,367✔
1815
_err:
×
1816
  nodesDestroyNode((SNode*)pInter);
×
1817
  nodesDestroyNode((SNode*)pInterval);
×
1818
  nodesDestroyNode((SNode*)pSliding);
×
1819
  return NULL;
×
1820
}
1821

1822
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
58,766,931✔
1823
                                SNode* pFill) {
1824
  SIntervalWindowNode* interval = NULL;
58,766,931✔
1825
  CHECK_PARSER_STATUS(pCxt);
58,766,931✔
1826
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
58,766,931✔
1827
  CHECK_MAKE_NODE(interval);
58,766,931✔
1828
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
58,766,931✔
1829
  CHECK_MAKE_NODE(interval->pCol);
58,766,931✔
1830
  interval->pInterval = pInterval;
58,766,931✔
1831
  interval->pOffset = pOffset;
58,766,931✔
1832
  interval->pSliding = pSliding;
58,767,313✔
1833
  interval->pFill = pFill;
58,766,931✔
1834
  TAOS_SET_OBJ_ALIGNED(&interval->timeRange, TSWINDOW_INITIALIZER);
58,766,931✔
1835
  interval->timezone = pCxt->pQueryCxt->timezone;
58,766,931✔
1836
  return (SNode*)interval;
58,766,931✔
1837
_err:
×
1838
  nodesDestroyNode((SNode*)interval);
×
1839
  nodesDestroyNode(pInterval);
×
1840
  nodesDestroyNode(pOffset);
×
1841
  nodesDestroyNode(pSliding);
×
1842
  nodesDestroyNode(pFill);
×
1843
  return NULL;
×
1844
}
1845

1846
SNode* createPeriodWindowNode(SAstCreateContext* pCxt, SNode* pPeriodTime, SNode* pOffset) {
15,727✔
1847
  SPeriodWindowNode* pPeriod = NULL;
15,727✔
1848
  CHECK_PARSER_STATUS(pCxt);
15,727✔
1849
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PERIOD_WINDOW, (SNode**)&pPeriod);
15,727✔
1850
  CHECK_MAKE_NODE(pPeriod);
15,727✔
1851
  pPeriod->pOffset = pOffset;
15,727✔
1852
  pPeriod->pPeroid = pPeriodTime;
15,727✔
1853
  return (SNode*)pPeriod;
15,727✔
1854
_err:
×
1855
  nodesDestroyNode((SNode*)pOffset);
×
1856
  nodesDestroyNode((SNode*)pPeriodTime);
×
1857
  nodesDestroyNode((SNode*)pPeriod);
×
1858
  return NULL;
×
1859
}
1860

1861
SNode* createWindowOffsetNode(SAstCreateContext* pCxt, SNode* pStartOffset, SNode* pEndOffset) {
575,466✔
1862
  SWindowOffsetNode* winOffset = NULL;
575,466✔
1863
  CHECK_PARSER_STATUS(pCxt);
575,466✔
1864
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WINDOW_OFFSET, (SNode**)&winOffset);
575,466✔
1865
  CHECK_MAKE_NODE(winOffset);
575,466✔
1866
  winOffset->pStartOffset = pStartOffset;
575,466✔
1867
  winOffset->pEndOffset = pEndOffset;
575,466✔
1868
  return (SNode*)winOffset;
575,466✔
1869
_err:
×
1870
  nodesDestroyNode((SNode*)winOffset);
×
1871
  nodesDestroyNode(pStartOffset);
×
1872
  nodesDestroyNode(pEndOffset);
×
1873
  return NULL;
×
1874
}
1875

1876
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
4,007,473✔
1877
  SFillNode* fill = NULL;
4,007,473✔
1878
  CHECK_PARSER_STATUS(pCxt);
4,007,965✔
1879
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
4,005,609✔
1880
  CHECK_MAKE_NODE(fill);
4,007,473✔
1881
  fill->mode = mode;
4,007,473✔
1882
  fill->pValues = pValues;
4,007,473✔
1883
  fill->pWStartTs = NULL;
4,007,473✔
1884
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&(fill->pWStartTs));
4,005,908✔
1885
  CHECK_MAKE_NODE(fill->pWStartTs);
4,008,359✔
1886
  tstrncpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart", TSDB_FUNC_NAME_LEN);
4,007,870✔
1887
  return (SNode*)fill;
4,005,920✔
1888
_err:
×
1889
  nodesDestroyNode((SNode*)fill);
×
1890
  nodesDestroyNode(pValues);
×
1891
  return NULL;
×
1892
}
1893

1894
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
65,463,223✔
1895
  SGroupingSetNode* groupingSet = NULL;
65,463,223✔
1896
  CHECK_PARSER_STATUS(pCxt);
65,463,223✔
1897
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
65,463,223✔
1898
  CHECK_MAKE_NODE(groupingSet);
65,463,223✔
1899
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
65,463,223✔
1900
  groupingSet->pParameterList = NULL;
65,463,223✔
1901
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
65,463,223✔
1902
  CHECK_PARSER_STATUS(pCxt);
65,463,223✔
1903
  return (SNode*)groupingSet;
65,463,223✔
1904
_err:
×
1905
  nodesDestroyNode((SNode*)groupingSet);
×
1906
  nodesDestroyNode(pNode);
×
1907
  return NULL;
×
1908
}
1909

1910
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
3,415,530✔
1911
  CHECK_PARSER_STATUS(pCxt);
3,415,530✔
1912
  if (isSubQueryNode(pStart) || isSubQueryNode(pEnd) || isSubQueryNode(pInterval)) {
3,414,647✔
1913
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
3,141✔
1914
    CHECK_PARSER_STATUS(pCxt);
4,320✔
1915
  }
1916

1917
  if (NULL == pInterval) {
3,411,210✔
1918
    if (pEnd && nodeType(pEnd) == QUERY_NODE_VALUE && ((SValueNode*)pEnd)->flag & VALUE_FLAG_IS_DURATION) {
3,296,615✔
1919
      return createInterpTimeAround(pCxt, pStart, NULL, pEnd);
1,371,365✔
1920
    }
1921
    return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
1,923,484✔
1922
  }
1923

1924
  return createInterpTimeAround(pCxt, pStart, pEnd, pInterval);
114,595✔
1925

1926
_err:
4,320✔
1927

1928
  nodesDestroyNode(pStart);
4,320✔
1929
  nodesDestroyNode(pEnd);
4,320✔
1930
  nodesDestroyNode(pInterval);
4,320✔
1931
  return NULL;
4,320✔
1932
}
1933

1934
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
1,436,355✔
1935
  CHECK_PARSER_STATUS(pCxt);
1,436,355✔
1936
  if (isSubQueryNode(pPoint)) {
1,434,885✔
1937
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
3,147✔
1938
    CHECK_PARSER_STATUS(pCxt);
2,160✔
1939
  }
1940

1941
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
1,433,510✔
1942
_err:
2,160✔
1943
  nodesDestroyNode(pPoint);
2,160✔
1944
  return NULL;
2,160✔
1945
}
1946

1947
SNode* createInterpTimeAround(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
1,485,960✔
1948
  CHECK_PARSER_STATUS(pCxt);
1,485,960✔
1949
  SRangeAroundNode* pAround = NULL;
1,485,376✔
1950
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RANGE_AROUND, (SNode**)&pAround);
1,485,376✔
1951
  CHECK_PARSER_STATUS(pCxt);
1,485,960✔
1952
  if (NULL == pEnd) {
1,486,354✔
1953
    pAround->pRange = createInterpTimePoint(pCxt, pStart);
1,371,759✔
1954
  } else {
1955
    pAround->pRange = createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
114,595✔
1956
  }
1957
  pAround->pInterval = pInterval;
1,484,582✔
1958
  CHECK_PARSER_STATUS(pCxt);
1,486,354✔
1959
  return (SNode*)pAround;
1,484,979✔
1960
_err:
×
1961
  return NULL;
×
1962
}
1963

1964
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
40,764,001✔
1965
  CHECK_PARSER_STATUS(pCxt);
40,764,001✔
1966
  SWhenThenNode* pWhenThen = NULL;
40,764,001✔
1967
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
40,764,001✔
1968
  CHECK_MAKE_NODE(pWhenThen);
40,764,001✔
1969
  pWhenThen->pWhen = pWhen;
40,764,001✔
1970
  pWhenThen->pThen = pThen;
40,764,001✔
1971
  return (SNode*)pWhenThen;
40,764,001✔
1972
_err:
×
1973
  nodesDestroyNode(pWhen);
×
1974
  nodesDestroyNode(pThen);
×
1975
  return NULL;
×
1976
}
1977

1978
static int32_t debugPrintNode(SNode* pNode) {
×
1979
  char*   pStr = NULL;
×
1980
  int32_t code = nodesNodeToString(pNode, false, &pStr, NULL);
×
1981
  if (TSDB_CODE_SUCCESS == code) {
×
1982
    (void)printf("%s\n", pStr);
×
1983
    taosMemoryFree(pStr);
×
1984
  }
1985
  return code;
×
1986
}
1987

1988
SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
40,342,539✔
1989
  CHECK_PARSER_STATUS(pCxt);
40,342,539✔
1990
  SCaseWhenNode* pCaseWhen = NULL;
40,342,539✔
1991
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CASE_WHEN, (SNode**)&pCaseWhen);
40,342,539✔
1992
  CHECK_MAKE_NODE(pCaseWhen);
40,342,539✔
1993
  pCaseWhen->pCase = pCase;
40,342,539✔
1994
  pCaseWhen->pWhenThenList = pWhenThenList;
40,342,539✔
1995
  pCaseWhen->pElse = pElse;
40,342,539✔
1996
  pCaseWhen->tz = pCxt->pQueryCxt->timezone;
40,342,539✔
1997
  pCaseWhen->charsetCxt = pCxt->pQueryCxt->charsetCxt;
40,342,539✔
1998
  // debugPrintNode((SNode*)pCaseWhen);
1999
  return (SNode*)pCaseWhen;
40,342,539✔
2000
_err:
×
2001
  nodesDestroyNode(pCase);
×
2002
  nodesDestroyList(pWhenThenList);
×
2003
  nodesDestroyNode(pElse);
×
2004
  return NULL;
×
2005
}
2006

2007
SNode* createNullIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
2,321✔
2008
  SNode *    pCase = NULL, *pEqual = NULL, *pThen = NULL;
2,321✔
2009
  SNode *    pWhenThenNode = NULL, *pElse = NULL;
2,321✔
2010
  SNodeList* pWhenThenList = NULL;
2,321✔
2011
  SNode*     pCaseWhen = NULL;
2,321✔
2012

2013
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2014
  pEqual = createOperatorNode(pCxt, OP_TYPE_EQUAL, pExpr1, pExpr2);
2,321✔
2015
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2016
  SToken nullToken = {
2,321✔
2017
      .n = 4,
2018
      .type = TK_NULL,
2019
      .z = "null",
2020
  };
2021
  pThen = createValueNode(pCxt, TSDB_DATA_TYPE_NULL, &nullToken);
2,321✔
2022
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2023
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pThen);
2,321✔
2024
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2025
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
2,321✔
2026
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2027
  pCxt->errCode = nodesCloneNode(pExpr1, &pElse);
2,321✔
2028
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2029
  pCaseWhen = createCaseWhenNode(pCxt, pCase, pWhenThenList, pElse);
2,321✔
2030
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2031
  // debugPrintNode((SNode*)pCaseWhen);
2032
  return (SNode*)pCaseWhen;
2,321✔
2033
_err:
×
2034
  nodesDestroyNode(pCase);
×
2035
  nodesDestroyNode(pEqual);
×
2036
  nodesDestroyNode(pThen);
×
2037
  nodesDestroyNode(pWhenThenNode);
×
2038
  nodesDestroyNode(pElse);
×
2039
  nodesDestroyList(pWhenThenList);
×
2040
  return NULL;
×
2041
}
2042

2043
SNode* createIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
64,026✔
2044
  SNode*     pCase = NULL;
64,026✔
2045
  SNode*     pWhenThenNode = NULL;
64,026✔
2046
  SNodeList* pWhenThenList = NULL;
64,026✔
2047
  SNode*     pCaseWhen = NULL;
64,026✔
2048

2049
  CHECK_PARSER_STATUS(pCxt);
64,026✔
2050
  pWhenThenNode = createWhenThenNode(pCxt, pExpr1, pExpr2);
64,026✔
2051
  CHECK_PARSER_STATUS(pCxt);
64,026✔
2052
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
64,026✔
2053
  CHECK_PARSER_STATUS(pCxt);
64,026✔
2054
  pCaseWhen = createCaseWhenNode(pCxt, pCase, pWhenThenList, pExpr3);
64,026✔
2055
  CHECK_PARSER_STATUS(pCxt);
64,026✔
2056
  // debugPrintNode((SNode*)pCaseWhen);
2057
  return (SNode*)pCaseWhen;
64,026✔
2058
_err:
×
2059
  nodesDestroyNode(pCase);
×
2060
  nodesDestroyNode(pWhenThenNode);
×
2061
  nodesDestroyList(pWhenThenList);
×
2062
  return NULL;
×
2063
}
2064

2065
SNode* createNvlNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
8,206✔
2066
  SNode *    pThen = NULL, *pEqual = NULL;
8,206✔
2067
  SNode*     pWhenThenNode = NULL;
8,206✔
2068
  SNodeList* pWhenThenList = NULL;
8,206✔
2069
  SNode*     pCaseWhen = NULL;
8,206✔
2070

2071
  CHECK_PARSER_STATUS(pCxt);
8,206✔
2072
  pEqual = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr1, NULL);
8,206✔
2073
  CHECK_PARSER_STATUS(pCxt);
8,206✔
2074
  pCxt->errCode = nodesCloneNode(pExpr1, &pThen);
8,206✔
2075
  CHECK_PARSER_STATUS(pCxt);
8,206✔
2076
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pThen);
8,206✔
2077
  CHECK_PARSER_STATUS(pCxt);
8,206✔
2078
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
8,206✔
2079
  CHECK_PARSER_STATUS(pCxt);
8,206✔
2080
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, pExpr2);
8,206✔
2081
  CHECK_PARSER_STATUS(pCxt);
8,206✔
2082
  // debugPrintNode((SNode*)pCaseWhen);
2083
  return (SNode*)pCaseWhen;
8,206✔
2084
_err:
×
2085
  nodesDestroyNode(pEqual);
×
2086
  nodesDestroyNode(pThen);
×
2087
  nodesDestroyNode(pWhenThenNode);
×
2088
  nodesDestroyList(pWhenThenList);
×
2089
  return NULL;
×
2090
}
2091

2092
SNode* createNvl2Node(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
2,321✔
2093
  SNode *    pEqual = NULL, *pWhenThenNode = NULL;
2,321✔
2094
  SNodeList* pWhenThenList = NULL;
2,321✔
2095
  SNode*     pCaseWhen = NULL;
2,321✔
2096

2097
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2098
  pEqual = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr1, NULL);
2,321✔
2099
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2100
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pExpr2);
2,321✔
2101
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2102
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
2,321✔
2103
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2104
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, pExpr3);
2,321✔
2105
  CHECK_PARSER_STATUS(pCxt);
2,321✔
2106
  // debugPrintNode((SNode*)pCaseWhen);
2107
  return (SNode*)pCaseWhen;
2,321✔
2108
_err:
×
2109
  nodesDestroyNode(pEqual);
×
2110
  nodesDestroyNode(pWhenThenNode);
×
2111
  nodesDestroyList(pWhenThenList);
×
2112
  return NULL;
×
2113
}
2114

2115
SNode* createCoalesceNode(SAstCreateContext* pCxt, SNodeList* pParamList) {
1,782✔
2116
  int32_t    sizeParam = LIST_LENGTH(pParamList);
1,782✔
2117
  SNode *    pNotNullCond = NULL, *pWhenThenNode = NULL, *pExpr = NULL;
1,782✔
2118
  SNodeList* pWhenThenList = NULL;
1,782✔
2119
  SNode *    pCaseWhen = NULL, *pThen = NULL;
1,782✔
2120

2121
  CHECK_PARSER_STATUS(pCxt);
1,782✔
2122

2123
  for (int i = 0; i < sizeParam; ++i) {
6,237✔
2124
    pExpr = nodesListGetNode(pParamList, i);
4,455✔
2125

2126
    pCxt->errCode = nodesCloneNode(pExpr, &pThen);
4,455✔
2127
    CHECK_PARSER_STATUS(pCxt);
4,455✔
2128

2129
    pNotNullCond = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr, NULL);
4,455✔
2130
    CHECK_PARSER_STATUS(pCxt);
4,455✔
2131

2132
    pWhenThenNode = createWhenThenNode(pCxt, pNotNullCond, pThen);
4,455✔
2133
    CHECK_PARSER_STATUS(pCxt);
4,455✔
2134

2135
    if (!pWhenThenList) {
4,455✔
2136
      pWhenThenList = createNodeList(pCxt, pWhenThenNode);
1,782✔
2137
    } else {
2138
      pCxt->errCode = nodesListAppend(pWhenThenList, pWhenThenNode);
2,673✔
2139
    }
2140
    CHECK_PARSER_STATUS(pCxt);
4,455✔
2141
  }
2142

2143
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, NULL);
1,782✔
2144
  CHECK_PARSER_STATUS(pCxt);
1,782✔
2145
  // debugPrintNode((SNode*)pCaseWhen);
2146
  return (SNode*)pCaseWhen;
1,782✔
2147
_err:
×
2148
  nodesDestroyNode(pExpr);
×
2149
  nodesDestroyNode(pNotNullCond);
×
2150
  nodesDestroyNode(pThen);
×
2151
  nodesDestroyNode(pWhenThenNode);
×
2152
  nodesDestroyList(pWhenThenList);
×
2153
  return NULL;
×
2154
}
2155

2156
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
92,150,536✔
2157
  CHECK_PARSER_STATUS(pCxt);
92,150,536✔
2158
  trimEscape(pCxt, pAlias, false);
92,150,536✔
2159
  SExprNode* pExpr = (SExprNode*)pNode;
92,150,536✔
2160
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
92,150,536✔
2161
  strncpy(pExpr->aliasName, pAlias->z, len);
92,150,536✔
2162
  pExpr->aliasName[len] = '\0';
92,150,536✔
2163
  strncpy(pExpr->userAlias, pAlias->z, len);
92,150,536✔
2164
  pExpr->userAlias[len] = '\0';
92,150,536✔
2165
  pExpr->asAlias = true;
92,150,536✔
2166
  return pNode;
92,150,536✔
2167
_err:
×
2168
  nodesDestroyNode(pNode);
×
2169
  return NULL;
×
2170
}
2171

2172
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
730,279,488✔
2173
  CHECK_PARSER_STATUS(pCxt);
730,279,488✔
2174
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730,279,069✔
2175
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
730,278,871✔
2176
  }
2177
  return pStmt;
730,276,467✔
2178
_err:
549✔
2179
  nodesDestroyNode(pStmt);
549✔
2180
  nodesDestroyNode(pWhere);
549✔
2181
  return NULL;
549✔
2182
}
2183

2184
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
730,278,523✔
2185
  CHECK_PARSER_STATUS(pCxt);
730,278,523✔
2186
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730,279,786✔
2187
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
730,276,890✔
2188
  }
2189
  return pStmt;
730,278,341✔
2190
_err:
549✔
2191
  nodesDestroyNode(pStmt);
549✔
2192
  nodesDestroyList(pPartitionByList);
549✔
2193
  return NULL;
549✔
2194
}
2195

2196
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
730,279,492✔
2197
  CHECK_PARSER_STATUS(pCxt);
730,279,492✔
2198
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730,281,705✔
2199
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
730,280,480✔
2200
  }
2201
  return pStmt;
730,279,303✔
2202
_err:
549✔
2203
  nodesDestroyNode(pStmt);
549✔
2204
  nodesDestroyNode(pWindow);
549✔
2205
  return NULL;
549✔
2206
}
2207

2208
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
730,279,504✔
2209
  CHECK_PARSER_STATUS(pCxt);
730,279,504✔
2210
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730,279,699✔
2211
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
730,279,468✔
2212
  }
2213
  return pStmt;
730,274,086✔
2214
_err:
549✔
2215
  nodesDestroyNode(pStmt);
549✔
2216
  nodesDestroyList(pGroupByList);
549✔
2217
  return NULL;
549✔
2218
}
2219

2220
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
730,278,996✔
2221
  CHECK_PARSER_STATUS(pCxt);
730,278,996✔
2222
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730,278,968✔
2223
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
730,276,182✔
2224
  }
2225
  return pStmt;
730,273,037✔
2226
_err:
549✔
2227
  nodesDestroyNode(pStmt);
549✔
2228
  nodesDestroyNode(pHaving);
549✔
2229
  return NULL;
549✔
2230
}
2231

2232
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
682,764,186✔
2233
  CHECK_PARSER_STATUS(pCxt);
682,764,186✔
2234
  if (NULL == pOrderByList) {
682,763,735✔
2235
    return pStmt;
512,767,314✔
2236
  }
2237
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
169,996,421✔
2238
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
154,210,738✔
2239
  } else {
2240
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
15,786,065✔
2241
  }
2242
  return pStmt;
169,995,049✔
2243
_err:
549✔
2244
  nodesDestroyNode(pStmt);
549✔
2245
  nodesDestroyList(pOrderByList);
549✔
2246
  return NULL;
549✔
2247
}
2248

2249
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
682,764,170✔
2250
  CHECK_PARSER_STATUS(pCxt);
682,764,170✔
2251
  if (NULL == pSlimit) {
682,765,475✔
2252
    return pStmt;
680,526,519✔
2253
  }
2254
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
2,238,956✔
2255
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
2,238,956✔
2256
  }
2257
  return pStmt;
2,238,956✔
2258
_err:
549✔
2259
  nodesDestroyNode(pStmt);
549✔
2260
  nodesDestroyNode(pSlimit);
549✔
2261
  return NULL;
549✔
2262
}
2263

2264
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
682,764,130✔
2265
  CHECK_PARSER_STATUS(pCxt);
682,764,130✔
2266
  if (NULL == pLimit) {
682,764,893✔
2267
    return pStmt;
647,700,364✔
2268
  }
2269
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
35,064,529✔
2270
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
34,022,337✔
2271
  } else {
2272
    ((SSetOperator*)pStmt)->pLimit = pLimit;
1,042,192✔
2273
  }
2274
  return pStmt;
35,064,529✔
2275
_err:
549✔
2276
  nodesDestroyNode(pStmt);
549✔
2277
  nodesDestroyNode(pLimit);
549✔
2278
  return NULL;
549✔
2279
}
2280

2281
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
730,278,484✔
2282
  CHECK_PARSER_STATUS(pCxt);
730,278,484✔
2283
  SSelectStmt* pSelect = (SSelectStmt*)pStmt;
730,278,907✔
2284
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730,278,907✔
2285
    if (pRange && nodeType(pRange) == QUERY_NODE_RANGE_AROUND) {
730,281,237✔
2286
      pSelect->pRangeAround = pRange;
1,480,267✔
2287
      SRangeAroundNode* pAround = (SRangeAroundNode*)pRange;
1,478,495✔
2288
      TSWAP(pSelect->pRange, pAround->pRange);
1,478,495✔
2289
    } else {
2290
      pSelect->pRange = pRange;
728,799,106✔
2291
    }
2292
  }
2293
  return pStmt;
730,277,462✔
2294
_err:
549✔
2295
  nodesDestroyNode(pStmt);
549✔
2296
  nodesDestroyNode(pRange);
549✔
2297
  return NULL;
549✔
2298
}
2299

2300
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
730,279,456✔
2301
  CHECK_PARSER_STATUS(pCxt);
730,279,456✔
2302
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730,280,323✔
2303
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
730,281,216✔
2304
  }
2305
  return pStmt;
730,277,555✔
2306
_err:
549✔
2307
  nodesDestroyNode(pStmt);
549✔
2308
  nodesDestroyNode(pEvery);
549✔
2309
  return NULL;
549✔
2310
}
2311

2312
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
730,278,984✔
2313
  CHECK_PARSER_STATUS(pCxt);
730,278,984✔
2314
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
730,279,289✔
2315
    SFillNode* pFillClause = (SFillNode*)pFill;
3,503,180✔
2316
    nodesDestroyNode(pFillClause->pWStartTs);
3,503,180✔
2317
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
3,500,726✔
2318
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
3,502,294✔
2319
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
3,503,574✔
2320
  }
2321
  return pStmt;
730,279,029✔
2322
_err:
549✔
2323
  nodesDestroyNode(pStmt);
549✔
2324
  nodesDestroyNode(pFill);
549✔
2325
  return NULL;
549✔
2326
}
2327

2328
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
42,494,962✔
2329
  CHECK_PARSER_STATUS(pCxt);
42,494,962✔
2330
  if (NULL == pJLimit) {
42,494,962✔
2331
    return pJoin;
42,295,250✔
2332
  }
2333
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
199,712✔
2334
  pJoinNode->pJLimit = pJLimit;
199,712✔
2335

2336
  return pJoin;
199,712✔
2337
_err:
×
2338
  nodesDestroyNode(pJoin);
×
2339
  nodesDestroyNode(pJLimit);
×
2340
  return NULL;
×
2341
}
2342

2343
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
42,494,962✔
2344
  CHECK_PARSER_STATUS(pCxt);
42,494,962✔
2345
  if (NULL == pWinOffset) {
42,494,962✔
2346
    return pJoin;
41,930,304✔
2347
  }
2348
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
564,658✔
2349
  pJoinNode->pWindowOffset = pWinOffset;
564,658✔
2350

2351
  return pJoin;
564,658✔
2352
_err:
×
2353
  nodesDestroyNode(pJoin);
×
2354
  nodesDestroyNode(pWinOffset);
×
2355
  return NULL;
×
2356
}
2357

2358
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
730,279,016✔
2359
                        SNodeList* pHint) {
2360
  CHECK_PARSER_STATUS(pCxt);
730,279,016✔
2361
  SNode* select = NULL;
730,279,721✔
2362
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
730,280,183✔
2363
  CHECK_MAKE_NODE(select);
730,278,873✔
2364
  return select;
730,278,873✔
2365
_err:
549✔
2366
  nodesDestroyList(pProjectionList);
549✔
2367
  nodesDestroyNode(pTable);
549✔
2368
  nodesDestroyList(pHint);
549✔
2369
  return NULL;
549✔
2370
}
2371

2372
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
730,278,996✔
2373
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730,278,996✔
2374
    if (pCxt->pQueryCxt->biMode) {
730,279,373✔
2375
      ((SSelectStmt*)pStmt)->tagScan = true;
6,196✔
2376
    } else {
2377
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
730,268,729✔
2378
    }
2379
  }
2380
  return pStmt;
730,278,692✔
2381
}
2382

2383
static void setSubquery(SNode* pStmt) {
109,823,816✔
2384
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
109,823,816✔
2385
    ((SSelectStmt*)pStmt)->isSubquery = true;
109,569,479✔
2386
  }
2387
}
109,823,816✔
2388

2389
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
54,911,908✔
2390
  CHECK_PARSER_STATUS(pCxt);
54,911,908✔
2391
  SSetOperator* setOp = NULL;
54,911,908✔
2392
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
54,911,908✔
2393
  CHECK_MAKE_NODE(setOp);
54,911,908✔
2394
  setOp->opType = type;
54,911,908✔
2395
  setOp->pLeft = pLeft;
54,911,908✔
2396
  setSubquery(setOp->pLeft);
54,911,908✔
2397
  setOp->pRight = pRight;
54,911,908✔
2398
  setSubquery(setOp->pRight);
54,911,908✔
2399
  snprintf(setOp->stmtName, TSDB_TABLE_NAME_LEN, "%p", setOp);
54,911,908✔
2400
  return (SNode*)setOp;
54,911,908✔
2401
_err:
×
2402
  nodesDestroyNode(pLeft);
×
2403
  nodesDestroyNode(pRight);
×
2404
  return NULL;
×
2405
}
2406

2407
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
1,862,202✔
2408
  if (!pOptions->walRetentionPeriodIsSet) {
1,862,202✔
2409
    pOptions->walRetentionPeriod =
1,852,622✔
2410
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
2411
  }
2412
  if (!pOptions->walRetentionSizeIsSet) {
1,862,202✔
2413
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
1,861,500✔
2414
  }
2415
  if (!pOptions->walRollPeriodIsSet) {
1,862,202✔
2416
    pOptions->walRollPeriod =
1,862,202✔
2417
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
2418
  }
2419
}
1,862,202✔
2420

2421
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
1,369,753✔
2422
  CHECK_PARSER_STATUS(pCxt);
1,369,753✔
2423
  SDatabaseOptions* pOptions = NULL;
1,369,753✔
2424
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
1,369,753✔
2425
  CHECK_MAKE_NODE(pOptions);
1,369,753✔
2426
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
1,369,753✔
2427
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
1,369,753✔
2428
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
1,369,753✔
2429
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
1,369,753✔
2430
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
1,369,753✔
2431
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
1,369,753✔
2432
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
1,369,753✔
2433
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
1,369,753✔
2434
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
1,369,753✔
2435
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
1,369,753✔
2436
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
1,369,753✔
2437
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
1,369,753✔
2438
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
1,369,753✔
2439
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
1,369,753✔
2440
  pOptions->precision = TSDB_DEFAULT_PRECISION;
1,369,753✔
2441
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
1,369,753✔
2442
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
1,369,753✔
2443
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
1,369,753✔
2444
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
1,369,753✔
2445
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
1,369,753✔
2446
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
1,369,753✔
2447
  updateWalOptionsDefault(pOptions);
1,369,753✔
2448
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
1,369,753✔
2449
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
1,369,753✔
2450
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
1,369,753✔
2451
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
1,369,753✔
2452
  pOptions->ssChunkSize = TSDB_DEFAULT_SS_CHUNK_SIZE;
1,369,753✔
2453
  pOptions->ssKeepLocal = TSDB_DEFAULT_SS_KEEP_LOCAL;
1,369,753✔
2454
  pOptions->ssCompact = TSDB_DEFAULT_SS_COMPACT;
1,369,753✔
2455
  pOptions->withArbitrator = TSDB_DEFAULT_DB_WITH_ARBITRATOR;
1,369,753✔
2456
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
1,369,753✔
2457
  pOptions->dnodeListStr[0] = 0;
1,369,753✔
2458
  pOptions->compactInterval = TSDB_DEFAULT_COMPACT_INTERVAL;
1,369,753✔
2459
  pOptions->compactStartTime = TSDB_DEFAULT_COMPACT_START_TIME;
1,369,753✔
2460
  pOptions->compactEndTime = TSDB_DEFAULT_COMPACT_END_TIME;
1,369,753✔
2461
  pOptions->compactTimeOffset = TSDB_DEFAULT_COMPACT_TIME_OFFSET;
1,369,753✔
2462
  pOptions->encryptAlgorithmStr[0] = 0;
1,369,753✔
2463
  pOptions->isAudit = 0;
1,369,753✔
2464
  return (SNode*)pOptions;
1,369,753✔
2465
_err:
×
2466
  return NULL;
×
2467
}
2468

2469
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
184,829✔
2470
  CHECK_PARSER_STATUS(pCxt);
184,829✔
2471
  SDatabaseOptions* pOptions = NULL;
184,829✔
2472
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
184,829✔
2473
  CHECK_MAKE_NODE(pOptions);
184,829✔
2474
  pOptions->buffer = -1;
184,829✔
2475
  pOptions->cacheModel = -1;
184,829✔
2476
  pOptions->cacheLastSize = -1;
184,829✔
2477
  pOptions->compressionLevel = -1;
184,829✔
2478
  pOptions->daysPerFile = -1;
184,829✔
2479
  pOptions->fsyncPeriod = -1;
184,829✔
2480
  pOptions->maxRowsPerBlock = -1;
184,829✔
2481
  pOptions->minRowsPerBlock = -1;
184,829✔
2482
  pOptions->keep[0] = -1;
184,829✔
2483
  pOptions->keep[1] = -1;
184,829✔
2484
  pOptions->keep[2] = -1;
184,829✔
2485
  pOptions->pages = -1;
184,829✔
2486
  pOptions->pagesize = -1;
184,829✔
2487
  pOptions->tsdbPageSize = -1;
184,829✔
2488
  pOptions->precision = -1;
184,829✔
2489
  pOptions->replica = -1;
184,829✔
2490
  pOptions->strict = -1;
184,829✔
2491
  pOptions->walLevel = -1;
184,829✔
2492
  pOptions->numOfVgroups = -1;
184,829✔
2493
  pOptions->singleStable = -1;
184,829✔
2494
  pOptions->schemaless = -1;
184,829✔
2495
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
184,829✔
2496
  pOptions->walRetentionSize = -2;    // -1 is a valid value
184,829✔
2497
  pOptions->walRollPeriod = -1;
184,829✔
2498
  pOptions->walSegmentSize = -1;
184,829✔
2499
  pOptions->sstTrigger = -1;
184,829✔
2500
  pOptions->tablePrefix = -1;
184,829✔
2501
  pOptions->tableSuffix = -1;
184,829✔
2502
  pOptions->ssChunkSize = -1;
184,829✔
2503
  pOptions->ssKeepLocal = -1;
184,829✔
2504
  pOptions->ssCompact = -1;
184,829✔
2505
  pOptions->withArbitrator = -1;
184,829✔
2506
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
184,829✔
2507
  pOptions->dnodeListStr[0] = 0;
184,829✔
2508
  pOptions->compactInterval = -1;
184,829✔
2509
  pOptions->compactStartTime = -1;
184,829✔
2510
  pOptions->compactEndTime = -1;
184,829✔
2511
  pOptions->compactTimeOffset = -1;
184,829✔
2512
  pOptions->encryptAlgorithmStr[0] = 0;
184,829✔
2513
  pOptions->isAudit = 0;
184,829✔
2514
  return (SNode*)pOptions;
184,829✔
2515
_err:
×
2516
  return NULL;
×
2517
}
2518

2519
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
2,235,265✔
2520
                                    bool alter) {
2521
  CHECK_PARSER_STATUS(pCxt);
2,235,265✔
2522
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
2,235,265✔
2523
  switch (type) {
2,235,265✔
2524
    case DB_OPTION_BUFFER:
52,537✔
2525
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
52,537✔
2526
      break;
52,537✔
2527
    case DB_OPTION_CACHEMODEL:
63,051✔
2528
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
63,051✔
2529
      break;
63,051✔
2530
    case DB_OPTION_CACHESIZE:
11,306✔
2531
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
11,306✔
2532
      break;
11,306✔
2533
    case DB_OPTION_COMP:
16,413✔
2534
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
16,413✔
2535
      break;
16,413✔
2536
    case DB_OPTION_DAYS: {
286,290✔
2537
      SToken* pToken = pVal;
286,290✔
2538
      if (TK_NK_INTEGER == pToken->type) {
286,290✔
2539
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
228,475✔
2540
      } else {
2541
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
57,815✔
2542
      }
2543
      break;
286,290✔
2544
    }
2545
    case DB_OPTION_FSYNC:
30,517✔
2546
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
30,517✔
2547
      break;
30,517✔
2548
    case DB_OPTION_MAXROWS:
26,594✔
2549
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
26,594✔
2550
      break;
26,594✔
2551
    case DB_OPTION_MINROWS:
30,475✔
2552
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
30,475✔
2553
      break;
30,475✔
2554
    case DB_OPTION_KEEP:
186,168✔
2555
      pDbOptions->pKeep = pVal;
186,168✔
2556
      break;
186,168✔
2557
    case DB_OPTION_PAGES:
12,665✔
2558
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
12,665✔
2559
      break;
12,665✔
2560
    case DB_OPTION_PAGESIZE:
5,423✔
2561
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5,423✔
2562
      break;
5,423✔
2563
    case DB_OPTION_TSDB_PAGESIZE:
4,113✔
2564
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4,113✔
2565
      break;
4,113✔
2566
    case DB_OPTION_PRECISION:
111,908✔
2567
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
111,908✔
2568
      break;
111,908✔
2569
    case DB_OPTION_REPLICA:
503,489✔
2570
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
503,489✔
2571
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
503,489✔
2572
      if (!alter) {
503,489✔
2573
        updateWalOptionsDefault(pDbOptions);
492,449✔
2574
      }
2575
      break;
503,489✔
2576
    case DB_OPTION_STRICT:
×
2577
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
2578
      break;
×
2579
    case DB_OPTION_WAL:
44,308✔
2580
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
44,308✔
2581
      break;
44,308✔
2582
    case DB_OPTION_VGROUPS:
486,813✔
2583
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
486,813✔
2584
      break;
486,813✔
2585
    case DB_OPTION_SINGLE_STABLE:
6,797✔
2586
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
6,797✔
2587
      break;
6,797✔
2588
    case DB_OPTION_RETENTIONS:
×
2589
      pDbOptions->pRetentions = pVal;
×
2590
      break;
×
2591
    case DB_OPTION_WAL_RETENTION_PERIOD:
82,554✔
2592
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
82,554✔
2593
      pDbOptions->walRetentionPeriodIsSet = true;
82,554✔
2594
      break;
82,554✔
2595
    case DB_OPTION_WAL_RETENTION_SIZE:
16,199✔
2596
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
16,199✔
2597
      pDbOptions->walRetentionSizeIsSet = true;
16,199✔
2598
      break;
16,199✔
2599
    case DB_OPTION_WAL_ROLL_PERIOD:
×
2600
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2601
      pDbOptions->walRollPeriodIsSet = true;
×
2602
      break;
×
2603
    case DB_OPTION_WAL_SEGMENT_SIZE:
×
2604
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2605
      break;
×
2606
    case DB_OPTION_STT_TRIGGER:
34,280✔
2607
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
34,280✔
2608
      break;
34,280✔
2609
    case DB_OPTION_TABLE_PREFIX: {
16,077✔
2610
      SValueNode* pNode = (SValueNode*)pVal;
16,077✔
2611
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
16,077✔
2612
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
16,077✔
2613
      } else {
2614
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
2615
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2616
      }
2617
      nodesDestroyNode((SNode*)pNode);
16,077✔
2618
      break;
16,077✔
2619
    }
2620
    case DB_OPTION_TABLE_SUFFIX: {
15,535✔
2621
      SValueNode* pNode = (SValueNode*)pVal;
15,535✔
2622
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
15,535✔
2623
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
15,535✔
2624
      } else {
2625
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
2626
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2627
      }
2628
      nodesDestroyNode((SNode*)pNode);
15,535✔
2629
      break;
15,535✔
2630
    }
2631
    case DB_OPTION_SS_CHUNKPAGES:
3,466✔
2632
      pDbOptions->ssChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
3,466✔
2633
      break;
3,466✔
2634
    case DB_OPTION_SS_KEEPLOCAL: {
5,326✔
2635
      SToken* pToken = pVal;
5,326✔
2636
      if (TK_NK_INTEGER == pToken->type) {
5,326✔
2637
        pDbOptions->ssKeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
1,148✔
2638
      } else {
2639
        pDbOptions->ssKeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
4,178✔
2640
      }
2641
      break;
5,326✔
2642
    }
2643
    case DB_OPTION_SS_COMPACT:
5,602✔
2644
      pDbOptions->ssCompact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
5,602✔
2645
      break;
5,602✔
2646
    case DB_OPTION_KEEP_TIME_OFFSET:
11,069✔
2647
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
11,069✔
2648
        pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
6,454✔
2649
      } else {
2650
        pDbOptions->pKeepTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
4,615✔
2651
      }
2652
      break;
11,069✔
2653
    case DB_OPTION_ENCRYPT_ALGORITHM:
8,701✔
2654
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
8,701✔
2655
      if (strlen(pDbOptions->encryptAlgorithmStr) == 0) pDbOptions->encryptAlgorithm = -1;
8,701✔
2656
      break;
8,701✔
2657
    case DB_OPTION_DNODES:
26,256✔
2658
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
26,256✔
2659
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
713✔
2660
                 TSDB_DNODE_LIST_LEN);
2661
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
713✔
2662
      } else {
2663
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
25,543✔
2664
      }
2665
      break;
26,256✔
2666
    case DB_OPTION_COMPACT_INTERVAL:
31,332✔
2667
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
31,332✔
2668
        pDbOptions->compactInterval = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4,784✔
2669
      } else {
2670
        pDbOptions->pCompactIntervalNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
26,548✔
2671
      }
2672
      break;
31,332✔
2673
    case DB_OPTION_COMPACT_TIME_RANGE:
48,836✔
2674
      pDbOptions->pCompactTimeRangeList = pVal;
48,836✔
2675
      break;
48,836✔
2676
    case DB_OPTION_COMPACT_TIME_OFFSET:
28,800✔
2677
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
28,800✔
2678
        pDbOptions->compactTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
14,050✔
2679
      } else {
2680
        pDbOptions->pCompactTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
14,750✔
2681
      }
2682
      break;
28,800✔
2683
    case DB_OPTION_IS_AUDIT:
2,976✔
2684
      pDbOptions->isAudit = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
2,976✔
2685
      break;
2,976✔
2686
    default:
19,389✔
2687
      break;
19,389✔
2688
  }
2689
  return pOptions;
2,235,265✔
2690
_err:
×
2691
  nodesDestroyNode(pOptions);
×
2692
  return NULL;
×
2693
}
2694

2695
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
2,035,199✔
2696
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
2,035,199✔
2697
}
2698

2699
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
200,066✔
2700
  CHECK_PARSER_STATUS(pCxt);
200,066✔
2701
  switch (pAlterOption->type) {
200,066✔
2702
    case DB_OPTION_KEEP:
60,897✔
2703
    case DB_OPTION_RETENTIONS:
2704
    case DB_OPTION_COMPACT_TIME_RANGE:
2705
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
60,897✔
2706
    default:
139,169✔
2707
      break;
139,169✔
2708
  }
2709
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
139,169✔
2710
_err:
×
2711
  nodesDestroyNode(pOptions);
×
2712
  nodesDestroyList(pAlterOption->pList);
×
2713
  return NULL;
×
2714
}
2715

2716
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
1,336,635✔
2717
  CHECK_PARSER_STATUS(pCxt);
1,336,635✔
2718
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,335,922✔
2719
  SCreateDatabaseStmt* pStmt = NULL;
1,334,703✔
2720
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
1,334,703✔
2721
  CHECK_MAKE_NODE(pStmt);
1,334,794✔
2722
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,334,794✔
2723
  pStmt->ignoreExists = ignoreExists;
1,334,794✔
2724
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
1,334,794✔
2725
  return (SNode*)pStmt;
1,334,703✔
2726
_err:
1,841✔
2727
  nodesDestroyNode(pOptions);
1,841✔
2728
  return NULL;
1,841✔
2729
}
2730

2731
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName, bool force) {
1,102,277✔
2732
  CHECK_PARSER_STATUS(pCxt);
1,102,277✔
2733
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,102,277✔
2734
  SDropDatabaseStmt* pStmt = NULL;
1,102,277✔
2735
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
1,102,277✔
2736
  CHECK_MAKE_NODE(pStmt);
1,102,277✔
2737
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,102,277✔
2738
  pStmt->ignoreNotExists = ignoreNotExists;
1,102,277✔
2739
  pStmt->force = force;
1,102,277✔
2740
  return (SNode*)pStmt;
1,102,277✔
2741
_err:
×
2742
  return NULL;
×
2743
}
2744

2745
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
180,439✔
2746
  CHECK_PARSER_STATUS(pCxt);
180,439✔
2747
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
180,439✔
2748
  SAlterDatabaseStmt* pStmt = NULL;
180,439✔
2749
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
180,439✔
2750
  CHECK_MAKE_NODE(pStmt);
180,439✔
2751
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
180,439✔
2752
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
180,439✔
2753
  return (SNode*)pStmt;
180,439✔
2754
_err:
×
2755
  nodesDestroyNode(pOptions);
×
2756
  return NULL;
×
2757
}
2758

2759
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
1,725,574✔
2760
  CHECK_PARSER_STATUS(pCxt);
1,725,574✔
2761
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,725,574✔
2762
  SFlushDatabaseStmt* pStmt = NULL;
1,725,574✔
2763
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
1,725,574✔
2764
  CHECK_MAKE_NODE(pStmt);
1,725,574✔
2765
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,725,574✔
2766
  return (SNode*)pStmt;
1,725,574✔
2767
_err:
×
2768
  return NULL;
×
2769
}
2770

2771
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
8,156✔
2772
  CHECK_PARSER_STATUS(pCxt);
8,156✔
2773
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
8,156✔
2774
  STrimDatabaseStmt* pStmt = NULL;
8,156✔
2775
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
8,156✔
2776
  CHECK_MAKE_NODE(pStmt);
8,156✔
2777
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
8,156✔
2778
  pStmt->maxSpeed = maxSpeed;
8,156✔
2779
  return (SNode*)pStmt;
8,156✔
2780
_err:
×
2781
  return NULL;
×
2782
}
2783

2784
SNode* createTrimDbWalStmt(SAstCreateContext* pCxt, SToken* pDbName) {
1,086✔
2785
  CHECK_PARSER_STATUS(pCxt);
1,086✔
2786
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,086✔
2787
  STrimDbWalStmt* pStmt = NULL;
1,086✔
2788
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_WAL_STMT, (SNode**)&pStmt);
1,086✔
2789
  CHECK_MAKE_NODE(pStmt);
1,086✔
2790
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,086✔
2791
  return (SNode*)pStmt;
1,086✔
2792
_err:
×
2793
  return NULL;
×
2794
}
2795

2796
SNode* createSsMigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
×
2797
  CHECK_PARSER_STATUS(pCxt);
×
2798
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
×
2799
  SSsMigrateDatabaseStmt* pStmt = NULL;
×
2800
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SSMIGRATE_DATABASE_STMT, (SNode**)&pStmt);
×
2801
  CHECK_MAKE_NODE(pStmt);
×
2802
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
×
2803
  return (SNode*)pStmt;
×
2804
_err:
×
2805
  return NULL;
×
2806
}
2807

2808
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd, bool metaOnly,
27,316✔
2809
                         bool force) {
2810
  CHECK_PARSER_STATUS(pCxt);
27,316✔
2811
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
27,316✔
2812
  SCompactDatabaseStmt* pStmt = NULL;
27,316✔
2813
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
27,316✔
2814
  CHECK_MAKE_NODE(pStmt);
27,316✔
2815
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
27,316✔
2816
  pStmt->pStart = pStart;
27,316✔
2817
  pStmt->pEnd = pEnd;
27,316✔
2818
  pStmt->metaOnly = metaOnly;
27,316✔
2819
  pStmt->force = force;
27,316✔
2820
  return (SNode*)pStmt;
27,316✔
2821
_err:
×
2822
  nodesDestroyNode(pStart);
×
2823
  nodesDestroyNode(pEnd);
×
2824
  return NULL;
×
2825
}
2826

2827
SNode* createCreateMountStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pMountName, SToken* pDnodeId,
1,386✔
2828
                             SToken* pMountPath) {
2829
#ifdef USE_MOUNT
2830
  CHECK_PARSER_STATUS(pCxt);
1,386✔
2831
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
1,386✔
2832
  CHECK_NAME(checkMountPath(pCxt, pMountPath));
1,386✔
2833
  SCreateMountStmt* pStmt = NULL;
1,386✔
2834
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MOUNT_STMT, (SNode**)&pStmt);
1,386✔
2835
  CHECK_MAKE_NODE(pStmt);
1,386✔
2836
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
1,386✔
2837
  COPY_STRING_FORM_STR_TOKEN(pStmt->mountPath, pMountPath);
1,386✔
2838
  pStmt->ignoreExists = ignoreExists;
1,386✔
2839
  if (TK_NK_INTEGER == pDnodeId->type) {
1,386✔
2840
    pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1,386✔
2841
  } else {
2842
    goto _err;
×
2843
  }
2844
  return (SNode*)pStmt;
1,386✔
2845
_err:
×
2846
  nodesDestroyNode((SNode*)pStmt);
×
2847
  return NULL;
×
2848
#else
2849
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
2850
  return NULL;
2851
#endif
2852
}
2853

2854
SNode* createDropMountStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pMountName) {
504✔
2855
#ifdef USE_MOUNT
2856
  CHECK_PARSER_STATUS(pCxt);
504✔
2857
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
504✔
2858
  SDropMountStmt* pStmt = NULL;
504✔
2859
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_MOUNT_STMT, (SNode**)&pStmt);
504✔
2860
  CHECK_MAKE_NODE(pStmt);
504✔
2861
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
504✔
2862
  pStmt->ignoreNotExists = ignoreNotExists;
504✔
2863
  return (SNode*)pStmt;
504✔
2864
_err:
×
2865
  return NULL;
×
2866
#else
2867
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
2868
  return NULL;
2869
#endif
2870
}
2871

2872
SNode* createCompactVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
4,446✔
2873
                                SNode* pEnd, bool metaOnly, bool force) {
2874
  CHECK_PARSER_STATUS(pCxt);
4,446✔
2875
  if (NULL == pDbName) {
4,446✔
2876
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
741✔
2877
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
741✔
2878
    CHECK_PARSER_STATUS(pCxt);
741✔
2879
  }
2880
  SCompactVgroupsStmt* pStmt = NULL;
3,705✔
2881
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_VGROUPS_STMT, (SNode**)&pStmt);
3,705✔
2882
  CHECK_MAKE_NODE(pStmt);
3,705✔
2883
  pStmt->pDbName = pDbName;
3,705✔
2884
  pStmt->vgidList = vgidList;
3,705✔
2885
  pStmt->pStart = pStart;
3,705✔
2886
  pStmt->pEnd = pEnd;
3,705✔
2887
  pStmt->metaOnly = metaOnly;
3,705✔
2888
  pStmt->force = force;
3,705✔
2889
  return (SNode*)pStmt;
3,705✔
2890
_err:
741✔
2891
  nodesDestroyNode(pDbName);
741✔
2892
  nodesDestroyList(vgidList);
741✔
2893
  nodesDestroyNode(pStart);
741✔
2894
  nodesDestroyNode(pEnd);
741✔
2895
  return NULL;
741✔
2896
}
2897

2898
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
44,322,114✔
2899
  CHECK_PARSER_STATUS(pCxt);
44,322,114✔
2900
  STableOptions* pOptions = NULL;
44,325,064✔
2901
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
44,327,619✔
2902
  CHECK_MAKE_NODE(pOptions);
44,327,667✔
2903
  pOptions->maxDelay1 = -1;
44,327,667✔
2904
  pOptions->maxDelay2 = -1;
44,325,223✔
2905
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
44,323,547✔
2906
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
44,320,003✔
2907
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
44,323,373✔
2908
  pOptions->keep = -1;
44,327,605✔
2909
  pOptions->virtualStb = false;
44,316,752✔
2910
  pOptions->commentNull = true;  // mark null
44,317,021✔
2911
  return (SNode*)pOptions;
44,320,324✔
2912
_err:
×
2913
  return NULL;
×
2914
}
2915

2916
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
68,320✔
2917
  CHECK_PARSER_STATUS(pCxt);
68,320✔
2918
  STableOptions* pOptions = NULL;
68,320✔
2919
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
68,320✔
2920
  CHECK_MAKE_NODE(pOptions);
68,320✔
2921
  pOptions->ttl = -1;
68,320✔
2922
  pOptions->commentNull = true;  // mark null
68,320✔
2923
  pOptions->keep = -1;
68,320✔
2924
  return (SNode*)pOptions;
68,320✔
2925
_err:
×
2926
  return NULL;
×
2927
}
2928

2929
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
504,423✔
2930
  CHECK_PARSER_STATUS(pCxt);
504,423✔
2931
  switch (type) {
504,423✔
2932
    case TABLE_OPTION_COMMENT:
48,970✔
2933
      if (checkComment(pCxt, (SToken*)pVal, true)) {
48,970✔
2934
        ((STableOptions*)pOptions)->commentNull = false;
40,715✔
2935
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
40,715✔
2936
      }
2937
      break;
48,970✔
2938
    case TABLE_OPTION_MAXDELAY:
×
2939
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
×
2940
      break;
×
2941
    case TABLE_OPTION_WATERMARK:
×
2942
      ((STableOptions*)pOptions)->pWatermark = pVal;
×
2943
      break;
×
2944
    case TABLE_OPTION_ROLLUP:
×
2945
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
×
2946
      break;
×
2947
    case TABLE_OPTION_TTL: {
103,908✔
2948
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
103,908✔
2949
      if (ttl > INT32_MAX) {
103,908✔
2950
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
723✔
2951
      } else {
2952
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
2953
        ((STableOptions*)pOptions)->ttl = ttl;
103,185✔
2954
      }
2955
      break;
103,908✔
2956
    }
2957
    case TABLE_OPTION_SMA:
239,214✔
2958
      ((STableOptions*)pOptions)->pSma = pVal;
239,214✔
2959
      break;
239,214✔
2960
    case TABLE_OPTION_DELETE_MARK:
×
2961
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
×
2962
      break;
×
2963
    case TABLE_OPTION_KEEP:
72,760✔
2964
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
72,760✔
2965
        ((STableOptions*)pOptions)->keep = taosStr2Int32(((SToken*)pVal)->z, NULL, 10) * 1440;
11,673✔
2966
      } else {
2967
        ((STableOptions*)pOptions)->pKeepNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
61,087✔
2968
      }
2969
      break;
72,760✔
2970
    case TABLE_OPTION_VIRTUAL: {
39,571✔
2971
      int64_t virtualStb = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
39,571✔
2972
      if (virtualStb != 0 && virtualStb != 1) {
39,571✔
2973
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
2974
      } else {
2975
        ((STableOptions*)pOptions)->virtualStb = virtualStb;
39,571✔
2976
      }
2977
      break;
39,571✔
2978
    }
2979
    default:
×
2980
      break;
×
2981
  }
2982
  return pOptions;
504,423✔
2983
_err:
×
2984
  nodesDestroyNode(pOptions);
×
2985
  return NULL;
×
2986
}
2987

2988
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
397,831,537✔
2989
  CHECK_PARSER_STATUS(pCxt);
397,831,537✔
2990
  SColumnOptions* pOptions = NULL;
397,831,454✔
2991
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
397,831,454✔
2992
  CHECK_MAKE_NODE(pOptions);
397,831,537✔
2993
  pOptions->commentNull = true;
397,831,537✔
2994
  pOptions->bPrimaryKey = false;
397,831,454✔
2995
  pOptions->hasRef = false;
397,831,537✔
2996
  return (SNode*)pOptions;
397,831,537✔
2997
_err:
×
2998
  return NULL;
×
2999
}
3000

3001
EColumnOptionType getColumnOptionType(const char* optionType) {
2,122,851✔
3002
  if (0 == strcasecmp(optionType, "ENCODE")) {
2,122,851✔
3003
    return COLUMN_OPTION_ENCODE;
573,016✔
3004
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
1,549,835✔
3005
    return COLUMN_OPTION_COMPRESS;
779,312✔
3006
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
770,523✔
3007
    return COLUMN_OPTION_LEVEL;
768,421✔
3008
  }
3009
  return 0;
2,102✔
3010
}
3011

3012
SNode* setColumnReference(SAstCreateContext* pCxt, SNode* pOptions, SNode* pRef) {
89,546,627✔
3013
  CHECK_PARSER_STATUS(pCxt);
89,546,627✔
3014

3015
  ((SColumnOptions*)pOptions)->hasRef = true;
89,546,627✔
3016
  tstrncpy(((SColumnOptions*)pOptions)->refDb, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
89,546,627✔
3017
  tstrncpy(((SColumnOptions*)pOptions)->refTable, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
89,546,627✔
3018
  tstrncpy(((SColumnOptions*)pOptions)->refColumn, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
89,546,627✔
3019
  return pOptions;
89,546,627✔
3020
_err:
×
3021
  nodesDestroyNode(pOptions);
×
3022
  return NULL;
×
3023
}
3024

3025
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
230,711✔
3026
  CHECK_PARSER_STATUS(pCxt);
230,711✔
3027
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
230,711✔
3028
  return pOptions;
230,711✔
3029
_err:
×
3030
  nodesDestroyNode(pOptions);
×
3031
  return NULL;
×
3032
}
3033

3034
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
2,122,851✔
3035
  CHECK_PARSER_STATUS(pCxt);
2,122,851✔
3036
  char optionType[TSDB_CL_OPTION_LEN];
2,105,895✔
3037

3038
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
2,122,851✔
3039
  strncpy(optionType, pVal1->z, TMIN(pVal1->n, TSDB_CL_OPTION_LEN));
2,122,851✔
3040
  if (0 == strlen(optionType)) {
2,122,851✔
3041
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3042
    return pOptions;
×
3043
  }
3044
  EColumnOptionType type = getColumnOptionType(optionType);
2,122,851✔
3045
  switch (type) {
2,122,851✔
3046
    case COLUMN_OPTION_ENCODE:
573,016✔
3047
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
573,016✔
3048
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
573,016✔
3049
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
573,016✔
3050
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
3051
      }
3052
      break;
573,016✔
3053
    case COLUMN_OPTION_COMPRESS:
779,312✔
3054
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
779,312✔
3055
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
779,312✔
3056
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
779,312✔
3057
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
3058
      }
3059
      break;
779,312✔
3060
    case COLUMN_OPTION_LEVEL:
768,421✔
3061
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
768,421✔
3062
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
768,421✔
3063
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
768,421✔
3064
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
3065
      }
3066
      break;
768,421✔
3067
    default:
2,102✔
3068
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2,102✔
3069
      break;
2,102✔
3070
  }
3071
  return pOptions;
2,122,851✔
3072
_err:
×
3073
  nodesDestroyNode(pOptions);
×
3074
  return NULL;
×
3075
}
3076

3077
SNode* createColumnRefNodeByNode(SAstCreateContext* pCxt, SToken* pColName, SNode* pRef) {
53,916,236✔
3078
  CHECK_PARSER_STATUS(pCxt);
53,916,236✔
3079
  CHECK_NAME(checkColumnName(pCxt, pColName));
53,916,236✔
3080

3081
  SColumnRefNode* pCol = NULL;
53,916,236✔
3082
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
53,916,236✔
3083
  CHECK_MAKE_NODE(pCol);
53,916,236✔
3084
  if (pColName) {
53,916,236✔
3085
    COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
53,916,236✔
3086
  }
3087
  tstrncpy(pCol->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
53,916,236✔
3088
  tstrncpy(pCol->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
53,916,236✔
3089
  tstrncpy(pCol->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
53,916,236✔
3090
  return (SNode*)pCol;
53,916,236✔
3091
_err:
×
3092
  return NULL;
×
3093
}
3094

3095
STokenTriplet* createTokenTriplet(SAstCreateContext* pCxt, SToken pName) {
144,234,387✔
3096
  CHECK_PARSER_STATUS(pCxt);
144,234,387✔
3097

3098
  STokenTriplet* pTokenTri = taosMemoryMalloc(sizeof(STokenTriplet));
144,234,387✔
3099
  CHECK_OUT_OF_MEM(pTokenTri);
144,234,387✔
3100
  pTokenTri->name[0] = pName;
144,234,387✔
3101
  pTokenTri->numOfName = 1;
144,234,387✔
3102

3103
  return pTokenTri;
144,234,387✔
3104
_err:
×
3105
  return NULL;
×
3106
}
3107

3108
STokenTriplet* setColumnName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri, SToken pName) {
144,460,254✔
3109
  CHECK_PARSER_STATUS(pCxt);
144,460,254✔
3110

3111
  if (pTokenTri->numOfName >= 3) {
144,460,254✔
3112
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3113
    goto _err;
×
3114
  }
3115

3116
  pTokenTri->name[pTokenTri->numOfName] = pName;
144,460,254✔
3117
  pTokenTri->numOfName++;
144,460,254✔
3118
  return pTokenTri;
144,460,254✔
3119
_err:
×
3120
  return NULL;
×
3121
}
3122

3123
SNode* createColumnRefNodeByName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri) {
144,234,387✔
3124
  SColumnRefNode* pCol = NULL;
144,234,387✔
3125
  CHECK_PARSER_STATUS(pCxt);
144,234,387✔
3126
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
144,234,387✔
3127
  CHECK_MAKE_NODE(pCol);
144,234,387✔
3128

3129
  switch (pTokenTri->numOfName) {
144,234,387✔
3130
    case 2: {
144,008,520✔
3131
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[0]));
144,008,520✔
3132
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[1]));
144,008,520✔
3133
      snprintf(pCol->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
144,008,520✔
3134
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[0]);
144,008,520✔
3135
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[1]);
144,008,520✔
3136
      break;
144,008,520✔
3137
    }
3138
    case 3: {
225,867✔
3139
      CHECK_NAME(checkDbName(pCxt, &pTokenTri->name[0], true));
225,867✔
3140
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[1]));
225,867✔
3141
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[2]));
225,867✔
3142
      COPY_STRING_FORM_ID_TOKEN(pCol->refDbName, &pTokenTri->name[0]);
225,867✔
3143
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[1]);
225,867✔
3144
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[2]);
225,867✔
3145
      break;
225,867✔
3146
    }
3147
    default: {
×
3148
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3149
      goto _err;
×
3150
    }
3151
  }
3152

3153
  taosMemFree(pTokenTri);
144,234,387✔
3154
  return (SNode*)pCol;
144,234,387✔
3155
_err:
×
3156
  taosMemFree(pTokenTri);
×
3157
  nodesDestroyNode((SNode*)pCol);
×
3158
  return NULL;
×
3159
}
3160

3161
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
396,220,715✔
3162
  CHECK_PARSER_STATUS(pCxt);
396,220,715✔
3163
  CHECK_NAME(checkColumnName(pCxt, pColName));
396,220,798✔
3164
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
396,218,395✔
3165
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
3,705✔
3166
    CHECK_PARSER_STATUS(pCxt);
3,705✔
3167
  }
3168
  SColumnDefNode* pCol = NULL;
396,214,690✔
3169
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
396,214,690✔
3170
  CHECK_MAKE_NODE(pCol);
396,214,690✔
3171
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
396,214,690✔
3172
  pCol->dataType = dataType;
396,214,607✔
3173
  pCol->pOptions = pNode;
396,214,690✔
3174
  pCol->sma = true;
396,214,607✔
3175
  return (SNode*)pCol;
396,214,690✔
3176
_err:
6,108✔
3177
  nodesDestroyNode(pNode);
6,108✔
3178
  return NULL;
6,108✔
3179
}
3180

3181
SDataType createDataType(uint8_t type) {
399,964,640✔
3182
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
399,964,640✔
3183
  return dt;
399,964,557✔
3184
}
3185

3186
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
108,084,390✔
3187
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
108,084,390✔
3188
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
108,084,390✔
3189
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
108,084,390✔
3190
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
108,084,390✔
3191
  return dt;
108,084,390✔
3192
}
3193

3194
SDataType createDecimalDataType(uint8_t type, const SToken* pPrecisionToken, const SToken* pScaleToken) {
219,518✔
3195
  SDataType dt = {0};
219,518✔
3196
  dt.precision = taosStr2UInt8(pPrecisionToken->z, NULL, 10);
219,518✔
3197
  dt.scale = pScaleToken ? taosStr2Int32(pScaleToken->z, NULL, 10) : 0;
219,518✔
3198
  dt.type = decimalTypeFromPrecision(dt.precision);
219,518✔
3199
  dt.bytes = tDataTypes[dt.type].bytes;
219,518✔
3200
  return dt;
219,518✔
3201
}
3202

3203
SNode* createCreateVTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols) {
152,779✔
3204
  SCreateVTableStmt* pStmt = NULL;
152,779✔
3205
  CHECK_PARSER_STATUS(pCxt);
152,779✔
3206
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
152,779✔
3207
  CHECK_MAKE_NODE(pStmt);
152,779✔
3208
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
152,779✔
3209
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
152,779✔
3210
  pStmt->ignoreExists = ignoreExists;
152,779✔
3211
  pStmt->pCols = pCols;
152,779✔
3212
  nodesDestroyNode(pRealTable);
152,779✔
3213
  return (SNode*)pStmt;
152,779✔
3214
_err:
×
3215
  nodesDestroyNode(pRealTable);
×
3216
  nodesDestroyList(pCols);
×
3217
  return NULL;
×
3218
}
3219

3220
SNode* createCreateVSubTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable,
241,741✔
3221
                                 SNodeList* pSpecificColRefs, SNodeList* pColRefs, SNode* pUseRealTable,
3222
                                 SNodeList* pSpecificTags, SNodeList* pValsOfTags) {
3223
  CHECK_PARSER_STATUS(pCxt);
241,741✔
3224
  SCreateVSubTableStmt* pStmt = NULL;
241,741✔
3225
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT, (SNode**)&pStmt);
241,741✔
3226
  CHECK_MAKE_NODE(pStmt);
241,741✔
3227
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
241,741✔
3228
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
241,741✔
3229
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
241,741✔
3230
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
241,741✔
3231
  pStmt->ignoreExists = ignoreExists;
241,741✔
3232
  pStmt->pSpecificTags = pSpecificTags;
241,741✔
3233
  pStmt->pValsOfTags = pValsOfTags;
241,741✔
3234
  pStmt->pSpecificColRefs = pSpecificColRefs;
241,741✔
3235
  pStmt->pColRefs = pColRefs;
241,741✔
3236
  nodesDestroyNode(pRealTable);
241,741✔
3237
  nodesDestroyNode(pUseRealTable);
241,741✔
3238
  return (SNode*)pStmt;
241,741✔
3239
_err:
×
3240
  nodesDestroyNode(pRealTable);
×
3241
  nodesDestroyNode(pUseRealTable);
×
3242
  nodesDestroyList(pSpecificTags);
×
3243
  nodesDestroyList(pValsOfTags);
×
3244
  nodesDestroyList(pSpecificColRefs);
×
3245
  nodesDestroyList(pColRefs);
×
3246
  return NULL;
×
3247
}
3248

3249
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
6,936,762✔
3250
                             SNodeList* pTags, SNode* pOptions) {
3251
  CHECK_PARSER_STATUS(pCxt);
6,936,762✔
3252
  SCreateTableStmt* pStmt = NULL;
6,935,480✔
3253
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
6,935,480✔
3254
  CHECK_MAKE_NODE(pStmt);
6,935,480✔
3255
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
6,935,480✔
3256
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
6,935,480✔
3257
  pStmt->ignoreExists = ignoreExists;
6,935,480✔
3258
  pStmt->pCols = pCols;
6,935,480✔
3259
  pStmt->pTags = pTags;
6,935,480✔
3260
  pStmt->pOptions = (STableOptions*)pOptions;
6,935,480✔
3261
  nodesDestroyNode(pRealTable);
6,935,480✔
3262
  return (SNode*)pStmt;
6,935,480✔
3263
_err:
1,282✔
3264
  nodesDestroyNode(pRealTable);
1,282✔
3265
  nodesDestroyList(pCols);
1,282✔
3266
  nodesDestroyList(pTags);
1,282✔
3267
  nodesDestroyNode(pOptions);
1,282✔
3268
  return NULL;
1,282✔
3269
}
3270

3271
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
37,377,933✔
3272
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
3273
  CHECK_PARSER_STATUS(pCxt);
37,377,933✔
3274
  SCreateSubTableClause* pStmt = NULL;
37,379,581✔
3275
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
37,379,170✔
3276
  CHECK_MAKE_NODE(pStmt);
37,384,130✔
3277
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
37,384,130✔
3278
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
37,376,241✔
3279
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
37,377,280✔
3280
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
37,374,134✔
3281
  pStmt->ignoreExists = ignoreExists;
37,374,257✔
3282
  pStmt->pSpecificTags = pSpecificTags;
37,373,055✔
3283
  pStmt->pValsOfTags = pValsOfTags;
37,374,766✔
3284
  pStmt->pOptions = (STableOptions*)pOptions;
37,362,623✔
3285
  nodesDestroyNode(pRealTable);
37,365,379✔
3286
  nodesDestroyNode(pUseRealTable);
37,372,519✔
3287
  return (SNode*)pStmt;
37,378,645✔
3288
_err:
×
3289
  nodesDestroyNode(pRealTable);
×
3290
  nodesDestroyNode(pOptions);
×
3291
  nodesDestroyNode(pUseRealTable);
×
3292
  nodesDestroyList(pSpecificTags);
×
3293
  nodesDestroyList(pValsOfTags);
×
3294
  return NULL;
×
3295
}
3296

3297
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
1,310✔
3298
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
3299
  CHECK_PARSER_STATUS(pCxt);
1,310✔
3300
  SCreateSubTableFromFileClause* pStmt = NULL;
1,310✔
3301
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
1,310✔
3302
  CHECK_MAKE_NODE(pStmt);
1,310✔
3303
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
1,310✔
3304
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
1,310✔
3305
  pStmt->ignoreExists = ignoreExists;
1,310✔
3306
  pStmt->pSpecificTags = pSpecificTags;
1,310✔
3307
  if (TK_NK_STRING == pFilePath->type) {
1,310✔
3308
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
1,310✔
3309
  } else {
3310
    strncpy(pStmt->filePath, pFilePath->z, pFilePath->n);
×
3311
  }
3312

3313
  nodesDestroyNode(pUseRealTable);
1,310✔
3314
  return (SNode*)pStmt;
1,310✔
3315
_err:
×
3316
  nodesDestroyNode(pUseRealTable);
×
3317
  nodesDestroyList(pSpecificTags);
×
3318
  return NULL;
×
3319
}
3320

3321
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
31,180,092✔
3322
  CHECK_PARSER_STATUS(pCxt);
31,180,092✔
3323
  SCreateMultiTablesStmt* pStmt = NULL;
31,177,924✔
3324
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
31,179,088✔
3325
  CHECK_MAKE_NODE(pStmt);
31,179,962✔
3326
  pStmt->pSubTables = pSubTables;
31,179,962✔
3327
  return (SNode*)pStmt;
31,183,066✔
3328
_err:
×
3329
  nodesDestroyList(pSubTables);
×
3330
  return NULL;
×
3331
}
3332

3333
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
2,180,324✔
3334
  CHECK_PARSER_STATUS(pCxt);
2,180,324✔
3335
  SDropTableClause* pStmt = NULL;
2,180,141✔
3336
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
2,180,141✔
3337
  CHECK_MAKE_NODE(pStmt);
2,180,141✔
3338
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2,180,141✔
3339
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2,180,141✔
3340
  pStmt->ignoreNotExists = ignoreNotExists;
2,180,141✔
3341
  nodesDestroyNode(pRealTable);
2,180,141✔
3342
  return (SNode*)pStmt;
2,180,141✔
3343
_err:
183✔
3344
  nodesDestroyNode(pRealTable);
183✔
3345
  return NULL;
183✔
3346
}
3347

3348
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
2,066,470✔
3349
  CHECK_PARSER_STATUS(pCxt);
2,066,470✔
3350
  SDropTableStmt* pStmt = NULL;
2,066,287✔
3351
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
2,066,287✔
3352
  CHECK_MAKE_NODE(pStmt);
2,066,287✔
3353
  pStmt->pTables = pTables;
2,066,287✔
3354
  pStmt->withOpt = withOpt;
2,066,287✔
3355
  return (SNode*)pStmt;
2,066,287✔
3356
_err:
183✔
3357
  nodesDestroyList(pTables);
183✔
3358
  return NULL;
183✔
3359
}
3360

3361
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
84,688✔
3362
  CHECK_PARSER_STATUS(pCxt);
84,688✔
3363
  SDropSuperTableStmt* pStmt = NULL;
84,688✔
3364
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
84,688✔
3365
  CHECK_MAKE_NODE(pStmt);
84,688✔
3366
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
84,688✔
3367
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
84,688✔
3368
  pStmt->ignoreNotExists = ignoreNotExists;
84,688✔
3369
  pStmt->withOpt = withOpt;
84,688✔
3370
  nodesDestroyNode(pRealTable);
84,688✔
3371
  return (SNode*)pStmt;
84,688✔
3372
_err:
×
3373
  nodesDestroyNode(pRealTable);
×
3374
  return NULL;
×
3375
}
3376

3377
SNode* createDropVirtualTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
70,372✔
3378
  CHECK_PARSER_STATUS(pCxt);
70,372✔
3379
  SDropVirtualTableStmt* pStmt = NULL;
70,372✔
3380
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
70,372✔
3381
  CHECK_MAKE_NODE(pStmt);
70,372✔
3382
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
70,372✔
3383
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
70,372✔
3384
  pStmt->ignoreNotExists = ignoreNotExists;
70,372✔
3385
  pStmt->withOpt = withOpt;
70,372✔
3386
  nodesDestroyNode(pRealTable);
70,372✔
3387
  return (SNode*)pStmt;
70,372✔
3388
_err:
×
3389
  nodesDestroyNode(pRealTable);
×
3390
  return NULL;
×
3391
}
3392

3393
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
19,098,875✔
3394
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
19,098,875✔
3395
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
19,098,875✔
3396
  nodesDestroyNode(pRealTable);
19,098,875✔
3397
  return (SNode*)pStmt;
19,098,875✔
3398
}
3399

3400
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
68,320✔
3401
  CHECK_PARSER_STATUS(pCxt);
68,320✔
3402
  SAlterTableStmt* pStmt = NULL;
60,624✔
3403
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
60,624✔
3404
  CHECK_MAKE_NODE(pStmt);
60,624✔
3405
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
60,624✔
3406
  pStmt->pOptions = (STableOptions*)pOptions;
60,624✔
3407
  return createAlterTableStmtFinalize(pRealTable, pStmt);
60,624✔
3408
_err:
7,696✔
3409
  nodesDestroyNode(pRealTable);
7,696✔
3410
  nodesDestroyNode(pOptions);
7,696✔
3411
  return NULL;
7,696✔
3412
}
3413

3414
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
2,566,310✔
3415
                                    SDataType dataType) {
3416
  CHECK_PARSER_STATUS(pCxt);
2,566,310✔
3417
  CHECK_NAME(checkColumnName(pCxt, pColName));
2,566,310✔
3418
  SAlterTableStmt* pStmt = NULL;
2,566,127✔
3419
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
2,566,127✔
3420
  CHECK_MAKE_NODE(pStmt);
2,566,127✔
3421
  pStmt->alterType = alterType;
2,566,127✔
3422
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
2,566,127✔
3423
  pStmt->dataType = dataType;
2,566,127✔
3424
  return createAlterTableStmtFinalize(pRealTable, pStmt);
2,566,127✔
3425
_err:
183✔
3426
  nodesDestroyNode(pRealTable);
183✔
3427
  return NULL;
183✔
3428
}
3429

3430
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
6,162,719✔
3431
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
3432
  SAlterTableStmt* pStmt = NULL;
6,162,719✔
3433
  CHECK_PARSER_STATUS(pCxt);
6,162,719✔
3434
  CHECK_NAME(checkColumnName(pCxt, pColName));
6,160,617✔
3435
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
6,160,434✔
3436
  CHECK_MAKE_NODE(pStmt);
6,160,434✔
3437
  pStmt->alterType = alterType;
6,160,434✔
3438
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
6,160,434✔
3439
  pStmt->dataType = dataType;
6,160,434✔
3440
  pStmt->pColOptions = (SColumnOptions*)pOptions;
6,160,434✔
3441

3442
  if (pOptions != NULL) {
6,160,434✔
3443
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
6,160,434✔
3444
    if (pOption->hasRef) {
6,160,434✔
3445
      if (!pOption->commentNull || pOption->bPrimaryKey || 0 != strcmp(pOption->compress, "") ||
33,696✔
3446
          0 != strcmp(pOption->encode, "") || 0 != strcmp(pOption->compressLevel, "")) {
33,696✔
3447
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
×
3448
      }
3449
      pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF;
33,696✔
3450
      tstrncpy(pStmt->refDbName, pOption->refDb, TSDB_DB_NAME_LEN);
33,696✔
3451
      tstrncpy(pStmt->refTableName, pOption->refTable, TSDB_TABLE_NAME_LEN);
33,696✔
3452
      tstrncpy(pStmt->refColName, pOption->refColumn, TSDB_COL_NAME_LEN);
33,696✔
3453
      CHECK_PARSER_STATUS(pCxt);
33,696✔
3454
    } else if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
6,126,738✔
3455
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
6,124,138✔
3456
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
264,780✔
3457
      } else {
3458
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
3459
        //                                         "not support alter column with option except compress");
3460
        // return NULL;
3461
      }
3462
    } else {
3463
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
2,600✔
3464
                                              "not support alter column with option except compress");
3465
      CHECK_PARSER_STATUS(pCxt);
2,600✔
3466
    }
3467
  }
3468
  return createAlterTableStmtFinalize(pRealTable, pStmt);
6,157,834✔
3469
_err:
4,885✔
3470
  nodesDestroyNode(pOptions);
4,885✔
3471
  nodesDestroyNode((SNode*)pStmt);
4,885✔
3472
  nodesDestroyNode(pRealTable);
4,885✔
3473
  return NULL;
4,885✔
3474
}
3475

3476
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
108,373✔
3477
                                           SToken* pColName, SNode* pOptions) {
3478
  CHECK_PARSER_STATUS(pCxt);
108,373✔
3479
  CHECK_NAME(checkColumnName(pCxt, pColName));
108,373✔
3480
  SAlterTableStmt* pStmt = NULL;
108,373✔
3481
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
108,373✔
3482
  CHECK_MAKE_NODE(pStmt);
108,373✔
3483
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
108,373✔
3484
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
108,373✔
3485
  pStmt->pColOptions = (SColumnOptions*)pOptions;
108,373✔
3486
  return createAlterTableStmtFinalize(pRealTable, pStmt);
108,373✔
3487
_err:
×
3488
  nodesDestroyNode(pOptions);
×
3489
  nodesDestroyNode(pRealTable);
×
3490
  return NULL;
×
3491
}
3492

3493
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
1,733,868✔
3494
  CHECK_PARSER_STATUS(pCxt);
1,733,868✔
3495
  CHECK_NAME(checkColumnName(pCxt, pColName));
1,733,868✔
3496
  SAlterTableStmt* pStmt = NULL;
1,733,502✔
3497
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
1,733,502✔
3498
  CHECK_MAKE_NODE(pStmt);
1,733,502✔
3499
  pStmt->alterType = alterType;
1,733,502✔
3500
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1,733,502✔
3501
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1,733,502✔
3502
_err:
366✔
3503
  nodesDestroyNode(pRealTable);
366✔
3504
  return NULL;
366✔
3505
}
3506

3507
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
189,256✔
3508
                                 SToken* pNewColName) {
3509
  CHECK_PARSER_STATUS(pCxt);
189,256✔
3510
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
189,256✔
3511
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
189,256✔
3512
  SAlterTableStmt* pStmt = NULL;
188,449✔
3513
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
188,449✔
3514
  CHECK_MAKE_NODE(pStmt);
188,449✔
3515
  pStmt->alterType = alterType;
188,449✔
3516
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
188,449✔
3517
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
188,449✔
3518
  return createAlterTableStmtFinalize(pRealTable, pStmt);
188,449✔
3519
_err:
807✔
3520
  nodesDestroyNode(pRealTable);
807✔
3521
  return NULL;
807✔
3522
}
3523

3524
SNode* createAlterTableAlterColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
108,632✔
3525
                                   SNode* pRef) {
3526
  CHECK_PARSER_STATUS(pCxt);
108,632✔
3527
  CHECK_NAME(checkColumnName(pCxt, pColName));
108,632✔
3528
  SAlterTableStmt* pStmt = NULL;
108,632✔
3529
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
108,632✔
3530
  CHECK_MAKE_NODE(pStmt);
108,632✔
3531
  pStmt->alterType = alterType;
108,632✔
3532
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
108,632✔
3533
  tstrncpy(pStmt->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
108,632✔
3534
  tstrncpy(pStmt->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
108,632✔
3535
  tstrncpy(pStmt->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
108,632✔
3536
  return createAlterTableStmtFinalize(pRealTable, pStmt);
108,632✔
3537
_err:
×
3538
  nodesDestroyNode(pRealTable);
×
3539
  return NULL;
×
3540
}
3541

3542
SNode* createAlterTableRemoveColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
63,582✔
3543
                                    const SToken* pLiteral) {
3544
  CHECK_PARSER_STATUS(pCxt);
63,582✔
3545
  CHECK_NAME(checkColumnName(pCxt, pColName));
63,582✔
3546
  SAlterTableStmt* pStmt = NULL;
63,582✔
3547
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
63,582✔
3548
  CHECK_MAKE_NODE(pStmt);
63,582✔
3549
  pStmt->alterType = alterType;
63,582✔
3550
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
63,582✔
3551
  return createAlterTableStmtFinalize(pRealTable, pStmt);
63,582✔
3552
_err:
×
3553
  nodesDestroyNode(pRealTable);
×
3554
  return NULL;
×
3555
}
3556

3557
SNode* createAlterSingleTagColumnNode(SAstCreateContext* pCtx, SToken* pTagName, SNode* pVal) {
8,146,043✔
3558
  CHECK_PARSER_STATUS(pCtx);
8,146,043✔
3559
  SAlterTableStmt* pStmt = NULL;
8,146,043✔
3560
  pCtx->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
8,146,043✔
3561
  CHECK_MAKE_NODE(pStmt);
8,146,043✔
3562
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
8,146,043✔
3563
  CHECK_NAME(checkColumnName(pCtx, pTagName));
8,146,043✔
3564
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
8,146,043✔
3565
  pStmt->pVal = (SValueNode*)pVal;
8,146,043✔
3566
  pStmt->pNodeListTagValue = NULL;
8,146,043✔
3567
  return (SNode*)pStmt;
8,146,043✔
3568
_err:
×
3569
  return NULL;
×
3570
}
3571

3572
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
×
3573
  CHECK_PARSER_STATUS(pCxt);
×
3574
  CHECK_NAME(checkColumnName(pCxt, pTagName));
×
3575
  SAlterTableStmt* pStmt = NULL;
×
3576
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
3577
  CHECK_MAKE_NODE(pStmt);
×
3578
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
×
3579
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
×
3580
  pStmt->pVal = (SValueNode*)pVal;
×
3581
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
3582
_err:
×
3583
  nodesDestroyNode(pVal);
×
3584
  nodesDestroyNode(pRealTable);
×
3585
  return NULL;
×
3586
}
3587

3588
SNode* createAlterTableSetMultiTagValue(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pList) {
8,111,752✔
3589
  CHECK_PARSER_STATUS(pCxt);
8,111,752✔
3590
  SAlterTableStmt* pStmt = NULL;
8,111,752✔
3591
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
8,111,752✔
3592

3593
  CHECK_MAKE_NODE(pStmt);
8,111,752✔
3594
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL;
8,111,752✔
3595
  pStmt->pNodeListTagValue = pList;
8,111,752✔
3596
  return createAlterTableStmtFinalize(pRealTable, pStmt);
8,111,752✔
3597
_err:
×
3598
  return NULL;
×
3599
}
3600

3601
SNode* setAlterSuperTableType(SNode* pStmt) {
493,938✔
3602
  if (!pStmt) return NULL;
493,938✔
3603
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
493,389✔
3604
  return pStmt;
493,389✔
3605
}
3606

3607
SNode* setAlterVirtualTableType(SNode* pStmt) {
370,881✔
3608
  if (!pStmt) return NULL;
370,881✔
3609
  setNodeType(pStmt, QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT);
370,881✔
3610
  return pStmt;
370,881✔
3611
}
3612

3613
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
2,079,388✔
3614
  CHECK_PARSER_STATUS(pCxt);
2,079,388✔
3615
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
2,080,325✔
3616
  SUseDatabaseStmt* pStmt = NULL;
2,080,325✔
3617
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
2,080,325✔
3618
  CHECK_MAKE_NODE(pStmt);
2,080,325✔
3619
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
2,080,325✔
3620
  return (SNode*)pStmt;
2,079,668✔
3621
_err:
×
3622
  return NULL;
×
3623
}
3624

3625
static bool needDbShowStmt(ENodeType type) {
1,560,584✔
3626
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
1,313,468✔
3627
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
867,937✔
3628
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
219,027✔
3629
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type ||
217,033✔
3630
         QUERY_NODE_SHOW_USAGE_STMT == type || QUERY_NODE_SHOW_VTABLES_STMT == type ||
2,874,052✔
3631
         QUERY_NODE_SHOW_STREAMS_STMT == type;
3632
}
3633

3634
SNode* createShowStmtWithLike(SAstCreateContext* pCxt, ENodeType type, SNode* pLikePattern) {
20,947✔
3635
  CHECK_PARSER_STATUS(pCxt);
20,947✔
3636
  SShowStmt* pStmt = NULL;
20,947✔
3637
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
20,947✔
3638
  CHECK_MAKE_NODE(pStmt);
20,947✔
3639
  pStmt->withFull = false;
20,947✔
3640
  pStmt->pTbName = pLikePattern;
20,947✔
3641
  if (pLikePattern) {
20,947✔
3642
    pStmt->tableCondType = OP_TYPE_LIKE;
3,983✔
3643
  }
3644
  return (SNode*)pStmt;
20,947✔
3645
_err:
×
3646
  nodesDestroyNode(pLikePattern);
×
3647
  return NULL;
×
3648
}
3649

3650
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
1,200,300✔
3651
  CHECK_PARSER_STATUS(pCxt);
1,200,300✔
3652
  SShowStmt* pStmt = NULL;
1,200,358✔
3653
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,200,358✔
3654
  CHECK_MAKE_NODE(pStmt);
1,200,358✔
3655
  pStmt->withFull = false;
1,200,358✔
3656
  return (SNode*)pStmt;
1,200,300✔
3657
_err:
×
3658
  return NULL;
×
3659
}
3660

3661
SNode* createShowXNodeResourcesStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType) {
1,062✔
3662
  CHECK_PARSER_STATUS(pCxt);
1,062✔
3663
  SShowStmt* pStmt = NULL;
1,062✔
3664
  switch (resourceType) {
1,062✔
3665
    case XNODE_TASK:
354✔
3666
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_TASKS_STMT, (SNode**)&pStmt);
354✔
3667
      CHECK_MAKE_NODE(pStmt);
354✔
3668
      break;
354✔
3669
    case XNODE_AGENT:
354✔
3670
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_AGENTS_STMT, (SNode**)&pStmt);
354✔
3671
      CHECK_MAKE_NODE(pStmt);
354✔
3672
      break;
354✔
3673
    case XNODE_JOB:
354✔
3674
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_JOBS_STMT, (SNode**)&pStmt);
354✔
3675
      CHECK_MAKE_NODE(pStmt);
354✔
3676
      break;
354✔
3677
    default:
×
3678
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3679
      goto _err;
×
3680
  }
3681
  return (SNode*)pStmt;
1,062✔
3682
_err:
×
3683
  return NULL;
×
3684
}
3685

3686
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
10,472✔
3687
  CHECK_PARSER_STATUS(pCxt);
10,472✔
3688
  SShowStmt* pStmt = NULL;
10,472✔
3689
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
10,472✔
3690
  CHECK_MAKE_NODE(pStmt);
10,472✔
3691
  pStmt->withFull = true;
10,472✔
3692
  return (SNode*)pStmt;
10,472✔
3693
_err:
×
3694
  return NULL;
×
3695
}
3696

3697
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
320,752✔
3698
  CHECK_PARSER_STATUS(pCxt);
320,752✔
3699
  SShowCompactsStmt* pStmt = NULL;
320,752✔
3700
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
320,752✔
3701
  CHECK_MAKE_NODE(pStmt);
320,752✔
3702
  return (SNode*)pStmt;
320,752✔
3703
_err:
×
3704
  return NULL;
×
3705
}
3706

3707
SNode* createShowSsMigratesStmt(SAstCreateContext* pCxt, ENodeType type) {
×
3708
  CHECK_PARSER_STATUS(pCxt);
×
3709
  SShowSsMigratesStmt* pStmt = NULL;
×
3710
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3711
  CHECK_MAKE_NODE(pStmt);
×
3712
  return (SNode*)pStmt;
×
3713
_err:
×
3714
  return NULL;
×
3715
}
3716

3717
SNode* createShowTokensStmt(SAstCreateContext* pCxt, ENodeType type) {
1,468✔
3718
  CHECK_PARSER_STATUS(pCxt);
1,468✔
3719
  SShowTokensStmt* pStmt = NULL;
1,468✔
3720
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,468✔
3721
  CHECK_MAKE_NODE(pStmt);
1,468✔
3722
  return (SNode*)pStmt;
1,468✔
3723
_err:
×
3724
  return NULL;
×
3725
}
3726

3727
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
688,123✔
3728
  if (pStmt == NULL) {
688,123✔
3729
    return NULL;
×
3730
  }
3731
  SShowStmt* pShow = (SShowStmt*)pStmt;
688,123✔
3732
  pShow->showKind = showKind;
688,123✔
3733
  return pStmt;
688,123✔
3734
}
3735

3736
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
1,455,975✔
3737
                              EOperatorType tableCondType) {
3738
  CHECK_PARSER_STATUS(pCxt);
1,455,975✔
3739
  if (needDbShowStmt(type) && NULL == pDbName) {
1,455,975✔
3740
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
14✔
3741
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
14✔
3742
    CHECK_PARSER_STATUS(pCxt);
14✔
3743
  }
3744
  SShowStmt* pStmt = NULL;
1,455,961✔
3745
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,455,961✔
3746
  CHECK_MAKE_NODE(pStmt);
1,455,961✔
3747
  pStmt->pDbName = pDbName;
1,455,961✔
3748
  pStmt->pTbName = pTbName;
1,455,961✔
3749
  pStmt->tableCondType = tableCondType;
1,455,961✔
3750
  return (SNode*)pStmt;
1,455,961✔
3751
_err:
14✔
3752
  nodesDestroyNode(pDbName);
14✔
3753
  nodesDestroyNode(pTbName);
14✔
3754
  return NULL;
14✔
3755
}
3756

3757
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
247,116✔
3758
                            EOperatorType tableCondType) {
3759
  CHECK_PARSER_STATUS(pCxt);
247,116✔
3760
  SNode* pDbName = NULL;
247,116✔
3761
  if (option.dbName.type == TK_NK_NIL) {
247,116✔
3762
    pDbName = createDefaultDatabaseCondValue(pCxt);
82,767✔
3763
  } else {
3764
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
164,349✔
3765
  }
3766

3767
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
247,116✔
3768
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3769
    return NULL;
×
3770
  }
3771

3772
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
247,116✔
3773
  CHECK_PARSER_STATUS(pCxt);
247,116✔
3774
  (void)setShowKind(pCxt, pStmt, option.kind);
247,116✔
3775
  return pStmt;
247,116✔
3776
_err:
×
3777
  nodesDestroyNode(pTbName);
×
3778
  return NULL;
×
3779
}
3780

3781
SNode* createShowVTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
57,590✔
3782
                             EOperatorType tableCondType) {
3783
  CHECK_PARSER_STATUS(pCxt);
57,590✔
3784
  SNode* pDbName = NULL;
57,590✔
3785
  if (option.dbName.type == TK_NK_NIL) {
57,590✔
3786
    pDbName = createDefaultDatabaseCondValue(pCxt);
3,671✔
3787
  } else {
3788
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
53,919✔
3789
  }
3790

3791
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
57,590✔
3792
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3793
    return NULL;
×
3794
  }
3795

3796
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_VTABLES_STMT, pDbName, pTbName, tableCondType);
57,590✔
3797
  CHECK_PARSER_STATUS(pCxt);
57,590✔
3798
  (void)setShowKind(pCxt, pStmt, option.kind);
57,590✔
3799
  return pStmt;
57,590✔
3800
_err:
×
3801
  nodesDestroyNode(pTbName);
×
3802
  return NULL;
×
3803
}
3804

3805
SNode* createShowSTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
210,385✔
3806
                             EOperatorType tableCondType) {
3807
  CHECK_PARSER_STATUS(pCxt);
210,385✔
3808
  SNode* pDbName = NULL;
210,385✔
3809
  if (option.dbName.type == TK_NK_NIL) {
210,385✔
3810
    pDbName = createDefaultDatabaseCondValue(pCxt);
56,995✔
3811
  } else {
3812
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
153,390✔
3813
  }
3814

3815
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_VIRTUAL &&
210,385✔
3816
      option.kind != SHOW_KIND_ALL) {
209,149✔
3817
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3818
    return NULL;
×
3819
  }
3820

3821
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_STABLES_STMT, pDbName, pTbName, tableCondType);
210,385✔
3822
  CHECK_PARSER_STATUS(pCxt);
210,385✔
3823
  (void)setShowKind(pCxt, pStmt, option.kind);
210,385✔
3824
  return pStmt;
210,385✔
3825
_err:
×
3826
  nodesDestroyNode(pTbName);
×
3827
  return NULL;
×
3828
}
3829

3830
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
52,825✔
3831
  CHECK_PARSER_STATUS(pCxt);
52,825✔
3832
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
52,825✔
3833
  SShowCreateDatabaseStmt* pStmt = NULL;
52,825✔
3834
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
52,825✔
3835
  CHECK_MAKE_NODE(pStmt);
52,825✔
3836
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
52,825✔
3837
  return (SNode*)pStmt;
52,825✔
3838
_err:
×
3839
  return NULL;
×
3840
}
3841

3842
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
5,085✔
3843
  CHECK_PARSER_STATUS(pCxt);
5,085✔
3844
  SToken  dbToken = {0};
5,085✔
3845
  SToken* pDbToken = NULL;
5,085✔
3846

3847
  if (pNode) {
5,085✔
3848
    SValueNode* pDbName = (SValueNode*)pNode;
1,499✔
3849
    if (pDbName->literal) {
1,499✔
3850
      dbToken.z = pDbName->literal;
1,499✔
3851
      dbToken.n = strlen(pDbName->literal);
1,499✔
3852
      pDbToken = &dbToken;
1,499✔
3853
    }
3854
  }
3855

3856
  if (pDbToken) {
5,085✔
3857
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
1,499✔
3858
  }
3859

3860
  SShowAliveStmt* pStmt = NULL;
5,085✔
3861
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
5,085✔
3862
  CHECK_PARSER_STATUS(pCxt);
5,085✔
3863

3864
  if (pDbToken) {
5,085✔
3865
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
1,499✔
3866
  }
3867
  if (pNode) {
5,085✔
3868
    nodesDestroyNode(pNode);
1,499✔
3869
  }
3870

3871
  return (SNode*)pStmt;
5,085✔
3872
_err:
×
3873
  nodesDestroyNode(pNode);
×
3874
  return NULL;
×
3875
}
3876

3877
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
73,218✔
3878
  CHECK_PARSER_STATUS(pCxt);
73,218✔
3879
  SShowCreateTableStmt* pStmt = NULL;
72,852✔
3880
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
72,852✔
3881
  CHECK_MAKE_NODE(pStmt);
72,852✔
3882
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
72,852✔
3883
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
72,852✔
3884
  nodesDestroyNode(pRealTable);
72,852✔
3885
  return (SNode*)pStmt;
72,852✔
3886
_err:
366✔
3887
  nodesDestroyNode(pRealTable);
366✔
3888
  return NULL;
366✔
3889
}
3890

3891
SNode* createShowCreateVTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
2,763✔
3892
  CHECK_PARSER_STATUS(pCxt);
2,763✔
3893
  SShowCreateTableStmt* pStmt = NULL;
2,763✔
3894
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,763✔
3895
  CHECK_MAKE_NODE(pStmt);
2,763✔
3896
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2,763✔
3897
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2,763✔
3898
  nodesDestroyNode(pRealTable);
2,763✔
3899
  return (SNode*)pStmt;
2,763✔
3900
_err:
×
3901
  nodesDestroyNode(pRealTable);
×
3902
  return NULL;
×
3903
}
3904

3905
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
×
3906
  CHECK_PARSER_STATUS(pCxt);
×
3907
  SShowCreateViewStmt* pStmt = NULL;
×
3908
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3909
  CHECK_MAKE_NODE(pStmt);
×
3910
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
3911
  tstrncpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
3912
  nodesDestroyNode(pRealTable);
×
3913
  return (SNode*)pStmt;
×
3914
_err:
×
3915
  nodesDestroyNode(pRealTable);
×
3916
  return NULL;
×
3917
}
3918

3919
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
3,011✔
3920
  CHECK_PARSER_STATUS(pCxt);
3,011✔
3921
  SShowTableDistributedStmt* pStmt = NULL;
3,011✔
3922
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
3,011✔
3923
  CHECK_MAKE_NODE(pStmt);
3,011✔
3924
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
3,011✔
3925
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
3,011✔
3926
  nodesDestroyNode(pRealTable);
3,011✔
3927
  return (SNode*)pStmt;
3,011✔
3928
_err:
×
3929
  nodesDestroyNode(pRealTable);
×
3930
  return NULL;
×
3931
}
3932

3933
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
38,291✔
3934
  CHECK_PARSER_STATUS(pCxt);
38,291✔
3935
  SShowDnodeVariablesStmt* pStmt = NULL;
38,291✔
3936
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
38,291✔
3937
  CHECK_MAKE_NODE(pStmt);
38,291✔
3938
  pStmt->pDnodeId = pDnodeId;
38,291✔
3939
  pStmt->pLikePattern = pLikePattern;
38,291✔
3940
  return (SNode*)pStmt;
38,291✔
3941
_err:
×
3942
  nodesDestroyNode(pDnodeId);
×
3943
  nodesDestroyNode(pLikePattern);
×
3944
  return NULL;
×
3945
}
3946

3947
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
4,065✔
3948
  CHECK_PARSER_STATUS(pCxt);
4,065✔
3949
  SShowVnodesStmt* pStmt = NULL;
4,065✔
3950
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
4,065✔
3951
  CHECK_MAKE_NODE(pStmt);
4,065✔
3952
  pStmt->pDnodeId = pDnodeId;
4,065✔
3953
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
4,065✔
3954
  return (SNode*)pStmt;
4,065✔
3955
_err:
×
3956
  nodesDestroyNode(pDnodeId);
×
3957
  nodesDestroyNode(pDnodeEndpoint);
×
3958
  return NULL;
×
3959
}
3960

3961
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
4,883✔
3962
  CHECK_PARSER_STATUS(pCxt);
4,883✔
3963
  if (NULL == pDbName) {
4,883✔
3964
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
3965
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3966
    CHECK_PARSER_STATUS(pCxt);
×
3967
  }
3968
  SShowTableTagsStmt* pStmt = NULL;
4,883✔
3969
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
4,883✔
3970
  CHECK_MAKE_NODE(pStmt);
4,883✔
3971
  pStmt->pDbName = pDbName;
4,883✔
3972
  pStmt->pTbName = pTbName;
4,883✔
3973
  pStmt->pTags = pTags;
4,883✔
3974
  return (SNode*)pStmt;
4,883✔
3975
_err:
×
3976
  nodesDestroyNode(pTbName);
×
3977
  nodesDestroyNode(pDbName);
×
3978
  nodesDestroyList(pTags);
×
3979
  return NULL;
×
3980
}
3981

3982
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
32,771✔
3983
  CHECK_PARSER_STATUS(pCxt);
32,771✔
3984
  SShowCompactDetailsStmt* pStmt = NULL;
32,771✔
3985
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
32,771✔
3986
  CHECK_MAKE_NODE(pStmt);
32,771✔
3987
  pStmt->pId = pCompactId;
32,771✔
3988
  return (SNode*)pStmt;
32,771✔
3989
_err:
×
3990
  nodesDestroyNode(pCompactId);
×
3991
  return NULL;
×
3992
}
3993

3994
SNode* createShowRetentionDetailsStmt(SAstCreateContext* pCxt, SNode* pId) {
2,223✔
3995
  CHECK_PARSER_STATUS(pCxt);
2,223✔
3996
  SShowRetentionDetailsStmt* pStmt = NULL;
2,223✔
3997
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_RETENTION_DETAILS_STMT, (SNode**)&pStmt);
2,223✔
3998
  CHECK_MAKE_NODE(pStmt);
2,223✔
3999
  pStmt->pId = pId;
2,223✔
4000
  return (SNode*)pStmt;
2,223✔
4001
_err:
×
4002
  nodesDestroyNode(pId);
×
4003
  return NULL;
×
4004
}
4005

4006
SNode* createShowTransactionDetailsStmt(SAstCreateContext* pCxt, SNode* pTransactionIdNode) {
280✔
4007
  CHECK_PARSER_STATUS(pCxt);
280✔
4008
  SShowTransactionDetailsStmt* pStmt = NULL;
280✔
4009
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT, (SNode**)&pStmt);
280✔
4010
  CHECK_MAKE_NODE(pStmt);
280✔
4011
  pStmt->pTransactionId = pTransactionIdNode;
280✔
4012
  return (SNode*)pStmt;
280✔
4013
_err:
×
4014
  nodesDestroyNode(pTransactionIdNode);
×
4015
  return NULL;
×
4016
}
4017

4018

4019

4020
static bool parseIp(const char* strIp, SIpRange* pIpRange) {
4,449✔
4021
  if (strchr(strIp, ':') == NULL) {
4,449✔
4022
    struct in_addr ip4;
4,449✔
4023
    if (inet_pton(AF_INET, strIp, &ip4) == 1) {
4,449✔
4024
      pIpRange->type = 0;
3,605✔
4025
      memcpy(&pIpRange->ipV4.ip, &ip4.s_addr, sizeof(ip4.s_addr));
3,605✔
4026
      return true;
3,605✔
4027
    }
4028
  } else {
4029
    struct in6_addr ip6;
×
4030
    if (inet_pton(AF_INET6, strIp, &ip6) == 1) {
×
4031
      pIpRange->type = 1;
×
4032
      memcpy(&pIpRange->ipV6.addr[0], ip6.s6_addr, 8);
×
4033
      memcpy(&pIpRange->ipV6.addr[1], ip6.s6_addr + 8, 8);
×
4034
      return true;
×
4035
    }
4036
  }
4037

4038
  return false;
844✔
4039
}
4040

4041

4042

4043
SIpRangeNode* parseIpRange(SAstCreateContext* pCxt, const SToken* token) {
4,449✔
4044
  CHECK_PARSER_STATUS(pCxt);
4,449✔
4045

4046
#ifdef TD_ASTRA
4047
  return NULL;
4048
#else
4049

4050
  SIpRangeNode* node = NULL;
4,449✔
4051
  int32_t code = nodesMakeNode(QUERY_NODE_IP_RANGE, (SNode**)&node);
4,449✔
4052
  if (node == NULL) {
4,449✔
4053
    goto _err;
×
4054
  }
4055

4056
  char buf[64];
4,449✔
4057
  if (token->n >= sizeof(buf)) {
4,449✔
4058
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
4059
    goto _err;
×
4060
  }
4061
  memcpy(buf, token->z, token->n);
4,449✔
4062
  buf[token->n] = '\0';
4,449✔
4063
  (void)strdequote(buf);
4,449✔
4064

4065
  char* slash = strchr(buf, '/');
4,449✔
4066
  if (slash) {
4,449✔
4067
    *slash = '\0';
1,528✔
4068
  }
4069

4070
  if (!parseIp(buf, &node->range)) {
4,449✔
4071
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
844✔
4072
    goto _err;
844✔
4073
  }
4074

4075
  int32_t mask = 0;
3,605✔
4076
  if (!slash) {
3,605✔
4077
    mask = node->range.type == 0 ? 32 : 128;
2,241✔
4078
  } else if (taosStr2int32(slash + 1, &mask) != TSDB_CODE_SUCCESS) {
1,364✔
4079
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
4080
    goto _err;
×
4081
  }
4082

4083
  code = tIpRangeSetMask(&node->range, mask);
3,605✔
4084
  if (code != TSDB_CODE_SUCCESS) {
3,605✔
4085
    goto _err;
436✔
4086
  }
4087

4088
  return node;
3,169✔
4089

4090
_err:
1,280✔
4091
  pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
1,280✔
4092
  nodesDestroyNode((SNode*)node);
1,280✔
4093
  return NULL;
1,280✔
4094

4095
#endif
4096
}
4097

4098

4099

4100
SDateTimeRangeNode* parseDateTimeRange(SAstCreateContext* pCxt, const SToken* token) {
2,992✔
4101
  CHECK_PARSER_STATUS(pCxt);
2,992✔
4102

4103
  SDateTimeRangeNode* node = NULL;
2,992✔
4104
  int32_t code = nodesMakeNode(QUERY_NODE_DATE_TIME_RANGE, (SNode**)&node);
2,992✔
4105
  if (code != TSDB_CODE_SUCCESS) {
2,992✔
4106
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
×
4107
    goto _err;
×
4108
  }
4109

4110
  char buf[128];
2,992✔
4111
  if (token->n >= sizeof(buf)) {
2,992✔
4112
    code = TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG;
×
4113
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Date time range string is too long");
×
4114
    goto _err;
×
4115
  }
4116
  memcpy(buf, token->z, token->n);
2,992✔
4117
  buf[token->n] = '\0';
2,992✔
4118
  (void)strdequote(buf);
2,992✔
4119

4120
  code = TSDB_CODE_PAR_INVALID_OPTION_VALUE;
2,992✔
4121
  int32_t year = 0, month = 0, day = 0, hour = 0, minute = 0, duration = 0;
2,992✔
4122
  if (buf[0] >= '1' && buf[0] <= '9') {
4,352✔
4123
    // format: YYYY-MM-DD HH:MM duration
4124
    int ret = sscanf(buf, "%d-%d-%d %d:%d %d", &year, &month, &day, &hour, &minute, &duration);
2,448✔
4125
    if (ret != 6) {
2,448✔
4126
      goto _err;
816✔
4127
    }
4128
    if (month < 1 || month > 12) {
1,632✔
4129
      goto _err;
272✔
4130
    }
4131
  } else {
4132
    // format: WEEKDAY HH:MM duration
4133
    char weekday[4];
544✔
4134
    int ret = sscanf(buf, "%3s %d:%d %d", weekday, &hour, &minute, &duration);
544✔
4135
    if (ret != 4) {
544✔
4136
      goto _err;
272✔
4137
    }
4138
    day = taosParseShortWeekday(weekday);
272✔
4139
    if (day < 0 || day > 6) {
272✔
4140
      goto _err;
×
4141
    }
4142
    month = -1;
272✔
4143
  }
4144

4145
  node->range.year = (int16_t)year;
1,632✔
4146
  node->range.month = (int8_t)month;
1,632✔
4147
  node->range.day = (int8_t)day;
1,632✔
4148
  node->range.hour = (int8_t)hour;
1,632✔
4149
  node->range.minute = (int8_t)minute;
1,632✔
4150
  node->range.duration = duration;
1,632✔
4151
  if (!isValidDateTimeRange(&node->range)) {
1,632✔
4152
    goto _err;
×
4153
  }
4154

4155
  return node;
1,632✔
4156

4157
_err:
1,360✔
4158
  if (code == TSDB_CODE_PAR_INVALID_OPTION_VALUE) { // other error types have been set above
1,360✔
4159
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid date time range");
1,360✔
4160
  }
4161
  nodesDestroyNode((SNode*)node);
1,360✔
4162
  return NULL;
1,360✔
4163
}
4164

4165

4166
SUserOptions* createDefaultUserOptions(SAstCreateContext* pCxt) {
130,726✔
4167
  SUserOptions* pOptions = NULL;
130,726✔
4168
  int32_t code = nodesMakeNode(QUERY_NODE_USER_OPTIONS, (SNode**)&pOptions);
130,726✔
4169
  if (pOptions == NULL) {
130,726✔
4170
    pCxt->errCode = code;
×
4171
    return NULL;
×
4172
  }
4173

4174
  pOptions->enable = 1;
130,726✔
4175
  pOptions->sysinfo = 1;
130,726✔
4176
  pOptions->createdb = 0;
130,726✔
4177
  pOptions->isImport = 0;
130,726✔
4178
  pOptions->changepass = 2;
130,726✔
4179
  pOptions->sessionPerUser = TSDB_USER_SESSION_PER_USER_DEFAULT;
130,726✔
4180
  pOptions->connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
130,726✔
4181
  pOptions->connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
130,726✔
4182
  pOptions->callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
130,726✔
4183
  pOptions->vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
130,726✔
4184
  pOptions->failedLoginAttempts = TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
130,726✔
4185
  pOptions->passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
130,726✔
4186
  pOptions->passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
130,726✔
4187
  pOptions->passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
130,726✔
4188
  pOptions->passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
130,726✔
4189
  pOptions->passwordGraceTime = TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
130,726✔
4190
  pOptions->inactiveAccountTime = TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
130,726✔
4191
  pOptions->allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
130,726✔
4192

4193
  return pOptions;
130,726✔
4194
}
4195

4196

4197

4198
SUserOptions* mergeUserOptions(SAstCreateContext* pCxt, SUserOptions* a, SUserOptions* b) {
157,672✔
4199
  if (a == NULL && b == NULL) {
157,672✔
4200
      return createDefaultUserOptions(pCxt);
130,726✔
4201
  }
4202
  if (b == NULL) {
26,946✔
4203
    return a;
16,654✔
4204
  }
4205
  if (a == NULL) {
10,292✔
4206
    return b;
×
4207
  }
4208

4209
  if (b->hasPassword) {
10,292✔
4210
    if (a->hasPassword) {
×
4211
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASS");
×
4212
    } else {
4213
      a->hasPassword = true;
×
4214
      tstrncpy(a->password, b->password, sizeof(a->password));
×
4215
    }
4216
  }
4217

4218
  if (b->hasTotpseed) {
10,292✔
4219
    if (a->hasTotpseed) {
×
4220
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TOTPSEED");
×
4221
    } else {
4222
      a->hasTotpseed = true;
×
4223
      tstrncpy(a->totpseed, b->totpseed, sizeof(a->totpseed));
×
4224
    }
4225
  }
4226

4227
  if (b->hasEnable) {
10,292✔
4228
    if (a->hasEnable) {
×
4229
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE/ACCOUNT LOCK/ACCOUNT UNLOCK");
×
4230
    } else {
4231
      a->hasEnable = true;
×
4232
      a->enable = b->enable;
×
4233
    }
4234
  }
4235

4236
  if (b->hasSysinfo) {
10,292✔
4237
    if (a->hasSysinfo) {
162✔
4238
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SYSINFO");
×
4239
    } else {
4240
      a->hasSysinfo = true;
162✔
4241
      a->sysinfo = b->sysinfo;
162✔
4242
    }
4243
  }
4244

4245
  if (b->hasCreatedb) {
10,292✔
4246
    if (a->hasCreatedb) {
1,416✔
4247
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CREATEDB");
×
4248
    } else {
4249
      a->hasCreatedb = true;
1,416✔
4250
      a->createdb = b->createdb;
1,416✔
4251
    }
4252
  }
4253

4254
  if (b->hasChangepass) {
10,292✔
4255
    if (a->hasChangepass) {
×
4256
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CHANGEPASS");
×
4257
    } else {
4258
      a->hasChangepass = true;
×
4259
      a->changepass = b->changepass;
×
4260
    }
4261
  }
4262

4263
  if (b->hasSessionPerUser) {
10,292✔
4264
    if (a->hasSessionPerUser) {
×
4265
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SESSION_PER_USER");
×
4266
    } else {
4267
      a->hasSessionPerUser = true;
×
4268
      a->sessionPerUser = b->sessionPerUser;
×
4269
    }
4270
  }
4271

4272
  if (b->hasConnectTime) {
10,292✔
4273
    if (a->hasConnectTime) {
272✔
4274
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_TIME");
×
4275
    } else {
4276
      a->hasConnectTime = true;
272✔
4277
      a->connectTime = b->connectTime;
272✔
4278
    }
4279
  }
4280

4281
  if (b->hasConnectIdleTime) {
10,292✔
4282
    if (a->hasConnectIdleTime) {
272✔
4283
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_IDLE_TIME");
×
4284
    } else {
4285
      a->hasConnectIdleTime = true;
272✔
4286
      a->connectIdleTime = b->connectIdleTime;
272✔
4287
    }
4288
  }
4289

4290
  if (b->hasCallPerSession) {
10,292✔
4291
    if (a->hasCallPerSession) {
272✔
4292
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CALLS_PER_SESSION");
×
4293
    } else {
4294
      a->hasCallPerSession = true;
272✔
4295
      a->callPerSession = b->callPerSession;
272✔
4296
    }
4297
  }
4298

4299
  if (b->hasVnodePerCall) {
10,292✔
4300
    if (a->hasVnodePerCall) {
408✔
4301
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "VNODES_PER_CALL");
×
4302
    } else {
4303
      a->hasVnodePerCall = true;
408✔
4304
      a->vnodePerCall = b->vnodePerCall;
408✔
4305
    }
4306
  }
4307

4308
  if (b->hasFailedLoginAttempts) {
10,292✔
4309
    if (a->hasFailedLoginAttempts) {
272✔
4310
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "FAILED_LOGIN_ATTEMPTS");
×
4311
    } else {
4312
      a->hasFailedLoginAttempts = true;
272✔
4313
      a->failedLoginAttempts = b->failedLoginAttempts;
272✔
4314
    }
4315
  }
4316

4317
  if (b->hasPasswordLifeTime) {
10,292✔
4318
    if (a->hasPasswordLifeTime) {
272✔
4319
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LIFE_TIME");
×
4320
    } else {
4321
      a->hasPasswordLifeTime = true;
272✔
4322
      a->passwordLifeTime = b->passwordLifeTime;
272✔
4323
    }
4324
  }
4325

4326
  if (b->hasPasswordReuseTime) {
10,292✔
4327
    if (a->hasPasswordReuseTime) {
1,881✔
4328
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_TIME");
×
4329
    } else {
4330
      a->hasPasswordReuseTime = true;
1,881✔
4331
      a->passwordReuseTime = b->passwordReuseTime;
1,881✔
4332
    }
4333
  }
4334

4335
  if (b->hasPasswordReuseMax) {
10,292✔
4336
    if (a->hasPasswordReuseMax) {
938✔
4337
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_MAX");
×
4338
    } else {
4339
      a->hasPasswordReuseMax = true;
938✔
4340
      a->passwordReuseMax = b->passwordReuseMax;
938✔
4341
    }
4342
  }
4343

4344
  if (b->hasPasswordLockTime) {
10,292✔
4345
    if (a->hasPasswordLockTime) {
272✔
4346
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LOCK_TIME");
×
4347
    } else {
4348
      a->hasPasswordLockTime = true;
272✔
4349
      a->passwordLockTime = b->passwordLockTime;
272✔
4350
    }
4351
  }
4352

4353
  if (b->hasPasswordGraceTime) {
10,292✔
4354
    if (a->hasPasswordGraceTime) {
272✔
4355
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_GRACE_TIME");
×
4356
    } else {
4357
      a->hasPasswordGraceTime = true;
272✔
4358
      a->passwordGraceTime = b->passwordGraceTime;
272✔
4359
    }
4360
  }
4361

4362
  if (b->hasInactiveAccountTime) {
10,292✔
4363
    if (a->hasInactiveAccountTime) {
272✔
4364
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "INACTIVE_ACCOUNT_TIME");
×
4365
    } else {
4366
      a->hasInactiveAccountTime = true;
272✔
4367
      a->inactiveAccountTime = b->inactiveAccountTime;
272✔
4368
    }
4369
  }
4370

4371
  if (b->hasAllowTokenNum) {
10,292✔
4372
    if (a->hasAllowTokenNum) {
272✔
4373
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ALLOW_TOKEN_NUM");
×
4374
    } else {
4375
      a->hasAllowTokenNum = true;
272✔
4376
      a->allowTokenNum = b->allowTokenNum;
272✔
4377
    }
4378
  }
4379

4380
  if (b->pIpRanges != NULL) {
10,292✔
4381
    if (a->pIpRanges == NULL) {
1,369✔
4382
      a->pIpRanges = b->pIpRanges;
1,097✔
4383
    } else {
4384
      int32_t code = nodesListAppendList(a->pIpRanges, b->pIpRanges);
272✔
4385
      if (code != TSDB_CODE_SUCCESS) {
272✔
4386
        pCxt->errCode = code;
×
4387
      }
4388
    }
4389
    b->pIpRanges = NULL;
1,369✔
4390
  }
4391

4392
  if (b->pDropIpRanges != NULL) {
10,292✔
4393
    if (a->pDropIpRanges == NULL) {
136✔
4394
      a->pDropIpRanges = b->pDropIpRanges;
×
4395
    } else {
4396
      int32_t code = nodesListAppendList(a->pDropIpRanges, b->pDropIpRanges);
136✔
4397
      if (code != TSDB_CODE_SUCCESS) {
136✔
4398
        pCxt->errCode = code;
×
4399
      }
4400
    }
4401
    b->pDropIpRanges = NULL;
136✔
4402
  }
4403

4404
  if (b->pTimeRanges != NULL) {
10,292✔
4405
    if (a->pTimeRanges == NULL) {
272✔
4406
      a->pTimeRanges = b->pTimeRanges;
136✔
4407
    } else {
4408
      int32_t code = nodesListAppendList(a->pTimeRanges, b->pTimeRanges);
136✔
4409
      if (code != TSDB_CODE_SUCCESS) {
136✔
4410
        pCxt->errCode = code;
×
4411
      }
4412
    }
4413
    b->pTimeRanges = NULL;
272✔
4414
  }
4415

4416
  if (b->pDropTimeRanges != NULL) {
10,292✔
4417
    if (a->pDropTimeRanges == NULL) {
136✔
4418
      a->pDropTimeRanges = b->pDropTimeRanges;
×
4419
    } else {
4420
      int32_t code = nodesListAppendList(a->pDropTimeRanges, b->pDropTimeRanges);
136✔
4421
      if (code != TSDB_CODE_SUCCESS) {
136✔
4422
        pCxt->errCode = code;
×
4423
      }
4424
    }
4425
    b->pDropTimeRanges = NULL;
136✔
4426
  }
4427

4428
  nodesDestroyNode((SNode*)b);
10,292✔
4429
  return a;
10,292✔
4430
}
4431

4432

4433

4434
void setUserOptionsTotpseed(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pTotpseed) {
×
4435
  pUserOptions->hasTotpseed = true;
×
4436

4437
  if (pTotpseed == NULL) { // clear TOTP secret
×
4438
    memset(pUserOptions->totpseed, 0, sizeof(pUserOptions->totpseed));
×
4439
    return;
×
4440
  }
4441

4442
  if (pTotpseed->n >= sizeof(pUserOptions->totpseed) * 2) {
×
4443
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
4444
    return;
×
4445
  }
4446

4447
  char buf[sizeof(pUserOptions->totpseed) * 2 + 1];
×
4448
  memcpy(buf, pTotpseed->z, pTotpseed->n);
×
4449
  buf[pTotpseed->n] = 0;
×
4450
  (void)strdequote(buf);
×
4451
  size_t len = strtrim(buf);
×
4452

4453
  if (len >= sizeof(pUserOptions->totpseed)) {
×
4454
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
4455
  } else if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
4456
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT, "TOTPSEED", TSDB_USER_TOTPSEED_MIN_LEN);
×
4457
  } else {
4458
    tstrncpy(pUserOptions->totpseed, buf, sizeof(pUserOptions->totpseed));
×
4459
  }
4460
}
4461

4462

4463

4464
void setUserOptionsPassword(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pPassword) {
108,222✔
4465
  pUserOptions->hasPassword = true;
108,222✔
4466

4467
  if (pPassword->n >= sizeof(pUserOptions->password) * 2) {
108,222✔
4468
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
4469
    return;
×
4470
  }
4471

4472
  char buf[sizeof(pUserOptions->password) * 2 + 1];
107,093✔
4473
  memcpy(buf, pPassword->z, pPassword->n);
108,222✔
4474
  buf[pPassword->n] = 0;
108,222✔
4475
  (void)strdequote(buf);
108,222✔
4476
  size_t len = strtrim(buf);
108,222✔
4477

4478
  if (len >= sizeof(pUserOptions->password)) {
108,222✔
4479
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
620✔
4480
  } else if (len < TSDB_PASSWORD_MIN_LEN) {
107,602✔
4481
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
4,459✔
4482
  } else {
4483
    tstrncpy(pUserOptions->password, buf, sizeof(pUserOptions->password));
103,143✔
4484
  }
4485
}
4486

4487

4488

4489
static bool isValidUserOptions(SAstCreateContext* pCxt, const SUserOptions* opts) {
112,289✔
4490
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
112,289✔
4491
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
495✔
4492
    return false;
495✔
4493
  }
4494

4495
  if (opts->hasSysinfo && (opts->sysinfo < 0 || opts->sysinfo > 1)) {
111,794✔
4496
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SYSINFO");
825✔
4497
    return false;
825✔
4498
  }
4499

4500
  if (opts->hasIsImport && (opts->isImport < 0 || opts->isImport > 1)) {
110,969✔
4501
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "IS_IMPORT");
×
4502
    return false;
×
4503
  }
4504

4505
  if (opts->hasCreatedb && (opts->createdb < 0 || opts->createdb > 1)) {
110,969✔
4506
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CREATEDB");
495✔
4507
    return false;
495✔
4508
  }
4509

4510
  if (opts->hasTotpseed && opts->totpseed[0] != 0 && !taosIsComplexString(opts->totpseed)) {
110,474✔
4511
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TOTPSEED");
×
4512
    return false;
×
4513
  }
4514

4515
  if (opts->hasPassword && !isValidPassword(pCxt, opts->password, opts->hasIsImport && opts->isImport)) {
110,474✔
4516
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
20,295✔
4517
    return false;
20,295✔
4518
  }
4519

4520
  if (opts->hasChangepass && (opts->changepass < 0 || opts->changepass > 2)) {
90,179✔
4521
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CHANGEPASS");
136✔
4522
    return false;
136✔
4523
  }
4524

4525
  if (opts->hasSessionPerUser && (opts->sessionPerUser < -1 || opts->sessionPerUser == 0)) {
90,043✔
4526
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SESSION_PER_USER");
×
4527
    return false;
×
4528
  }
4529

4530
  if (opts->hasConnectTime && (opts->connectTime < -1 || opts->connectTime == 0)) {
90,043✔
4531
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_TIME");
×
4532
    return false;
×
4533
  }
4534

4535
  if (opts->hasConnectIdleTime && (opts->connectIdleTime < -1 || opts->connectIdleTime == 0)) {
90,043✔
4536
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_IDLE_TIME");
×
4537
    return false;
×
4538
  }
4539

4540
  if (opts->hasCallPerSession && (opts->callPerSession < -1 || opts->callPerSession == 0)) {
90,043✔
4541
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CALLS_PER_SESSION");
×
4542
    return false;
×
4543
  }
4544

4545
  if (opts->hasVnodePerCall && (opts->vnodePerCall < -1 || opts->vnodePerCall == 0)) {
90,043✔
4546
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "VNODES_PER_CALL");
136✔
4547
    return false;
136✔
4548
  }
4549

4550
  if (opts->hasFailedLoginAttempts && (opts->failedLoginAttempts < -1 || opts->failedLoginAttempts == 0)) {
89,907✔
4551
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "FAILED_LOGIN_ATTEMPTS");
136✔
4552
    return false;
136✔
4553
  }
4554

4555
  if (opts->hasPasswordLockTime && (opts->passwordLockTime < -1 || opts->passwordLockTime == 0)) {
89,771✔
4556
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LOCK_TIME");
136✔
4557
    return false;
136✔
4558
  }
4559

4560
  if (opts->hasPasswordLifeTime && (opts->passwordLifeTime < -1 || opts->passwordLifeTime == 0)) {
89,635✔
4561
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LIFE_TIME");
136✔
4562
    return false;
136✔
4563
  }
4564

4565
  if (opts->hasPasswordGraceTime && (opts->passwordGraceTime < -1)) {
89,499✔
4566
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_GRACE_TIME");
×
4567
    return false;
×
4568
  }
4569

4570
  if (opts->hasPasswordReuseTime && (opts->passwordReuseTime < 0 || opts->passwordReuseTime > TSDB_USER_PASSWORD_REUSE_TIME_MAX)) {
89,499✔
4571
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_TIME");
136✔
4572
    return false;
136✔
4573
  }
4574

4575
  if (opts->hasPasswordReuseMax && (opts->passwordReuseMax < 0 || opts->passwordReuseMax > TSDB_USER_PASSWORD_REUSE_MAX_MAX)) {
89,363✔
4576
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_MAX");
136✔
4577
    return false;
136✔
4578
  }
4579

4580
  if (opts->hasInactiveAccountTime && (opts->inactiveAccountTime < -1 || opts->inactiveAccountTime == 0)) {
89,227✔
4581
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "INACTIVE_ACCOUNT_TIME");
136✔
4582
    return false;
136✔
4583
  }
4584

4585
  if (opts->hasAllowTokenNum && opts->allowTokenNum < -1) {
89,091✔
4586
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ALLOW_TOKEN_NUM");
×
4587
    return false;
×
4588
  }
4589

4590
  // ip ranges and date time ranges has been validated during parsing
4591

4592
  return true;
89,091✔
4593
}
4594

4595

4596

4597
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* opts, bool ignoreExists) {
76,668✔
4598
  SCreateUserStmt* pStmt = NULL;
76,668✔
4599

4600
  CHECK_PARSER_STATUS(pCxt);
76,668✔
4601
  CHECK_NAME(checkUserName(pCxt, pUserName));
70,430✔
4602

4603
  if (!isValidUserOptions(pCxt, opts)) {
70,305✔
4604
    goto _err;
11,813✔
4605
  }
4606

4607
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
58,492✔
4608
  CHECK_MAKE_NODE(pStmt);
58,492✔
4609
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
58,492✔
4610
  tstrncpy(pStmt->password, opts->password, sizeof(pStmt->password));
58,492✔
4611
  tstrncpy(pStmt->totpseed, opts->totpseed, sizeof(pStmt->totpseed));
58,492✔
4612

4613
  pStmt->ignoreExists = ignoreExists;
58,492✔
4614
  pStmt->sysinfo = opts->sysinfo;
58,492✔
4615
  pStmt->createDb = opts->createdb;
58,492✔
4616
  pStmt->isImport = opts->isImport;
58,492✔
4617
  pStmt->changepass = opts->changepass;
58,492✔
4618
  pStmt->enable = opts->enable;
58,492✔
4619

4620
  pStmt->sessionPerUser = opts->sessionPerUser;
58,492✔
4621
  pStmt->connectTime = opts->connectTime;
58,492✔
4622
  pStmt->connectIdleTime = opts->connectIdleTime;
58,492✔
4623
  pStmt->callPerSession = opts->callPerSession;
58,492✔
4624
  pStmt->vnodePerCall = opts->vnodePerCall;
58,492✔
4625
  pStmt->failedLoginAttempts = opts->failedLoginAttempts;
58,492✔
4626
  pStmt->passwordLifeTime = opts->passwordLifeTime;
58,492✔
4627
  pStmt->passwordReuseTime = opts->passwordReuseTime;
58,492✔
4628
  pStmt->passwordReuseMax = opts->passwordReuseMax;
58,492✔
4629
  pStmt->passwordLockTime = opts->passwordLockTime;
58,492✔
4630
  pStmt->passwordGraceTime = opts->passwordGraceTime;
58,492✔
4631
  pStmt->inactiveAccountTime = opts->inactiveAccountTime;
58,492✔
4632
  pStmt->allowTokenNum = opts->allowTokenNum;
58,492✔
4633

4634
  pStmt->numIpRanges = LIST_LENGTH(opts->pIpRanges);
58,492✔
4635
  pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpRange));
58,492✔
4636
  CHECK_OUT_OF_MEM(pStmt->pIpRanges);
58,492✔
4637
  int i = 0;
58,492✔
4638
  SNode* pNode = NULL;
58,492✔
4639
  FOREACH(pNode, opts->pIpRanges) {
60,129✔
4640
    SIpRangeNode* node = (SIpRangeNode*)(pNode);
1,637✔
4641
    pStmt->pIpRanges[i++] = node->range;
1,637✔
4642
  }
4643

4644
  pStmt->numTimeRanges = LIST_LENGTH(opts->pTimeRanges);
58,492✔
4645
  pStmt->pTimeRanges = taosMemoryMalloc(pStmt->numTimeRanges * sizeof(SDateTimeRange));
58,492✔
4646
  CHECK_OUT_OF_MEM(pStmt->pTimeRanges);
58,492✔
4647
  i = 0;
58,492✔
4648
  pNode = NULL;
58,492✔
4649
  FOREACH(pNode, opts->pTimeRanges) {
59,580✔
4650
    SDateTimeRangeNode* node = (SDateTimeRangeNode*)(pNode);
1,088✔
4651
    pStmt->pTimeRanges[i++] = node->range;
1,088✔
4652
  }
4653

4654
  nodesDestroyNode((SNode*)opts);
58,492✔
4655
  return (SNode*)pStmt;
58,492✔
4656

4657
_err:
18,176✔
4658
  nodesDestroyNode((SNode*)pStmt);
18,176✔
4659
  nodesDestroyNode((SNode*)opts);
18,176✔
4660
  return NULL;
18,176✔
4661
}
4662

4663

4664

4665
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* pUserOptions) {
43,465✔
4666
  SAlterUserStmt* pStmt = NULL;
43,465✔
4667
  CHECK_PARSER_STATUS(pCxt);
43,465✔
4668
  CHECK_NAME(checkUserName(pCxt, pUserName));
41,984✔
4669
  if (!isValidUserOptions(pCxt, pUserOptions)) {
41,984✔
4670
    goto _err;
11,385✔
4671
  }
4672

4673
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
30,599✔
4674
  CHECK_MAKE_NODE(pStmt);
30,599✔
4675
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
30,599✔
4676
  pStmt->pUserOptions = pUserOptions;
30,599✔
4677
  return (SNode*)pStmt;
30,599✔
4678

4679
_err:
12,866✔
4680
  nodesDestroyNode((SNode*)pStmt);
12,866✔
4681
  nodesDestroyNode((SNode*)pUserOptions);
12,866✔
4682
  return NULL;
12,866✔
4683
}
4684

4685

4686

4687
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName, bool ignoreNotExists) {
22,369✔
4688
  CHECK_PARSER_STATUS(pCxt);
22,369✔
4689
  CHECK_NAME(checkUserName(pCxt, pUserName));
22,369✔
4690
  SDropUserStmt* pStmt = NULL;
22,369✔
4691
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
22,369✔
4692
  CHECK_MAKE_NODE(pStmt);
22,369✔
4693
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
22,369✔
4694
  pStmt->ignoreNotExists = ignoreNotExists;
22,369✔
4695
  return (SNode*)pStmt;
22,369✔
4696
_err:
×
4697
  return NULL;
×
4698
}
4699

4700
static bool checkRoleName(SAstCreateContext* pCxt, SToken* pName, bool checkSysName) {
573,939✔
4701
  if (NULL == pName) {
573,939✔
4702
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4703
  } else {
4704
    if (pName->n >= TSDB_ROLE_LEN) {
573,939✔
4705
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
4706
    }
4707
  }
4708
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
573,939✔
4709
    trimEscape(pCxt, pName, true);
573,939✔
4710
  }
4711
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
573,939✔
4712
    if (checkSysName && taosStrncasecmp(pName->z, "sys", 3) == 0) {  // system reserved role name prefix
573,939✔
4713
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4714
                                              "Cannot create/drop/alter roles with reserved prefix 'sys'");
4715
    }
4716
  }
4717
  return TSDB_CODE_SUCCESS == pCxt->errCode;
573,939✔
4718
}
4719

4720

4721
STokenOptions* createDefaultTokenOptions(SAstCreateContext* pCxt) {
22,305✔
4722
  STokenOptions* pOptions = NULL;
22,305✔
4723
  int32_t code = nodesMakeNode(QUERY_NODE_TOKEN_OPTIONS, (SNode**)&pOptions);
22,305✔
4724
  if (pOptions == NULL) {
22,305✔
4725
    pCxt->errCode = code;
×
4726
    return NULL;
×
4727
  }
4728

4729
  pOptions->enable = 1;
22,305✔
4730
  pOptions->ttl = 0;
22,305✔
4731
  return pOptions;
22,305✔
4732
}
4733

4734

4735

4736
STokenOptions* mergeTokenOptions(SAstCreateContext* pCxt, STokenOptions* a, STokenOptions* b) {
5,022✔
4737
  if (a == NULL && b == NULL) {
5,022✔
4738
      return createDefaultTokenOptions(pCxt);
3,888✔
4739
  }
4740
  if (b == NULL) {
1,134✔
4741
    return a;
×
4742
  }
4743
  if (a == NULL) {
1,134✔
4744
    return b;
×
4745
  }
4746

4747
  if (b->hasEnable) {
1,134✔
4748
    if (a->hasEnable) {
×
4749
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE");
×
4750
    } else {
4751
      a->hasEnable = true;
×
4752
      a->enable = b->enable;
×
4753
    }
4754
  }
4755

4756
  if (b->hasTtl) {
1,134✔
4757
    if (a->hasTtl) {
324✔
4758
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TTL");
×
4759
    } else {
4760
      a->hasTtl = true;
324✔
4761
      a->ttl = b->ttl;
324✔
4762
    }
4763
  }
4764

4765
  if (b->hasProvider) {
1,134✔
4766
    if (a->hasProvider) {
486✔
4767
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PROVIDER");
×
4768
    } else {
4769
      a->hasProvider = true;
486✔
4770
      tstrncpy(a->provider, b->provider, sizeof(a->provider));
486✔
4771
    }
4772
  }
4773

4774
  if (b->hasExtraInfo) {
1,134✔
4775
    if (a->hasExtraInfo) {
324✔
4776
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "EXTRA_INFO");
×
4777
    } else {
4778
      a->hasExtraInfo = true;
324✔
4779
      tstrncpy(a->extraInfo, b->extraInfo, sizeof(a->extraInfo));
324✔
4780
    }
4781
  }
4782
  nodesDestroyNode((SNode*)b);
1,134✔
4783
  return a;
1,134✔
4784
}
4785

4786

4787

4788
void setTokenOptionsProvider(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pProvider) {
648✔
4789
  pTokenOptions->hasProvider = true;
648✔
4790

4791
  if (pProvider->n >= sizeof(pTokenOptions->provider) * 2) {
648✔
4792
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
×
4793
    return;
×
4794
  }
4795

4796
  char buf[sizeof(pTokenOptions->provider) * 2 + 1];
648✔
4797
  memcpy(buf, pProvider->z, pProvider->n);
648✔
4798
  buf[pProvider->n] = 0;
648✔
4799
  (void)strdequote(buf);
648✔
4800
  size_t len = strtrim(buf);
648✔
4801

4802
  if (len >= sizeof(pTokenOptions->provider)) {
648✔
4803
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
×
4804
  } else {
4805
    tstrncpy(pTokenOptions->provider, buf, sizeof(pTokenOptions->provider));
648✔
4806
  }
4807
}
4808

4809

4810

4811
void setTokenOptionsExtraInfo(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pExtraInfo) {
648✔
4812
  pTokenOptions->hasExtraInfo = true;
648✔
4813

4814
  if (pExtraInfo->n >= sizeof(pTokenOptions->extraInfo) * 2) {
648✔
4815
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
×
4816
    return;
×
4817
  }
4818

4819
  char buf[sizeof(pTokenOptions->extraInfo) * 2 + 1];
648✔
4820
  memcpy(buf, pExtraInfo->z, pExtraInfo->n);
648✔
4821
  buf[pExtraInfo->n] = 0;
648✔
4822
  (void)strdequote(buf);
648✔
4823
  size_t len = strtrim(buf);
648✔
4824

4825
  if (len >= sizeof(pTokenOptions->extraInfo)) {
648✔
4826
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
×
4827
  } else {
4828
    tstrncpy(pTokenOptions->extraInfo, buf, sizeof(pTokenOptions->extraInfo));
648✔
4829
  }
4830
}
4831

4832

4833

4834
static bool isValidTokenOptions(SAstCreateContext* pCxt, const STokenOptions* opts) {
2,754✔
4835
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
2,754✔
4836
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
324✔
4837
    return false;
324✔
4838
  }
4839

4840
  if (opts->hasTtl && (opts->ttl < 0)) {
2,430✔
4841
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TTL");
×
4842
    return false;
×
4843
  }
4844

4845
  return true;
2,430✔
4846
}
4847

4848

4849

4850
static bool checkTokenName(SAstCreateContext* pCxt, SToken* pTokenName) {
23,439✔
4851
  if (NULL == pTokenName) {
23,439✔
4852
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4853
  } else {
4854
    if (pTokenName->n >= TSDB_TOKEN_NAME_LEN) {
23,439✔
4855
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOKEN_NAME", TSDB_TOKEN_NAME_LEN);
324✔
4856
    }
4857
  }
4858
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
23,439✔
4859
    trimEscape(pCxt, pTokenName, true);
23,115✔
4860
  }
4861
  return TSDB_CODE_SUCCESS == pCxt->errCode;
23,439✔
4862
}
4863

4864
SNode* createCreateRoleStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pName) {
161✔
4865
  CHECK_PARSER_STATUS(pCxt);
161✔
4866
  CHECK_NAME(checkRoleName(pCxt, pName, true));
161✔
4867
  SCreateRoleStmt* pStmt = NULL;
161✔
4868
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ROLE_STMT, (SNode**)&pStmt);
161✔
4869
  CHECK_MAKE_NODE(pStmt);
161✔
4870
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
161✔
4871
  pStmt->ignoreExists = ignoreExists;
161✔
4872
  return (SNode*)pStmt;
161✔
4873
_err:
×
4874
  return NULL;
×
4875
}
4876

4877
SNode* createDropRoleStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pName) {
×
4878
  CHECK_PARSER_STATUS(pCxt);
×
4879
  CHECK_NAME(checkRoleName(pCxt, pName, true));
×
4880
  SDropRoleStmt* pStmt = NULL;
×
4881
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ROLE_STMT, (SNode**)&pStmt);
×
4882
  CHECK_MAKE_NODE(pStmt);
×
4883
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
×
4884
  pStmt->ignoreNotExists = ignoreNotExists;
×
4885
  return (SNode*)pStmt;
×
4886
_err:
×
4887
  return NULL;
×
4888
}
4889

4890
/**
4891
 * used by user and role
4892
 */
4893
SNode* createAlterRoleStmt(SAstCreateContext* pCxt, SToken* pName, int8_t alterType, void* pAlterInfo) {
×
4894
  SAlterRoleStmt* pStmt = NULL;
×
4895
  CHECK_PARSER_STATUS(pCxt);
×
4896
  CHECK_NAME(checkUserName(pCxt, pName));
×
4897
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ROLE_STMT, (SNode**)&pStmt);
×
4898
  CHECK_MAKE_NODE(pStmt);
×
4899
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
×
4900
  pStmt->alterType = alterType;
×
4901
  switch (alterType) {
×
4902
    case TSDB_ALTER_ROLE_LOCK: {
×
4903
      SToken* pVal = pAlterInfo;
×
4904
      pStmt->lock = taosStr2Int8(pVal->z, NULL, 10);
×
4905
      break;
×
4906
    }
4907
    default:
×
4908
      break;
×
4909
  }
4910
  return (SNode*)pStmt;
×
4911
_err:
×
4912
  nodesDestroyNode((SNode*)pStmt);
×
4913
  return NULL;
×
4914
}
4915

4916

4917
SNode* createCreateTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, SToken* pUserName, STokenOptions* opts, bool ignoreExists) {
20,037✔
4918
  SCreateTokenStmt* pStmt = NULL;
20,037✔
4919

4920
  CHECK_PARSER_STATUS(pCxt);
20,037✔
4921
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
20,037✔
4922
  CHECK_NAME(checkUserName(pCxt, pUserName));
19,713✔
4923

4924
  if (opts == NULL) {
19,713✔
4925
    opts = createDefaultTokenOptions(pCxt);
18,417✔
4926
  } else if (!isValidTokenOptions(pCxt, opts)) {
1,296✔
4927
    goto _err;
162✔
4928
  }
4929

4930
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOKEN_STMT, (SNode**)&pStmt);
19,551✔
4931
  CHECK_MAKE_NODE(pStmt);
19,551✔
4932

4933
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
19,551✔
4934
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
19,551✔
4935
  pStmt->enable = opts->enable;
19,551✔
4936
  pStmt->ignoreExists = ignoreExists;
19,551✔
4937
  pStmt->ttl = opts->ttl;
19,551✔
4938
  tstrncpy(pStmt->provider, opts->provider, sizeof(pStmt->provider));
19,551✔
4939
  tstrncpy(pStmt->extraInfo, opts->extraInfo, sizeof(pStmt->extraInfo));
19,551✔
4940
  nodesDestroyNode((SNode*)opts);
19,551✔
4941
  return (SNode*)pStmt;
19,551✔
4942

4943
_err:
486✔
4944
  nodesDestroyNode((SNode*)pStmt);
486✔
4945
  nodesDestroyNode((SNode*)opts);
486✔
4946
  return NULL;
486✔
4947
}
4948

4949

4950

4951
SNode* createAlterTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, STokenOptions* opts) {
1,458✔
4952
  SAlterTokenStmt* pStmt = NULL;
1,458✔
4953

4954
  CHECK_PARSER_STATUS(pCxt);
1,458✔
4955
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
1,458✔
4956
  if (!isValidTokenOptions(pCxt, opts)) {
1,458✔
4957
    goto _err;
162✔
4958
  }
4959

4960
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TOKEN_STMT, (SNode**)&pStmt);
1,296✔
4961
  CHECK_MAKE_NODE(pStmt);
1,296✔
4962

4963
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
1,296✔
4964
  pStmt->pTokenOptions = opts;
1,296✔
4965
  return (SNode*)pStmt;
1,296✔
4966

4967
_err:
162✔
4968
  nodesDestroyNode((SNode*)pStmt);
162✔
4969
  nodesDestroyNode((SNode*)opts);
162✔
4970
  return NULL;
162✔
4971
}
4972

4973

4974

4975
SNode* createDropTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, bool ignoreNotExists) {
1,944✔
4976
  SDropTokenStmt* pStmt = NULL;
1,944✔
4977

4978
  CHECK_PARSER_STATUS(pCxt);
1,944✔
4979
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
1,944✔
4980

4981
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOKEN_STMT, (SNode**)&pStmt);
1,944✔
4982
  CHECK_MAKE_NODE(pStmt);
1,944✔
4983

4984
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
1,944✔
4985
  pStmt->ignoreNotExists = ignoreNotExists;
1,944✔
4986
  return (SNode*)pStmt;
1,944✔
4987

4988
_err:
×
4989
  nodesDestroyNode((SNode*)pStmt);
×
4990
  return NULL;
×
4991
}
4992

4993
SNode* createCreateTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
17,559✔
4994
  SCreateTotpSecretStmt* pStmt = NULL;
17,559✔
4995

4996
  CHECK_PARSER_STATUS(pCxt);
17,559✔
4997
  CHECK_NAME(checkUserName(pCxt, pUserName));
17,559✔
4998

4999
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOTP_SECRET_STMT, (SNode**)&pStmt);
17,404✔
5000
  CHECK_MAKE_NODE(pStmt);
17,404✔
5001

5002
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
17,404✔
5003
  return (SNode*)pStmt;
17,404✔
5004

5005
_err:
155✔
5006
  nodesDestroyNode((SNode*)pStmt);
155✔
5007
  return NULL;
155✔
5008
}
5009

5010

5011
SNode* createDropTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
8,060✔
5012
  SDropTotpSecretStmt* pStmt = NULL;
8,060✔
5013

5014
  CHECK_PARSER_STATUS(pCxt);
8,060✔
5015
  CHECK_NAME(checkUserName(pCxt, pUserName));
8,060✔
5016

5017
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOTP_SECRET_STMT, (SNode**)&pStmt);
6,510✔
5018
  CHECK_MAKE_NODE(pStmt);
6,510✔
5019

5020
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
6,510✔
5021
  return (SNode*)pStmt;
6,510✔
5022

5023
_err:
1,550✔
5024
  nodesDestroyNode((SNode*)pStmt);
1,550✔
5025
  return NULL;
1,550✔
5026
}
5027

5028

5029
SNode* createDropEncryptAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId) {
×
5030
  CHECK_PARSER_STATUS(pCxt);
×
5031
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5032
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
5033
    goto _err;
×
5034
  }
5035
  SDropEncryptAlgrStmt* pStmt = NULL;
×
5036
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ENCRYPT_ALGR_STMT, (SNode**)&pStmt);
×
5037
  CHECK_MAKE_NODE(pStmt);
×
5038
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
5039
  return (SNode*)pStmt;
×
5040
_err:
×
5041
  return NULL;
×
5042
}
5043

5044
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
151,069✔
5045
  CHECK_PARSER_STATUS(pCxt);
151,069✔
5046
  SCreateDnodeStmt* pStmt = NULL;
151,069✔
5047
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
151,069✔
5048
  CHECK_MAKE_NODE(pStmt);
151,069✔
5049
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
151,069✔
5050
    nodesDestroyNode((SNode*)pStmt);
×
5051
    return NULL;
×
5052
  }
5053
  return (SNode*)pStmt;
151,069✔
5054
_err:
×
5055
  return NULL;
×
5056
}
5057

5058
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
10,242✔
5059
  CHECK_PARSER_STATUS(pCxt);
10,242✔
5060
  SDropDnodeStmt* pStmt = NULL;
10,242✔
5061
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
10,242✔
5062
  CHECK_MAKE_NODE(pStmt);
10,242✔
5063
  if (TK_NK_INTEGER == pDnode->type) {
10,242✔
5064
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
10,242✔
5065
  } else {
5066
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
×
5067
      nodesDestroyNode((SNode*)pStmt);
×
5068
      return NULL;
×
5069
    }
5070
  }
5071
  pStmt->force = force;
10,242✔
5072
  pStmt->unsafe = unsafe;
10,242✔
5073
  return (SNode*)pStmt;
10,242✔
5074
_err:
×
5075
  return NULL;
×
5076
}
5077

5078
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
104,812✔
5079
                            const SToken* pValue) {
5080
  CHECK_PARSER_STATUS(pCxt);
104,812✔
5081
  SAlterDnodeStmt* pStmt = NULL;
104,812✔
5082
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
104,812✔
5083
  CHECK_MAKE_NODE(pStmt);
104,812✔
5084
  if (NULL != pDnode) {
104,812✔
5085
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
64,633✔
5086
  } else {
5087
    pStmt->dnodeId = -1;
40,179✔
5088
  }
5089
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
104,812✔
5090
  if (NULL != pValue) {
104,812✔
5091
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
49,134✔
5092
  }
5093
  return (SNode*)pStmt;
104,812✔
5094
_err:
×
5095
  return NULL;
×
5096
}
5097

5098
SNode* createCreateAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId, const SToken* name, const SToken* desc,
×
5099
                            const SToken* type, const SToken* osslAlgrName) {
5100
  CHECK_PARSER_STATUS(pCxt);
×
5101
  SCreateEncryptAlgrStmt* pStmt = NULL;
×
5102
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ENCRYPT_ALGORITHMS_STMT, (SNode**)&pStmt);
×
5103
  CHECK_MAKE_NODE(pStmt);
×
5104
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5105
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
5106
    goto _err;
×
5107
  }
5108
  if (name->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5109
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_NAME_TOO_LONG);
×
5110
    goto _err;
×
5111
  }
5112
  if (desc->n >= TSDB_ENCRYPT_ALGR_DESC_LEN) {
×
5113
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_DESC_TOO_LONG);
×
5114
    goto _err;
×
5115
  }
5116
  if (type->n >= TSDB_ENCRYPT_ALGR_TYPE_LEN) {
×
5117
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_TYPE_TOO_LONG);
×
5118
    goto _err;
×
5119
  }
5120
  if (osslAlgrName->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5121
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_OSSL_NAME_TOO_LONG);
×
5122
    goto _err;
×
5123
  }
5124
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
5125
  (void)trimString(name->z, name->n, pStmt->name, sizeof(pStmt->name));
×
5126
  (void)trimString(desc->z, desc->n, pStmt->desc, sizeof(pStmt->desc));
×
5127
  (void)trimString(type->z, type->n, pStmt->algrType, sizeof(pStmt->algrType));
×
5128
  (void)trimString(osslAlgrName->z, osslAlgrName->n, pStmt->osslAlgrName, sizeof(pStmt->osslAlgrName));
×
5129
  return (SNode*)pStmt;
×
5130
_err:
×
5131
  return NULL;
×
5132
}
5133

5134
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
×
5135
  CHECK_PARSER_STATUS(pCxt);
×
5136
  SCreateAnodeStmt* pStmt = NULL;
×
5137
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
×
5138
  CHECK_MAKE_NODE(pStmt);
×
5139
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
×
5140
  return (SNode*)pStmt;
×
5141
_err:
×
5142
  return NULL;
×
5143
}
5144

5145
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
×
5146
  CHECK_PARSER_STATUS(pCxt);
×
5147
  SUpdateAnodeStmt* pStmt = NULL;
×
5148
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
×
5149
  CHECK_MAKE_NODE(pStmt);
×
5150
  if (NULL != pAnode) {
×
5151
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5152
  } else {
5153
    pStmt->anodeId = -1;
×
5154
  }
5155
  return (SNode*)pStmt;
×
5156
_err:
×
5157
  return NULL;
×
5158
}
5159

5160
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
5161
  CHECK_PARSER_STATUS(pCxt);
×
5162
  SUpdateAnodeStmt* pStmt = NULL;
×
5163
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
5164
  CHECK_MAKE_NODE(pStmt);
×
5165
  if (NULL != pAnode) {
×
5166
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5167
  } else {
5168
    pStmt->anodeId = -1;
×
5169
  }
5170
  return (SNode*)pStmt;
×
5171
_err:
×
5172
  return NULL;
×
5173
}
5174

5175
SNode* createCreateBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId, SNode* pOptions) {
23,195✔
5176
  CHECK_PARSER_STATUS(pCxt);
23,195✔
5177
  SCreateBnodeStmt* pStmt = NULL;
23,195✔
5178
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_BNODE_STMT, (SNode**)&pStmt);
23,195✔
5179
  CHECK_MAKE_NODE(pStmt);
23,195✔
5180
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
23,195✔
5181

5182
  pStmt->pOptions = (SBnodeOptions*)pOptions;
23,195✔
5183

5184
  return (SNode*)pStmt;
23,195✔
5185
_err:
×
5186
  return NULL;
×
5187
}
5188

5189
SNode* createDropBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId) {
27,485✔
5190
  CHECK_PARSER_STATUS(pCxt);
27,485✔
5191
  SUpdateBnodeStmt* pStmt = NULL;
27,485✔
5192
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_BNODE_STMT, (SNode**)&pStmt);
27,485✔
5193
  CHECK_MAKE_NODE(pStmt);
27,485✔
5194
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
27,485✔
5195

5196
  return (SNode*)pStmt;
27,485✔
5197
_err:
×
5198
  return NULL;
×
5199
}
5200

5201
SNode* createDefaultBnodeOptions(SAstCreateContext* pCxt) {
23,195✔
5202
  CHECK_PARSER_STATUS(pCxt);
23,195✔
5203
  SBnodeOptions* pOptions = NULL;
23,195✔
5204
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BNODE_OPTIONS, (SNode**)&pOptions);
23,195✔
5205
  CHECK_MAKE_NODE(pOptions);
23,195✔
5206

5207
  tstrncpy(pOptions->protoStr, TSDB_BNODE_OPT_PROTO_DFT_STR, TSDB_BNODE_OPT_PROTO_STR_LEN);
23,195✔
5208
  pOptions->proto = TSDB_BNODE_OPT_PROTO_DEFAULT;
23,195✔
5209

5210
  return (SNode*)pOptions;
23,195✔
5211
_err:
×
5212
  return NULL;
×
5213
}
5214

5215
static SNode* setBnodeOptionImpl(SAstCreateContext* pCxt, SNode* pBodeOptions, EBnodeOptionType type, void* pVal,
×
5216
                                 bool alter) {
5217
  CHECK_PARSER_STATUS(pCxt);
×
5218
  SBnodeOptions* pOptions = (SBnodeOptions*)pBodeOptions;
×
5219
  switch (type) {
×
5220
    case BNODE_OPTION_PROTOCOL:
×
5221
      COPY_STRING_FORM_STR_TOKEN(pOptions->protoStr, (SToken*)pVal);
×
5222
      break;
×
5223
    default:
×
5224
      break;
×
5225
  }
5226

5227
  return pBodeOptions;
×
5228
_err:
×
5229
  nodesDestroyNode(pBodeOptions);
×
5230
  return NULL;
×
5231
}
5232

5233
SNode* setBnodeOption(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pOption, void* pVal) {
×
5234
  if (0 == strncasecmp(pOption->z, "protocol", 8)) {
×
5235
    return setBnodeOptionImpl(pCxt, pOptions, BNODE_OPTION_PROTOCOL, pVal, false);
×
5236
  } else {
5237
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5238
    return pOptions;
×
5239
  }
5240
}
5241

5242
SNode* createCreateXnodeWithUserPassStmt(SAstCreateContext* pCxt, const SToken* pUrl, SToken* pUser,
1,647✔
5243
                                         const SToken* pPass) {
5244
  CHECK_PARSER_STATUS(pCxt);
1,647✔
5245
  SCreateXnodeStmt* pStmt = NULL;
1,647✔
5246
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
1,647✔
5247
  CHECK_MAKE_NODE(pStmt);
1,647✔
5248

5249
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
1,647✔
5250

5251
  if (pUser != NULL) {
1,647✔
5252
    CHECK_NAME(checkUserName(pCxt, pUser));
1,647✔
5253
    COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUser);
1,647✔
5254
  }
5255
  if (pPass != NULL) {
1,647✔
5256
    if (pPass->n <= 2) {
1,647✔
5257
      pCxt->errCode =
×
5258
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode password should not be empty");
×
5259
      goto _err;
×
5260
    }
5261
    strncpy(pStmt->pass, pPass->z + 1, pPass->n - 2);
1,647✔
5262
    pStmt->pass[sizeof(pStmt->pass) - 1] = '\0';
1,647✔
5263
  }
5264
  return (SNode*)pStmt;
1,647✔
5265
_err:
×
5266
  return NULL;
×
5267
}
5268
SNode* createCreateXnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
2,159✔
5269
  CHECK_PARSER_STATUS(pCxt);
2,159✔
5270
  SCreateXnodeStmt* pStmt = NULL;
2,159✔
5271
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
2,159✔
5272
  CHECK_MAKE_NODE(pStmt);
2,159✔
5273
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
2,159✔
5274
  return (SNode*)pStmt;
2,159✔
5275
_err:
×
5276
  return NULL;
×
5277
}
5278

5279
SNode* createDropXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode, bool force) {
7,793✔
5280
  if (NULL == pXnode) {
7,793✔
5281
    pCxt->errCode =
×
5282
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
5283
    goto _err;
×
5284
  }
5285
  CHECK_PARSER_STATUS(pCxt);
7,793✔
5286
  SDropXnodeStmt* pStmt = NULL;
7,793✔
5287
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_STMT, (SNode**)&pStmt);
7,793✔
5288
  CHECK_MAKE_NODE(pStmt);
7,793✔
5289

5290
  pStmt->force = force;
7,793✔
5291
  if (pXnode->type == TK_NK_STRING) {
7,793✔
5292
    if (pXnode->n <= 2) {
4,293✔
5293
      pCxt->errCode =
×
5294
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode url should not be all be NULL");
×
5295
        goto _err;
×
5296
    }
5297
    COPY_STRING_FORM_STR_TOKEN(pStmt->url, pXnode);
4,293✔
5298
  } else if(pXnode->type == TK_NK_INTEGER) {
3,500✔
5299
    pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
3,500✔
5300
  } else {
5301
    pCxt->errCode =
×
5302
      generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id or url should not be all be NULL");
×
5303
  }
5304

5305
  return (SNode*)pStmt;
7,793✔
5306
_err:
×
5307
  return NULL;
×
5308
}
5309

5310
SNode* createDrainXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode) {
2,390✔
5311
  CHECK_PARSER_STATUS(pCxt);
2,390✔
5312
  if (NULL == pXnode) {
2,390✔
5313
    pCxt->errCode =
×
5314
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
5315
    goto _err;
×
5316
  }
5317
  if (pXnode->type != TK_NK_INTEGER) {
2,390✔
5318
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be an integer");
×
5319
    goto _err;
×
5320
  }
5321

5322
  SDrainXnodeStmt* pStmt = NULL;
2,390✔
5323
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DRAIN_XNODE_STMT, (SNode**)&pStmt);
2,390✔
5324
  CHECK_MAKE_NODE(pStmt);
2,390✔
5325
  pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
2,390✔
5326
  if (pStmt->xnodeId <= 0) {
2,390✔
5327
    pCxt->errCode =
×
5328
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be greater than 0");
×
5329
    goto _err;
×
5330
  }
5331

5332
  return (SNode*)pStmt;
2,390✔
5333
_err:
×
5334
  return NULL;
×
5335
}
5336

5337
// SNode* createUpdateXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode, bool updateAll) {
5338
//   CHECK_PARSER_STATUS(pCxt);
5339
//   SUpdateXnodeStmt* pStmt = NULL;
5340
//   pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_XNODE_STMT, (SNode**)&pStmt);
5341
//   CHECK_MAKE_NODE(pStmt);
5342
//   if (NULL != pXnode) {
5343
//     pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
5344
//   } else {
5345
//     pStmt->xnodeId = -1;
5346
//   }
5347
//   return (SNode*)pStmt;
5348
// _err:
5349
//   return NULL;
5350
// }
5351

5352
EXnodeResourceType setXnodeResourceType(SAstCreateContext* pCxt, const SToken* pResourceId) {
27,369✔
5353
  CHECK_PARSER_STATUS(pCxt);
27,369✔
5354
  const size_t TASK_LEN = 4;
27,369✔
5355
  const size_t TASKS_LEN = 5;
27,369✔
5356
  const size_t AGENT_LEN = 5;
27,369✔
5357
  const size_t AGENTS_LEN = 6;
27,369✔
5358
  const size_t JOB_LEN = 3;
27,369✔
5359
  const size_t JOBS_LEN = 4;
27,369✔
5360

5361
  if (pResourceId->z[0] == '`') {
27,369✔
5362
    if (strncmp(pResourceId->z + 1, "task", TASK_LEN) == 0 || strncmp(pResourceId->z + 1, "tasks", TASKS_LEN) == 0) {
×
5363
      return XNODE_TASK;
×
5364
    }
5365
    if (strncmp(pResourceId->z + 1, "agent", AGENT_LEN) == 0 ||
×
5366
        strncmp(pResourceId->z + 1, "agents", AGENTS_LEN) == 0) {
×
5367
      return XNODE_AGENT;
×
5368
    }
5369
    if (strncmp(pResourceId->z + 1, "job", JOB_LEN) == 0 || strncmp(pResourceId->z + 1, "jobs", JOBS_LEN) == 0) {
×
5370
      return XNODE_JOB;
×
5371
    }
5372

5373
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5374
                                            "Invalid xnode resource type (task/agent) at: %s", pResourceId->z);
×
5375
    goto _err;
×
5376
  }
5377
  if (strncmp(pResourceId->z, "task", TASK_LEN) == 0 || strncmp(pResourceId->z, "tasks", TASKS_LEN) == 0) {
27,369✔
5378
    return XNODE_TASK;
20,334✔
5379
  }
5380
  if (strncmp(pResourceId->z, "agent", AGENT_LEN) == 0 || strncmp(pResourceId->z, "agents", AGENTS_LEN) == 0) {
7,035✔
5381
    return XNODE_AGENT;
2,019✔
5382
  }
5383
  if (strncmp(pResourceId->z, "job", JOB_LEN) == 0 || strncmp(pResourceId->z, "jobs", JOBS_LEN) == 0) {
5,016✔
5384
    return XNODE_JOB;
5,016✔
5385
  }
5386
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5387
                                          "Invalid xnode resource type (task/agent/job) at: %s", pResourceId->z);
×
5388
  goto _err;
×
5389

5390
_err:
×
5391
  return XNODE_UNKNOWN;
×
5392
}
5393
SNode* createXnodeSourceAsDsn(SAstCreateContext* pCxt, const SToken* pToken) {
4,440✔
5394
  SXTaskSource* pSource = NULL;
4,440✔
5395
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
4,440✔
5396
  CHECK_MAKE_NODE(pSource);
4,440✔
5397
  if (pToken == NULL || pToken->n <= 0) {
4,440✔
5398
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5399
                                            "xnode source dsn should not be NULL or empty");
5400
    goto _err;
×
5401
  }
5402
  if (pToken->n > TSDB_XNODE_TASK_SOURCE_LEN) {
4,440✔
5403
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5404
                                            "Invalid xnode source dsn length: %d, max length: %d", pToken->n,
×
5405
                                            TSDB_XNODE_TASK_SOURCE_LEN);
5406
    goto _err;
×
5407
  }
5408
  pSource->source.type = XNODE_TASK_SOURCE_DSN;
4,440✔
5409
  COPY_COW_STR_FROM_STR_TOKEN(pSource->source.cstr, pToken);
4,440✔
5410
  return (SNode*)pSource;
4,440✔
5411
_err:
×
5412
  return NULL;
×
5413
}
5414
SNode* createXnodeSourceAsDatabase(SAstCreateContext* pCxt, const SToken* pToken) {
1,110✔
5415
  SXTaskSource* pSource = NULL;
1,110✔
5416
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
1,110✔
5417
  CHECK_MAKE_NODE(pSource);
1,110✔
5418
  if (pToken == NULL || pToken->n <= 0) {
1,110✔
5419
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5420
                                            "xnode source database should not be NULL or empty");
5421
    goto _err;
×
5422
  }
5423
  if (pToken->n > TSDB_XNODE_TASK_SOURCE_LEN) {
1,110✔
5424
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5425
                                            "Invalid xnode source database length: %d, max length: %d", pToken->n,
×
5426
                                            TSDB_XNODE_TASK_SOURCE_LEN);
5427
    goto _err;
×
5428
  }
5429
  pSource->source.type = XNODE_TASK_SOURCE_DATABASE;
1,110✔
5430
  COPY_COW_STR_FROM_ID_TOKEN(pSource->source.cstr, pToken);
1,110✔
5431
  return (SNode*)pSource;
1,110✔
5432
_err:
×
5433
  return NULL;
×
5434
}
5435
SNode* createXnodeSourceAsTopic(SAstCreateContext* pCxt, const SToken* pToken) {
2,220✔
5436
  SXTaskSource* pSource = NULL;
2,220✔
5437
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
2,220✔
5438
  CHECK_MAKE_NODE(pSource);
2,220✔
5439
  if (pToken == NULL || pToken->n <= 0) {
2,220✔
5440
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5441
                                            "xnode source dsn should not be NULL or empty");
5442
    goto _err;
×
5443
  }
5444
  if (pToken->n > TSDB_TOPIC_NAME_LEN) {
2,220✔
5445
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5446
                                            "Invalid xnode source topic length: %d, max length: %d", pToken->n,
×
5447
                                            TSDB_TOPIC_NAME_LEN);
5448
    goto _err;
×
5449
  }
5450
  pSource->source.type = XNODE_TASK_SOURCE_TOPIC;
2,220✔
5451
  COPY_COW_STR_FROM_STR_TOKEN(pSource->source.cstr, pToken);
2,220✔
5452
  return (SNode*)pSource;
2,220✔
5453
_err:
×
5454
  return NULL;
×
5455
}
5456
SNode* createXnodeSinkAsDsn(SAstCreateContext* pCxt, const SToken* pToken) {
4,440✔
5457
  SXTaskSink* pSink = NULL;
4,440✔
5458
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SINK_OPT, (SNode**)&pSink);
4,440✔
5459
  CHECK_MAKE_NODE(pSink);
4,440✔
5460
  if (pToken == NULL || pToken->n <= 0) {
4,440✔
5461
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5462
                                            "xnode sink dsn should not be NULL or empty");
5463
    goto _err;
×
5464
  }
5465
  if (pToken->n > TSDB_XNODE_TASK_SINK_LEN) {
4,440✔
5466
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5467
                                            "Invalid xnode sink dsn length: %d, max length: %d", pToken->n,
×
5468
                                            TSDB_XNODE_TASK_SINK_LEN);
5469
    goto _err;
×
5470
  }
5471
  pSink->sink.type = XNODE_TASK_SINK_DSN;
4,440✔
5472
  COPY_COW_STR_FROM_STR_TOKEN(pSink->sink.cstr, pToken);
4,440✔
5473
  return (SNode*)pSink;
4,440✔
5474
_err:
×
5475
  return NULL;
×
5476
}
5477
SNode* createXnodeSinkAsDatabase(SAstCreateContext* pCxt, const SToken* pToken) {
4,440✔
5478
  SXTaskSink* pSink = NULL;
4,440✔
5479
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SINK_OPT, (SNode**)&pSink);
4,440✔
5480
  CHECK_MAKE_NODE(pSink);
4,440✔
5481
  if (pToken == NULL || pToken->n <= 0) {
4,440✔
5482
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5483
                                            "Xnode sink database should not be NULL or empty");
5484
    goto _err;
×
5485
  }
5486
  if (pToken->n > TSDB_XNODE_TASK_SINK_LEN) {
4,440✔
5487
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5488
                                            "Invalid xnode sink database length: %d, max length: %d", pToken->n,
×
5489
                                            TSDB_XNODE_TASK_SINK_LEN);
5490
    goto _err;
×
5491
  }
5492
  pSink->sink.type = XNODE_TASK_SINK_DATABASE;
4,440✔
5493
  if (pToken->type == TK_NK_STRING) {
4,440✔
5494
    COPY_COW_STR_FROM_STR_TOKEN(pSink->sink.cstr, pToken);
×
5495
  } else if (pToken->type == TK_NK_ID) {
4,440✔
5496
    COPY_COW_STR_FROM_ID_TOKEN(pSink->sink.cstr, pToken);
4,440✔
5497
  } else {
5498
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5499
                                            "Invalid xnode sink database type: %d", pToken->type);
×
5500
    goto _err;
×
5501
  }
5502

5503
  return (SNode*)pSink;
4,440✔
5504
_err:
×
5505
  return NULL;
×
5506
}
5507

5508
SNode* createXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pSource,
7,770✔
5509
                                          SNode* pSink, SNode* pNode) {
5510
  SNode* pStmt = NULL;
7,770✔
5511
  if (pResourceName == NULL) {
7,770✔
5512
    pCxt->errCode =
×
5513
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name should not be NULL");
×
5514
    goto _err;
×
5515
  }
5516
  if (pSource == NULL || pSink == NULL) {
7,770✔
5517
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
1,110✔
5518
                                            "Xnode task source and sink should not be NULL");
5519
    goto _err;
1,110✔
5520
  }
5521
  if (nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT || nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT) {
6,660✔
5522
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5523
                                            "Xnode task source and sink should be valid nodes");
5524
    goto _err;
×
5525
  }
5526
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_TASK_STMT, (SNode**)&pStmt);
6,660✔
5527
  CHECK_MAKE_NODE(pStmt);
6,660✔
5528
  SCreateXnodeTaskStmt* pTaskStmt = (SCreateXnodeTaskStmt*)pStmt;
6,660✔
5529
  if (pResourceName->type == TK_NK_STRING) {
6,660✔
5530
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
6,660✔
5531
  } else if (pResourceName->type == TK_NK_ID) {
×
5532
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
×
5533
  } else {
5534
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode name type: %d",
×
5535
                                            pResourceName->type);
×
5536
    goto _err;
×
5537
  }
5538

5539
  if (pSource != NULL) {
6,660✔
5540
    SXTaskSource* source = (SXTaskSource*)(pSource);
6,660✔
5541
    pTaskStmt->source = source;
6,660✔
5542
  }
5543
  if (pSink != NULL) {
6,660✔
5544
    SXTaskSink* sink = (SXTaskSink*)(pSink);
6,660✔
5545
    pTaskStmt->sink = sink;
6,660✔
5546
  }
5547
  if (pNode != NULL) {
6,660✔
5548
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
6,660✔
5549
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
6,660✔
5550
      pTaskStmt->options = options;
6,660✔
5551
    }
5552
  }
5553
  return (SNode*)pTaskStmt;
6,660✔
5554
_err:
1,110✔
5555
  if (pStmt != NULL) {
1,110✔
5556
    nodesDestroyNode(pStmt);
×
5557
  }
5558
  if (pNode != NULL) {
1,110✔
5559
    nodesDestroyNode(pNode);
1,110✔
5560
  }
5561
  return NULL;
1,110✔
5562
}
5563

5564
SNode* createXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pOptions) {
222✔
5565
  SNode* pStmt = NULL;
222✔
5566
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_AGENT_STMT, (SNode**)&pStmt);
222✔
5567
  CHECK_MAKE_NODE(pStmt);
222✔
5568
  SCreateXnodeAgentStmt* pAgentStmt = (SCreateXnodeAgentStmt*)pStmt;
222✔
5569

5570
  if (pOptions != NULL) {
222✔
5571
    if (nodeType(pOptions) == QUERY_NODE_XNODE_TASK_OPTIONS) {
222✔
5572
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pOptions);
222✔
5573
      pAgentStmt->options = options;
222✔
5574
    }
5575
  }
5576

5577
  if (pResourceName->type == TK_NK_STRING && pResourceName->n > 2) {
222✔
5578
    COPY_STRING_FORM_STR_TOKEN(pAgentStmt->name, pResourceName);
222✔
5579
  } else {
5580
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5581
                                            "Invalid xnode agent name type: %d", pResourceName->type);
×
5582
    goto _err;
×
5583
  }
5584

5585
  return (SNode*)pAgentStmt;
222✔
5586
_err:
×
5587
  if (pStmt != NULL) {
×
5588
    nodesDestroyNode(pStmt);
×
5589
  }
5590
  return NULL;
×
5591
}
5592

5593
SNode* createXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResourceName,
7,992✔
5594
                                  SNode* pSource, SNode* pSink, SNode* pNode) {
5595
  CHECK_PARSER_STATUS(pCxt);
7,992✔
5596

5597
  switch (resourceType) {
7,992✔
5598
    case XNODE_TASK: {
7,770✔
5599
      return createXnodeTaskWithOptionsDirectly(pCxt, pResourceName, pSource, pSink, pNode);
7,770✔
5600
    }
5601
    case XNODE_AGENT: {
222✔
5602
      return createXnodeAgentWithOptionsDirectly(pCxt, pResourceName, pNode);
222✔
5603
      break;
5604
    }
5605
    default:
×
5606
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5607
                                              "Invalid xnode resource type: %d", resourceType);
5608
      goto _err;
×
5609
  }
5610
_err:
×
5611
  return NULL;
×
5612
}
5613

5614
SNode* createStartXnodeTaskStmt(SAstCreateContext* pCxt, const EXnodeResourceType resourceType, SToken* pIdOrName) {
1,110✔
5615
  SNode* pStmt = NULL;
1,110✔
5616
  CHECK_PARSER_STATUS(pCxt);
1,110✔
5617
  if (resourceType != XNODE_TASK) {
1,110✔
5618
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5619
                                            "Invalid xnode resource type: %d", resourceType);
5620
    goto _err;
×
5621
  }
5622
  if (pIdOrName == NULL || (pIdOrName != NULL && pIdOrName->type != TK_NK_INTEGER && pIdOrName->type != TK_NK_STRING)) {
1,110✔
5623
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5624
                                            "Xnode task id or name should be an integer or string");
5625
    goto _err;
×
5626
  }
5627

5628
  pCxt->errCode = nodesMakeNode(QUERY_NODE_START_XNODE_TASK_STMT, (SNode**)&pStmt);
1,110✔
5629
  CHECK_MAKE_NODE(pStmt);
1,110✔
5630
  SStartXnodeTaskStmt* pTaskStmt = (SStartXnodeTaskStmt*)pStmt;
1,110✔
5631
  if (pIdOrName->type == TK_NK_INTEGER) {
1,110✔
5632
    pTaskStmt->tid = taosStr2Int32(pIdOrName->z, NULL, 10);
1,110✔
5633
    if (pTaskStmt->tid <= 0) {
1,110✔
5634
      pCxt->errCode =
×
5635
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Task id should be greater than 0");
×
5636
      goto _err;
×
5637
    }
5638
  } else {
5639
    if (pIdOrName->n > TSDB_XNODE_RESOURCE_NAME_LEN + 2) {
×
5640
      pCxt->errCode =
×
5641
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5642
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_RESOURCE_NAME_LEN);
5643
      goto _err;
×
5644
    }
5645
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
×
5646
    COPY_STRING_FORM_STR_TOKEN(buf, pIdOrName);
×
5647
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
×
5648
  }
5649

5650
  return (SNode*)pTaskStmt;
1,110✔
5651
_err:
×
5652
  if (pStmt != NULL) {
×
5653
    nodesDestroyNode(pStmt);
×
5654
  }
5655
  return NULL;
×
5656
}
5657

5658
SNode* createStopXnodeTaskStmt(SAstCreateContext* pCxt, const EXnodeResourceType resourceType, SToken* pIdOrName) {
1,110✔
5659
  SNode* pStmt = NULL;
1,110✔
5660
  CHECK_PARSER_STATUS(pCxt);
1,110✔
5661
  if (resourceType != XNODE_TASK) {
1,110✔
5662
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5663
                                            "Only support stop task, invalid resource type: %d", resourceType);
5664
    goto _err;
×
5665
  }
5666
  if (pIdOrName != NULL && pIdOrName->type != TK_NK_INTEGER && pIdOrName->type != TK_NK_STRING) {
1,110✔
5667
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5668
                                            "Xnode task id or name should be an integer or string");
5669
    goto _err;
×
5670
  }
5671
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STOP_XNODE_TASK_STMT, (SNode**)&pStmt);
1,110✔
5672
  CHECK_MAKE_NODE(pStmt);
1,110✔
5673
  SStopXnodeTaskStmt* pTaskStmt = (SStopXnodeTaskStmt*)pStmt;
1,110✔
5674
  if (pIdOrName->type == TK_NK_INTEGER) {
1,110✔
5675
    pTaskStmt->tid = taosStr2Int32(pIdOrName->z, NULL, 10);
1,110✔
5676
    if (pTaskStmt->tid <= 0) {
1,110✔
5677
      pCxt->errCode =
×
5678
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Task id should be greater than 0");
×
5679
      goto _err;
×
5680
    }
5681
  } else {
5682
    if (pIdOrName->n > TSDB_XNODE_RESOURCE_NAME_LEN + 2) {
×
5683
      pCxt->errCode =
×
5684
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5685
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_RESOURCE_NAME_LEN);
5686
      goto _err;
×
5687
    }
5688
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
×
5689
    COPY_STRING_FORM_STR_TOKEN(buf, pIdOrName);
×
5690
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
×
5691
  }
5692

5693
  return (SNode*)pTaskStmt;
1,110✔
5694
_err:
×
5695
  if (pStmt != NULL) {
×
5696
    nodesDestroyNode(pStmt);
×
5697
  }
5698
  return NULL;
×
5699
}
5700

5701
SNode* rebalanceXnodeJobWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceId, SNode* pNode) {
2,220✔
5702
  SNode* pStmt = NULL;
2,220✔
5703
  if (pResourceId == NULL) {
2,220✔
5704
    pCxt->errCode =
×
5705
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should not be NULL");
×
5706
    goto _err;
×
5707
  }
5708
  if (pNode == NULL) {
2,220✔
5709
    pCxt->errCode =
×
5710
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job options should not be NULL");
×
5711
    goto _err;
×
5712
  }
5713
  if (pResourceId->type != TK_NK_INTEGER) {
2,220✔
5714
    pCxt->errCode =
×
5715
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should be an integer");
×
5716
    goto _err;
×
5717
  }
5718
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_STMT, (SNode**)&pStmt);
2,220✔
5719
  CHECK_MAKE_NODE(pStmt);
2,220✔
5720

5721
  SRebalanceXnodeJobStmt* pJobStmt = (SRebalanceXnodeJobStmt*)pStmt;
2,220✔
5722
  char                    buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
2,220✔
5723
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceId);
2,220✔
5724
  pJobStmt->jid = atoi(buf);
2,220✔
5725

5726
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
2,220✔
5727
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
2,220✔
5728
    // printXnodeTaskOptions(&options->opts);
5729
    pJobStmt->options = options;
2,220✔
5730
  }
5731
  return (SNode*)pJobStmt;
2,220✔
5732
_err:
×
5733
  return NULL;
×
5734
}
5735

5736
SNode* createRebalanceXnodeJobStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* resourceId,
2,220✔
5737
                                   SNode* pNodeOptions) {
5738
  CHECK_PARSER_STATUS(pCxt);
2,220✔
5739

5740
  switch (resourceType) {
2,220✔
5741
    case XNODE_JOB: {
2,220✔
5742
      return rebalanceXnodeJobWithOptionsDirectly(pCxt, resourceId, pNodeOptions);
2,220✔
5743
    }
5744
    default:
×
5745
      pCxt->errCode =
×
5746
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5747
                                  "Invalid xnode resource type: %d, rebalance only support job", resourceType);
5748
      goto _err;
×
5749
  }
5750
_err:
×
5751
  return NULL;
×
5752
}
5753

5754
SNode* rebalanceXnodeJobWhereDirectly(SAstCreateContext* pCxt, SNode* pWhere) {
1,221✔
5755
  int32_t code = 0;
1,221✔
5756
  SNode*  pStmt = NULL;
1,221✔
5757

5758
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_WHERE_STMT, (SNode**)&pStmt);
1,221✔
5759
  CHECK_MAKE_NODE(pStmt);
1,221✔
5760

5761
  SRebalanceXnodeJobWhereStmt* pJobStmt = (SRebalanceXnodeJobWhereStmt*)pStmt;
1,221✔
5762
  pJobStmt->pWhere = pWhere;
1,221✔
5763

5764
  return (SNode*)pJobStmt;
1,221✔
5765
_err:
×
5766
  return NULL;
×
5767
}
5768

5769
SNode* createRebalanceXnodeJobWhereStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
1,221✔
5770
  CHECK_PARSER_STATUS(pCxt);
1,221✔
5771

5772
  switch (resourceType) {
1,221✔
5773
    case XNODE_JOB: {
1,221✔
5774
      return rebalanceXnodeJobWhereDirectly(pCxt, pWhere);
1,221✔
5775
    }
5776
    default:
×
5777
      pCxt->errCode =
×
5778
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5779
                                  "Invalid xnode resource type: %d, rebalance only support job", resourceType);
5780
      goto _err;
×
5781
  }
5782
_err:
×
5783
  return NULL;
×
5784
}
5785

5786
SNode* updateXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pSource,
4,440✔
5787
                                          SNode* pSink, SNode* pNode) {
5788
  SNode* pStmt = NULL;
4,440✔
5789

5790
  if ((pSource != NULL && nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT) ||
4,440✔
5791
      (pSink != NULL && nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT)) {
2,220✔
5792
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5793
                                            "Xnode task source and sink should be valid nodes");
5794
    goto _err;
×
5795
  }
5796
  if (pSource == NULL && pSink == NULL && pNode == NULL) {
4,440✔
5797
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5798
                                            "Xnode task source, sink, and with options can't all be NULL");
5799
    goto _err;
×
5800
  }
5801

5802
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_XNODE_TASK_STMT, (SNode**)&pStmt);
4,440✔
5803
  CHECK_MAKE_NODE(pStmt);
4,440✔
5804
  SUpdateXnodeTaskStmt* pTaskStmt = (SUpdateXnodeTaskStmt*)pStmt;
4,440✔
5805
  if (pResIdOrName->type == TK_NK_INTEGER) {
4,440✔
5806
    pTaskStmt->tid = taosStr2Int32(pResIdOrName->z, NULL, 10);
4,440✔
5807
  } else {
5808
    if (pResIdOrName->n <= 2) {
×
5809
      pCxt->errCode =
×
5810
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name can't be empty string");
×
5811
      goto _err;
×
5812
    }
5813
    char buf[TSDB_XNODE_TASK_NAME_LEN] = {0};
×
5814
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
×
5815
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
×
5816
  }
5817

5818
  if (pSource != NULL) {
4,440✔
5819
    SXTaskSource* source = (SXTaskSource*)(pSource);
1,110✔
5820
    pTaskStmt->source = source;
1,110✔
5821
  }
5822
  if (pSink != NULL) {
4,440✔
5823
    SXTaskSink* sink = (SXTaskSink*)(pSink);
2,220✔
5824
    pTaskStmt->sink = sink;
2,220✔
5825
  }
5826
  if (pNode != NULL) {
4,440✔
5827
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
4,440✔
5828
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
4,440✔
5829
      pTaskStmt->options = options;
4,440✔
5830
    }
5831
  }
5832
  return (SNode*)pTaskStmt;
4,440✔
5833
_err:
×
5834
  if (pStmt != NULL) {
×
5835
    nodesDestroyNode(pStmt);
×
5836
  }
5837
  return NULL;
×
5838
}
5839

5840
SNode* alterXnodeJobWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pNode) {
×
5841
  SNode* pStmt = NULL;
×
5842
  if (pResourceName == NULL) {
×
5843
    pCxt->errCode =
×
5844
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should not be NULL");
×
5845
    goto _err;
×
5846
  }
5847
  if (pNode == NULL) {
×
5848
    pCxt->errCode =
×
5849
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job options should not be NULL");
×
5850
    goto _err;
×
5851
  }
5852
  if (pResourceName->type != TK_NK_INTEGER) {
×
5853
    pCxt->errCode =
×
5854
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should be integer");
×
5855
    goto _err;
×
5856
  }
5857
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_JOB_STMT, (SNode**)&pStmt);
×
5858
  CHECK_MAKE_NODE(pStmt);
×
5859

5860
  SAlterXnodeJobStmt* pJobStmt = (SAlterXnodeJobStmt*)pStmt;
×
5861
  char                buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
×
5862
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
5863
  pJobStmt->jid = atoi(buf);
×
5864

5865
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
×
5866
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
×
5867
    pJobStmt->options = options;
×
5868
  }
5869
  return (SNode*)pJobStmt;
×
5870
_err:
×
5871
  if (pStmt != NULL) {
×
5872
    nodesDestroyNode(pStmt);
×
5873
  }
5874
  return NULL;
×
5875
}
5876

5877
SNode* alterXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pNode) {
222✔
5878
  SNode* pStmt = NULL;
222✔
5879
  if (NULL == pNode) {
222✔
5880
    pCxt->errCode =
×
5881
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode alter agent options can't be null");
×
5882
    goto _err;
×
5883
  }
5884

5885
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_AGENT_STMT, (SNode**)&pStmt);
222✔
5886
  CHECK_MAKE_NODE(pStmt);
222✔
5887
  SAlterXnodeAgentStmt* pAgentStmt = (SAlterXnodeAgentStmt*)pStmt;
222✔
5888
  if (pResIdOrName->type == TK_NK_INTEGER) {
222✔
5889
    pAgentStmt->id = taosStr2Int32(pResIdOrName->z, NULL, 10);
×
5890
  } else {
5891
    if (pResIdOrName->n <= 2) {
222✔
5892
      pCxt->errCode =
×
5893
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode alter agent name can't be empty string");
×
5894
      goto _err;
×
5895
    }
5896
    char buf[TSDB_XNODE_AGENT_NAME_LEN] = {0};
222✔
5897
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
222✔
5898
    pAgentStmt->name = xCreateCowStr(strlen(buf), buf, true);
222✔
5899
  }
5900

5901
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
222✔
5902
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
222✔
5903
    pAgentStmt->options = options;
222✔
5904
  }
5905

5906
  return (SNode*)pAgentStmt;
222✔
5907
_err:
×
5908
  if (pStmt != NULL) {
×
5909
    nodesDestroyNode(pStmt);
×
5910
  }
5911
  return NULL;
×
5912
}
5913

5914
SNode* alterXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResIdOrName,
4,662✔
5915
                                 SNode* pSource, SNode* pSink, SNode* pNode) {
5916
  CHECK_PARSER_STATUS(pCxt);
4,662✔
5917

5918
  switch (resourceType) {
4,662✔
5919
    case XNODE_TASK: {
4,440✔
5920
      return updateXnodeTaskWithOptionsDirectly(pCxt, pResIdOrName, pSource, pSink, pNode);
4,440✔
5921
    }
5922
    case XNODE_AGENT: {
222✔
5923
      return alterXnodeAgentWithOptionsDirectly(pCxt, pResIdOrName, pNode);
222✔
5924
    }
5925
    case XNODE_JOB: {
×
5926
      return alterXnodeJobWithOptionsDirectly(pCxt, pResIdOrName, pNode);
×
5927
    }
5928
    default:
×
5929
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5930
                                              "Invalid xnode resource type: %d", resourceType);
5931
      goto _err;
×
5932
  }
5933
_err:
×
5934
  return NULL;
×
5935
}
5936

5937
SNode* dropXnodeResource(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SToken* pResourceName) {
7,881✔
5938
  SNode* pStmt = NULL;
7,881✔
5939
  char   buf[TSDB_XNODE_TASK_NAME_LEN + 1] = {0};
7,881✔
5940

5941
  CHECK_PARSER_STATUS(pCxt);
7,881✔
5942
  if (pResourceName == NULL || pResourceName->n <= 0) {
7,881✔
5943
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5944
                                            "Xnode resource name should not be NULL or empty");
5945
    goto _err;
×
5946
  }
5947
  if (pResourceName->n > TSDB_XNODE_RESOURCE_NAME_LEN) {
7,881✔
5948
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5949
                                            "Invalid xnode resource name length: %d, max length: %d", pResourceName->n,
5950
                                            TSDB_XNODE_RESOURCE_NAME_LEN);
5951
    goto _err;
×
5952
  }
5953
  switch (resourceType) {
7,881✔
5954
    case XNODE_TASK:
5,550✔
5955
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
5,550✔
5956
      CHECK_MAKE_NODE(pStmt);
5,550✔
5957
      SDropXnodeTaskStmt* pTaskStmt = (SDropXnodeTaskStmt*)pStmt;
5,550✔
5958

5959
      if (pResourceName->type == TK_NK_STRING) {
5,550✔
5960
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
4,440✔
5961
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5962
                                                  "Invalid xnode task name length: %d, max length: %d",
5963
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
5964
          goto _err;
×
5965
        }
5966
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
4,440✔
5967
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
4,440✔
5968
      } else if (pResourceName->type == TK_NK_ID) {
1,110✔
5969
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
5970
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
×
5971
      } else if (pResourceName->type == TK_NK_INTEGER) {
1,110✔
5972
        pTaskStmt->id = taosStr2Int32(pResourceName->z, NULL, 10);
1,110✔
5973
      } else {
5974
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5975
                                                "Invalid xnode job id type: %d", pResourceName->type);
5976
        goto _err;
×
5977
      }
5978
      break;
5,550✔
5979
    case XNODE_AGENT:
1,221✔
5980
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
1,221✔
5981
      CHECK_MAKE_NODE(pStmt);
1,221✔
5982
      SDropXnodeAgentStmt* pDropAgent = (SDropXnodeAgentStmt*)pStmt;
1,221✔
5983

5984
      if (pResourceName->type == TK_NK_STRING) {
1,221✔
5985
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
1,221✔
5986
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5987
                                                  "Invalid xnode task name length: %d, max length: %d",
5988
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
5989
          goto _err;
×
5990
        }
5991
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
1,221✔
5992
        pDropAgent->name = taosStrndupi(buf, sizeof(buf));
1,221✔
5993
      } else if (pResourceName->type == TK_NK_ID) {
×
5994
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
5995
        pDropAgent->name = taosStrndupi(buf, sizeof(buf));
×
5996
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
5997
        pDropAgent->id = taosStr2Int32(pResourceName->z, NULL, 10);
×
5998
      } else {
5999
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6000
                                                "Invalid xnode agent id type: %d", pResourceName->type);
6001
        goto _err;
×
6002
      }
6003
      break;
1,221✔
6004
    case XNODE_JOB:
1,110✔
6005
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_JOB_STMT, (SNode**)&pStmt);
1,110✔
6006
      CHECK_MAKE_NODE(pStmt);
1,110✔
6007
      SDropXnodeJobStmt* pJobStmt = (SDropXnodeJobStmt*)pStmt;
1,110✔
6008

6009
      if (pResourceName->type == TK_NK_STRING) {
1,110✔
6010
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
×
6011
      } else if (pResourceName->type == TK_NK_INTEGER) {
1,110✔
6012
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
1,110✔
6013
      } else {
6014
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6015
                                                "Invalid xnode job id type: %d", pResourceName->type);
6016
        goto _err;
×
6017
      }
6018
      break;
1,110✔
6019
    default:
×
6020
      break;
×
6021
  }
6022
  return (SNode*)pStmt;
7,881✔
6023
_err:
×
6024
  if (pStmt != NULL) {
×
6025
    nodesDestroyNode(pStmt);
×
6026
  }
6027
  return NULL;
×
6028
}
6029
SNode* dropXnodeResourceOn(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SToken* pResource, SNode* pWhere) {
×
6030
  char resourceId[TSDB_XNODE_RESOURCE_ID_LEN + 1];
6031
  SShowStmt* pStmt = NULL;
×
6032

6033
  CHECK_PARSER_STATUS(pCxt);
×
6034
  if (pResource == NULL || pResource->n <= 0) {
×
6035
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6036
                                            "xnode resource name should not be NULL or empty");
6037
    goto _err;
×
6038
  }
6039
  if (pResource->n > TSDB_XNODE_RESOURCE_NAME_LEN) {
×
6040
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6041
                                            "Invalid xnode resource name length: %d, max length: %d", pResource->n,
6042
                                            TSDB_XNODE_RESOURCE_NAME_LEN);
6043
    goto _err;
×
6044
  }
6045
  switch (resourceType) {
×
6046
    case XNODE_TASK:
×
6047
      // pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
6048
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_TASKS_STMT, (SNode**)&pStmt);
×
6049
      CHECK_MAKE_NODE(pStmt);
×
6050
      break;
×
6051
    case XNODE_AGENT:
×
6052
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
×
6053
      CHECK_MAKE_NODE(pStmt);
×
6054
      break;
×
6055
    case XNODE_JOB:
×
6056
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_JOBS_STMT, (SNode**)&pStmt);
×
6057
      CHECK_MAKE_NODE(pStmt);
×
6058
      break;
×
6059
    default:
×
6060
      break;
×
6061
  }
6062
  return (SNode*)pStmt;
×
6063
_err:
×
6064
  return NULL;
×
6065
}
6066
// SNode* dropXnodeResourceWhere(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
6067
//   CHECK_PARSER_STATUS(pCxt);
6068
//   SShowStmt* pStmt = NULL;
6069
//   switch (resourceType) {
6070
//     case XNODE_TASK:
6071
//       // pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
6072
//       pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_TASKS_STMT, (SNode**)&pStmt);
6073
//       CHECK_MAKE_NODE(pStmt);
6074
//       break;
6075
//     case XNODE_AGENT:
6076
//       // pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
6077
//       pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_AGENTS_STMT, (SNode**)&pStmt);
6078
//       CHECK_MAKE_NODE(pStmt);
6079
//       break;
6080
//     case XNODE_JOB:
6081
//       pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_JOBS_STMT, (SNode**)&pStmt);
6082
//       CHECK_MAKE_NODE(pStmt);
6083
//       break;
6084
//     default:
6085
//       break;
6086
//   }
6087
//   return (SNode*)pStmt;
6088
// _err:
6089
//   return NULL;
6090
// }
6091
SNode* createDefaultXnodeTaskOptions(SAstCreateContext* pCxt) {
14,985✔
6092
  CHECK_PARSER_STATUS(pCxt);
14,985✔
6093
  SXnodeTaskOptions* pOptions = NULL;
14,985✔
6094
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_OPTIONS, (SNode**)&pOptions);
14,985✔
6095
  CHECK_MAKE_NODE(pOptions);
14,985✔
6096
  return (SNode*)pOptions;
14,985✔
6097
_err:
×
6098
  return NULL;
×
6099
}
6100

6101
static char   TRIGGER[8] = "trigger";
6102
static SToken TRIGGER_TOKEN = {
6103
    .n = 7,
6104
    .type = TK_NK_ID,
6105
    .z = TRIGGER,
6106
};
6107
SToken* createTriggerToken() { return &TRIGGER_TOKEN; }
×
6108

6109
SNode*  setXnodeTaskOption(SAstCreateContext* pCxt, SNode* pTaskOptions, SToken* pKey, SToken* pVal) {
32,301✔
6110
  CHECK_PARSER_STATUS(pCxt);
32,301✔
6111
  if (pTaskOptions == NULL) {
32,301✔
6112
    pTaskOptions = createDefaultXnodeTaskOptions(pCxt);
14,985✔
6113
    if (pTaskOptions == NULL) {
14,985✔
6114
      pCxt->errCode =
×
6115
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task options should not be NULL");
×
6116
      goto _err;
×
6117
    }
6118
  }
6119
  SXnodeTaskOptions* pOptions = (SXnodeTaskOptions*)pTaskOptions;
32,301✔
6120
  char               key[TSDB_COL_NAME_LEN] = {0};
32,301✔
6121
  if (pKey == NULL) {
32,301✔
6122
    pCxt->errCode =
×
6123
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
6124
    goto _err;
×
6125
  }
6126
  TRIM_STRING_FORM_ID_TOKEN(key, pKey);
32,301✔
6127

6128
  if (strlen(key) == 0) {
32,301✔
6129
    pCxt->errCode =
×
6130
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
6131
    goto _err;
×
6132
  }
6133
  char via[TSDB_COL_NAME_LEN] = {0};
32,301✔
6134
  char buf[TSDB_XNODE_TASK_OPTIONS_MAX_NUM] = {0};
32,301✔
6135
  if (strcmp(key, "trigger") == 0) {
32,301✔
6136
    if (pVal->type == TK_NK_STRING) {
4,773✔
6137
      (void)trimString(pVal->z, pVal->n, pOptions->trigger, sizeof(pOptions->trigger));
4,773✔
6138
      pOptions->triggerLen = pVal->n == 2 ? 1 : pVal->n - 2;
4,773✔
6139
    } else {
6140
      pCxt->errCode =
×
6141
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option trigger must be string");
×
6142
      goto _err;
×
6143
    }
6144
  } else if (strcmp(key, "parser") == 0 || strcmp(key, "transform") == 0) {
27,528✔
6145
    if (pVal->type == TK_NK_STRING) {
4,440✔
6146
      (void)trimString(pVal->z, pVal->n, pOptions->parser, sizeof(pOptions->parser));
4,440✔
6147
      pOptions->parserLen = pVal->n == 2 ? 1 : pVal->n - 2;
4,440✔
6148
    } else {
6149
      pCxt->errCode =
×
6150
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option parser must be string");
×
6151
      goto _err;
×
6152
    }
6153
  } else if (strcmp(key, "health") == 0) {
23,088✔
6154
    if (pVal->type == TK_NK_STRING) {
×
6155
      (void)trimString(pVal->z, pVal->n, pOptions->health, sizeof(pOptions->health));
×
6156
      pOptions->healthLen = pVal->n == 2 ? 1 : pVal->n - 2;
×
6157
    } else {
6158
      pCxt->errCode =
×
6159
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option health must be string");
×
6160
      goto _err;
×
6161
    }
6162
  } else if (strcmp(key, "via") == 0) {
23,088✔
6163
    switch (pVal->type) {
×
6164
      case TK_NK_STRING:
×
6165
        (void)trimString(pVal->z, pVal->n, via, sizeof(via));
×
6166
        pOptions->via = taosStr2Int32(via, NULL, 10);
×
6167
        if (pOptions->via <= 0) {
×
6168
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6169
                                                   "Invalid xnode task option via: %s", pVal->z);
6170
          goto _err;
×
6171
        }
6172
        break;
×
6173
      case TK_NK_INTEGER:
×
6174
        pOptions->via = taosStr2Int32(pVal->z, NULL, 10);
×
6175
        break;
×
6176
      default:
×
6177
        pCxt->errCode =
×
6178
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode task option: %s", key);
×
6179
    }
6180
  } else {
6181
    if (pOptions->optionsNum < TSDB_XNODE_TASK_OPTIONS_MAX_NUM) {
23,088✔
6182
      char* pKeyVal = NULL;
23,088✔
6183
      if (pVal != NULL) {
23,088✔
6184
        parserDebug("key value length expected: %d, actual: %d\n", pKey->n + pVal->n + 2, pKey->n + pVal->n);
23,088✔
6185
        pKeyVal = taosMemoryMalloc(pKey->n + pVal->n + 2);
23,088✔
6186
        memset(pKeyVal, 0, pKey->n + pVal->n + 2);
23,088✔
6187

6188
        CHECK_OUT_OF_MEM(pKeyVal);
23,088✔
6189
        size_t pos = strlen(key);
23,088✔
6190
        memcpy(pKeyVal, key, pos);
23,088✔
6191
        pKeyVal[pos] = '=';  // Add '=' after the key
23,088✔
6192
        pos++;
23,088✔
6193

6194
        if (pVal->type == TK_NK_STRING) {
23,088✔
6195
          (void)trimString(pVal->z, pVal->n, pKeyVal + pos, pVal->n + 1);
7,548✔
6196
        } else {
6197
          strncpy(pKeyVal + pos, pVal->z, TMIN(pVal->n, pKey->n + pVal->n + 2 - pos - 1));
15,540✔
6198
          pKeyVal[pos + pVal->n] = '\0';
15,540✔
6199
        }
6200
      } else {
6201
        size_t keyLen = strlen(key);
×
6202
        pKeyVal = taosMemoryMalloc(keyLen + 1);
×
6203
        memset(pKeyVal, 0, keyLen + 1);
×
6204
        CHECK_OUT_OF_MEM(pKeyVal);
×
6205
        memcpy(pKeyVal, key, keyLen);
×
6206
      }
6207
      pOptions->options[pOptions->optionsNum] = pKeyVal;
23,088✔
6208
      pOptions->optionsNum++;
23,088✔
6209
    } else {
6210
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6211
                                               "reaches max options number(%d) %s", pOptions->optionsNum, key);
6212
      goto _err;
×
6213
    }
6214
  }
6215
_err:
32,301✔
6216
  return pTaskOptions;
32,301✔
6217
}
6218

6219
SNode* createXnodeTaskJobWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pTidToken,
111✔
6220
                                     SNode* pNodeOptions) {
6221
  CHECK_PARSER_STATUS(pCxt);
111✔
6222
  SNode* pStmt = NULL;
111✔
6223

6224
  switch (resourceType) {
111✔
6225
    case XNODE_JOB: {
111✔
6226
      if (pTidToken == NULL || pTidToken->n <= 0) {
111✔
6227
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6228
                                                "Xnode job task id should not be NULL or empty");
6229
        goto _err;
×
6230
      }
6231
      if (pNodeOptions == NULL || nodeType(pNodeOptions) != QUERY_NODE_XNODE_TASK_OPTIONS) {
111✔
6232
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6233
                                                "Xnode job options should not be NULL or empty");
6234
        goto _err;
×
6235
      }
6236
      pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_JOB_STMT, &pStmt);
111✔
6237
      CHECK_MAKE_NODE(pStmt);
111✔
6238
      SCreateXnodeJobStmt* pJobStmt = (SCreateXnodeJobStmt*)pStmt;
111✔
6239
      pJobStmt->options = (SXnodeTaskOptions*)pNodeOptions;
111✔
6240
      pJobStmt->tid = pTidToken->type == TK_NK_STRING ? atoi(pTidToken->z) : taosStr2Int32(pTidToken->z, NULL, 10);
111✔
6241
      break;
111✔
6242
    }
6243
    default:
×
6244
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6245
                                              "Invalid xnode resource type: %d with ON clause", resourceType);
6246
      goto _err;
×
6247
  }
6248
  return pStmt;
111✔
6249
_err:
×
6250
  if (pStmt != NULL) {
×
6251
    nodesDestroyNode(pStmt);
×
6252
  }
6253
  return NULL;
×
6254
}
6255

6256
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
1,647✔
6257
  SToken config;
1,647✔
6258
  config.type = TK_NK_STRING;
1,647✔
6259
  config.z = "\"encrypt_key\"";
1,647✔
6260
  config.n = strlen(config.z);
1,647✔
6261
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
1,647✔
6262
}
6263

6264
SNode* createAlterEncryptKeyStmt(SAstCreateContext* pCxt, int8_t keyType, const SToken* pValue) {
×
6265
  CHECK_PARSER_STATUS(pCxt);
×
6266
  SAlterEncryptKeyStmt* pStmt = NULL;
×
6267
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ENCRYPT_KEY_STMT, (SNode**)&pStmt);
×
6268
  CHECK_MAKE_NODE(pStmt);
×
6269

6270
  pStmt->keyType = keyType;
×
6271
  if (NULL != pValue) {
×
6272
    (void)trimString(pValue->z, pValue->n, pStmt->newKey, sizeof(pStmt->newKey));
×
6273
  }
6274

6275
  return (SNode*)pStmt;
×
6276
_err:
×
6277
  return NULL;
×
6278
}
6279

6280
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
3,318✔
6281
  if (!checkIndexName(pCxt, pIndexName)) {
3,318✔
6282
    return NULL;
×
6283
  }
6284
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
3,318✔
6285
}
6286

6287
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
11,875✔
6288
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
6289
  CHECK_PARSER_STATUS(pCxt);
11,875✔
6290
  SCreateIndexStmt* pStmt = NULL;
11,875✔
6291
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
11,875✔
6292
  CHECK_MAKE_NODE(pStmt);
11,875✔
6293
  pStmt->indexType = type;
11,875✔
6294
  pStmt->ignoreExists = ignoreExists;
11,875✔
6295

6296
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
11,875✔
6297
  if (strlen(pFullTable->table.dbName) == 0) {
11,875✔
6298
    // no db specified,
6299
    if (pCxt->pQueryCxt->db == NULL) {
×
6300
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
6301
      CHECK_PARSER_STATUS(pCxt);
×
6302
    } else {
6303
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
6304
    }
6305
  } else {
6306
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
11,875✔
6307
  }
6308
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
11,875✔
6309
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
11,875✔
6310
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
11,875✔
6311
  nodesDestroyNode(pIndexName);
11,875✔
6312
  nodesDestroyNode(pRealTable);
11,875✔
6313
  pStmt->pCols = pCols;
11,875✔
6314
  pStmt->pOptions = (SIndexOptions*)pOptions;
11,875✔
6315
  return (SNode*)pStmt;
11,875✔
6316
_err:
×
6317
  nodesDestroyNode(pIndexName);
×
6318
  nodesDestroyNode(pRealTable);
×
6319
  nodesDestroyNode(pOptions);
×
6320
  nodesDestroyList(pCols);
×
6321
  return NULL;
×
6322
}
6323

6324
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
×
6325
                         SNode* pStreamOptions) {
6326
  CHECK_PARSER_STATUS(pCxt);
×
6327
  SIndexOptions* pOptions = NULL;
×
6328
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
×
6329
  CHECK_MAKE_NODE(pOptions);
×
6330
  pOptions->pFuncs = pFuncs;
×
6331
  pOptions->pInterval = pInterval;
×
6332
  pOptions->pOffset = pOffset;
×
6333
  pOptions->pSliding = pSliding;
×
6334
  pOptions->pStreamOptions = pStreamOptions;
×
6335
  return (SNode*)pOptions;
×
6336
_err:
×
6337
  nodesDestroyNode(pInterval);
×
6338
  nodesDestroyNode(pOffset);
×
6339
  nodesDestroyNode(pSliding);
×
6340
  nodesDestroyNode(pStreamOptions);
×
6341
  return NULL;
×
6342
}
6343

6344
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
3,318✔
6345
  CHECK_PARSER_STATUS(pCxt);
3,318✔
6346
  SDropIndexStmt* pStmt = NULL;
3,318✔
6347
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
3,318✔
6348
  CHECK_MAKE_NODE(pStmt);
3,318✔
6349
  pStmt->ignoreNotExists = ignoreNotExists;
3,318✔
6350
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
3,318✔
6351
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
3,318✔
6352
  nodesDestroyNode(pIndexName);
3,318✔
6353
  return (SNode*)pStmt;
3,318✔
6354
_err:
×
6355
  nodesDestroyNode(pIndexName);
×
6356
  return NULL;
×
6357
}
6358

6359
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
98,918✔
6360
  CHECK_PARSER_STATUS(pCxt);
98,918✔
6361
  SCreateComponentNodeStmt* pStmt = NULL;
98,918✔
6362
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
98,918✔
6363
  CHECK_MAKE_NODE(pStmt);
98,918✔
6364
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
98,918✔
6365
  return (SNode*)pStmt;
98,918✔
6366
_err:
×
6367
  return NULL;
×
6368
}
6369

6370
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
46,870✔
6371
  CHECK_PARSER_STATUS(pCxt);
46,870✔
6372
  SDropComponentNodeStmt* pStmt = NULL;
46,870✔
6373
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
46,870✔
6374
  CHECK_MAKE_NODE(pStmt);
46,870✔
6375
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
46,870✔
6376
  return (SNode*)pStmt;
46,870✔
6377
_err:
×
6378
  return NULL;
×
6379
}
6380

6381
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
1,894✔
6382
  CHECK_PARSER_STATUS(pCxt);
1,894✔
6383
  SRestoreComponentNodeStmt* pStmt = NULL;
1,894✔
6384
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,894✔
6385
  CHECK_MAKE_NODE(pStmt);
1,894✔
6386
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1,894✔
6387
  return (SNode*)pStmt;
1,894✔
6388
_err:
×
6389
  return NULL;
×
6390
}
6391

6392
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery, bool reload) {
134,560✔
6393
  CHECK_PARSER_STATUS(pCxt);
134,560✔
6394
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
134,560✔
6395
  SCreateTopicStmt* pStmt = NULL;
134,033✔
6396
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
134,033✔
6397
  CHECK_MAKE_NODE(pStmt);
134,033✔
6398
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
134,033✔
6399
  pStmt->ignoreExists = ignoreExists;
134,033✔
6400
  pStmt->pQuery = pQuery;
134,033✔
6401
  pStmt->reload = reload;
134,033✔
6402
  return (SNode*)pStmt;
134,033✔
6403
_err:
527✔
6404
  nodesDestroyNode(pQuery);
527✔
6405
  return NULL;
527✔
6406
}
6407

6408
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
24,367✔
6409
                                  int8_t withMeta) {
6410
  CHECK_PARSER_STATUS(pCxt);
24,367✔
6411
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
24,367✔
6412
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
24,367✔
6413
  SCreateTopicStmt* pStmt = NULL;
24,367✔
6414
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
24,367✔
6415
  CHECK_MAKE_NODE(pStmt);
24,367✔
6416
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
24,367✔
6417
  pStmt->ignoreExists = ignoreExists;
24,367✔
6418
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
24,367✔
6419
  pStmt->withMeta = withMeta;
24,367✔
6420
  return (SNode*)pStmt;
24,367✔
6421
_err:
×
6422
  return NULL;
×
6423
}
6424

6425
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
11,165✔
6426
                                     int8_t withMeta, SNode* pWhere) {
6427
  CHECK_PARSER_STATUS(pCxt);
11,165✔
6428
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
11,165✔
6429
  SCreateTopicStmt* pStmt = NULL;
11,165✔
6430
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
11,165✔
6431
  CHECK_MAKE_NODE(pStmt);
11,165✔
6432
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
11,165✔
6433
  pStmt->ignoreExists = ignoreExists;
11,165✔
6434
  pStmt->withMeta = withMeta;
11,165✔
6435
  pStmt->pWhere = pWhere;
11,165✔
6436

6437
  tstrncpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
11,165✔
6438
  tstrncpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
11,165✔
6439
  nodesDestroyNode(pRealTable);
11,165✔
6440
  return (SNode*)pStmt;
11,165✔
6441
_err:
×
6442
  nodesDestroyNode(pRealTable);
×
6443
  nodesDestroyNode(pWhere);
×
6444
  return NULL;
×
6445
}
6446

6447
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName, bool force) {
107,786✔
6448
  CHECK_PARSER_STATUS(pCxt);
107,786✔
6449
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
107,786✔
6450
  SDropTopicStmt* pStmt = NULL;
107,603✔
6451
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
107,603✔
6452
  CHECK_MAKE_NODE(pStmt);
107,603✔
6453
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
107,603✔
6454
  pStmt->ignoreNotExists = ignoreNotExists;
107,603✔
6455
  pStmt->force = force;
107,603✔
6456
  return (SNode*)pStmt;
107,603✔
6457
_err:
183✔
6458
  return NULL;
183✔
6459
}
6460

6461
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName,
829✔
6462
                            bool force) {
6463
  CHECK_PARSER_STATUS(pCxt);
829✔
6464
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
829✔
6465
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
829✔
6466
  SDropCGroupStmt* pStmt = NULL;
829✔
6467
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
829✔
6468
  CHECK_MAKE_NODE(pStmt);
829✔
6469
  pStmt->ignoreNotExists = ignoreNotExists;
829✔
6470
  pStmt->force = force;
829✔
6471
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
829✔
6472
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
829✔
6473
  return (SNode*)pStmt;
829✔
6474
_err:
×
6475
  return NULL;
×
6476
}
6477

6478
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
936✔
6479
  CHECK_PARSER_STATUS(pCxt);
936✔
6480
  SAlterClusterStmt* pStmt = NULL;
936✔
6481
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
936✔
6482
  CHECK_MAKE_NODE(pStmt);
936✔
6483
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
936✔
6484
  if (NULL != pValue) {
936✔
6485
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
936✔
6486
  }
6487
  return (SNode*)pStmt;
936✔
6488
_err:
×
6489
  return NULL;
×
6490
}
6491

6492
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
231,039✔
6493
  CHECK_PARSER_STATUS(pCxt);
231,039✔
6494
  SAlterLocalStmt* pStmt = NULL;
231,039✔
6495
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
231,039✔
6496
  CHECK_MAKE_NODE(pStmt);
231,039✔
6497
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
231,039✔
6498
  if (NULL != pValue) {
231,039✔
6499
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
225,941✔
6500
  }
6501
  return (SNode*)pStmt;
231,039✔
6502
_err:
×
6503
  return NULL;
×
6504
}
6505

6506
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
63,926,601✔
6507
  CHECK_PARSER_STATUS(pCxt);
63,926,601✔
6508
  SExplainOptions* pOptions = NULL;
63,926,601✔
6509
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
63,926,601✔
6510
  CHECK_MAKE_NODE(pOptions);
63,926,601✔
6511
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
63,926,601✔
6512
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
63,926,601✔
6513
  return (SNode*)pOptions;
63,926,601✔
6514
_err:
×
6515
  return NULL;
×
6516
}
6517

6518
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
38,477,442✔
6519
  CHECK_PARSER_STATUS(pCxt);
38,477,442✔
6520
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
38,477,442✔
6521
  return pOptions;
38,477,442✔
6522
_err:
×
6523
  return NULL;
×
6524
}
6525

6526
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
1,530,000✔
6527
  CHECK_PARSER_STATUS(pCxt);
1,530,000✔
6528
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1,530,000✔
6529
  return pOptions;
1,530,000✔
6530
_err:
×
6531
  return NULL;
×
6532
}
6533

6534
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
59,984,961✔
6535
  CHECK_PARSER_STATUS(pCxt);
59,984,961✔
6536
  SExplainStmt* pStmt = NULL;
59,984,961✔
6537
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
59,984,961✔
6538
  CHECK_MAKE_NODE(pStmt);
59,984,961✔
6539
  pStmt->analyze = analyze;
59,984,961✔
6540
  pStmt->pOptions = (SExplainOptions*)pOptions;
59,984,961✔
6541
  pStmt->pQuery = pQuery;
59,984,961✔
6542
  return (SNode*)pStmt;
59,984,961✔
6543
_err:
×
6544
  nodesDestroyNode(pOptions);
×
6545
  nodesDestroyNode(pQuery);
×
6546
  return NULL;
×
6547
}
6548

6549
SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
496,814✔
6550
  CHECK_PARSER_STATUS(pCxt);
496,814✔
6551
  SDescribeStmt* pStmt = NULL;
496,814✔
6552
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT, (SNode**)&pStmt);
496,814✔
6553
  CHECK_MAKE_NODE(pStmt);
496,814✔
6554
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
496,814✔
6555
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
496,814✔
6556
  nodesDestroyNode(pRealTable);
496,814✔
6557
  return (SNode*)pStmt;
496,814✔
6558
_err:
×
6559
  nodesDestroyNode(pRealTable);
×
6560
  return NULL;
×
6561
}
6562

6563
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
2,010,790✔
6564
  CHECK_PARSER_STATUS(pCxt);
2,010,790✔
6565
  SNode* pStmt = NULL;
2,010,790✔
6566
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
2,010,790✔
6567
  CHECK_MAKE_NODE(pStmt);
2,010,790✔
6568
  return pStmt;
2,010,790✔
6569
_err:
×
6570
  return NULL;
×
6571
}
6572

6573
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
31,828✔
6574
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
31,828✔
6575
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
28,701✔
6576
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
3,127✔
6577
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
3,127✔
6578
  } else {
6579
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6580
                                            "udf programming language supports c and python");
6581
  }
6582
  return pCxt->errCode;
31,828✔
6583
}
6584

6585
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
31,828✔
6586
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
6587
                                bool orReplace) {
6588
  CHECK_PARSER_STATUS(pCxt);
31,828✔
6589
  if (pLibPath->n <= 2) {
31,828✔
6590
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
6591
    CHECK_PARSER_STATUS(pCxt);
×
6592
  }
6593
  int8_t language = 0;
31,828✔
6594
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
31,828✔
6595
  CHECK_PARSER_STATUS(pCxt);
31,828✔
6596
  SCreateFunctionStmt* pStmt = NULL;
31,828✔
6597
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
31,828✔
6598
  CHECK_MAKE_NODE(pStmt);
31,828✔
6599
  pStmt->orReplace = orReplace;
31,828✔
6600
  pStmt->ignoreExists = ignoreExists;
31,828✔
6601
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
31,828✔
6602
  pStmt->isAgg = aggFunc;
31,828✔
6603
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
31,828✔
6604
  pStmt->outputDt = dataType;
31,828✔
6605
  pStmt->bufSize = bufSize;
31,828✔
6606
  pStmt->language = language;
31,828✔
6607
  return (SNode*)pStmt;
31,828✔
6608
_err:
×
6609
  return NULL;
×
6610
}
6611

6612
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
23,715✔
6613
  CHECK_PARSER_STATUS(pCxt);
23,715✔
6614
  SDropFunctionStmt* pStmt = NULL;
23,715✔
6615
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
23,715✔
6616
  CHECK_MAKE_NODE(pStmt);
23,715✔
6617
  pStmt->ignoreNotExists = ignoreNotExists;
23,715✔
6618
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
23,715✔
6619
  return (SNode*)pStmt;
23,715✔
6620
_err:
×
6621
  return NULL;
×
6622
}
6623

6624
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
18,206✔
6625
  SCreateViewStmt* pStmt = NULL;
18,206✔
6626
  CHECK_PARSER_STATUS(pCxt);
18,206✔
6627
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
18,206✔
6628
  CHECK_MAKE_NODE(pStmt);
18,206✔
6629
  int32_t i = pAs->n;
18,206✔
6630
  while (isspace(*(pAs->z + i))) {
36,412✔
6631
    ++i;
18,206✔
6632
  }
6633
  pStmt->pQuerySql = tstrdup(pAs->z + i);
18,206✔
6634
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
18,206✔
6635
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
18,206✔
6636
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
18,206✔
6637
  nodesDestroyNode(pView);
18,206✔
6638
  pStmt->orReplace = orReplace;
18,206✔
6639
  pStmt->pQuery = pQuery;
18,206✔
6640
  return (SNode*)pStmt;
18,206✔
6641
_err:
×
6642
  nodesDestroyNode(pView);
×
6643
  nodesDestroyNode(pQuery);
×
6644
  nodesDestroyNode((SNode*)pStmt);
×
6645
  return NULL;
×
6646
}
6647

6648
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
366✔
6649
  CHECK_PARSER_STATUS(pCxt);
366✔
6650
  SDropViewStmt* pStmt = NULL;
×
6651
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
×
6652
  CHECK_MAKE_NODE(pStmt);
×
6653
  pStmt->ignoreNotExists = ignoreNotExists;
×
6654
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
×
6655
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
×
6656
  nodesDestroyNode(pView);
×
6657
  return (SNode*)pStmt;
×
6658
_err:
366✔
6659
  nodesDestroyNode(pView);
366✔
6660
  return NULL;
366✔
6661
}
6662

6663
SNode* createStreamOutTableNode(SAstCreateContext* pCxt, SNode* pIntoTable, SNode* pOutputSubTable, SNodeList* pColList,
275,427✔
6664
                                SNodeList* pTagList) {
6665
  SStreamOutTableNode* pOutTable = NULL;
275,427✔
6666
  CHECK_PARSER_STATUS(pCxt);
275,427✔
6667
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OUT_TABLE, (SNode**)&pOutTable);
275,244✔
6668
  CHECK_MAKE_NODE(pOutTable);
275,244✔
6669
  pOutTable->pOutTable = pIntoTable;
275,244✔
6670
  pOutTable->pSubtable = pOutputSubTable;
275,244✔
6671
  pOutTable->pCols = pColList;
275,244✔
6672
  pOutTable->pTags = pTagList;
275,244✔
6673
  return (SNode*)pOutTable;
275,244✔
6674

6675
_err:
183✔
6676
  nodesDestroyNode((SNode*)pOutTable);
183✔
6677
  nodesDestroyNode(pIntoTable);
183✔
6678
  nodesDestroyNode(pOutputSubTable);
183✔
6679
  nodesDestroyList(pColList);
183✔
6680
  nodesDestroyList(pTagList);
183✔
6681
  return NULL;
183✔
6682
}
6683

6684
SNode* createStreamTriggerNode(SAstCreateContext* pCxt, SNode* pTriggerWindow, SNode* pTriggerTable,
279,788✔
6685
                               SNodeList* pPartitionList, SNode* pOptions, SNode* pNotification) {
6686
  SStreamTriggerNode* pTrigger = NULL;
279,788✔
6687
  CHECK_PARSER_STATUS(pCxt);
279,788✔
6688
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER, (SNode**)&pTrigger);
279,714✔
6689
  CHECK_MAKE_NODE(pTrigger);
279,714✔
6690

6691
  pTrigger->pOptions = pOptions;
279,714✔
6692
  pTrigger->pNotify = pNotification;
279,714✔
6693
  pTrigger->pTrigerTable = pTriggerTable;
279,714✔
6694
  pTrigger->pPartitionList = pPartitionList;
279,714✔
6695
  pTrigger->pTriggerWindow = pTriggerWindow;
279,714✔
6696
  return (SNode*)pTrigger;
279,714✔
6697

6698
_err:
74✔
6699
  nodesDestroyNode((SNode*)pTrigger);
74✔
6700
  nodesDestroyNode(pTriggerWindow);
74✔
6701
  nodesDestroyNode(pTriggerTable);
74✔
6702
  nodesDestroyNode(pOptions);
74✔
6703
  nodesDestroyNode(pNotification);
74✔
6704
  nodesDestroyList(pPartitionList);
74✔
6705
  return NULL;
74✔
6706
}
6707

6708
SNode* createSlidingWindowNode(SAstCreateContext* pCxt, SNode* pSlidingVal, SNode* pOffset) {
104,367✔
6709
  SSlidingWindowNode* pSliding = NULL;
104,367✔
6710
  CHECK_PARSER_STATUS(pCxt);
104,367✔
6711
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SLIDING_WINDOW, (SNode**)&pSliding);
104,367✔
6712
  CHECK_MAKE_NODE(pSliding);
104,367✔
6713
  pSliding->pSlidingVal = pSlidingVal;
104,367✔
6714
  pSliding->pOffset = pOffset;
104,367✔
6715
  return (SNode*)pSliding;
104,367✔
6716
_err:
×
6717
  nodesDestroyNode(pSlidingVal);
×
6718
  nodesDestroyNode(pOffset);
×
6719
  nodesDestroyNode((SNode*)pSliding);
×
6720
  return NULL;
×
6721
}
6722

6723
SNode* createStreamTriggerOptions(SAstCreateContext* pCxt) {
111,812✔
6724
  SStreamTriggerOptions* pOptions = NULL;
111,812✔
6725
  CHECK_PARSER_STATUS(pCxt);
111,812✔
6726
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER_OPTIONS, (SNode**)&pOptions);
111,812✔
6727
  CHECK_MAKE_NODE(pOptions);
111,812✔
6728
  pOptions->pPreFilter = NULL;
111,812✔
6729
  pOptions->pWaterMark = NULL;
111,812✔
6730
  pOptions->pMaxDelay = NULL;
111,812✔
6731
  pOptions->pExpiredTime = NULL;
111,812✔
6732
  pOptions->pFillHisStartTime = NULL;
111,812✔
6733
  pOptions->pEventType = EVENT_NONE;
111,812✔
6734
  pOptions->calcNotifyOnly = false;
111,812✔
6735
  pOptions->deleteOutputTable = false;
111,812✔
6736
  pOptions->deleteRecalc = false;
111,812✔
6737
  pOptions->fillHistory = false;
111,812✔
6738
  pOptions->fillHistoryFirst = false;
111,812✔
6739
  pOptions->lowLatencyCalc = false;
111,812✔
6740
  pOptions->forceOutput = false;
111,812✔
6741
  pOptions->ignoreDisorder = false;
111,812✔
6742
  pOptions->ignoreNoDataTrigger = false;
111,812✔
6743
  return (SNode*)pOptions;
111,812✔
6744
_err:
×
6745
  nodesDestroyNode((SNode*)pOptions);
×
6746
  return NULL;
×
6747
}
6748

6749
SNode* createStreamTagDefNode(SAstCreateContext* pCxt, SToken* pTagName, SDataType dataType, SNode* tagExpression) {
29,157✔
6750
  SStreamTagDefNode* pTagDef = NULL;
29,157✔
6751
  CHECK_PARSER_STATUS(pCxt);
29,157✔
6752
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TAG_DEF, (SNode**)&pTagDef);
29,157✔
6753
  CHECK_MAKE_NODE(pTagDef);
29,157✔
6754
  COPY_STRING_FORM_ID_TOKEN(pTagDef->tagName, pTagName);
29,157✔
6755
  int32_t nameLen = strdequote(pTagDef->tagName);
29,157✔
6756
  pTagDef->tagName[nameLen] = '\0';
29,157✔
6757
  pTagDef->dataType = dataType;
29,157✔
6758
  pTagDef->pTagExpr = tagExpression;
29,157✔
6759
  return (SNode*)pTagDef;
29,157✔
6760
_err:
×
6761
  nodesDestroyNode(tagExpression);
×
6762
  nodesDestroyNode((SNode*)pTagDef);
×
6763
  return NULL;
×
6764
}
6765

6766
SNode* setStreamTriggerOptions(SAstCreateContext* pCxt, SNode* pOptions, SStreamTriggerOption* pOptionUnit) {
142,420✔
6767
  CHECK_PARSER_STATUS(pCxt);
142,420✔
6768
  SStreamTriggerOptions* pStreamOptions = (SStreamTriggerOptions*)pOptions;
142,420✔
6769
  switch (pOptionUnit->type) {
142,420✔
6770
    case STREAM_TRIGGER_OPTION_CALC_NOTIFY_ONLY:
1,702✔
6771
      if (pStreamOptions->calcNotifyOnly) {
1,702✔
6772
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6773
                                                "CALC_NOTIFY_ONLY specified multiple times");
6774
        goto _err;
×
6775
      }
6776
      pStreamOptions->calcNotifyOnly = true;
1,702✔
6777
      break;
1,702✔
6778
    case STREAM_TRIGGER_OPTION_DELETE_OUTPUT_TABLE:
246✔
6779
      if (pStreamOptions->deleteOutputTable) {
246✔
6780
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6781
                                                "DELETE_OUTPUT_TABLE specified multiple times");
6782
        goto _err;
×
6783
      }
6784
      pStreamOptions->deleteOutputTable = true;
246✔
6785
      break;
246✔
6786
    case STREAM_TRIGGER_OPTION_DELETE_RECALC:
9,207✔
6787
      if (pStreamOptions->deleteRecalc) {
9,207✔
6788
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6789
                                                "DELETE_RECALC specified multiple times");
6790
        goto _err;
×
6791
      }
6792
      pStreamOptions->deleteRecalc = true;
9,207✔
6793
      break;
9,207✔
6794
    case STREAM_TRIGGER_OPTION_EXPIRED_TIME:
6,094✔
6795
      if (pStreamOptions->pExpiredTime != NULL) {
6,094✔
6796
        pCxt->errCode =
×
6797
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EXPIRED_TIME specified multiple times");
×
6798
        goto _err;
×
6799
      }
6800
      pStreamOptions->pExpiredTime = pOptionUnit->pNode;
6,094✔
6801
      break;
6,094✔
6802
    case STREAM_TRIGGER_OPTION_FORCE_OUTPUT:
4,056✔
6803
      if (pStreamOptions->forceOutput) {
4,056✔
6804
        pCxt->errCode =
×
6805
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FORCE_OUTPUT specified multiple times");
×
6806
        goto _err;
×
6807
      }
6808
      pStreamOptions->forceOutput = true;
4,056✔
6809
      break;
4,056✔
6810
    case STREAM_TRIGGER_OPTION_FILL_HISTORY:
35,751✔
6811
      if (pStreamOptions->fillHistoryFirst) {
35,751✔
6812
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6813
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
6814
        goto _err;
×
6815
      }
6816
      if (pStreamOptions->pFillHisStartTime != NULL) {
35,751✔
6817
        pCxt->errCode =
×
6818
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FILL_HISTORY specified multiple times");
×
6819
        goto _err;
×
6820
      }
6821
      pStreamOptions->fillHistory = true;
35,751✔
6822
      if (pOptionUnit->pNode == NULL) {
35,751✔
6823
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
18,355✔
6824
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
18,355✔
6825
      } else {
6826
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
17,396✔
6827
      }
6828
      break;
35,751✔
6829
    case STREAM_TRIGGER_OPTION_FILL_HISTORY_FIRST:
2,775✔
6830
      if (pStreamOptions->fillHistory) {
2,775✔
6831
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
430✔
6832
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
6833
        goto _err;
430✔
6834
      }
6835
      if (pStreamOptions->pFillHisStartTime != NULL) {
2,345✔
6836
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6837
                                                "FILL_HISTORY_FIRST specified multiple times");
6838
        goto _err;
×
6839
      }
6840
      pStreamOptions->fillHistoryFirst = true;
2,345✔
6841
      if (pOptionUnit->pNode == NULL) {
2,345✔
6842
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
430✔
6843
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
430✔
6844
      } else {
6845
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
1,915✔
6846
      }
6847
      break;
2,345✔
6848
    case STREAM_TRIGGER_OPTION_IGNORE_DISORDER:
19,422✔
6849
      if (pStreamOptions->ignoreDisorder) {
19,422✔
6850
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6851
                                                "IGNORE_DISORDER specified multiple times");
6852
        goto _err;
×
6853
      }
6854
      pStreamOptions->ignoreDisorder = true;
19,422✔
6855
      break;
19,422✔
6856
    case STREAM_TRIGGER_OPTION_LOW_LATENCY_CALC:
6,463✔
6857
      if (pStreamOptions->lowLatencyCalc) {
6,463✔
6858
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6859
                                                "LOW_LATENCY_CALC specified multiple times");
6860
        goto _err;
×
6861
      }
6862
      pStreamOptions->lowLatencyCalc = true;
6,463✔
6863
      break;
6,463✔
6864
    case STREAM_TRIGGER_OPTION_MAX_DELAY:
6,266✔
6865
      if (pStreamOptions->pMaxDelay != NULL) {
6,266✔
6866
        pCxt->errCode =
×
6867
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "MAX_DELAY specified multiple times");
×
6868
        goto _err;
×
6869
      }
6870
      pStreamOptions->pMaxDelay = pOptionUnit->pNode;
6,266✔
6871
      break;
6,266✔
6872
    case STREAM_TRIGGER_OPTION_WATERMARK:
8,473✔
6873
      if (pStreamOptions->pWaterMark != NULL) {
8,473✔
6874
        pCxt->errCode =
×
6875
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "WATERMARK specified multiple times");
×
6876
        goto _err;
×
6877
      }
6878
      pStreamOptions->pWaterMark = pOptionUnit->pNode;
8,473✔
6879
      break;
8,473✔
6880
    case STREAM_TRIGGER_OPTION_PRE_FILTER:
16,848✔
6881
      if (pStreamOptions->pPreFilter != NULL) {
16,848✔
6882
        pCxt->errCode =
×
6883
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "PRE_FILTER specified multiple times");
×
6884
        goto _err;
×
6885
      }
6886
      pStreamOptions->pPreFilter = pOptionUnit->pNode;
16,848✔
6887
      break;
16,848✔
6888
    case STREAM_TRIGGER_OPTION_EVENT_TYPE:
6,520✔
6889
      if (pStreamOptions->pEventType != EVENT_NONE) {
6,520✔
6890
        pCxt->errCode =
×
6891
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EVENT_TYPE specified multiple times");
×
6892
        goto _err;
×
6893
      }
6894
      pStreamOptions->pEventType = pOptionUnit->flag;
6,520✔
6895
      break;
6,520✔
6896
    case STREAM_TRIGGER_OPTION_IGNORE_NODATA_TRIGGER:
18,597✔
6897
      if (pStreamOptions->ignoreNoDataTrigger) {
18,597✔
6898
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6899
                                                "IGNORE_NODATA_TRIGGER specified multiple times");
6900
        goto _err;
×
6901
      }
6902
      pStreamOptions->ignoreNoDataTrigger = true;
18,597✔
6903
      break;
18,597✔
6904
    default:
×
6905
      break;
×
6906
  }
6907
  return pOptions;
141,990✔
6908
_err:
430✔
6909
  nodesDestroyNode(pOptionUnit->pNode);
430✔
6910
  nodesDestroyNode(pOptions);
430✔
6911
  return NULL;
430✔
6912
}
6913

6914
static bool validateNotifyUrl(const char* url) {
61,062✔
6915
  const char* prefix[] = {"ws://", "wss://"};
61,062✔
6916
  const char* host = NULL;
61,062✔
6917

6918
  if (!url || *url == '\0') return false;
61,062✔
6919

6920
  for (int32_t i = 0; i < ARRAY_SIZE(prefix); ++i) {
61,210✔
6921
    if (taosStrncasecmp(url, prefix[i], strlen(prefix[i])) == 0) {
61,136✔
6922
      host = url + strlen(prefix[i]);
60,988✔
6923
      break;
60,988✔
6924
    }
6925
  }
6926

6927
  return (host != NULL) && (*host != '\0') && (*host != '/');
61,062✔
6928
}
6929

6930
SNode* createStreamNotifyOptions(SAstCreateContext* pCxt, SNodeList* pAddrUrls, int64_t eventType, SNode* pWhere,
61,062✔
6931
                                 int64_t notifyType) {
6932
  SNode* pNode = NULL;
61,062✔
6933
  CHECK_PARSER_STATUS(pCxt);
61,062✔
6934

6935
  if (LIST_LENGTH(pAddrUrls) == 0) {
61,062✔
6936
    pCxt->errCode =
×
6937
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "notification address cannot be empty");
×
6938
    goto _err;
×
6939
  }
6940

6941
  FOREACH(pNode, pAddrUrls) {
122,050✔
6942
    char* url = ((SValueNode*)pNode)->literal;
61,062✔
6943
    if (strlen(url) >= TSDB_STREAM_NOTIFY_URL_LEN) {
61,062✔
6944
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6945
                                              "notification address \"%s\" exceed maximum length %d", url,
6946
                                              TSDB_STREAM_NOTIFY_URL_LEN);
6947
      goto _err;
×
6948
    }
6949
    if (!validateNotifyUrl(url)) {
61,062✔
6950
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
74✔
6951
                                              "invalid notification address \"%s\"", url);
6952
      goto _err;
74✔
6953
    }
6954
  }
6955

6956
  SStreamNotifyOptions* pNotifyOptions = NULL;
60,988✔
6957
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_NOTIFY_OPTIONS, (SNode**)&pNotifyOptions);
60,988✔
6958
  CHECK_MAKE_NODE(pNotifyOptions);
60,988✔
6959
  pNotifyOptions->pAddrUrls = pAddrUrls;
60,988✔
6960
  pNotifyOptions->pWhere = pWhere;
60,988✔
6961
  pNotifyOptions->eventType = eventType;
60,988✔
6962
  pNotifyOptions->notifyType = notifyType;
60,988✔
6963
  return (SNode*)pNotifyOptions;
60,988✔
6964
_err:
74✔
6965
  nodesDestroyList(pAddrUrls);
74✔
6966
  return NULL;
74✔
6967
}
6968

6969
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pStream, SNode* pTrigger,
273,309✔
6970
                              SNode* pOutTable, SNode* pQuery) {
6971
  SCreateStreamStmt* pStmt = NULL;
273,309✔
6972
  CHECK_PARSER_STATUS(pCxt);
273,309✔
6973
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
272,943✔
6974
  CHECK_MAKE_NODE(pStmt);
272,943✔
6975

6976
  if (pOutTable && ((SStreamOutTableNode*)pOutTable)->pOutTable) {
272,943✔
6977
    tstrncpy(pStmt->targetDbName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.dbName,
269,766✔
6978
             TSDB_DB_NAME_LEN);
6979
    tstrncpy(pStmt->targetTabName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.tableName,
269,766✔
6980
             TSDB_TABLE_NAME_LEN);
6981
  }
6982

6983
  if (pStream) {
272,943✔
6984
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
272,943✔
6985
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
272,943✔
6986
  } else {
6987
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
6988
    goto _err;
×
6989
  }
6990
  nodesDestroyNode(pStream);
272,943✔
6991

6992
  pStmt->ignoreExists = ignoreExists;
272,943✔
6993
  pStmt->pTrigger = pTrigger;
272,943✔
6994
  pStmt->pQuery = pQuery;
272,943✔
6995
  pStmt->pTags = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pTags : NULL;
272,943✔
6996
  pStmt->pSubtable = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pSubtable : NULL;
272,943✔
6997
  pStmt->pCols = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pCols : NULL;
272,943✔
6998
  return (SNode*)pStmt;
272,943✔
6999
_err:
366✔
7000
  nodesDestroyNode(pOutTable);
366✔
7001
  nodesDestroyNode(pQuery);
366✔
7002
  nodesDestroyNode(pTrigger);
366✔
7003
  nodesDestroyNode(pQuery);
366✔
7004
  return NULL;
366✔
7005
}
7006

7007
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNodeList* pStreamList) {
8,324✔
7008
  CHECK_PARSER_STATUS(pCxt);
8,324✔
7009
  SDropStreamStmt* pStmt = NULL;
8,324✔
7010
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
8,324✔
7011
  CHECK_MAKE_NODE(pStmt);
8,324✔
7012

7013
  if (pStreamList) {
8,324✔
7014
    pStmt->pStreamList = pStreamList;
8,324✔
7015
  } else {
7016
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7017
    goto _err;
×
7018
  }
7019

7020
  pStmt->ignoreNotExists = ignoreNotExists;
8,324✔
7021
  return (SNode*)pStmt;
8,324✔
7022
_err:
×
7023
  nodesDestroyList(pStreamList);
×
7024
  return NULL;
×
7025
}
7026

7027
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pStream) {
2,297✔
7028
  CHECK_PARSER_STATUS(pCxt);
2,297✔
7029
  SPauseStreamStmt* pStmt = NULL;
2,297✔
7030
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
2,297✔
7031
  CHECK_MAKE_NODE(pStmt);
2,297✔
7032
  if (pStream) {
2,297✔
7033
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
2,297✔
7034
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
2,297✔
7035
  } else {
7036
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7037
    goto _err;
×
7038
  }
7039
  nodesDestroyNode(pStream);
2,297✔
7040
  pStmt->ignoreNotExists = ignoreNotExists;
2,297✔
7041
  return (SNode*)pStmt;
2,297✔
7042
_err:
×
7043
  return NULL;
×
7044
}
7045

7046
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated, SNode* pStream) {
2,297✔
7047
  CHECK_PARSER_STATUS(pCxt);
2,297✔
7048
  SResumeStreamStmt* pStmt = NULL;
2,297✔
7049
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
2,297✔
7050
  CHECK_MAKE_NODE(pStmt);
2,297✔
7051
  if (pStream) {
2,297✔
7052
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
2,297✔
7053
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
2,297✔
7054
  } else {
7055
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7056
    goto _err;
×
7057
  }
7058
  nodesDestroyNode(pStream);
2,297✔
7059
  pStmt->ignoreNotExists = ignoreNotExists;
2,297✔
7060
  pStmt->ignoreUntreated = ignoreUntreated;
2,297✔
7061
  return (SNode*)pStmt;
2,297✔
7062
_err:
×
7063
  return NULL;
×
7064
}
7065

7066
SNode* createRecalcStreamStmt(SAstCreateContext* pCxt, SNode* pStream, SNode* pRange) {
10,911✔
7067
  CHECK_PARSER_STATUS(pCxt);
10,911✔
7068
  SRecalcStreamStmt* pStmt = NULL;
10,911✔
7069
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RECALCULATE_STREAM_STMT, (SNode**)&pStmt);
10,911✔
7070
  CHECK_MAKE_NODE(pStmt);
10,911✔
7071
  if (pStream) {
10,911✔
7072
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
10,911✔
7073
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
10,911✔
7074
  } else {
7075
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7076
    goto _err;
×
7077
  }
7078
  pStmt->pRange = pRange;
10,911✔
7079
  return (SNode*)pStmt;
10,911✔
7080
_err:
×
7081
  return NULL;
×
7082
}
7083

7084
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
2,041✔
7085
  CHECK_PARSER_STATUS(pCxt);
2,041✔
7086
  SKillStmt* pStmt = NULL;
2,041✔
7087
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,041✔
7088
  CHECK_MAKE_NODE(pStmt);
2,041✔
7089
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
2,041✔
7090
  return (SNode*)pStmt;
2,041✔
7091
_err:
×
7092
  return NULL;
×
7093
}
7094

7095
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
47✔
7096
  CHECK_PARSER_STATUS(pCxt);
47✔
7097
  SKillQueryStmt* pStmt = NULL;
47✔
7098
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
47✔
7099
  CHECK_MAKE_NODE(pStmt);
47✔
7100
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
47✔
7101
  return (SNode*)pStmt;
47✔
7102
_err:
×
7103
  return NULL;
×
7104
}
7105

7106
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
11,600✔
7107
  CHECK_PARSER_STATUS(pCxt);
11,600✔
7108
  SBalanceVgroupStmt* pStmt = NULL;
11,600✔
7109
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
11,600✔
7110
  CHECK_MAKE_NODE(pStmt);
11,600✔
7111
  return (SNode*)pStmt;
11,600✔
7112
_err:
×
7113
  return NULL;
×
7114
}
7115

7116
SNode* createAssignLeaderStmt(SAstCreateContext* pCxt) {
17✔
7117
  CHECK_PARSER_STATUS(pCxt);
17✔
7118
  SAssignLeaderStmt* pStmt = NULL;
17✔
7119
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ASSIGN_LEADER_STMT, (SNode**)&pStmt);
17✔
7120
  CHECK_MAKE_NODE(pStmt);
17✔
7121
  return (SNode*)pStmt;
17✔
7122
_err:
×
7123
  return NULL;
×
7124
}
7125

7126
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
2,128✔
7127
  CHECK_PARSER_STATUS(pCxt);
2,128✔
7128
  SBalanceVgroupLeaderStmt* pStmt = NULL;
2,128✔
7129
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
2,128✔
7130
  CHECK_MAKE_NODE(pStmt);
2,128✔
7131
  if (NULL != pVgId && NULL != pVgId->z) {
2,128✔
7132
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
86✔
7133
  }
7134
  return (SNode*)pStmt;
2,128✔
7135
_err:
×
7136
  return NULL;
×
7137
}
7138

7139
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
106✔
7140
  CHECK_PARSER_STATUS(pCxt);
106✔
7141
  SBalanceVgroupLeaderStmt* pStmt = NULL;
106✔
7142
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
106✔
7143
  CHECK_MAKE_NODE(pStmt);
106✔
7144
  if (NULL != pDbName) {
106✔
7145
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
106✔
7146
  }
7147
  return (SNode*)pStmt;
106✔
7148
_err:
×
7149
  return NULL;
×
7150
}
7151

7152
SNode* createSetVgroupKeepVersionStmt(SAstCreateContext* pCxt, const SToken* pVgId, const SToken* pKeepVersion) {
1,085✔
7153
  CHECK_PARSER_STATUS(pCxt);
1,085✔
7154
  SSetVgroupKeepVersionStmt* pStmt = NULL;
1,085✔
7155
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_VGROUP_KEEP_VERSION_STMT, (SNode**)&pStmt);
1,085✔
7156
  CHECK_MAKE_NODE(pStmt);
1,085✔
7157
  if (NULL != pVgId && NULL != pVgId->z) {
1,085✔
7158
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
1,085✔
7159
  }
7160
  if (NULL != pKeepVersion && NULL != pKeepVersion->z) {
1,085✔
7161
    pStmt->keepVersion = taosStr2Int64(pKeepVersion->z, NULL, 10);
1,085✔
7162
  }
7163
  return (SNode*)pStmt;
1,085✔
7164
_err:
×
7165
  return NULL;
×
7166
}
7167

7168
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
×
7169
  CHECK_PARSER_STATUS(pCxt);
×
7170
  SMergeVgroupStmt* pStmt = NULL;
×
7171
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
×
7172
  CHECK_MAKE_NODE(pStmt);
×
7173
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
×
7174
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
×
7175
  return (SNode*)pStmt;
×
7176
_err:
×
7177
  return NULL;
×
7178
}
7179

7180
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
45,166✔
7181
  CHECK_PARSER_STATUS(pCxt);
45,166✔
7182
  SRedistributeVgroupStmt* pStmt = NULL;
45,166✔
7183
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
45,166✔
7184
  CHECK_MAKE_NODE(pStmt);
45,166✔
7185
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
45,166✔
7186
  pStmt->pDnodes = pDnodes;
45,166✔
7187
  return (SNode*)pStmt;
45,166✔
7188
_err:
×
7189
  nodesDestroyList(pDnodes);
×
7190
  return NULL;
×
7191
}
7192

7193
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, bool force) {
17,176✔
7194
  CHECK_PARSER_STATUS(pCxt);
17,176✔
7195
  SSplitVgroupStmt* pStmt = NULL;
17,176✔
7196
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
17,176✔
7197
  CHECK_MAKE_NODE(pStmt);
17,176✔
7198
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
17,176✔
7199
  pStmt->force = force;
17,176✔
7200
  return (SNode*)pStmt;
17,176✔
7201
_err:
×
7202
  return NULL;
×
7203
}
7204

7205
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
7206
  CHECK_PARSER_STATUS(pCxt);
×
7207
  SNode* pStmt = NULL;
×
7208
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
7209
  CHECK_MAKE_NODE(pStmt);
×
7210
  return pStmt;
×
7211
_err:
×
7212
  return NULL;
×
7213
}
7214

7215
SNode* createGrantStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
570,208✔
7216
                       SNode* pCond, int8_t optrType) {
7217
  CHECK_PARSER_STATUS(pCxt);
570,208✔
7218
  CHECK_NAME(checkRoleName(pCxt, pPrincipal, false));
570,208✔
7219
  SGrantStmt* pStmt = NULL;
570,208✔
7220
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
570,208✔
7221
  CHECK_MAKE_NODE(pStmt);
570,208✔
7222
  pStmt->optrType = optrType;
570,208✔
7223
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
570,208✔
7224
  switch (optrType) {
570,208✔
7225
    case TSDB_ALTER_ROLE_LOCK:
×
7226
      break;
×
7227
    case TSDB_ALTER_ROLE_PRIVILEGES: {
567,940✔
7228
      CHECK_NAME(checkObjName(pCxt, &pPrivLevel->first, false));
567,940✔
7229
      CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
567,940✔
7230
      pStmt->privileges = *(SPrivSetArgs*)resouces;
567,940✔
7231
      if (TK_NK_NIL != pPrivLevel->first.type) {
567,940✔
7232
        COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
566,951✔
7233
      }
7234
      if (TK_NK_NIL != pPrivLevel->second.type) {
567,940✔
7235
        COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
242,747✔
7236
      }
7237
      pStmt->privileges.objType = pPrivLevel->objType;
567,940✔
7238
      pStmt->pCond = pCond;
567,940✔
7239
      break;
567,940✔
7240
    }
7241
    case TSDB_ALTER_ROLE_ROLE: {
2,268✔
7242
      SToken* pRole = (SToken*)resouces;
2,268✔
7243
      CHECK_NAME(checkRoleName(pCxt, pRole, false));
2,268✔
7244
      COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
2,268✔
7245
      break;
2,268✔
7246
    }
7247
    default:
×
7248
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported grant type");
×
7249
      goto _err;
×
7250
  }
7251
  return (SNode*)pStmt;
570,208✔
7252
_err:
×
7253
  nodesDestroyNode(pCond);
×
7254
  return NULL;
×
7255
}
7256

7257
SNode* createRevokeStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
410,629✔
7258
                        SNode* pCond, int8_t optrType) {
7259
  CHECK_PARSER_STATUS(pCxt);
410,629✔
7260
  CHECK_NAME(checkUserName(pCxt, pPrincipal));
410,629✔
7261
  SRevokeStmt* pStmt = NULL;
410,629✔
7262
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
410,629✔
7263
  CHECK_MAKE_NODE(pStmt);
410,629✔
7264
  pStmt->optrType = optrType;
410,629✔
7265
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
410,629✔
7266
  if (optrType == TSDB_ALTER_ROLE_PRIVILEGES) {
410,629✔
7267
    CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
409,327✔
7268
    CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
409,327✔
7269
    pStmt->privileges = *(SPrivSetArgs*)resouces;
409,327✔
7270
    COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
409,327✔
7271
    if (TK_NK_NIL != pPrivLevel->second.type) {
409,327✔
7272
      COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
222,958✔
7273
    }
7274
    pStmt->privileges.objType = pPrivLevel->objType;
409,327✔
7275
    pStmt->pCond = pCond;
409,327✔
7276
  } else if (optrType == TSDB_ALTER_ROLE_ROLE) {
1,302✔
7277
    SToken* pRole = (SToken*)resouces;
1,302✔
7278
    CHECK_NAME(checkRoleName(pCxt, pRole, false));
1,302✔
7279
    COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
1,302✔
7280
  } else {
7281
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported revoke type");
×
7282
    goto _err;
×
7283
  }
7284

7285
  return (SNode*)pStmt;
410,629✔
7286
_err:
×
7287
  nodesDestroyNode(pCond);
×
7288
  return NULL;
×
7289
}
7290

7291
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
5,247,024✔
7292
  SFunctionNode* pFunc = NULL;
5,247,024✔
7293
  CHECK_PARSER_STATUS(pCxt);
5,247,024✔
7294
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
5,247,024✔
7295
  CHECK_MAKE_NODE(pFunc);
5,247,024✔
7296
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
5,247,024✔
7297
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
5,247,024✔
7298
  CHECK_MAKE_NODE(pCol);
5,247,024✔
7299
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
5,247,024✔
7300
  CHECK_PARSER_STATUS(pCxt);
5,247,024✔
7301
  return (SNode*)pFunc;
5,247,024✔
7302
_err:
×
7303
  nodesDestroyNode((SNode*)pFunc);
×
7304
  return NULL;
×
7305
}
7306

7307
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
1,749,008✔
7308
  SDeleteStmt* pStmt = NULL;
1,749,008✔
7309
  CHECK_PARSER_STATUS(pCxt);
1,749,008✔
7310
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
1,749,008✔
7311
  CHECK_MAKE_NODE(pStmt);
1,749,008✔
7312
  pStmt->pFromTable = pTable;
1,749,008✔
7313
  pStmt->pWhere = pWhere;
1,749,008✔
7314
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
1,749,008✔
7315
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
1,749,008✔
7316
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
1,749,008✔
7317
  CHECK_MAKE_NODE(pStmt->pCountFunc);
1,749,008✔
7318
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
1,749,008✔
7319
  CHECK_MAKE_NODE(pStmt->pLastFunc);
1,749,008✔
7320
  return (SNode*)pStmt;
1,749,008✔
7321
_err:
×
7322
  nodesDestroyNode((SNode*)pStmt);
×
7323
  nodesDestroyNode(pTable);
×
7324
  nodesDestroyNode(pWhere);
×
7325
  return NULL;
×
7326
}
7327

7328
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
272,802✔
7329
  CHECK_PARSER_STATUS(pCxt);
272,802✔
7330
  SInsertStmt* pStmt = NULL;
272,802✔
7331
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
272,802✔
7332
  CHECK_MAKE_NODE(pStmt);
272,802✔
7333
  pStmt->pTable = pTable;
272,802✔
7334
  pStmt->pCols = pCols;
272,802✔
7335
  pStmt->pQuery = pQuery;
272,802✔
7336
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
272,802✔
7337
    tstrncpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
272,802✔
7338
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
×
7339
    tstrncpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
×
7340
  }
7341
  return (SNode*)pStmt;
272,802✔
7342
_err:
×
7343
  nodesDestroyNode(pTable);
×
7344
  nodesDestroyNode(pQuery);
×
7345
  nodesDestroyList(pCols);
×
7346
  return NULL;
×
7347
}
7348

7349
SNode* createCreateRsmaStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* rsmaName, SNode* pRealTable,
110,409✔
7350
                            SNodeList* pFuncs, SNodeList* pIntervals) {
7351
  SCreateRsmaStmt* pStmt = NULL;
110,409✔
7352

7353
  CHECK_PARSER_STATUS(pCxt);
110,409✔
7354
  CHECK_NAME(checkRsmaName(pCxt, rsmaName));
110,409✔
7355
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_RSMA_STMT, (SNode**)&pStmt);
110,409✔
7356
  CHECK_MAKE_NODE(pStmt);
110,409✔
7357

7358
  pStmt->ignoreExists = ignoreExists;
110,409✔
7359
  pStmt->pFuncs = pFuncs;
110,409✔
7360
  pStmt->pIntervals = pIntervals;
110,409✔
7361
  COPY_STRING_FORM_ID_TOKEN(pStmt->rsmaName, rsmaName);
110,409✔
7362

7363
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
110,409✔
7364
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
110,409✔
7365
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
110,409✔
7366
  nodesDestroyNode(pRealTable);
110,409✔
7367

7368
  return (SNode*)pStmt;
110,409✔
7369
_err:
×
7370
  nodesDestroyNode((SNode*)pStmt);
×
7371
  nodesDestroyList(pFuncs);
×
7372
  nodesDestroyNode(pRealTable);
×
7373
  nodesDestroyList(pIntervals);
×
7374
  return NULL;
×
7375
}
7376

7377
SNode* createDropRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
2,223✔
7378
  CHECK_PARSER_STATUS(pCxt);
2,223✔
7379
  SDropRsmaStmt* pStmt = NULL;
2,223✔
7380
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_RSMA_STMT, (SNode**)&pStmt);
2,223✔
7381
  CHECK_MAKE_NODE(pStmt);
2,223✔
7382

7383
  pStmt->ignoreNotExists = ignoreNotExists;
2,223✔
7384
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
2,223✔
7385

7386
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
2,223✔
7387
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
2,223✔
7388

7389
  nodesDestroyNode(pRealTable);
2,223✔
7390
  return (SNode*)pStmt;
2,223✔
7391
_err:
×
7392
  nodesDestroyNode(pRealTable);
×
7393
  return NULL;
×
7394
}
7395

7396
SNode* createAlterRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRsma, int8_t alterType,
20,007✔
7397
                           void* alterInfo) {
7398
  CHECK_PARSER_STATUS(pCxt);
20,007✔
7399
  SAlterRsmaStmt* pStmt = NULL;
20,007✔
7400
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_RSMA_STMT, (SNode**)&pStmt);
20,007✔
7401
  CHECK_MAKE_NODE(pStmt);
20,007✔
7402
  pStmt->ignoreNotExists = ignoreNotExists;
20,007✔
7403
  SRealTableNode* pTableNode = (SRealTableNode*)pRsma;
20,007✔
7404

7405
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
20,007✔
7406
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
20,007✔
7407
  nodesDestroyNode(pRsma);
20,007✔
7408

7409
  pStmt->alterType = alterType;
20,007✔
7410
  switch (alterType) {
20,007✔
7411
    case TSDB_ALTER_RSMA_FUNCTION: {
20,007✔
7412
      pStmt->pFuncs = (SNodeList*)alterInfo;
20,007✔
7413
      break;
20,007✔
7414
    }
7415
    default:
×
7416
      break;
×
7417
  }
7418
  return (SNode*)pStmt;
20,007✔
7419
_err:
×
7420
  return NULL;
×
7421
}
7422

7423
SNode* createShowCreateRsmaStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
2,223✔
7424
  CHECK_PARSER_STATUS(pCxt);
2,223✔
7425
  SShowCreateRsmaStmt* pStmt = NULL;
2,223✔
7426
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,223✔
7427
  CHECK_MAKE_NODE(pStmt);
2,223✔
7428
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
2,223✔
7429
  tstrncpy(pStmt->rsmaName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->rsmaName));
2,223✔
7430
  nodesDestroyNode(pRealTable);
2,223✔
7431
  return (SNode*)pStmt;
2,223✔
7432
_err:
×
7433
  nodesDestroyNode(pRealTable);
×
7434
  return NULL;
×
7435
}
7436

7437
SNode* createRollupStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
9,633✔
7438
  CHECK_PARSER_STATUS(pCxt);
9,633✔
7439
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
9,633✔
7440
  SRollupDatabaseStmt* pStmt = NULL;
9,633✔
7441
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_DATABASE_STMT, (SNode**)&pStmt);
9,633✔
7442
  CHECK_MAKE_NODE(pStmt);
9,633✔
7443
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
9,633✔
7444
  pStmt->pStart = pStart;
9,633✔
7445
  pStmt->pEnd = pEnd;
9,633✔
7446
  return (SNode*)pStmt;
9,633✔
7447
_err:
×
7448
  nodesDestroyNode(pStart);
×
7449
  nodesDestroyNode(pEnd);
×
7450
  return NULL;
×
7451
}
7452

7453
SNode* createRollupVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
4,446✔
7454
                                SNode* pEnd) {
7455
  CHECK_PARSER_STATUS(pCxt);
4,446✔
7456
  if (NULL == pDbName) {
4,446✔
7457
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
7458
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
7459
    CHECK_PARSER_STATUS(pCxt);
×
7460
  }
7461
  SRollupVgroupsStmt* pStmt = NULL;
4,446✔
7462
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_VGROUPS_STMT, (SNode**)&pStmt);
4,446✔
7463
  CHECK_MAKE_NODE(pStmt);
4,446✔
7464
  pStmt->pDbName = pDbName;
4,446✔
7465
  pStmt->vgidList = vgidList;
4,446✔
7466
  pStmt->pStart = pStart;
4,446✔
7467
  pStmt->pEnd = pEnd;
4,446✔
7468
  return (SNode*)pStmt;
4,446✔
7469
_err:
×
7470
  nodesDestroyNode(pDbName);
×
7471
  nodesDestroyList(vgidList);
×
7472
  nodesDestroyNode(pStart);
×
7473
  nodesDestroyNode(pEnd);
×
7474
  return NULL;
×
7475
}
7476

7477
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
806✔
7478
                            SNode* pRealTable, SNode* pInterval) {
7479
  SCreateTSMAStmt* pStmt = NULL;
806✔
7480

7481
  CHECK_PARSER_STATUS(pCxt);
806✔
7482
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
806✔
7483
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
806✔
7484
  CHECK_MAKE_NODE(pStmt);
806✔
7485

7486
  pStmt->ignoreExists = ignoreExists;
806✔
7487
  if (!pOptions) {
806✔
7488
    // recursive tsma
7489
    pStmt->pOptions = NULL;
×
7490
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
×
7491
    CHECK_MAKE_NODE(pStmt->pOptions);
×
7492
    pStmt->pOptions->recursiveTsma = true;
×
7493
  } else {
7494
    pStmt->pOptions = (STSMAOptions*)pOptions;
806✔
7495
  }
7496
  pStmt->pOptions->pInterval = pInterval;
806✔
7497
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
806✔
7498

7499
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
806✔
7500
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
806✔
7501
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
806✔
7502
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
806✔
7503
  nodesDestroyNode(pRealTable);
806✔
7504

7505
  return (SNode*)pStmt;
806✔
7506
_err:
×
7507
  nodesDestroyNode((SNode*)pStmt);
×
7508
  nodesDestroyNode(pOptions);
×
7509
  nodesDestroyNode(pRealTable);
×
7510
  nodesDestroyNode(pInterval);
×
7511
  return NULL;
×
7512
}
7513

7514
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
806✔
7515
  CHECK_PARSER_STATUS(pCxt);
806✔
7516
  STSMAOptions* pOptions = NULL;
806✔
7517
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
806✔
7518
  CHECK_MAKE_NODE(pOptions);
806✔
7519
  pOptions->pFuncs = pFuncs;
806✔
7520
  return (SNode*)pOptions;
806✔
7521
_err:
×
7522
  nodesDestroyList(pFuncs);
×
7523
  return NULL;
×
7524
}
7525

7526
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
7527
  CHECK_PARSER_STATUS(pCxt);
×
7528
  STSMAOptions* pOptions = NULL;
×
7529
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
7530
  CHECK_MAKE_NODE(pOptions);
×
7531
  return (SNode*)pOptions;
×
7532
_err:
×
7533
  return NULL;
×
7534
}
7535

7536
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
×
7537
  CHECK_PARSER_STATUS(pCxt);
×
7538
  SDropTSMAStmt* pStmt = NULL;
×
7539
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
×
7540
  CHECK_MAKE_NODE(pStmt);
×
7541

7542
  pStmt->ignoreNotExists = ignoreNotExists;
×
7543
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
×
7544

7545
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
×
7546
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
×
7547

7548
  nodesDestroyNode(pRealTable);
×
7549
  return (SNode*)pStmt;
×
7550
_err:
×
7551
  nodesDestroyNode(pRealTable);
×
7552
  return NULL;
×
7553
}
7554

7555
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
×
7556
  CHECK_PARSER_STATUS(pCxt);
×
7557

7558
  SShowStmt* pStmt = NULL;
×
7559
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
×
7560
  CHECK_MAKE_NODE(pStmt);
×
7561

7562
  pStmt->pDbName = dbName;
×
7563
  return (SNode*)pStmt;
×
7564
_err:
×
7565
  nodesDestroyNode(dbName);
×
7566
  return NULL;
×
7567
}
7568
SNode* createShowDiskUsageStmt(SAstCreateContext* pCxt, SNode* dbName, ENodeType type) {
315✔
7569
  CHECK_PARSER_STATUS(pCxt);
315✔
7570
  if (NULL == dbName) {
315✔
7571
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
7572
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
7573
    CHECK_PARSER_STATUS(pCxt);
×
7574
  }
7575

7576
  SShowStmt* pStmt = NULL;
315✔
7577
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
315✔
7578
  CHECK_MAKE_NODE(pStmt);
315✔
7579

7580
  pStmt->pDbName = dbName;
315✔
7581
  return (SNode*)pStmt;
315✔
7582
_err:
×
7583
  nodesDestroyNode(dbName);
×
7584
  return NULL;
×
7585
}
7586

7587
SNode* createShowStreamsStmt(SAstCreateContext* pCxt, SNode* pDbName, ENodeType type) {
104,609✔
7588
  CHECK_PARSER_STATUS(pCxt);
104,609✔
7589

7590
  if (needDbShowStmt(type) && NULL == pDbName) {
104,609✔
7591
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
213✔
7592
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
213✔
7593
    CHECK_PARSER_STATUS(pCxt);
213✔
7594
  }
7595

7596
  SShowStmt* pStmt = NULL;
104,396✔
7597
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
104,396✔
7598
  CHECK_MAKE_NODE(pStmt);
104,396✔
7599
  pStmt->withFull = false;
104,396✔
7600
  pStmt->pDbName = pDbName;
104,396✔
7601

7602
  return (SNode*)pStmt;
104,396✔
7603

7604
_err:
213✔
7605
  nodesDestroyNode(pDbName);
213✔
7606
  return NULL;
213✔
7607
}
7608

7609
SNode* createScanStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
118✔
7610
  CHECK_PARSER_STATUS(pCxt);
118✔
7611
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
118✔
7612
  SScanDatabaseStmt* pStmt = NULL;
118✔
7613
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_DATABASE_STMT, (SNode**)&pStmt);
118✔
7614
  CHECK_MAKE_NODE(pStmt);
118✔
7615
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
118✔
7616
  pStmt->pStart = pStart;
118✔
7617
  pStmt->pEnd = pEnd;
118✔
7618
  return (SNode*)pStmt;
118✔
7619
_err:
×
7620
  nodesDestroyNode(pStart);
×
7621
  nodesDestroyNode(pEnd);
×
7622
  return NULL;
×
7623
}
7624

7625
SNode* createScanVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart, SNode* pEnd) {
354✔
7626
  CHECK_PARSER_STATUS(pCxt);
354✔
7627
  if (NULL == pDbName) {
354✔
7628
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
59✔
7629
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
59✔
7630
    CHECK_PARSER_STATUS(pCxt);
59✔
7631
  }
7632
  SScanVgroupsStmt* pStmt = NULL;
295✔
7633
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_VGROUPS_STMT, (SNode**)&pStmt);
295✔
7634
  CHECK_MAKE_NODE(pStmt);
295✔
7635
  pStmt->pDbName = pDbName;
295✔
7636
  pStmt->vgidList = vgidList;
295✔
7637
  pStmt->pStart = pStart;
295✔
7638
  pStmt->pEnd = pEnd;
295✔
7639
  return (SNode*)pStmt;
295✔
7640
_err:
59✔
7641
  nodesDestroyNode(pDbName);
59✔
7642
  nodesDestroyList(vgidList);
59✔
7643
  nodesDestroyNode(pStart);
59✔
7644
  nodesDestroyNode(pEnd);
59✔
7645
  return NULL;
59✔
7646
}
7647

7648
SNode* createShowScansStmt(SAstCreateContext* pCxt, ENodeType type) {
2,596✔
7649
  CHECK_PARSER_STATUS(pCxt);
2,596✔
7650
  SShowScansStmt* pStmt = NULL;
2,596✔
7651
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,596✔
7652
  CHECK_MAKE_NODE(pStmt);
2,596✔
7653
  return (SNode*)pStmt;
2,596✔
7654
_err:
×
7655
  return NULL;
×
7656
}
7657

7658
SNode* createShowScanDetailsStmt(SAstCreateContext* pCxt, SNode* pScanIdNode) {
2,006✔
7659
  CHECK_PARSER_STATUS(pCxt);
2,006✔
7660
  SShowScanDetailsStmt* pStmt = NULL;
2,006✔
7661
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_SCAN_DETAILS_STMT, (SNode**)&pStmt);
2,006✔
7662
  CHECK_MAKE_NODE(pStmt);
2,006✔
7663
  pStmt->pScanId = pScanIdNode;
2,006✔
7664
  return (SNode*)pStmt;
2,006✔
7665
_err:
×
7666
  nodesDestroyNode(pScanIdNode);
×
7667
  return NULL;
×
7668
}
7669

7670
SNode* createAlterAllDnodeTLSStmt(SAstCreateContext* pCxt, SToken* alterName) {
×
7671
  CHECK_PARSER_STATUS(pCxt);
×
7672
  SAlterDnodeStmt* pStmt = NULL;
×
7673
  static char*     tls = "TLS";
7674
  if (NULL == alterName || alterName->n <= 0) {
×
7675
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter name is empty");
×
7676
    goto _err;
×
7677
  }
7678

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

7682
    memcpy(pStmt->config, "reload", strlen("reload"));
×
7683
    memcpy(pStmt->value, "tls", strlen("tls"));
×
7684
    pStmt->dnodeId = -1;
×
7685
  } else {
7686
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter is not supported");
×
7687
    goto _err;
×
7688
  }
7689

7690
  CHECK_MAKE_NODE(pStmt);
×
7691

7692
  return (SNode*)pStmt;
×
7693
_err:
×
7694
  return NULL;
×
7695
}
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