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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

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

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

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

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

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

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

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

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

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

77
static void trimEscape(SAstCreateContext* pCxt, SToken* pName) {
497,519✔
78
  // todo need to deal with `ioo``ii` -> ioo`ii: done
79
  if (NULL != pName && pName->n > 1 && TS_ESCAPE_CHAR == pName->z[0]) {
497,519✔
80
    if (!pCxt->pQueryCxt->hasDupQuoteChar) {
22,246!
81
      pName->z += 1;
22,248✔
82
      pName->n -= 2;
22,248✔
83
    } else {
84
      int32_t i = 1, j = 0;
×
85
      for (; i < pName->n - 1; ++i) {
×
86
        if ((pName->z[i] == TS_ESCAPE_CHAR) && (pName->z[i + 1] == TS_ESCAPE_CHAR)) {
×
87
          pName->z[j++] = TS_ESCAPE_CHAR;
×
88
          ++i;
×
89
        } else {
90
          pName->z[j++] = pName->z[i];
×
91
        }
92
      }
93
      pName->n = j;
×
94
    }
95
  }
96
}
497,519✔
97

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

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

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

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

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

130
  bool charTypes[4] = {0};
66✔
131
  for (int32_t i = 0; i < strlen(pPassword); ++i) {
738✔
132
    if (taosIsBigChar(pPassword[i])) {
672✔
133
      charTypes[0] = true;
36✔
134
    } else if (taosIsSmallChar(pPassword[i])) {
636✔
135
      charTypes[1] = true;
192✔
136
    } else if (taosIsNumberChar(pPassword[i])) {
444✔
137
      charTypes[2] = true;
378✔
138
    } else if (taosIsSpecialChar(pPassword[i])) {
66!
139
      charTypes[3] = true;
66✔
140
    } else {
141
      return true;
×
142
    }
143
  }
144

145
  int32_t numOfTypes = 0;
66✔
146
  for (int32_t i = 0; i < 4; ++i) {
330✔
147
    numOfTypes += charTypes[i];
264✔
148
  }
149

150
  if (numOfTypes < 3) {
66!
151
    return true;
×
152
  }
153

154
  return false;
66✔
155
}
156

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

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

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

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

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

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

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

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

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

246
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
101,340✔
247
  if (NULL == pDbName) {
101,340✔
248
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
16,775✔
249
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
7✔
250
    }
251
  } else {
252
    trimEscape(pCxt, pDbName);
84,565✔
253
    if (pDbName->n >= TSDB_DB_NAME_LEN || pDbName->n == 0) {
84,555!
254
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
1✔
255
    }
256
  }
257
  return TSDB_CODE_SUCCESS == pCxt->errCode;
101,329✔
258
}
259

260
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
254,796✔
261
  trimEscape(pCxt, pTableName);
254,796✔
262
  if (NULL != pTableName && pTableName->type != TK_NK_NIL &&
254,773✔
263
      (pTableName->n >= TSDB_TABLE_NAME_LEN || pTableName->n == 0)) {
113,808!
264
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
×
265
    return false;
×
266
  }
267
  return true;
254,787✔
268
}
269

270
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
151,789✔
271
  trimEscape(pCxt, pColumnName);
151,789✔
272
  if (NULL != pColumnName && pColumnName->type != TK_NK_NIL &&
151,779!
273
      (pColumnName->n >= TSDB_COL_NAME_LEN || pColumnName->n == 0)) {
151,780!
274
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
×
275
    return false;
×
276
  }
277
  return true;
151,780✔
278
}
279

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

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

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

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

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

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

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

346
static bool checkMountPath(SAstCreateContext* pCxt, SToken* pMountPath) {
×
347
  trimEscape(pCxt, pMountPath);
×
348
  if (pMountPath->n >= TSDB_MOUNT_PATH_LEN || pMountPath->n == 0) {
×
349
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pMountPath->z);
×
350
    return false;
×
351
  }
352
  return true;
×
353
}
354

355
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
73,366✔
356
  CHECK_PARSER_STATUS(pCxt);
73,366✔
357
  SRawExprNode* target = NULL;
73,342✔
358
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
73,342✔
359
  CHECK_MAKE_NODE(target);
73,350!
360
  target->p = pToken->z;
73,350✔
361
  target->n = pToken->n;
73,350✔
362
  target->pNode = pNode;
73,350✔
363
  return (SNode*)target;
73,350✔
364
_err:
24✔
365
  nodesDestroyNode(pNode);
24✔
366
  return NULL;
24✔
367
}
368

369
SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
71,686✔
370
  CHECK_PARSER_STATUS(pCxt);
71,686!
371
  SRawExprNode* target = NULL;
71,686✔
372
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RAW_EXPR, (SNode**)&target);
71,686✔
373
  CHECK_MAKE_NODE(target);
71,727!
374
  target->p = pStart->z;
71,727✔
375
  target->n = (pEnd->z + pEnd->n) - pStart->z;
71,727✔
376
  target->pNode = pNode;
71,727✔
377
  return (SNode*)target;
71,727✔
378
_err:
×
379
  nodesDestroyNode(pNode);
×
380
  return NULL;
×
381
}
382

383
SNode* setRawExprNodeIsPseudoColumn(SAstCreateContext* pCxt, SNode* pNode, bool isPseudoColumn) {
7,236✔
384
  CHECK_PARSER_STATUS(pCxt);
7,236!
385
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
7,236!
386
    return pNode;
×
387
  }
388
  ((SRawExprNode*)pNode)->isPseudoColumn = isPseudoColumn;
7,236✔
389
  return pNode;
7,236✔
390
_err:
×
391
  nodesDestroyNode(pNode);
×
392
  return NULL;
×
393
}
394

395
SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
144,933✔
396
  CHECK_PARSER_STATUS(pCxt);
144,933!
397
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
144,933✔
398
  SNode*        pRealizedExpr = pRawExpr->pNode;
144,933✔
399
  if (nodesIsExprNode(pRealizedExpr)) {
144,933✔
400
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
141,725✔
401
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
141,725✔
402
      tstrncpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
49,543✔
403
      tstrncpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName, TSDB_COL_NAME_LEN);
49,543✔
404
    } else if (pRawExpr->isPseudoColumn) {
92,182✔
405
      // all pseudo column are translate to function with same name
406
      tstrncpy(pExpr->aliasName, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
5,361✔
407
      if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_column") == 0) {
5,361✔
408
        SValueNode* pColId = (SValueNode*)nodesListGetNode(((SFunctionNode*)pExpr)->pParameterList, 0);
48✔
409
        snprintf(pExpr->userAlias, sizeof(pExpr->userAlias), "%%%%%s", pColId->literal);
48✔
410
      } else if (strcmp(((SFunctionNode*)pExpr)->functionName, "_placeholder_tbname") == 0) {
5,313✔
411
        tstrncpy(pExpr->userAlias, "%%tbname", TSDB_COL_NAME_LEN);
24✔
412
      } else {
413
        tstrncpy(pExpr->userAlias, ((SFunctionNode*)pExpr)->functionName, TSDB_COL_NAME_LEN);
5,289✔
414
      }
415
    } else {
416
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
86,821✔
417

418
      // See TS-3398.
419
      // Len of pRawExpr->p could be larger than len of aliasName[TSDB_COL_NAME_LEN].
420
      // If aliasName is truncated, hash value of aliasName could be the same.
421
      uint64_t hashVal = MurmurHash3_64(pRawExpr->p, pRawExpr->n);
86,821✔
422
      snprintf(pExpr->aliasName, TSDB_COL_NAME_LEN, "%" PRIu64, hashVal);
86,810✔
423
      strncpy(pExpr->userAlias, pRawExpr->p, len);
86,810✔
424
      pExpr->userAlias[len] = 0;
86,810✔
425
    }
426
  }
427
  pRawExpr->pNode = NULL;
144,903✔
428
  nodesDestroyNode(pNode);
144,903✔
429
  return pRealizedExpr;
144,958✔
430
_err:
×
431
  nodesDestroyNode(pNode);
×
432
  return NULL;
×
433
}
434

435
SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
53,281✔
436
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
53,281!
437
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
438
    return nil_token;
×
439
  }
440
  SRawExprNode* target = (SRawExprNode*)pNode;
53,281✔
441
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
53,281✔
442
  return t;
53,281✔
443
}
444

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

466
_err:
×
467
  nodesDestroyNode(pFuncNode);
×
468
  nodesDestroyList(pNodeList);
×
469
  return NULL;
×
470
}
471

472
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
114,119✔
473
  CHECK_PARSER_STATUS(pCxt);
114,119!
474
  SNodeList* list = NULL;
114,119✔
475
  pCxt->errCode = nodesMakeList(&list);
114,119✔
476
  CHECK_MAKE_NODE(list);
114,215!
477
  pCxt->errCode = nodesListAppend(list, pNode);
114,215✔
478
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
114,219!
479
    nodesDestroyList(list);
×
480
    return NULL;
×
481
  }
482
  return list;
114,230✔
483
_err:
×
484
  nodesDestroyNode(pNode);
×
485
  return NULL;
×
486
}
487

488
SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
783,297✔
489
  CHECK_PARSER_STATUS(pCxt);
783,297!
490
  pCxt->errCode = nodesListAppend(pList, pNode);
783,297✔
491
  return pList;
787,654✔
492
_err:
×
493
  nodesDestroyNode(pNode);
×
494
  nodesDestroyList(pList);
×
495
  return NULL;
×
496
}
497

498
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
62,894✔
499
  CHECK_PARSER_STATUS(pCxt);
62,894!
500
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
62,894!
501
    return NULL;
×
502
  }
503
  SColumnNode* col = NULL;
62,866✔
504
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&col);
62,866✔
505
  CHECK_MAKE_NODE(col);
62,880!
506
  if (NULL != pTableAlias) {
62,880✔
507
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
13,618✔
508
  }
509
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
62,880✔
510
  return (SNode*)col;
62,880✔
511
_err:
×
512
  return NULL;
×
513
}
514

515
SNode* createPlaceHolderColumnNode(SAstCreateContext* pCxt, SNode* pColId) {
48✔
516
  CHECK_PARSER_STATUS(pCxt);
48!
517
  SFunctionNode* pFunc = NULL;
48✔
518
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
48✔
519
  CHECK_PARSER_STATUS(pCxt);
48!
520
  tstrncpy(pFunc->functionName, "_placeholder_column", TSDB_FUNC_NAME_LEN);
48✔
521
  ((SValueNode*)pColId)->notReserved = true;
48✔
522
  pCxt->errCode = nodesListMakeAppend(&pFunc->pParameterList, pColId);
48✔
523
  CHECK_PARSER_STATUS(pCxt);
48!
524
  pFunc->tz = pCxt->pQueryCxt->timezone;
48✔
525
  pFunc->charsetCxt = pCxt->pQueryCxt->charsetCxt;
48✔
526
  return (SNode*)pFunc;
48✔
527
_err:
×
528
  return NULL;
×
529
}
530

531
static void copyValueTrimEscape(char* buf, int32_t bufLen, const SToken* pToken, bool trim) {
664,294✔
532
  int32_t len = TMIN(pToken->n, bufLen - 1);
664,294✔
533
  if (trim && (pToken->z[0] == TS_ESCAPE_CHAR)) {
664,294!
534
    int32_t i = 1, j = 0;
×
535
    for (; i < len - 1; ++i) {
×
536
      buf[j++] = pToken->z[i];
×
537
      if (pToken->z[i] == TS_ESCAPE_CHAR) {
×
538
        if (pToken->z[i + 1] == TS_ESCAPE_CHAR) ++i;
×
539
      }
540
    }
541
    buf[j] = 0;
×
542
  } else {
543
    tstrncpy(buf, pToken->z, len + 1);
664,294✔
544
  }
545
}
664,294✔
546

547
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
664,908✔
548
  CHECK_PARSER_STATUS(pCxt);
664,908!
549
  SValueNode* val = NULL;
664,908✔
550
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
664,908✔
551
  CHECK_MAKE_NODE(val);
661,619!
552
  if (!(val->literal = taosMemoryMalloc(pLiteral->n + 1))) {
661,619!
553
    pCxt->errCode = terrno;
×
554
    nodesDestroyNode((SNode*)val);
×
555
    return NULL;
×
556
  }
557
  copyValueTrimEscape(val->literal, pLiteral->n + 1, pLiteral,
664,605✔
558
                      pCxt->pQueryCxt->hasDupQuoteChar && (TK_NK_ID == pLiteral->type));
664,605!
559
  if (TK_NK_ID != pLiteral->type && TK_TIMEZONE != pLiteral->type &&
664,344!
560
      (IS_VAR_DATA_TYPE(dataType) || TSDB_DATA_TYPE_TIMESTAMP == dataType)) {
656,296!
561
    (void)trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
8,743✔
562
  }
563
  val->node.resType.type = dataType;
664,856✔
564
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
664,856!
565
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
664,856✔
566
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
595✔
567
  }
568
  val->translate = false;
664,856✔
569
  val->tz = pCxt->pQueryCxt->timezone;
664,856✔
570
  val->charsetCxt = pCxt->pQueryCxt->charsetCxt;
664,856✔
571
  return (SNode*)val;
664,856✔
572
_err:
×
573
  return NULL;
×
574
}
575

576
SNode* createRawValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pNode) {
74,387✔
577
  CHECK_PARSER_STATUS(pCxt);
74,387!
578
  SValueNode* val = NULL;
74,387✔
579
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
74,387✔
580
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
74,417✔
581
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
5✔
582
    goto _exit;
×
583
  }
584
  if (pLiteral) {
74,412✔
585
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
74,400!
586
    if (!val->literal) {
74,391!
587
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
588
      goto _exit;
×
589
    }
590
  } else if (pNode) {
12!
591
    SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
12✔
592
    if (!nodesIsExprNode(pRawExpr->pNode)) {
12!
593
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pRawExpr->p);
×
594
      goto _exit;
×
595
    }
596
    val->literal = taosStrndup(pRawExpr->p, pRawExpr->n);
12!
597
    if (!val->literal) {
×
598
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
599
      goto _exit;
×
600
    }
601
  } else {
602
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
603
    goto _exit;
×
604
  }
605
  if (!val->literal) {
74,381!
606
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY, "Out of memory");
×
607
    goto _exit;
×
608
  }
609

610
  val->node.resType.type = dataType;
74,381✔
611
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
74,381!
612
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
74,381!
613
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
614
  }
615
_exit:
74,381✔
616
  nodesDestroyNode(pNode);
74,381✔
617
  if (pCxt->errCode != 0) {
74,386!
618
    nodesDestroyNode((SNode*)val);
×
619
    return NULL;
×
620
  }
621
  return (SNode*)val;
74,389✔
622
_err:
×
623
  nodesDestroyNode(pNode);
×
624
  return NULL;
×
625
}
626

627
SNode* createRawValueNodeExt(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral, SNode* pLeft,
12✔
628
                             SNode* pRight) {
629
  SValueNode* val = NULL;
12✔
630
  CHECK_PARSER_STATUS(pCxt);
12!
631

632
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
12✔
633
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
12!
634
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, pCxt->errCode, "");
×
635
    goto _exit;
×
636
  }
637
  if (pLiteral) {
12!
638
    if (!(val->literal = taosStrndup(pLiteral->z, pLiteral->n))) {
12!
639
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, terrno, "Out of memory");
×
640
      goto _exit;
×
641
    }
642
  } else {
643
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTERNAL_ERROR, "Invalid parameters");
×
644
    goto _exit;
×
645
  }
646

647
  val->node.resType.type = dataType;
12✔
648
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
12!
649
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
12!
650
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
651
  }
652
_exit:
12✔
653
  nodesDestroyNode(pLeft);
12✔
654
  nodesDestroyNode(pRight);
12✔
655
  CHECK_PARSER_STATUS(pCxt);
12!
656
  return (SNode*)val;
12✔
657
_err:
×
658
  nodesDestroyNode((SNode*)val);
×
659
  nodesDestroyNode(pLeft);
×
660
  nodesDestroyNode(pRight);
×
661
  return NULL;
×
662
}
663

664
static bool hasHint(SNodeList* pHintList, EHintOption hint) {
×
665
  if (!pHintList) return false;
×
666
  SNode* pNode;
667
  FOREACH(pNode, pHintList) {
×
668
    SHintNode* pHint = (SHintNode*)pNode;
×
669
    if (pHint->option == hint) {
×
670
      return true;
×
671
    }
672
  }
673
  return false;
×
674
}
675

676
bool addHintNodeToList(SAstCreateContext* pCxt, SNodeList** ppHintList, EHintOption opt, SToken* paramList,
×
677
                       int32_t paramNum) {
678
  void* value = NULL;
×
679
  switch (opt) {
×
680
    case HINT_SKIP_TSMA:
×
681
    case HINT_BATCH_SCAN:
682
    case HINT_NO_BATCH_SCAN: {
683
      if (paramNum > 0) {
×
684
        return true;
×
685
      }
686
      break;
×
687
    }
688
    case HINT_SORT_FOR_GROUP:
×
689
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARTITION_FIRST)) return true;
×
690
      break;
×
691
    case HINT_PARTITION_FIRST:
×
692
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SORT_FOR_GROUP)) return true;
×
693
      break;
×
694
    case HINT_PARA_TABLES_SORT:
×
695
      if (paramNum > 0 || hasHint(*ppHintList, HINT_PARA_TABLES_SORT)) return true;
×
696
      break;
×
697
    case HINT_SMALLDATA_TS_SORT:
×
698
      if (paramNum > 0 || hasHint(*ppHintList, HINT_SMALLDATA_TS_SORT)) return true;
×
699
      break;
×
700
    case HINT_HASH_JOIN:
×
701
      if (paramNum > 0 || hasHint(*ppHintList, HINT_HASH_JOIN)) return true;
×
702
      break;
×
703
    default:
×
704
      return true;
×
705
  }
706

707
  SHintNode* hint = NULL;
×
708
  pCxt->errCode = nodesMakeNode(QUERY_NODE_HINT, (SNode**)&hint);
×
709
  if (!hint) {
×
710
    return true;
×
711
  }
712
  hint->option = opt;
×
713
  hint->value = value;
×
714

715
  if (NULL == *ppHintList) {
×
716
    pCxt->errCode = nodesMakeList(ppHintList);
×
717
    if (!*ppHintList) {
×
718
      nodesDestroyNode((SNode*)hint);
×
719
      return true;
×
720
    }
721
  }
722

723
  pCxt->errCode = nodesListStrictAppend(*ppHintList, (SNode*)hint);
×
724
  if (pCxt->errCode) {
×
725
    return true;
×
726
  }
727

728
  return false;
×
729
}
730

731
SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) {
27,698✔
732
  CHECK_PARSER_STATUS(pCxt);
27,698!
733
  if (NULL == pLiteral || pLiteral->n <= 5) {
27,698!
734
    return NULL;
27,698✔
735
  }
736
  SNodeList* pHintList = NULL;
×
737
  char*      hint = taosStrndup(pLiteral->z + 3, pLiteral->n - 5);
×
738
  if (!hint) return NULL;
×
739
  int32_t     i = 0;
×
740
  bool        quit = false;
×
741
  bool        inParamList = false;
×
742
  bool        lastComma = false;
×
743
  EHintOption opt = 0;
×
744
  int32_t     paramNum = 0;
×
745
  SToken      paramList[10];
746
  while (!quit) {
×
747
    SToken t0 = {0};
×
748
    if (hint[i] == 0) {
×
749
      break;
×
750
    }
751
    t0.n = tGetToken(&hint[i], &t0.type, NULL);
×
752
    t0.z = hint + i;
×
753
    i += t0.n;
×
754

755
    switch (t0.type) {
×
756
      case TK_BATCH_SCAN:
×
757
        lastComma = false;
×
758
        if (0 != opt || inParamList) {
×
759
          quit = true;
×
760
          break;
×
761
        }
762
        opt = HINT_BATCH_SCAN;
×
763
        break;
×
764
      case TK_NO_BATCH_SCAN:
×
765
        lastComma = false;
×
766
        if (0 != opt || inParamList) {
×
767
          quit = true;
×
768
          break;
×
769
        }
770
        opt = HINT_NO_BATCH_SCAN;
×
771
        break;
×
772
      case TK_SORT_FOR_GROUP:
×
773
        lastComma = false;
×
774
        if (0 != opt || inParamList) {
×
775
          quit = true;
×
776
          break;
×
777
        }
778
        opt = HINT_SORT_FOR_GROUP;
×
779
        break;
×
780
      case TK_PARTITION_FIRST:
×
781
        lastComma = false;
×
782
        if (0 != opt || inParamList) {
×
783
          quit = true;
×
784
          break;
×
785
        }
786
        opt = HINT_PARTITION_FIRST;
×
787
        break;
×
788
      case TK_PARA_TABLES_SORT:
×
789
        lastComma = false;
×
790
        if (0 != opt || inParamList) {
×
791
          quit = true;
×
792
          break;
×
793
        }
794
        opt = HINT_PARA_TABLES_SORT;
×
795
        break;
×
796
      case TK_SMALLDATA_TS_SORT:
×
797
        lastComma = false;
×
798
        if (0 != opt || inParamList) {
×
799
          quit = true;
×
800
          break;
×
801
        }
802
        opt = HINT_SMALLDATA_TS_SORT;
×
803
        break;
×
804
      case TK_HASH_JOIN:
×
805
        lastComma = false;
×
806
        if (0 != opt || inParamList) {
×
807
          quit = true;
×
808
          break;
×
809
        }
810
        opt = HINT_HASH_JOIN;
×
811
        break;
×
812
      case TK_SKIP_TSMA:
×
813
        lastComma = false;
×
814
        if (0 != opt || inParamList) {
×
815
          quit = true;
×
816
          break;
×
817
        }
818
        opt = HINT_SKIP_TSMA;
×
819
        break;
×
820
      case TK_NK_LP:
×
821
        lastComma = false;
×
822
        if (0 == opt || inParamList) {
×
823
          quit = true;
×
824
        }
825
        inParamList = true;
×
826
        break;
×
827
      case TK_NK_RP:
×
828
        lastComma = false;
×
829
        if (0 == opt || !inParamList) {
×
830
          quit = true;
×
831
        } else {
832
          quit = addHintNodeToList(pCxt, &pHintList, opt, paramList, paramNum);
×
833
          inParamList = false;
×
834
          paramNum = 0;
×
835
          opt = 0;
×
836
        }
837
        break;
×
838
      case TK_NK_ID:
×
839
        lastComma = false;
×
840
        if (0 == opt || !inParamList) {
×
841
          quit = true;
×
842
        } else {
843
          paramList[paramNum++] = t0;
×
844
        }
845
        break;
×
846
      case TK_NK_COMMA:
×
847
        if (lastComma) {
×
848
          quit = true;
×
849
        }
850
        lastComma = true;
×
851
        break;
×
852
      case TK_NK_SPACE:
×
853
        break;
×
854
      default:
×
855
        lastComma = false;
×
856
        quit = true;
×
857
        break;
×
858
    }
859
  }
860

861
  taosMemoryFree(hint);
×
862
  return pHintList;
×
863
_err:
×
864
  return NULL;
×
865
}
866

867
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
165✔
868
  trimEscape(pCxt, pLiteral);
165✔
869
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
165✔
870
}
871

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

926
SNode* createTimeOffsetValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
×
927
  CHECK_PARSER_STATUS(pCxt);
×
928
  SValueNode* val = NULL;
×
929
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
×
930
  CHECK_MAKE_NODE(val);
×
931
  if (pLiteral->type == TK_NK_STRING) {
×
932
    // like '100s' or "100d"
933
    // check format: ^[0-9]+[smwbauhdny]$'
934
    if (pLiteral->n < 4) {
×
935
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
936
      return NULL;
×
937
    }
938
    char unit = pLiteral->z[pLiteral->n - 2];
×
939
    switch (unit) {
×
940
      case 'a':
×
941
      case 'b':
942
      case 'd':
943
      case 'h':
944
      case 'm':
945
      case 's':
946
      case 'u':
947
      case 'w':
948
        break;
×
949
      default:
×
950
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
951
        return NULL;
×
952
    }
953
    for (uint32_t i = 1; i < pLiteral->n - 2; ++i) {
×
954
      if (!isdigit(pLiteral->z[i])) {
×
955
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
×
956
        return NULL;
×
957
      }
958
    }
959
    val->literal = taosStrndup(pLiteral->z + 1, pLiteral->n - 2);
×
960
  } else {
961
    val->literal = taosStrndup(pLiteral->z, pLiteral->n);
×
962
  }
963
  if (!val->literal) {
×
964
    nodesDestroyNode((SNode*)val);
×
965
    pCxt->errCode = terrno;
×
966
    return NULL;
×
967
  }
968
  val->flag |= VALUE_FLAG_IS_TIME_OFFSET;
×
969
  val->translate = false;
×
970
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
×
971
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
×
972
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
×
973
  return (SNode*)val;
×
974
_err:
×
975
  return NULL;
×
976
}
977

978
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
141✔
979
  CHECK_PARSER_STATUS(pCxt);
141!
980
  if (NULL == pCxt->pQueryCxt->db) {
141✔
981
    return NULL;
1✔
982
  }
983

984
  SValueNode* val = NULL;
140✔
985
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
140✔
986
  CHECK_MAKE_NODE(val);
140!
987
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
140!
988
  if (!val->literal) {
140!
989
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
990
    nodesDestroyNode((SNode*)val);
×
991
    return NULL;
×
992
  }
993
  val->translate = false;
140✔
994
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
140✔
995
  val->node.resType.bytes = strlen(val->literal);
140✔
996
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
140✔
997
  return (SNode*)val;
140✔
998
_err:
×
999
  return NULL;
×
1000
}
1001

1002
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
55✔
1003
  CHECK_PARSER_STATUS(pCxt);
55!
1004
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
55✔
1005
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
12✔
1006
    return NULL;
12✔
1007
  }
1008
  SValueNode* val = NULL;
43✔
1009
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&val);
43✔
1010
  CHECK_MAKE_NODE(val);
43!
1011
  val->literal = taosStrndup(pLiteral->z, pLiteral->n);
43!
1012
  if (!val->literal) {
43!
1013
    pCxt->errCode = terrno;
×
1014
    nodesDestroyNode((SNode*)val);
×
1015
    return NULL;
×
1016
  }
1017
  val->placeholderNo = ++pCxt->placeholderNo;
43✔
1018
  if (NULL == pCxt->pPlaceholderValues) {
43✔
1019
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
28✔
1020
    if (NULL == pCxt->pPlaceholderValues) {
28!
1021
      nodesDestroyNode((SNode*)val);
×
1022
      return NULL;
×
1023
    }
1024
  }
1025
  if (NULL == taosArrayPush(pCxt->pPlaceholderValues, &val)) {
86!
1026
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
×
1027
    nodesDestroyNode((SNode*)val);
×
1028
    taosArrayDestroy(pCxt->pPlaceholderValues);
×
1029
    return NULL;
×
1030
  }
1031
  return (SNode*)val;
43✔
1032
_err:
×
1033
  return NULL;
×
1034
}
1035

1036
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
12,512✔
1037
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType &&
12,512✔
1038
      ((SLogicConditionNode*)pParam)->condType != LOGIC_COND_TYPE_NOT) {
158!
1039
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
158✔
1040
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
158✔
1041
    nodesDestroyNode(pParam);
158✔
1042
    return code;
158✔
1043
  } else {
1044
    return nodesListAppend(pCond->pParameterList, pParam);
12,354✔
1045
  }
1046
}
1047

1048
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
6,262✔
1049
  CHECK_PARSER_STATUS(pCxt);
6,262!
1050
  SLogicConditionNode* cond = NULL;
6,262✔
1051
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION, (SNode**)&cond);
6,262✔
1052
  CHECK_MAKE_NODE(cond);
6,262!
1053
  cond->condType = type;
6,262✔
1054
  cond->pParameterList = NULL;
6,262✔
1055
  pCxt->errCode = nodesMakeList(&cond->pParameterList);
6,262✔
1056
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
6,262!
1057
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam1);
6,262✔
1058
  }
1059
  if (TSDB_CODE_SUCCESS == pCxt->errCode && NULL != pParam2) {
6,262!
1060
    pCxt->errCode = addParamToLogicConditionNode(cond, pParam2);
6,250✔
1061
  }
1062
  if (TSDB_CODE_SUCCESS != pCxt->errCode) {
6,262!
1063
    nodesDestroyNode((SNode*)cond);
×
1064
    return NULL;
×
1065
  }
1066
  return (SNode*)cond;
6,262✔
1067
_err:
×
1068
  nodesDestroyNode(pParam1);
×
1069
  nodesDestroyNode(pParam2);
×
1070
  return NULL;
×
1071
}
1072

1073
static uint8_t getMinusDataType(uint8_t orgType) {
1,016✔
1074
  switch (orgType) {
1,016✔
1075
    case TSDB_DATA_TYPE_UTINYINT:
982✔
1076
    case TSDB_DATA_TYPE_USMALLINT:
1077
    case TSDB_DATA_TYPE_UINT:
1078
    case TSDB_DATA_TYPE_UBIGINT:
1079
      return TSDB_DATA_TYPE_BIGINT;
982✔
1080
    default:
34✔
1081
      break;
34✔
1082
  }
1083
  return orgType;
34✔
1084
}
1085

1086
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
22,943✔
1087
  CHECK_PARSER_STATUS(pCxt);
22,943!
1088
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
22,943✔
1089
    SValueNode* pVal = (SValueNode*)pLeft;
1,016✔
1090
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
1,016!
1091
    if (!pNewLiteral) {
1,016!
1092
      pCxt->errCode = terrno;
×
1093
      goto _err;
×
1094
    }
1095
    if ('+' == pVal->literal[0]) {
1,016!
1096
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal + 1);
×
1097
    } else if ('-' == pVal->literal[0]) {
1,016!
1098
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "%s", pVal->literal + 1);
×
1099
    } else {
1100
      snprintf(pNewLiteral, strlen(pVal->literal) + 2, "-%s", pVal->literal);
1,016✔
1101
    }
1102
    taosMemoryFree(pVal->literal);
1,016!
1103
    pVal->literal = pNewLiteral;
1,016✔
1104
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
1,016✔
1105
    return pLeft;
1,016✔
1106
  }
1107
  if (pLeft && QUERY_NODE_VALUE == nodeType(pLeft)) {
21,927!
1108
    SValueNode* pVal = (SValueNode*)pLeft;
241✔
1109
    pVal->tz = pCxt->pQueryCxt->timezone;
241✔
1110
  }
1111
  if (pRight && QUERY_NODE_VALUE == nodeType(pRight)) {
21,927✔
1112
    SValueNode* pVal = (SValueNode*)pRight;
14,207✔
1113
    pVal->tz = pCxt->pQueryCxt->timezone;
14,207✔
1114
  }
1115

1116
  SOperatorNode* op = NULL;
21,927✔
1117
  pCxt->errCode = nodesMakeNode(QUERY_NODE_OPERATOR, (SNode**)&op);
21,927✔
1118
  CHECK_MAKE_NODE(op);
21,927!
1119
  op->opType = type;
21,927✔
1120
  op->pLeft = pLeft;
21,927✔
1121
  op->pRight = pRight;
21,927✔
1122
  op->tz = pCxt->pQueryCxt->timezone;
21,927✔
1123
  op->charsetCxt = pCxt->pQueryCxt->charsetCxt;
21,927✔
1124
  return (SNode*)op;
21,927✔
1125
_err:
×
1126
  nodesDestroyNode(pLeft);
×
1127
  nodesDestroyNode(pRight);
×
1128
  return NULL;
×
1129
}
1130

1131
SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
495✔
1132
  SNode *pNew = NULL, *pGE = NULL, *pLE = NULL;
495✔
1133
  CHECK_PARSER_STATUS(pCxt);
495!
1134
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
495✔
1135
  CHECK_PARSER_STATUS(pCxt);
495!
1136
  pGE = createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft);
495✔
1137
  CHECK_PARSER_STATUS(pCxt);
495!
1138
  pLE = createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, pNew, pRight);
495✔
1139
  CHECK_PARSER_STATUS(pCxt);
495!
1140
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND, pGE, pLE);
495✔
1141
  CHECK_PARSER_STATUS(pCxt);
495!
1142
  return pRet;
495✔
1143
_err:
×
1144
  nodesDestroyNode(pNew);
×
1145
  nodesDestroyNode(pGE);
×
1146
  nodesDestroyNode(pLE);
×
1147
  nodesDestroyNode(pExpr);
×
1148
  nodesDestroyNode(pLeft);
×
1149
  nodesDestroyNode(pRight);
×
1150
  return NULL;
×
1151
}
1152

1153
SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
1✔
1154
  SNode *pNew = NULL, *pLT = NULL, *pGT = NULL;
1✔
1155
  CHECK_PARSER_STATUS(pCxt);
1!
1156
  pCxt->errCode = nodesCloneNode(pExpr, &pNew);
1✔
1157
  CHECK_PARSER_STATUS(pCxt);
1!
1158
  pLT = createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft);
1✔
1159
  CHECK_PARSER_STATUS(pCxt);
1!
1160
  pGT = createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, pNew, pRight);
1✔
1161
  CHECK_PARSER_STATUS(pCxt);
1!
1162
  SNode* pRet = createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, pLT, pGT);
1✔
1163
  CHECK_PARSER_STATUS(pCxt);
1!
1164
  return pRet;
1✔
1165
_err:
×
1166
  nodesDestroyNode(pNew);
×
1167
  nodesDestroyNode(pGT);
×
1168
  nodesDestroyNode(pLT);
×
1169
  nodesDestroyNode(pExpr);
×
1170
  nodesDestroyNode(pLeft);
×
1171
  nodesDestroyNode(pRight);
×
1172
  return NULL;
×
1173
}
1174

1175
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
9,434✔
1176
  CHECK_PARSER_STATUS(pCxt);
9,434!
1177
  SColumnNode* pCol = NULL;
9,434✔
1178
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN, (SNode**)&pCol);
9,434✔
1179
  CHECK_MAKE_NODE(pCol);
9,434!
1180
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
9,434✔
1181
  if (NULL == pFuncName) {
9,434✔
1182
    tstrncpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME, TSDB_COL_NAME_LEN);
7,559✔
1183
  } else {
1184
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
1,875✔
1185
  }
1186
  pCol->isPrimTs = true;
9,434✔
1187
  return (SNode*)pCol;
9,434✔
1188
_err:
×
1189
  return NULL;
×
1190
}
1191

1192
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
31,771✔
1193
  CHECK_PARSER_STATUS(pCxt);
31,771!
1194
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
31,771✔
1195
    return createPrimaryKeyCol(pCxt, pFuncName);
1,924✔
1196
  }
1197
  SFunctionNode* func = NULL;
29,847✔
1198
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
29,847✔
1199
  CHECK_MAKE_NODE(func);
29,876!
1200
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
29,876✔
1201
  func->pParameterList = pParameterList;
29,876✔
1202
  func->tz = pCxt->pQueryCxt->timezone;
29,876✔
1203
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
29,876✔
1204
  return (SNode*)func;
29,876✔
1205
_err:
×
1206
  nodesDestroyList(pParameterList);
×
1207
  return NULL;
×
1208
}
1209

1210
SNode* createPHTbnameFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
24✔
1211
  CHECK_PARSER_STATUS(pCxt);
24!
1212
  SFunctionNode* func = NULL;
24✔
1213
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
24✔
1214
  CHECK_MAKE_NODE(func);
24!
1215
  tstrncpy(func->functionName, "_placeholder_tbname", TSDB_FUNC_NAME_LEN);
24✔
1216
  func->pParameterList = pParameterList;
24✔
1217
  func->tz = pCxt->pQueryCxt->timezone;
24✔
1218
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
24✔
1219
  return (SNode*)func;
24✔
1220
_err:
×
1221
  nodesDestroyList(pParameterList);
×
1222
  return NULL;
×
1223
}
1224

1225
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
2,607✔
1226
  SFunctionNode* func = NULL;
2,607✔
1227
  CHECK_PARSER_STATUS(pCxt);
2,607!
1228
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
2,607✔
1229
  CHECK_MAKE_NODE(func);
2,607!
1230
  tstrncpy(func->functionName, "cast", TSDB_FUNC_NAME_LEN);
2,607✔
1231
  func->node.resType = dt;
2,607✔
1232
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
2,607!
1233
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
829✔
1234
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
1,778✔
1235
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
171✔
1236
  }
1237
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
2,607✔
1238
  CHECK_PARSER_STATUS(pCxt);
2,607!
1239
  func->tz = pCxt->pQueryCxt->timezone;
2,607✔
1240
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
2,607✔
1241

1242
  return (SNode*)func;
2,607✔
1243
_err:
×
1244
  nodesDestroyNode((SNode*)func);
×
1245
  nodesDestroyNode(pExpr);
×
1246
  return NULL;
×
1247
}
1248

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

1267
SNode* createTrimFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, ETrimType type) {
68✔
1268
  SFunctionNode* func = NULL;
68✔
1269
  CHECK_PARSER_STATUS(pCxt);
68!
1270
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
68✔
1271
  CHECK_MAKE_NODE(func);
68!
1272
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
68✔
1273
  func->trimType = type;
68✔
1274
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
68✔
1275
  CHECK_PARSER_STATUS(pCxt);
68!
1276
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
68✔
1277
  return (SNode*)func;
68✔
1278
_err:
×
1279
  nodesDestroyNode((SNode*)func);
×
1280
  nodesDestroyNode(pExpr);
×
1281
  return NULL;
×
1282
}
1283

1284
SNode* createTrimFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, ETrimType type) {
252✔
1285
  SFunctionNode* func = NULL;
252✔
1286
  CHECK_PARSER_STATUS(pCxt);
252!
1287
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
252✔
1288
  CHECK_MAKE_NODE(func);
252!
1289
  tstrncpy(func->functionName, "trim", TSDB_FUNC_NAME_LEN);
252✔
1290
  func->trimType = type;
252✔
1291
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
252✔
1292
  CHECK_PARSER_STATUS(pCxt);
252!
1293
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
252✔
1294
  CHECK_PARSER_STATUS(pCxt);
252!
1295
  func->charsetCxt = pCxt->pQueryCxt->charsetCxt;
252✔
1296
  return (SNode*)func;
252✔
1297
_err:
×
1298
  nodesDestroyNode((SNode*)func);
×
1299
  nodesDestroyNode(pExpr);
×
1300
  nodesDestroyNode(pExpr2);
×
1301
  return NULL;
×
1302
}
1303

1304
SNode* createSubstrFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2) {
40✔
1305
  SFunctionNode* func = NULL;
40✔
1306
  CHECK_PARSER_STATUS(pCxt);
40!
1307
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
40✔
1308
  CHECK_MAKE_NODE(func);
40!
1309
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
40✔
1310
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
40✔
1311
  CHECK_PARSER_STATUS(pCxt);
40!
1312
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
40✔
1313
  CHECK_PARSER_STATUS(pCxt);
40!
1314
  return (SNode*)func;
40✔
1315
_err:
×
1316
  nodesDestroyNode((SNode*)func);
×
1317
  nodesDestroyNode(pExpr);
×
1318
  nodesDestroyNode(pExpr2);
×
1319
  return NULL;
×
1320
}
1321

1322
SNode* createSubstrFunctionNodeExt(SAstCreateContext* pCxt, SNode* pExpr, SNode* pExpr2, SNode* pExpr3) {
88✔
1323
  SFunctionNode* func = NULL;
88✔
1324
  CHECK_PARSER_STATUS(pCxt);
88!
1325
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&func);
88✔
1326
  CHECK_MAKE_NODE(func);
88!
1327
  tstrncpy(func->functionName, "substr", TSDB_FUNC_NAME_LEN);
88✔
1328
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr);
88✔
1329
  CHECK_PARSER_STATUS(pCxt);
88!
1330
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr2);
88✔
1331
  CHECK_PARSER_STATUS(pCxt);
88!
1332
  pCxt->errCode = nodesListMakeAppend(&func->pParameterList, pExpr3);
88✔
1333
  CHECK_PARSER_STATUS(pCxt);
88!
1334
  return (SNode*)func;
88✔
1335
_err:
×
1336
  nodesDestroyNode((SNode*)func);
×
1337
  nodesDestroyNode(pExpr);
×
1338
  nodesDestroyNode(pExpr2);
×
1339
  nodesDestroyNode(pExpr3);
×
1340
  return NULL;
×
1341
}
1342

1343
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
570✔
1344
  SNodeListNode* list = NULL;
570✔
1345
  CHECK_PARSER_STATUS(pCxt);
570!
1346
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
570✔
1347
  CHECK_MAKE_NODE(list);
570!
1348
  list->pNodeList = pList;
570✔
1349
  return (SNode*)list;
570✔
1350
_err:
×
1351
  nodesDestroyList(pList);
×
1352
  return NULL;
×
1353
}
1354

1355
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
192✔
1356
  SNodeListNode* list = NULL;
192✔
1357
  CHECK_PARSER_STATUS(pCxt);
192!
1358
  pCxt->errCode = nodesMakeNode(QUERY_NODE_NODE_LIST, (SNode**)&list);
192✔
1359
  CHECK_MAKE_NODE(list);
192!
1360
  pCxt->errCode = nodesListMakeStrictAppend(&list->pNodeList, p1);
192✔
1361
  CHECK_PARSER_STATUS(pCxt);
192!
1362
  pCxt->errCode = nodesListStrictAppend(list->pNodeList, p2);
192✔
1363
  CHECK_PARSER_STATUS(pCxt);
192!
1364
  return (SNode*)list;
192✔
1365
_err:
×
1366
  nodesDestroyNode((SNode*)list);
×
1367
  nodesDestroyNode(p1);
×
1368
  nodesDestroyNode(p2);
×
1369
  return NULL;
×
1370
}
1371

1372
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
94,616✔
1373
  CHECK_PARSER_STATUS(pCxt);
94,616!
1374
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
94,616✔
1375
  CHECK_NAME(checkTableName(pCxt, pTableName));
94,553!
1376
  CHECK_NAME(checkTableName(pCxt, pTableAlias));
94,539!
1377
  SRealTableNode* realTable = NULL;
94,532✔
1378
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REAL_TABLE, (SNode**)&realTable);
94,532✔
1379
  CHECK_MAKE_NODE(realTable);
94,621!
1380
  if (NULL != pDbName) {
94,621✔
1381
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
77,852✔
1382
  } else {
1383
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
16,769✔
1384
  }
1385
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
94,621✔
1386
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
3,143✔
1387
  } else {
1388
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
91,478✔
1389
  }
1390
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
94,621✔
1391
  return (SNode*)realTable;
94,621✔
1392
_err:
7✔
1393
  return NULL;
7✔
1394
}
1395

1396
SNode* createPlaceHolderTableNode(SAstCreateContext* pCxt, EStreamPlaceholder type, SToken* pTableAlias) {
30✔
1397
  CHECK_PARSER_STATUS(pCxt);
30!
1398

1399
  SPlaceHolderTableNode* phTable = NULL;
30✔
1400
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PLACE_HOLDER_TABLE, (SNode**)&phTable);
30✔
1401
  CHECK_MAKE_NODE(phTable);
30!
1402

1403
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
30!
1404
    COPY_STRING_FORM_ID_TOKEN(phTable->table.tableAlias, pTableAlias);
×
1405
  }
1406

1407
  phTable->placeholderType = type;
30✔
1408
  return (SNode*)phTable;
30✔
1409
_err:
×
1410
  return NULL;
×
1411
}
1412

1413
SNode* createStreamNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pStreamName) {
1,416✔
1414
  CHECK_PARSER_STATUS(pCxt);
1,416!
1415
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
1,416!
1416
  CHECK_NAME(checkStreamName(pCxt, pStreamName));
1,416!
1417
  SStreamNode* pStream = NULL;
1,416✔
1418
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM, (SNode**)&pStream);
1,416✔
1419
  CHECK_MAKE_NODE(pStream);
1,416!
1420
  if (NULL != pDbName) {
1,416!
1421
    COPY_STRING_FORM_ID_TOKEN(pStream->dbName, pDbName);
1,416✔
1422
  } else {
1423
    snprintf(pStream->dbName, sizeof(pStream->dbName), "%s", pCxt->pQueryCxt->db);
×
1424
  }
1425
  COPY_STRING_FORM_ID_TOKEN(pStream->streamName, pStreamName);
1,416✔
1426
  return (SNode*)pStream;
1,416✔
1427
_err:
×
1428
  return NULL;
×
1429
}
1430

1431
SNode* createRecalcRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
×
1432
  SStreamCalcRangeNode* pRange = NULL;
×
1433
  CHECK_PARSER_STATUS(pCxt);
×
1434
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_CALC_RANGE, (SNode**)&pRange);
×
1435
  CHECK_MAKE_NODE(pRange);
×
1436
  if (NULL == pStart && NULL == pEnd) {
×
1437
    pRange->calcAll = true;
×
1438
  } else {
1439
    pRange->calcAll = false;
×
1440
    pRange->pStart = pStart;
×
1441
    pRange->pEnd = pEnd;
×
1442
  }
1443

1444
  return (SNode*)pRange;
×
1445
_err:
×
1446
  nodesDestroyNode((SNode*)pRange);
×
1447
  nodesDestroyNode(pStart);
×
1448
  nodesDestroyNode(pEnd);
×
1449
  return NULL;
×
1450
}
1451

1452
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, SToken* pTableAlias) {
2,760✔
1453
  CHECK_PARSER_STATUS(pCxt);
2,760!
1454
  if (!checkTableName(pCxt, pTableAlias)) {
2,760!
1455
    return NULL;
×
1456
  }
1457
  STempTableNode* tempTable = NULL;
2,760✔
1458
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TEMP_TABLE, (SNode**)&tempTable);
2,760✔
1459
  CHECK_MAKE_NODE(tempTable);
2,760!
1460
  tempTable->pSubquery = pSubquery;
2,760✔
1461
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
2,760!
1462
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
2,368✔
1463
  } else {
1464
    taosRandStr(tempTable->table.tableAlias, 8);
392✔
1465
  }
1466
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
2,760✔
1467
    tstrncpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
2,736✔
1468
    ((SSelectStmt*)pSubquery)->isSubquery = true;
2,736✔
1469
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
24!
1470
    tstrncpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias, TSDB_TABLE_NAME_LEN);
24✔
1471
  }
1472
  return (SNode*)tempTable;
2,760✔
1473
_err:
×
1474
  nodesDestroyNode(pSubquery);
×
1475
  return NULL;
×
1476
}
1477

1478
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, EJoinSubType stype, SNode* pLeft, SNode* pRight,
2,727✔
1479
                           SNode* pJoinCond) {
1480
  CHECK_PARSER_STATUS(pCxt);
2,727!
1481
  SJoinTableNode* joinTable = NULL;
2,727✔
1482
  pCxt->errCode = nodesMakeNode(QUERY_NODE_JOIN_TABLE, (SNode**)&joinTable);
2,727✔
1483
  CHECK_MAKE_NODE(joinTable);
2,727!
1484
  joinTable->joinType = type;
2,727✔
1485
  joinTable->subType = stype;
2,727✔
1486
  joinTable->pLeft = pLeft;
2,727✔
1487
  joinTable->pRight = pRight;
2,727✔
1488
  joinTable->pOnCond = pJoinCond;
2,727✔
1489
  return (SNode*)joinTable;
2,727✔
1490
_err:
×
1491
  nodesDestroyNode(pLeft);
×
1492
  nodesDestroyNode(pRight);
×
1493
  nodesDestroyNode(pJoinCond);
×
1494
  return NULL;
×
1495
}
1496

1497
SNode* createViewNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pViewName) {
×
1498
  CHECK_PARSER_STATUS(pCxt);
×
1499
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
×
1500
  CHECK_NAME(checkViewName(pCxt, pViewName));
×
1501
  SViewNode* pView = NULL;
×
1502
  pCxt->errCode = nodesMakeNode(QUERY_NODE_VIEW, (SNode**)&pView);
×
1503
  CHECK_MAKE_NODE(pView);
×
1504
  if (NULL != pDbName) {
×
1505
    COPY_STRING_FORM_ID_TOKEN(pView->table.dbName, pDbName);
×
1506
  } else {
1507
    snprintf(pView->table.dbName, sizeof(pView->table.dbName), "%s", pCxt->pQueryCxt->db);
×
1508
  }
1509
  COPY_STRING_FORM_ID_TOKEN(pView->table.tableName, pViewName);
×
1510
  return (SNode*)pView;
×
1511
_err:
×
1512
  return NULL;
×
1513
}
1514

1515
SNode* createLimitNode(SAstCreateContext* pCxt, SNode* pLimit, SNode* pOffset) {
4,820✔
1516
  CHECK_PARSER_STATUS(pCxt);
4,820!
1517
  SLimitNode* limitNode = NULL;
4,820✔
1518
  pCxt->errCode = nodesMakeNode(QUERY_NODE_LIMIT, (SNode**)&limitNode);
4,820✔
1519
  CHECK_MAKE_NODE(limitNode);
4,820!
1520
  limitNode->limit = (SValueNode*)pLimit;
4,820✔
1521
  if (NULL != pOffset) {
4,820✔
1522
    limitNode->offset = (SValueNode*)pOffset;
414✔
1523
  }
1524
  return (SNode*)limitNode;
4,820✔
1525
_err:
×
1526
  return NULL;
×
1527
}
1528

1529
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
4,456✔
1530
  CHECK_PARSER_STATUS(pCxt);
4,456!
1531
  SOrderByExprNode* orderByExpr = NULL;
4,456✔
1532
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR, (SNode**)&orderByExpr);
4,456✔
1533
  CHECK_MAKE_NODE(orderByExpr);
4,456!
1534
  orderByExpr->pExpr = pExpr;
4,456✔
1535
  orderByExpr->order = order;
4,456✔
1536
  if (NULL_ORDER_DEFAULT == nullOrder) {
4,456✔
1537
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
4,330✔
1538
  }
1539
  orderByExpr->nullOrder = nullOrder;
4,456✔
1540
  return (SNode*)orderByExpr;
4,456✔
1541
_err:
×
1542
  nodesDestroyNode(pExpr);
×
1543
  return NULL;
×
1544
}
1545

1546
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
136✔
1547
  CHECK_PARSER_STATUS(pCxt);
136!
1548
  SSessionWindowNode* session = NULL;
136✔
1549
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SESSION_WINDOW, (SNode**)&session);
136✔
1550
  CHECK_MAKE_NODE(session);
136!
1551
  session->pCol = (SColumnNode*)pCol;
136✔
1552
  session->pGap = (SValueNode*)pGap;
136✔
1553
  return (SNode*)session;
136✔
1554
_err:
×
1555
  nodesDestroyNode(pCol);
×
1556
  nodesDestroyNode(pGap);
×
1557
  return NULL;
×
1558
}
1559

1560
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr, SNode* pTrueForLimit) {
145✔
1561
  SStateWindowNode* state = NULL;
145✔
1562
  CHECK_PARSER_STATUS(pCxt);
145!
1563
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STATE_WINDOW, (SNode**)&state);
145✔
1564
  CHECK_MAKE_NODE(state);
145!
1565
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
145✔
1566
  CHECK_MAKE_NODE(state->pCol);
145!
1567
  state->pExpr = pExpr;
145✔
1568
  state->pTrueForLimit = pTrueForLimit;
145✔
1569
  return (SNode*)state;
145✔
1570
_err:
×
1571
  nodesDestroyNode((SNode*)state);
×
1572
  nodesDestroyNode(pExpr);
×
1573
  nodesDestroyNode(pTrueForLimit);
×
1574
  return NULL;
×
1575
}
1576

1577
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond, SNode* pTrueForLimit) {
116✔
1578
  SEventWindowNode* pEvent = NULL;
116✔
1579
  CHECK_PARSER_STATUS(pCxt);
116!
1580
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EVENT_WINDOW, (SNode**)&pEvent);
116✔
1581
  CHECK_MAKE_NODE(pEvent);
116!
1582
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
116✔
1583
  CHECK_MAKE_NODE(pEvent->pCol);
116!
1584
  pEvent->pStartCond = pStartCond;
116✔
1585
  pEvent->pEndCond = pEndCond;
116✔
1586
  pEvent->pTrueForLimit = pTrueForLimit;
116✔
1587
  return (SNode*)pEvent;
116✔
1588
_err:
×
1589
  nodesDestroyNode((SNode*)pEvent);
×
1590
  nodesDestroyNode(pStartCond);
×
1591
  nodesDestroyNode(pEndCond);
×
1592
  nodesDestroyNode(pTrueForLimit);
×
1593
  return NULL;
×
1594
}
1595

1596
SNode* createCountWindowNode(SAstCreateContext* pCxt, const SToken* pCountToken, const SToken* pSlidingToken,
×
1597
                             SNodeList* pColList) {
1598
  SCountWindowNode* pCount = NULL;
×
1599
  CHECK_PARSER_STATUS(pCxt);
×
1600
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
×
1601
  CHECK_MAKE_NODE(pCount);
×
1602
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1603
  CHECK_MAKE_NODE(pCount->pCol);
×
1604
  pCount->windowCount = taosStr2Int64(pCountToken->z, NULL, 10);
×
1605
  if (pSlidingToken == NULL) {
×
1606
    pCount->windowSliding = taosStr2Int64(pSlidingToken->z, NULL, 10);
×
1607
  } else {
1608
    pCount->windowSliding = taosStr2Int64(pCountToken->z, NULL, 10);
×
1609
  }
1610
  pCount->pColList = pColList;
×
1611
  return (SNode*)pCount;
×
1612
_err:
×
1613
  nodesDestroyNode((SNode*)pCount);
×
1614
  return NULL;
×
1615
}
1616

1617
SNode* createCountWindowNodeFromArgs(SAstCreateContext* pCxt, SNode* arg) {
98✔
1618
  SCountWindowArgs* args = (SCountWindowArgs*)arg;
98✔
1619
  SCountWindowNode* pCount = NULL;
98✔
1620
  CHECK_PARSER_STATUS(pCxt);
98!
1621
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW, (SNode**)&pCount);
98✔
1622
  CHECK_MAKE_NODE(pCount);
98!
1623
  pCount->pCol = createPrimaryKeyCol(pCxt, NULL);
98✔
1624
  CHECK_MAKE_NODE(pCount->pCol);
98!
1625
  pCount->windowCount = args->count;
98✔
1626
  pCount->windowSliding = args->sliding;
98✔
1627
  pCount->pColList = args->pColList;
98✔
1628
  args->pColList = NULL;
98✔
1629
  nodesDestroyNode(arg);
98✔
1630
  return (SNode*)pCount;
98✔
1631
_err:
×
1632
  nodesDestroyNode((SNode*)pCount);
×
1633
  return NULL;
×
1634
}
1635

1636
SNode* createCountWindowArgs(SAstCreateContext* pCxt, const SToken* countToken, const SToken* slidingToken,
98✔
1637
                             SNodeList* colList) {
1638
  CHECK_PARSER_STATUS(pCxt);
98!
1639

1640
  SCountWindowArgs* args = NULL;
98✔
1641
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COUNT_WINDOW_ARGS, (SNode**)&args);
98✔
1642
  CHECK_MAKE_NODE(args);
98!
1643
  args->count = taosStr2Int64(countToken->z, NULL, 10);
98✔
1644
  if (slidingToken && slidingToken->type == TK_NK_INTEGER) {
98!
1645
    args->sliding = taosStr2Int64(slidingToken->z, NULL, 10);
24✔
1646
  } else {
1647
    args->sliding = taosStr2Int64(countToken->z, NULL, 10);
74✔
1648
  }
1649
  args->pColList = colList;
98✔
1650
  return (SNode*)args;
98✔
1651
_err:
×
1652
  return NULL;
×
1653
}
1654

1655
SNode* createAnomalyWindowNode(SAstCreateContext* pCxt, SNode* pExpr, const SToken* pFuncOpt) {
×
1656
  SAnomalyWindowNode* pAnomaly = NULL;
×
1657
  CHECK_PARSER_STATUS(pCxt);
×
1658
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ANOMALY_WINDOW, (SNode**)&pAnomaly);
×
1659
  CHECK_MAKE_NODE(pAnomaly);
×
1660
  pAnomaly->pCol = createPrimaryKeyCol(pCxt, NULL);
×
1661
  CHECK_MAKE_NODE(pAnomaly->pCol);
×
1662
  pAnomaly->pExpr = pExpr;
×
1663
  if (pFuncOpt == NULL) {
×
1664
    tstrncpy(pAnomaly->anomalyOpt, "algo=iqr", TSDB_ANALYTIC_ALGO_OPTION_LEN);
×
1665
  } else {
1666
    (void)trimString(pFuncOpt->z, pFuncOpt->n, pAnomaly->anomalyOpt, sizeof(pAnomaly->anomalyOpt));
×
1667
  }
1668
  return (SNode*)pAnomaly;
×
1669
_err:
×
1670
  nodesDestroyNode((SNode*)pAnomaly);
×
1671
  return NULL;
×
1672
}
1673

1674
SNode* createIntervalWindowNodeExt(SAstCreateContext* pCxt, SNode* pInter, SNode* pSliding) {
966✔
1675
  SIntervalWindowNode* pInterval = NULL;
966✔
1676
  CHECK_PARSER_STATUS(pCxt);
966!
1677
  if (pInter) {
966✔
1678
    pInterval = (SIntervalWindowNode*)pInter;
828✔
1679
  } else {
1680
    pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&pInterval);
138✔
1681
    CHECK_MAKE_NODE(pInterval);
138!
1682
  }
1683
  pInterval->pCol = createPrimaryKeyCol(pCxt, NULL);
966✔
1684
  CHECK_MAKE_NODE(pInterval->pCol);
966!
1685
  pInterval->pSliding = ((SSlidingWindowNode*)pSliding)->pSlidingVal;
966✔
1686
  pInterval->pSOffset = ((SSlidingWindowNode*)pSliding)->pOffset;
966✔
1687
  return (SNode*)pInterval;
966✔
1688
_err:
×
1689
  nodesDestroyNode((SNode*)pInter);
×
1690
  nodesDestroyNode((SNode*)pInterval);
×
1691
  nodesDestroyNode((SNode*)pSliding);
×
1692
  return NULL;
×
1693
}
1694

1695
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
2,282✔
1696
                                SNode* pFill) {
1697
  SIntervalWindowNode* interval = NULL;
2,282✔
1698
  CHECK_PARSER_STATUS(pCxt);
2,282!
1699
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW, (SNode**)&interval);
2,282✔
1700
  CHECK_MAKE_NODE(interval);
2,282!
1701
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
2,282✔
1702
  CHECK_MAKE_NODE(interval->pCol);
2,282!
1703
  interval->pInterval = pInterval;
2,282✔
1704
  interval->pOffset = pOffset;
2,282✔
1705
  interval->pSliding = pSliding;
2,282✔
1706
  interval->pFill = pFill;
2,282✔
1707
  TAOS_SET_OBJ_ALIGNED(&interval->timeRange, TSWINDOW_INITIALIZER);
2,282✔
1708
  interval->timezone = pCxt->pQueryCxt->timezone;
2,282✔
1709
  return (SNode*)interval;
2,282✔
1710
_err:
×
1711
  nodesDestroyNode((SNode*)interval);
×
1712
  nodesDestroyNode(pInterval);
×
1713
  nodesDestroyNode(pOffset);
×
1714
  nodesDestroyNode(pSliding);
×
1715
  nodesDestroyNode(pFill);
×
1716
  return NULL;
×
1717
}
1718

1719
SNode* createPeriodWindowNode(SAstCreateContext* pCxt, SNode* pPeriodTime, SNode* pOffset) {
156✔
1720
  SPeriodWindowNode* pPeriod = NULL;
156✔
1721
  CHECK_PARSER_STATUS(pCxt);
156!
1722
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PERIOD_WINDOW, (SNode**)&pPeriod);
156✔
1723
  CHECK_MAKE_NODE(pPeriod);
156!
1724
  pPeriod->pOffset = pOffset;
156✔
1725
  pPeriod->pPeroid = pPeriodTime;
156✔
1726
  return (SNode*)pPeriod;
156✔
1727
_err:
×
1728
  nodesDestroyNode((SNode*)pOffset);
×
1729
  nodesDestroyNode((SNode*)pPeriodTime);
×
1730
  nodesDestroyNode((SNode*)pPeriod);
×
1731
  return NULL;
×
1732
}
1733

1734
SNode* createWindowOffsetNode(SAstCreateContext* pCxt, SNode* pStartOffset, SNode* pEndOffset) {
×
1735
  SWindowOffsetNode* winOffset = NULL;
×
1736
  CHECK_PARSER_STATUS(pCxt);
×
1737
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WINDOW_OFFSET, (SNode**)&winOffset);
×
1738
  CHECK_MAKE_NODE(winOffset);
×
1739
  winOffset->pStartOffset = pStartOffset;
×
1740
  winOffset->pEndOffset = pEndOffset;
×
1741
  return (SNode*)winOffset;
×
1742
_err:
×
1743
  nodesDestroyNode((SNode*)winOffset);
×
1744
  nodesDestroyNode(pStartOffset);
×
1745
  nodesDestroyNode(pEndOffset);
×
1746
  return NULL;
×
1747
}
1748

1749
SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
429✔
1750
  SFillNode* fill = NULL;
429✔
1751
  CHECK_PARSER_STATUS(pCxt);
429!
1752
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FILL, (SNode**)&fill);
429✔
1753
  CHECK_MAKE_NODE(fill);
429!
1754
  fill->mode = mode;
429✔
1755
  fill->pValues = pValues;
429✔
1756
  fill->pWStartTs = NULL;
429✔
1757
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&(fill->pWStartTs));
429✔
1758
  CHECK_MAKE_NODE(fill->pWStartTs);
429!
1759
  tstrncpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart", TSDB_FUNC_NAME_LEN);
429✔
1760
  return (SNode*)fill;
429✔
1761
_err:
×
1762
  nodesDestroyNode((SNode*)fill);
×
1763
  nodesDestroyNode(pValues);
×
1764
  return NULL;
×
1765
}
1766

1767
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
1,515✔
1768
  SGroupingSetNode* groupingSet = NULL;
1,515✔
1769
  CHECK_PARSER_STATUS(pCxt);
1,515!
1770
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GROUPING_SET, (SNode**)&groupingSet);
1,515✔
1771
  CHECK_MAKE_NODE(groupingSet);
1,515!
1772
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
1,515✔
1773
  groupingSet->pParameterList = NULL;
1,515✔
1774
  pCxt->errCode = nodesListMakeAppend(&groupingSet->pParameterList, pNode);
1,515✔
1775
  CHECK_PARSER_STATUS(pCxt);
1,515!
1776
  return (SNode*)groupingSet;
1,515✔
1777
_err:
×
1778
  nodesDestroyNode((SNode*)groupingSet);
×
1779
  nodesDestroyNode(pNode);
×
1780
  return NULL;
×
1781
}
1782

1783
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
353✔
1784
  CHECK_PARSER_STATUS(pCxt);
353!
1785
  if (NULL == pInterval) {
353✔
1786
    if (pEnd && nodeType(pEnd) == QUERY_NODE_VALUE && ((SValueNode*)pEnd)->flag & VALUE_FLAG_IS_DURATION) {
167!
1787
      return createInterpTimeAround(pCxt, pStart, NULL, pEnd);
×
1788
    }
1789
    return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
167✔
1790
  }
1791

1792
  return createInterpTimeAround(pCxt, pStart, pEnd, pInterval);
186✔
1793

1794
_err:
×
1795

1796
  nodesDestroyNode(pStart);
×
1797
  nodesDestroyNode(pEnd);
×
1798
  return NULL;
×
1799
}
1800

1801
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
×
1802
  CHECK_PARSER_STATUS(pCxt);
×
1803
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
×
1804
_err:
×
1805
  nodesDestroyNode(pPoint);
×
1806
  return NULL;
×
1807
}
1808

1809
SNode* createInterpTimeAround(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd, SNode* pInterval) {
186✔
1810
  CHECK_PARSER_STATUS(pCxt);
186!
1811
  SRangeAroundNode* pAround = NULL;
186✔
1812
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RANGE_AROUND, (SNode**)&pAround);
186✔
1813
  CHECK_PARSER_STATUS(pCxt);
186!
1814
  if (NULL == pEnd) {
186!
1815
    pAround->pRange = createInterpTimePoint(pCxt, pStart);
×
1816
  } else {
1817
    pAround->pRange = createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
186✔
1818
  }
1819
  pAround->pInterval = pInterval;
186✔
1820
  CHECK_PARSER_STATUS(pCxt);
186!
1821
  return (SNode*)pAround;
186✔
1822
_err:
×
1823
  return NULL;
×
1824
}
1825

1826
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
56✔
1827
  CHECK_PARSER_STATUS(pCxt);
56!
1828
  SWhenThenNode* pWhenThen = NULL;
56✔
1829
  pCxt->errCode = nodesMakeNode(QUERY_NODE_WHEN_THEN, (SNode**)&pWhenThen);
56✔
1830
  CHECK_MAKE_NODE(pWhenThen);
56!
1831
  pWhenThen->pWhen = pWhen;
56✔
1832
  pWhenThen->pThen = pThen;
56✔
1833
  return (SNode*)pWhenThen;
56✔
1834
_err:
×
1835
  nodesDestroyNode(pWhen);
×
1836
  nodesDestroyNode(pThen);
×
1837
  return NULL;
×
1838
}
1839

1840
SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
41✔
1841
  CHECK_PARSER_STATUS(pCxt);
41!
1842
  SCaseWhenNode* pCaseWhen = NULL;
41✔
1843
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CASE_WHEN, (SNode**)&pCaseWhen);
41✔
1844
  CHECK_MAKE_NODE(pCaseWhen);
41!
1845
  pCaseWhen->pCase = pCase;
41✔
1846
  pCaseWhen->pWhenThenList = pWhenThenList;
41✔
1847
  pCaseWhen->pElse = pElse;
41✔
1848
  pCaseWhen->tz = pCxt->pQueryCxt->timezone;
41✔
1849
  pCaseWhen->charsetCxt = pCxt->pQueryCxt->charsetCxt;
41✔
1850
  return (SNode*)pCaseWhen;
41✔
1851
_err:
×
1852
  nodesDestroyNode(pCase);
×
1853
  nodesDestroyList(pWhenThenList);
×
1854
  nodesDestroyNode(pElse);
×
1855
  return NULL;
×
1856
}
1857

1858
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
4,427✔
1859
  CHECK_PARSER_STATUS(pCxt);
4,427!
1860
  trimEscape(pCxt, pAlias);
4,427✔
1861
  SExprNode* pExpr = (SExprNode*)pNode;
4,427✔
1862
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
4,427✔
1863
  strncpy(pExpr->aliasName, pAlias->z, len);
4,427✔
1864
  pExpr->aliasName[len] = '\0';
4,427✔
1865
  strncpy(pExpr->userAlias, pAlias->z, len);
4,427✔
1866
  pExpr->userAlias[len] = '\0';
4,427✔
1867
  pExpr->asAlias = true;
4,427✔
1868
  return pNode;
4,427✔
1869
_err:
×
1870
  nodesDestroyNode(pNode);
×
1871
  return NULL;
×
1872
}
1873

1874
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
27,681✔
1875
  CHECK_PARSER_STATUS(pCxt);
27,681!
1876
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
27,681✔
1877
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
27,680✔
1878
  }
1879
  return pStmt;
27,681✔
1880
_err:
×
1881
  nodesDestroyNode(pStmt);
×
1882
  nodesDestroyNode(pWhere);
×
1883
  return NULL;
×
1884
}
1885

1886
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
27,679✔
1887
  CHECK_PARSER_STATUS(pCxt);
27,679!
1888
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
27,679!
1889
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
27,681✔
1890
  }
1891
  return pStmt;
27,679✔
1892
_err:
×
1893
  nodesDestroyNode(pStmt);
×
1894
  nodesDestroyList(pPartitionByList);
×
1895
  return NULL;
×
1896
}
1897

1898
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
27,604✔
1899
  CHECK_PARSER_STATUS(pCxt);
27,604!
1900
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
27,604!
1901
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
27,648✔
1902
  }
1903
  return pStmt;
27,604✔
1904
_err:
×
1905
  nodesDestroyNode(pStmt);
×
1906
  nodesDestroyNode(pWindow);
×
1907
  return NULL;
×
1908
}
1909

1910
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
27,674✔
1911
  CHECK_PARSER_STATUS(pCxt);
27,674!
1912
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
27,674!
1913
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
27,680✔
1914
  }
1915
  return pStmt;
27,674✔
1916
_err:
×
1917
  nodesDestroyNode(pStmt);
×
1918
  nodesDestroyList(pGroupByList);
×
1919
  return NULL;
×
1920
}
1921

1922
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
27,641✔
1923
  CHECK_PARSER_STATUS(pCxt);
27,641!
1924
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
27,641!
1925
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
27,642✔
1926
  }
1927
  return pStmt;
27,641✔
1928
_err:
×
1929
  nodesDestroyNode(pStmt);
×
1930
  nodesDestroyNode(pHaving);
×
1931
  return NULL;
×
1932
}
1933

1934
SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
27,556✔
1935
  CHECK_PARSER_STATUS(pCxt);
27,556!
1936
  if (NULL == pOrderByList) {
27,556✔
1937
    return pStmt;
23,628✔
1938
  }
1939
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
3,928✔
1940
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
3,894✔
1941
  } else {
1942
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
34✔
1943
  }
1944
  return pStmt;
3,928✔
1945
_err:
×
1946
  nodesDestroyNode(pStmt);
×
1947
  nodesDestroyList(pOrderByList);
×
1948
  return NULL;
×
1949
}
1950

1951
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
27,536✔
1952
  CHECK_PARSER_STATUS(pCxt);
27,536!
1953
  if (NULL == pSlimit) {
27,536✔
1954
    return pStmt;
26,679✔
1955
  }
1956
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
857!
1957
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
876✔
1958
  }
1959
  return pStmt;
857✔
1960
_err:
×
1961
  nodesDestroyNode(pStmt);
×
1962
  nodesDestroyNode(pSlimit);
×
1963
  return NULL;
×
1964
}
1965

1966
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
27,508✔
1967
  CHECK_PARSER_STATUS(pCxt);
27,508!
1968
  if (NULL == pLimit) {
27,508✔
1969
    return pStmt;
23,597✔
1970
  }
1971
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
3,911!
1972
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
3,932✔
1973
  } else {
1974
    ((SSetOperator*)pStmt)->pLimit = pLimit;
×
1975
  }
1976
  return pStmt;
3,911✔
1977
_err:
×
1978
  nodesDestroyNode(pStmt);
×
1979
  nodesDestroyNode(pLimit);
×
1980
  return NULL;
×
1981
}
1982

1983
SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
27,632✔
1984
  CHECK_PARSER_STATUS(pCxt);
27,632!
1985
  SSelectStmt* pSelect = (SSelectStmt*)pStmt;
27,632✔
1986
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
27,632!
1987
    if (pRange && nodeType(pRange) == QUERY_NODE_RANGE_AROUND) {
27,659✔
1988
      pSelect->pRangeAround = pRange;
186✔
1989
      SRangeAroundNode* pAround = (SRangeAroundNode*)pRange;
186✔
1990
      TSWAP(pSelect->pRange, pAround->pRange);
186✔
1991
    } else {
1992
      pSelect->pRange = pRange;
27,473✔
1993
    }
1994
  }
1995
  return pStmt;
27,632✔
1996
_err:
×
1997
  nodesDestroyNode(pStmt);
×
1998
  nodesDestroyNode(pRange);
×
1999
  return NULL;
×
2000
}
2001

2002
SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
27,616✔
2003
  CHECK_PARSER_STATUS(pCxt);
27,616!
2004
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
27,616!
2005
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
27,661✔
2006
  }
2007
  return pStmt;
27,616✔
2008
_err:
×
2009
  nodesDestroyNode(pStmt);
×
2010
  nodesDestroyNode(pEvery);
×
2011
  return NULL;
×
2012
}
2013

2014
SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
27,614✔
2015
  CHECK_PARSER_STATUS(pCxt);
27,614!
2016
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
27,614!
2017
    SFillNode* pFillClause = (SFillNode*)pFill;
353✔
2018
    nodesDestroyNode(pFillClause->pWStartTs);
353✔
2019
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
353✔
2020
    CHECK_MAKE_NODE(pFillClause->pWStartTs);
353!
2021
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
353✔
2022
  }
2023
  return pStmt;
27,614✔
2024
_err:
×
2025
  nodesDestroyNode(pStmt);
×
2026
  nodesDestroyNode(pFill);
×
2027
  return NULL;
×
2028
}
2029

2030
SNode* addJLimitClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pJLimit) {
2,162✔
2031
  CHECK_PARSER_STATUS(pCxt);
2,162!
2032
  if (NULL == pJLimit) {
2,162!
2033
    return pJoin;
2,162✔
2034
  }
2035
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
×
2036
  pJoinNode->pJLimit = pJLimit;
×
2037

2038
  return pJoin;
×
2039
_err:
×
2040
  nodesDestroyNode(pJoin);
×
2041
  nodesDestroyNode(pJLimit);
×
2042
  return NULL;
×
2043
}
2044

2045
SNode* addWindowOffsetClause(SAstCreateContext* pCxt, SNode* pJoin, SNode* pWinOffset) {
2,162✔
2046
  CHECK_PARSER_STATUS(pCxt);
2,162!
2047
  if (NULL == pWinOffset) {
2,162!
2048
    return pJoin;
2,162✔
2049
  }
2050
  SJoinTableNode* pJoinNode = (SJoinTableNode*)pJoin;
×
2051
  pJoinNode->pWindowOffset = pWinOffset;
×
2052

2053
  return pJoin;
×
2054
_err:
×
2055
  nodesDestroyNode(pJoin);
×
2056
  nodesDestroyNode(pWinOffset);
×
2057
  return NULL;
×
2058
}
2059

2060
SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable,
27,684✔
2061
                        SNodeList* pHint) {
2062
  CHECK_PARSER_STATUS(pCxt);
27,684!
2063
  SNode* select = NULL;
27,684✔
2064
  pCxt->errCode = createSelectStmtImpl(isDistinct, pProjectionList, pTable, pHint, &select);
27,684✔
2065
  CHECK_MAKE_NODE(select);
27,658!
2066
  return select;
27,658✔
2067
_err:
×
2068
  nodesDestroyList(pProjectionList);
×
2069
  nodesDestroyNode(pTable);
×
2070
  nodesDestroyList(pHint);
×
2071
  return NULL;
22✔
2072
}
2073

2074
SNode* setSelectStmtTagMode(SAstCreateContext* pCxt, SNode* pStmt, bool bSelectTags) {
27,582✔
2075
  if (pStmt && QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
27,582!
2076
    if (pCxt->pQueryCxt->biMode) {
27,597✔
2077
      ((SSelectStmt*)pStmt)->tagScan = true;
10✔
2078
    } else {
2079
      ((SSelectStmt*)pStmt)->tagScan = bSelectTags;
27,587✔
2080
    }
2081
  }
2082
  return pStmt;
27,582✔
2083
}
2084

2085
static void setSubquery(SNode* pStmt) {
270✔
2086
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
270✔
2087
    ((SSelectStmt*)pStmt)->isSubquery = true;
264✔
2088
  }
2089
}
270✔
2090

2091
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
135✔
2092
  CHECK_PARSER_STATUS(pCxt);
135!
2093
  SSetOperator* setOp = NULL;
135✔
2094
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SET_OPERATOR, (SNode**)&setOp);
135✔
2095
  CHECK_MAKE_NODE(setOp);
135!
2096
  setOp->opType = type;
135✔
2097
  setOp->pLeft = pLeft;
135✔
2098
  setSubquery(setOp->pLeft);
135✔
2099
  setOp->pRight = pRight;
135✔
2100
  setSubquery(setOp->pRight);
135✔
2101
  snprintf(setOp->stmtName, TSDB_TABLE_NAME_LEN, "%p", setOp);
135✔
2102
  return (SNode*)setOp;
135✔
2103
_err:
×
2104
  nodesDestroyNode(pLeft);
×
2105
  nodesDestroyNode(pRight);
×
2106
  return NULL;
×
2107
}
2108

2109
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
827✔
2110
  if (!pOptions->walRetentionPeriodIsSet) {
827✔
2111
    pOptions->walRetentionPeriod =
826✔
2112
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
2113
  }
2114
  if (!pOptions->walRetentionSizeIsSet) {
827!
2115
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
827✔
2116
  }
2117
  if (!pOptions->walRollPeriodIsSet) {
827!
2118
    pOptions->walRollPeriod =
827✔
2119
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
2120
  }
2121
}
827✔
2122

2123
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
705✔
2124
  CHECK_PARSER_STATUS(pCxt);
705!
2125
  SDatabaseOptions* pOptions = NULL;
705✔
2126
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
705✔
2127
  CHECK_MAKE_NODE(pOptions);
705!
2128
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
705✔
2129
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
705✔
2130
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
705✔
2131
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
705✔
2132
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
705✔
2133
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
705✔
2134
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
705✔
2135
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
705✔
2136
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
705✔
2137
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
705✔
2138
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
705✔
2139
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
705✔
2140
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
705✔
2141
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
705✔
2142
  pOptions->precision = TSDB_DEFAULT_PRECISION;
705✔
2143
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
705✔
2144
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
705✔
2145
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
705✔
2146
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
705✔
2147
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
705✔
2148
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
705✔
2149
  updateWalOptionsDefault(pOptions);
705✔
2150
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
705✔
2151
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
705✔
2152
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
705✔
2153
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
705✔
2154
  pOptions->ssChunkSize = TSDB_DEFAULT_SS_CHUNK_SIZE;
705✔
2155
  pOptions->ssKeepLocal = TSDB_DEFAULT_SS_KEEP_LOCAL;
705✔
2156
  pOptions->ssCompact = TSDB_DEFAULT_SS_COMPACT;
705✔
2157
  pOptions->withArbitrator = TSDB_DEFAULT_DB_WITH_ARBITRATOR;
705✔
2158
  pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
705✔
2159
  pOptions->dnodeListStr[0] = 0;
705✔
2160
  pOptions->compactInterval = TSDB_DEFAULT_COMPACT_INTERVAL;
705✔
2161
  pOptions->compactStartTime = TSDB_DEFAULT_COMPACT_START_TIME;
705✔
2162
  pOptions->compactEndTime = TSDB_DEFAULT_COMPACT_END_TIME;
705✔
2163
  pOptions->compactTimeOffset = TSDB_DEFAULT_COMPACT_TIME_OFFSET;
705✔
2164
  return (SNode*)pOptions;
705✔
2165
_err:
×
2166
  return NULL;
×
2167
}
2168

2169
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
600✔
2170
  CHECK_PARSER_STATUS(pCxt);
600!
2171
  SDatabaseOptions* pOptions = NULL;
600✔
2172
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS, (SNode**)&pOptions);
600✔
2173
  CHECK_MAKE_NODE(pOptions);
600!
2174
  pOptions->buffer = -1;
600✔
2175
  pOptions->cacheModel = -1;
600✔
2176
  pOptions->cacheLastSize = -1;
600✔
2177
  pOptions->compressionLevel = -1;
600✔
2178
  pOptions->daysPerFile = -1;
600✔
2179
  pOptions->fsyncPeriod = -1;
600✔
2180
  pOptions->maxRowsPerBlock = -1;
600✔
2181
  pOptions->minRowsPerBlock = -1;
600✔
2182
  pOptions->keep[0] = -1;
600✔
2183
  pOptions->keep[1] = -1;
600✔
2184
  pOptions->keep[2] = -1;
600✔
2185
  pOptions->pages = -1;
600✔
2186
  pOptions->pagesize = -1;
600✔
2187
  pOptions->tsdbPageSize = -1;
600✔
2188
  pOptions->precision = -1;
600✔
2189
  pOptions->replica = -1;
600✔
2190
  pOptions->strict = -1;
600✔
2191
  pOptions->walLevel = -1;
600✔
2192
  pOptions->numOfVgroups = -1;
600✔
2193
  pOptions->singleStable = -1;
600✔
2194
  pOptions->schemaless = -1;
600✔
2195
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
600✔
2196
  pOptions->walRetentionSize = -2;    // -1 is a valid value
600✔
2197
  pOptions->walRollPeriod = -1;
600✔
2198
  pOptions->walSegmentSize = -1;
600✔
2199
  pOptions->sstTrigger = -1;
600✔
2200
  pOptions->tablePrefix = -1;
600✔
2201
  pOptions->tableSuffix = -1;
600✔
2202
  pOptions->ssChunkSize = -1;
600✔
2203
  pOptions->ssKeepLocal = -1;
600✔
2204
  pOptions->ssCompact = -1;
600✔
2205
  pOptions->withArbitrator = -1;
600✔
2206
  pOptions->encryptAlgorithm = -1;
600✔
2207
  pOptions->dnodeListStr[0] = 0;
600✔
2208
  pOptions->compactInterval = -1;
600✔
2209
  pOptions->compactStartTime = -1;
600✔
2210
  pOptions->compactEndTime = -1;
600✔
2211
  pOptions->compactTimeOffset = -1;
600✔
2212
  return (SNode*)pOptions;
600✔
2213
_err:
×
2214
  return NULL;
×
2215
}
2216

2217
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
2,196✔
2218
                                    bool alter) {
2219
  CHECK_PARSER_STATUS(pCxt);
2,196!
2220
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
2,196✔
2221
  switch (type) {
2,196!
2222
    case DB_OPTION_BUFFER:
103✔
2223
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
103✔
2224
      break;
103✔
2225
    case DB_OPTION_CACHEMODEL:
86✔
2226
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
86!
2227
      break;
86✔
2228
    case DB_OPTION_CACHESIZE:
84✔
2229
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
84✔
2230
      break;
84✔
2231
    case DB_OPTION_COMP:
77✔
2232
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
77✔
2233
      break;
77✔
2234
    case DB_OPTION_DAYS: {
97✔
2235
      SToken* pToken = pVal;
97✔
2236
      if (TK_NK_INTEGER == pToken->type) {
97✔
2237
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
12✔
2238
      } else {
2239
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
85✔
2240
      }
2241
      break;
97✔
2242
    }
2243
    case DB_OPTION_FSYNC:
73✔
2244
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
73✔
2245
      break;
73✔
2246
    case DB_OPTION_MAXROWS:
86✔
2247
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
86✔
2248
      break;
86✔
2249
    case DB_OPTION_MINROWS:
141✔
2250
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
141✔
2251
      break;
141✔
2252
    case DB_OPTION_KEEP:
260✔
2253
      pDbOptions->pKeep = pVal;
260✔
2254
      break;
260✔
2255
    case DB_OPTION_PAGES:
90✔
2256
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
90✔
2257
      break;
90✔
2258
    case DB_OPTION_PAGESIZE:
12✔
2259
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
12✔
2260
      break;
12✔
2261
    case DB_OPTION_TSDB_PAGESIZE:
12✔
2262
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
12✔
2263
      break;
12✔
2264
    case DB_OPTION_PRECISION:
294✔
2265
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
294!
2266
      break;
294✔
2267
    case DB_OPTION_REPLICA:
158✔
2268
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
158✔
2269
      pDbOptions->withArbitrator = (pDbOptions->replica == 2);
158✔
2270
      if (!alter) {
158✔
2271
        updateWalOptionsDefault(pDbOptions);
122✔
2272
      }
2273
      break;
158✔
2274
    case DB_OPTION_STRICT:
×
2275
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
×
2276
      break;
×
2277
    case DB_OPTION_WAL:
61✔
2278
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
61✔
2279
      break;
61✔
2280
    case DB_OPTION_VGROUPS:
112✔
2281
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
112✔
2282
      break;
112✔
2283
    case DB_OPTION_SINGLE_STABLE:
12✔
2284
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
12✔
2285
      break;
12✔
2286
    case DB_OPTION_RETENTIONS:
84✔
2287
      pDbOptions->pRetentions = pVal;
84✔
2288
      break;
84✔
2289
    case DB_OPTION_WAL_RETENTION_PERIOD:
89✔
2290
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
89✔
2291
      pDbOptions->walRetentionPeriodIsSet = true;
89✔
2292
      break;
89✔
2293
    case DB_OPTION_WAL_RETENTION_SIZE:
54✔
2294
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
54✔
2295
      pDbOptions->walRetentionSizeIsSet = true;
54✔
2296
      break;
54✔
2297
    case DB_OPTION_WAL_ROLL_PERIOD:
12✔
2298
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
12✔
2299
      pDbOptions->walRollPeriodIsSet = true;
12✔
2300
      break;
12✔
2301
    case DB_OPTION_WAL_SEGMENT_SIZE:
12✔
2302
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
12✔
2303
      break;
12✔
2304
    case DB_OPTION_STT_TRIGGER:
60✔
2305
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
60✔
2306
      break;
60✔
2307
    case DB_OPTION_TABLE_PREFIX: {
13✔
2308
      SValueNode* pNode = (SValueNode*)pVal;
13✔
2309
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
13!
2310
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
13✔
2311
      } else {
2312
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
×
2313
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2314
      }
2315
      nodesDestroyNode((SNode*)pNode);
13✔
2316
      break;
13✔
2317
    }
2318
    case DB_OPTION_TABLE_SUFFIX: {
12✔
2319
      SValueNode* pNode = (SValueNode*)pVal;
12✔
2320
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
12!
2321
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
12✔
2322
      } else {
2323
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
×
2324
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2325
      }
2326
      nodesDestroyNode((SNode*)pNode);
12✔
2327
      break;
12✔
2328
    }
2329
    case DB_OPTION_SS_CHUNKPAGES:
×
2330
      pDbOptions->ssChunkSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2331
      break;
×
2332
    case DB_OPTION_SS_KEEPLOCAL: {
×
2333
      SToken* pToken = pVal;
×
2334
      if (TK_NK_INTEGER == pToken->type) {
×
2335
        pDbOptions->ssKeepLocal = taosStr2Int32(pToken->z, NULL, 10) * 1440;
×
2336
      } else {
2337
        pDbOptions->ssKeepLocalStr = (SValueNode*)createDurationValueNode(pCxt, pToken);
×
2338
      }
2339
      break;
×
2340
    }
2341
    case DB_OPTION_SS_COMPACT:
×
2342
      pDbOptions->ssCompact = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
×
2343
      break;
×
2344
    case DB_OPTION_KEEP_TIME_OFFSET:
×
2345
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
×
2346
        pDbOptions->keepTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2347
      } else {
2348
        pDbOptions->pKeepTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
×
2349
      }
2350
      break;
×
2351
    case DB_OPTION_ENCRYPT_ALGORITHM:
×
2352
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->encryptAlgorithmStr, (SToken*)pVal);
×
2353
      pDbOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO;
×
2354
      break;
×
2355
    case DB_OPTION_DNODES:
×
2356
      if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) {
×
2357
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)",
×
2358
                 TSDB_DNODE_LIST_LEN);
2359
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2360
      } else {
2361
        COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal);
×
2362
      }
2363
      break;
×
2364
    case DB_OPTION_COMPACT_INTERVAL:
×
2365
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
×
2366
        pDbOptions->compactInterval = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2367
      } else {
2368
        pDbOptions->pCompactIntervalNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
×
2369
      }
2370
      break;
×
2371
    case DB_OPTION_COMPACT_TIME_RANGE:
×
2372
      pDbOptions->pCompactTimeRangeList = pVal;
×
2373
      break;
×
2374
    case DB_OPTION_COMPACT_TIME_OFFSET:
×
2375
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
×
2376
        pDbOptions->compactTimeOffset = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
×
2377
      } else {
2378
        pDbOptions->pCompactTimeOffsetNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
×
2379
      }
2380
      break;
×
2381
    default:
102✔
2382
      break;
102✔
2383
  }
2384
  return pOptions;
2,196✔
2385
_err:
×
2386
  nodesDestroyNode(pOptions);
×
2387
  return NULL;
×
2388
}
2389

2390
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
1,476✔
2391
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
1,476✔
2392
}
2393

2394
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
720✔
2395
  CHECK_PARSER_STATUS(pCxt);
720!
2396
  switch (pAlterOption->type) {
720✔
2397
    case DB_OPTION_KEEP:
156✔
2398
    case DB_OPTION_RETENTIONS:
2399
    case DB_OPTION_COMPACT_TIME_RANGE:
2400
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
156✔
2401
    default:
564✔
2402
      break;
564✔
2403
  }
2404
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
564✔
2405
_err:
×
2406
  nodesDestroyNode(pOptions);
×
2407
  nodesDestroyList(pAlterOption->pList);
×
2408
  return NULL;
×
2409
}
2410

2411
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
705✔
2412
  CHECK_PARSER_STATUS(pCxt);
705!
2413
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
705!
2414
  SCreateDatabaseStmt* pStmt = NULL;
705✔
2415
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT, (SNode**)&pStmt);
705✔
2416
  CHECK_MAKE_NODE(pStmt);
705!
2417
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
705✔
2418
  pStmt->ignoreExists = ignoreExists;
705✔
2419
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
705✔
2420
  return (SNode*)pStmt;
705✔
2421
_err:
×
2422
  nodesDestroyNode(pOptions);
×
2423
  return NULL;
×
2424
}
2425

2426
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName, bool force) {
457✔
2427
  CHECK_PARSER_STATUS(pCxt);
457!
2428
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
457!
2429
  SDropDatabaseStmt* pStmt = NULL;
457✔
2430
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT, (SNode**)&pStmt);
457✔
2431
  CHECK_MAKE_NODE(pStmt);
457!
2432
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
457✔
2433
  pStmt->ignoreNotExists = ignoreNotExists;
457✔
2434
  pStmt->force = force;
457✔
2435
  return (SNode*)pStmt;
457✔
2436
_err:
×
2437
  return NULL;
×
2438
}
2439

2440
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
600✔
2441
  CHECK_PARSER_STATUS(pCxt);
600!
2442
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
600!
2443
  SAlterDatabaseStmt* pStmt = NULL;
600✔
2444
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT, (SNode**)&pStmt);
600✔
2445
  CHECK_MAKE_NODE(pStmt);
600!
2446
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
600✔
2447
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
600✔
2448
  return (SNode*)pStmt;
600✔
2449
_err:
×
2450
  nodesDestroyNode(pOptions);
×
2451
  return NULL;
×
2452
}
2453

2454
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
1,328✔
2455
  CHECK_PARSER_STATUS(pCxt);
1,328!
2456
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,328!
2457
  SFlushDatabaseStmt* pStmt = NULL;
1,328✔
2458
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT, (SNode**)&pStmt);
1,328✔
2459
  CHECK_MAKE_NODE(pStmt);
1,328!
2460
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,328✔
2461
  return (SNode*)pStmt;
1,328✔
2462
_err:
×
2463
  return NULL;
×
2464
}
2465

2466
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
24✔
2467
  CHECK_PARSER_STATUS(pCxt);
24!
2468
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
24!
2469
  STrimDatabaseStmt* pStmt = NULL;
24✔
2470
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT, (SNode**)&pStmt);
24✔
2471
  CHECK_MAKE_NODE(pStmt);
24!
2472
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
24✔
2473
  pStmt->maxSpeed = maxSpeed;
24✔
2474
  return (SNode*)pStmt;
24✔
2475
_err:
×
2476
  return NULL;
×
2477
}
2478

2479
SNode* createSsMigrateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
×
2480
  CHECK_PARSER_STATUS(pCxt);
×
2481
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
×
2482
  SSsMigrateDatabaseStmt* pStmt = NULL;
×
2483
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SSMIGRATE_DATABASE_STMT, (SNode**)&pStmt);
×
2484
  CHECK_MAKE_NODE(pStmt);
×
2485
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
×
2486
  return (SNode*)pStmt;
×
2487
_err:
×
2488
  return NULL;
×
2489
}
2490

2491
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd, bool metaOnly) {
48✔
2492
  CHECK_PARSER_STATUS(pCxt);
48!
2493
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
48!
2494
  SCompactDatabaseStmt* pStmt = NULL;
48✔
2495
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT, (SNode**)&pStmt);
48✔
2496
  CHECK_MAKE_NODE(pStmt);
48!
2497
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
48✔
2498
  pStmt->pStart = pStart;
48✔
2499
  pStmt->pEnd = pEnd;
48✔
2500
  pStmt->metaOnly = metaOnly;
48✔
2501
  return (SNode*)pStmt;
48✔
2502
_err:
×
2503
  nodesDestroyNode(pStart);
×
2504
  nodesDestroyNode(pEnd);
×
2505
  return NULL;
×
2506
}
2507

2508
SNode* createCreateMountStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pMountName, SToken* pDnodeId,
×
2509
                             SToken* pMountPath) {
2510
#ifdef USE_MOUNT
2511
  CHECK_PARSER_STATUS(pCxt);
×
2512
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
×
2513
  CHECK_NAME(checkMountPath(pCxt, pMountPath));
×
2514
  SCreateMountStmt* pStmt = NULL;
×
2515
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MOUNT_STMT, (SNode**)&pStmt);
×
2516
  CHECK_MAKE_NODE(pStmt);
×
2517
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
×
2518
  COPY_STRING_FORM_STR_TOKEN(pStmt->mountPath, pMountPath);
×
2519
  pStmt->ignoreExists = ignoreExists;
×
2520
  if (TK_NK_INTEGER == pDnodeId->type) {
×
2521
    pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
×
2522
  } else {
2523
    goto _err;
×
2524
  }
2525
  return (SNode*)pStmt;
×
2526
_err:
×
2527
  nodesDestroyNode((SNode*)pStmt);
×
2528
  return NULL;
×
2529
#else
2530
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
2531
  return NULL;
2532
#endif
2533
}
2534

2535
SNode* createDropMountStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pMountName) {
×
2536
#ifdef USE_MOUNT
2537
  CHECK_PARSER_STATUS(pCxt);
×
2538
  CHECK_NAME(checkDbName(pCxt, pMountName, false));
×
2539
  SDropMountStmt* pStmt = NULL;
×
2540
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_MOUNT_STMT, (SNode**)&pStmt);
×
2541
  CHECK_MAKE_NODE(pStmt);
×
2542
  COPY_STRING_FORM_ID_TOKEN(pStmt->mountName, pMountName);
×
2543
  pStmt->ignoreNotExists = ignoreNotExists;
×
2544
  return (SNode*)pStmt;
×
2545
_err:
×
2546
  return NULL;
×
2547
#else
2548
  pCxt->errCode = TSDB_CODE_OPS_NOT_SUPPORT;
2549
  return NULL;
2550
#endif
2551
}
2552

2553
SNode* createCompactVgroupsStmt(SAstCreateContext* pCxt, SNode* pDbName, SNodeList* vgidList, SNode* pStart,
×
2554
                                SNode* pEnd, bool metaOnly) {
2555
  CHECK_PARSER_STATUS(pCxt);
×
2556
  if (NULL == pDbName) {
×
2557
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
2558
    pCxt->errCode = TSDB_CODE_PAR_DB_NOT_SPECIFIED;
×
2559
    CHECK_PARSER_STATUS(pCxt);
×
2560
  }
2561
  SCompactVgroupsStmt* pStmt = NULL;
×
2562
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COMPACT_VGROUPS_STMT, (SNode**)&pStmt);
×
2563
  CHECK_MAKE_NODE(pStmt);
×
2564
  pStmt->pDbName = pDbName;
×
2565
  pStmt->vgidList = vgidList;
×
2566
  pStmt->pStart = pStart;
×
2567
  pStmt->pEnd = pEnd;
×
2568
  pStmt->metaOnly = metaOnly;
×
2569
  return (SNode*)pStmt;
×
2570
_err:
×
2571
  nodesDestroyNode(pDbName);
×
2572
  nodesDestroyList(vgidList);
×
2573
  nodesDestroyNode(pStart);
×
2574
  nodesDestroyNode(pEnd);
×
2575
  return NULL;
×
2576
}
2577

2578
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
30,133✔
2579
  CHECK_PARSER_STATUS(pCxt);
30,133!
2580
  STableOptions* pOptions = NULL;
30,133✔
2581
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
30,133✔
2582
  CHECK_MAKE_NODE(pOptions);
30,152!
2583
  pOptions->maxDelay1 = -1;
30,152✔
2584
  pOptions->maxDelay2 = -1;
30,152✔
2585
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
30,152✔
2586
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
30,152✔
2587
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
30,152✔
2588
  pOptions->keep = -1;
30,152✔
2589
  pOptions->commentNull = true;  // mark null
30,152✔
2590
  return (SNode*)pOptions;
30,152✔
2591
_err:
×
2592
  return NULL;
×
2593
}
2594

2595
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
58✔
2596
  CHECK_PARSER_STATUS(pCxt);
58!
2597
  STableOptions* pOptions = NULL;
58✔
2598
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS, (SNode**)&pOptions);
58✔
2599
  CHECK_MAKE_NODE(pOptions);
58!
2600
  pOptions->ttl = -1;
58✔
2601
  pOptions->commentNull = true;  // mark null
58✔
2602
  pOptions->keep = -1;
58✔
2603
  return (SNode*)pOptions;
58✔
2604
_err:
×
2605
  return NULL;
×
2606
}
2607

2608
SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
303✔
2609
  CHECK_PARSER_STATUS(pCxt);
303!
2610
  switch (type) {
303!
2611
    case TABLE_OPTION_COMMENT:
70✔
2612
      if (checkComment(pCxt, (SToken*)pVal, true)) {
70!
2613
        ((STableOptions*)pOptions)->commentNull = false;
70✔
2614
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
70!
2615
      }
2616
      break;
70✔
2617
    case TABLE_OPTION_MAXDELAY:
36✔
2618
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
36✔
2619
      break;
36✔
2620
    case TABLE_OPTION_WATERMARK:
36✔
2621
      ((STableOptions*)pOptions)->pWatermark = pVal;
36✔
2622
      break;
36✔
2623
    case TABLE_OPTION_ROLLUP:
60✔
2624
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
60✔
2625
      break;
60✔
2626
    case TABLE_OPTION_TTL: {
52✔
2627
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
52✔
2628
      if (ttl > INT32_MAX) {
52!
2629
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
2630
      } else {
2631
        // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
2632
        ((STableOptions*)pOptions)->ttl = ttl;
52✔
2633
      }
2634
      break;
52✔
2635
    }
2636
    case TABLE_OPTION_SMA:
36✔
2637
      ((STableOptions*)pOptions)->pSma = pVal;
36✔
2638
      break;
36✔
2639
    case TABLE_OPTION_DELETE_MARK:
12✔
2640
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
12✔
2641
      break;
12✔
2642
    case TABLE_OPTION_KEEP:
×
2643
      if (TK_NK_INTEGER == ((SToken*)pVal)->type) {
×
2644
        ((STableOptions*)pOptions)->keep = taosStr2Int32(((SToken*)pVal)->z, NULL, 10) * 1440;
×
2645
      } else {
2646
        ((STableOptions*)pOptions)->pKeepNode = (SValueNode*)createDurationValueNode(pCxt, (SToken*)pVal);
×
2647
      }
2648
      break;
×
2649
    case TABLE_OPTION_VIRTUAL: {
1✔
2650
      int64_t virtualStb = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
1✔
2651
      if (virtualStb != 0 && virtualStb != 1) {
1!
2652
        pCxt->errCode = TSDB_CODE_TSC_VALUE_OUT_OF_RANGE;
×
2653
      } else {
2654
        ((STableOptions*)pOptions)->virtualStb = virtualStb;
1✔
2655
      }
2656
      break;
1✔
2657
    }
2658
    default:
×
2659
      break;
×
2660
  }
2661
  return pOptions;
303✔
2662
_err:
×
2663
  nodesDestroyNode(pOptions);
×
2664
  return NULL;
×
2665
}
2666

2667
SNode* createDefaultColumnOptions(SAstCreateContext* pCxt) {
83,629✔
2668
  CHECK_PARSER_STATUS(pCxt);
83,629!
2669
  SColumnOptions* pOptions = NULL;
83,629✔
2670
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_OPTIONS, (SNode**)&pOptions);
83,629✔
2671
  CHECK_MAKE_NODE(pOptions);
83,629!
2672
  pOptions->commentNull = true;
83,629✔
2673
  pOptions->bPrimaryKey = false;
83,629✔
2674
  pOptions->hasRef = false;
83,629✔
2675
  return (SNode*)pOptions;
83,629✔
2676
_err:
×
2677
  return NULL;
×
2678
}
2679

2680
EColumnOptionType getColumnOptionType(const char* optionType) {
3,238✔
2681
  if (0 == strcasecmp(optionType, "ENCODE")) {
3,238✔
2682
    return COLUMN_OPTION_ENCODE;
16✔
2683
  } else if (0 == strcasecmp(optionType, "COMPRESS")) {
3,222✔
2684
    return COLUMN_OPTION_COMPRESS;
1,611✔
2685
  } else if (0 == strcasecmp(optionType, "LEVEL")) {
1,611!
2686
    return COLUMN_OPTION_LEVEL;
1,611✔
2687
  }
2688
  return 0;
×
2689
}
2690

2691
SNode* setColumnReference(SAstCreateContext* pCxt, SNode* pOptions, SNode* pRef) {
1✔
2692
  CHECK_PARSER_STATUS(pCxt);
1!
2693

2694
  ((SColumnOptions*)pOptions)->hasRef = true;
1✔
2695
  tstrncpy(((SColumnOptions*)pOptions)->refDb, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
1✔
2696
  tstrncpy(((SColumnOptions*)pOptions)->refTable, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
1✔
2697
  tstrncpy(((SColumnOptions*)pOptions)->refColumn, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
1✔
2698
  return pOptions;
1✔
2699
_err:
×
2700
  nodesDestroyNode(pOptions);
×
2701
  return NULL;
×
2702
}
2703

2704
SNode* setColumnOptionsPK(SAstCreateContext* pCxt, SNode* pOptions) {
32✔
2705
  CHECK_PARSER_STATUS(pCxt);
32!
2706
  ((SColumnOptions*)pOptions)->bPrimaryKey = true;
32✔
2707
  return pOptions;
32✔
2708
_err:
×
2709
  nodesDestroyNode(pOptions);
×
2710
  return NULL;
×
2711
}
2712

2713
SNode* setColumnOptions(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal1, void* pVal2) {
3,238✔
2714
  CHECK_PARSER_STATUS(pCxt);
3,238!
2715
  char optionType[TSDB_CL_OPTION_LEN];
2716

2717
  memset(optionType, 0, TSDB_CL_OPTION_LEN);
3,238✔
2718
  strncpy(optionType, pVal1->z, TMIN(pVal1->n, TSDB_CL_OPTION_LEN));
3,238✔
2719
  if (0 == strlen(optionType)) {
3,238!
2720
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2721
    return pOptions;
×
2722
  }
2723
  EColumnOptionType type = getColumnOptionType(optionType);
3,238✔
2724
  switch (type) {
3,238!
2725
    case COLUMN_OPTION_ENCODE:
16✔
2726
      memset(((SColumnOptions*)pOptions)->encode, 0, TSDB_CL_COMPRESS_OPTION_LEN);
16✔
2727
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->encode, (SToken*)pVal2);
16!
2728
      if (0 == strlen(((SColumnOptions*)pOptions)->encode)) {
16!
2729
        pCxt->errCode = TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
×
2730
      }
2731
      break;
16✔
2732
    case COLUMN_OPTION_COMPRESS:
1,611✔
2733
      memset(((SColumnOptions*)pOptions)->compress, 0, TSDB_CL_COMPRESS_OPTION_LEN);
1,611✔
2734
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compress, (SToken*)pVal2);
1,611!
2735
      if (0 == strlen(((SColumnOptions*)pOptions)->compress)) {
1,611!
2736
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_PARAM_ERROR;
×
2737
      }
2738
      break;
1,611✔
2739
    case COLUMN_OPTION_LEVEL:
1,611✔
2740
      memset(((SColumnOptions*)pOptions)->compressLevel, 0, TSDB_CL_COMPRESS_OPTION_LEN);
1,611✔
2741
      COPY_STRING_FORM_STR_TOKEN(((SColumnOptions*)pOptions)->compressLevel, (SToken*)pVal2);
1,611!
2742
      if (0 == strlen(((SColumnOptions*)pOptions)->compressLevel)) {
1,611!
2743
        pCxt->errCode = TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR;
×
2744
      }
2745
      break;
1,611✔
2746
    default:
×
2747
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2748
      break;
×
2749
  }
2750
  return pOptions;
3,238✔
2751
_err:
×
2752
  nodesDestroyNode(pOptions);
×
2753
  return NULL;
×
2754
}
2755

2756
SNode* createColumnRefNodeByNode(SAstCreateContext* pCxt, SToken* pColName, SNode* pRef) {
×
2757
  CHECK_PARSER_STATUS(pCxt);
×
2758
  CHECK_NAME(checkColumnName(pCxt, pColName));
×
2759

2760
  SColumnRefNode* pCol = NULL;
×
2761
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
×
2762
  CHECK_MAKE_NODE(pCol);
×
2763
  if (pColName) {
×
2764
    COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
×
2765
  }
2766
  tstrncpy(pCol->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
×
2767
  tstrncpy(pCol->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
×
2768
  tstrncpy(pCol->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
×
2769
  return (SNode*)pCol;
×
2770
_err:
×
2771
  return NULL;
×
2772
}
2773

2774
STokenTriplet* createTokenTriplet(SAstCreateContext* pCxt, SToken pName) {
2✔
2775
  CHECK_PARSER_STATUS(pCxt);
2!
2776

2777
  STokenTriplet* pTokenTri = taosMemoryMalloc(sizeof(STokenTriplet));
2!
2778
  CHECK_OUT_OF_MEM(pTokenTri);
2!
2779
  pTokenTri->name[0] = pName;
2✔
2780
  pTokenTri->numOfName = 1;
2✔
2781

2782
  return pTokenTri;
2✔
2783
_err:
×
2784
  return NULL;
×
2785
}
2786

2787
STokenTriplet* setColumnName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri, SToken pName) {
2✔
2788
  CHECK_PARSER_STATUS(pCxt);
2!
2789

2790
  if (pTokenTri->numOfName >= 3) {
2!
2791
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2792
    goto _err;
×
2793
  }
2794

2795
  pTokenTri->name[pTokenTri->numOfName] = pName;
2✔
2796
  pTokenTri->numOfName++;
2✔
2797
  return pTokenTri;
2✔
2798
_err:
×
2799
  return NULL;
×
2800
}
2801

2802
SNode* createColumnRefNodeByName(SAstCreateContext* pCxt, STokenTriplet* pTokenTri) {
2✔
2803
  SColumnRefNode* pCol = NULL;
2✔
2804
  CHECK_PARSER_STATUS(pCxt);
2!
2805
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_REF, (SNode**)&pCol);
2✔
2806
  CHECK_MAKE_NODE(pCol);
2!
2807

2808
  switch (pTokenTri->numOfName) {
2!
2809
    case 2: {
2✔
2810
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[0]));
2!
2811
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[1]));
2!
2812
      snprintf(pCol->refDbName, TSDB_DB_NAME_LEN, "%s", pCxt->pQueryCxt->db);
2✔
2813
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[0]);
2✔
2814
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[1]);
2✔
2815
      break;
2✔
2816
    }
2817
    case 3: {
×
2818
      CHECK_NAME(checkDbName(pCxt, &pTokenTri->name[0], true));
×
2819
      CHECK_NAME(checkTableName(pCxt, &pTokenTri->name[1]));
×
2820
      CHECK_NAME(checkColumnName(pCxt, &pTokenTri->name[2]));
×
2821
      COPY_STRING_FORM_ID_TOKEN(pCol->refDbName, &pTokenTri->name[0]);
×
2822
      COPY_STRING_FORM_ID_TOKEN(pCol->refTableName, &pTokenTri->name[1]);
×
2823
      COPY_STRING_FORM_ID_TOKEN(pCol->refColName, &pTokenTri->name[2]);
×
2824
      break;
×
2825
    }
2826
    default: {
×
2827
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
2828
      goto _err;
×
2829
    }
2830
  }
2831

2832
  taosMemFree(pTokenTri);
2✔
2833
  return (SNode*)pCol;
2✔
2834
_err:
×
2835
  taosMemFree(pTokenTri);
×
2836
  nodesDestroyNode((SNode*)pCol);
×
2837
  return NULL;
×
2838
}
2839

2840
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, SNode* pNode) {
83,645✔
2841
  CHECK_PARSER_STATUS(pCxt);
83,645!
2842
  CHECK_NAME(checkColumnName(pCxt, pColName));
83,645!
2843
  if (IS_VAR_DATA_TYPE(dataType.type) && dataType.bytes == 0) {
83,645!
2844
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
×
2845
    CHECK_PARSER_STATUS(pCxt);
×
2846
  }
2847
  SColumnDefNode* pCol = NULL;
83,645✔
2848
  pCxt->errCode = nodesMakeNode(QUERY_NODE_COLUMN_DEF, (SNode**)&pCol);
83,645✔
2849
  CHECK_MAKE_NODE(pCol);
83,645!
2850
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
83,645✔
2851
  pCol->dataType = dataType;
83,645✔
2852
  pCol->pOptions = pNode;
83,645✔
2853
  pCol->sma = true;
83,645✔
2854
  return (SNode*)pCol;
83,645✔
2855
_err:
×
2856
  nodesDestroyNode(pNode);
×
2857
  return NULL;
×
2858
}
2859

2860
SDataType createDataType(uint8_t type) {
82,817✔
2861
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
82,817✔
2862
  return dt;
82,817✔
2863
}
2864

2865
SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
8,499✔
2866
  int32_t len = TSDB_MAX_BINARY_LEN - VARSTR_HEADER_SIZE;
8,499✔
2867
  if (type == TSDB_DATA_TYPE_NCHAR) len /= TSDB_NCHAR_SIZE;
8,499✔
2868
  if (pLen) len = taosStr2Int32(pLen->z, NULL, 10);
8,499✔
2869
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = len};
8,499✔
2870
  return dt;
8,499✔
2871
}
2872

2873
SDataType createDecimalDataType(uint8_t type, const SToken* pPrecisionToken, const SToken* pScaleToken) {
9✔
2874
  SDataType dt = {0};
9✔
2875
  dt.precision = taosStr2UInt8(pPrecisionToken->z, NULL, 10);
9✔
2876
  dt.scale = pScaleToken ? taosStr2Int32(pScaleToken->z, NULL, 10) : 0;
9!
2877
  dt.type = decimalTypeFromPrecision(dt.precision);
9✔
2878
  dt.bytes = tDataTypes[dt.type].bytes;
9✔
2879
  return dt;
9✔
2880
}
2881

2882
SNode* createCreateVTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols) {
1✔
2883
  SCreateVTableStmt* pStmt = NULL;
1✔
2884
  CHECK_PARSER_STATUS(pCxt);
1!
2885
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
1✔
2886
  CHECK_MAKE_NODE(pStmt);
1!
2887
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
1✔
2888
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
1✔
2889
  pStmt->ignoreExists = ignoreExists;
1✔
2890
  pStmt->pCols = pCols;
1✔
2891
  nodesDestroyNode(pRealTable);
1✔
2892
  return (SNode*)pStmt;
1✔
2893
_err:
×
2894
  nodesDestroyNode(pRealTable);
×
2895
  nodesDestroyList(pCols);
×
2896
  return NULL;
×
2897
}
2898

2899
SNode* createCreateVSubTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable,
1✔
2900
                                 SNodeList* pSpecificColRefs, SNodeList* pColRefs, SNode* pUseRealTable,
2901
                                 SNodeList* pSpecificTags, SNodeList* pValsOfTags) {
2902
  CHECK_PARSER_STATUS(pCxt);
1!
2903
  SCreateVSubTableStmt* pStmt = NULL;
1✔
2904
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIRTUAL_SUBTABLE_STMT, (SNode**)&pStmt);
1✔
2905
  CHECK_MAKE_NODE(pStmt);
1!
2906
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
1✔
2907
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
1✔
2908
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
1✔
2909
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
1✔
2910
  pStmt->ignoreExists = ignoreExists;
1✔
2911
  pStmt->pSpecificTags = pSpecificTags;
1✔
2912
  pStmt->pValsOfTags = pValsOfTags;
1✔
2913
  pStmt->pSpecificColRefs = pSpecificColRefs;
1✔
2914
  pStmt->pColRefs = pColRefs;
1✔
2915
  nodesDestroyNode(pRealTable);
1✔
2916
  nodesDestroyNode(pUseRealTable);
1✔
2917
  return (SNode*)pStmt;
1✔
2918
_err:
×
2919
  nodesDestroyNode(pRealTable);
×
2920
  nodesDestroyNode(pUseRealTable);
×
2921
  nodesDestroyList(pSpecificTags);
×
2922
  nodesDestroyList(pValsOfTags);
×
2923
  nodesDestroyList(pSpecificColRefs);
×
2924
  nodesDestroyList(pColRefs);
×
2925
  return NULL;
×
2926
}
2927

2928
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
661✔
2929
                             SNodeList* pTags, SNode* pOptions) {
2930
  CHECK_PARSER_STATUS(pCxt);
661!
2931
  SCreateTableStmt* pStmt = NULL;
661✔
2932
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT, (SNode**)&pStmt);
661✔
2933
  CHECK_MAKE_NODE(pStmt);
661!
2934
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
661✔
2935
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
661✔
2936
  pStmt->ignoreExists = ignoreExists;
661✔
2937
  pStmt->pCols = pCols;
661✔
2938
  pStmt->pTags = pTags;
661✔
2939
  pStmt->pOptions = (STableOptions*)pOptions;
661✔
2940
  nodesDestroyNode(pRealTable);
661✔
2941
  return (SNode*)pStmt;
661✔
2942
_err:
×
2943
  nodesDestroyNode(pRealTable);
×
2944
  nodesDestroyList(pCols);
×
2945
  nodesDestroyList(pTags);
×
2946
  nodesDestroyNode(pOptions);
×
2947
  return NULL;
×
2948
}
2949

2950
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
29,472✔
2951
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
2952
  CHECK_PARSER_STATUS(pCxt);
29,472!
2953
  SCreateSubTableClause* pStmt = NULL;
29,472✔
2954
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE, (SNode**)&pStmt);
29,472✔
2955
  CHECK_MAKE_NODE(pStmt);
29,496!
2956
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
29,496✔
2957
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
29,496✔
2958
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
29,496✔
2959
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
29,496✔
2960
  pStmt->ignoreExists = ignoreExists;
29,496✔
2961
  pStmt->pSpecificTags = pSpecificTags;
29,496✔
2962
  pStmt->pValsOfTags = pValsOfTags;
29,496✔
2963
  pStmt->pOptions = (STableOptions*)pOptions;
29,496✔
2964
  nodesDestroyNode(pRealTable);
29,496✔
2965
  nodesDestroyNode(pUseRealTable);
29,483✔
2966
  return (SNode*)pStmt;
29,494✔
2967
_err:
×
2968
  nodesDestroyNode(pRealTable);
×
2969
  nodesDestroyNode(pOptions);
×
2970
  nodesDestroyNode(pUseRealTable);
×
2971
  nodesDestroyList(pSpecificTags);
×
2972
  nodesDestroyList(pValsOfTags);
×
2973
  return NULL;
×
2974
}
2975

2976
SNode* createCreateSubTableFromFileClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pUseRealTable,
×
2977
                                          SNodeList* pSpecificTags, const SToken* pFilePath) {
2978
  CHECK_PARSER_STATUS(pCxt);
×
2979
  SCreateSubTableFromFileClause* pStmt = NULL;
×
2980
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_FROM_FILE_CLAUSE, (SNode**)&pStmt);
×
2981
  CHECK_MAKE_NODE(pStmt);
×
2982
  tstrncpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
2983
  tstrncpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
2984
  pStmt->ignoreExists = ignoreExists;
×
2985
  pStmt->pSpecificTags = pSpecificTags;
×
2986
  if (TK_NK_STRING == pFilePath->type) {
×
2987
    (void)trimString(pFilePath->z, pFilePath->n, pStmt->filePath, PATH_MAX);
×
2988
  } else {
2989
    strncpy(pStmt->filePath, pFilePath->z, pFilePath->n);
×
2990
  }
2991

2992
  nodesDestroyNode(pUseRealTable);
×
2993
  return (SNode*)pStmt;
×
2994
_err:
×
2995
  nodesDestroyNode(pUseRealTable);
×
2996
  nodesDestroyList(pSpecificTags);
×
2997
  return NULL;
×
2998
}
2999

3000
SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
24,842✔
3001
  CHECK_PARSER_STATUS(pCxt);
24,842!
3002
  SCreateMultiTablesStmt* pStmt = NULL;
24,842✔
3003
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT, (SNode**)&pStmt);
24,842✔
3004
  CHECK_MAKE_NODE(pStmt);
24,868!
3005
  pStmt->pSubTables = pSubTables;
24,868✔
3006
  return (SNode*)pStmt;
24,868✔
3007
_err:
×
3008
  nodesDestroyList(pSubTables);
×
3009
  return NULL;
×
3010
}
3011

3012
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
73✔
3013
  CHECK_PARSER_STATUS(pCxt);
73!
3014
  SDropTableClause* pStmt = NULL;
73✔
3015
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE, (SNode**)&pStmt);
73✔
3016
  CHECK_MAKE_NODE(pStmt);
73!
3017
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
73✔
3018
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
73✔
3019
  pStmt->ignoreNotExists = ignoreNotExists;
73✔
3020
  nodesDestroyNode(pRealTable);
73✔
3021
  return (SNode*)pStmt;
73✔
3022
_err:
×
3023
  nodesDestroyNode(pRealTable);
×
3024
  return NULL;
×
3025
}
3026

3027
SNode* createDropTableStmt(SAstCreateContext* pCxt, bool withOpt, SNodeList* pTables) {
45✔
3028
  CHECK_PARSER_STATUS(pCxt);
45!
3029
  SDropTableStmt* pStmt = NULL;
45✔
3030
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT, (SNode**)&pStmt);
45✔
3031
  CHECK_MAKE_NODE(pStmt);
45!
3032
  pStmt->pTables = pTables;
45✔
3033
  pStmt->withOpt = withOpt;
45✔
3034
  return (SNode*)pStmt;
45✔
3035
_err:
×
3036
  nodesDestroyList(pTables);
×
3037
  return NULL;
×
3038
}
3039

3040
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
12✔
3041
  CHECK_PARSER_STATUS(pCxt);
12!
3042
  SDropSuperTableStmt* pStmt = NULL;
12✔
3043
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT, (SNode**)&pStmt);
12✔
3044
  CHECK_MAKE_NODE(pStmt);
12!
3045
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
12✔
3046
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
12✔
3047
  pStmt->ignoreNotExists = ignoreNotExists;
12✔
3048
  pStmt->withOpt = withOpt;
12✔
3049
  nodesDestroyNode(pRealTable);
12✔
3050
  return (SNode*)pStmt;
12✔
3051
_err:
×
3052
  nodesDestroyNode(pRealTable);
×
3053
  return NULL;
×
3054
}
3055

3056
SNode* createDropVirtualTableStmt(SAstCreateContext* pCxt, bool withOpt, bool ignoreNotExists, SNode* pRealTable) {
×
3057
  CHECK_PARSER_STATUS(pCxt);
×
3058
  SDropVirtualTableStmt* pStmt = NULL;
×
3059
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIRTUAL_TABLE_STMT, (SNode**)&pStmt);
×
3060
  CHECK_MAKE_NODE(pStmt);
×
3061
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
3062
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
3063
  pStmt->ignoreNotExists = ignoreNotExists;
×
3064
  pStmt->withOpt = withOpt;
×
3065
  nodesDestroyNode(pRealTable);
×
3066
  return (SNode*)pStmt;
×
3067
_err:
×
3068
  nodesDestroyNode(pRealTable);
×
3069
  return NULL;
×
3070
}
3071

3072
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
5,249✔
3073
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
5,249✔
3074
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
5,249✔
3075
  nodesDestroyNode(pRealTable);
5,249✔
3076
  return (SNode*)pStmt;
5,249✔
3077
}
3078

3079
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
58✔
3080
  CHECK_PARSER_STATUS(pCxt);
58!
3081
  SAlterTableStmt* pStmt = NULL;
58✔
3082
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
58✔
3083
  CHECK_MAKE_NODE(pStmt);
58!
3084
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
58✔
3085
  pStmt->pOptions = (STableOptions*)pOptions;
58✔
3086
  return createAlterTableStmtFinalize(pRealTable, pStmt);
58✔
3087
_err:
×
3088
  nodesDestroyNode(pRealTable);
×
3089
  nodesDestroyNode(pOptions);
×
3090
  return NULL;
×
3091
}
3092

3093
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
2,845✔
3094
                                    SDataType dataType) {
3095
  CHECK_PARSER_STATUS(pCxt);
2,845!
3096
  CHECK_NAME(checkColumnName(pCxt, pColName));
2,845!
3097
  SAlterTableStmt* pStmt = NULL;
2,845✔
3098
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
2,845✔
3099
  CHECK_MAKE_NODE(pStmt);
2,845!
3100
  pStmt->alterType = alterType;
2,845✔
3101
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
2,845✔
3102
  pStmt->dataType = dataType;
2,845✔
3103
  return createAlterTableStmtFinalize(pRealTable, pStmt);
2,845✔
3104
_err:
×
3105
  nodesDestroyNode(pRealTable);
×
3106
  return NULL;
×
3107
}
3108

3109
SNode* createAlterTableAddModifyColOptions2(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
2,120✔
3110
                                            SToken* pColName, SDataType dataType, SNode* pOptions) {
3111
  SAlterTableStmt* pStmt = NULL;
2,120✔
3112
  CHECK_PARSER_STATUS(pCxt);
2,120!
3113
  CHECK_NAME(checkColumnName(pCxt, pColName));
2,120!
3114
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
2,120✔
3115
  CHECK_MAKE_NODE(pStmt);
2,120!
3116
  pStmt->alterType = alterType;
2,120✔
3117
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
2,120✔
3118
  pStmt->dataType = dataType;
2,120✔
3119
  pStmt->pColOptions = (SColumnOptions*)pOptions;
2,120✔
3120

3121
  if (pOptions != NULL) {
2,120!
3122
    SColumnOptions* pOption = (SColumnOptions*)pOptions;
2,120✔
3123
    if (pOption->hasRef) {
2,120!
3124
      if (!pOption->commentNull || pOption->bPrimaryKey || 0 != strcmp(pOption->compress, "") ||
×
3125
          0 != strcmp(pOption->encode, "") || 0 != strcmp(pOption->compressLevel, "")) {
×
3126
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
×
3127
      }
3128
      pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COLUMN_REF;
×
3129
      tstrncpy(pStmt->refDbName, pOption->refDb, TSDB_DB_NAME_LEN);
×
3130
      tstrncpy(pStmt->refTableName, pOption->refTable, TSDB_TABLE_NAME_LEN);
×
3131
      tstrncpy(pStmt->refColName, pOption->refColumn, TSDB_COL_NAME_LEN);
×
3132
      CHECK_PARSER_STATUS(pCxt);
×
3133
    } else if (pOption->bPrimaryKey == false && pOption->commentNull == true) {
2,120!
3134
      if (strlen(pOption->compress) != 0 || strlen(pOption->compressLevel) || strlen(pOption->encode) != 0) {
2,120!
3135
        pStmt->alterType = TSDB_ALTER_TABLE_ADD_COLUMN_WITH_COMPRESS_OPTION;
×
3136
      } else {
3137
        // pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
3138
        //                                         "not support alter column with option except compress");
3139
        // return NULL;
3140
      }
3141
    } else {
3142
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
3143
                                              "not support alter column with option except compress");
3144
      CHECK_PARSER_STATUS(pCxt);
×
3145
    }
3146
  }
3147
  return createAlterTableStmtFinalize(pRealTable, pStmt);
2,120✔
3148
_err:
×
3149
  nodesDestroyNode(pOptions);
×
3150
  nodesDestroyNode((SNode*)pStmt);
×
3151
  nodesDestroyNode(pRealTable);
×
3152
  return NULL;
×
3153
}
3154

3155
SNode* createAlterTableAddModifyColOptions(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
×
3156
                                           SToken* pColName, SNode* pOptions) {
3157
  CHECK_PARSER_STATUS(pCxt);
×
3158
  CHECK_NAME(checkColumnName(pCxt, pColName));
×
3159
  SAlterTableStmt* pStmt = NULL;
×
3160
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
3161
  CHECK_MAKE_NODE(pStmt);
×
3162
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_COLUMN_COMPRESS;
×
3163
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
×
3164
  pStmt->pColOptions = (SColumnOptions*)pOptions;
×
3165
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
3166
_err:
×
3167
  nodesDestroyNode(pOptions);
×
3168
  nodesDestroyNode(pRealTable);
×
3169
  return NULL;
×
3170
}
3171

3172
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
97✔
3173
  CHECK_PARSER_STATUS(pCxt);
97!
3174
  CHECK_NAME(checkColumnName(pCxt, pColName));
97!
3175
  SAlterTableStmt* pStmt = NULL;
97✔
3176
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
97✔
3177
  CHECK_MAKE_NODE(pStmt);
97!
3178
  pStmt->alterType = alterType;
97✔
3179
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
97✔
3180
  return createAlterTableStmtFinalize(pRealTable, pStmt);
97✔
3181
_err:
×
3182
  nodesDestroyNode(pRealTable);
×
3183
  return NULL;
×
3184
}
3185

3186
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
82✔
3187
                                 SToken* pNewColName) {
3188
  CHECK_PARSER_STATUS(pCxt);
82!
3189
  CHECK_NAME(checkColumnName(pCxt, pOldColName));
82!
3190
  CHECK_NAME(checkColumnName(pCxt, pNewColName));
82!
3191
  SAlterTableStmt* pStmt = NULL;
82✔
3192
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
82✔
3193
  CHECK_MAKE_NODE(pStmt);
82!
3194
  pStmt->alterType = alterType;
82✔
3195
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
82✔
3196
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
82✔
3197
  return createAlterTableStmtFinalize(pRealTable, pStmt);
82✔
3198
_err:
×
3199
  nodesDestroyNode(pRealTable);
×
3200
  return NULL;
×
3201
}
3202

3203
SNode* createAlterTableAlterColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
×
3204
                                   SNode* pRef) {
3205
  CHECK_PARSER_STATUS(pCxt);
×
3206
  CHECK_NAME(checkColumnName(pCxt, pColName));
×
3207
  SAlterTableStmt* pStmt = NULL;
×
3208
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
3209
  CHECK_MAKE_NODE(pStmt);
×
3210
  pStmt->alterType = alterType;
×
3211
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
×
3212
  tstrncpy(pStmt->refDbName, ((SColumnRefNode*)pRef)->refDbName, TSDB_DB_NAME_LEN);
×
3213
  tstrncpy(pStmt->refTableName, ((SColumnRefNode*)pRef)->refTableName, TSDB_TABLE_NAME_LEN);
×
3214
  tstrncpy(pStmt->refColName, ((SColumnRefNode*)pRef)->refColName, TSDB_COL_NAME_LEN);
×
3215
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
3216
_err:
×
3217
  nodesDestroyNode(pRealTable);
×
3218
  return NULL;
×
3219
}
3220

3221
SNode* createAlterTableRemoveColRef(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
×
3222
                                    const SToken* pLiteral) {
3223
  CHECK_PARSER_STATUS(pCxt);
×
3224
  CHECK_NAME(checkColumnName(pCxt, pColName));
×
3225
  SAlterTableStmt* pStmt = NULL;
×
3226
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
3227
  CHECK_MAKE_NODE(pStmt);
×
3228
  pStmt->alterType = alterType;
×
3229
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
×
3230
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
3231
_err:
×
3232
  nodesDestroyNode(pRealTable);
×
3233
  return NULL;
×
3234
}
3235

3236
SNode* createAlterSingleTagColumnNode(SAstCreateContext* pCtx, SToken* pTagName, SNode* pVal) {
47✔
3237
  CHECK_PARSER_STATUS(pCtx);
47!
3238
  SAlterTableStmt* pStmt = NULL;
47✔
3239
  pCtx->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
47✔
3240
  CHECK_MAKE_NODE(pStmt);
47!
3241
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
47✔
3242
  CHECK_NAME(checkColumnName(pCtx, pTagName));
47!
3243
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
47✔
3244
  pStmt->pVal = (SValueNode*)pVal;
47✔
3245
  pStmt->pNodeListTagValue = NULL;
47✔
3246
  return (SNode*)pStmt;
47✔
3247
_err:
×
3248
  return NULL;
×
3249
}
3250

3251
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
×
3252
  CHECK_PARSER_STATUS(pCxt);
×
3253
  CHECK_NAME(checkColumnName(pCxt, pTagName));
×
3254
  SAlterTableStmt* pStmt = NULL;
×
3255
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
×
3256
  CHECK_MAKE_NODE(pStmt);
×
3257
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
×
3258
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
×
3259
  pStmt->pVal = (SValueNode*)pVal;
×
3260
  return createAlterTableStmtFinalize(pRealTable, pStmt);
×
3261
_err:
×
3262
  nodesDestroyNode(pVal);
×
3263
  nodesDestroyNode(pRealTable);
×
3264
  return NULL;
×
3265
}
3266

3267
SNode* createAlterTableSetMultiTagValue(SAstCreateContext* pCxt, SNode* pRealTable, SNodeList* pList) {
47✔
3268
  CHECK_PARSER_STATUS(pCxt);
47!
3269
  SAlterTableStmt* pStmt = NULL;
47✔
3270
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT, (SNode**)&pStmt);
47✔
3271

3272
  CHECK_MAKE_NODE(pStmt);
47!
3273
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_MULTI_TAG_VAL;
47✔
3274
  pStmt->pNodeListTagValue = pList;
47✔
3275
  return createAlterTableStmtFinalize(pRealTable, pStmt);
47✔
3276
_err:
×
3277
  return NULL;
×
3278
}
3279

3280
SNode* setAlterSuperTableType(SNode* pStmt) {
159✔
3281
  if (!pStmt) return NULL;
159!
3282
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
159✔
3283
  return pStmt;
159✔
3284
}
3285

3286
SNode* setAlterVirtualTableType(SNode* pStmt) {
×
3287
  if (!pStmt) return NULL;
×
3288
  setNodeType(pStmt, QUERY_NODE_ALTER_VIRTUAL_TABLE_STMT);
×
3289
  return pStmt;
×
3290
}
3291

3292
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
1,957✔
3293
  CHECK_PARSER_STATUS(pCxt);
1,957!
3294
  CHECK_NAME(checkDbName(pCxt, pDbName, false));
1,957!
3295
  SUseDatabaseStmt* pStmt = NULL;
1,957✔
3296
  pCxt->errCode = nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT, (SNode**)&pStmt);
1,957✔
3297
  CHECK_MAKE_NODE(pStmt);
1,957!
3298
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1,957✔
3299
  return (SNode*)pStmt;
1,957✔
3300
_err:
×
3301
  return NULL;
×
3302
}
3303

3304
static bool needDbShowStmt(ENodeType type) {
189✔
3305
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
138✔
3306
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
57✔
3307
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type ||
18!
3308
         QUERY_NODE_SHOW_VIEWS_STMT == type || QUERY_NODE_SHOW_TSMAS_STMT == type ||
18!
3309
         QUERY_NODE_SHOW_USAGE_STMT == type || QUERY_NODE_SHOW_VTABLES_STMT == type ||
327!
3310
         QUERY_NODE_SHOW_STREAMS_STMT == type;
3311
}
3312

3313
SNode* createShowStmtWithLike(SAstCreateContext* pCxt, ENodeType type, SNode* pLikePattern) {
146✔
3314
  CHECK_PARSER_STATUS(pCxt);
146!
3315
  SShowStmt* pStmt = NULL;
146✔
3316
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
146✔
3317
  CHECK_MAKE_NODE(pStmt);
146!
3318
  pStmt->withFull = false;
146✔
3319
  pStmt->pTbName = pLikePattern;
146✔
3320
  if (pLikePattern) {
146!
3321
    pStmt->tableCondType = OP_TYPE_LIKE;
×
3322
  }
3323
  return (SNode*)pStmt;
146✔
3324
_err:
×
3325
  nodesDestroyNode(pLikePattern);
×
3326
  return NULL;
×
3327
}
3328

3329
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
166✔
3330
  CHECK_PARSER_STATUS(pCxt);
166!
3331
  SShowStmt* pStmt = NULL;
166✔
3332
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
166✔
3333
  CHECK_MAKE_NODE(pStmt);
166!
3334
  pStmt->withFull = false;
166✔
3335
  return (SNode*)pStmt;
166✔
3336
_err:
×
3337
  return NULL;
×
3338
}
3339

3340
SNode* createShowStmtWithFull(SAstCreateContext* pCxt, ENodeType type) {
×
3341
  CHECK_PARSER_STATUS(pCxt);
×
3342
  SShowStmt* pStmt = NULL;
×
3343
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3344
  CHECK_MAKE_NODE(pStmt);
×
3345
  pStmt->withFull = true;
×
3346
  return (SNode*)pStmt;
×
3347
_err:
×
3348
  return NULL;
×
3349
}
3350

3351
SNode* createShowCompactsStmt(SAstCreateContext* pCxt, ENodeType type) {
×
3352
  CHECK_PARSER_STATUS(pCxt);
×
3353
  SShowCompactsStmt* pStmt = NULL;
×
3354
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3355
  CHECK_MAKE_NODE(pStmt);
×
3356
  return (SNode*)pStmt;
×
3357
_err:
×
3358
  return NULL;
×
3359
}
3360

3361
SNode* setShowKind(SAstCreateContext* pCxt, SNode* pStmt, EShowKind showKind) {
138✔
3362
  if (pStmt == NULL) {
138!
3363
    return NULL;
×
3364
  }
3365
  SShowStmt* pShow = (SShowStmt*)pStmt;
138✔
3366
  pShow->showKind = showKind;
138✔
3367
  return pStmt;
138✔
3368
}
3369

3370
SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
177✔
3371
                              EOperatorType tableCondType) {
3372
  CHECK_PARSER_STATUS(pCxt);
177!
3373
  if (needDbShowStmt(type) && NULL == pDbName) {
177!
3374
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
1✔
3375
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1✔
3376
    CHECK_PARSER_STATUS(pCxt);
1!
3377
  }
3378
  SShowStmt* pStmt = NULL;
176✔
3379
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
176✔
3380
  CHECK_MAKE_NODE(pStmt);
176!
3381
  pStmt->pDbName = pDbName;
176✔
3382
  pStmt->pTbName = pTbName;
176✔
3383
  pStmt->tableCondType = tableCondType;
176✔
3384
  return (SNode*)pStmt;
176✔
3385
_err:
1✔
3386
  nodesDestroyNode(pDbName);
1✔
3387
  nodesDestroyNode(pTbName);
1✔
3388
  return NULL;
1✔
3389
}
3390

3391
SNode* createShowTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
51✔
3392
                            EOperatorType tableCondType) {
3393
  CHECK_PARSER_STATUS(pCxt);
51!
3394
  SNode* pDbName = NULL;
51✔
3395
  if (option.dbName.type == TK_NK_NIL) {
51✔
3396
    pDbName = createDefaultDatabaseCondValue(pCxt);
26✔
3397
  } else {
3398
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
25✔
3399
  }
3400

3401
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
51!
3402
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3403
    return NULL;
×
3404
  }
3405

3406
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_TABLES_STMT, pDbName, pTbName, tableCondType);
51✔
3407
  CHECK_PARSER_STATUS(pCxt);
51✔
3408
  (void)setShowKind(pCxt, pStmt, option.kind);
50✔
3409
  return pStmt;
50✔
3410
_err:
1✔
3411
  nodesDestroyNode(pTbName);
1✔
3412
  return NULL;
1✔
3413
}
3414

3415
SNode* createShowVTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
6✔
3416
                             EOperatorType tableCondType) {
3417
  CHECK_PARSER_STATUS(pCxt);
6!
3418
  SNode* pDbName = NULL;
6✔
3419
  if (option.dbName.type == TK_NK_NIL) {
6!
3420
    pDbName = createDefaultDatabaseCondValue(pCxt);
×
3421
  } else {
3422
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
6✔
3423
  }
3424

3425
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_CHILD && option.kind != SHOW_KIND_ALL) {
6!
3426
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3427
    return NULL;
×
3428
  }
3429

3430
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_VTABLES_STMT, pDbName, pTbName, tableCondType);
6✔
3431
  CHECK_PARSER_STATUS(pCxt);
6!
3432
  (void)setShowKind(pCxt, pStmt, option.kind);
6✔
3433
  return pStmt;
6✔
3434
_err:
×
3435
  nodesDestroyNode(pTbName);
×
3436
  return NULL;
×
3437
}
3438

3439
SNode* createShowSTablesStmt(SAstCreateContext* pCxt, SShowTablesOption option, SNode* pTbName,
52✔
3440
                             EOperatorType tableCondType) {
3441
  CHECK_PARSER_STATUS(pCxt);
52!
3442
  SNode* pDbName = NULL;
52✔
3443
  if (option.dbName.type == TK_NK_NIL) {
52✔
3444
    pDbName = createDefaultDatabaseCondValue(pCxt);
24✔
3445
  } else {
3446
    pDbName = createIdentifierValueNode(pCxt, &option.dbName);
28✔
3447
  }
3448

3449
  if (option.kind != SHOW_KIND_TABLES_NORMAL && option.kind != SHOW_KIND_TABLES_VIRTUAL &&
52✔
3450
      option.kind != SHOW_KIND_ALL) {
50!
3451
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3452
    return NULL;
×
3453
  }
3454

3455
  SNode* pStmt = createShowStmtWithCond(pCxt, QUERY_NODE_SHOW_STABLES_STMT, pDbName, pTbName, tableCondType);
52✔
3456
  CHECK_PARSER_STATUS(pCxt);
52!
3457
  (void)setShowKind(pCxt, pStmt, option.kind);
52✔
3458
  return pStmt;
52✔
3459
_err:
×
3460
  nodesDestroyNode(pTbName);
×
3461
  return NULL;
×
3462
}
3463

3464
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
12✔
3465
  CHECK_PARSER_STATUS(pCxt);
12!
3466
  CHECK_NAME(checkDbName(pCxt, pDbName, true));
12!
3467
  SShowCreateDatabaseStmt* pStmt = NULL;
12✔
3468
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT, (SNode**)&pStmt);
12✔
3469
  CHECK_MAKE_NODE(pStmt);
12!
3470
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
12✔
3471
  return (SNode*)pStmt;
12✔
3472
_err:
×
3473
  return NULL;
×
3474
}
3475

3476
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
×
3477
  CHECK_PARSER_STATUS(pCxt);
×
3478
  SToken  dbToken = {0};
×
3479
  SToken* pDbToken = NULL;
×
3480

3481
  if (pNode) {
×
3482
    SValueNode* pDbName = (SValueNode*)pNode;
×
3483
    if (pDbName->literal) {
×
3484
      dbToken.z = pDbName->literal;
×
3485
      dbToken.n = strlen(pDbName->literal);
×
3486
      pDbToken = &dbToken;
×
3487
    }
3488
  }
3489

3490
  if (pDbToken) {
×
3491
    CHECK_NAME(checkDbName(pCxt, pDbToken, true));
×
3492
  }
3493

3494
  SShowAliveStmt* pStmt = NULL;
×
3495
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3496
  CHECK_PARSER_STATUS(pCxt);
×
3497

3498
  if (pDbToken) {
×
3499
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
×
3500
  }
3501
  if (pNode) {
×
3502
    nodesDestroyNode(pNode);
×
3503
  }
3504

3505
  return (SNode*)pStmt;
×
3506
_err:
×
3507
  nodesDestroyNode(pNode);
×
3508
  return NULL;
×
3509
}
3510

3511
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
100✔
3512
  CHECK_PARSER_STATUS(pCxt);
100!
3513
  SShowCreateTableStmt* pStmt = NULL;
100✔
3514
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
100✔
3515
  CHECK_MAKE_NODE(pStmt);
100!
3516
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
100✔
3517
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
100✔
3518
  nodesDestroyNode(pRealTable);
100✔
3519
  return (SNode*)pStmt;
100✔
3520
_err:
×
3521
  nodesDestroyNode(pRealTable);
×
3522
  return NULL;
×
3523
}
3524

3525
SNode* createShowCreateVTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
2✔
3526
  CHECK_PARSER_STATUS(pCxt);
2!
3527
  SShowCreateTableStmt* pStmt = NULL;
2✔
3528
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
2✔
3529
  CHECK_MAKE_NODE(pStmt);
2!
3530
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
2✔
3531
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
2✔
3532
  nodesDestroyNode(pRealTable);
2✔
3533
  return (SNode*)pStmt;
2✔
3534
_err:
×
3535
  nodesDestroyNode(pRealTable);
×
3536
  return NULL;
×
3537
}
3538

3539
SNode* createShowCreateViewStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
×
3540
  CHECK_PARSER_STATUS(pCxt);
×
3541
  SShowCreateViewStmt* pStmt = NULL;
×
3542
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
3543
  CHECK_MAKE_NODE(pStmt);
×
3544
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
×
3545
  tstrncpy(pStmt->viewName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
×
3546
  nodesDestroyNode(pRealTable);
×
3547
  return (SNode*)pStmt;
×
3548
_err:
×
3549
  nodesDestroyNode(pRealTable);
×
3550
  return NULL;
×
3551
}
3552

3553
SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
18✔
3554
  CHECK_PARSER_STATUS(pCxt);
18!
3555
  SShowTableDistributedStmt* pStmt = NULL;
18✔
3556
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT, (SNode**)&pStmt);
18✔
3557
  CHECK_MAKE_NODE(pStmt);
18!
3558
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
18✔
3559
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
18✔
3560
  nodesDestroyNode(pRealTable);
18✔
3561
  return (SNode*)pStmt;
18✔
3562
_err:
×
3563
  nodesDestroyNode(pRealTable);
×
3564
  return NULL;
×
3565
}
3566

3567
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
30✔
3568
  CHECK_PARSER_STATUS(pCxt);
30!
3569
  SShowDnodeVariablesStmt* pStmt = NULL;
30✔
3570
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT, (SNode**)&pStmt);
30✔
3571
  CHECK_MAKE_NODE(pStmt);
30!
3572
  pStmt->pDnodeId = pDnodeId;
30✔
3573
  pStmt->pLikePattern = pLikePattern;
30✔
3574
  return (SNode*)pStmt;
30✔
3575
_err:
×
3576
  nodesDestroyNode(pDnodeId);
×
3577
  nodesDestroyNode(pLikePattern);
×
3578
  return NULL;
×
3579
}
3580

3581
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
24✔
3582
  CHECK_PARSER_STATUS(pCxt);
24!
3583
  SShowVnodesStmt* pStmt = NULL;
24✔
3584
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT, (SNode**)&pStmt);
24✔
3585
  CHECK_MAKE_NODE(pStmt);
24!
3586
  pStmt->pDnodeId = pDnodeId;
24✔
3587
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
24✔
3588
  return (SNode*)pStmt;
24✔
3589
_err:
×
3590
  nodesDestroyNode(pDnodeId);
×
3591
  nodesDestroyNode(pDnodeEndpoint);
×
3592
  return NULL;
×
3593
}
3594

3595
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
39✔
3596
  CHECK_PARSER_STATUS(pCxt);
39!
3597
  if (NULL == pDbName) {
39!
3598
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
3599
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3600
    CHECK_PARSER_STATUS(pCxt);
×
3601
  }
3602
  SShowTableTagsStmt* pStmt = NULL;
39✔
3603
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT, (SNode**)&pStmt);
39✔
3604
  CHECK_MAKE_NODE(pStmt);
39!
3605
  pStmt->pDbName = pDbName;
39✔
3606
  pStmt->pTbName = pTbName;
39✔
3607
  pStmt->pTags = pTags;
39✔
3608
  return (SNode*)pStmt;
39✔
3609
_err:
×
3610
  nodesDestroyNode(pTbName);
×
3611
  nodesDestroyNode(pDbName);
×
3612
  nodesDestroyList(pTags);
×
3613
  return NULL;
×
3614
}
3615

3616
SNode* createShowCompactDetailsStmt(SAstCreateContext* pCxt, SNode* pCompactId) {
×
3617
  CHECK_PARSER_STATUS(pCxt);
×
3618
  SShowCompactDetailsStmt* pStmt = NULL;
×
3619
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_COMPACT_DETAILS_STMT, (SNode**)&pStmt);
×
3620
  CHECK_MAKE_NODE(pStmt);
×
3621
  pStmt->pCompactId = pCompactId;
×
3622
  return (SNode*)pStmt;
×
3623
_err:
×
3624
  nodesDestroyNode(pCompactId);
×
3625
  return NULL;
×
3626
}
3627

3628
SNode* createShowTransactionDetailsStmt(SAstCreateContext* pCxt, SNode* pTransactionIdNode) {
×
3629
  CHECK_PARSER_STATUS(pCxt);
×
3630
  SShowTransactionDetailsStmt* pStmt = NULL;
×
3631
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TRANSACTION_DETAILS_STMT, (SNode**)&pStmt);
×
3632
  CHECK_MAKE_NODE(pStmt);
×
3633
  pStmt->pTransactionId = pTransactionIdNode;
×
3634
  return (SNode*)pStmt;
×
3635
_err:
×
3636
  nodesDestroyNode(pTransactionIdNode);
×
3637
  return NULL;
×
3638
}
3639

3640
static int32_t getIpRangeFromStr(char* ipRange, SIpRange* pIpRange) {
×
3641
  int32_t code = 0;
×
3642

3643
  int8_t isIp6 = ((strchr(ipRange, ':')) != NULL ? 1 : 0);
×
3644
  if (isIp6) {
×
3645
    struct in6_addr ip6;
3646
    if (inet_pton(AF_INET6, ipRange, &ip6) == 1) {
×
3647
      pIpRange->type = 1;
×
3648
      memcpy(&pIpRange->ipV6.addr[0], ip6.s6_addr, 8);
×
3649
      memcpy(&pIpRange->ipV6.addr[1], ip6.s6_addr + 8, 8);
×
3650

3651
    } else {
3652
      return TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3653
    }
3654
  } else {
3655
    struct in_addr ip4;
3656
    if (inet_pton(AF_INET, ipRange, &ip4) == 1) {
×
3657
      pIpRange->type = 0;
×
3658
      memcpy(&pIpRange->ipV4.ip, &ip4.s_addr, sizeof(ip4.s_addr));
×
3659
    } else {
3660
      return TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3661
    }
3662
  }
3663

3664
  return code;
×
3665
}
3666
static int32_t getIpRangeFromWhitelistItem(char* ipRange, SIpRange* pIpRange) {
×
3667
  int32_t code = TSDB_CODE_SUCCESS;
×
3668
  int32_t lino = 0;
×
3669
  char*   ipCopy = NULL;
×
3670
  int32_t mask = 0;
×
3671
#ifndef TD_ASTRA
3672

3673
  ipCopy = taosStrdup(ipRange);
×
3674
  if (ipCopy == NULL) {
×
3675
    code = terrno;
×
3676
    TAOS_CHECK_GOTO(code, &lino, _error);
×
3677
  }
3678

3679
  char* slash = strchr(ipCopy, '/');
×
3680
  if (slash) {
×
3681
    *slash = '\0';
×
3682
    code = taosStr2int32(slash + 1, &mask);
×
3683
    TAOS_CHECK_GOTO(code, &lino, _error);
×
3684
  }
3685

3686
  code = getIpRangeFromStr(ipCopy, pIpRange);
×
3687
  TAOS_CHECK_GOTO(code, &lino, _error);
×
3688

3689
  if (!slash) {
×
3690
    mask = pIpRange->type == 0 ? 32 : 128;
×
3691
  }
3692
  code = tIpRangeSetMask(pIpRange, mask);
×
3693
  TAOS_CHECK_GOTO(code, &lino, _error);
×
3694

3695
#endif
3696
_error:
×
3697
  taosMemoryFreeClear(ipCopy);
×
3698
  return code;
×
3699
}
3700

3701
static int32_t fillIpRangesFromWhiteList(SAstCreateContext* pCxt, SNodeList* pIpRangesNodeList, SIpRange* pIpRanges) {
×
3702
  int32_t i = 0;
×
3703
  int32_t code = 0;
×
3704

3705
  SNode* pNode = NULL;
×
3706
  FOREACH(pNode, pIpRangesNodeList) {
×
3707
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
×
3708
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IP_RANGE);
×
3709
      return TSDB_CODE_PAR_INVALID_IP_RANGE;
×
3710
    }
3711
    SValueNode* pValNode = (SValueNode*)(pNode);
×
3712
    code = getIpRangeFromWhitelistItem(pValNode->literal, pIpRanges + i);
×
3713
    ++i;
×
3714
    if (code != TSDB_CODE_SUCCESS) {
×
3715
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Invalid IP range %s", pValNode->literal);
×
3716
      return code;
×
3717
    }
3718
  }
3719
  return TSDB_CODE_SUCCESS;
×
3720
}
3721

3722
SNode* addCreateUserStmtWhiteList(SAstCreateContext* pCxt, SNode* pCreateUserStmt, SNodeList* pIpRangesNodeList) {
55✔
3723
  if (NULL == pCreateUserStmt) {
55✔
3724
    if (pIpRangesNodeList != NULL) {
1!
3725
      nodesDestroyList(pIpRangesNodeList);
×
3726
    }
3727
    return NULL;
1✔
3728
  }
3729

3730
  if (NULL == pIpRangesNodeList) {
54!
3731
    return pCreateUserStmt;
54✔
3732
  }
3733

3734
  ((SCreateUserStmt*)pCreateUserStmt)->pNodeListIpRanges = pIpRangesNodeList;
×
3735
  SCreateUserStmt* pCreateUser = (SCreateUserStmt*)pCreateUserStmt;
×
3736
  pCreateUser->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
×
3737
  pCreateUser->pIpRanges = taosMemoryMalloc(pCreateUser->numIpRanges * sizeof(SIpRange));
×
3738
  CHECK_OUT_OF_MEM(pCreateUser->pIpRanges);
×
3739

3740
  pCxt->errCode = fillIpRangesFromWhiteList(pCxt, pIpRangesNodeList, pCreateUser->pIpRanges);
×
3741
  CHECK_PARSER_STATUS(pCxt);
×
3742

3743
  return pCreateUserStmt;
×
3744
_err:
×
3745
  nodesDestroyNode(pCreateUserStmt);
×
3746
  nodesDestroyList(pIpRangesNodeList);
×
3747
  return NULL;
×
3748
}
3749

3750
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo,
55✔
3751
                            int8_t createDb, int8_t is_import) {
3752
  CHECK_PARSER_STATUS(pCxt);
55!
3753
  char password[TSDB_USET_PASSWORD_LONGLEN + 3] = {0};
55✔
3754
  CHECK_NAME(checkUserName(pCxt, pUserName));
55!
3755
  if (is_import == 0) {
55!
3756
    CHECK_NAME(checkPassword(pCxt, pPassword, password));
55✔
3757
  } else {
3758
    CHECK_NAME(checkImportPassword(pCxt, pPassword, password));
×
3759
  }
3760
  SCreateUserStmt* pStmt = NULL;
54✔
3761
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_USER_STMT, (SNode**)&pStmt);
54✔
3762
  CHECK_MAKE_NODE(pStmt);
54!
3763
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
54✔
3764
  tstrncpy(pStmt->password, password, TSDB_USET_PASSWORD_LONGLEN);
54✔
3765
  pStmt->sysinfo = sysinfo;
54✔
3766
  pStmt->createDb = createDb;
54✔
3767
  pStmt->isImport = is_import;
54✔
3768
  return (SNode*)pStmt;
54✔
3769
_err:
1✔
3770
  return NULL;
1✔
3771
}
3772

3773
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, void* pAlterInfo) {
36✔
3774
  SAlterUserStmt* pStmt = NULL;
36✔
3775
  CHECK_PARSER_STATUS(pCxt);
36!
3776
  CHECK_NAME(checkUserName(pCxt, pUserName));
36!
3777
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_USER_STMT, (SNode**)&pStmt);
36✔
3778
  CHECK_MAKE_NODE(pStmt);
36!
3779
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
36✔
3780
  pStmt->alterType = alterType;
36✔
3781
  switch (alterType) {
36!
3782
    case TSDB_ALTER_USER_PASSWD: {
12✔
3783
      char    password[TSDB_USET_PASSWORD_LONGLEN] = {0};
12✔
3784
      SToken* pVal = pAlterInfo;
12✔
3785
      CHECK_NAME(checkPassword(pCxt, pVal, password));
12!
3786
      tstrncpy(pStmt->password, password, TSDB_USET_PASSWORD_LONGLEN);
12✔
3787
      break;
12✔
3788
    }
3789
    case TSDB_ALTER_USER_ENABLE: {
12✔
3790
      SToken* pVal = pAlterInfo;
12✔
3791
      pStmt->enable = taosStr2Int8(pVal->z, NULL, 10);
12✔
3792
      break;
12✔
3793
    }
3794
    case TSDB_ALTER_USER_SYSINFO: {
12✔
3795
      SToken* pVal = pAlterInfo;
12✔
3796
      pStmt->sysinfo = taosStr2Int8(pVal->z, NULL, 10);
12✔
3797
      break;
12✔
3798
    }
3799
    case TSDB_ALTER_USER_CREATEDB: {
×
3800
      SToken* pVal = pAlterInfo;
×
3801
      pStmt->createdb = taosStr2Int8(pVal->z, NULL, 10);
×
3802
      break;
×
3803
    }
3804
    case TSDB_ALTER_USER_ADD_WHITE_LIST:
×
3805
    case TSDB_ALTER_USER_DROP_WHITE_LIST: {
3806
      SNodeList* pIpRangesNodeList = pAlterInfo;
×
3807
      pStmt->pNodeListIpRanges = pIpRangesNodeList;
×
3808
      pStmt->numIpRanges = LIST_LENGTH(pIpRangesNodeList);
×
3809
      pStmt->pIpRanges = taosMemoryMalloc(pStmt->numIpRanges * sizeof(SIpRange));
×
3810
      CHECK_OUT_OF_MEM(pStmt->pIpRanges);
×
3811

3812
      pCxt->errCode = fillIpRangesFromWhiteList(pCxt, pIpRangesNodeList, pStmt->pIpRanges);
×
3813
      CHECK_PARSER_STATUS(pCxt);
×
3814
      break;
×
3815
    }
3816
    default:
×
3817
      break;
×
3818
  }
3819
  return (SNode*)pStmt;
36✔
3820
_err:
×
3821
  nodesDestroyNode((SNode*)pStmt);
×
3822
  return NULL;
×
3823
}
3824

3825
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
13✔
3826
  CHECK_PARSER_STATUS(pCxt);
13!
3827
  CHECK_NAME(checkUserName(pCxt, pUserName));
13!
3828
  SDropUserStmt* pStmt = NULL;
13✔
3829
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_USER_STMT, (SNode**)&pStmt);
13✔
3830
  CHECK_MAKE_NODE(pStmt);
13!
3831
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
13✔
3832
  return (SNode*)pStmt;
13✔
3833
_err:
×
3834
  return NULL;
×
3835
}
3836

3837
SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
76✔
3838
  CHECK_PARSER_STATUS(pCxt);
76!
3839
  SCreateDnodeStmt* pStmt = NULL;
76✔
3840
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT, (SNode**)&pStmt);
76✔
3841
  CHECK_MAKE_NODE(pStmt);
76!
3842
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
76!
3843
    nodesDestroyNode((SNode*)pStmt);
×
3844
    return NULL;
×
3845
  }
3846
  return (SNode*)pStmt;
76✔
3847
_err:
×
3848
  return NULL;
×
3849
}
3850

3851
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
108✔
3852
  CHECK_PARSER_STATUS(pCxt);
108!
3853
  SDropDnodeStmt* pStmt = NULL;
108✔
3854
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT, (SNode**)&pStmt);
108✔
3855
  CHECK_MAKE_NODE(pStmt);
108!
3856
  if (TK_NK_INTEGER == pDnode->type) {
108✔
3857
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
36✔
3858
  } else {
3859
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
72!
3860
      nodesDestroyNode((SNode*)pStmt);
×
3861
      return NULL;
×
3862
    }
3863
  }
3864
  pStmt->force = force;
108✔
3865
  pStmt->unsafe = unsafe;
108✔
3866
  return (SNode*)pStmt;
108✔
3867
_err:
×
3868
  return NULL;
×
3869
}
3870

3871
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
52✔
3872
                            const SToken* pValue) {
3873
  CHECK_PARSER_STATUS(pCxt);
52!
3874
  SAlterDnodeStmt* pStmt = NULL;
52✔
3875
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT, (SNode**)&pStmt);
52✔
3876
  CHECK_MAKE_NODE(pStmt);
52!
3877
  if (NULL != pDnode) {
52✔
3878
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
28✔
3879
  } else {
3880
    pStmt->dnodeId = -1;
24✔
3881
  }
3882
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
52✔
3883
  if (NULL != pValue) {
52✔
3884
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
24✔
3885
  }
3886
  return (SNode*)pStmt;
52✔
3887
_err:
×
3888
  return NULL;
×
3889
}
3890

3891
SNode* createCreateAnodeStmt(SAstCreateContext* pCxt, const SToken* pUrl) {
×
3892
  CHECK_PARSER_STATUS(pCxt);
×
3893
  SCreateAnodeStmt* pStmt = NULL;
×
3894
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_ANODE_STMT, (SNode**)&pStmt);
×
3895
  CHECK_MAKE_NODE(pStmt);
×
3896
  (void)trimString(pUrl->z, pUrl->n, pStmt->url, sizeof(pStmt->url));
×
3897
  return (SNode*)pStmt;
×
3898
_err:
×
3899
  return NULL;
×
3900
}
3901

3902
SNode* createDropAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode) {
×
3903
  CHECK_PARSER_STATUS(pCxt);
×
3904
  SUpdateAnodeStmt* pStmt = NULL;
×
3905
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_ANODE_STMT, (SNode**)&pStmt);
×
3906
  CHECK_MAKE_NODE(pStmt);
×
3907
  if (NULL != pAnode) {
×
3908
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
3909
  } else {
3910
    pStmt->anodeId = -1;
×
3911
  }
3912
  return (SNode*)pStmt;
×
3913
_err:
×
3914
  return NULL;
×
3915
}
3916

3917
SNode* createUpdateAnodeStmt(SAstCreateContext* pCxt, const SToken* pAnode, bool updateAll) {
×
3918
  CHECK_PARSER_STATUS(pCxt);
×
3919
  SUpdateAnodeStmt* pStmt = NULL;
×
3920
  pCxt->errCode = nodesMakeNode(QUERY_NODE_UPDATE_ANODE_STMT, (SNode**)&pStmt);
×
3921
  CHECK_MAKE_NODE(pStmt);
×
3922
  if (NULL != pAnode) {
×
3923
    pStmt->anodeId = taosStr2Int32(pAnode->z, NULL, 10);
×
3924
  } else {
3925
    pStmt->anodeId = -1;
×
3926
  }
3927
  return (SNode*)pStmt;
×
3928
_err:
×
3929
  return NULL;
×
3930
}
3931

3932
SNode* createCreateBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId, SNode* pOptions) {
×
3933
  CHECK_PARSER_STATUS(pCxt);
×
3934
  SCreateBnodeStmt* pStmt = NULL;
×
3935
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_BNODE_STMT, (SNode**)&pStmt);
×
3936
  CHECK_MAKE_NODE(pStmt);
×
3937
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
×
3938

3939
  pStmt->pOptions = (SBnodeOptions*)pOptions;
×
3940

3941
  return (SNode*)pStmt;
×
3942
_err:
×
3943
  return NULL;
×
3944
}
3945

3946
SNode* createDropBnodeStmt(SAstCreateContext* pCxt, const SToken* pDnodeId) {
×
3947
  CHECK_PARSER_STATUS(pCxt);
×
3948
  SUpdateBnodeStmt* pStmt = NULL;
×
3949
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_BNODE_STMT, (SNode**)&pStmt);
×
3950
  CHECK_MAKE_NODE(pStmt);
×
3951
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
×
3952

3953
  return (SNode*)pStmt;
×
3954
_err:
×
3955
  return NULL;
×
3956
}
3957

3958
SNode* createDefaultBnodeOptions(SAstCreateContext* pCxt) {
×
3959
  CHECK_PARSER_STATUS(pCxt);
×
3960
  SBnodeOptions* pOptions = NULL;
×
3961
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BNODE_OPTIONS, (SNode**)&pOptions);
×
3962
  CHECK_MAKE_NODE(pOptions);
×
3963

3964
  tstrncpy(pOptions->protoStr, TSDB_BNODE_OPT_PROTO_DFT_STR, TSDB_BNODE_OPT_PROTO_STR_LEN);
×
3965
  pOptions->proto = TSDB_BNODE_OPT_PROTO_DEFAULT;
×
3966

3967
  return (SNode*)pOptions;
×
3968
_err:
×
3969
  return NULL;
×
3970
}
3971

3972
static SNode* setBnodeOptionImpl(SAstCreateContext* pCxt, SNode* pBodeOptions, EBnodeOptionType type, void* pVal,
×
3973
                                 bool alter) {
3974
  CHECK_PARSER_STATUS(pCxt);
×
3975
  SBnodeOptions* pOptions = (SBnodeOptions*)pBodeOptions;
×
3976
  switch (type) {
×
3977
    case BNODE_OPTION_PROTOCOL:
×
3978
      COPY_STRING_FORM_STR_TOKEN(pOptions->protoStr, (SToken*)pVal);
×
3979
      break;
×
3980
    default:
×
3981
      break;
×
3982
  }
3983

3984
  return pBodeOptions;
×
3985
_err:
×
3986
  nodesDestroyNode(pBodeOptions);
×
3987
  return NULL;
×
3988
}
3989

3990
SNode* setBnodeOption(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pOption, void* pVal) {
×
3991
  if (0 == strncasecmp(pOption->z, "protocol", 8)) {
×
3992
    return setBnodeOptionImpl(pCxt, pOptions, BNODE_OPTION_PROTOCOL, pVal, false);
×
3993
  } else {
3994
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
3995
    return pOptions;
×
3996
  }
3997
}
3998

3999
SNode* createEncryptKeyStmt(SAstCreateContext* pCxt, const SToken* pValue) {
×
4000
  SToken config;
4001
  config.type = TK_NK_STRING;
×
4002
  config.z = "\"encrypt_key\"";
×
4003
  config.n = strlen(config.z);
×
4004
  return createAlterDnodeStmt(pCxt, NULL, &config, pValue);
×
4005
}
4006

4007
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
24✔
4008
  if (!checkIndexName(pCxt, pIndexName)) {
24!
4009
    return NULL;
×
4010
  }
4011
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
24✔
4012
}
4013

4014
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
×
4015
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
4016
  CHECK_PARSER_STATUS(pCxt);
×
4017
  SCreateIndexStmt* pStmt = NULL;
×
4018
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT, (SNode**)&pStmt);
×
4019
  CHECK_MAKE_NODE(pStmt);
×
4020
  pStmt->indexType = type;
×
4021
  pStmt->ignoreExists = ignoreExists;
×
4022

4023
  SRealTableNode* pFullTable = (SRealTableNode*)pRealTable;
×
4024
  if (strlen(pFullTable->table.dbName) == 0) {
×
4025
    // no db specified,
4026
    if (pCxt->pQueryCxt->db == NULL) {
×
4027
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
×
4028
      CHECK_PARSER_STATUS(pCxt);
×
4029
    } else {
4030
      snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pCxt->pQueryCxt->db);
×
4031
    }
4032
  } else {
4033
    snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", pFullTable->table.dbName);
×
4034
  }
4035
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SColumnNode*)pIndexName)->colName);
×
4036
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
×
4037
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
×
4038
  nodesDestroyNode(pIndexName);
×
4039
  nodesDestroyNode(pRealTable);
×
4040
  pStmt->pCols = pCols;
×
4041
  pStmt->pOptions = (SIndexOptions*)pOptions;
×
4042
  return (SNode*)pStmt;
×
4043
_err:
×
4044
  nodesDestroyNode(pIndexName);
×
4045
  nodesDestroyNode(pRealTable);
×
4046
  nodesDestroyNode(pOptions);
×
4047
  nodesDestroyList(pCols);
×
4048
  return NULL;
×
4049
}
4050

4051
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
×
4052
                         SNode* pStreamOptions) {
4053
  CHECK_PARSER_STATUS(pCxt);
×
4054
  SIndexOptions* pOptions = NULL;
×
4055
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS, (SNode**)&pOptions);
×
4056
  CHECK_MAKE_NODE(pOptions);
×
4057
  pOptions->pFuncs = pFuncs;
×
4058
  pOptions->pInterval = pInterval;
×
4059
  pOptions->pOffset = pOffset;
×
4060
  pOptions->pSliding = pSliding;
×
4061
  pOptions->pStreamOptions = pStreamOptions;
×
4062
  return (SNode*)pOptions;
×
4063
_err:
×
4064
  nodesDestroyNode(pInterval);
×
4065
  nodesDestroyNode(pOffset);
×
4066
  nodesDestroyNode(pSliding);
×
4067
  nodesDestroyNode(pStreamOptions);
×
4068
  return NULL;
×
4069
}
4070

4071
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
24✔
4072
  CHECK_PARSER_STATUS(pCxt);
24!
4073
  SDropIndexStmt* pStmt = NULL;
24✔
4074
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT, (SNode**)&pStmt);
24✔
4075
  CHECK_MAKE_NODE(pStmt);
24!
4076
  pStmt->ignoreNotExists = ignoreNotExists;
24✔
4077
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
24✔
4078
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
24✔
4079
  nodesDestroyNode(pIndexName);
24✔
4080
  return (SNode*)pStmt;
24✔
4081
_err:
×
4082
  nodesDestroyNode(pIndexName);
×
4083
  return NULL;
×
4084
}
4085

4086
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
36✔
4087
  CHECK_PARSER_STATUS(pCxt);
36!
4088
  SCreateComponentNodeStmt* pStmt = NULL;
36✔
4089
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
36✔
4090
  CHECK_MAKE_NODE(pStmt);
36!
4091
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
36✔
4092
  return (SNode*)pStmt;
36✔
4093
_err:
×
4094
  return NULL;
×
4095
}
4096

4097
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
36✔
4098
  CHECK_PARSER_STATUS(pCxt);
36!
4099
  SDropComponentNodeStmt* pStmt = NULL;
36✔
4100
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
36✔
4101
  CHECK_MAKE_NODE(pStmt);
36!
4102
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
36✔
4103
  return (SNode*)pStmt;
36✔
4104
_err:
×
4105
  return NULL;
×
4106
}
4107

4108
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
48✔
4109
  CHECK_PARSER_STATUS(pCxt);
48!
4110
  SRestoreComponentNodeStmt* pStmt = NULL;
48✔
4111
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
48✔
4112
  CHECK_MAKE_NODE(pStmt);
48!
4113
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
48✔
4114
  return (SNode*)pStmt;
48✔
4115
_err:
×
4116
  return NULL;
×
4117
}
4118

4119
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery) {
58✔
4120
  CHECK_PARSER_STATUS(pCxt);
58!
4121
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
58!
4122
  SCreateTopicStmt* pStmt = NULL;
58✔
4123
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
58✔
4124
  CHECK_MAKE_NODE(pStmt);
58!
4125
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
58✔
4126
  pStmt->ignoreExists = ignoreExists;
58✔
4127
  pStmt->pQuery = pQuery;
58✔
4128
  return (SNode*)pStmt;
58✔
4129
_err:
×
4130
  nodesDestroyNode(pQuery);
×
4131
  return NULL;
×
4132
}
4133

4134
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
51✔
4135
                                  int8_t withMeta) {
4136
  CHECK_PARSER_STATUS(pCxt);
51!
4137
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
51!
4138
  CHECK_NAME(checkDbName(pCxt, pSubDbName, true));
51!
4139
  SCreateTopicStmt* pStmt = NULL;
51✔
4140
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
51✔
4141
  CHECK_MAKE_NODE(pStmt);
51!
4142
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
51✔
4143
  pStmt->ignoreExists = ignoreExists;
51✔
4144
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
51✔
4145
  pStmt->withMeta = withMeta;
51✔
4146
  return (SNode*)pStmt;
51✔
4147
_err:
×
4148
  return NULL;
×
4149
}
4150

4151
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
52✔
4152
                                     int8_t withMeta, SNode* pWhere) {
4153
  CHECK_PARSER_STATUS(pCxt);
52!
4154
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
52!
4155
  SCreateTopicStmt* pStmt = NULL;
52✔
4156
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT, (SNode**)&pStmt);
52✔
4157
  CHECK_MAKE_NODE(pStmt);
52!
4158
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
52✔
4159
  pStmt->ignoreExists = ignoreExists;
52✔
4160
  pStmt->withMeta = withMeta;
52✔
4161
  pStmt->pWhere = pWhere;
52✔
4162

4163
  tstrncpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
52✔
4164
  tstrncpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
52✔
4165
  nodesDestroyNode(pRealTable);
52✔
4166
  return (SNode*)pStmt;
52✔
4167
_err:
×
4168
  nodesDestroyNode(pRealTable);
×
4169
  nodesDestroyNode(pWhere);
×
4170
  return NULL;
×
4171
}
4172

4173
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName, bool force) {
83✔
4174
  CHECK_PARSER_STATUS(pCxt);
83!
4175
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
83!
4176
  SDropTopicStmt* pStmt = NULL;
83✔
4177
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT, (SNode**)&pStmt);
83✔
4178
  CHECK_MAKE_NODE(pStmt);
83!
4179
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
83✔
4180
  pStmt->ignoreNotExists = ignoreNotExists;
83✔
4181
  pStmt->force = force;
83✔
4182
  return (SNode*)pStmt;
83✔
4183
_err:
×
4184
  return NULL;
×
4185
}
4186

4187
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId, SToken* pTopicName,
25✔
4188
                            bool force) {
4189
  CHECK_PARSER_STATUS(pCxt);
25!
4190
  CHECK_NAME(checkTopicName(pCxt, pTopicName));
25!
4191
  CHECK_NAME(checkCGroupName(pCxt, pCGroupId));
25!
4192
  SDropCGroupStmt* pStmt = NULL;
25✔
4193
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT, (SNode**)&pStmt);
25✔
4194
  CHECK_MAKE_NODE(pStmt);
25!
4195
  pStmt->ignoreNotExists = ignoreNotExists;
25✔
4196
  pStmt->force = force;
25✔
4197
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
25✔
4198
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
25✔
4199
  return (SNode*)pStmt;
25✔
4200
_err:
×
4201
  return NULL;
×
4202
}
4203

4204
SNode* createAlterClusterStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
×
4205
  CHECK_PARSER_STATUS(pCxt);
×
4206
  SAlterClusterStmt* pStmt = NULL;
×
4207
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_CLUSTER_STMT, (SNode**)&pStmt);
×
4208
  CHECK_MAKE_NODE(pStmt);
×
4209
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
×
4210
  if (NULL != pValue) {
×
4211
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
×
4212
  }
4213
  return (SNode*)pStmt;
×
4214
_err:
×
4215
  return NULL;
×
4216
}
4217

4218
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
37✔
4219
  CHECK_PARSER_STATUS(pCxt);
37!
4220
  SAlterLocalStmt* pStmt = NULL;
37✔
4221
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT, (SNode**)&pStmt);
37✔
4222
  CHECK_MAKE_NODE(pStmt);
37!
4223
  (void)trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
37✔
4224
  if (NULL != pValue) {
37✔
4225
    (void)trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
20✔
4226
  }
4227
  return (SNode*)pStmt;
37✔
4228
_err:
×
4229
  return NULL;
×
4230
}
4231

4232
SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
1,300✔
4233
  CHECK_PARSER_STATUS(pCxt);
1,300!
4234
  SExplainOptions* pOptions = NULL;
1,300✔
4235
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS, (SNode**)&pOptions);
1,300✔
4236
  CHECK_MAKE_NODE(pOptions);
1,300!
4237
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
1,300✔
4238
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
1,300✔
4239
  return (SNode*)pOptions;
1,300✔
4240
_err:
×
4241
  return NULL;
×
4242
}
4243

4244
SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
68✔
4245
  CHECK_PARSER_STATUS(pCxt);
68!
4246
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
68✔
4247
  return pOptions;
68✔
4248
_err:
×
4249
  return NULL;
×
4250
}
4251

4252
SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
15✔
4253
  CHECK_PARSER_STATUS(pCxt);
15!
4254
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
15✔
4255
  return pOptions;
15✔
4256
_err:
×
4257
  return NULL;
×
4258
}
4259

4260
SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
1,300✔
4261
  CHECK_PARSER_STATUS(pCxt);
1,300!
4262
  SExplainStmt* pStmt = NULL;
1,300✔
4263
  pCxt->errCode = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT, (SNode**)&pStmt);
1,300✔
4264
  CHECK_MAKE_NODE(pStmt);
1,300!
4265
  pStmt->analyze = analyze;
1,300✔
4266
  pStmt->pOptions = (SExplainOptions*)pOptions;
1,300✔
4267
  pStmt->pQuery = pQuery;
1,300✔
4268
  return (SNode*)pStmt;
1,300✔
4269
_err:
×
4270
  nodesDestroyNode(pOptions);
×
4271
  nodesDestroyNode(pQuery);
×
4272
  return NULL;
×
4273
}
4274

4275
SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
1,038✔
4276
  CHECK_PARSER_STATUS(pCxt);
1,038!
4277
  SDescribeStmt* pStmt = NULL;
1,038✔
4278
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT, (SNode**)&pStmt);
1,038✔
4279
  CHECK_MAKE_NODE(pStmt);
1,038!
4280
  tstrncpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName, TSDB_DB_NAME_LEN);
1,038✔
4281
  tstrncpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName, TSDB_TABLE_NAME_LEN);
1,038✔
4282
  nodesDestroyNode(pRealTable);
1,038✔
4283
  return (SNode*)pStmt;
1,038✔
4284
_err:
×
4285
  nodesDestroyNode(pRealTable);
×
4286
  return NULL;
×
4287
}
4288

4289
SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
1,204✔
4290
  CHECK_PARSER_STATUS(pCxt);
1,204!
4291
  SNode* pStmt = NULL;
1,204✔
4292
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT, (SNode**)&pStmt);
1,204✔
4293
  CHECK_MAKE_NODE(pStmt);
1,204!
4294
  return pStmt;
1,204✔
4295
_err:
×
4296
  return NULL;
×
4297
}
4298

4299
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
24✔
4300
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
24!
4301
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
12✔
4302
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
12!
4303
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
12✔
4304
  } else {
4305
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4306
                                            "udf programming language supports c and python");
4307
  }
4308
  return pCxt->errCode;
24✔
4309
}
4310

4311
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
24✔
4312
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
4313
                                bool orReplace) {
4314
  CHECK_PARSER_STATUS(pCxt);
24!
4315
  if (pLibPath->n <= 2) {
24!
4316
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
4317
    CHECK_PARSER_STATUS(pCxt);
×
4318
  }
4319
  int8_t language = 0;
24✔
4320
  pCxt->errCode = convertUdfLanguageType(pCxt, pLanguage, &language);
24✔
4321
  CHECK_PARSER_STATUS(pCxt);
24!
4322
  SCreateFunctionStmt* pStmt = NULL;
24✔
4323
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT, (SNode**)&pStmt);
24✔
4324
  CHECK_MAKE_NODE(pStmt);
24!
4325
  pStmt->orReplace = orReplace;
24✔
4326
  pStmt->ignoreExists = ignoreExists;
24✔
4327
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
24✔
4328
  pStmt->isAgg = aggFunc;
24✔
4329
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
24!
4330
  pStmt->outputDt = dataType;
24✔
4331
  pStmt->bufSize = bufSize;
24✔
4332
  pStmt->language = language;
24✔
4333
  return (SNode*)pStmt;
24✔
4334
_err:
×
4335
  return NULL;
×
4336
}
4337

4338
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
×
4339
  CHECK_PARSER_STATUS(pCxt);
×
4340
  SDropFunctionStmt* pStmt = NULL;
×
4341
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT, (SNode**)&pStmt);
×
4342
  CHECK_MAKE_NODE(pStmt);
×
4343
  pStmt->ignoreNotExists = ignoreNotExists;
×
4344
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
×
4345
  return (SNode*)pStmt;
×
4346
_err:
×
4347
  return NULL;
×
4348
}
4349

4350
SNode* createCreateViewStmt(SAstCreateContext* pCxt, bool orReplace, SNode* pView, const SToken* pAs, SNode* pQuery) {
×
4351
  SCreateViewStmt* pStmt = NULL;
×
4352
  CHECK_PARSER_STATUS(pCxt);
×
4353
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_VIEW_STMT, (SNode**)&pStmt);
×
4354
  CHECK_MAKE_NODE(pStmt);
×
4355
  int32_t i = pAs->n;
×
4356
  while (isspace(*(pAs->z + i))) {
×
4357
    ++i;
×
4358
  }
4359
  pStmt->pQuerySql = tstrdup(pAs->z + i);
×
4360
  CHECK_OUT_OF_MEM(pStmt->pQuerySql);
×
4361
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
×
4362
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
×
4363
  nodesDestroyNode(pView);
×
4364
  pStmt->orReplace = orReplace;
×
4365
  pStmt->pQuery = pQuery;
×
4366
  return (SNode*)pStmt;
×
4367
_err:
×
4368
  nodesDestroyNode(pView);
×
4369
  nodesDestroyNode(pQuery);
×
4370
  nodesDestroyNode((SNode*)pStmt);
×
4371
  return NULL;
×
4372
}
4373

4374
SNode* createDropViewStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pView) {
×
4375
  CHECK_PARSER_STATUS(pCxt);
×
4376
  SDropViewStmt* pStmt = NULL;
×
4377
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_VIEW_STMT, (SNode**)&pStmt);
×
4378
  CHECK_MAKE_NODE(pStmt);
×
4379
  pStmt->ignoreNotExists = ignoreNotExists;
×
4380
  tstrncpy(pStmt->dbName, ((SViewNode*)pView)->table.dbName, TSDB_DB_NAME_LEN);
×
4381
  tstrncpy(pStmt->viewName, ((SViewNode*)pView)->table.tableName, TSDB_VIEW_NAME_LEN);
×
4382
  nodesDestroyNode(pView);
×
4383
  return (SNode*)pStmt;
×
4384
_err:
×
4385
  nodesDestroyNode(pView);
×
4386
  return NULL;
×
4387
}
4388

4389
SNode* createStreamOutTableNode(SAstCreateContext* pCxt, SNode* pIntoTable, SNode* pOutputSubTable, SNodeList* pColList,
1,296✔
4390
                                SNodeList* pTagList) {
4391
  SStreamOutTableNode* pOutTable = NULL;
1,296✔
4392
  CHECK_PARSER_STATUS(pCxt);
1,296!
4393
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_OUT_TABLE, (SNode**)&pOutTable);
1,296✔
4394
  CHECK_MAKE_NODE(pOutTable);
1,296!
4395
  pOutTable->pOutTable = pIntoTable;
1,296✔
4396
  pOutTable->pSubtable = pOutputSubTable;
1,296✔
4397
  pOutTable->pCols = pColList;
1,296✔
4398
  pOutTable->pTags = pTagList;
1,296✔
4399
  return (SNode*)pOutTable;
1,296✔
4400

4401
_err:
×
4402
  nodesDestroyNode((SNode*)pOutTable);
×
4403
  nodesDestroyNode(pIntoTable);
×
4404
  nodesDestroyNode(pOutputSubTable);
×
4405
  nodesDestroyList(pColList);
×
4406
  nodesDestroyList(pTagList);
×
4407
  return NULL;
×
4408
}
4409

4410
SNode* createStreamTriggerNode(SAstCreateContext* pCxt, SNode* pTriggerWindow, SNode* pTriggerTable,
1,314✔
4411
                               SNodeList* pPartitionList, SNode* pOptions, SNode* pNotification) {
4412
  SStreamTriggerNode* pTrigger = NULL;
1,314✔
4413
  CHECK_PARSER_STATUS(pCxt);
1,314!
4414
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER, (SNode**)&pTrigger);
1,314✔
4415
  CHECK_MAKE_NODE(pTrigger);
1,314!
4416

4417
  pTrigger->pOptions = pOptions;
1,314✔
4418
  pTrigger->pNotify = pNotification;
1,314✔
4419
  pTrigger->pTrigerTable = pTriggerTable;
1,314✔
4420
  pTrigger->pPartitionList = pPartitionList;
1,314✔
4421
  pTrigger->pTriggerWindow = pTriggerWindow;
1,314✔
4422
  return (SNode*)pTrigger;
1,314✔
4423

4424
_err:
×
4425
  nodesDestroyNode((SNode*)pTrigger);
×
4426
  nodesDestroyNode(pTriggerWindow);
×
4427
  nodesDestroyNode(pTriggerTable);
×
4428
  nodesDestroyNode(pOptions);
×
4429
  nodesDestroyNode(pNotification);
×
4430
  nodesDestroyList(pPartitionList);
×
4431
  return NULL;
×
4432
}
4433

4434
SNode* createSlidingWindowNode(SAstCreateContext* pCxt, SNode* pSlidingVal, SNode* pOffset) {
966✔
4435
  SSlidingWindowNode* pSliding = NULL;
966✔
4436
  CHECK_PARSER_STATUS(pCxt);
966!
4437
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SLIDING_WINDOW, (SNode**)&pSliding);
966✔
4438
  CHECK_MAKE_NODE(pSliding);
966!
4439
  pSliding->pSlidingVal = pSlidingVal;
966✔
4440
  pSliding->pOffset = pOffset;
966✔
4441
  return (SNode*)pSliding;
966✔
4442
_err:
×
4443
  nodesDestroyNode(pSlidingVal);
×
4444
  nodesDestroyNode(pOffset);
×
4445
  nodesDestroyNode((SNode*)pSliding);
×
4446
  return NULL;
×
4447
}
4448

4449
SNode* createStreamTriggerOptions(SAstCreateContext* pCxt) {
282✔
4450
  SStreamTriggerOptions* pOptions = NULL;
282✔
4451
  CHECK_PARSER_STATUS(pCxt);
282!
4452
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TRIGGER_OPTIONS, (SNode**)&pOptions);
282✔
4453
  CHECK_MAKE_NODE(pOptions);
282!
4454
  pOptions->pPreFilter = NULL;
282✔
4455
  pOptions->pWaterMark = NULL;
282✔
4456
  pOptions->pMaxDelay = NULL;
282✔
4457
  pOptions->pExpiredTime = NULL;
282✔
4458
  pOptions->pFillHisStartTime = NULL;
282✔
4459
  pOptions->pEventType = EVENT_NONE;
282✔
4460
  pOptions->calcNotifyOnly = false;
282✔
4461
  pOptions->deleteOutputTable = false;
282✔
4462
  pOptions->deleteRecalc = false;
282✔
4463
  pOptions->fillHistory = false;
282✔
4464
  pOptions->fillHistoryFirst = false;
282✔
4465
  pOptions->lowLatencyCalc = false;
282✔
4466
  pOptions->forceOutput = false;
282✔
4467
  pOptions->ignoreDisorder = false;
282✔
4468
  pOptions->ignoreNoDataTrigger = false;
282✔
4469
  return (SNode*)pOptions;
282✔
4470
_err:
×
4471
  nodesDestroyNode((SNode*)pOptions);
×
4472
  return NULL;
×
4473
}
4474

4475
SNode* createStreamTagDefNode(SAstCreateContext* pCxt, SToken* pTagName, SDataType dataType, SNode* tagExpression) {
84✔
4476
  SStreamTagDefNode* pTagDef = NULL;
84✔
4477
  CHECK_PARSER_STATUS(pCxt);
84!
4478
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_TAG_DEF, (SNode**)&pTagDef);
84✔
4479
  CHECK_MAKE_NODE(pTagDef);
84!
4480
  COPY_STRING_FORM_ID_TOKEN(pTagDef->tagName, pTagName);
84✔
4481
  pTagDef->dataType = dataType;
84✔
4482
  pTagDef->pTagExpr = tagExpression;
84✔
4483
  return (SNode*)pTagDef;
84✔
4484
_err:
×
4485
  nodesDestroyNode(tagExpression);
×
4486
  nodesDestroyNode((SNode*)pTagDef);
×
4487
  return NULL;
×
4488
}
4489

4490
SNode* setStreamTriggerOptions(SAstCreateContext* pCxt, SNode* pOptions, SStreamTriggerOption* pOptionUnit) {
300✔
4491
  CHECK_PARSER_STATUS(pCxt);
300!
4492
  SStreamTriggerOptions* pStreamOptions = (SStreamTriggerOptions*)pOptions;
300✔
4493
  switch (pOptionUnit->type) {
300!
4494
    case STREAM_TRIGGER_OPTION_CALC_NOTIFY_ONLY:
12✔
4495
      if (pStreamOptions->calcNotifyOnly) {
12!
4496
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4497
                                                "CALC_NOTIFY_ONLY specified multiple times");
4498
        goto _err;
×
4499
      }
4500
      pStreamOptions->calcNotifyOnly = true;
12✔
4501
      break;
12✔
4502
    case STREAM_TRIGGER_OPTION_DELETE_OUTPUT_TABLE:
12✔
4503
      if (pStreamOptions->deleteOutputTable) {
12!
4504
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4505
                                                "DELETE_OUTPUT_TABLE specified multiple times");
4506
        goto _err;
×
4507
      }
4508
      pStreamOptions->deleteOutputTable = true;
12✔
4509
      break;
12✔
4510
    case STREAM_TRIGGER_OPTION_DELETE_RECALC:
12✔
4511
      if (pStreamOptions->deleteRecalc) {
12!
4512
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4513
                                                "DELETE_RECALC specified multiple times");
4514
        goto _err;
×
4515
      }
4516
      pStreamOptions->deleteRecalc = true;
12✔
4517
      break;
12✔
4518
    case STREAM_TRIGGER_OPTION_EXPIRED_TIME:
54✔
4519
      if (pStreamOptions->pExpiredTime != NULL) {
54!
4520
        pCxt->errCode =
×
4521
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EXPIRED_TIME specified multiple times");
×
4522
        goto _err;
×
4523
      }
4524
      pStreamOptions->pExpiredTime = pOptionUnit->pNode;
54✔
4525
      break;
54✔
4526
    case STREAM_TRIGGER_OPTION_FORCE_OUTPUT:
×
4527
      if (pStreamOptions->forceOutput) {
×
4528
        pCxt->errCode =
×
4529
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FORCE_OUTPUT specified multiple times");
×
4530
        goto _err;
×
4531
      }
4532
      pStreamOptions->forceOutput = true;
×
4533
      break;
×
4534
    case STREAM_TRIGGER_OPTION_FILL_HISTORY:
18✔
4535
      if (pStreamOptions->fillHistoryFirst) {
18!
4536
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4537
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
4538
        goto _err;
×
4539
      }
4540
      if (pStreamOptions->pFillHisStartTime != NULL) {
18!
4541
        pCxt->errCode =
×
4542
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "FILL_HISTORY specified multiple times");
×
4543
        goto _err;
×
4544
      }
4545
      pStreamOptions->fillHistory = true;
18✔
4546
      if (pOptionUnit->pNode == NULL) {
18!
4547
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
×
4548
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
×
4549
      } else {
4550
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
18✔
4551
      }
4552
      break;
18✔
4553
    case STREAM_TRIGGER_OPTION_FILL_HISTORY_FIRST:
18✔
4554
      if (pStreamOptions->fillHistory) {
18✔
4555
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
6✔
4556
                                                "FILL_HISTORY_FIRST and FILL_HISTORY cannot be used at the same time");
4557
        goto _err;
6✔
4558
      }
4559
      if (pStreamOptions->pFillHisStartTime != NULL) {
12!
4560
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4561
                                                "FILL_HISTORY_FIRST specified multiple times");
4562
        goto _err;
×
4563
      }
4564
      pStreamOptions->fillHistoryFirst = true;
12✔
4565
      if (pOptionUnit->pNode == NULL) {
12!
4566
        pCxt->errCode = nodesMakeValueNodeFromInt64(INT64_MIN, &pStreamOptions->pFillHisStartTime);
×
4567
        CHECK_MAKE_NODE(pStreamOptions->pFillHisStartTime);
×
4568
      } else {
4569
        pStreamOptions->pFillHisStartTime = pOptionUnit->pNode;
12✔
4570
      }
4571
      break;
12✔
4572
    case STREAM_TRIGGER_OPTION_IGNORE_DISORDER:
24✔
4573
      if (pStreamOptions->ignoreDisorder) {
24✔
4574
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
6✔
4575
                                                "IGNORE_DISORDER specified multiple times");
4576
        goto _err;
6✔
4577
      }
4578
      pStreamOptions->ignoreDisorder = true;
18✔
4579
      break;
18✔
4580
    case STREAM_TRIGGER_OPTION_LOW_LATENCY_CALC:
12✔
4581
      if (pStreamOptions->lowLatencyCalc) {
12!
4582
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4583
                                                "LOW_LATENCY_CALC specified multiple times");
4584
        goto _err;
×
4585
      }
4586
      pStreamOptions->lowLatencyCalc = true;
12✔
4587
      break;
12✔
4588
    case STREAM_TRIGGER_OPTION_MAX_DELAY:
54✔
4589
      if (pStreamOptions->pMaxDelay != NULL) {
54!
4590
        pCxt->errCode =
×
4591
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "MAX_DELAY specified multiple times");
×
4592
        goto _err;
×
4593
      }
4594
      pStreamOptions->pMaxDelay = pOptionUnit->pNode;
54✔
4595
      break;
54✔
4596
    case STREAM_TRIGGER_OPTION_WATERMARK:
24✔
4597
      if (pStreamOptions->pWaterMark != NULL) {
24!
4598
        pCxt->errCode =
×
4599
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "WATERMARK specified multiple times");
×
4600
        goto _err;
×
4601
      }
4602
      pStreamOptions->pWaterMark = pOptionUnit->pNode;
24✔
4603
      break;
24✔
4604
    case STREAM_TRIGGER_OPTION_PRE_FILTER:
24✔
4605
      if (pStreamOptions->pPreFilter != NULL) {
24✔
4606
        pCxt->errCode =
6✔
4607
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "PRE_FILTER specified multiple times");
6✔
4608
        goto _err;
6✔
4609
      }
4610
      pStreamOptions->pPreFilter = pOptionUnit->pNode;
18✔
4611
      break;
18✔
4612
    case STREAM_TRIGGER_OPTION_EVENT_TYPE:
36✔
4613
      if (pStreamOptions->pEventType != EVENT_NONE) {
36!
4614
        pCxt->errCode =
×
4615
            generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "EVENT_TYPE specified multiple times");
×
4616
        goto _err;
×
4617
      }
4618
      pStreamOptions->pEventType = pOptionUnit->flag;
36✔
4619
      break;
36✔
4620
    case STREAM_TRIGGER_OPTION_IGNORE_NODATA_TRIGGER:
×
4621
      if (pStreamOptions->ignoreNoDataTrigger) {
×
4622
        pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4623
                                                "IGNORE_NODATA_TRIGGER specified multiple times");
4624
        goto _err;
×
4625
      }
4626
      pStreamOptions->ignoreNoDataTrigger = true;
×
4627
      break;
×
4628
    default:
×
4629
      break;
×
4630
  }
4631
  return pOptions;
282✔
4632
_err:
18✔
4633
  nodesDestroyNode(pOptionUnit->pNode);
18✔
4634
  nodesDestroyNode(pOptions);
18✔
4635
  return NULL;
18✔
4636
}
4637

4638
static bool validateNotifyUrl(const char* url) {
90✔
4639
  const char* prefix[] = {"ws://", "wss://"};
90✔
4640
  const char* host = NULL;
90✔
4641

4642
  if (!url || *url == '\0') return false;
90!
4643

4644
  for (int32_t i = 0; i < ARRAY_SIZE(prefix); ++i) {
90!
4645
    if (taosStrncasecmp(url, prefix[i], strlen(prefix[i])) == 0) {
90!
4646
      host = url + strlen(prefix[i]);
90✔
4647
      break;
90✔
4648
    }
4649
  }
4650

4651
  return (host != NULL) && (*host != '\0') && (*host != '/');
90!
4652
}
4653

4654
SNode* createStreamNotifyOptions(SAstCreateContext* pCxt, SNodeList* pAddrUrls, int64_t eventType, SNode* pWhere,
60✔
4655
                                 int64_t notifyType) {
4656
  SNode* pNode = NULL;
60✔
4657
  CHECK_PARSER_STATUS(pCxt);
60!
4658

4659
  if (LIST_LENGTH(pAddrUrls) == 0) {
60!
4660
    pCxt->errCode =
×
4661
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "notification address cannot be empty");
×
4662
    goto _err;
×
4663
  }
4664

4665
  FOREACH(pNode, pAddrUrls) {
150!
4666
    char* url = ((SValueNode*)pNode)->literal;
90✔
4667
    if (strlen(url) >= TSDB_STREAM_NOTIFY_URL_LEN) {
90!
4668
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4669
                                              "notification address \"%s\" exceed maximum length %d", url,
4670
                                              TSDB_STREAM_NOTIFY_URL_LEN);
4671
      goto _err;
×
4672
    }
4673
    if (!validateNotifyUrl(url)) {
90!
4674
      pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
×
4675
                                              "invalid notification address \"%s\"", url);
4676
      goto _err;
×
4677
    }
4678
  }
4679

4680
  SStreamNotifyOptions* pNotifyOptions = NULL;
60✔
4681
  pCxt->errCode = nodesMakeNode(QUERY_NODE_STREAM_NOTIFY_OPTIONS, (SNode**)&pNotifyOptions);
60✔
4682
  CHECK_MAKE_NODE(pNotifyOptions);
60!
4683
  pNotifyOptions->pAddrUrls = pAddrUrls;
60✔
4684
  pNotifyOptions->pWhere = pWhere;
60✔
4685
  pNotifyOptions->eventType = eventType;
60✔
4686
  pNotifyOptions->notifyType = notifyType;
60✔
4687
  return (SNode*)pNotifyOptions;
60✔
4688
_err:
×
4689
  nodesDestroyList(pAddrUrls);
×
4690
  return NULL;
×
4691
}
4692

4693
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pStream, SNode* pTrigger,
1,308✔
4694
                              SNode* pOutTable, SNode* pQuery) {
4695
  SCreateStreamStmt* pStmt = NULL;
1,308✔
4696
  CHECK_PARSER_STATUS(pCxt);
1,308!
4697
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT, (SNode**)&pStmt);
1,308✔
4698
  CHECK_MAKE_NODE(pStmt);
1,308!
4699

4700
  if (pOutTable && ((SStreamOutTableNode*)pOutTable)->pOutTable) {
1,308!
4701
    tstrncpy(pStmt->targetDbName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.dbName,
1,296✔
4702
             TSDB_DB_NAME_LEN);
4703
    tstrncpy(pStmt->targetTabName, ((SRealTableNode*)((SStreamOutTableNode*)pOutTable)->pOutTable)->table.tableName,
1,296✔
4704
             TSDB_TABLE_NAME_LEN);
4705
  }
4706

4707
  if (pStream) {
1,308!
4708
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
1,308✔
4709
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
1,308✔
4710
  } else {
4711
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
4712
    goto _err;
×
4713
  }
4714
  nodesDestroyNode(pStream);
1,308✔
4715

4716
  pStmt->ignoreExists = ignoreExists;
1,308✔
4717
  pStmt->pTrigger = pTrigger;
1,308✔
4718
  pStmt->pQuery = pQuery;
1,308✔
4719
  pStmt->pTags = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pTags : NULL;
1,308✔
4720
  pStmt->pSubtable = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pSubtable : NULL;
1,308✔
4721
  pStmt->pCols = pOutTable ? ((SStreamOutTableNode*)pOutTable)->pCols : NULL;
1,308✔
4722
  return (SNode*)pStmt;
1,308✔
4723
_err:
×
4724
  nodesDestroyNode(pOutTable);
×
4725
  nodesDestroyNode(pQuery);
×
4726
  nodesDestroyNode(pTrigger);
×
4727
  nodesDestroyNode(pQuery);
×
4728
  return NULL;
×
4729
}
4730

4731
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pStream) {
×
4732
  CHECK_PARSER_STATUS(pCxt);
×
4733
  SDropStreamStmt* pStmt = NULL;
×
4734
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT, (SNode**)&pStmt);
×
4735
  CHECK_MAKE_NODE(pStmt);
×
4736
  if (pStream) {
×
4737
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
×
4738
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
×
4739
  } else {
4740
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
4741
    goto _err;
×
4742
  }
4743
  nodesDestroyNode(pStream);
×
4744
  pStmt->ignoreNotExists = ignoreNotExists;
×
4745
  return (SNode*)pStmt;
×
4746
_err:
×
4747
  return NULL;
×
4748
}
4749

4750
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pStream) {
×
4751
  CHECK_PARSER_STATUS(pCxt);
×
4752
  SPauseStreamStmt* pStmt = NULL;
×
4753
  pCxt->errCode = nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT, (SNode**)&pStmt);
×
4754
  CHECK_MAKE_NODE(pStmt);
×
4755
  if (pStream) {
×
4756
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
×
4757
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
×
4758
  } else {
4759
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
4760
    goto _err;
×
4761
  }
4762
  nodesDestroyNode(pStream);
×
4763
  pStmt->ignoreNotExists = ignoreNotExists;
×
4764
  return (SNode*)pStmt;
×
4765
_err:
×
4766
  return NULL;
×
4767
}
4768

4769
SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated, SNode* pStream) {
×
4770
  CHECK_PARSER_STATUS(pCxt);
×
4771
  SResumeStreamStmt* pStmt = NULL;
×
4772
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT, (SNode**)&pStmt);
×
4773
  CHECK_MAKE_NODE(pStmt);
×
4774
  if (pStream) {
×
4775
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
×
4776
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
×
4777
  } else {
4778
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
4779
    goto _err;
×
4780
  }
4781
  nodesDestroyNode(pStream);
×
4782
  pStmt->ignoreNotExists = ignoreNotExists;
×
4783
  pStmt->ignoreUntreated = ignoreUntreated;
×
4784
  return (SNode*)pStmt;
×
4785
_err:
×
4786
  return NULL;
×
4787
}
4788

4789
SNode* createRecalcStreamStmt(SAstCreateContext* pCxt, SNode* pStream, SNode* pRange) {
×
4790
  CHECK_PARSER_STATUS(pCxt);
×
4791
  SRecalcStreamStmt* pStmt = NULL;
×
4792
  pCxt->errCode = nodesMakeNode(QUERY_NODE_RECALCULATE_STREAM_STMT, (SNode**)&pStmt);
×
4793
  CHECK_MAKE_NODE(pStmt);
×
4794
  if (pStream) {
×
4795
    tstrncpy(pStmt->streamDbName, ((SStreamNode*)pStream)->dbName, TSDB_DB_NAME_LEN);
×
4796
    tstrncpy(pStmt->streamName, ((SStreamNode*)pStream)->streamName, TSDB_STREAM_NAME_LEN);
×
4797
  } else {
4798
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream name cannot be empty");
×
4799
    goto _err;
×
4800
  }
4801
  pStmt->pRange = pRange;
×
4802
  return (SNode*)pStmt;
×
4803
_err:
×
4804
  return NULL;
×
4805
}
4806

4807
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
×
4808
  CHECK_PARSER_STATUS(pCxt);
×
4809
  SKillStmt* pStmt = NULL;
×
4810
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
4811
  CHECK_MAKE_NODE(pStmt);
×
4812
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
×
4813
  return (SNode*)pStmt;
×
4814
_err:
×
4815
  return NULL;
×
4816
}
4817

4818
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
×
4819
  CHECK_PARSER_STATUS(pCxt);
×
4820
  SKillQueryStmt* pStmt = NULL;
×
4821
  pCxt->errCode = nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT, (SNode**)&pStmt);
×
4822
  CHECK_MAKE_NODE(pStmt);
×
4823
  (void)trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
×
4824
  return (SNode*)pStmt;
×
4825
_err:
×
4826
  return NULL;
×
4827
}
4828

4829
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
12✔
4830
  CHECK_PARSER_STATUS(pCxt);
12!
4831
  SBalanceVgroupStmt* pStmt = NULL;
12✔
4832
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT, (SNode**)&pStmt);
12✔
4833
  CHECK_MAKE_NODE(pStmt);
12!
4834
  return (SNode*)pStmt;
12✔
4835
_err:
×
4836
  return NULL;
×
4837
}
4838

4839
SNode* createAssignLeaderStmt(SAstCreateContext* pCxt) {
×
4840
  CHECK_PARSER_STATUS(pCxt);
×
4841
  SAssignLeaderStmt* pStmt = NULL;
×
4842
  pCxt->errCode = nodesMakeNode(QUERY_NODE_ASSIGN_LEADER_STMT, (SNode**)&pStmt);
×
4843
  CHECK_MAKE_NODE(pStmt);
×
4844
  return (SNode*)pStmt;
×
4845
_err:
×
4846
  return NULL;
×
4847
}
4848

4849
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
12✔
4850
  CHECK_PARSER_STATUS(pCxt);
12!
4851
  SBalanceVgroupLeaderStmt* pStmt = NULL;
12✔
4852
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT, (SNode**)&pStmt);
12✔
4853
  CHECK_MAKE_NODE(pStmt);
12!
4854
  if (NULL != pVgId && NULL != pVgId->z) {
12!
4855
    pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
×
4856
  }
4857
  return (SNode*)pStmt;
12✔
4858
_err:
×
4859
  return NULL;
×
4860
}
4861

4862
SNode* createBalanceVgroupLeaderDBNameStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
4863
  CHECK_PARSER_STATUS(pCxt);
×
4864
  SBalanceVgroupLeaderStmt* pStmt = NULL;
×
4865
  pCxt->errCode = nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_DATABASE_STMT, (SNode**)&pStmt);
×
4866
  CHECK_MAKE_NODE(pStmt);
×
4867
  if (NULL != pDbName) {
×
4868
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
×
4869
  }
4870
  return (SNode*)pStmt;
×
4871
_err:
×
4872
  return NULL;
×
4873
}
4874

4875
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
12✔
4876
  CHECK_PARSER_STATUS(pCxt);
12!
4877
  SMergeVgroupStmt* pStmt = NULL;
12✔
4878
  pCxt->errCode = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT, (SNode**)&pStmt);
12✔
4879
  CHECK_MAKE_NODE(pStmt);
12!
4880
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
12✔
4881
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
12✔
4882
  return (SNode*)pStmt;
12✔
4883
_err:
×
4884
  return NULL;
×
4885
}
4886

4887
SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
24✔
4888
  CHECK_PARSER_STATUS(pCxt);
24!
4889
  SRedistributeVgroupStmt* pStmt = NULL;
24✔
4890
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT, (SNode**)&pStmt);
24✔
4891
  CHECK_MAKE_NODE(pStmt);
24!
4892
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
24✔
4893
  pStmt->pDnodes = pDnodes;
24✔
4894
  return (SNode*)pStmt;
24✔
4895
_err:
×
4896
  nodesDestroyList(pDnodes);
×
4897
  return NULL;
×
4898
}
4899

4900
SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, bool force) {
12✔
4901
  CHECK_PARSER_STATUS(pCxt);
12!
4902
  SSplitVgroupStmt* pStmt = NULL;
12✔
4903
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT, (SNode**)&pStmt);
12✔
4904
  CHECK_MAKE_NODE(pStmt);
12!
4905
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
12✔
4906
  pStmt->force = force;
12✔
4907
  return (SNode*)pStmt;
12✔
4908
_err:
×
4909
  return NULL;
×
4910
}
4911

4912
SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
×
4913
  CHECK_PARSER_STATUS(pCxt);
×
4914
  SNode* pStmt = NULL;
×
4915
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SYNCDB_STMT, (SNode**)&pStmt);
×
4916
  CHECK_MAKE_NODE(pStmt);
×
4917
  return pStmt;
×
4918
_err:
×
4919
  return NULL;
×
4920
}
4921

4922
SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
108✔
4923
                       SNode* pTagCond) {
4924
  CHECK_PARSER_STATUS(pCxt);
108!
4925
  CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
108!
4926
  CHECK_NAME(checkUserName(pCxt, pUserName));
108!
4927
  CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
108!
4928
  SGrantStmt* pStmt = NULL;
108✔
4929
  pCxt->errCode = nodesMakeNode(QUERY_NODE_GRANT_STMT, (SNode**)&pStmt);
108✔
4930
  CHECK_MAKE_NODE(pStmt);
108!
4931
  pStmt->privileges = privileges;
108✔
4932
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
108✔
4933
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
108✔
4934
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
24✔
4935
  }
4936
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
108✔
4937
  pStmt->pTagCond = pTagCond;
108✔
4938
  return (SNode*)pStmt;
108✔
4939
_err:
×
4940
  nodesDestroyNode(pTagCond);
×
4941
  return NULL;
×
4942
}
4943

4944
SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
72✔
4945
                        SNode* pTagCond) {
4946
  CHECK_PARSER_STATUS(pCxt);
72!
4947
  CHECK_NAME(checkDbName(pCxt, &pPrivLevel->first, false));
72!
4948
  CHECK_NAME(checkUserName(pCxt, pUserName));
72!
4949
  CHECK_NAME(checkTableName(pCxt, &pPrivLevel->second));
72!
4950
  SRevokeStmt* pStmt = NULL;
72✔
4951
  pCxt->errCode = nodesMakeNode(QUERY_NODE_REVOKE_STMT, (SNode**)&pStmt);
72✔
4952
  CHECK_MAKE_NODE(pStmt);
72!
4953
  pStmt->privileges = privileges;
72✔
4954
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
72✔
4955
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
72!
4956
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
×
4957
  }
4958
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
72✔
4959
  pStmt->pTagCond = pTagCond;
72✔
4960
  return (SNode*)pStmt;
72✔
4961
_err:
×
4962
  nodesDestroyNode(pTagCond);
×
4963
  return NULL;
×
4964
}
4965

4966
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
3,246✔
4967
  SFunctionNode* pFunc = NULL;
3,246✔
4968
  CHECK_PARSER_STATUS(pCxt);
3,246!
4969
  pCxt->errCode = nodesMakeNode(QUERY_NODE_FUNCTION, (SNode**)&pFunc);
3,246✔
4970
  CHECK_MAKE_NODE(pFunc);
3,246!
4971
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
3,246✔
4972
  SNode* pCol = createPrimaryKeyCol(pCxt, NULL);
3,246✔
4973
  CHECK_MAKE_NODE(pCol);
3,246!
4974
  pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pCol);
3,246✔
4975
  CHECK_PARSER_STATUS(pCxt);
3,246!
4976
  return (SNode*)pFunc;
3,246✔
4977
_err:
×
4978
  nodesDestroyNode((SNode*)pFunc);
×
4979
  return NULL;
×
4980
}
4981

4982
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
1,082✔
4983
  SDeleteStmt* pStmt = NULL;
1,082✔
4984
  CHECK_PARSER_STATUS(pCxt);
1,082!
4985
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DELETE_STMT, (SNode**)&pStmt);
1,082✔
4986
  CHECK_MAKE_NODE(pStmt);
1,082!
4987
  pStmt->pFromTable = pTable;
1,082✔
4988
  pStmt->pWhere = pWhere;
1,082✔
4989
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
1,082✔
4990
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
1,082✔
4991
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
1,082✔
4992
  CHECK_MAKE_NODE(pStmt->pCountFunc);
1,082!
4993
  CHECK_MAKE_NODE(pStmt->pFirstFunc);
1,082!
4994
  CHECK_MAKE_NODE(pStmt->pLastFunc);
1,082!
4995
  return (SNode*)pStmt;
1,082✔
4996
_err:
×
4997
  nodesDestroyNode((SNode*)pStmt);
×
4998
  nodesDestroyNode(pTable);
×
4999
  nodesDestroyNode(pWhere);
×
5000
  return NULL;
×
5001
}
5002

5003
SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
31✔
5004
  CHECK_PARSER_STATUS(pCxt);
31!
5005
  SInsertStmt* pStmt = NULL;
31✔
5006
  pCxt->errCode = nodesMakeNode(QUERY_NODE_INSERT_STMT, (SNode**)&pStmt);
31✔
5007
  CHECK_MAKE_NODE(pStmt);
31!
5008
  pStmt->pTable = pTable;
31✔
5009
  pStmt->pCols = pCols;
31✔
5010
  pStmt->pQuery = pQuery;
31✔
5011
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
31✔
5012
    tstrncpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
28✔
5013
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
3!
5014
    tstrncpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias, TSDB_TABLE_NAME_LEN);
3✔
5015
  }
5016
  return (SNode*)pStmt;
31✔
5017
_err:
×
5018
  nodesDestroyNode(pTable);
×
5019
  nodesDestroyNode(pQuery);
×
5020
  nodesDestroyList(pCols);
×
5021
  return NULL;
×
5022
}
5023

5024
SNode* createCreateTSMAStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* tsmaName, SNode* pOptions,
×
5025
                            SNode* pRealTable, SNode* pInterval) {
5026
  SCreateTSMAStmt* pStmt = NULL;
×
5027
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "TSMA not support yet");
×
5028
  goto _err;
×
5029

5030
  CHECK_PARSER_STATUS(pCxt);
5031
  CHECK_NAME(checkTsmaName(pCxt, tsmaName));
5032
  pCxt->errCode = nodesMakeNode(QUERY_NODE_CREATE_TSMA_STMT, (SNode**)&pStmt);
5033
  CHECK_MAKE_NODE(pStmt);
5034

5035
  pStmt->ignoreExists = ignoreExists;
5036
  if (!pOptions) {
5037
    // recursive tsma
5038
    pStmt->pOptions = NULL;
5039
    pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pStmt->pOptions);
5040
    CHECK_MAKE_NODE(pStmt->pOptions);
5041
    pStmt->pOptions->recursiveTsma = true;
5042
  } else {
5043
    pStmt->pOptions = (STSMAOptions*)pOptions;
5044
  }
5045
  pStmt->pOptions->pInterval = pInterval;
5046
  COPY_STRING_FORM_ID_TOKEN(pStmt->tsmaName, tsmaName);
5047

5048
  SRealTableNode* pTable = (SRealTableNode*)pRealTable;
5049
  memcpy(pStmt->dbName, pTable->table.dbName, TSDB_DB_NAME_LEN);
5050
  memcpy(pStmt->tableName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
5051
  memcpy(pStmt->originalTbName, pTable->table.tableName, TSDB_TABLE_NAME_LEN);
5052
  nodesDestroyNode(pRealTable);
5053

5054
  return (SNode*)pStmt;
5055
_err:
×
5056
  nodesDestroyNode((SNode*)pStmt);
×
5057
  nodesDestroyNode(pOptions);
×
5058
  nodesDestroyNode(pRealTable);
×
5059
  nodesDestroyNode(pInterval);
×
5060
  return NULL;
×
5061
}
5062

5063
SNode* createTSMAOptions(SAstCreateContext* pCxt, SNodeList* pFuncs) {
×
5064
  CHECK_PARSER_STATUS(pCxt);
×
5065
  STSMAOptions* pOptions = NULL;
×
5066
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
5067
  CHECK_MAKE_NODE(pOptions);
×
5068
  pOptions->pFuncs = pFuncs;
×
5069
  return (SNode*)pOptions;
×
5070
_err:
×
5071
  nodesDestroyList(pFuncs);
×
5072
  return NULL;
×
5073
}
5074

5075
SNode* createDefaultTSMAOptions(SAstCreateContext* pCxt) {
×
5076
  CHECK_PARSER_STATUS(pCxt);
×
5077
  STSMAOptions* pOptions = NULL;
×
5078
  pCxt->errCode = nodesMakeNode(QUERY_NODE_TSMA_OPTIONS, (SNode**)&pOptions);
×
5079
  CHECK_MAKE_NODE(pOptions);
×
5080
  return (SNode*)pOptions;
×
5081
_err:
×
5082
  return NULL;
×
5083
}
5084

5085
SNode* createDropTSMAStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
×
5086
  pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "TSMA not support yet");
×
5087
  goto _err;
×
5088
  CHECK_PARSER_STATUS(pCxt);
5089
  SDropTSMAStmt* pStmt = NULL;
5090
  pCxt->errCode = nodesMakeNode(QUERY_NODE_DROP_TSMA_STMT, (SNode**)&pStmt);
5091
  CHECK_MAKE_NODE(pStmt);
5092

5093
  pStmt->ignoreNotExists = ignoreNotExists;
5094
  SRealTableNode* pTableNode = (SRealTableNode*)pRealTable;
5095

5096
  memcpy(pStmt->tsmaName, pTableNode->table.tableName, TSDB_TABLE_NAME_LEN);
5097
  memcpy(pStmt->dbName, pTableNode->table.dbName, TSDB_DB_NAME_LEN);
5098

5099
  nodesDestroyNode(pRealTable);
5100
  return (SNode*)pStmt;
5101
_err:
×
5102
  nodesDestroyNode(pRealTable);
×
5103
  return NULL;
×
5104
}
5105

5106
SNode* createShowTSMASStmt(SAstCreateContext* pCxt, SNode* dbName) {
×
5107
  CHECK_PARSER_STATUS(pCxt);
×
5108

5109
  SShowStmt* pStmt = NULL;
×
5110
  pCxt->errCode = nodesMakeNode(QUERY_NODE_SHOW_TSMAS_STMT, (SNode**)&pStmt);
×
5111
  CHECK_MAKE_NODE(pStmt);
×
5112

5113
  pStmt->pDbName = dbName;
×
5114
  return (SNode*)pStmt;
×
5115
_err:
×
5116
  nodesDestroyNode(dbName);
×
5117
  return NULL;
×
5118
}
5119
SNode* createShowDiskUsageStmt(SAstCreateContext* pCxt, SNode* dbName, ENodeType type) {
×
5120
  CHECK_PARSER_STATUS(pCxt);
×
5121
  if (NULL == dbName) {
×
5122
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
5123
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5124
    CHECK_PARSER_STATUS(pCxt);
×
5125
  }
5126

5127
  SShowStmt* pStmt = NULL;
×
5128
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
×
5129
  CHECK_MAKE_NODE(pStmt);
×
5130

5131
  pStmt->pDbName = dbName;
×
5132
  return (SNode*)pStmt;
×
5133
_err:
×
5134
  nodesDestroyNode(dbName);
×
5135
  return NULL;
×
5136
}
5137

5138
SNode* createShowStreamsStmt(SAstCreateContext* pCxt, SNode* pDbName, ENodeType type) {
12✔
5139
  CHECK_PARSER_STATUS(pCxt);
12!
5140

5141
  if (needDbShowStmt(type) && NULL == pDbName) {
12!
5142
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
×
5143
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
×
5144
    CHECK_PARSER_STATUS(pCxt);
×
5145
  }
5146

5147
  SShowStmt* pStmt = NULL;
12✔
5148
  pCxt->errCode = nodesMakeNode(type, (SNode**)&pStmt);
12✔
5149
  CHECK_MAKE_NODE(pStmt);
12!
5150
  pStmt->withFull = false;
12✔
5151
  pStmt->pDbName = pDbName;
12✔
5152

5153
  return (SNode*)pStmt;
12✔
5154

5155
_err:
×
5156
  nodesDestroyNode(pDbName);
×
5157
  return NULL;
×
5158
}
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

© 2025 Coveralls, Inc