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

taosdata / TDengine / #4917

07 Jan 2026 03:52PM UTC coverage: 65.42% (+0.02%) from 65.402%
#4917

push

travis-ci

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

31 of 34 new or added lines in 2 files covered. (91.18%)

819 existing lines in 129 files now uncovered.

202679 of 309814 relevant lines covered (65.42%)

116724351.99 hits per line

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

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

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

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

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

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

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

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

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

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

98
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
267,933,221✔
99
  memset(pCxt, 0, sizeof(SAstCreateContext));
267,933,221✔
100
  pCxt->pQueryCxt = pParseCxt;
267,933,221✔
101
  pCxt->msgBuf.buf = pParseCxt->pMsg;
267,922,438✔
102
  pCxt->msgBuf.len = pParseCxt->msgLen;
267,929,082✔
103
  pCxt->notSupport = false;
267,935,049✔
104
  pCxt->pRootNode = NULL;
267,926,066✔
105
  pCxt->placeholderNo = 0;
267,927,232✔
106
  pCxt->pPlaceholderValues = NULL;
267,931,533✔
107
  pCxt->errCode = TSDB_CODE_SUCCESS;
267,935,191✔
108
}
267,932,381✔
109

110
static void trimEscape(SAstCreateContext* pCxt, SToken* pName, bool trimStar) {
2,147,483,647✔
111
  // todo need to deal with `ioo``ii` -> ioo`ii: done
112
  if (NULL != pName && pName->n > 1 && TS_ESCAPE_CHAR == pName->z[0]) {
2,147,483,647✔
113
    if (!pCxt->pQueryCxt->hasDupQuoteChar) {
11,163,182✔
114
      pName->z += 1;
11,063,153✔
115
      pName->n -= 2;
11,063,278✔
116
      // * is forbidden as an identifier name
117
      if (pName->z[0] == '*' && trimStar && pName->n == 1) {
11,063,063✔
118
        pName->z[0] = '\0';
×
119
        pName->n = 0;
×
120
      }
121
    } else {
122
      int32_t i = 1, j = 0;
100,098✔
123
      for (; i < pName->n - 1; ++i) {
612,540✔
124
        if ((pName->z[i] == TS_ESCAPE_CHAR) && (pName->z[i + 1] == TS_ESCAPE_CHAR)) {
512,442✔
125
          pName->z[j++] = TS_ESCAPE_CHAR;
241,530✔
126
          ++i;
241,530✔
127
        } else {
128
          pName->z[j++] = pName->z[i];
270,912✔
129
        }
130
      }
131
      pName->n = j;
100,098✔
132
    }
133
  }
134
}
2,147,483,647✔
135

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

150

151

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

161

162

163
static bool isValidStrongPassword(const char* password) {
72,226✔
164
  if (strcmp(password, "taosdata") == 0) {
72,226✔
165
    return true;
10,217✔
166
  }
167
  return taosIsComplexString(password);
62,009✔
168
}
169

170

171

172
static bool isValidPassword(SAstCreateContext* pCxt, const char* password, bool imported) {
72,706✔
173
  if (imported) {
72,706✔
174
    return strlen(password) == TSDB_PASSWORD_LEN;
×
175
  }
176

177
  if (tsEnableStrongPassword) {
72,706✔
178
    return isValidStrongPassword(password);
72,226✔
179
  }
180

181
  return isValidSimplePassword(password);
480✔
182
}
183

184

185

186
static int32_t parsePort(SAstCreateContext* pCxt, const char* p, int32_t* pPort) {
148,457✔
187
  *pPort = taosStr2Int32(p, NULL, 10);
148,457✔
188
  if (*pPort >= UINT16_MAX || *pPort <= 0) {
148,457✔
189
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
×
190
  }
191
  return TSDB_CODE_SUCCESS;
148,457✔
192
}
193

194
static int32_t parseEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
148,457✔
195
  if (pEp->n >= (NULL == pPort ? (TSDB_FQDN_LEN + 1 + 5) : TSDB_FQDN_LEN)) {  // format 'fqdn:port' or 'fqdn'
148,457✔
196
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
197
  }
198

199
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
148,457✔
200
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
148,457✔
201
  (void)strdequote(ep);
148,457✔
202
  (void)strtrim(ep);
148,457✔
203
  if (NULL == pPort) {
148,457✔
204
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
147,633✔
205
    return TSDB_CODE_SUCCESS;
147,633✔
206
  }
207
  char* pColon = strrchr(ep, ':');
824✔
208
  if (NULL == pColon) {
824✔
209
    *pPort = tsServerPort;
×
210
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
×
211
    return TSDB_CODE_SUCCESS;
×
212
  }
213
  strncpy(pFqdn, ep, pColon - ep);
824✔
214
  return parsePort(pCxt, pColon + 1, pPort);
824✔
215
}
216

217
static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, const SToken* pPortToken, char* pFqdn,
148,457✔
218
                                  int32_t* pPort) {
219
  if (NULL == pEp) {
148,457✔
220
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
221
    return false;
×
222
  }
223

224
  if (NULL != pPortToken) {
148,457✔
225
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
147,633✔
226
  }
227

228
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
148,457✔
229
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
148,457✔
230
  }
231

232
  return TSDB_CODE_SUCCESS == pCxt->errCode;
148,457✔
233
}
234

235
static bool checkObjName(SAstCreateContext* pCxt, SToken* pObjName, bool need) {
558,065✔
236
  if (NULL == pObjName || TK_NK_NIL == pObjName->type) {
558,065✔
237
    if (need) {
963✔
238
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME);
×
239
    }
240
  } else {
241
    trimEscape(pCxt, pObjName, true);
557,102✔
242
    if (pObjName->n >= TSDB_OBJ_NAME_LEN || pObjName->n == 0) {
557,102✔
243
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pObjName->z);
×
244
    }
245
  }
246
  return TSDB_CODE_SUCCESS == pCxt->errCode;
558,065✔
247
}
248

249
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
568,374,791✔
250
  if (NULL == pDbName) {
568,374,791✔
251
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
405,744,928✔
252
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
2,023✔
253
    }
254
  } else {
255
    trimEscape(pCxt, pDbName, true);
162,629,863✔
256
    if (pDbName->n >= TSDB_DB_NAME_LEN || (demandDb && (pDbName->n == 0))) {
162,632,565✔
257
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
1,176✔
258
    }
259
  }
260
  return TSDB_CODE_SUCCESS == pCxt->errCode;
568,372,598✔
261
}
262

263
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
2,147,483,647✔
264
  trimEscape(pCxt, pTableName, true);
2,147,483,647✔
265
  if (NULL != pTableName && pTableName->type != TK_NK_NIL &&
2,147,483,647✔
266
      (pTableName->n >= TSDB_TABLE_NAME_LEN || pTableName->n == 0)) {
928,599,339✔
267
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
26,486✔
268
    return false;
3,766✔
269
  }
270
  return true;
2,147,483,647✔
271
}
272

273
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
1,475,052,522✔
274
  trimEscape(pCxt, pColumnName, true);
1,475,052,522✔
275
  if (NULL != pColumnName && pColumnName->type != TK_NK_NIL &&
1,475,050,943✔
276
      (pColumnName->n >= TSDB_COL_NAME_LEN || pColumnName->n == 0)) {
1,475,051,407✔
277
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
4,877✔
278
    return false;
3,865✔
279
  }
280
  return true;
1,475,049,602✔
281
}
282

283
static bool checkIndexName(SAstCreateContext* pCxt, SToken* pIndexName) {
3,228✔
284
  trimEscape(pCxt, pIndexName, true);
3,228✔
285
  if (NULL != pIndexName && (pIndexName->n >= TSDB_INDEX_NAME_LEN || pIndexName->n == 0)) {
3,228✔
286
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pIndexName->z);
×
287
    return false;
×
288
  }
289
  return true;
3,228✔
290
}
291

292
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
270,844✔
293
  trimEscape(pCxt, pTopicName, true);
270,844✔
294
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN || pTopicName->n == 0) {
270,844✔
295
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
689✔
296
    return false;
689✔
297
  }
298
  return true;
270,155✔
299
}
300

301
static bool checkCGroupName(SAstCreateContext* pCxt, SToken* pCGroup) {
810✔
302
  trimEscape(pCxt, pCGroup, true);
810✔
303
  if (pCGroup->n >= TSDB_CGROUP_LEN || pCGroup->n == 0) {
810✔
304
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pCGroup->z);
×
305
    return false;
×
306
  }
307
  return true;
810✔
308
}
309

310
static bool checkViewName(SAstCreateContext* pCxt, SToken* pViewName) {
18,001✔
311
  trimEscape(pCxt, pViewName, true);
18,001✔
312
  if (pViewName->n >= TSDB_VIEW_NAME_LEN || pViewName->n == 0) {
18,001✔
313
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pViewName->z);
352✔
314
    return false;
352✔
315
  }
316
  return true;
17,649✔
317
}
318

319
static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
287,017✔
320
  trimEscape(pCxt, pStreamName, true);
287,017✔
321
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN || pStreamName->n == 0) {
287,017✔
322
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
176✔
323
    return false;
176✔
324
  }
325
  return true;
286,841✔
326
}
327

328
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
48,389✔
329
  if (NULL == pCommentToken) {
48,389✔
330
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
×
331
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
48,389✔
332
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
8,190✔
333
  }
334
  return TSDB_CODE_SUCCESS == pCxt->errCode;
48,389✔
335
}
336

337
static bool checkRsmaName(SAstCreateContext* pCxt, SToken* pRsmaToken) {
108,919✔
338
  trimEscape(pCxt, pRsmaToken, true);
108,919✔
339
  if (NULL == pRsmaToken) {
108,919✔
340
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
341
  } else if (pRsmaToken->n >= TSDB_TABLE_NAME_LEN) {
108,919✔
342
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
×
343
  } else if (pRsmaToken->n == 0) {
108,919✔
344
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pRsmaToken->z);
×
345
  }
346
  return pCxt->errCode == TSDB_CODE_SUCCESS;
108,919✔
347
}
348

349
static bool checkTsmaName(SAstCreateContext* pCxt, SToken* pTsmaToken) {
800✔
350
  trimEscape(pCxt, pTsmaToken, true);
800✔
351
  if (NULL == pTsmaToken) {
800✔
352
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
353
  } else if (pTsmaToken->n >= TSDB_TABLE_NAME_LEN - strlen(TSMA_RES_STB_POSTFIX)) {
800✔
354
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
×
355
  } else if (pTsmaToken->n == 0) {
800✔
356
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTsmaToken->z);
×
357
  }
358
  return pCxt->errCode == TSDB_CODE_SUCCESS;
800✔
359
}
360

361
static bool checkMountPath(SAstCreateContext* pCxt, SToken* pMountPath) {
1,364✔
362
  trimEscape(pCxt, pMountPath, true);
1,364✔
363
  if (pMountPath->n >= TSDB_MOUNT_PATH_LEN || pMountPath->n == 0) {
1,364✔
364
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pMountPath->z);
×
365
    return false;
×
366
  }
367
  return true;
1,364✔
368
}
369

370
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
1,192,023,228✔
371
  CHECK_PARSER_STATUS(pCxt);
1,192,023,228✔
372
  SRawExprNode* target = NULL;
1,192,021,611✔
373
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
1,192,022,862✔
374
  CHECK_MAKE_NODE(target);
1,192,027,637✔
375
  target->p = pToken->z;
1,192,027,637✔
376
  target->n = pToken->n;
1,192,025,888✔
377
  target->pNode = pNode;
1,192,024,528✔
378
  return (SNode*)target;
1,192,026,968✔
379
_err:
4,917✔
380
  nodesDestroyNode(pNode);
4,917✔
381
  return NULL;
4,917✔
382
}
383

384
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
1,345,982,639✔
385
  CHECK_PARSER_STATUS(pCxt);
1,345,982,639✔
386
  SRawExprNode* target = NULL;
1,345,982,942✔
387
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
1,345,983,397✔
388
  CHECK_MAKE_NODE(target);
1,345,984,316✔
389
  target->p = pStart->z;
1,345,984,316✔
390
  target->n = (pEnd->z + pEnd->n) - pStart->z;
1,345,983,873✔
391
  target->pNode = pNode;
1,345,983,019✔
392
  return (SNode*)target;
1,345,980,659✔
393
_err:
×
394
  nodesDestroyNode(pNode);
×
395
  return NULL;
×
396
}
397

398
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
75,963,681✔
399
  CHECK_PARSER_STATUS(pCxt);
75,963,681✔
400
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
75,963,961✔
401
    return pNode;
×
402
  }
403
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
75,963,083✔
404
  return pNode;
75,964,173✔
405
_err:
×
406
  nodesDestroyNode(pNode);
×
407
  return NULL;
×
408
}
409

410
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
2,147,483,647✔
411
  CHECK_PARSER_STATUS(pCxt);
2,147,483,647✔
412
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,147,483,647✔
413
  SNode*        pRealizedExpr = pRawExpr->pNode;
2,147,483,647✔
414
  if (nodesIsExprNode(pRealizedExpr)) {
2,147,483,647✔
415
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
2,147,483,647✔
416
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
2,147,483,647✔
417
      tstrncpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
934,846,786✔
418
      tstrncpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
934,845,309✔
419
    } else if (pRawExpr->isPseudoColumn) {
1,592,012,091✔
420
      // all pseudo column are translate to function with same name
421
      tstrncpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
64,626,081✔
422
      if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_column") == 0) {
64,624,817✔
423
        SValueNode* pColId = (SValueNode*)nodesListGetNode(((SFunctionNode*)pExpr)->pParameterList, 0);
35,870✔
424
        snprintf(pExpr->userAlias, sizeof(pExpr->userAlias), "%%%%%s", pColId->literal);
35,870✔
425
      } else if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_tbname") == 0) {
64,587,577✔
426
        tstrncpy(pExpr->userAlias, "%%tbname", TSDB_COL_NAME_LEN);
33,036✔
427
      } else {
428
        tstrncpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
64,552,399✔
429
      }
430
    } else {
431
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
1,527,386,496✔
432

433
      // See TS-3398.
434
      // Len of pRawExpr->p could be larger than len of aliasName[TSDB_COL_NAME_LEN].
435
      // If aliasName is truncated, hash value of aliasName could be the same.
436
      uint64_t hashVal = MurmurHash3_64(pRawExpr->p, pRawExpr->n);
1,527,388,388✔
437
      snprintf(pExpr->aliasName, TSDB_COL_NAME_LEN, "%" PRIu64, hashVal);
1,527,387,792✔
438
      strncpy(pExpr->userAlias, pRawExpr->p, len);
1,527,388,245✔
439
      pExpr->userAlias[len] = 0;
1,527,391,840✔
440
    }
441
  }
442
  pRawExpr->pNode = NULL;
2,147,483,647✔
443
  nodesDestroyNode(pNode);
2,147,483,647✔
444
  return pRealizedExpr;
2,147,483,647✔
445
_err:
×
446
  nodesDestroyNode(pNode);
×
447
  return NULL;
×
448
}
449

450
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
808,369,650✔
451
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
808,369,650✔
452
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
96✔
453
    return nil_token;
×
454
  }
455
  SRawExprNode* target = (SRawExprNode*)pNode;
808,370,171✔
456
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
808,370,171✔
457
  return t;
808,373,606✔
458
}
459

460
SNodeList* createColsFuncParamNodeList(SAstCreateContext* pCxt, SNode* pNode, SNodeList* pNodeList, SToken* pAlias) {
1,017,016✔
461
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
1,017,016✔
462
  SNode*        pFuncNode = pRawExpr->pNode;
1,017,016✔
463
  CHECK_PARSER_STATUS(pCxt);
1,017,016✔
464
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
1,017,016✔
465
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
466
  }
467
  CHECK_PARSER_STATUS(pCxt);
1,017,016✔
468
  if (pFuncNode->type != QUERY_NODE_FUNCTION) {
1,017,016✔
469
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
470
  }
471
  CHECK_PARSER_STATUS(pCxt);
1,017,016✔
472
  SNodeList* list = NULL;
1,017,016✔
473
  pCxt->errCode = nodesMakeList(&list);
1,017,016✔
474
  CHECK_MAKE_NODE(list);
1,017,016✔
475
  pCxt->errCode = nodesListAppend(list, pFuncNode);
1,017,016✔
476
  CHECK_PARSER_STATUS(pCxt);
1,017,016✔
477
  pCxt->errCode = nodesListAppendList(list, pNodeList);
1,017,016✔
478
  CHECK_PARSER_STATUS(pCxt);
1,017,016✔
479
  return list;
1,017,016✔
480

481
_err:
×
482
  nodesDestroyNode(pFuncNode);
×
483
  nodesDestroyList(pNodeList);
×
484
  return NULL;
×
485
}
486

487
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
1,070,457,849✔
488
  CHECK_PARSER_STATUS(pCxt);
1,070,457,849✔
489
  SNodeList* list = NULL;
1,070,451,762✔
490
  pCxt->errCode = nodesMakeList(&list);
1,070,456,321✔
491
  CHECK_MAKE_NODE(list);
1,070,461,388✔
492
  pCxt->errCode = nodesListAppend(list, pNode);
1,070,461,388✔
493
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
1,070,456,038✔
494
    nodesDestroyList(list);
×
495
    return NULL;
×
496
  }
497
  return list;
1,070,457,353✔
498
_err:
4,826✔
499
  nodesDestroyNode(pNode);
4,826✔
500
  return NULL;
4,826✔
501
}
502

503
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
845,557,675✔
504
  CHECK_PARSER_STATUS(pCxt);
845,557,675✔
505
  pCxt->errCode = nodesListAppend(pList, pNode);
845,561,466✔
506
  return pList;
845,562,583✔
507
_err:
1,690✔
508
  nodesDestroyNode(pNode);
1,690✔
509
  nodesDestroyList(pList);
1,690✔
510
  return NULL;
1,690✔
511
}
512

513
SPrivSetArgs privArgsAdd(SAstCreateContext* pCxt, SPrivSetArgs arg1, SPrivSetArgs arg2) {
11,840✔
514
  CHECK_PARSER_STATUS(pCxt);
11,840✔
515
  SPrivSetArgs merged = arg1;
11,840✔
516
  merged.nPrivArgs += arg2.nPrivArgs;
11,840✔
517
  if (merged.nPrivArgs > TSDB_PRIV_MAX_INPUT_ARGS) {
11,840✔
518
    pCxt->errCode =
×
519
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
520
                                "Invalid privilege types: exceed max privilege number:%d", TSDB_PRIV_MAX_INPUT_ARGS);
521
    CHECK_PARSER_STATUS(pCxt);
×
522
  }
523
  for (int32_t i = 0; i < PRIV_GROUP_CNT; ++i) {
59,200✔
524
    if (arg2.privSet.set[i]) {
47,360✔
525
      merged.privSet.set[i] |= arg2.privSet.set[i];
11,840✔
526
    }
527
  }
528
  if (merged.selectCols) {
11,840✔
529
    if (arg2.selectCols) {
×
530
      pCxt->errCode = nodesListAppendList((SNodeList*)merged.selectCols, (SNodeList*)arg2.selectCols);
×
531
      CHECK_PARSER_STATUS(pCxt);
×
532
    }
533
  } else if (arg2.selectCols) {
11,840✔
534
    merged.selectCols = arg2.selectCols;
×
535
  }
536
  if (LIST_LENGTH((SNodeList*)merged.selectCols) > TSDB_MAX_COLUMNS) {
11,840✔
537
    pCxt->errCode =
×
538
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
539
                                "Invalid privilege columns: SELECT exceed max columns number:%d", TSDB_MAX_COLUMNS);
540
    CHECK_PARSER_STATUS(pCxt);
×
541
  }
542
  if (merged.insertCols) {
11,840✔
543
    if (arg2.insertCols) {
×
544
      pCxt->errCode = nodesListAppendList((SNodeList*)merged.insertCols, (SNodeList*)arg2.insertCols);
×
545
      CHECK_PARSER_STATUS(pCxt);
×
546
    }
547
  } else if (arg2.insertCols) {
11,840✔
548
    merged.insertCols = arg2.insertCols;
×
549
  }
550
  if (LIST_LENGTH((SNodeList*)merged.insertCols) > TSDB_MAX_COLUMNS) {
11,840✔
551
    pCxt->errCode =
×
552
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
553
                                "Invalid privilege columns: INSERT exceed max columns number:%d", TSDB_MAX_COLUMNS);
554
    CHECK_PARSER_STATUS(pCxt);
×
555
  }
556
  if (merged.updateCols) {
11,840✔
557
    if (arg2.updateCols) {
×
558
      pCxt->errCode = nodesListAppendList((SNodeList*)merged.updateCols, (SNodeList*)arg2.updateCols);
×
559
      CHECK_PARSER_STATUS(pCxt);
×
560
    }
561
  } else if (arg2.updateCols) {
11,840✔
562
    merged.updateCols = arg2.updateCols;
×
563
  }
564
  if (LIST_LENGTH((SNodeList*)merged.updateCols) > TSDB_MAX_COLUMNS) {
11,840✔
565
    pCxt->errCode =
×
566
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
567
                                "Invalid privilege columns: UPDATE exceed max columns number:%d", TSDB_MAX_COLUMNS);
568
    CHECK_PARSER_STATUS(pCxt);
×
569
  }
570
_err:
11,840✔
571
  return merged;
11,840✔
572
}
573

574
SPrivSetArgs privArgsSetType(SAstCreateContext* pCxt, EPrivType type) {
×
575
  CHECK_PARSER_STATUS(pCxt);
×
576
  SPrivSetArgs args = {.nPrivArgs = 1};
×
577
  privAddType(&args.privSet, type);
578
_err:
×
579
  return args;
×
580
}
581

582
SPrivSetArgs privArgsSetCols(SAstCreateContext* pCxt, SNodeList* selectCols, SNodeList* insertCols,
×
583
                             SNodeList* updateCols) {
584
  CHECK_PARSER_STATUS(pCxt);
×
585
  SPrivSetArgs args = {.nPrivArgs = 1, .selectCols = selectCols, .insertCols = insertCols, .updateCols = updateCols};
×
586
_err:
×
587
  return args;
×
588
}
589

590
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
1,021,944,118✔
591
  CHECK_PARSER_STATUS(pCxt);
1,021,944,118✔
592
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
1,021,943,977✔
593
    return NULL;
×
594
  }
595
  SColumnNode* col = NULL;
1,021,945,522✔
596
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
1,021,946,390✔
597
  CHECK_MAKE_NODE(col);
1,021,944,914✔
598
  if (NULL != pTableAlias) {
1,021,944,914✔
599
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
209,950,629✔
600
  }
601
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
1,021,944,914✔
602
  return (SNode*)col;
1,021,942,602✔
603
_err:
×
604
  return NULL;
×
605
}
606

607
/**
608
 * @param type: 1 with mask; 0 without mask
609
 */
610
SNode* createColumnNodeExt(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName, int8_t type) {
×
611
  SNode* result = createColumnNode(pCxt, pTableAlias, pColumnName);
×
612
  if (result != NULL) {
×
613
    if (type == 1) ((SColumnNode*)result)->hasMask = 1;
×
614
  }
615
  return result;
×
616
}
617

618
SNode* createPlaceHolderColumnNode(SAstCreateContext* pCxt, SNode* pColId) {
35,870✔
619
  CHECK_PARSER_STATUS(pCxt);
35,870✔
620
  SFunctionNode* pFunc = NULL;
35,870✔
621
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
35,870✔
622
  CHECK_PARSER_STATUS(pCxt);
35,870✔
623
  tstrncpy(pFunc->functionName, "_placeholder_column", TSDB_FUNC_NAME_LEN);
35,870✔
624
  ((SValueNode*)pColId)->notReserved = true;
35,870✔
625
  pCxt->errCode = nodesListMakeAppend(&pFunc->pParameterList, pColId);
35,870✔
626
  CHECK_PARSER_STATUS(pCxt);
35,870✔
627
  pFunc->tz = pCxt->pQueryCxt->timezone;
35,870✔
628
  pFunc->charsetCxt = pCxt->pQueryCxt->charsetCxt;
35,870✔
629
  return (SNode*)pFunc;
35,870✔
630
_err:
×
631
  return NULL;
×
632
}
633

634
static void copyValueTrimEscape(char* buf, int32_t bufLen, const SToken* pToken, bool trim) {
374,316,996✔
635
  int32_t len = TMIN(pToken->n, bufLen - 1);
374,316,996✔
636
  if (trim && (pToken->z[0] == TS_ESCAPE_CHAR)) {
374,317,102✔
637
    int32_t i = 1, j = 0;
27,390✔
638
    for (; i < len - 1; ++i) {
154,380✔
639
      buf[j++] = pToken->z[i];
126,990✔
640
      if (pToken->z[i] == TS_ESCAPE_CHAR) {
126,990✔
641
        if (pToken->z[i + 1] == TS_ESCAPE_CHAR) ++i;
64,242✔
642
      }
643
    }
644
    buf[j] = 0;
27,390✔
645
  } else {
646
    tstrncpy(buf, pToken->z, len + 1);
374,289,712✔
647
  }
648
}
374,316,494✔
649

650
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
374,316,022✔
651
  CHECK_PARSER_STATUS(pCxt);
374,316,022✔
652
  SValueNode* val = NULL;
374,317,372✔
653
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
374,317,372✔
654
  CHECK_MAKE_NODE(val);
374,318,260✔
655
  if (!(val->literal = taosMemoryMalloc(pLiteral->n + 1))) {
374,318,260✔
656
    pCxt->errCode = terrno;
×
657
    nodesDestroyNode((SNode*)val);
×
658
    return NULL;
×
659
  }
660
  copyValueTrimEscape(val->literal, pLiteral->n + 1, pLiteral,
374,317,382✔
661
                      pCxt->pQueryCxt->hasDupQuoteChar && (TK_NK_ID == pLiteral->type));
374,317,478✔
662
  if (TK_NK_STRING == pLiteral->type) {
374,315,144✔
663
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
47,156,236✔
664
  }
665
  val->node.resType.type = dataType;
374,316,149✔
666
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
374,316,159✔
667
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
374,316,391✔
668
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
10,599✔
669
  }
670
  val->translate = false;
374,316,391✔
671
  val->tz = pCxt->pQueryCxt->timezone;
374,314,843✔
672
  val->charsetCxt = pCxt->pQueryCxt->charsetCxt;
374,316,176✔
673
  return (SNode*)val;
374,314,403✔
674
_err:
×
675
  return NULL;
×
676
}
677

678
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
120,540,138✔
679
  CHECK_PARSER_STATUS(pCxt);
120,540,138✔
680
  SValueNode* val = NULL;
120,543,648✔
681
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
120,543,673✔
682
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
120,553,310✔
683
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
12✔
684
    goto _exit;
×
685
  }
686
  if (pLiteral) {
120,549,856✔
687
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
120,382,528✔
688
    if (!val->literal) {
120,380,217✔
689
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
690
      goto _exit;
×
691
    }
692
  } else if (pNode) {
167,328✔
693
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
167,328✔
694
    if (!nodesIsExprNode(pRawExpr->pNode)) {
167,328✔
695
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
696
      goto _exit;
×
697
    }
698
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
167,328✔
699
    if (!val->literal) {
167,328✔
700
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
701
      goto _exit;
×
702
    }
703
  } else {
704
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
705
    goto _exit;
×
706
  }
707
  if (!val->literal) {
120,549,526✔
708
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
709
    goto _exit;
×
710
  }
711

712
  val->node.resType.type = dataType;
120,542,243✔
713
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
120,548,375✔
714
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
120,541,493✔
715
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
716
  }
717
_exit:
120,541,493✔
718
  nodesDestroyNode(pNode);
120,540,610✔
719
  if (pCxt->errCode != 0) {
120,535,869✔
720
    nodesDestroyNode((SNode*)val);
×
721
    return NULL;
×
722
  }
723
  return (SNode*)val;
120,539,964✔
724
_err:
×
725
  nodesDestroyNode(pNode);
×
726
  return NULL;
×
727
}
728

729
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
46,815✔
730
                             SNode* pRight) {
731
  SValueNode* val = NULL;
46,815✔
732
  CHECK_PARSER_STATUS(pCxt);
46,815✔
733

734
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
46,815✔
735
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
46,815✔
736
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
737
    goto _exit;
×
738
  }
739
  if (pLiteral) {
46,815✔
740
    if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) {
46,815✔
741
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
742
      goto _exit;
×
743
    }
744
  } else {
745
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
746
    goto _exit;
×
747
  }
748

749
  val->node.resType.type = dataType;
46,815✔
750
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
46,815✔
751
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
46,815✔
752
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
753
  }
754
_exit:
46,815✔
755
  nodesDestroyNode(pLeft);
46,815✔
756
  nodesDestroyNode(pRight);
46,815✔
757
  CHECK_PARSER_STATUS(pCxt);
46,815✔
758
  return (SNode*)val;
46,815✔
759
_err:
×
760
  nodesDestroyNode((SNode*)val);
×
761
  nodesDestroyNode(pLeft);
×
762
  nodesDestroyNode(pRight);
×
763
  return NULL;
×
764
}
765

766
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
7,922,963✔
767
  if (!pHintList) return false;
7,922,963✔
768
  SNode* pNode;
769
  FOREACH(pNode, pHintList) {
1,350✔
770
    SHintNode* pHint = (SHintNode*)pNode;
1,350✔
771
    if (pHint->option == hint) {
1,350✔
772
      return true;
1,350✔
773
    }
774
  }
775
  return false;
×
776
}
777

778
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
7,926,553✔
779
                       int32_t paramNum) {
780
  void* value = NULL;
7,926,553✔
781
  switch (opt) {
7,926,553✔
782
    case HINT_SKIP_TSMA:
3,590✔
783
    case HINT_BATCH_SCAN:
784
    case HINT_NO_BATCH_SCAN: {
785
      if (paramNum > 0) {
3,590✔
786
        return true;
718✔
787
      }
788
      break;
2,872✔
789
    }
790
    case HINT_SORT_FOR_GROUP:
54,900✔
791
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
54,900✔
792
      break;
54,450✔
793
    case HINT_PARTITION_FIRST:
1,800✔
794
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
1,800✔
795
      break;
900✔
796
    case HINT_PARA_TABLES_SORT:
7,865,537✔
797
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
7,865,537✔
798
      break;
7,865,537✔
799
    case HINT_SMALLDATA_TS_SORT:
×
800
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
801
      break;
×
802
    case HINT_HASH_JOIN:
726✔
803
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
726✔
804
      break;
726✔
805
    case HINT_WIN_OPTIMIZE_BATCH:
×
806
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_BATCH)) return true;
×
807
      break;
×
808
    case HINT_WIN_OPTIMIZE_SINGLE:
×
809
      if (paramNum > 0 || hasHint(*ppHintList, HINT_WIN_OPTIMIZE_SINGLE)) return true;
×
810
      break;
×
811
    default:
×
812
      return true;
×
813
  }
814

815
  SHintNode* hint = NULL;
7,924,485✔
816
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
7,924,485✔
817
  if (!hint) {
7,924,485✔
818
    return true;
×
819
  }
820
  hint->option = opt;
7,924,485✔
821
  hint->value = value;
7,924,485✔
822

823
  if (NULL == *ppHintList) {
7,924,485✔
824
    pCxt->errCode = nodesMakeList(ppHintList);
7,923,767✔
825
    if (!*ppHintList) {
7,923,767✔
826
      nodesDestroyNode((SNode*)hint);
×
827
      return true;
×
828
    }
829
  }
830

831
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
7,924,485✔
832
  if (pCxt->errCode) {
7,924,485✔
833
    return true;
×
834
  }
835

836
  return false;
7,924,485✔
837
}
838

839
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
460,690,562✔
840
  CHECK_PARSER_STATUS(pCxt);
460,690,562✔
841
  if (NULL == pLiteral || pLiteral->n <= 5) {
460,691,650✔
842
    return NULL;
452,764,044✔
843
  }
844
  SNodeList* pHintList = NULL;
7,927,606✔
845
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
7,924,844✔
846
  if (!hint) return NULL;
7,924,844✔
847
  int32_t     i = 0;
7,924,844✔
848
  bool        quit = false;
7,924,844✔
849
  bool        inParamList = false;
7,924,844✔
850
  bool        lastComma = false;
7,924,844✔
851
  EHintOption opt = 0;
7,924,844✔
852
  int32_t     paramNum = 0;
7,924,844✔
853
  SToken      paramList[10];
7,924,844✔
854
  while (!quit) {
47,554,818✔
855
    SToken t0 = {0};
47,552,391✔
856
    if (hint[i] == 0) {
47,552,391✔
857
      break;
7,922,417✔
858
    }
859
    t0.n = tGetToken(&hint[i], &t0.type, NULL);
39,629,974✔
860
    t0.z = hint + i;
39,629,974✔
861
    i += t0.n;
39,629,974✔
862

863
    switch (t0.type) {
39,629,974✔
864
      case TK_BATCH_SCAN:
2,154✔
865
        lastComma = false;
2,154✔
866
        if (0 != opt || inParamList) {
2,154✔
867
          quit = true;
×
868
          break;
×
869
        }
870
        opt = HINT_BATCH_SCAN;
2,154✔
871
        break;
2,154✔
872
      case TK_NO_BATCH_SCAN:
1,436✔
873
        lastComma = false;
1,436✔
874
        if (0 != opt || inParamList) {
1,436✔
875
          quit = true;
×
876
          break;
×
877
        }
878
        opt = HINT_NO_BATCH_SCAN;
1,436✔
879
        break;
1,436✔
880
      case TK_SORT_FOR_GROUP:
54,900✔
881
        lastComma = false;
54,900✔
882
        if (0 != opt || inParamList) {
54,900✔
883
          quit = true;
×
884
          break;
×
885
        }
886
        opt = HINT_SORT_FOR_GROUP;
54,900✔
887
        break;
54,900✔
888
      case TK_PARTITION_FIRST:
1,800✔
889
        lastComma = false;
1,800✔
890
        if (0 != opt || inParamList) {
1,800✔
891
          quit = true;
×
892
          break;
×
893
        }
894
        opt = HINT_PARTITION_FIRST;
1,800✔
895
        break;
1,800✔
896
      case TK_PARA_TABLES_SORT:
7,865,537✔
897
        lastComma = false;
7,865,537✔
898
        if (0 != opt || inParamList) {
7,865,537✔
899
          quit = true;
×
900
          break;
×
901
        }
902
        opt = HINT_PARA_TABLES_SORT;
7,865,537✔
903
        break;
7,865,537✔
904
      case TK_SMALLDATA_TS_SORT:
×
905
        lastComma = false;
×
906
        if (0 != opt || inParamList) {
×
907
          quit = true;
×
908
          break;
×
909
        }
910
        opt = HINT_SMALLDATA_TS_SORT;
×
911
        break;
×
912
      case TK_HASH_JOIN:
726✔
913
        lastComma = false;
726✔
914
        if (0 != opt || inParamList) {
726✔
915
          quit = true;
×
916
          break;
×
917
        }
918
        opt = HINT_HASH_JOIN;
726✔
919
        break;
726✔
920
      case TK_SKIP_TSMA:
×
921
        lastComma = false;
×
922
        if (0 != opt || inParamList) {
×
923
          quit = true;
×
924
          break;
×
925
        }
926
        opt = HINT_SKIP_TSMA;
×
927
        break;
×
928
      case TK_WIN_OPTIMIZE_BATCH:
×
929
        lastComma = false;
×
930
        if (0 != opt || inParamList) {
×
931
          quit = true;
×
932
          break;
×
933
        }
934
        opt = HINT_WIN_OPTIMIZE_BATCH;
×
935
        break;
×
936
      case TK_WIN_OPTIMIZE_SINGLE:
×
937
        lastComma = false;
×
938
        if (0 != opt || inParamList) {
×
939
          quit = true;
×
940
          break;
×
941
        }
942
        opt = HINT_WIN_OPTIMIZE_SINGLE;
×
943
        break;
×
944
      case TK_NK_LP:
7,926,553✔
945
        lastComma = false;
7,926,553✔
946
        if (0 == opt || inParamList) {
7,926,553✔
947
          quit = true;
×
948
        }
949
        inParamList = true;
7,926,553✔
950
        break;
7,926,553✔
951
      case TK_NK_RP:
7,926,553✔
952
        lastComma = false;
7,926,553✔
953
        if (0 == opt || !inParamList) {
7,926,553✔
954
          quit = true;
×
955
        } else {
956
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
7,926,553✔
957
          inParamList = false;
7,926,553✔
958
          paramNum = 0;
7,926,553✔
959
          opt = 0;
7,926,553✔
960
        }
961
        break;
7,926,553✔
962
      case TK_NK_ID:
1,077✔
963
        lastComma = false;
1,077✔
964
        if (0 == opt || !inParamList) {
1,077✔
965
          quit = true;
359✔
966
        } else {
967
          paramList[paramNum++] = t0;
718✔
968
        }
969
        break;
1,077✔
970
      case TK_NK_COMMA:
718✔
971
        if (lastComma) {
718✔
972
          quit = true;
×
973
        }
974
        lastComma = true;
718✔
975
        break;
718✔
976
      case TK_NK_SPACE:
15,848,520✔
977
        break;
15,848,520✔
978
      default:
×
979
        lastComma = false;
×
980
        quit = true;
×
981
        break;
×
982
    }
983
  }
984

985
  taosMemoryFree(hint);
7,924,844✔
986
  return pHintList;
7,924,844✔
987
_err:
×
988
  return NULL;
×
989
}
990

991
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
1,339,050✔
992
  trimEscape(pCxt, pLiteral, false);
1,339,050✔
993
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
1,339,050✔
994
}
995

996
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
58,150,648✔
997
  CHECK_PARSER_STATUS(pCxt);
58,150,648✔
998
  SValueNode* val = NULL;
58,151,034✔
999
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
58,150,156✔
1000
  CHECK_MAKE_NODE(val);
58,151,034✔
1001
  if (pLiteral->type == TK_NK_STRING) {
58,151,034✔
1002
    // like '100s' or "100d"
1003
    // check format: ^[0-9]+[smwbauhdny]$'
1004
    if (pLiteral->n < 4) {
15,038✔
1005
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
698✔
1006
      return NULL;
698✔
1007
    }
1008
    char unit = pLiteral->z[pLiteral->n - 2];
14,340✔
1009
    switch (unit) {
14,340✔
1010
      case 'a':
12,246✔
1011
      case 'b':
1012
      case 'd':
1013
      case 'h':
1014
      case 'm':
1015
      case 's':
1016
      case 'u':
1017
      case 'w':
1018
      case 'y':
1019
      case 'n':
1020
        break;
12,246✔
1021
      default:
2,094✔
1022
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
2,094✔
1023
        return NULL;
2,094✔
1024
    }
1025
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
44,703✔
1026
      if (!isdigit(pLiteral->z[i])) {
34,582✔
1027
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
2,125✔
1028
        return NULL;
2,125✔
1029
      }
1030
    }
1031
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
10,121✔
1032
  } else {
1033
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
58,133,960✔
1034
  }
1035
  if (!val->literal) {
58,145,133✔
1036
    nodesDestroyNode((SNode*)val);
×
1037
    pCxt->errCode = terrno;
×
1038
    return NULL;
×
1039
  }
1040
  val->flag |= VALUE_FLAG_IS_DURATION;
58,142,711✔
1041
  val->translate = false;
58,145,731✔
1042
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
58,145,837✔
1043
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
58,145,731✔
1044
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
58,144,467✔
1045
  return (SNode*)val;
58,145,133✔
1046
_err:
×
1047
  return NULL;
×
1048
}
1049

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

1102
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
860,924✔
1103
  CHECK_PARSER_STATUS(pCxt);
860,924✔
1104
  if (NULL == pCxt->pQueryCxt->db) {
860,924✔
1105
    return NULL;
3,371✔
1106
  }
1107

1108
  SValueNode* val = NULL;
857,553✔
1109
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
857,553✔
1110
  CHECK_MAKE_NODE(val);
857,553✔
1111
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
857,553✔
1112
  if (!val->literal) {
857,553✔
1113
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
1114
    nodesDestroyNode((SNode*)val);
×
1115
    return NULL;
×
1116
  }
1117
  val->translate = false;
857,553✔
1118
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
857,553✔
1119
  val->node.resType.bytes = strlen(val->literal);
857,553✔
1120
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
857,553✔
1121
  return (SNode*)val;
857,553✔
1122
_err:
×
1123
  return NULL;
×
1124
}
1125

1126
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
9,534✔
1127
  CHECK_PARSER_STATUS(pCxt);
9,534✔
1128
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
9,534✔
1129
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
1130
    return NULL;
×
1131
  }
1132
  SValueNode* val = NULL;
9,534✔
1133
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
9,534✔
1134
  CHECK_MAKE_NODE(val);
9,534✔
1135
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
9,534✔
1136
  if (!val->literal) {
9,534✔
1137
    pCxt->errCode = terrno;
×
1138
    nodesDestroyNode((SNode*)val);
×
1139
    return NULL;
×
1140
  }
1141
  val->placeholderNo = ++pCxt->placeholderNo;
9,534✔
1142
  if (NULL == pCxt->pPlaceholderValues) {
9,534✔
1143
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
8,966✔
1144
    if (NULL == pCxt->pPlaceholderValues) {
8,966✔
1145
      nodesDestroyNode((SNode*)val);
×
1146
      return NULL;
×
1147
    }
1148
  }
1149
  if (NULL == taosArrayPush(pCxt->pPlaceholderValues, &val)) {
19,068✔
1150
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
1151
    nodesDestroyNode((SNode*)val);
×
1152
    taosArrayDestroy(pCxt->pPlaceholderValues);
×
1153
    return NULL;
×
1154
  }
1155
  return (SNode*)val;
9,534✔
1156
_err:
×
1157
  return NULL;
×
1158
}
1159

1160
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
164,463,601✔
1161
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
164,463,601✔
1162
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
25,099,031✔
1163
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
25,070,051✔
1164
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
25,070,051✔
1165
    nodesDestroyNode(pParam);
25,070,051✔
1166
    return code;
25,070,051✔
1167
  } else {
1168
    return nodesListAppend(pCond->pParameterList, pParam);
139,394,708✔
1169
  }
1170
}
1171

1172
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
82,302,130✔
1173
  CHECK_PARSER_STATUS(pCxt);
82,302,130✔
1174
  SLogicConditionNode* cond = NULL;
82,300,654✔
1175
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
82,300,654✔
1176
  CHECK_MAKE_NODE(cond);
82,302,902✔
1177
  cond->condType = type;
82,302,902✔
1178
  cond->pParameterList = NULL;
82,302,516✔
1179
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
82,300,268✔
1180
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
82,302,130✔
1181
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
82,302,024✔
1182
  }
1183
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
82,303,008✔
1184
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
82,161,471✔
1185
  }
1186
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
82,303,394✔
1187
    nodesDestroyNode((SNode*)cond);
×
1188
    return NULL;
×
1189
  }
1190
  return (SNode*)cond;
82,302,130✔
1191
_err:
×
1192
  nodesDestroyNode(pParam1);
×
1193
  nodesDestroyNode(pParam2);
×
1194
  return NULL;
×
1195
}
1196

1197
static uint8_t getMinusDataType(uint8_t orgType) {
23,334,694✔
1198
  switch (orgType) {
23,334,694✔
1199
    case TSDB_DATA_TYPE_UTINYINT:
16,060,083✔
1200
    case TSDB_DATA_TYPE_USMALLINT:
1201
    case TSDB_DATA_TYPE_UINT:
1202
    case TSDB_DATA_TYPE_UBIGINT:
1203
      return TSDB_DATA_TYPE_BIGINT;
16,060,083✔
1204
    default:
7,274,611✔
1205
      break;
7,274,611✔
1206
  }
1207
  return orgType;
7,274,611✔
1208
}
1209

1210
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
392,636,246✔
1211
  CHECK_PARSER_STATUS(pCxt);
392,636,246✔
1212
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
392,637,845✔
1213
    SValueNode* pVal = (SValueNode*)pLeft;
23,334,656✔
1214
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
23,334,656✔
1215
    if (!pNewLiteral) {
23,334,694✔
1216
      pCxt->errCode = terrno;
×
1217
      goto _err;
×
1218
    }
1219
    if ('+' == pVal->literal[0]) {
23,334,694✔
1220
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal + 1);
×
1221
    } else if ('-' == pVal->literal[0]) {
23,334,694✔
UNCOV
1222
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "%s", pVal->literal + 1);
×
1223
    } else {
1224
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal);
23,334,656✔
1225
    }
1226
    taosMemoryFree(pVal->literal);
23,334,694✔
1227
    pVal->literal = pNewLiteral;
23,334,694✔
1228
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
23,334,694✔
1229
    return pLeft;
23,334,694✔
1230
  }
1231
  if (pLeft && QUERY_NODE_VALUE == nodeType(pLeft)) {
369,303,189✔
1232
    SValueNode* pVal = (SValueNode*)pLeft;
14,892,306✔
1233
    pVal->tz = pCxt->pQueryCxt->timezone;
14,892,306✔
1234
  }
1235
  if (pRight && QUERY_NODE_VALUE == nodeType(pRight)) {
369,303,083✔
1236
    SValueNode* pVal = (SValueNode*)pRight;
187,569,261✔
1237
    pVal->tz = pCxt->pQueryCxt->timezone;
187,569,261✔
1238
  }
1239

1240
  SOperatorNode* op = NULL;
369,303,303✔
1241
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
369,302,031✔
1242
  CHECK_MAKE_NODE(op);
369,306,933✔
1243
  op->opType = type;
369,306,933✔
1244
  op->pLeft = pLeft;
369,306,161✔
1245
  op->pRight = pRight;
369,303,615✔
1246
  op->tz = pCxt->pQueryCxt->timezone;
369,302,139✔
1247
  op->charsetCxt = pCxt->pQueryCxt->charsetCxt;
369,302,659✔
1248
  return (SNode*)op;
369,301,702✔
1249
_err:
×
1250
  nodesDestroyNode(pLeft);
×
1251
  nodesDestroyNode(pRight);
×
1252
  return NULL;
×
1253
}
1254

1255
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
16,755,812✔
1256
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
16,755,812✔
1257
  CHECK_PARSER_STATUS(pCxt);
16,756,198✔
1258
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
16,755,426✔
1259
  CHECK_PARSER_STATUS(pCxt);
16,755,706✔
1260
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
16,754,934✔
1261
  CHECK_PARSER_STATUS(pCxt);
16,754,654✔
1262
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
16,755,812✔
1263
  CHECK_PARSER_STATUS(pCxt);
16,756,584✔
1264
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
16,756,584✔
1265
  CHECK_PARSER_STATUS(pCxt);
16,755,320✔
1266
  return pRet;
16,754,828✔
1267
_err:
×
1268
  nodesDestroyNode(pNew);
×
1269
  nodesDestroyNode(pGE);
×
1270
  nodesDestroyNode(pLE);
×
1271
  nodesDestroyNode(pExpr);
×
1272
  nodesDestroyNode(pLeft);
×
1273
  nodesDestroyNode(pRight);
×
1274
  return NULL;
×
1275
}
1276

1277
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
3,279,203✔
1278
  SNode *pNew = NULL, *pLT = NULL, *pGT = NULL;
3,279,203✔
1279
  CHECK_PARSER_STATUS(pCxt);
3,279,203✔
1280
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
3,279,203✔
1281
  CHECK_PARSER_STATUS(pCxt);
3,279,203✔
1282
  pLT = createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft);
3,279,203✔
1283
  CHECK_PARSER_STATUS(pCxt);
3,279,203✔
1284
  pGT = createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pNew, pRight);
3,279,203✔
1285
  CHECK_PARSER_STATUS(pCxt);
3,279,203✔
1286
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, pLT, pGT);
3,279,203✔
1287
  CHECK_PARSER_STATUS(pCxt);
3,279,203✔
1288
  return pRet;
3,279,203✔
1289
_err:
×
1290
  nodesDestroyNode(pNew);
×
1291
  nodesDestroyNode(pGT);
×
1292
  nodesDestroyNode(pLT);
×
1293
  nodesDestroyNode(pExpr);
×
1294
  nodesDestroyNode(pLeft);
×
1295
  nodesDestroyNode(pRight);
×
1296
  return NULL;
×
1297
}
1298

1299
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
63,035,012✔
1300
  CHECK_PARSER_STATUS(pCxt);
63,035,012✔
1301
  SColumnNode* pCol = NULL;
63,036,556✔
1302
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
63,036,768✔
1303
  CHECK_MAKE_NODE(pCol);
63,039,508✔
1304
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
63,039,508✔
1305
  if (NULL == pFuncName) {
63,039,402✔
1306
    tstrncpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME, TSDB_COL_NAME_LEN);
51,701,130✔
1307
  } else {
1308
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
11,338,272✔
1309
  }
1310
  pCol->isPrimTs = true;
63,037,540✔
1311
  return (SNode*)pCol;
63,038,910✔
1312
_err:
×
1313
  return NULL;
×
1314
}
1315

1316
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
396,144,158✔
1317
  CHECK_PARSER_STATUS(pCxt);
396,144,158✔
1318
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
396,145,334✔
1319
    return createPrimaryKeyCol(pCxt, pFuncName);
11,340,531✔
1320
  }
1321
  SFunctionNode* func = NULL;
384,807,176✔
1322
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
384,807,573✔
1323
  CHECK_MAKE_NODE(func);
384,807,753✔
1324
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
384,807,753✔
1325
  func->pParameterList = pParameterList;
384,808,003✔
1326
  func->tz = pCxt->pQueryCxt->timezone;
384,806,807✔
1327
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
384,803,479✔
1328
  return (SNode*)func;
384,808,086✔
1329
_err:
×
1330
  nodesDestroyList(pParameterList);
×
1331
  return NULL;
×
1332
}
1333

1334
SNode* createPHTbnameFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
33,036✔
1335
  CHECK_PARSER_STATUS(pCxt);
33,036✔
1336
  SFunctionNode* func = NULL;
33,036✔
1337
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
33,036✔
1338
  CHECK_MAKE_NODE(func);
33,036✔
1339
  tstrncpy(func->functionName, "_placeholder_tbname", TSDB_FUNC_NAME_LEN);
33,036✔
1340
  func->pParameterList = pParameterList;
33,036✔
1341
  func->tz = pCxt->pQueryCxt->timezone;
33,036✔
1342
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
33,036✔
1343
  return (SNode*)func;
33,036✔
1344
_err:
×
1345
  nodesDestroyList(pParameterList);
×
1346
  return NULL;
×
1347
}
1348

1349
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
94,711,752✔
1350
  SFunctionNode* func = NULL;
94,711,752✔
1351
  CHECK_PARSER_STATUS(pCxt);
94,711,752✔
1352
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
94,711,752✔
1353
  CHECK_MAKE_NODE(func);
94,711,752✔
1354
  tstrncpy(func->functionName, "cast", TSDB_FUNC_NAME_LEN);
94,711,752✔
1355
  func->node.resType = dt;
94,711,752✔
1356
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
94,711,752✔
1357
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
76,175,820✔
1358
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
18,535,932✔
1359
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
3,572,762✔
1360
  }
1361
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
94,711,752✔
1362
  CHECK_PARSER_STATUS(pCxt);
94,711,752✔
1363
  func->tz = pCxt->pQueryCxt->timezone;
94,711,752✔
1364
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
94,711,752✔
1365

1366
  return (SNode*)func;
94,711,752✔
1367
_err:
×
1368
  nodesDestroyNode((SNode*)func);
×
1369
  nodesDestroyNode(pExpr);
×
1370
  return NULL;
×
1371
}
1372

1373
SNode* createPositionFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
198,146✔
1374
  SFunctionNode* func = NULL;
198,146✔
1375
  CHECK_PARSER_STATUS(pCxt);
198,146✔
1376
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
198,146✔
1377
  CHECK_MAKE_NODE(func);
198,146✔
1378
  tstrncpy(func->functionName, "position", TSDB_FUNC_NAME_LEN);
198,146✔
1379
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
198,146✔
1380
  CHECK_PARSER_STATUS(pCxt);
198,146✔
1381
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
198,146✔
1382
  CHECK_PARSER_STATUS(pCxt);
198,146✔
1383
  return (SNode*)func;
198,146✔
1384
_err:
×
1385
  nodesDestroyNode((SNode*)func);
×
1386
  nodesDestroyNode(pExpr);
×
1387
  nodesDestroyNode(pExpr2);
×
1388
  return NULL;
×
1389
}
1390

1391
SNode* createTrimFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, ETrimType type) {
35,026✔
1392
  SFunctionNode* func = NULL;
35,026✔
1393
  CHECK_PARSER_STATUS(pCxt);
35,026✔
1394
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
35,026✔
1395
  CHECK_MAKE_NODE(func);
35,026✔
1396
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
35,026✔
1397
  func->trimType = type;
35,026✔
1398
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
35,026✔
1399
  CHECK_PARSER_STATUS(pCxt);
35,026✔
1400
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
35,026✔
1401
  return (SNode*)func;
35,026✔
1402
_err:
×
1403
  nodesDestroyNode((SNode*)func);
×
1404
  nodesDestroyNode(pExpr);
×
1405
  return NULL;
×
1406
}
1407

1408
SNode* createTrimFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, ETrimType type) {
109,616✔
1409
  SFunctionNode* func = NULL;
109,616✔
1410
  CHECK_PARSER_STATUS(pCxt);
109,616✔
1411
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
109,616✔
1412
  CHECK_MAKE_NODE(func);
109,616✔
1413
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
109,616✔
1414
  func->trimType = type;
109,616✔
1415
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
109,616✔
1416
  CHECK_PARSER_STATUS(pCxt);
109,616✔
1417
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
109,616✔
1418
  CHECK_PARSER_STATUS(pCxt);
109,616✔
1419
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
109,616✔
1420
  return (SNode*)func;
109,616✔
1421
_err:
×
1422
  nodesDestroyNode((SNode*)func);
×
1423
  nodesDestroyNode(pExpr);
×
1424
  nodesDestroyNode(pExpr2);
×
1425
  return NULL;
×
1426
}
1427

1428
SNode* createSubstrFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
18,266✔
1429
  SFunctionNode* func = NULL;
18,266✔
1430
  CHECK_PARSER_STATUS(pCxt);
18,266✔
1431
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
18,266✔
1432
  CHECK_MAKE_NODE(func);
18,266✔
1433
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
18,266✔
1434
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
18,266✔
1435
  CHECK_PARSER_STATUS(pCxt);
18,266✔
1436
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
18,266✔
1437
  CHECK_PARSER_STATUS(pCxt);
18,266✔
1438
  return (SNode*)func;
18,266✔
1439
_err:
×
1440
  nodesDestroyNode((SNode*)func);
×
1441
  nodesDestroyNode(pExpr);
×
1442
  nodesDestroyNode(pExpr2);
×
1443
  return NULL;
×
1444
}
1445

1446
SNode* createSubstrFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, SNode* pExpr3) {
40,080✔
1447
  SFunctionNode* func = NULL;
40,080✔
1448
  CHECK_PARSER_STATUS(pCxt);
40,080✔
1449
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
40,080✔
1450
  CHECK_MAKE_NODE(func);
40,080✔
1451
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
40,080✔
1452
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
40,080✔
1453
  CHECK_PARSER_STATUS(pCxt);
40,080✔
1454
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
40,080✔
1455
  CHECK_PARSER_STATUS(pCxt);
40,080✔
1456
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr3);
40,080✔
1457
  CHECK_PARSER_STATUS(pCxt);
40,080✔
1458
  return (SNode*)func;
40,080✔
1459
_err:
×
1460
  nodesDestroyNode((SNode*)func);
×
1461
  nodesDestroyNode(pExpr);
×
1462
  nodesDestroyNode(pExpr2);
×
1463
  nodesDestroyNode(pExpr3);
×
1464
  return NULL;
×
1465
}
1466

1467
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
5,424,909✔
1468
  SNodeListNode* list = NULL;
5,424,909✔
1469
  CHECK_PARSER_STATUS(pCxt);
5,425,295✔
1470
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
5,424,417✔
1471
  CHECK_MAKE_NODE(list);
5,424,909✔
1472
  list->pNodeList = pList;
5,424,909✔
1473
  return (SNode*)list;
5,424,909✔
1474
_err:
×
1475
  nodesDestroyList(pList);
×
1476
  return NULL;
×
1477
}
1478

1479
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
×
1480
  SNodeListNode* list = NULL;
×
1481
  CHECK_PARSER_STATUS(pCxt);
×
1482
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
×
1483
  CHECK_MAKE_NODE(list);
×
1484
  pCxt->errCode = nodesListMakeStrictAppend(&list->pNodeList, p1);
×
1485
  CHECK_PARSER_STATUS(pCxt);
×
1486
  pCxt->errCode = nodesListStrictAppend(list->pNodeList, p2);
×
1487
  CHECK_PARSER_STATUS(pCxt);
×
1488
  return (SNode*)list;
×
1489
_err:
×
1490
  nodesDestroyNode((SNode*)list);
×
1491
  nodesDestroyNode(p1);
×
1492
  nodesDestroyNode(p2);
×
1493
  return NULL;
×
1494
}
1495

1496
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
561,079,268✔
1497
  CHECK_PARSER_STATUS(pCxt);
561,079,268✔
1498
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
561,081,599✔
1499
  CHECK_NAME(checkTableName(pCxt, pTableName));
561,079,976✔
1500
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
561,079,513✔
1501
  SRealTableNode* realTable = NULL;
561,073,768✔
1502
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
561,074,418✔
1503
  CHECK_MAKE_NODE(realTable);
561,078,742✔
1504
  if (NULL != pDbName) {
561,078,742✔
1505
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
155,452,482✔
1506
  } else {
1507
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
405,626,260✔
1508
  }
1509
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
561,068,417✔
1510
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
64,046,451✔
1511
  } else {
1512
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
497,023,727✔
1513
  }
1514
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
561,066,424✔
1515
  return (SNode*)realTable;
561,064,318✔
1516
_err:
5,789✔
1517
  return NULL;
5,789✔
1518
}
1519

1520
SNode* createPlaceHolderTableNode(SAstCreateContext* pCxt, EStreamPlaceholder type, SToken* pTableAlias) {
115,564✔
1521
  CHECK_PARSER_STATUS(pCxt);
115,564✔
1522

1523
  SPlaceHolderTableNode* phTable = NULL;
115,564✔
1524
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PLACE_HOLDER_TABLE, (SNode**)&phTable);
115,564✔
1525
  CHECK_MAKE_NODE(phTable);
115,564✔
1526

1527
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
115,564✔
1528
    COPY_STRING_FORM_ID_TOKEN(phTable->table.tableAlias, pTableAlias);
2,545✔
1529
  }
1530

1531
  phTable->placeholderType = type;
115,564✔
1532
  return (SNode*)phTable;
115,564✔
1533
_err:
×
1534
  return NULL;
×
1535
}
1536

1537
SNode* createStreamNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pStreamName) {
287,017✔
1538
  CHECK_PARSER_STATUS(pCxt);
287,017✔
1539
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
287,017✔
1540
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
287,017✔
1541
  SStreamNode* pStream = NULL;
286,841✔
1542
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM, (SNode**)&pStream);
286,841✔
1543
  CHECK_MAKE_NODE(pStream);
286,841✔
1544
  if (NULL != pDbName) {
286,841✔
1545
    COPY_STRING_FORM_ID_TOKEN(pStream->dbName, pDbName);
191,272✔
1546
  } else {
1547
    snprintf(pStream->dbName, sizeof(pStream->dbName), "%s", pCxt->pQueryCxt->db);
95,569✔
1548
  }
1549
  COPY_STRING_FORM_ID_TOKEN(pStream->streamName, pStreamName);
286,841✔
1550
  return (SNode*)pStream;
286,841✔
1551
_err:
176✔
1552
  return NULL;
176✔
1553
}
1554

1555
SNode* createRecalcRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
10,475✔
1556
  SStreamCalcRangeNode* pRange = NULL;
10,475✔
1557
  CHECK_PARSER_STATUS(pCxt);
10,475✔
1558
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_CALC_RANGE, (SNode**)&pRange);
10,475✔
1559
  CHECK_MAKE_NODE(pRange);
10,475✔
1560
  if (NULL == pStart && NULL == pEnd) {
10,475✔
1561
    pRange->calcAll = true;
×
1562
  } else {
1563
    pRange->calcAll = false;
10,475✔
1564
    pRange->pStart = pStart;
10,475✔
1565
    pRange->pEnd = pEnd;
10,475✔
1566
  }
1567

1568
  return (SNode*)pRange;
10,475✔
1569
_err:
×
1570
  nodesDestroyNode((SNode*)pRange);
×
1571
  nodesDestroyNode(pStart);
×
1572
  nodesDestroyNode(pEnd);
×
1573
  return NULL;
×
1574
}
1575

1576
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
28,991,943✔
1577
  CHECK_PARSER_STATUS(pCxt);
28,991,943✔
1578
  if (!checkTableName(pCxt, pTableAlias)) {
28,991,943✔
1579
    return NULL;
×
1580
  }
1581
  STempTableNode* tempTable = NULL;
28,991,943✔
1582
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
28,991,943✔
1583
  CHECK_MAKE_NODE(tempTable);
28,991,943✔
1584
  tempTable->pSubquery = pSubquery;
28,991,943✔
1585
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
28,991,943✔
1586
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
679,854✔
1587
  } else {
1588
    taosRandStr(tempTable->table.tableAlias, 32);
28,312,089✔
1589
  }
1590
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
28,991,943✔
1591
    tstrncpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
25,069,598✔
1592
    ((SSelectStmt*)pSubquery)->isSubquery = true;
25,069,598✔
1593
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
3,922,345✔
1594
    tstrncpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
3,922,345✔
1595
  }
1596
  return (SNode*)tempTable;
28,991,943✔
1597
_err:
×
1598
  nodesDestroyNode(pSubquery);
×
1599
  return NULL;
×
1600
}
1601

1602
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, EJoinSubType stype, SNode* pLeft, SNode* pRight,
33,699,636✔
1603
                           SNode* pJoinCond) {
1604
  CHECK_PARSER_STATUS(pCxt);
33,699,636✔
1605
  SJoinTableNode* joinTable = NULL;
33,699,636✔
1606
  pCxt->errCode = nodesMakeNode(QUERY_NODE_JOIN_TABLE, (SNode**)&joinTable);
33,699,636✔
1607
  CHECK_MAKE_NODE(joinTable);
33,699,636✔
1608
  joinTable->joinType = type;
33,699,636✔
1609
  joinTable->subType = stype;
33,699,636✔
1610
  joinTable->pLeft = pLeft;
33,699,636✔
1611
  joinTable->pRight = pRight;
33,699,636✔
1612
  joinTable->pOnCond = pJoinCond;
33,699,636✔
1613
  return (SNode*)joinTable;
33,699,636✔
1614
_err:
×
1615
  nodesDestroyNode(pLeft);
×
1616
  nodesDestroyNode(pRight);
×
1617
  nodesDestroyNode(pJoinCond);
×
1618
  return NULL;
×
1619
}
1620

1621
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
18,353✔
1622
  CHECK_PARSER_STATUS(pCxt);
18,353✔
1623
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
18,353✔
1624
  CHECK_NAME(checkViewName(pCxt, pViewName));
18,001✔
1625
  SViewNode* pView = NULL;
17,649✔
1626
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
17,649✔
1627
  CHECK_MAKE_NODE(pView);
17,649✔
1628
  if (NULL != pDbName) {
17,649✔
1629
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
703✔
1630
  } else {
1631
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
16,946✔
1632
  }
1633
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
17,649✔
1634
  return (SNode*)pView;
17,649✔
1635
_err:
704✔
1636
  return NULL;
704✔
1637
}
1638

1639
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) {
25,757,031✔
1640
  CHECK_PARSER_STATUS(pCxt);
25,757,031✔
1641
  SLimitNode* limitNode = NULL;
25,757,031✔
1642
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
25,757,031✔
1643
  CHECK_MAKE_NODE(limitNode);
25,757,031✔
1644
  limitNode->limit = (SValueNode*)pLimit;
25,757,031✔
1645
  if (NULL != pOffset) {
25,757,031✔
1646
    limitNode->offset = (SValueNode*)pOffset;
6,132,020✔
1647
  }
1648
  return (SNode*)limitNode;
25,757,031✔
1649
_err:
×
1650
  return NULL;
×
1651
}
1652

1653
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
133,954,165✔
1654
  CHECK_PARSER_STATUS(pCxt);
133,954,165✔
1655
  SOrderByExprNode* orderByExpr = NULL;
133,954,165✔
1656
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
133,954,165✔
1657
  CHECK_MAKE_NODE(orderByExpr);
133,954,165✔
1658
  orderByExpr->pExpr = pExpr;
133,954,165✔
1659
  orderByExpr->order = order;
133,954,165✔
1660
  if (NULL_ORDER_DEFAULT == nullOrder) {
133,954,165✔
1661
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
133,952,935✔
1662
  }
1663
  orderByExpr->nullOrder = nullOrder;
133,954,165✔
1664
  return (SNode*)orderByExpr;
133,954,165✔
1665
_err:
×
1666
  nodesDestroyNode(pExpr);
×
1667
  return NULL;
×
1668
}
1669

1670
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
9,304,663✔
1671
  CHECK_PARSER_STATUS(pCxt);
9,304,663✔
1672
  SSessionWindowNode* session = NULL;
9,304,663✔
1673
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
9,304,663✔
1674
  CHECK_MAKE_NODE(session);
9,304,663✔
1675
  session->pCol = (SColumnNode*)pCol;
9,304,663✔
1676
  session->pGap = (SValueNode*)pGap;
9,304,663✔
1677
  return (SNode*)session;
9,304,663✔
1678
_err:
×
1679
  nodesDestroyNode(pCol);
×
1680
  nodesDestroyNode(pGap);
×
1681
  return NULL;
×
1682
}
1683

1684
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr, SNodeList* pOptions, SNode* pTrueForLimit) {
3,474,989✔
1685
  SStateWindowNode* state = NULL;
3,474,989✔
1686
  CHECK_PARSER_STATUS(pCxt);
3,474,989✔
1687
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
3,474,989✔
1688
  CHECK_MAKE_NODE(state);
3,474,989✔
1689
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
3,474,989✔
1690
  CHECK_MAKE_NODE(state->pCol);
3,474,989✔
1691
  state->pExpr = pExpr;
3,474,989✔
1692
  state->pTrueForLimit = pTrueForLimit;
3,474,989✔
1693
  if (pOptions != NULL) {
3,474,989✔
1694
    if (pOptions->length >= 1) {
20,978✔
1695
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 0), &state->pExtend);
20,978✔
1696
      CHECK_MAKE_NODE(state->pExtend);
20,978✔
1697
    }
1698
    if (pOptions->length == 2) {
20,978✔
1699
      pCxt->errCode = nodesCloneNode(nodesListGetNode(pOptions, 1), &state->pZeroth);
×
1700
      CHECK_MAKE_NODE(state->pZeroth);
×
1701
    }
1702
    nodesDestroyList(pOptions);
20,978✔
1703
  }
1704
  return (SNode*)state;
3,474,989✔
1705
_err:
×
1706
  nodesDestroyNode((SNode*)state);
×
1707
  nodesDestroyNode(pExpr);
×
1708
  nodesDestroyNode(pTrueForLimit);
×
1709
  nodesDestroyList(pOptions);
×
1710
  return NULL;
×
1711
}
1712

1713
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond, SNode* pTrueForLimit) {
3,136,376✔
1714
  SEventWindowNode* pEvent = NULL;
3,136,376✔
1715
  CHECK_PARSER_STATUS(pCxt);
3,136,376✔
1716
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
3,136,376✔
1717
  CHECK_MAKE_NODE(pEvent);
3,136,376✔
1718
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
3,136,376✔
1719
  CHECK_MAKE_NODE(pEvent->pCol);
3,136,376✔
1720
  pEvent->pStartCond = pStartCond;
3,136,376✔
1721
  pEvent->pEndCond = pEndCond;
3,136,376✔
1722
  pEvent->pTrueForLimit = pTrueForLimit;
3,136,376✔
1723
  return (SNode*)pEvent;
3,136,376✔
1724
_err:
×
1725
  nodesDestroyNode((SNode*)pEvent);
×
1726
  nodesDestroyNode(pStartCond);
×
1727
  nodesDestroyNode(pEndCond);
×
1728
  nodesDestroyNode(pTrueForLimit);
×
1729
  return NULL;
×
1730
}
1731

1732
SNode* createCountWindowNode(SAstCreateContext* pCxt, const SToken* pCountToken, const SToken* pSlidingToken,
×
1733
                             SNodeList* pColList) {
1734
  SCountWindowNode* pCount = NULL;
×
1735
  CHECK_PARSER_STATUS(pCxt);
×
1736
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
×
1737
  CHECK_MAKE_NODE(pCount);
×
1738
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1739
  CHECK_MAKE_NODE(pCount->pCol);
×
1740
  pCount->windowCount = taosStr2Int64(pCountToken->z, NULL, 10);
×
1741
  if (pSlidingToken == NULL) {
×
1742
    pCount->windowSliding = taosStr2Int64(pSlidingToken->z, NULL, 10);
×
1743
  } else {
1744
    pCount->windowSliding = taosStr2Int64(pCountToken->z, NULL, 10);
×
1745
  }
1746
  pCount->pColList = pColList;
×
1747
  return (SNode*)pCount;
×
1748
_err:
×
1749
  nodesDestroyNode((SNode*)pCount);
×
1750
  return NULL;
×
1751
}
1752

1753
SNode* createCountWindowNodeFromArgs(SAstCreateContext* pCxt, SNode* arg) {
3,125,928✔
1754
  SCountWindowArgs* args = (SCountWindowArgs*)arg;
3,125,928✔
1755
  SCountWindowNode* pCount = NULL;
3,125,928✔
1756
  CHECK_PARSER_STATUS(pCxt);
3,125,928✔
1757
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
3,125,928✔
1758
  CHECK_MAKE_NODE(pCount);
3,125,928✔
1759
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
3,125,928✔
1760
  CHECK_MAKE_NODE(pCount->pCol);
3,125,928✔
1761
  pCount->windowCount = args->count;
3,125,928✔
1762
  pCount->windowSliding = args->sliding;
3,125,928✔
1763
  pCount->pColList = args->pColList;
3,125,928✔
1764
  args->pColList = NULL;
3,125,928✔
1765
  nodesDestroyNode(arg);
3,125,928✔
1766
  return (SNode*)pCount;
3,125,928✔
1767
_err:
×
1768
  nodesDestroyNode((SNode*)pCount);
×
1769
  return NULL;
×
1770
}
1771

1772
SNode* createCountWindowArgs(SAstCreateContext* pCxt, const SToken* countToken, const SToken* slidingToken,
3,125,928✔
1773
                             SNodeList* colList) {
1774
  CHECK_PARSER_STATUS(pCxt);
3,125,928✔
1775

1776
  SCountWindowArgs* args = NULL;
3,125,928✔
1777
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW_ARGS, (SNode**)&args);
3,125,928✔
1778
  CHECK_MAKE_NODE(args);
3,125,928✔
1779
  args->count = taosStr2Int64(countToken->z, NULL, 10);
3,125,928✔
1780
  if (slidingToken && slidingToken->type == TK_NK_INTEGER) {
3,125,928✔
1781
    args->sliding = taosStr2Int64(slidingToken->z, NULL, 10);
19,970✔
1782
  } else {
1783
    args->sliding = taosStr2Int64(countToken->z, NULL, 10);
3,105,958✔
1784
  }
1785
  args->pColList = colList;
3,125,928✔
1786
  return (SNode*)args;
3,125,928✔
1787
_err:
×
1788
  return NULL;
×
1789
}
1790

1791
SNode* createAnomalyWindowNode(SAstCreateContext* pCxt, SNode* pExpr, const SToken* pFuncOpt) {
×
1792
  SAnomalyWindowNode* pAnomaly = NULL;
×
1793
  CHECK_PARSER_STATUS(pCxt);
×
1794
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ANOMALY_WINDOW, (SNode**)&pAnomaly);
×
1795
  CHECK_MAKE_NODE(pAnomaly);
×
1796
  pAnomaly->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1797
  CHECK_MAKE_NODE(pAnomaly->pCol);
×
1798
  pAnomaly->pExpr = pExpr;
×
1799
  if (pFuncOpt == NULL) {
×
1800
    tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
1801
  } else {
1802
    (void)trimString(pFuncOpt->z, pFuncOpt->n, pAnomaly->anomalyOpt, sizeof(pAnomaly->anomalyOpt));
×
1803
  }
1804
  return (SNode*)pAnomaly;
×
1805
_err:
×
1806
  nodesDestroyNode((SNode*)pAnomaly);
×
1807
  return NULL;
×
1808
}
1809

1810
SNode* createIntervalWindowNodeExt(SAstCreateContext* pCxt, SNode* pInter, SNode* pSliding) {
101,667✔
1811
  SIntervalWindowNode* pInterval = NULL;
101,667✔
1812
  CHECK_PARSER_STATUS(pCxt);
101,667✔
1813
  if (pInter) {
101,667✔
1814
    pInterval = (SIntervalWindowNode*)pInter;
80,479✔
1815
  } else {
1816
    pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&pInterval);
21,188✔
1817
    CHECK_MAKE_NODE(pInterval);
21,188✔
1818
  }
1819
  pInterval->pCol = createPrimaryKeyCol(pCxt, NULL);
101,667✔
1820
  CHECK_MAKE_NODE(pInterval->pCol);
101,667✔
1821
  pInterval->pSliding = ((SSlidingWindowNode*)pSliding)->pSlidingVal;
101,667✔
1822
  pInterval->pSOffset = ((SSlidingWindowNode*)pSliding)->pOffset;
101,667✔
1823
  return (SNode*)pInterval;
101,667✔
1824
_err:
×
1825
  nodesDestroyNode((SNode*)pInter);
×
1826
  nodesDestroyNode((SNode*)pInterval);
×
1827
  nodesDestroyNode((SNode*)pSliding);
×
1828
  return NULL;
×
1829
}
1830

1831
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
32,079,058✔
1832
                                SNode* pFill) {
1833
  SIntervalWindowNode* interval = NULL;
32,079,058✔
1834
  CHECK_PARSER_STATUS(pCxt);
32,079,058✔
1835
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
32,079,058✔
1836
  CHECK_MAKE_NODE(interval);
32,079,058✔
1837
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
32,079,058✔
1838
  CHECK_MAKE_NODE(interval->pCol);
32,079,058✔
1839
  interval->pInterval = pInterval;
32,079,058✔
1840
  interval->pOffset = pOffset;
32,079,058✔
1841
  interval->pSliding = pSliding;
32,079,058✔
1842
  interval->pFill = pFill;
32,079,058✔
1843
  TAOS_SET_OBJ_ALIGNED(&interval->timeRange, TSWINDOW_INITIALIZER);
32,079,058✔
1844
  interval->timezone = pCxt->pQueryCxt->timezone;
32,079,058✔
1845
  return (SNode*)interval;
32,079,058✔
1846
_err:
×
1847
  nodesDestroyNode((SNode*)interval);
×
1848
  nodesDestroyNode(pInterval);
×
1849
  nodesDestroyNode(pOffset);
×
1850
  nodesDestroyNode(pSliding);
×
1851
  nodesDestroyNode(pFill);
×
1852
  return NULL;
×
1853
}
1854

1855
SNode* createPeriodWindowNode(SAstCreateContext* pCxt, SNode* pPeriodTime, SNode* pOffset) {
15,465✔
1856
  SPeriodWindowNode* pPeriod = NULL;
15,465✔
1857
  CHECK_PARSER_STATUS(pCxt);
15,465✔
1858
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PERIOD_WINDOW, (SNode**)&pPeriod);
15,465✔
1859
  CHECK_MAKE_NODE(pPeriod);
15,465✔
1860
  pPeriod->pOffset = pOffset;
15,465✔
1861
  pPeriod->pPeroid = pPeriodTime;
15,465✔
1862
  return (SNode*)pPeriod;
15,465✔
1863
_err:
×
1864
  nodesDestroyNode((SNode*)pOffset);
×
1865
  nodesDestroyNode((SNode*)pPeriodTime);
×
1866
  nodesDestroyNode((SNode*)pPeriod);
×
1867
  return NULL;
×
1868
}
1869

1870
SNode* createWindowOffsetNode(SAstCreateContext* pCxt, SNode* pStartOffset, SNode* pEndOffset) {
552,300✔
1871
  SWindowOffsetNode* winOffset = NULL;
552,300✔
1872
  CHECK_PARSER_STATUS(pCxt);
552,300✔
1873
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WINDOW_OFFSET, (SNode**)&winOffset);
552,300✔
1874
  CHECK_MAKE_NODE(winOffset);
552,300✔
1875
  winOffset->pStartOffset = pStartOffset;
552,300✔
1876
  winOffset->pEndOffset = pEndOffset;
552,300✔
1877
  return (SNode*)winOffset;
552,300✔
1878
_err:
×
1879
  nodesDestroyNode((SNode*)winOffset);
×
1880
  nodesDestroyNode(pStartOffset);
×
1881
  nodesDestroyNode(pEndOffset);
×
1882
  return NULL;
×
1883
}
1884

1885
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
2,835,840✔
1886
  SFillNode* fill = NULL;
2,835,840✔
1887
  CHECK_PARSER_STATUS(pCxt);
2,835,840✔
1888
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
2,835,454✔
1889
  CHECK_MAKE_NODE(fill);
2,836,612✔
1890
  fill->mode = mode;
2,836,612✔
1891
  fill->pValues = pValues;
2,837,104✔
1892
  fill->pWStartTs = NULL;
2,835,840✔
1893
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&(fill->pWStartTs));
2,836,718✔
1894
  CHECK_MAKE_NODE(fill->pWStartTs);
2,837,490✔
1895
  tstrncpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart", TSDB_FUNC_NAME_LEN);
2,835,840✔
1896
  return (SNode*)fill;
2,835,348✔
1897
_err:
×
1898
  nodesDestroyNode((SNode*)fill);
×
1899
  nodesDestroyNode(pValues);
×
1900
  return NULL;
×
1901
}
1902

1903
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
46,217,061✔
1904
  SGroupingSetNode* groupingSet = NULL;
46,217,061✔
1905
  CHECK_PARSER_STATUS(pCxt);
46,217,061✔
1906
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
46,217,061✔
1907
  CHECK_MAKE_NODE(groupingSet);
46,217,061✔
1908
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
46,217,061✔
1909
  groupingSet->pParameterList = NULL;
46,217,061✔
1910
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
46,217,061✔
1911
  CHECK_PARSER_STATUS(pCxt);
46,217,061✔
1912
  return (SNode*)groupingSet;
46,217,061✔
1913
_err:
×
1914
  nodesDestroyNode((SNode*)groupingSet);
×
1915
  nodesDestroyNode(pNode);
×
1916
  return NULL;
×
1917
}
1918

1919
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
2,275,841✔
1920
  CHECK_PARSER_STATUS(pCxt);
2,275,841✔
1921
  if (isSubQueryNode(pStart) || isSubQueryNode(pEnd) || isSubQueryNode(pInterval)) {
2,276,227✔
1922
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
6,850✔
1923
    CHECK_PARSER_STATUS(pCxt);
4,216✔
1924
  }
1925
  
1926
  if (NULL == pInterval) {
2,270,747✔
1927
    if (pEnd && nodeType(pEnd) == QUERY_NODE_VALUE && ((SValueNode*)pEnd)->flag & VALUE_FLAG_IS_DURATION) {
2,214,021✔
1928
      return createInterpTimeAround(pCxt, pStart, NULL, pEnd);
875,154✔
1929
    }
1930
    return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
1,337,217✔
1931
  }
1932

1933
  return createInterpTimeAround(pCxt, pStart, pEnd, pInterval);
56,726✔
1934

1935
_err:
4,216✔
1936

1937
  nodesDestroyNode(pStart);
4,216✔
1938
  nodesDestroyNode(pEnd);
4,216✔
1939
  nodesDestroyNode(pInterval);
4,216✔
1940
  return NULL;
4,216✔
1941
}
1942

1943
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
919,838✔
1944
  CHECK_PARSER_STATUS(pCxt);
919,838✔
1945
  if (isSubQueryNode(pPoint)) {
919,452✔
1946
    pCxt->errCode = TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_USAGE;
2,494✔
1947
    CHECK_PARSER_STATUS(pCxt);
2,108✔
1948
  }
1949
  
1950
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
917,344✔
1951
_err:
2,108✔
1952
  nodesDestroyNode(pPoint);
2,108✔
1953
  return NULL;
2,108✔
1954
}
1955

1956
SNode* createInterpTimeAround(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
933,144✔
1957
  CHECK_PARSER_STATUS(pCxt);
933,144✔
1958
  SRangeAroundNode* pAround = NULL;
931,494✔
1959
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RANGE_AROUND, (SNode**)&pAround);
932,758✔
1960
  CHECK_PARSER_STATUS(pCxt);
934,514✔
1961
  if (NULL == pEnd) {
934,022✔
1962
    pAround->pRange = createInterpTimePoint(pCxt, pStart);
877,296✔
1963
  } else {
1964
    pAround->pRange = createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
56,726✔
1965
  }
1966
  pAround->pInterval = pInterval;
933,250✔
1967
  CHECK_PARSER_STATUS(pCxt);
932,864✔
1968
  return (SNode*)pAround;
932,758✔
1969
_err:
×
1970
  return NULL;
×
1971
}
1972

1973
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
22,214,844✔
1974
  CHECK_PARSER_STATUS(pCxt);
22,214,844✔
1975
  SWhenThenNode* pWhenThen = NULL;
22,214,844✔
1976
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
22,214,844✔
1977
  CHECK_MAKE_NODE(pWhenThen);
22,214,844✔
1978
  pWhenThen->pWhen = pWhen;
22,214,844✔
1979
  pWhenThen->pThen = pThen;
22,214,844✔
1980
  return (SNode*)pWhenThen;
22,214,844✔
1981
_err:
×
1982
  nodesDestroyNode(pWhen);
×
1983
  nodesDestroyNode(pThen);
×
1984
  return NULL;
×
1985
}
1986

1987
static int32_t debugPrintNode(SNode* pNode) {
×
1988
  char*   pStr = NULL;
×
1989
  int32_t code = nodesNodeToString(pNode, false, &pStr, NULL);
×
1990
  if (TSDB_CODE_SUCCESS == code) {
×
1991
    (void)printf("%s\n", pStr);
×
1992
    taosMemoryFree(pStr);
×
1993
  }
1994
  return code;
×
1995
}
1996

1997
SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
21,876,127✔
1998
  CHECK_PARSER_STATUS(pCxt);
21,876,127✔
1999
  SCaseWhenNode* pCaseWhen = NULL;
21,876,127✔
2000
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CASE_WHEN, (SNode**)&pCaseWhen);
21,876,127✔
2001
  CHECK_MAKE_NODE(pCaseWhen);
21,876,127✔
2002
  pCaseWhen->pCase = pCase;
21,876,127✔
2003
  pCaseWhen->pWhenThenList = pWhenThenList;
21,876,127✔
2004
  pCaseWhen->pElse = pElse;
21,876,127✔
2005
  pCaseWhen->tz = pCxt->pQueryCxt->timezone;
21,876,127✔
2006
  pCaseWhen->charsetCxt = pCxt->pQueryCxt->charsetCxt;
21,876,127✔
2007
  // debugPrintNode((SNode*)pCaseWhen);
2008
  return (SNode*)pCaseWhen;
21,876,127✔
2009
_err:
×
2010
  nodesDestroyNode(pCase);
×
2011
  nodesDestroyList(pWhenThenList);
×
2012
  nodesDestroyNode(pElse);
×
2013
  return NULL;
×
2014
}
2015

2016
SNode* createNullIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
2,270✔
2017
  SNode *    pCase = NULL, *pEqual = NULL, *pThen = NULL;
2,270✔
2018
  SNode *    pWhenThenNode = NULL, *pElse = NULL;
2,270✔
2019
  SNodeList* pWhenThenList = NULL;
2,270✔
2020
  SNode*     pCaseWhen = NULL;
2,270✔
2021

2022
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2023
  pEqual = createOperatorNode(pCxt, OP_TYPE_EQUAL, pExpr1, pExpr2);
2,270✔
2024
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2025
  SToken nullToken = {
2,270✔
2026
      .n = 4,
2027
      .type = TK_NULL,
2028
      .z = "null",
2029
  };
2030
  pThen = createValueNode(pCxt, TSDB_DATA_TYPE_NULL, &nullToken);
2,270✔
2031
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2032
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pThen);
2,270✔
2033
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2034
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
2,270✔
2035
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2036
  pCxt->errCode = nodesCloneNode(pExpr1, &pElse);
2,270✔
2037
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2038
  pCaseWhen = createCaseWhenNode(pCxt, pCase, pWhenThenList, pElse);
2,270✔
2039
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2040
  // debugPrintNode((SNode*)pCaseWhen);
2041
  return (SNode*)pCaseWhen;
2,270✔
2042
_err:
×
2043
  nodesDestroyNode(pCase);
×
2044
  nodesDestroyNode(pEqual);
×
2045
  nodesDestroyNode(pThen);
×
2046
  nodesDestroyNode(pWhenThenNode);
×
2047
  nodesDestroyNode(pElse);
×
2048
  nodesDestroyList(pWhenThenList);
×
2049
  return NULL;
×
2050
}
2051

2052
SNode* createIfNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
62,286✔
2053
  SNode*     pCase = NULL;
62,286✔
2054
  SNode*     pWhenThenNode = NULL;
62,286✔
2055
  SNodeList* pWhenThenList = NULL;
62,286✔
2056
  SNode*     pCaseWhen = NULL;
62,286✔
2057

2058
  CHECK_PARSER_STATUS(pCxt);
62,286✔
2059
  pWhenThenNode = createWhenThenNode(pCxt, pExpr1, pExpr2);
62,286✔
2060
  CHECK_PARSER_STATUS(pCxt);
62,286✔
2061
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
62,286✔
2062
  CHECK_PARSER_STATUS(pCxt);
62,286✔
2063
  pCaseWhen = createCaseWhenNode(pCxt, pCase, pWhenThenList, pExpr3);
62,286✔
2064
  CHECK_PARSER_STATUS(pCxt);
62,286✔
2065
  // debugPrintNode((SNode*)pCaseWhen);
2066
  return (SNode*)pCaseWhen;
62,286✔
2067
_err:
×
2068
  nodesDestroyNode(pCase);
×
2069
  nodesDestroyNode(pWhenThenNode);
×
2070
  nodesDestroyList(pWhenThenList);
×
2071
  return NULL;
×
2072
}
2073

2074
SNode* createNvlNode(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2) {
8,028✔
2075
  SNode *    pThen = NULL, *pEqual = NULL;
8,028✔
2076
  SNode*     pWhenThenNode = NULL;
8,028✔
2077
  SNodeList* pWhenThenList = NULL;
8,028✔
2078
  SNode*     pCaseWhen = NULL;
8,028✔
2079

2080
  CHECK_PARSER_STATUS(pCxt);
8,028✔
2081
  pEqual = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr1, NULL);
8,028✔
2082
  CHECK_PARSER_STATUS(pCxt);
8,028✔
2083
  pCxt->errCode = nodesCloneNode(pExpr1, &pThen);
8,028✔
2084
  CHECK_PARSER_STATUS(pCxt);
8,028✔
2085
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pThen);
8,028✔
2086
  CHECK_PARSER_STATUS(pCxt);
8,028✔
2087
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
8,028✔
2088
  CHECK_PARSER_STATUS(pCxt);
8,028✔
2089
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, pExpr2);
8,028✔
2090
  CHECK_PARSER_STATUS(pCxt);
8,028✔
2091
  // debugPrintNode((SNode*)pCaseWhen);
2092
  return (SNode*)pCaseWhen;
8,028✔
2093
_err:
×
2094
  nodesDestroyNode(pEqual);
×
2095
  nodesDestroyNode(pThen);
×
2096
  nodesDestroyNode(pWhenThenNode);
×
2097
  nodesDestroyList(pWhenThenList);
×
2098
  return NULL;
×
2099
}
2100

2101
SNode* createNvl2Node(SAstCreateContext* pCxt, SNode* pExpr1, SNode* pExpr2, SNode* pExpr3) {
2,270✔
2102
  SNode *    pEqual = NULL, *pWhenThenNode = NULL;
2,270✔
2103
  SNodeList* pWhenThenList = NULL;
2,270✔
2104
  SNode*     pCaseWhen = NULL;
2,270✔
2105

2106
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2107
  pEqual = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr1, NULL);
2,270✔
2108
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2109
  pWhenThenNode = createWhenThenNode(pCxt, pEqual, pExpr2);
2,270✔
2110
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2111
  pWhenThenList = createNodeList(pCxt, pWhenThenNode);
2,270✔
2112
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2113
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, pExpr3);
2,270✔
2114
  CHECK_PARSER_STATUS(pCxt);
2,270✔
2115
  // debugPrintNode((SNode*)pCaseWhen);
2116
  return (SNode*)pCaseWhen;
2,270✔
2117
_err:
×
2118
  nodesDestroyNode(pEqual);
×
2119
  nodesDestroyNode(pWhenThenNode);
×
2120
  nodesDestroyList(pWhenThenList);
×
2121
  return NULL;
×
2122
}
2123

2124
SNode* createCoalesceNode(SAstCreateContext* pCxt, SNodeList* pParamList) {
1,744✔
2125
  int32_t    sizeParam = LIST_LENGTH(pParamList);
1,744✔
2126
  SNode *    pNotNullCond = NULL, *pWhenThenNode = NULL, *pExpr = NULL;
1,744✔
2127
  SNodeList* pWhenThenList = NULL;
1,744✔
2128
  SNode *    pCaseWhen = NULL, *pThen = NULL;
1,744✔
2129

2130
  CHECK_PARSER_STATUS(pCxt);
1,744✔
2131

2132
  for (int i = 0; i < sizeParam; ++i) {
6,104✔
2133
    pExpr = nodesListGetNode(pParamList, i);
4,360✔
2134

2135
    pCxt->errCode = nodesCloneNode(pExpr, &pThen);
4,360✔
2136
    CHECK_PARSER_STATUS(pCxt);
4,360✔
2137

2138
    pNotNullCond = createOperatorNode(pCxt, OP_TYPE_IS_NOT_NULL, pExpr, NULL);
4,360✔
2139
    CHECK_PARSER_STATUS(pCxt);
4,360✔
2140

2141
    pWhenThenNode = createWhenThenNode(pCxt, pNotNullCond, pThen);
4,360✔
2142
    CHECK_PARSER_STATUS(pCxt);
4,360✔
2143

2144
    if (!pWhenThenList) {
4,360✔
2145
      pWhenThenList = createNodeList(pCxt, pWhenThenNode);
1,744✔
2146
    } else {
2147
      pCxt->errCode = nodesListAppend(pWhenThenList, pWhenThenNode);
2,616✔
2148
    }
2149
    CHECK_PARSER_STATUS(pCxt);
4,360✔
2150
  }
2151

2152
  pCaseWhen = createCaseWhenNode(pCxt, NULL, pWhenThenList, NULL);
1,744✔
2153
  CHECK_PARSER_STATUS(pCxt);
1,744✔
2154
  // debugPrintNode((SNode*)pCaseWhen);
2155
  return (SNode*)pCaseWhen;
1,744✔
2156
_err:
×
2157
  nodesDestroyNode(pExpr);
×
2158
  nodesDestroyNode(pNotNullCond);
×
2159
  nodesDestroyNode(pThen);
×
2160
  nodesDestroyNode(pWhenThenNode);
×
2161
  nodesDestroyList(pWhenThenList);
×
2162
  return NULL;
×
2163
}
2164

2165
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
81,890,014✔
2166
  CHECK_PARSER_STATUS(pCxt);
81,890,014✔
2167
  trimEscape(pCxt, pAlias, false);
81,890,014✔
2168
  SExprNode* pExpr = (SExprNode*)pNode;
81,890,014✔
2169
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
81,890,014✔
2170
  strncpy(pExpr->aliasName, pAlias->z, len);
81,890,014✔
2171
  pExpr->aliasName[len] = '\0';
81,890,014✔
2172
  strncpy(pExpr->userAlias, pAlias->z, len);
81,890,014✔
2173
  pExpr->userAlias[len] = '\0';
81,890,014✔
2174
  pExpr->asAlias = true;
81,890,014✔
2175
  return pNode;
81,890,014✔
2176
_err:
×
2177
  nodesDestroyNode(pNode);
×
2178
  return NULL;
×
2179
}
2180

2181
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
451,577,101✔
2182
  CHECK_PARSER_STATUS(pCxt);
451,577,101✔
2183
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
451,576,093✔
2184
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
451,577,848✔
2185
  }
2186
  return pStmt;
451,576,006✔
2187
_err:
528✔
2188
  nodesDestroyNode(pStmt);
528✔
2189
  nodesDestroyNode(pWhere);
528✔
2190
  return NULL;
528✔
2191
}
2192

2193
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
451,577,081✔
2194
  CHECK_PARSER_STATUS(pCxt);
451,577,081✔
2195
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
451,577,988✔
2196
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
451,578,767✔
2197
  }
2198
  return pStmt;
451,577,222✔
2199
_err:
528✔
2200
  nodesDestroyNode(pStmt);
528✔
2201
  nodesDestroyList(pPartitionByList);
528✔
2202
  return NULL;
528✔
2203
}
2204

2205
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
451,576,572✔
2206
  CHECK_PARSER_STATUS(pCxt);
451,576,572✔
2207
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
451,577,030✔
2208
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
451,578,355✔
2209
  }
2210
  return pStmt;
451,577,486✔
2211
_err:
528✔
2212
  nodesDestroyNode(pStmt);
528✔
2213
  nodesDestroyNode(pWindow);
528✔
2214
  return NULL;
528✔
2215
}
2216

2217
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
451,576,573✔
2218
  CHECK_PARSER_STATUS(pCxt);
451,576,573✔
2219
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
451,576,117✔
2220
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
451,577,337✔
2221
  }
2222
  return pStmt;
451,574,040✔
2223
_err:
528✔
2224
  nodesDestroyNode(pStmt);
528✔
2225
  nodesDestroyList(pGroupByList);
528✔
2226
  return NULL;
528✔
2227
}
2228

2229
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
451,576,580✔
2230
  CHECK_PARSER_STATUS(pCxt);
451,576,580✔
2231
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
451,577,852✔
2232
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
451,578,343✔
2233
  }
2234
  return pStmt;
451,576,038✔
2235
_err:
528✔
2236
  nodesDestroyNode(pStmt);
528✔
2237
  nodesDestroyNode(pHaving);
528✔
2238
  return NULL;
528✔
2239
}
2240

2241
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
425,384,881✔
2242
  CHECK_PARSER_STATUS(pCxt);
425,384,881✔
2243
  if (NULL == pOrderByList) {
425,386,096✔
2244
    return pStmt;
319,932,639✔
2245
  }
2246
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
105,453,457✔
2247
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
95,105,486✔
2248
  } else {
2249
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
10,347,971✔
2250
  }
2251
  return pStmt;
105,453,457✔
2252
_err:
528✔
2253
  nodesDestroyNode(pStmt);
528✔
2254
  nodesDestroyList(pOrderByList);
528✔
2255
  return NULL;
528✔
2256
}
2257

2258
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
425,384,880✔
2259
  CHECK_PARSER_STATUS(pCxt);
425,384,880✔
2260
  if (NULL == pSlimit) {
425,385,276✔
2261
    return pStmt;
423,522,378✔
2262
  }
2263
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,862,898✔
2264
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
1,862,898✔
2265
  }
2266
  return pStmt;
1,862,898✔
2267
_err:
528✔
2268
  nodesDestroyNode(pStmt);
528✔
2269
  nodesDestroyNode(pSlimit);
528✔
2270
  return NULL;
528✔
2271
}
2272

2273
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
425,384,474✔
2274
  CHECK_PARSER_STATUS(pCxt);
425,384,474✔
2275
  if (NULL == pLimit) {
425,382,958✔
2276
    return pStmt;
401,681,461✔
2277
  }
2278
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
23,701,497✔
2279
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
22,694,959✔
2280
  } else {
2281
    ((SSetOperator*)pStmt)->pLimit = pLimit;
1,006,538✔
2282
  }
2283
  return pStmt;
23,701,497✔
2284
_err:
528✔
2285
  nodesDestroyNode(pStmt);
528✔
2286
  nodesDestroyNode(pLimit);
528✔
2287
  return NULL;
528✔
2288
}
2289

2290
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
451,576,585✔
2291
  CHECK_PARSER_STATUS(pCxt);
451,576,585✔
2292
  SSelectStmt* pSelect = (SSelectStmt*)pStmt;
451,576,572✔
2293
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
451,576,572✔
2294
    if (pRange && nodeType(pRange) == QUERY_NODE_RANGE_AROUND) {
451,576,278✔
2295
      pSelect->pRangeAround = pRange;
929,912✔
2296
      SRangeAroundNode* pAround = (SRangeAroundNode*)pRange;
929,912✔
2297
      TSWAP(pSelect->pRange, pAround->pRange);
929,912✔
2298
    } else {
2299
      pSelect->pRange = pRange;
450,646,366✔
2300
    }
2301
  }
2302
  return pStmt;
451,576,125✔
2303
_err:
528✔
2304
  nodesDestroyNode(pStmt);
528✔
2305
  nodesDestroyNode(pRange);
528✔
2306
  return NULL;
528✔
2307
}
2308

2309
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
451,576,558✔
2310
  CHECK_PARSER_STATUS(pCxt);
451,576,558✔
2311
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
451,576,604✔
2312
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
451,576,868✔
2313
  }
2314
  return pStmt;
451,577,338✔
2315
_err:
528✔
2316
  nodesDestroyNode(pStmt);
528✔
2317
  nodesDestroyNode(pEvery);
528✔
2318
  return NULL;
528✔
2319
}
2320

2321
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
451,576,544✔
2322
  CHECK_PARSER_STATUS(pCxt);
451,576,544✔
2323
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
451,576,125✔
2324
    SFillNode* pFillClause = (SFillNode*)pFill;
2,344,277✔
2325
    nodesDestroyNode(pFillClause->pWStartTs);
2,344,277✔
2326
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
2,342,801✔
2327
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
2,345,435✔
2328
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
2,344,277✔
2329
  }
2330
  return pStmt;
451,578,299✔
2331
_err:
528✔
2332
  nodesDestroyNode(pStmt);
528✔
2333
  nodesDestroyNode(pFill);
528✔
2334
  return NULL;
528✔
2335
}
2336

2337
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
26,319,881✔
2338
  CHECK_PARSER_STATUS(pCxt);
26,319,881✔
2339
  if (NULL == pJLimit) {
26,319,881✔
2340
    return pJoin;
26,127,245✔
2341
  }
2342
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
192,636✔
2343
  pJoinNode->pJLimit = pJLimit;
192,636✔
2344

2345
  return pJoin;
192,636✔
2346
_err:
×
2347
  nodesDestroyNode(pJoin);
×
2348
  nodesDestroyNode(pJLimit);
×
2349
  return NULL;
×
2350
}
2351

2352
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
26,319,881✔
2353
  CHECK_PARSER_STATUS(pCxt);
26,319,881✔
2354
  if (NULL == pWinOffset) {
26,319,881✔
2355
    return pJoin;
25,778,037✔
2356
  }
2357
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
541,844✔
2358
  pJoinNode->pWindowOffset = pWinOffset;
541,844✔
2359

2360
  return pJoin;
541,844✔
2361
_err:
×
2362
  nodesDestroyNode(pJoin);
×
2363
  nodesDestroyNode(pWinOffset);
×
2364
  return NULL;
×
2365
}
2366

2367
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
451,577,047✔
2368
                        SNodeList* pHint) {
2369
  CHECK_PARSER_STATUS(pCxt);
451,577,047✔
2370
  SNode* select = NULL;
451,576,131✔
2371
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
451,576,555✔
2372
  CHECK_MAKE_NODE(select);
451,575,942✔
2373
  return select;
451,575,942✔
2374
_err:
528✔
2375
  nodesDestroyList(pProjectionList);
528✔
2376
  nodesDestroyNode(pTable);
528✔
2377
  nodesDestroyList(pHint);
528✔
2378
  return NULL;
528✔
2379
}
2380

2381
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
451,577,114✔
2382
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
451,577,114✔
2383
    if (pCxt->pQueryCxt->biMode) {
451,577,988✔
2384
      ((SSelectStmt*)pStmt)->tagScan = true;
6,046✔
2385
    } else {
2386
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
451,568,374✔
2387
    }
2388
  }
2389
  return pStmt;
451,577,013✔
2390
}
2391

2392
static void setSubquery(SNode* pStmt) {
66,756,724✔
2393
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
66,756,724✔
2394
    ((SSelectStmt*)pStmt)->isSubquery = true;
66,508,584✔
2395
  }
2396
}
66,756,724✔
2397

2398
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
33,378,362✔
2399
  CHECK_PARSER_STATUS(pCxt);
33,378,362✔
2400
  SSetOperator* setOp = NULL;
33,378,362✔
2401
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
33,378,362✔
2402
  CHECK_MAKE_NODE(setOp);
33,378,362✔
2403
  setOp->opType = type;
33,378,362✔
2404
  setOp->pLeft = pLeft;
33,378,362✔
2405
  setSubquery(setOp->pLeft);
33,378,362✔
2406
  setOp->pRight = pRight;
33,378,362✔
2407
  setSubquery(setOp->pRight);
33,378,362✔
2408
  snprintf(setOp->stmtName, TSDB_TABLE_NAME_LEN, "%p", setOp);
33,378,362✔
2409
  return (SNode*)setOp;
33,378,362✔
2410
_err:
×
2411
  nodesDestroyNode(pLeft);
×
2412
  nodesDestroyNode(pRight);
×
2413
  return NULL;
×
2414
}
2415

2416
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
1,817,076✔
2417
  if (!pOptions->walRetentionPeriodIsSet) {
1,817,076✔
2418
    pOptions->walRetentionPeriod =
1,807,810✔
2419
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
2420
  }
2421
  if (!pOptions->walRetentionSizeIsSet) {
1,817,076✔
2422
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
1,816,381✔
2423
  }
2424
  if (!pOptions->walRollPeriodIsSet) {
1,817,076✔
2425
    pOptions->walRollPeriod =
1,817,076✔
2426
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
2427
  }
2428
}
1,817,076✔
2429

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

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

2528
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
2,186,364✔
2529
                                    bool alter) {
2530
  CHECK_PARSER_STATUS(pCxt);
2,186,364✔
2531
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
2,186,364✔
2532
  switch (type) {
2,186,364✔
2533
    case DB_OPTION_BUFFER:
51,294✔
2534
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
51,294✔
2535
      break;
51,294✔
2536
    case DB_OPTION_CACHEMODEL:
61,708✔
2537
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
61,708✔
2538
      break;
61,708✔
2539
    case DB_OPTION_CACHESIZE:
11,121✔
2540
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
11,121✔
2541
      break;
11,121✔
2542
    case DB_OPTION_COMP:
16,199✔
2543
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
16,199✔
2544
      break;
16,199✔
2545
    case DB_OPTION_DAYS: {
281,807✔
2546
      SToken* pToken = pVal;
281,807✔
2547
      if (TK_NK_INTEGER == pToken->type) {
281,807✔
2548
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
225,297✔
2549
      } else {
2550
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
56,510✔
2551
      }
2552
      break;
281,807✔
2553
    }
2554
    case DB_OPTION_FSYNC:
30,626✔
2555
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
30,626✔
2556
      break;
30,626✔
2557
    case DB_OPTION_MAXROWS:
26,177✔
2558
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
26,177✔
2559
      break;
26,177✔
2560
    case DB_OPTION_MINROWS:
29,985✔
2561
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
29,985✔
2562
      break;
29,985✔
2563
    case DB_OPTION_KEEP:
182,080✔
2564
      pDbOptions->pKeep = pVal;
182,080✔
2565
      break;
182,080✔
2566
    case DB_OPTION_PAGES:
12,425✔
2567
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
12,425✔
2568
      break;
12,425✔
2569
    case DB_OPTION_PAGESIZE:
5,321✔
2570
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5,321✔
2571
      break;
5,321✔
2572
    case DB_OPTION_TSDB_PAGESIZE:
4,033✔
2573
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4,033✔
2574
      break;
4,033✔
2575
    case DB_OPTION_PRECISION:
108,122✔
2576
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
108,122✔
2577
      break;
108,122✔
2578
    case DB_OPTION_REPLICA:
494,980✔
2579
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
494,980✔
2580
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
494,980✔
2581
      if (!alter) {
494,980✔
2582
        updateWalOptionsDefault(pDbOptions);
483,683✔
2583
      }
2584
      break;
494,980✔
2585
    case DB_OPTION_STRICT:
×
2586
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
2587
      break;
×
2588
    case DB_OPTION_WAL:
43,989✔
2589
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
43,989✔
2590
      break;
43,989✔
2591
    case DB_OPTION_VGROUPS:
469,237✔
2592
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
469,237✔
2593
      break;
469,237✔
2594
    case DB_OPTION_SINGLE_STABLE:
6,681✔
2595
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
6,681✔
2596
      break;
6,681✔
2597
    case DB_OPTION_RETENTIONS:
×
2598
      pDbOptions->pRetentions = pVal;
×
2599
      break;
×
2600
    case DB_OPTION_WAL_RETENTION_PERIOD:
82,234✔
2601
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
82,234✔
2602
      pDbOptions->walRetentionPeriodIsSet = true;
82,234✔
2603
      break;
82,234✔
2604
    case DB_OPTION_WAL_RETENTION_SIZE:
16,985✔
2605
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
16,985✔
2606
      pDbOptions->walRetentionSizeIsSet = true;
16,985✔
2607
      break;
16,985✔
2608
    case DB_OPTION_WAL_ROLL_PERIOD:
×
2609
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2610
      pDbOptions->walRollPeriodIsSet = true;
×
2611
      break;
×
2612
    case DB_OPTION_WAL_SEGMENT_SIZE:
×
2613
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2614
      break;
×
2615
    case DB_OPTION_STT_TRIGGER:
33,694✔
2616
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
33,694✔
2617
      break;
33,694✔
2618
    case DB_OPTION_TABLE_PREFIX: {
15,830✔
2619
      SValueNode* pNode = (SValueNode*)pVal;
15,830✔
2620
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
15,830✔
2621
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
15,830✔
2622
      } else {
2623
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
2624
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2625
      }
2626
      nodesDestroyNode((SNode*)pNode);
15,830✔
2627
      break;
15,830✔
2628
    }
2629
    case DB_OPTION_TABLE_SUFFIX: {
15,296✔
2630
      SValueNode* pNode = (SValueNode*)pVal;
15,296✔
2631
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
15,296✔
2632
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
15,296✔
2633
      } else {
2634
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
2635
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2636
      }
2637
      nodesDestroyNode((SNode*)pNode);
15,296✔
2638
      break;
15,296✔
2639
    }
2640
    case DB_OPTION_SS_CHUNKPAGES:
3,403✔
2641
      pDbOptions->ssChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
3,403✔
2642
      break;
3,403✔
2643
    case DB_OPTION_SS_KEEPLOCAL: {
5,229✔
2644
      SToken* pToken = pVal;
5,229✔
2645
      if (TK_NK_INTEGER == pToken->type) {
5,229✔
2646
        pDbOptions->ssKeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
1,124✔
2647
      } else {
2648
        pDbOptions->ssKeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
4,105✔
2649
      }
2650
      break;
5,229✔
2651
    }
2652
    case DB_OPTION_SS_COMPACT:
5,509✔
2653
      pDbOptions->ssCompact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
5,509✔
2654
      break;
5,509✔
2655
    case DB_OPTION_KEEP_TIME_OFFSET:
10,898✔
2656
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
10,898✔
2657
        pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
6,348✔
2658
      } else {
2659
        pDbOptions->pKeepTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
4,550✔
2660
      }
2661
      break;
10,898✔
2662
    case DB_OPTION_ENCRYPT_ALGORITHM:
7,029✔
2663
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
7,029✔
2664
      if (strlen(pDbOptions->encryptAlgorithmStr) == 0) pDbOptions->encryptAlgorithm = -1;
7,029✔
2665
      break;
7,029✔
2666
    case DB_OPTION_DNODES:
25,308✔
2667
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
25,308✔
2668
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
703✔
2669
                 TSDB_DNODE_LIST_LEN);
2670
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
703✔
2671
      } else {
2672
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
24,605✔
2673
      }
2674
      break;
25,308✔
2675
    case DB_OPTION_COMPACT_INTERVAL:
30,884✔
2676
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
30,884✔
2677
        pDbOptions->compactInterval = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4,714✔
2678
      } else {
2679
        pDbOptions->pCompactIntervalNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
26,170✔
2680
      }
2681
      break;
30,884✔
2682
    case DB_OPTION_COMPACT_TIME_RANGE:
48,158✔
2683
      pDbOptions->pCompactTimeRangeList = pVal;
48,158✔
2684
      break;
48,158✔
2685
    case DB_OPTION_COMPACT_TIME_OFFSET:
28,392✔
2686
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
28,392✔
2687
        pDbOptions->compactTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
13,860✔
2688
      } else {
2689
        pDbOptions->pCompactTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
14,532✔
2690
      }
2691
      break;
28,392✔
2692
    case DB_OPTION_IS_AUDIT:
2,849✔
2693
      pDbOptions->isAudit = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
2,849✔
2694
      break;
2,849✔
2695
    default:
18,881✔
2696
      break;
18,881✔
2697
  }
2698
  return pOptions;
2,186,364✔
2699
_err:
×
2700
  nodesDestroyNode(pOptions);
×
2701
  return NULL;
×
2702
}
2703

2704
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
1,988,410✔
2705
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
1,988,410✔
2706
}
2707

2708
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
197,954✔
2709
  CHECK_PARSER_STATUS(pCxt);
197,954✔
2710
  switch (pAlterOption->type) {
197,954✔
2711
    case DB_OPTION_KEEP:
60,040✔
2712
    case DB_OPTION_RETENTIONS:
2713
    case DB_OPTION_COMPACT_TIME_RANGE:
2714
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
60,040✔
2715
    default:
137,914✔
2716
      break;
137,914✔
2717
  }
2718
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
137,914✔
2719
_err:
×
2720
  nodesDestroyNode(pOptions);
×
2721
  nodesDestroyList(pAlterOption->pList);
×
2722
  return NULL;
×
2723
}
2724

2725
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
1,300,423✔
2726
  CHECK_PARSER_STATUS(pCxt);
1,300,423✔
2727
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,299,720✔
2728
  SCreateDatabaseStmt* pStmt = NULL;
1,298,609✔
2729
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
1,298,609✔
2730
  CHECK_MAKE_NODE(pStmt);
1,298,609✔
2731
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,298,609✔
2732
  pStmt->ignoreExists = ignoreExists;
1,298,609✔
2733
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
1,298,609✔
2734
  return (SNode*)pStmt;
1,298,609✔
2735
_err:
1,814✔
2736
  nodesDestroyNode(pOptions);
1,814✔
2737
  return NULL;
1,814✔
2738
}
2739

2740
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName, bool force) {
1,070,786✔
2741
  CHECK_PARSER_STATUS(pCxt);
1,070,786✔
2742
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,070,869✔
2743
  SDropDatabaseStmt* pStmt = NULL;
1,070,869✔
2744
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
1,070,869✔
2745
  CHECK_MAKE_NODE(pStmt);
1,070,869✔
2746
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,070,869✔
2747
  pStmt->ignoreNotExists = ignoreNotExists;
1,070,786✔
2748
  pStmt->force = force;
1,070,786✔
2749
  return (SNode*)pStmt;
1,070,869✔
2750
_err:
×
2751
  return NULL;
×
2752
}
2753

2754
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
178,530✔
2755
  CHECK_PARSER_STATUS(pCxt);
178,530✔
2756
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
178,530✔
2757
  SAlterDatabaseStmt* pStmt = NULL;
178,530✔
2758
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
178,530✔
2759
  CHECK_MAKE_NODE(pStmt);
178,530✔
2760
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
178,530✔
2761
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
178,530✔
2762
  return (SNode*)pStmt;
178,530✔
2763
_err:
×
2764
  nodesDestroyNode(pOptions);
×
2765
  return NULL;
×
2766
}
2767

2768
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
1,747,188✔
2769
  CHECK_PARSER_STATUS(pCxt);
1,747,188✔
2770
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,747,188✔
2771
  SFlushDatabaseStmt* pStmt = NULL;
1,747,188✔
2772
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
1,747,188✔
2773
  CHECK_MAKE_NODE(pStmt);
1,747,188✔
2774
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,747,188✔
2775
  return (SNode*)pStmt;
1,747,188✔
2776
_err:
×
2777
  return NULL;
×
2778
}
2779

2780
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
8,039✔
2781
  CHECK_PARSER_STATUS(pCxt);
8,039✔
2782
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
8,039✔
2783
  STrimDatabaseStmt* pStmt = NULL;
8,039✔
2784
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
8,039✔
2785
  CHECK_MAKE_NODE(pStmt);
8,039✔
2786
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
8,039✔
2787
  pStmt->maxSpeed = maxSpeed;
8,039✔
2788
  return (SNode*)pStmt;
8,039✔
2789
_err:
×
2790
  return NULL;
×
2791
}
2792

2793
SNode* createTrimDbWalStmt(SAstCreateContext* pCxt, SToken* pDbName) {
532✔
2794
  CHECK_PARSER_STATUS(pCxt);
532✔
2795
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
532✔
2796
  STrimDbWalStmt* pStmt = NULL;
532✔
2797
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_WAL_STMT, (SNode**)&pStmt);
532✔
2798
  CHECK_MAKE_NODE(pStmt);
532✔
2799
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
532✔
2800
  return (SNode*)pStmt;
532✔
2801
_err:
×
2802
  return NULL;
×
2803
}
2804

2805
SNode* createSsMigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
×
2806
  CHECK_PARSER_STATUS(pCxt);
×
2807
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
×
2808
  SSsMigrateDatabaseStmt* pStmt = NULL;
×
2809
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SSMIGRATE_DATABASE_STMT, (SNode**)&pStmt);
×
2810
  CHECK_MAKE_NODE(pStmt);
×
2811
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
×
2812
  return (SNode*)pStmt;
×
2813
_err:
×
2814
  return NULL;
×
2815
}
2816

2817
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd, bool metaOnly,
28,091✔
2818
                         bool force) {
2819
  CHECK_PARSER_STATUS(pCxt);
28,091✔
2820
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
28,091✔
2821
  SCompactDatabaseStmt* pStmt = NULL;
28,091✔
2822
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
28,091✔
2823
  CHECK_MAKE_NODE(pStmt);
28,091✔
2824
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
28,091✔
2825
  pStmt->pStart = pStart;
28,091✔
2826
  pStmt->pEnd = pEnd;
28,091✔
2827
  pStmt->metaOnly = metaOnly;
28,091✔
2828
  pStmt->force = force;
28,091✔
2829
  return (SNode*)pStmt;
28,091✔
2830
_err:
×
2831
  nodesDestroyNode(pStart);
×
2832
  nodesDestroyNode(pEnd);
×
2833
  return NULL;
×
2834
}
2835

2836
SNode* createCreateMountStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pMountName, SToken* pDnodeId,
1,364✔
2837
                             SToken* pMountPath) {
2838
#ifdef USE_MOUNT
2839
  CHECK_PARSER_STATUS(pCxt);
1,364✔
2840
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
1,364✔
2841
  CHECK_NAME(checkMountPath(pCxt, pMountPath));
1,364✔
2842
  SCreateMountStmt* pStmt = NULL;
1,364✔
2843
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MOUNT_STMT, (SNode**)&pStmt);
1,364✔
2844
  CHECK_MAKE_NODE(pStmt);
1,364✔
2845
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
1,364✔
2846
  COPY_STRING_FORM_STR_TOKEN(pStmt->mountPath, pMountPath);
1,364✔
2847
  pStmt->ignoreExists = ignoreExists;
1,364✔
2848
  if (TK_NK_INTEGER == pDnodeId->type) {
1,364✔
2849
    pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1,364✔
2850
  } else {
2851
    goto _err;
×
2852
  }
2853
  return (SNode*)pStmt;
1,364✔
2854
_err:
×
2855
  nodesDestroyNode((SNode*)pStmt);
×
2856
  return NULL;
×
2857
#else
2858
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
2859
  return NULL;
2860
#endif
2861
}
2862

2863
SNode* createDropMountStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pMountName) {
496✔
2864
#ifdef USE_MOUNT
2865
  CHECK_PARSER_STATUS(pCxt);
496✔
2866
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
496✔
2867
  SDropMountStmt* pStmt = NULL;
496✔
2868
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_MOUNT_STMT, (SNode**)&pStmt);
496✔
2869
  CHECK_MAKE_NODE(pStmt);
496✔
2870
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
496✔
2871
  pStmt->ignoreNotExists = ignoreNotExists;
496✔
2872
  return (SNode*)pStmt;
496✔
2873
_err:
×
2874
  return NULL;
×
2875
#else
2876
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
2877
  return NULL;
2878
#endif
2879
}
2880

2881
SNode* createCompactVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
4,386✔
2882
                                SNode* pEnd, bool metaOnly, bool force) {
2883
  CHECK_PARSER_STATUS(pCxt);
4,386✔
2884
  if (NULL == pDbName) {
4,386✔
2885
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
731✔
2886
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
731✔
2887
    CHECK_PARSER_STATUS(pCxt);
731✔
2888
  }
2889
  SCompactVgroupsStmt* pStmt = NULL;
3,655✔
2890
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_VGROUPS_STMT, (SNode**)&pStmt);
3,655✔
2891
  CHECK_MAKE_NODE(pStmt);
3,655✔
2892
  pStmt->pDbName = pDbName;
3,655✔
2893
  pStmt->vgidList = vgidList;
3,655✔
2894
  pStmt->pStart = pStart;
3,655✔
2895
  pStmt->pEnd = pEnd;
3,655✔
2896
  pStmt->metaOnly = metaOnly;
3,655✔
2897
  pStmt->force = force;
3,655✔
2898
  return (SNode*)pStmt;
3,655✔
2899
_err:
731✔
2900
  nodesDestroyNode(pDbName);
731✔
2901
  nodesDestroyList(vgidList);
731✔
2902
  nodesDestroyNode(pStart);
731✔
2903
  nodesDestroyNode(pEnd);
731✔
2904
  return NULL;
731✔
2905
}
2906

2907
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
52,805,686✔
2908
  CHECK_PARSER_STATUS(pCxt);
52,805,686✔
2909
  STableOptions* pOptions = NULL;
52,804,396✔
2910
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
52,805,815✔
2911
  CHECK_MAKE_NODE(pOptions);
52,812,450✔
2912
  pOptions->maxDelay1 = -1;
52,812,450✔
2913
  pOptions->maxDelay2 = -1;
52,808,561✔
2914
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
52,803,874✔
2915
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
52,810,690✔
2916
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
52,805,810✔
2917
  pOptions->keep = -1;
52,798,402✔
2918
  pOptions->virtualStb = false;
52,802,043✔
2919
  pOptions->commentNull = true;  // mark null
52,805,012✔
2920
  return (SNode*)pOptions;
52,801,504✔
2921
_err:
×
2922
  return NULL;
×
2923
}
2924

2925
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
67,439✔
2926
  CHECK_PARSER_STATUS(pCxt);
67,439✔
2927
  STableOptions* pOptions = NULL;
67,439✔
2928
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
67,439✔
2929
  CHECK_MAKE_NODE(pOptions);
67,439✔
2930
  pOptions->ttl = -1;
67,439✔
2931
  pOptions->commentNull = true;  // mark null
67,439✔
2932
  pOptions->keep = -1;
67,439✔
2933
  return (SNode*)pOptions;
67,439✔
2934
_err:
×
2935
  return NULL;
×
2936
}
2937

2938
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
327,795✔
2939
  CHECK_PARSER_STATUS(pCxt);
327,795✔
2940
  switch (type) {
327,795✔
2941
    case TABLE_OPTION_COMMENT:
48,389✔
2942
      if (checkComment(pCxt, (SToken*)pVal, true)) {
48,389✔
2943
        ((STableOptions*)pOptions)->commentNull = false;
40,199✔
2944
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
40,199✔
2945
      }
2946
      break;
48,389✔
2947
    case TABLE_OPTION_MAXDELAY:
×
2948
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
×
2949
      break;
×
2950
    case TABLE_OPTION_WATERMARK:
×
2951
      ((STableOptions*)pOptions)->pWatermark = pVal;
×
2952
      break;
×
2953
    case TABLE_OPTION_ROLLUP:
×
2954
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
×
2955
      break;
×
2956
    case TABLE_OPTION_TTL: {
102,485✔
2957
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
102,485✔
2958
      if (ttl > INT32_MAX) {
102,485✔
2959
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
713✔
2960
      } else {
2961
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
2962
        ((STableOptions*)pOptions)->ttl = ttl;
101,772✔
2963
      }
2964
      break;
102,485✔
2965
    }
2966
    case TABLE_OPTION_SMA:
74,060✔
2967
      ((STableOptions*)pOptions)->pSma = pVal;
74,060✔
2968
      break;
74,060✔
2969
    case TABLE_OPTION_DELETE_MARK:
×
2970
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
×
2971
      break;
×
2972
    case TABLE_OPTION_KEEP:
71,782✔
2973
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
71,782✔
2974
        ((STableOptions*)pOptions)->keep = taosStr2Int32(((SToken*)pVal)->z, NULL, 10) * 1440;
11,502✔
2975
      } else {
2976
        ((STableOptions*)pOptions)->pKeepNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
60,280✔
2977
      }
2978
      break;
71,782✔
2979
    case TABLE_OPTION_VIRTUAL: {
31,079✔
2980
      int64_t virtualStb = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
31,079✔
2981
      if (virtualStb != 0 && virtualStb != 1) {
31,079✔
2982
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
2983
      } else {
2984
        ((STableOptions*)pOptions)->virtualStb = virtualStb;
31,079✔
2985
      }
2986
      break;
31,079✔
2987
    }
2988
    default:
×
2989
      break;
×
2990
  }
2991
  return pOptions;
327,795✔
2992
_err:
×
2993
  nodesDestroyNode(pOptions);
×
2994
  return NULL;
×
2995
}
2996

2997
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
309,093,415✔
2998
  CHECK_PARSER_STATUS(pCxt);
309,093,415✔
2999
  SColumnOptions* pOptions = NULL;
309,093,415✔
3000
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
309,093,415✔
3001
  CHECK_MAKE_NODE(pOptions);
309,093,415✔
3002
  pOptions->commentNull = true;
309,093,415✔
3003
  pOptions->bPrimaryKey = false;
309,093,415✔
3004
  pOptions->hasRef = false;
309,093,415✔
3005
  return (SNode*)pOptions;
309,093,415✔
3006
_err:
×
3007
  return NULL;
×
3008
}
3009

3010
EColumnOptionType getColumnOptionType(const char* optionType) {
2,089,685✔
3011
  if (0 == strcasecmp(optionType, "ENCODE")) {
2,089,685✔
3012
    return COLUMN_OPTION_ENCODE;
564,845✔
3013
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
1,524,840✔
3014
    return COLUMN_OPTION_COMPRESS;
766,695✔
3015
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
758,145✔
3016
    return COLUMN_OPTION_LEVEL;
756,075✔
3017
  }
3018
  return 0;
2,070✔
3019
}
3020

3021
SNode* setColumnReference(SAstCreateContext* pCxt, SNode* pOptions, SNode* pRef) {
57,266,478✔
3022
  CHECK_PARSER_STATUS(pCxt);
57,266,478✔
3023

3024
  ((SColumnOptions*)pOptions)->hasRef = true;
57,266,478✔
3025
  tstrncpy(((SColumnOptions*)pOptions)->refDb, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
57,266,478✔
3026
  tstrncpy(((SColumnOptions*)pOptions)->refTable, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
57,266,478✔
3027
  tstrncpy(((SColumnOptions*)pOptions)->refColumn, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
57,266,478✔
3028
  return pOptions;
57,266,478✔
3029
_err:
×
3030
  nodesDestroyNode(pOptions);
×
3031
  return NULL;
×
3032
}
3033

3034
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
227,649✔
3035
  CHECK_PARSER_STATUS(pCxt);
227,649✔
3036
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
227,649✔
3037
  return pOptions;
227,649✔
3038
_err:
×
3039
  nodesDestroyNode(pOptions);
×
3040
  return NULL;
×
3041
}
3042

3043
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
2,089,685✔
3044
  CHECK_PARSER_STATUS(pCxt);
2,089,685✔
3045
  char optionType[TSDB_CL_OPTION_LEN];
2,072,258✔
3046

3047
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
2,089,685✔
3048
  strncpy(optionType, pVal1->z, TMIN(pVal1->n, TSDB_CL_OPTION_LEN));
2,089,685✔
3049
  if (0 == strlen(optionType)) {
2,089,685✔
3050
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3051
    return pOptions;
×
3052
  }
3053
  EColumnOptionType type = getColumnOptionType(optionType);
2,089,685✔
3054
  switch (type) {
2,089,685✔
3055
    case COLUMN_OPTION_ENCODE:
564,845✔
3056
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
564,845✔
3057
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
564,845✔
3058
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
564,845✔
3059
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
3060
      }
3061
      break;
564,845✔
3062
    case COLUMN_OPTION_COMPRESS:
766,695✔
3063
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
766,695✔
3064
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
766,695✔
3065
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
766,695✔
3066
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
3067
      }
3068
      break;
766,695✔
3069
    case COLUMN_OPTION_LEVEL:
756,075✔
3070
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
756,075✔
3071
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
756,075✔
3072
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
756,075✔
3073
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
3074
      }
3075
      break;
756,075✔
3076
    default:
2,070✔
3077
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2,070✔
3078
      break;
2,070✔
3079
  }
3080
  return pOptions;
2,089,685✔
3081
_err:
×
3082
  nodesDestroyNode(pOptions);
×
3083
  return NULL;
×
3084
}
3085

3086
SNode* createColumnRefNodeByNode(SAstCreateContext* pCxt, SToken* pColName, SNode* pRef) {
34,359,672✔
3087
  CHECK_PARSER_STATUS(pCxt);
34,359,672✔
3088
  CHECK_NAME(checkColumnName(pCxt, pColName));
34,359,672✔
3089

3090
  SColumnRefNode* pCol = NULL;
34,359,672✔
3091
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
34,359,672✔
3092
  CHECK_MAKE_NODE(pCol);
34,359,672✔
3093
  if (pColName) {
34,359,672✔
3094
    COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
34,359,672✔
3095
  }
3096
  tstrncpy(pCol->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
34,359,672✔
3097
  tstrncpy(pCol->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
34,359,672✔
3098
  tstrncpy(pCol->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
34,359,672✔
3099
  return (SNode*)pCol;
34,359,672✔
3100
_err:
×
3101
  return NULL;
×
3102
}
3103

3104
STokenTriplet* createTokenTriplet(SAstCreateContext* pCxt, SToken pName) {
92,379,362✔
3105
  CHECK_PARSER_STATUS(pCxt);
92,379,362✔
3106

3107
  STokenTriplet* pTokenTri = taosMemoryMalloc(sizeof(STokenTriplet));
92,379,362✔
3108
  CHECK_OUT_OF_MEM(pTokenTri);
92,379,362✔
3109
  pTokenTri->name[0] = pName;
92,379,362✔
3110
  pTokenTri->numOfName = 1;
92,379,362✔
3111

3112
  return pTokenTri;
92,379,362✔
3113
_err:
×
3114
  return NULL;
×
3115
}
3116

3117
STokenTriplet* setColumnName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri, SToken pName) {
92,530,110✔
3118
  CHECK_PARSER_STATUS(pCxt);
92,530,110✔
3119

3120
  if (pTokenTri->numOfName >= 3) {
92,530,110✔
3121
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3122
    goto _err;
×
3123
  }
3124

3125
  pTokenTri->name[pTokenTri->numOfName] = pName;
92,530,110✔
3126
  pTokenTri->numOfName++;
92,530,110✔
3127
  return pTokenTri;
92,530,110✔
3128
_err:
×
3129
  return NULL;
×
3130
}
3131

3132
SNode* createColumnRefNodeByName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri) {
92,379,362✔
3133
  SColumnRefNode* pCol = NULL;
92,379,362✔
3134
  CHECK_PARSER_STATUS(pCxt);
92,379,362✔
3135
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
92,379,362✔
3136
  CHECK_MAKE_NODE(pCol);
92,379,362✔
3137

3138
  switch (pTokenTri->numOfName) {
92,379,362✔
3139
    case 2: {
92,228,614✔
3140
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[0]));
92,228,614✔
3141
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[1]));
92,228,614✔
3142
      snprintf(pCol->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
92,228,614✔
3143
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[0]);
92,228,614✔
3144
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[1]);
92,228,614✔
3145
      break;
92,228,614✔
3146
    }
3147
    case 3: {
150,748✔
3148
      CHECK_NAME(checkDbName(pCxt, &pTokenTri->name[0], true));
150,748✔
3149
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[1]));
150,748✔
3150
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[2]));
150,748✔
3151
      COPY_STRING_FORM_ID_TOKEN(pCol->refDbName, &pTokenTri->name[0]);
150,748✔
3152
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[1]);
150,748✔
3153
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[2]);
150,748✔
3154
      break;
150,748✔
3155
    }
3156
    default: {
×
3157
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3158
      goto _err;
×
3159
    }
3160
  }
3161

3162
  taosMemFree(pTokenTri);
92,379,362✔
3163
  return (SNode*)pCol;
92,379,362✔
3164
_err:
×
3165
  taosMemFree(pTokenTri);
×
3166
  nodesDestroyNode((SNode*)pCol);
×
3167
  return NULL;
×
3168
}
3169

3170
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
307,382,535✔
3171
  CHECK_PARSER_STATUS(pCxt);
307,382,535✔
3172
  CHECK_NAME(checkColumnName(pCxt, pColName));
307,382,535✔
3173
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
307,380,166✔
3174
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
3,655✔
3175
    CHECK_PARSER_STATUS(pCxt);
3,655✔
3176
  }
3177
  SColumnDefNode* pCol = NULL;
307,376,511✔
3178
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
307,376,511✔
3179
  CHECK_MAKE_NODE(pCol);
307,376,511✔
3180
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
307,376,511✔
3181
  pCol->dataType = dataType;
307,376,511✔
3182
  pCol->pOptions = pNode;
307,376,511✔
3183
  pCol->sma = true;
307,376,511✔
3184
  return (SNode*)pCol;
307,376,511✔
3185
_err:
6,024✔
3186
  nodesDestroyNode(pNode);
6,024✔
3187
  return NULL;
6,024✔
3188
}
3189

3190
SDataType createDataType(uint8_t type) {
308,581,648✔
3191
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
308,581,648✔
3192
  return dt;
308,581,648✔
3193
}
3194

3195
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
101,964,342✔
3196
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
101,964,342✔
3197
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
101,964,342✔
3198
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
101,964,342✔
3199
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
101,964,342✔
3200
  return dt;
101,964,342✔
3201
}
3202

3203
SDataType createDecimalDataType(uint8_t type, const SToken* pPrecisionToken, const SToken* pScaleToken) {
214,910✔
3204
  SDataType dt = {0};
214,910✔
3205
  dt.precision = taosStr2UInt8(pPrecisionToken->z, NULL, 10);
214,910✔
3206
  dt.scale = pScaleToken ? taosStr2Int32(pScaleToken->z, NULL, 10) : 0;
214,910✔
3207
  dt.type = decimalTypeFromPrecision(dt.precision);
214,910✔
3208
  dt.bytes = tDataTypes[dt.type].bytes;
214,910✔
3209
  return dt;
214,910✔
3210
}
3211

3212
SNode* createCreateVTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols) {
129,522✔
3213
  SCreateVTableStmt* pStmt = NULL;
129,522✔
3214
  CHECK_PARSER_STATUS(pCxt);
129,522✔
3215
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
129,522✔
3216
  CHECK_MAKE_NODE(pStmt);
129,522✔
3217
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
129,522✔
3218
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
129,522✔
3219
  pStmt->ignoreExists = ignoreExists;
129,522✔
3220
  pStmt->pCols = pCols;
129,522✔
3221
  nodesDestroyNode(pRealTable);
129,522✔
3222
  return (SNode*)pStmt;
129,522✔
3223
_err:
×
3224
  nodesDestroyNode(pRealTable);
×
3225
  nodesDestroyList(pCols);
×
3226
  return NULL;
×
3227
}
3228

3229
SNode* createCreateVSubTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable,
207,046✔
3230
                                 SNodeList* pSpecificColRefs, SNodeList* pColRefs, SNode* pUseRealTable,
3231
                                 SNodeList* pSpecificTags, SNodeList* pValsOfTags) {
3232
  CHECK_PARSER_STATUS(pCxt);
207,046✔
3233
  SCreateVSubTableStmt* pStmt = NULL;
207,046✔
3234
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT, (SNode**)&pStmt);
207,046✔
3235
  CHECK_MAKE_NODE(pStmt);
207,046✔
3236
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
207,046✔
3237
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
207,046✔
3238
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
207,046✔
3239
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
207,046✔
3240
  pStmt->ignoreExists = ignoreExists;
207,046✔
3241
  pStmt->pSpecificTags = pSpecificTags;
207,046✔
3242
  pStmt->pValsOfTags = pValsOfTags;
207,046✔
3243
  pStmt->pSpecificColRefs = pSpecificColRefs;
207,046✔
3244
  pStmt->pColRefs = pColRefs;
207,046✔
3245
  nodesDestroyNode(pRealTable);
207,046✔
3246
  nodesDestroyNode(pUseRealTable);
207,046✔
3247
  return (SNode*)pStmt;
207,046✔
3248
_err:
×
3249
  nodesDestroyNode(pRealTable);
×
3250
  nodesDestroyNode(pUseRealTable);
×
3251
  nodesDestroyList(pSpecificTags);
×
3252
  nodesDestroyList(pValsOfTags);
×
3253
  nodesDestroyList(pSpecificColRefs);
×
3254
  nodesDestroyList(pColRefs);
×
3255
  return NULL;
×
3256
}
3257

3258
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
16,308,730✔
3259
                             SNodeList* pTags, SNode* pOptions) {
3260
  CHECK_PARSER_STATUS(pCxt);
16,308,730✔
3261
  SCreateTableStmt* pStmt = NULL;
16,307,463✔
3262
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
16,307,463✔
3263
  CHECK_MAKE_NODE(pStmt);
16,307,463✔
3264
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
16,307,463✔
3265
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
16,307,463✔
3266
  pStmt->ignoreExists = ignoreExists;
16,307,463✔
3267
  pStmt->pCols = pCols;
16,307,463✔
3268
  pStmt->pTags = pTags;
16,307,463✔
3269
  pStmt->pOptions = (STableOptions*)pOptions;
16,307,463✔
3270
  nodesDestroyNode(pRealTable);
16,307,463✔
3271
  return (SNode*)pStmt;
16,307,463✔
3272
_err:
1,267✔
3273
  nodesDestroyNode(pRealTable);
1,267✔
3274
  nodesDestroyList(pCols);
1,267✔
3275
  nodesDestroyList(pTags);
1,267✔
3276
  nodesDestroyNode(pOptions);
1,267✔
3277
  return NULL;
1,267✔
3278
}
3279

3280
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
36,488,137✔
3281
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
3282
  CHECK_PARSER_STATUS(pCxt);
36,488,137✔
3283
  SCreateSubTableClause* pStmt = NULL;
36,481,971✔
3284
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
36,483,404✔
3285
  CHECK_MAKE_NODE(pStmt);
36,490,393✔
3286
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
36,490,393✔
3287
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
36,486,862✔
3288
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
36,489,077✔
3289
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
36,482,616✔
3290
  pStmt->ignoreExists = ignoreExists;
36,485,735✔
3291
  pStmt->pSpecificTags = pSpecificTags;
36,489,821✔
3292
  pStmt->pValsOfTags = pValsOfTags;
36,479,374✔
3293
  pStmt->pOptions = (STableOptions*)pOptions;
36,475,578✔
3294
  nodesDestroyNode(pRealTable);
36,486,276✔
3295
  nodesDestroyNode(pUseRealTable);
36,484,905✔
3296
  return (SNode*)pStmt;
36,491,028✔
3297
_err:
×
3298
  nodesDestroyNode(pRealTable);
×
3299
  nodesDestroyNode(pOptions);
×
3300
  nodesDestroyNode(pUseRealTable);
×
3301
  nodesDestroyList(pSpecificTags);
×
3302
  nodesDestroyList(pValsOfTags);
×
3303
  return NULL;
×
3304
}
3305

3306
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
1,288✔
3307
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
3308
  CHECK_PARSER_STATUS(pCxt);
1,288✔
3309
  SCreateSubTableFromFileClause* pStmt = NULL;
1,288✔
3310
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
1,288✔
3311
  CHECK_MAKE_NODE(pStmt);
1,288✔
3312
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
1,288✔
3313
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
1,288✔
3314
  pStmt->ignoreExists = ignoreExists;
1,288✔
3315
  pStmt->pSpecificTags = pSpecificTags;
1,288✔
3316
  if (TK_NK_STRING == pFilePath->type) {
1,288✔
3317
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
1,288✔
3318
  } else {
3319
    strncpy(pStmt->filePath, pFilePath->z, pFilePath->n);
×
3320
  }
3321

3322
  nodesDestroyNode(pUseRealTable);
1,288✔
3323
  return (SNode*)pStmt;
1,288✔
3324
_err:
×
3325
  nodesDestroyNode(pUseRealTable);
×
3326
  nodesDestroyList(pSpecificTags);
×
3327
  return NULL;
×
3328
}
3329

3330
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
30,439,343✔
3331
  CHECK_PARSER_STATUS(pCxt);
30,439,343✔
3332
  SCreateMultiTablesStmt* pStmt = NULL;
30,431,110✔
3333
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
30,436,999✔
3334
  CHECK_MAKE_NODE(pStmt);
30,440,822✔
3335
  pStmt->pSubTables = pSubTables;
30,440,822✔
3336
  return (SNode*)pStmt;
30,436,210✔
3337
_err:
×
3338
  nodesDestroyList(pSubTables);
×
3339
  return NULL;
×
3340
}
3341

3342
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
2,126,771✔
3343
  CHECK_PARSER_STATUS(pCxt);
2,126,771✔
3344
  SDropTableClause* pStmt = NULL;
2,126,595✔
3345
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
2,126,595✔
3346
  CHECK_MAKE_NODE(pStmt);
2,126,595✔
3347
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2,126,595✔
3348
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2,126,595✔
3349
  pStmt->ignoreNotExists = ignoreNotExists;
2,126,595✔
3350
  nodesDestroyNode(pRealTable);
2,126,595✔
3351
  return (SNode*)pStmt;
2,126,595✔
3352
_err:
176✔
3353
  nodesDestroyNode(pRealTable);
176✔
3354
  return NULL;
176✔
3355
}
3356

3357
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
2,015,644✔
3358
  CHECK_PARSER_STATUS(pCxt);
2,015,644✔
3359
  SDropTableStmt* pStmt = NULL;
2,015,468✔
3360
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
2,015,468✔
3361
  CHECK_MAKE_NODE(pStmt);
2,015,468✔
3362
  pStmt->pTables = pTables;
2,015,468✔
3363
  pStmt->withOpt = withOpt;
2,015,468✔
3364
  return (SNode*)pStmt;
2,015,468✔
3365
_err:
176✔
3366
  nodesDestroyList(pTables);
176✔
3367
  return NULL;
176✔
3368
}
3369

3370
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
81,396✔
3371
  CHECK_PARSER_STATUS(pCxt);
81,396✔
3372
  SDropSuperTableStmt* pStmt = NULL;
81,396✔
3373
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
81,396✔
3374
  CHECK_MAKE_NODE(pStmt);
81,396✔
3375
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
81,396✔
3376
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
81,396✔
3377
  pStmt->ignoreNotExists = ignoreNotExists;
81,396✔
3378
  pStmt->withOpt = withOpt;
81,396✔
3379
  nodesDestroyNode(pRealTable);
81,396✔
3380
  return (SNode*)pStmt;
81,396✔
3381
_err:
×
3382
  nodesDestroyNode(pRealTable);
×
3383
  return NULL;
×
3384
}
3385

3386
SNode* createDropVirtualTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
69,203✔
3387
  CHECK_PARSER_STATUS(pCxt);
69,203✔
3388
  SDropVirtualTableStmt* pStmt = NULL;
69,203✔
3389
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
69,203✔
3390
  CHECK_MAKE_NODE(pStmt);
69,203✔
3391
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
69,203✔
3392
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
69,203✔
3393
  pStmt->ignoreNotExists = ignoreNotExists;
69,203✔
3394
  pStmt->withOpt = withOpt;
69,203✔
3395
  nodesDestroyNode(pRealTable);
69,203✔
3396
  return (SNode*)pStmt;
69,203✔
3397
_err:
×
3398
  nodesDestroyNode(pRealTable);
×
3399
  return NULL;
×
3400
}
3401

3402
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
18,814,428✔
3403
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
18,814,428✔
3404
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
18,814,428✔
3405
  nodesDestroyNode(pRealTable);
18,814,428✔
3406
  return (SNode*)pStmt;
18,814,428✔
3407
}
3408

3409
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
67,439✔
3410
  CHECK_PARSER_STATUS(pCxt);
67,439✔
3411
  SAlterTableStmt* pStmt = NULL;
59,803✔
3412
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
59,803✔
3413
  CHECK_MAKE_NODE(pStmt);
59,803✔
3414
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
59,803✔
3415
  pStmt->pOptions = (STableOptions*)pOptions;
59,803✔
3416
  return createAlterTableStmtFinalize(pRealTable, pStmt);
59,803✔
3417
_err:
7,636✔
3418
  nodesDestroyNode(pRealTable);
7,636✔
3419
  nodesDestroyNode(pOptions);
7,636✔
3420
  return NULL;
7,636✔
3421
}
3422

3423
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
2,527,116✔
3424
                                    SDataType dataType) {
3425
  CHECK_PARSER_STATUS(pCxt);
2,527,116✔
3426
  CHECK_NAME(checkColumnName(pCxt, pColName));
2,527,116✔
3427
  SAlterTableStmt* pStmt = NULL;
2,526,940✔
3428
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
2,526,940✔
3429
  CHECK_MAKE_NODE(pStmt);
2,526,940✔
3430
  pStmt->alterType = alterType;
2,526,940✔
3431
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
2,526,940✔
3432
  pStmt->dataType = dataType;
2,526,940✔
3433
  return createAlterTableStmtFinalize(pRealTable, pStmt);
2,526,940✔
3434
_err:
176✔
3435
  nodesDestroyNode(pRealTable);
176✔
3436
  return NULL;
176✔
3437
}
3438

3439
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
6,055,347✔
3440
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
3441
  SAlterTableStmt* pStmt = NULL;
6,055,347✔
3442
  CHECK_PARSER_STATUS(pCxt);
6,055,347✔
3443
  CHECK_NAME(checkColumnName(pCxt, pColName));
6,053,277✔
3444
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
6,053,101✔
3445
  CHECK_MAKE_NODE(pStmt);
6,053,101✔
3446
  pStmt->alterType = alterType;
6,053,101✔
3447
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
6,053,101✔
3448
  pStmt->dataType = dataType;
6,053,101✔
3449
  pStmt->pColOptions = (SColumnOptions*)pOptions;
6,053,101✔
3450

3451
  if (pOptions != NULL) {
6,053,101✔
3452
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
6,053,101✔
3453
    if (pOption->hasRef) {
6,053,101✔
3454
      if (!pOption->commentNull || pOption->bPrimaryKey || 0 != strcmp(pOption->compress, "") ||
33,108✔
3455
          0 != strcmp(pOption->encode, "") || 0 != strcmp(pOption->compressLevel, "")) {
33,108✔
3456
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
×
3457
      }
3458
      pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF;
33,108✔
3459
      tstrncpy(pStmt->refDbName, pOption->refDb, TSDB_DB_NAME_LEN);
33,108✔
3460
      tstrncpy(pStmt->refTableName, pOption->refTable, TSDB_TABLE_NAME_LEN);
33,108✔
3461
      tstrncpy(pStmt->refColName, pOption->refColumn, TSDB_COL_NAME_LEN);
33,108✔
3462
      CHECK_PARSER_STATUS(pCxt);
33,108✔
3463
    } else if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
6,019,993✔
3464
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
6,017,425✔
3465
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
260,788✔
3466
      } else {
3467
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
3468
        //                                         "not support alter column with option except compress");
3469
        // return NULL;
3470
      }
3471
    } else {
3472
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
2,568✔
3473
                                              "not support alter column with option except compress");
3474
      CHECK_PARSER_STATUS(pCxt);
2,568✔
3475
    }
3476
  }
3477
  return createAlterTableStmtFinalize(pRealTable, pStmt);
6,050,533✔
3478
_err:
4,814✔
3479
  nodesDestroyNode(pOptions);
4,814✔
3480
  nodesDestroyNode((SNode*)pStmt);
4,814✔
3481
  nodesDestroyNode(pRealTable);
4,814✔
3482
  return NULL;
4,814✔
3483
}
3484

3485
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
106,535✔
3486
                                           SToken* pColName, SNode* pOptions) {
3487
  CHECK_PARSER_STATUS(pCxt);
106,535✔
3488
  CHECK_NAME(checkColumnName(pCxt, pColName));
106,535✔
3489
  SAlterTableStmt* pStmt = NULL;
106,535✔
3490
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
106,535✔
3491
  CHECK_MAKE_NODE(pStmt);
106,535✔
3492
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
106,535✔
3493
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
106,535✔
3494
  pStmt->pColOptions = (SColumnOptions*)pOptions;
106,535✔
3495
  return createAlterTableStmtFinalize(pRealTable, pStmt);
106,535✔
3496
_err:
×
3497
  nodesDestroyNode(pOptions);
×
3498
  nodesDestroyNode(pRealTable);
×
3499
  return NULL;
×
3500
}
3501

3502
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
1,708,850✔
3503
  CHECK_PARSER_STATUS(pCxt);
1,708,850✔
3504
  CHECK_NAME(checkColumnName(pCxt, pColName));
1,708,850✔
3505
  SAlterTableStmt* pStmt = NULL;
1,708,498✔
3506
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
1,708,498✔
3507
  CHECK_MAKE_NODE(pStmt);
1,708,498✔
3508
  pStmt->alterType = alterType;
1,708,498✔
3509
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1,708,498✔
3510
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1,708,498✔
3511
_err:
352✔
3512
  nodesDestroyNode(pRealTable);
352✔
3513
  return NULL;
352✔
3514
}
3515

3516
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
195,684✔
3517
                                 SToken* pNewColName) {
3518
  CHECK_PARSER_STATUS(pCxt);
195,684✔
3519
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
195,684✔
3520
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
195,684✔
3521
  SAlterTableStmt* pStmt = NULL;
194,892✔
3522
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
194,892✔
3523
  CHECK_MAKE_NODE(pStmt);
194,892✔
3524
  pStmt->alterType = alterType;
194,892✔
3525
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
194,892✔
3526
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
194,892✔
3527
  return createAlterTableStmtFinalize(pRealTable, pStmt);
194,892✔
3528
_err:
792✔
3529
  nodesDestroyNode(pRealTable);
792✔
3530
  return NULL;
792✔
3531
}
3532

3533
SNode* createAlterTableAlterColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
106,770✔
3534
                                   SNode* pRef) {
3535
  CHECK_PARSER_STATUS(pCxt);
106,770✔
3536
  CHECK_NAME(checkColumnName(pCxt, pColName));
106,770✔
3537
  SAlterTableStmt* pStmt = NULL;
106,770✔
3538
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
106,770✔
3539
  CHECK_MAKE_NODE(pStmt);
106,770✔
3540
  pStmt->alterType = alterType;
106,770✔
3541
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
106,770✔
3542
  tstrncpy(pStmt->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
106,770✔
3543
  tstrncpy(pStmt->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
106,770✔
3544
  tstrncpy(pStmt->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
106,770✔
3545
  return createAlterTableStmtFinalize(pRealTable, pStmt);
106,770✔
3546
_err:
×
3547
  nodesDestroyNode(pRealTable);
×
3548
  return NULL;
×
3549
}
3550

3551
SNode* createAlterTableRemoveColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
62,468✔
3552
                                    const SToken* pLiteral) {
3553
  CHECK_PARSER_STATUS(pCxt);
62,468✔
3554
  CHECK_NAME(checkColumnName(pCxt, pColName));
62,468✔
3555
  SAlterTableStmt* pStmt = NULL;
62,468✔
3556
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
62,468✔
3557
  CHECK_MAKE_NODE(pStmt);
62,468✔
3558
  pStmt->alterType = alterType;
62,468✔
3559
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
62,468✔
3560
  return createAlterTableStmtFinalize(pRealTable, pStmt);
62,468✔
3561
_err:
×
3562
  nodesDestroyNode(pRealTable);
×
3563
  return NULL;
×
3564
}
3565

3566
SNode* createAlterSingleTagColumnNode(SAstCreateContext* pCtx, SToken* pTagName, SNode* pVal) {
8,031,750✔
3567
  CHECK_PARSER_STATUS(pCtx);
8,031,750✔
3568
  SAlterTableStmt* pStmt = NULL;
8,031,750✔
3569
  pCtx->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
8,031,750✔
3570
  CHECK_MAKE_NODE(pStmt);
8,031,750✔
3571
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
8,031,750✔
3572
  CHECK_NAME(checkColumnName(pCtx, pTagName));
8,031,750✔
3573
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
8,031,750✔
3574
  pStmt->pVal = (SValueNode*)pVal;
8,031,750✔
3575
  pStmt->pNodeListTagValue = NULL;
8,031,750✔
3576
  return (SNode*)pStmt;
8,031,750✔
3577
_err:
×
3578
  return NULL;
×
3579
}
3580

3581
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
×
3582
  CHECK_PARSER_STATUS(pCxt);
×
3583
  CHECK_NAME(checkColumnName(pCxt, pTagName));
×
3584
  SAlterTableStmt* pStmt = NULL;
×
3585
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
3586
  CHECK_MAKE_NODE(pStmt);
×
3587
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
×
3588
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
×
3589
  pStmt->pVal = (SValueNode*)pVal;
×
3590
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
3591
_err:
×
3592
  nodesDestroyNode(pVal);
×
3593
  nodesDestroyNode(pRealTable);
×
3594
  return NULL;
×
3595
}
3596

3597
SNode* createAlterTableSetMultiTagValue(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pList) {
7,997,989✔
3598
  CHECK_PARSER_STATUS(pCxt);
7,997,989✔
3599
  SAlterTableStmt* pStmt = NULL;
7,997,989✔
3600
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
7,997,989✔
3601

3602
  CHECK_MAKE_NODE(pStmt);
7,997,989✔
3603
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL;
7,997,989✔
3604
  pStmt->pNodeListTagValue = pList;
7,997,989✔
3605
  return createAlterTableStmtFinalize(pRealTable, pStmt);
7,997,989✔
3606
_err:
×
3607
  return NULL;
×
3608
}
3609

3610
SNode* setAlterSuperTableType(SNode* pStmt) {
484,387✔
3611
  if (!pStmt) return NULL;
484,387✔
3612
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
483,859✔
3613
  return pStmt;
483,859✔
3614
}
3615

3616
SNode* setAlterVirtualTableType(SNode* pStmt) {
363,416✔
3617
  if (!pStmt) return NULL;
363,416✔
3618
  setNodeType(pStmt, QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT);
363,416✔
3619
  return pStmt;
363,416✔
3620
}
3621

3622
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
2,002,461✔
3623
  CHECK_PARSER_STATUS(pCxt);
2,002,461✔
3624
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
2,002,898✔
3625
  SUseDatabaseStmt* pStmt = NULL;
2,002,911✔
3626
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
2,002,911✔
3627
  CHECK_MAKE_NODE(pStmt);
2,002,518✔
3628
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
2,002,518✔
3629
  return (SNode*)pStmt;
2,002,474✔
3630
_err:
×
3631
  return NULL;
×
3632
}
3633

3634
static bool needDbShowStmt(ENodeType type) {
1,536,370✔
3635
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
1,273,423✔
3636
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
853,109✔
3637
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
213,088✔
3638
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type ||
211,140✔
3639
         QUERY_NODE_SHOW_USAGE_STMT == type || QUERY_NODE_SHOW_VTABLES_STMT == type ||
2,809,793✔
3640
         QUERY_NODE_SHOW_STREAMS_STMT == type;
3641
}
3642

3643
SNode* createShowStmtWithLike(SAstCreateContext* pCxt, ENodeType type, SNode* pLikePattern) {
19,369✔
3644
  CHECK_PARSER_STATUS(pCxt);
19,369✔
3645
  SShowStmt* pStmt = NULL;
19,369✔
3646
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
19,369✔
3647
  CHECK_MAKE_NODE(pStmt);
19,369✔
3648
  pStmt->withFull = false;
19,369✔
3649
  pStmt->pTbName = pLikePattern;
19,369✔
3650
  if (pLikePattern) {
19,369✔
3651
    pStmt->tableCondType = OP_TYPE_LIKE;
2,940✔
3652
  }
3653
  return (SNode*)pStmt;
19,369✔
3654
_err:
×
3655
  nodesDestroyNode(pLikePattern);
×
3656
  return NULL;
×
3657
}
3658

3659
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
1,290,956✔
3660
  CHECK_PARSER_STATUS(pCxt);
1,290,956✔
3661
  SShowStmt* pStmt = NULL;
1,290,956✔
3662
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,290,956✔
3663
  CHECK_MAKE_NODE(pStmt);
1,290,956✔
3664
  pStmt->withFull = false;
1,290,956✔
3665
  return (SNode*)pStmt;
1,290,956✔
3666
_err:
×
3667
  return NULL;
×
3668
}
3669

3670
SNode* createShowXNodeResourcesStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType) {
1,152✔
3671
  CHECK_PARSER_STATUS(pCxt);
1,152✔
3672
  SShowStmt* pStmt = NULL;
1,152✔
3673
  switch (resourceType) {
1,152✔
3674
    case XNODE_TASK:
384✔
3675
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_TASKS_STMT, (SNode**)&pStmt);
384✔
3676
      CHECK_MAKE_NODE(pStmt);
384✔
3677
      break;
384✔
3678
    case XNODE_AGENT:
384✔
3679
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_AGENTS_STMT, (SNode**)&pStmt);
384✔
3680
      CHECK_MAKE_NODE(pStmt);
384✔
3681
      break;
384✔
3682
    case XNODE_JOB:
384✔
3683
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_JOBS_STMT, (SNode**)&pStmt);
384✔
3684
      CHECK_MAKE_NODE(pStmt);
384✔
3685
      break;
384✔
3686
    default:
×
3687
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3688
      goto _err;
×
3689
  }
3690
  return (SNode*)pStmt;
1,152✔
3691
_err:
×
3692
  return NULL;
×
3693
}
3694

3695
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
×
3696
  CHECK_PARSER_STATUS(pCxt);
×
3697
  SShowStmt* pStmt = NULL;
×
3698
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3699
  CHECK_MAKE_NODE(pStmt);
×
3700
  pStmt->withFull = true;
×
3701
  return (SNode*)pStmt;
×
3702
_err:
×
3703
  return NULL;
×
3704
}
3705

3706
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
324,112✔
3707
  CHECK_PARSER_STATUS(pCxt);
324,112✔
3708
  SShowCompactsStmt* pStmt = NULL;
324,112✔
3709
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
324,112✔
3710
  CHECK_MAKE_NODE(pStmt);
324,112✔
3711
  return (SNode*)pStmt;
324,112✔
3712
_err:
×
3713
  return NULL;
×
3714
}
3715

3716
SNode* createShowSsMigratesStmt(SAstCreateContext* pCxt, ENodeType type) {
×
3717
  CHECK_PARSER_STATUS(pCxt);
×
3718
  SShowSsMigratesStmt* pStmt = NULL;
×
3719
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3720
  CHECK_MAKE_NODE(pStmt);
×
3721
  return (SNode*)pStmt;
×
3722
_err:
×
3723
  return NULL;
×
3724
}
3725

3726
SNode* createShowTokensStmt(SAstCreateContext* pCxt, ENodeType type) {
×
3727
  CHECK_PARSER_STATUS(pCxt);
×
3728
  SShowTokensStmt* pStmt = NULL;
×
3729
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3730
  CHECK_MAKE_NODE(pStmt);
×
3731
  return (SNode*)pStmt;
×
3732
_err:
×
3733
  return NULL;
×
3734
}
3735

3736
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
682,049✔
3737
  if (pStmt == NULL) {
682,049✔
3738
    return NULL;
×
3739
  }
3740
  SShowStmt* pShow = (SShowStmt*)pStmt;
682,049✔
3741
  pShow->showKind = showKind;
682,049✔
3742
  return pStmt;
682,049✔
3743
}
3744

3745
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
1,435,412✔
3746
                              EOperatorType tableCondType) {
3747
  CHECK_PARSER_STATUS(pCxt);
1,435,412✔
3748
  if (needDbShowStmt(type) && NULL == pDbName) {
1,435,412✔
3749
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
13✔
3750
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
13✔
3751
    CHECK_PARSER_STATUS(pCxt);
13✔
3752
  }
3753
  SShowStmt* pStmt = NULL;
1,435,399✔
3754
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,435,399✔
3755
  CHECK_MAKE_NODE(pStmt);
1,435,399✔
3756
  pStmt->pDbName = pDbName;
1,435,399✔
3757
  pStmt->pTbName = pTbName;
1,435,399✔
3758
  pStmt->tableCondType = tableCondType;
1,435,399✔
3759
  return (SNode*)pStmt;
1,435,399✔
3760
_err:
13✔
3761
  nodesDestroyNode(pDbName);
13✔
3762
  nodesDestroyNode(pTbName);
13✔
3763
  return NULL;
13✔
3764
}
3765

3766
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
262,947✔
3767
                            EOperatorType tableCondType) {
3768
  CHECK_PARSER_STATUS(pCxt);
262,947✔
3769
  SNode* pDbName = NULL;
262,947✔
3770
  if (option.dbName.type == TK_NK_NIL) {
262,947✔
3771
    pDbName = createDefaultDatabaseCondValue(pCxt);
81,328✔
3772
  } else {
3773
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
181,619✔
3774
  }
3775

3776
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
262,947✔
3777
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3778
    return NULL;
×
3779
  }
3780

3781
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
262,947✔
3782
  CHECK_PARSER_STATUS(pCxt);
262,947✔
3783
  (void)setShowKind(pCxt, pStmt, option.kind);
262,947✔
3784
  return pStmt;
262,947✔
3785
_err:
×
3786
  nodesDestroyNode(pTbName);
×
3787
  return NULL;
×
3788
}
3789

3790
SNode* createShowVTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
56,819✔
3791
                             EOperatorType tableCondType) {
3792
  CHECK_PARSER_STATUS(pCxt);
56,819✔
3793
  SNode* pDbName = NULL;
56,819✔
3794
  if (option.dbName.type == TK_NK_NIL) {
56,819✔
3795
    pDbName = createDefaultDatabaseCondValue(pCxt);
3,600✔
3796
  } else {
3797
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
53,219✔
3798
  }
3799

3800
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
56,819✔
3801
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3802
    return NULL;
×
3803
  }
3804

3805
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_VTABLES_STMT, pDbName, pTbName, tableCondType);
56,819✔
3806
  CHECK_PARSER_STATUS(pCxt);
56,819✔
3807
  (void)setShowKind(pCxt, pStmt, option.kind);
56,819✔
3808
  return pStmt;
56,819✔
3809
_err:
×
3810
  nodesDestroyNode(pTbName);
×
3811
  return NULL;
×
3812
}
3813

3814
SNode* createShowSTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
205,730✔
3815
                             EOperatorType tableCondType) {
3816
  CHECK_PARSER_STATUS(pCxt);
205,730✔
3817
  SNode* pDbName = NULL;
205,730✔
3818
  if (option.dbName.type == TK_NK_NIL) {
205,730✔
3819
    pDbName = createDefaultDatabaseCondValue(pCxt);
55,771✔
3820
  } else {
3821
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
149,959✔
3822
  }
3823

3824
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_VIRTUAL &&
205,730✔
3825
      option.kind != SHOW_KIND_ALL) {
204,512✔
3826
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3827
    return NULL;
×
3828
  }
3829

3830
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_STABLES_STMT, pDbName, pTbName, tableCondType);
205,730✔
3831
  CHECK_PARSER_STATUS(pCxt);
205,730✔
3832
  (void)setShowKind(pCxt, pStmt, option.kind);
205,730✔
3833
  return pStmt;
205,730✔
3834
_err:
×
3835
  nodesDestroyNode(pTbName);
×
3836
  return NULL;
×
3837
}
3838

3839
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
60,843✔
3840
  CHECK_PARSER_STATUS(pCxt);
60,843✔
3841
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
60,843✔
3842
  SShowCreateDatabaseStmt* pStmt = NULL;
60,843✔
3843
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
60,843✔
3844
  CHECK_MAKE_NODE(pStmt);
60,843✔
3845
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
60,843✔
3846
  return (SNode*)pStmt;
60,843✔
3847
_err:
×
3848
  return NULL;
×
3849
}
3850

3851
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
4,582✔
3852
  CHECK_PARSER_STATUS(pCxt);
4,582✔
3853
  SToken  dbToken = {0};
4,582✔
3854
  SToken* pDbToken = NULL;
4,582✔
3855

3856
  if (pNode) {
4,582✔
3857
    SValueNode* pDbName = (SValueNode*)pNode;
1,471✔
3858
    if (pDbName->literal) {
1,471✔
3859
      dbToken.z = pDbName->literal;
1,471✔
3860
      dbToken.n = strlen(pDbName->literal);
1,471✔
3861
      pDbToken = &dbToken;
1,471✔
3862
    }
3863
  }
3864

3865
  if (pDbToken) {
4,582✔
3866
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
1,471✔
3867
  }
3868

3869
  SShowAliveStmt* pStmt = NULL;
4,582✔
3870
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
4,582✔
3871
  CHECK_PARSER_STATUS(pCxt);
4,582✔
3872

3873
  if (pDbToken) {
4,582✔
3874
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
1,471✔
3875
  }
3876
  if (pNode) {
4,582✔
3877
    nodesDestroyNode(pNode);
1,471✔
3878
  }
3879

3880
  return (SNode*)pStmt;
4,582✔
3881
_err:
×
3882
  nodesDestroyNode(pNode);
×
3883
  return NULL;
×
3884
}
3885

3886
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
72,092✔
3887
  CHECK_PARSER_STATUS(pCxt);
72,092✔
3888
  SShowCreateTableStmt* pStmt = NULL;
71,740✔
3889
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
71,740✔
3890
  CHECK_MAKE_NODE(pStmt);
71,740✔
3891
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
71,740✔
3892
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
71,740✔
3893
  nodesDestroyNode(pRealTable);
71,740✔
3894
  return (SNode*)pStmt;
71,740✔
3895
_err:
352✔
3896
  nodesDestroyNode(pRealTable);
352✔
3897
  return NULL;
352✔
3898
}
3899

3900
SNode* createShowCreateVTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
2,712✔
3901
  CHECK_PARSER_STATUS(pCxt);
2,712✔
3902
  SShowCreateTableStmt* pStmt = NULL;
2,712✔
3903
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,712✔
3904
  CHECK_MAKE_NODE(pStmt);
2,712✔
3905
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2,712✔
3906
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2,712✔
3907
  nodesDestroyNode(pRealTable);
2,712✔
3908
  return (SNode*)pStmt;
2,712✔
3909
_err:
×
3910
  nodesDestroyNode(pRealTable);
×
3911
  return NULL;
×
3912
}
3913

3914
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
×
3915
  CHECK_PARSER_STATUS(pCxt);
×
3916
  SShowCreateViewStmt* pStmt = NULL;
×
3917
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3918
  CHECK_MAKE_NODE(pStmt);
×
3919
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
3920
  tstrncpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
3921
  nodesDestroyNode(pRealTable);
×
3922
  return (SNode*)pStmt;
×
3923
_err:
×
3924
  nodesDestroyNode(pRealTable);
×
3925
  return NULL;
×
3926
}
3927

3928
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
2,967✔
3929
  CHECK_PARSER_STATUS(pCxt);
2,967✔
3930
  SShowTableDistributedStmt* pStmt = NULL;
2,967✔
3931
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
2,967✔
3932
  CHECK_MAKE_NODE(pStmt);
2,967✔
3933
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2,967✔
3934
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2,967✔
3935
  nodesDestroyNode(pRealTable);
2,967✔
3936
  return (SNode*)pStmt;
2,967✔
3937
_err:
×
3938
  nodesDestroyNode(pRealTable);
×
3939
  return NULL;
×
3940
}
3941

3942
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
38,266✔
3943
  CHECK_PARSER_STATUS(pCxt);
38,266✔
3944
  SShowDnodeVariablesStmt* pStmt = NULL;
38,266✔
3945
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
38,266✔
3946
  CHECK_MAKE_NODE(pStmt);
38,266✔
3947
  pStmt->pDnodeId = pDnodeId;
38,266✔
3948
  pStmt->pLikePattern = pLikePattern;
38,266✔
3949
  return (SNode*)pStmt;
38,266✔
3950
_err:
×
3951
  nodesDestroyNode(pDnodeId);
×
3952
  nodesDestroyNode(pLikePattern);
×
3953
  return NULL;
×
3954
}
3955

3956
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
3,951✔
3957
  CHECK_PARSER_STATUS(pCxt);
3,951✔
3958
  SShowVnodesStmt* pStmt = NULL;
3,951✔
3959
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
3,951✔
3960
  CHECK_MAKE_NODE(pStmt);
3,951✔
3961
  pStmt->pDnodeId = pDnodeId;
3,951✔
3962
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
3,951✔
3963
  return (SNode*)pStmt;
3,951✔
3964
_err:
×
3965
  nodesDestroyNode(pDnodeId);
×
3966
  nodesDestroyNode(pDnodeEndpoint);
×
3967
  return NULL;
×
3968
}
3969

3970
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
4,770✔
3971
  CHECK_PARSER_STATUS(pCxt);
4,770✔
3972
  if (NULL == pDbName) {
4,770✔
3973
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
3974
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3975
    CHECK_PARSER_STATUS(pCxt);
×
3976
  }
3977
  SShowTableTagsStmt* pStmt = NULL;
4,770✔
3978
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
4,770✔
3979
  CHECK_MAKE_NODE(pStmt);
4,770✔
3980
  pStmt->pDbName = pDbName;
4,770✔
3981
  pStmt->pTbName = pTbName;
4,770✔
3982
  pStmt->pTags = pTags;
4,770✔
3983
  return (SNode*)pStmt;
4,770✔
3984
_err:
×
3985
  nodesDestroyNode(pTbName);
×
3986
  nodesDestroyNode(pDbName);
×
3987
  nodesDestroyList(pTags);
×
3988
  return NULL;
×
3989
}
3990

3991
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
30,865✔
3992
  CHECK_PARSER_STATUS(pCxt);
30,865✔
3993
  SShowCompactDetailsStmt* pStmt = NULL;
30,865✔
3994
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
30,865✔
3995
  CHECK_MAKE_NODE(pStmt);
30,865✔
3996
  pStmt->pId = pCompactId;
30,865✔
3997
  return (SNode*)pStmt;
30,865✔
3998
_err:
×
3999
  nodesDestroyNode(pCompactId);
×
4000
  return NULL;
×
4001
}
4002

4003
SNode* createShowRetentionDetailsStmt(SAstCreateContext* pCxt, SNode* pId) {
2,193✔
4004
  CHECK_PARSER_STATUS(pCxt);
2,193✔
4005
  SShowRetentionDetailsStmt* pStmt = NULL;
2,193✔
4006
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_RETENTION_DETAILS_STMT, (SNode**)&pStmt);
2,193✔
4007
  CHECK_MAKE_NODE(pStmt);
2,193✔
4008
  pStmt->pId = pId;
2,193✔
4009
  return (SNode*)pStmt;
2,193✔
4010
_err:
×
4011
  nodesDestroyNode(pId);
×
4012
  return NULL;
×
4013
}
4014

4015
SNode* createShowTransactionDetailsStmt(SAstCreateContext* pCxt, SNode* pTransactionIdNode) {
275✔
4016
  CHECK_PARSER_STATUS(pCxt);
275✔
4017
  SShowTransactionDetailsStmt* pStmt = NULL;
275✔
4018
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT, (SNode**)&pStmt);
275✔
4019
  CHECK_MAKE_NODE(pStmt);
275✔
4020
  pStmt->pTransactionId = pTransactionIdNode;
275✔
4021
  return (SNode*)pStmt;
275✔
4022
_err:
×
4023
  nodesDestroyNode(pTransactionIdNode);
×
4024
  return NULL;
×
4025
}
4026

4027

4028

4029
static bool parseIp(const char* strIp, SIpRange* pIpRange) {
2,064✔
4030
  if (strchr(strIp, ':') == NULL) {
2,064✔
4031
    struct in_addr ip4;
2,064✔
4032
    if (inet_pton(AF_INET, strIp, &ip4) == 1) {
2,064✔
4033
      pIpRange->type = 0;
1,906✔
4034
      memcpy(&pIpRange->ipV4.ip, &ip4.s_addr, sizeof(ip4.s_addr));
1,906✔
4035
      return true;
1,906✔
4036
    }
4037
  } else {
4038
    struct in6_addr ip6;
×
4039
    if (inet_pton(AF_INET6, strIp, &ip6) == 1) {
×
4040
      pIpRange->type = 1;
×
4041
      memcpy(&pIpRange->ipV6.addr[0], ip6.s6_addr, 8);
×
4042
      memcpy(&pIpRange->ipV6.addr[1], ip6.s6_addr + 8, 8);
×
4043
      return true;
×
4044
    }
4045
  }
4046

4047
  return false;
158✔
4048
}
4049

4050

4051

4052
SIpRangeNode* parseIpRange(SAstCreateContext* pCxt, const SToken* token) {
2,064✔
4053
  CHECK_PARSER_STATUS(pCxt);
2,064✔
4054

4055
#ifdef TD_ASTRA
4056
  return NULL;
4057
#else
4058

4059
  SIpRangeNode* node = NULL;
2,064✔
4060
  int32_t code = nodesMakeNode(QUERY_NODE_IP_RANGE, (SNode**)&node);
2,064✔
4061
  if (node == NULL) {
2,064✔
4062
    goto _err;
×
4063
  }
4064

4065
  char buf[64];
2,064✔
4066
  if (token->n >= sizeof(buf)) {
2,064✔
4067
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
4068
    goto _err;
×
4069
  }
4070
  memcpy(buf, token->z, token->n);
2,064✔
4071
  buf[token->n] = '\0';
2,064✔
4072
  (void)strdequote(buf);
2,064✔
4073

4074
  char* slash = strchr(buf, '/');
2,064✔
4075
  if (slash) {
2,064✔
4076
    *slash = '\0';
948✔
4077
  }
4078

4079
  if (!parseIp(buf, &node->range)) {
2,064✔
4080
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
158✔
4081
    goto _err;
158✔
4082
  }
4083

4084
  int32_t mask = 0;
1,906✔
4085
  if (!slash) {
1,906✔
4086
    mask = node->range.type == 0 ? 32 : 128;
1,116✔
4087
  } else if (taosStr2int32(slash + 1, &mask) != TSDB_CODE_SUCCESS) {
790✔
4088
    code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
4089
    goto _err;
×
4090
  }
4091

4092
  code = tIpRangeSetMask(&node->range, mask);
1,906✔
4093
  if (code != TSDB_CODE_SUCCESS) {
1,906✔
4094
    goto _err;
158✔
4095
  }
4096

4097
  return node;
1,748✔
4098

4099
_err:
316✔
4100
  pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
316✔
4101
  nodesDestroyNode((SNode*)node);
316✔
4102
  return NULL;
316✔
4103

4104
#endif
4105
}
4106

4107

4108

4109
SDateTimeRangeNode* parseDateTimeRange(SAstCreateContext* pCxt, const SToken* token) {
×
4110
  CHECK_PARSER_STATUS(pCxt);
×
4111

4112
  SDateTimeRangeNode* node = NULL;
×
4113
  int32_t code = nodesMakeNode(QUERY_NODE_DATE_TIME_RANGE, (SNode**)&node);
×
4114
  if (code != TSDB_CODE_SUCCESS) {
×
4115
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code);
×
4116
    goto _err;
×
4117
  }
4118

4119
  char buf[128];
×
4120
  if (token->n >= sizeof(buf)) {
×
4121
    code = TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG;
×
4122
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Date time range string is too long");
×
4123
    goto _err;
×
4124
  }
4125
  memcpy(buf, token->z, token->n);
×
4126
  buf[token->n] = '\0';
×
4127
  (void)strdequote(buf);
×
4128

4129
  code = TSDB_CODE_PAR_INVALID_OPTION_VALUE;
×
4130
  int32_t year = 0, month = 0, day = 0, hour = 0, minute = 0, duration = 0;
×
4131
  if (buf[0] >= '1' && buf[0] <= '9') {
×
4132
    // format: YYYY-MM-DD HH:MM duration
4133
    int ret = sscanf(buf, "%d-%d-%d %d:%d %d", &year, &month, &day, &hour, &minute, &duration);
×
4134
    if (ret != 6) {
×
4135
      goto _err;
×
4136
    }
4137
    if (month < 1 || month > 12) {
×
4138
      goto _err;
×
4139
    }
4140
  } else {
4141
    // format: WEEKDAY HH:MM duration
4142
    char weekday[4];
×
4143
    int ret = sscanf(buf, "%3s %d:%d %d", weekday, &hour, &minute, &duration);
×
4144
    if (ret != 4) {
×
4145
      goto _err;
×
4146
    }
4147
    day = taosParseShortWeekday(weekday);
×
4148
    if (day < 0 || day > 6) {
×
4149
      goto _err;
×
4150
    }
4151
    month = -1;
×
4152
  }
4153

4154
  node->range.year = (int16_t)year;
×
4155
  node->range.month = (int8_t)month;
×
4156
  node->range.day = (int8_t)day;
×
4157
  node->range.hour = (int8_t)hour;
×
4158
  node->range.minute = (int8_t)minute;
×
4159
  node->range.duration = duration;
×
4160
  if (!isValidDateTimeRange(&node->range)) {
×
4161
    goto _err;
×
4162
  }
4163

4164
  return node;
×
4165

4166
_err:
×
4167
  if (code == TSDB_CODE_PAR_INVALID_OPTION_VALUE) { // other error types have been set above
×
4168
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid date time range");
×
4169
  }
4170
  nodesDestroyNode((SNode*)node);
×
4171
  return NULL;
×
4172
}
4173

4174

4175
SUserOptions* createDefaultUserOptions(SAstCreateContext* pCxt) {
93,373✔
4176
  SUserOptions* pOptions = NULL;
93,373✔
4177
  int32_t code = nodesMakeNode(QUERY_NODE_USER_OPTIONS, (SNode**)&pOptions);
93,373✔
4178
  if (pOptions == NULL) {
93,373✔
4179
    pCxt->errCode = code;
×
4180
    return NULL;
×
4181
  }
4182

4183
  pOptions->enable = 1;
93,373✔
4184
  pOptions->sysinfo = 1;
93,373✔
4185
  pOptions->createdb = 0;
93,373✔
4186
  pOptions->isImport = 0;
93,373✔
4187
  pOptions->changepass = 2;
93,373✔
4188
  pOptions->sessionPerUser = TSDB_USER_SESSION_PER_USER_DEFAULT;
93,373✔
4189
  pOptions->connectTime = TSDB_USER_CONNECT_TIME_DEFAULT;
93,373✔
4190
  pOptions->connectIdleTime = TSDB_USER_CONNECT_IDLE_TIME_DEFAULT;
93,373✔
4191
  pOptions->callPerSession = TSDB_USER_CALL_PER_SESSION_DEFAULT;
93,373✔
4192
  pOptions->vnodePerCall = TSDB_USER_VNODE_PER_CALL_DEFAULT;  // not implemented yet, so use -1 instead of
93,373✔
4193
                                                                // TSDB_USER_VNODE_PER_CALL_DEFAULT;
4194
  pOptions->failedLoginAttempts = TSDB_USER_FAILED_LOGIN_ATTEMPTS_DEFAULT;
93,373✔
4195
  pOptions->passwordLifeTime = TSDB_USER_PASSWORD_LIFE_TIME_DEFAULT;
93,373✔
4196
  pOptions->passwordReuseTime = TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
93,373✔
4197
  pOptions->passwordReuseMax = TSDB_USER_PASSWORD_REUSE_MAX_DEFAULT;
93,373✔
4198
  pOptions->passwordLockTime = TSDB_USER_PASSWORD_LOCK_TIME_DEFAULT;
93,373✔
4199
  pOptions->passwordGraceTime = TSDB_USER_PASSWORD_GRACE_TIME_DEFAULT;
93,373✔
4200
  pOptions->inactiveAccountTime = TSDB_USER_INACTIVE_ACCOUNT_TIME_DEFAULT;
93,373✔
4201
  pOptions->allowTokenNum = TSDB_USER_ALLOW_TOKEN_NUM_DEFAULT;
93,373✔
4202

4203
  return pOptions;
93,373✔
4204
}
4205

4206

4207

4208
SUserOptions* mergeUserOptions(SAstCreateContext* pCxt, SUserOptions* a, SUserOptions* b) {
104,447✔
4209
  if (a == NULL && b == NULL) {
104,447✔
4210
      return createDefaultUserOptions(pCxt);
93,373✔
4211
  }
4212
  if (b == NULL) {
11,074✔
4213
    return a;
6,375✔
4214
  }
4215
  if (a == NULL) {
4,699✔
4216
    return b;
×
4217
  }
4218

4219
  if (b->hasPassword) {
4,699✔
4220
    if (a->hasPassword) {
×
4221
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASS");
×
4222
    } else {
4223
      a->hasPassword = true;
×
4224
      tstrncpy(a->password, b->password, sizeof(a->password));
×
4225
    }
4226
  }
4227

4228
  if (b->hasTotpseed) {
4,699✔
4229
    if (a->hasTotpseed) {
×
4230
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TOTPSEED");
×
4231
    } else {
4232
      a->hasTotpseed = true;
×
4233
      tstrncpy(a->totpseed, b->totpseed, sizeof(a->totpseed));
×
4234
    }
4235
  }
4236

4237
  if (b->hasEnable) {
4,699✔
4238
    if (a->hasEnable) {
×
4239
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE/ACCOUNT LOCK/ACCOUNT UNLOCK");
×
4240
    } else {
4241
      a->hasEnable = true;
×
4242
      a->enable = b->enable;
×
4243
    }
4244
  }
4245

4246
  if (b->hasSysinfo) {
4,699✔
4247
    if (a->hasSysinfo) {
×
4248
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SYSINFO");
×
4249
    } else {
4250
      a->hasSysinfo = true;
×
4251
      a->sysinfo = b->sysinfo;
×
4252
    }
4253
  }
4254

4255
  if (b->hasCreatedb) {
4,699✔
4256
    if (a->hasCreatedb) {
1,366✔
4257
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CREATEDB");
×
4258
    } else {
4259
      a->hasCreatedb = true;
1,366✔
4260
      a->createdb = b->createdb;
1,366✔
4261
    }
4262
  }
4263

4264
  if (b->hasChangepass) {
4,699✔
4265
    if (a->hasChangepass) {
×
4266
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CHANGEPASS");
×
4267
    } else {
4268
      a->hasChangepass = true;
×
4269
      a->changepass = b->changepass;
×
4270
    }
4271
  }
4272

4273
  if (b->hasSessionPerUser) {
4,699✔
4274
    if (a->hasSessionPerUser) {
×
4275
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "SESSION_PER_USER");
×
4276
    } else {
4277
      a->hasSessionPerUser = true;
×
4278
      a->sessionPerUser = b->sessionPerUser;
×
4279
    }
4280
  }
4281

4282
  if (b->hasConnectTime) {
4,699✔
4283
    if (a->hasConnectTime) {
×
4284
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_TIME");
×
4285
    } else {
4286
      a->hasConnectTime = true;
×
4287
      a->connectTime = b->connectTime;
×
4288
    }
4289
  }
4290

4291
  if (b->hasConnectIdleTime) {
4,699✔
4292
    if (a->hasConnectIdleTime) {
×
4293
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CONNECT_IDLE_TIME");
×
4294
    } else {
4295
      a->hasConnectIdleTime = true;
×
4296
      a->connectIdleTime = b->connectIdleTime;
×
4297
    }
4298
  }
4299

4300
  if (b->hasCallPerSession) {
4,699✔
4301
    if (a->hasCallPerSession) {
×
4302
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "CALLS_PER_SESSION");
×
4303
    } else {
4304
      a->hasCallPerSession = true;
×
4305
      a->callPerSession = b->callPerSession;
×
4306
    }
4307
  }
4308

4309
  if (b->hasVnodePerCall) {
4,699✔
4310
    if (a->hasVnodePerCall) {
×
4311
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "VNODES_PER_CALL");
×
4312
    } else {
4313
      a->hasVnodePerCall = true;
×
4314
      a->vnodePerCall = b->vnodePerCall;
×
4315
    }
4316
  }
4317

4318
  if (b->hasFailedLoginAttempts) {
4,699✔
4319
    if (a->hasFailedLoginAttempts) {
×
4320
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "FAILED_LOGIN_ATTEMPTS");
×
4321
    } else {
4322
      a->hasFailedLoginAttempts = true;
×
4323
      a->failedLoginAttempts = b->failedLoginAttempts;
×
4324
    }
4325
  }
4326

4327
  if (b->hasPasswordLifeTime) {
4,699✔
4328
    if (a->hasPasswordLifeTime) {
×
4329
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LIFE_TIME");
×
4330
    } else {
4331
      a->hasPasswordLifeTime = true;
×
4332
      a->passwordLifeTime = b->passwordLifeTime;
×
4333
    }
4334
  }
4335

4336
  if (b->hasPasswordReuseTime) {
4,699✔
4337
    if (a->hasPasswordReuseTime) {
1,060✔
4338
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_TIME");
×
4339
    } else {
4340
      a->hasPasswordReuseTime = true;
1,060✔
4341
      a->passwordReuseTime = b->passwordReuseTime;
1,060✔
4342
    }
4343
  }
4344

4345
  if (b->hasPasswordReuseMax) {
4,699✔
4346
    if (a->hasPasswordReuseMax) {
513✔
4347
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_REUSE_MAX");
×
4348
    } else {
4349
      a->hasPasswordReuseMax = true;
513✔
4350
      a->passwordReuseMax = b->passwordReuseMax;
513✔
4351
    }
4352
  }
4353

4354
  if (b->hasPasswordLockTime) {
4,699✔
4355
    if (a->hasPasswordLockTime) {
×
4356
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_LOCK_TIME");
×
4357
    } else {
4358
      a->hasPasswordLockTime = true;
×
4359
      a->passwordLockTime = b->passwordLockTime;
×
4360
    }
4361
  }
4362

4363
  if (b->hasPasswordGraceTime) {
4,699✔
4364
    if (a->hasPasswordGraceTime) {
×
4365
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PASSWORD_GRACE_TIME");
×
4366
    } else {
4367
      a->hasPasswordGraceTime = true;
×
4368
      a->passwordGraceTime = b->passwordGraceTime;
×
4369
    }
4370
  }
4371

4372
  if (b->hasInactiveAccountTime) {
4,699✔
4373
    if (a->hasInactiveAccountTime) {
×
4374
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "INACTIVE_ACCOUNT_TIME");
×
4375
    } else {
4376
      a->hasInactiveAccountTime = true;
×
4377
      a->inactiveAccountTime = b->inactiveAccountTime;
×
4378
    }
4379
  }
4380

4381
  if (b->hasAllowTokenNum) {
4,699✔
4382
    if (a->hasAllowTokenNum) {
×
4383
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ALLOW_TOKEN_NUM");
×
4384
    } else {
4385
      a->hasAllowTokenNum = true;
×
4386
      a->allowTokenNum = b->allowTokenNum;
×
4387
    }
4388
  }
4389

4390
  if (b->pIpRanges != NULL) {
4,699✔
4391
    if (a->pIpRanges == NULL) {
800✔
4392
      a->pIpRanges = b->pIpRanges;
800✔
4393
    } else {
4394
      int32_t code = nodesListAppendList(a->pIpRanges, b->pIpRanges);
×
4395
      if (code != TSDB_CODE_SUCCESS) {
×
4396
        pCxt->errCode = code;
×
4397
      }
4398
    }
4399
    b->pIpRanges = NULL;
800✔
4400
  }
4401

4402
  if (b->pDropIpRanges != NULL) {
4,699✔
4403
    if (a->pDropIpRanges == NULL) {
×
4404
      a->pDropIpRanges = b->pDropIpRanges;
×
4405
    } else {
4406
      int32_t code = nodesListAppendList(a->pDropIpRanges, b->pDropIpRanges);
×
4407
      if (code != TSDB_CODE_SUCCESS) {
×
4408
        pCxt->errCode = code;
×
4409
      }
4410
    }
4411
    b->pDropIpRanges = NULL;
×
4412
  }
4413

4414
  if (b->pTimeRanges != NULL) {
4,699✔
4415
    if (a->pTimeRanges == NULL) {
×
4416
      a->pTimeRanges = b->pTimeRanges;
×
4417
    } else {
4418
      int32_t code = nodesListAppendList(a->pTimeRanges, b->pTimeRanges);
×
4419
      if (code != TSDB_CODE_SUCCESS) {
×
4420
        pCxt->errCode = code;
×
4421
      }
4422
    }
4423
    b->pTimeRanges = NULL;
×
4424
  }
4425

4426
  if (b->pDropTimeRanges != NULL) {
4,699✔
4427
    if (a->pDropTimeRanges == NULL) {
×
4428
      a->pDropTimeRanges = b->pDropTimeRanges;
×
4429
    } else {
4430
      int32_t code = nodesListAppendList(a->pDropTimeRanges, b->pDropTimeRanges);
×
4431
      if (code != TSDB_CODE_SUCCESS) {
×
4432
        pCxt->errCode = code;
×
4433
      }
4434
    }
4435
    b->pDropTimeRanges = NULL;
×
4436
  }
4437

4438
  nodesDestroyNode((SNode*)b);
4,699✔
4439
  return a;
4,699✔
4440
}
4441

4442

4443

4444
void setUserOptionsTotpseed(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pTotpseed) {
×
4445
  pUserOptions->hasTotpseed = true;
×
4446

4447
  if (pTotpseed == NULL) { // clear TOTP secret
×
4448
    memset(pUserOptions->totpseed, 0, sizeof(pUserOptions->totpseed));
×
4449
    return;
×
4450
  }
4451

4452
  if (pTotpseed->n >= sizeof(pUserOptions->totpseed) * 2) {
×
4453
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
4454
    return;
×
4455
  }
4456

4457
  char buf[sizeof(pUserOptions->totpseed) * 2 + 1];
×
4458
  memcpy(buf, pTotpseed->z, pTotpseed->n);
×
4459
  buf[pTotpseed->n] = 0;
×
4460
  (void)strdequote(buf);
×
4461
  size_t len = strtrim(buf);
×
4462

4463
  if (len >= sizeof(pUserOptions->totpseed)) {
×
4464
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOTPSEED", sizeof(pUserOptions->totpseed));
×
4465
  } else if (len < TSDB_USER_TOTPSEED_MIN_LEN) {
×
4466
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT, "TOTPSEED", TSDB_USER_TOTPSEED_MIN_LEN);
×
4467
  } else {
4468
    tstrncpy(pUserOptions->totpseed, buf, sizeof(pUserOptions->totpseed));
×
4469
  }
4470
}
4471

4472

4473

4474
void setUserOptionsPassword(SAstCreateContext* pCxt, SUserOptions* pUserOptions, const SToken* pPassword) {
78,597✔
4475
  pUserOptions->hasPassword = true;
78,597✔
4476

4477
  if (pPassword->n >= sizeof(pUserOptions->password) * 2) {
78,597✔
4478
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
4479
    return;
×
4480
  }
4481

4482
  char buf[sizeof(pUserOptions->password) * 2 + 1];
77,468✔
4483
  memcpy(buf, pPassword->z, pPassword->n);
78,597✔
4484
  buf[pPassword->n] = 0;
78,597✔
4485
  (void)strdequote(buf);
78,597✔
4486
  size_t len = strtrim(buf);
78,597✔
4487

4488
  if (len >= sizeof(pUserOptions->password)) {
78,597✔
4489
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
603✔
4490
  } else if (len < TSDB_PASSWORD_MIN_LEN) {
77,994✔
4491
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
4,360✔
4492
  } else {
4493
    tstrncpy(pUserOptions->password, buf, sizeof(pUserOptions->password));
73,634✔
4494
  }
4495
}
4496

4497

4498

4499
static bool isValidUserOptions(SAstCreateContext* pCxt, const SUserOptions* opts) {
83,112✔
4500
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
83,112✔
4501
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
489✔
4502
    return false;
489✔
4503
  }
4504

4505
  if (opts->hasSysinfo && (opts->sysinfo < 0 || opts->sysinfo > 1)) {
82,623✔
4506
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SYSINFO");
815✔
4507
    return false;
815✔
4508
  }
4509

4510
  if (opts->hasIsImport && (opts->isImport < 0 || opts->isImport > 1)) {
81,808✔
4511
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "IS_IMPORT");
×
4512
    return false;
×
4513
  }
4514

4515
  if (opts->hasCreatedb && (opts->createdb < 0 || opts->createdb > 1)) {
81,808✔
4516
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CREATEDB");
489✔
4517
    return false;
489✔
4518
  }
4519

4520
  if (opts->hasTotpseed && opts->totpseed[0] != 0 && !taosIsComplexString(opts->totpseed)) {
81,319✔
4521
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TOTPSEED");
×
4522
    return false;
×
4523
  }
4524

4525
  if (opts->hasPassword && !isValidPassword(pCxt, opts->password, opts->hasIsImport && opts->isImport)) {
81,319✔
4526
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
19,360✔
4527
    return false;
19,360✔
4528
  }
4529

4530
  if (opts->hasChangepass && (opts->changepass < 0 || opts->changepass > 2)) {
61,959✔
4531
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CHANGEPASS");
×
4532
    return false;
×
4533
  }
4534

4535
  if (opts->hasSessionPerUser && (opts->sessionPerUser < -1 || opts->sessionPerUser == 0)) {
61,959✔
4536
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "SESSION_PER_USER");
×
4537
    return false;
×
4538
  }
4539

4540
  if (opts->hasConnectTime && (opts->connectTime < -1 || opts->connectTime == 0)) {
61,959✔
4541
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_TIME");
×
4542
    return false;
×
4543
  }
4544

4545
  if (opts->hasConnectIdleTime && (opts->connectIdleTime < -1 || opts->connectIdleTime == 0)) {
61,959✔
4546
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CONNECT_IDLE_TIME");
×
4547
    return false;
×
4548
  }
4549

4550
  if (opts->hasCallPerSession && (opts->callPerSession < -1 || opts->callPerSession == 0)) {
61,959✔
4551
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "CALLS_PER_SESSION");
×
4552
    return false;
×
4553
  }
4554

4555
  if (opts->hasVnodePerCall && (opts->vnodePerCall < -1 || opts->vnodePerCall == 0)) {
61,959✔
4556
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "VNODES_PER_CALL");
×
4557
    return false;
×
4558
  }
4559

4560
  if (opts->hasFailedLoginAttempts && (opts->failedLoginAttempts < -1 || opts->failedLoginAttempts == 0)) {
61,959✔
4561
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "FAILED_LOGIN_ATTEMPTS");
×
4562
    return false;
×
4563
  }
4564

4565
  if (opts->hasPasswordLockTime && (opts->passwordLockTime < -1 || opts->passwordLockTime == 0)) {
61,959✔
4566
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LOCK_TIME");
×
4567
    return false;
×
4568
  }
4569

4570
  if (opts->hasPasswordLifeTime && (opts->passwordLifeTime < -1 || opts->passwordLifeTime == 0)) {
61,959✔
4571
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_LIFE_TIME");
×
4572
    return false;
×
4573
  }
4574

4575
  if (opts->hasPasswordGraceTime && (opts->passwordGraceTime < -1)) {
61,959✔
4576
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_GRACE_TIME");
×
4577
    return false;
×
4578
  }
4579

4580
  if (opts->hasPasswordReuseTime && (opts->passwordReuseTime < 0 || opts->passwordReuseTime > TSDB_USER_PASSWORD_REUSE_TIME_MAX)) {
61,959✔
4581
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_TIME");
×
4582
    return false;
×
4583
  }
4584

4585
  if (opts->hasPasswordReuseMax && (opts->passwordReuseMax < 0 || opts->passwordReuseMax > TSDB_USER_PASSWORD_REUSE_MAX_MAX)) {
61,959✔
4586
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "PASSWORD_REUSE_MAX");
×
4587
    return false;
×
4588
  }
4589

4590
  if (opts->hasInactiveAccountTime && (opts->inactiveAccountTime < -1 || opts->inactiveAccountTime == 0)) {
61,959✔
4591
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "INACTIVE_ACCOUNT_TIME");
×
4592
    return false;
×
4593
  }
4594

4595
  if (opts->hasAllowTokenNum && (opts->allowTokenNum < -1 || opts->allowTokenNum == 0)) {
61,959✔
4596
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ALLOW_TOKEN_NUM");
×
4597
    return false;
×
4598
  }
4599

4600
  // ip ranges and date time ranges has been validated during parsing
4601

4602
  return true;
61,959✔
4603
}
4604

4605

4606

4607
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* opts, bool ignoreExists) {
63,120✔
4608
  SCreateUserStmt* pStmt = NULL;
63,120✔
4609

4610
  CHECK_PARSER_STATUS(pCxt);
63,120✔
4611
  CHECK_NAME(checkUserName(pCxt, pUserName));
59,281✔
4612

4613
  if (!isValidUserOptions(pCxt, opts)) {
59,158✔
4614
    goto _err;
10,409✔
4615
  }
4616

4617
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
48,749✔
4618
  CHECK_MAKE_NODE(pStmt);
48,749✔
4619
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
48,749✔
4620
  tstrncpy(pStmt->password, opts->password, sizeof(pStmt->password));
48,749✔
4621
  tstrncpy(pStmt->totpseed, opts->totpseed, sizeof(pStmt->totpseed));
48,749✔
4622

4623
  pStmt->ignoreExists = ignoreExists;
48,749✔
4624
  pStmt->sysinfo = opts->sysinfo;
48,749✔
4625
  pStmt->createDb = opts->createdb;
48,749✔
4626
  pStmt->isImport = opts->isImport;
48,749✔
4627
  pStmt->changepass = opts->changepass;
48,749✔
4628
  pStmt->enable = opts->enable;
48,749✔
4629

4630
  pStmt->sessionPerUser = opts->sessionPerUser;
48,749✔
4631
  pStmt->connectTime = opts->connectTime;
48,749✔
4632
  pStmt->connectIdleTime = opts->connectIdleTime;
48,749✔
4633
  pStmt->callPerSession = opts->callPerSession;
48,749✔
4634
  pStmt->vnodePerCall = opts->vnodePerCall;
48,749✔
4635
  pStmt->failedLoginAttempts = opts->failedLoginAttempts;
48,749✔
4636
  pStmt->passwordLifeTime = opts->passwordLifeTime;
48,749✔
4637
  pStmt->passwordReuseTime = opts->passwordReuseTime;
48,749✔
4638
  pStmt->passwordReuseMax = opts->passwordReuseMax;
48,749✔
4639
  pStmt->passwordLockTime = opts->passwordLockTime;
48,749✔
4640
  pStmt->passwordGraceTime = opts->passwordGraceTime;
48,749✔
4641
  pStmt->inactiveAccountTime = opts->inactiveAccountTime;
48,749✔
4642
  pStmt->allowTokenNum = opts->allowTokenNum;
48,749✔
4643

4644
  pStmt->numIpRanges = LIST_LENGTH(opts->pIpRanges);
48,749✔
4645
  pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpRange));
48,749✔
4646
  CHECK_OUT_OF_MEM(pStmt->pIpRanges);
48,749✔
4647
  int i = 0;
48,749✔
4648
  SNode* pNode = NULL;
48,749✔
4649
  FOREACH(pNode, opts->pIpRanges) {
49,541✔
4650
    SIpRangeNode* node = (SIpRangeNode*)(pNode);
792✔
4651
    pStmt->pIpRanges[i++] = node->range;
792✔
4652
  }
4653

4654
  pStmt->numTimeRanges = LIST_LENGTH(opts->pTimeRanges);
48,749✔
4655
  pStmt->pTimeRanges = taosMemoryMalloc(pStmt->numTimeRanges * sizeof(SDateTimeRange));
48,749✔
4656
  CHECK_OUT_OF_MEM(pStmt->pTimeRanges);
48,749✔
4657
  i = 0;
48,749✔
4658
  pNode = NULL;
48,749✔
4659
  FOREACH(pNode, opts->pTimeRanges) {
48,749✔
4660
    SDateTimeRangeNode* node = (SDateTimeRangeNode*)(pNode);
×
4661
    pStmt->pTimeRanges[i++] = node->range;
×
4662
  }
4663

4664
  nodesDestroyNode((SNode*)opts);
48,749✔
4665
  return (SNode*)pStmt;
48,749✔
4666

4667
_err:
14,371✔
4668
  nodesDestroyNode((SNode*)pStmt);
14,371✔
4669
  nodesDestroyNode((SNode*)opts);
14,371✔
4670
  return NULL;
14,371✔
4671
}
4672

4673

4674

4675
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, SUserOptions* pUserOptions) {
25,394✔
4676
  SAlterUserStmt* pStmt = NULL;
25,394✔
4677
  CHECK_PARSER_STATUS(pCxt);
25,394✔
4678
  CHECK_NAME(checkUserName(pCxt, pUserName));
23,954✔
4679
  if (!isValidUserOptions(pCxt, pUserOptions)) {
23,954✔
4680
    goto _err;
10,744✔
4681
  }
4682

4683
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
13,210✔
4684
  CHECK_MAKE_NODE(pStmt);
13,210✔
4685
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
13,210✔
4686
  pStmt->pUserOptions = pUserOptions;
13,210✔
4687
  return (SNode*)pStmt;
13,210✔
4688

4689
_err:
12,184✔
4690
  nodesDestroyNode((SNode*)pStmt);
12,184✔
4691
  nodesDestroyNode((SNode*)pUserOptions);
12,184✔
4692
  return NULL;
12,184✔
4693
}
4694

4695

4696

4697
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName, bool ignoreNotExists) {
21,322✔
4698
  CHECK_PARSER_STATUS(pCxt);
21,322✔
4699
  CHECK_NAME(checkUserName(pCxt, pUserName));
21,322✔
4700
  SDropUserStmt* pStmt = NULL;
21,322✔
4701
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
21,322✔
4702
  CHECK_MAKE_NODE(pStmt);
21,322✔
4703
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
21,322✔
4704
  pStmt->ignoreNotExists = ignoreNotExists;
21,322✔
4705
  return (SNode*)pStmt;
21,322✔
4706
_err:
×
4707
  return NULL;
×
4708
}
4709

4710
static bool checkRoleName(SAstCreateContext* pCxt, SToken* pName, bool checkSysName) {
563,921✔
4711
  if (NULL == pName) {
563,921✔
4712
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4713
  } else {
4714
    if (pName->n >= TSDB_ROLE_LEN) {
563,921✔
4715
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
4716
    }
4717
  }
4718
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
563,921✔
4719
    trimEscape(pCxt, pName, true);
563,921✔
4720
  }
4721
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
563,921✔
4722
    if (checkSysName && taosStrncasecmp(pName->z, "sys", 3) == 0) {  // system reserved role name prefix
563,921✔
4723
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4724
                                              "Cannot create/drop/alter roles with reserved prefix 'sys'");
4725
    }
4726
  }
4727
  return TSDB_CODE_SUCCESS == pCxt->errCode;
563,921✔
4728
}
4729

4730

4731
STokenOptions* createDefaultTokenOptions(SAstCreateContext* pCxt) {
45✔
4732
  STokenOptions* pOptions = NULL;
45✔
4733
  int32_t code = nodesMakeNode(QUERY_NODE_TOKEN_OPTIONS, (SNode**)&pOptions);
45✔
4734
  if (pOptions == NULL) {
45✔
4735
    pCxt->errCode = code;
×
4736
    return NULL;
×
4737
  }
4738

4739
  pOptions->enable = 1;
45✔
4740
  pOptions->ttl = 0;
45✔
4741
  return pOptions;
45✔
4742
}
4743

4744

4745

4746
STokenOptions* mergeTokenOptions(SAstCreateContext* pCxt, STokenOptions* a, STokenOptions* b) {
×
4747
  if (a == NULL && b == NULL) {
×
4748
      return createDefaultTokenOptions(pCxt);
×
4749
  }
4750
  if (b == NULL) {
×
4751
    return a;
×
4752
  }
4753
  if (a == NULL) {
×
4754
    return b;
×
4755
  }
4756

4757
  if (b->hasEnable) {
×
4758
    if (a->hasEnable) {
×
4759
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "ENABLE");
×
4760
    } else {
4761
      a->hasEnable = true;
×
4762
      a->enable = b->enable;
×
4763
    }
4764
  }
4765

4766
  if (b->hasTtl) {
×
4767
    if (a->hasTtl) {
×
4768
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "TTL");
×
4769
    } else {
4770
      a->hasTtl = true;
×
4771
      a->ttl = b->ttl;
×
4772
    }
4773
  }
4774

4775
  if (b->hasProvider) {
×
4776
    if (a->hasProvider) {
×
4777
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "PROVIDER");
×
4778
    } else {
4779
      a->hasProvider = true;
×
4780
      tstrncpy(a->provider, b->provider, sizeof(a->provider));
×
4781
    }
4782
  }
4783

4784
  if (b->hasExtraInfo) {
×
4785
    if (a->hasExtraInfo) {
×
4786
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_DUPLICATED, "EXTRA_INFO");
×
4787
    } else {
4788
      a->hasExtraInfo = true;
×
4789
      tstrncpy(a->extraInfo, b->extraInfo, sizeof(a->extraInfo));
×
4790
    }
4791
  }
4792
  nodesDestroyNode((SNode*)b);
×
4793
  return a;
×
4794
}
4795

4796

4797

4798
void setTokenOptionsProvider(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pProvider) {
×
4799
  pTokenOptions->hasProvider = true;
×
4800

4801
  if (pProvider->n >= sizeof(pTokenOptions->provider) * 2) {
×
4802
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
×
4803
    return;
×
4804
  }
4805

4806
  char buf[sizeof(pTokenOptions->provider) * 2 + 1];
×
4807
  memcpy(buf, pProvider->z, pProvider->n);
×
4808
  buf[pProvider->n] = 0;
×
4809
  (void)strdequote(buf);
×
4810
  size_t len = strtrim(buf);
×
4811

4812
  if (len >= sizeof(pTokenOptions->provider)) {
×
4813
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "PROVIDER", sizeof(pTokenOptions->provider));
×
4814
  } else {
4815
    tstrncpy(pTokenOptions->provider, buf, sizeof(pTokenOptions->provider));
×
4816
  }
4817
}
4818

4819

4820

4821
void setTokenOptionsExtraInfo(SAstCreateContext* pCxt, STokenOptions* pTokenOptions, const SToken* pExtraInfo) {
×
4822
  pTokenOptions->hasExtraInfo = true;
×
4823

4824
  if (pExtraInfo->n >= sizeof(pTokenOptions->extraInfo) * 2) {
×
4825
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
×
4826
    return;
×
4827
  }
4828

4829
  char buf[sizeof(pTokenOptions->extraInfo) * 2 + 1];
×
4830
  memcpy(buf, pExtraInfo->z, pExtraInfo->n);
×
4831
  buf[pExtraInfo->n] = 0;
×
4832
  (void)strdequote(buf);
×
4833
  size_t len = strtrim(buf);
×
4834

4835
  if (len >= sizeof(pTokenOptions->extraInfo)) {
×
4836
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "EXTRA_INFO", sizeof(pTokenOptions->extraInfo));
×
4837
  } else {
4838
    tstrncpy(pTokenOptions->extraInfo, buf, sizeof(pTokenOptions->extraInfo));
×
4839
  }
4840
}
4841

4842

4843

4844
static bool isValidTokenOptions(SAstCreateContext* pCxt, const STokenOptions* opts) {
×
4845
  if (opts->hasEnable && (opts->enable < 0 || opts->enable > 1)) {
×
4846
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "ENABLE");
×
4847
    return false;
×
4848
  }
4849

4850
  if (opts->hasTtl && (opts->ttl < 0)) {
×
4851
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_VALUE, "TTL");
×
4852
    return false;
×
4853
  }
4854

4855
  return true;
×
4856
}
4857

4858

4859

4860
static bool checkTokenName(SAstCreateContext* pCxt, SToken* pTokenName) {
45✔
4861
  if (NULL == pTokenName) {
45✔
4862
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4863
  } else {
4864
    if (pTokenName->n >= TSDB_TOKEN_NAME_LEN) {
45✔
4865
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG, "TOKEN_NAME", TSDB_TOKEN_NAME_LEN);
×
4866
    }
4867
  }
4868
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
45✔
4869
    trimEscape(pCxt, pTokenName, true);
45✔
4870
  }
4871
  return TSDB_CODE_SUCCESS == pCxt->errCode;
45✔
4872
}
4873

4874
SNode* createCreateRoleStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pName) {
156✔
4875
  CHECK_PARSER_STATUS(pCxt);
156✔
4876
  CHECK_NAME(checkRoleName(pCxt, pName, true));
156✔
4877
  SCreateRoleStmt* pStmt = NULL;
156✔
4878
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ROLE_STMT, (SNode**)&pStmt);
156✔
4879
  CHECK_MAKE_NODE(pStmt);
156✔
4880
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
156✔
4881
  pStmt->ignoreExists = ignoreExists;
156✔
4882
  return (SNode*)pStmt;
156✔
4883
_err:
×
4884
  return NULL;
×
4885
}
4886

4887
SNode* createDropRoleStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pName) {
×
4888
  CHECK_PARSER_STATUS(pCxt);
×
4889
  CHECK_NAME(checkRoleName(pCxt, pName, true));
×
4890
  SDropRoleStmt* pStmt = NULL;
×
4891
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ROLE_STMT, (SNode**)&pStmt);
×
4892
  CHECK_MAKE_NODE(pStmt);
×
4893
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
×
4894
  pStmt->ignoreNotExists = ignoreNotExists;
×
4895
  return (SNode*)pStmt;
×
4896
_err:
×
4897
  return NULL;
×
4898
}
4899

4900
/**
4901
 * used by user and role
4902
 */
4903
SNode* createAlterRoleStmt(SAstCreateContext* pCxt, SToken* pName, int8_t alterType, void* pAlterInfo) {
×
4904
  SAlterRoleStmt* pStmt = NULL;
×
4905
  CHECK_PARSER_STATUS(pCxt);
×
4906
  CHECK_NAME(checkUserName(pCxt, pName));
×
4907
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ROLE_STMT, (SNode**)&pStmt);
×
4908
  CHECK_MAKE_NODE(pStmt);
×
4909
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pName);
×
4910
  pStmt->alterType = alterType;
×
4911
  switch (alterType) {
×
4912
    case TSDB_ALTER_ROLE_LOCK: {
×
4913
      SToken* pVal = pAlterInfo;
×
4914
      pStmt->lock = taosStr2Int8(pVal->z, NULL, 10);
×
4915
      break;
×
4916
    }
4917
    default:
×
4918
      break;
×
4919
  }
4920
  return (SNode*)pStmt;
×
4921
_err:
×
4922
  nodesDestroyNode((SNode*)pStmt);
×
4923
  return NULL;
×
4924
}
4925

4926

4927
SNode* createCreateTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, SToken* pUserName, STokenOptions* opts, bool ignoreExists) {
45✔
4928
  SCreateTokenStmt* pStmt = NULL;
45✔
4929

4930
  CHECK_PARSER_STATUS(pCxt);
45✔
4931
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
45✔
4932
  CHECK_NAME(checkUserName(pCxt, pUserName));
45✔
4933

4934
  if (opts == NULL) {
45✔
4935
    opts = createDefaultTokenOptions(pCxt);
45✔
4936
  } else if (!isValidTokenOptions(pCxt, opts)) {
×
4937
    goto _err;
×
4938
  }
4939

4940
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOKEN_STMT, (SNode**)&pStmt);
45✔
4941
  CHECK_MAKE_NODE(pStmt);
45✔
4942

4943
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
45✔
4944
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
45✔
4945
  pStmt->enable = opts->enable;
45✔
4946
  pStmt->ignoreExists = ignoreExists;
45✔
4947
  pStmt->ttl = opts->ttl;
45✔
4948
  tstrncpy(pStmt->provider, opts->provider, sizeof(pStmt->provider));
45✔
4949
  tstrncpy(pStmt->extraInfo, opts->extraInfo, sizeof(pStmt->extraInfo));
45✔
4950
  nodesDestroyNode((SNode*)opts);
45✔
4951
  return (SNode*)pStmt;
45✔
4952

4953
_err:
×
4954
  nodesDestroyNode((SNode*)pStmt);
×
4955
  nodesDestroyNode((SNode*)opts);
×
4956
  return NULL;
×
4957
}
4958

4959

4960

4961
SNode* createAlterTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, STokenOptions* opts) {
×
4962
  SAlterTokenStmt* pStmt = NULL;
×
4963

4964
  CHECK_PARSER_STATUS(pCxt);
×
4965
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
×
4966
  if (!isValidTokenOptions(pCxt, opts)) {
×
4967
    goto _err;
×
4968
  }
4969

4970
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TOKEN_STMT, (SNode**)&pStmt);
×
4971
  CHECK_MAKE_NODE(pStmt);
×
4972

4973
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
×
4974
  pStmt->pTokenOptions = opts;
×
4975
  return (SNode*)pStmt;
×
4976

4977
_err:
×
4978
  nodesDestroyNode((SNode*)pStmt);
×
4979
  nodesDestroyNode((SNode*)opts);
×
4980
  return NULL;
×
4981
}
4982

4983

4984

4985
SNode* createDropTokenStmt(SAstCreateContext* pCxt, SToken* pTokenName, bool ignoreNotExists) {
×
4986
  SDropTokenStmt* pStmt = NULL;
×
4987

4988
  CHECK_PARSER_STATUS(pCxt);
×
4989
  CHECK_NAME(checkTokenName(pCxt, pTokenName));
×
4990

4991
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOKEN_STMT, (SNode**)&pStmt);
×
4992
  CHECK_MAKE_NODE(pStmt);
×
4993

4994
  COPY_STRING_FORM_ID_TOKEN(pStmt->name, pTokenName);
×
4995
  pStmt->ignoreNotExists = ignoreNotExists;
×
4996
  return (SNode*)pStmt;
×
4997

4998
_err:
×
4999
  nodesDestroyNode((SNode*)pStmt);
×
5000
  return NULL;
×
5001
}
5002

5003
SNode* createCreateTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
45✔
5004
  SCreateTotpSecretStmt* pStmt = NULL;
45✔
5005

5006
  CHECK_PARSER_STATUS(pCxt);
45✔
5007
  CHECK_NAME(checkUserName(pCxt, pUserName));
45✔
5008

5009
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOTP_SECRET_STMT, (SNode**)&pStmt);
45✔
5010
  CHECK_MAKE_NODE(pStmt);
45✔
5011

5012
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
45✔
5013
  return (SNode*)pStmt;
45✔
5014

5015
_err:
×
5016
  nodesDestroyNode((SNode*)pStmt);
×
5017
  return NULL;
×
5018
}
5019

5020

5021
SNode* createDropTotpSecretStmt(SAstCreateContext* pCxt, SToken* pUserName) {
×
5022
  SDropTotpSecretStmt* pStmt = NULL;
×
5023

5024
  CHECK_PARSER_STATUS(pCxt);
×
5025
  CHECK_NAME(checkUserName(pCxt, pUserName));
×
5026

5027
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOTP_SECRET_STMT, (SNode**)&pStmt);
×
5028
  CHECK_MAKE_NODE(pStmt);
×
5029

5030
  COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUserName);
×
5031
  return (SNode*)pStmt;
×
5032

5033
_err:
×
5034
  nodesDestroyNode((SNode*)pStmt);
×
5035
  return NULL;
×
5036
}
5037

5038

5039
SNode* createDropEncryptAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId) {
×
5040
  CHECK_PARSER_STATUS(pCxt);
×
5041
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5042
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
5043
    goto _err;
×
5044
  }
5045
  SDropEncryptAlgrStmt* pStmt = NULL;
×
5046
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ENCRYPT_ALGR_STMT, (SNode**)&pStmt);
×
5047
  CHECK_MAKE_NODE(pStmt);
×
5048
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
5049
  return (SNode*)pStmt;
×
5050
_err:
×
5051
  return NULL;
×
5052
}
5053

5054
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
148,457✔
5055
  CHECK_PARSER_STATUS(pCxt);
148,457✔
5056
  SCreateDnodeStmt* pStmt = NULL;
148,457✔
5057
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
148,457✔
5058
  CHECK_MAKE_NODE(pStmt);
148,457✔
5059
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
148,457✔
5060
    nodesDestroyNode((SNode*)pStmt);
×
5061
    return NULL;
×
5062
  }
5063
  return (SNode*)pStmt;
148,457✔
5064
_err:
×
5065
  return NULL;
×
5066
}
5067

5068
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
10,053✔
5069
  CHECK_PARSER_STATUS(pCxt);
10,053✔
5070
  SDropDnodeStmt* pStmt = NULL;
10,053✔
5071
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
10,053✔
5072
  CHECK_MAKE_NODE(pStmt);
10,053✔
5073
  if (TK_NK_INTEGER == pDnode->type) {
10,053✔
5074
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
10,053✔
5075
  } else {
5076
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
×
5077
      nodesDestroyNode((SNode*)pStmt);
×
5078
      return NULL;
×
5079
    }
5080
  }
5081
  pStmt->force = force;
10,053✔
5082
  pStmt->unsafe = unsafe;
10,053✔
5083
  return (SNode*)pStmt;
10,053✔
5084
_err:
×
5085
  return NULL;
×
5086
}
5087

5088
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
93,507✔
5089
                            const SToken* pValue) {
5090
  CHECK_PARSER_STATUS(pCxt);
93,507✔
5091
  SAlterDnodeStmt* pStmt = NULL;
93,507✔
5092
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
93,507✔
5093
  CHECK_MAKE_NODE(pStmt);
93,507✔
5094
  if (NULL != pDnode) {
93,507✔
5095
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
60,770✔
5096
  } else {
5097
    pStmt->dnodeId = -1;
32,737✔
5098
  }
5099
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
93,507✔
5100
  if (NULL != pValue) {
93,507✔
5101
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
46,735✔
5102
  }
5103
  return (SNode*)pStmt;
93,507✔
5104
_err:
×
5105
  return NULL;
×
5106
}
5107

5108
SNode* createCreateAlgrStmt(SAstCreateContext* pCxt, SToken* algorithmId, const SToken* name, const SToken* desc,
×
5109
                            const SToken* type, const SToken* osslAlgrName) {
5110
  CHECK_PARSER_STATUS(pCxt);
×
5111
  SCreateEncryptAlgrStmt* pStmt = NULL;
×
5112
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ENCRYPT_ALGORITHMS_STMT, (SNode**)&pStmt);
×
5113
  CHECK_MAKE_NODE(pStmt);
×
5114
  if (algorithmId->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5115
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_ID_TOO_LONG);
×
5116
    goto _err;
×
5117
  }
5118
  if (name->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5119
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_NAME_TOO_LONG);
×
5120
    goto _err;
×
5121
  }
5122
  if (desc->n >= TSDB_ENCRYPT_ALGR_DESC_LEN) {
×
5123
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_DESC_TOO_LONG);
×
5124
    goto _err;
×
5125
  }
5126
  if (type->n >= TSDB_ENCRYPT_ALGR_TYPE_LEN) {
×
5127
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_TYPE_TOO_LONG);
×
5128
    goto _err;
×
5129
  }
5130
  if (osslAlgrName->n >= TSDB_ENCRYPT_ALGR_NAME_LEN) {
×
5131
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ALGR_OSSL_NAME_TOO_LONG);
×
5132
    goto _err;
×
5133
  }
5134
  (void)trimString(algorithmId->z, algorithmId->n, pStmt->algorithmId, sizeof(pStmt->algorithmId));
×
5135
  (void)trimString(name->z, name->n, pStmt->name, sizeof(pStmt->name));
×
5136
  (void)trimString(desc->z, desc->n, pStmt->desc, sizeof(pStmt->desc));
×
5137
  (void)trimString(type->z, type->n, pStmt->algrType, sizeof(pStmt->algrType));
×
5138
  (void)trimString(osslAlgrName->z, osslAlgrName->n, pStmt->osslAlgrName, sizeof(pStmt->osslAlgrName));
×
5139
  return (SNode*)pStmt;
×
5140
_err:
×
5141
  return NULL;
×
5142
}
5143

5144
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
×
5145
  CHECK_PARSER_STATUS(pCxt);
×
5146
  SCreateAnodeStmt* pStmt = NULL;
×
5147
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
×
5148
  CHECK_MAKE_NODE(pStmt);
×
5149
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
×
5150
  return (SNode*)pStmt;
×
5151
_err:
×
5152
  return NULL;
×
5153
}
5154

5155
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
×
5156
  CHECK_PARSER_STATUS(pCxt);
×
5157
  SUpdateAnodeStmt* pStmt = NULL;
×
5158
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
×
5159
  CHECK_MAKE_NODE(pStmt);
×
5160
  if (NULL != pAnode) {
×
5161
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5162
  } else {
5163
    pStmt->anodeId = -1;
×
5164
  }
5165
  return (SNode*)pStmt;
×
5166
_err:
×
5167
  return NULL;
×
5168
}
5169

5170
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
5171
  CHECK_PARSER_STATUS(pCxt);
×
5172
  SUpdateAnodeStmt* pStmt = NULL;
×
5173
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
5174
  CHECK_MAKE_NODE(pStmt);
×
5175
  if (NULL != pAnode) {
×
5176
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
5177
  } else {
5178
    pStmt->anodeId = -1;
×
5179
  }
5180
  return (SNode*)pStmt;
×
5181
_err:
×
5182
  return NULL;
×
5183
}
5184

5185
SNode* createCreateBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId, SNode* pOptions) {
22,492✔
5186
  CHECK_PARSER_STATUS(pCxt);
22,492✔
5187
  SCreateBnodeStmt* pStmt = NULL;
22,492✔
5188
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_BNODE_STMT, (SNode**)&pStmt);
22,492✔
5189
  CHECK_MAKE_NODE(pStmt);
22,492✔
5190
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
22,492✔
5191

5192
  pStmt->pOptions = (SBnodeOptions*)pOptions;
22,492✔
5193

5194
  return (SNode*)pStmt;
22,492✔
5195
_err:
×
5196
  return NULL;
×
5197
}
5198

5199
SNode* createDropBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId) {
26,647✔
5200
  CHECK_PARSER_STATUS(pCxt);
26,647✔
5201
  SUpdateBnodeStmt* pStmt = NULL;
26,647✔
5202
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_BNODE_STMT, (SNode**)&pStmt);
26,647✔
5203
  CHECK_MAKE_NODE(pStmt);
26,647✔
5204
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
26,647✔
5205

5206
  return (SNode*)pStmt;
26,647✔
5207
_err:
×
5208
  return NULL;
×
5209
}
5210

5211
SNode* createDefaultBnodeOptions(SAstCreateContext* pCxt) {
22,492✔
5212
  CHECK_PARSER_STATUS(pCxt);
22,492✔
5213
  SBnodeOptions* pOptions = NULL;
22,492✔
5214
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BNODE_OPTIONS, (SNode**)&pOptions);
22,492✔
5215
  CHECK_MAKE_NODE(pOptions);
22,492✔
5216

5217
  tstrncpy(pOptions->protoStr, TSDB_BNODE_OPT_PROTO_DFT_STR, TSDB_BNODE_OPT_PROTO_STR_LEN);
22,492✔
5218
  pOptions->proto = TSDB_BNODE_OPT_PROTO_DEFAULT;
22,492✔
5219

5220
  return (SNode*)pOptions;
22,492✔
5221
_err:
×
5222
  return NULL;
×
5223
}
5224

5225
static SNode* setBnodeOptionImpl(SAstCreateContext* pCxt, SNode* pBodeOptions, EBnodeOptionType type, void* pVal,
×
5226
                                 bool alter) {
5227
  CHECK_PARSER_STATUS(pCxt);
×
5228
  SBnodeOptions* pOptions = (SBnodeOptions*)pBodeOptions;
×
5229
  switch (type) {
×
5230
    case BNODE_OPTION_PROTOCOL:
×
5231
      COPY_STRING_FORM_STR_TOKEN(pOptions->protoStr, (SToken*)pVal);
×
5232
      break;
×
5233
    default:
×
5234
      break;
×
5235
  }
5236

5237
  return pBodeOptions;
×
5238
_err:
×
5239
  nodesDestroyNode(pBodeOptions);
×
5240
  return NULL;
×
5241
}
5242

5243
SNode* setBnodeOption(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pOption, void* pVal) {
×
5244
  if (0 == strncasecmp(pOption->z, "protocol", 8)) {
×
5245
    return setBnodeOptionImpl(pCxt, pOptions, BNODE_OPTION_PROTOCOL, pVal, false);
×
5246
  } else {
5247
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5248
    return pOptions;
×
5249
  }
5250
}
5251

5252
SNode* createCreateXnodeWithUserPassStmt(SAstCreateContext* pCxt, const SToken* pUrl, SToken* pUser,
127✔
5253
                                         const SToken* pPass) {
5254
  CHECK_PARSER_STATUS(pCxt);
127✔
5255
  SCreateXnodeStmt* pStmt = NULL;
127✔
5256
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
127✔
5257
  CHECK_MAKE_NODE(pStmt);
127✔
5258

5259
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
127✔
5260

5261
  if (pUser != NULL) {
127✔
5262
    CHECK_NAME(checkUserName(pCxt, pUser));
127✔
5263
    COPY_STRING_FORM_ID_TOKEN(pStmt->user, pUser);
127✔
5264
  }
5265
  if (pPass != NULL) {
127✔
5266
    if (pPass->n <= 2) {
127✔
5267
      pCxt->errCode =
×
5268
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode password should not be empty");
×
5269
      goto _err;
×
5270
    }
5271
    strncpy(pStmt->pass, pPass->z + 1, pPass->n - 2);
127✔
5272
    pStmt->pass[sizeof(pStmt->pass) - 1] = '\0';
127✔
5273
  }
5274
  return (SNode*)pStmt;
127✔
5275
_err:
×
5276
  return NULL;
×
5277
}
5278
SNode* createCreateXnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
127✔
5279
  CHECK_PARSER_STATUS(pCxt);
127✔
5280
  SCreateXnodeStmt* pStmt = NULL;
127✔
5281
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_STMT, (SNode**)&pStmt);
127✔
5282
  CHECK_MAKE_NODE(pStmt);
127✔
5283
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
127✔
5284
  return (SNode*)pStmt;
127✔
5285
_err:
×
5286
  return NULL;
×
5287
}
5288

5289
SNode* createDropXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode, bool force) {
3,937✔
5290
  if (NULL == pXnode) {
3,937✔
5291
    pCxt->errCode =
×
5292
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
5293
    goto _err;
×
5294
  }
5295
  CHECK_PARSER_STATUS(pCxt);
3,937✔
5296
  SDropXnodeStmt* pStmt = NULL;
3,937✔
5297
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_STMT, (SNode**)&pStmt);
3,937✔
5298
  CHECK_MAKE_NODE(pStmt);
3,937✔
5299

5300
  pStmt->force = force;
3,937✔
5301
  if (pXnode->type == TK_NK_STRING) {
3,937✔
5302
    if (pXnode->n <= 2) {
1,397✔
5303
      pCxt->errCode =
×
5304
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode url should not be all be NULL");
×
5305
        goto _err;
×
5306
    }
5307
    COPY_STRING_FORM_STR_TOKEN(pStmt->url, pXnode);
1,397✔
5308
  } else if(pXnode->type == TK_NK_INTEGER) {
2,540✔
5309
    pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
2,540✔
5310
  } else {
5311
    pCxt->errCode =
×
5312
      generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id or url should not be all be NULL");
×
5313
  }
5314

5315
  return (SNode*)pStmt;
3,937✔
5316
_err:
×
5317
  return NULL;
×
5318
}
5319

5320
SNode* createDrainXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode) {
1,270✔
5321
  CHECK_PARSER_STATUS(pCxt);
1,270✔
5322
  if (NULL == pXnode) {
1,270✔
5323
    pCxt->errCode =
×
5324
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should not be NULL or empty");
×
5325
    goto _err;
×
5326
  }
5327
  if (pXnode->type != TK_NK_INTEGER) {
1,270✔
5328
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be an integer");
×
5329
    goto _err;
×
5330
  }
5331

5332
  SDrainXnodeStmt* pStmt = NULL;
1,270✔
5333
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DRAIN_XNODE_STMT, (SNode**)&pStmt);
1,270✔
5334
  CHECK_MAKE_NODE(pStmt);
1,270✔
5335
  pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
1,270✔
5336
  if (pStmt->xnodeId <= 0) {
1,270✔
5337
    pCxt->errCode =
×
5338
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "xnode id should be greater than 0");
×
5339
    goto _err;
×
5340
  }
5341

5342
  return (SNode*)pStmt;
1,270✔
5343
_err:
×
5344
  return NULL;
×
5345
}
5346

5347
// SNode* createUpdateXnodeStmt(SAstCreateContext* pCxt, const SToken* pXnode, bool updateAll) {
5348
//   CHECK_PARSER_STATUS(pCxt);
5349
//   SUpdateXnodeStmt* pStmt = NULL;
5350
//   pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_XNODE_STMT, (SNode**)&pStmt);
5351
//   CHECK_MAKE_NODE(pStmt);
5352
//   if (NULL != pXnode) {
5353
//     pStmt->xnodeId = taosStr2Int32(pXnode->z, NULL, 10);
5354
//   } else {
5355
//     pStmt->xnodeId = -1;
5356
//   }
5357
//   return (SNode*)pStmt;
5358
// _err:
5359
//   return NULL;
5360
// }
5361

5362
EXnodeResourceType setXnodeResourceType(SAstCreateContext* pCxt, const SToken* pResourceId) {
1,152✔
5363
  CHECK_PARSER_STATUS(pCxt);
1,152✔
5364
  const size_t TASK_LEN = 4;
1,152✔
5365
  const size_t TASKS_LEN = 5;
1,152✔
5366
  const size_t AGENT_LEN = 5;
1,152✔
5367
  const size_t AGENTS_LEN = 6;
1,152✔
5368
  const size_t JOB_LEN = 3;
1,152✔
5369
  const size_t JOBS_LEN = 4;
1,152✔
5370

5371
  if (pResourceId->z[0] == '`') {
1,152✔
5372
    if (strncmp(pResourceId->z + 1, "task", TASK_LEN) == 0 || strncmp(pResourceId->z + 1, "tasks", TASKS_LEN) == 0) {
×
5373
      return XNODE_TASK;
×
5374
    }
5375
    if (strncmp(pResourceId->z + 1, "agent", AGENT_LEN) == 0 ||
×
5376
        strncmp(pResourceId->z + 1, "agents", AGENTS_LEN) == 0) {
×
5377
      return XNODE_AGENT;
×
5378
    }
5379
    if (strncmp(pResourceId->z + 1, "job", JOB_LEN) == 0 || strncmp(pResourceId->z + 1, "jobs", JOBS_LEN) == 0) {
×
5380
      return XNODE_JOB;
×
5381
    }
5382

5383
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5384
                                            "Invalid xnode resource type (task/agent) at: %s", pResourceId->z);
×
5385
    goto _err;
×
5386
  }
5387
  if (strncmp(pResourceId->z, "task", TASK_LEN) == 0 || strncmp(pResourceId->z, "tasks", TASKS_LEN) == 0) {
1,152✔
5388
    return XNODE_TASK;
384✔
5389
  }
5390
  if (strncmp(pResourceId->z, "agent", AGENT_LEN) == 0 || strncmp(pResourceId->z, "agents", AGENTS_LEN) == 0) {
768✔
5391
    return XNODE_AGENT;
384✔
5392
  }
5393
  if (strncmp(pResourceId->z, "job", JOB_LEN) == 0 || strncmp(pResourceId->z, "jobs", JOBS_LEN) == 0) {
384✔
5394
    return XNODE_JOB;
384✔
5395
  }
5396
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5397
                                          "Invalid xnode resource type (task/agent/job) at: %s", pResourceId->z);
×
5398
  goto _err;
×
5399

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

5513
  return (SNode*)pSink;
×
5514
_err:
×
5515
  return NULL;
×
5516
}
5517

5518
SNode* createXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pSource,
×
5519
                                          SNode* pSink, SNode* pNode) {
5520
  SNode* pStmt = NULL;
×
5521
  if (pResourceName == NULL) {
×
5522
    pCxt->errCode =
×
5523
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name should not be NULL");
×
5524
    goto _err;
×
5525
  }
5526
  if (pSource == NULL || pSink == NULL) {
×
5527
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5528
                                            "Xnode task source and sink should not be NULL");
5529
    goto _err;
×
5530
  }
5531
  if (nodeType(pSource) != QUERY_NODE_XNODE_TASK_SOURCE_OPT || nodeType(pSink) != QUERY_NODE_XNODE_TASK_SINK_OPT) {
×
5532
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5533
                                            "Xnode task source and sink should be valid nodes");
5534
    goto _err;
×
5535
  }
5536
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_TASK_STMT, (SNode**)&pStmt);
×
5537
  CHECK_MAKE_NODE(pStmt);
×
5538
  SCreateXnodeTaskStmt* pTaskStmt = (SCreateXnodeTaskStmt*)pStmt;
×
5539
  if (pResourceName->type == TK_NK_STRING) {
×
5540
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
×
5541
  } else if (pResourceName->type == TK_NK_ID) {
×
5542
    COPY_STRING_FORM_STR_TOKEN(pTaskStmt->name, pResourceName);
×
5543
  } else {
5544
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode name type: %d",
×
5545
                                            pResourceName->type);
×
5546
    goto _err;
×
5547
  }
5548

5549
  if (pSource != NULL) {
×
5550
    SXTaskSource* source = (SXTaskSource*)(pSource);
×
5551
    pTaskStmt->source = source;
×
5552
  }
5553
  if (pSink != NULL) {
×
5554
    SXTaskSink* sink = (SXTaskSink*)(pSink);
×
5555
    pTaskStmt->sink = sink;
×
5556
  }
5557
  if (pNode != NULL) {
×
5558
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
×
5559
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
×
5560
      pTaskStmt->options = options;
×
5561
    }
5562
  }
5563
  return (SNode*)pTaskStmt;
×
5564
_err:
×
5565
  if (pStmt != NULL) {
×
5566
    nodesDestroyNode(pStmt);
×
5567
  }
5568
  return NULL;
×
5569
}
5570

5571
SNode* createXnodeAgentWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResourceName, SNode* pSource,
×
5572
                                           SNode* pSink, SNode* pNode) {
5573
  SNode* pStmt = NULL;
×
5574
  if (pSource != NULL || pSink != NULL) {
×
5575
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5576
                                            "Xnode agent should not have source or sink");
5577
    goto _err;
×
5578
  }
5579

5580
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODES_STMT, (SNode**)&pStmt);
×
5581
  CHECK_MAKE_NODE(pStmt);
×
5582
_err:
×
5583
  if (pStmt != NULL) {
×
5584
    nodesDestroyNode(pStmt);
×
5585
  }
5586
  return NULL;
×
5587
}
5588

5589
SNode* createXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResourceName,
×
5590
                                  SNode* pSource, SNode* pSink, SNode* pNode) {
5591
  CHECK_PARSER_STATUS(pCxt);
×
5592

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

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

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

5646
  return (SNode*)pTaskStmt;
×
5647
_err:
×
5648
  if (pStmt != NULL) {
×
5649
    nodesDestroyNode(pStmt);
×
5650
  }
5651
  return NULL;
×
5652
}
5653

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

5689
  return (SNode*)pTaskStmt;
×
5690
_err:
×
5691
  if (pStmt != NULL) {
×
5692
    nodesDestroyNode(pStmt);
×
5693
  }
5694
  return NULL;
×
5695
}
5696

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

5717
  SRebalanceXnodeJobStmt* pJobStmt = (SRebalanceXnodeJobStmt*)pStmt;
×
5718
  char                    buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
×
5719
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceId);
×
5720
  pJobStmt->jid = atoi(buf);
×
5721

5722
  if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
×
5723
    SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
×
5724
    // printXnodeTaskOptions(&options->opts);
5725
    pJobStmt->options = options;
×
5726
  }
5727
  return (SNode*)pJobStmt;
×
5728
_err:
×
5729
  return NULL;
×
5730
}
5731

5732
SNode* createRebalanceXnodeJobStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* resourceId,
×
5733
                                   SNode* pNodeOptions) {
5734
  CHECK_PARSER_STATUS(pCxt);
×
5735

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

5750
SNode* rebalanceXnodeJobWhereDirectly(SAstCreateContext* pCxt, SNode* pWhere) {
×
5751
  int32_t code = 0;
×
5752
  SNode*  pStmt = NULL;
×
5753

5754
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REBALANCE_XNODE_JOB_WHERE_STMT, (SNode**)&pStmt);
×
5755
  CHECK_MAKE_NODE(pStmt);
×
5756

5757
  SRebalanceXnodeJobWhereStmt* pJobStmt = (SRebalanceXnodeJobWhereStmt*)pStmt;
×
5758
  pJobStmt->pWhere = pWhere;
×
5759

5760
  return (SNode*)pJobStmt;
×
5761
_err:
×
5762
  return NULL;
×
5763
}
5764

5765
SNode* createRebalanceXnodeJobWhereStmt(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
×
5766
  CHECK_PARSER_STATUS(pCxt);
×
5767

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

5782
SNode* updateXnodeTaskWithOptionsDirectly(SAstCreateContext* pCxt, const SToken* pResIdOrName, SNode* pSource,
×
5783
                                          SNode* pSink, SNode* pNode) {
5784
  SNode* pStmt = NULL;
×
5785

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

5798
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_XNODE_TASK_STMT, (SNode**)&pStmt);
×
5799
  CHECK_MAKE_NODE(pStmt);
×
5800
  SUpdateXnodeTaskStmt* pTaskStmt = (SUpdateXnodeTaskStmt*)pStmt;
×
5801
  if (pResIdOrName->type == TK_NK_INTEGER) {
×
5802
    pTaskStmt->tid = taosStr2Int32(pResIdOrName->z, NULL, 10);
×
5803
  } else {
5804
    if (pResIdOrName->n <= 2) {
×
5805
      pCxt->errCode =
×
5806
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task name be empty string");
×
5807
      goto _err;
×
5808
    }
5809
    char buf[TSDB_XNODE_NAME_LEN];
×
5810
    COPY_STRING_FORM_STR_TOKEN(buf, pResIdOrName);
×
5811
    pTaskStmt->name = xCreateCowStr(strlen(buf) + 1, buf, true);
×
5812
  }
5813

5814
  if (pSource != NULL) {
×
5815
    SXTaskSource* source = (SXTaskSource*)(pSource);
×
5816
    pTaskStmt->source = source;
×
5817
  }
5818
  if (pSink != NULL) {
×
5819
    SXTaskSink* sink = (SXTaskSink*)(pSink);
×
5820
    pTaskStmt->sink = sink;
×
5821
  }
5822
  if (pNode != NULL) {
×
5823
    if (nodeType(pNode) == QUERY_NODE_XNODE_TASK_OPTIONS) {
×
5824
      SXnodeTaskOptions* options = (SXnodeTaskOptions*)(pNode);
×
5825
      pTaskStmt->options = options;
×
5826
    }
5827
  }
5828
  return (SNode*)pTaskStmt;
×
5829
_err:
×
5830
  if (pStmt != NULL) {
×
5831
    nodesDestroyNode(pStmt);
×
5832
  }
5833
  return NULL;
×
5834
}
5835

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

5856
  SAlterXnodeJobStmt* pJobStmt = (SAlterXnodeJobStmt*)pStmt;
×
5857
  char                buf[TSDB_XNODE_RESOURCE_ID_LEN] = {0};
×
5858
  COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
5859
  pJobStmt->jid = atoi(buf);
×
5860

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

5873
SNode* alterXnodeTaskWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pResourceId,
×
5874
                                 SNode* pSource, SNode* pSink, SNode* pNode) {
5875
  CHECK_PARSER_STATUS(pCxt);
×
5876

5877
  switch (resourceType) {
×
5878
    case XNODE_TASK: {
×
5879
      return updateXnodeTaskWithOptionsDirectly(pCxt, pResourceId, pSource, pSink, pNode);
×
5880
    }
5881
    case XNODE_AGENT: {
×
5882
      return createXnodeAgentWithOptionsDirectly(pCxt, pResourceId, pSource, pSink, pNode);
×
5883
      break;
5884
    }
5885
    case XNODE_JOB: {
×
5886
      return alterXnodeJobWithOptionsDirectly(pCxt, pResourceId, pNode);
×
5887
    }
5888
    default:
×
5889
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5890
                                              "Invalid xnode resource type: %d", resourceType);
5891
      goto _err;
×
5892
  }
5893
_err:
×
5894
  return NULL;
×
5895
}
5896

5897
SNode* dropXnodeResource(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SToken* pResourceName) {
×
5898
  SNode* pStmt = NULL;
×
5899
  char   buf[TSDB_XNODE_TASK_NAME_LEN + 1];
×
5900

5901
  CHECK_PARSER_STATUS(pCxt);
×
5902
  if (pResourceName == NULL || pResourceName->n <= 0) {
×
5903
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5904
                                            "Xnode resource name should not be NULL or empty");
5905
    goto _err;
×
5906
  }
5907
  if (pResourceName->n > TSDB_XNODE_RESOURCE_NAME_LEN) {
×
5908
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5909
                                            "Invalid xnode resource name length: %d, max length: %d", pResourceName->n,
5910
                                            TSDB_XNODE_RESOURCE_NAME_LEN);
5911
    goto _err;
×
5912
  }
5913
  switch (resourceType) {
×
5914
    case XNODE_TASK:
×
5915
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
×
5916
      CHECK_MAKE_NODE(pStmt);
×
5917
      SDropXnodeTaskStmt* pTaskStmt = (SDropXnodeTaskStmt*)pStmt;
×
5918

5919
      if (pResourceName->type == TK_NK_STRING) {
×
5920
        if (pResourceName->n > TSDB_XNODE_TASK_NAME_LEN + 2) {
×
5921
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5922
                                                  "Invalid xnode task name length: %d, max length: %d",
5923
                                                  pResourceName->n, TSDB_XNODE_TASK_NAME_LEN);
5924
          goto _err;
×
5925
        }
5926
        COPY_STRING_FORM_STR_TOKEN(buf, pResourceName);
×
5927
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
×
5928
      } else if (pResourceName->type == TK_NK_ID) {
×
5929
        COPY_STRING_FORM_ID_TOKEN(buf, pResourceName);
×
5930
        pTaskStmt->name = taosStrndupi(buf, sizeof(buf));
×
5931
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
5932
        pTaskStmt->tid = taosStr2Int32(pResourceName->z, NULL, 10);
×
5933
      } else {
5934
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5935
                                                "Invalid xnode job id type: %d", pResourceName->type);
5936
        goto _err;
×
5937
      }
5938
      break;
×
5939
    case XNODE_AGENT:
×
5940
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
×
5941
      CHECK_MAKE_NODE(pStmt);
×
5942
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5943
                                              "Unimplemented drop xnode agent with: %s", pResourceName->z);
5944
      goto _err;
×
5945
      break;
5946
    case XNODE_JOB:
×
5947
      pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_JOB_STMT, (SNode**)&pStmt);
×
5948
      CHECK_MAKE_NODE(pStmt);
×
5949
      SDropXnodeJobStmt* pJobStmt = (SDropXnodeJobStmt*)pStmt;
×
5950

5951
      if (pResourceName->type == TK_NK_STRING) {
×
5952
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
×
5953
      } else if (pResourceName->type == TK_NK_INTEGER) {
×
5954
        pJobStmt->jid = taosStr2Int32(pResourceName->z, NULL, 10);
×
5955
      } else {
5956
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5957
                                                "Invalid xnode job id type: %d", pResourceName->type);
5958
        goto _err;
×
5959
      }
5960
      break;
×
5961
    default:
×
5962
      break;
×
5963
  }
5964
  return (SNode*)pStmt;
×
5965
_err:
×
5966
  if (pStmt != NULL) {
×
5967
    nodesDestroyNode(pStmt);
×
5968
  }
5969
  return NULL;
×
5970
}
5971
SNode* dropXnodeResourceOn(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SToken* pResource, SNode* pWhere) {
×
5972
  char resourceId[TSDB_XNODE_RESOURCE_ID_LEN + 1];
5973
  SShowStmt* pStmt = NULL;
×
5974

5975
  CHECK_PARSER_STATUS(pCxt);
×
5976
  if (pResource == NULL || pResource->n <= 0) {
×
5977
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5978
                                            "xnode resource name should not be NULL or empty");
5979
    goto _err;
×
5980
  }
5981
  if (pResource->n > TSDB_XNODE_RESOURCE_NAME_LEN) {
×
5982
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
5983
                                            "Invalid xnode resource name length: %d, max length: %d", pResource->n,
5984
                                            TSDB_XNODE_RESOURCE_NAME_LEN);
5985
    goto _err;
×
5986
  }
5987
  switch (resourceType) {
×
5988
    case XNODE_TASK:
×
5989
      // pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
5990
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_TASKS_STMT, (SNode**)&pStmt);
×
5991
      CHECK_MAKE_NODE(pStmt);
×
5992
      break;
×
5993
    case XNODE_AGENT:
×
5994
      // pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
5995
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_AGENTS_STMT, (SNode**)&pStmt);
×
5996
      CHECK_MAKE_NODE(pStmt);
×
5997
      break;
×
5998
    case XNODE_JOB:
×
5999
      pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_JOBS_STMT, (SNode**)&pStmt);
×
6000
      CHECK_MAKE_NODE(pStmt);
×
6001
      break;
×
6002
    default:
×
6003
      break;
×
6004
  }
6005
  return (SNode*)pStmt;
×
6006
_err:
×
6007
  return NULL;
×
6008
}
6009
// SNode* dropXnodeResourceWhere(SAstCreateContext* pCxt, EXnodeResourceType resourceType, SNode* pWhere) {
6010
//   CHECK_PARSER_STATUS(pCxt);
6011
//   SShowStmt* pStmt = NULL;
6012
//   switch (resourceType) {
6013
//     case XNODE_TASK:
6014
//       // pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_TASK_STMT, (SNode**)&pStmt);
6015
//       pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_TASKS_STMT, (SNode**)&pStmt);
6016
//       CHECK_MAKE_NODE(pStmt);
6017
//       break;
6018
//     case XNODE_AGENT:
6019
//       // pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_XNODE_AGENT_STMT, (SNode**)&pStmt);
6020
//       pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_AGENTS_STMT, (SNode**)&pStmt);
6021
//       CHECK_MAKE_NODE(pStmt);
6022
//       break;
6023
//     case XNODE_JOB:
6024
//       pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_XNODE_JOBS_STMT, (SNode**)&pStmt);
6025
//       CHECK_MAKE_NODE(pStmt);
6026
//       break;
6027
//     default:
6028
//       break;
6029
//   }
6030
//   return (SNode*)pStmt;
6031
// _err:
6032
//   return NULL;
6033
// }
6034
SNode* createDefaultXnodeTaskOptions(SAstCreateContext* pCxt) {
×
6035
  CHECK_PARSER_STATUS(pCxt);
×
6036
  SXnodeTaskOptions* pOptions = NULL;
×
6037
  pCxt->errCode = nodesMakeNode(QUERY_NODE_XNODE_TASK_OPTIONS, (SNode**)&pOptions);
×
6038
  CHECK_MAKE_NODE(pOptions);
×
6039
  return (SNode*)pOptions;
×
6040
_err:
×
6041
  return NULL;
×
6042
}
6043

6044
static char   TRIGGER[8] = "trigger";
6045
static SToken TRIGGER_TOKEN = {
6046
    .n = 7,
6047
    .type = TK_NK_ID,
6048
    .z = TRIGGER,
6049
};
6050
SToken* createTriggerToken() { return &TRIGGER_TOKEN; }
×
6051

6052
SNode*  setXnodeTaskOption(SAstCreateContext* pCxt, SNode* pTaskOptions, SToken* pKey, SToken* pVal) {
×
6053
  CHECK_PARSER_STATUS(pCxt);
×
6054
  if (pTaskOptions == NULL) {
×
6055
    pTaskOptions = createDefaultXnodeTaskOptions(pCxt);
×
6056
    if (pTaskOptions == NULL) {
×
6057
      pCxt->errCode =
×
6058
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Xnode task options should not be NULL");
×
6059
      goto _err;
×
6060
    }
6061
  }
6062
  SXnodeTaskOptions* pOptions = (SXnodeTaskOptions*)pTaskOptions;
×
6063
  char               key[TSDB_COL_NAME_LEN] = {0};
×
6064
  if (pKey == NULL) {
×
6065
    pCxt->errCode =
×
6066
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
6067
    goto _err;
×
6068
  }
6069
  TRIM_STRING_FORM_ID_TOKEN(key, pKey);
×
6070

6071
  if (strlen(key) == 0) {
×
6072
    pCxt->errCode =
×
6073
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option name for xnode should not be empty");
×
6074
    goto _err;
×
6075
  }
6076
  char via[TSDB_COL_NAME_LEN] = {0};
×
6077
  char buf[TSDB_XNODE_TASK_OPTIONS_MAX_NUM] = {0};
×
6078
  if (strcmp(key, "trigger") == 0) {
×
6079
    if (pVal->type == TK_NK_STRING) {
×
6080
      (void)trimString(pVal->z, pVal->n, pOptions->trigger, sizeof(pOptions->trigger));
×
6081
      pOptions->triggerLen = pVal->n == 2 ? 1 : pVal->n - 2;
×
6082
    } else {
6083
      pCxt->errCode =
×
6084
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option trigger must be string");
×
6085
      goto _err;
×
6086
    }
6087
  } else if (strcmp(key, "parser") == 0 || strcmp(key, "transform") == 0) {
×
6088
    if (pVal->type == TK_NK_STRING) {
×
6089
      (void)trimString(pVal->z, pVal->n, pOptions->parser, sizeof(pOptions->parser));
×
6090
      pOptions->parserLen = pVal->n == 2 ? 1 : pVal->n - 2;
×
6091
    } else {
6092
      pCxt->errCode =
×
6093
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option parser must be string");
×
6094
      goto _err;
×
6095
    }
6096
  } else if (strcmp(key, "health") == 0) {
×
6097
    if (pVal->type == TK_NK_STRING) {
×
6098
      (void)trimString(pVal->z, pVal->n, pOptions->health, sizeof(pOptions->health));
×
6099
      pOptions->healthLen = pVal->n == 2 ? 1 : pVal->n - 2;
×
6100
    } else {
6101
      pCxt->errCode =
×
6102
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Option health must be string");
×
6103
      goto _err;
×
6104
    }
6105
  } else if (strcmp(key, "via") == 0) {
×
6106
    switch (pVal->type) {
×
6107
      case TK_NK_STRING:
×
6108
        (void)trimString(pVal->z, pVal->n, via, sizeof(via));
×
6109
        pOptions->via = taosStr2Int32(via, NULL, 10);
×
6110
        if (pOptions->via <= 0) {
×
6111
          pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6112
                                                   "Invalid xnode task option via: %s", pVal->z);
6113
          goto _err;
×
6114
        }
6115
        break;
×
6116
      case TK_NK_INTEGER:
×
6117
        pOptions->via = taosStr2Int32(pVal->z, NULL, 10);
×
6118
        break;
×
6119
      default:
×
6120
        pCxt->errCode =
×
6121
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid xnode task option: %s", key);
×
6122
    }
6123
  } else {
6124
    if (pOptions->optionsNum < TSDB_XNODE_TASK_OPTIONS_MAX_NUM) {
×
6125
      char* pKeyVal = NULL;
×
6126
      if (pVal != NULL) {
×
6127
        parserDebug("key value length expected: %d, actual: %d\n", pKey->n + pVal->n + 2, pKey->n + pVal->n);
×
6128
        pKeyVal = taosMemoryMalloc(pKey->n + pVal->n + 2);
×
6129
        memset(pKeyVal, 0, pKey->n + pVal->n + 2);
×
6130

6131
        CHECK_OUT_OF_MEM(pKeyVal);
×
6132
        size_t pos = strlen(key);
×
6133
        memcpy(pKeyVal, key, pos);
×
6134
        pKeyVal[pos] = '=';  // Add '=' after the key
×
6135
        pos++;
×
6136

6137
        if (pVal->type == TK_NK_STRING) {
×
6138
          (void)trimString(pVal->z, pVal->n, pKeyVal + pos, pVal->n + 1);
×
6139
        } else {
6140
          strncpy(pKeyVal + pos, pVal->z, TMIN(pVal->n, pKey->n + pVal->n + 2 - pos - 1));
×
6141
          pKeyVal[pos + pVal->n] = '\0';
×
6142
        }
6143
      } else {
6144
        size_t keyLen = strlen(key);
×
6145
        pKeyVal = taosMemoryMalloc(keyLen + 1);
×
6146
        memset(pKeyVal, 0, keyLen + 1);
×
6147
        CHECK_OUT_OF_MEM(pKeyVal);
×
6148
        memcpy(pKeyVal, key, keyLen);
×
6149
      }
6150
      pOptions->options[pOptions->optionsNum] = pKeyVal;
×
6151
      pOptions->optionsNum++;
×
6152
    } else {
6153
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6154
                                               "reaches max options number(%d) %s", pOptions->optionsNum, key);
6155
      goto _err;
×
6156
    }
6157
  }
6158
_err:
×
6159
  return pTaskOptions;
×
6160
}
6161

6162
SNode* createXnodeTaskJobWithOptions(SAstCreateContext* pCxt, EXnodeResourceType resourceType, const SToken* pTidToken,
×
6163
                                     SNode* pNodeOptions) {
6164
  CHECK_PARSER_STATUS(pCxt);
×
6165
  SNode* pStmt = NULL;
×
6166

6167
  switch (resourceType) {
×
6168
    case XNODE_JOB: {
×
6169
      if (pTidToken == NULL || pTidToken->n <= 0) {
×
6170
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6171
                                                "Xnode job task id should not be NULL or empty");
6172
        goto _err;
×
6173
      }
6174
      if (pNodeOptions == NULL || nodeType(pNodeOptions) != QUERY_NODE_XNODE_TASK_OPTIONS) {
×
6175
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6176
                                                "Xnode job options should not be NULL or empty");
6177
        goto _err;
×
6178
      }
6179
      pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_XNODE_JOB_STMT, &pStmt);
×
6180
      CHECK_MAKE_NODE(pStmt);
×
6181
      SCreateXnodeJobStmt* pJobStmt = (SCreateXnodeJobStmt*)pStmt;
×
6182
      pJobStmt->options = (SXnodeTaskOptions*)pNodeOptions;
×
6183
      pJobStmt->tid = pTidToken->type == TK_NK_STRING ? atoi(pTidToken->z) : taosStr2Int32(pTidToken->z, NULL, 10);
×
6184
      break;
×
6185
    }
6186
    default:
×
6187
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6188
                                              "Invalid xnode resource type: %d with ON clause", resourceType);
6189
      goto _err;
×
6190
  }
6191
  return pStmt;
×
6192
_err:
×
6193
  if (pStmt != NULL) {
×
6194
    nodesDestroyNode(pStmt);
×
6195
  }
6196
  return NULL;
×
6197
}
6198

6199
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
160✔
6200
  SToken config;
160✔
6201
  config.type = TK_NK_STRING;
160✔
6202
  config.z = "\"encrypt_key\"";
160✔
6203
  config.n = strlen(config.z);
160✔
6204
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
160✔
6205
}
6206

6207
SNode* createAlterEncryptKeyStmt(SAstCreateContext* pCxt, int8_t keyType, const SToken* pValue) {
×
6208
  CHECK_PARSER_STATUS(pCxt);
×
6209
  SAlterEncryptKeyStmt* pStmt = NULL;
×
6210
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_ENCRYPT_KEY_STMT, (SNode**)&pStmt);
×
6211
  CHECK_MAKE_NODE(pStmt);
×
6212

6213
  pStmt->keyType = keyType;
×
6214
  if (NULL != pValue) {
×
6215
    (void)trimString(pValue->z, pValue->n, pStmt->newKey, sizeof(pStmt->newKey));
×
6216
  }
6217

6218
  return (SNode*)pStmt;
×
6219
_err:
×
6220
  return NULL;
×
6221
}
6222

6223
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
3,228✔
6224
  if (!checkIndexName(pCxt, pIndexName)) {
3,228✔
6225
    return NULL;
×
6226
  }
6227
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
3,228✔
6228
}
6229

6230
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
11,553✔
6231
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
6232
  CHECK_PARSER_STATUS(pCxt);
11,553✔
6233
  SCreateIndexStmt* pStmt = NULL;
11,553✔
6234
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
11,553✔
6235
  CHECK_MAKE_NODE(pStmt);
11,553✔
6236
  pStmt->indexType = type;
11,553✔
6237
  pStmt->ignoreExists = ignoreExists;
11,553✔
6238

6239
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
11,553✔
6240
  if (strlen(pFullTable->table.dbName) == 0) {
11,553✔
6241
    // no db specified,
6242
    if (pCxt->pQueryCxt->db == NULL) {
×
6243
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
6244
      CHECK_PARSER_STATUS(pCxt);
×
6245
    } else {
6246
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
6247
    }
6248
  } else {
6249
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
11,553✔
6250
  }
6251
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
11,553✔
6252
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
11,553✔
6253
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
11,553✔
6254
  nodesDestroyNode(pIndexName);
11,553✔
6255
  nodesDestroyNode(pRealTable);
11,553✔
6256
  pStmt->pCols = pCols;
11,553✔
6257
  pStmt->pOptions = (SIndexOptions*)pOptions;
11,553✔
6258
  return (SNode*)pStmt;
11,553✔
6259
_err:
×
6260
  nodesDestroyNode(pIndexName);
×
6261
  nodesDestroyNode(pRealTable);
×
6262
  nodesDestroyNode(pOptions);
×
6263
  nodesDestroyList(pCols);
×
6264
  return NULL;
×
6265
}
6266

6267
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
×
6268
                         SNode* pStreamOptions) {
6269
  CHECK_PARSER_STATUS(pCxt);
×
6270
  SIndexOptions* pOptions = NULL;
×
6271
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
×
6272
  CHECK_MAKE_NODE(pOptions);
×
6273
  pOptions->pFuncs = pFuncs;
×
6274
  pOptions->pInterval = pInterval;
×
6275
  pOptions->pOffset = pOffset;
×
6276
  pOptions->pSliding = pSliding;
×
6277
  pOptions->pStreamOptions = pStreamOptions;
×
6278
  return (SNode*)pOptions;
×
6279
_err:
×
6280
  nodesDestroyNode(pInterval);
×
6281
  nodesDestroyNode(pOffset);
×
6282
  nodesDestroyNode(pSliding);
×
6283
  nodesDestroyNode(pStreamOptions);
×
6284
  return NULL;
×
6285
}
6286

6287
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
3,228✔
6288
  CHECK_PARSER_STATUS(pCxt);
3,228✔
6289
  SDropIndexStmt* pStmt = NULL;
3,228✔
6290
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
3,228✔
6291
  CHECK_MAKE_NODE(pStmt);
3,228✔
6292
  pStmt->ignoreNotExists = ignoreNotExists;
3,228✔
6293
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
3,228✔
6294
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
3,228✔
6295
  nodesDestroyNode(pIndexName);
3,228✔
6296
  return (SNode*)pStmt;
3,228✔
6297
_err:
×
6298
  nodesDestroyNode(pIndexName);
×
6299
  return NULL;
×
6300
}
6301

6302
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
91,404✔
6303
  CHECK_PARSER_STATUS(pCxt);
91,404✔
6304
  SCreateComponentNodeStmt* pStmt = NULL;
91,404✔
6305
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
91,404✔
6306
  CHECK_MAKE_NODE(pStmt);
91,404✔
6307
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
91,404✔
6308
  return (SNode*)pStmt;
91,404✔
6309
_err:
×
6310
  return NULL;
×
6311
}
6312

6313
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
47,822✔
6314
  CHECK_PARSER_STATUS(pCxt);
47,822✔
6315
  SDropComponentNodeStmt* pStmt = NULL;
47,822✔
6316
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
47,822✔
6317
  CHECK_MAKE_NODE(pStmt);
47,822✔
6318
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
47,822✔
6319
  return (SNode*)pStmt;
47,822✔
6320
_err:
×
6321
  return NULL;
×
6322
}
6323

6324
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
1,867✔
6325
  CHECK_PARSER_STATUS(pCxt);
1,867✔
6326
  SRestoreComponentNodeStmt* pStmt = NULL;
1,867✔
6327
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,867✔
6328
  CHECK_MAKE_NODE(pStmt);
1,867✔
6329
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1,867✔
6330
  return (SNode*)pStmt;
1,867✔
6331
_err:
×
6332
  return NULL;
×
6333
}
6334

6335
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery, bool reload) {
130,942✔
6336
  CHECK_PARSER_STATUS(pCxt);
130,942✔
6337
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
130,942✔
6338
  SCreateTopicStmt* pStmt = NULL;
130,429✔
6339
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
130,429✔
6340
  CHECK_MAKE_NODE(pStmt);
130,429✔
6341
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
130,429✔
6342
  pStmt->ignoreExists = ignoreExists;
130,429✔
6343
  pStmt->pQuery = pQuery;
130,429✔
6344
  pStmt->reload = reload;
130,429✔
6345
  return (SNode*)pStmt;
130,429✔
6346
_err:
513✔
6347
  nodesDestroyNode(pQuery);
513✔
6348
  return NULL;
513✔
6349
}
6350

6351
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
23,624✔
6352
                                  int8_t withMeta) {
6353
  CHECK_PARSER_STATUS(pCxt);
23,624✔
6354
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
23,624✔
6355
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
23,624✔
6356
  SCreateTopicStmt* pStmt = NULL;
23,624✔
6357
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
23,624✔
6358
  CHECK_MAKE_NODE(pStmt);
23,624✔
6359
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
23,624✔
6360
  pStmt->ignoreExists = ignoreExists;
23,624✔
6361
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
23,624✔
6362
  pStmt->withMeta = withMeta;
23,624✔
6363
  return (SNode*)pStmt;
23,624✔
6364
_err:
×
6365
  return NULL;
×
6366
}
6367

6368
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
10,867✔
6369
                                     int8_t withMeta, SNode* pWhere) {
6370
  CHECK_PARSER_STATUS(pCxt);
10,867✔
6371
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
10,867✔
6372
  SCreateTopicStmt* pStmt = NULL;
10,867✔
6373
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
10,867✔
6374
  CHECK_MAKE_NODE(pStmt);
10,867✔
6375
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
10,867✔
6376
  pStmt->ignoreExists = ignoreExists;
10,867✔
6377
  pStmt->withMeta = withMeta;
10,867✔
6378
  pStmt->pWhere = pWhere;
10,867✔
6379

6380
  tstrncpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
10,867✔
6381
  tstrncpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
10,867✔
6382
  nodesDestroyNode(pRealTable);
10,867✔
6383
  return (SNode*)pStmt;
10,867✔
6384
_err:
×
6385
  nodesDestroyNode(pRealTable);
×
6386
  nodesDestroyNode(pWhere);
×
6387
  return NULL;
×
6388
}
6389

6390
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName, bool force) {
104,601✔
6391
  CHECK_PARSER_STATUS(pCxt);
104,601✔
6392
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
104,601✔
6393
  SDropTopicStmt* pStmt = NULL;
104,425✔
6394
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
104,425✔
6395
  CHECK_MAKE_NODE(pStmt);
104,425✔
6396
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
104,425✔
6397
  pStmt->ignoreNotExists = ignoreNotExists;
104,425✔
6398
  pStmt->force = force;
104,425✔
6399
  return (SNode*)pStmt;
104,425✔
6400
_err:
176✔
6401
  return NULL;
176✔
6402
}
6403

6404
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName,
810✔
6405
                            bool force) {
6406
  CHECK_PARSER_STATUS(pCxt);
810✔
6407
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
810✔
6408
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
810✔
6409
  SDropCGroupStmt* pStmt = NULL;
810✔
6410
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
810✔
6411
  CHECK_MAKE_NODE(pStmt);
810✔
6412
  pStmt->ignoreNotExists = ignoreNotExists;
810✔
6413
  pStmt->force = force;
810✔
6414
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
810✔
6415
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
810✔
6416
  return (SNode*)pStmt;
810✔
6417
_err:
×
6418
  return NULL;
×
6419
}
6420

6421
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
882✔
6422
  CHECK_PARSER_STATUS(pCxt);
882✔
6423
  SAlterClusterStmt* pStmt = NULL;
882✔
6424
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
882✔
6425
  CHECK_MAKE_NODE(pStmt);
882✔
6426
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
882✔
6427
  if (NULL != pValue) {
882✔
6428
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
882✔
6429
  }
6430
  return (SNode*)pStmt;
882✔
6431
_err:
×
6432
  return NULL;
×
6433
}
6434

6435
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
227,726✔
6436
  CHECK_PARSER_STATUS(pCxt);
227,726✔
6437
  SAlterLocalStmt* pStmt = NULL;
227,726✔
6438
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
227,726✔
6439
  CHECK_MAKE_NODE(pStmt);
227,726✔
6440
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
227,726✔
6441
  if (NULL != pValue) {
227,726✔
6442
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
222,562✔
6443
  }
6444
  return (SNode*)pStmt;
227,726✔
6445
_err:
×
6446
  return NULL;
×
6447
}
6448

6449
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
12,704,819✔
6450
  CHECK_PARSER_STATUS(pCxt);
12,704,819✔
6451
  SExplainOptions* pOptions = NULL;
12,704,819✔
6452
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
12,704,819✔
6453
  CHECK_MAKE_NODE(pOptions);
12,704,819✔
6454
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
12,704,819✔
6455
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
12,704,819✔
6456
  return (SNode*)pOptions;
12,704,819✔
6457
_err:
×
6458
  return NULL;
×
6459
}
6460

6461
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
4,461,362✔
6462
  CHECK_PARSER_STATUS(pCxt);
4,461,362✔
6463
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
4,461,362✔
6464
  return pOptions;
4,461,362✔
6465
_err:
×
6466
  return NULL;
×
6467
}
6468

6469
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
1,490,220✔
6470
  CHECK_PARSER_STATUS(pCxt);
1,490,220✔
6471
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1,490,220✔
6472
  return pOptions;
1,490,220✔
6473
_err:
×
6474
  return NULL;
×
6475
}
6476

6477
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
12,534,236✔
6478
  CHECK_PARSER_STATUS(pCxt);
12,534,236✔
6479
  SExplainStmt* pStmt = NULL;
12,534,236✔
6480
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
12,534,236✔
6481
  CHECK_MAKE_NODE(pStmt);
12,534,236✔
6482
  pStmt->analyze = analyze;
12,534,236✔
6483
  pStmt->pOptions = (SExplainOptions*)pOptions;
12,534,236✔
6484
  pStmt->pQuery = pQuery;
12,534,236✔
6485
  return (SNode*)pStmt;
12,534,236✔
6486
_err:
×
6487
  nodesDestroyNode(pOptions);
×
6488
  nodesDestroyNode(pQuery);
×
6489
  return NULL;
×
6490
}
6491

6492
SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
484,729✔
6493
  CHECK_PARSER_STATUS(pCxt);
484,729✔
6494
  SDescribeStmt* pStmt = NULL;
485,460✔
6495
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT, (SNode**)&pStmt);
485,460✔
6496
  CHECK_MAKE_NODE(pStmt);
485,460✔
6497
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
485,460✔
6498
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
485,460✔
6499
  nodesDestroyNode(pRealTable);
485,460✔
6500
  return (SNode*)pStmt;
485,460✔
6501
_err:
×
6502
  nodesDestroyNode(pRealTable);
×
6503
  return NULL;
×
6504
}
6505

6506
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
1,963,142✔
6507
  CHECK_PARSER_STATUS(pCxt);
1,963,142✔
6508
  SNode* pStmt = NULL;
1,963,142✔
6509
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
1,963,142✔
6510
  CHECK_MAKE_NODE(pStmt);
1,963,142✔
6511
  return pStmt;
1,963,142✔
6512
_err:
×
6513
  return NULL;
×
6514
}
6515

6516
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
47,457✔
6517
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
47,457✔
6518
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
44,118✔
6519
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
3,339✔
6520
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
3,339✔
6521
  } else {
6522
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6523
                                            "udf programming language supports c and python");
6524
  }
6525
  return pCxt->errCode;
47,457✔
6526
}
6527

6528
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
47,457✔
6529
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
6530
                                bool orReplace) {
6531
  CHECK_PARSER_STATUS(pCxt);
47,457✔
6532
  if (pLibPath->n <= 2) {
47,457✔
6533
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
6534
    CHECK_PARSER_STATUS(pCxt);
×
6535
  }
6536
  int8_t language = 0;
47,457✔
6537
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
47,457✔
6538
  CHECK_PARSER_STATUS(pCxt);
47,457✔
6539
  SCreateFunctionStmt* pStmt = NULL;
47,457✔
6540
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
47,457✔
6541
  CHECK_MAKE_NODE(pStmt);
47,457✔
6542
  pStmt->orReplace = orReplace;
47,457✔
6543
  pStmt->ignoreExists = ignoreExists;
47,457✔
6544
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
47,457✔
6545
  pStmt->isAgg = aggFunc;
47,457✔
6546
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
47,457✔
6547
  pStmt->outputDt = dataType;
47,457✔
6548
  pStmt->bufSize = bufSize;
47,457✔
6549
  pStmt->language = language;
47,457✔
6550
  return (SNode*)pStmt;
47,457✔
6551
_err:
×
6552
  return NULL;
×
6553
}
6554

6555
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
36,792✔
6556
  CHECK_PARSER_STATUS(pCxt);
36,792✔
6557
  SDropFunctionStmt* pStmt = NULL;
36,792✔
6558
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
36,792✔
6559
  CHECK_MAKE_NODE(pStmt);
36,792✔
6560
  pStmt->ignoreNotExists = ignoreNotExists;
36,792✔
6561
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
36,792✔
6562
  return (SNode*)pStmt;
36,792✔
6563
_err:
×
6564
  return NULL;
×
6565
}
6566

6567
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
17,649✔
6568
  SCreateViewStmt* pStmt = NULL;
17,649✔
6569
  CHECK_PARSER_STATUS(pCxt);
17,649✔
6570
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
17,649✔
6571
  CHECK_MAKE_NODE(pStmt);
17,649✔
6572
  int32_t i = pAs->n;
17,649✔
6573
  while (isspace(*(pAs->z + i))) {
35,298✔
6574
    ++i;
17,649✔
6575
  }
6576
  pStmt->pQuerySql = tstrdup(pAs->z + i);
17,649✔
6577
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
17,649✔
6578
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
17,649✔
6579
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
17,649✔
6580
  nodesDestroyNode(pView);
17,649✔
6581
  pStmt->orReplace = orReplace;
17,649✔
6582
  pStmt->pQuery = pQuery;
17,649✔
6583
  return (SNode*)pStmt;
17,649✔
6584
_err:
×
6585
  nodesDestroyNode(pView);
×
6586
  nodesDestroyNode(pQuery);
×
6587
  nodesDestroyNode((SNode*)pStmt);
×
6588
  return NULL;
×
6589
}
6590

6591
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
352✔
6592
  CHECK_PARSER_STATUS(pCxt);
352✔
6593
  SDropViewStmt* pStmt = NULL;
×
6594
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
×
6595
  CHECK_MAKE_NODE(pStmt);
×
6596
  pStmt->ignoreNotExists = ignoreNotExists;
×
6597
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
×
6598
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
×
6599
  nodesDestroyNode(pView);
×
6600
  return (SNode*)pStmt;
×
6601
_err:
352✔
6602
  nodesDestroyNode(pView);
352✔
6603
  return NULL;
352✔
6604
}
6605

6606
SNode* createStreamOutTableNode(SAstCreateContext* pCxt, SNode* pIntoTable, SNode* pOutputSubTable, SNodeList* pColList,
253,958✔
6607
                                SNodeList* pTagList) {
6608
  SStreamOutTableNode* pOutTable = NULL;
253,958✔
6609
  CHECK_PARSER_STATUS(pCxt);
253,958✔
6610
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OUT_TABLE, (SNode**)&pOutTable);
253,782✔
6611
  CHECK_MAKE_NODE(pOutTable);
253,782✔
6612
  pOutTable->pOutTable = pIntoTable;
253,782✔
6613
  pOutTable->pSubtable = pOutputSubTable;
253,782✔
6614
  pOutTable->pCols = pColList;
253,782✔
6615
  pOutTable->pTags = pTagList;
253,782✔
6616
  return (SNode*)pOutTable;
253,782✔
6617

6618
_err:
176✔
6619
  nodesDestroyNode((SNode*)pOutTable);
176✔
6620
  nodesDestroyNode(pIntoTable);
176✔
6621
  nodesDestroyNode(pOutputSubTable);
176✔
6622
  nodesDestroyList(pColList);
176✔
6623
  nodesDestroyList(pTagList);
176✔
6624
  return NULL;
176✔
6625
}
6626

6627
SNode* createStreamTriggerNode(SAstCreateContext* pCxt, SNode* pTriggerWindow, SNode* pTriggerTable,
258,268✔
6628
                               SNodeList* pPartitionList, SNode* pOptions, SNode* pNotification) {
6629
  SStreamTriggerNode* pTrigger = NULL;
258,268✔
6630
  CHECK_PARSER_STATUS(pCxt);
258,268✔
6631
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER, (SNode**)&pTrigger);
258,194✔
6632
  CHECK_MAKE_NODE(pTrigger);
258,194✔
6633

6634
  pTrigger->pOptions = pOptions;
258,194✔
6635
  pTrigger->pNotify = pNotification;
258,194✔
6636
  pTrigger->pTrigerTable = pTriggerTable;
258,194✔
6637
  pTrigger->pPartitionList = pPartitionList;
258,194✔
6638
  pTrigger->pTriggerWindow = pTriggerWindow;
258,194✔
6639
  return (SNode*)pTrigger;
258,194✔
6640

6641
_err:
74✔
6642
  nodesDestroyNode((SNode*)pTrigger);
74✔
6643
  nodesDestroyNode(pTriggerWindow);
74✔
6644
  nodesDestroyNode(pTriggerTable);
74✔
6645
  nodesDestroyNode(pOptions);
74✔
6646
  nodesDestroyNode(pNotification);
74✔
6647
  nodesDestroyList(pPartitionList);
74✔
6648
  return NULL;
74✔
6649
}
6650

6651
SNode* createSlidingWindowNode(SAstCreateContext* pCxt, SNode* pSlidingVal, SNode* pOffset) {
101,667✔
6652
  SSlidingWindowNode* pSliding = NULL;
101,667✔
6653
  CHECK_PARSER_STATUS(pCxt);
101,667✔
6654
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SLIDING_WINDOW, (SNode**)&pSliding);
101,667✔
6655
  CHECK_MAKE_NODE(pSliding);
101,667✔
6656
  pSliding->pSlidingVal = pSlidingVal;
101,667✔
6657
  pSliding->pOffset = pOffset;
101,667✔
6658
  return (SNode*)pSliding;
101,667✔
6659
_err:
×
6660
  nodesDestroyNode(pSlidingVal);
×
6661
  nodesDestroyNode(pOffset);
×
6662
  nodesDestroyNode((SNode*)pSliding);
×
6663
  return NULL;
×
6664
}
6665

6666
SNode* createStreamTriggerOptions(SAstCreateContext* pCxt) {
106,998✔
6667
  SStreamTriggerOptions* pOptions = NULL;
106,998✔
6668
  CHECK_PARSER_STATUS(pCxt);
106,998✔
6669
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER_OPTIONS, (SNode**)&pOptions);
106,998✔
6670
  CHECK_MAKE_NODE(pOptions);
106,998✔
6671
  pOptions->pPreFilter = NULL;
106,998✔
6672
  pOptions->pWaterMark = NULL;
106,998✔
6673
  pOptions->pMaxDelay = NULL;
106,998✔
6674
  pOptions->pExpiredTime = NULL;
106,998✔
6675
  pOptions->pFillHisStartTime = NULL;
106,998✔
6676
  pOptions->pEventType = EVENT_NONE;
106,998✔
6677
  pOptions->calcNotifyOnly = false;
106,998✔
6678
  pOptions->deleteOutputTable = false;
106,998✔
6679
  pOptions->deleteRecalc = false;
106,998✔
6680
  pOptions->fillHistory = false;
106,998✔
6681
  pOptions->fillHistoryFirst = false;
106,998✔
6682
  pOptions->lowLatencyCalc = false;
106,998✔
6683
  pOptions->forceOutput = false;
106,998✔
6684
  pOptions->ignoreDisorder = false;
106,998✔
6685
  pOptions->ignoreNoDataTrigger = false;
106,998✔
6686
  return (SNode*)pOptions;
106,998✔
6687
_err:
×
6688
  nodesDestroyNode((SNode*)pOptions);
×
6689
  return NULL;
×
6690
}
6691

6692
SNode* createStreamTagDefNode(SAstCreateContext* pCxt, SToken* pTagName, SDataType dataType, SNode* tagExpression) {
28,329✔
6693
  SStreamTagDefNode* pTagDef = NULL;
28,329✔
6694
  CHECK_PARSER_STATUS(pCxt);
28,329✔
6695
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TAG_DEF, (SNode**)&pTagDef);
28,329✔
6696
  CHECK_MAKE_NODE(pTagDef);
28,329✔
6697
  COPY_STRING_FORM_ID_TOKEN(pTagDef->tagName, pTagName);
28,329✔
6698
  int32_t nameLen = strdequote(pTagDef->tagName);
28,329✔
6699
  pTagDef->tagName[nameLen] = '\0';
28,329✔
6700
  pTagDef->dataType = dataType;
28,329✔
6701
  pTagDef->pTagExpr = tagExpression;
28,329✔
6702
  return (SNode*)pTagDef;
28,329✔
6703
_err:
×
6704
  nodesDestroyNode(tagExpression);
×
6705
  nodesDestroyNode((SNode*)pTagDef);
×
6706
  return NULL;
×
6707
}
6708

6709
SNode* setStreamTriggerOptions(SAstCreateContext* pCxt, SNode* pOptions, SStreamTriggerOption* pOptionUnit) {
135,925✔
6710
  CHECK_PARSER_STATUS(pCxt);
135,925✔
6711
  SStreamTriggerOptions* pStreamOptions = (SStreamTriggerOptions*)pOptions;
135,925✔
6712
  switch (pOptionUnit->type) {
135,925✔
6713
    case STREAM_TRIGGER_OPTION_CALC_NOTIFY_ONLY:
1,638✔
6714
      if (pStreamOptions->calcNotifyOnly) {
1,638✔
6715
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6716
                                                "CALC_NOTIFY_ONLY specified multiple times");
6717
        goto _err;
×
6718
      }
6719
      pStreamOptions->calcNotifyOnly = true;
1,638✔
6720
      break;
1,638✔
6721
    case STREAM_TRIGGER_OPTION_DELETE_OUTPUT_TABLE:
237✔
6722
      if (pStreamOptions->deleteOutputTable) {
237✔
6723
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6724
                                                "DELETE_OUTPUT_TABLE specified multiple times");
6725
        goto _err;
×
6726
      }
6727
      pStreamOptions->deleteOutputTable = true;
237✔
6728
      break;
237✔
6729
    case STREAM_TRIGGER_OPTION_DELETE_RECALC:
8,990✔
6730
      if (pStreamOptions->deleteRecalc) {
8,990✔
6731
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6732
                                                "DELETE_RECALC specified multiple times");
6733
        goto _err;
×
6734
      }
6735
      pStreamOptions->deleteRecalc = true;
8,990✔
6736
      break;
8,990✔
6737
    case STREAM_TRIGGER_OPTION_EXPIRED_TIME:
5,894✔
6738
      if (pStreamOptions->pExpiredTime != NULL) {
5,894✔
6739
        pCxt->errCode =
×
6740
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EXPIRED_TIME specified multiple times");
×
6741
        goto _err;
×
6742
      }
6743
      pStreamOptions->pExpiredTime = pOptionUnit->pNode;
5,894✔
6744
      break;
5,894✔
6745
    case STREAM_TRIGGER_OPTION_FORCE_OUTPUT:
3,769✔
6746
      if (pStreamOptions->forceOutput) {
3,769✔
6747
        pCxt->errCode =
×
6748
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FORCE_OUTPUT specified multiple times");
×
6749
        goto _err;
×
6750
      }
6751
      pStreamOptions->forceOutput = true;
3,769✔
6752
      break;
3,769✔
6753
    case STREAM_TRIGGER_OPTION_FILL_HISTORY:
34,190✔
6754
      if (pStreamOptions->fillHistoryFirst) {
34,190✔
6755
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6756
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
6757
        goto _err;
×
6758
      }
6759
      if (pStreamOptions->pFillHisStartTime != NULL) {
34,190✔
6760
        pCxt->errCode =
×
6761
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FILL_HISTORY specified multiple times");
×
6762
        goto _err;
×
6763
      }
6764
      pStreamOptions->fillHistory = true;
34,190✔
6765
      if (pOptionUnit->pNode == NULL) {
34,190✔
6766
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
17,650✔
6767
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
17,650✔
6768
      } else {
6769
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
16,540✔
6770
      }
6771
      break;
34,190✔
6772
    case STREAM_TRIGGER_OPTION_FILL_HISTORY_FIRST:
2,708✔
6773
      if (pStreamOptions->fillHistory) {
2,708✔
6774
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
432✔
6775
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
6776
        goto _err;
432✔
6777
      }
6778
      if (pStreamOptions->pFillHisStartTime != NULL) {
2,276✔
6779
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6780
                                                "FILL_HISTORY_FIRST specified multiple times");
6781
        goto _err;
×
6782
      }
6783
      pStreamOptions->fillHistoryFirst = true;
2,276✔
6784
      if (pOptionUnit->pNode == NULL) {
2,276✔
6785
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
432✔
6786
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
432✔
6787
      } else {
6788
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
1,844✔
6789
      }
6790
      break;
2,276✔
6791
    case STREAM_TRIGGER_OPTION_IGNORE_DISORDER:
17,912✔
6792
      if (pStreamOptions->ignoreDisorder) {
17,912✔
6793
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6794
                                                "IGNORE_DISORDER specified multiple times");
6795
        goto _err;
×
6796
      }
6797
      pStreamOptions->ignoreDisorder = true;
17,912✔
6798
      break;
17,912✔
6799
    case STREAM_TRIGGER_OPTION_LOW_LATENCY_CALC:
6,149✔
6800
      if (pStreamOptions->lowLatencyCalc) {
6,149✔
6801
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6802
                                                "LOW_LATENCY_CALC specified multiple times");
6803
        goto _err;
×
6804
      }
6805
      pStreamOptions->lowLatencyCalc = true;
6,149✔
6806
      break;
6,149✔
6807
    case STREAM_TRIGGER_OPTION_MAX_DELAY:
6,169✔
6808
      if (pStreamOptions->pMaxDelay != NULL) {
6,169✔
6809
        pCxt->errCode =
×
6810
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "MAX_DELAY specified multiple times");
×
6811
        goto _err;
×
6812
      }
6813
      pStreamOptions->pMaxDelay = pOptionUnit->pNode;
6,169✔
6814
      break;
6,169✔
6815
    case STREAM_TRIGGER_OPTION_WATERMARK:
8,098✔
6816
      if (pStreamOptions->pWaterMark != NULL) {
8,098✔
6817
        pCxt->errCode =
×
6818
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "WATERMARK specified multiple times");
×
6819
        goto _err;
×
6820
      }
6821
      pStreamOptions->pWaterMark = pOptionUnit->pNode;
8,098✔
6822
      break;
8,098✔
6823
    case STREAM_TRIGGER_OPTION_PRE_FILTER:
16,380✔
6824
      if (pStreamOptions->pPreFilter != NULL) {
16,380✔
6825
        pCxt->errCode =
×
6826
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "PRE_FILTER specified multiple times");
×
6827
        goto _err;
×
6828
      }
6829
      pStreamOptions->pPreFilter = pOptionUnit->pNode;
16,380✔
6830
      break;
16,380✔
6831
    case STREAM_TRIGGER_OPTION_EVENT_TYPE:
5,560✔
6832
      if (pStreamOptions->pEventType != EVENT_NONE) {
5,560✔
6833
        pCxt->errCode =
×
6834
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EVENT_TYPE specified multiple times");
×
6835
        goto _err;
×
6836
      }
6837
      pStreamOptions->pEventType = pOptionUnit->flag;
5,560✔
6838
      break;
5,560✔
6839
    case STREAM_TRIGGER_OPTION_IGNORE_NODATA_TRIGGER:
18,231✔
6840
      if (pStreamOptions->ignoreNoDataTrigger) {
18,231✔
6841
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6842
                                                "IGNORE_NODATA_TRIGGER specified multiple times");
6843
        goto _err;
×
6844
      }
6845
      pStreamOptions->ignoreNoDataTrigger = true;
18,231✔
6846
      break;
18,231✔
6847
    default:
×
6848
      break;
×
6849
  }
6850
  return pOptions;
135,493✔
6851
_err:
432✔
6852
  nodesDestroyNode(pOptionUnit->pNode);
432✔
6853
  nodesDestroyNode(pOptions);
432✔
6854
  return NULL;
432✔
6855
}
6856

6857
static bool validateNotifyUrl(const char* url) {
59,980✔
6858
  const char* prefix[] = {"ws://", "wss://"};
59,980✔
6859
  const char* host = NULL;
59,980✔
6860

6861
  if (!url || *url == '\0') return false;
59,980✔
6862

6863
  for (int32_t i = 0; i < ARRAY_SIZE(prefix); ++i) {
60,128✔
6864
    if (taosStrncasecmp(url, prefix[i], strlen(prefix[i])) == 0) {
60,054✔
6865
      host = url + strlen(prefix[i]);
59,906✔
6866
      break;
59,906✔
6867
    }
6868
  }
6869

6870
  return (host != NULL) && (*host != '\0') && (*host != '/');
59,980✔
6871
}
6872

6873
SNode* createStreamNotifyOptions(SAstCreateContext* pCxt, SNodeList* pAddrUrls, int64_t eventType, SNode* pWhere,
59,980✔
6874
                                 int64_t notifyType) {
6875
  SNode* pNode = NULL;
59,980✔
6876
  CHECK_PARSER_STATUS(pCxt);
59,980✔
6877

6878
  if (LIST_LENGTH(pAddrUrls) == 0) {
59,980✔
6879
    pCxt->errCode =
×
6880
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "notification address cannot be empty");
×
6881
    goto _err;
×
6882
  }
6883

6884
  FOREACH(pNode, pAddrUrls) {
119,886✔
6885
    char* url = ((SValueNode*)pNode)->literal;
59,980✔
6886
    if (strlen(url) >= TSDB_STREAM_NOTIFY_URL_LEN) {
59,980✔
6887
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
6888
                                              "notification address \"%s\" exceed maximum length %d", url,
6889
                                              TSDB_STREAM_NOTIFY_URL_LEN);
6890
      goto _err;
×
6891
    }
6892
    if (!validateNotifyUrl(url)) {
59,980✔
6893
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
74✔
6894
                                              "invalid notification address \"%s\"", url);
6895
      goto _err;
74✔
6896
    }
6897
  }
6898

6899
  SStreamNotifyOptions* pNotifyOptions = NULL;
59,906✔
6900
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_NOTIFY_OPTIONS, (SNode**)&pNotifyOptions);
59,906✔
6901
  CHECK_MAKE_NODE(pNotifyOptions);
59,906✔
6902
  pNotifyOptions->pAddrUrls = pAddrUrls;
59,906✔
6903
  pNotifyOptions->pWhere = pWhere;
59,906✔
6904
  pNotifyOptions->eventType = eventType;
59,906✔
6905
  pNotifyOptions->notifyType = notifyType;
59,906✔
6906
  return (SNode*)pNotifyOptions;
59,906✔
6907
_err:
74✔
6908
  nodesDestroyList(pAddrUrls);
74✔
6909
  return NULL;
74✔
6910
}
6911

6912
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pStream, SNode* pTrigger,
251,967✔
6913
                              SNode* pOutTable, SNode* pQuery) {
6914
  SCreateStreamStmt* pStmt = NULL;
251,967✔
6915
  CHECK_PARSER_STATUS(pCxt);
251,967✔
6916
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
251,615✔
6917
  CHECK_MAKE_NODE(pStmt);
251,615✔
6918

6919
  if (pOutTable && ((SStreamOutTableNode*)pOutTable)->pOutTable) {
251,615✔
6920
    tstrncpy(pStmt->targetDbName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.dbName,
248,486✔
6921
             TSDB_DB_NAME_LEN);
6922
    tstrncpy(pStmt->targetTabName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.tableName,
248,486✔
6923
             TSDB_TABLE_NAME_LEN);
6924
  }
6925

6926
  if (pStream) {
251,615✔
6927
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
251,615✔
6928
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
251,615✔
6929
  } else {
6930
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
6931
    goto _err;
×
6932
  }
6933
  nodesDestroyNode(pStream);
251,615✔
6934

6935
  pStmt->ignoreExists = ignoreExists;
251,615✔
6936
  pStmt->pTrigger = pTrigger;
251,615✔
6937
  pStmt->pQuery = pQuery;
251,615✔
6938
  pStmt->pTags = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pTags : NULL;
251,615✔
6939
  pStmt->pSubtable = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pSubtable : NULL;
251,615✔
6940
  pStmt->pCols = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pCols : NULL;
251,615✔
6941
  return (SNode*)pStmt;
251,615✔
6942
_err:
352✔
6943
  nodesDestroyNode(pOutTable);
352✔
6944
  nodesDestroyNode(pQuery);
352✔
6945
  nodesDestroyNode(pTrigger);
352✔
6946
  nodesDestroyNode(pQuery);
352✔
6947
  return NULL;
352✔
6948
}
6949

6950
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNodeList* pStreamList) {
7,946✔
6951
  CHECK_PARSER_STATUS(pCxt);
7,946✔
6952
  SDropStreamStmt* pStmt = NULL;
7,946✔
6953
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
7,946✔
6954
  CHECK_MAKE_NODE(pStmt);
7,946✔
6955

6956
  if (pStreamList) {
7,946✔
6957
    pStmt->pStreamList = pStreamList;
7,946✔
6958
  } else {
6959
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
6960
    goto _err;
×
6961
  }
6962

6963
  pStmt->ignoreNotExists = ignoreNotExists;
7,946✔
6964
  return (SNode*)pStmt;
7,946✔
6965
_err:
×
6966
  nodesDestroyList(pStreamList);
×
6967
  return NULL;
×
6968
}
6969

6970
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pStream) {
2,007✔
6971
  CHECK_PARSER_STATUS(pCxt);
2,007✔
6972
  SPauseStreamStmt* pStmt = NULL;
2,007✔
6973
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
2,007✔
6974
  CHECK_MAKE_NODE(pStmt);
2,007✔
6975
  if (pStream) {
2,007✔
6976
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
2,007✔
6977
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
2,007✔
6978
  } else {
6979
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
6980
    goto _err;
×
6981
  }
6982
  nodesDestroyNode(pStream);
2,007✔
6983
  pStmt->ignoreNotExists = ignoreNotExists;
2,007✔
6984
  return (SNode*)pStmt;
2,007✔
6985
_err:
×
6986
  return NULL;
×
6987
}
6988

6989
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated, SNode* pStream) {
2,007✔
6990
  CHECK_PARSER_STATUS(pCxt);
2,007✔
6991
  SResumeStreamStmt* pStmt = NULL;
2,007✔
6992
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
2,007✔
6993
  CHECK_MAKE_NODE(pStmt);
2,007✔
6994
  if (pStream) {
2,007✔
6995
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
2,007✔
6996
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
2,007✔
6997
  } else {
6998
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
6999
    goto _err;
×
7000
  }
7001
  nodesDestroyNode(pStream);
2,007✔
7002
  pStmt->ignoreNotExists = ignoreNotExists;
2,007✔
7003
  pStmt->ignoreUntreated = ignoreUntreated;
2,007✔
7004
  return (SNode*)pStmt;
2,007✔
7005
_err:
×
7006
  return NULL;
×
7007
}
7008

7009
SNode* createRecalcStreamStmt(SAstCreateContext* pCxt, SNode* pStream, SNode* pRange) {
10,475✔
7010
  CHECK_PARSER_STATUS(pCxt);
10,475✔
7011
  SRecalcStreamStmt* pStmt = NULL;
10,475✔
7012
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RECALCULATE_STREAM_STMT, (SNode**)&pStmt);
10,475✔
7013
  CHECK_MAKE_NODE(pStmt);
10,475✔
7014
  if (pStream) {
10,475✔
7015
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
10,475✔
7016
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
10,475✔
7017
  } else {
7018
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
7019
    goto _err;
×
7020
  }
7021
  pStmt->pRange = pRange;
10,475✔
7022
  return (SNode*)pStmt;
10,475✔
7023
_err:
×
7024
  return NULL;
×
7025
}
7026

7027
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
2,008✔
7028
  CHECK_PARSER_STATUS(pCxt);
2,008✔
7029
  SKillStmt* pStmt = NULL;
2,008✔
7030
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,008✔
7031
  CHECK_MAKE_NODE(pStmt);
2,008✔
7032
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
2,008✔
7033
  return (SNode*)pStmt;
2,008✔
7034
_err:
×
7035
  return NULL;
×
7036
}
7037

7038
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
45✔
7039
  CHECK_PARSER_STATUS(pCxt);
45✔
7040
  SKillQueryStmt* pStmt = NULL;
45✔
7041
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
45✔
7042
  CHECK_MAKE_NODE(pStmt);
45✔
7043
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
45✔
7044
  return (SNode*)pStmt;
45✔
7045
_err:
×
7046
  return NULL;
×
7047
}
7048

7049
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
11,441✔
7050
  CHECK_PARSER_STATUS(pCxt);
11,441✔
7051
  SBalanceVgroupStmt* pStmt = NULL;
11,441✔
7052
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
11,441✔
7053
  CHECK_MAKE_NODE(pStmt);
11,441✔
7054
  return (SNode*)pStmt;
11,441✔
7055
_err:
×
7056
  return NULL;
×
7057
}
7058

7059
SNode* createAssignLeaderStmt(SAstCreateContext* pCxt) {
16✔
7060
  CHECK_PARSER_STATUS(pCxt);
16✔
7061
  SAssignLeaderStmt* pStmt = NULL;
16✔
7062
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ASSIGN_LEADER_STMT, (SNode**)&pStmt);
16✔
7063
  CHECK_MAKE_NODE(pStmt);
16✔
7064
  return (SNode*)pStmt;
16✔
7065
_err:
×
7066
  return NULL;
×
7067
}
7068

7069
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
2,071✔
7070
  CHECK_PARSER_STATUS(pCxt);
2,071✔
7071
  SBalanceVgroupLeaderStmt* pStmt = NULL;
2,071✔
7072
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
2,071✔
7073
  CHECK_MAKE_NODE(pStmt);
2,071✔
7074
  if (NULL != pVgId && NULL != pVgId->z) {
2,071✔
7075
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
83✔
7076
  }
7077
  return (SNode*)pStmt;
2,071✔
7078
_err:
×
7079
  return NULL;
×
7080
}
7081

7082
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
106✔
7083
  CHECK_PARSER_STATUS(pCxt);
106✔
7084
  SBalanceVgroupLeaderStmt* pStmt = NULL;
106✔
7085
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
106✔
7086
  CHECK_MAKE_NODE(pStmt);
106✔
7087
  if (NULL != pDbName) {
106✔
7088
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
106✔
7089
  }
7090
  return (SNode*)pStmt;
106✔
7091
_err:
×
7092
  return NULL;
×
7093
}
7094

7095
SNode* createSetVgroupKeepVersionStmt(SAstCreateContext* pCxt, const SToken* pVgId, const SToken* pKeepVersion) {
1,063✔
7096
  CHECK_PARSER_STATUS(pCxt);
1,063✔
7097
  SSetVgroupKeepVersionStmt* pStmt = NULL;
1,063✔
7098
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_VGROUP_KEEP_VERSION_STMT, (SNode**)&pStmt);
1,063✔
7099
  CHECK_MAKE_NODE(pStmt);
1,063✔
7100
  if (NULL != pVgId && NULL != pVgId->z) {
1,063✔
7101
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
1,063✔
7102
  }
7103
  if (NULL != pKeepVersion && NULL != pKeepVersion->z) {
1,063✔
7104
    pStmt->keepVersion = taosStr2Int64(pKeepVersion->z, NULL, 10);
1,063✔
7105
  }
7106
  return (SNode*)pStmt;
1,063✔
7107
_err:
×
7108
  return NULL;
×
7109
}
7110

7111
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
×
7112
  CHECK_PARSER_STATUS(pCxt);
×
7113
  SMergeVgroupStmt* pStmt = NULL;
×
7114
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
×
7115
  CHECK_MAKE_NODE(pStmt);
×
7116
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
×
7117
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
×
7118
  return (SNode*)pStmt;
×
7119
_err:
×
7120
  return NULL;
×
7121
}
7122

7123
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
43,924✔
7124
  CHECK_PARSER_STATUS(pCxt);
43,924✔
7125
  SRedistributeVgroupStmt* pStmt = NULL;
43,924✔
7126
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
43,924✔
7127
  CHECK_MAKE_NODE(pStmt);
43,924✔
7128
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
43,924✔
7129
  pStmt->pDnodes = pDnodes;
43,924✔
7130
  return (SNode*)pStmt;
43,924✔
7131
_err:
×
7132
  nodesDestroyList(pDnodes);
×
7133
  return NULL;
×
7134
}
7135

7136
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, bool force) {
20,790✔
7137
  CHECK_PARSER_STATUS(pCxt);
20,790✔
7138
  SSplitVgroupStmt* pStmt = NULL;
20,790✔
7139
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
20,790✔
7140
  CHECK_MAKE_NODE(pStmt);
20,790✔
7141
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
20,790✔
7142
  pStmt->force = force;
20,790✔
7143
  return (SNode*)pStmt;
20,790✔
7144
_err:
×
7145
  return NULL;
×
7146
}
7147

7148
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
7149
  CHECK_PARSER_STATUS(pCxt);
×
7150
  SNode* pStmt = NULL;
×
7151
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
7152
  CHECK_MAKE_NODE(pStmt);
×
7153
  return pStmt;
×
7154
_err:
×
7155
  return NULL;
×
7156
}
7157

7158
SNode* createGrantStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
560,277✔
7159
                       SNode* pCond, int8_t optrType) {
7160
  CHECK_PARSER_STATUS(pCxt);
560,277✔
7161
  CHECK_NAME(checkRoleName(pCxt, pPrincipal, false));
560,277✔
7162
  SGrantStmt* pStmt = NULL;
560,277✔
7163
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
560,277✔
7164
  CHECK_MAKE_NODE(pStmt);
560,277✔
7165
  pStmt->optrType = optrType;
560,277✔
7166
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
560,277✔
7167
  switch (optrType) {
560,277✔
7168
    case TSDB_ALTER_ROLE_LOCK:
×
7169
      break;
×
7170
    case TSDB_ALTER_ROLE_PRIVILEGES: {
558,065✔
7171
      CHECK_NAME(checkObjName(pCxt, &pPrivLevel->first, false));
558,065✔
7172
      CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
558,065✔
7173
      pStmt->privileges = *(SPrivSetArgs*)resouces;
558,065✔
7174
      if (TK_NK_NIL != pPrivLevel->first.type) {
558,065✔
7175
        COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
557,102✔
7176
      }
7177
      if (TK_NK_NIL != pPrivLevel->second.type) {
558,065✔
7178
        COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
238,328✔
7179
      }
7180
      pStmt->privileges.objType = pPrivLevel->objType;
558,065✔
7181
      pStmt->pCond = pCond;
558,065✔
7182
      break;
558,065✔
7183
    }
7184
    case TSDB_ALTER_ROLE_ROLE: {
2,212✔
7185
      SToken* pRole = (SToken*)resouces;
2,212✔
7186
      CHECK_NAME(checkRoleName(pCxt, pRole, false));
2,212✔
7187
      COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
2,212✔
7188
      break;
2,212✔
7189
    }
7190
    default:
×
7191
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported grant type");
×
7192
      goto _err;
×
7193
  }
7194
  return (SNode*)pStmt;
560,277✔
7195
_err:
×
7196
  nodesDestroyNode(pCond);
×
7197
  return NULL;
×
7198
}
7199

7200
SNode* createRevokeStmt(SAstCreateContext* pCxt, void* resouces, SPrivLevelArgs* pPrivLevel, SToken* pPrincipal,
404,520✔
7201
                        SNode* pCond, int8_t optrType) {
7202
  CHECK_PARSER_STATUS(pCxt);
404,520✔
7203
  CHECK_NAME(checkUserName(pCxt, pPrincipal));
404,520✔
7204
  SRevokeStmt* pStmt = NULL;
404,520✔
7205
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
404,520✔
7206
  CHECK_MAKE_NODE(pStmt);
404,520✔
7207
  pStmt->optrType = optrType;
404,520✔
7208
  COPY_STRING_FORM_ID_TOKEN(pStmt->principal, pPrincipal);
404,520✔
7209
  if (optrType == TSDB_ALTER_ROLE_PRIVILEGES) {
404,520✔
7210
    CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
403,244✔
7211
    CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
403,244✔
7212
    pStmt->privileges = *(SPrivSetArgs*)resouces;
403,244✔
7213
    COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
403,244✔
7214
    if (TK_NK_NIL != pPrivLevel->second.type) {
403,244✔
7215
      COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
219,597✔
7216
    }
7217
    pStmt->privileges.objType = pPrivLevel->objType;
403,244✔
7218
    pStmt->pCond = pCond;
403,244✔
7219
  } else if (optrType == TSDB_ALTER_ROLE_ROLE) {
1,276✔
7220
    SToken* pRole = (SToken*)resouces;
1,276✔
7221
    CHECK_NAME(checkRoleName(pCxt, pRole, false));
1,276✔
7222
    COPY_STRING_FORM_ID_TOKEN(pStmt->roleName, pRole);
1,276✔
7223
  } else {
7224
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "unsupported revoke type");
×
7225
    goto _err;
×
7226
  }
7227

7228
  return (SNode*)pStmt;
404,520✔
7229
_err:
×
7230
  nodesDestroyNode(pCond);
×
7231
  return NULL;
×
7232
}
7233

7234
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
5,124,354✔
7235
  SFunctionNode* pFunc = NULL;
5,124,354✔
7236
  CHECK_PARSER_STATUS(pCxt);
5,124,354✔
7237
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
5,124,354✔
7238
  CHECK_MAKE_NODE(pFunc);
5,124,354✔
7239
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
5,124,354✔
7240
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
5,124,354✔
7241
  CHECK_MAKE_NODE(pCol);
5,124,354✔
7242
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
5,124,354✔
7243
  CHECK_PARSER_STATUS(pCxt);
5,124,354✔
7244
  return (SNode*)pFunc;
5,124,354✔
7245
_err:
×
7246
  nodesDestroyNode((SNode*)pFunc);
×
7247
  return NULL;
×
7248
}
7249

7250
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
1,708,118✔
7251
  SDeleteStmt* pStmt = NULL;
1,708,118✔
7252
  CHECK_PARSER_STATUS(pCxt);
1,708,118✔
7253
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
1,708,118✔
7254
  CHECK_MAKE_NODE(pStmt);
1,708,118✔
7255
  pStmt->pFromTable = pTable;
1,708,118✔
7256
  pStmt->pWhere = pWhere;
1,708,118✔
7257
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
1,708,118✔
7258
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
1,708,118✔
7259
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
1,708,118✔
7260
  CHECK_MAKE_NODE(pStmt->pCountFunc);
1,708,118✔
7261
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
1,708,118✔
7262
  CHECK_MAKE_NODE(pStmt->pLastFunc);
1,708,118✔
7263
  return (SNode*)pStmt;
1,708,118✔
7264
_err:
×
7265
  nodesDestroyNode((SNode*)pStmt);
×
7266
  nodesDestroyNode(pTable);
×
7267
  nodesDestroyNode(pWhere);
×
7268
  return NULL;
×
7269
}
7270

7271
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
184,371✔
7272
  CHECK_PARSER_STATUS(pCxt);
184,371✔
7273
  SInsertStmt* pStmt = NULL;
184,371✔
7274
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
184,371✔
7275
  CHECK_MAKE_NODE(pStmt);
184,371✔
7276
  pStmt->pTable = pTable;
184,371✔
7277
  pStmt->pCols = pCols;
184,371✔
7278
  pStmt->pQuery = pQuery;
184,371✔
7279
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
184,371✔
7280
    tstrncpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
184,371✔
7281
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
×
7282
    tstrncpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
×
7283
  }
7284
  return (SNode*)pStmt;
184,371✔
7285
_err:
×
7286
  nodesDestroyNode(pTable);
×
7287
  nodesDestroyNode(pQuery);
×
7288
  nodesDestroyList(pCols);
×
7289
  return NULL;
×
7290
}
7291

7292
SNode* createCreateRsmaStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* rsmaName, SNode* pRealTable,
108,919✔
7293
                            SNodeList* pFuncs, SNodeList* pIntervals) {
7294
  SCreateRsmaStmt* pStmt = NULL;
108,919✔
7295

7296
  CHECK_PARSER_STATUS(pCxt);
108,919✔
7297
  CHECK_NAME(checkRsmaName(pCxt, rsmaName));
108,919✔
7298
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_RSMA_STMT, (SNode**)&pStmt);
108,919✔
7299
  CHECK_MAKE_NODE(pStmt);
108,919✔
7300

7301
  pStmt->ignoreExists = ignoreExists;
108,919✔
7302
  pStmt->pFuncs = pFuncs;
108,919✔
7303
  pStmt->pIntervals = pIntervals;
108,919✔
7304
  COPY_STRING_FORM_ID_TOKEN(pStmt->rsmaName, rsmaName);
108,919✔
7305

7306
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
108,919✔
7307
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
108,919✔
7308
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
108,919✔
7309
  nodesDestroyNode(pRealTable);
108,919✔
7310

7311
  return (SNode*)pStmt;
108,919✔
7312
_err:
×
7313
  nodesDestroyNode((SNode*)pStmt);
×
7314
  nodesDestroyList(pFuncs);
×
7315
  nodesDestroyNode(pRealTable);
×
7316
  nodesDestroyList(pIntervals);
×
7317
  return NULL;
×
7318
}
7319

7320
SNode* createDropRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
2,193✔
7321
  CHECK_PARSER_STATUS(pCxt);
2,193✔
7322
  SDropRsmaStmt* pStmt = NULL;
2,193✔
7323
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_RSMA_STMT, (SNode**)&pStmt);
2,193✔
7324
  CHECK_MAKE_NODE(pStmt);
2,193✔
7325

7326
  pStmt->ignoreNotExists = ignoreNotExists;
2,193✔
7327
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
2,193✔
7328

7329
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
2,193✔
7330
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
2,193✔
7331

7332
  nodesDestroyNode(pRealTable);
2,193✔
7333
  return (SNode*)pStmt;
2,193✔
7334
_err:
×
7335
  nodesDestroyNode(pRealTable);
×
7336
  return NULL;
×
7337
}
7338

7339
SNode* createAlterRsmaStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRsma, int8_t alterType,
19,737✔
7340
                           void* alterInfo) {
7341
  CHECK_PARSER_STATUS(pCxt);
19,737✔
7342
  SAlterRsmaStmt* pStmt = NULL;
19,737✔
7343
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_RSMA_STMT, (SNode**)&pStmt);
19,737✔
7344
  CHECK_MAKE_NODE(pStmt);
19,737✔
7345
  pStmt->ignoreNotExists = ignoreNotExists;
19,737✔
7346
  SRealTableNode* pTableNode = (SRealTableNode*)pRsma;
19,737✔
7347

7348
  memcpy(pStmt->rsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
19,737✔
7349
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
19,737✔
7350
  nodesDestroyNode(pRsma);
19,737✔
7351

7352
  pStmt->alterType = alterType;
19,737✔
7353
  switch (alterType) {
19,737✔
7354
    case TSDB_ALTER_RSMA_FUNCTION: {
19,737✔
7355
      pStmt->pFuncs = (SNodeList*)alterInfo;
19,737✔
7356
      break;
19,737✔
7357
    }
7358
    default:
×
7359
      break;
×
7360
  }
7361
  return (SNode*)pStmt;
19,737✔
7362
_err:
×
7363
  return NULL;
×
7364
}
7365

7366
SNode* createShowCreateRsmaStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
2,193✔
7367
  CHECK_PARSER_STATUS(pCxt);
2,193✔
7368
  SShowCreateRsmaStmt* pStmt = NULL;
2,193✔
7369
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,193✔
7370
  CHECK_MAKE_NODE(pStmt);
2,193✔
7371
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, sizeof(pStmt->dbName));
2,193✔
7372
  tstrncpy(pStmt->rsmaName, ((SRealTableNode*)pRealTable)->table.tableName, sizeof(pStmt->rsmaName));
2,193✔
7373
  nodesDestroyNode(pRealTable);
2,193✔
7374
  return (SNode*)pStmt;
2,193✔
7375
_err:
×
7376
  nodesDestroyNode(pRealTable);
×
7377
  return NULL;
×
7378
}
7379

7380
SNode* createRollupStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
9,503✔
7381
  CHECK_PARSER_STATUS(pCxt);
9,503✔
7382
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
9,503✔
7383
  SRollupDatabaseStmt* pStmt = NULL;
9,503✔
7384
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_DATABASE_STMT, (SNode**)&pStmt);
9,503✔
7385
  CHECK_MAKE_NODE(pStmt);
9,503✔
7386
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
9,503✔
7387
  pStmt->pStart = pStart;
9,503✔
7388
  pStmt->pEnd = pEnd;
9,503✔
7389
  return (SNode*)pStmt;
9,503✔
7390
_err:
×
7391
  nodesDestroyNode(pStart);
×
7392
  nodesDestroyNode(pEnd);
×
7393
  return NULL;
×
7394
}
7395

7396
SNode* createRollupVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
4,386✔
7397
                                SNode* pEnd) {
7398
  CHECK_PARSER_STATUS(pCxt);
4,386✔
7399
  if (NULL == pDbName) {
4,386✔
7400
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
7401
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
7402
    CHECK_PARSER_STATUS(pCxt);
×
7403
  }
7404
  SRollupVgroupsStmt* pStmt = NULL;
4,386✔
7405
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ROLLUP_VGROUPS_STMT, (SNode**)&pStmt);
4,386✔
7406
  CHECK_MAKE_NODE(pStmt);
4,386✔
7407
  pStmt->pDbName = pDbName;
4,386✔
7408
  pStmt->vgidList = vgidList;
4,386✔
7409
  pStmt->pStart = pStart;
4,386✔
7410
  pStmt->pEnd = pEnd;
4,386✔
7411
  return (SNode*)pStmt;
4,386✔
7412
_err:
×
7413
  nodesDestroyNode(pDbName);
×
7414
  nodesDestroyList(vgidList);
×
7415
  nodesDestroyNode(pStart);
×
7416
  nodesDestroyNode(pEnd);
×
7417
  return NULL;
×
7418
}
7419

7420
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
800✔
7421
                            SNode* pRealTable, SNode* pInterval) {
7422
  SCreateTSMAStmt* pStmt = NULL;
800✔
7423

7424
  CHECK_PARSER_STATUS(pCxt);
800✔
7425
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
800✔
7426
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
800✔
7427
  CHECK_MAKE_NODE(pStmt);
800✔
7428

7429
  pStmt->ignoreExists = ignoreExists;
800✔
7430
  if (!pOptions) {
800✔
7431
    // recursive tsma
7432
    pStmt->pOptions = NULL;
×
7433
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
×
7434
    CHECK_MAKE_NODE(pStmt->pOptions);
×
7435
    pStmt->pOptions->recursiveTsma = true;
×
7436
  } else {
7437
    pStmt->pOptions = (STSMAOptions*)pOptions;
800✔
7438
  }
7439
  pStmt->pOptions->pInterval = pInterval;
800✔
7440
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
800✔
7441

7442
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
800✔
7443
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
800✔
7444
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
800✔
7445
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
800✔
7446
  nodesDestroyNode(pRealTable);
800✔
7447

7448
  return (SNode*)pStmt;
800✔
7449
_err:
×
7450
  nodesDestroyNode((SNode*)pStmt);
×
7451
  nodesDestroyNode(pOptions);
×
7452
  nodesDestroyNode(pRealTable);
×
7453
  nodesDestroyNode(pInterval);
×
7454
  return NULL;
×
7455
}
7456

7457
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
800✔
7458
  CHECK_PARSER_STATUS(pCxt);
800✔
7459
  STSMAOptions* pOptions = NULL;
800✔
7460
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
800✔
7461
  CHECK_MAKE_NODE(pOptions);
800✔
7462
  pOptions->pFuncs = pFuncs;
800✔
7463
  return (SNode*)pOptions;
800✔
7464
_err:
×
7465
  nodesDestroyList(pFuncs);
×
7466
  return NULL;
×
7467
}
7468

7469
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
7470
  CHECK_PARSER_STATUS(pCxt);
×
7471
  STSMAOptions* pOptions = NULL;
×
7472
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
7473
  CHECK_MAKE_NODE(pOptions);
×
7474
  return (SNode*)pOptions;
×
7475
_err:
×
7476
  return NULL;
×
7477
}
7478

7479
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
×
7480
  CHECK_PARSER_STATUS(pCxt);
×
7481
  SDropTSMAStmt* pStmt = NULL;
×
7482
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
×
7483
  CHECK_MAKE_NODE(pStmt);
×
7484

7485
  pStmt->ignoreNotExists = ignoreNotExists;
×
7486
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
×
7487

7488
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
×
7489
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
×
7490

7491
  nodesDestroyNode(pRealTable);
×
7492
  return (SNode*)pStmt;
×
7493
_err:
×
7494
  nodesDestroyNode(pRealTable);
×
7495
  return NULL;
×
7496
}
7497

7498
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
×
7499
  CHECK_PARSER_STATUS(pCxt);
×
7500

7501
  SShowStmt* pStmt = NULL;
×
7502
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
×
7503
  CHECK_MAKE_NODE(pStmt);
×
7504

7505
  pStmt->pDbName = dbName;
×
7506
  return (SNode*)pStmt;
×
7507
_err:
×
7508
  nodesDestroyNode(dbName);
×
7509
  return NULL;
×
7510
}
7511
SNode* createShowDiskUsageStmt(SAstCreateContext* pCxt, SNode* dbName, ENodeType type) {
306✔
7512
  CHECK_PARSER_STATUS(pCxt);
306✔
7513
  if (NULL == dbName) {
306✔
7514
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
7515
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
7516
    CHECK_PARSER_STATUS(pCxt);
×
7517
  }
7518

7519
  SShowStmt* pStmt = NULL;
306✔
7520
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
306✔
7521
  CHECK_MAKE_NODE(pStmt);
306✔
7522

7523
  pStmt->pDbName = dbName;
306✔
7524
  return (SNode*)pStmt;
306✔
7525
_err:
×
7526
  nodesDestroyNode(dbName);
×
7527
  return NULL;
×
7528
}
7529

7530
SNode* createShowStreamsStmt(SAstCreateContext* pCxt, SNode* pDbName, ENodeType type) {
100,958✔
7531
  CHECK_PARSER_STATUS(pCxt);
100,958✔
7532

7533
  if (needDbShowStmt(type) && NULL == pDbName) {
100,958✔
7534
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
206✔
7535
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
206✔
7536
    CHECK_PARSER_STATUS(pCxt);
206✔
7537
  }
7538

7539
  SShowStmt* pStmt = NULL;
100,752✔
7540
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
100,752✔
7541
  CHECK_MAKE_NODE(pStmt);
100,752✔
7542
  pStmt->withFull = false;
100,752✔
7543
  pStmt->pDbName = pDbName;
100,752✔
7544

7545
  return (SNode*)pStmt;
100,752✔
7546

7547
_err:
206✔
7548
  nodesDestroyNode(pDbName);
206✔
7549
  return NULL;
206✔
7550
}
7551

7552
SNode* createScanStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
114✔
7553
  CHECK_PARSER_STATUS(pCxt);
114✔
7554
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
114✔
7555
  SScanDatabaseStmt* pStmt = NULL;
114✔
7556
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_DATABASE_STMT, (SNode**)&pStmt);
114✔
7557
  CHECK_MAKE_NODE(pStmt);
114✔
7558
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
114✔
7559
  pStmt->pStart = pStart;
114✔
7560
  pStmt->pEnd = pEnd;
114✔
7561
  return (SNode*)pStmt;
114✔
7562
_err:
×
7563
  nodesDestroyNode(pStart);
×
7564
  nodesDestroyNode(pEnd);
×
7565
  return NULL;
×
7566
}
7567

7568
SNode* createScanVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart, SNode* pEnd) {
342✔
7569
  CHECK_PARSER_STATUS(pCxt);
342✔
7570
  if (NULL == pDbName) {
342✔
7571
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
57✔
7572
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
57✔
7573
    CHECK_PARSER_STATUS(pCxt);
57✔
7574
  }
7575
  SScanVgroupsStmt* pStmt = NULL;
285✔
7576
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SCAN_VGROUPS_STMT, (SNode**)&pStmt);
285✔
7577
  CHECK_MAKE_NODE(pStmt);
285✔
7578
  pStmt->pDbName = pDbName;
285✔
7579
  pStmt->vgidList = vgidList;
285✔
7580
  pStmt->pStart = pStart;
285✔
7581
  pStmt->pEnd = pEnd;
285✔
7582
  return (SNode*)pStmt;
285✔
7583
_err:
57✔
7584
  nodesDestroyNode(pDbName);
57✔
7585
  nodesDestroyList(vgidList);
57✔
7586
  nodesDestroyNode(pStart);
57✔
7587
  nodesDestroyNode(pEnd);
57✔
7588
  return NULL;
57✔
7589
}
7590

7591
SNode* createShowScansStmt(SAstCreateContext* pCxt, ENodeType type) {
1,995✔
7592
  CHECK_PARSER_STATUS(pCxt);
1,995✔
7593
  SShowScansStmt* pStmt = NULL;
1,995✔
7594
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,995✔
7595
  CHECK_MAKE_NODE(pStmt);
1,995✔
7596
  return (SNode*)pStmt;
1,995✔
7597
_err:
×
7598
  return NULL;
×
7599
}
7600

7601
SNode* createShowScanDetailsStmt(SAstCreateContext* pCxt, SNode* pScanIdNode) {
2,280✔
7602
  CHECK_PARSER_STATUS(pCxt);
2,280✔
7603
  SShowScanDetailsStmt* pStmt = NULL;
2,280✔
7604
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_SCAN_DETAILS_STMT, (SNode**)&pStmt);
2,280✔
7605
  CHECK_MAKE_NODE(pStmt);
2,280✔
7606
  pStmt->pScanId = pScanIdNode;
2,280✔
7607
  return (SNode*)pStmt;
2,280✔
7608
_err:
×
7609
  nodesDestroyNode(pScanIdNode);
×
7610
  return NULL;
×
7611
}
7612

7613
SNode* createAlterAllDnodeTLSStmt(SAstCreateContext* pCxt, SToken* alterName) {
×
7614
  CHECK_PARSER_STATUS(pCxt);
×
7615
  SAlterDnodeStmt* pStmt = NULL;
×
7616
  static char*     tls = "TLS";
7617
  if (NULL == alterName || alterName->n <= 0) {
×
7618
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter name is empty");
×
7619
    goto _err;
×
7620
  }
7621

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

7625
    memcpy(pStmt->config, "reload", strlen("reload"));
×
7626
    memcpy(pStmt->value, "tls", strlen("tls"));
×
7627
    pStmt->dnodeId = -1;
×
7628
  } else {
7629
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "alter is not supported");
×
7630
    goto _err;
×
7631
  }
7632

7633
  CHECK_MAKE_NODE(pStmt);
×
7634

7635
  return (SNode*)pStmt;
×
7636
_err:
×
7637
  return NULL;
×
7638
}
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