tdataformat.c 43.3 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

C
Cary Xu 已提交
23 24
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson);

H
Hongze Cheng 已提交
25 26 27
#pragma pack(push, 1)
typedef struct {
  int16_t nCols;
H
more  
Hongze Cheng 已提交
28
  uint8_t idx[];
H
Hongze Cheng 已提交
29 30 31
} STSKVRow;
#pragma pack(pop)

H
Hongze Cheng 已提交
32 33
#define TSROW_IS_KV_ROW(r) ((r)->flags & TSROW_KV_ROW)

H
Hongze Cheng 已提交
34
// SValue
H
Hongze Cheng 已提交
35
int32_t tPutValue(uint8_t *p, SValue *pValue, int8_t type) {
H
Hongze Cheng 已提交
36
  if (IS_VAR_DATA_TYPE(type)) {
H
Hongze Cheng 已提交
37
    return tPutBinary(p, pValue->pData, pValue->nData);
H
Hongze Cheng 已提交
38
  } else {
H
Hongze Cheng 已提交
39 40
    if (p) memcpy(p, &pValue->val, tDataTypes[type].bytes);
    return tDataTypes[type].bytes;
H
Hongze Cheng 已提交
41 42 43
  }
}

H
Hongze Cheng 已提交
44
int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type) {
H
Hongze Cheng 已提交
45
  if (IS_VAR_DATA_TYPE(type)) {
H
Hongze Cheng 已提交
46
    return tGetBinary(p, &pValue->pData, pValue ? &pValue->nData : NULL);
H
Hongze Cheng 已提交
47
  } else {
H
Hongze Cheng 已提交
48 49
    memcpy(&pValue->val, p, tDataTypes[type].bytes);
    return tDataTypes[type].bytes;
H
Hongze Cheng 已提交
50 51 52
  }
}

H
more  
Hongze Cheng 已提交
53 54 55 56 57
int tValueCmprFn(const SValue *pValue1, const SValue *pValue2, int8_t type) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
58
// STSRow2 ========================================================================
H
Hongze Cheng 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static void setBitMap(uint8_t *pb, uint8_t v, int32_t idx, uint8_t flags) {
  if (pb) {
    switch (flags & 0xf) {
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
      case TSROW_HAS_VAL | TSROW_HAS_NONE:
        if (v) {
          SET_BIT1(pb, idx, (uint8_t)1);
        } else {
          SET_BIT1(pb, idx, (uint8_t)0);
        }
        break;
      case TSROW_HAS_VAL | TSROW_HAS_NULL:
        v = v - 1;
        SET_BIT1(pb, idx, v);
        break;
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
        SET_BIT2(pb, idx, v);
        break;
H
more  
Hongze Cheng 已提交
77

H
Hongze Cheng 已提交
78 79
      default:
        ASSERT(0);
H
more  
Hongze Cheng 已提交
80 81
    }
  }
H
Hongze Cheng 已提交
82 83 84 85 86 87 88 89 90 91 92
}
#define SET_IDX(p, i, n, f)        \
  do {                             \
    if ((f)&TSROW_KV_SMALL) {      \
      ((uint8_t *)(p))[i] = (n);   \
    } else if ((f)&TSROW_KV_MID) { \
      ((uint16_t *)(p))[i] = (n);  \
    } else {                       \
      ((uint32_t *)(p))[i] = (n);  \
    }                              \
  } while (0)
H
more  
Hongze Cheng 已提交
93

H
Hongze Cheng 已提交
94
int32_t tTSRowNew(STSRowBuilder *pBuilder, SArray *pArray, STSchema *pTSchema, STSRow2 **ppRow) {
H
Hongze Cheng 已提交
95 96
  int32_t code = 0;
#if 0
H
Hongze Cheng 已提交
97 98 99 100
  STColumn *pTColumn;
  SColVal  *pColVal;
  int32_t   nColVal = taosArrayGetSize(pArray);
  int32_t   iColVal;
H
more  
Hongze Cheng 已提交
101

H
Hongze Cheng 已提交
102
  ASSERT(nColVal > 0);
H
more  
Hongze Cheng 已提交
103

H
Hongze Cheng 已提交
104 105 106 107 108 109
  // try
  uint8_t  flags = 0;
  uint32_t ntv = 0;
  uint32_t nkv = 0;
  int16_t  nTag = 0;
  uint32_t maxIdx = 0;
H
more  
Hongze Cheng 已提交
110

H
Hongze Cheng 已提交
111 112
  iColVal = 0;
  for (int32_t iColumn = 0; iColumn < pTSchema->numOfCols; iColumn++) {
H
more  
Hongze Cheng 已提交
113 114 115 116 117 118 119
    pTColumn = &pTSchema->columns[iColumn];
    if (iColVal < nColVal) {
      pColVal = (SColVal *)taosArrayGet(pArray, iColVal);
    } else {
      pColVal = NULL;
    }

H
Hongze Cheng 已提交
120 121 122
    if (iColumn == 0) {
      ASSERT(pColVal->cid == pTColumn->colId);
      ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);
C
Cary Xu 已提交
123
      ASSERT(pTColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID);
H
Hongze Cheng 已提交
124 125 126 127 128 129 130

      iColVal++;
    } else {
      if (pColVal) {
        if (pColVal->cid == pTColumn->colId) {
          iColVal++;

H
Hongze Cheng 已提交
131
          if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
132
            flags |= TSROW_HAS_NONE;
H
Hongze Cheng 已提交
133
          } else if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
            flags |= TSROW_HAS_NULL;
            maxIdx = nkv;
            nTag++;
            nkv += tPutI16v(NULL, -pTColumn->colId);
          } else {
            flags |= TSROW_HAS_VAL;
            maxIdx = nkv;
            nTag++;
            nkv += tPutI16v(NULL, pTColumn->colId);
            nkv += tPutValue(NULL, &pColVal->value, pTColumn->type);
            if (IS_VAR_DATA_TYPE(pTColumn->type)) {
              ntv += tPutValue(NULL, &pColVal->value, pTColumn->type);
            }
          }
        } else if (pColVal->cid > pTColumn->colId) {
          flags |= TSROW_HAS_NONE;
H
more  
Hongze Cheng 已提交
150
        } else {
H
Hongze Cheng 已提交
151
          ASSERT(0);
H
more  
Hongze Cheng 已提交
152 153
        }
      } else {
H
Hongze Cheng 已提交
154
        flags |= TSROW_HAS_NONE;
H
more  
Hongze Cheng 已提交
155 156
      }
    }
H
Hongze Cheng 已提交
157
  }
H
more  
Hongze Cheng 已提交
158

C
Cary Xu 已提交
159
  ASSERT(flags);
H
more  
Hongze Cheng 已提交
160

H
Hongze Cheng 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  // decide
  uint32_t nData = 0;
  uint32_t nDataT = 0;
  uint32_t nDataK = 0;
  if (flags == TSROW_HAS_NONE || flags == TSROW_HAS_NULL) {
    nData = 0;
  } else {
    switch (flags) {
      case TSROW_HAS_VAL:
        nDataT = pTSchema->flen + ntv;
        break;
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
        nDataT = BIT1_SIZE(pTSchema->numOfCols - 1);
        break;
      case TSROW_HAS_VAL | TSROW_HAS_NONE:
      case TSROW_HAS_VAL | TSROW_HAS_NULL:
        nDataT = BIT1_SIZE(pTSchema->numOfCols - 1) + pTSchema->flen + ntv;
        break;
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
        nDataT = BIT2_SIZE(pTSchema->numOfCols - 1) + pTSchema->flen + ntv;
        break;
      default:
C
Cary Xu 已提交
183 184
        break;
        ASSERT(0);
H
more  
Hongze Cheng 已提交
185 186
    }

H
Hongze Cheng 已提交
187 188 189 190 191 192 193 194 195 196
    uint8_t tflags = 0;
    if (maxIdx <= UINT8_MAX) {
      nDataK = sizeof(STSKVRow) + sizeof(uint8_t) * nTag + nkv;
      tflags |= TSROW_KV_SMALL;
    } else if (maxIdx <= UINT16_MAX) {
      nDataK = sizeof(STSKVRow) + sizeof(uint16_t) * nTag + nkv;
      tflags |= TSROW_KV_MID;
    } else {
      nDataK = sizeof(STSKVRow) + sizeof(uint32_t) * nTag + nkv;
      tflags |= TSROW_KV_BIG;
H
more  
Hongze Cheng 已提交
197 198
    }

C
Cary Xu 已提交
199
    if (nDataT <= nDataK) {
H
Hongze Cheng 已提交
200
      nData = nDataT;
H
more  
Hongze Cheng 已提交
201
    } else {
H
Hongze Cheng 已提交
202 203
      nData = nDataK;
      flags |= tflags;
H
more  
Hongze Cheng 已提交
204 205
    }
  }
H
more  
Hongze Cheng 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

  // alloc
  if (pBuilder) {
    // create from a builder
    if (nData == 0) {
      pBuilder->tsRow.nData = 0;
      pBuilder->tsRow.pData = NULL;
    } else {
      if (pBuilder->szBuf < nData) {
        uint8_t *p = taosMemoryRealloc(pBuilder->pBuf, nData);
        if (p == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _exit;
        }
        pBuilder->pBuf = p;
        pBuilder->szBuf = nData;
      }

H
Hongze Cheng 已提交
224
      pBuilder->tsRow.nData = nData;
H
more  
Hongze Cheng 已提交
225 226 227 228 229 230 231 232
      pBuilder->tsRow.pData = pBuilder->pBuf;
    }

    *ppRow = &pBuilder->tsRow;
  } else {
    // create a new one
    *ppRow = (STSRow2 *)taosMemoryMalloc(sizeof(STSRow2));
    if (*ppRow == NULL) {
H
more  
Hongze Cheng 已提交
233 234 235
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
236 237 238 239 240
    if (nData == 0) {
      (*ppRow)->nData = 0;
      (*ppRow)->pData = NULL;
    } else {
      (*ppRow)->nData = nData;
H
more  
Hongze Cheng 已提交
241 242
      (*ppRow)->pData = taosMemoryMalloc(nData);
      if ((*ppRow)->pData == NULL) {
H
Hongze Cheng 已提交
243
        taosMemoryFree(*ppRow);
H
more  
Hongze Cheng 已提交
244 245 246 247
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _exit;
      }
    }
H
more  
Hongze Cheng 已提交
248
  }
H
more  
Hongze Cheng 已提交
249

H
Hongze Cheng 已提交
250 251 252
  // build
  (*ppRow)->flags = flags;
  (*ppRow)->sver = pTSchema->version;
H
more  
Hongze Cheng 已提交
253

H
Hongze Cheng 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267
  pColVal = (SColVal *)taosArrayGet(pArray, 0);
  (*ppRow)->ts = pColVal->value.ts;

  if ((*ppRow)->pData) {
    STSKVRow *pTSKVRow = NULL;
    uint8_t  *pidx = NULL;
    uint8_t  *pkv = NULL;
    uint8_t  *pb = NULL;
    uint8_t  *pf = NULL;
    uint8_t  *ptv = NULL;
    nkv = 0;
    ntv = 0;
    iColVal = 1;

C
Cary Xu 已提交
268
    if ((flags & 0xf0) == 0) {
H
Hongze Cheng 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
      switch (flags & 0xf) {
        case TSROW_HAS_VAL:
          pf = (*ppRow)->pData;
          ptv = pf + pTSchema->flen;
          break;
        case TSROW_HAS_NULL | TSROW_HAS_NONE:
          pb = (*ppRow)->pData;
          break;
        case TSROW_HAS_VAL | TSROW_HAS_NONE:
        case TSROW_HAS_VAL | TSROW_HAS_NULL:
          pb = (*ppRow)->pData;
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          ptv = pf + pTSchema->flen;
          break;
        case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
          pb = (*ppRow)->pData;
          pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
          ptv = pf + pTSchema->flen;
          break;
        default:
C
Cary Xu 已提交
289
          ASSERT(0);
C
Cary Xu 已提交
290
          break;
H
Hongze Cheng 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
      }
    } else {
      pTSKVRow = (STSKVRow *)(*ppRow)->pData;
      pTSKVRow->nCols = 0;
      pidx = pTSKVRow->idx;
      if (flags & TSROW_KV_SMALL) {
        pkv = pidx + sizeof(uint8_t) * nTag;
      } else if (flags & TSROW_KV_MID) {
        pkv = pidx + sizeof(uint16_t) * nTag;
      } else {
        pkv = pidx + sizeof(uint32_t) * nTag;
      }
    }

    for (int32_t iColumn = 1; iColumn < pTSchema->numOfCols; iColumn++) {
      pTColumn = &pTSchema->columns[iColumn];
      if (iColVal < nColVal) {
        pColVal = (SColVal *)taosArrayGet(pArray, iColVal);
      } else {
        pColVal = NULL;
      }

      if (pColVal) {
        if (pColVal->cid == pTColumn->colId) {
          iColVal++;

H
Hongze Cheng 已提交
317
          if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
318
            goto _set_none;
H
Hongze Cheng 已提交
319
          } else if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333
            goto _set_null;
          } else {
            goto _set_value;
          }
        } else if (pColVal->cid > pTColumn->colId) {
          goto _set_none;
        } else {
          ASSERT(0);
        }
      } else {
        goto _set_none;
      }

    _set_none:
C
Cary Xu 已提交
334
      if ((flags & 0xf0) == 0) {
H
Hongze Cheng 已提交
335
        setBitMap(pb, 0, iColumn - 1, flags);
H
Hongze Cheng 已提交
336
        if (flags & TSROW_HAS_VAL) {  // set 0
C
Cary Xu 已提交
337 338 339 340 341 342
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
            *(VarDataOffsetT *)(pf + pTColumn->offset) = 0;
          } else {
            tPutValue(pf + pTColumn->offset, &((SValue){0}), pTColumn->type);
          }
        }
H
Hongze Cheng 已提交
343 344 345 346
      }
      continue;

    _set_null:
C
Cary Xu 已提交
347
      if ((flags & 0xf0) == 0) {
H
Hongze Cheng 已提交
348
        setBitMap(pb, 1, iColumn - 1, flags);
H
Hongze Cheng 已提交
349
        if (flags & TSROW_HAS_VAL) {  // set 0
C
Cary Xu 已提交
350 351 352 353 354 355
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
            *(VarDataOffsetT *)(pf + pTColumn->offset) = 0;
          } else {
            tPutValue(pf + pTColumn->offset, &((SValue){0}), pTColumn->type);
          }
        }
H
Hongze Cheng 已提交
356 357 358 359 360 361
      } else {
        SET_IDX(pidx, pTSKVRow->nCols, nkv, flags);
        pTSKVRow->nCols++;
        nkv += tPutI16v(pkv + nkv, -pTColumn->colId);
      }
      continue;
H
more  
Hongze Cheng 已提交
362

H
Hongze Cheng 已提交
363
    _set_value:
C
Cary Xu 已提交
364
      if ((flags & 0xf0) == 0) {
H
Hongze Cheng 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
        setBitMap(pb, 2, iColumn - 1, flags);

        if (IS_VAR_DATA_TYPE(pTColumn->type)) {
          *(VarDataOffsetT *)(pf + pTColumn->offset) = ntv;
          ntv += tPutValue(ptv + ntv, &pColVal->value, pTColumn->type);
        } else {
          tPutValue(pf + pTColumn->offset, &pColVal->value, pTColumn->type);
        }
      } else {
        SET_IDX(pidx, pTSKVRow->nCols, nkv, flags);
        pTSKVRow->nCols++;
        nkv += tPutI16v(pkv + nkv, pColVal->cid);
        nkv += tPutValue(pkv + nkv, &pColVal->value, pTColumn->type);
      }
      continue;
    }
H
more  
Hongze Cheng 已提交
381 382
  }

H
Hongze Cheng 已提交
383
#endif
H
more  
Hongze Cheng 已提交
384
_exit:
H
Hongze Cheng 已提交
385 386 387
  return code;
}

H
Hongze Cheng 已提交
388 389
int32_t tTSRowClone(const STSRow2 *pRow, STSRow2 **ppRow) {
  int32_t code = 0;
H
Hongze Cheng 已提交
390
  int32_t rLen;
H
Hongze Cheng 已提交
391

H
Hongze Cheng 已提交
392 393
  TSROW_LEN(pRow, rLen);
  (*ppRow) = (STSRow2 *)taosMemoryMalloc(rLen);
H
Hongze Cheng 已提交
394
  if (*ppRow == NULL) {
H
Hongze Cheng 已提交
395 396
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
397
  }
H
Hongze Cheng 已提交
398
  memcpy(*ppRow, pRow, rLen);
H
Hongze Cheng 已提交
399

H
Hongze Cheng 已提交
400 401
_exit:
  return code;
H
Hongze Cheng 已提交
402 403 404
}

void tTSRowFree(STSRow2 *pRow) {
H
Hongze Cheng 已提交
405 406 407
  if (pRow) {
    taosMemoryFree(pRow);
  }
H
Hongze Cheng 已提交
408 409
}

H
Hongze Cheng 已提交
410
void tTSRowGet(STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
411
#if 0
C
Cary Xu 已提交
412
  uint8_t   isTuple = ((pRow->flags & 0xf0) == 0) ? 1 : 0;
H
Hongze Cheng 已提交
413
  STColumn *pTColumn = &pTSchema->columns[iCol];
H
Hongze Cheng 已提交
414 415
  uint8_t   flags = pRow->flags & (uint8_t)0xf;
  SValue    value;
H
Hongze Cheng 已提交
416

H
Hongze Cheng 已提交
417
  ASSERT(iCol < pTSchema->numOfCols);
C
Cary Xu 已提交
418
  ASSERT(flags);
H
Hongze Cheng 已提交
419
  ASSERT(pRow->sver == pTSchema->version);
H
Hongze Cheng 已提交
420

H
Hongze Cheng 已提交
421 422 423
  if (iCol == 0) {
    value.ts = pRow->ts;
    goto _return_value;
H
Hongze Cheng 已提交
424 425
  }

H
Hongze Cheng 已提交
426
  if (flags == TSROW_HAS_NONE) {
H
more  
Hongze Cheng 已提交
427
    goto _return_none;
C
Cary Xu 已提交
428
  } else if (flags == TSROW_HAS_NULL) {
H
more  
Hongze Cheng 已提交
429
    goto _return_null;
H
Hongze Cheng 已提交
430
  }
H
Hongze Cheng 已提交
431

H
Hongze Cheng 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445
  ASSERT(pRow->nData && pRow->pData);

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

    // bit
    switch (flags) {
      case TSROW_HAS_VAL:
        pf = pb;
        break;
H
Hongze Cheng 已提交
446
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
447 448 449
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
H
Hongze Cheng 已提交
450
        } else {
H
Hongze Cheng 已提交
451
          goto _return_null;
H
Hongze Cheng 已提交
452
        }
H
Hongze Cheng 已提交
453
      case TSROW_HAS_VAL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
454 455 456
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
H
Hongze Cheng 已提交
457
        } else {
H
Hongze Cheng 已提交
458 459
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
460
        }
H
Hongze Cheng 已提交
461
      case TSROW_HAS_VAL | TSROW_HAS_NULL:
H
Hongze Cheng 已提交
462 463 464
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_null;
H
Hongze Cheng 已提交
465
        } else {
H
Hongze Cheng 已提交
466 467
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
468
        }
H
Hongze Cheng 已提交
469
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
470 471 472 473 474
        b = GET_BIT2(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
        } else if (b == 1) {
          goto _return_null;
H
Hongze Cheng 已提交
475
        } else {
H
Hongze Cheng 已提交
476 477
          pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
478
        }
H
Hongze Cheng 已提交
479
      default:
H
Hongze Cheng 已提交
480
        ASSERT(0);
H
Hongze Cheng 已提交
481
    }
H
Hongze Cheng 已提交
482

H
Hongze Cheng 已提交
483 484 485
    ASSERT(pf);

    p = pf + pTColumn->offset;
H
Hongze Cheng 已提交
486
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
H
Hongze Cheng 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
      pv = pf + pTSchema->flen;
      p = pv + *(VarDataOffsetT *)p;
    }
    tGetValue(p, &value, pTColumn->type);
    goto _return_value;
  } else {
    STSKVRow *pRowK = (STSKVRow *)pRow->pData;
    int16_t   lidx = 0;
    int16_t   ridx = pRowK->nCols - 1;
    uint8_t  *p;
    int16_t   midx;
    uint32_t  n;
    int16_t   cid;

    ASSERT(pRowK->nCols > 0);

    if (pRow->flags & TSROW_KV_SMALL) {
      p = pRow->pData + sizeof(STSKVRow) + sizeof(uint8_t) * pRowK->nCols;
    } else if (pRow->flags & TSROW_KV_MID) {
      p = pRow->pData + sizeof(STSKVRow) + sizeof(uint16_t) * pRowK->nCols;
    } else if (pRow->flags & TSROW_KV_BIG) {
      p = pRow->pData + sizeof(STSKVRow) + sizeof(uint32_t) * pRowK->nCols;
H
Hongze Cheng 已提交
509
    } else {
H
Hongze Cheng 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
      ASSERT(0);
    }
    while (lidx <= ridx) {
      midx = (lidx + ridx) / 2;

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

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

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

        return;
      } else if (TABS(cid) > pTColumn->colId) {
        ridx = midx - 1;
      } else {
        lidx = midx + 1;
      }
H
Hongze Cheng 已提交
539
    }
H
Hongze Cheng 已提交
540 541 542

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

H
Hongze Cheng 已提交
545
_return_none:
H
Hongze Cheng 已提交
546
  *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
547 548 549
  return;

_return_null:
H
Hongze Cheng 已提交
550
  *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
H
Hongze Cheng 已提交
551 552 553
  return;

_return_value:
H
Hongze Cheng 已提交
554
  *pColVal = COL_VAL_VALUE(pTColumn->colId, pTColumn->type, value);
H
Hongze Cheng 已提交
555
  return;
H
Hongze Cheng 已提交
556
#endif
H
Hongze Cheng 已提交
557 558 559 560
}

int32_t tTSRowToArray(STSRow2 *pRow, STSchema *pTSchema, SArray **ppArray) {
  int32_t code = 0;
H
Hongze Cheng 已提交
561
#if 0
H
Hongze Cheng 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574
  SColVal cv;

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

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

H
Hongze Cheng 已提交
575
#endif
H
Hongze Cheng 已提交
576 577 578 579 580
_exit:
  return code;
}

int32_t tPutTSRow(uint8_t *p, STSRow2 *pRow) {
H
Hongze Cheng 已提交
581
  int32_t n;
H
Hongze Cheng 已提交
582

H
Hongze Cheng 已提交
583 584 585
  TSROW_LEN(pRow, n);
  if (p) {
    memcpy(p, pRow, n);
H
Hongze Cheng 已提交
586 587 588 589 590
  }

  return n;
}

H
Hongze Cheng 已提交
591 592
int32_t tGetTSRow(uint8_t *p, STSRow2 **ppRow) {
  int32_t n;
H
Hongze Cheng 已提交
593

H
Hongze Cheng 已提交
594 595
  *ppRow = (STSRow2 *)p;
  TSROW_LEN(*ppRow, n);
H
Hongze Cheng 已提交
596 597

  return n;
H
Hongze Cheng 已提交
598 599
}

H
Hongze Cheng 已提交
600
// STSchema ========================================
H
Hongze Cheng 已提交
601 602 603 604 605 606 607
int32_t tTSchemaCreate(int32_t sver, SSchema *pSchema, int32_t ncols, STSchema **ppTSchema) {
  *ppTSchema = (STSchema *)taosMemoryMalloc(sizeof(STSchema) + sizeof(STColumn) * ncols);
  if (*ppTSchema == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

H
Hongze Cheng 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
  (*ppTSchema)->numOfCols = ncols;
  (*ppTSchema)->version = sver;
  (*ppTSchema)->flen = 0;
  (*ppTSchema)->vlen = 0;
  (*ppTSchema)->tlen = 0;

  for (int32_t iCol = 0; iCol < ncols; iCol++) {
    SSchema  *pColumn = &pSchema[iCol];
    STColumn *pTColumn = &((*ppTSchema)->columns[iCol]);

    pTColumn->colId = pColumn->colId;
    pTColumn->type = pColumn->type;
    pTColumn->flags = pColumn->flags;
    pTColumn->bytes = pColumn->bytes;
    pTColumn->offset = (*ppTSchema)->flen;

    // skip first column
    if (iCol) {
      (*ppTSchema)->flen += TYPE_BYTES[pColumn->type];
      if (IS_VAR_DATA_TYPE(pColumn->type)) {
        (*ppTSchema)->vlen += (pColumn->bytes + 5);
      }
    }
  }

H
Hongze Cheng 已提交
633 634 635
  return 0;
}

H
Hongze Cheng 已提交
636 637 638
void tTSchemaDestroy(STSchema *pTSchema) {
  if (pTSchema) taosMemoryFree(pTSchema);
}
H
Hongze Cheng 已提交
639

H
Hongze Cheng 已提交
640
// STag ========================================
H
Hongze Cheng 已提交
641 642
static int tTagValCmprFn(const void *p1, const void *p2) {
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
643
    return -1;
H
Hongze Cheng 已提交
644
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
645 646
    return 1;
  }
H
Hongze Cheng 已提交
647

H
Hongze Cheng 已提交
648 649
  return 0;
}
H
Hongze Cheng 已提交
650 651 652
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
}
C
Cary Xu 已提交
653 654 655 656 657 658 659

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};
660
      strncpy(tmpVal, val, vlen > 31 ? 31 : vlen);
C
Cary Xu 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
      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 已提交
699 700 701
    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 已提交
702 703 704 705 706 707 708
    default:
      ASSERT(0);
      break;
  }
}

void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
H
Hongze Cheng 已提交
709 710
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
711 712 713 714 715 716 717 718 719 720
  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 已提交
721
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
722 723 724 725 726 727 728 729
    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 已提交
730
    } else {
731
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
C
Cary Xu 已提交
732
    }
733
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
C
Cary Xu 已提交
734
    tGetTagVal(p + offset, &tagVal, isJson);
735
    if (IS_VAR_DATA_TYPE(tagVal.type)) {
wmmhello's avatar
wmmhello 已提交
736
      debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
737
    } else {
wmmhello's avatar
wmmhello 已提交
738 739
      debugPrintTagVal(tagVal.type, &tagVal.i64, tDataTypes[tagVal.type].bytes, __func__, __LINE__);
    }
C
Cary Xu 已提交
740 741 742 743
  }
  printf("\n");
}

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

H
Hongze Cheng 已提交
747 748 749 750 751 752
  // key
  if (isJson) {
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
  } else {
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
  }
H
Hongze Cheng 已提交
753

H
Hongze Cheng 已提交
754 755 756 757 758 759 760
  // 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 已提交
761
    p = p ? p + n : p;
wmmhello's avatar
wmmhello 已提交
762
    n += tDataTypes[pTagVal->type].bytes;
763
    if (p) memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
H
Hongze Cheng 已提交
764 765
  }

H
Hongze Cheng 已提交
766 767 768 769 770 771 772 773 774 775
  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 已提交
776 777
  }

H
Hongze Cheng 已提交
778 779
  // type
  n += tGetI8(p + n, &pTagVal->type);
H
Hongze Cheng 已提交
780

H
Hongze Cheng 已提交
781 782 783 784
  // value
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
  } else {
wmmhello's avatar
wmmhello 已提交
785
    memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
wmmhello's avatar
wmmhello 已提交
786
    n += tDataTypes[pTagVal->type].bytes;
H
Hongze Cheng 已提交
787 788 789 790
  }

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

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

H
Hongze Cheng 已提交
794 795 796 797 798
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 已提交
799 800
}

H
Hongze Cheng 已提交
801
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
H
Hongze Cheng 已提交
802 803 804
  int32_t  code = 0;
  uint8_t *p = NULL;
  int16_t  n = 0;
H
Hongze Cheng 已提交
805
  int16_t  nTag = taosArrayGetSize(pArray);
H
Hongze Cheng 已提交
806 807
  int32_t  szTag = 0;
  int8_t   isLarge = 0;
H
Hongze Cheng 已提交
808 809 810

  // sort
  if (isJson) {
wafwerar's avatar
wafwerar 已提交
811
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
H
Hongze Cheng 已提交
812
  } else {
wafwerar's avatar
wafwerar 已提交
813
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
H
Hongze Cheng 已提交
814 815 816
  }

  // get size
H
Hongze Cheng 已提交
817
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
818
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
819
  }
H
Hongze Cheng 已提交
820 821 822 823 824 825
  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 已提交
826

H
Hongze Cheng 已提交
827
  ASSERT(szTag <= INT16_MAX);
H
Hongze Cheng 已提交
828 829

  // build tag
C
Cary Xu 已提交
830
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
H
Hongze Cheng 已提交
831 832 833 834
  if ((*ppTag) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
835 836 837 838 839 840 841
  (*ppTag)->flags = 0;
  if (isJson) {
    (*ppTag)->flags |= TD_TAG_JSON;
  }
  if (isLarge) {
    (*ppTag)->flags |= TD_TAG_LARGE;
  }
H
Hongze Cheng 已提交
842 843 844
  (*ppTag)->len = szTag;
  (*ppTag)->nTag = nTag;
  (*ppTag)->ver = version;
H
Hongze Cheng 已提交
845

H
Hongze Cheng 已提交
846 847 848 849 850
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
  } else {
    p = (uint8_t *)&(*ppTag)->idx[nTag];
  }
H
Hongze Cheng 已提交
851 852
  n = 0;
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
853 854 855 856 857
    if (isLarge) {
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
    } else {
      (*ppTag)->idx[iTag] = n;
    }
H
Hongze Cheng 已提交
858
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
859
  }
860
#ifdef TD_DEBUG_PRINT_TAG
C
Cary Xu 已提交
861
  debugPrintSTag(*ppTag, __func__, __LINE__);
862
#endif
C
Cary Xu 已提交
863

H
Hongze Cheng 已提交
864 865 866 867
  return code;

_err:
  return code;
H
Hongze Cheng 已提交
868 869 870 871 872 873
}

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

874 875 876
char *tTagValToData(const STagVal *value, bool isJson) {
  if (!value) return NULL;
  char  *data = NULL;
wmmhello's avatar
wmmhello 已提交
877 878 879 880
  int8_t typeBytes = 0;
  if (isJson) {
    typeBytes = CHAR_BYTES;
  }
881
  if (IS_VAR_DATA_TYPE(value->type)) {
wmmhello's avatar
wmmhello 已提交
882
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
883 884
    if (data == NULL) return NULL;
    if (isJson) *data = value->type;
wmmhello's avatar
wmmhello 已提交
885 886
    varDataLen(data + typeBytes) = value->nData;
    memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
887 888
  } else {
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
wmmhello's avatar
wmmhello 已提交
889 890 891 892 893
  }

  return data;
}

C
Cary Xu 已提交
894
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
H
Hongze Cheng 已提交
895 896 897
  int16_t  lidx = 0;
  int16_t  ridx = pTag->nTag - 1;
  int16_t  midx;
H
Hongze Cheng 已提交
898 899 900 901
  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 已提交
902 903 904
  STagVal  tv;
  int      c;

H
Hongze Cheng 已提交
905 906 907 908 909 910
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }

H
Hongze Cheng 已提交
911 912 913 914 915
  pTagVal->type = TSDB_DATA_TYPE_NULL;
  pTagVal->pData = NULL;
  pTagVal->nData = 0;
  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;
H
Hongze Cheng 已提交
916 917 918 919 920
    if (isLarge) {
      offset = ((int16_t *)pTag->idx)[midx];
    } else {
      offset = pTag->idx[midx];
    }
H
Hongze Cheng 已提交
921

H
Hongze Cheng 已提交
922 923
    tGetTagVal(p + offset, &tv, isJson);
    if (isJson) {
H
Hongze Cheng 已提交
924
      c = tTagValJsonCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
925
    } else {
H
Hongze Cheng 已提交
926
      c = tTagValCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
927 928
    }

H
Hongze Cheng 已提交
929 930 931 932
    if (c < 0) {
      ridx = midx - 1;
    } else if (c > 0) {
      lidx = midx + 1;
H
Hongze Cheng 已提交
933
    } else {
H
Hongze Cheng 已提交
934
      memcpy(pTagVal, &tv, sizeof(tv));
C
Cary Xu 已提交
935
      return true;
H
Hongze Cheng 已提交
936 937
    }
  }
C
Cary Xu 已提交
938
  return false;
H
Hongze Cheng 已提交
939 940
}

H
more  
Hongze Cheng 已提交
941 942
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
H
Hongze Cheng 已提交
943 944
}

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

C
Cary Xu 已提交
947
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
H
Hongze Cheng 已提交
948
  int32_t  code = 0;
949 950 951 952 953 954 955 956 957 958
  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 已提交
959

H
more  
Hongze Cheng 已提交
960
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
H
Hongze Cheng 已提交
961
  if (*ppArray == NULL) {
H
Hongze Cheng 已提交
962 963 964 965 966
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
967 968 969 970 971 972
    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 已提交
973
    taosArrayPush(*ppArray, &tv);
H
Hongze Cheng 已提交
974 975 976 977 978 979 980 981
  }

  return code;

_err:
  return code;
}

wmmhello's avatar
wmmhello 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
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 已提交
1002
#if 1  // ===================================================================================================================
C
Cary Xu 已提交
1003
int tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
1004 1005 1006
  if (pBuilder == NULL) return -1;

  pBuilder->tCols = 256;
wafwerar's avatar
wafwerar 已提交
1007
  pBuilder->columns = (STColumn *)taosMemoryMalloc(sizeof(STColumn) * pBuilder->tCols);
H
Hongze Cheng 已提交
1008 1009 1010 1011 1012 1013 1014 1015
  if (pBuilder->columns == NULL) return -1;

  tdResetTSchemaBuilder(pBuilder, version);
  return 0;
}

void tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder) {
  if (pBuilder) {
wafwerar's avatar
wafwerar 已提交
1016
    taosMemoryFreeClear(pBuilder->columns);
H
Hongze Cheng 已提交
1017 1018 1019
  }
}

C
Cary Xu 已提交
1020
void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
1021 1022 1023
  pBuilder->nCols = 0;
  pBuilder->tlen = 0;
  pBuilder->flen = 0;
T
Tao Liu 已提交
1024
  pBuilder->vlen = 0;
H
Hongze Cheng 已提交
1025 1026 1027
  pBuilder->version = version;
}

C
Cary Xu 已提交
1028
int32_t tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes) {
1029
  if (!isValidDataType(type)) return -1;
H
Hongze Cheng 已提交
1030 1031 1032

  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
wafwerar's avatar
wafwerar 已提交
1033
    STColumn *columns = (STColumn *)taosMemoryRealloc(pBuilder->columns, sizeof(STColumn) * pBuilder->tCols);
T
tickduan 已提交
1034 1035
    if (columns == NULL) return -1;
    pBuilder->columns = columns;
H
Hongze Cheng 已提交
1036 1037 1038
  }

  STColumn *pCol = &(pBuilder->columns[pBuilder->nCols]);
H
Hongze Cheng 已提交
1039 1040 1041
  pCol->type = type;
  pCol->colId = colId;
  pCol->flags = flags;
H
Hongze Cheng 已提交
1042
  if (pBuilder->nCols == 0) {
H
Hongze Cheng 已提交
1043
    pCol->offset = 0;
H
Hongze Cheng 已提交
1044
  } else {
S
Shengliang Guan 已提交
1045
    STColumn *pTCol = &(pBuilder->columns[pBuilder->nCols - 1]);
H
Hongze Cheng 已提交
1046
    pCol->offset = pTCol->offset + TYPE_BYTES[pTCol->type];
H
Hongze Cheng 已提交
1047 1048 1049
  }

  if (IS_VAR_DATA_TYPE(type)) {
H
Hongze Cheng 已提交
1050
    pCol->bytes = bytes;
T
Tao Liu 已提交
1051 1052
    pBuilder->tlen += (TYPE_BYTES[type] + bytes);
    pBuilder->vlen += bytes - sizeof(VarDataLenT);
H
Hongze Cheng 已提交
1053
  } else {
H
Hongze Cheng 已提交
1054
    pCol->bytes = TYPE_BYTES[type];
H
Hongze Cheng 已提交
1055
    pBuilder->tlen += TYPE_BYTES[type];
T
Tao Liu 已提交
1056
    pBuilder->vlen += TYPE_BYTES[type];
H
Hongze Cheng 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  }

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

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

  return 0;
}

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

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

wafwerar's avatar
wafwerar 已提交
1072
  STSchema *pSchema = (STSchema *)taosMemoryMalloc(tlen);
H
Hongze Cheng 已提交
1073 1074
  if (pSchema == NULL) return NULL;

H
Hongze Cheng 已提交
1075 1076 1077 1078 1079
  pSchema->version = pBuilder->version;
  pSchema->numOfCols = pBuilder->nCols;
  pSchema->tlen = pBuilder->tlen;
  pSchema->flen = pBuilder->flen;
  pSchema->vlen = pBuilder->vlen;
H
Hongze Cheng 已提交
1080

C
Cary Xu 已提交
1081
#ifdef TD_SUPPORT_BITMAP
H
Hongze Cheng 已提交
1082
  pSchema->tlen += (int)TD_BITMAP_BYTES(pSchema->numOfCols);
C
Cary Xu 已提交
1083 1084
#endif

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

H
TD-27  
hzcheng 已提交
1087 1088 1089
  return pSchema;
}

H
refact  
Hongze Cheng 已提交
1090 1091 1092
#endif

// SColData ========================================
H
Hongze Cheng 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
void tColDataDestroy(void *ph) {
  SColData *pColData = (SColData *)ph;

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

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

H
Hongze Cheng 已提交
1108
void tColDataClear(SColData *pColData) {
H
Hongze Cheng 已提交
1109 1110 1111 1112 1113
  pColData->nVal = 0;
  pColData->flag = 0;
  pColData->nData = 0;
}

H
Hongze Cheng 已提交
1114 1115 1116 1117 1118 1119 1120 1121
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, SColVal *pColVal) {
  int32_t code = 0;

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

H
Hongze Cheng 已提交
1122
    if (pColVal->value.nData) {
H
Hongze Cheng 已提交
1123 1124 1125 1126 1127 1128
      code = tRealloc(&pColData->pData, pColData->nData + pColVal->value.nData);
      if (code) goto _exit;
      memcpy(pColData->pData + pColData->nData, pColVal->value.pData, pColVal->value.nData);
      pColData->nData += pColVal->value.nData;
    }
  } else {
H
Hongze Cheng 已提交
1129
    ASSERT(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal);
H
Hongze Cheng 已提交
1130 1131 1132 1133 1134 1135 1136 1137
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
    if (code) goto _exit;
    pColData->nData += tPutValue(pColData->pData + pColData->nData, &pColVal->value, pColVal->type);
  }

_exit:
  return code;
}
H
Hongze Cheng 已提交
1138
static FORCE_INLINE int32_t tColDataAppendValue0(SColData *pColData, SColVal *pColVal) {  // 0
H
Hongze Cheng 已提交
1139 1140
  int32_t code = 0;

H
Hongze Cheng 已提交
1141
  if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
1142
    pColData->flag = HAS_NONE;
H
Hongze Cheng 已提交
1143
  } else if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
    pColData->flag = HAS_NULL;
  } else {
    pColData->flag = HAS_VALUE;
    code = tColDataPutValue(pColData, pColVal);
    if (code) goto _exit;
  }
  pColData->nVal++;

_exit:
  return code;
}
static FORCE_INLINE int32_t tColDataAppendValue1(SColData *pColData, SColVal *pColVal) {  // HAS_NONE
  int32_t code = 0;

H
Hongze Cheng 已提交
1158
  if (!COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
1159 1160 1161
    int32_t nBit = BIT1_SIZE(pColData->nVal + 1);

    code = tRealloc(&pColData->pBitMap, nBit);
H
Hongze Cheng 已提交
1162 1163
    if (code) goto _exit;

H
Hongze Cheng 已提交
1164
    memset(pColData->pBitMap, 0, nBit);
H
Hongze Cheng 已提交
1165 1166
    SET_BIT1(pColData->pBitMap, pColData->nVal, 1);

H
Hongze Cheng 已提交
1167
    if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
1168 1169 1170
      pColData->flag |= HAS_NULL;
    } else {
      pColData->flag |= HAS_VALUE;
H
Hongze Cheng 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

      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) goto _exit;
          memset(pColData->aOffset, 0, nOffset);
        } else {
          pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
          code = tRealloc(&pColData->pData, pColData->nData);
          if (code) goto _exit;
          memset(pColData->pData, 0, pColData->nData);
        }
      }

H
Hongze Cheng 已提交
1186 1187 1188
      code = tColDataPutValue(pColData, pColVal);
      if (code) goto _exit;
    }
H
Hongze Cheng 已提交
1189 1190 1191
  }
  pColData->nVal++;

H
Hongze Cheng 已提交
1192
_exit:
H
Hongze Cheng 已提交
1193 1194 1195 1196 1197
  return code;
}
static FORCE_INLINE int32_t tColDataAppendValue2(SColData *pColData, SColVal *pColVal) {  // HAS_NULL
  int32_t code = 0;

H
Hongze Cheng 已提交
1198
  if (!COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
1199 1200
    int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
    code = tRealloc(&pColData->pBitMap, nBit);
H
Hongze Cheng 已提交
1201 1202
    if (code) goto _exit;

H
Hongze Cheng 已提交
1203
    if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
1204 1205
      pColData->flag |= HAS_NONE;

H
Hongze Cheng 已提交
1206
      memset(pColData->pBitMap, 255, nBit);
H
Hongze Cheng 已提交
1207 1208 1209 1210
      SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
    } else {
      pColData->flag |= HAS_VALUE;

H
Hongze Cheng 已提交
1211
      memset(pColData->pBitMap, 0, nBit);
H
Hongze Cheng 已提交
1212 1213
      SET_BIT1(pColData->pBitMap, pColData->nVal, 1);

H
Hongze Cheng 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
      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) goto _exit;
          memset(pColData->aOffset, 0, nOffset);
        } else {
          pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
          code = tRealloc(&pColData->pData, pColData->nData);
          if (code) goto _exit;
          memset(pColData->pData, 0, pColData->nData);
        }
      }

H
Hongze Cheng 已提交
1228 1229 1230
      code = tColDataPutValue(pColData, pColVal);
      if (code) goto _exit;
    }
H
Hongze Cheng 已提交
1231 1232 1233
  }
  pColData->nVal++;

H
Hongze Cheng 已提交
1234
_exit:
H
Hongze Cheng 已提交
1235 1236
  return code;
}
H
Hongze Cheng 已提交
1237
static FORCE_INLINE int32_t tColDataAppendValue3(SColData *pColData, SColVal *pColVal) {  // HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
1238 1239
  int32_t code = 0;

H
Hongze Cheng 已提交
1240
  if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
1241 1242 1243 1244
    code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
    if (code) goto _exit;

    SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1245
  } else if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
1246 1247 1248 1249
    code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
    if (code) goto _exit;

    SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1250
  } else {
H
Hongze Cheng 已提交
1251 1252
    pColData->flag |= HAS_VALUE;

H
Hongze Cheng 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    uint8_t *pBitMap = NULL;
    code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
    if (code) goto _exit;

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

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

H
Hongze Cheng 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
    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) goto _exit;
        memset(pColData->aOffset, 0, nOffset);
      } else {
        pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
        code = tRealloc(&pColData->pData, pColData->nData);
        if (code) goto _exit;
        memset(pColData->pData, 0, pColData->nData);
      }
    }

H
Hongze Cheng 已提交
1279 1280
    code = tColDataPutValue(pColData, pColVal);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1281 1282 1283
  }
  pColData->nVal++;

H
Hongze Cheng 已提交
1284
_exit:
H
Hongze Cheng 已提交
1285 1286 1287 1288 1289
  return code;
}
static FORCE_INLINE int32_t tColDataAppendValue4(SColData *pColData, SColVal *pColVal) {  // HAS_VALUE
  int32_t code = 0;

H
Hongze Cheng 已提交
1290 1291
  if (!COL_VAL_IS_VALUE(pColVal)) {
    if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
1292 1293 1294
      pColData->flag |= HAS_NONE;
    } else {
      pColData->flag |= HAS_NULL;
H
Hongze Cheng 已提交
1295
    }
H
Hongze Cheng 已提交
1296

H
Hongze Cheng 已提交
1297 1298
    int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
    code = tRealloc(&pColData->pBitMap, nBit);
H
Hongze Cheng 已提交
1299 1300
    if (code) goto _exit;

H
Hongze Cheng 已提交
1301
    memset(pColData->pBitMap, 255, nBit);
H
Hongze Cheng 已提交
1302
    SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1303

H
Hongze Cheng 已提交
1304
    code = tColDataPutValue(pColData, pColVal);
H
Hongze Cheng 已提交
1305
    if (code) goto _exit;
H
Hongze Cheng 已提交
1306
  } else {
H
Hongze Cheng 已提交
1307 1308
    code = tColDataPutValue(pColData, pColVal);
    if (code) goto _exit;
H
Hongze Cheng 已提交
1309 1310 1311
  }
  pColData->nVal++;

H
Hongze Cheng 已提交
1312
_exit:
H
Hongze Cheng 已提交
1313 1314
  return code;
}
H
Hongze Cheng 已提交
1315
static FORCE_INLINE int32_t tColDataAppendValue5(SColData *pColData, SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
H
Hongze Cheng 已提交
1316 1317
  int32_t code = 0;

H
Hongze Cheng 已提交
1318
  if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
1319 1320 1321 1322 1323 1324
    pColData->flag |= HAS_NULL;

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

H
Hongze Cheng 已提交
1325
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
H
Hongze Cheng 已提交
1326 1327 1328 1329 1330 1331
      SET_BIT2(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
    }
    SET_BIT2(pBitMap, pColData->nVal, 1);

    tFree(pColData->pBitMap);
    pColData->pBitMap = pBitMap;
H
Hongze Cheng 已提交
1332
  } else {
H
Hongze Cheng 已提交
1333 1334 1335
    code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
    if (code) goto _exit;

H
Hongze Cheng 已提交
1336
    if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
1337 1338 1339 1340
      SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
    } else {
      SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
    }
H
Hongze Cheng 已提交
1341
  }
H
Hongze Cheng 已提交
1342 1343
  code = tColDataPutValue(pColData, pColVal);
  if (code) goto _exit;
H
Hongze Cheng 已提交
1344

H
Hongze Cheng 已提交
1345 1346
  pColData->nVal++;

H
Hongze Cheng 已提交
1347
_exit:
H
Hongze Cheng 已提交
1348 1349
  return code;
}
H
Hongze Cheng 已提交
1350
static FORCE_INLINE int32_t tColDataAppendValue6(SColData *pColData, SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
H
Hongze Cheng 已提交
1351 1352
  int32_t code = 0;

H
Hongze Cheng 已提交
1353
  if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
1354 1355 1356 1357 1358 1359
    pColData->flag |= HAS_NONE;

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

H
Hongze Cheng 已提交
1360
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
H
Hongze Cheng 已提交
1361 1362 1363 1364 1365 1366
      SET_BIT2(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
    }
    SET_BIT2(pBitMap, pColData->nVal, 0);

    tFree(pColData->pBitMap);
    pColData->pBitMap = pBitMap;
H
Hongze Cheng 已提交
1367
  } else {
H
Hongze Cheng 已提交
1368 1369 1370
    code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
    if (code) goto _exit;

H
Hongze Cheng 已提交
1371
    if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
1372 1373 1374 1375
      SET_BIT1(pColData->pBitMap, pColData->nVal, 0);
    } else {
      SET_BIT1(pColData->pBitMap, pColData->nVal, 1);
    }
H
Hongze Cheng 已提交
1376
  }
H
Hongze Cheng 已提交
1377 1378 1379
  code = tColDataPutValue(pColData, pColVal);
  if (code) goto _exit;

H
Hongze Cheng 已提交
1380 1381
  pColData->nVal++;

H
Hongze Cheng 已提交
1382
_exit:
H
Hongze Cheng 已提交
1383 1384 1385
  return code;
}
static FORCE_INLINE int32_t tColDataAppendValue7(SColData *pColData,
H
Hongze Cheng 已提交
1386
                                                 SColVal  *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
1387 1388
  int32_t code = 0;

H
Hongze Cheng 已提交
1389 1390 1391
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
  if (code) goto _exit;

H
Hongze Cheng 已提交
1392
  if (COL_VAL_IS_NONE(pColVal)) {
H
Hongze Cheng 已提交
1393
    SET_BIT2(pColData->pBitMap, pColData->nVal, 0);
H
Hongze Cheng 已提交
1394
  } else if (COL_VAL_IS_NULL(pColVal)) {
H
Hongze Cheng 已提交
1395
    SET_BIT2(pColData->pBitMap, pColData->nVal, 1);
H
Hongze Cheng 已提交
1396
  } else {
H
Hongze Cheng 已提交
1397
    SET_BIT2(pColData->pBitMap, pColData->nVal, 2);
H
Hongze Cheng 已提交
1398
  }
H
Hongze Cheng 已提交
1399 1400 1401
  code = tColDataPutValue(pColData, pColVal);
  if (code) goto _exit;

H
Hongze Cheng 已提交
1402 1403
  pColData->nVal++;

H
Hongze Cheng 已提交
1404
_exit:
H
Hongze Cheng 已提交
1405 1406
  return code;
}
H
Hongze Cheng 已提交
1407 1408 1409 1410
static int32_t (*tColDataAppendValueImpl[])(SColData *pColData, SColVal *pColVal) = {
    tColDataAppendValue0,  // 0
    tColDataAppendValue1,  // HAS_NONE
    tColDataAppendValue2,  // HAS_NULL
H
refact  
Hongze Cheng 已提交
1411
    tColDataAppendValue3,  // HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
1412
    tColDataAppendValue4,  // HAS_VALUE
H
refact  
Hongze Cheng 已提交
1413 1414 1415
    tColDataAppendValue5,  // HAS_VALUE|HAS_NONE
    tColDataAppendValue6,  // HAS_VALUE|HAS_NULL
    tColDataAppendValue7   // HAS_VALUE|HAS_NULL|HAS_NONE
H
Hongze Cheng 已提交
1416
};
H
refact  
Hongze Cheng 已提交
1417
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
H
Hongze Cheng 已提交
1418 1419
  ASSERT(pColData->cid == pColVal->cid && pColData->type == pColVal->type);
  return tColDataAppendValueImpl[pColData->flag](pColData, pColVal);
H
refact  
Hongze Cheng 已提交
1420 1421
}

H
Hongze Cheng 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
}
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
}
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL|HAS_NONE
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
    case 0:
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
      break;
    case 1:
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
      break;
    default:
      ASSERT(0);
  }
}
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
  SValue value;
  if (IS_VAR_DATA_TYPE(pColData->type)) {
    if (iVal + 1 < pColData->nVal) {
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
    } else {
      value.nData = pColData->nData - pColData->aOffset[iVal];
    }
    value.pData = pColData->pData + pColData->aOffset[iVal];
  } else {
    tGetValue(pColData->pData + tDataTypes[pColData->type].bytes * iVal, &value, pColData->type);
  }
  *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 已提交
1507
  ASSERT(iVal >= 0 && iVal < pColData->nVal && pColData->flag);
H
Hongze Cheng 已提交
1508 1509 1510
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
}

H
Hongze Cheng 已提交
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
uint8_t tColDataGetBitValue(SColData *pColData, int32_t iVal) {
  uint8_t v;
  switch (pColData->flag) {
    case HAS_NONE:
      v = 0;
      break;
    case HAS_NULL:
      v = 1;
      break;
    case (HAS_NULL | HAS_NONE):
      v = GET_BIT1(pColData->pBitMap, iVal);
      break;
    case HAS_VALUE:
      v = 2;
      break;
    case (HAS_VALUE | HAS_NONE):
      v = GET_BIT1(pColData->pBitMap, iVal);
      if (v) v = 2;
      break;
    case (HAS_VALUE | HAS_NULL):
      v = GET_BIT1(pColData->pBitMap, iVal) + 1;
      break;
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
      v = GET_BIT2(pColData->pBitMap, iVal);
      break;
    default:
      ASSERT(0);
      break;
  }
  return v;
}

H
refact  
Hongze Cheng 已提交
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
int32_t tColDataCopy(SColData *pColDataSrc, SColData *pColDataDest) {
  int32_t code = 0;
  int32_t size;

  ASSERT(pColDataSrc->nVal > 0);
  ASSERT(pColDataDest->cid = pColDataSrc->cid);
  ASSERT(pColDataDest->type = pColDataSrc->type);

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

  // bitmap
  if (pColDataSrc->flag != HAS_NONE && pColDataSrc->flag != HAS_NULL && pColDataSrc->flag != HAS_VALUE) {
    size = BIT2_SIZE(pColDataSrc->nVal);
    code = tRealloc(&pColDataDest->pBitMap, size);
    if (code) goto _exit;
    memcpy(pColDataDest->pBitMap, pColDataSrc->pBitMap, size);
  }

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

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

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

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

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