tdataformat.c 89.4 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
Hongze Cheng 已提交
23 24
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData);

H
more  
Hongze Cheng 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// 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 已提交
48 49 50 51 52 53 54 55 56 57
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 已提交
58
// ================================
C
Cary Xu 已提交
59 60
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson);

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

H
Hongze Cheng 已提交
66 67 68
#define BIT_FLG_NONE  ((uint8_t)0x0)
#define BIT_FLG_NULL  ((uint8_t)0x1)
#define BIT_FLG_VALUE ((uint8_t)0x2)
H
Hongze Cheng 已提交
69

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

H
Hongze Cheng 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#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 已提交
100
int32_t tRowBuild(SArray *aColVal, STSchema *pTSchema, SRow **ppRow) {
H
Hongze Cheng 已提交
101
  int32_t code = 0;
H
Hongze Cheng 已提交
102

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

  // scan ---------------
H
Hongze Cheng 已提交
108
  SRow         *pRow = NULL;
H
Hongze Cheng 已提交
109
  SColVal      *colVals = (SColVal *)TARRAY_DATA(aColVal);
H
Hongze Cheng 已提交
110 111
  uint8_t       flag = 0;
  int32_t       iColVal = 1;
H
Hongze Cheng 已提交
112 113
  const int32_t nColVal = TARRAY_SIZE(aColVal);
  SColVal      *pColVal = (iColVal < nColVal) ? &colVals[iColVal] : NULL;
H
Hongze Cheng 已提交
114 115 116 117 118 119
  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 已提交
120 121 122 123 124
  while (pTColumn) {
    if (pColVal) {
      if (pColVal->cid == pTColumn->colId) {
        if (COL_VAL_IS_VALUE(pColVal)) {  // VALUE
          flag |= HAS_VALUE;
H
Hongze Cheng 已提交
125
          maxIdx = nkv;
H
Hongze Cheng 已提交
126 127 128 129
          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 已提交
130
            nkv = nkv + tPutI16v(NULL, pTColumn->colId) + pTColumn->bytes;
H
Hongze Cheng 已提交
131
          }
H
Hongze Cheng 已提交
132
          nIdx++;
H
Hongze Cheng 已提交
133 134
        } else if (COL_VAL_IS_NONE(pColVal)) {  // NONE
          flag |= HAS_NONE;
H
Hongze Cheng 已提交
135
        } else if (COL_VAL_IS_NULL(pColVal)) {  // NULL
H
Hongze Cheng 已提交
136
          flag |= HAS_NULL;
H
Hongze Cheng 已提交
137
          maxIdx = nkv;
H
Hongze Cheng 已提交
138
          nkv += tPutI16v(NULL, -pTColumn->colId);
H
Hongze Cheng 已提交
139
          nIdx++;
H
Hongze Cheng 已提交
140 141
        } else {
          ASSERT(0);
H
Hongze Cheng 已提交
142
        }
H
Hongze Cheng 已提交
143

H
Hongze Cheng 已提交
144
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
H
Hongze Cheng 已提交
145
        pColVal = (++iColVal < nColVal) ? &colVals[iColVal] : NULL;
H
Hongze Cheng 已提交
146
      } else if (pColVal->cid > pTColumn->colId) {  // NONE
H
Hongze Cheng 已提交
147 148
        flag |= HAS_NONE;
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
H
Hongze Cheng 已提交
149
      } else {
H
Hongze Cheng 已提交
150
        pColVal = (++iColVal < nColVal) ? &colVals[iColVal] : NULL;
H
Hongze Cheng 已提交
151 152 153 154
      }
    } else {  // NONE
      flag |= HAS_NONE;
      pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
H
more  
Hongze Cheng 已提交
155 156
    }
  }
H
Hongze Cheng 已提交
157

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

H
Hongze Cheng 已提交
199
  // alloc --------------
H
Hongze Cheng 已提交
200 201 202 203 204
  pRow = taosMemoryMalloc(nRow);
  if (NULL == pRow) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
205

H
Hongze Cheng 已提交
206
  // build --------------
H
Hongze Cheng 已提交
207
  pColVal = &colVals[0];
H
Hongze Cheng 已提交
208

H
Hongze Cheng 已提交
209 210 211 212 213 214 215 216 217
  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 已提交
218

H
Hongze Cheng 已提交
219
  iColVal = 1;
H
Hongze Cheng 已提交
220
  pColVal = (iColVal < nColVal) ? &colVals[iColVal] : NULL;
H
Hongze Cheng 已提交
221 222
  iTColumn = 1;
  pTColumn = pTSchema->columns + iTColumn;
H
Hongze Cheng 已提交
223
  if (flag >> 4) {  // KV
H
Hongze Cheng 已提交
224
    SKVIdx  *pIdx = (SKVIdx *)pRow->data;
H
Hongze Cheng 已提交
225 226 227 228 229 230 231 232 233 234 235
    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 已提交
236 237 238 239 240

    while (pTColumn) {
      if (pColVal) {
        if (pColVal->cid == pTColumn->colId) {
          if (COL_VAL_IS_VALUE(pColVal)) {
H
Hongze Cheng 已提交
241 242 243 244 245 246 247 248 249
            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 已提交
250 251 252 253 254 255 256 257 258 259
            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 已提交
260 261 262 263 264 265 266 267
            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 已提交
268
            nv += tPutI16v(pv + nv, -pTColumn->colId);
H
Hongze Cheng 已提交
269 270 271
          }

          pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
H
Hongze Cheng 已提交
272
          pColVal = (++iColVal < nColVal) ? &colVals[iColVal] : NULL;
H
Hongze Cheng 已提交
273 274 275
        } else if (pColVal->cid > pTColumn->colId) {  // NONE
          pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
        } else {
H
Hongze Cheng 已提交
276
          pColVal = (++iColVal < nColVal) ? &colVals[iColVal] : NULL;
H
Hongze Cheng 已提交
277 278 279 280 281 282
        }
      } else {  // NONE
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
      }
    }
  } else {  // TUPLE
H
Hongze Cheng 已提交
283 284
    uint8_t *pb = NULL;
    uint8_t *pf = NULL;
H
Hongze Cheng 已提交
285 286 287
    uint8_t *pv = NULL;
    int32_t  nv = 0;

H
Hongze Cheng 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    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 已提交
310
    }
H
Hongze Cheng 已提交
311

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

            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 已提交
329
          } else if (COL_VAL_IS_NONE(pColVal)) {  // NONE
H
Hongze Cheng 已提交
330
            ROW_SET_BITMAP(pb, flag, iTColumn - 1, BIT_FLG_NONE);
H
Hongze Cheng 已提交
331 332
            if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
          } else {  // NULL
H
Hongze Cheng 已提交
333
            ROW_SET_BITMAP(pb, flag, iTColumn - 1, BIT_FLG_NULL);
H
Hongze Cheng 已提交
334
            if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
335
          }
H
Hongze Cheng 已提交
336

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

H
more  
Hongze Cheng 已提交
354
_exit:
H
Hongze Cheng 已提交
355 356 357 358 359 360
  if (code) {
    *ppRow = NULL;
    tRowDestroy(pRow);
  } else {
    *ppRow = pRow;
  }
H
Hongze Cheng 已提交
361 362 363
  return code;
}

H
Hongze Cheng 已提交
364
void tRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
365
  ASSERT(iCol < pTSchema->numOfCols);
H
Hongze Cheng 已提交
366
  ASSERT(pRow->sver == pTSchema->version);
H
Hongze Cheng 已提交
367

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

H
Hongze Cheng 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  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 已提交
388
  if (pRow->flag >> 4) {  // KV Row
H
Hongze Cheng 已提交
389
    SKVIdx  *pIdx = (SKVIdx *)pRow->data;
H
Hongze Cheng 已提交
390 391 392 393 394 395 396 397
    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 已提交
398 399

    int16_t lidx = 0;
H
Hongze Cheng 已提交
400
    int16_t ridx = pIdx->nCol - 1;
H
Hongze Cheng 已提交
401 402
    while (lidx <= ridx) {
      int16_t  mid = (lidx + ridx) >> 1;
H
Hongze Cheng 已提交
403 404 405 406 407 408 409 410
      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 已提交
411

H
Hongze Cheng 已提交
412
      int16_t cid;
H
Hongze Cheng 已提交
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 438 439 440 441 442 443
      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
H
Hongze Cheng 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
    if (pRow->flag == HAS_VALUE) {
      pColVal->cid = pTColumn->colId;
      pColVal->type = pTColumn->type;
      pColVal->flag = CV_FLAG_VALUE;
      if (IS_VAR_DATA_TYPE(pTColumn->type)) {
        uint8_t *pData = pRow->data + pTSchema->flen + *(int32_t *)(pRow->data + pTColumn->offset);
        pData += tGetU32v(pData, &pColVal->value.nData);
        if (pColVal->value.nData) {
          pColVal->value.pData = pData;
        } else {
          pColVal->value.pData = NULL;
        }
      } else {
        memcpy(&pColVal->value.val, pRow->data + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
      }
    } else {
      uint8_t *pf;
      uint8_t *pv;
H
Hongze Cheng 已提交
462
      uint8_t  bv = BIT_FLG_VALUE;
H
Hongze Cheng 已提交
463

H
Hongze Cheng 已提交
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
      switch (pRow->flag) {
        case (HAS_NULL | HAS_NONE):
          bv = GET_BIT1(pRow->data, iCol - 1);
          break;
        case (HAS_VALUE | HAS_NONE):
          bv = GET_BIT1(pRow->data, iCol - 1);
          if (bv) bv++;
          pf = pRow->data + BIT1_SIZE(pTSchema->numOfCols - 1);
          pv = pf + pTSchema->flen;
          break;
        case (HAS_VALUE | HAS_NULL):
          bv = GET_BIT1(pRow->data, iCol - 1);
          bv++;
          pf = pRow->data + BIT1_SIZE(pTSchema->numOfCols - 1);
          pv = pf + pTSchema->flen;
          break;
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
          bv = GET_BIT2(pRow->data, iCol - 1);
          pf = pRow->data + BIT2_SIZE(pTSchema->numOfCols - 1);
          pv = pf + pTSchema->flen;
          break;
        default:
          ASSERT(0);
          break;
      }
H
Hongze Cheng 已提交
489

H
Hongze Cheng 已提交
490
      if (bv == BIT_FLG_NONE) {
H
Hongze Cheng 已提交
491 492
        *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
        return;
H
Hongze Cheng 已提交
493
      } else if (bv == BIT_FLG_NULL) {
H
Hongze Cheng 已提交
494 495 496
        *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
        return;
      }
H
Hongze Cheng 已提交
497

H
Hongze Cheng 已提交
498 499 500 501 502 503 504 505 506 507 508
      pColVal->cid = pTColumn->colId;
      pColVal->type = pTColumn->type;
      pColVal->flag = CV_FLAG_VALUE;
      if (IS_VAR_DATA_TYPE(pTColumn->type)) {
        uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
        pData += tGetU32v(pData, &pColVal->value.nData);
        if (pColVal->value.nData) {
          pColVal->value.pData = pData;
        } else {
          pColVal->value.pData = NULL;
        }
H
Hongze Cheng 已提交
509
      } else {
H
Hongze Cheng 已提交
510
        memcpy(&pColVal->value.val, pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
511
      }
H
Hongze Cheng 已提交
512
    }
H
Hongze Cheng 已提交
513 514 515
  }
}

H
Hongze Cheng 已提交
516 517 518
void tRowDestroy(SRow *pRow) {
  if (pRow) taosMemoryFree(pRow);
}
H
Hongze Cheng 已提交
519

H
Hongze Cheng 已提交
520
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
521
  if ((*(SRow **)p1)->ts < (*(SRow **)p2)->ts) {
H
Hongze Cheng 已提交
522
    return -1;
H
Hongze Cheng 已提交
523
  } else if ((*(SRow **)p1)->ts > (*(SRow **)p2)->ts) {
H
Hongze Cheng 已提交
524 525 526
    return 1;
  }

H
Hongze Cheng 已提交
527 528
  return 0;
}
H
Hongze Cheng 已提交
529
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
H
Hongze Cheng 已提交
530
static int32_t tRowMergeImpl(SArray *aRowP, STSchema *pTSchema, int32_t iStart, int32_t iEnd, int8_t flag) {
H
Hongze Cheng 已提交
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 556 557 558 559 560 561 562
  int32_t code = 0;

  int32_t    nRow = iEnd - iStart;
  SRowIter **aIter = NULL;
  SArray    *aColVal = NULL;
  SRow      *pRow = NULL;

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

  for (int32_t i = 0; i < nRow; i++) {
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);

    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
    if (code) goto _exit;
  }

  // merge
  aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
  if (aColVal == NULL) {
    code = TSDB_CODE_TDB_OUT_OF_MEMORY;
    goto _exit;
  }

  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
    SColVal *pColVal = NULL;
    for (int32_t iRow = 0; iRow < nRow; iRow++) {
      SColVal *pColValT = tRowIterNext(aIter[iRow]);

H
Hongze Cheng 已提交
563 564
      // todo: take strategy according to the flag
      if (COL_VAL_IS_VALUE(pColValT)) {
H
Hongze Cheng 已提交
565
        pColVal = pColValT;
H
Hongze Cheng 已提交
566 567 568 569
      } else if (COL_VAL_IS_NULL(pColValT)) {
        if (pColVal == NULL) {
          pColVal = pColValT;
        }
H
Hongze Cheng 已提交
570 571 572
      }
    }

H
Hongze Cheng 已提交
573
    if (pColVal) taosArrayPush(aColVal, pColVal);
H
Hongze Cheng 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  }

  // build
  code = tRowBuild(aColVal, pTSchema, &pRow);
  if (code) goto _exit;

  taosArrayRemoveBatch(aRowP, iStart, nRow, (FDelete)tRowPDestroy);
  taosArrayInsert(aRowP, iStart, &pRow);

_exit:
  if (aIter) {
    for (int32_t i = 0; i < nRow; i++) {
      tRowIterClose(&aIter[i]);
    }
    taosMemoryFree(aIter);
  }
  if (aColVal) taosArrayDestroy(aColVal);
  if (code) tRowDestroy(pRow);
  return code;
}
H
Hongze Cheng 已提交
594

H
Hongze Cheng 已提交
595 596
void tRowSort(SArray *aRowP) {
  if (TARRAY_SIZE(aRowP) <= 1) return;
H
Hongze Cheng 已提交
597
  taosArraySort(aRowP, tRowPCmprFn);
H
Hongze Cheng 已提交
598 599 600 601
}

int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, int8_t flag) {
  int32_t code = 0;
H
Hongze Cheng 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

  int32_t iStart = 0;
  while (iStart < aRowP->size) {
    SRow *pRow = (SRow *)taosArrayGetP(aRowP, iStart);

    int32_t iEnd = iStart + 1;
    while (iEnd < aRowP->size) {
      SRow *pRowT = (SRow *)taosArrayGetP(aRowP, iEnd);

      if (pRow->ts != pRowT->ts) break;

      iEnd++;
    }

    if (iEnd - iStart > 1) {
H
Hongze Cheng 已提交
617
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, flag);
H
Hongze Cheng 已提交
618
      if (code) return code;
H
Hongze Cheng 已提交
619 620 621 622 623 624 625 626
    }

    // the array is also changing, so the iStart just ++ instead of iEnd
    iStart++;
  }

  return code;
}
H
Hongze Cheng 已提交
627

H
Hongze Cheng 已提交
628 629 630 631 632 633 634 635
// SRowIter ========================================
struct SRowIter {
  SRow     *pRow;
  STSchema *pTSchema;

  int32_t iTColumn;
  union {
    struct {  // kv
H
Hongze Cheng 已提交
636 637
      int32_t iCol;
      SKVIdx *pIdx;
H
Hongze Cheng 已提交
638
    };
H
Hongze Cheng 已提交
639 640 641
    struct {  // tuple
      uint8_t *pb;
      uint8_t *pf;
H
Hongze Cheng 已提交
642 643
    };
  };
H
Hongze Cheng 已提交
644 645
  uint8_t *pv;
  SColVal  cv;
H
Hongze Cheng 已提交
646 647 648
};

int32_t tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter) {
H
Hongze Cheng 已提交
649 650
  ASSERT(pRow->sver == pTSchema->version);

H
Hongze Cheng 已提交
651 652 653 654 655 656 657 658 659 660
  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;
H
Hongze Cheng 已提交
661 662 663 664
  pIter->iTColumn = 0;

  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;

H
Hongze Cheng 已提交
665
  if (pRow->flag >> 4) {
H
Hongze Cheng 已提交
666 667 668 669 670
    pIter->iCol = 0;
    pIter->pIdx = (SKVIdx *)pRow->data;
    if (pRow->flag & KV_FLG_LIT) {
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
    } else if (pRow->flag & KV_FLG_MID) {
H
Hongze Cheng 已提交
671
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
H
Hongze Cheng 已提交
672
    } else {
H
Hongze Cheng 已提交
673
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
H
Hongze Cheng 已提交
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
    }
  } else {
    switch (pRow->flag) {
      case (HAS_NULL | HAS_NONE):
        pIter->pb = pRow->data;
        break;
      case HAS_VALUE:
        pIter->pf = pRow->data;
        pIter->pv = pIter->pf + pTSchema->flen;
        break;
      case (HAS_VALUE | HAS_NONE):
      case (HAS_VALUE | HAS_NULL):
        pIter->pb = pRow->data;
        pIter->pf = pRow->data + BIT1_SIZE(pTSchema->numOfCols - 1);
        pIter->pv = pIter->pf + pTSchema->flen;
        break;
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
        pIter->pb = pRow->data;
        pIter->pf = pRow->data + BIT2_SIZE(pTSchema->numOfCols - 1);
        pIter->pv = pIter->pf + pTSchema->flen;
        break;
      default:
        ASSERT(0);
        break;
    }
  }
H
Hongze Cheng 已提交
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

_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 已提交
745
  if (pIter->pRow->flag >> 4) {  // KV
H
Hongze Cheng 已提交
746
    if (pIter->iCol < pIter->pIdx->nCol) {
H
Hongze Cheng 已提交
747
      uint8_t *pData;
H
Hongze Cheng 已提交
748

H
Hongze Cheng 已提交
749 750 751 752 753 754 755 756 757
      if (pIter->pRow->flag & KV_FLG_LIT) {
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
      } else if (pIter->pRow->flag & KV_FLG_MID) {
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
      } else {
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
      }

      int16_t cid;
H
Hongze Cheng 已提交
758
      pData += tGetI16v(pData, &cid);
H
Hongze Cheng 已提交
759

H
Hongze Cheng 已提交
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
      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 已提交
792
  } else {  // Tuple
H
Hongze Cheng 已提交
793
    uint8_t bv = BIT_FLG_VALUE;
H
Hongze Cheng 已提交
794 795 796 797
    if (pIter->pb) {
      switch (pIter->pRow->flag) {
        case (HAS_NULL | HAS_NONE):
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
H
Hongze Cheng 已提交
798
          break;
H
Hongze Cheng 已提交
799 800 801 802 803 804 805 806 807 808 809 810
        case (HAS_VALUE | HAS_NONE):
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
          if (bv) bv++;
          break;
        case (HAS_VALUE | HAS_NULL):
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
          break;
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
          break;
        default:
          ASSERT(0);
H
Hongze Cheng 已提交
811 812 813
          break;
      }

H
Hongze Cheng 已提交
814
      if (bv == BIT_FLG_NONE) {
H
Hongze Cheng 已提交
815 816
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
        goto _exit;
H
Hongze Cheng 已提交
817
      } else if (bv == BIT_FLG_NULL) {
H
Hongze Cheng 已提交
818 819 820 821
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
        goto _exit;
      }
    }
H
Hongze Cheng 已提交
822

H
Hongze Cheng 已提交
823 824 825 826 827 828 829 830
    pIter->cv.cid = pTColumn->colId;
    pIter->cv.type = pTColumn->type;
    pIter->cv.flag = CV_FLAG_VALUE;
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
      pData += tGetU32v(pData, &pIter->cv.value.nData);
      if (pIter->cv.value.nData > 0) {
        pIter->cv.value.pData = pData;
H
Hongze Cheng 已提交
831
      } else {
H
Hongze Cheng 已提交
832
        pIter->cv.value.pData = NULL;
H
Hongze Cheng 已提交
833
      }
H
Hongze Cheng 已提交
834
    } else {
H
Hongze Cheng 已提交
835
      memcpy(&pIter->cv.value.val, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
836
    }
H
Hongze Cheng 已提交
837 838 839 840
    goto _exit;
  }

_exit:
H
Hongze Cheng 已提交
841 842 843 844
  pIter->iTColumn++;
  return &pIter->cv;
}

H
Hongze Cheng 已提交
845
static int32_t tRowAppendNoneToColData(SColData *aColData, int32_t nColData) {
H
Hongze Cheng 已提交
846 847 848
  int32_t code = 0;

  for (int32_t iColData = 0; iColData < nColData; iColData++) {
H
Hongze Cheng 已提交
849
    SColData *pColData = &aColData[iColData];
H
Hongze Cheng 已提交
850 851 852 853 854 855 856
    code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
    if (code) goto _exit;
  }

_exit:
  return code;
}
H
Hongze Cheng 已提交
857
static int32_t tRowAppendNullToColData(SColData *aColData, int32_t nColData, STSchema *pSchema) {
H
Hongze Cheng 已提交
858 859 860
  int32_t code = 0;

  int32_t   iColData = 0;
H
Hongze Cheng 已提交
861
  SColData *pColData = &aColData[iColData];
H
Hongze Cheng 已提交
862 863 864 865 866 867 868 869
  int32_t   iTColumn = 1;
  STColumn *pTColumn = &pSchema->columns[iTColumn];

  while (pColData) {
    if (pTColumn) {
      if (pTColumn->colId == pColData->cid) {  // NULL
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
        if (code) goto _exit;
H
Hongze Cheng 已提交
870
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
871 872 873 874
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
      } else if (pTColumn->colId > pColData->cid) {  // NONE
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
        if (code) goto _exit;
H
Hongze Cheng 已提交
875
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
876 877 878 879 880 881 882
      } else {
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
      }
    } else {
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
      if (code) goto _exit;

H
Hongze Cheng 已提交
883
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
884 885 886
    }
  }

H
Hongze Cheng 已提交
887 888 889
_exit:
  return code;
}
H
Hongze Cheng 已提交
890
static int32_t tRowAppendTupleToColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData) {
H
Hongze Cheng 已提交
891 892 893
  int32_t code = 0;

  int32_t   iColData = 0;
H
Hongze Cheng 已提交
894
  SColData *pColData = &aColData[iColData];
H
Hongze Cheng 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 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
  int32_t   iTColumn = 1;
  STColumn *pTColumn = &pTSchema->columns[iTColumn];

  uint8_t *pb = NULL;
  uint8_t *pf;
  uint8_t *pv;

  switch (pRow->flag) {
    case HAS_VALUE:
      pf = pRow->data;
      pv = pf + pTSchema->flen;
      break;
    case (HAS_NULL | HAS_NONE):
      pb = pRow->data;
      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;
  }

  while (pColData) {
    if (pTColumn) {
      if (pTColumn->colId == pColData->cid) {
        ASSERT(pTColumn->type == pColData->type);
        if (pb) {
          uint8_t bv;
          switch (pRow->flag) {
            case (HAS_NULL | HAS_NONE):
              bv = GET_BIT1(pb, iTColumn - 1);
              break;
            case (HAS_VALUE | HAS_NONE):
              bv = GET_BIT1(pb, iTColumn - 1);
              if (bv) bv++;
              break;
            case (HAS_VALUE | HAS_NULL):
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
              break;
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
              bv = GET_BIT2(pb, iTColumn - 1);
              break;
            default:
              ASSERT(0);
              break;
          }

H
Hongze Cheng 已提交
951
          if (bv == BIT_FLG_NONE) {
H
Hongze Cheng 已提交
952 953 954
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
            if (code) goto _exit;
            goto _continue;
H
Hongze Cheng 已提交
955
          } else if (bv == BIT_FLG_NULL) {
H
Hongze Cheng 已提交
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
            if (code) goto _exit;
            goto _continue;
          }
        }

        if (IS_VAR_DATA_TYPE(pColData->type)) {
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
          uint32_t nData;
          pData += tGetU32v(pData, &nData);
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
          if (code) goto _exit;
        } else {
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
                                                                        TYPE_BYTES[pColData->type]);
          if (code) goto _exit;
        }

      _continue:
H
Hongze Cheng 已提交
975
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
976 977 978 979 980
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
      } else if (pTColumn->colId > pColData->cid) {  // NONE
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
        if (code) goto _exit;

H
Hongze Cheng 已提交
981
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
982 983 984 985 986 987 988
      } else {
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
      }
    } else {
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
      if (code) goto _exit;

H
Hongze Cheng 已提交
989
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
990 991 992 993 994 995
    }
  }

_exit:
  return code;
}
H
Hongze Cheng 已提交
996
static int32_t tRowAppendKVToColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData) {
H
Hongze Cheng 已提交
997 998 999 1000 1001
  int32_t code = 0;

  SKVIdx   *pKVIdx = (SKVIdx *)pRow->data;
  uint8_t  *pv = NULL;
  int32_t   iColData = 0;
H
Hongze Cheng 已提交
1002
  SColData *pColData = &aColData[iColData];
H
Hongze Cheng 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
  int32_t   iTColumn = 1;
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
  int32_t   iCol = 0;

  if (pRow->flag & KV_FLG_LIT) {
    pv = pKVIdx->idx + pKVIdx->nCol;
  } else if (pRow->flag & KV_FLG_MID) {
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
  } else if (pRow->flag & KV_FLG_BIG) {
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
  } else {
    ASSERT(0);
  }

  while (pColData) {
    if (pTColumn) {
      if (pTColumn->colId == pColData->cid) {
        while (iCol < pKVIdx->nCol) {
          uint8_t *pData;
          if (pRow->flag & KV_FLG_LIT) {
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
          } else if (pRow->flag & KV_FLG_MID) {
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
          } else if (pRow->flag & KV_FLG_BIG) {
            pData = pv + ((uint32_t *)pKVIdx->idx)[iCol];
          } else {
            ASSERT(0);
          }

          int16_t cid;
          pData += tGetI16(pData, &cid);

          if (TABS(cid) == pTColumn->colId) {
            if (cid < 0) {
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
              if (code) goto _exit;
            } else {
              uint32_t nData;
              pData += tGetU32v(pData, &nData);
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
              if (code) goto _exit;
            }
            iCol++;
            goto _continue;
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
            break;
          } else {
            iCol++;
          }
        }

        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
        if (code) goto _exit;

      _continue:
H
Hongze Cheng 已提交
1058
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1059 1060 1061 1062
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
      } else if (pTColumn->colId > pColData->cid) {
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
        if (code) goto _exit;
H
Hongze Cheng 已提交
1063
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1064 1065 1066 1067 1068 1069
      } else {
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
      }
    } else {
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
      if (code) goto _exit;
H
Hongze Cheng 已提交
1070
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1071 1072 1073
    }
  }

H
Hongze Cheng 已提交
1074 1075 1076
_exit:
  return code;
}
H
Hongze Cheng 已提交
1077
int32_t tRowAppendToColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData) {
H
Hongze Cheng 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
  ASSERT(pRow->sver == pTSchema->version);
  ASSERT(nColData > 0);

  int32_t code = 0;

  if (pRow->flag == HAS_NONE) {
    code = tRowAppendNoneToColData(aColData, nColData);
    goto _exit;
  } else if (pRow->flag == HAS_NULL) {
    code = tRowAppendNullToColData(aColData, nColData, pTSchema);
    goto _exit;
  }

  if (pRow->flag >> 4) {  // KV row
H
Hongze Cheng 已提交
1092 1093 1094 1095 1096
    code = tRowAppendKVToColData(pRow, pTSchema, aColData, nColData);
    if (code) goto _exit;
  } else {
    code = tRowAppendTupleToColData(pRow, pTSchema, aColData, nColData);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1097 1098 1099 1100 1101
  }

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

H
Hongze Cheng 已提交
1103
// STag ========================================
H
Hongze Cheng 已提交
1104 1105
static int tTagValCmprFn(const void *p1, const void *p2) {
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
1106
    return -1;
H
Hongze Cheng 已提交
1107
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
1108 1109
    return 1;
  }
H
Hongze Cheng 已提交
1110

H
Hongze Cheng 已提交
1111 1112
  return 0;
}
H
Hongze Cheng 已提交
1113 1114 1115
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
}
C
Cary Xu 已提交
1116 1117 1118 1119 1120 1121 1122

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};
1123
      strncpy(tmpVal, val, vlen > 31 ? 31 : vlen);
C
Cary Xu 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
      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 已提交
1162 1163 1164
    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 已提交
1165 1166 1167 1168 1169 1170 1171
    default:
      ASSERT(0);
      break;
  }
}

void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
H
Hongze Cheng 已提交
1172 1173
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  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 已提交
1184
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
1185 1186 1187 1188 1189 1190 1191 1192
    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 已提交
1193
    } else {
1194
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
C
Cary Xu 已提交
1195
    }
1196
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
C
Cary Xu 已提交
1197
    tGetTagVal(p + offset, &tagVal, isJson);
1198
    if (IS_VAR_DATA_TYPE(tagVal.type)) {
wmmhello's avatar
wmmhello 已提交
1199
      debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
1200
    } else {
wmmhello's avatar
wmmhello 已提交
1201 1202
      debugPrintTagVal(tagVal.type, &tagVal.i64, tDataTypes[tagVal.type].bytes, __func__, __LINE__);
    }
C
Cary Xu 已提交
1203 1204 1205 1206
  }
  printf("\n");
}

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

H
Hongze Cheng 已提交
1210 1211 1212 1213 1214 1215
  // key
  if (isJson) {
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
  } else {
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
  }
H
Hongze Cheng 已提交
1216

H
Hongze Cheng 已提交
1217 1218 1219 1220 1221 1222 1223
  // 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 已提交
1224
    p = p ? p + n : p;
wmmhello's avatar
wmmhello 已提交
1225
    n += tDataTypes[pTagVal->type].bytes;
1226
    if (p) memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
H
Hongze Cheng 已提交
1227 1228
  }

H
Hongze Cheng 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
  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 已提交
1239 1240
  }

H
Hongze Cheng 已提交
1241 1242
  // type
  n += tGetI8(p + n, &pTagVal->type);
H
Hongze Cheng 已提交
1243

H
Hongze Cheng 已提交
1244 1245 1246 1247
  // value
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
  } else {
wmmhello's avatar
wmmhello 已提交
1248
    memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
wmmhello's avatar
wmmhello 已提交
1249
    n += tDataTypes[pTagVal->type].bytes;
H
Hongze Cheng 已提交
1250 1251 1252 1253
  }

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

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

H
Hongze Cheng 已提交
1257 1258 1259 1260 1261
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 已提交
1262 1263
}

H
Hongze Cheng 已提交
1264
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
H
Hongze Cheng 已提交
1265 1266 1267
  int32_t  code = 0;
  uint8_t *p = NULL;
  int16_t  n = 0;
H
Hongze Cheng 已提交
1268
  int16_t  nTag = taosArrayGetSize(pArray);
H
Hongze Cheng 已提交
1269 1270
  int32_t  szTag = 0;
  int8_t   isLarge = 0;
H
Hongze Cheng 已提交
1271 1272 1273

  // sort
  if (isJson) {
wafwerar's avatar
wafwerar 已提交
1274
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
H
Hongze Cheng 已提交
1275
  } else {
wafwerar's avatar
wafwerar 已提交
1276
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
H
Hongze Cheng 已提交
1277 1278 1279
  }

  // get size
H
Hongze Cheng 已提交
1280
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
1281
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
1282
  }
H
Hongze Cheng 已提交
1283 1284 1285 1286 1287 1288
  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 已提交
1289

H
Hongze Cheng 已提交
1290
  ASSERT(szTag <= INT16_MAX);
H
Hongze Cheng 已提交
1291 1292

  // build tag
C
Cary Xu 已提交
1293
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
H
Hongze Cheng 已提交
1294 1295 1296 1297
  if ((*ppTag) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1298 1299 1300 1301 1302 1303 1304
  (*ppTag)->flags = 0;
  if (isJson) {
    (*ppTag)->flags |= TD_TAG_JSON;
  }
  if (isLarge) {
    (*ppTag)->flags |= TD_TAG_LARGE;
  }
H
Hongze Cheng 已提交
1305 1306 1307
  (*ppTag)->len = szTag;
  (*ppTag)->nTag = nTag;
  (*ppTag)->ver = version;
H
Hongze Cheng 已提交
1308

H
Hongze Cheng 已提交
1309 1310 1311 1312 1313
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
  } else {
    p = (uint8_t *)&(*ppTag)->idx[nTag];
  }
H
Hongze Cheng 已提交
1314 1315
  n = 0;
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
1316 1317 1318 1319 1320
    if (isLarge) {
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
    } else {
      (*ppTag)->idx[iTag] = n;
    }
H
Hongze Cheng 已提交
1321
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
1322
  }
1323
#ifdef TD_DEBUG_PRINT_TAG
C
Cary Xu 已提交
1324
  debugPrintSTag(*ppTag, __func__, __LINE__);
1325
#endif
C
Cary Xu 已提交
1326

H
Hongze Cheng 已提交
1327 1328 1329 1330
  return code;

_err:
  return code;
H
Hongze Cheng 已提交
1331 1332 1333 1334 1335 1336
}

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

1337
char *tTagValToData(const STagVal *value, bool isJson) {
H
Haojun Liao 已提交
1338 1339 1340 1341
  if (!value) {
    return NULL;
  }

1342
  char  *data = NULL;
wmmhello's avatar
wmmhello 已提交
1343 1344 1345 1346
  int8_t typeBytes = 0;
  if (isJson) {
    typeBytes = CHAR_BYTES;
  }
H
Haojun Liao 已提交
1347

1348
  if (IS_VAR_DATA_TYPE(value->type)) {
wmmhello's avatar
wmmhello 已提交
1349
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
H
Haojun Liao 已提交
1350 1351 1352 1353 1354 1355 1356 1357
    if (data == NULL) {
      return NULL;
    }

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

wmmhello's avatar
wmmhello 已提交
1358 1359
    varDataLen(data + typeBytes) = value->nData;
    memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
1360 1361
  } else {
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
wmmhello's avatar
wmmhello 已提交
1362 1363 1364 1365 1366
  }

  return data;
}

C
Cary Xu 已提交
1367
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
H
Hongze Cheng 已提交
1368
  if (!pTag || !pTagVal) {
wmmhello's avatar
wmmhello 已提交
1369 1370 1371
    return false;
  }

H
Hongze Cheng 已提交
1372 1373 1374
  int16_t  lidx = 0;
  int16_t  ridx = pTag->nTag - 1;
  int16_t  midx;
H
Hongze Cheng 已提交
1375 1376 1377 1378
  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 已提交
1379 1380 1381
  STagVal  tv;
  int      c;

H
Hongze Cheng 已提交
1382 1383 1384 1385 1386 1387
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }

H
Hongze Cheng 已提交
1388 1389 1390 1391 1392
  pTagVal->type = TSDB_DATA_TYPE_NULL;
  pTagVal->pData = NULL;
  pTagVal->nData = 0;
  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;
H
Hongze Cheng 已提交
1393 1394 1395 1396 1397
    if (isLarge) {
      offset = ((int16_t *)pTag->idx)[midx];
    } else {
      offset = pTag->idx[midx];
    }
H
Hongze Cheng 已提交
1398

H
Hongze Cheng 已提交
1399 1400
    tGetTagVal(p + offset, &tv, isJson);
    if (isJson) {
H
Hongze Cheng 已提交
1401
      c = tTagValJsonCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1402
    } else {
H
Hongze Cheng 已提交
1403
      c = tTagValCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1404 1405
    }

H
Hongze Cheng 已提交
1406 1407 1408 1409
    if (c < 0) {
      ridx = midx - 1;
    } else if (c > 0) {
      lidx = midx + 1;
H
Hongze Cheng 已提交
1410
    } else {
H
Hongze Cheng 已提交
1411
      memcpy(pTagVal, &tv, sizeof(tv));
C
Cary Xu 已提交
1412
      return true;
H
Hongze Cheng 已提交
1413 1414
    }
  }
C
Cary Xu 已提交
1415
  return false;
H
Hongze Cheng 已提交
1416 1417
}

H
more  
Hongze Cheng 已提交
1418 1419
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
H
Hongze Cheng 已提交
1420 1421
}

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

C
Cary Xu 已提交
1424
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
H
Hongze Cheng 已提交
1425
  int32_t  code = 0;
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
  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 已提交
1436

H
more  
Hongze Cheng 已提交
1437
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
H
Hongze Cheng 已提交
1438
  if (*ppArray == NULL) {
H
Hongze Cheng 已提交
1439 1440 1441 1442 1443
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
1444 1445 1446 1447 1448 1449
    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 已提交
1450
    taosArrayPush(*ppArray, &tv);
H
Hongze Cheng 已提交
1451 1452 1453 1454 1455 1456 1457 1458
  }

  return code;

_err:
  return code;
}

wmmhello's avatar
wmmhello 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
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 已提交
1479
// STSchema ========================================
H
Hongze Cheng 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
  if (pTSchema == NULL) return NULL;

  pTSchema->numOfCols = numOfCols;
  pTSchema->version = version;

  // timestamp column
  ASSERT(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID);
  pTSchema->columns[0].colId = aSchema[0].colId;
  pTSchema->columns[0].type = aSchema[0].type;
  pTSchema->columns[0].flags = aSchema[0].flags;
H
Hongze Cheng 已提交
1493
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
H
Hongze Cheng 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
  pTSchema->columns[0].offset = -1;

  // other columns
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
    SSchema  *pSchema = &aSchema[iCol];
    STColumn *pTColumn = &pTSchema->columns[iCol];

    pTColumn->colId = pSchema->colId;
    pTColumn->type = pSchema->type;
    pTColumn->flags = pSchema->flags;
    pTColumn->offset = pTSchema->flen;

H
Hongze Cheng 已提交
1506 1507 1508 1509 1510 1511 1512 1513
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
      pTColumn->bytes = pSchema->bytes;
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
    } else {
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
    }

H
Hongze Cheng 已提交
1514 1515 1516
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
  }

H
Hongze Cheng 已提交
1517 1518 1519 1520
#if 1  // todo : remove this
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
#endif

H
Hongze Cheng 已提交
1521 1522 1523 1524 1525 1526 1527
  return pTSchema;
}

void tDestroyTSchema(STSchema *pTSchema) {
  if (pTSchema) taosMemoryFree(pTSchema);
}

H
refact  
Hongze Cheng 已提交
1528
// SColData ========================================
H
Hongze Cheng 已提交
1529 1530 1531 1532
void tColDataDestroy(void *ph) {
  SColData *pColData = (SColData *)ph;

  tFree(pColData->pBitMap);
H
Hongze Cheng 已提交
1533
  tFree(pColData->aOffset);
H
Hongze Cheng 已提交
1534 1535 1536 1537 1538 1539 1540
  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 已提交
1541
  tColDataClear(pColData);
H
Hongze Cheng 已提交
1542 1543
}

H
Hongze Cheng 已提交
1544
void tColDataClear(SColData *pColData) {
H
Hongze Cheng 已提交
1545 1546 1547 1548 1549
  pColData->nVal = 0;
  pColData->flag = 0;
  pColData->nData = 0;
}

D
dapan1121 已提交
1550 1551 1552 1553 1554 1555 1556 1557
void tColDataDeepClear(SColData *pColData) {
  pColData->pBitMap = NULL;
  pColData->aOffset = NULL;
  pColData->pData = NULL;

  tColDataClear(pColData);
}

H
Hongze Cheng 已提交
1558
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1559 1560 1561
  int32_t code = 0;

  if (IS_VAR_DATA_TYPE(pColData->type)) {
H
Hongze Cheng 已提交
1562
    code = tRealloc((uint8_t **)(&pColData->aOffset), (pColData->nVal + 1) << 2);
H
Hongze Cheng 已提交
1563 1564 1565
    if (code) goto _exit;
    pColData->aOffset[pColData->nVal] = pColData->nData;

H
Hongze Cheng 已提交
1566 1567
    if (nData) {
      code = tRealloc(&pColData->pData, pColData->nData + nData);
H
Hongze Cheng 已提交
1568
      if (code) goto _exit;
H
Hongze Cheng 已提交
1569 1570
      memcpy(pColData->pData + pColData->nData, pData, nData);
      pColData->nData += nData;
H
Hongze Cheng 已提交
1571 1572
    }
  } else {
H
Hongze Cheng 已提交
1573
    ASSERT(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal);
H
Hongze Cheng 已提交
1574 1575
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1576 1577 1578 1579 1580
    if (pData) {
      memcpy(pColData->pData + pColData->nData, pData, TYPE_BYTES[pColData->type]);
    } else {
      memset(pColData->pData + pColData->nData, 0, TYPE_BYTES[pColData->type]);
    }
H
Hongze Cheng 已提交
1581
    pColData->nData += tDataTypes[pColData->type].bytes;
H
Hongze Cheng 已提交
1582
  }
H
Hongze Cheng 已提交
1583
  pColData->nVal++;
H
Hongze Cheng 已提交
1584 1585 1586 1587

_exit:
  return code;
}
H
Hongze Cheng 已提交
1588
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1589
  pColData->flag = HAS_VALUE;
H
Hongze Cheng 已提交
1590
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1591
}
H
Hongze Cheng 已提交
1592
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1593 1594
  pColData->flag = HAS_NONE;
  pColData->nVal++;
H
Hongze Cheng 已提交
1595
  return 0;
H
Hongze Cheng 已提交
1596
}
H
Hongze Cheng 已提交
1597
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1598 1599
  pColData->flag = HAS_NULL;
  pColData->nVal++;
H
Hongze Cheng 已提交
1600
  return 0;
H
Hongze Cheng 已提交
1601
}
H
Hongze Cheng 已提交
1602
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1603 1604
  int32_t code = 0;

H
Hongze Cheng 已提交
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
  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 已提交
1626
  }
H
Hongze Cheng 已提交
1627

H
Hongze Cheng 已提交
1628
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1629
}
H
Hongze Cheng 已提交
1630
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1631
  pColData->nVal++;
H
Hongze Cheng 已提交
1632
  return 0;
H
Hongze Cheng 已提交
1633
}
H
Hongze Cheng 已提交
1634
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1635 1636
  int32_t code = 0;

H
Hongze Cheng 已提交
1637 1638 1639
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1640

H
Hongze Cheng 已提交
1641 1642
  memset(pColData->pBitMap, 0, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1643

H
Hongze Cheng 已提交
1644 1645
  pColData->flag |= HAS_NULL;
  pColData->nVal++;
H
Hongze Cheng 已提交
1646

H
Hongze Cheng 已提交
1647 1648
  return code;
}
H
Hongze Cheng 已提交
1649
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1650
  int32_t code = 0;
H
Hongze Cheng 已提交
1651

H
Hongze Cheng 已提交
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
  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 已提交
1672
    }
H
Hongze Cheng 已提交
1673
  }
H
Hongze Cheng 已提交
1674

H
Hongze Cheng 已提交
1675
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1676
}
H
Hongze Cheng 已提交
1677
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1678 1679
  int32_t code = 0;

H
Hongze Cheng 已提交
1680 1681 1682
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1683

H
Hongze Cheng 已提交
1684 1685
  memset(pColData->pBitMap, 255, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1686

H
Hongze Cheng 已提交
1687 1688
  pColData->flag |= HAS_NONE;
  pColData->nVal++;
H
Hongze Cheng 已提交
1689

H
Hongze Cheng 已提交
1690 1691
  return code;
}
H
Hongze Cheng 已提交
1692
static FORCE_INLINE int32_t tColDataAppendValue22(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1693
  pColData->nVal++;
H
Hongze Cheng 已提交
1694
  return 0;
H
Hongze Cheng 已提交
1695
}
H
Hongze Cheng 已提交
1696
static FORCE_INLINE int32_t tColDataAppendValue30(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1697
  int32_t code = 0;
H
Hongze Cheng 已提交
1698

H
Hongze Cheng 已提交
1699
  pColData->flag |= HAS_VALUE;
H
Hongze Cheng 已提交
1700

H
Hongze Cheng 已提交
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
  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 已提交
1724
    }
H
Hongze Cheng 已提交
1725
  }
H
Hongze Cheng 已提交
1726

H
Hongze Cheng 已提交
1727
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1728
}
H
Hongze Cheng 已提交
1729
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1730 1731
  int32_t code = 0;

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

H
Hongze Cheng 已提交
1735 1736
  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
  pColData->nVal++;
H
Hongze Cheng 已提交
1737

H
Hongze Cheng 已提交
1738 1739
  return code;
}
H
Hongze Cheng 已提交
1740
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1741
  int32_t code = 0;
H
Hongze Cheng 已提交
1742

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

H
Hongze Cheng 已提交
1746 1747
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
  pColData->nVal++;
H
Hongze Cheng 已提交
1748

H
Hongze Cheng 已提交
1749 1750
  return code;
}
H
Hongze Cheng 已提交
1751
#define tColDataAppendValue40 tColDataPutValue
H
Hongze Cheng 已提交
1752
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
  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 已提交
1764
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1765
}
H
Hongze Cheng 已提交
1766
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1767 1768
  int32_t code = 0;

H
Hongze Cheng 已提交
1769
  pColData->flag |= HAS_NULL;
H
Hongze Cheng 已提交
1770

H
Hongze Cheng 已提交
1771 1772 1773
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1774

H
Hongze Cheng 已提交
1775 1776 1777
  memset(pColData->pBitMap, 255, nBit);
  SET_BIT1(pColData->pBitMap, pColData->nVal, 0);

H
Hongze Cheng 已提交
1778
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1779
}
H
Hongze Cheng 已提交
1780
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1781 1782
  int32_t code = 0;

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

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

H
Hongze Cheng 已提交
1788
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1789
}
H
Hongze Cheng 已提交
1790
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1791 1792 1793 1794 1795 1796 1797
  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 已提交
1798
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1799
}
H
Hongze Cheng 已提交
1800
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
  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 已提交
1811
  }
H
Hongze Cheng 已提交
1812 1813 1814 1815 1816
  SET_BIT2(pBitMap, pColData->nVal, 1);

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

H
Hongze Cheng 已提交
1817
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1818
}
H
Hongze Cheng 已提交
1819
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1820 1821
  int32_t code = 0;

H
Hongze Cheng 已提交
1822 1823 1824
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;
  SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1825

H
Hongze Cheng 已提交
1826
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1827
}
H
Hongze Cheng 已提交
1828
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1829
  int32_t code = 0;
H
Hongze Cheng 已提交
1830

H
Hongze Cheng 已提交
1831 1832 1833 1834 1835 1836 1837 1838
  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 已提交
1839
  }
H
Hongze Cheng 已提交
1840 1841 1842 1843 1844
  SET_BIT2(pBitMap, pColData->nVal, 0);

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

H
Hongze Cheng 已提交
1845
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1846
}
H
Hongze Cheng 已提交
1847
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1848 1849 1850 1851 1852 1853
  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 已提交
1854
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1855
}
H
Hongze Cheng 已提交
1856
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1857 1858
  int32_t code = 0;

H
Hongze Cheng 已提交
1859
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
H
Hongze Cheng 已提交
1860 1861
  if (code) return code;
  SET_BIT2(pColData->pBitMap, pColData->nVal, 2);
H
Hongze Cheng 已提交
1862

H
Hongze Cheng 已提交
1863
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1864
}
H
Hongze Cheng 已提交
1865
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1866 1867 1868 1869 1870 1871
  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 已提交
1872
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1873
}
H
Hongze Cheng 已提交
1874
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1875 1876 1877 1878 1879 1880
  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 已提交
1881
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1882
}
H
Hongze Cheng 已提交
1883
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData) = {
H
Hongze Cheng 已提交
1884 1885 1886
    {tColDataAppendValue00, tColDataAppendValue01, tColDataAppendValue02},  // 0
    {tColDataAppendValue10, tColDataAppendValue11, tColDataAppendValue12},  // HAS_NONE
    {tColDataAppendValue20, tColDataAppendValue21, tColDataAppendValue22},  // HAS_NULL
H
Hongze Cheng 已提交
1887
    {tColDataAppendValue30, tColDataAppendValue31, tColDataAppendValue32},  // HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
1888 1889 1890 1891
    {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 已提交
1892
};
H
refact  
Hongze Cheng 已提交
1893
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1894
  ASSERT(pColData->cid == pColVal->cid && pColData->type == pColVal->type);
H
Hongze Cheng 已提交
1895 1896 1897
  return tColDataAppendValueImpl[pColData->flag][pColVal->flag](
      pColData, IS_VAR_DATA_TYPE(pColData->type) ? pColVal->value.pData : (uint8_t *)&pColVal->value.val,
      pColVal->value.nData);
H
refact  
Hongze Cheng 已提交
1898 1899
}

H
Hongze Cheng 已提交
1900 1901 1902 1903 1904 1905
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);
}
H
Hongze Cheng 已提交
1906 1907
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
  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 已提交
1929
    memcpy(&value.val, pColData->pData + tDataTypes[pColData->type].bytes * iVal, tDataTypes[pColData->type].bytes);
H
Hongze Cheng 已提交
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 1955 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
  }
  *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 已提交
1986
  ASSERT(iVal >= 0 && iVal < pColData->nVal && pColData->flag);
H
Hongze Cheng 已提交
1987 1988 1989
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
}

H
Haojun Liao 已提交
1990
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
H
Hongze Cheng 已提交
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 2019 2020 2021
  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
Hongze Cheng 已提交
2022
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
H
refact  
Hongze Cheng 已提交
2023 2024
  int32_t code = 0;

H
Hongze Cheng 已提交
2025
  *pColData = *pColDataFrom;
H
refact  
Hongze Cheng 已提交
2026 2027

  // bitmap
H
Hongze Cheng 已提交
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
  switch (pColData->flag) {
    case (HAS_NULL | HAS_NONE):
    case (HAS_VALUE | HAS_NONE):
    case (HAS_VALUE | HAS_NULL):
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
      if (pColData->pBitMap == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _exit;
      }
      memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
      break;
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
      pColData->pBitMap = xMalloc(arg, BIT2_SIZE(pColData->nVal));
      if (pColData->pBitMap == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _exit;
      }
      memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT2_SIZE(pColData->nVal));
      break;
    default:
      pColData->pBitMap = NULL;
      break;
H
refact  
Hongze Cheng 已提交
2050 2051 2052
  }

  // offset
H
Hongze Cheng 已提交
2053 2054 2055 2056 2057 2058 2059 2060 2061
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
    if (pColData->aOffset == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
  } else {
    pColData->aOffset = NULL;
H
refact  
Hongze Cheng 已提交
2062 2063 2064
  }

  // value
H
Hongze Cheng 已提交
2065 2066
  if (pColData->nData) {
    pColData->pData = xMalloc(arg, pColData->nData);
H
Hongze Cheng 已提交
2067 2068 2069 2070 2071 2072 2073 2074 2075
    if (pColData->pData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
  } else {
    pColData->pData = NULL;
  }
H
refact  
Hongze Cheng 已提交
2076 2077 2078

_exit:
  return code;
G
Ganlin Zhao 已提交
2079
}
H
Hongze Cheng 已提交
2080

H
Hongze Cheng 已提交
2081 2082 2083
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind) {
  int32_t code = 0;

H
Hongze Cheng 已提交
2084
  ASSERT(pColData->type == pBind->buffer_type);
H
Hongze Cheng 已提交
2085

H
Hongze Cheng 已提交
2086
  if (IS_VAR_DATA_TYPE(pBind->buffer_type)) {  // var-length data type
H
Hongze Cheng 已提交
2087
    for (int32_t i = 0; i < pBind->num; ++i) {
H
Hongze Cheng 已提交
2088
      if (pBind->is_null && pBind->is_null[i]) {
H
Hongze Cheng 已提交
2089 2090
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
        if (code) goto _exit;
H
Hongze Cheng 已提交
2091
      } else {
H
Hongze Cheng 已提交
2092 2093
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
H
Hongze Cheng 已提交
2094 2095
      }
    }
H
Hongze Cheng 已提交
2096
  } else {  // fixed-length data type
H
Hongze Cheng 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
    bool allValue;
    bool allNull;
    if (pBind->is_null) {
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
      allNull = (same && pBind->is_null[0] != 0);
      allValue = (same && pBind->is_null[0] == 0);
    } else {
      allNull = false;
      allValue = true;
    }
H
Hongze Cheng 已提交
2107 2108

    if (allValue) {
H
Hongze Cheng 已提交
2109 2110 2111 2112 2113
      // optimize (todo)
      for (int32_t i = 0; i < pBind->num; ++i) {
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
      }
H
Hongze Cheng 已提交
2114
    } else if (allNull) {
H
Hongze Cheng 已提交
2115 2116 2117 2118 2119
      // optimize (todo)
      for (int32_t i = 0; i < pBind->num; ++i) {
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
        if (code) goto _exit;
      }
H
Hongze Cheng 已提交
2120 2121 2122
    } else {
      for (int32_t i = 0; i < pBind->num; ++i) {
        if (pBind->is_null[i]) {
H
Hongze Cheng 已提交
2123 2124
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
          if (code) goto _exit;
H
Hongze Cheng 已提交
2125
        } else {
H
Hongze Cheng 已提交
2126 2127
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
H
Hongze Cheng 已提交
2128 2129 2130 2131 2132 2133 2134 2135 2136
        }
      }
    }
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
static int32_t tColDataSwapValue(SColData *pColData, int32_t i, int32_t j) {
  int32_t code = 0;

  if (IS_VAR_DATA_TYPE(pColData->type)) {
    int32_t  nData1 = pColData->aOffset[i + 1] - pColData->aOffset[i];
    int32_t  nData2 = (j < pColData->nVal - 1) ? pColData->aOffset[j + 1] - pColData->aOffset[j]
                                               : pColData->nData - pColData->aOffset[j];
    uint8_t *pData = taosMemoryMalloc(TMAX(nData1, nData2));
    if (pData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    if (nData1 > nData2) {
      memcpy(pData, pColData->pData + pColData->aOffset[i], nData1);
      memcpy(pColData->pData + pColData->aOffset[i], pColData->pData + pColData->aOffset[j], nData2);
      memmove(pColData->pData + pColData->aOffset[i] + nData2, pColData->pData + pColData->aOffset[i] + nData1,
              pColData->aOffset[j] - pColData->aOffset[i + 1]);
      memcpy(pColData->pData + pColData->aOffset[j] + nData2 - nData1, pData, nData1);
    } else {
      memcpy(pData, pColData->pData + pColData->aOffset[j], nData2);
      memcpy(pColData->pData + pColData->aOffset[j] + nData2 - nData1, pColData->pData + pColData->aOffset[i], nData1);
      memmove(pColData->pData + pColData->aOffset[j] + nData2 - nData1, pColData->pData + pColData->aOffset[i] + nData1,
              pColData->aOffset[j] - pColData->aOffset[i + 1]);
      memcpy(pColData->pData + pColData->aOffset[i], pData, nData2);
    }
    for (int32_t k = i + 1; k <= j; ++k) {
      pColData->aOffset[k] = pColData->aOffset[k] + nData2 - nData1;
    }

    taosMemoryFree(pData);
  } else {
    uint64_t val;
    memcpy(&val, &pColData->pData[TYPE_BYTES[pColData->type] * i], TYPE_BYTES[pColData->type]);
    memcpy(&pColData->pData[TYPE_BYTES[pColData->type] * i], &pColData->pData[TYPE_BYTES[pColData->type] * j],
           TYPE_BYTES[pColData->type]);
    memcpy(&pColData->pData[TYPE_BYTES[pColData->type] * j], &val, TYPE_BYTES[pColData->type]);
  }

_exit:
  return code;
}
static void tColDataSwap(SColData *pColData, int32_t i, int32_t j) {
  ASSERT(i < j);
  ASSERT(j < pColData->nVal);

  switch (pColData->flag) {
    case HAS_NONE:
    case HAS_NULL:
      break;
    case (HAS_NULL | HAS_NONE): {
      uint8_t bv = GET_BIT1(pColData->pBitMap, i);
      SET_BIT1(pColData->pBitMap, i, GET_BIT1(pColData->pBitMap, j));
      SET_BIT1(pColData->pBitMap, j, bv);
    } break;
    case HAS_VALUE: {
      tColDataSwapValue(pColData, i, j);
    } break;
    case (HAS_VALUE | HAS_NONE):
    case (HAS_VALUE | HAS_NULL): {
      uint8_t bv = GET_BIT1(pColData->pBitMap, i);
      SET_BIT1(pColData->pBitMap, i, GET_BIT1(pColData->pBitMap, j));
      SET_BIT1(pColData->pBitMap, j, bv);
      tColDataSwapValue(pColData, i, j);
    } break;
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
      uint8_t bv = GET_BIT2(pColData->pBitMap, i);
      SET_BIT2(pColData->pBitMap, i, GET_BIT2(pColData->pBitMap, j));
      SET_BIT2(pColData->pBitMap, j, bv);
      tColDataSwapValue(pColData, i, j);
    } break;
    default:
      ASSERT(0);
      break;
  }
}
H
Hongze Cheng 已提交
2213 2214 2215 2216 2217 2218 2219 2220
static void tColDataSort(SColData *aColData, int32_t nColData) {
  if (aColData[0].nVal == 0) return;
  // TODO
}
static void tColDataMergeImpl(SColData *pColData, int32_t iStart, int32_t iEnd /* not included */) {
  switch (pColData->flag) {
    case HAS_NONE:
    case HAS_NULL: {
H
Hongze Cheng 已提交
2221
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2222
    } break;
H
Hongze Cheng 已提交
2223
    case (HAS_NULL | HAS_NONE): {
H
Hongze Cheng 已提交
2224
      if (GET_BIT1(pColData->pBitMap, iStart) == 0) {
H
Hongze Cheng 已提交
2225
        for (int32_t i = iStart + 1; i < iEnd; ++i) {
H
Hongze Cheng 已提交
2226 2227
          if (GET_BIT1(pColData->pBitMap, i) == 1) {
            SET_BIT1(pColData->pBitMap, iStart, 1);
H
Hongze Cheng 已提交
2228 2229 2230 2231 2232 2233 2234 2235
            break;
          }
        }
      }
      for (int32_t i = iEnd, j = iStart + 1; i < pColData->nVal; ++i, ++j) {
        SET_BIT1(pColData->pBitMap, j, GET_BIT1(pColData->pBitMap, i));
      }

H
Hongze Cheng 已提交
2236
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253

      uint8_t flag = 0;
      for (int32_t i = 0; i < pColData->nVal; ++i) {
        uint8_t bv = GET_BIT1(pColData->pBitMap, i);
        if (bv == BIT_FLG_NONE) {
          flag |= HAS_NONE;
        } else if (bv == BIT_FLG_NULL) {
          flag |= HAS_NULL;
        } else {
          ASSERT(0);
        }

        if (flag == pColData->flag) break;
      }
      pColData->flag = flag;
    } break;
    case HAS_VALUE: {
H
Hongze Cheng 已提交
2254 2255 2256
      if (IS_VAR_DATA_TYPE(pColData->type)) {
        int32_t nDiff = pColData->aOffset[iEnd - 1] - pColData->aOffset[iStart];

H
Hongze Cheng 已提交
2257
        memmove(pColData->pData + pColData->aOffset[iStart], pColData->pData + pColData->aOffset[iEnd - 1],
H
Hongze Cheng 已提交
2258 2259 2260 2261 2262 2263 2264
                pColData->nData - pColData->aOffset[iEnd - 1]);
        pColData->nData -= nDiff;

        for (int32_t i = iEnd, j = iStart + 1; i < pColData->nVal; ++i, ++j) {
          pColData->aOffset[j] = pColData->aOffset[i] - nDiff;
        }
      } else {
H
Hongze Cheng 已提交
2265 2266
        memmove(pColData->pData + TYPE_BYTES[pColData->type] * iStart,
                pColData->pData + TYPE_BYTES[pColData->type] * (iEnd - 1),
H
Hongze Cheng 已提交
2267 2268 2269 2270
                TYPE_BYTES[pColData->type] * (pColData->nVal - iEnd + 1));
        pColData->nData -= (TYPE_BYTES[pColData->type] * (iEnd - iStart - 1));
      }

H
Hongze Cheng 已提交
2271
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2272
    } break;
H
Hongze Cheng 已提交
2273
    case (HAS_VALUE | HAS_NONE): {
H
Hongze Cheng 已提交
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
      uint8_t bv;
      int32_t iv;
      for (int32_t i = iEnd - 1; i >= iStart; --i) {
        bv = GET_BIT1(pColData->pBitMap, i);
        if (bv) {
          iv = i;
          break;
        }
      }

      if (bv) {  // has a value
        if (IS_VAR_DATA_TYPE(pColData->type)) {
          if (iv != iStart) {
            memmove(&pColData->pData[pColData->aOffset[iStart]], &pColData->pData[pColData->aOffset[iv]],
                    iv < (pColData->nVal - 1) ? pColData->aOffset[iv + 1] - pColData->aOffset[iv]
                                              : pColData->nData - pColData->aOffset[iv]);
          }
          // TODO
          ASSERT(0);
        } else {
          if (iv != iStart) {
            memcpy(&pColData->pData[TYPE_BYTES[pColData->type] * iStart],
                   &pColData->pData[TYPE_BYTES[pColData->type] * iv], TYPE_BYTES[pColData->type]);
          }
          memmove(&pColData->pData[TYPE_BYTES[pColData->type] * (iStart + 1)],
                  &pColData->pData[TYPE_BYTES[pColData->type] * iEnd],
                  TYPE_BYTES[pColData->type] * (iEnd - iStart - 1));
          pColData->nData -= (TYPE_BYTES[pColData->type] * (iEnd - iStart - 1));
        }

        SET_BIT1(pColData->pBitMap, iStart, 1);
        for (int32_t i = iEnd, j = iStart + 1; i < pColData->nVal; ++i, ++j) {
          SET_BIT1(pColData->pBitMap, j, GET_BIT1(pColData->pBitMap, i));
        }

        uint8_t flag = HAS_VALUE;
        for (int32_t i = 0; i < pColData->nVal - (iEnd - iStart - 1); ++i) {
          if (GET_BIT1(pColData->pBitMap, i) == 0) {
            flag |= HAS_NONE;
          }

          if (flag == pColData->flag) break;
        }
        pColData->flag = flag;
      } else {  // all NONE
        if (IS_VAR_DATA_TYPE(pColData->type)) {
          int32_t nDiff = pColData->aOffset[iEnd - 1] - pColData->aOffset[iStart];

          memmove(&pColData->pData[pColData->aOffset[iStart]], &pColData->pData[pColData->aOffset[iEnd - 1]],
                  pColData->nData - pColData->aOffset[iEnd - 1]);
          pColData->nData -= nDiff;

          for (int32_t i = iEnd, j = iStart + 1; i < pColData->nVal; ++i, ++j) {
            pColData->aOffset[j] = pColData->aOffset[i] - nDiff;
          }
        } else {
H
Hongze Cheng 已提交
2330 2331
          memmove(pColData->pData + TYPE_BYTES[pColData->type] * (iStart + 1),
                  pColData->pData + TYPE_BYTES[pColData->type] * iEnd,
H
Hongze Cheng 已提交
2332 2333 2334 2335 2336 2337 2338 2339
                  TYPE_BYTES[pColData->type] * (pColData->nVal - iEnd + 1));
          pColData->nData -= (TYPE_BYTES[pColData->type] * (iEnd - iStart - 1));
        }

        for (int32_t i = iEnd, j = iStart + 1; i < pColData->nVal; ++i, ++j) {
          SET_BIT1(pColData->pBitMap, j, GET_BIT1(pColData->pBitMap, i));
        }
      }
H
Hongze Cheng 已提交
2340
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2341
    } break;
H
Hongze Cheng 已提交
2342 2343 2344 2345
    case (HAS_VALUE | HAS_NULL): {
      if (IS_VAR_DATA_TYPE(pColData->type)) {
        int32_t nDiff = pColData->aOffset[iEnd - 1] - pColData->aOffset[iStart];

H
Hongze Cheng 已提交
2346
        memmove(pColData->pData + pColData->aOffset[iStart], pColData->pData + pColData->aOffset[iEnd - 1],
H
Hongze Cheng 已提交
2347 2348 2349 2350 2351 2352 2353
                pColData->nData - pColData->aOffset[iEnd - 1]);
        pColData->nData -= nDiff;

        for (int32_t i = iEnd, j = iStart + 1; i < pColData->nVal; ++i, ++j) {
          pColData->aOffset[j] = pColData->aOffset[i] - nDiff;
        }
      } else {
H
Hongze Cheng 已提交
2354 2355
        memmove(pColData->pData + TYPE_BYTES[pColData->type] * iStart,
                pColData->pData + TYPE_BYTES[pColData->type] * (iEnd - 1),
H
Hongze Cheng 已提交
2356 2357 2358 2359 2360 2361 2362 2363
                TYPE_BYTES[pColData->type] * (pColData->nVal - iEnd + 1));
        pColData->nData -= (TYPE_BYTES[pColData->type] * (iEnd - iStart - 1));
      }

      for (int32_t i = iEnd - 1, j = iStart; i < pColData->nVal; ++i, ++j) {
        SET_BIT1(pColData->pBitMap, j, GET_BIT1(pColData->pBitMap, i));
      }

H
Hongze Cheng 已提交
2364
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2365 2366 2367

      uint8_t flag = 0;
      for (int32_t i = 0; i < pColData->nVal; ++i) {
H
Hongze Cheng 已提交
2368
        if (GET_BIT1(pColData->pBitMap, i)) {
H
Hongze Cheng 已提交
2369 2370 2371 2372 2373 2374 2375 2376
          flag |= HAS_VALUE;
        } else {
          flag |= HAS_NULL;
        }

        if (flag == pColData->flag) break;
      }
      pColData->flag = flag;
H
Hongze Cheng 已提交
2377
    } break;
H
Hongze Cheng 已提交
2378
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
H
Hongze Cheng 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
      uint8_t bv;
      int32_t iv;
      for (int32_t i = iEnd - 1; i >= iStart; --i) {
        bv = GET_BIT2(pColData->pBitMap, i);
        if (bv) {
          iv = i;
          break;
        }
      }

      if (bv) {
        // TODO
        ASSERT(0);
      } else {  // ALL NONE
        if (IS_VAR_DATA_TYPE(pColData->type)) {
          // TODO
          ASSERT(0);
        } else {
          memmove(pColData->pData + TYPE_BYTES[pColData->type] * (iStart + 1),
                  pColData->pData + TYPE_BYTES[pColData->type] * iEnd,
                  TYPE_BYTES[pColData->type] * (pColData->nVal - iEnd));
          pColData->nData -= (TYPE_BYTES[pColData->type] * (iEnd - iStart - 1));
        }

        for (int32_t i = iEnd, j = iStart + 1; i < pColData->nVal; ++i, ++j) {
          SET_BIT2(pColData->pBitMap, j, GET_BIT2(pColData->pBitMap, i));
        }
      }
H
Hongze Cheng 已提交
2407
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2408 2409 2410 2411 2412 2413 2414 2415 2416
    } break;
    default:
      ASSERT(0);
      break;
  }
}
static void tColDataMerge(SColData *aColData, int32_t nColData) {
  int32_t iStart = 0;
  for (;;) {
H
Hongze Cheng 已提交
2417
    if (iStart >= aColData[0].nVal - 1) break;
H
Hongze Cheng 已提交
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434

    int32_t iEnd = iStart + 1;
    while (iEnd < aColData[0].nVal) {
      if (((TSKEY *)aColData[0].pData)[iEnd] != ((TSKEY *)aColData[0].pData)[iStart]) break;

      iEnd++;
    }

    if (iEnd - iStart > 1) {
      for (int32_t i = 0; i < nColData; i++) {
        tColDataMergeImpl(&aColData[i], iStart, iEnd);
      }
    }

    iStart++;
  }
}
H
Hongze Cheng 已提交
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
void tColDataSortMerge(SArray *colDataArr) {
  int32_t   nColData = TARRAY_SIZE(colDataArr);
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);

  if (aColData[0].nVal <= 1) goto _exit;

  ASSERT(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID);
  ASSERT(aColData[0].flag == HAS_VALUE);

  int8_t doSort = 0;
  int8_t doMerge = 0;
  // scan -------
  TSKEY *aKey = (TSKEY *)aColData[0].pData;
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
    if (aKey[iVal] > aKey[iVal - 1]) {
      continue;
    } else if (aKey[iVal] < aKey[iVal - 1]) {
      doSort = 1;
      break;
    } else {
      doMerge = 1;
    }
  }

  // sort -------
  if (doSort) {
H
Hongze Cheng 已提交
2462
    tColDataSort(aColData, nColData);
H
Hongze Cheng 已提交
2463 2464 2465 2466
  }

  // merge -------
  if (doMerge) {
H
Hongze Cheng 已提交
2467
    tColDataMerge(aColData, nColData);
H
Hongze Cheng 已提交
2468 2469 2470 2471
  }

_exit:
  return;
H
Hongze Cheng 已提交
2472 2473
}

H
Hongze Cheng 已提交
2474 2475 2476
int32_t tPutColData(uint8_t *pBuf, SColData *pColData) {
  int32_t n = 0;

H
Hongze Cheng 已提交
2477 2478 2479 2480
  n += tPutI16v(pBuf ? pBuf + n : NULL, pColData->cid);
  n += tPutI8(pBuf ? pBuf + n : NULL, pColData->type);
  n += tPutI32v(pBuf ? pBuf + n : NULL, pColData->nVal);
  n += tPutI8(pBuf ? pBuf + n : NULL, pColData->flag);
H
Hongze Cheng 已提交
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503

  // bitmap
  switch (pColData->flag) {
    case (HAS_NULL | HAS_NONE):
    case (HAS_VALUE | HAS_NONE):
    case (HAS_VALUE | HAS_NULL):
      if (pBuf) memcpy(pBuf + n, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
      n += BIT1_SIZE(pColData->nVal);
      break;
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
      if (pBuf) memcpy(pBuf + n, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
      n += BIT2_SIZE(pColData->nVal);
      break;
    default:
      break;
  }

  // value
  if (pColData->flag & HAS_VALUE) {
    if (IS_VAR_DATA_TYPE(pColData->type)) {
      if (pBuf) memcpy(pBuf + n, pColData->aOffset, pColData->nVal << 2);
      n += (pColData->nVal << 2);

H
Hongze Cheng 已提交
2504
      n += tPutI32v(pBuf ? pBuf + n : NULL, pColData->nData);
H
Hongze Cheng 已提交
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549
      if (pBuf) memcpy(pBuf + n, pColData->pData, pColData->nData);
      n += pColData->nData;
    } else {
      if (pBuf) memcpy(pBuf + n, pColData->pData, pColData->nData);
      n += pColData->nData;
    }
  }

  return n;
}

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

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

  // bitmap
  switch (pColData->flag) {
    case (HAS_NULL | HAS_NONE):
    case (HAS_VALUE | HAS_NONE):
    case (HAS_VALUE | HAS_NULL):
      pColData->pBitMap = pBuf + n;
      n += BIT1_SIZE(pColData->nVal);
      break;
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
      pColData->pBitMap = pBuf + n;
      n += BIT2_SIZE(pColData->nVal);
      break;
    default:
      break;
  }

  // value
  if (pColData->flag & HAS_VALUE) {
    if (IS_VAR_DATA_TYPE(pColData->type)) {
      pColData->aOffset = (int32_t *)(pBuf + n);
      n += (pColData->nVal << 2);

      n += tGetI32v(pBuf + n, &pColData->nData);
      pColData->pData = pBuf + n;
      n += pColData->nData;
    } else {
H
Hongze Cheng 已提交
2550
      pColData->pData = pBuf + n;
H
Hongze Cheng 已提交
2551 2552 2553 2554 2555 2556
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
      n += pColData->nData;
    }
  }

  return n;
H
Hongze Cheng 已提交
2557 2558
}

H
Hongze Cheng 已提交
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
#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 已提交
2584
          (*numOfNull)++;
H
Hongze Cheng 已提交
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615
          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 已提交
2616
          (*numOfNull)++;
H
Hongze Cheng 已提交
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
          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 已提交
2648
          (*numOfNull)++;
H
Hongze Cheng 已提交
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679
          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 已提交
2680
          (*numOfNull)++;
H
Hongze Cheng 已提交
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711
          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 已提交
2712
          (*numOfNull)++;
H
Hongze Cheng 已提交
2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
          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 已提交
2744
          (*numOfNull)++;
H
Hongze Cheng 已提交
2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
          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 已提交
2776
          (*numOfNull)++;
H
Hongze Cheng 已提交
2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
          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 已提交
2808
          (*numOfNull)++;
H
Hongze Cheng 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839
          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 已提交
2840
          (*numOfNull)++;
H
Hongze Cheng 已提交
2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871
          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 已提交
2872
          (*numOfNull)++;
H
Hongze Cheng 已提交
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903
          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 已提交
2904
          (*numOfNull)++;
H
Hongze Cheng 已提交
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939
          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
};