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

taosdata / TDengine / #3598

07 Feb 2025 05:42AM UTC coverage: 63.396% (-0.2%) from 63.546%
#3598

push

travis-ci

web-flow
Merge pull request #29683 from taosdata/enh/TS-5614/regexp

enh: regexp

140887 of 285630 branches covered (49.33%)

Branch coverage included in aggregate %.

219506 of 282846 relevant lines covered (77.61%)

19304593.48 hits per line

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

61.25
/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) {
609,471,661✔
65
  int32_t n = 0;
609,471,661✔
66
  n += tPutI8(p ? p + n : p, index->type);
609,471,661✔
67
  n += tPutU32v(p ? p + n : p, index->offset);
609,471,661✔
68
  return n;
609,471,661✔
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++;
24,315,512✔
113
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
24,315,512✔
114
  sinfo->kvPayloadSize += tPutI16v(NULL, -pTColumn->colId);
24,315,512✔
115
  return 0;
24,315,512✔
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,466,438✔
123
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
206,466,438✔
124
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
206,466,438!
125
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
206,466,438✔
126
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
206,466,438✔
127
    sinfo->numOfPKs++;
206,466,438✔
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
661,569,168✔
133
                           + colVal->value.nData;               // value
661,569,168✔
134

135
    sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
661,569,168✔
136
                            + tPutU32v(NULL, colVal->value.nData)  // size
661,569,168✔
137
                            + colVal->value.nData;                 // value
661,569,168✔
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,266,935,841✔
146
  int32_t  code = 0;
1,266,935,841✔
147
  int32_t  colValIndex = 1;
1,266,935,841✔
148
  int32_t  numOfColVals = TARRAY_SIZE(colVals);
1,266,935,841✔
149
  SColVal *colValArray = (SColVal *)TARRAY_DATA(colVals);
1,266,935,841✔
150

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

161
  *sinfo = (SRowBuildScanInfo){
1,266,935,841✔
162
      .tupleFixedSize = schema->flen,
1,266,935,841✔
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;
48,631,024!
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) {
3,440,158✔
190
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
62!
191
        break;
31✔
192
      } else {  // skip useless value
193
        colValIndex++;
3,440,127✔
194
      }
195
    }
196
  }
197

198
  if (sinfo->numOfNone) {
1,266,935,841✔
199
    sinfo->flag |= HAS_NONE;
438,428,541✔
200
  }
201
  if (sinfo->numOfNull) {
1,266,935,841✔
202
    sinfo->flag |= HAS_NULL;
16,684,511✔
203
  }
204
  if (sinfo->numOfValue) {
1,266,935,841✔
205
    sinfo->flag |= HAS_VALUE;
1,213,301,552✔
206
  }
207

208
  // Tuple
209
  sinfo->tupleFlag = sinfo->flag;
1,266,935,841✔
210
  switch (sinfo->flag) {
1,266,935,841!
211
    case HAS_NONE:
61,290,837✔
212
    case HAS_NULL:
213
      sinfo->tupleBitmapSize = 0;
61,290,837✔
214
      sinfo->tupleFixedSize = 0;
61,290,837✔
215
      break;
61,290,837✔
216
    case HAS_VALUE:
821,670,480✔
217
      sinfo->tupleBitmapSize = 0;
821,670,480✔
218
      sinfo->tupleFixedSize = schema->flen;
821,670,480✔
219
      break;
821,670,480✔
220
    case (HAS_NONE | HAS_NULL):
24,098✔
221
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
24,098✔
222
      sinfo->tupleFixedSize = 0;
24,098✔
223
      break;
24,098✔
224
    case (HAS_NONE | HAS_VALUE):
392,984,740✔
225
    case (HAS_NULL | HAS_VALUE):
226
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
392,984,740✔
227
      sinfo->tupleFixedSize = schema->flen;
392,984,740✔
228
      break;
392,984,740✔
229
    case (HAS_NONE | HAS_NULL | HAS_VALUE):
453,748✔
230
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
453,748✔
231
      sinfo->tupleFixedSize = schema->flen;
453,748✔
232
      break;
453,748✔
233
  }
234
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
1,473,759,346✔
235
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
207,500,347✔
236
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
207,500,347✔
237
  }
238
  sinfo->tupleRowSize = sizeof(SRow)              // SRow
1,266,258,999✔
239
                        + sinfo->tuplePKSize      // primary keys
1,266,258,999✔
240
                        + sinfo->tupleBitmapSize  // bitmap
1,266,258,999✔
241
                        + sinfo->tupleFixedSize   // fixed part
1,266,258,999✔
242
                        + sinfo->tupleVarSize;    // var part
1,266,258,999✔
243

244
  // Key-Value
245
  if (sinfo->kvMaxOffset <= UINT8_MAX) {
1,266,258,999!
246
    sinfo->kvFlag = (KV_FLG_LIT | sinfo->flag);
1,268,734,432✔
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint8_t);
1,268,734,432✔
248
  } else if (sinfo->kvMaxOffset <= UINT16_MAX) {
×
249
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
6,967,647✔
250
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
6,967,647✔
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,472,617,902✔
256
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
206,614,172✔
257
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
206,614,172✔
258
  }
259
  sinfo->kvRowSize = sizeof(SRow)             // SRow
1,266,003,730✔
260
                     + sinfo->kvPKSize        // primary keys
1,266,003,730✔
261
                     + sinfo->kvIndexSize     // index array
1,266,003,730✔
262
                     + sinfo->kvPayloadSize;  // payload
1,266,003,730✔
263

264
_exit:
1,266,003,730✔
265
  return code;
1,266,003,730✔
266
}
267

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

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

282
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
898,084,680✔
283
    return 0;
63,251,303✔
284
  }
285

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

291
  // primary keys
292
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
1,039,779,653✔
293
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
207,729,504✔
294
  }
295

296
  // bitmap + fixed + varlen
297
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
832,050,149✔
298
  int32_t colValIndex = 1;
832,050,149✔
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;
164,411,071✔
312
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
164,411,071✔
313
            if (colValArray[colValIndex].value.nData) {
164,411,071!
314
              (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
167,518,063✔
315
              varlen += colValArray[colValIndex].value.nData;
167,518,063✔
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
10,193,013!
322
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
10,907,883!
323
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
324
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
461,980!
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
×
330
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
9!
331
        break;
9✔
332
      } else {
333
        colValIndex++;
×
334
      }
335
    }
336
  }
337

338
  return 0;
832,050,149✔
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) {
6,819,715!
345
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
7,550,989✔
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) {
383,700,231✔
353
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
383,700,231✔
354

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

365
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
383,994,022!
366
    return TSDB_CODE_INVALID_PARA;
×
367
  }
368

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

374
  // primary keys
375
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
386,346,209✔
376
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
2,332,170✔
377
  }
378

379
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
384,014,039✔
380
  int32_t colValIndex = 1;
384,014,039✔
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);
496,055,216✔
392
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
496,055,216✔
393
            if (colValArray[colValIndex].value.nData > 0) {
496,055,216!
394
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
508,827,475✔
395
                           colValArray[colValIndex].value.nData);
508,827,475✔
396
            }
397
            payloadSize += colValArray[colValIndex].value.nData;
496,055,216✔
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,061,394✔
406
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
26,122,788✔
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;
384,014,039✔
420
}
421

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

426
  code = tRowBuildScan(aColVal, pTSchema, &sinfo);
1,266,944,569✔
427
  if (code) return code;
1,275,259,342!
428

429
  if (sinfo.tupleRowSize <= sinfo.kvRowSize) {
1,275,259,342✔
430
    code = tRowBuildTupleRow(aColVal, &sinfo, pTSchema, ppRow);
891,772,989✔
431
  } else {
432
    code = tRowBuildKVRow(aColVal, &sinfo, pTSchema, ppRow);
383,486,353✔
433
  }
434
  return code;
1,275,598,820✔
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,
40,969✔
456
                          SArray *rowArray, bool *pOrdered, bool *pDupTs) {
457
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
40,969!
458
    return TSDB_CODE_INVALID_PARA;
×
459
  }
460

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

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

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

474
  SRowKey rowKey, lastRowKey;
475
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
132,460✔
476
    taosArrayClear(colValArray);
92,160✔
477

478
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
739,774✔
479
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
662,424✔
480
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
682✔
481
      } else {
482
        SValue value = {
661,742✔
483
            .type = infos[iInfo].type,
661,742✔
484
        };
485
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
661,742!
486
          value.nData = infos[iInfo].bind->length[iRow];
177,517✔
487
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
177,517!
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;
177,517✔
492
        } else {
493
          (void)memcpy(&value.val, (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
484,225✔
494
                       infos[iInfo].bind->buffer_length);
484,225✔
495
        }
496
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
661,742✔
497
      }
498
      if (taosArrayPush(colValArray, &colVal) == NULL) {
647,672!
499
        code = terrno;
×
500
        goto _exit;
×
501
      }
502
    }
503

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

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

514
    if (pOrdered && pDupTs) {
91,411!
515
      tRowGetKey(row, &rowKey);
91,444!
516
      if (iRow == 0) {
91,478✔
517
        *pOrdered = true;
40,863✔
518
        *pDupTs = false;
40,863✔
519
      } else {
520
        // no more compare if we already get disordered or duplicate rows
521
        if (*pOrdered && !*pDupTs) {
50,615!
522
          int32_t code = tRowKeyCompare(&rowKey, &lastRowKey);
50,620✔
523
          *pOrdered = (code >= 0);
50,620✔
524
          *pDupTs = (code == 0);
50,620✔
525
        }
526
      }
527
      lastRowKey = rowKey;
91,478✔
528
    }
529
  }
530

531
_exit:
40,300✔
532
  taosArrayDestroy(colValArray);
40,300✔
533
  return code;
41,033✔
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;
42,575,667✔
544
    pColVal->value.type = pTColumn->type;
42,575,667✔
545
    pColVal->flag = CV_FLAG_VALUE;
42,575,667✔
546
    (void)memcpy(&pColVal->value.val, &pRow->ts, sizeof(TSKEY));
42,575,667✔
547
    return 0;
42,575,667✔
548
  }
549

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

555
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
556
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
941,853✔
557
    return 0;
941,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);
34,004,703✔
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) {
55,141,177!
573
      pv = pIdx->idx + (pIdx->nCol << 1);
82,916,672✔
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) {
349,427,401!
586
        pData = pv + ((uint16_t *)pIdx->idx)[mid];
349,676,288✔
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);
87,800,666✔
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);
736,770,920!
604
            if (pColVal->value.nData > 0) {
736,770,920!
605
              pColVal->value.pData = pData;
884,339,752✔
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,147,483,647✔
622
  } else {  // Tuple Row
623
    uint8_t *bitmap = data;
616,883,269✔
624
    uint8_t *fixed;
625
    uint8_t *varlen;
626
    uint8_t  bit;
627

628
    if (pRow->flag == HAS_VALUE) {
616,883,269✔
629
      fixed = bitmap;
598,237,407✔
630
      bit = BIT_FLG_VALUE;
598,237,407✔
631
    } else if (pRow->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
18,645,862!
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;
18,645,862✔
636
      bit = GET_BIT1(bitmap, iCol - 1);
18,645,862✔
637

638
      if (pRow->flag == (HAS_NONE | HAS_VALUE)) {
18,645,862✔
639
        if (bit) bit++;
20,375✔
640
      } else if (pRow->flag == (HAS_NULL | HAS_VALUE)) {
18,625,487✔
641
        bit++;
2,318,150✔
642
      }
643
    }
644
    varlen = fixed + pTSchema->flen;
616,883,269✔
645

646
    if (bit == BIT_FLG_NONE) {
616,883,269✔
647
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
7,239✔
648
      return 0;
7,239✔
649
    } else if (bit == BIT_FLG_NULL) {
616,876,030✔
650
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
302,357✔
651
      return 0;
302,357✔
652
    }
653

654
    pColVal->cid = pTColumn->colId;
616,573,673✔
655
    pColVal->value.type = pTColumn->type;
616,573,673✔
656
    pColVal->flag = CV_FLAG_VALUE;
616,573,673✔
657
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
616,573,673!
658
      pColVal->value.pData = varlen + *(int32_t *)(fixed + pTColumn->offset);
56,092,488✔
659
      pColVal->value.pData += tGetU32v(pColVal->value.pData, &pColVal->value.nData);
112,184,976✔
660
    } else {
661
      (void)memcpy(&pColVal->value.val, fixed + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
560,481,185✔
662
    }
663
  }
664

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

668
void tRowDestroy(SRow *pRow) {
840,305,568✔
669
  if (pRow) taosMemoryFree(pRow);
840,305,568!
670
}
840,452,380✔
671

672
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
281,365,879✔
673
  SRowKey key1, key2;
674
  tRowGetKey(*(SRow **)p1, &key1);
281,365,879✔
675
  tRowGetKey(*(SRow **)p2, &key2);
266,527,942✔
676
  return tRowKeyCompare(&key1, &key2);
279,346,918✔
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) {
35,044✔
757
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
35,044✔
758
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
35,041✔
759
  if (code != TSDB_CODE_SUCCESS) {
35,041!
760
    uError("taosArrayMSort failed caused by %d", code);
×
761
  }
762
  return code;
35,041✔
763
}
764

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

768
  int32_t iStart = 0;
35,094✔
769
  while (iStart < aRowP->size) {
40,369,891!
770
    SRowKey key1;
771
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
40,419,218✔
772

773
    tRowGetKey(row1, &key1);
40,791,062✔
774

775
    int32_t iEnd = iStart + 1;
40,349,391✔
776
    while (iEnd < aRowP->size) {
40,319,304!
777
      SRowKey key2;
778
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
40,324,867✔
779
      tRowGetKey(row2, &key2);
40,632,210✔
780

781
      if (tRowKeyCompare(&key1, &key2) != 0) break;
40,310,273!
782

783
      iEnd++;
×
784
    }
785

786
    if (iEnd - iStart > 1) {
40,334,797✔
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++;
40,334,797✔
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,137,012✔
819
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
1,137,012!
820

821
  int32_t code = 0;
1,137,012✔
822

823
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
1,137,012!
824
  if (pIter == NULL) {
1,179,918!
825
    code = terrno;
×
826
    goto _exit;
×
827
  }
828

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

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

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

841
  if (pRow->flag >> 4) {
1,188,857✔
842
    pIter->iCol = 0;
1,181,502✔
843
    pIter->pIdx = (SKVIdx *)data;
1,181,502✔
844
    if (pRow->flag & KV_FLG_LIT) {
1,181,502✔
845
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
1,179,667✔
846
    } else if (pRow->flag & KV_FLG_MID) {
1,835!
847
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
2,400✔
848
    } else {
849
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
850
    }
851
  } else {
852
    switch (pRow->flag) {
7,355!
853
      case (HAS_NULL | HAS_NONE):
1✔
854
        pIter->pb = data;
1✔
855
        break;
1✔
856
      case HAS_VALUE:
2,367✔
857
        pIter->pf = data;
2,367✔
858
        pIter->pv = pIter->pf + pTSchema->flen;
2,367✔
859
        break;
2,367✔
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:
4,268✔
872
        break;
4,268✔
873
    }
874
  }
875

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

885
void tRowIterClose(SRowIter **ppIter) {
1,115,385✔
886
  SRowIter *pIter = *ppIter;
1,115,385✔
887
  if (pIter) {
1,115,385!
888
    taosMemoryFree(pIter);
1,115,606!
889
  }
890
  *ppIter = NULL;
1,191,021✔
891
}
1,191,021✔
892

893
SColVal *tRowIterNext(SRowIter *pIter) {
13,208,651✔
894
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
13,208,651✔
895
    return NULL;
1,115,980✔
896
  }
897

898
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
12,092,671✔
899

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

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

914
  if (pIter->pRow->flag == HAS_NULL) {
10,935,850✔
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
10,934,009!
920
    if (pIter->iCol < pIter->pIdx->nCol) {
11,616,672✔
921
      uint8_t *pData;
922

923
      if (pIter->pRow->flag & KV_FLG_LIT) {
9,720,482✔
924
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
9,664,097✔
925
      } else if (pIter->pRow->flag & KV_FLG_MID) {
56,385!
926
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
72,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);
9,720,482✔
933

934
      if (TABS(cid) == pTColumn->colId) {
9,720,482!
935
        if (cid < 0) {
9,901,268✔
936
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,764,930✔
937
        } else {
938
          pIter->cv.cid = pTColumn->colId;
8,136,338✔
939
          pIter->cv.value.type = pTColumn->type;
8,136,338✔
940
          pIter->cv.flag = CV_FLAG_VALUE;
8,136,338✔
941

942
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
8,136,338!
943
            pData += tGetU32v(pData, &pIter->cv.value.nData);
882,680!
944
            if (pIter->cv.value.nData > 0) {
882,680!
945
              pIter->cv.value.pData = pData;
2,259,517✔
946
            } else {
947
              pIter->cv.value.pData = NULL;
×
948
            }
949
          } else {
950
            (void)memcpy(&pIter->cv.value.val, pData, pTColumn->bytes);
7,253,658✔
951
          }
952
        }
953

954
        pIter->iCol++;
9,901,268✔
955
        goto _exit;
9,901,268✔
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,896,190✔
965
      goto _exit;
1,896,190✔
966
    }
967
  } else {  // Tuple
968
    uint8_t bv = BIT_FLG_VALUE;
×
969
    if (pIter->pb) {
×
970
      switch (pIter->pRow->flag) {
3,001!
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,961✔
979
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
2,961✔
980
          break;
2,961✔
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,001✔
989
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
13✔
990
        goto _exit;
13✔
991
      } else if (bv == BIT_FLG_NULL) {
2,988✔
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,973✔
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,009✔
1010
    }
1011
    goto _exit;
×
1012
  }
1013

1014
_exit:
12,273,551✔
1015
  pIter->iTColumn++;
12,273,551✔
1016
  return &pIter->cv;
12,273,551✔
1017
}
1018

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

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

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

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

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

1039
  while (pColData) {
598,169✔
1040
    if (pTColumn) {
433,399!
1041
      if (pTColumn->colId == pColData->cid) {  // NULL
433,399!
1042
        if (flag == 0) {
433,413✔
1043
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
433,371✔
1044
        } else {
1045
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
42✔
1046
        }
1047
        if (code) goto _exit;
433,404!
1048

1049
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
433,404✔
1050
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
433,404✔
1051
      } else if (pTColumn->colId > pColData->cid) {  // NONE
×
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;
×
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:
164,770✔
1064
  return code;
164,770✔
1065
}
1066
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
807,108,782✔
1067
                                      int32_t flag) {
1068
  int32_t code = 0;
807,108,782✔
1069

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

1075
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
807,108,782✔
1076
  SPrimaryKeyIndex index;
1077
  uint8_t         *data = pRow->data;
807,108,782✔
1078
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,035,398,543✔
1079
    data += tGetPrimaryKeyIndex(data, &index);
228,360,110✔
1080
  }
1081

1082
  switch (pRow->flag) {
807,038,433!
1083
    case HAS_VALUE:
799,660,178✔
1084
      pf = data;  // TODO: fix here
799,660,178✔
1085
      pv = pf + pTSchema->flen;
799,660,178✔
1086
      break;
799,660,178✔
1087
    case (HAS_NULL | HAS_NONE):
1,296✔
1088
      pb = data;
1,296✔
1089
      break;
1,296✔
1090
    case (HAS_VALUE | HAS_NONE):
7,697,273✔
1091
    case (HAS_VALUE | HAS_NULL):
1092
      pb = data;
7,697,273✔
1093
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
7,697,273✔
1094
      pv = pf + pTSchema->flen;
7,697,273✔
1095
      break;
7,697,273✔
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,147,483,647✔
1107
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
1108
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647!
1109
          return TSDB_CODE_INVALID_PARA;
×
1110
        }
1111
        if (pb) {
2,147,483,647✔
1112
          uint8_t bv;
1113
          switch (pRow->flag) {
22,821,689!
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,142✔
1118
              bv = GET_BIT1(pb, iTColumn - 1);
199,142✔
1119
              if (bv) bv++;
199,142✔
1120
              break;
199,142✔
1121
            case (HAS_VALUE | HAS_NULL):
22,597,553✔
1122
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
22,597,553✔
1123
              break;
22,597,553✔
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) {
22,822,615✔
1132
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
43,343!
1133
              goto _exit;
×
1134
            goto _continue;
43,344✔
1135
          } else if (bv == BIT_FLG_NULL) {
22,779,272✔
1136
            if (flag == 0) {
7,682,528✔
1137
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
7,680,394✔
1138
            } else {
1139
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
2,134✔
1140
            }
1141
            if (code) goto _exit;
7,682,815!
1142
            goto _continue;
7,682,815✔
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);
46,234,325!
1148
          uint32_t nData;
1149
          pData += tGetU32v(pData, &nData);
46,234,325✔
1150
          if (flag == 0) {
46,234,325!
1151
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
49,542,610✔
1152
          } else {
1153
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
1154
          }
1155
          if (code) goto _exit;
50,197,840!
1156
        } else {
1157
          if (flag == 0) {
2,147,483,647✔
1158
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
1159
                                                                          TYPE_BYTES[pColData->type]);
2,147,483,647✔
1160
          } else {
1161
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
1,261,775✔
1162
                                                                          TYPE_BYTES[pColData->type], flag > 0);
1,261,775✔
1163
          }
1164
          if (code) goto _exit;
2,147,483,647!
1165
        }
1166

1167
      _continue:
2,147,483,647✔
1168
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
1169
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
1170
      } else if (pTColumn->colId > pColData->cid) {  // NONE
403,652!
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;
403,652!
1175
      }
1176
    } else {
1177
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
420,050!
1178
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
420,034✔
1179
    }
1180
  }
1181

1182
_exit:
810,605,883✔
1183
  return code;
810,605,883✔
1184
}
1185
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
4,493,659✔
1186
  int32_t code = 0;
4,493,659✔
1187

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

1195
  // primary keys
1196
  uint8_t         *data = pRow->data;
4,493,659✔
1197
  SPrimaryKeyIndex index;
1198
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
4,652,422✔
1199
    data += tGetPrimaryKeyIndex(data, &index);
158,764✔
1200
  }
1201

1202
  SKVIdx *pKVIdx = (SKVIdx *)data;
4,493,658✔
1203
  if (pRow->flag & KV_FLG_LIT) {
4,493,658✔
1204
    pv = pKVIdx->idx + pKVIdx->nCol;
4,435,266✔
1205
  } else if (pRow->flag & KV_FLG_MID) {
58,392!
1206
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
61,924✔
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,216,781✔
1214
    if (pTColumn) {
37,835,466✔
1215
      if (pTColumn->colId == pColData->cid) {
37,436,264✔
1216
        while (iCol < pKVIdx->nCol) {
37,363,924✔
1217
          uint8_t *pData;
1218
          if (pRow->flag & KV_FLG_LIT) {
32,265,238✔
1219
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
30,593,012✔
1220
          } else if (pRow->flag & KV_FLG_MID) {
1,672,226!
1221
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
1,672,259✔
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,265,271✔
1230

1231
          if (TABS(cid) == pTColumn->colId) {
32,265,271✔
1232
            if (cid < 0) {
31,878,835✔
1233
              if (flag == 0) {
6,753,178✔
1234
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
6,752,225✔
1235
              } else {
1236
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
953✔
1237
              }
1238
              if (code) goto _exit;
6,341,457!
1239
            } else {
1240
              uint32_t nData;
1241
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
25,125,657!
1242
                pData += tGetU32v(pData, &nData);
6,065,626✔
1243
              } else {
1244
                nData = 0;
19,060,031✔
1245
              }
1246
              if (flag == 0) {
25,125,657!
1247
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
25,298,701✔
1248
              } else {
1249
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
1250
              }
1251
              if (code) goto _exit;
25,422,003!
1252
            }
1253
            iCol++;
31,763,460✔
1254
            goto _continue;
31,763,460✔
1255
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
386,436✔
1256
            break;
39,305✔
1257
          } else {
1258
            iCol++;
347,131✔
1259
          }
1260
        }
1261

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

1264
      _continue:
5,137,470✔
1265
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
36,900,930✔
1266
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
36,900,930✔
1267
      } else if (pTColumn->colId > pColData->cid) {
419,471!
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;
419,471!
1272
      }
1273
    } else {
1274
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
399,202!
1275
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
399,190✔
1276
    }
1277
  }
1278

1279
_exit:
4,381,315✔
1280
  return code;
4,381,315✔
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) {
811,662,514✔
1287
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
811,662,514!
1288
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
811,662,514!
1289

1290
  if (pRow->flag == HAS_NONE) {
811,662,514✔
1291
    return tRowNoneUpsertColData(aColData, nColData, flag);
1,125✔
1292
  } else if (pRow->flag == HAS_NULL) {
811,661,389✔
1293
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
164,778✔
1294
  } else if (pRow->flag >> 4) {  // KV row
811,496,611✔
1295
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
4,496,050✔
1296
  } else {  // TUPLE row
1297
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
807,000,561✔
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,112,527✔
1323
    }
1324

1325
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
2,147,483,647!
1326
      key->pks[i].pData = tdata;
890✔
1327
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
1,780!
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) {
44,353,292✔
1346
  switch (tv1->type) {
44,353,292!
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:
989,239✔
1353
      T_COMPARE_SCALAR_VALUE(int32_t, &tv1->val, &tv2->val);
989,239!
1354
    case TSDB_DATA_TYPE_BIGINT:
43,359,256✔
1355
    case TSDB_DATA_TYPE_TIMESTAMP:
1356
      T_COMPARE_SCALAR_VALUE(int64_t, &tv1->val, &tv2->val);
43,359,256✔
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:
270✔
1370
    case TSDB_DATA_TYPE_BINARY: {
1371
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
270✔
1372
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
270✔
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:
4,527✔
1384
      break;
4,527✔
1385
  }
1386

1387
  return 0;
4,527✔
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,757,785!
1401
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
73,798,233!
1402
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
44,345,577✔
1403
      if (ret) return ret;
44,330,310!
1404
    }
1405
  } else if (key1->numOfPKs < key2->numOfPKs) {
×
1406
    return -1;
×
1407
  } else {
1408
    return 1;
×
1409
  }
1410

1411
  return 0;
29,452,656✔
1412
}
1413

1414
void tRowKeyAssign(SRowKey *pDst, SRowKey *pSrc) {
825,231,070✔
1415
  pDst->ts = pSrc->ts;
825,231,070✔
1416
  pDst->numOfPKs = pSrc->numOfPKs;
825,231,070✔
1417

1418
  if (pSrc->numOfPKs > 0) {
825,231,070✔
1419
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
54,204,048✔
1420
      SValue *pVal = &pDst->pks[i];
27,175,474✔
1421
      pVal->type = pSrc->pks[i].type;
27,175,474✔
1422

1423
      if (IS_NUMERIC_TYPE(pVal->type)) {
27,175,474!
1424
        pVal->val = pSrc->pks[i].val;
27,175,464✔
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
}
825,231,070✔
1432

1433
// STag ========================================
1434
static int tTagValCmprFn(const void *p1, const void *p2) {
134,678,693✔
1435
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
134,678,693✔
1436
    return -1;
45,972,929✔
1437
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
88,705,764✔
1438
    return 1;
49,044,977✔
1439
  }
1440

1441
  return 0;
39,660,787✔
1442
}
1443
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
11,549✔
1444
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
11,549✔
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,430,251✔
1541
  int32_t n = 0;
1,430,251✔
1542

1543
  // key
1544
  if (isJson) {
1,430,251✔
1545
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
1,096✔
1546
  } else {
1547
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
2,859,406✔
1548
  }
1549

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

1553
  // value
1554
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
1,430,251✔
1555
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
703,566✔
1556
  } else {
1557
    p = p ? p + n : p;
1,078,468✔
1558
    n += tDataTypes[pTagVal->type].bytes;
1,078,468✔
1559
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
1,078,468✔
1560
  }
1561

1562
  return n;
1,430,251✔
1563
}
1564
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
133,107,098✔
1565
  int32_t n = 0;
133,107,098✔
1566

1567
  // key
1568
  if (isJson) {
133,107,098✔
1569
    n += tGetCStr(p + n, &pTagVal->pKey);
26,604!
1570
  } else {
1571
    n += tGetI16v(p + n, &pTagVal->cid);
266,187,592!
1572
  }
1573

1574
  // type
1575
  n += tGetI8(p + n, &pTagVal->type);
133,107,098!
1576

1577
  // value
1578
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
133,107,098!
1579
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
44,355,482!
1580
  } else {
1581
    (void)memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
110,929,357✔
1582
    n += tDataTypes[pTagVal->type].bytes;
110,929,357✔
1583
  }
1584

1585
  return n;
133,107,098✔
1586
}
1587

1588
bool tTagIsJson(const void *pTag) { return (((const STag *)pTag)->flags & TD_TAG_JSON); }
41,242✔
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) {
190,531✔
1598
  int32_t  code = 0;
190,531✔
1599
  uint8_t *p = NULL;
190,531✔
1600
  int16_t  n = 0;
190,531✔
1601
  int16_t  nTag = taosArrayGetSize(pArray);
190,531✔
1602
  int32_t  szTag = 0;
190,536✔
1603
  int8_t   isLarge = 0;
190,536✔
1604

1605
  // sort
1606
  if (isJson) {
190,536✔
1607
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
229✔
1608
  } else {
1609
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
190,307✔
1610
  }
1611

1612
  // get size
1613
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
905,661✔
1614
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
715,123✔
1615
  }
1616
  if (szTag <= INT8_MAX) {
190,538✔
1617
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
175,634✔
1618
  } else {
1619
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
14,904✔
1620
    isLarge = 1;
14,904✔
1621
  }
1622

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

1640
  if (isLarge) {
190,572✔
1641
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
14,910✔
1642
  } else {
1643
    p = (uint8_t *)&(*ppTag)->idx[nTag];
175,662✔
1644
  }
1645
  n = 0;
190,572✔
1646
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
905,771✔
1647
    if (isLarge) {
715,213✔
1648
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
83,254✔
1649
    } else {
1650
      (*ppTag)->idx[iTag] = n;
631,959✔
1651
    }
1652
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
715,213✔
1653
  }
1654
#ifdef TD_DEBUG_PRINT_TAG
1655
  debugPrintSTag(*ppTag, __func__, __LINE__);
1656
#endif
1657

1658
  return code;
190,558✔
1659

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

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

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

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

1679
  if (IS_VAR_DATA_TYPE(value->type)) {
12,749,986✔
1680
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
3,613,500!
1681
    if (data == NULL) {
3,614,650!
1682
      return NULL;
×
1683
    }
1684

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

1689
    varDataLen(data + typeBytes) = value->nData;
3,614,650✔
1690
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
3,614,650✔
1691
  } else {
1692
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
9,136,486✔
1693
  }
1694

1695
  return data;
12,751,136✔
1696
}
1697

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

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

1713
  if (isLarge) {
41,634,423✔
1714
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
34,331,637✔
1715
  } else {
1716
    p = (uint8_t *)&pTag->idx[pTag->nTag];
7,302,786✔
1717
  }
1718

1719
  pTagVal->type = TSDB_DATA_TYPE_NULL;
41,634,423✔
1720
  pTagVal->pData = NULL;
41,634,423✔
1721
  pTagVal->nData = 0;
41,634,423✔
1722
  while (lidx <= ridx) {
135,462,495✔
1723
    midx = (lidx + ridx) / 2;
133,706,891✔
1724
    if (isLarge) {
133,706,891✔
1725
      offset = ((int16_t *)pTag->idx)[midx];
110,771,407✔
1726
    } else {
1727
      offset = pTag->idx[midx];
22,935,484✔
1728
    }
1729

1730
    int32_t nt = tGetTagVal(p + offset, &tv, isJson);
133,706,891✔
1731
    if (isJson) {
133,903,818✔
1732
      c = tTagValJsonCmprFn(pTagVal, &tv);
11,386✔
1733
    } else {
1734
      c = tTagValCmprFn(pTagVal, &tv);
133,892,432✔
1735
    }
1736

1737
    if (c < 0) {
135,228,852✔
1738
      ridx = midx - 1;
45,084,501✔
1739
    } else if (c > 0) {
90,144,351✔
1740
      lidx = midx + 1;
48,743,571✔
1741
    } else {
1742
      (void)memcpy(pTagVal, &tv, sizeof(tv));
41,400,780✔
1743
      return true;
41,400,780✔
1744
    }
1745
  }
1746
  return false;
1,755,604✔
1747
}
1748

1749
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
817,306✔
1750
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
1,634,612✔
1751
}
1752

1753
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
20,079,260✔
1754

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

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

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

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

1787
  return code;
1,451✔
1788

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

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

1801
  pTSchema->numOfCols = numOfCols;
15,566,166✔
1802
  pTSchema->version = version;
15,566,166✔
1803

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

1819
  // other columns
1820
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
183,027,238✔
1821
    SSchema  *pSchema = &aSchema[iCol];
167,461,072✔
1822
    STColumn *pTColumn = &pTSchema->columns[iCol];
167,461,072✔
1823

1824
    pTColumn->colId = pSchema->colId;
167,461,072✔
1825
    pTColumn->type = pSchema->type;
167,461,072✔
1826
    pTColumn->flags = pSchema->flags;
167,461,072✔
1827
    pTColumn->offset = pTSchema->flen;
167,461,072✔
1828

1829
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
167,461,072!
1830
      pTColumn->bytes = pSchema->bytes;
33,284,131✔
1831
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
33,284,131✔
1832
    } else {
1833
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
134,176,941✔
1834
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
134,176,941✔
1835
    }
1836

1837
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
167,461,072✔
1838
  }
1839

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

1844
  return pTSchema;
15,566,166✔
1845
}
1846

1847
static int32_t tTColumnCompare(const void *p1, const void *p2) {
6,088,461✔
1848
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
6,088,461✔
1849
    return -1;
910,037✔
1850
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
5,178,424✔
1851
    return 1;
3,658,175✔
1852
  }
1853

1854
  return 0;
1,520,249✔
1855
}
1856

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

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

1865
// SColData ========================================
1866
void tColDataDestroy(void *ph) {
21,868,865✔
1867
  if (ph) {
21,868,865!
1868
    SColData *pColData = (SColData *)ph;
21,869,058✔
1869

1870
    tFree(pColData->pBitMap);
21,869,058!
1871
    tFree(pColData->aOffset);
21,869,072!
1872
    tFree(pColData->pData);
21,869,117!
1873
  }
1874
}
21,869,884✔
1875

1876
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
20,925,580✔
1877
  pColData->cid = cid;
20,925,580✔
1878
  pColData->type = type;
20,925,580✔
1879
  pColData->cflag = cflag;
20,925,580✔
1880
  tColDataClear(pColData);
20,925,580✔
1881
}
20,927,320✔
1882

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

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

1897
  tColDataClear(pColData);
829,430✔
1898
}
829,434✔
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);
88,467,623!
1905
    if (code) goto _exit;
96,971,087!
1906
    pColData->aOffset[pColData->nVal] = pColData->nData;
96,971,087✔
1907

1908
    if (nData) {
96,971,087!
1909
      code = tRealloc(&pColData->pData, pColData->nData + nData);
95,732,450!
1910
      if (code) goto _exit;
96,359,655!
1911
      (void)memcpy(pColData->pData + pColData->nData, pData, nData);
96,359,655✔
1912
      pColData->nData += nData;
96,359,655✔
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]);
18,321,959✔
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,579,343✔
1933
  pColData->flag = HAS_VALUE;
3,579,388✔
1934
  pColData->numOfValue++;
3,579,343✔
1935
  return tColDataPutValue(pColData, pData, nData);
3,579,690✔
1936
}
1937
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
173,013✔
1938
  pColData->flag = HAS_NONE;
173,013✔
1939
  pColData->numOfNone++;
173,013✔
1940
  pColData->nVal++;
173,013✔
1941
  return 0;
173,013✔
1942
}
1943
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
163,904✔
1944
  pColData->flag = HAS_NULL;
163,962✔
1945
  pColData->numOfNull++;
163,962✔
1946
  pColData->nVal++;
163,962✔
1947
  return 0;
163,904✔
1948
}
1949
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,348✔
1950
  int32_t code = 0;
1,348✔
1951

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

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

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

1962
  if (pColData->nVal) {
1,350!
1963
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,349!
1964
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
238✔
1965
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
238!
1966
      if (code) return code;
243!
1967
      memset(pColData->aOffset, 0, nOffset);
243✔
1968
    } else {
1969
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
1,111✔
1970
      code = tRealloc(&pColData->pData, pColData->nData);
1,111!
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,352✔
1977
}
1978
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
11,036,101✔
1979
  pColData->nVal++;
11,036,101✔
1980
  pColData->numOfNone++;
11,036,101✔
1981
  return 0;
11,036,101✔
1982
}
1983
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
601✔
1984
  int32_t code = 0;
601✔
1985

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

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

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

1997
  return code;
601✔
1998
}
1999
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
17,568✔
2000
  int32_t code = 0;
17,568✔
2001

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

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

2009
  pColData->flag |= HAS_VALUE;
17,567✔
2010
  pColData->numOfValue++;
17,567✔
2011

2012
  if (pColData->nVal) {
17,567!
2013
    if (IS_VAR_DATA_TYPE(pColData->type)) {
17,568!
2014
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
7,878✔
2015
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
7,878!
2016
      if (code) return code;
7,879!
2017
      memset(pColData->aOffset, 0, nOffset);
7,879✔
2018
    } else {
2019
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
9,690✔
2020
      code = tRealloc(&pColData->pData, pColData->nData);
9,690!
2021
      if (code) return code;
9,690!
2022
      memset(pColData->pData, 0, pColData->nData);
9,690✔
2023
    }
2024
  }
2025

2026
  return tColDataPutValue(pColData, pData, nData);
17,569✔
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,425,582✔
2045
  pColData->nVal++;
4,425,582✔
2046
  pColData->numOfNull++;
4,425,582✔
2047
  return 0;
4,425,582✔
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,382✔
2096
  int32_t code = 0;
9,382✔
2097

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

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

2105
  return code;
9,386✔
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,090✔
2112
  int32_t code = 0;
6,090✔
2113

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

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

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

2124
  return tColDataPutValue(pColData, NULL, 0);
6,094✔
2125
}
2126
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
228,235✔
2127
  int32_t code = 0;
233,620✔
2128

2129
  pColData->flag |= HAS_NULL;
233,620✔
2130
  pColData->numOfNull++;
233,620✔
2131

2132
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
233,620✔
2133
  code = tRealloc(&pColData->pBitMap, nBit);
233,316✔
2134
  if (code) return code;
233,627!
2135

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

2139
  return tColDataPutValue(pColData, NULL, 0);
233,630✔
2140
}
2141
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
57,049✔
2142
  int32_t code = 0;
59,050✔
2143

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

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

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

2155
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
102,103!
2156
  if (code) return code;
102,116!
2157

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

2161
  return tColDataPutValue(pColData, NULL, 0);
102,255✔
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) {
247,452,393✔
2184
  int32_t code = 0;
247,455,670✔
2185

2186
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
247,455,270!
2187
  if (code) return code;
248,314,456!
2188
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
248,314,456!
2189
  pColData->numOfValue++;
248,314,456!
2190

2191
  return tColDataPutValue(pColData, pData, nData);
250,449,595✔
2192
}
2193
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,994✔
2194
  int32_t code = 0;
3,994✔
2195

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

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

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

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

2211
  return tColDataPutValue(pColData, NULL, 0);
3,995✔
2212
}
2213
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
16,652,261✔
2214
  int32_t code = 0;
16,666,189✔
2215

2216
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
16,662,594!
2217
  if (code) return code;
16,679,723!
2218
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
16,679,723✔
2219
  pColData->numOfNull++;
16,679,723✔
2220

2221
  return tColDataPutValue(pColData, NULL, 0);
16,694,106✔
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) {
36✔
2296
  if (forward) {
36!
2297
    pColData->numOfNull--;
36✔
2298
    pColData->nVal--;
36✔
2299
    if (pColData->numOfNull) {
36!
2300
      return tColDataAppendValue20(pColData, pData, nData);
×
2301
    } else {
2302
      pColData->flag = 0;
36✔
2303
      return tColDataAppendValue00(pColData, pData, nData);
36✔
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,737,933✔
2343
  if (forward) {  // VALUE ==> VALUE
6,737,933!
2344
    pColData->nVal--;
6,738,101✔
2345
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,738,101!
2346
      pColData->nData = pColData->aOffset[pColData->nVal];
864,705✔
2347
    } else {
2348
      pColData->nData -= TYPE_BYTES[pColData->type];
5,873,396✔
2349
    }
2350
    return tColDataPutValue(pColData, pData, nData);
6,757,630✔
2351
  }
2352
  return 0;
×
2353
}
2354
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,115✔
2355
  if (forward) {  // VALUE ==> NULL
5,115!
2356
    pColData->numOfValue--;
5,115✔
2357
    pColData->nVal--;
5,115✔
2358
    if (pColData->numOfValue) {
5,115✔
2359
      if (IS_VAR_DATA_TYPE(pColData->type)) {
5,081!
2360
        pColData->nData = pColData->aOffset[pColData->nVal];
1,042✔
2361
      } else {
2362
        pColData->nData -= TYPE_BYTES[pColData->type];
4,039✔
2363
      }
2364
      return tColDataAppendValue42(pColData, pData, nData);
5,082✔
2365
    } else {
2366
      pColData->flag = 0;
34✔
2367
      pColData->nData = 0;
34✔
2368
      return tColDataAppendValue02(pColData, pData, nData);
34✔
2369
    }
2370
  }
2371
  return 0;
×
2372
}
2373
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,602✔
2374
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
5,602✔
2375
    pColData->numOfNone--;
5,593✔
2376
    pColData->nVal--;
5,593✔
2377
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
5,593!
2378
      pColData->nData -= TYPE_BYTES[pColData->type];
4,473✔
2379
    }
2380
    if (pColData->numOfNone) {
5,593✔
2381
      return tColDataAppendValue50(pColData, pData, nData);
1,999✔
2382
    } else {
2383
      pColData->flag = HAS_VALUE;
3,593✔
2384
      return tColDataAppendValue40(pColData, pData, nData);
3,593✔
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) {
31,815✔
2429
  if (forward) {
31,815!
2430
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
31,815✔
2431
      pColData->numOfNull--;
21,820✔
2432
      pColData->nVal--;
21,820✔
2433
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
21,820!
2434
        pColData->nData -= TYPE_BYTES[pColData->type];
16,972✔
2435
      }
2436
      if (pColData->numOfNull) {
21,820✔
2437
        return tColDataAppendValue60(pColData, pData, nData);
2,877✔
2438
      } else {
2439
        pColData->flag = HAS_VALUE;
18,943✔
2440
        return tColDataAppendValue40(pColData, pData, nData);
18,943✔
2441
      }
2442
    } else {  // VALUE ==> VALUE
2443
      pColData->nVal--;
9,995✔
2444
      if (IS_VAR_DATA_TYPE(pColData->type)) {
9,995!
2445
        pColData->nData = pColData->aOffset[pColData->nVal];
2,658✔
2446
      } else {
2447
        pColData->nData -= TYPE_BYTES[pColData->type];
7,337✔
2448
      }
2449
      return tColDataPutValue(pColData, pData, nData);
9,995✔
2450
    }
2451
  }
2452
  return 0;
×
2453
}
2454
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
24,798✔
2455
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
24,798!
2456
    pColData->numOfValue--;
10,333✔
2457
    pColData->nVal--;
10,333✔
2458
    if (pColData->numOfValue) {
10,333!
2459
      if (IS_VAR_DATA_TYPE(pColData->type)) {
10,333!
2460
        pColData->nData = pColData->aOffset[pColData->nVal];
2,510✔
2461
      } else {
2462
        pColData->nData -= TYPE_BYTES[pColData->type];
7,823✔
2463
      }
2464
      return tColDataAppendValue62(pColData, pData, nData);
10,331✔
2465
    } else {
2466
      pColData->flag = HAS_NULL;
×
2467
      pColData->nData = 0;
×
2468
      return tColDataAppendValue20(pColData, pData, nData);
×
2469
    }
2470
  }
2471
  return 0;
14,465✔
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,200✔
2487
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
20,800✔
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,595✔
2528
    pColData->nVal--;
3,595✔
2529
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
3,595!
2530
      pColData->nData -= TYPE_BYTES[pColData->type];
2,542✔
2531
    }
2532
    if (pColData->numOfNone) {
3,595!
2533
      return tColDataAppendValue72(pColData, pData, nData);
×
2534
    } else {
2535
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
114,251✔
2536
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
110,656✔
2537
      }
2538
      pColData->flag = (HAS_VALUE | HAS_NULL);
3,595!
2539
      return tColDataAppendValue62(pColData, pData, nData);
3,593✔
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) {
12,400✔
2563
  return 0;
12,400✔
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) {
5,050,745✔
2578
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) return TSDB_CODE_INVALID_PARA;
5,050,745!
2579
  if (!(pColData->nVal > 0)) return TSDB_CODE_INVALID_PARA;
5,050,791!
2580

2581
  if (tColDataUpdateValueImpl[pColData->flag][pColVal->flag] == NULL) return 0;
5,050,791!
2582

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

2588
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
54,945,332✔
2589
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
54,945,332✔
2590
}
54,945,332✔
2591
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,725,624✔
2592
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,725,624✔
2593
}
1,725,624✔
2594
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
11,041✔
2595
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
2596
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
11,041!
2597
    case 0:
1,004✔
2598
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,004✔
2599
      break;
1,004✔
2600
    case 1:
10,038✔
2601
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
10,038✔
2602
      break;
10,038✔
2603
    default:
×
2604
      break;
×
2605
  }
2606
}
11,041✔
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) {
201,277,452!
2611
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
207,115,306✔
2612
    } else {
2613
      value.nData = pColData->nData - pColData->aOffset[iVal];
×
2614
    }
2615
    value.pData = pColData->pData + pColData->aOffset[iVal];
201,277,452✔
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
}
103,028,837✔
2622
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
254,949✔
2623
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
2624
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
254,949!
2625
    case 0:
106,536✔
2626
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
106,536✔
2627
      break;
106,536✔
2628
    case 1:
148,698✔
2629
      tColDataGetValue4(pColData, iVal, pColVal);
2630
      break;
148,698✔
2631
    default:
×
2632
      break;
×
2633
  }
2634
}
254,949✔
2635
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
109,267,470✔
2636
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
2637
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
109,267,470!
2638
    case 0:
6,543,124✔
2639
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
6,543,124✔
2640
      break;
6,543,124✔
2641
    case 1:
102,860,675✔
2642
      tColDataGetValue4(pColData, iVal, pColVal);
2643
      break;
102,860,675✔
2644
    default:
×
2645
      break;
×
2646
  }
2647
}
109,267,470✔
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) {
430,940,239✔
2684
  switch (pColData->flag) {
430,940,239!
2685
    case HAS_NONE:
×
2686
      return 0;
×
2687
    case HAS_NULL:
×
2688
      return 1;
×
2689
    case (HAS_NULL | HAS_NONE):
11,055✔
2690
      return GET_BIT1(pColData->pBitMap, iVal);
11,055✔
2691
    case HAS_VALUE:
×
2692
      return 2;
×
2693
    case (HAS_VALUE | HAS_NONE):
743,722✔
2694
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
743,722✔
2695
    case (HAS_VALUE | HAS_NULL):
430,230,751✔
2696
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
430,230,751✔
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) {
449✔
2705
  int32_t code = 0;
449✔
2706

2707
  *pColData = *pColDataFrom;
449✔
2708

2709
  // bitmap
2710
  switch (pColData->flag) {
449!
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:
435✔
2730
      pColData->pBitMap = NULL;
435✔
2731
      break;
435✔
2732
  }
2733

2734
  // offset
2735
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
449!
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;
345✔
2744
  }
2745

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

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

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

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

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

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

2780
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
2,692,602✔
2781
    return 0;
139,655✔
2782
  }
2783

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

2789
  // bitmap
2790
  if (colData->flag != HAS_VALUE) {
2,552,947✔
2791
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
205,867✔
2792
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1✔
2793
    } else {
2794
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
205,866✔
2795
    }
2796

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

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

2809
    info->bitmapCompressedSize = cinfo.compressedSize;
205,882✔
2810
  }
2811

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

2817
  // offset
2818
  if (IS_VAR_DATA_TYPE(colData->type)) {
2,552,361!
2819
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
320,176✔
2820

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

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

2833
    info->offsetCompressedSize = cinfo.compressedSize;
320,094✔
2834
  }
2835

2836
  // data
2837
  if (colData->nData > 0) {
2,552,279!
2838
    info->dataOriginalSize = colData->nData;
2,552,379✔
2839

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

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

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

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

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

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

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

2876
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
18,451,215✔
2877
    goto _exit;
1,948,239✔
2878
  }
2879

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

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

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

2901
    data += cinfo.compressedSize;
303,790✔
2902
  }
2903

2904
  if (info->flag == (HAS_NONE | HAS_NULL)) {
16,502,935✔
2905
    goto _exit;
601✔
2906
  }
2907

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

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

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

2929
    data += cinfo.compressedSize;
2,850,580✔
2930
  }
2931

2932
  // data
2933
  if (info->dataOriginalSize > 0) {
16,502,341✔
2934
    colData->nData = info->dataOriginalSize;
16,501,265✔
2935

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

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

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

2955
    data += cinfo.compressedSize;
16,501,134✔
2956
  }
2957

2958
_exit:
1,076✔
2959
  switch (colData->flag) {
18,451,050✔
2960
    case HAS_NONE:
1,520,470✔
2961
      colData->numOfNone = colData->nVal;
1,520,470✔
2962
      break;
1,520,470✔
2963
    case HAS_NULL:
429,384✔
2964
      colData->numOfNull = colData->nVal;
429,384✔
2965
      break;
429,384✔
2966
    case HAS_VALUE:
16,197,297✔
2967
      colData->numOfValue = colData->nVal;
16,197,297✔
2968
      break;
16,197,297✔
2969
    default:
303,899✔
2970
      for (int32_t i = 0; i < colData->nVal; i++) {
208,628,746✔
2971
        uint8_t bitValue = tColDataGetBitValue(colData, i);
208,327,138✔
2972
        if (bitValue == 0) {
208,324,847✔
2973
          colData->numOfNone++;
213,088✔
2974
        } else if (bitValue == 1) {
208,111,759✔
2975
          colData->numOfNull++;
17,810,456✔
2976
        } else {
2977
          colData->numOfValue++;
190,301,303✔
2978
        }
2979
      }
2980
  }
2981
  tBufferDestroy(&local);
2982
  return 0;
18,448,759✔
2983
}
2984

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

2999
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
561!
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;
461✔
3023
    bool allNull = true;
461✔
3024
    for (int32_t i = 0; i < nRows; ++i) {
1,266✔
3025
      if (!colDataIsNull_f(lengthOrbitmap, i)) {
805✔
3026
        allNull = false;
592✔
3027
      } else {
3028
        allValue = false;
213✔
3029
      }
3030
    }
3031
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
461!
3032
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3033
      goto _exit;
×
3034
    }
3035

3036
    if (allValue) {
461✔
3037
      // optimize (todo)
3038
      for (int32_t i = 0; i < nRows; ++i) {
945✔
3039
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
578✔
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;
573✔
3061
}
3062

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

3067
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
8,413,770✔
3068
    if (!(pColData->type == pBind->buffer_type)) {
7,844,125!
3069
      return TSDB_CODE_INVALID_PARA;
×
3070
    }
3071
  }
3072

3073
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
8,413,770!
3074
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
894,672✔
3075
      code = igeos();
3✔
3076
      if (code) {
3!
3077
        return code;
×
3078
      }
3079
    }
3080
    for (int32_t i = 0; i < pBind->num; ++i) {
3,392,929✔
3081
      if (pBind->is_null && pBind->is_null[i]) {
2,330,406✔
3082
        if (pColData->cflag & COL_IS_KEY) {
102,358!
3083
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3084
          goto _exit;
×
3085
        }
3086
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
102,358✔
3087
        if (code) goto _exit;
102,920!
3088
      } else if (pBind->length[i] > buffMaxLen) {
2,228,048✔
3089
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
10✔
3090
      } else {
3091
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
2,228,038✔
3092
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
5✔
3093
          if (code) goto _exit;
5✔
3094
        }
3095
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,228,037✔
3096
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
2,228,037✔
3097
      }
3098
    }
3099
  } else {  // fixed-length data type
3100
    bool allValue;
3101
    bool allNull;
3102
    if (pBind->is_null) {
7,519,098✔
3103
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
492,825✔
3104
      allNull = (same && pBind->is_null[0] != 0);
492,825✔
3105
      allValue = (same && pBind->is_null[0] == 0);
492,825✔
3106
    } else {
3107
      allNull = false;
7,026,273✔
3108
      allValue = true;
7,026,273✔
3109
    }
3110

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

3116
    if (allValue) {
7,519,098✔
3117
      // optimize (todo)
3118
      for (int32_t i = 0; i < pBind->num; ++i) {
18,001,770✔
3119
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
9,037,002✔
3120
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
9,037,002✔
3121
      }
3122
    } else if (allNull) {
426,755!
3123
      // optimize (todo)
3124
      for (int32_t i = 0; i < pBind->num; ++i) {
971,005✔
3125
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
479,271✔
3126
        if (code) goto _exit;
491,597!
3127
      }
3128
    } else {
3129
      for (int32_t i = 0; i < pBind->num; ++i) {
×
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:
×
3142
  return code;
10,466,373✔
3143
}
3144

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

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

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

3163
    uint8_t *buf = pBind->buffer;
49✔
3164
    for (int32_t i = 0; i < pBind->num; ++i) {
9,455✔
3165
      if (pBind->is_null && pBind->is_null[i]) {
9,438!
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) {
9,438!
3178
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
3179
      } else {
3180
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
9,438!
3181
          code = cgeos(buf, pBind->length[i]);
×
3182
          if (code) goto _exit;
×
3183
        }
3184
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
9,438✔
3185
        buf += pBind->length[i];
9,406✔
3186
      }
3187
    }
3188
  } else {  // fixed-length data type
3189
    bool allValue;
3190
    bool allNull;
3191
    bool allNone;
3192
    if (pBind->is_null) {
49✔
3193
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
24✔
3194
      allNull = (same && pBind->is_null[0] == 1);
24!
3195
      allNone = (same && pBind->is_null[0] > 1);
24!
3196
      allValue = (same && pBind->is_null[0] == 0);
24!
3197
    } else {
3198
      allNull = false;
25✔
3199
      allNone = false;
25✔
3200
      allValue = true;
25✔
3201
    }
3202

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

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

3216
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
8,604✔
3217
      }
3218
    } else if (allNull) {
1!
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) {
1!
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) {
1!
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:
1✔
3253
  return code;
63✔
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,
8✔
3266
                           SArray *rowArray, bool *pOrdered, bool *pDupTs) {
3267
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
8!
3268
    return TSDB_CODE_INVALID_PARA;
×
3269
  }
3270

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

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

3280
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
8!
3281
    return terrno;
×
3282
  }
3283
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
8!
3284
    taosArrayDestroy(colValArray);
×
3285
    return terrno;
×
3286
  }
3287
  for (int i = 0; i < numOfInfos; ++i) {
40✔
3288
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
64!
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++) {
3,782!
3297
    taosArrayClear(colValArray);
3,996✔
3298

3299
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
17,723✔
3300
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
12,722!
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 = {
12,722✔
3308
            .type = infos[iInfo].type,
12,722✔
3309
        };
3310
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
12,722!
3311
          int32_t   length = infos[iInfo].bind->length[iRow];
×
3312
          uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
×
3313
          value.nData = length;
×
3314
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
×
3315
            code = TSDB_CODE_INVALID_PARA;
×
3316
            goto _exit;
×
3317
          }
3318
          value.pData = *data;
×
3319
          *data += length;
×
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;
12,722✔
3323
          if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
12,722!
3324
            *val = 1;
×
3325
          }
3326
          (void)memcpy(&value.val, val,
12,722✔
3327
                       /*(uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,*/
3328
                       infos[iInfo].bytes /*bind->buffer_length*/);
12,722✔
3329
        }
3330
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
12,722✔
3331
      }
3332
      if (taosArrayPush(colValArray, &colVal) == NULL) {
13,731!
3333
        code = terrno;
×
3334
        goto _exit;
×
3335
      }
3336
    }
3337

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

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

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

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

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

3374
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
1,000!
3375
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
728✔
3376
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
267✔
3377
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
364✔
3378
    if (iToRow == 0) {
364✔
3379
      pToColData->aOffset[iToRow] = 0;
44✔
3380
    }
3381

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

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

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

3399
  switch (pFromColData->flag) {
1,008!
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: {
952✔
3407
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
952!
3408
    } break;
952✔
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;
1,008✔
3423
}
3424

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

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

3436
  return code;
94✔
3437
}
3438

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

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

3454
  return code;
94✔
3455
}
3456

3457
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
3,095,622✔
3458
  SColVal cv;
3459

3460
  key->ts = ((TSKEY *)aColData[0].pData)[iRow];
3,095,622✔
3461
  key->numOfPKs = 0;
3,095,622✔
3462

3463
  for (int i = 1; i < nColData; i++) {
3,115,085!
3464
    if (aColData[i].cflag & COL_IS_KEY) {
3,116,652✔
3465
      tColDataGetValue4(&aColData[i], iRow, &cv);
19,463!
3466
      key->pks[key->numOfPKs++] = cv.value;
19,463✔
3467
    } else {
3468
      break;
3,097,189✔
3469
    }
3470
  }
3471
}
3,095,622✔
3472

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

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

3488
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
26✔
3489
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
26✔
3490
  while (i <= mid && j <= end) {
72✔
3491
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
46✔
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));
22!
3496
      tColDataArrGetRowKey(aColData, nColData, j, &keyj);
22✔
3497
    }
3498
  }
3499

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

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

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

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

3519
  return TSDB_CODE_SUCCESS;
26✔
3520
}
3521

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

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

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

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

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

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

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

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

3550
  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
4✔
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,602✔
3625
  SArray   *colDataArr = *arr;
92,602✔
3626
  int32_t   nColData = TARRAY_SIZE(colDataArr);
92,602✔
3627
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
92,602✔
3628

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

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

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

3650
    int32_t c = tRowKeyCompare(&lastKey, &key);
1,464,670✔
3651
    if (c < 0) {
1,464,670✔
3652
      lastKey = key;
1,464,299✔
3653
      continue;
1,464,299✔
3654
    } else if (c > 0) {
371✔
3655
      doSort = 1;
4✔
3656
      break;
4✔
3657
    } else {
3658
      doMerge = 1;
367✔
3659
    }
3660
  }
3661

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

3667
  if (doMerge != 1) {
93,114✔
3668
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
90,836✔
3669
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
1,557,117✔
3670
      SRowKey key;
3671
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
1,468,555✔
3672

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

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

3688
_exit:
90,840✔
3689
  return 0;
92,608✔
3690
}
3691

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

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

3700
  // bitmap
3701
  switch (pColData->flag) {
1,611,403!
3702
    case (HAS_NULL | HAS_NONE):
19,070✔
3703
    case (HAS_VALUE | HAS_NONE):
3704
    case (HAS_VALUE | HAS_NULL):
3705
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
19,070✔
3706
      if (code) return code;
19,070!
3707
      break;
19,070✔
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,592,333✔
3713
      break;
1,592,333✔
3714
  }
3715

3716
  // value
3717
  if (pColData->flag & HAS_VALUE) {
1,611,403✔
3718
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,611,056!
3719
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
217,779✔
3720
      if (code) return code;
217,779!
3721

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

3725
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
217,779✔
3726
      if (code) return code;
217,779!
3727
    } else {
3728
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
1,393,277✔
3729
      if (code) return code;
1,393,277!
3730
    }
3731
  }
3732

3733
  return code;
1,611,403✔
3734
}
3735

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

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

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

3748
  // bitmap
3749
  switch (pColData->flag) {
2,011!
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,959✔
3761
      break;
1,959✔
3762
  }
3763

3764
  // value
3765
  if (pColData->flag & HAS_VALUE) {
2,011✔
3766
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,819!
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,443✔
3777
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
1,443!
3778
      if (code) return code;
1,443!
3779
    }
3780
  }
3781
  pColData->cflag = 0;
2,012✔
3782

3783
  return code;
2,012✔
3784
}
3785

3786
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
1,611,402✔
3787
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
1,611,402✔
3788
  if (code) return code;
1,611,435!
3789
  return tEncodeI8(pEncoder, pColData->cflag);
3,222,870✔
3790
}
3791

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

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

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

3810
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData) {
2,011✔
3811
  if (version == 0) {
2,011!
3812
    return tDecodeColDataVersion0(pDecoder, pColData);
×
3813
  } else if (version == 1) {
2,011!
3814
    return tDecodeColDataVersion1(pDecoder, pColData);
2,011✔
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) {
906,914,221✔
3823
  if (ppRow == NULL) {
906,914,221!
3824
    return TSDB_CODE_INVALID_PARA;
×
3825
  }
3826

3827
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
906,914,221!
3828
    return TSDB_CODE_OUT_OF_RANGE;
×
3829
  }
3830

3831
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
906,914,221✔
3832
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
1,813,828,442!
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,
5,549✔
3843
                                             int16_t *numOfNull) {
3844
  *sum = 0;
5,549✔
3845
  *max = 0;
5,549✔
3846
  *min = 1;
5,549✔
3847
  *numOfNull = 0;
5,549✔
3848

3849
  int8_t val;
3850
  if (HAS_VALUE == pColData->flag) {
5,549!
3851
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,985,753✔
3852
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
3,980,204✔
3853
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
3,980,204✔
3854
    }
3855
  } else {
3856
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3857
      switch (tColDataGetBitValue(pColData, iVal)) {
×
3858
        case 0:
×
3859
        case 1:
3860
          (*numOfNull)++;
×
3861
          break;
×
3862
        case 2:
×
3863
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
×
3864
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
×
3865
          break;
×
3866
        default:
×
3867
          break;
×
3868
      }
3869
    }
3870
  }
3871
}
5,549✔
3872

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

3880
  int8_t val;
3881
  if (HAS_VALUE == pColData->flag) {
5,585!
3882
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,107,242✔
3883
      val = ((int8_t *)pColData->pData)[iVal];
4,101,657✔
3884
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,101,657✔
3885
    }
3886
  } else {
3887
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3888
      switch (tColDataGetBitValue(pColData, iVal)) {
×
3889
        case 0:
×
3890
        case 1:
3891
          (*numOfNull)++;
×
3892
          break;
×
3893
        case 2:
×
3894
          val = ((int8_t *)pColData->pData)[iVal];
×
3895
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
×
3896
          break;
×
3897
        default:
×
3898
          break;
×
3899
      }
3900
    }
3901
  }
3902
}
5,585✔
3903

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

3911
  int16_t val;
3912
  if (HAS_VALUE == pColData->flag) {
5,585✔
3913
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,106,839✔
3914
      val = ((int16_t *)pColData->pData)[iVal];
4,101,255✔
3915
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,101,255✔
3916
    }
3917
  } else {
3918
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1!
3919
      switch (tColDataGetBitValue(pColData, iVal)) {
×
3920
        case 0:
×
3921
        case 1:
3922
          (*numOfNull)++;
×
3923
          break;
×
3924
        case 2:
×
3925
          val = ((int16_t *)pColData->pData)[iVal];
×
3926
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
×
3927
          break;
×
3928
        default:
×
3929
          break;
×
3930
      }
3931
    }
3932
  }
3933
}
5,585✔
3934

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

3942
  int32_t val;
3943
  if (HAS_VALUE == pColData->flag) {
271,323✔
3944
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
850,704,461✔
3945
      val = ((int32_t *)pColData->pData)[iVal];
850,464,301✔
3946
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
850,464,301✔
3947
    }
3948
  } else {
3949
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
86,166,564✔
3950
      switch (tColDataGetBitValue(pColData, iVal)) {
86,135,678!
3951
        case 0:
5,092,489✔
3952
        case 1:
3953
          (*numOfNull)++;
5,092,489✔
3954
          break;
5,092,489✔
3955
        case 2:
81,078,017✔
3956
          val = ((int32_t *)pColData->pData)[iVal];
81,078,017✔
3957
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
81,078,017✔
3958
          break;
81,078,017✔
3959
        default:
×
3960
          break;
×
3961
      }
3962
    }
3963
  }
3964
}
271,046✔
3965

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

3973
  int64_t val;
3974
  if (HAS_VALUE == pColData->flag) {
85,550✔
3975
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
236,513,148✔
3976
      val = ((int64_t *)pColData->pData)[iVal];
236,431,735✔
3977
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
236,431,735✔
3978
    }
3979
  } else {
3980
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
15,084,065✔
3981
      switch (tColDataGetBitValue(pColData, iVal)) {
15,079,974!
3982
        case 0:
1,571,242✔
3983
        case 1:
3984
          (*numOfNull)++;
1,571,242✔
3985
          break;
1,571,242✔
3986
        case 2:
13,509,356✔
3987
          val = ((int64_t *)pColData->pData)[iVal];
13,509,356✔
3988
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
13,509,356✔
3989
          break;
13,509,356✔
3990
        default:
×
3991
          break;
×
3992
      }
3993
    }
3994
  }
3995
}
85,504✔
3996

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

4004
  float val;
4005
  if (HAS_VALUE == pColData->flag) {
246,799✔
4006
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
990,667,578✔
4007
      val = ((float *)pColData->pData)[iVal];
990,420,780✔
4008
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
990,420,780✔
4009
    }
4010
  } else {
4011
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
106✔
4012
      switch (tColDataGetBitValue(pColData, iVal)) {
105!
4013
        case 0:
95✔
4014
        case 1:
4015
          (*numOfNull)++;
95✔
4016
          break;
95✔
4017
        case 2:
10✔
4018
          val = ((float *)pColData->pData)[iVal];
10✔
4019
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
10!
4020
          break;
10✔
4021
        default:
×
4022
          break;
×
4023
      }
4024
    }
4025
  }
4026
}
246,799✔
4027

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

4035
  double val;
4036
  if (HAS_VALUE == pColData->flag) {
7,063!
4037
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
12,462,724✔
4038
      val = ((double *)pColData->pData)[iVal];
12,455,661✔
4039
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
12,455,661✔
4040
    }
4041
  } else {
4042
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4043
      switch (tColDataGetBitValue(pColData, iVal)) {
×
4044
        case 0:
×
4045
        case 1:
4046
          (*numOfNull)++;
×
4047
          break;
×
4048
        case 2:
×
4049
          val = ((double *)pColData->pData)[iVal];
×
4050
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
×
4051
          break;
×
4052
        default:
×
4053
          break;
×
4054
      }
4055
    }
4056
  }
4057
}
7,063✔
4058

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

4066
  uint8_t val;
4067
  if (HAS_VALUE == pColData->flag) {
2,670!
4068
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,223,280✔
4069
      val = ((uint8_t *)pColData->pData)[iVal];
3,220,610✔
4070
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,220,610✔
4071
    }
4072
  } else {
4073
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4074
      switch (tColDataGetBitValue(pColData, iVal)) {
×
4075
        case 0:
×
4076
        case 1:
4077
          (*numOfNull)++;
×
4078
          break;
×
4079
        case 2:
×
4080
          val = ((uint8_t *)pColData->pData)[iVal];
×
4081
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
4082
          break;
×
4083
        default:
×
4084
          break;
×
4085
      }
4086
    }
4087
  }
4088
}
2,670✔
4089

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

4097
  uint16_t val;
4098
  if (HAS_VALUE == pColData->flag) {
2,670!
4099
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,223,280✔
4100
      val = ((uint16_t *)pColData->pData)[iVal];
3,220,610✔
4101
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,220,610✔
4102
    }
4103
  } else {
4104
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4105
      switch (tColDataGetBitValue(pColData, iVal)) {
×
4106
        case 0:
×
4107
        case 1:
4108
          (*numOfNull)++;
×
4109
          break;
×
4110
        case 2:
×
4111
          val = ((uint16_t *)pColData->pData)[iVal];
×
4112
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
4113
          break;
×
4114
        default:
×
4115
          break;
×
4116
      }
4117
    }
4118
  }
4119
}
2,670✔
4120

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

4128
  uint32_t val;
4129
  if (HAS_VALUE == pColData->flag) {
2,670!
4130
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,223,280✔
4131
      val = ((uint32_t *)pColData->pData)[iVal];
3,220,610✔
4132
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,220,610✔
4133
    }
4134
  } else {
4135
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4136
      switch (tColDataGetBitValue(pColData, iVal)) {
×
4137
        case 0:
×
4138
        case 1:
4139
          (*numOfNull)++;
×
4140
          break;
×
4141
        case 2:
×
4142
          val = ((uint32_t *)pColData->pData)[iVal];
×
4143
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
4144
          break;
×
4145
        default:
×
4146
          break;
×
4147
      }
4148
    }
4149
  }
4150
}
2,670✔
4151

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

4159
  uint64_t val;
4160
  if (HAS_VALUE == pColData->flag) {
2,670!
4161
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,223,280✔
4162
      val = ((uint64_t *)pColData->pData)[iVal];
3,220,610✔
4163
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,220,610✔
4164
    }
4165
  } else {
4166
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4167
      switch (tColDataGetBitValue(pColData, iVal)) {
×
4168
        case 0:
×
4169
        case 1:
4170
          (*numOfNull)++;
×
4171
          break;
×
4172
        case 2:
×
4173
          val = ((uint64_t *)pColData->pData)[iVal];
×
4174
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
4175
          break;
×
4176
        default:
×
4177
          break;
×
4178
      }
4179
    }
4180
  }
4181
}
2,670✔
4182

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

4190
  switch (pColData->flag) {
17,982!
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,982✔
4197
      *numOfNull = 0;
17,982✔
4198
      break;
17,982✔
4199
    case (HAS_VALUE | HAS_NULL):
×
4200
    case (HAS_VALUE | HAS_NONE):
4201
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4202
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
×
4203
          (*numOfNull)++;
×
4204
        }
4205
      }
4206
      break;
×
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
}
17,982✔
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) {
19,047,151✔
4245
  valCol->type = TSDB_DATA_TYPE_NULL;
19,047,151✔
4246
  valCol->numOfValues = 0;
19,047,151✔
4247
  tBufferInit(&valCol->data);
19,047,151✔
4248
  tBufferInit(&valCol->offsets);
19,047,151✔
4249
  return 0;
19,047,151✔
4250
}
4251

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

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

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

4271
  if (valCol->numOfValues == 0) {
924,967✔
4272
    valCol->type = value->type;
4,358✔
4273
  }
4274

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

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

4292
  return 0;
924,967✔
4293
}
4294

4295
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
243,726,391✔
4296
  int32_t code;
4297

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

4302
  if (IS_VAR_DATA_TYPE(valCol->type)) {
243,735,859!
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,
243,756,268✔
4321
                        tDataTypes[valCol->type].bytes);
243,757,792✔
4322
  }
4323
  return 0;
4324
}
4325

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

4331
  value->type = valCol->type;
491,795,173✔
4332
  if (IS_VAR_DATA_TYPE(value->type)) {
491,795,173!
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) {
106✔
4338
      nextOffset = tBufferGetSize(&valCol->data);
102✔
4339
    } else {
4340
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
4!
4341
    }
4342
    value->nData = nextOffset - offset;
106✔
4343
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
106✔
4344
  } else {
4345
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
491,835,080✔
4346
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, &value->val));
983,670,160!
4347
  }
4348
  return 0;
491,835,186✔
4349
}
4350

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

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

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

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

4371
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
14✔
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,358✔
4380
      .cmprAlg = info->cmprAlg,
4,358✔
4381
      .dataType = valCol->type,
4,358✔
4382
      .originalSize = valCol->data.size,
4,358✔
4383
  };
4384

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

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

4391
  return 0;
4,358✔
4392
}
4393

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

4398
  tValueColumnClear(valCol);
44,334✔
4399
  valCol->type = info->type;
44,335✔
4400
  // offset
4401
  if (IS_VAR_DATA_TYPE(valCol->type)) {
44,341!
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;
44,329✔
4417
  }
4418

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

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

4432
  return 0;
44,336✔
4433
}
4434

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

4439
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
8,716!
4440
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
8,716!
4441
  if ((code = tBufferPutI8(buffer, info->type))) return code;
8,716!
4442
  if (IS_VAR_DATA_TYPE(info->type)) {
4,358!
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,716!
4447
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
8,716!
4448

4449
  return 0;
4,358✔
4450
}
4451

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

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

4473
  return 0;
44,336✔
4474
}
4475

4476
int32_t tCompressData(void          *input,       // input
6,179,650✔
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;
6,179,650!
4486
  if (!(outputSize >= extraSizeNeeded)) {
6,179,650!
4487
    return TSDB_CODE_INVALID_PARA;
×
4488
  }
4489

4490
  if (info->cmprAlg == NO_COMPRESSION) {
6,179,650!
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) {
7,803,478!
4494
    SBuffer local;
4495

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

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

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

4524
    tBufferDestroy(&local);
4525
  } else {
4526
    DEFINE_VAR(info->cmprAlg)
4,558,028✔
4527
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
4,558,028!
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,558,028!
4536
      buffer = &local;
×
4537
    }
4538
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
4,558,028✔
4539

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

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

4559
  return 0;
6,182,598✔
4560
}
4561

4562
int32_t tDecompressData(void                *input,       // input
68,306,022✔
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)) {
68,306,022!
4571
    return TSDB_CODE_INVALID_PARA;
×
4572
  }
4573

4574
  if (info->cmprAlg == NO_COMPRESSION) {
68,306,022!
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) {
95,688,986!
4580
    SBuffer local;
4581

4582
    tBufferInit(&local);
4583
    if (buffer == NULL) {
27,360,281!
4584
      buffer = &local;
×
4585
    }
4586

4587
    if (info->cmprAlg == TWO_STAGE_COMP) {
27,360,281!
4588
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
27,383,639✔
4589
      if (code) {
27,384,530!
4590
        tBufferDestroy(&local);
4591
        return code;
×
4592
      }
4593
    }
4594

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

4610
    if (!(decompressedSize == info->originalSize)) {
27,382,964!
4611
      return TSDB_CODE_COMPRESS_ERROR;
×
4612
    }
4613
    tBufferDestroy(&local);
4614
  } else {
4615
    DEFINE_VAR(info->cmprAlg);
40,945,741✔
4616
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
40,945,741!
4617
      (void)memcpy(output, input, info->compressedSize);
×
4618
      return 0;
×
4619
    }
4620
    SBuffer local;
4621

4622
    tBufferInit(&local);
4623
    if (buffer == NULL) {
40,945,741!
4624
      buffer = &local;
×
4625
    }
4626
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
40,945,741✔
4627
    if (code) {
40,946,240!
4628
      return code;
×
4629
    }
4630

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

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

4652
  return 0;
68,332,414✔
4653
}
4654

4655
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
6,180,365✔
4656
  int32_t code;
4657

4658
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
6,180,365✔
4659
  if (code) return code;
6,180,356!
4660

4661
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
6,180,356✔
4662
  if (code) return code;
6,180,572!
4663

4664
  output->size += info->compressedSize;
6,180,572✔
4665
  return 0;
6,180,572✔
4666
}
4667

4668
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
27,381,642✔
4669
  int32_t code;
4670

4671
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
27,381,642✔
4672
  if (code) return code;
27,379,500!
4673

4674
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
27,379,500✔
4675
  if (code) return code;
27,382,871!
4676

4677
  output->size += info->originalSize;
27,382,871✔
4678
  return 0;
27,382,871✔
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