tdataformat.c 38.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"
S
Shengliang Guan 已提交
18
#include "tcoding.h"
L
Liu Jicong 已提交
19
#include "tdatablock.h"
S
log  
Shengliang Guan 已提交
20
#include "tlog.h"
H
more  
hzcheng 已提交
21

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

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

H
Hongze Cheng 已提交
31
#define TSROW_IS_KV_ROW(r) ((r)->flags & TSROW_KV_ROW)
H
Hongze Cheng 已提交
32 33
#define BIT1_SIZE(n)       (((n)-1) / 8 + 1)
#define BIT2_SIZE(n)       (((n)-1) / 4 + 1)
H
Hongze Cheng 已提交
34 35
#define SET_BIT1(p, i, v)  ((p)[(i) / 8] = (p)[(i) / 8] & (~(((uint8_t)1) << ((i) % 8))) | ((v) << ((i) % 8)))
#define SET_BIT2(p, i, v)  ((p)[(i) / 4] = (p)[(i) / 4] & (~(((uint8_t)3) << ((i) % 4))) | ((v) << ((i) % 4)))
H
Hongze Cheng 已提交
36 37
#define GET_BIT1(p, i)     (((p)[(i) / 8] >> ((i) % 8)) & ((uint8_t)1))
#define GET_BIT2(p, i)     (((p)[(i) / 4] >> ((i) % 4)) & ((uint8_t)3))
H
Hongze Cheng 已提交
38

H
Hongze Cheng 已提交
39
// SValue
H
more  
Hongze Cheng 已提交
40
static FORCE_INLINE int32_t tPutValue(uint8_t *p, SValue *pValue, int8_t type) {
H
Hongze Cheng 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  int32_t n = 0;

  if (IS_VAR_DATA_TYPE(type)) {
    n += tPutBinary(p ? p + n : p, pValue->pData, pValue->nData);
  } else {
    switch (type) {
      case TSDB_DATA_TYPE_BOOL:
        n += tPutI8(p ? p + n : p, pValue->i8 ? 1 : 0);
        break;
      case TSDB_DATA_TYPE_TINYINT:
        n += tPutI8(p ? p + n : p, pValue->i8);
        break;
      case TSDB_DATA_TYPE_SMALLINT:
        n += tPutI16(p ? p + n : p, pValue->i16);
        break;
      case TSDB_DATA_TYPE_INT:
        n += tPutI32(p ? p + n : p, pValue->i32);
        break;
      case TSDB_DATA_TYPE_BIGINT:
        n += tPutI64(p ? p + n : p, pValue->i64);
        break;
      case TSDB_DATA_TYPE_FLOAT:
        n += tPutFloat(p ? p + n : p, pValue->f);
        break;
      case TSDB_DATA_TYPE_DOUBLE:
        n += tPutDouble(p ? p + n : p, pValue->d);
        break;
      case TSDB_DATA_TYPE_TIMESTAMP:
        n += tPutI64(p ? p + n : p, pValue->ts);
        break;
      case TSDB_DATA_TYPE_UTINYINT:
        n += tPutU8(p ? p + n : p, pValue->u8);
        break;
      case TSDB_DATA_TYPE_USMALLINT:
        n += tPutU16(p ? p + n : p, pValue->u16);
        break;
      case TSDB_DATA_TYPE_UINT:
        n += tPutU32(p ? p + n : p, pValue->u32);
        break;
      case TSDB_DATA_TYPE_UBIGINT:
        n += tPutU64(p ? p + n : p, pValue->u64);
        break;
      default:
        ASSERT(0);
    }
  }

  return n;
}

H
more  
Hongze Cheng 已提交
91
static FORCE_INLINE int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type) {
H
Hongze Cheng 已提交
92 93 94
  int32_t n = 0;

  if (IS_VAR_DATA_TYPE(type)) {
H
Hongze Cheng 已提交
95
    n += tGetBinary(p, &pValue->pData, pValue ? &pValue->nData : NULL);
H
Hongze Cheng 已提交
96 97 98
  } else {
    switch (type) {
      case TSDB_DATA_TYPE_BOOL:
H
Hongze Cheng 已提交
99
        n += tGetI8(p, &pValue->i8);
H
Hongze Cheng 已提交
100 101
        break;
      case TSDB_DATA_TYPE_TINYINT:
H
Hongze Cheng 已提交
102
        n += tGetI8(p, &pValue->i8);
H
Hongze Cheng 已提交
103 104
        break;
      case TSDB_DATA_TYPE_SMALLINT:
H
Hongze Cheng 已提交
105
        n += tGetI16(p, &pValue->i16);
H
Hongze Cheng 已提交
106 107
        break;
      case TSDB_DATA_TYPE_INT:
H
Hongze Cheng 已提交
108
        n += tGetI32(p, &pValue->i32);
H
Hongze Cheng 已提交
109 110
        break;
      case TSDB_DATA_TYPE_BIGINT:
H
Hongze Cheng 已提交
111
        n += tGetI64(p, &pValue->i64);
H
Hongze Cheng 已提交
112 113
        break;
      case TSDB_DATA_TYPE_FLOAT:
H
Hongze Cheng 已提交
114
        n += tGetFloat(p, &pValue->f);
H
Hongze Cheng 已提交
115 116
        break;
      case TSDB_DATA_TYPE_DOUBLE:
H
Hongze Cheng 已提交
117
        n += tGetDouble(p, &pValue->d);
H
Hongze Cheng 已提交
118 119
        break;
      case TSDB_DATA_TYPE_TIMESTAMP:
H
Hongze Cheng 已提交
120
        n += tGetI64(p, &pValue->ts);
H
Hongze Cheng 已提交
121 122
        break;
      case TSDB_DATA_TYPE_UTINYINT:
H
Hongze Cheng 已提交
123
        n += tGetU8(p, &pValue->u8);
H
Hongze Cheng 已提交
124 125
        break;
      case TSDB_DATA_TYPE_USMALLINT:
H
Hongze Cheng 已提交
126
        n += tGetU16(p, &pValue->u16);
H
Hongze Cheng 已提交
127 128
        break;
      case TSDB_DATA_TYPE_UINT:
H
Hongze Cheng 已提交
129
        n += tGetU32(p, &pValue->u32);
H
Hongze Cheng 已提交
130 131
        break;
      case TSDB_DATA_TYPE_UBIGINT:
H
Hongze Cheng 已提交
132
        n += tGetU64(p, &pValue->u64);
H
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141
        break;
      default:
        ASSERT(0);
    }
  }

  return n;
}

H
more  
Hongze Cheng 已提交
142 143 144 145 146
int tValueCmprFn(const SValue *pValue1, const SValue *pValue2, int8_t type) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
147
// STSRow2 ========================================================================
H
Hongze Cheng 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
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 已提交
166

H
Hongze Cheng 已提交
167 168
      default:
        ASSERT(0);
H
more  
Hongze Cheng 已提交
169 170
    }
  }
H
Hongze Cheng 已提交
171 172 173 174 175 176 177 178 179 180 181
}
#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 已提交
182

H
Hongze Cheng 已提交
183 184 185 186 187 188
int32_t tTSRowNew(STSRowBuilder *pBuilder, SArray *pArray, STSchema *pTSchema, STSRow2 **ppRow) {
  int32_t   code = 0;
  STColumn *pTColumn;
  SColVal  *pColVal;
  int32_t   nColVal = taosArrayGetSize(pArray);
  int32_t   iColVal;
H
more  
Hongze Cheng 已提交
189

H
Hongze Cheng 已提交
190
  ASSERT(nColVal > 0);
H
more  
Hongze Cheng 已提交
191

H
Hongze Cheng 已提交
192 193 194 195 196 197
  // 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 已提交
198

H
Hongze Cheng 已提交
199 200
  iColVal = 0;
  for (int32_t iColumn = 0; iColumn < pTSchema->numOfCols; iColumn++) {
H
more  
Hongze Cheng 已提交
201 202 203 204 205 206 207
    pTColumn = &pTSchema->columns[iColumn];
    if (iColVal < nColVal) {
      pColVal = (SColVal *)taosArrayGet(pArray, iColVal);
    } else {
      pColVal = NULL;
    }

H
Hongze Cheng 已提交
208 209 210
    if (iColumn == 0) {
      ASSERT(pColVal->cid == pTColumn->colId);
      ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);
C
Cary Xu 已提交
211
      ASSERT(pTColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID);
H
Hongze Cheng 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

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

          if (pColVal->isNone) {
            flags |= TSROW_HAS_NONE;
          } else if (pColVal->isNull) {
            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 已提交
238
        } else {
H
Hongze Cheng 已提交
239
          ASSERT(0);
H
more  
Hongze Cheng 已提交
240 241
        }
      } else {
H
Hongze Cheng 已提交
242
        flags |= TSROW_HAS_NONE;
H
more  
Hongze Cheng 已提交
243 244
      }
    }
H
Hongze Cheng 已提交
245
  }
H
more  
Hongze Cheng 已提交
246

C
Cary Xu 已提交
247
  ASSERT(flags);
H
more  
Hongze Cheng 已提交
248

H
Hongze Cheng 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  // 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 已提交
271 272
        break;
        ASSERT(0);
H
more  
Hongze Cheng 已提交
273 274
    }

H
Hongze Cheng 已提交
275 276 277 278 279 280 281 282 283 284
    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 已提交
285 286
    }

C
Cary Xu 已提交
287
    if (nDataT <= nDataK) {
H
Hongze Cheng 已提交
288
      nData = nDataT;
H
more  
Hongze Cheng 已提交
289
    } else {
H
Hongze Cheng 已提交
290 291
      nData = nDataK;
      flags |= tflags;
H
more  
Hongze Cheng 已提交
292 293
    }
  }
H
more  
Hongze Cheng 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

  // 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 已提交
312
      pBuilder->tsRow.nData = nData;
H
more  
Hongze Cheng 已提交
313 314 315 316 317 318 319 320
      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 已提交
321 322 323
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
324 325 326 327 328
    if (nData == 0) {
      (*ppRow)->nData = 0;
      (*ppRow)->pData = NULL;
    } else {
      (*ppRow)->nData = nData;
H
more  
Hongze Cheng 已提交
329 330
      (*ppRow)->pData = taosMemoryMalloc(nData);
      if ((*ppRow)->pData == NULL) {
H
Hongze Cheng 已提交
331
        taosMemoryFree(*ppRow);
H
more  
Hongze Cheng 已提交
332 333 334 335
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _exit;
      }
    }
H
more  
Hongze Cheng 已提交
336
  }
H
more  
Hongze Cheng 已提交
337

H
Hongze Cheng 已提交
338 339 340
  // build
  (*ppRow)->flags = flags;
  (*ppRow)->sver = pTSchema->version;
H
more  
Hongze Cheng 已提交
341

H
Hongze Cheng 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355
  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 已提交
356
    if ((flags & 0xf0) == 0) {
H
Hongze Cheng 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
      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 已提交
377
          ASSERT(0);
C
Cary Xu 已提交
378
          break;
H
Hongze Cheng 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
      }
    } 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++;

          if (pColVal->isNone) {
            goto _set_none;
          } else if (pColVal->isNull) {
            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 已提交
422
      if ((flags & 0xf0) == 0) {
H
Hongze Cheng 已提交
423
        setBitMap(pb, 0, iColumn - 1, flags);
C
Cary Xu 已提交
424 425 426 427 428 429 430
        if (flags & TSROW_HAS_VAL) { // set 0
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
            *(VarDataOffsetT *)(pf + pTColumn->offset) = 0;
          } else {
            tPutValue(pf + pTColumn->offset, &((SValue){0}), pTColumn->type);
          }
        }
H
Hongze Cheng 已提交
431 432 433 434
      }
      continue;

    _set_null:
C
Cary Xu 已提交
435
      if ((flags & 0xf0) == 0) {
H
Hongze Cheng 已提交
436
        setBitMap(pb, 1, iColumn - 1, flags);
C
Cary Xu 已提交
437 438 439 440 441 442 443
        if (flags & TSROW_HAS_VAL) { // set 0
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
            *(VarDataOffsetT *)(pf + pTColumn->offset) = 0;
          } else {
            tPutValue(pf + pTColumn->offset, &((SValue){0}), pTColumn->type);
          }
        }
H
Hongze Cheng 已提交
444 445 446 447 448 449
      } else {
        SET_IDX(pidx, pTSKVRow->nCols, nkv, flags);
        pTSKVRow->nCols++;
        nkv += tPutI16v(pkv + nkv, -pTColumn->colId);
      }
      continue;
H
more  
Hongze Cheng 已提交
450

H
Hongze Cheng 已提交
451
    _set_value:
C
Cary Xu 已提交
452
      if ((flags & 0xf0) == 0) {
H
Hongze Cheng 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        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 已提交
469 470
  }

H
more  
Hongze Cheng 已提交
471
_exit:
H
Hongze Cheng 已提交
472 473 474
  return code;
}

H
Hongze Cheng 已提交
475 476
int32_t tTSRowClone(const STSRow2 *pRow, STSRow2 **ppRow) {
  int32_t code = 0;
H
Hongze Cheng 已提交
477

H
Hongze Cheng 已提交
478
  (*ppRow) = (STSRow2 *)taosMemoryMalloc(sizeof(**ppRow));
H
Hongze Cheng 已提交
479
  if (*ppRow == NULL) {
H
Hongze Cheng 已提交
480 481
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
482
  }
H
Hongze Cheng 已提交
483 484
  **ppRow = *pRow;
  (*ppRow)->pData = NULL;
H
Hongze Cheng 已提交
485 486

  if (pRow->nData) {
H
Hongze Cheng 已提交
487 488 489 490 491 492
    (*ppRow)->pData = taosMemoryMalloc(pRow->nData);
    if ((*ppRow)->pData == NULL) {
      taosMemoryFree(*ppRow);
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
493 494 495
    memcpy((*ppRow)->pData, pRow->pData, pRow->nData);
  }

H
Hongze Cheng 已提交
496 497
_exit:
  return code;
H
Hongze Cheng 已提交
498 499 500
}

void tTSRowFree(STSRow2 *pRow) {
H
Hongze Cheng 已提交
501 502 503 504
  if (pRow) {
    if (pRow->pData) taosMemoryFree(pRow->pData);
    taosMemoryFree(pRow);
  }
H
Hongze Cheng 已提交
505 506
}

H
Hongze Cheng 已提交
507
void tTSRowGet(STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
C
Cary Xu 已提交
508
  uint8_t   isTuple = ((pRow->flags & 0xf0) == 0) ? 1 : 0;
H
Hongze Cheng 已提交
509
  STColumn *pTColumn = &pTSchema->columns[iCol];
H
Hongze Cheng 已提交
510 511
  uint8_t   flags = pRow->flags & (uint8_t)0xf;
  SValue    value;
H
Hongze Cheng 已提交
512

H
Hongze Cheng 已提交
513
  ASSERT(iCol < pTSchema->numOfCols);
C
Cary Xu 已提交
514
  ASSERT(flags);
H
Hongze Cheng 已提交
515
  ASSERT(pRow->sver == pTSchema->version);
H
Hongze Cheng 已提交
516

H
Hongze Cheng 已提交
517 518 519
  if (iCol == 0) {
    value.ts = pRow->ts;
    goto _return_value;
H
Hongze Cheng 已提交
520 521
  }

H
Hongze Cheng 已提交
522
  if (flags == TSROW_HAS_NONE) {
H
more  
Hongze Cheng 已提交
523
    goto _return_none;
C
Cary Xu 已提交
524
  } else if (flags == TSROW_HAS_NULL) {
H
more  
Hongze Cheng 已提交
525
    goto _return_null;
H
Hongze Cheng 已提交
526
  }
H
Hongze Cheng 已提交
527

H
Hongze Cheng 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541
  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 已提交
542
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
543 544 545
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
H
Hongze Cheng 已提交
546
        } else {
H
Hongze Cheng 已提交
547
          goto _return_null;
H
Hongze Cheng 已提交
548
        }
H
Hongze Cheng 已提交
549
      case TSROW_HAS_VAL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
550 551 552
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
H
Hongze Cheng 已提交
553
        } else {
H
Hongze Cheng 已提交
554 555
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
556
        }
H
Hongze Cheng 已提交
557
      case TSROW_HAS_VAL | TSROW_HAS_NULL:
H
Hongze Cheng 已提交
558 559 560
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_null;
H
Hongze Cheng 已提交
561
        } else {
H
Hongze Cheng 已提交
562 563
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
564
        }
H
Hongze Cheng 已提交
565
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
566 567 568 569 570
        b = GET_BIT2(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
        } else if (b == 1) {
          goto _return_null;
H
Hongze Cheng 已提交
571
        } else {
H
Hongze Cheng 已提交
572 573
          pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
574
        }
H
Hongze Cheng 已提交
575
      default:
H
Hongze Cheng 已提交
576
        ASSERT(0);
H
Hongze Cheng 已提交
577
    }
H
Hongze Cheng 已提交
578

H
Hongze Cheng 已提交
579 580 581
    ASSERT(pf);

    p = pf + pTColumn->offset;
H
Hongze Cheng 已提交
582
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
H
Hongze Cheng 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
      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 已提交
605
    } else {
H
Hongze Cheng 已提交
606 607 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 633 634
      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 已提交
635
    }
H
Hongze Cheng 已提交
636 637 638

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

H
Hongze Cheng 已提交
641
_return_none:
H
more  
Hongze Cheng 已提交
642
  *pColVal = COL_VAL_NONE(pTColumn->colId);
H
Hongze Cheng 已提交
643 644 645
  return;

_return_null:
H
more  
Hongze Cheng 已提交
646
  *pColVal = COL_VAL_NULL(pTColumn->colId);
H
Hongze Cheng 已提交
647 648 649
  return;

_return_value:
H
more  
Hongze Cheng 已提交
650
  *pColVal = COL_VAL_VALUE(pTColumn->colId, value);
H
Hongze Cheng 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
  return;
}

int32_t tTSRowToArray(STSRow2 *pRow, STSchema *pTSchema, SArray **ppArray) {
  int32_t code = 0;
  SColVal cv;

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

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

_exit:
  return code;
}

int32_t tPutTSRow(uint8_t *p, STSRow2 *pRow) {
  int32_t n = 0;

  n += tPutI64(p ? p + n : p, pRow->ts);
  n += tPutI8(p ? p + n : p, pRow->flags);
  n += tPutI32v(p ? p + n : p, pRow->sver);

  ASSERT(pRow->flags & 0xf);

  switch (pRow->flags & 0xf) {
    case TSROW_HAS_NONE:
    case TSROW_HAS_NULL:
      ASSERT(pRow->nData == 0);
      ASSERT(pRow->pData == NULL);
      break;
    default:
      ASSERT(pRow->nData && pRow->pData);
      n += tPutBinary(p ? p + n : p, pRow->pData, pRow->nData);
      break;
  }

  return n;
}

int32_t tGetTSRow(uint8_t *p, STSRow2 *pRow) {
  int32_t n = 0;

  n += tGetI64(p + n, &pRow->ts);
  n += tGetI8(p + n, &pRow->flags);
  n += tGetI32v(p + n, &pRow->sver);

  ASSERT(pRow->flags);
  switch (pRow->flags & 0xf) {
    case TSROW_HAS_NONE:
    case TSROW_HAS_NULL:
      pRow->nData = 0;
      pRow->pData = NULL;
      break;
    default:
      n += tGetBinary(p + n, &pRow->pData, &pRow->nData);
      break;
  }

  return n;
H
Hongze Cheng 已提交
717 718 719
}

// STSchema
H
Hongze Cheng 已提交
720 721 722 723 724 725 726
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 已提交
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
  (*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 已提交
752 753 754
  return 0;
}

H
Hongze Cheng 已提交
755 756 757
void tTSchemaDestroy(STSchema *pTSchema) {
  if (pTSchema) taosMemoryFree(pTSchema);
}
H
Hongze Cheng 已提交
758

H
Hongze Cheng 已提交
759
// STSRowBuilder
H
Hongze Cheng 已提交
760

H
more  
Hongze Cheng 已提交
761
// STag
H
Hongze Cheng 已提交
762 763
static int tTagValCmprFn(const void *p1, const void *p2) {
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
764
    return -1;
H
Hongze Cheng 已提交
765
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
766 767
    return 1;
  }
H
Hongze Cheng 已提交
768

H
Hongze Cheng 已提交
769 770
  return 0;
}
H
Hongze Cheng 已提交
771 772 773
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
}
C
Cary Xu 已提交
774 775 776 777 778 779 780

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};
781
      strncpy(tmpVal, val, vlen > 31 ? 31 : vlen);
C
Cary Xu 已提交
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 809 810 811 812 813 814 815 816 817 818 819
      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 已提交
820 821 822
    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 已提交
823 824 825 826 827 828 829
    default:
      ASSERT(0);
      break;
  }
}

void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
H
Hongze Cheng 已提交
830 831
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
832 833 834 835 836 837 838 839 840 841
  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 已提交
842
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
843 844 845 846 847 848 849 850
    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 已提交
851
    } else {
852
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
C
Cary Xu 已提交
853
    }
854
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
C
Cary Xu 已提交
855
    tGetTagVal(p + offset, &tagVal, isJson);
856
    if (IS_VAR_DATA_TYPE(tagVal.type)) {
wmmhello's avatar
wmmhello 已提交
857
      debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
858
    } else {
wmmhello's avatar
wmmhello 已提交
859 860
      debugPrintTagVal(tagVal.type, &tagVal.i64, tDataTypes[tagVal.type].bytes, __func__, __LINE__);
    }
C
Cary Xu 已提交
861 862 863 864
  }
  printf("\n");
}

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

H
Hongze Cheng 已提交
868 869 870 871 872 873
  // key
  if (isJson) {
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
  } else {
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
  }
H
Hongze Cheng 已提交
874

H
Hongze Cheng 已提交
875 876 877 878 879 880 881
  // 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 已提交
882
    p = p ? p + n : p;
wmmhello's avatar
wmmhello 已提交
883
    n += tDataTypes[pTagVal->type].bytes;
884
    if (p) memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
H
Hongze Cheng 已提交
885 886
  }

H
Hongze Cheng 已提交
887 888 889 890 891 892 893 894 895 896
  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 已提交
897 898
  }

H
Hongze Cheng 已提交
899 900
  // type
  n += tGetI8(p + n, &pTagVal->type);
H
Hongze Cheng 已提交
901

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

  return n;
}
wmmhello's avatar
wmmhello 已提交
912 913 914 915 916 917 918 919 920 921 922 923

bool tTagIsJson(const void *pTag){
  return (((const STag *)pTag)->flags & TD_TAG_JSON);
}

bool tTagIsJsonNull(void *data){
  STag *pTag = (STag*)data;
  int8_t   isJson = tTagIsJson(pTag);
  if(!isJson) return false;
  return ((STag*)data)->nTag == 0;
}

H
Hongze Cheng 已提交
924
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
H
Hongze Cheng 已提交
925 926 927
  int32_t  code = 0;
  uint8_t *p = NULL;
  int16_t  n = 0;
H
Hongze Cheng 已提交
928
  int16_t  nTag = taosArrayGetSize(pArray);
H
Hongze Cheng 已提交
929 930
  int32_t  szTag = 0;
  int8_t   isLarge = 0;
H
Hongze Cheng 已提交
931 932 933

  // sort
  if (isJson) {
H
Hongze Cheng 已提交
934
    qsort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
H
Hongze Cheng 已提交
935
  } else {
H
Hongze Cheng 已提交
936
    qsort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
H
Hongze Cheng 已提交
937 938 939
  }

  // get size
H
Hongze Cheng 已提交
940
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
941
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
942
  }
H
Hongze Cheng 已提交
943 944 945 946 947 948
  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 已提交
949

H
Hongze Cheng 已提交
950
  ASSERT(szTag <= INT16_MAX);
H
Hongze Cheng 已提交
951 952

  // build tag
C
Cary Xu 已提交
953
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
H
Hongze Cheng 已提交
954 955 956 957
  if ((*ppTag) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
958 959 960 961 962 963 964
  (*ppTag)->flags = 0;
  if (isJson) {
    (*ppTag)->flags |= TD_TAG_JSON;
  }
  if (isLarge) {
    (*ppTag)->flags |= TD_TAG_LARGE;
  }
H
Hongze Cheng 已提交
965 966 967
  (*ppTag)->len = szTag;
  (*ppTag)->nTag = nTag;
  (*ppTag)->ver = version;
H
Hongze Cheng 已提交
968

H
Hongze Cheng 已提交
969 970 971 972 973
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
  } else {
    p = (uint8_t *)&(*ppTag)->idx[nTag];
  }
H
Hongze Cheng 已提交
974 975
  n = 0;
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
976 977 978 979 980
    if (isLarge) {
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
    } else {
      (*ppTag)->idx[iTag] = n;
    }
H
Hongze Cheng 已提交
981
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
982
  }
983
#ifdef TD_DEBUG_PRINT_TAG
C
Cary Xu 已提交
984
  debugPrintSTag(*ppTag, __func__, __LINE__);
985
#endif
C
Cary Xu 已提交
986

H
Hongze Cheng 已提交
987 988 989 990
  return code;

_err:
  return code;
H
Hongze Cheng 已提交
991 992 993 994 995 996
}

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

997 998 999
char *tTagValToData(const STagVal *value, bool isJson) {
  if (!value) return NULL;
  char  *data = NULL;
wmmhello's avatar
wmmhello 已提交
1000 1001 1002 1003
  int8_t typeBytes = 0;
  if (isJson) {
    typeBytes = CHAR_BYTES;
  }
1004
  if (IS_VAR_DATA_TYPE(value->type)) {
wmmhello's avatar
wmmhello 已提交
1005
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
1006 1007
    if (data == NULL) return NULL;
    if (isJson) *data = value->type;
wmmhello's avatar
wmmhello 已提交
1008 1009
    varDataLen(data + typeBytes) = value->nData;
    memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
1010 1011
  } else {
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
wmmhello's avatar
wmmhello 已提交
1012 1013 1014 1015 1016
  }

  return data;
}

C
Cary Xu 已提交
1017
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
H
Hongze Cheng 已提交
1018 1019 1020
  int16_t  lidx = 0;
  int16_t  ridx = pTag->nTag - 1;
  int16_t  midx;
H
Hongze Cheng 已提交
1021 1022 1023 1024
  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 已提交
1025 1026 1027
  STagVal  tv;
  int      c;

H
Hongze Cheng 已提交
1028 1029 1030 1031 1032 1033
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }

H
Hongze Cheng 已提交
1034 1035 1036 1037 1038
  pTagVal->type = TSDB_DATA_TYPE_NULL;
  pTagVal->pData = NULL;
  pTagVal->nData = 0;
  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;
H
Hongze Cheng 已提交
1039 1040 1041 1042 1043
    if (isLarge) {
      offset = ((int16_t *)pTag->idx)[midx];
    } else {
      offset = pTag->idx[midx];
    }
H
Hongze Cheng 已提交
1044

H
Hongze Cheng 已提交
1045 1046
    tGetTagVal(p + offset, &tv, isJson);
    if (isJson) {
H
Hongze Cheng 已提交
1047
      c = tTagValJsonCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1048
    } else {
H
Hongze Cheng 已提交
1049
      c = tTagValCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1050 1051
    }

H
Hongze Cheng 已提交
1052 1053 1054 1055
    if (c < 0) {
      ridx = midx - 1;
    } else if (c > 0) {
      lidx = midx + 1;
H
Hongze Cheng 已提交
1056
    } else {
H
Hongze Cheng 已提交
1057
      memcpy(pTagVal, &tv, sizeof(tv));
C
Cary Xu 已提交
1058
      return true;
H
Hongze Cheng 已提交
1059 1060
    }
  }
C
Cary Xu 已提交
1061
  return false;
H
Hongze Cheng 已提交
1062 1063
}

H
more  
Hongze Cheng 已提交
1064 1065
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
H
Hongze Cheng 已提交
1066 1067
}

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

C
Cary Xu 已提交
1070
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
H
Hongze Cheng 已提交
1071
  int32_t  code = 0;
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  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 已提交
1082

H
more  
Hongze Cheng 已提交
1083
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
H
Hongze Cheng 已提交
1084
  if (*ppArray == NULL) {
H
Hongze Cheng 已提交
1085 1086 1087 1088 1089
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
1090 1091 1092 1093 1094 1095
    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 已提交
1096
    taosArrayPush(*ppArray, &tv);
H
Hongze Cheng 已提交
1097 1098 1099 1100 1101 1102 1103 1104
  }

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
1105
#if 1  // ===================================================================================================================
1106
static void dataColSetNEleNull(SDataCol *pCol, int nEle);
H
Hongze Cheng 已提交
1107
int         tdAllocMemForCol(SDataCol *pCol, int maxPoints) {
L
Liu Jicong 已提交
1108
  int spaceNeeded = pCol->bytes * maxPoints;
S
Shengliang Guan 已提交
1109
  if (IS_VAR_DATA_TYPE(pCol->type)) {
L
Liu Jicong 已提交
1110
    spaceNeeded += sizeof(VarDataOffsetT) * maxPoints;
L
Liu Jicong 已提交
1111
  }
C
Cary Xu 已提交
1112
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
1113 1114 1115 1116
  int32_t nBitmapBytes = (int32_t)TD_BITMAP_BYTES(maxPoints);
  spaceNeeded += (int)nBitmapBytes;
  // TODO: Currently, the compression of bitmap parts is affiliated to the column data parts, thus allocate 1 more
  // TYPE_BYTES as to comprise complete TYPE_BYTES. Otherwise, invalid read/write would be triggered.
H
Hongze Cheng 已提交
1117 1118
  // spaceNeeded += TYPE_BYTES[pCol->type]; // the bitmap part is append as a single part since 2022.04.03, thus
  // remove the additional space
C
Cary Xu 已提交
1119
#endif
C
Cary Xu 已提交
1120

S
Shengliang Guan 已提交
1121
  if (pCol->spaceSize < spaceNeeded) {
wafwerar's avatar
wafwerar 已提交
1122
    void *ptr = taosMemoryRealloc(pCol->pData, spaceNeeded);
S
Shengliang Guan 已提交
1123 1124
    if (ptr == NULL) {
      uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)spaceNeeded, strerror(errno));
L
Liu Jicong 已提交
1125
      return -1;
L
Liu Jicong 已提交
1126 1127 1128
    } else {
      pCol->pData = ptr;
      pCol->spaceSize = spaceNeeded;
1129 1130
    }
  }
C
Cary Xu 已提交
1131
#ifdef TD_SUPPORT_BITMAP
1132

C
Cary Xu 已提交
1133 1134 1135 1136
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->pBitmap = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
    pCol->dataOff = POINTER_SHIFT(pCol->pBitmap, nBitmapBytes);
  } else {
C
Cary Xu 已提交
1137
    pCol->pBitmap = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
L
Liu Jicong 已提交
1138
  }
C
Cary Xu 已提交
1139 1140 1141 1142
#else
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->dataOff = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
  }
C
Cary Xu 已提交
1143
#endif
L
Liu Jicong 已提交
1144
  return 0;
1145 1146
}

H
hzcheng 已提交
1147 1148 1149
/**
 * Duplicate the schema and return a new object
 */
H
Hongze Cheng 已提交
1150
STSchema *tdDupSchema(const STSchema *pSchema) {
S
Shengliang Guan 已提交
1151
  int       tlen = sizeof(STSchema) + sizeof(STColumn) * schemaNCols(pSchema);
wafwerar's avatar
wafwerar 已提交
1152
  STSchema *tSchema = (STSchema *)taosMemoryMalloc(tlen);
H
hzcheng 已提交
1153 1154
  if (tSchema == NULL) return NULL;

H
Hongze Cheng 已提交
1155
  memcpy((void *)tSchema, (void *)pSchema, tlen);
H
hzcheng 已提交
1156 1157 1158 1159

  return tSchema;
}

H
TD-27  
hzcheng 已提交
1160 1161 1162
/**
 * Encode a schema to dst, and return the next pointer
 */
H
TD-353  
Hongze Cheng 已提交
1163 1164 1165 1166
int tdEncodeSchema(void **buf, STSchema *pSchema) {
  int tlen = 0;
  tlen += taosEncodeFixedI32(buf, schemaVersion(pSchema));
  tlen += taosEncodeFixedI32(buf, schemaNCols(pSchema));
H
TD-166  
hzcheng 已提交
1167

H
TD-27  
hzcheng 已提交
1168 1169
  for (int i = 0; i < schemaNCols(pSchema); i++) {
    STColumn *pCol = schemaColAt(pSchema, i);
H
TD-353  
Hongze Cheng 已提交
1170
    tlen += taosEncodeFixedI8(buf, colType(pCol));
C
Cary Xu 已提交
1171
    tlen += taosEncodeFixedI8(buf, colFlags(pCol));
H
TD-353  
Hongze Cheng 已提交
1172
    tlen += taosEncodeFixedI16(buf, colColId(pCol));
1173
    tlen += taosEncodeFixedI16(buf, colBytes(pCol));
H
TD-27  
hzcheng 已提交
1174 1175
  }

H
TD-353  
Hongze Cheng 已提交
1176
  return tlen;
H
TD-27  
hzcheng 已提交
1177 1178 1179 1180 1181
}

/**
 * Decode a schema from a binary.
 */
H
TD-353  
Hongze Cheng 已提交
1182
void *tdDecodeSchema(void *buf, STSchema **pRSchema) {
S
Shengliang Guan 已提交
1183 1184
  int             version = 0;
  int             numOfCols = 0;
H
TD-353  
Hongze Cheng 已提交
1185
  STSchemaBuilder schemaBuilder;
H
TD-27  
hzcheng 已提交
1186

H
TD-353  
Hongze Cheng 已提交
1187 1188
  buf = taosDecodeFixedI32(buf, &version);
  buf = taosDecodeFixedI32(buf, &numOfCols);
H
TD-27  
hzcheng 已提交
1189

H
Hongze Cheng 已提交
1190 1191
  if (tdInitTSchemaBuilder(&schemaBuilder, version) < 0) return NULL;

H
TD-353  
Hongze Cheng 已提交
1192
  for (int i = 0; i < numOfCols; i++) {
1193
    col_type_t  type = 0;
C
Cary Xu 已提交
1194
    int8_t      flags = 0;
1195 1196
    col_id_t    colId = 0;
    col_bytes_t bytes = 0;
H
TD-353  
Hongze Cheng 已提交
1197
    buf = taosDecodeFixedI8(buf, &type);
C
Cary Xu 已提交
1198
    buf = taosDecodeFixedI8(buf, &flags);
H
TD-353  
Hongze Cheng 已提交
1199
    buf = taosDecodeFixedI16(buf, &colId);
1200
    buf = taosDecodeFixedI32(buf, &bytes);
C
Cary Xu 已提交
1201
    if (tdAddColToSchema(&schemaBuilder, type, flags, colId, bytes) < 0) {
H
Hongze Cheng 已提交
1202 1203 1204
      tdDestroyTSchemaBuilder(&schemaBuilder);
      return NULL;
    }
H
TD-27  
hzcheng 已提交
1205 1206
  }

H
TD-353  
Hongze Cheng 已提交
1207
  *pRSchema = tdGetSchemaFromBuilder(&schemaBuilder);
H
Hongze Cheng 已提交
1208
  tdDestroyTSchemaBuilder(&schemaBuilder);
H
TD-353  
Hongze Cheng 已提交
1209
  return buf;
H
Hongze Cheng 已提交
1210 1211
}

C
Cary Xu 已提交
1212
int tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
1213 1214 1215
  if (pBuilder == NULL) return -1;

  pBuilder->tCols = 256;
wafwerar's avatar
wafwerar 已提交
1216
  pBuilder->columns = (STColumn *)taosMemoryMalloc(sizeof(STColumn) * pBuilder->tCols);
H
Hongze Cheng 已提交
1217 1218 1219 1220 1221 1222 1223 1224
  if (pBuilder->columns == NULL) return -1;

  tdResetTSchemaBuilder(pBuilder, version);
  return 0;
}

void tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder) {
  if (pBuilder) {
wafwerar's avatar
wafwerar 已提交
1225
    taosMemoryFreeClear(pBuilder->columns);
H
Hongze Cheng 已提交
1226 1227 1228
  }
}

C
Cary Xu 已提交
1229
void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
1230 1231 1232
  pBuilder->nCols = 0;
  pBuilder->tlen = 0;
  pBuilder->flen = 0;
T
Tao Liu 已提交
1233
  pBuilder->vlen = 0;
H
Hongze Cheng 已提交
1234 1235 1236
  pBuilder->version = version;
}

C
Cary Xu 已提交
1237
int32_t tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes) {
1238
  if (!isValidDataType(type)) return -1;
H
Hongze Cheng 已提交
1239 1240 1241

  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
wafwerar's avatar
wafwerar 已提交
1242
    STColumn *columns = (STColumn *)taosMemoryRealloc(pBuilder->columns, sizeof(STColumn) * pBuilder->tCols);
T
tickduan 已提交
1243 1244
    if (columns == NULL) return -1;
    pBuilder->columns = columns;
H
Hongze Cheng 已提交
1245 1246 1247 1248 1249
  }

  STColumn *pCol = &(pBuilder->columns[pBuilder->nCols]);
  colSetType(pCol, type);
  colSetColId(pCol, colId);
C
Cary Xu 已提交
1250
  colSetFlags(pCol, flags);
H
Hongze Cheng 已提交
1251 1252 1253
  if (pBuilder->nCols == 0) {
    colSetOffset(pCol, 0);
  } else {
S
Shengliang Guan 已提交
1254
    STColumn *pTCol = &(pBuilder->columns[pBuilder->nCols - 1]);
H
Hongze Cheng 已提交
1255 1256 1257 1258 1259
    colSetOffset(pCol, pTCol->offset + TYPE_BYTES[pTCol->type]);
  }

  if (IS_VAR_DATA_TYPE(type)) {
    colSetBytes(pCol, bytes);
T
Tao Liu 已提交
1260 1261
    pBuilder->tlen += (TYPE_BYTES[type] + bytes);
    pBuilder->vlen += bytes - sizeof(VarDataLenT);
H
Hongze Cheng 已提交
1262 1263 1264
  } else {
    colSetBytes(pCol, TYPE_BYTES[type]);
    pBuilder->tlen += TYPE_BYTES[type];
T
Tao Liu 已提交
1265
    pBuilder->vlen += TYPE_BYTES[type];
H
Hongze Cheng 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
  }

  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 已提交
1281
  STSchema *pSchema = (STSchema *)taosMemoryMalloc(tlen);
H
Hongze Cheng 已提交
1282 1283 1284 1285 1286 1287
  if (pSchema == NULL) return NULL;

  schemaVersion(pSchema) = pBuilder->version;
  schemaNCols(pSchema) = pBuilder->nCols;
  schemaTLen(pSchema) = pBuilder->tlen;
  schemaFLen(pSchema) = pBuilder->flen;
T
Tao Liu 已提交
1288
  schemaVLen(pSchema) = pBuilder->vlen;
H
Hongze Cheng 已提交
1289

C
Cary Xu 已提交
1290
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
1291
  schemaTLen(pSchema) += (int)TD_BITMAP_BYTES(schemaNCols(pSchema));
C
Cary Xu 已提交
1292 1293
#endif

H
Hongze Cheng 已提交
1294 1295
  memcpy(schemaColAt(pSchema, 0), pBuilder->columns, sizeof(STColumn) * pBuilder->nCols);

H
TD-27  
hzcheng 已提交
1296 1297 1298
  return pSchema;
}

1299
void dataColInit(SDataCol *pDataCol, STColumn *pCol, int maxPoints) {
H
TD-166  
hzcheng 已提交
1300 1301 1302
  pDataCol->type = colType(pCol);
  pDataCol->colId = colColId(pCol);
  pDataCol->bytes = colBytes(pCol);
S
Shengliang Guan 已提交
1303
  pDataCol->offset = colOffset(pCol) + 0;  // TD_DATA_ROW_HEAD_SIZE;
H
TD-166  
hzcheng 已提交
1304 1305 1306

  pDataCol->len = 0;
}
C
Cary Xu 已提交
1307

L
Liu Jicong 已提交
1308 1309 1310 1311 1312 1313
static FORCE_INLINE const void *tdGetColDataOfRowUnsafe(SDataCol *pCol, int row) {
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    return POINTER_SHIFT(pCol->pData, pCol->dataOff[row]);
  } else {
    return POINTER_SHIFT(pCol->pData, TYPE_BYTES[pCol->type] * row);
  }
H
TD-166  
hzcheng 已提交
1314 1315
}

H
TD-166  
hzcheng 已提交
1316
bool isNEleNull(SDataCol *pCol, int nEle) {
S
Shengliang Guan 已提交
1317
  if (isAllRowsNull(pCol)) return true;
1318
  for (int i = 0; i < nEle; ++i) {
L
Liu Jicong 已提交
1319
    if (!isNull(tdGetColDataOfRowUnsafe(pCol, i), pCol->type)) return false;
H
TD-166  
hzcheng 已提交
1320
  }
H
Hongze Cheng 已提交
1321
  return true;
H
TD-166  
hzcheng 已提交
1322 1323
}

C
Cary Xu 已提交
1324
void *dataColSetOffset(SDataCol *pCol, int nEle) {
H
TD-166  
hzcheng 已提交
1325 1326
  ASSERT(((pCol->type == TSDB_DATA_TYPE_BINARY) || (pCol->type == TSDB_DATA_TYPE_NCHAR)));

H
Hongze Cheng 已提交
1327
  void *tptr = pCol->pData;
H
TD-166  
hzcheng 已提交
1328
  // char *tptr = (char *)(pCol->pData);
H
TD-166  
hzcheng 已提交
1329

H
TD-166  
hzcheng 已提交
1330
  VarDataOffsetT offset = 0;
1331
  for (int i = 0; i < nEle; ++i) {
H
TD-166  
hzcheng 已提交
1332
    pCol->dataOff[i] = offset;
H
TD-166  
hzcheng 已提交
1333
    offset += varDataTLen(tptr);
H
hzcheng 已提交
1334
    tptr = POINTER_SHIFT(tptr, varDataTLen(tptr));
H
TD-166  
hzcheng 已提交
1335
  }
C
Cary Xu 已提交
1336
  return POINTER_SHIFT(tptr, varDataTLen(tptr));
H
TD-166  
hzcheng 已提交
1337 1338
}

L
Liu Jicong 已提交
1339
SDataCols *tdNewDataCols(int maxCols, int maxRows) {
wafwerar's avatar
wafwerar 已提交
1340
  SDataCols *pCols = (SDataCols *)taosMemoryCalloc(1, sizeof(SDataCols));
H
Haojun Liao 已提交
1341
  if (pCols == NULL) {
S
Shengliang Guan 已提交
1342
    uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)sizeof(SDataCols), strerror(errno));
H
Haojun Liao 已提交
1343 1344
    return NULL;
  }
H
TD-34  
hzcheng 已提交
1345

H
Hongze Cheng 已提交
1346
  pCols->maxPoints = maxRows;
L
Liu Jicong 已提交
1347 1348 1349
  pCols->maxCols = maxCols;
  pCols->numOfRows = 0;
  pCols->numOfCols = 0;
1350
  pCols->bitmapMode = TSDB_BITMODE_DEFAULT;
H
Hongze Cheng 已提交
1351 1352

  if (maxCols > 0) {
wafwerar's avatar
wafwerar 已提交
1353
    pCols->cols = (SDataCol *)taosMemoryCalloc(maxCols, sizeof(SDataCol));
H
Hongze Cheng 已提交
1354 1355 1356 1357 1358 1359
    if (pCols->cols == NULL) {
      uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)sizeof(SDataCol) * maxCols,
             strerror(errno));
      tdFreeDataCols(pCols);
      return NULL;
    }
1360
#if 0  // no need as calloc used
L
Liu Jicong 已提交
1361
    int i;
S
Shengliang Guan 已提交
1362
    for (i = 0; i < maxCols; i++) {
L
Liu Jicong 已提交
1363
      pCols->cols[i].spaceSize = 0;
L
Liu Jicong 已提交
1364
      pCols->cols[i].len = 0;
L
Liu Jicong 已提交
1365 1366 1367
      pCols->cols[i].pData = NULL;
      pCols->cols[i].dataOff = NULL;
    }
1368
#endif
H
Hongze Cheng 已提交
1369 1370
  }

H
TD-34  
hzcheng 已提交
1371 1372 1373
  return pCols;
}

H
Hongze Cheng 已提交
1374
int tdInitDataCols(SDataCols *pCols, STSchema *pSchema) {
1375 1376
  int i;
  int oldMaxCols = pCols->maxCols;
L
Liu Jicong 已提交
1377
  if (schemaNCols(pSchema) > oldMaxCols) {
H
Hongze Cheng 已提交
1378
    pCols->maxCols = schemaNCols(pSchema);
wafwerar's avatar
wafwerar 已提交
1379
    void *ptr = (SDataCol *)taosMemoryRealloc(pCols->cols, sizeof(SDataCol) * pCols->maxCols);
L
Liu Jicong 已提交
1380 1381
    if (ptr == NULL) return -1;
    pCols->cols = ptr;
1382
    for (i = oldMaxCols; i < pCols->maxCols; ++i) {
1383 1384
      pCols->cols[i].pData = NULL;
      pCols->cols[i].dataOff = NULL;
1385
      pCols->cols[i].pBitmap = NULL;
L
Liu Jicong 已提交
1386
      pCols->cols[i].spaceSize = 0;
1387
    }
L
Liu Jicong 已提交
1388
  }
1389 1390 1391
#if 0
  tdResetDataCols(pCols); // redundant loop to reset len/blen to 0, already reset in following dataColInit(...)
#endif
H
Hongze Cheng 已提交
1392

1393
  pCols->numOfRows = 0;
1394
  pCols->bitmapMode = TSDB_BITMODE_DEFAULT;
H
TD-34  
hzcheng 已提交
1395 1396
  pCols->numOfCols = schemaNCols(pSchema);

1397
  for (i = 0; i < schemaNCols(pSchema); ++i) {
1398
    dataColInit(pCols->cols + i, schemaColAt(pSchema, i), pCols->maxPoints);
H
TD-34  
hzcheng 已提交
1399
  }
S
Shengliang Guan 已提交
1400

H
Hongze Cheng 已提交
1401
  return 0;
H
TD-34  
hzcheng 已提交
1402 1403
}

H
Hongze Cheng 已提交
1404
SDataCols *tdFreeDataCols(SDataCols *pCols) {
1405
  int i;
H
TD-34  
hzcheng 已提交
1406
  if (pCols) {
S
Shengliang Guan 已提交
1407
    if (pCols->cols) {
1408
      int maxCols = pCols->maxCols;
1409
      for (i = 0; i < maxCols; ++i) {
1410
        SDataCol *pCol = &pCols->cols[i];
wafwerar's avatar
wafwerar 已提交
1411
        taosMemoryFreeClear(pCol->pData);
1412
      }
wafwerar's avatar
wafwerar 已提交
1413
      taosMemoryFree(pCols->cols);
1414 1415
      pCols->cols = NULL;
    }
wafwerar's avatar
wafwerar 已提交
1416
    taosMemoryFree(pCols);
H
TD-34  
hzcheng 已提交
1417
  }
H
Hongze Cheng 已提交
1418
  return NULL;
H
TD-34  
hzcheng 已提交
1419 1420 1421
}

void tdResetDataCols(SDataCols *pCols) {
B
Bomin Zhang 已提交
1422 1423
  if (pCols != NULL) {
    pCols->numOfRows = 0;
C
Cary Xu 已提交
1424
    pCols->bitmapMode = 0;
1425
    for (int i = 0; i < pCols->maxCols; ++i) {
B
Bomin Zhang 已提交
1426 1427
      dataColReset(pCols->cols + i);
    }
H
TD-34  
hzcheng 已提交
1428 1429
  }
}
H
Hongze Cheng 已提交
1430

H
Hongze Cheng 已提交
1431
#endif