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

taosdata / TDengine / #3585

20 Jan 2025 09:22AM UTC coverage: 63.647% (-0.1%) from 63.756%
#3585

push

travis-ci

web-flow
Merge pull request #29603 from taosdata/enh/3.0/TD-32588

enh:[TD-32588]refactor stmt-async-bind loop usleep to Producer Consumer Model

140935 of 284561 branches covered (49.53%)

Branch coverage included in aggregate %.

219502 of 281745 relevant lines covered (77.91%)

18611836.92 hits per line

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

63.84
/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) {
601,206,429✔
65
  int32_t n = 0;
601,206,429✔
66
  n += tPutI8(p ? p + n : p, index->type);
601,206,429✔
67
  n += tPutU32v(p ? p + n : p, index->offset);
601,206,429✔
68
  return n;
601,206,429✔
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++;
27,946,892✔
113
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
27,946,892✔
114
  sinfo->kvPayloadSize += tPutI16v(NULL, -pTColumn->colId);
27,946,892✔
115
  return 0;
27,946,892✔
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,083,655✔
123
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
206,083,655✔
124
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
206,083,655!
125
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
206,083,655✔
126
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
206,083,655✔
127
    sinfo->numOfPKs++;
206,083,655✔
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
690,211,745✔
133
                           + colVal->value.nData;               // value
690,211,745✔
134

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

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

161
  *sinfo = (SRowBuildScanInfo){
1,099,408,942✔
162
      .tupleFixedSize = schema->flen,
1,099,408,942✔
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;
55,893,784!
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) {
349,947✔
190
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
62!
191
        break;
31✔
192
      } else {  // skip useless value
193
        colValIndex++;
349,916✔
194
      }
195
    }
196
  }
197

198
  if (sinfo->numOfNone) {
1,099,408,942✔
199
    sinfo->flag |= HAS_NONE;
459,942,002✔
200
  }
201
  if (sinfo->numOfNull) {
1,099,408,942✔
202
    sinfo->flag |= HAS_NULL;
19,140,729✔
203
  }
204
  if (sinfo->numOfValue) {
1,099,408,942✔
205
    sinfo->flag |= HAS_VALUE;
1,031,174,587✔
206
  }
207

208
  // Tuple
209
  sinfo->tupleFlag = sinfo->flag;
1,099,408,942✔
210
  switch (sinfo->flag) {
1,099,408,942!
211
    case HAS_NONE:
74,026,573✔
212
    case HAS_NULL:
213
      sinfo->tupleBitmapSize = 0;
74,026,573✔
214
      sinfo->tupleFixedSize = 0;
74,026,573✔
215
      break;
74,026,573✔
216
    case HAS_VALUE:
628,063,193✔
217
      sinfo->tupleBitmapSize = 0;
628,063,193✔
218
      sinfo->tupleFixedSize = schema->flen;
628,063,193✔
219
      break;
628,063,193✔
220
    case (HAS_NONE | HAS_NULL):
24,263✔
221
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
24,263✔
222
      sinfo->tupleFixedSize = 0;
24,263✔
223
      break;
24,263✔
224
    case (HAS_NONE | HAS_VALUE):
404,341,939✔
225
    case (HAS_NULL | HAS_VALUE):
226
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
404,341,939✔
227
      sinfo->tupleFixedSize = schema->flen;
404,341,939✔
228
      break;
404,341,939✔
229
    case (HAS_NONE | HAS_NULL | HAS_VALUE):
453,348✔
230
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
453,348✔
231
      sinfo->tupleFixedSize = schema->flen;
453,348✔
232
      break;
453,348✔
233
  }
234
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
1,303,518,987✔
235
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
205,042,900✔
236
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
205,042,900✔
237
  }
238
  sinfo->tupleRowSize = sizeof(SRow)              // SRow
1,098,476,087✔
239
                        + sinfo->tuplePKSize      // primary keys
1,098,476,087✔
240
                        + sinfo->tupleBitmapSize  // bitmap
1,098,476,087✔
241
                        + sinfo->tupleFixedSize   // fixed part
1,098,476,087✔
242
                        + sinfo->tupleVarSize;    // var part
1,098,476,087✔
243

244
  // Key-Value
245
  if (sinfo->kvMaxOffset <= UINT8_MAX) {
1,098,476,087!
246
    sinfo->kvFlag = (KV_FLG_LIT | sinfo->flag);
1,101,546,938✔
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint8_t);
1,101,546,938✔
248
  } else if (sinfo->kvMaxOffset <= UINT16_MAX) {
×
249
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
4,339,727✔
250
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
4,339,727✔
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,301,494,243✔
256
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
204,031,846✔
257
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
204,031,846✔
258
  }
259
  sinfo->kvRowSize = sizeof(SRow)             // SRow
1,097,462,397✔
260
                     + sinfo->kvPKSize        // primary keys
1,097,462,397✔
261
                     + sinfo->kvIndexSize     // index array
1,097,462,397✔
262
                     + sinfo->kvPayloadSize;  // payload
1,097,462,397✔
263

264
_exit:
1,097,462,397✔
265
  return code;
1,097,462,397✔
266
}
267

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

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

282
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
720,661,657✔
283
    return 0;
75,587,291✔
284
  }
285

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

291
  // primary keys
292
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
848,938,308✔
293
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
206,760,219✔
294
  }
295

296
  // bitmap + fixed + varlen
297
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
642,178,089✔
298
  int32_t colValIndex = 1;
642,178,089✔
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;
167,907,343✔
312
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
167,907,343✔
313
            if (colValArray[colValIndex].value.nData) {
167,907,343!
314
              (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
172,036,588✔
315
              varlen += colValArray[colValIndex].value.nData;
172,036,588✔
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
12,961,686!
322
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
15,080,003!
323
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
324
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
466,313!
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
2,090,891✔
330
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
9!
331
        break;
9✔
332
      } else {
333
        colValIndex++;
2,090,882✔
334
      }
335
    }
336
  }
337

338
  return 0;
642,178,089✔
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,771,770!
345
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
7,551,219✔
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) {
392,185,785✔
353
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
392,185,785✔
354

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

365
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
392,576,952!
366
    return TSDB_CODE_INVALID_PARA;
×
367
  }
368

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

374
  // primary keys
375
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
394,223,062✔
376
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
1,580,120✔
377
  }
378

379
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
392,642,942✔
380
  int32_t colValIndex = 1;
392,642,942✔
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);
515,216,725✔
392
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
515,216,725✔
393
            if (colValArray[colValIndex].value.nData > 0) {
515,216,725!
394
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
535,424,507✔
395
                           colValArray[colValIndex].value.nData);
535,424,507✔
396
            }
397
            payloadSize += colValArray[colValIndex].value.nData;
515,216,725✔
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);
12,625,769✔
406
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
25,251,538✔
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;
392,642,942✔
420
}
421

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

426
  code = tRowBuildScan(aColVal, pTSchema, &sinfo);
1,099,646,306✔
427
  if (code) return code;
1,104,910,939!
428

429
  if (sinfo.tupleRowSize <= sinfo.kvRowSize) {
1,104,910,939✔
430
    code = tRowBuildTupleRow(aColVal, &sinfo, pTSchema, ppRow);
712,953,307✔
431
  } else {
432
    code = tRowBuildKVRow(aColVal, &sinfo, pTSchema, ppRow);
391,957,632✔
433
  }
434
  return code;
1,107,383,837✔
435
}
436

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

555
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
556
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
847,041✔
557
    return 0;
847,041✔
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);
72,759,201✔
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) {
73,703,349!
573
      pv = pIdx->idx + (pIdx->nCol << 1);
99,590,642✔
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) {
419,263,603!
586
        pData = pv + ((uint16_t *)pIdx->idx)[mid];
419,492,456✔
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,011,390✔
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);
699,317,281!
604
            if (pColVal->value.nData > 0) {
699,317,281!
605
              pColVal->value.pData = pData;
855,818,548✔
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;
820,812,950✔
624
    uint8_t *fixed;
625
    uint8_t *varlen;
626
    uint8_t  bit;
627

628
    if (pRow->flag == HAS_VALUE) {
820,812,950✔
629
      fixed = bitmap;
682,729,782✔
630
      bit = BIT_FLG_VALUE;
682,729,782✔
631
    } else if (pRow->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
138,083,168!
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;
138,083,168✔
636
      bit = GET_BIT1(bitmap, iCol - 1);
138,083,168✔
637

638
      if (pRow->flag == (HAS_NONE | HAS_VALUE)) {
138,083,168✔
639
        if (bit) bit++;
20,213✔
640
      } else if (pRow->flag == (HAS_NULL | HAS_VALUE)) {
138,062,955✔
641
        bit++;
96,331,100✔
642
      }
643
    }
644
    varlen = fixed + pTSchema->flen;
820,812,950✔
645

646
    if (bit == BIT_FLG_NONE) {
820,812,950✔
647
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
7,218✔
648
      return 0;
7,218✔
649
    } else if (bit == BIT_FLG_NULL) {
820,805,732✔
650
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
10,150,705✔
651
      return 0;
10,150,705✔
652
    }
653

654
    pColVal->cid = pTColumn->colId;
810,655,027✔
655
    pColVal->value.type = pTColumn->type;
810,655,027✔
656
    pColVal->flag = CV_FLAG_VALUE;
810,655,027✔
657
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
810,655,027!
658
      pColVal->value.pData = varlen + *(int32_t *)(fixed + pTColumn->offset);
105,357,168✔
659
      pColVal->value.pData += tGetU32v(pColVal->value.pData, &pColVal->value.nData);
210,714,336✔
660
    } else {
661
      (void)memcpy(&pColVal->value.val, fixed + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
705,297,859✔
662
    }
663
  }
664

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

668
void tRowDestroy(SRow *pRow) {
642,830,834✔
669
  if (pRow) taosMemoryFree(pRow);
642,830,834!
670
}
642,821,800✔
671

672
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
257,393,652✔
673
  SRowKey key1, key2;
674
  tRowGetKey(*(SRow **)p1, &key1);
257,393,652✔
675
  tRowGetKey(*(SRow **)p2, &key2);
239,969,440✔
676
  return tRowKeyCompare(&key1, &key2);
255,040,437✔
677
}
678
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
890✔
679
static int32_t tRowMergeImpl(SArray *aRowP, STSchema *pTSchema, int32_t iStart, int32_t iEnd, int8_t flag) {
235✔
680
  int32_t code = 0;
235✔
681

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

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

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

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

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

707
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
895✔
708
    SColVal *pColVal = NULL;
660✔
709
    for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
790✔
710
      SColVal *pColValT = tRowIterNext(aIter[iRow]);
746✔
711
      while (pColValT->cid < pTSchema->columns[iCol].colId) {
838✔
712
        pColValT = tRowIterNext(aIter[iRow]);
92✔
713
      }
714

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

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

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

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

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

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

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

768
  int32_t iStart = 0;
34,150✔
769
  while (iStart < aRowP->size) {
37,090,792!
770
    SRowKey key1;
771
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
37,098,311✔
772

773
    tRowGetKey(row1, &key1);
37,698,588✔
774

775
    int32_t iEnd = iStart + 1;
37,048,747✔
776
    while (iEnd < aRowP->size) {
37,028,898✔
777
      SRowKey key2;
778
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
37,005,580✔
779
      tRowGetKey(row2, &key2);
37,374,110✔
780

781
      if (tRowKeyCompare(&key1, &key2) != 0) break;
37,013,475!
782

783
      iEnd++;
×
784
    }
785

786
    if (iEnd - iStart > 1) {
37,056,642✔
787
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, flag);
235✔
788
      if (code) return code;
235!
789
    }
790

791
    // the array is also changing, so the iStart just ++ instead of iEnd
792
    iStart++;
37,056,642✔
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,189,097✔
819
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
1,189,097!
820

821
  int32_t code = 0;
1,189,097✔
822

823
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
1,189,097!
824
  if (pIter == NULL) {
1,238,695!
825
    code = terrno;
×
826
    goto _exit;
×
827
  }
828

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

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

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

841
  if (pRow->flag >> 4) {
1,238,488✔
842
    pIter->iCol = 0;
1,232,541✔
843
    pIter->pIdx = (SKVIdx *)data;
1,232,541✔
844
    if (pRow->flag & KV_FLG_LIT) {
1,232,541✔
845
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
1,229,026✔
846
    } else if (pRow->flag & KV_FLG_MID) {
3,515!
847
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
3,600✔
848
    } else {
849
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
850
    }
851
  } else {
852
    switch (pRow->flag) {
5,947!
853
      case (HAS_NULL | HAS_NONE):
1✔
854
        pIter->pb = data;
1✔
855
        break;
1✔
856
      case HAS_VALUE:
2,245✔
857
        pIter->pf = data;
2,245✔
858
        pIter->pv = pIter->pf + pTSchema->flen;
2,245✔
859
        break;
2,245✔
860
      case (HAS_VALUE | HAS_NONE):
716✔
861
      case (HAS_VALUE | HAS_NULL):
862
        pIter->pb = data;
716✔
863
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
716✔
864
        pIter->pv = pIter->pf + pTSchema->flen;
716✔
865
        break;
716✔
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:
2,985✔
872
        break;
2,985✔
873
    }
874
  }
875

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

885
void tRowIterClose(SRowIter **ppIter) {
1,142,511✔
886
  SRowIter *pIter = *ppIter;
1,142,511✔
887
  if (pIter) {
1,142,511!
888
    taosMemoryFree(pIter);
1,142,741!
889
  }
890
  *ppIter = NULL;
1,240,724✔
891
}
1,240,724✔
892

893
SColVal *tRowIterNext(SRowIter *pIter) {
13,278,502✔
894
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
13,278,502✔
895
    return NULL;
1,143,647✔
896
  }
897

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

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

909
  if (pIter->pRow->flag == HAS_NONE) {
10,929,610✔
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,929,609✔
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,927,768!
920
    if (pIter->iCol < pIter->pIdx->nCol) {
11,668,553✔
921
      uint8_t *pData;
922

923
      if (pIter->pRow->flag & KV_FLG_LIT) {
9,774,270✔
924
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
9,678,707✔
925
      } else if (pIter->pRow->flag & KV_FLG_MID) {
95,563!
926
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
108,000✔
927
      } else {
928
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
×
929
      }
930

931
      int16_t cid;
932
      pData += tGetI16v(pData, &cid);
9,774,270✔
933

934
      if (TABS(cid) == pTColumn->colId) {
9,774,270!
935
        if (cid < 0) {
9,890,853✔
936
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,798,200✔
937
        } else {
938
          pIter->cv.cid = pTColumn->colId;
8,092,653✔
939
          pIter->cv.value.type = pTColumn->type;
8,092,653✔
940
          pIter->cv.flag = CV_FLAG_VALUE;
8,092,653✔
941

942
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
8,092,653!
943
            pData += tGetU32v(pData, &pIter->cv.value.nData);
835,428!
944
            if (pIter->cv.value.nData > 0) {
835,428!
945
              pIter->cv.value.pData = pData;
2,308,007✔
946
            } else {
947
              pIter->cv.value.pData = NULL;
×
948
            }
949
          } else {
950
            (void)memcpy(&pIter->cv.value.val, pData, pTColumn->bytes);
7,257,225✔
951
          }
952
        }
953

954
        pIter->iCol++;
9,890,853✔
955
        goto _exit;
9,890,853✔
956
      } else if (TABS(cid) > pTColumn->colId) {
×
957
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
82✔
958
        goto _exit;
82✔
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,894,283✔
965
      goto _exit;
1,894,283✔
966
    }
967
  } else {  // Tuple
968
    uint8_t bv = BIT_FLG_VALUE;
×
969
    if (pIter->pb) {
×
970
      switch (pIter->pRow->flag) {
2,996!
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):
31✔
975
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
31✔
976
          if (bv) bv++;
31✔
977
          break;
31✔
978
        case (HAS_VALUE | HAS_NULL):
2,962✔
979
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
2,962✔
980
          break;
2,962✔
981
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
982
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
983
          break;
×
984
        default:
×
985
          break;
×
986
      }
987

988
      if (bv == BIT_FLG_NONE) {
2,996✔
989
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
13✔
990
        goto _exit;
13✔
991
      } else if (bv == BIT_FLG_NULL) {
2,983✔
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,980✔
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]);
26,661✔
1010
    }
1011
    goto _exit;
×
1012
  }
1013

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

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

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

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

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

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

1039
  while (pColData) {
146,995✔
1040
    if (pTColumn) {
131,911!
1041
      if (pTColumn->colId == pColData->cid) {  // NULL
131,911!
1042
        if (flag == 0) {
131,922✔
1043
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
131,876✔
1044
        } else {
1045
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
46✔
1046
        }
1047
        if (code) goto _exit;
131,951!
1048

1049
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
131,951✔
1050
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
131,951✔
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:
15,084✔
1064
  return code;
15,084✔
1065
}
1066
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
605,326,642✔
1067
                                      int32_t flag) {
1068
  int32_t code = 0;
605,326,642✔
1069

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

1075
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
605,326,642✔
1076
  SPrimaryKeyIndex index;
1077
  uint8_t         *data = pRow->data;
605,326,642✔
1078
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
828,837,246✔
1079
    data += tGetPrimaryKeyIndex(data, &index);
223,609,713✔
1080
  }
1081

1082
  switch (pRow->flag) {
605,227,533!
1083
    case HAS_VALUE:
596,235,613✔
1084
      pf = data;  // TODO: fix here
596,235,613✔
1085
      pv = pf + pTSchema->flen;
596,235,613✔
1086
      break;
596,235,613✔
1087
    case (HAS_NULL | HAS_NONE):
1,296✔
1088
      pb = data;
1,296✔
1089
      break;
1,296✔
1090
    case (HAS_VALUE | HAS_NONE):
9,164,711✔
1091
    case (HAS_VALUE | HAS_NULL):
1092
      pb = data;
9,164,711✔
1093
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
9,164,711✔
1094
      pv = pf + pTSchema->flen;
9,164,711✔
1095
      break;
9,164,711✔
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,016,562,090✔
1107
      if (pTColumn->colId == pColData->cid) {
2,016,142,032!
1108
        if (!(pTColumn->type == pColData->type)) {
2,016,380,349!
1109
          return TSDB_CODE_INVALID_PARA;
×
1110
        }
1111
        if (pb) {
2,016,380,349✔
1112
          uint8_t bv;
1113
          switch (pRow->flag) {
62,226,225!
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,113✔
1118
              bv = GET_BIT1(pb, iTColumn - 1);
199,113✔
1119
              if (bv) bv++;
199,113✔
1120
              break;
199,113✔
1121
            case (HAS_VALUE | HAS_NULL):
62,002,296✔
1122
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
62,002,296✔
1123
              break;
62,002,296✔
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) {
62,227,329✔
1132
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
43,336!
1133
              goto _exit;
×
1134
            goto _continue;
43,332✔
1135
          } else if (bv == BIT_FLG_NULL) {
62,183,993✔
1136
            if (flag == 0) {
10,264,942✔
1137
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
10,094,001✔
1138
            } else {
1139
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
170,941✔
1140
            }
1141
            if (code) goto _exit;
10,265,839!
1142
            goto _continue;
10,265,839✔
1143
          }
1144
        }
1145

1146
        if (IS_VAR_DATA_TYPE(pColData->type)) {
2,062,494,191!
1147
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
52,526,306!
1148
          uint32_t nData;
1149
          pData += tGetU32v(pData, &nData);
52,526,306✔
1150
          if (flag == 0) {
52,526,306!
1151
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
55,436,984✔
1152
          } else {
1153
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
1154
          }
1155
          if (code) goto _exit;
56,421,016!
1156
        } else {
1157
          if (flag == 0) {
1,953,546,869✔
1158
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
1,950,573,173✔
1159
                                                                          TYPE_BYTES[pColData->type]);
1,950,573,173✔
1160
          } else {
1161
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,973,696✔
1162
                                                                          TYPE_BYTES[pColData->type], flag > 0);
2,973,696✔
1163
          }
1164
          if (code) goto _exit;
1,953,198,314!
1165
        }
1166

1167
      _continue:
1,953,198,314✔
1168
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,019,928,501✔
1169
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,019,928,501✔
1170
      } else if (pTColumn->colId > pColData->cid) {  // NONE
×
1171
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
1172
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
1173
      } else {
1174
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
×
1175
      }
1176
    } else {
1177
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
420,058!
1178
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
420,048✔
1179
    }
1180
  }
1181

1182
_exit:
608,949,762✔
1183
  return code;
608,949,762✔
1184
}
1185
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
3,142,613✔
1186
  int32_t code = 0;
3,142,613✔
1187

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

1195
  // primary keys
1196
  uint8_t         *data = pRow->data;
3,142,613✔
1197
  SPrimaryKeyIndex index;
1198
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
3,291,450✔
1199
    data += tGetPrimaryKeyIndex(data, &index);
148,839✔
1200
  }
1201

1202
  SKVIdx *pKVIdx = (SKVIdx *)data;
3,142,611✔
1203
  if (pRow->flag & KV_FLG_LIT) {
3,142,611✔
1204
    pv = pKVIdx->idx + pKVIdx->nCol;
3,080,603✔
1205
  } else if (pRow->flag & KV_FLG_MID) {
62,008!
1206
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
62,883✔
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) {
38,141,068✔
1214
    if (pTColumn) {
35,091,549✔
1215
      if (pTColumn->colId == pColData->cid) {
34,692,329✔
1216
        while (iCol < pKVIdx->nCol) {
34,628,916✔
1217
          uint8_t *pData;
1218
          if (pRow->flag & KV_FLG_LIT) {
29,466,665✔
1219
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
27,753,928✔
1220
          } else if (pRow->flag & KV_FLG_MID) {
1,712,737!
1221
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
1,712,771✔
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);
29,466,699✔
1230

1231
          if (TABS(cid) == pTColumn->colId) {
29,466,699✔
1232
            if (cid < 0) {
29,067,951✔
1233
              if (flag == 0) {
5,372,809✔
1234
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
5,369,278✔
1235
              } else {
1236
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
3,531✔
1237
              }
1238
              if (code) goto _exit;
4,898,191!
1239
            } else {
1240
              uint32_t nData;
1241
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
23,695,142!
1242
                pData += tGetU32v(pData, &nData);
5,992,688✔
1243
              } else {
1244
                nData = 0;
17,702,454✔
1245
              }
1246
              if (flag == 0) {
23,695,142!
1247
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
23,898,349✔
1248
              } else {
1249
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
1250
              }
1251
              if (code) goto _exit;
24,076,177!
1252
            }
1253
            iCol++;
28,974,368✔
1254
            goto _continue;
28,974,368✔
1255
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
398,748✔
1256
            break;
39,210✔
1257
          } else {
1258
            iCol++;
359,538✔
1259
          }
1260
        }
1261

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

1264
      _continue:
5,201,056✔
1265
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
34,175,424✔
1266
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
34,175,424✔
1267
      } else if (pTColumn->colId > pColData->cid) {
422,951!
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;
422,951!
1272
      }
1273
    } else {
1274
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
399,220!
1275
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
399,207✔
1276
    }
1277
  }
1278

1279
_exit:
3,049,519✔
1280
  return code;
3,049,519✔
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) {
608,570,282✔
1287
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
608,570,282!
1288
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
608,570,282!
1289

1290
  if (pRow->flag == HAS_NONE) {
608,570,282✔
1291
    return tRowNoneUpsertColData(aColData, nColData, flag);
1,069✔
1292
  } else if (pRow->flag == HAS_NULL) {
608,569,213✔
1293
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
15,056✔
1294
  } else if (pRow->flag >> 4) {  // KV row
608,554,157✔
1295
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
3,146,188✔
1296
  } else {  // TUPLE row
1297
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
605,407,969✔
1298
  }
1299
}
1300

1301
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
2,120,022,986✔
1302
  key->numOfPKs = row->numOfPKs;
2,120,022,986✔
1303

1304
  if (key->numOfPKs == 0) {
2,120,022,986!
1305
    return;
×
1306
  }
1307

1308
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
1309

1310
  uint8_t *data = row->data;
2,120,022,986✔
1311

1312
  for (int32_t i = 0; i < row->numOfPKs; i++) {
2,147,483,647✔
1313
    data += tGetPrimaryKeyIndex(data, &indices[i]);
2,129,490,460✔
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,132,359,429✔
1319

1320
    uint8_t *tdata = data + indices[i].offset;
2,132,359,429✔
1321
    if (row->flag >> 4) {
2,132,359,429✔
1322
      tdata += tGetI16v(tdata, NULL);
3,037,834✔
1323
    }
1324

1325
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
2,132,359,429!
1326
      key->pks[i].pData = tdata;
881✔
1327
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
1,762!
1328
    } else {
1329
      (void)memcpy(&key->pks[i].val, tdata, tDataTypes[indices[i].type].bytes);
2,132,358,548✔
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) {
43,417,530✔
1346
  switch (tv1->type) {
43,417,530!
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:
953,568✔
1353
      T_COMPARE_SCALAR_VALUE(int32_t, &tv1->val, &tv2->val);
953,568✔
1354
    case TSDB_DATA_TYPE_BIGINT:
42,461,390✔
1355
    case TSDB_DATA_TYPE_TIMESTAMP:
1356
      T_COMPARE_SCALAR_VALUE(int64_t, &tv1->val, &tv2->val);
42,461,390✔
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:
269✔
1370
    case TSDB_DATA_TYPE_BINARY: {
1371
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
269✔
1372
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
269✔
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:
2,303✔
1384
      break;
2,303✔
1385
  }
1386

1387
  return 0;
2,303✔
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) {
45,436,188!
1401
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
65,130,639!
1402
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
43,409,040✔
1403
      if (ret) return ret;
43,394,768!
1404
    }
1405
  } else if (key1->numOfPKs < key2->numOfPKs) {
×
1406
    return -1;
×
1407
  } else {
1408
    return 1;
×
1409
  }
1410

1411
  return 0;
21,721,599✔
1412
}
1413

1414
void tRowKeyAssign(SRowKey *pDst, SRowKey *pSrc) {
790,561,589✔
1415
  pDst->ts = pSrc->ts;
790,561,589✔
1416
  pDst->numOfPKs = pSrc->numOfPKs;
790,561,589✔
1417

1418
  if (pSrc->numOfPKs > 0) {
790,561,589✔
1419
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
52,712,732✔
1420
      SValue *pVal = &pDst->pks[i];
26,440,143✔
1421
      pVal->type = pSrc->pks[i].type;
26,440,143✔
1422

1423
      if (IS_NUMERIC_TYPE(pVal->type)) {
26,440,143!
1424
        pVal->val = pSrc->pks[i].val;
26,440,133✔
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
}
790,561,589✔
1432

1433
// STag ========================================
1434
static int tTagValCmprFn(const void *p1, const void *p2) {
123,676,528✔
1435
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
123,676,528✔
1436
    return -1;
41,665,757✔
1437
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
82,010,771✔
1438
    return 1;
45,590,142✔
1439
  }
1440

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

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

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

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

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

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

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

1553
  // value
1554
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
1,395,539✔
1555
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
662,872✔
1556
  } else {
1557
    p = p ? p + n : p;
1,064,103✔
1558
    n += tDataTypes[pTagVal->type].bytes;
1,064,103✔
1559
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
1,064,103✔
1560
  }
1561

1562
  return n;
1,395,539✔
1563
}
1564
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
121,678,280✔
1565
  int32_t n = 0;
121,678,280✔
1566

1567
  // key
1568
  if (isJson) {
121,678,280✔
1569
    n += tGetCStr(p + n, &pTagVal->pKey);
26,600!
1570
  } else {
1571
    n += tGetI16v(p + n, &pTagVal->cid);
243,329,960!
1572
  }
1573

1574
  // type
1575
  n += tGetI8(p + n, &pTagVal->type);
121,678,280!
1576

1577
  // value
1578
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
121,678,280!
1579
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
37,808,196!
1580
  } else {
1581
    (void)memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
102,774,182✔
1582
    n += tDataTypes[pTagVal->type].bytes;
102,774,182✔
1583
  }
1584

1585
  return n;
121,678,280✔
1586
}
1587

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

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

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

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

1612
  // get size
1613
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
884,791✔
1614
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
697,785✔
1615
  }
1616
  if (szTag <= INT8_MAX) {
187,006✔
1617
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
172,940✔
1618
  } else {
1619
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
14,066✔
1620
    isLarge = 1;
14,066✔
1621
  }
1622

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

1640
  if (isLarge) {
187,059✔
1641
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
14,073✔
1642
  } else {
1643
    p = (uint8_t *)&(*ppTag)->idx[nTag];
172,986✔
1644
  }
1645
  n = 0;
187,059✔
1646
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
884,918✔
1647
    if (isLarge) {
697,884✔
1648
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
75,065✔
1649
    } else {
1650
      (*ppTag)->idx[iTag] = n;
622,819✔
1651
    }
1652
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
697,884✔
1653
  }
1654
#ifdef TD_DEBUG_PRINT_TAG
1655
  debugPrintSTag(*ppTag, __func__, __LINE__);
1656
#endif
1657

1658
  return code;
187,034✔
1659

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

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

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

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

1679
  if (IS_VAR_DATA_TYPE(value->type)) {
11,022,721✔
1680
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
2,582,471!
1681
    if (data == NULL) {
2,583,479!
1682
      return NULL;
×
1683
    }
1684

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

1689
    varDataLen(data + typeBytes) = value->nData;
2,583,479✔
1690
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
2,583,479✔
1691
  } else {
1692
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
8,440,250✔
1693
  }
1694

1695
  return data;
11,023,729✔
1696
}
1697

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

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

1713
  if (isLarge) {
37,773,843✔
1714
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
30,759,953✔
1715
  } else {
1716
    p = (uint8_t *)&pTag->idx[pTag->nTag];
7,013,890✔
1717
  }
1718

1719
  pTagVal->type = TSDB_DATA_TYPE_NULL;
37,773,843✔
1720
  pTagVal->pData = NULL;
37,773,843✔
1721
  pTagVal->nData = 0;
37,773,843✔
1722
  while (lidx <= ridx) {
123,915,238✔
1723
    midx = (lidx + ridx) / 2;
122,136,959✔
1724
    if (isLarge) {
122,136,959✔
1725
      offset = ((int16_t *)pTag->idx)[midx];
99,310,392✔
1726
    } else {
1727
      offset = pTag->idx[midx];
22,826,567✔
1728
    }
1729

1730
    int32_t nt = tGetTagVal(p + offset, &tv, isJson);
122,136,959✔
1731
    if (isJson) {
122,794,795✔
1732
      c = tTagValJsonCmprFn(pTagVal, &tv);
11,388✔
1733
    } else {
1734
      c = tTagValCmprFn(pTagVal, &tv);
122,783,407✔
1735
    }
1736

1737
    if (c < 0) {
124,073,462✔
1738
      ridx = midx - 1;
40,875,855✔
1739
    } else if (c > 0) {
83,197,607✔
1740
      lidx = midx + 1;
45,265,540✔
1741
    } else {
1742
      (void)memcpy(pTagVal, &tv, sizeof(tv));
37,932,067✔
1743
      return true;
37,932,067✔
1744
    }
1745
  }
1746
  return false;
1,778,279✔
1747
}
1748

1749
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
811,426✔
1750
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
1,622,852✔
1751
}
1752

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

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

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

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

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

1787
  return code;
1,448✔
1788

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

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

1801
  pTSchema->numOfCols = numOfCols;
15,668,835✔
1802
  pTSchema->version = version;
15,668,835✔
1803

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

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

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

1829
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
167,514,237!
1830
      pTColumn->bytes = pSchema->bytes;
33,246,145✔
1831
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
33,246,145✔
1832
    } else {
1833
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
134,268,092✔
1834
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
134,268,092✔
1835
    }
1836

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

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

1844
  return pTSchema;
15,668,835✔
1845
}
1846

1847
static int32_t tTColumnCompare(const void *p1, const void *p2) {
4,384,116✔
1848
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
4,384,116✔
1849
    return -1;
653,802✔
1850
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
3,730,314✔
1851
    return 1;
2,633,854✔
1852
  }
1853

1854
  return 0;
1,096,460✔
1855
}
1856

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

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

1865
// SColData ========================================
1866
void tColDataDestroy(void *ph) {
18,297,653✔
1867
  if (ph) {
18,297,653!
1868
    SColData *pColData = (SColData *)ph;
18,297,788✔
1869

1870
    tFree(pColData->pBitMap);
18,297,788!
1871
    tFree(pColData->aOffset);
18,297,804!
1872
    tFree(pColData->pData);
18,297,816!
1873
  }
1874
}
18,299,383✔
1875

1876
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
17,359,415✔
1877
  pColData->cid = cid;
17,359,415✔
1878
  pColData->type = type;
17,359,415✔
1879
  pColData->cflag = cflag;
17,359,415✔
1880
  tColDataClear(pColData);
17,359,415✔
1881
}
17,361,012✔
1882

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

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

1897
  tColDataClear(pColData);
823,769✔
1898
}
823,768✔
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);
97,283,306!
1905
    if (code) goto _exit;
103,776,322!
1906
    pColData->aOffset[pColData->nVal] = pColData->nData;
103,776,322✔
1907

1908
    if (nData) {
103,776,322!
1909
      code = tRealloc(&pColData->pData, pColData->nData + nData);
101,136,408!
1910
      if (code) goto _exit;
101,625,477!
1911
      (void)memcpy(pColData->pData + pColData->nData, pData, nData);
101,625,477✔
1912
      pColData->nData += nData;
101,625,477✔
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]);
20,720,018✔
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,315,939✔
1933
  pColData->flag = HAS_VALUE;
3,316,015✔
1934
  pColData->numOfValue++;
3,315,939✔
1935
  return tColDataPutValue(pColData, pData, nData);
3,316,190✔
1936
}
1937
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
174,291✔
1938
  pColData->flag = HAS_NONE;
174,291✔
1939
  pColData->numOfNone++;
174,291✔
1940
  pColData->nVal++;
174,291✔
1941
  return 0;
174,291✔
1942
}
1943
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
157,899✔
1944
  pColData->flag = HAS_NULL;
157,990✔
1945
  pColData->numOfNull++;
157,990✔
1946
  pColData->nVal++;
157,990✔
1947
  return 0;
157,899✔
1948
}
1949
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,307✔
1950
  int32_t code = 0;
1,307✔
1951

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

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

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

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

1976
  return tColDataPutValue(pColData, pData, nData);
1,309✔
1977
}
1978
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
11,183,024✔
1979
  pColData->nVal++;
11,183,024✔
1980
  pColData->numOfNone++;
11,183,024✔
1981
  return 0;
11,183,024✔
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) {
20,167✔
2000
  int32_t code = 0;
20,171✔
2001

2002
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
20,171✔
2003
  code = tRealloc(&pColData->pBitMap, nBit);
20,170!
2004
  if (code) return code;
20,178!
2005

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

2009
  pColData->flag |= HAS_VALUE;
20,178✔
2010
  pColData->numOfValue++;
20,178✔
2011

2012
  if (pColData->nVal) {
20,178!
2013
    if (IS_VAR_DATA_TYPE(pColData->type)) {
20,182!
2014
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
8,569✔
2015
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
8,569!
2016
      if (code) return code;
8,572!
2017
      memset(pColData->aOffset, 0, nOffset);
8,572✔
2018
    } else {
2019
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
11,613✔
2020
      code = tRealloc(&pColData->pData, pColData->nData);
11,613!
2021
      if (code) return code;
11,604!
2022
      memset(pColData->pData, 0, pColData->nData);
11,604✔
2023
    }
2024
  }
2025

2026
  return tColDataPutValue(pColData, pData, nData);
20,176✔
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,405,792✔
2045
  pColData->nVal++;
4,405,792✔
2046
  pColData->numOfNull++;
4,405,792✔
2047
  return 0;
4,405,792✔
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,388✔
2096
  int32_t code = 0;
9,388✔
2097

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

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

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

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

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

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

2124
  return tColDataPutValue(pColData, NULL, 0);
6,055✔
2125
}
2126
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
260,574✔
2127
  int32_t code = 0;
266,652✔
2128

2129
  pColData->flag |= HAS_NULL;
266,652✔
2130
  pColData->numOfNull++;
266,652✔
2131

2132
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
266,652✔
2133
  code = tRealloc(&pColData->pBitMap, nBit);
266,348✔
2134
  if (code) return code;
266,660!
2135

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

2139
  return tColDataPutValue(pColData, NULL, 0);
266,663✔
2140
}
2141
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
52,176✔
2142
  int32_t code = 0;
54,177✔
2143

2144
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
54,176!
2145
  if (code) return code;
54,286!
2146

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

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

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

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

2161
  return tColDataPutValue(pColData, NULL, 0);
102,162✔
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) {
337,220,012✔
2184
  int32_t code = 0;
337,385,347✔
2185

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

2191
  return tColDataPutValue(pColData, pData, nData);
337,558,489✔
2192
}
2193
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,996✔
2194
  int32_t code = 0;
3,996✔
2195

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

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

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

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

2211
  return tColDataPutValue(pColData, NULL, 0);
3,994✔
2212
}
2213
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
19,356,925✔
2214
  int32_t code = 0;
19,533,411✔
2215

2216
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
19,529,817!
2217
  if (code) return code;
19,536,165!
2218
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
19,536,165✔
2219
  pColData->numOfNull++;
19,536,165✔
2220

2221
  return tColDataPutValue(pColData, NULL, 0);
19,540,182✔
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,103,400,314✔
2266
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) {
2,103,400,314!
2267
    return TSDB_CODE_INVALID_PARA;
×
2268
  }
2269
  return tColDataAppendValueImpl[pColData->flag][pColVal->flag](
2,103,628,422✔
2270
      pColData, IS_VAR_DATA_TYPE(pColData->type) ? pColVal->value.pData : (uint8_t *)&pColVal->value.val,
2,103,628,422!
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) {
70✔
2296
  if (forward) {
70!
2297
    pColData->numOfNull--;
70✔
2298
    pColData->nVal--;
70✔
2299
    if (pColData->numOfNull) {
70✔
2300
      return tColDataAppendValue20(pColData, pData, nData);
3✔
2301
    } else {
2302
      pColData->flag = 0;
67✔
2303
      return tColDataAppendValue00(pColData, pData, nData);
67✔
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) {
5,203,448✔
2343
  if (forward) {  // VALUE ==> VALUE
5,203,448!
2344
    pColData->nVal--;
5,203,539✔
2345
    if (IS_VAR_DATA_TYPE(pColData->type)) {
5,203,539!
2346
      pColData->nData = pColData->aOffset[pColData->nVal];
597,367✔
2347
    } else {
2348
      pColData->nData -= TYPE_BYTES[pColData->type];
4,606,172✔
2349
    }
2350
    return tColDataPutValue(pColData, pData, nData);
5,221,750✔
2351
  }
2352
  return 0;
×
2353
}
2354
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,841✔
2355
  if (forward) {  // VALUE ==> NULL
5,841!
2356
    pColData->numOfValue--;
5,841✔
2357
    pColData->nVal--;
5,841✔
2358
    if (pColData->numOfValue) {
5,841✔
2359
      if (IS_VAR_DATA_TYPE(pColData->type)) {
5,774!
2360
        pColData->nData = pColData->aOffset[pColData->nVal];
1,129✔
2361
      } else {
2362
        pColData->nData -= TYPE_BYTES[pColData->type];
4,645✔
2363
      }
2364
      return tColDataAppendValue42(pColData, pData, nData);
5,776✔
2365
    } else {
2366
      pColData->flag = 0;
67✔
2367
      pColData->nData = 0;
67✔
2368
      return tColDataAppendValue02(pColData, pData, nData);
67✔
2369
    }
2370
  }
2371
  return 0;
×
2372
}
2373
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,601✔
2374
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
5,601✔
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,476✔
2379
    }
2380
    if (pColData->numOfNone) {
5,593✔
2381
      return tColDataAppendValue50(pColData, pData, nData);
2,000✔
2382
    } else {
2383
      pColData->flag = HAS_VALUE;
3,593✔
2384
      return tColDataAppendValue40(pColData, pData, nData);
3,598✔
2385
    }
2386
  } else if (forward) {  // VALUE ==> VALUE
8!
2387
    pColData->nVal--;
9✔
2388
    if (IS_VAR_DATA_TYPE(pColData->type)) {
9!
2389
      pColData->nData = pColData->aOffset[pColData->nVal];
8✔
2390
    } else {
2391
      pColData->nData -= TYPE_BYTES[pColData->type];
1✔
2392
    }
2393
    return tColDataPutValue(pColData, pData, nData);
9✔
2394
  }
2395
  return 0;
×
2396
}
2397
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
304✔
2398
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
304!
2399
    pColData->numOfNone--;
304✔
2400
    pColData->nVal--;
304✔
2401
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
304!
2402
      pColData->nData -= TYPE_BYTES[pColData->type];
244✔
2403
    }
2404
    if (pColData->numOfNone) {
304!
2405
      return tColDataAppendValue52(pColData, pData, nData);
×
2406
    } else {
2407
      pColData->flag = HAS_VALUE;
304✔
2408
      return tColDataAppendValue42(pColData, pData, nData);
304✔
2409
    }
2410
  } else if (forward) {  // VALUE ==> NULL
×
2411
    pColData->numOfValue--;
×
2412
    pColData->nVal--;
×
2413
    if (pColData->numOfValue) {
×
2414
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
2415
        pColData->nData = pColData->aOffset[pColData->nVal];
×
2416
      } else {
2417
        pColData->nData -= TYPE_BYTES[pColData->type];
×
2418
      }
2419
      return tColDataAppendValue52(pColData, pData, nData);
×
2420
    } else {
2421
      pColData->flag = HAS_NONE;
×
2422
      pColData->nData = 0;
×
2423
      return tColDataAppendValue12(pColData, pData, nData);
×
2424
    }
2425
  }
2426
  return 0;
×
2427
}
2428
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,290,478✔
2429
  if (forward) {
3,290,478!
2430
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,290,478✔
2431
      pColData->numOfNull--;
184,687✔
2432
      pColData->nVal--;
184,687✔
2433
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
184,687!
2434
        pColData->nData -= TYPE_BYTES[pColData->type];
154,943✔
2435
      }
2436
      if (pColData->numOfNull) {
184,687✔
2437
        return tColDataAppendValue60(pColData, pData, nData);
164,936✔
2438
      } else {
2439
        pColData->flag = HAS_VALUE;
19,752✔
2440
        return tColDataAppendValue40(pColData, pData, nData);
19,754✔
2441
      }
2442
    } else {  // VALUE ==> VALUE
2443
      pColData->nVal--;
3,105,791✔
2444
      if (IS_VAR_DATA_TYPE(pColData->type)) {
3,105,791!
2445
        pColData->nData = pColData->aOffset[pColData->nVal];
479,270✔
2446
      } else {
2447
        pColData->nData -= TYPE_BYTES[pColData->type];
2,626,521✔
2448
      }
2449
      return tColDataPutValue(pColData, pData, nData);
3,105,793✔
2450
    }
2451
  }
2452
  return 0;
×
2453
}
2454
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
196,077✔
2455
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
196,077!
2456
    pColData->numOfValue--;
172,893✔
2457
    pColData->nVal--;
172,893✔
2458
    if (pColData->numOfValue) {
172,893✔
2459
      if (IS_VAR_DATA_TYPE(pColData->type)) {
172,892!
2460
        pColData->nData = pColData->aOffset[pColData->nVal];
27,373✔
2461
      } else {
2462
        pColData->nData -= TYPE_BYTES[pColData->type];
145,519✔
2463
      }
2464
      return tColDataAppendValue62(pColData, pData, nData);
172,890✔
2465
    } else {
2466
      pColData->flag = HAS_NULL;
1✔
2467
      pColData->nData = 0;
1!
2468
      return tColDataAppendValue20(pColData, pData, nData);
2✔
2469
    }
2470
  }
2471
  return 0;
23,184✔
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,594✔
2528
    pColData->nVal--;
3,594✔
2529
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
3,594!
2530
      pColData->nData -= TYPE_BYTES[pColData->type];
2,542✔
2531
    }
2532
    if (pColData->numOfNone) {
3,594!
2533
      return tColDataAppendValue72(pColData, pData, nData);
×
2534
    } else {
2535
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
114,227✔
2536
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
110,633✔
2537
      }
2538
      pColData->flag = (HAS_VALUE | HAS_NULL);
3,594!
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) {
13,453✔
2563
  return 0;
13,453✔
2564
}
2565
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
2566
    {NULL, NULL, NULL},                                                     // 0
2567
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
2568
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
2569
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
2570
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
2571
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
2572
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
2573
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
2574

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

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

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

2588
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
40,325,519✔
2589
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
40,325,519✔
2590
}
40,325,519✔
2591
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,383,620✔
2592
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,383,620✔
2593
}
1,383,620✔
2594
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
11,264✔
2595
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
2596
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
11,264!
2597
    case 0:
1,023✔
2598
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,023✔
2599
      break;
1,023✔
2600
    case 1:
10,243✔
2601
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
10,243✔
2602
      break;
10,243✔
2603
    default:
×
2604
      break;
×
2605
  }
2606
}
11,264✔
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) {
228,790,948!
2611
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
238,925,336✔
2612
    } else {
2613
      value.nData = pColData->nData - pColData->aOffset[iVal];
×
2614
    }
2615
    value.pData = pColData->pData + pColData->aOffset[iVal];
228,790,948✔
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
}
199,102,931✔
2622
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
223,478✔
2623
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
2624
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
223,478!
2625
    case 0:
102,083✔
2626
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
102,083✔
2627
      break;
102,083✔
2628
    case 1:
121,692✔
2629
      tColDataGetValue4(pColData, iVal, pColVal);
2630
      break;
121,692✔
2631
    default:
×
2632
      break;
×
2633
  }
2634
}
223,478✔
2635
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
210,357,389✔
2636
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
2637
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
210,357,389!
2638
    case 0:
11,519,385✔
2639
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
11,519,385✔
2640
      break;
11,519,385✔
2641
    case 1:
198,981,238✔
2642
      tColDataGetValue4(pColData, iVal, pColVal);
2643
      break;
198,981,238✔
2644
    default:
×
2645
      break;
×
2646
  }
2647
}
210,357,389✔
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) {
313,648,711✔
2684
  switch (pColData->flag) {
313,648,711!
2685
    case HAS_NONE:
×
2686
      return 0;
×
2687
    case HAS_NULL:
×
2688
      return 1;
×
2689
    case (HAS_NULL | HAS_NONE):
11,275✔
2690
      return GET_BIT1(pColData->pBitMap, iVal);
11,275✔
2691
    case HAS_VALUE:
×
2692
      return 2;
×
2693
    case (HAS_VALUE | HAS_NONE):
606,982✔
2694
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
606,982✔
2695
    case (HAS_VALUE | HAS_NULL):
313,072,705✔
2696
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
313,072,705✔
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) {
445✔
2705
  int32_t code = 0;
445✔
2706

2707
  *pColData = *pColDataFrom;
445✔
2708

2709
  // bitmap
2710
  switch (pColData->flag) {
445!
2711
    case (HAS_NULL | HAS_NONE):
12✔
2712
    case (HAS_VALUE | HAS_NONE):
2713
    case (HAS_VALUE | HAS_NULL):
2714
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
12✔
2715
      if (pColData->pBitMap == NULL) {
12!
2716
        code = TSDB_CODE_OUT_OF_MEMORY;
×
2717
        goto _exit;
×
2718
      }
2719
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
12✔
2720
      break;
12✔
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:
433✔
2730
      pColData->pBitMap = NULL;
433✔
2731
      break;
433✔
2732
  }
2733

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

2746
  // value
2747
  if (pColData->nData) {
445✔
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;
86✔
2757
  }
2758

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

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

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

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

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

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

2789
  // bitmap
2790
  if (colData->flag != HAS_VALUE) {
2,295,695✔
2791
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
241,003✔
2792
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1✔
2793
    } else {
2794
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
241,002✔
2795
    }
2796

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

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

2809
    info->bitmapCompressedSize = cinfo.compressedSize;
241,037✔
2810
  }
2811

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

2817
  // offset
2818
  if (IS_VAR_DATA_TYPE(colData->type)) {
2,295,128!
2819
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
323,839✔
2820

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

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

2833
    info->offsetCompressedSize = cinfo.compressedSize;
323,876✔
2834
  }
2835

2836
  // data
2837
  if (colData->nData > 0) {
2,295,165✔
2838
    info->dataOriginalSize = colData->nData;
2,295,046✔
2839

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

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

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

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

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

2864
  tBufferInit(&local);
2865
  if (assist == NULL) {
14,857,580!
2866
    assist = &local;
×
2867
  }
2868

2869
  tColDataClear(colData);
14,857,580✔
2870
  colData->cid = info->columnId;
14,857,391✔
2871
  colData->type = info->dataType;
14,857,391✔
2872
  colData->cflag = info->columnFlag;
14,857,391✔
2873
  colData->nVal = info->numOfData;
14,857,391✔
2874
  colData->flag = info->flag;
14,857,391✔
2875

2876
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
14,857,391✔
2877
    goto _exit;
1,184,949✔
2878
  }
2879

2880
  // bitmap
2881
  if (info->bitmapOriginalSize > 0) {
13,672,442✔
2882
    SCompressInfo cinfo = {
665,597✔
2883
        .dataType = TSDB_DATA_TYPE_TINYINT,
2884
        .cmprAlg = info->cmprAlg,
665,597✔
2885
        .originalSize = info->bitmapOriginalSize,
665,597✔
2886
        .compressedSize = info->bitmapCompressedSize,
665,597✔
2887
    };
2888

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

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

2901
    data += cinfo.compressedSize;
665,533✔
2902
  }
2903

2904
  if (info->flag == (HAS_NONE | HAS_NULL)) {
13,672,378✔
2905
    goto _exit;
605✔
2906
  }
2907

2908
  // offset
2909
  if (info->offsetOriginalSize > 0) {
13,671,773✔
2910
    SCompressInfo cinfo = {
2,582,702✔
2911
        .cmprAlg = info->cmprAlg,
2,582,702✔
2912
        .dataType = TSDB_DATA_TYPE_INT,
2913
        .originalSize = info->offsetOriginalSize,
2,582,702✔
2914
        .compressedSize = info->offsetCompressedSize,
2,582,702✔
2915
    };
2916

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

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

2929
    data += cinfo.compressedSize;
2,582,720✔
2930
  }
2931

2932
  // data
2933
  if (info->dataOriginalSize > 0) {
13,671,791✔
2934
    colData->nData = info->dataOriginalSize;
13,670,269✔
2935

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

2943
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
13,670,269!
2944
    if (code) {
13,671,044!
2945
      tBufferDestroy(&local);
2946
      return code;
×
2947
    }
2948

2949
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
13,671,044✔
2950
    if (code) {
13,670,372!
2951
      tBufferDestroy(&local);
2952
      return code;
×
2953
    }
2954

2955
    data += cinfo.compressedSize;
13,670,372✔
2956
  }
2957

2958
_exit:
1,522✔
2959
  switch (colData->flag) {
14,857,448✔
2960
    case HAS_NONE:
1,096,681✔
2961
      colData->numOfNone = colData->nVal;
1,096,681✔
2962
      break;
1,096,681✔
2963
    case HAS_NULL:
89,434✔
2964
      colData->numOfNull = colData->nVal;
89,434✔
2965
      break;
89,434✔
2966
    case HAS_VALUE:
13,005,179✔
2967
      colData->numOfValue = colData->nVal;
13,005,179✔
2968
      break;
13,005,179✔
2969
    default:
666,154✔
2970
      for (int32_t i = 0; i < colData->nVal; i++) {
196,579,316✔
2971
        uint8_t bitValue = tColDataGetBitValue(colData, i);
195,915,405✔
2972
        if (bitValue == 0) {
195,913,162✔
2973
          colData->numOfNone++;
195,308✔
2974
        } else if (bitValue == 1) {
195,717,854✔
2975
          colData->numOfNull++;
11,055,726✔
2976
        } else {
2977
          colData->numOfValue++;
184,662,128✔
2978
        }
2979
      }
2980
  }
2981
  tBufferDestroy(&local);
2982
  return 0;
14,855,205✔
2983
}
2984

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

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

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

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

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

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

3073
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
2,249,074!
3074
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
279,133✔
3075
      code = igeos();
2✔
3076
      if (code) {
2!
3077
        return code;
×
3078
      }
3079
    }
3080
    for (int32_t i = 0; i < pBind->num; ++i) {
832,253✔
3081
      if (pBind->is_null && pBind->is_null[i]) {
553,132✔
3082
        if (pColData->cflag & COL_IS_KEY) {
2,650!
3083
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3084
          goto _exit;
×
3085
        }
3086
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2,650✔
3087
        if (code) goto _exit;
2,650!
3088
      } else if (pBind->length[i] > buffMaxLen) {
550,482✔
3089
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
9✔
3090
      } else {
3091
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
550,473✔
3092
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
4✔
3093
          if (code) goto _exit;
4!
3094
        }
3095
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
550,473✔
3096
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
550,473✔
3097
      }
3098
    }
3099
  } else {  // fixed-length data type
3100
    bool allValue;
3101
    bool allNull;
3102
    if (pBind->is_null) {
1,969,941✔
3103
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
15,778✔
3104
      allNull = (same && pBind->is_null[0] != 0);
15,778✔
3105
      allValue = (same && pBind->is_null[0] == 0);
15,778✔
3106
    } else {
3107
      allNull = false;
1,954,163✔
3108
      allValue = true;
1,954,163✔
3109
    }
3110

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

3116
    if (allValue) {
1,969,941✔
3117
      // optimize (todo)
3118
      for (int32_t i = 0; i < pBind->num; ++i) {
5,858,660✔
3119
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
3,904,118✔
3120
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
3,904,118✔
3121
      }
3122
    } else if (allNull) {
15,404✔
3123
      // optimize (todo)
3124
      for (int32_t i = 0; i < pBind->num; ++i) {
2✔
3125
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1✔
3126
        if (code) goto _exit;
1!
3127
      }
3128
    } else {
3129
      for (int32_t i = 0; i < pBind->num; ++i) {
47,017✔
3130
        if (pBind->is_null[i]) {
31,614✔
3131
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
15,807✔
3132
          if (code) goto _exit;
15,807!
3133
        } else {
3134
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
15,807✔
3135
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
15,807✔
3136
        }
3137
      }
3138
    }
3139
  }
3140

3141
_exit:
15,403✔
3142
  return code;
2,249,067✔
3143
}
3144

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3422
  return code;
294✔
3423
}
3424

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

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

3436
  return code;
60✔
3437
}
3438

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

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

3454
  return code;
60✔
3455
}
3456

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

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

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

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

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

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

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

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

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

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

3519
  return TSDB_CODE_SUCCESS;
17✔
3520
}
3521

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3688
_exit:
90,596✔
3689
  return 0;
92,297✔
3690
}
3691

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

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

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

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

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

3725
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
208,456✔
3726
      if (code) return code;
208,456!
3727
    } else {
3728
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
1,396,591✔
3729
      if (code) return code;
1,396,591!
3730
    }
3731
  }
3732

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

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

3739
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
3,983!
3740
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
3,981!
3741
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
3,980!
3742
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
3,979!
3743

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

3748
  // bitmap
3749
  switch (pColData->flag) {
1,989!
3750
    case (HAS_NULL | HAS_NONE):
48✔
3751
    case (HAS_VALUE | HAS_NONE):
3752
    case (HAS_VALUE | HAS_NULL):
3753
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
48!
3754
      if (code) return code;
48!
3755
      break;
48✔
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,941✔
3761
      break;
1,941✔
3762
  }
3763

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

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

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

3783
  return code;
1,993✔
3784
}
3785

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

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

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

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

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

3827
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
729,656,569!
3828
    return TSDB_CODE_OUT_OF_RANGE;
×
3829
  }
3830

3831
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
729,656,569✔
3832
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
1,459,313,138!
3833
}
3834

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

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

3849
  int8_t val;
3850
  if (HAS_VALUE == pColData->flag) {
7,093✔
3851
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,247,536✔
3852
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
4,241,910✔
3853
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,241,910✔
3854
    }
3855
  } else {
3856
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,157✔
3857
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,690!
3858
        case 0:
236,784✔
3859
        case 1:
3860
          (*numOfNull)++;
236,784✔
3861
          break;
236,784✔
3862
        case 2:
4,497,906✔
3863
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
4,497,906✔
3864
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,497,906✔
3865
          break;
4,497,906✔
3866
        default:
×
3867
          break;
×
3868
      }
3869
    }
3870
  }
3871
}
7,093✔
3872

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

3880
  int8_t val;
3881
  if (HAS_VALUE == pColData->flag) {
7,129✔
3882
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,366,888✔
3883
      val = ((int8_t *)pColData->pData)[iVal];
4,361,226✔
3884
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,361,226✔
3885
    }
3886
  } else {
3887
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,157✔
3888
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,690!
3889
        case 0:
235,823✔
3890
        case 1:
3891
          (*numOfNull)++;
235,823✔
3892
          break;
235,823✔
3893
        case 2:
4,498,867✔
3894
          val = ((int8_t *)pColData->pData)[iVal];
4,498,867✔
3895
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,498,867✔
3896
          break;
4,498,867✔
3897
        default:
×
3898
          break;
×
3899
      }
3900
    }
3901
  }
3902
}
7,129✔
3903

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

3911
  int16_t val;
3912
  if (HAS_VALUE == pColData->flag) {
7,130✔
3913
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,366,698✔
3914
      val = ((int16_t *)pColData->pData)[iVal];
4,361,036✔
3915
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,361,036✔
3916
    }
3917
  } else {
3918
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,158✔
3919
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,690!
3920
        case 0:
236,381✔
3921
        case 1:
3922
          (*numOfNull)++;
236,381✔
3923
          break;
236,381✔
3924
        case 2:
4,498,309✔
3925
          val = ((int16_t *)pColData->pData)[iVal];
4,498,309✔
3926
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,498,309✔
3927
          break;
4,498,309✔
3928
        default:
×
3929
          break;
×
3930
      }
3931
    }
3932
  }
3933
}
7,130✔
3934

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

3942
  int32_t val;
3943
  if (HAS_VALUE == pColData->flag) {
220,534✔
3944
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
662,212,736✔
3945
      val = ((int32_t *)pColData->pData)[iVal];
662,019,032✔
3946
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
662,019,032✔
3947
    }
3948
  } else {
3949
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
70,698,331✔
3950
      switch (tColDataGetBitValue(pColData, iVal)) {
70,672,537!
3951
        case 0:
3,566,537✔
3952
        case 1:
3953
          (*numOfNull)++;
3,566,537✔
3954
          break;
3,566,537✔
3955
        case 2:
67,134,345✔
3956
          val = ((int32_t *)pColData->pData)[iVal];
67,134,345✔
3957
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
67,134,345✔
3958
          break;
67,134,345✔
3959
        default:
×
3960
          break;
×
3961
      }
3962
    }
3963
  }
3964
}
219,498✔
3965

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

3973
  int64_t val;
3974
  if (HAS_VALUE == pColData->flag) {
83,132✔
3975
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
237,700,936✔
3976
      val = ((int64_t *)pColData->pData)[iVal];
237,619,732✔
3977
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
237,619,732✔
3978
    }
3979
  } else {
3980
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,826,300✔
3981
      switch (tColDataGetBitValue(pColData, iVal)) {
4,824,372!
3982
        case 0:
311,797✔
3983
        case 1:
3984
          (*numOfNull)++;
311,797✔
3985
          break;
311,797✔
3986
        case 2:
4,512,575✔
3987
          val = ((int64_t *)pColData->pData)[iVal];
4,512,575✔
3988
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,512,575✔
3989
          break;
4,512,575✔
3990
        default:
×
3991
          break;
×
3992
      }
3993
    }
3994
  }
3995
}
83,132✔
3996

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

4004
  float val;
4005
  if (HAS_VALUE == pColData->flag) {
157,154✔
4006
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
617,889,642✔
4007
      val = ((float *)pColData->pData)[iVal];
617,733,956✔
4008
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
617,733,956✔
4009
    }
4010
  } else {
4011
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,263✔
4012
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,795!
4013
        case 0:
234,774✔
4014
        case 1:
4015
          (*numOfNull)++;
234,774✔
4016
          break;
234,774✔
4017
        case 2:
4,500,021✔
4018
          val = ((float *)pColData->pData)[iVal];
4,500,021✔
4019
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
4,500,021✔
4020
          break;
4,500,021✔
4021
        default:
×
4022
          break;
×
4023
      }
4024
    }
4025
  }
4026
}
157,154✔
4027

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

4035
  double val;
4036
  if (HAS_VALUE == pColData->flag) {
7,887✔
4037
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
9,873,577✔
4038
      val = ((double *)pColData->pData)[iVal];
9,867,157✔
4039
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
9,867,157✔
4040
    }
4041
  } else {
4042
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,157✔
4043
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,690!
4044
        case 0:
235,590✔
4045
        case 1:
4046
          (*numOfNull)++;
235,590✔
4047
          break;
235,590✔
4048
        case 2:
4,499,100✔
4049
          val = ((double *)pColData->pData)[iVal];
4,499,100✔
4050
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
4,499,100✔
4051
          break;
4,499,100✔
4052
        default:
×
4053
          break;
×
4054
      }
4055
    }
4056
  }
4057
}
7,887✔
4058

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

4066
  uint8_t val;
4067
  if (HAS_VALUE == pColData->flag) {
4,214✔
4068
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,482,929✔
4069
      val = ((uint8_t *)pColData->pData)[iVal];
3,480,183✔
4070
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,480,183✔
4071
    }
4072
  } else {
4073
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,158✔
4074
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,690!
4075
        case 0:
236,388✔
4076
        case 1:
4077
          (*numOfNull)++;
236,388✔
4078
          break;
236,388✔
4079
        case 2:
4,498,302✔
4080
          val = ((uint8_t *)pColData->pData)[iVal];
4,498,302✔
4081
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,498,302✔
4082
          break;
4,498,302✔
4083
        default:
×
4084
          break;
×
4085
      }
4086
    }
4087
  }
4088
}
4,214✔
4089

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

4097
  uint16_t val;
4098
  if (HAS_VALUE == pColData->flag) {
4,215✔
4099
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,482,927✔
4100
      val = ((uint16_t *)pColData->pData)[iVal];
3,480,180✔
4101
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,480,180✔
4102
    }
4103
  } else {
4104
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,158✔
4105
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,690!
4106
        case 0:
236,891✔
4107
        case 1:
4108
          (*numOfNull)++;
236,891✔
4109
          break;
236,891✔
4110
        case 2:
4,497,799✔
4111
          val = ((uint16_t *)pColData->pData)[iVal];
4,497,799✔
4112
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,497,799✔
4113
          break;
4,497,799✔
4114
        default:
×
4115
          break;
×
4116
      }
4117
    }
4118
  }
4119
}
4,215✔
4120

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

4128
  uint32_t val;
4129
  if (HAS_VALUE == pColData->flag) {
4,213✔
4130
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,482,946✔
4131
      val = ((uint32_t *)pColData->pData)[iVal];
3,480,199✔
4132
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,480,199✔
4133
    }
4134
  } else {
4135
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,156✔
4136
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,690!
4137
        case 0:
236,579✔
4138
        case 1:
4139
          (*numOfNull)++;
236,579✔
4140
          break;
236,579✔
4141
        case 2:
4,498,111✔
4142
          val = ((uint32_t *)pColData->pData)[iVal];
4,498,111✔
4143
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,498,111✔
4144
          break;
4,498,111✔
4145
        default:
×
4146
          break;
×
4147
      }
4148
    }
4149
  }
4150
}
4,213✔
4151

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

4159
  uint64_t val;
4160
  if (HAS_VALUE == pColData->flag) {
4,215✔
4161
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,482,931✔
4162
      val = ((uint64_t *)pColData->pData)[iVal];
3,480,184✔
4163
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,480,184✔
4164
    }
4165
  } else {
4166
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,736,158✔
4167
      switch (tColDataGetBitValue(pColData, iVal)) {
4,734,690!
4168
        case 0:
236,288✔
4169
        case 1:
4170
          (*numOfNull)++;
236,288✔
4171
          break;
236,288✔
4172
        case 2:
4,498,402✔
4173
          val = ((uint64_t *)pColData->pData)[iVal];
4,498,402✔
4174
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,498,402✔
4175
          break;
4,498,402✔
4176
        default:
×
4177
          break;
×
4178
      }
4179
    }
4180
  }
4181
}
4,215✔
4182

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

4190
  switch (pColData->flag) {
19,627!
4191
    case HAS_NONE:
×
4192
    case HAS_NULL:
4193
    case (HAS_NONE | HAS_NULL):
4194
      *numOfNull = pColData->nVal;
×
4195
      break;
×
4196
    case HAS_VALUE:
16,691✔
4197
      *numOfNull = 0;
16,691✔
4198
      break;
16,691✔
4199
    case (HAS_VALUE | HAS_NULL):
2,936✔
4200
    case (HAS_VALUE | HAS_NONE):
4201
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
9,472,316✔
4202
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
9,469,380✔
4203
          (*numOfNull)++;
472,465✔
4204
        }
4205
      }
4206
      break;
2,936✔
4207
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
4208
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4209
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
4210
          (*numOfNull)++;
×
4211
        }
4212
      }
4213
      break;
×
4214
    default:
×
4215
      break;
×
4216
  }
4217
}
19,627✔
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) {
16,468,973✔
4245
  valCol->type = TSDB_DATA_TYPE_NULL;
16,468,973✔
4246
  valCol->numOfValues = 0;
16,468,973✔
4247
  tBufferInit(&valCol->data);
16,468,973✔
4248
  tBufferInit(&valCol->offsets);
16,468,973✔
4249
  return 0;
16,468,973✔
4250
}
4251

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

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

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

4271
  if (valCol->numOfValues == 0) {
887,042✔
4272
    valCol->type = value->type;
4,204✔
4273
  }
4274

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

4279
  if (IS_VAR_DATA_TYPE(value->type)) {
887,042!
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);
887,025✔
4288
    if (code) return code;
887,025!
4289
  }
4290
  valCol->numOfValues++;
887,042✔
4291

4292
  return 0;
887,042✔
4293
}
4294

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

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

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

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

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

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

4331
  value->type = valCol->type;
480,574,055✔
4332
  if (IS_VAR_DATA_TYPE(value->type)) {
480,574,055!
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) {
104✔
4338
      nextOffset = tBufferGetSize(&valCol->data);
101✔
4339
    } else {
4340
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
3!
4341
    }
4342
    value->nData = nextOffset - offset;
104✔
4343
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
104✔
4344
  } else {
4345
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
480,620,636✔
4346
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, &value->val));
961,241,272!
4347
  }
4348
  return 0;
480,620,740✔
4349
}
4350

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

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

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

4363
  // offset
4364
  if (IS_VAR_DATA_TYPE(valCol->type)) {
4,204!
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,204✔
4380
      .cmprAlg = info->cmprAlg,
4,204✔
4381
      .dataType = valCol->type,
4,204✔
4382
      .originalSize = valCol->data.size,
4,204✔
4383
  };
4384

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

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

4391
  return 0;
4,204✔
4392
}
4393

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

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

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

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

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

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

4432
  return 0;
40,470✔
4433
}
4434

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

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

4449
  return 0;
4,204✔
4450
}
4451

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

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

4473
  return 0;
40,469✔
4474
}
4475

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

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

4490
  if (info->cmprAlg == NO_COMPRESSION) {
5,230,951!
4491
    (void)memcpy(output, input, info->originalSize);
×
4492
    info->compressedSize = info->originalSize;
×
4493
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
6,369,772!
4494
    SBuffer local;
4495

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

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

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

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

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

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

4559
  return 0;
5,233,907✔
4560
}
4561

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

4574
  if (info->cmprAlg == NO_COMPRESSION) {
57,246,657!
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) {
80,772,283!
4580
    SBuffer local;
4581

4582
    tBufferInit(&local);
4583
    if (buffer == NULL) {
23,503,438!
4584
      buffer = &local;
×
4585
    }
4586

4587
    if (info->cmprAlg == TWO_STAGE_COMP) {
23,503,438!
4588
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
23,525,127✔
4589
      if (code) {
23,525,815!
4590
        tBufferDestroy(&local);
4591
        return code;
×
4592
      }
4593
    }
4594

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

4610
    if (!(decompressedSize == info->originalSize)) {
23,525,626!
4611
      return TSDB_CODE_COMPRESS_ERROR;
×
4612
    }
4613
    tBufferDestroy(&local);
4614
  } else {
4615
    DEFINE_VAR(info->cmprAlg);
33,743,219✔
4616
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
33,743,219!
4617
      (void)memcpy(output, input, info->compressedSize);
×
4618
      return 0;
×
4619
    }
4620
    SBuffer local;
4621

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

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

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

4652
  return 0;
57,271,861✔
4653
}
4654

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

4658
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
5,231,375✔
4659
  if (code) return code;
5,231,377!
4660

4661
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
5,231,377✔
4662
  if (code) return code;
5,232,004!
4663

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

4668
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
23,522,751✔
4669
  int32_t code;
4670

4671
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
23,522,751✔
4672
  if (code) return code;
23,521,635!
4673

4674
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
23,521,635✔
4675
  if (code) return code;
23,524,653!
4676

4677
  output->size += info->originalSize;
23,524,653✔
4678
  return 0;
23,524,653✔
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