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

taosdata / TDengine / #4911

04 Jan 2026 09:05AM UTC coverage: 65.028% (-0.8%) from 65.864%
#4911

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

1517 existing lines in 134 files now uncovered.

195276 of 300296 relevant lines covered (65.03%)

116931714.52 hits per line

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

48.79
/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 "parInsertUtil.h"
22
#include "parInt.h"
23
#include "parToken.h"
24
#include "tname.h"
25

26
bool qIsInsertValuesSql(const char* pStr, size_t length) {
1,519,364,376✔
27
  if (NULL == pStr) {
1,519,364,376✔
28
    return false;
×
29
  }
30

31
  const char* pSql = pStr;
1,519,364,376✔
32

33
  int32_t index = 0;
1,519,364,376✔
34
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
1,519,371,050✔
35
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
1,519,372,111✔
36
    return false;
515,494,892✔
37
  }
38

39
  do {
40
    pStr += index;
2,147,483,647✔
41
    index = 0;
2,147,483,647✔
42
    t = tStrGetToken((char*)pStr, &index, false, NULL);
2,147,483,647✔
43
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
2,147,483,647✔
44
      return true;
1,003,576,638✔
45
    } else if (TK_SELECT == t.type) {
2,147,483,647✔
46
      return false;
302,642✔
47
    }
48
    if (0 == t.type || 0 == t.n) {
2,147,483,647✔
49
      break;
50
    }
51
  } while (pStr - pSql < length);
2,147,483,647✔
52
  return false;
2,224✔
53
}
54

55
bool qIsUpdateSetSql(const char* pStr, size_t length, SName* pTableName, int32_t acctId, const char* dbName,
3,318,480✔
56
                     char* msgBuf, int32_t msgBufLen, int* pCode) {
57
                        if (NULL == pStr) {
3,318,480✔
58
    return false;
×
59
  }
60

61
  const char* pSql = pStr;
3,318,480✔
62

63
  int32_t index = 0;
3,318,480✔
64
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
3,318,799✔
65
    if (TK_UPDATE != t.type) {
3,319,437✔
66
    return false;
3,319,437✔
67
  }
68
  SMsgBuf pMsgBuf = {.len = msgBufLen, .buf = msgBuf};
×
69
  pStr += index;
×
70
  index = 0;
×
71
  t = tStrGetToken((char*)pStr, &index, false, NULL);
×
72
  if (t.n == 0 || t.z == NULL) {
×
73
    *pCode = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_TSC_STMT_TBNAME_ERROR, "Invalid table name");
×
74
    return false;
×
75
  }
76

77
  if (pTableName != NULL) {
×
78
    *pCode = insCreateSName(pTableName, &t, acctId, dbName, &pMsgBuf);
×
79
    if ((*pCode) != TSDB_CODE_SUCCESS) {
×
80
      return false;
×
81
    }
82
  }
83
    do {
84
    pStr += index;
×
85
    index = 0;
×
86
    t = tStrGetToken((char*)pStr, &index, false, NULL);
×
87
        if (TK_SET == t.type) {
×
88
      return true;
×
89
    }
90
    if (0 == t.type || 0 == t.n) {
×
91
      break;
92
    }
93
  } while (pStr - pSql < length);
×
94
  return false;
×
95
}
96

97
bool qIsSelectFromSql(const char* pStr, size_t length) {
2,807✔
98
  if (NULL == pStr) {
2,807✔
99
    return false;
×
100
  }
101

102
  const char* pSql = pStr;
2,807✔
103

104
  int32_t index = 0;
2,807✔
105
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
2,849✔
106
  if (TK_SELECT != t.type) {
2,807✔
107
    return false;
2,799✔
108
  }
109

110
  do {
111
    pStr += index;
16✔
112
    index = 0;
16✔
113
    t = tStrGetToken((char*)pStr, &index, false, NULL);
16✔
114
    if (TK_FROM == t.type) {
16✔
115
      return true;
8✔
116
    }
117
    if (0 == t.type || 0 == t.n) {
8✔
118
      break;
119
    }
120
  } while (pStr - pSql < length);
8✔
121

122
  return false;
×
123
}
124

125
static bool isColumnPrimaryKey(const STableMeta* pTableMeta, const char* colName, int32_t colNameLen, int32_t* colId) {
×
126
  if (pTableMeta == NULL || colName == NULL) {
×
127
    return false;
×
128
  }
129

130
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; i++) {
×
131
    const SSchema* pSchema = &pTableMeta->schema[i];
×
132
    if (strncmp(pSchema->name, colName, colNameLen) == 0 && strlen(pSchema->name) == colNameLen) {
×
133
      if (colId) {
×
134
        *colId = i;
×
135
      }
136
      if ((pSchema->flags & COL_IS_KEY || pSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
×
137
        return true;
×
138
      }
139
      return false;
×
140
    }
141
  }
142
  return false;
×
143
}
144

145
int32_t convertUpdateToInsert(const char* pSql, char** pNewSql, STableMeta* pTableMeta, SSHashObj* predicateCols,
×
146
                              char* msgBuf, int32_t msgBufLen) {
147
  if (NULL == pSql || NULL == pNewSql) {
×
148
    return TSDB_CODE_INVALID_PARA;
×
149
  }
150

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

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

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

181
  p += sprintf(p, "INSERT INTO ");
×
182
  memcpy(p, t.z, t.n);
×
183
  p += t.n;
×
184
  p += sprintf(p, " (");
×
185
  pSql += index;
×
186

187
  // SET
188
  index = 0;
×
189
  t = tStrGetToken((char*)pSql, &index, false, NULL);
×
190
  if (TK_SET != t.type) {
×
191
    taosMemoryFree(newSql);
×
192
    code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected SET keyword");
×
193
    return code;
×
194
  }
195
  pSql += index;
×
196

197
  bool    firstColumn = true;
×
198
  int32_t columnCount = 0;
×
199
  bool inSetClause = true;
×
200
  int32_t numOfCols = 0;
×
201

202
  // col name
203
  while (inSetClause && pSql < pEnd) {
×
204
    index = 0;
×
205
    t = tStrGetToken((char*)pSql, &index, false, NULL);
×
206
    if (t.n == 0 || t.z == NULL) {
×
207
      break;
208
    }
209

210
    // pk can't set
211
    if (pTableMeta != NULL && isColumnPrimaryKey(pTableMeta, t.z, t.n, NULL)) {
×
212
      taosMemoryFree(newSql);
×
213
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Cannot update primary key column '%.*s'",
×
214
                                     t.n, t.z);
215
      return code;
×
216
    }
217

218
    if (!firstColumn) {
×
219
      *p++ = ',';
×
220
    }
221
    numOfCols++;
×
222
    memcpy(p, t.z, t.n);
×
223
    p += t.n;
×
224
    firstColumn = false;
×
225
    columnCount++;
×
226
    pSql += index;
×
227

228
    index = 0;
×
229
    t = tStrGetToken((char*)pSql, &index, false, NULL);
×
230
    if (t.type != TK_NK_EQ) {
×
231
      taosMemoryFree(newSql);
×
232
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '=' after column name");
×
233
      return code;
×
234
    }
235
    pSql += index;
×
236

237
    // value must be ?
238
    index = 0;
×
239
    t = tStrGetToken((char*)pSql, &index, false, NULL);
×
240
    if (t.n == 0 || t.z == NULL) {
×
241
      break;
242
    }
243
    if (t.type != TK_NK_QUESTION) {
×
244
      taosMemoryFree(newSql);
×
245
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '?' placeholder");
×
246
      return code;
×
247
    }
248
    pSql += index;
×
249

250
    index = 0;
×
251
    t = tStrGetToken((char*)pSql, &index, false, NULL);
×
252
    if (t.type == TK_WHERE) {
×
253
      inSetClause = false;
×
254
      pSql += index;
×
255
    }
256
  }
257

258
  // where clause
259
  if (pSql < pEnd) {
×
260
    bool inWhereClause = true;
×
261
    int32_t bracketLevel = 0;
×
262

263
    while (inWhereClause && pSql < pEnd) {
×
264
      index = 0;
×
265
      t = tStrGetToken((char*)pSql, &index, false, NULL);
×
266
      if (t.n == 0 || t.z == NULL) {
×
267
        break;
268
      }
269

270
      if (t.type == TK_NK_LP) {
×
271
        bracketLevel++;
×
272
        pSql += index;
×
273
        continue;
×
274
      } else if (t.type == TK_NK_RP) {
×
275
        bracketLevel--;
×
276
        pSql += index;
×
277
        continue;
×
278
      } else if (t.type == TK_IN || t.type == TK_EXISTS) {
×
279
        while (pSql < pEnd) {
×
280
          pSql += index;
×
281
          index = 0;
×
282
          t = tStrGetToken((char*)pSql, &index, false, NULL);
×
283
          if (t.type == TK_AND || t.type == TK_OR || t.n == 0 || t.z == NULL) {
×
284
            break;
285
          }
286
        }
287
        continue;
×
288
      }
289

290
      const char* colName = t.z;
×
291
      int32_t     colNameLen = t.n;
×
292
      pSql += index;
×
293

294
      index = 0;
×
295
      t = tStrGetToken((char*)pSql, &index, false, NULL);
×
296
      if (t.n == 0 || t.z == NULL) {
×
297
        break;
298
      }
299
      pSql += index;
×
300

301
      index = 0;
×
302
      t = tStrGetToken((char*)pSql, &index, false, NULL);
×
303
      if (t.n == 0 || t.z == NULL) {
×
304
        break;
305
      }
306

307
      // where cols muset be pk, ignore others
308
      int32_t colId = -1;
×
309
      if (t.type == TK_NK_QUESTION) {
×
310
        if (pTableMeta != NULL && isColumnPrimaryKey(pTableMeta, colName, colNameLen, &colId)) {
×
311
          if (!firstColumn) {
×
312
            *p++ = ',';
×
313
          }
314
          memcpy(p, colName, colNameLen);
×
315
          p += colNameLen;
×
316
          firstColumn = false;
×
317
          columnCount++;
×
318
        } else {
319
          if (tSimpleHashPut(predicateCols, &numOfCols, sizeof(int32_t), &colId, sizeof(int32_t))) {
×
320
            taosMemoryFree(newSql);
×
321
            code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '?' placeholder");
×
322
            return code;
×
323
          }
324
        }
325
        numOfCols++;
×
326
      }
327
      pSql += index;
×
328

329
      index = 0;
×
330
      t = tStrGetToken((char*)pSql, &index, false, NULL);
×
331
      if (t.type == TK_AND || t.type == TK_OR) {
×
332
        pSql += index;
×
333
      } else {
334
        if (bracketLevel == 0) {
×
335
          break;
×
336
        }
337
        pSql += index;
×
338
      }
339
    }
340
  }
341

342
  p += sprintf(p, ") VALUES (");
×
343
  for (int32_t i = 0; i < columnCount; i++) {
×
344
    if (i > 0) {
×
345
      *p++ = ',';
×
346
    }
347
    *p++ = '?';
×
348
  }
349
  *p++ = ')';
×
350
  *p = '\0';
×
351

352
  *pNewSql = newSql;
×
353
  return code;
×
354
}
355

356
bool qIsCreateTbFromFileSql(const char* pStr, size_t length) {
258,161,180✔
357
  if (NULL == pStr) {
258,161,180✔
358
    return false;
×
359
  }
360

361
  const char* pSql = pStr;
258,161,180✔
362

363
  int32_t index = 0;
258,161,180✔
364
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
258,163,580✔
365
  if (TK_CREATE != t.type) {
258,168,631✔
366
    return false;
208,674,134✔
367
  }
368

369
  do {
370
    pStr += index;
1,465,774,776✔
371
    index = 0;
1,465,786,626✔
372
    t = tStrGetToken((char*)pStr, &index, false, NULL);
1,465,786,626✔
373
    if (TK_FILE == t.type) {
1,465,774,778✔
374
      return true;
2,570✔
375
    }
376
    if (0 == t.type || 0 == t.n) {
1,465,772,208✔
377
      break;
378
    }
379
  } while (pStr - pSql < length);
1,416,287,689✔
380
  return false;
49,491,929✔
381
}
382

383
bool qParseDbName(const char* pStr, size_t length, char** pDbName) {
3,321,343✔
384
  (void)length;
385
  int32_t index = 0;
3,321,343✔
386
  SToken  t;
387

388
  if (NULL == pStr) {
3,321,662✔
389
    *pDbName = NULL;
×
390
    return false;
×
391
  }
392

393
  t = tStrGetToken((char*)pStr, &index, false, NULL);
3,321,662✔
394
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
3,322,341✔
395
    *pDbName = NULL;
10,062✔
396
    return false;
10,062✔
397
  }
398

399
  t = tStrGetToken((char*)pStr, &index, false, NULL);
3,312,279✔
400
  if (TK_INTO != t.type) {
3,312,557✔
401
    *pDbName = NULL;
×
402
    return false;
×
403
  }
404

405
  t = tStrGetToken((char*)pStr, &index, false, NULL);
3,312,557✔
406
  if (t.n == 0 || t.z == NULL) {
3,312,917✔
407
    *pDbName = NULL;
998✔
UNCOV
408
    return false;
×
409
  }
410
  char* dotPos = strnchr(t.z, '.', t.n, true);
3,311,919✔
411
  if (dotPos != NULL) {
3,309,292✔
412
    int dbNameLen = dotPos - t.z;
104,225✔
413
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
104,225✔
414
    if (*pDbName == NULL) {
105,936✔
415
      return false;
×
416
    }
417
    strncpy(*pDbName, t.z, dbNameLen);
105,936✔
418
    (*pDbName)[dbNameLen] = '\0';
105,936✔
419
    return true;
105,895✔
420
  }
421
  return false;
3,205,067✔
422
}
423

424
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
250,474,830✔
425
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
250,474,830✔
426

427
  if (pCxt->parseOnly) {
250,471,943✔
428
    return code;
9,113✔
429
  }
430

431
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
250,463,656✔
432
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
8,952✔
433
    return TSDB_CODE_SUCCESS;
8,952✔
434
  }
435

436
  if (TSDB_CODE_SUCCESS == code) {
250,456,352✔
437
    code = translate(pCxt, pQuery, pMetaCache);
250,305,771✔
438
  }
439
  if (TSDB_CODE_SUCCESS == code) {
250,445,495✔
440
    code = calculateConstant(pCxt, pQuery);
204,265,603✔
441
  }
442
  return code;
250,429,241✔
443
}
444

445
static int32_t parseSqlIntoAst(SParseContext* pCxt, SQuery** pQuery) {
8,952✔
446
  int32_t code = parse(pCxt, pQuery);
8,952✔
447
  if (TSDB_CODE_SUCCESS == code) {
8,952✔
448
    code = analyseSemantic(pCxt, *pQuery, NULL);
8,952✔
449
  }
450
  return code;
8,952✔
451
}
452

453
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
258,159,158✔
454
  int32_t code = parse(pCxt, pQuery);
258,159,158✔
455
  if (TSDB_CODE_SUCCESS == code) {
258,148,100✔
456
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
250,453,271✔
457
  }
458
  return code;
258,161,283✔
459
}
460

461
static int32_t setValueByBindParam(SValueNode* pVal, TAOS_MULTI_BIND* pParam, void *charsetCxt) {
9,503✔
462
  if (!pParam || IS_NULL_TYPE(pParam->buffer_type)) {
9,503✔
463
    return TSDB_CODE_APP_ERROR;
×
464
  }
465
  if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) {
9,503✔
466
    taosMemoryFreeClear(pVal->datum.p);
×
467
  }
468

469
  if (pParam->is_null && 1 == *(pParam->is_null)) {
9,503✔
470
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
×
471
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
×
472
    return TSDB_CODE_SUCCESS;
×
473
  }
474

475
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
9,503✔
476
  pVal->node.resType.type = pParam->buffer_type;
9,503✔
477
  pVal->node.resType.bytes = inputSize;
9,503✔
478

479
  switch (pParam->buffer_type) {
9,503✔
480
    case TSDB_DATA_TYPE_VARBINARY:
×
481
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
482
      if (NULL == pVal->datum.p) {
×
483
        return terrno;
×
484
      }
485
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
486
      memcpy(varDataVal(pVal->datum.p), pParam->buffer, pVal->node.resType.bytes);
×
487
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
488
      break;
×
489
    case TSDB_DATA_TYPE_VARCHAR:
3,354✔
490
    case TSDB_DATA_TYPE_GEOMETRY:
491
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
3,354✔
492
      if (NULL == pVal->datum.p) {
3,354✔
493
        return terrno;
×
494
      }
495
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
3,354✔
496
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
3,354✔
497
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
3,354✔
498
      break;
3,354✔
499
    case TSDB_DATA_TYPE_NCHAR: {
×
500
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
×
501
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
502
      if (NULL == pVal->datum.p) {
×
503
        return terrno;
×
504
      }
505

506
      int32_t output = 0;
×
507
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
508
                         &output, charsetCxt)) {
509
        return terrno;
×
510
      }
511
      varDataSetLen(pVal->datum.p, output);
×
512
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
513
      break;
×
514
    }
515
    default: {
6,149✔
516
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
6,149✔
517
      if (code) {
6,149✔
518
        return code;
×
519
      }
520
      break;
6,149✔
521
    }
522
  }
523
  pVal->translate = true;
9,503✔
524
  return TSDB_CODE_SUCCESS;
9,503✔
525
}
526

527
static EDealRes rewriteQueryExprAliasImpl(SNode* pNode, void* pContext) {
46,469✔
528
  if (nodesIsExprNode(pNode) && QUERY_NODE_COLUMN != nodeType(pNode)) {
46,469✔
529
    snprintf(((SExprNode*)pNode)->aliasName, TSDB_COL_NAME_LEN, "#%d", *(int32_t*)pContext);
20,723✔
530
    ++(*(int32_t*)pContext);
20,723✔
531
  }
532
  return DEAL_RES_CONTINUE;
46,469✔
533
}
534

535
static void rewriteQueryExprAlias(SNode* pRoot, int32_t* pNo) {
8,952✔
536
  switch (nodeType(pRoot)) {
8,952✔
537
    case QUERY_NODE_SELECT_STMT:
8,952✔
538
      nodesWalkSelectStmt((SSelectStmt*)pRoot, SQL_CLAUSE_FROM, rewriteQueryExprAliasImpl, pNo);
8,952✔
539
      break;
8,952✔
540
    case QUERY_NODE_SET_OPERATOR: {
×
541
      SSetOperator* pSetOper = (SSetOperator*)pRoot;
×
542
      rewriteQueryExprAlias(pSetOper->pLeft, pNo);
×
543
      rewriteQueryExprAlias(pSetOper->pRight, pNo);
×
544
      break;
×
545
    }
546
    default:
×
547
      break;
×
548
  }
549
}
8,952✔
550

551
static void rewriteExprAlias(SNode* pRoot) {
8,952✔
552
  int32_t no = 1;
8,952✔
553
  rewriteQueryExprAlias(pRoot, &no);
8,952✔
554
}
8,952✔
555

556
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
3,318,620✔
557
  int32_t code = TSDB_CODE_SUCCESS;
3,318,620✔
558
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
3,318,620✔
559
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
3,311,189✔
560
  } else {
561
    code = parseSqlIntoAst(pCxt, pQuery);
8,952✔
562
  }
563
  terrno = code;
3,315,994✔
564
  return code;
3,318,865✔
565
}
566

567
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
258,159,795✔
568
  SParseMetaCache metaCache = {0};
258,159,795✔
569
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
258,161,326✔
570
  if (TSDB_CODE_SUCCESS == code) {
258,161,003✔
571
    code = buildCatalogReq(&metaCache, pCatalogReq);
250,466,713✔
572
  }
573
  destoryParseMetaCache(&metaCache, true);
258,160,272✔
574
  return code;
258,152,069✔
575
}
576

577
static int32_t parseCreateTbFromFileSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
2,570✔
578
  if (NULL == *pQuery) return parseQuerySyntax(pCxt, pQuery, pCatalogReq);
2,570✔
579

580
  return continueCreateTbFromFile(pCxt, pQuery);
1,285✔
581
}
582

583
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
755,003,244✔
584
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
755,003,244✔
585
  if (TSDB_CODE_SUCCESS == code) {
755,007,067✔
586
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
755,007,067✔
587
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
496,846,388✔
588
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
258,166,542✔
589
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
2,570✔
590
    } else {
591
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
258,159,595✔
592
    }
593
  }
594
  (void)nodesReleaseAllocator(pCxt->allocatorId);
754,989,159✔
595
  terrno = code;
755,004,825✔
596
  return code;
754,997,272✔
597
}
598

599
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
250,469,029✔
600
                            struct SMetaData* pMetaData, SQuery* pQuery) {
601
  SParseMetaCache metaCache = {0};
250,469,029✔
602
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
250,469,079✔
603
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
250,470,229✔
604
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
250,470,644✔
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
250,469,695✔
607
    code = analyseSemantic(pCxt, pQuery, &metaCache);
250,466,831✔
608
  }
609
  (void)nodesReleaseAllocator(pCxt->allocatorId);
250,451,897✔
610
  destoryParseMetaCache(&metaCache, false);
250,467,016✔
611
  terrno = code;
250,451,100✔
612
  return code;
250,456,897✔
613
}
614

615
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
6,491,184✔
616
                          SQuery* pQuery) {
617
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
6,491,184✔
618
}
619

620
int32_t qContinueParsePostQuery(SParseContext* pCxt, SQuery* pQuery, SSDataBlock* pBlock) {
×
621
  int32_t code = TSDB_CODE_SUCCESS;
×
622
  switch (nodeType(pQuery->pRoot)) {
×
623
    default:
624
      break;
×
625
  }
626

627
  return code;
×
628
}
629

630
static void destoryTablesReq(void* p) {
902,283,237✔
631
  STablesReq* pRes = (STablesReq*)p;
902,283,237✔
632
  taosArrayDestroy(pRes->pTables);
902,283,237✔
633
}
902,277,110✔
634

635
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
755,010,448✔
636
  if (NULL == pCatalogReq) {
755,010,448✔
637
    return;
9,113✔
638
  }
639
  taosArrayDestroy(pCatalogReq->pDbVgroup);
755,001,335✔
640
  taosArrayDestroy(pCatalogReq->pDbCfg);
755,001,294✔
641
  taosArrayDestroy(pCatalogReq->pDbInfo);
755,001,109✔
642
  if (pCatalogReq->cloned) {
755,001,440✔
643
    taosArrayDestroy(pCatalogReq->pTableMeta);
×
644
    taosArrayDestroy(pCatalogReq->pTableHash);
×
645
#ifdef TD_ENTERPRISE
646
    taosArrayDestroy(pCatalogReq->pView);
×
647
#endif
648
    taosArrayDestroy(pCatalogReq->pTableTSMAs);
×
649
    taosArrayDestroy(pCatalogReq->pTSMAs);
×
650
    taosArrayDestroy(pCatalogReq->pTableName);
×
651
  } else {
652
    taosArrayDestroyEx(pCatalogReq->pTableMeta, destoryTablesReq);
755,000,130✔
653
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
754,995,111✔
654
#ifdef TD_ENTERPRISE
655
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
754,992,978✔
656
#endif
657
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
754,997,821✔
658
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
754,995,446✔
659
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
754,997,049✔
660
  }
661
  taosArrayDestroy(pCatalogReq->pUdf);
754,999,606✔
662
  taosArrayDestroy(pCatalogReq->pIndex);
754,997,306✔
663
  taosArrayDestroy(pCatalogReq->pUser);
754,995,249✔
664
  taosArrayDestroy(pCatalogReq->pTableIndex);
754,992,760✔
665
  taosArrayDestroy(pCatalogReq->pTableCfg);
754,995,242✔
666
  taosArrayDestroy(pCatalogReq->pTableTag);
754,995,239✔
667
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
754,995,641✔
668
}
669

670
void tfreeSParseQueryRes(void* p) {
9,113✔
671
  if (NULL == p) {
9,113✔
672
    return;
×
673
  }
674

675
  SParseQueryRes* pRes = p;
9,113✔
676
  destoryCatalogReq(pRes->pCatalogReq);
9,113✔
677
  taosMemoryFree(pRes->pCatalogReq);
9,113✔
678
  catalogFreeMetaData(&pRes->meta);
9,113✔
679
}
680

681
void qDestroyParseContext(SParseContext* pCxt) {
754,995,764✔
682
  if (NULL == pCxt) {
754,995,764✔
683
    return;
×
684
  }
685

686
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
754,995,764✔
687
  taosArrayDestroy(pCxt->pTableMetaPos);
755,002,468✔
688
  taosArrayDestroy(pCxt->pTableVgroupPos);
755,003,267✔
689
  taosMemoryFree(pCxt);
755,001,658✔
690
}
691

692
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
819,690,048✔
693

694
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
93,367✔
695
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
93,367✔
696
}
697

698
int32_t qSetSTableIdForRsma(SNode* pStmt, int64_t uid) {
×
699
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
×
700
    SNode* pTable = ((SSelectStmt*)pStmt)->pFromTable;
×
701
    if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
×
702
      ((SRealTableNode*)pTable)->pMeta->uid = uid;
×
703
      ((SRealTableNode*)pTable)->pMeta->suid = uid;
×
704
      return TSDB_CODE_SUCCESS;
×
705
    }
706
  }
707
  return TSDB_CODE_FAILED;
×
708
}
709

710
int32_t qInitKeywordsTable() { return taosInitKeywordsTable(); }
1,171,532✔
711

712
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
1,171,546✔
713

714
int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx, void *charsetCxt) {
8,944✔
715
  int32_t code = TSDB_CODE_SUCCESS;
8,944✔
716

717
  if (colIdx < 0) {
8,944✔
718
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
8,944✔
719
    for (int32_t i = 0; i < size; ++i) {
18,447✔
720
      code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
9,503✔
721
      if (TSDB_CODE_SUCCESS != code) {
9,503✔
722
        return code;
×
723
      }
724
    }
725
  } else {
726
    code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
×
727
  }
728

729
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
8,944✔
730
    nodesDestroyNode(pQuery->pRoot);
8,944✔
731
    pQuery->pRoot = NULL;
8,944✔
732
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
8,944✔
733
  }
734
  if (TSDB_CODE_SUCCESS == code) {
8,944✔
735
    rewriteExprAlias(pQuery->pRoot);
8,944✔
736
  }
737
  return code;
8,944✔
738
}
739

740
static int32_t setValueByBindParam2(SValueNode* pVal, TAOS_STMT2_BIND* pParam, void* charsetCxt) {
16✔
741
  if (!pParam || IS_NULL_TYPE(pParam->buffer_type)) {
16✔
742
    return TSDB_CODE_APP_ERROR;
×
743
  }
744
  if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) {
16✔
745
    taosMemoryFreeClear(pVal->datum.p);
×
746
  }
747

748
  if (pParam->is_null && 1 == *(pParam->is_null)) {
16✔
749
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
×
750
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
×
751
    return TSDB_CODE_SUCCESS;
×
752
  }
753

754
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
16✔
755
  pVal->node.resType.type = pParam->buffer_type;
16✔
756
  pVal->node.resType.bytes = inputSize;
16✔
757

758
  switch (pParam->buffer_type) {
16✔
759
    case TSDB_DATA_TYPE_VARBINARY:
×
760
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
761
      if (NULL == pVal->datum.p) {
×
762
        return terrno;
×
763
      }
764
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
765
      memcpy(varDataVal(pVal->datum.p), pParam->buffer, pVal->node.resType.bytes);
×
766
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
767
      break;
×
768
    case TSDB_DATA_TYPE_VARCHAR:
×
769
    case TSDB_DATA_TYPE_GEOMETRY:
770
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
771
      if (NULL == pVal->datum.p) {
×
772
        return terrno;
×
773
      }
774
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
775
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
×
776
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
777
      break;
×
778
    case TSDB_DATA_TYPE_NCHAR: {
×
779
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
×
780
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
781
      if (NULL == pVal->datum.p) {
×
782
        return terrno;
×
783
      }
784

785
      int32_t output = 0;
×
786
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
787
                         &output, charsetCxt)) {
788
        return terrno;
×
789
      }
790
      varDataSetLen(pVal->datum.p, output);
×
791
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
792
      break;
×
793
    }
794
    case TSDB_DATA_TYPE_BLOB:
×
795
    case TSDB_DATA_TYPE_MEDIUMBLOB:
796
      return TSDB_CODE_BLOB_NOT_SUPPORT;  // BLOB data type is not supported in stmt2
×
797
    default: {
16✔
798
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
16✔
799
      if (code) {
16✔
800
        return code;
×
801
      }
802
      break;
16✔
803
    }
804
  }
805
  pVal->translate = true;
16✔
806
  return TSDB_CODE_SUCCESS;
16✔
807
}
808

809
int32_t qStmtBindParams2(SQuery* pQuery, TAOS_STMT2_BIND* pParams, int32_t colIdx, void* charsetCxt) {
8✔
810
  int32_t code = TSDB_CODE_SUCCESS;
8✔
811

812
  if (colIdx < 0) {
8✔
813
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
8✔
814
    for (int32_t i = 0; i < size; ++i) {
24✔
815
      code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
16✔
816
      if (TSDB_CODE_SUCCESS != code) {
16✔
817
        return code;
×
818
      }
819
    }
820
  } else {
821
    code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
×
822
  }
823

824
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
8✔
825
    nodesDestroyNode(pQuery->pRoot);
8✔
826
    pQuery->pRoot = NULL;
8✔
827
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
8✔
828
  }
829
  if (TSDB_CODE_SUCCESS == code) {
8✔
830
    rewriteExprAlias(pQuery->pRoot);
8✔
831
  }
832
  return code;
8✔
833
}
834

835
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery) {
8,952✔
836
  int32_t code = translate(pCxt, pQuery, NULL);
8,952✔
837
  if (TSDB_CODE_SUCCESS == code) {
8,952✔
838
    code = calculateConstant(pCxt, pQuery);
8,952✔
839
  }
840
  return code;
8,952✔
841
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc