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

taosdata / TDengine / #4881

14 Dec 2025 03:48AM UTC coverage: 60.617% (+0.5%) from 60.092%
#4881

push

travis-ci

web-flow
test: update coverage workflow time (#33918)

156854 of 258761 relevant lines covered (60.62%)

75258957.81 hits per line

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

49.91
/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) {
691,416,907✔
27
  if (NULL == pStr) {
691,416,907✔
28
    return false;
×
29
  }
30

31
  const char* pSql = pStr;
691,416,907✔
32

33
  int32_t index = 0;
691,416,907✔
34
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
691,419,126✔
35
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
691,415,952✔
36
    return false;
197,529,120✔
37
  }
38

39
  do {
40
    pStr += index;
2,041,295,381✔
41
    index = 0;
2,041,295,607✔
42
    t = tStrGetToken((char*)pStr, &index, false, NULL);
2,041,295,607✔
43
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
2,041,297,981✔
44
      return true;
493,780,670✔
45
    } else if (TK_SELECT == t.type) {
1,547,517,311✔
46
      return false;
108,154✔
47
    }
48
    if (0 == t.type || 0 == t.n) {
1,547,409,157✔
49
      break;
50
    }
51
  } while (pStr - pSql < length);
1,547,409,069✔
52
  return false;
608✔
53
}
54

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

61
  const char* pSql = pStr;
118,431✔
62

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

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

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

102
  const char* pSql = pStr;
10,109✔
103

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

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

122
  return false;
×
123
}
124

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

361
  const char* pSql = pStr;
99,004,526✔
362

363
  int32_t index = 0;
99,004,526✔
364
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
99,005,871✔
365
  if (TK_CREATE != t.type) {
99,008,433✔
366
    return false;
76,029,548✔
367
  }
368

369
  do {
370
    pStr += index;
442,606,927✔
371
    index = 0;
442,609,269✔
372
    t = tStrGetToken((char*)pStr, &index, false, NULL);
442,609,269✔
373
    if (TK_FILE == t.type) {
442,607,149✔
374
      return true;
1,944✔
375
    }
376
    if (0 == t.type || 0 == t.n) {
442,605,205✔
377
      break;
378
    }
379
  } while (pStr - pSql < length);
419,632,803✔
380
  return false;
22,977,163✔
381
}
382

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

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

393
  t = tStrGetToken((char*)pStr, &index, false, NULL);
128,577✔
394
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
128,728✔
395
    *pDbName = NULL;
7,588✔
396
    return false;
7,588✔
397
  }
398

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

405
  t = tStrGetToken((char*)pStr, &index, false, NULL);
121,140✔
406
  if (t.n == 0 || t.z == NULL) {
121,140✔
407
    *pDbName = NULL;
×
408
    return false;
×
409
  }
410
  char* dotPos = strnchr(t.z, '.', t.n, true);
121,140✔
411
  if (dotPos != NULL) {
120,804✔
412
    int dbNameLen = dotPos - t.z;
92,459✔
413
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
92,459✔
414
    if (*pDbName == NULL) {
92,768✔
415
      return false;
×
416
    }
417
    strncpy(*pDbName, t.z, dbNameLen);
92,768✔
418
    (*pDbName)[dbNameLen] = '\0';
92,617✔
419
    return true;
92,617✔
420
  }
421
  return false;
28,366✔
422
}
423

424
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
96,640,965✔
425
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
96,640,965✔
426

427
  if (pCxt->parseOnly) {
96,640,378✔
428
    return code;
154,088✔
429
  }
430

431
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
96,486,999✔
432
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
6,781✔
433
    return TSDB_CODE_SUCCESS;
6,781✔
434
  }
435

436
  if (TSDB_CODE_SUCCESS == code) {
96,480,748✔
437
    code = translate(pCxt, pQuery, pMetaCache);
96,341,226✔
438
  }
439
  if (TSDB_CODE_SUCCESS == code) {
96,478,769✔
440
    code = calculateConstant(pCxt, pQuery);
87,843,740✔
441
  }
442
  return code;
96,465,074✔
443
}
444

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

453
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
99,004,088✔
454
  int32_t code = parse(pCxt, pQuery);
99,004,088✔
455
  if (TSDB_CODE_SUCCESS == code) {
98,994,769✔
456
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
96,629,417✔
457
  }
458
  return code;
99,004,360✔
459
}
460

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

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

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

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

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

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

556
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
120,219✔
557
  int32_t code = TSDB_CODE_SUCCESS;
120,219✔
558
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
120,219✔
559
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
113,598✔
560
  } else {
561
    code = parseSqlIntoAst(pCxt, pQuery);
6,781✔
562
  }
563
  terrno = code;
119,906✔
564
  return code;
120,379✔
565
}
566

567
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
99,004,287✔
568
  SParseMetaCache metaCache = {0};
99,004,287✔
569
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
99,004,788✔
570
  if (TSDB_CODE_SUCCESS == code) {
99,002,358✔
571
    code = buildCatalogReq(&metaCache, pCatalogReq);
96,635,037✔
572
  }
573
  destoryParseMetaCache(&metaCache, true);
99,003,984✔
574
  return code;
98,997,465✔
575
}
576

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

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

583
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
345,705,915✔
584
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
345,705,915✔
585
  if (TSDB_CODE_SUCCESS == code) {
345,703,999✔
586
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
345,703,999✔
587
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
246,701,852✔
588
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
99,008,150✔
589
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
1,944✔
590
    } else {
591
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
99,006,097✔
592
    }
593
  }
594
  (void)nodesReleaseAllocator(pCxt->allocatorId);
345,698,262✔
595
  terrno = code;
345,706,345✔
596
  return code;
345,705,911✔
597
}
598

599
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
96,637,833✔
600
                            struct SMetaData* pMetaData, SQuery* pQuery) {
601
  SParseMetaCache metaCache = {0};
96,637,833✔
602
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
96,638,100✔
603
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
96,637,338✔
604
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
96,637,338✔
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
96,637,071✔
607
    code = analyseSemantic(pCxt, pQuery, &metaCache);
96,637,419✔
608
  }
609
  (void)nodesReleaseAllocator(pCxt->allocatorId);
96,622,739✔
610
  destoryParseMetaCache(&metaCache, false);
96,632,576✔
611
  terrno = code;
96,631,495✔
612
  return code;
96,629,918✔
613
}
614

615
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
5,554,624✔
616
                          SQuery* pQuery) {
617
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
5,554,624✔
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) {
348,835,138✔
631
  STablesReq* pRes = (STablesReq*)p;
348,835,138✔
632
  taosArrayDestroy(pRes->pTables);
348,835,138✔
633
}
348,824,518✔
634

635
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
345,852,514✔
636
  if (NULL == pCatalogReq) {
345,852,514✔
637
    return;
154,088✔
638
  }
639
  taosArrayDestroy(pCatalogReq->pDbVgroup);
345,698,426✔
640
  taosArrayDestroy(pCatalogReq->pDbCfg);
345,699,566✔
641
  taosArrayDestroy(pCatalogReq->pDbInfo);
345,698,361✔
642
  if (pCatalogReq->cloned) {
345,698,106✔
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);
345,696,981✔
653
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
345,695,472✔
654
#ifdef TD_ENTERPRISE
655
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
345,696,798✔
656
#endif
657
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
345,697,290✔
658
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
345,694,628✔
659
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
345,696,271✔
660
  }
661
  taosArrayDestroy(pCatalogReq->pUdf);
345,695,348✔
662
  taosArrayDestroy(pCatalogReq->pIndex);
345,697,656✔
663
  taosArrayDestroy(pCatalogReq->pUser);
345,699,173✔
664
  taosArrayDestroy(pCatalogReq->pTableIndex);
345,691,314✔
665
  taosArrayDestroy(pCatalogReq->pTableCfg);
345,695,731✔
666
  taosArrayDestroy(pCatalogReq->pTableTag);
345,696,389✔
667
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
345,693,128✔
668
}
669

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

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

681
void qDestroyParseContext(SParseContext* pCxt) {
345,695,672✔
682
  if (NULL == pCxt) {
345,695,672✔
683
    return;
×
684
  }
685

686
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
345,695,672✔
687
  taosArrayDestroy(pCxt->pTableMetaPos);
345,699,558✔
688
  taosArrayDestroy(pCxt->pTableVgroupPos);
345,700,682✔
689
  taosMemoryFree(pCxt);
345,700,561✔
690
}
691

692
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
359,859,487✔
693

694
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
9,795✔
695
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
9,795✔
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(); }
736,014✔
711

712
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
736,186✔
713

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

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

729
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
6,760✔
730
    nodesDestroyNode(pQuery->pRoot);
6,760✔
731
    pQuery->pRoot = NULL;
6,760✔
732
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
6,760✔
733
  }
734
  if (TSDB_CODE_SUCCESS == code) {
6,760✔
735
    rewriteExprAlias(pQuery->pRoot);
6,760✔
736
  }
737
  return code;
6,760✔
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:
×
769
    case TSDB_DATA_TYPE_GEOMETRY:
770
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
771
      if (NULL == pVal->datum.p) {
×
772
        return terrno;
×
773
      }
774
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
775
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
×
776
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
777
      break;
×
778
    case TSDB_DATA_TYPE_NCHAR: {
×
779
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
×
780
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
781
      if (NULL == pVal->datum.p) {
×
782
        return terrno;
×
783
      }
784

785
      int32_t output = 0;
×
786
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
787
                         &output, charsetCxt)) {
788
        return terrno;
×
789
      }
790
      varDataSetLen(pVal->datum.p, output);
×
791
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
792
      break;
×
793
    }
794
    case TSDB_DATA_TYPE_BLOB:
×
795
    case TSDB_DATA_TYPE_MEDIUMBLOB:
796
      return TSDB_CODE_BLOB_NOT_SUPPORT;  // BLOB data type is not supported in stmt2
×
797
    default: {
42✔
798
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
42✔
799
      if (code) {
42✔
800
        return code;
×
801
      }
802
      break;
42✔
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) {
21✔
810
  int32_t code = TSDB_CODE_SUCCESS;
21✔
811

812
  if (colIdx < 0) {
21✔
813
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
21✔
814
    for (int32_t i = 0; i < size; ++i) {
63✔
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)) {
21✔
825
    nodesDestroyNode(pQuery->pRoot);
21✔
826
    pQuery->pRoot = NULL;
21✔
827
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
21✔
828
  }
829
  if (TSDB_CODE_SUCCESS == code) {
21✔
830
    rewriteExprAlias(pQuery->pRoot);
21✔
831
  }
832
  return code;
21✔
833
}
834

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