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

taosdata / TDengine / #3535

23 Nov 2024 02:07AM UTC coverage: 60.85% (+0.03%) from 60.825%
#3535

push

travis-ci

web-flow
Merge pull request #28893 from taosdata/doc/internal

refact: rename taos lib name

120252 of 252737 branches covered (47.58%)

Branch coverage included in aggregate %.

201187 of 275508 relevant lines covered (73.02%)

15886166.19 hits per line

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

74.55
/source/libs/parser/src/parUtil.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 "parUtil.h"
17
#include "cJSON.h"
18
#include "querynodes.h"
19
#include "tarray.h"
20
#include "tlog.h"
21

22
#define USER_AUTH_KEY_MAX_LEN TSDB_USER_LEN + TSDB_TABLE_FNAME_LEN + 2
23

24
const void* nullPointer = NULL;
25

26
static char* getSyntaxErrFormat(int32_t errCode) {
58,454✔
27
  switch (errCode) {
58,454!
28
    case TSDB_CODE_PAR_SYNTAX_ERROR:
28,375✔
29
      return "syntax error near \"%s\"";
28,375✔
30
    case TSDB_CODE_PAR_INCOMPLETE_SQL:
1,707✔
31
      return "Incomplete SQL statement";
1,707✔
32
    case TSDB_CODE_PAR_INVALID_COLUMN:
9,715✔
33
      return "Invalid column name: %s";
9,715✔
34
    case TSDB_CODE_PAR_TABLE_NOT_EXIST:
727✔
35
      return "Table does not exist: %s";
727✔
36
    case TSDB_CODE_PAR_GET_META_ERROR:
5,309✔
37
      return "Fail to get table info, error: %s";
5,309✔
38
    case TSDB_CODE_PAR_AMBIGUOUS_COLUMN:
327✔
39
      return "Column ambiguously defined: %s";
327✔
40
    case TSDB_CODE_PAR_WRONG_VALUE_TYPE:
1✔
41
      return "Invalid value type: %s";
1✔
42
    case TSDB_CODE_PAR_INVALID_VARBINARY:
992✔
43
      return "Invalid varbinary value: %s";
992✔
44
    case TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION:
3,594✔
45
      return "There mustn't be aggregation";
3,594✔
46
    case TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT:
48✔
47
      return "ORDER BY / GROUP BY item must be the number of a SELECT-list expression";
48✔
48
    case TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION:
600✔
49
      return "Not a GROUP BY expression";
600✔
50
    case TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION:
132✔
51
      return "Not SELECTed expression";
132✔
52
    case TSDB_CODE_PAR_NOT_SINGLE_GROUP:
846✔
53
      return "Not a single-group group function";
846✔
54
    case TSDB_CODE_PAR_TAGS_NOT_MATCHED:
80✔
55
      return "Tags number not matched";
80✔
56
    case TSDB_CODE_PAR_INVALID_TAG_NAME:
40✔
57
      return "Invalid tag name: %s";
40✔
58
    case TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG:
4✔
59
      return "Name or password too long";
4✔
60
    case TSDB_CODE_PAR_PASSWD_EMPTY:
4✔
61
      return "Password can not be empty";
4✔
62
    case TSDB_CODE_PAR_INVALID_PORT:
×
63
      return "Port should be an integer that is less than 65535 and greater than 0";
×
64
    case TSDB_CODE_PAR_INVALID_ENDPOINT:
×
65
      return "Endpoint should be in the format of 'fqdn:port'";
×
66
    case TSDB_CODE_PAR_EXPRIE_STATEMENT:
1✔
67
      return "This statement is no longer supported";
1✔
68
    case TSDB_CODE_PAR_INTER_VALUE_TOO_SMALL:
2✔
69
      return "Interval cannot be less than %d %s";
2✔
70
    case TSDB_CODE_PAR_INTER_VALUE_TOO_BIG:
×
71
      return "Interval cannot be more than %d %s";
×
72
    case TSDB_CODE_PAR_DB_NOT_SPECIFIED:
21✔
73
      return "Database not specified";
21✔
74
    case TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME:
42✔
75
      return "Invalid identifier name: %s";
42✔
76
    case TSDB_CODE_PAR_CORRESPONDING_STABLE_ERR:
6✔
77
      return "Corresponding super table not in this db";
6✔
78
    case TSDB_CODE_PAR_GROUPBY_WINDOW_COEXIST:
38✔
79
      return "GROUP BY and WINDOW-clause can't be used together";
38✔
80
    case TSDB_CODE_PAR_AGG_FUNC_NESTING:
27✔
81
      return "Aggregate functions do not support nesting";
27✔
82
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE:
×
83
      return "Only support STATE_WINDOW on integer/bool/varchar column";
×
84
    case TSDB_CODE_PAR_INVALID_STATE_WIN_COL:
1✔
85
      return "Not support STATE_WINDOW on tag column";
1✔
86
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TABLE:
×
87
      return "STATE_WINDOW not support for super table query";
×
88
    case TSDB_CODE_PAR_INTER_SESSION_GAP:
×
89
      return "SESSION gap should be fixed time window, and greater than 0";
×
90
    case TSDB_CODE_PAR_INTER_SESSION_COL:
14✔
91
      return "Only support SESSION on primary timestamp column";
14✔
92
    case TSDB_CODE_PAR_INTER_OFFSET_NEGATIVE:
×
93
      return "Interval offset cannot be negative";
×
94
    case TSDB_CODE_PAR_INTER_OFFSET_UNIT:
×
95
      return "Cannot use 'year' as offset when interval is 'month'";
×
96
    case TSDB_CODE_PAR_INTER_OFFSET_TOO_BIG:
×
97
      return "Interval offset should be shorter than interval";
×
98
    case TSDB_CODE_PAR_INTER_SLIDING_UNIT:
10✔
99
      return "Does not support sliding when interval is natural month/year";
10✔
100
    case TSDB_CODE_PAR_INTER_SLIDING_TOO_BIG:
4✔
101
      return "sliding value no larger than the interval value";
4✔
102
    case TSDB_CODE_PAR_INTER_SLIDING_TOO_SMALL:
3✔
103
      return "sliding value can not less than 1%% of interval value";
3✔
104
    case TSDB_CODE_PAR_ONLY_ONE_JSON_TAG:
20✔
105
      return "Only one tag if there is a json tag";
20✔
106
    case TSDB_CODE_PAR_INCORRECT_NUM_OF_COL:
×
107
      return "Query block has incorrect number of result columns";
×
108
    case TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL:
×
109
      return "Incorrect TIMESTAMP value: %s";
×
110
    case TSDB_CODE_PAR_OFFSET_LESS_ZERO:
×
111
      return "soffset/offset can not be less than 0";
×
112
    case TSDB_CODE_PAR_SLIMIT_LEAK_PARTITION_GROUP_BY:
1,047✔
113
      return "slimit/soffset only available for PARTITION/GROUP BY query";
1,047✔
114
    case TSDB_CODE_PAR_INVALID_TOPIC_QUERY:
81✔
115
      return "Invalid topic query";
81✔
116
    case TSDB_CODE_PAR_INVALID_DROP_STABLE:
11✔
117
      return "Cannot drop super table in batch";
11✔
118
    case TSDB_CODE_PAR_INVALID_FILL_TIME_RANGE:
23✔
119
      return "Start(end) time of query range required or time range too large";
23✔
120
    case TSDB_CODE_PAR_DUPLICATED_COLUMN:
5✔
121
      return "Duplicated column names";
5✔
122
    case TSDB_CODE_PAR_INVALID_TAGS_LENGTH:
361✔
123
      return "Tags length exceeds max length %d";
361✔
124
    case TSDB_CODE_PAR_INVALID_ROW_LENGTH:
170✔
125
      return "Row length exceeds max length %d";
170✔
126
    case TSDB_CODE_PAR_INVALID_COLUMNS_NUM:
26✔
127
      return "Illegal number of columns";
26✔
128
    case TSDB_CODE_PAR_TOO_MANY_COLUMNS:
16✔
129
      return "Too many columns";
16✔
130
    case TSDB_CODE_PAR_INVALID_FIRST_COLUMN:
×
131
      return "First column must be timestamp";
×
132
    case TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN:
52✔
133
      return "Invalid column length for var length type";
52✔
134
    case TSDB_CODE_PAR_INVALID_TAGS_NUM:
×
135
      return "Invalid number of tag columns";
×
136
    case TSDB_CODE_PAR_INVALID_INTERNAL_PK:
×
137
      return "Invalid _c0 or _rowts expression";
×
138
    case TSDB_CODE_PAR_INVALID_TIMELINE_FUNC:
×
139
      return "Invalid timeline function";
×
140
    case TSDB_CODE_PAR_INVALID_PASSWD:
2✔
141
      return "Invalid password";
2✔
142
    case TSDB_CODE_PAR_INVALID_ALTER_TABLE:
34✔
143
      return "Invalid alter table statement";
34✔
144
    case TSDB_CODE_PAR_CANNOT_DROP_PRIMARY_KEY:
1✔
145
      return "Primary timestamp column cannot be dropped";
1✔
146
    case TSDB_CODE_PAR_INVALID_MODIFY_COL:
814✔
147
      return "Only varbinary/binary/nchar/geometry column length could be modified, and the length can only be "
814✔
148
             "increased, not decreased";
149
    case TSDB_CODE_PAR_INVALID_TBNAME:
50✔
150
      return "Invalid tbname pseudo column";
50✔
151
    case TSDB_CODE_PAR_INVALID_FUNCTION_NAME:
4✔
152
      return "Invalid function name";
4✔
153
    case TSDB_CODE_PAR_COMMENT_TOO_LONG:
13✔
154
      return "Comment too long";
13✔
155
    case TSDB_CODE_PAR_NOT_ALLOWED_FUNC:
1,009✔
156
      return "Some functions are allowed only in the SELECT list of a query. "
1,009✔
157
             "And, cannot be mixed with other non scalar functions or columns.";
158
    case TSDB_CODE_PAR_NOT_ALLOWED_WIN_QUERY:
145✔
159
      return "Window query not supported, since not valid primary timestamp column as input";
145✔
160
    case TSDB_CODE_PAR_INVALID_DROP_COL:
3✔
161
      return "No columns can be dropped";
3✔
162
    case TSDB_CODE_PAR_INVALID_COL_JSON:
8✔
163
      return "Only tag can be json type";
8✔
164
    case TSDB_CODE_PAR_VALUE_TOO_LONG:
620✔
165
      return "Value too long for column/tag: %s";
620✔
166
    case TSDB_CODE_PAR_INVALID_DELETE_WHERE:
366✔
167
      return "The DELETE statement must only have a definite time window range";
366✔
168
    case TSDB_CODE_PAR_INVALID_REDISTRIBUTE_VG:
×
169
      return "The REDISTRIBUTE VGROUP statement only support 1 to 3 dnodes";
×
170
    case TSDB_CODE_PAR_FILL_NOT_ALLOWED_FUNC:
28✔
171
      return "%s function is not supported in fill query";
28✔
172
    case TSDB_CODE_PAR_INVALID_WINDOW_PC:
1✔
173
      return "_WSTART, _WEND and _WDURATION can only be used in window query";
1✔
174
    case TSDB_CODE_PAR_INVALID_TAGS_PC:
13✔
175
      return "Tags can only applied to super table and child table";
13✔
176
    case TSDB_CODE_PAR_WINDOW_NOT_ALLOWED_FUNC:
×
177
      return "%s function is not supported in time window query";
×
178
    case TSDB_CODE_PAR_STREAM_NOT_ALLOWED_FUNC:
×
179
      return "%s function is not supported in stream query";
×
180
    case TSDB_CODE_PAR_GROUP_BY_NOT_ALLOWED_FUNC:
×
181
      return "%s function is not supported in group query";
×
182
    case TSDB_CODE_PAR_SYSTABLE_NOT_ALLOWED_FUNC:
13✔
183
      return "%s function is not supported in system table query";
13✔
184
    case TSDB_CODE_PAR_SYSTABLE_NOT_ALLOWED:
×
185
      return "%s is not supported in system table query";
×
186
    case TSDB_CODE_PAR_INVALID_INTERP_CLAUSE:
4✔
187
      return "Invalid usage of RANGE clause, EVERY clause or FILL clause";
4✔
188
    case TSDB_CODE_PAR_INVALID_FORECAST_CLAUSE:
×
189
      return "Invalid usage of forecast clause";
×
190
    case TSDB_CODE_PAR_NO_VALID_FUNC_IN_WIN:
×
191
      return "No valid function in window query";
×
192
    case TSDB_CODE_PAR_INVALID_OPTR_USAGE:
612✔
193
      return "Invalid usage of expr: %s";
612✔
194
    case TSDB_CODE_PAR_INVALID_IP_RANGE:
×
195
      return "invalid ip range";
×
196
    case TSDB_CODE_OUT_OF_MEMORY:
×
197
      return "Out of memory";
×
198
    case TSDB_CODE_PAR_ORDERBY_AMBIGUOUS:
22✔
199
      return "ORDER BY \"%s\" is ambiguous";
22✔
200
    case TSDB_CODE_PAR_NOT_SUPPORT_MULTI_RESULT:
5✔
201
      return "Operator not supported multi result: %s";
5✔
202
    case TSDB_CODE_PAR_INVALID_WJOIN_HAVING_EXPR:
19✔
203
      return "Not supported window join having expr";
19✔
204
    case TSDB_CODE_PAR_INVALID_WIN_OFFSET_UNIT:
66✔
205
      return "Invalid WINDOW_OFFSET unit \"%c\"";
66✔
206
    case TSDB_CODE_PAR_VALID_PRIM_TS_REQUIRED:
34✔
207
      return "Valid primary timestamp required";
34✔
208
    case TSDB_CODE_PAR_NOT_WIN_FUNC:
×
209
      return "Column exists for window join with aggregation functions";
×
210
    case TSDB_CODE_PAR_TAG_IS_PRIMARY_KEY:
×
211
      return "tag %s can not be primary key";
×
212
    case TSDB_CODE_PAR_SECOND_COL_PK:
8✔
213
      return "primary key column must be second column";
8✔
214
    case TSDB_CODE_PAR_COL_PK_TYPE:
24✔
215
      return "primary key column must be of type int, uint, bigint, ubigint, and varchar";
24✔
216
    case TSDB_CODE_PAR_INVALID_PK_OP:
46✔
217
      return "primary key column can not be added, modified, and dropped";
46✔
218
    case TSDB_CODE_TSMA_NAME_TOO_LONG:
8✔
219
      return "Tsma name too long";
8✔
220
    case TSDB_CODE_PAR_TBNAME_ERROR:
×
221
      return "Pseudo tag tbname not set";
×
222
    case TSDB_CODE_PAR_TBNAME_DUPLICATED:
×
223
      return "Table name:%s duplicated";
×
224
    case TSDB_CODE_PAR_TAG_NAME_DUPLICATED:
×
225
      return "Tag name:%s duplicated";
×
226
    case TSDB_CODE_PAR_NOT_ALLOWED_DIFFERENT_BY_ROW_FUNC:
×
227
      return "Some functions cannot appear in the select list at the same time";
×
228
    case TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR:
×
229
      return "Syntax error in regular expression";
×
230
    default:
×
231
      return "Unknown error";
×
232
  }
233
}
234

235
int32_t generateSyntaxErrMsg(SMsgBuf* pBuf, int32_t errCode, ...) {
58,454✔
236
  va_list vArgList;
237
  va_start(vArgList, errCode);
58,454✔
238
  (void)vsnprintf(pBuf->buf, pBuf->len, getSyntaxErrFormat(errCode), vArgList);
58,454✔
239
  va_end(vArgList);
58,454✔
240
  return errCode;
58,454✔
241
}
242

243
int32_t generateSyntaxErrMsgExt(SMsgBuf* pBuf, int32_t errCode, const char* pFormat, ...) {
3,055✔
244
  va_list vArgList;
245
  va_start(vArgList, pFormat);
3,055✔
246
  (void)vsnprintf(pBuf->buf, pBuf->len, pFormat, vArgList);
3,055✔
247
  va_end(vArgList);
3,055✔
248
  return errCode;
3,055✔
249
}
250

251
int32_t buildInvalidOperationMsg(SMsgBuf* pBuf, const char* msg) {
427✔
252
  if (pBuf->buf) {
427!
253
    strncpy(pBuf->buf, msg, pBuf->len);
427✔
254
  }
255

256
  return TSDB_CODE_TSC_INVALID_OPERATION;
427✔
257
}
258

259
int32_t buildInvalidOperationMsgExt(SMsgBuf* pBuf, const char* pFormat, ...) {
34✔
260
  va_list vArgList;
261
  va_start(vArgList, pFormat);
34✔
262
  (void)vsnprintf(pBuf->buf, pBuf->len, pFormat, vArgList);
34✔
263
  va_end(vArgList);
34✔
264
  return TSDB_CODE_TSC_INVALID_OPERATION;
34✔
265
}
266

267
int32_t buildSyntaxErrMsg(SMsgBuf* pBuf, const char* additionalInfo, const char* sourceStr) {
33,943✔
268
  if (pBuf == NULL) return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
33,943✔
269
  const char* msgFormat1 = "syntax error near \'%s\'";
33,939✔
270
  const char* msgFormat2 = "syntax error near \'%s\' (%s)";
33,939✔
271
  const char* msgFormat3 = "%s";
33,939✔
272

273
  const char* prefix = "syntax error";
33,939✔
274
  if (sourceStr == NULL) {
33,939!
275
    snprintf(pBuf->buf, pBuf->len, msgFormat1, additionalInfo);
×
276
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
×
277
  }
278

279
  char buf[64] = {0};  // only extract part of sql string
33,939✔
280
  strncpy(buf, sourceStr, tListLen(buf) - 1);
33,939✔
281

282
  if (additionalInfo != NULL) {
33,939!
283
    snprintf(pBuf->buf, pBuf->len, msgFormat2, buf, additionalInfo);
33,939✔
284
  } else {
285
    const char* msgFormat = (0 == strncmp(sourceStr, prefix, strlen(prefix))) ? msgFormat3 : msgFormat1;
×
286
    snprintf(pBuf->buf, pBuf->len, msgFormat, buf);
×
287
  }
288

289
  return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
33,939✔
290
}
291

292
SSchema* getTableColumnSchema(const STableMeta* pTableMeta) { return (SSchema*)pTableMeta->schema; }
358,277,907✔
293

294
static SSchema* getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex) {
155,922✔
295
  SSchema* pSchema = (SSchema*)pTableMeta->schema;
155,922✔
296
  return &pSchema[colIndex];
155,922✔
297
}
298

299
SSchema* getTableTagSchema(const STableMeta* pTableMeta) {
155,932✔
300
  return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns);
155,932✔
301
}
302

303
int32_t getNumOfColumns(const STableMeta* pTableMeta) {
2,105,103✔
304
  // table created according to super table, use data from super table
305
  return getTableInfo(pTableMeta).numOfColumns;
2,105,103✔
306
}
307

308
int32_t getNumOfTags(const STableMeta* pTableMeta) { return getTableInfo(pTableMeta).numOfTags; }
174,284✔
309

310
STableComInfo getTableInfo(const STableMeta* pTableMeta) { return pTableMeta->tableInfo; }
1,696,191,869✔
311

312
int32_t getTableTypeFromTableNode(SNode* pTable) {
96,340✔
313
  if (NULL == pTable) {
96,340!
314
    return -1;
×
315
  }
316
  if (QUERY_NODE_REAL_TABLE != nodeType(pTable)) {
96,340✔
317
    return -1;
13,991✔
318
  }
319
  return ((SRealTableNode*)pTable)->pMeta->tableType;
82,349✔
320
}
321

322
STableMeta* tableMetaDup(const STableMeta* pTableMeta) {
11,944,150✔
323
  int32_t numOfFields = TABLE_TOTAL_COL_NUM(pTableMeta);
11,944,150✔
324
  if (numOfFields > TSDB_MAX_COLUMNS || numOfFields < TSDB_MIN_COLUMNS) {
11,944,150!
325
    return NULL;
×
326
  }
327

328
  bool   hasSchemaExt = pTableMeta->schemaExt == NULL ? false : true;
11,961,939✔
329
  size_t schemaExtSize = hasSchemaExt ? pTableMeta->tableInfo.numOfColumns * sizeof(SSchemaExt) : 0;
11,961,939✔
330

331
  size_t      size = sizeof(STableMeta) + numOfFields * sizeof(SSchema);
11,961,939✔
332
  STableMeta* p = taosMemoryMalloc(size + schemaExtSize);
11,961,939✔
333
  if (NULL == p) return NULL;
11,956,179!
334

335
  memcpy(p, pTableMeta, schemaExtSize + size);
11,956,179✔
336
  if (hasSchemaExt) {
11,956,179✔
337
    p->schemaExt = (SSchemaExt*)(((char*)p) + size);
2,887,406✔
338
  } else {
339
    p->schemaExt = NULL;
9,068,773✔
340
  }
341
  return p;
11,956,179✔
342
}
343

344
int32_t trimString(const char* src, int32_t len, char* dst, int32_t dlen) {
154,428,400✔
345
  if (len <= 0 || dlen <= 0) return 0;
154,428,400!
346

347
  char    delim = src[0];
154,511,859✔
348
  int32_t j = 0;
154,511,859✔
349
  for (uint32_t k = 1; k < len - 1; ++k) {
2,147,483,647✔
350
    if (j >= dlen) {
2,147,483,647✔
351
      dst[j - 1] = '\0';
3✔
352
      return j;
3✔
353
    }
354
    if (src[k] == delim && src[k + 1] == delim) {  // deal with "", ''
2,147,483,647!
355
      dst[j] = src[k + 1];
4✔
356
      j++;
4✔
357
      k++;
4✔
358
      continue;
4✔
359
    }
360

361
    if (src[k] == '\\') {  // deal with escape character
2,147,483,647✔
362
      if (src[k + 1] == 'n') {
578✔
363
        dst[j] = '\n';
1✔
364
      } else if (src[k + 1] == 'r') {
577!
365
        dst[j] = '\r';
×
366
      } else if (src[k + 1] == 't') {
577!
367
        dst[j] = '\t';
×
368
      } else if (src[k + 1] == '\\') {
577✔
369
        dst[j] = '\\';
17✔
370
      } else if (src[k + 1] == '\'') {
560✔
371
        dst[j] = '\'';
11✔
372
      } else if (src[k + 1] == '"') {
549✔
373
        dst[j] = '"';
75✔
374
      } else if (src[k + 1] == '%' || src[k + 1] == '_' || src[k + 1] == 'x') {
474✔
375
        dst[j++] = src[k];
285✔
376
        dst[j] = src[k + 1];
285✔
377
      } else {
378
        dst[j] = src[k + 1];
189✔
379
      }
380
      j++;
578✔
381
      k++;
578✔
382
      continue;
578✔
383
    }
384

385
    dst[j] = src[k];
2,147,483,647✔
386
    j++;
2,147,483,647✔
387
  }
388
  if (j >= dlen) j = dlen - 1;
154,511,856✔
389
  dst[j] = '\0';
154,511,856✔
390
  return j;
154,511,856✔
391
}
392

393
static bool isValidateTag(char* input) {
988✔
394
  if (!input) return false;
988!
395
  for (size_t i = 0; i < strlen(input); ++i) {
5,696✔
396
#ifdef WINDOWS
397
    if (input[i] < 0x20 || input[i] > 0x7E) return false;
398
#else
399
    if (isprint(input[i]) == 0) return false;
4,720✔
400
#endif
401
  }
402
  return true;
976✔
403
}
404

405
int32_t parseJsontoTagData(const char* json, SArray* pTagVals, STag** ppTag, void* pMsgBuf) {
1,034✔
406
  int32_t   retCode = TSDB_CODE_SUCCESS;
1,034✔
407
  cJSON*    root = NULL;
1,034✔
408
  SHashObj* keyHash = NULL;
1,034✔
409
  int32_t   size = 0;
1,034✔
410
  // set json NULL data
411
  if (!json || strcasecmp(json, TSDB_DATA_NULL_STR_L) == 0 || strtrim((char*)json) == 0) {
1,034!
412
    retCode = TSDB_CODE_SUCCESS;
40✔
413
    goto end;
40✔
414
  }
415

416
  // set json real data
417
  root = cJSON_Parse(json);
994✔
418
  if (root == NULL) {
994✔
419
    retCode = buildSyntaxErrMsg(pMsgBuf, "json parse error", json);
242✔
420
    goto end;
242✔
421
  }
422

423
  size = cJSON_GetArraySize(root);
752✔
424
  if (!cJSON_IsObject(root)) {
752✔
425
    retCode = buildSyntaxErrMsg(pMsgBuf, "json error invalide value", json);
250✔
426
    goto end;
250✔
427
  }
428

429
  keyHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false);
502✔
430
  if (!keyHash) {
502!
431
    retCode = terrno;
×
432
    goto end;
×
433
  }
434
  for (int32_t i = 0; i < size; i++) {
1,458✔
435
    cJSON* item = cJSON_GetArrayItem(root, i);
988✔
436
    if (!item) {
988!
437
      uError("json inner error:%d", i);
×
438
      retCode = buildSyntaxErrMsg(pMsgBuf, "json inner error", json);
×
439
      goto end;
32✔
440
    }
441

442
    char* jsonKey = item->string;
988✔
443
    if (!isValidateTag(jsonKey)) {
988✔
444
      retCode = buildSyntaxErrMsg(pMsgBuf, "json key not validate", jsonKey);
12✔
445
      goto end;
12✔
446
    }
447
    size_t keyLen = strlen(jsonKey);
976✔
448
    if (keyLen > TSDB_MAX_JSON_KEY_LEN) {
976✔
449
      uError("json key too long error");
4!
450
      retCode = buildSyntaxErrMsg(pMsgBuf, "json key too long, more than 256", jsonKey);
4✔
451
      goto end;
4✔
452
    }
453
    if (keyLen == 0 || taosHashGet(keyHash, jsonKey, keyLen) != NULL) {
972✔
454
      continue;
16✔
455
    }
456
    STagVal val = {0};
956✔
457
    //    strcpy(val.colName, colName);
458
    val.pKey = jsonKey;
956✔
459
    retCode = taosHashPut(keyHash, jsonKey, keyLen, &keyLen,
956✔
460
                          CHAR_BYTES);  // add key to hash to remove dumplicate, value is useless
461
    if (TSDB_CODE_SUCCESS != retCode) {
956!
462
      goto end;
×
463
    }
464

465
    if (item->type == cJSON_String) {  // add json value  format: type|data
956✔
466
      char*   jsonValue = item->valuestring;
817✔
467
      int32_t valLen = (int32_t)strlen(jsonValue);
817✔
468
      char*   tmp = taosMemoryCalloc(1, valLen * TSDB_NCHAR_SIZE);
817✔
469
      if (!tmp) {
817!
470
        retCode = terrno;
×
471
        goto end;
×
472
      }
473
      val.type = TSDB_DATA_TYPE_NCHAR;
817✔
474
      if (valLen > 0 && !taosMbsToUcs4(jsonValue, valLen, (TdUcs4*)tmp, (int32_t)(valLen * TSDB_NCHAR_SIZE), &valLen)) {
817!
475
        uError("charset:%s to %s. val:%s, errno:%s, convert failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, jsonValue,
×
476
               strerror(terrno));
477
        retCode = buildSyntaxErrMsg(pMsgBuf, "charset convert json error", jsonValue);
×
478
        taosMemoryFree(tmp);
×
479
        goto end;
×
480
      }
481
      val.nData = valLen;
817✔
482
      val.pData = tmp;
817✔
483
    } else if (item->type == cJSON_Number) {
139✔
484
      if (!isfinite(item->valuedouble)) {
83✔
485
        uError("json value is invalidate");
8!
486
        retCode = buildSyntaxErrMsg(pMsgBuf, "json value number is illegal", json);
8✔
487
        goto end;
8✔
488
      }
489
      val.type = TSDB_DATA_TYPE_DOUBLE;
75✔
490
      *((double*)&(val.i64)) = item->valuedouble;
75✔
491
    } else if (item->type == cJSON_True || item->type == cJSON_False) {
56✔
492
      val.type = TSDB_DATA_TYPE_BOOL;
24✔
493
      *((char*)&(val.i64)) = (char)(item->valueint);
24✔
494
    } else if (item->type == cJSON_NULL) {
32✔
495
      val.type = TSDB_DATA_TYPE_NULL;
24✔
496
    } else {
497
      retCode = buildSyntaxErrMsg(pMsgBuf, "invalidate json value", json);
8✔
498
      goto end;
8✔
499
    }
500
    if (NULL == taosArrayPush(pTagVals, &val)) {
940!
501
      retCode = terrno;
×
502
      goto end;
×
503
    }
504
  }
505

506
end:
470✔
507
  taosHashCleanup(keyHash);
1,034✔
508
  if (retCode == TSDB_CODE_SUCCESS) {
1,034✔
509
    retCode = tTagNew(pTagVals, 1, true, ppTag);
510✔
510
  }
511
  for (int i = 0; i < taosArrayGetSize(pTagVals); ++i) {
1,974✔
512
    STagVal* p = (STagVal*)taosArrayGet(pTagVals, i);
940✔
513
    if (IS_VAR_DATA_TYPE(p->type)) {
940!
514
      taosMemoryFreeClear(p->pData);
817!
515
    }
516
  }
517
  cJSON_Delete(root);
1,034✔
518
  return retCode;
1,034✔
519
}
520

521
static int32_t getInsTagsTableTargetNameFromOp(int32_t acctId, SOperatorNode* pOper, SName* pName) {
21,693✔
522
  if (OP_TYPE_EQUAL != pOper->opType) {
21,693✔
523
    return TSDB_CODE_SUCCESS;
13,383✔
524
  }
525

526
  SColumnNode* pCol = NULL;
8,310✔
527
  SValueNode*  pVal = NULL;
8,310✔
528
  if (QUERY_NODE_COLUMN == nodeType(pOper->pLeft)) {
8,310!
529
    pCol = (SColumnNode*)pOper->pLeft;
8,310✔
530
  } else if (QUERY_NODE_VALUE == nodeType(pOper->pLeft)) {
×
531
    pVal = (SValueNode*)pOper->pLeft;
×
532
  }
533
  if (QUERY_NODE_COLUMN == nodeType(pOper->pRight)) {
8,310!
534
    pCol = (SColumnNode*)pOper->pRight;
×
535
  } else if (QUERY_NODE_VALUE == nodeType(pOper->pRight)) {
8,310!
536
    pVal = (SValueNode*)pOper->pRight;
8,310✔
537
  }
538
  if (NULL == pCol || NULL == pVal || NULL == pVal->literal || 0 == strcmp(pVal->literal, "")) {
8,310!
539
    return TSDB_CODE_SUCCESS;
2✔
540
  }
541

542
  if (0 == strcmp(pCol->colName, "db_name")) {
8,308✔
543
    return tNameSetDbName(pName, acctId, pVal->literal, strlen(pVal->literal));
4,023✔
544
  } else if (0 == strcmp(pCol->colName, "table_name")) {
4,285✔
545
    return tNameAddTbName(pName, pVal->literal, strlen(pVal->literal));
4,211✔
546
  }
547

548
  return TSDB_CODE_SUCCESS;
74✔
549
}
550

551
static int32_t getInsTagsTableTargetObjName(int32_t acctId, SNode* pNode, SName* pName) {
5,110✔
552
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
5,110!
553
    return getInsTagsTableTargetNameFromOp(acctId, (SOperatorNode*)pNode, pName);
5,110✔
554
  }
555
  return TSDB_CODE_SUCCESS;
×
556
}
557

558
static int32_t getInsTagsTableTargetNameFromCond(int32_t acctId, SLogicConditionNode* pCond, SName* pName) {
2,536✔
559
  if (LOGIC_COND_TYPE_AND != pCond->condType) {
2,536!
560
    return TSDB_CODE_SUCCESS;
×
561
  }
562

563
  SNode* pNode = NULL;
2,536✔
564
  FOREACH(pNode, pCond->pParameterList) {
7,646!
565
    int32_t code = getInsTagsTableTargetObjName(acctId, pNode, pName);
5,110✔
566
    if (TSDB_CODE_SUCCESS != code) {
5,110!
567
      return code;
×
568
    }
569
  }
570
  if ('\0' == pName->dbname[0]) {
2,536!
571
    pName->type = 0;
×
572
  }
573
  return TSDB_CODE_SUCCESS;
2,536✔
574
}
575

576
int32_t getVnodeSysTableTargetName(int32_t acctId, SNode* pWhere, SName* pName) {
19,273✔
577
  if (NULL == pWhere) {
19,273✔
578
    return TSDB_CODE_SUCCESS;
154✔
579
  }
580

581
  if (QUERY_NODE_OPERATOR == nodeType(pWhere)) {
19,119✔
582
    int32_t code = getInsTagsTableTargetNameFromOp(acctId, (SOperatorNode*)pWhere, pName);
16,583✔
583
    if (TSDB_CODE_SUCCESS == code && '\0' == pName->dbname[0]) {
16,583!
584
      pName->type = 0;
15,096✔
585
    }
586
    return code;
16,583✔
587
  }
588

589
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pWhere)) {
2,536!
590
    return getInsTagsTableTargetNameFromCond(acctId, (SLogicConditionNode*)pWhere, pName);
2,536✔
591
  }
592

593
  return TSDB_CODE_SUCCESS;
×
594
}
595

596
static int32_t userAuthToString(int32_t acctId, const char* pUser, const char* pDb, const char* pTable, AUTH_TYPE type,
2,563,119✔
597
                                char* pStr, bool isView) {
598
  return sprintf(pStr, "%s*%d*%s*%s*%d*%d", pUser, acctId, pDb, (NULL == pTable || '\0' == pTable[0]) ? "``" : pTable,
2,563,119✔
599
                 type, isView);
600
}
601

602
static int32_t getIntegerFromAuthStr(const char* pStart, char** pNext) {
3,698,065✔
603
  char* p = strchr(pStart, '*');
3,698,065✔
604
  char  buf[10] = {0};
3,698,065✔
605
  if (NULL == p) {
3,698,065✔
606
    strcpy(buf, pStart);
1,232,688✔
607
    *pNext = NULL;
1,232,688✔
608
  } else {
609
    strncpy(buf, pStart, p - pStart);
2,465,377✔
610
    *pNext = ++p;
2,465,377✔
611
  }
612
  return taosStr2Int32(buf, NULL, 10);
3,698,065✔
613
}
614

615
static void getStringFromAuthStr(const char* pStart, char* pStr, char** pNext) {
3,698,092✔
616
  char* p = strchr(pStart, '*');
3,698,092✔
617
  if (NULL == p) {
3,698,092!
618
    strcpy(pStr, pStart);
×
619
    *pNext = NULL;
×
620
  } else {
621
    strncpy(pStr, pStart, p - pStart);
3,698,092✔
622
    *pNext = ++p;
3,698,092✔
623
  }
624
  if (*pStart == '`' && *(pStart + 1) == '`') {
3,698,092!
625
    *pStr = 0;
70,974✔
626
  }
627
}
3,698,092✔
628

629
static void stringToUserAuth(const char* pStr, int32_t len, SUserAuthInfo* pUserAuth) {
1,232,708✔
630
  char* p = NULL;
1,232,708✔
631
  getStringFromAuthStr(pStr, pUserAuth->user, &p);
1,232,708✔
632
  pUserAuth->tbName.acctId = getIntegerFromAuthStr(p, &p);
1,232,699✔
633
  getStringFromAuthStr(p, pUserAuth->tbName.dbname, &p);
1,232,710✔
634
  getStringFromAuthStr(p, pUserAuth->tbName.tname, &p);
1,232,708✔
635
  if (pUserAuth->tbName.tname[0]) {
1,232,712✔
636
    pUserAuth->tbName.type = TSDB_TABLE_NAME_T;
1,161,734✔
637
  } else {
638
    pUserAuth->tbName.type = TSDB_DB_NAME_T;
70,978✔
639
  }
640
  pUserAuth->type = getIntegerFromAuthStr(p, &p);
1,232,712✔
641
  pUserAuth->isView = getIntegerFromAuthStr(p, &p);
1,232,710✔
642
}
1,232,698✔
643

644
static int32_t buildTableReq(SHashObj* pTablesHash, SArray** pTables) {
6,762,686✔
645
  if (NULL != pTablesHash) {
6,762,686✔
646
    *pTables = taosArrayInit(taosHashGetSize(pTablesHash), sizeof(SName));
4,312,307✔
647
    if (NULL == *pTables) {
4,312,160!
648
      return terrno;
×
649
    }
650
    void* p = taosHashIterate(pTablesHash, NULL);
4,312,160✔
651
    while (NULL != p) {
9,246,267✔
652
      size_t len = 0;
4,934,000✔
653
      char*  pKey = taosHashGetKey(p, &len);
4,934,000✔
654
      char   fullName[TSDB_TABLE_FNAME_LEN] = {0};
4,933,973✔
655
      strncpy(fullName, pKey, len);
4,933,973✔
656
      SName   name = {0};
4,933,973✔
657
      int32_t code = tNameFromString(&name, fullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
4,933,973✔
658
      if (TSDB_CODE_SUCCESS == code) {
4,934,010✔
659
        if (NULL == taosArrayPush(*pTables, &name)) {
9,867,938!
660
          code = terrno;
×
661
        }
662
      }
663
      if (TSDB_CODE_SUCCESS != code) {
4,933,946!
664
        taosHashCancelIterate(pTablesHash, p);
×
665
        taosArrayDestroy(*pTables);
×
666
        *pTables = NULL;
×
667
        return code;
×
668
      }
669
      p = taosHashIterate(pTablesHash, p);
4,933,946✔
670
    }
671
  }
672
  return TSDB_CODE_SUCCESS;
6,762,646✔
673
}
674

675
static int32_t buildDbReq(SHashObj* pDbsHash, SArray** pDbs) {
3,686,004✔
676
  if (NULL != pDbsHash) {
3,686,004✔
677
    *pDbs = taosArrayInit(taosHashGetSize(pDbsHash), TSDB_DB_FNAME_LEN);
1,126,224✔
678
    if (NULL == *pDbs) {
1,126,215!
679
      return terrno;
×
680
    }
681
    void* p = taosHashIterate(pDbsHash, NULL);
1,126,215✔
682
    while (NULL != p) {
2,254,016✔
683
      size_t len = 0;
1,127,770✔
684
      char*  pKey = taosHashGetKey(p, &len);
1,127,770✔
685
      char   fullName[TSDB_DB_FNAME_LEN] = {0};
1,127,776✔
686
      strncpy(fullName, pKey, len);
1,127,776✔
687
      if (NULL == taosArrayPush(*pDbs, fullName)) {
2,255,547!
688
        taosHashCancelIterate(pDbsHash, p);
×
689
        taosArrayDestroy(*pDbs);
×
690
        *pDbs = NULL;
×
691
        return terrno;
×
692
      }
693
      p = taosHashIterate(pDbsHash, p);
1,127,771✔
694
    }
695
  }
696
  return TSDB_CODE_SUCCESS;
3,686,026✔
697
}
698

699
static int32_t buildTableReqFromDb(SHashObj* pDbsHash, SArray** pDbs) {
7,371,910✔
700
  if (NULL != pDbsHash) {
7,371,910✔
701
    if (NULL == *pDbs) {
4,297,438!
702
      *pDbs = taosArrayInit(taosHashGetSize(pDbsHash), sizeof(STablesReq));
4,297,452✔
703
      if (NULL == *pDbs) {
4,297,372!
704
        return terrno;
×
705
      }
706
    }
707
    SParseTablesMetaReq* p = taosHashIterate(pDbsHash, NULL);
4,297,358✔
708
    while (NULL != p) {
8,602,925✔
709
      STablesReq req = {0};
4,305,428✔
710
      strcpy(req.dbFName, p->dbFName);
4,305,428✔
711
      int32_t code = buildTableReq(p->pTables, &req.pTables);
4,305,428✔
712
      if (TSDB_CODE_SUCCESS == code) {
4,305,403!
713
        if (NULL == taosArrayPush(*pDbs, &req)) {
8,610,814!
714
          code = terrno;
×
715
        }
716
      }
717
      if (TSDB_CODE_SUCCESS != code) {
4,305,387!
718
        taosHashCancelIterate(pDbsHash, p);
×
719
        taosArrayDestroy(*pDbs);
×
720
        *pDbs = NULL;
×
721
        return code;
×
722
      }
723
      p = taosHashIterate(pDbsHash, p);
4,305,387✔
724
    }
725
  }
726
  return TSDB_CODE_SUCCESS;
7,371,969✔
727
}
728

729
static int32_t buildUserAuthReq(SHashObj* pUserAuthHash, SArray** pUserAuth) {
1,228,671✔
730
  if (NULL != pUserAuthHash) {
1,228,671✔
731
    *pUserAuth = taosArrayInit(taosHashGetSize(pUserAuthHash), sizeof(SUserAuthInfo));
1,085,084✔
732
    if (NULL == *pUserAuth) {
1,085,078!
733
      return terrno;
×
734
    }
735
    void* p = taosHashIterate(pUserAuthHash, NULL);
1,085,078✔
736
    while (NULL != p) {
2,317,805✔
737
      size_t len = 0;
1,232,714✔
738
      char*  pKey = taosHashGetKey(p, &len);
1,232,714✔
739
      char   key[USER_AUTH_KEY_MAX_LEN] = {0};
1,232,713✔
740
      strncpy(key, pKey, len);
1,232,713✔
741
      SUserAuthInfo userAuth = {0};
1,232,713✔
742
      stringToUserAuth(key, len, &userAuth);
1,232,713✔
743
      if (NULL == taosArrayPush(*pUserAuth, &userAuth)) {
2,465,397!
744
        taosHashCancelIterate(pUserAuthHash, p);
×
745
        taosArrayDestroy(*pUserAuth);
×
746
        *pUserAuth = NULL;
×
747
        return terrno;
×
748
      }
749
      p = taosHashIterate(pUserAuthHash, p);
1,232,698✔
750
    }
751
  }
752
  return TSDB_CODE_SUCCESS;
1,228,678✔
753
}
754

755
static int32_t buildUdfReq(SHashObj* pUdfHash, SArray** pUdf) {
1,228,653✔
756
  if (NULL != pUdfHash) {
1,228,653✔
757
    *pUdf = taosArrayInit(taosHashGetSize(pUdfHash), TSDB_FUNC_NAME_LEN);
924✔
758
    if (NULL == *pUdf) {
924!
759
      return terrno;
×
760
    }
761
    void* p = taosHashIterate(pUdfHash, NULL);
924✔
762
    while (NULL != p) {
2,168✔
763
      size_t len = 0;
1,244✔
764
      char*  pFunc = taosHashGetKey(p, &len);
1,244✔
765
      char   func[TSDB_FUNC_NAME_LEN] = {0};
1,244✔
766
      strncpy(func, pFunc, len);
1,244✔
767
      if (NULL == taosArrayPush(*pUdf, func)) {
2,488!
768
        taosHashCancelIterate(pUdfHash, p);
×
769
        taosArrayDestroy(*pUdf);
×
770
        *pUdf = NULL;
×
771
        return terrno;
×
772
      }
773
      p = taosHashIterate(pUdfHash, p);
1,244✔
774
    }
775
  }
776
  return TSDB_CODE_SUCCESS;
1,228,653✔
777
}
778

779
int32_t buildCatalogReq(const SParseMetaCache* pMetaCache, SCatalogReq* pCatalogReq) {
1,228,643✔
780
  int32_t code = buildTableReqFromDb(pMetaCache->pTableMeta, &pCatalogReq->pTableMeta);
1,228,643✔
781
  if (TSDB_CODE_SUCCESS == code) {
1,228,684!
782
    code = buildDbReq(pMetaCache->pDbVgroup, &pCatalogReq->pDbVgroup);
1,228,690✔
783
  }
784
  if (TSDB_CODE_SUCCESS == code) {
1,228,678!
785
    code = buildTableReqFromDb(pMetaCache->pTableVgroup, &pCatalogReq->pTableHash);
1,228,687✔
786
  }
787
  if (TSDB_CODE_SUCCESS == code) {
1,228,679!
788
    code = buildDbReq(pMetaCache->pDbCfg, &pCatalogReq->pDbCfg);
1,228,689✔
789
  }
790
  if (TSDB_CODE_SUCCESS == code) {
1,228,679✔
791
    code = buildDbReq(pMetaCache->pDbInfo, &pCatalogReq->pDbInfo);
1,228,673✔
792
  }
793
  if (TSDB_CODE_SUCCESS == code) {
1,228,688✔
794
    code = buildUserAuthReq(pMetaCache->pUserAuth, &pCatalogReq->pUser);
1,228,685✔
795
  }
796
  if (TSDB_CODE_SUCCESS == code) {
1,228,682✔
797
    code = buildUdfReq(pMetaCache->pUdf, &pCatalogReq->pUdf);
1,228,681✔
798
  }
799
  if (TSDB_CODE_SUCCESS == code) {
1,228,661!
800
    code = buildTableReq(pMetaCache->pTableIndex, &pCatalogReq->pTableIndex);
1,228,667✔
801
  }
802
  if (TSDB_CODE_SUCCESS == code) {
1,228,667!
803
    code = buildTableReq(pMetaCache->pTableCfg, &pCatalogReq->pTableCfg);
1,228,669✔
804
  }
805
  if (TSDB_CODE_SUCCESS == code) {
1,228,672!
806
    code = buildTableReqFromDb(pMetaCache->pTableMeta, &pCatalogReq->pTableTSMAs);
1,228,676✔
807
  }
808
  if (TSDB_CODE_SUCCESS == code) {
1,228,678!
809
    code = buildTableReqFromDb(pMetaCache->pTSMAs, &pCatalogReq->pTSMAs);
1,228,681✔
810
  }
811
  if (TSDB_CODE_SUCCESS == code) {
1,228,680!
812
    code = buildTableReqFromDb(pMetaCache->pTableName, &pCatalogReq->pTableName);
1,228,683✔
813
  }
814
#ifdef TD_ENTERPRISE
815
  if (TSDB_CODE_SUCCESS == code) {
1,228,676!
816
    code = buildTableReqFromDb(pMetaCache->pTableMeta, &pCatalogReq->pView);
1,228,680✔
817
  }
818
#endif
819
  pCatalogReq->dNodeRequired = pMetaCache->dnodeRequired;
1,228,685✔
820
  pCatalogReq->forceFetchViewMeta = pMetaCache->forceFetchViewMeta;
1,228,685✔
821
  return code;
1,228,685✔
822
}
823

824
int32_t createSelectStmtImpl(bool isDistinct, SNodeList* pProjectionList, SNode* pTable, SNodeList* pHint,
1,406,133✔
825
                             SNode** ppSelect) {
826
  SSelectStmt* select = NULL;
1,406,133✔
827
  int32_t      code = nodesMakeNode(QUERY_NODE_SELECT_STMT, (SNode**)&select);
1,406,133✔
828
  if (NULL == select) {
1,406,151!
829
    return code;
×
830
  }
831
  select->isDistinct = isDistinct;
1,406,151✔
832
  select->pProjectionList = pProjectionList;
1,406,151✔
833
  select->pFromTable = pTable;
1,406,151✔
834
  sprintf(select->stmtName, "%p", select);
1,406,151✔
835
  select->timeLineResMode = select->isDistinct ? TIME_LINE_NONE : TIME_LINE_GLOBAL;
1,406,151✔
836
  select->timeLineCurMode = TIME_LINE_GLOBAL;
1,406,151✔
837
  select->onlyHasKeepOrderFunc = true;
1,406,151✔
838
  select->timeRange = TSWINDOW_INITIALIZER;
1,406,151✔
839
  select->pHint = pHint;
1,406,151✔
840
  select->lastProcessByRowFuncId = -1;
1,406,151✔
841
  *ppSelect = (SNode*)select;
1,406,151✔
842
  return code;
1,406,151✔
843
}
844

845
static int32_t putMetaDataToHash(const char* pKey, int32_t len, const SArray* pData, int32_t index, SHashObj** pHash) {
6,093,036✔
846
  if (NULL == *pHash) {
6,093,036✔
847
    *pHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
5,459,107✔
848
    if (NULL == *pHash) {
5,458,938!
849
      return terrno;
×
850
    }
851
  }
852
  SMetaRes* pRes = taosArrayGet(pData, index);
6,092,867✔
853
  return taosHashPut(*pHash, pKey, len, &pRes, POINTER_BYTES);
6,092,896✔
854
}
855

856
int32_t getMetaDataFromHash(const char* pKey, int32_t len, SHashObj* pHash, void** pOutput) {
3,688,389✔
857
  SMetaRes** pRes = taosHashGet(pHash, pKey, len);
3,688,389✔
858
  if (NULL == pRes || NULL == *pRes) {
3,688,206!
859
    return TSDB_CODE_PAR_INTERNAL_ERROR;
23,758✔
860
  }
861
  if (TSDB_CODE_SUCCESS == (*pRes)->code) {
3,664,448✔
862
    *pOutput = (*pRes)->pRes;
3,640,178✔
863
  }
864
  return (*pRes)->code;
3,664,448✔
865
}
866

867
static int32_t putTableDataToCache(const SArray* pTableReq, const SArray* pTableData, SHashObj** pTable) {
2,459,204✔
868
  int32_t ntables = taosArrayGetSize(pTableReq);
2,459,204✔
869
  for (int32_t i = 0; i < ntables; ++i) {
2,466,649✔
870
    char    fullName[TSDB_TABLE_FNAME_LEN];
871
    int32_t code = tNameExtractFullName(taosArrayGet(pTableReq, i), fullName);
7,449✔
872
    if (TSDB_CODE_SUCCESS != code) {
7,449!
873
      return code;
×
874
    }
875
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(fullName, strlen(fullName), pTableData, i, pTable)) {
7,449!
876
      return TSDB_CODE_OUT_OF_MEMORY;
×
877
    }
878
  }
879
  return TSDB_CODE_SUCCESS;
2,459,200✔
880
}
881

882
static int32_t putDbDataToCache(const SArray* pDbReq, const SArray* pDbData, SHashObj** pDb) {
3,688,816✔
883
  int32_t nvgs = taosArrayGetSize(pDbReq);
3,688,816✔
884
  for (int32_t i = 0; i < nvgs; ++i) {
4,818,019✔
885
    char* pDbFName = taosArrayGet(pDbReq, i);
1,129,202✔
886
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(pDbFName, strlen(pDbFName), pDbData, i, pDb)) {
1,129,195!
887
      return TSDB_CODE_OUT_OF_MEMORY;
×
888
    }
889
  }
890
  return TSDB_CODE_SUCCESS;
3,688,817✔
891
}
892

893
static int32_t putDbTableDataToCache(const SArray* pDbReq, const SArray* pTableData, SHashObj** pTable) {
7,377,534✔
894
  if (!pTableData || pTableData->size == 0) return TSDB_CODE_SUCCESS;
7,377,534✔
895
  int32_t ndbs = taosArrayGetSize(pDbReq);
4,314,659✔
896
  int32_t tableNo = 0;
4,314,649✔
897
  for (int32_t i = 0; i < ndbs; ++i) {
7,560,599✔
898
    STablesReq* pReq = taosArrayGet(pDbReq, i);
3,245,921✔
899
    int32_t     ntables = taosArrayGetSize(pReq->pTables);
3,245,893✔
900
    for (int32_t j = 0; j < ntables; ++j) {
6,967,653✔
901
      char    fullName[TSDB_TABLE_FNAME_LEN];
902
      int32_t code = tNameExtractFullName(taosArrayGet(pReq->pTables, j), fullName);
3,721,703✔
903
      if (TSDB_CODE_SUCCESS != code) {
3,721,778!
904
        return code;
×
905
      }
906
      if (TSDB_CODE_SUCCESS != putMetaDataToHash(fullName, strlen(fullName), pTableData, tableNo, pTable)) {
3,721,778!
907
        return TSDB_CODE_OUT_OF_MEMORY;
×
908
      }
909
      ++tableNo;
3,721,781✔
910
    }
911
  }
912
  return TSDB_CODE_SUCCESS;
4,314,678✔
913
}
914

915
static int32_t putUserAuthToCache(const SArray* pUserAuthReq, const SArray* pUserAuthData, SHashObj** pUserAuth) {
1,229,614✔
916
  int32_t nvgs = taosArrayGetSize(pUserAuthReq);
1,229,614✔
917
  for (int32_t i = 0; i < nvgs; ++i) {
2,463,085✔
918
    SUserAuthInfo* pUser = taosArrayGet(pUserAuthReq, i);
1,233,462✔
919
    char           key[USER_AUTH_KEY_MAX_LEN] = {0};
1,233,462✔
920
    int32_t        len = userAuthToString(pUser->tbName.acctId, pUser->user, pUser->tbName.dbname, pUser->tbName.tname,
1,233,462✔
921
                                          pUser->type, key, pUser->isView);
1,233,462✔
922
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(key, len, pUserAuthData, i, pUserAuth)) {
1,233,470!
923
      return TSDB_CODE_OUT_OF_MEMORY;
×
924
    }
925
  }
926
  return TSDB_CODE_SUCCESS;
1,229,623✔
927
}
928

929
static int32_t putUdfToCache(const SArray* pUdfReq, const SArray* pUdfData, SHashObj** pUdf) {
1,229,618✔
930
  int32_t num = taosArrayGetSize(pUdfReq);
1,229,618✔
931
  for (int32_t i = 0; i < num; ++i) {
1,230,861✔
932
    char* pFunc = taosArrayGet(pUdfReq, i);
1,244✔
933
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(pFunc, strlen(pFunc), pUdfData, i, pUdf)) {
1,244!
934
      return TSDB_CODE_OUT_OF_MEMORY;
×
935
    }
936
  }
937
  return TSDB_CODE_SUCCESS;
1,229,617✔
938
}
939

940
int32_t putMetaDataToCache(const SCatalogReq* pCatalogReq, const SMetaData* pMetaData, SParseMetaCache* pMetaCache) {
1,229,608✔
941
  int32_t code = putDbTableDataToCache(pCatalogReq->pTableMeta, pMetaData->pTableMeta, &pMetaCache->pTableMeta);
1,229,608✔
942
  if (TSDB_CODE_SUCCESS == code) {
1,229,615!
943
    code = putDbDataToCache(pCatalogReq->pDbVgroup, pMetaData->pDbVgroup, &pMetaCache->pDbVgroup);
1,229,615✔
944
  }
945
  if (TSDB_CODE_SUCCESS == code) {
1,229,616!
946
    code = putDbTableDataToCache(pCatalogReq->pTableHash, pMetaData->pTableHash, &pMetaCache->pTableVgroup);
1,229,617✔
947
  }
948
  if (TSDB_CODE_SUCCESS == code) {
1,229,617!
949
    code = putDbDataToCache(pCatalogReq->pDbCfg, pMetaData->pDbCfg, &pMetaCache->pDbCfg);
1,229,619✔
950
  }
951
  if (TSDB_CODE_SUCCESS == code) {
1,229,613!
952
    code = putDbDataToCache(pCatalogReq->pDbInfo, pMetaData->pDbInfo, &pMetaCache->pDbInfo);
1,229,615✔
953
  }
954
  if (TSDB_CODE_SUCCESS == code) {
1,229,609!
955
    code = putUserAuthToCache(pCatalogReq->pUser, pMetaData->pUser, &pMetaCache->pUserAuth);
1,229,613✔
956
  }
957
  if (TSDB_CODE_SUCCESS == code) {
1,229,614!
958
    code = putUdfToCache(pCatalogReq->pUdf, pMetaData->pUdfList, &pMetaCache->pUdf);
1,229,619✔
959
  }
960
  if (TSDB_CODE_SUCCESS == code) {
1,229,611!
961
    code = putTableDataToCache(pCatalogReq->pTableIndex, pMetaData->pTableIndex, &pMetaCache->pTableIndex);
1,229,617✔
962
  }
963
  if (TSDB_CODE_SUCCESS == code) {
1,229,606!
964
    code = putTableDataToCache(pCatalogReq->pTableCfg, pMetaData->pTableCfg, &pMetaCache->pTableCfg);
1,229,609✔
965
  }
966
  if (TSDB_CODE_SUCCESS == code) {
1,229,608!
967
    code = putDbTableDataToCache(pCatalogReq->pTableTSMAs, pMetaData->pTableTsmas, &pMetaCache->pTableTSMAs);
1,229,611✔
968
  }
969
  if (TSDB_CODE_SUCCESS == code) {
1,229,604!
970
    code = putDbTableDataToCache(pCatalogReq->pTSMAs, pMetaData->pTsmas, &pMetaCache->pTSMAs);
1,229,609✔
971
  }
972
  if (TSDB_CODE_SUCCESS == code) {
1,229,598!
973
    code = putDbTableDataToCache(pCatalogReq->pTableName, pMetaData->pTableMeta, &pMetaCache->pTableName);
1,229,604✔
974
  }
975
#ifdef TD_ENTERPRISE
976
  if (TSDB_CODE_SUCCESS == code) {
1,229,600!
977
    code = putDbTableDataToCache(pCatalogReq->pView, pMetaData->pView, &pMetaCache->pViews);
1,229,607✔
978
  }
979
#endif
980
  pMetaCache->pDnodes = pMetaData->pDnodeList;
1,229,618✔
981
  return code;
1,229,618✔
982
}
983

984
static int32_t reserveTableReqInCacheImpl(const char* pTbFName, int32_t len, SHashObj** pTables) {
2,660,661✔
985
  if (NULL == *pTables) {
2,660,661✔
986
    *pTables = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
2,173,353✔
987
    if (NULL == *pTables) {
2,173,331!
988
      return terrno;
×
989
    }
990
  }
991
  return taosHashPut(*pTables, pTbFName, len, &nullPointer, POINTER_BYTES);
2,660,639✔
992
}
993

994
static int32_t reserveTableReqInCache(int32_t acctId, const char* pDb, const char* pTable, SHashObj** pTables) {
2,660,545✔
995
  char    fullName[TSDB_TABLE_FNAME_LEN];
996
  int32_t len = tsnprintf(fullName, sizeof(fullName), "%d.%s.%s", acctId, pDb, pTable);
2,660,545✔
997
  return reserveTableReqInCacheImpl(fullName, len, pTables);
2,660,702✔
998
}
999

1000
static int32_t reserveTableReqInDbCacheImpl(int32_t acctId, const char* pDb, const char* pTable, SHashObj* pDbs) {
2,154,474✔
1001
  SParseTablesMetaReq req = {0};
2,154,474✔
1002
  int32_t             len = tsnprintf(req.dbFName, sizeof(req.dbFName), "%d.%s", acctId, pDb);
2,154,474✔
1003
  int32_t             code = reserveTableReqInCache(acctId, pDb, pTable, &req.pTables);
2,154,529✔
1004
  if (TSDB_CODE_SUCCESS == code) {
2,154,526!
1005
    code = taosHashPut(pDbs, req.dbFName, len, &req, sizeof(SParseTablesMetaReq));
2,154,530✔
1006
  }
1007
  return code;
2,154,534✔
1008
}
1009

1010
static int32_t reserveTableReqInDbCache(int32_t acctId, const char* pDb, const char* pTable, SHashObj** pDbs) {
2,639,835✔
1011
  if (NULL == *pDbs) {
2,639,835✔
1012
    *pDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
2,150,577✔
1013
    if (NULL == *pDbs) {
2,150,587!
1014
      return terrno;
×
1015
    }
1016
  }
1017
  char                 fullName[TSDB_DB_FNAME_LEN];
1018
  int32_t              len = tsnprintf(fullName, sizeof(fullName), "%d.%s", acctId, pDb);
2,639,845✔
1019
  SParseTablesMetaReq* pReq = taosHashGet(*pDbs, fullName, len);
2,639,993✔
1020
  if (NULL == pReq) {
2,639,883✔
1021
    return reserveTableReqInDbCacheImpl(acctId, pDb, pTable, *pDbs);
2,154,498✔
1022
  }
1023
  return reserveTableReqInCache(acctId, pDb, pTable, &pReq->pTables);
485,385✔
1024
}
1025

1026
int32_t reserveTableMetaInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
1,318,179✔
1027
  return reserveTableReqInDbCache(acctId, pDb, pTable, &pMetaCache->pTableMeta);
1,318,179✔
1028
}
1029

1030
int32_t reserveTableMetaInCacheExt(const SName* pName, SParseMetaCache* pMetaCache) {
×
1031
  return reserveTableReqInDbCache(pName->acctId, pName->dbname, pName->tname, &pMetaCache->pTableMeta);
×
1032
}
1033

1034
int32_t reserveTableUidInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
1,319✔
1035
  return reserveTableReqInDbCache(acctId, pDb, pTable, &pMetaCache->pTableName);
1,319✔
1036
}
1037

1038
int32_t getTableMetaFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta) {
2,261,235✔
1039
  char    fullName[TSDB_TABLE_FNAME_LEN];
1040
  int32_t code = tNameExtractFullName(pName, fullName);
2,261,235✔
1041
  if (TSDB_CODE_SUCCESS != code) {
2,261,277!
1042
    return code;
×
1043
  }
1044
  STableMeta* pTableMeta = NULL;
2,261,277✔
1045
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableMeta, (void**)&pTableMeta);
2,261,277✔
1046
  if (TSDB_CODE_SUCCESS == code) {
2,261,223✔
1047
    *pMeta = tableMetaDup(pTableMeta);
2,247,948✔
1048
    if (NULL == *pMeta) {
2,247,901!
1049
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1050
    }
1051
  }
1052
  return code;
2,261,176✔
1053
}
1054

1055
int32_t getTableNameFromCache(SParseMetaCache* pMetaCache, const SName* pName, char* pTbName) {
703✔
1056
  char    fullName[TSDB_TABLE_FNAME_LEN];
1057
  int32_t code = tNameExtractFullName(pName, fullName);
703✔
1058
  if (TSDB_CODE_SUCCESS != code) {
703!
1059
    return code;
×
1060
  }
1061
  const STableMeta* pMeta = NULL;
703✔
1062
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableName, (void**)&pMeta);
703✔
1063
  if (TSDB_CODE_SUCCESS == code) {
703✔
1064
    if (!pMeta) code = TSDB_CODE_PAR_INTERNAL_ERROR;
683!
1065
    int32_t metaSize =
683✔
1066
        sizeof(STableMeta) + sizeof(SSchema) * (pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags);
683✔
1067
    int32_t schemaExtSize =
683!
1068
        (useCompress(pMeta->tableType) && pMeta->schemaExt) ? sizeof(SSchemaExt) * pMeta->tableInfo.numOfColumns : 0;
1,366!
1069
    const char* pTableName = (const char*)pMeta + metaSize + schemaExtSize;
683✔
1070
    tstrncpy(pTbName, pTableName, TSDB_TABLE_NAME_LEN);
683✔
1071
  }
1072

1073
  return code;
703✔
1074
}
1075

1076
int32_t buildTableMetaFromViewMeta(STableMeta** pMeta, SViewMeta* pViewMeta) {
813✔
1077
  *pMeta = taosMemoryCalloc(1, sizeof(STableMeta) + pViewMeta->numOfCols * sizeof(SSchema));
813✔
1078
  if (NULL == *pMeta) {
813!
1079
    return terrno;
×
1080
  }
1081
  (*pMeta)->uid = pViewMeta->viewId;
813✔
1082
  (*pMeta)->vgId = MNODE_HANDLE;
813✔
1083
  (*pMeta)->tableType = TSDB_VIEW_TABLE;
813✔
1084
  (*pMeta)->sversion = pViewMeta->version;
813✔
1085
  (*pMeta)->tversion = pViewMeta->version;
813✔
1086
  (*pMeta)->tableInfo.precision = pViewMeta->precision;
813✔
1087
  (*pMeta)->tableInfo.numOfColumns = pViewMeta->numOfCols;
813✔
1088
  memcpy((*pMeta)->schema, pViewMeta->pSchema, sizeof(SSchema) * pViewMeta->numOfCols);
813✔
1089

1090
  for (int32_t i = 0; i < pViewMeta->numOfCols; ++i) {
3,861✔
1091
    (*pMeta)->tableInfo.rowSize += (*pMeta)->schema[i].bytes;
3,048✔
1092
  }
1093
  return TSDB_CODE_SUCCESS;
813✔
1094
}
1095

1096
int32_t getViewMetaFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta) {
10,985✔
1097
  char    fullName[TSDB_TABLE_FNAME_LEN];
1098
  int32_t code = tNameExtractFullName(pName, fullName);
10,985✔
1099
  if (TSDB_CODE_SUCCESS != code) {
10,985!
1100
    return code;
×
1101
  }
1102
  SViewMeta* pViewMeta = NULL;
10,985✔
1103
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pViews, (void**)&pViewMeta);
10,985✔
1104
  if (TSDB_CODE_SUCCESS == code) {
10,985✔
1105
    code = buildTableMetaFromViewMeta(pMeta, pViewMeta);
797✔
1106
  }
1107
  return code;
10,985✔
1108
}
1109

1110
static int32_t reserveDbReqInCache(int32_t acctId, const char* pDb, SHashObj** pDbs) {
1,371,484✔
1111
  if (NULL == *pDbs) {
1,371,484✔
1112
    *pDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,126,226✔
1113
    if (NULL == *pDbs) {
1,126,227!
1114
      return terrno;
×
1115
    }
1116
  }
1117
  char    fullName[TSDB_TABLE_FNAME_LEN];
1118
  int32_t len = tsnprintf(fullName, sizeof(fullName), "%d.%s", acctId, pDb);
1,371,485✔
1119
  return taosHashPut(*pDbs, fullName, len, &nullPointer, POINTER_BYTES);
1,371,510✔
1120
}
1121

1122
int32_t reserveDbVgInfoInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
1,205,913✔
1123
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbVgroup);
1,205,913✔
1124
}
1125

1126
int32_t getDbVgInfoFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, SArray** pVgInfo) {
477,616✔
1127
  SArray* pVgList = NULL;
477,616✔
1128
  int32_t code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbVgroup, (void**)&pVgList);
477,616✔
1129
  // pVgList is null, which is a legal value, indicating that the user DB has not been created
1130
  if (TSDB_CODE_SUCCESS == code && NULL != pVgList) {
477,616!
1131
    *pVgInfo = taosArrayDup(pVgList, NULL);
477,615✔
1132
    if (NULL == *pVgInfo) {
477,615!
1133
      code = terrno;
×
1134
    }
1135
  }
1136
  return code;
477,616✔
1137
}
1138

1139
int32_t reserveTableVgroupInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
1,319,442✔
1140
  return reserveTableReqInDbCache(acctId, pDb, pTable, &pMetaCache->pTableVgroup);
1,319,442✔
1141
}
1142

1143
int32_t reserveTableVgroupInCacheExt(const SName* pName, SParseMetaCache* pMetaCache) {
678✔
1144
  return reserveTableReqInDbCache(pName->acctId, pName->dbname, pName->tname, &pMetaCache->pTableVgroup);
678✔
1145
}
1146

1147
int32_t getTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup) {
817,849✔
1148
  char    fullName[TSDB_TABLE_FNAME_LEN];
1149
  int32_t code = tNameExtractFullName(pName, fullName);
817,849✔
1150
  if (TSDB_CODE_SUCCESS != code) {
817,881!
1151
    return code;
×
1152
  }
1153
  SVgroupInfo* pVg = NULL;
817,881✔
1154
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableVgroup, (void**)&pVg);
817,881✔
1155
  if (TSDB_CODE_SUCCESS == code) {
817,819!
1156
    memcpy(pVgroup, pVg, sizeof(SVgroupInfo));
817,832✔
1157
  }
1158
  return code;
817,819✔
1159
}
1160

1161
int32_t getDbTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup) {
442✔
1162
  char    fullName[TSDB_TABLE_FNAME_LEN];
1163
  int32_t code = tNameExtractFullName(pName, fullName);
442✔
1164
  if (TSDB_CODE_SUCCESS != code) {
442!
1165
    return code;
×
1166
  }
1167
  const char* pDb = strstr(fullName, ".");
442✔
1168
  if (pDb == NULL) return TSDB_CODE_PAR_INTERNAL_ERROR;
442!
1169
  pDb = strstr(pDb + 1, ".");
442✔
1170
  if (pDb == NULL) return TSDB_CODE_PAR_INTERNAL_ERROR;
442!
1171
  int32_t fullDbLen = pDb - fullName;
442✔
1172
  int32_t fullTbLen = strlen(fullName);
442✔
1173

1174
  SArray*     pVgArray = NULL;
442✔
1175
  SDbCfgInfo* pDbCfg = NULL;
442✔
1176
  code = getMetaDataFromHash(fullName, fullDbLen, pMetaCache->pDbVgroup, (void**)&pVgArray);
442✔
1177
  if (TSDB_CODE_SUCCESS == code) {
442!
1178
    code = getMetaDataFromHash(fullName, fullDbLen, pMetaCache->pDbCfg, (void**)&pDbCfg);
442✔
1179
  }
1180
  if (TSDB_CODE_SUCCESS == code) {
442!
1181
    uint32_t hashValue =
442✔
1182
        taosGetTbHashVal(fullName, fullTbLen, pDbCfg->hashMethod, pDbCfg->hashPrefix, pDbCfg->hashSuffix);
442!
1183
    SVgroupInfo* pVg = taosArraySearch(pVgArray, &hashValue, ctgHashValueComp, TD_EQ);
442✔
1184
    if (pVg) {
442!
1185
      memcpy(pVgroup, pVg, sizeof(SVgroupInfo));
442✔
1186
    } else {
1187
      code = TSDB_CODE_PAR_INTERNAL_ERROR;
×
1188
    }
1189
  }
1190
  return code;
442✔
1191
}
1192

1193
int32_t reserveDbVgVersionInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
9,409✔
1194
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbInfo);
9,409✔
1195
}
1196

1197
int32_t getDbVgVersionFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, int32_t* pVersion, int64_t* pDbId,
9,417✔
1198
                                int32_t* pTableNum, int64_t* pStateTs) {
1199
  SDbInfo* pDbInfo = NULL;
9,417✔
1200
  int32_t  code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbInfo, (void**)&pDbInfo);
9,417✔
1201
  if (TSDB_CODE_SUCCESS == code) {
9,418!
1202
    *pVersion = pDbInfo->vgVer;
9,418✔
1203
    *pDbId = pDbInfo->dbId;
9,418✔
1204
    *pTableNum = pDbInfo->tbNum;
9,418✔
1205
    *pStateTs = pDbInfo->stateTs;
9,418✔
1206
  }
1207
  return code;
9,418✔
1208
}
1209

1210
int32_t reserveDbCfgInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
156,160✔
1211
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbCfg);
156,160✔
1212
}
1213

1214
int32_t getDbCfgFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, SDbCfgInfo* pInfo) {
69,750✔
1215
  SDbCfgInfo* pDbCfg = NULL;
69,750✔
1216
  int32_t     code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbCfg, (void**)&pDbCfg);
69,750✔
1217
  if (TSDB_CODE_SUCCESS == code) {
69,754✔
1218
    memcpy(pInfo, pDbCfg, sizeof(SDbCfgInfo));
69,753✔
1219
  }
1220
  return code;
69,754✔
1221
}
1222

1223
static int32_t reserveUserAuthInCacheImpl(const char* pKey, int32_t len, SParseMetaCache* pMetaCache) {
1,328,789✔
1224
  if (NULL == pMetaCache->pUserAuth) {
1,328,789✔
1225
    pMetaCache->pUserAuth = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
1,085,073✔
1226
    if (NULL == pMetaCache->pUserAuth) {
1,085,089!
1227
      return terrno;
×
1228
    }
1229
  }
1230
  return taosHashPut(pMetaCache->pUserAuth, pKey, len, &nullPointer, POINTER_BYTES);
1,328,805✔
1231
}
1232

1233
int32_t reserveUserAuthInCache(int32_t acctId, const char* pUser, const char* pDb, const char* pTable, AUTH_TYPE type,
1,328,430✔
1234
                               SParseMetaCache* pMetaCache) {
1235
  char    key[USER_AUTH_KEY_MAX_LEN] = {0};
1,328,430✔
1236
  int32_t len = userAuthToString(acctId, pUser, pDb, pTable, type, key, false);
1,328,430✔
1237
  return reserveUserAuthInCacheImpl(key, len, pMetaCache);
1,328,435✔
1238
}
1239

1240
int32_t reserveViewUserAuthInCache(int32_t acctId, const char* pUser, const char* pDb, const char* pTable,
383✔
1241
                                   AUTH_TYPE type, SParseMetaCache* pMetaCache) {
1242
  char    key[USER_AUTH_KEY_MAX_LEN] = {0};
383✔
1243
  int32_t len = userAuthToString(acctId, pUser, pDb, pTable, type, key, true);
383✔
1244
  return reserveUserAuthInCacheImpl(key, len, pMetaCache);
383✔
1245
}
1246

1247
int32_t getUserAuthFromCache(SParseMetaCache* pMetaCache, SUserAuthInfo* pAuthReq, SUserAuthRes* pAuthRes) {
855✔
1248
  char          key[USER_AUTH_KEY_MAX_LEN] = {0};
855✔
1249
  int32_t       len = userAuthToString(pAuthReq->tbName.acctId, pAuthReq->user, pAuthReq->tbName.dbname,
855✔
1250
                                       pAuthReq->tbName.tname, pAuthReq->type, key, pAuthReq->isView);
855✔
1251
  SUserAuthRes* pAuth = NULL;
855✔
1252
  int32_t       code = getMetaDataFromHash(key, len, pMetaCache->pUserAuth, (void**)&pAuth);
855✔
1253
  if (TSDB_CODE_SUCCESS == code) {
855✔
1254
    memcpy(pAuthRes, pAuth, sizeof(SUserAuthRes));
775✔
1255
  }
1256
  return code;
855✔
1257
}
1258

1259
int32_t reserveUdfInCache(const char* pFunc, SParseMetaCache* pMetaCache) {
1,596✔
1260
  if (NULL == pMetaCache->pUdf) {
1,596✔
1261
    pMetaCache->pUdf = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
924✔
1262
    if (NULL == pMetaCache->pUdf) {
924!
1263
      return terrno;
×
1264
    }
1265
  }
1266
  return taosHashPut(pMetaCache->pUdf, pFunc, strlen(pFunc), &nullPointer, POINTER_BYTES);
1,596✔
1267
}
1268

1269
int32_t getUdfInfoFromCache(SParseMetaCache* pMetaCache, const char* pFunc, SFuncInfo* pInfo) {
1,593✔
1270
  SFuncInfo* pFuncInfo = NULL;
1,593✔
1271
  int32_t    code = getMetaDataFromHash(pFunc, strlen(pFunc), pMetaCache->pUdf, (void**)&pFuncInfo);
1,593✔
1272
  if (TSDB_CODE_SUCCESS == code) {
1,593✔
1273
    memcpy(pInfo, pFuncInfo, sizeof(SFuncInfo));
1,583✔
1274
  }
1275
  return code;
1,593✔
1276
}
1277

1278
static void destroySmaIndex(void* p) { taosMemoryFree(((STableIndexInfo*)p)->expr); }
×
1279

1280
static SArray* smaIndexesDup(SArray* pSrc) {
×
1281
  SArray* pDst = taosArrayDup(pSrc, NULL);
×
1282
  if (NULL == pDst) {
×
1283
    return NULL;
×
1284
  }
1285
  int32_t size = taosArrayGetSize(pDst);
×
1286
  for (int32_t i = 0; i < size; ++i) {
×
1287
    ((STableIndexInfo*)taosArrayGet(pDst, i))->expr = NULL;
×
1288
  }
1289
  for (int32_t i = 0; i < size; ++i) {
×
1290
    STableIndexInfo* pIndex = taosArrayGet(pDst, i);
×
1291
    pIndex->expr = taosStrdup(((STableIndexInfo*)taosArrayGet(pSrc, i))->expr);
×
1292
    if (NULL == pIndex->expr) {
×
1293
      taosArrayDestroyEx(pDst, destroySmaIndex);
×
1294
      return NULL;
×
1295
    }
1296
  }
1297
  return pDst;
×
1298
}
1299

1300
int32_t reserveTableIndexInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
7,338✔
1301
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableIndex);
7,338✔
1302
}
1303

1304
int32_t reserveTableCfgInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
143✔
1305
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableCfg);
143✔
1306
}
1307

1308
int32_t reserveTableTSMAInfoInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
13,207✔
1309
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableTSMAs);
13,207✔
1310
}
1311

1312
int32_t reserveTSMAInfoInCache(int32_t acctId, const char* pDb, const char* pTsmaName, SParseMetaCache* pMetaCache) {
302✔
1313
  return reserveTableReqInDbCache(acctId, pDb, pTsmaName, &pMetaCache->pTSMAs);
302✔
1314
}
1315

1316
int32_t getTableIndexFromCache(SParseMetaCache* pMetaCache, const SName* pName, SArray** pIndexes) {
×
1317
  char    fullName[TSDB_TABLE_FNAME_LEN];
1318
  int32_t code = tNameExtractFullName(pName, fullName);
×
1319
  if (TSDB_CODE_SUCCESS != code) return code;
×
1320
  ;
1321
  SArray* pSmaIndexes = NULL;
×
1322
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableIndex, (void**)&pSmaIndexes);
×
1323
  if (TSDB_CODE_SUCCESS == code && NULL != pSmaIndexes) {
×
1324
    *pIndexes = smaIndexesDup(pSmaIndexes);
×
1325
    if (NULL == *pIndexes) {
×
1326
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1327
    }
1328
  }
1329
  return code;
×
1330
}
1331

1332
int32_t getTableTsmasFromCache(SParseMetaCache* pMetaCache, const SName* pTbName, SArray** pTsmas) {
35,954✔
1333
  char    tbFName[TSDB_TABLE_FNAME_LEN];
1334
  int32_t code = tNameExtractFullName(pTbName, tbFName);
35,954✔
1335
  if (TSDB_CODE_SUCCESS != code) {
35,954!
1336
    return code;
×
1337
  }
1338
  STableTSMAInfoRsp* pTsmasRsp = NULL;
35,954✔
1339
  code = getMetaDataFromHash(tbFName, strlen(tbFName), pMetaCache->pTableTSMAs, (void**)&pTsmasRsp);
35,954✔
1340
  if (TSDB_CODE_SUCCESS == code && pTsmasRsp) {
35,954✔
1341
    *pTsmas = pTsmasRsp->pTsmas;
11,849✔
1342
  }
1343
  return TSDB_CODE_SUCCESS;
35,954✔
1344
}
1345

1346
int32_t getTsmaFromCache(SParseMetaCache* pMetaCache, const SName* pTsmaName, STableTSMAInfo** pTsma) {
302✔
1347
  char    tsmaFName[TSDB_TABLE_FNAME_LEN];
1348
  int32_t code = tNameExtractFullName(pTsmaName, tsmaFName);
302✔
1349
  if (TSDB_CODE_SUCCESS != code) {
302!
1350
    return code;
×
1351
  }
1352
  STableTSMAInfoRsp* pTsmaRsp = NULL;
302✔
1353
  code = getMetaDataFromHash(tsmaFName, strlen(tsmaFName), pMetaCache->pTSMAs, (void**)&pTsmaRsp);
302✔
1354
  if (TSDB_CODE_SUCCESS == code) {
302!
1355
    if (!pTsmaRsp || pTsmaRsp->pTsmas->size != 1) {
302!
1356
      return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1357
    }
1358
    *pTsma = taosArrayGetP(pTsmaRsp->pTsmas, 0);
302✔
1359
  } else if (code == TSDB_CODE_PAR_INTERNAL_ERROR) {
×
1360
    code = TSDB_CODE_MND_SMA_NOT_EXIST;
×
1361
  }
1362
  return code;
302✔
1363
}
1364

1365
STableCfg* tableCfgDup(STableCfg* pCfg) {
116✔
1366
  STableCfg* pNew = taosMemoryMalloc(sizeof(*pNew));
116✔
1367
  if (!pNew) {
116!
1368
    return NULL;
×
1369
  }
1370
  memcpy(pNew, pCfg, sizeof(*pNew));
116✔
1371
  pNew->pComment = NULL;
116✔
1372
  pNew->pFuncs = NULL;
116✔
1373
  pNew->pTags = NULL;
116✔
1374
  pNew->pSchemas = NULL;
116✔
1375
  pNew->pSchemaExt = NULL;
116✔
1376
  if (NULL != pCfg->pComment) {
116!
1377
    pNew->pComment = taosMemoryCalloc(pNew->commentLen + 1, 1);
×
1378
    if (!pNew->pComment) goto err;
×
1379
    memcpy(pNew->pComment, pCfg->pComment, pNew->commentLen);
×
1380
  }
1381
  if (NULL != pCfg->pFuncs) {
116!
1382
    pNew->pFuncs = taosArrayDup(pCfg->pFuncs, NULL);
×
1383
    if (!pNew->pFuncs) goto err;
×
1384
  }
1385
  if (NULL != pCfg->pTags) {
116✔
1386
    pNew->pTags = taosMemoryCalloc(pCfg->tagsLen + 1, 1);
39✔
1387
    if (!pNew->pTags) goto err;
39!
1388
    memcpy(pNew->pTags, pCfg->pTags, pNew->tagsLen);
39✔
1389
  }
1390

1391
  int32_t schemaSize = (pCfg->numOfColumns + pCfg->numOfTags) * sizeof(SSchema);
116✔
1392

1393
  SSchema* pSchema = taosMemoryMalloc(schemaSize);
116✔
1394
  if (!pSchema) goto err;
116!
1395
  memcpy(pSchema, pCfg->pSchemas, schemaSize);
116✔
1396
  pNew->pSchemas = pSchema;
116✔
1397

1398
  SSchemaExt* pSchemaExt = NULL;
116✔
1399
  if (useCompress(pCfg->tableType) && pCfg->pSchemaExt) {
116!
1400
    int32_t schemaExtSize = pCfg->numOfColumns * sizeof(SSchemaExt);
86✔
1401
    pSchemaExt = taosMemoryMalloc(schemaExtSize);
86✔
1402
    if (!pSchemaExt) goto err;
86!
1403
    memcpy(pSchemaExt, pCfg->pSchemaExt, schemaExtSize);
86✔
1404
  }
1405

1406
  pNew->pSchemaExt = pSchemaExt;
116✔
1407

1408
  return pNew;
116✔
1409
err:
×
1410
  if (pNew->pComment) taosMemoryFreeClear(pNew->pComment);
×
1411
  if (pNew->pFuncs) taosArrayDestroy(pNew->pFuncs);
×
1412
  if (pNew->pTags) taosMemoryFreeClear(pNew->pTags);
×
1413
  if (pNew->pSchemas) taosMemoryFreeClear(pNew->pSchemas);
×
1414
  if (pNew->pSchemaExt) taosMemoryFreeClear(pNew->pSchemaExt);
×
1415
  taosMemoryFreeClear(pNew);
×
1416
  return NULL;
×
1417
}
1418

1419
int32_t getTableCfgFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableCfg** pOutput) {
122✔
1420
  char    fullName[TSDB_TABLE_FNAME_LEN];
1421
  int32_t code = tNameExtractFullName(pName, fullName);
122✔
1422
  if (TSDB_CODE_SUCCESS != code) {
122!
1423
    return code;
×
1424
  }
1425
  STableCfg* pCfg = NULL;
122✔
1426
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableCfg, (void**)&pCfg);
122✔
1427
  if (TSDB_CODE_SUCCESS == code && NULL != pCfg) {
122!
1428
    *pOutput = tableCfgDup(pCfg);
116✔
1429
    if (NULL == *pOutput) {
116!
1430
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1431
    }
1432
  }
1433
  return code;
122✔
1434
}
1435

1436
int32_t reserveDnodeRequiredInCache(SParseMetaCache* pMetaCache) {
92✔
1437
  pMetaCache->dnodeRequired = true;
92✔
1438
  return TSDB_CODE_SUCCESS;
92✔
1439
}
1440

1441
int32_t getDnodeListFromCache(SParseMetaCache* pMetaCache, SArray** pDnodes) {
87✔
1442
  SMetaRes* pRes = taosArrayGet(pMetaCache->pDnodes, 0);
87✔
1443
  if (TSDB_CODE_SUCCESS != pRes->code) {
87!
1444
    return pRes->code;
×
1445
  }
1446

1447
  *pDnodes = taosArrayDup((SArray*)pRes->pRes, NULL);
87✔
1448
  if (NULL == *pDnodes) {
87!
1449
    return terrno;
×
1450
  }
1451
  return TSDB_CODE_SUCCESS;
87✔
1452
}
1453

1454
void destoryParseTablesMetaReqHash(SHashObj* pHash) {
6,303,734✔
1455
  SParseTablesMetaReq* p = taosHashIterate(pHash, NULL);
6,303,734✔
1456
  while (NULL != p) {
8,458,295✔
1457
    taosHashCleanup(p->pTables);
2,154,527✔
1458
    p = taosHashIterate(pHash, p);
2,154,497✔
1459
  }
1460
  taosHashCleanup(pHash);
6,303,768✔
1461
}
6,303,765✔
1462

1463
void destoryParseMetaCache(SParseMetaCache* pMetaCache, bool request) {
2,489,878✔
1464
  if (request) {
2,489,878✔
1465
    destoryParseTablesMetaReqHash(pMetaCache->pTableMeta);
1,260,757✔
1466
    destoryParseTablesMetaReqHash(pMetaCache->pTableVgroup);
1,260,760✔
1467
    destoryParseTablesMetaReqHash(pMetaCache->pViews);
1,260,773✔
1468
    destoryParseTablesMetaReqHash(pMetaCache->pTSMAs);
1,260,758✔
1469
    destoryParseTablesMetaReqHash(pMetaCache->pTableName);
1,260,769✔
1470
  } else {
1471
    taosHashCleanup(pMetaCache->pTableMeta);
1,229,121✔
1472
    taosHashCleanup(pMetaCache->pTableVgroup);
1,229,189✔
1473
    taosHashCleanup(pMetaCache->pViews);
1,229,195✔
1474
    taosHashCleanup(pMetaCache->pTSMAs);
1,229,201✔
1475
    taosHashCleanup(pMetaCache->pTableName);
1,229,202✔
1476
  }
1477
  taosHashCleanup(pMetaCache->pDbVgroup);
2,489,972✔
1478
  taosHashCleanup(pMetaCache->pDbCfg);
2,489,938✔
1479
  taosHashCleanup(pMetaCache->pDbInfo);
2,489,956✔
1480
  taosHashCleanup(pMetaCache->pUserAuth);
2,489,959✔
1481
  taosHashCleanup(pMetaCache->pUdf);
2,489,967✔
1482
  taosHashCleanup(pMetaCache->pTableIndex);
2,489,963✔
1483
  taosHashCleanup(pMetaCache->pTableCfg);
2,489,960✔
1484
  taosHashCleanup(pMetaCache->pTableTSMAs);
2,489,960✔
1485
}
2,489,954✔
1486

1487
int64_t int64SafeSub(int64_t a, int64_t b) {
8,009✔
1488
  int64_t res = (uint64_t)a - (uint64_t)b;
8,009✔
1489

1490
  if (a >= 0 && b < 0) {
8,009!
1491
    if ((uint64_t)res > (uint64_t)INT64_MAX) {
×
1492
      // overflow
1493
      res = INT64_MAX;
×
1494
    }
1495
  } else if (a < 0 && b > 0 && res >= 0) {
8,009!
1496
    // underflow
1497
    res = INT64_MIN;
×
1498
  }
1499
  return res;
8,009✔
1500
}
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