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

taosdata / TDengine / #4667

14 Aug 2025 01:04PM UTC coverage: 59.532% (-0.6%) from 60.112%
#4667

push

travis-ci

web-flow
fix(query): fix order by column check of union operator (#32524)

136437 of 292055 branches covered (46.72%)

Branch coverage included in aggregate %.

1 of 13 new or added lines in 1 file covered. (7.69%)

2683 existing lines in 164 files now uncovered.

206730 of 284385 relevant lines covered (72.69%)

4603587.36 hits per line

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

62.78
/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) {
965,007✔
27
  if (NULL == pStr) {
965,007!
UNCOV
28
    return false;
×
29
  }
30

31
  const char* pSql = pStr;
965,007✔
32

33
  int32_t index = 0;
965,007✔
34
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
965,007✔
35
  if (TK_INSERT != t.type && TK_IMPORT != t.type) {
965,079✔
36
    return false;
162,186✔
37
  }
38

39
  do {
40
    pStr += index;
2,515,696✔
41
    index = 0;
2,515,696✔
42
    t = tStrGetToken((char*)pStr, &index, false, NULL);
2,515,696✔
43
    if (TK_USING == t.type || TK_VALUES == t.type || TK_FILE == t.type) {
2,515,694✔
44
      return true;
802,796✔
45
    } else if (TK_SELECT == t.type) {
1,712,898✔
46
      return false;
87✔
47
    }
48
    if (0 == t.type || 0 == t.n) {
1,712,811!
49
      break;
50
    }
51
  } while (pStr - pSql < length);
1,712,827✔
52
  return false;
8✔
53
}
54

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

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

63
  int32_t index = 0;
10,976✔
64
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
10,976✔
65
  if (TK_UPDATE != t.type) {
10,997✔
66
    return false;
10,984✔
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) {
79,757✔
330
  if (NULL == pStr) {
79,757!
331
    return false;
×
332
  }
333

334
  const char* pSql = pStr;
79,757✔
335

336
  int32_t index = 0;
79,757✔
337
  SToken  t = tStrGetToken((char*)pStr, &index, false, NULL);
79,757✔
338
  if (TK_CREATE != t.type) {
79,780✔
339
    return false;
48,048✔
340
  }
341

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

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

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

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

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

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

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

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

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

409
  if (TSDB_CODE_SUCCESS == code) {
78,783✔
410
    code = translate(pCxt, pQuery, pMetaCache);
78,628✔
411
  }
412
  if (TSDB_CODE_SUCCESS == code) {
78,760✔
413
    code = calculateConstant(pCxt, pQuery);
74,881✔
414
  }
415
  return code;
78,780✔
416
}
417

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

426
static int32_t parseSqlSyntax(SParseContext* pCxt, SQuery** pQuery, SParseMetaCache* pMetaCache) {
79,760✔
427
  int32_t code = parse(pCxt, pQuery);
79,760✔
428
  if (TSDB_CODE_SUCCESS == code) {
79,746✔
429
    code = collectMetaKey(pCxt, *pQuery, pMetaCache);
78,574✔
430
  }
431
  return code;
79,735✔
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) {
44✔
501
  if (nodesIsExprNode(pNode) && QUERY_NODE_COLUMN != nodeType(pNode)) {
44✔
502
    snprintf(((SExprNode*)pNode)->aliasName, TSDB_COL_NAME_LEN, "#%d", *(int32_t*)pContext);
21✔
503
    ++(*(int32_t*)pContext);
21✔
504
  }
505
  return DEAL_RES_CONTINUE;
44✔
506
}
507

508
static void rewriteQueryExprAlias(SNode* pRoot, int32_t* pNo) {
8✔
509
  switch (nodeType(pRoot)) {
8!
510
    case QUERY_NODE_SELECT_STMT:
8✔
511
      nodesWalkSelectStmt((SSelectStmt*)pRoot, SQL_CLAUSE_FROM, rewriteQueryExprAliasImpl, pNo);
8✔
512
      break;
8✔
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
}
8✔
523

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

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

540
static int32_t parseQuerySyntax(SParseContext* pCxt, SQuery** pQuery, struct SCatalogReq* pCatalogReq) {
79,765✔
541
  SParseMetaCache metaCache = {0};
79,765✔
542
  int32_t         code = parseSqlSyntax(pCxt, pQuery, &metaCache);
79,765✔
543
  if (TSDB_CODE_SUCCESS == code) {
79,727✔
544
    code = buildCatalogReq(&metaCache, pCatalogReq);
78,565✔
545
  }
546
  destoryParseMetaCache(&metaCache, true);
79,745✔
547
  return code;
79,654✔
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) {
464,539✔
557
  int32_t code = nodesAcquireAllocator(pCxt->allocatorId);
464,539✔
558
  if (TSDB_CODE_SUCCESS == code) {
464,548!
559
    if (qIsInsertValuesSql(pCxt->pSql, pCxt->sqlLen)) {
464,548✔
560
      code = parseInsertSql(pCxt, pQuery, pCatalogReq, NULL);
384,812✔
561
    } else if (qIsCreateTbFromFileSql(pCxt->pSql, pCxt->sqlLen)) {
79,777!
562
      code = parseCreateTbFromFileSyntax(pCxt, pQuery, pCatalogReq);
×
563
    } else {
564
      code = parseQuerySyntax(pCxt, pQuery, pCatalogReq);
79,780✔
565
    }
566
  }
567
  (void)nodesReleaseAllocator(pCxt->allocatorId);
464,427✔
568
  terrno = code;
464,523✔
569
  return code;
464,530✔
570
}
571

572
int32_t qAnalyseSqlSemantic(SParseContext* pCxt, const struct SCatalogReq* pCatalogReq,
78,601✔
573
                            struct SMetaData* pMetaData, SQuery* pQuery) {
574
  SParseMetaCache metaCache = {0};
78,601✔
575
  int32_t         code = nodesAcquireAllocator(pCxt->allocatorId);
78,601✔
576
  if (TSDB_CODE_SUCCESS == code && pCatalogReq) {
78,596!
577
    code = putMetaDataToCache(pCatalogReq, pMetaData, &metaCache);
78,597✔
578
  }
579
  if (TSDB_CODE_SUCCESS == code) {
78,581!
580
    code = analyseSemantic(pCxt, pQuery, &metaCache);
78,586✔
581
  }
582
  (void)nodesReleaseAllocator(pCxt->allocatorId);
78,561✔
583
  destoryParseMetaCache(&metaCache, false);
78,594✔
584
  terrno = code;
78,577✔
585
  return code;
78,572✔
586
}
587

588
int32_t qContinueParseSql(SParseContext* pCxt, struct SCatalogReq* pCatalogReq, const struct SMetaData* pMetaData,
1,107✔
589
                          SQuery* pQuery) {
590
  return parseInsertSql(pCxt, &pQuery, pCatalogReq, pMetaData);
1,107✔
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) {
267,044✔
604
  STablesReq* pRes = (STablesReq*)p;
267,044✔
605
  taosArrayDestroy(pRes->pTables);
267,044✔
606
}
267,058✔
607

608
void destoryCatalogReq(SCatalogReq* pCatalogReq) {
464,744✔
609
  if (NULL == pCatalogReq) {
464,744✔
610
    return;
639✔
611
  }
612
  taosArrayDestroy(pCatalogReq->pDbVgroup);
464,105✔
613
  taosArrayDestroy(pCatalogReq->pDbCfg);
464,111✔
614
  taosArrayDestroy(pCatalogReq->pDbInfo);
464,128✔
615
  if (pCatalogReq->cloned) {
464,122!
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);
464,122✔
626
    taosArrayDestroyEx(pCatalogReq->pTableHash, destoryTablesReq);
464,096✔
627
#ifdef TD_ENTERPRISE
628
    taosArrayDestroyEx(pCatalogReq->pView, destoryTablesReq);
464,120✔
629
#endif
630
    taosArrayDestroyEx(pCatalogReq->pTableTSMAs, destoryTablesReq);
464,127✔
631
    taosArrayDestroyEx(pCatalogReq->pTSMAs, destoryTablesReq);
464,126✔
632
    taosArrayDestroyEx(pCatalogReq->pTableName, destoryTablesReq);
464,126✔
633
  }
634
  taosArrayDestroy(pCatalogReq->pUdf);
464,124✔
635
  taosArrayDestroy(pCatalogReq->pIndex);
464,127✔
636
  taosArrayDestroy(pCatalogReq->pUser);
464,126✔
637
  taosArrayDestroy(pCatalogReq->pTableIndex);
464,119✔
638
  taosArrayDestroy(pCatalogReq->pTableCfg);
464,118✔
639
  taosArrayDestroy(pCatalogReq->pTableTag);
464,121✔
640
  taosArrayDestroy(pCatalogReq->pVStbRefDbs);
464,119✔
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) {
464,092✔
655
  if (NULL == pCxt) {
464,092!
656
    return;
×
657
  }
658

659
  taosArrayDestroyEx(pCxt->pSubMetaList, tfreeSParseQueryRes);
464,092✔
660
  taosArrayDestroy(pCxt->pTableMetaPos);
464,130✔
661
  taosArrayDestroy(pCxt->pTableVgroupPos);
464,134✔
662
  taosMemoryFree(pCxt);
464,129!
663
}
664

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

667
int32_t qExtractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
412✔
668
  return extractResultSchema(pRoot, numOfCols, pSchema, NULL);
412✔
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,780✔
684

685
void qCleanupKeywordsTable() { taosCleanupKeywordsTable(); }
2,547✔
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

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

UNCOV
721
  if (pParam->is_null && 1 == *(pParam->is_null)) {
×
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

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

UNCOV
731
  switch (pParam->buffer_type) {
×
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
×
UNCOV
770
    default: {
×
UNCOV
771
      int32_t code = nodesSetValueNodeValue(pVal, pParam->buffer);
×
UNCOV
772
      if (code) {
×
773
        return code;
×
774
      }
UNCOV
775
      break;
×
776
    }
777
  }
UNCOV
778
  pVal->translate = true;
×
UNCOV
779
  return TSDB_CODE_SUCCESS;
×
780
}
781

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

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

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

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