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

taosdata / TDengine / #4658

09 Aug 2025 02:19PM UTC coverage: 59.866% (-1.3%) from 61.2%
#4658

push

travis-ci

GitHub
fix(stream)[TD-37079]: force close the last window in fill_history (#32436)

137251 of 291849 branches covered (47.03%)

Branch coverage included in aggregate %.

207628 of 284239 relevant lines covered (73.05%)

4861547.17 hits per line

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

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

31
  const char* pSql = pStr;
1,071,195✔
32

33
  int32_t index = 0;
1,071,195✔
34
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
1,071,195✔
35
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
1,071,300✔
36
    return false;
174,414✔
37
  }
38

39
  do {
40
    pStr += index;
2,800,514✔
41
    index = 0;
2,800,514✔
42
    t = tStrGetToken((char*)pStr, &index, false, NULL);
2,800,514✔
43
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
2,800,491✔
44
      return true;
896,773✔
45
    } else if (TK_SELECT == t.type) {
1,903,718✔
46
      return false;
67✔
47
    }
48
    if (0 == t.type || 0 == t.n) {
1,903,651!
49
      break;
50
    }
51
  } while (pStr - pSql < length);
1,903,662✔
52
  return false;
23✔
53
}
54

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

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

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

77
  if (pTableName != NULL) {
12!
78
    *pCode = insCreateSName(pTableName, &t, acctId, dbName, &pMsgBuf);
12✔
79
    if ((*pCode) != TSDB_CODE_SUCCESS) {
12!
80
      return false;
×
81
    }
82
  }
83

84
  do {
85
    pStr += index;
43✔
86
    index = 0;
43✔
87
    t = tStrGetToken((char*)pStr, &index, false, NULL);
43✔
88
    if (TK_SET == t.type) {
43✔
89
      return true;
11✔
90
    }
91
    if (0 == t.type || 0 == t.n) {
32!
92
      break;
93
    }
94
  } while (pStr - pSql < length);
31!
95
  return false;
1✔
96
}
97

98
static bool isColumnPrimaryKey(const STableMeta* pTableMeta, const char* colName, int32_t colNameLen, int32_t* colId) {
32✔
99
  if (pTableMeta == NULL || colName == NULL) {
32!
100
    return false;
×
101
  }
102

103
  for (int32_t i = 0; i < pTableMeta->tableInfo.numOfColumns; i++) {
95!
104
    const SSchema* pSchema = &pTableMeta->schema[i];
95✔
105
    if (strncmp(pSchema->name, colName, colNameLen) == 0 && strlen(pSchema->name) == colNameLen) {
95!
106
      if (colId) {
32✔
107
        *colId = i;
10✔
108
      }
109
      if ((pSchema->flags & COL_IS_KEY || pSchema->colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
32✔
110
        return true;
8✔
111
      }
112
      return false;
24✔
113
    }
114
  }
115
  return false;
×
116
}
117

118
int32_t convertUpdateToInsert(const char* pSql, char** pNewSql, STableMeta* pTableMeta, SSHashObj* predicateCols,
10✔
119
                              char* msgBuf, int32_t msgBufLen) {
120
  if (NULL == pSql || NULL == pNewSql) {
10!
121
    return TSDB_CODE_INVALID_PARA;
×
122
  }
123

124
  const char* pEnd = pSql + strlen(pSql);
10✔
125
  size_t      maxSqlLen = strlen(pSql) * 2;
10✔
126
  char*  newSql = taosMemoryMalloc(maxSqlLen);
10!
127
  if (newSql == NULL) {
10!
128
    return terrno;
×
129
  }
130
  char*   p = newSql;
10✔
131
  int32_t index = 0;
10✔
132
  SToken  t;
133
  int32_t code = TSDB_CODE_SUCCESS;
10✔
134
  SMsgBuf pMsgBuf = {msgBufLen, msgBuf};
10✔
135

136
  // UPDATE
137
  t = tStrGetToken((char*)pSql, &index, false, NULL);
10✔
138
  if (TK_UPDATE != t.type) {
10!
139
    taosMemoryFree(newSql);
×
140
    code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected UPDATE keyword");
×
141
    return code;
×
142
  }
143
  pSql += index;
10✔
144

145
  // tbname
146
  index = 0;
10✔
147
  t = tStrGetToken((char*)pSql, &index, false, NULL);
10✔
148
  if (t.n == 0 || t.z == NULL) {
10!
149
    taosMemoryFree(newSql);
×
150
    code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Invalid table name");
×
151
    return code;
×
152
  }
153

154
  p += sprintf(p, "INSERT INTO ");
10✔
155
  memcpy(p, t.z, t.n);
10✔
156
  p += t.n;
10✔
157
  p += sprintf(p, " (");
10✔
158
  pSql += index;
10✔
159

160
  // SET
161
  index = 0;
10✔
162
  t = tStrGetToken((char*)pSql, &index, false, NULL);
10✔
163
  if (TK_SET != t.type) {
10✔
164
    taosMemoryFree(newSql);
1!
165
    code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected SET keyword");
1✔
166
    return code;
1✔
167
  }
168
  pSql += index;
9✔
169

170
  bool    firstColumn = true;
9✔
171
  int32_t columnCount = 0;
9✔
172
  bool inSetClause = true;
9✔
173
  int32_t numOfCols = 0;
9✔
174

175
  // col name
176
  while (inSetClause && pSql < pEnd) {
28✔
177
    index = 0;
22✔
178
    t = tStrGetToken((char*)pSql, &index, false, NULL);
22✔
179
    if (t.n == 0 || t.z == NULL) {
22!
180
      break;
181
    }
182

183
    // pk can't set
184
    if (pTableMeta != NULL && isColumnPrimaryKey(pTableMeta, t.z, t.n, NULL)) {
22!
185
      taosMemoryFree(newSql);
2!
186
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Cannot update primary key column '%.*s'",
2✔
187
                                     t.n, t.z);
188
      return code;
2✔
189
    }
190

191
    if (!firstColumn) {
20✔
192
      *p++ = ',';
12✔
193
    }
194
    numOfCols++;
20✔
195
    memcpy(p, t.z, t.n);
20✔
196
    p += t.n;
20✔
197
    firstColumn = false;
20✔
198
    columnCount++;
20✔
199
    pSql += index;
20✔
200

201
    index = 0;
20✔
202
    t = tStrGetToken((char*)pSql, &index, false, NULL);
20✔
203
    if (t.type != TK_NK_EQ) {
20!
204
      taosMemoryFree(newSql);
×
205
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '=' after column name");
×
206
      return code;
×
207
    }
208
    pSql += index;
20✔
209

210
    // value must be ?
211
    index = 0;
20✔
212
    t = tStrGetToken((char*)pSql, &index, false, NULL);
20✔
213
    if (t.n == 0 || t.z == NULL) {
20!
214
      break;
215
    }
216
    if (t.type != TK_NK_QUESTION) {
20✔
217
      taosMemoryFree(newSql);
1!
218
      code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '?' placeholder");
1✔
219
      return code;
1✔
220
    }
221
    pSql += index;
19✔
222

223
    index = 0;
19✔
224
    t = tStrGetToken((char*)pSql, &index, false, NULL);
19✔
225
    if (t.type == TK_WHERE) {
19✔
226
      inSetClause = false;
5✔
227
      pSql += index;
5✔
228
    }
229
  }
230

231
  // where clause
232
  if (pSql < pEnd) {
6✔
233
    bool inWhereClause = true;
5✔
234
    int32_t bracketLevel = 0;
5✔
235

236
    while (inWhereClause && pSql < pEnd) {
19!
237
      index = 0;
17✔
238
      t = tStrGetToken((char*)pSql, &index, false, NULL);
17✔
239
      if (t.n == 0 || t.z == NULL) {
17!
240
        break;
241
      }
242

243
      if (t.type == TK_NK_LP) {
17✔
244
        bracketLevel++;
1✔
245
        pSql += index;
1✔
246
        continue;
3✔
247
      } else if (t.type == TK_NK_RP) {
16✔
248
        bracketLevel--;
1✔
249
        pSql += index;
1✔
250
        continue;
1✔
251
      } else if (t.type == TK_IN || t.type == TK_EXISTS) {
15!
252
        while (pSql < pEnd) {
13!
253
          pSql += index;
13✔
254
          index = 0;
13✔
255
          t = tStrGetToken((char*)pSql, &index, false, NULL);
13✔
256
          if (t.type == TK_AND || t.type == TK_OR || t.n == 0 || t.z == NULL) {
13!
257
            break;
258
          }
259
        }
260
        continue;
1✔
261
      }
262

263
      const char* colName = t.z;
14✔
264
      int32_t     colNameLen = t.n;
14✔
265
      pSql += index;
14✔
266

267
      index = 0;
14✔
268
      t = tStrGetToken((char*)pSql, &index, false, NULL);
14✔
269
      if (t.n == 0 || t.z == NULL) {
14!
270
        break;
271
      }
272
      pSql += index;
14✔
273

274
      index = 0;
14✔
275
      t = tStrGetToken((char*)pSql, &index, false, NULL);
14✔
276
      if (t.n == 0 || t.z == NULL) {
14!
277
        break;
278
      }
279

280
      // where cols muset be pk, ignore others
281
      int32_t colId = -1;
14✔
282
      if (t.type == TK_NK_QUESTION) {
14✔
283
        if (pTableMeta != NULL && isColumnPrimaryKey(pTableMeta, colName, colNameLen, &colId)) {
10!
284
          if (!firstColumn) {
6!
285
            *p++ = ',';
6✔
286
          }
287
          memcpy(p, colName, colNameLen);
6✔
288
          p += colNameLen;
6✔
289
          firstColumn = false;
6✔
290
          columnCount++;
6✔
291
        } else {
292
          if (tSimpleHashPut(predicateCols, &numOfCols, sizeof(int32_t), &colId, sizeof(int32_t))) {
4!
293
            taosMemoryFree(newSql);
×
294
            code = generateSyntaxErrMsgExt(&pMsgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "Expected '?' placeholder");
×
295
            return code;
×
296
          }
297
        }
298
        numOfCols++;
10✔
299
      }
300
      pSql += index;
14✔
301

302
      index = 0;
14✔
303
      t = tStrGetToken((char*)pSql, &index, false, NULL);
14✔
304
      if (t.type == TK_AND || t.type == TK_OR) {
14!
305
        pSql += index;
10✔
306
      } else {
307
        if (bracketLevel == 0) {
4✔
308
          break;
3✔
309
        }
310
        pSql += index;
1✔
311
      }
312
    }
313
  }
314

315
  p += sprintf(p, ") VALUES (");
6✔
316
  for (int32_t i = 0; i < columnCount; i++) {
29✔
317
    if (i > 0) {
23✔
318
      *p++ = ',';
17✔
319
    }
320
    *p++ = '?';
23✔
321
  }
322
  *p++ = ')';
6✔
323
  *p = '\0';
6✔
324

325
  *pNewSql = newSql;
6✔
326
  return code;
6✔
327
}
328

329
bool qIsCreateTbFromFileSql(const char* pStr, size_t length) {
86,089✔
330
  if (NULL == pStr) {
86,089!
331
    return false;
×
332
  }
333

334
  const char* pSql = pStr;
86,089✔
335

336
  int32_t index = 0;
86,089✔
337
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
86,089✔
338
  if (TK_CREATE != t.type) {
86,107✔
339
    return false;
52,079✔
340
  }
341

342
  do {
343
    pStr += index;
597,613✔
344
    index = 0;
597,613✔
345
    t = tStrGetToken((char*)pStr, &index, false, NULL);
597,613✔
346
    if (TK_FILE == t.type) {
597,613!
347
      return true;
×
348
    }
349
    if (0 == t.type || 0 == t.n) {
597,613✔
350
      break;
351
    }
352
  } while (pStr - pSql < length);
563,598✔
353
  return false;
34,028✔
354
}
355

356
bool qParseDbName(const char* pStr, size_t length, char** pDbName) {
11,148✔
357
  (void)length;
358
  int32_t index = 0;
11,148✔
359
  SToken  t;
360

361
  if (NULL == pStr) {
11,148!
362
    *pDbName = NULL;
×
363
    return false;
×
364
  }
365

366
  t = tStrGetToken((char*)pStr, &index, false, NULL);
11,148✔
367
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
11,177!
368
    *pDbName = NULL;
23✔
369
    return false;
23✔
370
  }
371

372
  t = tStrGetToken((char*)pStr, &index, false, NULL);
11,154✔
373
  if (TK_INTO != t.type) {
11,152!
374
    *pDbName = NULL;
×
375
    return false;
×
376
  }
377

378
  t = tStrGetToken((char*)pStr, &index, false, NULL);
11,152✔
379
  if (t.n == 0 || t.z == NULL) {
11,153✔
380
    *pDbName = NULL;
4✔
381
    return false;
4✔
382
  }
383
  char* dotPos = strnchr(t.z, '.', t.n, true);
11,149✔
384
  if (dotPos != NULL) {
11,147✔
385
    int dbNameLen = dotPos - t.z;
970✔
386
    *pDbName = taosMemoryMalloc(dbNameLen + 1);
970!
387
    if (*pDbName == NULL) {
947!
388
      return false;
×
389
    }
390
    strncpy(*pDbName, t.z, dbNameLen);
947✔
391
    (*pDbName)[dbNameLen] = '\0';
947✔
392
    return true;
947✔
393
  }
394
  return false;
10,177✔
395
}
396

397
static int32_t analyseSemantic(SParseContext* pCxt, SQuery* pQuery, SParseMetaCache* pMetaCache) {
85,651✔
398
  int32_t code = authenticate(pCxt, pQuery, pMetaCache);
85,651✔
399

400
  if (pCxt->parseOnly) {
85,646✔
401
    return code;
639✔
402
  }
403

404
  if (TSDB_CODE_SUCCESS == code && pQuery->placeholderNum > 0) {
85,007✔
405
    TSWAP(pQuery->pPrepareRoot, pQuery->pRoot);
12✔
406
    return TSDB_CODE_SUCCESS;
12✔
407
  }
408

409
  if (TSDB_CODE_SUCCESS == code) {
84,995✔
410
    code = translate(pCxt, pQuery, pMetaCache);
84,842✔
411
  }
412
  if (TSDB_CODE_SUCCESS == code) {
84,982✔
413
    code = calculateConstant(pCxt, pQuery);
80,747✔
414
  }
415
  return code;
84,975✔
416
}
417

418
static int32_t parseSqlIntoAst(SParseContext* pCxt, SQuery** pQuery) {
883✔
419
  int32_t code = parse(pCxt, pQuery);
883✔
420
  if (TSDB_CODE_SUCCESS == code) {
883✔
421
    code = analyseSemantic(pCxt, *pQuery, NULL);
849✔
422
  }
423
  return code;
883✔
424
}
425

426
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
86,087✔
427
  int32_t code = parse(pCxt, pQuery);
86,087✔
428
  if (TSDB_CODE_SUCCESS == code) {
86,076✔
429
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
84,791✔
430
  }
431
  return code;
86,075✔
432
}
433

434
static int32_t setValueByBindParam(SValueNode* pVal, TAOS_MULTI_BIND* pParam, void *charsetCxt) {
10✔
435
  if (!pParam || IS_NULL_TYPE(pParam->buffer_type)) {
10!
436
    return TSDB_CODE_APP_ERROR;
×
437
  }
438
  if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) {
10!
439
    taosMemoryFreeClear(pVal->datum.p);
×
440
  }
441

442
  if (pParam->is_null && 1 == *(pParam->is_null)) {
10!
443
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
×
444
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
×
445
    return TSDB_CODE_SUCCESS;
×
446
  }
447

448
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
10!
449
  pVal->node.resType.type = pParam->buffer_type;
10✔
450
  pVal->node.resType.bytes = inputSize;
10✔
451

452
  switch (pParam->buffer_type) {
10!
453
    case TSDB_DATA_TYPE_VARBINARY:
1✔
454
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
1!
455
      if (NULL == pVal->datum.p) {
1!
456
        return terrno;
×
457
      }
458
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
1✔
459
      memcpy(varDataVal(pVal->datum.p), pParam->buffer, pVal->node.resType.bytes);
1✔
460
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
1✔
461
      break;
1✔
462
    case TSDB_DATA_TYPE_VARCHAR:
2✔
463
    case TSDB_DATA_TYPE_GEOMETRY:
464
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
2!
465
      if (NULL == pVal->datum.p) {
2!
466
        return terrno;
×
467
      }
468
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
2✔
469
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
2✔
470
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
2✔
471
      break;
2✔
472
    case TSDB_DATA_TYPE_NCHAR: {
×
473
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
×
474
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
475
      if (NULL == pVal->datum.p) {
×
476
        return terrno;
×
477
      }
478

479
      int32_t output = 0;
×
480
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
481
                         &output, charsetCxt)) {
482
        return terrno;
×
483
      }
484
      varDataSetLen(pVal->datum.p, output);
×
485
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
486
      break;
×
487
    }
488
    default: {
7✔
489
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
7✔
490
      if (code) {
7!
491
        return code;
×
492
      }
493
      break;
7✔
494
    }
495
  }
496
  pVal->translate = true;
10✔
497
  return TSDB_CODE_SUCCESS;
10✔
498
}
499

500
static EDealRes rewriteQueryExprAliasImpl(SNode* pNode, void* pContext) {
81✔
501
  if (nodesIsExprNode(pNode) && QUERY_NODE_COLUMN != nodeType(pNode)) {
81✔
502
    snprintf(((SExprNode*)pNode)->aliasName, TSDB_COL_NAME_LEN, "#%d", *(int32_t*)pContext);
40✔
503
    ++(*(int32_t*)pContext);
40✔
504
  }
505
  return DEAL_RES_CONTINUE;
81✔
506
}
507

508
static void rewriteQueryExprAlias(SNode* pRoot, int32_t* pNo) {
13✔
509
  switch (nodeType(pRoot)) {
13!
510
    case QUERY_NODE_SELECT_STMT:
13✔
511
      nodesWalkSelectStmt((SSelectStmt*)pRoot, SQL_CLAUSE_FROM, rewriteQueryExprAliasImpl, pNo);
13✔
512
      break;
13✔
513
    case QUERY_NODE_SET_OPERATOR: {
×
514
      SSetOperator* pSetOper = (SSetOperator*)pRoot;
×
515
      rewriteQueryExprAlias(pSetOper->pLeft, pNo);
×
516
      rewriteQueryExprAlias(pSetOper->pRight, pNo);
×
517
      break;
×
518
    }
519
    default:
×
520
      break;
×
521
  }
522
}
13✔
523

524
static void rewriteExprAlias(SNode* pRoot) {
13✔
525
  int32_t no = 1;
13✔
526
  rewriteQueryExprAlias(pRoot, &no);
13✔
527
}
13✔
528

529
int32_t qParseSql(SParseContext* pCxt, SQuery** pQuery) {
11,979✔
530
  int32_t code = TSDB_CODE_SUCCESS;
11,979✔
531
  if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
11,979✔
532
    code = parseInsertSql(pCxt, pQuery, NULL, NULL);
11,122✔
533
  } else {
534
    code = parseSqlIntoAst(pCxt, pQuery);
883✔
535
  }
536
  terrno = code;
11,975✔
537
  return code;
11,995✔
538
}
539

540
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
86,090✔
541
  SParseMetaCache metaCache = {0};
86,090✔
542
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
86,090✔
543
  if (TSDB_CODE_SUCCESS == code) {
86,071✔
544
    code = buildCatalogReq(&metaCache, pCatalogReq);
84,783✔
545
  }
546
  destoryParseMetaCache(&metaCache, true);
86,085✔
547
  return code;
86,009✔
548
}
549

550
static int32_t parseCreateTbFromFileSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
×
551
  if (NULL == *pQuery) return parseQuerySyntax(pCxt, pQuery, pCatalogReq);
×
552

553
  return continueCreateTbFromFile(pCxt, pQuery);
×
554
}
555

556
int32_t qParseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
517,677✔
557
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
517,677✔
558
  if (TSDB_CODE_SUCCESS == code) {
517,691!
559
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
517,692✔
560
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
431,613✔
561
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
86,109!
562
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
×
563
    } else {
564
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
86,106✔
565
    }
566
  }
567
  (void)nodesReleaseAllocator(pCxt->allocatorId);
517,549✔
568
  terrno = code;
517,664✔
569
  return code;
517,682✔
570
}
571

572
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
84,813✔
573
                            struct SMetaData* pMetaData, SQuery* pQuery) {
574
  SParseMetaCache metaCache = {0};
84,813✔
575
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
84,813✔
576
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
84,809!
577
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
84,810✔
578
  }
579
  if (TSDB_CODE_SUCCESS == code) {
84,802!
580
    code = analyseSemantic(pCxt, pQuery, &metaCache);
84,805✔
581
  }
582
  (void)nodesReleaseAllocator(pCxt->allocatorId);
84,761✔
583
  destoryParseMetaCache(&metaCache, false);
84,795✔
584
  terrno = code;
84,801✔
585
  return code;
84,791✔
586
}
587

588
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
21,164✔
589
                          SQuery* pQuery) {
590
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
21,164✔
591
}
592

593
int32_t qContinueParsePostQuery(SParseContext* pCxt, SQuery* pQuery, SSDataBlock* pBlock) {
×
594
  int32_t code = TSDB_CODE_SUCCESS;
×
595
  switch (nodeType(pQuery->pRoot)) {
×
596
    default:
597
      break;
×
598
  }
599

600
  return code;
×
601
}
602

603
static void destoryTablesReq(void* p) {
286,605✔
604
  STablesReq* pRes = (STablesReq*)p;
286,605✔
605
  taosArrayDestroy(pRes->pTables);
286,605✔
606
}
286,616✔
607

608
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
517,877✔
609
  if (NULL == pCatalogReq) {
517,877✔
610
    return;
654✔
611
  }
612
  taosArrayDestroy(pCatalogReq->pDbVgroup);
517,223✔
613
  taosArrayDestroy(pCatalogReq->pDbCfg);
517,236✔
614
  taosArrayDestroy(pCatalogReq->pDbInfo);
517,250✔
615
  if (pCatalogReq->cloned) {
517,255!
616
    taosArrayDestroy(pCatalogReq->pTableMeta);
×
617
    taosArrayDestroy(pCatalogReq->pTableHash);
×
618
#ifdef TD_ENTERPRISE
619
    taosArrayDestroy(pCatalogReq->pView);
×
620
#endif
621
    taosArrayDestroy(pCatalogReq->pTableTSMAs);
×
622
    taosArrayDestroy(pCatalogReq->pTSMAs);
×
623
    taosArrayDestroy(pCatalogReq->pTableName);
×
624
  } else {
625
    taosArrayDestroyEx(pCatalogReq->pTableMeta, destoryTablesReq);
517,255✔
626
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
517,230✔
627
#ifdef TD_ENTERPRISE
628
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
517,247✔
629
#endif
630
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
517,254✔
631
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
517,256✔
632
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
517,255✔
633
  }
634
  taosArrayDestroy(pCatalogReq->pUdf);
517,257✔
635
  taosArrayDestroy(pCatalogReq->pIndex);
517,255✔
636
  taosArrayDestroy(pCatalogReq->pUser);
517,256✔
637
  taosArrayDestroy(pCatalogReq->pTableIndex);
517,246✔
638
  taosArrayDestroy(pCatalogReq->pTableCfg);
517,246✔
639
  taosArrayDestroy(pCatalogReq->pTableTag);
517,252✔
640
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
517,261✔
641
}
642

643
void tfreeSParseQueryRes(void* p) {
639✔
644
  if (NULL == p) {
639!
645
    return;
×
646
  }
647

648
  SParseQueryRes* pRes = p;
639✔
649
  destoryCatalogReq(pRes->pCatalogReq);
639✔
650
  taosMemoryFree(pRes->pCatalogReq);
639!
651
  catalogFreeMetaData(&pRes->meta);
639✔
652
}
653

654
void qDestroyParseContext(SParseContext* pCxt) {
517,226✔
655
  if (NULL == pCxt) {
517,226!
656
    return;
×
657
  }
658

659
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
517,226✔
660
  taosArrayDestroy(pCxt->pTableMetaPos);
517,281✔
661
  taosArrayDestroy(pCxt->pTableVgroupPos);
517,282✔
662
  taosMemoryFree(pCxt);
517,283!
663
}
664

665
void qDestroyQuery(SQuery* pQueryNode) { nodesDestroyNode((SNode*)pQueryNode); }
557,327✔
666

667
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
402✔
668
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
402✔
669
}
670

671
int32_t qSetSTableIdForRsma(SNode* pStmt, int64_t uid) {
×
672
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
×
673
    SNode* pTable = ((SSelectStmt*)pStmt)->pFromTable;
×
674
    if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
×
675
      ((SRealTableNode*)pTable)->pMeta->uid = uid;
×
676
      ((SRealTableNode*)pTable)->pMeta->suid = uid;
×
677
      return TSDB_CODE_SUCCESS;
×
678
    }
679
  }
680
  return TSDB_CODE_FAILED;
×
681
}
682

683
int32_t qInitKeywordsTable() { return taosInitKeywordsTable(); }
2,750✔
684

685
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
2,517✔
686

687
int32_t qStmtBindParams(SQuery* pQuery, TAOS_MULTI_BIND* pParams, int32_t colIdx, void *charsetCxt) {
8✔
688
  int32_t code = TSDB_CODE_SUCCESS;
8✔
689

690
  if (colIdx < 0) {
8✔
691
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
4✔
692
    for (int32_t i = 0; i < size; ++i) {
10✔
693
      code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
6✔
694
      if (TSDB_CODE_SUCCESS != code) {
6!
695
        return code;
×
696
      }
697
    }
698
  } else {
699
    code = setValueByBindParam((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
4✔
700
  }
701

702
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
8!
703
    nodesDestroyNode(pQuery->pRoot);
8✔
704
    pQuery->pRoot = NULL;
8✔
705
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
8✔
706
  }
707
  if (TSDB_CODE_SUCCESS == code) {
8!
708
    rewriteExprAlias(pQuery->pRoot);
8✔
709
  }
710
  return code;
8✔
711
}
712

713
static int32_t setValueByBindParam2(SValueNode* pVal, TAOS_STMT2_BIND* pParam, void* charsetCxt) {
9✔
714
  if (!pParam || IS_NULL_TYPE(pParam->buffer_type)) {
9!
715
    return TSDB_CODE_APP_ERROR;
×
716
  }
717
  if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) {
9!
718
    taosMemoryFreeClear(pVal->datum.p);
×
719
  }
720

721
  if (pParam->is_null && 1 == *(pParam->is_null)) {
9!
722
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
×
723
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
×
724
    return TSDB_CODE_SUCCESS;
×
725
  }
726

727
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
9!
728
  pVal->node.resType.type = pParam->buffer_type;
9✔
729
  pVal->node.resType.bytes = inputSize;
9✔
730

731
  switch (pParam->buffer_type) {
9!
732
    case TSDB_DATA_TYPE_VARBINARY:
×
733
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
734
      if (NULL == pVal->datum.p) {
×
735
        return terrno;
×
736
      }
737
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
738
      memcpy(varDataVal(pVal->datum.p), pParam->buffer, pVal->node.resType.bytes);
×
739
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
740
      break;
×
741
    case TSDB_DATA_TYPE_VARCHAR:
×
742
    case TSDB_DATA_TYPE_GEOMETRY:
743
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
744
      if (NULL == pVal->datum.p) {
×
745
        return terrno;
×
746
      }
747
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
×
748
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
×
749
      pVal->node.resType.bytes += VARSTR_HEADER_SIZE;
×
750
      break;
×
751
    case TSDB_DATA_TYPE_NCHAR: {
×
752
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
×
753
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
×
754
      if (NULL == pVal->datum.p) {
×
755
        return terrno;
×
756
      }
757

758
      int32_t output = 0;
×
759
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
×
760
                         &output, charsetCxt)) {
761
        return terrno;
×
762
      }
763
      varDataSetLen(pVal->datum.p, output);
×
764
      pVal->node.resType.bytes = output + VARSTR_HEADER_SIZE;
×
765
      break;
×
766
    }
767
    case TSDB_DATA_TYPE_BLOB:
×
768
    case TSDB_DATA_TYPE_MEDIUMBLOB:
769
      return TSDB_CODE_BLOB_NOT_SUPPORT;  // BLOB data type is not supported in stmt2
×
770
    default: {
9✔
771
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
9✔
772
      if (code) {
9!
773
        return code;
×
774
      }
775
      break;
9✔
776
    }
777
  }
778
  pVal->translate = true;
9✔
779
  return TSDB_CODE_SUCCESS;
9✔
780
}
781

782
int32_t qStmtBindParams2(SQuery* pQuery, TAOS_STMT2_BIND* pParams, int32_t colIdx, void* charsetCxt) {
5✔
783
  int32_t code = TSDB_CODE_SUCCESS;
5✔
784

785
  if (colIdx < 0) {
5!
786
    int32_t size = taosArrayGetSize(pQuery->pPlaceholderValues);
5✔
787
    for (int32_t i = 0; i < size; ++i) {
14✔
788
      code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, i), pParams + i, charsetCxt);
9✔
789
      if (TSDB_CODE_SUCCESS != code) {
9!
790
        return code;
×
791
      }
792
    }
793
  } else {
794
    code = setValueByBindParam2((SValueNode*)taosArrayGetP(pQuery->pPlaceholderValues, colIdx), pParams, charsetCxt);
×
795
  }
796

797
  if (TSDB_CODE_SUCCESS == code && (colIdx < 0 || colIdx + 1 == pQuery->placeholderNum)) {
5!
798
    nodesDestroyNode(pQuery->pRoot);
5✔
799
    pQuery->pRoot = NULL;
5✔
800
    code = nodesCloneNode(pQuery->pPrepareRoot, &pQuery->pRoot);
5✔
801
  }
802
  if (TSDB_CODE_SUCCESS == code) {
5!
803
    rewriteExprAlias(pQuery->pRoot);
5✔
804
  }
805
  return code;
5✔
806
}
807

808
int32_t qStmtParseQuerySql(SParseContext* pCxt, SQuery* pQuery) {
13✔
809
  int32_t code = translate(pCxt, pQuery, NULL);
13✔
810
  if (TSDB_CODE_SUCCESS == code) {
13!
811
    code = calculateConstant(pCxt, pQuery);
13✔
812
  }
813
  return code;
13✔
814
}
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