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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

67.01
/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,438,592,110✔
27
  if (NULL == pStr) {
1,438,592,110✔
28
    return false;
2✔
29
  }
30

31
  const char* pSql = pStr;
1,438,592,108✔
32

33
  int32_t index = 0;
1,438,592,108✔
34
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
1,438,598,765✔
35
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
1,438,599,531✔
36
    return false;
411,363,718✔
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,027,035,357✔
45
    } else if (TK_SELECT == t.type) {
2,147,483,647✔
46
      return false;
200,762✔
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;
3,984✔
53
}
54

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

61
  const char* pSql = pStr;
946,520✔
62

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

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

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

102
  const char* pSql = pStr;
38✔
103

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

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

122
  return false;
×
123
}
124

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

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

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

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

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

172
  // tbname
173
  index = 0;
20✔
174
  t = tStrGetToken((char*)pSql, &index, false, NULL);
20✔
175
  if (t.n == 0 || t.z == NULL) {
20!
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 ");
20!
182
  memcpy(p, t.z, t.n);
20!
183
  p += t.n;
20✔
184
  p += sprintf(p, " (");
20!
185
  pSql += index;
20✔
186

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

356
bool qIsCreateTbFromFileSql(const char* pStr, size_t length) {
206,326,997✔
357
  if (NULL == pStr) {
206,326,997!
358
    return false;
×
359
  }
360

361
  const char* pSql = pStr;
206,326,997✔
362

363
  int32_t index = 0;
206,326,997✔
364
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
206,331,022✔
365
  if (TK_CREATE != t.type) {
206,336,647✔
366
    return false;
166,852,442✔
367
  }
368

369
  do {
370
    pStr += index;
846,522,025✔
371
    index = 0;
846,524,942✔
372
    t = tStrGetToken((char*)pStr, &index, false, NULL);
846,524,942✔
373
    if (TK_FILE == t.type) {
846,521,518✔
374
      return true;
1,764✔
375
    }
376
    if (0 == t.type || 0 == t.n) {
846,519,754✔
377
      break;
378
    }
379
  } while (pStr - pSql < length);
807,038,600✔
380
  return false;
39,481,934✔
381
}
382

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

388
  if (NULL == pStr) {
976,235!
389
    *pDbName = NULL;
×
390
    return false;
×
391
  }
392

393
  t = tStrGetToken((char*)pStr, &index, false, NULL);
976,235✔
394
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
976,066!
395
    *pDbName = NULL;
13,818✔
396
    return false;
13,818✔
397
  }
398

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

405
  t = tStrGetToken((char*)pStr, &index, false, NULL);
962,626✔
406
  if (t.n == 0 || t.z == NULL) {
962,675!
407
    *pDbName = NULL;
378✔
408
    return false;
×
409
  }
410
  char* dotPos = strnchr(t.z, '.', t.n, true);
962,297✔
411
  if (dotPos != NULL) {
962,170✔
412
    int dbNameLen = dotPos - t.z;
394,067✔
413
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
394,067!
414
    if (*pDbName == NULL) {
394,265!
415
      return false;
×
416
    }
417
    strncpy(*pDbName, t.z, dbNameLen);
394,265!
418
    (*pDbName)[dbNameLen] = '\0';
394,265✔
419
    return true;
394,265✔
420
  }
421
  return false;
568,103✔
422
}
423

424
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
200,372,586✔
425
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
200,372,586✔
426

427
  if (pCxt->parseOnly) {
200,371,194✔
428
    return code;
376,962✔
429
  }
430

431
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
199,994,691✔
432
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
12,390✔
433
    return TSDB_CODE_SUCCESS;
12,390✔
434
  }
435

436
  if (TSDB_CODE_SUCCESS == code) {
199,982,400✔
437
    code = translate(pCxt, pQuery, pMetaCache);
199,720,741✔
438
  }
439
  if (TSDB_CODE_SUCCESS == code) {
199,973,163✔
440
    code = calculateConstant(pCxt, pQuery);
177,296,840✔
441
  }
442
  return code;
199,950,147✔
443
}
444

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

453
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
206,323,888✔
454
  int32_t code = parse(pCxt, pQuery);
206,323,888✔
455
  if (TSDB_CODE_SUCCESS == code) {
206,320,814✔
456
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
200,344,770✔
457
  }
458
  return code;
206,326,535✔
459
}
460

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

469
  if (pParam->is_null && 1 == *(pParam->is_null)) {
13,141!
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);
13,141✔
476
  pVal->node.resType.type = pParam->buffer_type;
13,141✔
477
  pVal->node.resType.bytes = inputSize;
13,141✔
478

479
  switch (pParam->buffer_type) {
13,141!
480
    case TSDB_DATA_TYPE_VARBINARY:
249✔
481
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
249!
482
      if (NULL == pVal->datum.p) {
249!
483
        return terrno;
×
484
      }
485
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
249✔
486
      memcpy(varDataVal(pVal->datum.p), pParam->buffer, pVal->node.resType.bytes);
249!
487
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
249✔
488
      break;
249✔
489
    case TSDB_DATA_TYPE_VARCHAR:
4,693✔
490
    case TSDB_DATA_TYPE_GEOMETRY:
491
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
4,693!
492
      if (NULL == pVal->datum.p) {
4,693!
493
        return terrno;
×
494
      }
495
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
4,693✔
496
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
4,693!
497
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
4,693✔
498
      break;
4,693✔
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: {
8,199✔
516
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
8,199✔
517
      if (code) {
8,199!
518
        return code;
×
519
      }
520
      break;
8,199✔
521
    }
522
  }
523
  pVal->translate = true;
13,141✔
524
  return TSDB_CODE_SUCCESS;
13,141✔
525
}
526

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

535
static void rewriteQueryExprAlias(SNode* pRoot, int32_t* pNo) {
12,408✔
536
  switch (nodeType(pRoot)) {
12,408!
537
    case QUERY_NODE_SELECT_STMT:
12,408✔
538
      nodesWalkSelectStmt((SSelectStmt*)pRoot, SQL_CLAUSE_FROM, rewriteQueryExprAliasImpl, pNo);
12,408✔
539
      break;
12,408✔
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
}
12,408✔
550

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

556
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
965,426✔
557
  int32_t code = TSDB_CODE_SUCCESS;
965,426✔
558
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
965,426✔
559
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
945,771✔
560
  } else {
561
    code = parseSqlIntoAst(pCxt, pQuery);
20,998✔
562
  }
563
  terrno = code;
965,857✔
564
  return code;
966,609✔
565
}
566

567
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
206,325,628✔
568
  SParseMetaCache metaCache = {0};
206,325,628✔
569
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
206,327,692✔
570
  if (TSDB_CODE_SUCCESS == code) {
206,326,005✔
571
    code = buildCatalogReq(&metaCache, pCatalogReq);
200,352,176✔
572
  }
573
  destoryParseMetaCache(&metaCache, true);
206,326,756✔
574
  return code;
206,317,578✔
575
}
576

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

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

583
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
718,373,921✔
584
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
718,373,921✔
585
  if (TSDB_CODE_SUCCESS == code) {
718,375,161!
586
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
718,376,993✔
587
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
512,055,568✔
588
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
206,334,566✔
589
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
1,764✔
590
    } else {
591
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
206,330,365✔
592
    }
593
  }
594
  (void)nodesReleaseAllocator(pCxt->allocatorId);
718,356,728✔
595
  terrno = code;
718,376,744✔
596
  return code;
718,366,556✔
597
}
598

599
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
200,356,676✔
600
                            struct SMetaData* pMetaData, SQuery* pQuery) {
601
  SParseMetaCache metaCache = {0};
200,356,676✔
602
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
200,357,116✔
603
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
200,356,831!
604
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
200,356,917✔
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
200,355,796✔
607
    code = analyseSemantic(pCxt, pQuery, &metaCache);
200,353,215✔
608
  }
609
  (void)nodesReleaseAllocator(pCxt->allocatorId);
200,331,629✔
610
  destoryParseMetaCache(&metaCache, false);
200,353,898✔
611
  terrno = code;
200,336,632✔
612
  return code;
200,335,344✔
613
}
614

615
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
25,473,410✔
616
                          SQuery* pQuery) {
617
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
25,473,410✔
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) {
720,096,646✔
631
  STablesReq* pRes = (STablesReq*)p;
720,096,646✔
632
  taosArrayDestroy(pRes->pTables);
720,096,646✔
633
}
720,093,161✔
634

635
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
718,742,373✔
636
  if (NULL == pCatalogReq) {
718,742,373✔
637
    return;
377,002✔
638
  }
639
  taosArrayDestroy(pCatalogReq->pDbVgroup);
718,365,371✔
640
  taosArrayDestroy(pCatalogReq->pDbCfg);
718,364,764✔
641
  taosArrayDestroy(pCatalogReq->pDbInfo);
718,364,477✔
642
  if (pCatalogReq->cloned) {
718,368,602!
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);
718,366,365✔
653
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
718,358,771✔
654
#ifdef TD_ENTERPRISE
655
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
718,364,373✔
656
#endif
657
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
718,363,639✔
658
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
718,363,597✔
659
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
718,365,310✔
660
  }
661
  taosArrayDestroy(pCatalogReq->pUdf);
718,367,462✔
662
  taosArrayDestroy(pCatalogReq->pIndex);
718,364,486✔
663
  taosArrayDestroy(pCatalogReq->pUser);
718,359,442✔
664
  taosArrayDestroy(pCatalogReq->pTableIndex);
718,360,791✔
665
  taosArrayDestroy(pCatalogReq->pTableCfg);
718,362,145✔
666
  taosArrayDestroy(pCatalogReq->pTableTag);
718,366,199✔
667
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
718,368,242✔
668
}
669

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

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

681
void qDestroyParseContext(SParseContext* pCxt) {
718,361,853✔
682
  if (NULL == pCxt) {
718,361,853!
683
    return;
×
684
  }
685

686
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
718,361,853✔
687
  taosArrayDestroy(pCxt->pTableMetaPos);
718,367,008✔
688
  taosArrayDestroy(pCxt->pTableVgroupPos);
718,367,181✔
689
  taosMemoryFree(pCxt);
718,363,939!
690
}
691

692
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
761,526,919✔
693

694
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
40,142✔
695
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
40,142✔
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(); }
916,183✔
711

712
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
914,286✔
713

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

717
  if (colIdx < 0) {
12,382✔
718
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
12,342✔
719
    for (int32_t i = 0; i < size; ++i) {
25,443✔
720
      code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
13,101✔
721
      if (TSDB_CODE_SUCCESS != code) {
13,101!
722
        return code;
×
723
      }
724
    }
725
  } else {
726
    code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
40✔
727
  }
728

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

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

748
  if (pParam->is_null && 1 == *(pParam->is_null)) {
42!
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);
42!
755
  pVal->node.resType.type = pParam->buffer_type;
42✔
756
  pVal->node.resType.bytes = inputSize;
42✔
757

758
  switch (pParam->buffer_type) {
42!
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:
4✔
769
    case TSDB_DATA_TYPE_GEOMETRY:
770
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
4!
771
      if (NULL == pVal->datum.p) {
4!
772
        return terrno;
×
773
      }
774
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
4✔
775
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
4!
776
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
4✔
777
      break;
4✔
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: {
38✔
798
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
38✔
799
      if (code) {
38!
800
        return code;
×
801
      }
802
      break;
38✔
803
    }
804
  }
805
  pVal->translate = true;
42✔
806
  return TSDB_CODE_SUCCESS;
42✔
807
}
808

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

812
  if (colIdx < 0) {
26!
813
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
26✔
814
    for (int32_t i = 0; i < size; ++i) {
68✔
815
      code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
42✔
816
      if (TSDB_CODE_SUCCESS != code) {
42!
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)) {
26!
825
    nodesDestroyNode(pQuery->pRoot);
26✔
826
    pQuery->pRoot = NULL;
26✔
827
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
26✔
828
  }
829
  if (TSDB_CODE_SUCCESS == code) {
26!
830
    rewriteExprAlias(pQuery->pRoot);
26✔
831
  }
832
  return code;
26✔
833
}
834

835
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery) {
12,408✔
836
  int32_t code = translate(pCxt, pQuery, NULL);
12,408✔
837
  if (TSDB_CODE_SUCCESS == code) {
12,408✔
838
    code = calculateConstant(pCxt, pQuery);
12,404✔
839
  }
840
  return code;
12,408✔
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