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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

45.12
/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) {
2,016,878,996✔
30
  if (NULL == pStr) {
2,016,878,996✔
31
    return false;
×
32
  }
33

34
  const char* pSql = pStr;
2,016,878,996✔
35

36
  int32_t index = 0;
2,016,878,996✔
37
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
2,016,890,621✔
38
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
2,016,917,745✔
39
    return false;
872,874,208✔
40
  }
41

42
  do {
43
    pStr += index;
2,147,483,647✔
44
    index = 0;
2,147,483,647✔
45
    t = tStrGetToken((char*)pStr, &index, false, NULL);
2,147,483,647✔
46
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
2,147,483,647✔
47
      return true;
1,143,377,417✔
48
    } else if (TK_SELECT == t.type) {
2,147,483,647✔
49
      return false;
659,552✔
50
    }
51
    if (0 == t.type || 0 == t.n) {
2,147,483,647✔
52
      break;
53
    }
54
  } while (pStr - pSql < length);
2,147,483,647✔
55
  return false;
2,328✔
56
}
57

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

64
  const char* pSql = pStr;
6,379,700✔
65

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

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

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

105
  const char* pSql = pStr;
110,018✔
106

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

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

125
  return false;
×
126
}
127

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

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

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

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

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

175
  // tbname
176
  index = 0;
×
177
  t = tStrGetToken((char*)pSql, &index, false, NULL);
×
178
  if (t.n == 0 || t.z == NULL) {
×
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 ");
×
185
  memcpy(p, t.z, t.n);
×
186
  p += t.n;
×
187
  p += sprintf(p, " (");
×
188
  pSql += index;
×
189

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

364
  const char* pSql = pStr;
437,066,874✔
365

366
  int32_t index = 0;
437,066,874✔
367
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
437,071,067✔
368
  if (TK_CREATE != t.type) {
437,088,874✔
369
    return false;
382,181,541✔
370
  }
371

372
  do {
373
    pStr += index;
1,936,773,430✔
374
    index = 0;
1,936,787,834✔
375
    t = tStrGetToken((char*)pStr, &index, false, NULL);
1,936,787,834✔
376
    if (TK_FILE == t.type) {
1,936,770,927✔
377
      return true;
2,728✔
378
    }
379
    if (0 == t.type || 0 == t.n) {
1,936,768,199✔
380
      break;
381
    }
382
  } while (pStr - pSql < length);
1,881,882,928✔
383
  return false;
54,902,102✔
384
}
385

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

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

396
  t = tStrGetToken((char*)pStr, &index, false, NULL);
6,494,580✔
397
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
6,495,247✔
398
    *pDbName = NULL;
10,638✔
399
    return false;
10,638✔
400
  }
401

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

408
  t = tStrGetToken((char*)pStr, &index, false, NULL);
6,486,224✔
409
  if (t.n == 0 || t.z == NULL) {
6,486,224✔
410
    *pDbName = NULL;
1,605✔
UNCOV
411
    return false;
×
412
  }
413
  char* dotPos = strnchr(t.z, '.', t.n, true);
6,484,619✔
414
  if (dotPos != NULL) {
6,479,413✔
415
    int dbNameLen = dotPos - t.z;
107,942✔
416
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
107,942✔
417
    if (*pDbName == NULL) {
108,985✔
418
      return false;
×
419
    }
420
    strncpy(*pDbName, t.z, dbNameLen);
108,985✔
421
    (*pDbName)[dbNameLen] = '\0';
108,985✔
422
    return true;
108,985✔
423
  }
424
  return false;
6,371,471✔
425
}
426

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

430
  if (pCxt->parseOnly) {
424,246,639✔
431
    return code;
290,745✔
432
  }
433

434
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
423,957,950✔
435
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
9,464✔
436
    return TSDB_CODE_SUCCESS;
9,464✔
437
  }
438

439
  if (TSDB_CODE_SUCCESS == code) {
423,954,329✔
440
    code = translate(pCxt, pQuery, pMetaCache);
423,720,310✔
441
  }
442
  if (TSDB_CODE_SUCCESS == code) {
423,933,439✔
443
    code = calculateConstant(pCxt, pQuery);
352,412,753✔
444
  }
445
  return code;
423,896,221✔
446
}
447

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

456
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
437,061,203✔
457
  int32_t code = parse(pCxt, pQuery);
437,061,203✔
458
  if (TSDB_CODE_SUCCESS == code) {
437,024,807✔
459
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
424,211,904✔
460
  }
461
  return code;
437,062,393✔
462
}
463

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

472
  if (pParam->is_null && 1 == *(pParam->is_null)) {
10,047✔
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);
10,047✔
479
  pVal->node.resType.type = pParam->buffer_type;
10,047✔
480
  pVal->node.resType.bytes = inputSize;
10,047✔
481

482
  switch (pParam->buffer_type) {
10,047✔
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,546✔
493
    case TSDB_DATA_TYPE_GEOMETRY:
494
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
3,546✔
495
      if (NULL == pVal->datum.p) {
3,546✔
496
        return terrno;
×
497
      }
498
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
3,546✔
499
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
3,546✔
500
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
3,546✔
501
      break;
3,546✔
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: {
6,501✔
519
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
6,501✔
520
      if (code) {
6,501✔
521
        return code;
×
522
      }
523
      break;
6,501✔
524
    }
525
  }
526
  pVal->translate = true;
10,047✔
527
  return TSDB_CODE_SUCCESS;
10,047✔
528
}
529

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

538
static void rewriteQueryExprAlias(SNode* pRoot, int32_t* pNo) {
9,464✔
539
  switch (nodeType(pRoot)) {
9,464✔
540
    case QUERY_NODE_SELECT_STMT:
9,464✔
541
      nodesWalkSelectStmt((SSelectStmt*)pRoot, SQL_CLAUSE_FROM, rewriteQueryExprAliasImpl, pNo);
9,464✔
542
      break;
9,464✔
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
}
9,464✔
553

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

559
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
6,483,532✔
560
  int32_t code = TSDB_CODE_SUCCESS;
6,483,532✔
561
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
6,483,532✔
562
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
6,483,970✔
563
  } else {
564
    code = parseSqlIntoAst(pCxt, pQuery);
9,464✔
565
  }
566
  terrno = code;
6,476,550✔
567
  return code;
6,491,486✔
568
}
569

570
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
437,061,693✔
571
  SParseMetaCache metaCache = {0};
437,061,693✔
572
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
437,066,988✔
573
  if (TSDB_CODE_SUCCESS == code) {
437,058,002✔
574
    code = buildCatalogReq(&metaCache, pCatalogReq);
424,241,167✔
575
  }
576
  destoryParseMetaCache(&metaCache, true);
437,063,726✔
577
  return code;
437,060,443✔
578
}
579

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

583
  return continueCreateTbFromFile(pCxt, pQuery);
1,364✔
584
}
585

586
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
999,044,485✔
587
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
999,044,485✔
588
  if (TSDB_CODE_SUCCESS == code) {
999,056,577✔
589
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
999,056,641✔
590
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
561,988,559✔
591
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
437,080,801✔
592
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
2,728✔
593
    } else {
594
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
437,074,635✔
595
    }
596
  }
597
  (void)nodesReleaseAllocator(pCxt->allocatorId);
999,036,940✔
598
  terrno = code;
999,059,849✔
599
  return code;
999,029,317✔
600
}
601

602
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
424,250,014✔
603
                            struct SMetaData* pMetaData, SQuery* pQuery) {
604
  SParseMetaCache metaCache = {0};
424,250,014✔
605
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
424,250,066✔
606
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
424,253,139✔
607
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
424,253,237✔
608
  }
609
  if (TSDB_CODE_SUCCESS == code) {
424,246,442✔
610
    code = analyseSemantic(pCxt, pQuery, &metaCache);
424,243,803✔
611
  }
612
  (void)nodesReleaseAllocator(pCxt->allocatorId);
424,184,910✔
613
  destoryParseMetaCache(&metaCache, false);
424,238,144✔
614
  terrno = code;
424,216,024✔
615
  return code;
424,211,346✔
616
}
617

618
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
6,701,657✔
619
                          SQuery* pQuery) {
620
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
6,701,657✔
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) {
1,293,983,703✔
634
  STablesReq* pRes = (STablesReq*)p;
1,293,983,703✔
635
  taosArrayDestroy(pRes->pTables);
1,293,983,703✔
636
}
1,293,977,663✔
637

638
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
999,305,604✔
639
  if (NULL == pCatalogReq) {
999,305,604✔
640
    return;
290,745✔
641
  }
642
  taosArrayDestroy(pCatalogReq->pDbVgroup);
999,014,859✔
643
  taosArrayDestroy(pCatalogReq->pDbCfg);
999,049,649✔
644
  taosArrayDestroy(pCatalogReq->pDbInfo);
999,054,018✔
645
  if (pCatalogReq->cloned) {
999,052,505✔
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);
999,048,492✔
656
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
999,037,244✔
657
#ifdef TD_ENTERPRISE
658
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
999,046,966✔
659
#endif
660
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
999,049,051✔
661
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
999,047,383✔
662
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
999,045,393✔
663
  }
664
  taosArrayDestroy(pCatalogReq->pUdf);
999,050,762✔
665
  taosArrayDestroy(pCatalogReq->pIndex);
999,045,994✔
666
  taosArrayDestroy(pCatalogReq->pUser);
999,042,102✔
667
  taosArrayDestroy(pCatalogReq->pTableIndex);
999,042,405✔
668
  taosArrayDestroy(pCatalogReq->pTableCfg);
999,044,843✔
669
  taosArrayDestroy(pCatalogReq->pTableTag);
999,045,858✔
670
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
999,046,317✔
671
}
672

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

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

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

689
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
999,033,625✔
690
  taosArrayDestroy(pCxt->pTableMetaPos);
999,045,495✔
691
  taosArrayDestroy(pCxt->pTableVgroupPos);
999,049,887✔
692
  taosMemoryFree(pCxt);
999,051,867✔
693
}
694

695
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
1,181,020,009✔
696

697
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
100,092✔
698
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
100,092✔
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(); }
1,308,931✔
714

715
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
1,308,973✔
716

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

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

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

743
static int32_t setValueByBindParam2(SValueNode* pVal, TAOS_STMT2_BIND* pParam, void* charsetCxt) {
16✔
744
  if (!pParam || IS_NULL_TYPE(pParam->buffer_type)) {
16✔
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) {
16✔
748
    taosMemoryFreeClear(pVal->datum.p);
×
749
  }
750

751
  if (pParam->is_null && 1 == *(pParam->is_null)) {
16✔
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);
16✔
758
  pVal->node.resType.type = pParam->buffer_type;
16✔
759
  pVal->node.resType.bytes = inputSize;
16✔
760

761
  switch (pParam->buffer_type) {
16✔
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:
×
772
    case TSDB_DATA_TYPE_GEOMETRY:
773
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
774
      if (NULL == pVal->datum.p) {
×
775
        return terrno;
×
776
      }
777
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
778
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
×
779
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
780
      if (IS_DURATION_VAL(pVal->flag)) {
×
781
        taosMemoryFreeClear(pVal->literal);
×
782
        taosMemoryFreeClear(pVal->datum.p);
×
783
        pVal->literal = taosStrndup((const char*)pParam->buffer, pVal->node.resType.bytes - VARSTR_HEADER_SIZE);
×
784
        if (!pVal->literal) {
×
785
          return terrno;
×
786
        }
787
        int64_t duration = 0;
×
788
        char    unit = 0;
×
789
        if (parseNatualDuration(pVal->literal, strlen(pVal->literal), &duration, &unit,
×
790
                                pVal->node.resType.precision, true) != TSDB_CODE_SUCCESS) {
×
791
          return TSDB_CODE_PAR_WRONG_VALUE_TYPE;
×
792
        }
793
        pVal->datum.i = duration;
×
794
        pVal->unit = unit;
×
795
        *(int64_t*)&pVal->typeData = duration;
×
796
        pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT;
×
797
        pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
×
798
      }
799
      break;
×
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: {
×
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)) {
×
820
        // Buffer is string, need to convert
821
        uint8_t precision = pVal->node.resType.precision;
×
822
        uint8_t scale = pVal->node.resType.scale;
×
823
        // If precision/scale not set, use default (should not happen in normal case)
824
        if (precision == 0 && scale == 0) {
×
825
          precision = 18;
×
826
          scale = 0;
×
827
        }
828
        Decimal64 dec = {0};
×
829
        int32_t   code = decimal64FromStr((const char*)pParam->buffer, *(pParam->length), precision, scale, &dec);
×
830
        if (code != TSDB_CODE_SUCCESS) {
×
831
          return code;
×
832
        }
833
        int64_t value = DECIMAL64_GET_VALUE(&dec);
×
834
        pVal->datum.i = value;
×
835
        pVal->typeData = value;
×
836
        pVal->node.resType.bytes = sizeof(int64_t);
×
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;
×
845
    }
846
    case TSDB_DATA_TYPE_DECIMAL: {
×
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;
×
849
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes);
×
850
      if (NULL == pVal->datum.p) {
×
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;
×
856
      if (strLen > 0 && strLen != pVal->node.resType.bytes) {
×
857
        // Buffer is string, need to convert to decimal128
858
        uint8_t precision = pVal->node.resType.precision;
×
859
        uint8_t scale = pVal->node.resType.scale;
×
860
        // If precision/scale not set, use default (should not happen in normal case)
861
        if (precision == 0 && scale == 0) {
×
862
          precision = 38;
×
863
          scale = 0;
×
864
        }
865
        Decimal128 dec = {0};
×
866
        int32_t    code = decimal128FromStr((const char*)pParam->buffer, strLen, precision, scale, &dec);
×
867
        if (code != TSDB_CODE_SUCCESS) {
×
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));
×
874
      } else {
875
        // Buffer is already binary format, copy directly
876
        memcpy(pVal->datum.p, pParam->buffer, pVal->node.resType.bytes);
×
877
      }
878
      break;
×
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: {
16✔
884
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
16✔
885
      if (code) {
16✔
886
        return code;
×
887
      }
888
      break;
16✔
889
    }
890
  }
891
  pVal->translate = true;
16✔
892
  return TSDB_CODE_SUCCESS;
16✔
893
}
894

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

898
  if (colIdx < 0) {
8✔
899
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
8✔
900
    for (int32_t i = 0; i < size; ++i) {
24✔
901
      code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
16✔
902
      if (TSDB_CODE_SUCCESS != code) {
16✔
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)) {
8✔
911
    nodesDestroyNode(pQuery->pRoot);
8✔
912
    pQuery->pRoot = NULL;
8✔
913
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
8✔
914
  }
915
  if (TSDB_CODE_SUCCESS == code) {
8✔
916
    rewriteExprAlias(pQuery->pRoot);
8✔
917
  }
918
  return code;
8✔
919
}
920

921
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery, SMetaData* pMetaData) {
9,464✔
922
  SParseMetaCache metaCache = {0};
9,464✔
923
  int32_t         code = TSDB_CODE_SUCCESS;
9,464✔
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) {
9,464✔
928
    SCatalogReq catalogReq = {0};
8✔
929
    // Collect metadata requirements from query
930
    code = collectMetaKey(pCxt, pQuery, &metaCache);
8✔
931
    if (TSDB_CODE_SUCCESS == code) {
8✔
932
      // Build catalog request from collected metadata requirements
933
      code = buildCatalogReq(&metaCache, &catalogReq);
8✔
934
    }
935
    if (TSDB_CODE_SUCCESS == code) {
8✔
936
      // Put metadata to cache using the catalogReq to match data
937
      code = putMetaDataToCache(&catalogReq, pMetaData, &metaCache);
8✔
938
    }
939
    // Clean up catalog request
940
    destoryCatalogReq(&catalogReq);
8✔
941
    if (TSDB_CODE_SUCCESS != code) {
8✔
942
      destoryParseMetaCache(&metaCache, false);
×
943
      return code;
×
944
    }
945
  }
946

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