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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

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

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

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

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

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

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

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

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

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

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

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

100
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
473,232,014✔
101
  memset(pCxt, 0, sizeof(SAstCreateContext));
473,232,014✔
102
  pCxt->pQueryCxt = pParseCxt;
473,232,014✔
103
  pCxt->msgBuf.buf = pParseCxt->pMsg;
473,229,836✔
104
  pCxt->msgBuf.len = pParseCxt->msgLen;
473,224,673✔
105
  pCxt->notSupport = false;
473,223,118✔
106
  pCxt->pRootNode = NULL;
473,228,727✔
107
  pCxt->placeholderNo = 0;
473,232,326✔
108
  pCxt->pPlaceholderValues = NULL;
473,230,249✔
109
  pCxt->errCode = TSDB_CODE_SUCCESS;
473,223,549✔
110
}
473,230,399✔
111

112
static void trimEscape(SAstCreateContext* pCxt, SToken* pName, bool trimStar) {
2,147,483,647✔
113
  // todo need to deal with `ioo``ii` -> ioo`ii: done
114
  if (NULL != pName && pName->n > 1 && TS_ESCAPE_CHAR == pName->z[0]) {
2,147,483,647✔
115
    if (!pCxt->pQueryCxt->hasDupQuoteChar) {
12,640,372✔
116
      pName->z += 1;
12,531,151✔
117
      pName->n -= 2;
12,531,228✔
118
      // * is forbidden as an identifier name
119
      if (pName->z[0] == '*' && trimStar && pName->n == 1) {
12,531,707✔
120
        pName->z[0] = '\0';
×
121
        pName->n = 0;
×
122
      }
123
    } else {
124
      int32_t i = 1, j = 0;
109,143✔
125
      for (; i < pName->n - 1; ++i) {
667,890✔
126
        if ((pName->z[i] == TS_ESCAPE_CHAR) && (pName->z[i + 1] == TS_ESCAPE_CHAR)) {
558,747✔
127
          pName->z[j++] = TS_ESCAPE_CHAR;
263,355✔
128
          ++i;
263,355✔
129
        } else {
130
          pName->z[j++] = pName->z[i];
295,392✔
131
        }
132
      }
133
      pName->n = j;
109,143✔
134
    }
135
  }
136
}
2,147,483,647✔
137

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

152

153

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

164

165

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

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

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

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

204
  if (NULL != pPortToken) {
177,200✔
205
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
169,135✔
206
  }
207

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

212
  return TSDB_CODE_SUCCESS == pCxt->errCode;
177,200✔
213
}
214

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

229
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
971,400,121✔
230
  if (NULL == pDbName) {
971,400,121✔
231
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
686,137,818✔
232
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
6,404✔
233
    }
234
  } else {
235
    trimEscape(pCxt, pDbName, true);
285,262,303✔
236
    if (pDbName->n >= TSDB_DB_NAME_LEN || (demandDb && (pDbName->n == 0))) {
285,286,526✔
237
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
15,907✔
238
    }
239
  }
240
  return TSDB_CODE_SUCCESS == pCxt->errCode;
971,413,480✔
241
}
242

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

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

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

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

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

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

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

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

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

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

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

350
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
1,821,889,817✔
351
  CHECK_PARSER_STATUS(pCxt);
1,821,889,817✔
352
  SRawExprNode* target = NULL;
1,821,882,661✔
353
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
1,821,883,926✔
354
  CHECK_MAKE_NODE(target);
1,821,939,866✔
355
  target->p = pToken->z;
1,821,939,866✔
356
  target->n = pToken->n;
1,821,935,744✔
357
  target->pNode = pNode;
1,821,939,941✔
358
  return (SNode*)target;
1,821,937,412✔
359
_err:
6,944✔
360
  nodesDestroyNode(pNode);
6,944✔
361
  return NULL;
6,944✔
362
}
363

364
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
1,957,996,636✔
365
  CHECK_PARSER_STATUS(pCxt);
1,957,996,636✔
366
  SRawExprNode* target = NULL;
1,957,995,978✔
367
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
1,957,996,743✔
368
  CHECK_MAKE_NODE(target);
1,958,087,068✔
369
  target->p = pStart->z;
1,958,087,068✔
370
  target->n = (pEnd->z + pEnd->n) - pStart->z;
1,958,086,026✔
371
  target->pNode = pNode;
1,958,085,301✔
372
  return (SNode*)target;
1,958,085,990✔
373
_err:
×
374
  nodesDestroyNode(pNode);
×
375
  return NULL;
×
376
}
377

378
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
100,413,759✔
379
  CHECK_PARSER_STATUS(pCxt);
100,413,759✔
380
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
100,413,796✔
381
    return pNode;
×
382
  }
383
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
100,414,799✔
384
  return pNode;
100,412,796✔
385
_err:
×
386
  nodesDestroyNode(pNode);
×
387
  return NULL;
×
388
}
389

390
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
2,147,483,647✔
391
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
392
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,147,483,647✔
393
  SNode*        pRealizedExpr = pRawExpr->pNode;
2,147,483,647✔
394
  if (nodesIsExprNode(pRealizedExpr)) {
2,147,483,647✔
395
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
2,147,483,647✔
396
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
2,147,483,647✔
397
      tstrncpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
1,420,865,002✔
398
      tstrncpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
1,420,865,732✔
399
    } else if (pRawExpr->isPseudoColumn) {
2,147,483,647✔
400
      // all pseudo column are translate to function with same name
401
      tstrncpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
88,109,076✔
402
      if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_column") == 0) {
88,108,216✔
403
        SValueNode* pColId = (SValueNode*)nodesListGetNode(((SFunctionNode*)pExpr)->pParameterList, 0);
55,356✔
404
        snprintf(pExpr->userAlias, sizeof(pExpr->userAlias), "%%%%%s", pColId->literal);
55,356✔
405
      } else if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_tbname") == 0) {
88,051,834✔
406
        tstrncpy(pExpr->userAlias, "%%tbname", TSDB_COL_NAME_LEN);
46,429✔
407
      } else {
408
        tstrncpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
88,003,448✔
409
      }
410
    } else {
411
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
2,147,483,647✔
412

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

430
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
1,247,250,621✔
431
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
1,247,250,621✔
432
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
570✔
433
    return nil_token;
×
434
  }
435
  SRawExprNode* target = (SRawExprNode*)pNode;
1,247,282,229✔
436
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
1,247,282,229✔
437
  return t;
1,247,283,367✔
438
}
439

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

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

467
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
1,576,179,734✔
468
  CHECK_PARSER_STATUS(pCxt);
1,576,179,734✔
469
  SNodeList* list = NULL;
1,576,166,702✔
470
  pCxt->errCode = nodesMakeList(&list);
1,576,166,741✔
471
  CHECK_MAKE_NODE(list);
1,576,238,782✔
472
  pCxt->errCode = nodesListAppend(list, pNode);
1,576,238,782✔
473
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
1,576,219,017✔
UNCOV
474
    nodesDestroyList(list);
×
475
    return NULL;
×
476
  }
477
  return list;
1,576,233,844✔
478
_err:
9,276✔
479
  nodesDestroyNode(pNode);
9,276✔
480
  return NULL;
9,276✔
481
}
482

483
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
1,168,292,830✔
484
  CHECK_PARSER_STATUS(pCxt);
1,168,292,830✔
485
  pCxt->errCode = nodesListAppend(pList, pNode);
1,168,295,783✔
486
  return pList;
1,168,318,243✔
487
_err:
1,850✔
488
  nodesDestroyNode(pNode);
1,850✔
489
  nodesDestroyList(pList);
1,850✔
490
  return NULL;
1,850✔
491
}
492

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

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

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

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

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

693
_err:
×
694
  return args;
×
695
}
696

697
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
1,533,993,252✔
698
  CHECK_PARSER_STATUS(pCxt);
1,533,993,252✔
699
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
1,533,993,064✔
700
    return NULL;
404✔
701
  }
702
  SColumnNode* col = NULL;
1,534,005,776✔
703
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
1,534,005,888✔
704
  CHECK_MAKE_NODE(col);
1,534,029,285✔
705
  if (NULL != pTableAlias) {
1,534,029,285✔
706
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
269,573,680✔
707
  }
708
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
1,534,029,285✔
709
  return (SNode*)col;
1,534,031,350✔
710
_err:
×
711
  return NULL;
×
712
}
713

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

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

741
static void copyValueTrimEscape(char* buf, int32_t bufLen, const SToken* pToken, bool trim) {
544,594,856✔
742
  int32_t len = TMIN(pToken->n, bufLen - 1);
544,594,856✔
743
  if (trim && (pToken->z[0] == TS_ESCAPE_CHAR)) {
544,594,942✔
744
    int32_t i = 1, j = 0;
29,865✔
745
    for (; i < len - 1; ++i) {
168,330✔
746
      buf[j++] = pToken->z[i];
138,465✔
747
      if (pToken->z[i] == TS_ESCAPE_CHAR) {
138,465✔
748
        if (pToken->z[i + 1] == TS_ESCAPE_CHAR) ++i;
70,047✔
749
      }
750
    }
751
    buf[j] = 0;
29,865✔
752
  } else {
753
    tstrncpy(buf, pToken->z, len + 1);
544,565,077✔
754
  }
755
}
544,595,466✔
756

757
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
544,583,487✔
758
  CHECK_PARSER_STATUS(pCxt);
544,583,487✔
759
  SValueNode* val = NULL;
544,583,560✔
760
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
544,582,998✔
761
  CHECK_MAKE_NODE(val);
544,600,768✔
762
  if (!(val->literal = taosMemoryMalloc(pLiteral->n + 1))) {
544,600,768✔
763
    pCxt->errCode = terrno;
×
764
    nodesDestroyNode((SNode*)val);
×
765
    return NULL;
7✔
766
  }
767
  copyValueTrimEscape(val->literal, pLiteral->n + 1, pLiteral,
544,594,711✔
768
                      pCxt->pQueryCxt->hasDupQuoteChar && (TK_NK_ID == pLiteral->type));
544,594,244✔
769
  if (TK_NK_STRING == pLiteral->type) {
544,597,085✔
770
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
65,810,848✔
771
  }
772
  val->node.resType.type = dataType;
544,595,881✔
773
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
544,595,375✔
774
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
544,591,467✔
775
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
18,932✔
776
  }
777
  val->translate = false;
544,591,467✔
778
  val->tz = pCxt->pQueryCxt->timezone;
544,595,341✔
779
  val->charsetCxt = pCxt->pQueryCxt->charsetCxt;
544,593,436✔
780
  return (SNode*)val;
544,595,911✔
781
_err:
×
782
  return NULL;
×
783
}
784

785
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
147,918,922✔
786
  CHECK_PARSER_STATUS(pCxt);
147,918,922✔
787
  SValueNode* val = NULL;
147,916,700✔
788
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
147,917,070✔
789
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
147,934,461✔
790
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
791
    goto _exit;
×
792
  }
793
  if (pLiteral) {
147,925,249✔
794
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
147,333,162✔
795
    if (!val->literal) {
147,334,620✔
796
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
797
      goto _exit;
×
798
    }
799
  } else if (pNode) {
592,087✔
800
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
592,087✔
801
    if (!nodesIsExprNode(pRawExpr->pNode)) {
592,087✔
802
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
803
      goto _exit;
×
804
    }
805
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
592,087✔
806
    if (!val->literal) {
592,074✔
807
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
808
      goto _exit;
×
809
    }
810
  } else {
811
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
812
    goto _exit;
×
813
  }
814
  if (!val->literal) {
147,922,009✔
815
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
816
    goto _exit;
×
817
  }
818

819
  val->node.resType.type = dataType;
147,913,598✔
820
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
147,922,987✔
821
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
147,921,896✔
822
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
823
  }
824
_exit:
147,921,896✔
825
  nodesDestroyNode(pNode);
147,922,269✔
826
  if (pCxt->errCode != 0) {
147,904,028✔
827
    nodesDestroyNode((SNode*)val);
×
828
    return NULL;
×
829
  }
830
  return (SNode*)val;
147,918,519✔
831
_err:
×
832
  nodesDestroyNode(pNode);
×
833
  return NULL;
×
834
}
835

836
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
51,700✔
837
                             SNode* pRight) {
838
  SValueNode* val = NULL;
51,700✔
839
  CHECK_PARSER_STATUS(pCxt);
51,700✔
840

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

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

873
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
8,902,370✔
874
  if (!pHintList) return false;
8,902,370✔
875
  SNode* pNode;
876
  FOREACH(pNode, pHintList) {
1,434✔
877
    SHintNode* pHint = (SHintNode*)pNode;
1,434✔
878
    if (pHint->option == hint) {
1,434✔
879
      return true;
1,434✔
880
    }
881
  }
882
  return false;
×
883
}
884

885
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
8,906,510✔
886
                       int32_t paramNum) {
887
  void* value = NULL;
8,906,510✔
888
  switch (opt) {
8,906,510✔
889
    case HINT_SKIP_TSMA:
4,140✔
890
    case HINT_BATCH_SCAN:
891
    case HINT_NO_BATCH_SCAN: {
892
      if (paramNum > 0) {
4,140✔
893
        return true;
802✔
894
      }
895
      break;
3,338✔
896
    }
897
    case HINT_SORT_FOR_GROUP:
58,316✔
898
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
58,316✔
899
      break;
57,838✔
900
    case HINT_PARTITION_FIRST:
1,912✔
901
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
1,912✔
902
      break;
956✔
903
    case HINT_PARA_TABLES_SORT:
8,841,328✔
904
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
8,841,328✔
905
      break;
8,841,328✔
906
    case HINT_SMALLDATA_TS_SORT:
×
907
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
908
      break;
×
909
    case HINT_HASH_JOIN:
814✔
910
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
814✔
911
      break;
814✔
912
    case HINT_WIN_OPTIMIZE_BATCH:
×
913
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_BATCH)) return true;
×
914
      break;
×
915
    case HINT_WIN_OPTIMIZE_SINGLE:
×
916
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_SINGLE)) return true;
×
917
      break;
×
918
    default:
×
919
      return true;
×
920
  }
921

922
  SHintNode* hint = NULL;
8,904,274✔
923
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
8,904,274✔
924
  if (!hint) {
8,904,274✔
925
    return true;
×
926
  }
927
  hint->option = opt;
8,904,274✔
928
  hint->value = value;
8,904,274✔
929

930
  if (NULL == *ppHintList) {
8,904,274✔
931
    pCxt->errCode = nodesMakeList(ppHintList);
8,903,472✔
932
    if (!*ppHintList) {
8,903,472✔
933
      nodesDestroyNode((SNode*)hint);
×
934
      return true;
×
935
    }
936
  }
937

938
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
8,904,274✔
939
  if (pCxt->errCode) {
8,904,274✔
940
    return true;
×
941
  }
942

943
  return false;
8,904,274✔
944
}
945

946
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
766,393,687✔
947
  CHECK_PARSER_STATUS(pCxt);
766,393,687✔
948
  if (NULL == pLiteral || pLiteral->n <= 5) {
766,394,066✔
949
    return NULL;
757,490,836✔
950
  }
951
  SNodeList* pHintList = NULL;
8,903,294✔
952
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
8,904,675✔
953
  if (!hint) return NULL;
8,904,675✔
954
  int32_t     i = 0;
8,904,675✔
955
  bool        quit = false;
8,904,675✔
956
  bool        inParamList = false;
8,904,675✔
957
  bool        lastComma = false;
8,904,675✔
958
  EHintOption opt = 0;
8,904,675✔
959
  int32_t     paramNum = 0;
8,904,675✔
960
  SToken      paramList[10];
8,904,675✔
961
  while (!quit) {
53,434,150✔
962
    SToken t0 = {0};
53,431,513✔
963
    if (hint[i] == 0) {
53,431,513✔
964
      break;
8,902,038✔
965
    }
966
    t0.n = tGetToken(&hint[i], &t0.type, NULL);
44,529,475✔
967
    t0.z = hint + i;
44,529,475✔
968
    i += t0.n;
44,529,475✔
969

970
    switch (t0.type) {
44,529,475✔
971
      case TK_BATCH_SCAN:
2,406✔
972
        lastComma = false;
2,406✔
973
        if (0 != opt || inParamList) {
2,406✔
974
          quit = true;
×
975
          break;
×
976
        }
977
        opt = HINT_BATCH_SCAN;
2,406✔
978
        break;
2,406✔
979
      case TK_NO_BATCH_SCAN:
1,604✔
980
        lastComma = false;
1,604✔
981
        if (0 != opt || inParamList) {
1,604✔
982
          quit = true;
×
983
          break;
×
984
        }
985
        opt = HINT_NO_BATCH_SCAN;
1,604✔
986
        break;
1,604✔
987
      case TK_SORT_FOR_GROUP:
58,316✔
988
        lastComma = false;
58,316✔
989
        if (0 != opt || inParamList) {
58,316✔
990
          quit = true;
×
991
          break;
×
992
        }
993
        opt = HINT_SORT_FOR_GROUP;
58,316✔
994
        break;
58,316✔
995
      case TK_PARTITION_FIRST:
1,912✔
996
        lastComma = false;
1,912✔
997
        if (0 != opt || inParamList) {
1,912✔
998
          quit = true;
×
999
          break;
×
1000
        }
1001
        opt = HINT_PARTITION_FIRST;
1,912✔
1002
        break;
1,912✔
1003
      case TK_PARA_TABLES_SORT:
8,841,328✔
1004
        lastComma = false;
8,841,328✔
1005
        if (0 != opt || inParamList) {
8,841,328✔
1006
          quit = true;
×
1007
          break;
×
1008
        }
1009
        opt = HINT_PARA_TABLES_SORT;
8,841,328✔
1010
        break;
8,841,328✔
1011
      case TK_SMALLDATA_TS_SORT:
×
1012
        lastComma = false;
×
1013
        if (0 != opt || inParamList) {
×
1014
          quit = true;
×
1015
          break;
×
1016
        }
1017
        opt = HINT_SMALLDATA_TS_SORT;
×
1018
        break;
×
1019
      case TK_HASH_JOIN:
814✔
1020
        lastComma = false;
814✔
1021
        if (0 != opt || inParamList) {
814✔
1022
          quit = true;
×
1023
          break;
×
1024
        }
1025
        opt = HINT_HASH_JOIN;
814✔
1026
        break;
814✔
1027
      case TK_SKIP_TSMA:
130✔
1028
        lastComma = false;
130✔
1029
        if (0 != opt || inParamList) {
130✔
1030
          quit = true;
×
1031
          break;
×
1032
        }
1033
        opt = HINT_SKIP_TSMA;
130✔
1034
        break;
130✔
1035
      case TK_WIN_OPTIMIZE_BATCH:
×
1036
        lastComma = false;
×
1037
        if (0 != opt || inParamList) {
×
1038
          quit = true;
×
1039
          break;
×
1040
        }
1041
        opt = HINT_WIN_OPTIMIZE_BATCH;
×
1042
        break;
×
1043
      case TK_WIN_OPTIMIZE_SINGLE:
×
1044
        lastComma = false;
×
1045
        if (0 != opt || inParamList) {
×
1046
          quit = true;
×
1047
          break;
×
1048
        }
1049
        opt = HINT_WIN_OPTIMIZE_SINGLE;
×
1050
        break;
×
1051
      case TK_NK_LP:
8,906,510✔
1052
        lastComma = false;
8,906,510✔
1053
        if (0 == opt || inParamList) {
8,906,510✔
1054
          quit = true;
×
1055
        }
1056
        inParamList = true;
8,906,510✔
1057
        break;
8,906,510✔
1058
      case TK_NK_RP:
8,906,510✔
1059
        lastComma = false;
8,906,510✔
1060
        if (0 == opt || !inParamList) {
8,906,510✔
1061
          quit = true;
×
1062
        } else {
1063
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
8,906,510✔
1064
          inParamList = false;
8,906,510✔
1065
          paramNum = 0;
8,906,510✔
1066
          opt = 0;
8,906,510✔
1067
        }
1068
        break;
8,906,510✔
1069
      case TK_NK_ID:
1,203✔
1070
        lastComma = false;
1,203✔
1071
        if (0 == opt || !inParamList) {
1,203✔
1072
          quit = true;
401✔
1073
        } else {
1074
          paramList[paramNum++] = t0;
802✔
1075
        }
1076
        break;
1,203✔
1077
      case TK_NK_COMMA:
802✔
1078
        if (lastComma) {
802✔
1079
          quit = true;
×
1080
        }
1081
        lastComma = true;
802✔
1082
        break;
802✔
1083
      case TK_NK_SPACE:
17,807,940✔
1084
        break;
17,807,940✔
1085
      default:
×
1086
        lastComma = false;
×
1087
        quit = true;
×
1088
        break;
×
1089
    }
1090
  }
1091

1092
  taosMemoryFree(hint);
8,904,675✔
1093
  return pHintList;
8,904,675✔
1094
_err:
×
1095
  return NULL;
×
1096
}
1097

1098
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
1,464,579✔
1099
  trimEscape(pCxt, pLiteral, false);
1,464,579✔
1100
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
1,464,579✔
1101
}
1102

1103
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
81,119,010✔
1104
  CHECK_PARSER_STATUS(pCxt);
81,119,010✔
1105
  SValueNode* val = NULL;
81,118,067✔
1106
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
81,117,637✔
1107
  CHECK_MAKE_NODE(val);
81,126,404✔
1108
  if (pLiteral->type == TK_NK_STRING) {
81,126,404✔
1109
    // like '100s' or "100d"
1110
    // check format: ^[0-9]+[smwbauhdny]$'
1111
    if (pLiteral->n < 4) {
18,080✔
1112
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
768✔
1113
      return NULL;
768✔
1114
    }
1115
    char unit = pLiteral->z[pLiteral->n - 2];
17,312✔
1116
    switch (unit) {
17,312✔
1117
      case 'a':
14,224✔
1118
      case 'b':
1119
      case 'd':
1120
      case 'h':
1121
      case 'm':
1122
      case 's':
1123
      case 'u':
1124
      case 'w':
1125
      case 'y':
1126
      case 'n':
1127
        break;
14,224✔
1128
      default:
3,088✔
1129
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
3,088✔
1130
        return NULL;
3,088✔
1131
    }
1132
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
50,720✔
1133
      if (!isdigit(pLiteral->z[i])) {
38,800✔
1134
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
2,304✔
1135
        return NULL;
2,304✔
1136
      }
1137
    }
1138
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
11,920✔
1139
  } else {
1140
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
81,104,917✔
1141
  }
1142
  if (!val->literal) {
81,119,760✔
1143
    nodesDestroyNode((SNode*)val);
×
1144
    pCxt->errCode = terrno;
×
UNCOV
1145
    return NULL;
×
1146
  }
1147
  val->flag |= VALUE_FLAG_IS_DURATION;
81,118,900✔
1148
  val->translate = false;
81,118,823✔
1149
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
81,117,809✔
1150
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
81,117,886✔
1151
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
81,118,900✔
1152
  return (SNode*)val;
81,118,470✔
1153
_err:
×
1154
  return NULL;
×
1155
}
1156

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

1209
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
960,929✔
1210
  CHECK_PARSER_STATUS(pCxt);
960,929✔
1211
  if (NULL == pCxt->pQueryCxt->db) {
960,929✔
1212
    return NULL;
4,760✔
1213
  }
1214

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

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

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

1279
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
204,553,184✔
1280
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
204,553,184✔
1281
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
28,544,694✔
1282
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
28,512,714✔
1283
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
28,512,714✔
1284
    nodesDestroyNode(pParam);
28,512,714✔
1285
    return code;
28,512,714✔
1286
  } else {
1287
    return nodesListAppend(pCond->pParameterList, pParam);
176,040,040✔
1288
  }
1289
}
1290

1291
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
102,453,072✔
1292
  CHECK_PARSER_STATUS(pCxt);
102,453,072✔
1293
  SLogicConditionNode* cond = NULL;
102,451,699✔
1294
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
102,451,736✔
1295
  CHECK_MAKE_NODE(cond);
102,454,775✔
1296
  cond->condType = type;
102,454,775✔
1297
  cond->pParameterList = NULL;
102,455,245✔
1298
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
102,451,067✔
1299
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
102,453,017✔
1300
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
102,452,058✔
1301
  }
1302
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
102,455,635✔
1303
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
102,102,623✔
1304
  }
1305
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
102,456,468✔
1306
    nodesDestroyNode((SNode*)cond);
×
1307
    return NULL;
×
1308
  }
1309
  return (SNode*)cond;
102,455,492✔
1310
_err:
×
1311
  nodesDestroyNode(pParam1);
×
1312
  nodesDestroyNode(pParam2);
×
UNCOV
1313
  return NULL;
×
1314
}
1315

1316
static uint8_t getMinusDataType(uint8_t orgType) {
30,408,585✔
1317
  switch (orgType) {
30,408,585✔
1318
    case TSDB_DATA_TYPE_UTINYINT:
22,203,577✔
1319
    case TSDB_DATA_TYPE_USMALLINT:
1320
    case TSDB_DATA_TYPE_UINT:
1321
    case TSDB_DATA_TYPE_UBIGINT:
1322
      return TSDB_DATA_TYPE_BIGINT;
22,203,577✔
1323
    default:
8,205,094✔
1324
      break;
8,205,094✔
1325
  }
1326
  return orgType;
8,205,094✔
1327
}
1328

1329
SNode* setNodeQuantifyType(SAstCreateContext* pCxt, SNode* pNode, EQuantifyType type) {
59,440,302✔
1330
  CHECK_PARSER_STATUS(pCxt);
59,440,302✔
1331

1332
  switch (nodeType(pNode)) {
59,440,302✔
1333
    case QUERY_NODE_SELECT_STMT: {
59,253,618✔
1334
      SSelectStmt* pSelect = (SSelectStmt*)pNode;
59,253,618✔
1335
      pSelect->quantify = type;
59,253,618✔
1336
      pSelect->subQType = E_SUB_QUERY_COLUMN;
59,253,618✔
1337
      break;
59,253,618✔
1338
    }  
1339
    case QUERY_NODE_SET_OPERATOR:{
187,416✔
1340
      SSetOperator* pSet = (SSetOperator*)pNode;
187,416✔
1341
      pSet->quantify = type;
187,416✔
1342
      pSet->subQType = E_SUB_QUERY_COLUMN;
187,416✔
1343
      break;
187,416✔
1344
    }
1345
    default:
×
1346
      pCxt->errCode = TSDB_CODE_PAR_INVALID_EXPR_SUBQ;
×
1347
      CHECK_PARSER_STATUS(pCxt);
×
1348
      break;
×
1349
  }
1350

1351
  return pNode;
59,440,302✔
1352

1353
_err:
×
1354
  return NULL;
×
1355
}
1356

1357
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
624,026,895✔
1358
  CHECK_PARSER_STATUS(pCxt);
624,026,895✔
1359
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
624,026,388✔
1360
    SValueNode* pVal = (SValueNode*)pLeft;
30,408,880✔
1361
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
30,408,880✔
1362
    if (!pNewLiteral) {
30,408,813✔
1363
      pCxt->errCode = terrno;
×
1364
      goto _err;
×
1365
    }
1366
    if ('+' == pVal->literal[0]) {
30,408,813✔
1367
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal + 1);
×
1368
    } else if ('-' == pVal->literal[0]) {
30,408,774✔
1369
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "%s", pVal->literal + 1);
12,960✔
1370
    } else {
1371
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal);
30,395,814✔
1372
    }
1373
    taosMemoryFree(pVal->literal);
30,408,813✔
1374
    pVal->literal = pNewLiteral;
30,408,727✔
1375
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
30,408,727✔
1376
    return pLeft;
30,408,624✔
1377
  }
1378
  if ((OP_TYPE_IN == type || OP_TYPE_NOT_IN == type) && pRight) {
593,617,508✔
1379
    if (QUERY_NODE_SELECT_STMT == nodeType(pRight)) {
23,877,281✔
1380
      ((SSelectStmt*)pRight)->subQType= E_SUB_QUERY_COLUMN;
17,500,218✔
1381
    } else if (QUERY_NODE_SET_OPERATOR == nodeType(pRight)) {
6,377,063✔
1382
      ((SSetOperator*)pRight)->subQType= E_SUB_QUERY_COLUMN;
874,979✔
1383
    }
1384
  }
1385
  if ((OP_TYPE_EXISTS == type || OP_TYPE_NOT_EXISTS == type) && pLeft) {
593,617,508✔
1386
    if (QUERY_NODE_SELECT_STMT == nodeType(pLeft)) {
4,805,656✔
1387
      ((SSelectStmt*)pLeft)->subQType= E_SUB_QUERY_ROWNUM;
4,805,656✔
1388
    } else if (QUERY_NODE_SET_OPERATOR == nodeType(pLeft)) {
×
1389
      ((SSetOperator*)pLeft)->subQType= E_SUB_QUERY_ROWNUM;
×
1390
    }
1391
  }
1392
  if (pLeft && QUERY_NODE_VALUE == nodeType(pLeft)) {
593,617,508✔
1393
    SValueNode* pVal = (SValueNode*)pLeft;
38,474,904✔
1394
    pVal->tz = pCxt->pQueryCxt->timezone;
38,474,904✔
1395
  }
1396
  if (pRight && QUERY_NODE_VALUE == nodeType(pRight)) {
593,617,115✔
1397
    SValueNode* pVal = (SValueNode*)pRight;
265,771,676✔
1398
    pVal->tz = pCxt->pQueryCxt->timezone;
265,771,676✔
1399
  }
1400

1401
  SOperatorNode* op = NULL;
593,617,310✔
1402
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
593,618,977✔
1403
  CHECK_MAKE_NODE(op);
593,642,841✔
1404
  op->opType = type;
593,642,841✔
1405
  op->pLeft = pLeft;
593,642,823✔
1406
  op->pRight = pRight;
593,637,363✔
1407
  op->tz = pCxt->pQueryCxt->timezone;
593,638,844✔
1408
  op->charsetCxt = pCxt->pQueryCxt->charsetCxt;
593,642,735✔
1409
  return (SNode*)op;
593,640,757✔
1410
_err:
×
1411
  nodesDestroyNode(pLeft);
×
1412
  nodesDestroyNode(pRight);
×
1413
  return NULL;
×
1414
}
1415

1416
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
21,131,500✔
1417
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
21,131,500✔
1418
  CHECK_PARSER_STATUS(pCxt);
21,131,500✔
1419
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
21,131,070✔
1420
  CHECK_PARSER_STATUS(pCxt);
21,131,188✔
1421
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
21,132,131✔
1422
  CHECK_PARSER_STATUS(pCxt);
21,131,939✔
1423
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
21,131,939✔
1424
  CHECK_PARSER_STATUS(pCxt);
21,132,302✔
1425
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
21,131,365✔
1426
  CHECK_PARSER_STATUS(pCxt);
21,130,361✔
1427
  return pRet;
21,131,728✔
1428
_err:
×
1429
  nodesDestroyNode(pNew);
×
1430
  nodesDestroyNode(pGE);
×
1431
  nodesDestroyNode(pLE);
×
1432
  nodesDestroyNode(pExpr);
×
1433
  nodesDestroyNode(pLeft);
×
1434
  nodesDestroyNode(pRight);
×
1435
  return NULL;
×
1436
}
1437

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

1460
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
87,279,061✔
1461
  CHECK_PARSER_STATUS(pCxt);
87,279,061✔
1462
  SColumnNode* pCol = NULL;
87,278,161✔
1463
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
87,279,491✔
1464
  CHECK_MAKE_NODE(pCol);
87,284,484✔
1465
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
87,284,484✔
1466
  if (NULL == pFuncName) {
87,283,883✔
1467
    tstrncpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME, TSDB_COL_NAME_LEN);
74,978,075✔
1468
  } else {
1469
    tstrncpy(pCol->colName, pFuncName->z, pFuncName->n + 1);
12,305,808✔
1470
  }
1471
  pCol->isPrimTs = true;
87,281,750✔
1472
  return (SNode*)pCol;
87,282,131✔
1473
_err:
×
1474
  return NULL;
×
1475
}
1476

1477
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
511,784,949✔
1478
  CHECK_PARSER_STATUS(pCxt);
511,784,949✔
1479
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
511,784,631✔
1480
    return createPrimaryKeyCol(pCxt, pFuncName);
12,299,191✔
1481
  }
1482
  SFunctionNode* func = NULL;
499,485,483✔
1483
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
499,486,206✔
1484
  CHECK_MAKE_NODE(func);
499,489,474✔
1485
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
499,489,474✔
1486
  func->pParameterList = pParameterList;
499,485,350✔
1487
  func->tz = pCxt->pQueryCxt->timezone;
499,490,539✔
1488
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
499,489,362✔
1489
  return (SNode*)func;
499,489,538✔
1490
_err:
×
1491
  nodesDestroyList(pParameterList);
×
1492
  return NULL;
×
1493
}
1494

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

1510
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
109,456,304✔
1511
  SFunctionNode* func = NULL;
109,456,304✔
1512
  CHECK_PARSER_STATUS(pCxt);
109,456,304✔
1513
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
109,456,304✔
1514
  CHECK_MAKE_NODE(func);
109,457,432✔
1515
  tstrncpy(func->functionName, "cast", TSDB_FUNC_NAME_LEN);
109,457,432✔
1516
  func->node.resType = dt;
109,457,432✔
1517
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
109,457,432✔
1518
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
84,734,607✔
1519
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
24,722,825✔
1520
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
3,875,979✔
1521
  }
1522
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
109,457,432✔
1523
  CHECK_PARSER_STATUS(pCxt);
109,456,874✔
1524
  func->tz = pCxt->pQueryCxt->timezone;
109,456,874✔
1525
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
109,456,874✔
1526

1527
  return (SNode*)func;
109,456,874✔
1528
_err:
×
1529
  nodesDestroyNode((SNode*)func);
×
1530
  nodesDestroyNode(pExpr);
×
1531
  return NULL;
642✔
1532
}
1533

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

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

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

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

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

1628
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
7,409,170✔
1629
  SNodeListNode* list = NULL;
7,409,170✔
1630
  CHECK_PARSER_STATUS(pCxt);
7,409,170✔
1631
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
7,408,144✔
1632
  CHECK_MAKE_NODE(list);
7,409,600✔
1633
  list->pNodeList = pList;
7,409,600✔
1634
  return (SNode*)list;
7,409,600✔
1635
_err:
×
1636
  nodesDestroyList(pList);
×
1637
  return NULL;
×
1638
}
1639

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

1657
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
868,919,441✔
1658
  CHECK_PARSER_STATUS(pCxt);
868,919,441✔
1659
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
868,919,535✔
1660
  CHECK_NAME(checkTableName(pCxt, pTableName));
868,926,807✔
1661
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
868,924,924✔
1662
  SRealTableNode* realTable = NULL;
868,923,563✔
1663
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
868,926,330✔
1664
  CHECK_MAKE_NODE(realTable);
868,934,562✔
1665
  if (NULL != pDbName) {
868,934,562✔
1666
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
183,172,046✔
1667
  } else {
1668
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
685,762,516✔
1669
  }
1670
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
868,937,574✔
1671
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
91,293,603✔
1672
  } else {
1673
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
777,643,508✔
1674
  }
1675
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
868,924,508✔
1676
  return (SNode*)realTable;
868,926,640✔
1677
_err:
10,748✔
1678
  return NULL;
10,748✔
1679
}
1680

1681
SNode* createPlaceHolderTableNode(SAstCreateContext* pCxt, EStreamPlaceholder type, SToken* pTableAlias) {
149,877✔
1682
  CHECK_PARSER_STATUS(pCxt);
149,877✔
1683

1684
  SPlaceHolderTableNode* phTable = NULL;
149,877✔
1685
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PLACE_HOLDER_TABLE, (SNode**)&phTable);
149,877✔
1686
  CHECK_MAKE_NODE(phTable);
149,877✔
1687

1688
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
149,877✔
1689
    COPY_STRING_FORM_ID_TOKEN(phTable->table.tableAlias, pTableAlias);
3,112✔
1690
  }
1691

1692
  phTable->placeholderType = type;
149,877✔
1693
  return (SNode*)phTable;
149,877✔
1694
_err:
×
1695
  return NULL;
×
1696
}
1697

1698
SNode* createStreamNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pStreamName) {
545,557✔
1699
  CHECK_PARSER_STATUS(pCxt);
545,557✔
1700
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
545,557✔
1701
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
545,557✔
1702
  SStreamNode* pStream = NULL;
545,345✔
1703
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM, (SNode**)&pStream);
545,345✔
1704
  CHECK_MAKE_NODE(pStream);
545,345✔
1705
  if (NULL != pDbName) {
545,345✔
1706
    COPY_STRING_FORM_ID_TOKEN(pStream->dbName, pDbName);
374,234✔
1707
  } else {
1708
    snprintf(pStream->dbName, sizeof(pStream->dbName), "%s", pCxt->pQueryCxt->db);
171,111✔
1709
  }
1710
  COPY_STRING_FORM_ID_TOKEN(pStream->streamName, pStreamName);
545,345✔
1711
  return (SNode*)pStream;
545,345✔
1712
_err:
212✔
1713
  return NULL;
212✔
1714
}
1715

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

1729
  return (SNode*)pRange;
12,249✔
1730
_err:
×
1731
  nodesDestroyNode((SNode*)pRange);
×
1732
  nodesDestroyNode(pStart);
×
1733
  nodesDestroyNode(pEnd);
×
1734
  return NULL;
×
1735
}
1736

1737
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
32,922,199✔
1738
  CHECK_PARSER_STATUS(pCxt);
32,922,199✔
1739
  if (!checkTableName(pCxt, pTableAlias)) {
32,921,781✔
1740
    return NULL;
×
1741
  }
1742
  STempTableNode* tempTable = NULL;
32,921,781✔
1743
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
32,921,781✔
1744
  CHECK_MAKE_NODE(tempTable);
32,922,199✔
1745
  tempTable->pSubquery = pSubquery;
32,922,199✔
1746
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
32,922,199✔
1747
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
1,103,519✔
1748
  } else {
1749
    taosRandStr(tempTable->table.tableAlias, 32);
31,818,680✔
1750
  }
1751
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
32,922,199✔
1752
    tstrncpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
28,569,134✔
1753
    ((SSelectStmt*)pSubquery)->isSubquery = true;
28,569,134✔
1754
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
4,353,065✔
1755
    tstrncpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
4,352,647✔
1756
  }
1757
  return (SNode*)tempTable;
32,922,199✔
1758
_err:
×
1759
  nodesDestroyNode(pSubquery);
×
1760
  return NULL;
×
1761
}
1762

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

1782
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
399,983✔
1783
  CHECK_PARSER_STATUS(pCxt);
399,983✔
1784
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
399,983✔
1785
  CHECK_NAME(checkViewName(pCxt, pViewName));
399,559✔
1786
  SViewNode* pView = NULL;
398,751✔
1787
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
398,751✔
1788
  CHECK_MAKE_NODE(pView);
398,751✔
1789
  if (NULL != pDbName) {
398,751✔
1790
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
195,948✔
1791
  } else {
1792
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
202,803✔
1793
  }
1794
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
398,751✔
1795
  return (SNode*)pView;
398,751✔
1796
_err:
1,232✔
1797
  return NULL;
1,232✔
1798
}
1799

1800
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) {
31,235,533✔
1801
  CHECK_PARSER_STATUS(pCxt);
31,235,533✔
1802
  SLimitNode* limitNode = NULL;
31,235,533✔
1803
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
31,235,570✔
1804
  CHECK_MAKE_NODE(limitNode);
31,236,026✔
1805
  limitNode->limit = (SValueNode*)pLimit;
31,236,026✔
1806
  if (NULL != pOffset) {
31,236,063✔
1807
    limitNode->offset = (SValueNode*)pOffset;
7,026,195✔
1808
  }
1809
  return (SNode*)limitNode;
31,236,063✔
1810
_err:
×
1811
  return NULL;
×
1812
}
1813

1814
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
189,634,169✔
1815
  CHECK_PARSER_STATUS(pCxt);
189,634,169✔
1816
  SOrderByExprNode* orderByExpr = NULL;
189,634,206✔
1817
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
189,633,788✔
1818
  CHECK_MAKE_NODE(orderByExpr);
189,646,064✔
1819
  orderByExpr->pExpr = pExpr;
189,646,064✔
1820
  orderByExpr->order = order;
189,646,027✔
1821
  if (NULL_ORDER_DEFAULT == nullOrder) {
189,645,646✔
1822
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
189,417,435✔
1823
  }
1824
  orderByExpr->nullOrder = nullOrder;
189,645,646✔
1825
  return (SNode*)orderByExpr;
189,645,609✔
1826
_err:
×
1827
  nodesDestroyNode(pExpr);
×
1828
  return NULL;
×
1829
}
1830

1831
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
15,166,319✔
1832
  CHECK_PARSER_STATUS(pCxt);
15,166,319✔
1833
  SSessionWindowNode* session = NULL;
15,166,319✔
1834
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
15,166,319✔
1835
  CHECK_MAKE_NODE(session);
15,168,051✔
1836
  session->pCol = (SColumnNode*)pCol;
15,168,051✔
1837
  session->pGap = (SValueNode*)pGap;
15,168,051✔
1838
  return (SNode*)session;
15,168,051✔
1839
_err:
×
1840
  nodesDestroyNode(pCol);
×
1841
  nodesDestroyNode(pGap);
×
1842
  return NULL;
×
1843
}
1844

1845
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr, SNodeList* pOptions, SNode* pTrueForLimit) {
6,016,911✔
1846
  SStateWindowNode* state = NULL;
6,016,911✔
1847
  CHECK_PARSER_STATUS(pCxt);
6,016,911✔
1848
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
6,016,519✔
1849
  CHECK_MAKE_NODE(state);
6,017,438✔
1850
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
6,017,438✔
1851
  CHECK_MAKE_NODE(state->pCol);
6,016,409✔
1852
  state->pExpr = pExpr;
6,016,409✔
1853
  state->pTrueForLimit = pTrueForLimit;
6,016,409✔
1854
  if (pOptions != NULL) {
6,016,409✔
1855
    if (pOptions->length >= 1) {
427,540✔
1856
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 0), &state->pExtend);
427,540✔
1857
      CHECK_MAKE_NODE(state->pExtend);
427,540✔
1858
    }
1859
    if (pOptions->length == 2) {
427,540✔
1860
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 1), &state->pZeroth);
×
1861
      CHECK_MAKE_NODE(state->pZeroth);
×
1862
    }
1863
    nodesDestroyList(pOptions);
427,540✔
1864
  }
1865
  return (SNode*)state;
6,016,781✔
1866
_err:
392✔
1867
  nodesDestroyNode((SNode*)state);
392✔
1868
  nodesDestroyNode(pExpr);
392✔
1869
  nodesDestroyNode(pTrueForLimit);
392✔
1870
  nodesDestroyList(pOptions);
392✔
1871
  return NULL;
485✔
1872
}
1873

1874
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond, SNode* pTrueForLimit) {
5,449,175✔
1875
  SEventWindowNode* pEvent = NULL;
5,449,175✔
1876
  CHECK_PARSER_STATUS(pCxt);
5,449,175✔
1877
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
5,449,175✔
1878
  CHECK_MAKE_NODE(pEvent);
5,449,485✔
1879
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
5,449,485✔
1880
  CHECK_MAKE_NODE(pEvent->pCol);
5,449,596✔
1881
  pEvent->pStartCond = pStartCond;
5,449,596✔
1882
  pEvent->pEndCond = pEndCond;
5,449,596✔
1883
  pEvent->pTrueForLimit = pTrueForLimit;
5,449,596✔
1884
  return (SNode*)pEvent;
5,449,596✔
1885
_err:
×
1886
  nodesDestroyNode((SNode*)pEvent);
×
1887
  nodesDestroyNode(pStartCond);
×
1888
  nodesDestroyNode(pEndCond);
×
1889
  nodesDestroyNode(pTrueForLimit);
×
UNCOV
1890
  return NULL;
×
1891
}
1892

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

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

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

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

1964
SNode* createCountWindowNodeFromArgs(SAstCreateContext* pCxt, SNode* arg) {
5,108,181✔
1965
  SCountWindowArgs* args = (SCountWindowArgs*)arg;
5,108,181✔
1966
  SCountWindowNode* pCount = NULL;
5,108,181✔
1967
  CHECK_PARSER_STATUS(pCxt);
5,108,181✔
1968
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
5,108,181✔
1969
  CHECK_MAKE_NODE(pCount);
5,109,198✔
1970
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
5,109,198✔
1971
  CHECK_MAKE_NODE(pCount->pCol);
5,107,085✔
1972
  pCount->windowCount = args->count;
5,107,085✔
1973
  pCount->windowSliding = args->sliding;
5,107,085✔
1974
  pCount->pColList = args->pColList;
5,107,085✔
1975
  args->pColList = NULL;
5,107,085✔
1976
  nodesDestroyNode(arg);
5,107,085✔
1977
  return (SNode*)pCount;
5,108,768✔
1978
_err:
×
1979
  nodesDestroyNode((SNode*)pCount);
×
1980
  return NULL;
×
1981
}
1982

1983
SNode* createCountWindowArgs(SAstCreateContext* pCxt, const SToken* countToken, const SToken* slidingToken,
5,111,962✔
1984
                             SNodeList* colList) {
1985
  CHECK_PARSER_STATUS(pCxt);
5,111,962✔
1986

1987
  SCountWindowArgs* args = NULL;
5,111,962✔
1988
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW_ARGS, (SNode**)&args);
5,111,962✔
1989
  CHECK_MAKE_NODE(args);
5,113,403✔
1990
  args->count = taosStr2Int64(countToken->z, NULL, 10);
5,113,403✔
1991
  if (slidingToken && slidingToken->type == TK_NK_INTEGER) {
5,113,127✔
1992
    args->sliding = taosStr2Int64(slidingToken->z, NULL, 10);
30,780✔
1993
  } else {
1994
    args->sliding = taosStr2Int64(countToken->z, NULL, 10);
5,082,347✔
1995
  }
1996
  args->pColList = colList;
5,112,791✔
1997
  return (SNode*)args;
5,112,791✔
1998
_err:
×
1999
  return NULL;
×
2000
}
2001

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

2011
  pAnomaly->pExpr = pExprList;
×
2012

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

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

2029
  return (SNode*)pAnomaly;
×
2030

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

2036
SNode* createIntervalWindowNodeExt(SAstCreateContext* pCxt, SNode* pInter, SNode* pSliding) {
214,279✔
2037
  SIntervalWindowNode* pInterval = NULL;
214,279✔
2038
  CHECK_PARSER_STATUS(pCxt);
214,279✔
2039
  if (pInter) {
214,279✔
2040
    pInterval = (SIntervalWindowNode*)pInter;
161,986✔
2041
  } else {
2042
    pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&pInterval);
52,293✔
2043
    CHECK_MAKE_NODE(pInterval);
52,293✔
2044
  }
2045
  pInterval->pCol = createPrimaryKeyCol(pCxt, NULL);
214,279✔
2046
  CHECK_MAKE_NODE(pInterval->pCol);
214,279✔
2047
  pInterval->pSliding = ((SSlidingWindowNode*)pSliding)->pSlidingVal;
214,279✔
2048
  pInterval->pSOffset = ((SSlidingWindowNode*)pSliding)->pOffset;
214,279✔
2049
  return (SNode*)pInterval;
214,279✔
2050
_err:
×
2051
  nodesDestroyNode((SNode*)pInter);
×
2052
  nodesDestroyNode((SNode*)pInterval);
×
2053
  nodesDestroyNode((SNode*)pSliding);
×
2054
  return NULL;
×
2055
}
2056

2057
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
44,766,991✔
2058
                                SNode* pFill) {
2059
  SIntervalWindowNode* interval = NULL;
44,766,991✔
2060
  CHECK_PARSER_STATUS(pCxt);
44,766,991✔
2061
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
44,766,991✔
2062
  CHECK_MAKE_NODE(interval);
44,772,694✔
2063
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
44,772,694✔
2064
  CHECK_MAKE_NODE(interval->pCol);
44,772,343✔
2065
  interval->pInterval = pInterval;
44,772,343✔
2066
  interval->pOffset = pOffset;
44,772,343✔
2067
  interval->pSliding = pSliding;
44,772,343✔
2068
  interval->pFill = pFill;
44,772,343✔
2069
  TAOS_SET_OBJ_ALIGNED(&interval->timeRange, TSWINDOW_INITIALIZER);
44,772,343✔
2070
  interval->timezone = pCxt->pQueryCxt->timezone;
44,772,343✔
2071
  return (SNode*)interval;
44,772,761✔
2072
_err:
×
2073
  nodesDestroyNode((SNode*)interval);
×
2074
  nodesDestroyNode(pInterval);
×
2075
  nodesDestroyNode(pOffset);
×
2076
  nodesDestroyNode(pSliding);
×
2077
  nodesDestroyNode(pFill);
×
UNCOV
2078
  return NULL;
×
2079
}
2080

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

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

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

2127
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
4,328,480✔
2128
  return createFillNodeWithSurroundingTime(pCxt, mode, pValues, NULL);
4,328,480✔
2129
}
2130

2131
SNode* createFillNodeWithSurroundNode(SAstCreateContext* pCxt, EFillMode mode,
2,043,145✔
2132
                                      SNode* pSurroundNode) {
2133
  if (pSurroundNode == NULL) {
2,043,145✔
2134
    return createFillNode(pCxt, mode, NULL);
1,960,246✔
2135
  }
2136

2137
  SSurroundNode* pSurround = (SSurroundNode*)pSurroundNode;
82,899✔
2138
  SNode* pSurroundingTime = pSurround->pSurroundingTime;
82,899✔
2139
  SNode* pValues = pSurround->pValues;
82,899✔
2140

2141
  /* set surround node to NULL to avoid freeing before using */
2142
  pSurround->pSurroundingTime = NULL;
82,899✔
2143
  pSurround->pValues = NULL;
82,899✔
2144
  nodesDestroyNode((SNode*)pSurround);
82,899✔
2145

2146
  return createFillNodeWithSurroundingTime(pCxt, mode, pValues,
82,899✔
2147
                                           pSurroundingTime);
2148
}
2149

2150
SNode* createFillNodeWithSurroundingTime(SAstCreateContext* pCxt,
4,411,456✔
2151
                                         EFillMode mode, SNode* pValues,
2152
                                         SNode* pSurroundingTime) {
2153
  SFillNode* fill = NULL;
4,411,456✔
2154
  CHECK_PARSER_STATUS(pCxt);
4,411,456✔
2155
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
4,409,499✔
2156
  CHECK_MAKE_NODE(fill);
4,410,777✔
2157
  fill->mode = mode;
4,410,777✔
2158
  fill->pValues = pValues;
4,411,803✔
2159
  fill->pSurroundingTime = pSurroundingTime;
4,411,886✔
2160
  fill->pWStartTs = NULL;
4,409,576✔
2161
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION,
4,425,559✔
2162
                                (SNode**)&(fill->pWStartTs));
4,408,215✔
2163
  CHECK_MAKE_NODE(fill->pWStartTs);
4,411,886✔
2164
  tstrncpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart",
4,411,373✔
2165
           TSDB_FUNC_NAME_LEN);
2166
  return (SNode*)fill;
4,409,333✔
2167
_err:
×
2168
  nodesDestroyNode((SNode*)fill);
×
2169
  nodesDestroyNode(pValues);
×
2170
  nodesDestroyNode(pSurroundingTime);
×
2171
  return NULL;
×
2172
}
2173

2174
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
60,563,573✔
2175
  SGroupingSetNode* groupingSet = NULL;
60,563,573✔
2176
  CHECK_PARSER_STATUS(pCxt);
60,563,573✔
2177
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
60,563,573✔
2178
  CHECK_MAKE_NODE(groupingSet);
60,567,506✔
2179
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
60,567,506✔
2180
  groupingSet->pParameterList = NULL;
60,567,506✔
2181
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
60,567,506✔
2182
  CHECK_PARSER_STATUS(pCxt);
60,565,897✔
2183
  return (SNode*)groupingSet;
60,565,897✔
2184
_err:
×
2185
  nodesDestroyNode((SNode*)groupingSet);
×
2186
  nodesDestroyNode(pNode);
×
2187
  return NULL;
1,481✔
2188
}
2189

2190
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
3,690,272✔
2191
  CHECK_PARSER_STATUS(pCxt);
3,690,272✔
2192
  if (isSubQueryNode(pStart) || isSubQueryNode(pEnd) || isSubQueryNode(pInterval)) {
3,689,412✔
2193
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
1,182✔
2194
    CHECK_PARSER_STATUS(pCxt);
752✔
2195
  }
2196

2197
  if (NULL == pInterval) {
3,689,520✔
2198
    if (pEnd && nodeType(pEnd) == QUERY_NODE_VALUE && ((SValueNode*)pEnd)->flag & VALUE_FLAG_IS_DURATION) {
3,567,692✔
2199
      return createInterpTimeAround(pCxt, pStart, NULL, pEnd);
1,462,501✔
2200
    }
2201
    return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
2,104,678✔
2202
  }
2203

2204
  return createInterpTimeAround(pCxt, pStart, pEnd, pInterval);
121,828✔
2205

2206
_err:
752✔
2207

2208
  nodesDestroyNode(pStart);
752✔
2209
  nodesDestroyNode(pEnd);
752✔
2210
  nodesDestroyNode(pInterval);
752✔
2211
  return NULL;
752✔
2212
}
2213

2214
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
1,598,311✔
2215
  CHECK_PARSER_STATUS(pCxt);
1,598,311✔
2216
  if (isSubQueryNode(pPoint)) {
1,597,798✔
2217
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
376✔
2218
    CHECK_PARSER_STATUS(pCxt);
376✔
2219
  }
2220

2221
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
1,598,282✔
2222
_err:
376✔
2223
  nodesDestroyNode(pPoint);
376✔
2224
  return NULL;
376✔
2225
}
2226

2227
SNode* createInterpTimeAround(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
1,584,925✔
2228
  CHECK_PARSER_STATUS(pCxt);
1,584,925✔
2229
  SRangeAroundNode* pAround = NULL;
1,583,392✔
2230
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RANGE_AROUND, (SNode**)&pAround);
1,583,822✔
2231
  CHECK_PARSER_STATUS(pCxt);
1,584,842✔
2232
  if (NULL == pEnd) {
1,585,355✔
2233
    pAround->pRange = createInterpTimePoint(pCxt, pStart);
1,463,527✔
2234
  } else {
2235
    pAround->pRange = createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
121,828✔
2236
  }
2237
  pAround->pInterval = pInterval;
1,583,899✔
2238
  CHECK_PARSER_STATUS(pCxt);
1,584,495✔
2239
  return (SNode*)pAround;
1,584,412✔
2240
_err:
×
2241
  return NULL;
×
2242
}
2243

2244
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
36,224,618✔
2245
  CHECK_PARSER_STATUS(pCxt);
36,224,618✔
2246
  SWhenThenNode* pWhenThen = NULL;
36,224,618✔
2247
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
36,224,618✔
2248
  CHECK_MAKE_NODE(pWhenThen);
36,229,037✔
2249
  pWhenThen->pWhen = pWhen;
36,229,037✔
2250
  pWhenThen->pThen = pThen;
36,229,037✔
2251
  return (SNode*)pWhenThen;
36,229,037✔
2252
_err:
×
2253
  nodesDestroyNode(pWhen);
×
2254
  nodesDestroyNode(pThen);
×
2255
  return NULL;
×
2256
}
2257

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

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

2287
SNode* createNullIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
5,486✔
2288
  SNode *    pCase = NULL, *pEqual = NULL, *pThen = NULL;
5,486✔
2289
  SNode *    pWhenThenNode = NULL, *pElse = NULL;
5,486✔
2290
  SNodeList* pWhenThenList = NULL;
5,486✔
2291
  SNode*     pCaseWhen = NULL;
5,486✔
2292

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

2323
SNode* createIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
71,717✔
2324
  SNode*     pCase = NULL;
71,717✔
2325
  SNode*     pWhenThenNode = NULL;
71,717✔
2326
  SNodeList* pWhenThenList = NULL;
71,717✔
2327
  SNode*     pCaseWhen = NULL;
71,717✔
2328

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

2345
SNode* createNvlNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
14,143✔
2346
  SNode *    pThen = NULL, *pEqual = NULL;
14,143✔
2347
  SNode*     pWhenThenNode = NULL;
14,143✔
2348
  SNodeList* pWhenThenList = NULL;
14,143✔
2349
  SNode*     pCaseWhen = NULL;
14,143✔
2350

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

2372
SNode* createNvl2Node(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
4,320✔
2373
  SNode *    pEqual = NULL, *pWhenThenNode = NULL;
4,320✔
2374
  SNodeList* pWhenThenList = NULL;
4,320✔
2375
  SNode*     pCaseWhen = NULL;
4,320✔
2376

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

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

2401
  CHECK_PARSER_STATUS(pCxt);
3,116✔
2402

2403
  for (int i = 0; i < sizeParam; ++i) {
10,323✔
2404
    pExpr = nodesListGetNode(pParamList, i);
7,207✔
2405

2406
    pCxt->errCode = nodesCloneNode(pExpr, &pThen);
7,207✔
2407
    CHECK_PARSER_STATUS(pCxt);
7,207✔
2408

2409
    pNotNullCond = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr, NULL);
7,207✔
2410
    CHECK_PARSER_STATUS(pCxt);
7,207✔
2411

2412
    pWhenThenNode = createWhenThenNode(pCxt, pNotNullCond, pThen);
7,207✔
2413
    CHECK_PARSER_STATUS(pCxt);
7,207✔
2414

2415
    if (!pWhenThenList) {
7,207✔
2416
      pWhenThenList = createNodeList(pCxt, pWhenThenNode);
3,116✔
2417
    } else {
2418
      pCxt->errCode = nodesListAppend(pWhenThenList, pWhenThenNode);
4,091✔
2419
    }
2420
    CHECK_PARSER_STATUS(pCxt);
7,207✔
2421
  }
2422

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

2436
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
96,219,520✔
2437
  CHECK_PARSER_STATUS(pCxt);
96,219,520✔
2438
  trimEscape(pCxt, pAlias, false);
96,219,520✔
2439
  SExprNode* pExpr = (SExprNode*)pNode;
96,220,732✔
2440
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
96,220,732✔
2441
  tstrncpy(pExpr->aliasName, pAlias->z, len + 1);
96,220,732✔
2442
  tstrncpy(pExpr->userAlias, pAlias->z, len + 1);
96,220,732✔
2443
  pExpr->asAlias = true;
96,220,732✔
2444
  return pNode;
96,220,732✔
2445
_err:
×
2446
  nodesDestroyNode(pNode);
×
2447
  return NULL;
×
2448
}
2449

2450
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
752,013,954✔
2451
  CHECK_PARSER_STATUS(pCxt);
752,013,954✔
2452
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
752,009,973✔
2453
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
752,040,884✔
2454
  }
2455
  return pStmt;
752,007,937✔
2456
_err:
1,276✔
2457
  nodesDestroyNode(pStmt);
1,276✔
2458
  nodesDestroyNode(pWhere);
1,276✔
2459
  return NULL;
1,276✔
2460
}
2461

2462
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
752,019,780✔
2463
  CHECK_PARSER_STATUS(pCxt);
752,019,780✔
2464
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
752,019,068✔
2465
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
752,031,268✔
2466
  }
2467
  return pStmt;
752,013,946✔
2468
_err:
1,276✔
2469
  nodesDestroyNode(pStmt);
1,276✔
2470
  nodesDestroyList(pPartitionByList);
1,276✔
2471
  return NULL;
1,276✔
2472
}
2473

2474
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
752,015,023✔
2475
  CHECK_PARSER_STATUS(pCxt);
752,015,023✔
2476
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
752,012,636✔
2477
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
752,031,223✔
2478
  }
2479
  return pStmt;
752,006,156✔
2480
_err:
1,276✔
2481
  nodesDestroyNode(pStmt);
1,276✔
2482
  nodesDestroyNode(pWindow);
1,276✔
2483
  return NULL;
1,276✔
2484
}
2485

2486
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
752,013,214✔
2487
  CHECK_PARSER_STATUS(pCxt);
752,013,214✔
2488
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
752,011,369✔
2489
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
752,027,958✔
2490
  }
2491
  return pStmt;
752,004,318✔
2492
_err:
1,276✔
2493
  nodesDestroyNode(pStmt);
1,276✔
2494
  nodesDestroyList(pGroupByList);
1,276✔
2495
  return NULL;
1,276✔
2496
}
2497

2498
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
752,011,750✔
2499
  CHECK_PARSER_STATUS(pCxt);
752,011,750✔
2500
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
752,010,600✔
2501
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
752,026,054✔
2502
  }
2503
  return pStmt;
752,004,207✔
2504
_err:
1,276✔
2505
  nodesDestroyNode(pStmt);
1,276✔
2506
  nodesDestroyNode(pHaving);
1,276✔
2507
  return NULL;
1,276✔
2508
}
2509

2510
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
714,549,927✔
2511
  CHECK_PARSER_STATUS(pCxt);
714,549,927✔
2512
  if (NULL == pOrderByList) {
714,549,689✔
2513
    return pStmt;
564,671,203✔
2514
  }
2515
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
149,878,486✔
2516
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
134,940,219✔
2517
  } else {
2518
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
14,938,267✔
2519
  }
2520
  return pStmt;
149,878,523✔
2521
_err:
1,276✔
2522
  nodesDestroyNode(pStmt);
1,276✔
2523
  nodesDestroyList(pOrderByList);
1,276✔
2524
  return NULL;
1,276✔
2525
}
2526

2527
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
714,547,687✔
2528
  CHECK_PARSER_STATUS(pCxt);
714,547,687✔
2529
  if (NULL == pSlimit) {
714,544,970✔
2530
    return pStmt;
712,139,791✔
2531
  }
2532
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
2,415,268✔
2533
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
2,417,878✔
2534
  }
2535
  return pStmt;
2,415,268✔
2536
_err:
1,276✔
2537
  nodesDestroyNode(pStmt);
1,276✔
2538
  nodesDestroyNode(pSlimit);
1,276✔
2539
  return NULL;
1,276✔
2540
}
2541

2542
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
714,530,248✔
2543
  CHECK_PARSER_STATUS(pCxt);
714,530,248✔
2544
  if (NULL == pLimit) {
714,529,147✔
2545
    return pStmt;
685,966,175✔
2546
  }
2547
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
28,562,972✔
2548
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
27,475,050✔
2549
  } else {
2550
    ((SSetOperator*)pStmt)->pLimit = pLimit;
1,111,050✔
2551
  }
2552
  return pStmt;
28,563,009✔
2553
_err:
1,276✔
2554
  nodesDestroyNode(pStmt);
1,276✔
2555
  nodesDestroyNode(pLimit);
1,276✔
2556
  return NULL;
1,276✔
2557
}
2558

2559
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
752,002,753✔
2560
  CHECK_PARSER_STATUS(pCxt);
752,002,753✔
2561
  SSelectStmt* pSelect = (SSelectStmt*)pStmt;
752,001,441✔
2562
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
752,001,441✔
2563
    if (pRange && nodeType(pRange) == QUERY_NODE_RANGE_AROUND) {
752,030,343✔
2564
      pSelect->pRangeAround = pRange;
1,577,426✔
2565
      SRangeAroundNode* pAround = (SRangeAroundNode*)pRange;
1,578,369✔
2566
      TSWAP(pSelect->pRange, pAround->pRange);
1,578,369✔
2567
    } else {
2568
      pSelect->pRange = pRange;
750,452,487✔
2569
    }
2570
  }
2571
  return pStmt;
752,001,641✔
2572
_err:
1,276✔
2573
  nodesDestroyNode(pStmt);
1,276✔
2574
  nodesDestroyNode(pRange);
1,276✔
2575
  return NULL;
1,276✔
2576
}
2577

2578
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
751,999,842✔
2579
  CHECK_PARSER_STATUS(pCxt);
751,999,842✔
2580
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
751,999,773✔
2581
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
752,023,712✔
2582
  }
2583
  return pStmt;
751,997,687✔
2584
_err:
1,276✔
2585
  nodesDestroyNode(pStmt);
1,276✔
2586
  nodesDestroyNode(pEvery);
1,276✔
2587
  return NULL;
1,276✔
2588
}
2589

2590
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
751,996,975✔
2591
  CHECK_PARSER_STATUS(pCxt);
751,996,975✔
2592
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
751,995,775✔
2593
    SFillNode* pFillClause = (SFillNode*)pFill;
3,832,326✔
2594
    nodesDestroyNode(pFillClause->pWStartTs);
3,832,326✔
2595
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
3,832,326✔
2596
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
3,832,326✔
2597
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
3,832,249✔
2598
  }
2599
  return pStmt;
751,995,720✔
2600
_err:
1,276✔
2601
  nodesDestroyNode(pStmt);
1,276✔
2602
  nodesDestroyNode(pFill);
1,276✔
2603
  return NULL;
1,276✔
2604
}
2605

2606
SNode* createExternalWindowClause(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pAlias, SNode* pFill) {
140,224✔
2607
  SExternalWindowNode* pExtWin = NULL;
140,224✔
2608
  CHECK_PARSER_STATUS(pCxt);
140,224✔
2609
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXTERNAL_WINDOW, (SNode**)&pExtWin);
140,224✔
2610
  CHECK_MAKE_NODE(pExtWin);
140,224✔
2611

2612
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
140,224✔
2613
    ((SSelectStmt*)pSubquery)->subQType= E_SUB_QUERY_TABLE;
139,817✔
2614
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
407✔
2615
    ((SSetOperator*)pSubquery)->subQType= E_SUB_QUERY_TABLE;
407✔
2616
  }
2617

2618
    pExtWin->pCol = createPrimaryKeyCol(pCxt, NULL);
140,224✔
2619
    CHECK_MAKE_NODE(pExtWin->pCol);
140,224✔
2620

2621
  // Attach subquery and optional fill node
2622
  pExtWin->pSubquery = pSubquery;
140,224✔
2623
  pExtWin->pFill = pFill;
140,224✔
2624

2625
  // Set alias if provided; enforce length constraint (report error if too long)
2626
  pExtWin->aliasName[0] = '\0';
140,224✔
2627
  if (pAlias && pAlias->type != TK_NK_NIL) {
140,224✔
2628
    trimEscape(pCxt, pAlias, false);
140,224✔
2629
    if (pAlias->n >= TSDB_COL_NAME_LEN || pAlias->n == 0) {
140,224✔
NEW
2630
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pAlias->z);
×
NEW
2631
      goto _err;
×
2632
    }
2633
    int32_t len = pAlias->n;
140,224✔
2634
    strncpy(pExtWin->aliasName, pAlias->z, len);
140,224✔
2635
    pExtWin->aliasName[len] = '\0';
140,224✔
2636
  }
2637

2638
  return (SNode*)pExtWin;
140,224✔
NEW
2639
_err:
×
NEW
2640
  if (pExtWin) {
×
NEW
2641
    pExtWin->pSubquery = NULL;
×
NEW
2642
    pExtWin->pFill = NULL;
×
NEW
2643
    nodesDestroyNode((SNode*)pExtWin);
×
2644
  }
NEW
2645
  nodesDestroyNode(pSubquery);
×
NEW
2646
  nodesDestroyNode(pFill);
×
NEW
2647
  return NULL;
×
2648
}
2649

2650
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
38,568,626✔
2651
  CHECK_PARSER_STATUS(pCxt);
38,568,626✔
2652
  if (NULL == pJLimit) {
38,568,626✔
2653
    return pJoin;
38,338,948✔
2654
  }
2655
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
229,678✔
2656
  pJoinNode->pJLimit = pJLimit;
229,678✔
2657

2658
  return pJoin;
229,678✔
2659
_err:
×
2660
  nodesDestroyNode(pJoin);
×
2661
  nodesDestroyNode(pJLimit);
×
2662
  return NULL;
×
2663
}
2664

2665
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
38,568,827✔
2666
  CHECK_PARSER_STATUS(pCxt);
38,568,827✔
2667
  if (NULL == pWinOffset) {
38,568,827✔
2668
    return pJoin;
37,914,323✔
2669
  }
2670
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
654,973✔
2671
  pJoinNode->pWindowOffset = pWinOffset;
654,973✔
2672

2673
  return pJoin;
654,973✔
2674
_err:
×
2675
  nodesDestroyNode(pJoin);
×
2676
  nodesDestroyNode(pWinOffset);
×
2677
  return NULL;
×
2678
}
2679

2680
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
752,023,166✔
2681
                        SNodeList* pHint) {
2682
  CHECK_PARSER_STATUS(pCxt);
752,023,166✔
2683
  SNode* select = NULL;
752,020,954✔
2684
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
752,021,742✔
2685
  CHECK_MAKE_NODE(select);
752,016,296✔
2686
  return select;
752,016,296✔
2687
_err:
1,276✔
2688
  nodesDestroyList(pProjectionList);
1,276✔
2689
  nodesDestroyNode(pTable);
1,276✔
2690
  nodesDestroyList(pHint);
1,276✔
2691
  return NULL;
12,933✔
2692
}
2693

2694
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
752,019,827✔
2695
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
752,019,827✔
2696
    if (pCxt->pQueryCxt->biMode) {
752,038,720✔
2697
      ((SSelectStmt*)pStmt)->tagScan = true;
8,197✔
2698
    } else {
2699
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
752,025,693✔
2700
    }
2701
  }
2702
  return pStmt;
752,014,484✔
2703
}
2704

2705
static void setSubquery(SNode* pStmt) {
90,868,646✔
2706
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
90,868,646✔
2707
    ((SSelectStmt*)pStmt)->isSubquery = true;
90,574,373✔
2708
  }
2709
}
90,868,646✔
2710

2711
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
45,432,661✔
2712
  CHECK_PARSER_STATUS(pCxt);
45,432,661✔
2713
  SSetOperator* setOp = NULL;
45,432,661✔
2714
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
45,432,661✔
2715
  CHECK_MAKE_NODE(setOp);
45,435,778✔
2716
  setOp->opType = type;
45,435,778✔
2717
  setOp->pLeft = pLeft;
45,435,778✔
2718
  setSubquery(setOp->pLeft);
45,435,778✔
2719
  setOp->pRight = pRight;
45,435,306✔
2720
  setSubquery(setOp->pRight);
45,435,306✔
2721
  snprintf(setOp->stmtName, TSDB_TABLE_NAME_LEN, "%p", setOp);
45,436,133✔
2722
  return (SNode*)setOp;
45,436,133✔
2723
_err:
×
2724
  nodesDestroyNode(pLeft);
×
2725
  nodesDestroyNode(pRight);
×
2726
  return NULL;
17✔
2727
}
2728

2729
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
2,191,282✔
2730
  if (!pOptions->walRetentionPeriodIsSet) {
2,191,282✔
2731
    pOptions->walRetentionPeriod =
2,180,968✔
2732
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
2733
  }
2734
  if (!pOptions->walRetentionSizeIsSet) {
2,191,282✔
2735
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
2,190,535✔
2736
  }
2737
  if (!pOptions->walRollPeriodIsSet) {
2,191,282✔
2738
    pOptions->walRollPeriod =
2,191,282✔
2739
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
2740
  }
2741
}
2,191,282✔
2742

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

2793
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
235,877✔
2794
  CHECK_PARSER_STATUS(pCxt);
235,877✔
2795
  SDatabaseOptions* pOptions = NULL;
235,877✔
2796
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
235,877✔
2797
  CHECK_MAKE_NODE(pOptions);
235,877✔
2798
  pOptions->buffer = -1;
235,877✔
2799
  pOptions->cacheModel = -1;
235,877✔
2800
  pOptions->cacheLastSize = -1;
235,877✔
2801
  pOptions->cacheLastShardBits = -2;  // -2 means not set, -1 means auto-calculate
235,877✔
2802
  pOptions->compressionLevel = -1;
235,877✔
2803
  pOptions->daysPerFile = -1;
235,877✔
2804
  pOptions->fsyncPeriod = -1;
235,877✔
2805
  pOptions->maxRowsPerBlock = -1;
235,877✔
2806
  pOptions->minRowsPerBlock = -1;
235,877✔
2807
  pOptions->keep[0] = -1;
235,877✔
2808
  pOptions->keep[1] = -1;
235,877✔
2809
  pOptions->keep[2] = -1;
235,877✔
2810
  pOptions->pages = -1;
235,877✔
2811
  pOptions->pagesize = -1;
235,877✔
2812
  pOptions->tsdbPageSize = -1;
235,877✔
2813
  pOptions->precision = -1;
235,877✔
2814
  pOptions->replica = -1;
235,877✔
2815
  pOptions->strict = -1;
235,877✔
2816
  pOptions->walLevel = -1;
235,877✔
2817
  pOptions->numOfVgroups = -1;
235,877✔
2818
  pOptions->singleStable = -1;
235,877✔
2819
  pOptions->schemaless = -1;
235,877✔
2820
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
235,877✔
2821
  pOptions->walRetentionSize = -2;    // -1 is a valid value
235,877✔
2822
  pOptions->walRollPeriod = -1;
235,877✔
2823
  pOptions->walSegmentSize = -1;
235,877✔
2824
  pOptions->sstTrigger = -1;
235,877✔
2825
  pOptions->tablePrefix = -1;
235,877✔
2826
  pOptions->tableSuffix = -1;
235,877✔
2827
  pOptions->ssChunkSize = -1;
235,877✔
2828
  pOptions->ssKeepLocal = -1;
235,877✔
2829
  pOptions->ssCompact = -1;
235,877✔
2830
  pOptions->withArbitrator = -1;
235,877✔
2831
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
235,877✔
2832
  pOptions->dnodeListStr[0] = 0;
235,877✔
2833
  pOptions->compactInterval = -1;
235,877✔
2834
  pOptions->compactStartTime = -1;
235,877✔
2835
  pOptions->compactEndTime = -1;
235,877✔
2836
  pOptions->compactTimeOffset = -1;
235,877✔
2837
  pOptions->encryptAlgorithmStr[0] = 0;
235,877✔
2838
  pOptions->isAudit = -1;
235,877✔
2839
  pOptions->allowDrop = -1;
235,877✔
2840
  pOptions->secureDelete = -1;
235,877✔
2841
  return (SNode*)pOptions;
235,877✔
2842
_err:
×
2843
  return NULL;
×
2844
}
2845

2846
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
2,687,255✔
2847
                                    bool alter) {
2848
  CHECK_PARSER_STATUS(pCxt);
2,687,255✔
2849
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
2,687,255✔
2850
  switch (type) {
2,687,255✔
2851
    case DB_OPTION_BUFFER:
75,249✔
2852
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
75,249✔
2853
      break;
75,249✔
2854
    case DB_OPTION_CACHEMODEL:
85,786✔
2855
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
85,786✔
2856
      break;
85,786✔
2857
    case DB_OPTION_CACHESIZE:
28,523✔
2858
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
28,523✔
2859
      break;
28,523✔
2860
    case DB_OPTION_CACHESHARDBITS:
17,770✔
2861
      pDbOptions->cacheLastShardBits = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
17,770✔
2862
      break;
17,770✔
2863
    case DB_OPTION_COMP:
18,298✔
2864
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
18,298✔
2865
      break;
18,298✔
2866
    case DB_OPTION_DAYS: {
320,456✔
2867
      SToken* pToken = pVal;
320,456✔
2868
      if (TK_NK_INTEGER == pToken->type) {
320,456✔
2869
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
257,605✔
2870
      } else {
2871
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
62,851✔
2872
      }
2873
      break;
320,456✔
2874
    }
2875
    case DB_OPTION_FSYNC:
37,586✔
2876
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
37,586✔
2877
      break;
37,586✔
2878
    case DB_OPTION_MAXROWS:
33,492✔
2879
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
33,492✔
2880
      break;
33,492✔
2881
    case DB_OPTION_MINROWS:
40,000✔
2882
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
40,000✔
2883
      break;
40,000✔
2884
    case DB_OPTION_KEEP:
219,164✔
2885
      pDbOptions->pKeep = pVal;
219,164✔
2886
      break;
219,164✔
2887
    case DB_OPTION_PAGES:
18,338✔
2888
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
18,338✔
2889
      break;
18,338✔
2890
    case DB_OPTION_PAGESIZE:
6,655✔
2891
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
6,655✔
2892
      break;
6,655✔
2893
    case DB_OPTION_TSDB_PAGESIZE:
5,221✔
2894
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5,221✔
2895
      break;
5,221✔
2896
    case DB_OPTION_PRECISION:
142,463✔
2897
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
142,463✔
2898
      break;
142,463✔
2899
    case DB_OPTION_REPLICA:
566,928✔
2900
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
566,928✔
2901
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
566,928✔
2902
      if (!alter) {
566,928✔
2903
        updateWalOptionsDefault(pDbOptions);
553,355✔
2904
      }
2905
      break;
566,928✔
2906
    case DB_OPTION_STRICT:
×
2907
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
2908
      break;
×
2909
    case DB_OPTION_WAL:
51,634✔
2910
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
51,634✔
2911
      break;
51,634✔
2912
    case DB_OPTION_VGROUPS:
594,851✔
2913
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
594,851✔
2914
      break;
594,851✔
2915
    case DB_OPTION_SINGLE_STABLE:
8,118✔
2916
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
8,118✔
2917
      break;
8,118✔
2918
    case DB_OPTION_RETENTIONS:
5,488✔
2919
      pDbOptions->pRetentions = pVal;
5,488✔
2920
      break;
5,488✔
2921
    case DB_OPTION_WAL_RETENTION_PERIOD:
87,826✔
2922
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
87,826✔
2923
      pDbOptions->walRetentionPeriodIsSet = true;
87,826✔
2924
      break;
87,826✔
2925
    case DB_OPTION_WAL_RETENTION_SIZE:
22,007✔
2926
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
22,007✔
2927
      pDbOptions->walRetentionSizeIsSet = true;
22,007✔
2928
      break;
22,007✔
2929
    case DB_OPTION_WAL_ROLL_PERIOD:
784✔
2930
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
784✔
2931
      pDbOptions->walRollPeriodIsSet = true;
784✔
2932
      break;
784✔
2933
    case DB_OPTION_WAL_SEGMENT_SIZE:
864✔
2934
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
864✔
2935
      break;
864✔
2936
    case DB_OPTION_STT_TRIGGER:
45,988✔
2937
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
45,988✔
2938
      break;
45,988✔
2939
    case DB_OPTION_TABLE_PREFIX: {
18,083✔
2940
      SValueNode* pNode = (SValueNode*)pVal;
18,083✔
2941
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
18,083✔
2942
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
18,083✔
2943
      } else {
2944
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
2945
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2946
      }
2947
      nodesDestroyNode((SNode*)pNode);
18,083✔
2948
      break;
18,083✔
2949
    }
2950
    case DB_OPTION_TABLE_SUFFIX: {
17,500✔
2951
      SValueNode* pNode = (SValueNode*)pVal;
17,500✔
2952
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
17,500✔
2953
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
17,500✔
2954
      } else {
2955
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
2956
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2957
      }
2958
      nodesDestroyNode((SNode*)pNode);
17,500✔
2959
      break;
17,500✔
2960
    }
2961
    case DB_OPTION_SS_CHUNKPAGES:
5,108✔
2962
      pDbOptions->ssChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5,108✔
2963
      break;
5,108✔
2964
    case DB_OPTION_SS_KEEPLOCAL: {
5,761✔
2965
      SToken* pToken = pVal;
5,761✔
2966
      if (TK_NK_INTEGER == pToken->type) {
5,761✔
2967
        pDbOptions->ssKeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
1,244✔
2968
      } else {
2969
        pDbOptions->ssKeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
4,517✔
2970
      }
2971
      break;
5,761✔
2972
    }
2973
    case DB_OPTION_SS_COMPACT:
6,055✔
2974
      pDbOptions->ssCompact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
6,055✔
2975
      break;
6,055✔
2976
    case DB_OPTION_KEEP_TIME_OFFSET:
11,949✔
2977
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
11,949✔
2978
        pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
6,985✔
2979
      } else {
2980
        pDbOptions->pKeepTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
4,964✔
2981
      }
2982
      break;
11,949✔
2983
    case DB_OPTION_ENCRYPT_ALGORITHM:
10,828✔
2984
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
10,828✔
2985
      if (strlen(pDbOptions->encryptAlgorithmStr) == 0) pDbOptions->encryptAlgorithm = -1;
10,828✔
2986
      break;
10,828✔
2987
    case DB_OPTION_DNODES:
28,303✔
2988
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
28,303✔
2989
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
769✔
2990
                 TSDB_DNODE_LIST_LEN);
2991
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
769✔
2992
      } else {
2993
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
27,534✔
2994
      }
2995
      break;
28,303✔
2996
    case DB_OPTION_COMPACT_INTERVAL:
33,710✔
2997
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
33,710✔
2998
        pDbOptions->compactInterval = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5,152✔
2999
      } else {
3000
        pDbOptions->pCompactIntervalNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
28,558✔
3001
      }
3002
      break;
33,710✔
3003
    case DB_OPTION_COMPACT_TIME_RANGE:
52,492✔
3004
      pDbOptions->pCompactTimeRangeList = pVal;
52,492✔
3005
      break;
52,492✔
3006
    case DB_OPTION_COMPACT_TIME_OFFSET:
30,973✔
3007
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
30,973✔
3008
        pDbOptions->compactTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
15,097✔
3009
      } else {
3010
        pDbOptions->pCompactTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
15,876✔
3011
      }
3012
      break;
30,973✔
3013
    case DB_OPTION_IS_AUDIT:
4,708✔
3014
      pDbOptions->isAudit = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
4,708✔
3015
      break;
4,708✔
3016
    case DB_OPTION_ALLOW_DROP:
×
3017
      pDbOptions->allowDrop = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
×
3018
      break;
×
3019
    case DB_OPTION_SECURE_DELETE:
6,740✔
3020
      pDbOptions->secureDelete = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
6,740✔
3021
      break;
6,740✔
3022
    default:
21,556✔
3023
      break;
21,556✔
3024
  }
3025
  return pOptions;
2,687,255✔
3026
_err:
×
3027
  nodesDestroyNode(pOptions);
×
3028
  return NULL;
×
3029
}
3030

3031
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
2,427,148✔
3032
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
2,427,148✔
3033
}
3034

3035
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
260,107✔
3036
  CHECK_PARSER_STATUS(pCxt);
260,107✔
3037
  switch (pAlterOption->type) {
260,107✔
3038
    case DB_OPTION_KEEP:
77,023✔
3039
    case DB_OPTION_RETENTIONS:
3040
    case DB_OPTION_COMPACT_TIME_RANGE:
3041
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
77,023✔
3042
    default:
183,084✔
3043
      break;
183,084✔
3044
  }
3045
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
183,084✔
3046
_err:
×
3047
  nodesDestroyNode(pOptions);
×
3048
  nodesDestroyList(pAlterOption->pList);
×
3049
  return NULL;
×
3050
}
3051

3052
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
1,602,272✔
3053
  CHECK_PARSER_STATUS(pCxt);
1,602,272✔
3054
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,601,503✔
3055
  SCreateDatabaseStmt* pStmt = NULL;
1,600,274✔
3056
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
1,600,274✔
3057
  CHECK_MAKE_NODE(pStmt);
1,600,274✔
3058
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,600,274✔
3059
  pStmt->ignoreExists = ignoreExists;
1,600,274✔
3060
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
1,600,274✔
3061
  return (SNode*)pStmt;
1,600,274✔
3062
_err:
1,998✔
3063
  nodesDestroyNode(pOptions);
1,998✔
3064
  return NULL;
1,998✔
3065
}
3066

3067
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName, bool force) {
1,330,499✔
3068
  CHECK_PARSER_STATUS(pCxt);
1,330,499✔
3069
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,330,499✔
3070
  SDropDatabaseStmt* pStmt = NULL;
1,330,499✔
3071
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
1,330,499✔
3072
  CHECK_MAKE_NODE(pStmt);
1,330,499✔
3073
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,330,499✔
3074
  pStmt->ignoreNotExists = ignoreNotExists;
1,330,499✔
3075
  pStmt->force = force;
1,330,499✔
3076
  return (SNode*)pStmt;
1,330,499✔
3077
_err:
×
3078
  return NULL;
×
3079
}
3080

3081
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
231,155✔
3082
  CHECK_PARSER_STATUS(pCxt);
231,155✔
3083
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
231,155✔
3084
  SAlterDatabaseStmt* pStmt = NULL;
231,155✔
3085
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
231,155✔
3086
  CHECK_MAKE_NODE(pStmt);
231,155✔
3087
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
231,155✔
3088
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
231,155✔
3089
  return (SNode*)pStmt;
231,155✔
3090
_err:
×
3091
  nodesDestroyNode(pOptions);
×
3092
  return NULL;
×
3093
}
3094

3095
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
1,938,167✔
3096
  CHECK_PARSER_STATUS(pCxt);
1,938,167✔
3097
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,938,167✔
3098
  SFlushDatabaseStmt* pStmt = NULL;
1,938,167✔
3099
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
1,938,167✔
3100
  CHECK_MAKE_NODE(pStmt);
1,938,154✔
3101
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,938,154✔
3102
  return (SNode*)pStmt;
1,938,154✔
3103
_err:
×
3104
  return NULL;
×
3105
}
3106

3107
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
10,717✔
3108
  CHECK_PARSER_STATUS(pCxt);
10,717✔
3109
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
10,717✔
3110
  STrimDatabaseStmt* pStmt = NULL;
10,717✔
3111
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
10,717✔
3112
  CHECK_MAKE_NODE(pStmt);
10,717✔
3113
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
10,717✔
3114
  pStmt->maxSpeed = maxSpeed;
10,717✔
3115
  return (SNode*)pStmt;
10,717✔
3116
_err:
×
3117
  return NULL;
×
3118
}
3119

3120
SNode* createTrimDbWalStmt(SAstCreateContext* pCxt, SToken* pDbName) {
2,264✔
3121
  CHECK_PARSER_STATUS(pCxt);
2,264✔
3122
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
2,264✔
3123
  STrimDbWalStmt* pStmt = NULL;
2,264✔
3124
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_WAL_STMT, (SNode**)&pStmt);
2,264✔
3125
  CHECK_MAKE_NODE(pStmt);
2,264✔
3126
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
2,264✔
3127
  return (SNode*)pStmt;
2,264✔
3128
_err:
×
3129
  return NULL;
×
3130
}
3131

3132
SNode* createSsMigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
3,159✔
3133
  CHECK_PARSER_STATUS(pCxt);
3,159✔
3134
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
3,159✔
3135
  SSsMigrateDatabaseStmt* pStmt = NULL;
3,159✔
3136
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SSMIGRATE_DATABASE_STMT, (SNode**)&pStmt);
3,159✔
3137
  CHECK_MAKE_NODE(pStmt);
3,159✔
3138
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
3,159✔
3139
  return (SNode*)pStmt;
3,159✔
3140
_err:
×
3141
  return NULL;
×
3142
}
3143

3144
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd, bool metaOnly,
34,448✔
3145
                         bool force) {
3146
  CHECK_PARSER_STATUS(pCxt);
34,448✔
3147
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
34,448✔
3148
  SCompactDatabaseStmt* pStmt = NULL;
34,448✔
3149
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
34,448✔
3150
  CHECK_MAKE_NODE(pStmt);
34,448✔
3151
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
34,448✔
3152
  pStmt->pStart = pStart;
34,448✔
3153
  pStmt->pEnd = pEnd;
34,448✔
3154
  pStmt->metaOnly = metaOnly;
34,448✔
3155
  pStmt->force = force;
34,448✔
3156
  return (SNode*)pStmt;
34,448✔
3157
_err:
×
3158
  nodesDestroyNode(pStart);
×
3159
  nodesDestroyNode(pEnd);
×
3160
  return NULL;
×
3161
}
3162

3163
SNode* createCreateMountStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pMountName, SToken* pDnodeId,
1,781✔
3164
                             SToken* pMountPath) {
3165
#ifdef USE_MOUNT
3166
  CHECK_PARSER_STATUS(pCxt);
1,781✔
3167
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
1,781✔
3168
  CHECK_NAME(checkMountPath(pCxt, pMountPath));
1,781✔
3169
  SCreateMountStmt* pStmt = NULL;
1,781✔
3170
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MOUNT_STMT, (SNode**)&pStmt);
1,781✔
3171
  CHECK_MAKE_NODE(pStmt);
1,781✔
3172
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
1,781✔
3173
  COPY_STRING_FORM_STR_TOKEN(pStmt->mountPath, pMountPath);
1,781✔
3174
  pStmt->ignoreExists = ignoreExists;
1,781✔
3175
  if (TK_NK_INTEGER == pDnodeId->type) {
1,781✔
3176
    pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1,781✔
3177
  } else {
3178
    goto _err;
×
3179
  }
3180
  return (SNode*)pStmt;
1,781✔
3181
_err:
×
3182
  nodesDestroyNode((SNode*)pStmt);
×
3183
  return NULL;
×
3184
#else
3185
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
3186
  return NULL;
3187
#endif
3188
}
3189

3190
SNode* createDropMountStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pMountName) {
596✔
3191
#ifdef USE_MOUNT
3192
  CHECK_PARSER_STATUS(pCxt);
596✔
3193
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
596✔
3194
  SDropMountStmt* pStmt = NULL;
596✔
3195
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_MOUNT_STMT, (SNode**)&pStmt);
596✔
3196
  CHECK_MAKE_NODE(pStmt);
596✔
3197
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
596✔
3198
  pStmt->ignoreNotExists = ignoreNotExists;
596✔
3199
  return (SNode*)pStmt;
596✔
3200
_err:
×
3201
  return NULL;
×
3202
#else
3203
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
3204
  return NULL;
3205
#endif
3206
}
3207

3208
SNode* createCompactVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
4,776✔
3209
                                SNode* pEnd, bool metaOnly, bool force) {
3210
  CHECK_PARSER_STATUS(pCxt);
4,776✔
3211
  if (NULL == pDbName) {
4,776✔
3212
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
796✔
3213
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
796✔
3214
    CHECK_PARSER_STATUS(pCxt);
796✔
3215
  }
3216
  SCompactVgroupsStmt* pStmt = NULL;
3,980✔
3217
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_VGROUPS_STMT, (SNode**)&pStmt);
3,980✔
3218
  CHECK_MAKE_NODE(pStmt);
3,980✔
3219
  pStmt->pDbName = pDbName;
3,980✔
3220
  pStmt->vgidList = vgidList;
3,980✔
3221
  pStmt->pStart = pStart;
3,980✔
3222
  pStmt->pEnd = pEnd;
3,980✔
3223
  pStmt->metaOnly = metaOnly;
3,980✔
3224
  pStmt->force = force;
3,980✔
3225
  return (SNode*)pStmt;
3,980✔
3226
_err:
796✔
3227
  nodesDestroyNode(pDbName);
796✔
3228
  nodesDestroyList(vgidList);
796✔
3229
  nodesDestroyNode(pStart);
796✔
3230
  nodesDestroyNode(pEnd);
796✔
3231
  return NULL;
796✔
3232
}
3233

3234
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
53,116,643✔
3235
  CHECK_PARSER_STATUS(pCxt);
53,116,643✔
3236
  STableOptions* pOptions = NULL;
53,117,488✔
3237
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
53,118,840✔
3238
  CHECK_MAKE_NODE(pOptions);
53,123,648✔
3239
  pOptions->maxDelay1 = -1;
53,123,648✔
3240
  pOptions->maxDelay2 = -1;
53,121,790✔
3241
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
53,116,482✔
3242
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
53,115,829✔
3243
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
53,115,643✔
3244
  pOptions->keep = -1;
53,117,932✔
3245
  pOptions->virtualStb = false;
53,107,043✔
3246
  pOptions->commentNull = true;  // mark null
53,101,559✔
3247
  pOptions->secureDelete = 0;
53,112,264✔
3248
  return (SNode*)pOptions;
53,115,959✔
3249
_err:
×
3250
  return NULL;
×
3251
}
3252

3253
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
74,302✔
3254
  CHECK_PARSER_STATUS(pCxt);
74,302✔
3255
  STableOptions* pOptions = NULL;
74,302✔
3256
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
74,302✔
3257
  CHECK_MAKE_NODE(pOptions);
74,302✔
3258
  pOptions->ttl = -1;
74,302✔
3259
  pOptions->commentNull = true;  // mark null
74,302✔
3260
  pOptions->keep = -1;
74,302✔
3261
  pOptions->secureDelete = -1;
74,302✔
3262
  return (SNode*)pOptions;
74,302✔
3263
_err:
×
3264
  return NULL;
×
3265
}
3266

3267
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
966,495✔
3268
  CHECK_PARSER_STATUS(pCxt);
966,495✔
3269
  switch (type) {
966,495✔
3270
    case TABLE_OPTION_COMMENT:
53,939✔
3271
      if (checkComment(pCxt, (SToken*)pVal, true)) {
53,939✔
3272
        ((STableOptions*)pOptions)->commentNull = false;
45,047✔
3273
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
45,047✔
3274
      }
3275
      break;
53,939✔
3276
    case TABLE_OPTION_MAXDELAY:
2,352✔
3277
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
2,352✔
3278
      break;
2,352✔
3279
    case TABLE_OPTION_WATERMARK:
2,352✔
3280
      ((STableOptions*)pOptions)->pWatermark = pVal;
2,352✔
3281
      break;
2,352✔
3282
    case TABLE_OPTION_ROLLUP:
3,920✔
3283
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
3,920✔
3284
      break;
3,920✔
3285
    case TABLE_OPTION_TTL: {
115,956✔
3286
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
115,956✔
3287
      if (ttl > INT32_MAX) {
115,956✔
3288
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
787✔
3289
      } else {
3290
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
3291
        ((STableOptions*)pOptions)->ttl = ttl;
115,169✔
3292
      }
3293
      break;
115,956✔
3294
    }
3295
    case TABLE_OPTION_SMA:
640,597✔
3296
      ((STableOptions*)pOptions)->pSma = pVal;
640,597✔
3297
      break;
640,597✔
3298
    case TABLE_OPTION_DELETE_MARK:
784✔
3299
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
784✔
3300
      break;
784✔
3301
    case TABLE_OPTION_KEEP:
77,718✔
3302
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
77,718✔
3303
        ((STableOptions*)pOptions)->keep = taosStr2Int32(((SToken*)pVal)->z, NULL, 10) * 1440;
12,447✔
3304
      } else {
3305
        ((STableOptions*)pOptions)->pKeepNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
65,271✔
3306
      }
3307
      break;
77,718✔
3308
    case TABLE_OPTION_VIRTUAL: {
63,029✔
3309
      int64_t virtualStb = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
63,029✔
3310
      if (virtualStb != 0 && virtualStb != 1) {
63,029✔
3311
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
3312
      } else {
3313
        ((STableOptions*)pOptions)->virtualStb = virtualStb;
63,029✔
3314
      }
3315
      break;
63,029✔
3316
    }
3317
    case TABLE_OPTION_SECURE_DELETE: {
5,848✔
3318
      int64_t secureDelete = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
5,848✔
3319
      if (secureDelete != 0 && secureDelete != 1) {
5,848✔
3320
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
3321
      } else {
3322
        ((STableOptions*)pOptions)->secureDelete = (int8_t)secureDelete;
5,848✔
3323
      }
3324
      break;
5,848✔
3325
    }
3326
    default:
×
3327
      break;
×
3328
  }
3329
  return pOptions;
966,495✔
3330
_err:
×
3331
  nodesDestroyNode(pOptions);
×
3332
  return NULL;
×
3333
}
3334

3335
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
441,155,475✔
3336
  CHECK_PARSER_STATUS(pCxt);
441,155,475✔
3337
  SColumnOptions* pOptions = NULL;
441,155,475✔
3338
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
441,155,475✔
3339
  CHECK_MAKE_NODE(pOptions);
441,155,475✔
3340
  pOptions->commentNull = true;
441,155,475✔
3341
  pOptions->bPrimaryKey = false;
441,155,475✔
3342
  pOptions->hasRef = false;
441,155,475✔
3343
  return (SNode*)pOptions;
441,155,475✔
3344
_err:
×
3345
  return NULL;
×
3346
}
3347

3348
EColumnOptionType getColumnOptionType(const char* optionType) {
2,286,422✔
3349
  if (0 == strcasecmp(optionType, "ENCODE")) {
2,286,422✔
3350
    return COLUMN_OPTION_ENCODE;
615,356✔
3351
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
1,671,066✔
3352
    return COLUMN_OPTION_COMPRESS;
840,039✔
3353
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
831,027✔
3354
    return COLUMN_OPTION_LEVEL;
828,771✔
3355
  }
3356
  return 0;
2,256✔
3357
}
3358

3359
SNode* setColumnReference(SAstCreateContext* pCxt, SNode* pOptions, SNode* pRef) {
96,445,640✔
3360
  CHECK_PARSER_STATUS(pCxt);
96,445,640✔
3361

3362
  ((SColumnOptions*)pOptions)->hasRef = true;
96,445,640✔
3363
  tstrncpy(((SColumnOptions*)pOptions)->refDb, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
96,445,640✔
3364
  tstrncpy(((SColumnOptions*)pOptions)->refTable, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
96,445,640✔
3365
  tstrncpy(((SColumnOptions*)pOptions)->refColumn, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
96,445,640✔
3366
  return pOptions;
96,445,640✔
3367
_err:
×
3368
  nodesDestroyNode(pOptions);
×
3369
  return NULL;
×
3370
}
3371

3372
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
252,608✔
3373
  CHECK_PARSER_STATUS(pCxt);
252,608✔
3374
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
252,608✔
3375
  return pOptions;
252,608✔
3376
_err:
×
3377
  nodesDestroyNode(pOptions);
×
3378
  return NULL;
×
3379
}
3380

3381
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
2,286,422✔
3382
  CHECK_PARSER_STATUS(pCxt);
2,286,422✔
3383
  char optionType[TSDB_CL_OPTION_LEN];
2,267,390✔
3384

3385
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
2,286,422✔
3386
  tstrncpy(optionType, pVal1->z, pVal1->n < TSDB_CL_OPTION_LEN ? pVal1->n + 1 : TSDB_CL_OPTION_LEN);
2,286,422✔
3387

3388
  if (0 == strlen(optionType)) {
2,286,422✔
3389
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3390
    return pOptions;
×
3391
  }
3392
  EColumnOptionType type = getColumnOptionType(optionType);
2,286,422✔
3393
  switch (type) {
2,286,422✔
3394
    case COLUMN_OPTION_ENCODE:
615,356✔
3395
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
615,356✔
3396
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
615,356✔
3397
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
615,356✔
3398
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
3399
      }
3400
      break;
615,356✔
3401
    case COLUMN_OPTION_COMPRESS:
840,039✔
3402
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
840,039✔
3403
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
840,039✔
3404
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
840,039✔
3405
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
3406
      }
3407
      break;
840,039✔
3408
    case COLUMN_OPTION_LEVEL:
828,771✔
3409
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
828,771✔
3410
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
828,771✔
3411
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
828,771✔
3412
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
3413
      }
3414
      break;
828,771✔
3415
    default:
2,256✔
3416
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2,256✔
3417
      break;
2,256✔
3418
  }
3419
  return pOptions;
2,286,422✔
3420
_err:
×
3421
  nodesDestroyNode(pOptions);
×
3422
  return NULL;
×
3423
}
3424

3425
SNode* createColumnRefNodeByNode(SAstCreateContext* pCxt, SToken* pColName, SNode* pRef) {
58,340,236✔
3426
  CHECK_PARSER_STATUS(pCxt);
58,340,236✔
3427
  CHECK_NAME(checkColumnName(pCxt, pColName));
58,340,236✔
3428

3429
  SColumnRefNode* pCol = NULL;
58,340,236✔
3430
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
58,340,236✔
3431
  CHECK_MAKE_NODE(pCol);
58,340,236✔
3432
  if (pColName) {
58,340,236✔
3433
    COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
58,340,236✔
3434
  }
3435
  tstrncpy(pCol->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
58,340,236✔
3436
  tstrncpy(pCol->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
58,340,236✔
3437
  tstrncpy(pCol->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
58,340,236✔
3438
  return (SNode*)pCol;
58,340,236✔
3439
_err:
×
3440
  return NULL;
×
3441
}
3442

3443
// Create a SColumnRefNode from db.table.col triplet tokens (for positional tag refs in vtags_literal)
3444
SNode* createColumnRefNodeFromTriplet(SAstCreateContext* pCxt, SToken* pDb, SToken* pTable, SToken* pCol) {
×
3445
  CHECK_PARSER_STATUS(pCxt);
×
3446
  CHECK_NAME(checkDbName(pCxt, pDb, true));
×
3447
  CHECK_NAME(checkTableName(pCxt, pTable));
×
3448
  CHECK_NAME(checkColumnName(pCxt, pCol));
×
3449

3450
  SColumnRefNode* pNode = NULL;
×
3451
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pNode);
×
3452
  CHECK_MAKE_NODE(pNode);
×
3453
  COPY_STRING_FORM_ID_TOKEN(pNode->refDbName, pDb);
×
3454
  COPY_STRING_FORM_ID_TOKEN(pNode->refTableName, pTable);
×
3455
  COPY_STRING_FORM_ID_TOKEN(pNode->refColName, pCol);
×
3456
  return (SNode*)pNode;
×
3457
_err:
×
3458
  return NULL;
×
3459
}
3460

3461
// Create a SColumnRefNode from table.col pair tokens (for positional tag refs in vtags_literal)
3462
SNode* createColumnRefNodeFromPair(SAstCreateContext* pCxt, SToken* pTable, SToken* pCol) {
×
3463
  CHECK_PARSER_STATUS(pCxt);
×
3464
  CHECK_NAME(checkTableName(pCxt, pTable));
×
3465
  CHECK_NAME(checkColumnName(pCxt, pCol));
×
3466

3467
  SColumnRefNode* pNode = NULL;
×
3468
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pNode);
×
3469
  CHECK_MAKE_NODE(pNode);
×
3470
  snprintf(pNode->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
×
3471
  COPY_STRING_FORM_ID_TOKEN(pNode->refTableName, pTable);
×
3472
  COPY_STRING_FORM_ID_TOKEN(pNode->refColName, pCol);
×
3473
  return (SNode*)pNode;
×
3474
_err:
×
3475
  return NULL;
×
3476
}
3477

3478
STokenTriplet* createTokenTriplet(SAstCreateContext* pCxt, SToken pName) {
155,670,887✔
3479
  CHECK_PARSER_STATUS(pCxt);
155,670,887✔
3480

3481
  STokenTriplet* pTokenTri = taosMemoryMalloc(sizeof(STokenTriplet));
155,670,887✔
3482
  CHECK_OUT_OF_MEM(pTokenTri);
155,670,887✔
3483
  pTokenTri->name[0] = pName;
155,670,887✔
3484
  pTokenTri->numOfName = 1;
155,670,887✔
3485

3486
  return pTokenTri;
155,670,887✔
3487
_err:
×
3488
  return NULL;
×
3489
}
3490

3491
STokenTriplet* setColumnName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri, SToken pName) {
156,285,967✔
3492
  CHECK_PARSER_STATUS(pCxt);
156,285,967✔
3493

3494
  if (pTokenTri->numOfName >= 3) {
156,285,967✔
3495
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3496
    goto _err;
×
3497
  }
3498

3499
  pTokenTri->name[pTokenTri->numOfName] = pName;
156,285,967✔
3500
  pTokenTri->numOfName++;
156,285,967✔
3501
  return pTokenTri;
156,285,967✔
3502
_err:
×
3503
  return NULL;
×
3504
}
3505

3506
SNode* createColumnRefNodeByName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri) {
155,670,887✔
3507
  SColumnRefNode* pCol = NULL;
155,670,887✔
3508
  CHECK_PARSER_STATUS(pCxt);
155,670,887✔
3509
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
155,670,887✔
3510
  CHECK_MAKE_NODE(pCol);
155,670,887✔
3511

3512
  switch (pTokenTri->numOfName) {
155,670,887✔
3513
    case 2: {
155,055,807✔
3514
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[0]));
155,055,807✔
3515
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[1]));
155,055,807✔
3516
      snprintf(pCol->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
155,055,807✔
3517
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[0]);
155,055,807✔
3518
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[1]);
155,055,807✔
3519
      break;
155,055,807✔
3520
    }
3521
    case 3: {
615,080✔
3522
      CHECK_NAME(checkDbName(pCxt, &pTokenTri->name[0], true));
615,080✔
3523
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[1]));
615,080✔
3524
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[2]));
615,080✔
3525
      COPY_STRING_FORM_ID_TOKEN(pCol->refDbName, &pTokenTri->name[0]);
615,080✔
3526
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[1]);
615,080✔
3527
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[2]);
615,080✔
3528
      break;
615,080✔
3529
    }
3530
    default: {
×
3531
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3532
      goto _err;
×
3533
    }
3534
  }
3535

3536
  taosMemFree(pTokenTri);
155,670,887✔
3537
  return (SNode*)pCol;
155,670,887✔
3538
_err:
×
3539
  taosMemFree(pTokenTri);
×
3540
  nodesDestroyNode((SNode*)pCol);
×
3541
  return NULL;
×
3542
}
3543

3544
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
439,903,459✔
3545
  CHECK_PARSER_STATUS(pCxt);
439,903,459✔
3546
  CHECK_NAME(checkColumnName(pCxt, pColName));
439,903,459✔
3547
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
439,900,857✔
3548
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
3,980✔
3549
    CHECK_PARSER_STATUS(pCxt);
3,980✔
3550
  }
3551
  SColumnDefNode* pCol = NULL;
439,896,877✔
3552
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
439,896,877✔
3553
  CHECK_MAKE_NODE(pCol);
439,896,877✔
3554
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
439,896,877✔
3555
  pCol->dataType = dataType;
439,896,877✔
3556
  pCol->pOptions = pNode;
439,896,877✔
3557
  pCol->sma = true;
439,896,877✔
3558
  return (SNode*)pCol;
439,896,877✔
3559
_err:
6,582✔
3560
  nodesDestroyNode(pNode);
6,582✔
3561
  return NULL;
6,582✔
3562
}
3563

3564
SDataType createDataType(uint8_t type) {
442,779,680✔
3565
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
442,779,680✔
3566
  return dt;
442,779,680✔
3567
}
3568

3569
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
115,785,032✔
3570
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
115,785,032✔
3571
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
115,785,032✔
3572
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
115,785,032✔
3573
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
115,785,032✔
3574
  return dt;
115,785,032✔
3575
}
3576

3577
SDataType createDecimalDataType(uint8_t type, const SToken* pPrecisionToken, const SToken* pScaleToken) {
253,000✔
3578
  SDataType dt = {0};
253,000✔
3579
  dt.precision = taosStr2UInt8(pPrecisionToken->z, NULL, 10);
253,000✔
3580
  dt.scale = pScaleToken ? taosStr2Int32(pScaleToken->z, NULL, 10) : 0;
253,000✔
3581
  dt.type = decimalTypeFromPrecision(dt.precision);
253,000✔
3582
  dt.bytes = tDataTypes[dt.type].bytes;
253,000✔
3583
  return dt;
253,000✔
3584
}
3585

3586
SNode* createCreateVTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols) {
226,497✔
3587
  SCreateVTableStmt* pStmt = NULL;
226,497✔
3588
  CHECK_PARSER_STATUS(pCxt);
226,497✔
3589
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
226,497✔
3590
  CHECK_MAKE_NODE(pStmt);
226,497✔
3591
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
226,497✔
3592
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->tableName));
226,497✔
3593
  pStmt->ignoreExists = ignoreExists;
226,497✔
3594
  pStmt->pCols = pCols;
226,497✔
3595
  nodesDestroyNode(pRealTable);
226,497✔
3596
  return (SNode*)pStmt;
226,497✔
3597
_err:
×
3598
  nodesDestroyNode(pRealTable);
×
3599
  nodesDestroyList(pCols);
×
3600
  return NULL;
×
3601
}
3602

3603
SNode* createCreateVSubTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable,
400,885✔
3604
                                 SNodeList* pSpecificColRefs, SNodeList* pColRefs, SNode* pUseRealTable,
3605
                                 SNodeList* pSpecificTags, SNodeList* pValsOfTags,
3606
                                 SNodeList* pSpecificTagRefs, SNodeList* pTagRefs) {
3607
  CHECK_PARSER_STATUS(pCxt);
400,885✔
3608
  SCreateVSubTableStmt* pStmt = NULL;
400,885✔
3609
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT, (SNode**)&pStmt);
400,885✔
3610
  CHECK_MAKE_NODE(pStmt);
400,885✔
3611

3612
  if (pTagRefs != NULL) {
400,885✔
3613
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3614
    goto _err;
×
3615
  }
3616
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
400,885✔
3617
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->tableName));
400,885✔
3618
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, sizeof(pStmt->useDbName));
400,885✔
3619
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, sizeof(pStmt->useTableName));
400,885✔
3620
  pStmt->ignoreExists = ignoreExists;
400,885✔
3621
  pStmt->pSpecificTags = pSpecificTags;
400,885✔
3622
  pStmt->pValsOfTags = pValsOfTags;
400,885✔
3623
  pStmt->pSpecificColRefs = pSpecificColRefs;
400,885✔
3624
  pStmt->pColRefs = pColRefs;
400,885✔
3625
  pStmt->pSpecificTagRefs = pSpecificTagRefs;
400,885✔
3626
  pStmt->pTagRefs = pTagRefs;
400,885✔
3627
  nodesDestroyNode(pRealTable);
400,885✔
3628
  nodesDestroyNode(pUseRealTable);
400,885✔
3629
  return (SNode*)pStmt;
400,885✔
3630
_err:
×
3631
  nodesDestroyNode(pRealTable);
×
3632
  nodesDestroyNode(pUseRealTable);
×
3633
  nodesDestroyList(pSpecificTags);
×
3634
  nodesDestroyList(pValsOfTags);
×
3635
  nodesDestroyList(pSpecificColRefs);
×
3636
  nodesDestroyList(pColRefs);
×
3637
  nodesDestroyList(pSpecificTagRefs);
×
3638
  nodesDestroyList(pTagRefs);
×
3639
  return NULL;
×
3640
}
3641

3642
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
9,334,443✔
3643
                             SNodeList* pTags, SNode* pOptions) {
3644
  CHECK_PARSER_STATUS(pCxt);
9,334,443✔
3645
  SCreateTableStmt* pStmt = NULL;
9,333,048✔
3646
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
9,333,048✔
3647
  CHECK_MAKE_NODE(pStmt);
9,333,048✔
3648
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
9,333,048✔
3649
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
9,333,048✔
3650
  pStmt->ignoreExists = ignoreExists;
9,333,048✔
3651
  pStmt->pCols = pCols;
9,333,048✔
3652
  pStmt->pTags = pTags;
9,333,048✔
3653
  pStmt->pOptions = (STableOptions*)pOptions;
9,333,048✔
3654
  nodesDestroyNode(pRealTable);
9,333,048✔
3655
  return (SNode*)pStmt;
9,333,048✔
3656
_err:
1,395✔
3657
  nodesDestroyNode(pRealTable);
1,395✔
3658
  nodesDestroyList(pCols);
1,395✔
3659
  nodesDestroyList(pTags);
1,395✔
3660
  nodesDestroyNode(pOptions);
1,395✔
3661
  return NULL;
1,395✔
3662
}
3663

3664
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
43,774,298✔
3665
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
3666
  CHECK_PARSER_STATUS(pCxt);
43,774,298✔
3667
  SCreateSubTableClause* pStmt = NULL;
43,765,657✔
3668
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
43,772,723✔
3669
  CHECK_MAKE_NODE(pStmt);
43,777,859✔
3670
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
43,777,859✔
3671
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
43,769,596✔
3672
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
43,769,418✔
3673
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
43,752,750✔
3674
  pStmt->ignoreExists = ignoreExists;
43,771,157✔
3675
  pStmt->pSpecificTags = pSpecificTags;
43,769,465✔
3676
  pStmt->pValsOfTags = pValsOfTags;
43,755,467✔
3677
  pStmt->pOptions = (STableOptions*)pOptions;
43,760,863✔
3678
  nodesDestroyNode(pRealTable);
43,768,178✔
3679
  nodesDestroyNode(pUseRealTable);
43,767,602✔
3680
  return (SNode*)pStmt;
43,774,775✔
3681
_err:
×
3682
  nodesDestroyNode(pRealTable);
×
3683
  nodesDestroyNode(pOptions);
×
3684
  nodesDestroyNode(pUseRealTable);
×
3685
  nodesDestroyList(pSpecificTags);
×
3686
  nodesDestroyList(pValsOfTags);
×
3687
  return NULL;
×
3688
}
3689

3690
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
1,434✔
3691
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
3692
  CHECK_PARSER_STATUS(pCxt);
1,434✔
3693
  SCreateSubTableFromFileClause* pStmt = NULL;
1,434✔
3694
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
1,434✔
3695
  CHECK_MAKE_NODE(pStmt);
1,434✔
3696
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
1,434✔
3697
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
1,434✔
3698
  pStmt->ignoreExists = ignoreExists;
1,434✔
3699
  pStmt->pSpecificTags = pSpecificTags;
1,434✔
3700
  if (TK_NK_STRING == pFilePath->type) {
1,434✔
3701
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
1,434✔
3702
  } else {
3703
    tstrncpy(pStmt->filePath, pFilePath->z,
×
3704
             pFilePath->n < sizeof(pStmt->filePath) ? pFilePath->n + 1 : sizeof(pStmt->filePath));
3705
  }
3706

3707
  nodesDestroyNode(pUseRealTable);
1,434✔
3708
  return (SNode*)pStmt;
1,434✔
3709
_err:
×
3710
  nodesDestroyNode(pUseRealTable);
×
3711
  nodesDestroyList(pSpecificTags);
×
3712
  return NULL;
×
3713
}
3714

3715
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
36,594,152✔
3716
  CHECK_PARSER_STATUS(pCxt);
36,594,152✔
3717
  SCreateMultiTablesStmt* pStmt = NULL;
36,586,106✔
3718
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
36,594,288✔
3719
  CHECK_MAKE_NODE(pStmt);
36,595,003✔
3720
  pStmt->pSubTables = pSubTables;
36,595,003✔
3721
  return (SNode*)pStmt;
36,597,966✔
3722
_err:
×
3723
  nodesDestroyList(pSubTables);
×
3724
  return NULL;
×
3725
}
3726

3727
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
2,531,542✔
3728
  CHECK_PARSER_STATUS(pCxt);
2,531,542✔
3729
  SDropTableClause* pStmt = NULL;
2,531,330✔
3730
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
2,531,330✔
3731
  CHECK_MAKE_NODE(pStmt);
2,531,330✔
3732
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2,531,330✔
3733
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2,531,330✔
3734
  pStmt->ignoreNotExists = ignoreNotExists;
2,531,330✔
3735
  nodesDestroyNode(pRealTable);
2,531,330✔
3736
  return (SNode*)pStmt;
2,531,330✔
3737
_err:
212✔
3738
  nodesDestroyNode(pRealTable);
212✔
3739
  return NULL;
212✔
3740
}
3741

3742
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
2,413,826✔
3743
  CHECK_PARSER_STATUS(pCxt);
2,413,826✔
3744
  SDropTableStmt* pStmt = NULL;
2,413,614✔
3745
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
2,413,614✔
3746
  CHECK_MAKE_NODE(pStmt);
2,413,614✔
3747
  pStmt->pTables = pTables;
2,413,614✔
3748
  pStmt->withOpt = withOpt;
2,413,614✔
3749
  return (SNode*)pStmt;
2,413,614✔
3750
_err:
212✔
3751
  nodesDestroyList(pTables);
212✔
3752
  return NULL;
212✔
3753
}
3754

3755
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
94,589✔
3756
  CHECK_PARSER_STATUS(pCxt);
94,589✔
3757
  SDropSuperTableStmt* pStmt = NULL;
94,589✔
3758
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
94,589✔
3759
  CHECK_MAKE_NODE(pStmt);
94,589✔
3760
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
94,589✔
3761
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
94,589✔
3762
  pStmt->ignoreNotExists = ignoreNotExists;
94,589✔
3763
  pStmt->withOpt = withOpt;
94,589✔
3764
  nodesDestroyNode(pRealTable);
94,589✔
3765
  return (SNode*)pStmt;
94,589✔
3766
_err:
×
3767
  nodesDestroyNode(pRealTable);
×
3768
  return NULL;
×
3769
}
3770

3771
SNode* createDropVirtualTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
76,703✔
3772
  CHECK_PARSER_STATUS(pCxt);
76,703✔
3773
  SDropVirtualTableStmt* pStmt = NULL;
76,703✔
3774
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
76,703✔
3775
  CHECK_MAKE_NODE(pStmt);
76,703✔
3776
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
76,703✔
3777
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
76,703✔
3778
  pStmt->ignoreNotExists = ignoreNotExists;
76,703✔
3779
  pStmt->withOpt = withOpt;
76,703✔
3780
  nodesDestroyNode(pRealTable);
76,703✔
3781
  return (SNode*)pStmt;
76,703✔
3782
_err:
×
3783
  nodesDestroyNode(pRealTable);
×
3784
  return NULL;
×
3785
}
3786

3787
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
11,829,962✔
3788
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
11,829,962✔
3789
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
11,829,962✔
3790
  nodesDestroyNode(pRealTable);
11,829,962✔
3791
  return (SNode*)pStmt;
11,829,962✔
3792
}
3793

3794
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
74,302✔
3795
  CHECK_PARSER_STATUS(pCxt);
74,302✔
3796
  SAlterTableStmt* pStmt = NULL;
66,018✔
3797
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
66,018✔
3798
  CHECK_MAKE_NODE(pStmt);
66,018✔
3799
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
66,018✔
3800
  pStmt->pOptions = (STableOptions*)pOptions;
66,018✔
3801
  return createAlterTableStmtFinalize(pRealTable, pStmt);
66,018✔
3802
_err:
8,284✔
3803
  nodesDestroyNode(pRealTable);
8,284✔
3804
  nodesDestroyNode(pOptions);
8,284✔
3805
  return NULL;
8,284✔
3806
}
3807

3808
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
2,799,591✔
3809
                                    SDataType dataType) {
3810
  CHECK_PARSER_STATUS(pCxt);
2,799,591✔
3811
  CHECK_NAME(checkColumnName(pCxt, pColName));
2,799,591✔
3812
  SAlterTableStmt* pStmt = NULL;
2,799,379✔
3813
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
2,799,379✔
3814
  CHECK_MAKE_NODE(pStmt);
2,799,379✔
3815
  pStmt->alterType = alterType;
2,799,379✔
3816
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
2,799,379✔
3817
  pStmt->dataType = dataType;
2,799,379✔
3818
  return createAlterTableStmtFinalize(pRealTable, pStmt);
2,799,379✔
3819
_err:
212✔
3820
  nodesDestroyNode(pRealTable);
212✔
3821
  return NULL;
212✔
3822
}
3823

3824
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
6,591,895✔
3825
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
3826
  SAlterTableStmt* pStmt = NULL;
6,591,895✔
3827
  CHECK_PARSER_STATUS(pCxt);
6,591,895✔
3828
  CHECK_NAME(checkColumnName(pCxt, pColName));
6,589,639✔
3829
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
6,589,427✔
3830
  CHECK_MAKE_NODE(pStmt);
6,589,427✔
3831
  pStmt->alterType = alterType;
6,589,427✔
3832
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
6,589,427✔
3833
  pStmt->dataType = dataType;
6,589,427✔
3834
  pStmt->pColOptions = (SColumnOptions*)pOptions;
6,589,427✔
3835

3836
  if (pOptions != NULL) {
6,589,427✔
3837
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
6,589,427✔
3838
    if (pOption->hasRef) {
6,589,427✔
3839
      if (!pOption->commentNull || pOption->bPrimaryKey || 0 != strcmp(pOption->compress, "") ||
37,800✔
3840
          0 != strcmp(pOption->encode, "") || 0 != strcmp(pOption->compressLevel, "")) {
37,800✔
3841
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
×
3842
      }
3843
      pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF;
37,800✔
3844
      tstrncpy(pStmt->refDbName, pOption->refDb, TSDB_DB_NAME_LEN);
37,800✔
3845
      tstrncpy(pStmt->refTableName, pOption->refTable, TSDB_TABLE_NAME_LEN);
37,800✔
3846
      tstrncpy(pStmt->refColName, pOption->refColumn, TSDB_COL_NAME_LEN);
37,800✔
3847
      CHECK_PARSER_STATUS(pCxt);
37,800✔
3848
    } else if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
6,551,627✔
3849
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
6,548,783✔
3850
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
283,842✔
3851
      } else {
3852
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
3853
        //                                         "not support alter column with option except compress");
3854
        // return NULL;
3855
      }
3856
    } else {
3857
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
2,844✔
3858
                                              "not support alter column with option except compress");
3859
      CHECK_PARSER_STATUS(pCxt);
2,844✔
3860
    }
3861
  }
3862
  return createAlterTableStmtFinalize(pRealTable, pStmt);
6,586,583✔
3863
_err:
5,312✔
3864
  nodesDestroyNode(pOptions);
5,312✔
3865
  nodesDestroyNode((SNode*)pStmt);
5,312✔
3866
  nodesDestroyNode(pRealTable);
5,312✔
3867
  return NULL;
5,312✔
3868
}
3869

3870
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
116,128✔
3871
                                           SToken* pColName, SNode* pOptions) {
3872
  CHECK_PARSER_STATUS(pCxt);
116,128✔
3873
  CHECK_NAME(checkColumnName(pCxt, pColName));
116,128✔
3874
  SAlterTableStmt* pStmt = NULL;
116,128✔
3875
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
116,128✔
3876
  CHECK_MAKE_NODE(pStmt);
116,128✔
3877
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
116,128✔
3878
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
116,128✔
3879
  pStmt->pColOptions = (SColumnOptions*)pOptions;
116,128✔
3880
  return createAlterTableStmtFinalize(pRealTable, pStmt);
116,128✔
3881
_err:
×
3882
  nodesDestroyNode(pOptions);
×
3883
  nodesDestroyNode(pRealTable);
×
3884
  return NULL;
×
3885
}
3886

3887
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
1,838,522✔
3888
  CHECK_PARSER_STATUS(pCxt);
1,838,522✔
3889
  CHECK_NAME(checkColumnName(pCxt, pColName));
1,838,522✔
3890
  SAlterTableStmt* pStmt = NULL;
1,838,098✔
3891
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
1,838,098✔
3892
  CHECK_MAKE_NODE(pStmt);
1,838,098✔
3893
  pStmt->alterType = alterType;
1,838,098✔
3894
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1,838,098✔
3895
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1,838,098✔
3896
_err:
424✔
3897
  nodesDestroyNode(pRealTable);
424✔
3898
  return NULL;
424✔
3899
}
3900

3901
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
202,992✔
3902
                                 SToken* pNewColName) {
3903
  CHECK_PARSER_STATUS(pCxt);
202,992✔
3904
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
202,992✔
3905
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
202,992✔
3906
  SAlterTableStmt* pStmt = NULL;
202,101✔
3907
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
202,101✔
3908
  CHECK_MAKE_NODE(pStmt);
202,101✔
3909
  pStmt->alterType = alterType;
202,101✔
3910
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
202,101✔
3911
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
202,101✔
3912
  return createAlterTableStmtFinalize(pRealTable, pStmt);
202,101✔
3913
_err:
891✔
3914
  nodesDestroyNode(pRealTable);
891✔
3915
  return NULL;
891✔
3916
}
3917

3918
SNode* createAlterTableAlterColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
120,033✔
3919
                                   SNode* pRef) {
3920
  CHECK_PARSER_STATUS(pCxt);
120,033✔
3921
  CHECK_NAME(checkColumnName(pCxt, pColName));
120,033✔
3922
  SAlterTableStmt* pStmt = NULL;
120,033✔
3923
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
120,033✔
3924
  CHECK_MAKE_NODE(pStmt);
120,033✔
3925
  pStmt->alterType = alterType;
120,033✔
3926
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
120,033✔
3927
  tstrncpy(pStmt->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
120,033✔
3928
  tstrncpy(pStmt->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
120,033✔
3929
  tstrncpy(pStmt->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
120,033✔
3930
  return createAlterTableStmtFinalize(pRealTable, pStmt);
120,033✔
3931
_err:
×
3932
  nodesDestroyNode(pRealTable);
×
3933
  return NULL;
×
3934
}
3935

3936
SNode* createAlterTableRemoveColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
71,104✔
3937
                                    const SToken* pLiteral) {
3938
  CHECK_PARSER_STATUS(pCxt);
71,104✔
3939
  CHECK_NAME(checkColumnName(pCxt, pColName));
71,104✔
3940
  SAlterTableStmt* pStmt = NULL;
71,104✔
3941
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
71,104✔
3942
  CHECK_MAKE_NODE(pStmt);
71,104✔
3943
  pStmt->alterType = alterType;
71,104✔
3944
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
71,104✔
3945
  return createAlterTableStmtFinalize(pRealTable, pStmt);
71,104✔
3946
_err:
×
3947
  nodesDestroyNode(pRealTable);
×
3948
  return NULL;
×
3949
}
3950

3951

3952

3953
SNode* createAlterTagValueNode(SAstCreateContext* pCxt, SToken* pTagName, SNode* pVal) {
8,728,907✔
3954
  CHECK_PARSER_STATUS(pCxt);
8,728,907✔
3955
  CHECK_NAME(checkColumnName(pCxt, pTagName));
8,728,907✔
3956
  SUpdateTagValueNode* pNode = NULL;
8,728,907✔
3957
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_TAG_VALUE, (SNode**)&pNode);
8,728,907✔
3958
  CHECK_MAKE_NODE(pNode);
8,728,907✔
3959
  COPY_STRING_FORM_ID_TOKEN(pNode->tagName, pTagName);
8,728,907✔
3960
  pNode->pVal = (SValueNode*)pVal;
8,728,907✔
3961
  return (SNode*)pNode;
8,728,907✔
3962

3963
_err:
×
3964
  nodesDestroyNode((SNode*)pNode);
×
3965
  return NULL;
×
3966
}
3967

3968

3969
// NOTE: this function only supports REGEXP_REPLACE for now, it should be extended
3970
// for full expression support in the future, and the prototype needs to be changed
3971
// accordingly.
3972
SNode* createAlterTagValueNodeWithExpression(SAstCreateContext* pCxt, SToken* pTagName, const SToken* pattern, const SToken* replacement) {
13,847✔
3973
  CHECK_PARSER_STATUS(pCxt);
13,847✔
3974
  CHECK_NAME(checkColumnName(pCxt, pTagName));
13,847✔
3975
  SUpdateTagValueNode* pNode = NULL;
13,847✔
3976
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_TAG_VALUE, (SNode**)&pNode);
13,847✔
3977
  CHECK_MAKE_NODE(pNode);
13,847✔
3978
  COPY_STRING_FORM_ID_TOKEN(pNode->tagName, pTagName);
13,847✔
3979

3980
  pNode->regexp = taosMemoryMalloc(pattern->n + 1);
13,847✔
3981
  if (pNode->regexp == NULL) {
13,847✔
3982
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
3983
    goto _err;
×
3984
  }
3985
  (void)trimString(pattern->z, pattern->n, pNode->regexp, pattern->n);
13,847✔
3986

3987
  int32_t code = checkRegexPattern(pNode->regexp);
13,847✔
3988
  if (code != 0) {
13,847✔
3989
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "'%s' is not a valid regular expression", pNode->regexp);
1,347✔
3990
    goto _err;
1,347✔
3991
  }
3992

3993
  pNode->replacement = taosMemoryMalloc(replacement->n + 1);
12,500✔
3994
  if (pNode->replacement == NULL) {
12,500✔
3995
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
3996
    goto _err;
×
3997
  }
3998
  (void)trimString(replacement->z, replacement->n, pNode->replacement, replacement->n);
12,500✔
3999

4000
  return (SNode*)pNode;
12,500✔
4001

4002
_err:
1,347✔
4003
  nodesDestroyNode((SNode*)pNode);
1,347✔
4004
  return NULL;
1,347✔
4005
}
4006

4007

4008

4009
SNode* createAlterTableUpdateTagValClause(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pTagList) {
8,625,903✔
4010
  CHECK_PARSER_STATUS(pCxt);
8,625,903✔
4011
  SAlterTableUpdateTagValClause* pClause = NULL;
8,625,903✔
4012
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_UPDATE_TAG_VAL_CLAUSE, (SNode**)&pClause);
8,625,903✔
4013

4014
  CHECK_MAKE_NODE(pClause);
8,625,903✔
4015
  pClause->pTagList = pTagList;
8,625,903✔
4016

4017
  tstrncpy(pClause->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pClause->dbName));
8,625,903✔
4018
  tstrncpy(pClause->tableName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pClause->tableName));
8,625,903✔
4019
  nodesDestroyNode(pRealTable);
8,625,903✔
4020

4021
  return (SNode*)pClause;
8,625,903✔
4022

4023
_err:
×
4024
  nodesDestroyNode((SNode*)pClause);
×
4025
  return NULL;
×
4026
}
4027

4028

4029

4030
SNode* createAlterMultiTableUpdateTagValStmt(SAstCreateContext* pCxt, SNodeList* pTableList) {
8,585,788✔
4031
  CHECK_PARSER_STATUS(pCxt);
8,585,788✔
4032
  SAlterTableStmt* pStmt = NULL;
8,585,788✔
4033
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
8,585,788✔
4034

4035
  CHECK_MAKE_NODE(pStmt);
8,585,788✔
4036
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TABLE_TAG_VAL;
8,585,788✔
4037
  pStmt->pList = pTableList;
8,585,788✔
4038
  return (SNode*)pStmt;
8,585,788✔
4039

4040
_err:
×
4041
  nodesDestroyNode((SNode*)pStmt);
×
4042
  return NULL;
×
4043
}
4044

4045

4046

4047
SNode* createAlterChildTableUpdateTagValStmt(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pTagList, SNode* pWhere) {
30,518✔
4048
  CHECK_PARSER_STATUS(pCxt);
30,518✔
4049
  SAlterTableStmt* pStmt = NULL;
30,518✔
4050
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
30,518✔
4051

4052
  CHECK_MAKE_NODE(pStmt);
30,518✔
4053
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_CHILD_TABLE_TAG_VAL;
30,518✔
4054
  pStmt->pList = pTagList;
30,518✔
4055
  pStmt->pWhere = pWhere;
30,518✔
4056
  return createAlterTableStmtFinalize(pRealTable, pStmt);
30,518✔
4057

4058
_err:
×
4059
  nodesDestroyNode((SNode*)pStmt);
×
4060
  return NULL;
×
4061
}
4062

4063

4064

4065
SNode* setAlterSuperTableType(SNode* pStmt) {
550,152✔
4066
  if (!pStmt) return NULL;
550,152✔
4067
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
549,516✔
4068
  return pStmt;
549,516✔
4069
}
4070

4071
SNode* setAlterVirtualTableType(SNode* pStmt) {
434,589✔
4072
  if (!pStmt) return NULL;
434,589✔
4073
  setNodeType(pStmt, QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT);
434,589✔
4074
  return pStmt;
434,589✔
4075
}
4076

4077
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
95,189,691✔
4078
  CHECK_PARSER_STATUS(pCxt);
95,189,691✔
4079
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
95,190,585✔
4080
  SUseDatabaseStmt* pStmt = NULL;
95,193,446✔
4081
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
95,193,446✔
4082
  CHECK_MAKE_NODE(pStmt);
95,193,323✔
4083
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
95,193,323✔
4084
  return (SNode*)pStmt;
95,193,323✔
4085
_err:
×
4086
  return NULL;
×
4087
}
4088

4089
static bool needDbShowStmt(ENodeType type) {
1,692,686✔
4090
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
4091
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
4092
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
4093
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type ||
4094
         QUERY_NODE_SHOW_USAGE_STMT == type || QUERY_NODE_SHOW_VTABLES_STMT == type ||
4095
         QUERY_NODE_SHOW_STREAMS_STMT == type || QUERY_NODE_SHOW_VALIDATE_VTABLE_STMT;
1,692,686✔
4096
}
4097

4098
SNode* createShowStmtWithLike(SAstCreateContext* pCxt, ENodeType type, SNode* pLikePattern) {
26,539✔
4099
  CHECK_PARSER_STATUS(pCxt);
26,539✔
4100
  SShowStmt* pStmt = NULL;
26,539✔
4101
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
26,539✔
4102
  CHECK_MAKE_NODE(pStmt);
26,539✔
4103
  pStmt->withFull = false;
26,539✔
4104
  pStmt->pTbName = pLikePattern;
26,539✔
4105
  if (pLikePattern) {
26,539✔
4106
    pStmt->tableCondType = OP_TYPE_LIKE;
5,010✔
4107
  }
4108
  return (SNode*)pStmt;
26,539✔
4109
_err:
×
4110
  nodesDestroyNode(pLikePattern);
×
4111
  return NULL;
×
4112
}
4113

4114
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
1,573,849✔
4115
  CHECK_PARSER_STATUS(pCxt);
1,573,849✔
4116
  SShowStmt* pStmt = NULL;
1,573,849✔
4117
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,573,849✔
4118
  CHECK_MAKE_NODE(pStmt);
1,573,849✔
4119
  pStmt->withFull = false;
1,573,849✔
4120
  return (SNode*)pStmt;
1,573,849✔
4121
_err:
×
4122
  return NULL;
×
4123
}
4124

4125
SNode* createShowXnodeStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pWhere) {
18,093✔
4126
  CHECK_PARSER_STATUS(pCxt);
18,093✔
4127
  SShowStmt* pStmt = NULL;
18,093✔
4128
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
18,093✔
4129
  CHECK_MAKE_NODE(pStmt);
18,093✔
4130
  pStmt->pWhere = pWhere;
18,093✔
4131
  return (SNode*)pStmt;
18,093✔
4132
_err:
×
4133
  return NULL;
×
4134
}
4135

4136
SNode* createShowXNodeResourcesWhereStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
15,860✔
4137
  CHECK_PARSER_STATUS(pCxt);
15,860✔
4138
  SShowStmt* pStmt = NULL;
15,860✔
4139
  ENodeType  type;
4140
  switch (resourceType) {
15,860✔
4141
    case XNODE_TASK:
6,815✔
4142
      type = QUERY_NODE_SHOW_XNODE_TASKS_STMT;
6,815✔
4143
      break;
6,815✔
4144
    case XNODE_JOB:
4,981✔
4145
      type = QUERY_NODE_SHOW_XNODE_JOBS_STMT;
4,981✔
4146
      break;
4,981✔
4147
    case XNODE_AGENT:
4,064✔
4148
      type = QUERY_NODE_SHOW_XNODE_AGENTS_STMT;
4,064✔
4149
      break;
4,064✔
4150
    default:
×
4151
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4152
                                              "Xnode not support show xnode resource type");
4153
      goto _err;
×
4154
  }
4155

4156
  pStmt = (SShowStmt*)createShowXnodeStmtWithCond(pCxt, type, pWhere);
15,860✔
4157
  CHECK_MAKE_NODE(pStmt);
15,860✔
4158

4159
  return (SNode*)pStmt;
15,860✔
4160
_err:
×
4161
  return NULL;
×
4162
}
4163

4164
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
14,277✔
4165
  CHECK_PARSER_STATUS(pCxt);
14,277✔
4166
  SShowStmt* pStmt = NULL;
14,277✔
4167
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
14,277✔
4168
  CHECK_MAKE_NODE(pStmt);
14,277✔
4169
  pStmt->withFull = true;
14,277✔
4170
  return (SNode*)pStmt;
14,277✔
4171
_err:
×
4172
  return NULL;
×
4173
}
4174

4175
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
359,738✔
4176
  CHECK_PARSER_STATUS(pCxt);
359,738✔
4177
  SShowCompactsStmt* pStmt = NULL;
359,738✔
4178
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
359,738✔
4179
  CHECK_MAKE_NODE(pStmt);
359,738✔
4180
  return (SNode*)pStmt;
359,738✔
4181
_err:
×
4182
  return NULL;
×
4183
}
4184

4185
SNode* createShowSsMigratesStmt(SAstCreateContext* pCxt, ENodeType type) {
92,280✔
4186
  CHECK_PARSER_STATUS(pCxt);
92,280✔
4187
  SShowSsMigratesStmt* pStmt = NULL;
92,280✔
4188
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
92,280✔
4189
  CHECK_MAKE_NODE(pStmt);
92,280✔
4190
  return (SNode*)pStmt;
92,280✔
4191
_err:
×
4192
  return NULL;
×
4193
}
4194

4195
SNode* createShowTokensStmt(SAstCreateContext* pCxt, ENodeType type) {
1,868✔
4196
  CHECK_PARSER_STATUS(pCxt);
1,868✔
4197
  SShowTokensStmt* pStmt = NULL;
1,868✔
4198
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,868✔
4199
  CHECK_MAKE_NODE(pStmt);
1,868✔
4200
  return (SNode*)pStmt;
1,868✔
4201
_err:
×
4202
  return NULL;
×
4203
}
4204

4205
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
754,770✔
4206
  if (pStmt == NULL) {
754,770✔
4207
    return NULL;
×
4208
  }
4209
  SShowStmt* pShow = (SShowStmt*)pStmt;
754,770✔
4210
  pShow->showKind = showKind;
754,770✔
4211
  return pStmt;
754,770✔
4212
}
4213

4214
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
1,574,727✔
4215
                              EOperatorType tableCondType) {
4216
  CHECK_PARSER_STATUS(pCxt);
1,574,727✔
4217
  if (needDbShowStmt(type) && NULL == pDbName) {
1,574,727✔
4218
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
1,107✔
4219
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1,107✔
4220
    CHECK_PARSER_STATUS(pCxt);
1,107✔
4221
  }
4222
  SShowStmt* pStmt = NULL;
1,573,620✔
4223
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,573,620✔
4224
  CHECK_MAKE_NODE(pStmt);
1,573,620✔
4225
  pStmt->pDbName = pDbName;
1,573,620✔
4226
  pStmt->pTbName = pTbName;
1,573,620✔
4227
  pStmt->tableCondType = tableCondType;
1,573,620✔
4228
  return (SNode*)pStmt;
1,573,620✔
4229
_err:
1,107✔
4230
  nodesDestroyNode(pDbName);
1,107✔
4231
  nodesDestroyNode(pTbName);
1,107✔
4232
  return NULL;
1,107✔
4233
}
4234

4235
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
261,651✔
4236
                            EOperatorType tableCondType) {
4237
  CHECK_PARSER_STATUS(pCxt);
261,651✔
4238
  SNode* pDbName = NULL;
261,651✔
4239
  if (option.dbName.type == TK_NK_NIL) {
261,651✔
4240
    pDbName = createDefaultDatabaseCondValue(pCxt);
91,183✔
4241
  } else {
4242
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
170,468✔
4243
  }
4244

4245
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
261,651✔
4246
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4247
    return NULL;
×
4248
  }
4249

4250
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
261,651✔
4251
  CHECK_PARSER_STATUS(pCxt);
261,651✔
4252
  (void)setShowKind(pCxt, pStmt, option.kind);
260,559✔
4253
  return pStmt;
260,559✔
4254
_err:
1,092✔
4255
  nodesDestroyNode(pTbName);
1,092✔
4256
  return NULL;
1,092✔
4257
}
4258

4259
SNode* createShowVTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
62,727✔
4260
                             EOperatorType tableCondType) {
4261
  CHECK_PARSER_STATUS(pCxt);
62,727✔
4262
  SNode* pDbName = NULL;
62,727✔
4263
  if (option.dbName.type == TK_NK_NIL) {
62,727✔
4264
    pDbName = createDefaultDatabaseCondValue(pCxt);
4,029✔
4265
  } else {
4266
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
58,698✔
4267
  }
4268

4269
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
62,727✔
4270
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4271
    return NULL;
×
4272
  }
4273

4274
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_VTABLES_STMT, pDbName, pTbName, tableCondType);
62,727✔
4275
  CHECK_PARSER_STATUS(pCxt);
62,727✔
4276
  (void)setShowKind(pCxt, pStmt, option.kind);
62,727✔
4277
  return pStmt;
62,727✔
4278
_err:
×
4279
  nodesDestroyNode(pTbName);
×
4280
  return NULL;
×
4281
}
4282

4283
SNode* createShowSTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
237,811✔
4284
                             EOperatorType tableCondType) {
4285
  CHECK_PARSER_STATUS(pCxt);
237,811✔
4286
  SNode* pDbName = NULL;
237,811✔
4287
  if (option.dbName.type == TK_NK_NIL) {
237,811✔
4288
    pDbName = createDefaultDatabaseCondValue(pCxt);
67,124✔
4289
  } else {
4290
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
170,687✔
4291
  }
4292

4293
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_VIRTUAL &&
237,811✔
4294
      option.kind != SHOW_KIND_ALL) {
236,495✔
4295
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4296
    return NULL;
×
4297
  }
4298

4299
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_STABLES_STMT, pDbName, pTbName, tableCondType);
237,811✔
4300
  CHECK_PARSER_STATUS(pCxt);
237,811✔
4301
  (void)setShowKind(pCxt, pStmt, option.kind);
237,811✔
4302
  return pStmt;
237,811✔
4303
_err:
×
4304
  nodesDestroyNode(pTbName);
×
4305
  return NULL;
×
4306
}
4307

4308
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
59,757✔
4309
  CHECK_PARSER_STATUS(pCxt);
59,757✔
4310
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
59,757✔
4311
  SShowCreateDatabaseStmt* pStmt = NULL;
59,757✔
4312
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
59,757✔
4313
  CHECK_MAKE_NODE(pStmt);
59,757✔
4314
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
59,757✔
4315
  return (SNode*)pStmt;
59,757✔
4316
_err:
×
4317
  return NULL;
×
4318
}
4319

4320
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
5,162✔
4321
  CHECK_PARSER_STATUS(pCxt);
5,162✔
4322
  SToken  dbToken = {0};
5,162✔
4323
  SToken* pDbToken = NULL;
5,162✔
4324

4325
  if (pNode) {
5,162✔
4326
    SValueNode* pDbName = (SValueNode*)pNode;
1,655✔
4327
    if (pDbName->literal) {
1,655✔
4328
      dbToken.z = pDbName->literal;
1,655✔
4329
      dbToken.n = strlen(pDbName->literal);
1,655✔
4330
      pDbToken = &dbToken;
1,655✔
4331
    }
4332
  }
4333

4334
  if (pDbToken) {
5,162✔
4335
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
1,655✔
4336
  }
4337

4338
  SShowAliveStmt* pStmt = NULL;
5,162✔
4339
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
5,162✔
4340
  CHECK_PARSER_STATUS(pCxt);
5,162✔
4341

4342
  if (pDbToken) {
5,162✔
4343
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
1,655✔
4344
  }
4345
  if (pNode) {
5,162✔
4346
    nodesDestroyNode(pNode);
1,655✔
4347
  }
4348

4349
  return (SNode*)pStmt;
5,162✔
4350
_err:
×
4351
  nodesDestroyNode(pNode);
×
4352
  return NULL;
×
4353
}
4354

4355
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
90,954✔
4356
  CHECK_PARSER_STATUS(pCxt);
90,954✔
4357
  SShowCreateTableStmt* pStmt = NULL;
90,530✔
4358
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
90,530✔
4359
  CHECK_MAKE_NODE(pStmt);
90,530✔
4360
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
90,530✔
4361
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
90,530✔
4362
  nodesDestroyNode(pRealTable);
90,530✔
4363
  return (SNode*)pStmt;
90,530✔
4364
_err:
424✔
4365
  nodesDestroyNode(pRealTable);
424✔
4366
  return NULL;
424✔
4367
}
4368

4369
SNode* createShowCreateVTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
7,397✔
4370
  CHECK_PARSER_STATUS(pCxt);
7,397✔
4371
  SShowCreateTableStmt* pStmt = NULL;
7,397✔
4372
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
7,397✔
4373
  CHECK_MAKE_NODE(pStmt);
7,397✔
4374
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
7,397✔
4375
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
7,397✔
4376
  nodesDestroyNode(pRealTable);
7,397✔
4377
  return (SNode*)pStmt;
7,397✔
4378
_err:
×
4379
  nodesDestroyNode(pRealTable);
×
4380
  return NULL;
×
4381
}
4382

4383
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
8,820✔
4384
  CHECK_PARSER_STATUS(pCxt);
8,820✔
4385
  SShowCreateViewStmt* pStmt = NULL;
8,820✔
4386
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
8,820✔
4387
  CHECK_MAKE_NODE(pStmt);
8,820✔
4388
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
8,820✔
4389
  tstrncpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
8,820✔
4390
  nodesDestroyNode(pRealTable);
8,820✔
4391
  return (SNode*)pStmt;
8,820✔
4392
_err:
×
4393
  nodesDestroyNode(pRealTable);
×
4394
  return NULL;
×
4395
}
4396

4397
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
4,462✔
4398
  CHECK_PARSER_STATUS(pCxt);
4,462✔
4399
  SShowTableDistributedStmt* pStmt = NULL;
4,462✔
4400
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
4,462✔
4401
  CHECK_MAKE_NODE(pStmt);
4,462✔
4402
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
4,462✔
4403
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
4,462✔
4404
  nodesDestroyNode(pRealTable);
4,462✔
4405
  return (SNode*)pStmt;
4,462✔
4406
_err:
×
4407
  nodesDestroyNode(pRealTable);
×
4408
  return NULL;
×
4409
}
4410

4411
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
41,132✔
4412
  CHECK_PARSER_STATUS(pCxt);
41,132✔
4413
  SShowDnodeVariablesStmt* pStmt = NULL;
41,132✔
4414
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
41,132✔
4415
  CHECK_MAKE_NODE(pStmt);
41,132✔
4416
  pStmt->pDnodeId = pDnodeId;
41,132✔
4417
  pStmt->pLikePattern = pLikePattern;
41,132✔
4418
  return (SNode*)pStmt;
41,132✔
4419
_err:
×
4420
  nodesDestroyNode(pDnodeId);
×
4421
  nodesDestroyNode(pLikePattern);
×
4422
  return NULL;
×
4423
}
4424

4425
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
5,930✔
4426
  CHECK_PARSER_STATUS(pCxt);
5,930✔
4427
  SShowVnodesStmt* pStmt = NULL;
5,930✔
4428
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
5,930✔
4429
  CHECK_MAKE_NODE(pStmt);
5,930✔
4430
  pStmt->pDnodeId = pDnodeId;
5,930✔
4431
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
5,930✔
4432
  return (SNode*)pStmt;
5,930✔
4433
_err:
×
4434
  nodesDestroyNode(pDnodeId);
×
4435
  nodesDestroyNode(pDnodeEndpoint);
×
4436
  return NULL;
×
4437
}
4438

4439
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
7,941✔
4440
  CHECK_PARSER_STATUS(pCxt);
7,941✔
4441
  if (NULL == pDbName) {
7,941✔
4442
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
4443
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4444
    CHECK_PARSER_STATUS(pCxt);
×
4445
  }
4446
  SShowTableTagsStmt* pStmt = NULL;
7,941✔
4447
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
7,941✔
4448
  CHECK_MAKE_NODE(pStmt);
7,941✔
4449
  pStmt->pDbName = pDbName;
7,941✔
4450
  pStmt->pTbName = pTbName;
7,941✔
4451
  pStmt->pTags = pTags;
7,941✔
4452
  return (SNode*)pStmt;
7,941✔
4453
_err:
×
4454
  nodesDestroyNode(pTbName);
×
4455
  nodesDestroyNode(pDbName);
×
4456
  nodesDestroyList(pTags);
×
4457
  return NULL;
×
4458
}
4459

4460
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
32,025✔
4461
  CHECK_PARSER_STATUS(pCxt);
32,025✔
4462
  SShowCompactDetailsStmt* pStmt = NULL;
32,025✔
4463
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
32,025✔
4464
  CHECK_MAKE_NODE(pStmt);
32,025✔
4465
  pStmt->pId = pCompactId;
32,025✔
4466
  return (SNode*)pStmt;
32,025✔
4467
_err:
×
4468
  nodesDestroyNode(pCompactId);
×
4469
  return NULL;
×
4470
}
4471

4472
SNode* createShowRetentionDetailsStmt(SAstCreateContext* pCxt, SNode* pId) {
2,307✔
4473
  CHECK_PARSER_STATUS(pCxt);
2,307✔
4474
  SShowRetentionDetailsStmt* pStmt = NULL;
2,307✔
4475
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_RETENTION_DETAILS_STMT, (SNode**)&pStmt);
2,307✔
4476
  CHECK_MAKE_NODE(pStmt);
2,307✔
4477
  pStmt->pId = pId;
2,307✔
4478
  return (SNode*)pStmt;
2,307✔
4479
_err:
×
4480
  nodesDestroyNode(pId);
×
4481
  return NULL;
×
4482
}
4483

4484
SNode* createShowTransactionDetailsStmt(SAstCreateContext* pCxt, SNode* pTransactionIdNode) {
299✔
4485
  CHECK_PARSER_STATUS(pCxt);
299✔
4486
  SShowTransactionDetailsStmt* pStmt = NULL;
299✔
4487
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT, (SNode**)&pStmt);
299✔
4488
  CHECK_MAKE_NODE(pStmt);
299✔
4489
  pStmt->pTransactionId = pTransactionIdNode;
299✔
4490
  return (SNode*)pStmt;
299✔
4491
_err:
×
4492
  nodesDestroyNode(pTransactionIdNode);
×
4493
  return NULL;
×
4494
}
4495

4496
SNode* createShowValidateVirtualTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pVTable) {
3,798✔
4497
  CHECK_PARSER_STATUS(pCxt);
3,798✔
4498
  SShowValidateVirtualTable* pStmt = NULL;
3,798✔
4499
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,798✔
4500
  CHECK_MAKE_NODE(pStmt);
3,798✔
4501

4502
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pVTable)->table.dbName, TSDB_DB_NAME_LEN);
3,798✔
4503
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pVTable)->table.tableName, TSDB_TABLE_NAME_LEN);
3,798✔
4504
  nodesDestroyNode(pVTable);
3,798✔
4505
  return (SNode*)pStmt;
3,798✔
4506
_err:
×
4507
  nodesDestroyNode(pVTable);
×
4508
  return NULL;
×
4509
}
4510

4511
static bool parseIp(const char* strIp, SIpRange* pIpRange) {
5,575✔
4512
  if (strchr(strIp, ':') == NULL) {
5,575✔
4513
    struct in_addr ip4;
5,575✔
4514
    if (inet_pton(AF_INET, strIp, &ip4) == 1) {
5,575✔
4515
      pIpRange->type = 0;
4,625✔
4516
      memcpy(&pIpRange->ipV4.ip, &ip4.s_addr, sizeof(ip4.s_addr));
4,625✔
4517
      return true;
4,625✔
4518
    }
4519
  } else {
4520
    struct in6_addr ip6;
×
4521
    if (inet_pton(AF_INET6, strIp, &ip6) == 1) {
×
4522
      pIpRange->type = 1;
×
4523
      memcpy(&pIpRange->ipV6.addr[0], ip6.s6_addr, 8);
×
4524
      memcpy(&pIpRange->ipV6.addr[1], ip6.s6_addr + 8, 8);
×
4525
      return true;
×
4526
    }
4527
  }
4528

4529
  return false;
950✔
4530
}
4531

4532

4533

4534
SIpRangeNode* parseIpRange(SAstCreateContext* pCxt, const SToken* token) {
5,575✔
4535
  CHECK_PARSER_STATUS(pCxt);
5,575✔
4536

4537
#ifdef TD_ASTRA
4538
  return NULL;
4539
#else
4540

4541
  SIpRangeNode* node = NULL;
5,575✔
4542
  int32_t code = nodesMakeNode(QUERY_NODE_IP_RANGE, (SNode**)&node);
5,575✔
4543
  if (node == NULL) {
5,575✔
4544
    goto _err;
×
4545
  }
4546

4547
  char buf[64];
5,575✔
4548
  if (token->n >= sizeof(buf)) {
5,575✔
4549
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
4550
    goto _err;
×
4551
  }
4552
  memcpy(buf, token->z, token->n);
5,575✔
4553
  buf[token->n] = '\0';
5,575✔
4554
  (void)strdequote(buf);
5,575✔
4555

4556
  char* slash = strchr(buf, '/');
5,575✔
4557
  if (slash) {
5,575✔
4558
    *slash = '\0';
1,980✔
4559
  }
4560

4561
  if (!parseIp(buf, &node->range)) {
5,575✔
4562
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
950✔
4563
    goto _err;
950✔
4564
  }
4565

4566
  int32_t mask = 0;
4,625✔
4567
  if (!slash) {
4,625✔
4568
    mask = node->range.type == 0 ? 32 : 128;
2,820✔
4569
  } else if (taosStr2int32(slash + 1, &mask) != TSDB_CODE_SUCCESS) {
1,805✔
4570
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
4571
    goto _err;
×
4572
  }
4573

4574
  code = tIpRangeSetMask(&node->range, mask);
4,625✔
4575
  if (code != TSDB_CODE_SUCCESS) {
4,625✔
4576
    goto _err;
485✔
4577
  }
4578

4579
  return node;
4,140✔
4580

4581
_err:
1,435✔
4582
  pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
1,435✔
4583
  nodesDestroyNode((SNode*)node);
1,435✔
4584
  return NULL;
1,435✔
4585

4586
#endif
4587
}
4588

4589

4590

4591
SDateTimeRangeNode* parseDateTimeRange(SAstCreateContext* pCxt, const SToken* token) {
5,270✔
4592
  CHECK_PARSER_STATUS(pCxt);
5,270✔
4593

4594
  SDateTimeRangeNode* node = NULL;
5,270✔
4595
  int32_t code = nodesMakeNode(QUERY_NODE_DATE_TIME_RANGE, (SNode**)&node);
5,270✔
4596
  if (code != TSDB_CODE_SUCCESS) {
5,270✔
4597
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
×
4598
    goto _err;
×
4599
  }
4600

4601
  char buf[128];
5,270✔
4602
  if (token->n >= sizeof(buf)) {
5,270✔
4603
    code = TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG;
×
4604
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Date time range string is too long");
×
4605
    goto _err;
×
4606
  }
4607
  memcpy(buf, token->z, token->n);
5,270✔
4608
  buf[token->n] = '\0';
5,270✔
4609
  (void)strdequote(buf);
5,270✔
4610

4611
  code = TSDB_CODE_PAR_INVALID_OPTION_VALUE;
5,270✔
4612
  int32_t year = 0, month = 0, day = 0, hour = 0, minute = 0, duration = 0;
5,270✔
4613
  if (buf[0] >= '1' && buf[0] <= '9') {
8,060✔
4614
    // format: YYYY-MM-DD HH:MM duration
4615
    int ret = sscanf(buf, "%d-%d-%d %d:%d %d", &year, &month, &day, &hour, &minute, &duration);
4,030✔
4616
    if (ret != 6) {
4,030✔
4617
      goto _err;
930✔
4618
    }
4619
    if (month < 1 || month > 12) {
3,100✔
4620
      goto _err;
310✔
4621
    }
4622
  } else {
4623
    // format: WEEKDAY HH:MM duration
4624
    char weekday[4];
1,240✔
4625
    int ret = sscanf(buf, "%3s %d:%d %d", weekday, &hour, &minute, &duration);
1,240✔
4626
    if (ret != 4) {
1,240✔
4627
      goto _err;
310✔
4628
    }
4629
    day = taosParseShortWeekday(weekday);
930✔
4630
    if (day < 0 || day > 6) {
930✔
4631
      goto _err;
×
4632
    }
4633
    month = -1;
930✔
4634
  }
4635

4636
  node->range.year = (int16_t)year;
3,720✔
4637
  node->range.month = (int8_t)month;
3,720✔
4638
  node->range.day = (int8_t)day;
3,720✔
4639
  node->range.hour = (int8_t)hour;
3,720✔
4640
  node->range.minute = (int8_t)minute;
3,720✔
4641
  node->range.duration = duration;
3,720✔
4642
  if (!isValidDateTimeRange(&node->range)) {
3,720✔
4643
    goto _err;
×
4644
  }
4645

4646
  return node;
3,720✔
4647

4648
_err:
1,550✔
4649
  if (code == TSDB_CODE_PAR_INVALID_OPTION_VALUE) { // other error types have been set above
1,550✔
4650
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid date time range");
1,550✔
4651
  }
4652
  nodesDestroyNode((SNode*)node);
1,550✔
4653
  return NULL;
1,550✔
4654
}
4655

4656

4657
SUserOptions* createDefaultUserOptions(SAstCreateContext* pCxt) {
188,988✔
4658
  SUserOptions* pOptions = NULL;
188,988✔
4659
  int32_t code = nodesMakeNode(QUERY_NODE_USER_OPTIONS, (SNode**)&pOptions);
188,988✔
4660
  if (pOptions == NULL) {
188,988✔
4661
    pCxt->errCode = code;
×
4662
    return NULL;
×
4663
  }
4664

4665
  pOptions->enable = 1;
188,988✔
4666
  pOptions->sysinfo = 1;
188,988✔
4667
  pOptions->createdb = 0;
188,988✔
4668
  pOptions->isImport = 0;
188,988✔
4669
  pOptions->changepass = 2;
188,988✔
4670

4671
  pOptions->sessionPerUser = TSDB_USER_SESSION_PER_USER_DEFAULT;
188,988✔
4672
  pOptions->connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
188,988✔
4673
  pOptions->connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
188,988✔
4674
  pOptions->callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
188,988✔
4675
  pOptions->vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;
188,988✔
4676
  pOptions->failedLoginAttempts = TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
188,988✔
4677
  pOptions->passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
188,988✔
4678
  pOptions->passwordReuseTime = TSDB_USER_PASSWORD_REUSE_TIME_DEFAULT;
188,988✔
4679
  pOptions->passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
188,988✔
4680
  pOptions->passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
188,988✔
4681
  pOptions->passwordGraceTime = TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
188,988✔
4682
  pOptions->inactiveAccountTime = TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
188,988✔
4683
  pOptions->allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
188,988✔
4684

4685
  return pOptions;
188,988✔
4686
}
4687

4688

4689

4690
SUserOptions* mergeUserOptions(SAstCreateContext* pCxt, SUserOptions* a, SUserOptions* b) {
225,237✔
4691
  if (a == NULL && b == NULL) {
225,237✔
4692
      return createDefaultUserOptions(pCxt);
188,988✔
4693
  }
4694
  if (b == NULL) {
36,249✔
4695
    return a;
22,637✔
4696
  }
4697
  if (a == NULL) {
13,612✔
4698
    return b;
×
4699
  }
4700

4701
  if (b->hasPassword) {
13,612✔
4702
    if (a->hasPassword) {
×
4703
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASS");
×
4704
    } else {
4705
      a->hasPassword = true;
×
4706
      tstrncpy(a->password, b->password, sizeof(a->password));
×
4707
    }
4708
  }
4709

4710
  if (b->hasTotpseed) {
13,612✔
4711
    if (a->hasTotpseed) {
×
4712
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TOTPSEED");
×
4713
    } else {
4714
      a->hasTotpseed = true;
×
4715
      tstrncpy(a->totpseed, b->totpseed, sizeof(a->totpseed));
×
4716
    }
4717
  }
4718

4719
  if (b->hasEnable) {
13,612✔
4720
    if (a->hasEnable) {
×
4721
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE/ACCOUNT LOCK/ACCOUNT UNLOCK");
×
4722
    } else {
4723
      a->hasEnable = true;
×
4724
      a->enable = b->enable;
×
4725
    }
4726
  }
4727

4728
  if (b->hasSysinfo) {
13,612✔
4729
    if (a->hasSysinfo) {
176✔
4730
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SYSINFO");
×
4731
    } else {
4732
      a->hasSysinfo = true;
176✔
4733
      a->sysinfo = b->sysinfo;
176✔
4734
    }
4735
  }
4736

4737
  if (b->hasCreatedb) {
13,612✔
4738
    if (a->hasCreatedb) {
2,121✔
4739
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CREATEDB");
×
4740
    } else {
4741
      a->hasCreatedb = true;
2,121✔
4742
      a->createdb = b->createdb;
2,121✔
4743
    }
4744
  }
4745

4746
  if (b->hasChangepass) {
13,612✔
4747
    if (a->hasChangepass) {
×
4748
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CHANGEPASS");
×
4749
    } else {
4750
      a->hasChangepass = true;
×
4751
      a->changepass = b->changepass;
×
4752
    }
4753
  }
4754

4755
  if (b->hasSessionPerUser) {
13,612✔
4756
    if (a->hasSessionPerUser) {
×
4757
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SESSION_PER_USER");
×
4758
    } else {
4759
      a->hasSessionPerUser = true;
×
4760
      a->sessionPerUser = b->sessionPerUser;
×
4761
    }
4762
  }
4763

4764
  if (b->hasConnectTime) {
13,612✔
4765
    if (a->hasConnectTime) {
310✔
4766
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_TIME");
×
4767
    } else {
4768
      a->hasConnectTime = true;
310✔
4769
      a->connectTime = b->connectTime;
310✔
4770
    }
4771
  }
4772

4773
  if (b->hasConnectIdleTime) {
13,612✔
4774
    if (a->hasConnectIdleTime) {
310✔
4775
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_IDLE_TIME");
×
4776
    } else {
4777
      a->hasConnectIdleTime = true;
310✔
4778
      a->connectIdleTime = b->connectIdleTime;
310✔
4779
    }
4780
  }
4781

4782
  if (b->hasCallPerSession) {
13,612✔
4783
    if (a->hasCallPerSession) {
310✔
4784
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CALLS_PER_SESSION");
×
4785
    } else {
4786
      a->hasCallPerSession = true;
310✔
4787
      a->callPerSession = b->callPerSession;
310✔
4788
    }
4789
  }
4790

4791
  if (b->hasVnodePerCall) {
13,612✔
4792
    if (a->hasVnodePerCall) {
465✔
4793
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "VNODES_PER_CALL");
×
4794
    } else {
4795
      a->hasVnodePerCall = true;
465✔
4796
      a->vnodePerCall = b->vnodePerCall;
465✔
4797
    }
4798
  }
4799

4800
  if (b->hasFailedLoginAttempts) {
13,612✔
4801
    if (a->hasFailedLoginAttempts) {
310✔
4802
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "FAILED_LOGIN_ATTEMPTS");
×
4803
    } else {
4804
      a->hasFailedLoginAttempts = true;
310✔
4805
      a->failedLoginAttempts = b->failedLoginAttempts;
310✔
4806
    }
4807
  }
4808

4809
  if (b->hasPasswordLifeTime) {
13,612✔
4810
    if (a->hasPasswordLifeTime) {
310✔
4811
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LIFE_TIME");
×
4812
    } else {
4813
      a->hasPasswordLifeTime = true;
310✔
4814
      a->passwordLifeTime = b->passwordLifeTime;
310✔
4815
    }
4816
  }
4817

4818
  if (b->hasPasswordReuseTime) {
13,612✔
4819
    if (a->hasPasswordReuseTime) {
2,064✔
4820
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_TIME");
×
4821
    } else {
4822
      a->hasPasswordReuseTime = true;
2,064✔
4823
      a->passwordReuseTime = b->passwordReuseTime;
2,064✔
4824
    }
4825
  }
4826

4827
  if (b->hasPasswordReuseMax) {
13,612✔
4828
    if (a->hasPasswordReuseMax) {
1,957✔
4829
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_MAX");
×
4830
    } else {
4831
      a->hasPasswordReuseMax = true;
1,957✔
4832
      a->passwordReuseMax = b->passwordReuseMax;
1,957✔
4833
    }
4834
  }
4835

4836
  if (b->hasPasswordLockTime) {
13,612✔
4837
    if (a->hasPasswordLockTime) {
310✔
4838
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LOCK_TIME");
×
4839
    } else {
4840
      a->hasPasswordLockTime = true;
310✔
4841
      a->passwordLockTime = b->passwordLockTime;
310✔
4842
    }
4843
  }
4844

4845
  if (b->hasPasswordGraceTime) {
13,612✔
4846
    if (a->hasPasswordGraceTime) {
310✔
4847
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_GRACE_TIME");
×
4848
    } else {
4849
      a->hasPasswordGraceTime = true;
310✔
4850
      a->passwordGraceTime = b->passwordGraceTime;
310✔
4851
    }
4852
  }
4853

4854
  if (b->hasInactiveAccountTime) {
13,612✔
4855
    if (a->hasInactiveAccountTime) {
310✔
4856
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "INACTIVE_ACCOUNT_TIME");
×
4857
    } else {
4858
      a->hasInactiveAccountTime = true;
310✔
4859
      a->inactiveAccountTime = b->inactiveAccountTime;
310✔
4860
    }
4861
  }
4862

4863
  if (b->hasAllowTokenNum) {
13,612✔
4864
    if (a->hasAllowTokenNum) {
310✔
4865
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ALLOW_TOKEN_NUM");
×
4866
    } else {
4867
      a->hasAllowTokenNum = true;
310✔
4868
      a->allowTokenNum = b->allowTokenNum;
310✔
4869
    }
4870
  }
4871

4872
  if (b->pIpRanges != NULL) {
13,612✔
4873
    if (a->pIpRanges == NULL) {
1,540✔
4874
      a->pIpRanges = b->pIpRanges;
1,230✔
4875
    } else {
4876
      int32_t code = nodesListAppendList(a->pIpRanges, b->pIpRanges);
310✔
4877
      if (code != TSDB_CODE_SUCCESS) {
310✔
4878
        pCxt->errCode = code;
×
4879
      }
4880
    }
4881
    b->pIpRanges = NULL;
1,540✔
4882
  }
4883

4884
  if (b->pDropIpRanges != NULL) {
13,612✔
4885
    if (a->pDropIpRanges == NULL) {
155✔
4886
      a->pDropIpRanges = b->pDropIpRanges;
×
4887
    } else {
4888
      int32_t code = nodesListAppendList(a->pDropIpRanges, b->pDropIpRanges);
155✔
4889
      if (code != TSDB_CODE_SUCCESS) {
155✔
4890
        pCxt->errCode = code;
×
4891
      }
4892
    }
4893
    b->pDropIpRanges = NULL;
155✔
4894
  }
4895

4896
  if (b->pTimeRanges != NULL) {
13,612✔
4897
    if (a->pTimeRanges == NULL) {
930✔
4898
      a->pTimeRanges = b->pTimeRanges;
620✔
4899
    } else {
4900
      int32_t code = nodesListAppendList(a->pTimeRanges, b->pTimeRanges);
310✔
4901
      if (code != TSDB_CODE_SUCCESS) {
310✔
4902
        pCxt->errCode = code;
×
4903
      }
4904
    }
4905
    b->pTimeRanges = NULL;
930✔
4906
  }
4907

4908
  if (b->pDropTimeRanges != NULL) {
13,612✔
4909
    if (a->pDropTimeRanges == NULL) {
155✔
4910
      a->pDropTimeRanges = b->pDropTimeRanges;
×
4911
    } else {
4912
      int32_t code = nodesListAppendList(a->pDropTimeRanges, b->pDropTimeRanges);
155✔
4913
      if (code != TSDB_CODE_SUCCESS) {
155✔
4914
        pCxt->errCode = code;
×
4915
      }
4916
    }
4917
    b->pDropTimeRanges = NULL;
155✔
4918
  }
4919

4920
  nodesDestroyNode((SNode*)b);
13,612✔
4921
  return a;
13,612✔
4922
}
4923

4924

4925

4926
void setUserOptionsTotpseed(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pTotpseed) {
×
4927
  pUserOptions->hasTotpseed = true;
×
4928

4929
  if (pTotpseed == NULL) { // clear TOTP secret
×
4930
    memset(pUserOptions->totpseed, 0, sizeof(pUserOptions->totpseed));
×
4931
    return;
×
4932
  }
4933

4934
  if (pTotpseed->n >= sizeof(pUserOptions->totpseed) * 2) {
×
4935
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
4936
    return;
×
4937
  }
4938

4939
  char buf[sizeof(pUserOptions->totpseed) * 2 + 1];
×
4940
  memcpy(buf, pTotpseed->z, pTotpseed->n);
×
4941
  buf[pTotpseed->n] = 0;
×
4942
  (void)strdequote(buf);
×
4943
  size_t len = strtrim(buf);
×
4944

4945
  if (len >= sizeof(pUserOptions->totpseed)) {
×
4946
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
4947
  } else if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
4948
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT, "TOTPSEED", TSDB_USER_TOTPSEED_MIN_LEN);
×
4949
  } else {
4950
    tstrncpy(pUserOptions->totpseed, buf, sizeof(pUserOptions->totpseed));
×
4951
  }
4952
}
4953

4954

4955

4956
void setUserOptionsPassword(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pPassword) {
153,948✔
4957
  pUserOptions->hasPassword = true;
153,948✔
4958

4959
  if (pPassword->n >= sizeof(pUserOptions->password) * 2) {
153,948✔
4960
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
4961
    return;
×
4962
  }
4963

4964
  char buf[sizeof(pUserOptions->password) * 2 + 1];
151,187✔
4965
  memcpy(buf, pPassword->z, pPassword->n);
153,948✔
4966
  buf[pPassword->n] = 0;
153,948✔
4967
  (void)strdequote(buf);
153,948✔
4968
  size_t len = strtrim(buf);
153,948✔
4969

4970
  if (len >= sizeof(pUserOptions->password)) {
153,948✔
4971
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
694✔
4972
  } else if (len < TSDB_PASSWORD_MIN_LEN) {
153,254✔
4973
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
5,593✔
4974
  } else {
4975
    tstrncpy(pUserOptions->password, buf, sizeof(pUserOptions->password));
147,661✔
4976
  }
4977
}
4978

4979

4980

4981
static bool isValidUserOptions(SAstCreateContext* pCxt, const SUserOptions* opts) {
165,441✔
4982
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
165,441✔
4983
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
1,286✔
4984
    return false;
1,286✔
4985
  }
4986

4987
  if (opts->hasSysinfo && (opts->sysinfo < 0 || opts->sysinfo > 1)) {
164,155✔
4988
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SYSINFO");
2,386✔
4989
    return false;
2,386✔
4990
  }
4991

4992
  if (opts->hasIsImport && (opts->isImport < 0 || opts->isImport > 1)) {
161,769✔
4993
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "IS_IMPORT");
×
4994
    return false;
×
4995
  }
4996

4997
  if (opts->hasCreatedb && (opts->createdb < 0 || opts->createdb > 1)) {
161,769✔
4998
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CREATEDB");
1,286✔
4999
    return false;
1,286✔
5000
  }
5001

5002
  if (opts->hasTotpseed && opts->totpseed[0] != 0 && !taosIsComplexString(opts->totpseed)) {
160,483✔
5003
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TOTPSEED");
×
5004
    return false;
×
5005
  }
5006

5007
  if (opts->hasPassword && !isValidPassword(pCxt, opts->password, opts->hasIsImport && opts->isImport)) {
160,483✔
5008
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
×
5009
    return false;
×
5010
  }
5011

5012
  if (opts->hasChangepass && (opts->changepass < 0 || opts->changepass > 2)) {
160,483✔
5013
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CHANGEPASS");
155✔
5014
    return false;
155✔
5015
  }
5016

5017
  if (opts->hasSessionPerUser && (opts->sessionPerUser < -1 || opts->sessionPerUser == 0)) {
160,328✔
5018
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SESSION_PER_USER");
×
5019
    return false;
×
5020
  }
5021

5022
  if (opts->hasConnectTime && (opts->connectTime < -1 || opts->connectTime == 0)) {
160,328✔
5023
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_TIME");
×
5024
    return false;
×
5025
  }
5026

5027
  if (opts->hasConnectIdleTime && (opts->connectIdleTime < -1 || opts->connectIdleTime == 0)) {
160,328✔
5028
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_IDLE_TIME");
×
5029
    return false;
×
5030
  }
5031

5032
  if (opts->hasCallPerSession && (opts->callPerSession < -1 || opts->callPerSession == 0)) {
160,328✔
5033
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CALLS_PER_SESSION");
×
5034
    return false;
×
5035
  }
5036

5037
  if (opts->hasVnodePerCall && (opts->vnodePerCall < -1 || opts->vnodePerCall == 0)) {
160,328✔
5038
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "VNODES_PER_CALL");
155✔
5039
    return false;
155✔
5040
  }
5041

5042
  if (opts->hasFailedLoginAttempts && (opts->failedLoginAttempts < -1 || opts->failedLoginAttempts == 0)) {
160,173✔
5043
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "FAILED_LOGIN_ATTEMPTS");
155✔
5044
    return false;
155✔
5045
  }
5046

5047
  if (opts->hasPasswordLockTime && (opts->passwordLockTime < -1 || opts->passwordLockTime == 0)) {
160,018✔
5048
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LOCK_TIME");
155✔
5049
    return false;
155✔
5050
  }
5051

5052
  if (opts->hasPasswordLifeTime && opts->passwordLifeTime != -1 && opts->passwordLifeTime < TSDB_USER_PASSWORD_LIFE_TIME_MIN) {
159,863✔
5053
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LIFE_TIME");
155✔
5054
    return false;
155✔
5055
  }
5056

5057
  if (opts->hasPasswordGraceTime && (opts->passwordGraceTime < -1)) {
159,708✔
5058
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_GRACE_TIME");
×
5059
    return false;
×
5060
  }
5061

5062
  if (opts->hasPasswordReuseTime && (opts->passwordReuseTime < 0 || opts->passwordReuseTime > TSDB_USER_PASSWORD_REUSE_TIME_MAX)) {
159,708✔
5063
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_TIME");
155✔
5064
    return false;
155✔
5065
  }
5066

5067
  if (opts->hasPasswordReuseMax && (opts->passwordReuseMax < 0 || opts->passwordReuseMax > TSDB_USER_PASSWORD_REUSE_MAX_MAX)) {
159,553✔
5068
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_MAX");
155✔
5069
    return false;
155✔
5070
  }
5071

5072
  if (opts->hasInactiveAccountTime && (opts->inactiveAccountTime < -1 || opts->inactiveAccountTime == 0)) {
159,398✔
5073
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "INACTIVE_ACCOUNT_TIME");
155✔
5074
    return false;
155✔
5075
  }
5076

5077
  if (opts->hasAllowTokenNum && opts->allowTokenNum < -1) {
159,243✔
5078
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ALLOW_TOKEN_NUM");
×
5079
    return false;
×
5080
  }
5081

5082
  // ip ranges and date time ranges has been validated during parsing
5083

5084
  return true;
159,243✔
5085
}
5086

5087

5088

5089
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* opts, bool ignoreExists) {
116,206✔
5090
  SCreateUserStmt* pStmt = NULL;
116,206✔
5091

5092
  CHECK_PARSER_STATUS(pCxt);
116,206✔
5093
  CHECK_NAME(checkUserName(pCxt, pUserName));
108,952✔
5094

5095
  if (!isValidUserOptions(pCxt, opts)) {
108,628✔
5096
    goto _err;
2,526✔
5097
  }
5098

5099
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
106,102✔
5100
  CHECK_MAKE_NODE(pStmt);
106,102✔
5101

5102
  pStmt->hasSessionPerUser = opts->hasSessionPerUser;
106,102✔
5103
  pStmt->hasConnectTime = opts->hasConnectTime;
106,102✔
5104
  pStmt->hasConnectIdleTime = opts->hasConnectIdleTime;
106,102✔
5105
  pStmt->hasCallPerSession = opts->hasCallPerSession;
106,102✔
5106
  pStmt->hasVnodePerCall = opts->hasVnodePerCall;
106,102✔
5107
  pStmt->hasFailedLoginAttempts = opts->hasFailedLoginAttempts;
106,102✔
5108
  pStmt->hasPasswordLifeTime = opts->hasPasswordLifeTime;
106,102✔
5109
  pStmt->hasPasswordReuseTime = opts->hasPasswordReuseTime;
106,102✔
5110
  pStmt->hasPasswordReuseMax = opts->hasPasswordReuseMax;
106,102✔
5111
  pStmt->hasPasswordLockTime = opts->hasPasswordLockTime;
106,102✔
5112
  pStmt->hasPasswordGraceTime = opts->hasPasswordGraceTime;
106,102✔
5113
  pStmt->hasInactiveAccountTime = opts->hasInactiveAccountTime;
106,102✔
5114
  pStmt->hasAllowTokenNum = opts->hasAllowTokenNum;
106,102✔
5115

5116
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
106,102✔
5117
  tstrncpy(pStmt->password, opts->password, sizeof(pStmt->password));
106,102✔
5118
  tstrncpy(pStmt->totpseed, opts->totpseed, sizeof(pStmt->totpseed));
106,102✔
5119

5120
  pStmt->ignoreExists = ignoreExists;
106,102✔
5121
  pStmt->sysinfo = opts->sysinfo;
106,102✔
5122
  pStmt->createDb = opts->createdb;
106,102✔
5123
  pStmt->isImport = opts->isImport;
106,102✔
5124
  pStmt->changepass = opts->changepass;
106,102✔
5125
  pStmt->enable = opts->enable;
106,102✔
5126

5127
  pStmt->sessionPerUser = opts->sessionPerUser;
106,102✔
5128
  pStmt->connectTime = opts->connectTime;
106,102✔
5129
  pStmt->connectIdleTime = opts->connectIdleTime;
106,102✔
5130
  pStmt->callPerSession = opts->callPerSession;
106,102✔
5131
  pStmt->vnodePerCall = opts->vnodePerCall;
106,102✔
5132
  pStmt->failedLoginAttempts = opts->failedLoginAttempts;
106,102✔
5133
  pStmt->passwordLifeTime = opts->passwordLifeTime;
106,102✔
5134
  pStmt->passwordReuseTime = opts->passwordReuseTime;
106,102✔
5135
  pStmt->passwordReuseMax = opts->passwordReuseMax;
106,102✔
5136
  pStmt->passwordLockTime = opts->passwordLockTime;
106,102✔
5137
  pStmt->passwordGraceTime = opts->passwordGraceTime;
106,102✔
5138
  pStmt->inactiveAccountTime = opts->inactiveAccountTime;
106,102✔
5139
  pStmt->allowTokenNum = opts->allowTokenNum;
106,102✔
5140

5141
  pStmt->numIpRanges = LIST_LENGTH(opts->pIpRanges);
106,102✔
5142
  pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpRange));
106,102✔
5143
  CHECK_OUT_OF_MEM(pStmt->pIpRanges);
106,102✔
5144
  int i = 0;
106,102✔
5145
  SNode* pNode = NULL;
106,102✔
5146
  FOREACH(pNode, opts->pIpRanges) {
108,904✔
5147
    SIpRangeNode* node = (SIpRangeNode*)(pNode);
2,802✔
5148
    pStmt->pIpRanges[i++] = node->range;
2,802✔
5149
  }
5150

5151
  pStmt->numTimeRanges = LIST_LENGTH(opts->pTimeRanges);
106,102✔
5152
  pStmt->pTimeRanges = taosMemoryMalloc(pStmt->numTimeRanges * sizeof(SDateTimeRange));
106,102✔
5153
  CHECK_OUT_OF_MEM(pStmt->pTimeRanges);
106,102✔
5154
  i = 0;
106,102✔
5155
  pNode = NULL;
106,102✔
5156
  FOREACH(pNode, opts->pTimeRanges) {
108,582✔
5157
    SDateTimeRangeNode* node = (SDateTimeRangeNode*)(pNode);
2,480✔
5158
    pStmt->pTimeRanges[i++] = node->range;
2,480✔
5159
  }
5160

5161
  nodesDestroyNode((SNode*)opts);
106,102✔
5162
  return (SNode*)pStmt;
106,102✔
5163

5164
_err:
10,104✔
5165
  nodesDestroyNode((SNode*)pStmt);
10,104✔
5166
  nodesDestroyNode((SNode*)opts);
10,104✔
5167
  return NULL;
10,104✔
5168
}
5169

5170

5171

5172
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* pUserOptions) {
58,831✔
5173
  SAlterUserStmt* pStmt = NULL;
58,831✔
5174
  CHECK_PARSER_STATUS(pCxt);
58,831✔
5175
  CHECK_NAME(checkUserName(pCxt, pUserName));
56,813✔
5176
  if (!isValidUserOptions(pCxt, pUserOptions)) {
56,813✔
5177
    goto _err;
3,672✔
5178
  }
5179

5180
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
53,141✔
5181
  CHECK_MAKE_NODE(pStmt);
53,141✔
5182
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
53,141✔
5183
  pStmt->pUserOptions = pUserOptions;
53,141✔
5184
  return (SNode*)pStmt;
53,141✔
5185

5186
_err:
5,690✔
5187
  nodesDestroyNode((SNode*)pStmt);
5,690✔
5188
  nodesDestroyNode((SNode*)pUserOptions);
5,690✔
5189
  return NULL;
5,690✔
5190
}
5191

5192

5193

5194
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName, bool ignoreNotExists) {
51,557✔
5195
  CHECK_PARSER_STATUS(pCxt);
51,557✔
5196
  CHECK_NAME(checkUserName(pCxt, pUserName));
51,557✔
5197
  SDropUserStmt* pStmt = NULL;
51,557✔
5198
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
51,557✔
5199
  CHECK_MAKE_NODE(pStmt);
51,557✔
5200
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
51,557✔
5201
  pStmt->ignoreNotExists = ignoreNotExists;
51,557✔
5202
  return (SNode*)pStmt;
51,557✔
5203
_err:
×
5204
  return NULL;
×
5205
}
5206

5207
static bool checkRoleName(SAstCreateContext* pCxt, SToken* pName, bool checkSysName) {
938,287✔
5208
  if (NULL == pName) {
938,287✔
5209
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5210
  } else {
5211
    if (pName->n >= TSDB_ROLE_LEN) {
938,287✔
5212
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
5213
    }
5214
  }
5215
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
938,287✔
5216
    trimEscape(pCxt, pName, true);
938,287✔
5217
  }
5218
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
938,287✔
5219
    if (checkSysName && taosStrncasecmp(pName->z, "sys", 3) == 0) {  // system reserved role name prefix
938,287✔
5220
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
426✔
5221
                                              "Cannot create/drop/alter roles with reserved prefix 'sys'");
5222
    }
5223
  }
5224
  return TSDB_CODE_SUCCESS == pCxt->errCode;
938,287✔
5225
}
5226

5227

5228
STokenOptions* createDefaultTokenOptions(SAstCreateContext* pCxt) {
28,296✔
5229
  STokenOptions* pOptions = NULL;
28,296✔
5230
  int32_t code = nodesMakeNode(QUERY_NODE_TOKEN_OPTIONS, (SNode**)&pOptions);
28,296✔
5231
  if (pOptions == NULL) {
28,296✔
5232
    pCxt->errCode = code;
×
5233
    return NULL;
×
5234
  }
5235

5236
  pOptions->enable = 1;
28,296✔
5237
  pOptions->ttl = 0;
28,296✔
5238
  return pOptions;
28,296✔
5239
}
5240

5241

5242

5243
STokenOptions* mergeTokenOptions(SAstCreateContext* pCxt, STokenOptions* a, STokenOptions* b) {
8,506✔
5244
  if (a == NULL && b == NULL) {
8,506✔
5245
      return createDefaultTokenOptions(pCxt);
6,936✔
5246
  }
5247
  if (b == NULL) {
1,570✔
5248
    return a;
×
5249
  }
5250
  if (a == NULL) {
1,570✔
5251
    return b;
×
5252
  }
5253

5254
  if (b->hasEnable) {
1,570✔
5255
    if (a->hasEnable) {
×
5256
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE");
×
5257
    } else {
5258
      a->hasEnable = true;
×
5259
      a->enable = b->enable;
×
5260
    }
5261
  }
5262

5263
  if (b->hasTtl) {
1,570✔
5264
    if (a->hasTtl) {
690✔
5265
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TTL");
×
5266
    } else {
5267
      a->hasTtl = true;
690✔
5268
      a->ttl = b->ttl;
690✔
5269
    }
5270
  }
5271

5272
  if (b->hasProvider) {
1,570✔
5273
    if (a->hasProvider) {
528✔
5274
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PROVIDER");
×
5275
    } else {
5276
      a->hasProvider = true;
528✔
5277
      tstrncpy(a->provider, b->provider, sizeof(a->provider));
528✔
5278
    }
5279
  }
5280

5281
  if (b->hasExtraInfo) {
1,570✔
5282
    if (a->hasExtraInfo) {
352✔
5283
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "EXTRA_INFO");
×
5284
    } else {
5285
      a->hasExtraInfo = true;
352✔
5286
      tstrncpy(a->extraInfo, b->extraInfo, sizeof(a->extraInfo));
352✔
5287
    }
5288
  }
5289
  nodesDestroyNode((SNode*)b);
1,570✔
5290
  return a;
1,570✔
5291
}
5292

5293

5294

5295
void setTokenOptionsProvider(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pProvider) {
1,056✔
5296
  pTokenOptions->hasProvider = true;
1,056✔
5297

5298
  if (pProvider->n >= sizeof(pTokenOptions->provider) * 2) {
1,056✔
5299
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
×
5300
    return;
×
5301
  }
5302

5303
  char buf[sizeof(pTokenOptions->provider) * 2 + 1];
1,056✔
5304
  memcpy(buf, pProvider->z, pProvider->n);
1,056✔
5305
  buf[pProvider->n] = 0;
1,056✔
5306
  (void)strdequote(buf);
1,056✔
5307
  size_t len = strtrim(buf);
1,056✔
5308

5309
  if (len >= sizeof(pTokenOptions->provider)) {
1,056✔
5310
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
352✔
5311
  } else {
5312
    tstrncpy(pTokenOptions->provider, buf, sizeof(pTokenOptions->provider));
704✔
5313
  }
5314
}
5315

5316

5317

5318
void setTokenOptionsExtraInfo(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pExtraInfo) {
1,056✔
5319
  pTokenOptions->hasExtraInfo = true;
1,056✔
5320

5321
  if (pExtraInfo->n >= sizeof(pTokenOptions->extraInfo) * 2) {
1,056✔
5322
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
×
5323
    return;
×
5324
  }
5325

5326
  char buf[sizeof(pTokenOptions->extraInfo) * 2 + 1];
1,056✔
5327
  memcpy(buf, pExtraInfo->z, pExtraInfo->n);
1,056✔
5328
  buf[pExtraInfo->n] = 0;
1,056✔
5329
  (void)strdequote(buf);
1,056✔
5330
  size_t len = strtrim(buf);
1,056✔
5331

5332
  if (len >= sizeof(pTokenOptions->extraInfo)) {
1,056✔
5333
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
352✔
5334
  } else {
5335
    tstrncpy(pTokenOptions->extraInfo, buf, sizeof(pTokenOptions->extraInfo));
704✔
5336
  }
5337
}
5338

5339

5340

5341
static bool isValidTokenOptions(SAstCreateContext* pCxt, const STokenOptions* opts) {
4,662✔
5342
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
4,662✔
5343
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
352✔
5344
    return false;
352✔
5345
  }
5346

5347
  if (opts->hasTtl && (opts->ttl < 0)) {
4,310✔
5348
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TTL");
×
5349
    return false;
×
5350
  }
5351

5352
  return true;
4,310✔
5353
}
5354

5355

5356

5357
static bool checkTokenName(SAstCreateContext* pCxt, SToken* pTokenName) {
28,966✔
5358
  if (NULL == pTokenName) {
28,966✔
5359
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5360
  } else {
5361
    if (pTokenName->n >= TSDB_TOKEN_NAME_LEN) {
28,966✔
5362
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOKEN_NAME", TSDB_TOKEN_NAME_LEN);
352✔
5363
    }
5364
  }
5365
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
28,966✔
5366
    trimEscape(pCxt, pTokenName, true);
28,614✔
5367
  }
5368
  return TSDB_CODE_SUCCESS == pCxt->errCode;
28,966✔
5369
}
5370

5371
SNode* createCreateRoleStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pName) {
2,150✔
5372
  CHECK_PARSER_STATUS(pCxt);
2,150✔
5373
  CHECK_NAME(checkRoleName(pCxt, pName, true));
2,150✔
5374
  SCreateRoleStmt* pStmt = NULL;
2,150✔
5375
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ROLE_STMT, (SNode**)&pStmt);
2,150✔
5376
  CHECK_MAKE_NODE(pStmt);
2,150✔
5377
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
2,150✔
5378
  pStmt->ignoreExists = ignoreExists;
2,150✔
5379
  return (SNode*)pStmt;
2,150✔
5380
_err:
×
5381
  return NULL;
×
5382
}
5383

5384
SNode* createDropRoleStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pName) {
1,988✔
5385
  CHECK_PARSER_STATUS(pCxt);
1,988✔
5386
  CHECK_NAME(checkRoleName(pCxt, pName, true));
1,988✔
5387
  SDropRoleStmt* pStmt = NULL;
1,562✔
5388
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ROLE_STMT, (SNode**)&pStmt);
1,562✔
5389
  CHECK_MAKE_NODE(pStmt);
1,562✔
5390
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
1,562✔
5391
  pStmt->ignoreNotExists = ignoreNotExists;
1,562✔
5392
  return (SNode*)pStmt;
1,562✔
5393
_err:
426✔
5394
  return NULL;
426✔
5395
}
5396

5397
/**
5398
 * used by user and role
5399
 */
5400
SNode* createAlterRoleStmt(SAstCreateContext* pCxt, SToken* pName, int8_t alterType, void* pAlterInfo) {
426✔
5401
  SAlterRoleStmt* pStmt = NULL;
426✔
5402
  CHECK_PARSER_STATUS(pCxt);
426✔
5403
  CHECK_NAME(checkUserName(pCxt, pName));
426✔
5404
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ROLE_STMT, (SNode**)&pStmt);
426✔
5405
  CHECK_MAKE_NODE(pStmt);
426✔
5406
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
426✔
5407
  pStmt->alterType = alterType;
426✔
5408
  switch (alterType) {
426✔
5409
    case TSDB_ALTER_ROLE_LOCK: {
426✔
5410
      SToken* pVal = pAlterInfo;
426✔
5411
      pStmt->lock = taosStr2Int8(pVal->z, NULL, 10);
426✔
5412
      break;
426✔
5413
    }
5414
    default:
×
5415
      break;
×
5416
  }
5417
  return (SNode*)pStmt;
426✔
5418
_err:
×
5419
  nodesDestroyNode((SNode*)pStmt);
×
5420
  return NULL;
×
5421
}
5422

5423

5424
SNode* createCreateTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, SToken* pUserName, STokenOptions* opts, bool ignoreExists) {
23,810✔
5425
  SCreateTokenStmt* pStmt = NULL;
23,810✔
5426

5427
  CHECK_PARSER_STATUS(pCxt);
23,810✔
5428
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
23,458✔
5429
  CHECK_NAME(checkUserName(pCxt, pUserName));
23,106✔
5430

5431
  if (opts == NULL) {
23,106✔
5432
    opts = createDefaultTokenOptions(pCxt);
21,360✔
5433
  } else if (!isValidTokenOptions(pCxt, opts)) {
1,746✔
5434
    goto _err;
176✔
5435
  }
5436

5437
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOKEN_STMT, (SNode**)&pStmt);
22,930✔
5438
  CHECK_MAKE_NODE(pStmt);
22,930✔
5439

5440
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
22,930✔
5441
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
22,930✔
5442
  pStmt->enable = opts->enable;
22,930✔
5443
  pStmt->ignoreExists = ignoreExists;
22,930✔
5444
  pStmt->ttl = opts->ttl;
22,930✔
5445
  tstrncpy(pStmt->provider, opts->provider, sizeof(pStmt->provider));
22,930✔
5446
  tstrncpy(pStmt->extraInfo, opts->extraInfo, sizeof(pStmt->extraInfo));
22,930✔
5447
  nodesDestroyNode((SNode*)opts);
22,930✔
5448
  return (SNode*)pStmt;
22,930✔
5449

5450
_err:
880✔
5451
  nodesDestroyNode((SNode*)pStmt);
880✔
5452
  nodesDestroyNode((SNode*)opts);
880✔
5453
  return NULL;
880✔
5454
}
5455

5456

5457

5458
SNode* createAlterTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, STokenOptions* opts) {
3,268✔
5459
  SAlterTokenStmt* pStmt = NULL;
3,268✔
5460

5461
  CHECK_PARSER_STATUS(pCxt);
3,268✔
5462
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
2,916✔
5463
  if (!isValidTokenOptions(pCxt, opts)) {
2,916✔
5464
    goto _err;
176✔
5465
  }
5466

5467
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TOKEN_STMT, (SNode**)&pStmt);
2,740✔
5468
  CHECK_MAKE_NODE(pStmt);
2,740✔
5469

5470
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
2,740✔
5471
  pStmt->pTokenOptions = opts;
2,740✔
5472
  return (SNode*)pStmt;
2,740✔
5473

5474
_err:
528✔
5475
  nodesDestroyNode((SNode*)pStmt);
528✔
5476
  nodesDestroyNode((SNode*)opts);
528✔
5477
  return NULL;
528✔
5478
}
5479

5480

5481

5482
SNode* createDropTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, bool ignoreNotExists) {
2,592✔
5483
  SDropTokenStmt* pStmt = NULL;
2,592✔
5484

5485
  CHECK_PARSER_STATUS(pCxt);
2,592✔
5486
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
2,592✔
5487

5488
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOKEN_STMT, (SNode**)&pStmt);
2,592✔
5489
  CHECK_MAKE_NODE(pStmt);
2,592✔
5490

5491
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
2,592✔
5492
  pStmt->ignoreNotExists = ignoreNotExists;
2,592✔
5493
  return (SNode*)pStmt;
2,592✔
5494

5495
_err:
×
5496
  nodesDestroyNode((SNode*)pStmt);
×
5497
  return NULL;
×
5498
}
5499

5500
SNode* createCreateTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
18,082✔
5501
  SCreateTotpSecretStmt* pStmt = NULL;
18,082✔
5502

5503
  CHECK_PARSER_STATUS(pCxt);
18,082✔
5504
  CHECK_NAME(checkUserName(pCxt, pUserName));
18,082✔
5505

5506
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOTP_SECRET_STMT, (SNode**)&pStmt);
17,918✔
5507
  CHECK_MAKE_NODE(pStmt);
17,918✔
5508

5509
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
17,918✔
5510
  return (SNode*)pStmt;
17,918✔
5511

5512
_err:
164✔
5513
  nodesDestroyNode((SNode*)pStmt);
164✔
5514
  return NULL;
164✔
5515
}
5516

5517

5518
SNode* createDropTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
8,528✔
5519
  SDropTotpSecretStmt* pStmt = NULL;
8,528✔
5520

5521
  CHECK_PARSER_STATUS(pCxt);
8,528✔
5522
  CHECK_NAME(checkUserName(pCxt, pUserName));
8,528✔
5523

5524
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOTP_SECRET_STMT, (SNode**)&pStmt);
6,888✔
5525
  CHECK_MAKE_NODE(pStmt);
6,888✔
5526

5527
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
6,888✔
5528
  return (SNode*)pStmt;
6,888✔
5529

5530
_err:
1,640✔
5531
  nodesDestroyNode((SNode*)pStmt);
1,640✔
5532
  return NULL;
1,640✔
5533
}
5534

5535

5536
SNode* createDropEncryptAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId) {
×
5537
  CHECK_PARSER_STATUS(pCxt);
×
5538
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5539
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
5540
    goto _err;
×
5541
  }
5542
  SDropEncryptAlgrStmt* pStmt = NULL;
×
5543
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ENCRYPT_ALGR_STMT, (SNode**)&pStmt);
×
5544
  CHECK_MAKE_NODE(pStmt);
×
5545
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
5546
  return (SNode*)pStmt;
×
5547
_err:
×
5548
  return NULL;
×
5549
}
5550

5551
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
172,496✔
5552
  CHECK_PARSER_STATUS(pCxt);
172,496✔
5553
  SCreateDnodeStmt* pStmt = NULL;
172,496✔
5554
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
172,496✔
5555
  CHECK_MAKE_NODE(pStmt);
172,496✔
5556
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
172,496✔
5557
    nodesDestroyNode((SNode*)pStmt);
×
5558
    return NULL;
×
5559
  }
5560
  return (SNode*)pStmt;
172,496✔
5561
_err:
×
5562
  return NULL;
×
5563
}
5564

5565
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
18,284✔
5566
  CHECK_PARSER_STATUS(pCxt);
18,284✔
5567
  SDropDnodeStmt* pStmt = NULL;
18,284✔
5568
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
18,284✔
5569
  CHECK_MAKE_NODE(pStmt);
18,284✔
5570
  if (TK_NK_INTEGER == pDnode->type) {
18,284✔
5571
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
13,580✔
5572
  } else {
5573
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
4,704✔
5574
      nodesDestroyNode((SNode*)pStmt);
×
5575
      return NULL;
×
5576
    }
5577
  }
5578
  pStmt->force = force;
18,284✔
5579
  pStmt->unsafe = unsafe;
18,284✔
5580
  return (SNode*)pStmt;
18,284✔
5581
_err:
×
5582
  return NULL;
×
5583
}
5584

5585
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
125,415✔
5586
                            const SToken* pValue) {
5587
  CHECK_PARSER_STATUS(pCxt);
125,415✔
5588
  SAlterDnodeStmt* pStmt = NULL;
125,415✔
5589
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
125,415✔
5590
  CHECK_MAKE_NODE(pStmt);
125,415✔
5591
  if (NULL != pDnode) {
125,415✔
5592
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
69,856✔
5593
  } else {
5594
    pStmt->dnodeId = -1;
55,559✔
5595
  }
5596
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
125,415✔
5597
  if (NULL != pValue) {
125,415✔
5598
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
54,511✔
5599
  }
5600
  return (SNode*)pStmt;
125,415✔
5601
_err:
×
5602
  return NULL;
×
5603
}
5604

5605
SNode* createCreateAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId, const SToken* name, const SToken* desc,
×
5606
                            const SToken* type, const SToken* osslAlgrName) {
5607
  CHECK_PARSER_STATUS(pCxt);
×
5608
  SCreateEncryptAlgrStmt* pStmt = NULL;
×
5609
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ENCRYPT_ALGORITHMS_STMT, (SNode**)&pStmt);
×
5610
  CHECK_MAKE_NODE(pStmt);
×
5611
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5612
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
5613
    goto _err;
×
5614
  }
5615
  if (name->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5616
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_NAME_TOO_LONG);
×
5617
    goto _err;
×
5618
  }
5619
  if (desc->n >= TSDB_ENCRYPT_ALGR_DESC_LEN) {
×
5620
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_DESC_TOO_LONG);
×
5621
    goto _err;
×
5622
  }
5623
  if (type->n >= TSDB_ENCRYPT_ALGR_TYPE_LEN) {
×
5624
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_TYPE_TOO_LONG);
×
5625
    goto _err;
×
5626
  }
5627
  if (osslAlgrName->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5628
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_OSSL_NAME_TOO_LONG);
×
5629
    goto _err;
×
5630
  }
5631
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
5632
  (void)trimString(name->z, name->n, pStmt->name, sizeof(pStmt->name));
×
5633
  (void)trimString(desc->z, desc->n, pStmt->desc, sizeof(pStmt->desc));
×
5634
  (void)trimString(type->z, type->n, pStmt->algrType, sizeof(pStmt->algrType));
×
5635
  (void)trimString(osslAlgrName->z, osslAlgrName->n, pStmt->osslAlgrName, sizeof(pStmt->osslAlgrName));
×
5636
  return (SNode*)pStmt;
×
5637
_err:
×
5638
  return NULL;
×
5639
}
5640

5641
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
×
5642
  CHECK_PARSER_STATUS(pCxt);
×
5643
  SCreateAnodeStmt* pStmt = NULL;
×
5644
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
×
5645
  CHECK_MAKE_NODE(pStmt);
×
5646
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
×
5647
  return (SNode*)pStmt;
×
5648
_err:
×
5649
  return NULL;
×
5650
}
5651

5652
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
×
5653
  CHECK_PARSER_STATUS(pCxt);
×
5654
  SUpdateAnodeStmt* pStmt = NULL;
×
5655
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
×
5656
  CHECK_MAKE_NODE(pStmt);
×
5657
  if (NULL != pAnode) {
×
5658
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5659
  } else {
5660
    pStmt->anodeId = -1;
×
5661
  }
5662
  return (SNode*)pStmt;
×
5663
_err:
×
5664
  return NULL;
×
5665
}
5666

5667
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
5668
  CHECK_PARSER_STATUS(pCxt);
×
5669
  SUpdateAnodeStmt* pStmt = NULL;
×
5670
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
5671
  CHECK_MAKE_NODE(pStmt);
×
5672
  if (NULL != pAnode) {
×
5673
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5674
  } else {
5675
    pStmt->anodeId = -1;
×
5676
  }
5677
  return (SNode*)pStmt;
×
5678
_err:
×
5679
  return NULL;
×
5680
}
5681

5682
SNode* createCreateBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId, SNode* pOptions) {
26,289✔
5683
  CHECK_PARSER_STATUS(pCxt);
26,289✔
5684
  SCreateBnodeStmt* pStmt = NULL;
26,289✔
5685
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_BNODE_STMT, (SNode**)&pStmt);
26,289✔
5686
  CHECK_MAKE_NODE(pStmt);
26,289✔
5687
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
26,289✔
5688

5689
  pStmt->pOptions = (SBnodeOptions*)pOptions;
26,289✔
5690

5691
  return (SNode*)pStmt;
26,289✔
5692
_err:
×
5693
  return NULL;
×
5694
}
5695

5696
SNode* createDropBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId) {
31,151✔
5697
  CHECK_PARSER_STATUS(pCxt);
31,151✔
5698
  SUpdateBnodeStmt* pStmt = NULL;
31,151✔
5699
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_BNODE_STMT, (SNode**)&pStmt);
31,151✔
5700
  CHECK_MAKE_NODE(pStmt);
31,151✔
5701
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
31,151✔
5702

5703
  return (SNode*)pStmt;
31,151✔
5704
_err:
×
5705
  return NULL;
×
5706
}
5707

5708
SNode* createDefaultBnodeOptions(SAstCreateContext* pCxt) {
26,289✔
5709
  CHECK_PARSER_STATUS(pCxt);
26,289✔
5710
  SBnodeOptions* pOptions = NULL;
26,289✔
5711
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BNODE_OPTIONS, (SNode**)&pOptions);
26,289✔
5712
  CHECK_MAKE_NODE(pOptions);
26,289✔
5713

5714
  tstrncpy(pOptions->protoStr, TSDB_BNODE_OPT_PROTO_DFT_STR, TSDB_BNODE_OPT_PROTO_STR_LEN);
26,289✔
5715
  pOptions->proto = TSDB_BNODE_OPT_PROTO_DEFAULT;
26,289✔
5716

5717
  return (SNode*)pOptions;
26,289✔
5718
_err:
×
5719
  return NULL;
×
5720
}
5721

5722
static SNode* setBnodeOptionImpl(SAstCreateContext* pCxt, SNode* pBodeOptions, EBnodeOptionType type, void* pVal,
×
5723
                                 bool alter) {
5724
  CHECK_PARSER_STATUS(pCxt);
×
5725
  SBnodeOptions* pOptions = (SBnodeOptions*)pBodeOptions;
×
5726
  switch (type) {
×
5727
    case BNODE_OPTION_PROTOCOL:
×
5728
      COPY_STRING_FORM_STR_TOKEN(pOptions->protoStr, (SToken*)pVal);
×
5729
      break;
×
5730
    default:
×
5731
      break;
×
5732
  }
5733

5734
  return pBodeOptions;
×
5735
_err:
×
5736
  nodesDestroyNode(pBodeOptions);
×
5737
  return NULL;
×
5738
}
5739

5740
SNode* setBnodeOption(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pOption, void* pVal) {
×
5741
  if (0 == strncasecmp(pOption->z, "protocol", 8)) {
×
5742
    return setBnodeOptionImpl(pCxt, pOptions, BNODE_OPTION_PROTOCOL, pVal, false);
×
5743
  } else {
5744
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5745
    return pOptions;
×
5746
  }
5747
}
5748

5749
SNode* createCreateXnodeWithTokenStmt(SAstCreateContext* pCxt, const SToken* pUrl, SToken* pToken) {
131✔
5750
  CHECK_PARSER_STATUS(pCxt);
131✔
5751
  SCreateXnodeStmt* pStmt = NULL;
131✔
5752
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
131✔
5753
  CHECK_MAKE_NODE(pStmt);
131✔
5754

5755
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
131✔
5756

5757
  if (pToken != NULL) {
131✔
5758
    if (pToken->n <= 2) {
131✔
5759
      pCxt->errCode =
×
5760
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode token should not be empty");
×
5761
      goto _err;
×
5762
    }
5763
    if (pToken->n > TSDB_TOKEN_LEN + 2) {
131✔
5764
      pCxt->errCode =
×
5765
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode token length is illegal");
×
5766
      goto _err;
×
5767
    }
5768
    tstrncpy(pStmt->token, pToken->z + 1, TMIN(pToken->n - 2 + 1, sizeof(pStmt->token)));
131✔
5769
  }
5770
  return (SNode*)pStmt;
131✔
5771
_err:
×
5772
  return NULL;
×
5773
}
5774

5775
SNode* createCreateXnodeWithUserPassStmt(SAstCreateContext* pCxt, const SToken* pUrl, SToken* pUser,
1,182✔
5776
                                         const SToken* pPass) {
5777
  CHECK_PARSER_STATUS(pCxt);
1,182✔
5778
  SCreateXnodeStmt* pStmt = NULL;
1,182✔
5779
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
1,182✔
5780
  CHECK_MAKE_NODE(pStmt);
1,182✔
5781

5782
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
1,182✔
5783

5784
  if (pUser != NULL) {
1,182✔
5785
    CHECK_NAME(checkUserName(pCxt, pUser));
1,182✔
5786
    COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUser);
1,182✔
5787
  }
5788
  if (pPass != NULL) {
1,182✔
5789
    if (pPass->n <= 2) {
1,182✔
5790
      pCxt->errCode =
×
5791
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode password should not be empty");
×
5792
      goto _err;
×
5793
    }
5794
    tstrncpy(pStmt->pass, pPass->z + 1,
1,182✔
5795
             (pPass->n - 2) < sizeof(pStmt->pass) ? (pPass->n - 2) + 1 : sizeof(pStmt->pass));
5796
  }
5797
  return (SNode*)pStmt;
1,182✔
5798
_err:
×
5799
  return NULL;
×
5800
}
5801
SNode* createCreateXnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
1,317✔
5802
  CHECK_PARSER_STATUS(pCxt);
1,317✔
5803
  SCreateXnodeStmt* pStmt = NULL;
1,317✔
5804
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
1,317✔
5805
  CHECK_MAKE_NODE(pStmt);
1,317✔
5806
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
1,317✔
5807
  return (SNode*)pStmt;
1,317✔
5808
_err:
×
5809
  return NULL;
×
5810
}
5811

5812
SNode* createDropXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode, bool force) {
2,496✔
5813
  if (NULL == pXnode) {
2,496✔
5814
    pCxt->errCode =
×
5815
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
5816
    goto _err;
×
5817
  }
5818
  CHECK_PARSER_STATUS(pCxt);
2,496✔
5819
  SDropXnodeStmt* pStmt = NULL;
2,496✔
5820
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_STMT, (SNode**)&pStmt);
2,496✔
5821
  CHECK_MAKE_NODE(pStmt);
2,496✔
5822

5823
  pStmt->force = force;
2,496✔
5824
  if (pXnode->type == TK_NK_STRING) {
2,496✔
5825
    if (pXnode->n <= 2) {
1,971✔
5826
      pCxt->errCode =
×
5827
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode url should not be all be NULL");
×
5828
        goto _err;
×
5829
    }
5830
    COPY_STRING_FORM_STR_TOKEN(pStmt->url, pXnode);
1,971✔
5831
  } else if(pXnode->type == TK_NK_INTEGER) {
525✔
5832
    pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
525✔
5833
  } else {
5834
    pCxt->errCode =
×
5835
      generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id or url should not be all be NULL");
×
5836
  }
5837

5838
  return (SNode*)pStmt;
2,496✔
5839
_err:
×
5840
  return NULL;
×
5841
}
5842

5843
SNode* createDrainXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode) {
394✔
5844
  CHECK_PARSER_STATUS(pCxt);
394✔
5845
  if (NULL == pXnode) {
394✔
5846
    pCxt->errCode =
×
5847
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
5848
    goto _err;
×
5849
  }
5850
  if (pXnode->type != TK_NK_INTEGER) {
394✔
5851
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be an integer");
×
5852
    goto _err;
×
5853
  }
5854

5855
  SDrainXnodeStmt* pStmt = NULL;
394✔
5856
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DRAIN_XNODE_STMT, (SNode**)&pStmt);
394✔
5857
  CHECK_MAKE_NODE(pStmt);
394✔
5858
  pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
394✔
5859
  if (pStmt->xnodeId <= 0) {
394✔
5860
    pCxt->errCode =
×
5861
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be greater than 0");
×
5862
    goto _err;
×
5863
  }
5864

5865
  return (SNode*)pStmt;
394✔
5866
_err:
×
5867
  return NULL;
×
5868
}
5869

5870
SNode* createAlterXnodeStmt(SAstCreateContext* pCxt, const SToken* pToken, const SToken* pUser, const SToken* pPass) {
917✔
5871
  CHECK_PARSER_STATUS(pCxt);
917✔
5872
  SAlterXnodeStmt* pStmt = NULL;
917✔
5873
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_STMT, (SNode**)&pStmt);
917✔
5874
  CHECK_MAKE_NODE(pStmt);
917✔
5875
  if (pToken != NULL) {
917✔
5876
    pStmt->token = xCreateCowStr(pToken->n - 2, pToken->z + 1, true);
262✔
5877
  }
5878
  if (pUser != NULL && pPass != NULL) {
917✔
5879
    if (pUser->n <= 2) {
655✔
5880
      pCxt->errCode =
×
5881
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode user should not be NULL or empty");
×
5882
      goto _err;
×
5883
    }
5884
    if (pPass->n <= 2) {
655✔
5885
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5886
                                              "xnode password should not be NULL or empty");
5887
      goto _err;
×
5888
    }
5889
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
655✔
5890
    COPY_COW_STR_FROM_ID_TOKEN(pStmt->user, pUser);
655✔
5891
    pStmt->pass = xCreateCowStr(pPass->n - 2, pPass->z + 1, true);
655✔
5892
  }
5893

5894
  return (SNode*)pStmt;
917✔
5895
_err:
×
5896
  nodesDestroyNode((SNode*)pStmt);
×
5897
  return NULL;
×
5898
}
5899

5900
EXnodeResourceType setXnodeResourceType(SAstCreateContext* pCxt, const SToken* pResourceId) {
64,330✔
5901
  CHECK_PARSER_STATUS(pCxt);
64,330✔
5902
  const size_t TASK_LEN = 4;
64,330✔
5903
  const size_t TASKS_LEN = 5;
64,330✔
5904
  const size_t AGENT_LEN = 5;
64,330✔
5905
  const size_t AGENTS_LEN = 6;
64,330✔
5906
  const size_t JOB_LEN = 3;
64,330✔
5907
  const size_t JOBS_LEN = 4;
64,330✔
5908

5909
  if (pResourceId->z[0] == '`') {
64,330✔
5910
    if (strncmp(pResourceId->z + 1, "task", TASK_LEN) == 0 || strncmp(pResourceId->z + 1, "tasks", TASKS_LEN) == 0) {
×
5911
      return XNODE_TASK;
×
5912
    }
5913
    if (strncmp(pResourceId->z + 1, "agent", AGENT_LEN) == 0 ||
×
5914
        strncmp(pResourceId->z + 1, "agents", AGENTS_LEN) == 0) {
×
5915
      return XNODE_AGENT;
×
5916
    }
5917
    if (strncmp(pResourceId->z + 1, "job", JOB_LEN) == 0 || strncmp(pResourceId->z + 1, "jobs", JOBS_LEN) == 0) {
×
5918
      return XNODE_JOB;
×
5919
    }
5920

5921
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5922
                                            "Invalid xnode resource type (task/agent) at: %s", pResourceId->z);
×
5923
    goto _err;
×
5924
  }
5925
  if (strncmp(pResourceId->z, "task", TASK_LEN) == 0 || strncmp(pResourceId->z, "tasks", TASKS_LEN) == 0) {
64,330✔
5926
    return XNODE_TASK;
15,723✔
5927
  }
5928
  if (strncmp(pResourceId->z, "agent", AGENT_LEN) == 0 || strncmp(pResourceId->z, "agents", AGENTS_LEN) == 0) {
48,607✔
5929
    return XNODE_AGENT;
9,566✔
5930
  }
5931
  if (strncmp(pResourceId->z, "job", JOB_LEN) == 0 || strncmp(pResourceId->z, "jobs", JOBS_LEN) == 0) {
39,041✔
5932
    return XNODE_JOB;
39,041✔
5933
  }
5934
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5935
                                          "Invalid xnode resource type (task/agent/job) at: %s", pResourceId->z);
×
5936
  goto _err;
×
5937

5938
_err:
×
5939
  return XNODE_UNKNOWN;
×
5940
}
5941
SNode* createXnodeSourceAsDsn(SAstCreateContext* pCxt, const SToken* pToken) {
3,799✔
5942
  SXTaskSource* pSource = NULL;
3,799✔
5943
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
3,799✔
5944
  CHECK_MAKE_NODE(pSource);
3,799✔
5945
  if (pToken == NULL || pToken->n <= 0) {
3,799✔
5946
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5947
                                            "xnode source dsn should not be NULL or empty");
5948
    goto _err;
×
5949
  }
5950
  if (pToken->n > TSDB_XNODE_TASK_SOURCE_LEN) {
3,799✔
5951
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5952
                                            "Invalid xnode source dsn length: %d, max length: %d", pToken->n,
×
5953
                                            TSDB_XNODE_TASK_SOURCE_LEN);
5954
    goto _err;
×
5955
  }
5956
  pSource->source.type = XNODE_TASK_SOURCE_DSN;
3,799✔
5957
  COPY_COW_STR_FROM_STR_TOKEN(pSource->source.cstr, pToken);
3,799✔
5958
  return (SNode*)pSource;
3,799✔
5959
_err:
×
5960
  return NULL;
×
5961
}
5962
SNode* createXnodeSourceAsDatabase(SAstCreateContext* pCxt, const SToken* pToken) {
131✔
5963
  SXTaskSource* pSource = NULL;
131✔
5964
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
131✔
5965
  CHECK_MAKE_NODE(pSource);
131✔
5966
  if (pToken == NULL || pToken->n <= 0) {
131✔
5967
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5968
                                            "xnode source database should not be NULL or empty");
5969
    goto _err;
×
5970
  }
5971
  if (pToken->n > TSDB_XNODE_TASK_SOURCE_LEN) {
131✔
5972
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5973
                                            "Invalid xnode source database length: %d, max length: %d", pToken->n,
×
5974
                                            TSDB_XNODE_TASK_SOURCE_LEN);
5975
    goto _err;
×
5976
  }
5977
  pSource->source.type = XNODE_TASK_SOURCE_DATABASE;
131✔
5978
  COPY_COW_STR_FROM_ID_TOKEN(pSource->source.cstr, pToken);
131✔
5979
  return (SNode*)pSource;
131✔
5980
_err:
×
5981
  return NULL;
×
5982
}
5983
SNode* createXnodeSourceAsTopic(SAstCreateContext* pCxt, const SToken* pToken) {
655✔
5984
  SXTaskSource* pSource = NULL;
655✔
5985
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SOURCE_OPT, (SNode**)&pSource);
655✔
5986
  CHECK_MAKE_NODE(pSource);
655✔
5987
  if (pToken == NULL || pToken->n <= 0) {
655✔
5988
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5989
                                            "xnode source dsn should not be NULL or empty");
5990
    goto _err;
×
5991
  }
5992
  if (pToken->n > TSDB_TOPIC_NAME_LEN) {
655✔
5993
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5994
                                            "Invalid xnode source topic length: %d, max length: %d", pToken->n,
×
5995
                                            TSDB_TOPIC_NAME_LEN);
5996
    goto _err;
×
5997
  }
5998
  pSource->source.type = XNODE_TASK_SOURCE_TOPIC;
655✔
5999
  COPY_COW_STR_FROM_STR_TOKEN(pSource->source.cstr, pToken);
655✔
6000
  return (SNode*)pSource;
655✔
6001
_err:
×
6002
  return NULL;
×
6003
}
6004
SNode* createXnodeSinkAsDsn(SAstCreateContext* pCxt, const SToken* pToken) {
2,096✔
6005
  SXTaskSink* pSink = NULL;
2,096✔
6006
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SINK_OPT, (SNode**)&pSink);
2,096✔
6007
  CHECK_MAKE_NODE(pSink);
2,096✔
6008
  if (pToken == NULL || pToken->n <= 0) {
2,096✔
6009
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6010
                                            "xnode sink dsn should not be NULL or empty");
6011
    goto _err;
×
6012
  }
6013
  if (pToken->n > TSDB_XNODE_TASK_SINK_LEN) {
2,096✔
6014
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6015
                                            "Invalid xnode sink dsn length: %d, max length: %d", pToken->n,
×
6016
                                            TSDB_XNODE_TASK_SINK_LEN);
6017
    goto _err;
×
6018
  }
6019
  pSink->sink.type = XNODE_TASK_SINK_DSN;
2,096✔
6020
  COPY_COW_STR_FROM_STR_TOKEN(pSink->sink.cstr, pToken);
2,096✔
6021
  return (SNode*)pSink;
2,096✔
6022
_err:
×
6023
  return NULL;
×
6024
}
6025
SNode* createXnodeSinkAsDatabase(SAstCreateContext* pCxt, const SToken* pToken) {
2,620✔
6026
  SXTaskSink* pSink = NULL;
2,620✔
6027
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_SINK_OPT, (SNode**)&pSink);
2,620✔
6028
  CHECK_MAKE_NODE(pSink);
2,620✔
6029
  if (pToken == NULL || pToken->n <= 0) {
2,620✔
6030
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6031
                                            "Xnode sink database should not be NULL or empty");
6032
    goto _err;
×
6033
  }
6034
  if (pToken->n > TSDB_XNODE_TASK_SINK_LEN) {
2,620✔
6035
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6036
                                            "Invalid xnode sink database length: %d, max length: %d", pToken->n,
×
6037
                                            TSDB_XNODE_TASK_SINK_LEN);
6038
    goto _err;
×
6039
  }
6040
  pSink->sink.type = XNODE_TASK_SINK_DATABASE;
2,620✔
6041
  if (pToken->type == TK_NK_STRING) {
2,620✔
6042
    COPY_COW_STR_FROM_STR_TOKEN(pSink->sink.cstr, pToken);
×
6043
  } else if (pToken->type == TK_NK_ID) {
2,620✔
6044
    COPY_COW_STR_FROM_ID_TOKEN(pSink->sink.cstr, pToken);
2,620✔
6045
  } else {
6046
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6047
                                            "Invalid xnode sink database type: %d", pToken->type);
×
6048
    goto _err;
×
6049
  }
6050

6051
  return (SNode*)pSink;
2,620✔
6052
_err:
×
6053
  return NULL;
×
6054
}
6055

6056
SNode* createXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pSource,
4,454✔
6057
                                          SNode* pSink, SNode* pNode) {
6058
  SNode* pStmt = NULL;
4,454✔
6059
  if (pResourceName == NULL) {
4,454✔
6060
    pCxt->errCode =
×
6061
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name should not be NULL");
×
6062
    goto _err;
×
6063
  }
6064
  if (pSource == NULL || pSink == NULL) {
4,454✔
6065
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6066
                                            "Xnode task source and sink should not be NULL");
6067
    goto _err;
×
6068
  }
6069
  if (nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT || nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT) {
4,454✔
6070
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6071
                                            "Xnode task source and sink should be valid nodes");
6072
    goto _err;
×
6073
  }
6074
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_TASK_STMT, (SNode**)&pStmt);
4,454✔
6075
  CHECK_MAKE_NODE(pStmt);
4,454✔
6076
  SCreateXnodeTaskStmt* pTaskStmt = (SCreateXnodeTaskStmt*)pStmt;
4,454✔
6077
  if (pResourceName->type == TK_NK_STRING) {
4,454✔
6078
    if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
4,454✔
6079
      pCxt->errCode =
131✔
6080
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
131✔
6081
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_TASK_NAME_LEN);
6082
      goto _err;
131✔
6083
    }
6084
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
4,323✔
6085
  } else if (pResourceName->type == TK_NK_ID) {
×
6086
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
×
6087
  } else {
6088
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode name type: %d",
×
6089
                                            pResourceName->type);
×
6090
    goto _err;
×
6091
  }
6092

6093
  if (pSource != NULL) {
4,323✔
6094
    SXTaskSource* source = (SXTaskSource*)(pSource);
4,323✔
6095
    pTaskStmt->source = source;
4,323✔
6096
  }
6097
  if (pSink != NULL) {
4,323✔
6098
    SXTaskSink* sink = (SXTaskSink*)(pSink);
4,323✔
6099
    pTaskStmt->sink = sink;
4,323✔
6100
  }
6101
  if (pNode != NULL) {
4,323✔
6102
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
4,323✔
6103
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
4,323✔
6104
      pTaskStmt->options = options;
4,323✔
6105
    }
6106
  }
6107
  return (SNode*)pTaskStmt;
4,323✔
6108
_err:
131✔
6109
  if (pStmt != NULL) {
131✔
6110
    nodesDestroyNode(pStmt);
131✔
6111
  }
6112
  if (pNode != NULL) {
131✔
6113
    nodesDestroyNode(pNode);
131✔
6114
  }
6115
  return NULL;
131✔
6116
}
6117

6118
SNode* createXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pOptions) {
2,489✔
6119
  SNode* pStmt = NULL;
2,489✔
6120
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_AGENT_STMT, (SNode**)&pStmt);
2,489✔
6121
  CHECK_MAKE_NODE(pStmt);
2,489✔
6122
  SCreateXnodeAgentStmt* pAgentStmt = (SCreateXnodeAgentStmt*)pStmt;
2,489✔
6123

6124
  if (pOptions != NULL) {
2,489✔
6125
    if (nodeType(pOptions) == QUERY_NODE_XNODE_TASK_OPTIONS) {
2,489✔
6126
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pOptions);
2,489✔
6127
      pAgentStmt->options = options;
2,489✔
6128
    }
6129
  }
6130

6131
  if (pResourceName->type == TK_NK_STRING && pResourceName->n > 2) {
2,489✔
6132
    if (pResourceName->n > TSDB_XNODE_AGENT_NAME_LEN + 2) {
2,489✔
6133
      pCxt->errCode =
×
6134
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6135
                                  "Xnode agent name should be less than %d characters", TSDB_XNODE_AGENT_NAME_LEN);
6136
      goto _err;
×
6137
    }
6138
    COPY_STRING_FORM_STR_TOKEN(pAgentStmt->name, pResourceName);
2,489✔
6139
  } else {
6140
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6141
                                            "Invalid xnode agent name type: %d", pResourceName->type);
×
6142
    goto _err;
×
6143
  }
6144

6145
  return (SNode*)pAgentStmt;
2,489✔
6146
_err:
×
6147
  if (pStmt != NULL) {
×
6148
    nodesDestroyNode(pStmt);
×
6149
  }
6150
  return NULL;
×
6151
}
6152

6153
SNode* createXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResourceName,
6,943✔
6154
                                  SNode* pSource, SNode* pSink, SNode* pOptions) {
6155
  CHECK_PARSER_STATUS(pCxt);
6,943✔
6156

6157
  switch (resourceType) {
6,943✔
6158
    case XNODE_TASK: {
4,454✔
6159
      SNode* rs = createXnodeTaskWithOptionsDirectly(pCxt, pResourceName, pSource, pSink, pOptions);
4,454✔
6160
      if (rs == NULL) {
4,454✔
6161
        goto _err;
131✔
6162
      }
6163
      return rs;
4,323✔
6164
    }
6165
    case XNODE_AGENT: {
2,489✔
6166
      SNode* rs = createXnodeAgentWithOptionsDirectly(pCxt, pResourceName, pOptions);
2,489✔
6167
      if (rs == NULL) {
2,489✔
6168
        goto _err;
×
6169
      }
6170
      return rs;
2,489✔
6171
    }
6172
    default:
×
6173
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6174
                                              "Invalid xnode resource type: %d", resourceType);
6175
      goto _err;
×
6176
  }
6177
_err:
131✔
6178
  nodesDestroyNode(pSource);
131✔
6179
  nodesDestroyNode(pSink);
131✔
6180
  nodesDestroyNode(pOptions);
131✔
6181
  return NULL;
131✔
6182
}
6183

6184
SNode* createStartXnodeTaskStmt(SAstCreateContext* pCxt, const EXnodeResourceType resourceType, SToken* pIdOrName) {
131✔
6185
  SNode* pStmt = NULL;
131✔
6186
  CHECK_PARSER_STATUS(pCxt);
131✔
6187
  if (resourceType != XNODE_TASK) {
131✔
6188
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6189
                                            "Invalid xnode resource type: %d", resourceType);
6190
    goto _err;
×
6191
  }
6192
  if (pIdOrName == NULL || (pIdOrName != NULL && pIdOrName->type != TK_NK_INTEGER && pIdOrName->type != TK_NK_STRING)) {
131✔
6193
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6194
                                            "Xnode task id or name should be an integer or string");
6195
    goto _err;
×
6196
  }
6197

6198
  pCxt->errCode = nodesMakeNode(QUERY_NODE_START_XNODE_TASK_STMT, (SNode**)&pStmt);
131✔
6199
  CHECK_MAKE_NODE(pStmt);
131✔
6200
  SStartXnodeTaskStmt* pTaskStmt = (SStartXnodeTaskStmt*)pStmt;
131✔
6201
  if (pIdOrName->type == TK_NK_INTEGER) {
131✔
6202
    pTaskStmt->tid = taosStr2Int32(pIdOrName->z, NULL, 10);
×
6203
    if (pTaskStmt->tid <= 0) {
×
6204
      pCxt->errCode =
×
6205
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Task id should be greater than 0");
×
6206
      goto _err;
×
6207
    }
6208
  } else {
6209
    if (pIdOrName->n > TSDB_XNODE_RESOURCE_NAME_LEN + 2) {
131✔
6210
      pCxt->errCode =
×
6211
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6212
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_RESOURCE_NAME_LEN);
6213
      goto _err;
×
6214
    }
6215
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
131✔
6216
    COPY_STRING_FORM_STR_TOKEN(buf, pIdOrName);
131✔
6217
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
131✔
6218
  }
6219

6220
  return (SNode*)pTaskStmt;
131✔
6221
_err:
×
6222
  if (pStmt != NULL) {
×
6223
    nodesDestroyNode(pStmt);
×
6224
  }
6225
  return NULL;
×
6226
}
6227

6228
SNode* createStopXnodeTaskStmt(SAstCreateContext* pCxt, const EXnodeResourceType resourceType, SToken* pIdOrName) {
131✔
6229
  SNode* pStmt = NULL;
131✔
6230
  CHECK_PARSER_STATUS(pCxt);
131✔
6231
  if (resourceType != XNODE_TASK) {
131✔
6232
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6233
                                            "Only support stop task, invalid resource type: %d", resourceType);
6234
    goto _err;
×
6235
  }
6236
  if (pIdOrName != NULL && pIdOrName->type != TK_NK_INTEGER && pIdOrName->type != TK_NK_STRING) {
131✔
6237
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6238
                                            "Xnode task id or name should be an integer or string");
6239
    goto _err;
×
6240
  }
6241
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STOP_XNODE_TASK_STMT, (SNode**)&pStmt);
131✔
6242
  CHECK_MAKE_NODE(pStmt);
131✔
6243
  SStopXnodeTaskStmt* pTaskStmt = (SStopXnodeTaskStmt*)pStmt;
131✔
6244
  if (pIdOrName->type == TK_NK_INTEGER) {
131✔
6245
    pTaskStmt->tid = taosStr2Int32(pIdOrName->z, NULL, 10);
×
6246
    if (pTaskStmt->tid <= 0) {
×
6247
      pCxt->errCode =
×
6248
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Task id should be greater than 0");
×
6249
      goto _err;
×
6250
    }
6251
  } else {
6252
    if (pIdOrName->n > TSDB_XNODE_RESOURCE_NAME_LEN + 2) {
131✔
6253
      pCxt->errCode =
×
6254
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6255
                                  "Xnode task name should be less than %d characters", TSDB_XNODE_RESOURCE_NAME_LEN);
6256
      goto _err;
×
6257
    }
6258
    char buf[TSDB_XNODE_RESOURCE_NAME_LEN + 1] = {0};
131✔
6259
    COPY_STRING_FORM_STR_TOKEN(buf, pIdOrName);
131✔
6260
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
131✔
6261
  }
6262

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

6271
SNode* rebalanceXnodeJobWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceId, SNode* pNode) {
262✔
6272
  SNode* pStmt = NULL;
262✔
6273
  if (pResourceId == NULL) {
262✔
6274
    pCxt->errCode =
×
6275
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should not be NULL");
×
6276
    goto _err;
×
6277
  }
6278
  if (pNode == NULL) {
262✔
6279
    pCxt->errCode =
×
6280
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job options should not be NULL");
×
6281
    goto _err;
×
6282
  }
6283
  if (pResourceId->type != TK_NK_INTEGER) {
262✔
6284
    pCxt->errCode =
×
6285
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should be an integer");
×
6286
    goto _err;
×
6287
  }
6288
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_STMT, (SNode**)&pStmt);
262✔
6289
  CHECK_MAKE_NODE(pStmt);
262✔
6290

6291
  SRebalanceXnodeJobStmt* pJobStmt = (SRebalanceXnodeJobStmt*)pStmt;
262✔
6292
  char                    buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
262✔
6293
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceId);
262✔
6294
  pJobStmt->jid = taosStr2Int32(buf, NULL, 10);
262✔
6295

6296
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
262✔
6297
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
262✔
6298
    // printXnodeTaskOptions(&options->opts);
6299
    pJobStmt->options = options;
262✔
6300
  }
6301
  return (SNode*)pJobStmt;
262✔
6302
_err:
×
6303
  return NULL;
×
6304
}
6305

6306
SNode* createRebalanceXnodeJobStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* resourceId,
262✔
6307
                                   SNode* pNodeOptions) {
6308
  CHECK_PARSER_STATUS(pCxt);
262✔
6309

6310
  switch (resourceType) {
262✔
6311
    case XNODE_JOB: {
262✔
6312
      return rebalanceXnodeJobWithOptionsDirectly(pCxt, resourceId, pNodeOptions);
262✔
6313
    }
6314
    default:
×
6315
      pCxt->errCode =
×
6316
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6317
                                  "Invalid xnode resource type: %d, rebalance only support job", resourceType);
6318
      goto _err;
×
6319
  }
6320
_err:
×
6321
  return NULL;
×
6322
}
6323

6324
SNode* rebalanceXnodeJobWhereDirectly(SAstCreateContext* pCxt, SNode* pWhere) {
786✔
6325
  int32_t code = 0;
786✔
6326
  SNode*  pStmt = NULL;
786✔
6327

6328
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_WHERE_STMT, (SNode**)&pStmt);
786✔
6329
  CHECK_MAKE_NODE(pStmt);
786✔
6330

6331
  SRebalanceXnodeJobWhereStmt* pJobStmt = (SRebalanceXnodeJobWhereStmt*)pStmt;
786✔
6332
  pJobStmt->pWhere = pWhere;
786✔
6333

6334
  return (SNode*)pJobStmt;
786✔
6335
_err:
×
6336
  return NULL;
×
6337
}
6338

6339
SNode* createRebalanceXnodeJobWhereStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
786✔
6340
  CHECK_PARSER_STATUS(pCxt);
786✔
6341

6342
  switch (resourceType) {
786✔
6343
    case XNODE_JOB: {
786✔
6344
      return rebalanceXnodeJobWhereDirectly(pCxt, pWhere);
786✔
6345
    }
6346
    default:
×
6347
      pCxt->errCode =
×
6348
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6349
                                  "Invalid xnode resource type: %d, rebalance only support job", resourceType);
6350
      goto _err;
×
6351
  }
6352
_err:
×
6353
  return NULL;
×
6354
}
6355

6356
SNode* updateXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pSource,
524✔
6357
                                          SNode* pSink, SNode* pNode) {
6358
  SNode* pStmt = NULL;
524✔
6359

6360
  if ((pSource != NULL && nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT) ||
524✔
6361
      (pSink != NULL && nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT)) {
262✔
6362
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6363
                                            "Xnode task source and sink should be valid nodes");
6364
    goto _err;
×
6365
  }
6366
  if (pSource == NULL && pSink == NULL && pNode == NULL) {
524✔
6367
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6368
                                            "Xnode task source, sink, and with options can't all be NULL");
6369
    goto _err;
×
6370
  }
6371

6372
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_XNODE_TASK_STMT, (SNode**)&pStmt);
524✔
6373
  CHECK_MAKE_NODE(pStmt);
524✔
6374
  SUpdateXnodeTaskStmt* pTaskStmt = (SUpdateXnodeTaskStmt*)pStmt;
524✔
6375
  if (pResIdOrName->type == TK_NK_INTEGER) {
524✔
6376
    pTaskStmt->tid = taosStr2Int32(pResIdOrName->z, NULL, 10);
131✔
6377
  } else {
6378
    if (pResIdOrName->n <= 2) {
393✔
6379
      pCxt->errCode =
×
6380
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name can't be empty string");
×
6381
      goto _err;
×
6382
    }
6383
    char buf[TSDB_XNODE_TASK_NAME_LEN] = {0};
393✔
6384
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
393✔
6385
    pTaskStmt->name = xCreateCowStr(strlen(buf), buf, true);
393✔
6386
  }
6387

6388
  if (pSource != NULL) {
524✔
6389
    SXTaskSource* source = (SXTaskSource*)(pSource);
131✔
6390
    pTaskStmt->source = source;
131✔
6391
  }
6392
  if (pSink != NULL) {
524✔
6393
    SXTaskSink* sink = (SXTaskSink*)(pSink);
262✔
6394
    pTaskStmt->sink = sink;
262✔
6395
  }
6396
  if (pNode != NULL) {
524✔
6397
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
524✔
6398
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
524✔
6399
      pTaskStmt->options = options;
524✔
6400
    }
6401
  }
6402
  return (SNode*)pTaskStmt;
524✔
6403
_err:
×
6404
  if (pStmt != NULL) {
×
6405
    nodesDestroyNode(pStmt);
×
6406
  }
6407
  return NULL;
×
6408
}
6409

6410
SNode* alterXnodeJobWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pNode) {
131✔
6411
  SNode* pStmt = NULL;
131✔
6412
  if (pResourceName == NULL) {
131✔
6413
    pCxt->errCode =
×
6414
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should not be NULL");
×
6415
    goto _err;
×
6416
  }
6417
  if (pNode == NULL) {
131✔
6418
    pCxt->errCode =
×
6419
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job options should not be NULL");
×
6420
    goto _err;
×
6421
  }
6422
  if (pResourceName->type != TK_NK_INTEGER) {
131✔
6423
    pCxt->errCode =
×
6424
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode job id should be integer");
×
6425
    goto _err;
×
6426
  }
6427
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_JOB_STMT, (SNode**)&pStmt);
131✔
6428
  CHECK_MAKE_NODE(pStmt);
131✔
6429

6430
  SAlterXnodeJobStmt* pJobStmt = (SAlterXnodeJobStmt*)pStmt;
131✔
6431
  char                buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
131✔
6432
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
131✔
6433
  pJobStmt->jid = taosStr2Int32(buf, NULL, 10);
131✔
6434

6435
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
131✔
6436
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
131✔
6437
    pJobStmt->options = options;
131✔
6438
  }
6439
  return (SNode*)pJobStmt;
131✔
6440
_err:
×
6441
  if (pStmt != NULL) {
×
6442
    nodesDestroyNode(pStmt);
×
6443
  }
6444
  return NULL;
×
6445
}
6446

6447
SNode* alterXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pNode) {
393✔
6448
  SNode* pStmt = NULL;
393✔
6449
  if (NULL == pNode) {
393✔
6450
    pCxt->errCode =
×
6451
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode alter agent options can't be null");
×
6452
    goto _err;
×
6453
  }
6454

6455
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_XNODE_AGENT_STMT, (SNode**)&pStmt);
393✔
6456
  CHECK_MAKE_NODE(pStmt);
393✔
6457
  SAlterXnodeAgentStmt* pAgentStmt = (SAlterXnodeAgentStmt*)pStmt;
393✔
6458
  if (pResIdOrName->type == TK_NK_INTEGER) {
393✔
6459
    pAgentStmt->id = taosStr2Int32(pResIdOrName->z, NULL, 10);
×
6460
  } else {
6461
    if (pResIdOrName->n <= 2) {
393✔
6462
      pCxt->errCode =
×
6463
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode alter agent name can't be empty string");
×
6464
      goto _err;
×
6465
    }
6466
    char buf[TSDB_XNODE_AGENT_NAME_LEN] = {0};
393✔
6467
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
393✔
6468
    pAgentStmt->name = xCreateCowStr(strlen(buf), buf, true);
393✔
6469
  }
6470

6471
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
393✔
6472
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
393✔
6473
    pAgentStmt->options = options;
393✔
6474
  }
6475

6476
  return (SNode*)pAgentStmt;
393✔
6477
_err:
×
6478
  if (pStmt != NULL) {
×
6479
    nodesDestroyNode(pStmt);
×
6480
  }
6481
  return NULL;
×
6482
}
6483

6484
SNode* alterXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResIdOrName,
1,048✔
6485
                                 SNode* pSource, SNode* pSink, SNode* pNode) {
6486
  CHECK_PARSER_STATUS(pCxt);
1,048✔
6487

6488
  switch (resourceType) {
1,048✔
6489
    case XNODE_TASK: {
524✔
6490
      return updateXnodeTaskWithOptionsDirectly(pCxt, pResIdOrName, pSource, pSink, pNode);
524✔
6491
    }
6492
    case XNODE_AGENT: {
393✔
6493
      return alterXnodeAgentWithOptionsDirectly(pCxt, pResIdOrName, pNode);
393✔
6494
    }
6495
    case XNODE_JOB: {
131✔
6496
      return alterXnodeJobWithOptionsDirectly(pCxt, pResIdOrName, pNode);
131✔
6497
    }
6498
    default:
×
6499
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6500
                                              "Invalid xnode resource type: %d", resourceType);
6501
      goto _err;
×
6502
  }
6503
_err:
×
6504
  return NULL;
×
6505
}
6506

6507
SNode* dropXnodeResource(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SToken* pResourceName) {
6,288✔
6508
  SNode* pStmt = NULL;
6,288✔
6509
  char   buf[TSDB_XNODE_TASK_NAME_LEN + 1] = {0};
6,288✔
6510

6511
  CHECK_PARSER_STATUS(pCxt);
6,288✔
6512
  if (pResourceName == NULL || pResourceName->n <= 0) {
6,288✔
6513
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6514
                                            "Xnode resource name should not be NULL or empty");
6515
    goto _err;
×
6516
  }
6517
  if (pResourceName->n > TSDB_XNODE_RESOURCE_NAME_LEN) {
6,288✔
6518
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
131✔
6519
                                            "Invalid xnode resource name length: %d, max length: %d", pResourceName->n,
6520
                                            TSDB_XNODE_RESOURCE_NAME_LEN);
6521
    goto _err;
131✔
6522
  }
6523
  switch (resourceType) {
6,157✔
6524
    case XNODE_TASK:
3,537✔
6525
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
3,537✔
6526
      CHECK_MAKE_NODE(pStmt);
3,537✔
6527
      SDropXnodeTaskStmt* pTaskStmt = (SDropXnodeTaskStmt*)pStmt;
3,537✔
6528

6529
      if (pResourceName->type == TK_NK_STRING) {
3,537✔
6530
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
3,537✔
6531
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6532
                                                  "Invalid xnode task name length: %d, max length: %d",
6533
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
6534
          goto _err;
×
6535
        }
6536
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
3,537✔
6537
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
3,537✔
6538
      } else if (pResourceName->type == TK_NK_ID) {
×
6539
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
6540
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
×
6541
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
6542
        pTaskStmt->id = taosStr2Int32(pResourceName->z, NULL, 10);
×
6543
      } else {
6544
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6545
                                                "Invalid xnode job id type: %d", pResourceName->type);
6546
        goto _err;
×
6547
      }
6548
      break;
3,537✔
6549
    case XNODE_AGENT:
2,620✔
6550
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
2,620✔
6551
      CHECK_MAKE_NODE(pStmt);
2,620✔
6552
      SDropXnodeAgentStmt* pDropAgent = (SDropXnodeAgentStmt*)pStmt;
2,620✔
6553

6554
      if (pResourceName->type == TK_NK_STRING) {
2,620✔
6555
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
2,620✔
6556
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6557
                                                  "Invalid xnode task name length: %d, max length: %d",
6558
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
6559
          goto _err;
×
6560
        }
6561
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
2,620✔
6562
        pDropAgent->name = taosStrndupi(buf, sizeof(buf));
2,620✔
6563
      } else if (pResourceName->type == TK_NK_ID) {
×
6564
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
6565
        pDropAgent->name = taosStrndupi(buf, sizeof(buf));
×
6566
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
6567
        pDropAgent->id = taosStr2Int32(pResourceName->z, NULL, 10);
×
6568
      } else {
6569
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6570
                                                "Invalid xnode agent id type: %d", pResourceName->type);
6571
        goto _err;
×
6572
      }
6573
      break;
2,620✔
6574
    case XNODE_JOB:
×
6575
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_JOB_STMT, (SNode**)&pStmt);
×
6576
      CHECK_MAKE_NODE(pStmt);
×
6577
      SDropXnodeJobStmt* pJobStmt = (SDropXnodeJobStmt*)pStmt;
×
6578

6579
      if (pResourceName->type == TK_NK_STRING) {
×
6580
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
×
6581
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
6582
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
×
6583
      } else {
6584
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6585
                                                "Invalid xnode job id type: %d", pResourceName->type);
6586
        goto _err;
×
6587
      }
6588
      break;
×
6589
    default:
×
6590
      break;
×
6591
  }
6592
  return (SNode*)pStmt;
6,157✔
6593
_err:
131✔
6594
  if (pStmt != NULL) {
131✔
6595
    nodesDestroyNode(pStmt);
×
6596
  }
6597
  return NULL;
131✔
6598
}
6599

6600
SNode* dropXnodeResourceWhere(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
21,484✔
6601
  CHECK_PARSER_STATUS(pCxt);
21,484✔
6602
  SDropXnodeJobStmt* pStmt = NULL;
21,484✔
6603
  switch (resourceType) {
21,484✔
6604
    case XNODE_TASK:
×
6605
    case XNODE_AGENT:
6606
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6607
                                              "Xnode only drop xnode job where ... support");
6608
      goto _err;
×
6609
    case XNODE_JOB:
21,484✔
6610
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_JOB_STMT, (SNode**)&pStmt);
21,484✔
6611
      CHECK_MAKE_NODE(pStmt);
21,484✔
6612
      pStmt->pWhere = pWhere;
21,484✔
6613
      break;
21,484✔
6614
    default:
×
6615
      break;
×
6616
  }
6617
  return (SNode*)pStmt;
21,484✔
6618
_err:
×
6619
  nodesDestroyNode(pWhere);
×
6620
  return NULL;
×
6621
}
6622

6623
SNode* createDefaultXnodeTaskOptions(SAstCreateContext* pCxt) {
19,650✔
6624
  CHECK_PARSER_STATUS(pCxt);
19,650✔
6625
  SXnodeTaskOptions* pOptions = NULL;
19,650✔
6626
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_OPTIONS, (SNode**)&pOptions);
19,650✔
6627
  CHECK_MAKE_NODE(pOptions);
19,650✔
6628
  return (SNode*)pOptions;
19,650✔
6629
_err:
×
6630
  return NULL;
×
6631
}
6632

6633
static char   TRIGGER[8] = "trigger";
6634
static SToken TRIGGER_TOKEN = {
6635
    .n = 7,
6636
    .type = TK_NK_ID,
6637
    .z = TRIGGER,
6638
};
6639
SToken* createTriggerToken() { return &TRIGGER_TOKEN; }
×
6640

6641
SNode*  setXnodeTaskOption(SAstCreateContext* pCxt, SNode* pTaskOptions, SToken* pKey, SToken* pVal) {
48,470✔
6642
  CHECK_PARSER_STATUS(pCxt);
48,470✔
6643
  if (pTaskOptions == NULL) {
48,470✔
6644
    pTaskOptions = createDefaultXnodeTaskOptions(pCxt);
19,650✔
6645
    if (pTaskOptions == NULL) {
19,650✔
6646
      pCxt->errCode =
×
6647
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task options should not be NULL");
×
6648
      goto _err;
×
6649
    }
6650
  }
6651
  SXnodeTaskOptions* pOptions = (SXnodeTaskOptions*)pTaskOptions;
48,470✔
6652
  char               key[TSDB_COL_NAME_LEN] = {0};
48,470✔
6653
  if (pKey == NULL) {
48,470✔
6654
    pCxt->errCode =
×
6655
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
6656
    goto _err;
×
6657
  }
6658
  TRIM_STRING_FORM_ID_TOKEN(key, pKey);
48,470✔
6659

6660
  if (strlen(key) == 0) {
48,470✔
6661
    pCxt->errCode =
×
6662
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
6663
    goto _err;
×
6664
  }
6665
  char via[TSDB_COL_NAME_LEN] = {0};
48,470✔
6666
  char buf[TSDB_XNODE_TASK_OPTIONS_MAX_NUM] = {0};
48,470✔
6667
  if (strcmp(key, "trigger") == 0) {
48,470✔
6668
    if (pVal->type == TK_NK_STRING) {
3,537✔
6669
      (void)trimString(pVal->z, pVal->n, pOptions->trigger, sizeof(pOptions->trigger));
3,537✔
6670
      pOptions->triggerLen = pVal->n == 2 ? 1 : pVal->n - 2;
3,537✔
6671
    } else {
6672
      pCxt->errCode =
×
6673
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option trigger must be string");
×
6674
      goto _err;
×
6675
    }
6676
  } else if (strcmp(key, "parser") == 0 || strcmp(key, "transform") == 0) {
44,933✔
6677
    if (pVal->type == TK_NK_STRING) {
3,013✔
6678
      if (pVal->n > TSDB_XNODE_TASK_PARSER_LEN + 2) {
3,013✔
6679
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_MND_XNODE_TASK_PARSER_TOO_LONG,
×
6680
                                                "Option parser must be string with length <= %d",
6681
                                                TSDB_XNODE_TASK_PARSER_LEN);
6682
        goto _err;
×
6683
      }
6684
      if (pOptions->parser) {
3,013✔
6685
        taosMemFreeClear(pOptions->parser);
×
6686
      }
6687
      pOptions->parserLen = pVal->n == 2 ? 1 : pVal->n - 2;
3,013✔
6688
      pOptions->parser = taosMemoryCalloc(1, pOptions->parserLen + 1);
3,013✔
6689
      (void)trimString(pVal->z, pVal->n, pOptions->parser, pOptions->parserLen + 1);
3,013✔
6690
    } else {
6691
      pCxt->errCode =
×
6692
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option parser must be string");
×
6693
      goto _err;
×
6694
    }
6695
  } else if (strcmp(key, "health") == 0) {
41,920✔
6696
    if (pVal->type == TK_NK_STRING) {
×
6697
      (void)trimString(pVal->z, pVal->n, pOptions->health, sizeof(pOptions->health));
×
6698
      pOptions->healthLen = pVal->n == 2 ? 1 : pVal->n - 2;
×
6699
    } else {
6700
      pCxt->errCode =
×
6701
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option health must be string");
×
6702
      goto _err;
×
6703
    }
6704
  } else if (strcmp(key, "via") == 0) {
41,920✔
6705
    switch (pVal->type) {
655✔
6706
      case TK_NK_STRING:
×
6707
        (void)trimString(pVal->z, pVal->n, via, sizeof(via));
×
6708
        pOptions->via = taosStr2Int32(via, NULL, 10);
×
6709
        if (pOptions->via <= 0) {
×
6710
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6711
                                                   "Invalid xnode task option via: %s", pVal->z);
6712
          goto _err;
×
6713
        }
6714
        break;
×
6715
      case TK_NK_INTEGER:
655✔
6716
        pOptions->via = taosStr2Int32(pVal->z, NULL, 10);
655✔
6717
        break;
655✔
6718
      default:
×
6719
        pCxt->errCode =
×
6720
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode task option: %s", key);
×
6721
    }
6722
  } else {
6723
    if (pOptions->optionsNum < TSDB_XNODE_TASK_OPTIONS_MAX_NUM) {
41,265✔
6724
      char* pKeyVal = NULL;
41,265✔
6725
      if (pVal != NULL) {
41,265✔
6726
        parserDebug("key value length expected: %d, actual: %d\n", pKey->n + pVal->n + 2, pKey->n + pVal->n);
41,265✔
6727
        pKeyVal = taosMemoryMalloc(pKey->n + pVal->n + 2);
41,265✔
6728
        memset(pKeyVal, 0, pKey->n + pVal->n + 2);
41,265✔
6729

6730
        CHECK_OUT_OF_MEM(pKeyVal);
41,265✔
6731
        size_t pos = strlen(key);
41,265✔
6732
        memcpy(pKeyVal, key, pos);
41,265✔
6733
        pKeyVal[pos] = '=';  // Add '=' after the key
41,265✔
6734
        pos++;
41,265✔
6735

6736
        if (pVal->type == TK_NK_STRING) {
41,265✔
6737
          (void)trimString(pVal->z, pVal->n, pKeyVal + pos, pVal->n + 1);
25,545✔
6738
        } else {
6739
          TAOS_STRNCPY(pKeyVal + pos, pVal->z, TMIN(pVal->n, pKey->n + pVal->n + 2 - pos - 1));
15,720✔
6740
          pKeyVal[pos + pVal->n] = '\0';
15,720✔
6741
        }
6742
      } else {
6743
        size_t keyLen = strlen(key);
×
6744
        pKeyVal = taosMemoryMalloc(keyLen + 1);
×
6745
        memset(pKeyVal, 0, keyLen + 1);
×
6746
        CHECK_OUT_OF_MEM(pKeyVal);
×
6747
        memcpy(pKeyVal, key, keyLen);
×
6748
      }
6749
      pOptions->options[pOptions->optionsNum] = pKeyVal;
41,265✔
6750
      pOptions->optionsNum++;
41,265✔
6751
    } else {
6752
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6753
                                               "reaches max options number(%d) %s", pOptions->optionsNum, key);
6754
      goto _err;
×
6755
    }
6756
  }
6757
  return pTaskOptions;
48,470✔
6758
_err:
×
6759
  nodesDestroyNode(pTaskOptions);
×
6760
  return NULL;
×
6761
}
6762

6763
SNode* createXnodeTaskJobWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pTidToken,
11,397✔
6764
                                     SNode* pNodeOptions) {
6765
  CHECK_PARSER_STATUS(pCxt);
11,397✔
6766
  SNode* pStmt = NULL;
11,397✔
6767

6768
  switch (resourceType) {
11,397✔
6769
    case XNODE_JOB: {
11,397✔
6770
      if (pTidToken == NULL || pTidToken->n <= 0) {
11,397✔
6771
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6772
                                                "Xnode job task id should not be NULL or empty");
6773
        goto _err;
×
6774
      }
6775
      if (pNodeOptions == NULL || nodeType(pNodeOptions) != QUERY_NODE_XNODE_TASK_OPTIONS) {
11,397✔
6776
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6777
                                                "Xnode job options should not be NULL or empty");
6778
        goto _err;
×
6779
      }
6780
      pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_JOB_STMT, &pStmt);
11,397✔
6781
      CHECK_MAKE_NODE(pStmt);
11,397✔
6782
      SCreateXnodeJobStmt* pJobStmt = (SCreateXnodeJobStmt*)pStmt;
11,397✔
6783
      pJobStmt->options = (SXnodeTaskOptions*)pNodeOptions;
11,397✔
6784
      pJobStmt->tid = taosStr2Int32(pTidToken->z, NULL, 10);
11,397✔
6785
      break;
11,397✔
6786
    }
6787
    default:
×
6788
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6789
                                              "Invalid xnode resource type: %d with ON clause", resourceType);
6790
      goto _err;
×
6791
  }
6792
  return pStmt;
11,397✔
6793
_err:
×
6794
  if (pStmt != NULL) {
×
6795
    nodesDestroyNode(pStmt);
×
6796
  }
6797
  return NULL;
×
6798
}
6799

6800
EPrivType xnodeResourceToPrivType(SAstCreateContext* pCxt, SToken* pResourceId, EPrivType privType) {
×
6801
  CHECK_PARSER_STATUS(pCxt);
×
6802
  const size_t TASK_LEN = 4;
×
6803
  const size_t TASKS_LEN = 5;
×
6804

6805
  if (pResourceId->z[0] == '`') {
×
6806
    if ((pResourceId->n == (2 + TASK_LEN) && strncmp(pResourceId->z + 1, "task", TASK_LEN) == 0) ||
×
6807
        (pResourceId->n == (2 + TASKS_LEN) && strncmp(pResourceId->z + 1, "tasks", TASKS_LEN) == 0)) {
×
6808
      return privType;
×
6809
    }
6810
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6811
                                            "Syntax error: Invalid grant xnode resource type at: %s", pResourceId->z);
6812
    goto _err;
×
6813
  }
6814
  if ((pResourceId->n == TASK_LEN && strncmp(pResourceId->z, "task", TASK_LEN) == 0) ||
×
6815
      (pResourceId->n == TASKS_LEN && strncmp(pResourceId->z, "tasks", TASKS_LEN) == 0)) {
×
6816
    return privType;
×
6817
  }
6818
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6819
                                          "Syntax error: Invalid grant xnode resource type at: %s", pResourceId->z);
6820

6821
_err:
×
6822
  return PRIV_TYPE_UNKNOWN;
×
6823
}
6824

6825
SPrivLevelArgs xnodeTaskObjPrivLevelSet(SAstCreateContext* pCxt, SToken* resId, SPrivLevelArgs privLevelArgs) {
×
6826
  (void)xnodeResourceToPrivType(pCxt, resId, PRIV_TYPE_UNKNOWN);
×
6827
  CHECK_PARSER_STATUS(pCxt);
×
6828

6829
_err:
×
6830
  return privLevelArgs;
×
6831
}
6832

6833
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
1,777✔
6834
  SToken config;
1,777✔
6835
  config.type = TK_NK_STRING;
1,777✔
6836
  config.z = "\"encrypt_key\"";
1,777✔
6837
  config.n = strlen(config.z);
1,777✔
6838
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
1,777✔
6839
}
6840

6841
SNode* createAlterEncryptKeyStmt(SAstCreateContext* pCxt, int8_t keyType, const SToken* pValue) {
308✔
6842
  CHECK_PARSER_STATUS(pCxt);
308✔
6843
  SAlterEncryptKeyStmt* pStmt = NULL;
308✔
6844
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ENCRYPT_KEY_STMT, (SNode**)&pStmt);
308✔
6845
  CHECK_MAKE_NODE(pStmt);
308✔
6846

6847
  pStmt->keyType = keyType;
308✔
6848
  if (NULL != pValue) {
308✔
6849
    (void)trimString(pValue->z, pValue->n, pStmt->newKey, sizeof(pStmt->newKey));
308✔
6850
  }
6851

6852
  return (SNode*)pStmt;
308✔
6853
_err:
×
6854
  return NULL;
×
6855
}
6856

6857
SNode* createAlterKeyExpirationStmt(SAstCreateContext* pCxt, const SToken* pDays, const SToken* pStrategy) {
×
6858
  CHECK_PARSER_STATUS(pCxt);
×
6859
  SAlterKeyExpirationStmt* pStmt = NULL;
×
6860
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_KEY_EXPIRATION_STMT, (SNode**)&pStmt);
×
6861
  CHECK_MAKE_NODE(pStmt);
×
6862

6863
  if (NULL != pDays) {
×
6864
    pStmt->days = taosStr2Int32(pDays->z, NULL, 10);
×
6865
  }
6866
  if (NULL != pStrategy) {
×
6867
    (void)trimString(pStrategy->z, pStrategy->n, pStmt->strategy, sizeof(pStmt->strategy));
×
6868
  }
6869

6870
  return (SNode*)pStmt;
×
6871
_err:
×
6872
  return NULL;
×
6873
}
6874

6875
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
11,748✔
6876
  if (!checkIndexName(pCxt, pIndexName)) {
11,748✔
6877
    return NULL;
×
6878
  }
6879
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
11,748✔
6880
}
6881

6882
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
22,861✔
6883
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
6884
  CHECK_PARSER_STATUS(pCxt);
22,861✔
6885
  SCreateIndexStmt* pStmt = NULL;
22,861✔
6886
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
22,861✔
6887
  CHECK_MAKE_NODE(pStmt);
22,861✔
6888
  pStmt->indexType = type;
22,861✔
6889
  pStmt->ignoreExists = ignoreExists;
22,861✔
6890

6891
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
22,861✔
6892
  if (strlen(pFullTable->table.dbName) == 0) {
22,861✔
6893
    // no db specified,
6894
    if (pCxt->pQueryCxt->db == NULL) {
×
6895
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
6896
      CHECK_PARSER_STATUS(pCxt);
×
6897
    } else {
6898
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
6899
    }
6900
  } else {
6901
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
22,861✔
6902
  }
6903
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
22,861✔
6904
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
22,861✔
6905
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
22,861✔
6906
  nodesDestroyNode(pIndexName);
22,861✔
6907
  nodesDestroyNode(pRealTable);
22,861✔
6908
  pStmt->pCols = pCols;
22,861✔
6909
  pStmt->pOptions = (SIndexOptions*)pOptions;
22,861✔
6910
  return (SNode*)pStmt;
22,861✔
6911
_err:
×
6912
  nodesDestroyNode(pIndexName);
×
6913
  nodesDestroyNode(pRealTable);
×
6914
  nodesDestroyNode(pOptions);
×
6915
  nodesDestroyList(pCols);
×
6916
  return NULL;
×
6917
}
6918

6919
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
×
6920
                         SNode* pStreamOptions) {
6921
  CHECK_PARSER_STATUS(pCxt);
×
6922
  SIndexOptions* pOptions = NULL;
×
6923
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
×
6924
  CHECK_MAKE_NODE(pOptions);
×
6925
  pOptions->pFuncs = pFuncs;
×
6926
  pOptions->pInterval = pInterval;
×
6927
  pOptions->pOffset = pOffset;
×
6928
  pOptions->pSliding = pSliding;
×
6929
  pOptions->pStreamOptions = pStreamOptions;
×
6930
  return (SNode*)pOptions;
×
6931
_err:
×
6932
  nodesDestroyNode(pInterval);
×
6933
  nodesDestroyNode(pOffset);
×
6934
  nodesDestroyNode(pSliding);
×
6935
  nodesDestroyNode(pStreamOptions);
×
6936
  return NULL;
×
6937
}
6938

6939
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
11,748✔
6940
  CHECK_PARSER_STATUS(pCxt);
11,748✔
6941
  SDropIndexStmt* pStmt = NULL;
11,748✔
6942
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
11,748✔
6943
  CHECK_MAKE_NODE(pStmt);
11,748✔
6944
  pStmt->ignoreNotExists = ignoreNotExists;
11,748✔
6945
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
11,748✔
6946
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
11,748✔
6947
  nodesDestroyNode(pIndexName);
11,748✔
6948
  return (SNode*)pStmt;
11,748✔
6949
_err:
×
6950
  nodesDestroyNode(pIndexName);
×
6951
  return NULL;
×
6952
}
6953

6954
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
122,194✔
6955
  CHECK_PARSER_STATUS(pCxt);
122,194✔
6956
  SCreateComponentNodeStmt* pStmt = NULL;
122,194✔
6957
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
122,194✔
6958
  CHECK_MAKE_NODE(pStmt);
122,194✔
6959
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
122,194✔
6960
  return (SNode*)pStmt;
122,194✔
6961
_err:
×
6962
  return NULL;
×
6963
}
6964

6965
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
68,476✔
6966
  CHECK_PARSER_STATUS(pCxt);
68,476✔
6967
  SDropComponentNodeStmt* pStmt = NULL;
68,476✔
6968
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
68,476✔
6969
  CHECK_MAKE_NODE(pStmt);
68,476✔
6970
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
68,476✔
6971
  return (SNode*)pStmt;
68,476✔
6972
_err:
×
6973
  return NULL;
×
6974
}
6975

6976
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
2,709✔
6977
  CHECK_PARSER_STATUS(pCxt);
2,709✔
6978
  SRestoreComponentNodeStmt* pStmt = NULL;
2,709✔
6979
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,709✔
6980
  CHECK_MAKE_NODE(pStmt);
2,709✔
6981
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
2,709✔
6982
  pStmt->vgId = 0;
2,709✔
6983
  return (SNode*)pStmt;
2,709✔
6984
_err:
×
6985
  return NULL;
×
6986
}
6987

6988
SNode* createRestoreComponentNodeStmtWithVgId(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId, const SToken* pVgId) {
721✔
6989
  SRestoreComponentNodeStmt* pStmt = (SRestoreComponentNodeStmt*)createRestoreComponentNodeStmt(pCxt, type, pDnodeId);
721✔
6990
  if (pStmt == NULL) {
721✔
NEW
6991
    return NULL;
×
6992
  }
6993

6994
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
721✔
6995
  return (SNode*)pStmt;
721✔
6996
}
6997

6998
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery, bool reload) {
145,212✔
6999
  CHECK_PARSER_STATUS(pCxt);
145,212✔
7000
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
145,212✔
7001
  SCreateTopicStmt* pStmt = NULL;
144,623✔
7002
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
144,623✔
7003
  CHECK_MAKE_NODE(pStmt);
144,623✔
7004
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
144,623✔
7005
  pStmt->ignoreExists = ignoreExists;
144,623✔
7006
  pStmt->pQuery = pQuery;
144,623✔
7007
  pStmt->reload = reload;
144,623✔
7008
  return (SNode*)pStmt;
144,623✔
7009
_err:
589✔
7010
  nodesDestroyNode(pQuery);
589✔
7011
  return NULL;
589✔
7012
}
7013

7014
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
21,857✔
7015
                                  int8_t withMeta) {
7016
  CHECK_PARSER_STATUS(pCxt);
21,857✔
7017
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
21,857✔
7018
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
21,857✔
7019
  SCreateTopicStmt* pStmt = NULL;
21,857✔
7020
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
21,857✔
7021
  CHECK_MAKE_NODE(pStmt);
21,857✔
7022
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
21,857✔
7023
  pStmt->ignoreExists = ignoreExists;
21,857✔
7024
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
21,857✔
7025
  pStmt->withMeta = withMeta;
21,857✔
7026
  return (SNode*)pStmt;
21,857✔
7027
_err:
×
7028
  return NULL;
×
7029
}
7030

7031
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
17,539✔
7032
                                     int8_t withMeta, SNode* pWhere) {
7033
  CHECK_PARSER_STATUS(pCxt);
17,539✔
7034
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
17,539✔
7035
  SCreateTopicStmt* pStmt = NULL;
17,539✔
7036
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
17,539✔
7037
  CHECK_MAKE_NODE(pStmt);
17,539✔
7038
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
17,539✔
7039
  pStmt->ignoreExists = ignoreExists;
17,539✔
7040
  pStmt->withMeta = withMeta;
17,539✔
7041
  pStmt->pWhere = pWhere;
17,539✔
7042

7043
  tstrncpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
17,539✔
7044
  tstrncpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
17,539✔
7045
  nodesDestroyNode(pRealTable);
17,539✔
7046
  return (SNode*)pStmt;
17,539✔
7047
_err:
×
7048
  nodesDestroyNode(pRealTable);
×
7049
  nodesDestroyNode(pWhere);
×
7050
  return NULL;
×
7051
}
7052

7053
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName, bool force) {
109,241✔
7054
  CHECK_PARSER_STATUS(pCxt);
109,241✔
7055
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
109,241✔
7056
  SDropTopicStmt* pStmt = NULL;
109,029✔
7057
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
109,029✔
7058
  CHECK_MAKE_NODE(pStmt);
109,029✔
7059
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
109,029✔
7060
  pStmt->ignoreNotExists = ignoreNotExists;
109,029✔
7061
  pStmt->force = force;
109,029✔
7062
  return (SNode*)pStmt;
109,029✔
7063
_err:
212✔
7064
  return NULL;
212✔
7065
}
7066

7067
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName,
2,146✔
7068
                            bool force) {
7069
  CHECK_PARSER_STATUS(pCxt);
2,146✔
7070
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
2,146✔
7071
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
2,146✔
7072
  SDropCGroupStmt* pStmt = NULL;
2,146✔
7073
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
2,146✔
7074
  CHECK_MAKE_NODE(pStmt);
2,146✔
7075
  pStmt->ignoreNotExists = ignoreNotExists;
2,146✔
7076
  pStmt->force = force;
2,146✔
7077
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
2,146✔
7078
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
2,146✔
7079
  return (SNode*)pStmt;
2,146✔
7080
_err:
×
7081
  return NULL;
×
7082
}
7083

7084
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
972✔
7085
  CHECK_PARSER_STATUS(pCxt);
972✔
7086
  SAlterClusterStmt* pStmt = NULL;
972✔
7087
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
972✔
7088
  CHECK_MAKE_NODE(pStmt);
972✔
7089
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
972✔
7090
  if (NULL != pValue) {
972✔
7091
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
972✔
7092
  }
7093
  return (SNode*)pStmt;
972✔
7094
_err:
×
7095
  return NULL;
×
7096
}
7097

7098
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
487,238✔
7099
  CHECK_PARSER_STATUS(pCxt);
487,238✔
7100
  SAlterLocalStmt* pStmt = NULL;
487,238✔
7101
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
487,238✔
7102
  CHECK_MAKE_NODE(pStmt);
487,238✔
7103
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
487,238✔
7104
  if (NULL != pValue) {
487,238✔
7105
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
480,801✔
7106
  }
7107
  return (SNode*)pStmt;
487,238✔
7108
_err:
×
7109
  return NULL;
×
7110
}
7111

7112
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
105,388,987✔
7113
  CHECK_PARSER_STATUS(pCxt);
105,388,987✔
7114
  SExplainOptions* pOptions = NULL;
105,388,987✔
7115
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
105,388,987✔
7116
  CHECK_MAKE_NODE(pOptions);
105,392,537✔
7117
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
105,392,537✔
7118
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
105,392,537✔
7119
  return (SNode*)pOptions;
105,392,537✔
7120
_err:
×
7121
  return NULL;
×
7122
}
7123

7124
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
57,379,421✔
7125
  CHECK_PARSER_STATUS(pCxt);
57,379,421✔
7126
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
57,379,421✔
7127
  return pOptions;
57,379,421✔
7128
_err:
×
7129
  return NULL;
×
7130
}
7131

7132
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
97,280✔
7133
  CHECK_PARSER_STATUS(pCxt);
97,280✔
7134
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
97,280✔
7135
  return pOptions;
97,280✔
7136
_err:
×
7137
  return NULL;
×
7138
}
7139

7140
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
96,946,922✔
7141
  CHECK_PARSER_STATUS(pCxt);
96,946,922✔
7142
  SExplainStmt* pStmt = NULL;
96,946,922✔
7143
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
96,946,922✔
7144
  CHECK_MAKE_NODE(pStmt);
96,962,969✔
7145
  pStmt->analyze = analyze;
96,962,969✔
7146
  pStmt->pOptions = (SExplainOptions*)pOptions;
96,962,969✔
7147
  pStmt->pQuery = pQuery;
96,962,969✔
7148
  return (SNode*)pStmt;
96,962,969✔
7149
_err:
×
7150
  nodesDestroyNode(pOptions);
×
7151
  nodesDestroyNode(pQuery);
×
7152
  return NULL;
×
7153
}
7154

7155
SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
557,036✔
7156
  CHECK_PARSER_STATUS(pCxt);
557,036✔
7157
  SDescribeStmt* pStmt = NULL;
557,071✔
7158
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT, (SNode**)&pStmt);
557,071✔
7159
  CHECK_MAKE_NODE(pStmt);
557,071✔
7160
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
557,071✔
7161
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
557,036✔
7162
  nodesDestroyNode(pRealTable);
557,071✔
7163
  return (SNode*)pStmt;
557,036✔
7164
_err:
×
7165
  nodesDestroyNode(pRealTable);
×
7166
  return NULL;
×
7167
}
7168

7169
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
2,161,201✔
7170
  CHECK_PARSER_STATUS(pCxt);
2,161,201✔
7171
  SNode* pStmt = NULL;
2,161,201✔
7172
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
2,161,201✔
7173
  CHECK_MAKE_NODE(pStmt);
2,161,201✔
7174
  return pStmt;
2,161,201✔
7175
_err:
×
7176
  return NULL;
×
7177
}
7178

7179
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
16,363✔
7180
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
16,363✔
7181
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
12,187✔
7182
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
4,176✔
7183
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
4,176✔
7184
  } else {
7185
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7186
                                            "udf programming language supports c and python");
7187
  }
7188
  return pCxt->errCode;
16,363✔
7189
}
7190

7191
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
16,363✔
7192
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
7193
                                bool orReplace) {
7194
  CHECK_PARSER_STATUS(pCxt);
16,363✔
7195
  if (pLibPath->n <= 2) {
16,363✔
7196
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
7197
    CHECK_PARSER_STATUS(pCxt);
×
7198
  }
7199
  int8_t language = 0;
16,363✔
7200
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
16,363✔
7201
  CHECK_PARSER_STATUS(pCxt);
16,363✔
7202
  SCreateFunctionStmt* pStmt = NULL;
16,363✔
7203
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
16,363✔
7204
  CHECK_MAKE_NODE(pStmt);
16,363✔
7205
  pStmt->orReplace = orReplace;
16,363✔
7206
  pStmt->ignoreExists = ignoreExists;
16,363✔
7207
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
16,363✔
7208
  pStmt->isAgg = aggFunc;
16,363✔
7209
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
16,363✔
7210
  pStmt->outputDt = dataType;
16,363✔
7211
  pStmt->bufSize = bufSize;
16,363✔
7212
  pStmt->language = language;
16,363✔
7213
  return (SNode*)pStmt;
16,363✔
7214
_err:
×
7215
  return NULL;
×
7216
}
7217

7218
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
9,002✔
7219
  CHECK_PARSER_STATUS(pCxt);
9,002✔
7220
  SDropFunctionStmt* pStmt = NULL;
9,002✔
7221
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
9,002✔
7222
  CHECK_MAKE_NODE(pStmt);
9,002✔
7223
  pStmt->ignoreNotExists = ignoreNotExists;
9,002✔
7224
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
9,002✔
7225
  return (SNode*)pStmt;
9,002✔
7226
_err:
×
7227
  return NULL;
×
7228
}
7229

7230
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
225,943✔
7231
  SCreateViewStmt* pStmt = NULL;
225,943✔
7232
  CHECK_PARSER_STATUS(pCxt);
225,943✔
7233
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
225,943✔
7234
  CHECK_MAKE_NODE(pStmt);
225,943✔
7235
  int32_t i = pAs->n;
225,943✔
7236
  while (isspace(*(pAs->z + i))) {
451,886✔
7237
    ++i;
225,943✔
7238
  }
7239
  pStmt->pQuerySql = tstrdup(pAs->z + i);
225,943✔
7240
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
225,943✔
7241
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
225,943✔
7242
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
225,943✔
7243
  nodesDestroyNode(pView);
225,943✔
7244
  pStmt->orReplace = orReplace;
225,943✔
7245
  pStmt->pQuery = pQuery;
225,943✔
7246
  return (SNode*)pStmt;
225,943✔
7247
_err:
×
7248
  nodesDestroyNode(pView);
×
7249
  nodesDestroyNode(pQuery);
×
7250
  nodesDestroyNode((SNode*)pStmt);
×
7251
  return NULL;
×
7252
}
7253

7254
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
169,248✔
7255
  CHECK_PARSER_STATUS(pCxt);
169,248✔
7256
  SDropViewStmt* pStmt = NULL;
168,824✔
7257
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
168,824✔
7258
  CHECK_MAKE_NODE(pStmt);
168,824✔
7259
  pStmt->ignoreNotExists = ignoreNotExists;
168,824✔
7260
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
168,824✔
7261
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
168,824✔
7262
  nodesDestroyNode(pView);
168,824✔
7263
  return (SNode*)pStmt;
168,824✔
7264
_err:
424✔
7265
  nodesDestroyNode(pView);
424✔
7266
  return NULL;
424✔
7267
}
7268

7269
SNode* createStreamOutTableNode(SAstCreateContext* pCxt, SNode* pIntoTable, SNode* pOutputSubTable, SNodeList* pColList,
479,162✔
7270
                                SNodeList* pTagList, int32_t nodelayCreateSubtable) {
7271
  SStreamOutTableNode* pOutTable = NULL;
479,162✔
7272
  CHECK_PARSER_STATUS(pCxt);
479,162✔
7273
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OUT_TABLE, (SNode**)&pOutTable);
478,950✔
7274
  CHECK_MAKE_NODE(pOutTable);
478,950✔
7275
  pOutTable->pOutTable = pIntoTable;
478,950✔
7276
  pOutTable->pSubtable = pOutputSubTable;
478,950✔
7277
  pOutTable->pCols = pColList;
478,950✔
7278
  pOutTable->pTags = pTagList;
478,950✔
7279
  pOutTable->nodelayCreateSubtable = (nodelayCreateSubtable != 0) ? 1 : 0;
478,950✔
7280
  return (SNode*)pOutTable;
478,950✔
7281

7282
_err:
212✔
7283
  nodesDestroyNode((SNode*)pOutTable);
212✔
7284
  nodesDestroyNode(pIntoTable);
212✔
7285
  nodesDestroyNode(pOutputSubTable);
212✔
7286
  nodesDestroyList(pColList);
212✔
7287
  nodesDestroyList(pTagList);
212✔
7288
  return NULL;
212✔
7289
}
7290

7291
SNode* createStreamTriggerNode(SAstCreateContext* pCxt, SNode* pTriggerWindow, SNode* pTriggerTable,
486,151✔
7292
                               SNodeList* pPartitionList, SNode* pOptions, SNode* pNotification) {
7293
  SStreamTriggerNode* pTrigger = NULL;
486,151✔
7294
  CHECK_PARSER_STATUS(pCxt);
486,151✔
7295
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER, (SNode**)&pTrigger);
486,069✔
7296
  CHECK_MAKE_NODE(pTrigger);
486,069✔
7297

7298
  pTrigger->pOptions = pOptions;
486,069✔
7299
  pTrigger->pNotify = pNotification;
486,069✔
7300
  pTrigger->pTrigerTable = pTriggerTable;
486,069✔
7301
  pTrigger->pPartitionList = pPartitionList;
486,069✔
7302
  pTrigger->pTriggerWindow = pTriggerWindow;
486,069✔
7303
  return (SNode*)pTrigger;
486,069✔
7304

7305
_err:
82✔
7306
  nodesDestroyNode((SNode*)pTrigger);
82✔
7307
  nodesDestroyNode(pTriggerWindow);
82✔
7308
  nodesDestroyNode(pTriggerTable);
82✔
7309
  nodesDestroyNode(pOptions);
82✔
7310
  nodesDestroyNode(pNotification);
82✔
7311
  nodesDestroyList(pPartitionList);
82✔
7312
  return NULL;
82✔
7313
}
7314

7315
SNode* createSlidingWindowNode(SAstCreateContext* pCxt, SNode* pSlidingVal, SNode* pOffset) {
214,279✔
7316
  SSlidingWindowNode* pSliding = NULL;
214,279✔
7317
  CHECK_PARSER_STATUS(pCxt);
214,279✔
7318
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SLIDING_WINDOW, (SNode**)&pSliding);
214,279✔
7319
  CHECK_MAKE_NODE(pSliding);
214,279✔
7320
  pSliding->pSlidingVal = pSlidingVal;
214,279✔
7321
  pSliding->pOffset = pOffset;
214,279✔
7322
  return (SNode*)pSliding;
214,279✔
7323
_err:
×
7324
  nodesDestroyNode(pSlidingVal);
×
7325
  nodesDestroyNode(pOffset);
×
7326
  nodesDestroyNode((SNode*)pSliding);
×
7327
  return NULL;
×
7328
}
7329

7330
SNode* createStreamTriggerOptions(SAstCreateContext* pCxt) {
166,834✔
7331
  SStreamTriggerOptions* pOptions = NULL;
166,834✔
7332
  CHECK_PARSER_STATUS(pCxt);
166,834✔
7333
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER_OPTIONS, (SNode**)&pOptions);
166,834✔
7334
  CHECK_MAKE_NODE(pOptions);
166,834✔
7335
  pOptions->pPreFilter = NULL;
166,834✔
7336
  pOptions->pWaterMark = NULL;
166,834✔
7337
  pOptions->pMaxDelay = NULL;
166,834✔
7338
  pOptions->pExpiredTime = NULL;
166,834✔
7339
  pOptions->pFillHisStartTime = NULL;
166,834✔
7340
  pOptions->pIdleTimeout = NULL;
166,834✔
7341
  pOptions->pEventType = EVENT_NONE;
166,834✔
7342
  pOptions->calcNotifyOnly = false;
166,834✔
7343
  pOptions->deleteOutputTable = false;
166,834✔
7344
  pOptions->deleteRecalc = false;
166,834✔
7345
  pOptions->fillHistory = false;
166,834✔
7346
  pOptions->fillHistoryFirst = false;
166,834✔
7347
  pOptions->lowLatencyCalc = false;
166,834✔
7348
  pOptions->forceOutput = false;
166,834✔
7349
  pOptions->ignoreDisorder = false;
166,834✔
7350
  pOptions->ignoreNoDataTrigger = false;
166,834✔
7351
  return (SNode*)pOptions;
166,834✔
7352
_err:
×
7353
  nodesDestroyNode((SNode*)pOptions);
×
7354
  return NULL;
×
7355
}
7356

7357
SNode* createStreamTagDefNode(SAstCreateContext* pCxt, SToken* pTagName, SDataType dataType, SNode* tagExpression) {
40,846✔
7358
  SStreamTagDefNode* pTagDef = NULL;
40,846✔
7359
  CHECK_PARSER_STATUS(pCxt);
40,846✔
7360
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TAG_DEF, (SNode**)&pTagDef);
40,846✔
7361
  CHECK_MAKE_NODE(pTagDef);
40,846✔
7362
  COPY_STRING_FORM_ID_TOKEN(pTagDef->tagName, pTagName);
40,846✔
7363
  int32_t nameLen = strdequote(pTagDef->tagName);
40,846✔
7364
  pTagDef->tagName[nameLen] = '\0';
40,846✔
7365
  pTagDef->dataType = dataType;
40,846✔
7366
  pTagDef->pTagExpr = tagExpression;
40,846✔
7367
  return (SNode*)pTagDef;
40,846✔
7368
_err:
×
7369
  nodesDestroyNode(tagExpression);
×
7370
  nodesDestroyNode((SNode*)pTagDef);
×
7371
  return NULL;
×
7372
}
7373

7374
SNode* setStreamTriggerOptions(SAstCreateContext* pCxt, SNode* pOptions, SStreamTriggerOption* pOptionUnit) {
218,865✔
7375
  CHECK_PARSER_STATUS(pCxt);
218,865✔
7376
  SStreamTriggerOptions* pStreamOptions = (SStreamTriggerOptions*)pOptions;
218,865✔
7377
  switch (pOptionUnit->type) {
218,865✔
7378
    case STREAM_TRIGGER_OPTION_CALC_NOTIFY_ONLY:
2,730✔
7379
      if (pStreamOptions->calcNotifyOnly) {
2,730✔
7380
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7381
                                                "CALC_NOTIFY_ONLY specified multiple times");
7382
        goto _err;
×
7383
      }
7384
      pStreamOptions->calcNotifyOnly = true;
2,730✔
7385
      break;
2,730✔
7386
    case STREAM_TRIGGER_OPTION_DELETE_OUTPUT_TABLE:
784✔
7387
      if (pStreamOptions->deleteOutputTable) {
784✔
7388
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7389
                                                "DELETE_OUTPUT_TABLE specified multiple times");
7390
        goto _err;
×
7391
      }
7392
      pStreamOptions->deleteOutputTable = true;
784✔
7393
      break;
784✔
7394
    case STREAM_TRIGGER_OPTION_DELETE_RECALC:
10,471✔
7395
      if (pStreamOptions->deleteRecalc) {
10,471✔
7396
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7397
                                                "DELETE_RECALC specified multiple times");
7398
        goto _err;
×
7399
      }
7400
      pStreamOptions->deleteRecalc = true;
10,471✔
7401
      break;
10,471✔
7402
    case STREAM_TRIGGER_OPTION_EXPIRED_TIME:
10,226✔
7403
      if (pStreamOptions->pExpiredTime != NULL) {
10,226✔
7404
        pCxt->errCode =
×
7405
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EXPIRED_TIME specified multiple times");
×
7406
        goto _err;
×
7407
      }
7408
      pStreamOptions->pExpiredTime = pOptionUnit->pNode;
10,226✔
7409
      break;
10,226✔
7410
    case STREAM_TRIGGER_OPTION_FORCE_OUTPUT:
4,316✔
7411
      if (pStreamOptions->forceOutput) {
4,316✔
7412
        pCxt->errCode =
×
7413
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FORCE_OUTPUT specified multiple times");
×
7414
        goto _err;
×
7415
      }
7416
      pStreamOptions->forceOutput = true;
4,316✔
7417
      break;
4,316✔
7418
    case STREAM_TRIGGER_OPTION_FILL_HISTORY:
46,712✔
7419
      if (pStreamOptions->fillHistoryFirst) {
46,712✔
7420
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7421
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
7422
        goto _err;
×
7423
      }
7424
      if (pStreamOptions->pFillHisStartTime != NULL) {
46,712✔
7425
        pCxt->errCode =
×
7426
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FILL_HISTORY specified multiple times");
×
7427
        goto _err;
×
7428
      }
7429
      pStreamOptions->fillHistory = true;
46,712✔
7430
      if (pOptionUnit->pNode == NULL) {
46,712✔
7431
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
19,728✔
7432
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
19,728✔
7433
      } else {
7434
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
26,984✔
7435
      }
7436
      break;
46,712✔
7437
    case STREAM_TRIGGER_OPTION_FILL_HISTORY_FIRST:
4,367✔
7438
      if (pStreamOptions->fillHistory) {
4,367✔
7439
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
892✔
7440
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
7441
        goto _err;
892✔
7442
      }
7443
      if (pStreamOptions->pFillHisStartTime != NULL) {
3,475✔
7444
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7445
                                                "FILL_HISTORY_FIRST specified multiple times");
7446
        goto _err;
×
7447
      }
7448
      pStreamOptions->fillHistoryFirst = true;
3,475✔
7449
      if (pOptionUnit->pNode == NULL) {
3,475✔
7450
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
500✔
7451
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
500✔
7452
      } else {
7453
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
2,975✔
7454
      }
7455
      break;
3,475✔
7456
    case STREAM_TRIGGER_OPTION_IGNORE_DISORDER:
24,662✔
7457
      if (pStreamOptions->ignoreDisorder) {
24,662✔
7458
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
392✔
7459
                                                "IGNORE_DISORDER specified multiple times");
7460
        goto _err;
392✔
7461
      }
7462
      pStreamOptions->ignoreDisorder = true;
24,270✔
7463
      break;
24,270✔
7464
    case STREAM_TRIGGER_OPTION_LOW_LATENCY_CALC:
13,813✔
7465
      if (pStreamOptions->lowLatencyCalc) {
13,813✔
7466
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7467
                                                "LOW_LATENCY_CALC specified multiple times");
7468
        goto _err;
×
7469
      }
7470
      pStreamOptions->lowLatencyCalc = true;
13,813✔
7471
      break;
13,813✔
7472
    case STREAM_TRIGGER_OPTION_MAX_DELAY:
10,500✔
7473
      if (pStreamOptions->pMaxDelay != NULL) {
10,500✔
7474
        pCxt->errCode =
×
7475
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "MAX_DELAY specified multiple times");
×
7476
        goto _err;
×
7477
      }
7478
      pStreamOptions->pMaxDelay = pOptionUnit->pNode;
10,500✔
7479
      break;
10,500✔
7480
    case STREAM_TRIGGER_OPTION_WATERMARK:
11,024✔
7481
      if (pStreamOptions->pWaterMark != NULL) {
11,024✔
7482
        pCxt->errCode =
×
7483
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "WATERMARK specified multiple times");
×
7484
        goto _err;
×
7485
      }
7486
      pStreamOptions->pWaterMark = pOptionUnit->pNode;
11,024✔
7487
      break;
11,024✔
7488
    case STREAM_TRIGGER_OPTION_PRE_FILTER:
17,951✔
7489
      if (pStreamOptions->pPreFilter != NULL) {
17,951✔
7490
        pCxt->errCode =
392✔
7491
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "PRE_FILTER specified multiple times");
392✔
7492
        goto _err;
392✔
7493
      }
7494
      pStreamOptions->pPreFilter = pOptionUnit->pNode;
17,559✔
7495
      break;
17,559✔
7496
    case STREAM_TRIGGER_OPTION_EVENT_TYPE:
25,986✔
7497
      if (pStreamOptions->pEventType != EVENT_NONE) {
25,986✔
7498
        pCxt->errCode =
×
7499
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EVENT_TYPE specified multiple times");
×
7500
        goto _err;
×
7501
      }
7502
      pStreamOptions->pEventType = pOptionUnit->flag;
25,986✔
7503
      break;
25,986✔
7504
    case STREAM_TRIGGER_OPTION_IGNORE_NODATA_TRIGGER:
19,580✔
7505
      if (pStreamOptions->ignoreNoDataTrigger) {
19,580✔
7506
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7507
                                                "IGNORE_NODATA_TRIGGER specified multiple times");
7508
        goto _err;
×
7509
      }
7510
      pStreamOptions->ignoreNoDataTrigger = true;
19,580✔
7511
      break;
19,580✔
7512
    case STREAM_TRIGGER_OPTION_IDLE_TIMEOUT:
15,743✔
7513
      if (pStreamOptions->pIdleTimeout != NULL) {
15,743✔
7514
        pCxt->errCode =
×
7515
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "IDLE_TIMEOUT specified multiple times");
×
7516
        goto _err;
×
7517
      }
7518
      pStreamOptions->pIdleTimeout = pOptionUnit->pNode;
15,743✔
7519
      break;
15,743✔
7520
    default:
×
7521
      break;
×
7522
  }
7523
  return pOptions;
217,189✔
7524
_err:
1,676✔
7525
  nodesDestroyNode(pOptionUnit->pNode);
1,676✔
7526
  nodesDestroyNode(pOptions);
1,676✔
7527
  return NULL;
1,676✔
7528
}
7529

7530
static bool validateNotifyUrl(const char* url) {
72,289✔
7531
  const char* prefix[] = {"ws://", "wss://"};
72,289✔
7532
  const char* host = NULL;
72,289✔
7533

7534
  if (!url || *url == '\0') return false;
72,289✔
7535

7536
  for (int32_t i = 0; i < ARRAY_SIZE(prefix); ++i) {
72,453✔
7537
    if (taosStrncasecmp(url, prefix[i], strlen(prefix[i])) == 0) {
72,371✔
7538
      host = url + strlen(prefix[i]);
72,207✔
7539
      break;
72,207✔
7540
    }
7541
  }
7542

7543
  return (host != NULL) && (*host != '\0') && (*host != '/');
72,289✔
7544
}
7545

7546
SNode* createStreamNotifyOptions(SAstCreateContext* pCxt, SNodeList* pAddrUrls, int64_t eventType, SNode* pWhere,
69,153✔
7547
                                 int64_t notifyType) {
7548
  SNode* pNode = NULL;
69,153✔
7549
  CHECK_PARSER_STATUS(pCxt);
69,153✔
7550

7551
  if (LIST_LENGTH(pAddrUrls) == 0) {
69,153✔
7552
    pCxt->errCode =
×
7553
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "notification address cannot be empty");
×
7554
    goto _err;
×
7555
  }
7556

7557
  FOREACH(pNode, pAddrUrls) {
141,360✔
7558
    char* url = ((SValueNode*)pNode)->literal;
72,289✔
7559
    if (strlen(url) >= TSDB_STREAM_NOTIFY_URL_LEN) {
72,289✔
7560
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
7561
                                              "notification address \"%s\" exceed maximum length %d", url,
7562
                                              TSDB_STREAM_NOTIFY_URL_LEN);
7563
      goto _err;
×
7564
    }
7565
    if (!validateNotifyUrl(url)) {
72,289✔
7566
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
82✔
7567
                                              "invalid notification address \"%s\"", url);
7568
      goto _err;
82✔
7569
    }
7570
  }
7571

7572
  SStreamNotifyOptions* pNotifyOptions = NULL;
69,071✔
7573
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_NOTIFY_OPTIONS, (SNode**)&pNotifyOptions);
69,071✔
7574
  CHECK_MAKE_NODE(pNotifyOptions);
69,071✔
7575
  pNotifyOptions->pAddrUrls = pAddrUrls;
69,071✔
7576
  pNotifyOptions->pWhere = pWhere;
69,071✔
7577
  pNotifyOptions->eventType = eventType;
69,071✔
7578
  pNotifyOptions->notifyType = notifyType;
69,071✔
7579
  return (SNode*)pNotifyOptions;
69,071✔
7580
_err:
82✔
7581
  nodesDestroyList(pAddrUrls);
82✔
7582
  return NULL;
82✔
7583
}
7584

7585
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pStream, SNode* pTrigger,
477,139✔
7586
                              SNode* pOutTable, SNode* pQuery) {
7587
  SCreateStreamStmt* pStmt = NULL;
477,139✔
7588
  CHECK_PARSER_STATUS(pCxt);
477,139✔
7589
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
476,715✔
7590
  CHECK_MAKE_NODE(pStmt);
476,715✔
7591

7592
  if (pOutTable && ((SStreamOutTableNode*)pOutTable)->pOutTable) {
476,715✔
7593
    tstrncpy(pStmt->targetDbName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.dbName,
472,598✔
7594
             TSDB_DB_NAME_LEN);
7595
    tstrncpy(pStmt->targetTabName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.tableName,
472,598✔
7596
             TSDB_TABLE_NAME_LEN);
7597
  }
7598

7599
  if (pStream) {
476,715✔
7600
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
476,715✔
7601
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
476,715✔
7602
  } else {
7603
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7604
    goto _err;
×
7605
  }
7606
  nodesDestroyNode(pStream);
476,715✔
7607

7608
  pStmt->ignoreExists = ignoreExists;
476,715✔
7609
  pStmt->pTrigger = pTrigger;
476,715✔
7610
  pStmt->pQuery = pQuery;
476,715✔
7611
  pStmt->pTags = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pTags : NULL;
476,715✔
7612
  pStmt->pSubtable = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pSubtable : NULL;
476,715✔
7613
  pStmt->pCols = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pCols : NULL;
476,715✔
7614
  pStmt->nodelayCreateSubtable = pOutTable ? ((SStreamOutTableNode*)pOutTable)->nodelayCreateSubtable : 0;
476,715✔
7615
  return (SNode*)pStmt;
476,715✔
7616
_err:
424✔
7617
  nodesDestroyNode(pOutTable);
424✔
7618
  nodesDestroyNode(pQuery);
424✔
7619
  nodesDestroyNode(pTrigger);
424✔
7620
  nodesDestroyNode(pQuery);
424✔
7621
  return NULL;
424✔
7622
}
7623

7624
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNodeList* pStreamList) {
20,575✔
7625
  CHECK_PARSER_STATUS(pCxt);
20,575✔
7626
  SDropStreamStmt* pStmt = NULL;
20,575✔
7627
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
20,575✔
7628
  CHECK_MAKE_NODE(pStmt);
20,575✔
7629

7630
  if (pStreamList) {
20,575✔
7631
    pStmt->pStreamList = pStreamList;
20,575✔
7632
  } else {
7633
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7634
    goto _err;
×
7635
  }
7636

7637
  pStmt->ignoreNotExists = ignoreNotExists;
20,575✔
7638
  return (SNode*)pStmt;
20,575✔
7639
_err:
×
7640
  nodesDestroyList(pStreamList);
×
7641
  return NULL;
×
7642
}
7643

7644
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pStream) {
3,045✔
7645
  CHECK_PARSER_STATUS(pCxt);
3,045✔
7646
  SPauseStreamStmt* pStmt = NULL;
3,045✔
7647
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
3,045✔
7648
  CHECK_MAKE_NODE(pStmt);
3,045✔
7649
  if (pStream) {
3,045✔
7650
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
3,045✔
7651
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
3,045✔
7652
  } else {
7653
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7654
    goto _err;
×
7655
  }
7656
  nodesDestroyNode(pStream);
3,045✔
7657
  pStmt->ignoreNotExists = ignoreNotExists;
3,045✔
7658
  return (SNode*)pStmt;
3,045✔
7659
_err:
×
7660
  return NULL;
×
7661
}
7662

7663
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated, SNode* pStream) {
2,675✔
7664
  CHECK_PARSER_STATUS(pCxt);
2,675✔
7665
  SResumeStreamStmt* pStmt = NULL;
2,675✔
7666
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
2,675✔
7667
  CHECK_MAKE_NODE(pStmt);
2,675✔
7668
  if (pStream) {
2,675✔
7669
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
2,675✔
7670
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
2,675✔
7671
  } else {
7672
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7673
    goto _err;
×
7674
  }
7675
  nodesDestroyNode(pStream);
2,675✔
7676
  pStmt->ignoreNotExists = ignoreNotExists;
2,675✔
7677
  pStmt->ignoreUntreated = ignoreUntreated;
2,675✔
7678
  return (SNode*)pStmt;
2,675✔
7679
_err:
×
7680
  return NULL;
×
7681
}
7682

7683
SNode* createRecalcStreamStmt(SAstCreateContext* pCxt, SNode* pStream, SNode* pRange) {
12,249✔
7684
  CHECK_PARSER_STATUS(pCxt);
12,249✔
7685
  SRecalcStreamStmt* pStmt = NULL;
12,249✔
7686
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RECALCULATE_STREAM_STMT, (SNode**)&pStmt);
12,249✔
7687
  CHECK_MAKE_NODE(pStmt);
12,249✔
7688
  if (pStream) {
12,249✔
7689
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
12,249✔
7690
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
12,249✔
7691
  } else {
7692
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7693
    goto _err;
×
7694
  }
7695
  pStmt->pRange = pRange;
12,249✔
7696
  return (SNode*)pStmt;
12,249✔
7697
_err:
×
7698
  return NULL;
×
7699
}
7700

7701
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
2,752✔
7702
  CHECK_PARSER_STATUS(pCxt);
2,752✔
7703
  SKillStmt* pStmt = NULL;
2,752✔
7704
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,752✔
7705
  CHECK_MAKE_NODE(pStmt);
2,752✔
7706
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
2,752✔
7707
  return (SNode*)pStmt;
2,752✔
7708
_err:
×
7709
  return NULL;
×
7710
}
7711

7712
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
187✔
7713
  CHECK_PARSER_STATUS(pCxt);
187✔
7714
  SKillQueryStmt* pStmt = NULL;
187✔
7715
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
187✔
7716
  CHECK_MAKE_NODE(pStmt);
187✔
7717
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
187✔
7718
  return (SNode*)pStmt;
187✔
7719
_err:
×
7720
  return NULL;
×
7721
}
7722

7723
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
12,935✔
7724
  CHECK_PARSER_STATUS(pCxt);
12,935✔
7725
  SBalanceVgroupStmt* pStmt = NULL;
12,935✔
7726
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
12,935✔
7727
  CHECK_MAKE_NODE(pStmt);
12,935✔
7728
  return (SNode*)pStmt;
12,935✔
7729
_err:
×
7730
  return NULL;
×
7731
}
7732

7733
SNode* createAssignLeaderStmt(SAstCreateContext* pCxt) {
18✔
7734
  CHECK_PARSER_STATUS(pCxt);
18✔
7735
  SAssignLeaderStmt* pStmt = NULL;
18✔
7736
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ASSIGN_LEADER_STMT, (SNode**)&pStmt);
18✔
7737
  CHECK_MAKE_NODE(pStmt);
18✔
7738
  return (SNode*)pStmt;
18✔
7739
_err:
×
7740
  return NULL;
×
7741
}
7742

7743
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
3,045✔
7744
  CHECK_PARSER_STATUS(pCxt);
3,045✔
7745
  SBalanceVgroupLeaderStmt* pStmt = NULL;
3,045✔
7746
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
3,045✔
7747
  CHECK_MAKE_NODE(pStmt);
3,045✔
7748
  if (NULL != pVgId && NULL != pVgId->z) {
3,045✔
7749
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
75✔
7750
  }
7751
  return (SNode*)pStmt;
3,045✔
7752
_err:
×
7753
  return NULL;
×
7754
}
7755

7756
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
114✔
7757
  CHECK_PARSER_STATUS(pCxt);
114✔
7758
  SBalanceVgroupLeaderStmt* pStmt = NULL;
114✔
7759
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
114✔
7760
  CHECK_MAKE_NODE(pStmt);
114✔
7761
  if (NULL != pDbName) {
114✔
7762
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
114✔
7763
  }
7764
  return (SNode*)pStmt;
114✔
7765
_err:
×
7766
  return NULL;
×
7767
}
7768

7769
SNode* createSetVgroupKeepVersionStmt(SAstCreateContext* pCxt, const SToken* pVgId, const SToken* pKeepVersion) {
1,715✔
7770
  CHECK_PARSER_STATUS(pCxt);
1,715✔
7771
  SSetVgroupKeepVersionStmt* pStmt = NULL;
1,715✔
7772
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_VGROUP_KEEP_VERSION_STMT, (SNode**)&pStmt);
1,715✔
7773
  CHECK_MAKE_NODE(pStmt);
1,715✔
7774
  if (NULL != pVgId && NULL != pVgId->z) {
1,715✔
7775
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
1,715✔
7776
  }
7777
  if (NULL != pKeepVersion && NULL != pKeepVersion->z) {
1,715✔
7778
    pStmt->keepVersion = taosStr2Int64(pKeepVersion->z, NULL, 10);
1,715✔
7779
  }
7780
  return (SNode*)pStmt;
1,715✔
7781
_err:
×
7782
  return NULL;
×
7783
}
7784

7785
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
×
7786
  CHECK_PARSER_STATUS(pCxt);
×
7787
  SMergeVgroupStmt* pStmt = NULL;
×
7788
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
×
7789
  CHECK_MAKE_NODE(pStmt);
×
7790
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
×
7791
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
×
7792
  return (SNode*)pStmt;
×
7793
_err:
×
7794
  return NULL;
×
7795
}
7796

7797
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
47,899✔
7798
  CHECK_PARSER_STATUS(pCxt);
47,899✔
7799
  SRedistributeVgroupStmt* pStmt = NULL;
47,899✔
7800
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
47,899✔
7801
  CHECK_MAKE_NODE(pStmt);
47,899✔
7802
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
47,899✔
7803
  pStmt->pDnodes = pDnodes;
47,899✔
7804
  return (SNode*)pStmt;
47,899✔
7805
_err:
×
7806
  nodesDestroyList(pDnodes);
×
7807
  return NULL;
×
7808
}
7809

7810
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, bool force) {
22,451✔
7811
  CHECK_PARSER_STATUS(pCxt);
22,451✔
7812
  SSplitVgroupStmt* pStmt = NULL;
22,451✔
7813
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
22,451✔
7814
  CHECK_MAKE_NODE(pStmt);
22,451✔
7815
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
22,451✔
7816
  pStmt->force = force;
22,451✔
7817
  return (SNode*)pStmt;
22,451✔
7818
_err:
×
7819
  return NULL;
×
7820
}
7821

7822
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
7823
  CHECK_PARSER_STATUS(pCxt);
×
7824
  SNode* pStmt = NULL;
×
7825
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
7826
  CHECK_MAKE_NODE(pStmt);
×
7827
  return pStmt;
×
7828
_err:
×
7829
  return NULL;
×
7830
}
7831

7832
SNode* createGrantStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
921,939✔
7833
                       SNode* pCond, int8_t optrType) {
7834
  CHECK_PARSER_STATUS(pCxt);
921,939✔
7835
  CHECK_NAME(checkRoleName(pCxt, pPrincipal, false));
921,939✔
7836
  SGrantStmt* pStmt = NULL;
921,939✔
7837
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
921,939✔
7838
  CHECK_MAKE_NODE(pStmt);
921,939✔
7839
  pStmt->optrType = optrType;
921,939✔
7840
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
921,939✔
7841
  switch (optrType) {
921,939✔
7842
    case TSDB_ALTER_ROLE_LOCK:
×
7843
      break;
×
7844
    case TSDB_ALTER_ROLE_PRIVILEGES: {
915,331✔
7845
      CHECK_NAME(checkObjName(pCxt, &pPrivLevel->first, false));
915,331✔
7846
      CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
915,331✔
7847
      pStmt->privileges = *(SPrivSetArgs*)resouces;
915,331✔
7848
      if(pStmt->privileges.nPrivArgs <= 0) {
915,331✔
7849
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Unknown privilege type");
×
7850
        goto _err;
×
7851
      }
7852
      if (TK_NK_NIL != pPrivLevel->first.type) {
915,331✔
7853
        COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
904,632✔
7854
      }
7855
      if (TK_NK_NIL != pPrivLevel->second.type) {
915,331✔
7856
        COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
607,621✔
7857
      }
7858
      pStmt->privileges.objType = pPrivLevel->objType;
915,331✔
7859
      pStmt->pCond = pCond;
915,331✔
7860
      break;
915,331✔
7861
    }
7862
    case TSDB_ALTER_ROLE_ROLE: {
6,608✔
7863
      SToken* pRole = (SToken*)resouces;
6,608✔
7864
      CHECK_NAME(checkRoleName(pCxt, pRole, false));
6,608✔
7865
      COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
6,608✔
7866
      break;
6,608✔
7867
    }
7868
    default:
×
7869
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported grant type");
×
7870
      goto _err;
×
7871
  }
7872
  return (SNode*)pStmt;
921,939✔
7873
_err:
×
7874
  nodesDestroyNode(pCond);
×
7875
  return NULL;
×
7876
}
7877

7878
SNode* createRevokeStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
495,113✔
7879
                        SNode* pCond, int8_t optrType) {
7880
  CHECK_PARSER_STATUS(pCxt);
495,113✔
7881
  CHECK_NAME(checkUserName(pCxt, pPrincipal));
495,113✔
7882
  SRevokeStmt* pStmt = NULL;
495,113✔
7883
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
495,113✔
7884
  CHECK_MAKE_NODE(pStmt);
495,113✔
7885
  pStmt->optrType = optrType;
495,113✔
7886
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
495,113✔
7887
  if (optrType == TSDB_ALTER_ROLE_PRIVILEGES) {
495,113✔
7888
    CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
489,511✔
7889
    CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
489,511✔
7890
    pStmt->privileges = *(SPrivSetArgs*)resouces;
489,511✔
7891
    if (TK_NK_NIL != pPrivLevel->first.type) {
489,511✔
7892
      COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
483,925✔
7893
    }
7894
    if (TK_NK_NIL != pPrivLevel->second.type) {
489,511✔
7895
      COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
434,475✔
7896
    }
7897
    pStmt->privileges.objType = pPrivLevel->objType;
489,511✔
7898
    pStmt->pCond = pCond;
489,511✔
7899
  } else if (optrType == TSDB_ALTER_ROLE_ROLE) {
5,602✔
7900
    SToken* pRole = (SToken*)resouces;
5,602✔
7901
    CHECK_NAME(checkRoleName(pCxt, pRole, false));
5,602✔
7902
    COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
5,602✔
7903
  } else {
7904
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported revoke type");
×
7905
    goto _err;
×
7906
  }
7907

7908
  return (SNode*)pStmt;
495,113✔
7909
_err:
×
7910
  nodesDestroyNode(pCond);
×
7911
  return NULL;
×
7912
}
7913

7914
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
5,617,752✔
7915
  SFunctionNode* pFunc = NULL;
5,617,752✔
7916
  CHECK_PARSER_STATUS(pCxt);
5,617,752✔
7917
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
5,617,752✔
7918
  CHECK_MAKE_NODE(pFunc);
5,617,752✔
7919
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
5,617,752✔
7920
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
5,617,752✔
7921
  CHECK_MAKE_NODE(pCol);
5,617,752✔
7922
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
5,617,752✔
7923
  CHECK_PARSER_STATUS(pCxt);
5,617,752✔
7924
  return (SNode*)pFunc;
5,617,752✔
7925
_err:
×
7926
  nodesDestroyNode((SNode*)pFunc);
×
7927
  return NULL;
×
7928
}
7929

7930
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
1,872,584✔
7931
  SDeleteStmt* pStmt = NULL;
1,872,584✔
7932
  CHECK_PARSER_STATUS(pCxt);
1,872,584✔
7933
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
1,872,584✔
7934
  CHECK_MAKE_NODE(pStmt);
1,872,584✔
7935
  pStmt->pFromTable = pTable;
1,872,584✔
7936
  pStmt->pWhere = pWhere;
1,872,584✔
7937
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
1,872,584✔
7938
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
1,872,584✔
7939
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
1,872,584✔
7940
  CHECK_MAKE_NODE(pStmt->pCountFunc);
1,872,584✔
7941
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
1,872,584✔
7942
  CHECK_MAKE_NODE(pStmt->pLastFunc);
1,872,584✔
7943
  pStmt->secureDelete = 0;
1,872,584✔
7944
  return (SNode*)pStmt;
1,872,584✔
7945
_err:
×
7946
  nodesDestroyNode((SNode*)pStmt);
×
7947
  nodesDestroyNode(pTable);
×
7948
  nodesDestroyNode(pWhere);
×
7949
  return NULL;
×
7950
}
7951

7952
SNode* createSecureDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
2,168✔
7953
  SNode* pNode = createDeleteStmt(pCxt, pTable, pWhere);
2,168✔
7954
  if (pNode) {
2,168✔
7955
    ((SDeleteStmt*)pNode)->secureDelete = 1;
2,168✔
7956
  }
7957
  return pNode;
2,168✔
7958
}
7959

7960
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
554,016✔
7961
  CHECK_PARSER_STATUS(pCxt);
554,016✔
7962
  SInsertStmt* pStmt = NULL;
554,016✔
7963
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
554,016✔
7964
  CHECK_MAKE_NODE(pStmt);
554,016✔
7965
  pStmt->pTable = pTable;
554,016✔
7966
  pStmt->pCols = pCols;
554,016✔
7967
  pStmt->pQuery = pQuery;
554,016✔
7968
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
554,016✔
7969
    tstrncpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
553,820✔
7970
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
196✔
7971
    tstrncpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
196✔
7972
  }
7973
  return (SNode*)pStmt;
554,016✔
7974
_err:
×
7975
  nodesDestroyNode(pTable);
×
7976
  nodesDestroyNode(pQuery);
×
7977
  nodesDestroyList(pCols);
×
7978
  return NULL;
×
7979
}
7980

7981
SNode* createCreateRsmaStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* rsmaName, SNode* pRealTable,
116,344✔
7982
                            SNodeList* pFuncs, SNodeList* pIntervals) {
7983
  SCreateRsmaStmt* pStmt = NULL;
116,344✔
7984

7985
  CHECK_PARSER_STATUS(pCxt);
116,344✔
7986
  CHECK_NAME(checkRsmaName(pCxt, rsmaName));
116,344✔
7987
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_RSMA_STMT, (SNode**)&pStmt);
116,344✔
7988
  CHECK_MAKE_NODE(pStmt);
116,344✔
7989

7990
  pStmt->ignoreExists = ignoreExists;
116,344✔
7991
  pStmt->pFuncs = pFuncs;
116,344✔
7992
  pStmt->pIntervals = pIntervals;
116,344✔
7993
  COPY_STRING_FORM_ID_TOKEN(pStmt->rsmaName, rsmaName);
116,344✔
7994

7995
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
116,344✔
7996
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
116,344✔
7997
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
116,344✔
7998
  nodesDestroyNode(pRealTable);
116,344✔
7999

8000
  return (SNode*)pStmt;
116,344✔
8001
_err:
×
8002
  nodesDestroyNode((SNode*)pStmt);
×
8003
  nodesDestroyList(pFuncs);
×
8004
  nodesDestroyNode(pRealTable);
×
8005
  nodesDestroyList(pIntervals);
×
8006
  return NULL;
×
8007
}
8008

8009
SNode* createDropRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
10,707✔
8010
  CHECK_PARSER_STATUS(pCxt);
10,707✔
8011
  SDropRsmaStmt* pStmt = NULL;
10,707✔
8012
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_RSMA_STMT, (SNode**)&pStmt);
10,707✔
8013
  CHECK_MAKE_NODE(pStmt);
10,707✔
8014

8015
  pStmt->ignoreNotExists = ignoreNotExists;
10,707✔
8016
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
10,707✔
8017

8018
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
10,707✔
8019
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
10,707✔
8020

8021
  nodesDestroyNode(pRealTable);
10,707✔
8022
  return (SNode*)pStmt;
10,707✔
8023
_err:
×
8024
  nodesDestroyNode(pRealTable);
×
8025
  return NULL;
×
8026
}
8027

8028
SNode* createAlterRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRsma, int8_t alterType,
21,615✔
8029
                           void* alterInfo) {
8030
  CHECK_PARSER_STATUS(pCxt);
21,615✔
8031
  SAlterRsmaStmt* pStmt = NULL;
21,615✔
8032
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_RSMA_STMT, (SNode**)&pStmt);
21,615✔
8033
  CHECK_MAKE_NODE(pStmt);
21,615✔
8034
  pStmt->ignoreNotExists = ignoreNotExists;
21,615✔
8035
  SRealTableNode* pTableNode = (SRealTableNode*)pRsma;
21,615✔
8036

8037
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
21,615✔
8038
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
21,615✔
8039
  nodesDestroyNode(pRsma);
21,615✔
8040

8041
  pStmt->alterType = alterType;
21,615✔
8042
  switch (alterType) {
21,615✔
8043
    case TSDB_ALTER_RSMA_FUNCTION: {
21,615✔
8044
      pStmt->pFuncs = (SNodeList*)alterInfo;
21,615✔
8045
      break;
21,615✔
8046
    }
8047
    default:
×
8048
      break;
×
8049
  }
8050
  return (SNode*)pStmt;
21,615✔
8051
_err:
×
8052
  return NULL;
×
8053
}
8054

8055
SNode* createShowCreateRsmaStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
3,301✔
8056
  CHECK_PARSER_STATUS(pCxt);
3,301✔
8057
  SShowCreateRsmaStmt* pStmt = NULL;
3,301✔
8058
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,301✔
8059
  CHECK_MAKE_NODE(pStmt);
3,301✔
8060
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
3,301✔
8061
  tstrncpy(pStmt->rsmaName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->rsmaName));
3,301✔
8062
  nodesDestroyNode(pRealTable);
3,301✔
8063
  return (SNode*)pStmt;
3,301✔
8064
_err:
×
8065
  nodesDestroyNode(pRealTable);
×
8066
  return NULL;
×
8067
}
8068

8069
SNode* createRollupStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
10,281✔
8070
  CHECK_PARSER_STATUS(pCxt);
10,281✔
8071
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
10,281✔
8072
  SRollupDatabaseStmt* pStmt = NULL;
10,281✔
8073
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_DATABASE_STMT, (SNode**)&pStmt);
10,281✔
8074
  CHECK_MAKE_NODE(pStmt);
10,281✔
8075
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
10,281✔
8076
  pStmt->pStart = pStart;
10,281✔
8077
  pStmt->pEnd = pEnd;
10,281✔
8078
  return (SNode*)pStmt;
10,281✔
8079
_err:
×
8080
  nodesDestroyNode(pStart);
×
8081
  nodesDestroyNode(pEnd);
×
8082
  return NULL;
×
8083
}
8084

8085
SNode* createRollupVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
4,614✔
8086
                                SNode* pEnd) {
8087
  CHECK_PARSER_STATUS(pCxt);
4,614✔
8088
  if (NULL == pDbName) {
4,614✔
8089
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
8090
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
8091
    CHECK_PARSER_STATUS(pCxt);
×
8092
  }
8093
  SRollupVgroupsStmt* pStmt = NULL;
4,614✔
8094
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_VGROUPS_STMT, (SNode**)&pStmt);
4,614✔
8095
  CHECK_MAKE_NODE(pStmt);
4,614✔
8096
  pStmt->pDbName = pDbName;
4,614✔
8097
  pStmt->vgidList = vgidList;
4,614✔
8098
  pStmt->pStart = pStart;
4,614✔
8099
  pStmt->pEnd = pEnd;
4,614✔
8100
  return (SNode*)pStmt;
4,614✔
8101
_err:
×
8102
  nodesDestroyNode(pDbName);
×
8103
  nodesDestroyList(vgidList);
×
8104
  nodesDestroyNode(pStart);
×
8105
  nodesDestroyNode(pEnd);
×
8106
  return NULL;
×
8107
}
8108

8109
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
7,930✔
8110
                            SNode* pRealTable, SNode* pInterval) {
8111
  SCreateTSMAStmt* pStmt = NULL;
7,930✔
8112

8113
  CHECK_PARSER_STATUS(pCxt);
7,930✔
8114
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
7,930✔
8115
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
7,670✔
8116
  CHECK_MAKE_NODE(pStmt);
7,670✔
8117

8118
  pStmt->ignoreExists = ignoreExists;
7,670✔
8119
  if (!pOptions) {
7,670✔
8120
    // recursive tsma
8121
    pStmt->pOptions = NULL;
1,170✔
8122
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
1,170✔
8123
    CHECK_MAKE_NODE(pStmt->pOptions);
1,170✔
8124
    pStmt->pOptions->recursiveTsma = true;
1,170✔
8125
  } else {
8126
    pStmt->pOptions = (STSMAOptions*)pOptions;
6,500✔
8127
  }
8128
  pStmt->pOptions->pInterval = pInterval;
7,670✔
8129
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
7,670✔
8130

8131
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
7,670✔
8132
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
7,670✔
8133
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
7,670✔
8134
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
7,670✔
8135
  nodesDestroyNode(pRealTable);
7,670✔
8136

8137
  return (SNode*)pStmt;
7,670✔
8138
_err:
260✔
8139
  nodesDestroyNode((SNode*)pStmt);
260✔
8140
  nodesDestroyNode(pOptions);
260✔
8141
  nodesDestroyNode(pRealTable);
260✔
8142
  nodesDestroyNode(pInterval);
260✔
8143
  return NULL;
260✔
8144
}
8145

8146
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
7,410✔
8147
  CHECK_PARSER_STATUS(pCxt);
7,410✔
8148
  STSMAOptions* pOptions = NULL;
7,410✔
8149
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
7,410✔
8150
  CHECK_MAKE_NODE(pOptions);
7,410✔
8151
  pOptions->pFuncs = pFuncs;
7,410✔
8152
  return (SNode*)pOptions;
7,410✔
8153
_err:
×
8154
  nodesDestroyList(pFuncs);
×
8155
  return NULL;
×
8156
}
8157

8158
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
8159
  CHECK_PARSER_STATUS(pCxt);
×
8160
  STSMAOptions* pOptions = NULL;
×
8161
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
8162
  CHECK_MAKE_NODE(pOptions);
×
8163
  return (SNode*)pOptions;
×
8164
_err:
×
8165
  return NULL;
×
8166
}
8167

8168
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
3,676✔
8169
  CHECK_PARSER_STATUS(pCxt);
3,676✔
8170
  SDropTSMAStmt* pStmt = NULL;
3,676✔
8171
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
3,676✔
8172
  CHECK_MAKE_NODE(pStmt);
3,676✔
8173

8174
  pStmt->ignoreNotExists = ignoreNotExists;
3,676✔
8175
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
3,676✔
8176

8177
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
3,676✔
8178
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
3,676✔
8179

8180
  nodesDestroyNode(pRealTable);
3,676✔
8181
  return (SNode*)pStmt;
3,676✔
8182
_err:
×
8183
  nodesDestroyNode(pRealTable);
×
8184
  return NULL;
×
8185
}
8186

8187
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
142✔
8188
  CHECK_PARSER_STATUS(pCxt);
142✔
8189

8190
  SShowStmt* pStmt = NULL;
142✔
8191
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
142✔
8192
  CHECK_MAKE_NODE(pStmt);
142✔
8193

8194
  pStmt->pDbName = dbName;
142✔
8195
  return (SNode*)pStmt;
142✔
8196
_err:
×
8197
  nodesDestroyNode(dbName);
×
8198
  return NULL;
×
8199
}
8200
SNode* createShowDiskUsageStmt(SAstCreateContext* pCxt, SNode* dbName, ENodeType type) {
351✔
8201
  CHECK_PARSER_STATUS(pCxt);
351✔
8202
  if (NULL == dbName) {
351✔
8203
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
8204
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
8205
    CHECK_PARSER_STATUS(pCxt);
×
8206
  }
8207

8208
  SShowStmt* pStmt = NULL;
351✔
8209
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
351✔
8210
  CHECK_MAKE_NODE(pStmt);
351✔
8211

8212
  pStmt->pDbName = dbName;
351✔
8213
  return (SNode*)pStmt;
351✔
8214
_err:
×
8215
  nodesDestroyNode(dbName);
×
8216
  return NULL;
×
8217
}
8218

8219
SNode* createShowStreamsStmt(SAstCreateContext* pCxt, SNode* pDbName, ENodeType type) {
117,959✔
8220
  CHECK_PARSER_STATUS(pCxt);
117,959✔
8221

8222
  if (needDbShowStmt(type) && NULL == pDbName) {
117,959✔
8223
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
245✔
8224
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
245✔
8225
    CHECK_PARSER_STATUS(pCxt);
245✔
8226
  }
8227

8228
  SShowStmt* pStmt = NULL;
117,714✔
8229
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
117,714✔
8230
  CHECK_MAKE_NODE(pStmt);
117,714✔
8231
  pStmt->withFull = false;
117,714✔
8232
  pStmt->pDbName = pDbName;
117,714✔
8233

8234
  return (SNode*)pStmt;
117,714✔
8235

8236
_err:
245✔
8237
  nodesDestroyNode(pDbName);
245✔
8238
  return NULL;
245✔
8239
}
8240

8241
SNode* createScanStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
822✔
8242
  CHECK_PARSER_STATUS(pCxt);
822✔
8243
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
822✔
8244
  SScanDatabaseStmt* pStmt = NULL;
822✔
8245
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_DATABASE_STMT, (SNode**)&pStmt);
822✔
8246
  CHECK_MAKE_NODE(pStmt);
822✔
8247
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
822✔
8248
  pStmt->pStart = pStart;
822✔
8249
  pStmt->pEnd = pEnd;
822✔
8250
  return (SNode*)pStmt;
822✔
8251
_err:
×
8252
  nodesDestroyNode(pStart);
×
8253
  nodesDestroyNode(pEnd);
×
8254
  return NULL;
×
8255
}
8256

8257
SNode* createScanVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart, SNode* pEnd) {
336✔
8258
  CHECK_PARSER_STATUS(pCxt);
336✔
8259
  if (NULL == pDbName) {
336✔
8260
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
56✔
8261
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
56✔
8262
    CHECK_PARSER_STATUS(pCxt);
56✔
8263
  }
8264
  SScanVgroupsStmt* pStmt = NULL;
280✔
8265
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_VGROUPS_STMT, (SNode**)&pStmt);
280✔
8266
  CHECK_MAKE_NODE(pStmt);
280✔
8267
  pStmt->pDbName = pDbName;
280✔
8268
  pStmt->vgidList = vgidList;
280✔
8269
  pStmt->pStart = pStart;
280✔
8270
  pStmt->pEnd = pEnd;
280✔
8271
  return (SNode*)pStmt;
280✔
8272
_err:
56✔
8273
  nodesDestroyNode(pDbName);
56✔
8274
  nodesDestroyList(vgidList);
56✔
8275
  nodesDestroyNode(pStart);
56✔
8276
  nodesDestroyNode(pEnd);
56✔
8277
  return NULL;
56✔
8278
}
8279

8280
SNode* createShowScansStmt(SAstCreateContext* pCxt, ENodeType type) {
2,464✔
8281
  CHECK_PARSER_STATUS(pCxt);
2,464✔
8282
  SShowScansStmt* pStmt = NULL;
2,464✔
8283
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,464✔
8284
  CHECK_MAKE_NODE(pStmt);
2,464✔
8285
  return (SNode*)pStmt;
2,464✔
8286
_err:
×
8287
  return NULL;
×
8288
}
8289

8290
SNode* createShowScanDetailsStmt(SAstCreateContext* pCxt, SNode* pScanIdNode) {
2,352✔
8291
  CHECK_PARSER_STATUS(pCxt);
2,352✔
8292
  SShowScanDetailsStmt* pStmt = NULL;
2,352✔
8293
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_SCAN_DETAILS_STMT, (SNode**)&pStmt);
2,352✔
8294
  CHECK_MAKE_NODE(pStmt);
2,352✔
8295
  pStmt->pScanId = pScanIdNode;
2,352✔
8296
  return (SNode*)pStmt;
2,352✔
8297
_err:
×
8298
  nodesDestroyNode(pScanIdNode);
×
8299
  return NULL;
×
8300
}
8301

8302
SNode* createAlterAllDnodeTLSStmt(SAstCreateContext* pCxt, SToken* alterName) {
×
8303
  CHECK_PARSER_STATUS(pCxt);
×
8304
  SAlterDnodeStmt* pStmt = NULL;
×
8305
  static char*     tls = "TLS";
8306
  if (NULL == alterName || alterName->n <= 0) {
×
8307
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter name is empty");
×
8308
    goto _err;
×
8309
  }
8310

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

8314
    memcpy(pStmt->config, "reload", strlen("reload"));
×
8315
    memcpy(pStmt->value, "tls", strlen("tls"));
×
8316
    pStmt->dnodeId = -1;
×
8317
  } else {
8318
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter is not supported");
×
8319
    goto _err;
×
8320
  }
8321

8322
  CHECK_MAKE_NODE(pStmt);
×
8323

8324
  return (SNode*)pStmt;
×
8325
_err:
×
8326
  return NULL;
×
8327
}
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