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

taosdata / TDengine / #3911

24 Apr 2025 11:36PM UTC coverage: 53.735% (-1.6%) from 55.295%
#3911

push

travis-ci

happyguoxy
Sync branches at 2025-04-25 07:35

170049 of 316459 relevant lines covered (53.73%)

1192430.54 hits per line

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

52.76
/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
#include <regex.h>
16
#ifndef TD_ASTRA
17
#include <uv.h>
18
#endif
19

20
#include "nodes.h"
21
#include "parAst.h"
22
#include "parUtil.h"
23
#include "tglobal.h"
24
#include "ttime.h"
25

26
#define CHECK_MAKE_NODE(p) \
27
  do {                     \
28
    if (NULL == (p)) {     \
29
      goto _err;           \
30
    }                      \
31
  } while (0)
32

33
#define CHECK_OUT_OF_MEM(p)   \
34
  do {                        \
35
    if (NULL == (p)) {        \
36
      pCxt->errCode = terrno; \
37
      goto _err;              \
38
    }                         \
39
  } while (0)
40

41
#define CHECK_PARSER_STATUS(pCxt)             \
42
  do {                                        \
43
    if (TSDB_CODE_SUCCESS != pCxt->errCode) { \
44
      goto _err;                              \
45
    }                                         \
46
  } while (0)
47

48
#define CHECK_NAME(p) \
49
  do {                \
50
    if (!p) {         \
51
      goto _err;      \
52
    }                 \
53
  } while (0)
54

55
#define COPY_STRING_FORM_ID_TOKEN(buf, pToken) strncpy(buf, (pToken)->z, TMIN((pToken)->n, sizeof(buf) - 1))
56
#define COPY_STRING_FORM_STR_TOKEN(buf, pToken)                              \
57
  do {                                                                       \
58
    if ((pToken)->n > 2) {                                                   \
59
      strncpy(buf, (pToken)->z + 1, TMIN((pToken)->n - 2, sizeof(buf) - 1)); \
60
    }                                                                        \
61
  } while (0)
62

63
SToken nil_token = {.type = TK_NK_NIL, .n = 0, .z = NULL};
64

65
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
46,364✔
66
  memset(pCxt, 0, sizeof(SAstCreateContext));
46,364✔
67
  pCxt->pQueryCxt = pParseCxt;
46,364✔
68
  pCxt->msgBuf.buf = pParseCxt->pMsg;
46,364✔
69
  pCxt->msgBuf.len = pParseCxt->msgLen;
46,364✔
70
  pCxt->notSupport = false;
46,364✔
71
  pCxt->pRootNode = NULL;
46,364✔
72
  pCxt->placeholderNo = 0;
46,364✔
73
  pCxt->pPlaceholderValues = NULL;
46,364✔
74
  pCxt->errCode = TSDB_CODE_SUCCESS;
46,364✔
75
}
46,364✔
76

77
static void trimEscape(SToken* pName) {
285,324✔
78
  // todo need to deal with `ioo``ii` -> ioo`ii
79
  if (NULL != pName && pName->n > 1 && '`' == pName->z[0]) {
285,324✔
80
    pName->z += 1;
7,431✔
81
    pName->n -= 2;
7,431✔
82
  }
83
}
285,324✔
84

85
static bool checkUserName(SAstCreateContext* pCxt, SToken* pUserName) {
206✔
86
  if (NULL == pUserName) {
206✔
87
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
88
  } else {
89
    if (pUserName->n >= TSDB_USER_LEN) {
206✔
90
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
91
    }
92
  }
93
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
206✔
94
    trimEscape(pUserName);
206✔
95
  }
96
  return TSDB_CODE_SUCCESS == pCxt->errCode;
206✔
97
}
98

99
static bool invalidPassword(const char* pPassword) {
×
100
  regex_t regex;
101

102
  if (regcomp(&regex, "[ '\"`\\]", REG_EXTENDED | REG_ICASE) != 0) {
×
103
    return false;
×
104
  }
105

106
  /* Execute regular expression */
107
  int32_t res = regexec(&regex, pPassword, 0, NULL, 0);
×
108
  regfree(&regex);
×
109
  return 0 == res;
×
110
}
111

112
static bool invalidStrongPassword(const char* pPassword) {
79✔
113
  if (strcmp(pPassword, "taosdata") == 0) {
79✔
114
    return false;
13✔
115
  }
116

117
  bool charTypes[4] = {0};
66✔
118
  for (int32_t i = 0; i < strlen(pPassword); ++i) {
895✔
119
    if (taosIsBigChar(pPassword[i])) {
829✔
120
      charTypes[0] = true;
205✔
121
    } else if (taosIsSmallChar(pPassword[i])) {
624✔
122
      charTypes[1] = true;
263✔
123
    } else if (taosIsNumberChar(pPassword[i])) {
361✔
124
      charTypes[2] = true;
267✔
125
    } else if (taosIsSpecialChar(pPassword[i])) {
94✔
126
      charTypes[3] = true;
94✔
127
    } else {
128
      return true;
×
129
    }
130
  }
131

132
  int32_t numOfTypes = 0;
66✔
133
  for (int32_t i = 0; i < 4; ++i) {
330✔
134
    numOfTypes += charTypes[i];
264✔
135
  }
136

137
  if (numOfTypes < 3) {
66✔
138
    return true;
×
139
  }
140

141
  return false;
66✔
142
}
143

144
static bool checkPassword(SAstCreateContext* pCxt, const SToken* pPasswordToken, char* pPassword) {
80✔
145
  if (NULL == pPasswordToken) {
80✔
146
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
147
  } else if (pPasswordToken->n >= (TSDB_USET_PASSWORD_LONGLEN + 2)) {
80✔
148
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
149
  } else {
150
    strncpy(pPassword, pPasswordToken->z, pPasswordToken->n);
80✔
151
    (void)strdequote(pPassword);
80✔
152
    if (strtrim(pPassword) < TSDB_PASSWORD_MIN_LEN) {
80✔
153
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
1✔
154
    } else {
155
      if (tsEnableStrongPassword) {
79✔
156
        if (invalidStrongPassword(pPassword)) {
79✔
157
          pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
×
158
        }
159
      } else {
160
        if (invalidPassword(pPassword)) {
×
161
          pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
×
162
        }
163
      }
164
    }
165
  }
166
  return TSDB_CODE_SUCCESS == pCxt->errCode;
80✔
167
}
168

169
static bool checkImportPassword(SAstCreateContext* pCxt, const SToken* pPasswordToken, char* pPassword) {
×
170
  if (NULL == pPasswordToken) {
×
171
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
172
  } else if (pPasswordToken->n > (32 + 2)) {
×
173
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
174
  } else {
175
    strncpy(pPassword, pPasswordToken->z, pPasswordToken->n);
×
176
    (void)strdequote(pPassword);
×
177
    if (strtrim(pPassword) < 32) {
×
178
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
×
179
    }
180
  }
181
  return TSDB_CODE_SUCCESS == pCxt->errCode;
×
182
}
183

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

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

197
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
68✔
198
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
68✔
199
  (void)strdequote(ep);
68✔
200
  (void)strtrim(ep);
68✔
201
  if (NULL == pPort) {
68✔
202
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
14✔
203
    return TSDB_CODE_SUCCESS;
14✔
204
  }
205
  char* pColon = strchr(ep, ':');
54✔
206
  if (NULL == pColon) {
54✔
207
    *pPort = tsServerPort;
20✔
208
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
20✔
209
    return TSDB_CODE_SUCCESS;
20✔
210
  }
211
  strncpy(pFqdn, ep, pColon - ep);
34✔
212
  return parsePort(pCxt, pColon + 1, pPort);
34✔
213
}
214

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

222
  if (NULL != pPortToken) {
68✔
223
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
14✔
224
  }
225

226
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
68✔
227
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
68✔
228
  }
229

230
  return TSDB_CODE_SUCCESS == pCxt->errCode;
68✔
231
}
232

233
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
84,025✔
234
  if (NULL == pDbName) {
84,025✔
235
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
9,296✔
236
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
237
    }
238
  } else {
239
    trimEscape(pDbName);
74,729✔
240
    if (pDbName->n >= TSDB_DB_NAME_LEN || pDbName->n == 0) {
74,738✔
241
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
×
242
    }
243
  }
244
  return TSDB_CODE_SUCCESS == pCxt->errCode;
84,045✔
245
}
246

247
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
177,410✔
248
  trimEscape(pTableName);
177,410✔
249
  if (NULL != pTableName && pTableName->type != TK_NK_NIL &&
177,429✔
250
      (pTableName->n >= TSDB_TABLE_NAME_LEN || pTableName->n == 0)) {
82,848✔
251
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
×
252
    return false;
×
253
  }
254
  return true;
177,453✔
255
}
256

257
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
32,310✔
258
  trimEscape(pColumnName);
32,310✔
259
  if (NULL != pColumnName && pColumnName->type != TK_NK_NIL &&
32,313✔
260
      (pColumnName->n >= TSDB_COL_NAME_LEN || pColumnName->n == 0)) {
32,312✔
261
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
1✔
262
    return false;
1✔
263
  }
264
  return true;
32,312✔
265
}
266

267
static bool checkIndexName(SAstCreateContext* pCxt, SToken* pIndexName) {
22✔
268
  trimEscape(pIndexName);
22✔
269
  if (NULL != pIndexName && pIndexName->n >= TSDB_INDEX_NAME_LEN) {
22✔
270
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pIndexName->z);
×
271
    return false;
×
272
  }
273
  return true;
22✔
274
}
275

276
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
106✔
277
  trimEscape(pTopicName);
106✔
278
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN || pTopicName->n == 0) {
106✔
279
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
×
280
    return false;
×
281
  }
282
  return true;
106✔
283
}
284

285
static bool checkCGroupName(SAstCreateContext* pCxt, SToken* pCGroup) {
12✔
286
  trimEscape(pCGroup);
12✔
287
  if (pCGroup->n >= TSDB_CGROUP_LEN) {
12✔
288
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pCGroup->z);
×
289
    return false;
×
290
  }
291
  return true;
12✔
292
}
293

294
static bool checkViewName(SAstCreateContext* pCxt, SToken* pViewName) {
×
295
  trimEscape(pViewName);
×
296
  if (pViewName->n >= TSDB_VIEW_NAME_LEN || pViewName->n == 0) {
×
297
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pViewName->z);
×
298
    return false;
×
299
  }
300
  return true;
×
301
}
302

303
static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
78✔
304
  trimEscape(pStreamName);
78✔
305
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN || pStreamName->n == 0) {
78✔
306
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
×
307
    return false;
×
308
  }
309
  return true;
78✔
310
}
311

312
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
20✔
313
  if (NULL == pCommentToken) {
20✔
314
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
×
315
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
20✔
316
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
×
317
  }
318
  return TSDB_CODE_SUCCESS == pCxt->errCode;
20✔
319
}
320

321
static bool checkTsmaName(SAstCreateContext* pCxt, SToken* pTsmaToken) {
2✔
322
  trimEscape(pTsmaToken);
2✔
323
  if (NULL == pTsmaToken) {
2✔
324
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
325
  } else if (pTsmaToken->n >= TSDB_TABLE_NAME_LEN - strlen(TSMA_RES_STB_POSTFIX)) {
2✔
326
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
×
327
  } else if (pTsmaToken->n == 0) {
2✔
328
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTsmaToken->z);
×
329
  }
330
  return pCxt->errCode == TSDB_CODE_SUCCESS;
2✔
331
}
332

333
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
12,281✔
334
  CHECK_PARSER_STATUS(pCxt);
12,281✔
335
  SRawExprNode* target = NULL;
12,277✔
336
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
12,277✔
337
  CHECK_MAKE_NODE(target);
12,276✔
338
  target->p = pToken->z;
12,276✔
339
  target->n = pToken->n;
12,276✔
340
  target->pNode = pNode;
12,276✔
341
  return (SNode*)target;
12,276✔
342
_err:
4✔
343
  nodesDestroyNode(pNode);
4✔
344
  return NULL;
4✔
345
}
346

347
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
11,881✔
348
  CHECK_PARSER_STATUS(pCxt);
11,881✔
349
  SRawExprNode* target = NULL;
11,881✔
350
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
11,881✔
351
  CHECK_MAKE_NODE(target);
11,885✔
352
  target->p = pStart->z;
11,885✔
353
  target->n = (pEnd->z + pEnd->n) - pStart->z;
11,885✔
354
  target->pNode = pNode;
11,885✔
355
  return (SNode*)target;
11,885✔
356
_err:
×
357
  nodesDestroyNode(pNode);
×
358
  return NULL;
×
359
}
360

361
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
887✔
362
  CHECK_PARSER_STATUS(pCxt);
887✔
363
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
887✔
364
    return pNode;
×
365
  }
366
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
887✔
367
  return pNode;
887✔
368
_err:
×
369
  nodesDestroyNode(pNode);
×
370
  return NULL;
×
371
}
372

373
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
23,142✔
374
  CHECK_PARSER_STATUS(pCxt);
23,142✔
375
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
23,142✔
376
  SNode*        pRealizedExpr = pRawExpr->pNode;
23,142✔
377
  if (nodesIsExprNode(pRealizedExpr)) {
23,142✔
378
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
22,483✔
379
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
22,483✔
380
      tstrncpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
7,675✔
381
      tstrncpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
7,675✔
382
    } else if (pRawExpr->isPseudoColumn) {
14,808✔
383
      // all pseudo column are translate to function with same name
384
      tstrncpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
334✔
385
      tstrncpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
334✔
386
    } else {
387
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
14,474✔
388

389
      // See TS-3398.
390
      // Len of pRawExpr->p could be larger than len of aliasName[TSDB_COL_NAME_LEN].
391
      // If aliasName is truncated, hash value of aliasName could be the same.
392
      uint64_t hashVal = MurmurHash3_64(pRawExpr->p, pRawExpr->n);
14,474✔
393
      snprintf(pExpr->aliasName, TSDB_COL_NAME_LEN, "%" PRIu64, hashVal);
14,474✔
394
      strncpy(pExpr->userAlias, pRawExpr->p, len);
14,474✔
395
      pExpr->userAlias[len] = 0;
14,474✔
396
    }
397
  }
398
  pRawExpr->pNode = NULL;
23,148✔
399
  nodesDestroyNode(pNode);
23,148✔
400
  return pRealizedExpr;
23,145✔
401
_err:
×
402
  nodesDestroyNode(pNode);
×
403
  return NULL;
×
404
}
405

406
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
8,677✔
407
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
8,677✔
408
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
409
    return nil_token;
×
410
  }
411
  SRawExprNode* target = (SRawExprNode*)pNode;
8,677✔
412
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
8,677✔
413
  return t;
8,677✔
414
}
415

416
SNodeList* createColsFuncParamNodeList(SAstCreateContext* pCxt, SNode* pNode, SNodeList* pNodeList, SToken* pAlias) {
×
417
  CHECK_PARSER_STATUS(pCxt);
×
418
    if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
×
419
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
420
  }
421
  CHECK_PARSER_STATUS(pCxt);
×
422
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
×
423
  SNode*        pFuncNode = pRawExpr->pNode;
×
424
  if(pFuncNode->type != QUERY_NODE_FUNCTION) {
×
425
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
426
  }
427
  CHECK_PARSER_STATUS(pCxt);
×
428
  SNodeList* list = NULL;
×
429
  pCxt->errCode = nodesMakeList(&list);
×
430
  CHECK_MAKE_NODE(list);
×
431
  pCxt->errCode = nodesListAppend(list, pFuncNode);
×
432
  CHECK_PARSER_STATUS(pCxt);
×
433
  pCxt->errCode = nodesListAppendList(list, pNodeList);
×
434
  CHECK_PARSER_STATUS(pCxt);
×
435
  return list;
×
436

437
  _err:
×
438
  nodesDestroyNode(pFuncNode);
×
439
  nodesDestroyList(pNodeList);
×
440
  return NULL;
×
441
}
442

443
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
87,368✔
444
  CHECK_PARSER_STATUS(pCxt);
87,368✔
445
  SNodeList* list = NULL;
87,368✔
446
  pCxt->errCode = nodesMakeList(&list);
87,368✔
447
  CHECK_MAKE_NODE(list);
87,417✔
448
  pCxt->errCode = nodesListAppend(list, pNode);
87,417✔
449
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
87,436✔
450
    nodesDestroyList(list);
4✔
451
    return NULL;
×
452
  }
453
  return list;
87,432✔
454
_err:
×
455
  nodesDestroyNode(pNode);
×
456
  return NULL;
×
457
}
458

459
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
117,012✔
460
  CHECK_PARSER_STATUS(pCxt);
117,012✔
461
  pCxt->errCode = nodesListAppend(pList, pNode);
117,012✔
462
  return pList;
117,037✔
463
_err:
×
464
  nodesDestroyNode(pNode);
×
465
  nodesDestroyList(pList);
×
466
  return NULL;
×
467
}
468

469
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
12,587✔
470
  CHECK_PARSER_STATUS(pCxt);
12,587✔
471
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
12,587✔
472
    return NULL;
1✔
473
  }
474
  SColumnNode* col = NULL;
12,593✔
475
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
12,593✔
476
  CHECK_MAKE_NODE(col);
12,587✔
477
  if (NULL != pTableAlias) {
12,587✔
478
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
376✔
479
  }
480
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
12,587✔
481
  return (SNode*)col;
12,587✔
482
_err:
×
483
  return NULL;
×
484
}
485

486
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
45,854✔
487
  CHECK_PARSER_STATUS(pCxt);
45,854✔
488
  SValueNode* val = NULL;
45,854✔
489
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
45,854✔
490
  CHECK_MAKE_NODE(val);
45,911✔
491
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
45,911✔
492
  if (!val->literal) {
45,918✔
493
    pCxt->errCode = terrno;
×
494
    nodesDestroyNode((SNode*)val);
×
495
    return NULL;
×
496
  }
497
  if (TK_NK_ID != pLiteral->type && TK_TIMEZONE != pLiteral->type &&
45,918✔
498
      (IS_VAR_DATA_TYPE(dataType) || TSDB_DATA_TYPE_TIMESTAMP == dataType)) {
44,824✔
499
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
915✔
500
  }
501
  val->node.resType.type = dataType;
45,944✔
502
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
45,944✔
503
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
45,944✔
504
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
50✔
505
  }
506
  val->translate = false;
45,944✔
507
  val->tz = pCxt->pQueryCxt->timezone;
45,944✔
508
  val->charsetCxt = pCxt->pQueryCxt->charsetCxt;
45,944✔
509
  return (SNode*)val;
45,944✔
510
_err:
×
511
  return NULL;
×
512
}
513

514
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
88,101✔
515
  CHECK_PARSER_STATUS(pCxt);
88,101✔
516
  SValueNode* val = NULL;
88,101✔
517
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
88,101✔
518
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
88,154✔
519
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
520
    goto _exit;
×
521
  }
522
  if (pLiteral) {
88,159✔
523
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
87,155✔
524
    if (!val->literal) {
87,144✔
525
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
526
      goto _exit;
×
527
    }
528
  } else if (pNode) {
1,004✔
529
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
1,004✔
530
    if (!nodesIsExprNode(pRawExpr->pNode)) {
1,004✔
531
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
532
      goto _exit;
×
533
    }
534
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
1,004✔
535
    if (!val->literal) {
1,002✔
536
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
537
      goto _exit;
×
538
    }
539
  } else {
540
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
541
    goto _exit;
×
542
  }
543
  if (!val->literal) {
88,146✔
544
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
545
    goto _exit;
×
546
  }
547

548
  val->node.resType.type = dataType;
88,146✔
549
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
88,146✔
550
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
88,146✔
551
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
552
  }
553
_exit:
88,146✔
554
  nodesDestroyNode(pNode);
88,146✔
555
  if (pCxt->errCode != 0) {
88,143✔
556
    nodesDestroyNode((SNode*)val);
×
557
    return NULL;
×
558
  }
559
  return (SNode*)val;
88,145✔
560
_err:
×
561
  nodesDestroyNode(pNode);
×
562
  return NULL;
×
563
}
564

565
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
4✔
566
                             SNode* pRight) {
567
  SValueNode* val = NULL;
4✔
568
  CHECK_PARSER_STATUS(pCxt);
4✔
569

570
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
4✔
571
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
4✔
572
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
573
    goto _exit;
×
574
  }
575
  if (pLiteral) {
4✔
576
    if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) {
4✔
577
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
578
      goto _exit;
×
579
    }
580
  } else {
581
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
582
    goto _exit;
×
583
  }
584

585
  val->node.resType.type = dataType;
4✔
586
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
4✔
587
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
4✔
588
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
589
  }
590
_exit:
4✔
591
  nodesDestroyNode(pLeft);
4✔
592
  nodesDestroyNode(pRight);
4✔
593
  CHECK_PARSER_STATUS(pCxt);
4✔
594
  return (SNode*)val;
4✔
595
_err:
×
596
  nodesDestroyNode((SNode*)val);
×
597
  nodesDestroyNode(pLeft);
×
598
  nodesDestroyNode(pRight);
×
599
  return NULL;
×
600
}
601

602
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
×
603
  if (!pHintList) return false;
×
604
  SNode* pNode;
605
  FOREACH(pNode, pHintList) {
×
606
    SHintNode* pHint = (SHintNode*)pNode;
×
607
    if (pHint->option == hint) {
×
608
      return true;
×
609
    }
610
  }
611
  return false;
×
612
}
613

614
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
×
615
                       int32_t paramNum) {
616
  void* value = NULL;
×
617
  switch (opt) {
×
618
    case HINT_SKIP_TSMA:
×
619
    case HINT_BATCH_SCAN:
620
    case HINT_NO_BATCH_SCAN: {
621
      if (paramNum > 0) {
×
622
        return true;
×
623
      }
624
      break;
×
625
    }
626
    case HINT_SORT_FOR_GROUP:
×
627
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
×
628
      break;
×
629
    case HINT_PARTITION_FIRST:
×
630
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
×
631
      break;
×
632
    case HINT_PARA_TABLES_SORT:
×
633
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
×
634
      break;
×
635
    case HINT_SMALLDATA_TS_SORT:
×
636
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
637
      break;
×
638
    case HINT_HASH_JOIN:
×
639
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
×
640
      break;
×
641
    default:
×
642
      return true;
×
643
  }
644

645
  SHintNode* hint = NULL;
×
646
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
×
647
  if (!hint) {
×
648
    return true;
×
649
  }
650
  hint->option = opt;
×
651
  hint->value = value;
×
652

653
  if (NULL == *ppHintList) {
×
654
    pCxt->errCode = nodesMakeList(ppHintList);
×
655
    if (!*ppHintList) {
×
656
      nodesDestroyNode((SNode*)hint);
×
657
      return true;
×
658
    }
659
  }
660

661
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
×
662
  if (pCxt->errCode) {
×
663
    return true;
×
664
  }
665

666
  return false;
×
667
}
668

669
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
7,369✔
670
  CHECK_PARSER_STATUS(pCxt);
7,369✔
671
  if (NULL == pLiteral || pLiteral->n <= 5) {
7,369✔
672
    return NULL;
7,369✔
673
  }
674
  SNodeList* pHintList = NULL;
×
675
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
×
676
  if (!hint) return NULL;
×
677
  int32_t     i = 0;
×
678
  bool        quit = false;
×
679
  bool        inParamList = false;
×
680
  bool        lastComma = false;
×
681
  EHintOption opt = 0;
×
682
  int32_t     paramNum = 0;
×
683
  SToken      paramList[10];
684
  while (!quit) {
×
685
    SToken t0 = {0};
×
686
    if (hint[i] == 0) {
×
687
      break;
×
688
    }
689
    t0.n = tGetToken(&hint[i], &t0.type);
×
690
    t0.z = hint + i;
×
691
    i += t0.n;
×
692

693
    switch (t0.type) {
×
694
      case TK_BATCH_SCAN:
×
695
        lastComma = false;
×
696
        if (0 != opt || inParamList) {
×
697
          quit = true;
×
698
          break;
×
699
        }
700
        opt = HINT_BATCH_SCAN;
×
701
        break;
×
702
      case TK_NO_BATCH_SCAN:
×
703
        lastComma = false;
×
704
        if (0 != opt || inParamList) {
×
705
          quit = true;
×
706
          break;
×
707
        }
708
        opt = HINT_NO_BATCH_SCAN;
×
709
        break;
×
710
      case TK_SORT_FOR_GROUP:
×
711
        lastComma = false;
×
712
        if (0 != opt || inParamList) {
×
713
          quit = true;
×
714
          break;
×
715
        }
716
        opt = HINT_SORT_FOR_GROUP;
×
717
        break;
×
718
      case TK_PARTITION_FIRST:
×
719
        lastComma = false;
×
720
        if (0 != opt || inParamList) {
×
721
          quit = true;
×
722
          break;
×
723
        }
724
        opt = HINT_PARTITION_FIRST;
×
725
        break;
×
726
      case TK_PARA_TABLES_SORT:
×
727
        lastComma = false;
×
728
        if (0 != opt || inParamList) {
×
729
          quit = true;
×
730
          break;
×
731
        }
732
        opt = HINT_PARA_TABLES_SORT;
×
733
        break;
×
734
      case TK_SMALLDATA_TS_SORT:
×
735
        lastComma = false;
×
736
        if (0 != opt || inParamList) {
×
737
          quit = true;
×
738
          break;
×
739
        }
740
        opt = HINT_SMALLDATA_TS_SORT;
×
741
        break;
×
742
      case TK_HASH_JOIN:
×
743
        lastComma = false;
×
744
        if (0 != opt || inParamList) {
×
745
          quit = true;
×
746
          break;
×
747
        }
748
        opt = HINT_HASH_JOIN;
×
749
        break;
×
750
      case TK_SKIP_TSMA:
×
751
        lastComma = false;
×
752
        if (0 != opt || inParamList) {
×
753
          quit = true;
×
754
          break;
×
755
        }
756
        opt = HINT_SKIP_TSMA;
×
757
        break;
×
758
      case TK_NK_LP:
×
759
        lastComma = false;
×
760
        if (0 == opt || inParamList) {
×
761
          quit = true;
×
762
        }
763
        inParamList = true;
×
764
        break;
×
765
      case TK_NK_RP:
×
766
        lastComma = false;
×
767
        if (0 == opt || !inParamList) {
×
768
          quit = true;
×
769
        } else {
770
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
×
771
          inParamList = false;
×
772
          paramNum = 0;
×
773
          opt = 0;
×
774
        }
775
        break;
×
776
      case TK_NK_ID:
×
777
        lastComma = false;
×
778
        if (0 == opt || !inParamList) {
×
779
          quit = true;
×
780
        } else {
781
          paramList[paramNum++] = t0;
×
782
        }
783
        break;
×
784
      case TK_NK_COMMA:
×
785
        if (lastComma) {
×
786
          quit = true;
×
787
        }
788
        lastComma = true;
×
789
        break;
×
790
      case TK_NK_SPACE:
×
791
        break;
×
792
      default:
×
793
        lastComma = false;
×
794
        quit = true;
×
795
        break;
×
796
    }
797
  }
798

799
  taosMemoryFree(hint);
×
800
  return pHintList;
×
801
_err:
×
802
  return NULL;
×
803
}
804

805
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
232✔
806
  trimEscape(pLiteral);
232✔
807
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
232✔
808
}
809

810
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
775✔
811
  CHECK_PARSER_STATUS(pCxt);
775✔
812
  SValueNode* val = NULL;
775✔
813
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
775✔
814
  CHECK_MAKE_NODE(val);
775✔
815
  if (pLiteral->type == TK_NK_STRING) {
775✔
816
    // like '100s' or "100d"
817
    // check format: ^[0-9]+[smwbauhdny]$'
818
    if (pLiteral->n < 4) {
×
819
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
820
      return NULL;
×
821
    }
822
    char unit = pLiteral->z[pLiteral->n - 2];
×
823
    switch (unit) {
×
824
      case 'a':
×
825
      case 'b':
826
      case 'd':
827
      case 'h':
828
      case 'm':
829
      case 's':
830
      case 'u':
831
      case 'w':
832
      case 'y':
833
      case 'n':
834
        break;
×
835
      default:
×
836
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
837
        return NULL;
×
838
    }
839
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
×
840
      if (!isdigit(pLiteral->z[i])) {
×
841
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
842
        return NULL;
×
843
      }
844
    }
845
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
×
846
  } else {
847
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
775✔
848
  }
849
  if (!val->literal) {
775✔
850
    nodesDestroyNode((SNode*)val);
×
851
    pCxt->errCode = terrno;
×
852
    return NULL;
×
853
  }
854
  val->flag |= VALUE_FLAG_IS_DURATION;
775✔
855
  val->translate = false;
775✔
856
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
775✔
857
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
775✔
858
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
775✔
859
  return (SNode*)val;
775✔
860
_err:
×
861
  return NULL;
×
862
}
863

864
SNode* createTimeOffsetValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
×
865
  CHECK_PARSER_STATUS(pCxt);
×
866
  SValueNode* val = NULL;
×
867
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
×
868
  CHECK_MAKE_NODE(val);
×
869
  if (pLiteral->type == TK_NK_STRING) {
×
870
    // like '100s' or "100d"
871
    // check format: ^[0-9]+[smwbauhdny]$'
872
    if (pLiteral->n < 4) {
×
873
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
874
      return NULL;
×
875
    }
876
    char unit = pLiteral->z[pLiteral->n - 2];
×
877
    switch (unit) {
×
878
      case 'a':
×
879
      case 'b':
880
      case 'd':
881
      case 'h':
882
      case 'm':
883
      case 's':
884
      case 'u':
885
      case 'w':
886
        break;
×
887
      default:
×
888
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
889
        return NULL;
×
890
    }
891
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
×
892
      if (!isdigit(pLiteral->z[i])) {
×
893
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
894
        return NULL;
×
895
      }
896
    }
897
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
×
898
  } else {
899
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
×
900
  }
901
  if (!val->literal) {
×
902
    nodesDestroyNode((SNode*)val);
×
903
    pCxt->errCode = terrno;
×
904
    return NULL;
×
905
  }
906
  val->flag |= VALUE_FLAG_IS_TIME_OFFSET;
×
907
  val->translate = false;
×
908
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
×
909
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
×
910
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
911
  return (SNode*)val;
×
912
_err:
×
913
  return NULL;
×
914
}
915

916
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
65✔
917
  CHECK_PARSER_STATUS(pCxt);
65✔
918
  if (NULL == pCxt->pQueryCxt->db) {
65✔
919
    return NULL;
2✔
920
  }
921

922
  SValueNode* val = NULL;
63✔
923
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
63✔
924
  CHECK_MAKE_NODE(val);
63✔
925
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
63✔
926
  if (!val->literal) {
63✔
927
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
928
    nodesDestroyNode((SNode*)val);
×
929
    return NULL;
×
930
  }
931
  val->translate = false;
63✔
932
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
63✔
933
  val->node.resType.bytes = strlen(val->literal);
63✔
934
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
63✔
935
  return (SNode*)val;
63✔
936
_err:
×
937
  return NULL;
×
938
}
939

940
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
27✔
941
  CHECK_PARSER_STATUS(pCxt);
27✔
942
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
27✔
943
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
4✔
944
    return NULL;
4✔
945
  }
946
  SValueNode* val = NULL;
23✔
947
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
23✔
948
  CHECK_MAKE_NODE(val);
23✔
949
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
23✔
950
  if (!val->literal) {
23✔
951
    pCxt->errCode = terrno;
×
952
    nodesDestroyNode((SNode*)val);
×
953
    return NULL;
×
954
  }
955
  val->placeholderNo = ++pCxt->placeholderNo;
23✔
956
  if (NULL == pCxt->pPlaceholderValues) {
23✔
957
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
13✔
958
    if (NULL == pCxt->pPlaceholderValues) {
13✔
959
      nodesDestroyNode((SNode*)val);
×
960
      return NULL;
×
961
    }
962
  }
963
  if (NULL == taosArrayPush(pCxt->pPlaceholderValues, &val)) {
46✔
964
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
965
    nodesDestroyNode((SNode*)val);
×
966
    taosArrayDestroy(pCxt->pPlaceholderValues);
×
967
    return NULL;
×
968
  }
969
  return (SNode*)val;
23✔
970
_err:
×
971
  return NULL;
×
972
}
973

974
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
1,778✔
975
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
1,778✔
976
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
30✔
977
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
30✔
978
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
30✔
979
    nodesDestroyNode(pParam);
30✔
980
    return code;
30✔
981
  } else {
982
    return nodesListAppend(pCond->pParameterList, pParam);
1,748✔
983
  }
984
}
985

986
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
891✔
987
  CHECK_PARSER_STATUS(pCxt);
891✔
988
  SLogicConditionNode* cond = NULL;
891✔
989
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
891✔
990
  CHECK_MAKE_NODE(cond);
891✔
991
  cond->condType = type;
891✔
992
  cond->pParameterList = NULL;
891✔
993
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
891✔
994
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
892✔
995
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
892✔
996
  }
997
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
891✔
998
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
887✔
999
  }
1000
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
892✔
1001
    nodesDestroyNode((SNode*)cond);
×
1002
    return NULL;
×
1003
  }
1004
  return (SNode*)cond;
892✔
1005
_err:
×
1006
  nodesDestroyNode(pParam1);
×
1007
  nodesDestroyNode(pParam2);
×
1008
  return NULL;
×
1009
}
1010

1011
static uint8_t getMinusDataType(uint8_t orgType) {
234✔
1012
  switch (orgType) {
234✔
1013
    case TSDB_DATA_TYPE_UTINYINT:
215✔
1014
    case TSDB_DATA_TYPE_USMALLINT:
1015
    case TSDB_DATA_TYPE_UINT:
1016
    case TSDB_DATA_TYPE_UBIGINT:
1017
      return TSDB_DATA_TYPE_BIGINT;
215✔
1018
    default:
19✔
1019
      break;
19✔
1020
  }
1021
  return orgType;
19✔
1022
}
1023

1024
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
3,641✔
1025
  CHECK_PARSER_STATUS(pCxt);
3,641✔
1026
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
3,641✔
1027
    SValueNode* pVal = (SValueNode*)pLeft;
234✔
1028
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
234✔
1029
    if (!pNewLiteral) {
234✔
1030
      pCxt->errCode = terrno;
×
1031
      goto _err;
×
1032
    }
1033
    if ('+' == pVal->literal[0]) {
234✔
1034
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal + 1);
×
1035
    } else if ('-' == pVal->literal[0]) {
234✔
1036
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "%s", pVal->literal + 1);
×
1037
    } else {
1038
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal);
234✔
1039
    }
1040
    taosMemoryFree(pVal->literal);
234✔
1041
    pVal->literal = pNewLiteral;
233✔
1042
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
233✔
1043
    return pLeft;
234✔
1044
  }
1045
  SOperatorNode* op = NULL;
3,407✔
1046
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
3,407✔
1047
  CHECK_MAKE_NODE(op);
3,405✔
1048
  op->opType = type;
3,405✔
1049
  op->pLeft = pLeft;
3,405✔
1050
  op->pRight = pRight;
3,405✔
1051
  op->tz = pCxt->pQueryCxt->timezone;
3,405✔
1052
  op->charsetCxt = pCxt->pQueryCxt->charsetCxt;
3,405✔
1053
  return (SNode*)op;
3,405✔
1054
_err:
×
1055
  nodesDestroyNode(pLeft);
×
1056
  nodesDestroyNode(pRight);
×
1057
  return NULL;
×
1058
}
1059

1060
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
20✔
1061
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
20✔
1062
  CHECK_PARSER_STATUS(pCxt);
20✔
1063
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
20✔
1064
  CHECK_PARSER_STATUS(pCxt);
20✔
1065
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
20✔
1066
  CHECK_PARSER_STATUS(pCxt);
20✔
1067
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
20✔
1068
  CHECK_PARSER_STATUS(pCxt);
20✔
1069
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
20✔
1070
  CHECK_PARSER_STATUS(pCxt);
20✔
1071
  return pRet;
20✔
1072
_err:
×
1073
  nodesDestroyNode(pNew);
×
1074
  nodesDestroyNode(pGE);
×
1075
  nodesDestroyNode(pLE);
×
1076
  nodesDestroyNode(pExpr);
×
1077
  nodesDestroyNode(pLeft);
×
1078
  nodesDestroyNode(pRight);
×
1079
  return NULL;
×
1080
}
1081

1082
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
×
1083
  SNode *pNew = NULL, *pLT = NULL, *pGT = NULL;
×
1084
  CHECK_PARSER_STATUS(pCxt);
×
1085
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
×
1086
  CHECK_PARSER_STATUS(pCxt);
×
1087
  pLT = createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft);
×
1088
  CHECK_PARSER_STATUS(pCxt);
×
1089
  pGT = createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pNew, pRight);
×
1090
  CHECK_PARSER_STATUS(pCxt);
×
1091
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, pLT, pGT);
×
1092
  CHECK_PARSER_STATUS(pCxt);
×
1093
  return pRet;
×
1094
_err:
×
1095
  nodesDestroyNode(pNew);
×
1096
  nodesDestroyNode(pGT);
×
1097
  nodesDestroyNode(pLT);
×
1098
  nodesDestroyNode(pExpr);
×
1099
  nodesDestroyNode(pLeft);
×
1100
  nodesDestroyNode(pRight);
×
1101
  return NULL;
×
1102
}
1103

1104
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
1,507✔
1105
  CHECK_PARSER_STATUS(pCxt);
1,507✔
1106
  SColumnNode* pCol = NULL;
1,507✔
1107
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
1,507✔
1108
  CHECK_MAKE_NODE(pCol);
1,506✔
1109
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
1,506✔
1110
  if (NULL == pFuncName) {
1,506✔
1111
    tstrncpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME, TSDB_COL_NAME_LEN);
953✔
1112
  } else {
1113
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
553✔
1114
  }
1115
  pCol->isPrimTs = true;
1,506✔
1116
  return (SNode*)pCol;
1,506✔
1117
_err:
×
1118
  return NULL;
×
1119
}
1120

1121
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
8,229✔
1122
  CHECK_PARSER_STATUS(pCxt);
8,229✔
1123
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
8,229✔
1124
    return createPrimaryKeyCol(pCxt, pFuncName);
548✔
1125
  }
1126
  SFunctionNode* func = NULL;
7,681✔
1127
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
7,681✔
1128
  CHECK_MAKE_NODE(func);
7,679✔
1129
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
7,679✔
1130
  func->pParameterList = pParameterList;
7,679✔
1131
  func->tz = pCxt->pQueryCxt->timezone;
7,679✔
1132
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
7,679✔
1133
  return (SNode*)func;
7,679✔
1134
_err:
×
1135
  nodesDestroyList(pParameterList);
×
1136
  return NULL;
×
1137
}
1138

1139
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
18✔
1140
  SFunctionNode* func = NULL;
18✔
1141
  CHECK_PARSER_STATUS(pCxt);
18✔
1142
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
18✔
1143
  CHECK_MAKE_NODE(func);
18✔
1144
  tstrncpy(func->functionName, "cast", TSDB_FUNC_NAME_LEN);
18✔
1145
  func->node.resType = dt;
18✔
1146
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
18✔
1147
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
1✔
1148
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
17✔
1149
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
×
1150
  }
1151
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
18✔
1152
  CHECK_PARSER_STATUS(pCxt);
18✔
1153
  func->tz = pCxt->pQueryCxt->timezone;
18✔
1154
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
18✔
1155

1156
  return (SNode*)func;
18✔
1157
_err:
×
1158
  nodesDestroyNode((SNode*)func);
×
1159
  nodesDestroyNode(pExpr);
×
1160
  return NULL;
×
1161
}
1162

1163
SNode* createPositionFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
2✔
1164
  SFunctionNode* func = NULL;
2✔
1165
  CHECK_PARSER_STATUS(pCxt);
2✔
1166
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
2✔
1167
  CHECK_MAKE_NODE(func);
2✔
1168
  tstrncpy(func->functionName, "position", TSDB_FUNC_NAME_LEN);
2✔
1169
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
2✔
1170
  CHECK_PARSER_STATUS(pCxt);
2✔
1171
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
2✔
1172
  CHECK_PARSER_STATUS(pCxt);
2✔
1173
  return (SNode*)func;
2✔
1174
_err:
×
1175
  nodesDestroyNode((SNode*)func);
×
1176
  nodesDestroyNode(pExpr);
×
1177
  nodesDestroyNode(pExpr2);
×
1178
  return NULL;
×
1179
}
1180

1181
SNode* createTrimFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, ETrimType type) {
×
1182
  SFunctionNode* func = NULL;
×
1183
  CHECK_PARSER_STATUS(pCxt);
×
1184
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
×
1185
  CHECK_MAKE_NODE(func);
×
1186
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
×
1187
  func->trimType = type;
×
1188
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
×
1189
  CHECK_PARSER_STATUS(pCxt);
×
1190
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
×
1191
  return (SNode*)func;
×
1192
_err:
×
1193
  nodesDestroyNode((SNode*)func);
×
1194
  nodesDestroyNode(pExpr);
×
1195
  return NULL;
×
1196
}
1197

1198
SNode* createTrimFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, ETrimType type) {
2✔
1199
  SFunctionNode* func = NULL;
2✔
1200
  CHECK_PARSER_STATUS(pCxt);
2✔
1201
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
2✔
1202
  CHECK_MAKE_NODE(func);
2✔
1203
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
2✔
1204
  func->trimType = type;
2✔
1205
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
2✔
1206
  CHECK_PARSER_STATUS(pCxt);
2✔
1207
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
2✔
1208
  CHECK_PARSER_STATUS(pCxt);
2✔
1209
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
2✔
1210
  return (SNode*)func;
2✔
1211
_err:
×
1212
  nodesDestroyNode((SNode*)func);
×
1213
  nodesDestroyNode(pExpr);
×
1214
  nodesDestroyNode(pExpr2);
×
1215
  return NULL;
×
1216
}
1217

1218
SNode* createSubstrFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
×
1219
  SFunctionNode* func = NULL;
×
1220
  CHECK_PARSER_STATUS(pCxt);
×
1221
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
×
1222
  CHECK_MAKE_NODE(func);
×
1223
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
×
1224
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
×
1225
  CHECK_PARSER_STATUS(pCxt);
×
1226
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
×
1227
  CHECK_PARSER_STATUS(pCxt);
×
1228
  return (SNode*)func;
×
1229
_err:
×
1230
  nodesDestroyNode((SNode*)func);
×
1231
  nodesDestroyNode(pExpr);
×
1232
  nodesDestroyNode(pExpr2);
×
1233
  return NULL;
×
1234
}
1235

1236
SNode* createSubstrFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, SNode* pExpr3) {
×
1237
  SFunctionNode* func = NULL;
×
1238
  CHECK_PARSER_STATUS(pCxt);
×
1239
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
×
1240
  CHECK_MAKE_NODE(func);
×
1241
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
×
1242
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
×
1243
  CHECK_PARSER_STATUS(pCxt);
×
1244
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
×
1245
  CHECK_PARSER_STATUS(pCxt);
×
1246
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr3);
×
1247
  CHECK_PARSER_STATUS(pCxt);
×
1248
  return (SNode*)func;
×
1249
_err:
×
1250
  nodesDestroyNode((SNode*)func);
×
1251
  nodesDestroyNode(pExpr);
×
1252
  nodesDestroyNode(pExpr2);
×
1253
  nodesDestroyNode(pExpr3);
×
1254
  return NULL;
×
1255
}
1256

1257
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
446✔
1258
  SNodeListNode* list = NULL;
446✔
1259
  CHECK_PARSER_STATUS(pCxt);
446✔
1260
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
446✔
1261
  CHECK_MAKE_NODE(list);
446✔
1262
  list->pNodeList = pList;
446✔
1263
  return (SNode*)list;
446✔
1264
_err:
×
1265
  nodesDestroyList(pList);
×
1266
  return NULL;
×
1267
}
1268

1269
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
64✔
1270
  SNodeListNode* list = NULL;
64✔
1271
  CHECK_PARSER_STATUS(pCxt);
64✔
1272
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
64✔
1273
  CHECK_MAKE_NODE(list);
64✔
1274
  pCxt->errCode = nodesListMakeStrictAppend(&list->pNodeList, p1);
64✔
1275
  CHECK_PARSER_STATUS(pCxt);
64✔
1276
  pCxt->errCode = nodesListStrictAppend(list->pNodeList, p2);
64✔
1277
  CHECK_PARSER_STATUS(pCxt);
64✔
1278
  return (SNode*)list;
64✔
1279
_err:
×
1280
  nodesDestroyNode((SNode*)list);
×
1281
  nodesDestroyNode(p1);
×
1282
  nodesDestroyNode(p2);
×
1283
  return NULL;
×
1284
}
1285

1286
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
82,286✔
1287
  CHECK_PARSER_STATUS(pCxt);
82,286✔
1288
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
82,286✔
1289
  CHECK_NAME(checkTableName(pCxt, pTableName));
82,297✔
1290
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
82,322✔
1291
  SRealTableNode* realTable = NULL;
82,316✔
1292
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
82,316✔
1293
  CHECK_MAKE_NODE(realTable);
82,260✔
1294
  if (NULL != pDbName) {
82,260✔
1295
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
72,973✔
1296
  } else {
1297
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
9,287✔
1298
  }
1299
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
82,260✔
1300
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
95✔
1301
  } else {
1302
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
82,165✔
1303
  }
1304
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
82,260✔
1305
  return (SNode*)realTable;
82,260✔
1306
_err:
×
1307
  return NULL;
×
1308
}
1309

1310
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
202✔
1311
  CHECK_PARSER_STATUS(pCxt);
202✔
1312
  if (!checkTableName(pCxt, pTableAlias)) {
202✔
1313
    return NULL;
×
1314
  }
1315
  STempTableNode* tempTable = NULL;
202✔
1316
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
202✔
1317
  CHECK_MAKE_NODE(tempTable);
202✔
1318
  tempTable->pSubquery = pSubquery;
202✔
1319
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
202✔
1320
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
20✔
1321
  } else {
1322
    taosRandStr(tempTable->table.tableAlias, 8);
182✔
1323
  }
1324
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
202✔
1325
    tstrncpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
176✔
1326
    ((SSelectStmt*)pSubquery)->isSubquery = true;
176✔
1327
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
26✔
1328
    tstrncpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
26✔
1329
  }
1330
  return (SNode*)tempTable;
202✔
1331
_err:
×
1332
  nodesDestroyNode(pSubquery);
×
1333
  return NULL;
×
1334
}
1335

1336
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, EJoinSubType stype, SNode* pLeft, SNode* pRight,
77✔
1337
                           SNode* pJoinCond) {
1338
  CHECK_PARSER_STATUS(pCxt);
77✔
1339
  SJoinTableNode* joinTable = NULL;
77✔
1340
  pCxt->errCode = nodesMakeNode(QUERY_NODE_JOIN_TABLE, (SNode**)&joinTable);
77✔
1341
  CHECK_MAKE_NODE(joinTable);
77✔
1342
  joinTable->joinType = type;
77✔
1343
  joinTable->subType = stype;
77✔
1344
  joinTable->pLeft = pLeft;
77✔
1345
  joinTable->pRight = pRight;
77✔
1346
  joinTable->pOnCond = pJoinCond;
77✔
1347
  return (SNode*)joinTable;
77✔
1348
_err:
×
1349
  nodesDestroyNode(pLeft);
×
1350
  nodesDestroyNode(pRight);
×
1351
  nodesDestroyNode(pJoinCond);
×
1352
  return NULL;
×
1353
}
1354

1355
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
×
1356
  CHECK_PARSER_STATUS(pCxt);
×
1357
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
×
1358
  CHECK_NAME(checkViewName(pCxt, pViewName));
×
1359
  SViewNode* pView = NULL;
×
1360
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
×
1361
  CHECK_MAKE_NODE(pView);
×
1362
  if (NULL != pDbName) {
×
1363
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
×
1364
  } else {
1365
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
×
1366
  }
1367
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
×
1368
  return (SNode*)pView;
×
1369
_err:
×
1370
  return NULL;
×
1371
}
1372

1373
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) {
264✔
1374
  CHECK_PARSER_STATUS(pCxt);
264✔
1375
  SLimitNode* limitNode = NULL;
264✔
1376
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
264✔
1377
  CHECK_MAKE_NODE(limitNode);
264✔
1378
  limitNode->limit = (SValueNode*)pLimit;
264✔
1379
  if (NULL != pOffset) {
264✔
1380
    limitNode->offset = (SValueNode*)pOffset;
119✔
1381
  }
1382
  return (SNode*)limitNode;
264✔
1383
_err:
×
1384
  return NULL;
×
1385
}
1386

1387
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
395✔
1388
  CHECK_PARSER_STATUS(pCxt);
395✔
1389
  SOrderByExprNode* orderByExpr = NULL;
395✔
1390
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
395✔
1391
  CHECK_MAKE_NODE(orderByExpr);
395✔
1392
  orderByExpr->pExpr = pExpr;
395✔
1393
  orderByExpr->order = order;
395✔
1394
  if (NULL_ORDER_DEFAULT == nullOrder) {
395✔
1395
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
393✔
1396
  }
1397
  orderByExpr->nullOrder = nullOrder;
395✔
1398
  return (SNode*)orderByExpr;
395✔
1399
_err:
×
1400
  nodesDestroyNode(pExpr);
×
1401
  return NULL;
×
1402
}
1403

1404
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
11✔
1405
  CHECK_PARSER_STATUS(pCxt);
11✔
1406
  SSessionWindowNode* session = NULL;
11✔
1407
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
11✔
1408
  CHECK_MAKE_NODE(session);
11✔
1409
  session->pCol = (SColumnNode*)pCol;
11✔
1410
  session->pGap = (SValueNode*)pGap;
11✔
1411
  return (SNode*)session;
11✔
1412
_err:
×
1413
  nodesDestroyNode(pCol);
×
1414
  nodesDestroyNode(pGap);
×
1415
  return NULL;
×
1416
}
1417

1418
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pTrueForLimit) {
14✔
1419
  SStateWindowNode* state = NULL;
14✔
1420
  CHECK_PARSER_STATUS(pCxt);
14✔
1421
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
14✔
1422
  CHECK_MAKE_NODE(state);
14✔
1423
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
14✔
1424
  CHECK_MAKE_NODE(state->pCol);
14✔
1425
  state->pExpr = pExpr;
14✔
1426
  state->pTrueForLimit = pTrueForLimit;
14✔
1427
  return (SNode*)state;
14✔
1428
_err:
×
1429
  nodesDestroyNode((SNode*)state);
×
1430
  nodesDestroyNode(pExpr);
×
1431
  nodesDestroyNode(pTrueForLimit);
×
1432
  return NULL;
×
1433
}
1434

1435
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond, SNode* pTrueForLimit) {
2✔
1436
  SEventWindowNode* pEvent = NULL;
2✔
1437
  CHECK_PARSER_STATUS(pCxt);
2✔
1438
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
2✔
1439
  CHECK_MAKE_NODE(pEvent);
2✔
1440
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
2✔
1441
  CHECK_MAKE_NODE(pEvent->pCol);
2✔
1442
  pEvent->pStartCond = pStartCond;
2✔
1443
  pEvent->pEndCond = pEndCond;
2✔
1444
  pEvent->pTrueForLimit = pTrueForLimit;
2✔
1445
  return (SNode*)pEvent;
2✔
1446
_err:
×
1447
  nodesDestroyNode((SNode*)pEvent);
×
1448
  nodesDestroyNode(pStartCond);
×
1449
  nodesDestroyNode(pEndCond);
×
1450
  nodesDestroyNode(pTrueForLimit);
×
1451
  return NULL;
×
1452
}
1453

1454
SNode* createCountWindowNode(SAstCreateContext* pCxt, const SToken* pCountToken, const SToken* pSlidingToken) {
×
1455
  SCountWindowNode* pCount = NULL;
×
1456
  CHECK_PARSER_STATUS(pCxt);
×
1457
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
×
1458
  CHECK_MAKE_NODE(pCount);
×
1459
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1460
  CHECK_MAKE_NODE(pCount->pCol);
×
1461
  pCount->windowCount = taosStr2Int64(pCountToken->z, NULL, 10);
×
1462
  pCount->windowSliding = taosStr2Int64(pSlidingToken->z, NULL, 10);
×
1463
  return (SNode*)pCount;
×
1464
_err:
×
1465
  nodesDestroyNode((SNode*)pCount);
×
1466
  return NULL;
×
1467
}
1468

1469
SNode* createAnomalyWindowNode(SAstCreateContext* pCxt, SNode* pExpr, const SToken* pFuncOpt) {
×
1470
  SAnomalyWindowNode* pAnomaly = NULL;
×
1471
  CHECK_PARSER_STATUS(pCxt);
×
1472
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ANOMALY_WINDOW, (SNode**)&pAnomaly);
×
1473
  CHECK_MAKE_NODE(pAnomaly);
×
1474
  pAnomaly->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1475
  CHECK_MAKE_NODE(pAnomaly->pCol);
×
1476
  pAnomaly->pExpr = pExpr;
×
1477
  if (pFuncOpt == NULL) {
×
1478
    tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
1479
  } else {
1480
    (void)trimString(pFuncOpt->z, pFuncOpt->n, pAnomaly->anomalyOpt, sizeof(pAnomaly->anomalyOpt));
×
1481
  }
1482
  return (SNode*)pAnomaly;
×
1483
_err:
×
1484
  nodesDestroyNode((SNode*)pAnomaly);
×
1485
  return NULL;
×
1486
}
1487

1488
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
255✔
1489
                                SNode* pFill) {
1490
  SIntervalWindowNode* interval = NULL;
255✔
1491
  CHECK_PARSER_STATUS(pCxt);
255✔
1492
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
255✔
1493
  CHECK_MAKE_NODE(interval);
255✔
1494
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
255✔
1495
  CHECK_MAKE_NODE(interval->pCol);
255✔
1496
  interval->pInterval = pInterval;
255✔
1497
  interval->pOffset = pOffset;
255✔
1498
  interval->pSliding = pSliding;
255✔
1499
  interval->pFill = pFill;
255✔
1500
  TAOS_SET_OBJ_ALIGNED(&interval->timeRange, TSWINDOW_INITIALIZER);
255✔
1501
  interval->timezone = pCxt->pQueryCxt->timezone;
255✔
1502
  return (SNode*)interval;
255✔
1503
_err:
×
1504
  nodesDestroyNode((SNode*)interval);
×
1505
  nodesDestroyNode(pInterval);
×
1506
  nodesDestroyNode(pOffset);
×
1507
  nodesDestroyNode(pSliding);
×
1508
  nodesDestroyNode(pFill);
×
1509
  return NULL;
×
1510
}
1511

1512
SNode* createWindowOffsetNode(SAstCreateContext* pCxt, SNode* pStartOffset, SNode* pEndOffset) {
×
1513
  SWindowOffsetNode* winOffset = NULL;
×
1514
  CHECK_PARSER_STATUS(pCxt);
×
1515
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WINDOW_OFFSET, (SNode**)&winOffset);
×
1516
  CHECK_MAKE_NODE(winOffset);
×
1517
  winOffset->pStartOffset = pStartOffset;
×
1518
  winOffset->pEndOffset = pEndOffset;
×
1519
  return (SNode*)winOffset;
×
1520
_err:
×
1521
  nodesDestroyNode((SNode*)winOffset);
×
1522
  nodesDestroyNode(pStartOffset);
×
1523
  nodesDestroyNode(pEndOffset);
×
1524
  return NULL;
×
1525
}
1526

1527
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
39✔
1528
  SFillNode* fill = NULL;
39✔
1529
  CHECK_PARSER_STATUS(pCxt);
39✔
1530
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
39✔
1531
  CHECK_MAKE_NODE(fill);
39✔
1532
  fill->mode = mode;
39✔
1533
  fill->pValues = pValues;
39✔
1534
  fill->pWStartTs = NULL;
39✔
1535
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&(fill->pWStartTs));
39✔
1536
  CHECK_MAKE_NODE(fill->pWStartTs);
39✔
1537
  tstrncpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart", TSDB_FUNC_NAME_LEN);
39✔
1538
  return (SNode*)fill;
39✔
1539
_err:
×
1540
  nodesDestroyNode((SNode*)fill);
×
1541
  nodesDestroyNode(pValues);
×
1542
  return NULL;
×
1543
}
1544

1545
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
306✔
1546
  SGroupingSetNode* groupingSet = NULL;
306✔
1547
  CHECK_PARSER_STATUS(pCxt);
306✔
1548
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
306✔
1549
  CHECK_MAKE_NODE(groupingSet);
306✔
1550
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
306✔
1551
  groupingSet->pParameterList = NULL;
306✔
1552
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
306✔
1553
  CHECK_PARSER_STATUS(pCxt);
306✔
1554
  return (SNode*)groupingSet;
306✔
1555
_err:
×
1556
  nodesDestroyNode((SNode*)groupingSet);
×
1557
  nodesDestroyNode(pNode);
×
1558
  return NULL;
×
1559
}
1560

1561
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
8✔
1562
  CHECK_PARSER_STATUS(pCxt);
8✔
1563
  if (NULL == pInterval) {
8✔
1564
    if (pEnd && nodeType(pEnd) == QUERY_NODE_VALUE && ((SValueNode*)pEnd)->flag & VALUE_FLAG_IS_DURATION) {
8✔
1565
      return createInterpTimeAround(pCxt, pStart, NULL, pEnd);
×
1566
    }
1567
    return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
8✔
1568
  }
1569

1570
  return createInterpTimeAround(pCxt, pStart, pEnd, pInterval);
×
1571
  
1572
_err:
×
1573

1574
  nodesDestroyNode(pStart);
×
1575
  nodesDestroyNode(pEnd);
×
1576
  return NULL;
×
1577
}
1578

1579
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
×
1580
  CHECK_PARSER_STATUS(pCxt);
×
1581
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
×
1582
_err:
×
1583
  nodesDestroyNode(pPoint);
×
1584
  return NULL;
×
1585
}
1586

1587
SNode* createInterpTimeAround(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
×
1588
  CHECK_PARSER_STATUS(pCxt);
×
1589
  SRangeAroundNode* pAround = NULL;
×
1590
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RANGE_AROUND, (SNode**)&pAround);
×
1591
  CHECK_PARSER_STATUS(pCxt);
×
1592
  if (NULL == pEnd) {
×
1593
    pAround->pRange = createInterpTimePoint(pCxt, pStart);
×
1594
  } else {
1595
    pAround->pRange = createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
×
1596
  }
1597
  pAround->pInterval = pInterval;
×
1598
  CHECK_PARSER_STATUS(pCxt);
×
1599
  return (SNode*)pAround;
×
1600
_err:
×
1601
  return NULL;
×
1602
}
1603

1604
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
18✔
1605
  CHECK_PARSER_STATUS(pCxt);
18✔
1606
  SWhenThenNode* pWhenThen = NULL;
18✔
1607
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
18✔
1608
  CHECK_MAKE_NODE(pWhenThen);
18✔
1609
  pWhenThen->pWhen = pWhen;
18✔
1610
  pWhenThen->pThen = pThen;
18✔
1611
  return (SNode*)pWhenThen;
18✔
1612
_err:
×
1613
  nodesDestroyNode(pWhen);
×
1614
  nodesDestroyNode(pThen);
×
1615
  return NULL;
×
1616
}
1617

1618
SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
13✔
1619
  CHECK_PARSER_STATUS(pCxt);
13✔
1620
  SCaseWhenNode* pCaseWhen = NULL;
13✔
1621
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CASE_WHEN, (SNode**)&pCaseWhen);
13✔
1622
  CHECK_MAKE_NODE(pCaseWhen);
13✔
1623
  pCaseWhen->pCase = pCase;
13✔
1624
  pCaseWhen->pWhenThenList = pWhenThenList;
13✔
1625
  pCaseWhen->pElse = pElse;
13✔
1626
  pCaseWhen->tz = pCxt->pQueryCxt->timezone;
13✔
1627
  pCaseWhen->charsetCxt = pCxt->pQueryCxt->charsetCxt;
13✔
1628
  return (SNode*)pCaseWhen;
13✔
1629
_err:
×
1630
  nodesDestroyNode(pCase);
×
1631
  nodesDestroyList(pWhenThenList);
×
1632
  nodesDestroyNode(pElse);
×
1633
  return NULL;
×
1634
}
1635

1636
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
286✔
1637
  CHECK_PARSER_STATUS(pCxt);
286✔
1638
  trimEscape(pAlias);
286✔
1639
  SExprNode* pExpr = (SExprNode*)pNode;
286✔
1640
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
286✔
1641
  strncpy(pExpr->aliasName, pAlias->z, len);
286✔
1642
  pExpr->aliasName[len] = '\0';
286✔
1643
  strncpy(pExpr->userAlias, pAlias->z, len);
286✔
1644
  pExpr->userAlias[len] = '\0';
286✔
1645
  pExpr->asAlias = true;
286✔
1646
  return pNode;
286✔
1647
_err:
×
1648
  nodesDestroyNode(pNode);
×
1649
  return NULL;
×
1650
}
1651

1652
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
7,361✔
1653
  CHECK_PARSER_STATUS(pCxt);
7,361✔
1654
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
7,361✔
1655
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
7,363✔
1656
  }
1657
  return pStmt;
7,361✔
1658
_err:
×
1659
  nodesDestroyNode(pStmt);
×
1660
  nodesDestroyNode(pWhere);
×
1661
  return NULL;
×
1662
}
1663

1664
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
7,361✔
1665
  CHECK_PARSER_STATUS(pCxt);
7,361✔
1666
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
7,361✔
1667
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
7,365✔
1668
  }
1669
  return pStmt;
7,361✔
1670
_err:
×
1671
  nodesDestroyNode(pStmt);
×
1672
  nodesDestroyList(pPartitionByList);
×
1673
  return NULL;
×
1674
}
1675

1676
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
7,358✔
1677
  CHECK_PARSER_STATUS(pCxt);
7,358✔
1678
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
7,358✔
1679
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
7,361✔
1680
  }
1681
  return pStmt;
7,358✔
1682
_err:
×
1683
  nodesDestroyNode(pStmt);
×
1684
  nodesDestroyNode(pWindow);
×
1685
  return NULL;
×
1686
}
1687

1688
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
7,361✔
1689
  CHECK_PARSER_STATUS(pCxt);
7,361✔
1690
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
7,361✔
1691
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
7,365✔
1692
  }
1693
  return pStmt;
7,361✔
1694
_err:
×
1695
  nodesDestroyNode(pStmt);
×
1696
  nodesDestroyList(pGroupByList);
×
1697
  return NULL;
×
1698
}
1699

1700
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
7,358✔
1701
  CHECK_PARSER_STATUS(pCxt);
7,358✔
1702
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
7,358✔
1703
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
7,359✔
1704
  }
1705
  return pStmt;
7,358✔
1706
_err:
×
1707
  nodesDestroyNode(pStmt);
×
1708
  nodesDestroyNode(pHaving);
×
1709
  return NULL;
×
1710
}
1711

1712
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
7,306✔
1713
  CHECK_PARSER_STATUS(pCxt);
7,306✔
1714
  if (NULL == pOrderByList) {
7,306✔
1715
    return pStmt;
6,962✔
1716
  }
1717
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
344✔
1718
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
338✔
1719
  } else {
1720
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
6✔
1721
  }
1722
  return pStmt;
344✔
1723
_err:
×
1724
  nodesDestroyNode(pStmt);
×
1725
  nodesDestroyList(pOrderByList);
×
1726
  return NULL;
×
1727
}
1728

1729
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
7,306✔
1730
  CHECK_PARSER_STATUS(pCxt);
7,306✔
1731
  if (NULL == pSlimit) {
7,306✔
1732
    return pStmt;
7,303✔
1733
  }
1734
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
3✔
1735
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
6✔
1736
  }
1737
  return pStmt;
3✔
1738
_err:
×
1739
  nodesDestroyNode(pStmt);
×
1740
  nodesDestroyNode(pSlimit);
×
1741
  return NULL;
×
1742
}
1743

1744
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
7,303✔
1745
  CHECK_PARSER_STATUS(pCxt);
7,303✔
1746
  if (NULL == pLimit) {
7,303✔
1747
    return pStmt;
7,051✔
1748
  }
1749
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
252✔
1750
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
255✔
1751
  } else {
1752
    ((SSetOperator*)pStmt)->pLimit = pLimit;
×
1753
  }
1754
  return pStmt;
252✔
1755
_err:
×
1756
  nodesDestroyNode(pStmt);
×
1757
  nodesDestroyNode(pLimit);
×
1758
  return NULL;
×
1759
}
1760

1761
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
7,359✔
1762
  CHECK_PARSER_STATUS(pCxt);
7,359✔
1763
  SSelectStmt* pSelect = (SSelectStmt*)pStmt;
7,359✔
1764
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
7,359✔
1765
    if (pRange && nodeType(pRange) == QUERY_NODE_RANGE_AROUND) {
7,364✔
1766
      pSelect->pRangeAround = pRange;
×
1767
      SRangeAroundNode* pAround = (SRangeAroundNode*)pRange;
×
1768
      TSWAP(pSelect->pRange, pAround->pRange);
×
1769
    } else {
1770
      pSelect->pRange = pRange;
7,364✔
1771
    }
1772
  }
1773
  return pStmt;
7,359✔
1774
_err:
×
1775
  nodesDestroyNode(pStmt);
×
1776
  nodesDestroyNode(pRange);
×
1777
  return NULL;
×
1778
}
1779

1780
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
7,355✔
1781
  CHECK_PARSER_STATUS(pCxt);
7,355✔
1782
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
7,355✔
1783
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
7,360✔
1784
  }
1785
  return pStmt;
7,355✔
1786
_err:
×
1787
  nodesDestroyNode(pStmt);
×
1788
  nodesDestroyNode(pEvery);
×
1789
  return NULL;
×
1790
}
1791

1792
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
7,353✔
1793
  CHECK_PARSER_STATUS(pCxt);
7,353✔
1794
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
7,353✔
1795
    SFillNode* pFillClause = (SFillNode*)pFill;
8✔
1796
    nodesDestroyNode(pFillClause->pWStartTs);
8✔
1797
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
8✔
1798
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
8✔
1799
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
8✔
1800
  }
1801
  return pStmt;
7,353✔
1802
_err:
×
1803
  nodesDestroyNode(pStmt);
×
1804
  nodesDestroyNode(pFill);
×
1805
  return NULL;
×
1806
}
1807

1808
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
23✔
1809
  CHECK_PARSER_STATUS(pCxt);
23✔
1810
  if (NULL == pJLimit) {
23✔
1811
    return pJoin;
23✔
1812
  }
1813
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
×
1814
  pJoinNode->pJLimit = pJLimit;
×
1815

1816
  return pJoin;
×
1817
_err:
×
1818
  nodesDestroyNode(pJoin);
×
1819
  nodesDestroyNode(pJLimit);
×
1820
  return NULL;
×
1821
}
1822

1823
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
23✔
1824
  CHECK_PARSER_STATUS(pCxt);
23✔
1825
  if (NULL == pWinOffset) {
23✔
1826
    return pJoin;
23✔
1827
  }
1828
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
×
1829
  pJoinNode->pWindowOffset = pWinOffset;
×
1830

1831
  return pJoin;
×
1832
_err:
×
1833
  nodesDestroyNode(pJoin);
×
1834
  nodesDestroyNode(pWinOffset);
×
1835
  return NULL;
×
1836
}
1837

1838
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
7,362✔
1839
                        SNodeList* pHint) {
1840
  CHECK_PARSER_STATUS(pCxt);
7,362✔
1841
  SNode* select = NULL;
7,362✔
1842
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
7,362✔
1843
  CHECK_MAKE_NODE(select);
7,362✔
1844
  return select;
7,362✔
1845
_err:
×
1846
  nodesDestroyList(pProjectionList);
×
1847
  nodesDestroyNode(pTable);
×
1848
  nodesDestroyList(pHint);
×
1849
  return NULL;
×
1850
}
1851

1852
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
7,359✔
1853
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
7,359✔
1854
    if (pCxt->pQueryCxt->biMode) {
7,364✔
1855
      ((SSelectStmt*)pStmt)->tagScan = true;
3✔
1856
    } else {
1857
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
7,361✔
1858
    }
1859
  }
1860
  return pStmt;
7,359✔
1861
}
1862

1863
static void setSubquery(SNode* pStmt) {
126✔
1864
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
126✔
1865
    ((SSelectStmt*)pStmt)->isSubquery = true;
124✔
1866
  }
1867
}
126✔
1868

1869
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
63✔
1870
  CHECK_PARSER_STATUS(pCxt);
63✔
1871
  SSetOperator* setOp = NULL;
63✔
1872
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
63✔
1873
  CHECK_MAKE_NODE(setOp);
63✔
1874
  setOp->opType = type;
63✔
1875
  setOp->pLeft = pLeft;
63✔
1876
  setSubquery(setOp->pLeft);
63✔
1877
  setOp->pRight = pRight;
63✔
1878
  setSubquery(setOp->pRight);
63✔
1879
  snprintf(setOp->stmtName, TSDB_TABLE_NAME_LEN, "%p", setOp);
63✔
1880
  return (SNode*)setOp;
63✔
1881
_err:
×
1882
  nodesDestroyNode(pLeft);
×
1883
  nodesDestroyNode(pRight);
×
1884
  return NULL;
×
1885
}
1886

1887
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
442✔
1888
  if (!pOptions->walRetentionPeriodIsSet) {
442✔
1889
    pOptions->walRetentionPeriod =
441✔
1890
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
1891
  }
1892
  if (!pOptions->walRetentionSizeIsSet) {
442✔
1893
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
441✔
1894
  }
1895
  if (!pOptions->walRollPeriodIsSet) {
442✔
1896
    pOptions->walRollPeriod =
442✔
1897
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
1898
  }
1899
}
442✔
1900

1901
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
384✔
1902
  CHECK_PARSER_STATUS(pCxt);
384✔
1903
  SDatabaseOptions* pOptions = NULL;
384✔
1904
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
384✔
1905
  CHECK_MAKE_NODE(pOptions);
384✔
1906
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
384✔
1907
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
384✔
1908
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
384✔
1909
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
384✔
1910
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
384✔
1911
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
384✔
1912
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
384✔
1913
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
384✔
1914
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
384✔
1915
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
384✔
1916
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
384✔
1917
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
384✔
1918
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
384✔
1919
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
384✔
1920
  pOptions->precision = TSDB_DEFAULT_PRECISION;
384✔
1921
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
384✔
1922
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
384✔
1923
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
384✔
1924
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
384✔
1925
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
384✔
1926
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
384✔
1927
  updateWalOptionsDefault(pOptions);
384✔
1928
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
384✔
1929
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
384✔
1930
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
384✔
1931
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
384✔
1932
  pOptions->s3ChunkSize = TSDB_DEFAULT_S3_CHUNK_SIZE;
384✔
1933
  pOptions->s3KeepLocal = TSDB_DEFAULT_S3_KEEP_LOCAL;
384✔
1934
  pOptions->s3Compact = TSDB_DEFAULT_S3_COMPACT;
384✔
1935
  pOptions->withArbitrator = TSDB_DEFAULT_DB_WITH_ARBITRATOR;
384✔
1936
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
384✔
1937
  pOptions->dnodeListStr[0] = 0;
384✔
1938
  pOptions->compactInterval = TSDB_DEFAULT_COMPACT_INTERVAL;
384✔
1939
  pOptions->compactStartTime = TSDB_DEFAULT_COMPACT_START_TIME;
384✔
1940
  pOptions->compactEndTime = TSDB_DEFAULT_COMPACT_END_TIME;
384✔
1941
  pOptions->compactTimeOffset = TSDB_DEFAULT_COMPACT_TIME_OFFSET;
384✔
1942
  return (SNode*)pOptions;
384✔
1943
_err:
×
1944
  return NULL;
×
1945
}
1946

1947
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
212✔
1948
  CHECK_PARSER_STATUS(pCxt);
212✔
1949
  SDatabaseOptions* pOptions = NULL;
212✔
1950
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
212✔
1951
  CHECK_MAKE_NODE(pOptions);
212✔
1952
  pOptions->buffer = -1;
212✔
1953
  pOptions->cacheModel = -1;
212✔
1954
  pOptions->cacheLastSize = -1;
212✔
1955
  pOptions->compressionLevel = -1;
212✔
1956
  pOptions->daysPerFile = -1;
212✔
1957
  pOptions->fsyncPeriod = -1;
212✔
1958
  pOptions->maxRowsPerBlock = -1;
212✔
1959
  pOptions->minRowsPerBlock = -1;
212✔
1960
  pOptions->keep[0] = -1;
212✔
1961
  pOptions->keep[1] = -1;
212✔
1962
  pOptions->keep[2] = -1;
212✔
1963
  pOptions->pages = -1;
212✔
1964
  pOptions->pagesize = -1;
212✔
1965
  pOptions->tsdbPageSize = -1;
212✔
1966
  pOptions->precision = -1;
212✔
1967
  pOptions->replica = -1;
212✔
1968
  pOptions->strict = -1;
212✔
1969
  pOptions->walLevel = -1;
212✔
1970
  pOptions->numOfVgroups = -1;
212✔
1971
  pOptions->singleStable = -1;
212✔
1972
  pOptions->schemaless = -1;
212✔
1973
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
212✔
1974
  pOptions->walRetentionSize = -2;    // -1 is a valid value
212✔
1975
  pOptions->walRollPeriod = -1;
212✔
1976
  pOptions->walSegmentSize = -1;
212✔
1977
  pOptions->sstTrigger = -1;
212✔
1978
  pOptions->tablePrefix = -1;
212✔
1979
  pOptions->tableSuffix = -1;
212✔
1980
  pOptions->s3ChunkSize = -1;
212✔
1981
  pOptions->s3KeepLocal = -1;
212✔
1982
  pOptions->s3Compact = -1;
212✔
1983
  pOptions->withArbitrator = -1;
212✔
1984
  pOptions->encryptAlgorithm = -1;
212✔
1985
  pOptions->dnodeListStr[0] = 0;
212✔
1986
  pOptions->compactInterval = -1;
212✔
1987
  pOptions->compactStartTime = -1;
212✔
1988
  pOptions->compactEndTime = -1;
212✔
1989
  pOptions->compactTimeOffset = -1;
212✔
1990
  return (SNode*)pOptions;
212✔
1991
_err:
×
1992
  return NULL;
×
1993
}
1994

1995
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
1,209✔
1996
                                    bool alter) {
1997
  CHECK_PARSER_STATUS(pCxt);
1,209✔
1998
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
1,209✔
1999
  switch (type) {
1,209✔
2000
    case DB_OPTION_BUFFER:
44✔
2001
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
44✔
2002
      break;
44✔
2003
    case DB_OPTION_CACHEMODEL:
34✔
2004
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
34✔
2005
      break;
34✔
2006
    case DB_OPTION_CACHESIZE:
34✔
2007
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
34✔
2008
      break;
34✔
2009
    case DB_OPTION_COMP:
22✔
2010
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
22✔
2011
      break;
22✔
2012
    case DB_OPTION_DAYS: {
128✔
2013
      SToken* pToken = pVal;
128✔
2014
      if (TK_NK_INTEGER == pToken->type) {
128✔
2015
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
27✔
2016
      } else {
2017
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
101✔
2018
      }
2019
      break;
128✔
2020
    }
2021
    case DB_OPTION_FSYNC:
24✔
2022
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
24✔
2023
      break;
24✔
2024
    case DB_OPTION_MAXROWS:
22✔
2025
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
22✔
2026
      break;
22✔
2027
    case DB_OPTION_MINROWS:
40✔
2028
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
40✔
2029
      break;
40✔
2030
    case DB_OPTION_KEEP:
116✔
2031
      pDbOptions->pKeep = pVal;
116✔
2032
      break;
116✔
2033
    case DB_OPTION_PAGES:
24✔
2034
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
24✔
2035
      break;
24✔
2036
    case DB_OPTION_PAGESIZE:
4✔
2037
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4✔
2038
      break;
4✔
2039
    case DB_OPTION_TSDB_PAGESIZE:
5✔
2040
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
5✔
2041
      break;
5✔
2042
    case DB_OPTION_PRECISION:
90✔
2043
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
90✔
2044
      break;
90✔
2045
    case DB_OPTION_REPLICA:
72✔
2046
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
72✔
2047
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
72✔
2048
      if (!alter) {
72✔
2049
        updateWalOptionsDefault(pDbOptions);
58✔
2050
      }
2051
      break;
72✔
2052
    case DB_OPTION_STRICT:
×
2053
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
2054
      break;
×
2055
    case DB_OPTION_WAL:
41✔
2056
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
41✔
2057
      break;
41✔
2058
    case DB_OPTION_VGROUPS:
187✔
2059
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
187✔
2060
      break;
187✔
2061
    case DB_OPTION_SINGLE_STABLE:
4✔
2062
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
4✔
2063
      break;
4✔
2064
    case DB_OPTION_RETENTIONS:
28✔
2065
      pDbOptions->pRetentions = pVal;
28✔
2066
      break;
28✔
2067
    case DB_OPTION_WAL_RETENTION_PERIOD:
39✔
2068
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
39✔
2069
      pDbOptions->walRetentionPeriodIsSet = true;
39✔
2070
      break;
39✔
2071
    case DB_OPTION_WAL_RETENTION_SIZE:
22✔
2072
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
22✔
2073
      pDbOptions->walRetentionSizeIsSet = true;
22✔
2074
      break;
22✔
2075
    case DB_OPTION_WAL_ROLL_PERIOD:
4✔
2076
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4✔
2077
      pDbOptions->walRollPeriodIsSet = true;
4✔
2078
      break;
4✔
2079
    case DB_OPTION_WAL_SEGMENT_SIZE:
4✔
2080
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4✔
2081
      break;
4✔
2082
    case DB_OPTION_STT_TRIGGER:
28✔
2083
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
28✔
2084
      break;
28✔
2085
    case DB_OPTION_TABLE_PREFIX: {
4✔
2086
      SValueNode* pNode = (SValueNode*)pVal;
4✔
2087
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
4✔
2088
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
4✔
2089
      } else {
2090
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
2091
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2092
      }
2093
      nodesDestroyNode((SNode*)pNode);
4✔
2094
      break;
4✔
2095
    }
2096
    case DB_OPTION_TABLE_SUFFIX: {
4✔
2097
      SValueNode* pNode = (SValueNode*)pVal;
4✔
2098
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
4✔
2099
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
4✔
2100
      } else {
2101
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
2102
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2103
      }
2104
      nodesDestroyNode((SNode*)pNode);
4✔
2105
      break;
4✔
2106
    }
2107
    case DB_OPTION_S3_CHUNKPAGES:
63✔
2108
      pDbOptions->s3ChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
63✔
2109
      break;
63✔
2110
    case DB_OPTION_S3_KEEPLOCAL: {
64✔
2111
      SToken* pToken = pVal;
64✔
2112
      if (TK_NK_INTEGER == pToken->type) {
64✔
2113
        pDbOptions->s3KeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
62✔
2114
      } else {
2115
        pDbOptions->s3KeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
2✔
2116
      }
2117
      break;
64✔
2118
    }
2119
    case DB_OPTION_S3_COMPACT:
52✔
2120
      pDbOptions->s3Compact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
52✔
2121
      break;
52✔
2122
    case DB_OPTION_KEEP_TIME_OFFSET:
2✔
2123
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
2✔
2124
        pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
2✔
2125
      } else {
2126
        pDbOptions->pKeepTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
×
2127
      }      
2128
      break;
2✔
2129
    case DB_OPTION_ENCRYPT_ALGORITHM:
×
2130
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
×
2131
      pDbOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
×
2132
      break;
×
2133
    case DB_OPTION_DNODES:
×
2134
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
×
2135
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
×
2136
                 TSDB_DNODE_LIST_LEN);
2137
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2138
      } else {
2139
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
×
2140
      }
2141
      break;
×
2142
    case DB_OPTION_COMPACT_INTERVAL:
×
2143
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
×
2144
        pDbOptions->compactInterval = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2145
      } else {
2146
        pDbOptions->pCompactIntervalNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
×
2147
      }
2148
      break;
×
2149
    case DB_OPTION_COMPACT_TIME_RANGE:
×
2150
      pDbOptions->pCompactTimeRangeList = pVal;
×
2151
      break;
×
2152
    case DB_OPTION_COMPACT_TIME_OFFSET:
×
2153
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
×
2154
        pDbOptions->compactTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2155
      } else {
2156
        pDbOptions->pCompactTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
×
2157
      }
2158
      break;
×
2159
    default:
4✔
2160
      break;
4✔
2161
  }
2162
  return pOptions;
1,209✔
2163
_err:
×
2164
  nodesDestroyNode(pOptions);
×
2165
  return NULL;
×
2166
}
2167

2168

2169

2170
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
957✔
2171
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
957✔
2172
}
2173

2174
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
252✔
2175
  CHECK_PARSER_STATUS(pCxt);
252✔
2176
  switch (pAlterOption->type) {
252✔
2177
    case DB_OPTION_KEEP:
52✔
2178
    case DB_OPTION_RETENTIONS:
2179
    case DB_OPTION_COMPACT_TIME_RANGE:
2180
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
52✔
2181
    default:
200✔
2182
      break;
200✔
2183
  }
2184
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
200✔
2185
_err:
×
2186
  nodesDestroyNode(pOptions);
×
2187
  nodesDestroyList(pAlterOption->pList);
×
2188
  return NULL;
×
2189
}
2190

2191
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
381✔
2192
  CHECK_PARSER_STATUS(pCxt);
381✔
2193
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
381✔
2194
  SCreateDatabaseStmt* pStmt = NULL;
381✔
2195
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
381✔
2196
  CHECK_MAKE_NODE(pStmt);
381✔
2197
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
381✔
2198
  pStmt->ignoreExists = ignoreExists;
381✔
2199
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
381✔
2200
  return (SNode*)pStmt;
381✔
2201
_err:
×
2202
  nodesDestroyNode(pOptions);
×
2203
  return NULL;
×
2204
}
2205

2206
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
360✔
2207
  CHECK_PARSER_STATUS(pCxt);
360✔
2208
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
360✔
2209
  SDropDatabaseStmt* pStmt = NULL;
360✔
2210
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
360✔
2211
  CHECK_MAKE_NODE(pStmt);
360✔
2212
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
360✔
2213
  pStmt->ignoreNotExists = ignoreNotExists;
360✔
2214
  return (SNode*)pStmt;
360✔
2215
_err:
×
2216
  return NULL;
×
2217
}
2218

2219
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
212✔
2220
  CHECK_PARSER_STATUS(pCxt);
212✔
2221
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
212✔
2222
  SAlterDatabaseStmt* pStmt = NULL;
212✔
2223
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
212✔
2224
  CHECK_MAKE_NODE(pStmt);
212✔
2225
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
212✔
2226
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
212✔
2227
  return (SNode*)pStmt;
212✔
2228
_err:
×
2229
  nodesDestroyNode(pOptions);
×
2230
  return NULL;
×
2231
}
2232

2233
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
77✔
2234
  CHECK_PARSER_STATUS(pCxt);
77✔
2235
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
77✔
2236
  SFlushDatabaseStmt* pStmt = NULL;
77✔
2237
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
77✔
2238
  CHECK_MAKE_NODE(pStmt);
77✔
2239
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
77✔
2240
  return (SNode*)pStmt;
77✔
2241
_err:
×
2242
  return NULL;
×
2243
}
2244

2245
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
8✔
2246
  CHECK_PARSER_STATUS(pCxt);
8✔
2247
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
8✔
2248
  STrimDatabaseStmt* pStmt = NULL;
8✔
2249
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
8✔
2250
  CHECK_MAKE_NODE(pStmt);
8✔
2251
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
8✔
2252
  pStmt->maxSpeed = maxSpeed;
8✔
2253
  return (SNode*)pStmt;
8✔
2254
_err:
×
2255
  return NULL;
×
2256
}
2257

2258
SNode* createS3MigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
3✔
2259
  CHECK_PARSER_STATUS(pCxt);
3✔
2260
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
3✔
2261
  SS3MigrateDatabaseStmt* pStmt = NULL;
3✔
2262
  pCxt->errCode = nodesMakeNode(QUERY_NODE_S3MIGRATE_DATABASE_STMT, (SNode**)&pStmt);
3✔
2263
  CHECK_MAKE_NODE(pStmt);
3✔
2264
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
3✔
2265
  return (SNode*)pStmt;
3✔
2266
_err:
×
2267
  return NULL;
×
2268
}
2269

2270
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd, bool metaOnly) {
17✔
2271
  CHECK_PARSER_STATUS(pCxt);
17✔
2272
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
17✔
2273
  SCompactDatabaseStmt* pStmt = NULL;
17✔
2274
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
17✔
2275
  CHECK_MAKE_NODE(pStmt);
17✔
2276
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
17✔
2277
  pStmt->pStart = pStart;
17✔
2278
  pStmt->pEnd = pEnd;
17✔
2279
  pStmt->metaOnly = metaOnly;
17✔
2280
  return (SNode*)pStmt;
17✔
2281
_err:
×
2282
  nodesDestroyNode(pStart);
×
2283
  nodesDestroyNode(pEnd);
×
2284
  return NULL;
×
2285
}
2286

2287
SNode* createCompactVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
×
2288
                                SNode* pEnd, bool metaOnly) {
2289
  CHECK_PARSER_STATUS(pCxt);
×
2290
  if (NULL == pDbName) {
×
2291
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
2292
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
2293
    CHECK_PARSER_STATUS(pCxt);
×
2294
  }
2295
  SCompactVgroupsStmt* pStmt = NULL;
×
2296
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_VGROUPS_STMT, (SNode**)&pStmt);
×
2297
  CHECK_MAKE_NODE(pStmt);
×
2298
  pStmt->pDbName = pDbName;
×
2299
  pStmt->vgidList = vgidList;
×
2300
  pStmt->pStart = pStart;
×
2301
  pStmt->pEnd = pEnd;
×
2302
  pStmt->metaOnly = metaOnly;
×
2303
  return (SNode*)pStmt;
×
2304
_err:
×
2305
  nodesDestroyNode(pDbName);
×
2306
  nodesDestroyList(vgidList);
×
2307
  nodesDestroyNode(pStart);
×
2308
  nodesDestroyNode(pEnd);
×
2309
  return NULL;
×
2310
}
2311

2312
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
36,954✔
2313
  CHECK_PARSER_STATUS(pCxt);
36,954✔
2314
  STableOptions* pOptions = NULL;
36,954✔
2315
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
36,954✔
2316
  CHECK_MAKE_NODE(pOptions);
36,974✔
2317
  pOptions->maxDelay1 = -1;
36,974✔
2318
  pOptions->maxDelay2 = -1;
36,974✔
2319
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
36,974✔
2320
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
36,974✔
2321
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
36,974✔
2322
  pOptions->keep = -1;
36,974✔
2323
  pOptions->commentNull = true;  // mark null
36,974✔
2324
  return (SNode*)pOptions;
36,974✔
2325
_err:
×
2326
  return NULL;
×
2327
}
2328

2329
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
16✔
2330
  CHECK_PARSER_STATUS(pCxt);
16✔
2331
  STableOptions* pOptions = NULL;
16✔
2332
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
16✔
2333
  CHECK_MAKE_NODE(pOptions);
16✔
2334
  pOptions->ttl = -1;
16✔
2335
  pOptions->commentNull = true;  // mark null
16✔
2336
  pOptions->keep = -1;
16✔
2337
  return (SNode*)pOptions;
16✔
2338
_err:
×
2339
  return NULL;
×
2340
}
2341

2342
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
94✔
2343
  CHECK_PARSER_STATUS(pCxt);
94✔
2344
  switch (type) {
94✔
2345
    case TABLE_OPTION_COMMENT:
20✔
2346
      if (checkComment(pCxt, (SToken*)pVal, true)) {
20✔
2347
        ((STableOptions*)pOptions)->commentNull = false;
20✔
2348
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
20✔
2349
      }
2350
      break;
20✔
2351
    case TABLE_OPTION_MAXDELAY:
12✔
2352
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
12✔
2353
      break;
12✔
2354
    case TABLE_OPTION_WATERMARK:
12✔
2355
      ((STableOptions*)pOptions)->pWatermark = pVal;
12✔
2356
      break;
12✔
2357
    case TABLE_OPTION_ROLLUP:
20✔
2358
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
20✔
2359
      break;
20✔
2360
    case TABLE_OPTION_TTL: {
14✔
2361
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
14✔
2362
      if (ttl > INT32_MAX) {
14✔
2363
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
2364
      } else {
2365
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
2366
        ((STableOptions*)pOptions)->ttl = ttl;
14✔
2367
      }
2368
      break;
14✔
2369
    }
2370
    case TABLE_OPTION_SMA:
12✔
2371
      ((STableOptions*)pOptions)->pSma = pVal;
12✔
2372
      break;
12✔
2373
    case TABLE_OPTION_DELETE_MARK:
4✔
2374
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
4✔
2375
      break;
4✔
2376
    case TABLE_OPTION_KEEP:
×
2377
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
×
2378
        ((STableOptions*)pOptions)->keep = taosStr2Int32(((SToken*)pVal)->z, NULL, 10) * 1440;
×
2379
      } else {
2380
        ((STableOptions*)pOptions)->pKeepNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
×
2381
      }
2382
      break;
×
2383
    case TABLE_OPTION_VIRTUAL: {
×
2384
      int64_t virtualStb = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
×
2385
      if (virtualStb != 0 && virtualStb != 1) {
×
2386
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
2387
      } else {
2388
        ((STableOptions*)pOptions)->virtualStb = virtualStb;
×
2389
      }
2390
      break;
×
2391
    }
2392
    default:
×
2393
      break;
×
2394
  }
2395
  return pOptions;
94✔
2396
_err:
×
2397
  nodesDestroyNode(pOptions);
×
2398
  return NULL;
×
2399
}
2400

2401
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
18,784✔
2402
  CHECK_PARSER_STATUS(pCxt);
18,784✔
2403
  SColumnOptions* pOptions = NULL;
18,784✔
2404
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
18,784✔
2405
  CHECK_MAKE_NODE(pOptions);
18,784✔
2406
  pOptions->commentNull = true;
18,784✔
2407
  pOptions->bPrimaryKey = false;
18,784✔
2408
  pOptions->hasRef = false;
18,784✔
2409
  return (SNode*)pOptions;
18,784✔
2410
_err:
×
2411
  return NULL;
×
2412
}
2413

2414
EColumnOptionType getColumnOptionType(const char* optionType) {
2,478✔
2415
  if (0 == strcasecmp(optionType, "ENCODE")) {
2,478✔
2416
    return COLUMN_OPTION_ENCODE;
822✔
2417
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
1,656✔
2418
    return COLUMN_OPTION_COMPRESS;
830✔
2419
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
826✔
2420
    return COLUMN_OPTION_LEVEL;
826✔
2421
  }
2422
  return 0;
×
2423
}
2424

2425
SNode* setColumnReference(SAstCreateContext* pCxt, SNode* pOptions, SNode* pRef) {
×
2426
  CHECK_PARSER_STATUS(pCxt);
×
2427

2428
  ((SColumnOptions*)pOptions)->hasRef = true;
×
2429
  tstrncpy(((SColumnOptions*)pOptions)->refDb, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
×
2430
  tstrncpy(((SColumnOptions*)pOptions)->refTable, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
×
2431
  tstrncpy(((SColumnOptions*)pOptions)->refColumn, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
×
2432
  return pOptions;
×
2433
_err:
×
2434
  nodesDestroyNode(pOptions);
×
2435
  return NULL;
×
2436
}
2437

2438
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
3✔
2439
  CHECK_PARSER_STATUS(pCxt);
3✔
2440
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
3✔
2441
  return pOptions;
3✔
2442
_err:
×
2443
  nodesDestroyNode(pOptions);
×
2444
  return NULL;
×
2445
}
2446

2447
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
2,478✔
2448
  CHECK_PARSER_STATUS(pCxt);
2,478✔
2449
  char optionType[TSDB_CL_OPTION_LEN];
2450

2451
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
2,478✔
2452
  strncpy(optionType, pVal1->z, TMIN(pVal1->n, TSDB_CL_OPTION_LEN));
2,478✔
2453
  if (0 == strlen(optionType)) {
2,478✔
2454
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2455
    return pOptions;
×
2456
  }
2457
  EColumnOptionType type = getColumnOptionType(optionType);
2,478✔
2458
  switch (type) {
2,478✔
2459
    case COLUMN_OPTION_ENCODE:
822✔
2460
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
822✔
2461
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
822✔
2462
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
822✔
2463
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
2464
      }
2465
      break;
822✔
2466
    case COLUMN_OPTION_COMPRESS:
830✔
2467
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
830✔
2468
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
830✔
2469
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
830✔
2470
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
2471
      }
2472
      break;
830✔
2473
    case COLUMN_OPTION_LEVEL:
826✔
2474
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
826✔
2475
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
826✔
2476
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
826✔
2477
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2478
      }
2479
      break;
826✔
2480
    default:
×
2481
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2482
      break;
×
2483
  }
2484
  return pOptions;
2,478✔
2485
_err:
×
2486
  nodesDestroyNode(pOptions);
×
2487
  return NULL;
×
2488
}
2489

2490
SNode* createColumnRefNodeByNode(SAstCreateContext* pCxt, SToken* pColName, SNode* pRef) {
×
2491
  CHECK_PARSER_STATUS(pCxt);
×
2492
  CHECK_NAME(checkColumnName(pCxt, pColName));
×
2493

2494
  SColumnRefNode* pCol = NULL;
×
2495
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
×
2496
  CHECK_MAKE_NODE(pCol);
×
2497
  if (pColName) {
×
2498
    COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
×
2499
  }
2500
  tstrncpy(pCol->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
×
2501
  tstrncpy(pCol->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
×
2502
  tstrncpy(pCol->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
×
2503
  return (SNode*)pCol;
×
2504
_err:
×
2505
  return NULL;
×
2506
}
2507

2508
STokenTriplet* createTokenTriplet(SAstCreateContext* pCxt, SToken pName) {
×
2509
  CHECK_PARSER_STATUS(pCxt);
×
2510

2511
  STokenTriplet *pTokenTri = taosMemoryMalloc(sizeof(STokenTriplet));
×
2512
  CHECK_OUT_OF_MEM(pTokenTri);
×
2513
  pTokenTri->name[0] = pName;
×
2514
  pTokenTri->numOfName = 1;
×
2515

2516
  return pTokenTri;
×
2517
_err:
×
2518
  return NULL;
×
2519
}
2520

2521
STokenTriplet* setColumnName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri, SToken pName) {
×
2522
  CHECK_PARSER_STATUS(pCxt);
×
2523

2524
  if (pTokenTri->numOfName >= 3) {
×
2525
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2526
    goto _err;
×
2527
  }
2528

2529
  pTokenTri->name[pTokenTri->numOfName] = pName;
×
2530
  pTokenTri->numOfName++;
×
2531
  return pTokenTri;
×
2532
_err:
×
2533
  return NULL;
×
2534
}
2535

2536
SNode* createColumnRefNodeByName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri) {
×
2537
  SColumnRefNode* pCol = NULL;
×
2538
  CHECK_PARSER_STATUS(pCxt);
×
2539
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
×
2540
  CHECK_MAKE_NODE(pCol);
×
2541

2542
  switch(pTokenTri->numOfName) {
×
2543
    case 2: {
×
2544
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[0]));
×
2545
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[1]));
×
2546
      snprintf(pCol->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
×
2547
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[0]);
×
2548
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[1]);
×
2549
      break;
×
2550
    }
2551
    case 3: {
×
2552
      CHECK_NAME(checkDbName(pCxt, &pTokenTri->name[0], true));
×
2553
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[1]));
×
2554
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[2]));
×
2555
      COPY_STRING_FORM_ID_TOKEN(pCol->refDbName, &pTokenTri->name[0]);
×
2556
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[1]);
×
2557
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[2]);
×
2558
      break;
×
2559
    }
2560
    default: {
×
2561
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2562
      goto _err;
×
2563
    }
2564
  }
2565

2566
  taosMemFree(pTokenTri);
×
2567
  return (SNode*)pCol;
×
2568
_err:
×
2569
  taosMemFree(pTokenTri);
×
2570
  nodesDestroyNode((SNode*)pCol);
×
2571
  return NULL;
×
2572
}
2573

2574
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
19,591✔
2575
  CHECK_PARSER_STATUS(pCxt);
19,591✔
2576
  CHECK_NAME(checkColumnName(pCxt, pColName));
19,591✔
2577
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
19,591✔
2578
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
×
2579
    CHECK_PARSER_STATUS(pCxt);
×
2580
  }
2581
  SColumnDefNode* pCol = NULL;
19,591✔
2582
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
19,591✔
2583
  CHECK_MAKE_NODE(pCol);
19,591✔
2584
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
19,591✔
2585
  pCol->dataType = dataType;
19,591✔
2586
  pCol->pOptions = pNode;
19,591✔
2587
  pCol->sma = true;
19,591✔
2588
  return (SNode*)pCol;
19,591✔
2589
_err:
×
2590
  nodesDestroyNode(pNode);
×
2591
  return NULL;
×
2592
}
2593

2594
SDataType createDataType(uint8_t type) {
19,127✔
2595
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
19,127✔
2596
  return dt;
19,127✔
2597
}
2598

2599
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
608✔
2600
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
608✔
2601
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
608✔
2602
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
608✔
2603
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
608✔
2604
  return dt;
608✔
2605
}
2606

2607
SDataType createDecimalDataType(uint8_t type, const SToken* pPrecisionToken, const SToken* pScaleToken) {
22✔
2608
  SDataType dt = {0};
22✔
2609
  dt.precision = taosStr2UInt8(pPrecisionToken->z, NULL, 10);
22✔
2610
  dt.scale = pScaleToken ? taosStr2Int32(pScaleToken->z, NULL, 10) : 0;
22✔
2611
  dt.type = decimalTypeFromPrecision(dt.precision);
22✔
2612
  dt.bytes = tDataTypes[dt.type].bytes;
22✔
2613
  return dt;
22✔
2614
}
2615

2616
SNode* createCreateVTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols) {
×
2617
  SCreateVTableStmt * pStmt = NULL;
×
2618
  CHECK_PARSER_STATUS(pCxt);
×
2619
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
×
2620
  CHECK_MAKE_NODE(pStmt);
×
2621
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
×
2622
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
×
2623
  pStmt->ignoreExists = ignoreExists;
×
2624
  pStmt->pCols = pCols;
×
2625
  nodesDestroyNode(pRealTable);
×
2626
  return (SNode*)pStmt;
×
2627
_err:
×
2628
  nodesDestroyNode(pRealTable);
×
2629
  nodesDestroyList(pCols);
×
2630
  return NULL;
×
2631
}
2632

2633
SNode* createCreateVSubTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable,
×
2634
                                 SNodeList* pSpecificColRefs, SNodeList* pColRefs, SNode* pUseRealTable,
2635
                                 SNodeList* pSpecificTags, SNodeList* pValsOfTags) {
2636
  CHECK_PARSER_STATUS(pCxt);
×
2637
  SCreateVSubTableStmt* pStmt = NULL;
×
2638
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT, (SNode**)&pStmt);
×
2639
  CHECK_MAKE_NODE(pStmt);
×
2640
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
×
2641
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
×
2642
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
×
2643
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
×
2644
  pStmt->ignoreExists = ignoreExists;
×
2645
  pStmt->pSpecificTags = pSpecificTags;
×
2646
  pStmt->pValsOfTags = pValsOfTags;
×
2647
  pStmt->pSpecificColRefs = pSpecificColRefs;
×
2648
  pStmt->pColRefs = pColRefs;
×
2649
  nodesDestroyNode(pRealTable);
×
2650
  nodesDestroyNode(pUseRealTable);
×
2651
  return (SNode*)pStmt;
×
2652
_err:
×
2653
  nodesDestroyNode(pRealTable);
×
2654
  nodesDestroyNode(pUseRealTable);
×
2655
  nodesDestroyList(pSpecificTags);
×
2656
  nodesDestroyList(pValsOfTags);
×
2657
  nodesDestroyList(pSpecificColRefs);
×
2658
  nodesDestroyList(pColRefs);
×
2659
  return NULL;
×
2660
}
2661

2662
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
329✔
2663
                             SNodeList* pTags, SNode* pOptions) {
2664
  CHECK_PARSER_STATUS(pCxt);
329✔
2665
  SCreateTableStmt* pStmt = NULL;
329✔
2666
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
329✔
2667
  CHECK_MAKE_NODE(pStmt);
329✔
2668
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
329✔
2669
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
329✔
2670
  pStmt->ignoreExists = ignoreExists;
329✔
2671
  pStmt->pCols = pCols;
329✔
2672
  pStmt->pTags = pTags;
329✔
2673
  pStmt->pOptions = (STableOptions*)pOptions;
329✔
2674
  nodesDestroyNode(pRealTable);
329✔
2675
  return (SNode*)pStmt;
329✔
2676
_err:
×
2677
  nodesDestroyNode(pRealTable);
×
2678
  nodesDestroyList(pCols);
×
2679
  nodesDestroyList(pTags);
×
2680
  nodesDestroyNode(pOptions);
×
2681
  return NULL;
×
2682
}
2683

2684
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
36,620✔
2685
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
2686
  CHECK_PARSER_STATUS(pCxt);
36,620✔
2687
  SCreateSubTableClause* pStmt = NULL;
36,620✔
2688
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
36,620✔
2689
  CHECK_MAKE_NODE(pStmt);
36,651✔
2690
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
36,651✔
2691
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
36,651✔
2692
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
36,651✔
2693
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
36,651✔
2694
  pStmt->ignoreExists = ignoreExists;
36,651✔
2695
  pStmt->pSpecificTags = pSpecificTags;
36,651✔
2696
  pStmt->pValsOfTags = pValsOfTags;
36,651✔
2697
  pStmt->pOptions = (STableOptions*)pOptions;
36,651✔
2698
  nodesDestroyNode(pRealTable);
36,651✔
2699
  nodesDestroyNode(pUseRealTable);
36,636✔
2700
  return (SNode*)pStmt;
36,667✔
2701
_err:
×
2702
  nodesDestroyNode(pRealTable);
×
2703
  nodesDestroyNode(pOptions);
×
2704
  nodesDestroyNode(pUseRealTable);
×
2705
  nodesDestroyList(pSpecificTags);
×
2706
  nodesDestroyList(pValsOfTags);
×
2707
  return NULL;
×
2708
}
2709

2710
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
×
2711
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
2712
  CHECK_PARSER_STATUS(pCxt);
×
2713
  SCreateSubTableFromFileClause* pStmt = NULL;
×
2714
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
×
2715
  CHECK_MAKE_NODE(pStmt);
×
2716
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
2717
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
2718
  pStmt->ignoreExists = ignoreExists;
×
2719
  pStmt->pSpecificTags = pSpecificTags;
×
2720
  if (TK_NK_STRING == pFilePath->type) {
×
2721
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
×
2722
  } else {
2723
    strncpy(pStmt->filePath, pFilePath->z, pFilePath->n);
×
2724
  }
2725

2726
  nodesDestroyNode(pUseRealTable);
×
2727
  return (SNode*)pStmt;
×
2728
_err:
×
2729
  nodesDestroyNode(pUseRealTable);
×
2730
  nodesDestroyList(pSpecificTags);
×
2731
  return NULL;
×
2732
}
2733

2734
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
34,294✔
2735
  CHECK_PARSER_STATUS(pCxt);
34,294✔
2736
  SCreateMultiTablesStmt* pStmt = NULL;
34,294✔
2737
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
34,294✔
2738
  CHECK_MAKE_NODE(pStmt);
34,346✔
2739
  pStmt->pSubTables = pSubTables;
34,346✔
2740
  return (SNode*)pStmt;
34,346✔
2741
_err:
×
2742
  nodesDestroyList(pSubTables);
×
2743
  return NULL;
×
2744
}
2745

2746
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
696✔
2747
  CHECK_PARSER_STATUS(pCxt);
696✔
2748
  SDropTableClause* pStmt = NULL;
696✔
2749
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
696✔
2750
  CHECK_MAKE_NODE(pStmt);
696✔
2751
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
696✔
2752
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
696✔
2753
  pStmt->ignoreNotExists = ignoreNotExists;
696✔
2754
  nodesDestroyNode(pRealTable);
696✔
2755
  return (SNode*)pStmt;
696✔
2756
_err:
×
2757
  nodesDestroyNode(pRealTable);
×
2758
  return NULL;
×
2759
}
2760

2761
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
688✔
2762
  CHECK_PARSER_STATUS(pCxt);
688✔
2763
  SDropTableStmt* pStmt = NULL;
688✔
2764
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
688✔
2765
  CHECK_MAKE_NODE(pStmt);
688✔
2766
  pStmt->pTables = pTables;
688✔
2767
  pStmt->withOpt = withOpt;
688✔
2768
  return (SNode*)pStmt;
688✔
2769
_err:
×
2770
  nodesDestroyList(pTables);
×
2771
  return NULL;
×
2772
}
2773

2774
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
4✔
2775
  CHECK_PARSER_STATUS(pCxt);
4✔
2776
  SDropSuperTableStmt* pStmt = NULL;
4✔
2777
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
4✔
2778
  CHECK_MAKE_NODE(pStmt);
4✔
2779
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
4✔
2780
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
4✔
2781
  pStmt->ignoreNotExists = ignoreNotExists;
4✔
2782
  pStmt->withOpt = withOpt;
4✔
2783
  nodesDestroyNode(pRealTable);
4✔
2784
  return (SNode*)pStmt;
4✔
2785
_err:
×
2786
  nodesDestroyNode(pRealTable);
×
2787
  return NULL;
×
2788
}
2789

2790
SNode* createDropVirtualTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
×
2791
  CHECK_PARSER_STATUS(pCxt);
×
2792
  SDropVirtualTableStmt* pStmt = NULL;
×
2793
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
×
2794
  CHECK_MAKE_NODE(pStmt);
×
2795
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
2796
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
2797
  pStmt->ignoreNotExists = ignoreNotExists;
×
2798
  pStmt->withOpt = withOpt;
×
2799
  nodesDestroyNode(pRealTable);
×
2800
  return (SNode*)pStmt;
×
2801
_err:
×
2802
  nodesDestroyNode(pRealTable);
×
2803
  return NULL;
×
2804
}
2805

2806
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
120✔
2807
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
120✔
2808
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
120✔
2809
  nodesDestroyNode(pRealTable);
120✔
2810
  return (SNode*)pStmt;
120✔
2811
}
2812

2813
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
16✔
2814
  CHECK_PARSER_STATUS(pCxt);
16✔
2815
  SAlterTableStmt* pStmt = NULL;
16✔
2816
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
16✔
2817
  CHECK_MAKE_NODE(pStmt);
16✔
2818
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
16✔
2819
  pStmt->pOptions = (STableOptions*)pOptions;
16✔
2820
  return createAlterTableStmtFinalize(pRealTable, pStmt);
16✔
2821
_err:
×
2822
  nodesDestroyNode(pRealTable);
×
2823
  nodesDestroyNode(pOptions);
×
2824
  return NULL;
×
2825
}
2826

2827
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
40✔
2828
                                    SDataType dataType) {
2829
  CHECK_PARSER_STATUS(pCxt);
40✔
2830
  CHECK_NAME(checkColumnName(pCxt, pColName));
40✔
2831
  SAlterTableStmt* pStmt = NULL;
40✔
2832
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
40✔
2833
  CHECK_MAKE_NODE(pStmt);
40✔
2834
  pStmt->alterType = alterType;
40✔
2835
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
40✔
2836
  pStmt->dataType = dataType;
40✔
2837
  return createAlterTableStmtFinalize(pRealTable, pStmt);
40✔
2838
_err:
×
2839
  nodesDestroyNode(pRealTable);
×
2840
  return NULL;
×
2841
}
2842

2843
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
8✔
2844
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
2845
  SAlterTableStmt* pStmt = NULL;
8✔
2846
  CHECK_PARSER_STATUS(pCxt);
8✔
2847
  CHECK_NAME(checkColumnName(pCxt, pColName));
8✔
2848
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
8✔
2849
  CHECK_MAKE_NODE(pStmt);
8✔
2850
  pStmt->alterType = alterType;
8✔
2851
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
8✔
2852
  pStmt->dataType = dataType;
8✔
2853
  pStmt->pColOptions = (SColumnOptions*)pOptions;
8✔
2854

2855
  if (pOptions != NULL) {
8✔
2856
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
8✔
2857
    if (pOption->hasRef) {
8✔
2858
      if (!pOption->commentNull || pOption->bPrimaryKey || 0 != strcmp(pOption->compress, "") ||
×
2859
          0 != strcmp(pOption->encode, "") || 0 != strcmp(pOption->compressLevel, "")) {
×
2860
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
×
2861
      }
2862
      pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF;
×
2863
      tstrncpy(pStmt->refDbName, pOption->refDb, TSDB_DB_NAME_LEN);
×
2864
      tstrncpy(pStmt->refTableName, pOption->refTable, TSDB_TABLE_NAME_LEN);
×
2865
      tstrncpy(pStmt->refColName, pOption->refColumn, TSDB_COL_NAME_LEN);
×
2866
      CHECK_PARSER_STATUS(pCxt);
×
2867
    } else if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
8✔
2868
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
8✔
2869
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
×
2870
      } else {
2871
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
2872
        //                                         "not support alter column with option except compress");
2873
        // return NULL;
2874
      }
2875
    } else {
2876
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
2877
                                              "not support alter column with option except compress");
2878
      CHECK_PARSER_STATUS(pCxt);
×
2879
    }
2880
  }
2881
  return createAlterTableStmtFinalize(pRealTable, pStmt);
8✔
2882
_err:
×
2883
  nodesDestroyNode(pOptions);
×
2884
  nodesDestroyNode((SNode*)pStmt);
×
2885
  nodesDestroyNode(pRealTable);
×
2886
  return NULL;
×
2887
}
2888

2889
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
×
2890
                                           SToken* pColName, SNode* pOptions) {
2891
  CHECK_PARSER_STATUS(pCxt);
×
2892
  CHECK_NAME(checkColumnName(pCxt, pColName));
×
2893
  SAlterTableStmt* pStmt = NULL;
×
2894
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
2895
  CHECK_MAKE_NODE(pStmt);
×
2896
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
×
2897
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
×
2898
  pStmt->pColOptions = (SColumnOptions*)pOptions;
×
2899
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
2900
_err:
×
2901
  nodesDestroyNode(pOptions);
×
2902
  nodesDestroyNode(pRealTable);
×
2903
  return NULL;
×
2904
}
2905

2906
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
20✔
2907
  CHECK_PARSER_STATUS(pCxt);
20✔
2908
  CHECK_NAME(checkColumnName(pCxt, pColName));
20✔
2909
  SAlterTableStmt* pStmt = NULL;
20✔
2910
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
20✔
2911
  CHECK_MAKE_NODE(pStmt);
20✔
2912
  pStmt->alterType = alterType;
20✔
2913
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
20✔
2914
  return createAlterTableStmtFinalize(pRealTable, pStmt);
20✔
2915
_err:
×
2916
  nodesDestroyNode(pRealTable);
×
2917
  return NULL;
×
2918
}
2919

2920
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
24✔
2921
                                 SToken* pNewColName) {
2922
  CHECK_PARSER_STATUS(pCxt);
24✔
2923
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
24✔
2924
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
24✔
2925
  SAlterTableStmt* pStmt = NULL;
24✔
2926
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
24✔
2927
  CHECK_MAKE_NODE(pStmt);
24✔
2928
  pStmt->alterType = alterType;
24✔
2929
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
24✔
2930
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
24✔
2931
  return createAlterTableStmtFinalize(pRealTable, pStmt);
24✔
2932
_err:
×
2933
  nodesDestroyNode(pRealTable);
×
2934
  return NULL;
×
2935
}
2936

2937
SNode* createAlterTableAlterColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
×
2938
                                   SNode* pRef) {
2939
  CHECK_PARSER_STATUS(pCxt);
×
2940
  CHECK_NAME(checkColumnName(pCxt, pColName));
×
2941
  SAlterTableStmt* pStmt = NULL;
×
2942
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
2943
  CHECK_MAKE_NODE(pStmt);
×
2944
  pStmt->alterType = alterType;
×
2945
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
×
2946
  tstrncpy(pStmt->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
×
2947
  tstrncpy(pStmt->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
×
2948
  tstrncpy(pStmt->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
×
2949
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
2950
_err:
×
2951
  nodesDestroyNode(pRealTable);
×
2952
  return NULL;
×
2953
}
2954

2955
SNode* createAlterTableRemoveColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
×
2956
                                    const SToken* pLiteral) {
2957
  CHECK_PARSER_STATUS(pCxt);
×
2958
  CHECK_NAME(checkColumnName(pCxt, pColName));
×
2959
  SAlterTableStmt* pStmt = NULL;
×
2960
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
2961
  CHECK_MAKE_NODE(pStmt);
×
2962
  pStmt->alterType = alterType;
×
2963
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
×
2964
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
2965
_err:
×
2966
  nodesDestroyNode(pRealTable);
×
2967
  return NULL;
×
2968
}
2969

2970
SNode* createAlterSingleTagColumnNode(SAstCreateContext* pCtx, SToken* pTagName, SNode* pVal) {
12✔
2971
  CHECK_PARSER_STATUS(pCtx);
12✔
2972
  SAlterTableStmt* pStmt = NULL;
12✔
2973
  pCtx->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
12✔
2974
  CHECK_MAKE_NODE(pStmt);
12✔
2975
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
12✔
2976
  CHECK_NAME(checkColumnName(pCtx, pTagName));
12✔
2977
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
12✔
2978
  pStmt->pVal = (SValueNode*)pVal;
12✔
2979
  pStmt->pNodeListTagValue = NULL;
12✔
2980
  return (SNode*)pStmt;
12✔
2981
_err:
×
2982
  return NULL;
×
2983
}
2984

2985
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
×
2986
  CHECK_PARSER_STATUS(pCxt);
×
2987
  CHECK_NAME(checkColumnName(pCxt, pTagName));
×
2988
  SAlterTableStmt* pStmt = NULL;
×
2989
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
2990
  CHECK_MAKE_NODE(pStmt);
×
2991
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
×
2992
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
×
2993
  pStmt->pVal = (SValueNode*)pVal;
×
2994
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
2995
_err:
×
2996
  nodesDestroyNode(pVal);
×
2997
  nodesDestroyNode(pRealTable);
×
2998
  return NULL;
×
2999
}
3000

3001
SNode* createAlterTableSetMultiTagValue(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pList) {
12✔
3002
  CHECK_PARSER_STATUS(pCxt);
12✔
3003
  SAlterTableStmt* pStmt = NULL;
12✔
3004
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
12✔
3005

3006
  CHECK_MAKE_NODE(pStmt);
12✔
3007
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL;
12✔
3008
  pStmt->pNodeListTagValue = pList;
12✔
3009
  return createAlterTableStmtFinalize(pRealTable, pStmt);
12✔
3010
_err:
×
3011
  return NULL;
×
3012
}
3013

3014
SNode* setAlterSuperTableType(SNode* pStmt) {
52✔
3015
  if (!pStmt) return NULL;
52✔
3016
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
52✔
3017
  return pStmt;
52✔
3018
}
3019

3020
SNode* setAlterVirtualTableType(SNode* pStmt) {
×
3021
  if (!pStmt) return NULL;
×
3022
  setNodeType(pStmt, QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT);
×
3023
  return pStmt;
×
3024
}
3025

3026
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
619✔
3027
  CHECK_PARSER_STATUS(pCxt);
619✔
3028
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
619✔
3029
  SUseDatabaseStmt* pStmt = NULL;
619✔
3030
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
619✔
3031
  CHECK_MAKE_NODE(pStmt);
619✔
3032
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
619✔
3033
  return (SNode*)pStmt;
619✔
3034
_err:
×
3035
  return NULL;
×
3036
}
3037

3038
static bool needDbShowStmt(ENodeType type) {
258✔
3039
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
219✔
3040
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
13✔
3041
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
×
3042
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type ||
×
3043
         QUERY_NODE_SHOW_USAGE_STMT == type || QUERY_NODE_SHOW_VTABLES_STMT == type;
477✔
3044
}
3045

3046
SNode* createShowStmtWithLike(SAstCreateContext* pCxt, ENodeType type,   SNode*  pLikePattern) {
91✔
3047
  CHECK_PARSER_STATUS(pCxt);
91✔
3048
  SShowStmt* pStmt = NULL;
91✔
3049
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
91✔
3050
  CHECK_MAKE_NODE(pStmt);
91✔
3051
  pStmt->withFull = false;
91✔
3052
  pStmt->pTbName = pLikePattern;
91✔
3053
  if (pLikePattern) {
91✔
3054
    pStmt->tableCondType = OP_TYPE_LIKE;
×
3055
  }
3056
  return (SNode*)pStmt;
91✔
3057
_err:
×
3058
  nodesDestroyNode(pLikePattern);
×
3059
  return NULL;
×
3060
}
3061

3062
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
355✔
3063
  CHECK_PARSER_STATUS(pCxt);
355✔
3064
  SShowStmt* pStmt = NULL;
355✔
3065
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
355✔
3066
  CHECK_MAKE_NODE(pStmt);
358✔
3067
  pStmt->withFull = false;
358✔
3068
  return (SNode*)pStmt;
358✔
3069
_err:
×
3070
  return NULL;
×
3071
}
3072

3073
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
×
3074
  CHECK_PARSER_STATUS(pCxt);
×
3075
  SShowStmt* pStmt = NULL;
×
3076
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3077
  CHECK_MAKE_NODE(pStmt);
×
3078
  pStmt->withFull = true;
×
3079
  return (SNode*)pStmt;
×
3080
_err:
×
3081
  return NULL;
×
3082
}
3083

3084
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
×
3085
  CHECK_PARSER_STATUS(pCxt);
×
3086
  SShowCompactsStmt* pStmt = NULL;
×
3087
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3088
  CHECK_MAKE_NODE(pStmt);
×
3089
  return (SNode*)pStmt;
×
3090
_err:
×
3091
  return NULL;
×
3092
}
3093

3094
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
114✔
3095
  if (pStmt == NULL) {
114✔
3096
    return NULL;
×
3097
  }
3098
  SShowStmt* pShow = (SShowStmt*)pStmt;
114✔
3099
  pShow->showKind = showKind;
114✔
3100
  return pStmt;
114✔
3101
}
3102

3103
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
258✔
3104
                              EOperatorType tableCondType) {
3105
  CHECK_PARSER_STATUS(pCxt);
258✔
3106
  if (needDbShowStmt(type) && NULL == pDbName) {
258✔
3107
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
2✔
3108
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2✔
3109
    CHECK_PARSER_STATUS(pCxt);
2✔
3110
  }
3111
  SShowStmt* pStmt = NULL;
256✔
3112
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
256✔
3113
  CHECK_MAKE_NODE(pStmt);
256✔
3114
  pStmt->pDbName = pDbName;
256✔
3115
  pStmt->pTbName = pTbName;
256✔
3116
  pStmt->tableCondType = tableCondType;
256✔
3117
  return (SNode*)pStmt;
256✔
3118
_err:
2✔
3119
  nodesDestroyNode(pDbName);
2✔
3120
  nodesDestroyNode(pTbName);
2✔
3121
  return NULL;
2✔
3122
}
3123

3124
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
39✔
3125
                            EOperatorType tableCondType) {
3126
  CHECK_PARSER_STATUS(pCxt);
39✔
3127
  SNode* pDbName = NULL;
39✔
3128
  if (option.dbName.type == TK_NK_NIL) {
39✔
3129
    pDbName = createDefaultDatabaseCondValue(pCxt);
17✔
3130
  } else {
3131
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
22✔
3132
  }
3133

3134
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
39✔
3135
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3136
    return NULL;
×
3137
  }
3138

3139
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
39✔
3140
  CHECK_PARSER_STATUS(pCxt);
39✔
3141
  (void)setShowKind(pCxt, pStmt, option.kind);
38✔
3142
  return pStmt;
38✔
3143
_err:
1✔
3144
  nodesDestroyNode(pTbName);
1✔
3145
  return NULL;
1✔
3146
}
3147

3148
SNode* createShowVTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
×
3149
                             EOperatorType tableCondType) {
3150
  CHECK_PARSER_STATUS(pCxt);
×
3151
  SNode* pDbName = NULL;
×
3152
  if (option.dbName.type == TK_NK_NIL) {
×
3153
    pDbName = createDefaultDatabaseCondValue(pCxt);
×
3154
  } else {
3155
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
×
3156
  }
3157

3158
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
×
3159
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3160
    return NULL;
×
3161
  }
3162

3163
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_VTABLES_STMT, pDbName, pTbName, tableCondType);
×
3164
  CHECK_PARSER_STATUS(pCxt);
×
3165
  (void)setShowKind(pCxt, pStmt, option.kind);
×
3166
  return pStmt;
×
3167
_err:
×
3168
  nodesDestroyNode(pTbName);
×
3169
  return NULL;
×
3170
}
3171

3172
SNode* createShowSTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
52✔
3173
                             EOperatorType tableCondType) {
3174
  CHECK_PARSER_STATUS(pCxt);
52✔
3175
  SNode* pDbName = NULL;
52✔
3176
  if (option.dbName.type == TK_NK_NIL) {
52✔
3177
    pDbName = createDefaultDatabaseCondValue(pCxt);
19✔
3178
  } else {
3179
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
33✔
3180
  }
3181

3182
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_VIRTUAL && option.kind != SHOW_KIND_ALL) {
52✔
3183
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3184
    return NULL;
×
3185
  }
3186

3187
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_STABLES_STMT, pDbName, pTbName, tableCondType);
52✔
3188
  CHECK_PARSER_STATUS(pCxt);
52✔
3189
  (void)setShowKind(pCxt, pStmt, option.kind);
52✔
3190
  return pStmt;
52✔
3191
_err:
×
3192
  nodesDestroyNode(pTbName);
×
3193
  return NULL;
×
3194
}
3195

3196
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
4✔
3197
  CHECK_PARSER_STATUS(pCxt);
4✔
3198
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
4✔
3199
  SShowCreateDatabaseStmt* pStmt = NULL;
4✔
3200
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
4✔
3201
  CHECK_MAKE_NODE(pStmt);
4✔
3202
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
4✔
3203
  return (SNode*)pStmt;
4✔
3204
_err:
×
3205
  return NULL;
×
3206
}
3207

3208
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
×
3209
  CHECK_PARSER_STATUS(pCxt);
×
3210
  SToken  dbToken = {0};
×
3211
  SToken* pDbToken = NULL;
×
3212

3213
  if (pNode) {
×
3214
    SValueNode* pDbName = (SValueNode*)pNode;
×
3215
    if (pDbName->literal) {
×
3216
      dbToken.z = pDbName->literal;
×
3217
      dbToken.n = strlen(pDbName->literal);
×
3218
      pDbToken = &dbToken;
×
3219
    }
3220
  }
3221

3222
  if (pDbToken) {
×
3223
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
×
3224
  }
3225

3226
  SShowAliveStmt* pStmt = NULL;
×
3227
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3228
  CHECK_PARSER_STATUS(pCxt);
×
3229

3230
  if (pDbToken) {
×
3231
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
×
3232
  }
3233
  if (pNode) {
×
3234
    nodesDestroyNode(pNode);
×
3235
  }
3236

3237
  return (SNode*)pStmt;
×
3238
_err:
×
3239
  nodesDestroyNode(pNode);
×
3240
  return NULL;
×
3241
}
3242

3243
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
55✔
3244
  CHECK_PARSER_STATUS(pCxt);
55✔
3245
  SShowCreateTableStmt* pStmt = NULL;
55✔
3246
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
55✔
3247
  CHECK_MAKE_NODE(pStmt);
55✔
3248
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
55✔
3249
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
55✔
3250
  nodesDestroyNode(pRealTable);
55✔
3251
  return (SNode*)pStmt;
55✔
3252
_err:
×
3253
  nodesDestroyNode(pRealTable);
×
3254
  return NULL;
×
3255
}
3256

3257
SNode* createShowCreateVTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
×
3258
  CHECK_PARSER_STATUS(pCxt);
×
3259
  SShowCreateTableStmt* pStmt = NULL;
×
3260
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3261
  CHECK_MAKE_NODE(pStmt);
×
3262
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
3263
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
3264
  nodesDestroyNode(pRealTable);
×
3265
  return (SNode*)pStmt;
×
3266
_err:
×
3267
  nodesDestroyNode(pRealTable);
×
3268
  return NULL;
×
3269
}
3270

3271
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
×
3272
  CHECK_PARSER_STATUS(pCxt);
×
3273
  SShowCreateViewStmt* pStmt = NULL;
×
3274
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3275
  CHECK_MAKE_NODE(pStmt);
×
3276
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
3277
  tstrncpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
3278
  nodesDestroyNode(pRealTable);
×
3279
  return (SNode*)pStmt;
×
3280
_err:
×
3281
  nodesDestroyNode(pRealTable);
×
3282
  return NULL;
×
3283
}
3284

3285
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
9✔
3286
  CHECK_PARSER_STATUS(pCxt);
9✔
3287
  SShowTableDistributedStmt* pStmt = NULL;
9✔
3288
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
9✔
3289
  CHECK_MAKE_NODE(pStmt);
9✔
3290
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
9✔
3291
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
9✔
3292
  nodesDestroyNode(pRealTable);
9✔
3293
  return (SNode*)pStmt;
9✔
3294
_err:
×
3295
  nodesDestroyNode(pRealTable);
×
3296
  return NULL;
×
3297
}
3298

3299
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
10✔
3300
  CHECK_PARSER_STATUS(pCxt);
10✔
3301
  SShowDnodeVariablesStmt* pStmt = NULL;
10✔
3302
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
10✔
3303
  CHECK_MAKE_NODE(pStmt);
10✔
3304
  pStmt->pDnodeId = pDnodeId;
10✔
3305
  pStmt->pLikePattern = pLikePattern;
10✔
3306
  return (SNode*)pStmt;
10✔
3307
_err:
×
3308
  nodesDestroyNode(pDnodeId);
×
3309
  nodesDestroyNode(pLikePattern);
×
3310
  return NULL;
×
3311
}
3312

3313
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
8✔
3314
  CHECK_PARSER_STATUS(pCxt);
8✔
3315
  SShowVnodesStmt* pStmt = NULL;
8✔
3316
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
8✔
3317
  CHECK_MAKE_NODE(pStmt);
8✔
3318
  pStmt->pDnodeId = pDnodeId;
8✔
3319
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
8✔
3320
  return (SNode*)pStmt;
8✔
3321
_err:
×
3322
  nodesDestroyNode(pDnodeId);
×
3323
  nodesDestroyNode(pDnodeEndpoint);
×
3324
  return NULL;
×
3325
}
3326

3327
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
13✔
3328
  CHECK_PARSER_STATUS(pCxt);
13✔
3329
  if (NULL == pDbName) {
13✔
3330
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
3331
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3332
    CHECK_PARSER_STATUS(pCxt);
×
3333
  }
3334
  SShowTableTagsStmt* pStmt = NULL;
13✔
3335
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
13✔
3336
  CHECK_MAKE_NODE(pStmt);
13✔
3337
  pStmt->pDbName = pDbName;
13✔
3338
  pStmt->pTbName = pTbName;
13✔
3339
  pStmt->pTags = pTags;
13✔
3340
  return (SNode*)pStmt;
13✔
3341
_err:
×
3342
  nodesDestroyNode(pTbName);
×
3343
  nodesDestroyNode(pDbName);
×
3344
  nodesDestroyList(pTags);
×
3345
  return NULL;
×
3346
}
3347

3348
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
×
3349
  CHECK_PARSER_STATUS(pCxt);
×
3350
  SShowCompactDetailsStmt* pStmt = NULL;
×
3351
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
×
3352
  CHECK_MAKE_NODE(pStmt);
×
3353
  pStmt->pCompactId = pCompactId;
×
3354
  return (SNode*)pStmt;
×
3355
_err:
×
3356
  nodesDestroyNode(pCompactId);
×
3357
  return NULL;
×
3358
}
3359

3360
SNode* createShowTransactionDetailsStmt(SAstCreateContext* pCxt, SNode* pTransactionIdNode) {
×
3361
  CHECK_PARSER_STATUS(pCxt);
×
3362
  SShowTransactionDetailsStmt* pStmt = NULL;
×
3363
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT, (SNode**)&pStmt);
×
3364
  CHECK_MAKE_NODE(pStmt);
×
3365
  pStmt->pTransactionId = pTransactionIdNode;
×
3366
  return (SNode*)pStmt;
×
3367
_err:
×
3368
  nodesDestroyNode(pTransactionIdNode);
×
3369
  return NULL;
×
3370
}
3371

3372
static int32_t getIpV4RangeFromWhitelistItem(char* ipRange, SIpV4Range* pIpRange) {
×
3373
  int32_t code = TSDB_CODE_SUCCESS;
×
3374
#ifndef TD_ASTRA
3375
  char* ipCopy = taosStrdup(ipRange);
×
3376
  if (!ipCopy) return terrno;
×
3377
  char* slash = strchr(ipCopy, '/');
×
3378
  if (slash) {
×
3379
    *slash = '\0';
×
3380
    struct in_addr addr;
3381
    if (uv_inet_pton(AF_INET, ipCopy, &addr) == 0) {
×
3382
      int32_t prefix = 0;
×
3383
      code = taosStr2int32(slash + 1, &prefix);
×
3384
      if (code == 0) {
×
3385
        if (prefix < 0 || prefix > 32) {
×
3386
          code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3387
        } else {
3388
          pIpRange->ip = addr.s_addr;
×
3389
          pIpRange->mask = prefix;
×
3390
          code = TSDB_CODE_SUCCESS;
×
3391
        }
3392
      }
3393
    } else {
3394
      code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3395
    }
3396
  } else {
3397
    struct in_addr addr;
3398
    if (uv_inet_pton(AF_INET, ipCopy, &addr) == 0) {
×
3399
      pIpRange->ip = addr.s_addr;
×
3400
      pIpRange->mask = 32;
×
3401
      code = TSDB_CODE_SUCCESS;
×
3402
    } else {
3403
      code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3404
    }
3405
  }
3406

3407
  taosMemoryFreeClear(ipCopy);
×
3408
#endif
3409
  return code;
×
3410
}
3411

3412
static int32_t fillIpRangesFromWhiteList(SAstCreateContext* pCxt, SNodeList* pIpRangesNodeList, SIpV4Range* pIpRanges) {
×
3413
  int32_t i = 0;
×
3414
  int32_t code = 0;
×
3415

3416
  SNode* pNode = NULL;
×
3417
  FOREACH(pNode, pIpRangesNodeList) {
×
3418
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
×
3419
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IP_RANGE);
×
3420
      return TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3421
    }
3422
    SValueNode* pValNode = (SValueNode*)(pNode);
×
3423
    code = getIpV4RangeFromWhitelistItem(pValNode->literal, pIpRanges + i);
×
3424
    ++i;
×
3425
    if (code != TSDB_CODE_SUCCESS) {
×
3426
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid IP range %s", pValNode->literal);
×
3427
      return code;
×
3428
    }
3429
  }
3430
  return TSDB_CODE_SUCCESS;
×
3431
}
3432

3433
SNode* addCreateUserStmtWhiteList(SAstCreateContext* pCxt, SNode* pCreateUserStmt, SNodeList* pIpRangesNodeList) {
63✔
3434
  if (NULL == pCreateUserStmt) {
63✔
3435
    if (pIpRangesNodeList != NULL) {
1✔
3436
      nodesDestroyList(pIpRangesNodeList);
×
3437
    }
3438
    return NULL;
1✔
3439
  }
3440

3441
  if (NULL == pIpRangesNodeList) {
62✔
3442
    return pCreateUserStmt;
62✔
3443
  }
3444

3445
  ((SCreateUserStmt*)pCreateUserStmt)->pNodeListIpRanges = pIpRangesNodeList;
×
3446
  SCreateUserStmt* pCreateUser = (SCreateUserStmt*)pCreateUserStmt;
×
3447
  pCreateUser->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
×
3448
  pCreateUser->pIpRanges = taosMemoryMalloc(pCreateUser->numIpRanges * sizeof(SIpV4Range));
×
3449
  CHECK_OUT_OF_MEM(pCreateUser->pIpRanges);
×
3450

3451
  pCxt->errCode = fillIpRangesFromWhiteList(pCxt, pIpRangesNodeList, pCreateUser->pIpRanges);
×
3452
  CHECK_PARSER_STATUS(pCxt);
×
3453

3454
  return pCreateUserStmt;
×
3455
_err:
×
3456
  nodesDestroyNode(pCreateUserStmt);
×
3457
  nodesDestroyList(pIpRangesNodeList);
×
3458
  return NULL;
×
3459
}
3460

3461
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo,
63✔
3462
                            int8_t createDb, int8_t is_import) {
3463
  CHECK_PARSER_STATUS(pCxt);
63✔
3464
  char password[TSDB_USET_PASSWORD_LONGLEN + 3] = {0};
63✔
3465
  CHECK_NAME(checkUserName(pCxt, pUserName));
63✔
3466
  if (is_import == 0) {
63✔
3467
    CHECK_NAME(checkPassword(pCxt, pPassword, password));
63✔
3468
  } else {
3469
    CHECK_NAME(checkImportPassword(pCxt, pPassword, password));
×
3470
  }
3471
  SCreateUserStmt* pStmt = NULL;
62✔
3472
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
62✔
3473
  CHECK_MAKE_NODE(pStmt);
62✔
3474
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
62✔
3475
  tstrncpy(pStmt->password, password, TSDB_USET_PASSWORD_LONGLEN);
62✔
3476
  pStmt->sysinfo = sysinfo;
62✔
3477
  pStmt->createDb = createDb;
62✔
3478
  pStmt->isImport = is_import;
62✔
3479
  return (SNode*)pStmt;
62✔
3480
_err:
1✔
3481
  return NULL;
1✔
3482
}
3483

3484
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, void* pAlterInfo) {
38✔
3485
  SAlterUserStmt* pStmt = NULL;
38✔
3486
  CHECK_PARSER_STATUS(pCxt);
38✔
3487
  CHECK_NAME(checkUserName(pCxt, pUserName));
38✔
3488
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
38✔
3489
  CHECK_MAKE_NODE(pStmt);
38✔
3490
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
38✔
3491
  pStmt->alterType = alterType;
38✔
3492
  switch (alterType) {
38✔
3493
    case TSDB_ALTER_USER_PASSWD: {
17✔
3494
      char    password[TSDB_USET_PASSWORD_LONGLEN] = {0};
17✔
3495
      SToken* pVal = pAlterInfo;
17✔
3496
      CHECK_NAME(checkPassword(pCxt, pVal, password));
17✔
3497
      tstrncpy(pStmt->password, password, TSDB_USET_PASSWORD_LONGLEN);
17✔
3498
      break;
17✔
3499
    }
3500
    case TSDB_ALTER_USER_ENABLE: {
5✔
3501
      SToken* pVal = pAlterInfo;
5✔
3502
      pStmt->enable = taosStr2Int8(pVal->z, NULL, 10);
5✔
3503
      break;
5✔
3504
    }
3505
    case TSDB_ALTER_USER_SYSINFO: {
16✔
3506
      SToken* pVal = pAlterInfo;
16✔
3507
      pStmt->sysinfo = taosStr2Int8(pVal->z, NULL, 10);
16✔
3508
      break;
16✔
3509
    }
3510
    case TSDB_ALTER_USER_CREATEDB: {
×
3511
      SToken* pVal = pAlterInfo;
×
3512
      pStmt->createdb = taosStr2Int8(pVal->z, NULL, 10);
×
3513
      break;
×
3514
    }
3515
    case TSDB_ALTER_USER_ADD_WHITE_LIST:
×
3516
    case TSDB_ALTER_USER_DROP_WHITE_LIST: {
3517
      SNodeList* pIpRangesNodeList = pAlterInfo;
×
3518
      pStmt->pNodeListIpRanges = pIpRangesNodeList;
×
3519
      pStmt->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
×
3520
      pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpV4Range));
×
3521
      CHECK_OUT_OF_MEM(pStmt->pIpRanges);
×
3522

3523
      pCxt->errCode = fillIpRangesFromWhiteList(pCxt, pIpRangesNodeList, pStmt->pIpRanges);
×
3524
      CHECK_PARSER_STATUS(pCxt);
×
3525
      break;
×
3526
    }
3527
    default:
×
3528
      break;
×
3529
  }
3530
  return (SNode*)pStmt;
38✔
3531
_err:
×
3532
  nodesDestroyNode((SNode*)pStmt);
×
3533
  return NULL;
×
3534
}
3535

3536
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
56✔
3537
  CHECK_PARSER_STATUS(pCxt);
56✔
3538
  CHECK_NAME(checkUserName(pCxt, pUserName));
56✔
3539
  SDropUserStmt* pStmt = NULL;
56✔
3540
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
56✔
3541
  CHECK_MAKE_NODE(pStmt);
56✔
3542
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
56✔
3543
  return (SNode*)pStmt;
56✔
3544
_err:
×
3545
  return NULL;
×
3546
}
3547

3548
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
44✔
3549
  CHECK_PARSER_STATUS(pCxt);
44✔
3550
  SCreateDnodeStmt* pStmt = NULL;
44✔
3551
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
44✔
3552
  CHECK_MAKE_NODE(pStmt);
44✔
3553
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
44✔
3554
    nodesDestroyNode((SNode*)pStmt);
×
3555
    return NULL;
×
3556
  }
3557
  return (SNode*)pStmt;
44✔
3558
_err:
×
3559
  return NULL;
×
3560
}
3561

3562
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
37✔
3563
  CHECK_PARSER_STATUS(pCxt);
37✔
3564
  SDropDnodeStmt* pStmt = NULL;
37✔
3565
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
37✔
3566
  CHECK_MAKE_NODE(pStmt);
37✔
3567
  if (TK_NK_INTEGER == pDnode->type) {
37✔
3568
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
13✔
3569
  } else {
3570
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
24✔
3571
      nodesDestroyNode((SNode*)pStmt);
×
3572
      return NULL;
×
3573
    }
3574
  }
3575
  pStmt->force = force;
37✔
3576
  pStmt->unsafe = unsafe;
37✔
3577
  return (SNode*)pStmt;
37✔
3578
_err:
×
3579
  return NULL;
×
3580
}
3581

3582
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
36✔
3583
                            const SToken* pValue) {
3584
  CHECK_PARSER_STATUS(pCxt);
36✔
3585
  SAlterDnodeStmt* pStmt = NULL;
36✔
3586
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
36✔
3587
  CHECK_MAKE_NODE(pStmt);
36✔
3588
  if (NULL != pDnode) {
36✔
3589
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
22✔
3590
  } else {
3591
    pStmt->dnodeId = -1;
14✔
3592
  }
3593
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
36✔
3594
  if (NULL != pValue) {
36✔
3595
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
8✔
3596
  }
3597
  return (SNode*)pStmt;
36✔
3598
_err:
×
3599
  return NULL;
×
3600
}
3601

3602
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
×
3603
  CHECK_PARSER_STATUS(pCxt);
×
3604
  SCreateAnodeStmt* pStmt = NULL;
×
3605
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
×
3606
  CHECK_MAKE_NODE(pStmt);
×
3607
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
×
3608
  return (SNode*)pStmt;
×
3609
_err:
×
3610
  return NULL;
×
3611
}
3612

3613
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
×
3614
  CHECK_PARSER_STATUS(pCxt);
×
3615
  SUpdateAnodeStmt* pStmt = NULL;
×
3616
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
×
3617
  CHECK_MAKE_NODE(pStmt);
×
3618
  if (NULL != pAnode) {
×
3619
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
3620
  } else {
3621
    pStmt->anodeId = -1;
×
3622
  }
3623
  return (SNode*)pStmt;
×
3624
_err:
×
3625
  return NULL;
×
3626
}
3627

3628
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
3629
  CHECK_PARSER_STATUS(pCxt);
×
3630
  SUpdateAnodeStmt* pStmt = NULL;
×
3631
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
3632
  CHECK_MAKE_NODE(pStmt);
×
3633
  if (NULL != pAnode) {
×
3634
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
3635
  } else {
3636
    pStmt->anodeId = -1;
×
3637
  }
3638
  return (SNode*)pStmt;
×
3639
_err:
×
3640
  return NULL;
×
3641
}
3642

3643
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
×
3644
  SToken config;
3645
  config.type = TK_NK_STRING;
×
3646
  config.z = "\"encrypt_key\"";
×
3647
  config.n = strlen(config.z);
×
3648
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
×
3649
}
3650

3651
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
22✔
3652
  if (!checkIndexName(pCxt, pIndexName)) {
22✔
3653
    return NULL;
×
3654
  }
3655
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
22✔
3656
}
3657

3658
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
26✔
3659
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
3660
  CHECK_PARSER_STATUS(pCxt);
26✔
3661
  SCreateIndexStmt* pStmt = NULL;
26✔
3662
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
26✔
3663
  CHECK_MAKE_NODE(pStmt);
26✔
3664
  pStmt->indexType = type;
26✔
3665
  pStmt->ignoreExists = ignoreExists;
26✔
3666

3667
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
26✔
3668
  if (strlen(pFullTable->table.dbName) == 0) {
26✔
3669
    // no db specified,
3670
    if (pCxt->pQueryCxt->db == NULL) {
×
3671
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
3672
      CHECK_PARSER_STATUS(pCxt);
×
3673
    } else {
3674
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
3675
    }
3676
  } else {
3677
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
26✔
3678
  }
3679
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
26✔
3680
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
26✔
3681
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
26✔
3682
  nodesDestroyNode(pIndexName);
26✔
3683
  nodesDestroyNode(pRealTable);
26✔
3684
  pStmt->pCols = pCols;
26✔
3685
  pStmt->pOptions = (SIndexOptions*)pOptions;
26✔
3686
  return (SNode*)pStmt;
26✔
3687
_err:
×
3688
  nodesDestroyNode(pIndexName);
×
3689
  nodesDestroyNode(pRealTable);
×
3690
  nodesDestroyNode(pOptions);
×
3691
  nodesDestroyList(pCols);
×
3692
  return NULL;
×
3693
}
3694

3695
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
9✔
3696
                         SNode* pStreamOptions) {
3697
  CHECK_PARSER_STATUS(pCxt);
9✔
3698
  SIndexOptions* pOptions = NULL;
9✔
3699
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
9✔
3700
  CHECK_MAKE_NODE(pOptions);
9✔
3701
  pOptions->pFuncs = pFuncs;
9✔
3702
  pOptions->pInterval = pInterval;
9✔
3703
  pOptions->pOffset = pOffset;
9✔
3704
  pOptions->pSliding = pSliding;
9✔
3705
  pOptions->pStreamOptions = pStreamOptions;
9✔
3706
  return (SNode*)pOptions;
9✔
3707
_err:
×
3708
  nodesDestroyNode(pInterval);
×
3709
  nodesDestroyNode(pOffset);
×
3710
  nodesDestroyNode(pSliding);
×
3711
  nodesDestroyNode(pStreamOptions);
×
3712
  return NULL;
×
3713
}
3714

3715
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
22✔
3716
  CHECK_PARSER_STATUS(pCxt);
22✔
3717
  SDropIndexStmt* pStmt = NULL;
22✔
3718
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
22✔
3719
  CHECK_MAKE_NODE(pStmt);
22✔
3720
  pStmt->ignoreNotExists = ignoreNotExists;
22✔
3721
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
22✔
3722
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
22✔
3723
  nodesDestroyNode(pIndexName);
22✔
3724
  return (SNode*)pStmt;
22✔
3725
_err:
×
3726
  nodesDestroyNode(pIndexName);
×
3727
  return NULL;
×
3728
}
3729

3730
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
25✔
3731
  CHECK_PARSER_STATUS(pCxt);
25✔
3732
  SCreateComponentNodeStmt* pStmt = NULL;
25✔
3733
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
25✔
3734
  CHECK_MAKE_NODE(pStmt);
25✔
3735
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
25✔
3736
  return (SNode*)pStmt;
25✔
3737
_err:
×
3738
  return NULL;
×
3739
}
3740

3741
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
16✔
3742
  CHECK_PARSER_STATUS(pCxt);
16✔
3743
  SDropComponentNodeStmt* pStmt = NULL;
16✔
3744
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
16✔
3745
  CHECK_MAKE_NODE(pStmt);
16✔
3746
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
16✔
3747
  return (SNode*)pStmt;
16✔
3748
_err:
×
3749
  return NULL;
×
3750
}
3751

3752
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
16✔
3753
  CHECK_PARSER_STATUS(pCxt);
16✔
3754
  SRestoreComponentNodeStmt* pStmt = NULL;
16✔
3755
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
16✔
3756
  CHECK_MAKE_NODE(pStmt);
16✔
3757
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
16✔
3758
  return (SNode*)pStmt;
16✔
3759
_err:
×
3760
  return NULL;
×
3761
}
3762

3763
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery) {
32✔
3764
  CHECK_PARSER_STATUS(pCxt);
32✔
3765
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
32✔
3766
  SCreateTopicStmt* pStmt = NULL;
32✔
3767
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
32✔
3768
  CHECK_MAKE_NODE(pStmt);
32✔
3769
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
32✔
3770
  pStmt->ignoreExists = ignoreExists;
32✔
3771
  pStmt->pQuery = pQuery;
32✔
3772
  return (SNode*)pStmt;
32✔
3773
_err:
×
3774
  nodesDestroyNode(pQuery);
×
3775
  return NULL;
×
3776
}
3777

3778
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
9✔
3779
                                  int8_t withMeta) {
3780
  CHECK_PARSER_STATUS(pCxt);
9✔
3781
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
9✔
3782
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
9✔
3783
  SCreateTopicStmt* pStmt = NULL;
9✔
3784
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
9✔
3785
  CHECK_MAKE_NODE(pStmt);
9✔
3786
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
9✔
3787
  pStmt->ignoreExists = ignoreExists;
9✔
3788
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
9✔
3789
  pStmt->withMeta = withMeta;
9✔
3790
  return (SNode*)pStmt;
9✔
3791
_err:
×
3792
  return NULL;
×
3793
}
3794

3795
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
16✔
3796
                                     int8_t withMeta, SNode* pWhere) {
3797
  CHECK_PARSER_STATUS(pCxt);
16✔
3798
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
16✔
3799
  SCreateTopicStmt* pStmt = NULL;
16✔
3800
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
16✔
3801
  CHECK_MAKE_NODE(pStmt);
16✔
3802
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
16✔
3803
  pStmt->ignoreExists = ignoreExists;
16✔
3804
  pStmt->withMeta = withMeta;
16✔
3805
  pStmt->pWhere = pWhere;
16✔
3806

3807
  tstrncpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
16✔
3808
  tstrncpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
16✔
3809
  nodesDestroyNode(pRealTable);
16✔
3810
  return (SNode*)pStmt;
16✔
3811
_err:
×
3812
  nodesDestroyNode(pRealTable);
×
3813
  nodesDestroyNode(pWhere);
×
3814
  return NULL;
×
3815
}
3816

3817
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName, bool force) {
37✔
3818
  CHECK_PARSER_STATUS(pCxt);
37✔
3819
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
37✔
3820
  SDropTopicStmt* pStmt = NULL;
37✔
3821
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
37✔
3822
  CHECK_MAKE_NODE(pStmt);
37✔
3823
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
37✔
3824
  pStmt->ignoreNotExists = ignoreNotExists;
37✔
3825
  pStmt->force = force;
37✔
3826
  return (SNode*)pStmt;
37✔
3827
_err:
×
3828
  return NULL;
×
3829
}
3830

3831
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName, bool force) {
12✔
3832
  CHECK_PARSER_STATUS(pCxt);
12✔
3833
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
12✔
3834
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
12✔
3835
  SDropCGroupStmt* pStmt = NULL;
12✔
3836
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
12✔
3837
  CHECK_MAKE_NODE(pStmt);
12✔
3838
  pStmt->ignoreNotExists = ignoreNotExists;
12✔
3839
  pStmt->force = force;
12✔
3840
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
12✔
3841
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
12✔
3842
  return (SNode*)pStmt;
12✔
3843
_err:
×
3844
  return NULL;
×
3845
}
3846

3847
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
×
3848
  CHECK_PARSER_STATUS(pCxt);
×
3849
  SAlterClusterStmt* pStmt = NULL;
×
3850
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
×
3851
  CHECK_MAKE_NODE(pStmt);
×
3852
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
×
3853
  if (NULL != pValue) {
×
3854
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
×
3855
  }
3856
  return (SNode*)pStmt;
×
3857
_err:
×
3858
  return NULL;
×
3859
}
3860

3861
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
31✔
3862
  CHECK_PARSER_STATUS(pCxt);
31✔
3863
  SAlterLocalStmt* pStmt = NULL;
31✔
3864
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
31✔
3865
  CHECK_MAKE_NODE(pStmt);
31✔
3866
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
31✔
3867
  if (NULL != pValue) {
31✔
3868
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
10✔
3869
  }
3870
  return (SNode*)pStmt;
31✔
3871
_err:
×
3872
  return NULL;
×
3873
}
3874

3875
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
27✔
3876
  CHECK_PARSER_STATUS(pCxt);
27✔
3877
  SExplainOptions* pOptions = NULL;
27✔
3878
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
27✔
3879
  CHECK_MAKE_NODE(pOptions);
27✔
3880
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
27✔
3881
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
27✔
3882
  return (SNode*)pOptions;
27✔
3883
_err:
×
3884
  return NULL;
×
3885
}
3886

3887
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
7✔
3888
  CHECK_PARSER_STATUS(pCxt);
7✔
3889
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
7✔
3890
  return pOptions;
7✔
3891
_err:
×
3892
  return NULL;
×
3893
}
3894

3895
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
5✔
3896
  CHECK_PARSER_STATUS(pCxt);
5✔
3897
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
5✔
3898
  return pOptions;
5✔
3899
_err:
×
3900
  return NULL;
×
3901
}
3902

3903
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
27✔
3904
  CHECK_PARSER_STATUS(pCxt);
27✔
3905
  SExplainStmt* pStmt = NULL;
27✔
3906
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
27✔
3907
  CHECK_MAKE_NODE(pStmt);
27✔
3908
  pStmt->analyze = analyze;
27✔
3909
  pStmt->pOptions = (SExplainOptions*)pOptions;
27✔
3910
  pStmt->pQuery = pQuery;
27✔
3911
  return (SNode*)pStmt;
27✔
3912
_err:
×
3913
  nodesDestroyNode(pOptions);
×
3914
  nodesDestroyNode(pQuery);
×
3915
  return NULL;
×
3916
}
3917

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

3932
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
34✔
3933
  CHECK_PARSER_STATUS(pCxt);
34✔
3934
  SNode* pStmt = NULL;
34✔
3935
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
34✔
3936
  CHECK_MAKE_NODE(pStmt);
34✔
3937
  return pStmt;
34✔
3938
_err:
×
3939
  return NULL;
×
3940
}
3941

3942
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
100✔
3943
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
100✔
3944
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
41✔
3945
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
59✔
3946
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
59✔
3947
  } else {
3948
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
3949
                                            "udf programming language supports c and python");
3950
  }
3951
  return pCxt->errCode;
100✔
3952
}
3953

3954
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
100✔
3955
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
3956
                                bool orReplace) {
3957
  CHECK_PARSER_STATUS(pCxt);
100✔
3958
  if (pLibPath->n <= 2) {
100✔
3959
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3960
    CHECK_PARSER_STATUS(pCxt);
×
3961
  }
3962
  int8_t language = 0;
100✔
3963
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
100✔
3964
  CHECK_PARSER_STATUS(pCxt);
100✔
3965
  SCreateFunctionStmt* pStmt = NULL;
100✔
3966
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
100✔
3967
  CHECK_MAKE_NODE(pStmt);
100✔
3968
  pStmt->orReplace = orReplace;
100✔
3969
  pStmt->ignoreExists = ignoreExists;
100✔
3970
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
100✔
3971
  pStmt->isAgg = aggFunc;
100✔
3972
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
100✔
3973
  pStmt->outputDt = dataType;
100✔
3974
  pStmt->bufSize = bufSize;
100✔
3975
  pStmt->language = language;
100✔
3976
  return (SNode*)pStmt;
100✔
3977
_err:
×
3978
  return NULL;
×
3979
}
3980

3981
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
26✔
3982
  CHECK_PARSER_STATUS(pCxt);
26✔
3983
  SDropFunctionStmt* pStmt = NULL;
26✔
3984
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
26✔
3985
  CHECK_MAKE_NODE(pStmt);
26✔
3986
  pStmt->ignoreNotExists = ignoreNotExists;
26✔
3987
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
26✔
3988
  return (SNode*)pStmt;
26✔
3989
_err:
×
3990
  return NULL;
×
3991
}
3992

3993
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
×
3994
  SCreateViewStmt* pStmt = NULL;
×
3995
  CHECK_PARSER_STATUS(pCxt);
×
3996
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
×
3997
  CHECK_MAKE_NODE(pStmt);
×
3998
  int32_t i = pAs->n;
×
3999
  while (isspace(*(pAs->z + i))) {
×
4000
    ++i;
×
4001
  }
4002
  pStmt->pQuerySql = tstrdup(pAs->z + i);
×
4003
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
×
4004
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
×
4005
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
×
4006
  nodesDestroyNode(pView);
×
4007
  pStmt->orReplace = orReplace;
×
4008
  pStmt->pQuery = pQuery;
×
4009
  return (SNode*)pStmt;
×
4010
_err:
×
4011
  nodesDestroyNode(pView);
×
4012
  nodesDestroyNode(pQuery);
×
4013
  nodesDestroyNode((SNode*)pStmt);
×
4014
  return NULL;
×
4015
}
4016

4017
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
×
4018
  CHECK_PARSER_STATUS(pCxt);
×
4019
  SDropViewStmt* pStmt = NULL;
×
4020
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
×
4021
  CHECK_MAKE_NODE(pStmt);
×
4022
  pStmt->ignoreNotExists = ignoreNotExists;
×
4023
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
×
4024
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
×
4025
  nodesDestroyNode(pView);
×
4026
  return (SNode*)pStmt;
×
4027
_err:
×
4028
  nodesDestroyNode(pView);
×
4029
  return NULL;
×
4030
}
4031

4032
SNode* createStreamOptions(SAstCreateContext* pCxt) {
62✔
4033
  CHECK_PARSER_STATUS(pCxt);
62✔
4034
  SStreamOptions* pOptions = NULL;
62✔
4035
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OPTIONS, (SNode**)&pOptions);
62✔
4036
  CHECK_MAKE_NODE(pOptions);
62✔
4037
  pOptions->triggerType = STREAM_TRIGGER_WINDOW_CLOSE;
62✔
4038
  pOptions->fillHistory = STREAM_DEFAULT_FILL_HISTORY;
62✔
4039
  pOptions->ignoreExpired = STREAM_DEFAULT_IGNORE_EXPIRED;
62✔
4040
  pOptions->ignoreUpdate = STREAM_DEFAULT_IGNORE_UPDATE;
62✔
4041
  pOptions->runHistoryAsync = false;
62✔
4042
  return (SNode*)pOptions;
62✔
4043
_err:
×
4044
  return NULL;
×
4045
}
4046

4047
static int8_t getTriggerType(uint32_t tokenType) {
10✔
4048
  switch (tokenType) {
10✔
4049
    case TK_AT_ONCE:
1✔
4050
      return STREAM_TRIGGER_AT_ONCE;
1✔
4051
    case TK_WINDOW_CLOSE:
1✔
4052
      return STREAM_TRIGGER_WINDOW_CLOSE;
1✔
4053
    case TK_MAX_DELAY:
8✔
4054
      return STREAM_TRIGGER_MAX_DELAY;
8✔
4055
    case TK_FORCE_WINDOW_CLOSE:
×
4056
      return STREAM_TRIGGER_FORCE_WINDOW_CLOSE;
×
4057
    case TK_CONTINUOUS_WINDOW_CLOSE:
×
4058
      return STREAM_TRIGGER_CONTINUOUS_WINDOW_CLOSE;
×
4059
    default:
×
4060
      break;
×
4061
  }
4062
  return STREAM_TRIGGER_WINDOW_CLOSE;
×
4063
}
4064

4065
SNode* setStreamOptions(SAstCreateContext* pCxt, SNode* pOptions, EStreamOptionsSetFlag setflag, SToken* pToken,
47✔
4066
                        SNode* pNode, bool runHistoryAsync) {
4067
  SStreamOptions* pStreamOptions = (SStreamOptions*)pOptions;
47✔
4068
  if (BIT_FLAG_TEST_MASK(setflag, pStreamOptions->setFlag)) {
47✔
4069
    pCxt->errCode =
×
4070
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream options each item is only set once");
×
4071
    return pOptions;
×
4072
  }
4073

4074
  switch (setflag) {
47✔
4075
    case SOPT_TRIGGER_TYPE_SET:
10✔
4076
      pStreamOptions->triggerType = getTriggerType(pToken->type);
10✔
4077
      if (STREAM_TRIGGER_MAX_DELAY == pStreamOptions->triggerType) {
10✔
4078
        pStreamOptions->pDelay = pNode;
8✔
4079
      }
4080
      if (STREAM_TRIGGER_CONTINUOUS_WINDOW_CLOSE == pStreamOptions->triggerType) {
10✔
4081
        pStreamOptions->pRecInterval = pNode;
×
4082
      }
4083
      break;
10✔
4084
    case SOPT_WATERMARK_SET:
9✔
4085
      pStreamOptions->pWatermark = pNode;
9✔
4086
      break;
9✔
4087
    case SOPT_DELETE_MARK_SET:
×
4088
      pStreamOptions->pDeleteMark = pNode;
×
4089
      break;
×
4090
    case SOPT_FILL_HISTORY_SET:
10✔
4091
      pStreamOptions->fillHistory = taosStr2Int8(pToken->z, NULL, 10);
10✔
4092
      break;
10✔
4093
    case SOPT_IGNORE_EXPIRED_SET:
9✔
4094
      pStreamOptions->ignoreExpired = taosStr2Int8(pToken->z, NULL, 10);
9✔
4095
      break;
9✔
4096
    case SOPT_IGNORE_UPDATE_SET:
9✔
4097
      pStreamOptions->ignoreUpdate = taosStr2Int8(pToken->z, NULL, 10);
9✔
4098
      break;
9✔
4099
    default:
×
4100
      break;
×
4101
  }
4102
  BIT_FLAG_SET_MASK(pStreamOptions->setFlag, setflag);
47✔
4103
  pStreamOptions->runHistoryAsync = runHistoryAsync;
47✔
4104
  return pOptions;
47✔
4105
}
4106

4107
static bool validateNotifyUrl(const char* url) {
×
4108
  const char* prefix[] = {"http://", "https://", "ws://", "wss://"};
×
4109
  const char* host = NULL;
×
4110

4111
  if (!url || *url == '\0') return false;
×
4112

4113
  for (int32_t i = 0; i < ARRAY_SIZE(prefix); ++i) {
×
4114
    if (taosStrncasecmp(url, prefix[i], strlen(prefix[i])) == 0) {
×
4115
      host = url + strlen(prefix[i]);
×
4116
      break;
×
4117
    }
4118
  }
4119

4120
  return (host != NULL) && (*host != '\0') && (*host != '/');
×
4121
}
4122

4123
SNode* createStreamNotifyOptions(SAstCreateContext* pCxt, SNodeList* pAddrUrls, SNodeList* pEventTypes) {
×
4124
  SNode*                 pNode = NULL;
×
4125
  EStreamNotifyEventType eventTypes = 0;
×
4126
  const char*            eWindowOpenStr = "WINDOW_OPEN";
×
4127
  const char*            eWindowCloseStr = "WINDOW_CLOSE";
×
4128

4129
  CHECK_PARSER_STATUS(pCxt);
×
4130

4131
  if (LIST_LENGTH(pAddrUrls) == 0) {
×
4132
    pCxt->errCode =
×
4133
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "notification address cannot be empty");
×
4134
    goto _err;
×
4135
  }
4136

4137
  FOREACH(pNode, pAddrUrls) {
×
4138
    char *url = ((SValueNode*)pNode)->literal;
×
4139
    if (strlen(url) >= TSDB_STREAM_NOTIFY_URL_LEN) {
×
4140
      pCxt->errCode =
×
4141
          generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4142
                                  "notification address \"%s\" exceed maximum length %d", url, TSDB_STREAM_NOTIFY_URL_LEN);
4143
      goto _err;
×
4144
    }
4145
    if (!validateNotifyUrl(url)) {
×
4146
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4147
                                              "invalid notification address \"%s\"", url);
4148
      goto _err;
×
4149
    }
4150
  }
4151

4152
  if (LIST_LENGTH(pEventTypes) == 0) {
×
4153
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4154
                                            "event types must be specified for notification");
4155
    goto _err;
×
4156
  }
4157

4158
  FOREACH(pNode, pEventTypes) {
×
4159
    char *eventStr = ((SValueNode *)pNode)->literal;
×
4160
    if (taosStrncasecmp(eventStr, eWindowOpenStr, strlen(eWindowOpenStr) + 1) == 0) {
×
4161
      BIT_FLAG_SET_MASK(eventTypes, SNOTIFY_EVENT_WINDOW_OPEN);
×
4162
    } else if (taosStrncasecmp(eventStr, eWindowCloseStr, strlen(eWindowCloseStr) + 1) == 0) {
×
4163
      BIT_FLAG_SET_MASK(eventTypes, SNOTIFY_EVENT_WINDOW_CLOSE);
×
4164
    } else {
4165
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4166
                                              "invalid event type '%s' for notification", eventStr);
4167
      goto _err;
×
4168
    }
4169
  }
4170

4171
  SStreamNotifyOptions* pNotifyOptions = NULL;
×
4172
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_NOTIFY_OPTIONS, (SNode**)&pNotifyOptions);
×
4173
  CHECK_MAKE_NODE(pNotifyOptions);
×
4174
  pNotifyOptions->pAddrUrls = pAddrUrls;
×
4175
  pNotifyOptions->eventTypes = eventTypes;
×
4176
  pNotifyOptions->errorHandle = SNOTIFY_ERROR_HANDLE_PAUSE;
×
4177
  pNotifyOptions->notifyHistory = false;
×
4178
  nodesDestroyList(pEventTypes);
×
4179
  return (SNode*)pNotifyOptions;
×
4180
_err:
×
4181
  nodesDestroyList(pAddrUrls);
×
4182
  nodesDestroyList(pEventTypes);
×
4183
  return NULL;
×
4184
}
4185

4186
SNode* setStreamNotifyOptions(SAstCreateContext* pCxt, SNode* pNode, EStreamNotifyOptionSetFlag setFlag,
×
4187
                              SToken* pToken) {
4188
  CHECK_PARSER_STATUS(pCxt);
×
4189

4190
  SStreamNotifyOptions* pNotifyOption = (SStreamNotifyOptions*)pNode;
×
4191
  if (BIT_FLAG_TEST_MASK(pNotifyOption->setFlag, setFlag)) {
×
4192
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4193
                                         "stream notify options each item can only be set once");
4194
    goto _err;
×
4195
  }
4196
  switch (setFlag) {
×
4197
    case SNOTIFY_OPT_ERROR_HANDLE_SET:
×
4198
      pNotifyOption->errorHandle = (pToken->type == TK_DROP) ? SNOTIFY_ERROR_HANDLE_DROP : SNOTIFY_ERROR_HANDLE_PAUSE;
×
4199
      break;
×
4200
    case SNOTIFY_OPT_NOTIFY_HISTORY_SET:
×
4201
      pNotifyOption->notifyHistory = taosStr2Int8(pToken->z, NULL, 10);
×
4202
      break;
×
4203
    default:
×
4204
      break;
×
4205
  }
4206
  BIT_FLAG_SET_MASK(pNotifyOption->setFlag, setFlag);
×
4207
  return pNode;
×
4208
_err:
×
4209
  nodesDestroyNode(pNode);
×
4210
  return NULL;
×
4211
}
4212

4213
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pStreamName, SNode* pRealTable,
53✔
4214
                              SNode* pOptions, SNodeList* pTags, SNode* pSubtable, SNode* pQuery, SNodeList* pCols,
4215
                              SNode* pNotifyOptions) {
4216
  CHECK_PARSER_STATUS(pCxt);
53✔
4217
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
53✔
4218
  SCreateStreamStmt* pStmt = NULL;
53✔
4219
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
53✔
4220
  CHECK_MAKE_NODE(pStmt);
53✔
4221
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
53✔
4222
  tstrncpy(pStmt->targetDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
53✔
4223
  tstrncpy(pStmt->targetTabName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
53✔
4224
  nodesDestroyNode(pRealTable);
53✔
4225
  pStmt->ignoreExists = ignoreExists;
53✔
4226
  pStmt->pOptions = (SStreamOptions*)pOptions;
53✔
4227
  pStmt->pQuery = pQuery;
53✔
4228
  pStmt->pTags = pTags;
53✔
4229
  pStmt->pSubtable = pSubtable;
53✔
4230
  pStmt->pCols = pCols;
53✔
4231
  pStmt->pNotifyOptions = (SStreamNotifyOptions*)pNotifyOptions;
53✔
4232
  return (SNode*)pStmt;
53✔
4233
_err:
×
4234
  nodesDestroyNode(pRealTable);
×
4235
  nodesDestroyNode(pQuery);
×
4236
  nodesDestroyNode(pOptions);
×
4237
  nodesDestroyList(pTags);
×
4238
  nodesDestroyNode(pSubtable);
×
4239
  nodesDestroyList(pCols);
×
4240
  nodesDestroyNode(pNotifyOptions);
×
4241
  return NULL;
×
4242
}
4243

4244
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
9✔
4245
  CHECK_PARSER_STATUS(pCxt);
9✔
4246
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
9✔
4247
  SDropStreamStmt* pStmt = NULL;
9✔
4248
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
9✔
4249
  CHECK_MAKE_NODE(pStmt);
9✔
4250
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
9✔
4251
  pStmt->ignoreNotExists = ignoreNotExists;
9✔
4252
  return (SNode*)pStmt;
9✔
4253
_err:
×
4254
  return NULL;
×
4255
}
4256

4257
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
8✔
4258
  CHECK_PARSER_STATUS(pCxt);
8✔
4259
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
8✔
4260
  SPauseStreamStmt* pStmt = NULL;
8✔
4261
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
8✔
4262
  CHECK_MAKE_NODE(pStmt);
8✔
4263
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
8✔
4264
  pStmt->ignoreNotExists = ignoreNotExists;
8✔
4265
  return (SNode*)pStmt;
8✔
4266
_err:
×
4267
  return NULL;
×
4268
}
4269

4270
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated,
8✔
4271
                              SToken* pStreamName) {
4272
  CHECK_PARSER_STATUS(pCxt);
8✔
4273
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
8✔
4274
  SResumeStreamStmt* pStmt = NULL;
8✔
4275
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
8✔
4276
  CHECK_MAKE_NODE(pStmt);
8✔
4277
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
8✔
4278
  pStmt->ignoreNotExists = ignoreNotExists;
8✔
4279
  pStmt->ignoreUntreated = ignoreUntreated;
8✔
4280
  return (SNode*)pStmt;
8✔
4281
_err:
×
4282
  return NULL;
×
4283
}
4284

4285
SNode* createResetStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
×
4286
  CHECK_PARSER_STATUS(pCxt);
×
4287
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
×
4288
  SPauseStreamStmt* pStmt = NULL;
×
4289
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_STREAM_STMT, (SNode**)&pStmt);
×
4290
  CHECK_MAKE_NODE(pStmt);
×
4291
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
×
4292
  pStmt->ignoreNotExists = ignoreNotExists;
×
4293
  return (SNode*)pStmt;
×
4294
_err:
×
4295
  return NULL;
×
4296
}
4297

4298

4299
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
×
4300
  CHECK_PARSER_STATUS(pCxt);
×
4301
  SKillStmt* pStmt = NULL;
×
4302
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
4303
  CHECK_MAKE_NODE(pStmt);
×
4304
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
×
4305
  return (SNode*)pStmt;
×
4306
_err:
×
4307
  return NULL;
×
4308
}
4309

4310
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
×
4311
  CHECK_PARSER_STATUS(pCxt);
×
4312
  SKillQueryStmt* pStmt = NULL;
×
4313
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
×
4314
  CHECK_MAKE_NODE(pStmt);
×
4315
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
×
4316
  return (SNode*)pStmt;
×
4317
_err:
×
4318
  return NULL;
×
4319
}
4320

4321
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
4✔
4322
  CHECK_PARSER_STATUS(pCxt);
4✔
4323
  SBalanceVgroupStmt* pStmt = NULL;
4✔
4324
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
4✔
4325
  CHECK_MAKE_NODE(pStmt);
4✔
4326
  return (SNode*)pStmt;
4✔
4327
_err:
×
4328
  return NULL;
×
4329
}
4330

4331
SNode* createAssignLeaderStmt(SAstCreateContext* pCxt) {
×
4332
  CHECK_PARSER_STATUS(pCxt);
×
4333
  SAssignLeaderStmt* pStmt = NULL;
×
4334
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ASSIGN_LEADER_STMT, (SNode**)&pStmt);
×
4335
  CHECK_MAKE_NODE(pStmt);
×
4336
  return (SNode*)pStmt;
×
4337
_err:
×
4338
  return NULL;
×
4339
}
4340

4341
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
4✔
4342
  CHECK_PARSER_STATUS(pCxt);
4✔
4343
  SBalanceVgroupLeaderStmt* pStmt = NULL;
4✔
4344
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
4✔
4345
  CHECK_MAKE_NODE(pStmt);
4✔
4346
  if (NULL != pVgId && NULL != pVgId->z) {
4✔
4347
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
×
4348
  }
4349
  return (SNode*)pStmt;
4✔
4350
_err:
×
4351
  return NULL;
×
4352
}
4353

4354
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
4355
  CHECK_PARSER_STATUS(pCxt);
×
4356
  SBalanceVgroupLeaderStmt* pStmt = NULL;
×
4357
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
×
4358
  CHECK_MAKE_NODE(pStmt);
×
4359
  if (NULL != pDbName) {
×
4360
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
×
4361
  }
4362
  return (SNode*)pStmt;
×
4363
_err:
×
4364
  return NULL;
×
4365
}
4366

4367
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
4✔
4368
  CHECK_PARSER_STATUS(pCxt);
4✔
4369
  SMergeVgroupStmt* pStmt = NULL;
4✔
4370
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
4✔
4371
  CHECK_MAKE_NODE(pStmt);
4✔
4372
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
4✔
4373
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
4✔
4374
  return (SNode*)pStmt;
4✔
4375
_err:
×
4376
  return NULL;
×
4377
}
4378

4379
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
8✔
4380
  CHECK_PARSER_STATUS(pCxt);
8✔
4381
  SRedistributeVgroupStmt* pStmt = NULL;
8✔
4382
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
8✔
4383
  CHECK_MAKE_NODE(pStmt);
8✔
4384
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
8✔
4385
  pStmt->pDnodes = pDnodes;
8✔
4386
  return (SNode*)pStmt;
8✔
4387
_err:
×
4388
  nodesDestroyList(pDnodes);
×
4389
  return NULL;
×
4390
}
4391

4392
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
4✔
4393
  CHECK_PARSER_STATUS(pCxt);
4✔
4394
  SSplitVgroupStmt* pStmt = NULL;
4✔
4395
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
4✔
4396
  CHECK_MAKE_NODE(pStmt);
4✔
4397
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
4✔
4398
  return (SNode*)pStmt;
4✔
4399
_err:
×
4400
  return NULL;
×
4401
}
4402

4403
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
4404
  CHECK_PARSER_STATUS(pCxt);
×
4405
  SNode* pStmt = NULL;
×
4406
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
4407
  CHECK_MAKE_NODE(pStmt);
×
4408
  return pStmt;
×
4409
_err:
×
4410
  return NULL;
×
4411
}
4412

4413
SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
25✔
4414
                       SNode* pTagCond) {
4415
  CHECK_PARSER_STATUS(pCxt);
25✔
4416
  CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
25✔
4417
  CHECK_NAME(checkUserName(pCxt, pUserName));
25✔
4418
  CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
25✔
4419
  SGrantStmt* pStmt = NULL;
25✔
4420
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
25✔
4421
  CHECK_MAKE_NODE(pStmt);
25✔
4422
  pStmt->privileges = privileges;
25✔
4423
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
25✔
4424
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
25✔
4425
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
×
4426
  }
4427
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
25✔
4428
  pStmt->pTagCond = pTagCond;
25✔
4429
  return (SNode*)pStmt;
25✔
4430
_err:
×
4431
  nodesDestroyNode(pTagCond);
×
4432
  return NULL;
×
4433
}
4434

4435
SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
24✔
4436
                        SNode* pTagCond) {
4437
  CHECK_PARSER_STATUS(pCxt);
24✔
4438
  CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
24✔
4439
  CHECK_NAME(checkUserName(pCxt, pUserName));
24✔
4440
  CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
24✔
4441
  SRevokeStmt* pStmt = NULL;
24✔
4442
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
24✔
4443
  CHECK_MAKE_NODE(pStmt);
24✔
4444
  pStmt->privileges = privileges;
24✔
4445
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
24✔
4446
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
24✔
4447
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
×
4448
  }
4449
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
24✔
4450
  pStmt->pTagCond = pTagCond;
24✔
4451
  return (SNode*)pStmt;
24✔
4452
_err:
×
4453
  nodesDestroyNode(pTagCond);
×
4454
  return NULL;
×
4455
}
4456

4457
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
666✔
4458
  SFunctionNode* pFunc = NULL;
666✔
4459
  CHECK_PARSER_STATUS(pCxt);
666✔
4460
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
666✔
4461
  CHECK_MAKE_NODE(pFunc);
666✔
4462
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
666✔
4463
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
666✔
4464
  CHECK_MAKE_NODE(pCol);
666✔
4465
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
666✔
4466
  CHECK_PARSER_STATUS(pCxt);
666✔
4467
  return (SNode*)pFunc;
666✔
4468
_err:
×
4469
  nodesDestroyNode((SNode*)pFunc);
×
4470
  return NULL;
×
4471
}
4472

4473
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
222✔
4474
  SDeleteStmt* pStmt = NULL;
222✔
4475
  CHECK_PARSER_STATUS(pCxt);
222✔
4476
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
222✔
4477
  CHECK_MAKE_NODE(pStmt);
222✔
4478
  pStmt->pFromTable = pTable;
222✔
4479
  pStmt->pWhere = pWhere;
222✔
4480
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
222✔
4481
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
222✔
4482
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
222✔
4483
  CHECK_MAKE_NODE(pStmt->pCountFunc);
222✔
4484
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
222✔
4485
  CHECK_MAKE_NODE(pStmt->pLastFunc);
222✔
4486
  return (SNode*)pStmt;
222✔
4487
_err:
×
4488
  nodesDestroyNode((SNode*)pStmt);
×
4489
  nodesDestroyNode(pTable);
×
4490
  nodesDestroyNode(pWhere);
×
4491
  return NULL;
×
4492
}
4493

4494
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
7✔
4495
  CHECK_PARSER_STATUS(pCxt);
7✔
4496
  SInsertStmt* pStmt = NULL;
7✔
4497
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
7✔
4498
  CHECK_MAKE_NODE(pStmt);
7✔
4499
  pStmt->pTable = pTable;
7✔
4500
  pStmt->pCols = pCols;
7✔
4501
  pStmt->pQuery = pQuery;
7✔
4502
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
7✔
4503
    tstrncpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
6✔
4504
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
1✔
4505
    tstrncpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
1✔
4506
  }
4507
  return (SNode*)pStmt;
7✔
4508
_err:
×
4509
  nodesDestroyNode(pTable);
×
4510
  nodesDestroyNode(pQuery);
×
4511
  nodesDestroyList(pCols);
×
4512
  return NULL;
×
4513
}
4514

4515
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
2✔
4516
                            SNode* pRealTable, SNode* pInterval) {
4517
  SCreateTSMAStmt* pStmt = NULL;
2✔
4518
  CHECK_PARSER_STATUS(pCxt);
2✔
4519
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
2✔
4520
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
2✔
4521
  CHECK_MAKE_NODE(pStmt);
2✔
4522

4523
  pStmt->ignoreExists = ignoreExists;
2✔
4524
  if (!pOptions) {
2✔
4525
    // recursive tsma
4526
    pStmt->pOptions = NULL;
×
4527
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
×
4528
    CHECK_MAKE_NODE(pStmt->pOptions);
×
4529
    pStmt->pOptions->recursiveTsma = true;
×
4530
  } else {
4531
    pStmt->pOptions = (STSMAOptions*)pOptions;
2✔
4532
  }
4533
  pStmt->pOptions->pInterval = pInterval;
2✔
4534
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
2✔
4535

4536
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
2✔
4537
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
2✔
4538
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
2✔
4539
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
2✔
4540
  nodesDestroyNode(pRealTable);
2✔
4541

4542
  return (SNode*)pStmt;
2✔
4543
_err:
×
4544
  nodesDestroyNode((SNode*)pStmt);
×
4545
  nodesDestroyNode(pOptions);
×
4546
  nodesDestroyNode(pRealTable);
×
4547
  nodesDestroyNode(pInterval);
×
4548
  return NULL;
×
4549
}
4550

4551
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
2✔
4552
  CHECK_PARSER_STATUS(pCxt);
2✔
4553
  STSMAOptions* pOptions = NULL;
2✔
4554
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
2✔
4555
  CHECK_MAKE_NODE(pOptions);
2✔
4556
  pOptions->pFuncs = pFuncs;
2✔
4557
  return (SNode*)pOptions;
2✔
4558
_err:
×
4559
  nodesDestroyList(pFuncs);
×
4560
  return NULL;
×
4561
}
4562

4563
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
4564
  CHECK_PARSER_STATUS(pCxt);
×
4565
  STSMAOptions* pOptions = NULL;
×
4566
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
4567
  CHECK_MAKE_NODE(pOptions);
×
4568
  return (SNode*)pOptions;
×
4569
_err:
×
4570
  return NULL;
×
4571
}
4572

4573
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
×
4574
  CHECK_PARSER_STATUS(pCxt);
×
4575
  SDropTSMAStmt* pStmt = NULL;
×
4576
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
×
4577
  CHECK_MAKE_NODE(pStmt);
×
4578

4579
  pStmt->ignoreNotExists = ignoreNotExists;
×
4580
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
×
4581

4582
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
×
4583
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
×
4584

4585
  nodesDestroyNode(pRealTable);
×
4586
  return (SNode*)pStmt;
×
4587
_err:
×
4588
  nodesDestroyNode(pRealTable);
×
4589
  return NULL;
×
4590
}
4591

4592
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
×
4593
  CHECK_PARSER_STATUS(pCxt);
×
4594

4595
  SShowStmt* pStmt = NULL;
×
4596
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
×
4597
  CHECK_MAKE_NODE(pStmt);
×
4598

4599
  pStmt->pDbName = dbName;
×
4600
  return (SNode*)pStmt;
×
4601
_err:
×
4602
  nodesDestroyNode(dbName);
×
4603
  return NULL;
×
4604
}
4605
SNode* createShowDiskUsageStmt(SAstCreateContext* pCxt, SNode* dbName, ENodeType type) {
×
4606
  CHECK_PARSER_STATUS(pCxt);
×
4607
  if (NULL == dbName) {
×
4608
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
4609
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4610
    CHECK_PARSER_STATUS(pCxt);
×
4611
  }
4612

4613
  SShowStmt* pStmt = NULL;
×
4614
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
4615
  CHECK_MAKE_NODE(pStmt);
×
4616

4617
  pStmt->pDbName = dbName;
×
4618
  return (SNode*)pStmt;
×
4619
_err:
×
4620
  nodesDestroyNode(dbName);
×
4621
  return NULL;
×
4622
}
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