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

H
Hongze Cheng 已提交
22
typedef struct SKVIdx {
H
Hongze Cheng 已提交
23 24
  int32_t cid;
  int32_t offset;
H
Hongze Cheng 已提交
25
} SKVIdx;
H
Hongze Cheng 已提交
26

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

H
Hongze Cheng 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
typedef struct STagIdx {
  int16_t  cid;
  uint16_t offset;
} STagIdx;

#pragma pack(push, 1)
struct STag {
  uint16_t len;
  uint16_t nTag;
  STagIdx  idx[];
};
#pragma pack(pop)

H
Hongze Cheng 已提交
47
#define TSROW_IS_KV_ROW(r) ((r)->flags & TSROW_KV_ROW)
H
Hongze Cheng 已提交
48 49
#define BIT1_SIZE(n)       (((n)-1) / 8 + 1)
#define BIT2_SIZE(n)       (((n)-1) / 4 + 1)
H
Hongze Cheng 已提交
50 51
#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 已提交
52 53
#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 已提交
54

H
Hongze Cheng 已提交
55 56
static FORCE_INLINE int tSKVIdxCmprFn(const void *p1, const void *p2);

H
Hongze Cheng 已提交
57
// STSRow2
H
Hongze Cheng 已提交
58 59
int32_t tPutTSRow(uint8_t *p, STSRow2 *pRow) {
  int32_t n = 0;
H
Hongze Cheng 已提交
60

H
Hongze Cheng 已提交
61 62 63 64 65
  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);
H
Hongze Cheng 已提交
66 67 68 69

  switch (pRow->flags & 0xf) {
    case TSROW_HAS_NONE:
    case TSROW_HAS_NULL:
H
Hongze Cheng 已提交
70
      break;
H
Hongze Cheng 已提交
71
    default:
H
Hongze Cheng 已提交
72
      n += tPutBinary(p ? p + n : p, pRow->pData, pRow->nData);
H
Hongze Cheng 已提交
73
      break;
H
Hongze Cheng 已提交
74
  }
H
Hongze Cheng 已提交
75

H
Hongze Cheng 已提交
76
  return n;
H
Hongze Cheng 已提交
77 78
}

H
Hongze Cheng 已提交
79 80 81
int32_t tGetTSRow(uint8_t *p, STSRow2 *pRow) {
  int32_t n = 0;
  uint8_t flags;
H
Hongze Cheng 已提交
82

H
Hongze Cheng 已提交
83 84 85
  n += tGetI64(p + n, pRow ? &pRow->ts : NULL);
  n += tGetI8(p + n, pRow ? &pRow->flags : &flags);
  n += tGetI32v(p + n, pRow ? &pRow->sver : NULL);
H
Hongze Cheng 已提交
86

H
Hongze Cheng 已提交
87 88
  if (pRow) flags = pRow->flags;
  switch (flags & 0xf) {
H
Hongze Cheng 已提交
89 90
    case TSROW_HAS_NONE:
    case TSROW_HAS_NULL:
H
Hongze Cheng 已提交
91
      break;
H
Hongze Cheng 已提交
92
    default:
H
Hongze Cheng 已提交
93
      n += tGetBinary(p + n, pRow ? &pRow->pData : NULL, pRow ? &pRow->nData : NULL);
H
Hongze Cheng 已提交
94
      break;
H
Hongze Cheng 已提交
95
  }
H
Hongze Cheng 已提交
96

H
Hongze Cheng 已提交
97
  return n;
H
Hongze Cheng 已提交
98 99
}

H
Hongze Cheng 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
int32_t tTSRowDup(const STSRow2 *pRow, STSRow2 **ppRow) {
  (*ppRow) = taosMemoryMalloc(sizeof(*pRow) + pRow->nData);
  if (*ppRow == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  (*ppRow)->ts = pRow->ts;
  (*ppRow)->flags = pRow->flags;
  (*ppRow)->sver = pRow->sver;
  (*ppRow)->nData = pRow->nData;
  if (pRow->nData) {
    (*ppRow)->pData = (uint8_t *)(&(*ppRow)[1]);
    memcpy((*ppRow)->pData, pRow->pData, pRow->nData);
  } else {
    (*ppRow)->pData = NULL;
  }

  return 0;
}

void tTSRowFree(STSRow2 *pRow) {
  if (pRow) taosMemoryFree(pRow);
}

H
Hongze Cheng 已提交
125
int32_t tTSRowGet(const STSRow2 *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
H
Hongze Cheng 已提交
126 127 128 129 130 131 132
  uint32_t  n;
  uint8_t  *p;
  uint8_t   v;
  int32_t   bidx = iCol - 1;
  STColumn *pTColumn = &pTSchema->columns[iCol];
  STSKVRow *pTSKVRow;
  SKVIdx   *pKVIdx;
H
Hongze Cheng 已提交
133

H
Hongze Cheng 已提交
134
  ASSERT(iCol != 0);
H
Hongze Cheng 已提交
135 136 137 138 139
  ASSERT(pTColumn->colId != 0);

  ASSERT(pRow->flags & 0xf != 0);
  switch (pRow->flags & 0xf) {
    case TSROW_HAS_NONE:
H
Hongze Cheng 已提交
140
      *pColVal = ColValNONE;
H
Hongze Cheng 已提交
141 142
      return 0;
    case TSROW_HAS_NULL:
H
Hongze Cheng 已提交
143
      *pColVal = ColValNULL;
H
Hongze Cheng 已提交
144 145 146 147 148 149 150
      return 0;
  }

  if (TSROW_IS_KV_ROW(pRow)) {
    ASSERT((pRow->flags & 0xf) != TSROW_HAS_VAL);

    pTSKVRow = (STSKVRow *)pRow->pData;
H
Hongze Cheng 已提交
151 152
    pKVIdx =
        bsearch(&((SKVIdx){.cid = pTColumn->colId}), pTSKVRow->idx, pTSKVRow->nCols, sizeof(SKVIdx), tSKVIdxCmprFn);
H
Hongze Cheng 已提交
153
    if (pKVIdx == NULL) {
H
Hongze Cheng 已提交
154
      *pColVal = ColValNONE;
H
Hongze Cheng 已提交
155
    } else if (pKVIdx->offset < 0) {
H
Hongze Cheng 已提交
156
      *pColVal = ColValNULL;
H
Hongze Cheng 已提交
157 158
    } else {
      p = pRow->pData + sizeof(STSKVRow) + sizeof(SKVIdx) * pTSKVRow->nCols + pKVIdx->offset;
H
Hongze Cheng 已提交
159 160
      pColVal->type = COL_VAL_DATA;
      tGetBinary(p, &pColVal->pData, &pColVal->nData);
H
Hongze Cheng 已提交
161 162 163
    }
  } else {
    // get bitmap
H
Hongze Cheng 已提交
164
    p = pRow->pData;
H
Hongze Cheng 已提交
165
    switch (pRow->flags & 0xf) {
H
Hongze Cheng 已提交
166
      case TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
167 168
        v = GET_BIT1(p, bidx);
        if (v == 0) {
H
Hongze Cheng 已提交
169
          *pColVal = ColValNONE;
H
Hongze Cheng 已提交
170
        } else {
H
Hongze Cheng 已提交
171
          *pColVal = ColValNULL;
H
Hongze Cheng 已提交
172
        }
H
Hongze Cheng 已提交
173
        return 0;
H
Hongze Cheng 已提交
174
      case TSROW_HAS_VAL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
175 176
        v = GET_BIT1(p, bidx);
        if (v == 1) {
H
Hongze Cheng 已提交
177
          p = p + BIT1_SIZE(pTSchema->numOfCols - 1);
H
Hongze Cheng 已提交
178 179
          break;
        } else {
H
Hongze Cheng 已提交
180
          *pColVal = ColValNONE;
H
Hongze Cheng 已提交
181 182
          return 0;
        }
H
Hongze Cheng 已提交
183
      case TSROW_HAS_VAL | TSROW_HAS_NULL:
H
Hongze Cheng 已提交
184 185
        v = GET_BIT1(p, bidx);
        if (v == 1) {
H
Hongze Cheng 已提交
186
          p = p + BIT1_SIZE(pTSchema->numOfCols - 1);
H
Hongze Cheng 已提交
187
          break;
H
Hongze Cheng 已提交
188
        } else {
H
Hongze Cheng 已提交
189
          *pColVal = ColValNULL;
H
Hongze Cheng 已提交
190
          return 0;
H
Hongze Cheng 已提交
191
        }
H
Hongze Cheng 已提交
192
      case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
193 194
        v = GET_BIT2(p, bidx);
        if (v == 0) {
H
Hongze Cheng 已提交
195
          *pColVal = ColValNONE;
H
Hongze Cheng 已提交
196 197
          return 0;
        } else if (v == 1) {
H
Hongze Cheng 已提交
198
          *pColVal = ColValNULL;
H
Hongze Cheng 已提交
199 200
          return 0;
        } else if (v == 2) {
H
Hongze Cheng 已提交
201
          p = p + BIT2_SIZE(pTSchema->numOfCols - 1);
H
Hongze Cheng 已提交
202 203 204 205
          break;
        } else {
          ASSERT(0);
        }
H
Hongze Cheng 已提交
206
      default:
H
Hongze Cheng 已提交
207
        break;
H
Hongze Cheng 已提交
208
    }
H
Hongze Cheng 已提交
209 210 211

    // get real value
    p = p + pTColumn->offset;
H
Hongze Cheng 已提交
212
    pColVal->type = COL_VAL_DATA;
H
Hongze Cheng 已提交
213
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
H
Hongze Cheng 已提交
214
      tGetBinary(p + pTSchema->flen + *(int32_t *)p, &pColVal->pData, &pColVal->nData);
H
Hongze Cheng 已提交
215
    } else {
H
Hongze Cheng 已提交
216 217
      pColVal->pData = p;
      pColVal->nData = pTColumn->bytes;
H
Hongze Cheng 已提交
218
    }
H
Hongze Cheng 已提交
219
  }
H
Hongze Cheng 已提交
220

H
Hongze Cheng 已提交
221 222 223 224
  return 0;
}

// STSchema
H
Hongze Cheng 已提交
225 226 227 228 229 230 231
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 已提交
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
  (*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 已提交
257 258 259
  return 0;
}

H
Hongze Cheng 已提交
260 261 262
void tTSchemaDestroy(STSchema *pTSchema) {
  if (pTSchema) taosMemoryFree(pTSchema);
}
H
Hongze Cheng 已提交
263

H
Hongze Cheng 已提交
264
// STSRowBuilder
H
Hongze Cheng 已提交
265
int32_t tTSRowBuilderInit(STSRowBuilder *pBuilder, int32_t sver, int32_t nCols, SSchema *pSchema) {
H
Hongze Cheng 已提交
266 267
  if (tTSchemaCreate(sver, pSchema, nCols, &pBuilder->pTSchema) < 0) return -1;

H
Hongze Cheng 已提交
268 269
  pBuilder->szBitMap1 = BIT1_SIZE(nCols - 1);
  pBuilder->szBitMap2 = BIT2_SIZE(nCols - 1);
H
Hongze Cheng 已提交
270 271
  pBuilder->szKVBuf =
      sizeof(STSKVRow) + sizeof(SKVIdx) * (nCols - 1) + pBuilder->pTSchema->flen + pBuilder->pTSchema->vlen;
H
Hongze Cheng 已提交
272 273 274
  pBuilder->szTPBuf = pBuilder->szBitMap2 + pBuilder->pTSchema->flen + pBuilder->pTSchema->vlen;
  pBuilder->pKVBuf = taosMemoryMalloc(pBuilder->szKVBuf);
  if (pBuilder->pKVBuf == NULL) {
H
Hongze Cheng 已提交
275 276
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    tTSchemaDestroy(pBuilder->pTSchema);
H
Hongze Cheng 已提交
277
    return -1;
H
Hongze Cheng 已提交
278
  }
H
Hongze Cheng 已提交
279 280 281
  pBuilder->pTPBuf = taosMemoryMalloc(pBuilder->szTPBuf);
  if (pBuilder->pTPBuf == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
282 283
    taosMemoryFree(pBuilder->pKVBuf);
    tTSchemaDestroy(pBuilder->pTSchema);
H
Hongze Cheng 已提交
284
    return -1;
H
Hongze Cheng 已提交
285 286
  }

H
Hongze Cheng 已提交
287 288 289
  return 0;
}

H
Hongze Cheng 已提交
290
void tTSRowBuilderClear(STSRowBuilder *pBuilder) {
H
Hongze Cheng 已提交
291 292 293 294
  if (pBuilder->pTPBuf) {
    taosMemoryFree(pBuilder->pTPBuf);
    pBuilder->pTPBuf = NULL;
  }
H
Hongze Cheng 已提交
295 296 297 298 299 300
  if (pBuilder->pKVBuf) {
    taosMemoryFree(pBuilder->pKVBuf);
    pBuilder->pKVBuf = NULL;
  }
  tTSchemaDestroy(pBuilder->pTSchema);
  pBuilder->pTSchema = NULL;
H
Hongze Cheng 已提交
301 302
}

H
Hongze Cheng 已提交
303
void tTSRowBuilderReset(STSRowBuilder *pBuilder) {
H
Hongze Cheng 已提交
304
  for (int32_t iCol = pBuilder->pTSchema->numOfCols - 1; iCol >= 0; iCol--) {
H
Hongze Cheng 已提交
305 306
    STColumn *pTColumn = &pBuilder->pTSchema->columns[iCol];
    COL_CLR_SET(pTColumn->flags);
H
Hongze Cheng 已提交
307 308
  }

H
Hongze Cheng 已提交
309
  pBuilder->iCol = 0;
H
Hongze Cheng 已提交
310
  ((STSKVRow *)pBuilder->pKVBuf)->nCols = 0;
H
Hongze Cheng 已提交
311 312
  pBuilder->vlenKV = 0;
  pBuilder->vlenTP = 0;
H
Hongze Cheng 已提交
313
  pBuilder->row.flags = 0;
H
Hongze Cheng 已提交
314 315
}

H
Hongze Cheng 已提交
316
int32_t tTSRowBuilderPut(STSRowBuilder *pBuilder, int32_t cid, uint8_t *pData, uint32_t nData) {
H
Hongze Cheng 已提交
317 318 319
  STColumn *pTColumn = &pBuilder->pTSchema->columns[pBuilder->iCol];
  uint8_t  *p;
  int32_t   iCol;
H
Hongze Cheng 已提交
320
  STSKVRow *pTSKVRow = (STSKVRow *)pBuilder->pKVBuf;
H
Hongze Cheng 已提交
321

H
Hongze Cheng 已提交
322 323
  // use interp search
  if (pTColumn->colId < cid) {  // right search
H
Hongze Cheng 已提交
324 325
    for (iCol = pBuilder->iCol + 1; iCol < pBuilder->pTSchema->numOfCols; iCol++) {
      pTColumn = &pBuilder->pTSchema->columns[iCol];
H
Hongze Cheng 已提交
326
      if (pTColumn->colId >= cid) break;
H
Hongze Cheng 已提交
327
    }
H
Hongze Cheng 已提交
328
  } else if (pTColumn->colId > cid) {  // left search
H
Hongze Cheng 已提交
329 330
    for (iCol = pBuilder->iCol - 1; iCol >= 0; iCol--) {
      pTColumn = &pBuilder->pTSchema->columns[iCol];
H
Hongze Cheng 已提交
331
      if (pTColumn->colId <= cid) break;
H
Hongze Cheng 已提交
332
    }
H
Hongze Cheng 已提交
333 334
  }

H
Hongze Cheng 已提交
335
  if (pTColumn->colId != cid || COL_IS_SET(pTColumn->flags)) {
H
Hongze Cheng 已提交
336 337 338
    return -1;
  }

H
Hongze Cheng 已提交
339 340
  pBuilder->iCol = iCol;

H
Hongze Cheng 已提交
341 342
  // set value
  if (cid == 0) {
H
Hongze Cheng 已提交
343
    ASSERT(pData && nData == sizeof(TSKEY) && iCol == 0);
H
Hongze Cheng 已提交
344
    pBuilder->row.ts = *(TSKEY *)pData;
H
Hongze Cheng 已提交
345
    pTColumn->flags |= COL_SET_VAL;
H
Hongze Cheng 已提交
346
  } else {
H
Hongze Cheng 已提交
347 348
    if (pData) {
      // set VAL
H
Hongze Cheng 已提交
349

H
Hongze Cheng 已提交
350 351 352 353 354
      pBuilder->row.flags |= TSROW_HAS_VAL;
      pTColumn->flags |= COL_SET_VAL;

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

H
Hongze Cheng 已提交
358 359
        p = pBuilder->pKVBuf + sizeof(STSKVRow) + sizeof(SKVIdx) * (pBuilder->pTSchema->numOfCols - 1) +
            pBuilder->vlenKV;
H
Hongze Cheng 已提交
360 361 362 363 364 365 366 367
        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 已提交
368 369
      }

H
Hongze Cheng 已提交
370 371 372 373 374
      /* 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 已提交
375

H
Hongze Cheng 已提交
376
        p = pBuilder->pTPBuf + pBuilder->szBitMap2 + pBuilder->pTSchema->flen + pBuilder->vlenTP;
H
Hongze Cheng 已提交
377
        pBuilder->vlenTP += tPutBinary(p, pData, nData);
H
Hongze Cheng 已提交
378
      } else {
H
Hongze Cheng 已提交
379
        ASSERT(nData == pTColumn->bytes);
H
Hongze Cheng 已提交
380 381
        memcpy(p, pData, nData);
      }
H
Hongze Cheng 已提交
382 383 384
    } else {
      // set NULL

H
Hongze Cheng 已提交
385
      pBuilder->row.flags |= TSROW_HAS_NULL;
H
Hongze Cheng 已提交
386
      pTColumn->flags |= COL_SET_NULL;
H
Hongze Cheng 已提交
387

H
Hongze Cheng 已提交
388 389
      pTSKVRow->idx[pTSKVRow->nCols].cid = cid;
      pTSKVRow->idx[pTSKVRow->nCols].offset = -1;
H
Hongze Cheng 已提交
390
    }
H
Hongze Cheng 已提交
391

H
Hongze Cheng 已提交
392
    pTSKVRow->nCols++;
H
Hongze Cheng 已提交
393 394 395 396 397
  }

  return 0;
}

H
Hongze Cheng 已提交
398 399 400 401 402 403 404 405 406 407
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 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
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 已提交
427
        } else if (pTColumn->flags & COL_SET_VAL) {
H
Hongze Cheng 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
          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 已提交
444
int32_t tTSRowBuilderGetRow(STSRowBuilder *pBuilder, const STSRow2 **ppRow) {
H
Hongze Cheng 已提交
445 446 447
  int32_t   nDataTP, nDataKV;
  uint32_t  flags;
  STSKVRow *pTSKVRow = (STSKVRow *)pBuilder->pKVBuf;
H
Hongze Cheng 已提交
448
  int32_t   nCols = pBuilder->pTSchema->numOfCols;
H
Hongze Cheng 已提交
449 450

  // error not set ts
H
Hongze Cheng 已提交
451
  if (!COL_IS_SET(pBuilder->pTSchema->columns->flags)) {
H
Hongze Cheng 已提交
452 453 454
    return -1;
  }

H
Hongze Cheng 已提交
455 456
  ASSERT(pTSKVRow->nCols < nCols);
  if (pTSKVRow->nCols < nCols - 1) {
H
Hongze Cheng 已提交
457
    pBuilder->row.flags |= TSROW_HAS_NONE;
H
Hongze Cheng 已提交
458
  }
H
Hongze Cheng 已提交
459

H
Hongze Cheng 已提交
460 461 462 463 464
  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 已提交
465 466
      pBuilder->row.nData = 0;
      pBuilder->row.pData = NULL;
H
Hongze Cheng 已提交
467 468
      return 0;
    case TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
469
      nDataTP = pBuilder->szBitMap1;
H
Hongze Cheng 已提交
470
      break;
H
Hongze Cheng 已提交
471
    case TSROW_HAS_VAL:
H
Hongze Cheng 已提交
472
      nDataTP = pBuilder->pTSchema->flen + pBuilder->vlenTP;
H
Hongze Cheng 已提交
473
      break;
H
Hongze Cheng 已提交
474 475
    case TSROW_HAS_VAL | TSROW_HAS_NONE:
    case TSROW_HAS_VAL | TSROW_HAS_NULL:
H
Hongze Cheng 已提交
476
      nDataTP = pBuilder->szBitMap1 + pBuilder->pTSchema->flen + pBuilder->vlenTP;
H
Hongze Cheng 已提交
477
      break;
H
Hongze Cheng 已提交
478
    case TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE:
H
Hongze Cheng 已提交
479
      nDataTP = pBuilder->szBitMap2 + pBuilder->pTSchema->flen + pBuilder->vlenTP;
H
Hongze Cheng 已提交
480 481
      break;
    default:
H
Hongze Cheng 已提交
482
      ASSERT(0);
H
Hongze Cheng 已提交
483 484
  }

H
Hongze Cheng 已提交
485 486
  nDataKV = sizeof(STSKVRow) + sizeof(SKVIdx) * pTSKVRow->nCols + pBuilder->vlenKV;
  pBuilder->row.sver = pBuilder->pTSchema->version;
H
Hongze Cheng 已提交
487 488 489
  if (nDataKV < nDataTP) {
    // generate KV row

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

H
Hongze Cheng 已提交
492
    pBuilder->row.flags |= TSROW_KV_ROW;
H
Hongze Cheng 已提交
493
    pBuilder->row.nData = nDataKV;
H
Hongze Cheng 已提交
494
    pBuilder->row.pData = pBuilder->pKVBuf;
H
Hongze Cheng 已提交
495

H
Hongze Cheng 已提交
496
    qsort(pTSKVRow->idx, pTSKVRow->nCols, sizeof(SKVIdx), tSKVIdxCmprFn);
H
Hongze Cheng 已提交
497 498
    if (pTSKVRow->nCols < nCols - 1) {
      memmove(&pTSKVRow->idx[pTSKVRow->nCols], &pTSKVRow->idx[nCols - 1], pBuilder->vlenKV);
H
Hongze Cheng 已提交
499 500
    }
  } else {
H
Hongze Cheng 已提交
501 502 503 504
    // generate TUPLE row

    pBuilder->row.nData = nDataTP;

H
Hongze Cheng 已提交
505 506
    uint8_t *p;
    uint8_t  flags = pBuilder->row.flags & 0xf;
H
Hongze Cheng 已提交
507

H
Hongze Cheng 已提交
508 509
    if (flags == TSROW_HAS_VAL) {
      pBuilder->row.pData = pBuilder->pTPBuf + pBuilder->szBitMap2;
H
Hongze Cheng 已提交
510
    } else {
H
Hongze Cheng 已提交
511 512
      if (flags == TSROW_HAS_VAL | TSROW_HAS_NULL | TSROW_HAS_NONE) {
        pBuilder->row.pData = pBuilder->pTPBuf;
H
Hongze Cheng 已提交
513
      } else {
H
Hongze Cheng 已提交
514
        pBuilder->row.pData = pBuilder->pTPBuf + pBuilder->szBitMap2 - pBuilder->szBitMap1;
H
Hongze Cheng 已提交
515 516
      }

H
Hongze Cheng 已提交
517
      setBitMap(pBuilder->row.pData, pBuilder->pTSchema, flags);
H
Hongze Cheng 已提交
518
    }
H
Hongze Cheng 已提交
519 520 521 522 523
  }

  return 0;
}

H
Hongze Cheng 已提交
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
static FORCE_INLINE int tTagIdxCmprFn(const void *p1, const void *p2) {
  STagIdx *pTagIdx1 = (STagIdx *)p1;
  STagIdx *pTagIdx2 = (STagIdx *)p2;
  if (pTagIdx1->cid < pTagIdx1->cid) {
    return -1;
  } else if (pTagIdx1->cid > pTagIdx1->cid) {
    return 1;
  }
  return 0;
}
int32_t tTagNew(STagVal *pTagVals, int16_t nTag, STag **ppTag) {
  STagVal *pTagVal;
  uint8_t *p;
  int32_t  n;
  uint16_t tsize = sizeof(STag) + sizeof(STagIdx) * nTag;

  for (int16_t iTag = 0; iTag < nTag; iTag++) {
    pTagVal = &pTagVals[iTag];

    if (IS_VAR_DATA_TYPE(pTagVal->type)) {
      tsize += tPutBinary(NULL, pTagVal->pData, pTagVal->nData);
    } else {
      ASSERT(pTagVal->nData == TYPE_BYTES[pTagVal->type]);
      tsize += pTagVal->nData;
    }
  }

  (*ppTag) = (STag *)taosMemoryMalloc(tsize);
  if (*ppTag == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  p = (uint8_t *)&((*ppTag)->idx[nTag]);
  n = 0;

  (*ppTag)->len = tsize;
  (*ppTag)->nTag = nTag;
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
    pTagVal = &pTagVals[iTag];

    (*ppTag)->idx[iTag].cid = pTagVal->cid;
    (*ppTag)->idx[iTag].offset = n;

    if (IS_VAR_DATA_TYPE(pTagVal->type)) {
      n += tPutBinary(p + n, pTagVal->pData, pTagVal->nData);
    } else {
      memcpy(p + n, pTagVal->pData, pTagVal->nData);
      n += pTagVal->nData;
    }
  }

  qsort((*ppTag)->idx, (*ppTag)->nTag, sizeof(STagIdx), tTagIdxCmprFn);
  return 0;
}

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

H
Hongze Cheng 已提交
584 585 586 587 588 589 590 591 592 593 594 595 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 621 622 623 624 625 626 627 628 629
int32_t tTagSet(STag *pTag, SSchema *pSchema, int32_t nCols, int iCol, uint8_t *pData, uint32_t nData, STag **ppTag) {
  STagVal *pTagVals;
  int16_t  nTags = 0;
  SSchema *pColumn;
  uint8_t *p;
  uint32_t n;

  pTagVals = (STagVal *)taosMemoryMalloc(sizeof(*pTagVals) * nCols);
  if (pTagVals == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  for (int32_t i = 0; i < nCols; i++) {
    pColumn = &pSchema[i];

    if (i == iCol) {
      p = pData;
      n = nData;
    } else {
      tTagGet(pTag, pColumn->colId, pColumn->type, &p, &n);
    }

    if (p == NULL) continue;

    ASSERT(IS_VAR_DATA_TYPE(pColumn->type) || n == pColumn->bytes);

    pTagVals[nTags].cid = pColumn->colId;
    pTagVals[nTags].type = pColumn->type;
    pTagVals[nTags].nData = n;
    pTagVals[nTags].pData = p;

    nTags++;
  }

  // create new tag
  if (tTagNew(pTagVals, nTags, ppTag) < 0) {
    taosMemoryFree(pTagVals);
    return -1;
  }

  taosMemoryFree(pTagVals);
  return 0;
}

void tTagGet(STag *pTag, int16_t cid, int8_t type, uint8_t **ppData, uint32_t *nData) {
H
Hongze Cheng 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
  STagIdx *pTagIdx = bsearch(&((STagIdx){.cid = cid}), pTag->idx, pTag->nTag, sizeof(STagIdx), tTagIdxCmprFn);
  if (pTagIdx == NULL) {
    *ppData = NULL;
    *nData = 0;
  } else {
    uint8_t *p = (uint8_t *)&pTag->idx[pTag->nTag] + pTagIdx->offset;
    if (IS_VAR_DATA_TYPE(type)) {
      tGetBinary(p, ppData, nData);
    } else {
      *ppData = p;
      *nData = TYPE_BYTES[type];
    }
  }
}

H
more  
Hongze Cheng 已提交
645 646
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
H
Hongze Cheng 已提交
647 648 649
}

int32_t tDecodeTag(SDecoder *pDecoder, const STag **ppTag) {
H
more  
Hongze Cheng 已提交
650
  return tDecodeBinary(pDecoder, (const uint8_t **)ppTag, NULL);
H
Hongze Cheng 已提交
651 652 653
}

#if 1  // ===================================================================================================================
654
static void dataColSetNEleNull(SDataCol *pCol, int nEle);
H
Hongze Cheng 已提交
655
int         tdAllocMemForCol(SDataCol *pCol, int maxPoints) {
L
Liu Jicong 已提交
656
  int spaceNeeded = pCol->bytes * maxPoints;
S
Shengliang Guan 已提交
657
  if (IS_VAR_DATA_TYPE(pCol->type)) {
L
Liu Jicong 已提交
658
    spaceNeeded += sizeof(VarDataOffsetT) * maxPoints;
L
Liu Jicong 已提交
659
  }
C
Cary Xu 已提交
660
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
661 662 663 664
  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 已提交
665 666
  // 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 已提交
667
#endif
C
Cary Xu 已提交
668

S
Shengliang Guan 已提交
669
  if (pCol->spaceSize < spaceNeeded) {
wafwerar's avatar
wafwerar 已提交
670
    void *ptr = taosMemoryRealloc(pCol->pData, spaceNeeded);
S
Shengliang Guan 已提交
671 672
    if (ptr == NULL) {
      uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)spaceNeeded, strerror(errno));
L
Liu Jicong 已提交
673
      return -1;
L
Liu Jicong 已提交
674 675 676
    } else {
      pCol->pData = ptr;
      pCol->spaceSize = spaceNeeded;
677 678
    }
  }
C
Cary Xu 已提交
679
#ifdef TD_SUPPORT_BITMAP
680

C
Cary Xu 已提交
681 682 683 684
  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 已提交
685
    pCol->pBitmap = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
L
Liu Jicong 已提交
686
  }
C
Cary Xu 已提交
687 688 689 690
#else
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->dataOff = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
  }
C
Cary Xu 已提交
691
#endif
L
Liu Jicong 已提交
692
  return 0;
693 694
}

H
hzcheng 已提交
695 696 697
/**
 * Duplicate the schema and return a new object
 */
H
Hongze Cheng 已提交
698
STSchema *tdDupSchema(const STSchema *pSchema) {
S
Shengliang Guan 已提交
699
  int       tlen = sizeof(STSchema) + sizeof(STColumn) * schemaNCols(pSchema);
wafwerar's avatar
wafwerar 已提交
700
  STSchema *tSchema = (STSchema *)taosMemoryMalloc(tlen);
H
hzcheng 已提交
701 702
  if (tSchema == NULL) return NULL;

H
Hongze Cheng 已提交
703
  memcpy((void *)tSchema, (void *)pSchema, tlen);
H
hzcheng 已提交
704 705 706 707

  return tSchema;
}

H
TD-27  
hzcheng 已提交
708 709 710
/**
 * Encode a schema to dst, and return the next pointer
 */
H
TD-353  
Hongze Cheng 已提交
711 712 713 714
int tdEncodeSchema(void **buf, STSchema *pSchema) {
  int tlen = 0;
  tlen += taosEncodeFixedI32(buf, schemaVersion(pSchema));
  tlen += taosEncodeFixedI32(buf, schemaNCols(pSchema));
H
TD-166  
hzcheng 已提交
715

H
TD-27  
hzcheng 已提交
716 717
  for (int i = 0; i < schemaNCols(pSchema); i++) {
    STColumn *pCol = schemaColAt(pSchema, i);
H
TD-353  
Hongze Cheng 已提交
718
    tlen += taosEncodeFixedI8(buf, colType(pCol));
C
Cary Xu 已提交
719
    tlen += taosEncodeFixedI8(buf, colFlags(pCol));
H
TD-353  
Hongze Cheng 已提交
720
    tlen += taosEncodeFixedI16(buf, colColId(pCol));
721
    tlen += taosEncodeFixedI16(buf, colBytes(pCol));
H
TD-27  
hzcheng 已提交
722 723
  }

H
TD-353  
Hongze Cheng 已提交
724
  return tlen;
H
TD-27  
hzcheng 已提交
725 726 727 728 729
}

/**
 * Decode a schema from a binary.
 */
H
TD-353  
Hongze Cheng 已提交
730
void *tdDecodeSchema(void *buf, STSchema **pRSchema) {
S
Shengliang Guan 已提交
731 732
  int             version = 0;
  int             numOfCols = 0;
H
TD-353  
Hongze Cheng 已提交
733
  STSchemaBuilder schemaBuilder;
H
TD-27  
hzcheng 已提交
734

H
TD-353  
Hongze Cheng 已提交
735 736
  buf = taosDecodeFixedI32(buf, &version);
  buf = taosDecodeFixedI32(buf, &numOfCols);
H
TD-27  
hzcheng 已提交
737

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

H
TD-353  
Hongze Cheng 已提交
740
  for (int i = 0; i < numOfCols; i++) {
741
    col_type_t  type = 0;
C
Cary Xu 已提交
742
    int8_t      flags = 0;
743 744
    col_id_t    colId = 0;
    col_bytes_t bytes = 0;
H
TD-353  
Hongze Cheng 已提交
745
    buf = taosDecodeFixedI8(buf, &type);
C
Cary Xu 已提交
746
    buf = taosDecodeFixedI8(buf, &flags);
H
TD-353  
Hongze Cheng 已提交
747
    buf = taosDecodeFixedI16(buf, &colId);
748
    buf = taosDecodeFixedI32(buf, &bytes);
C
Cary Xu 已提交
749
    if (tdAddColToSchema(&schemaBuilder, type, flags, colId, bytes) < 0) {
H
Hongze Cheng 已提交
750 751 752
      tdDestroyTSchemaBuilder(&schemaBuilder);
      return NULL;
    }
H
TD-27  
hzcheng 已提交
753 754
  }

H
TD-353  
Hongze Cheng 已提交
755
  *pRSchema = tdGetSchemaFromBuilder(&schemaBuilder);
H
Hongze Cheng 已提交
756
  tdDestroyTSchemaBuilder(&schemaBuilder);
H
TD-353  
Hongze Cheng 已提交
757
  return buf;
H
Hongze Cheng 已提交
758 759
}

C
Cary Xu 已提交
760
int tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
761 762 763
  if (pBuilder == NULL) return -1;

  pBuilder->tCols = 256;
wafwerar's avatar
wafwerar 已提交
764
  pBuilder->columns = (STColumn *)taosMemoryMalloc(sizeof(STColumn) * pBuilder->tCols);
H
Hongze Cheng 已提交
765 766 767 768 769 770 771 772
  if (pBuilder->columns == NULL) return -1;

  tdResetTSchemaBuilder(pBuilder, version);
  return 0;
}

void tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder) {
  if (pBuilder) {
wafwerar's avatar
wafwerar 已提交
773
    taosMemoryFreeClear(pBuilder->columns);
H
Hongze Cheng 已提交
774 775 776
  }
}

C
Cary Xu 已提交
777
void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
778 779 780
  pBuilder->nCols = 0;
  pBuilder->tlen = 0;
  pBuilder->flen = 0;
T
Tao Liu 已提交
781
  pBuilder->vlen = 0;
H
Hongze Cheng 已提交
782 783 784
  pBuilder->version = version;
}

C
Cary Xu 已提交
785
int32_t tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes) {
786
  if (!isValidDataType(type)) return -1;
H
Hongze Cheng 已提交
787 788 789

  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
wafwerar's avatar
wafwerar 已提交
790
    STColumn *columns = (STColumn *)taosMemoryRealloc(pBuilder->columns, sizeof(STColumn) * pBuilder->tCols);
T
tickduan 已提交
791 792
    if (columns == NULL) return -1;
    pBuilder->columns = columns;
H
Hongze Cheng 已提交
793 794 795 796 797
  }

  STColumn *pCol = &(pBuilder->columns[pBuilder->nCols]);
  colSetType(pCol, type);
  colSetColId(pCol, colId);
C
Cary Xu 已提交
798
  colSetFlags(pCol, flags);
H
Hongze Cheng 已提交
799 800 801
  if (pBuilder->nCols == 0) {
    colSetOffset(pCol, 0);
  } else {
S
Shengliang Guan 已提交
802
    STColumn *pTCol = &(pBuilder->columns[pBuilder->nCols - 1]);
H
Hongze Cheng 已提交
803 804 805 806 807
    colSetOffset(pCol, pTCol->offset + TYPE_BYTES[pTCol->type]);
  }

  if (IS_VAR_DATA_TYPE(type)) {
    colSetBytes(pCol, bytes);
T
Tao Liu 已提交
808 809
    pBuilder->tlen += (TYPE_BYTES[type] + bytes);
    pBuilder->vlen += bytes - sizeof(VarDataLenT);
H
Hongze Cheng 已提交
810 811 812
  } else {
    colSetBytes(pCol, TYPE_BYTES[type]);
    pBuilder->tlen += TYPE_BYTES[type];
T
Tao Liu 已提交
813
    pBuilder->vlen += TYPE_BYTES[type];
H
Hongze Cheng 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
  }

  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 已提交
829
  STSchema *pSchema = (STSchema *)taosMemoryMalloc(tlen);
H
Hongze Cheng 已提交
830 831 832 833 834 835
  if (pSchema == NULL) return NULL;

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

C
Cary Xu 已提交
838
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
839
  schemaTLen(pSchema) += (int)TD_BITMAP_BYTES(schemaNCols(pSchema));
C
Cary Xu 已提交
840 841
#endif

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

H
TD-27  
hzcheng 已提交
844 845 846
  return pSchema;
}

847
void dataColInit(SDataCol *pDataCol, STColumn *pCol, int maxPoints) {
H
TD-166  
hzcheng 已提交
848 849 850
  pDataCol->type = colType(pCol);
  pDataCol->colId = colColId(pCol);
  pDataCol->bytes = colBytes(pCol);
S
Shengliang Guan 已提交
851
  pDataCol->offset = colOffset(pCol) + 0;  // TD_DATA_ROW_HEAD_SIZE;
H
TD-166  
hzcheng 已提交
852 853 854

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

L
Liu Jicong 已提交
856 857 858 859 860 861
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 已提交
862 863
}

H
TD-166  
hzcheng 已提交
864
bool isNEleNull(SDataCol *pCol, int nEle) {
S
Shengliang Guan 已提交
865
  if (isAllRowsNull(pCol)) return true;
866
  for (int i = 0; i < nEle; ++i) {
L
Liu Jicong 已提交
867
    if (!isNull(tdGetColDataOfRowUnsafe(pCol, i), pCol->type)) return false;
H
TD-166  
hzcheng 已提交
868
  }
H
Hongze Cheng 已提交
869
  return true;
H
TD-166  
hzcheng 已提交
870 871
}

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

H
Hongze Cheng 已提交
875
  void *tptr = pCol->pData;
H
TD-166  
hzcheng 已提交
876
  // char *tptr = (char *)(pCol->pData);
H
TD-166  
hzcheng 已提交
877

H
TD-166  
hzcheng 已提交
878
  VarDataOffsetT offset = 0;
879
  for (int i = 0; i < nEle; ++i) {
H
TD-166  
hzcheng 已提交
880
    pCol->dataOff[i] = offset;
H
TD-166  
hzcheng 已提交
881
    offset += varDataTLen(tptr);
H
hzcheng 已提交
882
    tptr = POINTER_SHIFT(tptr, varDataTLen(tptr));
H
TD-166  
hzcheng 已提交
883
  }
C
Cary Xu 已提交
884
  return POINTER_SHIFT(tptr, varDataTLen(tptr));
H
TD-166  
hzcheng 已提交
885 886
}

L
Liu Jicong 已提交
887
SDataCols *tdNewDataCols(int maxCols, int maxRows) {
wafwerar's avatar
wafwerar 已提交
888
  SDataCols *pCols = (SDataCols *)taosMemoryCalloc(1, sizeof(SDataCols));
H
Haojun Liao 已提交
889
  if (pCols == NULL) {
S
Shengliang Guan 已提交
890
    uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)sizeof(SDataCols), strerror(errno));
H
Haojun Liao 已提交
891 892
    return NULL;
  }
H
TD-34  
hzcheng 已提交
893

H
Hongze Cheng 已提交
894
  pCols->maxPoints = maxRows;
L
Liu Jicong 已提交
895 896 897
  pCols->maxCols = maxCols;
  pCols->numOfRows = 0;
  pCols->numOfCols = 0;
898
  pCols->bitmapMode = TSDB_BITMODE_DEFAULT;
H
Hongze Cheng 已提交
899 900

  if (maxCols > 0) {
wafwerar's avatar
wafwerar 已提交
901
    pCols->cols = (SDataCol *)taosMemoryCalloc(maxCols, sizeof(SDataCol));
H
Hongze Cheng 已提交
902 903 904 905 906 907
    if (pCols->cols == NULL) {
      uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)sizeof(SDataCol) * maxCols,
             strerror(errno));
      tdFreeDataCols(pCols);
      return NULL;
    }
908
#if 0  // no need as calloc used
L
Liu Jicong 已提交
909
    int i;
S
Shengliang Guan 已提交
910
    for (i = 0; i < maxCols; i++) {
L
Liu Jicong 已提交
911
      pCols->cols[i].spaceSize = 0;
L
Liu Jicong 已提交
912
      pCols->cols[i].len = 0;
L
Liu Jicong 已提交
913 914 915
      pCols->cols[i].pData = NULL;
      pCols->cols[i].dataOff = NULL;
    }
916
#endif
H
Hongze Cheng 已提交
917 918
  }

H
TD-34  
hzcheng 已提交
919 920 921
  return pCols;
}

H
Hongze Cheng 已提交
922
int tdInitDataCols(SDataCols *pCols, STSchema *pSchema) {
923 924
  int i;
  int oldMaxCols = pCols->maxCols;
L
Liu Jicong 已提交
925
  if (schemaNCols(pSchema) > oldMaxCols) {
H
Hongze Cheng 已提交
926
    pCols->maxCols = schemaNCols(pSchema);
wafwerar's avatar
wafwerar 已提交
927
    void *ptr = (SDataCol *)taosMemoryRealloc(pCols->cols, sizeof(SDataCol) * pCols->maxCols);
L
Liu Jicong 已提交
928 929
    if (ptr == NULL) return -1;
    pCols->cols = ptr;
930
    for (i = oldMaxCols; i < pCols->maxCols; ++i) {
931 932
      pCols->cols[i].pData = NULL;
      pCols->cols[i].dataOff = NULL;
933
      pCols->cols[i].pBitmap = NULL;
L
Liu Jicong 已提交
934
      pCols->cols[i].spaceSize = 0;
935
    }
L
Liu Jicong 已提交
936
  }
937 938 939
#if 0
  tdResetDataCols(pCols); // redundant loop to reset len/blen to 0, already reset in following dataColInit(...)
#endif
H
Hongze Cheng 已提交
940

941
  pCols->numOfRows = 0;
942
  pCols->bitmapMode = TSDB_BITMODE_DEFAULT;
H
TD-34  
hzcheng 已提交
943 944
  pCols->numOfCols = schemaNCols(pSchema);

945
  for (i = 0; i < schemaNCols(pSchema); ++i) {
946
    dataColInit(pCols->cols + i, schemaColAt(pSchema, i), pCols->maxPoints);
H
TD-34  
hzcheng 已提交
947
  }
S
Shengliang Guan 已提交
948

H
Hongze Cheng 已提交
949
  return 0;
H
TD-34  
hzcheng 已提交
950 951
}

H
Hongze Cheng 已提交
952
SDataCols *tdFreeDataCols(SDataCols *pCols) {
953
  int i;
H
TD-34  
hzcheng 已提交
954
  if (pCols) {
S
Shengliang Guan 已提交
955
    if (pCols->cols) {
956
      int maxCols = pCols->maxCols;
957
      for (i = 0; i < maxCols; ++i) {
958
        SDataCol *pCol = &pCols->cols[i];
wafwerar's avatar
wafwerar 已提交
959
        taosMemoryFreeClear(pCol->pData);
960
      }
wafwerar's avatar
wafwerar 已提交
961
      taosMemoryFree(pCols->cols);
962 963
      pCols->cols = NULL;
    }
wafwerar's avatar
wafwerar 已提交
964
    taosMemoryFree(pCols);
H
TD-34  
hzcheng 已提交
965
  }
H
Hongze Cheng 已提交
966
  return NULL;
H
TD-34  
hzcheng 已提交
967 968 969
}

void tdResetDataCols(SDataCols *pCols) {
B
Bomin Zhang 已提交
970 971
  if (pCols != NULL) {
    pCols->numOfRows = 0;
C
Cary Xu 已提交
972
    pCols->bitmapMode = 0;
973
    for (int i = 0; i < pCols->maxCols; ++i) {
B
Bomin Zhang 已提交
974 975
      dataColReset(pCols->cols + i);
    }
H
TD-34  
hzcheng 已提交
976 977
  }
}
H
Hongze Cheng 已提交
978

H
Hongze Cheng 已提交
979
SKVRow tdKVRowDup(SKVRow row) {
wafwerar's avatar
wafwerar 已提交
980
  SKVRow trow = taosMemoryMalloc(kvRowLen(row));
H
Hongze Cheng 已提交
981 982
  if (trow == NULL) return NULL;

H
Hongze Cheng 已提交
983
  kvRowCpy(trow, row);
H
Hongze Cheng 已提交
984 985 986
  return trow;
}

S
Shengliang Guan 已提交
987 988 989
static int compareColIdx(const void *a, const void *b) {
  const SColIdx *x = (const SColIdx *)a;
  const SColIdx *y = (const SColIdx *)b;
B
Bomin Zhang 已提交
990 991 992 993 994 995 996 997 998
  if (x->colId > y->colId) {
    return 1;
  }
  if (x->colId < y->colId) {
    return -1;
  }
  return 0;
}

S
Shengliang Guan 已提交
999
void tdSortKVRowByColIdx(SKVRow row) { qsort(kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), compareColIdx); }
B
Bomin Zhang 已提交
1000

H
TD-90  
Hongze Cheng 已提交
1001 1002 1003 1004
int tdSetKVRowDataOfCol(SKVRow *orow, int16_t colId, int8_t type, void *value) {
  SColIdx *pColIdx = NULL;
  SKVRow   row = *orow;
  SKVRow   nrow = NULL;
S
Shengliang Guan 已提交
1005
  void    *ptr = taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_GE);
H
TD-90  
Hongze Cheng 已提交
1006

1007
  if (ptr == NULL || ((SColIdx *)ptr)->colId > colId) {  // need to add a column value to the row
C
Cary Xu 已提交
1008
    int diff = IS_VAR_DATA_TYPE(type) ? varDataTLen(value) : TYPE_BYTES[type];
1009 1010 1011 1012
    int nRowLen = kvRowLen(row) + sizeof(SColIdx) + diff;
    int oRowCols = kvRowNCols(row);

    ASSERT(diff > 0);
wafwerar's avatar
wafwerar 已提交
1013
    nrow = taosMemoryMalloc(nRowLen);
H
TD-90  
Hongze Cheng 已提交
1014 1015
    if (nrow == NULL) return -1;

1016 1017
    kvRowSetLen(nrow, nRowLen);
    kvRowSetNCols(nrow, oRowCols + 1);
H
TD-90  
Hongze Cheng 已提交
1018

1019 1020
    memcpy(kvRowColIdx(nrow), kvRowColIdx(row), sizeof(SColIdx) * oRowCols);
    memcpy(kvRowValues(nrow), kvRowValues(row), kvRowValLen(row));
H
TD-90  
Hongze Cheng 已提交
1021

1022 1023 1024
    pColIdx = kvRowColIdxAt(nrow, oRowCols);
    pColIdx->colId = colId;
    pColIdx->offset = kvRowValLen(row);
H
TD-90  
Hongze Cheng 已提交
1025

1026
    memcpy(kvRowColVal(nrow, pColIdx), value, diff);  // copy new value
H
TD-90  
Hongze Cheng 已提交
1027

1028
    tdSortKVRowByColIdx(nrow);
H
TD-90  
Hongze Cheng 已提交
1029 1030

    *orow = nrow;
wafwerar's avatar
wafwerar 已提交
1031
    taosMemoryFree(row);
H
TD-90  
Hongze Cheng 已提交
1032 1033 1034 1035 1036
  } else {
    ASSERT(((SColIdx *)ptr)->colId == colId);
    if (IS_VAR_DATA_TYPE(type)) {
      void *pOldVal = kvRowColVal(row, (SColIdx *)ptr);

S
Shengliang Guan 已提交
1037
      if (varDataTLen(value) == varDataTLen(pOldVal)) {  // just update the column value in place
H
TD-90  
Hongze Cheng 已提交
1038
        memcpy(pOldVal, value, varDataTLen(value));
1039 1040
      } else {  // need to reallocate the memory
        int16_t nlen = kvRowLen(row) + (varDataTLen(value) - varDataTLen(pOldVal));
H
TD-90  
Hongze Cheng 已提交
1041
        ASSERT(nlen > 0);
wafwerar's avatar
wafwerar 已提交
1042
        nrow = taosMemoryMalloc(nlen);
H
TD-90  
Hongze Cheng 已提交
1043
        if (nrow == NULL) return -1;
H
TD-90  
Hongze Cheng 已提交
1044 1045 1046 1047

        kvRowSetLen(nrow, nlen);
        kvRowSetNCols(nrow, kvRowNCols(row));

1048 1049 1050 1051 1052 1053 1054 1055
        int zsize = sizeof(SColIdx) * kvRowNCols(row) + ((SColIdx *)ptr)->offset;
        memcpy(kvRowColIdx(nrow), kvRowColIdx(row), zsize);
        memcpy(kvRowColVal(nrow, ((SColIdx *)ptr)), value, varDataTLen(value));
        // Copy left value part
        int lsize = kvRowLen(row) - TD_KV_ROW_HEAD_SIZE - zsize - varDataTLen(pOldVal);
        if (lsize > 0) {
          memcpy(POINTER_SHIFT(nrow, TD_KV_ROW_HEAD_SIZE + zsize + varDataTLen(value)),
                 POINTER_SHIFT(row, TD_KV_ROW_HEAD_SIZE + zsize + varDataTLen(pOldVal)), lsize);
H
TD-90  
Hongze Cheng 已提交
1056 1057
        }

1058 1059 1060 1061 1062
        for (int i = 0; i < kvRowNCols(nrow); i++) {
          pColIdx = kvRowColIdxAt(nrow, i);

          if (pColIdx->offset > ((SColIdx *)ptr)->offset) {
            pColIdx->offset = pColIdx->offset - varDataTLen(pOldVal) + varDataTLen(value);
H
TD-90  
Hongze Cheng 已提交
1063 1064 1065 1066
          }
        }

        *orow = nrow;
wafwerar's avatar
wafwerar 已提交
1067
        taosMemoryFree(row);
H
TD-90  
Hongze Cheng 已提交
1068 1069 1070 1071 1072 1073 1074
      }
    } else {
      memcpy(kvRowColVal(row, (SColIdx *)ptr), value, TYPE_BYTES[type]);
    }
  }

  return 0;
H
Hongze Cheng 已提交
1075 1076
}

H
TD-353  
Hongze Cheng 已提交
1077
int tdEncodeKVRow(void **buf, SKVRow row) {
H
Hongze Cheng 已提交
1078
  // May change the encode purpose
H
TD-353  
Hongze Cheng 已提交
1079 1080 1081 1082 1083 1084
  if (buf != NULL) {
    kvRowCpy(*buf, row);
    *buf = POINTER_SHIFT(*buf, kvRowLen(row));
  }

  return kvRowLen(row);
H
Hongze Cheng 已提交
1085 1086
}

H
Hongze Cheng 已提交
1087 1088
void *tdDecodeKVRow(void *buf, SKVRow *row) {
  *row = tdKVRowDup(buf);
H
TD-353  
Hongze Cheng 已提交
1089
  if (*row == NULL) return NULL;
H
Hongze Cheng 已提交
1090
  return POINTER_SHIFT(buf, kvRowLen(*row));
H
Hongze Cheng 已提交
1091 1092
}

H
Hongze Cheng 已提交
1093
int tdInitKVRowBuilder(SKVRowBuilder *pBuilder) {
H
Hongze Cheng 已提交
1094 1095
  pBuilder->tCols = 128;
  pBuilder->nCols = 0;
wafwerar's avatar
wafwerar 已提交
1096
  pBuilder->pColIdx = (SColIdx *)taosMemoryMalloc(sizeof(SColIdx) * pBuilder->tCols);
H
Hongze Cheng 已提交
1097 1098 1099
  if (pBuilder->pColIdx == NULL) return -1;
  pBuilder->alloc = 1024;
  pBuilder->size = 0;
wafwerar's avatar
wafwerar 已提交
1100
  pBuilder->buf = taosMemoryMalloc(pBuilder->alloc);
H
Hongze Cheng 已提交
1101
  if (pBuilder->buf == NULL) {
wafwerar's avatar
wafwerar 已提交
1102
    taosMemoryFree(pBuilder->pColIdx);
H
Hongze Cheng 已提交
1103 1104 1105 1106 1107
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
1108
void tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder) {
wafwerar's avatar
wafwerar 已提交
1109 1110
  taosMemoryFreeClear(pBuilder->pColIdx);
  taosMemoryFreeClear(pBuilder->buf);
H
Hongze Cheng 已提交
1111 1112
}

H
Hongze Cheng 已提交
1113
void tdResetKVRowBuilder(SKVRowBuilder *pBuilder) {
H
Hongze Cheng 已提交
1114 1115 1116 1117
  pBuilder->nCols = 0;
  pBuilder->size = 0;
}

H
Hongze Cheng 已提交
1118
SKVRow tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder) {
C
Cary Xu 已提交
1119
  int tlen = sizeof(SColIdx) * pBuilder->nCols + pBuilder->size;
1120
  // if (tlen == 0) return NULL;    // nCols == 0 means no tags
H
Hongze Cheng 已提交
1121

H
Hongze Cheng 已提交
1122 1123
  tlen += TD_KV_ROW_HEAD_SIZE;

wafwerar's avatar
wafwerar 已提交
1124
  SKVRow row = taosMemoryMalloc(tlen);
H
Hongze Cheng 已提交
1125 1126
  if (row == NULL) return NULL;

H
Hongze Cheng 已提交
1127
  kvRowSetNCols(row, pBuilder->nCols);
H
Hongze Cheng 已提交
1128
  kvRowSetLen(row, tlen);
H
Hongze Cheng 已提交
1129

1130 1131 1132 1133
  if(pBuilder->nCols > 0){
    memcpy(kvRowColIdx(row), pBuilder->pColIdx, sizeof(SColIdx) * pBuilder->nCols);
    memcpy(kvRowValues(row), pBuilder->buf, pBuilder->size);
  }
H
Hongze Cheng 已提交
1134 1135

  return row;
1136
}
H
Hongze Cheng 已提交
1137
#endif