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

taosdata / TDengine / #4946

02 Feb 2026 09:27AM UTC coverage: 66.932% (+0.06%) from 66.87%
#4946

push

travis-ci

web-flow
enh: [6690002267] Optimize virtual table query with plenty of columns. (#34341)

527 of 634 new or added lines in 23 files covered. (83.12%)

413 existing lines in 119 files now uncovered.

205724 of 307364 relevant lines covered (66.93%)

126528878.24 hits per line

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

47.19
/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
#include "ttime.h"
26

27
bool qIsInsertValuesSql(const char* pStr, size_t length) {
1,664,461,485✔
28
  if (NULL == pStr) {
1,664,461,485✔
29
    return false;
×
30
  }
31

32
  const char* pSql = pStr;
1,664,461,485✔
33

34
  int32_t index = 0;
1,664,461,485✔
35
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
1,664,467,046✔
36
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
1,664,470,597✔
37
    return false;
558,687,983✔
38
  }
39

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

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

62
  const char* pSql = pStr;
6,520,175✔
63

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

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

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

103
  const char* pSql = pStr;
2,915✔
104

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

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

123
  return false;
×
124
}
125

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

357
bool qIsCreateTbFromFileSql(const char* pStr, size_t length) {
280,020,545✔
358
  if (NULL == pStr) {
280,020,545✔
359
    return false;
×
360
  }
361

362
  const char* pSql = pStr;
280,020,545✔
363

364
  int32_t index = 0;
280,020,545✔
365
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
280,021,640✔
366
  if (TK_CREATE != t.type) {
280,029,156✔
367
    return false;
237,635,540✔
368
  }
369

370
  do {
371
    pStr += index;
1,713,007,078✔
372
    index = 0;
1,713,013,095✔
373
    t = tStrGetToken((char*)pStr, &index, false, NULL);
1,713,013,095✔
374
    if (TK_FILE == t.type) {
1,713,003,183✔
375
      return true;
2,644✔
376
    }
377
    if (0 == t.type || 0 == t.n) {
1,713,000,539✔
378
      break;
379
    }
380
  } while (pStr - pSql < length);
1,670,625,634✔
381
  return false;
42,387,077✔
382
}
383

384
bool qParseDbName(const char* pStr, size_t length, char** pDbName) {
6,522,790✔
385
  (void)length;
386
  int32_t index = 0;
6,522,790✔
387
  SToken  t;
388

389
  if (NULL == pStr) {
6,524,694✔
390
    *pDbName = NULL;
×
391
    return false;
×
392
  }
393

394
  t = tStrGetToken((char*)pStr, &index, false, NULL);
6,524,694✔
395
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
6,527,876✔
396
    *pDbName = NULL;
10,314✔
397
    return false;
10,314✔
398
  }
399

400
  t = tStrGetToken((char*)pStr, &index, false, NULL);
6,517,562✔
401
  if (TK_INTO != t.type) {
6,515,319✔
402
    *pDbName = NULL;
×
403
    return false;
×
404
  }
405

406
  t = tStrGetToken((char*)pStr, &index, false, NULL);
6,515,319✔
407
  if (t.n == 0 || t.z == NULL) {
6,517,562✔
UNCOV
408
    *pDbName = NULL;
×
409
    return false;
×
410
  }
411
  char* dotPos = strnchr(t.z, '.', t.n, true);
6,517,562✔
412
  if (dotPos != NULL) {
6,512,077✔
413
    int dbNameLen = dotPos - t.z;
110,381✔
414
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
110,381✔
415
    if (*pDbName == NULL) {
110,115✔
416
      return false;
×
417
    }
418
    strncpy(*pDbName, t.z, dbNameLen);
110,115✔
419
    (*pDbName)[dbNameLen] = '\0';
110,115✔
420
    return true;
110,115✔
421
  }
422
  return false;
6,401,696✔
423
}
424

425
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
270,780,292✔
426
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
270,780,292✔
427

428
  if (pCxt->parseOnly) {
270,782,979✔
429
    return code;
271,858✔
430
  }
431

432
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
270,512,389✔
433
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
9,176✔
434
    return TSDB_CODE_SUCCESS;
9,176✔
435
  }
436

437
  if (TSDB_CODE_SUCCESS == code) {
270,502,561✔
438
    code = translate(pCxt, pQuery, pMetaCache);
270,306,360✔
439
  }
440
  if (TSDB_CODE_SUCCESS == code) {
270,491,545✔
441
    code = calculateConstant(pCxt, pQuery);
216,408,772✔
442
  }
443
  return code;
270,475,651✔
444
}
445

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

454
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
280,019,994✔
455
  int32_t code = parse(pCxt, pQuery);
280,019,994✔
456
  if (TSDB_CODE_SUCCESS == code) {
280,006,372✔
457
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
270,761,015✔
458
  }
459
  return code;
280,024,452✔
460
}
461

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

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

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

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

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

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

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

552
static void rewriteExprAlias(SNode* pRoot) {
9,176✔
553
  int32_t no = 1;
9,176✔
554
  rewriteQueryExprAlias(pRoot, &no);
9,176✔
555
}
9,176✔
556

557
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
6,514,972✔
558
  int32_t code = TSDB_CODE_SUCCESS;
6,514,972✔
559
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
6,514,972✔
560
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
6,515,420✔
561
  } else {
562
    code = parseSqlIntoAst(pCxt, pQuery);
9,176✔
563
  }
564
  terrno = code;
6,510,462✔
565
  return code;
6,520,751✔
566
}
567

568
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
280,019,226✔
569
  SParseMetaCache metaCache = {0};
280,019,226✔
570
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
280,020,498✔
571
  if (TSDB_CODE_SUCCESS == code) {
280,021,254✔
572
    code = buildCatalogReq(&metaCache, pCatalogReq);
270,774,872✔
573
  }
574
  destoryParseMetaCache(&metaCache, true);
280,024,133✔
575
  return code;
280,012,044✔
576
}
577

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

581
  return continueCreateTbFromFile(pCxt, pQuery);
1,322✔
582
}
583

584
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
822,899,818✔
585
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
822,899,818✔
586
  if (TSDB_CODE_SUCCESS == code) {
822,900,876✔
587
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
822,899,978✔
588
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
542,882,505✔
589
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
280,027,265✔
590
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
2,644✔
591
    } else {
592
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
280,022,065✔
593
    }
594
  }
595
  (void)nodesReleaseAllocator(pCxt->allocatorId);
822,893,802✔
596
  terrno = code;
822,902,714✔
597
  return code;
822,901,275✔
598
}
599

600
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
270,778,379✔
601
                            struct SMetaData* pMetaData, SQuery* pQuery) {
602
  SParseMetaCache metaCache = {0};
270,778,379✔
603
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
270,778,876✔
604
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
270,776,370✔
605
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
270,776,370✔
606
  }
607
  if (TSDB_CODE_SUCCESS == code) {
270,777,901✔
608
    code = analyseSemantic(pCxt, pQuery, &metaCache);
270,777,906✔
609
  }
610
  (void)nodesReleaseAllocator(pCxt->allocatorId);
270,750,241✔
611
  destoryParseMetaCache(&metaCache, false);
270,761,420✔
612
  terrno = code;
270,754,822✔
613
  return code;
270,762,917✔
614
}
615

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

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

628
  return code;
×
629
}
630

631
static void destoryTablesReq(void* p) {
1,003,075,931✔
632
  STablesReq* pRes = (STablesReq*)p;
1,003,075,931✔
633
  taosArrayDestroy(pRes->pTables);
1,003,075,931✔
634
}
1,003,061,749✔
635

636
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
823,168,931✔
637
  if (NULL == pCatalogReq) {
823,168,931✔
638
    return;
271,858✔
639
  }
640
  taosArrayDestroy(pCatalogReq->pDbVgroup);
822,897,073✔
641
  taosArrayDestroy(pCatalogReq->pDbCfg);
822,897,385✔
642
  taosArrayDestroy(pCatalogReq->pDbInfo);
822,896,444✔
643
  if (pCatalogReq->cloned) {
822,895,070✔
644
    taosArrayDestroy(pCatalogReq->pTableMeta);
×
645
    taosArrayDestroy(pCatalogReq->pTableHash);
×
646
#ifdef TD_ENTERPRISE
647
    taosArrayDestroy(pCatalogReq->pView);
×
648
#endif
649
    taosArrayDestroy(pCatalogReq->pTableTSMAs);
×
650
    taosArrayDestroy(pCatalogReq->pTSMAs);
×
651
    taosArrayDestroy(pCatalogReq->pTableName);
×
652
  } else {
653
    taosArrayDestroyEx(pCatalogReq->pTableMeta, destoryTablesReq);
822,896,048✔
654
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
822,896,876✔
655
#ifdef TD_ENTERPRISE
656
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
822,895,806✔
657
#endif
658
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
822,895,542✔
659
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
822,891,038✔
660
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
822,892,982✔
661
  }
662
  taosArrayDestroy(pCatalogReq->pUdf);
822,891,075✔
663
  taosArrayDestroy(pCatalogReq->pIndex);
822,893,136✔
664
  taosArrayDestroy(pCatalogReq->pUser);
822,893,929✔
665
  taosArrayDestroy(pCatalogReq->pTableIndex);
822,889,249✔
666
  taosArrayDestroy(pCatalogReq->pTableCfg);
822,893,340✔
667
  taosArrayDestroy(pCatalogReq->pTableTag);
822,891,134✔
668
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
822,891,633✔
669
}
670

671
void tfreeSParseQueryRes(void* p) {
271,858✔
672
  if (NULL == p) {
271,858✔
673
    return;
×
674
  }
675

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

682
void qDestroyParseContext(SParseContext* pCxt) {
822,891,120✔
683
  if (NULL == pCxt) {
822,891,120✔
684
    return;
×
685
  }
686

687
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
822,891,120✔
688
  taosArrayDestroy(pCxt->pTableMetaPos);
822,897,611✔
689
  taosArrayDestroy(pCxt->pTableVgroupPos);
822,897,983✔
690
  taosMemoryFree(pCxt);
822,898,351✔
691
}
692

693
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
904,459,579✔
694

695
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
94,819✔
696
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
94,819✔
697
}
698

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

711
int32_t qInitKeywordsTable() { return taosInitKeywordsTable(); }
1,616,501✔
712

713
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
1,616,546✔
714

715
int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx, void *charsetCxt) {
9,168✔
716
  int32_t code = TSDB_CODE_SUCCESS;
9,168✔
717

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

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

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

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

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

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

805
      int32_t output = 0;
×
806
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
807
                         &output, charsetCxt)) {
808
        return terrno;
×
809
      }
810
      varDataSetLen(pVal->datum.p, output);
×
811
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
812
      break;
×
813
    }
814
    case TSDB_DATA_TYPE_BLOB:
×
815
    case TSDB_DATA_TYPE_MEDIUMBLOB:
816
      return TSDB_CODE_BLOB_NOT_SUPPORT;  // BLOB data type is not supported in stmt2
×
817
    default: {
16✔
818
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
16✔
819
      if (code) {
16✔
820
        return code;
×
821
      }
822
      break;
16✔
823
    }
824
  }
825
  pVal->translate = true;
16✔
826
  return TSDB_CODE_SUCCESS;
16✔
827
}
828

829
int32_t qStmtBindParams2(SQuery* pQuery, TAOS_STMT2_BIND* pParams, int32_t colIdx, void* charsetCxt) {
8✔
830
  int32_t code = TSDB_CODE_SUCCESS;
8✔
831

832
  if (colIdx < 0) {
8✔
833
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
8✔
834
    for (int32_t i = 0; i < size; ++i) {
24✔
835
      code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
16✔
836
      if (TSDB_CODE_SUCCESS != code) {
16✔
837
        return code;
×
838
      }
839
    }
840
  } else {
841
    code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
×
842
  }
843

844
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
8✔
845
    nodesDestroyNode(pQuery->pRoot);
8✔
846
    pQuery->pRoot = NULL;
8✔
847
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
8✔
848
  }
849
  if (TSDB_CODE_SUCCESS == code) {
8✔
850
    rewriteExprAlias(pQuery->pRoot);
8✔
851
  }
852
  return code;
8✔
853
}
854

855
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery) {
9,176✔
856
  int32_t code = translate(pCxt, pQuery, NULL);
9,176✔
857
  if (TSDB_CODE_SUCCESS == code) {
9,176✔
858
    code = calculateConstant(pCxt, pQuery);
9,176✔
859
  }
860
  return code;
9,176✔
861
}
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