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

taosdata / TDengine / #4308

14 Jun 2025 02:06PM UTC coverage: 62.454% (-0.3%) from 62.777%
#4308

push

travis-ci

web-flow
fix: taosdump windows pthread_mutex_unlock crash(3.0) (#31357)

* fix: windows pthread_mutex_unlock crash

* enh: sync from main fix taosdump crash windows

* fix: restore .github action branch to main

153985 of 315105 branches covered (48.87%)

Branch coverage included in aggregate %.

238120 of 312727 relevant lines covered (76.14%)

6462519.65 hits per line

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

71.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) {
1,216,894✔
66
  memset(pCxt, 0, sizeof(SAstCreateContext));
1,216,894✔
67
  pCxt->pQueryCxt = pParseCxt;
1,216,894✔
68
  pCxt->msgBuf.buf = pParseCxt->pMsg;
1,216,894✔
69
  pCxt->msgBuf.len = pParseCxt->msgLen;
1,216,894✔
70
  pCxt->notSupport = false;
1,216,894✔
71
  pCxt->pRootNode = NULL;
1,216,894✔
72
  pCxt->placeholderNo = 0;
1,216,894✔
73
  pCxt->pPlaceholderValues = NULL;
1,216,894✔
74
  pCxt->errCode = TSDB_CODE_SUCCESS;
1,216,894✔
75
}
1,216,894✔
76

77
static void trimEscape(SAstCreateContext* pCxt, SToken* pName) {
16,264,866✔
78
  // todo need to deal with `ioo``ii` -> ioo`ii: done
79
  if (NULL != pName && pName->n > 1 && TS_ESCAPE_CHAR == pName->z[0]) {
16,264,866✔
80
    if (!pCxt->pQueryCxt->hasDupQuoteChar) {
143,444✔
81
      pName->z += 1;
142,640✔
82
      pName->n -= 2;
142,640✔
83
    } else {
84
      int32_t i = 1, j = 0;
804✔
85
      for (; i < pName->n - 1; ++i) {
4,920✔
86
        if ((pName->z[i] == TS_ESCAPE_CHAR) && (pName->z[i + 1] == TS_ESCAPE_CHAR)) {
4,116!
87
          pName->z[j++] = TS_ESCAPE_CHAR;
1,940✔
88
          ++i;
1,940✔
89
        } else {
90
          pName->z[j++] = pName->z[i];
2,176✔
91
        }
92
      }
93
      pName->n = j;
804✔
94
    }
95
  }
96
}
16,264,866✔
97

98
static bool checkUserName(SAstCreateContext* pCxt, SToken* pUserName) {
2,484✔
99
  if (NULL == pUserName) {
2,484!
100
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
101
  } else {
102
    if (pUserName->n >= TSDB_USER_LEN) {
2,484✔
103
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
2✔
104
    }
105
  }
106
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
2,484✔
107
    trimEscape(pCxt, pUserName);
2,482✔
108
  }
109
  return TSDB_CODE_SUCCESS == pCxt->errCode;
2,484✔
110
}
111

112
static bool invalidPassword(const char* pPassword) {
3✔
113
  regex_t regex;
114

115
  if (regcomp(&regex, "[ '\"`\\]", REG_EXTENDED | REG_ICASE) != 0) {
3!
116
    return false;
×
117
  }
118

119
  /* Execute regular expression */
120
  int32_t res = regexec(&regex, pPassword, 0, NULL, 0);
3✔
121
  regfree(&regex);
3✔
122
  return 0 == res;
3✔
123
}
124

125
static bool invalidStrongPassword(const char* pPassword) {
295✔
126
  if (strcmp(pPassword, "taosdata") == 0) {
295✔
127
    return false;
54✔
128
  }
129

130
  bool charTypes[4] = {0};
241✔
131
  for (int32_t i = 0; i < strlen(pPassword); ++i) {
3,274✔
132
    if (taosIsBigChar(pPassword[i])) {
3,037✔
133
      charTypes[0] = true;
298✔
134
    } else if (taosIsSmallChar(pPassword[i])) {
2,739✔
135
      charTypes[1] = true;
1,685✔
136
    } else if (taosIsNumberChar(pPassword[i])) {
1,054✔
137
      charTypes[2] = true;
722✔
138
    } else if (taosIsSpecialChar(pPassword[i])) {
332✔
139
      charTypes[3] = true;
328✔
140
    } else {
141
      return true;
4✔
142
    }
143
  }
144

145
  int32_t numOfTypes = 0;
237✔
146
  for (int32_t i = 0; i < 4; ++i) {
1,185✔
147
    numOfTypes += charTypes[i];
948✔
148
  }
149

150
  if (numOfTypes < 3) {
237✔
151
    return true;
52✔
152
  }
153

154
  return false;
185✔
155
}
156

157
static bool checkPassword(SAstCreateContext* pCxt, const SToken* pPasswordToken, char* pPassword) {
356✔
158
  if (NULL == pPasswordToken) {
356!
159
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
160
  } else if (pPasswordToken->n >= (TSDB_USET_PASSWORD_LONGLEN + 2)) {
356✔
161
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
4✔
162
  } else {
163
    strncpy(pPassword, pPasswordToken->z, pPasswordToken->n);
352✔
164
    (void)strdequote(pPassword);
352✔
165
    if (strtrim(pPassword) < TSDB_PASSWORD_MIN_LEN) {
352✔
166
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
54✔
167
    } else {
168
      if (tsEnableStrongPassword) {
298✔
169
        if (invalidStrongPassword(pPassword)) {
295✔
170
          pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
56✔
171
        }
172
      } else {
173
        if (invalidPassword(pPassword)) {
3!
174
          pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
×
175
        }
176
      }
177
    }
178
  }
179
  return TSDB_CODE_SUCCESS == pCxt->errCode;
356✔
180
}
181

182
static bool checkImportPassword(SAstCreateContext* pCxt, const SToken* pPasswordToken, char* pPassword) {
2✔
183
  if (NULL == pPasswordToken) {
2!
184
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
185
  } else if (pPasswordToken->n > (32 + 2)) {
2!
186
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
187
  } else {
188
    strncpy(pPassword, pPasswordToken->z, pPasswordToken->n);
2✔
189
    (void)strdequote(pPassword);
2✔
190
    if (strtrim(pPassword) < 32) {
2✔
191
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY);
1✔
192
    }
193
  }
194
  return TSDB_CODE_SUCCESS == pCxt->errCode;
2✔
195
}
196

197
static int32_t parsePort(SAstCreateContext* pCxt, const char* p, int32_t* pPort) {
559✔
198
  *pPort = taosStr2Int32(p, NULL, 10);
559✔
199
  if (*pPort >= UINT16_MAX || *pPort <= 0) {
559!
200
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
×
201
  }
202
  return TSDB_CODE_SUCCESS;
559✔
203
}
204

205
static int32_t parseEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
579✔
206
  if (pEp->n >= (NULL == pPort ? (TSDB_FQDN_LEN + 1 + 5) : TSDB_FQDN_LEN)) {  // format 'fqdn:port' or 'fqdn'
579!
207
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
208
  }
209

210
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
579✔
211
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
579✔
212
  (void)strdequote(ep);
579✔
213
  (void)strtrim(ep);
579✔
214
  if (NULL == pPort) {
579✔
215
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
166✔
216
    return TSDB_CODE_SUCCESS;
166✔
217
  }
218
  char* pColon = strrchr(ep, ':');
413✔
219
  if (NULL == pColon) {
413✔
220
    *pPort = tsServerPort;
20✔
221
    tstrncpy(pFqdn, ep, TSDB_FQDN_LEN);
20✔
222
    return TSDB_CODE_SUCCESS;
20✔
223
  }
224
  strncpy(pFqdn, ep, pColon - ep);
393✔
225
  return parsePort(pCxt, pColon + 1, pPort);
393✔
226
}
227

228
static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, const SToken* pPortToken, char* pFqdn,
579✔
229
                                  int32_t* pPort) {
230
  if (NULL == pEp) {
579!
231
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
232
    return false;
×
233
  }
234

235
  if (NULL != pPortToken) {
579✔
236
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
166✔
237
  }
238

239
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
579!
240
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
579✔
241
  }
242

243
  return TSDB_CODE_SUCCESS == pCxt->errCode;
579✔
244
}
245

246
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
1,404,395✔
247
  if (NULL == pDbName) {
1,404,395✔
248
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
526,508!
249
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
23✔
250
    }
251
  } else {
252
    trimEscape(pCxt, pDbName);
877,887✔
253
    if (pDbName->n >= TSDB_DB_NAME_LEN || pDbName->n == 0) {
877,911!
254
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
×
255
    }
256
  }
257
  return TSDB_CODE_SUCCESS == pCxt->errCode;
1,404,444✔
258
}
259

260
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
8,523,140✔
261
  trimEscape(pCxt, pTableName);
8,523,140✔
262
  if (NULL != pTableName && pTableName->type != TK_NK_NIL &&
8,523,152✔
263
      (pTableName->n >= TSDB_TABLE_NAME_LEN || pTableName->n == 0)) {
2,916,441✔
264
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
7✔
265
    return false;
15✔
266
  }
267
  return true;
8,523,145✔
268
}
269

270
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
6,107,441✔
271
  trimEscape(pCxt, pColumnName);
6,107,441✔
272
  if (NULL != pColumnName && pColumnName->type != TK_NK_NIL &&
6,107,441!
273
      (pColumnName->n >= TSDB_COL_NAME_LEN || pColumnName->n == 0)) {
6,107,446✔
274
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
8✔
275
    return false;
13✔
276
  }
277
  return true;
6,107,433✔
278
}
279

280
static bool checkIndexName(SAstCreateContext* pCxt, SToken* pIndexName) {
29✔
281
  trimEscape(pCxt, pIndexName);
29✔
282
  if (NULL != pIndexName && pIndexName->n >= TSDB_INDEX_NAME_LEN) {
29!
283
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pIndexName->z);
×
284
    return false;
×
285
  }
286
  return true;
29✔
287
}
288

289
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
1,047✔
290
  trimEscape(pCxt, pTopicName);
1,047✔
291
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN || pTopicName->n == 0) {
1,047✔
292
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
3✔
293
    return false;
3✔
294
  }
295
  return true;
1,044✔
296
}
297

298
static bool checkCGroupName(SAstCreateContext* pCxt, SToken* pCGroup) {
14✔
299
  trimEscape(pCxt, pCGroup);
14✔
300
  if (pCGroup->n >= TSDB_CGROUP_LEN) {
14!
301
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pCGroup->z);
×
302
    return false;
×
303
  }
304
  return true;
14✔
305
}
306

307
static bool checkViewName(SAstCreateContext* pCxt, SToken* pViewName) {
953✔
308
  trimEscape(pCxt, pViewName);
953✔
309
  if (pViewName->n >= TSDB_VIEW_NAME_LEN || pViewName->n == 0) {
953✔
310
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pViewName->z);
3✔
311
    return false;
3✔
312
  }
313
  return true;
950✔
314
}
315

316
static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
1,963✔
317
  trimEscape(pCxt, pStreamName);
1,963✔
318
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN || pStreamName->n == 0) {
1,963!
319
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
1✔
320
    return false;
1✔
321
  }
322
  return true;
1,962✔
323
}
324

325
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
159✔
326
  if (NULL == pCommentToken) {
159!
327
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
×
328
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
159✔
329
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
13✔
330
  }
331
  return TSDB_CODE_SUCCESS == pCxt->errCode;
159✔
332
}
333

334
static bool checkTsmaName(SAstCreateContext* pCxt, SToken* pTsmaToken) {
325✔
335
  trimEscape(pCxt, pTsmaToken);
325✔
336
  if (NULL == pTsmaToken) {
325!
337
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
338
  } else if (pTsmaToken->n >= TSDB_TABLE_NAME_LEN - strlen(TSMA_RES_STB_POSTFIX)) {
325!
339
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
×
340
  } else if (pTsmaToken->n == 0) {
325!
341
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTsmaToken->z);
×
342
  }
343
  return pCxt->errCode == TSDB_CODE_SUCCESS;
325✔
344
}
345

346
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
6,306,887✔
347
  CHECK_PARSER_STATUS(pCxt);
6,306,887✔
348
  SRawExprNode* target = NULL;
6,306,825✔
349
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
6,306,825✔
350
  CHECK_MAKE_NODE(target);
6,306,828!
351
  target->p = pToken->z;
6,306,828✔
352
  target->n = pToken->n;
6,306,828✔
353
  target->pNode = pNode;
6,306,828✔
354
  return (SNode*)target;
6,306,828✔
355
_err:
62✔
356
  nodesDestroyNode(pNode);
62✔
357
  return NULL;
62✔
358
}
359

360
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
6,935,602✔
361
  CHECK_PARSER_STATUS(pCxt);
6,935,602!
362
  SRawExprNode* target = NULL;
6,935,602✔
363
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
6,935,602✔
364
  CHECK_MAKE_NODE(target);
6,935,615!
365
  target->p = pStart->z;
6,935,615✔
366
  target->n = (pEnd->z + pEnd->n) - pStart->z;
6,935,615✔
367
  target->pNode = pNode;
6,935,615✔
368
  return (SNode*)target;
6,935,615✔
369
_err:
×
370
  nodesDestroyNode(pNode);
×
371
  return NULL;
×
372
}
373

374
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
550,865✔
375
  CHECK_PARSER_STATUS(pCxt);
550,865!
376
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
550,865!
377
    return pNode;
×
378
  }
379
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
550,865✔
380
  return pNode;
550,865✔
381
_err:
×
382
  nodesDestroyNode(pNode);
×
383
  return NULL;
×
384
}
385

386
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
13,230,289✔
387
  CHECK_PARSER_STATUS(pCxt);
13,230,289!
388
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
13,230,289✔
389
  SNode*        pRealizedExpr = pRawExpr->pNode;
13,230,289✔
390
  if (nodesIsExprNode(pRealizedExpr)) {
13,230,289✔
391
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
12,821,504✔
392
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
12,821,504✔
393
      tstrncpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
5,282,109✔
394
      tstrncpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
5,282,109✔
395
    } else if (pRawExpr->isPseudoColumn) {
7,539,395✔
396
      // all pseudo column are translate to function with same name
397
      tstrncpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
449,271✔
398
      tstrncpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
449,271✔
399
    } else {
400
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
7,090,124✔
401

402
      // See TS-3398.
403
      // Len of pRawExpr->p could be larger than len of aliasName[TSDB_COL_NAME_LEN].
404
      // If aliasName is truncated, hash value of aliasName could be the same.
405
      uint64_t hashVal = MurmurHash3_64(pRawExpr->p, pRawExpr->n);
7,090,124✔
406
      snprintf(pExpr->aliasName, TSDB_COL_NAME_LEN, "%" PRIu64, hashVal);
7,090,160✔
407
      strncpy(pExpr->userAlias, pRawExpr->p, len);
7,090,160✔
408
      pExpr->userAlias[len] = 0;
7,090,160✔
409
    }
410
  }
411
  pRawExpr->pNode = NULL;
13,230,324✔
412
  nodesDestroyNode(pNode);
13,230,324✔
413
  return pRealizedExpr;
13,230,354✔
414
_err:
×
415
  nodesDestroyNode(pNode);
×
416
  return NULL;
×
417
}
418

419
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
4,209,058✔
420
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
4,209,058!
421
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
422
    return nil_token;
×
423
  }
424
  SRawExprNode* target = (SRawExprNode*)pNode;
4,209,061✔
425
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
4,209,061✔
426
  return t;
4,209,061✔
427
}
428

429
SNodeList* createColsFuncParamNodeList(SAstCreateContext* pCxt, SNode* pNode, SNodeList* pNodeList, SToken* pAlias) {
2,428✔
430
  CHECK_PARSER_STATUS(pCxt);
2,428!
431
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
2,428!
432
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
433
  }
434
  CHECK_PARSER_STATUS(pCxt);
2,428!
435
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,428✔
436
  SNode*        pFuncNode = pRawExpr->pNode;
2,428✔
437
  if (pFuncNode->type != QUERY_NODE_FUNCTION) {
2,428!
438
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
439
  }
440
  CHECK_PARSER_STATUS(pCxt);
2,428!
441
  SNodeList* list = NULL;
2,428✔
442
  pCxt->errCode = nodesMakeList(&list);
2,428✔
443
  CHECK_MAKE_NODE(list);
2,428!
444
  pCxt->errCode = nodesListAppend(list, pFuncNode);
2,428✔
445
  CHECK_PARSER_STATUS(pCxt);
2,428!
446
  pCxt->errCode = nodesListAppendList(list, pNodeList);
2,428✔
447
  CHECK_PARSER_STATUS(pCxt);
2,428!
448
  return list;
2,428✔
449

450
_err:
×
451
  nodesDestroyNode(pFuncNode);
×
452
  nodesDestroyList(pNodeList);
×
453
  return NULL;
×
454
}
455

456
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
4,131,149✔
457
  CHECK_PARSER_STATUS(pCxt);
4,131,149✔
458
  SNodeList* list = NULL;
4,131,129✔
459
  pCxt->errCode = nodesMakeList(&list);
4,131,129✔
460
  CHECK_MAKE_NODE(list);
4,131,167!
461
  pCxt->errCode = nodesListAppend(list, pNode);
4,131,167✔
462
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
4,131,172!
463
    nodesDestroyList(list);
×
464
    return NULL;
×
465
  }
466
  return list;
4,131,174✔
467
_err:
20✔
468
  nodesDestroyNode(pNode);
20✔
469
  return NULL;
20✔
470
}
471

472
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
4,171,520✔
473
  CHECK_PARSER_STATUS(pCxt);
4,171,520✔
474
  pCxt->errCode = nodesListAppend(pList, pNode);
4,171,513✔
475
  return pList;
4,171,539✔
476
_err:
7✔
477
  nodesDestroyNode(pNode);
7✔
478
  nodesDestroyList(pList);
7✔
479
  return NULL;
7✔
480
}
481

482
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
5,461,524✔
483
  CHECK_PARSER_STATUS(pCxt);
5,461,524!
484
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
5,461,524!
485
    return NULL;
1✔
486
  }
487
  SColumnNode* col = NULL;
5,461,523✔
488
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
5,461,523✔
489
  CHECK_MAKE_NODE(col);
5,461,516!
490
  if (NULL != pTableAlias) {
5,461,516✔
491
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
1,280,778✔
492
  }
493
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
5,461,516✔
494
  return (SNode*)col;
5,461,516✔
495
_err:
×
496
  return NULL;
×
497
}
498

499
static void copyValueTrimEscape(char* buf, int32_t bufLen, const SToken* pToken, bool trim) {
2,003,971✔
500
  int32_t len = TMIN(pToken->n, bufLen - 1);
2,003,971✔
501
  if (trim && (pToken->z[0] == TS_ESCAPE_CHAR)) {
2,003,971!
502
    int32_t i = 1, j = 0;
220✔
503
    for (; i < len - 1; ++i) {
1,240✔
504
      buf[j++] = pToken->z[i];
1,020✔
505
      if (pToken->z[i] == TS_ESCAPE_CHAR) {
1,020✔
506
        if (pToken->z[i + 1] == TS_ESCAPE_CHAR) ++i;
516!
507
      }
508
    }
509
    buf[j] = 0;
220✔
510
  } else {
511
    tstrncpy(buf, pToken->z, len + 1);
2,003,751✔
512
  }
513
}
2,003,971✔
514

515
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
2,003,964✔
516
  CHECK_PARSER_STATUS(pCxt);
2,003,964!
517
  SValueNode* val = NULL;
2,003,964✔
518
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
2,003,964✔
519
  CHECK_MAKE_NODE(val);
2,003,972!
520
  if (!(val->literal = taosMemoryMalloc(pLiteral->n + 1))) {
2,003,972!
521
    pCxt->errCode = terrno;
×
522
    nodesDestroyNode((SNode*)val);
×
523
    return NULL;
×
524
  }
525
  copyValueTrimEscape(val->literal, pLiteral->n + 1, pLiteral,
2,003,972✔
526
                      pCxt->pQueryCxt->hasDupQuoteChar && (TK_NK_ID == pLiteral->type));
2,003,972✔
527
  if (TK_NK_ID != pLiteral->type && TK_TIMEZONE != pLiteral->type &&
2,003,967!
528
      (IS_VAR_DATA_TYPE(dataType) || TSDB_DATA_TYPE_TIMESTAMP == dataType)) {
1,498,256!
529
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
495,983✔
530
  }
531
  val->node.resType.type = dataType;
2,003,972✔
532
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
2,003,972!
533
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
2,003,972✔
534
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
528✔
535
  }
536
  val->translate = false;
2,003,972✔
537
  val->tz = pCxt->pQueryCxt->timezone;
2,003,972✔
538
  val->charsetCxt = pCxt->pQueryCxt->charsetCxt;
2,003,972✔
539
  return (SNode*)val;
2,003,972✔
540
_err:
×
541
  return NULL;
×
542
}
543

544
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
377,457✔
545
  CHECK_PARSER_STATUS(pCxt);
377,457!
546
  SValueNode* val = NULL;
377,457✔
547
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
377,457✔
548
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
377,499!
549
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
550
    goto _exit;
×
551
  }
552
  if (pLiteral) {
377,501✔
553
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
374,711!
554
    if (!val->literal) {
374,700!
555
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
556
      goto _exit;
×
557
    }
558
  } else if (pNode) {
2,790!
559
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,790✔
560
    if (!nodesIsExprNode(pRawExpr->pNode)) {
2,790!
561
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
562
      goto _exit;
×
563
    }
564
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
2,790!
565
    if (!val->literal) {
2,768!
566
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
567
      goto _exit;
×
568
    }
569
  } else {
570
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
571
    goto _exit;
×
572
  }
573
  if (!val->literal) {
377,468!
574
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
575
    goto _exit;
×
576
  }
577

578
  val->node.resType.type = dataType;
377,468✔
579
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
377,468!
580
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
377,468!
581
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
582
  }
583
_exit:
377,468✔
584
  nodesDestroyNode(pNode);
377,468✔
585
  if (pCxt->errCode != 0) {
377,488✔
586
    nodesDestroyNode((SNode*)val);
1✔
587
    return NULL;
×
588
  }
589
  return (SNode*)val;
377,487✔
590
_err:
×
591
  nodesDestroyNode(pNode);
×
592
  return NULL;
×
593
}
594

595
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
1,295✔
596
                             SNode* pRight) {
597
  SValueNode* val = NULL;
1,295✔
598
  CHECK_PARSER_STATUS(pCxt);
1,295!
599

600
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
1,295✔
601
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
1,295!
602
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
603
    goto _exit;
×
604
  }
605
  if (pLiteral) {
1,295!
606
    if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) {
1,295!
607
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
608
      goto _exit;
×
609
    }
610
  } else {
611
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
612
    goto _exit;
×
613
  }
614

615
  val->node.resType.type = dataType;
1,295✔
616
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
1,295!
617
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
1,295!
618
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
619
  }
620
_exit:
1,295✔
621
  nodesDestroyNode(pLeft);
1,295✔
622
  nodesDestroyNode(pRight);
1,295✔
623
  CHECK_PARSER_STATUS(pCxt);
1,295!
624
  return (SNode*)val;
1,295✔
625
_err:
×
626
  nodesDestroyNode((SNode*)val);
×
627
  nodesDestroyNode(pLeft);
×
628
  nodesDestroyNode(pRight);
×
629
  return NULL;
×
630
}
631

632
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
32,224✔
633
  if (!pHintList) return false;
32,224✔
634
  SNode* pNode;
635
  FOREACH(pNode, pHintList) {
12!
636
    SHintNode* pHint = (SHintNode*)pNode;
12✔
637
    if (pHint->option == hint) {
12!
638
      return true;
12✔
639
    }
640
  }
641
  return false;
×
642
}
643

644
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
32,268✔
645
                       int32_t paramNum) {
646
  void* value = NULL;
32,268✔
647
  switch (opt) {
32,268!
648
    case HINT_SKIP_TSMA:
44✔
649
    case HINT_BATCH_SCAN:
650
    case HINT_NO_BATCH_SCAN: {
651
      if (paramNum > 0) {
44✔
652
        return true;
8✔
653
      }
654
      break;
36✔
655
    }
656
    case HINT_SORT_FOR_GROUP:
488✔
657
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
488!
658
      break;
484✔
659
    case HINT_PARTITION_FIRST:
16✔
660
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
16!
661
      break;
8✔
662
    case HINT_PARA_TABLES_SORT:
31,720✔
663
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
31,720!
664
      break;
31,720✔
665
    case HINT_SMALLDATA_TS_SORT:
×
666
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
667
      break;
×
668
    case HINT_HASH_JOIN:
×
669
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
×
670
      break;
×
671
    default:
×
672
      return true;
×
673
  }
674

675
  SHintNode* hint = NULL;
32,248✔
676
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
32,248✔
677
  if (!hint) {
32,248!
678
    return true;
×
679
  }
680
  hint->option = opt;
32,248✔
681
  hint->value = value;
32,248✔
682

683
  if (NULL == *ppHintList) {
32,248✔
684
    pCxt->errCode = nodesMakeList(ppHintList);
32,240✔
685
    if (!*ppHintList) {
32,240!
686
      nodesDestroyNode((SNode*)hint);
×
687
      return true;
×
688
    }
689
  }
690

691
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
32,248✔
692
  if (pCxt->errCode) {
32,248!
693
    return true;
×
694
  }
695

696
  return false;
32,248✔
697
}
698

699
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
1,410,440✔
700
  CHECK_PARSER_STATUS(pCxt);
1,410,440!
701
  if (NULL == pLiteral || pLiteral->n <= 5) {
1,410,440✔
702
    return NULL;
1,378,188✔
703
  }
704
  SNodeList* pHintList = NULL;
32,252✔
705
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
32,252!
706
  if (!hint) return NULL;
32,252!
707
  int32_t     i = 0;
32,252✔
708
  bool        quit = false;
32,252✔
709
  bool        inParamList = false;
32,252✔
710
  bool        lastComma = false;
32,252✔
711
  EHintOption opt = 0;
32,252✔
712
  int32_t     paramNum = 0;
32,252✔
713
  SToken      paramList[10];
714
  while (!quit) {
193,560✔
715
    SToken t0 = {0};
193,536✔
716
    if (hint[i] == 0) {
193,536✔
717
      break;
32,228✔
718
    }
719
    t0.n = tGetToken(&hint[i], &t0.type, NULL);
161,308✔
720
    t0.z = hint + i;
161,308✔
721
    i += t0.n;
161,308✔
722

723
    switch (t0.type) {
161,308!
724
      case TK_BATCH_SCAN:
24✔
725
        lastComma = false;
24✔
726
        if (0 != opt || inParamList) {
24!
727
          quit = true;
×
728
          break;
×
729
        }
730
        opt = HINT_BATCH_SCAN;
24✔
731
        break;
24✔
732
      case TK_NO_BATCH_SCAN:
16✔
733
        lastComma = false;
16✔
734
        if (0 != opt || inParamList) {
16!
735
          quit = true;
×
736
          break;
×
737
        }
738
        opt = HINT_NO_BATCH_SCAN;
16✔
739
        break;
16✔
740
      case TK_SORT_FOR_GROUP:
488✔
741
        lastComma = false;
488✔
742
        if (0 != opt || inParamList) {
488!
743
          quit = true;
×
744
          break;
×
745
        }
746
        opt = HINT_SORT_FOR_GROUP;
488✔
747
        break;
488✔
748
      case TK_PARTITION_FIRST:
16✔
749
        lastComma = false;
16✔
750
        if (0 != opt || inParamList) {
16!
751
          quit = true;
×
752
          break;
×
753
        }
754
        opt = HINT_PARTITION_FIRST;
16✔
755
        break;
16✔
756
      case TK_PARA_TABLES_SORT:
31,720✔
757
        lastComma = false;
31,720✔
758
        if (0 != opt || inParamList) {
31,720!
759
          quit = true;
×
760
          break;
×
761
        }
762
        opt = HINT_PARA_TABLES_SORT;
31,720✔
763
        break;
31,720✔
764
      case TK_SMALLDATA_TS_SORT:
×
765
        lastComma = false;
×
766
        if (0 != opt || inParamList) {
×
767
          quit = true;
×
768
          break;
×
769
        }
770
        opt = HINT_SMALLDATA_TS_SORT;
×
771
        break;
×
772
      case TK_HASH_JOIN:
×
773
        lastComma = false;
×
774
        if (0 != opt || inParamList) {
×
775
          quit = true;
×
776
          break;
×
777
        }
778
        opt = HINT_HASH_JOIN;
×
779
        break;
×
780
      case TK_SKIP_TSMA:
4✔
781
        lastComma = false;
4✔
782
        if (0 != opt || inParamList) {
4!
783
          quit = true;
×
784
          break;
×
785
        }
786
        opt = HINT_SKIP_TSMA;
4✔
787
        break;
4✔
788
      case TK_NK_LP:
32,268✔
789
        lastComma = false;
32,268✔
790
        if (0 == opt || inParamList) {
32,268!
791
          quit = true;
×
792
        }
793
        inParamList = true;
32,268✔
794
        break;
32,268✔
795
      case TK_NK_RP:
32,268✔
796
        lastComma = false;
32,268✔
797
        if (0 == opt || !inParamList) {
32,268!
798
          quit = true;
×
799
        } else {
800
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
32,268✔
801
          inParamList = false;
32,268✔
802
          paramNum = 0;
32,268✔
803
          opt = 0;
32,268✔
804
        }
805
        break;
32,268✔
806
      case TK_NK_ID:
12✔
807
        lastComma = false;
12✔
808
        if (0 == opt || !inParamList) {
12!
809
          quit = true;
4✔
810
        } else {
811
          paramList[paramNum++] = t0;
8✔
812
        }
813
        break;
12✔
814
      case TK_NK_COMMA:
8✔
815
        if (lastComma) {
8!
816
          quit = true;
×
817
        }
818
        lastComma = true;
8✔
819
        break;
8✔
820
      case TK_NK_SPACE:
64,484✔
821
        break;
64,484✔
822
      default:
×
823
        lastComma = false;
×
824
        quit = true;
×
825
        break;
×
826
    }
827
  }
828

829
  taosMemoryFree(hint);
32,252!
830
  return pHintList;
32,252✔
831
_err:
×
832
  return NULL;
×
833
}
834

835
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
2,409✔
836
  trimEscape(pCxt, pLiteral);
2,409✔
837
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
2,409✔
838
}
839

840
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
163,397✔
841
  CHECK_PARSER_STATUS(pCxt);
163,397!
842
  SValueNode* val = NULL;
163,397✔
843
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
163,397✔
844
  CHECK_MAKE_NODE(val);
163,399!
845
  if (pLiteral->type == TK_NK_STRING) {
163,399✔
846
    // like '100s' or "100d"
847
    // check format: ^[0-9]+[smwbauhdny]$'
848
    if (pLiteral->n < 4) {
180✔
849
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
8✔
850
      return NULL;
8✔
851
    }
852
    char unit = pLiteral->z[pLiteral->n - 2];
172✔
853
    switch (unit) {
172✔
854
      case 'a':
148✔
855
      case 'b':
856
      case 'd':
857
      case 'h':
858
      case 'm':
859
      case 's':
860
      case 'u':
861
      case 'w':
862
      case 'y':
863
      case 'n':
864
        break;
148✔
865
      default:
24✔
866
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
24✔
867
        return NULL;
24✔
868
    }
869
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
544✔
870
      if (!isdigit(pLiteral->z[i])) {
422✔
871
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
26✔
872
        return NULL;
26✔
873
      }
874
    }
875
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
122!
876
  } else {
877
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
163,219!
878
  }
879
  if (!val->literal) {
163,340!
880
    nodesDestroyNode((SNode*)val);
×
881
    pCxt->errCode = terrno;
×
882
    return NULL;
×
883
  }
884
  val->flag |= VALUE_FLAG_IS_DURATION;
163,340✔
885
  val->translate = false;
163,340✔
886
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
163,340✔
887
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
163,340✔
888
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
163,340✔
889
  return (SNode*)val;
163,340✔
890
_err:
×
891
  return NULL;
×
892
}
893

894
SNode* createTimeOffsetValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
3,150✔
895
  CHECK_PARSER_STATUS(pCxt);
3,150!
896
  SValueNode* val = NULL;
3,150✔
897
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
3,150✔
898
  CHECK_MAKE_NODE(val);
3,150!
899
  if (pLiteral->type == TK_NK_STRING) {
3,150!
900
    // like '100s' or "100d"
901
    // check format: ^[0-9]+[smwbauhdny]$'
902
    if (pLiteral->n < 4) {
×
903
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
904
      return NULL;
×
905
    }
906
    char unit = pLiteral->z[pLiteral->n - 2];
×
907
    switch (unit) {
×
908
      case 'a':
×
909
      case 'b':
910
      case 'd':
911
      case 'h':
912
      case 'm':
913
      case 's':
914
      case 'u':
915
      case 'w':
916
        break;
×
917
      default:
×
918
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
919
        return NULL;
×
920
    }
921
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
×
922
      if (!isdigit(pLiteral->z[i])) {
×
923
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
924
        return NULL;
×
925
      }
926
    }
927
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
×
928
  } else {
929
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
3,150!
930
  }
931
  if (!val->literal) {
3,150!
932
    nodesDestroyNode((SNode*)val);
×
933
    pCxt->errCode = terrno;
×
934
    return NULL;
×
935
  }
936
  val->flag |= VALUE_FLAG_IS_TIME_OFFSET;
3,150✔
937
  val->translate = false;
3,150✔
938
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
3,150✔
939
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
3,150✔
940
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
3,150✔
941
  return (SNode*)val;
3,150✔
942
_err:
×
943
  return NULL;
×
944
}
945

946
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
2,311✔
947
  CHECK_PARSER_STATUS(pCxt);
2,311!
948
  if (NULL == pCxt->pQueryCxt->db) {
2,311✔
949
    return NULL;
6✔
950
  }
951

952
  SValueNode* val = NULL;
2,305✔
953
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
2,305✔
954
  CHECK_MAKE_NODE(val);
2,305!
955
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
2,305!
956
  if (!val->literal) {
2,305!
957
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
958
    nodesDestroyNode((SNode*)val);
×
959
    return NULL;
×
960
  }
961
  val->translate = false;
2,305✔
962
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
2,305✔
963
  val->node.resType.bytes = strlen(val->literal);
2,305✔
964
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
2,305✔
965
  return (SNode*)val;
2,305✔
966
_err:
×
967
  return NULL;
×
968
}
969

970
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
46✔
971
  CHECK_PARSER_STATUS(pCxt);
46!
972
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
46✔
973
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
4✔
974
    return NULL;
4✔
975
  }
976
  SValueNode* val = NULL;
42✔
977
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
42✔
978
  CHECK_MAKE_NODE(val);
42!
979
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
42!
980
  if (!val->literal) {
42!
981
    pCxt->errCode = terrno;
×
982
    nodesDestroyNode((SNode*)val);
×
983
    return NULL;
×
984
  }
985
  val->placeholderNo = ++pCxt->placeholderNo;
42✔
986
  if (NULL == pCxt->pPlaceholderValues) {
42✔
987
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
30✔
988
    if (NULL == pCxt->pPlaceholderValues) {
30!
989
      nodesDestroyNode((SNode*)val);
×
990
      return NULL;
×
991
    }
992
  }
993
  if (NULL == taosArrayPush(pCxt->pPlaceholderValues, &val)) {
84!
994
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
995
    nodesDestroyNode((SNode*)val);
×
996
    taosArrayDestroy(pCxt->pPlaceholderValues);
×
997
    return NULL;
×
998
  }
999
  return (SNode*)val;
42✔
1000
_err:
×
1001
  return NULL;
×
1002
}
1003

1004
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
1,180,398✔
1005
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
1,180,398✔
1006
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
227,371✔
1007
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
227,071✔
1008
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
227,071✔
1009
    nodesDestroyNode(pParam);
227,071✔
1010
    return code;
227,071✔
1011
  } else {
1012
    return nodesListAppend(pCond->pParameterList, pParam);
953,327✔
1013
  }
1014
}
1015

1016
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
590,925✔
1017
  CHECK_PARSER_STATUS(pCxt);
590,925!
1018
  SLogicConditionNode* cond = NULL;
590,925✔
1019
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
590,925✔
1020
  CHECK_MAKE_NODE(cond);
590,936!
1021
  cond->condType = type;
590,936✔
1022
  cond->pParameterList = NULL;
590,936✔
1023
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
590,936✔
1024
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
590,935!
1025
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
590,935✔
1026
  }
1027
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
590,936!
1028
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
589,481✔
1029
  }
1030
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
590,933!
1031
    nodesDestroyNode((SNode*)cond);
×
1032
    return NULL;
×
1033
  }
1034
  return (SNode*)cond;
590,933✔
1035
_err:
×
1036
  nodesDestroyNode(pParam1);
×
1037
  nodesDestroyNode(pParam2);
×
1038
  return NULL;
×
1039
}
1040

1041
static uint8_t getMinusDataType(uint8_t orgType) {
250,146✔
1042
  switch (orgType) {
250,146✔
1043
    case TSDB_DATA_TYPE_UTINYINT:
158,552✔
1044
    case TSDB_DATA_TYPE_USMALLINT:
1045
    case TSDB_DATA_TYPE_UINT:
1046
    case TSDB_DATA_TYPE_UBIGINT:
1047
      return TSDB_DATA_TYPE_BIGINT;
158,552✔
1048
    default:
91,594✔
1049
      break;
91,594✔
1050
  }
1051
  return orgType;
91,594✔
1052
}
1053

1054
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
2,077,212✔
1055
  CHECK_PARSER_STATUS(pCxt);
2,077,212!
1056
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
2,077,212✔
1057
    SValueNode* pVal = (SValueNode*)pLeft;
250,146✔
1058
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
250,146!
1059
    if (!pNewLiteral) {
250,146!
1060
      pCxt->errCode = terrno;
×
1061
      goto _err;
×
1062
    }
1063
    if ('+' == pVal->literal[0]) {
250,146!
1064
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal + 1);
×
1065
    } else if ('-' == pVal->literal[0]) {
250,146✔
1066
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "%s", pVal->literal + 1);
174✔
1067
    } else {
1068
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal);
249,972✔
1069
    }
1070
    taosMemoryFree(pVal->literal);
250,146!
1071
    pVal->literal = pNewLiteral;
250,146✔
1072
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
250,146✔
1073
    return pLeft;
250,146✔
1074
  }
1075
  SOperatorNode* op = NULL;
1,827,066✔
1076
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
1,827,066✔
1077
  CHECK_MAKE_NODE(op);
1,827,065!
1078
  op->opType = type;
1,827,065✔
1079
  op->pLeft = pLeft;
1,827,065✔
1080
  op->pRight = pRight;
1,827,065✔
1081
  op->tz = pCxt->pQueryCxt->timezone;
1,827,065✔
1082
  op->charsetCxt = pCxt->pQueryCxt->charsetCxt;
1,827,065✔
1083
  return (SNode*)op;
1,827,065✔
1084
_err:
×
1085
  nodesDestroyNode(pLeft);
×
1086
  nodesDestroyNode(pRight);
×
1087
  return NULL;
4✔
1088
}
1089

1090
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
131,696✔
1091
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
131,696✔
1092
  CHECK_PARSER_STATUS(pCxt);
131,696!
1093
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
131,696✔
1094
  CHECK_PARSER_STATUS(pCxt);
131,705!
1095
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
131,705✔
1096
  CHECK_PARSER_STATUS(pCxt);
131,704!
1097
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
131,704✔
1098
  CHECK_PARSER_STATUS(pCxt);
131,704!
1099
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
131,704✔
1100
  CHECK_PARSER_STATUS(pCxt);
131,708!
1101
  return pRet;
131,708✔
1102
_err:
×
1103
  nodesDestroyNode(pNew);
×
1104
  nodesDestroyNode(pGE);
×
1105
  nodesDestroyNode(pLE);
×
1106
  nodesDestroyNode(pExpr);
×
1107
  nodesDestroyNode(pLeft);
×
1108
  nodesDestroyNode(pRight);
×
1109
  return NULL;
×
1110
}
1111

1112
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
34,692✔
1113
  SNode *pNew = NULL, *pLT = NULL, *pGT = NULL;
34,692✔
1114
  CHECK_PARSER_STATUS(pCxt);
34,692!
1115
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
34,692✔
1116
  CHECK_PARSER_STATUS(pCxt);
34,692!
1117
  pLT = createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft);
34,692✔
1118
  CHECK_PARSER_STATUS(pCxt);
34,692!
1119
  pGT = createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pNew, pRight);
34,692✔
1120
  CHECK_PARSER_STATUS(pCxt);
34,692!
1121
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, pLT, pGT);
34,692✔
1122
  CHECK_PARSER_STATUS(pCxt);
34,692!
1123
  return pRet;
34,692✔
1124
_err:
×
1125
  nodesDestroyNode(pNew);
×
1126
  nodesDestroyNode(pGT);
×
1127
  nodesDestroyNode(pLT);
×
1128
  nodesDestroyNode(pExpr);
×
1129
  nodesDestroyNode(pLeft);
×
1130
  nodesDestroyNode(pRight);
×
1131
  return NULL;
×
1132
}
1133

1134
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
195,616✔
1135
  CHECK_PARSER_STATUS(pCxt);
195,616!
1136
  SColumnNode* pCol = NULL;
195,616✔
1137
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
195,616✔
1138
  CHECK_MAKE_NODE(pCol);
195,615!
1139
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
195,615✔
1140
  if (NULL == pFuncName) {
195,615✔
1141
    tstrncpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME, TSDB_COL_NAME_LEN);
94,017✔
1142
  } else {
1143
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
101,598✔
1144
  }
1145
  pCol->isPrimTs = true;
195,615✔
1146
  return (SNode*)pCol;
195,615✔
1147
_err:
×
1148
  return NULL;
×
1149
}
1150

1151
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
2,541,564✔
1152
  CHECK_PARSER_STATUS(pCxt);
2,541,564!
1153
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
2,541,564✔
1154
    return createPrimaryKeyCol(pCxt, pFuncName);
101,597✔
1155
  }
1156
  SFunctionNode* func = NULL;
2,439,967✔
1157
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
2,439,967✔
1158
  CHECK_MAKE_NODE(func);
2,439,955!
1159
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
2,439,955✔
1160
  func->pParameterList = pParameterList;
2,439,955✔
1161
  func->tz = pCxt->pQueryCxt->timezone;
2,439,955✔
1162
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
2,439,955✔
1163
  return (SNode*)func;
2,439,955✔
1164
_err:
×
1165
  nodesDestroyList(pParameterList);
×
1166
  return NULL;
×
1167
}
1168

1169
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
876,807✔
1170
  SFunctionNode* func = NULL;
876,807✔
1171
  CHECK_PARSER_STATUS(pCxt);
876,807!
1172
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
876,807✔
1173
  CHECK_MAKE_NODE(func);
876,807!
1174
  tstrncpy(func->functionName, "cast", TSDB_FUNC_NAME_LEN);
876,807✔
1175
  func->node.resType = dt;
876,807✔
1176
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
876,807!
1177
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
684,045✔
1178
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
192,762✔
1179
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
30,027✔
1180
  }
1181
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
876,807✔
1182
  CHECK_PARSER_STATUS(pCxt);
876,807!
1183
  func->tz = pCxt->pQueryCxt->timezone;
876,807✔
1184
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
876,807✔
1185

1186
  return (SNode*)func;
876,807✔
1187
_err:
×
1188
  nodesDestroyNode((SNode*)func);
×
1189
  nodesDestroyNode(pExpr);
×
1190
  return NULL;
×
1191
}
1192

1193
SNode* createPositionFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
232✔
1194
  SFunctionNode* func = NULL;
232✔
1195
  CHECK_PARSER_STATUS(pCxt);
232!
1196
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
232✔
1197
  CHECK_MAKE_NODE(func);
232!
1198
  tstrncpy(func->functionName, "position", TSDB_FUNC_NAME_LEN);
232✔
1199
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
232✔
1200
  CHECK_PARSER_STATUS(pCxt);
232!
1201
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
232✔
1202
  CHECK_PARSER_STATUS(pCxt);
232!
1203
  return (SNode*)func;
232✔
1204
_err:
×
1205
  nodesDestroyNode((SNode*)func);
×
1206
  nodesDestroyNode(pExpr);
×
1207
  nodesDestroyNode(pExpr2);
×
1208
  return NULL;
×
1209
}
1210

1211
SNode* createTrimFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, ETrimType type) {
36✔
1212
  SFunctionNode* func = NULL;
36✔
1213
  CHECK_PARSER_STATUS(pCxt);
36!
1214
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
36✔
1215
  CHECK_MAKE_NODE(func);
36!
1216
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
36✔
1217
  func->trimType = type;
36✔
1218
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
36✔
1219
  CHECK_PARSER_STATUS(pCxt);
36!
1220
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
36✔
1221
  return (SNode*)func;
36✔
1222
_err:
×
1223
  nodesDestroyNode((SNode*)func);
×
1224
  nodesDestroyNode(pExpr);
×
1225
  return NULL;
×
1226
}
1227

1228
SNode* createTrimFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, ETrimType type) {
126✔
1229
  SFunctionNode* func = NULL;
126✔
1230
  CHECK_PARSER_STATUS(pCxt);
126!
1231
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
126✔
1232
  CHECK_MAKE_NODE(func);
126!
1233
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
126✔
1234
  func->trimType = type;
126✔
1235
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
126✔
1236
  CHECK_PARSER_STATUS(pCxt);
126!
1237
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
126✔
1238
  CHECK_PARSER_STATUS(pCxt);
126!
1239
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
126✔
1240
  return (SNode*)func;
126✔
1241
_err:
×
1242
  nodesDestroyNode((SNode*)func);
×
1243
  nodesDestroyNode(pExpr);
×
1244
  nodesDestroyNode(pExpr2);
×
1245
  return NULL;
×
1246
}
1247

1248
SNode* createSubstrFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
20✔
1249
  SFunctionNode* func = NULL;
20✔
1250
  CHECK_PARSER_STATUS(pCxt);
20!
1251
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
20✔
1252
  CHECK_MAKE_NODE(func);
20!
1253
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
20✔
1254
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
20✔
1255
  CHECK_PARSER_STATUS(pCxt);
20!
1256
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
20✔
1257
  CHECK_PARSER_STATUS(pCxt);
20!
1258
  return (SNode*)func;
20✔
1259
_err:
×
1260
  nodesDestroyNode((SNode*)func);
×
1261
  nodesDestroyNode(pExpr);
×
1262
  nodesDestroyNode(pExpr2);
×
1263
  return NULL;
×
1264
}
1265

1266
SNode* createSubstrFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, SNode* pExpr3) {
44✔
1267
  SFunctionNode* func = NULL;
44✔
1268
  CHECK_PARSER_STATUS(pCxt);
44!
1269
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
44✔
1270
  CHECK_MAKE_NODE(func);
44!
1271
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
44✔
1272
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
44✔
1273
  CHECK_PARSER_STATUS(pCxt);
44!
1274
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
44✔
1275
  CHECK_PARSER_STATUS(pCxt);
44!
1276
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr3);
44✔
1277
  CHECK_PARSER_STATUS(pCxt);
44!
1278
  return (SNode*)func;
44✔
1279
_err:
×
1280
  nodesDestroyNode((SNode*)func);
×
1281
  nodesDestroyNode(pExpr);
×
1282
  nodesDestroyNode(pExpr2);
×
1283
  nodesDestroyNode(pExpr3);
×
1284
  return NULL;
×
1285
}
1286

1287
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
51,718✔
1288
  SNodeListNode* list = NULL;
51,718✔
1289
  CHECK_PARSER_STATUS(pCxt);
51,718!
1290
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
51,718✔
1291
  CHECK_MAKE_NODE(list);
51,720!
1292
  list->pNodeList = pList;
51,720✔
1293
  return (SNode*)list;
51,720✔
1294
_err:
×
1295
  nodesDestroyList(pList);
×
1296
  return NULL;
×
1297
}
1298

1299
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
79✔
1300
  SNodeListNode* list = NULL;
79✔
1301
  CHECK_PARSER_STATUS(pCxt);
79!
1302
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
79✔
1303
  CHECK_MAKE_NODE(list);
79!
1304
  pCxt->errCode = nodesListMakeStrictAppend(&list->pNodeList, p1);
79✔
1305
  CHECK_PARSER_STATUS(pCxt);
79!
1306
  pCxt->errCode = nodesListStrictAppend(list->pNodeList, p2);
79✔
1307
  CHECK_PARSER_STATUS(pCxt);
79!
1308
  return (SNode*)list;
79✔
1309
_err:
×
1310
  nodesDestroyNode((SNode*)list);
×
1311
  nodesDestroyNode(p1);
×
1312
  nodesDestroyNode(p2);
×
1313
  return NULL;
×
1314
}
1315

1316
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
1,374,119✔
1317
  CHECK_PARSER_STATUS(pCxt);
1,374,119!
1318
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
1,374,119✔
1319
  CHECK_NAME(checkTableName(pCxt, pTableName));
1,374,143✔
1320
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
1,374,103!
1321
  SRealTableNode* realTable = NULL;
1,374,117✔
1322
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
1,374,117✔
1323
  CHECK_MAKE_NODE(realTable);
1,374,100!
1324
  if (NULL != pDbName) {
1,374,100✔
1325
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
848,075✔
1326
  } else {
1327
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
526,025✔
1328
  }
1329
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
1,374,100✔
1330
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
247,192✔
1331
  } else {
1332
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
1,126,908✔
1333
  }
1334
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
1,374,100✔
1335
  return (SNode*)realTable;
1,374,100✔
1336
_err:
38✔
1337
  return NULL;
38✔
1338
}
1339

1340
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
309,218✔
1341
  CHECK_PARSER_STATUS(pCxt);
309,218!
1342
  if (!checkTableName(pCxt, pTableAlias)) {
309,218!
1343
    return NULL;
×
1344
  }
1345
  STempTableNode* tempTable = NULL;
309,218✔
1346
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
309,218✔
1347
  CHECK_MAKE_NODE(tempTable);
309,219!
1348
  tempTable->pSubquery = pSubquery;
309,219✔
1349
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
309,219!
1350
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
10,526✔
1351
  } else {
1352
    taosRandStr(tempTable->table.tableAlias, 8);
298,693✔
1353
  }
1354
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
309,219✔
1355
    tstrncpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
273,449✔
1356
    ((SSelectStmt*)pSubquery)->isSubquery = true;
273,449✔
1357
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
35,770!
1358
    tstrncpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
35,770✔
1359
  }
1360
  return (SNode*)tempTable;
309,219✔
1361
_err:
×
1362
  nodesDestroyNode(pSubquery);
×
1363
  return NULL;
×
1364
}
1365

1366
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, EJoinSubType stype, SNode* pLeft, SNode* pRight,
140,128✔
1367
                           SNode* pJoinCond) {
1368
  CHECK_PARSER_STATUS(pCxt);
140,128!
1369
  SJoinTableNode* joinTable = NULL;
140,128✔
1370
  pCxt->errCode = nodesMakeNode(QUERY_NODE_JOIN_TABLE, (SNode**)&joinTable);
140,128✔
1371
  CHECK_MAKE_NODE(joinTable);
140,128!
1372
  joinTable->joinType = type;
140,128✔
1373
  joinTable->subType = stype;
140,128✔
1374
  joinTable->pLeft = pLeft;
140,128✔
1375
  joinTable->pRight = pRight;
140,128✔
1376
  joinTable->pOnCond = pJoinCond;
140,128✔
1377
  return (SNode*)joinTable;
140,128✔
1378
_err:
×
1379
  nodesDestroyNode(pLeft);
×
1380
  nodesDestroyNode(pRight);
×
1381
  nodesDestroyNode(pJoinCond);
×
1382
  return NULL;
×
1383
}
1384

1385
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
955✔
1386
  CHECK_PARSER_STATUS(pCxt);
955!
1387
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
955✔
1388
  CHECK_NAME(checkViewName(pCxt, pViewName));
953✔
1389
  SViewNode* pView = NULL;
950✔
1390
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
950✔
1391
  CHECK_MAKE_NODE(pView);
950!
1392
  if (NULL != pDbName) {
950✔
1393
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
506✔
1394
  } else {
1395
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
444✔
1396
  }
1397
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
950✔
1398
  return (SNode*)pView;
950✔
1399
_err:
5✔
1400
  return NULL;
5✔
1401
}
1402

1403
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) {
161,664✔
1404
  CHECK_PARSER_STATUS(pCxt);
161,664!
1405
  SLimitNode* limitNode = NULL;
161,664✔
1406
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
161,664✔
1407
  CHECK_MAKE_NODE(limitNode);
161,664!
1408
  limitNode->limit = (SValueNode*)pLimit;
161,664✔
1409
  if (NULL != pOffset) {
161,664✔
1410
    limitNode->offset = (SValueNode*)pOffset;
64,310✔
1411
  }
1412
  return (SNode*)limitNode;
161,664✔
1413
_err:
×
1414
  return NULL;
×
1415
}
1416

1417
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
432,156✔
1418
  CHECK_PARSER_STATUS(pCxt);
432,156!
1419
  SOrderByExprNode* orderByExpr = NULL;
432,156✔
1420
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
432,156✔
1421
  CHECK_MAKE_NODE(orderByExpr);
432,159!
1422
  orderByExpr->pExpr = pExpr;
432,159✔
1423
  orderByExpr->order = order;
432,159✔
1424
  if (NULL_ORDER_DEFAULT == nullOrder) {
432,159✔
1425
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
431,426✔
1426
  }
1427
  orderByExpr->nullOrder = nullOrder;
432,159✔
1428
  return (SNode*)orderByExpr;
432,159✔
1429
_err:
×
1430
  nodesDestroyNode(pExpr);
×
1431
  return NULL;
×
1432
}
1433

1434
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
7,137✔
1435
  CHECK_PARSER_STATUS(pCxt);
7,137!
1436
  SSessionWindowNode* session = NULL;
7,137✔
1437
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
7,137✔
1438
  CHECK_MAKE_NODE(session);
7,137!
1439
  session->pCol = (SColumnNode*)pCol;
7,137✔
1440
  session->pGap = (SValueNode*)pGap;
7,137✔
1441
  return (SNode*)session;
7,137✔
1442
_err:
×
1443
  nodesDestroyNode(pCol);
×
1444
  nodesDestroyNode(pGap);
×
1445
  return NULL;
×
1446
}
1447

1448
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pTrueForLimit) {
6,659✔
1449
  SStateWindowNode* state = NULL;
6,659✔
1450
  CHECK_PARSER_STATUS(pCxt);
6,659!
1451
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
6,659✔
1452
  CHECK_MAKE_NODE(state);
6,659!
1453
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
6,659✔
1454
  CHECK_MAKE_NODE(state->pCol);
6,659!
1455
  state->pExpr = pExpr;
6,659✔
1456
  state->pTrueForLimit = pTrueForLimit;
6,659✔
1457
  return (SNode*)state;
6,659✔
1458
_err:
×
1459
  nodesDestroyNode((SNode*)state);
×
1460
  nodesDestroyNode(pExpr);
×
1461
  nodesDestroyNode(pTrueForLimit);
×
1462
  return NULL;
×
1463
}
1464

1465
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond, SNode* pTrueForLimit) {
1,146✔
1466
  SEventWindowNode* pEvent = NULL;
1,146✔
1467
  CHECK_PARSER_STATUS(pCxt);
1,146!
1468
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
1,146✔
1469
  CHECK_MAKE_NODE(pEvent);
1,146!
1470
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
1,146✔
1471
  CHECK_MAKE_NODE(pEvent->pCol);
1,146!
1472
  pEvent->pStartCond = pStartCond;
1,146✔
1473
  pEvent->pEndCond = pEndCond;
1,146✔
1474
  pEvent->pTrueForLimit = pTrueForLimit;
1,146✔
1475
  return (SNode*)pEvent;
1,146✔
1476
_err:
×
1477
  nodesDestroyNode((SNode*)pEvent);
×
1478
  nodesDestroyNode(pStartCond);
×
1479
  nodesDestroyNode(pEndCond);
×
1480
  nodesDestroyNode(pTrueForLimit);
×
1481
  return NULL;
×
1482
}
1483

1484
SNode* createCountWindowNode(SAstCreateContext* pCxt, const SToken* pCountToken, const SToken* pSlidingToken) {
1,008✔
1485
  SCountWindowNode* pCount = NULL;
1,008✔
1486
  CHECK_PARSER_STATUS(pCxt);
1,008!
1487
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
1,008✔
1488
  CHECK_MAKE_NODE(pCount);
1,008!
1489
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
1,008✔
1490
  CHECK_MAKE_NODE(pCount->pCol);
1,008!
1491
  pCount->windowCount = taosStr2Int64(pCountToken->z, NULL, 10);
1,008✔
1492
  pCount->windowSliding = taosStr2Int64(pSlidingToken->z, NULL, 10);
1,008✔
1493
  return (SNode*)pCount;
1,008✔
1494
_err:
×
1495
  nodesDestroyNode((SNode*)pCount);
×
1496
  return NULL;
×
1497
}
1498

1499
SNode* createAnomalyWindowNode(SAstCreateContext* pCxt, SNode* pExpr, const SToken* pFuncOpt) {
13✔
1500
  SAnomalyWindowNode* pAnomaly = NULL;
13✔
1501
  CHECK_PARSER_STATUS(pCxt);
13!
1502
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ANOMALY_WINDOW, (SNode**)&pAnomaly);
13✔
1503
  CHECK_MAKE_NODE(pAnomaly);
13!
1504
  pAnomaly->pCol = createPrimaryKeyCol(pCxt, NULL);
13✔
1505
  CHECK_MAKE_NODE(pAnomaly->pCol);
13!
1506
  pAnomaly->pExpr = pExpr;
13✔
1507
  if (pFuncOpt == NULL) {
13!
1508
    tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
1509
  } else {
1510
    (void)trimString(pFuncOpt->z, pFuncOpt->n, pAnomaly->anomalyOpt, sizeof(pAnomaly->anomalyOpt));
13✔
1511
  }
1512
  return (SNode*)pAnomaly;
13✔
1513
_err:
×
1514
  nodesDestroyNode((SNode*)pAnomaly);
×
1515
  return NULL;
×
1516
}
1517

1518
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
28,887✔
1519
                                SNode* pFill) {
1520
  SIntervalWindowNode* interval = NULL;
28,887✔
1521
  CHECK_PARSER_STATUS(pCxt);
28,887!
1522
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
28,887✔
1523
  CHECK_MAKE_NODE(interval);
28,887!
1524
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
28,887✔
1525
  CHECK_MAKE_NODE(interval->pCol);
28,886!
1526
  interval->pInterval = pInterval;
28,886✔
1527
  interval->pOffset = pOffset;
28,886✔
1528
  interval->pSliding = pSliding;
28,886✔
1529
  interval->pFill = pFill;
28,886✔
1530
  TAOS_SET_OBJ_ALIGNED(&interval->timeRange, TSWINDOW_INITIALIZER);
28,886✔
1531
  interval->timezone = pCxt->pQueryCxt->timezone;
28,886✔
1532
  return (SNode*)interval;
28,886✔
1533
_err:
×
1534
  nodesDestroyNode((SNode*)interval);
×
1535
  nodesDestroyNode(pInterval);
×
1536
  nodesDestroyNode(pOffset);
×
1537
  nodesDestroyNode(pSliding);
×
1538
  nodesDestroyNode(pFill);
×
1539
  return NULL;
1✔
1540
}
1541

1542
SNode* createWindowOffsetNode(SAstCreateContext* pCxt, SNode* pStartOffset, SNode* pEndOffset) {
1,567✔
1543
  SWindowOffsetNode* winOffset = NULL;
1,567✔
1544
  CHECK_PARSER_STATUS(pCxt);
1,567!
1545
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WINDOW_OFFSET, (SNode**)&winOffset);
1,567✔
1546
  CHECK_MAKE_NODE(winOffset);
1,567!
1547
  winOffset->pStartOffset = pStartOffset;
1,567✔
1548
  winOffset->pEndOffset = pEndOffset;
1,567✔
1549
  return (SNode*)winOffset;
1,567✔
1550
_err:
×
1551
  nodesDestroyNode((SNode*)winOffset);
×
1552
  nodesDestroyNode(pStartOffset);
×
1553
  nodesDestroyNode(pEndOffset);
×
1554
  return NULL;
×
1555
}
1556

1557
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
25,056✔
1558
  SFillNode* fill = NULL;
25,056✔
1559
  CHECK_PARSER_STATUS(pCxt);
25,056!
1560
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
25,056✔
1561
  CHECK_MAKE_NODE(fill);
25,059!
1562
  fill->mode = mode;
25,059✔
1563
  fill->pValues = pValues;
25,059✔
1564
  fill->pWStartTs = NULL;
25,059✔
1565
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&(fill->pWStartTs));
25,059✔
1566
  CHECK_MAKE_NODE(fill->pWStartTs);
25,061!
1567
  tstrncpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart", TSDB_FUNC_NAME_LEN);
25,061✔
1568
  return (SNode*)fill;
25,061✔
1569
_err:
×
1570
  nodesDestroyNode((SNode*)fill);
×
1571
  nodesDestroyNode(pValues);
×
1572
  return NULL;
×
1573
}
1574

1575
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
205,023✔
1576
  SGroupingSetNode* groupingSet = NULL;
205,023✔
1577
  CHECK_PARSER_STATUS(pCxt);
205,023!
1578
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
205,023✔
1579
  CHECK_MAKE_NODE(groupingSet);
205,023!
1580
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
205,023✔
1581
  groupingSet->pParameterList = NULL;
205,023✔
1582
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
205,023✔
1583
  CHECK_PARSER_STATUS(pCxt);
205,023!
1584
  return (SNode*)groupingSet;
205,023✔
1585
_err:
×
1586
  nodesDestroyNode((SNode*)groupingSet);
×
1587
  nodesDestroyNode(pNode);
×
1588
  return NULL;
×
1589
}
1590

1591
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
14,191✔
1592
  CHECK_PARSER_STATUS(pCxt);
14,191!
1593
  if (NULL == pInterval) {
14,191✔
1594
    if (pEnd && nodeType(pEnd) == QUERY_NODE_VALUE && ((SValueNode*)pEnd)->flag & VALUE_FLAG_IS_DURATION) {
13,985!
1595
      return createInterpTimeAround(pCxt, pStart, NULL, pEnd);
5,071✔
1596
    }
1597
    return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
8,914✔
1598
  }
1599

1600
  return createInterpTimeAround(pCxt, pStart, pEnd, pInterval);
206✔
1601

1602
_err:
×
1603

1604
  nodesDestroyNode(pStart);
×
1605
  nodesDestroyNode(pEnd);
×
1606
  return NULL;
×
1607
}
1608

1609
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
5,475✔
1610
  CHECK_PARSER_STATUS(pCxt);
5,475!
1611
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
5,475✔
1612
_err:
×
1613
  nodesDestroyNode(pPoint);
×
1614
  return NULL;
×
1615
}
1616

1617
SNode* createInterpTimeAround(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
5,280✔
1618
  CHECK_PARSER_STATUS(pCxt);
5,280!
1619
  SRangeAroundNode* pAround = NULL;
5,280✔
1620
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RANGE_AROUND, (SNode**)&pAround);
5,280✔
1621
  CHECK_PARSER_STATUS(pCxt);
5,280!
1622
  if (NULL == pEnd) {
5,280✔
1623
    pAround->pRange = createInterpTimePoint(pCxt, pStart);
5,070✔
1624
  } else {
1625
    pAround->pRange = createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
210✔
1626
  }
1627
  pAround->pInterval = pInterval;
5,282✔
1628
  CHECK_PARSER_STATUS(pCxt);
5,282!
1629
  return (SNode*)pAround;
5,282✔
1630
_err:
×
1631
  return NULL;
×
1632
}
1633

1634
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
15,076✔
1635
  CHECK_PARSER_STATUS(pCxt);
15,076!
1636
  SWhenThenNode* pWhenThen = NULL;
15,076✔
1637
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
15,076✔
1638
  CHECK_MAKE_NODE(pWhenThen);
15,076!
1639
  pWhenThen->pWhen = pWhen;
15,076✔
1640
  pWhenThen->pThen = pThen;
15,076✔
1641
  return (SNode*)pWhenThen;
15,076✔
1642
_err:
×
1643
  nodesDestroyNode(pWhen);
×
1644
  nodesDestroyNode(pThen);
×
1645
  return NULL;
×
1646
}
1647

1648
SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
11,389✔
1649
  CHECK_PARSER_STATUS(pCxt);
11,389!
1650
  SCaseWhenNode* pCaseWhen = NULL;
11,389✔
1651
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CASE_WHEN, (SNode**)&pCaseWhen);
11,389✔
1652
  CHECK_MAKE_NODE(pCaseWhen);
11,389!
1653
  pCaseWhen->pCase = pCase;
11,389✔
1654
  pCaseWhen->pWhenThenList = pWhenThenList;
11,389✔
1655
  pCaseWhen->pElse = pElse;
11,389✔
1656
  pCaseWhen->tz = pCxt->pQueryCxt->timezone;
11,389✔
1657
  pCaseWhen->charsetCxt = pCxt->pQueryCxt->charsetCxt;
11,389✔
1658
  return (SNode*)pCaseWhen;
11,389✔
1659
_err:
×
1660
  nodesDestroyNode(pCase);
×
1661
  nodesDestroyList(pWhenThenList);
×
1662
  nodesDestroyNode(pElse);
×
1663
  return NULL;
×
1664
}
1665

1666
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
747,291✔
1667
  CHECK_PARSER_STATUS(pCxt);
747,291!
1668
  trimEscape(pCxt, pAlias);
747,291✔
1669
  SExprNode* pExpr = (SExprNode*)pNode;
747,291✔
1670
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
747,291✔
1671
  strncpy(pExpr->aliasName, pAlias->z, len);
747,291✔
1672
  pExpr->aliasName[len] = '\0';
747,291✔
1673
  strncpy(pExpr->userAlias, pAlias->z, len);
747,291✔
1674
  pExpr->userAlias[len] = '\0';
747,291✔
1675
  pExpr->asAlias = true;
747,291✔
1676
  return pNode;
747,291✔
1677
_err:
×
1678
  nodesDestroyNode(pNode);
×
1679
  return NULL;
×
1680
}
1681

1682
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
1,396,689✔
1683
  CHECK_PARSER_STATUS(pCxt);
1,396,689✔
1684
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,396,684!
1685
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
1,396,692✔
1686
  }
1687
  return pStmt;
1,396,684✔
1688
_err:
5✔
1689
  nodesDestroyNode(pStmt);
5✔
1690
  nodesDestroyNode(pWhere);
5✔
1691
  return NULL;
5✔
1692
}
1693

1694
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
1,396,686✔
1695
  CHECK_PARSER_STATUS(pCxt);
1,396,686✔
1696
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,396,681!
1697
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
1,396,693✔
1698
  }
1699
  return pStmt;
1,396,681✔
1700
_err:
5✔
1701
  nodesDestroyNode(pStmt);
5✔
1702
  nodesDestroyList(pPartitionByList);
5✔
1703
  return NULL;
5✔
1704
}
1705

1706
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
1,396,686✔
1707
  CHECK_PARSER_STATUS(pCxt);
1,396,686✔
1708
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,396,681!
1709
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
1,396,689✔
1710
  }
1711
  return pStmt;
1,396,681✔
1712
_err:
5✔
1713
  nodesDestroyNode(pStmt);
5✔
1714
  nodesDestroyNode(pWindow);
5✔
1715
  return NULL;
5✔
1716
}
1717

1718
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
1,396,682✔
1719
  CHECK_PARSER_STATUS(pCxt);
1,396,682✔
1720
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,396,677!
1721
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
1,396,687✔
1722
  }
1723
  return pStmt;
1,396,677✔
1724
_err:
5✔
1725
  nodesDestroyNode(pStmt);
5✔
1726
  nodesDestroyList(pGroupByList);
5✔
1727
  return NULL;
5✔
1728
}
1729

1730
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
1,396,683✔
1731
  CHECK_PARSER_STATUS(pCxt);
1,396,683✔
1732
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,396,678!
1733
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
1,396,689✔
1734
  }
1735
  return pStmt;
1,396,678✔
1736
_err:
5✔
1737
  nodesDestroyNode(pStmt);
5✔
1738
  nodesDestroyNode(pHaving);
5✔
1739
  return NULL;
5✔
1740
}
1741

1742
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
1,366,344✔
1743
  CHECK_PARSER_STATUS(pCxt);
1,366,344✔
1744
  if (NULL == pOrderByList) {
1,366,339✔
1745
    return pStmt;
1,022,941✔
1746
  }
1747
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
343,398✔
1748
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
303,772✔
1749
  } else {
1750
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
39,626✔
1751
  }
1752
  return pStmt;
343,398✔
1753
_err:
5✔
1754
  nodesDestroyNode(pStmt);
5✔
1755
  nodesDestroyList(pOrderByList);
5✔
1756
  return NULL;
5✔
1757
}
1758

1759
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
1,366,346✔
1760
  CHECK_PARSER_STATUS(pCxt);
1,366,346✔
1761
  if (NULL == pSlimit) {
1,366,341✔
1762
    return pStmt;
1,349,574✔
1763
  }
1764
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
16,767!
1765
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
16,773✔
1766
  }
1767
  return pStmt;
16,767✔
1768
_err:
5✔
1769
  nodesDestroyNode(pStmt);
5✔
1770
  nodesDestroyNode(pSlimit);
5✔
1771
  return NULL;
5✔
1772
}
1773

1774
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
1,366,347✔
1775
  CHECK_PARSER_STATUS(pCxt);
1,366,347✔
1776
  if (NULL == pLimit) {
1,366,342✔
1777
    return pStmt;
1,221,997✔
1778
  }
1779
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
144,345✔
1780
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
133,835✔
1781
  } else {
1782
    ((SSetOperator*)pStmt)->pLimit = pLimit;
10,510✔
1783
  }
1784
  return pStmt;
144,345✔
1785
_err:
5✔
1786
  nodesDestroyNode(pStmt);
5✔
1787
  nodesDestroyNode(pLimit);
5✔
1788
  return NULL;
5✔
1789
}
1790

1791
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
1,396,683✔
1792
  CHECK_PARSER_STATUS(pCxt);
1,396,683✔
1793
  SSelectStmt* pSelect = (SSelectStmt*)pStmt;
1,396,678✔
1794
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,396,678!
1795
    if (pRange && nodeType(pRange) == QUERY_NODE_RANGE_AROUND) {
1,396,694✔
1796
      pSelect->pRangeAround = pRange;
5,271✔
1797
      SRangeAroundNode* pAround = (SRangeAroundNode*)pRange;
5,271✔
1798
      TSWAP(pSelect->pRange, pAround->pRange);
5,271✔
1799
    } else {
1800
      pSelect->pRange = pRange;
1,391,423✔
1801
    }
1802
  }
1803
  return pStmt;
1,396,678✔
1804
_err:
5✔
1805
  nodesDestroyNode(pStmt);
5✔
1806
  nodesDestroyNode(pRange);
5✔
1807
  return NULL;
5✔
1808
}
1809

1810
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
1,396,680✔
1811
  CHECK_PARSER_STATUS(pCxt);
1,396,680✔
1812
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,396,675!
1813
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
1,396,686✔
1814
  }
1815
  return pStmt;
1,396,675✔
1816
_err:
5✔
1817
  nodesDestroyNode(pStmt);
5✔
1818
  nodesDestroyNode(pEvery);
5✔
1819
  return NULL;
5✔
1820
}
1821

1822
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
1,396,686✔
1823
  CHECK_PARSER_STATUS(pCxt);
1,396,686✔
1824
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
1,396,681!
1825
    SFillNode* pFillClause = (SFillNode*)pFill;
14,871✔
1826
    nodesDestroyNode(pFillClause->pWStartTs);
14,871✔
1827
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
14,872✔
1828
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
14,873!
1829
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
14,873✔
1830
  }
1831
  return pStmt;
1,396,683✔
1832
_err:
5✔
1833
  nodesDestroyNode(pStmt);
5✔
1834
  nodesDestroyNode(pFill);
5✔
1835
  return NULL;
5✔
1836
}
1837

1838
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
60,024✔
1839
  CHECK_PARSER_STATUS(pCxt);
60,024!
1840
  if (NULL == pJLimit) {
60,024✔
1841
    return pJoin;
59,482✔
1842
  }
1843
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
542✔
1844
  pJoinNode->pJLimit = pJLimit;
542✔
1845

1846
  return pJoin;
542✔
1847
_err:
×
1848
  nodesDestroyNode(pJoin);
×
1849
  nodesDestroyNode(pJLimit);
×
1850
  return NULL;
×
1851
}
1852

1853
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
60,024✔
1854
  CHECK_PARSER_STATUS(pCxt);
60,024!
1855
  if (NULL == pWinOffset) {
60,024✔
1856
    return pJoin;
58,475✔
1857
  }
1858
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
1,549✔
1859
  pJoinNode->pWindowOffset = pWinOffset;
1,549✔
1860

1861
  return pJoin;
1,549✔
1862
_err:
×
1863
  nodesDestroyNode(pJoin);
×
1864
  nodesDestroyNode(pWinOffset);
×
1865
  return NULL;
×
1866
}
1867

1868
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
1,396,687✔
1869
                        SNodeList* pHint) {
1870
  CHECK_PARSER_STATUS(pCxt);
1,396,687✔
1871
  SNode* select = NULL;
1,396,682✔
1872
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
1,396,682✔
1873
  CHECK_MAKE_NODE(select);
1,396,692!
1874
  return select;
1,396,692✔
1875
_err:
5✔
1876
  nodesDestroyList(pProjectionList);
5✔
1877
  nodesDestroyNode(pTable);
5✔
1878
  nodesDestroyList(pHint);
5✔
1879
  return NULL;
9✔
1880
}
1881

1882
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
1,396,687✔
1883
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,396,687!
1884
    if (pCxt->pQueryCxt->biMode) {
1,396,693✔
1885
      ((SSelectStmt*)pStmt)->tagScan = true;
17✔
1886
    } else {
1887
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
1,396,676✔
1888
    }
1889
  }
1890
  return pStmt;
1,396,687✔
1891
}
1892

1893
static void setSubquery(SNode* pStmt) {
169,888✔
1894
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
169,888✔
1895
    ((SSelectStmt*)pStmt)->isSubquery = true;
168,916✔
1896
  }
1897
}
169,888✔
1898

1899
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
84,944✔
1900
  CHECK_PARSER_STATUS(pCxt);
84,944!
1901
  SSetOperator* setOp = NULL;
84,944✔
1902
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
84,944✔
1903
  CHECK_MAKE_NODE(setOp);
84,944!
1904
  setOp->opType = type;
84,944✔
1905
  setOp->pLeft = pLeft;
84,944✔
1906
  setSubquery(setOp->pLeft);
84,944✔
1907
  setOp->pRight = pRight;
84,944✔
1908
  setSubquery(setOp->pRight);
84,944✔
1909
  snprintf(setOp->stmtName, TSDB_TABLE_NAME_LEN, "%p", setOp);
84,944✔
1910
  return (SNode*)setOp;
84,944✔
1911
_err:
×
1912
  nodesDestroyNode(pLeft);
×
1913
  nodesDestroyNode(pRight);
×
1914
  return NULL;
×
1915
}
1916

1917
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
6,842✔
1918
  if (!pOptions->walRetentionPeriodIsSet) {
6,842✔
1919
    pOptions->walRetentionPeriod =
6,820✔
1920
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
1921
  }
1922
  if (!pOptions->walRetentionSizeIsSet) {
6,842✔
1923
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
6,841✔
1924
  }
1925
  if (!pOptions->walRollPeriodIsSet) {
6,842!
1926
    pOptions->walRollPeriod =
6,842✔
1927
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
1928
  }
1929
}
6,842✔
1930

1931
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
5,605✔
1932
  CHECK_PARSER_STATUS(pCxt);
5,605!
1933
  SDatabaseOptions* pOptions = NULL;
5,605✔
1934
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
5,605✔
1935
  CHECK_MAKE_NODE(pOptions);
5,605!
1936
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
5,605✔
1937
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
5,605✔
1938
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
5,605✔
1939
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
5,605✔
1940
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
5,605✔
1941
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
5,605✔
1942
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
5,605✔
1943
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
5,605✔
1944
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
5,605✔
1945
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
5,605✔
1946
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
5,605✔
1947
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
5,605✔
1948
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
5,605✔
1949
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
5,605✔
1950
  pOptions->precision = TSDB_DEFAULT_PRECISION;
5,605✔
1951
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
5,605✔
1952
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
5,605✔
1953
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
5,605✔
1954
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
5,605✔
1955
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
5,605✔
1956
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
5,605✔
1957
  updateWalOptionsDefault(pOptions);
5,605✔
1958
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
5,605✔
1959
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
5,605✔
1960
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
5,605✔
1961
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
5,605✔
1962
  pOptions->s3ChunkSize = TSDB_DEFAULT_S3_CHUNK_SIZE;
5,605✔
1963
  pOptions->s3KeepLocal = TSDB_DEFAULT_S3_KEEP_LOCAL;
5,605✔
1964
  pOptions->s3Compact = TSDB_DEFAULT_S3_COMPACT;
5,605✔
1965
  pOptions->withArbitrator = TSDB_DEFAULT_DB_WITH_ARBITRATOR;
5,605✔
1966
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
5,605✔
1967
  pOptions->dnodeListStr[0] = 0;
5,605✔
1968
  pOptions->compactInterval = TSDB_DEFAULT_COMPACT_INTERVAL;
5,605✔
1969
  pOptions->compactStartTime = TSDB_DEFAULT_COMPACT_START_TIME;
5,605✔
1970
  pOptions->compactEndTime = TSDB_DEFAULT_COMPACT_END_TIME;
5,605✔
1971
  pOptions->compactTimeOffset = TSDB_DEFAULT_COMPACT_TIME_OFFSET;
5,605✔
1972
  return (SNode*)pOptions;
5,605✔
1973
_err:
×
1974
  return NULL;
×
1975
}
1976

1977
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
521✔
1978
  CHECK_PARSER_STATUS(pCxt);
521!
1979
  SDatabaseOptions* pOptions = NULL;
521✔
1980
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
521✔
1981
  CHECK_MAKE_NODE(pOptions);
521!
1982
  pOptions->buffer = -1;
521✔
1983
  pOptions->cacheModel = -1;
521✔
1984
  pOptions->cacheLastSize = -1;
521✔
1985
  pOptions->compressionLevel = -1;
521✔
1986
  pOptions->daysPerFile = -1;
521✔
1987
  pOptions->fsyncPeriod = -1;
521✔
1988
  pOptions->maxRowsPerBlock = -1;
521✔
1989
  pOptions->minRowsPerBlock = -1;
521✔
1990
  pOptions->keep[0] = -1;
521✔
1991
  pOptions->keep[1] = -1;
521✔
1992
  pOptions->keep[2] = -1;
521✔
1993
  pOptions->pages = -1;
521✔
1994
  pOptions->pagesize = -1;
521✔
1995
  pOptions->tsdbPageSize = -1;
521✔
1996
  pOptions->precision = -1;
521✔
1997
  pOptions->replica = -1;
521✔
1998
  pOptions->strict = -1;
521✔
1999
  pOptions->walLevel = -1;
521✔
2000
  pOptions->numOfVgroups = -1;
521✔
2001
  pOptions->singleStable = -1;
521✔
2002
  pOptions->schemaless = -1;
521✔
2003
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
521✔
2004
  pOptions->walRetentionSize = -2;    // -1 is a valid value
521✔
2005
  pOptions->walRollPeriod = -1;
521✔
2006
  pOptions->walSegmentSize = -1;
521✔
2007
  pOptions->sstTrigger = -1;
521✔
2008
  pOptions->tablePrefix = -1;
521✔
2009
  pOptions->tableSuffix = -1;
521✔
2010
  pOptions->s3ChunkSize = -1;
521✔
2011
  pOptions->s3KeepLocal = -1;
521✔
2012
  pOptions->s3Compact = -1;
521✔
2013
  pOptions->withArbitrator = -1;
521✔
2014
  pOptions->encryptAlgorithm = -1;
521✔
2015
  pOptions->dnodeListStr[0] = 0;
521✔
2016
  pOptions->compactInterval = -1;
521✔
2017
  pOptions->compactStartTime = -1;
521✔
2018
  pOptions->compactEndTime = -1;
521✔
2019
  pOptions->compactTimeOffset = -1;
521✔
2020
  return (SNode*)pOptions;
521✔
2021
_err:
×
2022
  return NULL;
×
2023
}
2024

2025
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
8,331✔
2026
                                    bool alter) {
2027
  CHECK_PARSER_STATUS(pCxt);
8,331!
2028
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
8,331✔
2029
  switch (type) {
8,331!
2030
    case DB_OPTION_BUFFER:
108✔
2031
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
108✔
2032
      break;
108✔
2033
    case DB_OPTION_CACHEMODEL:
306✔
2034
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
306!
2035
      break;
306✔
2036
    case DB_OPTION_CACHESIZE:
66✔
2037
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
66✔
2038
      break;
66✔
2039
    case DB_OPTION_COMP:
36✔
2040
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
36✔
2041
      break;
36✔
2042
    case DB_OPTION_DAYS: {
865✔
2043
      SToken* pToken = pVal;
865✔
2044
      if (TK_NK_INTEGER == pToken->type) {
865✔
2045
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
651✔
2046
      } else {
2047
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
214✔
2048
      }
2049
      break;
865✔
2050
    }
2051
    case DB_OPTION_FSYNC:
75✔
2052
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
75✔
2053
      break;
75✔
2054
    case DB_OPTION_MAXROWS:
68✔
2055
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
68✔
2056
      break;
68✔
2057
    case DB_OPTION_MINROWS:
89✔
2058
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
89✔
2059
      break;
89✔
2060
    case DB_OPTION_KEEP:
818✔
2061
      pDbOptions->pKeep = pVal;
818✔
2062
      break;
818✔
2063
    case DB_OPTION_PAGES:
61✔
2064
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
61✔
2065
      break;
61✔
2066
    case DB_OPTION_PAGESIZE:
18✔
2067
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
18✔
2068
      break;
18✔
2069
    case DB_OPTION_TSDB_PAGESIZE:
16✔
2070
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
16✔
2071
      break;
16✔
2072
    case DB_OPTION_PRECISION:
630✔
2073
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
630!
2074
      break;
630✔
2075
    case DB_OPTION_REPLICA:
1,268✔
2076
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
1,268✔
2077
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
1,268✔
2078
      if (!alter) {
1,268✔
2079
        updateWalOptionsDefault(pDbOptions);
1,238✔
2080
      }
2081
      break;
1,268✔
2082
    case DB_OPTION_STRICT:
×
2083
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
2084
      break;
×
2085
    case DB_OPTION_WAL:
92✔
2086
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
92✔
2087
      break;
92✔
2088
    case DB_OPTION_VGROUPS:
2,229✔
2089
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
2,229✔
2090
      break;
2,229✔
2091
    case DB_OPTION_SINGLE_STABLE:
19✔
2092
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
19✔
2093
      break;
19✔
2094
    case DB_OPTION_RETENTIONS:
33✔
2095
      pDbOptions->pRetentions = pVal;
33✔
2096
      break;
33✔
2097
    case DB_OPTION_WAL_RETENTION_PERIOD:
271✔
2098
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
271✔
2099
      pDbOptions->walRetentionPeriodIsSet = true;
271✔
2100
      break;
271✔
2101
    case DB_OPTION_WAL_RETENTION_SIZE:
59✔
2102
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
59✔
2103
      pDbOptions->walRetentionSizeIsSet = true;
59✔
2104
      break;
59✔
2105
    case DB_OPTION_WAL_ROLL_PERIOD:
4✔
2106
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4✔
2107
      pDbOptions->walRollPeriodIsSet = true;
4✔
2108
      break;
4✔
2109
    case DB_OPTION_WAL_SEGMENT_SIZE:
4✔
2110
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
4✔
2111
      break;
4✔
2112
    case DB_OPTION_STT_TRIGGER:
582✔
2113
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
582✔
2114
      break;
582✔
2115
    case DB_OPTION_TABLE_PREFIX: {
44✔
2116
      SValueNode* pNode = (SValueNode*)pVal;
44✔
2117
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
44!
2118
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
44✔
2119
      } else {
2120
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
2121
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2122
      }
2123
      nodesDestroyNode((SNode*)pNode);
44✔
2124
      break;
44✔
2125
    }
2126
    case DB_OPTION_TABLE_SUFFIX: {
43✔
2127
      SValueNode* pNode = (SValueNode*)pVal;
43✔
2128
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
43!
2129
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
43✔
2130
      } else {
2131
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
2132
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2133
      }
2134
      nodesDestroyNode((SNode*)pNode);
43✔
2135
      break;
43✔
2136
    }
2137
    case DB_OPTION_S3_CHUNKPAGES:
69✔
2138
      pDbOptions->s3ChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
69✔
2139
      break;
69✔
2140
    case DB_OPTION_S3_KEEPLOCAL: {
76✔
2141
      SToken* pToken = pVal;
76✔
2142
      if (TK_NK_INTEGER == pToken->type) {
76✔
2143
        pDbOptions->s3KeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
67✔
2144
      } else {
2145
        pDbOptions->s3KeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
9✔
2146
      }
2147
      break;
76✔
2148
    }
2149
    case DB_OPTION_S3_COMPACT:
61✔
2150
      pDbOptions->s3Compact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
61✔
2151
      break;
61✔
2152
    case DB_OPTION_KEEP_TIME_OFFSET:
18✔
2153
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
18✔
2154
        pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
11✔
2155
      } else {
2156
        pDbOptions->pKeepTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
7✔
2157
      }
2158
      break;
18✔
2159
    case DB_OPTION_ENCRYPT_ALGORITHM:
14✔
2160
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
14✔
2161
      pDbOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
14✔
2162
      break;
14✔
2163
    case DB_OPTION_DNODES:
37✔
2164
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
37✔
2165
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
1✔
2166
                 TSDB_DNODE_LIST_LEN);
2167
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1✔
2168
      } else {
2169
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
36!
2170
      }
2171
      break;
37✔
2172
    case DB_OPTION_COMPACT_INTERVAL:
46✔
2173
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
46✔
2174
        pDbOptions->compactInterval = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
8✔
2175
      } else {
2176
        pDbOptions->pCompactIntervalNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
38✔
2177
      }
2178
      break;
46✔
2179
    case DB_OPTION_COMPACT_TIME_RANGE:
68✔
2180
      pDbOptions->pCompactTimeRangeList = pVal;
68✔
2181
      break;
68✔
2182
    case DB_OPTION_COMPACT_TIME_OFFSET:
41✔
2183
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
41✔
2184
        pDbOptions->compactTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
19✔
2185
      } else {
2186
        pDbOptions->pCompactTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
22✔
2187
      }
2188
      break;
41✔
2189
    default:
97✔
2190
      break;
97✔
2191
  }
2192
  return pOptions;
8,331✔
2193
_err:
×
2194
  nodesDestroyNode(pOptions);
×
2195
  return NULL;
×
2196
}
2197

2198
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
7,738✔
2199
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
7,738✔
2200
}
2201

2202
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
594✔
2203
  CHECK_PARSER_STATUS(pCxt);
594!
2204
  switch (pAlterOption->type) {
594✔
2205
    case DB_OPTION_KEEP:
128✔
2206
    case DB_OPTION_RETENTIONS:
2207
    case DB_OPTION_COMPACT_TIME_RANGE:
2208
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
128✔
2209
    default:
466✔
2210
      break;
466✔
2211
  }
2212
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
466✔
2213
_err:
×
2214
  nodesDestroyNode(pOptions);
×
2215
  nodesDestroyList(pAlterOption->pList);
×
2216
  return NULL;
×
2217
}
2218

2219
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
5,550✔
2220
  CHECK_PARSER_STATUS(pCxt);
5,550✔
2221
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
5,549✔
2222
  SCreateDatabaseStmt* pStmt = NULL;
5,546✔
2223
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
5,546✔
2224
  CHECK_MAKE_NODE(pStmt);
5,546!
2225
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
5,546✔
2226
  pStmt->ignoreExists = ignoreExists;
5,546✔
2227
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
5,546✔
2228
  return (SNode*)pStmt;
5,546✔
2229
_err:
3✔
2230
  nodesDestroyNode(pOptions);
3✔
2231
  return NULL;
3✔
2232
}
2233

2234
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
3,815✔
2235
  CHECK_PARSER_STATUS(pCxt);
3,815!
2236
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
3,815!
2237
  SDropDatabaseStmt* pStmt = NULL;
3,815✔
2238
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
3,815✔
2239
  CHECK_MAKE_NODE(pStmt);
3,815!
2240
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
3,815✔
2241
  pStmt->ignoreNotExists = ignoreNotExists;
3,815✔
2242
  return (SNode*)pStmt;
3,815✔
2243
_err:
×
2244
  return NULL;
×
2245
}
2246

2247
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
515✔
2248
  CHECK_PARSER_STATUS(pCxt);
515!
2249
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
515!
2250
  SAlterDatabaseStmt* pStmt = NULL;
515✔
2251
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
515✔
2252
  CHECK_MAKE_NODE(pStmt);
515!
2253
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
515✔
2254
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
515✔
2255
  return (SNode*)pStmt;
515✔
2256
_err:
×
2257
  nodesDestroyNode(pOptions);
×
2258
  return NULL;
×
2259
}
2260

2261
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
9,974✔
2262
  CHECK_PARSER_STATUS(pCxt);
9,974!
2263
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
9,974!
2264
  SFlushDatabaseStmt* pStmt = NULL;
9,974✔
2265
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
9,974✔
2266
  CHECK_MAKE_NODE(pStmt);
9,974!
2267
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
9,974✔
2268
  return (SNode*)pStmt;
9,974✔
2269
_err:
×
2270
  return NULL;
×
2271
}
2272

2273
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
17✔
2274
  CHECK_PARSER_STATUS(pCxt);
17!
2275
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
17!
2276
  STrimDatabaseStmt* pStmt = NULL;
17✔
2277
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
17✔
2278
  CHECK_MAKE_NODE(pStmt);
17!
2279
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
17✔
2280
  pStmt->maxSpeed = maxSpeed;
17✔
2281
  return (SNode*)pStmt;
17✔
2282
_err:
×
2283
  return NULL;
×
2284
}
2285

2286
SNode* createS3MigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
3✔
2287
  CHECK_PARSER_STATUS(pCxt);
3!
2288
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
3!
2289
  SS3MigrateDatabaseStmt* pStmt = NULL;
3✔
2290
  pCxt->errCode = nodesMakeNode(QUERY_NODE_S3MIGRATE_DATABASE_STMT, (SNode**)&pStmt);
3✔
2291
  CHECK_MAKE_NODE(pStmt);
3!
2292
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
3✔
2293
  return (SNode*)pStmt;
3✔
2294
_err:
×
2295
  return NULL;
×
2296
}
2297

2298
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd, bool metaOnly) {
107✔
2299
  CHECK_PARSER_STATUS(pCxt);
107!
2300
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
107!
2301
  SCompactDatabaseStmt* pStmt = NULL;
107✔
2302
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
107✔
2303
  CHECK_MAKE_NODE(pStmt);
107!
2304
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
107✔
2305
  pStmt->pStart = pStart;
107✔
2306
  pStmt->pEnd = pEnd;
107✔
2307
  pStmt->metaOnly = metaOnly;
107✔
2308
  return (SNode*)pStmt;
107✔
2309
_err:
×
2310
  nodesDestroyNode(pStart);
×
2311
  nodesDestroyNode(pEnd);
×
2312
  return NULL;
×
2313
}
2314

2315
SNode* createCompactVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
6✔
2316
                                SNode* pEnd, bool metaOnly) {
2317
  CHECK_PARSER_STATUS(pCxt);
6!
2318
  if (NULL == pDbName) {
6✔
2319
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
1✔
2320
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
1✔
2321
    CHECK_PARSER_STATUS(pCxt);
1!
2322
  }
2323
  SCompactVgroupsStmt* pStmt = NULL;
5✔
2324
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_VGROUPS_STMT, (SNode**)&pStmt);
5✔
2325
  CHECK_MAKE_NODE(pStmt);
5!
2326
  pStmt->pDbName = pDbName;
5✔
2327
  pStmt->vgidList = vgidList;
5✔
2328
  pStmt->pStart = pStart;
5✔
2329
  pStmt->pEnd = pEnd;
5✔
2330
  pStmt->metaOnly = metaOnly;
5✔
2331
  return (SNode*)pStmt;
5✔
2332
_err:
1✔
2333
  nodesDestroyNode(pDbName);
1✔
2334
  nodesDestroyList(vgidList);
1✔
2335
  nodesDestroyNode(pStart);
1✔
2336
  nodesDestroyNode(pEnd);
1✔
2337
  return NULL;
1✔
2338
}
2339

2340
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
113,610✔
2341
  CHECK_PARSER_STATUS(pCxt);
113,610!
2342
  STableOptions* pOptions = NULL;
113,610✔
2343
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
113,610✔
2344
  CHECK_MAKE_NODE(pOptions);
113,640!
2345
  pOptions->maxDelay1 = -1;
113,640✔
2346
  pOptions->maxDelay2 = -1;
113,640✔
2347
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
113,640✔
2348
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
113,640✔
2349
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
113,640✔
2350
  pOptions->keep = -1;
113,640✔
2351
  pOptions->commentNull = true;  // mark null
113,640✔
2352
  return (SNode*)pOptions;
113,640✔
2353
_err:
×
2354
  return NULL;
×
2355
}
2356

2357
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
182✔
2358
  CHECK_PARSER_STATUS(pCxt);
182!
2359
  STableOptions* pOptions = NULL;
182✔
2360
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
182✔
2361
  CHECK_MAKE_NODE(pOptions);
182!
2362
  pOptions->ttl = -1;
182✔
2363
  pOptions->commentNull = true;  // mark null
182✔
2364
  pOptions->keep = -1;
182✔
2365
  return (SNode*)pOptions;
182✔
2366
_err:
×
2367
  return NULL;
×
2368
}
2369

2370
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
704✔
2371
  CHECK_PARSER_STATUS(pCxt);
704!
2372
  switch (type) {
704!
2373
    case TABLE_OPTION_COMMENT:
159✔
2374
      if (checkComment(pCxt, (SToken*)pVal, true)) {
159✔
2375
        ((STableOptions*)pOptions)->commentNull = false;
146✔
2376
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
146✔
2377
      }
2378
      break;
159✔
2379
    case TABLE_OPTION_MAXDELAY:
14✔
2380
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
14✔
2381
      break;
14✔
2382
    case TABLE_OPTION_WATERMARK:
13✔
2383
      ((STableOptions*)pOptions)->pWatermark = pVal;
13✔
2384
      break;
13✔
2385
    case TABLE_OPTION_ROLLUP:
23✔
2386
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
23✔
2387
      break;
23✔
2388
    case TABLE_OPTION_TTL: {
202✔
2389
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
202✔
2390
      if (ttl > INT32_MAX) {
202✔
2391
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
5✔
2392
      } else {
2393
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
2394
        ((STableOptions*)pOptions)->ttl = ttl;
197✔
2395
      }
2396
      break;
202✔
2397
    }
2398
    case TABLE_OPTION_SMA:
167✔
2399
      ((STableOptions*)pOptions)->pSma = pVal;
167✔
2400
      break;
167✔
2401
    case TABLE_OPTION_DELETE_MARK:
4✔
2402
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
4✔
2403
      break;
4✔
2404
    case TABLE_OPTION_KEEP:
90✔
2405
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
90✔
2406
        ((STableOptions*)pOptions)->keep = taosStr2Int32(((SToken*)pVal)->z, NULL, 10) * 1440;
18✔
2407
      } else {
2408
        ((STableOptions*)pOptions)->pKeepNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
72✔
2409
      }
2410
      break;
90✔
2411
    case TABLE_OPTION_VIRTUAL: {
32✔
2412
      int64_t virtualStb = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
32✔
2413
      if (virtualStb != 0 && virtualStb != 1) {
32!
2414
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
2415
      } else {
2416
        ((STableOptions*)pOptions)->virtualStb = virtualStb;
32✔
2417
      }
2418
      break;
32✔
2419
    }
2420
    default:
×
2421
      break;
×
2422
  }
2423
  return pOptions;
704✔
2424
_err:
×
2425
  nodesDestroyNode(pOptions);
×
2426
  return NULL;
×
2427
}
2428

2429
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
596,808✔
2430
  CHECK_PARSER_STATUS(pCxt);
596,808!
2431
  SColumnOptions* pOptions = NULL;
596,808✔
2432
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
596,808✔
2433
  CHECK_MAKE_NODE(pOptions);
596,808!
2434
  pOptions->commentNull = true;
596,808✔
2435
  pOptions->bPrimaryKey = false;
596,808✔
2436
  pOptions->hasRef = false;
596,808✔
2437
  return (SNode*)pOptions;
596,808✔
2438
_err:
×
2439
  return NULL;
×
2440
}
2441

2442
EColumnOptionType getColumnOptionType(const char* optionType) {
5,008✔
2443
  if (0 == strcasecmp(optionType, "ENCODE")) {
5,008✔
2444
    return COLUMN_OPTION_ENCODE;
556✔
2445
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
4,452✔
2446
    return COLUMN_OPTION_COMPRESS;
2,288✔
2447
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
2,164✔
2448
    return COLUMN_OPTION_LEVEL;
2,162✔
2449
  }
2450
  return 0;
2✔
2451
}
2452

2453
SNode* setColumnReference(SAstCreateContext* pCxt, SNode* pOptions, SNode* pRef) {
1,184✔
2454
  CHECK_PARSER_STATUS(pCxt);
1,184!
2455

2456
  ((SColumnOptions*)pOptions)->hasRef = true;
1,184✔
2457
  tstrncpy(((SColumnOptions*)pOptions)->refDb, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
1,184✔
2458
  tstrncpy(((SColumnOptions*)pOptions)->refTable, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
1,184✔
2459
  tstrncpy(((SColumnOptions*)pOptions)->refColumn, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
1,184✔
2460
  return pOptions;
1,184✔
2461
_err:
×
2462
  nodesDestroyNode(pOptions);
×
2463
  return NULL;
×
2464
}
2465

2466
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
627✔
2467
  CHECK_PARSER_STATUS(pCxt);
627!
2468
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
627✔
2469
  return pOptions;
627✔
2470
_err:
×
2471
  nodesDestroyNode(pOptions);
×
2472
  return NULL;
×
2473
}
2474

2475
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
5,008✔
2476
  CHECK_PARSER_STATUS(pCxt);
5,008!
2477
  char optionType[TSDB_CL_OPTION_LEN];
2478

2479
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
5,008✔
2480
  strncpy(optionType, pVal1->z, TMIN(pVal1->n, TSDB_CL_OPTION_LEN));
5,008✔
2481
  if (0 == strlen(optionType)) {
5,008!
2482
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2483
    return pOptions;
×
2484
  }
2485
  EColumnOptionType type = getColumnOptionType(optionType);
5,008✔
2486
  switch (type) {
5,008✔
2487
    case COLUMN_OPTION_ENCODE:
556✔
2488
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
556✔
2489
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
556!
2490
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
556!
2491
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
2492
      }
2493
      break;
556✔
2494
    case COLUMN_OPTION_COMPRESS:
2,288✔
2495
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
2,288✔
2496
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
2,288!
2497
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
2,288!
2498
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
2499
      }
2500
      break;
2,288✔
2501
    case COLUMN_OPTION_LEVEL:
2,162✔
2502
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
2,162✔
2503
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
2,162!
2504
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
2,162!
2505
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2506
      }
2507
      break;
2,162✔
2508
    default:
2✔
2509
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2✔
2510
      break;
2✔
2511
  }
2512
  return pOptions;
5,008✔
2513
_err:
×
2514
  nodesDestroyNode(pOptions);
×
2515
  return NULL;
×
2516
}
2517

2518
SNode* createColumnRefNodeByNode(SAstCreateContext* pCxt, SToken* pColName, SNode* pRef) {
419✔
2519
  CHECK_PARSER_STATUS(pCxt);
419!
2520
  CHECK_NAME(checkColumnName(pCxt, pColName));
419!
2521

2522
  SColumnRefNode* pCol = NULL;
419✔
2523
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
419✔
2524
  CHECK_MAKE_NODE(pCol);
419!
2525
  if (pColName) {
419!
2526
    COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
419✔
2527
  }
2528
  tstrncpy(pCol->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
419✔
2529
  tstrncpy(pCol->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
419✔
2530
  tstrncpy(pCol->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
419✔
2531
  return (SNode*)pCol;
419✔
2532
_err:
×
2533
  return NULL;
×
2534
}
2535

2536
STokenTriplet* createTokenTriplet(SAstCreateContext* pCxt, SToken pName) {
2,221✔
2537
  CHECK_PARSER_STATUS(pCxt);
2,221!
2538

2539
  STokenTriplet* pTokenTri = taosMemoryMalloc(sizeof(STokenTriplet));
2,221!
2540
  CHECK_OUT_OF_MEM(pTokenTri);
2,221!
2541
  pTokenTri->name[0] = pName;
2,221✔
2542
  pTokenTri->numOfName = 1;
2,221✔
2543

2544
  return pTokenTri;
2,221✔
2545
_err:
×
2546
  return NULL;
×
2547
}
2548

2549
STokenTriplet* setColumnName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri, SToken pName) {
2,398✔
2550
  CHECK_PARSER_STATUS(pCxt);
2,398!
2551

2552
  if (pTokenTri->numOfName >= 3) {
2,398!
2553
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2554
    goto _err;
×
2555
  }
2556

2557
  pTokenTri->name[pTokenTri->numOfName] = pName;
2,398✔
2558
  pTokenTri->numOfName++;
2,398✔
2559
  return pTokenTri;
2,398✔
2560
_err:
×
2561
  return NULL;
×
2562
}
2563

2564
SNode* createColumnRefNodeByName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri) {
2,221✔
2565
  SColumnRefNode* pCol = NULL;
2,221✔
2566
  CHECK_PARSER_STATUS(pCxt);
2,221!
2567
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
2,221✔
2568
  CHECK_MAKE_NODE(pCol);
2,221!
2569

2570
  switch (pTokenTri->numOfName) {
2,221!
2571
    case 2: {
2,044✔
2572
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[0]));
2,044!
2573
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[1]));
2,044!
2574
      snprintf(pCol->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
2,044✔
2575
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[0]);
2,044✔
2576
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[1]);
2,044✔
2577
      break;
2,044✔
2578
    }
2579
    case 3: {
177✔
2580
      CHECK_NAME(checkDbName(pCxt, &pTokenTri->name[0], true));
177!
2581
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[1]));
177!
2582
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[2]));
177!
2583
      COPY_STRING_FORM_ID_TOKEN(pCol->refDbName, &pTokenTri->name[0]);
177✔
2584
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[1]);
177✔
2585
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[2]);
177✔
2586
      break;
177✔
2587
    }
2588
    default: {
×
2589
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2590
      goto _err;
×
2591
    }
2592
  }
2593

2594
  taosMemFree(pTokenTri);
2,221✔
2595
  return (SNode*)pCol;
2,221✔
2596
_err:
×
2597
  taosMemFree(pTokenTri);
×
2598
  nodesDestroyNode((SNode*)pCol);
×
2599
  return NULL;
×
2600
}
2601

2602
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
624,350✔
2603
  CHECK_PARSER_STATUS(pCxt);
624,350!
2604
  CHECK_NAME(checkColumnName(pCxt, pColName));
624,350✔
2605
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
624,346✔
2606
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
20✔
2607
    CHECK_PARSER_STATUS(pCxt);
20!
2608
  }
2609
  SColumnDefNode* pCol = NULL;
624,326✔
2610
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
624,326✔
2611
  CHECK_MAKE_NODE(pCol);
624,326!
2612
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
624,326✔
2613
  pCol->dataType = dataType;
624,326✔
2614
  pCol->pOptions = pNode;
624,326✔
2615
  pCol->sma = true;
624,326✔
2616
  return (SNode*)pCol;
624,326✔
2617
_err:
26✔
2618
  nodesDestroyNode(pNode);
26✔
2619
  return NULL;
26✔
2620
}
2621

2622
SDataType createDataType(uint8_t type) {
664,921✔
2623
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
664,921✔
2624
  return dt;
664,921✔
2625
}
2626

2627
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
844,339✔
2628
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
844,339✔
2629
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
844,339✔
2630
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
844,339✔
2631
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
844,339✔
2632
  return dt;
844,339✔
2633
}
2634

2635
SDataType createDecimalDataType(uint8_t type, const SToken* pPrecisionToken, const SToken* pScaleToken) {
977✔
2636
  SDataType dt = {0};
977✔
2637
  dt.precision = taosStr2UInt8(pPrecisionToken->z, NULL, 10);
977✔
2638
  dt.scale = pScaleToken ? taosStr2Int32(pScaleToken->z, NULL, 10) : 0;
977✔
2639
  dt.type = decimalTypeFromPrecision(dt.precision);
977✔
2640
  dt.bytes = tDataTypes[dt.type].bytes;
977✔
2641
  return dt;
977✔
2642
}
2643

2644
SNode* createCreateVTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols) {
204✔
2645
  SCreateVTableStmt* pStmt = NULL;
204✔
2646
  CHECK_PARSER_STATUS(pCxt);
204!
2647
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
204✔
2648
  CHECK_MAKE_NODE(pStmt);
204!
2649
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
204✔
2650
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
204✔
2651
  pStmt->ignoreExists = ignoreExists;
204✔
2652
  pStmt->pCols = pCols;
204✔
2653
  nodesDestroyNode(pRealTable);
204✔
2654
  return (SNode*)pStmt;
204✔
2655
_err:
×
2656
  nodesDestroyNode(pRealTable);
×
2657
  nodesDestroyList(pCols);
×
2658
  return NULL;
×
2659
}
2660

2661
SNode* createCreateVSubTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable,
180✔
2662
                                 SNodeList* pSpecificColRefs, SNodeList* pColRefs, SNode* pUseRealTable,
2663
                                 SNodeList* pSpecificTags, SNodeList* pValsOfTags) {
2664
  CHECK_PARSER_STATUS(pCxt);
180!
2665
  SCreateVSubTableStmt* pStmt = NULL;
180✔
2666
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT, (SNode**)&pStmt);
180✔
2667
  CHECK_MAKE_NODE(pStmt);
180!
2668
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
180✔
2669
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
180✔
2670
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
180✔
2671
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
180✔
2672
  pStmt->ignoreExists = ignoreExists;
180✔
2673
  pStmt->pSpecificTags = pSpecificTags;
180✔
2674
  pStmt->pValsOfTags = pValsOfTags;
180✔
2675
  pStmt->pSpecificColRefs = pSpecificColRefs;
180✔
2676
  pStmt->pColRefs = pColRefs;
180✔
2677
  nodesDestroyNode(pRealTable);
180✔
2678
  nodesDestroyNode(pUseRealTable);
180✔
2679
  return (SNode*)pStmt;
180✔
2680
_err:
×
2681
  nodesDestroyNode(pRealTable);
×
2682
  nodesDestroyNode(pUseRealTable);
×
2683
  nodesDestroyList(pSpecificTags);
×
2684
  nodesDestroyList(pValsOfTags);
×
2685
  nodesDestroyList(pSpecificColRefs);
×
2686
  nodesDestroyList(pColRefs);
×
2687
  return NULL;
×
2688
}
2689

2690
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
21,638✔
2691
                             SNodeList* pTags, SNode* pOptions) {
2692
  CHECK_PARSER_STATUS(pCxt);
21,638✔
2693
  SCreateTableStmt* pStmt = NULL;
21,633✔
2694
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
21,633✔
2695
  CHECK_MAKE_NODE(pStmt);
21,632!
2696
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
21,632✔
2697
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
21,632✔
2698
  pStmt->ignoreExists = ignoreExists;
21,632✔
2699
  pStmt->pCols = pCols;
21,632✔
2700
  pStmt->pTags = pTags;
21,632✔
2701
  pStmt->pOptions = (STableOptions*)pOptions;
21,632✔
2702
  nodesDestroyNode(pRealTable);
21,632✔
2703
  return (SNode*)pStmt;
21,633✔
2704
_err:
5✔
2705
  nodesDestroyNode(pRealTable);
5✔
2706
  nodesDestroyList(pCols);
5✔
2707
  nodesDestroyList(pTags);
5✔
2708
  nodesDestroyNode(pOptions);
5✔
2709
  return NULL;
5✔
2710
}
2711

2712
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
91,951✔
2713
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
2714
  CHECK_PARSER_STATUS(pCxt);
91,951!
2715
  SCreateSubTableClause* pStmt = NULL;
91,951✔
2716
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
91,951✔
2717
  CHECK_MAKE_NODE(pStmt);
91,983!
2718
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
91,983✔
2719
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
91,983✔
2720
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
91,983✔
2721
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
91,983✔
2722
  pStmt->ignoreExists = ignoreExists;
91,983✔
2723
  pStmt->pSpecificTags = pSpecificTags;
91,983✔
2724
  pStmt->pValsOfTags = pValsOfTags;
91,983✔
2725
  pStmt->pOptions = (STableOptions*)pOptions;
91,983✔
2726
  nodesDestroyNode(pRealTable);
91,983✔
2727
  nodesDestroyNode(pUseRealTable);
91,980✔
2728
  return (SNode*)pStmt;
91,989✔
2729
_err:
×
2730
  nodesDestroyNode(pRealTable);
×
2731
  nodesDestroyNode(pOptions);
×
2732
  nodesDestroyNode(pUseRealTable);
×
2733
  nodesDestroyList(pSpecificTags);
×
2734
  nodesDestroyList(pValsOfTags);
×
2735
  return NULL;
×
2736
}
2737

2738
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
2✔
2739
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
2740
  CHECK_PARSER_STATUS(pCxt);
2!
2741
  SCreateSubTableFromFileClause* pStmt = NULL;
2✔
2742
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
2✔
2743
  CHECK_MAKE_NODE(pStmt);
2!
2744
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2✔
2745
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2✔
2746
  pStmt->ignoreExists = ignoreExists;
2✔
2747
  pStmt->pSpecificTags = pSpecificTags;
2✔
2748
  if (TK_NK_STRING == pFilePath->type) {
2!
2749
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
2✔
2750
  } else {
2751
    strncpy(pStmt->filePath, pFilePath->z, pFilePath->n);
×
2752
  }
2753

2754
  nodesDestroyNode(pUseRealTable);
2✔
2755
  return (SNode*)pStmt;
2✔
2756
_err:
×
2757
  nodesDestroyNode(pUseRealTable);
×
2758
  nodesDestroyList(pSpecificTags);
×
2759
  return NULL;
×
2760
}
2761

2762
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
71,076✔
2763
  CHECK_PARSER_STATUS(pCxt);
71,076!
2764
  SCreateMultiTablesStmt* pStmt = NULL;
71,076✔
2765
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
71,076✔
2766
  CHECK_MAKE_NODE(pStmt);
71,122!
2767
  pStmt->pSubTables = pSubTables;
71,122✔
2768
  return (SNode*)pStmt;
71,122✔
2769
_err:
×
2770
  nodesDestroyList(pSubTables);
×
2771
  return NULL;
×
2772
}
2773

2774
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
18,516✔
2775
  CHECK_PARSER_STATUS(pCxt);
18,516✔
2776
  SDropTableClause* pStmt = NULL;
18,515✔
2777
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
18,515✔
2778
  CHECK_MAKE_NODE(pStmt);
18,515!
2779
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
18,515✔
2780
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
18,515✔
2781
  pStmt->ignoreNotExists = ignoreNotExists;
18,515✔
2782
  nodesDestroyNode(pRealTable);
18,515✔
2783
  return (SNode*)pStmt;
18,515✔
2784
_err:
1✔
2785
  nodesDestroyNode(pRealTable);
1✔
2786
  return NULL;
1✔
2787
}
2788

2789
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
17,586✔
2790
  CHECK_PARSER_STATUS(pCxt);
17,586✔
2791
  SDropTableStmt* pStmt = NULL;
17,585✔
2792
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
17,585✔
2793
  CHECK_MAKE_NODE(pStmt);
17,585!
2794
  pStmt->pTables = pTables;
17,585✔
2795
  pStmt->withOpt = withOpt;
17,585✔
2796
  return (SNode*)pStmt;
17,585✔
2797
_err:
1✔
2798
  nodesDestroyList(pTables);
1✔
2799
  return NULL;
1✔
2800
}
2801

2802
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
596✔
2803
  CHECK_PARSER_STATUS(pCxt);
596!
2804
  SDropSuperTableStmt* pStmt = NULL;
596✔
2805
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
596✔
2806
  CHECK_MAKE_NODE(pStmt);
596!
2807
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
596✔
2808
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
596✔
2809
  pStmt->ignoreNotExists = ignoreNotExists;
596✔
2810
  pStmt->withOpt = withOpt;
596✔
2811
  nodesDestroyNode(pRealTable);
596✔
2812
  return (SNode*)pStmt;
596✔
2813
_err:
×
2814
  nodesDestroyNode(pRealTable);
×
2815
  return NULL;
×
2816
}
2817

2818
SNode* createDropVirtualTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
62✔
2819
  CHECK_PARSER_STATUS(pCxt);
62!
2820
  SDropVirtualTableStmt* pStmt = NULL;
62✔
2821
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
62✔
2822
  CHECK_MAKE_NODE(pStmt);
62!
2823
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
62✔
2824
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
62✔
2825
  pStmt->ignoreNotExists = ignoreNotExists;
62✔
2826
  pStmt->withOpt = withOpt;
62✔
2827
  nodesDestroyNode(pRealTable);
62✔
2828
  return (SNode*)pStmt;
62✔
2829
_err:
×
2830
  nodesDestroyNode(pRealTable);
×
2831
  return NULL;
×
2832
}
2833

2834
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
18,256✔
2835
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
18,256✔
2836
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
18,256✔
2837
  nodesDestroyNode(pRealTable);
18,256✔
2838
  return (SNode*)pStmt;
18,256✔
2839
}
2840

2841
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
182✔
2842
  CHECK_PARSER_STATUS(pCxt);
182✔
2843
  SAlterTableStmt* pStmt = NULL;
169✔
2844
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
169✔
2845
  CHECK_MAKE_NODE(pStmt);
169!
2846
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
169✔
2847
  pStmt->pOptions = (STableOptions*)pOptions;
169✔
2848
  return createAlterTableStmtFinalize(pRealTable, pStmt);
169✔
2849
_err:
13✔
2850
  nodesDestroyNode(pRealTable);
13✔
2851
  nodesDestroyNode(pOptions);
13✔
2852
  return NULL;
13✔
2853
}
2854

2855
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
4,628✔
2856
                                    SDataType dataType) {
2857
  CHECK_PARSER_STATUS(pCxt);
4,628!
2858
  CHECK_NAME(checkColumnName(pCxt, pColName));
4,628✔
2859
  SAlterTableStmt* pStmt = NULL;
4,627✔
2860
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
4,627✔
2861
  CHECK_MAKE_NODE(pStmt);
4,627!
2862
  pStmt->alterType = alterType;
4,627✔
2863
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
4,627✔
2864
  pStmt->dataType = dataType;
4,627✔
2865
  return createAlterTableStmtFinalize(pRealTable, pStmt);
4,627✔
2866
_err:
1✔
2867
  nodesDestroyNode(pRealTable);
1✔
2868
  return NULL;
1✔
2869
}
2870

2871
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
4,261✔
2872
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
2873
  SAlterTableStmt* pStmt = NULL;
4,261✔
2874
  CHECK_PARSER_STATUS(pCxt);
4,261✔
2875
  CHECK_NAME(checkColumnName(pCxt, pColName));
4,259✔
2876
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
4,258✔
2877
  CHECK_MAKE_NODE(pStmt);
4,258!
2878
  pStmt->alterType = alterType;
4,258✔
2879
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
4,258✔
2880
  pStmt->dataType = dataType;
4,258✔
2881
  pStmt->pColOptions = (SColumnOptions*)pOptions;
4,258✔
2882

2883
  if (pOptions != NULL) {
4,258!
2884
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
4,258✔
2885
    if (pOption->hasRef) {
4,258✔
2886
      if (!pOption->commentNull || pOption->bPrimaryKey || 0 != strcmp(pOption->compress, "") ||
54!
2887
          0 != strcmp(pOption->encode, "") || 0 != strcmp(pOption->compressLevel, "")) {
54!
2888
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
×
2889
      }
2890
      pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF;
54✔
2891
      tstrncpy(pStmt->refDbName, pOption->refDb, TSDB_DB_NAME_LEN);
54✔
2892
      tstrncpy(pStmt->refTableName, pOption->refTable, TSDB_TABLE_NAME_LEN);
54✔
2893
      tstrncpy(pStmt->refColName, pOption->refColumn, TSDB_COL_NAME_LEN);
54✔
2894
      CHECK_PARSER_STATUS(pCxt);
54!
2895
    } else if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
4,204!
2896
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
4,200✔
2897
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
349✔
2898
      } else {
2899
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
2900
        //                                         "not support alter column with option except compress");
2901
        // return NULL;
2902
      }
2903
    } else {
2904
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
4✔
2905
                                              "not support alter column with option except compress");
2906
      CHECK_PARSER_STATUS(pCxt);
4!
2907
    }
2908
  }
2909
  return createAlterTableStmtFinalize(pRealTable, pStmt);
4,254✔
2910
_err:
7✔
2911
  nodesDestroyNode(pOptions);
7✔
2912
  nodesDestroyNode((SNode*)pStmt);
7✔
2913
  nodesDestroyNode(pRealTable);
7✔
2914
  return NULL;
7✔
2915
}
2916

2917
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
103✔
2918
                                           SToken* pColName, SNode* pOptions) {
2919
  CHECK_PARSER_STATUS(pCxt);
103!
2920
  CHECK_NAME(checkColumnName(pCxt, pColName));
103!
2921
  SAlterTableStmt* pStmt = NULL;
103✔
2922
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
103✔
2923
  CHECK_MAKE_NODE(pStmt);
103!
2924
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
103✔
2925
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
103✔
2926
  pStmt->pColOptions = (SColumnOptions*)pOptions;
103✔
2927
  return createAlterTableStmtFinalize(pRealTable, pStmt);
103✔
2928
_err:
×
2929
  nodesDestroyNode(pOptions);
×
2930
  nodesDestroyNode(pRealTable);
×
2931
  return NULL;
×
2932
}
2933

2934
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
1,392✔
2935
  CHECK_PARSER_STATUS(pCxt);
1,392!
2936
  CHECK_NAME(checkColumnName(pCxt, pColName));
1,392✔
2937
  SAlterTableStmt* pStmt = NULL;
1,390✔
2938
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
1,390✔
2939
  CHECK_MAKE_NODE(pStmt);
1,390!
2940
  pStmt->alterType = alterType;
1,390✔
2941
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1,390✔
2942
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1,390✔
2943
_err:
2✔
2944
  nodesDestroyNode(pRealTable);
2✔
2945
  return NULL;
2✔
2946
}
2947

2948
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
777✔
2949
                                 SToken* pNewColName) {
2950
  CHECK_PARSER_STATUS(pCxt);
777!
2951
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
777!
2952
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
777✔
2953
  SAlterTableStmt* pStmt = NULL;
775✔
2954
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
775✔
2955
  CHECK_MAKE_NODE(pStmt);
775!
2956
  pStmt->alterType = alterType;
775✔
2957
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
775✔
2958
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
775✔
2959
  return createAlterTableStmtFinalize(pRealTable, pStmt);
775✔
2960
_err:
2✔
2961
  nodesDestroyNode(pRealTable);
2✔
2962
  return NULL;
2✔
2963
}
2964

2965
SNode* createAlterTableAlterColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
120✔
2966
                                   SNode* pRef) {
2967
  CHECK_PARSER_STATUS(pCxt);
120!
2968
  CHECK_NAME(checkColumnName(pCxt, pColName));
120!
2969
  SAlterTableStmt* pStmt = NULL;
120✔
2970
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
120✔
2971
  CHECK_MAKE_NODE(pStmt);
120!
2972
  pStmt->alterType = alterType;
120✔
2973
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
120✔
2974
  tstrncpy(pStmt->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
120✔
2975
  tstrncpy(pStmt->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
120✔
2976
  tstrncpy(pStmt->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
120✔
2977
  return createAlterTableStmtFinalize(pRealTable, pStmt);
120✔
2978
_err:
×
2979
  nodesDestroyNode(pRealTable);
×
2980
  return NULL;
×
2981
}
2982

2983
SNode* createAlterTableRemoveColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
52✔
2984
                                    const SToken* pLiteral) {
2985
  CHECK_PARSER_STATUS(pCxt);
52!
2986
  CHECK_NAME(checkColumnName(pCxt, pColName));
52!
2987
  SAlterTableStmt* pStmt = NULL;
52✔
2988
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
52✔
2989
  CHECK_MAKE_NODE(pStmt);
52!
2990
  pStmt->alterType = alterType;
52✔
2991
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
52✔
2992
  return createAlterTableStmtFinalize(pRealTable, pStmt);
52✔
2993
_err:
×
2994
  nodesDestroyNode(pRealTable);
×
2995
  return NULL;
×
2996
}
2997

2998
SNode* createAlterSingleTagColumnNode(SAstCreateContext* pCtx, SToken* pTagName, SNode* pVal) {
6,819✔
2999
  CHECK_PARSER_STATUS(pCtx);
6,819!
3000
  SAlterTableStmt* pStmt = NULL;
6,819✔
3001
  pCtx->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
6,819✔
3002
  CHECK_MAKE_NODE(pStmt);
6,819!
3003
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
6,819✔
3004
  CHECK_NAME(checkColumnName(pCtx, pTagName));
6,819!
3005
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
6,819✔
3006
  pStmt->pVal = (SValueNode*)pVal;
6,819✔
3007
  pStmt->pNodeListTagValue = NULL;
6,819✔
3008
  return (SNode*)pStmt;
6,819✔
3009
_err:
×
3010
  return NULL;
×
3011
}
3012

3013
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
×
3014
  CHECK_PARSER_STATUS(pCxt);
×
3015
  CHECK_NAME(checkColumnName(pCxt, pTagName));
×
3016
  SAlterTableStmt* pStmt = NULL;
×
3017
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
3018
  CHECK_MAKE_NODE(pStmt);
×
3019
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
×
3020
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
×
3021
  pStmt->pVal = (SValueNode*)pVal;
×
3022
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
3023
_err:
×
3024
  nodesDestroyNode(pVal);
×
3025
  nodesDestroyNode(pRealTable);
×
3026
  return NULL;
×
3027
}
3028

3029
SNode* createAlterTableSetMultiTagValue(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pList) {
6,766✔
3030
  CHECK_PARSER_STATUS(pCxt);
6,766!
3031
  SAlterTableStmt* pStmt = NULL;
6,766✔
3032
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
6,766✔
3033

3034
  CHECK_MAKE_NODE(pStmt);
6,766!
3035
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL;
6,766✔
3036
  pStmt->pNodeListTagValue = pList;
6,766✔
3037
  return createAlterTableStmtFinalize(pRealTable, pStmt);
6,766✔
3038
_err:
×
3039
  return NULL;
×
3040
}
3041

3042
SNode* setAlterSuperTableType(SNode* pStmt) {
1,239✔
3043
  if (!pStmt) return NULL;
1,239✔
3044
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
1,236✔
3045
  return pStmt;
1,236✔
3046
}
3047

3048
SNode* setAlterVirtualTableType(SNode* pStmt) {
433✔
3049
  if (!pStmt) return NULL;
433!
3050
  setNodeType(pStmt, QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT);
433✔
3051
  return pStmt;
433✔
3052
}
3053

3054
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
6,971✔
3055
  CHECK_PARSER_STATUS(pCxt);
6,971!
3056
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
6,971!
3057
  SUseDatabaseStmt* pStmt = NULL;
6,991✔
3058
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
6,991✔
3059
  CHECK_MAKE_NODE(pStmt);
6,986!
3060
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
6,986✔
3061
  return (SNode*)pStmt;
6,986✔
3062
_err:
×
3063
  return NULL;
×
3064
}
3065

3066
static bool needDbShowStmt(ENodeType type) {
3,744✔
3067
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
2,457✔
3068
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
1,024✔
3069
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
155!
3070
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type ||
117!
3071
         QUERY_NODE_SHOW_USAGE_STMT == type || QUERY_NODE_SHOW_VTABLES_STMT == type;
6,201!
3072
}
3073

3074
SNode* createShowStmtWithLike(SAstCreateContext* pCxt, ENodeType type, SNode* pLikePattern) {
615✔
3075
  CHECK_PARSER_STATUS(pCxt);
615!
3076
  SShowStmt* pStmt = NULL;
615✔
3077
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
615✔
3078
  CHECK_MAKE_NODE(pStmt);
615!
3079
  pStmt->withFull = false;
615✔
3080
  pStmt->pTbName = pLikePattern;
615✔
3081
  if (pLikePattern) {
615✔
3082
    pStmt->tableCondType = OP_TYPE_LIKE;
18✔
3083
  }
3084
  return (SNode*)pStmt;
615✔
3085
_err:
×
3086
  nodesDestroyNode(pLikePattern);
×
3087
  return NULL;
×
3088
}
3089

3090
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
2,426✔
3091
  CHECK_PARSER_STATUS(pCxt);
2,426!
3092
  SShowStmt* pStmt = NULL;
2,426✔
3093
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,426✔
3094
  CHECK_MAKE_NODE(pStmt);
2,426!
3095
  pStmt->withFull = false;
2,426✔
3096
  return (SNode*)pStmt;
2,426✔
3097
_err:
×
3098
  return NULL;
×
3099
}
3100

3101
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
×
3102
  CHECK_PARSER_STATUS(pCxt);
×
3103
  SShowStmt* pStmt = NULL;
×
3104
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3105
  CHECK_MAKE_NODE(pStmt);
×
3106
  pStmt->withFull = true;
×
3107
  return (SNode*)pStmt;
×
3108
_err:
×
3109
  return NULL;
×
3110
}
3111

3112
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
1,347✔
3113
  CHECK_PARSER_STATUS(pCxt);
1,347!
3114
  SShowCompactsStmt* pStmt = NULL;
1,347✔
3115
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
1,347✔
3116
  CHECK_MAKE_NODE(pStmt);
1,347!
3117
  return (SNode*)pStmt;
1,347✔
3118
_err:
×
3119
  return NULL;
×
3120
}
3121

3122
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
2,408✔
3123
  if (pStmt == NULL) {
2,408!
3124
    return NULL;
×
3125
  }
3126
  SShowStmt* pShow = (SShowStmt*)pStmt;
2,408✔
3127
  pShow->showKind = showKind;
2,408✔
3128
  return pStmt;
2,408✔
3129
}
3130

3131
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
3,744✔
3132
                              EOperatorType tableCondType) {
3133
  CHECK_PARSER_STATUS(pCxt);
3,744!
3134
  if (needDbShowStmt(type) && NULL == pDbName) {
3,744!
3135
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
2✔
3136
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2✔
3137
    CHECK_PARSER_STATUS(pCxt);
2!
3138
  }
3139
  SShowStmt* pStmt = NULL;
3,742✔
3140
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,742✔
3141
  CHECK_MAKE_NODE(pStmt);
3,742!
3142
  pStmt->pDbName = pDbName;
3,742✔
3143
  pStmt->pTbName = pTbName;
3,742✔
3144
  pStmt->tableCondType = tableCondType;
3,742✔
3145
  return (SNode*)pStmt;
3,742✔
3146
_err:
2✔
3147
  nodesDestroyNode(pDbName);
2✔
3148
  nodesDestroyNode(pTbName);
2✔
3149
  return NULL;
2✔
3150
}
3151

3152
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
1,287✔
3153
                            EOperatorType tableCondType) {
3154
  CHECK_PARSER_STATUS(pCxt);
1,287!
3155
  SNode* pDbName = NULL;
1,287✔
3156
  if (option.dbName.type == TK_NK_NIL) {
1,287✔
3157
    pDbName = createDefaultDatabaseCondValue(pCxt);
1,045✔
3158
  } else {
3159
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
242✔
3160
  }
3161

3162
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
1,287!
3163
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3164
    return NULL;
×
3165
  }
3166

3167
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
1,287✔
3168
  CHECK_PARSER_STATUS(pCxt);
1,287✔
3169
  (void)setShowKind(pCxt, pStmt, option.kind);
1,286✔
3170
  return pStmt;
1,286✔
3171
_err:
1✔
3172
  nodesDestroyNode(pTbName);
1✔
3173
  return NULL;
1✔
3174
}
3175

3176
SNode* createShowVTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
117✔
3177
                             EOperatorType tableCondType) {
3178
  CHECK_PARSER_STATUS(pCxt);
117!
3179
  SNode* pDbName = NULL;
117✔
3180
  if (option.dbName.type == TK_NK_NIL) {
117✔
3181
    pDbName = createDefaultDatabaseCondValue(pCxt);
24✔
3182
  } else {
3183
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
93✔
3184
  }
3185

3186
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
117!
3187
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3188
    return NULL;
×
3189
  }
3190

3191
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_VTABLES_STMT, pDbName, pTbName, tableCondType);
117✔
3192
  CHECK_PARSER_STATUS(pCxt);
117!
3193
  (void)setShowKind(pCxt, pStmt, option.kind);
117✔
3194
  return pStmt;
117✔
3195
_err:
×
3196
  nodesDestroyNode(pTbName);
×
3197
  return NULL;
×
3198
}
3199

3200
SNode* createShowSTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
747✔
3201
                             EOperatorType tableCondType) {
3202
  CHECK_PARSER_STATUS(pCxt);
747!
3203
  SNode* pDbName = NULL;
747✔
3204
  if (option.dbName.type == TK_NK_NIL) {
747✔
3205
    pDbName = createDefaultDatabaseCondValue(pCxt);
142✔
3206
  } else {
3207
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
605✔
3208
  }
3209

3210
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_VIRTUAL &&
747✔
3211
      option.kind != SHOW_KIND_ALL) {
743!
3212
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3213
    return NULL;
×
3214
  }
3215

3216
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_STABLES_STMT, pDbName, pTbName, tableCondType);
747✔
3217
  CHECK_PARSER_STATUS(pCxt);
747!
3218
  (void)setShowKind(pCxt, pStmt, option.kind);
747✔
3219
  return pStmt;
747✔
3220
_err:
×
3221
  nodesDestroyNode(pTbName);
×
3222
  return NULL;
×
3223
}
3224

3225
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
103✔
3226
  CHECK_PARSER_STATUS(pCxt);
103!
3227
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
103!
3228
  SShowCreateDatabaseStmt* pStmt = NULL;
103✔
3229
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
103✔
3230
  CHECK_MAKE_NODE(pStmt);
103!
3231
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
103✔
3232
  return (SNode*)pStmt;
103✔
3233
_err:
×
3234
  return NULL;
×
3235
}
3236

3237
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
25✔
3238
  CHECK_PARSER_STATUS(pCxt);
25!
3239
  SToken  dbToken = {0};
25✔
3240
  SToken* pDbToken = NULL;
25✔
3241

3242
  if (pNode) {
25✔
3243
    SValueNode* pDbName = (SValueNode*)pNode;
5✔
3244
    if (pDbName->literal) {
5!
3245
      dbToken.z = pDbName->literal;
5✔
3246
      dbToken.n = strlen(pDbName->literal);
5✔
3247
      pDbToken = &dbToken;
5✔
3248
    }
3249
  }
3250

3251
  if (pDbToken) {
25✔
3252
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
5!
3253
  }
3254

3255
  SShowAliveStmt* pStmt = NULL;
25✔
3256
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
25✔
3257
  CHECK_PARSER_STATUS(pCxt);
25!
3258

3259
  if (pDbToken) {
25✔
3260
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
5✔
3261
  }
3262
  if (pNode) {
25✔
3263
    nodesDestroyNode(pNode);
5✔
3264
  }
3265

3266
  return (SNode*)pStmt;
25✔
3267
_err:
×
3268
  nodesDestroyNode(pNode);
×
3269
  return NULL;
×
3270
}
3271

3272
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
258✔
3273
  CHECK_PARSER_STATUS(pCxt);
258✔
3274
  SShowCreateTableStmt* pStmt = NULL;
256✔
3275
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
256✔
3276
  CHECK_MAKE_NODE(pStmt);
256!
3277
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
256✔
3278
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
256✔
3279
  nodesDestroyNode(pRealTable);
256✔
3280
  return (SNode*)pStmt;
256✔
3281
_err:
2✔
3282
  nodesDestroyNode(pRealTable);
2✔
3283
  return NULL;
2✔
3284
}
3285

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

3300
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
20✔
3301
  CHECK_PARSER_STATUS(pCxt);
20!
3302
  SShowCreateViewStmt* pStmt = NULL;
20✔
3303
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
20✔
3304
  CHECK_MAKE_NODE(pStmt);
20!
3305
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
20✔
3306
  tstrncpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
20✔
3307
  nodesDestroyNode(pRealTable);
20✔
3308
  return (SNode*)pStmt;
20✔
3309
_err:
×
3310
  nodesDestroyNode(pRealTable);
×
3311
  return NULL;
×
3312
}
3313

3314
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
18✔
3315
  CHECK_PARSER_STATUS(pCxt);
18!
3316
  SShowTableDistributedStmt* pStmt = NULL;
18✔
3317
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
18✔
3318
  CHECK_MAKE_NODE(pStmt);
18!
3319
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
18✔
3320
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
18✔
3321
  nodesDestroyNode(pRealTable);
18✔
3322
  return (SNode*)pStmt;
18✔
3323
_err:
×
3324
  nodesDestroyNode(pRealTable);
×
3325
  return NULL;
×
3326
}
3327

3328
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
448✔
3329
  CHECK_PARSER_STATUS(pCxt);
448!
3330
  SShowDnodeVariablesStmt* pStmt = NULL;
448✔
3331
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
448✔
3332
  CHECK_MAKE_NODE(pStmt);
448!
3333
  pStmt->pDnodeId = pDnodeId;
448✔
3334
  pStmt->pLikePattern = pLikePattern;
448✔
3335
  return (SNode*)pStmt;
448✔
3336
_err:
×
3337
  nodesDestroyNode(pDnodeId);
×
3338
  nodesDestroyNode(pLikePattern);
×
3339
  return NULL;
×
3340
}
3341

3342
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
28✔
3343
  CHECK_PARSER_STATUS(pCxt);
28!
3344
  SShowVnodesStmt* pStmt = NULL;
28✔
3345
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
28✔
3346
  CHECK_MAKE_NODE(pStmt);
28!
3347
  pStmt->pDnodeId = pDnodeId;
28✔
3348
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
28✔
3349
  return (SNode*)pStmt;
28✔
3350
_err:
×
3351
  nodesDestroyNode(pDnodeId);
×
3352
  nodesDestroyNode(pDnodeEndpoint);
×
3353
  return NULL;
×
3354
}
3355

3356
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
43✔
3357
  CHECK_PARSER_STATUS(pCxt);
43!
3358
  if (NULL == pDbName) {
43!
3359
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
3360
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3361
    CHECK_PARSER_STATUS(pCxt);
×
3362
  }
3363
  SShowTableTagsStmt* pStmt = NULL;
43✔
3364
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
43✔
3365
  CHECK_MAKE_NODE(pStmt);
43!
3366
  pStmt->pDbName = pDbName;
43✔
3367
  pStmt->pTbName = pTbName;
43✔
3368
  pStmt->pTags = pTags;
43✔
3369
  return (SNode*)pStmt;
43✔
3370
_err:
×
3371
  nodesDestroyNode(pTbName);
×
3372
  nodesDestroyNode(pDbName);
×
3373
  nodesDestroyList(pTags);
×
3374
  return NULL;
×
3375
}
3376

3377
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
39✔
3378
  CHECK_PARSER_STATUS(pCxt);
39!
3379
  SShowCompactDetailsStmt* pStmt = NULL;
39✔
3380
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
39✔
3381
  CHECK_MAKE_NODE(pStmt);
39!
3382
  pStmt->pCompactId = pCompactId;
39✔
3383
  return (SNode*)pStmt;
39✔
3384
_err:
×
3385
  nodesDestroyNode(pCompactId);
×
3386
  return NULL;
×
3387
}
3388

3389
SNode* createShowTransactionDetailsStmt(SAstCreateContext* pCxt, SNode* pTransactionIdNode) {
1✔
3390
  CHECK_PARSER_STATUS(pCxt);
1!
3391
  SShowTransactionDetailsStmt* pStmt = NULL;
1✔
3392
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT, (SNode**)&pStmt);
1✔
3393
  CHECK_MAKE_NODE(pStmt);
1!
3394
  pStmt->pTransactionId = pTransactionIdNode;
1✔
3395
  return (SNode*)pStmt;
1✔
3396
_err:
×
3397
  nodesDestroyNode(pTransactionIdNode);
×
3398
  return NULL;
×
3399
}
3400

3401
static int32_t getIpRangeFromStr(char* ipRange, SIpRange* pIpRange) {
9✔
3402
  int32_t code = 0;
9✔
3403

3404
  int8_t isIp6 = ((strchr(ipRange, ':')) != NULL ? 1 : 0);
9✔
3405
  if (isIp6) {
9!
3406
    struct in6_addr ip6;
3407
    if (inet_pton(AF_INET6, ipRange, &ip6) == 1) {
×
3408
      pIpRange->type = 1;
×
3409
      memcpy(&pIpRange->ipV6.addr[0], ip6.s6_addr, 8);
×
3410
      memcpy(&pIpRange->ipV6.addr[1], ip6.s6_addr + 8, 8);
×
3411

3412
    } else {
3413
      return TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3414
    }
3415
  } else {
3416
    struct in_addr ip4;
3417
    if (inet_pton(AF_INET, ipRange, &ip4) == 1) {
9✔
3418
      pIpRange->type = 0;
8✔
3419
      memcpy(&pIpRange->ipV4.ip, &ip4.s_addr, sizeof(ip4.s_addr));
8✔
3420
    } else {
3421
      return TSDB_CODE_PAR_INVALID_IP_RANGE;
1✔
3422
    }
3423
  }
3424

3425
  return code;
8✔
3426
}
3427
static int32_t getIpRangeFromWhitelistItem(char* ipRange, SIpRange* pIpRange) {
9✔
3428
  int32_t code = TSDB_CODE_SUCCESS;
9✔
3429
  int32_t lino = 0;
9✔
3430
  char*   ipCopy = NULL;
9✔
3431
  int32_t mask = 0;
9✔
3432
#ifndef TD_ASTRA
3433

3434
  ipCopy = taosStrdup(ipRange);
9!
3435
  if (ipCopy == NULL) {
9!
3436
    code = terrno;
×
3437
    TAOS_CHECK_GOTO(code, &lino, _error);
×
3438
  }
3439

3440
  char* slash = strchr(ipCopy, '/');
9✔
3441
  if (slash) {
9✔
3442
    *slash = '\0';
6✔
3443
    code = taosStr2int32(slash + 1, &mask);
6✔
3444
    TAOS_CHECK_GOTO(code, &lino, _error);
6!
3445
  }
3446

3447
  code = getIpRangeFromStr(ipCopy, pIpRange);
9✔
3448
  TAOS_CHECK_GOTO(code, &lino, _error);
9✔
3449

3450
  if (!slash) {
8✔
3451
    mask = pIpRange->type == 0 ? 32 : 128;
3!
3452
  }
3453
  code = tIpRangeSetMask(pIpRange, mask);
8✔
3454
  TAOS_CHECK_GOTO(code, &lino, _error);
8✔
3455

3456
#endif
3457
_error:
7✔
3458
  taosMemoryFreeClear(ipCopy);
9!
3459
  return code;
9✔
3460
}
3461

3462
static int32_t fillIpRangesFromWhiteList(SAstCreateContext* pCxt, SNodeList* pIpRangesNodeList, SIpRange* pIpRanges) {
7✔
3463
  int32_t i = 0;
7✔
3464
  int32_t code = 0;
7✔
3465

3466
  SNode* pNode = NULL;
7✔
3467
  FOREACH(pNode, pIpRangesNodeList) {
14!
3468
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
9!
3469
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IP_RANGE);
×
3470
      return TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3471
    }
3472
    SValueNode* pValNode = (SValueNode*)(pNode);
9✔
3473
    code = getIpRangeFromWhitelistItem(pValNode->literal, pIpRanges + i);
9✔
3474
    ++i;
9✔
3475
    if (code != TSDB_CODE_SUCCESS) {
9✔
3476
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid IP range %s", pValNode->literal);
2✔
3477
      return code;
2✔
3478
    }
3479
  }
3480
  return TSDB_CODE_SUCCESS;
5✔
3481
}
3482

3483
SNode* addCreateUserStmtWhiteList(SAstCreateContext* pCxt, SNode* pCreateUserStmt, SNodeList* pIpRangesNodeList) {
283✔
3484
  if (NULL == pCreateUserStmt) {
283✔
3485
    if (pIpRangesNodeList != NULL) {
82✔
3486
      nodesDestroyList(pIpRangesNodeList);
4✔
3487
    }
3488
    return NULL;
82✔
3489
  }
3490

3491
  if (NULL == pIpRangesNodeList) {
201✔
3492
    return pCreateUserStmt;
196✔
3493
  }
3494

3495
  ((SCreateUserStmt*)pCreateUserStmt)->pNodeListIpRanges = pIpRangesNodeList;
5✔
3496
  SCreateUserStmt* pCreateUser = (SCreateUserStmt*)pCreateUserStmt;
5✔
3497
  pCreateUser->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
5!
3498
  pCreateUser->pIpRanges = taosMemoryMalloc(pCreateUser->numIpRanges * sizeof(SIpRange));
5!
3499
  CHECK_OUT_OF_MEM(pCreateUser->pIpRanges);
5!
3500

3501
  pCxt->errCode = fillIpRangesFromWhiteList(pCxt, pIpRangesNodeList, pCreateUser->pIpRanges);
5✔
3502
  CHECK_PARSER_STATUS(pCxt);
5✔
3503

3504
  return pCreateUserStmt;
3✔
3505
_err:
2✔
3506
  nodesDestroyNode(pCreateUserStmt);
2✔
3507
  nodesDestroyList(pIpRangesNodeList);
2✔
3508
  return NULL;
2✔
3509
}
3510

3511
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo,
283✔
3512
                            int8_t createDb, int8_t is_import) {
3513
  CHECK_PARSER_STATUS(pCxt);
283!
3514
  char password[TSDB_USET_PASSWORD_LONGLEN + 3] = {0};
283✔
3515
  CHECK_NAME(checkUserName(pCxt, pUserName));
283✔
3516
  if (is_import == 0) {
281✔
3517
    CHECK_NAME(checkPassword(pCxt, pPassword, password));
279✔
3518
  } else {
3519
    CHECK_NAME(checkImportPassword(pCxt, pPassword, password));
2✔
3520
  }
3521
  SCreateUserStmt* pStmt = NULL;
201✔
3522
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
201✔
3523
  CHECK_MAKE_NODE(pStmt);
201!
3524
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
201✔
3525
  tstrncpy(pStmt->password, password, TSDB_USET_PASSWORD_LONGLEN);
201✔
3526
  pStmt->sysinfo = sysinfo;
201✔
3527
  pStmt->createDb = createDb;
201✔
3528
  pStmt->isImport = is_import;
201✔
3529
  return (SNode*)pStmt;
201✔
3530
_err:
82✔
3531
  return NULL;
82✔
3532
}
3533

3534
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, void* pAlterInfo) {
146✔
3535
  SAlterUserStmt* pStmt = NULL;
146✔
3536
  CHECK_PARSER_STATUS(pCxt);
146!
3537
  CHECK_NAME(checkUserName(pCxt, pUserName));
146!
3538
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
146✔
3539
  CHECK_MAKE_NODE(pStmt);
146!
3540
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
146✔
3541
  pStmt->alterType = alterType;
146✔
3542
  switch (alterType) {
146!
3543
    case TSDB_ALTER_USER_PASSWD: {
77✔
3544
      char    password[TSDB_USET_PASSWORD_LONGLEN] = {0};
77✔
3545
      SToken* pVal = pAlterInfo;
77✔
3546
      CHECK_NAME(checkPassword(pCxt, pVal, password));
77✔
3547
      tstrncpy(pStmt->password, password, TSDB_USET_PASSWORD_LONGLEN);
42✔
3548
      break;
42✔
3549
    }
3550
    case TSDB_ALTER_USER_ENABLE: {
20✔
3551
      SToken* pVal = pAlterInfo;
20✔
3552
      pStmt->enable = taosStr2Int8(pVal->z, NULL, 10);
20✔
3553
      break;
20✔
3554
    }
3555
    case TSDB_ALTER_USER_SYSINFO: {
25✔
3556
      SToken* pVal = pAlterInfo;
25✔
3557
      pStmt->sysinfo = taosStr2Int8(pVal->z, NULL, 10);
25✔
3558
      break;
25✔
3559
    }
3560
    case TSDB_ALTER_USER_CREATEDB: {
22✔
3561
      SToken* pVal = pAlterInfo;
22✔
3562
      pStmt->createdb = taosStr2Int8(pVal->z, NULL, 10);
22✔
3563
      break;
22✔
3564
    }
3565
    case TSDB_ALTER_USER_ADD_WHITE_LIST:
2✔
3566
    case TSDB_ALTER_USER_DROP_WHITE_LIST: {
3567
      SNodeList* pIpRangesNodeList = pAlterInfo;
2✔
3568
      pStmt->pNodeListIpRanges = pIpRangesNodeList;
2✔
3569
      pStmt->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
2!
3570
      pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpRange));
2!
3571
      CHECK_OUT_OF_MEM(pStmt->pIpRanges);
2!
3572

3573
      pCxt->errCode = fillIpRangesFromWhiteList(pCxt, pIpRangesNodeList, pStmt->pIpRanges);
2✔
3574
      CHECK_PARSER_STATUS(pCxt);
2!
3575
      break;
2✔
3576
    }
3577
    default:
×
3578
      break;
×
3579
  }
3580
  return (SNode*)pStmt;
111✔
3581
_err:
35✔
3582
  nodesDestroyNode((SNode*)pStmt);
35✔
3583
  return NULL;
35✔
3584
}
3585

3586
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
71✔
3587
  CHECK_PARSER_STATUS(pCxt);
71!
3588
  CHECK_NAME(checkUserName(pCxt, pUserName));
71!
3589
  SDropUserStmt* pStmt = NULL;
71✔
3590
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
71✔
3591
  CHECK_MAKE_NODE(pStmt);
71!
3592
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
71✔
3593
  return (SNode*)pStmt;
71✔
3594
_err:
×
3595
  return NULL;
×
3596
}
3597

3598
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
555✔
3599
  CHECK_PARSER_STATUS(pCxt);
555!
3600
  SCreateDnodeStmt* pStmt = NULL;
555✔
3601
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
555✔
3602
  CHECK_MAKE_NODE(pStmt);
555!
3603
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
555!
3604
    nodesDestroyNode((SNode*)pStmt);
×
3605
    return NULL;
×
3606
  }
3607
  return (SNode*)pStmt;
555✔
3608
_err:
×
3609
  return NULL;
×
3610
}
3611

3612
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
65✔
3613
  CHECK_PARSER_STATUS(pCxt);
65!
3614
  SDropDnodeStmt* pStmt = NULL;
65✔
3615
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
65✔
3616
  CHECK_MAKE_NODE(pStmt);
65!
3617
  if (TK_NK_INTEGER == pDnode->type) {
65✔
3618
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
41✔
3619
  } else {
3620
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
24!
3621
      nodesDestroyNode((SNode*)pStmt);
×
3622
      return NULL;
×
3623
    }
3624
  }
3625
  pStmt->force = force;
65✔
3626
  pStmt->unsafe = unsafe;
65✔
3627
  return (SNode*)pStmt;
65✔
3628
_err:
×
3629
  return NULL;
×
3630
}
3631

3632
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
390✔
3633
                            const SToken* pValue) {
3634
  CHECK_PARSER_STATUS(pCxt);
390!
3635
  SAlterDnodeStmt* pStmt = NULL;
390✔
3636
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
390✔
3637
  CHECK_MAKE_NODE(pStmt);
390!
3638
  if (NULL != pDnode) {
390✔
3639
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
125✔
3640
  } else {
3641
    pStmt->dnodeId = -1;
265✔
3642
  }
3643
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
390✔
3644
  if (NULL != pValue) {
390✔
3645
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
71✔
3646
  }
3647
  return (SNode*)pStmt;
390✔
3648
_err:
×
3649
  return NULL;
×
3650
}
3651

3652
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
2✔
3653
  CHECK_PARSER_STATUS(pCxt);
2!
3654
  SCreateAnodeStmt* pStmt = NULL;
2✔
3655
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
2✔
3656
  CHECK_MAKE_NODE(pStmt);
2!
3657
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
2✔
3658
  return (SNode*)pStmt;
2✔
3659
_err:
×
3660
  return NULL;
×
3661
}
3662

3663
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
2✔
3664
  CHECK_PARSER_STATUS(pCxt);
2!
3665
  SUpdateAnodeStmt* pStmt = NULL;
2✔
3666
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
2✔
3667
  CHECK_MAKE_NODE(pStmt);
2!
3668
  if (NULL != pAnode) {
2!
3669
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
2✔
3670
  } else {
3671
    pStmt->anodeId = -1;
×
3672
  }
3673
  return (SNode*)pStmt;
2✔
3674
_err:
×
3675
  return NULL;
×
3676
}
3677

3678
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
3679
  CHECK_PARSER_STATUS(pCxt);
×
3680
  SUpdateAnodeStmt* pStmt = NULL;
×
3681
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
3682
  CHECK_MAKE_NODE(pStmt);
×
3683
  if (NULL != pAnode) {
×
3684
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
3685
  } else {
3686
    pStmt->anodeId = -1;
×
3687
  }
3688
  return (SNode*)pStmt;
×
3689
_err:
×
3690
  return NULL;
×
3691
}
3692

3693
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
3✔
3694
  SToken config;
3695
  config.type = TK_NK_STRING;
3✔
3696
  config.z = "\"encrypt_key\"";
3✔
3697
  config.n = strlen(config.z);
3✔
3698
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
3✔
3699
}
3700

3701
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
29✔
3702
  if (!checkIndexName(pCxt, pIndexName)) {
29!
3703
    return NULL;
×
3704
  }
3705
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
29✔
3706
}
3707

3708
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
48✔
3709
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
3710
  CHECK_PARSER_STATUS(pCxt);
48!
3711
  SCreateIndexStmt* pStmt = NULL;
48✔
3712
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
48✔
3713
  CHECK_MAKE_NODE(pStmt);
48!
3714
  pStmt->indexType = type;
48✔
3715
  pStmt->ignoreExists = ignoreExists;
48✔
3716

3717
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
48✔
3718
  if (strlen(pFullTable->table.dbName) == 0) {
48!
3719
    // no db specified,
3720
    if (pCxt->pQueryCxt->db == NULL) {
×
3721
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
3722
      CHECK_PARSER_STATUS(pCxt);
×
3723
    } else {
3724
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
3725
    }
3726
  } else {
3727
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
48✔
3728
  }
3729
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
48✔
3730
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
48✔
3731
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
48✔
3732
  nodesDestroyNode(pIndexName);
48✔
3733
  nodesDestroyNode(pRealTable);
48✔
3734
  pStmt->pCols = pCols;
48✔
3735
  pStmt->pOptions = (SIndexOptions*)pOptions;
48✔
3736
  return (SNode*)pStmt;
48✔
3737
_err:
×
3738
  nodesDestroyNode(pIndexName);
×
3739
  nodesDestroyNode(pRealTable);
×
3740
  nodesDestroyNode(pOptions);
×
3741
  nodesDestroyList(pCols);
×
3742
  return NULL;
×
3743
}
3744

3745
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
9✔
3746
                         SNode* pStreamOptions) {
3747
  CHECK_PARSER_STATUS(pCxt);
9!
3748
  SIndexOptions* pOptions = NULL;
9✔
3749
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
9✔
3750
  CHECK_MAKE_NODE(pOptions);
9!
3751
  pOptions->pFuncs = pFuncs;
9✔
3752
  pOptions->pInterval = pInterval;
9✔
3753
  pOptions->pOffset = pOffset;
9✔
3754
  pOptions->pSliding = pSliding;
9✔
3755
  pOptions->pStreamOptions = pStreamOptions;
9✔
3756
  return (SNode*)pOptions;
9✔
3757
_err:
×
3758
  nodesDestroyNode(pInterval);
×
3759
  nodesDestroyNode(pOffset);
×
3760
  nodesDestroyNode(pSliding);
×
3761
  nodesDestroyNode(pStreamOptions);
×
3762
  return NULL;
×
3763
}
3764

3765
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
29✔
3766
  CHECK_PARSER_STATUS(pCxt);
29!
3767
  SDropIndexStmt* pStmt = NULL;
29✔
3768
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
29✔
3769
  CHECK_MAKE_NODE(pStmt);
29!
3770
  pStmt->ignoreNotExists = ignoreNotExists;
29✔
3771
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
29✔
3772
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
29✔
3773
  nodesDestroyNode(pIndexName);
29✔
3774
  return (SNode*)pStmt;
29✔
3775
_err:
×
3776
  nodesDestroyNode(pIndexName);
×
3777
  return NULL;
×
3778
}
3779

3780
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
759✔
3781
  CHECK_PARSER_STATUS(pCxt);
759!
3782
  SCreateComponentNodeStmt* pStmt = NULL;
759✔
3783
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
759✔
3784
  CHECK_MAKE_NODE(pStmt);
759!
3785
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
759✔
3786
  return (SNode*)pStmt;
759✔
3787
_err:
×
3788
  return NULL;
×
3789
}
3790

3791
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
48✔
3792
  CHECK_PARSER_STATUS(pCxt);
48!
3793
  SDropComponentNodeStmt* pStmt = NULL;
48✔
3794
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
48✔
3795
  CHECK_MAKE_NODE(pStmt);
48!
3796
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
48✔
3797
  return (SNode*)pStmt;
48✔
3798
_err:
×
3799
  return NULL;
×
3800
}
3801

3802
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
26✔
3803
  CHECK_PARSER_STATUS(pCxt);
26!
3804
  SRestoreComponentNodeStmt* pStmt = NULL;
26✔
3805
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
26✔
3806
  CHECK_MAKE_NODE(pStmt);
26!
3807
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
26✔
3808
  return (SNode*)pStmt;
26✔
3809
_err:
×
3810
  return NULL;
×
3811
}
3812

3813
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery) {
533✔
3814
  CHECK_PARSER_STATUS(pCxt);
533!
3815
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
533✔
3816
  SCreateTopicStmt* pStmt = NULL;
531✔
3817
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
531✔
3818
  CHECK_MAKE_NODE(pStmt);
531!
3819
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
531✔
3820
  pStmt->ignoreExists = ignoreExists;
531✔
3821
  pStmt->pQuery = pQuery;
531✔
3822
  return (SNode*)pStmt;
531✔
3823
_err:
2✔
3824
  nodesDestroyNode(pQuery);
2✔
3825
  return NULL;
2✔
3826
}
3827

3828
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
86✔
3829
                                  int8_t withMeta) {
3830
  CHECK_PARSER_STATUS(pCxt);
86!
3831
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
86!
3832
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
86!
3833
  SCreateTopicStmt* pStmt = NULL;
86✔
3834
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
86✔
3835
  CHECK_MAKE_NODE(pStmt);
86!
3836
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
86✔
3837
  pStmt->ignoreExists = ignoreExists;
86✔
3838
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
86✔
3839
  pStmt->withMeta = withMeta;
86✔
3840
  return (SNode*)pStmt;
86✔
3841
_err:
×
3842
  return NULL;
×
3843
}
3844

3845
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
51✔
3846
                                     int8_t withMeta, SNode* pWhere) {
3847
  CHECK_PARSER_STATUS(pCxt);
51!
3848
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
51!
3849
  SCreateTopicStmt* pStmt = NULL;
51✔
3850
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
51✔
3851
  CHECK_MAKE_NODE(pStmt);
51!
3852
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
51✔
3853
  pStmt->ignoreExists = ignoreExists;
51✔
3854
  pStmt->withMeta = withMeta;
51✔
3855
  pStmt->pWhere = pWhere;
51✔
3856

3857
  tstrncpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
51✔
3858
  tstrncpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
51✔
3859
  nodesDestroyNode(pRealTable);
51✔
3860
  return (SNode*)pStmt;
51✔
3861
_err:
×
3862
  nodesDestroyNode(pRealTable);
×
3863
  nodesDestroyNode(pWhere);
×
3864
  return NULL;
×
3865
}
3866

3867
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName, bool force) {
363✔
3868
  CHECK_PARSER_STATUS(pCxt);
363!
3869
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
363✔
3870
  SDropTopicStmt* pStmt = NULL;
362✔
3871
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
362✔
3872
  CHECK_MAKE_NODE(pStmt);
362!
3873
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
362✔
3874
  pStmt->ignoreNotExists = ignoreNotExists;
362✔
3875
  pStmt->force = force;
362✔
3876
  return (SNode*)pStmt;
362✔
3877
_err:
1✔
3878
  return NULL;
1✔
3879
}
3880

3881
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName,
14✔
3882
                            bool force) {
3883
  CHECK_PARSER_STATUS(pCxt);
14!
3884
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
14!
3885
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
14!
3886
  SDropCGroupStmt* pStmt = NULL;
14✔
3887
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
14✔
3888
  CHECK_MAKE_NODE(pStmt);
14!
3889
  pStmt->ignoreNotExists = ignoreNotExists;
14✔
3890
  pStmt->force = force;
14✔
3891
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
14✔
3892
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
14✔
3893
  return (SNode*)pStmt;
14✔
3894
_err:
×
3895
  return NULL;
×
3896
}
3897

3898
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
21✔
3899
  CHECK_PARSER_STATUS(pCxt);
21!
3900
  SAlterClusterStmt* pStmt = NULL;
21✔
3901
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
21✔
3902
  CHECK_MAKE_NODE(pStmt);
21!
3903
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
21✔
3904
  if (NULL != pValue) {
21!
3905
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
21✔
3906
  }
3907
  return (SNode*)pStmt;
21✔
3908
_err:
×
3909
  return NULL;
×
3910
}
3911

3912
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
2,476✔
3913
  CHECK_PARSER_STATUS(pCxt);
2,476!
3914
  SAlterLocalStmt* pStmt = NULL;
2,476✔
3915
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
2,476✔
3916
  CHECK_MAKE_NODE(pStmt);
2,476!
3917
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
2,476✔
3918
  if (NULL != pValue) {
2,476✔
3919
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
2,397✔
3920
  }
3921
  return (SNode*)pStmt;
2,476✔
3922
_err:
×
3923
  return NULL;
×
3924
}
3925

3926
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
127,159✔
3927
  CHECK_PARSER_STATUS(pCxt);
127,159!
3928
  SExplainOptions* pOptions = NULL;
127,159✔
3929
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
127,159✔
3930
  CHECK_MAKE_NODE(pOptions);
127,159!
3931
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
127,159✔
3932
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
127,159✔
3933
  return (SNode*)pOptions;
127,159✔
3934
_err:
×
3935
  return NULL;
×
3936
}
3937

3938
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
33,757✔
3939
  CHECK_PARSER_STATUS(pCxt);
33,757!
3940
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
33,757✔
3941
  return pOptions;
33,757✔
3942
_err:
×
3943
  return NULL;
×
3944
}
3945

3946
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
12,245✔
3947
  CHECK_PARSER_STATUS(pCxt);
12,245!
3948
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
12,245✔
3949
  return pOptions;
12,245✔
3950
_err:
×
3951
  return NULL;
×
3952
}
3953

3954
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
126,247✔
3955
  CHECK_PARSER_STATUS(pCxt);
126,247!
3956
  SExplainStmt* pStmt = NULL;
126,247✔
3957
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
126,247✔
3958
  CHECK_MAKE_NODE(pStmt);
126,247!
3959
  pStmt->analyze = analyze;
126,247✔
3960
  pStmt->pOptions = (SExplainOptions*)pOptions;
126,247✔
3961
  pStmt->pQuery = pQuery;
126,247✔
3962
  return (SNode*)pStmt;
126,247✔
3963
_err:
×
3964
  nodesDestroyNode(pOptions);
×
3965
  nodesDestroyNode(pQuery);
×
3966
  return NULL;
×
3967
}
3968

3969
SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
3,796✔
3970
  CHECK_PARSER_STATUS(pCxt);
3,796!
3971
  SDescribeStmt* pStmt = NULL;
3,796✔
3972
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT, (SNode**)&pStmt);
3,796✔
3973
  CHECK_MAKE_NODE(pStmt);
3,796!
3974
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
3,796✔
3975
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
3,796✔
3976
  nodesDestroyNode(pRealTable);
3,796✔
3977
  return (SNode*)pStmt;
3,796✔
3978
_err:
×
3979
  nodesDestroyNode(pRealTable);
×
3980
  return NULL;
×
3981
}
3982

3983
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
7,953✔
3984
  CHECK_PARSER_STATUS(pCxt);
7,953!
3985
  SNode* pStmt = NULL;
7,953✔
3986
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
7,953✔
3987
  CHECK_MAKE_NODE(pStmt);
7,953!
3988
  return pStmt;
7,953✔
3989
_err:
×
3990
  return NULL;
×
3991
}
3992

3993
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
176✔
3994
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
176!
3995
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
117✔
3996
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
59!
3997
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
59✔
3998
  } else {
3999
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4000
                                            "udf programming language supports c and python");
4001
  }
4002
  return pCxt->errCode;
176✔
4003
}
4004

4005
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
176✔
4006
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
4007
                                bool orReplace) {
4008
  CHECK_PARSER_STATUS(pCxt);
176!
4009
  if (pLibPath->n <= 2) {
176!
4010
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4011
    CHECK_PARSER_STATUS(pCxt);
×
4012
  }
4013
  int8_t language = 0;
176✔
4014
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
176✔
4015
  CHECK_PARSER_STATUS(pCxt);
176!
4016
  SCreateFunctionStmt* pStmt = NULL;
176✔
4017
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
176✔
4018
  CHECK_MAKE_NODE(pStmt);
176!
4019
  pStmt->orReplace = orReplace;
176✔
4020
  pStmt->ignoreExists = ignoreExists;
176✔
4021
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
176✔
4022
  pStmt->isAgg = aggFunc;
176✔
4023
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
176!
4024
  pStmt->outputDt = dataType;
176✔
4025
  pStmt->bufSize = bufSize;
176✔
4026
  pStmt->language = language;
176✔
4027
  return (SNode*)pStmt;
176✔
4028
_err:
×
4029
  return NULL;
×
4030
}
4031

4032
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
86✔
4033
  CHECK_PARSER_STATUS(pCxt);
86!
4034
  SDropFunctionStmt* pStmt = NULL;
86✔
4035
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
86✔
4036
  CHECK_MAKE_NODE(pStmt);
86!
4037
  pStmt->ignoreNotExists = ignoreNotExists;
86✔
4038
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
86✔
4039
  return (SNode*)pStmt;
86✔
4040
_err:
×
4041
  return NULL;
×
4042
}
4043

4044
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
527✔
4045
  SCreateViewStmt* pStmt = NULL;
527✔
4046
  CHECK_PARSER_STATUS(pCxt);
527!
4047
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
527✔
4048
  CHECK_MAKE_NODE(pStmt);
527!
4049
  int32_t i = pAs->n;
527✔
4050
  while (isspace(*(pAs->z + i))) {
1,054✔
4051
    ++i;
527✔
4052
  }
4053
  pStmt->pQuerySql = tstrdup(pAs->z + i);
527✔
4054
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
527!
4055
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
527✔
4056
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
527✔
4057
  nodesDestroyNode(pView);
527✔
4058
  pStmt->orReplace = orReplace;
527✔
4059
  pStmt->pQuery = pQuery;
527✔
4060
  return (SNode*)pStmt;
527✔
4061
_err:
×
4062
  nodesDestroyNode(pView);
×
4063
  nodesDestroyNode(pQuery);
×
4064
  nodesDestroyNode((SNode*)pStmt);
×
4065
  return NULL;
×
4066
}
4067

4068
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
415✔
4069
  CHECK_PARSER_STATUS(pCxt);
415✔
4070
  SDropViewStmt* pStmt = NULL;
413✔
4071
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
413✔
4072
  CHECK_MAKE_NODE(pStmt);
413!
4073
  pStmt->ignoreNotExists = ignoreNotExists;
413✔
4074
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
413✔
4075
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
413✔
4076
  nodesDestroyNode(pView);
413✔
4077
  return (SNode*)pStmt;
413✔
4078
_err:
2✔
4079
  nodesDestroyNode(pView);
2✔
4080
  return NULL;
2✔
4081
}
4082

4083
SNode* createStreamOptions(SAstCreateContext* pCxt) {
1,288✔
4084
  CHECK_PARSER_STATUS(pCxt);
1,288!
4085
  SStreamOptions* pOptions = NULL;
1,288✔
4086
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OPTIONS, (SNode**)&pOptions);
1,288✔
4087
  CHECK_MAKE_NODE(pOptions);
1,288!
4088
  pOptions->triggerType = STREAM_TRIGGER_WINDOW_CLOSE;
1,288✔
4089
  pOptions->fillHistory = STREAM_DEFAULT_FILL_HISTORY;
1,288✔
4090
  pOptions->ignoreExpired = STREAM_DEFAULT_IGNORE_EXPIRED;
1,288✔
4091
  pOptions->ignoreUpdate = STREAM_DEFAULT_IGNORE_UPDATE;
1,288✔
4092
  pOptions->runHistoryAsync = false;
1,288✔
4093
  return (SNode*)pOptions;
1,288✔
4094
_err:
×
4095
  return NULL;
×
4096
}
4097

4098
static int8_t getTriggerType(uint32_t tokenType) {
1,128✔
4099
  switch (tokenType) {
1,128!
4100
    case TK_AT_ONCE:
848✔
4101
      return STREAM_TRIGGER_AT_ONCE;
848✔
4102
    case TK_WINDOW_CLOSE:
78✔
4103
      return STREAM_TRIGGER_WINDOW_CLOSE;
78✔
4104
    case TK_MAX_DELAY:
66✔
4105
      return STREAM_TRIGGER_MAX_DELAY;
66✔
4106
    case TK_FORCE_WINDOW_CLOSE:
129✔
4107
      return STREAM_TRIGGER_FORCE_WINDOW_CLOSE;
129✔
4108
    case TK_CONTINUOUS_WINDOW_CLOSE:
7✔
4109
      return STREAM_TRIGGER_CONTINUOUS_WINDOW_CLOSE;
7✔
4110
    default:
×
4111
      break;
×
4112
  }
4113
  return STREAM_TRIGGER_WINDOW_CLOSE;
×
4114
}
4115

4116
SNode* setStreamOptions(SAstCreateContext* pCxt, SNode* pOptions, EStreamOptionsSetFlag setflag, SToken* pToken,
3,568✔
4117
                        SNode* pNode, bool runHistoryAsync) {
4118
  SStreamOptions* pStreamOptions = (SStreamOptions*)pOptions;
3,568✔
4119
  if (BIT_FLAG_TEST_MASK(setflag, pStreamOptions->setFlag)) {
3,568!
4120
    pCxt->errCode =
×
4121
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream options each item is only set once");
×
4122
    return pOptions;
×
4123
  }
4124

4125
  switch (setflag) {
3,568!
4126
    case SOPT_TRIGGER_TYPE_SET:
1,128✔
4127
      pStreamOptions->triggerType = getTriggerType(pToken->type);
1,128✔
4128
      if (STREAM_TRIGGER_MAX_DELAY == pStreamOptions->triggerType) {
1,128✔
4129
        pStreamOptions->pDelay = pNode;
66✔
4130
      }
4131
      if (STREAM_TRIGGER_CONTINUOUS_WINDOW_CLOSE == pStreamOptions->triggerType) {
1,128✔
4132
        pStreamOptions->pRecInterval = pNode;
7✔
4133
      }
4134
      break;
1,128✔
4135
    case SOPT_WATERMARK_SET:
101✔
4136
      pStreamOptions->pWatermark = pNode;
101✔
4137
      break;
101✔
4138
    case SOPT_DELETE_MARK_SET:
4✔
4139
      pStreamOptions->pDeleteMark = pNode;
4✔
4140
      break;
4✔
4141
    case SOPT_FILL_HISTORY_SET:
345✔
4142
      pStreamOptions->fillHistory = taosStr2Int8(pToken->z, NULL, 10);
345✔
4143
      break;
345✔
4144
    case SOPT_IGNORE_EXPIRED_SET:
1,008✔
4145
      pStreamOptions->ignoreExpired = taosStr2Int8(pToken->z, NULL, 10);
1,008✔
4146
      break;
1,008✔
4147
    case SOPT_IGNORE_UPDATE_SET:
982✔
4148
      pStreamOptions->ignoreUpdate = taosStr2Int8(pToken->z, NULL, 10);
982✔
4149
      break;
982✔
4150
    default:
×
4151
      break;
×
4152
  }
4153
  BIT_FLAG_SET_MASK(pStreamOptions->setFlag, setflag);
3,568✔
4154
  pStreamOptions->runHistoryAsync = runHistoryAsync;
3,568✔
4155
  return pOptions;
3,568✔
4156
}
4157

4158
static bool validateNotifyUrl(const char* url) {
×
4159
  const char* prefix[] = {"http://", "https://", "ws://", "wss://"};
×
4160
  const char* host = NULL;
×
4161

4162
  if (!url || *url == '\0') return false;
×
4163

4164
  for (int32_t i = 0; i < ARRAY_SIZE(prefix); ++i) {
×
4165
    if (taosStrncasecmp(url, prefix[i], strlen(prefix[i])) == 0) {
×
4166
      host = url + strlen(prefix[i]);
×
4167
      break;
×
4168
    }
4169
  }
4170

4171
  return (host != NULL) && (*host != '\0') && (*host != '/');
×
4172
}
4173

4174
SNode* createStreamNotifyOptions(SAstCreateContext* pCxt, SNodeList* pAddrUrls, SNodeList* pEventTypes) {
×
4175
  SNode*                 pNode = NULL;
×
4176
  EStreamNotifyEventType eventTypes = 0;
×
4177
  const char*            eWindowOpenStr = "WINDOW_OPEN";
×
4178
  const char*            eWindowCloseStr = "WINDOW_CLOSE";
×
4179

4180
  CHECK_PARSER_STATUS(pCxt);
×
4181

4182
  if (LIST_LENGTH(pAddrUrls) == 0) {
×
4183
    pCxt->errCode =
×
4184
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "notification address cannot be empty");
×
4185
    goto _err;
×
4186
  }
4187

4188
  FOREACH(pNode, pAddrUrls) {
×
4189
    char* url = ((SValueNode*)pNode)->literal;
×
4190
    if (strlen(url) >= TSDB_STREAM_NOTIFY_URL_LEN) {
×
4191
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4192
                                              "notification address \"%s\" exceed maximum length %d", url,
4193
                                              TSDB_STREAM_NOTIFY_URL_LEN);
4194
      goto _err;
×
4195
    }
4196
    if (!validateNotifyUrl(url)) {
×
4197
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4198
                                              "invalid notification address \"%s\"", url);
4199
      goto _err;
×
4200
    }
4201
  }
4202

4203
  if (LIST_LENGTH(pEventTypes) == 0) {
×
4204
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4205
                                            "event types must be specified for notification");
4206
    goto _err;
×
4207
  }
4208

4209
  FOREACH(pNode, pEventTypes) {
×
4210
    char* eventStr = ((SValueNode*)pNode)->literal;
×
4211
    if (taosStrncasecmp(eventStr, eWindowOpenStr, strlen(eWindowOpenStr) + 1) == 0) {
×
4212
      BIT_FLAG_SET_MASK(eventTypes, SNOTIFY_EVENT_WINDOW_OPEN);
×
4213
    } else if (taosStrncasecmp(eventStr, eWindowCloseStr, strlen(eWindowCloseStr) + 1) == 0) {
×
4214
      BIT_FLAG_SET_MASK(eventTypes, SNOTIFY_EVENT_WINDOW_CLOSE);
×
4215
    } else {
4216
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4217
                                              "invalid event type '%s' for notification", eventStr);
4218
      goto _err;
×
4219
    }
4220
  }
4221

4222
  SStreamNotifyOptions* pNotifyOptions = NULL;
×
4223
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_NOTIFY_OPTIONS, (SNode**)&pNotifyOptions);
×
4224
  CHECK_MAKE_NODE(pNotifyOptions);
×
4225
  pNotifyOptions->pAddrUrls = pAddrUrls;
×
4226
  pNotifyOptions->eventTypes = eventTypes;
×
4227
  pNotifyOptions->errorHandle = SNOTIFY_ERROR_HANDLE_PAUSE;
×
4228
  pNotifyOptions->notifyHistory = false;
×
4229
  nodesDestroyList(pEventTypes);
×
4230
  return (SNode*)pNotifyOptions;
×
4231
_err:
×
4232
  nodesDestroyList(pAddrUrls);
×
4233
  nodesDestroyList(pEventTypes);
×
4234
  return NULL;
×
4235
}
4236

4237
SNode* setStreamNotifyOptions(SAstCreateContext* pCxt, SNode* pNode, EStreamNotifyOptionSetFlag setFlag,
×
4238
                              SToken* pToken) {
4239
  CHECK_PARSER_STATUS(pCxt);
×
4240

4241
  SStreamNotifyOptions* pNotifyOption = (SStreamNotifyOptions*)pNode;
×
4242
  if (BIT_FLAG_TEST_MASK(pNotifyOption->setFlag, setFlag)) {
×
4243
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4244
                                         "stream notify options each item can only be set once");
4245
    goto _err;
×
4246
  }
4247
  switch (setFlag) {
×
4248
    case SNOTIFY_OPT_ERROR_HANDLE_SET:
×
4249
      pNotifyOption->errorHandle = (pToken->type == TK_DROP) ? SNOTIFY_ERROR_HANDLE_DROP : SNOTIFY_ERROR_HANDLE_PAUSE;
×
4250
      break;
×
4251
    case SNOTIFY_OPT_NOTIFY_HISTORY_SET:
×
4252
      pNotifyOption->notifyHistory = taosStr2Int8(pToken->z, NULL, 10);
×
4253
      break;
×
4254
    default:
×
4255
      break;
×
4256
  }
4257
  BIT_FLAG_SET_MASK(pNotifyOption->setFlag, setFlag);
×
4258
  return pNode;
×
4259
_err:
×
4260
  nodesDestroyNode(pNode);
×
4261
  return NULL;
×
4262
}
4263

4264
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pStreamName, SNode* pRealTable,
1,255✔
4265
                              SNode* pOptions, SNodeList* pTags, SNode* pSubtable, SNode* pQuery, SNodeList* pCols,
4266
                              SNode* pNotifyOptions) {
4267
  CHECK_PARSER_STATUS(pCxt);
1,255!
4268
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
1,255✔
4269
  SCreateStreamStmt* pStmt = NULL;
1,254✔
4270
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
1,254✔
4271
  CHECK_MAKE_NODE(pStmt);
1,254!
4272
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1,254✔
4273
  tstrncpy(pStmt->targetDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
1,254✔
4274
  tstrncpy(pStmt->targetTabName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
1,254✔
4275
  nodesDestroyNode(pRealTable);
1,254✔
4276
  pStmt->ignoreExists = ignoreExists;
1,254✔
4277
  pStmt->pOptions = (SStreamOptions*)pOptions;
1,254✔
4278
  pStmt->pQuery = pQuery;
1,254✔
4279
  pStmt->pTags = pTags;
1,254✔
4280
  pStmt->pSubtable = pSubtable;
1,254✔
4281
  pStmt->pCols = pCols;
1,254✔
4282
  pStmt->pNotifyOptions = (SStreamNotifyOptions*)pNotifyOptions;
1,254✔
4283
  return (SNode*)pStmt;
1,254✔
4284
_err:
1✔
4285
  nodesDestroyNode(pRealTable);
1✔
4286
  nodesDestroyNode(pQuery);
1✔
4287
  nodesDestroyNode(pOptions);
1✔
4288
  nodesDestroyList(pTags);
1✔
4289
  nodesDestroyNode(pSubtable);
1✔
4290
  nodesDestroyList(pCols);
1✔
4291
  nodesDestroyNode(pNotifyOptions);
1✔
4292
  return NULL;
1✔
4293
}
4294

4295
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
633✔
4296
  CHECK_PARSER_STATUS(pCxt);
633!
4297
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
633!
4298
  SDropStreamStmt* pStmt = NULL;
633✔
4299
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
633✔
4300
  CHECK_MAKE_NODE(pStmt);
633!
4301
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
633✔
4302
  pStmt->ignoreNotExists = ignoreNotExists;
633✔
4303
  return (SNode*)pStmt;
633✔
4304
_err:
×
4305
  return NULL;
×
4306
}
4307

4308
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
38✔
4309
  CHECK_PARSER_STATUS(pCxt);
38!
4310
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
38!
4311
  SPauseStreamStmt* pStmt = NULL;
38✔
4312
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
38✔
4313
  CHECK_MAKE_NODE(pStmt);
38!
4314
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
38✔
4315
  pStmt->ignoreNotExists = ignoreNotExists;
38✔
4316
  return (SNode*)pStmt;
38✔
4317
_err:
×
4318
  return NULL;
×
4319
}
4320

4321
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated,
37✔
4322
                              SToken* pStreamName) {
4323
  CHECK_PARSER_STATUS(pCxt);
37!
4324
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
37!
4325
  SResumeStreamStmt* pStmt = NULL;
37✔
4326
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
37✔
4327
  CHECK_MAKE_NODE(pStmt);
37!
4328
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
37✔
4329
  pStmt->ignoreNotExists = ignoreNotExists;
37✔
4330
  pStmt->ignoreUntreated = ignoreUntreated;
37✔
4331
  return (SNode*)pStmt;
37✔
4332
_err:
×
4333
  return NULL;
×
4334
}
4335

4336
SNode* createResetStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
×
4337
  CHECK_PARSER_STATUS(pCxt);
×
4338
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
×
4339
  SPauseStreamStmt* pStmt = NULL;
×
4340
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_STREAM_STMT, (SNode**)&pStmt);
×
4341
  CHECK_MAKE_NODE(pStmt);
×
4342
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
×
4343
  pStmt->ignoreNotExists = ignoreNotExists;
×
4344
  return (SNode*)pStmt;
×
4345
_err:
×
4346
  return NULL;
×
4347
}
4348

4349
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
5✔
4350
  CHECK_PARSER_STATUS(pCxt);
5!
4351
  SKillStmt* pStmt = NULL;
5✔
4352
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
5✔
4353
  CHECK_MAKE_NODE(pStmt);
5!
4354
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
5✔
4355
  return (SNode*)pStmt;
5✔
4356
_err:
×
4357
  return NULL;
×
4358
}
4359

4360
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
×
4361
  CHECK_PARSER_STATUS(pCxt);
×
4362
  SKillQueryStmt* pStmt = NULL;
×
4363
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
×
4364
  CHECK_MAKE_NODE(pStmt);
×
4365
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
×
4366
  return (SNode*)pStmt;
×
4367
_err:
×
4368
  return NULL;
×
4369
}
4370

4371
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
22✔
4372
  CHECK_PARSER_STATUS(pCxt);
22!
4373
  SBalanceVgroupStmt* pStmt = NULL;
22✔
4374
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
22✔
4375
  CHECK_MAKE_NODE(pStmt);
22!
4376
  return (SNode*)pStmt;
22✔
4377
_err:
×
4378
  return NULL;
×
4379
}
4380

4381
SNode* createAssignLeaderStmt(SAstCreateContext* pCxt) {
1✔
4382
  CHECK_PARSER_STATUS(pCxt);
1!
4383
  SAssignLeaderStmt* pStmt = NULL;
1✔
4384
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ASSIGN_LEADER_STMT, (SNode**)&pStmt);
1✔
4385
  CHECK_MAKE_NODE(pStmt);
1!
4386
  return (SNode*)pStmt;
1✔
4387
_err:
×
4388
  return NULL;
×
4389
}
4390

4391
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
12✔
4392
  CHECK_PARSER_STATUS(pCxt);
12!
4393
  SBalanceVgroupLeaderStmt* pStmt = NULL;
12✔
4394
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
12✔
4395
  CHECK_MAKE_NODE(pStmt);
12!
4396
  if (NULL != pVgId && NULL != pVgId->z) {
12!
4397
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
1✔
4398
  }
4399
  return (SNode*)pStmt;
12✔
4400
_err:
×
4401
  return NULL;
×
4402
}
4403

4404
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
1✔
4405
  CHECK_PARSER_STATUS(pCxt);
1!
4406
  SBalanceVgroupLeaderStmt* pStmt = NULL;
1✔
4407
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
1✔
4408
  CHECK_MAKE_NODE(pStmt);
1!
4409
  if (NULL != pDbName) {
1!
4410
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1✔
4411
  }
4412
  return (SNode*)pStmt;
1✔
4413
_err:
×
4414
  return NULL;
×
4415
}
4416

4417
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
4✔
4418
  CHECK_PARSER_STATUS(pCxt);
4!
4419
  SMergeVgroupStmt* pStmt = NULL;
4✔
4420
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
4✔
4421
  CHECK_MAKE_NODE(pStmt);
4!
4422
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
4✔
4423
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
4✔
4424
  return (SNode*)pStmt;
4✔
4425
_err:
×
4426
  return NULL;
×
4427
}
4428

4429
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
87✔
4430
  CHECK_PARSER_STATUS(pCxt);
87!
4431
  SRedistributeVgroupStmt* pStmt = NULL;
87✔
4432
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
87✔
4433
  CHECK_MAKE_NODE(pStmt);
87!
4434
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
87✔
4435
  pStmt->pDnodes = pDnodes;
87✔
4436
  return (SNode*)pStmt;
87✔
4437
_err:
×
4438
  nodesDestroyList(pDnodes);
×
4439
  return NULL;
×
4440
}
4441

4442
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
61✔
4443
  CHECK_PARSER_STATUS(pCxt);
61!
4444
  SSplitVgroupStmt* pStmt = NULL;
61✔
4445
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
61✔
4446
  CHECK_MAKE_NODE(pStmt);
61!
4447
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
61✔
4448
  return (SNode*)pStmt;
61✔
4449
_err:
×
4450
  return NULL;
×
4451
}
4452

4453
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
4454
  CHECK_PARSER_STATUS(pCxt);
×
4455
  SNode* pStmt = NULL;
×
4456
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
4457
  CHECK_MAKE_NODE(pStmt);
×
4458
  return pStmt;
×
4459
_err:
×
4460
  return NULL;
×
4461
}
4462

4463
SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
1,444✔
4464
                       SNode* pTagCond) {
4465
  CHECK_PARSER_STATUS(pCxt);
1,444!
4466
  CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
1,444!
4467
  CHECK_NAME(checkUserName(pCxt, pUserName));
1,444!
4468
  CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
1,444!
4469
  SGrantStmt* pStmt = NULL;
1,444✔
4470
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
1,444✔
4471
  CHECK_MAKE_NODE(pStmt);
1,444!
4472
  pStmt->privileges = privileges;
1,444✔
4473
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
1,444✔
4474
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
1,444✔
4475
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
1,122✔
4476
  }
4477
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1,444✔
4478
  pStmt->pTagCond = pTagCond;
1,444✔
4479
  return (SNode*)pStmt;
1,444✔
4480
_err:
×
4481
  nodesDestroyNode(pTagCond);
×
4482
  return NULL;
×
4483
}
4484

4485
SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
540✔
4486
                        SNode* pTagCond) {
4487
  CHECK_PARSER_STATUS(pCxt);
540!
4488
  CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
540!
4489
  CHECK_NAME(checkUserName(pCxt, pUserName));
540!
4490
  CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
540!
4491
  SRevokeStmt* pStmt = NULL;
540✔
4492
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
540✔
4493
  CHECK_MAKE_NODE(pStmt);
540!
4494
  pStmt->privileges = privileges;
540✔
4495
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
540✔
4496
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
540✔
4497
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
265✔
4498
  }
4499
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
540✔
4500
  pStmt->pTagCond = pTagCond;
540✔
4501
  return (SNode*)pStmt;
540✔
4502
_err:
×
4503
  nodesDestroyNode(pTagCond);
×
4504
  return NULL;
×
4505
}
4506

4507
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
26,841✔
4508
  SFunctionNode* pFunc = NULL;
26,841✔
4509
  CHECK_PARSER_STATUS(pCxt);
26,841!
4510
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
26,841✔
4511
  CHECK_MAKE_NODE(pFunc);
26,841!
4512
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
26,841✔
4513
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
26,841✔
4514
  CHECK_MAKE_NODE(pCol);
26,841!
4515
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
26,841✔
4516
  CHECK_PARSER_STATUS(pCxt);
26,841!
4517
  return (SNode*)pFunc;
26,841✔
4518
_err:
×
4519
  nodesDestroyNode((SNode*)pFunc);
×
4520
  return NULL;
×
4521
}
4522

4523
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
8,947✔
4524
  SDeleteStmt* pStmt = NULL;
8,947✔
4525
  CHECK_PARSER_STATUS(pCxt);
8,947!
4526
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
8,947✔
4527
  CHECK_MAKE_NODE(pStmt);
8,947!
4528
  pStmt->pFromTable = pTable;
8,947✔
4529
  pStmt->pWhere = pWhere;
8,947✔
4530
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
8,947✔
4531
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
8,947✔
4532
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
8,947✔
4533
  CHECK_MAKE_NODE(pStmt->pCountFunc);
8,947!
4534
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
8,947!
4535
  CHECK_MAKE_NODE(pStmt->pLastFunc);
8,947!
4536
  return (SNode*)pStmt;
8,947✔
4537
_err:
×
4538
  nodesDestroyNode((SNode*)pStmt);
×
4539
  nodesDestroyNode(pTable);
×
4540
  nodesDestroyNode(pWhere);
×
4541
  return NULL;
×
4542
}
4543

4544
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
232✔
4545
  CHECK_PARSER_STATUS(pCxt);
232!
4546
  SInsertStmt* pStmt = NULL;
232✔
4547
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
232✔
4548
  CHECK_MAKE_NODE(pStmt);
232!
4549
  pStmt->pTable = pTable;
232✔
4550
  pStmt->pCols = pCols;
232✔
4551
  pStmt->pQuery = pQuery;
232✔
4552
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
232✔
4553
    tstrncpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
231✔
4554
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
1!
4555
    tstrncpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
1✔
4556
  }
4557
  return (SNode*)pStmt;
232✔
4558
_err:
×
4559
  nodesDestroyNode(pTable);
×
4560
  nodesDestroyNode(pQuery);
×
4561
  nodesDestroyList(pCols);
×
4562
  return NULL;
×
4563
}
4564

4565
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
325✔
4566
                            SNode* pRealTable, SNode* pInterval) {
4567
  SCreateTSMAStmt* pStmt = NULL;
325✔
4568
  CHECK_PARSER_STATUS(pCxt);
325!
4569
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
325!
4570
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
325✔
4571
  CHECK_MAKE_NODE(pStmt);
325!
4572

4573
  pStmt->ignoreExists = ignoreExists;
325✔
4574
  if (!pOptions) {
325✔
4575
    // recursive tsma
4576
    pStmt->pOptions = NULL;
106✔
4577
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
106✔
4578
    CHECK_MAKE_NODE(pStmt->pOptions);
106!
4579
    pStmt->pOptions->recursiveTsma = true;
106✔
4580
  } else {
4581
    pStmt->pOptions = (STSMAOptions*)pOptions;
219✔
4582
  }
4583
  pStmt->pOptions->pInterval = pInterval;
325✔
4584
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
325✔
4585

4586
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
325✔
4587
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
325✔
4588
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
325✔
4589
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
325✔
4590
  nodesDestroyNode(pRealTable);
325✔
4591

4592
  return (SNode*)pStmt;
325✔
4593
_err:
×
4594
  nodesDestroyNode((SNode*)pStmt);
×
4595
  nodesDestroyNode(pOptions);
×
4596
  nodesDestroyNode(pRealTable);
×
4597
  nodesDestroyNode(pInterval);
×
4598
  return NULL;
×
4599
}
4600

4601
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
239✔
4602
  CHECK_PARSER_STATUS(pCxt);
239!
4603
  STSMAOptions* pOptions = NULL;
239✔
4604
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
239✔
4605
  CHECK_MAKE_NODE(pOptions);
239!
4606
  pOptions->pFuncs = pFuncs;
239✔
4607
  return (SNode*)pOptions;
239✔
4608
_err:
×
4609
  nodesDestroyList(pFuncs);
×
4610
  return NULL;
×
4611
}
4612

4613
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
4614
  CHECK_PARSER_STATUS(pCxt);
×
4615
  STSMAOptions* pOptions = NULL;
×
4616
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
4617
  CHECK_MAKE_NODE(pOptions);
×
4618
  return (SNode*)pOptions;
×
4619
_err:
×
4620
  return NULL;
×
4621
}
4622

4623
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
145✔
4624
  CHECK_PARSER_STATUS(pCxt);
145✔
4625
  SDropTSMAStmt* pStmt = NULL;
144✔
4626
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
144✔
4627
  CHECK_MAKE_NODE(pStmt);
144!
4628

4629
  pStmt->ignoreNotExists = ignoreNotExists;
144✔
4630
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
144✔
4631

4632
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
144✔
4633
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
144✔
4634

4635
  nodesDestroyNode(pRealTable);
144✔
4636
  return (SNode*)pStmt;
144✔
4637
_err:
1✔
4638
  nodesDestroyNode(pRealTable);
1✔
4639
  return NULL;
1✔
4640
}
4641

4642
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
2✔
4643
  CHECK_PARSER_STATUS(pCxt);
2!
4644

4645
  SShowStmt* pStmt = NULL;
2✔
4646
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
2✔
4647
  CHECK_MAKE_NODE(pStmt);
2!
4648

4649
  pStmt->pDbName = dbName;
2✔
4650
  return (SNode*)pStmt;
2✔
4651
_err:
×
4652
  nodesDestroyNode(dbName);
×
4653
  return NULL;
×
4654
}
4655
SNode* createShowDiskUsageStmt(SAstCreateContext* pCxt, SNode* dbName, ENodeType type) {
4✔
4656
  CHECK_PARSER_STATUS(pCxt);
4!
4657
  if (NULL == dbName) {
4!
4658
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
4659
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4660
    CHECK_PARSER_STATUS(pCxt);
×
4661
  }
4662

4663
  SShowStmt* pStmt = NULL;
4✔
4664
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
4✔
4665
  CHECK_MAKE_NODE(pStmt);
4!
4666

4667
  pStmt->pDbName = dbName;
4✔
4668
  return (SNode*)pStmt;
4✔
4669
_err:
×
4670
  nodesDestroyNode(dbName);
×
4671
  return NULL;
×
4672
}
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