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

taosdata / TDengine / #4822

27 Oct 2025 05:42AM UTC coverage: 59.732% (+1.0%) from 58.728%
#4822

push

travis-ci

web-flow
Merge pull request #33377 from taosdata/fix/main/rename-udf-path

fix: update UDF example links to correct file paths

121214 of 258518 branches covered (46.89%)

Branch coverage included in aggregate %.

193636 of 268583 relevant lines covered (72.1%)

4002399.5 hits per line

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

69.57
/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) {
292,736✔
27
  if (NULL == pStr) {
292,736✔
28
    return false;
1✔
29
  }
30

31
  const char* pSql = pStr;
292,735✔
32

33
  int32_t index = 0;
292,735✔
34
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
292,735✔
35
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
292,796✔
36
    return false;
122,410✔
37
  }
38

39
  do {
40
    pStr += index;
553,477✔
41
    index = 0;
553,477✔
42
    t = tStrGetToken((char*)pStr, &index, false, NULL);
553,477✔
43
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
553,426✔
44
      return true;
170,286✔
45
    } else if (TK_SELECT == t.type) {
383,140✔
46
      return false;
41✔
47
    }
48
    if (0 == t.type || 0 == t.n) {
383,099!
49
      break;
50
    }
51
  } while (pStr - pSql < length);
383,099✔
52
  return false;
8✔
53
}
54

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

61
  const char* pSql = pStr;
10,992✔
62

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

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

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

102
  const char* pSql = pStr;
19✔
103

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

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

122
  return false;
×
123
}
124

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

361
  const char* pSql = pStr;
58,490✔
362

363
  int32_t index = 0;
58,490✔
364
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
58,490✔
365
  if (TK_CREATE != t.type) {
58,506✔
366
    return false;
33,311✔
367
  }
368

369
  do {
370
    pStr += index;
491,966✔
371
    index = 0;
491,966✔
372
    t = tStrGetToken((char*)pStr, &index, false, NULL);
491,966✔
373
    if (TK_FILE == t.type) {
491,965!
374
      return true;
×
375
    }
376
    if (0 == t.type || 0 == t.n) {
491,965✔
377
      break;
378
    }
379
  } while (pStr - pSql < length);
466,782✔
380
  return false;
25,194✔
381
}
382

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

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

393
  t = tStrGetToken((char*)pStr, &index, false, NULL);
11,183✔
394
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
11,200!
395
    *pDbName = NULL;
12✔
396
    return false;
12✔
397
  }
398

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

405
  t = tStrGetToken((char*)pStr, &index, false, NULL);
11,188✔
406
  if (t.n == 0 || t.z == NULL) {
11,187!
407
    *pDbName = NULL;
×
408
    return false;
×
409
  }
410
  char* dotPos = strnchr(t.z, '.', t.n, true);
11,187✔
411
  if (dotPos != NULL) {
11,184✔
412
    int dbNameLen = dotPos - t.z;
989✔
413
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
989!
414
    if (*pDbName == NULL) {
978!
415
      return false;
×
416
    }
417
    strncpy(*pDbName, t.z, dbNameLen);
978✔
418
    (*pDbName)[dbNameLen] = '\0';
978✔
419
    return true;
978✔
420
  }
421
  return false;
10,195✔
422
}
423

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

427
  if (pCxt->parseOnly) {
60,912✔
428
    return code;
1✔
429
  }
430

431
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
60,911!
432
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
28✔
433
    return TSDB_CODE_SUCCESS;
28✔
434
  }
435

436
  if (TSDB_CODE_SUCCESS == code) {
60,883!
437
    code = translate(pCxt, pQuery, pMetaCache);
60,888✔
438
  }
439
  if (TSDB_CODE_SUCCESS == code) {
60,871✔
440
    code = calculateConstant(pCxt, pQuery);
58,630✔
441
  }
442
  return code;
60,881✔
443
}
444

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

453
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
58,486✔
454
  int32_t code = parse(pCxt, pQuery);
58,486✔
455
  if (TSDB_CODE_SUCCESS == code) {
58,470✔
456
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
58,360✔
457
  }
458
  return code;
58,475✔
459
}
460

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

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

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

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

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

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

556
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
13,781✔
557
  int32_t code = TSDB_CODE_SUCCESS;
13,781✔
558
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
13,781✔
559
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
11,182✔
560
  } else {
561
    code = parseSqlIntoAst(pCxt, pQuery);
2,613✔
562
  }
563
  terrno = code;
13,782✔
564
  return code;
13,787✔
565
}
566

567
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
58,487✔
568
  SParseMetaCache metaCache = {0};
58,487✔
569
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
58,487✔
570
  if (TSDB_CODE_SUCCESS == code) {
58,470✔
571
    code = buildCatalogReq(&metaCache, pCatalogReq);
58,364✔
572
  }
573
  destoryParseMetaCache(&metaCache, true);
58,483✔
574
  return code;
58,409✔
575
}
576

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

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

583
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
126,661✔
584
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
126,661✔
585
  if (TSDB_CODE_SUCCESS == code) {
126,659!
586
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
126,665✔
587
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
68,200✔
588
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
58,503!
589
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
×
590
    } else {
591
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
58,505✔
592
    }
593
  }
594
  (void)nodesReleaseAllocator(pCxt->allocatorId);
126,590✔
595
  terrno = code;
126,635✔
596
  return code;
126,652✔
597
}
598

599
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
58,389✔
600
                            struct SMetaData* pMetaData, SQuery* pQuery) {
601
  SParseMetaCache metaCache = {0};
58,389✔
602
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
58,389✔
603
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
58,382!
604
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
58,383✔
605
  }
606
  if (TSDB_CODE_SUCCESS == code) {
58,384!
607
    code = analyseSemantic(pCxt, pQuery, &metaCache);
58,385✔
608
  }
609
  (void)nodesReleaseAllocator(pCxt->allocatorId);
58,356✔
610
  destoryParseMetaCache(&metaCache, false);
58,382✔
611
  terrno = code;
58,367✔
612
  return code;
58,355✔
613
}
614

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

635
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
125,328✔
636
  if (NULL == pCatalogReq) {
125,328✔
637
    return;
21✔
638
  }
639
  taosArrayDestroy(pCatalogReq->pDbVgroup);
125,307✔
640
  taosArrayDestroy(pCatalogReq->pDbCfg);
125,307✔
641
  taosArrayDestroy(pCatalogReq->pDbInfo);
125,323✔
642
  if (pCatalogReq->cloned) {
125,329!
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);
125,329✔
653
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
125,303✔
654
#ifdef TD_ENTERPRISE
655
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
125,325✔
656
#endif
657
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
125,334✔
658
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
125,337✔
659
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
125,338✔
660
  }
661
  taosArrayDestroy(pCatalogReq->pUdf);
125,336✔
662
  taosArrayDestroy(pCatalogReq->pIndex);
125,336✔
663
  taosArrayDestroy(pCatalogReq->pUser);
125,333✔
664
  taosArrayDestroy(pCatalogReq->pTableIndex);
125,322✔
665
  taosArrayDestroy(pCatalogReq->pTableCfg);
125,332✔
666
  taosArrayDestroy(pCatalogReq->pTableTag);
125,327✔
667
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
125,330✔
668
}
669

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

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

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

686
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
125,316✔
687
  taosArrayDestroy(pCxt->pTableMetaPos);
125,358✔
688
  taosArrayDestroy(pCxt->pTableVgroupPos);
125,361✔
689
  taosMemoryFree(pCxt);
125,357!
690
}
691

692
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
167,538✔
693

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

712
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
3,582✔
713

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

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

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

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

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

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

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

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

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