tname.c 11.1 KB
Newer Older
H
hjxilinx 已提交
1 2 3
#include "os.h"
#include "tutil.h"

S
slguan 已提交
4
#include "tname.h"
H
hjxilinx 已提交
5
#include "tstoken.h"
6
#include "tvariant.h"
H
hjxilinx 已提交
7

8
#define VALIDNUMOFCOLS(x)  ((x) >= TSDB_MIN_COLUMNS && (x) <= TSDB_MAX_COLUMNS)
H
Haojun Liao 已提交
9 10
#define VALIDNUMOFTAGS(x)  ((x) >= 0 && (x) <= TSDB_MAX_TAGS)

11
#define VALID_NAME_TYPE(x)  ((x) == TSDB_DB_NAME_T || (x) == TSDB_TABLE_NAME_T)
H
hjxilinx 已提交
12

13
//TODO remove it
14
void extractTableName(const char* tableId, char* name) {
H
hjxilinx 已提交
15 16
  size_t s1 = strcspn(tableId, &TS_PATH_DELIMITER[0]);
  size_t s2 = strcspn(&tableId[s1 + 1], &TS_PATH_DELIMITER[0]);
H
hjxilinx 已提交
17
  
B
Bomin Zhang 已提交
18
  tstrncpy(name, &tableId[s1 + s2 + 2], TSDB_TABLE_NAME_LEN);
H
hjxilinx 已提交
19 20 21 22 23 24 25 26
}

char* extractDBName(const char* tableId, char* name) {
  size_t offset1 = strcspn(tableId, &TS_PATH_DELIMITER[0]);
  size_t len = strcspn(&tableId[offset1 + 1], &TS_PATH_DELIMITER[0]);
  
  return strncpy(name, &tableId[offset1 + 1], len);
}
H
Haojun Liao 已提交
27

28
// todo remove it
29
size_t tableIdPrefix(const char* name, char* prefix, int32_t len) {
30 31 32 33 34 35
  tstrncpy(prefix, name, len);
  strcat(prefix, TS_PATH_DELIMITER);

  return strlen(prefix);
}

Y
yihaoDeng 已提交
36 37 38 39 40 41 42 43
SSchema tGetBlockDistColumnSchema() {
  SSchema s = {0};
  s.bytes = TSDB_MAX_BINARY_LEN;;
  s.type  = TSDB_DATA_TYPE_BINARY;
  s.colId = TSDB_BLOCK_DIST_COLUMN_INDEX;
  tstrncpy(s.name, TSQL_BLOCK_DIST_L, TSDB_COL_NAME_LEN);
  return s;
}
H
Haojun Liao 已提交
44

H
Haojun Liao 已提交
45
SSchema tGetUserSpecifiedColumnSchema(tVariant* pVal, SStrToken* exprStr, const char* name) {
H
Haojun Liao 已提交
46 47
  SSchema s = {0};

48
  s.type  = pVal->nType;
H
Haojun Liao 已提交
49
  if (s.type == TSDB_DATA_TYPE_BINARY || s.type == TSDB_DATA_TYPE_NCHAR) {
50
    s.bytes = (int16_t)(pVal->nLen + VARSTR_HEADER_SIZE);
H
Haojun Liao 已提交
51
  } else {
H
Haojun Liao 已提交
52
    s.bytes = tDataTypes[pVal->nType].bytes;
H
Haojun Liao 已提交
53 54 55
  }

  s.colId = TSDB_UD_COLUMN_INDEX;
56 57 58
  if (name != NULL) {
    tstrncpy(s.name, name, sizeof(s.name));
  } else {
H
Haojun Liao 已提交
59
    size_t tlen = MIN(sizeof(s.name), exprStr->n + 1);
H
Haojun Liao 已提交
60
    tstrncpy(s.name, exprStr->z, tlen);
H
Haojun Liao 已提交
61
    strdequote(s.name);
62
  }
H
Haojun Liao 已提交
63

H
Haojun Liao 已提交
64 65 66 67 68
  return s;
}

bool tscValidateTableNameLength(size_t len) {
  return len < TSDB_TABLE_NAME_LEN;
H
Haojun Liao 已提交
69 70
}

71
SColumnFilterInfo* tFilterInfoDup(const SColumnFilterInfo* src, int32_t numOfFilters) {
H
Haojun Liao 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  if (numOfFilters == 0) {
    assert(src == NULL);
    return NULL;
  }

  SColumnFilterInfo* pFilter = calloc(1, numOfFilters * sizeof(SColumnFilterInfo));

  memcpy(pFilter, src, sizeof(SColumnFilterInfo) * numOfFilters);
  for (int32_t j = 0; j < numOfFilters; ++j) {

    if (pFilter[j].filterstr) {
      size_t len = (size_t) pFilter[j].len + 1 * TSDB_NCHAR_SIZE;
      pFilter[j].pz = (int64_t) calloc(1, len);

      memcpy((char*)pFilter[j].pz, (char*)src[j].pz, (size_t)len);
    }
  }

  assert(src->filterstr == 0 || src->filterstr == 1);
  assert(!(src->lowerRelOptr == TSDB_RELATION_INVALID && src->upperRelOptr == TSDB_RELATION_INVALID));

  return pFilter;
}
H
Haojun Liao 已提交
95

B
Bomin Zhang 已提交
96
#if 0
H
Haojun Liao 已提交
97 98 99 100
int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, int64_t intervalTime, char timeUnit, int16_t precision) {
  if (slidingTime == 0) {
    return startTime;
  }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  int64_t start = startTime;
  if (timeUnit == 'n' || timeUnit == 'y') {
    start /= 1000;
    if (precision == TSDB_TIME_PRECISION_MICRO) {
      start /= 1000;
    }
    struct tm tm;
    time_t t = (time_t)start;
    localtime_r(&t, &tm);
    tm.tm_sec = 0;
    tm.tm_min = 0;
    tm.tm_hour = 0;
    tm.tm_mday = 1;

    if (timeUnit == 'y') {
      tm.tm_mon = 0;
S
Shengliang Guan 已提交
117
      tm.tm_year = (int)(tm.tm_year / slidingTime * slidingTime);
118 119
    } else {
      int mon = tm.tm_year * 12 + tm.tm_mon;
S
Shengliang Guan 已提交
120
      mon = (int)(mon / slidingTime * slidingTime);
121 122 123
      tm.tm_year = mon / 12;
      tm.tm_mon = mon % 12;
    }
H
Haojun Liao 已提交
124

125 126 127 128 129
    start = mktime(&tm) * 1000L;
    if (precision == TSDB_TIME_PRECISION_MICRO) {
      start *= 1000L;
    }
  } else {
H
Haojun Liao 已提交
130 131 132 133
    int64_t delta = startTime - intervalTime;
    int32_t factor = delta > 0? 1:-1;

    start = (delta / slidingTime + factor) * slidingTime;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    if (timeUnit == 'd' || timeUnit == 'w') {
      /*
      * here we revised the start time of day according to the local time zone,
      * but in case of DST, the start time of one day need to be dynamically decided.
      */
      // todo refactor to extract function that is available for Linux/Windows/Mac platform
  #if defined(WINDOWS) && _MSC_VER >= 1900
      // see https://docs.microsoft.com/en-us/cpp/c-runtime-library/daylight-dstbias-timezone-and-tzname?view=vs-2019
      int64_t timezone = _timezone;
      int32_t daylight = _daylight;
      char**  tzname = _tzname;
  #endif

      int64_t t = (precision == TSDB_TIME_PRECISION_MILLI) ? MILLISECOND_PER_SECOND : MILLISECOND_PER_SECOND * 1000L;
      start += timezone * t;
    }
H
Haojun Liao 已提交
151

152 153 154 155
    int64_t end = start + intervalTime - 1;
    if (end < startTime) {
      start += slidingTime;
    }
H
Haojun Liao 已提交
156 157 158 159
  }

  return start;
}
H
Haojun Liao 已提交
160

B
Bomin Zhang 已提交
161 162
#endif

H
Haojun Liao 已提交
163 164 165 166
/*
 * tablePrefix.columnName
 * extract table name and save it in pTable, with only column name in pToken
 */
H
Haojun Liao 已提交
167
void extractTableNameFromToken(SStrToken* pToken, SStrToken* pTable) {
H
Haojun Liao 已提交
168 169 170 171 172 173 174 175 176
  const char sep = TS_PATH_DELIMITER[0];

  if (pToken == pTable || pToken == NULL || pTable == NULL) {
    return;
  }

  char* r = strnchr(pToken->z, sep, pToken->n, false);

  if (r != NULL) {  // record the table name token
177
    pTable->n = (uint32_t)(r - pToken->z);
H
Haojun Liao 已提交
178 179 180
    pTable->z = pToken->z;

    r += 1;
181
    pToken->n -= (uint32_t)(r - pToken->z);
H
Haojun Liao 已提交
182 183 184
    pToken->z = r;
  }
}
H
Haojun Liao 已提交
185

186 187 188 189 190 191
static struct SSchema _s = {
    .colId = TSDB_TBNAME_COLUMN_INDEX,
    .type  = TSDB_DATA_TYPE_BINARY,
    .bytes = TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE,
    .name = TSQL_TBNAME_L,
};
H
Haojun Liao 已提交
192

193 194
SSchema* tGetTbnameColumnSchema() {
  return &_s;
195
}
H
Haojun Liao 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

static bool doValidateSchema(SSchema* pSchema, int32_t numOfCols, int32_t maxLen) {
  int32_t rowLen = 0;

  for (int32_t i = 0; i < numOfCols; ++i) {
    // 1. valid types
    if (!isValidDataType(pSchema[i].type)) {
      return false;
    }

    // 2. valid length for each type
    if (pSchema[i].type == TSDB_DATA_TYPE_BINARY) {
      if (pSchema[i].bytes > TSDB_MAX_BINARY_LEN) {
        return false;
      }
    } else if (pSchema[i].type == TSDB_DATA_TYPE_NCHAR) {
      if (pSchema[i].bytes > TSDB_MAX_NCHAR_LEN) {
        return false;
      }
    } else {
      if (pSchema[i].bytes != tDataTypes[pSchema[i].type].bytes) {
        return false;
      }
    }

    // 3. valid column names
    for (int32_t j = i + 1; j < numOfCols; ++j) {
      if (strncasecmp(pSchema[i].name, pSchema[j].name, sizeof(pSchema[i].name) - 1) == 0) {
        return false;
      }
    }

    rowLen += pSchema[i].bytes;
  }

  return rowLen <= maxLen;
}

234
bool tIsValidSchema(struct SSchema* pSchema, int32_t numOfCols, int32_t numOfTags) {
H
Haojun Liao 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  if (!VALIDNUMOFCOLS(numOfCols)) {
    return false;
  }

  if (!VALIDNUMOFTAGS(numOfTags)) {
    return false;
  }

  /* first column must be the timestamp, which is a primary key */
  if (pSchema[0].type != TSDB_DATA_TYPE_TIMESTAMP) {
    return false;
  }

  if (!doValidateSchema(pSchema, numOfCols, TSDB_MAX_BYTES_PER_ROW)) {
    return false;
  }

  if (!doValidateSchema(&pSchema[numOfCols], numOfTags, TSDB_MAX_TAGS_LEN)) {
    return false;
  }

  return true;
}
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

int32_t tNameExtractFullName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);

  // invalid full name format, abort
  if (!tIsValidName(name)) {
    return -1;
  }

  int32_t len = snprintf(dst, TSDB_ACCT_ID_LEN + 1 + TSDB_DB_NAME_LEN, "%s.%s", name->acctId, name->dbname);

  size_t tnameLen = strlen(name->tname);
  if (tnameLen > 0) {
    assert(name->type == TSDB_TABLE_NAME_T);
    dst[len] = TS_PATH_DELIMITER[0];

    memcpy(dst + len + 1, name->tname, tnameLen);
    dst[len + tnameLen + 1] = 0;
  }

  return 0;
}

int32_t tNameLen(const SName* name) {
  assert(name != NULL);
  int32_t len  = (int32_t) strlen(name->acctId);
  int32_t len1 = (int32_t) strlen(name->dbname);
  int32_t len2 = (int32_t) strlen(name->tname);

  if (name->type == TSDB_DB_NAME_T) {
    assert(len2 == 0);
    return len + len1 + TS_PATH_DELIMITER_LEN;
  } else {
    assert(len2 > 0);
    return len + len1 + len2 + TS_PATH_DELIMITER_LEN * 2;
  }
}

bool tIsValidName(const SName* name) {
  assert(name != NULL);

  if (!VALID_NAME_TYPE(name->type)) {
    return false;
  }

  if (strlen(name->acctId) <= 0) {
    return false;
  }

  if (name->type == TSDB_DB_NAME_T) {
    return strlen(name->dbname) > 0;
  } else {
    return strlen(name->dbname) > 0 && strlen(name->tname) > 0;
  }
}

SName* tNameDup(const SName* name) {
  assert(name != NULL);

  SName* p = calloc(1, sizeof(SName));
  memcpy(p, name, sizeof(SName));
  return p;
}

int32_t tNameGetDbName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);
  strncpy(dst, name->dbname, tListLen(name->dbname));
  return 0;
}

int32_t tNameGetFullDbName(const SName* name, char* dst) {
  assert(name != NULL && dst != NULL);
  snprintf(dst, TSDB_ACCT_ID_LEN + TS_PATH_DELIMITER_LEN + TSDB_DB_NAME_LEN,
      "%s.%s", name->acctId, name->dbname);
  return 0;
}

bool tNameIsEmpty(const SName* name) {
  assert(name != NULL);
  return name->type == 0 || strlen(name->acctId) <= 0;
}

const char* tNameGetTableName(const SName* name) {
  assert(name != NULL && name->type == TSDB_TABLE_NAME_T);
  return &name->tname[0];
}

void tNameAssign(SName* dst, const SName* src) {
  memcpy(dst, src, sizeof(SName));
}

int32_t tNameSetDbName(SName* dst, const char* acct, SStrToken* dbToken) {
  assert(dst != NULL && dbToken != NULL && acct != NULL);

  // too long account id or too long db name
  if (strlen(acct) >= tListLen(dst->acctId) || dbToken->n >= tListLen(dst->dbname)) {
    return -1;
  }

  dst->type = TSDB_DB_NAME_T;
  tstrncpy(dst->acctId, acct, tListLen(dst->acctId));
  tstrncpy(dst->dbname, dbToken->z, dbToken->n + 1);
  return 0;
}

int32_t tNameSetAcctId(SName* dst, const char* acct) {
  assert(dst != NULL && acct != NULL);

  // too long account id or too long db name
  if (strlen(acct) >= tListLen(dst->acctId)) {
    return -1;
  }

  tstrncpy(dst->acctId, acct, tListLen(dst->acctId));
D
dapan1121 已提交
372 373 374

  assert(strlen(dst->acctId) > 0);
  
375 376 377 378 379 380 381 382 383 384 385 386 387
  return 0;
}

int32_t tNameFromString(SName* dst, const char* str, uint32_t type) {
  assert(dst != NULL && str != NULL && strlen(str) > 0);

  char* p = NULL;
  if ((type & T_NAME_ACCT) == T_NAME_ACCT) {
    p = strstr(str, TS_PATH_DELIMITER);
    if (p == NULL) {
      return -1;
    }

H
Haojun Liao 已提交
388
    int32_t len = (int32_t)(p - str);
389 390

    // too long account id or too long db name
D
dapan1121 已提交
391
    if ((len >= tListLen(dst->acctId)) || (len <= 0)) {
392 393 394 395 396
      return -1;
    }

    memcpy (dst->acctId, str, len);
    dst->acctId[len] = 0;
D
dapan1121 已提交
397 398

    assert(strlen(dst->acctId) > 0);
399 400 401 402 403 404 405 406 407
  }

  if ((type & T_NAME_DB) == T_NAME_DB) {
    dst->type = TSDB_DB_NAME_T;
    char* start = (char*)((p == NULL)? str:(p+1));

    int32_t len = 0;
    p = strstr(start, TS_PATH_DELIMITER);
    if (p == NULL) {
H
Haojun Liao 已提交
408
      len = (int32_t) strlen(start);
409
    } else {
H
Haojun Liao 已提交
410
      len = (int32_t) (p - start);
411 412 413
    }

    // too long account id or too long db name
D
dapan1121 已提交
414
    if ((len >= tListLen(dst->dbname)) || (len <= 0)) {
415 416 417 418 419 420 421 422 423 424 425
      return -1;
    }

    memcpy (dst->dbname, start, len);
    dst->dbname[len] = 0;
  }

  if ((type & T_NAME_TABLE) == T_NAME_TABLE) {
    dst->type = TSDB_TABLE_NAME_T;
    char* start = (char*) ((p == NULL)? str: (p+1));

H
Haojun Liao 已提交
426
    int32_t len = (int32_t) strlen(start);
427 428

    // too long account id or too long db name
D
dapan1121 已提交
429
    if ((len >= tListLen(dst->tname)) || (len <= 0)) {
430 431 432 433 434 435 436 437 438
      return -1;
    }

    memcpy (dst->tname, start, len);
    dst->tname[len] = 0;
  }

  return 0;
}