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

taosdata / TDengine / #3578

11 Jan 2025 11:19AM UTC coverage: 63.183% (-0.03%) from 63.211%
#3578

push

travis-ci

web-flow
Merge pull request #29546 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

139873 of 284461 branches covered (49.17%)

Branch coverage included in aggregate %.

20 of 26 new or added lines in 2 files covered. (76.92%)

717 existing lines in 102 files now uncovered.

217827 of 281671 relevant lines covered (77.33%)

19620733.66 hits per line

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

61.18
/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) {
612,313,398✔
65
  int32_t n = 0;
612,313,398✔
66
  n += tPutI8(p ? p + n : p, index->type);
612,313,398✔
67
  n += tPutU32v(p ? p + n : p, index->offset);
612,313,398✔
68
  return n;
612,313,398✔
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,742,607✔
113
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
27,742,607✔
114
  sinfo->kvPayloadSize += tPutI16v(NULL, -pTColumn->colId);
27,742,607✔
115
  return 0;
27,742,607✔
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,569,246✔
123
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
206,569,246✔
124
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
206,569,246!
125
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
206,569,246✔
126
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
206,569,246✔
127
    sinfo->numOfPKs++;
206,569,246✔
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
790,815,719✔
133
                           + colVal->value.nData;               // value
790,815,719✔
134

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

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

161
  *sinfo = (SRowBuildScanInfo){
1,251,504,713✔
162
      .tupleFixedSize = schema->flen,
1,251,504,713✔
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,485,214!
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) {
5,975,097✔
190
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
62!
191
        break;
31✔
192
      } else {  // skip useless value
193
        colValIndex++;
5,975,066✔
194
      }
195
    }
196
  }
197

198
  if (sinfo->numOfNone) {
1,251,504,713✔
199
    sinfo->flag |= HAS_NONE;
488,622,366✔
200
  }
201
  if (sinfo->numOfNull) {
1,251,504,713✔
202
    sinfo->flag |= HAS_NULL;
18,998,966✔
203
  }
204
  if (sinfo->numOfValue) {
1,251,504,713✔
205
    sinfo->flag |= HAS_VALUE;
1,193,691,311✔
206
  }
207

208
  // Tuple
209
  sinfo->tupleFlag = sinfo->flag;
1,251,504,713✔
210
  switch (sinfo->flag) {
1,251,504,713!
211
    case HAS_NONE:
64,172,956✔
212
    case HAS_NULL:
213
      sinfo->tupleBitmapSize = 0;
64,172,956✔
214
      sinfo->tupleFixedSize = 0;
64,172,956✔
215
      break;
64,172,956✔
216
    case HAS_VALUE:
751,830,173✔
217
      sinfo->tupleBitmapSize = 0;
751,830,173✔
218
      sinfo->tupleFixedSize = schema->flen;
751,830,173✔
219
      break;
751,830,173✔
220
    case (HAS_NONE | HAS_NULL):
24,336✔
221
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
24,336✔
222
      sinfo->tupleFixedSize = 0;
24,336✔
223
      break;
24,336✔
224
    case (HAS_NONE | HAS_VALUE):
442,542,415✔
225
    case (HAS_NULL | HAS_VALUE):
226
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
442,542,415✔
227
      sinfo->tupleFixedSize = schema->flen;
442,542,415✔
228
      break;
442,542,415✔
229
    case (HAS_NONE | HAS_NULL | HAS_VALUE):
452,839✔
230
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
452,839✔
231
      sinfo->tupleFixedSize = schema->flen;
452,839✔
232
      break;
452,839✔
233
  }
234
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
1,458,337,377✔
235
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
207,386,192✔
236
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
207,386,192✔
237
  }
238
  sinfo->tupleRowSize = sizeof(SRow)              // SRow
1,250,951,185✔
239
                        + sinfo->tuplePKSize      // primary keys
1,250,951,185✔
240
                        + sinfo->tupleBitmapSize  // bitmap
1,250,951,185✔
241
                        + sinfo->tupleFixedSize   // fixed part
1,250,951,185✔
242
                        + sinfo->tupleVarSize;    // var part
1,250,951,185✔
243

244
  // Key-Value
245
  if (sinfo->kvMaxOffset <= UINT8_MAX) {
1,250,951,185!
246
    sinfo->kvFlag = (KV_FLG_LIT | sinfo->flag);
1,253,761,178✔
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint8_t);
1,253,761,178✔
248
  } else if (sinfo->kvMaxOffset <= UINT16_MAX) {
×
249
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
4,707,082✔
250
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
4,707,082✔
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,457,187,576✔
256
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
206,698,820✔
257
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
206,698,820✔
258
  }
259
  sinfo->kvRowSize = sizeof(SRow)             // SRow
1,250,488,756✔
260
                     + sinfo->kvPKSize        // primary keys
1,250,488,756✔
261
                     + sinfo->kvIndexSize     // index array
1,250,488,756✔
262
                     + sinfo->kvPayloadSize;  // payload
1,250,488,756✔
263

264
_exit:
1,250,488,756✔
265
  return code;
1,250,488,756✔
266
}
267

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

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

282
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
833,313,606✔
283
    return 0;
65,841,578✔
284
  }
285

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

291
  // primary keys
292
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
973,296,709✔
293
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
207,979,871✔
294
  }
295

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

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

310
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647!
311
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
170,580,155✔
312
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
170,580,155✔
313
            if (colValArray[colValIndex].value.nData) {
170,580,155!
314
              (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
172,909,770✔
315
              varlen += colValArray[colValIndex].value.nData;
172,909,770✔
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
14,438,204!
322
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
14,871,366!
323
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
324
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
461,716!
325
        }
326

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

338
  return 0;
765,316,838✔
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,002,583!
345
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
7,550,511✔
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) {
430,679,712✔
353
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
430,679,712✔
354

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

365
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
430,819,511!
366
    return TSDB_CODE_INVALID_PARA;
50,558✔
367
  }
368

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

374
  // primary keys
375
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
432,201,681✔
376
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
1,432,734✔
377
  }
378

379
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
430,768,947✔
380
  int32_t colValIndex = 1;
430,768,947✔
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);
617,397,045✔
392
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
617,397,045✔
393
            if (colValArray[colValIndex].value.nData > 0) {
617,397,045!
394
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
635,407,774✔
395
                           colValArray[colValIndex].value.nData);
635,407,774✔
396
            }
397
            payloadSize += colValArray[colValIndex].value.nData;
617,397,045✔
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,018✔
406
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
25,250,036✔
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;
430,768,947✔
420
}
421

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

426
  code = tRowBuildScan(aColVal, pTSchema, &sinfo);
1,251,452,249✔
427
  if (code) return code;
1,257,906,109!
428

429
  if (sinfo.tupleRowSize <= sinfo.kvRowSize) {
1,257,906,109✔
430
    code = tRowBuildTupleRow(aColVal, &sinfo, pTSchema, ppRow);
827,467,584✔
431
  } else {
432
    code = tRowBuildKVRow(aColVal, &sinfo, pTSchema, ppRow);
430,438,525✔
433
  }
434
  return code;
1,259,443,016✔
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,466✔
456
                          SArray *rowArray, bool *pOrdered, bool *pDupTs) {
457
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
5,466!
458
    return TSDB_CODE_INVALID_PARA;
×
459
  }
460

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

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

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

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

478
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
164,315✔
479
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
153,376✔
480
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
650✔
481
      } else {
482
        SValue value = {
152,726✔
483
            .type = infos[iInfo].type,
152,726✔
484
        };
485
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
152,726!
486
          value.nData = infos[iInfo].bind->length[iRow];
21,809✔
487
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
21,809!
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,809✔
492
        } else {
493
          (void)memcpy(&value.val, (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
130,917✔
494
                       infos[iInfo].bind->buffer_length);
130,917✔
495
        }
496
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
152,726✔
497
      }
498
      if (taosArrayPush(colValArray, &colVal) == NULL) {
153,356!
499
        code = terrno;
×
500
        goto _exit;
×
501
      }
502
    }
503

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

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

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

531
_exit:
5,460✔
532
  taosArrayDestroy(colValArray);
5,460✔
533
  return code;
5,466✔
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;
65,892,310✔
544
    pColVal->value.type = pTColumn->type;
65,892,310✔
545
    pColVal->flag = CV_FLAG_VALUE;
65,892,310✔
546
    (void)memcpy(&pColVal->value.val, &pRow->ts, sizeof(TSKEY));
65,892,310✔
547
    return 0;
65,892,310✔
548
  }
549

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

555
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
556
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
846,965✔
557
    return 0;
846,965✔
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);
79,894,481✔
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) {
65,198,915!
573
      pv = pIdx->idx + (pIdx->nCol << 1);
108,770,066✔
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) {
450,920,480!
586
        pData = pv + ((uint16_t *)pIdx->idx)[mid];
451,101,706✔
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);
86,843,278✔
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);
935,052,593!
604
            if (pColVal->value.nData > 0) {
935,052,593!
605
              pColVal->value.pData = pData;
1,079,192,020✔
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;
794,104,869✔
624
    uint8_t *fixed;
625
    uint8_t *varlen;
626
    uint8_t  bit;
627

628
    if (pRow->flag == HAS_VALUE) {
794,104,869✔
629
      fixed = bitmap;
683,853,864✔
630
      bit = BIT_FLG_VALUE;
683,853,864✔
631
    } else if (pRow->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
110,251,005!
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;
110,251,005✔
636
      bit = GET_BIT1(bitmap, iCol - 1);
110,251,005✔
637

638
      if (pRow->flag == (HAS_NONE | HAS_VALUE)) {
110,251,005✔
639
        if (bit) bit++;
20,213✔
640
      } else if (pRow->flag == (HAS_NULL | HAS_VALUE)) {
110,230,792✔
641
        bit++;
93,739,456✔
642
      }
643
    }
644
    varlen = fixed + pTSchema->flen;
794,104,869✔
645

646
    if (bit == BIT_FLG_NONE) {
794,104,869✔
647
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
7,218✔
648
      return 0;
7,218✔
649
    } else if (bit == BIT_FLG_NULL) {
794,097,651✔
650
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
9,926,747✔
651
      return 0;
9,926,747✔
652
    }
653

654
    pColVal->cid = pTColumn->colId;
784,170,904✔
655
    pColVal->value.type = pTColumn->type;
784,170,904✔
656
    pColVal->flag = CV_FLAG_VALUE;
784,170,904✔
657
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
784,170,904!
658
      pColVal->value.pData = varlen + *(int32_t *)(fixed + pTColumn->offset);
75,357,324✔
659
      pColVal->value.pData += tGetU32v(pColVal->value.pData, &pColVal->value.nData);
150,714,648✔
660
    } else {
661
      (void)memcpy(&pColVal->value.val, fixed + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
708,813,580✔
662
    }
663
  }
664

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

668
void tRowDestroy(SRow *pRow) {
765,270,605✔
669
  if (pRow) taosMemoryFree(pRow);
765,270,605!
670
}
765,250,315✔
671

672
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
275,897,096✔
673
  SRowKey key1, key2;
674
  tRowGetKey(*(SRow **)p1, &key1);
275,897,096✔
675
  tRowGetKey(*(SRow **)p2, &key2);
271,270,307✔
676
  return tRowKeyCompare(&key1, &key2);
276,660,228✔
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,377✔
757
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
34,377✔
758
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
34,374✔
759
  if (code != TSDB_CODE_SUCCESS) {
34,374!
760
    uError("taosArrayMSort failed caused by %d", code);
×
761
  }
762
  return code;
34,374✔
763
}
764

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

768
  int32_t iStart = 0;
34,417✔
769
  while (iStart < aRowP->size) {
38,821,915!
770
    SRowKey key1;
771
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
38,837,384✔
772

773
    tRowGetKey(row1, &key1);
38,953,769✔
774

775
    int32_t iEnd = iStart + 1;
38,791,222✔
776
    while (iEnd < aRowP->size) {
38,771,880✔
777
      SRowKey key2;
778
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
38,760,014✔
779
      tRowGetKey(row2, &key2);
38,886,156✔
780

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

783
      iEnd++;
×
784
    }
785

786
    if (iEnd - iStart > 1) {
38,787,498✔
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++;
38,787,498✔
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,191,553✔
819
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
1,191,553!
820

821
  int32_t code = 0;
1,191,553✔
822

823
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
1,191,553!
824
  if (pIter == NULL) {
1,249,002!
825
    code = terrno;
×
826
    goto _exit;
×
827
  }
828

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

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

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

841
  if (pRow->flag >> 4) {
1,245,182✔
842
    pIter->iCol = 0;
1,243,018✔
843
    pIter->pIdx = (SKVIdx *)data;
1,243,018✔
844
    if (pRow->flag & KV_FLG_LIT) {
1,243,018✔
845
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
1,241,252✔
846
    } else if (pRow->flag & KV_FLG_MID) {
1,766!
847
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
3,600✔
848
    } else {
849
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
850
    }
851
  } else {
852
    switch (pRow->flag) {
2,164!
853
      case (HAS_NULL | HAS_NONE):
1✔
854
        pIter->pb = data;
1✔
855
        break;
1✔
856
      case HAS_VALUE:
2,240✔
857
        pIter->pf = data;
2,240✔
858
        pIter->pv = pIter->pf + pTSchema->flen;
2,240✔
859
        break;
2,240✔
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:
×
872
        break;
×
873
    }
874
  }
875

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

885
void tRowIterClose(SRowIter **ppIter) {
1,156,587✔
886
  SRowIter *pIter = *ppIter;
1,156,587✔
887
  if (pIter) {
1,156,587!
888
    taosMemoryFree(pIter);
1,156,911!
889
  }
890
  *ppIter = NULL;
1,254,109✔
891
}
1,254,109✔
892

893
SColVal *tRowIterNext(SRowIter *pIter) {
12,793,502✔
894
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
12,793,502✔
895
    return NULL;
1,158,963✔
896
  }
897

898
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
11,634,539✔
899

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

909
  if (pIter->pRow->flag == HAS_NONE) {
10,422,753✔
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,422,752✔
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,420,911!
920
    if (pIter->iCol < pIter->pIdx->nCol) {
11,313,516✔
921
      uint8_t *pData;
922

923
      if (pIter->pRow->flag & KV_FLG_LIT) {
9,799,520✔
924
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
9,723,542✔
925
      } else if (pIter->pRow->flag & KV_FLG_MID) {
75,978!
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,799,520✔
933

934
      if (TABS(cid) == pTColumn->colId) {
9,799,520!
935
        if (cid < 0) {
9,974,435✔
936
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,919,731✔
937
        } else {
938
          pIter->cv.cid = pTColumn->colId;
8,054,704✔
939
          pIter->cv.value.type = pTColumn->type;
8,054,704✔
940
          pIter->cv.flag = CV_FLAG_VALUE;
8,054,704✔
941

942
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
8,054,704!
943
            pData += tGetU32v(pData, &pIter->cv.value.nData);
862,953!
944
            if (pIter->cv.value.nData > 0) {
862,953!
945
              pIter->cv.value.pData = pData;
2,360,639✔
946
            } else {
947
              pIter->cv.value.pData = NULL;
×
948
            }
949
          } else {
950
            (void)memcpy(&pIter->cv.value.val, pData, pTColumn->bytes);
7,191,751✔
951
          }
952
        }
953

954
        pIter->iCol++;
9,974,435✔
955
        goto _exit;
9,974,435✔
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,513,996✔
965
      goto _exit;
1,513,996✔
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,960✔
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,431✔
1010
    }
1011
    goto _exit;
×
1012
  }
1013

1014
_exit:
11,809,536✔
1015
  pIter->iTColumn++;
11,809,536✔
1016
  return &pIter->cv;
11,809,536✔
1017
}
1018

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

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

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

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

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

1039
  while (pColData) {
147,165✔
1040
    if (pTColumn) {
132,167!
1041
      if (pTColumn->colId == pColData->cid) {  // NULL
132,167✔
1042
        if (flag == 0) {
132,158✔
1043
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
132,102✔
1044
        } else {
1045
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
56✔
1046
        }
1047
        if (code) goto _exit;
132,173!
1048

1049
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
132,173✔
1050
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
132,173✔
1051
      } else if (pTColumn->colId > pColData->cid) {  // NONE
9!
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;
9!
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:
14,998✔
1064
  return code;
14,998✔
1065
}
1066
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
727,949,259✔
1067
                                      int32_t flag) {
1068
  int32_t code = 0;
727,949,259✔
1069

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

1075
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
727,949,259✔
1076
  SPrimaryKeyIndex index;
1077
  uint8_t         *data = pRow->data;
727,949,259✔
1078
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
953,113,288✔
1079
    data += tGetPrimaryKeyIndex(data, &index);
225,224,891✔
1080
  }
1081

1082
  switch (pRow->flag) {
727,888,397!
1083
    case HAS_VALUE:
718,834,838✔
1084
      pf = data;  // TODO: fix here
718,834,838✔
1085
      pv = pf + pTSchema->flen;
718,834,838✔
1086
      break;
718,834,838✔
1087
    case (HAS_NULL | HAS_NONE):
1,296✔
1088
      pb = data;
1,296✔
1089
      break;
1,296✔
1090
    case (HAS_VALUE | HAS_NONE):
9,160,651✔
1091
    case (HAS_VALUE | HAS_NULL):
1092
      pb = data;
9,160,651✔
1093
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
9,160,651✔
1094
      pv = pf + pTSchema->flen;
9,160,651✔
1095
      break;
9,160,651✔
1096
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
1097
      pb = data;
×
1098
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
1099
      pv = pf + pTSchema->flen;
×
1100
      break;
×
1101
    default:
×
1102
      return TSDB_CODE_INVALID_DATA_FMT;
×
1103
  }
1104

1105
  while (pColData) {
2,147,483,647✔
1106
    if (pTColumn) {
2,147,483,647✔
1107
      if (pTColumn->colId == pColData->cid) {
2,147,483,647!
1108
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647!
1109
          return TSDB_CODE_INVALID_PARA;
×
1110
        }
1111
        if (pb) {
2,147,483,647✔
1112
          uint8_t bv;
1113
          switch (pRow->flag) {
62,183,417!
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,051✔
1118
              bv = GET_BIT1(pb, iTColumn - 1);
199,051✔
1119
              if (bv) bv++;
199,051✔
1120
              break;
199,051✔
1121
            case (HAS_VALUE | HAS_NULL):
61,958,468✔
1122
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
61,958,468✔
1123
              break;
61,958,468✔
1124
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
1125
              bv = GET_BIT2(pb, iTColumn - 1);
×
1126
              break;
×
UNCOV
1127
            default:
×
UNCOV
1128
              return TSDB_CODE_INVALID_DATA_FMT;
×
1129
          }
1130

1131
          if (bv == BIT_FLG_NONE) {
62,183,439✔
1132
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
43,348!
1133
              goto _exit;
×
1134
            goto _continue;
43,351✔
1135
          } else if (bv == BIT_FLG_NULL) {
62,140,091✔
1136
            if (flag == 0) {
10,259,338✔
1137
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
10,087,243✔
1138
            } else {
1139
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
172,095✔
1140
            }
1141
            if (code) goto _exit;
10,260,864!
1142
            goto _continue;
10,260,864✔
1143
          }
1144
        }
1145

1146
        if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647!
1147
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
55,420,619!
1148
          uint32_t nData;
1149
          pData += tGetU32v(pData, &nData);
55,420,619✔
1150
          if (flag == 0) {
55,420,619!
1151
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
57,971,544✔
1152
          } else {
1153
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
1154
          }
1155
          if (code) goto _exit;
59,737,709!
1156
        } else {
1157
          if (flag == 0) {
2,147,483,647✔
1158
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
1159
                                                                          TYPE_BYTES[pColData->type]);
2,147,483,647✔
1160
          } else {
1161
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
6,077,688✔
1162
                                                                          TYPE_BYTES[pColData->type], flag > 0);
6,077,688✔
1163
          }
1164
          if (code) goto _exit;
2,147,483,647!
1165
        }
1166

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

1182
_exit:
731,163,783✔
1183
  return code;
731,163,783✔
1184
}
1185
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
3,124,256✔
1186
  int32_t code = 0;
3,124,256✔
1187

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

1195
  // primary keys
1196
  uint8_t         *data = pRow->data;
3,124,256✔
1197
  SPrimaryKeyIndex index;
1198
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
3,272,933✔
1199
    data += tGetPrimaryKeyIndex(data, &index);
148,677✔
1200
  }
1201

1202
  SKVIdx *pKVIdx = (SKVIdx *)data;
3,124,256✔
1203
  if (pRow->flag & KV_FLG_LIT) {
3,124,256✔
1204
    pv = pKVIdx->idx + pKVIdx->nCol;
3,065,799✔
1205
  } else if (pRow->flag & KV_FLG_MID) {
58,457!
1206
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
62,843✔
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,071,853✔
1214
    if (pTColumn) {
34,960,833✔
1215
      if (pTColumn->colId == pColData->cid) {
34,561,539✔
1216
        while (iCol < pKVIdx->nCol) {
34,542,232✔
1217
          uint8_t *pData;
1218
          if (pRow->flag & KV_FLG_LIT) {
29,378,069✔
1219
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
27,673,472✔
1220
          } else if (pRow->flag & KV_FLG_MID) {
1,704,597!
1221
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
1,704,640✔
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,378,112✔
1230

1231
          if (TABS(cid) == pTColumn->colId) {
29,378,112✔
1232
            if (cid < 0) {
28,947,838✔
1233
              if (flag == 0) {
5,388,165✔
1234
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
5,380,464✔
1235
              } else {
1236
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
7,701✔
1237
              }
1238
              if (code) goto _exit;
4,761,177!
1239
            } else {
1240
              uint32_t nData;
1241
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
23,559,673!
1242
                pData += tGetU32v(pData, &nData);
5,900,864✔
1243
              } else {
1244
                nData = 0;
17,658,809✔
1245
              }
1246
              if (flag == 0) {
23,559,673!
1247
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
23,871,810✔
1248
              } else {
1249
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
1250
              }
1251
              if (code) goto _exit;
24,169,374!
1252
            }
1253
            iCol++;
28,930,551✔
1254
            goto _continue;
28,930,551✔
1255
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
430,274✔
1256
            break;
39,155✔
1257
          } else {
1258
            iCol++;
391,119✔
1259
          }
1260
        }
1261

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

1264
      _continue:
5,202,959✔
1265
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
34,133,510✔
1266
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
34,133,510✔
1267
      } else if (pTColumn->colId > pColData->cid) {
410,426!
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;
410,426!
1272
      }
1273
    } else {
1274
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
399,294!
1275
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
399,275✔
1276
    }
1277
  }
1278

1279
_exit:
3,111,020✔
1280
  return code;
3,111,020✔
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) {
730,850,053✔
1287
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
730,850,053!
1288
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
730,850,053!
1289

1290
  if (pRow->flag == HAS_NONE) {
730,850,053✔
1291
    return tRowNoneUpsertColData(aColData, nColData, flag);
1,041✔
1292
  } else if (pRow->flag == HAS_NULL) {
730,849,012✔
1293
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
14,982✔
1294
  } else if (pRow->flag >> 4) {  // KV row
730,834,030✔
1295
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
3,124,143✔
1296
  } else {  // TUPLE row
1297
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
727,709,887✔
1298
  }
1299
}
1300

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

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

1308
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
1309

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

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

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

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

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

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

1345
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
43,233,921✔
1346
  switch (tv1->type) {
43,233,921!
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,469✔
1353
      T_COMPARE_SCALAR_VALUE(int32_t, &tv1->val, &tv2->val);
953,469✔
1354
    case TSDB_DATA_TYPE_BIGINT:
42,282,287✔
1355
    case TSDB_DATA_TYPE_TIMESTAMP:
1356
      T_COMPARE_SCALAR_VALUE(int64_t, &tv1->val, &tv2->val);
42,282,287✔
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:
×
1384
      break;
×
1385
  }
1386

1387
  return 0;
×
1388
}
1389

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

1400
  if (key1->numOfPKs == key2->numOfPKs) {
46,590,291!
1401
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
67,354,435!
1402
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
43,234,178✔
1403
      if (ret) return ret;
43,205,321!
1404
    }
1405
  } else if (key1->numOfPKs < key2->numOfPKs) {
×
1406
    return -1;
×
1407
  } else {
1408
    return 1;
×
1409
  }
1410

1411
  return 0;
24,120,257✔
1412
}
1413

1414
void tRowKeyAssign(SRowKey *pDst, SRowKey *pSrc) {
830,249,001✔
1415
  pDst->ts = pSrc->ts;
830,249,001✔
1416
  pDst->numOfPKs = pSrc->numOfPKs;
830,249,001✔
1417

1418
  if (pSrc->numOfPKs > 0) {
830,249,001✔
1419
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
50,953,611✔
1420
      SValue *pVal = &pDst->pks[i];
25,547,306✔
1421
      pVal->type = pSrc->pks[i].type;
25,547,306✔
1422

1423
      if (IS_NUMERIC_TYPE(pVal->type)) {
25,547,306!
1424
        pVal->val = pSrc->pks[i].val;
25,547,296✔
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
}
830,249,001✔
1432

1433
// STag ========================================
1434
static int tTagValCmprFn(const void *p1, const void *p2) {
134,231,141✔
1435
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
134,231,141✔
1436
    return -1;
45,219,578✔
1437
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
89,011,563✔
1438
    return 1;
49,891,544✔
1439
  }
1440

1441
  return 0;
39,120,019✔
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,392,662✔
1541
  int32_t n = 0;
1,392,662✔
1542

1543
  // key
1544
  if (isJson) {
1,392,662✔
1545
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
1,076✔
1546
  } else {
1547
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
2,784,248✔
1548
  }
1549

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

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

1562
  return n;
1,392,662✔
1563
}
1564
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
132,438,996✔
1565
  int32_t n = 0;
132,438,996✔
1566

1567
  // key
1568
  if (isJson) {
132,438,996✔
1569
    n += tGetCStr(p + n, &pTagVal->pKey);
26,598!
1570
  } else {
1571
    n += tGetI16v(p + n, &pTagVal->cid);
264,851,394✔
1572
  }
1573

1574
  // type
1575
  n += tGetI8(p + n, &pTagVal->type);
132,438,996!
1576

1577
  // value
1578
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
132,438,996!
1579
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
43,288,856!
1580
  } else {
1581
    (void)memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
110,794,568✔
1582
    n += tDataTypes[pTagVal->type].bytes;
110,794,568✔
1583
  }
1584

1585
  return n;
132,438,996✔
1586
}
1587

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

1605
  // sort
1606
  if (isJson) {
185,960✔
1607
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
218✔
1608
  } else {
1609
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
185,742✔
1610
  }
1611

1612
  // get size
1613
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
882,306✔
1614
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
696,355✔
1615
  }
1616
  if (szTag <= INT8_MAX) {
185,951✔
1617
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
171,879✔
1618
  } else {
1619
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
14,072✔
1620
    isLarge = 1;
14,072✔
1621
  }
1622

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

1640
  if (isLarge) {
185,997✔
1641
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
14,077✔
1642
  } else {
1643
    p = (uint8_t *)&(*ppTag)->idx[nTag];
171,920✔
1644
  }
1645
  n = 0;
185,997✔
1646
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
882,416✔
1647
    if (isLarge) {
696,434✔
1648
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
75,097✔
1649
    } else {
1650
      (*ppTag)->idx[iTag] = n;
621,337✔
1651
    }
1652
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
696,434✔
1653
  }
1654
#ifdef TD_DEBUG_PRINT_TAG
1655
  debugPrintSTag(*ppTag, __func__, __LINE__);
1656
#endif
1657

1658
  return code;
185,982✔
1659

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

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

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

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

1679
  if (IS_VAR_DATA_TYPE(value->type)) {
13,794,205!
1680
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
3,674,214!
1681
    if (data == NULL) {
3,675,120!
1682
      return NULL;
×
1683
    }
1684

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

1689
    varDataLen(data + typeBytes) = value->nData;
3,675,120✔
1690
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
3,675,120✔
1691
  } else {
1692
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
10,119,991✔
1693
  }
1694

1695
  return data;
13,795,111✔
1696
}
1697

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

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

1713
  if (isLarge) {
40,877,152✔
1714
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
33,986,951✔
1715
  } else {
1716
    p = (uint8_t *)&pTag->idx[pTag->nTag];
6,890,201✔
1717
  }
1718

1719
  pTagVal->type = TSDB_DATA_TYPE_NULL;
40,877,152✔
1720
  pTagVal->pData = NULL;
40,877,152✔
1721
  pTagVal->nData = 0;
40,877,152✔
1722
  while (lidx <= ridx) {
134,877,906✔
1723
    midx = (lidx + ridx) / 2;
132,841,637✔
1724
    if (isLarge) {
132,841,637✔
1725
      offset = ((int16_t *)pTag->idx)[midx];
110,350,601✔
1726
    } else {
1727
      offset = pTag->idx[midx];
22,491,036✔
1728
    }
1729

1730
    int32_t nt = tGetTagVal(p + offset, &tv, isJson);
132,841,637✔
1731
    if (isJson) {
133,539,318✔
1732
      c = tTagValJsonCmprFn(pTagVal, &tv);
11,388✔
1733
    } else {
1734
      c = tTagValCmprFn(pTagVal, &tv);
133,527,930✔
1735
    }
1736

1737
    if (c < 0) {
134,889,423✔
1738
      ridx = midx - 1;
44,429,668✔
1739
    } else if (c > 0) {
90,459,755✔
1740
      lidx = midx + 1;
49,571,086✔
1741
    } else {
1742
      (void)memcpy(pTagVal, &tv, sizeof(tv));
40,888,669✔
1743
      return true;
40,888,669✔
1744
    }
1745
  }
1746
  return false;
2,036,269✔
1747
}
1748

1749
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
816,148✔
1750
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
1,632,296✔
1751
}
1752

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

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

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

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

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

1787
  return code;
1,445✔
1788

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

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

1801
  pTSchema->numOfCols = numOfCols;
15,726,069✔
1802
  pTSchema->version = version;
15,726,069✔
1803

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

1819
  // other columns
1820
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
184,276,936✔
1821
    SSchema  *pSchema = &aSchema[iCol];
168,550,867✔
1822
    STColumn *pTColumn = &pTSchema->columns[iCol];
168,550,867✔
1823

1824
    pTColumn->colId = pSchema->colId;
168,550,867✔
1825
    pTColumn->type = pSchema->type;
168,550,867✔
1826
    pTColumn->flags = pSchema->flags;
168,550,867✔
1827
    pTColumn->offset = pTSchema->flen;
168,550,867✔
1828

1829
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
168,550,867!
1830
      pTColumn->bytes = pSchema->bytes;
33,399,353✔
1831
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
33,399,353✔
1832
    } else {
1833
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
135,151,514✔
1834
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
135,151,514✔
1835
    }
1836

1837
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
168,550,867✔
1838
  }
1839

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

1844
  return pTSchema;
15,726,069✔
1845
}
1846

1847
static int32_t tTColumnCompare(const void *p1, const void *p2) {
9,500,586✔
1848
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
9,500,586✔
1849
    return -1;
1,428,470✔
1850
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
8,072,116✔
1851
    return 1;
5,688,098✔
1852
  }
1853

1854
  return 0;
2,384,018✔
1855
}
1856

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

1862
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
2,384,733✔
1863
}
1864

1865
// SColData ========================================
1866
void tColDataDestroy(void *ph) {
24,030,226✔
1867
  if (ph) {
24,030,226!
1868
    SColData *pColData = (SColData *)ph;
24,030,353✔
1869

1870
    tFree(pColData->pBitMap);
24,030,353!
1871
    tFree(pColData->aOffset);
24,030,369✔
1872
    tFree(pColData->pData);
24,030,359!
1873
  }
1874
}
24,031,042✔
1875

1876
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
23,092,470✔
1877
  pColData->cid = cid;
23,092,470✔
1878
  pColData->type = type;
23,092,470✔
1879
  pColData->cflag = cflag;
23,092,470✔
1880
  tColDataClear(pColData);
23,092,470✔
1881
}
23,094,825✔
1882

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

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

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

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

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

1908
    if (nData) {
110,304,325!
1909
      code = tRealloc(&pColData->pData, pColData->nData + nData);
107,369,249!
1910
      if (code) goto _exit;
108,328,884!
1911
      (void)memcpy(pColData->pData + pColData->nData, pData, nData);
108,328,884✔
1912
      pColData->nData += nData;
108,328,884✔
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]);
19,771,579✔
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,511,802✔
1933
  pColData->flag = HAS_VALUE;
3,511,908✔
1934
  pColData->numOfValue++;
3,511,802✔
1935
  return tColDataPutValue(pColData, pData, nData);
3,512,353✔
1936
}
1937
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
174,967✔
1938
  pColData->flag = HAS_NONE;
174,967✔
1939
  pColData->numOfNone++;
174,967✔
1940
  pColData->nVal++;
174,967✔
1941
  return 0;
174,967✔
1942
}
1943
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
157,888✔
1944
  pColData->flag = HAS_NULL;
157,986✔
1945
  pColData->numOfNull++;
157,986✔
1946
  pColData->nVal++;
157,986✔
1947
  return 0;
157,888✔
1948
}
1949
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,315✔
1950
  int32_t code = 0;
1,315✔
1951

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

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

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

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

1976
  return tColDataPutValue(pColData, pData, nData);
1,315✔
1977
}
1978
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
11,145,915✔
1979
  pColData->nVal++;
11,145,915✔
1980
  pColData->numOfNone++;
11,145,915✔
1981
  return 0;
11,145,915✔
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) {
19,724✔
2000
  int32_t code = 0;
19,727✔
2001

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

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

2009
  pColData->flag |= HAS_VALUE;
19,729✔
2010
  pColData->numOfValue++;
19,729✔
2011

2012
  if (pColData->nVal) {
19,729!
2013
    if (IS_VAR_DATA_TYPE(pColData->type)) {
19,730!
2014
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
8,433✔
2015
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
8,433!
2016
      if (code) return code;
8,435!
2017
      memset(pColData->aOffset, 0, nOffset);
8,435✔
2018
    } else {
2019
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
11,297✔
2020
      code = tRealloc(&pColData->pData, pColData->nData);
11,297!
2021
      if (code) return code;
11,298!
2022
      memset(pColData->pData, 0, pColData->nData);
11,298✔
2023
    }
2024
  }
2025

2026
  return tColDataPutValue(pColData, pData, nData);
19,733✔
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,415,769✔
2045
  pColData->nVal++;
4,415,769✔
2046
  pColData->numOfNull++;
4,415,769✔
2047
  return 0;
4,415,769✔
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,380✔
2096
  int32_t code = 0;
9,380✔
2097

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

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

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

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

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

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

2124
  return tColDataPutValue(pColData, NULL, 0);
6,019✔
2125
}
2126
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
261,391✔
2127
  int32_t code = 0;
267,535✔
2128

2129
  pColData->flag |= HAS_NULL;
267,535✔
2130
  pColData->numOfNull++;
267,535✔
2131

2132
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
267,535✔
2133
  code = tRealloc(&pColData->pBitMap, nBit);
267,232✔
2134
  if (code) return code;
267,536!
2135

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

2139
  return tColDataPutValue(pColData, NULL, 0);
267,546✔
2140
}
2141
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
51,440✔
2142
  int32_t code = 0;
53,441✔
2143

2144
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
53,440!
2145
  if (code) return code;
53,521!
2146

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

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

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

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

2161
  return tColDataPutValue(pColData, NULL, 0);
102,132✔
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) {
336,381,308✔
2184
  int32_t code = 0;
336,545,866✔
2185

2186
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
336,545,466!
2187
  if (code) return code;
336,792,877!
2188
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
336,792,877!
2189
  pColData->numOfValue++;
336,792,877✔
2190

2191
  return tColDataPutValue(pColData, pData, nData);
336,712,400✔
2192
}
2193
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,995✔
2194
  int32_t code = 0;
3,995✔
2195

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

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

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

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

2211
  return tColDataPutValue(pColData, NULL, 0);
3,997✔
2212
}
2213
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
19,354,992✔
2214
  int32_t code = 0;
19,532,734✔
2215

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

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

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

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

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

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

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

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

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

2274
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
9✔
2275
  pColData->numOfNone--;
9✔
2276
  pColData->nVal--;
9✔
2277
  if (pColData->numOfNone) {
9!
2278
    return tColDataAppendValue10(pColData, pData, nData);
×
2279
  } else {
2280
    pColData->flag = 0;
9✔
2281
    return tColDataAppendValue00(pColData, pData, nData);
9✔
2282
  }
2283
}
2284
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
24✔
2285
  pColData->numOfNone--;
24✔
2286
  pColData->nVal--;
24✔
2287
  if (pColData->numOfNone) {
24!
2288
    return tColDataAppendValue12(pColData, pData, nData);
×
2289
  } else {
2290
    pColData->flag = 0;
24✔
2291
    return tColDataAppendValue02(pColData, pData, nData);
24✔
2292
  }
2293
  return 0;
2294
}
2295
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
100✔
2296
  if (forward) {
100!
2297
    pColData->numOfNull--;
100✔
2298
    pColData->nVal--;
100✔
2299
    if (pColData->numOfNull) {
100✔
2300
      return tColDataAppendValue20(pColData, pData, nData);
3✔
2301
    } else {
2302
      pColData->flag = 0;
97✔
2303
      return tColDataAppendValue00(pColData, pData, nData);
97✔
2304
    }
2305
  }
2306
  return 0;
×
2307
}
2308
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
2309
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
2310
    pColData->numOfNone--;
×
2311
    pColData->nVal--;
×
2312
    if (pColData->numOfNone) {
×
2313
      return tColDataAppendValue30(pColData, pData, nData);
×
2314
    } else {
2315
      pColData->flag = HAS_NULL;
×
2316
      return tColDataAppendValue20(pColData, pData, nData);
×
2317
    }
2318
  } else if (forward) {  // NULL ==> VALUE
×
2319
    pColData->numOfNull--;
×
2320
    pColData->nVal--;
×
2321
    if (pColData->numOfNull) {
×
2322
      return tColDataAppendValue30(pColData, pData, nData);
×
2323
    } else {
2324
      pColData->flag = HAS_NONE;
×
2325
      return tColDataAppendValue10(pColData, pData, nData);
×
2326
    }
2327
  }
2328
  return 0;
×
2329
}
2330
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
80✔
2331
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
80!
2332
    pColData->numOfNone--;
80✔
2333
    pColData->numOfNull++;
80✔
2334
    if (pColData->numOfNone) {
80!
2335
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
×
2336
    } else {
2337
      pColData->flag = HAS_NULL;
80✔
2338
    }
2339
  }
2340
  return 0;
80✔
2341
}
2342
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
6,586,348✔
2343
  if (forward) {  // VALUE ==> VALUE
6,586,348!
2344
    pColData->nVal--;
6,586,461✔
2345
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,586,461!
2346
      pColData->nData = pColData->aOffset[pColData->nVal];
734,333✔
2347
    } else {
2348
      pColData->nData -= TYPE_BYTES[pColData->type];
5,852,128✔
2349
    }
2350
    return tColDataPutValue(pColData, pData, nData);
6,602,482✔
2351
  }
2352
  return 0;
×
2353
}
2354
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,914✔
2355
  if (forward) {  // VALUE ==> NULL
5,914!
2356
    pColData->numOfValue--;
5,915✔
2357
    pColData->nVal--;
5,915✔
2358
    if (pColData->numOfValue) {
5,915✔
2359
      if (IS_VAR_DATA_TYPE(pColData->type)) {
5,841!
2360
        pColData->nData = pColData->aOffset[pColData->nVal];
1,150✔
2361
      } else {
2362
        pColData->nData -= TYPE_BYTES[pColData->type];
4,691✔
2363
      }
2364
      return tColDataAppendValue42(pColData, pData, nData);
5,841✔
2365
    } else {
2366
      pColData->flag = 0;
74✔
2367
      pColData->nData = 0;
74✔
2368
      return tColDataAppendValue02(pColData, pData, nData);
74✔
2369
    }
2370
  }
2371
  return 0;
×
2372
}
2373
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,606✔
2374
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
5,606✔
2375
    pColData->numOfNone--;
5,597✔
2376
    pColData->nVal--;
5,597✔
2377
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
5,597!
2378
      pColData->nData -= TYPE_BYTES[pColData->type];
4,477✔
2379
    }
2380
    if (pColData->numOfNone) {
5,597✔
2381
      return tColDataAppendValue50(pColData, pData, nData);
2,000✔
2382
    } else {
2383
      pColData->flag = HAS_VALUE;
3,597✔
2384
      return tColDataAppendValue40(pColData, pData, nData);
3,597✔
2385
    }
2386
  } else if (forward) {  // VALUE ==> VALUE
9!
2387
    pColData->nVal--;
9✔
2388
    if (IS_VAR_DATA_TYPE(pColData->type)) {
9!
2389
      pColData->nData = pColData->aOffset[pColData->nVal];
8✔
2390
    } else {
2391
      pColData->nData -= TYPE_BYTES[pColData->type];
1✔
2392
    }
2393
    return tColDataPutValue(pColData, pData, nData);
9✔
2394
  }
2395
  return 0;
×
2396
}
2397
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
303✔
2398
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
303!
2399
    pColData->numOfNone--;
303✔
2400
    pColData->nVal--;
303✔
2401
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
303!
2402
      pColData->nData -= TYPE_BYTES[pColData->type];
243✔
2403
    }
2404
    if (pColData->numOfNone) {
303!
2405
      return tColDataAppendValue52(pColData, pData, nData);
×
2406
    } else {
2407
      pColData->flag = HAS_VALUE;
303!
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,294,884✔
2429
  if (forward) {
3,294,884!
2430
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,294,884✔
2431
      pColData->numOfNull--;
183,870✔
2432
      pColData->nVal--;
183,870✔
2433
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
183,870!
2434
        pColData->nData -= TYPE_BYTES[pColData->type];
154,387✔
2435
      }
2436
      if (pColData->numOfNull) {
183,870✔
2437
        return tColDataAppendValue60(pColData, pData, nData);
164,158✔
2438
      } else {
2439
        pColData->flag = HAS_VALUE;
19,712✔
2440
        return tColDataAppendValue40(pColData, pData, nData);
19,710✔
2441
      }
2442
    } else {  // VALUE ==> VALUE
2443
      pColData->nVal--;
3,111,014✔
2444
      if (IS_VAR_DATA_TYPE(pColData->type)) {
3,111,014!
2445
        pColData->nData = pColData->aOffset[pColData->nVal];
479,925✔
2446
      } else {
2447
        pColData->nData -= TYPE_BYTES[pColData->type];
2,631,089✔
2448
      }
2449
      return tColDataPutValue(pColData, pData, nData);
3,111,014✔
2450
    }
2451
  }
2452
  return 0;
×
2453
}
2454
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
197,251✔
2455
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
197,251!
2456
    pColData->numOfValue--;
174,149✔
2457
    pColData->nVal--;
174,149✔
2458
    if (pColData->numOfValue) {
174,149!
2459
      if (IS_VAR_DATA_TYPE(pColData->type)) {
174,149!
2460
        pColData->nData = pColData->aOffset[pColData->nVal];
27,893✔
2461
      } else {
2462
        pColData->nData -= TYPE_BYTES[pColData->type];
146,256✔
2463
      }
2464
      return tColDataAppendValue62(pColData, pData, nData);
174,149✔
2465
    } else {
UNCOV
2466
      pColData->flag = HAS_NULL;
×
UNCOV
2467
      pColData->nData = 0;
×
UNCOV
2468
      return tColDataAppendValue20(pColData, pData, nData);
×
2469
    }
2470
  }
2471
  return 0;
23,102✔
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,593✔
2528
    pColData->nVal--;
3,593✔
2529
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
3,593!
2530
      pColData->nData -= TYPE_BYTES[pColData->type];
2,544✔
2531
    }
2532
    if (pColData->numOfNone) {
3,593!
2533
      return tColDataAppendValue72(pColData, pData, nData);
×
2534
    } else {
2535
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
114,411✔
2536
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
110,818✔
2537
      }
2538
      pColData->flag = (HAS_VALUE | HAS_NULL);
3,593!
2539
      return tColDataAppendValue62(pColData, pData, nData);
3,595✔
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) {
14,491✔
2563
  return 0;
14,491✔
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,016,064✔
2578
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) return TSDB_CODE_INVALID_PARA;
4,016,064!
2579
  if (!(pColData->nVal > 0)) return TSDB_CODE_INVALID_PARA;
4,016,147!
2580

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

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

2588
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
98,128,348✔
2589
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
98,128,348✔
2590
}
98,128,348✔
2591
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,399,758✔
2592
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,399,758✔
2593
}
1,399,758✔
2594
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
11,323✔
2595
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
2596
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
11,323!
2597
    case 0:
1,030✔
2598
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,030✔
2599
      break;
1,030✔
2600
    case 1:
10,294✔
2601
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
10,294✔
2602
      break;
10,294✔
2603
    default:
×
2604
      break;
×
2605
  }
2606
}
11,323✔
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) {
236,084,147!
2611
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
240,413,653✔
2612
    } else {
2613
      value.nData = pColData->nData - pColData->aOffset[iVal];
×
2614
    }
2615
    value.pData = pColData->pData + pColData->aOffset[iVal];
236,084,147✔
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
}
198,899,604✔
2622
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
225,166✔
2623
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
2624
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
225,166!
2625
    case 0:
102,586✔
2626
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
102,586✔
2627
      break;
102,586✔
2628
    case 1:
122,786✔
2629
      tColDataGetValue4(pColData, iVal, pColVal);
2630
      break;
122,786✔
2631
    default:
×
2632
      break;
×
2633
  }
2634
}
225,166✔
2635
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
210,145,975✔
2636
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
2637
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
210,145,975!
2638
    case 0:
11,501,018✔
2639
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
11,501,018✔
2640
      break;
11,501,018✔
2641
    case 1:
198,776,817✔
2642
      tColDataGetValue4(pColData, iVal, pColVal);
2643
      break;
198,776,817✔
2644
    default:
×
2645
      break;
×
2646
  }
2647
}
210,145,975✔
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) {
311,704,247✔
2684
  switch (pColData->flag) {
311,704,247!
2685
    case HAS_NONE:
×
2686
      return 0;
×
2687
    case HAS_NULL:
×
2688
      return 1;
×
2689
    case (HAS_NULL | HAS_NONE):
11,330✔
2690
      return GET_BIT1(pColData->pBitMap, iVal);
11,330✔
2691
    case HAS_VALUE:
×
2692
      return 2;
×
2693
    case (HAS_VALUE | HAS_NONE):
607,283✔
2694
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
607,283✔
2695
    case (HAS_VALUE | HAS_NULL):
311,115,249✔
2696
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
311,115,249✔
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) {
447✔
2705
  int32_t code = 0;
447✔
2706

2707
  *pColData = *pColDataFrom;
447✔
2708

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

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

2746
  // value
2747
  if (pColData->nData) {
447✔
2748
    pColData->pData = xMalloc(arg, pColData->nData);
360✔
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;
87✔
2757
  }
2758

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

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

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

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

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

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

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

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

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

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

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

2817
  // offset
2818
  if (IS_VAR_DATA_TYPE(colData->type)) {
2,475,358!
2819
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
325,468✔
2820

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

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

2833
    info->offsetCompressedSize = cinfo.compressedSize;
325,649✔
2834
  }
2835

2836
  // data
2837
  if (colData->nData > 0) {
2,475,539!
2838
    info->dataOriginalSize = colData->nData;
2,475,574✔
2839

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

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

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

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

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

2864
  tBufferInit(&local);
2865
  if (assist == NULL) {
20,546,428!
2866
    assist = &local;
×
2867
  }
2868

2869
  tColDataClear(colData);
20,546,428✔
2870
  colData->cid = info->columnId;
20,544,880✔
2871
  colData->type = info->dataType;
20,544,880✔
2872
  colData->cflag = info->columnFlag;
20,544,880✔
2873
  colData->nVal = info->numOfData;
20,544,880✔
2874
  colData->flag = info->flag;
20,544,880✔
2875

2876
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
20,544,880✔
2877
    goto _exit;
2,486,209✔
2878
  }
2879

2880
  // bitmap
2881
  if (info->bitmapOriginalSize > 0) {
18,058,671✔
2882
    SCompressInfo cinfo = {
701,541✔
2883
        .dataType = TSDB_DATA_TYPE_TINYINT,
2884
        .cmprAlg = info->cmprAlg,
701,541✔
2885
        .originalSize = info->bitmapOriginalSize,
701,541✔
2886
        .compressedSize = info->bitmapCompressedSize,
701,541✔
2887
    };
2888

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

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

2901
    data += cinfo.compressedSize;
701,499✔
2902
  }
2903

2904
  if (info->flag == (HAS_NONE | HAS_NULL)) {
18,058,629✔
2905
    goto _exit;
606✔
2906
  }
2907

2908
  // offset
2909
  if (info->offsetOriginalSize > 0) {
18,058,023✔
2910
    SCompressInfo cinfo = {
3,043,384✔
2911
        .cmprAlg = info->cmprAlg,
3,043,384✔
2912
        .dataType = TSDB_DATA_TYPE_INT,
2913
        .originalSize = info->offsetOriginalSize,
3,043,384✔
2914
        .compressedSize = info->offsetCompressedSize,
3,043,384✔
2915
    };
2916

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

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

2929
    data += cinfo.compressedSize;
3,043,377✔
2930
  }
2931

2932
  // data
2933
  if (info->dataOriginalSize > 0) {
18,058,016✔
2934
    colData->nData = info->dataOriginalSize;
18,057,532✔
2935

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

2943
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
18,057,532!
2944
    if (code) {
18,057,585!
2945
      tBufferDestroy(&local);
2946
      return code;
×
2947
    }
2948

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

2955
    data += cinfo.compressedSize;
18,057,092✔
2956
  }
2957

2958
_exit:
484✔
2959
  switch (colData->flag) {
20,544,391✔
2960
    case HAS_NONE:
2,384,735✔
2961
      colData->numOfNone = colData->nVal;
2,384,735✔
2962
      break;
2,384,735✔
2963
    case HAS_NULL:
103,248✔
2964
      colData->numOfNull = colData->nVal;
103,248✔
2965
      break;
103,248✔
2966
    case HAS_VALUE:
17,355,974✔
2967
      colData->numOfValue = colData->nVal;
17,355,974✔
2968
      break;
17,355,974✔
2969
    default:
700,434✔
2970
      for (int32_t i = 0; i < colData->nVal; i++) {
194,544,655✔
2971
        uint8_t bitValue = tColDataGetBitValue(colData, i);
193,844,513✔
2972
        if (bitValue == 0) {
193,844,221✔
2973
          colData->numOfNone++;
195,353✔
2974
        } else if (bitValue == 1) {
193,648,868✔
2975
          colData->numOfNull++;
10,934,861✔
2976
        } else {
2977
          colData->numOfValue++;
182,714,007✔
2978
        }
2979
      }
2980
  }
2981
  tBufferDestroy(&local);
2982
  return 0;
20,544,099✔
2983
}
2984

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

2999
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
566!
3000
    for (int32_t i = 0; i < nRows; ++i) {
273✔
3001
      int32_t offset = *((int32_t *)lengthOrbitmap + i);
172✔
3002
      if (offset == -1) {
172!
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) {
172!
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),
172✔
3018
                                                                      varDataLen(data + offset));
172✔
3019
      }
3020
    }
3021
  } else {  // fixed-length data type
3022
    bool allValue = true;
465✔
3023
    bool allNull = true;
465✔
3024
    for (int32_t i = 0; i < nRows; ++i) {
1,274✔
3025
      if (!colDataIsNull_f(lengthOrbitmap, i)) {
809✔
3026
        allNull = false;
596✔
3027
      } else {
3028
        allValue = false;
213✔
3029
      }
3030
    }
3031
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
465!
3032
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
3033
      goto _exit;
×
3034
    }
3035

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

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

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

3073
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
2,248,830!
3074
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
303,226!
3075
      code = igeos();
×
3076
      if (code) {
×
3077
        return code;
×
3078
      }
3079
    }
3080
    for (int32_t i = 0; i < pBind->num; ++i) {
904,739✔
3081
      if (pBind->is_null && pBind->is_null[i]) {
601,528✔
3082
        if (pColData->cflag & COL_IS_KEY) {
2,550!
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,550✔
3087
        if (code) goto _exit;
2,550!
3088
      } else if (pBind->length[i] > buffMaxLen) {
598,978✔
3089
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
9✔
3090
      } else {
3091
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
598,969!
3092
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
×
3093
          if (code) goto _exit;
×
3094
        }
3095
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
598,969✔
3096
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
598,969✔
3097
      }
3098
    }
3099
  } else {  // fixed-length data type
3100
    bool allValue;
3101
    bool allNull;
3102
    if (pBind->is_null) {
1,945,604✔
3103
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
15,873✔
3104
      allNull = (same && pBind->is_null[0] != 0);
15,873✔
3105
      allValue = (same && pBind->is_null[0] == 0);
15,873✔
3106
    } else {
3107
      allNull = false;
1,929,731✔
3108
      allValue = true;
1,929,731✔
3109
    }
3110

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

3116
    if (allValue) {
1,945,604✔
3117
      // optimize (todo)
3118
      for (int32_t i = 0; i < pBind->num; ++i) {
5,785,376✔
3119
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
3,855,251✔
3120
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
3,855,251✔
3121
      }
3122
    } else if (allNull) {
15,504✔
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,317✔
3130
        if (pBind->is_null[i]) {
31,814✔
3131
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
15,907✔
3132
          if (code) goto _exit;
15,907!
3133
        } else {
3134
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
15,907✔
3135
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
15,907✔
3136
        }
3137
      }
3138
    }
3139
  }
3140

3141
_exit:
15,503✔
3142
  return code;
2,248,840✔
3143
}
3144

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

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

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

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

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

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

3216
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
×
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;
×
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,
×
3266
                           SArray *rowArray, bool *pOrdered, bool *pDupTs) {
3267
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
×
3268
    return TSDB_CODE_INVALID_PARA;
×
3269
  }
3270

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

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

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

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

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

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

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

3365
_exit:
×
3366
  taosArrayDestroy(colValArray);
×
3367
  taosArrayDestroy(bufArray);
×
3368
  return code;
×
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,069,463✔
3458
  SColVal cv;
3459

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

3463
  for (int i = 1; i < nColData; i++) {
1,069,463!
3464
    if (aColData[i].cflag & COL_IS_KEY) {
1,069,468!
3465
      tColDataGetValue4(&aColData[i], iRow, &cv);
×
3466
      key->pks[key->numOfPKs++] = cv.value;
×
3467
    } else {
3468
      break;
1,069,468✔
3469
    }
3470
  }
3471
}
1,069,463✔
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,192✔
3625
  SArray   *colDataArr = *arr;
92,192✔
3626
  int32_t   nColData = TARRAY_SIZE(colDataArr);
92,192✔
3627
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
92,192✔
3628

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

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

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

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

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

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

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

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

3688
_exit:
90,493✔
3689
  return 0;
92,192✔
3690
}
3691

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

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

3700
  // bitmap
3701
  switch (pColData->flag) {
1,604,706!
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,587,560✔
3713
      break;
1,587,560✔
3714
  }
3715

3716
  // value
3717
  if (pColData->flag & HAS_VALUE) {
1,604,706✔
3718
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,604,551!
3719
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
216,450✔
3720
      if (code) return code;
216,450!
3721

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

3725
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
216,450✔
3726
      if (code) return code;
216,450!
3727
    } else {
3728
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
1,388,101✔
3729
      if (code) return code;
1,388,101!
3730
    }
3731
  }
3732

3733
  return code;
1,604,706✔
3734
}
3735

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

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

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

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

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

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

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

3783
  return code;
2,027✔
3784
}
3785

3786
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
1,604,706✔
3787
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
1,604,706✔
3788
  if (code) return code;
1,604,724!
3789
  return tEncodeI8(pEncoder, pColData->cflag);
3,209,448✔
3790
}
3791

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

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

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

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

3827
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
859,873,754!
3828
    return TSDB_CODE_OUT_OF_RANGE;
×
3829
  }
3830

3831
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
859,873,754✔
3832
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
1,719,747,508!
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,080✔
3843
                                             int16_t *numOfNull) {
3844
  *sum = 0;
7,080✔
3845
  *max = 0;
7,080✔
3846
  *min = 1;
7,080✔
3847
  *numOfNull = 0;
7,080✔
3848

3849
  int8_t val;
3850
  if (HAS_VALUE == pColData->flag) {
7,080✔
3851
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,241,471✔
3852
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
4,235,860✔
3853
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,235,860✔
3854
    }
3855
  } else {
3856
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,150✔
3857
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,681!
3858
        case 0:
237,771✔
3859
        case 1:
3860
          (*numOfNull)++;
237,771✔
3861
          break;
237,771✔
3862
        case 2:
4,505,910✔
3863
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
4,505,910✔
3864
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,505,910✔
3865
          break;
4,505,910✔
3866
        default:
×
3867
          break;
×
3868
      }
3869
    }
3870
  }
3871
}
7,080✔
3872

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

3880
  int8_t val;
3881
  if (HAS_VALUE == pColData->flag) {
7,116✔
3882
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,360,847✔
3883
      val = ((int8_t *)pColData->pData)[iVal];
4,355,199✔
3884
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,355,199✔
3885
    }
3886
  } else {
3887
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,149✔
3888
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,681!
3889
        case 0:
236,790✔
3890
        case 1:
3891
          (*numOfNull)++;
236,790✔
3892
          break;
236,790✔
3893
        case 2:
4,506,891✔
3894
          val = ((int8_t *)pColData->pData)[iVal];
4,506,891✔
3895
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,506,891✔
3896
          break;
4,506,891✔
3897
        default:
×
3898
          break;
×
3899
      }
3900
    }
3901
  }
3902
}
7,116✔
3903

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

3911
  int16_t val;
3912
  if (HAS_VALUE == pColData->flag) {
7,116✔
3913
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,360,691✔
3914
      val = ((int16_t *)pColData->pData)[iVal];
4,355,043✔
3915
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,355,043✔
3916
    }
3917
  } else {
3918
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,149✔
3919
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,681!
3920
        case 0:
237,197✔
3921
        case 1:
3922
          (*numOfNull)++;
237,197✔
3923
          break;
237,197✔
3924
        case 2:
4,506,484✔
3925
          val = ((int16_t *)pColData->pData)[iVal];
4,506,484✔
3926
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,506,484✔
3927
          break;
4,506,484✔
3928
        default:
×
3929
          break;
×
3930
      }
3931
    }
3932
  }
3933
}
7,116✔
3934

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

3942
  int32_t val;
3943
  if (HAS_VALUE == pColData->flag) {
253,449✔
3944
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
792,808,882✔
3945
      val = ((int32_t *)pColData->pData)[iVal];
792,582,194✔
3946
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
792,582,194✔
3947
    }
3948
  } else {
3949
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
70,814,775✔
3950
      switch (tColDataGetBitValue(pColData, iVal)) {
70,803,924!
3951
        case 0:
3,558,744✔
3952
        case 1:
3953
          (*numOfNull)++;
3,558,744✔
3954
          break;
3,558,744✔
3955
        case 2:
67,262,452✔
3956
          val = ((int32_t *)pColData->pData)[iVal];
67,262,452✔
3957
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
67,262,452✔
3958
          break;
67,262,452✔
3959
        default:
×
3960
          break;
×
3961
      }
3962
    }
3963
  }
3964
}
237,539✔
3965

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

3973
  int64_t val;
3974
  if (HAS_VALUE == pColData->flag) {
84,459✔
3975
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
241,131,284✔
3976
      val = ((int64_t *)pColData->pData)[iVal];
241,048,755✔
3977
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
241,048,755✔
3978
    }
3979
  } else {
3980
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,835,293✔
3981
      switch (tColDataGetBitValue(pColData, iVal)) {
4,833,363!
3982
        case 0:
312,881✔
3983
        case 1:
3984
          (*numOfNull)++;
312,881✔
3985
          break;
312,881✔
3986
        case 2:
4,520,482✔
3987
          val = ((int64_t *)pColData->pData)[iVal];
4,520,482✔
3988
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
4,520,482✔
3989
          break;
4,520,482✔
3990
        default:
×
3991
          break;
×
3992
      }
3993
    }
3994
  }
3995
}
84,459✔
3996

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

4004
  float val;
4005
  if (HAS_VALUE == pColData->flag) {
215,313✔
4006
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
855,861,453✔
4007
      val = ((float *)pColData->pData)[iVal];
855,647,609✔
4008
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
855,647,609✔
4009
    }
4010
  } else {
4011
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,255✔
4012
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,786!
4013
        case 0:
237,084✔
4014
        case 1:
4015
          (*numOfNull)++;
237,084✔
4016
          break;
237,084✔
4017
        case 2:
4,506,702✔
4018
          val = ((float *)pColData->pData)[iVal];
4,506,702✔
4019
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
4,506,702✔
4020
          break;
4,506,702✔
4021
        default:
×
4022
          break;
×
4023
      }
4024
    }
4025
  }
4026
}
215,313✔
4027

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

4035
  double val;
4036
  if (HAS_VALUE == pColData->flag) {
8,242✔
4037
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
11,108,632✔
4038
      val = ((double *)pColData->pData)[iVal];
11,101,858✔
4039
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
11,101,858✔
4040
    }
4041
  } else {
4042
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,149✔
4043
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,681!
4044
        case 0:
236,425✔
4045
        case 1:
4046
          (*numOfNull)++;
236,425✔
4047
          break;
236,425✔
4048
        case 2:
4,507,256✔
4049
          val = ((double *)pColData->pData)[iVal];
4,507,256✔
4050
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
4,507,256✔
4051
          break;
4,507,256✔
4052
        default:
×
4053
          break;
×
4054
      }
4055
    }
4056
  }
4057
}
8,242✔
4058

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

4066
  uint8_t val;
4067
  if (HAS_VALUE == pColData->flag) {
4,201✔
4068
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,476,893✔
4069
      val = ((uint8_t *)pColData->pData)[iVal];
3,474,160✔
4070
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,474,160✔
4071
    }
4072
  } else {
4073
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,149✔
4074
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,681!
4075
        case 0:
237,081✔
4076
        case 1:
4077
          (*numOfNull)++;
237,081✔
4078
          break;
237,081✔
4079
        case 2:
4,506,600✔
4080
          val = ((uint8_t *)pColData->pData)[iVal];
4,506,600✔
4081
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,506,600✔
4082
          break;
4,506,600✔
4083
        default:
×
4084
          break;
×
4085
      }
4086
    }
4087
  }
4088
}
4,201✔
4089

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

4097
  uint16_t val;
4098
  if (HAS_VALUE == pColData->flag) {
4,201✔
4099
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,476,893✔
4100
      val = ((uint16_t *)pColData->pData)[iVal];
3,474,160✔
4101
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,474,160✔
4102
    }
4103
  } else {
4104
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,149✔
4105
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,681!
4106
        case 0:
237,624✔
4107
        case 1:
4108
          (*numOfNull)++;
237,624✔
4109
          break;
237,624✔
4110
        case 2:
4,506,057✔
4111
          val = ((uint16_t *)pColData->pData)[iVal];
4,506,057✔
4112
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,506,057✔
4113
          break;
4,506,057✔
4114
        default:
×
4115
          break;
×
4116
      }
4117
    }
4118
  }
4119
}
4,201✔
4120

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

4128
  uint32_t val;
4129
  if (HAS_VALUE == pColData->flag) {
4,201✔
4130
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,476,893✔
4131
      val = ((uint32_t *)pColData->pData)[iVal];
3,474,160✔
4132
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,474,160✔
4133
    }
4134
  } else {
4135
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,149✔
4136
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,681!
4137
        case 0:
238,188✔
4138
        case 1:
4139
          (*numOfNull)++;
238,188✔
4140
          break;
238,188✔
4141
        case 2:
4,505,493✔
4142
          val = ((uint32_t *)pColData->pData)[iVal];
4,505,493✔
4143
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,505,493✔
4144
          break;
4,505,493✔
4145
        default:
×
4146
          break;
×
4147
      }
4148
    }
4149
  }
4150
}
4,201✔
4151

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

4159
  uint64_t val;
4160
  if (HAS_VALUE == pColData->flag) {
4,201✔
4161
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
3,476,893✔
4162
      val = ((uint64_t *)pColData->pData)[iVal];
3,474,160✔
4163
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
3,474,160✔
4164
    }
4165
  } else {
4166
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
4,745,149✔
4167
      switch (tColDataGetBitValue(pColData, iVal)) {
4,743,681!
4168
        case 0:
238,217✔
4169
        case 1:
4170
          (*numOfNull)++;
238,217✔
4171
          break;
238,217✔
4172
        case 2:
4,505,464✔
4173
          val = ((uint64_t *)pColData->pData)[iVal];
4,505,464✔
4174
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
4,505,464✔
4175
          break;
4,505,464✔
4176
        default:
×
4177
          break;
×
4178
      }
4179
    }
4180
  }
4181
}
4,201✔
4182

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

4190
  switch (pColData->flag) {
20,391!
4191
    case HAS_NONE:
×
4192
    case HAS_NULL:
4193
    case (HAS_NONE | HAS_NULL):
4194
      *numOfNull = pColData->nVal;
×
4195
      break;
×
4196
    case HAS_VALUE:
17,456✔
4197
      *numOfNull = 0;
17,456✔
4198
      break;
17,456✔
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,490,298✔
4202
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
9,487,362✔
4203
          (*numOfNull)++;
474,971✔
4204
        }
4205
      }
4206
      break;
2,936✔
4207
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
4208
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
4209
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
4210
          (*numOfNull)++;
×
4211
        }
4212
      }
4213
      break;
×
4214
    default:
×
4215
      break;
×
4216
  }
4217
}
20,391✔
4218

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

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

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

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

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

4271
  if (valCol->numOfValues == 0) {
878,361✔
4272
    valCol->type = value->type;
4,188✔
4273
  }
4274

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

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

4292
  return 0;
878,361✔
4293
}
4294

4295
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
237,632,594✔
4296
  int32_t code;
4297

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

4302
  if (IS_VAR_DATA_TYPE(valCol->type)) {
237,642,806!
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,
237,685,169✔
4321
                        tDataTypes[valCol->type].bytes);
237,684,738✔
4322
  }
4323
  return 0;
4324
}
4325

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

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

4336
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
×
4337
    if (idx == valCol->numOfValues - 1) {
106✔
4338
      nextOffset = tBufferGetSize(&valCol->data);
102✔
4339
    } else {
4340
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
4!
4341
    }
4342
    value->nData = nextOffset - offset;
106✔
4343
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
106✔
4344
  } else {
4345
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
479,704,142✔
4346
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, &value->val));
959,408,284!
4347
  }
4348
  return 0;
479,704,248✔
4349
}
4350

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

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

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

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

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

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

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

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

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

4391
  return 0;
4,188✔
4392
}
4393

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

4398
  tValueColumnClear(valCol);
43,300✔
4399
  valCol->type = info->type;
43,300✔
4400
  // offset
4401
  if (IS_VAR_DATA_TYPE(valCol->type)) {
43,306!
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;
43,294✔
4417
  }
4418

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

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

4432
  return 0;
43,300✔
4433
}
4434

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

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

4449
  return 0;
4,187✔
4450
}
4451

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

4456
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
43,300!
4457
  if (fmtVer == 0) {
43,300!
4458
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
43,300!
4459
    if ((code = tBufferGetI8(reader, &info->type))) return code;
43,299!
4460
    if (IS_VAR_DATA_TYPE(info->type)) {
43,299!
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;
43,293✔
4465
      info->offsetCompressedSize = 0;
43,293✔
4466
    }
4467
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
43,299!
4468
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
43,299!
4469
  } else {
4470
    return TSDB_CODE_INVALID_PARA;
×
4471
  }
4472

4473
  return 0;
43,299✔
4474
}
4475

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

4490
  if (info->cmprAlg == NO_COMPRESSION) {
5,853,796!
4491
    (void)memcpy(output, input, info->originalSize);
×
4492
    info->compressedSize = info->originalSize;
×
4493
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
7,285,042✔
4494
    SBuffer local;
4495

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

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

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

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

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

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

4559
  return 0;
5,856,750✔
4560
}
4561

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

4570
  if (!(outputSize >= info->originalSize)) {
68,794,954!
4571
    return TSDB_CODE_INVALID_PARA;
×
4572
  }
4573

4574
  if (info->cmprAlg == NO_COMPRESSION) {
68,794,954!
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) {
96,072,435!
4580
    SBuffer local;
4581

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

4587
    if (info->cmprAlg == TWO_STAGE_COMP) {
27,252,496!
4588
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
27,279,949✔
4589
      if (code) {
27,281,348!
4590
        tBufferDestroy(&local);
4591
        return code;
×
4592
      }
4593
    }
4594

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

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

4622
    tBufferInit(&local);
4623
    if (buffer == NULL) {
41,542,458!
4624
      buffer = &local;
×
4625
    }
4626
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
41,542,458✔
4627
    if (code) {
41,543,093!
4628
      return code;
×
4629
    }
4630

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

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

4652
  return 0;
68,823,006✔
4653
}
4654

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

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

4661
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
5,854,401✔
4662
  if (code) return code;
5,855,057!
4663

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

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

4671
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
27,276,027✔
4672
  if (code) return code;
27,275,387!
4673

4674
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
27,275,387✔
4675
  if (code) return code;
27,277,127!
4676

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