• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3582

16 Jan 2025 05:41AM UTC coverage: 63.803% (-0.006%) from 63.809%
#3582

push

travis-ci

web-flow
Merge pull request #29579 from taosdata/test/3.0/TS-5873

Test(wal): add case for remove wal.

141331 of 284535 branches covered (49.67%)

Branch coverage included in aggregate %.

219941 of 281694 relevant lines covered (78.08%)

19199606.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.49
/source/common/src/tdataformat.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "tdataformat.h"
18
#include "tRealloc.h"
19
#include "tdatablock.h"
20
#include "tlog.h"
21

22
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData);
23
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward);
24

25
// ================================
26
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson);
27

28
// SRow ========================================================================
29
#define KV_FLG_LIT ((uint8_t)0x10)
30
#define KV_FLG_MID ((uint8_t)0x20)
31
#define KV_FLG_BIG ((uint8_t)0x40)
32

33
#define BIT_FLG_NONE  ((uint8_t)0x0)
34
#define BIT_FLG_NULL  ((uint8_t)0x1)
35
#define BIT_FLG_VALUE ((uint8_t)0x2)
36

37
#pragma pack(push, 1)
38
typedef struct {
39
  int16_t nCol;
40
  uint8_t idx[];  // uint8_t * | uint16_t * | uint32_t *
41
} SKVIdx;
42
#pragma pack(pop)
43

44
#define ROW_SET_BITMAP(PB, FLAG, IDX, VAL)      \
45
  do {                                          \
46
    switch (FLAG) {                             \
47
      case (HAS_NULL | HAS_NONE):               \
48
        SET_BIT1(PB, IDX, VAL);                 \
49
        break;                                  \
50
      case (HAS_VALUE | HAS_NONE):              \
51
        SET_BIT1(PB, IDX, (VAL) ? (VAL)-1 : 0); \
52
        break;                                  \
53
      case (HAS_VALUE | HAS_NULL):              \
54
        SET_BIT1(PB, IDX, (VAL)-1);             \
55
        break;                                  \
56
      case (HAS_VALUE | HAS_NULL | HAS_NONE):   \
57
        SET_BIT2(PB, IDX, VAL);                 \
58
        break;                                  \
59
      default:                                  \
60
        break;                                  \
61
    }                                           \
62
  } while (0)
63

64
static int32_t tPutPrimaryKeyIndex(uint8_t *p, const SPrimaryKeyIndex *index) {
611,513,485✔
65
  int32_t n = 0;
611,513,485✔
66
  n += tPutI8(p ? p + n : p, index->type);
611,513,485✔
67
  n += tPutU32v(p ? p + n : p, index->offset);
611,513,485✔
68
  return n;
611,513,485✔
69
}
70

71
static int32_t tGetPrimaryKeyIndex(uint8_t *p, SPrimaryKeyIndex *index) {
2,147,483,647✔
72
  int32_t n = 0;
2,147,483,647✔
73
  n += tGetI8(p + n, &index->type);
2,147,483,647!
74
  n += tGetU32v(p + n, &index->offset);
2,147,483,647!
75
  return n;
2,147,483,647✔
76
}
77

78
typedef struct {
79
  int32_t numOfNone;
80
  int32_t numOfNull;
81
  int32_t numOfValue;
82
  int32_t numOfPKs;
83
  int8_t  flag;
84

85
  // tuple
86
  int8_t           tupleFlag;
87
  SPrimaryKeyIndex tupleIndices[TD_MAX_PK_COLS];
88
  int32_t          tuplePKSize;      // primary key size
89
  int32_t          tupleBitmapSize;  // bitmap size
90
  int32_t          tupleFixedSize;   // fixed part size
91
  int32_t          tupleVarSize;     // var part size
92
  int32_t          tupleRowSize;
93

94
  // key-value
95
  int8_t           kvFlag;
96
  SPrimaryKeyIndex kvIndices[TD_MAX_PK_COLS];
97
  int32_t          kvMaxOffset;
98
  int32_t          kvPKSize;       // primary key size
99
  int32_t          kvIndexSize;    // offset array size
100
  int32_t          kvPayloadSize;  // payload size
101
  int32_t          kvRowSize;
102
} SRowBuildScanInfo;
103

104
static FORCE_INLINE int32_t tRowBuildScanAddNone(SRowBuildScanInfo *sinfo, const STColumn *pTColumn) {
105
  if ((pTColumn->flags & COL_IS_KEY)) return TSDB_CODE_PAR_PRIMARY_KEY_IS_NONE;
×
106
  sinfo->numOfNone++;
2,147,483,647✔
107
  return 0;
2,147,483,647✔
108
}
109

110
static FORCE_INLINE int32_t tRowBuildScanAddNull(SRowBuildScanInfo *sinfo, const STColumn *pTColumn) {
111
  if ((pTColumn->flags & COL_IS_KEY)) return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
112
  sinfo->numOfNull++;
28,931,801✔
113
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
28,931,801✔
114
  sinfo->kvPayloadSize += tPutI16v(NULL, -pTColumn->colId);
28,931,801✔
115
  return 0;
28,931,801✔
116
}
117

118
static FORCE_INLINE void tRowBuildScanAddValue(SRowBuildScanInfo *sinfo, SColVal *colVal, const STColumn *pTColumn) {
119
  bool isPK = ((pTColumn->flags & COL_IS_KEY) != 0);
2,147,483,647✔
120

121
  if (isPK) {
2,147,483,647✔
122
    sinfo->tupleIndices[sinfo->numOfPKs].type = colVal->value.type;
206,379,263✔
123
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
206,379,263✔
124
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
206,379,263!
125
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
206,379,263✔
126
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
206,379,263✔
127
    sinfo->numOfPKs++;
206,379,263✔
128
  }
129

130
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
2,147,483,647✔
131
  if (IS_VAR_DATA_TYPE(colVal->value.type)) {
2,147,483,647!
132
    sinfo->tupleVarSize += tPutU32v(NULL, colVal->value.nData)  // size
623,635,819✔
133
                           + colVal->value.nData;               // value
623,635,819✔
134

135
    sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
623,635,819✔
136
                            + tPutU32v(NULL, colVal->value.nData)  // size
623,635,819✔
137
                            + colVal->value.nData;                 // value
623,635,819✔
138
  } else {
139
    sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)              // colId
2,147,483,647✔
140
                            + tDataTypes[colVal->value.type].bytes;  // value
2,147,483,647✔
141
  }
142
  sinfo->numOfValue++;
2,147,483,647✔
143
}
2,147,483,647✔
144

145
static int32_t tRowBuildScan(SArray *colVals, const STSchema *schema, SRowBuildScanInfo *sinfo) {
1,085,718,651✔
146
  int32_t  code = 0;
1,085,718,651✔
147
  int32_t  colValIndex = 1;
1,085,718,651✔
148
  int32_t  numOfColVals = TARRAY_SIZE(colVals);
1,085,718,651✔
149
  SColVal *colValArray = (SColVal *)TARRAY_DATA(colVals);
1,085,718,651✔
150

151
  if (!(numOfColVals > 0)) {
1,085,718,651!
152
    return TSDB_CODE_INVALID_PARA;
×
153
  }
154
  if (!(colValArray[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
1,085,718,651!
155
    return TSDB_CODE_INVALID_PARA;
×
156
  }
157
  if (!(colValArray[0].value.type == TSDB_DATA_TYPE_TIMESTAMP)) {
1,085,718,651!
158
    return TSDB_CODE_INVALID_PARA;
×
159
  }
160

161
  *sinfo = (SRowBuildScanInfo){
1,085,718,651✔
162
      .tupleFixedSize = schema->flen,
1,085,718,651✔
163
  };
164

165
  // loop scan
166
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
167
    for (;;) {
168
      if (colValIndex >= numOfColVals) {
2,147,483,647✔
169
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
88!
170
        break;
44✔
171
      }
172

173
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
174
        if (!(colValArray[colValIndex].value.type == schema->columns[i].type)) {
2,147,483,647!
175
          code = TSDB_CODE_INVALID_PARA;
×
176
          goto _exit;
×
177
        }
178

179
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {
2,147,483,647✔
180
          tRowBuildScanAddValue(sinfo, &colValArray[colValIndex], schema->columns + i);
2,147,483,647✔
181
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {
2,147,483,647✔
182
          if ((code = tRowBuildScanAddNull(sinfo, schema->columns + i))) goto _exit;
57,863,602!
183
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {
2,147,483,647!
184
          if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
2,147,483,647!
185
        }
186

187
        colValIndex++;
2,147,483,647✔
188
        break;
2,147,483,647✔
189
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {
7,503,304✔
190
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
62!
191
        break;
31✔
192
      } else {  // skip useless value
193
        colValIndex++;
7,503,273✔
194
      }
195
    }
196
  }
197

198
  if (sinfo->numOfNone) {
1,085,718,651✔
199
    sinfo->flag |= HAS_NONE;
411,122,957✔
200
  }
201
  if (sinfo->numOfNull) {
1,085,718,651✔
202
    sinfo->flag |= HAS_NULL;
20,124,727✔
203
  }
204
  if (sinfo->numOfValue) {
1,085,718,651✔
205
    sinfo->flag |= HAS_VALUE;
1,011,288,205✔
206
  }
207

208
  // Tuple
209
  sinfo->tupleFlag = sinfo->flag;
1,085,718,651✔
210
  switch (sinfo->flag) {
1,085,718,651!
211
    case HAS_NONE:
81,477,814✔
212
    case HAS_NULL:
213
      sinfo->tupleBitmapSize = 0;
81,477,814✔
214
      sinfo->tupleFixedSize = 0;
81,477,814✔
215
      break;
81,477,814✔
216
    case HAS_VALUE:
663,021,113✔
217
      sinfo->tupleBitmapSize = 0;
663,021,113✔
218
      sinfo->tupleFixedSize = schema->flen;
663,021,113✔
219
      break;
663,021,113✔
220
    case (HAS_NONE | HAS_NULL):
24,405✔
221
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
24,405✔
222
      sinfo->tupleFixedSize = 0;
24,405✔
223
      break;
24,405✔
224
    case (HAS_NONE | HAS_VALUE):
348,978,656✔
225
    case (HAS_NULL | HAS_VALUE):
226
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
348,978,656✔
227
      sinfo->tupleFixedSize = schema->flen;
348,978,656✔
228
      break;
348,978,656✔
229
    case (HAS_NONE | HAS_NULL | HAS_VALUE):
452,291✔
230
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
452,291✔
231
      sinfo->tupleFixedSize = schema->flen;
452,291✔
232
      break;
452,291✔
233
  }
234
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
1,292,848,542✔
235
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
207,441,892✔
236
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
207,441,892✔
237
  }
238
  sinfo->tupleRowSize = sizeof(SRow)              // SRow
1,085,406,650✔
239
                        + sinfo->tuplePKSize      // primary keys
1,085,406,650✔
240
                        + sinfo->tupleBitmapSize  // bitmap
1,085,406,650✔
241
                        + sinfo->tupleFixedSize   // fixed part
1,085,406,650✔
242
                        + sinfo->tupleVarSize;    // var part
1,085,406,650✔
243

244
  // Key-Value
245
  if (sinfo->kvMaxOffset <= UINT8_MAX) {
1,085,406,650!
246
    sinfo->kvFlag = (KV_FLG_LIT | sinfo->flag);
1,088,972,169✔
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint8_t);
1,088,972,169✔
248
  } else if (sinfo->kvMaxOffset <= UINT16_MAX) {
×
249
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
4,659,074✔
250
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
4,659,074✔
251
  } else {
252
    sinfo->kvFlag = (KV_FLG_BIG | sinfo->flag);
×
253
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint32_t);
×
254
  }
255
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
1,291,979,164✔
256
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
206,918,587✔
257
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
206,918,587✔
258
  }
259
  sinfo->kvRowSize = sizeof(SRow)             // SRow
1,085,060,577✔
260
                     + sinfo->kvPKSize        // primary keys
1,085,060,577✔
261
                     + sinfo->kvIndexSize     // index array
1,085,060,577✔
262
                     + sinfo->kvPayloadSize;  // payload
1,085,060,577✔
263

264
_exit:
1,085,060,577✔
265
  return code;
1,085,060,577✔
266
}
267

268
static int32_t tRowBuildTupleRow(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
755,878,693✔
269
                                 SRow **ppRow) {
270
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
755,878,693✔
271

272
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->tupleRowSize);
755,878,693!
273
  if (*ppRow == NULL) {
764,853,490!
274
    return terrno;
×
275
  }
276
  (*ppRow)->flag = sinfo->tupleFlag;
764,853,490✔
277
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
764,853,490✔
278
  (*ppRow)->sver = schema->version;
764,853,490✔
279
  (*ppRow)->len = sinfo->tupleRowSize;
764,853,490✔
280
  (*ppRow)->ts = colValArray[0].value.val;
764,853,490✔
281

282
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
764,853,490✔
283
    return 0;
85,822,426✔
284
  }
285

286
  uint8_t *primaryKeys = (*ppRow)->data;
679,031,064✔
287
  uint8_t *bitmap = primaryKeys + sinfo->tuplePKSize;
679,031,064✔
288
  uint8_t *fixed = bitmap + sinfo->tupleBitmapSize;
679,031,064✔
289
  uint8_t *varlen = fixed + sinfo->tupleFixedSize;
679,031,064✔
290

291
  // primary keys
292
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
884,783,394✔
293
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
207,596,511✔
294
  }
295

296
  // bitmap + fixed + varlen
297
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
677,186,883✔
298
  int32_t colValIndex = 1;
677,186,883✔
299
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
300
    for (;;) {
301
      if (colValIndex >= numOfColVals) {  // NONE
2,147,483,647!
302
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
303
        break;
×
304
      }
305

306
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
307
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
2,147,483,647✔
308
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_VALUE);
2,147,483,647!
309

310
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647!
311
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
170,211,334✔
312
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
170,211,334✔
313
            if (colValArray[colValIndex].value.nData) {
170,211,334!
314
              (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
172,775,433✔
315
              varlen += colValArray[colValIndex].value.nData;
172,775,433✔
316
            }
317
          } else {
318
            (void)memcpy(fixed + schema->columns[i].offset, &colValArray[colValIndex].value.val,
2,147,483,647✔
319
                         tDataTypes[schema->columns[i].type].bytes);
2,147,483,647✔
320
          }
321
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
15,065,897!
322
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
15,494,141!
323
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
324
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
461,543!
325
        }
326

327
        colValIndex++;
2,147,483,647✔
328
        break;
2,147,483,647✔
329
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
28,017✔
330
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
9!
331
        break;
9✔
332
      } else {
333
        colValIndex++;
28,008✔
334
      }
335
    }
336
  }
337

338
  return 0;
677,186,883✔
339
}
340

341
static FORCE_INLINE void tRowBuildKVRowSetIndex(uint8_t flag, SKVIdx *indices, uint32_t offset) {
342
  if (flag & KV_FLG_LIT) {
2,147,483,647✔
343
    ((uint8_t *)indices->idx)[indices->nCol] = (uint8_t)offset;
2,147,483,647✔
344
  } else if (flag & KV_FLG_MID) {
5,850,971!
345
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
7,550,971✔
346
  } else {
347
    ((uint32_t *)indices->idx)[indices->nCol] = (uint32_t)offset;
×
348
  }
349
  indices->nCol++;
2,147,483,647✔
350
}
2,147,483,647✔
351

352
static int32_t tRowBuildKVRow(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema, SRow **ppRow) {
336,366,759✔
353
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
336,366,759✔
354

355
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->kvRowSize);
336,366,759!
356
  if (*ppRow == NULL) {
337,131,820!
357
    return terrno;
×
358
  }
359
  (*ppRow)->flag = sinfo->kvFlag;
337,131,820✔
360
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
337,131,820✔
361
  (*ppRow)->sver = schema->version;
337,131,820✔
362
  (*ppRow)->len = sinfo->kvRowSize;
337,131,820✔
363
  (*ppRow)->ts = colValArray[0].value.val;
337,131,820✔
364

365
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
337,131,820!
366
    return TSDB_CODE_INVALID_PARA;
9,426✔
367
  }
368

369
  uint8_t *primaryKeys = (*ppRow)->data;
337,122,394✔
370
  SKVIdx  *indices = (SKVIdx *)(primaryKeys + sinfo->kvPKSize);
337,122,394✔
371
  uint8_t *payload = primaryKeys + sinfo->kvPKSize + sinfo->kvIndexSize;
337,122,394✔
372
  uint32_t payloadSize = 0;
337,122,394✔
373

374
  // primary keys
375
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
338,748,551✔
376
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
1,626,163✔
377
  }
378

379
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
337,122,388✔
380
  int32_t colValIndex = 1;
337,122,388✔
381
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
382
    for (;;) {
383
      if (colValIndex >= numOfColVals) {  // NONE
2,147,483,647✔
384
        break;
44✔
385
      }
386

387
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647!
388
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
2,147,483,647✔
389
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
2,147,483,647✔
390
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647!
391
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
456,495,150✔
392
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
456,495,150✔
393
            if (colValArray[colValIndex].value.nData > 0) {
456,495,150!
394
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
469,051,426✔
395
                           colValArray[colValIndex].value.nData);
469,051,426✔
396
            }
397
            payloadSize += colValArray[colValIndex].value.nData;
456,495,150✔
398
          } else {
399
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,147,483,647✔
400
            (void)memcpy(payload + payloadSize, &colValArray[colValIndex].value.val,
2,147,483,647✔
401
                         tDataTypes[schema->columns[i].type].bytes);
2,147,483,647✔
402
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
2,147,483,647✔
403
          }
404
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
2,147,483,647✔
405
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
13,094,430✔
406
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
26,188,860✔
407
        }
408

409
        colValIndex++;
2,147,483,647✔
410
        break;
2,147,483,647✔
411
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
412
        break;
22✔
413
      } else {
414
        colValIndex++;
×
415
      }
416
    }
417
  }
418

419
  return 0;
337,122,388✔
420
}
421

422
int32_t tRowBuild(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow) {
1,085,968,650✔
423
  int32_t           code;
424
  SRowBuildScanInfo sinfo;
425

426
  code = tRowBuildScan(aColVal, pTSchema, &sinfo);
1,085,968,650✔
427
  if (code) return code;
1,093,096,430!
428

429
  if (sinfo.tupleRowSize <= sinfo.kvRowSize) {
1,093,096,430✔
430
    code = tRowBuildTupleRow(aColVal, &sinfo, pTSchema, ppRow);
756,955,754✔
431
  } else {
432
    code = tRowBuildKVRow(aColVal, &sinfo, pTSchema, ppRow);
336,140,676✔
433
  }
434
  return code;
1,097,110,732✔
435
}
436

437
static int32_t tBindInfoCompare(const void *p1, const void *p2, const void *param) {
×
438
  if (((SBindInfo *)p1)->columnId < ((SBindInfo *)p2)->columnId) {
×
439
    return -1;
×
440
  } else if (((SBindInfo *)p1)->columnId > ((SBindInfo *)p2)->columnId) {
×
441
    return 1;
×
442
  }
443
  return 0;
×
444
}
445

446
/* build rows to `rowArray` from bind
447
 * `infos` is the bind information array
448
 * `numOfInfos` is the number of bind information
449
 * `infoSorted` is whether the bind information is sorted by column id
450
 * `pTSchema` is the schema of the table
451
 * `rowArray` is the array to store the rows
452
 * `pOrdered` is the pointer to store ordered
453
 * `pDupTs` is the pointer to store duplicateTs
454
 */
455
int32_t tRowBuildFromBind(SBindInfo *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
5,492✔
456
                          SArray *rowArray, bool *pOrdered, bool *pDupTs) {
457
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
5,492!
458
    return TSDB_CODE_INVALID_PARA;
×
459
  }
460

461
  if (!infoSorted) {
5,493!
462
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
463
  }
464

465
  int32_t code = 0;
5,493✔
466
  int32_t numOfRows = infos[0].bind->num;
5,493✔
467
  SArray *colValArray;
468
  SColVal colVal;
469

470
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
5,493!
471
    return terrno;
×
472
  }
473

474
  SRowKey rowKey, lastRowKey;
475
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
16,477✔
476
    taosArrayClear(colValArray);
10,986✔
477

478
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
164,552✔
479
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
153,582✔
480
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
650✔
481
      } else {
482
        SValue value = {
152,932✔
483
            .type = infos[iInfo].type,
152,932✔
484
        };
485
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
152,932!
486
          value.nData = infos[iInfo].bind->length[iRow];
21,817✔
487
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
21,817!
488
            code = TSDB_CODE_INVALID_PARA;
×
489
            goto _exit;
×
490
          }
491
          value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
21,817✔
492
        } else {
493
          (void)memcpy(&value.val, (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
131,115✔
494
                       infos[iInfo].bind->buffer_length);
131,115✔
495
        }
496
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
152,932✔
497
      }
498
      if (taosArrayPush(colValArray, &colVal) == NULL) {
153,566!
499
        code = terrno;
×
500
        goto _exit;
×
501
      }
502
    }
503

504
    SRow *row;
505
    if ((code = tRowBuild(colValArray, pTSchema, &row))) {
10,970!
506
      goto _exit;
×
507
    }
508

509
    if ((taosArrayPush(rowArray, &row)) == NULL) {
10,983!
510
      code = terrno;
×
511
      goto _exit;
×
512
    }
513

514
    if (pOrdered && pDupTs) {
10,983!
515
      tRowGetKey(row, &rowKey);
10,983!
516
      if (iRow == 0) {
10,984✔
517
        *pOrdered = true;
5,493✔
518
        *pDupTs = false;
5,493✔
519
      } else {
520
        // no more compare if we already get disordered or duplicate rows
521
        if (*pOrdered && !*pDupTs) {
5,491!
522
          int32_t code = tRowKeyCompare(&rowKey, &lastRowKey);
5,491✔
523
          *pOrdered = (code >= 0);
5,491✔
524
          *pDupTs = (code == 0);
5,491✔
525
        }
526
      }
527
      lastRowKey = rowKey;
10,984✔
528
    }
529
  }
530

531
_exit:
5,491✔
532
  taosArrayDestroy(colValArray);
5,491✔
533
  return code;
5,493✔
534
}
535

536
int32_t tRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
2,147,483,647✔
537
  if (!(iCol < pTSchema->numOfCols)) return TSDB_CODE_INVALID_PARA;
2,147,483,647!
538
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
2,147,483,647!
539

540
  STColumn *pTColumn = pTSchema->columns + iCol;
2,147,483,647✔
541

542
  if (iCol == 0) {
2,147,483,647✔
543
    pColVal->cid = pTColumn->colId;
63,901,594✔
544
    pColVal->value.type = pTColumn->type;
63,901,594✔
545
    pColVal->flag = CV_FLAG_VALUE;
63,901,594✔
546
    (void)memcpy(&pColVal->value.val, &pRow->ts, sizeof(TSKEY));
63,901,594✔
547
    return 0;
63,901,594✔
548
  }
549

550
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
551
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
389,438✔
552
    return 0;
389,438✔
553
  }
554

555
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
556
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
952,853✔
557
    return 0;
952,853✔
558
  }
559

560
  SPrimaryKeyIndex index;
561
  uint8_t         *data = pRow->data;
2,147,483,647✔
562
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
2,147,483,647✔
563
    data += tGetPrimaryKeyIndex(data, &index);
76,659,666✔
564
  }
565

566
  if (pRow->flag >> 4) {  // KV Row
2,147,483,647✔
567
    SKVIdx  *pIdx = (SKVIdx *)data;
2,147,483,647✔
568
    uint8_t *pv = NULL;
2,147,483,647✔
569

570
    if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
571
      pv = pIdx->idx + pIdx->nCol;
2,147,483,647✔
572
    } else if (pRow->flag & KV_FLG_MID) {
66,384,858!
573
      pv = pIdx->idx + (pIdx->nCol << 1);
93,089,610✔
574
    } else {
575
      pv = pIdx->idx + (pIdx->nCol << 2);
×
576
    }
577

578
    int16_t lidx = 0;
2,147,483,647✔
579
    int16_t ridx = pIdx->nCol - 1;
2,147,483,647✔
580
    while (lidx <= ridx) {
2,147,483,647✔
581
      int16_t  mid = (lidx + ridx) >> 1;
2,147,483,647✔
582
      uint8_t *pData = NULL;
2,147,483,647✔
583
      if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
584
        pData = pv + ((uint8_t *)pIdx->idx)[mid];
2,147,483,647✔
585
      } else if (pRow->flag & KV_FLG_MID) {
391,029,196!
586
        pData = pv + ((uint16_t *)pIdx->idx)[mid];
391,109,321✔
587
      } else {
588
        pData = pv + ((uint32_t *)pIdx->idx)[mid];
×
589
      }
590

591
      int16_t cid;
592
      pData += tGetI16v(pData, &cid);
2,147,483,647✔
593

594
      if (TABS(cid) == pTColumn->colId) {
2,147,483,647✔
595
        if (cid < 0) {
2,147,483,647✔
596
          *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
88,322,583✔
597
        } else {
598
          pColVal->cid = pTColumn->colId;
2,147,483,647✔
599
          pColVal->value.type = pTColumn->type;
2,147,483,647✔
600
          pColVal->flag = CV_FLAG_VALUE;
2,147,483,647✔
601

602
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647!
603
            pData += tGetU32v(pData, &pColVal->value.nData);
702,892,041!
604
            if (pColVal->value.nData > 0) {
702,892,041!
605
              pColVal->value.pData = pData;
829,576,869✔
606
            } else {
607
              pColVal->value.pData = NULL;
×
608
            }
609
          } else {
610
            (void)memcpy(&pColVal->value.val, pData, pTColumn->bytes);
2,147,483,647✔
611
          }
612
        }
613
        return 0;
2,147,483,647✔
614
      } else if (TABS(cid) < pTColumn->colId) {
2,147,483,647✔
615
        lidx = mid + 1;
2,147,483,647✔
616
      } else {
617
        ridx = mid - 1;
2,147,483,647✔
618
      }
619
    }
620

621
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
2,068,340,054✔
622
  } else {  // Tuple Row
623
    uint8_t *bitmap = data;
805,947,541✔
624
    uint8_t *fixed;
625
    uint8_t *varlen;
626
    uint8_t  bit;
627

628
    if (pRow->flag == HAS_VALUE) {
805,947,541✔
629
      fixed = bitmap;
737,019,795✔
630
      bit = BIT_FLG_VALUE;
737,019,795✔
631
    } else if (pRow->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
68,927,746!
632
      fixed = BIT2_SIZE(pTSchema->numOfCols - 1) + bitmap;
×
633
      bit = GET_BIT2(bitmap, iCol - 1);
×
634
    } else {
635
      fixed = BIT1_SIZE(pTSchema->numOfCols - 1) + bitmap;
68,927,746✔
636
      bit = GET_BIT1(bitmap, iCol - 1);
68,927,746✔
637

638
      if (pRow->flag == (HAS_NONE | HAS_VALUE)) {
68,927,746✔
639
        if (bit) bit++;
20,375✔
640
      } else if (pRow->flag == (HAS_NULL | HAS_VALUE)) {
68,907,371!
641
        bit++;
94,520,381✔
642
      }
643
    }
644
    varlen = fixed + pTSchema->flen;
805,947,541✔
645

646
    if (bit == BIT_FLG_NONE) {
805,947,541✔
647
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
7,239✔
648
      return 0;
7,239✔
649
    } else if (bit == BIT_FLG_NULL) {
805,940,302✔
650
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
9,980,540✔
651
      return 0;
9,980,540✔
652
    }
653

654
    pColVal->cid = pTColumn->colId;
795,959,762✔
655
    pColVal->value.type = pTColumn->type;
795,959,762✔
656
    pColVal->flag = CV_FLAG_VALUE;
795,959,762✔
657
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
795,959,762!
658
      pColVal->value.pData = varlen + *(int32_t *)(fixed + pTColumn->offset);
35,940,485✔
659
      pColVal->value.pData += tGetU32v(pColVal->value.pData, &pColVal->value.nData);
71,880,970!
660
    } else {
661
      (void)memcpy(&pColVal->value.val, fixed + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
760,019,277✔
662
    }
663
  }
664

665
  return 0;
2,147,483,647✔
666
}
667

668
void tRowDestroy(SRow *pRow) {
677,599,350✔
669
  if (pRow) taosMemoryFree(pRow);
677,599,350!
670
}
677,589,008✔
671

672
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
275,404,401✔
673
  SRowKey key1, key2;
674
  tRowGetKey(*(SRow **)p1, &key1);
275,404,401✔
675
  tRowGetKey(*(SRow **)p2, &key2);
271,357,260✔
676
  return tRowKeyCompare(&key1, &key2);
276,177,503✔
677
}
678
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
1,034✔
679
static int32_t tRowMergeImpl(SArray *aRowP, STSchema *pTSchema, int32_t iStart, int32_t iEnd, int8_t flag) {
253✔
680
  int32_t code = 0;
253✔
681

682
  int32_t    nRow = iEnd - iStart;
253✔
683
  SRowIter **aIter = NULL;
253✔
684
  SArray    *aColVal = NULL;
253✔
685
  SRow      *pRow = NULL;
253✔
686

687
  aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
253!
688
  if (aIter == NULL) {
253!
689
    code = terrno;
×
690
    goto _exit;
×
691
  }
692

693
  for (int32_t i = 0; i < nRow; i++) {
1,287✔
694
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
1,034✔
695

696
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
1,034✔
697
    if (code) goto _exit;
1,034!
698
  }
699

700
  // merge
701
  aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
253✔
702
  if (aColVal == NULL) {
253!
703
    code = terrno;
×
704
    goto _exit;
×
705
  }
706

707
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
973✔
708
    SColVal *pColVal = NULL;
720✔
709
    for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
862✔
710
      SColVal *pColValT = tRowIterNext(aIter[iRow]);
818✔
711
      while (pColValT->cid < pTSchema->columns[iCol].colId) {
916✔
712
        pColValT = tRowIterNext(aIter[iRow]);
98✔
713
      }
714

715
      // todo: take strategy according to the flag
716
      if (COL_VAL_IS_VALUE(pColValT)) {
818✔
717
        pColVal = pColValT;
676✔
718
        break;
676✔
719
      } else if (COL_VAL_IS_NULL(pColValT)) {
142✔
720
        if (pColVal == NULL) {
2✔
721
          pColVal = pColValT;
1✔
722
        }
723
      }
724
    }
725

726
    if (pColVal) {
720✔
727
      if (taosArrayPush(aColVal, pColVal) == NULL) {
677!
728
        code = terrno;
×
729
        goto _exit;
×
730
      }
731
    }
732
  }
733

734
  // build
735
  code = tRowBuild(aColVal, pTSchema, &pRow);
253✔
736
  if (code) goto _exit;
253!
737

738
  taosArrayRemoveBatch(aRowP, iStart, nRow, (FDelete)tRowPDestroy);
253✔
739
  if (taosArrayInsert(aRowP, iStart, &pRow) == NULL) {
253!
740
    code = terrno;
×
741
    goto _exit;
×
742
  }
743

744
_exit:
253✔
745
  if (aIter) {
253!
746
    for (int32_t i = 0; i < nRow; i++) {
1,287✔
747
      tRowIterClose(&aIter[i]);
1,034✔
748
    }
749
    taosMemoryFree(aIter);
253!
750
  }
751
  if (aColVal) taosArrayDestroy(aColVal);
253!
752
  if (code) tRowDestroy(pRow);
253!
753
  return code;
253✔
754
}
755

756
int32_t tRowSort(SArray *aRowP) {
34,359✔
757
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
34,359✔
758
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
34,356✔
759
  if (code != TSDB_CODE_SUCCESS) {
34,356!
760
    uError("taosArrayMSort failed caused by %d", code);
×
761
  }
762
  return code;
34,356✔
763
}
764

765
int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, int8_t flag) {
34,408✔
766
  int32_t code = 0;
34,408✔
767

768
  int32_t iStart = 0;
34,408✔
769
  while (iStart < aRowP->size) {
38,657,276!
770
    SRowKey key1;
771
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
38,671,103✔
772

773
    tRowGetKey(row1, &key1);
38,765,119✔
774

775
    int32_t iEnd = iStart + 1;
38,627,951✔
776
    while (iEnd < aRowP->size) {
38,608,622✔
777
      SRowKey key2;
778
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
38,598,804✔
779
      tRowGetKey(row2, &key2);
38,705,730✔
780

781
      if (tRowKeyCompare(&key1, &key2) != 0) break;
38,593,721!
782

783
      iEnd++;
×
784
    }
785

786
    if (iEnd - iStart > 1) {
38,622,868✔
787
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, flag);
253✔
788
      if (code) return code;
253!
789
    }
790

791
    // the array is also changing, so the iStart just ++ instead of iEnd
792
    iStart++;
38,622,868✔
793
  }
794

795
  return code;
×
796
}
797

798
// SRowIter ========================================
799
struct SRowIter {
800
  SRow     *pRow;
801
  STSchema *pTSchema;
802

803
  int32_t iTColumn;
804
  union {
805
    struct {  // kv
806
      int32_t iCol;
807
      SKVIdx *pIdx;
808
    };
809
    struct {  // tuple
810
      uint8_t *pb;
811
      uint8_t *pf;
812
    };
813
  };
814
  uint8_t *pv;
815
  SColVal  cv;
816
};
817

818
int32_t tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter) {
1,408,217✔
819
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
1,408,217!
820

821
  int32_t code = 0;
1,408,217✔
822

823
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
1,408,217!
824
  if (pIter == NULL) {
1,482,298!
825
    code = terrno;
×
826
    goto _exit;
×
827
  }
828

829
  pIter->pRow = pRow;
1,482,298✔
830
  pIter->pTSchema = pTSchema;
1,482,298✔
831
  pIter->iTColumn = 0;
1,482,298✔
832

833
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
1,482,298!
834

835
  uint8_t         *data = pRow->data;
1,483,789✔
836
  SPrimaryKeyIndex index;
837
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,483,828✔
838
    data += tGetPrimaryKeyIndex(data, &index);
39✔
839
  }
840

841
  if (pRow->flag >> 4) {
1,483,789✔
842
    pIter->iCol = 0;
1,481,536✔
843
    pIter->pIdx = (SKVIdx *)data;
1,481,536✔
844
    if (pRow->flag & KV_FLG_LIT) {
1,481,536✔
845
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
1,479,807✔
846
    } else if (pRow->flag & KV_FLG_MID) {
1,729!
847
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
3,600✔
848
    } else {
849
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
850
    }
851
  } else {
852
    switch (pRow->flag) {
2,253!
853
      case (HAS_NULL | HAS_NONE):
1✔
854
        pIter->pb = data;
1✔
855
        break;
1✔
856
      case HAS_VALUE:
2,395✔
857
        pIter->pf = data;
2,395✔
858
        pIter->pv = pIter->pf + pTSchema->flen;
2,395✔
859
        break;
2,395✔
860
      case (HAS_VALUE | HAS_NONE):
719✔
861
      case (HAS_VALUE | HAS_NULL):
862
        pIter->pb = data;
719✔
863
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
719✔
864
        pIter->pv = pIter->pf + pTSchema->flen;
719✔
865
        break;
719✔
866
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
867
        pIter->pb = data;
×
868
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
869
        pIter->pv = pIter->pf + pTSchema->flen;
×
870
        break;
×
871
      default:
×
872
        break;
×
873
    }
874
  }
875

876
_exit:
1,482,298✔
877
  if (code) {
1,482,298!
878
    *ppIter = NULL;
×
879
  } else {
880
    *ppIter = pIter;
1,482,298✔
881
  }
882
  return code;
1,482,298✔
883
}
884

885
void tRowIterClose(SRowIter **ppIter) {
1,382,540✔
886
  SRowIter *pIter = *ppIter;
1,382,540✔
887
  if (pIter) {
1,382,540!
888
    taosMemoryFree(pIter);
1,382,908!
889
  }
890
  *ppIter = NULL;
1,493,450✔
891
}
1,493,450✔
892

893
SColVal *tRowIterNext(SRowIter *pIter) {
14,662,692✔
894
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
14,662,692✔
895
    return NULL;
1,387,632✔
896
  }
897

898
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
13,275,060✔
899

900
  // timestamp
901
  if (0 == pIter->iTColumn) {
13,275,060✔
902
    pIter->cv.cid = pTColumn->colId;
1,441,483✔
903
    pIter->cv.value.type = pTColumn->type;
1,441,483✔
904
    pIter->cv.flag = CV_FLAG_VALUE;
1,441,483✔
905
    (void)memcpy(&pIter->cv.value.val, &pIter->pRow->ts, sizeof(TSKEY));
1,441,483✔
906
    goto _exit;
1,441,483✔
907
  }
908

909
  if (pIter->pRow->flag == HAS_NONE) {
11,833,577✔
910
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1✔
911
    goto _exit;
1✔
912
  }
913

914
  if (pIter->pRow->flag == HAS_NULL) {
11,833,576✔
915
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,841✔
916
    goto _exit;
1,841✔
917
  }
918

919
  if (pIter->pRow->flag >> 4) {  // KV
11,831,735!
920
    if (pIter->iCol < pIter->pIdx->nCol) {
12,908,670✔
921
      uint8_t *pData;
922

923
      if (pIter->pRow->flag & KV_FLG_LIT) {
11,409,539✔
924
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
11,327,508✔
925
      } else if (pIter->pRow->flag & KV_FLG_MID) {
82,031!
926
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
108,000✔
927
      } else {
928
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
×
929
      }
930

931
      int16_t cid;
932
      pData += tGetI16v(pData, &cid);
11,409,539✔
933

934
      if (TABS(cid) == pTColumn->colId) {
11,409,539!
935
        if (cid < 0) {
11,646,878✔
936
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
2,366,579✔
937
        } else {
938
          pIter->cv.cid = pTColumn->colId;
9,280,299✔
939
          pIter->cv.value.type = pTColumn->type;
9,280,299✔
940
          pIter->cv.flag = CV_FLAG_VALUE;
9,280,299✔
941

942
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
9,280,299!
943
            pData += tGetU32v(pData, &pIter->cv.value.nData);
804,969!
944
            if (pIter->cv.value.nData > 0) {
804,969!
945
              pIter->cv.value.pData = pData;
2,819,421✔
946
            } else {
947
              pIter->cv.value.pData = NULL;
×
948
            }
949
          } else {
950
            (void)memcpy(&pIter->cv.value.val, pData, pTColumn->bytes);
8,475,330✔
951
          }
952
        }
953

954
        pIter->iCol++;
11,646,878✔
955
        goto _exit;
11,646,878✔
956
      } else if (TABS(cid) > pTColumn->colId) {
×
957
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
94✔
958
        goto _exit;
94✔
959
      } else {
960
        uError("unexpected column id %d, %d", cid, pTColumn->colId);
×
961
        goto _exit;
×
962
      }
963
    } else {
964
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,499,131✔
965
      goto _exit;
1,499,131✔
966
    }
967
  } else {  // Tuple
968
    uint8_t bv = BIT_FLG_VALUE;
×
969
    if (pIter->pb) {
×
970
      switch (pIter->pRow->flag) {
3,002!
971
        case (HAS_NULL | HAS_NONE):
3✔
972
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
3✔
973
          break;
3✔
974
        case (HAS_VALUE | HAS_NONE):
37✔
975
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
37✔
976
          if (bv) bv++;
37✔
977
          break;
37✔
978
        case (HAS_VALUE | HAS_NULL):
2,962✔
979
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
2,962✔
980
          break;
2,962✔
981
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
982
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
983
          break;
×
984
        default:
×
985
          break;
×
986
      }
987

988
      if (bv == BIT_FLG_NONE) {
3,002✔
989
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
13✔
990
        goto _exit;
13✔
991
      } else if (bv == BIT_FLG_NULL) {
2,989✔
992
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
325✔
993
        goto _exit;
325✔
994
      }
995
    }
996

997
    pIter->cv.cid = pTColumn->colId;
×
998
    pIter->cv.value.type = pTColumn->type;
×
999
    pIter->cv.flag = CV_FLAG_VALUE;
×
1000
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
×
1001
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
×
1002
      pData += tGetU32v(pData, &pIter->cv.value.nData);
×
1003
      if (pIter->cv.value.nData > 0) {
×
1004
        pIter->cv.value.pData = pData;
1,963✔
1005
      } else {
1006
        pIter->cv.value.pData = NULL;
×
1007
      }
1008
    } else {
1009
      (void)memcpy(&pIter->cv.value.val, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
27,188✔
1010
    }
1011
    goto _exit;
×
1012
  }
1013

1014
_exit:
13,512,493✔
1015
  pIter->iTColumn++;
13,512,493✔
1016
  return &pIter->cv;
13,512,493✔
1017
}
1018

1019
static int32_t tRowNoneUpsertColData(SColData *aColData, int32_t nColData, int32_t flag) {
1,098✔
1020
  int32_t code = 0;
1,098✔
1021

1022
  if (flag) return code;
1,098!
1023

1024
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
7,746✔
1025
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
6,633✔
1026
    if (code) return code;
6,648!
1027
  }
1028

1029
  return code;
1,113✔
1030
}
1031
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
165,285✔
1032
  int32_t code = 0;
165,285✔
1033

1034
  int32_t   iColData = 0;
165,285✔
1035
  SColData *pColData = &aColData[iColData];
165,285✔
1036
  int32_t   iTColumn = 1;
165,285✔
1037
  STColumn *pTColumn = &pSchema->columns[iTColumn];
165,285✔
1038

1039
  while (pColData) {
596,435✔
1040
    if (pTColumn) {
431,152!
1041
      if (pTColumn->colId == pColData->cid) {  // NULL
431,152✔
1042
        if (flag == 0) {
431,148✔
1043
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
431,080✔
1044
        } else {
1045
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
68✔
1046
        }
1047
        if (code) goto _exit;
431,146!
1048

1049
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
431,146✔
1050
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
431,146✔
1051
      } else if (pTColumn->colId > pColData->cid) {  // NONE
4!
1052
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
1053
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
1054
      } else {
1055
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
4!
1056
      }
1057
    } else {  // NONE
1058
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
1059
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
1060
    }
1061
  }
1062

1063
_exit:
165,283✔
1064
  return code;
165,283✔
1065
}
1066
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
647,801,346✔
1067
                                      int32_t flag) {
1068
  int32_t code = 0;
647,801,346✔
1069

1070
  int32_t   iColData = 0;
647,801,346✔
1071
  SColData *pColData = &aColData[iColData];
647,801,346✔
1072
  int32_t   iTColumn = 1;
647,801,346✔
1073
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
647,801,346✔
1074

1075
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
647,801,346✔
1076
  SPrimaryKeyIndex index;
1077
  uint8_t         *data = pRow->data;
647,801,346✔
1078
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
871,873,818✔
1079
    data += tGetPrimaryKeyIndex(data, &index);
224,140,856✔
1080
  }
1081

1082
  switch (pRow->flag) {
647,732,962!
1083
    case HAS_VALUE:
637,181,782✔
1084
      pf = data;  // TODO: fix here
637,181,782✔
1085
      pv = pf + pTSchema->flen;
637,181,782✔
1086
      break;
637,181,782✔
1087
    case (HAS_NULL | HAS_NONE):
1,296✔
1088
      pb = data;
1,296✔
1089
      break;
1,296✔
1090
    case (HAS_VALUE | HAS_NONE):
10,705,730✔
1091
    case (HAS_VALUE | HAS_NULL):
1092
      pb = data;
10,705,730✔
1093
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
10,705,730✔
1094
      pv = pf + pTSchema->flen;
10,705,730✔
1095
      break;
10,705,730✔
1096
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
1097
      pb = data;
×
1098
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
1099
      pv = pf + pTSchema->flen;
×
1100
      break;
×
1101
    default:
×
1102
      return TSDB_CODE_INVALID_DATA_FMT;
×
1103
  }
1104

1105
  while (pColData) {
2,147,483,647✔
1106
    if (pTColumn) {
2,119,329,908✔
1107
      if (pTColumn->colId == pColData->cid) {
2,118,909,769!
1108
        if (!(pTColumn->type == pColData->type)) {
2,119,555,321!
1109
          return TSDB_CODE_INVALID_PARA;
×
1110
        }
1111
        if (pb) {
2,119,555,321✔
1112
          uint8_t bv;
1113
          switch (pRow->flag) {
65,694,914!
1114
            case (HAS_NULL | HAS_NONE):
25,920✔
1115
              bv = GET_BIT1(pb, iTColumn - 1);
25,920✔
1116
              break;
25,920✔
1117
            case (HAS_VALUE | HAS_NONE):
199,139✔
1118
              bv = GET_BIT1(pb, iTColumn - 1);
199,139✔
1119
              if (bv) bv++;
199,139✔
1120
              break;
199,139✔
1121
            case (HAS_VALUE | HAS_NULL):
65,469,902✔
1122
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
65,469,902✔
1123
              break;
65,469,902✔
1124
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
1125
              bv = GET_BIT2(pb, iTColumn - 1);
×
1126
              break;
×
1127
            default:
×
1128
              return TSDB_CODE_INVALID_DATA_FMT;
×
1129
          }
1130

1131
          if (bv == BIT_FLG_NONE) {
65,694,961✔
1132
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
43,367!
1133
              goto _exit;
×
1134
            goto _continue;
43,367✔
1135
          } else if (bv == BIT_FLG_NULL) {
65,651,594✔
1136
            if (flag == 0) {
11,804,835✔
1137
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
11,633,864✔
1138
            } else {
1139
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
170,971✔
1140
            }
1141
            if (code) goto _exit;
11,805,223!
1142
            goto _continue;
11,805,223✔
1143
          }
1144
        }
1145

1146
        if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647!
1147
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
54,764,085!
1148
          uint32_t nData;
1149
          pData += tGetU32v(pData, &nData);
54,764,085✔
1150
          if (flag == 0) {
54,764,085!
1151
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
57,577,583✔
1152
          } else {
1153
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
1154
          }
1155
          if (code) goto _exit;
59,115,604!
1156
        } else {
1157
          if (flag == 0) {
2,052,943,081✔
1158
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,048,230,346✔
1159
                                                                          TYPE_BYTES[pColData->type]);
2,048,230,346✔
1160
          } else {
1161
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
4,712,735✔
1162
                                                                          TYPE_BYTES[pColData->type], flag > 0);
4,712,735✔
1163
          }
1164
          if (code) goto _exit;
2,052,261,021!
1165
        }
1166

1167
      _continue:
2,052,261,021✔
1168
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,123,225,215✔
1169
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,123,225,215✔
1170
      } else if (pTColumn->colId > pColData->cid) {  // NONE
×
1171
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
1172
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
1173
      } else {
1174
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
×
1175
      }
1176
    } else {
1177
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
420,139!
1178
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
420,142✔
1179
    }
1180
  }
1181

1182
_exit:
651,558,705✔
1183
  return code;
651,558,705✔
1184
}
1185
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
4,512,011✔
1186
  int32_t code = 0;
4,512,011✔
1187

1188
  uint8_t  *pv = NULL;
4,512,011✔
1189
  int32_t   iColData = 0;
4,512,011✔
1190
  SColData *pColData = &aColData[iColData];
4,512,011✔
1191
  int32_t   iTColumn = 1;
4,512,011✔
1192
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
4,512,011✔
1193
  int32_t   iCol = 0;
4,512,011✔
1194

1195
  // primary keys
1196
  uint8_t         *data = pRow->data;
4,512,011✔
1197
  SPrimaryKeyIndex index;
1198
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
4,660,535✔
1199
    data += tGetPrimaryKeyIndex(data, &index);
148,522✔
1200
  }
1201

1202
  SKVIdx *pKVIdx = (SKVIdx *)data;
4,512,013✔
1203
  if (pRow->flag & KV_FLG_LIT) {
4,512,013✔
1204
    pv = pKVIdx->idx + pKVIdx->nCol;
4,453,404✔
1205
  } else if (pRow->flag & KV_FLG_MID) {
58,609!
1206
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
62,910✔
1207
  } else if (pRow->flag & KV_FLG_BIG) {
×
1208
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
1209
  } else {
1210
    return TSDB_CODE_INVALID_PARA;
×
1211
  }
1212

1213
  while (pColData) {
42,606,984✔
1214
    if (pTColumn) {
38,080,969✔
1215
      if (pTColumn->colId == pColData->cid) {
37,681,881✔
1216
        while (iCol < pKVIdx->nCol) {
37,661,293✔
1217
          uint8_t *pData;
1218
          if (pRow->flag & KV_FLG_LIT) {
32,447,581✔
1219
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
30,729,137✔
1220
          } else if (pRow->flag & KV_FLG_MID) {
1,718,444!
1221
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
1,718,520✔
1222
          } else if (pRow->flag & KV_FLG_BIG) {
×
1223
            pData = pv + ((uint32_t *)pKVIdx->idx)[iCol];
×
1224
          } else {
1225
            return TSDB_CODE_INVALID_DATA_FMT;
×
1226
          }
1227

1228
          int16_t cid;
1229
          pData += tGetI16v(pData, &cid);
32,447,657✔
1230

1231
          if (TABS(cid) == pTColumn->colId) {
32,447,657✔
1232
            if (cid < 0) {
32,014,613✔
1233
              if (flag == 0) {
6,774,145✔
1234
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
6,757,554✔
1235
              } else {
1236
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
16,591✔
1237
              }
1238
              if (code) goto _exit;
6,245,453!
1239
            } else {
1240
              uint32_t nData;
1241
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
25,240,468!
1242
                pData += tGetU32v(pData, &nData);
6,101,046✔
1243
              } else {
1244
                nData = 0;
19,139,422✔
1245
              }
1246
              if (flag == 0) {
25,240,468!
1247
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
25,473,824✔
1248
              } else {
1249
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
1250
              }
1251
              if (code) goto _exit;
25,779,081!
1252
            }
1253
            iCol++;
32,024,534✔
1254
            goto _continue;
32,024,534✔
1255
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
433,044✔
1256
            break;
39,270✔
1257
          } else {
1258
            iCol++;
393,774✔
1259
          }
1260
        }
1261

1262
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
5,252,982!
1263

1264
      _continue:
5,252,702✔
1265
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
37,277,236✔
1266
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
37,277,236✔
1267
      } else if (pTColumn->colId > pColData->cid) {
414,362!
1268
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
1269
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
1270
      } else {
1271
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
414,362!
1272
      }
1273
    } else {
1274
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
399,088!
1275
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
399,072✔
1276
    }
1277
  }
1278

1279
_exit:
4,526,015✔
1280
  return code;
4,526,015✔
1281
}
1282
/* flag > 0: forward update
1283
 * flag == 0: append
1284
 * flag < 0: backward update
1285
 */
1286
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
652,504,976✔
1287
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
652,504,976!
1288
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
652,504,976!
1289

1290
  if (pRow->flag == HAS_NONE) {
652,504,976✔
1291
    return tRowNoneUpsertColData(aColData, nColData, flag);
1,098✔
1292
  } else if (pRow->flag == HAS_NULL) {
652,503,878✔
1293
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
165,286✔
1294
  } else if (pRow->flag >> 4) {  // KV row
652,338,592✔
1295
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
4,511,475✔
1296
  } else {  // TUPLE row
1297
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
647,827,117✔
1298
  }
1299
}
1300

1301
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
2,147,483,647✔
1302
  key->numOfPKs = row->numOfPKs;
2,147,483,647✔
1303

1304
  if (key->numOfPKs == 0) {
2,147,483,647!
1305
    return;
×
1306
  }
1307

1308
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
1309

1310
  uint8_t *data = row->data;
2,147,483,647✔
1311

1312
  for (int32_t i = 0; i < row->numOfPKs; i++) {
2,147,483,647✔
1313
    data += tGetPrimaryKeyIndex(data, &indices[i]);
2,147,483,647✔
1314
  }
1315

1316
  // primary keys
1317
  for (int32_t i = 0; i < row->numOfPKs; i++) {
2,147,483,647✔
1318
    key->pks[i].type = indices[i].type;
2,147,483,647✔
1319

1320
    uint8_t *tdata = data + indices[i].offset;
2,147,483,647✔
1321
    if (row->flag >> 4) {
2,147,483,647✔
1322
      tdata += tGetI16v(tdata, NULL);
3,050,639✔
1323
    }
1324

1325
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
2,147,483,647!
1326
      key->pks[i].pData = tdata;
867✔
1327
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
1,734!
1328
    } else {
1329
      (void)memcpy(&key->pks[i].val, tdata, tDataTypes[indices[i].type].bytes);
2,147,483,647✔
1330
    }
1331
  }
1332
}
1333

1334
#define T_COMPARE_SCALAR_VALUE(TYPE, V1, V2)    \
1335
  do {                                          \
1336
    if (*(TYPE *)(V1) < *(TYPE *)(V2)) {        \
1337
      return -1;                                \
1338
    } else if (*(TYPE *)(V1) > *(TYPE *)(V2)) { \
1339
      return 1;                                 \
1340
    } else {                                    \
1341
      return 0;                                 \
1342
    }                                           \
1343
  } while (0)
1344

1345
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
42,786,612✔
1346
  switch (tv1->type) {
42,786,612!
1347
    case TSDB_DATA_TYPE_BOOL:
×
1348
    case TSDB_DATA_TYPE_TINYINT:
1349
      T_COMPARE_SCALAR_VALUE(int8_t, &tv1->val, &tv2->val);
×
1350
    case TSDB_DATA_TYPE_SMALLINT:
×
1351
      T_COMPARE_SCALAR_VALUE(int16_t, &tv1->val, &tv2->val);
×
1352
    case TSDB_DATA_TYPE_INT:
954,130✔
1353
      T_COMPARE_SCALAR_VALUE(int32_t, &tv1->val, &tv2->val);
954,130✔
1354
    case TSDB_DATA_TYPE_BIGINT:
41,837,682✔
1355
    case TSDB_DATA_TYPE_TIMESTAMP:
1356
      T_COMPARE_SCALAR_VALUE(int64_t, &tv1->val, &tv2->val);
41,837,682✔
1357
    case TSDB_DATA_TYPE_FLOAT:
×
1358
      T_COMPARE_SCALAR_VALUE(float, &tv1->val, &tv2->val);
×
1359
    case TSDB_DATA_TYPE_DOUBLE:
×
1360
      T_COMPARE_SCALAR_VALUE(double, &tv1->val, &tv2->val);
×
1361
    case TSDB_DATA_TYPE_UTINYINT:
×
1362
      T_COMPARE_SCALAR_VALUE(uint8_t, &tv1->val, &tv2->val);
×
1363
    case TSDB_DATA_TYPE_USMALLINT:
×
1364
      T_COMPARE_SCALAR_VALUE(uint16_t, &tv1->val, &tv2->val);
×
1365
    case TSDB_DATA_TYPE_UINT:
×
1366
      T_COMPARE_SCALAR_VALUE(uint32_t, &tv1->val, &tv2->val);
×
1367
    case TSDB_DATA_TYPE_UBIGINT:
×
1368
      T_COMPARE_SCALAR_VALUE(uint64_t, &tv1->val, &tv2->val);
×
1369
    case TSDB_DATA_TYPE_GEOMETRY:
271✔
1370
    case TSDB_DATA_TYPE_BINARY: {
1371
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
271✔
1372
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
271✔
1373
    }
1374
    case TSDB_DATA_TYPE_NCHAR: {
×
1375
      int32_t ret = tasoUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
1376
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
1377
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
1378
    }
1379
    case TSDB_DATA_TYPE_VARBINARY: {
×
1380
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
1381
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
1382
    }
1383
    default:
×
1384
      break;
×
1385
  }
1386

1387
  return 0;
×
1388
}
1389

1390
// NOTE:
1391
// set key->numOfPKs to 0 as the smallest key with ts
1392
// set key->numOfPKs to (TD_MAX_PK_COLS + 1) as the largest key with ts
1393
FORCE_INLINE int32_t tRowKeyCompare(const SRowKey *key1, const SRowKey *key2) {
2,147,483,647✔
1394
  if (key1->ts < key2->ts) {
2,147,483,647!
1395
    return -1;
2,147,483,647✔
1396
  } else if (key1->ts > key2->ts) {
2,147,483,647!
1397
    return 1;
2,147,483,647✔
1398
  }
1399

1400
  if (key1->numOfPKs == key2->numOfPKs) {
48,550,746!
1401
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
67,047,333!
1402
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
42,794,140✔
1403
      if (ret) return ret;
42,778,339!
1404
    }
1405
  } else if (key1->numOfPKs < key2->numOfPKs) {
×
1406
    return -1;
×
1407
  } else {
1408
    return 1;
×
1409
  }
1410

1411
  return 0;
24,253,193✔
1412
}
1413

1414
void tRowKeyAssign(SRowKey *pDst, SRowKey *pSrc) {
779,507,501✔
1415
  pDst->ts = pSrc->ts;
779,507,501✔
1416
  pDst->numOfPKs = pSrc->numOfPKs;
779,507,501✔
1417

1418
  if (pSrc->numOfPKs > 0) {
779,507,501✔
1419
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
53,421,326✔
1420
      SValue *pVal = &pDst->pks[i];
26,707,978✔
1421
      pVal->type = pSrc->pks[i].type;
26,707,978✔
1422

1423
      if (IS_NUMERIC_TYPE(pVal->type)) {
26,707,978!
1424
        pVal->val = pSrc->pks[i].val;
26,707,968✔
1425
      } else {
1426
        pVal->nData = pSrc->pks[i].nData;
10✔
1427
        (void)memcpy(pVal->pData, pSrc->pks[i].pData, pVal->nData);
10✔
1428
      }
1429
    }
1430
  }
1431
}
779,507,501✔
1432

1433
// STag ========================================
1434
static int tTagValCmprFn(const void *p1, const void *p2) {
128,893,892✔
1435
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
128,893,892✔
1436
    return -1;
45,368,378✔
1437
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
83,525,514✔
1438
    return 1;
46,061,073✔
1439
  }
1440

1441
  return 0;
37,464,441✔
1442
}
1443
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
11,551✔
1444
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
11,551✔
1445
}
1446

1447
#ifdef TD_DEBUG_PRINT_TAG
1448
static void debugPrintTagVal(int8_t type, const void *val, int32_t vlen, const char *tag, int32_t ln) {
1449
  switch (type) {
1450
    case TSDB_DATA_TYPE_VARBINARY:
1451
    case TSDB_DATA_TYPE_JSON:
1452
    case TSDB_DATA_TYPE_VARCHAR:
1453
    case TSDB_DATA_TYPE_NCHAR:
1454
    case TSDB_DATA_TYPE_GEOMETRY: {
1455
      char tmpVal[32] = {0};
1456
      tstrncpy(tmpVal, val, vlen > 31 ? 31 : vlen);
1457
      printf("%s:%d type:%d vlen:%d, val:\"%s\"\n", tag, ln, (int32_t)type, vlen, tmpVal);
1458
    } break;
1459
    case TSDB_DATA_TYPE_FLOAT:
1460
      printf("%s:%d type:%d vlen:%d, val:%f\n", tag, ln, (int32_t)type, vlen, *(float *)val);
1461
      break;
1462
    case TSDB_DATA_TYPE_DOUBLE:
1463
      printf("%s:%d type:%d vlen:%d, val:%lf\n", tag, ln, (int32_t)type, vlen, *(double *)val);
1464
      break;
1465
    case TSDB_DATA_TYPE_BOOL:
1466
      printf("%s:%d type:%d vlen:%d, val:%" PRIu8 "\n", tag, ln, (int32_t)type, vlen, *(uint8_t *)val);
1467
      break;
1468
    case TSDB_DATA_TYPE_TINYINT:
1469
      printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val);
1470
      break;
1471
    case TSDB_DATA_TYPE_SMALLINT:
1472
      printf("%s:%d type:%d vlen:%d, val:%" PRIi16 "\n", tag, ln, (int32_t)type, vlen, *(int16_t *)val);
1473
      break;
1474
    case TSDB_DATA_TYPE_INT:
1475
      printf("%s:%d type:%d vlen:%d, val:%" PRIi32 "\n", tag, ln, (int32_t)type, vlen, *(int32_t *)val);
1476
      break;
1477
    case TSDB_DATA_TYPE_BIGINT:
1478
      printf("%s:%d type:%d vlen:%d, val:%" PRIi64 "\n", tag, ln, (int32_t)type, vlen, *(int64_t *)val);
1479
      break;
1480
    case TSDB_DATA_TYPE_TIMESTAMP:
1481
      printf("%s:%d type:%d vlen:%d, val:%" PRIi64 "\n", tag, ln, (int32_t)type, vlen, *(int64_t *)val);
1482
      break;
1483
    case TSDB_DATA_TYPE_UTINYINT:
1484
      printf("%s:%d type:%d vlen:%d, val:%" PRIu8 "\n", tag, ln, (int32_t)type, vlen, *(uint8_t *)val);
1485
      break;
1486
    case TSDB_DATA_TYPE_USMALLINT:
1487
      printf("%s:%d type:%d vlen:%d, val:%" PRIu16 "\n", tag, ln, (int32_t)type, vlen, *(uint16_t *)val);
1488
      break;
1489
    case TSDB_DATA_TYPE_UINT:
1490
      printf("%s:%d type:%d vlen:%d, val:%" PRIu32 "\n", tag, ln, (int32_t)type, vlen, *(uint32_t *)val);
1491
      break;
1492
    case TSDB_DATA_TYPE_UBIGINT:
1493
      printf("%s:%d type:%d vlen:%d, val:%" PRIu64 "\n", tag, ln, (int32_t)type, vlen, *(uint64_t *)val);
1494
      break;
1495
    case TSDB_DATA_TYPE_NULL:
1496
      printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val);
1497
      break;
1498
    default:
1499
      break;
1500
  }
1501
}
1502

1503
void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
1504
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
1505
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
1506
  uint8_t *p = NULL;
1507
  int16_t  offset = 0;
1508

1509
  if (isLarge) {
1510
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
1511
  } else {
1512
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1513
  }
1514
  printf("%s:%d >>> STAG === %s:%s, len: %d, nTag: %d, sver:%d\n", tag, ln, isJson ? "json" : "normal",
1515
         isLarge ? "large" : "small", (int32_t)pTag->len, (int32_t)pTag->nTag, pTag->ver);
1516
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
1517
    if (isLarge) {
1518
      offset = ((int16_t *)pTag->idx)[n];
1519
    } else {
1520
      offset = pTag->idx[n];
1521
    }
1522
    STagVal tagVal = {0};
1523
    if (isJson) {
1524
      tagVal.pKey = (char *)POINTER_SHIFT(p, offset);
1525
    } else {
1526
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
1527
    }
1528
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
1529
    tGetTagVal(p + offset, &tagVal, isJson);
1530
    if (IS_VAR_DATA_TYPE(tagVal.type)) {
1531
      debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
1532
    } else {
1533
      debugPrintTagVal(tagVal.type, &tagVal.i64, tDataTypes[tagVal.type].bytes, __func__, __LINE__);
1534
    }
1535
  }
1536
  printf("\n");
1537
}
1538
#endif
1539

1540
static int32_t tPutTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
1,404,042✔
1541
  int32_t n = 0;
1,404,042✔
1542

1543
  // key
1544
  if (isJson) {
1,404,042✔
1545
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
1,080✔
1546
  } else {
1547
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
2,807,004✔
1548
  }
1549

1550
  // type
1551
  n += tPutI8(p ? p + n : p, pTagVal->type);
1,404,042✔
1552

1553
  // value
1554
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
1,404,042✔
1555
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
638,234✔
1556
  } else {
1557
    p = p ? p + n : p;
1,084,925✔
1558
    n += tDataTypes[pTagVal->type].bytes;
1,084,925✔
1559
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
1,084,925✔
1560
  }
1561

1562
  return n;
1,404,042✔
1563
}
1564
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
127,147,110✔
1565
  int32_t n = 0;
127,147,110✔
1566

1567
  // key
1568
  if (isJson) {
127,147,110✔
1569
    n += tGetCStr(p + n, &pTagVal->pKey);
26,600!
1570
  } else {
1571
    n += tGetI16v(p + n, &pTagVal->cid);
254,267,620!
1572
  }
1573

1574
  // type
1575
  n += tGetI8(p + n, &pTagVal->type);
127,147,110!
1576

1577
  // value
1578
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
127,147,110!
1579
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
38,414,170!
1580
  } else {
1581
    (void)memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
107,940,025✔
1582
    n += tDataTypes[pTagVal->type].bytes;
107,940,025✔
1583
  }
1584

1585
  return n;
127,147,110✔
1586
}
1587

1588
bool tTagIsJson(const void *pTag) { return (((const STag *)pTag)->flags & TD_TAG_JSON); }
41,234✔
1589

1590
bool tTagIsJsonNull(void *data) {
6,866✔
1591
  STag  *pTag = (STag *)data;
6,866✔
1592
  int8_t isJson = tTagIsJson(pTag);
6,866✔
1593
  if (!isJson) return false;
6,866✔
1594
  return ((STag *)data)->nTag == 0;
2,894✔
1595
}
1596

1597
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
187,839✔
1598
  int32_t  code = 0;
187,839✔
1599
  uint8_t *p = NULL;
187,839✔
1600
  int16_t  n = 0;
187,839✔
1601
  int16_t  nTag = taosArrayGetSize(pArray);
187,839✔
1602
  int32_t  szTag = 0;
187,839✔
1603
  int8_t   isLarge = 0;
187,839✔
1604

1605
  // sort
1606
  if (isJson) {
187,839✔
1607
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
219✔
1608
  } else {
1609
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
187,620✔
1610
  }
1611

1612
  // get size
1613
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
889,924✔
1614
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
702,090✔
1615
  }
1616
  if (szTag <= INT8_MAX) {
187,834✔
1617
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
173,454✔
1618
  } else {
1619
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
14,380✔
1620
    isLarge = 1;
14,380✔
1621
  }
1622

1623
  // build tag
1624
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
187,834!
1625
  if ((*ppTag) == NULL) {
187,874!
1626
    code = terrno;
×
1627
    goto _err;
×
1628
  }
1629
  (*ppTag)->flags = 0;
187,874✔
1630
  if (isJson) {
187,874✔
1631
    (*ppTag)->flags |= TD_TAG_JSON;
219✔
1632
  }
1633
  if (isLarge) {
187,874✔
1634
    (*ppTag)->flags |= TD_TAG_LARGE;
14,387✔
1635
  }
1636
  (*ppTag)->len = szTag;
187,874✔
1637
  (*ppTag)->nTag = nTag;
187,874✔
1638
  (*ppTag)->ver = version;
187,874✔
1639

1640
  if (isLarge) {
187,874✔
1641
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
14,387✔
1642
  } else {
1643
    p = (uint8_t *)&(*ppTag)->idx[nTag];
173,487✔
1644
  }
1645
  n = 0;
187,874✔
1646
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
889,998✔
1647
    if (isLarge) {
702,133✔
1648
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
77,919✔
1649
    } else {
1650
      (*ppTag)->idx[iTag] = n;
624,214✔
1651
    }
1652
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
702,133✔
1653
  }
1654
#ifdef TD_DEBUG_PRINT_TAG
1655
  debugPrintSTag(*ppTag, __func__, __LINE__);
1656
#endif
1657

1658
  return code;
187,865✔
1659

1660
_err:
×
1661
  return code;
×
1662
}
1663

1664
void tTagFree(STag *pTag) {
54,729✔
1665
  if (pTag) taosMemoryFree(pTag);
54,729!
1666
}
54,729✔
1667

1668
char *tTagValToData(const STagVal *value, bool isJson) {
12,349,335✔
1669
  if (!value) {
12,349,335!
1670
    return NULL;
×
1671
  }
1672

1673
  char  *data = NULL;
12,349,335✔
1674
  int8_t typeBytes = 0;
12,349,335✔
1675
  if (isJson) {
12,349,335✔
1676
    typeBytes = CHAR_BYTES;
6,261✔
1677
  }
1678

1679
  if (IS_VAR_DATA_TYPE(value->type)) {
12,349,335!
1680
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
2,982,042!
1681
    if (data == NULL) {
2,983,180!
1682
      return NULL;
×
1683
    }
1684

1685
    if (isJson) {
2,983,180✔
1686
      *data = value->type;
2,448✔
1687
    }
1688

1689
    varDataLen(data + typeBytes) = value->nData;
2,983,180✔
1690
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
2,983,180✔
1691
  } else {
1692
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
9,367,293✔
1693
  }
1694

1695
  return data;
12,350,473✔
1696
}
1697

1698
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
39,034,328✔
1699
  if (!pTag || !pTagVal) {
39,034,328!
1700
    return false;
×
1701
  }
1702

1703
  int16_t  lidx = 0;
39,202,295✔
1704
  int16_t  ridx = pTag->nTag - 1;
39,202,295✔
1705
  int16_t  midx;
1706
  uint8_t *p;
1707
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
39,202,295✔
1708
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
39,202,295✔
1709
  int16_t  offset;
1710
  STagVal  tv;
1711
  int      c;
1712

1713
  if (isLarge) {
39,202,295✔
1714
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
32,161,596✔
1715
  } else {
1716
    p = (uint8_t *)&pTag->idx[pTag->nTag];
7,040,699✔
1717
  }
1718

1719
  pTagVal->type = TSDB_DATA_TYPE_NULL;
39,202,295✔
1720
  pTagVal->pData = NULL;
39,202,295✔
1721
  pTagVal->nData = 0;
39,202,295✔
1722
  while (lidx <= ridx) {
129,557,704✔
1723
    midx = (lidx + ridx) / 2;
127,722,670✔
1724
    if (isLarge) {
127,722,670✔
1725
      offset = ((int16_t *)pTag->idx)[midx];
104,910,797✔
1726
    } else {
1727
      offset = pTag->idx[midx];
22,811,873✔
1728
    }
1729

1730
    int32_t nt = tGetTagVal(p + offset, &tv, isJson);
127,722,670✔
1731
    if (isJson) {
128,403,771✔
1732
      c = tTagValJsonCmprFn(pTagVal, &tv);
11,388✔
1733
    } else {
1734
      c = tTagValCmprFn(pTagVal, &tv);
128,392,383✔
1735
    }
1736

1737
    if (c < 0) {
129,711,000✔
1738
      ridx = midx - 1;
44,574,128✔
1739
    } else if (c > 0) {
85,136,872✔
1740
      lidx = midx + 1;
45,781,281✔
1741
    } else {
1742
      (void)memcpy(pTagVal, &tv, sizeof(tv));
39,355,591✔
1743
      return true;
39,355,591✔
1744
    }
1745
  }
1746
  return false;
1,835,034✔
1747
}
1748

1749
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
819,234✔
1750
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
1,638,468✔
1751
}
1752

1753
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
19,430,780✔
1754

1755
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
1,448✔
1756
  int32_t  code = 0;
1,448✔
1757
  uint8_t *p = NULL;
1,448✔
1758
  STagVal  tv = {0};
1,448✔
1759
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
1,448✔
1760
  int16_t  offset = 0;
1,448✔
1761

1762
  if (isLarge) {
1,448✔
1763
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
16✔
1764
  } else {
1765
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,432✔
1766
  }
1767

1768
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
1,448✔
1769
  if (*ppArray == NULL) {
1,448!
1770
    code = terrno;
×
1771
    goto _err;
×
1772
  }
1773

1774
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
4,123✔
1775
    if (isLarge) {
2,675✔
1776
      offset = ((int16_t *)pTag->idx)[iTag];
16✔
1777
    } else {
1778
      offset = pTag->idx[iTag];
2,659✔
1779
    }
1780
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
2,675✔
1781
    if (taosArrayPush(*ppArray, &tv) == NULL) {
5,350!
1782
      code = terrno;
×
1783
      goto _err;
×
1784
    }
1785
  }
1786

1787
  return code;
1,448✔
1788

1789
_err:
×
1790
  return code;
×
1791
}
1792

1793
// STSchema ========================================
1794
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
15,799,681✔
1795
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
15,799,681!
1796
  if (pTSchema == NULL) {
15,828,154!
1797
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1798
    return NULL;
×
1799
  }
1800

1801
  pTSchema->numOfCols = numOfCols;
15,828,154✔
1802
  pTSchema->version = version;
15,828,154✔
1803

1804
  // timestamp column
1805
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
15,828,154!
1806
    terrno = TSDB_CODE_INVALID_PARA;
×
1807
    return NULL;
×
1808
  }
1809
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
15,828,154!
1810
    terrno = TSDB_CODE_INVALID_PARA;
×
1811
    return NULL;
×
1812
  }
1813
  pTSchema->columns[0].colId = aSchema[0].colId;
15,828,154✔
1814
  pTSchema->columns[0].type = aSchema[0].type;
15,828,154✔
1815
  pTSchema->columns[0].flags = aSchema[0].flags;
15,828,154✔
1816
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
15,828,154✔
1817
  pTSchema->columns[0].offset = -1;
15,828,154✔
1818

1819
  // other columns
1820
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
186,135,829✔
1821
    SSchema  *pSchema = &aSchema[iCol];
170,307,675✔
1822
    STColumn *pTColumn = &pTSchema->columns[iCol];
170,307,675✔
1823

1824
    pTColumn->colId = pSchema->colId;
170,307,675✔
1825
    pTColumn->type = pSchema->type;
170,307,675✔
1826
    pTColumn->flags = pSchema->flags;
170,307,675✔
1827
    pTColumn->offset = pTSchema->flen;
170,307,675✔
1828

1829
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
170,307,675!
1830
      pTColumn->bytes = pSchema->bytes;
33,751,407✔
1831
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
33,751,407✔
1832
    } else {
1833
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
136,556,268✔
1834
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
136,556,268✔
1835
    }
1836

1837
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
170,307,675✔
1838
  }
1839

1840
#if 1  // todo : remove this
1841
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
15,828,154✔
1842
#endif
1843

1844
  return pTSchema;
15,828,154✔
1845
}
1846

1847
static int32_t tTColumnCompare(const void *p1, const void *p2) {
7,970,732✔
1848
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
7,970,732✔
1849
    return -1;
1,185,922✔
1850
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
6,784,810✔
1851
    return 1;
4,794,681✔
1852
  }
1853

1854
  return 0;
1,990,129✔
1855
}
1856

1857
const STColumn *tTSchemaSearchColumn(const STSchema *pTSchema, int16_t cid) {
1,990,553✔
1858
  STColumn tcol = {
1,990,553✔
1859
      .colId = cid,
1860
  };
1861

1862
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
1,990,553✔
1863
}
1864

1865
// SColData ========================================
1866
void tColDataDestroy(void *ph) {
22,126,778✔
1867
  if (ph) {
22,126,778!
1868
    SColData *pColData = (SColData *)ph;
22,126,941✔
1869

1870
    tFree(pColData->pBitMap);
22,126,941!
1871
    tFree(pColData->aOffset);
22,126,952✔
1872
    tFree(pColData->pData);
22,126,956!
1873
  }
1874
}
22,127,830✔
1875

1876
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
21,184,819✔
1877
  pColData->cid = cid;
21,184,819✔
1878
  pColData->type = type;
21,184,819✔
1879
  pColData->cflag = cflag;
21,184,819✔
1880
  tColDataClear(pColData);
21,184,819✔
1881
}
21,186,873✔
1882

1883
void tColDataClear(SColData *pColData) {
44,787,751✔
1884
  pColData->numOfNone = 0;
44,787,751✔
1885
  pColData->numOfNull = 0;
44,787,751✔
1886
  pColData->numOfValue = 0;
44,787,751✔
1887
  pColData->nVal = 0;
44,787,751✔
1888
  pColData->flag = 0;
44,787,751✔
1889
  pColData->nData = 0;
44,787,751✔
1890
}
44,787,751✔
1891

1892
void tColDataDeepClear(SColData *pColData) {
823,826✔
1893
  pColData->pBitMap = NULL;
823,826✔
1894
  pColData->aOffset = NULL;
823,826✔
1895
  pColData->pData = NULL;
823,826✔
1896

1897
  tColDataClear(pColData);
823,826✔
1898
}
823,826✔
1899

1900
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, uint8_t *pData, uint32_t nData) {
1901
  int32_t code = 0;
2,147,483,647✔
1902

1903
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647!
1904
    code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
103,439,484!
1905
    if (code) goto _exit;
110,163,887!
1906
    pColData->aOffset[pColData->nVal] = pColData->nData;
110,163,887✔
1907

1908
    if (nData) {
110,163,887!
1909
      code = tRealloc(&pColData->pData, pColData->nData + nData);
107,248,607!
1910
      if (code) goto _exit;
108,103,321!
1911
      (void)memcpy(pColData->pData + pColData->nData, pData, nData);
108,103,321✔
1912
      pColData->nData += nData;
108,103,321✔
1913
    }
1914
  } else {
1915
    if (!(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal)) {
2,147,483,647!
1916
      return TSDB_CODE_INVALID_PARA;
×
1917
    }
1918
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
2,147,483,647!
1919
    if (code) goto _exit;
2,147,483,647!
1920
    if (pData) {
2,147,483,647!
1921
      (void)memcpy(pColData->pData + pColData->nData, pData, TYPE_BYTES[pColData->type]);
2,147,483,647✔
1922
    } else {
1923
      memset(pColData->pData + pColData->nData, 0, TYPE_BYTES[pColData->type]);
22,963,514✔
1924
    }
1925
    pColData->nData += tDataTypes[pColData->type].bytes;
2,147,483,647✔
1926
  }
1927
  pColData->nVal++;
2,147,483,647✔
1928

1929
_exit:
2,147,483,647✔
1930
  return code;
2,147,483,647✔
1931
}
1932
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,376,455✔
1933
  pColData->flag = HAS_VALUE;
3,376,554✔
1934
  pColData->numOfValue++;
3,376,455✔
1935
  return tColDataPutValue(pColData, pData, nData);
3,376,883✔
1936
}
1937
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
176,878✔
1938
  pColData->flag = HAS_NONE;
176,878✔
1939
  pColData->numOfNone++;
176,878✔
1940
  pColData->nVal++;
176,878✔
1941
  return 0;
176,878✔
1942
}
1943
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
160,720✔
1944
  pColData->flag = HAS_NULL;
160,829✔
1945
  pColData->numOfNull++;
160,829✔
1946
  pColData->nVal++;
160,829✔
1947
  return 0;
160,720✔
1948
}
1949
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,350✔
1950
  int32_t code = 0;
1,350✔
1951

1952
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
1,350✔
1953
  code = tRealloc(&pColData->pBitMap, nBit);
1,350!
1954
  if (code) return code;
1,357!
1955

1956
  memset(pColData->pBitMap, 0, nBit);
1,357✔
1957
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
1,357!
1958

1959
  pColData->flag |= HAS_VALUE;
1,357✔
1960
  pColData->numOfValue++;
1,357✔
1961

1962
  if (pColData->nVal) {
1,357!
1963
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,356!
1964
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
243✔
1965
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
243!
1966
      if (code) return code;
244!
1967
      memset(pColData->aOffset, 0, nOffset);
244✔
1968
    } else {
1969
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
1,113✔
1970
      code = tRealloc(&pColData->pData, pColData->nData);
1,113!
1971
      if (code) return code;
1,113!
1972
      memset(pColData->pData, 0, pColData->nData);
1,113✔
1973
    }
1974
  }
1975

1976
  return tColDataPutValue(pColData, pData, nData);
1,357✔
1977
}
1978
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
11,326,186✔
1979
  pColData->nVal++;
11,326,186✔
1980
  pColData->numOfNone++;
11,326,186✔
1981
  return 0;
11,326,186✔
1982
}
1983
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
600✔
1984
  int32_t code = 0;
600✔
1985

1986
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
600✔
1987
  code = tRealloc(&pColData->pBitMap, nBit);
600!
1988
  if (code) return code;
600!
1989

1990
  memset(pColData->pBitMap, 0, nBit);
600✔
1991
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
600!
1992

1993
  pColData->flag |= HAS_NULL;
600✔
1994
  pColData->numOfNull++;
600✔
1995
  pColData->nVal++;
600✔
1996

1997
  return code;
600✔
1998
}
1999
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
21,979✔
2000
  int32_t code = 0;
21,985✔
2001

2002
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
21,985✔
2003
  code = tRealloc(&pColData->pBitMap, nBit);
21,985!
2004
  if (code) return code;
21,986!
2005

2006
  memset(pColData->pBitMap, 0, nBit);
21,986✔
2007
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
21,986!
2008

2009
  pColData->flag |= HAS_VALUE;
21,986✔
2010
  pColData->numOfValue++;
21,986✔
2011

2012
  if (pColData->nVal) {
21,986!
2013
    if (IS_VAR_DATA_TYPE(pColData->type)) {
21,986!
2014
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
8,754✔
2015
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
8,754!
2016
      if (code) return code;
8,756!
2017
      memset(pColData->aOffset, 0, nOffset);
8,756✔
2018
    } else {
2019
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
13,232✔
2020
      code = tRealloc(&pColData->pData, pColData->nData);
13,232!
2021
      if (code) return code;
13,228!
2022
      memset(pColData->pData, 0, pColData->nData);
13,228✔
2023
    }
2024
  }
2025

2026
  return tColDataPutValue(pColData, pData, nData);
21,980✔
2027
}
2028
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
80✔
2029
  int32_t code = 0;
80✔
2030

2031
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
80✔
2032
  code = tRealloc(&pColData->pBitMap, nBit);
80✔
2033
  if (code) return code;
80!
2034

2035
  memset(pColData->pBitMap, 255, nBit);
80✔
2036
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
80!
2037

2038
  pColData->flag |= HAS_NONE;
80✔
2039
  pColData->numOfNone++;
80✔
2040
  pColData->nVal++;
80✔
2041

2042
  return code;
80✔
2043
}
2044
static FORCE_INLINE int32_t tColDataAppendValue22(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,429,300✔
2045
  pColData->nVal++;
4,429,300✔
2046
  pColData->numOfNull++;
4,429,300✔
2047
  return 0;
4,429,300✔
2048
}
2049
static FORCE_INLINE int32_t tColDataAppendValue30(SColData *pColData, uint8_t *pData, uint32_t nData) {
×
2050
  int32_t code = 0;
×
2051

2052
  pColData->flag |= HAS_VALUE;
×
2053
  pColData->numOfValue++;
×
2054

2055
  uint8_t *pBitMap = NULL;
×
2056
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
2057
  if (code) return code;
×
2058

2059
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
2060
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
2061
  }
2062
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
2063

2064
  tFree(pColData->pBitMap);
×
2065
  pColData->pBitMap = pBitMap;
×
2066

2067
  if (pColData->nVal) {
×
2068
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
2069
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
2070
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
2071
      if (code) return code;
×
2072
      memset(pColData->aOffset, 0, nOffset);
×
2073
    } else {
2074
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
2075
      code = tRealloc(&pColData->pData, pColData->nData);
×
2076
      if (code) return code;
×
2077
      memset(pColData->pData, 0, pColData->nData);
×
2078
    }
2079
  }
2080

2081
  return tColDataPutValue(pColData, pData, nData);
×
2082
}
2083
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, uint8_t *pData, uint32_t nData) {
400✔
2084
  int32_t code = 0;
400✔
2085

2086
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
400!
2087
  if (code) return code;
400!
2088

2089
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
400!
2090
  pColData->numOfNone++;
400✔
2091
  pColData->nVal++;
400✔
2092

2093
  return code;
400✔
2094
}
2095
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
9,378✔
2096
  int32_t code = 0;
9,378✔
2097

2098
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
9,378!
2099
  if (code) return code;
9,384!
2100

2101
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
9,384!
2102
  pColData->numOfNull++;
9,384✔
2103
  pColData->nVal++;
9,384✔
2104

2105
  return code;
9,384✔
2106
}
2107
static FORCE_INLINE int32_t tColDataAppendValue40(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
2108
  pColData->numOfValue++;
2,147,483,647✔
2109
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
2110
}
2111
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, uint8_t *pData, uint32_t nData) {
6,015✔
2112
  int32_t code = 0;
6,015✔
2113

2114
  pColData->flag |= HAS_NONE;
6,015✔
2115
  pColData->numOfNone++;
6,015✔
2116

2117
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
6,015✔
2118
  code = tRealloc(&pColData->pBitMap, nBit);
6,015✔
2119
  if (code) return code;
6,017!
2120

2121
  memset(pColData->pBitMap, 255, nBit);
6,017✔
2122
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
6,017✔
2123

2124
  return tColDataPutValue(pColData, NULL, 0);
6,020✔
2125
}
2126
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
267,463✔
2127
  int32_t code = 0;
273,549✔
2128

2129
  pColData->flag |= HAS_NULL;
273,549✔
2130
  pColData->numOfNull++;
273,549✔
2131

2132
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
273,549✔
2133
  code = tRealloc(&pColData->pBitMap, nBit);
273,245✔
2134
  if (code) return code;
273,557!
2135

2136
  memset(pColData->pBitMap, 255, nBit);
273,557✔
2137
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
273,557!
2138

2139
  return tColDataPutValue(pColData, NULL, 0);
273,561✔
2140
}
2141
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
57,455✔
2142
  int32_t code = 0;
59,456✔
2143

2144
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
59,455!
2145
  if (code) return code;
59,538!
2146

2147
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
59,538!
2148
  pColData->numOfValue++;
59,538!
2149

2150
  return tColDataPutValue(pColData, pData, nData);
60,969✔
2151
}
2152
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
101,333✔
2153
  int32_t code = 0;
101,333✔
2154

2155
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
101,333!
2156
  if (code) return code;
101,376!
2157

2158
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
101,376✔
2159
  pColData->numOfNone++;
101,376✔
2160

2161
  return tColDataPutValue(pColData, NULL, 0);
101,555✔
2162
}
2163
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
1✔
2164
  int32_t code = 0;
1✔
2165

2166
  pColData->flag |= HAS_NULL;
1✔
2167
  pColData->numOfNull++;
1✔
2168

2169
  uint8_t *pBitMap = NULL;
1✔
2170
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
1!
2171
  if (code) return code;
1!
2172

2173
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3!
2174
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
2!
2175
  }
2176
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
1!
2177

2178
  tFree(pColData->pBitMap);
1!
2179
  pColData->pBitMap = pBitMap;
1!
2180

2181
  return tColDataPutValue(pColData, NULL, 0);
1✔
2182
}
2183
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
362,855,076✔
2184
  int32_t code = 0;
363,020,794✔
2185

2186
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
363,020,394!
2187
  if (code) return code;
363,267,384!
2188
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
363,267,384!
2189
  pColData->numOfValue++;
363,267,384✔
2190

2191
  return tColDataPutValue(pColData, pData, nData);
363,227,382✔
2192
}
2193
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,997✔
2194
  int32_t code = 0;
3,997✔
2195

2196
  pColData->flag |= HAS_NONE;
3,997✔
2197
  pColData->numOfNone++;
3,997✔
2198

2199
  uint8_t *pBitMap = NULL;
3,997✔
2200
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
3,997!
2201
  if (code) return code;
3,997!
2202

2203
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
135,564✔
2204
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
131,567✔
2205
  }
2206
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
3,997✔
2207

2208
  tFree(pColData->pBitMap);
3,997!
2209
  pColData->pBitMap = pBitMap;
3,997✔
2210

2211
  return tColDataPutValue(pColData, NULL, 0);
3,997✔
2212
}
2213
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
22,424,230✔
2214
  int32_t code = 0;
22,601,041✔
2215

2216
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
22,597,448!
2217
  if (code) return code;
22,603,808!
2218
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
22,603,808✔
2219
  pColData->numOfNull++;
22,603,808✔
2220

2221
  return tColDataPutValue(pColData, NULL, 0);
22,605,736✔
2222
}
2223
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
×
2224
  int32_t code = 0;
×
2225

2226
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
2227
  if (code) return code;
×
2228
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
×
2229
  pColData->numOfValue++;
×
2230

2231
  return tColDataPutValue(pColData, pData, nData);
×
2232
}
2233
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
4✔
2234
  int32_t code = 0;
4✔
2235

2236
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
4!
2237
  if (code) return code;
4!
2238
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
4✔
2239
  pColData->numOfNone++;
4!
2240

2241
  return tColDataPutValue(pColData, NULL, 0);
4✔
2242
}
2243
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
×
2244
  int32_t code = 0;
×
2245

2246
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
2247
  if (code) return code;
×
2248
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
×
2249
  pColData->numOfNull++;
×
2250

2251
  return tColDataPutValue(pColData, NULL, 0);
×
2252
}
2253
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData) = {
2254
    {tColDataAppendValue00, tColDataAppendValue01, tColDataAppendValue02},  // 0
2255
    {tColDataAppendValue10, tColDataAppendValue11, tColDataAppendValue12},  // HAS_NONE
2256
    {tColDataAppendValue20, tColDataAppendValue21, tColDataAppendValue22},  // HAS_NULL
2257
    {tColDataAppendValue30, tColDataAppendValue31, tColDataAppendValue32},  // HAS_NULL|HAS_NONE
2258
    {tColDataAppendValue40, tColDataAppendValue41, tColDataAppendValue42},  // HAS_VALUE
2259
    {tColDataAppendValue50, tColDataAppendValue51, tColDataAppendValue52},  // HAS_VALUE|HAS_NONE
2260
    {tColDataAppendValue60, tColDataAppendValue61, tColDataAppendValue62},  // HAS_VALUE|HAS_NULL
2261
    {tColDataAppendValue70, tColDataAppendValue71, tColDataAppendValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
2262

2263
    //       VALUE                  NONE                     NULL
2264
};
2265
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
2,147,483,647✔
2266
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) {
2,147,483,647!
2267
    return TSDB_CODE_INVALID_PARA;
×
2268
  }
2269
  return tColDataAppendValueImpl[pColData->flag][pColVal->flag](
2,147,483,647✔
2270
      pColData, IS_VAR_DATA_TYPE(pColData->type) ? pColVal->value.pData : (uint8_t *)&pColVal->value.val,
2,147,483,647!
2271
      pColVal->value.nData);
2272
}
2273

2274
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
9✔
2275
  pColData->numOfNone--;
9✔
2276
  pColData->nVal--;
9✔
2277
  if (pColData->numOfNone) {
9!
2278
    return tColDataAppendValue10(pColData, pData, nData);
×
2279
  } else {
2280
    pColData->flag = 0;
9✔
2281
    return tColDataAppendValue00(pColData, pData, nData);
9✔
2282
  }
2283
}
2284
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
24✔
2285
  pColData->numOfNone--;
24✔
2286
  pColData->nVal--;
24✔
2287
  if (pColData->numOfNone) {
24!
2288
    return tColDataAppendValue12(pColData, pData, nData);
×
2289
  } else {
2290
    pColData->flag = 0;
24✔
2291
    return tColDataAppendValue02(pColData, pData, nData);
24✔
2292
  }
2293
  return 0;
2294
}
2295
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
96✔
2296
  if (forward) {
96!
2297
    pColData->numOfNull--;
96✔
2298
    pColData->nVal--;
96✔
2299
    if (pColData->numOfNull) {
96✔
2300
      return tColDataAppendValue20(pColData, pData, nData);
6✔
2301
    } else {
2302
      pColData->flag = 0;
90✔
2303
      return tColDataAppendValue00(pColData, pData, nData);
90✔
2304
    }
2305
  }
2306
  return 0;
×
2307
}
2308
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
2309
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
2310
    pColData->numOfNone--;
×
2311
    pColData->nVal--;
×
2312
    if (pColData->numOfNone) {
×
2313
      return tColDataAppendValue30(pColData, pData, nData);
×
2314
    } else {
2315
      pColData->flag = HAS_NULL;
×
2316
      return tColDataAppendValue20(pColData, pData, nData);
×
2317
    }
2318
  } else if (forward) {  // NULL ==> VALUE
×
2319
    pColData->numOfNull--;
×
2320
    pColData->nVal--;
×
2321
    if (pColData->numOfNull) {
×
2322
      return tColDataAppendValue30(pColData, pData, nData);
×
2323
    } else {
2324
      pColData->flag = HAS_NONE;
×
2325
      return tColDataAppendValue10(pColData, pData, nData);
×
2326
    }
2327
  }
2328
  return 0;
×
2329
}
2330
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
80✔
2331
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
80!
2332
    pColData->numOfNone--;
80✔
2333
    pColData->numOfNull++;
80✔
2334
    if (pColData->numOfNone) {
80!
2335
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
×
2336
    } else {
2337
      pColData->flag = HAS_NULL;
80✔
2338
    }
2339
  }
2340
  return 0;
80✔
2341
}
2342
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
6,111,439✔
2343
  if (forward) {  // VALUE ==> VALUE
6,111,439!
2344
    pColData->nVal--;
6,111,537✔
2345
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,111,537!
2346
      pColData->nData = pColData->aOffset[pColData->nVal];
721,580✔
2347
    } else {
2348
      pColData->nData -= TYPE_BYTES[pColData->type];
5,389,957✔
2349
    }
2350
    return tColDataPutValue(pColData, pData, nData);
6,128,266✔
2351
  }
2352
  return 0;
×
2353
}
2354
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,867✔
2355
  if (forward) {  // VALUE ==> NULL
5,867!
2356
    pColData->numOfValue--;
5,867✔
2357
    pColData->nVal--;
5,867✔
2358
    if (pColData->numOfValue) {
5,867✔
2359
      if (IS_VAR_DATA_TYPE(pColData->type)) {
5,782!
2360
        pColData->nData = pColData->aOffset[pColData->nVal];
1,146✔
2361
      } else {
2362
        pColData->nData -= TYPE_BYTES[pColData->type];
4,636✔
2363
      }
2364
      return tColDataAppendValue42(pColData, pData, nData);
5,783✔
2365
    } else {
2366
      pColData->flag = 0;
85✔
2367
      pColData->nData = 0;
85✔
2368
      return tColDataAppendValue02(pColData, pData, nData);
85✔
2369
    }
2370
  }
2371
  return 0;
×
2372
}
2373
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,606✔
2374
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
5,606✔
2375
    pColData->numOfNone--;
5,597✔
2376
    pColData->nVal--;
5,597✔
2377
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
5,597!
2378
      pColData->nData -= TYPE_BYTES[pColData->type];
4,478✔
2379
    }
2380
    if (pColData->numOfNone) {
5,597✔
2381
      return tColDataAppendValue50(pColData, pData, nData);
1,999✔
2382
    } else {
2383
      pColData->flag = HAS_VALUE;
3,597✔
2384
      return tColDataAppendValue40(pColData, pData, nData);
3,598✔
2385
    }
2386
  } else if (forward) {  // VALUE ==> VALUE
9!
2387
    pColData->nVal--;
9✔
2388
    if (IS_VAR_DATA_TYPE(pColData->type)) {
9!
2389
      pColData->nData = pColData->aOffset[pColData->nVal];
8✔
2390
    } else {
2391
      pColData->nData -= TYPE_BYTES[pColData->type];
1✔
2392
    }
2393
    return tColDataPutValue(pColData, pData, nData);
9✔
2394
  }
2395
  return 0;
×
2396
}
2397
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
304✔
2398
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
304!
2399
    pColData->numOfNone--;
304✔
2400
    pColData->nVal--;
304✔
2401
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
304!
2402
      pColData->nData -= TYPE_BYTES[pColData->type];
244✔
2403
    }
2404
    if (pColData->numOfNone) {
304!
2405
      return tColDataAppendValue52(pColData, pData, nData);
×
2406
    } else {
2407
      pColData->flag = HAS_VALUE;
304!
2408
      return tColDataAppendValue42(pColData, pData, nData);
304✔
2409
    }
2410
  } else if (forward) {  // VALUE ==> NULL
×
2411
    pColData->numOfValue--;
×
2412
    pColData->nVal--;
×
2413
    if (pColData->numOfValue) {
×
2414
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
2415
        pColData->nData = pColData->aOffset[pColData->nVal];
×
2416
      } else {
2417
        pColData->nData -= TYPE_BYTES[pColData->type];
×
2418
      }
2419
      return tColDataAppendValue52(pColData, pData, nData);
×
2420
    } else {
2421
      pColData->flag = HAS_NONE;
×
2422
      pColData->nData = 0;
×
2423
      return tColDataAppendValue12(pColData, pData, nData);
×
2424
    }
2425
  }
2426
  return 0;
×
2427
}
2428
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,293,342✔
2429
  if (forward) {
3,293,342!
2430
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,293,344✔
2431
      pColData->numOfNull--;
184,995✔
2432
      pColData->nVal--;
184,995✔
2433
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
184,995!
2434
        pColData->nData -= TYPE_BYTES[pColData->type];
154,853✔
2435
      }
2436
      if (pColData->numOfNull) {
184,995✔
2437
        return tColDataAppendValue60(pColData, pData, nData);
165,318✔
2438
      } else {
2439
        pColData->flag = HAS_VALUE;
19,677✔
2440
        return tColDataAppendValue40(pColData, pData, nData);
19,669✔
2441
      }
2442
    } else {  // VALUE ==> VALUE
2443
      pColData->nVal--;
3,108,349✔
2444
      if (IS_VAR_DATA_TYPE(pColData->type)) {
3,108,349!
2445
        pColData->nData = pColData->aOffset[pColData->nVal];
479,193✔
2446
      } else {
2447
        pColData->nData -= TYPE_BYTES[pColData->type];
2,629,156✔
2448
      }
2449
      return tColDataPutValue(pColData, pData, nData);
3,108,354✔
2450
    }
2451
  }
2452
  return 0;
×
2453
}
2454
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
196,225✔
2455
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
196,225!
2456
    pColData->numOfValue--;
173,218✔
2457
    pColData->nVal--;
173,218✔
2458
    if (pColData->numOfValue) {
173,218!
2459
      if (IS_VAR_DATA_TYPE(pColData->type)) {
173,218!
2460
        pColData->nData = pColData->aOffset[pColData->nVal];
27,538✔
2461
      } else {
2462
        pColData->nData -= TYPE_BYTES[pColData->type];
145,680✔
2463
      }
2464
      return tColDataAppendValue62(pColData, pData, nData);
173,216✔
2465
    } else {
2466
      pColData->flag = HAS_NULL;
×
2467
      pColData->nData = 0;
×
2468
      return tColDataAppendValue20(pColData, pData, nData);
×
2469
    }
2470
  }
2471
  return 0;
23,007✔
2472
}
2473
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
401✔
2474
  int32_t code = 0;
401✔
2475

2476
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
401✔
2477
  if (bv == 0) {  // NONE ==> VALUE
401✔
2478
    pColData->numOfNone--;
400✔
2479
    pColData->nVal--;
400✔
2480
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
400!
2481
      pColData->nData -= TYPE_BYTES[pColData->type];
320✔
2482
    }
2483
    if (pColData->numOfNone) {
400!
2484
      return tColDataAppendValue70(pColData, pData, nData);
×
2485
    } else {
2486
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
21,156✔
2487
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
20,756✔
2488
      }
2489
      pColData->flag = (HAS_VALUE | HAS_NULL);
400!
2490
      return tColDataAppendValue60(pColData, pData, nData);
400✔
2491
    }
2492
  } else if (bv == 1) {  // NULL ==> VALUE
1!
2493
    if (forward) {
1!
2494
      pColData->numOfNull--;
1✔
2495
      pColData->nVal--;
1✔
2496
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
1!
2497
        pColData->nData -= TYPE_BYTES[pColData->type];
1✔
2498
      }
2499
      if (pColData->numOfNull) {
1!
2500
        return tColDataAppendValue70(pColData, pData, nData);
×
2501
      } else {
2502
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
3✔
2503
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
2✔
2504
        }
2505
        pColData->flag = (HAS_VALUE | HAS_NONE);
1!
2506
        return tColDataAppendValue50(pColData, pData, nData);
1✔
2507
      }
2508
    }
2509
  } else if (bv == 2) {  // VALUE ==> VALUE
×
2510
    if (forward) {
×
2511
      pColData->nVal--;
×
2512
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
2513
        pColData->nData = pColData->aOffset[pColData->nVal];
×
2514
      } else {
2515
        pColData->nData -= TYPE_BYTES[pColData->type];
×
2516
      }
2517
      return tColDataPutValue(pColData, pData, nData);
×
2518
    }
2519
  } else {
2520
    return TSDB_CODE_INVALID_PARA;
×
2521
  }
2522
  return 0;
×
2523
}
2524
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,593✔
2525
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
3,593✔
2526
  if (bv == 0) {  // NONE ==> NULL
3,593!
2527
    pColData->numOfNone--;
3,593✔
2528
    pColData->nVal--;
3,593✔
2529
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
3,593!
2530
      pColData->nData -= TYPE_BYTES[pColData->type];
2,541✔
2531
    }
2532
    if (pColData->numOfNone) {
3,593!
2533
      return tColDataAppendValue72(pColData, pData, nData);
×
2534
    } else {
2535
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
114,320✔
2536
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
110,727✔
2537
      }
2538
      pColData->flag = (HAS_VALUE | HAS_NULL);
3,593!
2539
      return tColDataAppendValue62(pColData, pData, nData);
3,596✔
2540
    }
2541
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
×
2542
    pColData->numOfValue--;
×
2543
    pColData->nVal--;
×
2544
    if (pColData->numOfValue) {
×
2545
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
2546
        pColData->nData = pColData->aOffset[pColData->nVal];
×
2547
      } else {
2548
        pColData->nData -= TYPE_BYTES[pColData->type];
×
2549
      }
2550
      return tColDataAppendValue72(pColData, pData, nData);
×
2551
    } else {
2552
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
2553
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
×
2554
      }
2555
      pColData->flag = (HAS_NULL | HAS_NONE);
×
2556
      pColData->nData = 0;
×
2557
      return tColDataAppendValue32(pColData, pData, nData);
×
2558
    }
2559
  }
2560
  return 0;
×
2561
}
2562
static FORCE_INLINE int32_t tColDataUpdateNothing(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
13,441✔
2563
  return 0;
13,441✔
2564
}
2565
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
2566
    {NULL, NULL, NULL},                                                     // 0
2567
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
2568
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
2569
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
2570
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
2571
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
2572
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
2573
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
2574

2575
    //    VALUE             NONE        NULL
2576
};
2577
int32_t tColDataUpdateValue(SColData *pColData, SColVal *pColVal, bool forward) {
4,211,577✔
2578
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) return TSDB_CODE_INVALID_PARA;
4,211,577!
2579
  if (!(pColData->nVal > 0)) return TSDB_CODE_INVALID_PARA;
4,211,683!
2580

2581
  if (tColDataUpdateValueImpl[pColData->flag][pColVal->flag] == NULL) return 0;
4,211,683!
2582

2583
  return tColDataUpdateValueImpl[pColData->flag][pColVal->flag](
4,211,683✔
2584
      pColData, IS_VAR_DATA_TYPE(pColData->type) ? pColVal->value.pData : (uint8_t *)&pColVal->value.val,
4,211,683!
2585
      pColVal->value.nData, forward);
2586
}
2587

2588
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
74,642,390✔
2589
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
74,642,390✔
2590
}
74,642,390✔
2591
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,390,652✔
2592
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,390,652✔
2593
}
1,390,652✔
2594
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
11,213✔
2595
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
2596
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
11,213!
2597
    case 0:
1,020✔
2598
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,020✔
2599
      break;
1,020✔
2600
    case 1:
10,194✔
2601
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
10,194✔
2602
      break;
10,194✔
2603
    default:
×
2604
      break;
×
2605
  }
2606
}
11,213✔
2607
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
2608
  SValue value = {.type = pColData->type};
2,147,483,647✔
2609
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647!
2610
    if (iVal + 1 < pColData->nVal) {
240,137,858!
2611
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
245,276,466✔
2612
    } else {
2613
      value.nData = pColData->nData - pColData->aOffset[iVal];
×
2614
    }
2615
    value.pData = pColData->pData + pColData->aOffset[iVal];
240,137,858✔
2616
  } else {
2617
    (void)memcpy(&value.val, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
2618
                 tDataTypes[pColData->type].bytes);
2,147,483,647✔
2619
  }
2620
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
2621
}
201,286,979✔
2622
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
254,962✔
2623
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
2624
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
254,962!
2625
    case 0:
106,488✔
2626
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
106,488✔
2627
      break;
106,488✔
2628
    case 1:
148,700✔
2629
      tColDataGetValue4(pColData, iVal, pColVal);
2630
      break;
148,700✔
2631
    default:
×
2632
      break;
×
2633
  }
2634
}
254,962✔
2635
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
212,982,404✔
2636
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
2637
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
212,982,404!
2638
    case 0:
11,965,692✔
2639
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
11,965,692✔
2640
      break;
11,965,692✔
2641
    case 1:
201,138,278✔
2642
      tColDataGetValue4(pColData, iVal, pColVal);
2643
      break;
201,138,278✔
2644
    default:
×
2645
      break;
×
2646
  }
2647
}
212,982,404✔
2648
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
11✔
2649
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
2650
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
11!
2651
    case 0:
5✔
2652
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
5✔
2653
      break;
5✔
2654
    case 1:
5✔
2655
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
5✔
2656
      break;
5✔
2657
    case 2:
1!
2658
      tColDataGetValue4(pColData, iVal, pColVal);
2659
      break;
1✔
2660
    default:
×
2661
      break;
×
2662
  }
2663
}
11✔
2664
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
2665
    NULL,               // 0
2666
    tColDataGetValue1,  // HAS_NONE
2667
    tColDataGetValue2,  // HAS_NULL
2668
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
2669
    tColDataGetValue4,  // HAS_VALUE
2670
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
2671
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
2672
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
2673
};
2674
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
2675
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647!
2676
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647!
2677
    return TSDB_CODE_INVALID_PARA;
×
2678
  }
2679
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
2680
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
2681
}
2682

2683
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
580,093,236✔
2684
  switch (pColData->flag) {
580,093,236!
2685
    case HAS_NONE:
×
2686
      return 0;
×
2687
    case HAS_NULL:
×
2688
      return 1;
×
2689
    case (HAS_NULL | HAS_NONE):
11,220✔
2690
      return GET_BIT1(pColData->pBitMap, iVal);
11,220✔
2691
    case HAS_VALUE:
×
2692
      return 2;
×
2693
    case (HAS_VALUE | HAS_NONE):
743,145✔
2694
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
743,145✔
2695
    case (HAS_VALUE | HAS_NULL):
579,353,734✔
2696
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
579,353,734✔
2697
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
22✔
2698
      return GET_BIT2(pColData->pBitMap, iVal);
22✔
2699
    default:
×
2700
      return 0;
×
2701
  }
2702
}
2703

2704
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
448✔
2705
  int32_t code = 0;
448✔
2706

2707
  *pColData = *pColDataFrom;
448✔
2708

2709
  // bitmap
2710
  switch (pColData->flag) {
448!
2711
    case (HAS_NULL | HAS_NONE):
14✔
2712
    case (HAS_VALUE | HAS_NONE):
2713
    case (HAS_VALUE | HAS_NULL):
2714
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
14✔
2715
      if (pColData->pBitMap == NULL) {
14!
2716
        code = TSDB_CODE_OUT_OF_MEMORY;
×
2717
        goto _exit;
×
2718
      }
2719
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
14✔
2720
      break;
14✔
2721
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2722
      pColData->pBitMap = xMalloc(arg, BIT2_SIZE(pColData->nVal));
×
2723
      if (pColData->pBitMap == NULL) {
×
2724
        code = TSDB_CODE_OUT_OF_MEMORY;
×
2725
        goto _exit;
×
2726
      }
2727
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT2_SIZE(pColData->nVal));
×
2728
      break;
×
2729
    default:
434✔
2730
      pColData->pBitMap = NULL;
434✔
2731
      break;
434✔
2732
  }
2733

2734
  // offset
2735
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
448!
2736
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
104✔
2737
    if (pColData->aOffset == NULL) {
104!
2738
      code = TSDB_CODE_OUT_OF_MEMORY;
×
2739
      goto _exit;
×
2740
    }
2741
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
104✔
2742
  } else {
2743
    pColData->aOffset = NULL;
344✔
2744
  }
2745

2746
  // value
2747
  if (pColData->nData) {
448✔
2748
    pColData->pData = xMalloc(arg, pColData->nData);
360✔
2749
    if (pColData->pData == NULL) {
360!
2750
      code = TSDB_CODE_OUT_OF_MEMORY;
×
2751
      goto _exit;
×
2752
    }
2753

2754
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
360✔
2755
  } else {
2756
    pColData->pData = NULL;
88✔
2757
  }
2758

2759
_exit:
448✔
2760
  return code;
448✔
2761
}
2762

2763
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
2,483,424✔
2764
  int32_t code;
2765
  SBuffer local;
2766

2767
  if (!(colData->nVal > 0)) {
2,483,424!
2768
    return TSDB_CODE_INVALID_PARA;
×
2769
  }
2770

2771
  (*info) = (SColDataCompressInfo){
2,483,424✔
2772
      .cmprAlg = info->cmprAlg,
2,483,424✔
2773
      .columnFlag = colData->cflag,
2,483,424✔
2774
      .flag = colData->flag,
2,483,424✔
2775
      .dataType = colData->type,
2,483,424✔
2776
      .columnId = colData->cid,
2,483,424✔
2777
      .numOfData = colData->nVal,
2,483,424✔
2778
  };
2779

2780
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
2,483,424!
2781
    return 0;
135,670✔
2782
  }
2783

2784
  tBufferInit(&local);
2785
  if (assist == NULL) {
2,347,754!
2786
    assist = &local;
×
2787
  }
2788

2789
  // bitmap
2790
  if (colData->flag != HAS_VALUE) {
2,347,754✔
2791
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
248,743✔
2792
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1✔
2793
    } else {
2794
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
248,742✔
2795
    }
2796

2797
    SCompressInfo cinfo = {
248,743✔
2798
        .dataType = TSDB_DATA_TYPE_TINYINT,
2799
        .cmprAlg = info->cmprAlg,
248,743✔
2800
        .originalSize = info->bitmapOriginalSize,
248,743✔
2801
    };
2802

2803
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
248,743✔
2804
    if (code) {
248,758!
2805
      tBufferDestroy(&local);
2806
      return code;
×
2807
    }
2808

2809
    info->bitmapCompressedSize = cinfo.compressedSize;
248,758✔
2810
  }
2811

2812
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
2,347,769✔
2813
    tBufferDestroy(&local);
2814
    return 0;
601✔
2815
  }
2816

2817
  // offset
2818
  if (IS_VAR_DATA_TYPE(colData->type)) {
2,347,168!
2819
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
326,823✔
2820

2821
    SCompressInfo cinfo = {
326,823✔
2822
        .dataType = TSDB_DATA_TYPE_INT,
2823
        .cmprAlg = info->cmprAlg,
326,823✔
2824
        .originalSize = info->offsetOriginalSize,
326,823✔
2825
    };
2826

2827
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
326,823✔
2828
    if (code) {
326,946!
2829
      tBufferDestroy(&local);
2830
      return code;
×
2831
    }
2832

2833
    info->offsetCompressedSize = cinfo.compressedSize;
326,946✔
2834
  }
2835

2836
  // data
2837
  if (colData->nData > 0) {
2,347,291✔
2838
    info->dataOriginalSize = colData->nData;
2,347,151✔
2839

2840
    SCompressInfo cinfo = {
2,347,151✔
2841
        .dataType = colData->type,
2,347,151✔
2842
        .cmprAlg = info->cmprAlg,
2,347,151✔
2843
        .originalSize = info->dataOriginalSize,
2,347,151✔
2844
    };
2845

2846
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
2,347,151✔
2847
    if (code) {
2,347,466!
2848
      tBufferDestroy(&local);
2849
      return code;
×
2850
    }
2851

2852
    info->dataCompressedSize = cinfo.compressedSize;
2,347,466✔
2853
  }
2854

2855
  tBufferDestroy(&local);
2856
  return 0;
2,347,606✔
2857
}
2858

2859
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
18,656,117✔
2860
  int32_t  code;
2861
  SBuffer  local;
2862
  uint8_t *data = (uint8_t *)input;
18,656,117✔
2863

2864
  tBufferInit(&local);
2865
  if (assist == NULL) {
18,656,117!
2866
    assist = &local;
×
2867
  }
2868

2869
  tColDataClear(colData);
18,656,117✔
2870
  colData->cid = info->columnId;
18,655,345✔
2871
  colData->type = info->dataType;
18,655,345✔
2872
  colData->cflag = info->columnFlag;
18,655,345✔
2873
  colData->nVal = info->numOfData;
18,655,345✔
2874
  colData->flag = info->flag;
18,655,345✔
2875

2876
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
18,655,345✔
2877
    goto _exit;
2,078,721✔
2878
  }
2879

2880
  // bitmap
2881
  if (info->bitmapOriginalSize > 0) {
16,576,624✔
2882
    SCompressInfo cinfo = {
726,713✔
2883
        .dataType = TSDB_DATA_TYPE_TINYINT,
2884
        .cmprAlg = info->cmprAlg,
726,713✔
2885
        .originalSize = info->bitmapOriginalSize,
726,713✔
2886
        .compressedSize = info->bitmapCompressedSize,
726,713✔
2887
    };
2888

2889
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
726,713!
2890
    if (code) {
726,768!
2891
      tBufferDestroy(&local);
2892
      return code;
×
2893
    }
2894

2895
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
726,768✔
2896
    if (code) {
726,590!
2897
      tBufferDestroy(&local);
2898
      return code;
×
2899
    }
2900

2901
    data += cinfo.compressedSize;
726,590✔
2902
  }
2903

2904
  if (info->flag == (HAS_NONE | HAS_NULL)) {
16,576,501✔
2905
    goto _exit;
604✔
2906
  }
2907

2908
  // offset
2909
  if (info->offsetOriginalSize > 0) {
16,575,897✔
2910
    SCompressInfo cinfo = {
3,095,850✔
2911
        .cmprAlg = info->cmprAlg,
3,095,850✔
2912
        .dataType = TSDB_DATA_TYPE_INT,
2913
        .originalSize = info->offsetOriginalSize,
3,095,850✔
2914
        .compressedSize = info->offsetCompressedSize,
3,095,850✔
2915
    };
2916

2917
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
3,095,850!
2918
    if (code) {
3,095,922!
2919
      tBufferDestroy(&local);
2920
      return code;
×
2921
    }
2922

2923
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
3,095,922✔
2924
    if (code) {
3,095,669!
2925
      tBufferDestroy(&local);
2926
      return code;
×
2927
    }
2928

2929
    data += cinfo.compressedSize;
3,095,669✔
2930
  }
2931

2932
  // data
2933
  if (info->dataOriginalSize > 0) {
16,575,716✔
2934
    colData->nData = info->dataOriginalSize;
16,574,490✔
2935

2936
    SCompressInfo cinfo = {
16,574,490✔
2937
        .cmprAlg = info->cmprAlg,
16,574,490✔
2938
        .dataType = colData->type,
16,574,490✔
2939
        .originalSize = info->dataOriginalSize,
16,574,490✔
2940
        .compressedSize = info->dataCompressedSize,
16,574,490✔
2941
    };
2942

2943
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
16,574,490!
2944
    if (code) {
16,575,252!
2945
      tBufferDestroy(&local);
2946
      return code;
×
2947
    }
2948

2949
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
16,575,252✔
2950
    if (code) {
16,574,861!
2951
      tBufferDestroy(&local);
2952
      return code;
×
2953
    }
2954

2955
    data += cinfo.compressedSize;
16,574,861✔
2956
  }
2957

2958
_exit:
1,226✔
2959
  switch (colData->flag) {
18,655,412✔
2960
    case HAS_NONE:
1,990,581✔
2961
      colData->numOfNone = colData->nVal;
1,990,581✔
2962
      break;
1,990,581✔
2963
    case HAS_NULL:
89,489✔
2964
      colData->numOfNull = colData->nVal;
89,489✔
2965
      break;
89,489✔
2966
    case HAS_VALUE:
15,849,024✔
2967
      colData->numOfValue = colData->nVal;
15,849,024✔
2968
      break;
15,849,024✔
2969
    default:
726,318✔
2970
      for (int32_t i = 0; i < colData->nVal; i++) {
315,967,641✔
2971
        uint8_t bitValue = tColDataGetBitValue(colData, i);
315,242,744✔
2972
        if (bitValue == 0) {
315,241,323✔
2973
          colData->numOfNone++;
213,011✔
2974
        } else if (bitValue == 1) {
315,028,312✔
2975
          colData->numOfNull++;
23,392,232✔
2976
        } else {
2977
          colData->numOfValue++;
291,636,080✔
2978
        }
2979
      }
2980
  }
2981
  tBufferDestroy(&local);
2982
  return 0;
18,653,991✔
2983
}
2984

2985
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
571✔
2986
                                    char *data) {
2987
  int32_t code = 0;
571✔
2988
  if (data == NULL) {
571✔
2989
    if (pColData->cflag & COL_IS_KEY) {
11!
2990
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
2991
    } else {
2992
      for (int32_t i = 0; i < nRows; ++i) {
22✔
2993
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
11✔
2994
      }
2995
    }
2996
    goto _exit;
11✔
2997
  }
2998

2999
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
560!
3000
    for (int32_t i = 0; i < nRows; ++i) {
271✔
3001
      int32_t offset = *((int32_t *)lengthOrbitmap + i);
171✔
3002
      if (offset == -1) {
171!
3003
        if (pColData->cflag & COL_IS_KEY) {
×
3004
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3005
          goto _exit;
×
3006
        }
3007
        if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
3008
          goto _exit;
×
3009
        }
3010
      } else {
3011
        if (varDataTLen(data + offset) > bytes) {
171!
3012
          uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
3013
                 bytes);
3014
          code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
3015
          goto _exit;
×
3016
        }
3017
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
171✔
3018
                                                                      varDataLen(data + offset));
171✔
3019
      }
3020
    }
3021
  } else {  // fixed-length data type
3022
    bool allValue = true;
460✔
3023
    bool allNull = true;
460✔
3024
    for (int32_t i = 0; i < nRows; ++i) {
1,264✔
3025
      if (!colDataIsNull_f(lengthOrbitmap, i)) {
804✔
3026
        allNull = false;
591✔
3027
      } else {
3028
        allValue = false;
213✔
3029
      }
3030
    }
3031
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
460!
3032
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3033
      goto _exit;
×
3034
    }
3035

3036
    if (allValue) {
460✔
3037
      // optimize (todo)
3038
      for (int32_t i = 0; i < nRows; ++i) {
943✔
3039
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
577✔
3040
      }
3041
    } else if (allNull) {
94✔
3042
      // optimize (todo)
3043
      for (int32_t i = 0; i < nRows; ++i) {
249✔
3044
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
165✔
3045
        if (code) goto _exit;
165!
3046
      }
3047
    } else {
3048
      for (int32_t i = 0; i < nRows; ++i) {
72✔
3049
        if (colDataIsNull_f(lengthOrbitmap, i)) {
62✔
3050
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
48✔
3051
          if (code) goto _exit;
48!
3052
        } else {
3053
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
14✔
3054
        }
3055
      }
3056
    }
3057
  }
3058

3059
_exit:
10✔
3060
  return code;
571✔
3061
}
3062

3063
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32_t buffMaxLen, initGeosFn igeos,
2,249,105✔
3064
                               checkWKBGeometryFn cgeos) {
3065
  int32_t code = 0;
2,249,105✔
3066

3067
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
2,249,105✔
3068
    if (!(pColData->type == pBind->buffer_type)) {
2,249,102!
3069
      return TSDB_CODE_INVALID_PARA;
×
3070
    }
3071
  }
3072

3073
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
2,249,105!
3074
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
318,293✔
3075
      code = igeos();
2✔
3076
      if (code) {
2!
3077
        return code;
×
3078
      }
3079
    }
3080
    for (int32_t i = 0; i < pBind->num; ++i) {
949,943✔
3081
      if (pBind->is_null && pBind->is_null[i]) {
631,660✔
3082
        if (pColData->cflag & COL_IS_KEY) {
2,601!
3083
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3084
          goto _exit;
×
3085
        }
3086
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2,601✔
3087
        if (code) goto _exit;
2,601!
3088
      } else if (pBind->length[i] > buffMaxLen) {
629,059✔
3089
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
9✔
3090
      } else {
3091
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
629,050✔
3092
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
4✔
3093
          if (code) goto _exit;
4!
3094
        }
3095
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
629,050✔
3096
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
629,050✔
3097
      }
3098
    }
3099
  } else {  // fixed-length data type
3100
    bool allValue;
3101
    bool allNull;
3102
    if (pBind->is_null) {
1,930,812✔
3103
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
15,831✔
3104
      allNull = (same && pBind->is_null[0] != 0);
15,831✔
3105
      allValue = (same && pBind->is_null[0] == 0);
15,831✔
3106
    } else {
3107
      allNull = false;
1,914,981✔
3108
      allValue = true;
1,914,981✔
3109
    }
3110

3111
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
1,930,812!
3112
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3113
      goto _exit;
×
3114
    }
3115

3116
    if (allValue) {
1,930,812✔
3117
      // optimize (todo)
3118
      for (int32_t i = 0; i < pBind->num; ++i) {
5,741,060✔
3119
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
3,825,680✔
3120
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
3,825,680✔
3121
      }
3122
    } else if (allNull) {
15,455✔
3123
      // optimize (todo)
3124
      for (int32_t i = 0; i < pBind->num; ++i) {
4✔
3125
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2✔
3126
        if (code) goto _exit;
2!
3127
      }
3128
    } else {
3129
      for (int32_t i = 0; i < pBind->num; ++i) {
47,167✔
3130
        if (pBind->is_null[i]) {
31,714✔
3131
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
15,857✔
3132
          if (code) goto _exit;
15,857!
3133
        } else {
3134
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
15,857✔
3135
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
15,857✔
3136
        }
3137
      }
3138
    }
3139
  }
3140

3141
_exit:
15,453✔
3142
  return code;
2,249,118✔
3143
}
3144

3145
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen, initGeosFn igeos,
169✔
3146
                                checkWKBGeometryFn cgeos) {
3147
  int32_t code = 0;
169✔
3148

3149
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
169!
3150
    if (!(pColData->type == pBind->buffer_type)) {
169!
3151
      return TSDB_CODE_INVALID_PARA;
×
3152
    }
3153
  }
3154

3155
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
239!
3156
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
70✔
3157
      code = igeos();
2✔
3158
      if (code) {
2!
3159
        return code;
×
3160
      }
3161
    }
3162

3163
    uint8_t *buf = pBind->buffer;
70✔
3164
    for (int32_t i = 0; i < pBind->num; ++i) {
274✔
3165
      if (pBind->is_null && pBind->is_null[i]) {
204!
3166
        if (pColData->cflag & COL_IS_KEY) {
×
3167
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3168
          goto _exit;
×
3169
        }
3170
        if (pBind->is_null[i] == 1) {
×
3171
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
3172
          if (code) goto _exit;
×
3173
        } else {
3174
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
3175
          if (code) goto _exit;
×
3176
        }
3177
      } else if (pBind->length[i] > buffMaxLen) {
204!
3178
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
3179
      } else {
3180
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
204✔
3181
          code = cgeos(buf, pBind->length[i]);
4✔
3182
          if (code) goto _exit;
4!
3183
        }
3184
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
204✔
3185
        buf += pBind->length[i];
204✔
3186
      }
3187
    }
3188
  } else {  // fixed-length data type
3189
    bool allValue;
3190
    bool allNull;
3191
    bool allNone;
3192
    if (pBind->is_null) {
99!
3193
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
×
3194
      allNull = (same && pBind->is_null[0] == 1);
×
3195
      allNone = (same && pBind->is_null[0] > 1);
×
3196
      allValue = (same && pBind->is_null[0] == 0);
×
3197
    } else {
3198
      allNull = false;
99✔
3199
      allNone = false;
99✔
3200
      allValue = true;
99✔
3201
    }
3202

3203
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
99!
3204
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3205
      goto _exit;
×
3206
    }
3207

3208
    if (allValue) {
99!
3209
      // optimize (todo)
3210
      for (int32_t i = 0; i < pBind->num; ++i) {
356✔
3211
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
257✔
3212
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
257!
3213
          *val = 1;
1✔
3214
        }
3215

3216
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
257✔
3217
      }
3218
    } else if (allNull) {
×
3219
      // optimize (todo)
3220
      for (int32_t i = 0; i < pBind->num; ++i) {
×
3221
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
3222
        if (code) goto _exit;
×
3223
      }
3224
    } else if (allNone) {
×
3225
      // optimize (todo)
3226
      for (int32_t i = 0; i < pBind->num; ++i) {
×
3227
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
3228
        if (code) goto _exit;
×
3229
      }
3230
    } else {
3231
      for (int32_t i = 0; i < pBind->num; ++i) {
×
3232
        if (pBind->is_null[i]) {
×
3233
          if (pBind->is_null[i] == 1) {
×
3234
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
3235
            if (code) goto _exit;
×
3236
          } else {
3237
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
3238
            if (code) goto _exit;
×
3239
          }
3240
        } else {
3241
          uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
×
3242
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
×
3243
            *val = 1;
×
3244
          }
3245

3246
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
×
3247
        }
3248
      }
3249
    }
3250
  }
3251

3252
_exit:
×
3253
  return code;
169✔
3254
}
3255

3256
/* build rows to `rowArray` from bind
3257
 * `infos` is the bind information array
3258
 * `numOfInfos` is the number of bind information
3259
 * `infoSorted` is whether the bind information is sorted by column id
3260
 * `pTSchema` is the schema of the table
3261
 * `rowArray` is the array to store the rows
3262
 * `pOrdered` is the pointer to store ordered
3263
 * `pDupTs` is the pointer to store duplicateTs
3264
 */
3265
int32_t tRowBuildFromBind2(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
19✔
3266
                           SArray *rowArray, bool *pOrdered, bool *pDupTs) {
3267
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
19!
3268
    return TSDB_CODE_INVALID_PARA;
×
3269
  }
3270

3271
  if (!infoSorted) {
19!
3272
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
3273
  }
3274

3275
  int32_t code = 0;
19✔
3276
  int32_t numOfRows = infos[0].bind->num;
19✔
3277
  SArray *colValArray, *bufArray;
3278
  SColVal colVal;
3279

3280
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
19!
3281
    return terrno;
×
3282
  }
3283
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
19!
3284
    taosArrayDestroy(colValArray);
×
3285
    return terrno;
×
3286
  }
3287
  for (int i = 0; i < numOfInfos; ++i) {
61✔
3288
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
84!
3289
      taosArrayDestroy(colValArray);
×
3290
      taosArrayDestroy(bufArray);
×
3291
      return terrno;
×
3292
    }
3293
  }
3294

3295
  SRowKey rowKey, lastRowKey;
3296
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
83✔
3297
    taosArrayClear(colValArray);
64✔
3298

3299
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
232✔
3300
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
168!
3301
        if (infos[iInfo].bind->is_null[iRow] == 1) {
×
3302
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
×
3303
        } else {
3304
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
3305
        }
3306
      } else {
3307
        SValue value = {
168✔
3308
            .type = infos[iInfo].type,
168✔
3309
        };
3310
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
168!
3311
          int32_t   length = infos[iInfo].bind->length[iRow];
104✔
3312
          uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
104✔
3313
          value.nData = length;
104✔
3314
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
104!
3315
            code = TSDB_CODE_INVALID_PARA;
×
3316
            goto _exit;
×
3317
          }
3318
          value.pData = *data;
104✔
3319
          *data += length;
104✔
3320
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
3321
        } else {
3322
          uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
64✔
3323
          if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
64!
3324
            *val = 1;
×
3325
          }
3326
          (void)memcpy(&value.val, val,
64✔
3327
                       /*(uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,*/
3328
                       infos[iInfo].bytes /*bind->buffer_length*/);
64✔
3329
        }
3330
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
168✔
3331
      }
3332
      if (taosArrayPush(colValArray, &colVal) == NULL) {
168!
3333
        code = terrno;
×
3334
        goto _exit;
×
3335
      }
3336
    }
3337

3338
    SRow *row;
3339
    if ((code = tRowBuild(colValArray, pTSchema, &row))) {
64!
3340
      goto _exit;
×
3341
    }
3342

3343
    if ((taosArrayPush(rowArray, &row)) == NULL) {
64!
3344
      code = terrno;
×
3345
      goto _exit;
×
3346
    }
3347

3348
    if (pOrdered && pDupTs) {
64!
3349
      tRowGetKey(row, &rowKey);
64!
3350
      if (iRow == 0) {
64✔
3351
        *pOrdered = true;
19✔
3352
        *pDupTs = false;
19✔
3353
      } else {
3354
        // no more compare if we already get disordered or duplicate rows
3355
        if (*pOrdered && !*pDupTs) {
45!
3356
          int32_t code = tRowKeyCompare(&rowKey, &lastRowKey);
45✔
3357
          *pOrdered = (code >= 0);
45✔
3358
          *pDupTs = (code == 0);
45✔
3359
        }
3360
      }
3361
      lastRowKey = rowKey;
64✔
3362
    }
3363
  }
3364

3365
_exit:
19✔
3366
  taosArrayDestroy(colValArray);
19✔
3367
  taosArrayDestroy(bufArray);
19✔
3368
  return code;
19✔
3369
}
3370

3371
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
286✔
3372
  int32_t code = TSDB_CODE_SUCCESS;
286✔
3373

3374
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
286!
3375
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
116✔
3376
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
42✔
3377
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
58✔
3378
    if (iToRow == 0) {
58✔
3379
      pToColData->aOffset[iToRow] = 0;
8✔
3380
    }
3381

3382
    if (iToRow < pToColData->nVal - 1) {
58✔
3383
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
52✔
3384
    }
3385

3386
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
58✔
3387
                 nData);
3388
  } else {
3389
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
228✔
3390
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
228✔
3391
  }
3392
  return code;
286✔
3393
}
3394

3395
static int32_t tColDataCopyRowSingleCol(SColData *pFromColData, int32_t iFromRow, SColData *pToColData,
294✔
3396
                                        int32_t iToRow) {
3397
  int32_t code = TSDB_CODE_SUCCESS;
294✔
3398

3399
  switch (pFromColData->flag) {
294!
3400
    case HAS_NONE:
8✔
3401
    case HAS_NULL:
3402
      break;
8✔
3403
    case (HAS_NULL | HAS_NONE): {
×
3404
      SET_BIT1(pToColData->pBitMap, iToRow, GET_BIT1(pFromColData->pBitMap, iFromRow));
×
3405
    } break;
×
3406
    case HAS_VALUE: {
238✔
3407
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
238!
3408
    } break;
238✔
3409
    case (HAS_VALUE | HAS_NONE):
48✔
3410
    case (HAS_VALUE | HAS_NULL): {
3411
      SET_BIT1(pToColData->pBitMap, iToRow, GET_BIT1(pFromColData->pBitMap, iFromRow));
48✔
3412
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
48!
3413
    } break;
48✔
3414
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
3415
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
3416
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
3417
    } break;
×
3418
    default:
×
3419
      return -1;
×
3420
  }
3421

3422
  return code;
294✔
3423
}
3424

3425
static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
60✔
3426
                               int32_t nColData) {
3427
  int32_t code = TSDB_CODE_SUCCESS;
60✔
3428

3429
  for (int32_t i = 0; i < nColData; i++) {
354✔
3430
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
294✔
3431
    if (code != TSDB_CODE_SUCCESS) {
294!
3432
      return code;
×
3433
    }
3434
  }
3435

3436
  return code;
60✔
3437
}
3438

3439
static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
60✔
3440
  int32_t code = TSDB_CODE_SUCCESS;
60✔
3441

3442
  for (int32_t i = 0; i < nColData; i++) {
354✔
3443
    SColVal cv = {0};
294✔
3444
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
294✔
3445
    if (code != TSDB_CODE_SUCCESS) {
294!
3446
      return code;
×
3447
    }
3448
    code = tColDataAppendValue(&aToColData[i], &cv);
294✔
3449
    if (code != TSDB_CODE_SUCCESS) {
294!
3450
      return code;
×
3451
    }
3452
  }
3453

3454
  return code;
60✔
3455
}
3456

3457
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
1,070,086✔
3458
  SColVal cv;
3459

3460
  key->ts = ((TSKEY *)aColData[0].pData)[iRow];
1,070,086✔
3461
  key->numOfPKs = 0;
1,070,086✔
3462

3463
  for (int i = 1; i < nColData; i++) {
1,070,086✔
3464
    if (aColData[i].cflag & COL_IS_KEY) {
1,070,036!
3465
      tColDataGetValue4(&aColData[i], iRow, &cv);
×
3466
      key->pks[key->numOfPKs++] = cv.value;
×
3467
    } else {
3468
      break;
1,070,036✔
3469
    }
3470
  }
3471
}
1,070,086✔
3472

3473
static int32_t tColDataMergeSortMerge(SColData *aColData, int32_t start, int32_t mid, int32_t end, int32_t nColData) {
17✔
3474
  SColData *aDstColData = NULL;
17✔
3475
  int32_t   i = start, j = mid + 1, k = 0;
17✔
3476
  SRowKey   keyi, keyj;
3477

3478
  if (end > start) {
17!
3479
    aDstColData = taosMemoryCalloc(1, sizeof(SColData) * nColData);
17!
3480
    if (aDstColData == NULL) {
17!
3481
      return terrno;
×
3482
    }
3483
    for (int c = 0; c < nColData; ++c) {
99✔
3484
      tColDataInit(&aDstColData[c], aColData[c].cid, aColData[c].type, aColData[c].cflag);
82✔
3485
    }
3486
  }
3487

3488
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
17✔
3489
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
17✔
3490
  while (i <= mid && j <= end) {
48✔
3491
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
31✔
3492
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
24!
3493
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
24✔
3494
    } else {
3495
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
7!
3496
      tColDataArrGetRowKey(aColData, nColData, j, &keyj);
7✔
3497
    }
3498
  }
3499

3500
  while (i <= mid) {
26✔
3501
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
9!
3502
  }
3503

3504
  while (j <= end) {
37✔
3505
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
20!
3506
  }
3507

3508
  for (i = start, k = 0; i <= end; ++i, ++k) {
77✔
3509
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
60!
3510
  }
3511

3512
  if (aDstColData) {
17!
3513
    for (int32_t i = 0; i < nColData; i++) {
99✔
3514
      tColDataDestroy(&aDstColData[i]);
82✔
3515
    }
3516
    taosMemoryFree(aDstColData);
17!
3517
  }
3518

3519
  return TSDB_CODE_SUCCESS;
17✔
3520
}
3521

3522
static int32_t tColDataMergeSort(SColData *aColData, int32_t start, int32_t end, int32_t nColData) {
37✔
3523
  int32_t ret = TSDB_CODE_SUCCESS;
37✔
3524
  int32_t mid;
3525

3526
  if (start >= end) {
37✔
3527
    return TSDB_CODE_SUCCESS;
20✔
3528
  }
3529

3530
  mid = (start + end) / 2;
17✔
3531

3532
  ret = tColDataMergeSort(aColData, start, mid, nColData);
17✔
3533
  if (ret != TSDB_CODE_SUCCESS) {
17!
3534
    return ret;
×
3535
  }
3536

3537
  ret = tColDataMergeSort(aColData, mid + 1, end, nColData);
17✔
3538
  if (ret != TSDB_CODE_SUCCESS) {
17!
3539
    return ret;
×
3540
  }
3541

3542
  return tColDataMergeSortMerge(aColData, start, mid, end, nColData);
17✔
3543
}
3544

3545
static int32_t tColDataSort(SColData *aColData, int32_t nColData) {
3✔
3546
  int32_t nVal = aColData[0].nVal;
3✔
3547

3548
  if (nVal < 2) return TSDB_CODE_SUCCESS;
3!
3549

3550
  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
3✔
3551
}
3552

3553
static int32_t tColDataMerge(SArray **colArr) {
3✔
3554
  int32_t code = 0;
3✔
3555
  SArray *src = *colArr;
3✔
3556
  SArray *dst = NULL;
3✔
3557

3558
  dst = taosArrayInit(taosArrayGetSize(src), sizeof(SColData));
3✔
3559
  if (dst == NULL) {
3!
3560
    return terrno;
×
3561
  }
3562

3563
  for (int32_t i = 0; i < taosArrayGetSize(src); i++) {
12✔
3564
    SColData *srcCol = taosArrayGet(src, i);
9✔
3565

3566
    SColData *dstCol = taosArrayReserve(dst, 1);
9✔
3567
    if (dstCol == NULL) {
9!
3568
      code = terrno;
×
3569
      goto _exit;
×
3570
    }
3571
    tColDataInit(dstCol, srcCol->cid, srcCol->type, srcCol->cflag);
9✔
3572
  }
3573

3574
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
3✔
3575
  SRowKey lastKey;
3576
  for (int32_t i = 0; i < numRows; i++) {
9✔
3577
    SRowKey key;
3578
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
6✔
3579

3580
    if (i == 0 || tRowKeyCompare(&key, &lastKey) != 0) {  // append new row
9!
3581
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
12✔
3582
        SColData *srcCol = taosArrayGet(src, j);
9✔
3583
        SColData *dstCol = taosArrayGet(dst, j);
9✔
3584

3585
        SColVal cv;
3586
        code = tColDataGetValue(srcCol, i, &cv);
9✔
3587
        if (code != TSDB_CODE_SUCCESS) {
9!
3588
          goto _exit;
×
3589
        }
3590
        code = tColDataAppendValue(dstCol, &cv);
9✔
3591
        if (code) {
9!
3592
          goto _exit;
×
3593
        }
3594
      }
3595
      lastKey = key;
3✔
3596
    } else {  // update existing row
3597
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
12✔
3598
        SColData *srcCol = taosArrayGet(src, j);
9✔
3599
        SColData *dstCol = taosArrayGet(dst, j);
9✔
3600

3601
        SColVal cv;
3602
        code = tColDataGetValue(srcCol, i, &cv);
9✔
3603
        if (code != TSDB_CODE_SUCCESS) {
9!
3604
          goto _exit;
×
3605
        }
3606
        code = tColDataUpdateValue(dstCol, &cv, true);
9✔
3607
        if (code) {
9!
3608
          goto _exit;
×
3609
        }
3610
      }
3611
    }
3612
  }
3613

3614
_exit:
3✔
3615
  if (code) {
3!
3616
    taosArrayDestroyEx(dst, tColDataDestroy);
×
3617
  } else {
3618
    taosArrayDestroyEx(src, tColDataDestroy);
3✔
3619
    *colArr = dst;
3✔
3620
  }
3621
  return code;
3✔
3622
}
3623

3624
int32_t tColDataSortMerge(SArray **arr) {
92,299✔
3625
  SArray   *colDataArr = *arr;
92,299✔
3626
  int32_t   nColData = TARRAY_SIZE(colDataArr);
92,299✔
3627
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
92,299✔
3628

3629
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
92,299!
3630
    return TSDB_CODE_INVALID_PARA;
×
3631
  }
3632
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
92,299!
3633
    return TSDB_CODE_INVALID_PARA;
×
3634
  }
3635
  if (!(aColData[0].flag == HAS_VALUE)) {
92,299!
3636
    return TSDB_CODE_INVALID_PARA;
×
3637
  }
3638

3639
  if (aColData[0].nVal <= 1) goto _exit;
92,299✔
3640

3641
  int8_t doSort = 0;
90,598✔
3642
  int8_t doMerge = 0;
90,598✔
3643
  // scan -------
3644
  SRowKey lastKey;
3645
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
90,598✔
3646
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
534,891✔
3647
    SRowKey key;
3648
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
444,299✔
3649

3650
    int32_t c = tRowKeyCompare(&lastKey, &key);
444,297✔
3651
    if (c < 0) {
444,297✔
3652
      lastKey = key;
444,292✔
3653
      continue;
444,292✔
3654
    } else if (c > 0) {
5✔
3655
      doSort = 1;
3✔
3656
      break;
3✔
3657
    } else {
3658
      doMerge = 1;
2✔
3659
    }
3660
  }
3661

3662
  // sort -------
3663
  if (doSort) {
90,595✔
3664
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
3!
3665
  }
3666

3667
  if (doMerge != 1) {
90,603✔
3668
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
90,595✔
3669
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
534,901✔
3670
      SRowKey key;
3671
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
444,311✔
3672

3673
      int32_t c = tRowKeyCompare(&lastKey, &key);
444,306✔
3674
      if (c == 0) {
444,306!
3675
        doMerge = 1;
×
3676
        break;
×
3677
      }
3678
      lastKey = key;
444,306✔
3679
    }
3680
  }
3681

3682
  // merge -------
3683
  if (doMerge) {
90,598✔
3684
    int32_t code = tColDataMerge(arr);
3✔
3685
    if (code) return code;
3!
3686
  }
3687

3688
_exit:
90,598✔
3689
  return 0;
92,299✔
3690
}
3691

3692
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
1,605,225✔
3693
  int32_t code = 0;
1,605,225✔
3694

3695
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
3,210,450!
3696
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
3,210,450!
3697
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
3,210,450!
3698
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
3,210,450!
3699

3700
  // bitmap
3701
  switch (pColData->flag) {
1,605,225!
3702
    case (HAS_NULL | HAS_NONE):
17,150✔
3703
    case (HAS_VALUE | HAS_NONE):
3704
    case (HAS_VALUE | HAS_NULL):
3705
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
17,150✔
3706
      if (code) return code;
17,150!
3707
      break;
17,150✔
3708
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
3709
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
3710
      if (code) return code;
×
3711
      break;
×
3712
    default:
1,588,075✔
3713
      break;
1,588,075✔
3714
  }
3715

3716
  // value
3717
  if (pColData->flag & HAS_VALUE) {
1,605,225✔
3718
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,605,067!
3719
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
197,582✔
3720
      if (code) return code;
197,582!
3721

3722
      code = tEncodeI32v(pEncoder, pColData->nData);
197,582✔
3723
      if (code) return code;
197,582!
3724

3725
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
197,582✔
3726
      if (code) return code;
197,582!
3727
    } else {
3728
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
1,407,485✔
3729
      if (code) return code;
1,407,485!
3730
    }
3731
  }
3732

3733
  return code;
1,605,225✔
3734
}
3735

3736
static int32_t tDecodeColDataVersion0(SDecoder *pDecoder, SColData *pColData) {
2,007✔
3737
  int32_t code = 0;
2,007✔
3738

3739
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
4,016!
3740
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
4,018!
3741
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
4,017!
3742
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
4,017!
3743

3744
  if (pColData->type <= 0 || pColData->type >= TSDB_DATA_TYPE_MAX || pColData->flag <= 0 || pColData->flag >= 8) {
2,009!
3745
    return TSDB_CODE_INVALID_PARA;
×
3746
  }
3747

3748
  // bitmap
3749
  switch (pColData->flag) {
2,009!
3750
    case (HAS_NULL | HAS_NONE):
52✔
3751
    case (HAS_VALUE | HAS_NONE):
3752
    case (HAS_VALUE | HAS_NULL):
3753
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
52!
3754
      if (code) return code;
52!
3755
      break;
52✔
3756
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
3757
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
3758
      if (code) return code;
×
3759
      break;
×
3760
    default:
1,957✔
3761
      break;
1,957✔
3762
  }
3763

3764
  // value
3765
  if (pColData->flag & HAS_VALUE) {
2,009✔
3766
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,816!
3767
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
376!
3768
      if (code) return code;
376!
3769

3770
      code = tDecodeI32v(pDecoder, &pColData->nData);
376✔
3771
      if (code) return code;
377!
3772

3773
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
377!
3774
      if (code) return code;
377!
3775
    } else {
3776
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
1,440✔
3777
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
1,440!
3778
      if (code) return code;
1,440!
3779
    }
3780
  }
3781
  pColData->cflag = 0;
2,010✔
3782

3783
  return code;
2,010✔
3784
}
3785

3786
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
1,605,224✔
3787
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
1,605,224✔
3788
  if (code) return code;
1,605,235!
3789
  return tEncodeI8(pEncoder, pColData->cflag);
3,210,470✔
3790
}
3791

3792
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
2,008✔
3793
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
2,008✔
3794
  if (code) return code;
2,009!
3795

3796
  code = tDecodeI8(pDecoder, &pColData->cflag);
2,009✔
3797
  return code;
2,009✔
3798
}
3799

3800
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
1,605,224✔
3801
  if (version == 0) {
1,605,224!
3802
    return tEncodeColDataVersion0(pEncoder, pColData);
×
3803
  } else if (version == 1) {
1,605,224!
3804
    return tEncodeColDataVersion1(pEncoder, pColData);
1,605,224✔
3805
  } else {
3806
    return TSDB_CODE_INVALID_PARA;
×
3807
  }
3808
}
3809

3810
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData) {
2,008✔
3811
  if (version == 0) {
2,008!
3812
    return tDecodeColDataVersion0(pDecoder, pColData);
×
3813
  } else if (version == 1) {
2,008!
3814
    return tDecodeColDataVersion1(pDecoder, pColData);
2,009✔
3815
  } else {
3816
    return TSDB_CODE_INVALID_PARA;
×
3817
  }
3818
}
3819

3820
int32_t tEncodeRow(SEncoder *pEncoder, SRow *pRow) { return tEncodeFixed(pEncoder, pRow, pRow->len); }
2,147,483,647✔
3821

3822
int32_t tDecodeRow(SDecoder *pDecoder, SRow **ppRow) {
781,080,675✔
3823
  if (ppRow == NULL) {
781,080,675!
3824
    return TSDB_CODE_INVALID_PARA;
×
3825
  }
3826

3827
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
781,080,675!
3828
    return TSDB_CODE_OUT_OF_RANGE;
×
3829
  }
3830

3831
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
781,080,675✔
3832
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
1,562,161,350!
3833
}
3834

3835
#define CALC_SUM_MAX_MIN(SUM, MAX, MIN, VAL) \
3836
  do {                                       \
3837
    (SUM) += (VAL);                          \
3838
    if ((MAX) < (VAL)) (MAX) = (VAL);        \
3839
    if ((MIN) > (VAL)) (MIN) = (VAL);        \
3840
  } while (0)
3841

3842
static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
7,101✔
3843
                                             int16_t *numOfNull) {
3844
  *sum = 0;
7,101✔
3845
  *max = 0;
7,101✔
3846
  *min = 1;
7,101✔
3847
  *numOfNull = 0;
7,101✔
3848

3849
  int8_t val;
3850
  if (HAS_VALUE == pColData->flag) {
7,101✔
3851
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,249,092✔
3852
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
4,243,459✔
3853
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,243,459✔
3854
    }
3855
  } else {
3856
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,288✔
3857
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,820!
3858
        case 0:
237,330✔
3859
        case 1:
3860
          (*numOfNull)++;
237,330✔
3861
          break;
237,330✔
3862
        case 2:
4,505,490✔
3863
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
4,505,490✔
3864
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,505,490✔
3865
          break;
4,505,490✔
3866
        default:
×
3867
          break;
×
3868
      }
3869
    }
3870
  }
3871
}
7,101✔
3872

3873
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
7,137✔
3874
                                                int16_t *numOfNull) {
3875
  *sum = 0;
7,137✔
3876
  *max = INT8_MIN;
7,137✔
3877
  *min = INT8_MAX;
7,137✔
3878
  *numOfNull = 0;
7,137✔
3879

3880
  int8_t val;
3881
  if (HAS_VALUE == pColData->flag) {
7,137✔
3882
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,366,194✔
3883
      val = ((int8_t *)pColData->pData)[iVal];
4,360,525✔
3884
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,360,525✔
3885
    }
3886
  } else {
3887
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,288✔
3888
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,820!
3889
        case 0:
236,430✔
3890
        case 1:
3891
          (*numOfNull)++;
236,430✔
3892
          break;
236,430✔
3893
        case 2:
4,506,390✔
3894
          val = ((int8_t *)pColData->pData)[iVal];
4,506,390✔
3895
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,506,390✔
3896
          break;
4,506,390✔
3897
        default:
×
3898
          break;
×
3899
      }
3900
    }
3901
  }
3902
}
7,137✔
3903

3904
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
7,136✔
3905
                                                     int16_t *numOfNull) {
3906
  *sum = 0;
7,136✔
3907
  *max = INT16_MIN;
7,136✔
3908
  *min = INT16_MAX;
7,136✔
3909
  *numOfNull = 0;
7,136✔
3910

3911
  int16_t val;
3912
  if (HAS_VALUE == pColData->flag) {
7,136✔
3913
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,366,051✔
3914
      val = ((int16_t *)pColData->pData)[iVal];
4,360,383✔
3915
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,360,383✔
3916
    }
3917
  } else {
3918
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,288✔
3919
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,820!
3920
        case 0:
238,260✔
3921
        case 1:
3922
          (*numOfNull)++;
238,260✔
3923
          break;
238,260✔
3924
        case 2:
4,504,560✔
3925
          val = ((int16_t *)pColData->pData)[iVal];
4,504,560✔
3926
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,504,560✔
3927
          break;
4,504,560✔
3928
        default:
×
3929
          break;
×
3930
      }
3931
    }
3932
  }
3933
}
7,136✔
3934

3935
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
229,417✔
3936
                                            int16_t *numOfNull) {
3937
  *sum = 0;
229,417✔
3938
  *max = INT32_MIN;
229,417✔
3939
  *min = INT32_MAX;
229,417✔
3940
  *numOfNull = 0;
229,417✔
3941

3942
  int32_t val;
3943
  if (HAS_VALUE == pColData->flag) {
229,417✔
3944
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
688,926,425✔
3945
      val = ((int32_t *)pColData->pData)[iVal];
688,726,170✔
3946
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
688,726,170✔
3947
    }
3948
  } else {
3949
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
81,656,338!
3950
      switch (tColDataGetBitValue(pColData, iVal)) {
81,658,518!
3951
        case 0:
4,884,995✔
3952
        case 1:
3953
          (*numOfNull)++;
4,884,995✔
3954
          break;
4,884,995✔
3955
        case 2:
76,817,086✔
3956
          val = ((int32_t *)pColData->pData)[iVal];
76,817,086✔
3957
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
76,817,086✔
3958
          break;
76,817,086✔
3959
        default:
×
3960
          break;
×
3961
      }
3962
    }
3963
  }
3964
}
198,075✔
3965

3966
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
85,052✔
3967
                                               int16_t *numOfNull) {
3968
  *sum = 0;
85,052✔
3969
  *max = INT64_MIN;
85,052✔
3970
  *min = INT64_MAX;
85,052✔
3971
  *numOfNull = 0;
85,052✔
3972

3973
  int64_t val;
3974
  if (HAS_VALUE == pColData->flag) {
85,052✔
3975
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
230,999,779✔
3976
      val = ((int64_t *)pColData->pData)[iVal];
230,920,332✔
3977
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
230,920,332✔
3978
    }
3979
  } else {
3980
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
19,814,820✔
3981
      switch (tColDataGetBitValue(pColData, iVal)) {
19,809,281!
3982
        case 0:
1,809,458✔
3983
        case 1:
3984
          (*numOfNull)++;
1,809,458✔
3985
          break;
1,809,458✔
3986
        case 2:
18,001,236✔
3987
          val = ((int64_t *)pColData->pData)[iVal];
18,001,236✔
3988
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
18,001,236✔
3989
          break;
18,001,236✔
3990
        default:
×
3991
          break;
×
3992
      }
3993
    }
3994
  }
3995
}
84,986✔
3996

3997
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
170,102✔
3998
                                              int16_t *numOfNull) {
3999
  *(double *)sum = 0;
170,102✔
4000
  *(double *)max = -FLT_MAX;
170,102✔
4001
  *(double *)min = FLT_MAX;
170,102✔
4002
  *numOfNull = 0;
170,102✔
4003

4004
  float val;
4005
  if (HAS_VALUE == pColData->flag) {
170,102✔
4006
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
670,704,511✔
4007
      val = ((float *)pColData->pData)[iVal];
670,535,878✔
4008
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
670,535,878✔
4009
    }
4010
  } else {
4011
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,394✔
4012
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,925!
4013
        case 0:
237,103✔
4014
        case 1:
4015
          (*numOfNull)++;
237,103✔
4016
          break;
237,103✔
4017
        case 2:
4,505,822✔
4018
          val = ((float *)pColData->pData)[iVal];
4,505,822✔
4019
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
4,505,822✔
4020
          break;
4,505,822✔
4021
        default:
×
4022
          break;
×
4023
      }
4024
    }
4025
  }
4026
}
170,102✔
4027

4028
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
8,342✔
4029
                                               int16_t *numOfNull) {
4030
  *(double *)sum = 0;
8,342✔
4031
  *(double *)max = -DBL_MAX;
8,342✔
4032
  *(double *)min = DBL_MAX;
8,342✔
4033
  *numOfNull = 0;
8,342✔
4034

4035
  double val;
4036
  if (HAS_VALUE == pColData->flag) {
8,342✔
4037
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
11,298,630✔
4038
      val = ((double *)pColData->pData)[iVal];
11,291,756✔
4039
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
11,291,756✔
4040
    }
4041
  } else {
4042
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,288✔
4043
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,820!
4044
        case 0:
238,044✔
4045
        case 1:
4046
          (*numOfNull)++;
238,044✔
4047
          break;
238,044✔
4048
        case 2:
4,504,776✔
4049
          val = ((double *)pColData->pData)[iVal];
4,504,776✔
4050
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
4,504,776✔
4051
          break;
4,504,776✔
4052
        default:
×
4053
          break;
×
4054
      }
4055
    }
4056
  }
4057
}
8,342✔
4058

4059
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
4,222✔
4060
                                                 int16_t *numOfNull) {
4061
  *(uint64_t *)sum = 0;
4,222✔
4062
  *(uint64_t *)max = 0;
4,222✔
4063
  *(uint64_t *)min = UINT8_MAX;
4,222✔
4064
  *numOfNull = 0;
4,222✔
4065

4066
  uint8_t val;
4067
  if (HAS_VALUE == pColData->flag) {
4,222✔
4068
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,484,574✔
4069
      val = ((uint8_t *)pColData->pData)[iVal];
3,481,820✔
4070
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,481,820✔
4071
    }
4072
  } else {
4073
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,288✔
4074
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,820!
4075
        case 0:
238,084✔
4076
        case 1:
4077
          (*numOfNull)++;
238,084✔
4078
          break;
238,084✔
4079
        case 2:
4,504,736✔
4080
          val = ((uint8_t *)pColData->pData)[iVal];
4,504,736✔
4081
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,504,736✔
4082
          break;
4,504,736✔
4083
        default:
×
4084
          break;
×
4085
      }
4086
    }
4087
  }
4088
}
4,222✔
4089

4090
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
4,222✔
4091
                                                      int16_t *numOfNull) {
4092
  *(uint64_t *)sum = 0;
4,222✔
4093
  *(uint64_t *)max = 0;
4,222✔
4094
  *(uint64_t *)min = UINT16_MAX;
4,222✔
4095
  *numOfNull = 0;
4,222✔
4096

4097
  uint16_t val;
4098
  if (HAS_VALUE == pColData->flag) {
4,222✔
4099
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,484,574✔
4100
      val = ((uint16_t *)pColData->pData)[iVal];
3,481,820✔
4101
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,481,820✔
4102
    }
4103
  } else {
4104
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,288✔
4105
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,820!
4106
        case 0:
236,621✔
4107
        case 1:
4108
          (*numOfNull)++;
236,621✔
4109
          break;
236,621✔
4110
        case 2:
4,506,199✔
4111
          val = ((uint16_t *)pColData->pData)[iVal];
4,506,199✔
4112
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,506,199✔
4113
          break;
4,506,199✔
4114
        default:
×
4115
          break;
×
4116
      }
4117
    }
4118
  }
4119
}
4,222✔
4120

4121
static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
4,222✔
4122
                                             int16_t *numOfNull) {
4123
  *(uint64_t *)sum = 0;
4,222✔
4124
  *(uint64_t *)max = 0;
4,222✔
4125
  *(uint64_t *)min = UINT32_MAX;
4,222✔
4126
  *numOfNull = 0;
4,222✔
4127

4128
  uint32_t val;
4129
  if (HAS_VALUE == pColData->flag) {
4,222✔
4130
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,484,574✔
4131
      val = ((uint32_t *)pColData->pData)[iVal];
3,481,820✔
4132
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,481,820✔
4133
    }
4134
  } else {
4135
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,288✔
4136
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,820!
4137
        case 0:
237,199✔
4138
        case 1:
4139
          (*numOfNull)++;
237,199✔
4140
          break;
237,199✔
4141
        case 2:
4,505,621✔
4142
          val = ((uint32_t *)pColData->pData)[iVal];
4,505,621✔
4143
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,505,621✔
4144
          break;
4,505,621✔
4145
        default:
×
4146
          break;
×
4147
      }
4148
    }
4149
  }
4150
}
4,222✔
4151

4152
static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
4,222✔
4153
                                                int16_t *numOfNull) {
4154
  *(uint64_t *)sum = 0;
4,222✔
4155
  *(uint64_t *)max = 0;
4,222✔
4156
  *(uint64_t *)min = UINT64_MAX;
4,222✔
4157
  *numOfNull = 0;
4,222✔
4158

4159
  uint64_t val;
4160
  if (HAS_VALUE == pColData->flag) {
4,222✔
4161
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,484,536✔
4162
      val = ((uint64_t *)pColData->pData)[iVal];
3,481,782✔
4163
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,481,782✔
4164
    }
4165
  } else {
4166
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,744,288✔
4167
      switch (tColDataGetBitValue(pColData, iVal)) {
4,742,820!
4168
        case 0:
236,286✔
4169
        case 1:
4170
          (*numOfNull)++;
236,286✔
4171
          break;
236,286✔
4172
        case 2:
4,506,534✔
4173
          val = ((uint64_t *)pColData->pData)[iVal];
4,506,534✔
4174
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,506,534✔
4175
          break;
4,506,534✔
4176
        default:
×
4177
          break;
×
4178
      }
4179
    }
4180
  }
4181
}
4,222✔
4182

4183
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min,
20,634✔
4184
                                                int16_t *numOfNull) {
4185
  *(uint64_t *)sum = 0;
20,634✔
4186
  *(uint64_t *)max = 0;
20,634✔
4187
  *(uint64_t *)min = 0;
20,634✔
4188
  *numOfNull = 0;
20,634✔
4189

4190
  switch (pColData->flag) {
20,634!
4191
    case HAS_NONE:
×
4192
    case HAS_NULL:
4193
    case (HAS_NONE | HAS_NULL):
4194
      *numOfNull = pColData->nVal;
×
4195
      break;
×
4196
    case HAS_VALUE:
17,698✔
4197
      *numOfNull = 0;
17,698✔
4198
      break;
17,698✔
4199
    case (HAS_VALUE | HAS_NULL):
2,936✔
4200
    case (HAS_VALUE | HAS_NONE):
4201
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
9,488,576✔
4202
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
9,485,640✔
4203
          (*numOfNull)++;
474,179✔
4204
        }
4205
      }
4206
      break;
2,936✔
4207
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
4208
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4209
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
4210
          (*numOfNull)++;
×
4211
        }
4212
      }
4213
      break;
×
4214
    default:
×
4215
      break;
×
4216
  }
4217
}
20,634✔
4218

4219
void (*tColDataCalcSMA[])(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min, int16_t *numOfNull) = {
4220
    NULL,
4221
    tColDataCalcSMABool,           // TSDB_DATA_TYPE_BOOL
4222
    tColDataCalcSMATinyInt,        // TSDB_DATA_TYPE_TINYINT
4223
    tColDataCalcSMATinySmallInt,   // TSDB_DATA_TYPE_SMALLINT
4224
    tColDataCalcSMAInt,            // TSDB_DATA_TYPE_INT
4225
    tColDataCalcSMABigInt,         // TSDB_DATA_TYPE_BIGINT
4226
    tColDataCalcSMAFloat,          // TSDB_DATA_TYPE_FLOAT
4227
    tColDataCalcSMADouble,         // TSDB_DATA_TYPE_DOUBLE
4228
    tColDataCalcSMAVarType,        // TSDB_DATA_TYPE_VARCHAR
4229
    tColDataCalcSMABigInt,         // TSDB_DATA_TYPE_TIMESTAMP
4230
    tColDataCalcSMAVarType,        // TSDB_DATA_TYPE_NCHAR
4231
    tColDataCalcSMAUTinyInt,       // TSDB_DATA_TYPE_UTINYINT
4232
    tColDataCalcSMATinyUSmallInt,  // TSDB_DATA_TYPE_USMALLINT
4233
    tColDataCalcSMAUInt,           // TSDB_DATA_TYPE_UINT
4234
    tColDataCalcSMAUBigInt,        // TSDB_DATA_TYPE_UBIGINT
4235
    tColDataCalcSMAVarType,        // TSDB_DATA_TYPE_JSON
4236
    tColDataCalcSMAVarType,        // TSDB_DATA_TYPE_VARBINARY
4237
    tColDataCalcSMAVarType,        // TSDB_DATA_TYPE_DECIMAL
4238
    tColDataCalcSMAVarType,        // TSDB_DATA_TYPE_BLOB
4239
    NULL,                          // TSDB_DATA_TYPE_MEDIUMBLOB
4240
    tColDataCalcSMAVarType         // TSDB_DATA_TYPE_GEOMETRY
4241
};
4242

4243
// SValueColumn ================================
4244
int32_t tValueColumnInit(SValueColumn *valCol) {
18,156,358✔
4245
  valCol->type = TSDB_DATA_TYPE_NULL;
18,156,358✔
4246
  valCol->numOfValues = 0;
18,156,358✔
4247
  tBufferInit(&valCol->data);
18,156,358✔
4248
  tBufferInit(&valCol->offsets);
18,156,358✔
4249
  return 0;
18,156,358✔
4250
}
4251

4252
void tValueColumnDestroy(SValueColumn *valCol) {
42,779,097✔
4253
  valCol->type = TSDB_DATA_TYPE_NULL;
42,779,097✔
4254
  valCol->numOfValues = 0;
42,779,097✔
4255
  tBufferDestroy(&valCol->data);
42,779,097✔
4256
  tBufferDestroy(&valCol->offsets);
42,779,097✔
4257
  return;
42,779,097✔
4258
}
4259

4260
void tValueColumnClear(SValueColumn *valCol) {
20,193,920✔
4261
  valCol->type = TSDB_DATA_TYPE_NULL;
20,193,920✔
4262
  valCol->numOfValues = 0;
20,193,920✔
4263
  tBufferClear(&valCol->data);
20,193,920✔
4264
  tBufferClear(&valCol->offsets);
20,193,920✔
4265
  return;
20,193,920✔
4266
}
4267

4268
int32_t tValueColumnAppend(SValueColumn *valCol, const SValue *value) {
883,597✔
4269
  int32_t code;
4270

4271
  if (valCol->numOfValues == 0) {
883,597✔
4272
    valCol->type = value->type;
4,190✔
4273
  }
4274

4275
  if (!(value->type == valCol->type)) {
883,597!
4276
    return TSDB_CODE_INVALID_PARA;
×
4277
  }
4278

4279
  if (IS_VAR_DATA_TYPE(value->type)) {
883,597!
4280
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
30!
4281
      return code;
×
4282
    }
4283
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
30!
4284
      return code;
×
4285
    }
4286
  } else {
4287
    code = tBufferPut(&valCol->data, &value->val, tDataTypes[value->type].bytes);
883,582✔
4288
    if (code) return code;
883,582!
4289
  }
4290
  valCol->numOfValues++;
883,597✔
4291

4292
  return 0;
883,597✔
4293
}
4294

4295
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
238,530,225✔
4296
  int32_t code;
4297

4298
  if (idx < 0 || idx >= valCol->numOfValues) {
238,530,225!
4299
    return TSDB_CODE_OUT_OF_RANGE;
×
4300
  }
4301

4302
  if (IS_VAR_DATA_TYPE(valCol->type)) {
238,538,067!
4303
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
×
4304
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
×
4305
    int32_t  oldDataSize = nextOffset - offsets[idx];
×
4306
    int32_t  bytesAdded = value->nData - oldDataSize;
×
4307

4308
    if (bytesAdded != 0) {
×
4309
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
24!
4310
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
12✔
4311
              tBufferGetSize(&valCol->data) - nextOffset);
12✔
4312
      valCol->data.size += bytesAdded;
12✔
4313

4314
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
12!
4315
        offsets[i] += bytesAdded;
×
4316
      }
4317
    }
4318
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
×
4319
  } else {
4320
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, &value->val,
238,654,765✔
4321
                        tDataTypes[valCol->type].bytes);
238,556,659✔
4322
  }
4323
  return 0;
4324
}
4325

4326
int32_t tValueColumnGet(SValueColumn *valCol, int32_t idx, SValue *value) {
481,211,698✔
4327
  if (idx < 0 || idx >= valCol->numOfValues) {
481,211,698!
4328
    return TSDB_CODE_OUT_OF_RANGE;
×
4329
  }
4330

4331
  value->type = valCol->type;
481,254,444✔
4332
  if (IS_VAR_DATA_TYPE(value->type)) {
481,254,444!
4333
    int32_t       offset, nextOffset;
4334
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
×
4335

4336
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
×
4337
    if (idx == valCol->numOfValues - 1) {
102✔
4338
      nextOffset = tBufferGetSize(&valCol->data);
99✔
4339
    } else {
4340
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
3!
4341
    }
4342
    value->nData = nextOffset - offset;
102✔
4343
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
102✔
4344
  } else {
4345
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
481,373,735✔
4346
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, &value->val));
962,747,470!
4347
  }
4348
  return 0;
481,373,837✔
4349
}
4350

4351
int32_t tValueColumnCompress(SValueColumn *valCol, SValueColumnCompressInfo *info, SBuffer *output, SBuffer *assist) {
4,188✔
4352
  int32_t code;
4353

4354
  if (!(valCol->numOfValues > 0)) {
4,188!
4355
    return TSDB_CODE_INVALID_PARA;
×
4356
  }
4357

4358
  (*info) = (SValueColumnCompressInfo){
4,188✔
4359
      .cmprAlg = info->cmprAlg,
4,188✔
4360
      .type = valCol->type,
4,188✔
4361
  };
4362

4363
  // offset
4364
  if (IS_VAR_DATA_TYPE(valCol->type)) {
4,188!
4365
    SCompressInfo cinfo = {
13✔
4366
        .cmprAlg = info->cmprAlg,
13✔
4367
        .dataType = TSDB_DATA_TYPE_INT,
4368
        .originalSize = valCol->offsets.size,
13✔
4369
    };
4370

4371
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
13✔
4372
    if (code) return code;
14!
4373

4374
    info->offsetOriginalSize = cinfo.originalSize;
14✔
4375
    info->offsetCompressedSize = cinfo.compressedSize;
14✔
4376
  }
4377

4378
  // data
4379
  SCompressInfo cinfo = {
4,189✔
4380
      .cmprAlg = info->cmprAlg,
4,189✔
4381
      .dataType = valCol->type,
4,189✔
4382
      .originalSize = valCol->data.size,
4,189✔
4383
  };
4384

4385
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
4,189✔
4386
  if (code) return code;
4,190!
4387

4388
  info->dataOriginalSize = cinfo.originalSize;
4,190✔
4389
  info->dataCompressedSize = cinfo.compressedSize;
4,190✔
4390

4391
  return 0;
4,190✔
4392
}
4393

4394
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
40,840✔
4395
                               SBuffer *assist) {
4396
  int32_t code;
4397

4398
  tValueColumnClear(valCol);
40,840✔
4399
  valCol->type = info->type;
40,840✔
4400
  // offset
4401
  if (IS_VAR_DATA_TYPE(valCol->type)) {
40,846!
4402
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
6✔
4403

4404
    SCompressInfo cinfo = {
6✔
4405
        .dataType = TSDB_DATA_TYPE_INT,
4406
        .cmprAlg = info->cmprAlg,
6✔
4407
        .originalSize = info->offsetOriginalSize,
6✔
4408
        .compressedSize = info->offsetCompressedSize,
6✔
4409
    };
4410

4411
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
6✔
4412
    if (code) {
6!
4413
      return code;
×
4414
    }
4415
  } else {
4416
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
40,834✔
4417
  }
4418

4419
  // data
4420
  SCompressInfo cinfo = {
40,840✔
4421
      .dataType = valCol->type,
40,840✔
4422
      .cmprAlg = info->cmprAlg,
40,840✔
4423
      .originalSize = info->dataOriginalSize,
40,840✔
4424
      .compressedSize = info->dataCompressedSize,
40,840✔
4425
  };
4426

4427
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
40,840✔
4428
  if (code) {
40,839!
4429
    return code;
×
4430
  }
4431

4432
  return 0;
40,839✔
4433
}
4434

4435
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *info, SBuffer *buffer) {
4,190✔
4436
  int32_t code;
4437
  uint8_t fmtVer = 0;
4,190✔
4438

4439
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
8,380!
4440
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
8,380!
4441
  if ((code = tBufferPutI8(buffer, info->type))) return code;
8,380!
4442
  if (IS_VAR_DATA_TYPE(info->type)) {
4,190!
4443
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
28!
4444
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
28!
4445
  }
4446
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
8,380!
4447
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
8,380!
4448

4449
  return 0;
4,190✔
4450
}
4451

4452
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
40,840✔
4453
  int32_t code;
4454
  uint8_t fmtVer;
4455

4456
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
40,840!
4457
  if (fmtVer == 0) {
40,840!
4458
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
40,840!
4459
    if ((code = tBufferGetI8(reader, &info->type))) return code;
40,840!
4460
    if (IS_VAR_DATA_TYPE(info->type)) {
40,840!
4461
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
6!
4462
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
6!
4463
    } else {
4464
      info->offsetOriginalSize = 0;
40,834✔
4465
      info->offsetCompressedSize = 0;
40,834✔
4466
    }
4467
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
40,840!
4468
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
40,839!
4469
  } else {
4470
    return TSDB_CODE_INVALID_PARA;
×
4471
  }
4472

4473
  return 0;
40,840✔
4474
}
4475

4476
int32_t tCompressData(void          *input,       // input
5,395,416✔
4477
                      SCompressInfo *info,        // compress info
4478
                      void          *output,      // output
4479
                      int32_t        outputSize,  // output size
4480
                      SBuffer       *buffer       // assistant buffer provided by caller, can be NULL
4481
) {
4482
  int32_t extraSizeNeeded;
4483
  int32_t code;
4484

4485
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
5,395,416!
4486
  if (!(outputSize >= extraSizeNeeded)) {
5,395,416!
4487
    return TSDB_CODE_INVALID_PARA;
×
4488
  }
4489

4490
  if (info->cmprAlg == NO_COMPRESSION) {
5,395,416!
4491
    (void)memcpy(output, input, info->originalSize);
×
4492
    info->compressedSize = info->originalSize;
×
4493
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
6,593,978✔
4494
    SBuffer local;
4495

4496
    tBufferInit(&local);
4497
    if (buffer == NULL) {
1,196,513!
4498
      buffer = &local;
×
4499
    }
4500

4501
    if (info->cmprAlg == TWO_STAGE_COMP) {
1,196,513!
4502
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
1,198,423✔
4503
      if (code) {
1,198,423!
4504
        tBufferDestroy(&local);
4505
        return code;
×
4506
      }
4507
    }
4508

4509
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
2,395,075✔
4510
        input,                                                   // input
4511
        info->originalSize,                                      // input size
4512
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
1,196,513✔
4513
        output,                                                  // output
4514
        outputSize,                                              // output size
4515
        info->cmprAlg,                                           // compression algorithm
1,196,513✔
4516
        buffer->data,                                            // buffer
4517
        buffer->capacity                                         // buffer size
1,196,513✔
4518
    );
4519
    if (info->compressedSize < 0) {
1,198,562!
4520
      tBufferDestroy(&local);
4521
      return TSDB_CODE_COMPRESS_ERROR;
×
4522
    }
4523

4524
    tBufferDestroy(&local);
4525
  } else {
4526
    DEFINE_VAR(info->cmprAlg)
4,198,903✔
4527
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
4,198,903!
4528
      (void)memcpy(output, input, info->originalSize);
×
4529
      info->compressedSize = info->originalSize;
×
4530
      return 0;
×
4531
    }
4532
    SBuffer local;
4533

4534
    tBufferInit(&local);
4535
    if (buffer == NULL) {
4,198,903!
4536
      buffer = &local;
×
4537
    }
4538
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
4,198,903✔
4539

4540
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
8,398,874✔
4541
        input,                                                      // input
4542
        info->originalSize,                                         // input size
4543
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
4,198,929✔
4544
        output,                                                     // output
4545
        outputSize,                                                 // output size
4546
        info->cmprAlg,                                              // compression algorithm
4547
        buffer->data,                                               // buffer
4548
        buffer->capacity                                            // buffer size
4,198,929✔
4549
    );
4550
    if (info->compressedSize < 0) {
4,199,945!
4551
      tBufferDestroy(&local);
4552
      return TSDB_CODE_COMPRESS_ERROR;
×
4553
    }
4554

4555
    tBufferDestroy(&local);
4556
    // new col compress
4557
  }
4558

4559
  return 0;
5,398,507✔
4560
}
4561

4562
int32_t tDecompressData(void                *input,       // input
64,264,685✔
4563
                        const SCompressInfo *info,        // compress info
4564
                        void                *output,      // output
4565
                        int32_t              outputSize,  // output size
4566
                        SBuffer             *buffer       // assistant buffer provided by caller, can be NULL
4567
) {
4568
  int32_t code;
4569

4570
  if (!(outputSize >= info->originalSize)) {
64,264,685!
4571
    return TSDB_CODE_INVALID_PARA;
×
4572
  }
4573

4574
  if (info->cmprAlg == NO_COMPRESSION) {
64,264,685!
4575
    if (!(info->compressedSize == info->originalSize)) {
×
4576
      return TSDB_CODE_INVALID_PARA;
×
4577
    }
4578
    (void)memcpy(output, input, info->compressedSize);
×
4579
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
90,026,085!
4580
    SBuffer local;
4581

4582
    tBufferInit(&local);
4583
    if (buffer == NULL) {
25,736,639!
4584
      buffer = &local;
×
4585
    }
4586

4587
    if (info->cmprAlg == TWO_STAGE_COMP) {
25,736,639!
4588
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
25,763,381✔
4589
      if (code) {
25,764,850!
4590
        tBufferDestroy(&local);
4591
        return code;
×
4592
      }
4593
    }
4594

4595
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
25,738,108✔
4596
        input,                                                  // input
4597
        info->compressedSize,                                   // inputSize
25,738,108✔
4598
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
25,738,108✔
4599
        output,                                                 // output
4600
        outputSize,                                             // output size
4601
        info->cmprAlg,                                          // compression algorithm
25,738,108✔
4602
        buffer->data,                                           // helper buffer
4603
        buffer->capacity                                        // extra buffer size
25,738,108✔
4604
    );
4605
    if (decompressedSize < 0) {
25,761,400!
4606
      tBufferDestroy(&local);
4607
      return TSDB_CODE_COMPRESS_ERROR;
×
4608
    }
4609

4610
    if (!(decompressedSize == info->originalSize)) {
25,761,400!
4611
      return TSDB_CODE_COMPRESS_ERROR;
×
4612
    }
4613
    tBufferDestroy(&local);
4614
  } else {
4615
    DEFINE_VAR(info->cmprAlg);
38,528,046✔
4616
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
38,528,046!
4617
      (void)memcpy(output, input, info->compressedSize);
×
4618
      return 0;
×
4619
    }
4620
    SBuffer local;
4621

4622
    tBufferInit(&local);
4623
    if (buffer == NULL) {
38,528,046!
4624
      buffer = &local;
×
4625
    }
4626
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
38,528,046✔
4627
    if (code) {
38,528,599!
4628
      return code;
×
4629
    }
4630

4631
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
38,528,599✔
4632
        input,                                                  // input
4633
        info->compressedSize,                                   // inputSize
38,528,599✔
4634
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
38,528,599✔
4635
        output,                                                 // output
4636
        outputSize,                                             // output size
4637
        info->cmprAlg,                                          // compression algorithm
38,528,599✔
4638
        buffer->data,                                           // helper buffer
4639
        buffer->capacity                                        // extra buffer size
38,528,599✔
4640
    );
4641
    if (decompressedSize < 0) {
38,531,808!
4642
      tBufferDestroy(&local);
4643
      return TSDB_CODE_COMPRESS_ERROR;
×
4644
    }
4645

4646
    if (!(decompressedSize == info->originalSize)) {
38,531,808!
4647
      return TSDB_CODE_COMPRESS_ERROR;
×
4648
    }
4649
    tBufferDestroy(&local);
4650
  }
4651

4652
  return 0;
64,293,208✔
4653
}
4654

4655
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
5,396,011✔
4656
  int32_t code;
4657

4658
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
5,396,011✔
4659
  if (code) return code;
5,396,021!
4660

4661
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
5,396,021✔
4662
  if (code) return code;
5,396,776!
4663

4664
  output->size += info->compressedSize;
5,396,776✔
4665
  return 0;
5,396,776✔
4666
}
4667

4668
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
25,760,250✔
4669
  int32_t code;
4670

4671
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
25,760,250✔
4672
  if (code) return code;
25,759,320!
4673

4674
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
25,759,320✔
4675
  if (code) return code;
25,761,385!
4676

4677
  output->size += info->originalSize;
25,761,385✔
4678
  return 0;
25,761,385✔
4679
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc