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

taosdata / TDengine / #5011

03 Apr 2026 03:59PM UTC coverage: 72.3% (+0.008%) from 72.292%
#5011

push

travis-ci

web-flow
merge: from main to 3.0 branch #35067

4053 of 5985 new or added lines in 68 files covered. (67.72%)

732 existing lines in 143 files now uncovered.

257430 of 356056 relevant lines covered (72.3%)

131834103.52 hits per line

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

76.82
/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 "decimal.h"
19
#include "querynodes.h"
20
#include "tarray.h"
21
#include "tlog.h"
22

23
#define USER_AUTH_KEY_MAX_LEN TSDB_USER_LEN + TSDB_TABLE_FNAME_LEN + 2
24

25
const void* nullPointer = NULL;
26

27
static char* getSyntaxErrFormat(int32_t errCode) {
20,720,571✔
28
  switch (errCode) {
20,720,571✔
29
    case TSDB_CODE_PAR_SYNTAX_ERROR:
13,167,384✔
30
      return "syntax error near \"%s\"";
13,167,384✔
31
    case TSDB_CODE_PAR_INCOMPLETE_SQL:
243,106✔
32
      return "Incomplete SQL statement";
243,106✔
33
    case TSDB_CODE_PAR_INVALID_COLUMN:
1,532,278✔
34
      return "Invalid column name: %s";
1,532,278✔
35
    case TSDB_CODE_PAR_TABLE_NOT_EXIST:
119,983✔
36
      return "Table does not exist: %s";
119,983✔
37
    case TSDB_CODE_PAR_GET_META_ERROR:
2,639✔
38
      return "Fail to get table info, error: %s";
2,639✔
39
    case TSDB_CODE_PAR_AMBIGUOUS_COLUMN:
766,538✔
40
      return "Column is ambiguous: %s";
766,538✔
41
    case TSDB_CODE_PAR_WRONG_VALUE_TYPE:
2,376✔
42
      return "Invalid value type: %s";
2,376✔
43
    case TSDB_CODE_PAR_INVALID_VARBINARY:
57,312✔
44
      return "Invalid varbinary value: %s";
57,312✔
45
    case TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION:
426,947✔
46
      return "There mustn't be aggregation";
426,947✔
47
    case TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT:
7,268✔
48
      return "ORDER BY / GROUP BY item must be the number of a SELECT-list expression";
7,268✔
49
    case TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION:
223,290✔
50
      return "Not a GROUP BY expression";
223,290✔
51
    case TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION:
19,464✔
52
      return "Not SELECTed expression";
19,464✔
53
    case TSDB_CODE_PAR_NOT_SINGLE_GROUP:
134,944✔
54
      return "Not a single-group group function, '%s' is used incorrectly";
134,944✔
55
    case TSDB_CODE_PAR_TAGS_NOT_MATCHED:
13,207✔
56
      return "Tags number not matched";
13,207✔
57
    case TSDB_CODE_PAR_INVALID_TAG_NAME:
7,916✔
58
      return "Invalid tag name: %s";
7,916✔
59
    case TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG:
2,815✔
60
      return "Name or password too long";
2,815✔
61
    case TSDB_CODE_PAR_ALGR_ID_TOO_LONG:
×
62
      return "Algorithm ID too long, max lenght is 63 character";
×
63
    case TSDB_CODE_PAR_ALGR_NAME_TOO_LONG:
×
64
      return "Algorithm name too long, max lenght is 63 character";
×
65
    case TSDB_CODE_PAR_ALGR_DESC_TOO_LONG:
×
66
      return "Algorithm description too long, max lenght is 127 character";
×
67
    case TSDB_CODE_PAR_ALGR_TYPE_TOO_LONG:
×
68
      return "Algorithm type too long, max lenght is 63 character";
×
69
    case TSDB_CODE_PAR_ALGR_OSSL_NAME_TOO_LONG:
×
70
      return "Algorithm OpenSSL name too long, max lenght is 63 character";
×
71
    case TSDB_CODE_PAR_PASSWD_TOO_SHORT_OR_EMPTY:
5,547✔
72
      return "Password too short or empty";
5,547✔
73
    case TSDB_CODE_PAR_INVALID_PORT:
×
74
      return "Port should be an integer that is less than 65535 and greater than 0";
×
75
    case TSDB_CODE_PAR_INVALID_ENDPOINT:
×
76
      return "Endpoint should be in the format of 'fqdn:port'";
×
77
    case TSDB_CODE_PAR_EXPRIE_STATEMENT:
1,753✔
78
      return "This statement is no longer supported";
1,753✔
79
    case TSDB_CODE_PAR_INTER_VALUE_TOO_SMALL:
1,158✔
80
      return "Interval cannot be less than %d %s";
1,158✔
81
    case TSDB_CODE_PAR_INTER_VALUE_TOO_BIG:
×
82
      return "Interval cannot be more than %d %s";
×
83
    case TSDB_CODE_PAR_DB_NOT_SPECIFIED:
8,096✔
84
      return "Database not specified";
8,096✔
85
    case TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME:
14,647✔
86
      return "Invalid identifier name: %s";
14,647✔
87
    case TSDB_CODE_PAR_CORRESPONDING_STABLE_ERR:
23,590✔
88
      return "Corresponding super table not in this db";
23,590✔
89
    case TSDB_CODE_PAR_GROUPBY_WINDOW_COEXIST:
7,009✔
90
      return "GROUP BY and WINDOW-clause can't be used together";
7,009✔
91
    case TSDB_CODE_PAR_AGG_FUNC_NESTING:
10,077✔
92
      return "Aggregate functions do not support nesting";
10,077✔
93
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE:
392✔
94
      return "Only support STATE_WINDOW on integer/bool/varchar column";
392✔
95
    case TSDB_CODE_PAR_INVALID_STATE_WIN_COL:
581✔
96
      return "Not support STATE_WINDOW on tag column";
581✔
97
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TABLE:
×
98
      return "STATE_WINDOW not support for super table query";
×
99
    case TSDB_CODE_PAR_INVALID_STATE_WIN_EXTEND:
411✔
100
      return "Invalid state window extend option";
411✔
101
    case TSDB_CODE_PAR_INTER_SESSION_GAP:
1,176✔
102
      return "SESSION gap should be fixed time window, and greater than 0";
1,176✔
103
    case TSDB_CODE_PAR_INTER_SESSION_COL:
7,245✔
104
      return "Only support SESSION on primary timestamp column";
7,245✔
105
    case TSDB_CODE_PAR_INTER_OFFSET_NEGATIVE:
×
106
      return "Interval offset cannot be negative";
×
107
    case TSDB_CODE_PAR_INTER_OFFSET_UNIT:
2,744✔
108
      return "Cannot use 'year' as offset when interval is 'month'";
2,744✔
109
    case TSDB_CODE_PAR_INTER_OFFSET_TOO_BIG:
784✔
110
      return "Interval offset should be shorter than interval";
784✔
111
    case TSDB_CODE_PAR_INTER_SLIDING_UNIT:
3,082✔
112
      return "Does not support sliding when interval is natural month/year";
3,082✔
113
    case TSDB_CODE_PAR_INTER_SLIDING_TOO_BIG:
4,046✔
114
      return "sliding value no larger than the interval value";
4,046✔
115
    case TSDB_CODE_PAR_INTER_SLIDING_TOO_SMALL:
1,867✔
116
      return "sliding value can not less than 1%% of interval value";
1,867✔
117
    case TSDB_CODE_PAR_ONLY_ONE_JSON_TAG:
2,505✔
118
      return "Only one tag if there is a json tag";
2,505✔
119
    case TSDB_CODE_PAR_INCORRECT_NUM_OF_COL:
×
120
      return "Query block has incorrect number of result columns";
×
121
    case TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL:
×
122
      return "Incorrect TIMESTAMP value: %s";
×
123
    case TSDB_CODE_PAR_OFFSET_LESS_ZERO:
×
124
      return "soffset/offset can not be less than 0";
×
125
    case TSDB_CODE_PAR_SLIMIT_LEAK_PARTITION_GROUP_BY:
152,958✔
126
      return "slimit/soffset only available for PARTITION/GROUP BY query";
152,958✔
127
    case TSDB_CODE_PAR_INVALID_TOPIC_QUERY:
30,132✔
128
      return "Invalid topic query";
30,132✔
129
    case TSDB_CODE_PAR_INVALID_DROP_STABLE:
1,177✔
130
      return "Cannot drop super table in batch";
1,177✔
131
    case TSDB_CODE_PAR_INVALID_FILL_TIME_RANGE:
7,568✔
132
      return "Start(end) time of query range required or time range too large";
7,568✔
133
    case TSDB_CODE_PAR_DUPLICATED_COLUMN:
10,073✔
134
      return "Duplicated column names";
10,073✔
135
    case TSDB_CODE_PAR_INVALID_TAGS_LENGTH:
211,742✔
136
      return "Tags length exceeds max length %d";
211,742✔
137
    case TSDB_CODE_PAR_INVALID_ROW_LENGTH:
97,049✔
138
      return "Row length exceeds max length %d";
97,049✔
139
    case TSDB_CODE_PAR_INVALID_COLUMNS_NUM:
10,789✔
140
      return "Illegal number of columns";
10,789✔
141
    case TSDB_CODE_PAR_TOO_MANY_COLUMNS:
6,603✔
142
      return "Too many columns";
6,603✔
143
    case TSDB_CODE_PAR_INVALID_FIRST_COLUMN:
×
144
      return "First column must be timestamp";
×
145
    case TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN:
18,255✔
146
      return "Invalid column length for var length type";
18,255✔
147
    case TSDB_CODE_PAR_INVALID_TAGS_NUM:
×
148
      return "Invalid number of tag columns";
×
149
    case TSDB_CODE_PAR_INVALID_INTERNAL_PK:
×
150
      return "Invalid _c0 or _rowts expression";
×
151
    case TSDB_CODE_PAR_INVALID_TIMELINE_FUNC:
×
152
      return "Invalid timeline function";
×
153
    case TSDB_CODE_PAR_INVALID_PASSWD:
×
154
      return "Invalid password";
×
155
    case TSDB_CODE_PAR_INVALID_ALTER_TABLE:
153,450✔
156
      return "Invalid alter table statement";
153,450✔
157
    case TSDB_CODE_PAR_CANNOT_DROP_PRIMARY_KEY:
×
158
      return "Primary timestamp column cannot be dropped";
×
159
    case TSDB_CODE_PAR_INVALID_MODIFY_COL:
450,797✔
160
      return "Only varbinary/binary/nchar/geometry column length could be modified, and the length can only be "
450,797✔
161
             "increased, not decreased";
162
    case TSDB_CODE_PAR_INVALID_TBNAME:
12,166✔
163
      return "Invalid tbname pseudo column";
12,166✔
164
    case TSDB_CODE_PAR_INVALID_FUNCTION_NAME:
256✔
165
      return "Invalid function name";
256✔
166
    case TSDB_CODE_PAR_COMMENT_TOO_LONG:
8,892✔
167
      return "Comment too long";
8,892✔
168
    case TSDB_CODE_PAR_NOT_ALLOWED_FUNC:
×
169
      return "Some functions are allowed only in the SELECT list of a query. "
×
170
             "And, cannot be mixed with other non scalar functions or columns.";
171
    case TSDB_CODE_PAR_NOT_ALLOWED_WIN_QUERY:
49,546✔
172
      return "Window query not supported, since not valid primary timestamp column as input";
49,546✔
173
    case TSDB_CODE_PAR_INVALID_DROP_COL:
2,151✔
174
      return "No columns can be dropped";
2,151✔
175
    case TSDB_CODE_PAR_INVALID_COL_JSON:
1,002✔
176
      return "Only tag can be json type";
1,002✔
177
    case TSDB_CODE_PAR_VALUE_TOO_LONG:
278,244✔
178
      return "Value too long for column/tag: %s";
278,244✔
179
    case TSDB_CODE_PAR_INVALID_DELETE_WHERE:
170,920✔
180
      return "The DELETE statement must only have a definite time window range";
170,920✔
181
    case TSDB_CODE_PAR_INVALID_REDISTRIBUTE_VG:
×
182
      return "The REDISTRIBUTE VGROUP statement only support 1 to 3 dnodes";
×
183
    case TSDB_CODE_PAR_FILL_NOT_ALLOWED_FUNC:
3,878✔
184
      return "%s function is not supported in fill query";
3,878✔
185
    case TSDB_CODE_PAR_INVALID_WINDOW_PC:
8,370✔
186
      return "_WSTART, _WEND, _WDURATION, and _ANOMALYMARK can only be used in window query";
8,370✔
187
    case TSDB_CODE_PAR_INVALID_TAGS_PC:
3,042✔
188
      return "Tags can only applied to super table and child table";
3,042✔
189
    case TSDB_CODE_PAR_WINDOW_NOT_ALLOWED_FUNC:
×
190
      return "%s function is not supported in time window query";
×
191
    case TSDB_CODE_PAR_STREAM_NOT_ALLOWED_FUNC:
×
192
      return "%s function is not supported in stream query";
×
193
    case TSDB_CODE_PAR_GROUP_BY_NOT_ALLOWED_FUNC:
×
194
      return "%s function is not supported in group query";
×
195
    case TSDB_CODE_PAR_SYSTABLE_NOT_ALLOWED_FUNC:
5,616✔
196
      return "%s function is not supported in system table query";
5,616✔
197
    case TSDB_CODE_PAR_SYSTABLE_NOT_ALLOWED:
1,728✔
198
      return "%s is not supported in system table query";
1,728✔
199
    case TSDB_CODE_PAR_INVALID_INTERP_CLAUSE:
2,132✔
200
      return "Invalid usage of RANGE clause, EVERY clause or FILL clause";
2,132✔
201
    case TSDB_CODE_PAR_INVALID_FORECAST_CLAUSE:
×
202
      return "Invalid usage of forecast clause";
×
203
    case TSDB_CODE_PAR_NO_VALID_FUNC_IN_WIN:
×
204
      return "No valid function in window query";
×
205
    case TSDB_CODE_PAR_INVALID_OPTR_USAGE:
2,008,998✔
206
      return "Invalid usage of expr: %s";
2,008,998✔
207
    case TSDB_CODE_PAR_INVALID_IP_RANGE:
1,424✔
208
      return "invalid ip range";
1,424✔
209
    case TSDB_CODE_OUT_OF_MEMORY:
×
210
      return "Out of memory";
×
211
    case TSDB_CODE_PAR_ORDERBY_AMBIGUOUS:
4,484✔
212
      return "ORDER BY \"%s\" is ambiguous";
4,484✔
213
    case TSDB_CODE_PAR_NOT_SUPPORT_MULTI_RESULT:
3,449✔
214
      return "Operator not supported multi result: %s";
3,449✔
215
    case TSDB_CODE_PAR_INVALID_WJOIN_HAVING_EXPR:
7,761✔
216
      return "Not supported window join having expr";
7,761✔
217
    case TSDB_CODE_PAR_INVALID_WIN_OFFSET_UNIT:
26,470✔
218
      return "Invalid WINDOW_OFFSET unit \"%c\"";
26,470✔
219
    case TSDB_CODE_PAR_INVALID_PERIOD_UNIT:
×
220
      return "Invalid PERIOD unit \"%c\"";
×
221
    case TSDB_CODE_PAR_VALID_PRIM_TS_REQUIRED:
14,680✔
222
      return "Valid primary timestamp required";
14,680✔
223
    case TSDB_CODE_PAR_NOT_WIN_FUNC:
×
224
      return "Column exists for window join with aggregation functions";
×
225
    case TSDB_CODE_PAR_TAG_IS_PRIMARY_KEY:
×
226
      return "tag %s can not be composite primary key";
×
227
    case TSDB_CODE_PAR_SECOND_COL_PK:
4,266✔
228
      return "composite primary key column must be second column";
4,266✔
229
    case TSDB_CODE_PAR_COL_PK_TYPE:
20,147✔
230
      return "composite primary key column must be of type int, uint, bigint, ubigint, and varchar";
20,147✔
231
    case TSDB_CODE_PAR_INVALID_PK_OP:
35,470✔
232
      return "composite primary key column can not be added, modified, and dropped";
35,470✔
233
    case TSDB_CODE_TSMA_NAME_TOO_LONG:
252✔
234
      return "Tsma name too long";
252✔
235
    case TSDB_CODE_PAR_TBNAME_ERROR:
×
236
      return "Pseudo tag tbname not set";
×
237
    case TSDB_CODE_PAR_TBNAME_DUPLICATED:
×
238
      return "Table name:%s duplicated";
×
239
    case TSDB_CODE_PAR_TAG_NAME_DUPLICATED:
×
240
      return "Tag name:%s duplicated";
×
241
    case TSDB_CODE_PAR_NOT_ALLOWED_DIFFERENT_BY_ROW_FUNC:
×
242
      return "Some functions cannot appear in the select list at the same time";
×
243
    case TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR:
×
244
      return "Syntax error in regular expression";
×
245
    case TSDB_CODE_PAR_TRUE_FOR_NEGATIVE:
×
246
      return "True_for duration cannot be negative";
×
247
    case TSDB_CODE_PAR_TRUE_FOR_UNIT:
1,568✔
248
      return "Cannot use 'year' or 'month' as true_for duration";
1,568✔
249
    case TSDB_CODE_PAR_INVALID_COLUMN_REF:
×
250
      return "Invalid column reference";
×
251
    case TSDB_CODE_PAR_INVALID_SLIDING_OFFSET:
×
252
      return "Invalid sliding offset";
×
253
    case TSDB_CODE_PAR_INVALID_INTERVAL_OFFSET:
×
254
      return "Invalid interval offset";
×
255
    case TSDB_CODE_PAR_INVALID_REF_COLUMN:
×
256
      return "Invalid virtual table's ref column";
×
257
    case TSDB_CODE_PAR_INVALID_TABLE_TYPE:
×
258
      return "Invalid table type";
×
259
    case TSDB_CODE_PAR_INVALID_REF_COLUMN_TYPE:
×
260
      return "Invalid virtual table's ref column type";
×
261
    case TSDB_CODE_PAR_MISMATCH_STABLE_TYPE:
675✔
262
      return "Create child table using virtual super table";
675✔
263
    case TSDB_CODE_STREAM_INVALID_TIME_UNIT:
×
264
      return "Invalid time unit in create stream clause";
×
265
    case TSDB_CODE_STREAM_INVALID_SYNTAX:
×
266
      return "Invalid syntax in create stream clause";
×
267
    case TSDB_CODE_STREAM_INVALID_NOTIFY:
×
268
      return "Invalid notify clause in create stream clause";
×
269
    case TSDB_CODE_STREAM_INVALID_TRIGGER:
×
270
      return "Invalid trigger clause in create stream clause";
×
271
    case TSDB_CODE_STREAM_INVALID_QUERY:
×
272
      return "Invalid query clause in create stream clause";
×
273
    case TSDB_CODE_STREAM_INVALID_OUT_TABLE:
×
274
      return "Invalid out table clause in create stream clause";
×
275
    case TSDB_CODE_STREAM_NO_TRIGGER_TABLE:
×
276
      return "trigger table not specified in create stream clause";
×
277
    case TSDB_CODE_STREAM_INVALID_PRE_FILTER:
×
278
      return "Invalid pre-filter in create stream clause";
×
279
    case TSDB_CODE_PAR_INVALID_TRUE_FOR_COUNT:
392✔
280
      return "TRUE_FOR COUNT must be a non-negative integer not exceeding INT32_MAX";
392✔
281
    case TSDB_CODE_STREAM_INVALID_PARTITION:
×
282
      return "Invalid partition in create stream clause";
×
283
    case TSDB_CODE_STREAM_INVALID_SUBTABLE:
×
284
      return "Invalid subtable in create stream clause";
×
285
    case TSDB_CODE_STREAM_INVALID_OUT_TAGS:
×
286
      return "Invalid out tags in create stream clause";
×
287
    case TSDB_CODE_STREAM_INVALID_NOTIFY_COND:
×
288
      return "Invalid notify condition in create stream clause";
×
289
    case TSDB_CODE_STREAM_INVALID_PLACE_HOLDER:
×
290
      return "Invalid placeholder in create stream clause";
×
291
    case TSDB_CODE_PAR_ORDERBY_UNKNOWN_EXPR:
40,700✔
292
      return "Invalid expr in order by clause: %s";
40,700✔
293
    case TSDB_CODE_PAR_OPTION_DUPLICATED:
×
294
      return "Option:%s duplicated";
×
295
    case TSDB_CODE_PAR_INVALID_OPTION_VALUE:
6,449✔
296
      return "Option:%s invalid value";
6,449✔
297
    case TSDB_CODE_PAR_OPTION_VALUE_TOO_LONG:
1,050✔
298
      return "Option:%s value too long, should be less than %d";
1,050✔
299
    case TSDB_CODE_PAR_OPTION_VALUE_TOO_SHORT:
×
300
      return "Option:%s value too short, should be %d or longer";
×
301
    case TSDB_CODE_PAR_OPTION_VALUE_TOO_BIG:
×
302
      return "Option:%s value too big, should be less than %d";
×
303
    case TSDB_CODE_PAR_OPTION_VALUE_TOO_SMALL:
×
304
      return "Option:%s value too small, should be %d or greater";
×
305
    case TSDB_CODE_PAR_ORDERBY_INVALID_EXPR:
×
306
      return "Aggregate functions cannot be used for sorting in non-aggregate queries";
×
307
    case TSDB_CODE_PAR_COL_PERMISSION_DENIED:
8,037✔
308
      return "Permission denied for column: %s";
8,037✔
309
    case TSDB_CODE_PAR_NOT_ALLOWED_FILL_MODE:
2,595✔
310
      return "FILL NEAR mode is not supported in window query";
2,595✔
311
    case TSDB_CODE_PAR_NOT_ALLOWED_FILL_VALUES:
5,784✔
312
      return "Fill values can only be used with fill VALUE/VALUE_F "
5,784✔
313
             "or PREV/NEXT/NEAR mode with surrounding time";
314
    case TSDB_CODE_PAR_EXT_WIN_COL_IN_WHERE:
405✔
315
      return "WHERE clause cannot reference EXTERNAL_WINDOW column: %s";
405✔
316
    default:
2,803✔
317
      return "Unknown error";
2,803✔
318
  }
319
}
320

321
int32_t generateSyntaxErrMsg(SMsgBuf* pBuf, int32_t errCode, ...) {
20,720,015✔
322
  va_list vArgList;
9,182,512✔
323
  va_start(vArgList, errCode);
20,720,015✔
324
  (void)vsnprintf(pBuf->buf, pBuf->len, getSyntaxErrFormat(errCode), vArgList);
20,720,015✔
325
  va_end(vArgList);
20,721,033✔
326
  return errCode;
20,721,033✔
327
}
328

329
int32_t generateSyntaxErrMsgExt(SMsgBuf* pBuf, int32_t errCode, const char* pFormat, ...) {
1,871,213✔
330
  va_list vArgList;
1,807,383✔
331
  va_start(vArgList, pFormat);
1,871,213✔
332
  (void)vsnprintf(pBuf->buf, pBuf->len, pFormat, vArgList);
1,871,213✔
333
  va_end(vArgList);
1,871,213✔
334
  return errCode;
1,871,213✔
335
}
336

337
int32_t buildInvalidOperationMsg(SMsgBuf* pBuf, const char* msg) {
61,087✔
338
  if (pBuf->buf) {
61,087✔
339
    tstrncpy(pBuf->buf, msg, pBuf->len);
61,087✔
340
  }
341

342
  return TSDB_CODE_TSC_INVALID_OPERATION;
61,087✔
343
}
344

345
int32_t buildInvalidOperationMsgExt(SMsgBuf* pBuf, const char* pFormat, ...) {
×
346
  va_list vArgList;
×
347
  va_start(vArgList, pFormat);
×
348
  (void)vsnprintf(pBuf->buf, pBuf->len, pFormat, vArgList);
×
349
  va_end(vArgList);
×
350
  return TSDB_CODE_TSC_INVALID_OPERATION;
×
351
}
352

353
int32_t buildSyntaxErrMsg(SMsgBuf* pBuf, const char* additionalInfo, const char* sourceStr) {
1,773,160✔
354
  if (pBuf == NULL) return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
1,773,160✔
355
  const char* msgFormat1 = "syntax error near \'%s\'";
1,772,659✔
356
  const char* msgFormat2 = "syntax error near \'%s\' (%s)";
1,772,659✔
357
  const char* msgFormat3 = "%s";
1,772,659✔
358

359
  const char* prefix = "syntax error";
1,772,659✔
360
  if (sourceStr == NULL) {
1,772,659✔
361
    snprintf(pBuf->buf, pBuf->len, msgFormat1, additionalInfo);
×
362
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
×
363
  }
364

365
  char buf[64] = {0};  // only extract part of sql string
1,772,659✔
366
  tstrncpy(buf, sourceStr, tListLen(buf));
1,772,659✔
367

368
  if (additionalInfo != NULL) {
1,772,659✔
369
    snprintf(pBuf->buf, pBuf->len, msgFormat2, buf, additionalInfo);
1,772,659✔
370
  } else {
371
    const char* msgFormat = (0 == strncmp(sourceStr, prefix, strlen(prefix))) ? msgFormat3 : msgFormat1;
×
372
    snprintf(pBuf->buf, pBuf->len, msgFormat, buf);
×
373
  }
374

375
  return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
1,772,659✔
376
}
377

378
SSchema* getTableColumnSchema(const STableMeta* pTableMeta) { return (SSchema*)pTableMeta->schema; }
2,147,483,647✔
379

380
SSchemaExt* getTableColumnExtSchema(const STableMeta* pTableMeta) { return pTableMeta->schemaExt; }
2,147,483,647✔
381

382
static SSchema* getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex) {
101,138,140✔
383
  SSchema* pSchema = (SSchema*)pTableMeta->schema;
101,138,140✔
384
  return &pSchema[colIndex];
101,147,520✔
385
}
386

387
SSchema* getTableTagSchema(const STableMeta* pTableMeta) {
101,138,371✔
388
  return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns);
101,138,371✔
389
}
390

391
int32_t getNumOfColumns(const STableMeta* pTableMeta) {
1,386,171,614✔
392
  // table created according to super table, use data from super table
393
  return getTableInfo(pTableMeta).numOfColumns;
1,386,171,614✔
394
}
395

396
int32_t getNumOfTags(const STableMeta* pTableMeta) { return getTableInfo(pTableMeta).numOfTags; }
1,295,734,820✔
397

398
STableComInfo getTableInfo(const STableMeta* pTableMeta) { return pTableMeta->tableInfo; }
2,147,483,647✔
399

400
int32_t getTableTypeFromTableNode(SNode* pTable) {
56,653,322✔
401
  if (NULL == pTable) {
56,653,322✔
402
    return -1;
×
403
  }
404
  if (QUERY_NODE_REAL_TABLE != nodeType(pTable)) {
56,653,322✔
405
    return -1;
3,280,272✔
406
  }
407
  return ((SRealTableNode*)pTable)->pMeta->tableType;
53,373,050✔
408
}
409

410
STableMeta* tableMetaDup(const STableMeta* pTableMeta) {
1,600,943,996✔
411
  int32_t numOfFields = TABLE_TOTAL_COL_NUM(pTableMeta);
1,600,943,996✔
412
  if (numOfFields > TSDB_MAX_COLUMNS || numOfFields < TSDB_MIN_COLUMNS) {
1,600,976,617✔
413
    return NULL;
×
414
  }
415

416
  bool   hasSchemaExt = pTableMeta->schemaExt == NULL ? false : true;
1,601,009,469✔
417
  size_t schemaExtSize = hasSchemaExt ? pTableMeta->tableInfo.numOfColumns * sizeof(SSchemaExt) : 0;
1,600,982,789✔
418
  bool   hasColRef = pTableMeta->colRef == NULL ? false : true;
1,600,990,979✔
419
  size_t colRefSize = hasColRef ? pTableMeta->numOfColRefs * sizeof(SColRef) : 0;
1,600,980,608✔
420

421
  size_t      size = sizeof(STableMeta) + numOfFields * sizeof(SSchema);
1,600,980,608✔
422
  STableMeta* p = taosMemoryMalloc(size + schemaExtSize + colRefSize);
1,600,980,608✔
423
  if (NULL == p) return NULL;
1,600,938,700✔
424

425
  memcpy(p, pTableMeta, colRefSize + schemaExtSize + size);
1,600,938,700✔
426
  if (hasSchemaExt) {
1,600,938,700✔
427
    p->schemaExt = (SSchemaExt*)(((char*)p) + size);
1,586,467,314✔
428
  } else {
429
    p->schemaExt = NULL;
14,471,386✔
430
  }
431
  if (hasColRef) {
1,601,009,236✔
432
    p->colRef = (SColRef*)(((char*)p) + size + schemaExtSize);
7,217,610✔
433
  } else {
434
    p->colRef = NULL;
1,593,791,626✔
435
  }
436
  return p;
1,601,008,249✔
437
}
438

439
int32_t trimString(const char* src, int32_t len, char* dst, int32_t dlen) {
2,147,483,647✔
440
  if (len <= 0 || dlen <= 0) return 0;
2,147,483,647✔
441

442
  char    delim = src[0];
2,147,483,647✔
443
  int32_t j = 0;
2,147,483,647✔
444
  for (uint32_t k = 1; k < len - 1; ++k) {
2,147,483,647✔
445
    if (j >= dlen) {
2,147,483,647✔
446
      dst[j - 1] = '\0';
480✔
447
      return j;
480✔
448
    }
449
    if (src[k] == delim && src[k + 1] == delim) {  // deal with "", ''
2,147,483,647✔
450
      dst[j] = src[k + 1];
13,383✔
451
      j++;
13,383✔
452
      k++;
13,383✔
453
      continue;
13,383✔
454
    }
455

456
    if (src[k] == '\\') {  // deal with escape character
2,147,483,647✔
457
      if (src[k + 1] == 'n') {
309,755✔
458
        dst[j] = '\n';
×
459
      } else if (src[k + 1] == 'r') {
309,755✔
460
        dst[j] = '\r';
×
461
      } else if (src[k + 1] == 't') {
309,755✔
462
        dst[j] = '\t';
×
463
      } else if (src[k + 1] == '\\') {
309,755✔
464
        dst[j] = '\\';
6,734✔
465
      } else if (src[k + 1] == '\'') {
303,021✔
466
        dst[j] = '\'';
3,561✔
467
      } else if (src[k + 1] == '"') {
299,460✔
468
        dst[j] = '"';
249,654✔
469
      } else if (src[k + 1] == 'f') {
49,806✔
470
        dst[j] = '\f';
×
471
      } else if (src[k + 1] == 'b') {
49,806✔
472
        dst[j] = '\b';
×
473
      } else if (src[k + 1] == '%' || src[k + 1] == '_' || src[k + 1] == 'x') {
49,806✔
474
        dst[j++] = src[k];
23,262✔
475
        dst[j] = src[k + 1];
23,262✔
476
      } else {
477
        dst[j] = src[k + 1];
26,544✔
478
      }
479
      j++;
309,755✔
480
      k++;
309,755✔
481
      continue;
309,755✔
482
    }
483

484
    dst[j] = src[k];
2,147,483,647✔
485
    j++;
2,147,483,647✔
486
  }
487
  if (j >= dlen) j = dlen - 1;
2,147,483,647✔
488
  dst[j] = '\0';
2,147,483,647✔
489
  return j;
2,147,483,647✔
490
}
491

492
static bool isValidateTag(char* input) {
645,967✔
493
  if (!input) return false;
645,967✔
494
  for (size_t i = 0; i < strlen(input); ++i) {
2,506,288✔
495
#ifdef WINDOWS
496
    if (input[i] < 0x20 || input[i] > 0x7E) return false;
497
#else
498
    if (isprint(input[i]) == 0) return false;
1,861,824✔
499
#endif
500
  }
501
  return true;
644,464✔
502
}
503

504
int32_t parseJsontoTagData(const char* json, SArray* pTagVals, STag** ppTag, void* pMsgBuf, void *charsetCxt) {
676,785✔
505
  int32_t   retCode = TSDB_CODE_SUCCESS;
676,785✔
506
  cJSON*    root = NULL;
676,785✔
507
  SHashObj* keyHash = NULL;
676,785✔
508
  int32_t   size = 0;
676,785✔
509
  // set json NULL data
510
  if (!json || strcasecmp(json, TSDB_DATA_NULL_STR_L) == 0 || strtrim((char*)json) == 0) {
676,785✔
511
    retCode = TSDB_CODE_SUCCESS;
11,837✔
512
    goto end;
11,837✔
513
  }
514

515
  // set json real data
516
  root = cJSON_Parse(json);
664,948✔
517
  if (root == NULL) {
664,948✔
518
    retCode = buildSyntaxErrMsg(pMsgBuf, "json parse error", json);
145,396✔
519
    goto end;
145,396✔
520
  }
521

522
  size = cJSON_GetArraySize(root);
519,552✔
523
  if (!cJSON_IsObject(root)) {
519,552✔
524
    retCode = buildSyntaxErrMsg(pMsgBuf, "json error invalide value", json);
144,391✔
525
    goto end;
144,391✔
526
  }
527

528
  keyHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false);
375,161✔
529
  if (!keyHash) {
375,161✔
530
    retCode = terrno;
×
531
    goto end;
×
532
  }
533
  for (int32_t i = 0; i < size; i++) {
1,017,120✔
534
    cJSON* item = cJSON_GetArrayItem(root, i);
645,967✔
535
    if (!item) {
645,967✔
536
      uError("json inner error:%d", i);
×
537
      retCode = buildSyntaxErrMsg(pMsgBuf, "json inner error", json);
×
538
      goto end;
×
539
    }
540

541
    char* jsonKey = item->string;
645,967✔
542
    if (!isValidateTag(jsonKey)) {
645,967✔
543
      retCode = buildSyntaxErrMsg(pMsgBuf, "json key not validate", jsonKey);
1,503✔
544
      goto end;
1,503✔
545
    }
546
    size_t keyLen = strlen(jsonKey);
644,464✔
547
    if (keyLen > TSDB_MAX_JSON_KEY_LEN) {
644,464✔
548
      uError("json key too long error");
501✔
549
      retCode = buildSyntaxErrMsg(pMsgBuf, "json key too long, more than 256", jsonKey);
501✔
550
      goto end;
501✔
551
    }
552
    if (keyLen == 0 || taosHashGet(keyHash, jsonKey, keyLen) != NULL) {
643,963✔
553
      continue;
2,004✔
554
    }
555
    STagVal val = {0};
641,959✔
556
    //    TSDB_DB_FNAME_LENme, colName);
557
    val.pKey = jsonKey;
641,959✔
558
    retCode = taosHashPut(keyHash, jsonKey, keyLen, &keyLen,
641,959✔
559
                          CHAR_BYTES);  // add key to hash to remove dumplicate, value is useless
560
    if (TSDB_CODE_SUCCESS != retCode) {
641,959✔
561
      goto end;
×
562
    }
563

564
    if (item->type == cJSON_String) {  // add json value  format: type|data
641,959✔
565
      char*   jsonValue = item->valuestring;
614,417✔
566
      int32_t valLen = (int32_t)strlen(jsonValue);
614,417✔
567
      char*   tmp = taosMemoryCalloc(1, valLen * TSDB_NCHAR_SIZE);
614,417✔
568
      if (!tmp) {
614,417✔
569
        retCode = terrno;
×
570
        goto end;
×
571
      }
572
      val.type = TSDB_DATA_TYPE_NCHAR;
614,417✔
573
      if (valLen > 0 && !taosMbsToUcs4(jsonValue, valLen, (TdUcs4*)tmp, (int32_t)(valLen * TSDB_NCHAR_SIZE), &valLen, charsetCxt)) {
614,417✔
574
        uError("charset:%s to %s. val:%s, errno:%s, convert failed.", DEFAULT_UNICODE_ENCODEC,
×
575
               charsetCxt != NULL ? ((SConvInfo *)(charsetCxt))->charset : tsCharset, jsonValue,
576
               strerror(terrno));
577
        retCode = buildSyntaxErrMsg(pMsgBuf, "charset convert json error", jsonValue);
×
578
        taosMemoryFree(tmp);
×
579
        goto end;
×
580
      }
581
      val.nData = valLen;
614,417✔
582
      val.pData = tmp;
614,417✔
583
    } else if (item->type == cJSON_Number) {
27,542✔
584
      if (!isfinite(item->valuedouble)) {
18,326✔
585
        uError("json value is invalidate");
1,002✔
586
        retCode = buildSyntaxErrMsg(pMsgBuf, "json value number is illegal", json);
1,002✔
587
        goto end;
1,002✔
588
      }
589
      val.type = TSDB_DATA_TYPE_DOUBLE;
17,324✔
590
      *((double*)&(val.i64)) = item->valuedouble;
17,324✔
591
    } else if (item->type == cJSON_True || item->type == cJSON_False) {
9,216✔
592
      val.type = TSDB_DATA_TYPE_BOOL;
4,808✔
593
      *((char*)&(val.i64)) = (char)(item->valueint);
4,808✔
594
    } else if (item->type == cJSON_NULL) {
4,408✔
595
      val.type = TSDB_DATA_TYPE_NULL;
3,406✔
596
    } else {
597
      retCode = buildSyntaxErrMsg(pMsgBuf, "invalidate json value", json);
1,002✔
598
      goto end;
1,002✔
599
    }
600
    if (NULL == taosArrayPush(pTagVals, &val)) {
639,955✔
601
      retCode = terrno;
×
602
      goto end;
×
603
    }
604
  }
605

606
end:
371,153✔
607
  taosHashCleanup(keyHash);
676,785✔
608
  if (retCode == TSDB_CODE_SUCCESS) {
676,785✔
609
    retCode = tTagNew(pTagVals, 1, true, ppTag);
382,990✔
610
  }
611
  for (int i = 0; i < taosArrayGetSize(pTagVals); ++i) {
1,316,740✔
612
    STagVal* p = (STagVal*)taosArrayGet(pTagVals, i);
639,955✔
613
    if (IS_VAR_DATA_TYPE(p->type)) {
639,955✔
614
      taosMemoryFreeClear(p->pData);
614,417✔
615
    }
616
  }
617
  cJSON_Delete(root);
676,785✔
618
  return retCode;
676,785✔
619
}
620

621
static int32_t getInsTagsTableTargetNameFromOp(int32_t acctId, SOperatorNode* pOper, SName* pName) {
9,578,009✔
622
  if (OP_TYPE_EQUAL != pOper->opType) {
9,578,009✔
623
    return TSDB_CODE_SUCCESS;
4,734,440✔
624
  }
625

626
  SColumnNode* pCol = NULL;
4,844,099✔
627
  SValueNode*  pVal = NULL;
4,844,099✔
628
  if (QUERY_NODE_COLUMN == nodeType(pOper->pLeft)) {
4,844,099✔
629
    pCol = (SColumnNode*)pOper->pLeft;
4,745,692✔
630
  } else if (QUERY_NODE_VALUE == nodeType(pOper->pLeft)) {
98,366✔
631
    pVal = (SValueNode*)pOper->pLeft;
585✔
632
  }
633
  if (QUERY_NODE_COLUMN == nodeType(pOper->pRight)) {
4,844,058✔
634
    pCol = (SColumnNode*)pOper->pRight;
82✔
635
  } else if (QUERY_NODE_VALUE == nodeType(pOper->pRight)) {
4,843,976✔
636
    pVal = (SValueNode*)pOper->pRight;
4,825,774✔
637
  }
638
  if (NULL == pCol || NULL == pVal) {
4,844,099✔
639
    return TSDB_CODE_SUCCESS;
113,627✔
640
  }
641

642
  const char* valueStr = NULL;
4,730,472✔
643
  int32_t     valueLen = 0;
4,730,472✔
644

645
  if (((0 == strcmp(pCol->colName, "db_name") || 0 == strcmp(pCol->colName, "table_name")) ||
4,730,472✔
646
       0 == strcmp(pCol->colName, "virtual_db_name") || 0 == strcmp(pCol->colName, "virtual_table_name")) &&
1,016,027✔
647
      pVal->placeholderNo != 0) {
3,996,763✔
648
    if (NULL == pVal->datum.p) {
1,170✔
649
      qError("getInsTagsTableTargetNameFromOp: placeholderNo=%d but datum.p is NULL, colName=%s, literal=%s",
×
650
             pVal->placeholderNo, pCol->colName, pVal->literal ? pVal->literal : "NULL");
651
      return TSDB_CODE_SUCCESS;
×
652
    }
653

654
    if (TSDB_DATA_TYPE_NCHAR == pVal->node.resType.type) {
1,170✔
655
      int32_t ucs4Len = varDataLen(pVal->datum.p);
×
656
      char*   tmp = taosMemoryCalloc(1, ucs4Len * TSDB_NCHAR_SIZE + 1);
×
657
      if (NULL == tmp) {
×
658
        return terrno;
×
659
      }
660
      int32_t output = taosUcs4ToMbs((TdUcs4*)varDataVal(pVal->datum.p), ucs4Len, tmp, NULL);
×
661
      if (output < 0) {
×
662
        taosMemoryFree(tmp);
×
663
        return terrno;
×
664
      }
665
      valueStr = tmp;
×
666
      valueLen = output;
×
667
    } else if (IS_VAR_DATA_TYPE(pVal->node.resType.type)) {
1,170✔
668
      valueStr = varDataVal(pVal->datum.p);
1,170✔
669
      valueLen = varDataLen(pVal->datum.p);
1,170✔
670
      qDebug("getInsTagsTableTargetNameFromOp: extracted VARCHAR value, len=%d, value=%.*s", valueLen, valueLen,
1,170✔
671
             valueStr);
672
    } else {
673
      qError("getInsTagsTableTargetNameFromOp: unsupported data type %d for placeholder", pVal->node.resType.type);
×
674
      return TSDB_CODE_INVALID_PARA;
×
675
    }
676
  } else {
677
    if (NULL == pVal->literal || 0 == strcmp(pVal->literal, "")) {
4,729,302✔
678
      return TSDB_CODE_SUCCESS;
320✔
679
    }
680
    valueStr = pVal->literal;
4,728,982✔
681
    valueLen = strlen(pVal->literal);
4,728,982✔
682
  }
683

684
  int32_t code = TSDB_CODE_SUCCESS;
4,730,111✔
685
  bool    needFree = (pVal->placeholderNo != 0 && TSDB_DATA_TYPE_NCHAR == pVal->node.resType.type);
4,730,111✔
686

687
  if (0 == strcmp(pCol->colName, "db_name")) {
4,730,111✔
688
    code = tNameSetDbName(pName, acctId, valueStr, valueLen);
1,981,578✔
689
  } else if (0 == strcmp(pCol->colName, "table_name")) {
2,748,533✔
690
    code = tNameAddTbName(pName, valueStr, valueLen);
1,732,506✔
691
  } else if (0 == strcmp(pCol->colName, "virtual_db_name")) {
1,016,027✔
692
    code = tNameSetDbName(pName, acctId, valueStr, valueLen);
141,159✔
693
  } else if (0 == strcmp(pCol->colName, "virtual_table_name")) {
874,868✔
694
    code = tNameAddTbName(pName, valueStr, valueLen);
141,159✔
695
  }
696

697
  if (needFree) {
4,730,152✔
698
    taosMemoryFree((char*)valueStr);
×
699
  }
700

701
  return code;
4,730,152✔
702
}
703

704
static int32_t getInsTagsTableTargetObjName(int32_t acctId, SNode* pNode, SName* pName) {
6,266,475✔
705
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
6,266,475✔
706
    return getInsTagsTableTargetNameFromOp(acctId, (SOperatorNode*)pNode, pName);
6,255,899✔
707
  }
708
  return TSDB_CODE_SUCCESS;
10,576✔
709
}
710

711
static int32_t getInsTagsTableTargetNameFromCond(int32_t acctId, SLogicConditionNode* pCond, SName* pName) {
2,785,577✔
712
  if (LOGIC_COND_TYPE_AND != pCond->condType) {
2,785,577✔
713
    return TSDB_CODE_SUCCESS;
82✔
714
  }
715

716
  SNode* pNode = NULL;
2,785,495✔
717
  FOREACH(pNode, pCond->pParameterList) {
9,051,970✔
718
    int32_t code = getInsTagsTableTargetObjName(acctId, pNode, pName);
6,266,475✔
719
    if (TSDB_CODE_SUCCESS != code) {
6,266,475✔
720
      return code;
×
721
    }
722
  }
723
  if ('\0' == pName->dbname[0]) {
2,785,495✔
724
    pName->type = 0;
820,953✔
725
  }
726
  return TSDB_CODE_SUCCESS;
2,785,495✔
727
}
728

729
int32_t getVnodeSysTableTargetName(int32_t acctId, SNode* pWhere, SName* pName) {
8,463,602✔
730
  if (NULL == pWhere) {
8,463,602✔
731
    return TSDB_CODE_SUCCESS;
2,355,915✔
732
  }
733

734
  if (QUERY_NODE_OPERATOR == nodeType(pWhere)) {
6,107,687✔
735
    int32_t code = getInsTagsTableTargetNameFromOp(acctId, (SOperatorNode*)pWhere, pName);
3,322,640✔
736
    if (TSDB_CODE_SUCCESS == code && '\0' == pName->dbname[0]) {
3,322,110✔
737
      pName->type = 0;
3,163,874✔
738
    }
739
    return code;
3,322,110✔
740
  }
741

742
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pWhere)) {
2,785,577✔
743
    return getInsTagsTableTargetNameFromCond(acctId, (SLogicConditionNode*)pWhere, pName);
2,785,577✔
744
  }
745

746
  return TSDB_CODE_SUCCESS;
×
747
}
748

749
static int32_t userAuthToString(int32_t acctId, const char* pUser, const char* pDb, const char* pTable, EPrivType type,
2,147,483,647✔
750
                                EPrivObjType objType, char* pStr) {
751
  return snprintf(pStr, USER_AUTH_KEY_MAX_LEN, "`%s`*%d*`%s`*`%s`*%d*%d", pUser, acctId,
2,147,483,647✔
752
                  (pDb && pDb[0] != 0) ? pDb : "", (pTable && pTable[0] != 0) ? pTable : "", type, objType);
2,147,483,647✔
753
}
754

755
static int32_t getIntegerFromAuthStr(const char* pStart, char** pNext) {
2,147,483,647✔
756
  char* p = strchr(pStart, '*');
2,147,483,647✔
757
  char  buf[10] = {0};
2,147,483,647✔
758
  if (NULL == p) {
2,147,483,647✔
759
    tstrncpy(buf, pStart, 10);
864,126,249✔
760
    *pNext = NULL;
864,113,416✔
761
  } else {
762
    int32_t len = (int32_t)(p - pStart); 
1,728,199,716✔
763
    tstrncpy(buf, pStart, len < sizeof(buf) ? len + 1: sizeof(buf)); 
1,728,199,716✔
764
    *pNext = ++p;
1,728,194,361✔
765
  }
766
  return taosStr2Int32(buf, NULL, 10);
2,147,483,647✔
767
}
768

769
static int32_t getBackQuotedStringFromAuthStr(const char* pStart, char* pStr, uint32_t dstLen, char** pNext) {
2,147,483,647✔
770
  const char* pBeginQuote = strchr(pStart, '`');
2,147,483,647✔
771
  if (!pBeginQuote) {
2,147,483,647✔
772
    qWarn("failed to get string from auth string, %s, should be quoted with `", pStart);
×
773
    return TSDB_CODE_INVALID_PARA;
×
774
  }
775
  const char* pEndQuote = strchr(pBeginQuote + 1, '`');
2,147,483,647✔
776
  if (!pEndQuote) {
2,147,483,647✔
777
    qWarn("failed to get string from auth string, %s, should be quoted with `", pStart);
×
778
    return TSDB_CODE_INVALID_PARA;
×
779
  }
780

781
  pStr[0] = '\0';
2,147,483,647✔
782
  TAOS_STRNCPY(pStr, pBeginQuote + 1, TMIN(dstLen, pEndQuote - pBeginQuote - 1));
2,147,483,647✔
783

784
  char* pSeperator = strchr(pEndQuote + 1, '*');
2,147,483,647✔
785
  if (!pSeperator) {
2,147,483,647✔
786
    *pNext = NULL;
×
787
  } else {
788
    *pNext = ++pSeperator;
2,147,483,647✔
789
  }
790
  return 0;
2,147,483,647✔
791
}
792

793
static void getStringFromAuthStr(const char* pStart, char* pStr, uint32_t dstLen, char** pNext) {
×
794
  char* p = strchr(pStart, '*');
×
795
  if (NULL == p) {
×
796
    tstrncpy(pStr, pStart, dstLen);
×
797
    *pNext = NULL;
×
798
  } else {
799
    TAOS_STRNCPY(pStr, pStart, p - pStart);
×
800
    *pNext = ++p;
×
801
  }
802
  if (*pStart == '`' && *(pStart + 1) == '`') {
×
803
    *pStr = 0;
×
804
  }
805
}
×
806

807
static int32_t stringToUserAuth(const char* pStr, int32_t len, SUserAuthInfo* pUserAuth) {
864,116,537✔
808
  char*   p = NULL;
864,116,537✔
809
  int32_t code = getBackQuotedStringFromAuthStr(pStr, pUserAuth->user, TSDB_USER_LEN, &p);
864,120,748✔
810
  if (code == TSDB_CODE_SUCCESS) {
864,118,852✔
811
    pUserAuth->tbName.acctId = getIntegerFromAuthStr(p, &p);
864,121,603✔
812
    code = getBackQuotedStringFromAuthStr(p, pUserAuth->tbName.dbname, TSDB_DB_NAME_LEN, &p);
864,118,692✔
813
  }
814
  if (code == TSDB_CODE_SUCCESS) {
864,118,281✔
815
    code = getBackQuotedStringFromAuthStr(p, pUserAuth->tbName.tname, TSDB_TABLE_NAME_LEN, &p);
864,118,281✔
816
  }
817
  if (code == TSDB_CODE_SUCCESS) {
864,128,152✔
818
    if (pUserAuth->tbName.tname[0]) {
864,127,678✔
819
      pUserAuth->tbName.type = TSDB_TABLE_NAME_T;
366,343,828✔
820
    } else {
821
      pUserAuth->tbName.type = TSDB_DB_NAME_T;
497,774,921✔
822
    }
823
    pUserAuth->privType = getIntegerFromAuthStr(p, &p);
864,112,600✔
824
    pUserAuth->objType = getIntegerFromAuthStr(p, &p);
864,114,131✔
825
  }
826
  return code;
864,121,624✔
827
}
828

829
static int32_t buildTableReq(SHashObj* pTablesHash, SArray** pTables) {
2,147,483,647✔
830
  if (NULL != pTablesHash) {
2,147,483,647✔
831
    *pTables = taosArrayInit(taosHashGetSize(pTablesHash), sizeof(SName));
1,629,263,289✔
832
    if (NULL == *pTables) {
1,629,081,603✔
833
      return terrno;
×
834
    }
835
    void* p = taosHashIterate(pTablesHash, NULL);
1,629,088,085✔
836
    while (NULL != p) {
2,147,483,647✔
837
      size_t len = 0;
1,950,231,300✔
838
      char*  pKey = taosHashGetKey(p, &len);
1,950,232,707✔
839
      char   fullName[TSDB_TABLE_FNAME_LEN] = {0};
1,950,228,884✔
840
      tstrncpy(fullName, pKey, sizeof(fullName) < len ? sizeof(fullName) : len + 1);
1,950,234,026✔
841
      SName   name = {0};
1,950,223,795✔
842
      int32_t code = tNameFromString(&name, fullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
1,950,226,889✔
843
      if (TSDB_CODE_SUCCESS == code) {
1,950,212,350✔
844
        if (NULL == taosArrayPush(*pTables, &name)) {
2,147,483,647✔
845
          code = terrno;
×
846
        }
847
      }
848
      if (TSDB_CODE_SUCCESS != code) {
1,950,211,694✔
849
        taosHashCancelIterate(pTablesHash, p);
×
850
        taosArrayDestroy(*pTables);
×
851
        *pTables = NULL;
×
852
        return code;
×
853
      }
854
      p = taosHashIterate(pTablesHash, p);
1,950,211,694✔
855
    }
856
  }
857
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
858
}
859

860
static int32_t buildDbReq(SHashObj* pDbsHash, SArray** pDbs) {
1,372,697,792✔
861
  if (NULL != pDbsHash) {
1,372,697,792✔
862
    *pDbs = taosArrayInit(taosHashGetSize(pDbsHash), TSDB_DB_FNAME_LEN);
816,692,456✔
863
    if (NULL == *pDbs) {
816,636,722✔
864
      return terrno;
×
865
    }
866
    void* p = taosHashIterate(pDbsHash, NULL);
816,638,719✔
867
    while (NULL != p) {
1,634,575,976✔
868
      size_t len = 0;
817,857,997✔
869
      char*  pKey = taosHashGetKey(p, &len);
817,857,485✔
870
      char   fullName[TSDB_DB_FNAME_LEN] = {0};
817,869,183✔
871
      TAOS_STRNCPY(fullName, pKey, len);
817,863,130✔
872
      if (NULL == taosArrayPush(*pDbs, fullName)) {
1,635,737,410✔
873
        taosHashCancelIterate(pDbsHash, p);
×
874
        taosArrayDestroy(*pDbs);
×
875
        *pDbs = NULL;
×
876
        return terrno;
×
877
      }
878
      p = taosHashIterate(pDbsHash, p);
817,874,280✔
879
    }
880
  }
881
  return TSDB_CODE_SUCCESS;
1,372,723,315✔
882
}
883

884
static int32_t buildTableReqFromDb(SHashObj* pDbsHash, SArray** pDbs) {
2,147,483,647✔
885
  if (NULL != pDbsHash) {
2,147,483,647✔
886
    if (NULL == *pDbs) {
1,364,465,799✔
887
      *pDbs = taosArrayInit(taosHashGetSize(pDbsHash), sizeof(STablesReq));
1,364,490,027✔
888
      if (NULL == *pDbs) {
1,364,386,673✔
889
        return terrno;
×
890
      }
891
    }
892
    SParseTablesMetaReq* p = taosHashIterate(pDbsHash, NULL);
1,364,376,268✔
893
    while (NULL != p) {
2,147,483,647✔
894
      STablesReq req = {0};
1,369,856,965✔
895
      tstrncpy(req.dbFName, p->dbFName, TSDB_DB_FNAME_LEN);
1,369,856,428✔
896
      int32_t code = buildTableReq(p->pTables, &req.pTables);
1,369,857,893✔
897
      if (TSDB_CODE_SUCCESS == code) {
1,369,874,073✔
898
        if (NULL == taosArrayPush(*pDbs, &req)) {
2,147,483,647✔
899
          code = terrno;
×
900
        }
901
      }
902
      if (TSDB_CODE_SUCCESS != code) {
1,369,872,661✔
903
        taosHashCancelIterate(pDbsHash, p);
×
904
        taosArrayDestroy(*pDbs);
×
905
        *pDbs = NULL;
×
906
        return code;
×
907
      }
908
      p = taosHashIterate(pDbsHash, p);
1,369,872,661✔
909
    }
910
  }
911
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
912
}
913

914
static int32_t buildUserAuthReq(SHashObj* pUserAuthHash, SArray** pUserAuth) {
457,566,251✔
915
  if (NULL != pUserAuthHash) {
457,566,251✔
916
    *pUserAuth = taosArrayInit(taosHashGetSize(pUserAuthHash), sizeof(SUserAuthInfo));
449,593,474✔
917
    if (NULL == *pUserAuth) {
449,576,866✔
918
      return terrno;
×
919
    }
920
    void* p = taosHashIterate(pUserAuthHash, NULL);
449,577,972✔
921
    while (NULL != p) {
1,313,729,313✔
922
      size_t len = 0;
864,121,465✔
923
      char*  pKey = taosHashGetKey(p, &len);
864,124,630✔
924
      char   key[USER_AUTH_KEY_MAX_LEN] = {0};
864,127,714✔
925
      TAOS_STRNCPY(key, pKey, len);
864,121,732✔
926
      SUserAuthInfo userAuth = {0};
864,121,732✔
927
      int32_t code = stringToUserAuth(key, len, &userAuth);
864,127,866✔
928
      if (TSDB_CODE_SUCCESS != code) terrno = code;
864,095,234✔
929
      if (code != 0 || NULL == taosArrayPush(*pUserAuth, &userAuth)) {
1,728,212,471✔
930
        taosHashCancelIterate(pUserAuthHash, p);
10,447✔
931
        taosArrayDestroy(*pUserAuth);
×
932
        *pUserAuth = NULL;
×
933
        return terrno;
×
934
      }
935
      p = taosHashIterate(pUserAuthHash, p);
864,112,844✔
936
    }
937
  }
938
  return TSDB_CODE_SUCCESS;
457,580,625✔
939
}
940

941
static int32_t buildUdfReq(SHashObj* pUdfHash, SArray** pUdf) {
457,563,643✔
942
  if (NULL != pUdfHash) {
457,563,643✔
943
    *pUdf = taosArrayInit(taosHashGetSize(pUdfHash), TSDB_FUNC_NAME_LEN);
112,973✔
944
    if (NULL == *pUdf) {
112,973✔
945
      return terrno;
×
946
    }
947
    void* p = taosHashIterate(pUdfHash, NULL);
112,973✔
948
    while (NULL != p) {
265,157✔
949
      size_t len = 0;
151,709✔
950
      char*  pFunc = taosHashGetKey(p, &len);
151,709✔
951
      char   func[TSDB_FUNC_NAME_LEN] = {0};
151,709✔
952
      TAOS_STRNCPY(func, pFunc, len);
151,709✔
953
      if (NULL == taosArrayPush(*pUdf, func)) {
303,418✔
954
        taosHashCancelIterate(pUdfHash, p);
×
955
        taosArrayDestroy(*pUdf);
×
956
        *pUdf = NULL;
×
957
        return terrno;
×
958
      }
959
      p = taosHashIterate(pUdfHash, p);
151,709✔
960
    }
961
  }
962
  return TSDB_CODE_SUCCESS;
457,564,118✔
963
}
964

965
int32_t buildCatalogReq(SParseMetaCache* pMetaCache, SCatalogReq* pCatalogReq) {
457,552,504✔
966
  int32_t code = buildTableReqFromDb(pMetaCache->pTableMeta, &pCatalogReq->pTableMeta);
457,552,504✔
967
  if (TSDB_CODE_SUCCESS == code) {
457,572,505✔
968
    code = buildDbReq(pMetaCache->pDbVgroup, &pCatalogReq->pDbVgroup);
457,574,654✔
969
  }
970
  if (TSDB_CODE_SUCCESS == code) {
457,562,571✔
971
    code = buildTableReqFromDb(pMetaCache->pTableVgroup, &pCatalogReq->pTableHash);
457,552,264✔
972
  }
973
  if (TSDB_CODE_SUCCESS == code) {
457,590,351✔
974
    code = buildDbReq(pMetaCache->pDbCfg, &pCatalogReq->pDbCfg);
457,580,014✔
975
  }
976
  if (TSDB_CODE_SUCCESS == code) {
457,593,665✔
977
    code = buildDbReq(pMetaCache->pDbInfo, &pCatalogReq->pDbInfo);
457,583,421✔
978
  }
979
  if (TSDB_CODE_SUCCESS == code) {
457,591,250✔
980
    code = buildUserAuthReq(pMetaCache->pUserAuth, &pCatalogReq->pUser);
457,580,538✔
981
  }
982
  if (TSDB_CODE_SUCCESS == code) {
457,591,922✔
983
    code = buildUdfReq(pMetaCache->pUdf, &pCatalogReq->pUdf);
457,571,111✔
984
  }
985
  if (TSDB_CODE_SUCCESS == code) {
457,587,840✔
986
    code = buildTableReq(pMetaCache->pTableIndex, &pCatalogReq->pTableIndex);
457,567,928✔
987
  }
988
  if (TSDB_CODE_SUCCESS == code) {
457,594,254✔
989
    code = buildTableReq(pMetaCache->pTableCfg, &pCatalogReq->pTableCfg);
457,575,067✔
990
  }
991
  if (TSDB_CODE_SUCCESS == code) {
457,594,151✔
992
    code = buildTableReqFromDb(pMetaCache->pTableMeta, &pCatalogReq->pTableTSMAs);
457,576,162✔
993
  }
994
  if (TSDB_CODE_SUCCESS == code) {
457,592,469✔
995
    code = buildTableReqFromDb(pMetaCache->pTSMAs, &pCatalogReq->pTSMAs);
457,567,972✔
996
  }
997
  if (TSDB_CODE_SUCCESS == code) {
457,599,868✔
998
    code = buildTableReqFromDb(pMetaCache->pTableName, &pCatalogReq->pTableName);
457,575,562✔
999
  }
1000
#ifdef TD_ENTERPRISE
1001
  if (TSDB_CODE_SUCCESS == code) {
457,597,501✔
1002
    code = buildTableReqFromDb(pMetaCache->pTableMeta, &pCatalogReq->pView);
457,573,479✔
1003
  }
1004
#endif
1005
  if (TSDB_CODE_SUCCESS == code) {
457,603,191✔
1006
    code = buildTableReq(pMetaCache->pVStbRefDbs, &pCatalogReq->pVStbRefDbs);
457,579,141✔
1007
  }
1008
  pCatalogReq->dNodeRequired = pMetaCache->dnodeRequired;
457,580,584✔
1009
  pCatalogReq->forceFetchViewMeta = pMetaCache->forceFetchViewMeta;
457,581,509✔
1010
  return code;
457,575,051✔
1011
}
1012

1013
int32_t createSelectStmtImpl(bool isDistinct, SNodeList* pProjectionList, SNode* pTable, SNodeList* pHint,
776,440,987✔
1014
                             SNode** ppSelect) {
1015
  SSelectStmt* select = NULL;
776,440,987✔
1016
  int32_t      code = nodesMakeNode(QUERY_NODE_SELECT_STMT, (SNode**)&select);
776,441,132✔
1017
  if (NULL == select) {
776,475,942✔
1018
    return code;
×
1019
  }
1020
  select->isDistinct = isDistinct;
776,475,942✔
1021
  select->pProjectionList = pProjectionList;
776,474,810✔
1022
  select->pFromTable = pTable;
776,473,565✔
1023
  snprintf(select->stmtName, TSDB_TABLE_NAME_LEN, "%p", select);
776,473,085✔
1024
  select->timeLineResMode = select->isDistinct ? TIME_LINE_NONE : TIME_LINE_GLOBAL;
776,472,549✔
1025
  select->timeLineCurMode = TIME_LINE_GLOBAL;
776,475,995✔
1026
  select->onlyHasKeepOrderFunc = true;
776,472,776✔
1027
  TAOS_SET_OBJ_ALIGNED(&select->timeRange, TSWINDOW_INITIALIZER); 
776,474,485✔
1028
  select->pHint = pHint;
776,471,096✔
1029
  select->lastProcessByRowFuncId = -1;
776,471,647✔
1030
  *ppSelect = (SNode*)select;
776,475,467✔
1031
  return code;
776,475,049✔
1032
}
1033

1034
static int32_t putMetaDataToHash(const char* pKey, int32_t len, const SArray* pData, int32_t index, SHashObj** pHash) {
2,147,483,647✔
1035
  if (NULL == *pHash) {
2,147,483,647✔
1036
    *pHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
2,147,483,647✔
1037
    if (NULL == *pHash) {
2,147,483,647✔
1038
      return terrno;
×
1039
    }
1040
  }
1041
  SMetaRes* pRes = taosArrayGet(pData, index);
2,147,483,647✔
1042
  return taosHashPut(*pHash, pKey, len, &pRes, POINTER_BYTES);
2,147,483,647✔
1043
}
1044

1045
int32_t getMetaDataFromHash(const char* pKey, int32_t len, SHashObj* pHash, void** pOutput) {
1,932,152,352✔
1046
  SMetaRes** pRes = taosHashGet(pHash, pKey, len);
1,932,152,352✔
1047
  if (NULL == pRes || NULL == *pRes) {
1,932,222,166✔
1048
    parserDebug("%s key: %s get NULL from metadata cache", __func__, pKey);
10,811,023✔
1049
    return TSDB_CODE_PAR_INTERNAL_ERROR;
10,807,802✔
1050
  }
1051
  if (TSDB_CODE_SUCCESS == (*pRes)->code) {
1,921,411,069✔
1052
    *pOutput = (*pRes)->pRes;
1,917,130,146✔
1053
  }
1054
  return (*pRes)->code;
1,921,417,544✔
1055
}
1056

1057
static int32_t putTableDataToCache(const SArray* pTableReq, const SArray* pTableData, SHashObj** pTable) {
1,373,580,738✔
1058
  int32_t ntables = taosArrayGetSize(pTableReq);
1,373,580,738✔
1059
  for (int32_t i = 0; i < ntables; ++i) {
1,678,045,131✔
1060
    char    fullName[TSDB_TABLE_FNAME_LEN];
202,825,377✔
1061
    int32_t code = tNameExtractFullName(taosArrayGet(pTableReq, i), fullName);
304,453,757✔
1062
    if (TSDB_CODE_SUCCESS != code) {
304,453,460✔
1063
      return code;
×
1064
    }
1065
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(fullName, strlen(fullName), pTableData, i, pTable)) {
304,453,460✔
1066
      return TSDB_CODE_OUT_OF_MEMORY;
×
1067
    }
1068
  }
1069
  return TSDB_CODE_SUCCESS;
1,373,591,797✔
1070
}
1071

1072
static int32_t putDbDataToCache(const SArray* pDbReq, const SArray* pDbData, SHashObj** pDb) {
1,373,569,596✔
1073
  int32_t nvgs = taosArrayGetSize(pDbReq);
1,373,569,596✔
1074
  for (int32_t i = 0; i < nvgs; ++i) {
2,147,483,647✔
1075
    char* pDbFName = taosArrayGet(pDbReq, i);
818,426,439✔
1076
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(pDbFName, strlen(pDbFName), pDbData, i, pDb)) {
818,420,254✔
1077
      return TSDB_CODE_OUT_OF_MEMORY;
×
1078
    }
1079
  }
1080
  return TSDB_CODE_SUCCESS;
1,373,597,533✔
1081
}
1082

1083
static int32_t putDbTableDataToCache(const SArray* pDbReq, const SArray* pTableData, SHashObj** pTable) {
2,147,483,647✔
1084
  if (!pTableData || pTableData->size == 0) return TSDB_CODE_SUCCESS;
2,147,483,647✔
1085
  int32_t ndbs = taosArrayGetSize(pDbReq);
1,366,014,371✔
1086
  int32_t tableNo = 0;
1,366,040,365✔
1087
  for (int32_t i = 0; i < ndbs; ++i) {
2,147,483,647✔
1088
    STablesReq* pReq = taosArrayGet(pDbReq, i);
1,029,175,438✔
1089
    int32_t     ntables = taosArrayGetSize(pReq->pTables);
1,029,179,090✔
1090
    for (int32_t j = 0; j < ntables; ++j) {
2,147,483,647✔
1091
      char    fullName[TSDB_TABLE_FNAME_LEN];
931,589,818✔
1092
      int32_t code = tNameExtractFullName(taosArrayGet(pReq->pTables, j), fullName);
1,238,153,321✔
1093
      if (TSDB_CODE_SUCCESS != code) {
1,238,159,036✔
1094
        return code;
×
1095
      }
1096
      if (TSDB_CODE_SUCCESS != putMetaDataToHash(fullName, strlen(fullName), pTableData, tableNo, pTable)) {
1,238,159,036✔
1097
        return TSDB_CODE_OUT_OF_MEMORY;
×
1098
      }
1099
      ++tableNo;
1,238,172,126✔
1100
    }
1101
  }
1102
  return TSDB_CODE_SUCCESS;
1,366,066,695✔
1103
}
1104

1105
static int32_t putUserAuthToCache(const SArray* pUserAuthReq, const SArray* pUserAuthData, SHashObj** pUserAuth) {
457,868,065✔
1106
  int32_t nvgs = taosArrayGetSize(pUserAuthReq);
457,868,065✔
1107
  for (int32_t i = 0; i < nvgs; ++i) {
1,322,624,354✔
1108
    SUserAuthInfo* pUser = taosArrayGet(pUserAuthReq, i);
864,742,077✔
1109
    char           key[USER_AUTH_KEY_MAX_LEN] = {0};
864,744,277✔
1110
    int32_t        len = userAuthToString(pUser->tbName.acctId, pUser->user, pUser->tbName.dbname, pUser->tbName.tname,
864,746,101✔
1111
                                          pUser->privType, pUser->objType, key);
1112
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(key, len, pUserAuthData, i, pUserAuth)) {
864,737,371✔
1113
      return TSDB_CODE_OUT_OF_MEMORY;
×
1114
    }
1115
  }
1116
  return TSDB_CODE_SUCCESS;
457,882,277✔
1117
}
1118

1119
static int32_t putUdfToCache(const SArray* pUdfReq, const SArray* pUdfData, SHashObj** pUdf) {
457,869,965✔
1120
  int32_t num = taosArrayGetSize(pUdfReq);
457,869,965✔
1121
  for (int32_t i = 0; i < num; ++i) {
458,024,794✔
1122
    char* pFunc = taosArrayGet(pUdfReq, i);
151,709✔
1123
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(pFunc, strlen(pFunc), pUdfData, i, pUdf)) {
151,709✔
1124
      return TSDB_CODE_OUT_OF_MEMORY;
×
1125
    }
1126
  }
1127
  return TSDB_CODE_SUCCESS;
457,873,085✔
1128
}
1129

1130
int32_t putMetaDataToCache(const SCatalogReq* pCatalogReq, SMetaData* pMetaData, SParseMetaCache* pMetaCache) {
457,867,306✔
1131
  int32_t code = putDbTableDataToCache(pCatalogReq->pTableMeta, pMetaData->pTableMeta, &pMetaCache->pTableMeta);
457,867,306✔
1132
  if (TSDB_CODE_SUCCESS == code) {
457,862,287✔
1133
    code = putDbDataToCache(pCatalogReq->pDbVgroup, pMetaData->pDbVgroup, &pMetaCache->pDbVgroup);
457,864,048✔
1134
  }
1135
  if (TSDB_CODE_SUCCESS == code) {
457,869,019✔
1136
    code = putDbTableDataToCache(pCatalogReq->pTableHash, pMetaData->pTableHash, &pMetaCache->pTableVgroup);
457,867,865✔
1137
  }
1138
  if (TSDB_CODE_SUCCESS == code) {
457,873,747✔
1139
    code = putDbDataToCache(pCatalogReq->pDbCfg, pMetaData->pDbCfg, &pMetaCache->pDbCfg);
457,873,034✔
1140
  }
1141
  if (TSDB_CODE_SUCCESS == code) {
457,877,631✔
1142
    code = putDbDataToCache(pCatalogReq->pDbInfo, pMetaData->pDbInfo, &pMetaCache->pDbInfo);
457,877,086✔
1143
  }
1144
  if (TSDB_CODE_SUCCESS == code) {
457,870,365✔
1145
    code = putUserAuthToCache(pCatalogReq->pUser, pMetaData->pUser, &pMetaCache->pUserAuth);
457,870,237✔
1146
  }
1147
  if (TSDB_CODE_SUCCESS == code) {
457,877,778✔
1148
    code = putUdfToCache(pCatalogReq->pUdf, pMetaData->pUdfList, &pMetaCache->pUdf);
457,875,739✔
1149
  }
1150
  if (TSDB_CODE_SUCCESS == code) {
457,875,329✔
1151
    code = putTableDataToCache(pCatalogReq->pTableIndex, pMetaData->pTableIndex, &pMetaCache->pTableIndex);
457,873,834✔
1152
  }
1153
  if (TSDB_CODE_SUCCESS == code) {
457,867,276✔
1154
    code = putTableDataToCache(pCatalogReq->pTableCfg, pMetaData->pTableCfg, &pMetaCache->pTableCfg);
457,867,035✔
1155
  }
1156
  if (TSDB_CODE_SUCCESS == code) {
457,869,193✔
1157
    code = putDbTableDataToCache(pCatalogReq->pTableTSMAs, pMetaData->pTableTsmas, &pMetaCache->pTableTSMAs);
457,869,346✔
1158
  }
1159
  if (TSDB_CODE_SUCCESS == code) {
457,867,575✔
1160
    code = putDbTableDataToCache(pCatalogReq->pTSMAs, pMetaData->pTsmas, &pMetaCache->pTSMAs);
457,864,473✔
1161
  }
1162
  if (TSDB_CODE_SUCCESS == code) {
457,868,636✔
1163
    code = putDbTableDataToCache(pCatalogReq->pTableName, pMetaData->pTableMeta, &pMetaCache->pTableName);
457,866,826✔
1164
  }
1165
#ifdef TD_ENTERPRISE
1166
  if (TSDB_CODE_SUCCESS == code) {
457,871,517✔
1167
    code = putDbTableDataToCache(pCatalogReq->pView, pMetaData->pView, &pMetaCache->pViews);
457,869,850✔
1168
  }
1169
#endif
1170
  if (TSDB_CODE_SUCCESS == code) {
457,877,395✔
1171
    code = putTableDataToCache(pCatalogReq->pVStbRefDbs, pMetaData->pVStbRefDbs, &pMetaCache->pVStbRefDbs);
457,875,739✔
1172
  }
1173

1174
  pMetaCache->pDnodes = pMetaData->pDnodeList;
457,872,667✔
1175
  return code;
457,871,026✔
1176
}
1177

1178
static int32_t reserveTableReqInCacheImpl(const char* pTbFName, int32_t len, SHashObj** pTables) {
2,147,483,647✔
1179
  if (NULL == *pTables) {
2,147,483,647✔
1180
    *pTables = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
945,827,869✔
1181
    if (NULL == *pTables) {
945,841,733✔
1182
      return terrno;
×
1183
    }
1184
  }
1185
  return taosHashPut(*pTables, pTbFName, len, &nullPointer, POINTER_BYTES);
2,147,483,647✔
1186
}
1187

1188
static int32_t reserveTableReqInCache(int32_t acctId, const char* pDb, const char* pTable, SHashObj** pTables) {
2,147,483,647✔
1189
  char    fullName[TSDB_TABLE_FNAME_LEN];
1,171,602,330✔
1190
  int32_t len = snprintf(fullName, sizeof(fullName), "%d.%s.%s", acctId, pDb, pTable);
2,147,483,647✔
1191
  return reserveTableReqInCacheImpl(fullName, len, pTables);
2,147,483,647✔
1192
}
1193

1194
static int32_t reserveTableReqInDbCacheImpl(int32_t acctId, const char* pDb, const char* pTable, SHashObj* pDbs) {
686,040,528✔
1195
  SParseTablesMetaReq req = {0};
686,040,528✔
1196
  int32_t             len = snprintf(req.dbFName, sizeof(req.dbFName), "%d.%s", acctId, pDb);
686,043,834✔
1197
  int32_t             code = reserveTableReqInCache(acctId, pDb, pTable, &req.pTables);
686,043,834✔
1198
  if (TSDB_CODE_SUCCESS == code) {
686,067,869✔
1199
    code = taosHashPut(pDbs, req.dbFName, len, &req, sizeof(SParseTablesMetaReq));
686,061,858✔
1200
  }
1201
  return code;
686,077,510✔
1202
}
1203

1204
static int32_t reserveTableReqInDbCache(int32_t acctId, const char* pDb, const char* pTable, SHashObj** pDbs) {
1,901,953,058✔
1205
  if (NULL == *pDbs) {
1,901,953,058✔
1206
    *pDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
683,475,172✔
1207
    if (NULL == *pDbs) {
683,509,727✔
1208
      return terrno;
×
1209
    }
1210
  }
1211
  char                 fullName[TSDB_DB_FNAME_LEN];
958,021,727✔
1212
  int32_t              len = snprintf(fullName, sizeof(fullName), "%d.%s", acctId, pDb);
1,901,985,157✔
1213
  SParseTablesMetaReq* pReq = taosHashGet(*pDbs, fullName, len);
1,901,985,157✔
1214
  if (NULL == pReq) {
1,902,065,969✔
1215
    return reserveTableReqInDbCacheImpl(acctId, pDb, pTable, *pDbs);
686,053,765✔
1216
  }
1217
  return reserveTableReqInCache(acctId, pDb, pTable, &pReq->pTables);
1,216,012,204✔
1218
}
1219

1220
int32_t reserveTableMetaInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
949,917,372✔
1221
  return reserveTableReqInDbCache(acctId, pDb, pTable, &pMetaCache->pTableMeta);
949,917,372✔
1222
}
1223

1224
int32_t reserveTableMetaInCacheExt(const SName* pName, SParseMetaCache* pMetaCache) {
×
1225
  return reserveTableReqInDbCache(pName->acctId, pName->dbname, pName->tname, &pMetaCache->pTableMeta);
×
1226
}
1227

1228
int32_t reserveTableUidInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
199,553✔
1229
  return reserveTableReqInDbCache(acctId, pDb, pTable, &pMetaCache->pTableName);
199,553✔
1230
}
1231

1232
int32_t getTableMetaFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta) {
1,067,635,328✔
1233
  char    fullName[TSDB_TABLE_FNAME_LEN];
648,913,435✔
1234
  int32_t code = tNameExtractFullName(pName, fullName);
1,067,634,028✔
1235
  if (TSDB_CODE_SUCCESS != code) {
1,067,685,326✔
1236
    return code;
×
1237
  }
1238
  STableMeta* pTableMeta = NULL;
1,067,685,326✔
1239
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableMeta, (void**)&pTableMeta);
1,067,685,020✔
1240
  if (TSDB_CODE_SUCCESS == code) {
1,067,680,795✔
1241
    *pMeta = tableMetaDup(pTableMeta);
1,064,841,801✔
1242
    if (NULL == *pMeta) {
1,064,729,572✔
1243
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1244
    }
1245
  }
1246
  return code;
1,067,569,598✔
1247
}
1248

1249
int32_t getTableNameFromCache(SParseMetaCache* pMetaCache, const SName* pName, char* pTbName) {
86,100✔
1250
  char    fullName[TSDB_TABLE_FNAME_LEN];
86,100✔
1251
  int32_t code = tNameExtractFullName(pName, fullName);
86,100✔
1252
  if (TSDB_CODE_SUCCESS != code) {
86,100✔
1253
    return code;
×
1254
  }
1255
  const STableMeta* pMeta = NULL;
86,100✔
1256
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableName, (void**)&pMeta);
86,100✔
1257
  if (TSDB_CODE_SUCCESS == code) {
86,100✔
1258
    if (!pMeta) code = TSDB_CODE_PAR_INTERNAL_ERROR;
84,112✔
1259
    int32_t metaSize =
84,112✔
1260
        sizeof(STableMeta) + sizeof(SSchema) * (pMeta->tableInfo.numOfColumns + pMeta->tableInfo.numOfTags);
84,112✔
1261
    int32_t schemaExtSize =
84,112✔
1262
        (withExtSchema(pMeta->tableType) && pMeta->schemaExt) ? sizeof(SSchemaExt) * pMeta->tableInfo.numOfColumns : 0;
168,224✔
1263
    int32_t colRefSize = (hasRefCol(pMeta->tableType) && pMeta->colRef) ? sizeof(SColRef) * pMeta->numOfColRefs : 0;
84,112✔
1264
    const char* pTableName = (const char*)pMeta + metaSize + schemaExtSize + colRefSize;
84,112✔
1265
    tstrncpy(pTbName, pTableName, TSDB_TABLE_NAME_LEN);
84,112✔
1266
  }
1267

1268
  return code;
86,100✔
1269
}
1270

1271
int32_t buildTableMetaFromViewMeta(STableMeta** pMeta, SViewMeta* pViewMeta) {
580,445✔
1272
  *pMeta = taosMemoryCalloc(1, sizeof(STableMeta) + pViewMeta->numOfCols * sizeof(SSchema));
580,445✔
1273
  if (NULL == *pMeta) {
580,445✔
1274
    return terrno;
×
1275
  }
1276
  (*pMeta)->uid = pViewMeta->viewId;
580,445✔
1277
  (*pMeta)->ownerId = pViewMeta->ownerId;
580,445✔
1278
  (*pMeta)->vgId = MNODE_HANDLE;
580,445✔
1279
  (*pMeta)->tableType = TSDB_VIEW_TABLE;
580,445✔
1280
  (*pMeta)->sversion = pViewMeta->version;
580,445✔
1281
  (*pMeta)->tversion = pViewMeta->version;
580,445✔
1282
  (*pMeta)->rversion = pViewMeta->version;
580,445✔
1283
  (*pMeta)->tableInfo.precision = pViewMeta->precision;
580,445✔
1284
  (*pMeta)->tableInfo.numOfColumns = pViewMeta->numOfCols;
580,445✔
1285
  memcpy((*pMeta)->schema, pViewMeta->pSchema, sizeof(SSchema) * pViewMeta->numOfCols);
580,445✔
1286

1287
  for (int32_t i = 0; i < pViewMeta->numOfCols; ++i) {
2,480,391✔
1288
    (*pMeta)->tableInfo.rowSize += (*pMeta)->schema[i].bytes;
1,899,946✔
1289
  }
1290
  return TSDB_CODE_SUCCESS;
580,445✔
1291
}
1292

1293
int32_t getViewMetaFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta) {
1,885,712✔
1294
  char    fullName[TSDB_TABLE_FNAME_LEN];
1,846,340✔
1295
  int32_t code = tNameExtractFullName(pName, fullName);
1,885,712✔
1296
  if (TSDB_CODE_SUCCESS != code) {
1,885,712✔
1297
    return code;
×
1298
  }
1299
  SViewMeta* pViewMeta = NULL;
1,885,712✔
1300
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pViews, (void**)&pViewMeta);
1,885,712✔
1301
  if (TSDB_CODE_SUCCESS == code) {
1,885,712✔
1302
    code = buildTableMetaFromViewMeta(pMeta, pViewMeta);
571,319✔
1303
  }
1304
  return code;
1,885,712✔
1305
}
1306

1307
static int32_t reserveDbReqInCache(int32_t acctId, const char* pDb, SHashObj** pDbs) {
1,733,562,980✔
1308
  if (NULL == *pDbs) {
1,733,562,980✔
1309
    *pDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
816,677,654✔
1310
    if (NULL == *pDbs) {
816,685,864✔
1311
      return terrno;
×
1312
    }
1313
  }
1314
  char    fullName[TSDB_TABLE_FNAME_LEN];
596,519,690✔
1315
  int32_t len = snprintf(fullName, sizeof(fullName), "%d.%s", acctId, pDb);
1,733,567,374✔
1316
  return taosHashPut(*pDbs, fullName, len, &nullPointer, POINTER_BYTES);
1,733,567,374✔
1317
}
1318

1319
int32_t reserveDbVgInfoInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
724,497,582✔
1320
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbVgroup);
724,497,582✔
1321
}
1322

1323
int32_t getDbVgInfoFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, SArray** pVgInfo) {
168,644,737✔
1324
  SArray* pVgList = NULL;
168,644,737✔
1325
  int32_t code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbVgroup, (void**)&pVgList);
168,644,737✔
1326
  // pVgList is null, which is a legal value, indicating that the user DB has not been created
1327
  if (TSDB_CODE_SUCCESS == code && NULL != pVgList) {
168,656,112✔
1328
    *pVgInfo = taosArrayDup(pVgList, NULL);
168,650,092✔
1329
    if (NULL == *pVgInfo) {
168,647,329✔
1330
      code = terrno;
×
1331
    }
1332
  }
1333
  return code;
168,654,085✔
1334
}
1335

1336
int32_t reserveTableVgroupInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
951,519,867✔
1337
  return reserveTableReqInDbCache(acctId, pDb, pTable, &pMetaCache->pTableVgroup);
951,519,867✔
1338
}
1339

1340
int32_t reserveTableVgroupInCacheExt(const SName* pName, SParseMetaCache* pMetaCache) {
401,479✔
1341
  return reserveTableReqInDbCache(pName->acctId, pName->dbname, pName->tname, &pMetaCache->pTableVgroup);
401,479✔
1342
}
1343

1344
int32_t getTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup) {
548,378,493✔
1345
  char    fullName[TSDB_TABLE_FNAME_LEN];
237,805,921✔
1346
  int32_t code = tNameExtractFullName(pName, fullName);
548,379,879✔
1347
  if (TSDB_CODE_SUCCESS != code) {
548,401,126✔
1348
    return code;
×
1349
  }
1350
  SVgroupInfo* pVg = NULL;
548,401,126✔
1351
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableVgroup, (void**)&pVg);
548,397,085✔
1352
  if (TSDB_CODE_SUCCESS == code) {
548,397,447✔
1353
    memcpy(pVgroup, pVg, sizeof(SVgroupInfo));
548,395,986✔
1354
  }
1355
  return code;
548,397,447✔
1356
}
1357

1358
int32_t getDbTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup) {
54,922✔
1359
  char    fullName[TSDB_TABLE_FNAME_LEN];
54,922✔
1360
  int32_t code = TSDB_CODE_SUCCESS;
54,922✔
1361

1362
  PAR_ERR_JRET(tNameExtractFullName(pName, fullName));
54,922✔
1363

1364
  const char* pDb = strstr(fullName, ".");
54,922✔
1365
  if (pDb == NULL) {
54,922✔
1366
    parserError("%s failed, invalid full name:%s", __func__, fullName);
×
1367
    return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1368
  }
1369
  pDb = strstr(pDb + 1, ".");
54,922✔
1370
  if (pDb == NULL) {
54,922✔
1371
    parserError("%s failed, invalid full name:%s", __func__, fullName);
×
1372
    return TSDB_CODE_PAR_INTERNAL_ERROR;
×
1373
  }
1374

1375
  int32_t     fullDbLen = pDb - fullName;
54,922✔
1376
  int32_t     fullTbLen = strlen(fullName);
54,922✔
1377
  SArray*     pVgArray = NULL;
54,922✔
1378
  SDbCfgInfo* pDbCfg = NULL;
54,922✔
1379

1380
  PAR_ERR_JRET(getMetaDataFromHash(fullName, fullDbLen, pMetaCache->pDbVgroup, (void**)&pVgArray));
54,922✔
1381
  PAR_ERR_JRET(getMetaDataFromHash(fullName, fullDbLen, pMetaCache->pDbCfg, (void**)&pDbCfg));
54,922✔
1382

1383
  uint32_t hashValue =
54,922✔
1384
      taosGetTbHashVal(fullName, fullTbLen, pDbCfg->hashMethod, pDbCfg->hashPrefix, pDbCfg->hashSuffix);
54,922✔
1385
  SVgroupInfo* pVg = taosArraySearch(pVgArray, &hashValue, ctgHashValueComp, TD_EQ);
54,922✔
1386
  if (pVg) {
54,922✔
1387
    memcpy(pVgroup, pVg, sizeof(SVgroupInfo));
54,922✔
1388
  } else {
1389
    parserError("%s failed, can't find vgroup for table:%s", __func__, fullName);
×
1390
    PAR_ERR_JRET(TSDB_CODE_PAR_INTERNAL_ERROR);
×
1391
  }
1392

1393
  return code;
54,922✔
1394
_return:
×
1395
  parserError("%s failed, code:%d", __func__, code);
×
1396
  return code;
×
1397
}
1398

1399
int32_t reserveDbVgVersionInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
94,680,460✔
1400
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbInfo);
94,680,460✔
1401
}
1402

1403
int32_t getDbVgVersionFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, int32_t* pVersion, int64_t* pDbId,
94,674,609✔
1404
                                int32_t* pTableNum, int64_t* pStateTs) {
1405
  SDbInfo* pDbInfo = NULL;
94,674,609✔
1406
  int32_t  code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbInfo, (void**)&pDbInfo);
94,674,609✔
1407
  if (TSDB_CODE_SUCCESS == code) {
94,680,261✔
1408
    *pVersion = pDbInfo->vgVer;
94,680,300✔
1409
    taosSetInt64Aligned(pDbId, pDbInfo->dbId);
94,680,300✔
1410
    *pTableNum = pDbInfo->tbNum;
94,680,300✔
1411
    taosSetInt64Aligned(pStateTs, pDbInfo->stateTs);
94,680,300✔
1412
  }
1413
  return code;
94,680,138✔
1414
}
1415

1416
int32_t reserveDbCfgInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
914,450,799✔
1417
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbCfg);
914,450,799✔
1418
}
1419

1420
int32_t getDbCfgFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, SDbCfgInfo* pInfo) {
32,606,768✔
1421
  SDbCfgInfo* pDbCfg = NULL;
32,606,768✔
1422
  int32_t     code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbCfg, (void**)&pDbCfg);
32,606,844✔
1423
  if (TSDB_CODE_SUCCESS == code) {
32,608,850✔
1424
    memcpy(pInfo, pDbCfg, sizeof(SDbCfgInfo));
32,530,708✔
1425
  }
1426
  return code;
32,608,850✔
1427
}
1428

1429
static int32_t reserveUserAuthInCacheImpl(const char* pKey, int32_t len, SParseMetaCache* pMetaCache) {
1,859,273,423✔
1430
  if (NULL == pMetaCache->pUserAuth) {
1,859,273,423✔
1431
    pMetaCache->pUserAuth = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
449,584,460✔
1432
    if (NULL == pMetaCache->pUserAuth) {
449,599,761✔
1433
      return terrno;
×
1434
    }
1435
  }
1436
  return taosHashPut(pMetaCache->pUserAuth, pKey, len, &nullPointer, POINTER_BYTES);
1,859,291,019✔
1437
}
1438

1439
int32_t reserveUserAuthInCache(int32_t acctId, const char* pUser, const char* pDb, const char* pTable, EPrivType privType, EPrivObjType objType,
1,859,073,542✔
1440
                               SParseMetaCache* pMetaCache) {
1441
  char    key[USER_AUTH_KEY_MAX_LEN] = {0};
1,859,073,542✔
1442
  int32_t len = userAuthToString(acctId, pUser, pDb, pTable, privType, objType, key);
1,859,076,034✔
1443
  return reserveUserAuthInCacheImpl(key, len, pMetaCache);
1,859,107,269✔
1444
}
1445

1446
int32_t reserveViewUserAuthInCache(int32_t acctId, const char* pUser, const char* pDb, const char* pTable,
177,236✔
1447
                                   EPrivType privType, EPrivObjType objType, SParseMetaCache* pMetaCache) {
1448
  char    key[USER_AUTH_KEY_MAX_LEN] = {0};
177,236✔
1449
  int32_t len = userAuthToString(acctId, pUser, pDb, pTable, privType, objType, key);
177,236✔
1450
  return reserveUserAuthInCacheImpl(key, len, pMetaCache);
177,236✔
1451
}
1452

1453
int32_t getUserAuthFromCache(SParseMetaCache* pMetaCache, SUserAuthInfo* pAuthReq, SUserAuthRes* pAuthRes) {
2,194,644✔
1454
  char          key[USER_AUTH_KEY_MAX_LEN] = {0};
2,194,644✔
1455
  int32_t       len = userAuthToString(pAuthReq->tbName.acctId, pAuthReq->user, pAuthReq->tbName.dbname,
4,329,704✔
1456
                                       pAuthReq->tbName.tname, pAuthReq->privType, pAuthReq->objType, key);
2,194,644✔
1457
  SUserAuthRes* pAuth = NULL;
2,194,644✔
1458
  int32_t       code = getMetaDataFromHash(key, len, pMetaCache->pUserAuth, (void**)&pAuth);
2,194,644✔
1459
  if (TSDB_CODE_SUCCESS == code) {
2,194,644✔
1460
    memcpy(pAuthRes, pAuth, sizeof(SUserAuthRes));
2,194,644✔
1461
  }
1462
  return code;
2,194,644✔
1463
}
1464

1465
int32_t reserveUdfInCache(const char* pFunc, SParseMetaCache* pMetaCache) {
204,667✔
1466
  if (NULL == pMetaCache->pUdf) {
204,667✔
1467
    pMetaCache->pUdf = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
112,973✔
1468
    if (NULL == pMetaCache->pUdf) {
112,973✔
1469
      return terrno;
×
1470
    }
1471
  }
1472
  return taosHashPut(pMetaCache->pUdf, pFunc, strlen(pFunc), &nullPointer, POINTER_BYTES);
204,667✔
1473
}
1474

1475
int32_t getUdfInfoFromCache(SParseMetaCache* pMetaCache, const char* pFunc, SFuncInfo* pInfo) {
201,080✔
1476
  SFuncInfo* pFuncInfo = NULL;
201,080✔
1477
  int32_t    code = getMetaDataFromHash(pFunc, strlen(pFunc), pMetaCache->pUdf, (void**)&pFuncInfo);
201,080✔
1478
  if (TSDB_CODE_SUCCESS == code) {
201,080✔
1479
    memcpy(pInfo, pFuncInfo, sizeof(SFuncInfo));
196,016✔
1480
  }
1481
  return code;
201,080✔
1482
}
1483

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

1486
static SArray* smaIndexesDup(SArray* pSrc) {
×
1487
  SArray* pDst = taosArrayDup(pSrc, NULL);
×
1488
  if (NULL == pDst) {
×
1489
    return NULL;
×
1490
  }
1491
  int32_t size = taosArrayGetSize(pDst);
×
1492
  for (int32_t i = 0; i < size; ++i) {
×
1493
    ((STableIndexInfo*)taosArrayGet(pDst, i))->expr = NULL;
×
1494
  }
1495
  for (int32_t i = 0; i < size; ++i) {
×
1496
    STableIndexInfo* pIndex = taosArrayGet(pDst, i);
×
1497
    pIndex->expr = taosStrdup(((STableIndexInfo*)taosArrayGet(pSrc, i))->expr);
×
1498
    if (NULL == pIndex->expr) {
×
1499
      taosArrayDestroyEx(pDst, destroySmaIndex);
×
1500
      return NULL;
×
1501
    }
1502
  }
1503
  return pDst;
×
1504
}
1505

1506
int32_t reserveTableIndexInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
×
1507
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableIndex);
×
1508
}
1509

1510
int32_t reserveTableCfgInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
96,921✔
1511
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableCfg);
96,921✔
1512
}
1513

1514
int32_t reserveTableTSMAInfoInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
404,276✔
1515
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableTSMAs);
404,276✔
1516
}
1517

1518
int32_t reserveTSMAInfoInCache(int32_t acctId, const char* pDb, const char* pTsmaName, SParseMetaCache* pMetaCache) {
4,710✔
1519
  return reserveTableReqInDbCache(acctId, pDb, pTsmaName, &pMetaCache->pTSMAs);
4,710✔
1520
}
1521

1522
int32_t reserveVStbRefDbsInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
673,150,507✔
1523
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pVStbRefDbs);
673,150,507✔
1524
}
1525

1526
int32_t getTableIndexFromCache(SParseMetaCache* pMetaCache, const SName* pName, SArray** pIndexes) {
×
1527
  char    fullName[TSDB_TABLE_FNAME_LEN];
×
1528
  int32_t code = tNameExtractFullName(pName, fullName);
×
1529
  if (TSDB_CODE_SUCCESS != code) return code;
×
1530
  ;
1531
  SArray* pSmaIndexes = NULL;
×
1532
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableIndex, (void**)&pSmaIndexes);
×
1533
  if (TSDB_CODE_SUCCESS == code && NULL != pSmaIndexes) {
×
1534
    *pIndexes = smaIndexesDup(pSmaIndexes);
×
1535
    if (NULL == *pIndexes) {
×
1536
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1537
    }
1538
  }
1539
  return code;
×
1540
}
1541

1542
int32_t getTableTsmasFromCache(SParseMetaCache* pMetaCache, const SName* pTbName, SArray** pTsmas) {
11,114,110✔
1543
  char    tbFName[TSDB_TABLE_FNAME_LEN];
11,105,487✔
1544
  int32_t code = tNameExtractFullName(pTbName, tbFName);
11,114,110✔
1545
  if (TSDB_CODE_SUCCESS != code) {
11,114,110✔
1546
    return code;
×
1547
  }
1548
  STableTSMAInfoRsp* pTsmasRsp = NULL;
11,114,110✔
1549
  code = getMetaDataFromHash(tbFName, strlen(tbFName), pMetaCache->pTableTSMAs, (void**)&pTsmasRsp);
11,114,110✔
1550
  if (TSDB_CODE_SUCCESS == code && pTsmasRsp) {
11,114,110✔
1551
    *pTsmas = pTsmasRsp->pTsmas;
324,828✔
1552
  }
1553
  return TSDB_CODE_SUCCESS;
11,114,110✔
1554
}
1555

1556
int32_t getTsmaFromCache(SParseMetaCache* pMetaCache, const SName* pTsmaName, STableTSMAInfo** pTsma) {
4,426✔
1557
  char    tsmaFName[TSDB_TABLE_FNAME_LEN];
4,426✔
1558
  int32_t code = TSDB_CODE_SUCCESS;
4,426✔
1559

1560
  PAR_ERR_JRET(tNameExtractFullName(pTsmaName, tsmaFName));
4,426✔
1561

1562
  STableTSMAInfoRsp* pTsmaRsp = NULL;
4,426✔
1563
  code = getMetaDataFromHash(tsmaFName, strlen(tsmaFName), pMetaCache->pTSMAs, (void**)&pTsmaRsp);
4,426✔
1564
  if (TSDB_CODE_SUCCESS == code) {
4,426✔
1565
    if (!pTsmaRsp || pTsmaRsp->pTsmas->size != 1) {
3,670✔
1566
      parserError("%s failed at line: %d, invalid TSMA info", __func__, __LINE__);
×
1567
      PAR_ERR_JRET(TSDB_CODE_PAR_INTERNAL_ERROR);
×
1568
    }
1569
    *pTsma = taosArrayGetP(pTsmaRsp->pTsmas, 0);
3,670✔
1570
  } else if (code == TSDB_CODE_PAR_INTERNAL_ERROR) {
756✔
1571
    PAR_ERR_JRET(TSDB_CODE_MND_SMA_NOT_EXIST);
756✔
1572
  }
1573
  return code;
3,670✔
1574

1575
_return:
756✔
1576
  parserError("%s failed, code:%d", __func__, code);
756✔
1577
  return code;
756✔
1578
}
1579

1580
STableCfg* tableCfgDup(STableCfg* pCfg) {
88,578✔
1581
  STableCfg* pNew = taosMemoryMalloc(sizeof(*pNew));
88,578✔
1582
  if (!pNew) {
88,578✔
1583
    return NULL;
×
1584
  }
1585
  memcpy(pNew, pCfg, sizeof(*pNew));
88,578✔
1586
  pNew->pComment = NULL;
88,578✔
1587
  pNew->pFuncs = NULL;
88,578✔
1588
  pNew->pTags = NULL;
88,578✔
1589
  pNew->pSchemas = NULL;
88,578✔
1590
  pNew->pSchemaExt = NULL;
88,578✔
1591
  pNew->pColRefs = NULL;
88,578✔
1592
  pNew->pTagRefs = NULL;
88,578✔
1593
  if (NULL != pCfg->pComment) {
88,578✔
1594
    pNew->pComment = taosMemoryCalloc(pNew->commentLen + 1, 1);
×
1595
    if (!pNew->pComment) goto err;
×
1596
    memcpy(pNew->pComment, pCfg->pComment, pNew->commentLen);
×
1597
  }
1598
  if (NULL != pCfg->pFuncs) {
88,578✔
1599
    pNew->pFuncs = taosArrayDup(pCfg->pFuncs, NULL);
×
1600
    if (!pNew->pFuncs) goto err;
×
1601
  }
1602
  if (NULL != pCfg->pTags) {
88,578✔
1603
    pNew->pTags = taosMemoryCalloc(pCfg->tagsLen + 1, 1);
38,383✔
1604
    if (!pNew->pTags) goto err;
38,383✔
1605
    memcpy(pNew->pTags, pCfg->pTags, pNew->tagsLen);
38,383✔
1606
  }
1607

1608
  int32_t schemaSize = (pCfg->numOfColumns + pCfg->numOfTags) * sizeof(SSchema);
88,578✔
1609

1610
  SSchema* pSchema = taosMemoryMalloc(schemaSize);
88,578✔
1611
  if (!pSchema) goto err;
88,578✔
1612
  memcpy(pSchema, pCfg->pSchemas, schemaSize);
88,578✔
1613
  pNew->pSchemas = pSchema;
88,578✔
1614

1615
  SSchemaExt* pSchemaExt = NULL;
88,578✔
1616
  if (withExtSchema(pCfg->tableType) && pCfg->pSchemaExt) {
88,578✔
1617
    int32_t schemaExtSize = pCfg->numOfColumns * sizeof(SSchemaExt);
74,580✔
1618
    pSchemaExt = taosMemoryMalloc(schemaExtSize);
74,580✔
1619
    if (!pSchemaExt) goto err;
74,580✔
1620
    memcpy(pSchemaExt, pCfg->pSchemaExt, schemaExtSize);
74,580✔
1621
  }
1622

1623
  pNew->pSchemaExt = pSchemaExt;
88,578✔
1624

1625
  SColRef *pColRef = NULL;
88,578✔
1626
  if (hasRefCol(pCfg->tableType) && pCfg->pColRefs) {
88,578✔
1627
    int32_t colRefSize = pCfg->numOfColumns * sizeof(SColRef);
8,056✔
1628
    pColRef = taosMemoryMalloc(colRefSize);
8,056✔
1629
    if (!pColRef) goto err;
8,056✔
1630
    memcpy(pColRef, pCfg->pColRefs, colRefSize);
8,056✔
1631
  }
1632

1633
  pNew->pColRefs = pColRef;
88,578✔
1634

1635
  SColRef *pTagRef = NULL;
88,578✔
1636
  if (hasRefCol(pCfg->tableType) && pCfg->pTagRefs && pCfg->numOfTagRefs > 0) {
88,578✔
1637
    int32_t tagRefSize = pCfg->numOfTagRefs * sizeof(SColRef);
×
1638
    pTagRef = taosMemoryMalloc(tagRefSize);
×
1639
    if (!pTagRef) goto err;
×
1640
    memcpy(pTagRef, pCfg->pTagRefs, tagRefSize);
×
1641
  }
1642
  pNew->pTagRefs = pTagRef;
88,578✔
1643

1644
  return pNew;
88,578✔
1645
err:
×
1646
  if (pNew->pComment) taosMemoryFreeClear(pNew->pComment);
×
1647
  if (pNew->pFuncs) taosArrayDestroy(pNew->pFuncs);
×
1648
  if (pNew->pTags) taosMemoryFreeClear(pNew->pTags);
×
1649
  if (pNew->pSchemas) taosMemoryFreeClear(pNew->pSchemas);
×
1650
  if (pNew->pSchemaExt) taosMemoryFreeClear(pNew->pSchemaExt);
×
1651
  if (pNew->pColRefs) taosMemoryFreeClear(pNew->pColRefs);
×
1652
  if (pNew->pTagRefs) taosMemoryFreeClear(pNew->pTagRefs);
×
1653
  taosMemoryFreeClear(pNew);
×
1654
  return NULL;
×
1655
}
1656

1657
int32_t getTableCfgFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableCfg** pOutput) {
92,160✔
1658
  char    fullName[TSDB_TABLE_FNAME_LEN];
91,768✔
1659
  int32_t code = tNameExtractFullName(pName, fullName);
92,160✔
1660
  if (TSDB_CODE_SUCCESS != code) {
92,160✔
1661
    return code;
×
1662
  }
1663
  STableCfg* pCfg = NULL;
92,160✔
1664
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableCfg, (void**)&pCfg);
92,160✔
1665
  if (TSDB_CODE_SUCCESS == code && NULL != pCfg) {
92,160✔
1666
    *pOutput = tableCfgDup(pCfg);
88,578✔
1667
    if (NULL == *pOutput) {
88,578✔
1668
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1669
    }
1670
  }
1671
  return code;
92,160✔
1672
}
1673

1674
int32_t getVStbRefDbsFromCache(SParseMetaCache* pMetaCache, const SName* pName, SArray** pOutput) {
4,079,392✔
1675
  char    fullName[TSDB_TABLE_FNAME_LEN];
4,070,547✔
1676
  int32_t code = tNameExtractFullName(pName, fullName);
4,079,392✔
1677
  if (TSDB_CODE_SUCCESS != code) {
4,079,392✔
1678
    return code;
×
1679
  }
1680

1681
  if (NULL == pMetaCache || NULL == pMetaCache->pVStbRefDbs) {
4,079,392✔
1682
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
1683
  }
1684

1685
  SArray* pRefs = NULL;
4,079,392✔
1686
  code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pVStbRefDbs, (void**)&pRefs);
4,079,392✔
1687

1688
  // Special handling for stmt scenario where slot is reserved but data is not filled
1689
  // In this case, getMetaDataFromHash returns INTERNAL_ERROR because value is nullPointer
1690
  if (TSDB_CODE_PAR_INTERNAL_ERROR == code) {
4,079,392✔
1691
    // Data not filled in cache (stmt scenario without putMetaDataToCache)
1692
    // Return table not exist to indicate VStbRefDbs is not available
1693
    return TSDB_CODE_PAR_TABLE_NOT_EXIST;
×
1694
  }
1695

1696
  if (TSDB_CODE_SUCCESS == code && NULL != pRefs) {
4,079,392✔
1697
    *pOutput = pRefs;
4,079,392✔
1698
  }
1699
  return code;
4,079,392✔
1700
}
1701

1702
int32_t reserveDnodeRequiredInCache(SParseMetaCache* pMetaCache) {
42,518✔
1703
  pMetaCache->dnodeRequired = true;
42,518✔
1704
  return TSDB_CODE_SUCCESS;
42,518✔
1705
}
1706

1707
int32_t getDnodeListFromCache(SParseMetaCache* pMetaCache, SArray** pDnodes) {
39,941✔
1708
  SMetaRes* pRes = taosArrayGet(pMetaCache->pDnodes, 0);
39,941✔
1709
  if (TSDB_CODE_SUCCESS != pRes->code) {
39,941✔
1710
    return pRes->code;
×
1711
  }
1712

1713
  *pDnodes = taosArrayDup((SArray*)pRes->pRes, NULL);
39,941✔
1714
  if (NULL == *pDnodes) {
39,941✔
1715
    return terrno;
×
1716
  }
1717
  return TSDB_CODE_SUCCESS;
39,941✔
1718
}
1719

1720
void destoryParseTablesMetaReqHash(SHashObj* pHash) {
2,147,483,647✔
1721
  SParseTablesMetaReq* p = taosHashIterate(pHash, NULL);
2,147,483,647✔
1722
  while (NULL != p) {
2,147,483,647✔
1723
    taosHashCleanup(p->pTables);
686,069,247✔
1724
    p = taosHashIterate(pHash, p);
686,029,871✔
1725
  }
1726
  taosHashCleanup(pHash);
2,147,483,647✔
1727
}
2,147,483,647✔
1728

1729
void destoryParseMetaCache(SParseMetaCache* pMetaCache, bool request) {
928,750,711✔
1730
  if (request) {
928,750,711✔
1731
    destoryParseTablesMetaReqHash(pMetaCache->pTableMeta);
471,259,585✔
1732
    destoryParseTablesMetaReqHash(pMetaCache->pTableVgroup);
471,247,473✔
1733
    destoryParseTablesMetaReqHash(pMetaCache->pViews);
471,250,343✔
1734
    destoryParseTablesMetaReqHash(pMetaCache->pTSMAs);
471,255,530✔
1735
    destoryParseTablesMetaReqHash(pMetaCache->pTableName);
471,259,774✔
1736
  } else {
1737
    taosHashCleanup(pMetaCache->pTableMeta);
457,491,126✔
1738
    taosHashCleanup(pMetaCache->pTableVgroup);
457,548,754✔
1739
    taosHashCleanup(pMetaCache->pViews);
457,536,830✔
1740
    taosHashCleanup(pMetaCache->pTSMAs);
457,538,370✔
1741
    taosHashCleanup(pMetaCache->pTableName);
457,545,253✔
1742
  }
1743
  taosHashCleanup(pMetaCache->pDbVgroup);
928,802,777✔
1744
  taosHashCleanup(pMetaCache->pDbCfg);
928,774,873✔
1745
  taosHashCleanup(pMetaCache->pDbInfo);
928,775,768✔
1746
  taosHashCleanup(pMetaCache->pUserAuth);
928,795,146✔
1747
  taosHashCleanup(pMetaCache->pUdf);
928,787,885✔
1748
  taosHashCleanup(pMetaCache->pTableIndex);
928,797,885✔
1749
  taosHashCleanup(pMetaCache->pTableCfg);
928,813,149✔
1750
  taosHashCleanup(pMetaCache->pTableTSMAs);
928,809,884✔
1751
  taosHashCleanup(pMetaCache->pVStbRefDbs);
928,818,351✔
1752
}
928,803,057✔
1753

1754
int64_t int64SafeSub(int64_t a, int64_t b) {
4,153,805✔
1755
  int64_t res = (uint64_t)a - (uint64_t)b;
4,153,805✔
1756

1757
  if (a >= 0 && b < 0) {
4,153,805✔
1758
    if ((uint64_t)res > (uint64_t)INT64_MAX) {
×
1759
      // overflow
1760
      res = INT64_MAX;
×
1761
    }
1762
  } else if (a < 0 && b > 0 && res >= 0) {
4,153,805✔
1763
    // underflow
1764
    res = INT64_MIN;
2,247✔
1765
  }
1766
  return res;
4,153,805✔
1767
}
1768

1769
STypeMod calcTypeMod(const SDataType* pType) {
820,006,410✔
1770
  if (IS_DECIMAL_TYPE(pType->type)) {
820,006,410✔
1771
    return decimalCalcTypeMod(pType->precision, pType->scale);
1,590,087✔
1772
  }
1773
  return 0;
818,416,874✔
1774
}
1775

1776

1777
int32_t validateExprSubQuery(SNode* pNode) {
312,718,083✔
1778
  int32_t code = TSDB_CODE_SUCCESS;
312,718,083✔
1779
  
1780
  switch (nodeType(pNode)) {
312,718,083✔
1781
    case QUERY_NODE_SELECT_STMT: {
287,303,876✔
1782
      SSelectStmt* pSelect = (SSelectStmt*)pNode;
287,303,876✔
1783
      if (pSelect->pProjectionList && pSelect->pProjectionList->length != 1) {
287,303,876✔
1784
        return TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_RES_COLS;
51,106,776✔
1785
      }
1786
      break;
236,197,100✔
1787
    }
1788
    case QUERY_NODE_SET_OPERATOR: {
25,445,956✔
1789
      SSetOperator* pSet = (SSetOperator*)pNode;
25,445,956✔
1790
      if (pSet->pProjectionList && pSet->pProjectionList->length != 1) {
25,445,956✔
1791
        return TSDB_CODE_PAR_INVALID_SCALAR_SUBQ_RES_COLS;
92,064✔
1792
      }
1793
      break;
25,353,892✔
1794
    }
1795
    default:
×
1796
      code = TSDB_CODE_PAR_INVALID_EXPR_SUBQ;
×
1797
      break;
×
1798
  }
1799

1800
  return code;
261,519,243✔
1801
}
1802

1803
void getExprSubQueryResType(SNode* pNode, SDataType* pType) {
233,617,672✔
1804
  switch (nodeType(pNode)) {
233,617,672✔
1805
    case QUERY_NODE_SELECT_STMT: {
208,279,219✔
1806
      SSelectStmt* pSelect = (SSelectStmt*)pNode;
208,279,219✔
1807
      SExprNode*   pExpr = (SExprNode*)nodesListGetNode(pSelect->pProjectionList, 0);
208,279,219✔
1808
      memcpy(pType, &pExpr->resType, sizeof(*pType));
208,290,515✔
1809
      break;
208,290,515✔
1810
    }
1811
    case QUERY_NODE_SET_OPERATOR: {
25,354,605✔
1812
      SSetOperator* pSet = (SSetOperator*)pNode;
25,354,605✔
1813
      SExprNode*    pExpr = (SExprNode*)nodesListGetNode(pSet->pProjectionList, 0);
25,354,605✔
1814
      memcpy(pType, &pExpr->resType, sizeof(*pType));
25,355,682✔
1815
      break;
25,355,682✔
1816
    }
1817
    default:
×
1818
      break;
×
1819
  }
1820
}
233,630,045✔
1821

1822
void getExprSubQueryResCols(SNode* pNode, int32_t* cols) {
132,841✔
1823
  switch (nodeType(pNode)) {
132,841✔
1824
    case QUERY_NODE_SELECT_STMT: {
132,436✔
1825
      SSelectStmt* pSelect = (SSelectStmt*)pNode;
132,436✔
1826
      *cols = LIST_LENGTH(pSelect->pProjectionList);
132,436✔
1827
      break;
132,436✔
1828
    }
1829
    case QUERY_NODE_SET_OPERATOR: {
405✔
1830
      SSetOperator* pSet = (SSetOperator*)pNode;
405✔
1831
      *cols = LIST_LENGTH(pSet->pProjectionList);
405✔
1832
      break;
405✔
1833
    }
NEW
1834
    default:
×
NEW
1835
      *cols = 0;
×
NEW
1836
      break;
×
1837
  }
1838
}
132,841✔
1839

1840
int32_t updateExprSubQueryType(SNode* pNode, ESubQueryType* type) {
311,426,905✔
1841
  int32_t code = TSDB_CODE_SUCCESS;
311,426,905✔
1842
  
1843
  switch (nodeType(pNode)) {
311,426,905✔
1844
    case QUERY_NODE_SELECT_STMT: {
285,550,783✔
1845
      SSelectStmt* pSelect = (SSelectStmt*)pNode;
285,550,783✔
1846
      if (E_SUB_QUERY_NOT_SET == pSelect->subQType) {
285,550,783✔
1847
        pSelect->subQType = E_SUB_QUERY_SCALAR;
217,657,170✔
1848
      }
1849
      *type = pSelect->subQType;
285,550,783✔
1850
      parserDebug("update select subQType:%d, pSelect:%p", *type, pSelect);
285,550,783✔
1851
      break;
285,572,610✔
1852
    }
1853
    case QUERY_NODE_SET_OPERATOR: {
25,900,029✔
1854
      SSetOperator* pSet = (SSetOperator*)pNode;
25,900,029✔
1855
      if (E_SUB_QUERY_NOT_SET == pSet->subQType) {
25,900,029✔
1856
        pSet->subQType = E_SUB_QUERY_SCALAR;
24,955,142✔
1857
      }
1858
      *type = pSet->subQType;
25,900,029✔
1859
      parserDebug("update set subQType:%d, pSet:%p", *type, pSet);
25,900,029✔
1860
      break;
25,899,973✔
1861
    }
1862
    default:
×
1863
      code = TSDB_CODE_PAR_INVALID_EXPR_SUBQ;
×
1864
      break;
×
1865
  }
1866

1867
  return code;
311,448,676✔
1868
}
1869

1870

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