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

S
slguan 已提交
4
#include "tname.h"
5
#include "ttoken.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);
}

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

39
  s.type  = pVal->nType;
H
Haojun Liao 已提交
40
  if (s.type == TSDB_DATA_TYPE_BINARY || s.type == TSDB_DATA_TYPE_NCHAR) {
41
    s.bytes = (int16_t)(pVal->nLen + VARSTR_HEADER_SIZE);
H
Haojun Liao 已提交
42
  } else {
H
Haojun Liao 已提交
43
    s.bytes = tDataTypes[pVal->nType].bytes;
H
Haojun Liao 已提交
44 45 46
  }

  s.colId = TSDB_UD_COLUMN_INDEX;
47 48 49
  if (name != NULL) {
    tstrncpy(s.name, name, sizeof(s.name));
  } else {
H
Haojun Liao 已提交
50
    size_t tlen = MIN(sizeof(s.name), exprStr->n + 1);
H
Haojun Liao 已提交
51
    tstrncpy(s.name, exprStr->z, tlen);
H
Haojun Liao 已提交
52
    strdequote(s.name);
53
  }
H
Haojun Liao 已提交
54

H
Haojun Liao 已提交
55 56 57 58 59
  return s;
}

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

62
// TODO refactor
63
SColumnFilterInfo* tFilterInfoDup(const SColumnFilterInfo* src, int32_t numOfFilters) {
H
Haojun Liao 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  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 已提交
87

B
Bomin Zhang 已提交
88
#if 0
H
Haojun Liao 已提交
89 90 91 92
int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, int64_t intervalTime, char timeUnit, int16_t precision) {
  if (slidingTime == 0) {
    return startTime;
  }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  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 已提交
109
      tm.tm_year = (int)(tm.tm_year / slidingTime * slidingTime);
110 111
    } else {
      int mon = tm.tm_year * 12 + tm.tm_mon;
S
Shengliang Guan 已提交
112
      mon = (int)(mon / slidingTime * slidingTime);
113 114 115
      tm.tm_year = mon / 12;
      tm.tm_mon = mon % 12;
    }
H
Haojun Liao 已提交
116

117 118 119 120 121
    start = mktime(&tm) * 1000L;
    if (precision == TSDB_TIME_PRECISION_MICRO) {
      start *= 1000L;
    }
  } else {
H
Haojun Liao 已提交
122 123 124 125
    int64_t delta = startTime - intervalTime;
    int32_t factor = delta > 0? 1:-1;

    start = (delta / slidingTime + factor) * slidingTime;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    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 已提交
143

144 145 146 147
    int64_t end = start + intervalTime - 1;
    if (end < startTime) {
      start += slidingTime;
    }
H
Haojun Liao 已提交
148 149 150 151
  }

  return start;
}
H
Haojun Liao 已提交
152

B
Bomin Zhang 已提交
153 154
#endif

H
Haojun Liao 已提交
155 156 157 158
/*
 * tablePrefix.columnName
 * extract table name and save it in pTable, with only column name in pToken
 */
H
Haojun Liao 已提交
159
void extractTableNameFromToken(SStrToken* pToken, SStrToken* pTable) {
H
Haojun Liao 已提交
160 161 162 163 164 165 166 167 168
  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
169
    pTable->n = (uint32_t)(r - pToken->z);
H
Haojun Liao 已提交
170 171 172
    pTable->z = pToken->z;

    r += 1;
173
    pToken->n -= (uint32_t)(r - pToken->z);
H
Haojun Liao 已提交
174 175 176
    pToken->z = r;
  }
}
H
Haojun Liao 已提交
177

178 179 180 181 182 183
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 已提交
184

185 186
SSchema* tGetTbnameColumnSchema() {
  return &_s;
187
}
H
Haojun Liao 已提交
188 189 190 191 192 193 194 195 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

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

226
bool tIsValidSchema(struct SSchema* pSchema, int32_t numOfCols, int32_t numOfTags) {
H
Haojun Liao 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  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;
}
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

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 已提交
364 365 366

  assert(strlen(dst->acctId) > 0);
  
367 368 369 370 371 372 373 374 375 376 377 378 379
  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 已提交
380
    int32_t len = (int32_t)(p - str);
381 382

    // too long account id or too long db name
D
dapan1121 已提交
383
    if ((len >= tListLen(dst->acctId)) || (len <= 0)) {
384 385 386 387 388
      return -1;
    }

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

    assert(strlen(dst->acctId) > 0);
391 392 393 394 395 396 397 398 399
  }

  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 已提交
400
      len = (int32_t) strlen(start);
401
    } else {
H
Haojun Liao 已提交
402
      len = (int32_t) (p - start);
403 404 405
    }

    // too long account id or too long db name
D
dapan1121 已提交
406
    if ((len >= tListLen(dst->dbname)) || (len <= 0)) {
407 408 409 410 411 412 413 414 415 416 417
      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 已提交
418
    int32_t len = (int32_t) strlen(start);
419 420

    // too long account id or too long db name
D
dapan1121 已提交
421
    if ((len >= tListLen(dst->tname)) || (len <= 0)) {
422 423 424 425 426 427 428 429 430
      return -1;
    }

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

  return 0;
}