tsdbUtil.c 42.5 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "tsdb.h"

H
Hongze Cheng 已提交
18
// SMapData =======================================================================
H
Hongze Cheng 已提交
19 20 21 22
void tMapDataReset(SMapData *pMapData) {
  pMapData->nItem = 0;
  pMapData->nData = 0;
}
H
Hongze Cheng 已提交
23

H
Hongze Cheng 已提交
24
void tMapDataClear(SMapData *pMapData) {
H
Hongze Cheng 已提交
25 26
  tFree((uint8_t *)pMapData->aOffset);
  tFree(pMapData->pData);
27 28
  pMapData->pData = NULL;
  pMapData->aOffset = NULL;
H
Hongze Cheng 已提交
29 30
}

H
Hongze Cheng 已提交
31 32 33 34 35 36 37 38 39
int32_t tMapDataPutItem(SMapData *pMapData, void *pItem, int32_t (*tPutItemFn)(uint8_t *, void *)) {
  int32_t code = 0;
  int32_t offset = pMapData->nData;
  int32_t nItem = pMapData->nItem;

  pMapData->nItem++;
  pMapData->nData += tPutItemFn(NULL, pItem);

  // alloc
H
Hongze Cheng 已提交
40
  code = tRealloc((uint8_t **)&pMapData->aOffset, sizeof(int32_t) * pMapData->nItem);
H
Hongze Cheng 已提交
41
  if (code) goto _exit;
H
Hongze Cheng 已提交
42
  code = tRealloc(&pMapData->pData, pMapData->nData);
H
Hongze Cheng 已提交
43
  if (code) goto _exit;
H
Hongze Cheng 已提交
44 45

  // put
H
Hongze Cheng 已提交
46
  pMapData->aOffset[nItem] = offset;
H
Hongze Cheng 已提交
47 48
  tPutItemFn(pMapData->pData + offset, pItem);

H
Hongze Cheng 已提交
49
_exit:
H
Hongze Cheng 已提交
50 51 52
  return code;
}

H
Hongze Cheng 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
int32_t tMapDataSearch(SMapData *pMapData, void *pSearchItem, int32_t (*tGetItemFn)(uint8_t *, void *),
                       int32_t (*tItemCmprFn)(const void *, const void *), void *pItem) {
  int32_t code = 0;
  int32_t lidx = 0;
  int32_t ridx = pMapData->nItem - 1;
  int32_t midx;
  int32_t c;

  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;

    tMapDataGetItemByIdx(pMapData, midx, pItem, tGetItemFn);

    c = tItemCmprFn(pSearchItem, pItem);
    if (c == 0) {
      goto _exit;
    } else if (c < 0) {
      ridx = midx - 1;
    } else {
      lidx = midx + 1;
    }
  }

  code = TSDB_CODE_NOT_FOUND;

_exit:
  return code;
}
H
Hongze Cheng 已提交
81

H
Hongze Cheng 已提交
82 83 84
void tMapDataGetItemByIdx(SMapData *pMapData, int32_t idx, void *pItem, int32_t (*tGetItemFn)(uint8_t *, void *)) {
  ASSERT(idx >= 0 && idx < pMapData->nItem);
  tGetItemFn(pMapData->pData + pMapData->aOffset[idx], pItem);
H
Hongze Cheng 已提交
85 86 87 88 89 90
}

int32_t tPutMapData(uint8_t *p, SMapData *pMapData) {
  int32_t n = 0;

  n += tPutI32v(p ? p + n : p, pMapData->nItem);
H
Hongze Cheng 已提交
91
  if (pMapData->nItem) {
H
Hongze Cheng 已提交
92
    int32_t lOffset = 0;
H
Hongze Cheng 已提交
93
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
H
Hongze Cheng 已提交
94 95
      n += tPutI32v(p ? p + n : p, pMapData->aOffset[iItem] - lOffset);
      lOffset = pMapData->aOffset[iItem];
H
Hongze Cheng 已提交
96
    }
H
Hongze Cheng 已提交
97 98 99 100 101 102

    n += tPutI32v(p ? p + n : p, pMapData->nData);
    if (p) {
      memcpy(p + n, pMapData->pData, pMapData->nData);
    }
    n += pMapData->nData;
H
Hongze Cheng 已提交
103 104 105 106 107 108 109
  }

  return n;
}

int32_t tGetMapData(uint8_t *p, SMapData *pMapData) {
  int32_t n = 0;
H
Hongze Cheng 已提交
110 111 112
  int32_t offset;

  tMapDataReset(pMapData);
H
Hongze Cheng 已提交
113 114

  n += tGetI32v(p + n, &pMapData->nItem);
H
Hongze Cheng 已提交
115
  if (pMapData->nItem) {
H
Hongze Cheng 已提交
116
    if (tRealloc((uint8_t **)&pMapData->aOffset, sizeof(int32_t) * pMapData->nItem)) return -1;
H
Hongze Cheng 已提交
117

H
Hongze Cheng 已提交
118
    int32_t lOffset = 0;
H
Hongze Cheng 已提交
119 120
    for (int32_t iItem = 0; iItem < pMapData->nItem; iItem++) {
      n += tGetI32v(p + n, &pMapData->aOffset[iItem]);
H
Hongze Cheng 已提交
121 122
      pMapData->aOffset[iItem] += lOffset;
      lOffset = pMapData->aOffset[iItem];
H
Hongze Cheng 已提交
123
    }
H
Hongze Cheng 已提交
124 125

    n += tGetI32v(p + n, &pMapData->nData);
H
Hongze Cheng 已提交
126
    if (tRealloc(&pMapData->pData, pMapData->nData)) return -1;
H
Hongze Cheng 已提交
127 128
    memcpy(pMapData->pData, p + n, pMapData->nData);
    n += pMapData->nData;
H
Hongze Cheng 已提交
129 130 131 132 133
  }

  return n;
}

H
Hongze Cheng 已提交
134
// TABLEID =======================================================================
H
Hongze Cheng 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
int32_t tTABLEIDCmprFn(const void *p1, const void *p2) {
  TABLEID *pId1 = (TABLEID *)p1;
  TABLEID *pId2 = (TABLEID *)p2;

  if (pId1->suid < pId2->suid) {
    return -1;
  } else if (pId1->suid > pId2->suid) {
    return 1;
  }

  if (pId1->uid < pId2->uid) {
    return -1;
  } else if (pId1->uid > pId2->uid) {
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
154
// TSDBKEY =======================================================================
H
Hongze Cheng 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
int32_t tsdbKeyCmprFn(const void *p1, const void *p2) {
  TSDBKEY *pKey1 = (TSDBKEY *)p1;
  TSDBKEY *pKey2 = (TSDBKEY *)p2;

  if (pKey1->ts < pKey2->ts) {
    return -1;
  } else if (pKey1->ts > pKey2->ts) {
    return 1;
  }

  if (pKey1->version < pKey2->version) {
    return -1;
  } else if (pKey1->version > pKey2->version) {
    return 1;
  }

  return 0;
H
Hongze Cheng 已提交
172 173
}

H
Hongze Cheng 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
// TSDBKEY ======================================================
static FORCE_INLINE int32_t tPutTSDBKEY(uint8_t *p, TSDBKEY *pKey) {
  int32_t n = 0;

  n += tPutI64v(p ? p + n : p, pKey->version);
  n += tPutI64(p ? p + n : p, pKey->ts);

  return n;
}

static FORCE_INLINE int32_t tGetTSDBKEY(uint8_t *p, TSDBKEY *pKey) {
  int32_t n = 0;

  n += tGetI64v(p + n, &pKey->version);
  n += tGetI64(p + n, &pKey->ts);

  return n;
}

// SBlockIdx ======================================================
H
Hongze Cheng 已提交
194 195 196 197 198 199 200 201
int32_t tPutBlockIdx(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockIdx *pBlockIdx = (SBlockIdx *)ph;

  n += tPutI64(p ? p + n : p, pBlockIdx->suid);
  n += tPutI64(p ? p + n : p, pBlockIdx->uid);
  n += tPutI64v(p ? p + n : p, pBlockIdx->offset);
  n += tPutI64v(p ? p + n : p, pBlockIdx->size);
H
Hongze Cheng 已提交
202 203 204 205

  return n;
}

H
Hongze Cheng 已提交
206 207 208
int32_t tGetBlockIdx(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockIdx *pBlockIdx = (SBlockIdx *)ph;
H
Hongze Cheng 已提交
209

H
Hongze Cheng 已提交
210 211 212 213
  n += tGetI64(p + n, &pBlockIdx->suid);
  n += tGetI64(p + n, &pBlockIdx->uid);
  n += tGetI64v(p + n, &pBlockIdx->offset);
  n += tGetI64v(p + n, &pBlockIdx->size);
H
Hongze Cheng 已提交
214 215 216 217

  return n;
}

218
int32_t tCmprBlockIdx(void const *lhs, void const *rhs) {
M
Minglei Jin 已提交
219 220
  SBlockIdx *lBlockIdx = (SBlockIdx *)lhs;
  SBlockIdx *rBlockIdx = (SBlockIdx *)rhs;
221

M
Minglei Jin 已提交
222
  if (lBlockIdx->suid < rBlockIdx->suid) {
223
    return -1;
M
Minglei Jin 已提交
224
  } else if (lBlockIdx->suid > rBlockIdx->suid) {
225 226 227
    return 1;
  }

M
Minglei Jin 已提交
228
  if (lBlockIdx->uid < rBlockIdx->uid) {
229
    return -1;
M
Minglei Jin 已提交
230
  } else if (lBlockIdx->uid > rBlockIdx->uid) {
231 232 233 234 235 236
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
237
// SBlock ======================================================
H
Hongze Cheng 已提交
238
void tBlockReset(SBlock *pBlock) {
H
Hongze Cheng 已提交
239 240
  *pBlock =
      (SBlock){.minKey = TSDBKEY_MAX, .maxKey = TSDBKEY_MIN, .minVersion = VERSION_MAX, .maxVersion = VERSION_MIN};
H
Hongze Cheng 已提交
241 242
}

H
Hongze Cheng 已提交
243 244
int32_t tPutBlock(uint8_t *p, void *ph) {
  int32_t n = 0;
H
Hongze Cheng 已提交
245
  SBlock *pBlock = (SBlock *)ph;
H
Hongze Cheng 已提交
246

H
Hongze Cheng 已提交
247 248 249 250
  n += tPutTSDBKEY(p ? p + n : p, &pBlock->minKey);
  n += tPutTSDBKEY(p ? p + n : p, &pBlock->maxKey);
  n += tPutI64v(p ? p + n : p, pBlock->minVersion);
  n += tPutI64v(p ? p + n : p, pBlock->maxVersion);
H
Hongze Cheng 已提交
251 252 253 254 255
  n += tPutI32v(p ? p + n : p, pBlock->nRow);
  n += tPutI8(p ? p + n : p, pBlock->last);
  n += tPutI8(p ? p + n : p, pBlock->hasDup);
  n += tPutI8(p ? p + n : p, pBlock->nSubBlock);
  for (int8_t iSubBlock = 0; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
256
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].nRow);
H
Hongze Cheng 已提交
257
    n += tPutI8(p ? p + n : p, pBlock->aSubBlock[iSubBlock].cmprAlg);
H
Hongze Cheng 已提交
258
    n += tPutI64v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].offset);
H
Hongze Cheng 已提交
259 260 261 262 263 264
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].szBlockCol);
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].szVersion);
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].szTSKEY);
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].szBlock);
    n += tPutI64v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].sOffset);
    n += tPutI32v(p ? p + n : p, pBlock->aSubBlock[iSubBlock].nSma);
H
Hongze Cheng 已提交
265 266
  }

H
Hongze Cheng 已提交
267 268 269 270 271
  return n;
}

int32_t tGetBlock(uint8_t *p, void *ph) {
  int32_t n = 0;
H
Hongze Cheng 已提交
272
  SBlock *pBlock = (SBlock *)ph;
H
Hongze Cheng 已提交
273

H
Hongze Cheng 已提交
274 275 276 277
  n += tGetTSDBKEY(p + n, &pBlock->minKey);
  n += tGetTSDBKEY(p + n, &pBlock->maxKey);
  n += tGetI64v(p + n, &pBlock->minVersion);
  n += tGetI64v(p + n, &pBlock->maxVersion);
H
Hongze Cheng 已提交
278 279 280 281 282
  n += tGetI32v(p + n, &pBlock->nRow);
  n += tGetI8(p + n, &pBlock->last);
  n += tGetI8(p + n, &pBlock->hasDup);
  n += tGetI8(p + n, &pBlock->nSubBlock);
  for (int8_t iSubBlock = 0; iSubBlock < pBlock->nSubBlock; iSubBlock++) {
H
Hongze Cheng 已提交
283
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].nRow);
H
Hongze Cheng 已提交
284
    n += tGetI8(p + n, &pBlock->aSubBlock[iSubBlock].cmprAlg);
H
Hongze Cheng 已提交
285
    n += tGetI64v(p + n, &pBlock->aSubBlock[iSubBlock].offset);
H
Hongze Cheng 已提交
286 287 288 289 290 291
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].szBlockCol);
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].szVersion);
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].szTSKEY);
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].szBlock);
    n += tGetI64v(p + n, &pBlock->aSubBlock[iSubBlock].sOffset);
    n += tGetI32v(p + n, &pBlock->aSubBlock[iSubBlock].nSma);
H
Hongze Cheng 已提交
292 293
  }

H
Hongze Cheng 已提交
294 295 296
  return n;
}

H
Hongze Cheng 已提交
297 298 299 300
int32_t tBlockCmprFn(const void *p1, const void *p2) {
  SBlock *pBlock1 = (SBlock *)p1;
  SBlock *pBlock2 = (SBlock *)p2;

H
Hongze Cheng 已提交
301
  if (tsdbKeyCmprFn(&pBlock1->maxKey, &pBlock2->minKey) < 0) {
H
Hongze Cheng 已提交
302
    return -1;
H
Hongze Cheng 已提交
303
  } else if (tsdbKeyCmprFn(&pBlock1->minKey, &pBlock2->maxKey) > 0) {
H
Hongze Cheng 已提交
304
    return 1;
H
Hongze Cheng 已提交
305 306 307 308 309
  }

  return 0;
}

H
Hongze Cheng 已提交
310 311 312 313 314 315 316 317
bool tBlockHasSma(SBlock *pBlock) {
  if (pBlock->nSubBlock > 1) return false;
  if (pBlock->last) return false;
  if (pBlock->hasDup) return false;

  return pBlock->aSubBlock[0].nSma > 0;
}

H
Hongze Cheng 已提交
318 319 320 321 322 323 324 325 326
// SBlockCol ======================================================
int32_t tPutBlockCol(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockCol *pBlockCol = (SBlockCol *)ph;

  ASSERT(pBlockCol->flag && (pBlockCol->flag != HAS_NONE));

  n += tPutI16v(p ? p + n : p, pBlockCol->cid);
  n += tPutI8(p ? p + n : p, pBlockCol->type);
H
Hongze Cheng 已提交
327
  n += tPutI8(p ? p + n : p, pBlockCol->smaOn);
H
Hongze Cheng 已提交
328 329 330
  n += tPutI8(p ? p + n : p, pBlockCol->flag);

  if (pBlockCol->flag != HAS_NULL) {
H
Hongze Cheng 已提交
331 332 333 334 335
    n += tPutI32v(p ? p + n : p, pBlockCol->offset);
    n += tPutI32v(p ? p + n : p, pBlockCol->szBitmap);
    n += tPutI32v(p ? p + n : p, pBlockCol->szOffset);
    n += tPutI32v(p ? p + n : p, pBlockCol->szValue);
    n += tPutI32v(p ? p + n : p, pBlockCol->szOrigin);
H
Hongze Cheng 已提交
336 337 338 339 340 341 342 343 344 345 346
  }

  return n;
}

int32_t tGetBlockCol(uint8_t *p, void *ph) {
  int32_t    n = 0;
  SBlockCol *pBlockCol = (SBlockCol *)ph;

  n += tGetI16v(p + n, &pBlockCol->cid);
  n += tGetI8(p + n, &pBlockCol->type);
H
Hongze Cheng 已提交
347
  n += tGetI8(p + n, &pBlockCol->smaOn);
H
Hongze Cheng 已提交
348 349 350 351 352
  n += tGetI8(p + n, &pBlockCol->flag);

  ASSERT(pBlockCol->flag && (pBlockCol->flag != HAS_NONE));

  if (pBlockCol->flag != HAS_NULL) {
H
Hongze Cheng 已提交
353 354 355 356 357
    n += tGetI32v(p + n, &pBlockCol->offset);
    n += tGetI32v(p + n, &pBlockCol->szBitmap);
    n += tGetI32v(p + n, &pBlockCol->szOffset);
    n += tGetI32v(p + n, &pBlockCol->szValue);
    n += tGetI32v(p + n, &pBlockCol->szOrigin);
H
Hongze Cheng 已提交
358 359 360 361 362
  }

  return n;
}

H
Hongze Cheng 已提交
363 364 365 366 367 368 369 370 371 372
int32_t tBlockColCmprFn(const void *p1, const void *p2) {
  if (((SBlockCol *)p1)->cid < ((SBlockCol *)p2)->cid) {
    return -1;
  } else if (((SBlockCol *)p1)->cid > ((SBlockCol *)p2)->cid) {
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
373
// SDelIdx ======================================================
374
int32_t tCmprDelIdx(void const *lhs, void const *rhs) {
M
Minglei Jin 已提交
375 376
  SDelIdx *lDelIdx = (SDelIdx *)lhs;
  SDelIdx *rDelIdx = (SDelIdx *)rhs;
377

M
Minglei Jin 已提交
378
  if (lDelIdx->suid < rDelIdx->suid) {
379
    return -1;
M
Minglei Jin 已提交
380
  } else if (lDelIdx->suid > rDelIdx->suid) {
381 382 383
    return 1;
  }

M
Minglei Jin 已提交
384
  if (lDelIdx->uid < rDelIdx->uid) {
385
    return -1;
M
Minglei Jin 已提交
386
  } else if (lDelIdx->uid > rDelIdx->uid) {
387 388 389 390 391 392
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
393 394 395 396 397 398 399 400
int32_t tPutDelIdx(uint8_t *p, void *ph) {
  SDelIdx *pDelIdx = (SDelIdx *)ph;
  int32_t  n = 0;

  n += tPutI64(p ? p + n : p, pDelIdx->suid);
  n += tPutI64(p ? p + n : p, pDelIdx->uid);
  n += tPutI64v(p ? p + n : p, pDelIdx->offset);
  n += tPutI64v(p ? p + n : p, pDelIdx->size);
H
Hongze Cheng 已提交
401 402 403 404

  return n;
}

H
Hongze Cheng 已提交
405 406 407
int32_t tGetDelIdx(uint8_t *p, void *ph) {
  SDelIdx *pDelIdx = (SDelIdx *)ph;
  int32_t  n = 0;
H
Hongze Cheng 已提交
408

H
Hongze Cheng 已提交
409 410 411 412
  n += tGetI64(p + n, &pDelIdx->suid);
  n += tGetI64(p + n, &pDelIdx->uid);
  n += tGetI64v(p + n, &pDelIdx->offset);
  n += tGetI64v(p + n, &pDelIdx->size);
H
Hongze Cheng 已提交
413 414 415 416

  return n;
}

H
Hongze Cheng 已提交
417
// SDelData ======================================================
H
Hongze Cheng 已提交
418 419 420 421 422 423 424
int32_t tPutDelData(uint8_t *p, void *ph) {
  SDelData *pDelData = (SDelData *)ph;
  int32_t   n = 0;

  n += tPutI64v(p ? p + n : p, pDelData->version);
  n += tPutI64(p ? p + n : p, pDelData->sKey);
  n += tPutI64(p ? p + n : p, pDelData->eKey);
H
Hongze Cheng 已提交
425 426 427 428

  return n;
}

H
Hongze Cheng 已提交
429 430 431
int32_t tGetDelData(uint8_t *p, void *ph) {
  SDelData *pDelData = (SDelData *)ph;
  int32_t   n = 0;
H
Hongze Cheng 已提交
432

H
Hongze Cheng 已提交
433 434 435
  n += tGetI64v(p + n, &pDelData->version);
  n += tGetI64(p + n, &pDelData->sKey);
  n += tGetI64(p + n, &pDelData->eKey);
H
Hongze Cheng 已提交
436 437

  return n;
H
Hongze Cheng 已提交
438 439
}

H
Hongze Cheng 已提交
440 441 442 443 444 445
int32_t tsdbKeyFid(TSKEY key, int32_t minutes, int8_t precision) {
  if (key < 0) {
    return (int)((key + 1) / tsTickPerMin[precision] / minutes - 1);
  } else {
    return (int)((key / tsTickPerMin[precision] / minutes));
  }
H
Hongze Cheng 已提交
446 447
}

H
Hongze Cheng 已提交
448 449 450 451 452
void tsdbFidKeyRange(int32_t fid, int32_t minutes, int8_t precision, TSKEY *minKey, TSKEY *maxKey) {
  *minKey = fid * minutes * tsTickPerMin[precision];
  *maxKey = *minKey + minutes * tsTickPerMin[precision] - 1;
}

H
Hongze Cheng 已提交
453 454 455 456
int32_t tsdbFidLevel(int32_t fid, STsdbKeepCfg *pKeepCfg, int64_t now) {
  int32_t aFid[3];
  TSKEY   key;

H
Hongze Cheng 已提交
457 458 459 460 461 462 463 464 465 466
  if (pKeepCfg->precision == TSDB_TIME_PRECISION_MILLI) {
    now = now * 1000;
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_MICRO) {
    now = now * 1000000l;
  } else if (pKeepCfg->precision == TSDB_TIME_PRECISION_NANO) {
    now = now * 1000000000l;
  } else {
    ASSERT(0);
  }

H
Hongze Cheng 已提交
467
  key = now - pKeepCfg->keep0 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
468
  aFid[0] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
469
  key = now - pKeepCfg->keep1 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
470
  aFid[1] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
471
  key = now - pKeepCfg->keep2 * tsTickPerMin[pKeepCfg->precision];
H
Hongze Cheng 已提交
472
  aFid[2] = tsdbKeyFid(key, pKeepCfg->days, pKeepCfg->precision);
H
Hongze Cheng 已提交
473 474 475 476 477 478 479 480 481 482 483

  if (fid >= aFid[0]) {
    return 0;
  } else if (fid >= aFid[1]) {
    return 1;
  } else if (fid >= aFid[2]) {
    return 2;
  } else {
    return -1;
  }
}
H
Hongze Cheng 已提交
484

H
Hongze Cheng 已提交
485 486
// TSDBROW ======================================================
void tsdbRowGetColVal(TSDBROW *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
487 488 489
  STColumn *pTColumn = &pTSchema->columns[iCol];
  SValue    value;

H
Hongze Cheng 已提交
490 491
  ASSERT(iCol > 0);

H
Hongze Cheng 已提交
492
  if (pRow->type == 0) {
H
Hongze Cheng 已提交
493
    tTSRowGetVal(pRow->pTSRow, pTSchema, iCol, pColVal);
H
Hongze Cheng 已提交
494
  } else if (pRow->type == 1) {
H
Hongze Cheng 已提交
495
    SColData *pColData;
H
Hongze Cheng 已提交
496

H
Hongze Cheng 已提交
497
    tBlockDataGetColData(pRow->pBlockData, pTColumn->colId, &pColData);
H
Hongze Cheng 已提交
498

H
Hongze Cheng 已提交
499
    if (pColData) {
H
Hongze Cheng 已提交
500
      tColDataGetValue(pColData, pRow->iRow, pColVal);
H
Hongze Cheng 已提交
501
    } else {
H
Hongze Cheng 已提交
502
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
503 504 505 506
    }
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
507 508
}

H
Hongze Cheng 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow) {
  int32_t n = 0;

  n += tPutI64(p, pRow->version);
  if (p) memcpy(p + n, pRow->pTSRow, pRow->pTSRow->len);
  n += pRow->pTSRow->len;

  return n;
}

int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow) {
  int32_t n = 0;

  n += tGetI64(p, &pRow->version);
  pRow->pTSRow = (STSRow *)(p + n);
  n += pRow->pTSRow->len;

  return n;
}

H
Hongze Cheng 已提交
529 530 531 532
int32_t tsdbRowCmprFn(const void *p1, const void *p2) {
  return tsdbKeyCmprFn(&TSDBROW_KEY((TSDBROW *)p1), &TSDBROW_KEY((TSDBROW *)p2));
}

H
Hongze Cheng 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
// SRowIter ======================================================
void tRowIterInit(SRowIter *pIter, TSDBROW *pRow, STSchema *pTSchema) {
  pIter->pRow = pRow;
  if (pRow->type == 0) {
    ASSERT(pTSchema);
    pIter->pTSchema = pTSchema;
    pIter->i = 1;
  } else if (pRow->type == 1) {
    pIter->pTSchema = NULL;
    pIter->i = 0;
  } else {
    ASSERT(0);
  }
}

SColVal *tRowIterNext(SRowIter *pIter) {
  if (pIter->pRow->type == 0) {
    if (pIter->i < pIter->pTSchema->numOfCols) {
      tsdbRowGetColVal(pIter->pRow, pIter->pTSchema, pIter->i, &pIter->colVal);
      pIter->i++;

      return &pIter->colVal;
    }
  } else {
H
Hongze Cheng 已提交
557 558
    if (pIter->i < taosArrayGetSize(pIter->pRow->pBlockData->aIdx)) {
      SColData *pColData = tBlockDataGetColDataByIdx(pIter->pRow->pBlockData, pIter->i);
H
Hongze Cheng 已提交
559 560 561 562 563 564 565 566 567 568 569

      tColDataGetValue(pColData, pIter->pRow->iRow, &pIter->colVal);
      pIter->i++;

      return &pIter->colVal;
    }
  }

  return NULL;
}

H
Hongze Cheng 已提交
570 571 572
// SRowMerger ======================================================
int32_t tRowMergerInit(SRowMerger *pMerger, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t   code = 0;
H
Hongze Cheng 已提交
573
  TSDBKEY   key = TSDBROW_KEY(pRow);
H
Hongze Cheng 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
  SColVal  *pColVal = &(SColVal){0};
  STColumn *pTColumn;

  pMerger->pTSchema = pTSchema;
  pMerger->version = key.version;

  pMerger->pArray = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
  if (pMerger->pArray == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // ts
  pTColumn = &pTSchema->columns[0];

  ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);

  *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, (SValue){.ts = key.ts});
  if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  // other
  for (int16_t iCol = 1; iCol < pTSchema->numOfCols; iCol++) {
    tsdbRowGetColVal(pRow, pTSchema, iCol, pColVal);
    if (taosArrayPush(pMerger->pArray, pColVal) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
  }

_exit:
  return code;
}

void tRowMergerClear(SRowMerger *pMerger) { taosArrayDestroy(pMerger->pArray); }

int32_t tRowMerge(SRowMerger *pMerger, TSDBROW *pRow) {
  int32_t  code = 0;
H
Hongze Cheng 已提交
614
  TSDBKEY  key = TSDBROW_KEY(pRow);
H
Hongze Cheng 已提交
615 616 617 618 619 620 621 622
  SColVal *pColVal = &(SColVal){0};

  ASSERT(((SColVal *)pMerger->pArray->pData)->value.ts == key.ts);

  for (int32_t iCol = 1; iCol < pMerger->pTSchema->numOfCols; iCol++) {
    tsdbRowGetColVal(pRow, pMerger->pTSchema, iCol, pColVal);

    if (key.version > pMerger->version) {
H
Hongze Cheng 已提交
623 624 625
      if (!pColVal->isNone) {
        taosArraySet(pMerger->pArray, iCol, pColVal);
      }
H
Hongze Cheng 已提交
626
    } else if (key.version < pMerger->version) {
H
Hongze Cheng 已提交
627 628 629 630
      SColVal *tColVal = (SColVal *)taosArrayGet(pMerger->pArray, iCol);
      if (tColVal->isNone && !pColVal->isNone) {
        taosArraySet(pMerger->pArray, iCol, pColVal);
      }
H
Hongze Cheng 已提交
631 632 633 634 635 636 637 638 639 640 641 642 643
    } else {
      ASSERT(0);
    }
  }

  pMerger->version = key.version;

_exit:
  return code;
}

int32_t tRowMergerGetRow(SRowMerger *pMerger, STSRow **ppRow) {
  int32_t code = 0;
644 645 646

  code = tdSTSRowNew(pMerger->pArray, pMerger->pTSchema, ppRow);

H
Hongze Cheng 已提交
647 648 649
  return code;
}

H
Hongze Cheng 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
// delete skyline ======================================================
static int32_t tsdbMergeSkyline(SArray *aSkyline1, SArray *aSkyline2, SArray *aSkyline) {
  int32_t  code = 0;
  int32_t  i1 = 0;
  int32_t  n1 = taosArrayGetSize(aSkyline1);
  int32_t  i2 = 0;
  int32_t  n2 = taosArrayGetSize(aSkyline2);
  TSDBKEY *pSkyline1;
  TSDBKEY *pSkyline2;
  TSDBKEY  item;
  int64_t  version1 = 0;
  int64_t  version2 = 0;

  ASSERT(n1 > 0 && n2 > 0);

  taosArrayClear(aSkyline);

  while (i1 < n1 && i2 < n2) {
    pSkyline1 = (TSDBKEY *)taosArrayGet(aSkyline1, i1);
    pSkyline2 = (TSDBKEY *)taosArrayGet(aSkyline2, i2);

    if (pSkyline1->ts < pSkyline2->ts) {
      version1 = pSkyline1->version;
      i1++;
    } else if (pSkyline1->ts > pSkyline2->ts) {
      version2 = pSkyline2->version;
      i2++;
    } else {
      version1 = pSkyline1->version;
      version2 = pSkyline2->version;
      i1++;
      i2++;
    }

    item.ts = TMIN(pSkyline1->ts, pSkyline2->ts);
    item.version = TMAX(version1, version2);
    if (taosArrayPush(aSkyline, &item) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
  }

  while (i1 < n1) {
    pSkyline1 = (TSDBKEY *)taosArrayGet(aSkyline1, i1);
    item.ts = pSkyline1->ts;
    item.version = pSkyline1->version;
    if (taosArrayPush(aSkyline, &item) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    i1++;
  }

  while (i2 < n2) {
    pSkyline2 = (TSDBKEY *)taosArrayGet(aSkyline2, i2);
    item.ts = pSkyline2->ts;
    item.version = pSkyline2->version;
    if (taosArrayPush(aSkyline, &item) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    i2++;
  }

_exit:
  return code;
}
int32_t tsdbBuildDeleteSkyline(SArray *aDelData, int32_t sidx, int32_t eidx, SArray *aSkyline) {
  int32_t   code = 0;
  SDelData *pDelData;
  int32_t   midx;

  taosArrayClear(aSkyline);
  if (sidx == eidx) {
    pDelData = (SDelData *)taosArrayGet(aDelData, sidx);
    taosArrayPush(aSkyline, &(TSDBKEY){.ts = pDelData->sKey, .version = pDelData->version});
    taosArrayPush(aSkyline, &(TSDBKEY){.ts = pDelData->eKey, .version = 0});
  } else {
    SArray *aSkyline1 = NULL;
    SArray *aSkyline2 = NULL;

    aSkyline1 = taosArrayInit(0, sizeof(TSDBKEY));
    aSkyline2 = taosArrayInit(0, sizeof(TSDBKEY));
    if (aSkyline1 == NULL || aSkyline2 == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _clear;
    }

    midx = (sidx + eidx) / 2;

    code = tsdbBuildDeleteSkyline(aDelData, sidx, midx, aSkyline1);
    if (code) goto _clear;

    code = tsdbBuildDeleteSkyline(aDelData, midx + 1, eidx, aSkyline2);
    if (code) goto _clear;

    code = tsdbMergeSkyline(aSkyline1, aSkyline2, aSkyline);

  _clear:
    taosArrayDestroy(aSkyline1);
    taosArrayDestroy(aSkyline2);
  }

H
Hongze Cheng 已提交
753 754 755
  return code;
}

H
Hongze Cheng 已提交
756
// SColData ========================================
H
Hongze Cheng 已提交
757
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t smaOn) {
H
Hongze Cheng 已提交
758 759
  pColData->cid = cid;
  pColData->type = type;
H
Hongze Cheng 已提交
760
  pColData->smaOn = smaOn;
H
Hongze Cheng 已提交
761 762 763 764
  tColDataReset(pColData);
}

void tColDataReset(SColData *pColData) {
H
Hongze Cheng 已提交
765 766
  pColData->nVal = 0;
  pColData->flag = 0;
H
Hongze Cheng 已提交
767
  pColData->nData = 0;
H
Hongze Cheng 已提交
768 769
}

H
Hongze Cheng 已提交
770 771 772
void tColDataClear(void *ph) {
  SColData *pColData = (SColData *)ph;

H
Hongze Cheng 已提交
773 774 775
  tFree(pColData->pBitMap);
  tFree((uint8_t *)pColData->aOffset);
  tFree(pColData->pData);
H
Hongze Cheng 已提交
776 777 778 779
}

int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;
H
Hongze Cheng 已提交
780 781 782
  int64_t size;
  SValue  value = {0};
  SValue *pValue = &value;
H
Hongze Cheng 已提交
783

H
Hongze Cheng 已提交
784 785 786 787 788
  ASSERT(pColVal->cid == pColData->cid);
  ASSERT(pColVal->type == pColData->type);

  // realloc bitmap
  size = BIT2_SIZE(pColData->nVal + 1);
H
Hongze Cheng 已提交
789
  code = tRealloc(&pColData->pBitMap, size);
H
Hongze Cheng 已提交
790 791 792
  if (code) goto _exit;

  // put value
H
Hongze Cheng 已提交
793
  if (pColVal->isNone) {
H
Hongze Cheng 已提交
794 795
    pColData->flag |= HAS_NONE;
    SET_BIT2(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
796
  } else if (pColVal->isNull) {
H
Hongze Cheng 已提交
797 798
    pColData->flag |= HAS_NULL;
    SET_BIT2(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
799
  } else {
H
Hongze Cheng 已提交
800 801 802 803 804
    pColData->flag |= HAS_VALUE;
    SET_BIT2(pColData->pBitMap, pColData->nVal, 2);
    pValue = &pColVal->value;
  }

H
Hongze Cheng 已提交
805 806
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    // offset
H
Hongze Cheng 已提交
807
    code = tRealloc((uint8_t **)&pColData->aOffset, sizeof(int32_t) * (pColData->nVal + 1));
H
Hongze Cheng 已提交
808
    if (code) goto _exit;
H
Hongze Cheng 已提交
809
    pColData->aOffset[pColData->nVal] = pColData->nData;
H
Hongze Cheng 已提交
810

H
Hongze Cheng 已提交
811 812
    // value
    if ((!pColVal->isNone) && (!pColVal->isNull)) {
H
Hongze Cheng 已提交
813
      code = tRealloc(&pColData->pData, pColData->nData + pColVal->value.nData);
H
Hongze Cheng 已提交
814 815 816 817 818
      if (code) goto _exit;
      memcpy(pColData->pData + pColData->nData, pColVal->value.pData, pColVal->value.nData);
      pColData->nData += pColVal->value.nData;
    }
  } else {
H
Hongze Cheng 已提交
819
    code = tRealloc(&pColData->pData, pColData->nData + tPutValue(NULL, pValue, pColVal->type));
H
Hongze Cheng 已提交
820 821
    if (code) goto _exit;
    pColData->nData += tPutValue(pColData->pData + pColData->nData, pValue, pColVal->type);
H
Hongze Cheng 已提交
822 823
  }

H
Hongze Cheng 已提交
824 825 826
  pColData->nVal++;

_exit:
H
Hongze Cheng 已提交
827 828 829
  return code;
}

H
Hongze Cheng 已提交
830 831
int32_t tColDataCopy(SColData *pColDataSrc, SColData *pColDataDest) {
  int32_t code = 0;
H
Hongze Cheng 已提交
832
  int32_t size;
H
Hongze Cheng 已提交
833

H
Hongze Cheng 已提交
834 835
  ASSERT(pColDataSrc->nVal > 0);

H
Hongze Cheng 已提交
836 837 838
  pColDataDest->cid = pColDataSrc->cid;
  pColDataDest->type = pColDataSrc->type;
  pColDataDest->smaOn = pColDataSrc->smaOn;
H
Hongze Cheng 已提交
839 840 841
  pColDataDest->nVal = pColDataSrc->nVal;
  pColDataDest->flag = pColDataSrc->flag;

H
Hongze Cheng 已提交
842 843 844 845 846 847
  if (pColDataSrc->flag != HAS_NONE && pColDataSrc->flag != HAS_NULL && pColDataSrc->flag != HAS_VALUE) {
    size = BIT2_SIZE(pColDataSrc->nVal);
    code = tRealloc(&pColDataDest->pBitMap, size);
    if (code) goto _exit;
    memcpy(pColDataDest->pBitMap, pColDataSrc->pBitMap, size);
  }
H
Hongze Cheng 已提交
848

H
Hongze Cheng 已提交
849 850
  if (IS_VAR_DATA_TYPE(pColDataDest->type)) {
    size = sizeof(int32_t) * pColDataSrc->nVal;
H
Hongze Cheng 已提交
851

H
Hongze Cheng 已提交
852
    code = tRealloc((uint8_t **)&pColDataDest->aOffset, size);
H
Hongze Cheng 已提交
853 854
    if (code) goto _exit;

H
Hongze Cheng 已提交
855
    memcpy(pColDataDest->aOffset, pColDataSrc->aOffset, size);
H
Hongze Cheng 已提交
856 857
  }

H
Hongze Cheng 已提交
858
  code = tRealloc(&pColDataDest->pData, pColDataSrc->nData);
H
Hongze Cheng 已提交
859 860 861 862
  if (code) goto _exit;
  pColDataDest->nData = pColDataSrc->nData;
  memcpy(pColDataDest->pData, pColDataSrc->pData, pColDataDest->nData);

H
Hongze Cheng 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
_exit:
  return code;
}

int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
  int32_t code = 0;

  ASSERT(iVal < pColData->nVal);
  ASSERT(pColData->flag);

  if (pColData->flag == HAS_NONE) {
    *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
    goto _exit;
  } else if (pColData->flag == HAS_NULL) {
    *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
    goto _exit;
  } else if (pColData->flag != HAS_VALUE) {
    uint8_t v = GET_BIT2(pColData->pBitMap, iVal);
    if (v == 0) {
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
      goto _exit;
    } else if (v == 1) {
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
      goto _exit;
    }
  }

  // get value
  SValue value;
  if (IS_VAR_DATA_TYPE(pColData->type)) {
H
Hongze Cheng 已提交
893 894 895 896
    if (iVal + 1 < pColData->nVal) {
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
    } else {
      value.nData = pColData->nData - pColData->aOffset[iVal];
H
Hongze Cheng 已提交
897
    }
H
Hongze Cheng 已提交
898 899

    value.pData = pColData->pData + pColData->aOffset[iVal];
H
Hongze Cheng 已提交
900 901 902 903 904 905 906
  } else {
    tGetValue(pColData->pData + tDataTypes[pColData->type].bytes * iVal, &value, pColData->type);
  }
  *pColVal = COL_VAL_VALUE(pColData->cid, pColData->type, value);

_exit:
  return code;
H
Hongze Cheng 已提交
907 908
}

H
Hongze Cheng 已提交
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
int32_t tPutColData(uint8_t *p, SColData *pColData) {
  int32_t n = 0;

  n += tPutI16v(p ? p + n : p, pColData->cid);
  n += tPutI8(p ? p + n : p, pColData->type);
  n += tPutI8(p ? p + n : p, pColData->smaOn);
  n += tPutI32v(p ? p + n : p, pColData->nVal);
  n += tPutU8(p ? p + n : p, pColData->flag);

  if (pColData->flag == HAS_NONE || pColData->flag == HAS_NULL) goto _exit;
  if (pColData->flag != HAS_VALUE) {
    // bitmap

    int32_t size = BIT2_SIZE(pColData->nVal);
    if (p) {
      memcpy(p + n, pColData->pBitMap, size);
    }
    n += size;
  }
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    // offset

    int32_t size = sizeof(int32_t) * pColData->nVal;
    if (p) {
      memcpy(p + n, pColData->aOffset, size);
    }
    n += size;
  }
  n += tPutI32v(p ? p + n : p, pColData->nData);
  if (p) {
    memcpy(p + n, pColData->pData, pColData->nData);
  }
  n += pColData->nData;

_exit:
  return n;
}

int32_t tGetColData(uint8_t *p, SColData *pColData) {
  int32_t n = 0;

  n += tGetI16v(p + n, &pColData->cid);
  n += tGetI8(p + n, &pColData->type);
  n += tGetI8(p + n, &pColData->smaOn);
  n += tGetI32v(p + n, &pColData->nVal);
  n += tGetU8(p + n, &pColData->flag);

  if (pColData->flag == HAS_NONE || pColData->flag == HAS_NULL) goto _exit;
  if (pColData->flag != HAS_VALUE) {
    // bitmap

    int32_t size = BIT2_SIZE(pColData->nVal);
    pColData->pBitMap = p + n;
    n += size;
  }
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    // offset

    int32_t size = sizeof(int32_t) * pColData->nVal;
    pColData->aOffset = (int32_t *)(p + n);
    n += size;
  }
  n += tGetI32v(p + n, &pColData->nData);
  pColData->pData = p + n;
  n += pColData->nData;

_exit:
  return n;
}

H
Hongze Cheng 已提交
979 980 981
static FORCE_INLINE int32_t tColDataCmprFn(const void *p1, const void *p2) {
  SColData *pColData1 = (SColData *)p1;
  SColData *pColData2 = (SColData *)p2;
H
Hongze Cheng 已提交
982 983

  if (pColData1->cid < pColData2->cid) {
H
Hongze Cheng 已提交
984
    return -1;
H
Hongze Cheng 已提交
985
  } else if (pColData1->cid > pColData2->cid) {
H
Hongze Cheng 已提交
986 987 988 989 990 991
    return 1;
  }

  return 0;
}

H
Hongze Cheng 已提交
992
// SBlockData ======================================================
H
Hongze Cheng 已提交
993 994
int32_t tBlockDataInit(SBlockData *pBlockData) {
  int32_t code = 0;
H
Hongze Cheng 已提交
995

H
Hongze Cheng 已提交
996 997 998
  pBlockData->nRow = 0;
  pBlockData->aVersion = NULL;
  pBlockData->aTSKEY = NULL;
H
Hongze Cheng 已提交
999 1000
  pBlockData->aIdx = taosArrayInit(0, sizeof(int32_t));
  if (pBlockData->aIdx == NULL) {
H
Hongze Cheng 已提交
1001 1002 1003 1004 1005
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
  pBlockData->aColData = taosArrayInit(0, sizeof(SColData));
  if (pBlockData->aColData == NULL) {
H
Hongze Cheng 已提交
1006
    taosArrayDestroy(pBlockData->aIdx);
H
Hongze Cheng 已提交
1007 1008 1009
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
1010 1011 1012 1013 1014

_exit:
  return code;
}

H
Hongze Cheng 已提交
1015 1016
void tBlockDataReset(SBlockData *pBlockData) {
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
1017
  taosArrayClear(pBlockData->aIdx);
H
Hongze Cheng 已提交
1018
}
H
Hongze Cheng 已提交
1019

H
Hongze Cheng 已提交
1020
void tBlockDataClear(SBlockData *pBlockData, int8_t deepClear) {
H
Hongze Cheng 已提交
1021 1022
  tFree((uint8_t *)pBlockData->aVersion);
  tFree((uint8_t *)pBlockData->aTSKEY);
H
Hongze Cheng 已提交
1023
  taosArrayDestroy(pBlockData->aIdx);
H
Hongze Cheng 已提交
1024
  taosArrayDestroyEx(pBlockData->aColData, deepClear ? tColDataClear : NULL);
H
Haojun Liao 已提交
1025 1026 1027 1028
  pBlockData->aColData = NULL;
  pBlockData->aIdx = NULL;
  pBlockData->aTSKEY = NULL;
  pBlockData->aVersion = NULL;
H
Hongze Cheng 已提交
1029 1030
}

H
Hongze Cheng 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
int32_t tBlockDataSetSchema(SBlockData *pBlockData, STSchema *pTSchema) {
  int32_t   code = 0;
  SColData *pColData;
  STColumn *pTColumn;

  tBlockDataReset(pBlockData);
  for (int32_t iColumn = 1; iColumn < pTSchema->numOfCols; iColumn++) {
    pTColumn = &pTSchema->columns[iColumn];

    code = tBlockDataAddColData(pBlockData, iColumn - 1, &pColData);
    if (code) goto _exit;

    tColDataInit(pColData, pTColumn->colId, pTColumn->type, (pTColumn->flags & COL_SMA_ON) != 0);
  }

_exit:
  return code;
}

void tBlockDataClearData(SBlockData *pBlockData) {
  pBlockData->nRow = 0;
H
Hongze Cheng 已提交
1052 1053
  for (int32_t iColData = 0; iColData < taosArrayGetSize(pBlockData->aIdx); iColData++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1054 1055 1056 1057
    tColDataReset(pColData);
  }
}

H
Hongze Cheng 已提交
1058 1059
int32_t tBlockDataAddColData(SBlockData *pBlockData, int32_t iColData, SColData **ppColData) {
  int32_t   code = 0;
H
Hongze Cheng 已提交
1060
  SColData *pColData = NULL;
H
Hongze Cheng 已提交
1061
  int32_t   idx = taosArrayGetSize(pBlockData->aIdx);
H
Hongze Cheng 已提交
1062 1063

  if (idx >= taosArrayGetSize(pBlockData->aColData)) {
H
Hongze Cheng 已提交
1064 1065 1066 1067
    if (taosArrayPush(pBlockData->aColData, &((SColData){0})) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1068 1069 1070
  }
  pColData = (SColData *)taosArrayGet(pBlockData->aColData, idx);

H
Hongze Cheng 已提交
1071
  if (taosArrayInsert(pBlockData->aIdx, iColData, &idx) == NULL) {
H
Hongze Cheng 已提交
1072 1073
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
H
Hongze Cheng 已提交
1074 1075
  }

H
Hongze Cheng 已提交
1076 1077 1078 1079 1080 1081
  *ppColData = pColData;
  return code;

_err:
  *ppColData = NULL;
  return code;
H
Hongze Cheng 已提交
1082
}
H
Hongze Cheng 已提交
1083 1084 1085

int32_t tBlockDataAppendRow(SBlockData *pBlockData, TSDBROW *pRow, STSchema *pTSchema) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1086

H
Hongze Cheng 已提交
1087
  // TSDBKEY
H
Hongze Cheng 已提交
1088
  code = tRealloc((uint8_t **)&pBlockData->aVersion, sizeof(int64_t) * (pBlockData->nRow + 1));
H
Hongze Cheng 已提交
1089
  if (code) goto _err;
H
Hongze Cheng 已提交
1090
  code = tRealloc((uint8_t **)&pBlockData->aTSKEY, sizeof(TSKEY) * (pBlockData->nRow + 1));
H
Hongze Cheng 已提交
1091
  if (code) goto _err;
H
Hongze Cheng 已提交
1092 1093
  pBlockData->aVersion[pBlockData->nRow] = TSDBROW_VERSION(pRow);
  pBlockData->aTSKEY[pBlockData->nRow] = TSDBROW_TS(pRow);
H
Hongze Cheng 已提交
1094

H
Hongze Cheng 已提交
1095 1096
  // OTHER
  int32_t   iColData = 0;
H
Hongze Cheng 已提交
1097
  int32_t   nColData = taosArrayGetSize(pBlockData->aIdx);
H
Hongze Cheng 已提交
1098 1099
  SRowIter  iter = {0};
  SRowIter *pIter = &iter;
H
Hongze Cheng 已提交
1100 1101
  SColData *pColData;
  SColVal  *pColVal;
H
Hongze Cheng 已提交
1102

H
Hongze Cheng 已提交
1103
  if (nColData == 0) goto _exit;
H
Hongze Cheng 已提交
1104

H
Hongze Cheng 已提交
1105
  tRowIterInit(pIter, pRow, pTSchema);
H
Hongze Cheng 已提交
1106
  pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
H
Hongze Cheng 已提交
1107 1108
  pColVal = tRowIterNext(pIter);

H
Hongze Cheng 已提交
1109 1110 1111 1112 1113
  while (pColData) {
    if (pColVal) {
      if (pColData->cid == pColVal->cid) {
        code = tColDataAppendValue(pColData, pColVal);
        if (code) goto _err;
H
Hongze Cheng 已提交
1114

H
Hongze Cheng 已提交
1115
        pColVal = tRowIterNext(pIter);
H
Hongze Cheng 已提交
1116
        pColData = ((++iColData) < nColData) ? tBlockDataGetColDataByIdx(pBlockData, iColData) : NULL;
H
Hongze Cheng 已提交
1117 1118
      } else if (pColData->cid < pColVal->cid) {
        code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColData->cid, pColData->type));
H
Hongze Cheng 已提交
1119
        if (code) goto _err;
H
Hongze Cheng 已提交
1120

H
Hongze Cheng 已提交
1121
        pColData = ((++iColData) < nColData) ? tBlockDataGetColDataByIdx(pBlockData, iColData) : NULL;
H
Hongze Cheng 已提交
1122 1123 1124 1125 1126
      } else {
        pColVal = tRowIterNext(pIter);
      }
    } else {
      code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColData->cid, pColData->type));
H
Hongze Cheng 已提交
1127 1128
      if (code) goto _err;

H
Hongze Cheng 已提交
1129
      pColData = ((++iColData) < nColData) ? tBlockDataGetColDataByIdx(pBlockData, iColData) : NULL;
H
Hongze Cheng 已提交
1130
    }
H
Hongze Cheng 已提交
1131
  }
H
Hongze Cheng 已提交
1132

H
Hongze Cheng 已提交
1133
_exit:
H
Hongze Cheng 已提交
1134
  pBlockData->nRow++;
H
Hongze Cheng 已提交
1135
  return code;
H
Hongze Cheng 已提交
1136

H
Hongze Cheng 已提交
1137
_err:
H
Hongze Cheng 已提交
1138
  return code;
1139
}
H
Hongze Cheng 已提交
1140

H
Hongze Cheng 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
int32_t tBlockDataCorrectSchema(SBlockData *pBlockData, SBlockData *pBlockDataFrom) {
  int32_t code = 0;

  int32_t iColData = 0;
  for (int32_t iColDataFrom = 0; iColDataFrom < taosArrayGetSize(pBlockDataFrom->aIdx); iColDataFrom++) {
    SColData *pColDataFrom = tBlockDataGetColDataByIdx(pBlockDataFrom, iColDataFrom);

    while (true) {
      SColData *pColData;
      if (iColData < taosArrayGetSize(pBlockData->aIdx)) {
        pColData = tBlockDataGetColDataByIdx(pBlockData, iColData);
      } else {
        pColData = NULL;
      }

      if (pColData == NULL || pColData->cid > pColDataFrom->cid) {
        code = tBlockDataAddColData(pBlockData, iColData, &pColData);
        if (code) goto _exit;

H
Hongze Cheng 已提交
1160
        tColDataInit(pColData, pColDataFrom->cid, pColDataFrom->type, pColDataFrom->smaOn);
H
Hongze Cheng 已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
        for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
          code = tColDataAppendValue(pColData, &COL_VAL_NONE(pColData->cid, pColData->type));
          if (code) goto _exit;
        }

        iColData++;
        break;
      } else if (pColData->cid == pColDataFrom->cid) {
        iColData++;
        break;
      } else {
        iColData++;
      }
    }
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
1181 1182 1183
int32_t tBlockDataMerge(SBlockData *pBlockData1, SBlockData *pBlockData2, SBlockData *pBlockData) {
  int32_t code = 0;

H
Hongze Cheng 已提交
1184 1185
  // set target
  int32_t   iColData1 = 0;
H
Hongze Cheng 已提交
1186
  int32_t   nColData1 = taosArrayGetSize(pBlockData1->aIdx);
H
Hongze Cheng 已提交
1187
  int32_t   iColData2 = 0;
H
Hongze Cheng 已提交
1188
  int32_t   nColData2 = taosArrayGetSize(pBlockData2->aIdx);
H
Hongze Cheng 已提交
1189 1190 1191 1192
  SColData *pColData1;
  SColData *pColData2;
  SColData *pColData;

H
Hongze Cheng 已提交
1193
  tBlockDataReset(pBlockData);
H
Hongze Cheng 已提交
1194
  while (iColData1 < nColData1 && iColData2 < nColData2) {
H
Hongze Cheng 已提交
1195 1196
    pColData1 = tBlockDataGetColDataByIdx(pBlockData1, iColData1);
    pColData2 = tBlockDataGetColDataByIdx(pBlockData2, iColData2);
H
Hongze Cheng 已提交
1197 1198

    if (pColData1->cid == pColData2->cid) {
H
Hongze Cheng 已提交
1199
      code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
H
Hongze Cheng 已提交
1200 1201 1202 1203 1204 1205
      if (code) goto _exit;
      tColDataInit(pColData, pColData2->cid, pColData2->type, pColData2->smaOn);

      iColData1++;
      iColData2++;
    } else if (pColData1->cid < pColData2->cid) {
H
Hongze Cheng 已提交
1206
      code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
H
Hongze Cheng 已提交
1207 1208 1209 1210 1211
      if (code) goto _exit;
      tColDataInit(pColData, pColData1->cid, pColData1->type, pColData1->smaOn);

      iColData1++;
    } else {
H
Hongze Cheng 已提交
1212
      code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
H
Hongze Cheng 已提交
1213 1214 1215 1216 1217 1218 1219 1220
      if (code) goto _exit;
      tColDataInit(pColData, pColData2->cid, pColData2->type, pColData2->smaOn);

      iColData2++;
    }
  }

  while (iColData1 < nColData1) {
H
Hongze Cheng 已提交
1221
    code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
H
Hongze Cheng 已提交
1222 1223 1224 1225 1226 1227 1228
    if (code) goto _exit;
    tColDataInit(pColData, pColData1->cid, pColData1->type, pColData1->smaOn);

    iColData1++;
  }

  while (iColData2 < nColData2) {
H
Hongze Cheng 已提交
1229
    code = tBlockDataAddColData(pBlockData, taosArrayGetSize(pBlockData->aIdx), &pColData);
H
Hongze Cheng 已提交
1230 1231 1232 1233 1234
    if (code) goto _exit;
    tColDataInit(pColData, pColData2->cid, pColData2->type, pColData2->smaOn);

    iColData2++;
  }
H
Hongze Cheng 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291

  // loop to merge
  int32_t iRow1 = 0;
  int32_t nRow1 = pBlockData1->nRow;
  int32_t iRow2 = 0;
  int32_t nRow2 = pBlockData2->nRow;
  TSDBROW row1;
  TSDBROW row2;
  int32_t c;

  while (iRow1 < nRow1 && iRow2 < nRow2) {
    row1 = tsdbRowFromBlockData(pBlockData1, iRow1);
    row2 = tsdbRowFromBlockData(pBlockData2, iRow2);

    c = tsdbKeyCmprFn(&TSDBROW_KEY(&row1), &TSDBROW_KEY(&row2));
    if (c < 0) {
      code = tBlockDataAppendRow(pBlockData, &row1, NULL);
      if (code) goto _exit;
      iRow1++;
    } else if (c > 0) {
      code = tBlockDataAppendRow(pBlockData, &row2, NULL);
      if (code) goto _exit;
      iRow2++;
    } else {
      ASSERT(0);
    }
  }

  while (iRow1 < nRow1) {
    row1 = tsdbRowFromBlockData(pBlockData1, iRow1);
    code = tBlockDataAppendRow(pBlockData, &row1, NULL);
    if (code) goto _exit;
    iRow1++;
  }

  while (iRow2 < nRow2) {
    row2 = tsdbRowFromBlockData(pBlockData2, iRow2);
    code = tBlockDataAppendRow(pBlockData, &row2, NULL);
    if (code) goto _exit;
    iRow2++;
  }

_exit:
  return code;
}

int32_t tBlockDataCopy(SBlockData *pBlockDataSrc, SBlockData *pBlockDataDest) {
  int32_t   code = 0;
  SColData *pColDataSrc;
  SColData *pColDataDest;

  ASSERT(pBlockDataSrc->nRow > 0);

  tBlockDataReset(pBlockDataDest);

  pBlockDataDest->nRow = pBlockDataSrc->nRow;
  // TSDBKEY
H
Hongze Cheng 已提交
1292
  code = tRealloc((uint8_t **)&pBlockDataDest->aVersion, sizeof(int64_t) * pBlockDataSrc->nRow);
H
Hongze Cheng 已提交
1293
  if (code) goto _exit;
H
Hongze Cheng 已提交
1294
  code = tRealloc((uint8_t **)&pBlockDataDest->aTSKEY, sizeof(TSKEY) * pBlockDataSrc->nRow);
H
Hongze Cheng 已提交
1295 1296 1297 1298 1299
  if (code) goto _exit;
  memcpy(pBlockDataDest->aVersion, pBlockDataSrc->aVersion, sizeof(int64_t) * pBlockDataSrc->nRow);
  memcpy(pBlockDataDest->aTSKEY, pBlockDataSrc->aTSKEY, sizeof(TSKEY) * pBlockDataSrc->nRow);

  // other
H
Hongze Cheng 已提交
1300 1301
  for (size_t iColData = 0; iColData < taosArrayGetSize(pBlockDataSrc->aIdx); iColData++) {
    pColDataSrc = tBlockDataGetColDataByIdx(pBlockDataSrc, iColData);
H
Hongze Cheng 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310
    code = tBlockDataAddColData(pBlockDataDest, iColData, &pColDataDest);
    if (code) goto _exit;

    code = tColDataCopy(pColDataSrc, pColDataDest);
    if (code) goto _exit;
  }

_exit:
  return code;
1311
}
H
Hongze Cheng 已提交
1312

H
Hongze Cheng 已提交
1313 1314 1315 1316 1317
SColData *tBlockDataGetColDataByIdx(SBlockData *pBlockData, int32_t idx) {
  ASSERT(idx >= 0 && idx < taosArrayGetSize(pBlockData->aIdx));
  return (SColData *)taosArrayGet(pBlockData->aColData, *(int32_t *)taosArrayGet(pBlockData->aIdx, idx));
}

H
Hongze Cheng 已提交
1318 1319
void tBlockDataGetColData(SBlockData *pBlockData, int16_t cid, SColData **ppColData) {
  ASSERT(cid != PRIMARYKEY_TIMESTAMP_COL_ID);
H
Hongze Cheng 已提交
1320 1321
  int32_t lidx = 0;
  int32_t ridx = taosArrayGetSize(pBlockData->aIdx) - 1;
H
Hongze Cheng 已提交
1322

H
Hongze Cheng 已提交
1323
  while (lidx <= ridx) {
M
Minglei Jin 已提交
1324 1325 1326
    int32_t   midx = (lidx + ridx) / 2;
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, midx);
    int32_t   c = tColDataCmprFn(pColData, &(SColData){.cid = cid});
H
Hongze Cheng 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335

    if (c == 0) {
      *ppColData = pColData;
      return;
    } else if (c < 0) {
      lidx = midx + 1;
    } else {
      ridx = midx - 1;
    }
H
Hongze Cheng 已提交
1336
  }
H
Hongze Cheng 已提交
1337 1338

  *ppColData = NULL;
H
Hongze Cheng 已提交
1339 1340
}

H
Hongze Cheng 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
int32_t tPutBlockData(uint8_t *p, SBlockData *pBlockData) {
  int32_t n = 0;

  n += tPutI32v(p ? p + n : p, pBlockData->nRow);
  if (p) {
    memcpy(p + n, pBlockData->aVersion, sizeof(int64_t) * pBlockData->nRow);
  }
  n = n + sizeof(int64_t) * pBlockData->nRow;
  if (p) {
    memcpy(p + n, pBlockData->aTSKEY, sizeof(TSKEY) * pBlockData->nRow);
  }
  n = n + sizeof(TSKEY) * pBlockData->nRow;

  int32_t nCol = taosArrayGetSize(pBlockData->aIdx);
  n += tPutI32v(p ? p + n : p, nCol);
  for (int32_t iCol = 0; iCol < nCol; iCol++) {
    SColData *pColData = tBlockDataGetColDataByIdx(pBlockData, iCol);
    n += tPutColData(p ? p + n : p, pColData);
  }

  return n;
}

int32_t tGetBlockData(uint8_t *p, SBlockData *pBlockData) {
  int32_t n = 0;

  tBlockDataReset(pBlockData);

  n += tGetI32v(p + n, &pBlockData->nRow);
  pBlockData->aVersion = (int64_t *)(p + n);
  n = n + sizeof(int64_t) * pBlockData->nRow;
  pBlockData->aTSKEY = (TSKEY *)(p + n);
  n = n + sizeof(TSKEY) * pBlockData->nRow;

  int32_t nCol;
  n += tGetI32v(p + n, &nCol);
  for (int32_t iCol = 0; iCol < nCol; iCol++) {
    SColData *pColData;

    if (tBlockDataAddColData(pBlockData, iCol, &pColData)) return -1;
    n += tGetColData(p + n, pColData);
  }

  return n;
}

H
Hongze Cheng 已提交
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
// ALGORITHM ==============================
void tsdbCalcColDataSMA(SColData *pColData, SColumnDataAgg *pColAgg) {
  SColVal  colVal;
  SColVal *pColVal = &colVal;

  *pColAgg = (SColumnDataAgg){.colId = pColData->cid};
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
    tColDataGetValue(pColData, iVal, pColVal);

    if (pColVal->isNone || pColVal->isNull) {
      pColAgg->numOfNull++;
    } else {
      switch (pColData->type) {
        case TSDB_DATA_TYPE_NULL:
          break;
        case TSDB_DATA_TYPE_BOOL:
          break;
1404 1405 1406 1407 1408 1409 1410 1411
        case TSDB_DATA_TYPE_TINYINT:{
          pColAgg->sum += colVal.value.i8;
          if (pColAgg->min > colVal.value.i8) {
            pColAgg->min = colVal.value.i8;
          }
          if (pColAgg->max < colVal.value.i8) {
            pColAgg->max = colVal.value.i8;
          }
H
Hongze Cheng 已提交
1412
          break;
1413 1414 1415 1416 1417 1418 1419 1420 1421
        }
        case TSDB_DATA_TYPE_SMALLINT:{
          pColAgg->sum += colVal.value.i16;
          if (pColAgg->min > colVal.value.i16) {
            pColAgg->min = colVal.value.i16;
          }
          if (pColAgg->max < colVal.value.i16) {
            pColAgg->max = colVal.value.i16;
          }
H
Hongze Cheng 已提交
1422
          break;
1423
        }
1424 1425 1426 1427 1428 1429 1430 1431
        case TSDB_DATA_TYPE_INT: {
          pColAgg->sum += colVal.value.i32;
          if (pColAgg->min > colVal.value.i32) {
            pColAgg->min = colVal.value.i32;
          }
          if (pColAgg->max < colVal.value.i32) {
            pColAgg->max = colVal.value.i32;
          }
H
Hongze Cheng 已提交
1432
          break;
1433 1434 1435 1436 1437 1438 1439 1440 1441
        }
        case TSDB_DATA_TYPE_BIGINT: {
          pColAgg->sum += colVal.value.i64;
          if (pColAgg->min > colVal.value.i64) {
            pColAgg->min = colVal.value.i64;
          }
          if (pColAgg->max < colVal.value.i64) {
            pColAgg->max = colVal.value.i64;
          }
H
Hongze Cheng 已提交
1442
          break;
1443
        }
1444 1445 1446 1447 1448 1449 1450 1451
        case TSDB_DATA_TYPE_FLOAT:{
          pColAgg->sum += colVal.value.f;
          if (pColAgg->min > colVal.value.f) {
            pColAgg->min = colVal.value.f;
          }
          if (pColAgg->max < colVal.value.f) {
            pColAgg->max = colVal.value.f;
          }
H
Hongze Cheng 已提交
1452
          break;
1453 1454 1455 1456 1457 1458 1459 1460 1461
        }
        case TSDB_DATA_TYPE_DOUBLE:{
          pColAgg->sum += colVal.value.d;
          if (pColAgg->min > colVal.value.d) {
            pColAgg->min = colVal.value.d;
          }
          if (pColAgg->max < colVal.value.d) {
            pColAgg->max = colVal.value.d;
          }
H
Hongze Cheng 已提交
1462
          break;
1463
        }
H
Hongze Cheng 已提交
1464 1465
        case TSDB_DATA_TYPE_VARCHAR:
          break;
1466 1467 1468 1469 1470 1471 1472
        case TSDB_DATA_TYPE_TIMESTAMP:{
          if (pColAgg->min > colVal.value.i64) {
            pColAgg->min = colVal.value.i64;
          }
          if (pColAgg->max < colVal.value.i64) {
            pColAgg->max = colVal.value.i64;
          }
H
Hongze Cheng 已提交
1473
          break;
1474
        }
H
Hongze Cheng 已提交
1475 1476
        case TSDB_DATA_TYPE_NCHAR:
          break;
1477 1478 1479 1480 1481 1482 1483 1484
        case TSDB_DATA_TYPE_UTINYINT:{
          pColAgg->sum += colVal.value.u8;
          if (pColAgg->min > colVal.value.u8) {
            pColAgg->min = colVal.value.u8;
          }
          if (pColAgg->max < colVal.value.u8) {
            pColAgg->max = colVal.value.u8;
          }
H
Hongze Cheng 已提交
1485
          break;
1486 1487 1488 1489 1490 1491 1492 1493 1494
        }
        case TSDB_DATA_TYPE_USMALLINT:{
          pColAgg->sum += colVal.value.u16;
          if (pColAgg->min > colVal.value.u16) {
            pColAgg->min = colVal.value.u16;
          }
          if (pColAgg->max < colVal.value.u16) {
            pColAgg->max = colVal.value.u16;
          }
H
Hongze Cheng 已提交
1495
          break;
1496 1497 1498 1499 1500 1501 1502 1503 1504
        }
        case TSDB_DATA_TYPE_UINT:{
          pColAgg->sum += colVal.value.u32;
          if (pColAgg->min > colVal.value.u32) {
            pColAgg->min = colVal.value.u32;
          }
          if (pColAgg->max < colVal.value.u32) {
            pColAgg->max = colVal.value.u32;
          }
H
Hongze Cheng 已提交
1505
          break;
1506 1507 1508 1509 1510 1511 1512 1513 1514
        }
        case TSDB_DATA_TYPE_UBIGINT:{
          pColAgg->sum += colVal.value.u64;
          if (pColAgg->min > colVal.value.u64) {
            pColAgg->min = colVal.value.u64;
          }
          if (pColAgg->max < colVal.value.u64) {
            pColAgg->max = colVal.value.u64;
          }
H
Hongze Cheng 已提交
1515
          break;
1516
        }
H
Hongze Cheng 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
        case TSDB_DATA_TYPE_JSON:
          break;
        case TSDB_DATA_TYPE_VARBINARY:
          break;
        case TSDB_DATA_TYPE_DECIMAL:
          break;
        case TSDB_DATA_TYPE_BLOB:
          break;
        case TSDB_DATA_TYPE_MEDIUMBLOB:
          break;
        default:
          ASSERT(0);
      }
    }
  }
}