parUtil.c 41.1 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

X
Xiaoyu Wang 已提交
20
#define USER_AUTH_KEY_MAX_LEN TSDB_USER_LEN + TSDB_TABLE_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 35
    case TSDB_CODE_PAR_GET_META_ERROR:
      return "Fail to get table info, error: %s";
36
    case TSDB_CODE_PAR_AMBIGUOUS_COLUMN:
X
Xiaoyu Wang 已提交
37
      return "Column ambiguously defined: %s";
38
    case TSDB_CODE_PAR_WRONG_VALUE_TYPE:
X
Xiaoyu Wang 已提交
39
      return "Invalid value type: %s";
40 41 42 43 44 45 46 47 48 49 50
    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:
51
      return "Tags number not matched";
52
    case TSDB_CODE_PAR_INVALID_TAG_NAME:
X
Xiaoyu Wang 已提交
53
      return "Invalid tag name: %s";
54
    case TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG:
55
      return "Name or password too long";
56
    case TSDB_CODE_PAR_PASSWD_EMPTY:
57
      return "Password can not be empty";
58
    case TSDB_CODE_PAR_INVALID_PORT:
59
      return "Port should be an integer that is less than 65535 and greater than 0";
60
    case TSDB_CODE_PAR_INVALID_ENDPOINT:
61 62 63
      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 已提交
64
    case TSDB_CODE_PAR_INTER_VALUE_TOO_SMALL:
X
Xiaoyu Wang 已提交
65
      return "Interval cannot be less than %d %s";
X
Xiaoyu Wang 已提交
66
    case TSDB_CODE_PAR_DB_NOT_SPECIFIED:
67
      return "Database not specified";
X
Xiaoyu Wang 已提交
68
    case TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME:
X
Xiaoyu Wang 已提交
69
      return "Invalid identifier name: %s";
X
Xiaoyu Wang 已提交
70
    case TSDB_CODE_PAR_CORRESPONDING_STABLE_ERR:
71
      return "Corresponding super table not in this db";
X
Xiaoyu Wang 已提交
72 73
    case TSDB_CODE_PAR_GROUPBY_WINDOW_COEXIST:
      return "GROUP BY and WINDOW-clause can't be used together";
X
Xiaoyu Wang 已提交
74 75
    case TSDB_CODE_PAR_AGG_FUNC_NESTING:
      return "Aggregate functions do not support nesting";
X
Xiaoyu Wang 已提交
76
    case TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE:
77
      return "Only support STATE_WINDOW on integer/bool/varchar column";
X
Xiaoyu Wang 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    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:
97
      return "sliding value can not less than 1%% of interval value";
98 99
    case TSDB_CODE_PAR_ONLY_ONE_JSON_TAG:
      return "Only one tag if there is a json tag";
X
Xiaoyu Wang 已提交
100 101
    case TSDB_CODE_PAR_INCORRECT_NUM_OF_COL:
      return "Query block has incorrect number of result columns";
X
Xiaoyu Wang 已提交
102 103 104 105
    case TSDB_CODE_PAR_INCORRECT_TIMESTAMP_VAL:
      return "Incorrect TIMESTAMP value: %s";
    case TSDB_CODE_PAR_OFFSET_LESS_ZERO:
      return "soffset/offset can not be less than 0";
D
dapan1121 已提交
106 107
    case TSDB_CODE_PAR_SLIMIT_LEAK_PARTITION_GROUP_BY:
      return "slimit/soffset only available for PARTITION/GROUP BY query";
X
Xiaoyu Wang 已提交
108 109
    case TSDB_CODE_PAR_INVALID_TOPIC_QUERY:
      return "Invalid topic query";
X
Xiaoyu Wang 已提交
110 111
    case TSDB_CODE_PAR_INVALID_DROP_STABLE:
      return "Cannot drop super table in batch";
X
Xiaoyu Wang 已提交
112
    case TSDB_CODE_PAR_INVALID_FILL_TIME_RANGE:
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      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 已提交
130 131
    case TSDB_CODE_PAR_INVALID_INTERNAL_PK:
      return "Invalid _c0 or _rowts expression";
132 133 134 135
    case TSDB_CODE_PAR_INVALID_TIMELINE_FUNC:
      return "Invalid timeline function";
    case TSDB_CODE_PAR_INVALID_PASSWD:
      return "Invalid password";
X
Xiaoyu Wang 已提交
136 137
    case TSDB_CODE_PAR_INVALID_ALTER_TABLE:
      return "Invalid alter table statement";
X
Xiaoyu Wang 已提交
138 139 140
    case TSDB_CODE_PAR_CANNOT_DROP_PRIMARY_KEY:
      return "Primary timestamp column cannot be dropped";
    case TSDB_CODE_PAR_INVALID_MODIFY_COL:
S
Shungang Li 已提交
141
      return "Only binary/nchar/geometry column length could be modified, and the length can only be increased, not decreased";
142 143
    case TSDB_CODE_PAR_INVALID_TBNAME:
      return "Invalid tbname pseudo column";
144 145 146 147
    case TSDB_CODE_PAR_INVALID_FUNCTION_NAME:
      return "Invalid function name";
    case TSDB_CODE_PAR_COMMENT_TOO_LONG:
      return "Comment too long";
148
    case TSDB_CODE_PAR_NOT_ALLOWED_FUNC:
149
      return "Some functions are allowed only in the SELECT list of a query. "
150
             "And, cannot be mixed with other non scalar functions or columns.";
151 152
    case TSDB_CODE_PAR_NOT_ALLOWED_WIN_QUERY:
      return "Window query not supported, since the result of subquery not include valid timestamp column";
153 154
    case TSDB_CODE_PAR_INVALID_DROP_COL:
      return "No columns can be dropped";
wmmhello's avatar
wmmhello 已提交
155 156
    case TSDB_CODE_PAR_INVALID_COL_JSON:
      return "Only tag can be json type";
D
dapan1121 已提交
157 158
    case TSDB_CODE_PAR_VALUE_TOO_LONG:
      return "Value too long for column/tag: %s";
X
Xiaoyu Wang 已提交
159 160
    case TSDB_CODE_PAR_INVALID_DELETE_WHERE:
      return "The DELETE statement must have a definite time window range";
X
Xiaoyu Wang 已提交
161 162
    case TSDB_CODE_PAR_INVALID_REDISTRIBUTE_VG:
      return "The REDISTRIBUTE VGROUP statement only support 1 to 3 dnodes";
X
Xiaoyu Wang 已提交
163
    case TSDB_CODE_PAR_FILL_NOT_ALLOWED_FUNC:
164
      return "%s function is not supported in fill query";
X
Xiaoyu Wang 已提交
165
    case TSDB_CODE_PAR_INVALID_WINDOW_PC:
166
      return "_WSTART, _WEND and _WDURATION can only be used in window query";
167
    case TSDB_CODE_PAR_WINDOW_NOT_ALLOWED_FUNC:
168
      return "%s function is not supported in time window query";
169
    case TSDB_CODE_PAR_STREAM_NOT_ALLOWED_FUNC:
170
      return "%s function is not supported in stream query";
171
    case TSDB_CODE_PAR_GROUP_BY_NOT_ALLOWED_FUNC:
172
      return "%s function is not supported in group query";
X
Xiaoyu Wang 已提交
173 174
    case TSDB_CODE_PAR_INVALID_INTERP_CLAUSE:
      return "Invalid usage of RANGE clause, EVERY clause or FILL clause";
175 176
    case TSDB_CODE_PAR_NO_VALID_FUNC_IN_WIN:
      return "No valid function in window query";
D
dapan1121 已提交
177 178
    case TSDB_CODE_PAR_INVALID_OPTR_USAGE:
      return "Invalid usage of expr: %s";
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    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;
}

194 195 196 197 198 199 200 201
int32_t generateSyntaxErrMsgExt(SMsgBuf* pBuf, int32_t errCode, const char* pFormat, ...) {
  va_list vArgList;
  va_start(vArgList, pFormat);
  vsnprintf(pBuf->buf, pBuf->len, pFormat, vArgList);
  va_end(vArgList);
  return errCode;
}

X
Xiaoyu Wang 已提交
202 203 204 205 206 207
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) {
208
  if (pBuf == NULL) return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
209 210 211 212 213 214 215 216 217
  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) {
    snprintf(pBuf->buf, pBuf->len, msgFormat1, additionalInfo);
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
  }
218

X
Xiaoyu Wang 已提交
219 220 221 222 223 224 225 226 227 228 229 230
  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;
}
231

232
SSchema* getTableColumnSchema(const STableMeta* pTableMeta) { return (SSchema*)pTableMeta->schema; }
233

X
Xiaoyu Wang 已提交
234
static SSchema* getOneColumnSchema(const STableMeta* pTableMeta, int32_t colIndex) {
X
Xiaoyu Wang 已提交
235
  SSchema* pSchema = (SSchema*)pTableMeta->schema;
X
Xiaoyu Wang 已提交
236
  return &pSchema[colIndex];
237
}
238

X
Xiaoyu Wang 已提交
239 240
SSchema* getTableTagSchema(const STableMeta* pTableMeta) {
  return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns);
241 242
}

X
Xiaoyu Wang 已提交
243 244 245
int32_t getNumOfColumns(const STableMeta* pTableMeta) {
  // table created according to super table, use data from super table
  return getTableInfo(pTableMeta).numOfColumns;
246 247
}

248
int32_t getNumOfTags(const STableMeta* pTableMeta) { return getTableInfo(pTableMeta).numOfTags; }
249

250
STableComInfo getTableInfo(const STableMeta* pTableMeta) { return pTableMeta->tableInfo; }
251

252 253 254 255 256 257 258 259 260 261 262
int32_t getTableTypeFromTableNode(SNode *pTable) {
  if (NULL == pTable) {
    return -1;
  }
  if (QUERY_NODE_REAL_TABLE != nodeType(pTable)) {
    return -1;
  }
  return ((SRealTableNode *)pTable)->pMeta->tableType;
}


X
Xiaoyu Wang 已提交
263
STableMeta* tableMetaDup(const STableMeta* pTableMeta) {
X
Xiaoyu Wang 已提交
264 265
  int32_t numOfFields = TABLE_TOTAL_COL_NUM(pTableMeta);
  if (numOfFields > TSDB_MAX_COLUMNS || numOfFields < TSDB_MIN_COLUMNS) {
X
Xiaoyu Wang 已提交
266 267
    return NULL;
  }
X
Xiaoyu Wang 已提交
268

X
Xiaoyu Wang 已提交
269
  size_t      size = sizeof(STableMeta) + numOfFields * sizeof(SSchema);
X
Xiaoyu Wang 已提交
270 271 272 273 274
  STableMeta* p = taosMemoryMalloc(size);
  memcpy(p, pTableMeta, size);
  return p;
}

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

X
Xiaoyu Wang 已提交
278
  char    delim = src[0];
279 280 281
  int32_t j = 0;
  for (uint32_t k = 1; k < len - 1; ++k) {
    if (j >= dlen) {
282 283
      dst[j - 1] = '\0';
      return j;
284
    }
X
Xiaoyu Wang 已提交
285
    if (src[k] == delim && src[k + 1] == delim) {  // deal with "", ''
286 287 288 289 290
      dst[j] = src[k + 1];
      j++;
      k++;
      continue;
    }
291

X
Xiaoyu Wang 已提交
292 293
    if (src[k] == '\\') {  // deal with escape character
      if (src[k + 1] == 'n') {
294
        dst[j] = '\n';
X
Xiaoyu Wang 已提交
295
      } else if (src[k + 1] == 'r') {
296
        dst[j] = '\r';
X
Xiaoyu Wang 已提交
297
      } else if (src[k + 1] == 't') {
298
        dst[j] = '\t';
X
Xiaoyu Wang 已提交
299
      } else if (src[k + 1] == '\\') {
300
        dst[j] = '\\';
X
Xiaoyu Wang 已提交
301
      } else if (src[k + 1] == '\'') {
302
        dst[j] = '\'';
X
Xiaoyu Wang 已提交
303
      } else if (src[k + 1] == '"') {
304
        dst[j] = '"';
X
Xiaoyu Wang 已提交
305
      } else if (src[k + 1] == '%' || src[k + 1] == '_') {
306
        dst[j++] = src[k];
X
Xiaoyu Wang 已提交
307 308 309
        dst[j] = src[k + 1];
      } else {
        dst[j] = src[k + 1];
310 311 312 313 314 315
      }
      j++;
      k++;
      continue;
    }

316 317 318
    dst[j] = src[k];
    j++;
  }
K
kailixu 已提交
319
  if (j >= dlen) j = dlen - 1;
320 321 322
  dst[j] = '\0';
  return j;
}
323

X
Xiaoyu Wang 已提交
324
static bool isValidateTag(char* input) {
325 326
  if (!input) return false;
  for (size_t i = 0; i < strlen(input); ++i) {
327
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
328
    if (input[i] < 0x20 || input[i] > 0x7E) return false;
329
#else
330
    if (isprint(input[i]) == 0) return false;
331
#endif
332 333 334 335
  }
  return true;
}

wmmhello's avatar
wmmhello 已提交
336
int32_t parseJsontoTagData(const char* json, SArray* pTagVals, STag** ppTag, void* pMsgBuf) {
wmmhello's avatar
wmmhello 已提交
337
  int32_t   retCode = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
338
  cJSON*    root = NULL;
wmmhello's avatar
wmmhello 已提交
339
  SHashObj* keyHash = NULL;
X
Xiaoyu Wang 已提交
340
  int32_t   size = 0;
341
  // set json NULL data
342
  if (!json || strtrim((char*)json) == 0 || strcasecmp(json, TSDB_DATA_NULL_STR_L) == 0) {
wmmhello's avatar
wmmhello 已提交
343 344
    retCode = TSDB_CODE_SUCCESS;
    goto end;
345 346 347
  }

  // set json real data
wmmhello's avatar
wmmhello 已提交
348
  root = cJSON_Parse(json);
X
Xiaoyu Wang 已提交
349
  if (root == NULL) {
wmmhello's avatar
wmmhello 已提交
350 351
    retCode = buildSyntaxErrMsg(pMsgBuf, "json parse error", json);
    goto end;
352 353
  }

wmmhello's avatar
wmmhello 已提交
354
  size = cJSON_GetArraySize(root);
X
Xiaoyu Wang 已提交
355
  if (!cJSON_IsObject(root)) {
wmmhello's avatar
wmmhello 已提交
356 357
    retCode = buildSyntaxErrMsg(pMsgBuf, "json error invalide value", json);
    goto end;
358 359
  }

wmmhello's avatar
wmmhello 已提交
360
  keyHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false);
361
  for (int32_t i = 0; i < size; i++) {
362 363
    cJSON* item = cJSON_GetArrayItem(root, i);
    if (!item) {
wmmhello's avatar
wmmhello 已提交
364
      uError("json inner error:%d", i);
365
      retCode = buildSyntaxErrMsg(pMsgBuf, "json inner error", json);
366 367 368
      goto end;
    }

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

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

end:
  taosHashCleanup(keyHash);
X
Xiaoyu Wang 已提交
429
  if (retCode == TSDB_CODE_SUCCESS) {
wmmhello's avatar
wmmhello 已提交
430
    retCode = tTagNew(pTagVals, 1, true, ppTag);
wmmhello's avatar
wmmhello 已提交
431
  }
432 433 434 435 436 437
  for (int i = 0; i < taosArrayGetSize(pTagVals); ++i) {
    STagVal* p = (STagVal*)taosArrayGet(pTagVals, i);
    if (IS_VAR_DATA_TYPE(p->type)) {
      taosMemoryFreeClear(p->pData);
    }
  }
438 439
  cJSON_Delete(root);
  return retCode;
440 441
}

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
static int32_t getInsTagsTableTargetNameFromOp(int32_t acctId, SOperatorNode* pOper, SName* pName) {
  if (OP_TYPE_EQUAL != pOper->opType) {
    return TSDB_CODE_SUCCESS;
  }

  SColumnNode* pCol = NULL;
  SValueNode*  pVal = NULL;
  if (QUERY_NODE_COLUMN == nodeType(pOper->pLeft)) {
    pCol = (SColumnNode*)pOper->pLeft;
  } else if (QUERY_NODE_VALUE == nodeType(pOper->pLeft)) {
    pVal = (SValueNode*)pOper->pLeft;
  }
  if (QUERY_NODE_COLUMN == nodeType(pOper->pRight)) {
    pCol = (SColumnNode*)pOper->pRight;
  } else if (QUERY_NODE_VALUE == nodeType(pOper->pRight)) {
    pVal = (SValueNode*)pOper->pRight;
  }
459
  if (NULL == pCol || NULL == pVal || NULL == pVal->literal || 0 == strcmp(pVal->literal, "")) {
460 461 462 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
    return TSDB_CODE_SUCCESS;
  }

  if (0 == strcmp(pCol->colName, "db_name")) {
    return tNameSetDbName(pName, acctId, pVal->literal, strlen(pVal->literal));
  } else if (0 == strcmp(pCol->colName, "table_name")) {
    return tNameAddTbName(pName, pVal->literal, strlen(pVal->literal));
  }

  return TSDB_CODE_SUCCESS;
}

static void getInsTagsTableTargetObjName(int32_t acctId, SNode* pNode, SName* pName) {
  if (QUERY_NODE_OPERATOR == nodeType(pNode)) {
    getInsTagsTableTargetNameFromOp(acctId, (SOperatorNode*)pNode, pName);
  }
}

static int32_t getInsTagsTableTargetNameFromCond(int32_t acctId, SLogicConditionNode* pCond, SName* pName) {
  if (LOGIC_COND_TYPE_AND != pCond->condType) {
    return TSDB_CODE_SUCCESS;
  }

  SNode* pNode = NULL;
  FOREACH(pNode, pCond->pParameterList) { getInsTagsTableTargetObjName(acctId, pNode, pName); }
  if ('\0' == pName->dbname[0]) {
    pName->type = 0;
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
491
int32_t getVnodeSysTableTargetName(int32_t acctId, SNode* pWhere, SName* pName) {
492 493 494 495 496
  if (NULL == pWhere) {
    return TSDB_CODE_SUCCESS;
  }

  if (QUERY_NODE_OPERATOR == nodeType(pWhere)) {
497 498 499 500 501
    int32_t code = getInsTagsTableTargetNameFromOp(acctId, (SOperatorNode*)pWhere, pName);
    if (TSDB_CODE_SUCCESS == code && '\0' == pName->dbname[0]) {
      pName->type = 0;
    }
    return code;
502 503 504 505 506 507 508 509 510
  }

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pWhere)) {
    return getInsTagsTableTargetNameFromCond(acctId, (SLogicConditionNode*)pWhere, pName);
  }

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
511 512
static int32_t userAuthToString(int32_t acctId, const char* pUser, const char* pDb, const char* pTable, AUTH_TYPE type,
                                char* pStr) {
513
  return sprintf(pStr, "%s*%d*%s*%s*%d", pUser, acctId, pDb, (NULL == pTable || '\0' == pTable[0]) ? "``" : pTable,
X
Xiaoyu Wang 已提交
514
                 type);
515 516
}

X
Xiaoyu Wang 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
static int32_t getIntegerFromAuthStr(const char* pStart, char** pNext) {
  char* p = strchr(pStart, '*');
  char  buf[10] = {0};
  if (NULL == p) {
    strcpy(buf, pStart);
    *pNext = NULL;
  } else {
    strncpy(buf, pStart, p - pStart);
    *pNext = ++p;
  }
  return taosStr2Int32(buf, NULL, 10);
}

static void getStringFromAuthStr(const char* pStart, char* pStr, char** pNext) {
  char* p = strchr(pStart, '*');
  if (NULL == p) {
    strcpy(pStr, pStart);
    *pNext = NULL;
  } else {
    strncpy(pStr, pStart, p - pStart);
    *pNext = ++p;
  }
539 540 541
  if (*pStart == '`' && *(pStart + 1) == '`') {
    *pStr = 0;
  }
542 543 544
}

static void stringToUserAuth(const char* pStr, int32_t len, SUserAuthInfo* pUserAuth) {
X
Xiaoyu Wang 已提交
545 546 547 548 549
  char* p = NULL;
  getStringFromAuthStr(pStr, pUserAuth->user, &p);
  pUserAuth->tbName.acctId = getIntegerFromAuthStr(p, &p);
  getStringFromAuthStr(p, pUserAuth->tbName.dbname, &p);
  getStringFromAuthStr(p, pUserAuth->tbName.tname, &p);
550 551 552 553 554
  if (pUserAuth->tbName.tname[0]) {
    pUserAuth->tbName.type = TSDB_TABLE_NAME_T;
  } else {
    pUserAuth->tbName.type = TSDB_DB_NAME_T;
  }
X
Xiaoyu Wang 已提交
555
  pUserAuth->type = getIntegerFromAuthStr(p, &p);
556 557
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
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;
}

598 599 600
static int32_t buildTableReqFromDb(SHashObj* pDbsHash, SArray** pDbs) {
  if (NULL != pDbsHash) {
    if (NULL == *pDbs) {
X
Xiaoyu Wang 已提交
601 602 603 604
      *pDbs = taosArrayInit(taosHashGetSize(pDbsHash), sizeof(STablesReq));
      if (NULL == *pDbs) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
605 606 607 608 609 610 611 612 613 614 615 616 617
    }
    SParseTablesMetaReq* p = taosHashIterate(pDbsHash, NULL);
    while (NULL != p) {
      STablesReq req = {0};
      strcpy(req.dbFName, p->dbFName);
      buildTableReq(p->pTables, &req.pTables);
      taosArrayPush(*pDbs, &req);
      p = taosHashIterate(pDbsHash, p);
    }
  }
  return TSDB_CODE_SUCCESS;
}

618 619 620 621 622 623 624 625
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) {
X
Xiaoyu Wang 已提交
626 627 628 629
      size_t len = 0;
      char*  pKey = taosHashGetKey(p, &len);
      char   key[USER_AUTH_KEY_MAX_LEN] = {0};
      strncpy(key, pKey, len);
630
      SUserAuthInfo userAuth = {0};
X
Xiaoyu Wang 已提交
631
      stringToUserAuth(key, len, &userAuth);
632 633 634 635 636 637 638
      taosArrayPush(*pUserAuth, &userAuth);
      p = taosHashIterate(pUserAuthHash, p);
    }
  }
  return TSDB_CODE_SUCCESS;
}

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
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;
}

X
Xiaoyu Wang 已提交
658
int32_t buildCatalogReq(const SParseMetaCache* pMetaCache, SCatalogReq* pCatalogReq) {
659
  int32_t code = buildTableReqFromDb(pMetaCache->pTableMeta, &pCatalogReq->pTableMeta);
X
Xiaoyu Wang 已提交
660 661 662
  if (TSDB_CODE_SUCCESS == code) {
    code = buildDbReq(pMetaCache->pDbVgroup, &pCatalogReq->pDbVgroup);
  }
663
  if (TSDB_CODE_SUCCESS == code) {
664
    code = buildTableReqFromDb(pMetaCache->pTableVgroup, &pCatalogReq->pTableHash);
665 666
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
667
    code = buildDbReq(pMetaCache->pDbCfg, &pCatalogReq->pDbCfg);
668 669
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
670
    code = buildDbReq(pMetaCache->pDbInfo, &pCatalogReq->pDbInfo);
671
  }
672 673 674
  if (TSDB_CODE_SUCCESS == code) {
    code = buildUserAuthReq(pMetaCache->pUserAuth, &pCatalogReq->pUser);
  }
675 676
  if (TSDB_CODE_SUCCESS == code) {
    code = buildUdfReq(pMetaCache->pUdf, &pCatalogReq->pUdf);
677
  }
X
Xiaoyu Wang 已提交
678 679 680
  if (TSDB_CODE_SUCCESS == code) {
    code = buildTableReq(pMetaCache->pTableIndex, &pCatalogReq->pTableIndex);
  }
D
dapan1121 已提交
681 682 683
  if (TSDB_CODE_SUCCESS == code) {
    code = buildTableReq(pMetaCache->pTableCfg, &pCatalogReq->pTableCfg);
  }
684
  pCatalogReq->dNodeRequired = pMetaCache->dnodeRequired;
685 686 687
  return code;
}

688 689 690 691 692 693 694 695 696 697

SNode* createSelectStmtImpl(bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
  if (NULL == select) {
    return NULL;
  }
  select->isDistinct = isDistinct;
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
  sprintf(select->stmtName, "%p", select);
698
  select->timeLineResMode = select->isDistinct ? TIME_LINE_NONE : TIME_LINE_GLOBAL;
699 700 701 702 703
  select->onlyHasKeepOrderFunc = true;
  select->timeRange = TSWINDOW_INITIALIZER;
  return (SNode*)select;
}

704 705 706 707 708 709 710
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 已提交
711
  SMetaRes* pRes = taosArrayGet(pData, index);
712
  return taosHashPut(*pHash, pKey, len, &pRes, POINTER_BYTES);
713 714
}

X
Xiaoyu Wang 已提交
715 716 717 718
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;
719
  }
X
Xiaoyu Wang 已提交
720 721 722 723
  if (TSDB_CODE_SUCCESS == (*pRes)->code) {
    *pOutput = (*pRes)->pRes;
  }
  return (*pRes)->code;
724 725
}

726
static int32_t putTableDataToCache(const SArray* pTableReq, const SArray* pTableData, SHashObj** pTable) {
X
Xiaoyu Wang 已提交
727
  int32_t ntables = taosArrayGetSize(pTableReq);
728 729
  for (int32_t i = 0; i < ntables; ++i) {
    char fullName[TSDB_TABLE_FNAME_LEN];
X
Xiaoyu Wang 已提交
730 731
    tNameExtractFullName(taosArrayGet(pTableReq, i), fullName);
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(fullName, strlen(fullName), pTableData, i, pTable)) {
732 733 734 735 736 737
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

738
static int32_t putDbDataToCache(const SArray* pDbReq, const SArray* pDbData, SHashObj** pDb) {
X
Xiaoyu Wang 已提交
739
  int32_t nvgs = taosArrayGetSize(pDbReq);
740
  for (int32_t i = 0; i < nvgs; ++i) {
X
Xiaoyu Wang 已提交
741 742
    char* pDbFName = taosArrayGet(pDbReq, i);
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(pDbFName, strlen(pDbFName), pDbData, i, pDb)) {
743 744 745 746 747 748
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
static int32_t putDbTableDataToCache(const SArray* pDbReq, const SArray* pTableData, SHashObj** pTable) {
  int32_t ndbs = taosArrayGetSize(pDbReq);
  int32_t tableNo = 0;
  for (int32_t i = 0; i < ndbs; ++i) {
    STablesReq* pReq = taosArrayGet(pDbReq, i);
    int32_t     ntables = taosArrayGetSize(pReq->pTables);
    for (int32_t j = 0; j < ntables; ++j) {
      char fullName[TSDB_TABLE_FNAME_LEN];
      tNameExtractFullName(taosArrayGet(pReq->pTables, j), fullName);
      if (TSDB_CODE_SUCCESS != putMetaDataToHash(fullName, strlen(fullName), pTableData, tableNo, pTable)) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      ++tableNo;
    }
  }
  return TSDB_CODE_SUCCESS;
}

767
static int32_t putUserAuthToCache(const SArray* pUserAuthReq, const SArray* pUserAuthData, SHashObj** pUserAuth) {
768 769 770 771
  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};
X
Xiaoyu Wang 已提交
772 773
    int32_t        len = userAuthToString(pUser->tbName.acctId, pUser->user, pUser->tbName.dbname, pUser->tbName.tname,
                                          pUser->type, key);
X
Xiaoyu Wang 已提交
774
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(key, len, pUserAuthData, i, pUserAuth)) {
775 776 777 778 779 780
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

781
static int32_t putUdfToCache(const SArray* pUdfReq, const SArray* pUdfData, SHashObj** pUdf) {
782 783
  int32_t num = taosArrayGetSize(pUdfReq);
  for (int32_t i = 0; i < num; ++i) {
X
Xiaoyu Wang 已提交
784 785
    char* pFunc = taosArrayGet(pUdfReq, i);
    if (TSDB_CODE_SUCCESS != putMetaDataToHash(pFunc, strlen(pFunc), pUdfData, i, pUdf)) {
786 787 788 789 790 791
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
792
int32_t putMetaDataToCache(const SCatalogReq* pCatalogReq, const SMetaData* pMetaData, SParseMetaCache* pMetaCache) {
793
  int32_t code = putDbTableDataToCache(pCatalogReq->pTableMeta, pMetaData->pTableMeta, &pMetaCache->pTableMeta);
X
Xiaoyu Wang 已提交
794
  if (TSDB_CODE_SUCCESS == code) {
795
    code = putDbDataToCache(pCatalogReq->pDbVgroup, pMetaData->pDbVgroup, &pMetaCache->pDbVgroup);
X
Xiaoyu Wang 已提交
796
  }
797
  if (TSDB_CODE_SUCCESS == code) {
798
    code = putDbTableDataToCache(pCatalogReq->pTableHash, pMetaData->pTableHash, &pMetaCache->pTableVgroup);
799 800
  }
  if (TSDB_CODE_SUCCESS == code) {
801
    code = putDbDataToCache(pCatalogReq->pDbCfg, pMetaData->pDbCfg, &pMetaCache->pDbCfg);
802 803
  }
  if (TSDB_CODE_SUCCESS == code) {
804
    code = putDbDataToCache(pCatalogReq->pDbInfo, pMetaData->pDbInfo, &pMetaCache->pDbInfo);
805
  }
806
  if (TSDB_CODE_SUCCESS == code) {
807
    code = putUserAuthToCache(pCatalogReq->pUser, pMetaData->pUser, &pMetaCache->pUserAuth);
808
  }
809
  if (TSDB_CODE_SUCCESS == code) {
810
    code = putUdfToCache(pCatalogReq->pUdf, pMetaData->pUdfList, &pMetaCache->pUdf);
811
  }
X
Xiaoyu Wang 已提交
812
  if (TSDB_CODE_SUCCESS == code) {
813
    code = putTableDataToCache(pCatalogReq->pTableIndex, pMetaData->pTableIndex, &pMetaCache->pTableIndex);
X
Xiaoyu Wang 已提交
814
  }
D
dapan1121 已提交
815 816 817
  if (TSDB_CODE_SUCCESS == code) {
    code = putTableDataToCache(pCatalogReq->pTableCfg, pMetaData->pTableCfg, &pMetaCache->pTableCfg);
  }
818
  pMetaCache->pDnodes = pMetaData->pDnodeList;
819 820 821
  return code;
}

822
static int32_t reserveTableReqInCacheImpl(const char* pTbFName, int32_t len, SHashObj** pTables) {
823 824 825
  if (NULL == *pTables) {
    *pTables = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
    if (NULL == *pTables) {
826 827 828
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
829
  return taosHashPut(*pTables, pTbFName, len, &nullPointer, POINTER_BYTES);
830 831 832
}

static int32_t reserveTableReqInCache(int32_t acctId, const char* pDb, const char* pTable, SHashObj** pTables) {
833
  char    fullName[TSDB_TABLE_FNAME_LEN];
834
  int32_t len = snprintf(fullName, sizeof(fullName), "%d.%s.%s", acctId, pDb, pTable);
835
  return reserveTableReqInCacheImpl(fullName, len, pTables);
836 837
}

838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
static int32_t reserveTableReqInDbCacheImpl(int32_t acctId, const char* pDb, const char* pTable, SHashObj* pDbs) {
  SParseTablesMetaReq req = {0};
  int32_t             len = snprintf(req.dbFName, sizeof(req.dbFName), "%d.%s", acctId, pDb);
  int32_t             code = reserveTableReqInCache(acctId, pDb, pTable, &req.pTables);
  if (TSDB_CODE_SUCCESS == code) {
    code = taosHashPut(pDbs, req.dbFName, len, &req, sizeof(SParseTablesMetaReq));
  }
  return code;
}

static int32_t reserveTableReqInDbCache(int32_t acctId, const char* pDb, const char* pTable, 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_DB_FNAME_LEN];
  int32_t              len = snprintf(fullName, sizeof(fullName), "%d.%s", acctId, pDb);
  SParseTablesMetaReq* pReq = taosHashGet(*pDbs, fullName, len);
  if (NULL == pReq) {
    return reserveTableReqInDbCacheImpl(acctId, pDb, pTable, *pDbs);
  }
  return reserveTableReqInCache(acctId, pDb, pTable, &pReq->pTables);
}

864
int32_t reserveTableMetaInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
865
  return reserveTableReqInDbCache(acctId, pDb, pTable, &pMetaCache->pTableMeta);
866 867
}

868
int32_t reserveTableMetaInCacheExt(const SName* pName, SParseMetaCache* pMetaCache) {
869
  return reserveTableReqInDbCache(pName->acctId, pName->dbname, pName->tname, &pMetaCache->pTableMeta);
870 871 872 873 874
}

int32_t getTableMetaFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta) {
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
X
Xiaoyu Wang 已提交
875 876 877 878 879 880 881
  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;
    }
882
  }
X
Xiaoyu Wang 已提交
883
  return code;
884 885
}

886 887 888 889 890 891 892 893 894
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);
895
  return taosHashPut(*pDbs, fullName, len, &nullPointer, POINTER_BYTES);
896 897
}

898 899 900 901 902
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 已提交
903 904 905 906
  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) {
H
Haojun Liao 已提交
907
    *pVgInfo = taosArrayDup(pVgList, NULL);
908
    if (NULL == *pVgInfo) {
X
Xiaoyu Wang 已提交
909
      code = TSDB_CODE_OUT_OF_MEMORY;
910 911
    }
  }
X
Xiaoyu Wang 已提交
912
  return code;
913 914
}

915
int32_t reserveTableVgroupInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
916
  return reserveTableReqInDbCache(acctId, pDb, pTable, &pMetaCache->pTableVgroup);
917 918
}

919
int32_t reserveTableVgroupInCacheExt(const SName* pName, SParseMetaCache* pMetaCache) {
920
  return reserveTableReqInDbCache(pName->acctId, pName->dbname, pName->tname, &pMetaCache->pTableVgroup);
921 922
}

923
int32_t getTableVgroupFromCache(SParseMetaCache* pMetaCache, const SName* pName, SVgroupInfo* pVgroup) {
924 925
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
X
Xiaoyu Wang 已提交
926 927 928 929
  SVgroupInfo* pVg = NULL;
  int32_t      code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableVgroup, (void**)&pVg);
  if (TSDB_CODE_SUCCESS == code) {
    memcpy(pVgroup, pVg, sizeof(SVgroupInfo));
930
  }
X
Xiaoyu Wang 已提交
931
  return code;
932 933
}

934
int32_t reserveDbVgVersionInCache(int32_t acctId, const char* pDb, SParseMetaCache* pMetaCache) {
X
Xiaoyu Wang 已提交
935
  return reserveDbReqInCache(acctId, pDb, &pMetaCache->pDbInfo);
936 937
}

938
int32_t getDbVgVersionFromCache(SParseMetaCache* pMetaCache, const char* pDbFName, int32_t* pVersion, int64_t* pDbId,
D
dapan1121 已提交
939
                                int32_t* pTableNum, int64_t* pStateTs) {
X
Xiaoyu Wang 已提交
940 941 942 943 944 945
  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;
D
dapan1121 已提交
946
    *pStateTs = pDbInfo->stateTs;
947
  }
X
Xiaoyu Wang 已提交
948
  return code;
949 950
}

951 952 953 954 955
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 已提交
956 957 958 959
  SDbCfgInfo* pDbCfg = NULL;
  int32_t     code = getMetaDataFromHash(pDbFName, strlen(pDbFName), pMetaCache->pDbCfg, (void**)&pDbCfg);
  if (TSDB_CODE_SUCCESS == code) {
    memcpy(pInfo, pDbCfg, sizeof(SDbCfgInfo));
960
  }
X
Xiaoyu Wang 已提交
961
  return code;
962
}
963

964
static int32_t reserveUserAuthInCacheImpl(const char* pKey, int32_t len, SParseMetaCache* pMetaCache) {
965 966 967 968 969 970
  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;
    }
  }
X
Xiaoyu Wang 已提交
971
  return taosHashPut(pMetaCache->pUserAuth, pKey, len, &nullPointer, POINTER_BYTES);
972 973
}

X
Xiaoyu Wang 已提交
974
int32_t reserveUserAuthInCache(int32_t acctId, const char* pUser, const char* pDb, const char* pTable, AUTH_TYPE type,
975
                               SParseMetaCache* pMetaCache) {
976
  char    key[USER_AUTH_KEY_MAX_LEN] = {0};
X
Xiaoyu Wang 已提交
977
  int32_t len = userAuthToString(acctId, pUser, pDb, pTable, type, key);
978
  return reserveUserAuthInCacheImpl(key, len, pMetaCache);
979 980
}

X
Xiaoyu Wang 已提交
981 982 983 984 985 986
int32_t getUserAuthFromCache(SParseMetaCache* pMetaCache, SUserAuthInfo* pAuthReq, SUserAuthRes* pAuthRes) {
  char          key[USER_AUTH_KEY_MAX_LEN] = {0};
  int32_t       len = userAuthToString(pAuthReq->tbName.acctId, pAuthReq->user, pAuthReq->tbName.dbname,
                                       pAuthReq->tbName.tname, pAuthReq->type, key);
  SUserAuthRes* pAuth = NULL;
  int32_t       code = getMetaDataFromHash(key, len, pMetaCache->pUserAuth, (void**)&pAuth);
X
Xiaoyu Wang 已提交
987
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
988
    memcpy(pAuthRes, pAuth, sizeof(SUserAuthRes));
989
  }
X
Xiaoyu Wang 已提交
990
  return code;
991
}
992 993 994 995 996 997 998 999

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;
    }
  }
1000
  return taosHashPut(pMetaCache->pUdf, pFunc, strlen(pFunc), &nullPointer, POINTER_BYTES);
1001 1002
}

1003
int32_t getUdfInfoFromCache(SParseMetaCache* pMetaCache, const char* pFunc, SFuncInfo* pInfo) {
X
Xiaoyu Wang 已提交
1004 1005 1006 1007
  SFuncInfo* pFuncInfo = NULL;
  int32_t    code = getMetaDataFromHash(pFunc, strlen(pFunc), pMetaCache->pUdf, (void**)&pFuncInfo);
  if (TSDB_CODE_SUCCESS == code) {
    memcpy(pInfo, pFuncInfo, sizeof(SFuncInfo));
1008
  }
X
Xiaoyu Wang 已提交
1009
  return code;
1010
}
X
Xiaoyu Wang 已提交
1011

X
Xiaoyu Wang 已提交
1012 1013 1014
static void destroySmaIndex(void* p) { taosMemoryFree(((STableIndexInfo*)p)->expr); }

static SArray* smaIndexesDup(SArray* pSrc) {
H
Haojun Liao 已提交
1015
  SArray* pDst = taosArrayDup(pSrc, NULL);
X
Xiaoyu Wang 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024
  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);
1025
    pIndex->expr = taosStrdup(((STableIndexInfo*)taosArrayGet(pSrc, i))->expr);
X
Xiaoyu Wang 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
    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 已提交
1038 1039 1040 1041
int32_t reserveTableCfgInCache(int32_t acctId, const char* pDb, const char* pTable, SParseMetaCache* pMetaCache) {
  return reserveTableReqInCache(acctId, pDb, pTable, &pMetaCache->pTableCfg);
}

X
Xiaoyu Wang 已提交
1042
int32_t getTableIndexFromCache(SParseMetaCache* pMetaCache, const SName* pName, SArray** pIndexes) {
X
Xiaoyu Wang 已提交
1043 1044 1045 1046
  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 已提交
1047
  if (TSDB_CODE_SUCCESS == code && NULL != pSmaIndexes) {
X
Xiaoyu Wang 已提交
1048 1049 1050 1051 1052 1053
    *pIndexes = smaIndexesDup(pSmaIndexes);
    if (NULL == *pIndexes) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return code;
X
Xiaoyu Wang 已提交
1054
}
1055

D
dapan1121 已提交
1056 1057 1058 1059
STableCfg* tableCfgDup(STableCfg* pCfg) {
  STableCfg* pNew = taosMemoryMalloc(sizeof(*pNew));

  memcpy(pNew, pCfg, sizeof(*pNew));
1060
  if (NULL != pNew->pComment) {
D
dapan1121 已提交
1061 1062
    pNew->pComment = taosMemoryCalloc(pNew->commentLen + 1, 1);
    memcpy(pNew->pComment, pCfg->pComment, pNew->commentLen);
D
dapan1121 已提交
1063
  }
1064
  if (NULL != pNew->pFuncs) {
H
Haojun Liao 已提交
1065
    pNew->pFuncs = taosArrayDup(pNew->pFuncs, NULL);
D
dapan1121 已提交
1066
  }
1067
  if (NULL != pNew->pTags) {
D
dapan1121 已提交
1068 1069
    pNew->pTags = taosMemoryCalloc(pNew->tagsLen + 1, 1);
    memcpy(pNew->pTags, pCfg->pTags, pNew->tagsLen);
1070
  }
1071

D
dapan1121 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  int32_t schemaSize = (pCfg->numOfColumns + pCfg->numOfTags) * sizeof(SSchema);

  SSchema* pSchema = taosMemoryMalloc(schemaSize);
  memcpy(pSchema, pCfg->pSchemas, schemaSize);

  pNew->pSchemas = pSchema;

  return pNew;
}

int32_t getTableCfgFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableCfg** pOutput) {
D
dapan1121 已提交
1083 1084 1085
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
  STableCfg* pCfg = NULL;
1086
  int32_t    code = getMetaDataFromHash(fullName, strlen(fullName), pMetaCache->pTableCfg, (void**)&pCfg);
D
dapan1121 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095
  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = tableCfgDup(pCfg);
    if (NULL == *pOutput) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return code;
}

1096 1097 1098 1099 1100 1101
int32_t reserveDnodeRequiredInCache(SParseMetaCache* pMetaCache) {
  pMetaCache->dnodeRequired = true;
  return TSDB_CODE_SUCCESS;
}

int32_t getDnodeListFromCache(SParseMetaCache* pMetaCache, SArray** pDnodes) {
D
dapan1121 已提交
1102
  SMetaRes* pRes = taosArrayGet(pMetaCache->pDnodes, 0);
1103
  if (TSDB_CODE_SUCCESS != pRes->code) {
D
dapan1121 已提交
1104 1105
    return pRes->code;
  }
1106

H
Haojun Liao 已提交
1107
  *pDnodes = taosArrayDup((SArray*)pRes->pRes, NULL);
1108 1109 1110 1111 1112 1113
  if (NULL == *pDnodes) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  return TSDB_CODE_SUCCESS;
}

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
void destoryParseTablesMetaReqHash(SHashObj* pHash) {
  SParseTablesMetaReq* p = taosHashIterate(pHash, NULL);
  while (NULL != p) {
    taosHashCleanup(p->pTables);
    p = taosHashIterate(pHash, p);
  }
  taosHashCleanup(pHash);
}

void destoryParseMetaCache(SParseMetaCache* pMetaCache, bool request) {
  if (request) {
    destoryParseTablesMetaReqHash(pMetaCache->pTableMeta);
    destoryParseTablesMetaReqHash(pMetaCache->pTableVgroup);
  } else {
    taosHashCleanup(pMetaCache->pTableMeta);
    taosHashCleanup(pMetaCache->pTableVgroup);
  }
1131 1132 1133 1134 1135 1136
  taosHashCleanup(pMetaCache->pDbVgroup);
  taosHashCleanup(pMetaCache->pDbCfg);
  taosHashCleanup(pMetaCache->pDbInfo);
  taosHashCleanup(pMetaCache->pUserAuth);
  taosHashCleanup(pMetaCache->pUdf);
  taosHashCleanup(pMetaCache->pTableIndex);
D
dapan1121 已提交
1137
  taosHashCleanup(pMetaCache->pTableCfg);
1138
}