tdataformat.c 62.6 KB
Newer Older
H
hzcheng 已提交
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/>.
 */
S
common  
Shengliang Guan 已提交
15 16

#define _DEFAULT_SOURCE
S
slguan 已提交
17
#include "tdataformat.h"
H
refact  
Hongze Cheng 已提交
18
#include "tRealloc.h"
S
Shengliang Guan 已提交
19
#include "tcoding.h"
L
Liu Jicong 已提交
20
#include "tdatablock.h"
S
log  
Shengliang Guan 已提交
21
#include "tlog.h"
H
more  
hzcheng 已提交
22

H
more  
Hongze Cheng 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// SBuffer ================================
void tBufferDestroy(SBuffer *pBuffer) {
  tFree(pBuffer->pBuf);
  pBuffer->pBuf = NULL;
}

int32_t tBufferInit(SBuffer *pBuffer, int64_t size) {
  pBuffer->nBuf = 0;
  return tRealloc(&pBuffer->pBuf, size);
}

int32_t tBufferPut(SBuffer *pBuffer, const void *pData, int64_t nData) {
  int32_t code = 0;

  code = tRealloc(&pBuffer->pBuf, pBuffer->nBuf + nData);
  if (code) return code;

  memcpy(pBuffer->pBuf + pBuffer->nBuf, pData, nData);
  pBuffer->nBuf += nData;

  return code;
}

H
Hongze Cheng 已提交
46 47 48 49 50 51 52 53 54 55
int32_t tBufferReserve(SBuffer *pBuffer, int64_t nData, void **ppData) {
  int32_t code = tRealloc(&pBuffer->pBuf, pBuffer->nBuf + nData);
  if (code) return code;

  *ppData = pBuffer->pBuf + pBuffer->nBuf;
  pBuffer->nBuf += nData;

  return code;
}

H
more  
Hongze Cheng 已提交
56
// ================================
C
Cary Xu 已提交
57 58
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson);

H
Hongze Cheng 已提交
59
// SRow ========================================================================
H
Hongze Cheng 已提交
60 61 62
#define KV_FLG_LIT ((uint8_t)0x10)
#define KV_FLG_MID ((uint8_t)0x20)
#define KV_FLG_BIG ((uint8_t)0x30)
H
Hongze Cheng 已提交
63

H
Hongze Cheng 已提交
64 65 66 67
#define ROW_BIT_NONE  ((uint8_t)0x0)
#define ROW_BIT_NULL  ((uint8_t)0x1)
#define ROW_BIT_VALUE ((uint8_t)0x2)

H
Hongze Cheng 已提交
68
#pragma pack(push, 1)
H
Hongze Cheng 已提交
69
typedef struct {
H
Hongze Cheng 已提交
70
  int16_t nCol;
H
Hongze Cheng 已提交
71
  char    idx[];  // uint8_t * | uint16_t * | uint32_t *
H
Hongze Cheng 已提交
72
} SKVIdx;
H
Hongze Cheng 已提交
73
#pragma pack(pop)
H
Hongze Cheng 已提交
74

H
Hongze Cheng 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#define ROW_SET_BITMAP(PB, FLAG, IDX, VAL)        \
  do {                                            \
    if (PB) {                                     \
      switch (FLAG) {                             \
        case (HAS_NULL | HAS_NONE):               \
          SET_BIT1(PB, IDX, VAL);                 \
          break;                                  \
        case (HAS_VALUE | HAS_NONE):              \
          SET_BIT1(PB, IDX, (VAL) ? (VAL)-1 : 0); \
          break;                                  \
        case (HAS_VALUE | HAS_NULL):              \
          SET_BIT1(PB, IDX, (VAL)-1);             \
          break;                                  \
        case (HAS_VALUE | HAS_NULL | HAS_NONE):   \
          SET_BIT2(PB, IDX, VAL);                 \
          break;                                  \
        default:                                  \
          ASSERT(0);                              \
          break;                                  \
      }                                           \
    }                                             \
  } while (0)

H
Hongze Cheng 已提交
98
int32_t tRowBuild(SArray *aColVal, STSchema *pTSchema, SBuffer *pBuffer) {
H
Hongze Cheng 已提交
99
  int32_t code = 0;
H
Hongze Cheng 已提交
100

H
Hongze Cheng 已提交
101 102 103 104 105
  ASSERT(taosArrayGetSize(aColVal) > 0);
  ASSERT(((SColVal *)aColVal->pData)[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID);
  ASSERT(((SColVal *)aColVal->pData)[0].type == TSDB_DATA_TYPE_TIMESTAMP);

  // scan ---------------
H
Hongze Cheng 已提交
106 107 108 109 110 111 112 113 114 115
  uint8_t       flag = 0;
  int32_t       iColVal = 1;
  const int32_t nColVal = taosArrayGetSize(aColVal);
  SColVal      *pColVal = (iColVal < nColVal) ? (SColVal *)taosArrayGet(aColVal, iColVal) : NULL;
  int32_t       iTColumn = 1;
  STColumn     *pTColumn = pTSchema->columns + iTColumn;
  int32_t       ntp = 0;
  int32_t       nkv = 0;
  int32_t       maxIdx = 0;
  int32_t       nIdx = 0;
H
Hongze Cheng 已提交
116 117 118
  while (pTColumn) {
    if (pColVal) {
      if (pColVal->cid == pTColumn->colId) {
H
Hongze Cheng 已提交
119
        ntp += TYPE_BYTES[pTColumn->type];
H
Hongze Cheng 已提交
120 121
        if (COL_VAL_IS_VALUE(pColVal)) {  // VALUE
          flag |= HAS_VALUE;
H
Hongze Cheng 已提交
122
          maxIdx = nkv;
H
Hongze Cheng 已提交
123 124 125 126
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
            ntp = ntp + tPutU32v(NULL, pColVal->value.nData) + pColVal->value.nData;
            nkv = nkv + tPutI16v(NULL, pTColumn->colId) + tPutU32v(NULL, pColVal->value.nData) + pColVal->value.nData;
          } else {
H
Hongze Cheng 已提交
127
            nkv = nkv + tPutI16v(NULL, pTColumn->colId) + pTColumn->bytes;
H
Hongze Cheng 已提交
128
          }
H
Hongze Cheng 已提交
129
          nIdx++;
H
Hongze Cheng 已提交
130 131
        } else if (COL_VAL_IS_NONE(pColVal)) {  // NONE
          flag |= HAS_NONE;
H
Hongze Cheng 已提交
132
        } else if (COL_VAL_IS_NULL(pColVal)) {  // NULL
H
Hongze Cheng 已提交
133
          flag |= HAS_NULL;
H
Hongze Cheng 已提交
134
          maxIdx = nkv;
H
Hongze Cheng 已提交
135
          nkv += tPutI16v(NULL, -pTColumn->colId);
H
Hongze Cheng 已提交
136
          nIdx++;
H
Hongze Cheng 已提交
137 138
        } else {
          ASSERT(0);
H
Hongze Cheng 已提交
139
        }
H
Hongze Cheng 已提交
140

H
Hongze Cheng 已提交
141
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
H
Hongze Cheng 已提交
142 143
        pColVal = (++iColVal < nColVal) ? (SColVal *)taosArrayGet(aColVal, iColVal) : NULL;
      } else if (pColVal->cid > pTColumn->colId) {  // NONE
H
Hongze Cheng 已提交
144
        flag |= HAS_NONE;
H
Hongze Cheng 已提交
145
        ntp += TYPE_BYTES[pTColumn->type];
H
Hongze Cheng 已提交
146
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
H
Hongze Cheng 已提交
147 148
      } else {
        pColVal = (++iColVal < nColVal) ? (SColVal *)taosArrayGet(aColVal, iColVal) : NULL;
H
Hongze Cheng 已提交
149 150 151
      }
    } else {  // NONE
      flag |= HAS_NONE;
H
Hongze Cheng 已提交
152
      ntp += TYPE_BYTES[pTColumn->type];
H
Hongze Cheng 已提交
153
      pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
H
more  
Hongze Cheng 已提交
154 155
    }
  }
H
Hongze Cheng 已提交
156

H
Hongze Cheng 已提交
157 158 159 160
  // compare ---------------
  switch (flag) {
    case HAS_NONE:
    case HAS_NULL:
H
Hongze Cheng 已提交
161
      ntp = sizeof(SRow);
H
Hongze Cheng 已提交
162 163
      break;
    case HAS_VALUE:
H
Hongze Cheng 已提交
164
      ntp = sizeof(SRow) + ntp;
H
Hongze Cheng 已提交
165
      break;
H
Hongze Cheng 已提交
166
    case (HAS_NULL | HAS_NONE):
H
Hongze Cheng 已提交
167
      ntp = sizeof(SRow) + BIT1_SIZE(pTSchema->numOfCols - 1);
H
Hongze Cheng 已提交
168 169 170
      break;
    case (HAS_VALUE | HAS_NONE):
    case (HAS_VALUE | HAS_NULL):
H
Hongze Cheng 已提交
171
      ntp = sizeof(SRow) + BIT1_SIZE(pTSchema->numOfCols - 1) + ntp;
H
Hongze Cheng 已提交
172
      break;
H
Hongze Cheng 已提交
173
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
H
Hongze Cheng 已提交
174
      ntp = sizeof(SRow) + BIT2_SIZE(pTSchema->numOfCols - 1) + ntp;
H
Hongze Cheng 已提交
175 176 177 178 179
      break;
    default:
      ASSERT(0);
      break;
  }
H
Hongze Cheng 已提交
180
  if (maxIdx <= UINT8_MAX) {
H
Hongze Cheng 已提交
181
    nkv = sizeof(SRow) + sizeof(SKVIdx) + nIdx + nkv;
H
Hongze Cheng 已提交
182 183
    flag |= KV_FLG_LIT;
  } else if (maxIdx <= UINT16_MAX) {
H
Hongze Cheng 已提交
184
    nkv = sizeof(SRow) + sizeof(SKVIdx) + (nIdx << 1) + nkv;
H
Hongze Cheng 已提交
185 186
    flag |= KV_FLG_MID;
  } else {
H
Hongze Cheng 已提交
187
    nkv = sizeof(SRow) + sizeof(SKVIdx) + (nIdx << 2) + nkv;
H
Hongze Cheng 已提交
188 189 190 191 192 193 194 195 196
    flag |= KV_FLG_BIG;
  }
  int32_t nRow;
  if (nkv < ntp) {
    nRow = nkv;
  } else {
    nRow = ntp;
    flag &= ((uint8_t)0x0f);
  }
H
Hongze Cheng 已提交
197

H
Hongze Cheng 已提交
198
  // alloc --------------
H
Hongze Cheng 已提交
199 200 201
  SRow *pRow = NULL;
  code = tBufferReserve(pBuffer, nRow, (void **)&pRow);
  if (code) return code;
H
Hongze Cheng 已提交
202

H
Hongze Cheng 已提交
203 204
  // build --------------
  pColVal = (SColVal *)taosArrayGet(aColVal, 0);
H
Hongze Cheng 已提交
205

H
Hongze Cheng 已提交
206 207 208 209 210 211 212 213 214
  pRow->flag = flag;
  pRow->rsv = 0;
  pRow->sver = pTSchema->version;
  pRow->len = nRow;
  memcpy(&pRow->ts, &pColVal->value.val, sizeof(TSKEY));

  if (flag == HAS_NONE || flag == HAS_NULL) {
    goto _exit;
  }
H
Hongze Cheng 已提交
215

H
Hongze Cheng 已提交
216 217 218 219
  iColVal = 1;
  pColVal = (iColVal < nColVal) ? (SColVal *)taosArrayGet(aColVal, iColVal) : NULL;
  iTColumn = 1;
  pTColumn = pTSchema->columns + iTColumn;
H
Hongze Cheng 已提交
220
  if (flag & 0xf0) {  // KV
H
Hongze Cheng 已提交
221
    SKVIdx  *pIdx = (SKVIdx *)pRow->data;
H
Hongze Cheng 已提交
222 223 224 225 226 227 228 229 230 231 232
    int32_t  iIdx = 0;
    int32_t  nv = 0;
    uint8_t *pv = NULL;
    if (flag & KV_FLG_LIT) {
      pv = pIdx->idx + nIdx;
    } else if (flag & KV_FLG_MID) {
      pv = pIdx->idx + (nIdx << 1);
    } else {
      pv = pIdx->idx + (nIdx << 2);
    }
    pIdx->nCol = nIdx;
H
Hongze Cheng 已提交
233 234 235 236 237

    while (pTColumn) {
      if (pColVal) {
        if (pColVal->cid == pTColumn->colId) {
          if (COL_VAL_IS_VALUE(pColVal)) {
H
Hongze Cheng 已提交
238 239 240 241 242 243 244 245 246
            if (flag & KV_FLG_LIT) {
              ((uint8_t *)pIdx->idx)[iIdx] = (uint8_t)nv;
            } else if (flag & KV_FLG_MID) {
              ((uint16_t *)pIdx->idx)[iIdx] = (uint16_t)nv;
            } else {
              ((uint32_t *)pIdx->idx)[iIdx] = (uint32_t)nv;
            }
            iIdx++;

H
Hongze Cheng 已提交
247 248 249 250 251 252 253 254 255 256
            nv += tPutI16v(pv + nv, pTColumn->colId);
            if (IS_VAR_DATA_TYPE(pTColumn->type)) {
              nv += tPutU32v(pv + nv, pColVal->value.nData);
              memcpy(pv + nv, pColVal->value.pData, pColVal->value.nData);
              nv += pColVal->value.nData;
            } else {
              memcpy(pv + nv, &pColVal->value.val, pTColumn->bytes);
              nv += pTColumn->bytes;
            }
          } else if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
257 258 259 260 261 262 263 264
            if (flag & KV_FLG_LIT) {
              ((uint8_t *)pIdx->idx)[iIdx] = (uint8_t)nv;
            } else if (flag & KV_FLG_MID) {
              ((uint16_t *)pIdx->idx)[iIdx] = (uint16_t)nv;
            } else {
              ((uint32_t *)pIdx->idx)[iIdx] = (uint32_t)nv;
            }
            iIdx++;
H
Hongze Cheng 已提交
265
            nv += tPutI16v(pv + nv, -pTColumn->colId);
H
Hongze Cheng 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279
          }

          pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
          pColVal = (++iColVal < nColVal) ? (SColVal *)taosArrayGet(aColVal, iColVal) : NULL;
        } else if (pColVal->cid > pTColumn->colId) {  // NONE
          pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
        } else {
          pColVal = (++iColVal < nColVal) ? (SColVal *)taosArrayGet(aColVal, iColVal) : NULL;
        }
      } else {  // NONE
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
      }
    }
  } else {  // TUPLE
H
Hongze Cheng 已提交
280 281
    uint8_t *pb = NULL;
    uint8_t *pf = NULL;
H
Hongze Cheng 已提交
282 283 284
    uint8_t *pv = NULL;
    int32_t  nv = 0;

H
Hongze Cheng 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    switch (flag) {
      case (HAS_NULL | HAS_NONE):
        pb = pRow->data;
        break;
      case HAS_VALUE:
        pf = pRow->data;
        pv = pf + pTSchema->flen;
        break;
      case (HAS_VALUE | HAS_NONE):
      case (HAS_VALUE | HAS_NULL):
        pb = pRow->data;
        pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
        pv = pf + pTSchema->flen;
        break;
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
        pb = pRow->data;
        pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
        pv = pf + pTSchema->flen;
        break;
      default:
        ASSERT(0);
        break;
H
Hongze Cheng 已提交
307
    }
H
Hongze Cheng 已提交
308

H
Hongze Cheng 已提交
309
    // build impl
H
Hongze Cheng 已提交
310 311 312
    while (pTColumn) {
      if (pColVal) {
        if (pColVal->cid == pTColumn->colId) {
H
Hongze Cheng 已提交
313 314
          if (COL_VAL_IS_VALUE(pColVal)) {  // VALUE
            ROW_SET_BITMAP(pb, flag, iTColumn - 1, ROW_BIT_VALUE);
H
Hongze Cheng 已提交
315 316 317 318 319 320 321 322 323 324 325

            if (IS_VAR_DATA_TYPE(pTColumn->type)) {
              *(int32_t *)(pf + pTColumn->offset) = nv;
              nv += tPutU32v(pv + nv, pColVal->value.nData);
              if (pColVal->value.nData) {
                memcpy(pv + nv, pColVal->value.pData, pColVal->value.nData);
                nv += pColVal->value.nData;
              }
            } else {
              memcpy(pf + pTColumn->offset, &pColVal->value.val, TYPE_BYTES[pTColumn->type]);
            }
H
Hongze Cheng 已提交
326 327 328 329 330 331
          } else if (COL_VAL_IS_NONE(pColVal)) {  // NONE
            ROW_SET_BITMAP(pb, flag, iTColumn - 1, ROW_BIT_NONE);
            if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
          } else {  // NULL
            ROW_SET_BITMAP(pb, flag, iTColumn - 1, ROW_BIT_NULL);
            if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
332
          }
H
Hongze Cheng 已提交
333

H
Hongze Cheng 已提交
334 335 336
          pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
          pColVal = (++iColVal < nColVal) ? (SColVal *)taosArrayGet(aColVal, iColVal) : NULL;
        } else if (pColVal->cid > pTColumn->colId) {  // NONE
H
Hongze Cheng 已提交
337 338
          ROW_SET_BITMAP(pb, flag, iTColumn - 1, ROW_BIT_NONE);
          if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
339 340 341 342 343
          pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
        } else {
          pColVal = (++iColVal < nColVal) ? (SColVal *)taosArrayGet(aColVal, iColVal) : NULL;
        }
      } else {  // NONE
H
Hongze Cheng 已提交
344 345
        ROW_SET_BITMAP(pb, flag, iTColumn - 1, ROW_BIT_NONE);
        if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
346 347 348 349 350
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
      }
    }
  }

H
more  
Hongze Cheng 已提交
351
_exit:
H
Hongze Cheng 已提交
352 353 354
  return code;
}

H
Hongze Cheng 已提交
355
void tRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
356 357
  ASSERT(iCol < pTSchema->numOfCols);

H
Hongze Cheng 已提交
358 359
  STColumn *pTColumn = pTSchema->columns + iCol;

H
Hongze Cheng 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  if (iCol == 0) {
    pColVal->cid = pTColumn->colId;
    pColVal->type = pTColumn->type;
    pColVal->flag = CV_FLAG_VALUE;
    memcpy(&pColVal->value.val, &pRow->ts, sizeof(TSKEY));
    return;
  }

  if (pRow->flag == HAS_NONE) {
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
    return;
  }

  if (pRow->flag == HAS_NULL) {
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
    return;
  }

H
Hongze Cheng 已提交
378
  if (pRow->flag & 0xf0) {  // KV Row
H
Hongze Cheng 已提交
379
    SKVIdx  *pIdx = (SKVIdx *)pRow->data;
H
Hongze Cheng 已提交
380 381 382 383 384 385 386 387
    uint8_t *pv = NULL;
    if (pRow->flag & KV_FLG_LIT) {
      pv = pIdx->idx + pIdx->nCol;
    } else if (pRow->flag & KV_FLG_MID) {
      pv = pIdx->idx + (pIdx->nCol << 1);
    } else {
      pv = pIdx->idx + (pIdx->nCol << 2);
    }
H
Hongze Cheng 已提交
388 389

    int16_t lidx = 0;
H
Hongze Cheng 已提交
390
    int16_t ridx = pIdx->nCol - 1;
H
Hongze Cheng 已提交
391 392
    while (lidx <= ridx) {
      int16_t  mid = (lidx + ridx) >> 1;
H
Hongze Cheng 已提交
393 394 395 396 397 398 399 400
      uint8_t *pData = NULL;
      if (pRow->flag & KV_FLG_LIT) {
        pData = pv + ((uint8_t *)pIdx->idx)[mid];
      } else if (pRow->flag & KV_FLG_MID) {
        pData = pv + ((uint16_t *)pIdx->idx)[mid];
      } else {
        pData = pv + ((uint32_t *)pIdx->idx)[mid];
      }
H
Hongze Cheng 已提交
401

H
Hongze Cheng 已提交
402
      int16_t cid;
H
Hongze Cheng 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
      pData += tGetI16v(pData, &cid);

      if (TABS(cid) == pTColumn->colId) {
        if (cid < 0) {
          *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
        } else {
          pColVal->cid = pTColumn->colId;
          pColVal->type = pTColumn->type;
          pColVal->flag = CV_FLAG_VALUE;

          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
            pData += tGetU32v(pData, &pColVal->value.nData);
            if (pColVal->value.nData > 0) {
              pColVal->value.pData = pData;
            } else {
              pColVal->value.pData = NULL;
            }
          } else {
            memcpy(&pColVal->value.val, pData, pTColumn->bytes);
          }
        }
        return;
      } else if (TABS(cid) < pTColumn->colId) {
        lidx = mid + 1;
      } else {
        ridx = mid - 1;
      }
    }

    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
  } else {  // Tuple Row
    if (pRow->flag == HAS_VALUE) {
    } else {
      // todo
    }
H
Hongze Cheng 已提交
438 439 440
  }
}

H
Hongze Cheng 已提交
441 442 443 444 445 446 447 448 449
// SRowIter ========================================
struct SRowIter {
  SRow     *pRow;
  STSchema *pTSchema;

  int32_t iTColumn;
  union {
    struct {  // kv
      int32_t  iCol;
H
Hongze Cheng 已提交
450
      SKVIdx  *pIdx;
H
Hongze Cheng 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
      uint8_t *pv;
    };
    struct {  // tupl
      // todo
    };
  };

  SColVal cv;
};

int32_t tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter) {
  int32_t code = 0;

  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
  if (pIter == NULL) {
    code = TSDB_CODE_TDB_OUT_OF_MEMORY;
    goto _exit;
  }

  pIter->pRow = pRow;
  pIter->pTSchema = pTSchema;

_exit:
  if (code) {
    *ppIter = NULL;
    if (pIter) taosMemoryFree(pIter);
  } else {
    *ppIter = pIter;
  }
  return code;
}

void tRowIterClose(SRowIter **ppIter) {
  SRowIter *pIter = *ppIter;
  if (pIter) {
    taosMemoryFree(pIter);
  }
  *ppIter = NULL;
}

SColVal *tRowIterNext(SRowIter *pIter) {
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
    return NULL;
  }

  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;

  // timestamp
  if (0 == pIter->iTColumn) {
    pIter->cv.cid = pTColumn->colId;
    pIter->cv.type = pTColumn->type;
    pIter->cv.flag = CV_FLAG_VALUE;
    memcpy(&pIter->cv.value.val, &pIter->pRow->ts, sizeof(TSKEY));
    goto _exit;
  }

  if (pIter->pRow->flag == HAS_NONE) {
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
    goto _exit;
  }

  if (pIter->pRow->flag == HAS_NULL) {
    pIter->cv = COL_VAL_NULL(pTColumn->type, pTColumn->colId);
    goto _exit;
  }

H
Hongze Cheng 已提交
517
  if (pIter->pRow->flag & 0xf0) {  // KV
H
Hongze Cheng 已提交
518 519 520
    if (pIter->iCol < pIter->pIdx->nCol) {
      uint8_t *pData = pIter->pv + pIter->pIdx->idx[pIter->iCol];  // todo
      int16_t  cid;
H
Hongze Cheng 已提交
521

H
Hongze Cheng 已提交
522
      pData += tGetI16v(pData, &cid);
H
Hongze Cheng 已提交
523

H
Hongze Cheng 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
      if (TABS(cid) == pTColumn->colId) {
        if (cid < 0) {
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
        } else {
          pIter->cv.cid = pTColumn->colId;
          pIter->cv.type = pTColumn->type;
          pIter->cv.flag = CV_FLAG_VALUE;

          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
            pData += tGetU32v(pData, &pIter->cv.value.nData);
            if (pIter->cv.value.nData > 0) {
              pIter->cv.value.pData = pData;
            } else {
              pIter->cv.value.pData = NULL;
            }
          } else {
            memcpy(&pIter->cv.value.val, pData, pTColumn->bytes);
          }
        }

        pIter->iCol++;
        goto _exit;
      } else if (TABS(cid) > pTColumn->colId) {
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
        goto _exit;
      } else {
        ASSERT(0);
      }
    } else {
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
      goto _exit;
    }
H
Hongze Cheng 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569
  } else {  // Tuple
    // todo
  }

_exit:
  pIter->iTColumn++;
  return &pIter->cv;
}

SColVal *tRowIterNextNotNone(SRowIter *pIter) {
  // todo
  return NULL;
}

H
Hongze Cheng 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
#if 0
static void setBitMap(uint8_t *pb, uint8_t v, int32_t idx, uint8_t flags) {
  if (pb) {
    switch (flags & 0xf) {
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
      case TSROW_HAS_VAL | TSROW_HAS_NONE:
        if (v) {
          SET_BIT1(pb, idx, (uint8_t)1);
        } else {
          SET_BIT1(pb, idx, (uint8_t)0);
        }
        break;
      case TSROW_HAS_VAL | TSROW_HAS_NULL:
        v = v - 1;
        SET_BIT1(pb, idx, v);
        break;
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
        SET_BIT2(pb, idx, v);
        break;
H
Hongze Cheng 已提交
589

H
Hongze Cheng 已提交
590 591 592
      default:
        ASSERT(0);
    }
H
Hongze Cheng 已提交
593 594
  }
}
H
Hongze Cheng 已提交
595 596 597 598 599 600 601 602 603 604
#define SET_IDX(p, i, n, f)        \
  do {                             \
    if ((f)&TSROW_KV_SMALL) {      \
      ((uint8_t *)(p))[i] = (n);   \
    } else if ((f)&TSROW_KV_MID) { \
      ((uint16_t *)(p))[i] = (n);  \
    } else {                       \
      ((uint32_t *)(p))[i] = (n);  \
    }                              \
  } while (0)
H
Hongze Cheng 已提交
605

H
Hongze Cheng 已提交
606
void tTSRowFree(SRow *pRow) {
H
Hongze Cheng 已提交
607 608 609
  if (pRow) {
    taosMemoryFree(pRow);
  }
H
Hongze Cheng 已提交
610 611
}

H
Hongze Cheng 已提交
612
void tTSRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
C
Cary Xu 已提交
613
  uint8_t   isTuple = ((pRow->flags & 0xf0) == 0) ? 1 : 0;
H
Hongze Cheng 已提交
614
  STColumn *pTColumn = &pTSchema->columns[iCol];
H
Hongze Cheng 已提交
615 616
  uint8_t   flags = pRow->flags & (uint8_t)0xf;
  SValue    value;
H
Hongze Cheng 已提交
617

H
Hongze Cheng 已提交
618
  ASSERT(iCol < pTSchema->numOfCols);
C
Cary Xu 已提交
619
  ASSERT(flags);
H
Hongze Cheng 已提交
620
  ASSERT(pRow->sver == pTSchema->version);
H
Hongze Cheng 已提交
621

H
Hongze Cheng 已提交
622 623 624
  if (iCol == 0) {
    value.ts = pRow->ts;
    goto _return_value;
H
Hongze Cheng 已提交
625 626
  }

H
Hongze Cheng 已提交
627
  if (flags == TSROW_HAS_NONE) {
H
more  
Hongze Cheng 已提交
628
    goto _return_none;
C
Cary Xu 已提交
629
  } else if (flags == TSROW_HAS_NULL) {
H
more  
Hongze Cheng 已提交
630
    goto _return_null;
H
Hongze Cheng 已提交
631
  }
H
Hongze Cheng 已提交
632

H
Hongze Cheng 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646
  ASSERT(pRow->nData && pRow->pData);

  if (isTuple) {
    uint8_t *pb = pRow->pData;
    uint8_t *pf = NULL;
    uint8_t *pv = NULL;
    uint8_t *p;
    uint8_t  b;

    // bit
    switch (flags) {
      case TSROW_HAS_VAL:
        pf = pb;
        break;
H
Hongze Cheng 已提交
647
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
648 649 650
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
H
Hongze Cheng 已提交
651
        } else {
H
Hongze Cheng 已提交
652
          goto _return_null;
H
Hongze Cheng 已提交
653
        }
H
Hongze Cheng 已提交
654
      case TSROW_HAS_VAL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
655 656 657
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
H
Hongze Cheng 已提交
658
        } else {
H
Hongze Cheng 已提交
659 660
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
661
        }
H
Hongze Cheng 已提交
662
      case TSROW_HAS_VAL | TSROW_HAS_NULL:
H
Hongze Cheng 已提交
663 664 665
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_null;
H
Hongze Cheng 已提交
666
        } else {
H
Hongze Cheng 已提交
667 668
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
669
        }
H
Hongze Cheng 已提交
670
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
671 672 673 674 675
        b = GET_BIT2(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
        } else if (b == 1) {
          goto _return_null;
H
Hongze Cheng 已提交
676
        } else {
H
Hongze Cheng 已提交
677 678
          pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
679
        }
H
Hongze Cheng 已提交
680
      default:
H
Hongze Cheng 已提交
681
        ASSERT(0);
H
Hongze Cheng 已提交
682
    }
H
Hongze Cheng 已提交
683

H
Hongze Cheng 已提交
684 685 686
    ASSERT(pf);

    p = pf + pTColumn->offset;
H
Hongze Cheng 已提交
687
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
H
Hongze Cheng 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
      pv = pf + pTSchema->flen;
      p = pv + *(VarDataOffsetT *)p;
    }
    tGetValue(p, &value, pTColumn->type);
    goto _return_value;
  } else {
    STSKVRow *pRowK = (STSKVRow *)pRow->pData;
    int16_t   lidx = 0;
    int16_t   ridx = pRowK->nCols - 1;
    uint8_t  *p;
    int16_t   midx;
    uint32_t  n;
    int16_t   cid;

    ASSERT(pRowK->nCols > 0);

    if (pRow->flags & TSROW_KV_SMALL) {
      p = pRow->pData + sizeof(STSKVRow) + sizeof(uint8_t) * pRowK->nCols;
    } else if (pRow->flags & TSROW_KV_MID) {
      p = pRow->pData + sizeof(STSKVRow) + sizeof(uint16_t) * pRowK->nCols;
    } else if (pRow->flags & TSROW_KV_BIG) {
      p = pRow->pData + sizeof(STSKVRow) + sizeof(uint32_t) * pRowK->nCols;
H
Hongze Cheng 已提交
710
    } else {
H
Hongze Cheng 已提交
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
      ASSERT(0);
    }
    while (lidx <= ridx) {
      midx = (lidx + ridx) / 2;

      if (pRow->flags & TSROW_KV_SMALL) {
        n = ((uint8_t *)pRowK->idx)[midx];
      } else if (pRow->flags & TSROW_KV_MID) {
        n = ((uint16_t *)pRowK->idx)[midx];
      } else {
        n = ((uint32_t *)pRowK->idx)[midx];
      }

      n += tGetI16v(p + n, &cid);

      if (TABS(cid) == pTColumn->colId) {
        if (cid < 0) {
          goto _return_null;
        } else {
          n += tGetValue(p + n, &value, pTColumn->type);
          goto _return_value;
        }

        return;
      } else if (TABS(cid) > pTColumn->colId) {
        ridx = midx - 1;
      } else {
        lidx = midx + 1;
      }
H
Hongze Cheng 已提交
740
    }
H
Hongze Cheng 已提交
741 742 743

    // not found, return NONE
    goto _return_none;
H
Hongze Cheng 已提交
744
  }
H
Hongze Cheng 已提交
745

H
Hongze Cheng 已提交
746
_return_none:
H
Hongze Cheng 已提交
747
  *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
748 749 750
  return;

_return_null:
H
Hongze Cheng 已提交
751
  *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
752 753 754
  return;

_return_value:
H
Hongze Cheng 已提交
755
  *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, value);
H
Hongze Cheng 已提交
756 757 758
  return;
}

H
Hongze Cheng 已提交
759
int32_t tTSRowToArray(SRow *pRow, STSchema *pTSchema, SArray **ppArray) {
H
Hongze Cheng 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773
  int32_t code = 0;
  SColVal cv;

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

  for (int32_t iColumn = 0; iColumn < pTSchema->numOfCols; iColumn++) {
    tTSRowGet(pRow, pTSchema, iColumn, &cv);
    taosArrayPush(*ppArray, &cv);
  }

H
Haojun Liao 已提交
774

H
Hongze Cheng 已提交
775 776 777
_exit:
  return code;
}
H
Haojun Liao 已提交
778
#endif
H
Hongze Cheng 已提交
779

H
Hongze Cheng 已提交
780
// STSchema ========================================
H
Hongze Cheng 已提交
781 782 783
void tTSchemaDestroy(STSchema *pTSchema) {
  if (pTSchema) taosMemoryFree(pTSchema);
}
H
Hongze Cheng 已提交
784

H
Hongze Cheng 已提交
785
// STag ========================================
H
Hongze Cheng 已提交
786 787
static int tTagValCmprFn(const void *p1, const void *p2) {
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
788
    return -1;
H
Hongze Cheng 已提交
789
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
790 791
    return 1;
  }
H
Hongze Cheng 已提交
792

H
Hongze Cheng 已提交
793 794
  return 0;
}
H
Hongze Cheng 已提交
795 796 797
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
}
C
Cary Xu 已提交
798 799 800 801 802 803 804

static void debugPrintTagVal(int8_t type, const void *val, int32_t vlen, const char *tag, int32_t ln) {
  switch (type) {
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_NCHAR: {
      char tmpVal[32] = {0};
805
      strncpy(tmpVal, val, vlen > 31 ? 31 : vlen);
C
Cary Xu 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
      printf("%s:%d type:%d vlen:%d, val:\"%s\"\n", tag, ln, (int32_t)type, vlen, tmpVal);
    } break;
    case TSDB_DATA_TYPE_FLOAT:
      printf("%s:%d type:%d vlen:%d, val:%f\n", tag, ln, (int32_t)type, vlen, *(float *)val);
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      printf("%s:%d type:%d vlen:%d, val:%lf\n", tag, ln, (int32_t)type, vlen, *(double *)val);
      break;
    case TSDB_DATA_TYPE_BOOL:
      printf("%s:%d type:%d vlen:%d, val:%" PRIu8 "\n", tag, ln, (int32_t)type, vlen, *(uint8_t *)val);
      break;
    case TSDB_DATA_TYPE_TINYINT:
      printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val);
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      printf("%s:%d type:%d vlen:%d, val:%" PRIi16 "\n", tag, ln, (int32_t)type, vlen, *(int16_t *)val);
      break;
    case TSDB_DATA_TYPE_INT:
      printf("%s:%d type:%d vlen:%d, val:%" PRIi32 "\n", tag, ln, (int32_t)type, vlen, *(int32_t *)val);
      break;
    case TSDB_DATA_TYPE_BIGINT:
      printf("%s:%d type:%d vlen:%d, val:%" PRIi64 "\n", tag, ln, (int32_t)type, vlen, *(int64_t *)val);
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      printf("%s:%d type:%d vlen:%d, val:%" PRIi64 "\n", tag, ln, (int32_t)type, vlen, *(int64_t *)val);
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      printf("%s:%d type:%d vlen:%d, val:%" PRIu8 "\n", tag, ln, (int32_t)type, vlen, *(uint8_t *)val);
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      printf("%s:%d type:%d vlen:%d, val:%" PRIu16 "\n", tag, ln, (int32_t)type, vlen, *(uint16_t *)val);
      break;
    case TSDB_DATA_TYPE_UINT:
      printf("%s:%d type:%d vlen:%d, val:%" PRIu32 "\n", tag, ln, (int32_t)type, vlen, *(uint32_t *)val);
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      printf("%s:%d type:%d vlen:%d, val:%" PRIu64 "\n", tag, ln, (int32_t)type, vlen, *(uint64_t *)val);
      break;
wmmhello's avatar
wmmhello 已提交
844 845 846
    case TSDB_DATA_TYPE_NULL:
      printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val);
      break;
C
Cary Xu 已提交
847 848 849 850 851 852 853
    default:
      ASSERT(0);
      break;
  }
}

void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
H
Hongze Cheng 已提交
854 855
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
856 857 858 859 860 861 862 863 864 865
  uint8_t *p = NULL;
  int16_t  offset = 0;

  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }
  printf("%s:%d >>> STAG === %s:%s, len: %d, nTag: %d, sver:%d\n", tag, ln, isJson ? "json" : "normal",
         isLarge ? "large" : "small", (int32_t)pTag->len, (int32_t)pTag->nTag, pTag->ver);
C
Cary Xu 已提交
866
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
867 868 869 870 871 872 873 874
    if (isLarge) {
      offset = ((int16_t *)pTag->idx)[n];
    } else {
      offset = pTag->idx[n];
    }
    STagVal tagVal = {0};
    if (isJson) {
      tagVal.pKey = (char *)POINTER_SHIFT(p, offset);
C
Cary Xu 已提交
875
    } else {
876
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
C
Cary Xu 已提交
877
    }
878
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
C
Cary Xu 已提交
879
    tGetTagVal(p + offset, &tagVal, isJson);
880
    if (IS_VAR_DATA_TYPE(tagVal.type)) {
wmmhello's avatar
wmmhello 已提交
881
      debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
882
    } else {
wmmhello's avatar
wmmhello 已提交
883 884
      debugPrintTagVal(tagVal.type, &tagVal.i64, tDataTypes[tagVal.type].bytes, __func__, __LINE__);
    }
C
Cary Xu 已提交
885 886 887 888
  }
  printf("\n");
}

H
Hongze Cheng 已提交
889 890
static int32_t tPutTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
  int32_t n = 0;
H
Hongze Cheng 已提交
891

H
Hongze Cheng 已提交
892 893 894 895 896 897
  // key
  if (isJson) {
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
  } else {
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
  }
H
Hongze Cheng 已提交
898

H
Hongze Cheng 已提交
899 900 901 902 903 904 905
  // type
  n += tPutI8(p ? p + n : p, pTagVal->type);

  // value
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
  } else {
H
Hongze Cheng 已提交
906
    p = p ? p + n : p;
wmmhello's avatar
wmmhello 已提交
907
    n += tDataTypes[pTagVal->type].bytes;
908
    if (p) memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
H
Hongze Cheng 已提交
909 910
  }

H
Hongze Cheng 已提交
911 912 913 914 915 916 917 918 919 920
  return n;
}
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
  int32_t n = 0;

  // key
  if (isJson) {
    n += tGetCStr(p + n, &pTagVal->pKey);
  } else {
    n += tGetI16v(p + n, &pTagVal->cid);
H
Hongze Cheng 已提交
921 922
  }

H
Hongze Cheng 已提交
923 924
  // type
  n += tGetI8(p + n, &pTagVal->type);
H
Hongze Cheng 已提交
925

H
Hongze Cheng 已提交
926 927 928 929
  // value
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
  } else {
wmmhello's avatar
wmmhello 已提交
930
    memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
wmmhello's avatar
wmmhello 已提交
931
    n += tDataTypes[pTagVal->type].bytes;
H
Hongze Cheng 已提交
932 933 934 935
  }

  return n;
}
wmmhello's avatar
wmmhello 已提交
936

H
Hongze Cheng 已提交
937
bool tTagIsJson(const void *pTag) { return (((const STag *)pTag)->flags & TD_TAG_JSON); }
wmmhello's avatar
wmmhello 已提交
938

H
Hongze Cheng 已提交
939 940 941 942 943
bool tTagIsJsonNull(void *data) {
  STag  *pTag = (STag *)data;
  int8_t isJson = tTagIsJson(pTag);
  if (!isJson) return false;
  return ((STag *)data)->nTag == 0;
wmmhello's avatar
wmmhello 已提交
944 945
}

H
Hongze Cheng 已提交
946
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
H
Hongze Cheng 已提交
947 948 949
  int32_t  code = 0;
  uint8_t *p = NULL;
  int16_t  n = 0;
H
Hongze Cheng 已提交
950
  int16_t  nTag = taosArrayGetSize(pArray);
H
Hongze Cheng 已提交
951 952
  int32_t  szTag = 0;
  int8_t   isLarge = 0;
H
Hongze Cheng 已提交
953 954 955

  // sort
  if (isJson) {
wafwerar's avatar
wafwerar 已提交
956
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
H
Hongze Cheng 已提交
957
  } else {
wafwerar's avatar
wafwerar 已提交
958
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
H
Hongze Cheng 已提交
959 960 961
  }

  // get size
H
Hongze Cheng 已提交
962
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
963
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
964
  }
H
Hongze Cheng 已提交
965 966 967 968 969 970
  if (szTag <= INT8_MAX) {
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
  } else {
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
    isLarge = 1;
  }
H
Hongze Cheng 已提交
971

H
Hongze Cheng 已提交
972
  ASSERT(szTag <= INT16_MAX);
H
Hongze Cheng 已提交
973 974

  // build tag
C
Cary Xu 已提交
975
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
H
Hongze Cheng 已提交
976 977 978 979
  if ((*ppTag) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
980 981 982 983 984 985 986
  (*ppTag)->flags = 0;
  if (isJson) {
    (*ppTag)->flags |= TD_TAG_JSON;
  }
  if (isLarge) {
    (*ppTag)->flags |= TD_TAG_LARGE;
  }
H
Hongze Cheng 已提交
987 988 989
  (*ppTag)->len = szTag;
  (*ppTag)->nTag = nTag;
  (*ppTag)->ver = version;
H
Hongze Cheng 已提交
990

H
Hongze Cheng 已提交
991 992 993 994 995
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
  } else {
    p = (uint8_t *)&(*ppTag)->idx[nTag];
  }
H
Hongze Cheng 已提交
996 997
  n = 0;
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
998 999 1000 1001 1002
    if (isLarge) {
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
    } else {
      (*ppTag)->idx[iTag] = n;
    }
H
Hongze Cheng 已提交
1003
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
1004
  }
1005
#ifdef TD_DEBUG_PRINT_TAG
C
Cary Xu 已提交
1006
  debugPrintSTag(*ppTag, __func__, __LINE__);
1007
#endif
C
Cary Xu 已提交
1008

H
Hongze Cheng 已提交
1009 1010 1011 1012
  return code;

_err:
  return code;
H
Hongze Cheng 已提交
1013 1014 1015 1016 1017 1018
}

void tTagFree(STag *pTag) {
  if (pTag) taosMemoryFree(pTag);
}

1019
char *tTagValToData(const STagVal *value, bool isJson) {
H
Haojun Liao 已提交
1020 1021 1022 1023
  if (!value) {
    return NULL;
  }

1024
  char  *data = NULL;
wmmhello's avatar
wmmhello 已提交
1025 1026 1027 1028
  int8_t typeBytes = 0;
  if (isJson) {
    typeBytes = CHAR_BYTES;
  }
H
Haojun Liao 已提交
1029

1030
  if (IS_VAR_DATA_TYPE(value->type)) {
wmmhello's avatar
wmmhello 已提交
1031
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
H
Haojun Liao 已提交
1032 1033 1034 1035 1036 1037 1038 1039
    if (data == NULL) {
      return NULL;
    }

    if (isJson) {
      *data = value->type;
    }

wmmhello's avatar
wmmhello 已提交
1040 1041
    varDataLen(data + typeBytes) = value->nData;
    memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
1042 1043
  } else {
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
wmmhello's avatar
wmmhello 已提交
1044 1045 1046 1047 1048
  }

  return data;
}

C
Cary Xu 已提交
1049
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
H
Hongze Cheng 已提交
1050
  if (!pTag || !pTagVal) {
wmmhello's avatar
wmmhello 已提交
1051 1052 1053
    return false;
  }

H
Hongze Cheng 已提交
1054 1055 1056
  int16_t  lidx = 0;
  int16_t  ridx = pTag->nTag - 1;
  int16_t  midx;
H
Hongze Cheng 已提交
1057 1058 1059 1060
  uint8_t *p;
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
  int16_t  offset;
H
Hongze Cheng 已提交
1061 1062 1063
  STagVal  tv;
  int      c;

H
Hongze Cheng 已提交
1064 1065 1066 1067 1068 1069
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }

H
Hongze Cheng 已提交
1070 1071 1072 1073 1074
  pTagVal->type = TSDB_DATA_TYPE_NULL;
  pTagVal->pData = NULL;
  pTagVal->nData = 0;
  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;
H
Hongze Cheng 已提交
1075 1076 1077 1078 1079
    if (isLarge) {
      offset = ((int16_t *)pTag->idx)[midx];
    } else {
      offset = pTag->idx[midx];
    }
H
Hongze Cheng 已提交
1080

H
Hongze Cheng 已提交
1081 1082
    tGetTagVal(p + offset, &tv, isJson);
    if (isJson) {
H
Hongze Cheng 已提交
1083
      c = tTagValJsonCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1084
    } else {
H
Hongze Cheng 已提交
1085
      c = tTagValCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1086 1087
    }

H
Hongze Cheng 已提交
1088 1089 1090 1091
    if (c < 0) {
      ridx = midx - 1;
    } else if (c > 0) {
      lidx = midx + 1;
H
Hongze Cheng 已提交
1092
    } else {
H
Hongze Cheng 已提交
1093
      memcpy(pTagVal, &tv, sizeof(tv));
C
Cary Xu 已提交
1094
      return true;
H
Hongze Cheng 已提交
1095 1096
    }
  }
C
Cary Xu 已提交
1097
  return false;
H
Hongze Cheng 已提交
1098 1099
}

H
more  
Hongze Cheng 已提交
1100 1101
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
H
Hongze Cheng 已提交
1102 1103
}

H
more  
Hongze Cheng 已提交
1104
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
H
Hongze Cheng 已提交
1105

C
Cary Xu 已提交
1106
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
H
Hongze Cheng 已提交
1107
  int32_t  code = 0;
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  uint8_t *p = NULL;
  STagVal  tv = {0};
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
  int16_t  offset = 0;

  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }
H
Hongze Cheng 已提交
1118

H
more  
Hongze Cheng 已提交
1119
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
H
Hongze Cheng 已提交
1120
  if (*ppArray == NULL) {
H
Hongze Cheng 已提交
1121 1122 1123 1124 1125
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
1126 1127 1128 1129 1130 1131
    if (isLarge) {
      offset = ((int16_t *)pTag->idx)[iTag];
    } else {
      offset = pTag->idx[iTag];
    }
    tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
H
Hongze Cheng 已提交
1132
    taosArrayPush(*ppArray, &tv);
H
Hongze Cheng 已提交
1133 1134 1135 1136 1137 1138 1139 1140
  }

  return code;

_err:
  return code;
}

wmmhello's avatar
wmmhello 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
void tTagSetCid(const STag *pTag, int16_t iTag, int16_t cid) {
  uint8_t *p = NULL;
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
  int16_t  offset = 0;

  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }

  if (isLarge) {
    offset = ((int16_t *)pTag->idx)[iTag];
  } else {
    offset = pTag->idx[iTag];
  }

  tPutI16v(p + offset, cid);
}

H
Hongze Cheng 已提交
1161
#if 1  // ===================================================================================================================
C
Cary Xu 已提交
1162
int tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
1163 1164 1165
  if (pBuilder == NULL) return -1;

  pBuilder->tCols = 256;
wafwerar's avatar
wafwerar 已提交
1166
  pBuilder->columns = (STColumn *)taosMemoryMalloc(sizeof(STColumn) * pBuilder->tCols);
H
Hongze Cheng 已提交
1167 1168 1169 1170 1171 1172 1173 1174
  if (pBuilder->columns == NULL) return -1;

  tdResetTSchemaBuilder(pBuilder, version);
  return 0;
}

void tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder) {
  if (pBuilder) {
wafwerar's avatar
wafwerar 已提交
1175
    taosMemoryFreeClear(pBuilder->columns);
H
Hongze Cheng 已提交
1176 1177 1178
  }
}

C
Cary Xu 已提交
1179
void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
1180 1181 1182
  pBuilder->nCols = 0;
  pBuilder->tlen = 0;
  pBuilder->flen = 0;
T
Tao Liu 已提交
1183
  pBuilder->vlen = 0;
H
Hongze Cheng 已提交
1184 1185 1186
  pBuilder->version = version;
}

C
Cary Xu 已提交
1187
int32_t tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes) {
1188
  if (!isValidDataType(type)) return -1;
H
Hongze Cheng 已提交
1189 1190 1191

  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
wafwerar's avatar
wafwerar 已提交
1192
    STColumn *columns = (STColumn *)taosMemoryRealloc(pBuilder->columns, sizeof(STColumn) * pBuilder->tCols);
T
tickduan 已提交
1193 1194
    if (columns == NULL) return -1;
    pBuilder->columns = columns;
H
Hongze Cheng 已提交
1195 1196 1197
  }

  STColumn *pCol = &(pBuilder->columns[pBuilder->nCols]);
H
Hongze Cheng 已提交
1198 1199 1200
  pCol->type = type;
  pCol->colId = colId;
  pCol->flags = flags;
H
Hongze Cheng 已提交
1201
  if (pBuilder->nCols == 0) {
H
Hongze Cheng 已提交
1202
    pCol->offset = 0;
H
Hongze Cheng 已提交
1203
  } else {
S
Shengliang Guan 已提交
1204
    STColumn *pTCol = &(pBuilder->columns[pBuilder->nCols - 1]);
H
Hongze Cheng 已提交
1205
    pCol->offset = pTCol->offset + TYPE_BYTES[pTCol->type];
H
Hongze Cheng 已提交
1206 1207 1208
  }

  if (IS_VAR_DATA_TYPE(type)) {
H
Hongze Cheng 已提交
1209
    pCol->bytes = bytes;
T
Tao Liu 已提交
1210 1211
    pBuilder->tlen += (TYPE_BYTES[type] + bytes);
    pBuilder->vlen += bytes - sizeof(VarDataLenT);
H
Hongze Cheng 已提交
1212
  } else {
H
Hongze Cheng 已提交
1213
    pCol->bytes = TYPE_BYTES[type];
H
Hongze Cheng 已提交
1214
    pBuilder->tlen += TYPE_BYTES[type];
T
Tao Liu 已提交
1215
    pBuilder->vlen += TYPE_BYTES[type];
H
Hongze Cheng 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
  }

  pBuilder->nCols++;
  pBuilder->flen += TYPE_BYTES[type];

  ASSERT(pCol->offset < pBuilder->flen);

  return 0;
}

STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder) {
  if (pBuilder->nCols <= 0) return NULL;

  int tlen = sizeof(STSchema) + sizeof(STColumn) * pBuilder->nCols;

wafwerar's avatar
wafwerar 已提交
1231
  STSchema *pSchema = (STSchema *)taosMemoryMalloc(tlen);
H
Hongze Cheng 已提交
1232 1233
  if (pSchema == NULL) return NULL;

H
Hongze Cheng 已提交
1234 1235 1236 1237 1238
  pSchema->version = pBuilder->version;
  pSchema->numOfCols = pBuilder->nCols;
  pSchema->tlen = pBuilder->tlen;
  pSchema->flen = pBuilder->flen;
  pSchema->vlen = pBuilder->vlen;
H
Hongze Cheng 已提交
1239

C
Cary Xu 已提交
1240
#ifdef TD_SUPPORT_BITMAP
H
Hongze Cheng 已提交
1241
  pSchema->tlen += (int)TD_BITMAP_BYTES(pSchema->numOfCols);
C
Cary Xu 已提交
1242 1243
#endif

H
Hongze Cheng 已提交
1244
  memcpy(&pSchema->columns[0], pBuilder->columns, sizeof(STColumn) * pBuilder->nCols);
H
Hongze Cheng 已提交
1245

H
TD-27  
hzcheng 已提交
1246 1247 1248
  return pSchema;
}

H
refact  
Hongze Cheng 已提交
1249 1250 1251
#endif

// SColData ========================================
H
Hongze Cheng 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
void tColDataDestroy(void *ph) {
  SColData *pColData = (SColData *)ph;

  tFree(pColData->pBitMap);
  tFree((uint8_t *)pColData->aOffset);
  tFree(pColData->pData);
}

void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t smaOn) {
  pColData->cid = cid;
  pColData->type = type;
  pColData->smaOn = smaOn;
H
Hongze Cheng 已提交
1264
  tColDataClear(pColData);
H
Hongze Cheng 已提交
1265 1266
}

H
Hongze Cheng 已提交
1267
void tColDataClear(SColData *pColData) {
H
Hongze Cheng 已提交
1268 1269 1270 1271 1272
  pColData->nVal = 0;
  pColData->flag = 0;
  pColData->nData = 0;
}

H
Hongze Cheng 已提交
1273 1274 1275 1276 1277 1278 1279 1280
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;

  if (IS_VAR_DATA_TYPE(pColData->type)) {
    code = tRealloc((uint8_t **)(&pColData->aOffset), sizeof(int32_t) * (pColData->nVal + 1));
    if (code) goto _exit;
    pColData->aOffset[pColData->nVal] = pColData->nData;

H
Hongze Cheng 已提交
1281
    if (pColVal->value.nData) {
H
Hongze Cheng 已提交
1282 1283 1284 1285 1286 1287
      code = tRealloc(&pColData->pData, pColData->nData + pColVal->value.nData);
      if (code) goto _exit;
      memcpy(pColData->pData + pColData->nData, pColVal->value.pData, pColVal->value.nData);
      pColData->nData += pColVal->value.nData;
    }
  } else {
H
Hongze Cheng 已提交
1288
    ASSERT(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal);
H
Hongze Cheng 已提交
1289 1290
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1291 1292
    memcpy(pColData->pData + pColData->nData, &pColVal->value.val, tDataTypes[pColData->type].bytes);
    pColData->nData += tDataTypes[pColData->type].bytes;
H
Hongze Cheng 已提交
1293
  }
H
Hongze Cheng 已提交
1294
  pColData->nVal++;
H
Hongze Cheng 已提交
1295 1296 1297 1298

_exit:
  return code;
}
H
Hongze Cheng 已提交
1299 1300
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, SColVal *pColVal) {
  pColData->flag = HAS_VALUE;
H
Hongze Cheng 已提交
1301
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1302 1303 1304 1305
}
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, SColVal *pColVal) {
  pColData->flag = HAS_NONE;
  pColData->nVal++;
H
Hongze Cheng 已提交
1306
  return 0;
H
Hongze Cheng 已提交
1307 1308 1309 1310
}
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, SColVal *pColVal) {
  pColData->flag = HAS_NULL;
  pColData->nVal++;
H
Hongze Cheng 已提交
1311
  return 0;
H
Hongze Cheng 已提交
1312 1313
}
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1314 1315
  int32_t code = 0;

H
Hongze Cheng 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;

  memset(pColData->pBitMap, 0, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);

  pColData->flag |= HAS_VALUE;

  if (pColData->nVal) {
    if (IS_VAR_DATA_TYPE(pColData->type)) {
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
      if (code) return code;
      memset(pColData->aOffset, 0, nOffset);
    } else {
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
      code = tRealloc(&pColData->pData, pColData->nData);
      if (code) return code;
      memset(pColData->pData, 0, pColData->nData);
    }
H
Hongze Cheng 已提交
1337
  }
H
Hongze Cheng 已提交
1338

H
Hongze Cheng 已提交
1339
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1340
}
H
Hongze Cheng 已提交
1341 1342
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, SColVal *pColVal) {
  pColData->nVal++;
H
Hongze Cheng 已提交
1343
  return 0;
H
Hongze Cheng 已提交
1344 1345
}
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1346 1347
  int32_t code = 0;

H
Hongze Cheng 已提交
1348 1349 1350
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1351

H
Hongze Cheng 已提交
1352 1353
  memset(pColData->pBitMap, 0, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1354

H
Hongze Cheng 已提交
1355 1356
  pColData->flag |= HAS_NULL;
  pColData->nVal++;
H
Hongze Cheng 已提交
1357

H
Hongze Cheng 已提交
1358 1359 1360 1361
  return code;
}
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1362

H
Hongze Cheng 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;

  memset(pColData->pBitMap, 0, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);

  pColData->flag |= HAS_VALUE;

  if (pColData->nVal) {
    if (IS_VAR_DATA_TYPE(pColData->type)) {
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
      if (code) return code;
      memset(pColData->aOffset, 0, nOffset);
    } else {
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
      code = tRealloc(&pColData->pData, pColData->nData);
      if (code) return code;
      memset(pColData->pData, 0, pColData->nData);
H
Hongze Cheng 已提交
1383
    }
H
Hongze Cheng 已提交
1384
  }
H
Hongze Cheng 已提交
1385

H
Hongze Cheng 已提交
1386
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1387
}
H
Hongze Cheng 已提交
1388
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1389 1390
  int32_t code = 0;

H
Hongze Cheng 已提交
1391 1392 1393
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1394

H
Hongze Cheng 已提交
1395 1396
  memset(pColData->pBitMap, 255, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1397

H
Hongze Cheng 已提交
1398 1399
  pColData->flag |= HAS_NONE;
  pColData->nVal++;
H
Hongze Cheng 已提交
1400

H
Hongze Cheng 已提交
1401 1402 1403 1404
  return code;
}
static FORCE_INLINE int32_t tColDataAppendValue22(SColData *pColData, SColVal *pColVal) {
  pColData->nVal++;
H
Hongze Cheng 已提交
1405
  return 0;
H
Hongze Cheng 已提交
1406 1407 1408
}
static FORCE_INLINE int32_t tColDataAppendValue30(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1409

H
Hongze Cheng 已提交
1410
  pColData->flag |= HAS_VALUE;
H
Hongze Cheng 已提交
1411

H
Hongze Cheng 已提交
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  uint8_t *pBitMap = NULL;
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
  if (code) return code;

  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
    SET_BIT2(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
  }
  SET_BIT2(pBitMap, pColData->nVal, 2);

  tFree(pColData->pBitMap);
  pColData->pBitMap = pBitMap;

  if (pColData->nVal) {
    if (IS_VAR_DATA_TYPE(pColData->type)) {
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
      if (code) return code;
      memset(pColData->aOffset, 0, nOffset);
    } else {
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
      code = tRealloc(&pColData->pData, pColData->nData);
      if (code) return code;
      memset(pColData->pData, 0, pColData->nData);
H
Hongze Cheng 已提交
1435
    }
H
Hongze Cheng 已提交
1436
  }
H
Hongze Cheng 已提交
1437

H
Hongze Cheng 已提交
1438
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1439
}
H
Hongze Cheng 已提交
1440
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1441 1442
  int32_t code = 0;

H
Hongze Cheng 已提交
1443 1444
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;
H
Hongze Cheng 已提交
1445

H
Hongze Cheng 已提交
1446 1447
  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
  pColData->nVal++;
H
Hongze Cheng 已提交
1448

H
Hongze Cheng 已提交
1449 1450 1451 1452
  return code;
}
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1453

H
Hongze Cheng 已提交
1454 1455
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;
H
Hongze Cheng 已提交
1456

H
Hongze Cheng 已提交
1457 1458
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
  pColData->nVal++;
H
Hongze Cheng 已提交
1459

H
Hongze Cheng 已提交
1460 1461
  return code;
}
H
Hongze Cheng 已提交
1462
#define tColDataAppendValue40 tColDataPutValue
H
Hongze Cheng 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;

  pColData->flag |= HAS_NONE;

  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;

  memset(pColData->pBitMap, 255, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);

H
Hongze Cheng 已提交
1475
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1476
}
H
Hongze Cheng 已提交
1477
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1478 1479
  int32_t code = 0;

H
Hongze Cheng 已提交
1480
  pColData->flag |= HAS_NULL;
H
Hongze Cheng 已提交
1481

H
Hongze Cheng 已提交
1482 1483 1484
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1485

H
Hongze Cheng 已提交
1486 1487 1488
  memset(pColData->pBitMap, 255, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);

H
Hongze Cheng 已提交
1489
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1490
}
H
Hongze Cheng 已提交
1491
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1492 1493
  int32_t code = 0;

H
Hongze Cheng 已提交
1494 1495
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;
H
Hongze Cheng 已提交
1496

H
Hongze Cheng 已提交
1497
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1498

H
Hongze Cheng 已提交
1499
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508
}
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;

  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;

  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);

H
Hongze Cheng 已提交
1509
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
}
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;

  pColData->flag |= HAS_NULL;

  uint8_t *pBitMap = NULL;
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
  if (code) return code;

  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
    SET_BIT2(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
H
Hongze Cheng 已提交
1522
  }
H
Hongze Cheng 已提交
1523 1524 1525 1526 1527
  SET_BIT2(pBitMap, pColData->nVal, 1);

  tFree(pColData->pBitMap);
  pColData->pBitMap = pBitMap;

H
Hongze Cheng 已提交
1528
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1529
}
H
Hongze Cheng 已提交
1530
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1531 1532
  int32_t code = 0;

H
Hongze Cheng 已提交
1533 1534 1535
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1536

H
Hongze Cheng 已提交
1537
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1538 1539 1540
}
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;
H
Hongze Cheng 已提交
1541

H
Hongze Cheng 已提交
1542 1543 1544 1545 1546 1547 1548 1549
  pColData->flag |= HAS_NONE;

  uint8_t *pBitMap = NULL;
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
  if (code) return code;

  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
    SET_BIT2(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
H
Hongze Cheng 已提交
1550
  }
H
Hongze Cheng 已提交
1551 1552 1553 1554 1555
  SET_BIT2(pBitMap, pColData->nVal, 0);

  tFree(pColData->pBitMap);
  pColData->pBitMap = pBitMap;

H
Hongze Cheng 已提交
1556
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1557
}
H
Hongze Cheng 已提交
1558 1559 1560 1561 1562 1563 1564
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;

  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;
  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);

H
Hongze Cheng 已提交
1565
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1566 1567
}
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1568 1569
  int32_t code = 0;

H
Hongze Cheng 已提交
1570
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
H
Hongze Cheng 已提交
1571 1572
  if (code) return code;
  SET_BIT2(pColData->pBitMap, pColData->nVal, 2);
H
Hongze Cheng 已提交
1573

H
Hongze Cheng 已提交
1574
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1575 1576 1577 1578 1579 1580 1581 1582
}
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;

  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
  if (code) return code;
  SET_BIT2(pColData->pBitMap, pColData->nVal, 0);

H
Hongze Cheng 已提交
1583
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1584 1585 1586 1587 1588 1589 1590 1591
}
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;

  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
  if (code) return code;
  SET_BIT2(pColData->pBitMap, pColData->nVal, 1);

H
Hongze Cheng 已提交
1592
  return tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1593
}
H
Hongze Cheng 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, SColVal *pColVal) = {
    {tColDataAppendValue00, tColDataAppendValue01, tColDataAppendValue02},  // 0
    {tColDataAppendValue10, tColDataAppendValue11, tColDataAppendValue12},  // HAS_NONE
    {tColDataAppendValue20, tColDataAppendValue21, tColDataAppendValue22},  // HAS_NULL
    {tColDataAppendValue30, tColDataAppendValue31, tColDataAppendValue32},  // HAS_NULL|HAS_NONE
    {tColDataAppendValue40, tColDataAppendValue41, tColDataAppendValue42},  // HAS_VALUE
    {tColDataAppendValue50, tColDataAppendValue51, tColDataAppendValue52},  // HAS_VALUE|HAS_NONE
    {tColDataAppendValue60, tColDataAppendValue61, tColDataAppendValue62},  // HAS_VALUE|HAS_NULL
    {tColDataAppendValue70, tColDataAppendValue71, tColDataAppendValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
1603
};
H
refact  
Hongze Cheng 已提交
1604
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1605
  ASSERT(pColData->cid == pColVal->cid && pColData->type == pColVal->type);
H
Hongze Cheng 已提交
1606
  return tColDataAppendValueImpl[pColData->flag][pColVal->flag](pColData, pColVal);
H
refact  
Hongze Cheng 已提交
1607 1608
}

H
Hongze Cheng 已提交
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
}
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
}
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL|HAS_NONE
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
    case 0:
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
      break;
    case 1:
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
      break;
    default:
      ASSERT(0);
  }
}
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
  SValue value;
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    if (iVal + 1 < pColData->nVal) {
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
    } else {
      value.nData = pColData->nData - pColData->aOffset[iVal];
    }
    value.pData = pColData->pData + pColData->aOffset[iVal];
  } else {
H
Hongze Cheng 已提交
1637
    memcpy(&value.val, pColData->pData + tDataTypes[pColData->type].bytes * iVal, tDataTypes[pColData->type].bytes);
H
Hongze Cheng 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
  }
  *pColVal = COL_VAL_VALUE(pColData->cid, pColData->type, value);
}
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
    case 0:
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
      break;
    case 1:
      tColDataGetValue4(pColData, iVal, pColVal);
      break;
    default:
      ASSERT(0);
  }
}
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
    case 0:
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
      break;
    case 1:
      tColDataGetValue4(pColData, iVal, pColVal);
      break;
    default:
      ASSERT(0);
  }
}
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
    case 0:
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
      break;
    case 1:
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
      break;
    case 2:
      tColDataGetValue4(pColData, iVal, pColVal);
      break;
    default:
      ASSERT(0);
  }
}
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
    NULL,               // 0
    tColDataGetValue1,  // HAS_NONE
    tColDataGetValue2,  // HAS_NULL
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
    tColDataGetValue4,  // HAS_VALUE
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
};
void tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
H
Hongze Cheng 已提交
1694
  ASSERT(iVal >= 0 && iVal < pColData->nVal && pColData->flag);
H
Hongze Cheng 已提交
1695 1696 1697
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
}

H
Haojun Liao 已提交
1698
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
H
Hongze Cheng 已提交
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
  uint8_t v;
  switch (pColData->flag) {
    case HAS_NONE:
      v = 0;
      break;
    case HAS_NULL:
      v = 1;
      break;
    case (HAS_NULL | HAS_NONE):
      v = GET_BIT1(pColData->pBitMap, iVal);
      break;
    case HAS_VALUE:
      v = 2;
      break;
    case (HAS_VALUE | HAS_NONE):
      v = GET_BIT1(pColData->pBitMap, iVal);
      if (v) v = 2;
      break;
    case (HAS_VALUE | HAS_NULL):
      v = GET_BIT1(pColData->pBitMap, iVal) + 1;
      break;
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
      v = GET_BIT2(pColData->pBitMap, iVal);
      break;
    default:
      ASSERT(0);
      break;
  }
  return v;
}

H
refact  
Hongze Cheng 已提交
1730 1731 1732 1733 1734
int32_t tColDataCopy(SColData *pColDataSrc, SColData *pColDataDest) {
  int32_t code = 0;
  int32_t size;

  ASSERT(pColDataSrc->nVal > 0);
G
Ganlin Zhao 已提交
1735 1736
  ASSERT(pColDataDest->cid == pColDataSrc->cid);
  ASSERT(pColDataDest->type == pColDataSrc->type);
H
refact  
Hongze Cheng 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767

  pColDataDest->smaOn = pColDataSrc->smaOn;
  pColDataDest->nVal = pColDataSrc->nVal;
  pColDataDest->flag = pColDataSrc->flag;

  // bitmap
  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);
  }

  // offset
  if (IS_VAR_DATA_TYPE(pColDataDest->type)) {
    size = sizeof(int32_t) * pColDataSrc->nVal;

    code = tRealloc((uint8_t **)&pColDataDest->aOffset, size);
    if (code) goto _exit;

    memcpy(pColDataDest->aOffset, pColDataSrc->aOffset, size);
  }

  // value
  pColDataDest->nData = pColDataSrc->nData;
  code = tRealloc(&pColDataDest->pData, pColDataSrc->nData);
  if (code) goto _exit;
  memcpy(pColDataDest->pData, pColDataSrc->pData, pColDataDest->nData);

_exit:
  return code;
G
Ganlin Zhao 已提交
1768
}
H
Hongze Cheng 已提交
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794

#define CALC_SUM_MAX_MIN(SUM, MAX, MIN, VAL) \
  do {                                       \
    (SUM) += (VAL);                          \
    if ((MAX) < (VAL)) (MAX) = (VAL);        \
    if ((MIN) > (VAL)) (MIN) = (VAL);        \
  } while (0)

static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                             int16_t *numOfNull) {
  *sum = 0;
  *max = 0;
  *min = 1;
  *numOfNull = 0;

  int8_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
1795
          (*numOfNull)++;
H
Hongze Cheng 已提交
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
          break;
        case 2:
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                                int16_t *numOfNull) {
  *sum = 0;
  *max = INT8_MIN;
  *min = INT8_MAX;
  *numOfNull = 0;

  int8_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((int8_t *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
1827
          (*numOfNull)++;
H
Hongze Cheng 已提交
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
          break;
        case 2:
          val = ((int8_t *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                                     int16_t *numOfNull) {
  *sum = 0;
  *max = INT16_MIN;
  *min = INT16_MAX;
  *numOfNull = 0;

  int16_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((int16_t *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
1859
          (*numOfNull)++;
H
Hongze Cheng 已提交
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
          break;
        case 2:
          val = ((int16_t *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                            int16_t *numOfNull) {
  *sum = 0;
  *max = INT32_MIN;
  *min = INT32_MAX;
  *numOfNull = 0;

  int32_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((int32_t *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
1891
          (*numOfNull)++;
H
Hongze Cheng 已提交
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
          break;
        case 2:
          val = ((int32_t *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                               int16_t *numOfNull) {
  *sum = 0;
  *max = INT64_MIN;
  *min = INT64_MAX;
  *numOfNull = 0;

  int64_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((int64_t *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
1923
          (*numOfNull)++;
H
Hongze Cheng 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
          break;
        case 2:
          val = ((int64_t *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                              int16_t *numOfNull) {
  *(double *)sum = 0;
  *(double *)max = -FLT_MAX;
  *(double *)min = FLT_MAX;
  *numOfNull = 0;

  float val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((float *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
1955
          (*numOfNull)++;
H
Hongze Cheng 已提交
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
          break;
        case 2:
          val = ((float *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                               int16_t *numOfNull) {
  *(double *)sum = 0;
  *(double *)max = -DBL_MAX;
  *(double *)min = DBL_MAX;
  *numOfNull = 0;

  double val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((double *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
1987
          (*numOfNull)++;
H
Hongze Cheng 已提交
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
          break;
        case 2:
          val = ((double *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                                 int16_t *numOfNull) {
  *(uint64_t *)sum = 0;
  *(uint64_t *)max = 0;
  *(uint64_t *)min = UINT8_MAX;
  *numOfNull = 0;

  uint8_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((uint8_t *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
2019
          (*numOfNull)++;
H
Hongze Cheng 已提交
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
          break;
        case 2:
          val = ((uint8_t *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                                      int16_t *numOfNull) {
  *(uint64_t *)sum = 0;
  *(uint64_t *)max = 0;
  *(uint64_t *)min = UINT16_MAX;
  *numOfNull = 0;

  uint16_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((uint16_t *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
2051
          (*numOfNull)++;
H
Hongze Cheng 已提交
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
          break;
        case 2:
          val = ((uint16_t *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                             int16_t *numOfNull) {
  *(uint64_t *)sum = 0;
  *(uint64_t *)max = 0;
  *(uint64_t *)min = UINT32_MAX;
  *numOfNull = 0;

  uint32_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((uint32_t *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
2083
          (*numOfNull)++;
H
Hongze Cheng 已提交
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
          break;
        case 2:
          val = ((uint32_t *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
                                                int16_t *numOfNull) {
  *(uint64_t *)sum = 0;
  *(uint64_t *)max = 0;
  *(uint64_t *)min = UINT64_MAX;
  *numOfNull = 0;

  uint64_t val;
  if (HAS_VALUE == pColData->flag) {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      val = ((uint64_t *)pColData->pData)[iVal];
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
    }
  } else {
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
      switch (tColDataGetBitValue(pColData, iVal)) {
        case 0:
        case 1:
H
Hongze Cheng 已提交
2115
          (*numOfNull)++;
H
Hongze Cheng 已提交
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
          break;
        case 2:
          val = ((uint64_t *)pColData->pData)[iVal];
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
          break;
        default:
          ASSERT(0);
          break;
      }
    }
  }
}

void (*tColDataCalcSMA[])(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min, int16_t *numOfNull) = {
    NULL,
    tColDataCalcSMABool,           // TSDB_DATA_TYPE_BOOL
    tColDataCalcSMATinyInt,        // TSDB_DATA_TYPE_TINYINT
    tColDataCalcSMATinySmallInt,   // TSDB_DATA_TYPE_SMALLINT
    tColDataCalcSMAInt,            // TSDB_DATA_TYPE_INT
    tColDataCalcSMABigInt,         // TSDB_DATA_TYPE_BIGINT
    tColDataCalcSMAFloat,          // TSDB_DATA_TYPE_FLOAT
    tColDataCalcSMADouble,         // TSDB_DATA_TYPE_DOUBLE
    NULL,                          // TSDB_DATA_TYPE_VARCHAR
    tColDataCalcSMABigInt,         // TSDB_DATA_TYPE_TIMESTAMP
    NULL,                          // TSDB_DATA_TYPE_NCHAR
    tColDataCalcSMAUTinyInt,       // TSDB_DATA_TYPE_UTINYINT
    tColDataCalcSMATinyUSmallInt,  // TSDB_DATA_TYPE_USMALLINT
    tColDataCalcSMAUInt,           // TSDB_DATA_TYPE_UINT
    tColDataCalcSMAUBigInt,        // TSDB_DATA_TYPE_UBIGINT
    NULL,                          // TSDB_DATA_TYPE_JSON
    NULL,                          // TSDB_DATA_TYPE_VARBINARY
    NULL,                          // TSDB_DATA_TYPE_DECIMAL
    NULL,                          // TSDB_DATA_TYPE_BLOB
    NULL                           // TSDB_DATA_TYPE_MEDIUMBLOB
};