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

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

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

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

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

H
Hongze Cheng 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#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)

101
int32_t tRowBuild(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow) {
H
Hongze Cheng 已提交
102
  int32_t code = 0;
H
Hongze Cheng 已提交
103

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

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

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

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

H
Hongze Cheng 已提交
205
  // alloc --------------
H
Hongze Cheng 已提交
206 207 208 209 210
  pRow = taosMemoryMalloc(nRow);
  if (NULL == pRow) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
211

H
Hongze Cheng 已提交
212
  // build --------------
H
Hongze Cheng 已提交
213
  pColVal = &colVals[0];
H
Hongze Cheng 已提交
214

H
Hongze Cheng 已提交
215 216 217 218 219 220 221 222 223
  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 已提交
224

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

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

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

H
Hongze Cheng 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    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:
H
Hongze Cheng 已提交
314 315 316 317
        if (ASSERTS(0, "impossible")) {
          code = TSDB_CODE_INVALID_PARA;
          goto _exit;
        }
H
Hongze Cheng 已提交
318
    }
H
Hongze Cheng 已提交
319

H
Hongze Cheng 已提交
320 321 322 323 324 325 326 327
    if (pb) {
      if (flag == (HAS_VALUE | HAS_NULL | HAS_NONE)) {
        memset(pb, 0, BIT2_SIZE(pTSchema->numOfCols - 1));
      } else {
        memset(pb, 0, BIT1_SIZE(pTSchema->numOfCols - 1));
      }
    }

H
Hongze Cheng 已提交
328
    // build impl
H
Hongze Cheng 已提交
329 330 331
    while (pTColumn) {
      if (pColVal) {
        if (pColVal->cid == pTColumn->colId) {
H
Hongze Cheng 已提交
332
          if (COL_VAL_IS_VALUE(pColVal)) {  // VALUE
H
Hongze Cheng 已提交
333
            ROW_SET_BITMAP(pb, flag, iTColumn - 1, BIT_FLG_VALUE);
H
Hongze Cheng 已提交
334 335 336 337 338 339 340 341 342 343 344

            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 已提交
345
          } else if (COL_VAL_IS_NONE(pColVal)) {  // NONE
H
Hongze Cheng 已提交
346
            ROW_SET_BITMAP(pb, flag, iTColumn - 1, BIT_FLG_NONE);
H
Hongze Cheng 已提交
347 348
            if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
          } else {  // NULL
H
Hongze Cheng 已提交
349
            ROW_SET_BITMAP(pb, flag, iTColumn - 1, BIT_FLG_NULL);
H
Hongze Cheng 已提交
350
            if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
351
          }
H
Hongze Cheng 已提交
352

H
Hongze Cheng 已提交
353
          pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
H
Hongze Cheng 已提交
354
          pColVal = (++iColVal < nColVal) ? &colVals[iColVal] : NULL;
H
Hongze Cheng 已提交
355
        } else if (pColVal->cid > pTColumn->colId) {  // NONE
H
Hongze Cheng 已提交
356
          ROW_SET_BITMAP(pb, flag, iTColumn - 1, BIT_FLG_NONE);
H
Hongze Cheng 已提交
357
          if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
358 359
          pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
        } else {
H
Hongze Cheng 已提交
360
          pColVal = (++iColVal < nColVal) ? &colVals[iColVal] : NULL;
H
Hongze Cheng 已提交
361 362
        }
      } else {  // NONE
H
Hongze Cheng 已提交
363
        ROW_SET_BITMAP(pb, flag, iTColumn - 1, BIT_FLG_NONE);
H
Hongze Cheng 已提交
364
        if (pf) memset(pf + pTColumn->offset, 0, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
365 366 367 368 369
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? pTSchema->columns + iTColumn : NULL;
      }
    }
  }

H
more  
Hongze Cheng 已提交
370
_exit:
H
Hongze Cheng 已提交
371 372 373 374 375 376
  if (code) {
    *ppRow = NULL;
    tRowDestroy(pRow);
  } else {
    *ppRow = pRow;
  }
H
Hongze Cheng 已提交
377 378 379
  return code;
}

H
Hongze Cheng 已提交
380
int32_t tRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
381
  ASSERT(iCol < pTSchema->numOfCols);
H
Hongze Cheng 已提交
382
  ASSERT(pRow->sver == pTSchema->version);
H
Hongze Cheng 已提交
383

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

H
Hongze Cheng 已提交
386 387 388 389 390
  if (iCol == 0) {
    pColVal->cid = pTColumn->colId;
    pColVal->type = pTColumn->type;
    pColVal->flag = CV_FLAG_VALUE;
    memcpy(&pColVal->value.val, &pRow->ts, sizeof(TSKEY));
H
Hongze Cheng 已提交
391
    return 0;
H
Hongze Cheng 已提交
392 393 394 395
  }

  if (pRow->flag == HAS_NONE) {
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
396
    return 0;
H
Hongze Cheng 已提交
397 398 399 400
  }

  if (pRow->flag == HAS_NULL) {
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
401
    return 0;
H
Hongze Cheng 已提交
402 403
  }

H
Hongze Cheng 已提交
404
  if (pRow->flag >> 4) {  // KV Row
H
Hongze Cheng 已提交
405
    SKVIdx  *pIdx = (SKVIdx *)pRow->data;
H
Hongze Cheng 已提交
406 407 408 409 410 411 412 413
    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 已提交
414 415

    int16_t lidx = 0;
H
Hongze Cheng 已提交
416
    int16_t ridx = pIdx->nCol - 1;
H
Hongze Cheng 已提交
417 418
    while (lidx <= ridx) {
      int16_t  mid = (lidx + ridx) >> 1;
H
Hongze Cheng 已提交
419 420 421 422 423 424 425 426
      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 已提交
427

H
Hongze Cheng 已提交
428
      int16_t cid;
H
Hongze Cheng 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
      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);
          }
        }
H
Hongze Cheng 已提交
450
        return 0;
H
Hongze Cheng 已提交
451 452 453 454 455 456 457 458 459
      } 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 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    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 已提交
478
      uint8_t  bv = BIT_FLG_VALUE;
H
Hongze Cheng 已提交
479

H
Hongze Cheng 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
      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:
H
Hongze Cheng 已提交
502 503
          ASSERTS(0, "invalid row format");
          return TSDB_CODE_IVLD_DATA_FMT;
H
Hongze Cheng 已提交
504
      }
H
Hongze Cheng 已提交
505

H
Hongze Cheng 已提交
506
      if (bv == BIT_FLG_NONE) {
H
Hongze Cheng 已提交
507
        *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
508
        return 0;
H
Hongze Cheng 已提交
509
      } else if (bv == BIT_FLG_NULL) {
H
Hongze Cheng 已提交
510
        *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
511
        return 0;
H
Hongze Cheng 已提交
512
      }
H
Hongze Cheng 已提交
513

H
Hongze Cheng 已提交
514 515 516 517 518 519 520 521 522 523 524
      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 已提交
525
      } else {
H
Hongze Cheng 已提交
526
        memcpy(&pColVal->value.val, pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
527
      }
H
Hongze Cheng 已提交
528
    }
H
Hongze Cheng 已提交
529
  }
H
Hongze Cheng 已提交
530 531

  return 0;
H
Hongze Cheng 已提交
532 533
}

H
Hongze Cheng 已提交
534 535 536
void tRowDestroy(SRow *pRow) {
  if (pRow) taosMemoryFree(pRow);
}
H
Hongze Cheng 已提交
537

H
Hongze Cheng 已提交
538
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
H
Hongze Cheng 已提交
539
  if ((*(SRow **)p1)->ts < (*(SRow **)p2)->ts) {
H
Hongze Cheng 已提交
540
    return -1;
H
Hongze Cheng 已提交
541
  } else if ((*(SRow **)p1)->ts > (*(SRow **)p2)->ts) {
H
Hongze Cheng 已提交
542 543 544
    return 1;
  }

H
Hongze Cheng 已提交
545 546
  return 0;
}
H
Hongze Cheng 已提交
547
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
H
Hongze Cheng 已提交
548
static int32_t tRowMergeImpl(SArray *aRowP, STSchema *pTSchema, int32_t iStart, int32_t iEnd, int8_t flag) {
H
Hongze Cheng 已提交
549 550 551 552 553 554 555 556 557
  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) {
H
Hongze Cheng 已提交
558
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571
    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) {
H
Hongze Cheng 已提交
572
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
573 574 575 576 577 578 579 580
    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 已提交
581 582
      // todo: take strategy according to the flag
      if (COL_VAL_IS_VALUE(pColValT)) {
H
Hongze Cheng 已提交
583
        pColVal = pColValT;
H
Hongze Cheng 已提交
584 585 586 587
      } else if (COL_VAL_IS_NULL(pColValT)) {
        if (pColVal == NULL) {
          pColVal = pColValT;
        }
H
Hongze Cheng 已提交
588 589 590
      }
    }

H
Hongze Cheng 已提交
591
    if (pColVal) taosArrayPush(aColVal, pColVal);
H
Hongze Cheng 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
  }

  // 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 已提交
612

H
Hongze Cheng 已提交
613 614
void tRowSort(SArray *aRowP) {
  if (TARRAY_SIZE(aRowP) <= 1) return;
H
Hongze Cheng 已提交
615
  taosArraySort(aRowP, tRowPCmprFn);
H
Hongze Cheng 已提交
616 617 618 619
}

int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, int8_t flag) {
  int32_t code = 0;
H
Hongze Cheng 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634

  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 已提交
635
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, flag);
H
Hongze Cheng 已提交
636
      if (code) return code;
H
Hongze Cheng 已提交
637 638 639 640 641 642 643 644
    }

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

  return code;
}
H
Hongze Cheng 已提交
645

H
Hongze Cheng 已提交
646 647 648 649 650 651 652 653
// SRowIter ========================================
struct SRowIter {
  SRow     *pRow;
  STSchema *pTSchema;

  int32_t iTColumn;
  union {
    struct {  // kv
H
Hongze Cheng 已提交
654 655
      int32_t iCol;
      SKVIdx *pIdx;
H
Hongze Cheng 已提交
656
    };
H
Hongze Cheng 已提交
657 658 659
    struct {  // tuple
      uint8_t *pb;
      uint8_t *pf;
H
Hongze Cheng 已提交
660 661
    };
  };
H
Hongze Cheng 已提交
662 663
  uint8_t *pv;
  SColVal  cv;
H
Hongze Cheng 已提交
664 665 666
};

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

H
Hongze Cheng 已提交
669 670 671 672
  int32_t code = 0;

  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
  if (pIter == NULL) {
S
Shengliang Guan 已提交
673
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
674 675 676 677 678
    goto _exit;
  }

  pIter->pRow = pRow;
  pIter->pTSchema = pTSchema;
H
Hongze Cheng 已提交
679 680 681 682
  pIter->iTColumn = 0;

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

H
Hongze Cheng 已提交
683
  if (pRow->flag >> 4) {
H
Hongze Cheng 已提交
684 685 686 687 688
    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 已提交
689
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
H
Hongze Cheng 已提交
690
    } else {
H
Hongze Cheng 已提交
691
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
H
Hongze Cheng 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    }
  } 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 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

_exit:
  if (code) {
    *ppIter = NULL;
  } 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 已提交
762
  if (pIter->pRow->flag >> 4) {  // KV
H
Hongze Cheng 已提交
763
    if (pIter->iCol < pIter->pIdx->nCol) {
H
Hongze Cheng 已提交
764
      uint8_t *pData;
H
Hongze Cheng 已提交
765

H
Hongze Cheng 已提交
766 767 768 769 770 771 772 773 774
      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 已提交
775
      pData += tGetI16v(pData, &cid);
H
Hongze Cheng 已提交
776

H
Hongze Cheng 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
      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 已提交
809
  } else {  // Tuple
H
Hongze Cheng 已提交
810
    uint8_t bv = BIT_FLG_VALUE;
H
Hongze Cheng 已提交
811 812 813 814
    if (pIter->pb) {
      switch (pIter->pRow->flag) {
        case (HAS_NULL | HAS_NONE):
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
H
Hongze Cheng 已提交
815
          break;
H
Hongze Cheng 已提交
816 817 818 819 820 821 822 823 824 825 826 827
        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 已提交
828 829 830
          break;
      }

H
Hongze Cheng 已提交
831
      if (bv == BIT_FLG_NONE) {
H
Hongze Cheng 已提交
832 833
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
        goto _exit;
H
Hongze Cheng 已提交
834
      } else if (bv == BIT_FLG_NULL) {
H
Hongze Cheng 已提交
835 836 837 838
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
        goto _exit;
      }
    }
H
Hongze Cheng 已提交
839

H
Hongze Cheng 已提交
840 841 842 843 844 845 846 847
    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 已提交
848
      } else {
H
Hongze Cheng 已提交
849
        pIter->cv.value.pData = NULL;
H
Hongze Cheng 已提交
850
      }
H
Hongze Cheng 已提交
851
    } else {
H
Hongze Cheng 已提交
852
      memcpy(&pIter->cv.value.val, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
H
Hongze Cheng 已提交
853
    }
H
Hongze Cheng 已提交
854 855 856 857
    goto _exit;
  }

_exit:
H
Hongze Cheng 已提交
858 859 860 861
  pIter->iTColumn++;
  return &pIter->cv;
}

H
Hongze Cheng 已提交
862
static int32_t tRowNoneUpsertColData(SColData *aColData, int32_t nColData, int32_t flag) {
H
Hongze Cheng 已提交
863 864
  int32_t code = 0;

H
Hongze Cheng 已提交
865 866
  if (flag) return code;

H
Hongze Cheng 已提交
867
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
H
Hongze Cheng 已提交
868 869
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
    if (code) return code;
H
Hongze Cheng 已提交
870 871 872 873
  }

  return code;
}
H
Hongze Cheng 已提交
874
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
H
Hongze Cheng 已提交
875 876 877
  int32_t code = 0;

  int32_t   iColData = 0;
H
Hongze Cheng 已提交
878
  SColData *pColData = &aColData[iColData];
H
Hongze Cheng 已提交
879 880 881 882 883 884
  int32_t   iTColumn = 1;
  STColumn *pTColumn = &pSchema->columns[iTColumn];

  while (pColData) {
    if (pTColumn) {
      if (pTColumn->colId == pColData->cid) {  // NULL
H
Hongze Cheng 已提交
885 886 887 888 889
        if (flag == 0) {
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
        } else {
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
        }
H
Hongze Cheng 已提交
890
        if (code) goto _exit;
H
Hongze Cheng 已提交
891

H
Hongze Cheng 已提交
892
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
893 894
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
      } else if (pTColumn->colId > pColData->cid) {  // NONE
H
Hongze Cheng 已提交
895
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
H
Hongze Cheng 已提交
896
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
897 898 899
      } else {
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
      }
H
Hongze Cheng 已提交
900 901
    } else {  // NONE
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
H
Hongze Cheng 已提交
902
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
903 904 905
    }
  }

H
Hongze Cheng 已提交
906 907 908
_exit:
  return code;
}
H
Hongze Cheng 已提交
909 910
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
                                      int32_t flag) {
H
Hongze Cheng 已提交
911 912 913
  int32_t code = 0;

  int32_t   iColData = 0;
H
Hongze Cheng 已提交
914
  SColData *pColData = &aColData[iColData];
H
Hongze Cheng 已提交
915 916 917
  int32_t   iTColumn = 1;
  STColumn *pTColumn = &pTSchema->columns[iTColumn];

G
Ganlin Zhao 已提交
918
  uint8_t *pb = NULL, *pf = NULL, *pv = NULL;
H
Hongze Cheng 已提交
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939

  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:
H
Hongze Cheng 已提交
940 941
      ASSERTS(0, "Invalid row flag");
      return TSDB_CODE_IVLD_DATA_FMT;
H
Hongze Cheng 已提交
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
  }

  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:
H
Hongze Cheng 已提交
965 966
              ASSERTS(0, "Invalid row flag");
              return TSDB_CODE_IVLD_DATA_FMT;
H
Hongze Cheng 已提交
967 968
          }

H
Hongze Cheng 已提交
969
          if (bv == BIT_FLG_NONE) {
H
Hongze Cheng 已提交
970 971
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
              goto _exit;
H
Hongze Cheng 已提交
972
            goto _continue;
H
Hongze Cheng 已提交
973
          } else if (bv == BIT_FLG_NULL) {
H
Hongze Cheng 已提交
974 975 976 977 978
            if (flag == 0) {
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
            } else {
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
            }
H
Hongze Cheng 已提交
979 980 981 982 983 984 985 986 987
            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);
H
Hongze Cheng 已提交
988 989 990 991 992
          if (flag == 0) {
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
          } else {
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
          }
H
Hongze Cheng 已提交
993 994
          if (code) goto _exit;
        } else {
H
Hongze Cheng 已提交
995 996 997 998 999 1000 1001
          if (flag == 0) {
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
                                                                          TYPE_BYTES[pColData->type]);
          } else {
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
                                                                          TYPE_BYTES[pColData->type], flag > 0);
          }
H
Hongze Cheng 已提交
1002 1003 1004 1005
          if (code) goto _exit;
        }

      _continue:
H
Hongze Cheng 已提交
1006
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1007 1008
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
      } else if (pTColumn->colId > pColData->cid) {  // NONE
H
Hongze Cheng 已提交
1009
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
H
Hongze Cheng 已提交
1010
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1011 1012 1013 1014
      } else {
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
      }
    } else {
H
Hongze Cheng 已提交
1015
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
H
Hongze Cheng 已提交
1016
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1017 1018 1019 1020 1021 1022
    }
  }

_exit:
  return code;
}
H
Hongze Cheng 已提交
1023
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
H
Hongze Cheng 已提交
1024 1025 1026 1027 1028
  int32_t code = 0;

  SKVIdx   *pKVIdx = (SKVIdx *)pRow->data;
  uint8_t  *pv = NULL;
  int32_t   iColData = 0;
H
Hongze Cheng 已提交
1029
  SColData *pColData = &aColData[iColData];
H
Hongze Cheng 已提交
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
  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 {
H
Hongze Cheng 已提交
1056 1057
            ASSERTS(0, "Invalid KV row format");
            return TSDB_CODE_IVLD_DATA_FMT;
H
Hongze Cheng 已提交
1058 1059 1060
          }

          int16_t cid;
H
Hongze Cheng 已提交
1061
          pData += tGetI16v(pData, &cid);
H
Hongze Cheng 已提交
1062 1063 1064

          if (TABS(cid) == pTColumn->colId) {
            if (cid < 0) {
H
Hongze Cheng 已提交
1065 1066 1067 1068 1069
              if (flag == 0) {
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
              } else {
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
              }
H
Hongze Cheng 已提交
1070 1071
              if (code) goto _exit;
            } else {
H
Hongze Cheng 已提交
1072
              uint32_t nData;
H
Hongze Cheng 已提交
1073 1074
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
                pData += tGetU32v(pData, &nData);
H
Hongze Cheng 已提交
1075 1076 1077 1078
              } else {
                nData = 0;
              }
              if (flag == 0) {
H
Hongze Cheng 已提交
1079 1080
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
              } else {
H
Hongze Cheng 已提交
1081
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
H
Hongze Cheng 已提交
1082
              }
H
Hongze Cheng 已提交
1083
              if (code) goto _exit;
H
Hongze Cheng 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
            }
            iCol++;
            goto _continue;
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
            break;
          } else {
            iCol++;
          }
        }

H
Hongze Cheng 已提交
1094
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
H
Hongze Cheng 已提交
1095 1096

      _continue:
H
Hongze Cheng 已提交
1097
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1098 1099
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
      } else if (pTColumn->colId > pColData->cid) {
H
Hongze Cheng 已提交
1100
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
H
Hongze Cheng 已提交
1101
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1102 1103 1104 1105
      } else {
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
      }
    } else {
H
Hongze Cheng 已提交
1106
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
H
Hongze Cheng 已提交
1107
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
H
Hongze Cheng 已提交
1108 1109 1110
    }
  }

H
Hongze Cheng 已提交
1111 1112 1113
_exit:
  return code;
}
H
Hongze Cheng 已提交
1114 1115 1116 1117 1118
/* flag > 0: forward update
 * flag == 0: append
 * flag < 0: backward update
 */
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
H
Hongze Cheng 已提交
1119 1120 1121 1122
  ASSERT(pRow->sver == pTSchema->version);
  ASSERT(nColData > 0);

  if (pRow->flag == HAS_NONE) {
H
Hongze Cheng 已提交
1123
    return tRowNoneUpsertColData(aColData, nColData, flag);
H
Hongze Cheng 已提交
1124
  } else if (pRow->flag == HAS_NULL) {
H
Hongze Cheng 已提交
1125
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
H
Hongze Cheng 已提交
1126
  } else if (pRow->flag >> 4) {  // KV row
H
Hongze Cheng 已提交
1127
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
H
Hongze Cheng 已提交
1128
  } else {  // TUPLE row
H
Hongze Cheng 已提交
1129
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
H
Hongze Cheng 已提交
1130 1131
  }
}
H
Hongze Cheng 已提交
1132

H
Hongze Cheng 已提交
1133
// STag ========================================
H
Hongze Cheng 已提交
1134 1135
static int tTagValCmprFn(const void *p1, const void *p2) {
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
1136
    return -1;
H
Hongze Cheng 已提交
1137
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
1138 1139
    return 1;
  }
H
Hongze Cheng 已提交
1140

H
Hongze Cheng 已提交
1141 1142
  return 0;
}
H
Hongze Cheng 已提交
1143 1144 1145
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
}
C
Cary Xu 已提交
1146 1147 1148 1149 1150 1151 1152

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};
1153
      strncpy(tmpVal, val, vlen > 31 ? 31 : vlen);
C
Cary Xu 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
      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 已提交
1192 1193 1194
    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 已提交
1195 1196 1197 1198 1199 1200 1201
    default:
      ASSERT(0);
      break;
  }
}

void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
H
Hongze Cheng 已提交
1202 1203
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
  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 已提交
1214
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
1215 1216 1217 1218 1219 1220 1221 1222
    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 已提交
1223
    } else {
1224
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
C
Cary Xu 已提交
1225
    }
1226
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
C
Cary Xu 已提交
1227
    tGetTagVal(p + offset, &tagVal, isJson);
1228
    if (IS_VAR_DATA_TYPE(tagVal.type)) {
wmmhello's avatar
wmmhello 已提交
1229
      debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
1230
    } else {
wmmhello's avatar
wmmhello 已提交
1231 1232
      debugPrintTagVal(tagVal.type, &tagVal.i64, tDataTypes[tagVal.type].bytes, __func__, __LINE__);
    }
C
Cary Xu 已提交
1233 1234 1235 1236
  }
  printf("\n");
}

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

H
Hongze Cheng 已提交
1240 1241 1242 1243 1244 1245
  // key
  if (isJson) {
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
  } else {
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
  }
H
Hongze Cheng 已提交
1246

H
Hongze Cheng 已提交
1247 1248 1249 1250 1251 1252 1253
  // 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 已提交
1254
    p = p ? p + n : p;
wmmhello's avatar
wmmhello 已提交
1255
    n += tDataTypes[pTagVal->type].bytes;
1256
    if (p) memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
H
Hongze Cheng 已提交
1257 1258
  }

H
Hongze Cheng 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
  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 已提交
1269 1270
  }

H
Hongze Cheng 已提交
1271 1272
  // type
  n += tGetI8(p + n, &pTagVal->type);
H
Hongze Cheng 已提交
1273

H
Hongze Cheng 已提交
1274 1275 1276 1277
  // value
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
  } else {
wmmhello's avatar
wmmhello 已提交
1278
    memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
wmmhello's avatar
wmmhello 已提交
1279
    n += tDataTypes[pTagVal->type].bytes;
H
Hongze Cheng 已提交
1280 1281 1282 1283
  }

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

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

H
Hongze Cheng 已提交
1287 1288 1289 1290 1291
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 已提交
1292 1293
}

H
Hongze Cheng 已提交
1294
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
H
Hongze Cheng 已提交
1295 1296 1297
  int32_t  code = 0;
  uint8_t *p = NULL;
  int16_t  n = 0;
H
Hongze Cheng 已提交
1298
  int16_t  nTag = taosArrayGetSize(pArray);
H
Hongze Cheng 已提交
1299 1300
  int32_t  szTag = 0;
  int8_t   isLarge = 0;
H
Hongze Cheng 已提交
1301 1302 1303

  // sort
  if (isJson) {
wafwerar's avatar
wafwerar 已提交
1304
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
H
Hongze Cheng 已提交
1305
  } else {
wafwerar's avatar
wafwerar 已提交
1306
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
H
Hongze Cheng 已提交
1307 1308 1309
  }

  // get size
H
Hongze Cheng 已提交
1310
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
1311
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
1312
  }
H
Hongze Cheng 已提交
1313 1314 1315 1316 1317 1318
  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 已提交
1319

H
Hongze Cheng 已提交
1320
  ASSERT(szTag <= INT16_MAX);
H
Hongze Cheng 已提交
1321 1322

  // build tag
C
Cary Xu 已提交
1323
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
H
Hongze Cheng 已提交
1324 1325 1326 1327
  if ((*ppTag) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1328 1329 1330 1331 1332 1333 1334
  (*ppTag)->flags = 0;
  if (isJson) {
    (*ppTag)->flags |= TD_TAG_JSON;
  }
  if (isLarge) {
    (*ppTag)->flags |= TD_TAG_LARGE;
  }
H
Hongze Cheng 已提交
1335 1336 1337
  (*ppTag)->len = szTag;
  (*ppTag)->nTag = nTag;
  (*ppTag)->ver = version;
H
Hongze Cheng 已提交
1338

H
Hongze Cheng 已提交
1339 1340 1341 1342 1343
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
  } else {
    p = (uint8_t *)&(*ppTag)->idx[nTag];
  }
H
Hongze Cheng 已提交
1344 1345
  n = 0;
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
1346 1347 1348 1349 1350
    if (isLarge) {
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
    } else {
      (*ppTag)->idx[iTag] = n;
    }
H
Hongze Cheng 已提交
1351
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
1352
  }
1353
#ifdef TD_DEBUG_PRINT_TAG
C
Cary Xu 已提交
1354
  debugPrintSTag(*ppTag, __func__, __LINE__);
1355
#endif
C
Cary Xu 已提交
1356

H
Hongze Cheng 已提交
1357 1358 1359 1360
  return code;

_err:
  return code;
H
Hongze Cheng 已提交
1361 1362 1363 1364 1365 1366
}

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

1367
char *tTagValToData(const STagVal *value, bool isJson) {
H
Haojun Liao 已提交
1368 1369 1370 1371
  if (!value) {
    return NULL;
  }

1372
  char  *data = NULL;
wmmhello's avatar
wmmhello 已提交
1373 1374 1375 1376
  int8_t typeBytes = 0;
  if (isJson) {
    typeBytes = CHAR_BYTES;
  }
H
Haojun Liao 已提交
1377

1378
  if (IS_VAR_DATA_TYPE(value->type)) {
wmmhello's avatar
wmmhello 已提交
1379
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
H
Haojun Liao 已提交
1380 1381 1382 1383 1384 1385 1386 1387
    if (data == NULL) {
      return NULL;
    }

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

wmmhello's avatar
wmmhello 已提交
1388 1389
    varDataLen(data + typeBytes) = value->nData;
    memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
1390 1391
  } else {
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
wmmhello's avatar
wmmhello 已提交
1392 1393 1394 1395 1396
  }

  return data;
}

C
Cary Xu 已提交
1397
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
H
Hongze Cheng 已提交
1398
  if (!pTag || !pTagVal) {
wmmhello's avatar
wmmhello 已提交
1399 1400 1401
    return false;
  }

H
Hongze Cheng 已提交
1402 1403 1404
  int16_t  lidx = 0;
  int16_t  ridx = pTag->nTag - 1;
  int16_t  midx;
H
Hongze Cheng 已提交
1405 1406 1407 1408
  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 已提交
1409 1410 1411
  STagVal  tv;
  int      c;

H
Hongze Cheng 已提交
1412 1413 1414 1415 1416 1417
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }

H
Hongze Cheng 已提交
1418 1419 1420 1421 1422
  pTagVal->type = TSDB_DATA_TYPE_NULL;
  pTagVal->pData = NULL;
  pTagVal->nData = 0;
  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;
H
Hongze Cheng 已提交
1423 1424 1425 1426 1427
    if (isLarge) {
      offset = ((int16_t *)pTag->idx)[midx];
    } else {
      offset = pTag->idx[midx];
    }
H
Hongze Cheng 已提交
1428

H
Hongze Cheng 已提交
1429 1430
    tGetTagVal(p + offset, &tv, isJson);
    if (isJson) {
H
Hongze Cheng 已提交
1431
      c = tTagValJsonCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1432
    } else {
H
Hongze Cheng 已提交
1433
      c = tTagValCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1434 1435
    }

H
Hongze Cheng 已提交
1436 1437 1438 1439
    if (c < 0) {
      ridx = midx - 1;
    } else if (c > 0) {
      lidx = midx + 1;
H
Hongze Cheng 已提交
1440
    } else {
H
Hongze Cheng 已提交
1441
      memcpy(pTagVal, &tv, sizeof(tv));
C
Cary Xu 已提交
1442
      return true;
H
Hongze Cheng 已提交
1443 1444
    }
  }
C
Cary Xu 已提交
1445
  return false;
H
Hongze Cheng 已提交
1446 1447
}

H
more  
Hongze Cheng 已提交
1448 1449
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
H
Hongze Cheng 已提交
1450 1451
}

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

C
Cary Xu 已提交
1454
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
H
Hongze Cheng 已提交
1455
  int32_t  code = 0;
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
  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 已提交
1466

H
more  
Hongze Cheng 已提交
1467
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
H
Hongze Cheng 已提交
1468
  if (*ppArray == NULL) {
H
Hongze Cheng 已提交
1469 1470 1471 1472 1473
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
1474 1475 1476 1477 1478 1479
    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 已提交
1480
    taosArrayPush(*ppArray, &tv);
H
Hongze Cheng 已提交
1481 1482 1483 1484 1485 1486 1487 1488
  }

  return code;

_err:
  return code;
}

wmmhello's avatar
wmmhello 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
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 已提交
1509
// STSchema ========================================
H
Hongze Cheng 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
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 已提交
1523
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
H
Hongze Cheng 已提交
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
  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 已提交
1536 1537 1538 1539 1540 1541 1542 1543
    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 已提交
1544 1545 1546
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
  }

H
Hongze Cheng 已提交
1547 1548 1549 1550
#if 1  // todo : remove this
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
#endif

H
Hongze Cheng 已提交
1551 1552 1553
  return pTSchema;
}

H
refact  
Hongze Cheng 已提交
1554
// SColData ========================================
H
Hongze Cheng 已提交
1555 1556 1557 1558
void tColDataDestroy(void *ph) {
  SColData *pColData = (SColData *)ph;

  tFree(pColData->pBitMap);
H
Hongze Cheng 已提交
1559
  tFree(pColData->aOffset);
H
Hongze Cheng 已提交
1560 1561 1562 1563 1564 1565 1566
  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 已提交
1567
  tColDataClear(pColData);
H
Hongze Cheng 已提交
1568 1569
}

H
Hongze Cheng 已提交
1570
void tColDataClear(SColData *pColData) {
H
Hongze Cheng 已提交
1571 1572 1573
  pColData->numOfNone = 0;
  pColData->numOfNull = 0;
  pColData->numOfValue = 0;
H
Hongze Cheng 已提交
1574 1575 1576 1577 1578
  pColData->nVal = 0;
  pColData->flag = 0;
  pColData->nData = 0;
}

D
dapan1121 已提交
1579 1580 1581 1582 1583 1584 1585 1586
void tColDataDeepClear(SColData *pColData) {
  pColData->pBitMap = NULL;
  pColData->aOffset = NULL;
  pColData->pData = NULL;

  tColDataClear(pColData);
}

H
Hongze Cheng 已提交
1587
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1588 1589 1590
  int32_t code = 0;

  if (IS_VAR_DATA_TYPE(pColData->type)) {
H
Hongze Cheng 已提交
1591
    code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
H
Hongze Cheng 已提交
1592 1593 1594
    if (code) goto _exit;
    pColData->aOffset[pColData->nVal] = pColData->nData;

H
Hongze Cheng 已提交
1595 1596
    if (nData) {
      code = tRealloc(&pColData->pData, pColData->nData + nData);
H
Hongze Cheng 已提交
1597
      if (code) goto _exit;
H
Hongze Cheng 已提交
1598 1599
      memcpy(pColData->pData + pColData->nData, pData, nData);
      pColData->nData += nData;
H
Hongze Cheng 已提交
1600 1601
    }
  } else {
H
Hongze Cheng 已提交
1602
    ASSERT(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal);
H
Hongze Cheng 已提交
1603 1604
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1605 1606 1607 1608 1609
    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 已提交
1610
    pColData->nData += tDataTypes[pColData->type].bytes;
H
Hongze Cheng 已提交
1611
  }
H
Hongze Cheng 已提交
1612
  pColData->nVal++;
H
Hongze Cheng 已提交
1613 1614 1615 1616

_exit:
  return code;
}
H
Hongze Cheng 已提交
1617
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1618
  pColData->flag = HAS_VALUE;
H
Hongze Cheng 已提交
1619
  pColData->numOfValue++;
H
Hongze Cheng 已提交
1620
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1621
}
H
Hongze Cheng 已提交
1622
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1623
  pColData->flag = HAS_NONE;
H
Hongze Cheng 已提交
1624
  pColData->numOfNone++;
H
Hongze Cheng 已提交
1625
  pColData->nVal++;
H
Hongze Cheng 已提交
1626
  return 0;
H
Hongze Cheng 已提交
1627
}
H
Hongze Cheng 已提交
1628
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1629
  pColData->flag = HAS_NULL;
H
Hongze Cheng 已提交
1630
  pColData->numOfNull++;
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 tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1635 1636
  int32_t code = 0;

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

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

  pColData->flag |= HAS_VALUE;
H
Hongze Cheng 已提交
1645
  pColData->numOfValue++;
H
Hongze Cheng 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658

  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 已提交
1659
  }
H
Hongze Cheng 已提交
1660

H
Hongze Cheng 已提交
1661
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1662
}
H
Hongze Cheng 已提交
1663
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1664
  pColData->nVal++;
H
Hongze Cheng 已提交
1665
  pColData->numOfNone++;
H
Hongze Cheng 已提交
1666
  return 0;
H
Hongze Cheng 已提交
1667
}
H
Hongze Cheng 已提交
1668
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1669 1670
  int32_t code = 0;

H
Hongze Cheng 已提交
1671 1672 1673
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1674

H
Hongze Cheng 已提交
1675
  memset(pColData->pBitMap, 0, nBit);
H
Hongze Cheng 已提交
1676
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1677

H
Hongze Cheng 已提交
1678
  pColData->flag |= HAS_NULL;
H
Hongze Cheng 已提交
1679
  pColData->numOfNull++;
H
Hongze Cheng 已提交
1680
  pColData->nVal++;
H
Hongze Cheng 已提交
1681

H
Hongze Cheng 已提交
1682 1683
  return code;
}
H
Hongze Cheng 已提交
1684
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1685
  int32_t code = 0;
H
Hongze Cheng 已提交
1686

H
Hongze Cheng 已提交
1687 1688 1689 1690 1691
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;

  memset(pColData->pBitMap, 0, nBit);
H
Hongze Cheng 已提交
1692
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1693 1694

  pColData->flag |= HAS_VALUE;
H
Hongze Cheng 已提交
1695
  pColData->numOfValue++;
H
Hongze Cheng 已提交
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707

  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 已提交
1708
    }
H
Hongze Cheng 已提交
1709
  }
H
Hongze Cheng 已提交
1710

H
Hongze Cheng 已提交
1711
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1712
}
H
Hongze Cheng 已提交
1713
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1714 1715
  int32_t code = 0;

H
Hongze Cheng 已提交
1716 1717 1718
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1719

H
Hongze Cheng 已提交
1720
  memset(pColData->pBitMap, 255, nBit);
H
Hongze Cheng 已提交
1721
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1722

H
Hongze Cheng 已提交
1723
  pColData->flag |= HAS_NONE;
H
Hongze Cheng 已提交
1724
  pColData->numOfNone++;
H
Hongze Cheng 已提交
1725
  pColData->nVal++;
H
Hongze Cheng 已提交
1726

H
Hongze Cheng 已提交
1727 1728
  return code;
}
H
Hongze Cheng 已提交
1729
static FORCE_INLINE int32_t tColDataAppendValue22(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1730
  pColData->nVal++;
H
Hongze Cheng 已提交
1731
  pColData->numOfNull++;
H
Hongze Cheng 已提交
1732
  return 0;
H
Hongze Cheng 已提交
1733
}
H
Hongze Cheng 已提交
1734
static FORCE_INLINE int32_t tColDataAppendValue30(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1735
  int32_t code = 0;
H
Hongze Cheng 已提交
1736

H
Hongze Cheng 已提交
1737
  pColData->flag |= HAS_VALUE;
H
Hongze Cheng 已提交
1738
  pColData->numOfValue++;
H
Hongze Cheng 已提交
1739

H
Hongze Cheng 已提交
1740 1741 1742 1743 1744
  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++) {
H
Hongze Cheng 已提交
1745
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
H
Hongze Cheng 已提交
1746
  }
H
Hongze Cheng 已提交
1747
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
H
Hongze Cheng 已提交
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762

  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 已提交
1763
    }
H
Hongze Cheng 已提交
1764
  }
H
Hongze Cheng 已提交
1765

H
Hongze Cheng 已提交
1766
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1767
}
H
Hongze Cheng 已提交
1768
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1769 1770
  int32_t code = 0;

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

H
Hongze Cheng 已提交
1774
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1775
  pColData->numOfNone++;
H
Hongze Cheng 已提交
1776
  pColData->nVal++;
H
Hongze Cheng 已提交
1777

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

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_EX(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1787
  pColData->numOfNull++;
H
Hongze Cheng 已提交
1788
  pColData->nVal++;
H
Hongze Cheng 已提交
1789

H
Hongze Cheng 已提交
1790 1791
  return code;
}
H
Hongze Cheng 已提交
1792
static FORCE_INLINE int32_t tColDataAppendValue40(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1793
  pColData->numOfValue++;
H
Hongze Cheng 已提交
1794 1795
  return tColDataPutValue(pColData, pData, nData);
}
H
Hongze Cheng 已提交
1796
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1797 1798 1799
  int32_t code = 0;

  pColData->flag |= HAS_NONE;
H
Hongze Cheng 已提交
1800
  pColData->numOfNone++;
H
Hongze Cheng 已提交
1801 1802 1803 1804 1805 1806

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

  memset(pColData->pBitMap, 255, nBit);
H
Hongze Cheng 已提交
1807
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1808

H
Hongze Cheng 已提交
1809
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1810
}
H
Hongze Cheng 已提交
1811
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1812 1813
  int32_t code = 0;

H
Hongze Cheng 已提交
1814
  pColData->flag |= HAS_NULL;
H
Hongze Cheng 已提交
1815
  pColData->numOfNull++;
H
Hongze Cheng 已提交
1816

H
Hongze Cheng 已提交
1817 1818 1819
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
  code = tRealloc(&pColData->pBitMap, nBit);
  if (code) return code;
H
Hongze Cheng 已提交
1820

H
Hongze Cheng 已提交
1821
  memset(pColData->pBitMap, 255, nBit);
H
Hongze Cheng 已提交
1822
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1823

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

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

H
Hongze Cheng 已提交
1832
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1833
  pColData->numOfValue++;
H
Hongze Cheng 已提交
1834

H
Hongze Cheng 已提交
1835
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1836
}
H
Hongze Cheng 已提交
1837
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1838 1839 1840 1841 1842
  int32_t code = 0;

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

H
Hongze Cheng 已提交
1843
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1844
  pColData->numOfNone++;
H
Hongze Cheng 已提交
1845

H
Hongze Cheng 已提交
1846
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1847
}
H
Hongze Cheng 已提交
1848
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1849 1850 1851
  int32_t code = 0;

  pColData->flag |= HAS_NULL;
H
Hongze Cheng 已提交
1852
  pColData->numOfNull++;
H
Hongze Cheng 已提交
1853 1854 1855 1856 1857 1858

  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++) {
H
Hongze Cheng 已提交
1859
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
H
Hongze Cheng 已提交
1860
  }
H
Hongze Cheng 已提交
1861
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1862 1863 1864 1865

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

H
Hongze Cheng 已提交
1866
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1867
}
H
Hongze Cheng 已提交
1868
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1869 1870
  int32_t code = 0;

H
Hongze Cheng 已提交
1871 1872
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;
H
Hongze Cheng 已提交
1873
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1874
  pColData->numOfValue++;
H
Hongze Cheng 已提交
1875

H
Hongze Cheng 已提交
1876
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1877
}
H
Hongze Cheng 已提交
1878
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1879
  int32_t code = 0;
H
Hongze Cheng 已提交
1880

H
Hongze Cheng 已提交
1881
  pColData->flag |= HAS_NONE;
H
Hongze Cheng 已提交
1882
  pColData->numOfNone++;
H
Hongze Cheng 已提交
1883 1884 1885 1886 1887 1888

  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++) {
H
Hongze Cheng 已提交
1889
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
H
Hongze Cheng 已提交
1890
  }
H
Hongze Cheng 已提交
1891
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1892 1893 1894 1895

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

H
Hongze Cheng 已提交
1896
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1897
}
H
Hongze Cheng 已提交
1898
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1899 1900 1901 1902
  int32_t code = 0;

  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
  if (code) return code;
H
Hongze Cheng 已提交
1903
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1904
  pColData->numOfNull++;
H
Hongze Cheng 已提交
1905

H
Hongze Cheng 已提交
1906
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1907
}
H
Hongze Cheng 已提交
1908
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1909 1910
  int32_t code = 0;

H
Hongze Cheng 已提交
1911
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
H
Hongze Cheng 已提交
1912
  if (code) return code;
H
Hongze Cheng 已提交
1913
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
H
Hongze Cheng 已提交
1914
  pColData->numOfValue++;
H
Hongze Cheng 已提交
1915

H
Hongze Cheng 已提交
1916
  return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
1917
}
H
Hongze Cheng 已提交
1918
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1919 1920 1921 1922
  int32_t code = 0;

  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
  if (code) return code;
H
Hongze Cheng 已提交
1923
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1924
  pColData->numOfNone++;
H
Hongze Cheng 已提交
1925

H
Hongze Cheng 已提交
1926
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1927
}
H
Hongze Cheng 已提交
1928
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
1929 1930 1931 1932
  int32_t code = 0;

  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
  if (code) return code;
H
Hongze Cheng 已提交
1933
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1934
  pColData->numOfNull++;
H
Hongze Cheng 已提交
1935

H
Hongze Cheng 已提交
1936
  return tColDataPutValue(pColData, NULL, 0);
H
Hongze Cheng 已提交
1937
}
H
Hongze Cheng 已提交
1938
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData) = {
H
Hongze Cheng 已提交
1939 1940 1941
    {tColDataAppendValue00, tColDataAppendValue01, tColDataAppendValue02},  // 0
    {tColDataAppendValue10, tColDataAppendValue11, tColDataAppendValue12},  // HAS_NONE
    {tColDataAppendValue20, tColDataAppendValue21, tColDataAppendValue22},  // HAS_NULL
H
Hongze Cheng 已提交
1942
    {tColDataAppendValue30, tColDataAppendValue31, tColDataAppendValue32},  // HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
1943 1944 1945 1946
    {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 已提交
1947 1948

    //       VALUE                  NONE                     NULL
H
Hongze Cheng 已提交
1949
};
H
refact  
Hongze Cheng 已提交
1950
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1951
  ASSERT(pColData->cid == pColVal->cid && pColData->type == pColVal->type);
H
Hongze Cheng 已提交
1952 1953 1954
  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 已提交
1955 1956
}

H
Hongze Cheng 已提交
1957
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
1958 1959 1960 1961 1962 1963 1964 1965
  pColData->numOfNone--;
  pColData->nVal--;
  if (pColData->numOfNone) {
    return tColDataAppendValue10(pColData, pData, nData);
  } else {
    pColData->flag = 0;
    return tColDataAppendValue00(pColData, pData, nData);
  }
H
Hongze Cheng 已提交
1966
}
H
Hongze Cheng 已提交
1967
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
1968 1969 1970 1971 1972 1973 1974 1975
  pColData->numOfNone--;
  pColData->nVal--;
  if (pColData->numOfNone) {
    return tColDataAppendValue12(pColData, pData, nData);
  } else {
    pColData->flag = 0;
    return tColDataAppendValue02(pColData, pData, nData);
  }
H
Hongze Cheng 已提交
1976 1977
  return 0;
}
H
Hongze Cheng 已提交
1978
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
  if (forward) {
    pColData->numOfNull--;
    pColData->nVal--;
    if (pColData->numOfNull) {
      return tColDataAppendValue20(pColData, pData, nData);
    } else {
      pColData->flag = 0;
      return tColDataAppendValue00(pColData, pData, nData);
    }
  }
H
Hongze Cheng 已提交
1989 1990
  return 0;
}
H
Hongze Cheng 已提交
1991
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
1992
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
H
Hongze Cheng 已提交
1993 1994 1995 1996 1997 1998 1999 2000
    pColData->numOfNone--;
    pColData->nVal--;
    if (pColData->numOfNone) {
      return tColDataAppendValue30(pColData, pData, nData);
    } else {
      pColData->flag = HAS_NULL;
      return tColDataAppendValue20(pColData, pData, nData);
    }
H
Hongze Cheng 已提交
2001
  } else if (forward) {  // NULL ==> VALUE
H
Hongze Cheng 已提交
2002 2003 2004 2005 2006 2007 2008 2009 2010
    pColData->numOfNull--;
    pColData->nVal--;
    if (pColData->numOfNull) {
      return tColDataAppendValue30(pColData, pData, nData);
    } else {
      pColData->flag = HAS_NONE;
      return tColDataAppendValue10(pColData, pData, nData);
    }
  }
H
Hongze Cheng 已提交
2011 2012
  return 0;
}
H
Hongze Cheng 已提交
2013
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2014 2015 2016 2017 2018 2019 2020 2021 2022
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
    pColData->numOfNone--;
    pColData->numOfNull++;
    if (pColData->numOfNone) {
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
    } else {
      pColData->flag = HAS_NULL;
    }
  }
H
Hongze Cheng 已提交
2023 2024
  return 0;
}
H
Hongze Cheng 已提交
2025
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2026
  if (forward) {  // VALUE ==> VALUE
H
Hongze Cheng 已提交
2027
    pColData->nVal--;
H
Hongze Cheng 已提交
2028 2029
    if (IS_VAR_DATA_TYPE(pColData->type)) {
      pColData->nData = pColData->aOffset[pColData->nVal];
H
Hongze Cheng 已提交
2030
    } else {
H
Hongze Cheng 已提交
2031
      pColData->nData -= TYPE_BYTES[pColData->type];
H
Hongze Cheng 已提交
2032
    }
H
Hongze Cheng 已提交
2033
    return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
2034
  }
H
Hongze Cheng 已提交
2035 2036
  return 0;
}
H
Hongze Cheng 已提交
2037
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2038
  if (forward) {  // VALUE ==> NULL
H
Hongze Cheng 已提交
2039 2040 2041
    pColData->numOfValue--;
    pColData->nVal--;
    if (pColData->numOfValue) {
H
Hongze Cheng 已提交
2042 2043 2044 2045 2046
      if (IS_VAR_DATA_TYPE(pColData->type)) {
        pColData->nData = pColData->aOffset[pColData->nVal];
      } else {
        pColData->nData -= TYPE_BYTES[pColData->type];
      }
H
Hongze Cheng 已提交
2047 2048 2049
      return tColDataAppendValue42(pColData, pData, nData);
    } else {
      pColData->flag = 0;
H
Hongze Cheng 已提交
2050
      pColData->nData = 0;
H
Hongze Cheng 已提交
2051 2052 2053
      return tColDataAppendValue02(pColData, pData, nData);
    }
  }
H
Hongze Cheng 已提交
2054 2055
  return 0;
}
H
Hongze Cheng 已提交
2056
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2057
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
H
Hongze Cheng 已提交
2058 2059
    pColData->numOfNone--;
    pColData->nVal--;
H
Hongze Cheng 已提交
2060 2061 2062
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
      pColData->nData -= TYPE_BYTES[pColData->type];
    }
H
Hongze Cheng 已提交
2063 2064 2065 2066 2067 2068 2069 2070
    if (pColData->numOfNone) {
      return tColDataAppendValue50(pColData, pData, nData);
    } else {
      pColData->flag = HAS_VALUE;
      return tColDataAppendValue40(pColData, pData, nData);
    }
  } else if (forward) {  // VALUE ==> VALUE
    pColData->nVal--;
H
Hongze Cheng 已提交
2071 2072
    if (IS_VAR_DATA_TYPE(pColData->type)) {
      pColData->nData = pColData->aOffset[pColData->nVal];
H
Hongze Cheng 已提交
2073
    } else {
H
Hongze Cheng 已提交
2074
      pColData->nData -= TYPE_BYTES[pColData->type];
H
Hongze Cheng 已提交
2075
    }
H
Hongze Cheng 已提交
2076
    return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
2077
  }
H
Hongze Cheng 已提交
2078 2079
  return 0;
}
H
Hongze Cheng 已提交
2080
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2081
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
H
Hongze Cheng 已提交
2082 2083
    pColData->numOfNone--;
    pColData->nVal--;
H
Hongze Cheng 已提交
2084 2085 2086
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
      pColData->nData -= TYPE_BYTES[pColData->type];
    }
H
Hongze Cheng 已提交
2087 2088 2089
    if (pColData->numOfNone) {
      return tColDataAppendValue52(pColData, pData, nData);
    } else {
H
Hongze Cheng 已提交
2090
      pColData->flag = HAS_VALUE;
H
Hongze Cheng 已提交
2091 2092 2093 2094 2095 2096
      return tColDataAppendValue42(pColData, pData, nData);
    }
  } else if (forward) {  // VALUE ==> NULL
    pColData->numOfValue--;
    pColData->nVal--;
    if (pColData->numOfValue) {
H
Hongze Cheng 已提交
2097 2098 2099 2100 2101
      if (IS_VAR_DATA_TYPE(pColData->type)) {
        pColData->nData = pColData->aOffset[pColData->nVal];
      } else {
        pColData->nData -= TYPE_BYTES[pColData->type];
      }
H
Hongze Cheng 已提交
2102 2103
      return tColDataAppendValue52(pColData, pData, nData);
    } else {
H
Hongze Cheng 已提交
2104 2105
      pColData->flag = HAS_NONE;
      pColData->nData = 0;
H
Hongze Cheng 已提交
2106 2107 2108
      return tColDataAppendValue12(pColData, pData, nData);
    }
  }
H
Hongze Cheng 已提交
2109 2110
  return 0;
}
H
Hongze Cheng 已提交
2111
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2112 2113 2114 2115
  if (forward) {
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
      pColData->numOfNull--;
      pColData->nVal--;
H
Hongze Cheng 已提交
2116 2117 2118
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
        pColData->nData -= TYPE_BYTES[pColData->type];
      }
H
Hongze Cheng 已提交
2119 2120 2121 2122 2123 2124 2125 2126
      if (pColData->numOfNull) {
        return tColDataAppendValue60(pColData, pData, nData);
      } else {
        pColData->flag = HAS_VALUE;
        return tColDataAppendValue40(pColData, pData, nData);
      }
    } else {  // VALUE ==> VALUE
      pColData->nVal--;
H
Hongze Cheng 已提交
2127 2128
      if (IS_VAR_DATA_TYPE(pColData->type)) {
        pColData->nData = pColData->aOffset[pColData->nVal];
H
Hongze Cheng 已提交
2129
      } else {
H
Hongze Cheng 已提交
2130
        pColData->nData -= TYPE_BYTES[pColData->type];
H
Hongze Cheng 已提交
2131
      }
H
Hongze Cheng 已提交
2132
      return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
2133 2134
    }
  }
H
Hongze Cheng 已提交
2135 2136
  return 0;
}
H
Hongze Cheng 已提交
2137
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2138 2139 2140 2141
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
    pColData->numOfValue--;
    pColData->nVal--;
    if (pColData->numOfValue) {
H
Hongze Cheng 已提交
2142 2143 2144 2145 2146
      if (IS_VAR_DATA_TYPE(pColData->type)) {
        pColData->nData = pColData->aOffset[pColData->nVal];
      } else {
        pColData->nData -= TYPE_BYTES[pColData->type];
      }
H
Hongze Cheng 已提交
2147 2148 2149
      return tColDataAppendValue62(pColData, pData, nData);
    } else {
      pColData->flag = HAS_NULL;
H
Hongze Cheng 已提交
2150
      pColData->nData = 0;
H
Hongze Cheng 已提交
2151 2152 2153
      return tColDataAppendValue20(pColData, pData, nData);
    }
  }
H
Hongze Cheng 已提交
2154 2155
  return 0;
}
H
Hongze Cheng 已提交
2156
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2157 2158 2159 2160 2161 2162
  int32_t code = 0;

  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
  if (bv == 0) {  // NONE ==> VALUE
    pColData->numOfNone--;
    pColData->nVal--;
H
Hongze Cheng 已提交
2163 2164 2165
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
      pColData->nData -= TYPE_BYTES[pColData->type];
    }
H
Hongze Cheng 已提交
2166 2167 2168 2169 2170 2171
    if (pColData->numOfNone) {
      return tColDataAppendValue70(pColData, pData, nData);
    } else {
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
      }
H
Hongze Cheng 已提交
2172
      pColData->flag = (HAS_VALUE | HAS_NULL);
H
Hongze Cheng 已提交
2173 2174 2175 2176 2177 2178
      return tColDataAppendValue60(pColData, pData, nData);
    }
  } else if (bv == 1) {  // NULL ==> VALUE
    if (forward) {
      pColData->numOfNull--;
      pColData->nVal--;
H
Hongze Cheng 已提交
2179 2180 2181
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
        pColData->nData -= TYPE_BYTES[pColData->type];
      }
H
Hongze Cheng 已提交
2182 2183 2184 2185 2186 2187
      if (pColData->numOfNull) {
        return tColDataAppendValue70(pColData, pData, nData);
      } else {
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
        }
H
Hongze Cheng 已提交
2188
        pColData->flag = (HAS_VALUE | HAS_NONE);
H
Hongze Cheng 已提交
2189 2190 2191 2192 2193 2194
        return tColDataAppendValue50(pColData, pData, nData);
      }
    }
  } else if (bv == 2) {  // VALUE ==> VALUE
    if (forward) {
      pColData->nVal--;
H
Hongze Cheng 已提交
2195 2196
      if (IS_VAR_DATA_TYPE(pColData->type)) {
        pColData->nData = pColData->aOffset[pColData->nVal];
H
Hongze Cheng 已提交
2197
      } else {
H
Hongze Cheng 已提交
2198
        pColData->nData -= TYPE_BYTES[pColData->type];
H
Hongze Cheng 已提交
2199
      }
H
Hongze Cheng 已提交
2200
      return tColDataPutValue(pColData, pData, nData);
H
Hongze Cheng 已提交
2201 2202 2203 2204
    }
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
2205 2206
  return 0;
}
H
Hongze Cheng 已提交
2207
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
H
Hongze Cheng 已提交
2208 2209 2210 2211
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
  if (bv == 0) {  // NONE ==> NULL
    pColData->numOfNone--;
    pColData->nVal--;
H
Hongze Cheng 已提交
2212 2213 2214
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
      pColData->nData -= TYPE_BYTES[pColData->type];
    }
H
Hongze Cheng 已提交
2215 2216 2217 2218 2219 2220
    if (pColData->numOfNone) {
      return tColDataAppendValue72(pColData, pData, nData);
    } else {
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
      }
H
Hongze Cheng 已提交
2221
      pColData->flag = (HAS_VALUE | HAS_NULL);
H
Hongze Cheng 已提交
2222 2223 2224 2225 2226 2227
      return tColDataAppendValue62(pColData, pData, nData);
    }
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
    pColData->numOfValue--;
    pColData->nVal--;
    if (pColData->numOfValue) {
H
Hongze Cheng 已提交
2228 2229 2230 2231 2232
      if (IS_STR_DATA_TYPE(pColData->type)) {
        pColData->nData = pColData->aOffset[pColData->nVal];
      } else {
        pColData->nData -= TYPE_BYTES[pColData->type];
      }
H
Hongze Cheng 已提交
2233 2234 2235 2236 2237
      return tColDataAppendValue72(pColData, pData, nData);
    } else {
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
      }
H
Hongze Cheng 已提交
2238 2239
      pColData->flag = (HAS_NULL | HAS_NONE);
      pColData->nData = 0;
H
Hongze Cheng 已提交
2240 2241 2242
      return tColDataAppendValue32(pColData, pData, nData);
    }
  }
H
Hongze Cheng 已提交
2243 2244
  return 0;
}
H
Hongze Cheng 已提交
2245
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
H
Hongze Cheng 已提交
2246 2247
    {NULL, NULL, NULL},                                    // 0
    {tColDataUpdateValue10, NULL, tColDataUpdateValue12},  // HAS_NONE
H
Hongze Cheng 已提交
2248
    {tColDataUpdateValue20, NULL, NULL},                   // HAS_NULL
H
Hongze Cheng 已提交
2249 2250 2251 2252 2253 2254 2255
    {tColDataUpdateValue30, NULL, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
    {tColDataUpdateValue40, NULL, tColDataUpdateValue42},  // HAS_VALUE
    {tColDataUpdateValue50, NULL, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
    {tColDataUpdateValue60, NULL, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
    {tColDataUpdateValue70, NULL, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE

    //    VALUE             NONE        NULL
H
Hongze Cheng 已提交
2256
};
H
Hongze Cheng 已提交
2257
int32_t tColDataUpdateValue(SColData *pColData, SColVal *pColVal, bool forward) {
H
Hongze Cheng 已提交
2258
  ASSERT(pColData->cid == pColVal->cid && pColData->type == pColVal->type);
H
Hongze Cheng 已提交
2259 2260 2261 2262
  ASSERT(pColData->nVal > 0);

  if (tColDataUpdateValueImpl[pColData->flag][pColVal->flag] == NULL) return 0;

H
Hongze Cheng 已提交
2263 2264
  return tColDataUpdateValueImpl[pColData->flag][pColVal->flag](
      pColData, IS_VAR_DATA_TYPE(pColData->type) ? pColVal->value.pData : (uint8_t *)&pColVal->value.val,
H
Hongze Cheng 已提交
2265
      pColVal->value.nData, forward);
H
Hongze Cheng 已提交
2266 2267
}

H
Hongze Cheng 已提交
2268 2269 2270 2271 2272 2273
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 已提交
2274 2275
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
  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 已提交
2297
    memcpy(&value.val, pColData->pData + tDataTypes[pColData->type].bytes * iVal, tDataTypes[pColData->type].bytes);
H
Hongze Cheng 已提交
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 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
  }
  *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 已提交
2354
  ASSERT(iVal >= 0 && iVal < pColData->nVal && pColData->flag);
H
Hongze Cheng 已提交
2355 2356 2357
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
}

H
Haojun Liao 已提交
2358
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
H
Hongze Cheng 已提交
2359 2360
  switch (pColData->flag) {
    case HAS_NONE:
H
Hongze Cheng 已提交
2361
      return 0;
H
Hongze Cheng 已提交
2362
    case HAS_NULL:
H
Hongze Cheng 已提交
2363
      return 1;
H
Hongze Cheng 已提交
2364
    case (HAS_NULL | HAS_NONE):
H
Hongze Cheng 已提交
2365
      return GET_BIT1(pColData->pBitMap, iVal);
H
Hongze Cheng 已提交
2366
    case HAS_VALUE:
H
Hongze Cheng 已提交
2367
      return 2;
H
Hongze Cheng 已提交
2368
    case (HAS_VALUE | HAS_NONE):
H
Hongze Cheng 已提交
2369
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
H
Hongze Cheng 已提交
2370
    case (HAS_VALUE | HAS_NULL):
H
Hongze Cheng 已提交
2371
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
H
Hongze Cheng 已提交
2372
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
H
Hongze Cheng 已提交
2373
      return GET_BIT2(pColData->pBitMap, iVal);
H
Hongze Cheng 已提交
2374
    default:
H
Hongze Cheng 已提交
2375 2376
      ASSERTS(0, "not possible");
      return 0;
H
Hongze Cheng 已提交
2377 2378 2379
  }
}

H
Hongze Cheng 已提交
2380
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
H
refact  
Hongze Cheng 已提交
2381 2382
  int32_t code = 0;

H
Hongze Cheng 已提交
2383
  *pColData = *pColDataFrom;
H
refact  
Hongze Cheng 已提交
2384 2385

  // bitmap
H
Hongze Cheng 已提交
2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
  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 已提交
2408 2409 2410
  }

  // offset
H
Hongze Cheng 已提交
2411 2412 2413 2414 2415 2416 2417 2418 2419
  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 已提交
2420 2421 2422
  }

  // value
H
Hongze Cheng 已提交
2423 2424
  if (pColData->nData) {
    pColData->pData = xMalloc(arg, pColData->nData);
H
Hongze Cheng 已提交
2425 2426 2427 2428 2429 2430 2431 2432 2433
    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 已提交
2434 2435 2436

_exit:
  return code;
G
Ganlin Zhao 已提交
2437
}
H
Hongze Cheng 已提交
2438

M
Minglei Jin 已提交
2439 2440
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
                                    char *data) {
wmmhello's avatar
wmmhello 已提交
2441 2442 2443 2444
  int32_t code = 0;

  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
    for (int32_t i = 0; i < nRows; ++i) {
M
Minglei Jin 已提交
2445
      int32_t offset = *((int32_t *)lengthOrbitmap + i);
wmmhello's avatar
wmmhello 已提交
2446 2447 2448 2449
      if (offset == -1) {
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
        if (code) goto _exit;
      } else {
M
Minglei Jin 已提交
2450 2451
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
                                                                      varDataLen(data + offset));
wmmhello's avatar
wmmhello 已提交
2452 2453 2454 2455
      }
    }
  } else {  // fixed-length data type
    bool allValue = true;
M
Minglei Jin 已提交
2456
    bool allNull = true;
wmmhello's avatar
wmmhello 已提交
2457
    for (int32_t i = 0; i < nRows; ++i) {
M
Minglei Jin 已提交
2458
      if (!colDataIsNull_f(lengthOrbitmap, i)) {
wmmhello's avatar
wmmhello 已提交
2459
        allNull = false;
M
Minglei Jin 已提交
2460
      } else {
wmmhello's avatar
wmmhello 已提交
2461 2462 2463 2464 2465 2466 2467
        allValue = false;
      }
    }

    if (allValue) {
      // optimize (todo)
      for (int32_t i = 0; i < nRows; ++i) {
M
Minglei Jin 已提交
2468
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
wmmhello's avatar
wmmhello 已提交
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
      }
    } else if (allNull) {
      // optimize (todo)
      for (int32_t i = 0; i < nRows; ++i) {
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
        if (code) goto _exit;
      }
    } else {
      for (int32_t i = 0; i < nRows; ++i) {
        if (colDataIsNull_f(lengthOrbitmap, i)) {
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
          if (code) goto _exit;
        } else {
M
Minglei Jin 已提交
2482
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
wmmhello's avatar
wmmhello 已提交
2483 2484 2485 2486 2487
        }
      }
    }
  }

M
Minglei Jin 已提交
2488
_exit:
wmmhello's avatar
wmmhello 已提交
2489 2490 2491
  return code;
}

H
Hongze Cheng 已提交
2492 2493 2494
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind) {
  int32_t code = 0;

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

H
Hongze Cheng 已提交
2497
  if (IS_VAR_DATA_TYPE(pBind->buffer_type)) {  // var-length data type
H
Hongze Cheng 已提交
2498
    for (int32_t i = 0; i < pBind->num; ++i) {
H
Hongze Cheng 已提交
2499
      if (pBind->is_null && pBind->is_null[i]) {
H
Hongze Cheng 已提交
2500 2501
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
        if (code) goto _exit;
H
Hongze Cheng 已提交
2502
      } else {
H
Hongze Cheng 已提交
2503 2504
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
H
Hongze Cheng 已提交
2505 2506
      }
    }
H
Hongze Cheng 已提交
2507
  } else {  // fixed-length data type
H
Hongze Cheng 已提交
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
    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 已提交
2518 2519

    if (allValue) {
H
Hongze Cheng 已提交
2520 2521 2522 2523 2524
      // 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 已提交
2525
    } else if (allNull) {
H
Hongze Cheng 已提交
2526 2527 2528 2529 2530
      // 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 已提交
2531 2532 2533
    } else {
      for (int32_t i = 0; i < pBind->num; ++i) {
        if (pBind->is_null[i]) {
H
Hongze Cheng 已提交
2534 2535
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
          if (code) goto _exit;
H
Hongze Cheng 已提交
2536
        } else {
H
Hongze Cheng 已提交
2537 2538
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
H
Hongze Cheng 已提交
2539 2540 2541 2542 2543 2544 2545 2546 2547
        }
      }
    }
  }

_exit:
  return code;
}

H
Hongze Cheng 已提交
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
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);
2564 2565 2566
      // memmove(pColData->pData + pColData->aOffset[i] + nData2, pColData->pData + pColData->aOffset[i] + nData1,
      //         pColData->aOffset[j] - pColData->aOffset[i + 1]);
      memmove(pColData->pData + pColData->aOffset[i] + nData2, pColData->pData + pColData->aOffset[i + 1],
H
Hongze Cheng 已提交
2567 2568 2569 2570 2571
              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);
2572 2573 2574 2575
      // memmove(pColData->pData + pColData->aOffset[j] + nData2 - nData1, pColData->pData + pColData->aOffset[i] +
      // nData1,
      //         pColData->aOffset[j] - pColData->aOffset[i + 1]);
      memmove(pColData->pData + pColData->aOffset[i] + nData2, pColData->pData + pColData->aOffset[i + 1],
H
Hongze Cheng 已提交
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594
              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;
}
2595

H
Hongze Cheng 已提交
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629
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;
  }
}
M
Minglei Jin 已提交
2630

2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 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 2680 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 2712 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 2744 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 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
  int32_t code = TSDB_CODE_SUCCESS;

  if (IS_VAR_DATA_TYPE(pToColData->type)) {
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
    if (iToRow == 0) {
      pToColData->aOffset[iToRow] = 0;
    }

    if (iToRow < pToColData->nVal - 1) {
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
    }

    memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
           nData);
  } else {
    memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
           &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
  }
  return code;
}

static int32_t tColDataCopyRowSingleCol(SColData *pFromColData, int32_t iFromRow, SColData *pToColData,
                                        int32_t iToRow) {
  int32_t code = TSDB_CODE_SUCCESS;

  switch (pFromColData->flag) {
    case HAS_NONE:
    case HAS_NULL:
      break;
    case (HAS_NULL | HAS_NONE): {
      SET_BIT1(pToColData->pBitMap, iToRow, GET_BIT1(pFromColData->pBitMap, iFromRow));
    } break;
    case HAS_VALUE: {
      tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow);
    } break;
    case (HAS_VALUE | HAS_NONE):
    case (HAS_VALUE | HAS_NULL): {
      SET_BIT1(pToColData->pBitMap, iToRow, GET_BIT1(pFromColData->pBitMap, iFromRow));
      tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow);
    } break;
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
      tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow);
    } break;
    default:
      return -1;
  }

  return code;
}

static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
                               int32_t nColData) {
  int32_t code = TSDB_CODE_SUCCESS;

  for (int32_t i = 0; i < nColData; i++) {
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
  }

  return code;
}

static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
  int32_t code = TSDB_CODE_SUCCESS;

  for (int32_t i = 0; i < nColData; i++) {
    SColVal cv = {0};
    tColDataGetValue(&aFromColData[i], iFromRow, &cv);
    code = tColDataAppendValue(&aToColData[i], &cv);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
  }

  return code;
}

static int32_t tColDataMergeSortMerge(SColData *aColData, int32_t start, int32_t mid, int32_t end, int32_t nColData) {
  SColData *aDstColData = NULL;
  TSKEY    *aKey = (TSKEY *)aColData[0].pData;

  int32_t i = start, j = mid + 1, k = 0;

  if (end > start) {
    aDstColData = taosMemoryCalloc(1, sizeof(SColData) * nColData);
    for (int c = 0; c < nColData; ++c) {
      tColDataInit(&aDstColData[c], aColData[c].cid, aColData[c].type, aColData[c].smaOn);
    }
    if (aDstColData == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    /*
    for (int32_t i = 0; i < nColData; i++) {
      tColDataCopy(&aColData[i], &aDstColData[i], tColDataDefaultMalloc, NULL);
    }
    */
  }

  while (i <= mid && j <= end) {
    if (aKey[i] <= aKey[j]) {
      // tColDataCopyRow(aColData, i++, aDstColData, k++);
      tColDataCopyRowAppend(aColData, i++, aDstColData, nColData);
    } else {
      // tColDataCopyRow(aColData, j++, aDstColData, k++);
      tColDataCopyRowAppend(aColData, j++, aDstColData, nColData);
    }
  }

  while (i <= mid) {
    // tColDataCopyRow(aColData, i++, aDstColData, k++);
    tColDataCopyRowAppend(aColData, i++, aDstColData, nColData);
  }

  while (j <= end) {
    // tColDataCopyRow(aColData, j++, aDstColData, k++);
    tColDataCopyRowAppend(aColData, j++, aDstColData, nColData);
  }

  for (i = start, k = 0; i <= end; ++i, ++k) {
    tColDataCopyRow(aDstColData, k, aColData, i, nColData);
  }

  if (aDstColData) {
    for (int32_t i = 0; i < nColData; i++) {
      tColDataDestroy(&aDstColData[i]);
    }
    taosMemoryFree(aDstColData);
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t tColDataMergeSort(SColData *aColData, int32_t start, int32_t end, int32_t nColData) {
  int32_t ret = TSDB_CODE_SUCCESS;
  int32_t mid;

  if (start >= end) {
    return TSDB_CODE_SUCCESS;
  }

  mid = (start + end) / 2;

  ret = tColDataMergeSort(aColData, start, mid, nColData);
  if (ret != TSDB_CODE_SUCCESS) {
    return ret;
  }

  ret = tColDataMergeSort(aColData, mid + 1, end, nColData);
  if (ret != TSDB_CODE_SUCCESS) {
    return ret;
  }

  return tColDataMergeSortMerge(aColData, start, mid, end, nColData);
}

static int32_t tColDataSort(SColData *aColData, int32_t nColData) {
  int32_t nVal = aColData[0].nVal;

  if (nVal < 2) return TSDB_CODE_SUCCESS;

  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
H
Hongze Cheng 已提交
2798 2799 2800 2801 2802
}
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 已提交
2803
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2804
    } break;
H
Hongze Cheng 已提交
2805
    case (HAS_NULL | HAS_NONE): {
H
Hongze Cheng 已提交
2806
      if (GET_BIT1(pColData->pBitMap, iStart) == 0) {
H
Hongze Cheng 已提交
2807
        for (int32_t i = iStart + 1; i < iEnd; ++i) {
H
Hongze Cheng 已提交
2808 2809
          if (GET_BIT1(pColData->pBitMap, i) == 1) {
            SET_BIT1(pColData->pBitMap, iStart, 1);
H
Hongze Cheng 已提交
2810 2811 2812 2813 2814 2815 2816 2817
            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 已提交
2818
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835

      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 已提交
2836 2837 2838
      if (IS_VAR_DATA_TYPE(pColData->type)) {
        int32_t nDiff = pColData->aOffset[iEnd - 1] - pColData->aOffset[iStart];

H
Hongze Cheng 已提交
2839
        memmove(pColData->pData + pColData->aOffset[iStart], pColData->pData + pColData->aOffset[iEnd - 1],
H
Hongze Cheng 已提交
2840 2841 2842 2843 2844 2845 2846
                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 已提交
2847 2848
        memmove(pColData->pData + TYPE_BYTES[pColData->type] * iStart,
                pColData->pData + TYPE_BYTES[pColData->type] * (iEnd - 1),
H
Hongze Cheng 已提交
2849 2850 2851 2852
                TYPE_BYTES[pColData->type] * (pColData->nVal - iEnd + 1));
        pColData->nData -= (TYPE_BYTES[pColData->type] * (iEnd - iStart - 1));
      }

H
Hongze Cheng 已提交
2853
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2854
    } break;
H
Hongze Cheng 已提交
2855
    case (HAS_VALUE | HAS_NONE): {
H
Hongze Cheng 已提交
2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 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 2904 2905 2906 2907 2908 2909 2910 2911
      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 已提交
2912 2913
          memmove(pColData->pData + TYPE_BYTES[pColData->type] * (iStart + 1),
                  pColData->pData + TYPE_BYTES[pColData->type] * iEnd,
H
Hongze Cheng 已提交
2914 2915 2916 2917 2918 2919 2920 2921
                  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 已提交
2922
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2923
    } break;
H
Hongze Cheng 已提交
2924 2925 2926 2927
    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 已提交
2928
        memmove(pColData->pData + pColData->aOffset[iStart], pColData->pData + pColData->aOffset[iEnd - 1],
H
Hongze Cheng 已提交
2929 2930 2931 2932 2933 2934 2935
                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 已提交
2936 2937
        memmove(pColData->pData + TYPE_BYTES[pColData->type] * iStart,
                pColData->pData + TYPE_BYTES[pColData->type] * (iEnd - 1),
H
Hongze Cheng 已提交
2938 2939 2940 2941 2942 2943 2944 2945
                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 已提交
2946
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2947 2948 2949

      uint8_t flag = 0;
      for (int32_t i = 0; i < pColData->nVal; ++i) {
H
Hongze Cheng 已提交
2950
        if (GET_BIT1(pColData->pBitMap, i)) {
H
Hongze Cheng 已提交
2951 2952 2953 2954 2955 2956 2957 2958
          flag |= HAS_VALUE;
        } else {
          flag |= HAS_NULL;
        }

        if (flag == pColData->flag) break;
      }
      pColData->flag = flag;
H
Hongze Cheng 已提交
2959
    } break;
H
Hongze Cheng 已提交
2960
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
H
Hongze Cheng 已提交
2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
      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 已提交
2989
      pColData->nVal -= (iEnd - iStart - 1);
H
Hongze Cheng 已提交
2990 2991 2992 2993 2994 2995 2996 2997 2998
    } break;
    default:
      ASSERT(0);
      break;
  }
}
static void tColDataMerge(SColData *aColData, int32_t nColData) {
  int32_t iStart = 0;
  for (;;) {
H
Hongze Cheng 已提交
2999
    if (iStart >= aColData[0].nVal - 1) break;
H
Hongze Cheng 已提交
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016

    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 已提交
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
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 已提交
3044
    tColDataSort(aColData, nColData);
H
Hongze Cheng 已提交
3045 3046
  }

3047 3048 3049 3050 3051 3052 3053 3054 3055
  if (doMerge != 1) {
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
      if (aKey[iVal] == aKey[iVal - 1]) {
        doMerge = 1;
        break;
      }
    }
  }

H
Hongze Cheng 已提交
3056 3057
  // merge -------
  if (doMerge) {
H
Hongze Cheng 已提交
3058
    tColDataMerge(aColData, nColData);
H
Hongze Cheng 已提交
3059 3060 3061 3062
  }

_exit:
  return;
H
Hongze Cheng 已提交
3063 3064
}

H
Hongze Cheng 已提交
3065 3066 3067
int32_t tPutColData(uint8_t *pBuf, SColData *pColData) {
  int32_t n = 0;

H
Hongze Cheng 已提交
3068 3069 3070 3071
  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 已提交
3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094

  // 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 已提交
3095
      n += tPutI32v(pBuf ? pBuf + n : NULL, pColData->nData);
H
Hongze Cheng 已提交
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
      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 已提交
3141
      pColData->pData = pBuf + n;
H
Hongze Cheng 已提交
3142 3143 3144 3145 3146 3147
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
      n += pColData->nData;
    }
  }

  return n;
H
Hongze Cheng 已提交
3148 3149
}

H
Hongze Cheng 已提交
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174
#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 已提交
3175
          (*numOfNull)++;
H
Hongze Cheng 已提交
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
          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 已提交
3207
          (*numOfNull)++;
H
Hongze Cheng 已提交
3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238
          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 已提交
3239
          (*numOfNull)++;
H
Hongze Cheng 已提交
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270
          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 已提交
3271
          (*numOfNull)++;
H
Hongze Cheng 已提交
3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302
          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 已提交
3303
          (*numOfNull)++;
H
Hongze Cheng 已提交
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334
          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 已提交
3335
          (*numOfNull)++;
H
Hongze Cheng 已提交
3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366
          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 已提交
3367
          (*numOfNull)++;
H
Hongze Cheng 已提交
3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398
          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 已提交
3399
          (*numOfNull)++;
H
Hongze Cheng 已提交
3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
          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 已提交
3431
          (*numOfNull)++;
H
Hongze Cheng 已提交
3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462
          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 已提交
3463
          (*numOfNull)++;
H
Hongze Cheng 已提交
3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494
          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 已提交
3495
          (*numOfNull)++;
H
Hongze Cheng 已提交
3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530
          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
};