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"
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 40
static FORCE_INLINE int tSKVIdxCmprFn(const void *p1, const void *p2);

H
Hongze Cheng 已提交
41
// SValue
H
more  
Hongze Cheng 已提交
42
static FORCE_INLINE int32_t tPutValue(uint8_t *p, SValue *pValue, int8_t type) {
H
Hongze Cheng 已提交
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 91 92
  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 已提交
93
static FORCE_INLINE int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type) {
H
Hongze Cheng 已提交
94 95 96
  int32_t n = 0;

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

  return n;
}

H
Hongze Cheng 已提交
144
// STSRow2 ========================================================================
H
more  
Hongze Cheng 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
static void tTupleTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow2 *pRow) {
  int32_t   nColVal = taosArrayGetSize(pArray);
  STColumn *pTColumn;
  SColVal  *pColVal;

  ASSERT(nColVal > 0);

  pRow->sver = pTSchema->version;

  // ts
  pTColumn = &pTSchema->columns[0];
  pColVal = (SColVal *)taosArrayGet(pArray, 0);

  ASSERT(pTColumn->colId == 0 && pColVal->cid == 0);
  ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);

  pRow->ts = pColVal->value.ts;

  // other fields
  int32_t  iColVal = 1;
  int32_t  bidx;
  uint32_t nv = 0;
  uint8_t *pb = NULL;
  uint8_t *pf = NULL;
  uint8_t *pv = NULL;
  uint8_t  flags = 0;
  for (int32_t iColumn = 1; iColumn < pTSchema->numOfCols; iColumn++) {
    bidx = iColumn - 1;
    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:
    flags |= TSROW_HAS_NONE;
    // SET_BIT2(pb, bidx, 0); (todo)
    continue;

  _set_null:
    flags != TSROW_HAS_NULL;
    // SET_BIT2(pb, bidx, 1); (todo)
    continue;

  _set_value:
    flags != TSROW_HAS_VAL;
    // SET_BIT2(pb, bidx, 2); (todo)
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
H
Hongze Cheng 已提交
214
      // nv += tPutColVal(pv ? pv + nv : pv, pColVal, pTColumn->type, 1);
H
more  
Hongze Cheng 已提交
215
    } else {
H
Hongze Cheng 已提交
216
      // tPutColVal(pf ? pf + pTColumn->offset : pf, pColVal, pTColumn->type, 1);
H
more  
Hongze Cheng 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    }
    continue;
  }

  ASSERT(flags);
  switch (flags & 0xf) {
    case TSROW_HAS_NONE:
    case TSROW_HAS_NULL:
      pRow->nData = 0;
      break;
    case TSROW_HAS_VAL:
      pRow->nData = pTSchema->flen + nv;
      break;
    case TSROW_HAS_NULL | TSROW_HAS_NONE:
      pRow->nData = BIT1_SIZE(pTSchema->numOfCols - 1);
      break;
    case TSROW_HAS_VAL | TSROW_HAS_NONE:
    case TSROW_HAS_VAL | TSROW_HAS_NULL:
      pRow->nData = BIT1_SIZE(pTSchema->numOfCols - 1) + pTSchema->flen + nv;
      break;
    case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
      pRow->nData = BIT2_SIZE(pTSchema->numOfCols - 1) + pTSchema->flen + nv;
      break;
    default:
      break;
  }
}

static void tMapTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow2 *pRow) {
  int32_t   nColVal = taosArrayGetSize(pArray);
  STColumn *pTColumn;
  SColVal  *pColVal;

  ASSERT(nColVal > 0);

  pRow->sver = pTSchema->version;

  // ts
  pTColumn = &pTSchema->columns[0];
  pColVal = (SColVal *)taosArrayGet(pArray, 0);

  ASSERT(pTColumn->colId == 0 && pColVal->cid == 0);
  ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP);

  pRow->ts = pColVal->value.ts;

  // other fields
  int32_t  iColVal = 1;
  uint32_t nv = 0;
  uint8_t *pv = NULL;
  uint8_t *pidx = NULL;
  uint8_t  flags = 0;
  int16_t  nCol = 0;
  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:
    flags |= TSROW_HAS_NONE;
    continue;

  _set_null:
    flags != TSROW_HAS_NULL;
    pidx[nCol++] = nv;
H
Hongze Cheng 已提交
305
    // nv += tPutColVal(pv ? pv + nv : pv, pColVal, pTColumn->type, 0);
H
more  
Hongze Cheng 已提交
306 307 308 309 310
    continue;

  _set_value:
    flags != TSROW_HAS_VAL;
    pidx[nCol++] = nv;
H
Hongze Cheng 已提交
311
    // nv += tPutColVal(pv ? pv + nv : pv, pColVal, pTColumn->type, 0);
H
more  
Hongze Cheng 已提交
312 313
    continue;
  }
H
more  
Hongze Cheng 已提交
314 315 316 317 318 319 320 321

  if (nv <= UINT8_MAX) {
    // small
  } else if (nv <= UINT16_MAX) {
    // mid
  } else {
    // large
  }
H
more  
Hongze Cheng 已提交
322 323
}

H
more  
Hongze Cheng 已提交
324
// try-decide-build
H
Hongze Cheng 已提交
325 326
int32_t tTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow2 **ppRow) {
  int32_t code = 0;
H
more  
Hongze Cheng 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340
  STSRow2 rowT = {0};
  STSRow2 rowM = {0};

  // try
  tTupleTSRowNew(pArray, pTSchema, &rowT);
  tMapTSRowNew(pArray, pTSchema, &rowM);

  // decide & build
  if (rowT.nData <= rowM.nData) {
    tTupleTSRowNew(pArray, pTSchema, &rowT);
  } else {
    tMapTSRowNew(pArray, pTSchema, &rowM);
  }

H
Hongze Cheng 已提交
341 342 343
  return code;
}

H
Hongze Cheng 已提交
344 345
int32_t tTSRowClone(const STSRow2 *pRow, STSRow2 **ppRow) {
  int32_t code = 0;
H
Hongze Cheng 已提交
346

H
Hongze Cheng 已提交
347
  (*ppRow) = (STSRow2 *)taosMemoryMalloc(sizeof(**ppRow));
H
Hongze Cheng 已提交
348
  if (*ppRow == NULL) {
H
Hongze Cheng 已提交
349 350
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
351
  }
H
Hongze Cheng 已提交
352 353
  **ppRow = *pRow;
  (*ppRow)->pData = NULL;
H
Hongze Cheng 已提交
354 355

  if (pRow->nData) {
H
Hongze Cheng 已提交
356 357 358 359 360 361
    (*ppRow)->pData = taosMemoryMalloc(pRow->nData);
    if ((*ppRow)->pData == NULL) {
      taosMemoryFree(*ppRow);
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
362 363 364
    memcpy((*ppRow)->pData, pRow->pData, pRow->nData);
  }

H
Hongze Cheng 已提交
365 366
_exit:
  return code;
H
Hongze Cheng 已提交
367 368 369
}

void tTSRowFree(STSRow2 *pRow) {
H
Hongze Cheng 已提交
370 371 372 373
  if (pRow) {
    if (pRow->pData) taosMemoryFree(pRow->pData);
    taosMemoryFree(pRow);
  }
H
Hongze Cheng 已提交
374 375
}

H
Hongze Cheng 已提交
376 377
void tTSRowGet(STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
  uint8_t   isTuple = (pRow->flags & 0xf0 == 0) ? 1 : 0;
H
Hongze Cheng 已提交
378
  STColumn *pTColumn = &pTSchema->columns[iCol];
H
Hongze Cheng 已提交
379 380
  uint8_t   flags = pRow->flags & (uint8_t)0xf;
  SValue    value;
H
Hongze Cheng 已提交
381

H
Hongze Cheng 已提交
382 383 384
  ASSERT(iCol < pTSchema->numOfCols);
  ASSERT(flags);
  ASSERT(pRow->sver == pTSchema->version);
H
Hongze Cheng 已提交
385

H
Hongze Cheng 已提交
386 387 388
  if (iCol == 0) {
    value.ts = pRow->ts;
    goto _return_value;
H
Hongze Cheng 已提交
389 390
  }

H
Hongze Cheng 已提交
391
  if (flags == TSROW_HAS_NONE) {
H
more  
Hongze Cheng 已提交
392
    goto _return_none;
H
Hongze Cheng 已提交
393
  } else if (flags == TSROW_HAS_NONE) {
H
more  
Hongze Cheng 已提交
394
    goto _return_null;
H
Hongze Cheng 已提交
395
  }
H
Hongze Cheng 已提交
396

H
Hongze Cheng 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410
  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 已提交
411
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
412 413 414
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
H
Hongze Cheng 已提交
415
        } else {
H
Hongze Cheng 已提交
416
          goto _return_null;
H
Hongze Cheng 已提交
417
        }
H
Hongze Cheng 已提交
418
      case TSROW_HAS_VAL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
419 420 421
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
H
Hongze Cheng 已提交
422
        } else {
H
Hongze Cheng 已提交
423 424
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
425
        }
H
Hongze Cheng 已提交
426
      case TSROW_HAS_VAL | TSROW_HAS_NULL:
H
Hongze Cheng 已提交
427 428 429
        b = GET_BIT1(pb, iCol - 1);
        if (b == 0) {
          goto _return_null;
H
Hongze Cheng 已提交
430
        } else {
H
Hongze Cheng 已提交
431 432
          pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
433
        }
H
Hongze Cheng 已提交
434
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
435 436 437 438 439
        b = GET_BIT2(pb, iCol - 1);
        if (b == 0) {
          goto _return_none;
        } else if (b == 1) {
          goto _return_null;
H
Hongze Cheng 已提交
440
        } else {
H
Hongze Cheng 已提交
441 442
          pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
          break;
H
Hongze Cheng 已提交
443
        }
H
Hongze Cheng 已提交
444
      default:
H
Hongze Cheng 已提交
445
        ASSERT(0);
H
Hongze Cheng 已提交
446
    }
H
Hongze Cheng 已提交
447

H
Hongze Cheng 已提交
448 449 450
    ASSERT(pf);

    p = pf + pTColumn->offset;
H
Hongze Cheng 已提交
451
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
H
Hongze Cheng 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      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 已提交
474
    } else {
H
Hongze Cheng 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
      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 已提交
504
    }
H
Hongze Cheng 已提交
505 506 507

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

H
Hongze Cheng 已提交
510
_return_none:
H
more  
Hongze Cheng 已提交
511
  *pColVal = COL_VAL_NONE(pTColumn->colId);
H
Hongze Cheng 已提交
512 513 514
  return;

_return_null:
H
more  
Hongze Cheng 已提交
515
  *pColVal = COL_VAL_NULL(pTColumn->colId);
H
Hongze Cheng 已提交
516 517 518
  return;

_return_value:
H
more  
Hongze Cheng 已提交
519
  *pColVal = COL_VAL_VALUE(pTColumn->colId, value);
H
Hongze Cheng 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  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 已提交
586 587 588
}

// STSchema
H
Hongze Cheng 已提交
589 590 591 592 593 594 595
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 已提交
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
  (*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 已提交
621 622 623
  return 0;
}

H
Hongze Cheng 已提交
624 625 626
void tTSchemaDestroy(STSchema *pTSchema) {
  if (pTSchema) taosMemoryFree(pTSchema);
}
H
Hongze Cheng 已提交
627

H
Hongze Cheng 已提交
628
// STSRowBuilder
H
Hongze Cheng 已提交
629
#if 0
H
Hongze Cheng 已提交
630
int32_t tTSRowBuilderInit(STSRowBuilder *pBuilder, int32_t sver, int32_t nCols, SSchema *pSchema) {
H
Hongze Cheng 已提交
631 632
  if (tTSchemaCreate(sver, pSchema, nCols, &pBuilder->pTSchema) < 0) return -1;

H
Hongze Cheng 已提交
633 634
  pBuilder->szBitMap1 = BIT1_SIZE(nCols - 1);
  pBuilder->szBitMap2 = BIT2_SIZE(nCols - 1);
H
Hongze Cheng 已提交
635 636
  pBuilder->szKVBuf =
      sizeof(STSKVRow) + sizeof(SKVIdx) * (nCols - 1) + pBuilder->pTSchema->flen + pBuilder->pTSchema->vlen;
H
Hongze Cheng 已提交
637 638 639
  pBuilder->szTPBuf = pBuilder->szBitMap2 + pBuilder->pTSchema->flen + pBuilder->pTSchema->vlen;
  pBuilder->pKVBuf = taosMemoryMalloc(pBuilder->szKVBuf);
  if (pBuilder->pKVBuf == NULL) {
H
Hongze Cheng 已提交
640 641
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    tTSchemaDestroy(pBuilder->pTSchema);
H
Hongze Cheng 已提交
642
    return -1;
H
Hongze Cheng 已提交
643
  }
H
Hongze Cheng 已提交
644 645 646
  pBuilder->pTPBuf = taosMemoryMalloc(pBuilder->szTPBuf);
  if (pBuilder->pTPBuf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
647 648
    taosMemoryFree(pBuilder->pKVBuf);
    tTSchemaDestroy(pBuilder->pTSchema);
H
Hongze Cheng 已提交
649
    return -1;
H
Hongze Cheng 已提交
650 651
  }

H
Hongze Cheng 已提交
652 653 654
  return 0;
}

H
Hongze Cheng 已提交
655
void tTSRowBuilderClear(STSRowBuilder *pBuilder) {
H
Hongze Cheng 已提交
656 657 658 659
  if (pBuilder->pTPBuf) {
    taosMemoryFree(pBuilder->pTPBuf);
    pBuilder->pTPBuf = NULL;
  }
H
Hongze Cheng 已提交
660 661 662 663 664 665
  if (pBuilder->pKVBuf) {
    taosMemoryFree(pBuilder->pKVBuf);
    pBuilder->pKVBuf = NULL;
  }
  tTSchemaDestroy(pBuilder->pTSchema);
  pBuilder->pTSchema = NULL;
H
Hongze Cheng 已提交
666 667
}

H
Hongze Cheng 已提交
668
void tTSRowBuilderReset(STSRowBuilder *pBuilder) {
H
Hongze Cheng 已提交
669
  for (int32_t iCol = pBuilder->pTSchema->numOfCols - 1; iCol >= 0; iCol--) {
H
Hongze Cheng 已提交
670 671
    STColumn *pTColumn = &pBuilder->pTSchema->columns[iCol];
    COL_CLR_SET(pTColumn->flags);
H
Hongze Cheng 已提交
672 673
  }

H
Hongze Cheng 已提交
674
  pBuilder->iCol = 0;
H
Hongze Cheng 已提交
675
  ((STSKVRow *)pBuilder->pKVBuf)->nCols = 0;
H
Hongze Cheng 已提交
676 677
  pBuilder->vlenKV = 0;
  pBuilder->vlenTP = 0;
H
Hongze Cheng 已提交
678
  pBuilder->row.flags = 0;
H
Hongze Cheng 已提交
679 680
}

H
Hongze Cheng 已提交
681
int32_t tTSRowBuilderPut(STSRowBuilder *pBuilder, int32_t cid, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
682 683 684
  STColumn *pTColumn = &pBuilder->pTSchema->columns[pBuilder->iCol];
  uint8_t  *p;
  int32_t   iCol;
H
Hongze Cheng 已提交
685
  STSKVRow *pTSKVRow = (STSKVRow *)pBuilder->pKVBuf;
H
Hongze Cheng 已提交
686

H
Hongze Cheng 已提交
687 688
  // use interp search
  if (pTColumn->colId < cid) {  // right search
H
Hongze Cheng 已提交
689 690
    for (iCol = pBuilder->iCol + 1; iCol < pBuilder->pTSchema->numOfCols; iCol++) {
      pTColumn = &pBuilder->pTSchema->columns[iCol];
H
Hongze Cheng 已提交
691
      if (pTColumn->colId >= cid) break;
H
Hongze Cheng 已提交
692
    }
H
Hongze Cheng 已提交
693
  } else if (pTColumn->colId > cid) {  // left search
H
Hongze Cheng 已提交
694 695
    for (iCol = pBuilder->iCol - 1; iCol >= 0; iCol--) {
      pTColumn = &pBuilder->pTSchema->columns[iCol];
H
Hongze Cheng 已提交
696
      if (pTColumn->colId <= cid) break;
H
Hongze Cheng 已提交
697
    }
H
Hongze Cheng 已提交
698 699
  }

H
Hongze Cheng 已提交
700
  if (pTColumn->colId != cid || COL_IS_SET(pTColumn->flags)) {
H
Hongze Cheng 已提交
701 702 703
    return -1;
  }

H
Hongze Cheng 已提交
704 705
  pBuilder->iCol = iCol;

H
Hongze Cheng 已提交
706 707
  // set value
  if (cid == 0) {
H
Hongze Cheng 已提交
708
    ASSERT(pData && nData == sizeof(TSKEY) && iCol == 0);
H
Hongze Cheng 已提交
709
    pBuilder->row.ts = *(TSKEY *)pData;
H
Hongze Cheng 已提交
710
    pTColumn->flags |= COL_SET_VAL;
H
Hongze Cheng 已提交
711
  } else {
H
Hongze Cheng 已提交
712 713
    if (pData) {
      // set VAL
H
Hongze Cheng 已提交
714

H
Hongze Cheng 已提交
715 716 717 718 719
      pBuilder->row.flags |= TSROW_HAS_VAL;
      pTColumn->flags |= COL_SET_VAL;

      /* KV */
      if (1) {  // avoid KV at some threshold (todo)
H
Hongze Cheng 已提交
720 721
        pTSKVRow->idx[pTSKVRow->nCols].cid = cid;
        pTSKVRow->idx[pTSKVRow->nCols].offset = pBuilder->vlenKV;
H
Hongze Cheng 已提交
722

H
Hongze Cheng 已提交
723 724
        p = pBuilder->pKVBuf + sizeof(STSKVRow) + sizeof(SKVIdx) * (pBuilder->pTSchema->numOfCols - 1) +
            pBuilder->vlenKV;
H
Hongze Cheng 已提交
725 726 727 728 729 730 731 732
        if (IS_VAR_DATA_TYPE(pTColumn->type)) {
          ASSERT(nData <= pTColumn->bytes);
          pBuilder->vlenKV += tPutBinary(p, pData, nData);
        } else {
          ASSERT(nData == pTColumn->bytes);
          memcpy(p, pData, nData);
          pBuilder->vlenKV += nData;
        }
H
Hongze Cheng 已提交
733 734
      }

H
Hongze Cheng 已提交
735 736 737 738 739
      /* TUPLE */
      p = pBuilder->pTPBuf + pBuilder->szBitMap2 + pTColumn->offset;
      if (IS_VAR_DATA_TYPE(pTColumn->type)) {
        ASSERT(nData <= pTColumn->bytes);
        *(int32_t *)p = pBuilder->vlenTP;
H
Hongze Cheng 已提交
740

H
Hongze Cheng 已提交
741
        p = pBuilder->pTPBuf + pBuilder->szBitMap2 + pBuilder->pTSchema->flen + pBuilder->vlenTP;
H
Hongze Cheng 已提交
742
        pBuilder->vlenTP += tPutBinary(p, pData, nData);
H
Hongze Cheng 已提交
743
      } else {
H
Hongze Cheng 已提交
744
        ASSERT(nData == pTColumn->bytes);
H
Hongze Cheng 已提交
745 746
        memcpy(p, pData, nData);
      }
H
Hongze Cheng 已提交
747 748 749
    } else {
      // set NULL

H
Hongze Cheng 已提交
750
      pBuilder->row.flags |= TSROW_HAS_NULL;
H
Hongze Cheng 已提交
751
      pTColumn->flags |= COL_SET_NULL;
H
Hongze Cheng 已提交
752

H
Hongze Cheng 已提交
753 754
      pTSKVRow->idx[pTSKVRow->nCols].cid = cid;
      pTSKVRow->idx[pTSKVRow->nCols].offset = -1;
H
Hongze Cheng 已提交
755
    }
H
Hongze Cheng 已提交
756

H
Hongze Cheng 已提交
757
    pTSKVRow->nCols++;
H
Hongze Cheng 已提交
758 759 760 761 762
  }

  return 0;
}

H
Hongze Cheng 已提交
763 764 765 766 767 768 769 770 771 772
static FORCE_INLINE int tSKVIdxCmprFn(const void *p1, const void *p2) {
  SKVIdx *pKVIdx1 = (SKVIdx *)p1;
  SKVIdx *pKVIdx2 = (SKVIdx *)p2;
  if (pKVIdx1->cid > pKVIdx2->cid) {
    return 1;
  } else if (pKVIdx1->cid < pKVIdx2->cid) {
    return -1;
  }
  return 0;
}
H
Hongze Cheng 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
static void setBitMap(uint8_t *p, STSchema *pTSchema, uint8_t flags) {
  int32_t   bidx;
  STColumn *pTColumn;

  for (int32_t iCol = 1; iCol < pTSchema->numOfCols; iCol++) {
    pTColumn = &pTSchema->columns[iCol];
    bidx = iCol - 1;

    switch (flags) {
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
        if (pTColumn->flags & COL_SET_NULL) {
          SET_BIT1(p, bidx, (uint8_t)1);
        } else {
          SET_BIT1(p, bidx, (uint8_t)0);
        }
        break;
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
        if (pTColumn->flags & COL_SET_NULL) {
          SET_BIT2(p, bidx, (uint8_t)1);
H
Hongze Cheng 已提交
792
        } else if (pTColumn->flags & COL_SET_VAL) {
H
Hongze Cheng 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
          SET_BIT2(p, bidx, (uint8_t)2);
        } else {
          SET_BIT2(p, bidx, (uint8_t)0);
        }
        break;
      default:
        if (pTColumn->flags & COL_SET_VAL) {
          SET_BIT1(p, bidx, (uint8_t)1);
        } else {
          SET_BIT1(p, bidx, (uint8_t)0);
        }

        break;
    }
  }
}
H
Hongze Cheng 已提交
809
int32_t tTSRowBuilderGetRow(STSRowBuilder *pBuilder, const STSRow2 **ppRow) {
H
Hongze Cheng 已提交
810 811 812
  int32_t   nDataTP, nDataKV;
  uint32_t  flags;
  STSKVRow *pTSKVRow = (STSKVRow *)pBuilder->pKVBuf;
H
Hongze Cheng 已提交
813
  int32_t   nCols = pBuilder->pTSchema->numOfCols;
H
Hongze Cheng 已提交
814 815

  // error not set ts
H
Hongze Cheng 已提交
816
  if (!COL_IS_SET(pBuilder->pTSchema->columns->flags)) {
H
Hongze Cheng 已提交
817 818 819
    return -1;
  }

H
Hongze Cheng 已提交
820 821
  ASSERT(pTSKVRow->nCols < nCols);
  if (pTSKVRow->nCols < nCols - 1) {
H
Hongze Cheng 已提交
822
    pBuilder->row.flags |= TSROW_HAS_NONE;
H
Hongze Cheng 已提交
823
  }
H
Hongze Cheng 已提交
824

H
Hongze Cheng 已提交
825 826 827 828 829
  ASSERT(pBuilder->row.flags & 0xf != 0);
  *(ppRow) = &pBuilder->row;
  switch (pBuilder->row.flags & 0xf) {
    case TSROW_HAS_NONE:
    case TSROW_HAS_NULL:
H
Hongze Cheng 已提交
830 831
      pBuilder->row.nData = 0;
      pBuilder->row.pData = NULL;
H
Hongze Cheng 已提交
832 833
      return 0;
    case TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
834
      nDataTP = pBuilder->szBitMap1;
H
Hongze Cheng 已提交
835
      break;
H
Hongze Cheng 已提交
836
    case TSROW_HAS_VAL:
H
Hongze Cheng 已提交
837
      nDataTP = pBuilder->pTSchema->flen + pBuilder->vlenTP;
H
Hongze Cheng 已提交
838
      break;
H
Hongze Cheng 已提交
839 840
    case TSROW_HAS_VAL | TSROW_HAS_NONE:
    case TSROW_HAS_VAL | TSROW_HAS_NULL:
H
Hongze Cheng 已提交
841
      nDataTP = pBuilder->szBitMap1 + pBuilder->pTSchema->flen + pBuilder->vlenTP;
H
Hongze Cheng 已提交
842
      break;
H
Hongze Cheng 已提交
843
    case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
844
      nDataTP = pBuilder->szBitMap2 + pBuilder->pTSchema->flen + pBuilder->vlenTP;
H
Hongze Cheng 已提交
845 846
      break;
    default:
H
Hongze Cheng 已提交
847
      ASSERT(0);
H
Hongze Cheng 已提交
848 849
  }

H
Hongze Cheng 已提交
850 851
  nDataKV = sizeof(STSKVRow) + sizeof(SKVIdx) * pTSKVRow->nCols + pBuilder->vlenKV;
  pBuilder->row.sver = pBuilder->pTSchema->version;
H
Hongze Cheng 已提交
852 853 854
  if (nDataKV < nDataTP) {
    // generate KV row

H
Hongze Cheng 已提交
855 856
    ASSERT(pBuilder->row.flags & 0xf != TSROW_HAS_VAL);

H
Hongze Cheng 已提交
857
    pBuilder->row.flags |= TSROW_KV_ROW;
H
Hongze Cheng 已提交
858
    pBuilder->row.nData = nDataKV;
H
Hongze Cheng 已提交
859
    pBuilder->row.pData = pBuilder->pKVBuf;
H
Hongze Cheng 已提交
860

H
Hongze Cheng 已提交
861
    qsort(pTSKVRow->idx, pTSKVRow->nCols, sizeof(SKVIdx), tSKVIdxCmprFn);
H
Hongze Cheng 已提交
862 863
    if (pTSKVRow->nCols < nCols - 1) {
      memmove(&pTSKVRow->idx[pTSKVRow->nCols], &pTSKVRow->idx[nCols - 1], pBuilder->vlenKV);
H
Hongze Cheng 已提交
864 865
    }
  } else {
H
Hongze Cheng 已提交
866 867 868 869
    // generate TUPLE row

    pBuilder->row.nData = nDataTP;

H
Hongze Cheng 已提交
870 871
    uint8_t *p;
    uint8_t  flags = pBuilder->row.flags & 0xf;
H
Hongze Cheng 已提交
872

H
Hongze Cheng 已提交
873 874
    if (flags == TSROW_HAS_VAL) {
      pBuilder->row.pData = pBuilder->pTPBuf + pBuilder->szBitMap2;
H
Hongze Cheng 已提交
875
    } else {
H
Hongze Cheng 已提交
876 877
      if (flags == TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE) {
        pBuilder->row.pData = pBuilder->pTPBuf;
H
Hongze Cheng 已提交
878
      } else {
H
Hongze Cheng 已提交
879
        pBuilder->row.pData = pBuilder->pTPBuf + pBuilder->szBitMap2 - pBuilder->szBitMap1;
H
Hongze Cheng 已提交
880 881
      }

H
Hongze Cheng 已提交
882
      setBitMap(pBuilder->row.pData, pBuilder->pTSchema, flags);
H
Hongze Cheng 已提交
883
    }
H
Hongze Cheng 已提交
884 885 886 887
  }

  return 0;
}
H
Hongze Cheng 已提交
888
#endif
H
Hongze Cheng 已提交
889

H
Hongze Cheng 已提交
890 891
static int tTagValCmprFn(const void *p1, const void *p2) {
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
892
    return -1;
H
Hongze Cheng 已提交
893
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
H
Hongze Cheng 已提交
894 895
    return 1;
  }
H
Hongze Cheng 已提交
896

H
Hongze Cheng 已提交
897 898
  return 0;
}
H
Hongze Cheng 已提交
899 900 901
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
}
C
Cary Xu 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

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};
      memcpy(tmpVal, val, 32);
      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;
    default:
      ASSERT(0);
      break;
  }
}

H
Hongze Cheng 已提交
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
// if (isLarge) {
//   p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
// } else {
//   p = (uint8_t *)&pTag->idx[pTag->nTag];
// }

// (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
// if (*ppArray == NULL) {
//   code = TSDB_CODE_OUT_OF_MEMORY;
//   goto _err;
// }

// for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
//   if (isLarge) {
//     offset = ((int16_t *)pTag->idx)[iTag];
//   } else {
//     offset = pTag->idx[iTag];
//   }
972

C
Cary Xu 已提交
973
void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
H
Hongze Cheng 已提交
974 975
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
976 977 978 979 980 981 982 983 984 985
  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 已提交
986
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
987 988 989 990 991 992 993 994
    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 已提交
995
    } else {
996
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
C
Cary Xu 已提交
997
    }
998
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
C
Cary Xu 已提交
999
    tGetTagVal(p + offset, &tagVal, isJson);
C
Cary Xu 已提交
1000 1001 1002 1003 1004
    debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
  }
  printf("\n");
}

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

H
Hongze Cheng 已提交
1008 1009 1010 1011 1012 1013
  // key
  if (isJson) {
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
  } else {
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
  }
H
Hongze Cheng 已提交
1014

H
Hongze Cheng 已提交
1015 1016 1017 1018 1019 1020 1021
  // 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 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    p = p ? p + n : p;
    switch (pTagVal->type) {
      case TSDB_DATA_TYPE_BOOL:
        n += tPutI8(p, pTagVal->i8 ? 1 : 0);
        break;
      case TSDB_DATA_TYPE_TINYINT:
        n += tPutI8(p, pTagVal->i8);
        break;
      case TSDB_DATA_TYPE_SMALLINT:
        n += tPutI16(p, pTagVal->i16);
        break;
      case TSDB_DATA_TYPE_INT:
        n += tPutI32(p, pTagVal->i32);
        break;
      case TSDB_DATA_TYPE_TIMESTAMP:
      case TSDB_DATA_TYPE_BIGINT:
        n += tPutI64(p, pTagVal->i64);
        break;
      case TSDB_DATA_TYPE_FLOAT:
        n += tPutFloat(p, pTagVal->f);
        break;
      case TSDB_DATA_TYPE_DOUBLE:
        n += tPutDouble(p, pTagVal->d);
        break;
      case TSDB_DATA_TYPE_UTINYINT:
        n += tPutU8(p, pTagVal->u8);
        break;
      case TSDB_DATA_TYPE_USMALLINT:
        n += tPutU16(p, pTagVal->u16);
        break;
      case TSDB_DATA_TYPE_UINT:
        n += tPutU32(p, pTagVal->u32);
        break;
      case TSDB_DATA_TYPE_UBIGINT:
        n += tPutU64(p, pTagVal->u64);
        break;
      default:
        ASSERT(0);
    }
H
Hongze Cheng 已提交
1061 1062
  }

H
Hongze Cheng 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
  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 已提交
1073 1074
  }

H
Hongze Cheng 已提交
1075 1076
  // type
  n += tGetI8(p + n, &pTagVal->type);
H
Hongze Cheng 已提交
1077

H
Hongze Cheng 已提交
1078 1079 1080 1081
  // value
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
  } else {
H
Hongze Cheng 已提交
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    switch (pTagVal->type) {
      case TSDB_DATA_TYPE_BOOL:
      case TSDB_DATA_TYPE_TINYINT:
        n += tGetI8(p + n, &pTagVal->i8);
        break;
      case TSDB_DATA_TYPE_SMALLINT:
        n += tGetI16(p, &pTagVal->i16);
        break;
      case TSDB_DATA_TYPE_INT:
        n += tGetI32(p, &pTagVal->i32);
        break;
      case TSDB_DATA_TYPE_TIMESTAMP:
      case TSDB_DATA_TYPE_BIGINT:
        n += tGetI64(p, &pTagVal->i64);
        break;
      case TSDB_DATA_TYPE_FLOAT:
        n += tGetFloat(p, &pTagVal->f);
        break;
      case TSDB_DATA_TYPE_DOUBLE:
        n += tGetDouble(p, &pTagVal->d);
        break;
      case TSDB_DATA_TYPE_UTINYINT:
        n += tGetU8(p, &pTagVal->u8);
        break;
      case TSDB_DATA_TYPE_USMALLINT:
        n += tGetU16(p, &pTagVal->u16);
        break;
      case TSDB_DATA_TYPE_UINT:
        n += tGetU32(p, &pTagVal->u32);
        break;
      case TSDB_DATA_TYPE_UBIGINT:
        n += tGetU64(p, &pTagVal->u64);
        break;
      default:
        ASSERT(0);
    }
H
Hongze Cheng 已提交
1118 1119 1120 1121
  }

  return n;
}
H
Hongze Cheng 已提交
1122
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
H
Hongze Cheng 已提交
1123 1124 1125
  int32_t  code = 0;
  uint8_t *p = NULL;
  int16_t  n = 0;
H
Hongze Cheng 已提交
1126
  int16_t  nTag = taosArrayGetSize(pArray);
H
Hongze Cheng 已提交
1127 1128
  int32_t  szTag = 0;
  int8_t   isLarge = 0;
H
Hongze Cheng 已提交
1129 1130 1131

  // sort
  if (isJson) {
H
Hongze Cheng 已提交
1132
    qsort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
H
Hongze Cheng 已提交
1133
  } else {
H
Hongze Cheng 已提交
1134
    qsort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
H
Hongze Cheng 已提交
1135 1136 1137
  }

  // get size
H
Hongze Cheng 已提交
1138
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
1139
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
1140
  }
H
Hongze Cheng 已提交
1141 1142 1143 1144 1145 1146
  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 已提交
1147

H
Hongze Cheng 已提交
1148
  ASSERT(szTag <= INT16_MAX);
H
Hongze Cheng 已提交
1149 1150

  // build tag
C
Cary Xu 已提交
1151
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
H
Hongze Cheng 已提交
1152 1153 1154 1155
  if ((*ppTag) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
1156 1157 1158 1159 1160 1161 1162
  (*ppTag)->flags = 0;
  if (isJson) {
    (*ppTag)->flags |= TD_TAG_JSON;
  }
  if (isLarge) {
    (*ppTag)->flags |= TD_TAG_LARGE;
  }
H
Hongze Cheng 已提交
1163 1164 1165
  (*ppTag)->len = szTag;
  (*ppTag)->nTag = nTag;
  (*ppTag)->ver = version;
H
Hongze Cheng 已提交
1166

H
Hongze Cheng 已提交
1167 1168 1169 1170 1171
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
  } else {
    p = (uint8_t *)&(*ppTag)->idx[nTag];
  }
H
Hongze Cheng 已提交
1172 1173
  n = 0;
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
H
Hongze Cheng 已提交
1174 1175 1176 1177 1178
    if (isLarge) {
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
    } else {
      (*ppTag)->idx[iTag] = n;
    }
H
Hongze Cheng 已提交
1179
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
H
Hongze Cheng 已提交
1180 1181
  }

C
Cary Xu 已提交
1182 1183
  debugPrintSTag(*ppTag, __func__, __LINE__);

H
Hongze Cheng 已提交
1184 1185 1186 1187
  return code;

_err:
  return code;
H
Hongze Cheng 已提交
1188 1189 1190 1191 1192 1193
}

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

C
Cary Xu 已提交
1194
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
H
Hongze Cheng 已提交
1195 1196 1197
  int16_t  lidx = 0;
  int16_t  ridx = pTag->nTag - 1;
  int16_t  midx;
H
Hongze Cheng 已提交
1198 1199 1200 1201
  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 已提交
1202 1203 1204
  STagVal  tv;
  int      c;

H
Hongze Cheng 已提交
1205 1206 1207 1208 1209 1210
  if (isLarge) {
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
  } else {
    p = (uint8_t *)&pTag->idx[pTag->nTag];
  }

H
Hongze Cheng 已提交
1211 1212 1213 1214 1215
  pTagVal->type = TSDB_DATA_TYPE_NULL;
  pTagVal->pData = NULL;
  pTagVal->nData = 0;
  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;
H
Hongze Cheng 已提交
1216 1217 1218 1219 1220
    if (isLarge) {
      offset = ((int16_t *)pTag->idx)[midx];
    } else {
      offset = pTag->idx[midx];
    }
H
Hongze Cheng 已提交
1221

H
Hongze Cheng 已提交
1222 1223
    tGetTagVal(p + offset, &tv, isJson);
    if (isJson) {
H
Hongze Cheng 已提交
1224
      c = tTagValJsonCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1225
    } else {
H
Hongze Cheng 已提交
1226
      c = tTagValCmprFn(pTagVal, &tv);
H
Hongze Cheng 已提交
1227 1228
    }

H
Hongze Cheng 已提交
1229 1230 1231 1232
    if (c < 0) {
      ridx = midx - 1;
    } else if (c > 0) {
      lidx = midx + 1;
H
Hongze Cheng 已提交
1233
    } else {
H
Hongze Cheng 已提交
1234
      memcpy(pTagVal, &tv, sizeof(tv));
C
Cary Xu 已提交
1235
      return true;
H
Hongze Cheng 已提交
1236 1237
    }
  }
C
Cary Xu 已提交
1238
  return false;
H
Hongze Cheng 已提交
1239 1240
}

H
more  
Hongze Cheng 已提交
1241 1242
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
H
Hongze Cheng 已提交
1243 1244
}

C
Cary Xu 已提交
1245 1246 1247 1248
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) {
  uint32_t len = 0;
  return tDecodeBinary(pDecoder, (uint8_t **)ppTag, &len);
}
H
Hongze Cheng 已提交
1249

C
Cary Xu 已提交
1250
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
H
Hongze Cheng 已提交
1251
  int32_t  code = 0;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
  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 已提交
1262

H
more  
Hongze Cheng 已提交
1263
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
H
Hongze Cheng 已提交
1264
  if (*ppArray == NULL) {
H
Hongze Cheng 已提交
1265 1266 1267 1268 1269
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
1270 1271 1272 1273 1274 1275
    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 已提交
1276
    taosArrayPush(*ppArray, &tv);
H
Hongze Cheng 已提交
1277 1278 1279 1280 1281 1282 1283 1284
  }

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
1285
#if 1  // ===================================================================================================================
1286
static void dataColSetNEleNull(SDataCol *pCol, int nEle);
H
Hongze Cheng 已提交
1287
int         tdAllocMemForCol(SDataCol *pCol, int maxPoints) {
L
Liu Jicong 已提交
1288
  int spaceNeeded = pCol->bytes * maxPoints;
S
Shengliang Guan 已提交
1289
  if (IS_VAR_DATA_TYPE(pCol->type)) {
L
Liu Jicong 已提交
1290
    spaceNeeded += sizeof(VarDataOffsetT) * maxPoints;
L
Liu Jicong 已提交
1291
  }
C
Cary Xu 已提交
1292
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
1293 1294 1295 1296
  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 已提交
1297 1298
  // 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 已提交
1299
#endif
C
Cary Xu 已提交
1300

S
Shengliang Guan 已提交
1301
  if (pCol->spaceSize < spaceNeeded) {
wafwerar's avatar
wafwerar 已提交
1302
    void *ptr = taosMemoryRealloc(pCol->pData, spaceNeeded);
S
Shengliang Guan 已提交
1303 1304
    if (ptr == NULL) {
      uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)spaceNeeded, strerror(errno));
L
Liu Jicong 已提交
1305
      return -1;
L
Liu Jicong 已提交
1306 1307 1308
    } else {
      pCol->pData = ptr;
      pCol->spaceSize = spaceNeeded;
1309 1310
    }
  }
C
Cary Xu 已提交
1311
#ifdef TD_SUPPORT_BITMAP
1312

C
Cary Xu 已提交
1313 1314 1315 1316
  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 已提交
1317
    pCol->pBitmap = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
L
Liu Jicong 已提交
1318
  }
C
Cary Xu 已提交
1319 1320 1321 1322
#else
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->dataOff = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
  }
C
Cary Xu 已提交
1323
#endif
L
Liu Jicong 已提交
1324
  return 0;
1325 1326
}

H
hzcheng 已提交
1327 1328 1329
/**
 * Duplicate the schema and return a new object
 */
H
Hongze Cheng 已提交
1330
STSchema *tdDupSchema(const STSchema *pSchema) {
S
Shengliang Guan 已提交
1331
  int       tlen = sizeof(STSchema) + sizeof(STColumn) * schemaNCols(pSchema);
wafwerar's avatar
wafwerar 已提交
1332
  STSchema *tSchema = (STSchema *)taosMemoryMalloc(tlen);
H
hzcheng 已提交
1333 1334
  if (tSchema == NULL) return NULL;

H
Hongze Cheng 已提交
1335
  memcpy((void *)tSchema, (void *)pSchema, tlen);
H
hzcheng 已提交
1336 1337 1338 1339

  return tSchema;
}

H
TD-27  
hzcheng 已提交
1340 1341 1342
/**
 * Encode a schema to dst, and return the next pointer
 */
H
TD-353  
Hongze Cheng 已提交
1343 1344 1345 1346
int tdEncodeSchema(void **buf, STSchema *pSchema) {
  int tlen = 0;
  tlen += taosEncodeFixedI32(buf, schemaVersion(pSchema));
  tlen += taosEncodeFixedI32(buf, schemaNCols(pSchema));
H
TD-166  
hzcheng 已提交
1347

H
TD-27  
hzcheng 已提交
1348 1349
  for (int i = 0; i < schemaNCols(pSchema); i++) {
    STColumn *pCol = schemaColAt(pSchema, i);
H
TD-353  
Hongze Cheng 已提交
1350
    tlen += taosEncodeFixedI8(buf, colType(pCol));
C
Cary Xu 已提交
1351
    tlen += taosEncodeFixedI8(buf, colFlags(pCol));
H
TD-353  
Hongze Cheng 已提交
1352
    tlen += taosEncodeFixedI16(buf, colColId(pCol));
1353
    tlen += taosEncodeFixedI16(buf, colBytes(pCol));
H
TD-27  
hzcheng 已提交
1354 1355
  }

H
TD-353  
Hongze Cheng 已提交
1356
  return tlen;
H
TD-27  
hzcheng 已提交
1357 1358 1359 1360 1361
}

/**
 * Decode a schema from a binary.
 */
H
TD-353  
Hongze Cheng 已提交
1362
void *tdDecodeSchema(void *buf, STSchema **pRSchema) {
S
Shengliang Guan 已提交
1363 1364
  int             version = 0;
  int             numOfCols = 0;
H
TD-353  
Hongze Cheng 已提交
1365
  STSchemaBuilder schemaBuilder;
H
TD-27  
hzcheng 已提交
1366

H
TD-353  
Hongze Cheng 已提交
1367 1368
  buf = taosDecodeFixedI32(buf, &version);
  buf = taosDecodeFixedI32(buf, &numOfCols);
H
TD-27  
hzcheng 已提交
1369

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

H
TD-353  
Hongze Cheng 已提交
1372
  for (int i = 0; i < numOfCols; i++) {
1373
    col_type_t  type = 0;
C
Cary Xu 已提交
1374
    int8_t      flags = 0;
1375 1376
    col_id_t    colId = 0;
    col_bytes_t bytes = 0;
H
TD-353  
Hongze Cheng 已提交
1377
    buf = taosDecodeFixedI8(buf, &type);
C
Cary Xu 已提交
1378
    buf = taosDecodeFixedI8(buf, &flags);
H
TD-353  
Hongze Cheng 已提交
1379
    buf = taosDecodeFixedI16(buf, &colId);
1380
    buf = taosDecodeFixedI32(buf, &bytes);
C
Cary Xu 已提交
1381
    if (tdAddColToSchema(&schemaBuilder, type, flags, colId, bytes) < 0) {
H
Hongze Cheng 已提交
1382 1383 1384
      tdDestroyTSchemaBuilder(&schemaBuilder);
      return NULL;
    }
H
TD-27  
hzcheng 已提交
1385 1386
  }

H
TD-353  
Hongze Cheng 已提交
1387
  *pRSchema = tdGetSchemaFromBuilder(&schemaBuilder);
H
Hongze Cheng 已提交
1388
  tdDestroyTSchemaBuilder(&schemaBuilder);
H
TD-353  
Hongze Cheng 已提交
1389
  return buf;
H
Hongze Cheng 已提交
1390 1391
}

C
Cary Xu 已提交
1392
int tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
1393 1394 1395
  if (pBuilder == NULL) return -1;

  pBuilder->tCols = 256;
wafwerar's avatar
wafwerar 已提交
1396
  pBuilder->columns = (STColumn *)taosMemoryMalloc(sizeof(STColumn) * pBuilder->tCols);
H
Hongze Cheng 已提交
1397 1398 1399 1400 1401 1402 1403 1404
  if (pBuilder->columns == NULL) return -1;

  tdResetTSchemaBuilder(pBuilder, version);
  return 0;
}

void tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder) {
  if (pBuilder) {
wafwerar's avatar
wafwerar 已提交
1405
    taosMemoryFreeClear(pBuilder->columns);
H
Hongze Cheng 已提交
1406 1407 1408
  }
}

C
Cary Xu 已提交
1409
void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
1410 1411 1412
  pBuilder->nCols = 0;
  pBuilder->tlen = 0;
  pBuilder->flen = 0;
T
Tao Liu 已提交
1413
  pBuilder->vlen = 0;
H
Hongze Cheng 已提交
1414 1415 1416
  pBuilder->version = version;
}

C
Cary Xu 已提交
1417
int32_t tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes) {
1418
  if (!isValidDataType(type)) return -1;
H
Hongze Cheng 已提交
1419 1420 1421

  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
wafwerar's avatar
wafwerar 已提交
1422
    STColumn *columns = (STColumn *)taosMemoryRealloc(pBuilder->columns, sizeof(STColumn) * pBuilder->tCols);
T
tickduan 已提交
1423 1424
    if (columns == NULL) return -1;
    pBuilder->columns = columns;
H
Hongze Cheng 已提交
1425 1426 1427 1428 1429
  }

  STColumn *pCol = &(pBuilder->columns[pBuilder->nCols]);
  colSetType(pCol, type);
  colSetColId(pCol, colId);
C
Cary Xu 已提交
1430
  colSetFlags(pCol, flags);
H
Hongze Cheng 已提交
1431 1432 1433
  if (pBuilder->nCols == 0) {
    colSetOffset(pCol, 0);
  } else {
S
Shengliang Guan 已提交
1434
    STColumn *pTCol = &(pBuilder->columns[pBuilder->nCols - 1]);
H
Hongze Cheng 已提交
1435 1436 1437 1438 1439
    colSetOffset(pCol, pTCol->offset + TYPE_BYTES[pTCol->type]);
  }

  if (IS_VAR_DATA_TYPE(type)) {
    colSetBytes(pCol, bytes);
T
Tao Liu 已提交
1440 1441
    pBuilder->tlen += (TYPE_BYTES[type] + bytes);
    pBuilder->vlen += bytes - sizeof(VarDataLenT);
H
Hongze Cheng 已提交
1442 1443 1444
  } else {
    colSetBytes(pCol, TYPE_BYTES[type]);
    pBuilder->tlen += TYPE_BYTES[type];
T
Tao Liu 已提交
1445
    pBuilder->vlen += TYPE_BYTES[type];
H
Hongze Cheng 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
  }

  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 已提交
1461
  STSchema *pSchema = (STSchema *)taosMemoryMalloc(tlen);
H
Hongze Cheng 已提交
1462 1463 1464 1465 1466 1467
  if (pSchema == NULL) return NULL;

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

C
Cary Xu 已提交
1470
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
1471
  schemaTLen(pSchema) += (int)TD_BITMAP_BYTES(schemaNCols(pSchema));
C
Cary Xu 已提交
1472 1473
#endif

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

H
TD-27  
hzcheng 已提交
1476 1477 1478
  return pSchema;
}

1479
void dataColInit(SDataCol *pDataCol, STColumn *pCol, int maxPoints) {
H
TD-166  
hzcheng 已提交
1480 1481 1482
  pDataCol->type = colType(pCol);
  pDataCol->colId = colColId(pCol);
  pDataCol->bytes = colBytes(pCol);
S
Shengliang Guan 已提交
1483
  pDataCol->offset = colOffset(pCol) + 0;  // TD_DATA_ROW_HEAD_SIZE;
H
TD-166  
hzcheng 已提交
1484 1485 1486

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

L
Liu Jicong 已提交
1488 1489 1490 1491 1492 1493
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 已提交
1494 1495
}

H
TD-166  
hzcheng 已提交
1496
bool isNEleNull(SDataCol *pCol, int nEle) {
S
Shengliang Guan 已提交
1497
  if (isAllRowsNull(pCol)) return true;
1498
  for (int i = 0; i < nEle; ++i) {
L
Liu Jicong 已提交
1499
    if (!isNull(tdGetColDataOfRowUnsafe(pCol, i), pCol->type)) return false;
H
TD-166  
hzcheng 已提交
1500
  }
H
Hongze Cheng 已提交
1501
  return true;
H
TD-166  
hzcheng 已提交
1502 1503
}

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

H
Hongze Cheng 已提交
1507
  void *tptr = pCol->pData;
H
TD-166  
hzcheng 已提交
1508
  // char *tptr = (char *)(pCol->pData);
H
TD-166  
hzcheng 已提交
1509

H
TD-166  
hzcheng 已提交
1510
  VarDataOffsetT offset = 0;
1511
  for (int i = 0; i < nEle; ++i) {
H
TD-166  
hzcheng 已提交
1512
    pCol->dataOff[i] = offset;
H
TD-166  
hzcheng 已提交
1513
    offset += varDataTLen(tptr);
H
hzcheng 已提交
1514
    tptr = POINTER_SHIFT(tptr, varDataTLen(tptr));
H
TD-166  
hzcheng 已提交
1515
  }
C
Cary Xu 已提交
1516
  return POINTER_SHIFT(tptr, varDataTLen(tptr));
H
TD-166  
hzcheng 已提交
1517 1518
}

L
Liu Jicong 已提交
1519
SDataCols *tdNewDataCols(int maxCols, int maxRows) {
wafwerar's avatar
wafwerar 已提交
1520
  SDataCols *pCols = (SDataCols *)taosMemoryCalloc(1, sizeof(SDataCols));
H
Haojun Liao 已提交
1521
  if (pCols == NULL) {
S
Shengliang Guan 已提交
1522
    uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)sizeof(SDataCols), strerror(errno));
H
Haojun Liao 已提交
1523 1524
    return NULL;
  }
H
TD-34  
hzcheng 已提交
1525

H
Hongze Cheng 已提交
1526
  pCols->maxPoints = maxRows;
L
Liu Jicong 已提交
1527 1528 1529
  pCols->maxCols = maxCols;
  pCols->numOfRows = 0;
  pCols->numOfCols = 0;
1530
  pCols->bitmapMode = TSDB_BITMODE_DEFAULT;
H
Hongze Cheng 已提交
1531 1532

  if (maxCols > 0) {
wafwerar's avatar
wafwerar 已提交
1533
    pCols->cols = (SDataCol *)taosMemoryCalloc(maxCols, sizeof(SDataCol));
H
Hongze Cheng 已提交
1534 1535 1536 1537 1538 1539
    if (pCols->cols == NULL) {
      uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)sizeof(SDataCol) * maxCols,
             strerror(errno));
      tdFreeDataCols(pCols);
      return NULL;
    }
1540
#if 0  // no need as calloc used
L
Liu Jicong 已提交
1541
    int i;
S
Shengliang Guan 已提交
1542
    for (i = 0; i < maxCols; i++) {
L
Liu Jicong 已提交
1543
      pCols->cols[i].spaceSize = 0;
L
Liu Jicong 已提交
1544
      pCols->cols[i].len = 0;
L
Liu Jicong 已提交
1545 1546 1547
      pCols->cols[i].pData = NULL;
      pCols->cols[i].dataOff = NULL;
    }
1548
#endif
H
Hongze Cheng 已提交
1549 1550
  }

H
TD-34  
hzcheng 已提交
1551 1552 1553
  return pCols;
}

H
Hongze Cheng 已提交
1554
int tdInitDataCols(SDataCols *pCols, STSchema *pSchema) {
1555 1556
  int i;
  int oldMaxCols = pCols->maxCols;
L
Liu Jicong 已提交
1557
  if (schemaNCols(pSchema) > oldMaxCols) {
H
Hongze Cheng 已提交
1558
    pCols->maxCols = schemaNCols(pSchema);
wafwerar's avatar
wafwerar 已提交
1559
    void *ptr = (SDataCol *)taosMemoryRealloc(pCols->cols, sizeof(SDataCol) * pCols->maxCols);
L
Liu Jicong 已提交
1560 1561
    if (ptr == NULL) return -1;
    pCols->cols = ptr;
1562
    for (i = oldMaxCols; i < pCols->maxCols; ++i) {
1563 1564
      pCols->cols[i].pData = NULL;
      pCols->cols[i].dataOff = NULL;
1565
      pCols->cols[i].pBitmap = NULL;
L
Liu Jicong 已提交
1566
      pCols->cols[i].spaceSize = 0;
1567
    }
L
Liu Jicong 已提交
1568
  }
1569 1570 1571
#if 0
  tdResetDataCols(pCols); // redundant loop to reset len/blen to 0, already reset in following dataColInit(...)
#endif
H
Hongze Cheng 已提交
1572

1573
  pCols->numOfRows = 0;
1574
  pCols->bitmapMode = TSDB_BITMODE_DEFAULT;
H
TD-34  
hzcheng 已提交
1575 1576
  pCols->numOfCols = schemaNCols(pSchema);

1577
  for (i = 0; i < schemaNCols(pSchema); ++i) {
1578
    dataColInit(pCols->cols + i, schemaColAt(pSchema, i), pCols->maxPoints);
H
TD-34  
hzcheng 已提交
1579
  }
S
Shengliang Guan 已提交
1580

H
Hongze Cheng 已提交
1581
  return 0;
H
TD-34  
hzcheng 已提交
1582 1583
}

H
Hongze Cheng 已提交
1584
SDataCols *tdFreeDataCols(SDataCols *pCols) {
1585
  int i;
H
TD-34  
hzcheng 已提交
1586
  if (pCols) {
S
Shengliang Guan 已提交
1587
    if (pCols->cols) {
1588
      int maxCols = pCols->maxCols;
1589
      for (i = 0; i < maxCols; ++i) {
1590
        SDataCol *pCol = &pCols->cols[i];
wafwerar's avatar
wafwerar 已提交
1591
        taosMemoryFreeClear(pCol->pData);
1592
      }
wafwerar's avatar
wafwerar 已提交
1593
      taosMemoryFree(pCols->cols);
1594 1595
      pCols->cols = NULL;
    }
wafwerar's avatar
wafwerar 已提交
1596
    taosMemoryFree(pCols);
H
TD-34  
hzcheng 已提交
1597
  }
H
Hongze Cheng 已提交
1598
  return NULL;
H
TD-34  
hzcheng 已提交
1599 1600 1601
}

void tdResetDataCols(SDataCols *pCols) {
B
Bomin Zhang 已提交
1602 1603
  if (pCols != NULL) {
    pCols->numOfRows = 0;
C
Cary Xu 已提交
1604
    pCols->bitmapMode = 0;
1605
    for (int i = 0; i < pCols->maxCols; ++i) {
B
Bomin Zhang 已提交
1606 1607
      dataColReset(pCols->cols + i);
    }
H
TD-34  
hzcheng 已提交
1608 1609
  }
}
H
Hongze Cheng 已提交
1610

H
Hongze Cheng 已提交
1611
#endif