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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

web-flow
Merge pull request #28973 from taosdata/merge/mainto3.0

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

71.21
/source/libs/parser/src/parAstCreater.c
1

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

19
#include "parAst.h"
20
#include "parUtil.h"
21
#include "tglobal.h"
22
#include "ttime.h"
23

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

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

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

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

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

61
SToken nil_token = {.type = TK_NK_NIL, .n = 0, .z = NULL};
62

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

75
static void trimEscape(SToken* pName) {
16,552,650✔
76
  // todo need to deal with `ioo``ii` -> ioo`ii
77
  if (NULL != pName && pName->n > 1 && '`' == pName->z[0]) {
16,552,650✔
78
    pName->z += 1;
105,206✔
79
    pName->n -= 2;
105,206✔
80
  }
81
}
16,552,650✔
82

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

97
static bool invalidPassword(const char* pPassword) {
179✔
98
  regex_t regex;
99

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

104
  /* Execute regular expression */
105
  int32_t res = regexec(&regex, pPassword, 0, NULL, 0);
179✔
106
  regfree(&regex);
179✔
107
  return 0 == res;
179✔
108
}
109

110
static bool checkPassword(SAstCreateContext* pCxt, const SToken* pPasswordToken, char* pPassword) {
179✔
111
  if (NULL == pPasswordToken) {
179!
112
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
113
  } else if (pPasswordToken->n >= (TSDB_USET_PASSWORD_LEN + 2)) {
179!
114
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
115
  } else {
116
    strncpy(pPassword, pPasswordToken->z, pPasswordToken->n);
179✔
117
    (void)strdequote(pPassword);
179✔
118
    if (strtrim(pPassword) <= 0) {
179!
119
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_EMPTY);
×
120
    } else if (invalidPassword(pPassword)) {
179!
121
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
×
122
    }
123
  }
124
  return TSDB_CODE_SUCCESS == pCxt->errCode;
179✔
125
}
126

127
static int32_t parsePort(SAstCreateContext* pCxt, const char* p, int32_t* pPort) {
512✔
128
  *pPort = taosStr2Int32(p, NULL, 10);
512✔
129
  if (*pPort >= UINT16_MAX || *pPort <= 0) {
512!
130
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
×
131
  }
132
  return TSDB_CODE_SUCCESS;
512✔
133
}
134

135
static int32_t parseEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
512✔
136
  if (pEp->n >= (NULL == pPort ? (TSDB_FQDN_LEN + 1 + 5) : TSDB_FQDN_LEN)) {  // format 'fqdn:port' or 'fqdn'
512!
137
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
×
138
  }
139

140
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
512✔
141
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
512✔
142
  (void)strdequote(ep);
512✔
143
  (void)strtrim(ep);
512✔
144
  if (NULL == pPort) {
512✔
145
    strcpy(pFqdn, ep);
158✔
146
    return TSDB_CODE_SUCCESS;
158✔
147
  }
148
  char* pColon = strchr(ep, ':');
354✔
149
  if (NULL == pColon) {
354!
150
    *pPort = tsServerPort;
×
151
    strcpy(pFqdn, ep);
×
152
    return TSDB_CODE_SUCCESS;
×
153
  }
154
  strncpy(pFqdn, ep, pColon - ep);
354✔
155
  return parsePort(pCxt, pColon + 1, pPort);
354✔
156
}
157

158
static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, const SToken* pPortToken, char* pFqdn,
512✔
159
                                  int32_t* pPort) {
160
  if (NULL == pEp) {
512!
161
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
162
    return false;
×
163
  }
164

165
  if (NULL != pPortToken) {
512✔
166
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
158✔
167
  }
168

169
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
512!
170
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
512✔
171
  }
172

173
  return TSDB_CODE_SUCCESS == pCxt->errCode;
512✔
174
}
175

176
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
1,463,356✔
177
  if (NULL == pDbName) {
1,463,356✔
178
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
597,122!
179
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
21✔
180
    }
181
  } else {
182
    trimEscape(pDbName);
866,234✔
183
    if (pDbName->n >= TSDB_DB_NAME_LEN || pDbName->n == 0) {
866,252✔
184
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
12✔
185
    }
186
  }
187
  return TSDB_CODE_SUCCESS == pCxt->errCode;
1,463,366✔
188
}
189

190
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
8,755,662✔
191
  trimEscape(pTableName);
8,755,662✔
192
  if (NULL != pTableName && pTableName->type != TK_NK_NIL &&
8,755,658✔
193
      (pTableName->n >= TSDB_TABLE_NAME_LEN || pTableName->n == 0)) {
2,872,481✔
194
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
23✔
195
    return false;
15✔
196
  }
197
  return true;
8,755,635✔
198
}
199

200
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
6,187,993✔
201
  trimEscape(pColumnName);
6,187,993✔
202
  if (NULL != pColumnName && pColumnName->type != TK_NK_NIL &&
6,187,993!
203
      (pColumnName->n >= TSDB_COL_NAME_LEN || pColumnName->n == 0)) {
6,187,989!
204
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
5✔
205
    return false;
13✔
206
  }
207
  return true;
6,187,988✔
208
}
209

210
static bool checkIndexName(SAstCreateContext* pCxt, SToken* pIndexName) {
37✔
211
  trimEscape(pIndexName);
37✔
212
  if (NULL != pIndexName && pIndexName->n >= TSDB_INDEX_NAME_LEN) {
37!
213
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pIndexName->z);
×
214
    return false;
×
215
  }
216
  return true;
37✔
217
}
218

219
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
978✔
220
  trimEscape(pTopicName);
978✔
221
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN || pTopicName->n == 0) {
978✔
222
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
3✔
223
    return false;
3✔
224
  }
225
  return true;
975✔
226
}
227

228
static bool checkCGroupName(SAstCreateContext* pCxt, SToken* pCGroup) {
5✔
229
  trimEscape(pCGroup);
5✔
230
  if (pCGroup->n >= TSDB_CGROUP_LEN) {
5!
231
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pCGroup->z);
×
232
    return false;
×
233
  }
234
  return true;
5✔
235
}
236

237
static bool checkViewName(SAstCreateContext* pCxt, SToken* pViewName) {
379✔
238
  trimEscape(pViewName);
379✔
239
  if (pViewName->n >= TSDB_VIEW_NAME_LEN || pViewName->n == 0) {
379✔
240
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pViewName->z);
3✔
241
    return false;
3✔
242
  }
243
  return true;
376✔
244
}
245

246
static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
2,593✔
247
  trimEscape(pStreamName);
2,593✔
248
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN || pStreamName->n == 0) {
2,593!
249
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
1✔
250
    return false;
1✔
251
  }
252
  return true;
2,592✔
253
}
254

255
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
135✔
256
  if (NULL == pCommentToken) {
135!
257
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
×
258
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
135✔
259
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
13✔
260
  }
261
  return TSDB_CODE_SUCCESS == pCxt->errCode;
135✔
262
}
263

264
static bool checkTsmaName(SAstCreateContext* pCxt, SToken* pTsmaToken) {
369✔
265
  trimEscape(pTsmaToken);
369✔
266
  if (NULL == pTsmaToken) {
369!
267
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
268
  } else if (pTsmaToken->n >= TSDB_TABLE_NAME_LEN - strlen(TSMA_RES_STB_POSTFIX)) {
369✔
269
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSMA_NAME_TOO_LONG);
8✔
270
  } else if (pTsmaToken->n == 0) {
361!
271
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTsmaToken->z);
×
272
  }
273
  return pCxt->errCode == TSDB_CODE_SUCCESS;
369✔
274
}
275

276
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
6,594,627✔
277
  CHECK_PARSER_STATUS(pCxt);
6,594,627✔
278
  SRawExprNode* target = NULL;
6,594,571✔
279
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
6,594,571✔
280
  CHECK_MAKE_NODE(target);
6,594,587!
281
  target->p = pToken->z;
6,594,587✔
282
  target->n = pToken->n;
6,594,587✔
283
  target->pNode = pNode;
6,594,587✔
284
  return (SNode*)target;
6,594,587✔
285
_err:
56✔
286
  nodesDestroyNode(pNode);
56✔
287
  return NULL;
56✔
288
}
289

290
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
7,179,148✔
291
  CHECK_PARSER_STATUS(pCxt);
7,179,148!
292
  SRawExprNode* target = NULL;
7,179,148✔
293
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
7,179,148✔
294
  CHECK_MAKE_NODE(target);
7,179,185!
295
  target->p = pStart->z;
7,179,185✔
296
  target->n = (pEnd->z + pEnd->n) - pStart->z;
7,179,185✔
297
  target->pNode = pNode;
7,179,185✔
298
  return (SNode*)target;
7,179,185✔
299
_err:
×
300
  nodesDestroyNode(pNode);
×
301
  return NULL;
×
302
}
303

304
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
535,246✔
305
  CHECK_PARSER_STATUS(pCxt);
535,246!
306
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
535,246!
307
    return pNode;
×
308
  }
309
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
535,246✔
310
  return pNode;
535,246✔
311
_err:
×
312
  nodesDestroyNode(pNode);
×
313
  return NULL;
×
314
}
315

316
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
13,764,087✔
317
  CHECK_PARSER_STATUS(pCxt);
13,764,087!
318
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
13,764,087✔
319
  SNode*        pRealizedExpr = pRawExpr->pNode;
13,764,087✔
320
  if (nodesIsExprNode(pRealizedExpr)) {
13,764,087✔
321
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
13,359,370✔
322
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
13,359,370✔
323
      strcpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName);
5,354,633✔
324
      strcpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName);
5,354,633✔
325
    } else if (pRawExpr->isPseudoColumn) {
8,004,737✔
326
      // all pseudo column are translate to function with same name
327
      strcpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName);
431,485✔
328
      strcpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName);
431,485✔
329
    } else {
330
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
7,573,252✔
331

332
      // See TS-3398.
333
      // Len of pRawExpr->p could be larger than len of aliasName[TSDB_COL_NAME_LEN].
334
      // If aliasName is truncated, hash value of aliasName could be the same.
335
      uint64_t hashVal = MurmurHash3_64(pRawExpr->p, pRawExpr->n);
7,573,252✔
336
      sprintf(pExpr->aliasName, "%" PRIu64, hashVal);
7,573,303✔
337
      strncpy(pExpr->userAlias, pRawExpr->p, len);
7,573,303✔
338
      pExpr->userAlias[len] = '\0';
7,573,303✔
339
    }
340
  }
341
  pRawExpr->pNode = NULL;
13,764,090✔
342
  nodesDestroyNode(pNode);
13,764,090✔
343
  return pRealizedExpr;
13,764,091✔
344
_err:
×
345
  nodesDestroyNode(pNode);
×
346
  return NULL;
×
347
}
348

349
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
4,971,851✔
350
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
4,971,851!
351
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
352
    return nil_token;
×
353
  }
354
  SRawExprNode* target = (SRawExprNode*)pNode;
4,971,865✔
355
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
4,971,865✔
356
  return t;
4,971,865✔
357
}
358

359
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
4,089,695✔
360
  CHECK_PARSER_STATUS(pCxt);
4,089,695✔
361
  SNodeList* list = NULL;
4,089,675✔
362
  pCxt->errCode = nodesMakeList(&list);
4,089,675✔
363
  CHECK_MAKE_NODE(list);
4,089,685!
364
  pCxt->errCode = nodesListAppend(list, pNode);
4,089,685✔
365
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
4,089,686!
366
    nodesDestroyList(list);
×
367
    return NULL;
×
368
  }
369
  return list;
4,089,688✔
370
_err:
20✔
371
  nodesDestroyNode(pNode);
20✔
372
  return NULL;
20✔
373
}
374

375
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
7,058,199✔
376
  CHECK_PARSER_STATUS(pCxt);
7,058,199✔
377
  pCxt->errCode = nodesListAppend(pList, pNode);
7,058,192✔
378
  return pList;
7,056,725✔
379
_err:
7✔
380
  nodesDestroyNode(pNode);
7✔
381
  nodesDestroyList(pList);
7✔
382
  return NULL;
7✔
383
}
384

385
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
5,581,291✔
386
  CHECK_PARSER_STATUS(pCxt);
5,581,291!
387
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
5,581,291!
388
    return NULL;
1✔
389
  }
390
  SColumnNode* col = NULL;
5,581,302✔
391
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
5,581,302✔
392
  CHECK_MAKE_NODE(col);
5,581,285!
393
  if (NULL != pTableAlias) {
5,581,285✔
394
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
1,203,702✔
395
  }
396
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
5,581,285✔
397
  return (SNode*)col;
5,581,285✔
398
_err:
×
399
  return NULL;
×
400
}
401

402
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
5,094,505✔
403
  CHECK_PARSER_STATUS(pCxt);
5,094,505!
404
  SValueNode* val = NULL;
5,094,505✔
405
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
5,094,505✔
406
  CHECK_MAKE_NODE(val);
5,089,380!
407
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
5,089,380✔
408
  if (!val->literal) {
5,094,674!
409
    pCxt->errCode = terrno;
×
410
    nodesDestroyNode((SNode*)val);
×
411
    return NULL;
×
412
  }
413
  if (TK_NK_ID != pLiteral->type && TK_TIMEZONE != pLiteral->type &&
5,094,674✔
414
      (IS_VAR_DATA_TYPE(dataType) || TSDB_DATA_TYPE_TIMESTAMP == dataType)) {
4,652,777!
415
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
431,530✔
416
  }
417
  val->node.resType.type = dataType;
5,093,937✔
418
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
5,093,937!
419
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
5,093,937✔
420
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
1✔
421
  }
422
  val->translate = false;
5,093,937✔
423
  return (SNode*)val;
5,093,937✔
424
_err:
×
425
  return NULL;
×
426
}
427

428
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
307,549✔
429
  CHECK_PARSER_STATUS(pCxt);
307,549!
430
  SValueNode* val = NULL;
307,549✔
431
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
307,549✔
432
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
307,539!
UNCOV
433
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
434
    goto _exit;
×
435
  }
436
  if (pLiteral) {
307,542✔
437
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
304,756✔
438
    if (!val->literal) {
304,751!
439
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
440
      goto _exit;
×
441
    }
442
  } else if (pNode) {
2,786!
443
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
2,786✔
444
    if (!nodesIsExprNode(pRawExpr->pNode)) {
2,786!
445
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
446
      goto _exit;
×
447
    }
448
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
2,786✔
449
    if (!val->literal) {
2,786✔
450
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
2✔
451
      goto _exit;
×
452
    }
453
  } else {
454
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
455
    goto _exit;
×
456
  }
457
  if (!val->literal) {
307,535!
458
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
459
    goto _exit;
×
460
  }
461

462
  val->node.resType.type = dataType;
307,535✔
463
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
307,535!
464
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
307,535!
465
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
466
  }
467
_exit:
307,535✔
468
  nodesDestroyNode(pNode);
307,535✔
469
  if (pCxt->errCode != 0) {
307,531!
UNCOV
470
    nodesDestroyNode((SNode*)val);
×
471
    return NULL;
×
472
  }
473
  return (SNode*)val;
307,534✔
474
_err:
×
475
  nodesDestroyNode(pNode);
×
476
  return NULL;
×
477
}
478

479
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
1,291✔
480
                             SNode* pRight) {
481
  SValueNode* val = NULL;
1,291✔
482
  CHECK_PARSER_STATUS(pCxt);
1,291!
483

484
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
1,291✔
485
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
1,291!
486
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
487
    goto _exit;
×
488
  }
489
  if (pLiteral) {
1,291!
490
    if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) {
1,291!
491
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
492
      goto _exit;
×
493
    }
494
  } else {
495
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
496
    goto _exit;
×
497
  }
498

499
  val->node.resType.type = dataType;
1,291✔
500
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
1,291!
501
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
1,291!
502
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
503
  }
504
_exit:
1,291✔
505
  nodesDestroyNode(pLeft);
1,291✔
506
  nodesDestroyNode(pRight);
1,291✔
507
  CHECK_PARSER_STATUS(pCxt);
1,291!
508
  return (SNode*)val;
1,291✔
509
_err:
×
510
  nodesDestroyNode((SNode*)val);
×
511
  nodesDestroyNode(pLeft);
×
512
  nodesDestroyNode(pRight);
×
513
  return NULL;
×
514
}
515

516
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
32,224✔
517
  if (!pHintList) return false;
32,224✔
518
  SNode* pNode;
519
  FOREACH(pNode, pHintList) {
12!
520
    SHintNode* pHint = (SHintNode*)pNode;
12✔
521
    if (pHint->option == hint) {
12!
522
      return true;
12✔
523
    }
524
  }
525
  return false;
×
526
}
527

528
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
32,272✔
529
                       int32_t paramNum) {
530
  void* value = NULL;
32,272✔
531
  switch (opt) {
32,272!
532
    case HINT_SKIP_TSMA:
48✔
533
    case HINT_BATCH_SCAN:
534
    case HINT_NO_BATCH_SCAN: {
535
      if (paramNum > 0) {
48✔
536
        return true;
8✔
537
      }
538
      break;
40✔
539
    }
540
    case HINT_SORT_FOR_GROUP:
488✔
541
      if (paramNum > 0) return true;
488!
542
      if (hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
488✔
543
      break;
484✔
544
    case HINT_PARTITION_FIRST:
16✔
545
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
16!
546
      break;
8✔
547
    case HINT_PARA_TABLES_SORT:
31,720✔
548
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
31,720!
549
      break;
31,720✔
550
    case HINT_SMALLDATA_TS_SORT:
×
551
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
552
      break;
×
553
    case HINT_HASH_JOIN:
×
554
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
×
555
      break;
×
556
    default:
×
557
      return true;
×
558
  }
559

560
  SHintNode* hint = NULL;
32,252✔
561
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
32,252✔
562
  if (!hint) {
32,252!
563
    return true;
×
564
  }
565
  hint->option = opt;
32,252✔
566
  hint->value = value;
32,252✔
567

568
  if (NULL == *ppHintList) {
32,252✔
569
    pCxt->errCode = nodesMakeList(ppHintList);
32,244✔
570
    if (!*ppHintList) {
32,244!
571
      nodesDestroyNode((SNode*)hint);
×
572
      return true;
×
573
    }
574
  }
575

576
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
32,252✔
577
  if (pCxt->errCode) {
32,252!
578
    return true;
×
579
  }
580

581
  return false;
32,252✔
582
}
583

584
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
1,420,072✔
585
  CHECK_PARSER_STATUS(pCxt);
1,420,072!
586
  if (NULL == pLiteral || pLiteral->n <= 5) {
1,420,072✔
587
    return NULL;
1,387,816✔
588
  }
589
  SNodeList* pHintList = NULL;
32,256✔
590
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
32,256✔
591
  if (!hint) return NULL;
32,256!
592
  int32_t     i = 0;
32,256✔
593
  bool        quit = false;
32,256✔
594
  bool        inParamList = false;
32,256✔
595
  bool        lastComma = false;
32,256✔
596
  EHintOption opt = 0;
32,256✔
597
  int32_t     paramNum = 0;
32,256✔
598
  SToken      paramList[10];
599
  while (!quit) {
193,580✔
600
    SToken t0 = {0};
193,556✔
601
    if (hint[i] == 0) {
193,556✔
602
      break;
32,232✔
603
    }
604
    t0.n = tGetToken(&hint[i], &t0.type);
161,324✔
605
    t0.z = hint + i;
161,324✔
606
    i += t0.n;
161,324✔
607

608
    switch (t0.type) {
161,324!
609
      case TK_BATCH_SCAN:
24✔
610
        lastComma = false;
24✔
611
        if (0 != opt || inParamList) {
24!
612
          quit = true;
×
613
          break;
×
614
        }
615
        opt = HINT_BATCH_SCAN;
24✔
616
        break;
24✔
617
      case TK_NO_BATCH_SCAN:
16✔
618
        lastComma = false;
16✔
619
        if (0 != opt || inParamList) {
16!
620
          quit = true;
×
621
          break;
×
622
        }
623
        opt = HINT_NO_BATCH_SCAN;
16✔
624
        break;
16✔
625
      case TK_SORT_FOR_GROUP:
488✔
626
        lastComma = false;
488✔
627
        if (0 != opt || inParamList) {
488!
628
          quit = true;
×
629
          break;
×
630
        }
631
        opt = HINT_SORT_FOR_GROUP;
488✔
632
        break;
488✔
633
      case TK_PARTITION_FIRST:
16✔
634
        lastComma = false;
16✔
635
        if (0 != opt || inParamList) {
16!
636
          quit = true;
×
637
          break;
×
638
        }
639
        opt = HINT_PARTITION_FIRST;
16✔
640
        break;
16✔
641
      case TK_PARA_TABLES_SORT:
31,720✔
642
        lastComma = false;
31,720✔
643
        if (0 != opt || inParamList) {
31,720!
644
          quit = true;
×
645
          break;
×
646
        }
647
        opt = HINT_PARA_TABLES_SORT;
31,720✔
648
        break;
31,720✔
649
      case TK_SMALLDATA_TS_SORT:
×
650
        lastComma = false;
×
651
        if (0 != opt || inParamList) {
×
652
          quit = true;
×
653
          break;
×
654
        }
655
        opt = HINT_SMALLDATA_TS_SORT;
×
656
        break;
×
657
      case TK_HASH_JOIN:
×
658
        lastComma = false;
×
659
        if (0 != opt || inParamList) {
×
660
          quit = true;
×
661
          break;
×
662
        }
663
        opt = HINT_HASH_JOIN;
×
664
        break;
×
665
      case TK_SKIP_TSMA:
8✔
666
        lastComma = false;
8✔
667
        if (0 != opt || inParamList) {
8!
668
          quit = true;
×
669
          break;
×
670
        }
671
        opt = HINT_SKIP_TSMA;
8✔
672
        break;
8✔
673
      case TK_NK_LP:
32,272✔
674
        lastComma = false;
32,272✔
675
        if (0 == opt || inParamList) {
32,272!
676
          quit = true;
×
677
        }
678
        inParamList = true;
32,272✔
679
        break;
32,272✔
680
      case TK_NK_RP:
32,272✔
681
        lastComma = false;
32,272✔
682
        if (0 == opt || !inParamList) {
32,272!
683
          quit = true;
×
684
        } else {
685
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
32,272✔
686
          inParamList = false;
32,272✔
687
          paramNum = 0;
32,272✔
688
          opt = 0;
32,272✔
689
        }
690
        break;
32,272✔
691
      case TK_NK_ID:
12✔
692
        lastComma = false;
12✔
693
        if (0 == opt || !inParamList) {
12!
694
          quit = true;
4✔
695
        } else {
696
          paramList[paramNum++] = t0;
8✔
697
        }
698
        break;
12✔
699
      case TK_NK_COMMA:
8✔
700
        if (lastComma) {
8!
701
          quit = true;
×
702
        }
703
        lastComma = true;
8✔
704
        break;
8✔
705
      case TK_NK_SPACE:
64,488✔
706
        break;
64,488✔
707
      default:
×
708
        lastComma = false;
×
709
        quit = true;
×
710
        break;
×
711
    }
712
  }
713

714
  taosMemoryFree(hint);
32,256✔
715
  return pHintList;
32,256✔
716
_err:
×
717
  return NULL;
×
718
}
719

720
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
2,481✔
721
  trimEscape(pLiteral);
2,481✔
722
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
2,481✔
723
}
724

725
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
147,699✔
726
  CHECK_PARSER_STATUS(pCxt);
147,699!
727
  SValueNode* val = NULL;
147,699✔
728
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
147,699✔
729
  CHECK_MAKE_NODE(val);
147,700!
730
  if (pLiteral->type == TK_NK_STRING) {
147,700✔
731
    // like '100s' or "100d"
732
    // check format: ^[0-9]+[smwbauhdny]$'
733
    if (pLiteral->n < 4) {
172✔
734
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
8✔
735
      return NULL;
8✔
736
    }
737
    char unit = pLiteral->z[pLiteral->n - 2];
164✔
738
    switch (unit) {
164✔
739
      case 'a':
140✔
740
      case 'b':
741
      case 'd':
742
      case 'h':
743
      case 'm':
744
      case 's':
745
      case 'u':
746
      case 'w':
747
      case 'y':
748
      case 'n':
749
        break;
140✔
750
      default:
24✔
751
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
24✔
752
        return NULL;
24✔
753
    }
754
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
512✔
755
      if (!isdigit(pLiteral->z[i])) {
396✔
756
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
24✔
757
        return NULL;
24✔
758
      }
759
    }
760
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
116✔
761
  } else {
762
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
147,528✔
763
  }
764
  if (!val->literal) {
147,644!
765
    nodesDestroyNode((SNode*)val);
×
766
    pCxt->errCode = terrno;
×
767
    return NULL;
×
768
  }
769
  val->flag |= VALUE_FLAG_IS_DURATION;
147,644✔
770
  val->translate = false;
147,644✔
771
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
147,644✔
772
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
147,644✔
773
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
147,644✔
774
  return (SNode*)val;
147,644✔
775
_err:
×
776
  return NULL;
×
777
}
778

779
SNode* createTimeOffsetValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
3,244✔
780
  CHECK_PARSER_STATUS(pCxt);
3,244!
781
  SValueNode* val = NULL;
3,244✔
782
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
3,244✔
783
  CHECK_MAKE_NODE(val);
3,244!
784
  if (pLiteral->type == TK_NK_STRING) {
3,244!
785
    // like '100s' or "100d"
786
    // check format: ^[0-9]+[smwbauhdny]$'
787
    if (pLiteral->n < 4) {
×
788
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
789
      return NULL;
×
790
    }
791
    char unit = pLiteral->z[pLiteral->n - 2];
×
792
    switch (unit) {
×
793
      case 'a':
×
794
      case 'b':
795
      case 'd':
796
      case 'h':
797
      case 'm':
798
      case 's':
799
      case 'u':
800
      case 'w':
801
        break;
×
802
      default:
×
803
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
804
        return NULL;
×
805
    }
806
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
×
807
      if (!isdigit(pLiteral->z[i])) {
×
808
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
809
        return NULL;
×
810
      }
811
    }
812
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
×
813
  } else {
814
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
3,244✔
815
  }
816
  if (!val->literal) {
3,244!
817
    nodesDestroyNode((SNode*)val);
×
818
    pCxt->errCode = terrno;
×
819
    return NULL;
×
820
  }
821
  val->flag |= VALUE_FLAG_IS_TIME_OFFSET;
3,244✔
822
  val->translate = false;
3,244✔
823
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
3,244✔
824
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
3,244✔
825
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
3,244✔
826
  return (SNode*)val;
3,244✔
827
_err:
×
828
  return NULL;
×
829
}
830

831
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
2,201✔
832
  CHECK_PARSER_STATUS(pCxt);
2,201!
833
  if (NULL == pCxt->pQueryCxt->db) {
2,201!
834
    return NULL;
×
835
  }
836

837
  SValueNode* val = NULL;
2,201✔
838
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
2,201✔
839
  CHECK_MAKE_NODE(val);
2,201!
840
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
2,201✔
841
  if (!val->literal) {
2,201!
842
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
843
    nodesDestroyNode((SNode*)val);
×
844
    return NULL;
×
845
  }
846
  val->translate = false;
2,201✔
847
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
2,201✔
848
  val->node.resType.bytes = strlen(val->literal);
2,201✔
849
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
2,201✔
850
  return (SNode*)val;
2,201✔
851
_err:
×
852
  return NULL;
×
853
}
854

855
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
17✔
856
  CHECK_PARSER_STATUS(pCxt);
17!
857
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
17!
858
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
859
    return NULL;
×
860
  }
861
  SValueNode* val = NULL;
17✔
862
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
17✔
863
  CHECK_MAKE_NODE(val);
17!
864
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
17✔
865
  if (!val->literal) {
17!
866
    pCxt->errCode = terrno;
×
867
    nodesDestroyNode((SNode*)val);
×
868
    return NULL;
×
869
  }
870
  val->placeholderNo = ++pCxt->placeholderNo;
17✔
871
  if (NULL == pCxt->pPlaceholderValues) {
17✔
872
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
16✔
873
    if (NULL == pCxt->pPlaceholderValues) {
16!
874
      nodesDestroyNode((SNode*)val);
×
875
      return NULL;
×
876
    }
877
  }
878
  if (NULL == taosArrayPush(pCxt->pPlaceholderValues, &val)) {
34!
879
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
880
    nodesDestroyNode((SNode*)val);
×
881
    taosArrayDestroy(pCxt->pPlaceholderValues);
×
882
    return NULL;
×
883
  }
884
  return (SNode*)val;
17✔
885
_err:
×
886
  return NULL;
×
887
}
888

889
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
1,434,335✔
890
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
1,434,335✔
891
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
225,469✔
892
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
225,169✔
893
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
225,169✔
894
    nodesDestroyNode(pParam);
225,169✔
895
    return code;
225,169✔
896
  } else {
897
    return nodesListAppend(pCond->pParameterList, pParam);
1,209,166✔
898
  }
899
}
900

901
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
717,881✔
902
  CHECK_PARSER_STATUS(pCxt);
717,881!
903
  SLogicConditionNode* cond = NULL;
717,881✔
904
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
717,881✔
905
  CHECK_MAKE_NODE(cond);
717,908!
906
  cond->condType = type;
717,908✔
907
  cond->pParameterList = NULL;
717,908✔
908
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
717,908✔
909
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
717,910✔
910
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
717,907✔
911
  }
912
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
717,907✔
913
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
716,453✔
914
  }
915
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
717,903!
916
    nodesDestroyNode((SNode*)cond);
×
917
    return NULL;
×
918
  }
919
  return (SNode*)cond;
717,903✔
920
_err:
×
921
  nodesDestroyNode(pParam1);
×
922
  nodesDestroyNode(pParam2);
×
923
  return NULL;
1✔
924
}
925

926
static uint8_t getMinusDataType(uint8_t orgType) {
264,145✔
927
  switch (orgType) {
264,145✔
928
    case TSDB_DATA_TYPE_UTINYINT:
170,184✔
929
    case TSDB_DATA_TYPE_USMALLINT:
930
    case TSDB_DATA_TYPE_UINT:
931
    case TSDB_DATA_TYPE_UBIGINT:
932
      return TSDB_DATA_TYPE_BIGINT;
170,184✔
933
    default:
93,961✔
934
      break;
93,961✔
935
  }
936
  return orgType;
93,961✔
937
}
938

939
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
2,288,101✔
940
  CHECK_PARSER_STATUS(pCxt);
2,288,101!
941
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
2,288,101✔
942
    SValueNode* pVal = (SValueNode*)pLeft;
264,145✔
943
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
264,145✔
944
    if (!pNewLiteral) {
264,145!
945
      pCxt->errCode = terrno;
×
946
      goto _err;
×
947
    }
948
    if ('+' == pVal->literal[0]) {
264,145!
949
      sprintf(pNewLiteral, "-%s", pVal->literal + 1);
×
950
    } else if ('-' == pVal->literal[0]) {
264,145✔
951
      sprintf(pNewLiteral, "%s", pVal->literal + 1);
363✔
952
    } else {
953
      sprintf(pNewLiteral, "-%s", pVal->literal);
263,782✔
954
    }
955
    taosMemoryFree(pVal->literal);
264,145✔
956
    pVal->literal = pNewLiteral;
264,145✔
957
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
264,145✔
958
    return pLeft;
264,145✔
959
  }
960
  SOperatorNode* op = NULL;
2,023,956✔
961
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
2,023,956✔
962
  CHECK_MAKE_NODE(op);
2,023,963!
963
  op->opType = type;
2,023,963✔
964
  op->pLeft = pLeft;
2,023,963✔
965
  op->pRight = pRight;
2,023,963✔
966
  return (SNode*)op;
2,023,963✔
967
_err:
×
968
  nodesDestroyNode(pLeft);
×
969
  nodesDestroyNode(pRight);
×
970
  return NULL;
×
971
}
972

973
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
115,693✔
974
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
115,693✔
975
  CHECK_PARSER_STATUS(pCxt);
115,693!
976
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
115,693✔
977
  CHECK_PARSER_STATUS(pCxt);
115,693!
978
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
115,693✔
979
  CHECK_PARSER_STATUS(pCxt);
115,693!
980
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
115,693✔
981
  CHECK_PARSER_STATUS(pCxt);
115,693!
982
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
115,693✔
983
  CHECK_PARSER_STATUS(pCxt);
115,693!
984
  return pRet;
115,693✔
985
_err:
×
986
  nodesDestroyNode(pNew);
×
987
  nodesDestroyNode(pGE);
×
988
  nodesDestroyNode(pLE);
×
989
  nodesDestroyNode(pExpr);
×
990
  nodesDestroyNode(pLeft);
×
991
  nodesDestroyNode(pRight);
×
992
  return NULL;
×
993
}
994

995
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
35,773✔
996
  SNode *pNew = NULL, *pLT = NULL, *pGT = NULL;
35,773✔
997
  CHECK_PARSER_STATUS(pCxt);
35,773!
998
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
35,773✔
999
  CHECK_PARSER_STATUS(pCxt);
35,773!
1000
  pLT = createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft);
35,773✔
1001
  CHECK_PARSER_STATUS(pCxt);
35,773!
1002
  pGT = createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pNew, pRight);
35,773✔
1003
  CHECK_PARSER_STATUS(pCxt);
35,773!
1004
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, pLT, pGT);
35,773✔
1005
  CHECK_PARSER_STATUS(pCxt);
35,773!
1006
  return pRet;
35,773✔
1007
_err:
×
1008
  nodesDestroyNode(pNew);
×
1009
  nodesDestroyNode(pGT);
×
1010
  nodesDestroyNode(pLT);
×
1011
  nodesDestroyNode(pExpr);
×
1012
  nodesDestroyNode(pLeft);
×
1013
  nodesDestroyNode(pRight);
×
1014
  return NULL;
×
1015
}
1016

1017
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
319,766✔
1018
  CHECK_PARSER_STATUS(pCxt);
319,766!
1019
  SColumnNode* pCol = NULL;
319,766✔
1020
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
319,766✔
1021
  CHECK_MAKE_NODE(pCol);
319,768!
1022
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
319,768✔
1023
  if (NULL == pFuncName) {
319,768✔
1024
    strcpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME);
216,003✔
1025
  } else {
1026
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
103,765✔
1027
  }
1028
  pCol->isPrimTs = true;
319,768✔
1029
  return (SNode*)pCol;
319,768✔
1030
_err:
×
1031
  return NULL;
×
1032
}
1033

1034
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
2,529,492✔
1035
  CHECK_PARSER_STATUS(pCxt);
2,529,492!
1036
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
2,529,492✔
1037
    return createPrimaryKeyCol(pCxt, pFuncName);
103,757✔
1038
  }
1039
  SFunctionNode* func = NULL;
2,425,735✔
1040
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
2,425,735✔
1041
  CHECK_MAKE_NODE(func);
2,425,733!
1042
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
2,425,733✔
1043
  func->pParameterList = pParameterList;
2,425,733✔
1044
  return (SNode*)func;
2,425,733✔
1045
_err:
×
1046
  nodesDestroyList(pParameterList);
×
1047
  return NULL;
×
1048
}
1049

1050
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
827,107✔
1051
  SFunctionNode* func = NULL;
827,107✔
1052
  CHECK_PARSER_STATUS(pCxt);
827,107!
1053
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
827,107✔
1054
  CHECK_MAKE_NODE(func);
827,107!
1055
  strcpy(func->functionName, "cast");
827,107✔
1056
  func->node.resType = dt;
827,107✔
1057
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
827,107!
1058
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
676,625✔
1059
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
150,482✔
1060
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
24,437✔
1061
  }
1062
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
827,107✔
1063
  CHECK_PARSER_STATUS(pCxt);
827,107!
1064
  return (SNode*)func;
827,107✔
1065
_err:
×
1066
  nodesDestroyNode((SNode*)func);
×
1067
  nodesDestroyNode(pExpr);
×
1068
  return NULL;
×
1069
}
1070

1071
SNode* createPositionFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
218✔
1072
  SFunctionNode* func = NULL;
218✔
1073
  CHECK_PARSER_STATUS(pCxt);
218!
1074
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
218✔
1075
  CHECK_MAKE_NODE(func);
218!
1076
  strcpy(func->functionName, "position");
218✔
1077
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
218✔
1078
  CHECK_PARSER_STATUS(pCxt);
218!
1079
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
218✔
1080
  CHECK_PARSER_STATUS(pCxt);
218!
1081
  return (SNode*)func;
218✔
1082
_err:
×
1083
  nodesDestroyNode((SNode*)func);
×
1084
  nodesDestroyNode(pExpr);
×
1085
  nodesDestroyNode(pExpr2);
×
1086
  return NULL;
×
1087
}
1088

1089
SNode* createTrimFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, ETrimType type) {
29✔
1090
  SFunctionNode* func = NULL;
29✔
1091
  CHECK_PARSER_STATUS(pCxt);
29!
1092
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
29✔
1093
  CHECK_MAKE_NODE(func);
29!
1094
  strcpy(func->functionName, "trim");
29✔
1095
  func->trimType = type;
29✔
1096
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
29✔
1097
  CHECK_PARSER_STATUS(pCxt);
29!
1098
  return (SNode*)func;
29✔
1099
_err:
×
1100
  nodesDestroyNode((SNode*)func);
×
1101
  nodesDestroyNode(pExpr);
×
1102
  return NULL;
×
1103
}
1104

1105
SNode* createTrimFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, ETrimType type) {
124✔
1106
  SFunctionNode* func = NULL;
124✔
1107
  CHECK_PARSER_STATUS(pCxt);
124!
1108
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
124✔
1109
  CHECK_MAKE_NODE(func);
124!
1110
  strcpy(func->functionName, "trim");
124✔
1111
  func->trimType = type;
124✔
1112
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
124✔
1113
  CHECK_PARSER_STATUS(pCxt);
124!
1114
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
124✔
1115
  CHECK_PARSER_STATUS(pCxt);
124!
1116
  return (SNode*)func;
124✔
1117
_err:
×
1118
  nodesDestroyNode((SNode*)func);
×
1119
  nodesDestroyNode(pExpr);
×
1120
  nodesDestroyNode(pExpr2);
×
1121
  return NULL;
×
1122
}
1123

1124
SNode* createSubstrFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
20✔
1125
  SFunctionNode* func = NULL;
20✔
1126
  CHECK_PARSER_STATUS(pCxt);
20!
1127
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
20✔
1128
  CHECK_MAKE_NODE(func);
20!
1129
  strcpy(func->functionName, "substr");
20✔
1130
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
20✔
1131
  CHECK_PARSER_STATUS(pCxt);
20!
1132
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
20✔
1133
  CHECK_PARSER_STATUS(pCxt);
20!
1134
  return (SNode*)func;
20✔
1135
_err:
×
1136
  nodesDestroyNode((SNode*)func);
×
1137
  nodesDestroyNode(pExpr);
×
1138
  nodesDestroyNode(pExpr2);
×
1139
  return NULL;
×
1140
}
1141

1142
SNode* createSubstrFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, SNode* pExpr3) {
44✔
1143
  SFunctionNode* func = NULL;
44✔
1144
  CHECK_PARSER_STATUS(pCxt);
44!
1145
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
44✔
1146
  CHECK_MAKE_NODE(func);
44!
1147
  strcpy(func->functionName, "substr");
44✔
1148
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
44✔
1149
  CHECK_PARSER_STATUS(pCxt);
44!
1150
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
44✔
1151
  CHECK_PARSER_STATUS(pCxt);
44!
1152
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr3);
44✔
1153
  CHECK_PARSER_STATUS(pCxt);
44!
1154
  return (SNode*)func;
44✔
1155
_err:
×
1156
  nodesDestroyNode((SNode*)func);
×
1157
  nodesDestroyNode(pExpr);
×
1158
  nodesDestroyNode(pExpr2);
×
1159
  nodesDestroyNode(pExpr3);
×
1160
  return NULL;
×
1161
}
1162

1163
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
44,431✔
1164
  SNodeListNode* list = NULL;
44,431✔
1165
  CHECK_PARSER_STATUS(pCxt);
44,431!
1166
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
44,431✔
1167
  CHECK_MAKE_NODE(list);
44,431!
1168
  list->pNodeList = pList;
44,431✔
1169
  return (SNode*)list;
44,431✔
1170
_err:
×
1171
  nodesDestroyList(pList);
×
1172
  return NULL;
×
1173
}
1174

1175
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
15✔
1176
  SNodeListNode* list = NULL;
15✔
1177
  CHECK_PARSER_STATUS(pCxt);
15!
1178
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
15✔
1179
  CHECK_MAKE_NODE(list);
15!
1180
  pCxt->errCode = nodesListMakeStrictAppend(&list->pNodeList, p1);
15✔
1181
  CHECK_PARSER_STATUS(pCxt);
15!
1182
  pCxt->errCode = nodesListStrictAppend(list->pNodeList, p2);
15✔
1183
  CHECK_PARSER_STATUS(pCxt);
15!
1184
  return (SNode*)list;
15✔
1185
_err:
×
1186
  nodesDestroyNode((SNode*)list);
×
1187
  nodesDestroyNode(p1);
×
1188
  nodesDestroyNode(p2);
×
1189
  return NULL;
×
1190
}
1191

1192
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
1,433,249✔
1193
  CHECK_PARSER_STATUS(pCxt);
1,433,249!
1194
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
1,433,249✔
1195
  CHECK_NAME(checkTableName(pCxt, pTableName));
1,433,233✔
1196
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
1,433,207!
1197
  SRealTableNode* realTable = NULL;
1,433,147✔
1198
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
1,433,147✔
1199
  CHECK_MAKE_NODE(realTable);
1,433,162!
1200
  if (NULL != pDbName) {
1,433,162✔
1201
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
836,394✔
1202
  } else {
1203
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
596,768✔
1204
  }
1205
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
1,433,162✔
1206
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
226,551✔
1207
  } else {
1208
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
1,206,611✔
1209
  }
1210
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
1,433,162✔
1211
  return (SNode*)realTable;
1,433,162✔
1212
_err:
36✔
1213
  return NULL;
36✔
1214
}
1215

1216
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
307,107✔
1217
  CHECK_PARSER_STATUS(pCxt);
307,107!
1218
  if (!checkTableName(pCxt, pTableAlias)) {
307,107!
1219
    return NULL;
×
1220
  }
1221
  STempTableNode* tempTable = NULL;
307,108✔
1222
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
307,108✔
1223
  CHECK_MAKE_NODE(tempTable);
307,107!
1224
  tempTable->pSubquery = pSubquery;
307,107✔
1225
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
307,107!
1226
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
7,966✔
1227
  } else {
1228
    taosRandStr(tempTable->table.tableAlias, 8);
299,141✔
1229
  }
1230
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
307,107✔
1231
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
271,564✔
1232
    ((SSelectStmt*)pSubquery)->isSubquery = true;
271,564✔
1233
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
35,543!
1234
    strcpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias);
35,543✔
1235
  }
1236
  return (SNode*)tempTable;
307,107✔
1237
_err:
×
1238
  nodesDestroyNode(pSubquery);
×
1239
  return NULL;
×
1240
}
1241

1242
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, EJoinSubType stype, SNode* pLeft, SNode* pRight,
129,163✔
1243
                           SNode* pJoinCond) {
1244
  CHECK_PARSER_STATUS(pCxt);
129,163!
1245
  SJoinTableNode* joinTable = NULL;
129,163✔
1246
  pCxt->errCode = nodesMakeNode(QUERY_NODE_JOIN_TABLE, (SNode**)&joinTable);
129,163✔
1247
  CHECK_MAKE_NODE(joinTable);
129,163!
1248
  joinTable->joinType = type;
129,163✔
1249
  joinTable->subType = stype;
129,163✔
1250
  joinTable->pLeft = pLeft;
129,163✔
1251
  joinTable->pRight = pRight;
129,163✔
1252
  joinTable->pOnCond = pJoinCond;
129,163✔
1253
  return (SNode*)joinTable;
129,163✔
1254
_err:
×
1255
  nodesDestroyNode(pLeft);
×
1256
  nodesDestroyNode(pRight);
×
1257
  nodesDestroyNode(pJoinCond);
×
1258
  return NULL;
×
1259
}
1260

1261
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
381✔
1262
  CHECK_PARSER_STATUS(pCxt);
381!
1263
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
381✔
1264
  CHECK_NAME(checkViewName(pCxt, pViewName));
379✔
1265
  SViewNode* pView = NULL;
376✔
1266
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
376✔
1267
  CHECK_MAKE_NODE(pView);
376!
1268
  if (NULL != pDbName) {
376✔
1269
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
69✔
1270
  } else {
1271
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
307✔
1272
  }
1273
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
376✔
1274
  return (SNode*)pView;
376✔
1275
_err:
5✔
1276
  return NULL;
5✔
1277
}
1278

1279
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
159,791✔
1280
  CHECK_PARSER_STATUS(pCxt);
159,791!
1281
  SLimitNode* limitNode = NULL;
159,791✔
1282
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
159,791✔
1283
  CHECK_MAKE_NODE(limitNode);
159,791!
1284
  limitNode->limit = taosStr2Int64(pLimit->z, NULL, 10);
159,791✔
1285
  if (NULL != pOffset) {
159,791✔
1286
    limitNode->offset = taosStr2Int64(pOffset->z, NULL, 10);
65,368✔
1287
  }
1288
  return (SNode*)limitNode;
159,791✔
1289
_err:
×
1290
  return NULL;
×
1291
}
1292

1293
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
425,093✔
1294
  CHECK_PARSER_STATUS(pCxt);
425,093!
1295
  SOrderByExprNode* orderByExpr = NULL;
425,093✔
1296
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
425,093✔
1297
  CHECK_MAKE_NODE(orderByExpr);
425,094!
1298
  orderByExpr->pExpr = pExpr;
425,094✔
1299
  orderByExpr->order = order;
425,094✔
1300
  if (NULL_ORDER_DEFAULT == nullOrder) {
425,094✔
1301
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
425,082✔
1302
  }
1303
  orderByExpr->nullOrder = nullOrder;
425,094✔
1304
  return (SNode*)orderByExpr;
425,094✔
1305
_err:
×
1306
  nodesDestroyNode(pExpr);
×
1307
  return NULL;
×
1308
}
1309

1310
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
6,105✔
1311
  CHECK_PARSER_STATUS(pCxt);
6,105!
1312
  SSessionWindowNode* session = NULL;
6,105✔
1313
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
6,105✔
1314
  CHECK_MAKE_NODE(session);
6,105!
1315
  session->pCol = (SColumnNode*)pCol;
6,105✔
1316
  session->pGap = (SValueNode*)pGap;
6,105✔
1317
  return (SNode*)session;
6,105✔
1318
_err:
×
1319
  nodesDestroyNode(pCol);
×
1320
  nodesDestroyNode(pGap);
×
1321
  return NULL;
×
1322
}
1323

1324
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
6,439✔
1325
  SStateWindowNode* state = NULL;
6,439✔
1326
  CHECK_PARSER_STATUS(pCxt);
6,439!
1327
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
6,439✔
1328
  CHECK_MAKE_NODE(state);
6,439!
1329
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
6,439✔
1330
  CHECK_MAKE_NODE(state->pCol);
6,439!
1331
  state->pExpr = pExpr;
6,439✔
1332
  return (SNode*)state;
6,439✔
1333
_err:
×
1334
  nodesDestroyNode((SNode*)state);
×
1335
  nodesDestroyNode(pExpr);
×
1336
  return NULL;
×
1337
}
1338

1339
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond) {
725✔
1340
  SEventWindowNode* pEvent = NULL;
725✔
1341
  CHECK_PARSER_STATUS(pCxt);
725!
1342
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
725✔
1343
  CHECK_MAKE_NODE(pEvent);
725!
1344
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
725✔
1345
  CHECK_MAKE_NODE(pEvent->pCol);
725!
1346
  pEvent->pStartCond = pStartCond;
725✔
1347
  pEvent->pEndCond = pEndCond;
725✔
1348
  return (SNode*)pEvent;
725✔
1349
_err:
×
1350
  nodesDestroyNode((SNode*)pEvent);
×
1351
  nodesDestroyNode(pStartCond);
×
1352
  nodesDestroyNode(pEndCond);
×
1353
  return NULL;
×
1354
}
1355

1356
SNode* createCountWindowNode(SAstCreateContext* pCxt, const SToken* pCountToken, const SToken* pSlidingToken) {
620✔
1357
  SCountWindowNode* pCount = NULL;
620✔
1358
  CHECK_PARSER_STATUS(pCxt);
620!
1359
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
620✔
1360
  CHECK_MAKE_NODE(pCount);
620!
1361
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
620✔
1362
  CHECK_MAKE_NODE(pCount->pCol);
620!
1363
  pCount->windowCount = taosStr2Int64(pCountToken->z, NULL, 10);
620✔
1364
  pCount->windowSliding = taosStr2Int64(pSlidingToken->z, NULL, 10);
620✔
1365
  return (SNode*)pCount;
620✔
1366
_err:
×
1367
  nodesDestroyNode((SNode*)pCount);
×
1368
  return NULL;
×
1369
}
1370

1371
SNode* createAnomalyWindowNode(SAstCreateContext* pCxt, SNode* pExpr, const SToken* pFuncOpt) {
×
1372
  SAnomalyWindowNode* pAnomaly = NULL;
×
1373
  CHECK_PARSER_STATUS(pCxt);
×
1374
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ANOMALY_WINDOW, (SNode**)&pAnomaly);
×
1375
  CHECK_MAKE_NODE(pAnomaly);
×
1376
  pAnomaly->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1377
  CHECK_MAKE_NODE(pAnomaly->pCol);
×
1378
  pAnomaly->pExpr = pExpr;
×
1379
  if (pFuncOpt == NULL) {
×
1380
    tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
1381
  } else {
1382
    (void)trimString(pFuncOpt->z, pFuncOpt->n, pAnomaly->anomalyOpt, sizeof(pAnomaly->anomalyOpt));
×
1383
  }
1384
  return (SNode*)pAnomaly;
×
1385
_err:
×
1386
  nodesDestroyNode((SNode*)pAnomaly);
×
1387
  return NULL;
×
1388
}
1389

1390
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
30,975✔
1391
                                SNode* pFill) {
1392
  SIntervalWindowNode* interval = NULL;
30,975✔
1393
  CHECK_PARSER_STATUS(pCxt);
30,975!
1394
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
30,975✔
1395
  CHECK_MAKE_NODE(interval);
30,976!
1396
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
30,976✔
1397
  CHECK_MAKE_NODE(interval->pCol);
30,976!
1398
  interval->pInterval = pInterval;
30,976✔
1399
  interval->pOffset = pOffset;
30,976✔
1400
  interval->pSliding = pSliding;
30,976✔
1401
  interval->pFill = pFill;
30,976✔
1402
  return (SNode*)interval;
30,976✔
1403
_err:
×
1404
  nodesDestroyNode((SNode*)interval);
×
1405
  nodesDestroyNode(pInterval);
×
1406
  nodesDestroyNode(pOffset);
×
1407
  nodesDestroyNode(pSliding);
×
1408
  nodesDestroyNode(pFill);
×
1409
  return NULL;
×
1410
}
1411

1412
SNode* createWindowOffsetNode(SAstCreateContext* pCxt, SNode* pStartOffset, SNode* pEndOffset) {
1,614✔
1413
  SWindowOffsetNode* winOffset = NULL;
1,614✔
1414
  CHECK_PARSER_STATUS(pCxt);
1,614!
1415
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WINDOW_OFFSET, (SNode**)&winOffset);
1,614✔
1416
  CHECK_MAKE_NODE(winOffset);
1,614!
1417
  winOffset->pStartOffset = pStartOffset;
1,614✔
1418
  winOffset->pEndOffset = pEndOffset;
1,614✔
1419
  return (SNode*)winOffset;
1,614✔
1420
_err:
×
1421
  nodesDestroyNode((SNode*)winOffset);
×
1422
  nodesDestroyNode(pStartOffset);
×
1423
  nodesDestroyNode(pEndOffset);
×
1424
  return NULL;
×
1425
}
1426

1427
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
12,372✔
1428
  SFillNode* fill = NULL;
12,372✔
1429
  CHECK_PARSER_STATUS(pCxt);
12,372!
1430
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
12,372✔
1431
  CHECK_MAKE_NODE(fill);
12,372!
1432
  fill->mode = mode;
12,372✔
1433
  fill->pValues = pValues;
12,372✔
1434
  fill->pWStartTs = NULL;
12,372✔
1435
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&(fill->pWStartTs));
12,372✔
1436
  CHECK_MAKE_NODE(fill->pWStartTs);
12,373!
1437
  strcpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart");
12,373✔
1438
  return (SNode*)fill;
12,373✔
1439
_err:
×
1440
  nodesDestroyNode((SNode*)fill);
×
1441
  nodesDestroyNode(pValues);
×
1442
  return NULL;
×
1443
}
1444

1445
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
197,257✔
1446
  SGroupingSetNode* groupingSet = NULL;
197,257✔
1447
  CHECK_PARSER_STATUS(pCxt);
197,257!
1448
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
197,257✔
1449
  CHECK_MAKE_NODE(groupingSet);
197,257!
1450
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
197,257✔
1451
  groupingSet->pParameterList = NULL;
197,257✔
1452
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
197,257✔
1453
  CHECK_PARSER_STATUS(pCxt);
197,257!
1454
  return (SNode*)groupingSet;
197,257✔
1455
_err:
×
1456
  nodesDestroyNode((SNode*)groupingSet);
×
1457
  nodesDestroyNode(pNode);
×
1458
  return NULL;
×
1459
}
1460

1461
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
3,435✔
1462
  CHECK_PARSER_STATUS(pCxt);
3,435!
1463
  return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
3,435✔
1464
_err:
×
1465
  nodesDestroyNode(pStart);
×
1466
  nodesDestroyNode(pEnd);
×
1467
  return NULL;
×
1468
}
1469

1470
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
396✔
1471
  CHECK_PARSER_STATUS(pCxt);
396!
1472
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
396✔
1473
_err:
×
1474
  nodesDestroyNode(pPoint);
×
1475
  return NULL;
×
1476
}
1477

1478
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
15,011✔
1479
  CHECK_PARSER_STATUS(pCxt);
15,011!
1480
  SWhenThenNode* pWhenThen = NULL;
15,011✔
1481
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
15,011✔
1482
  CHECK_MAKE_NODE(pWhenThen);
15,011!
1483
  pWhenThen->pWhen = pWhen;
15,011✔
1484
  pWhenThen->pThen = pThen;
15,011✔
1485
  return (SNode*)pWhenThen;
15,011✔
1486
_err:
×
1487
  nodesDestroyNode(pWhen);
×
1488
  nodesDestroyNode(pThen);
×
1489
  return NULL;
×
1490
}
1491

1492
SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
11,070✔
1493
  CHECK_PARSER_STATUS(pCxt);
11,070!
1494
  SCaseWhenNode* pCaseWhen = NULL;
11,070✔
1495
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CASE_WHEN, (SNode**)&pCaseWhen);
11,070✔
1496
  CHECK_MAKE_NODE(pCaseWhen);
11,070!
1497
  pCaseWhen->pCase = pCase;
11,070✔
1498
  pCaseWhen->pWhenThenList = pWhenThenList;
11,070✔
1499
  pCaseWhen->pElse = pElse;
11,070✔
1500
  return (SNode*)pCaseWhen;
11,070✔
1501
_err:
×
1502
  nodesDestroyNode(pCase);
×
1503
  nodesDestroyList(pWhenThenList);
×
1504
  nodesDestroyNode(pElse);
×
1505
  return NULL;
×
1506
}
1507

1508
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
734,936✔
1509
  CHECK_PARSER_STATUS(pCxt);
734,936!
1510
  trimEscape(pAlias);
734,936✔
1511
  SExprNode* pExpr = (SExprNode*)pNode;
734,936✔
1512
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
734,936✔
1513
  strncpy(pExpr->aliasName, pAlias->z, len);
734,936✔
1514
  pExpr->aliasName[len] = '\0';
734,936✔
1515
  strncpy(pExpr->userAlias, pAlias->z, len);
734,936✔
1516
  pExpr->userAlias[len] = '\0';
734,936✔
1517
  pExpr->asAlias = true;
734,936✔
1518
  return pNode;
734,936✔
1519
_err:
×
1520
  nodesDestroyNode(pNode);
×
1521
  return NULL;
×
1522
}
1523

1524
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
1,407,461✔
1525
  CHECK_PARSER_STATUS(pCxt);
1,407,461✔
1526
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,407,456!
1527
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
1,407,469✔
1528
  }
1529
  return pStmt;
1,407,456✔
1530
_err:
5✔
1531
  nodesDestroyNode(pStmt);
5✔
1532
  nodesDestroyNode(pWhere);
5✔
1533
  return NULL;
5✔
1534
}
1535

1536
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
1,407,462✔
1537
  CHECK_PARSER_STATUS(pCxt);
1,407,462✔
1538
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,407,457!
1539
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
1,407,468✔
1540
  }
1541
  return pStmt;
1,407,457✔
1542
_err:
5✔
1543
  nodesDestroyNode(pStmt);
5✔
1544
  nodesDestroyList(pPartitionByList);
5✔
1545
  return NULL;
5✔
1546
}
1547

1548
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
1,407,461✔
1549
  CHECK_PARSER_STATUS(pCxt);
1,407,461✔
1550
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,407,456!
1551
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
1,407,472✔
1552
  }
1553
  return pStmt;
1,407,456✔
1554
_err:
5✔
1555
  nodesDestroyNode(pStmt);
5✔
1556
  nodesDestroyNode(pWindow);
5✔
1557
  return NULL;
5✔
1558
}
1559

1560
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
1,407,463✔
1561
  CHECK_PARSER_STATUS(pCxt);
1,407,463✔
1562
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,407,458!
1563
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
1,407,471✔
1564
  }
1565
  return pStmt;
1,407,458✔
1566
_err:
5✔
1567
  nodesDestroyNode(pStmt);
5✔
1568
  nodesDestroyList(pGroupByList);
5✔
1569
  return NULL;
5✔
1570
}
1571

1572
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
1,407,460✔
1573
  CHECK_PARSER_STATUS(pCxt);
1,407,460✔
1574
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,407,455!
1575
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
1,407,470✔
1576
  }
1577
  return pStmt;
1,407,455✔
1578
_err:
5✔
1579
  nodesDestroyNode(pStmt);
5✔
1580
  nodesDestroyNode(pHaving);
5✔
1581
  return NULL;
5✔
1582
}
1583

1584
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
1,380,612✔
1585
  CHECK_PARSER_STATUS(pCxt);
1,380,612✔
1586
  if (NULL == pOrderByList) {
1,380,607✔
1587
    return pStmt;
1,048,155✔
1588
  }
1589
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
332,452✔
1590
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
292,863✔
1591
  } else {
1592
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
39,589✔
1593
  }
1594
  return pStmt;
332,452✔
1595
_err:
5✔
1596
  nodesDestroyNode(pStmt);
5✔
1597
  nodesDestroyList(pOrderByList);
5✔
1598
  return NULL;
5✔
1599
}
1600

1601
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
1,380,611✔
1602
  CHECK_PARSER_STATUS(pCxt);
1,380,611✔
1603
  if (NULL == pSlimit) {
1,380,606✔
1604
    return pStmt;
1,364,299✔
1605
  }
1606
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
16,307!
1607
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
16,318✔
1608
  }
1609
  return pStmt;
16,307✔
1610
_err:
5✔
1611
  nodesDestroyNode(pStmt);
5✔
1612
  nodesDestroyNode(pSlimit);
5✔
1613
  return NULL;
5✔
1614
}
1615

1616
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
1,380,620✔
1617
  CHECK_PARSER_STATUS(pCxt);
1,380,620✔
1618
  if (NULL == pLimit) {
1,380,615✔
1619
    return pStmt;
1,237,729✔
1620
  }
1621
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
142,886✔
1622
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
132,379✔
1623
  } else {
1624
    ((SSetOperator*)pStmt)->pLimit = pLimit;
10,507✔
1625
  }
1626
  return pStmt;
142,886✔
1627
_err:
5✔
1628
  nodesDestroyNode(pStmt);
5✔
1629
  nodesDestroyNode(pLimit);
5✔
1630
  return NULL;
5✔
1631
}
1632

1633
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
1,407,459✔
1634
  CHECK_PARSER_STATUS(pCxt);
1,407,459✔
1635
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,407,454!
1636
    ((SSelectStmt*)pStmt)->pRange = pRange;
1,407,463✔
1637
  }
1638
  return pStmt;
1,407,454✔
1639
_err:
5✔
1640
  nodesDestroyNode(pStmt);
5✔
1641
  nodesDestroyNode(pRange);
5✔
1642
  return NULL;
5✔
1643
}
1644

1645
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
1,407,461✔
1646
  CHECK_PARSER_STATUS(pCxt);
1,407,461✔
1647
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,407,456!
1648
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
1,407,465✔
1649
  }
1650
  return pStmt;
1,407,456✔
1651
_err:
5✔
1652
  nodesDestroyNode(pStmt);
5✔
1653
  nodesDestroyNode(pEvery);
5✔
1654
  return NULL;
5✔
1655
}
1656

1657
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
1,407,457✔
1658
  CHECK_PARSER_STATUS(pCxt);
1,407,457✔
1659
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
1,407,452!
1660
    SFillNode* pFillClause = (SFillNode*)pFill;
3,957✔
1661
    nodesDestroyNode(pFillClause->pWStartTs);
3,957✔
1662
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
3,957✔
1663
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
3,957!
1664
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
3,957✔
1665
  }
1666
  return pStmt;
1,407,452✔
1667
_err:
5✔
1668
  nodesDestroyNode(pStmt);
5✔
1669
  nodesDestroyNode(pFill);
5✔
1670
  return NULL;
5✔
1671
}
1672

1673
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
49,651✔
1674
  CHECK_PARSER_STATUS(pCxt);
49,651!
1675
  if (NULL == pJLimit) {
49,651✔
1676
    return pJoin;
49,067✔
1677
  }
1678
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
584✔
1679
  pJoinNode->pJLimit = pJLimit;
584✔
1680

1681
  return pJoin;
584✔
1682
_err:
×
1683
  nodesDestroyNode(pJoin);
×
1684
  nodesDestroyNode(pJLimit);
×
1685
  return NULL;
×
1686
}
1687

1688
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
49,651✔
1689
  CHECK_PARSER_STATUS(pCxt);
49,651!
1690
  if (NULL == pWinOffset) {
49,651✔
1691
    return pJoin;
48,055✔
1692
  }
1693
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
1,596✔
1694
  pJoinNode->pWindowOffset = pWinOffset;
1,596✔
1695

1696
  return pJoin;
1,596✔
1697
_err:
×
1698
  nodesDestroyNode(pJoin);
×
1699
  nodesDestroyNode(pWinOffset);
×
1700
  return NULL;
×
1701
}
1702

1703
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
1,407,467✔
1704
                        SNodeList* pHint) {
1705
  CHECK_PARSER_STATUS(pCxt);
1,407,467✔
1706
  SNode* select = NULL;
1,407,462✔
1707
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
1,407,462✔
1708
  CHECK_MAKE_NODE(select);
1,407,477!
1709
  return select;
1,407,477✔
1710
_err:
5✔
1711
  nodesDestroyList(pProjectionList);
5✔
1712
  nodesDestroyNode(pTable);
5✔
1713
  nodesDestroyList(pHint);
5✔
1714
  return NULL;
6✔
1715
}
1716

1717
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
1,407,461✔
1718
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
1,407,461!
1719
    if (pCxt->pQueryCxt->biMode) {
1,407,470✔
1720
      ((SSelectStmt*)pStmt)->tagScan = true;
12✔
1721
    } else {
1722
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
1,407,458✔
1723
    }
1724
  }
1725
  return pStmt;
1,407,461✔
1726
}
1727

1728
static void setSubquery(SNode* pStmt) {
162,858✔
1729
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
162,858✔
1730
    ((SSelectStmt*)pStmt)->isSubquery = true;
162,228✔
1731
  }
1732
}
162,858✔
1733

1734
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
81,429✔
1735
  CHECK_PARSER_STATUS(pCxt);
81,429!
1736
  SSetOperator* setOp = NULL;
81,429✔
1737
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
81,429✔
1738
  CHECK_MAKE_NODE(setOp);
81,429!
1739
  setOp->opType = type;
81,429✔
1740
  setOp->pLeft = pLeft;
81,429✔
1741
  setSubquery(setOp->pLeft);
81,429✔
1742
  setOp->pRight = pRight;
81,429✔
1743
  setSubquery(setOp->pRight);
81,429✔
1744
  sprintf(setOp->stmtName, "%p", setOp);
81,429✔
1745
  return (SNode*)setOp;
81,429✔
1746
_err:
×
1747
  nodesDestroyNode(pLeft);
×
1748
  nodesDestroyNode(pRight);
×
1749
  return NULL;
×
1750
}
1751

1752
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
6,288✔
1753
  if (!pOptions->walRetentionPeriodIsSet) {
6,288✔
1754
    pOptions->walRetentionPeriod =
6,268✔
1755
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
1756
  }
1757
  if (!pOptions->walRetentionSizeIsSet) {
6,288✔
1758
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
6,287✔
1759
  }
1760
  if (!pOptions->walRollPeriodIsSet) {
6,288!
1761
    pOptions->walRollPeriod =
6,288✔
1762
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
1763
  }
1764
}
6,288✔
1765

1766
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
5,070✔
1767
  CHECK_PARSER_STATUS(pCxt);
5,070!
1768
  SDatabaseOptions* pOptions = NULL;
5,070✔
1769
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
5,070✔
1770
  CHECK_MAKE_NODE(pOptions);
5,070!
1771
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
5,070✔
1772
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
5,070✔
1773
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
5,070✔
1774
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
5,070✔
1775
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
5,070✔
1776
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
5,070✔
1777
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
5,070✔
1778
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
5,070✔
1779
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
5,070✔
1780
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
5,070✔
1781
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
5,070✔
1782
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
5,070✔
1783
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
5,070✔
1784
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
5,070✔
1785
  pOptions->precision = TSDB_DEFAULT_PRECISION;
5,070✔
1786
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
5,070✔
1787
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
5,070✔
1788
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
5,070✔
1789
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
5,070✔
1790
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
5,070✔
1791
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
5,070✔
1792
  updateWalOptionsDefault(pOptions);
5,070✔
1793
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
5,070✔
1794
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
5,070✔
1795
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
5,070✔
1796
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
5,070✔
1797
  pOptions->s3ChunkSize = TSDB_DEFAULT_S3_CHUNK_SIZE;
5,070✔
1798
  pOptions->s3KeepLocal = TSDB_DEFAULT_S3_KEEP_LOCAL;
5,070✔
1799
  pOptions->s3Compact = TSDB_DEFAULT_S3_COMPACT;
5,070✔
1800
  pOptions->withArbitrator = TSDB_DEFAULT_DB_WITH_ARBITRATOR;
5,070✔
1801
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
5,070✔
1802
  pOptions->dnodeListStr[0] = 0;
5,070✔
1803
  return (SNode*)pOptions;
5,070✔
1804
_err:
×
1805
  return NULL;
×
1806
}
1807

1808
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
226✔
1809
  CHECK_PARSER_STATUS(pCxt);
226!
1810
  SDatabaseOptions* pOptions = NULL;
226✔
1811
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
226✔
1812
  CHECK_MAKE_NODE(pOptions);
226!
1813
  pOptions->buffer = -1;
226✔
1814
  pOptions->cacheModel = -1;
226✔
1815
  pOptions->cacheLastSize = -1;
226✔
1816
  pOptions->compressionLevel = -1;
226✔
1817
  pOptions->daysPerFile = -1;
226✔
1818
  pOptions->fsyncPeriod = -1;
226✔
1819
  pOptions->maxRowsPerBlock = -1;
226✔
1820
  pOptions->minRowsPerBlock = -1;
226✔
1821
  pOptions->keep[0] = -1;
226✔
1822
  pOptions->keep[1] = -1;
226✔
1823
  pOptions->keep[2] = -1;
226✔
1824
  pOptions->pages = -1;
226✔
1825
  pOptions->pagesize = -1;
226✔
1826
  pOptions->tsdbPageSize = -1;
226✔
1827
  pOptions->precision = -1;
226✔
1828
  pOptions->replica = -1;
226✔
1829
  pOptions->strict = -1;
226✔
1830
  pOptions->walLevel = -1;
226✔
1831
  pOptions->numOfVgroups = -1;
226✔
1832
  pOptions->singleStable = -1;
226✔
1833
  pOptions->schemaless = -1;
226✔
1834
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
226✔
1835
  pOptions->walRetentionSize = -2;    // -1 is a valid value
226✔
1836
  pOptions->walRollPeriod = -1;
226✔
1837
  pOptions->walSegmentSize = -1;
226✔
1838
  pOptions->sstTrigger = -1;
226✔
1839
  pOptions->tablePrefix = -1;
226✔
1840
  pOptions->tableSuffix = -1;
226✔
1841
  pOptions->s3ChunkSize = -1;
226✔
1842
  pOptions->s3KeepLocal = -1;
226✔
1843
  pOptions->s3Compact = -1;
226✔
1844
  pOptions->withArbitrator = -1;
226✔
1845
  pOptions->encryptAlgorithm = -1;
226✔
1846
  pOptions->dnodeListStr[0] = 0;
226✔
1847
  return (SNode*)pOptions;
226✔
1848
_err:
×
1849
  return NULL;
×
1850
}
1851

1852
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
6,733✔
1853
                                    bool alter) {
1854
  CHECK_PARSER_STATUS(pCxt);
6,733!
1855
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
6,733✔
1856
  switch (type) {
6,733!
1857
    case DB_OPTION_BUFFER:
59✔
1858
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
59✔
1859
      break;
59✔
1860
    case DB_OPTION_CACHEMODEL:
247✔
1861
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
247!
1862
      break;
247✔
1863
    case DB_OPTION_CACHESIZE:
34✔
1864
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
34✔
1865
      break;
34✔
1866
    case DB_OPTION_COMP:
36✔
1867
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
36✔
1868
      break;
36✔
1869
    case DB_OPTION_DAYS: {
813✔
1870
      SToken* pToken = pVal;
813✔
1871
      if (TK_NK_INTEGER == pToken->type) {
813✔
1872
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
638✔
1873
      } else {
1874
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
175✔
1875
      }
1876
      break;
813✔
1877
    }
1878
    case DB_OPTION_FSYNC:
43✔
1879
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
43✔
1880
      break;
43✔
1881
    case DB_OPTION_MAXROWS:
66✔
1882
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
66✔
1883
      break;
66✔
1884
    case DB_OPTION_MINROWS:
72✔
1885
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
72✔
1886
      break;
72✔
1887
    case DB_OPTION_KEEP:
714✔
1888
      pDbOptions->pKeep = pVal;
714✔
1889
      break;
714✔
1890
    case DB_OPTION_PAGES:
29✔
1891
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
29✔
1892
      break;
29✔
1893
    case DB_OPTION_PAGESIZE:
6✔
1894
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
6✔
1895
      break;
6✔
1896
    case DB_OPTION_TSDB_PAGESIZE:
6✔
1897
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
6✔
1898
      break;
6✔
1899
    case DB_OPTION_PRECISION:
558✔
1900
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
558!
1901
      break;
558✔
1902
    case DB_OPTION_REPLICA:
1,237✔
1903
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
1,237✔
1904
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
1,237✔
1905
      if (!alter) {
1,237✔
1906
        updateWalOptionsDefault(pDbOptions);
1,218✔
1907
      }
1908
      break;
1,237✔
1909
    case DB_OPTION_STRICT:
×
1910
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
1911
      break;
×
1912
    case DB_OPTION_WAL:
57✔
1913
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
57✔
1914
      break;
57✔
1915
    case DB_OPTION_VGROUPS:
2,041✔
1916
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
2,041✔
1917
      break;
2,041✔
1918
    case DB_OPTION_SINGLE_STABLE:
9✔
1919
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
9✔
1920
      break;
9✔
1921
    case DB_OPTION_RETENTIONS:
5✔
1922
      pDbOptions->pRetentions = pVal;
5✔
1923
      break;
5✔
1924
    case DB_OPTION_WAL_RETENTION_PERIOD:
250✔
1925
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
250✔
1926
      pDbOptions->walRetentionPeriodIsSet = true;
250✔
1927
      break;
250✔
1928
    case DB_OPTION_WAL_RETENTION_SIZE:
36✔
1929
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
36✔
1930
      pDbOptions->walRetentionSizeIsSet = true;
36✔
1931
      break;
36✔
1932
    case DB_OPTION_WAL_ROLL_PERIOD:
×
1933
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
1934
      pDbOptions->walRollPeriodIsSet = true;
×
1935
      break;
×
1936
    case DB_OPTION_WAL_SEGMENT_SIZE:
×
1937
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
1938
      break;
×
1939
    case DB_OPTION_STT_TRIGGER:
35✔
1940
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
35✔
1941
      break;
35✔
1942
    case DB_OPTION_TABLE_PREFIX: {
32✔
1943
      SValueNode* pNode = (SValueNode*)pVal;
32✔
1944
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
32!
1945
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
32✔
1946
      } else {
1947
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
1948
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
1949
      }
1950
      nodesDestroyNode((SNode*)pNode);
32✔
1951
      break;
32✔
1952
    }
1953
    case DB_OPTION_TABLE_SUFFIX: {
31✔
1954
      SValueNode* pNode = (SValueNode*)pVal;
31✔
1955
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
31!
1956
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
31✔
1957
      } else {
1958
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
1959
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
1960
      }
1961
      nodesDestroyNode((SNode*)pNode);
31✔
1962
      break;
31✔
1963
    }
1964
    case DB_OPTION_S3_CHUNKPAGES:
63✔
1965
      pDbOptions->s3ChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
63✔
1966
      break;
63✔
1967
    case DB_OPTION_S3_KEEPLOCAL: {
69✔
1968
      SToken* pToken = pVal;
69✔
1969
      if (TK_NK_INTEGER == pToken->type) {
69✔
1970
        pDbOptions->s3KeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
67✔
1971
      } else {
1972
        pDbOptions->s3KeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
2✔
1973
      }
1974
      break;
69✔
1975
    }
1976
    case DB_OPTION_S3_COMPACT:
52✔
1977
      pDbOptions->s3Compact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
52✔
1978
      break;
52✔
1979
    case DB_OPTION_KEEP_TIME_OFFSET:
2✔
1980
      pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
2✔
1981
      break;
2✔
1982
    case DB_OPTION_ENCRYPT_ALGORITHM:
8✔
1983
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
8✔
1984
      pDbOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
8✔
1985
      break;
8✔
1986
    case DB_OPTION_DNODES:
36✔
1987
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
36✔
1988
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
1✔
1989
                 TSDB_DNODE_LIST_LEN);
1990
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1✔
1991
      } else {
1992
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
35!
1993
      }
1994
    default:
1995
      break;
123✔
1996
  }
1997
  return pOptions;
6,733✔
1998
_err:
×
1999
  nodesDestroyNode(pOptions);
×
2000
  return NULL;
×
2001
}
2002

2003
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
6,486✔
2004
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
6,486✔
2005
}
2006

2007
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
247✔
2008
  CHECK_PARSER_STATUS(pCxt);
247!
2009
  switch (pAlterOption->type) {
247✔
2010
    case DB_OPTION_KEEP:
43✔
2011
    case DB_OPTION_RETENTIONS:
2012
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
43✔
2013
    default:
204✔
2014
      break;
204✔
2015
  }
2016
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
204✔
2017
_err:
×
2018
  nodesDestroyNode(pOptions);
×
2019
  nodesDestroyList(pAlterOption->pList);
×
2020
  return NULL;
×
2021
}
2022

2023
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
5,023✔
2024
  CHECK_PARSER_STATUS(pCxt);
5,023✔
2025
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
5,022✔
2026
  SCreateDatabaseStmt* pStmt = NULL;
5,020✔
2027
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
5,020✔
2028
  CHECK_MAKE_NODE(pStmt);
5,020!
2029
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
5,020✔
2030
  pStmt->ignoreExists = ignoreExists;
5,020✔
2031
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
5,020✔
2032
  return (SNode*)pStmt;
5,020✔
2033
_err:
3✔
2034
  nodesDestroyNode(pOptions);
3✔
2035
  return NULL;
3✔
2036
}
2037

2038
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
3,553✔
2039
  CHECK_PARSER_STATUS(pCxt);
3,553!
2040
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
3,553!
2041
  SDropDatabaseStmt* pStmt = NULL;
3,553✔
2042
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
3,553✔
2043
  CHECK_MAKE_NODE(pStmt);
3,553!
2044
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
3,553✔
2045
  pStmt->ignoreNotExists = ignoreNotExists;
3,553✔
2046
  return (SNode*)pStmt;
3,553✔
2047
_err:
×
2048
  return NULL;
×
2049
}
2050

2051
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
224✔
2052
  CHECK_PARSER_STATUS(pCxt);
224!
2053
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
224!
2054
  SAlterDatabaseStmt* pStmt = NULL;
224✔
2055
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
224✔
2056
  CHECK_MAKE_NODE(pStmt);
224!
2057
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
224✔
2058
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
224✔
2059
  return (SNode*)pStmt;
224✔
2060
_err:
×
2061
  nodesDestroyNode(pOptions);
×
2062
  return NULL;
×
2063
}
2064

2065
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
9,838✔
2066
  CHECK_PARSER_STATUS(pCxt);
9,838!
2067
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
9,838!
2068
  SFlushDatabaseStmt* pStmt = NULL;
9,838✔
2069
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
9,838✔
2070
  CHECK_MAKE_NODE(pStmt);
9,838!
2071
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
9,838✔
2072
  return (SNode*)pStmt;
9,838✔
2073
_err:
×
2074
  return NULL;
×
2075
}
2076

2077
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
6✔
2078
  CHECK_PARSER_STATUS(pCxt);
6!
2079
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
6!
2080
  STrimDatabaseStmt* pStmt = NULL;
6✔
2081
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
6✔
2082
  CHECK_MAKE_NODE(pStmt);
6!
2083
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
6✔
2084
  pStmt->maxSpeed = maxSpeed;
6✔
2085
  return (SNode*)pStmt;
6✔
2086
_err:
×
2087
  return NULL;
×
2088
}
2089

2090
SNode* createS3MigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
3✔
2091
  CHECK_PARSER_STATUS(pCxt);
3!
2092
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
3!
2093
  SS3MigrateDatabaseStmt* pStmt = NULL;
3✔
2094
  pCxt->errCode = nodesMakeNode(QUERY_NODE_S3MIGRATE_DATABASE_STMT, (SNode**)&pStmt);
3✔
2095
  CHECK_MAKE_NODE(pStmt);
3!
2096
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
3✔
2097
  return (SNode*)pStmt;
3✔
2098
_err:
×
2099
  return NULL;
×
2100
}
2101

2102
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
19✔
2103
  CHECK_PARSER_STATUS(pCxt);
19!
2104
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
19!
2105
  SCompactDatabaseStmt* pStmt = NULL;
19✔
2106
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
19✔
2107
  CHECK_MAKE_NODE(pStmt);
19!
2108
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
19✔
2109
  pStmt->pStart = pStart;
19✔
2110
  pStmt->pEnd = pEnd;
19✔
2111
  return (SNode*)pStmt;
19✔
2112
_err:
×
2113
  nodesDestroyNode(pStart);
×
2114
  nodesDestroyNode(pEnd);
×
2115
  return NULL;
×
2116
}
2117

2118
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
102,860✔
2119
  CHECK_PARSER_STATUS(pCxt);
102,860!
2120
  STableOptions* pOptions = NULL;
102,860✔
2121
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
102,860✔
2122
  CHECK_MAKE_NODE(pOptions);
102,858!
2123
  pOptions->maxDelay1 = -1;
102,858✔
2124
  pOptions->maxDelay2 = -1;
102,858✔
2125
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
102,858✔
2126
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
102,858✔
2127
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
102,858✔
2128
  pOptions->commentNull = true;  // mark null
102,858✔
2129
  return (SNode*)pOptions;
102,858✔
2130
_err:
×
2131
  return NULL;
×
2132
}
2133

2134
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
129✔
2135
  CHECK_PARSER_STATUS(pCxt);
129!
2136
  STableOptions* pOptions = NULL;
129✔
2137
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
129✔
2138
  CHECK_MAKE_NODE(pOptions);
129!
2139
  pOptions->ttl = -1;
129✔
2140
  pOptions->commentNull = true;  // mark null
129✔
2141
  return (SNode*)pOptions;
129✔
2142
_err:
×
2143
  return NULL;
×
2144
}
2145

2146
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
332✔
2147
  CHECK_PARSER_STATUS(pCxt);
332!
2148
  switch (type) {
332!
2149
    case TABLE_OPTION_COMMENT:
135✔
2150
      if (checkComment(pCxt, (SToken*)pVal, true)) {
135✔
2151
        ((STableOptions*)pOptions)->commentNull = false;
122✔
2152
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
122✔
2153
      }
2154
      break;
135✔
2155
    case TABLE_OPTION_MAXDELAY:
2✔
2156
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
2✔
2157
      break;
2✔
2158
    case TABLE_OPTION_WATERMARK:
1✔
2159
      ((STableOptions*)pOptions)->pWatermark = pVal;
1✔
2160
      break;
1✔
2161
    case TABLE_OPTION_ROLLUP:
3✔
2162
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
3✔
2163
      break;
3✔
2164
    case TABLE_OPTION_TTL: {
190✔
2165
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
190✔
2166
      if (ttl > INT32_MAX) {
190✔
2167
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
5✔
2168
      } else {
2169
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
2170
        ((STableOptions*)pOptions)->ttl = ttl;
185✔
2171
      }
2172
      break;
190✔
2173
    }
2174
    case TABLE_OPTION_SMA:
1✔
2175
      ((STableOptions*)pOptions)->pSma = pVal;
1✔
2176
      break;
1✔
2177
    case TABLE_OPTION_DELETE_MARK:
×
2178
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
×
2179
      break;
×
2180
    default:
×
2181
      break;
×
2182
  }
2183
  return pOptions;
332✔
2184
_err:
×
2185
  nodesDestroyNode(pOptions);
×
2186
  return NULL;
×
2187
}
2188

2189
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
563,607✔
2190
  CHECK_PARSER_STATUS(pCxt);
563,607!
2191
  SColumnOptions* pOptions = NULL;
563,607✔
2192
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
563,607✔
2193
  CHECK_MAKE_NODE(pOptions);
563,607!
2194
  pOptions->commentNull = true;
563,607✔
2195
  pOptions->bPrimaryKey = false;
563,607✔
2196
  return (SNode*)pOptions;
563,607✔
2197
_err:
×
2198
  return NULL;
×
2199
}
2200

2201
EColumnOptionType getColumnOptionType(const char* optionType) {
4,676✔
2202
  if (0 == strcasecmp(optionType, "ENCODE")) {
4,676✔
2203
    return COLUMN_OPTION_ENCODE;
453✔
2204
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
4,223✔
2205
    return COLUMN_OPTION_COMPRESS;
2,106✔
2206
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
2,117✔
2207
    return COLUMN_OPTION_LEVEL;
2,115✔
2208
  }
2209
  return 0;
2✔
2210
}
2211
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
597✔
2212
  CHECK_PARSER_STATUS(pCxt);
597!
2213
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
597✔
2214
  return pOptions;
597✔
2215
_err:
×
2216
  nodesDestroyNode(pOptions);
×
2217
  return NULL;
×
2218
}
2219

2220
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
4,676✔
2221
  CHECK_PARSER_STATUS(pCxt);
4,676!
2222
  char optionType[TSDB_CL_OPTION_LEN];
2223

2224
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
4,676✔
2225
  strncpy(optionType, pVal1->z, TMIN(pVal1->n, TSDB_CL_OPTION_LEN));
4,676✔
2226
  if (0 == strlen(optionType)) {
4,676!
2227
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2228
    return pOptions;
×
2229
  }
2230
  EColumnOptionType type = getColumnOptionType(optionType);
4,676✔
2231
  switch (type) {
4,676✔
2232
    case COLUMN_OPTION_ENCODE:
453✔
2233
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
453✔
2234
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
453!
2235
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
453!
2236
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
2237
      }
2238
      break;
453✔
2239
    case COLUMN_OPTION_COMPRESS:
2,106✔
2240
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
2,106✔
2241
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
2,106!
2242
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
2,106!
2243
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
2244
      }
2245
      break;
2,106✔
2246
    case COLUMN_OPTION_LEVEL:
2,115✔
2247
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
2,115✔
2248
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
2,115!
2249
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
2,115!
2250
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2251
      }
2252
      break;
2,115✔
2253
    default:
2✔
2254
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
2✔
2255
      break;
2✔
2256
  }
2257
  return pOptions;
4,676✔
2258
_err:
×
2259
  nodesDestroyNode(pOptions);
×
2260
  return NULL;
×
2261
}
2262

2263
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
591,822✔
2264
  CHECK_PARSER_STATUS(pCxt);
591,822!
2265
  CHECK_NAME(checkColumnName(pCxt, pColName));
591,822✔
2266
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
591,816✔
2267
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
20✔
2268
    CHECK_PARSER_STATUS(pCxt);
20!
2269
  }
2270
  SColumnDefNode* pCol = NULL;
591,796✔
2271
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
591,796✔
2272
  CHECK_MAKE_NODE(pCol);
591,796!
2273
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
591,796✔
2274
  pCol->dataType = dataType;
591,796✔
2275
  pCol->pOptions = pNode;
591,796✔
2276
  pCol->sma = true;
591,796✔
2277
  return (SNode*)pCol;
591,796✔
2278
_err:
26✔
2279
  nodesDestroyNode(pNode);
26✔
2280
  return NULL;
26✔
2281
}
2282

2283
SDataType createDataType(uint8_t type) {
598,635✔
2284
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
598,635✔
2285
  return dt;
598,635✔
2286
}
2287

2288
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
827,359✔
2289
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
827,359✔
2290
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
827,359✔
2291
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
827,359✔
2292
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
827,359✔
2293
  return dt;
827,359✔
2294
}
2295

2296
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
20,069✔
2297
                             SNodeList* pTags, SNode* pOptions) {
2298
  CHECK_PARSER_STATUS(pCxt);
20,069✔
2299
  SCreateTableStmt* pStmt = NULL;
20,064✔
2300
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
20,064✔
2301
  CHECK_MAKE_NODE(pStmt);
20,064!
2302
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
20,064✔
2303
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
20,064✔
2304
  pStmt->ignoreExists = ignoreExists;
20,064✔
2305
  pStmt->pCols = pCols;
20,064✔
2306
  pStmt->pTags = pTags;
20,064✔
2307
  pStmt->pOptions = (STableOptions*)pOptions;
20,064✔
2308
  nodesDestroyNode(pRealTable);
20,064✔
2309
  return (SNode*)pStmt;
20,064✔
2310
_err:
5✔
2311
  nodesDestroyNode(pRealTable);
5✔
2312
  nodesDestroyList(pCols);
5✔
2313
  nodesDestroyList(pTags);
5✔
2314
  nodesDestroyNode(pOptions);
5✔
2315
  return NULL;
5✔
2316
}
2317

2318
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
82,759✔
2319
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
2320
  CHECK_PARSER_STATUS(pCxt);
82,759!
2321
  SCreateSubTableClause* pStmt = NULL;
82,759✔
2322
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
82,759✔
2323
  CHECK_MAKE_NODE(pStmt);
82,764!
2324
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
82,764✔
2325
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
82,764✔
2326
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
82,764✔
2327
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
82,764✔
2328
  pStmt->ignoreExists = ignoreExists;
82,764✔
2329
  pStmt->pSpecificTags = pSpecificTags;
82,764✔
2330
  pStmt->pValsOfTags = pValsOfTags;
82,764✔
2331
  pStmt->pOptions = (STableOptions*)pOptions;
82,764✔
2332
  nodesDestroyNode(pRealTable);
82,764✔
2333
  nodesDestroyNode(pUseRealTable);
82,744✔
2334
  return (SNode*)pStmt;
82,732✔
2335
_err:
×
2336
  nodesDestroyNode(pRealTable);
×
2337
  nodesDestroyNode(pOptions);
×
2338
  nodesDestroyNode(pUseRealTable);
×
2339
  nodesDestroyList(pSpecificTags);
×
2340
  nodesDestroyList(pValsOfTags);
×
UNCOV
2341
  return NULL;
×
2342
}
2343

2344
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
×
2345
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
2346
  CHECK_PARSER_STATUS(pCxt);
×
2347
  SCreateSubTableFromFileClause* pStmt = NULL;
×
2348
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
×
2349
  CHECK_MAKE_NODE(pStmt);
×
2350
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
×
2351
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
×
2352
  pStmt->ignoreExists = ignoreExists;
×
2353
  pStmt->pSpecificTags = pSpecificTags;
×
2354
  if (TK_NK_STRING == pFilePath->type) {
×
2355
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
×
2356
  } else {
2357
    strncpy(pStmt->filePath, pFilePath->z, pFilePath->n);
×
2358
  }
2359

2360
  nodesDestroyNode(pUseRealTable);
×
2361
  return (SNode*)pStmt;
×
2362
_err:
×
2363
  nodesDestroyNode(pUseRealTable);
×
2364
  nodesDestroyList(pSpecificTags);
×
2365
  return NULL;
×
2366
}
2367

2368
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
48,864✔
2369
  CHECK_PARSER_STATUS(pCxt);
48,864!
2370
  SCreateMultiTablesStmt* pStmt = NULL;
48,864✔
2371
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
48,864✔
2372
  CHECK_MAKE_NODE(pStmt);
48,868!
2373
  pStmt->pSubTables = pSubTables;
48,868✔
2374
  return (SNode*)pStmt;
48,868✔
2375
_err:
×
2376
  nodesDestroyList(pSubTables);
×
2377
  return NULL;
×
2378
}
2379

2380
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
18,532✔
2381
  CHECK_PARSER_STATUS(pCxt);
18,532✔
2382
  SDropTableClause* pStmt = NULL;
18,531✔
2383
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
18,531✔
2384
  CHECK_MAKE_NODE(pStmt);
18,531!
2385
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
18,531✔
2386
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
18,531✔
2387
  pStmt->ignoreNotExists = ignoreNotExists;
18,531✔
2388
  nodesDestroyNode(pRealTable);
18,531✔
2389
  return (SNode*)pStmt;
18,531✔
2390
_err:
1✔
2391
  nodesDestroyNode(pRealTable);
1✔
2392
  return NULL;
1✔
2393
}
2394

2395
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
17,610✔
2396
  CHECK_PARSER_STATUS(pCxt);
17,610✔
2397
  SDropTableStmt* pStmt = NULL;
17,609✔
2398
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
17,609✔
2399
  CHECK_MAKE_NODE(pStmt);
17,609!
2400
  pStmt->pTables = pTables;
17,609✔
2401
  pStmt->withOpt = withOpt;
17,609✔
2402
  return (SNode*)pStmt;
17,609✔
2403
_err:
1✔
2404
  nodesDestroyList(pTables);
1✔
2405
  return NULL;
1✔
2406
}
2407

2408
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
339✔
2409
  CHECK_PARSER_STATUS(pCxt);
339!
2410
  SDropSuperTableStmt* pStmt = NULL;
339✔
2411
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
339✔
2412
  CHECK_MAKE_NODE(pStmt);
339!
2413
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
339✔
2414
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
339✔
2415
  pStmt->ignoreNotExists = ignoreNotExists;
339✔
2416
  pStmt->withOpt = withOpt;
339✔
2417
  nodesDestroyNode(pRealTable);
339✔
2418
  return (SNode*)pStmt;
339✔
2419
_err:
×
2420
  nodesDestroyNode(pRealTable);
×
2421
  return NULL;
×
2422
}
2423

2424
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
14,710✔
2425
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
14,710✔
2426
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
14,710✔
2427
  nodesDestroyNode(pRealTable);
14,710✔
2428
  return (SNode*)pStmt;
14,710✔
2429
}
2430

2431
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
129✔
2432
  CHECK_PARSER_STATUS(pCxt);
129✔
2433
  SAlterTableStmt* pStmt = NULL;
116✔
2434
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
116✔
2435
  CHECK_MAKE_NODE(pStmt);
116!
2436
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
116✔
2437
  pStmt->pOptions = (STableOptions*)pOptions;
116✔
2438
  return createAlterTableStmtFinalize(pRealTable, pStmt);
116✔
2439
_err:
13✔
2440
  nodesDestroyNode(pRealTable);
13✔
2441
  nodesDestroyNode(pOptions);
13✔
2442
  return NULL;
13✔
2443
}
2444

2445
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
4,263✔
2446
                                    SDataType dataType) {
2447
  CHECK_PARSER_STATUS(pCxt);
4,263!
2448
  CHECK_NAME(checkColumnName(pCxt, pColName));
4,263✔
2449
  SAlterTableStmt* pStmt = NULL;
4,262✔
2450
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
4,262✔
2451
  CHECK_MAKE_NODE(pStmt);
4,262!
2452
  pStmt->alterType = alterType;
4,262✔
2453
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
4,262✔
2454
  pStmt->dataType = dataType;
4,262✔
2455
  return createAlterTableStmtFinalize(pRealTable, pStmt);
4,262✔
2456
_err:
1✔
2457
  nodesDestroyNode(pRealTable);
1✔
2458
  return NULL;
1✔
2459
}
2460
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
2,639✔
2461
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
2462
  SAlterTableStmt* pStmt = NULL;
2,639✔
2463
  CHECK_PARSER_STATUS(pCxt);
2,639✔
2464
  CHECK_NAME(checkColumnName(pCxt, pColName));
2,637✔
2465
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
2,636✔
2466
  CHECK_MAKE_NODE(pStmt);
2,636!
2467
  pStmt->alterType = alterType;
2,636✔
2468
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
2,636✔
2469
  pStmt->dataType = dataType;
2,636✔
2470
  pStmt->pColOptions = (SColumnOptions*)pOptions;
2,636✔
2471

2472
  if (pOptions != NULL) {
2,636!
2473
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
2,636✔
2474
    if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
2,636!
2475
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
2,632✔
2476
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
219✔
2477
      } else {
2478
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
2479
        //                                         "not support alter column with option except compress");
2480
        // return NULL;
2481
      }
2482
    } else {
2483
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
4✔
2484
                                              "not support alter column with option except compress");
2485
      CHECK_PARSER_STATUS(pCxt);
4!
2486
    }
2487
  }
2488
  return createAlterTableStmtFinalize(pRealTable, pStmt);
2,632✔
2489
_err:
7✔
2490
  nodesDestroyNode(pOptions);
7✔
2491
  nodesDestroyNode((SNode*)pStmt);
7✔
2492
  nodesDestroyNode(pRealTable);
7✔
2493
  return NULL;
7✔
2494
}
2495

2496
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
102✔
2497
                                           SToken* pColName, SNode* pOptions) {
2498
  CHECK_PARSER_STATUS(pCxt);
102!
2499
  CHECK_NAME(checkColumnName(pCxt, pColName));
102!
2500
  SAlterTableStmt* pStmt = NULL;
102✔
2501
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
102✔
2502
  CHECK_MAKE_NODE(pStmt);
102!
2503
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
102✔
2504
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
102✔
2505
  pStmt->pColOptions = (SColumnOptions*)pOptions;
102✔
2506
  return createAlterTableStmtFinalize(pRealTable, pStmt);
102✔
2507
_err:
×
2508
  nodesDestroyNode(pOptions);
×
2509
  nodesDestroyNode(pRealTable);
×
2510
  return NULL;
×
2511
}
2512

2513
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
640✔
2514
  CHECK_PARSER_STATUS(pCxt);
640!
2515
  CHECK_NAME(checkColumnName(pCxt, pColName));
640✔
2516
  SAlterTableStmt* pStmt = NULL;
638✔
2517
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
638✔
2518
  CHECK_MAKE_NODE(pStmt);
638!
2519
  pStmt->alterType = alterType;
638✔
2520
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
638✔
2521
  return createAlterTableStmtFinalize(pRealTable, pStmt);
638✔
2522
_err:
2✔
2523
  nodesDestroyNode(pRealTable);
2✔
2524
  return NULL;
2✔
2525
}
2526

2527
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
216✔
2528
                                 SToken* pNewColName) {
2529
  CHECK_PARSER_STATUS(pCxt);
216!
2530
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
216!
2531
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
216✔
2532
  SAlterTableStmt* pStmt = NULL;
214✔
2533
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
214✔
2534
  CHECK_MAKE_NODE(pStmt);
214!
2535
  pStmt->alterType = alterType;
214✔
2536
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
214✔
2537
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
214✔
2538
  return createAlterTableStmtFinalize(pRealTable, pStmt);
214✔
2539
_err:
2✔
2540
  nodesDestroyNode(pRealTable);
2✔
2541
  return NULL;
2✔
2542
}
2543

2544
SNode* createAlterSingleTagColumnNode(SAstCreateContext* pCtx, SToken* pTagName, SNode* pVal) {
6,799✔
2545
  CHECK_PARSER_STATUS(pCtx);
6,799!
2546
  SAlterTableStmt* pStmt = NULL;
6,799✔
2547
  pCtx->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
6,799✔
2548
  CHECK_MAKE_NODE(pStmt);
6,799!
2549
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
6,799✔
2550
  CHECK_NAME(checkColumnName(pCtx, pTagName));
6,799!
2551
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
6,799✔
2552
  pStmt->pVal = (SValueNode*)pVal;
6,799✔
2553
  pStmt->pNodeListTagValue = NULL;
6,799✔
2554
  return (SNode*)pStmt;
6,799✔
NEW
2555
_err:
×
NEW
2556
  return NULL;
×
2557
}
2558

UNCOV
2559
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
×
UNCOV
2560
  CHECK_PARSER_STATUS(pCxt);
×
UNCOV
2561
  CHECK_NAME(checkColumnName(pCxt, pTagName));
×
UNCOV
2562
  SAlterTableStmt* pStmt = NULL;
×
UNCOV
2563
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
UNCOV
2564
  CHECK_MAKE_NODE(pStmt);
×
UNCOV
2565
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
×
UNCOV
2566
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
×
UNCOV
2567
  pStmt->pVal = (SValueNode*)pVal;
×
UNCOV
2568
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
UNCOV
2569
_err:
×
UNCOV
2570
  nodesDestroyNode(pVal);
×
UNCOV
2571
  nodesDestroyNode(pRealTable);
×
UNCOV
2572
  return NULL;
×
2573
}
2574

2575
SNode* createAlterTableSetMultiTagValue(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pList) {
6,746✔
2576
  CHECK_PARSER_STATUS(pCxt);
6,746!
2577
  SAlterTableStmt* pStmt = NULL;
6,746✔
2578
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
6,746✔
2579

2580
  CHECK_MAKE_NODE(pStmt);
6,746!
2581
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL;
6,746✔
2582
  pStmt->pNodeListTagValue = pList;
6,746✔
2583
  return createAlterTableStmtFinalize(pRealTable, pStmt);
6,746✔
NEW
2584
_err:
×
NEW
2585
  return NULL;
×
2586
}
2587

2588
SNode* setAlterSuperTableType(SNode* pStmt) {
1,023✔
2589
  if (!pStmt) return NULL;
1,023✔
2590
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
1,020✔
2591
  return pStmt;
1,020✔
2592
}
2593

2594
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
9,852✔
2595
  CHECK_PARSER_STATUS(pCxt);
9,852!
2596
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
9,852!
2597
  SUseDatabaseStmt* pStmt = NULL;
9,855✔
2598
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
9,855✔
2599
  CHECK_MAKE_NODE(pStmt);
9,854!
2600
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
9,854✔
2601
  return (SNode*)pStmt;
9,854✔
2602
_err:
×
2603
  return NULL;
×
2604
}
2605

2606
static bool needDbShowStmt(ENodeType type) {
3,757✔
2607
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
2,532✔
2608
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
890✔
2609
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
32!
2610
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type;
6,289!
2611
}
2612

2613
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
2,656✔
2614
  CHECK_PARSER_STATUS(pCxt);
2,656!
2615
  SShowStmt* pStmt = NULL;
2,656✔
2616
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2,656✔
2617
  CHECK_MAKE_NODE(pStmt);
2,656!
2618
  pStmt->withFull = false;
2,656✔
2619
  return (SNode*)pStmt;
2,656✔
2620
_err:
×
2621
  return NULL;
×
2622
}
2623

2624
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
×
2625
  CHECK_PARSER_STATUS(pCxt);
×
2626
  SShowStmt* pStmt = NULL;
×
2627
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
2628
  CHECK_MAKE_NODE(pStmt);
×
2629
  pStmt->withFull = true;
×
2630
  return (SNode*)pStmt;
×
2631
_err:
×
2632
  return NULL;
×
2633
}
2634

2635
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
162✔
2636
  CHECK_PARSER_STATUS(pCxt);
162!
2637
  SShowCompactsStmt* pStmt = NULL;
162✔
2638
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
162✔
2639
  CHECK_MAKE_NODE(pStmt);
162!
2640
  return (SNode*)pStmt;
162✔
2641
_err:
×
2642
  return NULL;
×
2643
}
2644

2645
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
1,469✔
2646
  if (pStmt == NULL) {
1,469!
2647
    return NULL;
×
2648
  }
2649
  SShowStmt* pShow = (SShowStmt*)pStmt;
1,469✔
2650
  pShow->showKind = showKind;
1,469✔
2651
  return pStmt;
1,469✔
2652
}
2653

2654
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
3,757✔
2655
                              EOperatorType tableCondType) {
2656
  CHECK_PARSER_STATUS(pCxt);
3,757!
2657
  if (needDbShowStmt(type) && NULL == pDbName) {
3,757!
2658
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
2659
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2660
    CHECK_PARSER_STATUS(pCxt);
×
2661
  }
2662
  SShowStmt* pStmt = NULL;
3,757✔
2663
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
3,757✔
2664
  CHECK_MAKE_NODE(pStmt);
3,757!
2665
  pStmt->pDbName = pDbName;
3,757✔
2666
  pStmt->pTbName = pTbName;
3,757✔
2667
  pStmt->tableCondType = tableCondType;
3,757✔
2668
  return (SNode*)pStmt;
3,757✔
2669
_err:
×
2670
  nodesDestroyNode(pDbName);
×
2671
  nodesDestroyNode(pTbName);
×
2672
  return NULL;
×
2673
}
2674

2675
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
1,225✔
2676
                            EOperatorType tableCondType) {
2677
  CHECK_PARSER_STATUS(pCxt);
1,225!
2678
  SNode* pDbName = NULL;
1,225✔
2679
  if (option.dbName.type == TK_NK_NIL) {
1,225✔
2680
    pDbName = createDefaultDatabaseCondValue(pCxt);
1,028✔
2681
  } else {
2682
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
197✔
2683
  }
2684
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
1,225✔
2685
  CHECK_PARSER_STATUS(pCxt);
1,225!
2686
  (void)setShowKind(pCxt, pStmt, option.kind);
1,225✔
2687
  return pStmt;
1,225✔
2688
_err:
×
2689
  nodesDestroyNode(pTbName);
×
2690
  return NULL;
×
2691
}
2692

2693
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
25✔
2694
  CHECK_PARSER_STATUS(pCxt);
25!
2695
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
25!
2696
  SShowCreateDatabaseStmt* pStmt = NULL;
25✔
2697
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
25✔
2698
  CHECK_MAKE_NODE(pStmt);
25!
2699
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
25✔
2700
  return (SNode*)pStmt;
25✔
2701
_err:
×
2702
  return NULL;
×
2703
}
2704

2705
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
31✔
2706
  CHECK_PARSER_STATUS(pCxt);
31!
2707
  SToken  dbToken = {0};
31✔
2708
  SToken* pDbToken = NULL;
31✔
2709

2710
  if (pNode) {
31✔
2711
    SValueNode* pDbName = (SValueNode*)pNode;
5✔
2712
    if (pDbName->literal) {
5!
2713
      dbToken.z = pDbName->literal;
5✔
2714
      dbToken.n = strlen(pDbName->literal);
5✔
2715
      pDbToken = &dbToken;
5✔
2716
    }
2717
  }
2718

2719
  if (pDbToken) {
31✔
2720
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
5!
2721
  }
2722

2723
  SShowAliveStmt* pStmt = NULL;
31✔
2724
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
31✔
2725
  CHECK_PARSER_STATUS(pCxt);
31!
2726

2727
  if (pDbToken) {
31✔
2728
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
5✔
2729
  }
2730
  if (pNode) {
31✔
2731
    nodesDestroyNode(pNode);
5✔
2732
  }
2733

2734
  return (SNode*)pStmt;
31✔
2735
_err:
×
2736
  nodesDestroyNode(pNode);
×
2737
  return NULL;
×
2738
}
2739

2740
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
145✔
2741
  CHECK_PARSER_STATUS(pCxt);
145✔
2742
  SShowCreateTableStmt* pStmt = NULL;
143✔
2743
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
143✔
2744
  CHECK_MAKE_NODE(pStmt);
143!
2745
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
143✔
2746
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
143✔
2747
  nodesDestroyNode(pRealTable);
143✔
2748
  return (SNode*)pStmt;
143✔
2749
_err:
2✔
2750
  nodesDestroyNode(pRealTable);
2✔
2751
  return NULL;
2✔
2752
}
2753

2754
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
14✔
2755
  CHECK_PARSER_STATUS(pCxt);
14!
2756
  SShowCreateViewStmt* pStmt = NULL;
14✔
2757
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
14✔
2758
  CHECK_MAKE_NODE(pStmt);
14!
2759
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
14✔
2760
  strcpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName);
14✔
2761
  nodesDestroyNode(pRealTable);
14✔
2762
  return (SNode*)pStmt;
14✔
2763
_err:
×
2764
  nodesDestroyNode(pRealTable);
×
2765
  return NULL;
×
2766
}
2767

2768
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
9✔
2769
  CHECK_PARSER_STATUS(pCxt);
9!
2770
  SShowTableDistributedStmt* pStmt = NULL;
9✔
2771
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
9✔
2772
  CHECK_MAKE_NODE(pStmt);
9!
2773
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
9✔
2774
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
9✔
2775
  nodesDestroyNode(pRealTable);
9✔
2776
  return (SNode*)pStmt;
9✔
2777
_err:
×
2778
  nodesDestroyNode(pRealTable);
×
2779
  return NULL;
×
2780
}
2781

2782
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
74✔
2783
  CHECK_PARSER_STATUS(pCxt);
74!
2784
  SShowDnodeVariablesStmt* pStmt = NULL;
74✔
2785
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
74✔
2786
  CHECK_MAKE_NODE(pStmt);
74!
2787
  pStmt->pDnodeId = pDnodeId;
74✔
2788
  pStmt->pLikePattern = pLikePattern;
74✔
2789
  return (SNode*)pStmt;
74✔
2790
_err:
×
2791
  nodesDestroyNode(pDnodeId);
×
2792
  nodesDestroyNode(pLikePattern);
×
2793
  return NULL;
×
2794
}
2795

2796
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
10✔
2797
  CHECK_PARSER_STATUS(pCxt);
10!
2798
  SShowVnodesStmt* pStmt = NULL;
10✔
2799
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
10✔
2800
  CHECK_MAKE_NODE(pStmt);
10!
2801
  pStmt->pDnodeId = pDnodeId;
10✔
2802
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
10✔
2803
  return (SNode*)pStmt;
10✔
2804
_err:
×
2805
  nodesDestroyNode(pDnodeId);
×
2806
  nodesDestroyNode(pDnodeEndpoint);
×
2807
  return NULL;
×
2808
}
2809

2810
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
30✔
2811
  CHECK_PARSER_STATUS(pCxt);
30!
2812
  if (NULL == pDbName) {
30!
2813
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
2814
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2815
    CHECK_PARSER_STATUS(pCxt);
×
2816
  }
2817
  SShowTableTagsStmt* pStmt = NULL;
30✔
2818
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
30✔
2819
  CHECK_MAKE_NODE(pStmt);
30!
2820
  pStmt->pDbName = pDbName;
30✔
2821
  pStmt->pTbName = pTbName;
30✔
2822
  pStmt->pTags = pTags;
30✔
2823
  return (SNode*)pStmt;
30✔
2824
_err:
×
2825
  nodesDestroyNode(pTbName);
×
2826
  nodesDestroyNode(pDbName);
×
2827
  nodesDestroyList(pTags);
×
2828
  return NULL;
×
2829
}
2830

2831
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
1✔
2832
  CHECK_PARSER_STATUS(pCxt);
1!
2833
  SShowCompactDetailsStmt* pStmt = NULL;
1✔
2834
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
1✔
2835
  CHECK_MAKE_NODE(pStmt);
1!
2836
  pStmt->pCompactId = pCompactId;
1✔
2837
  return (SNode*)pStmt;
1✔
2838
_err:
×
2839
  nodesDestroyNode(pCompactId);
×
2840
  return NULL;
×
2841
}
2842

2843
static int32_t getIpV4RangeFromWhitelistItem(char* ipRange, SIpV4Range* pIpRange) {
8✔
2844
  int32_t code = TSDB_CODE_SUCCESS;
8✔
2845
  char*   ipCopy = taosStrdup(ipRange);
8✔
2846
  if (!ipCopy) return terrno;
8!
2847
  char* slash = strchr(ipCopy, '/');
8✔
2848
  if (slash) {
8✔
2849
    *slash = '\0';
6✔
2850
    struct in_addr addr;
2851
    if (uv_inet_pton(AF_INET, ipCopy, &addr) == 0) {
6✔
2852
      int prefix = atoi(slash + 1);
5✔
2853
      if (prefix < 0 || prefix > 32) {
5!
2854
        code = TSDB_CODE_PAR_INVALID_IP_RANGE;
1✔
2855
      } else {
2856
        pIpRange->ip = addr.s_addr;
4✔
2857
        pIpRange->mask = prefix;
4✔
2858
        code = TSDB_CODE_SUCCESS;
4✔
2859
      }
2860
    } else {
2861
      code = TSDB_CODE_PAR_INVALID_IP_RANGE;
1✔
2862
    }
2863
  } else {
2864
    struct in_addr addr;
2865
    if (uv_inet_pton(AF_INET, ipCopy, &addr) == 0) {
2!
2866
      pIpRange->ip = addr.s_addr;
2✔
2867
      pIpRange->mask = 32;
2✔
2868
      code = TSDB_CODE_SUCCESS;
2✔
2869
    } else {
2870
      code = TSDB_CODE_PAR_INVALID_IP_RANGE;
×
2871
    }
2872
  }
2873

2874
  taosMemoryFreeClear(ipCopy);
8!
2875
  return code;
8✔
2876
}
2877

2878
static int32_t fillIpRangesFromWhiteList(SAstCreateContext* pCxt, SNodeList* pIpRangesNodeList, SIpV4Range* pIpRanges) {
6✔
2879
  int32_t i = 0;
6✔
2880
  int32_t code = 0;
6✔
2881

2882
  SNode* pNode = NULL;
6✔
2883
  FOREACH(pNode, pIpRangesNodeList) {
12!
2884
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
8!
2885
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IP_RANGE);
×
2886
      return TSDB_CODE_PAR_INVALID_IP_RANGE;
×
2887
    }
2888
    SValueNode* pValNode = (SValueNode*)(pNode);
8✔
2889
    code = getIpV4RangeFromWhitelistItem(pValNode->literal, pIpRanges + i);
8✔
2890
    ++i;
8✔
2891
    if (code != TSDB_CODE_SUCCESS) {
8✔
2892
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid IP range %s", pValNode->literal);
2✔
2893
      return code;
2✔
2894
    }
2895
  }
2896
  return TSDB_CODE_SUCCESS;
4✔
2897
}
2898

2899
SNode* addCreateUserStmtWhiteList(SAstCreateContext* pCxt, SNode* pCreateUserStmt, SNodeList* pIpRangesNodeList) {
149✔
2900
  if (NULL == pCreateUserStmt || NULL == pIpRangesNodeList) {
149✔
2901
    return pCreateUserStmt;
145✔
2902
  }
2903

2904
  ((SCreateUserStmt*)pCreateUserStmt)->pNodeListIpRanges = pIpRangesNodeList;
4✔
2905
  SCreateUserStmt* pCreateUser = (SCreateUserStmt*)pCreateUserStmt;
4✔
2906
  pCreateUser->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
4!
2907
  pCreateUser->pIpRanges = taosMemoryMalloc(pCreateUser->numIpRanges * sizeof(SIpV4Range));
4✔
2908
  CHECK_OUT_OF_MEM(pCreateUser->pIpRanges);
4!
2909

2910
  pCxt->errCode = fillIpRangesFromWhiteList(pCxt, pIpRangesNodeList, pCreateUser->pIpRanges);
4✔
2911
  CHECK_PARSER_STATUS(pCxt);
4✔
2912

2913
  return pCreateUserStmt;
2✔
2914
_err:
2✔
2915
  nodesDestroyNode(pCreateUserStmt);
2✔
2916
  nodesDestroyList(pIpRangesNodeList);
2✔
2917
  return NULL;
2✔
2918
}
2919

2920
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo,
149✔
2921
                            int8_t createDb, int8_t is_import) {
2922
  CHECK_PARSER_STATUS(pCxt);
149!
2923
  char password[TSDB_USET_PASSWORD_LEN + 3] = {0};
149✔
2924
  CHECK_NAME(checkUserName(pCxt, pUserName));
149✔
2925
  CHECK_NAME(checkPassword(pCxt, pPassword, password));
147!
2926
  SCreateUserStmt* pStmt = NULL;
147✔
2927
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
147✔
2928
  CHECK_MAKE_NODE(pStmt);
147!
2929
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
147✔
2930
  strcpy(pStmt->password, password);
147✔
2931
  pStmt->sysinfo = sysinfo;
147✔
2932
  pStmt->createDb = createDb;
147✔
2933
  pStmt->isImport = is_import;
147✔
2934
  return (SNode*)pStmt;
147✔
2935
_err:
2✔
2936
  return NULL;
2✔
2937
}
2938

2939
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, void* pAlterInfo) {
90✔
2940
  SAlterUserStmt* pStmt = NULL;
90✔
2941
  CHECK_PARSER_STATUS(pCxt);
90!
2942
  CHECK_NAME(checkUserName(pCxt, pUserName));
90!
2943
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
90✔
2944
  CHECK_MAKE_NODE(pStmt);
90!
2945
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
90✔
2946
  pStmt->alterType = alterType;
90✔
2947
  switch (alterType) {
90!
2948
    case TSDB_ALTER_USER_PASSWD: {
32✔
2949
      char    password[TSDB_USET_PASSWORD_LEN] = {0};
32✔
2950
      SToken* pVal = pAlterInfo;
32✔
2951
      CHECK_NAME(checkPassword(pCxt, pVal, password));
32!
2952
      strcpy(pStmt->password, password);
32✔
2953
      break;
32✔
2954
    }
2955
    case TSDB_ALTER_USER_ENABLE: {
15✔
2956
      SToken* pVal = pAlterInfo;
15✔
2957
      pStmt->enable = taosStr2Int8(pVal->z, NULL, 10);
15✔
2958
      break;
15✔
2959
    }
2960
    case TSDB_ALTER_USER_SYSINFO: {
19✔
2961
      SToken* pVal = pAlterInfo;
19✔
2962
      pStmt->sysinfo = taosStr2Int8(pVal->z, NULL, 10);
19✔
2963
      break;
19✔
2964
    }
2965
    case TSDB_ALTER_USER_CREATEDB: {
22✔
2966
      SToken* pVal = pAlterInfo;
22✔
2967
      pStmt->createdb = taosStr2Int8(pVal->z, NULL, 10);
22✔
2968
      break;
22✔
2969
    }
2970
    case TSDB_ALTER_USER_ADD_WHITE_LIST:
2✔
2971
    case TSDB_ALTER_USER_DROP_WHITE_LIST: {
2972
      SNodeList* pIpRangesNodeList = pAlterInfo;
2✔
2973
      pStmt->pNodeListIpRanges = pIpRangesNodeList;
2✔
2974
      pStmt->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
2!
2975
      pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpV4Range));
2✔
2976
      CHECK_OUT_OF_MEM(pStmt->pIpRanges);
2!
2977

2978
      pCxt->errCode = fillIpRangesFromWhiteList(pCxt, pIpRangesNodeList, pStmt->pIpRanges);
2✔
2979
      CHECK_PARSER_STATUS(pCxt);
2!
2980
      break;
2✔
2981
    }
2982
    default:
×
2983
      break;
×
2984
  }
2985
  return (SNode*)pStmt;
90✔
2986
_err:
×
2987
  nodesDestroyNode((SNode*)pStmt);
×
2988
  return NULL;
×
2989
}
2990

2991
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
51✔
2992
  CHECK_PARSER_STATUS(pCxt);
51!
2993
  CHECK_NAME(checkUserName(pCxt, pUserName));
51!
2994
  SDropUserStmt* pStmt = NULL;
51✔
2995
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
51✔
2996
  CHECK_MAKE_NODE(pStmt);
51!
2997
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
51✔
2998
  return (SNode*)pStmt;
51✔
2999
_err:
×
3000
  return NULL;
×
3001
}
3002

3003
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
512✔
3004
  CHECK_PARSER_STATUS(pCxt);
512!
3005
  SCreateDnodeStmt* pStmt = NULL;
512✔
3006
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
512✔
3007
  CHECK_MAKE_NODE(pStmt);
512!
3008
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
512!
3009
    nodesDestroyNode((SNode*)pStmt);
×
3010
    return NULL;
×
3011
  }
3012
  return (SNode*)pStmt;
512✔
3013
_err:
×
3014
  return NULL;
×
3015
}
3016

3017
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
30✔
3018
  CHECK_PARSER_STATUS(pCxt);
30!
3019
  SDropDnodeStmt* pStmt = NULL;
30✔
3020
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
30✔
3021
  CHECK_MAKE_NODE(pStmt);
30!
3022
  if (TK_NK_INTEGER == pDnode->type) {
30!
3023
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
30✔
3024
  } else {
3025
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
×
3026
      nodesDestroyNode((SNode*)pStmt);
×
3027
      return NULL;
×
3028
    }
3029
  }
3030
  pStmt->force = force;
30✔
3031
  pStmt->unsafe = unsafe;
30✔
3032
  return (SNode*)pStmt;
30✔
3033
_err:
×
3034
  return NULL;
×
3035
}
3036

3037
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
204✔
3038
                            const SToken* pValue) {
3039
  CHECK_PARSER_STATUS(pCxt);
204!
3040
  SAlterDnodeStmt* pStmt = NULL;
204✔
3041
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
204✔
3042
  CHECK_MAKE_NODE(pStmt);
204!
3043
  if (NULL != pDnode) {
204✔
3044
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
142✔
3045
  } else {
3046
    pStmt->dnodeId = -1;
62✔
3047
  }
3048
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
204✔
3049
  if (NULL != pValue) {
204✔
3050
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
71✔
3051
  }
3052
  return (SNode*)pStmt;
204✔
3053
_err:
×
3054
  return NULL;
×
3055
}
3056

3057
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
×
3058
  CHECK_PARSER_STATUS(pCxt);
×
3059
  SCreateAnodeStmt* pStmt = NULL;
×
3060
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
×
3061
  CHECK_MAKE_NODE(pStmt);
×
3062
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
×
3063
  return (SNode*)pStmt;
×
3064
_err:
×
3065
  return NULL;
×
3066
}
3067

3068
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
×
3069
  CHECK_PARSER_STATUS(pCxt);
×
3070
  SUpdateAnodeStmt* pStmt = NULL;
×
3071
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
×
3072
  CHECK_MAKE_NODE(pStmt);
×
3073
  if (NULL != pAnode) {
×
3074
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
3075
  } else {
3076
    pStmt->anodeId = -1;
×
3077
  }
3078
  return (SNode*)pStmt;
×
3079
_err:
×
3080
  return NULL;
×
3081
}
3082

3083
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
3084
  CHECK_PARSER_STATUS(pCxt);
×
3085
  SUpdateAnodeStmt* pStmt = NULL;
×
3086
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
3087
  CHECK_MAKE_NODE(pStmt);
×
3088
  if (NULL != pAnode) {
×
3089
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
3090
  } else {
3091
    pStmt->anodeId = -1;
×
3092
  }
3093
  return (SNode*)pStmt;
×
3094
_err:
×
3095
  return NULL;
×
3096
}
3097

3098
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
4✔
3099
  SToken config;
3100
  config.type = TK_NK_STRING;
4✔
3101
  config.z = "\"encrypt_key\"";
4✔
3102
  config.n = strlen(config.z);
4✔
3103
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
4✔
3104
}
3105

3106
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
37✔
3107
  if (!checkIndexName(pCxt, pIndexName)) {
37!
3108
    return NULL;
×
3109
  }
3110
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
37✔
3111
}
3112

3113
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
86✔
3114
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
3115
  CHECK_PARSER_STATUS(pCxt);
86!
3116
  SCreateIndexStmt* pStmt = NULL;
86✔
3117
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
86✔
3118
  CHECK_MAKE_NODE(pStmt);
86!
3119
  pStmt->indexType = type;
86✔
3120
  pStmt->ignoreExists = ignoreExists;
86✔
3121

3122
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
86✔
3123
  if (strlen(pFullTable->table.dbName) == 0) {
86!
3124
    // no db specified,
3125
    if (pCxt->pQueryCxt->db == NULL) {
×
3126
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
3127
      CHECK_PARSER_STATUS(pCxt);
×
3128
    } else {
3129
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
3130
    }
3131
  } else {
3132
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
86✔
3133
  }
3134
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
86✔
3135
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
86✔
3136
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
86✔
3137
  nodesDestroyNode(pIndexName);
86✔
3138
  nodesDestroyNode(pRealTable);
86✔
3139
  pStmt->pCols = pCols;
86✔
3140
  pStmt->pOptions = (SIndexOptions*)pOptions;
86✔
3141
  return (SNode*)pStmt;
86✔
3142
_err:
×
3143
  nodesDestroyNode(pIndexName);
×
3144
  nodesDestroyNode(pRealTable);
×
3145
  nodesDestroyNode(pOptions);
×
3146
  nodesDestroyList(pCols);
×
3147
  return NULL;
×
3148
}
3149

3150
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
41✔
3151
                         SNode* pStreamOptions) {
3152
  CHECK_PARSER_STATUS(pCxt);
41!
3153
  SIndexOptions* pOptions = NULL;
41✔
3154
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
41✔
3155
  CHECK_MAKE_NODE(pOptions);
41!
3156
  pOptions->pFuncs = pFuncs;
41✔
3157
  pOptions->pInterval = pInterval;
41✔
3158
  pOptions->pOffset = pOffset;
41✔
3159
  pOptions->pSliding = pSliding;
41✔
3160
  pOptions->pStreamOptions = pStreamOptions;
41✔
3161
  return (SNode*)pOptions;
41✔
3162
_err:
×
3163
  nodesDestroyNode(pInterval);
×
3164
  nodesDestroyNode(pOffset);
×
3165
  nodesDestroyNode(pSliding);
×
3166
  nodesDestroyNode(pStreamOptions);
×
3167
  return NULL;
×
3168
}
3169

3170
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
37✔
3171
  CHECK_PARSER_STATUS(pCxt);
37!
3172
  SDropIndexStmt* pStmt = NULL;
37✔
3173
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
37✔
3174
  CHECK_MAKE_NODE(pStmt);
37!
3175
  pStmt->ignoreNotExists = ignoreNotExists;
37✔
3176
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
37✔
3177
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
37✔
3178
  nodesDestroyNode(pIndexName);
37✔
3179
  return (SNode*)pStmt;
37✔
3180
_err:
×
3181
  nodesDestroyNode(pIndexName);
×
3182
  return NULL;
×
3183
}
3184

3185
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
699✔
3186
  CHECK_PARSER_STATUS(pCxt);
699!
3187
  SCreateComponentNodeStmt* pStmt = NULL;
699✔
3188
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
699✔
3189
  CHECK_MAKE_NODE(pStmt);
699!
3190
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
699✔
3191
  return (SNode*)pStmt;
699✔
3192
_err:
×
3193
  return NULL;
×
3194
}
3195

3196
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
36✔
3197
  CHECK_PARSER_STATUS(pCxt);
36!
3198
  SDropComponentNodeStmt* pStmt = NULL;
36✔
3199
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
36✔
3200
  CHECK_MAKE_NODE(pStmt);
36!
3201
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
36✔
3202
  return (SNode*)pStmt;
36✔
3203
_err:
×
3204
  return NULL;
×
3205
}
3206

3207
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
4✔
3208
  CHECK_PARSER_STATUS(pCxt);
4!
3209
  SRestoreComponentNodeStmt* pStmt = NULL;
4✔
3210
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
4✔
3211
  CHECK_MAKE_NODE(pStmt);
4!
3212
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
4✔
3213
  return (SNode*)pStmt;
4✔
3214
_err:
×
3215
  return NULL;
×
3216
}
3217

3218
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery) {
512✔
3219
  CHECK_PARSER_STATUS(pCxt);
512!
3220
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
512✔
3221
  SCreateTopicStmt* pStmt = NULL;
510✔
3222
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
510✔
3223
  CHECK_MAKE_NODE(pStmt);
510!
3224
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
510✔
3225
  pStmt->ignoreExists = ignoreExists;
510✔
3226
  pStmt->pQuery = pQuery;
510✔
3227
  return (SNode*)pStmt;
510✔
3228
_err:
2✔
3229
  nodesDestroyNode(pQuery);
2✔
3230
  return NULL;
2✔
3231
}
3232

3233
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
76✔
3234
                                  int8_t withMeta) {
3235
  CHECK_PARSER_STATUS(pCxt);
76!
3236
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
76!
3237
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
76!
3238
  SCreateTopicStmt* pStmt = NULL;
76✔
3239
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
76✔
3240
  CHECK_MAKE_NODE(pStmt);
76!
3241
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
76✔
3242
  pStmt->ignoreExists = ignoreExists;
76✔
3243
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
76✔
3244
  pStmt->withMeta = withMeta;
76✔
3245
  return (SNode*)pStmt;
76✔
3246
_err:
×
3247
  return NULL;
×
3248
}
3249

3250
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
36✔
3251
                                     int8_t withMeta, SNode* pWhere) {
3252
  CHECK_PARSER_STATUS(pCxt);
36!
3253
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
36!
3254
  SCreateTopicStmt* pStmt = NULL;
36✔
3255
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
36✔
3256
  CHECK_MAKE_NODE(pStmt);
36!
3257
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
36✔
3258
  pStmt->ignoreExists = ignoreExists;
36✔
3259
  pStmt->withMeta = withMeta;
36✔
3260
  pStmt->pWhere = pWhere;
36✔
3261

3262
  strcpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName);
36✔
3263
  strcpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName);
36✔
3264
  nodesDestroyNode(pRealTable);
36✔
3265
  return (SNode*)pStmt;
36✔
3266
_err:
×
3267
  nodesDestroyNode(pRealTable);
×
3268
  nodesDestroyNode(pWhere);
×
3269
  return NULL;
×
3270
}
3271

3272
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName) {
349✔
3273
  CHECK_PARSER_STATUS(pCxt);
349!
3274
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
349✔
3275
  SDropTopicStmt* pStmt = NULL;
348✔
3276
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
348✔
3277
  CHECK_MAKE_NODE(pStmt);
348!
3278
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
348✔
3279
  pStmt->ignoreNotExists = ignoreNotExists;
348✔
3280
  return (SNode*)pStmt;
348✔
3281
_err:
1✔
3282
  return NULL;
1✔
3283
}
3284

3285
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName) {
5✔
3286
  CHECK_PARSER_STATUS(pCxt);
5!
3287
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
5!
3288
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
5!
3289
  SDropCGroupStmt* pStmt = NULL;
5✔
3290
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
5✔
3291
  CHECK_MAKE_NODE(pStmt);
5!
3292
  pStmt->ignoreNotExists = ignoreNotExists;
5✔
3293
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
5✔
3294
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
5✔
3295
  return (SNode*)pStmt;
5✔
3296
_err:
×
3297
  return NULL;
×
3298
}
3299

3300
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
6✔
3301
  CHECK_PARSER_STATUS(pCxt);
6!
3302
  SAlterClusterStmt* pStmt = NULL;
6✔
3303
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
6✔
3304
  CHECK_MAKE_NODE(pStmt);
6!
3305
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
6✔
3306
  if (NULL != pValue) {
6!
3307
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
6✔
3308
  }
3309
  return (SNode*)pStmt;
6✔
3310
_err:
×
3311
  return NULL;
×
3312
}
3313

3314
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
15,781✔
3315
  CHECK_PARSER_STATUS(pCxt);
15,781!
3316
  SAlterLocalStmt* pStmt = NULL;
15,781✔
3317
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
15,781✔
3318
  CHECK_MAKE_NODE(pStmt);
15,781!
3319
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
15,781✔
3320
  if (NULL != pValue) {
15,781✔
3321
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
15,757✔
3322
  }
3323
  return (SNode*)pStmt;
15,781✔
3324
_err:
×
3325
  return NULL;
×
3326
}
3327

3328
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
133,846✔
3329
  CHECK_PARSER_STATUS(pCxt);
133,846!
3330
  SExplainOptions* pOptions = NULL;
133,846✔
3331
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
133,846✔
3332
  CHECK_MAKE_NODE(pOptions);
133,846!
3333
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
133,846✔
3334
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
133,846✔
3335
  return (SNode*)pOptions;
133,846✔
3336
_err:
×
3337
  return NULL;
×
3338
}
3339

3340
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
40,499✔
3341
  CHECK_PARSER_STATUS(pCxt);
40,499!
3342
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
40,499✔
3343
  return pOptions;
40,499✔
3344
_err:
×
3345
  return NULL;
×
3346
}
3347

3348
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
12,240✔
3349
  CHECK_PARSER_STATUS(pCxt);
12,240!
3350
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
12,240✔
3351
  return pOptions;
12,240✔
3352
_err:
×
3353
  return NULL;
×
3354
}
3355

3356
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
133,182✔
3357
  CHECK_PARSER_STATUS(pCxt);
133,182!
3358
  SExplainStmt* pStmt = NULL;
133,182✔
3359
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
133,182✔
3360
  CHECK_MAKE_NODE(pStmt);
133,182!
3361
  pStmt->analyze = analyze;
133,182✔
3362
  pStmt->pOptions = (SExplainOptions*)pOptions;
133,182✔
3363
  pStmt->pQuery = pQuery;
133,182✔
3364
  return (SNode*)pStmt;
133,182✔
3365
_err:
×
3366
  nodesDestroyNode(pOptions);
×
3367
  nodesDestroyNode(pQuery);
×
3368
  return NULL;
×
3369
}
3370

3371
SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
3,448✔
3372
  CHECK_PARSER_STATUS(pCxt);
3,448!
3373
  SDescribeStmt* pStmt = NULL;
3,448✔
3374
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT, (SNode**)&pStmt);
3,448✔
3375
  CHECK_MAKE_NODE(pStmt);
3,448!
3376
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
3,448✔
3377
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
3,448✔
3378
  nodesDestroyNode(pRealTable);
3,448✔
3379
  return (SNode*)pStmt;
3,448✔
3380
_err:
×
3381
  nodesDestroyNode(pRealTable);
×
3382
  return NULL;
×
3383
}
3384

3385
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
7,736✔
3386
  CHECK_PARSER_STATUS(pCxt);
7,736!
3387
  SNode* pStmt = NULL;
7,736✔
3388
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
7,736✔
3389
  CHECK_MAKE_NODE(pStmt);
7,736!
3390
  return pStmt;
7,736✔
3391
_err:
×
3392
  return NULL;
×
3393
}
3394

3395
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
150✔
3396
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
150!
3397
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
95✔
3398
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
55!
3399
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
55✔
3400
  } else {
3401
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
3402
                                            "udf programming language supports c and python");
3403
  }
3404
  return pCxt->errCode;
150✔
3405
}
3406

3407
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
150✔
3408
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
3409
                                bool orReplace) {
3410
  CHECK_PARSER_STATUS(pCxt);
150!
3411
  if (pLibPath->n <= 2) {
150!
3412
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3413
    CHECK_PARSER_STATUS(pCxt);
×
3414
  }
3415
  int8_t language = 0;
150✔
3416
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
150✔
3417
  CHECK_PARSER_STATUS(pCxt);
150!
3418
  SCreateFunctionStmt* pStmt = NULL;
150✔
3419
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
150✔
3420
  CHECK_MAKE_NODE(pStmt);
150!
3421
  pStmt->orReplace = orReplace;
150✔
3422
  pStmt->ignoreExists = ignoreExists;
150✔
3423
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
150✔
3424
  pStmt->isAgg = aggFunc;
150✔
3425
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
150!
3426
  pStmt->outputDt = dataType;
150✔
3427
  pStmt->bufSize = bufSize;
150✔
3428
  pStmt->language = language;
150✔
3429
  return (SNode*)pStmt;
150✔
3430
_err:
×
3431
  return NULL;
×
3432
}
3433

3434
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
71✔
3435
  CHECK_PARSER_STATUS(pCxt);
71!
3436
  SDropFunctionStmt* pStmt = NULL;
71✔
3437
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
71✔
3438
  CHECK_MAKE_NODE(pStmt);
71!
3439
  pStmt->ignoreNotExists = ignoreNotExists;
71✔
3440
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
71✔
3441
  return (SNode*)pStmt;
71✔
3442
_err:
×
3443
  return NULL;
×
3444
}
3445

3446
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
226✔
3447
  SCreateViewStmt* pStmt = NULL;
226✔
3448
  CHECK_PARSER_STATUS(pCxt);
226!
3449
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
226✔
3450
  CHECK_MAKE_NODE(pStmt);
226!
3451
  int32_t i = pAs->n;
226✔
3452
  while (isspace(*(pAs->z + i))) {
452✔
3453
    ++i;
226✔
3454
  }
3455
  pStmt->pQuerySql = tstrdup(pAs->z + i);
226✔
3456
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
226!
3457
  strcpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName);
226✔
3458
  strcpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName);
226✔
3459
  nodesDestroyNode(pView);
226✔
3460
  pStmt->orReplace = orReplace;
226✔
3461
  pStmt->pQuery = pQuery;
226✔
3462
  return (SNode*)pStmt;
226✔
3463
_err:
×
3464
  nodesDestroyNode(pView);
×
3465
  nodesDestroyNode(pQuery);
×
3466
  nodesDestroyNode((SNode*)pStmt);
×
3467
  return NULL;
×
3468
}
3469

3470
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
145✔
3471
  CHECK_PARSER_STATUS(pCxt);
145✔
3472
  SDropViewStmt* pStmt = NULL;
143✔
3473
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
143✔
3474
  CHECK_MAKE_NODE(pStmt);
143!
3475
  pStmt->ignoreNotExists = ignoreNotExists;
143✔
3476
  strcpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName);
143✔
3477
  strcpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName);
143✔
3478
  nodesDestroyNode(pView);
143✔
3479
  return (SNode*)pStmt;
143✔
3480
_err:
2✔
3481
  nodesDestroyNode(pView);
2✔
3482
  return NULL;
2✔
3483
}
3484

3485
SNode* createStreamOptions(SAstCreateContext* pCxt) {
966✔
3486
  CHECK_PARSER_STATUS(pCxt);
966!
3487
  SStreamOptions* pOptions = NULL;
966✔
3488
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OPTIONS, (SNode**)&pOptions);
966✔
3489
  CHECK_MAKE_NODE(pOptions);
966!
3490
  pOptions->triggerType = STREAM_TRIGGER_WINDOW_CLOSE;
966✔
3491
  pOptions->fillHistory = STREAM_DEFAULT_FILL_HISTORY;
966✔
3492
  pOptions->ignoreExpired = STREAM_DEFAULT_IGNORE_EXPIRED;
966✔
3493
  pOptions->ignoreUpdate = STREAM_DEFAULT_IGNORE_UPDATE;
966✔
3494
  return (SNode*)pOptions;
966✔
3495
_err:
×
3496
  return NULL;
×
3497
}
3498

3499
static int8_t getTriggerType(uint32_t tokenType) {
874✔
3500
  switch (tokenType) {
874!
3501
    case TK_AT_ONCE:
715✔
3502
      return STREAM_TRIGGER_AT_ONCE;
715✔
3503
    case TK_WINDOW_CLOSE:
68✔
3504
      return STREAM_TRIGGER_WINDOW_CLOSE;
68✔
3505
    case TK_MAX_DELAY:
51✔
3506
      return STREAM_TRIGGER_MAX_DELAY;
51✔
3507
    case TK_FORCE_WINDOW_CLOSE:
40✔
3508
      return STREAM_TRIGGER_FORCE_WINDOW_CLOSE;
40✔
3509
    default:
×
3510
      break;
×
3511
  }
3512
  return STREAM_TRIGGER_WINDOW_CLOSE;
×
3513
}
3514

3515
SNode* setStreamOptions(SAstCreateContext* pCxt, SNode* pOptions, EStreamOptionsSetFlag setflag, SToken* pToken,
2,814✔
3516
                        SNode* pNode) {
3517
  SStreamOptions* pStreamOptions = (SStreamOptions*)pOptions;
2,814✔
3518
  if (BIT_FLAG_TEST_MASK(setflag, pStreamOptions->setFlag)) {
2,814!
3519
    pCxt->errCode =
×
3520
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream options each item is only set once");
×
3521
    return pOptions;
×
3522
  }
3523

3524
  switch (setflag) {
2,814!
3525
    case SOPT_TRIGGER_TYPE_SET:
874✔
3526
      pStreamOptions->triggerType = getTriggerType(pToken->type);
874✔
3527
      if (STREAM_TRIGGER_MAX_DELAY == pStreamOptions->triggerType) {
874✔
3528
        pStreamOptions->pDelay = pNode;
51✔
3529
      }
3530
      break;
874✔
3531
    case SOPT_WATERMARK_SET:
75✔
3532
      pStreamOptions->pWatermark = pNode;
75✔
3533
      break;
75✔
3534
    case SOPT_DELETE_MARK_SET:
4✔
3535
      pStreamOptions->pDeleteMark = pNode;
4✔
3536
      break;
4✔
3537
    case SOPT_FILL_HISTORY_SET:
301✔
3538
      pStreamOptions->fillHistory = taosStr2Int8(pToken->z, NULL, 10);
301✔
3539
      break;
301✔
3540
    case SOPT_IGNORE_EXPIRED_SET:
793✔
3541
      pStreamOptions->ignoreExpired = taosStr2Int8(pToken->z, NULL, 10);
793✔
3542
      break;
793✔
3543
    case SOPT_IGNORE_UPDATE_SET:
767✔
3544
      pStreamOptions->ignoreUpdate = taosStr2Int8(pToken->z, NULL, 10);
767✔
3545
      break;
767✔
3546
    default:
×
3547
      break;
×
3548
  }
3549
  BIT_FLAG_SET_MASK(pStreamOptions->setFlag, setflag);
2,814✔
3550

3551
  return pOptions;
2,814✔
3552
}
3553

3554
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pStreamName, SNode* pRealTable,
921✔
3555
                              SNode* pOptions, SNodeList* pTags, SNode* pSubtable, SNode* pQuery, SNodeList* pCols) {
3556
  CHECK_PARSER_STATUS(pCxt);
921!
3557
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
921✔
3558
  SCreateStreamStmt* pStmt = NULL;
920✔
3559
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
920✔
3560
  CHECK_MAKE_NODE(pStmt);
920!
3561
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
920✔
3562
  strcpy(pStmt->targetDbName, ((SRealTableNode*)pRealTable)->table.dbName);
920✔
3563
  strcpy(pStmt->targetTabName, ((SRealTableNode*)pRealTable)->table.tableName);
920✔
3564
  nodesDestroyNode(pRealTable);
920✔
3565
  pStmt->ignoreExists = ignoreExists;
920✔
3566
  pStmt->pOptions = (SStreamOptions*)pOptions;
920✔
3567
  pStmt->pQuery = pQuery;
920✔
3568
  pStmt->pTags = pTags;
920✔
3569
  pStmt->pSubtable = pSubtable;
920✔
3570
  pStmt->pCols = pCols;
920✔
3571
  return (SNode*)pStmt;
920✔
3572
_err:
1✔
3573
  nodesDestroyNode(pRealTable);
1✔
3574
  nodesDestroyNode(pQuery);
1✔
3575
  nodesDestroyNode(pOptions);
1✔
3576
  nodesDestroyList(pTags);
1✔
3577
  nodesDestroyNode(pSubtable);
1✔
3578
  nodesDestroyList(pCols);
1✔
3579
  return NULL;
1✔
3580
}
3581

3582
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
606✔
3583
  CHECK_PARSER_STATUS(pCxt);
606!
3584
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
606!
3585
  SDropStreamStmt* pStmt = NULL;
606✔
3586
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
606✔
3587
  CHECK_MAKE_NODE(pStmt);
606!
3588
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
606✔
3589
  pStmt->ignoreNotExists = ignoreNotExists;
606✔
3590
  return (SNode*)pStmt;
606✔
3591
_err:
×
3592
  return NULL;
×
3593
}
3594

3595
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
499✔
3596
  CHECK_PARSER_STATUS(pCxt);
499!
3597
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
499!
3598
  SPauseStreamStmt* pStmt = NULL;
499✔
3599
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
499✔
3600
  CHECK_MAKE_NODE(pStmt);
499!
3601
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
499✔
3602
  pStmt->ignoreNotExists = ignoreNotExists;
499✔
3603
  return (SNode*)pStmt;
499✔
3604
_err:
×
3605
  return NULL;
×
3606
}
3607

3608
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated,
567✔
3609
                              SToken* pStreamName) {
3610
  CHECK_PARSER_STATUS(pCxt);
567!
3611
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
567!
3612
  SResumeStreamStmt* pStmt = NULL;
567✔
3613
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
567✔
3614
  CHECK_MAKE_NODE(pStmt);
567!
3615
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
567✔
3616
  pStmt->ignoreNotExists = ignoreNotExists;
567✔
3617
  pStmt->ignoreUntreated = ignoreUntreated;
567✔
3618
  return (SNode*)pStmt;
567✔
3619
_err:
×
3620
  return NULL;
×
3621
}
3622

3623
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
2✔
3624
  CHECK_PARSER_STATUS(pCxt);
2!
3625
  SKillStmt* pStmt = NULL;
2✔
3626
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2✔
3627
  CHECK_MAKE_NODE(pStmt);
2!
3628
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
2✔
3629
  return (SNode*)pStmt;
2✔
3630
_err:
×
3631
  return NULL;
×
3632
}
3633

3634
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
×
3635
  CHECK_PARSER_STATUS(pCxt);
×
3636
  SKillQueryStmt* pStmt = NULL;
×
3637
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
×
3638
  CHECK_MAKE_NODE(pStmt);
×
3639
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
×
3640
  return (SNode*)pStmt;
×
3641
_err:
×
3642
  return NULL;
×
3643
}
3644

3645
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
19✔
3646
  CHECK_PARSER_STATUS(pCxt);
19!
3647
  SBalanceVgroupStmt* pStmt = NULL;
19✔
3648
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
19✔
3649
  CHECK_MAKE_NODE(pStmt);
19!
3650
  return (SNode*)pStmt;
19✔
3651
_err:
×
3652
  return NULL;
×
3653
}
3654

3655
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
6✔
3656
  CHECK_PARSER_STATUS(pCxt);
6!
3657
  SBalanceVgroupLeaderStmt* pStmt = NULL;
6✔
3658
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
6✔
3659
  CHECK_MAKE_NODE(pStmt);
6!
3660
  if (NULL != pVgId && NULL != pVgId->z) {
6!
3661
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
1✔
3662
  }
3663
  return (SNode*)pStmt;
6✔
3664
_err:
×
3665
  return NULL;
×
3666
}
3667

3668
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
3669
  CHECK_PARSER_STATUS(pCxt);
×
3670
  SBalanceVgroupLeaderStmt* pStmt = NULL;
×
3671
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
×
3672
  CHECK_MAKE_NODE(pStmt);
×
3673
  if (NULL != pDbName) {
×
3674
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
×
3675
  }
3676
  return (SNode*)pStmt;
×
3677
_err:
×
3678
  return NULL;
×
3679
}
3680

3681
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
×
3682
  CHECK_PARSER_STATUS(pCxt);
×
3683
  SMergeVgroupStmt* pStmt = NULL;
×
3684
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
×
3685
  CHECK_MAKE_NODE(pStmt);
×
3686
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
×
3687
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
×
3688
  return (SNode*)pStmt;
×
3689
_err:
×
3690
  return NULL;
×
3691
}
3692

3693
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
78✔
3694
  CHECK_PARSER_STATUS(pCxt);
78!
3695
  SRedistributeVgroupStmt* pStmt = NULL;
78✔
3696
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
78✔
3697
  CHECK_MAKE_NODE(pStmt);
78!
3698
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
78✔
3699
  pStmt->pDnodes = pDnodes;
78✔
3700
  return (SNode*)pStmt;
78✔
3701
_err:
×
3702
  nodesDestroyList(pDnodes);
×
3703
  return NULL;
×
3704
}
3705

3706
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
55✔
3707
  CHECK_PARSER_STATUS(pCxt);
55!
3708
  SSplitVgroupStmt* pStmt = NULL;
55✔
3709
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
55✔
3710
  CHECK_MAKE_NODE(pStmt);
55!
3711
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
55✔
3712
  return (SNode*)pStmt;
55✔
3713
_err:
×
3714
  return NULL;
×
3715
}
3716

3717
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
3718
  CHECK_PARSER_STATUS(pCxt);
×
3719
  SNode* pStmt = NULL;
×
3720
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
3721
  CHECK_MAKE_NODE(pStmt);
×
3722
  return pStmt;
×
3723
_err:
×
3724
  return NULL;
×
3725
}
3726

3727
SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
1,001✔
3728
                       SNode* pTagCond) {
3729
  CHECK_PARSER_STATUS(pCxt);
1,001!
3730
  CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
1,001!
3731
  CHECK_NAME(checkUserName(pCxt, pUserName));
1,001!
3732
  CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
1,001!
3733
  SGrantStmt* pStmt = NULL;
1,001✔
3734
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
1,001✔
3735
  CHECK_MAKE_NODE(pStmt);
1,001!
3736
  pStmt->privileges = privileges;
1,001✔
3737
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
1,001✔
3738
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
1,001✔
3739
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
879✔
3740
  }
3741
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1,001✔
3742
  pStmt->pTagCond = pTagCond;
1,001✔
3743
  return (SNode*)pStmt;
1,001✔
3744
_err:
×
3745
  nodesDestroyNode(pTagCond);
×
3746
  return NULL;
×
3747
}
3748

3749
SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
108✔
3750
                        SNode* pTagCond) {
3751
  CHECK_PARSER_STATUS(pCxt);
108!
3752
  CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
108!
3753
  CHECK_NAME(checkUserName(pCxt, pUserName));
108!
3754
  CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
108!
3755
  SRevokeStmt* pStmt = NULL;
108✔
3756
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
108✔
3757
  CHECK_MAKE_NODE(pStmt);
108!
3758
  pStmt->privileges = privileges;
108✔
3759
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
108✔
3760
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
108✔
3761
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
30✔
3762
  }
3763
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
108✔
3764
  pStmt->pTagCond = pTagCond;
108✔
3765
  return (SNode*)pStmt;
108✔
3766
_err:
×
3767
  nodesDestroyNode(pTagCond);
×
3768
  return NULL;
×
3769
}
3770

3771
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
169,455✔
3772
  SFunctionNode* pFunc = NULL;
169,455✔
3773
  CHECK_PARSER_STATUS(pCxt);
169,455!
3774
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
169,455✔
3775
  CHECK_MAKE_NODE(pFunc);
169,459!
3776
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
169,459✔
3777
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
169,459✔
3778
  CHECK_MAKE_NODE(pCol);
169,453!
3779
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
169,453✔
3780
  CHECK_PARSER_STATUS(pCxt);
169,457!
3781
  return (SNode*)pFunc;
169,457✔
3782
_err:
×
3783
  nodesDestroyNode((SNode*)pFunc);
×
3784
  return NULL;
×
3785
}
3786

3787
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
56,484✔
3788
  SDeleteStmt* pStmt = NULL;
56,484✔
3789
  CHECK_PARSER_STATUS(pCxt);
56,484!
3790
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
56,484✔
3791
  CHECK_MAKE_NODE(pStmt);
56,485!
3792
  pStmt->pFromTable = pTable;
56,485✔
3793
  pStmt->pWhere = pWhere;
56,485✔
3794
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
56,485✔
3795
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
56,486✔
3796
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
56,486✔
3797
  CHECK_MAKE_NODE(pStmt->pCountFunc);
56,485!
3798
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
56,485!
3799
  CHECK_MAKE_NODE(pStmt->pLastFunc);
56,485!
3800
  return (SNode*)pStmt;
56,485✔
3801
_err:
×
3802
  nodesDestroyNode((SNode*)pStmt);
×
3803
  nodesDestroyNode(pTable);
×
3804
  nodesDestroyNode(pWhere);
×
3805
  return NULL;
2✔
3806
}
3807

3808
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
195✔
3809
  CHECK_PARSER_STATUS(pCxt);
195!
3810
  SInsertStmt* pStmt = NULL;
195✔
3811
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
195✔
3812
  CHECK_MAKE_NODE(pStmt);
195!
3813
  pStmt->pTable = pTable;
195✔
3814
  pStmt->pCols = pCols;
195✔
3815
  pStmt->pQuery = pQuery;
195✔
3816
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
195!
3817
    strcpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias);
195✔
3818
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
×
3819
    strcpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias);
×
3820
  }
3821
  return (SNode*)pStmt;
195✔
3822
_err:
×
3823
  nodesDestroyNode(pTable);
×
3824
  nodesDestroyNode(pQuery);
×
3825
  nodesDestroyList(pCols);
×
3826
  return NULL;
×
3827
}
3828

3829
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
369✔
3830
                            SNode* pRealTable, SNode* pInterval) {
3831
  SCreateTSMAStmt* pStmt = NULL;
369✔
3832
  CHECK_PARSER_STATUS(pCxt);
369!
3833
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
369✔
3834
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
361✔
3835
  CHECK_MAKE_NODE(pStmt);
361!
3836

3837
  pStmt->ignoreExists = ignoreExists;
361✔
3838
  if (!pOptions) {
361✔
3839
    // recursive tsma
3840
    pStmt->pOptions = NULL;
127✔
3841
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
127✔
3842
    CHECK_MAKE_NODE(pStmt->pOptions);
127!
3843
    pStmt->pOptions->recursiveTsma = true;
127✔
3844
  } else {
3845
    pStmt->pOptions = (STSMAOptions*)pOptions;
234✔
3846
  }
3847
  pStmt->pOptions->pInterval = pInterval;
361✔
3848
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
361✔
3849

3850
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
361✔
3851
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
361✔
3852
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
361✔
3853
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
361✔
3854
  nodesDestroyNode(pRealTable);
361✔
3855

3856
  return (SNode*)pStmt;
361✔
3857
_err:
8✔
3858
  nodesDestroyNode((SNode*)pStmt);
8✔
3859
  nodesDestroyNode(pOptions);
8✔
3860
  nodesDestroyNode(pRealTable);
8✔
3861
  nodesDestroyNode(pInterval);
8✔
3862
  return NULL;
8✔
3863
}
3864

3865
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
262✔
3866
  CHECK_PARSER_STATUS(pCxt);
262!
3867
  STSMAOptions* pOptions = NULL;
262✔
3868
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
262✔
3869
  CHECK_MAKE_NODE(pOptions);
262!
3870
  pOptions->pFuncs = pFuncs;
262✔
3871
  return (SNode*)pOptions;
262✔
3872
_err:
×
3873
  nodesDestroyList(pFuncs);
×
3874
  return NULL;
×
3875
}
3876

3877
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
3878
  CHECK_PARSER_STATUS(pCxt);
×
3879
  STSMAOptions* pOptions = NULL;
×
3880
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
3881
  CHECK_MAKE_NODE(pOptions);
×
3882
  return (SNode*)pOptions;
×
3883
_err:
×
3884
  return NULL;
×
3885
}
3886

3887
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
177✔
3888
  CHECK_PARSER_STATUS(pCxt);
177✔
3889
  SDropTSMAStmt* pStmt = NULL;
176✔
3890
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
176✔
3891
  CHECK_MAKE_NODE(pStmt);
176!
3892

3893
  pStmt->ignoreNotExists = ignoreNotExists;
176✔
3894
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
176✔
3895

3896
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
176✔
3897
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
176✔
3898

3899
  nodesDestroyNode(pRealTable);
176✔
3900
  return (SNode*)pStmt;
176✔
3901
_err:
1✔
3902
  nodesDestroyNode(pRealTable);
1✔
3903
  return NULL;
1✔
3904
}
3905

3906
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
2✔
3907
  CHECK_PARSER_STATUS(pCxt);
2!
3908

3909
  SShowStmt* pStmt = NULL;
2✔
3910
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
2✔
3911
  CHECK_MAKE_NODE(pStmt);
2!
3912

3913
  pStmt->pDbName = dbName;
2✔
3914
  return (SNode*)pStmt;
2✔
3915
_err:
×
3916
  nodesDestroyNode(dbName);
×
3917
  return NULL;
×
3918
}
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