parUtil.c 34.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
15

X
Xiaoyu Wang 已提交
16
#include "parUtil.h"
17
#include "cJSON.h"
18
#include "querynodes.h"
19

20
#define USER_AUTH_KEY_MAX_LEN TSDB_USER_LEN + TSDB_DB_FNAME_LEN + 2
21

22 23
const void* nullPointer = NULL;

24 25 26 27 28 29 30
static char* getSyntaxErrFormat(int32_t errCode) {
  switch (errCode) {
    case TSDB_CODE_PAR_SYNTAX_ERROR:
      return "syntax error near \"%s\"";
    case TSDB_CODE_PAR_INCOMPLETE_SQL:
      return "Incomplete SQL statement";
    case TSDB_CODE_PAR_INVALID_COLUMN:
X
Xiaoyu Wang 已提交
31
      return "Invalid column name: %s";
32
    case TSDB_CODE_PAR_TABLE_NOT_EXIST:
X
Xiaoyu Wang 已提交
33
      return "Table does not exist: %s";
34
    case TSDB_CODE_PAR_AMBIGUOUS_COLUMN:
X
Xiaoyu Wang 已提交
35
      return "Column ambiguously defined: %s";
36
    case TSDB_CODE_PAR_WRONG_VALUE_TYPE:
X
Xiaoyu Wang 已提交
37
      return "Invalid value type: %s";
38 39 40 41 42 43 44 45 46 47 48
    case TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION:
      return "There mustn't be aggregation";
    case TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT:
      return "ORDER BY item must be the number of a SELECT-list expression";
    case TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION:
      return "Not a GROUP BY expression";
    case TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION:
      return "Not SELECTed expression";
    case TSDB_CODE_PAR_NOT_SINGLE_GROUP:
      return "Not a single-group group function";
    case TSDB_CODE_PAR_TAGS_NOT_MATCHED:
49
      return "Tags number not matched";
50
    case TSDB_CODE_PAR_INVALID_TAG_NAME:
X
Xiaoyu Wang 已提交
51
      return "Invalid tag name: %s";
52
    case TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG:
53
      return "Name or password too long";
54
    case TSDB_CODE_PAR_PASSWD_EMPTY:
55
      return "Password can not be empty";
56
    case TSDB_CODE_PAR_INVALID_PORT:
57
      return "Port should be an integer that is less than 65535 and greater than 0";
58
    case TSDB_CODE_PAR_INVALID_ENDPOINT:
59 60 61
      return "Endpoint should be in the format of 'fqdn:port'";
    case TSDB_CODE_PAR_EXPRIE_STATEMENT:
      return "This statement is no longer supported";
X
Xiaoyu Wang 已提交
62 63
    case TSDB_CODE_PAR_INTER_VALUE_TOO_SMALL:
      return "Interval cannot be less than %d us";
X
Xiaoyu Wang 已提交
64
    case TSDB_CODE_PAR_DB_NOT_SPECIFIED:
65
      return "Database not specified";
X
Xiaoyu Wang 已提交
66
    case TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME:
X
Xiaoyu Wang 已提交
67
      return "Invalid identifier name: %s";
X
Xiaoyu Wang 已提交
68
    case TSDB_CODE_PAR_CORRESPONDING_STABLE_ERR:
69
      return "Corresponding super table not in this db";
X
Xiaoyu Wang 已提交
70
    case TSDB_CODE_PAR_INVALID_RANGE_OPTION:
X
Xiaoyu Wang 已提交
71
      return "Invalid option %s: %" PRId64 " valid range: [%d, %d]";
X
Xiaoyu Wang 已提交
72
    case TSDB_CODE_PAR_INVALID_STR_OPTION:
73
      return "Invalid option %s: %s";
X
Xiaoyu Wang 已提交
74
    case TSDB_CODE_PAR_INVALID_ENUM_OPTION:
X
Xiaoyu Wang 已提交
75
      return "Invalid option %s: %" PRId64 ", only %d, %d allowed";
X
Xiaoyu Wang 已提交
76
    case TSDB_CODE_PAR_INVALID_KEEP_NUM:
77
      return "Invalid number of keep options";
X
Xiaoyu Wang 已提交
78
    case TSDB_CODE_PAR_INVALID_KEEP_ORDER:
79
      return "Invalid keep value, should be keep0 <= keep1 <= keep2";
X
Xiaoyu Wang 已提交
80
    case TSDB_CODE_PAR_INVALID_KEEP_VALUE:
X
Xiaoyu Wang 已提交
81
      return "Invalid option keep: %" PRId64 ", %" PRId64 ", %" PRId64 " valid range: [%dm, %dm]";
X
Xiaoyu Wang 已提交
82
    case TSDB_CODE_PAR_INVALID_COMMENT_OPTION:
83
      return "Invalid option comment, length cannot exceed %d";
X
Xiaoyu Wang 已提交
84
    case TSDB_CODE_PAR_INVALID_F_RANGE_OPTION:
85
      return "Invalid option %s: %f valid range: [%d, %d]";
X
Xiaoyu Wang 已提交
86
    case TSDB_CODE_PAR_INVALID_ROLLUP_OPTION:
87
      return "Invalid option rollup: only one function is allowed";
X
Xiaoyu Wang 已提交
88
    case TSDB_CODE_PAR_INVALID_RETENTIONS_OPTION:
89
      return "Invalid option retentions";
X
Xiaoyu Wang 已提交
90 91
    case TSDB_CODE_PAR_GROUPBY_WINDOW_COEXIST:
      return "GROUP BY and WINDOW-clause can't be used together";
92
    case TSDB_CODE_PAR_INVALID_OPTION_UNIT:
X
Xiaoyu Wang 已提交
93
      return "Invalid option %s unit: %c, only %c, %c, %c allowed";
94
    case TSDB_CODE_PAR_INVALID_KEEP_UNIT:
X
Xiaoyu Wang 已提交
95
      return "Invalid option keep unit: %c, only m, h, d allowed";
X
Xiaoyu Wang 已提交
96 97
    case TSDB_CODE_PAR_AGG_FUNC_NESTING:
      return "Aggregate functions do not support nesting";
X
Xiaoyu Wang 已提交
98
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE:
99
      return "Only support STATE_WINDOW on integer/bool/varchar column";
X
Xiaoyu Wang 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    case TSDB_CODE_PAR_INVALID_STATE_WIN_COL:
      return "Not support STATE_WINDOW on tag column";
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TABLE:
      return "STATE_WINDOW not support for super table query";
    case TSDB_CODE_PAR_INTER_SESSION_GAP:
      return "SESSION gap should be fixed time window, and greater than 0";
    case TSDB_CODE_PAR_INTER_SESSION_COL:
      return "Only support SESSION on primary timestamp column";
    case TSDB_CODE_PAR_INTER_OFFSET_NEGATIVE:
      return "Interval offset cannot be negative";
    case TSDB_CODE_PAR_INTER_OFFSET_UNIT:
      return "Cannot use 'year' as offset when interval is 'month'";
    case TSDB_CODE_PAR_INTER_OFFSET_TOO_BIG:
      return "Interval offset should be shorter than interval";
    case TSDB_CODE_PAR_INTER_SLIDING_UNIT:
      return "Does not support sliding when interval is natural month/year";
    case TSDB_CODE_PAR_INTER_SLIDING_TOO_BIG:
      return "sliding value no larger than the interval value";
    case TSDB_CODE_PAR_INTER_SLIDING_TOO_SMALL:
      return "sliding value can not less than 1% of interval value";
120 121
    case TSDB_CODE_PAR_ONLY_ONE_JSON_TAG:
      return "Only one tag if there is a json tag";
X
Xiaoyu Wang 已提交
122 123
    case TSDB_CODE_PAR_INCORRECT_NUM_OF_COL:
      return "Query block has incorrect number of result columns";
X
Xiaoyu Wang 已提交
124 125 126 127 128 129 130 131
    case TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL:
      return "Incorrect TIMESTAMP value: %s";
    case TSDB_CODE_PAR_INVALID_DAYS_VALUE:
      return "Invalid days value, should be keep2 >= keep1 >= keep0 >= days";
    case TSDB_CODE_PAR_OFFSET_LESS_ZERO:
      return "soffset/offset can not be less than 0";
    case TSDB_CODE_PAR_SLIMIT_LEAK_PARTITION_BY:
      return "slimit/soffset only available for PARTITION BY query";
X
Xiaoyu Wang 已提交
132 133
    case TSDB_CODE_PAR_INVALID_TOPIC_QUERY:
      return "Invalid topic query";
X
Xiaoyu Wang 已提交
134 135
    case TSDB_CODE_PAR_INVALID_DROP_STABLE:
      return "Cannot drop super table in batch";
X
Xiaoyu Wang 已提交
136
    case TSDB_CODE_PAR_INVALID_FILL_TIME_RANGE:
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
      return "Start(end) time of query range required or time range too large";
    case TSDB_CODE_PAR_DUPLICATED_COLUMN:
      return "Duplicated column names";
    case TSDB_CODE_PAR_INVALID_TAGS_LENGTH:
      return "Tags length exceeds max length %d";
    case TSDB_CODE_PAR_INVALID_ROW_LENGTH:
      return "Row length exceeds max length %d";
    case TSDB_CODE_PAR_INVALID_COLUMNS_NUM:
      return "Illegal number of columns";
    case TSDB_CODE_PAR_TOO_MANY_COLUMNS:
      return "Too many columns";
    case TSDB_CODE_PAR_INVALID_FIRST_COLUMN:
      return "First column must be timestamp";
    case TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN:
      return "Invalid binary/nchar column length";
    case TSDB_CODE_PAR_INVALID_TAGS_NUM:
      return "Invalid number of tag columns";
X
Xiaoyu Wang 已提交
154 155
    case TSDB_CODE_PAR_INVALID_INTERNAL_PK:
      return "Invalid _c0 or _rowts expression";
156 157 158 159
    case TSDB_CODE_PAR_INVALID_TIMELINE_FUNC:
      return "Invalid timeline function";
    case TSDB_CODE_PAR_INVALID_PASSWD:
      return "Invalid password";
X
Xiaoyu Wang 已提交
160 161
    case TSDB_CODE_PAR_INVALID_ALTER_TABLE:
      return "Invalid alter table statement";
X
Xiaoyu Wang 已提交
162 163 164 165
    case TSDB_CODE_PAR_CANNOT_DROP_PRIMARY_KEY:
      return "Primary timestamp column cannot be dropped";
    case TSDB_CODE_PAR_INVALID_MODIFY_COL:
      return "Only binary/nchar column length could be modified";
166 167
    case TSDB_CODE_PAR_INVALID_TBNAME:
      return "Invalid tbname pseudo column";
168 169 170 171
    case TSDB_CODE_PAR_INVALID_FUNCTION_NAME:
      return "Invalid function name";
    case TSDB_CODE_PAR_COMMENT_TOO_LONG:
      return "Comment too long";
172
    case TSDB_CODE_PAR_NOT_ALLOWED_FUNC:
173
      return "Some functions are allowed only in the SELECT list of a query. "
174
             "And, cannot be mixed with other non scalar functions or columns.";
175 176
    case TSDB_CODE_PAR_NOT_ALLOWED_WIN_QUERY:
      return "Window query not supported, since the result of subquery not include valid timestamp column";
177 178
    case TSDB_CODE_PAR_INVALID_DROP_COL:
      return "No columns can be dropped";
wmmhello's avatar
wmmhello 已提交
179 180
    case TSDB_CODE_PAR_INVALID_COL_JSON:
      return "Only tag can be json type";
D
dapan1121 已提交
181 182
    case TSDB_CODE_PAR_VALUE_TOO_LONG:
      return "Value too long for column/tag: %s";
X
Xiaoyu Wang 已提交
183 184
    case TSDB_CODE_PAR_INVALID_DELETE_WHERE:
      return "The DELETE statement must have a definite time window range";
X
Xiaoyu Wang 已提交
185 186
    case TSDB_CODE_PAR_INVALID_REDISTRIBUTE_VG:
      return "The REDISTRIBUTE VGROUP statement only support 1 to 3 dnodes";
X
Xiaoyu Wang 已提交
187
    case TSDB_CODE_PAR_FILL_NOT_ALLOWED_FUNC:
188
      return "%s function does not supportted in fill query";
X
Xiaoyu Wang 已提交
189
    case TSDB_CODE_PAR_INVALID_WINDOW_PC:
190 191 192 193 194 195 196
      return "_WSTARTTS, _WENDTS and _WDURATION can only be used in window query";
    case TSDB_CODE_PAR_WINDOW_NOT_ALLOWED_FUNC:
      return "%s function does not supportted in time window query";
    case TSDB_CODE_PAR_STREAM_NOT_ALLOWED_FUNC:
      return "%s function does not supportted in stream query";
    case TSDB_CODE_PAR_GROUP_BY_NOT_ALLOWED_FUNC:
      return "%s function does not supportted in group query";
X
Xiaoyu Wang 已提交
197 198
    case TSDB_CODE_PAR_INVALID_TABLE_OPTION:
      return "Invalid option %s";
X
Xiaoyu Wang 已提交
199 200
    case TSDB_CODE_PAR_INVALID_INTERP_CLAUSE:
      return "Invalid usage of RANGE clause, EVERY clause or FILL clause";
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    case TSDB_CODE_OUT_OF_MEMORY:
      return "Out of memory";
    default:
      return "Unknown error";
  }
}

int32_t generateSyntaxErrMsg(SMsgBuf* pBuf, int32_t errCode, ...) {
  va_list vArgList;
  va_start(vArgList, errCode);
  vsnprintf(pBuf->buf, pBuf->len, getSyntaxErrFormat(errCode), vArgList);
  va_end(vArgList);
  return errCode;
}

X
Xiaoyu Wang 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
int32_t buildInvalidOperationMsg(SMsgBuf* pBuf, const char* msg) {
  strncpy(pBuf->buf, msg, pBuf->len);
  return TSDB_CODE_TSC_INVALID_OPERATION;
}

int32_t buildSyntaxErrMsg(SMsgBuf* pBuf, const char* additionalInfo, const char* sourceStr) {
  const char* msgFormat1 = "syntax error near \'%s\'";
  const char* msgFormat2 = "syntax error near \'%s\' (%s)";
  const char* msgFormat3 = "%s";

  const char* prefix = "syntax error";
  if (sourceStr == NULL) {
    assert(additionalInfo != NULL);
    snprintf(pBuf->buf, pBuf->len, msgFormat1, additionalInfo);
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
  }
232

X
Xiaoyu Wang 已提交
233 234 235 236 237 238 239 240 241 242 243 244
  char buf[64] = {0};  // only extract part of sql string
  strncpy(buf, sourceStr, tListLen(buf) - 1);

  if (additionalInfo != NULL) {
    snprintf(pBuf->buf, pBuf->len, msgFormat2, buf, additionalInfo);
  } else {
    const char* msgFormat = (0 == strncmp(sourceStr, prefix, strlen(prefix))) ? msgFormat3 : msgFormat1;
    snprintf(pBuf->buf, pBuf->len, msgFormat, buf);
  }

  return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
}
245

X
Xiaoyu Wang 已提交
246
SSchema* getTableColumnSchema(const STableMeta* pTableMeta) {
X
Xiaoyu Wang 已提交
247
  assert(pTableMeta != NULL);
X
Xiaoyu Wang 已提交
248
  return (SSchema*)pTableMeta->schema;
249 250
}

X
Xiaoyu Wang 已提交
251
static SSchema* getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex) {
X
Xiaoyu Wang 已提交
252 253
  assert(pTableMeta != NULL && pTableMeta->schema != NULL && colIndex >= 0 &&
         colIndex < (getNumOfColumns(pTableMeta) + getNumOfTags(pTableMeta)));
254

X
Xiaoyu Wang 已提交
255
  SSchema* pSchema = (SSchema*)pTableMeta->schema;
X
Xiaoyu Wang 已提交
256
  return &pSchema[colIndex];
257
}
258

X
Xiaoyu Wang 已提交
259
SSchema* getTableTagSchema(const STableMeta* pTableMeta) {
X
Xiaoyu Wang 已提交
260 261
  assert(pTableMeta != NULL &&
         (pTableMeta->tableType == TSDB_SUPER_TABLE || pTableMeta->tableType == TSDB_CHILD_TABLE));
X
Xiaoyu Wang 已提交
262
  return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns);
263 264
}

X
Xiaoyu Wang 已提交
265 266 267 268
int32_t getNumOfColumns(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  // table created according to super table, use data from super table
  return getTableInfo(pTableMeta).numOfColumns;
269 270
}

X
Xiaoyu Wang 已提交
271 272 273
int32_t getNumOfTags(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  return getTableInfo(pTableMeta).numOfTags;
274 275
}

X
Xiaoyu Wang 已提交
276
STableComInfo getTableInfo(const STableMeta* pTableMeta) {
277
  assert(pTableMeta != NULL);
X
Xiaoyu Wang 已提交
278
  return pTableMeta->tableInfo;
H
Haojun Liao 已提交
279
}
280

X
Xiaoyu Wang 已提交
281
STableMeta* tableMetaDup(const STableMeta* pTableMeta) {
282
  size_t size = TABLE_META_SIZE(pTableMeta);
X
Xiaoyu Wang 已提交
283 284 285 286 287 288

  STableMeta* p = taosMemoryMalloc(size);
  memcpy(p, pTableMeta, size);
  return p;
}

289
int32_t trimString(const char* src, int32_t len, char* dst, int32_t dlen) {
X
Xiaoyu Wang 已提交
290
  if (len <= 0 || dlen <= 0) return 0;
291

X
Xiaoyu Wang 已提交
292
  char    delim = src[0];
293 294 295
  int32_t j = 0;
  for (uint32_t k = 1; k < len - 1; ++k) {
    if (j >= dlen) {
296 297
      dst[j - 1] = '\0';
      return j;
298
    }
X
Xiaoyu Wang 已提交
299
    if (src[k] == delim && src[k + 1] == delim) {  // deal with "", ''
300 301 302 303 304
      dst[j] = src[k + 1];
      j++;
      k++;
      continue;
    }
305

X
Xiaoyu Wang 已提交
306 307
    if (src[k] == '\\') {  // deal with escape character
      if (src[k + 1] == 'n') {
308
        dst[j] = '\n';
X
Xiaoyu Wang 已提交
309
      } else if (src[k + 1] == 'r') {
310
        dst[j] = '\r';
X
Xiaoyu Wang 已提交
311
      } else if (src[k + 1] == 't') {
312
        dst[j] = '\t';
X
Xiaoyu Wang 已提交
313
      } else if (src[k + 1] == '\\') {
314
        dst[j] = '\\';
X
Xiaoyu Wang 已提交
315
      } else if (src[k + 1] == '\'') {
316
        dst[j] = '\'';
X
Xiaoyu Wang 已提交
317
      } else if (src[k + 1] == '"') {
318
        dst[j] = '"';
X
Xiaoyu Wang 已提交
319
      } else if (src[k + 1] == '%' || src[k + 1] == '_') {
320
        dst[j++] = src[k];
X
Xiaoyu Wang 已提交
321 322 323
        dst[j] = src[k + 1];
      } else {
        dst[j] = src[k + 1];
324 325 326 327 328 329
      }
      j++;
      k++;
      continue;
    }

330 331 332 333 334 335
    dst[j] = src[k];
    j++;
  }
  dst[j] = '\0';
  return j;
}
336

X
Xiaoyu Wang 已提交
337
static bool isValidateTag(char* input) {
338 339 340 341 342 343 344
  if (!input) return false;
  for (size_t i = 0; i < strlen(input); ++i) {
    if (isprint(input[i]) == 0) return false;
  }
  return true;
}

X
Xiaoyu Wang 已提交
345
int32_t parseJsontoTagData(const char* json, SArray* pTagVals, STag** ppTag, SMsgBuf* pMsgBuf) {
wmmhello's avatar
wmmhello 已提交
346
  int32_t   retCode = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
347
  cJSON*    root = NULL;
wmmhello's avatar
wmmhello 已提交
348
  SHashObj* keyHash = NULL;
X
Xiaoyu Wang 已提交
349
  int32_t   size = 0;
350
  // set json NULL data
351
  if (!json || strtrim((char*)json) == 0 || strcasecmp(json, TSDB_DATA_NULL_STR_L) == 0) {
wmmhello's avatar
wmmhello 已提交
352 353
    retCode = TSDB_CODE_SUCCESS;
    goto end;
354 355 356
  }

  // set json real data
wmmhello's avatar
wmmhello 已提交
357
  root = cJSON_Parse(json);
X
Xiaoyu Wang 已提交
358
  if (root == NULL) {
wmmhello's avatar
wmmhello 已提交
359 360
    retCode = buildSyntaxErrMsg(pMsgBuf, "json parse error", json);
    goto end;
361 362
  }

wmmhello's avatar
wmmhello 已提交
363
  size = cJSON_GetArraySize(root);
X
Xiaoyu Wang 已提交
364
  if (!cJSON_IsObject(root)) {
wmmhello's avatar
wmmhello 已提交
365 366
    retCode = buildSyntaxErrMsg(pMsgBuf, "json error invalide value", json);
    goto end;
367 368
  }

wmmhello's avatar
wmmhello 已提交
369
  keyHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false);
370
  for (int32_t i = 0; i < size; i++) {
371 372
    cJSON* item = cJSON_GetArrayItem(root, i);
    if (!item) {
wmmhello's avatar
wmmhello 已提交
373
      uError("json inner error:%d", i);
374
      retCode = buildSyntaxErrMsg(pMsgBuf, "json inner error", json);
375 376 377
      goto end;
    }

X
Xiaoyu Wang 已提交
378 379
    char* jsonKey = item->string;
    if (!isValidateTag(jsonKey)) {
380
      retCode = buildSyntaxErrMsg(pMsgBuf, "json key not validate", jsonKey);
381 382 383
      goto end;
    }
    size_t keyLen = strlen(jsonKey);
384
    if (keyLen > TSDB_MAX_JSON_KEY_LEN) {
wmmhello's avatar
wmmhello 已提交
385
      uError("json key too long error");
386
      retCode = buildSyntaxErrMsg(pMsgBuf, "json key too long, more than 256", jsonKey);
wmmhello's avatar
wmmhello 已提交
387 388
      goto end;
    }
X
Xiaoyu Wang 已提交
389
    if (keyLen == 0 || taosHashGet(keyHash, jsonKey, keyLen) != NULL) {
390 391
      continue;
    }
wmmhello's avatar
wmmhello 已提交
392 393
    STagVal val = {0};
    val.pKey = jsonKey;
X
Xiaoyu Wang 已提交
394 395
    taosHashPut(keyHash, jsonKey, keyLen, &keyLen,
                CHAR_BYTES);  // add key to hash to remove dumplicate, value is useless
396

X
Xiaoyu Wang 已提交
397 398
    if (item->type == cJSON_String) {  // add json value  format: type|data
      char*   jsonValue = item->valuestring;
399
      int32_t valLen = (int32_t)strlen(jsonValue);
wmmhello's avatar
wmmhello 已提交
400
      char*   tmp = taosMemoryCalloc(1, valLen * TSDB_NCHAR_SIZE);
X
Xiaoyu Wang 已提交
401
      if (!tmp) {
402 403
        retCode = TSDB_CODE_TSC_OUT_OF_MEMORY;
        goto end;
404
      }
wmmhello's avatar
wmmhello 已提交
405
      val.type = TSDB_DATA_TYPE_NCHAR;
X
Xiaoyu Wang 已提交
406
      if (valLen > 0 && !taosMbsToUcs4(jsonValue, valLen, (TdUcs4*)tmp, (int32_t)(valLen * TSDB_NCHAR_SIZE), &valLen)) {
wmmhello's avatar
wmmhello 已提交
407
        uError("charset:%s to %s. val:%s, errno:%s, convert failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, jsonValue,
X
Xiaoyu Wang 已提交
408
               strerror(errno));
409 410 411
        retCode = buildSyntaxErrMsg(pMsgBuf, "charset convert json error", jsonValue);
        goto end;
      }
wmmhello's avatar
wmmhello 已提交
412 413
      val.nData = valLen;
      val.pData = tmp;
X
Xiaoyu Wang 已提交
414 415
    } else if (item->type == cJSON_Number) {
      if (!isfinite(item->valuedouble)) {
wmmhello's avatar
wmmhello 已提交
416
        uError("json value is invalidate");
X
Xiaoyu Wang 已提交
417
        retCode = buildSyntaxErrMsg(pMsgBuf, "json value number is illegal", json);
418 419
        goto end;
      }
wmmhello's avatar
wmmhello 已提交
420 421
      val.type = TSDB_DATA_TYPE_DOUBLE;
      *((double*)&(val.i64)) = item->valuedouble;
X
Xiaoyu Wang 已提交
422
    } else if (item->type == cJSON_True || item->type == cJSON_False) {
wmmhello's avatar
wmmhello 已提交
423 424
      val.type = TSDB_DATA_TYPE_BOOL;
      *((char*)&(val.i64)) = (char)(item->valueint);
X
Xiaoyu Wang 已提交
425
    } else if (item->type == cJSON_NULL) {
wmmhello's avatar
wmmhello 已提交
426
      val.type = TSDB_DATA_TYPE_NULL;
X
Xiaoyu Wang 已提交
427
    } else {
428 429 430
      retCode = buildSyntaxErrMsg(pMsgBuf, "invalidate json value", json);
      goto end;
    }
wmmhello's avatar
wmmhello 已提交
431
    taosArrayPush(pTagVals, &val);
432 433 434 435
  }

end:
  taosHashCleanup(keyHash);
X
Xiaoyu Wang 已提交
436
  if (retCode == TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
437 438
    tTagNew(pTagVals, 1, true, ppTag);
  }
439 440
  cJSON_Delete(root);
  return retCode;
441 442
}

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
static int32_t userAuthToString(int32_t acctId, const char* pUser, const char* pDb, AUTH_TYPE type, char* pStr) {
  return sprintf(pStr, "%s*%d.%s*%d", pUser, acctId, pDb, type);
}

static int32_t userAuthToStringExt(const char* pUser, const char* pDbFName, AUTH_TYPE type, char* pStr) {
  return sprintf(pStr, "%s*%s*%d", pUser, pDbFName, type);
}

static void stringToUserAuth(const char* pStr, int32_t len, SUserAuthInfo* pUserAuth) {
  char* p1 = strchr(pStr, '*');
  strncpy(pUserAuth->user, pStr, p1 - pStr);
  ++p1;
  char* p2 = strchr(p1, '*');
  strncpy(pUserAuth->dbFName, p1, p2 - p1);
  ++p2;
  char buf[10] = {0};
  strncpy(buf, p2, len - (p2 - pStr));
  pUserAuth->type = taosStr2Int32(buf, NULL, 10);
}

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
static int32_t buildTableReq(SHashObj* pTablesHash, SArray** pTables) {
  if (NULL != pTablesHash) {
    *pTables = taosArrayInit(taosHashGetSize(pTablesHash), sizeof(SName));
    if (NULL == *pTables) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    void* p = taosHashIterate(pTablesHash, NULL);
    while (NULL != p) {
      size_t len = 0;
      char*  pKey = taosHashGetKey(p, &len);
      char   fullName[TSDB_TABLE_FNAME_LEN] = {0};
      strncpy(fullName, pKey, len);
      SName name = {0};
      tNameFromString(&name, fullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
      taosArrayPush(*pTables, &name);
      p = taosHashIterate(pTablesHash, p);
    }
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t buildDbReq(SHashObj* pDbsHash, SArray** pDbs) {
  if (NULL != pDbsHash) {
    *pDbs = taosArrayInit(taosHashGetSize(pDbsHash), TSDB_DB_FNAME_LEN);
    if (NULL == *pDbs) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    void* p = taosHashIterate(pDbsHash, NULL);
    while (NULL != p) {
      size_t len = 0;
      char*  pKey = taosHashGetKey(p, &len);
      char   fullName[TSDB_DB_FNAME_LEN] = {0};
      strncpy(fullName, pKey, len);
      taosArrayPush(*pDbs, fullName);
      p = taosHashIterate(pDbsHash, p);
    }
  }
  return TSDB_CODE_SUCCESS;
}

503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
static int32_t buildUserAuthReq(SHashObj* pUserAuthHash, SArray** pUserAuth) {
  if (NULL != pUserAuthHash) {
    *pUserAuth = taosArrayInit(taosHashGetSize(pUserAuthHash), sizeof(SUserAuthInfo));
    if (NULL == *pUserAuth) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    void* p = taosHashIterate(pUserAuthHash, NULL);
    while (NULL != p) {
      size_t        len = 0;
      char*         pKey = taosHashGetKey(p, &len);
      SUserAuthInfo userAuth = {0};
      stringToUserAuth(pKey, len, &userAuth);
      taosArrayPush(*pUserAuth, &userAuth);
      p = taosHashIterate(pUserAuthHash, p);
    }
  }
  return TSDB_CODE_SUCCESS;
}

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
static int32_t buildUdfReq(SHashObj* pUdfHash, SArray** pUdf) {
  if (NULL != pUdfHash) {
    *pUdf = taosArrayInit(taosHashGetSize(pUdfHash), TSDB_FUNC_NAME_LEN);
    if (NULL == *pUdf) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    void* p = taosHashIterate(pUdfHash, NULL);
    while (NULL != p) {
      size_t len = 0;
      char*  pFunc = taosHashGetKey(p, &len);
      char   func[TSDB_FUNC_NAME_LEN] = {0};
      strncpy(func, pFunc, len);
      taosArrayPush(*pUdf, func);
      p = taosHashIterate(pUdfHash, p);
    }
  }
  return TSDB_CODE_SUCCESS;
}

541
int32_t buildCatalogReq(const SParseMetaCache* pMetaCache, SCatalogReq* pCatalogReq) {
X
Xiaoyu Wang 已提交
542 543 544 545
  int32_t code = buildTableReq(pMetaCache->pTableMeta, &pCatalogReq->pTableMeta);
  if (TSDB_CODE_SUCCESS == code) {
    code = buildDbReq(pMetaCache->pDbVgroup, &pCatalogReq->pDbVgroup);
  }
546
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
547
    code = buildTableReq(pMetaCache->pTableVgroup, &pCatalogReq->pTableHash);
548 549
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
550
    code = buildDbReq(pMetaCache->pDbCfg, &pCatalogReq->pDbCfg);
551 552
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
553
    code = buildDbReq(pMetaCache->pDbInfo, &pCatalogReq->pDbInfo);
554
  }
555 556 557
  if (TSDB_CODE_SUCCESS == code) {
    code = buildUserAuthReq(pMetaCache->pUserAuth, &pCatalogReq->pUser);
  }
558 559
  if (TSDB_CODE_SUCCESS == code) {
    code = buildUdfReq(pMetaCache->pUdf, &pCatalogReq->pUdf);
560
  }
X
Xiaoyu Wang 已提交
561 562 563
  if (TSDB_CODE_SUCCESS == code) {
    code = buildTableReq(pMetaCache->pTableIndex, &pCatalogReq->pTableIndex);
  }
D
dapan1121 已提交
564 565 566
  if (TSDB_CODE_SUCCESS == code) {
    code = buildTableReq(pMetaCache->pTableCfg, &pCatalogReq->pTableCfg);
  }
567 568 569
  return code;
}

570 571 572 573 574 575 576
static int32_t putMetaDataToHash(const char* pKey, int32_t len, const SArray* pData, int32_t index, SHashObj** pHash) {
  if (NULL == *pHash) {
    *pHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
    if (NULL == *pHash) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
X
Xiaoyu Wang 已提交
577
  SMetaRes* pRes = taosArrayGet(pData, index);
578
  return taosHashPut(*pHash, pKey, len, &pRes, POINTER_BYTES);
579 580
}

X
Xiaoyu Wang 已提交
581 582 583 584
static int32_t getMetaDataFromHash(const char* pKey, int32_t len, SHashObj* pHash, void** pOutput) {
  SMetaRes** pRes = taosHashGet(pHash, pKey, len);
  if (NULL == pRes || NULL == *pRes) {
    return TSDB_CODE_PAR_INTERNAL_ERROR;
585
  }
X
Xiaoyu Wang 已提交
586 587 588 589
  if (TSDB_CODE_SUCCESS == (*pRes)->code) {
    *pOutput = (*pRes)->pRes;
  }
  return (*pRes)->code;
590 591
}

592
static int32_t putTableDataToCache(const SArray* pTableReq, const SArray* pTableData, SHashObj** pTable) {
X
Xiaoyu Wang 已提交
593
  int32_t ntables = taosArrayGetSize(pTableReq);
594 595
  for (int32_t i = 0; i < ntables; ++i) {
    char fullName[TSDB_TABLE_FNAME_LEN];
X
Xiaoyu Wang 已提交
596 597
    tNameExtractFullName(taosArrayGet(pTableReq, i), fullName);
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(fullName, strlen(fullName), pTableData, i, pTable)) {
598 599 600 601 602 603
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

604
static int32_t putDbDataToCache(const SArray* pDbReq, const SArray* pDbData, SHashObj** pDb) {
X
Xiaoyu Wang 已提交
605
  int32_t nvgs = taosArrayGetSize(pDbReq);
606
  for (int32_t i = 0; i < nvgs; ++i) {
X
Xiaoyu Wang 已提交
607 608
    char* pDbFName = taosArrayGet(pDbReq, i);
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(pDbFName, strlen(pDbFName), pDbData, i, pDb)) {
609 610 611 612 613 614
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

615
static int32_t putUserAuthToCache(const SArray* pUserAuthReq, const SArray* pUserAuthData, SHashObj** pUserAuth) {
616 617 618 619 620
  int32_t nvgs = taosArrayGetSize(pUserAuthReq);
  for (int32_t i = 0; i < nvgs; ++i) {
    SUserAuthInfo* pUser = taosArrayGet(pUserAuthReq, i);
    char           key[USER_AUTH_KEY_MAX_LEN] = {0};
    int32_t        len = userAuthToStringExt(pUser->user, pUser->dbFName, pUser->type, key);
X
Xiaoyu Wang 已提交
621
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(key, len, pUserAuthData, i, pUserAuth)) {
622 623 624 625 626 627
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

628
static int32_t putUdfToCache(const SArray* pUdfReq, const SArray* pUdfData, SHashObj** pUdf) {
629 630
  int32_t num = taosArrayGetSize(pUdfReq);
  for (int32_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
631 632
    char* pFunc = taosArrayGet(pUdfReq, i);
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(pFunc, strlen(pFunc), pUdfData, i, pUdf)) {
633 634 635 636 637 638
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

639
int32_t putMetaDataToCache(const SCatalogReq* pCatalogReq, const SMetaData* pMetaData, SParseMetaCache* pMetaCache) {
640
  int32_t code = putTableDataToCache(pCatalogReq->pTableMeta, pMetaData->pTableMeta, &pMetaCache->pTableMeta);
X
Xiaoyu Wang 已提交
641
  if (TSDB_CODE_SUCCESS == code) {
642
    code = putDbDataToCache(pCatalogReq->pDbVgroup, pMetaData->pDbVgroup, &pMetaCache->pDbVgroup);
X
Xiaoyu Wang 已提交
643
  }
644
  if (TSDB_CODE_SUCCESS == code) {
645
    code = putTableDataToCache(pCatalogReq->pTableHash, pMetaData->pTableHash, &pMetaCache->pTableVgroup);
646 647
  }
  if (TSDB_CODE_SUCCESS == code) {
648
    code = putDbDataToCache(pCatalogReq->pDbCfg, pMetaData->pDbCfg, &pMetaCache->pDbCfg);
649 650
  }
  if (TSDB_CODE_SUCCESS == code) {
651
    code = putDbDataToCache(pCatalogReq->pDbInfo, pMetaData->pDbInfo, &pMetaCache->pDbInfo);
652
  }
653
  if (TSDB_CODE_SUCCESS == code) {
654
    code = putUserAuthToCache(pCatalogReq->pUser, pMetaData->pUser, &pMetaCache->pUserAuth);
655
  }
656
  if (TSDB_CODE_SUCCESS == code) {
657
    code = putUdfToCache(pCatalogReq->pUdf, pMetaData->pUdfList, &pMetaCache->pUdf);
658
  }
X
Xiaoyu Wang 已提交
659
  if (TSDB_CODE_SUCCESS == code) {
660
    code = putTableDataToCache(pCatalogReq->pTableIndex, pMetaData->pTableIndex, &pMetaCache->pTableIndex);
X
Xiaoyu Wang 已提交
661
  }
D
dapan1121 已提交
662 663 664
  if (TSDB_CODE_SUCCESS == code) {
    code = putTableDataToCache(pCatalogReq->pTableCfg, pMetaData->pTableCfg, &pMetaCache->pTableCfg);
  }
665 666 667
  return code;
}

668
static int32_t reserveTableReqInCacheImpl(const char* pTbFName, int32_t len, SHashObj** pTables) {
669 670 671
  if (NULL == *pTables) {
    *pTables = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
    if (NULL == *pTables) {
672 673 674
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
675
  return taosHashPut(*pTables, pTbFName, len, &nullPointer, POINTER_BYTES);
676 677 678
}

static int32_t reserveTableReqInCache(int32_t acctId, const char* pDb, const char* pTable, SHashObj** pTables) {
679 680
  char    fullName[TSDB_TABLE_FNAME_LEN];
  int32_t len = snprintf(fullName, sizeof(fullName), "%d.%s.%s", acctId, pDb, pTable);
681
  return reserveTableReqInCacheImpl(fullName, len, pTables);
682 683 684 685
}

int32_t reserveTableMetaInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableMeta);
686 687
}

688 689 690 691
int32_t reserveTableMetaInCacheExt(const SName* pName, SParseMetaCache* pMetaCache) {
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
  return reserveTableReqInCacheImpl(fullName, strlen(fullName), &pMetaCache->pTableMeta);
692 693 694 695 696
}

int32_t getTableMetaFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta) {
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
X
Xiaoyu Wang 已提交
697 698 699 700 701 702 703
  STableMeta* pTableMeta = NULL;
  int32_t     code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableMeta, (void**)&pTableMeta);
  if (TSDB_CODE_SUCCESS == code) {
    *pMeta = tableMetaDup(pTableMeta);
    if (NULL == *pMeta) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
704
  }
X
Xiaoyu Wang 已提交
705
  return code;
706 707
}

708 709 710 711 712 713 714 715 716
static int32_t reserveDbReqInCache(int32_t acctId, const char* pDb, SHashObj** pDbs) {
  if (NULL == *pDbs) {
    *pDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
    if (NULL == *pDbs) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  char    fullName[TSDB_TABLE_FNAME_LEN];
  int32_t len = snprintf(fullName, sizeof(fullName), "%d.%s", acctId, pDb);
717
  return taosHashPut(*pDbs, fullName, len, &nullPointer, POINTER_BYTES);
718 719
}

720 721 722 723 724
int32_t reserveDbVgInfoInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbVgroup);
}

int32_t getDbVgInfoFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, SArray** pVgInfo) {
X
Xiaoyu Wang 已提交
725 726 727 728 729
  SArray* pVgList = NULL;
  int32_t code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbVgroup, (void**)&pVgList);
  // pVgList is null, which is a legal value, indicating that the user DB has not been created
  if (TSDB_CODE_SUCCESS == code && NULL != pVgList) {
    *pVgInfo = taosArrayDup(pVgList);
730
    if (NULL == *pVgInfo) {
X
Xiaoyu Wang 已提交
731
      code = TSDB_CODE_OUT_OF_MEMORY;
732 733
    }
  }
X
Xiaoyu Wang 已提交
734
  return code;
735 736
}

737 738
int32_t reserveTableVgroupInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableVgroup);
739 740
}

741
int32_t reserveTableVgroupInCacheExt(const SName* pName, SParseMetaCache* pMetaCache) {
742 743
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
744 745 746
  return reserveTableReqInCacheImpl(fullName, strlen(fullName), &pMetaCache->pTableVgroup);
}

747
int32_t getTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup) {
748 749
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
X
Xiaoyu Wang 已提交
750 751 752 753
  SVgroupInfo* pVg = NULL;
  int32_t      code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableVgroup, (void**)&pVg);
  if (TSDB_CODE_SUCCESS == code) {
    memcpy(pVgroup, pVg, sizeof(SVgroupInfo));
754
  }
X
Xiaoyu Wang 已提交
755
  return code;
756 757
}

758
int32_t reserveDbVgVersionInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
X
Xiaoyu Wang 已提交
759
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbInfo);
760 761
}

762
int32_t getDbVgVersionFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, int32_t* pVersion, int64_t* pDbId,
763
                                int32_t* pTableNum) {
X
Xiaoyu Wang 已提交
764 765 766 767 768 769
  SDbInfo* pDbInfo = NULL;
  int32_t  code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbInfo, (void**)&pDbInfo);
  if (TSDB_CODE_SUCCESS == code) {
    *pVersion = pDbInfo->vgVer;
    *pDbId = pDbInfo->dbId;
    *pTableNum = pDbInfo->tbNum;
770
  }
X
Xiaoyu Wang 已提交
771
  return code;
772 773
}

774 775 776 777 778
int32_t reserveDbCfgInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbCfg);
}

int32_t getDbCfgFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, SDbCfgInfo* pInfo) {
X
Xiaoyu Wang 已提交
779 780 781 782
  SDbCfgInfo* pDbCfg = NULL;
  int32_t     code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbCfg, (void**)&pDbCfg);
  if (TSDB_CODE_SUCCESS == code) {
    memcpy(pInfo, pDbCfg, sizeof(SDbCfgInfo));
783
  }
X
Xiaoyu Wang 已提交
784
  return code;
785
}
786

787
static int32_t reserveUserAuthInCacheImpl(const char* pKey, int32_t len, SParseMetaCache* pMetaCache) {
788 789 790 791 792 793
  if (NULL == pMetaCache->pUserAuth) {
    pMetaCache->pUserAuth = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
    if (NULL == pMetaCache->pUserAuth) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
794 795 796 797 798 799
  bool pass = false;
  return taosHashPut(pMetaCache->pUserAuth, pKey, len, &pass, sizeof(pass));
}

int32_t reserveUserAuthInCache(int32_t acctId, const char* pUser, const char* pDb, AUTH_TYPE type,
                               SParseMetaCache* pMetaCache) {
800 801
  char    key[USER_AUTH_KEY_MAX_LEN] = {0};
  int32_t len = userAuthToString(acctId, pUser, pDb, type, key);
802 803 804 805 806 807 808 809 810
  return reserveUserAuthInCacheImpl(key, len, pMetaCache);
}

int32_t reserveUserAuthInCacheExt(const char* pUser, const SName* pName, AUTH_TYPE type, SParseMetaCache* pMetaCache) {
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pName, dbFName);
  char    key[USER_AUTH_KEY_MAX_LEN] = {0};
  int32_t len = userAuthToStringExt(pUser, dbFName, type, key);
  return reserveUserAuthInCacheImpl(key, len, pMetaCache);
811 812 813 814 815 816
}

int32_t getUserAuthFromCache(SParseMetaCache* pMetaCache, const char* pUser, const char* pDbFName, AUTH_TYPE type,
                             bool* pPass) {
  char    key[USER_AUTH_KEY_MAX_LEN] = {0};
  int32_t len = userAuthToStringExt(pUser, pDbFName, type, key);
X
Xiaoyu Wang 已提交
817 818 819 820
  bool*   pRes = NULL;
  int32_t code = getMetaDataFromHash(key, len, pMetaCache->pUserAuth, (void**)&pRes);
  if (TSDB_CODE_SUCCESS == code) {
    *pPass = *pRes;
821
  }
X
Xiaoyu Wang 已提交
822
  return code;
823
}
824 825 826 827 828 829 830 831

int32_t reserveUdfInCache(const char* pFunc, SParseMetaCache* pMetaCache) {
  if (NULL == pMetaCache->pUdf) {
    pMetaCache->pUdf = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
    if (NULL == pMetaCache->pUdf) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
832
  return taosHashPut(pMetaCache->pUdf, pFunc, strlen(pFunc), &nullPointer, POINTER_BYTES);
833 834
}

835
int32_t getUdfInfoFromCache(SParseMetaCache* pMetaCache, const char* pFunc, SFuncInfo* pInfo) {
X
Xiaoyu Wang 已提交
836 837 838 839
  SFuncInfo* pFuncInfo = NULL;
  int32_t    code = getMetaDataFromHash(pFunc, strlen(pFunc), pMetaCache->pUdf, (void**)&pFuncInfo);
  if (TSDB_CODE_SUCCESS == code) {
    memcpy(pInfo, pFuncInfo, sizeof(SFuncInfo));
840
  }
X
Xiaoyu Wang 已提交
841
  return code;
842
}
X
Xiaoyu Wang 已提交
843

X
Xiaoyu Wang 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
static void destroySmaIndex(void* p) { taosMemoryFree(((STableIndexInfo*)p)->expr); }

static SArray* smaIndexesDup(SArray* pSrc) {
  SArray* pDst = taosArrayDup(pSrc);
  if (NULL == pDst) {
    return NULL;
  }
  int32_t size = taosArrayGetSize(pDst);
  for (int32_t i = 0; i < size; ++i) {
    ((STableIndexInfo*)taosArrayGet(pDst, i))->expr = NULL;
  }
  for (int32_t i = 0; i < size; ++i) {
    STableIndexInfo* pIndex = taosArrayGet(pDst, i);
    pIndex->expr = taosMemoryStrDup(((STableIndexInfo*)taosArrayGet(pSrc, i))->expr);
    if (NULL == pIndex->expr) {
      taosArrayDestroyEx(pDst, destroySmaIndex);
      return NULL;
    }
  }
  return pDst;
}

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

D
dapan1121 已提交
870 871 872 873
int32_t reserveTableCfgInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableCfg);
}

X
Xiaoyu Wang 已提交
874
int32_t getTableIndexFromCache(SParseMetaCache* pMetaCache, const SName* pName, SArray** pIndexes) {
X
Xiaoyu Wang 已提交
875 876 877 878
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
  SArray* pSmaIndexes = NULL;
  int32_t code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableIndex, (void**)&pSmaIndexes);
X
Xiaoyu Wang 已提交
879
  if (TSDB_CODE_SUCCESS == code && NULL != pSmaIndexes) {
X
Xiaoyu Wang 已提交
880 881 882 883 884 885
    *pIndexes = smaIndexesDup(pSmaIndexes);
    if (NULL == *pIndexes) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return code;
X
Xiaoyu Wang 已提交
886
}
887

D
dapan1121 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901
int32_t getTableCfgFromCache(SParseMetaCache* pMetaCache, const char* pFunc, STableCfg** pOutput) {
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
  STableCfg* pCfg = NULL;
  int32_t code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableIndex, (void**)&pCfg);
  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = tableCfgDup(pCfg);
    if (NULL == *pOutput) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return code;
}

902 903 904 905 906 907 908 909 910
void destoryParseMetaCache(SParseMetaCache* pMetaCache) {
  taosHashCleanup(pMetaCache->pTableMeta);
  taosHashCleanup(pMetaCache->pDbVgroup);
  taosHashCleanup(pMetaCache->pTableVgroup);
  taosHashCleanup(pMetaCache->pDbCfg);
  taosHashCleanup(pMetaCache->pDbInfo);
  taosHashCleanup(pMetaCache->pUserAuth);
  taosHashCleanup(pMetaCache->pUdf);
  taosHashCleanup(pMetaCache->pTableIndex);
D
dapan1121 已提交
911
  taosHashCleanup(pMetaCache->pTableCfg);
912
}