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

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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:
      return "Invalid column name : %s";
    case TSDB_CODE_PAR_TABLE_NOT_EXIST:
      return "Table does not exist : %s";
    case TSDB_CODE_PAR_AMBIGUOUS_COLUMN:
      return "Column ambiguously defined : %s";
    case TSDB_CODE_PAR_WRONG_VALUE_TYPE:
      return "Invalid value type : %s";
    case TSDB_CODE_PAR_INVALID_FUNTION:
      return "Invalid function name : %s";
    case TSDB_CODE_PAR_FUNTION_PARA_NUM:
      return "Invalid number of arguments : %s";
    case TSDB_CODE_PAR_FUNTION_PARA_TYPE:
      return "Inconsistent datatypes : %s";
    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:
50
      return "Tags number not matched";
51
    case TSDB_CODE_PAR_INVALID_TAG_NAME:
52
      return "Invalid tag name : %s";
53
    case TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG:
54
      return "Name or password too long";
55
    case TSDB_CODE_PAR_PASSWD_EMPTY:
56
      return "Password can not be empty";
57
    case TSDB_CODE_PAR_INVALID_PORT:
58
      return "Port should be an integer that is less than 65535 and greater than 0";
59
    case TSDB_CODE_PAR_INVALID_ENDPOINT:
60 61 62
      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 已提交
63 64
    case TSDB_CODE_PAR_INTERVAL_VALUE_TOO_SMALL:
      return "This interval value is too small : %s";
X
Xiaoyu Wang 已提交
65
    case TSDB_CODE_PAR_DB_NOT_SPECIFIED:
66
      return "Database not specified";
X
Xiaoyu Wang 已提交
67 68
    case TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME:
      return "Invalid identifier name : %s";
X
Xiaoyu Wang 已提交
69
    case TSDB_CODE_PAR_CORRESPONDING_STABLE_ERR:
70
      return "Corresponding super table not in this db";
X
Xiaoyu Wang 已提交
71
    case TSDB_CODE_PAR_INVALID_RANGE_OPTION:
72
      return "Invalid option %s: %"PRId64" valid range: [%d, %d]";
X
Xiaoyu Wang 已提交
73
    case TSDB_CODE_PAR_INVALID_STR_OPTION:
74
      return "Invalid option %s: %s";
X
Xiaoyu Wang 已提交
75
    case TSDB_CODE_PAR_INVALID_ENUM_OPTION:
76
      return "Invalid option %s: %"PRId64", only %d, %d allowed";
X
Xiaoyu Wang 已提交
77
    case TSDB_CODE_PAR_INVALID_TTL_OPTION:
78
      return "Invalid option ttl: %"PRId64", should be greater than or equal to %d";
X
Xiaoyu Wang 已提交
79
    case TSDB_CODE_PAR_INVALID_KEEP_NUM:
80
      return "Invalid number of keep options";
X
Xiaoyu Wang 已提交
81
    case TSDB_CODE_PAR_INVALID_KEEP_ORDER:
82
      return "Invalid keep value, should be keep0 <= keep1 <= keep2";
X
Xiaoyu Wang 已提交
83
    case TSDB_CODE_PAR_INVALID_KEEP_VALUE:
84
      return "Invalid option keep: %d, %d, %d valid range: [%d, %d]";
X
Xiaoyu Wang 已提交
85
    case TSDB_CODE_PAR_INVALID_COMMENT_OPTION:
86
      return "Invalid option comment, length cannot exceed %d";
X
Xiaoyu Wang 已提交
87
    case TSDB_CODE_PAR_INVALID_F_RANGE_OPTION:
88
      return "Invalid option %s: %f valid range: [%d, %d]";
X
Xiaoyu Wang 已提交
89
    case TSDB_CODE_PAR_INVALID_ROLLUP_OPTION:
90
      return "Invalid option rollup: only one function is allowed";
X
Xiaoyu Wang 已提交
91
    case TSDB_CODE_PAR_INVALID_RETENTIONS_OPTION:
92
      return "Invalid option retentions";
X
Xiaoyu Wang 已提交
93 94
    case TSDB_CODE_PAR_GROUPBY_WINDOW_COEXIST:
      return "GROUP BY and WINDOW-clause can't be used together";
95 96 97 98
    case TSDB_CODE_PAR_INVALID_OPTION_UNIT:
      return "Invalid option %s unit: %c, only m, h, d allowed";
    case TSDB_CODE_PAR_INVALID_KEEP_UNIT:
      return "Invalid option keep unit: %c, %c, %c, only m, h, d allowed";
99 100
    case TSDB_CODE_PAR_ONLY_ONE_JSON_TAG:
      return "Only one tag if there is a json tag";
101 102 103 104 105 106 107 108 109 110 111 112
    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);
X
Xiaoyu Wang 已提交
113
  terrno = errCode;
114 115 116
  return errCode;
}

X
Xiaoyu Wang 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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;
  }
133

X
Xiaoyu Wang 已提交
134 135 136 137 138 139 140 141 142 143 144 145
  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;
}
146

X
Xiaoyu Wang 已提交
147 148
static uint32_t getTableMetaSize(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
149

X
Xiaoyu Wang 已提交
150 151 152
  int32_t totalCols = 0;
  if (pTableMeta->tableInfo.numOfColumns >= 0) {
    totalCols = pTableMeta->tableInfo.numOfColumns + pTableMeta->tableInfo.numOfTags;
153 154
  }

X
Xiaoyu Wang 已提交
155
  return sizeof(STableMeta) + totalCols * sizeof(SSchema);
156 157
}

X
Xiaoyu Wang 已提交
158 159 160
STableMeta* tableMetaDup(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  size_t size = getTableMetaSize(pTableMeta);
161

wafwerar's avatar
wafwerar 已提交
162
  STableMeta* p = taosMemoryMalloc(size);
X
Xiaoyu Wang 已提交
163 164
  memcpy(p, pTableMeta, size);
  return p;
165 166
}

X
Xiaoyu Wang 已提交
167 168 169
SSchema *getTableColumnSchema(const STableMeta *pTableMeta) {
  assert(pTableMeta != NULL);
  return (SSchema*) pTableMeta->schema;
170 171
}

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

X
Xiaoyu Wang 已提交
175 176
  SSchema* pSchema = (SSchema*) pTableMeta->schema;
  return &pSchema[colIndex];
177
}
178

X
Xiaoyu Wang 已提交
179 180 181
SSchema* getTableTagSchema(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL && (pTableMeta->tableType == TSDB_SUPER_TABLE || pTableMeta->tableType == TSDB_CHILD_TABLE));
  return getOneColumnSchema(pTableMeta, getTableInfo(pTableMeta).numOfColumns);
182 183
}

X
Xiaoyu Wang 已提交
184 185 186 187
int32_t getNumOfColumns(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  // table created according to super table, use data from super table
  return getTableInfo(pTableMeta).numOfColumns;
188 189
}

X
Xiaoyu Wang 已提交
190 191 192
int32_t getNumOfTags(const STableMeta* pTableMeta) {
  assert(pTableMeta != NULL);
  return getTableInfo(pTableMeta).numOfTags;
193 194
}

X
Xiaoyu Wang 已提交
195
STableComInfo getTableInfo(const STableMeta* pTableMeta) {
196
  assert(pTableMeta != NULL);
X
Xiaoyu Wang 已提交
197
  return pTableMeta->tableInfo;
H
Haojun Liao 已提交
198
}
199 200

int32_t trimString(const char* src, int32_t len, char* dst, int32_t dlen) {
201 202
  if (len <=0 || dlen <= 0) return 0;

203 204 205 206
  char delim = src[0];
  int32_t j = 0;
  for (uint32_t k = 1; k < len - 1; ++k) {
    if (j >= dlen) {
207 208
      dst[j - 1] = '\0';
      return j;
209
    }
210
    if (src[k] == delim && src[k + 1] == delim) {   // deal with "", ''
211 212 213 214 215
      dst[j] = src[k + 1];
      j++;
      k++;
      continue;
    }
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

    if (src[k] == '\\') {   // deal with escape character
      if(src[k+1] == 'n'){
        dst[j] = '\n';
      }else if(src[k+1] == 'r'){
        dst[j] = '\r';
      }else if(src[k+1] == 't'){
        dst[j] = '\t';
      }else if(src[k+1] == '\\'){
        dst[j] = '\\';
      }else if(src[k+1] == '\''){
        dst[j] = '\'';
      }else if(src[k+1] == '"'){
        dst[j] = '"';
      }else if(src[k+1] == '%' || src[k+1] == '_'){
        dst[j++] = src[k];
        dst[j] = src[k+1];
      }else{
        dst[j] = src[k+1];
      }
      j++;
      k++;
      continue;
    }

241 242 243 244 245 246
    dst[j] = src[k];
    j++;
  }
  dst[j] = '\0';
  return j;
}
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

static bool isValidateTag(char *input) {
  if (!input) return false;
  for (size_t i = 0; i < strlen(input); ++i) {
    if (isprint(input[i]) == 0) return false;
  }
  return true;
}

int parseJsontoTagData(const char* json, SKVRowBuilder* kvRowBuilder, SMsgBuf* pMsgBuf, int16_t startColId){
  // set json NULL data
  uint8_t jsonKeyNULL = TSDB_JSON_KEY_NULL;
  uint8_t jsonNULL = TSDB_JSON_NULL;
  int jsonIndex = startColId + 1;
  tdAddColToKVRow(kvRowBuilder, jsonIndex++, &jsonKeyNULL, CHAR_BYTES);   // add json null type
  if (!json || strtrim(json) == 0 || strncasecmp(json, "null", 4) == 0){
    tdAddColToKVRow(kvRowBuilder, jsonIndex++, &jsonNULL, CHAR_BYTES);   // add json null value
    return TSDB_CODE_SUCCESS;
  }

  // set json real data
  cJSON *root = cJSON_Parse(json);
  if (root == NULL){
    return buildSyntaxErrMsg(pMsgBuf, "json parse error", json);
  }

  int size = cJSON_GetArraySize(root);
  if(!cJSON_IsObject(root)){
    return buildSyntaxErrMsg(pMsgBuf, "json error invalide value", json);
  }

  int retCode = 0;
  SHashObj* keyHash = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, false);
  for(int i = 0; i < size; i++) {
    cJSON* item = cJSON_GetArrayItem(root, i);
    if (!item) {
      qError("json inner error:%d", i);
      return buildSyntaxErrMsg(pMsgBuf, "json inner error", json);
      goto end;
    }

    char *jsonKey = item->string;
    if(!isValidateTag(jsonKey)){
      retCode =  buildSyntaxErrMsg(pMsgBuf, "json key not validate", jsonKey);
      goto end;
    }
//    if(strlen(jsonKey) > TSDB_MAX_JSON_KEY_LEN){
//      tscError("json key too long error");
//      retCode =  tscSQLSyntaxErrMsg(errMsg, "json key too long, more than 256", NULL);
//      goto end;
//    }
    size_t keyLen = strlen(jsonKey);
    if(keyLen == 0 || taosHashGet(keyHash, jsonKey, keyLen) != NULL){
      continue;
    }

    if(taosHashGetSize(keyHash) == 0){
      uint8_t jsonNotNULL = TSDB_JSON_NOT_NULL;
      tdAddColToKVRow(kvRowBuilder, jsonIndex++, &jsonNotNULL, CHAR_BYTES);   // add json type
    }
    // json key encode by binary
    void *tagKey = taosMemoryCalloc(keyLen + VARSTR_HEADER_SIZE, 1);
    if(!tagKey) {
      retCode = TSDB_CODE_TSC_OUT_OF_MEMORY;
      goto end;
    }
    strncpy(varDataVal(tagKey), jsonKey, keyLen);
    taosHashPut(keyHash, jsonKey, keyLen, &keyLen, CHAR_BYTES);  // add key to hash to remove dumplicate, value is useless

    varDataSetLen(tagKey, keyLen);
    tdAddColToKVRow(kvRowBuilder, jsonIndex++, tagKey, varDataTLen(tagKey));   // add json key
    taosMemoryFree(tagKey);

    if(item->type == cJSON_String){     // add json value  format: type|data
      char *jsonValue = item->valuestring;
      int32_t valLen = (int32_t)strlen(jsonValue);
      char *tagVal = taosMemoryCalloc(valLen * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE + CHAR_BYTES, 1);
      if(!tagVal) {
        retCode = TSDB_CODE_TSC_OUT_OF_MEMORY;
        goto end;
      } ;
      tagVal[0] = TSDB_DATA_TYPE_NCHAR;
      char* tagData = POINTER_SHIFT(tagVal, CHAR_BYTES);
      if (valLen > 0 && !taosMbsToUcs4(jsonValue, valLen, (TdUcs4*)varDataVal(tagData),
                                                  (int32_t)(valLen * TSDB_NCHAR_SIZE), &valLen)) {
        qError("charset:%s to %s. val:%s, errno:%s, convert failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, jsonValue, strerror(errno));
        retCode = buildSyntaxErrMsg(pMsgBuf, "charset convert json error", jsonValue);
        taosMemoryFree(tagVal);
        goto end;
      }

      varDataSetLen(tagData, valLen);
      tdAddColToKVRow(kvRowBuilder, jsonIndex++, tagVal, CHAR_BYTES + varDataTLen(tagData));
      taosMemoryFree(tagVal);
    }else if(item->type == cJSON_Number){
      if(!isfinite(item->valuedouble)){
        qError("json value is invalidate");
        retCode =  buildSyntaxErrMsg(pMsgBuf, "json value number is illegal", json);
        goto end;
      }
      char tagVal[LONG_BYTES + CHAR_BYTES] = {0};
      tagVal[0] = (item->valuedouble - (int64_t)(item->valuedouble) == 0) ? TSDB_DATA_TYPE_BIGINT
                                                                        : TSDB_DATA_TYPE_DOUBLE;
      char* tagData = POINTER_SHIFT(tagVal,CHAR_BYTES);
      if(tagVal[0]== TSDB_DATA_TYPE_DOUBLE) *((double *)tagData) = item->valuedouble;
      else if(tagVal[0] == TSDB_DATA_TYPE_BIGINT) *((int64_t *)tagData) = item->valueint;
      tdAddColToKVRow(kvRowBuilder, jsonIndex++, tagVal, LONG_BYTES + CHAR_BYTES);
    }else if(item->type == cJSON_True || item->type == cJSON_False){
      char tagVal[CHAR_BYTES + CHAR_BYTES] = {0};
      tagVal[0] = TSDB_DATA_TYPE_BOOL;
      tagVal[1] = (char)(item->valueint);
      tdAddColToKVRow(kvRowBuilder, jsonIndex++, tagVal, CHAR_BYTES + CHAR_BYTES);
    }else if(item->type == cJSON_NULL){
      char tagVal[CHAR_BYTES] = {TSDB_DATA_TYPE_NULL};
      tdAddColToKVRow(kvRowBuilder, jsonIndex++, tagVal, CHAR_BYTES);
    }
    else{
      retCode = buildSyntaxErrMsg(pMsgBuf, "invalidate json value", json);
      goto end;
    }
  }

  if(taosHashGetSize(keyHash) == 0){  // set json NULL true
    tdAddColToKVRow(kvRowBuilder, jsonIndex++, &jsonNULL, CHAR_BYTES);
  }

end:
  taosHashCleanup(keyHash);
  cJSON_Delete(root);
  return retCode;
}