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

taosdata / TDengine / #4829

30 Oct 2025 09:25AM UTC coverage: 49.734% (-11.3%) from 61.071%
#4829

push

travis-ci

web-flow
Merge pull request #33435 from taosdata/3.0

merge 3.0

123072 of 323930 branches covered (37.99%)

Branch coverage included in aggregate %.

7 of 25 new or added lines in 3 files covered. (28.0%)

35232 existing lines in 327 files now uncovered.

172062 of 269495 relevant lines covered (63.85%)

70709785.06 hits per line

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

32.51
/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) {
358,739,552✔
27
  if (NULL == pStr) {
358,739,552!
UNCOV
28
    return false;
×
29
  }
30

31
  const char* pSql = pStr;
358,739,552✔
32

33
  int32_t index = 0;
358,739,552✔
34
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
358,744,050✔
35
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
358,736,093✔
36
    return false;
89,929,565✔
37
  }
38

39
  do {
40
    pStr += index;
1,646,978,470✔
41
    index = 0;
1,646,977,859✔
42
    t = tStrGetToken((char*)pStr, &index, false, NULL);
1,646,977,859✔
43
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
1,646,985,838✔
44
      return true;
268,799,260✔
45
    } else if (TK_SELECT == t.type) {
1,378,186,578✔
46
      return false;
12,448✔
47
    }
48
    if (0 == t.type || 0 == t.n) {
1,378,174,130✔
49
      break;
50
    }
51
  } while (pStr - pSql < length);
1,378,172,689✔
52
  return false;
2,188✔
53
}
54

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

61
  const char* pSql = pStr;
65,792✔
62

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

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

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

UNCOV
102
  const char* pSql = pStr;
×
103

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

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

122
  return false;
×
123
}
124

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

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

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

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

163
  // UPDATE
UNCOV
164
  t = tStrGetToken((char*)pSql, &index, false, NULL);
×
UNCOV
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
  }
UNCOV
170
  pSql += index;
×
171

172
  // tbname
UNCOV
173
  index = 0;
×
UNCOV
174
  t = tStrGetToken((char*)pSql, &index, false, NULL);
×
UNCOV
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

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

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

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

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

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

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

UNCOV
228
    index = 0;
×
UNCOV
229
    t = tStrGetToken((char*)pSql, &index, false, NULL);
×
UNCOV
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
    }
UNCOV
235
    pSql += index;
×
236

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

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

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

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

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

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

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

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

307
      // where cols muset be pk, ignore others
UNCOV
308
      int32_t colId = -1;
×
UNCOV
309
      if (t.type == TK_NK_QUESTION) {
×
UNCOV
310
        if (pTableMeta != NULL && isColumnPrimaryKey(pTableMeta, colName, colNameLen, &colId)) {
×
UNCOV
311
          if (!firstColumn) {
×
UNCOV
312
            *p++ = ',';
×
313
          }
UNCOV
314
          memcpy(p, colName, colNameLen);
×
UNCOV
315
          p += colNameLen;
×
UNCOV
316
          firstColumn = false;
×
UNCOV
317
          columnCount++;
×
318
        } else {
UNCOV
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
        }
UNCOV
325
        numOfCols++;
×
326
      }
UNCOV
327
      pSql += index;
×
328

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

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

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

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

361
  const char* pSql = pStr;
45,046,516✔
362

363
  int32_t index = 0;
45,046,516✔
364
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
45,047,456✔
365
  if (TK_CREATE != t.type) {
45,050,109✔
366
    return false;
32,546,967✔
367
  }
368

369
  do {
370
    pStr += index;
213,720,936✔
371
    index = 0;
213,722,829✔
372
    t = tStrGetToken((char*)pStr, &index, false, NULL);
213,722,829✔
373
    if (TK_FILE == t.type) {
213,720,286!
UNCOV
374
      return true;
×
375
    }
376
    if (0 == t.type || 0 == t.n) {
213,720,286✔
377
      break;
378
    }
379
  } while (pStr - pSql < length);
201,221,451✔
380
  return false;
12,502,492✔
381
}
382

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

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

393
  t = tStrGetToken((char*)pStr, &index, false, NULL);
70,156✔
394
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
70,668!
395
    *pDbName = NULL;
490✔
396
    return false;
490✔
397
  }
398

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

405
  t = tStrGetToken((char*)pStr, &index, false, NULL);
69,922✔
406
  if (t.n == 0 || t.z == NULL) {
70,178!
UNCOV
407
    *pDbName = NULL;
×
UNCOV
408
    return false;
×
409
  }
410
  char* dotPos = strnchr(t.z, '.', t.n, true);
70,178✔
411
  if (dotPos != NULL) {
69,922✔
412
    int dbNameLen = dotPos - t.z;
60,752✔
413
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
60,752!
414
    if (*pDbName == NULL) {
61,008!
415
      return false;
×
416
    }
417
    strncpy(*pDbName, t.z, dbNameLen);
61,008!
418
    (*pDbName)[dbNameLen] = '\0';
60,752✔
419
    return true;
60,752✔
420
  }
421
  return false;
9,170✔
422
}
423

424
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
44,553,458✔
425
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
44,553,458✔
426

427
  if (pCxt->parseOnly) {
44,546,387!
428
    return code;
655✔
429
  }
430

431
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
44,546,066✔
432
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
490✔
433
    return TSDB_CODE_SUCCESS;
490✔
434
  }
435

436
  if (TSDB_CODE_SUCCESS == code) {
44,545,901✔
437
    code = translate(pCxt, pQuery, pMetaCache);
44,481,098✔
438
  }
439
  if (TSDB_CODE_SUCCESS == code) {
44,543,944✔
440
    code = calculateConstant(pCxt, pQuery);
41,961,948✔
441
  }
442
  return code;
44,535,219✔
443
}
444

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

453
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
45,046,797✔
454
  int32_t code = parse(pCxt, pQuery);
45,046,797✔
455
  if (TSDB_CODE_SUCCESS == code) {
45,043,628✔
456
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
44,549,889✔
457
  }
458
  return code;
45,049,778✔
459
}
460

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

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

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

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

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

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

556
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
70,436✔
557
  int32_t code = TSDB_CODE_SUCCESS;
70,436✔
558
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
70,436✔
559
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
70,202✔
560
  } else {
561
    code = parseSqlIntoAst(pCxt, pQuery);
490✔
562
  }
563
  terrno = code;
70,411✔
564
  return code;
70,692✔
565
}
566

567
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
45,047,122✔
568
  SParseMetaCache metaCache = {0};
45,047,122✔
569
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
45,048,115✔
570
  if (TSDB_CODE_SUCCESS == code) {
45,048,674✔
571
    code = buildCatalogReq(&metaCache, pCatalogReq);
44,554,646✔
572
  }
573
  destoryParseMetaCache(&metaCache, true);
45,046,855✔
574
  return code;
45,043,867✔
575
}
576

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

UNCOV
580
  return continueCreateTbFromFile(pCxt, pQuery);
×
581
}
582

583
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
179,330,035✔
584
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
179,330,035✔
585
  if (TSDB_CODE_SUCCESS == code) {
179,328,309✔
586
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
179,327,479✔
587
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
134,289,860✔
588
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
45,049,775!
UNCOV
589
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
×
590
    } else {
591
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
45,049,126✔
592
    }
593
  }
594
  (void)nodesReleaseAllocator(pCxt->allocatorId);
179,318,065✔
595
  terrno = code;
179,325,597✔
596
  return code;
179,330,938✔
597
}
598

599
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
44,554,746✔
600
                            struct SMetaData* pMetaData, SQuery* pQuery) {
601
  SParseMetaCache metaCache = {0};
44,554,746✔
602
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
44,554,746✔
603
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
44,555,106!
604
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
44,555,106✔
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
44,554,158!
607
    code = analyseSemantic(pCxt, pQuery, &metaCache);
44,554,158✔
608
  }
609
  (void)nodesReleaseAllocator(pCxt->allocatorId);
44,543,577✔
610
  destoryParseMetaCache(&metaCache, false);
44,549,809✔
611
  terrno = code;
44,546,335✔
612
  return code;
44,548,841✔
613
}
614

615
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
926,174✔
616
                          SQuery* pQuery) {
617
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
926,174✔
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) {
165,961,678✔
631
  STablesReq* pRes = (STablesReq*)p;
165,961,678✔
632
  taosArrayDestroy(pRes->pTables);
165,961,678✔
633
}
165,949,990✔
634

635
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
179,333,076✔
636
  if (NULL == pCatalogReq) {
179,333,076✔
637
    return;
655✔
638
  }
639
  taosArrayDestroy(pCatalogReq->pDbVgroup);
179,332,421✔
640
  taosArrayDestroy(pCatalogReq->pDbCfg);
179,333,539✔
641
  taosArrayDestroy(pCatalogReq->pDbInfo);
179,333,701✔
642
  if (pCatalogReq->cloned) {
179,332,925!
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);
179,331,492✔
653
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
179,326,951✔
654
#ifdef TD_ENTERPRISE
655
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
179,331,743✔
656
#endif
657
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
179,333,570✔
658
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
179,328,214✔
659
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
179,328,367✔
660
  }
661
  taosArrayDestroy(pCatalogReq->pUdf);
179,328,654✔
662
  taosArrayDestroy(pCatalogReq->pIndex);
179,331,715✔
663
  taosArrayDestroy(pCatalogReq->pUser);
179,332,154✔
664
  taosArrayDestroy(pCatalogReq->pTableIndex);
179,329,424✔
665
  taosArrayDestroy(pCatalogReq->pTableCfg);
179,331,145✔
666
  taosArrayDestroy(pCatalogReq->pTableTag);
179,328,090✔
667
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
179,326,654✔
668
}
669

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

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

681
void qDestroyParseContext(SParseContext* pCxt) {
179,330,800✔
682
  if (NULL == pCxt) {
179,330,800!
683
    return;
×
684
  }
685

686
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
179,330,800✔
687
  taosArrayDestroy(pCxt->pTableMetaPos);
179,336,585✔
688
  taosArrayDestroy(pCxt->pTableVgroupPos);
179,336,901✔
689
  taosMemoryFree(pCxt);
179,337,179!
690
}
691

692
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
185,119,203✔
693

694
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
12,106✔
695
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
12,106✔
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,416,768✔
711

712
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
1,416,768✔
713

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

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

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

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

UNCOV
748
  if (pParam->is_null && 1 == *(pParam->is_null)) {
×
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

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

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

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

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

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

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