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

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

// todo refactor
10
UNUSED_FUNC static FORCE_INLINE const char* skipSegments(const char* input, char delim, int32_t num) {
H
hjxilinx 已提交
11 12 13 14 15 16 17
  for (int32_t i = 0; i < num; ++i) {
    while (*input != 0 && *input++ != delim) {
    };
  }
  return input;
}

18
UNUSED_FUNC static FORCE_INLINE size_t copy(char* dst, const char* src, char delimiter) {
H
hjxilinx 已提交
19 20 21 22 23 24 25 26 27
  size_t len = 0;
  while (*src != delimiter && *src != 0) {
    *dst++ = *src++;
    len++;
  }
  
  return len;
}

28
void extractTableName(const char* tableId, char* name) {
H
hjxilinx 已提交
29 30
  size_t s1 = strcspn(tableId, &TS_PATH_DELIMITER[0]);
  size_t s2 = strcspn(&tableId[s1 + 1], &TS_PATH_DELIMITER[0]);
H
hjxilinx 已提交
31
  
B
Bomin Zhang 已提交
32
  tstrncpy(name, &tableId[s1 + s2 + 2], TSDB_TABLE_NAME_LEN);
H
hjxilinx 已提交
33 34 35 36 37 38 39 40
}

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 已提交
41 42 43 44 45 46

SSchema tGetTableNameColumnSchema() {
  SSchema s = {0};
  s.bytes = TSDB_TABLE_NAME_LEN - 1 + VARSTR_HEADER_SIZE;
  s.type  = TSDB_DATA_TYPE_BINARY;
  s.colId = TSDB_TBNAME_COLUMN_INDEX;
H
Haojun Liao 已提交
47 48 49 50
  tstrncpy(s.name, TSQL_TBNAME_L, TSDB_COL_NAME_LEN);
  return s;
}

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

54
  s.type  = pVal->nType;
H
Haojun Liao 已提交
55
  if (s.type == TSDB_DATA_TYPE_BINARY || s.type == TSDB_DATA_TYPE_NCHAR) {
56
    s.bytes = (int16_t)(pVal->nLen + VARSTR_HEADER_SIZE);
H
Haojun Liao 已提交
57
  } else {
58
    s.bytes = tDataTypeDesc[pVal->nType].nSize;
H
Haojun Liao 已提交
59 60 61
  }

  s.colId = TSDB_UD_COLUMN_INDEX;
62 63 64
  if (name != NULL) {
    tstrncpy(s.name, name, sizeof(s.name));
  } else {
H
Haojun Liao 已提交
65
    size_t tlen = MIN(sizeof(s.name), exprStr->n + 1);
H
Haojun Liao 已提交
66
    tstrncpy(s.name, exprStr->z, tlen);
H
Haojun Liao 已提交
67
    strdequote(s.name);
68
  }
H
Haojun Liao 已提交
69

H
Haojun Liao 已提交
70 71 72 73 74
  return s;
}

bool tscValidateTableNameLength(size_t len) {
  return len < TSDB_TABLE_NAME_LEN;
H
Haojun Liao 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
}

SColumnFilterInfo* tscFilterInfoClone(const SColumnFilterInfo* src, int32_t numOfFilters) {
  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 已提交
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
int32_t taosCountNatualInterval(int64_t skey, int64_t ekey, int64_t intervalTime, char timeUnit, int16_t precision) {
  skey /= 1000;
  ekey /= 1000;
  if (precision == TSDB_TIME_PRECISION_MICRO) {
    skey /= 1000;
    ekey /= 1000;
  }
  if (ekey < skey) {
    int64_t tmp = ekey;
    ekey = skey;
    skey = tmp;
  }

  struct tm tm;
  time_t t = (time_t)skey;
  localtime_r(&t, &tm);
  int smon = tm.tm_year * 12 + tm.tm_mon;

  t = (time_t)ekey;
  localtime_r(&t, &tm);
  int emon = tm.tm_year * 12 + tm.tm_mon;

  if (timeUnit == 'y') {
    intervalTime *= 12;
  }

  return (emon - smon) / (int32_t)intervalTime;
}

H
Haojun Liao 已提交
131 132 133 134
int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, int64_t intervalTime, char timeUnit, int16_t precision) {
  if (slidingTime == 0) {
    return startTime;
  }
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  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 已提交
151
      tm.tm_year = (int)(tm.tm_year / slidingTime * slidingTime);
152 153
    } else {
      int mon = tm.tm_year * 12 + tm.tm_mon;
S
Shengliang Guan 已提交
154
      mon = (int)(mon / slidingTime * slidingTime);
155 156 157
      tm.tm_year = mon / 12;
      tm.tm_mon = mon % 12;
    }
H
Haojun Liao 已提交
158

159 160 161 162 163
    start = mktime(&tm) * 1000L;
    if (precision == TSDB_TIME_PRECISION_MICRO) {
      start *= 1000L;
    }
  } else {
H
Haojun Liao 已提交
164 165 166 167
    int64_t delta = startTime - intervalTime;
    int32_t factor = delta > 0? 1:-1;

    start = (delta / slidingTime + factor) * slidingTime;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

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

186 187 188 189
    int64_t end = start + intervalTime - 1;
    if (end < startTime) {
      start += slidingTime;
    }
H
Haojun Liao 已提交
190 191 192 193
  }

  return start;
}
H
Haojun Liao 已提交
194 195 196 197 198

/*
 * tablePrefix.columnName
 * extract table name and save it in pTable, with only column name in pToken
 */
H
Haojun Liao 已提交
199
void extractTableNameFromToken(SStrToken* pToken, SStrToken* pTable) {
H
Haojun Liao 已提交
200 201 202 203 204 205 206 207 208
  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
209
    pTable->n = (uint32_t)(r - pToken->z);
H
Haojun Liao 已提交
210 211 212
    pTable->z = pToken->z;

    r += 1;
213
    pToken->n -= (uint32_t)(r - pToken->z);
H
Haojun Liao 已提交
214 215 216
    pToken->z = r;
  }
}