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

taosdata / TDengine / #5071

17 May 2026 01:15AM UTC coverage: 63.054% (-10.3%) from 73.326%
#5071

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

238317 of 377957 relevant lines covered (63.05%)

130539817.12 hits per line

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

78.89
/source/libs/parser/src/parser.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

16
#include "parser.h"
17
#include "os.h"
18

19
#include <stdio.h>
20
#include <string.h>
21
#include "decimal.h"
22
#include "parInsertUtil.h"
23
#include "parInt.h"
24
#include "parToken.h"
25
#include "parUtil.h"
26
#include "tname.h"
27
#include "ttime.h"
28

29
bool qIsInsertValuesSql(const char* pStr, size_t length) {
1,906,904,366✔
30
  if (NULL == pStr) {
1,906,904,366✔
31
    return false;
×
32
  }
33

34
  const char* pSql = pStr;
1,906,904,366✔
35

36
  int32_t index = 0;
1,906,904,366✔
37
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
1,906,917,229✔
38
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
1,906,928,312✔
39
    return false;
1,004,561,262✔
40
  }
41

42
  do {
43
    pStr += index;
2,147,483,647✔
44
    index = 0;
2,147,483,647✔
45
    t = tStrGetToken((char*)pStr, &index, false, NULL);
2,147,483,647✔
46
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
2,147,483,647✔
47
      return true;
901,153,228✔
48
    } else if (TK_SELECT == t.type) {
2,147,483,647✔
49
      return false;
1,222,550✔
50
    }
51
    if (0 == t.type || 0 == t.n) {
2,147,483,647✔
52
      break;
53
    }
54
  } while (pStr - pSql < length);
2,147,483,647✔
55
  return false;
2,524✔
56
}
57

58
bool qIsUpdateSetSql(const char* pStr, size_t length, SName* pTableName, int32_t acctId, const char* dbName,
7,803,713✔
59
                     char* msgBuf, int32_t msgBufLen, int* pCode) {
60
                        if (NULL == pStr) {
7,803,713✔
61
    return false;
×
62
  }
63

64
  const char* pSql = pStr;
7,803,713✔
65

66
  int32_t index = 0;
7,803,713✔
67
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
7,805,653✔
68
    if (TK_UPDATE != t.type) {
7,810,685✔
69
    return false;
7,809,333✔
70
  }
71
  SMsgBuf pMsgBuf = {.len = msgBufLen, .buf = msgBuf};
1,352✔
72
  pStr += index;
1,352✔
73
  index = 0;
1,352✔
74
  t = tStrGetToken((char*)pStr, &index, false, NULL);
1,352✔
75
  if (t.n == 0 || t.z == NULL) {
1,352✔
76
    *pCode = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_TSC_STMT_TBNAME_ERROR, "Invalid table name");
104✔
77
    return false;
104✔
78
  }
79

80
  if (pTableName != NULL) {
1,248✔
81
    *pCode = insCreateSName(pTableName, &t, acctId, dbName, &pMsgBuf);
1,248✔
82
    if ((*pCode) != TSDB_CODE_SUCCESS) {
1,248✔
83
      return false;
×
84
    }
85
  }
86
    do {
87
    pStr += index;
4,472✔
88
    index = 0;
4,472✔
89
    t = tStrGetToken((char*)pStr, &index, false, NULL);
4,472✔
90
        if (TK_SET == t.type) {
4,472✔
91
      return true;
1,144✔
92
    }
93
    if (0 == t.type || 0 == t.n) {
3,328✔
94
      break;
95
    }
96
  } while (pStr - pSql < length);
3,224✔
97
  return false;
104✔
98
}
99

100
bool qIsSelectFromSql(const char* pStr, size_t length) {
29,591✔
101
  if (NULL == pStr) {
29,591✔
102
    return false;
101✔
103
  }
104

105
  const char* pSql = pStr;
29,490✔
106

107
  int32_t index = 0;
29,490✔
108
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
29,490✔
109
  if (TK_SELECT != t.type) {
29,490✔
110
    return false;
22,334✔
111
  }
112

113
  do {
114
    pStr += index;
79,397✔
115
    index = 0;
79,397✔
116
    t = tStrGetToken((char*)pStr, &index, false, NULL);
79,397✔
117
    if (TK_FROM == t.type) {
79,397✔
118
      return true;
7,156✔
119
    }
120
    if (0 == t.type || 0 == t.n) {
72,241✔
121
      break;
122
    }
123
  } while (pStr - pSql < length);
72,241✔
124

125
  return false;
×
126
}
127

128
static bool isColumnPrimaryKey(const STableMeta* pTableMeta, const char* colName, int32_t colNameLen, int32_t* colId) {
3,328✔
129
  if (pTableMeta == NULL || colName == NULL) {
3,328✔
130
    return false;
×
131
  }
132

133
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; i++) {
9,880✔
134
    const SSchema* pSchema = &pTableMeta->schema[i];
9,880✔
135
    if (strncmp(pSchema->name, colName, colNameLen) == 0 && strlen(pSchema->name) == colNameLen) {
9,880✔
136
      if (colId) {
3,328✔
137
        *colId = i;
1,040✔
138
      }
139
      if ((pSchema->flags & COL_IS_KEY || pSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
3,328✔
140
        return true;
832✔
141
      }
142
      return false;
2,496✔
143
    }
144
  }
145
  return false;
×
146
}
147

148
int32_t convertUpdateToInsert(const char* pSql, char** pNewSql, STableMeta* pTableMeta, SSHashObj* predicateCols,
1,040✔
149
                              char* msgBuf, int32_t msgBufLen) {
150
  if (NULL == pSql || NULL == pNewSql) {
1,040✔
151
    return TSDB_CODE_INVALID_PARA;
×
152
  }
153

154
  const char* pEnd = pSql + strlen(pSql);
1,040✔
155
  size_t      maxSqlLen = strlen(pSql) * 2;
1,040✔
156
  char*  newSql = taosMemoryMalloc(maxSqlLen);
1,040✔
157
  if (newSql == NULL) {
1,040✔
158
    return terrno;
×
159
  }
160
  char*   p = newSql;
1,040✔
161
  int32_t index = 0;
1,040✔
162
  SToken  t;
163
  int32_t code = TSDB_CODE_SUCCESS;
1,040✔
164
  SMsgBuf pMsgBuf = {msgBufLen, msgBuf};
1,040✔
165

166
  // UPDATE
167
  t = tStrGetToken((char*)pSql, &index, false, NULL);
1,040✔
168
  if (TK_UPDATE != t.type) {
1,040✔
169
    taosMemoryFree(newSql);
×
170
    code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected UPDATE keyword");
×
171
    return code;
×
172
  }
173
  pSql += index;
1,040✔
174

175
  // tbname
176
  index = 0;
1,040✔
177
  t = tStrGetToken((char*)pSql, &index, false, NULL);
1,040✔
178
  if (t.n == 0 || t.z == NULL) {
1,040✔
179
    taosMemoryFree(newSql);
×
180
    code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid table name");
×
181
    return code;
×
182
  }
183

184
  {
185
    size_t rem = maxSqlLen - (p - newSql);
1,040✔
186
    int written = snprintf(p, rem, "INSERT INTO %.*s (", (int)t.n, t.z);
1,040✔
187
    if (written < 0 || (size_t)written >= rem) {
1,040✔
188
      taosMemoryFree(newSql);
×
189
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "sql too long");
×
190
      return code;
×
191
    }
192
    p += written;
1,040✔
193
  }
194
  pSql += index;
1,040✔
195

196
  // SET
197
  index = 0;
1,040✔
198
  t = tStrGetToken((char*)pSql, &index, false, NULL);
1,040✔
199
  if (TK_SET != t.type) {
1,040✔
200
    taosMemoryFree(newSql);
104✔
201
    code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected SET keyword");
104✔
202
    return code;
104✔
203
  }
204
  pSql += index;
936✔
205

206
  bool    firstColumn = true;
936✔
207
  int32_t columnCount = 0;
936✔
208
  bool inSetClause = true;
936✔
209
  int32_t numOfCols = 0;
936✔
210

211
  // col name
212
  while (inSetClause && pSql < pEnd) {
2,912✔
213
    index = 0;
2,288✔
214
    t = tStrGetToken((char*)pSql, &index, false, NULL);
2,288✔
215
    if (t.n == 0 || t.z == NULL) {
2,288✔
216
      break;
217
    }
218

219
    // pk can't set
220
    if (pTableMeta != NULL && isColumnPrimaryKey(pTableMeta, t.z, t.n, NULL)) {
2,288✔
221
      taosMemoryFree(newSql);
208✔
222
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Cannot update primary key column '%.*s'",
208✔
223
                                     t.n, t.z);
224
      return code;
208✔
225
    }
226

227
    if (!firstColumn) {
2,080✔
228
      *p++ = ',';
1,248✔
229
    }
230
    numOfCols++;
2,080✔
231
    memcpy(p, t.z, t.n);
2,080✔
232
    p += t.n;
2,080✔
233
    firstColumn = false;
2,080✔
234
    columnCount++;
2,080✔
235
    pSql += index;
2,080✔
236

237
    index = 0;
2,080✔
238
    t = tStrGetToken((char*)pSql, &index, false, NULL);
2,080✔
239
    if (t.type != TK_NK_EQ) {
2,080✔
240
      taosMemoryFree(newSql);
×
241
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '=' after column name");
×
242
      return code;
×
243
    }
244
    pSql += index;
2,080✔
245

246
    // value must be ?
247
    index = 0;
2,080✔
248
    t = tStrGetToken((char*)pSql, &index, false, NULL);
2,080✔
249
    if (t.n == 0 || t.z == NULL) {
2,080✔
250
      break;
251
    }
252
    if (t.type != TK_NK_QUESTION) {
2,080✔
253
      taosMemoryFree(newSql);
104✔
254
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '?' placeholder");
104✔
255
      return code;
104✔
256
    }
257
    pSql += index;
1,976✔
258

259
    index = 0;
1,976✔
260
    t = tStrGetToken((char*)pSql, &index, false, NULL);
1,976✔
261
    if (t.type == TK_WHERE) {
1,976✔
262
      inSetClause = false;
520✔
263
      pSql += index;
520✔
264
    }
265
  }
266

267
  // where clause
268
  if (pSql < pEnd) {
624✔
269
    bool inWhereClause = true;
520✔
270
    int32_t bracketLevel = 0;
520✔
271

272
    while (inWhereClause && pSql < pEnd) {
1,976✔
273
      index = 0;
1,768✔
274
      t = tStrGetToken((char*)pSql, &index, false, NULL);
1,768✔
275
      if (t.n == 0 || t.z == NULL) {
1,768✔
276
        break;
277
      }
278

279
      if (t.type == TK_NK_LP) {
1,768✔
280
        bracketLevel++;
104✔
281
        pSql += index;
104✔
282
        continue;
312✔
283
      } else if (t.type == TK_NK_RP) {
1,664✔
284
        bracketLevel--;
104✔
285
        pSql += index;
104✔
286
        continue;
104✔
287
      } else if (t.type == TK_IN || t.type == TK_EXISTS) {
1,560✔
288
        while (pSql < pEnd) {
1,352✔
289
          pSql += index;
1,352✔
290
          index = 0;
1,352✔
291
          t = tStrGetToken((char*)pSql, &index, false, NULL);
1,352✔
292
          if (t.type == TK_AND || t.type == TK_OR || t.n == 0 || t.z == NULL) {
1,352✔
293
            break;
294
          }
295
        }
296
        continue;
104✔
297
      }
298

299
      const char* colName = t.z;
1,456✔
300
      int32_t     colNameLen = t.n;
1,456✔
301
      pSql += index;
1,456✔
302

303
      index = 0;
1,456✔
304
      t = tStrGetToken((char*)pSql, &index, false, NULL);
1,456✔
305
      if (t.n == 0 || t.z == NULL) {
1,456✔
306
        break;
307
      }
308
      pSql += index;
1,456✔
309

310
      index = 0;
1,456✔
311
      t = tStrGetToken((char*)pSql, &index, false, NULL);
1,456✔
312
      if (t.n == 0 || t.z == NULL) {
1,456✔
313
        break;
314
      }
315

316
      // where cols muset be pk, ignore others
317
      int32_t colId = -1;
1,456✔
318
      if (t.type == TK_NK_QUESTION) {
1,456✔
319
        if (pTableMeta != NULL && isColumnPrimaryKey(pTableMeta, colName, colNameLen, &colId)) {
1,040✔
320
          if (!firstColumn) {
624✔
321
            *p++ = ',';
624✔
322
          }
323
          memcpy(p, colName, colNameLen);
624✔
324
          p += colNameLen;
624✔
325
          firstColumn = false;
624✔
326
          columnCount++;
624✔
327
        } else {
328
          if (tSimpleHashPut(predicateCols, &numOfCols, sizeof(int32_t), &colId, sizeof(int32_t))) {
416✔
329
            taosMemoryFree(newSql);
×
330
            code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '?' placeholder");
×
331
            return code;
×
332
          }
333
        }
334
        numOfCols++;
1,040✔
335
      }
336
      pSql += index;
1,456✔
337

338
      index = 0;
1,456✔
339
      t = tStrGetToken((char*)pSql, &index, false, NULL);
1,456✔
340
      if (t.type == TK_AND || t.type == TK_OR) {
1,456✔
341
        pSql += index;
1,040✔
342
      } else {
343
        if (bracketLevel == 0) {
416✔
344
          break;
312✔
345
        }
346
        pSql += index;
104✔
347
      }
348
    }
349
  }
350

351
  p += snprintf(p, maxSqlLen - (p - newSql), ") VALUES (");
624✔
352
  for (int32_t i = 0; i < columnCount; i++) {
3,016✔
353
    if (i > 0) {
2,392✔
354
      *p++ = ',';
1,768✔
355
    }
356
    *p++ = '?';
2,392✔
357
  }
358
  *p++ = ')';
624✔
359
  *p = '\0';
624✔
360

361
  *pNewSql = newSql;
624✔
362
  return code;
624✔
363
}
364

365
bool qIsCreateTbFromFileSql(const char* pStr, size_t length) {
503,224,730✔
366
  if (NULL == pStr) {
503,224,730✔
367
    return false;
×
368
  }
369

370
  const char* pSql = pStr;
503,224,730✔
371

372
  int32_t index = 0;
503,224,730✔
373
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
503,226,757✔
374
  if (TK_CREATE != t.type) {
503,258,081✔
375
    return false;
452,697,661✔
376
  }
377

378
  do {
379
    pStr += index;
2,114,564,139✔
380
    index = 0;
2,114,579,133✔
381
    t = tStrGetToken((char*)pStr, &index, false, NULL);
2,114,579,133✔
382
    if (TK_FILE == t.type) {
2,114,564,716✔
383
      return true;
3,000✔
384
    }
385
    if (0 == t.type || 0 == t.n) {
2,114,561,716✔
386
      break;
387
    }
388
  } while (pStr - pSql < length);
2,064,022,769✔
389
  return false;
50,557,997✔
390
}
391

392
bool qParseDbName(const char* pStr, size_t length, char** pDbName) {
7,831,200✔
393
  (void)length;
394
  int32_t index = 0;
7,831,200✔
395
  SToken  t;
396

397
  if (NULL == pStr) {
7,832,756✔
398
    *pDbName = NULL;
×
399
    return false;
×
400
  }
401

402
  t = tStrGetToken((char*)pStr, &index, false, NULL);
7,832,756✔
403
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
7,838,152✔
404
    *pDbName = NULL;
17,303✔
405
    return false;
17,303✔
406
  }
407

408
  t = tStrGetToken((char*)pStr, &index, false, NULL);
7,820,849✔
409
  if (TK_INTO != t.type) {
7,822,021✔
410
    *pDbName = NULL;
×
411
    return false;
×
412
  }
413

414
  t = tStrGetToken((char*)pStr, &index, false, NULL);
7,822,021✔
415
  if (t.n == 0 || t.z == NULL) {
7,821,243✔
416
    *pDbName = NULL;
×
417
    return false;
×
418
  }
419
  char* dotPos = strnchr(t.z, '.', t.n, true);
7,821,243✔
420
  if (dotPos != NULL) {
7,820,455✔
421
    int dbNameLen = dotPos - t.z;
30,638✔
422
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
30,638✔
423
    if (*pDbName == NULL) {
24,464✔
424
      return false;
×
425
    }
426
    tstrncpy(*pDbName, t.z, dbNameLen + 1);
24,464✔
427
    return true;
24,464✔
428
  }
429
  return false;
7,789,817✔
430
}
431

432
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
489,959,143✔
433
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
489,959,143✔
434

435
  if (pCxt->parseOnly) {
489,953,384✔
436
    return code;
326,288✔
437
  }
438

439
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
489,628,360✔
440
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
20,567✔
441
    return TSDB_CODE_SUCCESS;
20,567✔
442
  }
443

444
  if (TSDB_CODE_SUCCESS == code) {
489,610,417✔
445
    code = translate(pCxt, pQuery, pMetaCache);
489,421,129✔
446
  }
447
  if (TSDB_CODE_SUCCESS == code) {
489,584,975✔
448
    code = calculateConstant(pCxt, pQuery);
402,078,793✔
449
  }
450
  return code;
489,584,236✔
451
}
452

453
static int32_t parseSqlIntoAst(SParseContext* pCxt, SQuery** pQuery) {
121,484✔
454
  int32_t code = parse(pCxt, pQuery);
121,484✔
455
  if (TSDB_CODE_SUCCESS == code) {
121,484✔
456
    code = analyseSemantic(pCxt, *pQuery, NULL);
117,255✔
457
  }
458
  return code;
121,484✔
459
}
460

461
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
503,221,759✔
462
  int32_t code = parse(pCxt, pQuery);
503,221,759✔
463
  if (TSDB_CODE_SUCCESS == code) {
503,073,714✔
464
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
489,727,441✔
465
  }
466
  return code;
503,183,448✔
467
}
468

469
static int32_t setValueByBindParam(SValueNode* pVal, TAOS_MULTI_BIND* pParam, void *charsetCxt) {
15,909✔
470
  if (!pParam || IS_NULL_TYPE(pParam->buffer_type)) {
15,909✔
471
    return TSDB_CODE_APP_ERROR;
×
472
  }
473
  if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) {
15,909✔
474
    taosMemoryFreeClear(pVal->datum.p);
×
475
  }
476

477
  if (pParam->is_null && 1 == *(pParam->is_null)) {
15,909✔
478
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
×
479
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
×
480
    return TSDB_CODE_SUCCESS;
×
481
  }
482

483
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
15,909✔
484
  pVal->node.resType.type = pParam->buffer_type;
15,909✔
485
  pVal->node.resType.bytes = inputSize;
15,909✔
486

487
  switch (pParam->buffer_type) {
15,909✔
488
    case TSDB_DATA_TYPE_VARBINARY:
×
489
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
490
      if (NULL == pVal->datum.p) {
×
491
        return terrno;
×
492
      }
493
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
494
      memcpy(varDataVal(pVal->datum.p), pParam->buffer, pVal->node.resType.bytes);
×
495
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
496
      break;
×
497
    case TSDB_DATA_TYPE_VARCHAR:
4,024✔
498
    case TSDB_DATA_TYPE_GEOMETRY:
499
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
4,024✔
500
      if (NULL == pVal->datum.p) {
4,024✔
501
        return terrno;
×
502
      }
503
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
4,024✔
504
      TAOS_STRNCPY(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
4,024✔
505
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
4,024✔
506
      break;
4,024✔
507
    case TSDB_DATA_TYPE_NCHAR: {
×
508
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
×
509
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
510
      if (NULL == pVal->datum.p) {
×
511
        return terrno;
×
512
      }
513

514
      int32_t output = 0;
×
515
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
516
                         &output, charsetCxt)) {
517
        return terrno;
×
518
      }
519
      varDataSetLen(pVal->datum.p, output);
×
520
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
521
      break;
×
522
    }
523
    default: {
11,885✔
524
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
11,885✔
525
      if (code) {
11,885✔
526
        return code;
×
527
      }
528
      break;
11,885✔
529
    }
530
  }
531
  pVal->translate = true;
15,909✔
532
  return TSDB_CODE_SUCCESS;
15,909✔
533
}
534

535
static EDealRes rewriteQueryExprAliasImpl(SNode* pNode, void* pContext) {
216,551✔
536
  if (nodesIsExprNode(pNode) && QUERY_NODE_COLUMN != nodeType(pNode)) {
216,551✔
537
    snprintf(((SExprNode*)pNode)->aliasName, TSDB_COL_NAME_LEN, "#%d", *(int32_t*)pContext);
111,437✔
538
    ++(*(int32_t*)pContext);
111,437✔
539
  }
540
  return DEAL_RES_CONTINUE;
216,551✔
541
}
542

543
static void rewriteQueryExprAlias(SNode* pRoot, int32_t* pNo) {
21,623✔
544
  switch (nodeType(pRoot)) {
21,623✔
545
    case QUERY_NODE_SELECT_STMT:
21,623✔
546
      nodesWalkSelectStmt((SSelectStmt*)pRoot, SQL_CLAUSE_FROM, rewriteQueryExprAliasImpl, pNo);
21,623✔
547
      break;
21,623✔
548
    case QUERY_NODE_SET_OPERATOR: {
×
549
      SSetOperator* pSetOper = (SSetOperator*)pRoot;
×
550
      rewriteQueryExprAlias(pSetOper->pLeft, pNo);
×
551
      rewriteQueryExprAlias(pSetOper->pRight, pNo);
×
552
      break;
×
553
    }
554
    default:
×
555
      break;
×
556
  }
557
}
21,623✔
558

559
static void rewriteExprAlias(SNode* pRoot) {
21,623✔
560
  int32_t no = 1;
21,623✔
561
  rewriteQueryExprAlias(pRoot, &no);
21,623✔
562
}
21,623✔
563

564
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
7,938,949✔
565
  int32_t code = TSDB_CODE_SUCCESS;
7,938,949✔
566
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
7,938,949✔
567
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
7,824,063✔
568
  } else {
569
    code = parseSqlIntoAst(pCxt, pQuery);
121,484✔
570
  }
571
  terrno = code;
7,934,321✔
572
  return code;
7,942,849✔
573
}
574

575
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
503,229,740✔
576
  SParseMetaCache metaCache = {0};
503,229,740✔
577
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
503,232,431✔
578
  if (TSDB_CODE_SUCCESS == code) {
503,153,178✔
579
    code = buildCatalogReq(&metaCache, pCatalogReq);
489,796,909✔
580
  }
581
  destoryParseMetaCache(&metaCache, true);
503,202,342✔
582
  return code;
503,206,763✔
583
}
584

585
static int32_t parseCreateTbFromFileSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
3,000✔
586
  if (NULL == *pQuery) return parseQuerySyntax(pCxt, pQuery, pCatalogReq);
3,000✔
587

588
  return continueCreateTbFromFile(pCxt, pQuery);
1,500✔
589
}
590

591
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
942,061,423✔
592
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
942,061,423✔
593
  if (TSDB_CODE_SUCCESS == code) {
942,080,808✔
594
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
942,084,394✔
595
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
438,846,832✔
596
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
503,254,407✔
597
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
3,000✔
598
    } else {
599
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
503,247,878✔
600
    }
601
  }
602
  (void)nodesReleaseAllocator(pCxt->allocatorId);
941,990,073✔
603
  terrno = code;
942,041,940✔
604
  return code;
941,979,467✔
605
}
606

607
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
489,858,248✔
608
                            struct SMetaData* pMetaData, SQuery* pQuery) {
609
  SParseMetaCache metaCache = {0};
489,858,248✔
610
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
489,858,248✔
611
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
489,867,336✔
612
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
489,869,395✔
613
  }
614
  if (TSDB_CODE_SUCCESS == code) {
489,857,620✔
615
    code = analyseSemantic(pCxt, pQuery, &metaCache);
489,863,335✔
616
  }
617
  (void)nodesReleaseAllocator(pCxt->allocatorId);
489,767,732✔
618
  destoryParseMetaCache(&metaCache, false);
489,835,781✔
619
  terrno = code;
489,828,652✔
620
  return code;
489,787,483✔
621
}
622

623
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
3,694,562✔
624
                          SQuery* pQuery) {
625
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
3,694,562✔
626
}
627

628
int32_t qContinueParsePostQuery(SParseContext* pCxt, SQuery* pQuery, SSDataBlock* pBlock) {
×
629
  int32_t code = TSDB_CODE_SUCCESS;
×
630
  switch (nodeType(pQuery->pRoot)) {
×
631
    default:
632
      break;
×
633
  }
634

635
  return code;
×
636
}
637

638
static void destoryTablesReq(void* p) {
1,486,751,402✔
639
  STablesReq* pRes = (STablesReq*)p;
1,486,751,402✔
640
  taosArrayDestroy(pRes->pTables);
1,486,751,402✔
641
}
1,486,783,351✔
642

643
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
942,305,219✔
644
  if (NULL == pCatalogReq) {
942,305,219✔
645
    return;
329,116✔
646
  }
647
  taosArrayDestroy(pCatalogReq->pDbVgroup);
941,976,103✔
648
  taosArrayDestroy(pCatalogReq->pDbCfg);
942,037,523✔
649
  taosArrayDestroy(pCatalogReq->pDbInfo);
942,054,589✔
650
  if (pCatalogReq->cloned) {
942,045,307✔
651
    taosArrayDestroy(pCatalogReq->pTableMeta);
×
652
    taosArrayDestroy(pCatalogReq->pTableHash);
×
653
#ifdef TD_ENTERPRISE
654
    taosArrayDestroy(pCatalogReq->pView);
×
655
#endif
656
    taosArrayDestroy(pCatalogReq->pTableTSMAs);
×
657
    taosArrayDestroy(pCatalogReq->pTSMAs);
×
658
    taosArrayDestroy(pCatalogReq->pTableName);
×
659
  } else {
660
    taosArrayDestroyEx(pCatalogReq->pTableMeta, destoryTablesReq);
942,035,754✔
661
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
942,023,007✔
662
#ifdef TD_ENTERPRISE
663
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
942,037,888✔
664
#endif
665
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
942,037,794✔
666
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
942,035,673✔
667
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
942,035,392✔
668
  }
669
  taosArrayDestroy(pCatalogReq->pUdf);
942,033,814✔
670
  taosArrayDestroy(pCatalogReq->pIndex);
942,035,214✔
671
  taosArrayDestroy(pCatalogReq->pUser);
942,034,715✔
672
  taosArrayDestroy(pCatalogReq->pTableIndex);
942,039,345✔
673
  taosArrayDestroy(pCatalogReq->pTableCfg);
942,038,655✔
674
  taosArrayDestroy(pCatalogReq->pTableTag);
942,036,768✔
675
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
942,033,096✔
676
}
677

678
void tfreeSParseQueryRes(void* p) {
326,288✔
679
  if (NULL == p) {
326,288✔
680
    return;
×
681
  }
682

683
  SParseQueryRes* pRes = p;
326,288✔
684
  destoryCatalogReq(pRes->pCatalogReq);
326,288✔
685
  taosMemoryFree(pRes->pCatalogReq);
326,288✔
686
  catalogFreeMetaData(&pRes->meta);
326,288✔
687
}
688

689
void qDestroyParseContext(SParseContext* pCxt) {
941,999,641✔
690
  if (NULL == pCxt) {
941,999,641✔
691
    return;
×
692
  }
693

694
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
941,999,641✔
695
  taosArrayDestroy(pCxt->pTableMetaPos);
942,027,073✔
696
  taosArrayDestroy(pCxt->pTableVgroupPos);
942,033,233✔
697
  taosMemoryFree(pCxt);
942,032,173✔
698
}
699

700
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
1,161,619,754✔
701

702
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
73,913✔
703
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
73,913✔
704
}
705

706
int32_t qSetSTableIdForRsma(SNode* pStmt, int64_t uid) {
×
707
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
×
708
    SNode* pTable = ((SSelectStmt*)pStmt)->pFromTable;
×
709
    if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
×
710
      ((SRealTableNode*)pTable)->pMeta->uid = uid;
×
711
      ((SRealTableNode*)pTable)->pMeta->suid = uid;
×
712
      return TSDB_CODE_SUCCESS;
×
713
    }
714
  }
715
  return TSDB_CODE_FAILED;
×
716
}
717

718
int32_t qInitKeywordsTable() { return taosInitKeywordsTable(); }
1,608,805✔
719

720
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
1,598,311✔
721

722
int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx, void *charsetCxt) {
14,604✔
723
  int32_t code = TSDB_CODE_SUCCESS;
14,604✔
724

725
  if (colIdx < 0) {
14,604✔
726
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
14,180✔
727
    for (int32_t i = 0; i < size; ++i) {
29,665✔
728
      code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
15,485✔
729
      if (TSDB_CODE_SUCCESS != code) {
15,485✔
730
        return code;
×
731
      }
732
    }
733
  } else {
734
    code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
424✔
735
  }
736

737
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
14,604✔
738
    nodesDestroyNode(pQuery->pRoot);
14,604✔
739
    pQuery->pRoot = NULL;
14,604✔
740
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
14,604✔
741
  }
742
  if (TSDB_CODE_SUCCESS == code) {
14,604✔
743
    rewriteExprAlias(pQuery->pRoot);
14,604✔
744
  }
745
  return code;
14,604✔
746
}
747

748
static int32_t setValueByBindParam2(SValueNode* pVal, TAOS_STMT2_BIND* pParam, void* charsetCxt) {
10,590✔
749
  if (!pParam || IS_NULL_TYPE(pParam->buffer_type)) {
10,590✔
750
    return TSDB_CODE_APP_ERROR;
×
751
  }
752
  if (IS_VAR_DATA_TYPE(pVal->node.resType.type) || pVal->node.resType.type == TSDB_DATA_TYPE_DECIMAL) {
10,590✔
753
    taosMemoryFreeClear(pVal->datum.p);
×
754
  }
755

756
  if (pParam->is_null && 1 == *(pParam->is_null)) {
10,590✔
757
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
×
758
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
×
759
    return TSDB_CODE_SUCCESS;
×
760
  }
761

762
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
10,590✔
763
  pVal->node.resType.type = pParam->buffer_type;
10,590✔
764
  pVal->node.resType.bytes = inputSize;
10,590✔
765

766
  switch (pParam->buffer_type) {
10,590✔
767
    case TSDB_DATA_TYPE_VARBINARY:
×
768
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
769
      if (NULL == pVal->datum.p) {
×
770
        return terrno;
×
771
      }
772
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
773
      memcpy(varDataVal(pVal->datum.p), pParam->buffer, pVal->node.resType.bytes);
×
774
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
775
      break;
×
776
    case TSDB_DATA_TYPE_VARCHAR:
1,818✔
777
    case TSDB_DATA_TYPE_GEOMETRY:
778
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
1,818✔
779
      if (NULL == pVal->datum.p) {
1,818✔
780
        return terrno;
×
781
      }
782
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
1,818✔
783
      TAOS_STRNCPY(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
1,818✔
784
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
1,818✔
785
      if (IS_DURATION_VAL(pVal->flag)) {
1,818✔
786
        taosMemoryFreeClear(pVal->literal);
606✔
787
        taosMemoryFreeClear(pVal->datum.p);
606✔
788
        pVal->literal = taosStrndup((const char*)pParam->buffer, pVal->node.resType.bytes - VARSTR_HEADER_SIZE);
606✔
789
        if (!pVal->literal) {
606✔
790
          return terrno;
×
791
        }
792
        int64_t duration = 0;
606✔
793
        char    unit = 0;
606✔
794
        if (parseNatualDuration(pVal->literal, strlen(pVal->literal), &duration, &unit,
606✔
795
                                pVal->node.resType.precision, true) != TSDB_CODE_SUCCESS) {
606✔
796
          return TSDB_CODE_PAR_WRONG_VALUE_TYPE;
×
797
        }
798
        pVal->datum.i = duration;
606✔
799
        pVal->unit = unit;
606✔
800
        *(int64_t*)&pVal->typeData = duration;
606✔
801
        pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT;
606✔
802
        pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
606✔
803
      }
804
      break;
1,818✔
805
    case TSDB_DATA_TYPE_NCHAR: {
×
806
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
×
807
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
808
      if (NULL == pVal->datum.p) {
×
809
        return terrno;
×
810
      }
811

812
      int32_t output = 0;
×
813
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
814
                         &output, charsetCxt)) {
815
        return terrno;
×
816
      }
817
      varDataSetLen(pVal->datum.p, output);
×
818
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
819
      break;
×
820
    }
821
    case TSDB_DATA_TYPE_DECIMAL64: {
101✔
822
      // TSDB_DATA_TYPE_DECIMAL64: buffer may be string, need to convert to int64_t
823
      // If buffer is string, convert it to decimal64 value first
824
      if (pParam->length && *(pParam->length) > 0 && *(pParam->length) != sizeof(int64_t)) {
101✔
825
        // Buffer is string, need to convert
826
        uint8_t precision = pVal->node.resType.precision;
101✔
827
        uint8_t scale = pVal->node.resType.scale;
101✔
828
        // If precision/scale not set, use default (should not happen in normal case)
829
        if (precision == 0 && scale == 0) {
101✔
830
          precision = 18;
101✔
831
          scale = 0;
101✔
832
        }
833
        Decimal64 dec = {0};
101✔
834
        int32_t   code = decimal64FromStr((const char*)pParam->buffer, *(pParam->length), precision, scale, &dec);
101✔
835
        if (code != TSDB_CODE_SUCCESS) {
101✔
836
          return code;
×
837
        }
838
        int64_t value = DECIMAL64_GET_VALUE(&dec);
101✔
839
        pVal->datum.i = value;
101✔
840
        pVal->typeData = value;
101✔
841
        pVal->node.resType.bytes = sizeof(int64_t);
101✔
842
      } else {
843
        // Buffer is already int64_t value, use it directly
844
        int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
×
845
        if (code) {
×
846
          return code;
×
847
        }
848
      }
849
      break;
101✔
850
    }
851
    case TSDB_DATA_TYPE_DECIMAL: {
101✔
852
      // TSDB_DATA_TYPE_DECIMAL: buffer is string, need to convert to decimal128 binary format
853
      pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DECIMAL].bytes;
101✔
854
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes);
101✔
855
      if (NULL == pVal->datum.p) {
101✔
856
        return terrno;
×
857
      }
858

859
      // Check if buffer is string or already binary format
860
      int32_t strLen = (pParam->length && *(pParam->length) > 0) ? *(pParam->length) : 0;
101✔
861
      if (strLen > 0 && strLen != pVal->node.resType.bytes) {
101✔
862
        // Buffer is string, need to convert to decimal128
863
        uint8_t precision = pVal->node.resType.precision;
101✔
864
        uint8_t scale = pVal->node.resType.scale;
101✔
865
        // If precision/scale not set, use default (should not happen in normal case)
866
        if (precision == 0 && scale == 0) {
101✔
867
          precision = 38;
101✔
868
          scale = 0;
101✔
869
        }
870
        Decimal128 dec = {0};
101✔
871
        int32_t    code = decimal128FromStr((const char*)pParam->buffer, strLen, precision, scale, &dec);
101✔
872
        if (code != TSDB_CODE_SUCCESS) {
101✔
873
          taosMemoryFree(pVal->datum.p);
×
874
          pVal->datum.p = NULL;
×
875
          return code;
×
876
        }
877
        // Copy decimal128 binary data
878
        memcpy(pVal->datum.p, &dec, sizeof(Decimal128));
101✔
879
      } else {
880
        // Buffer is already binary format, copy directly
881
        memcpy(pVal->datum.p, pParam->buffer, pVal->node.resType.bytes);
×
882
      }
883
      break;
101✔
884
    }
885
    case TSDB_DATA_TYPE_BLOB:
×
886
    case TSDB_DATA_TYPE_MEDIUMBLOB:
887
      return TSDB_CODE_BLOB_NOT_SUPPORT;  // BLOB data type is not supported in stmt2
×
888
    default: {
8,570✔
889
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
8,570✔
890
      if (code) {
8,570✔
891
        return code;
×
892
      }
893
      break;
8,570✔
894
    }
895
  }
896
  pVal->translate = true;
10,590✔
897
  return TSDB_CODE_SUCCESS;
10,590✔
898
}
899

900
int32_t qStmtBindParams2(SQuery* pQuery, TAOS_STMT2_BIND* pParams, int32_t colIdx, void* charsetCxt) {
7,019✔
901
  int32_t code = TSDB_CODE_SUCCESS;
7,019✔
902

903
  if (colIdx < 0) {
7,019✔
904
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
7,019✔
905
    for (int32_t i = 0; i < size; ++i) {
17,609✔
906
      code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
10,590✔
907
      if (TSDB_CODE_SUCCESS != code) {
10,590✔
908
        return code;
×
909
      }
910
    }
911
  } else {
912
    code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
×
913
  }
914

915
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
7,019✔
916
    nodesDestroyNode(pQuery->pRoot);
7,019✔
917
    pQuery->pRoot = NULL;
7,019✔
918
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
7,019✔
919
  }
920
  if (TSDB_CODE_SUCCESS == code) {
7,019✔
921
    rewriteExprAlias(pQuery->pRoot);
7,019✔
922
  }
923
  return code;
7,019✔
924
}
925

926
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery, SMetaData* pMetaData) {
21,623✔
927
  SParseMetaCache metaCache = {0};
21,623✔
928
  int32_t         code = TSDB_CODE_SUCCESS;
21,623✔
929

930
  // If metaData is provided, we need to collect metadata keys first to build SCatalogReq
931
  // Then put the metaData into cache
932
  if (pMetaData) {
21,623✔
933
    SCatalogReq catalogReq = {0};
7,019✔
934
    // After collectMetaKey/buildCatalogReq, metaCache contains "request/reserved" hashes.
935
    // We must clear them before putMetaDataToCache, otherwise the hash ends up mixed
936
    // (db-keyed request entries + tb-keyed metadata entries) and destoryParseMetaCache(false)
937
    // will not release nested request hashes (leading to LeakSanitizer reports).
938
    bool metaCacheIsRequest = true;
7,019✔
939
    // Collect metadata requirements from query
940
    code = collectMetaKey(pCxt, pQuery, &metaCache);
7,019✔
941
    if (TSDB_CODE_SUCCESS == code) {
7,019✔
942
      // Build catalog request from collected metadata requirements
943
      code = buildCatalogReq(&metaCache, &catalogReq);
7,019✔
944
    }
945
    if (TSDB_CODE_SUCCESS == code) {
7,019✔
946
      // Switch metaCache from request-mode to metadata-mode.
947
      destoryParseMetaCache(&metaCache, true);
7,019✔
948
      (void)memset(&metaCache, 0, sizeof(metaCache));
7,019✔
949
      metaCacheIsRequest = false;
7,019✔
950

951
      // Put metadata to cache using the catalogReq to match data
952
      code = putMetaDataToCache(&catalogReq, pMetaData, &metaCache);
7,019✔
953
    }
954
    // Clean up catalog request
955
    destoryCatalogReq(&catalogReq);
7,019✔
956
    if (TSDB_CODE_SUCCESS != code) {
7,019✔
957
      destoryParseMetaCache(&metaCache, metaCacheIsRequest);
×
958
      return code;
×
959
    }
960
  }
961

962
  code = translate(pCxt, pQuery, &metaCache);
21,623✔
963
  if (TSDB_CODE_SUCCESS == code) {
21,623✔
964
    code = calculateConstant(pCxt, pQuery);
21,522✔
965
  }
966
  destoryParseMetaCache(&metaCache, false);
21,623✔
967
  return code;
21,623✔
968
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc