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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

76.69
/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 "decimal.h"
22
#include "parInsertUtil.h"
23
#include "parInt.h"
24
#include "parToken.h"
25
#include "parUtil.h"
26
#include "tname.h"
27
#include "ttime.h"
28

29
bool qIsInsertValuesSql(const char* pStr, size_t length) {
15,737✔
30
  if (NULL == pStr) {
15,737✔
31
    return false;
×
32
  }
33

34
  const char* pSql = pStr;
15,737✔
35

36
  int32_t index = 0;
15,737✔
37
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
15,737✔
38
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
15,737✔
39
    return false;
11,966✔
40
  }
41

42
  do {
43
    pStr += index;
17,514✔
44
    index = 0;
17,514✔
45
    t = tStrGetToken((char*)pStr, &index, false, NULL);
17,514✔
46
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
17,514✔
47
      return true;
3,762✔
48
    } else if (TK_SELECT == t.type) {
13,752✔
49
      return false;
9✔
50
    }
51
    if (0 == t.type || 0 == t.n) {
13,743✔
52
      break;
53
    }
54
  } while (pStr - pSql < length);
13,743✔
55
  return false;
×
56
}
57

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

64
  const char* pSql = pStr;
74✔
65

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

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

100
bool qIsSelectFromSql(const char* pStr, size_t length) {
330✔
101
  if (NULL == pStr) {
330✔
102
    return false;
3✔
103
  }
104

105
  const char* pSql = pStr;
327✔
106

107
  int32_t index = 0;
327✔
108
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
327✔
109
  if (TK_SELECT != t.type) {
327✔
110
    return false;
239✔
111
  }
112

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

125
  return false;
×
126
}
127

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

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

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

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

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

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

184
  p += sprintf(p, "INSERT INTO ");
30✔
185
  memcpy(p, t.z, t.n);
30✔
186
  p += t.n;
30✔
187
  p += sprintf(p, " (");
30✔
188
  pSql += index;
30✔
189

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

200
  bool    firstColumn = true;
27✔
201
  int32_t columnCount = 0;
27✔
202
  bool inSetClause = true;
27✔
203
  int32_t numOfCols = 0;
27✔
204

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

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

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

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

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

253
    index = 0;
57✔
254
    t = tStrGetToken((char*)pSql, &index, false, NULL);
57✔
255
    if (t.type == TK_WHERE) {
57✔
256
      inSetClause = false;
15✔
257
      pSql += index;
15✔
258
    }
259
  }
260

261
  // where clause
262
  if (pSql < pEnd) {
18✔
263
    bool inWhereClause = true;
15✔
264
    int32_t bracketLevel = 0;
15✔
265

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

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

293
      const char* colName = t.z;
42✔
294
      int32_t     colNameLen = t.n;
42✔
295
      pSql += index;
42✔
296

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

304
      index = 0;
42✔
305
      t = tStrGetToken((char*)pSql, &index, false, NULL);
42✔
306
      if (t.n == 0 || t.z == NULL) {
42✔
307
        break;
308
      }
309

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

332
      index = 0;
42✔
333
      t = tStrGetToken((char*)pSql, &index, false, NULL);
42✔
334
      if (t.type == TK_AND || t.type == TK_OR) {
42✔
335
        pSql += index;
30✔
336
      } else {
337
        if (bracketLevel == 0) {
12✔
338
          break;
9✔
339
        }
340
        pSql += index;
3✔
341
      }
342
    }
343
  }
344

345
  p += sprintf(p, ") VALUES (");
18✔
346
  for (int32_t i = 0; i < columnCount; i++) {
87✔
347
    if (i > 0) {
69✔
348
      *p++ = ',';
51✔
349
    }
350
    *p++ = '?';
69✔
351
  }
352
  *p++ = ')';
18✔
353
  *p = '\0';
18✔
354

355
  *pNewSql = newSql;
18✔
356
  return code;
18✔
357
}
358

359
bool qIsCreateTbFromFileSql(const char* pStr, size_t length) {
3,589✔
360
  if (NULL == pStr) {
3,589✔
361
    return false;
×
362
  }
363

364
  const char* pSql = pStr;
3,589✔
365

366
  int32_t index = 0;
3,589✔
367
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
3,589✔
368
  if (TK_CREATE != t.type) {
3,589✔
369
    return false;
2,348✔
370
  }
371

372
  do {
373
    pStr += index;
44,771✔
374
    index = 0;
44,771✔
375
    t = tStrGetToken((char*)pStr, &index, false, NULL);
44,771✔
376
    if (TK_FILE == t.type) {
44,771✔
377
      return true;
×
378
    }
379
    if (0 == t.type || 0 == t.n) {
44,771✔
380
      break;
381
    }
382
  } while (pStr - pSql < length);
43,530✔
383
  return false;
1,241✔
384
}
385

386
bool qParseDbName(const char* pStr, size_t length, char** pDbName) {
478✔
387
  (void)length;
388
  int32_t index = 0;
478✔
389
  SToken  t;
390

391
  if (NULL == pStr) {
478✔
392
    *pDbName = NULL;
×
393
    return false;
×
394
  }
395

396
  t = tStrGetToken((char*)pStr, &index, false, NULL);
478✔
397
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
478✔
398
    *pDbName = NULL;
27✔
399
    return false;
27✔
400
  }
401

402
  t = tStrGetToken((char*)pStr, &index, false, NULL);
451✔
403
  if (TK_INTO != t.type) {
451✔
404
    *pDbName = NULL;
×
405
    return false;
×
406
  }
407

408
  t = tStrGetToken((char*)pStr, &index, false, NULL);
451✔
409
  if (t.n == 0 || t.z == NULL) {
451✔
410
    *pDbName = NULL;
×
411
    return false;
×
412
  }
413
  char* dotPos = strnchr(t.z, '.', t.n, true);
451✔
414
  if (dotPos != NULL) {
451✔
415
    int dbNameLen = dotPos - t.z;
303✔
416
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
303✔
417
    if (*pDbName == NULL) {
303✔
418
      return false;
×
419
    }
420
    strncpy(*pDbName, t.z, dbNameLen);
303✔
421
    (*pDbName)[dbNameLen] = '\0';
303✔
422
    return true;
303✔
423
  }
424
  return false;
148✔
425
}
426

427
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
6,167✔
428
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
6,167✔
429

430
  if (pCxt->parseOnly) {
6,167✔
431
    return code;
×
432
  }
433

434
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
6,167✔
435
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
97✔
436
    return TSDB_CODE_SUCCESS;
97✔
437
  }
438

439
  if (TSDB_CODE_SUCCESS == code) {
6,070✔
440
    code = translate(pCxt, pQuery, pMetaCache);
6,065✔
441
  }
442
  if (TSDB_CODE_SUCCESS == code) {
6,070✔
443
    code = calculateConstant(pCxt, pQuery);
5,177✔
444
  }
445
  return code;
6,070✔
446
}
447

448
static int32_t parseSqlIntoAst(SParseContext* pCxt, SQuery** pQuery) {
2,704✔
449
  int32_t code = parse(pCxt, pQuery);
2,704✔
450
  if (TSDB_CODE_SUCCESS == code) {
2,704✔
451
    code = analyseSemantic(pCxt, *pQuery, NULL);
2,602✔
452
  }
453
  return code;
2,704✔
454
}
455

456
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
3,589✔
457
  int32_t code = parse(pCxt, pQuery);
3,589✔
458
  if (TSDB_CODE_SUCCESS == code) {
3,589✔
459
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
3,565✔
460
  }
461
  return code;
3,589✔
462
}
463

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

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

478
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
24✔
479
  pVal->node.resType.type = pParam->buffer_type;
24✔
480
  pVal->node.resType.bytes = inputSize;
24✔
481

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

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

530
static EDealRes rewriteQueryExprAliasImpl(SNode* pNode, void* pContext) {
1,238✔
531
  if (nodesIsExprNode(pNode) && QUERY_NODE_COLUMN != nodeType(pNode)) {
1,238✔
532
    snprintf(((SExprNode*)pNode)->aliasName, TSDB_COL_NAME_LEN, "#%d", *(int32_t*)pContext);
629✔
533
    ++(*(int32_t*)pContext);
629✔
534
  }
535
  return DEAL_RES_CONTINUE;
1,238✔
536
}
537

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

554
static void rewriteExprAlias(SNode* pRoot) {
133✔
555
  int32_t no = 1;
133✔
556
  rewriteQueryExprAlias(pRoot, &no);
133✔
557
}
133✔
558

559
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
3,248✔
560
  int32_t code = TSDB_CODE_SUCCESS;
3,248✔
561
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
3,248✔
562
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
544✔
563
  } else {
564
    code = parseSqlIntoAst(pCxt, pQuery);
2,704✔
565
  }
566
  terrno = code;
3,248✔
567
  return code;
3,248✔
568
}
569

570
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
3,589✔
571
  SParseMetaCache metaCache = {0};
3,589✔
572
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
3,589✔
573
  if (TSDB_CODE_SUCCESS == code) {
3,589✔
574
    code = buildCatalogReq(&metaCache, pCatalogReq);
3,565✔
575
  }
576
  destoryParseMetaCache(&metaCache, true);
3,589✔
577
  return code;
3,589✔
578
}
579

580
static int32_t parseCreateTbFromFileSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
×
581
  if (NULL == *pQuery) return parseQuerySyntax(pCxt, pQuery, pCatalogReq);
×
582

583
  return continueCreateTbFromFile(pCxt, pQuery);
×
584
}
585

586
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
4,398✔
587
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
4,398✔
588
  if (TSDB_CODE_SUCCESS == code) {
4,398✔
589
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
4,398✔
590
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
809✔
591
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
3,589✔
592
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
×
593
    } else {
594
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
3,589✔
595
    }
596
  }
597
  (void)nodesReleaseAllocator(pCxt->allocatorId);
4,398✔
598
  terrno = code;
4,398✔
599
  return code;
4,398✔
600
}
601

602
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
3,565✔
603
                            struct SMetaData* pMetaData, SQuery* pQuery) {
604
  SParseMetaCache metaCache = {0};
3,565✔
605
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
3,565✔
606
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
3,565✔
607
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
3,565✔
608
  }
609
  if (TSDB_CODE_SUCCESS == code) {
3,565✔
610
    code = analyseSemantic(pCxt, pQuery, &metaCache);
3,565✔
611
  }
612
  (void)nodesReleaseAllocator(pCxt->allocatorId);
3,565✔
613
  destoryParseMetaCache(&metaCache, false);
3,565✔
614
  terrno = code;
3,565✔
615
  return code;
3,565✔
616
}
617

618
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
95✔
619
                          SQuery* pQuery) {
620
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
95✔
621
}
622

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

630
  return code;
×
631
}
632

633
static void destoryTablesReq(void* p) {
4,619✔
634
  STablesReq* pRes = (STablesReq*)p;
4,619✔
635
  taosArrayDestroy(pRes->pTables);
4,619✔
636
}
4,619✔
637

638
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
3,413✔
639
  if (NULL == pCatalogReq) {
3,413✔
640
    return;
75✔
641
  }
642
  taosArrayDestroy(pCatalogReq->pDbVgroup);
3,338✔
643
  taosArrayDestroy(pCatalogReq->pDbCfg);
3,338✔
644
  taosArrayDestroy(pCatalogReq->pDbInfo);
3,338✔
645
  if (pCatalogReq->cloned) {
3,338✔
646
    taosArrayDestroy(pCatalogReq->pTableMeta);
×
647
    taosArrayDestroy(pCatalogReq->pTableHash);
×
648
#ifdef TD_ENTERPRISE
649
    taosArrayDestroy(pCatalogReq->pView);
×
650
#endif
651
    taosArrayDestroy(pCatalogReq->pTableTSMAs);
×
652
    taosArrayDestroy(pCatalogReq->pTSMAs);
×
653
    taosArrayDestroy(pCatalogReq->pTableName);
×
654
  } else {
655
    taosArrayDestroyEx(pCatalogReq->pTableMeta, destoryTablesReq);
3,338✔
656
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
3,338✔
657
#ifdef TD_ENTERPRISE
658
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
3,338✔
659
#endif
660
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
3,338✔
661
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
3,338✔
662
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
3,338✔
663
  }
664
  taosArrayDestroy(pCatalogReq->pUdf);
3,338✔
665
  taosArrayDestroy(pCatalogReq->pIndex);
3,338✔
666
  taosArrayDestroy(pCatalogReq->pUser);
3,338✔
667
  taosArrayDestroy(pCatalogReq->pTableIndex);
3,338✔
668
  taosArrayDestroy(pCatalogReq->pTableCfg);
3,338✔
669
  taosArrayDestroy(pCatalogReq->pTableTag);
3,338✔
670
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
3,338✔
671
}
672

673
void tfreeSParseQueryRes(void* p) {
×
674
  if (NULL == p) {
×
675
    return;
×
676
  }
677

678
  SParseQueryRes* pRes = p;
×
679
  destoryCatalogReq(pRes->pCatalogReq);
×
680
  taosMemoryFree(pRes->pCatalogReq);
×
681
  catalogFreeMetaData(&pRes->meta);
×
682
}
683

684
void qDestroyParseContext(SParseContext* pCxt) {
3,183✔
685
  if (NULL == pCxt) {
3,183✔
686
    return;
×
687
  }
688

689
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
3,183✔
690
  taosArrayDestroy(pCxt->pTableMetaPos);
3,183✔
691
  taosArrayDestroy(pCxt->pTableVgroupPos);
3,183✔
692
  taosMemoryFree(pCxt);
3,183✔
693
}
694

695
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
13,185✔
696

697
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
×
698
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
×
699
}
700

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

713
int32_t qInitKeywordsTable() { return taosInitKeywordsTable(); }
739✔
714

715
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
55✔
716

717
int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx, void *charsetCxt) {
18✔
718
  int32_t code = TSDB_CODE_SUCCESS;
18✔
719

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

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

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

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

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

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

807
      int32_t output = 0;
×
808
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
809
                         &output, charsetCxt)) {
810
        return terrno;
×
811
      }
812
      varDataSetLen(pVal->datum.p, output);
×
813
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
814
      break;
×
815
    }
816
    case TSDB_DATA_TYPE_DECIMAL64: {
3✔
817
      // TSDB_DATA_TYPE_DECIMAL64: buffer may be string, need to convert to int64_t
818
      // If buffer is string, convert it to decimal64 value first
819
      if (pParam->length && *(pParam->length) > 0 && *(pParam->length) != sizeof(int64_t)) {
3✔
820
        // Buffer is string, need to convert
821
        uint8_t precision = pVal->node.resType.precision;
3✔
822
        uint8_t scale = pVal->node.resType.scale;
3✔
823
        // If precision/scale not set, use default (should not happen in normal case)
824
        if (precision == 0 && scale == 0) {
3✔
825
          precision = 18;
3✔
826
          scale = 0;
3✔
827
        }
828
        Decimal64 dec = {0};
3✔
829
        int32_t   code = decimal64FromStr((const char*)pParam->buffer, *(pParam->length), precision, scale, &dec);
3✔
830
        if (code != TSDB_CODE_SUCCESS) {
3✔
831
          return code;
×
832
        }
833
        int64_t value = DECIMAL64_GET_VALUE(&dec);
3✔
834
        pVal->datum.i = value;
3✔
835
        pVal->typeData = value;
3✔
836
        pVal->node.resType.bytes = sizeof(int64_t);
3✔
837
      } else {
838
        // Buffer is already int64_t value, use it directly
839
        int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
×
840
        if (code) {
×
841
          return code;
×
842
        }
843
      }
844
      break;
3✔
845
    }
846
    case TSDB_DATA_TYPE_DECIMAL: {
3✔
847
      // TSDB_DATA_TYPE_DECIMAL: buffer is string, need to convert to decimal128 binary format
848
      pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DECIMAL].bytes;
3✔
849
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes);
3✔
850
      if (NULL == pVal->datum.p) {
3✔
851
        return terrno;
×
852
      }
853

854
      // Check if buffer is string or already binary format
855
      int32_t strLen = (pParam->length && *(pParam->length) > 0) ? *(pParam->length) : 0;
3✔
856
      if (strLen > 0 && strLen != pVal->node.resType.bytes) {
3✔
857
        // Buffer is string, need to convert to decimal128
858
        uint8_t precision = pVal->node.resType.precision;
3✔
859
        uint8_t scale = pVal->node.resType.scale;
3✔
860
        // If precision/scale not set, use default (should not happen in normal case)
861
        if (precision == 0 && scale == 0) {
3✔
862
          precision = 38;
3✔
863
          scale = 0;
3✔
864
        }
865
        Decimal128 dec = {0};
3✔
866
        int32_t    code = decimal128FromStr((const char*)pParam->buffer, strLen, precision, scale, &dec);
3✔
867
        if (code != TSDB_CODE_SUCCESS) {
3✔
868
          taosMemoryFree(pVal->datum.p);
×
869
          pVal->datum.p = NULL;
×
870
          return code;
×
871
        }
872
        // Copy decimal128 binary data
873
        memcpy(pVal->datum.p, &dec, sizeof(Decimal128));
3✔
874
      } else {
875
        // Buffer is already binary format, copy directly
876
        memcpy(pVal->datum.p, pParam->buffer, pVal->node.resType.bytes);
×
877
      }
878
      break;
3✔
879
    }
880
    case TSDB_DATA_TYPE_BLOB:
×
881
    case TSDB_DATA_TYPE_MEDIUMBLOB:
882
      return TSDB_CODE_BLOB_NOT_SUPPORT;  // BLOB data type is not supported in stmt2
×
883
    default: {
150✔
884
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
150✔
885
      if (code) {
150✔
886
        return code;
×
887
      }
888
      break;
150✔
889
    }
890
  }
891
  pVal->translate = true;
206✔
892
  return TSDB_CODE_SUCCESS;
206✔
893
}
894

895
int32_t qStmtBindParams2(SQuery* pQuery, TAOS_STMT2_BIND* pParams, int32_t colIdx, void* charsetCxt) {
115✔
896
  int32_t code = TSDB_CODE_SUCCESS;
115✔
897

898
  if (colIdx < 0) {
115✔
899
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
115✔
900
    for (int32_t i = 0; i < size; ++i) {
321✔
901
      code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
206✔
902
      if (TSDB_CODE_SUCCESS != code) {
206✔
903
        return code;
×
904
      }
905
    }
906
  } else {
907
    code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
×
908
  }
909

910
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
115✔
911
    nodesDestroyNode(pQuery->pRoot);
115✔
912
    pQuery->pRoot = NULL;
115✔
913
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
115✔
914
  }
915
  if (TSDB_CODE_SUCCESS == code) {
115✔
916
    rewriteExprAlias(pQuery->pRoot);
115✔
917
  }
918
  return code;
115✔
919
}
920

921
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery, SMetaData* pMetaData) {
133✔
922
  SParseMetaCache metaCache = {0};
133✔
923
  int32_t         code = TSDB_CODE_SUCCESS;
133✔
924

925
  // If metaData is provided, we need to collect metadata keys first to build SCatalogReq
926
  // Then put the metaData into cache
927
  if (pMetaData) {
133✔
928
    SCatalogReq catalogReq = {0};
115✔
929
    // Collect metadata requirements from query
930
    code = collectMetaKey(pCxt, pQuery, &metaCache);
115✔
931
    if (TSDB_CODE_SUCCESS == code) {
115✔
932
      // Build catalog request from collected metadata requirements
933
      code = buildCatalogReq(&metaCache, &catalogReq);
115✔
934
    }
935
    if (TSDB_CODE_SUCCESS == code) {
115✔
936
      // Put metadata to cache using the catalogReq to match data
937
      code = putMetaDataToCache(&catalogReq, pMetaData, &metaCache);
115✔
938
    }
939
    // Clean up catalog request
940
    destoryCatalogReq(&catalogReq);
115✔
941
    if (TSDB_CODE_SUCCESS != code) {
115✔
942
      destoryParseMetaCache(&metaCache, false);
×
943
      return code;
×
944
    }
945
  }
946

947
  code = translate(pCxt, pQuery, &metaCache);
133✔
948
  if (TSDB_CODE_SUCCESS == code) {
133✔
949
    code = calculateConstant(pCxt, pQuery);
130✔
950
  }
951
  destoryParseMetaCache(&metaCache, false);
133✔
952
  return code;
133✔
953
}
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