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

taosdata / TDengine / #4814

16 Oct 2025 11:40AM UTC coverage: 58.728% (+0.03%) from 58.702%
#4814

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

139974 of 303532 branches covered (46.12%)

Branch coverage included in aggregate %.

211648 of 295200 relevant lines covered (71.7%)

23886139.71 hits per line

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

44.69
/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 "decimal.h"
19
#include "tRealloc.h"
20
#include "tdatablock.h"
21
#include "tlog.h"
22

23
enum {
24
  BLOB_ROW_KV = 0,
25
  BLOB_ROW_TUPLE = 1,
26
};
27

28
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData);
29
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward);
30

31
static int32_t tRowMergeWithBlobImpl(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob, SBlobSet *pDstBlobSet,
32
                                     int32_t iStart, int32_t iEnd, int8_t flag);
33

34
static int32_t tRowBuildTupleWithBlob2(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
35
                                       SRow **ppRow, SBlobSet *pSrcBlobSet, SBlobSet *pDstBlobSet);
36

37
static int32_t tRowBuildKVRowWithBlob2(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
38
                                       SRow **ppRow, SBlobSet *pSrcBlob, SBlobSet *pDstBlobSet);
39
// ================================
40
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson);
41

42
// SRow ========================================================================
43
#define KV_FLG_LIT ((uint8_t)0x10)
44
#define KV_FLG_MID ((uint8_t)0x20)
45
#define KV_FLG_BIG ((uint8_t)0x40)
46

47
#define BIT_FLG_NONE  ((uint8_t)0x0)
48
#define BIT_FLG_NULL  ((uint8_t)0x1)
49
#define BIT_FLG_VALUE ((uint8_t)0x2)
50

51
#pragma pack(push, 1)
52
typedef struct {
53
  int16_t nCol;
54
  uint8_t idx[];  // uint8_t * | uint16_t * | uint32_t *
55
} SKVIdx;
56
#pragma pack(pop)
57

58
#define ROW_SET_BITMAP(PB, FLAG, IDX, VAL)      \
59
  do {                                          \
60
    switch (FLAG) {                             \
61
      case (HAS_NULL | HAS_NONE):               \
62
        SET_BIT1(PB, IDX, VAL);                 \
63
        break;                                  \
64
      case (HAS_VALUE | HAS_NONE):              \
65
        SET_BIT1(PB, IDX, (VAL) ? (VAL)-1 : 0); \
66
        break;                                  \
67
      case (HAS_VALUE | HAS_NULL):              \
68
        SET_BIT1(PB, IDX, (VAL)-1);             \
69
        break;                                  \
70
      case (HAS_VALUE | HAS_NULL | HAS_NONE):   \
71
        SET_BIT2(PB, IDX, VAL);                 \
72
        break;                                  \
73
      default:                                  \
74
        break;                                  \
75
    }                                           \
76
  } while (0)
77

78
static int32_t tPutPrimaryKeyIndex(uint8_t *p, const SPrimaryKeyIndex *index) {
1,247,332,289✔
79
  int32_t n = 0;
1,247,332,289✔
80
  n += tPutI8(p ? p + n : p, index->type);
1,247,332,289✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
1,247,332,289✔
82
  return n;
1,247,332,289✔
83
}
84

85
static int32_t tGetPrimaryKeyIndex(uint8_t *p, SPrimaryKeyIndex *index) {
2,147,483,647✔
86
  int32_t n = 0;
2,147,483,647✔
87
  n += tGetI8(p + n, &index->type);
2,147,483,647!
88
  n += tGetU32v(p + n, &index->offset);
2,147,483,647!
89
  return n;
2,147,483,647✔
90
}
91

92
static FORCE_INLINE int32_t tRowBuildScanAddNone(SRowBuildScanInfo *sinfo, const STColumn *pTColumn) {
93
  if ((pTColumn->flags & COL_IS_KEY)) return TSDB_CODE_PAR_PRIMARY_KEY_IS_NONE;
×
94
  sinfo->numOfNone++;
2,147,483,647✔
95
  return 0;
2,147,483,647✔
96
}
97

98
static FORCE_INLINE int32_t tRowBuildScanAddNull(SRowBuildScanInfo *sinfo, const STColumn *pTColumn) {
99
  if ((pTColumn->flags & COL_IS_KEY)) return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
12✔
100
  sinfo->numOfNull++;
15,101,571✔
101
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
15,101,571✔
102
  sinfo->kvPayloadSize += tPutI16v(NULL, -pTColumn->colId);
15,101,571✔
103
  return 0;
15,101,571✔
104
}
105

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

109
  if (isPK) {
2,147,483,647✔
110
    sinfo->tupleIndices[sinfo->numOfPKs].type = colVal->value.type;
423,555,458✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
423,555,458✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
423,555,458!
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
423,555,458✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
423,555,458✔
115
    sinfo->numOfPKs++;
423,555,458✔
116
  }
117

118
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
2,147,483,647✔
119
  if (IS_VAR_DATA_TYPE(colVal->value.type)) {
2,147,483,647!
120
    if (sinfo->hasBlob && IS_STR_DATA_BLOB(colVal->value.type)) {
1,249,591,478!
121
      sinfo->tupleVarSize += tPutU32v(NULL, colVal->value.nData)     // size
×
122
                             + BSE_SEQUECE_SIZE;                     // value
×
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
×
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
×
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
×
126
    } else {
127
      sinfo->tupleVarSize += tPutU32v(NULL, colVal->value.nData)  // size
1,249,591,478✔
128
                             + colVal->value.nData;               // value
1,249,591,478✔
129

130
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
1,249,591,478✔
131
                              + tPutU32v(NULL, colVal->value.nData)  // size
1,249,591,478✔
132
                              + colVal->value.nData;                 // value
1,249,591,478✔
133
    }
134
  } else {
135
    sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)              // colId
2,147,483,647✔
136
                            + tDataTypes[colVal->value.type].bytes;  // value
2,147,483,647✔
137
  }
138
  sinfo->numOfValue++;
2,147,483,647✔
139
}
2,147,483,647✔
140

141
static int32_t tRowBuildScan(SArray *colVals, const STSchema *schema, SRowBuildScanInfo *sinfo) {
2,147,483,647✔
142
  int32_t  code = 0;
2,147,483,647✔
143
  int32_t  colValIndex = 1;
2,147,483,647✔
144
  int32_t  numOfColVals = TARRAY_SIZE(colVals);
2,147,483,647✔
145
  SColVal *colValArray = (SColVal *)TARRAY_DATA(colVals);
2,147,483,647✔
146

147
  if (!(numOfColVals > 0)) {
2,147,483,647!
148
    return TSDB_CODE_INVALID_PARA;
×
149
  }
150
  if (!(colValArray[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
2,147,483,647!
151
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
152
  }
153
  if (!(colValArray[0].value.type == TSDB_DATA_TYPE_TIMESTAMP)) {
2,147,483,647!
154
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
155
  }
156

157
  *sinfo = (SRowBuildScanInfo){.tupleFixedSize = schema->flen, .hasBlob = sinfo->hasBlob, .scanType = sinfo->scanType};
2,147,483,647✔
158

159
  // loop scan
160
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
161
    for (;;) {
162
      if (colValIndex >= numOfColVals) {
2,147,483,647✔
163
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
644!
164
        break;
322✔
165
      }
166

167
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
168
        if (!(colValArray[colValIndex].value.type == schema->columns[i].type)) {
2,147,483,647!
169
          code = TSDB_CODE_INVALID_PARA;
×
170
          goto _exit;
×
171
        }
172

173
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {
2,147,483,647✔
174
          tRowBuildScanAddValue(sinfo, &colValArray[colValIndex], schema->columns + i);
2,147,483,647✔
175
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {
2,147,483,647✔
176
          if ((code = tRowBuildScanAddNull(sinfo, schema->columns + i))) goto _exit;
30,203,166✔
177
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {
2,147,483,647!
178
          if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
2,147,483,647!
179
        }
180

181
        colValIndex++;
2,147,483,647✔
182
        break;
2,147,483,647✔
183
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {
605,995✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
464!
185
        break;
232✔
186
      } else {  // skip useless value
187
        colValIndex++;
605,763✔
188
      }
189
    }
190
  }
191

192
  if (sinfo->numOfNone) {
2,147,483,647✔
193
    sinfo->flag |= HAS_NONE;
870,185,843✔
194
  }
195
  if (sinfo->numOfNull) {
2,147,483,647✔
196
    sinfo->flag |= HAS_NULL;
14,275,870✔
197
  }
198
  if (sinfo->numOfValue) {
2,147,483,647✔
199
    sinfo->flag |= HAS_VALUE;
2,147,483,647✔
200
  }
201

202
  // Tuple
203
  sinfo->tupleFlag = sinfo->flag;
2,147,483,647✔
204
  switch (sinfo->flag) {
2,147,483,647!
205
    case HAS_NONE:
35,107,386✔
206
    case HAS_NULL:
207
      sinfo->tupleBitmapSize = 0;
35,107,386✔
208
      sinfo->tupleFixedSize = 0;
35,107,386✔
209
      break;
35,107,386✔
210
    case HAS_VALUE:
1,327,492,487✔
211
      sinfo->tupleBitmapSize = 0;
1,327,492,487✔
212
      sinfo->tupleFixedSize = schema->flen;
1,327,492,487✔
213
      break;
1,327,492,487✔
214
    case (HAS_NONE | HAS_NULL):
4,662✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
4,662✔
216
      sinfo->tupleFixedSize = 0;
4,662✔
217
      break;
4,662✔
218
    case (HAS_NONE | HAS_VALUE):
849,538,608✔
219
    case (HAS_NULL | HAS_VALUE):
220
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
849,538,608✔
221
      sinfo->tupleFixedSize = schema->flen;
849,538,608✔
222
      break;
849,538,608✔
223
    case (HAS_NONE | HAS_NULL | HAS_VALUE):
86,257✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
86,257✔
225
      sinfo->tupleFixedSize = schema->flen;
86,257✔
226
      break;
86,257✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
422,153,108✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
422,153,108✔
231
  }
232
  sinfo->tupleRowSize = sizeof(SRow)              // SRow
2,147,483,647✔
233
                        + sinfo->tuplePKSize      // primary keys
2,147,483,647✔
234
                        + sinfo->tupleBitmapSize  // bitmap
2,147,483,647✔
235
                        + sinfo->tupleFixedSize   // fixed part
2,147,483,647✔
236
                        + sinfo->tupleVarSize;    // var part
2,147,483,647✔
237

238
  // Key-Value
239
  if (sinfo->kvMaxOffset <= UINT8_MAX) {
2,147,483,647!
240
    sinfo->kvFlag = (KV_FLG_LIT | sinfo->flag);
2,147,483,647✔
241
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint8_t);
2,147,483,647✔
242
  } else if (sinfo->kvMaxOffset <= UINT16_MAX) {
×
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
984,830✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
984,830✔
245
  } else {
246
    sinfo->kvFlag = (KV_FLG_BIG | sinfo->flag);
×
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint32_t);
×
248
  }
249
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
250
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
420,917,638✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
420,917,638✔
252
  }
253
  sinfo->kvRowSize = sizeof(SRow)             // SRow
2,147,483,647✔
254
                     + sinfo->kvPKSize        // primary keys
2,147,483,647✔
255
                     + sinfo->kvIndexSize     // index array
2,147,483,647✔
256
                     + sinfo->kvPayloadSize;  // payload
2,147,483,647✔
257

258
_exit:
2,147,483,647✔
259
  return code;
2,147,483,647✔
260
}
261

262
static int32_t tRowBuildTupleRow(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
1,373,856,953✔
263
                                 SRow **ppRow) {
264
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
1,373,856,953✔
265
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->tupleRowSize);
1,373,856,953!
266
  if (*ppRow == NULL) {
1,382,146,104!
267
    return terrno;
×
268
  }
269
  (*ppRow)->flag = sinfo->tupleFlag;
1,382,146,104✔
270
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
1,382,146,104✔
271
  (*ppRow)->sver = schema->version;
1,382,146,104✔
272
  (*ppRow)->len = sinfo->tupleRowSize;
1,382,146,104✔
273
  (*ppRow)->ts = VALUE_GET_TRIVIAL_DATUM(&colValArray[0].value);
1,382,146,104✔
274

275
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
1,382,146,104!
276
    return 0;
33,565,424✔
277
  }
278

279
  uint8_t *primaryKeys = (*ppRow)->data;
1,348,580,680✔
280
  uint8_t *bitmap = primaryKeys + sinfo->tuplePKSize;
1,348,580,680✔
281
  uint8_t *fixed = bitmap + sinfo->tupleBitmapSize;
1,348,580,680✔
282
  uint8_t *varlen = fixed + sinfo->tupleFixedSize;
1,348,580,680✔
283

284
  // primary keys
285
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
1,771,618,114✔
286
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
427,940,461✔
287
  }
288

289
  // bitmap + fixed + varlen
290
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
1,343,677,653✔
291
  int32_t colValIndex = 1;
1,343,677,653✔
292
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
293
    for (;;) {
294
      if (colValIndex >= numOfColVals) {  // NONE
2,147,483,647✔
295
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
22!
296
        break;
22✔
297
      }
298

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

303
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647!
304
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
63,426,877✔
305
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
63,426,877✔
306
            if (colValArray[colValIndex].value.nData) {
63,426,877!
307
              (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
64,578,716✔
308
              varlen += colValArray[colValIndex].value.nData;
64,578,716✔
309
            }
310
          } else {
311
            (void)memcpy(fixed + schema->columns[i].offset,
2,147,483,647✔
312
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,147,483,647!
313
                         tDataTypes[schema->columns[i].type].bytes);
2,147,483,647!
314
          }
315
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
13,754,384!
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
14,321,242!
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
86,534!
319
        }
320

321
        colValIndex++;
2,147,483,647✔
322
        break;
2,147,483,647✔
323
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
4!
325
        break;
4✔
326
      } else {
327
        colValIndex++;
×
328
      }
329
    }
330
  }
331

332
  return 0;
1,343,677,653✔
333
}
334

335
static int32_t addEmptyItemToBlobSet(SBlobSet *pBlobSet, int8_t type, uint64_t *pSeq) {
×
336
  SBlobItem item = {.type = type, .len = 0};
×
337
  uint64_t  seq = 0;
×
338
  int32_t   code = tBlobSetPush(pBlobSet, &item, &seq, 0);
×
339
  if (code != 0) return code;
×
340

341
  if (pSeq != NULL) {
×
342
    *pSeq = seq;
×
343
  }
344
  return 0;
×
345
}
346
static int32_t tRowBuildTupleWithBlob(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
×
347
                                      SRow **ppRow, SBlobSet *pBlobSet) {
348
  int32_t  code = 0;
×
349
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
×
350
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->tupleRowSize);
×
351
  if (*ppRow == NULL) {
×
352
    return terrno;
×
353
  }
354
  (*ppRow)->flag = sinfo->tupleFlag;
×
355
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
×
356
  (*ppRow)->sver = schema->version;
×
357
  (*ppRow)->len = sinfo->tupleRowSize;
×
358
  (*ppRow)->ts = colValArray[0].value.val;
×
359

360
  if (((*ppRow)->flag) == 0) {
×
361
    return TSDB_CODE_INVALID_PARA;
×
362
  }
363
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
×
364
    code = addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
365
    return code;
×
366
  }
367

368
  uint8_t *primaryKeys = (*ppRow)->data;
×
369
  uint8_t *bitmap = primaryKeys + sinfo->tuplePKSize;
×
370
  uint8_t *fixed = bitmap + sinfo->tupleBitmapSize;
×
371
  uint8_t *varlen = fixed + sinfo->tupleFixedSize;
×
372
  uint8_t *start = varlen;
×
373

374
  // primary keys
375
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
×
376
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
×
377
  }
378

379
  // bitmap + fixed + varlen
380
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
×
381
  int32_t colValIndex = 1;
×
382
  int8_t  firstBlobCol = 1;
×
383
  for (int32_t i = 1; i < schema->numOfCols; i++) {
×
384
    for (;;) {
×
385
      if (colValIndex >= numOfColVals) {  // NONE
×
386
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
387
        break;
×
388
      }
389

390
      int8_t hasBlob = 0;
×
391
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
×
392
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
×
393
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_VALUE);
×
394

395
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
×
396
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
397
              hasBlob = 1;
×
398
            }
399
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
×
400
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
×
401
            if (colValArray[colValIndex].value.nData) {
×
402
              if (hasBlob == 0) {
×
403
                (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
×
404
                varlen += colValArray[colValIndex].value.nData;
×
405
              } else {
406
                uint64_t  seq = 0;
×
407
                SBlobItem item = {.seqOffsetInRow = varlen - (*ppRow)->data,
×
408
                                  .data = colValArray[colValIndex].value.pData,
×
409
                                  .len = colValArray[colValIndex].value.nData,
×
410
                                  .type = TSDB_DATA_BLOB_VALUE};
411
                code = tBlobSetPush(pBlobSet, &item, &seq, firstBlobCol);
×
412
                if (firstBlobCol == 1) {
×
413
                  firstBlobCol = 0;
×
414
                }
415
                if (code != 0) return code;
×
416
                varlen += tPutU64(varlen, seq);
×
417
              }
418
            } else {
419
              if (hasBlob) TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
×
420
            }
421
          } else {
422
            (void)memcpy(fixed + schema->columns[i].offset,
×
423
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
×
424
                         tDataTypes[schema->columns[i].type].bytes);
×
425
          }
426
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
427
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
×
428
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
429
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
430
          }
431
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
432
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
433
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
434
          }
435
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
436
        }
437

438
        colValIndex++;
×
439
        break;
×
440
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
441
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
442
        break;
×
443
      } else {
444
        colValIndex++;
×
445
      }
446
    }
447
  }
448

449
  if (((*ppRow)->flag) == 0) {
×
450
    return TSDB_CODE_INVALID_PARA;
×
451
  }
452

453
  return 0;
×
454
}
455

456
static int32_t tRowBuildTupleWithBlob2(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
×
457
                                       SRow **ppRow, SBlobSet *pSrcBlobSet, SBlobSet *pDstBlobSet) {
458
  int32_t  code = 0;
×
459
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
×
460
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->tupleRowSize);
×
461
  if (*ppRow == NULL) {
×
462
    return terrno;
×
463
  }
464
  (*ppRow)->flag = sinfo->tupleFlag;
×
465
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
×
466
  (*ppRow)->sver = schema->version;
×
467
  (*ppRow)->len = sinfo->tupleRowSize;
×
468
  (*ppRow)->ts = colValArray[0].value.val;
×
469

470
  if (((*ppRow)->flag) == 0) {
×
471
    return TSDB_CODE_INVALID_PARA;
×
472
  }
473
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
×
474
    code = addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
475
    return code;
×
476
  }
477

478
  uint8_t *primaryKeys = (*ppRow)->data;
×
479
  uint8_t *bitmap = primaryKeys + sinfo->tuplePKSize;
×
480
  uint8_t *fixed = bitmap + sinfo->tupleBitmapSize;
×
481
  uint8_t *varlen = fixed + sinfo->tupleFixedSize;
×
482
  uint8_t *start = varlen;
×
483

484
  // primary keys
485
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
×
486
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
×
487
  }
488

489
  // bitmap + fixed + varlen
490
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
×
491
  int32_t colValIndex = 1;
×
492
  for (int32_t i = 1; i < schema->numOfCols; i++) {
×
493
    for (;;) {
×
494
      if (colValIndex >= numOfColVals) {  // NONE
×
495
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
496
        break;
×
497
      }
498

499
      int8_t hasBlob = 0;
×
500
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
×
501
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
×
502
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_VALUE);
×
503

504
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
×
505
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
506
              hasBlob = 1;
×
507
            }
508
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
×
509
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
×
510
            if (colValArray[colValIndex].value.nData) {
×
511
              if (hasBlob == 0) {
×
512
                (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
×
513
                varlen += colValArray[colValIndex].value.nData;
×
514
              } else {
515
                uint64_t seq = 0;
×
516
                if (tGetU64(colValArray[colValIndex].value.pData, &seq) < 0) {
×
517
                  TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
518
                }
519
                SBlobItem item = {0};
×
520

521
                code = tBlobSetGet(pSrcBlobSet, seq, &item);
×
522
                TAOS_CHECK_RETURN(code);
×
523

524
                code = tBlobSetPush(pDstBlobSet, &item, &seq, 0);
×
525
                TAOS_CHECK_RETURN(code);
×
526
                varlen += tPutU64(varlen, seq);
×
527
              }
528
            } else {
529
              if (hasBlob) TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
×
530
            }
531
          } else {
532
            (void)memcpy(fixed + schema->columns[i].offset,
×
533
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
×
534
                         tDataTypes[schema->columns[i].type].bytes);
×
535
          }
536
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
537
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
×
538
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
539
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
540
          }
541
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
×
542
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
543
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
544
          }
545
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
546
        }
547

548
        colValIndex++;
×
549
        break;
×
550
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
551
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
×
552
        break;
×
553
      } else {
554
        colValIndex++;
×
555
      }
556
    }
557
  }
558

559
  if (((*ppRow)->flag) == 0) {
×
560
    return TSDB_CODE_INVALID_PARA;
×
561
  }
562

563
  return 0;
×
564
}
565
static FORCE_INLINE void tRowBuildKVRowSetIndex(uint8_t flag, SKVIdx *indices, uint32_t offset) {
566
  if (flag & KV_FLG_LIT) {
2,147,483,647✔
567
    ((uint8_t *)indices->idx)[indices->nCol] = (uint8_t)offset;
2,147,483,647✔
568
  } else if (flag & KV_FLG_MID) {
×
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
256,224✔
570
  } else {
571
    ((uint32_t *)indices->idx)[indices->nCol] = (uint32_t)offset;
×
572
  }
573
  indices->nCol++;
2,147,483,647✔
574
}
2,147,483,647✔
575

576
static int32_t tRowBuildKVRow(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema, SRow **ppRow) {
835,796,888✔
577
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
835,796,888✔
578

579
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->kvRowSize);
835,796,888!
580
  if (*ppRow == NULL) {
836,019,602!
581
    return terrno;
×
582
  }
583
  (*ppRow)->flag = sinfo->kvFlag;
836,019,602✔
584
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
836,019,602✔
585
  (*ppRow)->sver = schema->version;
836,019,602✔
586
  (*ppRow)->len = sinfo->kvRowSize;
836,019,602✔
587
  (*ppRow)->ts = VALUE_GET_TRIVIAL_DATUM(&colValArray[0].value);
836,019,602✔
588

589
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
836,019,602!
590
    return TSDB_CODE_INVALID_PARA;
×
591
  }
592

593
  uint8_t *primaryKeys = (*ppRow)->data;
836,234,744✔
594
  SKVIdx  *indices = (SKVIdx *)(primaryKeys + sinfo->kvPKSize);
836,234,744✔
595
  uint8_t *payload = primaryKeys + sinfo->kvPKSize + sinfo->kvIndexSize;
836,234,744✔
596
  uint32_t payloadSize = 0;
836,234,744✔
597

598
  // primary keys
599
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
836,566,900✔
600
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
332,159✔
601
  }
602

603
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
836,234,741✔
604
  int32_t colValIndex = 1;
836,234,741✔
605
  for (int32_t i = 1; i < schema->numOfCols; i++) {
2,147,483,647✔
606
    for (;;) {
607
      if (colValIndex >= numOfColVals) {  // NONE
2,147,483,647✔
608
        break;
84✔
609
      }
610

611
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647!
612
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
2,147,483,647✔
613
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
2,147,483,647!
614
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647!
615
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
1,153,303,909✔
616
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
1,153,303,909✔
617
            if (colValArray[colValIndex].value.nData > 0) {
1,153,303,909!
618
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
1,203,403,790✔
619
                           colValArray[colValIndex].value.nData);
1,203,403,790✔
620
              payloadSize += colValArray[colValIndex].value.nData;
1,203,403,790✔
621
            }
622
          } else {
623
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,147,483,647✔
624
            (void)memcpy(payload + payloadSize,
2,147,483,647✔
625
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,147,483,647!
626
                         tDataTypes[schema->columns[i].type].bytes);
2,147,483,647✔
627
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
2,147,483,647✔
628
          }
629
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
2,147,483,647✔
630
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
786,447✔
631
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
1,572,894✔
632
        }
633

634
        colValIndex++;
2,147,483,647✔
635
        break;
2,147,483,647✔
636
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
637
        break;
228✔
638
      } else {
639
        colValIndex++;
×
640
      }
641
    }
642
  }
643
  return 0;
836,234,741✔
644
}
645

646
static int32_t tRowBuildKVRowWithBlob(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
×
647
                                      SRow **ppRow, SBlobSet *ppBlobSet) {
648
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
×
649

650
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->kvRowSize);
×
651
  if (*ppRow == NULL) {
×
652
    return terrno;
×
653
  }
654
  (*ppRow)->flag = sinfo->kvFlag;
×
655
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
×
656
  (*ppRow)->sver = schema->version;
×
657
  (*ppRow)->len = sinfo->kvRowSize;
×
658
  (*ppRow)->ts = colValArray[0].value.val;
×
659

660
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
×
661
    return TSDB_CODE_INVALID_PARA;
×
662
  }
663

664
  if (((*ppRow)->flag) == 0) {
×
665
    return TSDB_CODE_INVALID_PARA;
×
666
  }
667

668
  uint8_t *primaryKeys = (*ppRow)->data;
×
669
  SKVIdx  *indices = (SKVIdx *)(primaryKeys + sinfo->kvPKSize);
×
670
  uint8_t *payload = primaryKeys + sinfo->kvPKSize + sinfo->kvIndexSize;
×
671
  uint32_t payloadSize = 0;
×
672

673
  // primary keys
674
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
×
675
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
×
676
  }
677

678
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
×
679
  int32_t colValIndex = 1;
×
680
  int8_t  firstBlobCol = 1;
×
681
  for (int32_t i = 1; i < schema->numOfCols; i++) {
×
682
    for (;;) {
×
683
      if (colValIndex >= numOfColVals) {  // NONE
×
684
        break;
×
685
      }
686
      uint8_t hasBlob = 0;
×
687

688
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
×
689
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
×
690
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
×
691
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
×
692
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
693
              hasBlob = 1;
×
694
            }
695
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
×
696
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
×
697
            if (colValArray[colValIndex].value.nData > 0) {
×
698
              if (hasBlob == 0) {
×
699
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
×
700
                             colValArray[colValIndex].value.nData);
×
701
                payloadSize += colValArray[colValIndex].value.nData;
×
702
              } else {
703
                uint64_t  seq = 0;
×
704
                uint32_t  seqOffset = payloadSize + payload - (*ppRow)->data;
×
705
                SBlobItem item = {.seqOffsetInRow = seqOffset,
×
706
                                  .data = colValArray[colValIndex].value.pData,
×
707
                                  .len = colValArray[colValIndex].value.nData,
×
708
                                  .type = TSDB_DATA_BLOB_VALUE};
709
                int32_t   code = tBlobSetPush(ppBlobSet, &item, &seq, 0);
×
710
                if (code != 0) return code;
×
711
                payloadSize += tPutU64(payload + payloadSize, seq);
×
712
              }
713
            } else {
714
              if (hasBlob) {
×
715
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
×
716
              }
717
            }
718
          } else {
719
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
×
720
            (void)memcpy(payload + payloadSize, &colValArray[colValIndex].value.val,
×
721
                         tDataTypes[schema->columns[i].type].bytes);
×
722
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
×
723
          }
724
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
725
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
×
726
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
×
727
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
728
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
729
          }
730
        }
731
        colValIndex++;
×
732
        break;
×
733
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
734
        break;
×
735
      } else {
736
        colValIndex++;
×
737
      }
738
    }
739
  }
740

741
  if (((*ppRow)->flag) == 0) {
×
742
    return TSDB_CODE_INVALID_PARA;
×
743
  }
744
  return 0;
×
745
}
746

747
static int32_t tRowBuildKVRowWithBlob2(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema,
×
748
                                       SRow **ppRow, SBlobSet *pSrcBlobSet, SBlobSet *pDstBlobSet) {
749
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
×
750

751
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->kvRowSize);
×
752
  if (*ppRow == NULL) {
×
753
    return terrno;
×
754
  }
755
  (*ppRow)->flag = sinfo->kvFlag;
×
756
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
×
757
  (*ppRow)->sver = schema->version;
×
758
  (*ppRow)->len = sinfo->kvRowSize;
×
759
  (*ppRow)->ts = colValArray[0].value.val;
×
760

761
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
×
762
    return TSDB_CODE_INVALID_PARA;
×
763
  }
764

765
  if (((*ppRow)->flag) == 0) {
×
766
    return TSDB_CODE_INVALID_PARA;
×
767
  }
768

769
  uint8_t *primaryKeys = (*ppRow)->data;
×
770
  SKVIdx  *indices = (SKVIdx *)(primaryKeys + sinfo->kvPKSize);
×
771
  uint8_t *payload = primaryKeys + sinfo->kvPKSize + sinfo->kvIndexSize;
×
772
  uint32_t payloadSize = 0;
×
773

774
  // primary keys
775
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
×
776
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
×
777
  }
778

779
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
×
780
  int32_t colValIndex = 1;
×
781
  for (int32_t i = 1; i < schema->numOfCols; i++) {
×
782
    for (;;) {
×
783
      if (colValIndex >= numOfColVals) {  // NONE
×
784
        break;
×
785
      }
786
      uint8_t hasBlob = 0;
×
787

788
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
×
789
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
×
790
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
×
791
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
×
792
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
793
              hasBlob = 1;
×
794
            }
795
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
×
796
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
×
797
            if (colValArray[colValIndex].value.nData > 0) {
×
798
              if (hasBlob == 0) {
×
799
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
×
800
                             colValArray[colValIndex].value.nData);
×
801
                payloadSize += colValArray[colValIndex].value.nData;
×
802
              } else {
803
                uint64_t seq = 0;
×
804
                if (tGetU64(colValArray[colValIndex].value.pData, &seq) < 0) {
×
805
                  TAOS_CHECK_RETURN(TSDB_CODE_INVALID_PARA);
×
806
                }
807
                SBlobItem item = {0};
×
808

809
                int32_t code = tBlobSetGet(pSrcBlobSet, seq, &item);
×
810
                TAOS_CHECK_RETURN(code);
×
811

812
                code = tBlobSetPush(pDstBlobSet, &item, &seq, 0);
×
813
                TAOS_CHECK_RETURN(code);
×
814

815
                payloadSize += tPutU64(payload + payloadSize, seq);
×
816
              }
817
            } else {
818
              if (hasBlob) {
×
819
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
×
820
              }
821
            }
822
          } else {
823
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
×
824
            (void)memcpy(payload + payloadSize, &colValArray[colValIndex].value.val,
×
825
                         tDataTypes[schema->columns[i].type].bytes);
×
826
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
×
827
          }
828
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
829
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
×
830
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
×
831
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
832
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pDstBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
833
          }
834
        }
835
        colValIndex++;
×
836
        break;
×
837
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
838
        break;
×
839
      } else {
840
        colValIndex++;
×
841
      }
842
    }
843
  }
844

845
  if (((*ppRow)->flag) == 0) {
×
846
    return TSDB_CODE_INVALID_PARA;
×
847
  }
848
  return 0;
×
849
}
850
int32_t tRowBuild(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow, SRowBuildScanInfo *sinfo) {
2,147,483,647✔
851
  int32_t code;
852
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
2,147,483,647✔
853
  if (code) return code;
2,147,483,647✔
854

855
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
2,147,483,647✔
856
    code = tRowBuildTupleRow(aColVal, sinfo, pTSchema, ppRow);
1,375,329,832✔
857
  } else {
858
    code = tRowBuildKVRow(aColVal, sinfo, pTSchema, ppRow);
835,352,123✔
859
  }
860
  return code;
2,147,483,647✔
861
}
862

863
int32_t tBlobRowCreate(int64_t cap, int8_t type, SBlobSet **ppBlobRow) {
×
864
  int32_t    lino = 0;
×
865
  int32_t    code = 0;
×
866
  SBlobSet *p = taosMemCalloc(1, sizeof(SBlobSet));
×
867
  if (p == NULL) {
×
868
    return terrno;
×
869
  }
870

871
  p->type = type;
×
872
  p->pSeqTable = taosArrayInit(128, sizeof(SBlobValue));
×
873
  if (p->pSeqTable == NULL) {
×
874
    TAOS_CHECK_EXIT(terrno);
×
875
  }
876

877
  p->data = taosMemCalloc(1024, cap * sizeof(uint8_t));
×
878
  if (p->data == NULL) {
×
879
    TAOS_CHECK_EXIT(terrno);
×
880
  }
881

882
  p->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
×
883
  if (p->pSeqToffset == NULL) {
×
884
    TAOS_CHECK_EXIT(terrno);
×
885
  }
886

887
  p->pSet = taosArrayInit(4, sizeof(int32_t));
×
888
  if (p->pSet == NULL) {
×
889
    TAOS_CHECK_EXIT(terrno);
×
890
  }
891

892
  p->cap = cap;
×
893
  p->len = 0;
×
894
  p->seq = 1;
×
895

896
  *ppBlobRow = p;
×
897
_exit:
×
898
  if (code != 0) {
×
899
    taosHashCleanup(p->pSeqToffset);
×
900
    taosArrayDestroy(p->pSeqTable);
×
901
    taosArrayDestroy(p->pSet);
×
902

903
    taosMemoryFree(p->data);
×
904
    taosMemoryFree(p);
×
905
  }
906
  uDebug("create blob row %p", p);
×
907
  return code;
×
908
}
909
int32_t tBlobRowPush(SBlobSet *pBlobRow, SBlobItem *pItem, uint64_t *seq, int8_t nextRow) {
×
910
  if (pBlobRow == NULL || pItem == NULL || seq == NULL) {
×
911
    return TSDB_CODE_INVALID_PARA;
×
912
  }
913
  uTrace("push blob %p, seqOffsetInRow %d, dataLen %d, nextRow %d", pBlobRow, pItem->seqOffsetInRow, pItem->len,
×
914
         nextRow);
915
  int32_t  lino = 0;
×
916
  int32_t  code = 0;
×
917
  uint64_t offset;
918
  uint32_t len = pItem->len;
×
919
  uint8_t *data = pItem->data;
×
920
  int32_t  dataOffset = pItem->seqOffsetInRow;
×
921

922
  uint64_t tlen = pBlobRow->len + len;
×
923
  if (tlen > pBlobRow->cap) {
×
924
    int64_t cap = pBlobRow->cap;
×
925
    // opt later
926
    do {
927
      cap = cap * 2;
×
928
    } while (tlen > cap);
×
929

930
    uint8_t *data = taosMemRealloc(pBlobRow->data, cap);
×
931
    if (data == NULL) {
×
932
      return terrno;
×
933
    }
934
    pBlobRow->data = data;
×
935
    pBlobRow->cap = cap;
×
936
  }
937
  if (len > 0) {
×
938
    (void)memcpy(pBlobRow->data + pBlobRow->len, data, len);
×
939
  }
940

941
  offset = pBlobRow->len;
×
942
  pBlobRow->len += len;
×
943

944
  pBlobRow->seq++;
×
945
  *seq = pBlobRow->seq;
×
946

947
  SBlobValue value = {.offset = offset, .len = len, .dataOffset = dataOffset, .nextRow = nextRow};
×
948
  if (taosArrayPush(pBlobRow->pSeqTable, &value) == NULL) {
×
949
    TAOS_CHECK_EXIT(terrno);
×
950
  }
951

952
  int32_t sz = taosArrayGetSize(pBlobRow->pSeqTable);
×
953
  code = taosHashPut(pBlobRow->pSeqToffset, seq, sizeof(int64_t), &sz, sizeof(int32_t));
×
954
  if (code != 0) {
×
955
    TAOS_CHECK_EXIT(code);
×
956
  }
957

958
_exit:
×
959
  return code;
×
960
}
961
int32_t tBlobRowUpdate(SBlobSet *pBlobRow, uint64_t seq, SBlobItem *pItem) {
×
962
  int32_t code = 0;
×
963
  return code;
×
964
  if (pBlobRow == NULL || pItem == NULL) {
965
    return TSDB_CODE_INVALID_PARA;
966
  }
967
  if (pBlobRow->pSeqToffset == NULL || pBlobRow->pSeqTable == NULL) {
968
    return TSDB_CODE_INVALID_PARA;
969
  }
970

971
  return code;
972
}
973
int32_t tBlobRowGet(SBlobSet *pBlobRow, uint64_t seq, SBlobItem *pItem) {
×
974
  if (pBlobRow == NULL || pItem == NULL) {
×
975
    return TSDB_CODE_INVALID_PARA;
×
976
  }
977
  if (pBlobRow->pSeqToffset == NULL || pBlobRow->pSeqTable == NULL) {
×
978
    return TSDB_CODE_INVALID_PARA;
×
979
  }
980

981
  int32_t code = 0;
×
982
  int32_t len = 0;
×
983
  int32_t dataOffset = 0;
×
984
  int8_t  nextRow = 0;
×
985

986
  int32_t *offset = (int32_t *)taosHashGet(pBlobRow->pSeqToffset, &seq, sizeof(int64_t));
×
987
  if (offset == NULL) {
×
988
    return TSDB_CODE_INVALID_PARA;
×
989
  }
990

991
  SBlobValue *value = taosArrayGet(pBlobRow->pSeqTable, *offset - 1);
×
992
  if (value == NULL) {
×
993
    return TSDB_CODE_INVALID_PARA;
×
994
  }
995

996
  len = value->len;
×
997
  dataOffset = value->dataOffset;
×
998
  nextRow = value->nextRow;
×
999

1000
  pItem->seqOffsetInRow = dataOffset + pBlobRow->data - (uint8_t *)pBlobRow;
×
1001
  pItem->len = len;
×
1002
  pItem->data = pBlobRow->data + value->offset;
×
1003

1004
  return code;
×
1005
}
1006

1007
int32_t tBlobRowSize(SBlobSet *pBlobRow) {
×
1008
  if (pBlobRow == NULL) return 0;
×
1009
  return taosArrayGetSize(pBlobRow->pSeqTable);
×
1010
}
1011
int32_t tBlobRowEnd(SBlobSet *pBlobRow) {
×
1012
  if (pBlobRow == NULL) return 0;
×
1013
  int32_t sz = taosArrayGetSize(pBlobRow->pSeqTable);
×
1014
  if (taosArrayPush(pBlobRow->pSet, &sz) == NULL) {
×
1015
    return terrno;
×
1016
  }
1017
  return 0;
×
1018
}
1019
int32_t tBlobRowRebuild(SBlobSet *p, int32_t sRow, int32_t nrow, SBlobSet **pDst) {
×
1020
  int32_t code = 0;
×
1021

1022
  code = tBlobRowCreate(p->cap, p->type, pDst);
×
1023
  if (code != 0) {
×
1024
    return code;
×
1025
  }
1026

1027
  SBlobSet *pBlobRow = *pDst;
×
1028
  if (p->pSeqToffset == NULL || p->pSeqTable == NULL || p->data == NULL) {
×
1029
    return TSDB_CODE_INVALID_PARA;
×
1030
  }
1031

1032
  uint64_t seq = 0;
×
1033
  int32_t  count = 0;
×
1034
  for (int32_t i = sRow; i < taosArrayGetSize(p->pSeqTable); i++) {
×
1035
    SBlobValue *pValue = taosArrayGet(p->pSeqTable, i);
×
1036
    uint8_t    *data = p->data + pValue->offset;
×
1037
    int32_t     len = pValue->len;
×
1038

1039
    SBlobItem item = {.data = data, .len = len, .seqOffsetInRow = pValue->dataOffset};
×
1040

1041
    code = tBlobRowPush(pBlobRow, &item, &seq, pValue->nextRow);
×
1042
    count++;
×
1043
    if (count >= nrow) {
×
1044
      break;
×
1045
    }
1046
  }
1047

1048
  return code;
×
1049
}
1050

1051
int32_t tBlobRowDestroy(SBlobSet *pBlobRow) {
×
1052
  if (pBlobRow == NULL) return 0;
×
1053
  int32_t code = 0;
×
1054
  uTrace("destroy blob row, seqTable size %p", pBlobRow);
×
1055
  taosMemoryFree(pBlobRow->data);
×
1056
  taosArrayDestroy(pBlobRow->pSeqTable);
×
1057
  taosHashCleanup(pBlobRow->pSeqToffset);
×
1058
  taosArrayDestroy(pBlobRow->pSet);
×
1059
  taosMemoryFree(pBlobRow);
×
1060
  return code;
×
1061
}
1062
int32_t tBlobRowClear(SBlobSet *pBlobRow) {
×
1063
  if (pBlobRow == NULL) return 0;
×
1064
  int32_t code = 0;
×
1065
  uTrace("clear blob row, seqTable size %p", pBlobRow);
×
1066
  taosArrayClear(pBlobRow->pSeqTable);
×
1067
  taosHashClear(pBlobRow->pSeqToffset);
×
1068
  pBlobRow->len = 0;
×
1069
  pBlobRow->seq = 1;
×
1070
  return code;
×
1071
}
1072

1073
int32_t tRowBuildWithBlob(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow, SBlobSet *pBlobSet,
×
1074
                          SRowBuildScanInfo *sinfo) {
1075
  int32_t code;
1076

1077
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
×
1078
  if (code) return code;
×
1079

1080
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
×
1081
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
×
1082
  } else {
1083
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
×
1084
  }
1085

1086
  return code;
×
1087
}
1088

1089
int32_t tRowBuildWithBlob2(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow, SBlobSet *pSrcBlobSet,
×
1090
                           SBlobSet *pDstBlobSet, SRowBuildScanInfo *sinfo) {
1091
  int32_t code;
1092

1093
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
×
1094
  if (code) return code;
×
1095

1096
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
×
1097
    code = tRowBuildTupleWithBlob2(aColVal, sinfo, pTSchema, ppRow, pSrcBlobSet, pDstBlobSet);
×
1098
  } else {
1099
    code = tRowBuildKVRowWithBlob2(aColVal, sinfo, pTSchema, ppRow, pSrcBlobSet, pDstBlobSet);
×
1100
  }
1101

1102
  return code;
×
1103
}
1104

1105
int32_t tBlobSetCreate(int64_t cap, int8_t type, SBlobSet **ppBlobSet) {
×
1106
  int32_t    lino = 0;
×
1107
  int32_t    code = 0;
×
1108
  SBlobSet  *p = taosMemCalloc(1, sizeof(SBlobSet));
×
1109
  if (p == NULL) {
×
1110
    return terrno;
×
1111
  }
1112

1113
  p->type = type;
×
1114
  p->pSeqTable = taosArrayInit(128, sizeof(SBlobValue));
×
1115
  if (p->pSeqTable == NULL) {
×
1116
    TAOS_CHECK_EXIT(terrno);
×
1117
  }
1118

1119
  p->data = taosMemCalloc(1024, cap * sizeof(uint8_t));
×
1120
  if (p->data == NULL) {
×
1121
    TAOS_CHECK_EXIT(terrno);
×
1122
  }
1123

1124
  p->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
×
1125
  if (p->pSeqToffset == NULL) {
×
1126
    TAOS_CHECK_EXIT(terrno);
×
1127
  }
1128

1129
  p->pSet = taosArrayInit(4, sizeof(int32_t));
×
1130
  if (p->pSet == NULL) {
×
1131
    TAOS_CHECK_EXIT(terrno);
×
1132
  }
1133

1134
  p->cap = cap;
×
1135
  p->len = 0;
×
1136
  p->seq = 1;
×
1137

1138
  *ppBlobSet = p;
×
1139
_exit:
×
1140
  if (code != 0) {
×
1141
    taosHashCleanup(p->pSeqToffset);
×
1142
    taosArrayDestroy(p->pSeqTable);
×
1143
    taosArrayDestroy(p->pSet);
×
1144

1145
    taosMemoryFree(p->data);
×
1146
    taosMemoryFree(p);
×
1147
  }
1148
  uDebug("create blob row %p", p);
×
1149
  return code;
×
1150
}
1151
int32_t tBlobSetPush(SBlobSet *pBlobSet, SBlobItem *pItem, uint64_t *seq, int8_t nextRow) {
×
1152
  if (pBlobSet == NULL || pItem == NULL || seq == NULL) {
×
1153
    return TSDB_CODE_INVALID_PARA;
×
1154
  }
1155
  uTrace("push blob %p, seqOffsetInRow %d, dataLen %d, nextRow %d", pBlobSet, pItem->seqOffsetInRow, pItem->len,
×
1156
         nextRow);
1157
  int32_t  lino = 0;
×
1158
  int32_t  code = 0;
×
1159
  uint64_t offset;
1160
  uint32_t len = pItem->len;
×
1161
  uint8_t *data = pItem->data;
×
1162
  int32_t  dataOffset = pItem->seqOffsetInRow;
×
1163

1164
  uint64_t tlen = pBlobSet->len + len;
×
1165
  if (tlen > pBlobSet->cap) {
×
1166
    int64_t cap = pBlobSet->cap;
×
1167
    // opt later
1168
    do {
1169
      cap = cap * 2;
×
1170
    } while (tlen > cap);
×
1171

1172
    uint8_t *data = taosMemRealloc(pBlobSet->data, cap);
×
1173
    if (data == NULL) {
×
1174
      return terrno;
×
1175
    }
1176
    pBlobSet->data = data;
×
1177
    pBlobSet->cap = cap;
×
1178
  }
1179
  if (len > 0) {
×
1180
    (void)memcpy(pBlobSet->data + pBlobSet->len, data, len);
×
1181
  }
1182

1183
  offset = pBlobSet->len;
×
1184
  pBlobSet->len += len;
×
1185

1186
  pBlobSet->seq++;
×
1187
  *seq = pBlobSet->seq;
×
1188

1189
  SBlobValue value = {.offset = offset, .len = len, .dataOffset = dataOffset, .nextRow = nextRow, .type = pItem->type};
×
1190
  if (taosArrayPush(pBlobSet->pSeqTable, &value) == NULL) {
×
1191
    TAOS_CHECK_EXIT(terrno);
×
1192
  }
1193

1194
  int32_t sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
1195
  code = taosHashPut(pBlobSet->pSeqToffset, seq, sizeof(int64_t), &sz, sizeof(int32_t));
×
1196
  if (code != 0) {
×
1197
    TAOS_CHECK_EXIT(code);
×
1198
  }
1199

1200
_exit:
×
1201
  return code;
×
1202
}
1203

1204
void tBlobSetSwap(SBlobSet *p1, SBlobSet *p2) {
×
1205
  SBlobSet t = {0};
×
1206

1207
  memcpy(&t, p1, sizeof(SBlobSet));
×
1208

1209
  memcpy(p1, p2, sizeof(SBlobSet));
×
1210
  memcpy(p2, &t, sizeof(SBlobSet));
×
1211
}
×
1212

1213
int32_t tBlobSetUpdate(SBlobSet *pBlobSet, uint64_t seq, SBlobItem *pItem) {
×
1214
  int32_t code = 0;
×
1215
  return code;
×
1216
  if (pBlobSet == NULL || pItem == NULL) {
1217
    return TSDB_CODE_INVALID_PARA;
1218
  }
1219
  if (pBlobSet->pSeqToffset == NULL || pBlobSet->pSeqTable == NULL) {
1220
    return TSDB_CODE_INVALID_PARA;
1221
  }
1222

1223
  return code;
1224
}
1225
int32_t tBlobSetGet(SBlobSet *pBlobSet, uint64_t seq, SBlobItem *pItem) {
×
1226
  if (pBlobSet == NULL || pItem == NULL) {
×
1227
    return TSDB_CODE_INVALID_PARA;
×
1228
  }
1229
  if (pBlobSet->pSeqToffset == NULL || pBlobSet->pSeqTable == NULL) {
×
1230
    return TSDB_CODE_INVALID_PARA;
×
1231
  }
1232

1233
  int32_t code = 0;
×
1234
  int32_t len = 0;
×
1235
  int32_t dataOffset = 0;
×
1236
  int8_t  nextRow = 0;
×
1237

1238
  int32_t *offset = (int32_t *)taosHashGet(pBlobSet->pSeqToffset, &seq, sizeof(int64_t));
×
1239
  if (offset == NULL) {
×
1240
    return TSDB_CODE_INVALID_PARA;
×
1241
  }
1242

1243
  SBlobValue *value = taosArrayGet(pBlobSet->pSeqTable, *offset - 1);
×
1244
  if (value == NULL) {
×
1245
    return TSDB_CODE_INVALID_PARA;
×
1246
  }
1247

1248
  len = value->len;
×
1249
  dataOffset = value->dataOffset;
×
1250
  nextRow = value->nextRow;
×
1251

1252
  pItem->seqOffsetInRow = value->dataOffset;
×
1253
  pItem->len = len;
×
1254
  pItem->data = pBlobSet->data + value->offset;
×
1255
  pItem->type = value->type;
×
1256

1257
  return code;
×
1258
}
1259

1260
int32_t tBlobSetSize(SBlobSet *pBlobSet) {
31,755,867✔
1261
  if (pBlobSet == NULL) return 0;
31,755,867!
1262
  return taosArrayGetSize(pBlobSet->pSeqTable);
×
1263
}
1264

1265
void tBlobSetDestroy(SBlobSet *pBlobSet) {
22,229✔
1266
  if (pBlobSet == NULL) return;
22,229!
1267
  uTrace("destroy blob row, seqTable size %p", pBlobSet);
×
1268
  taosMemoryFree(pBlobSet->data);
×
1269
  taosArrayDestroy(pBlobSet->pSeqTable);
×
1270
  taosHashCleanup(pBlobSet->pSeqToffset);
×
1271
  taosArrayDestroy(pBlobSet->pSet);
×
1272
  taosMemoryFree(pBlobSet);
×
1273
}
1274
int32_t tBlobSetClear(SBlobSet *pBlobSet) {
×
1275
  if (pBlobSet == NULL) return 0;
×
1276
  int32_t code = 0;
×
1277
  uTrace("clear blob row, seqTable size %p", pBlobSet);
×
1278
  taosArrayClear(pBlobSet->pSeqTable);
×
1279
  taosHashClear(pBlobSet->pSeqToffset);
×
1280
  pBlobSet->len = 0;
×
1281
  pBlobSet->seq = 1;
×
1282
  return code;
×
1283
}
1284

1285
static int32_t tBindInfoCompare(const void *p1, const void *p2, const void *param) {
37✔
1286
  if (((SBindInfo *)p1)->columnId < ((SBindInfo *)p2)->columnId) {
37✔
1287
    return -1;
7✔
1288
  } else if (((SBindInfo *)p1)->columnId > ((SBindInfo *)p2)->columnId) {
30!
1289
    return 1;
30✔
1290
  }
1291
  return 0;
×
1292
}
1293

1294
/* build rows to `rowArray` from bind
1295
 * `infos` is the bind information array
1296
 * `numOfInfos` is the number of bind information
1297
 * `infoSorted` is whether the bind information is sorted by column id
1298
 * `pTSchema` is the schema of the table
1299
 * `rowArray` is the array to store the rows
1300
 * `pOrdered` is the pointer to store ordered
1301
 * `pDupTs` is the pointer to store duplicateTs
1302
 */
1303
int32_t tRowBuildFromBind(SBindInfo *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
5,626✔
1304
                          SArray *rowArray, bool *pOrdered, bool *pDupTs) {
1305
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
5,626!
1306
    return TSDB_CODE_INVALID_PARA;
×
1307
  }
1308

1309
  if (!infoSorted) {
5,627!
1310
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1311
  }
1312

1313
  int32_t code = 0;
5,627✔
1314
  int32_t numOfRows = infos[0].bind->num;
5,627✔
1315
  SArray *colValArray;
1316
  SColVal colVal;
1317

1318
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
5,627!
1319
    return terrno;
×
1320
  }
1321

1322
  SRowKey rowKey, lastRowKey;
1323
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
66,243✔
1324
    taosArrayClear(colValArray);
62,817✔
1325

1326
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
751,722✔
1327
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
692,885✔
1328
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
32✔
1329
      } else {
1330
        SValue value = {
692,853✔
1331
            .type = infos[iInfo].type,
692,853✔
1332
        };
1333
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
692,853!
1334
          value.nData = infos[iInfo].bind->length[iRow];
203,522✔
1335
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
203,522!
1336
            code = TSDB_CODE_INVALID_PARA;
×
1337
            goto _exit;
×
1338
          }
1339
          value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
203,522✔
1340
        } else {
1341
          valueSetDatum(&value, infos[iInfo].type,
489,331✔
1342
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
489,331✔
1343
                        infos[iInfo].bind->buffer_length);
489,331✔
1344
        }
1345
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
706,620✔
1346
      }
1347
      if (taosArrayPush(colValArray, &colVal) == NULL) {
689,257!
1348
        code = terrno;
×
1349
        goto _exit;
×
1350
      }
1351
    }
1352

1353
    SRow             *row;
1354
    SRowBuildScanInfo sinfo = {0};
58,837✔
1355
    if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
58,837!
1356
      goto _exit;
×
1357
    }
1358

1359
    if ((taosArrayPush(rowArray, &row)) == NULL) {
60,629!
1360
      code = terrno;
×
1361
      goto _exit;
×
1362
    }
1363

1364
    if (pOrdered && pDupTs) {
60,629!
1365
      tRowGetKey(row, &rowKey);
121,418!
1366
      if (iRow == 0) {
60,696✔
1367
        *pOrdered = true;
5,630✔
1368
        *pDupTs = false;
5,630✔
1369
      } else {
1370
        if (*pOrdered) {
55,066!
1371
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
55,090✔
1372
          *pOrdered = (res >= 0);
55,090✔
1373
          if (!*pDupTs) {
55,090✔
1374
            *pDupTs = (res == 0);
55,070✔
1375
          }
1376
        }
1377
      }
1378
      lastRowKey = rowKey;
60,696✔
1379
    }
1380
  }
1381

1382
_exit:
3,426✔
1383
  taosArrayDestroy(colValArray);
3,426✔
1384
  return code;
5,631✔
1385
}
1386

1387
int32_t tRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal) {
2,147,483,647✔
1388
  if (!(iCol < pTSchema->numOfCols)) return TSDB_CODE_INVALID_PARA;
2,147,483,647!
1389
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
2,147,483,647!
1390

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

1393
  if (iCol == 0) {
2,147,483,647✔
1394
    pColVal->cid = pTColumn->colId;
10,825,391✔
1395
    pColVal->value.type = pTColumn->type;
10,825,391✔
1396
    pColVal->flag = CV_FLAG_VALUE;
10,825,391✔
1397
    VALUE_SET_TRIVIAL_DATUM(&pColVal->value, pRow->ts);
10,825,391✔
1398
    return 0;
10,825,391✔
1399
  }
1400

1401
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
1402
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
10,121,515✔
1403
    return 0;
10,121,515✔
1404
  }
1405

1406
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
1407
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
613,583✔
1408
    return 0;
613,583✔
1409
  }
1410

1411
  SPrimaryKeyIndex index;
1412
  uint8_t         *data = pRow->data;
2,147,483,647✔
1413
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
2,147,483,647✔
1414
    data += tGetPrimaryKeyIndex(data, &index);
33,110,319✔
1415
  }
1416

1417
  if (pRow->flag >> 4) {  // KV Row
2,147,483,647✔
1418
    SKVIdx  *pIdx = (SKVIdx *)data;
2,147,483,647✔
1419
    uint8_t *pv = NULL;
2,147,483,647✔
1420

1421
    if (pRow->flag & KV_FLG_LIT) {
2,147,483,647!
1422
      pv = pIdx->idx + pIdx->nCol;
2,147,483,647✔
1423
    } else if (pRow->flag & KV_FLG_MID) {
×
1424
      pv = pIdx->idx + (pIdx->nCol << 1);
45,377,066✔
1425
    } else {
1426
      pv = pIdx->idx + (pIdx->nCol << 2);
×
1427
    }
1428

1429
    int16_t lidx = 0;
2,147,483,647✔
1430
    int16_t ridx = pIdx->nCol - 1;
2,147,483,647✔
1431
    while (lidx <= ridx) {
2,147,483,647✔
1432
      int16_t  mid = (lidx + ridx) >> 1;
2,147,483,647✔
1433
      uint8_t *pData = NULL;
2,147,483,647✔
1434
      if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
1435
        pData = pv + ((uint8_t *)pIdx->idx)[mid];
2,147,483,647✔
1436
      } else if (pRow->flag & KV_FLG_MID) {
184,620,019!
1437
        pData = pv + ((uint16_t *)pIdx->idx)[mid];
185,042,687✔
1438
      } else {
1439
        pData = pv + ((uint32_t *)pIdx->idx)[mid];
×
1440
      }
1441

1442
      int16_t cid;
1443
      pData += tGetI16v(pData, &cid);
2,147,483,647✔
1444

1445
      if (TABS(cid) == pTColumn->colId) {
2,147,483,647✔
1446
        if (cid < 0) {
2,147,483,647✔
1447
          *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,823,299✔
1448
        } else {
1449
          pColVal->cid = pTColumn->colId;
2,147,483,647✔
1450
          pColVal->value.type = pTColumn->type;
2,147,483,647✔
1451
          pColVal->flag = CV_FLAG_VALUE;
2,147,483,647✔
1452

1453
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647!
1454
            int8_t isBlob = IS_STR_DATA_BLOB(pTColumn->type) ? 1 : 0;
1,236,582,919!
1455

1456
            pData += tGetU32v(pData, &pColVal->value.nData);
1,236,582,919!
1457
            if (pColVal->value.nData > 0) {
1,236,582,919!
1458
              pColVal->value.pData = pData;
1,903,743,096✔
1459
            } else {
1460
              pColVal->value.pData = NULL;
×
1461
            }
1462
            if (isBlob == 1) {
1,236,582,919!
1463
              pData += BSE_SEQUECE_SIZE;  // skip seq
×
1464
            }
1465
          } else {
1466
            valueSetDatum(&pColVal->value, pTColumn->type, pData, pTColumn->bytes);
2,147,483,647✔
1467
          }
1468
        }
1469
        return 0;
2,147,483,647✔
1470
      } else if (TABS(cid) < pTColumn->colId) {
2,147,483,647✔
1471
        lidx = mid + 1;
2,147,483,647✔
1472
      } else {
1473
        ridx = mid - 1;
2,147,483,647✔
1474
      }
1475
    }
1476

1477
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
2,147,483,647✔
1478
  } else {  // Tuple Row
1479
    uint8_t *bitmap = data;
933,055,051✔
1480
    uint8_t *fixed;
1481
    uint8_t *varlen;
1482
    uint8_t  bit;
1483

1484
    if (pRow->flag == HAS_VALUE) {
933,055,051✔
1485
      fixed = bitmap;
684,718,285✔
1486
      bit = BIT_FLG_VALUE;
684,718,285✔
1487
    } else if (pRow->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
248,336,766!
1488
      fixed = BIT2_SIZE(pTSchema->numOfCols - 1) + bitmap;
×
1489
      bit = GET_BIT2(bitmap, iCol - 1);
×
1490
    } else {
1491
      fixed = BIT1_SIZE(pTSchema->numOfCols - 1) + bitmap;
248,336,766✔
1492
      bit = GET_BIT1(bitmap, iCol - 1);
248,336,766✔
1493

1494
      if (pRow->flag == (HAS_NONE | HAS_VALUE)) {
248,336,766✔
1495
        if (bit) bit++;
16,346✔
1496
      } else if (pRow->flag == (HAS_NULL | HAS_VALUE)) {
248,320,420✔
1497
        bit++;
10,255,209✔
1498
      }
1499
    }
1500
    varlen = fixed + pTSchema->flen;
933,055,051✔
1501

1502
    if (bit == BIT_FLG_NONE) {
933,055,051✔
1503
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
3,615✔
1504
      return 0;
3,615✔
1505
    } else if (bit == BIT_FLG_NULL) {
933,051,436✔
1506
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
2,730,255✔
1507
      return 0;
2,730,255✔
1508
    }
1509

1510
    pColVal->cid = pTColumn->colId;
930,321,181✔
1511
    pColVal->value.type = pTColumn->type;
930,321,181✔
1512
    pColVal->flag = CV_FLAG_VALUE;
930,321,181✔
1513
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
930,321,181!
1514
      int8_t isBlob = IS_STR_DATA_BLOB(pTColumn->type) ? 1 : 0;
357,535,623!
1515
      pColVal->value.pData = varlen + *(int32_t *)(fixed + pTColumn->offset);
357,535,623✔
1516
      pColVal->value.pData += tGetU32v(pColVal->value.pData, &pColVal->value.nData);
715,071,246✔
1517
      // TODO(yhDeng): support tuple
1518
      //  if (isBlob) pColVal->value.pData += sizeof(uint64_t);  // skip seq
1519
      //  }
1520
    } else {
1521
      valueSetDatum(&pColVal->value, pTColumn->type, fixed + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
572,785,558✔
1522
    }
1523
  }
1524

1525
  return 0;
2,147,483,647✔
1526
}
1527

1528
void tRowDestroy(SRow *pRow) {
1,340,372,384✔
1529
  if (pRow) taosMemoryFree(pRow);
1,340,372,384!
1530
}
1,340,193,282✔
1531

1532
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
527,088,475✔
1533
  SRowKey key1, key2;
1534
  tRowGetKey(*(SRow **)p1, &key1);
1,054,176,950✔
1535
  tRowGetKey(*(SRow **)p2, &key2);
1,037,418,268✔
1536
  return tRowKeyCompare(&key1, &key2);
527,161,932✔
1537
}
1538
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
212,953✔
1539

1540
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
1,739✔
1541
  SColVal* pColVal = tRowIterNext(iter);
1,739✔
1542
  while (pColVal != NULL && pColVal->cid < targetCid) {
1,743!
1543
    pColVal = tRowIterNext(iter);
4✔
1544
  }
1545
  return pColVal;
1,739✔
1546
}
1547

1548
static int32_t tRowMergeImpl(SArray *aRowP, STSchema *pTSchema, int32_t iStart, int32_t iEnd, ERowMergeStrategy strategy) {
680✔
1549
  int32_t code = 0;
680✔
1550

1551
  int32_t    nRow = iEnd - iStart;
680✔
1552
  SRowIter **aIter = NULL;
680✔
1553
  SArray    *aColVal = NULL;
680✔
1554
  SRow      *pRow = NULL;
680✔
1555
  uint8_t    hasBlob = 0;
680✔
1556
  aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
680!
1557
  if (aIter == NULL) {
680!
1558
    code = terrno;
×
1559
    goto _exit;
×
1560
  }
1561

1562
  for (int32_t i = 0; i < nRow; i++) {
213,633✔
1563
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
212,953✔
1564

1565
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
212,953✔
1566
    if (code) goto _exit;
212,953!
1567
  }
1568

1569
  // merge
1570
  aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
680✔
1571
  if (aColVal == NULL) {
680!
1572
    code = terrno;
×
1573
    goto _exit;
×
1574
  }
1575

1576
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
2,411✔
1577
    int32_t targetCid = pTSchema->columns[iCol].colId;
1,731✔
1578
    SColVal *pColVal = NULL;
1,731✔
1579

1580
    switch (strategy) {
1,731✔
1581
      case KEEP_CONSISTENCY:
1,651✔
1582
        if (nRow > 0)
1,651!
1583
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
1,651✔
1584
        break;
1,651✔
1585

1586
      default:  // default using PREFER_NON_NULL strategy
80✔
1587
      case PREFER_NON_NULL:
1588
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
88!
1589
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
88✔
1590

1591
          if (COL_VAL_IS_VALUE(pColValT)) {
88✔
1592
            pColVal = pColValT;
80✔
1593
            break;
80✔
1594
          } else if (pColVal == NULL) {
8!
1595
            pColVal = pColValT;
8✔
1596
          }
1597
        }
1598
        break;
80✔
1599
    }
1600

1601
    if (pColVal) {
1,731!
1602
      if (taosArrayPush(aColVal, pColVal) == NULL) {
1,731!
1603
        code = terrno;
×
1604
        goto _exit;
×
1605
      }
1606
    }
1607
  }
1608

1609
  // build
1610
  SRowBuildScanInfo sinfo = {0};
680✔
1611
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
680✔
1612

1613
  if (code) goto _exit;
680!
1614

1615
  taosArrayRemoveBatch(aRowP, iStart, nRow, (FDelete)tRowPDestroy);
680✔
1616
  if (taosArrayInsert(aRowP, iStart, &pRow) == NULL) {
680!
1617
    code = terrno;
×
1618
    goto _exit;
×
1619
  }
1620

1621
_exit:
680✔
1622
  if (aIter) {
680!
1623
    for (int32_t i = 0; i < nRow; i++) {
213,633✔
1624
      tRowIterClose(&aIter[i]);
212,953✔
1625
    }
1626
    taosMemoryFree(aIter);
680!
1627
  }
1628
  if (aColVal) taosArrayDestroy(aColVal);
680!
1629
  if (code) tRowDestroy(pRow);
680!
1630
  return code;
680✔
1631
}
1632
static int32_t tBlobSetTransferTo(SBlobSet *pSrc, SBlobSet *pDst, SColVal *pVal) {
×
1633
  int32_t code = 0;
×
1634
  int32_t lino = 0;
×
1635
  if (COL_VAL_IS_NULL(pVal) || pVal->value.pData == NULL) {
×
1636
    int8_t type = COL_VAL_IS_NULL(pVal) ? TSDB_DATA_BLOB_NULL_VALUE : TSDB_DATA_BLOB_EMPTY_VALUE;
×
1637
    code = addEmptyItemToBlobSet(pDst, type, NULL);
×
1638
    TAOS_CHECK_GOTO(code, &lino, _error);
×
1639
  } else {
1640
    uint64_t seq = 0;
×
1641
    if (tGetU64(pVal->value.pData, &seq) < 0) {
×
1642
      uError("tBlobSetTransferTo: invalid blob value, seq %p", pVal->value.pData);
×
1643
      return TSDB_CODE_INVALID_PARA;
×
1644
    }
1645

1646
    SBlobItem item = {0};
×
1647
    code = tBlobSetGet(pSrc, seq, &item);
×
1648
    TAOS_CHECK_GOTO(code, &lino, _error);
×
1649

1650
    code = tBlobSetPush(pDst, &item, &seq, 1);
×
1651
    TAOS_CHECK_GOTO(code, &lino, _error);
×
1652
    if (tPutU64(pVal->value.pData, seq) < 0) {
×
1653
      uError("tBlobSetTransferTo: put seq to colVal failed");
×
1654
      return TSDB_CODE_INVALID_PARA;
×
1655
    }
1656
  }
1657

1658
_error:
×
1659
  return code;
×
1660
}
1661

1662
static int32_t tRowRebuildBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob) {
×
1663
  int32_t code = 0;
×
1664
  int32_t   nRow = TARRAY_SIZE(aRowP);
×
1665
  int32_t   lino = 0;
×
1666
  SBlobSet *pTempBlob = NULL;
×
1667

1668
  SArray *aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
×
1669
  if (aColVal == NULL) {
×
1670
    TAOS_CHECK_RETURN(code = terrno);
×
1671
  }
1672

1673
  SRowIter **aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
×
1674
  if (aIter == NULL) {
×
1675
    TAOS_CHECK_GOTO(terrno, &lino, _error);
×
1676
  }
1677

1678
  for (int32_t i = 0; i < nRow; i++) {
×
1679
    SRow *pRowT = taosArrayGetP(aRowP, 0 + i);
×
1680
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
×
1681
    TAOS_CHECK_GOTO(code, &lino, _error);
×
1682
  }
1683

1684
  for (int32_t i = 0; i < nRow; i++) {
×
1685
    SColVal *pColVal = tRowIterNext(aIter[i]);
×
1686
    do {
1687
      if (COL_VAL_IS_VALUE(pColVal) || COL_VAL_IS_NULL(pColVal)) {
×
1688
        if (IS_STR_DATA_BLOB(pColVal->value.type)) {
×
1689
          if (taosArrayPush(aColVal, pColVal) == NULL) {
×
1690
            code = terrno;
×
1691
            TAOS_CHECK_GOTO(code, &lino, _error);
×
1692
          }
1693
          break;
×
1694
        }
1695
      }
1696
    } while ((pColVal = tRowIterNext(aIter[i])) != NULL);
×
1697
  }
1698

1699
  code = tBlobSetCreate(pBlob->cap, pBlob->type, &pTempBlob);
×
1700
  TAOS_CHECK_GOTO(code, &lino, _error);
×
1701

1702
  for (int32_t i = 0; i < taosArrayGetSize(aColVal); i++) {
×
1703
    uint64_t seq = 0;
×
1704
    SColVal *pVal = taosArrayGet(aColVal, i);
×
1705

1706
    code = tBlobSetTransferTo(pBlob, pTempBlob, pVal);
×
1707
    TAOS_CHECK_GOTO(code, &lino, _error);
×
1708
  }
1709

1710
  tBlobSetSwap(pBlob, pTempBlob);
×
1711

1712
_error:
×
1713
  if (code != 0) {
×
1714
    uError("tRowRebuildBlob failed at line %d, code %d", code, lino);
×
1715
  }
1716

1717
  if (aIter) {
×
1718
    for (int32_t i = 0; i < nRow; i++) {
×
1719
      tRowIterClose(&aIter[i]);
×
1720
    }
1721
    taosMemoryFree(aIter);
×
1722
  }
1723
  if (aColVal) {
×
1724
    taosArrayDestroy(aColVal);
×
1725
  }
1726
  tBlobSetDestroy(pTempBlob);
×
1727
  return code;
×
1728
}
1729

1730
static int32_t tRowMergeAndRebuildBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob) {
×
1731
  int32_t code = 0;
×
1732
  int32_t lino = 0;
×
1733

1734
  SBlobSet *pTempBlobSet = NULL;
×
1735
  int32_t   size = taosArrayGetSize(aRowP);
×
1736
  if (size <= 1) {
×
1737
    return code;
×
1738
  }
1739
  int32_t colBlobIdx = 0;
×
1740
  for (int32_t i = 0; i < pTSchema->numOfCols; i++) {
×
1741
    if (IS_STR_DATA_BLOB(pTSchema->columns[i].type)) {
×
1742
      colBlobIdx = i;
×
1743
      break;
×
1744
    }
1745
  }
1746

1747
  code = tBlobSetCreate(pBlob->cap, pBlob->type, &pTempBlobSet);
×
1748
  TAOS_CHECK_GOTO(code, &lino, _error);
×
1749

1750
  int32_t iStart = 0;
×
1751
  while (iStart < aRowP->size) {
×
1752
    SRowKey key1;
1753
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
×
1754

1755
    tRowGetKey(row1, &key1);
×
1756

1757
    int32_t iEnd = iStart + 1;
×
1758
    while (iEnd < aRowP->size) {
×
1759
      SRowKey key2;
1760
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
×
1761
      tRowGetKey(row2, &key2);
×
1762

1763
      if (tRowKeyCompare(&key1, &key2) != 0) break;
×
1764

1765
      iEnd++;
×
1766
    }
1767

1768
    if (iEnd - iStart > 1) {
×
1769
      code = tRowMergeWithBlobImpl(aRowP, pTSchema, pBlob, pTempBlobSet, iStart, iEnd, 0);
×
1770
      TAOS_CHECK_GOTO(code, &lino, _error);
×
1771
    } else {
1772
      SColVal colVal = {0};
×
1773
      code = tRowGet(row1, pTSchema, colBlobIdx, &colVal);
×
1774
      TAOS_CHECK_GOTO(code, &lino, _error);
×
1775

1776
      code = tBlobSetTransferTo(pBlob, pTempBlobSet, &colVal);
×
1777
      TAOS_CHECK_GOTO(code, &lino, _error);
×
1778
    }
1779
    // the array is also changing, so the iStart just ++ instead of iEnd
1780
    iStart++;
×
1781
  }
1782

1783
_error:
×
1784
  if (pBlob && pTempBlobSet) {
×
1785
    tBlobSetSwap(pBlob, pTempBlobSet);
×
1786
  }
1787

1788
  tBlobSetDestroy(pTempBlobSet);
×
1789
  return code;
×
1790
}
1791

1792
static int32_t tRowMergeWithBlobImpl(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob, SBlobSet *pDstBlob,
×
1793
                                     int32_t iStart, int32_t iEnd, int8_t flag) {
1794
  int32_t code = 0;
×
1795
  int32_t    lino = 0;
×
1796
  int32_t    nRow = iEnd - iStart;
×
1797
  SRowIter **aIter = NULL;
×
1798
  SArray    *aColVal = NULL;
×
1799
  SRow      *pRow = NULL;
×
1800

1801
  aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
×
1802
  if (aColVal == NULL) {
×
1803
    code = terrno;
×
1804
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
1805
  }
1806

1807
  aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
×
1808
  if (aIter == NULL) {
×
1809
    code = terrno;
×
1810
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
1811
  }
1812

1813
  for (int32_t i = 0; i < nRow; i++) {
×
1814
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
×
1815
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
×
1816
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
1817
  }
1818
  // merge
1819

1820
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
×
1821
    SColVal  *pColVal = NULL;
×
1822
    STColumn *pCol = pTSchema->columns + iCol;
×
1823

1824
    for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
×
1825
      SColVal *pColValT = tRowIterNext(aIter[iRow]);
×
1826
      while (pColValT->cid < pTSchema->columns[iCol].colId) {
×
1827
        pColValT = tRowIterNext(aIter[iRow]);
×
1828
      }
1829
      // todo: take strategy according to the flag
1830
      if (COL_VAL_IS_VALUE(pColValT)) {
×
1831
        pColVal = pColValT;
×
1832
        break;
×
1833
      } else if (COL_VAL_IS_NULL(pColValT)) {
×
1834
        if (pColVal == NULL) {
×
1835
          pColVal = pColValT;
×
1836
        }
1837
      }
1838
    }
1839

1840
    if (pColVal) {
×
1841
      if (taosArrayPush(aColVal, pColVal) == NULL) {
×
1842
        code = terrno;
×
1843
        TAOS_CHECK_GOTO(code, &lino, _exit);
×
1844
      }
1845
    }
1846
  }
1847

1848
  // build
1849

1850
  SRowBuildScanInfo sinfo = {.hasBlob = 1};
×
1851
  code = tRowBuildWithBlob2(aColVal, pTSchema, &pRow, pBlob, pDstBlob, &sinfo);
×
1852
  TAOS_CHECK_GOTO(code, &lino, _exit);
×
1853

1854
  taosArrayRemoveBatch(aRowP, iStart, nRow, (FDelete)tRowPDestroy);
×
1855
  if (taosArrayInsert(aRowP, iStart, &pRow) == NULL) {
×
1856
    code = terrno;
×
1857
    TAOS_CHECK_GOTO(code, &lino, _exit);
×
1858
  }
1859

1860
_exit:
×
1861
  if (aIter) {
×
1862
    for (int32_t i = 0; i < nRow; i++) {
×
1863
      tRowIterClose(&aIter[i]);
×
1864
    }
1865
    taosMemoryFree(aIter);
×
1866
  }
1867
  if (aColVal) taosArrayDestroy(aColVal);
×
1868
  if (code) tRowDestroy(pRow);
×
1869

1870
  return code;
×
1871
}
1872

1873
int32_t tRowSort(SArray *aRowP) {
72,711✔
1874
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
72,711✔
1875
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
72,709✔
1876
  if (code != TSDB_CODE_SUCCESS) {
72,711!
1877
    uError("taosArrayMSort failed caused by %d", code);
×
1878
  }
1879
  return code;
72,711✔
1880
}
1881

1882
int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, ERowMergeStrategy strategy) {
72,720✔
1883
  int32_t code = 0;
72,720✔
1884

1885
  int32_t iStart = 0;
72,720✔
1886
  while (iStart < aRowP->size) {
71,169,331✔
1887
    SRowKey key1;
1888
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
71,153,694✔
1889

1890
    tRowGetKey(row1, &key1);
142,271,914✔
1891

1892
    int32_t iEnd = iStart + 1;
71,097,171✔
1893
    while (iEnd < aRowP->size) {
71,272,551✔
1894
      SRowKey key2;
1895
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
71,237,991✔
1896
      tRowGetKey(row2, &key2);
142,509,890✔
1897

1898
      if (tRowKeyCompare(&key1, &key2) != 0) break;
71,237,431✔
1899

1900
      iEnd++;
175,380✔
1901
    }
1902

1903
    if (iEnd - iStart > 1) {
71,096,611✔
1904
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
680✔
1905
      if (code) return code;
680!
1906
    }
1907

1908
    // the array is also changing, so the iStart just ++ instead of iEnd
1909
    iStart++;
71,096,611✔
1910
  }
1911

1912
  return code;
15,637✔
1913
}
1914

1915
int32_t tRowSortWithBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlobSet) {
×
1916
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
×
1917
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
×
1918
  if (code != TSDB_CODE_SUCCESS) {
×
1919
    uError("taosArrayMSort failed caused by %d", code);
×
1920
    return code;
×
1921
  }
1922
  return code;
×
1923
}
1924

1925
int8_t tRowNeedDoMerge(SArray *aRowP) {
×
1926
  if (aRowP == NULL || aRowP->size <= 1) return 0;
×
1927

1928
  int8_t  doMerge = 0;
×
1929
  int32_t iStart = 0;
×
1930
  while (iStart < aRowP->size) {
×
1931
    SRowKey key1;
1932
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
×
1933

1934
    tRowGetKey(row1, &key1);
×
1935

1936
    int32_t iEnd = iStart + 1;
×
1937
    while (iEnd < aRowP->size) {
×
1938
      SRowKey key2;
1939
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
×
1940
      tRowGetKey(row2, &key2);
×
1941

1942
      if (tRowKeyCompare(&key1, &key2) != 0) break;
×
1943

1944
      iEnd++;
×
1945
    }
1946

1947
    if (iEnd - iStart > 1) {
×
1948
      doMerge = 1;
×
1949
      break;
×
1950
    }
1951
    iStart++;
×
1952
  }
1953
  return doMerge;
×
1954
}
1955

1956
int32_t tRowMergeWithBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlobSet, int8_t flag) {
×
1957
  int32_t code = 0;
×
1958

1959
  int8_t doMerge = tRowNeedDoMerge(aRowP);
×
1960
  if (!doMerge) {
×
1961
    code = tRowRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1962
  } else {
1963
    code = tRowRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1964
    TAOS_CHECK_RETURN(code);
×
1965

1966
    code = tRowMergeAndRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1967
  }
1968
  return code;
×
1969
}
1970

1971
// SRowIter ========================================
1972
struct SRowIter {
1973
  SRow     *pRow;
1974
  STSchema *pTSchema;
1975

1976
  int32_t iTColumn;
1977
  union {
1978
    struct {  // kv
1979
      int32_t iCol;
1980
      SKVIdx *pIdx;
1981
    };
1982
    struct {  // tuple
1983
      uint8_t *pb;
1984
      uint8_t *pf;
1985
    };
1986
  };
1987
  uint8_t *pv;
1988
  SColVal  cv;
1989
};
1990

1991
int32_t tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter) {
547,925✔
1992
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
547,925!
1993

1994
  int32_t code = 0;
547,925✔
1995

1996
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
547,925!
1997
  if (pIter == NULL) {
548,246!
1998
    code = terrno;
×
1999
    goto _exit;
×
2000
  }
2001

2002
  pIter->pRow = pRow;
548,246✔
2003
  pIter->pTSchema = pTSchema;
548,246✔
2004
  pIter->iTColumn = 0;
548,246✔
2005

2006
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
548,246✔
2007

2008
  uint8_t         *data = pRow->data;
547,272✔
2009
  SPrimaryKeyIndex index;
2010
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
555,304✔
2011
    data += tGetPrimaryKeyIndex(data, &index);
8,047✔
2012
  }
2013

2014
  if (pRow->flag >> 4) {
547,257✔
2015
    pIter->iCol = 0;
331,301✔
2016
    pIter->pIdx = (SKVIdx *)data;
331,301✔
2017
    if (pRow->flag & KV_FLG_LIT) {
331,301✔
2018
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
324,694✔
2019
    } else if (pRow->flag & KV_FLG_MID) {
6,607✔
2020
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
6,600✔
2021
    } else {
2022
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
7✔
2023
    }
2024
  } else {
2025
    switch (pRow->flag) {
215,956!
2026
      case (HAS_NULL | HAS_NONE):
×
2027
        pIter->pb = data;
×
2028
        break;
×
2029
      case HAS_VALUE:
215,593✔
2030
        pIter->pf = data;
215,593✔
2031
        pIter->pv = pIter->pf + pTSchema->flen;
215,593✔
2032
        break;
215,593✔
2033
      case (HAS_VALUE | HAS_NONE):
353✔
2034
      case (HAS_VALUE | HAS_NULL):
2035
        pIter->pb = data;
353✔
2036
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
353✔
2037
        pIter->pv = pIter->pf + pTSchema->flen;
353✔
2038
        break;
353✔
2039
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2040
        pIter->pb = data;
×
2041
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2042
        pIter->pv = pIter->pf + pTSchema->flen;
×
2043
        break;
×
2044
      default:
10✔
2045
        break;
10✔
2046
    }
2047
  }
2048

2049
_exit:
548,231✔
2050
  if (code) {
548,231!
2051
    *ppIter = NULL;
×
2052
  } else {
2053
    *ppIter = pIter;
548,231✔
2054
  }
2055
  return code;
548,231✔
2056
}
2057

2058
void tRowIterClose(SRowIter **ppIter) {
547,670✔
2059
  SRowIter *pIter = *ppIter;
547,670✔
2060
  if (pIter) {
547,670!
2061
    taosMemoryFree(pIter);
547,673✔
2062
  }
2063
  *ppIter = NULL;
548,214✔
2064
}
548,214✔
2065

2066
SColVal *tRowIterNext(SRowIter *pIter) {
8,610,062✔
2067
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
8,610,062✔
2068
    return NULL;
330,967✔
2069
  }
2070

2071
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
8,279,095✔
2072

2073
  // timestamp
2074
  if (0 == pIter->iTColumn) {
8,279,095✔
2075
    pIter->cv.cid = pTColumn->colId;
335,881✔
2076
    pIter->cv.value.type = pTColumn->type;
335,881✔
2077
    pIter->cv.flag = CV_FLAG_VALUE;
335,881✔
2078
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
335,881✔
2079
    goto _exit;
335,881✔
2080
  }
2081

2082
  if (pIter->pRow->flag == HAS_NONE) {
7,943,214✔
2083
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
16,104✔
2084
    goto _exit;
16,104✔
2085
  }
2086

2087
  if (pIter->pRow->flag == HAS_NULL) {
7,927,110✔
2088
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
789✔
2089
    goto _exit;
789✔
2090
  }
2091

2092
  if (pIter->pRow->flag >> 4) {  // KV
7,926,321!
2093
    if (pIter->iCol < pIter->pIdx->nCol) {
7,929,111✔
2094
      uint8_t *pData;
2095

2096
      if (pIter->pRow->flag & KV_FLG_LIT) {
4,650,942✔
2097
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
4,453,022✔
2098
      } else if (pIter->pRow->flag & KV_FLG_MID) {
197,920!
2099
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
198,000✔
2100
      } else {
2101
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
×
2102
      }
2103

2104
      int16_t cid;
2105
      pData += tGetI16v(pData, &cid);
4,650,942✔
2106

2107
      if (TABS(cid) == pTColumn->colId) {
4,650,942!
2108
        if (cid < 0) {
4,656,533✔
2109
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
3,147✔
2110
        } else {
2111
          pIter->cv.cid = pTColumn->colId;
4,653,386✔
2112
          pIter->cv.value.type = pTColumn->type;
4,653,386✔
2113
          pIter->cv.flag = CV_FLAG_VALUE;
4,653,386✔
2114

2115
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
4,653,386!
2116
            pData += tGetU32v(pData, &pIter->cv.value.nData);
752,284!
2117
            if (pIter->cv.value.nData > 0) {
752,284!
2118
              pIter->cv.value.pData = pData;
757,203✔
2119
            } else {
2120
              pIter->cv.value.pData = NULL;
×
2121
              // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2122
              //   pIter->cv.value.pData = pData;
2123
              // }
2124
            }
2125
          } else {
2126
            valueSetDatum(&pIter->cv.value, pTColumn->type, pData, pTColumn->bytes);
3,901,102✔
2127
          }
2128
        }
2129

2130
        pIter->iCol++;
4,659,329✔
2131
        goto _exit;
4,659,329✔
2132
      } else if (TABS(cid) > pTColumn->colId) {
×
2133
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
8✔
2134
        goto _exit;
8✔
2135
      } else {
2136
        uError("unexpected column id %d, %d", cid, pTColumn->colId);
×
2137
        goto _exit;
×
2138
      }
2139
    } else {
2140
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
3,278,169✔
2141
      goto _exit;
3,278,169✔
2142
    }
2143
  } else {  // Tuple
2144
    uint8_t bv = BIT_FLG_VALUE;
×
2145
    if (pIter->pb) {
×
2146
      switch (pIter->pRow->flag) {
1,455!
2147
        case (HAS_NULL | HAS_NONE):
×
2148
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
×
2149
          break;
×
2150
        case (HAS_VALUE | HAS_NONE):
4✔
2151
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
4✔
2152
          if (bv) bv++;
4!
2153
          break;
4✔
2154
        case (HAS_VALUE | HAS_NULL):
1,451✔
2155
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
1,451✔
2156
          break;
1,451✔
2157
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2158
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
2159
          break;
×
2160
        default:
×
2161
          break;
×
2162
      }
2163

2164
      if (bv == BIT_FLG_NONE) {
1,455!
2165
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
×
2166
        goto _exit;
×
2167
      } else if (bv == BIT_FLG_NULL) {
1,455✔
2168
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
267✔
2169
        goto _exit;
267✔
2170
      }
2171
    }
2172

2173
    pIter->cv.cid = pTColumn->colId;
×
2174
    pIter->cv.value.type = pTColumn->type;
×
2175
    pIter->cv.flag = CV_FLAG_VALUE;
×
2176
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
×
2177
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
×
2178
      pData += tGetU32v(pData, &pIter->cv.value.nData);
×
2179
      if (pIter->cv.value.nData > 0) {
×
2180
        pIter->cv.value.pData = pData;
629✔
2181
      } else {
2182
        pIter->cv.value.pData = NULL;
×
2183
        // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2184
        //   pIter->cv.value.pData = pData;
2185
        // }
2186
      }
2187
    } else {
2188
      valueSetDatum(&pIter->cv.value, pTColumn->type, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
10,751✔
2189
    }
2190
    goto _exit;
11,391✔
2191
  }
2192

2193
_exit:
8,301,938✔
2194
  pIter->iTColumn++;
8,301,938✔
2195
  return &pIter->cv;
8,301,938✔
2196
}
2197

2198
static int32_t tRowNoneUpsertColData(SColData *aColData, int32_t nColData, int32_t flag) {
1,570✔
2199
  int32_t code = 0;
1,570✔
2200

2201
  if (flag) return code;
1,570✔
2202

2203
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
13,335✔
2204
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
12,361✔
2205
    if (code) return code;
12,359!
2206
  }
2207

2208
  return code;
974✔
2209
}
2210
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
214,610✔
2211
  int32_t code = 0;
214,610✔
2212

2213
  int32_t   iColData = 0;
214,610✔
2214
  SColData *pColData = &aColData[iColData];
214,610✔
2215
  int32_t   iTColumn = 1;
214,610✔
2216
  STColumn *pTColumn = &pSchema->columns[iTColumn];
214,610✔
2217

2218
  while (pColData) {
1,070,107✔
2219
    if (pTColumn) {
855,912!
2220
      if (pTColumn->colId == pColData->cid) {  // NULL
855,912✔
2221
        if (flag == 0) {
855,872✔
2222
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
855,720✔
2223
        } else {
2224
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
152✔
2225
        }
2226
        if (code) goto _exit;
855,457!
2227

2228
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
855,457✔
2229
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
855,457✔
2230
      } else if (pTColumn->colId > pColData->cid) {  // NONE
40!
2231
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2232
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2233
      } else {
2234
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
40!
2235
      }
2236
    } else {  // NONE
2237
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2238
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2239
    }
2240
  }
2241

2242
_exit:
214,195✔
2243
  return code;
214,195✔
2244
}
2245
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
1,420,865,599✔
2246
                                      int32_t flag) {
2247
  int32_t code = 0;
1,420,865,599✔
2248

2249
  int32_t   iColData = 0;
1,420,865,599✔
2250
  SColData *pColData = &aColData[iColData];
1,420,865,599✔
2251
  int32_t   iTColumn = 1;
1,420,865,599✔
2252
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,420,865,599✔
2253

2254
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
1,420,865,599✔
2255
  SPrimaryKeyIndex index;
2256
  uint8_t         *data = pRow->data;
1,420,865,599✔
2257
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,886,546,498✔
2258
    data += tGetPrimaryKeyIndex(data, &index);
465,662,520✔
2259
  }
2260

2261
  switch (pRow->flag) {
1,420,883,978!
2262
    case HAS_VALUE:
1,405,592,937✔
2263
      pf = data;  // TODO: fix here
1,405,592,937✔
2264
      pv = pf + pTSchema->flen;
1,405,592,937✔
2265
      break;
1,405,592,937✔
2266
    case (HAS_NULL | HAS_NONE):
339✔
2267
      pb = data;
339✔
2268
      break;
339✔
2269
    case (HAS_VALUE | HAS_NONE):
16,047,110✔
2270
    case (HAS_VALUE | HAS_NULL):
2271
      pb = data;
16,047,110✔
2272
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
16,047,110✔
2273
      pv = pf + pTSchema->flen;
16,047,110✔
2274
      break;
16,047,110✔
2275
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2276
      pb = data;
×
2277
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2278
      pv = pf + pTSchema->flen;
×
2279
      break;
×
2280
    default:
×
2281
      return TSDB_CODE_INVALID_DATA_FMT;
×
2282
  }
2283

2284
  while (pColData) {
2,147,483,647✔
2285
    if (pTColumn) {
2,147,483,647✔
2286
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2287
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647!
2288
          return TSDB_CODE_INVALID_PARA;
×
2289
        }
2290
        if (pb) {
2,147,483,647✔
2291
          uint8_t bv;
2292
          switch (pRow->flag) {
61,725,488!
2293
            case (HAS_NULL | HAS_NONE):
6,579✔
2294
              bv = GET_BIT1(pb, iTColumn - 1);
6,579✔
2295
              break;
6,579✔
2296
            case (HAS_VALUE | HAS_NONE):
180,398✔
2297
              bv = GET_BIT1(pb, iTColumn - 1);
180,398✔
2298
              if (bv) bv++;
180,398✔
2299
              break;
180,398✔
2300
            case (HAS_VALUE | HAS_NULL):
61,539,137✔
2301
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
61,539,137✔
2302
              break;
61,539,137✔
2303
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2304
              bv = GET_BIT2(pb, iTColumn - 1);
×
2305
              break;
×
2306
            default:
×
2307
              return TSDB_CODE_INVALID_DATA_FMT;
×
2308
          }
2309

2310
          if (bv == BIT_FLG_NONE) {
61,726,114✔
2311
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
19,968!
2312
              goto _exit;
×
2313
            goto _continue;
19,960✔
2314
          } else if (bv == BIT_FLG_NULL) {
61,706,146✔
2315
            if (flag == 0) {
16,643,207!
2316
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
16,643,729✔
2317
            } else {
2318
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2319
            }
2320
            if (code) goto _exit;
16,646,149!
2321
            goto _continue;
16,646,149✔
2322
          }
2323
        }
2324

2325
        if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647!
2326
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
67,279,922!
2327
          uint32_t nData;
2328
          pData += tGetU32v(pData, &nData);
67,279,922✔
2329
          if (flag == 0) {
67,279,922!
2330
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
72,934,895✔
2331
          } else {
2332
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
2333
          }
2334
          if (code) goto _exit;
73,313,479!
2335
        } else {
2336
          if (flag == 0) {
2,147,483,647✔
2337
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
2338
                                                                          TYPE_BYTES[pColData->type]);
2,147,483,647✔
2339
          } else {
2340
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
3,729,072✔
2341
                                                                          TYPE_BYTES[pColData->type], flag > 0);
3,729,072✔
2342
          }
2343
          if (code) goto _exit;
2,147,483,647!
2344
        }
2345

2346
      _continue:
2,147,483,647✔
2347
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2348
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2349
      } else if (pTColumn->colId > pColData->cid) {  // NONE
643,347!
2350
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2351
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2352
      } else {
2353
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
643,347✔
2354
      }
2355
    } else {
2356
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
20,435!
2357
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
20,435✔
2358
    }
2359
  }
2360

2361
_exit:
1,420,837,007✔
2362
  return code;
1,420,837,007✔
2363
}
2364
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
3,124,898✔
2365
  int32_t code = 0;
3,124,898✔
2366

2367
  uint8_t  *pv = NULL;
3,124,898✔
2368
  int32_t   iColData = 0;
3,124,898✔
2369
  SColData *pColData = &aColData[iColData];
3,124,898✔
2370
  int32_t   iTColumn = 1;
3,124,898✔
2371
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
3,124,898✔
2372
  int32_t   iCol = 0;
3,124,898✔
2373

2374
  // primary keys
2375
  uint8_t         *data = pRow->data;
3,124,898✔
2376
  SPrimaryKeyIndex index;
2377
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
3,478,850✔
2378
    data += tGetPrimaryKeyIndex(data, &index);
353,967✔
2379
  }
2380

2381
  SKVIdx *pKVIdx = (SKVIdx *)data;
3,124,883✔
2382
  if (pRow->flag & KV_FLG_LIT) {
3,124,883✔
2383
    pv = pKVIdx->idx + pKVIdx->nCol;
3,097,336✔
2384
  } else if (pRow->flag & KV_FLG_MID) {
27,547!
2385
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
28,204✔
2386
  } else if (pRow->flag & KV_FLG_BIG) {
×
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
2388
  } else {
2389
    return TSDB_CODE_INVALID_PARA;
×
2390
  }
2391

2392
  while (pColData) {
32,989,038✔
2393
    if (pTColumn) {
29,869,031✔
2394
      if (pTColumn->colId == pColData->cid) {
29,868,999✔
2395
        while (iCol < pKVIdx->nCol) {
29,854,516✔
2396
          uint8_t *pData;
2397
          if (pRow->flag & KV_FLG_LIT) {
21,587,295✔
2398
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
20,810,165✔
2399
          } else if (pRow->flag & KV_FLG_MID) {
777,130!
2400
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
777,200✔
2401
          } else if (pRow->flag & KV_FLG_BIG) {
×
2402
            pData = pv + ((uint32_t *)pKVIdx->idx)[iCol];
×
2403
          } else {
2404
            return TSDB_CODE_INVALID_DATA_FMT;
×
2405
          }
2406

2407
          int16_t cid;
2408
          pData += tGetI16v(pData, &cid);
21,587,365✔
2409

2410
          if (TABS(cid) == pTColumn->colId) {
21,587,365✔
2411
            if (cid < 0) {
19,947,709✔
2412
              if (flag == 0) {
3,276,371✔
2413
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,274,179✔
2414
              } else {
2415
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
2,192✔
2416
              }
2417
              if (code) goto _exit;
3,137,168!
2418
            } else {
2419
              uint32_t nData;
2420
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
16,671,338!
2421
                pData += tGetU32v(pData, &nData);
2,814,798✔
2422
              } else {
2423
                nData = 0;
13,856,540✔
2424
              }
2425
              if (flag == 0) {
16,671,338!
2426
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
16,706,390✔
2427
              } else {
2428
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
×
2429
              }
2430
              if (code) goto _exit;
16,805,928!
2431
            }
2432
            iCol++;
19,943,096✔
2433
            goto _continue;
19,943,096✔
2434
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
1,639,656!
2435
            break;
1,644,915✔
2436
          } else {
2437
            iCol++;
×
2438
          }
2439
        }
2440

2441
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
9,912,136!
2442

2443
      _continue:
9,911,146✔
2444
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
29,854,242✔
2445
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
29,854,242✔
2446
      } else if (pTColumn->colId > pColData->cid) {
9,224!
2447
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2448
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2449
      } else {
2450
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
9,224✔
2451
      }
2452
    } else {
2453
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
32!
2454
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
32!
2455
    }
2456
  }
2457

2458
_exit:
3,120,007✔
2459
  return code;
3,120,007✔
2460
}
2461
/* flag > 0: forward update
2462
 * flag == 0: append
2463
 * flag < 0: backward update
2464
 */
2465
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
1,424,427,278✔
2466
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
1,424,427,278!
2467
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
1,424,427,278!
2468

2469
  if (pRow->flag == HAS_NONE) {
1,424,427,278✔
2470
    return tRowNoneUpsertColData(aColData, nColData, flag);
1,570✔
2471
  } else if (pRow->flag == HAS_NULL) {
1,424,425,708✔
2472
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
214,606✔
2473
  } else if (pRow->flag >> 4) {  // KV row
1,424,211,102✔
2474
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
3,125,648✔
2475
  } else {  // TUPLE row
2476
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,421,085,454✔
2477
  }
2478
}
2479

2480
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
2,147,483,647✔
2481
  key->numOfPKs = row->numOfPKs;
2,147,483,647✔
2482

2483
  if (key->numOfPKs == 0) {
2,147,483,647!
2484
    return;
×
2485
  }
2486

2487
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
2488

2489
  uint8_t *data = row->data;
2,147,483,647✔
2490

2491
  for (int32_t i = 0; i < row->numOfPKs; i++) {
2,147,483,647✔
2492
    data += tGetPrimaryKeyIndex(data, &indices[i]);
2,147,483,647✔
2493
  }
2494

2495
  // primary keys
2496
  for (int32_t i = 0; i < row->numOfPKs; i++) {
2,147,483,647✔
2497
    key->pks[i].type = indices[i].type;
2,147,483,647✔
2498

2499
    uint8_t *tdata = data + indices[i].offset;
2,147,483,647✔
2500
    if (row->flag >> 4) {
2,147,483,647✔
2501
      tdata += tGetI16v(tdata, NULL);
7,893,906✔
2502
    }
2503

2504
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
2,147,483,647!
2505
      key->pks[i].pData = tdata;
426,250✔
2506
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
852,500!
2507
    } else {
2508
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
2,147,483,647✔
2509
    }
2510
  }
2511
}
2512

2513
#define T_COMPARE_SCALAR_VALUE(TYPE, V1, V2)    \
2514
  do {                                          \
2515
    if (*(TYPE *)(V1) < *(TYPE *)(V2)) {        \
2516
      return -1;                                \
2517
    } else if (*(TYPE *)(V1) > *(TYPE *)(V2)) { \
2518
      return 1;                                 \
2519
    } else {                                    \
2520
      return 0;                                 \
2521
    }                                           \
2522
  } while (0)
2523

2524
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
92,810,713✔
2525
  switch (tv1->type) {
92,810,713!
2526
    case TSDB_DATA_TYPE_BOOL:
×
2527
    case TSDB_DATA_TYPE_TINYINT:
2528
      T_COMPARE_SCALAR_VALUE(int8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2529
    case TSDB_DATA_TYPE_SMALLINT:
×
2530
      T_COMPARE_SCALAR_VALUE(int16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2531
    case TSDB_DATA_TYPE_INT:
452,213✔
2532
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
452,213✔
2533
    case TSDB_DATA_TYPE_BIGINT:
92,105,461✔
2534
    case TSDB_DATA_TYPE_TIMESTAMP:
2535
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
92,105,461!
2536
    case TSDB_DATA_TYPE_FLOAT:
×
2537
      T_COMPARE_SCALAR_VALUE(float, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2538
    case TSDB_DATA_TYPE_DOUBLE:
×
2539
      T_COMPARE_SCALAR_VALUE(double, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2540
    case TSDB_DATA_TYPE_UTINYINT:
×
2541
      T_COMPARE_SCALAR_VALUE(uint8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2542
    case TSDB_DATA_TYPE_USMALLINT:
×
2543
      T_COMPARE_SCALAR_VALUE(uint16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2544
    case TSDB_DATA_TYPE_UINT:
64,650✔
2545
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
64,650✔
2546
    case TSDB_DATA_TYPE_UBIGINT:
65,091✔
2547
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
65,091✔
2548
    case TSDB_DATA_TYPE_GEOMETRY:
134,540✔
2549
    case TSDB_DATA_TYPE_BINARY: {
2550
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
134,540✔
2551
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
134,540✔
2552
    }
2553
    case TSDB_DATA_TYPE_NCHAR: {
×
2554
      int32_t ret = taosUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
2555
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2556
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2557
    }
2558
    case TSDB_DATA_TYPE_VARBINARY: {
×
2559
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2560
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2561
    }
2562
    default:
×
2563
      break;
×
2564
  }
2565

2566
  return 0;
×
2567
}
2568

2569
// NOTE:
2570
// set key->numOfPKs to 0 as the smallest key with ts
2571
// set key->numOfPKs to (TD_MAX_PK_COLS + 1) as the largest key with ts
2572
FORCE_INLINE int32_t tRowKeyCompare(const SRowKey *key1, const SRowKey *key2) {
2,147,483,647✔
2573
  if (key1->ts < key2->ts) {
2,147,483,647!
2574
    return -1;
2,147,483,647✔
2575
  } else if (key1->ts > key2->ts) {
2,147,483,647!
2576
    return 1;
2,147,483,647✔
2577
  }
2578

2579
  if (key1->numOfPKs == key2->numOfPKs) {
64,763,057!
2580
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
100,455,078!
2581
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
92,815,359✔
2582
      if (ret) return ret;
92,787,906!
2583
    }
2584
  } else if (key1->numOfPKs < key2->numOfPKs) {
×
2585
    return -1;
9✔
2586
  } else {
2587
    return 1;
×
2588
  }
2589

2590
  return 0;
7,639,719✔
2591
}
2592

2593
void tRowKeyAssign(SRowKey *pDst, SRowKey *pSrc) {
1,163,316,931✔
2594
  pDst->ts = pSrc->ts;
1,163,316,931✔
2595
  pDst->numOfPKs = pSrc->numOfPKs;
1,163,316,931✔
2596

2597
  if (pSrc->numOfPKs > 0) {
1,163,316,931✔
2598
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
109,637,195✔
2599
      SValue *pVal = &pDst->pks[i];
54,492,355✔
2600
      pVal->type = pSrc->pks[i].type;
54,492,355✔
2601

2602
      valueCloneDatum(pVal, pSrc->pks + i, pVal->type);
54,492,355✔
2603
    }
2604
  }
2605
}
1,163,967,757✔
2606

2607
// STag ========================================
2608
static int tTagValCmprFn(const void *p1, const void *p2) {
235,926,283✔
2609
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
235,926,283✔
2610
    return -1;
82,517,125✔
2611
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
153,409,158✔
2612
    return 1;
85,426,248✔
2613
  }
2614

2615
  return 0;
67,982,910✔
2616
}
2617
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
15,870✔
2618
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
15,870✔
2619
}
2620

2621
#ifdef TD_DEBUG_PRINT_TAG
2622
static void debugPrintTagVal(int8_t type, const void *val, int32_t vlen, const char *tag, int32_t ln) {
2623
  switch (type) {
2624
    case TSDB_DATA_TYPE_VARBINARY:
2625
    case TSDB_DATA_TYPE_JSON:
2626
    case TSDB_DATA_TYPE_VARCHAR:
2627
    case TSDB_DATA_TYPE_NCHAR:
2628
    case TSDB_DATA_TYPE_GEOMETRY: {
2629
      char tmpVal[32] = {0};
2630
      tstrncpy(tmpVal, val, vlen > 31 ? 31 : vlen);
2631
      printf("%s:%d type:%d vlen:%d, val:\"%s\"\n", tag, ln, (int32_t)type, vlen, tmpVal);
2632
    } break;
2633
    case TSDB_DATA_TYPE_FLOAT:
2634
      printf("%s:%d type:%d vlen:%d, val:%f\n", tag, ln, (int32_t)type, vlen, *(float *)val);
2635
      break;
2636
    case TSDB_DATA_TYPE_DOUBLE:
2637
      printf("%s:%d type:%d vlen:%d, val:%lf\n", tag, ln, (int32_t)type, vlen, *(double *)val);
2638
      break;
2639
    case TSDB_DATA_TYPE_BOOL:
2640
      printf("%s:%d type:%d vlen:%d, val:%" PRIu8 "\n", tag, ln, (int32_t)type, vlen, *(uint8_t *)val);
2641
      break;
2642
    case TSDB_DATA_TYPE_TINYINT:
2643
      printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val);
2644
      break;
2645
    case TSDB_DATA_TYPE_SMALLINT:
2646
      printf("%s:%d type:%d vlen:%d, val:%" PRIi16 "\n", tag, ln, (int32_t)type, vlen, *(int16_t *)val);
2647
      break;
2648
    case TSDB_DATA_TYPE_INT:
2649
      printf("%s:%d type:%d vlen:%d, val:%" PRIi32 "\n", tag, ln, (int32_t)type, vlen, *(int32_t *)val);
2650
      break;
2651
    case TSDB_DATA_TYPE_BIGINT:
2652
      printf("%s:%d type:%d vlen:%d, val:%" PRIi64 "\n", tag, ln, (int32_t)type, vlen, *(int64_t *)val);
2653
      break;
2654
    case TSDB_DATA_TYPE_TIMESTAMP:
2655
      printf("%s:%d type:%d vlen:%d, val:%" PRIi64 "\n", tag, ln, (int32_t)type, vlen, *(int64_t *)val);
2656
      break;
2657
    case TSDB_DATA_TYPE_UTINYINT:
2658
      printf("%s:%d type:%d vlen:%d, val:%" PRIu8 "\n", tag, ln, (int32_t)type, vlen, *(uint8_t *)val);
2659
      break;
2660
    case TSDB_DATA_TYPE_USMALLINT:
2661
      printf("%s:%d type:%d vlen:%d, val:%" PRIu16 "\n", tag, ln, (int32_t)type, vlen, *(uint16_t *)val);
2662
      break;
2663
    case TSDB_DATA_TYPE_UINT:
2664
      printf("%s:%d type:%d vlen:%d, val:%" PRIu32 "\n", tag, ln, (int32_t)type, vlen, *(uint32_t *)val);
2665
      break;
2666
    case TSDB_DATA_TYPE_UBIGINT:
2667
      printf("%s:%d type:%d vlen:%d, val:%" PRIu64 "\n", tag, ln, (int32_t)type, vlen, *(uint64_t *)val);
2668
      break;
2669
    case TSDB_DATA_TYPE_NULL:
2670
      printf("%s:%d type:%d vlen:%d, val:%" PRIi8 "\n", tag, ln, (int32_t)type, vlen, *(int8_t *)val);
2671
      break;
2672
    default:
2673
      break;
2674
  }
2675
}
2676

2677
void debugPrintSTag(STag *pTag, const char *tag, int32_t ln) {
2678
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
2679
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
2680
  uint8_t *p = NULL;
2681
  int16_t  offset = 0;
2682

2683
  if (isLarge) {
2684
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
2685
  } else {
2686
    p = (uint8_t *)&pTag->idx[pTag->nTag];
2687
  }
2688
  printf("%s:%d >>> STAG === %s:%s, len: %d, nTag: %d, sver:%d\n", tag, ln, isJson ? "json" : "normal",
2689
         isLarge ? "large" : "small", (int32_t)pTag->len, (int32_t)pTag->nTag, pTag->ver);
2690
  for (uint16_t n = 0; n < pTag->nTag; ++n) {
2691
    if (isLarge) {
2692
      offset = ((int16_t *)pTag->idx)[n];
2693
    } else {
2694
      offset = pTag->idx[n];
2695
    }
2696
    STagVal tagVal = {0};
2697
    if (isJson) {
2698
      tagVal.pKey = (char *)POINTER_SHIFT(p, offset);
2699
    } else {
2700
      tagVal.cid = *(int16_t *)POINTER_SHIFT(p, offset);
2701
    }
2702
    printf("%s:%d loop[%d-%d] offset=%d\n", __func__, __LINE__, (int32_t)pTag->nTag, (int32_t)n, (int32_t)offset);
2703
    tGetTagVal(p + offset, &tagVal, isJson);
2704
    if (IS_VAR_DATA_TYPE(tagVal.type)) {
2705
      debugPrintTagVal(tagVal.type, tagVal.pData, tagVal.nData, __func__, __LINE__);
2706
    } else {
2707
      debugPrintTagVal(tagVal.type, &tagVal.i64, tDataTypes[tagVal.type].bytes, __func__, __LINE__);
2708
    }
2709
  }
2710
  printf("\n");
2711
}
2712
#endif
2713

2714
static int32_t tPutTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
422,450✔
2715
  int32_t n = 0;
422,450✔
2716

2717
  // key
2718
  if (isJson) {
422,450✔
2719
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
2,440✔
2720
  } else {
2721
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
842,460✔
2722
  }
2723

2724
  // type
2725
  n += tPutI8(p ? p + n : p, pTagVal->type);
422,450✔
2726

2727
  // value
2728
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
422,450!
2729
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
276,220✔
2730
  } else {
2731
    p = p ? p + n : p;
284,340✔
2732
    n += tDataTypes[pTagVal->type].bytes;
284,340✔
2733
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
284,340✔
2734
  }
2735

2736
  return n;
422,450✔
2737
}
2738
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
233,665,031✔
2739
  int32_t n = 0;
233,665,031✔
2740

2741
  // key
2742
  if (isJson) {
233,665,031✔
2743
    n += tGetCStr(p + n, &pTagVal->pKey);
33,816!
2744
  } else {
2745
    n += tGetI16v(p + n, &pTagVal->cid);
467,296,246!
2746
  }
2747

2748
  // type
2749
  n += tGetI8(p + n, &pTagVal->type);
233,665,031!
2750

2751
  // value
2752
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
233,665,031!
2753
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
82,282,416!
2754
  } else {
2755
    (void)memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
192,523,823✔
2756
    n += tDataTypes[pTagVal->type].bytes;
192,523,823✔
2757
  }
2758

2759
  return n;
233,665,031✔
2760
}
2761

2762
bool tTagIsJson(const void *pTag) { return (((const STag *)pTag)->flags & TD_TAG_JSON); }
25,412✔
2763

2764
bool tTagIsJsonNull(void *data) {
3,321✔
2765
  STag  *pTag = (STag *)data;
3,321✔
2766
  int8_t isJson = tTagIsJson(pTag);
3,321✔
2767
  if (!isJson) return false;
3,321✔
2768
  return ((STag *)data)->nTag == 0;
1,387✔
2769
}
2770

2771
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
97,575✔
2772
  int32_t  code = 0;
97,575✔
2773
  uint8_t *p = NULL;
97,575✔
2774
  int16_t  n = 0;
97,575✔
2775
  int16_t  nTag = taosArrayGetSize(pArray);
97,575✔
2776
  int32_t  szTag = 0;
97,591✔
2777
  int8_t   isLarge = 0;
97,591✔
2778

2779
  // sort
2780
  if (isJson) {
97,591✔
2781
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
302✔
2782
  } else {
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
97,289✔
2784
  }
2785

2786
  // get size
2787
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
308,870✔
2788
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
211,293✔
2789
  }
2790
  if (szTag <= INT8_MAX) {
97,577✔
2791
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
92,291✔
2792
  } else {
2793
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
5,286✔
2794
    isLarge = 1;
5,286✔
2795
  }
2796

2797
  // build tag
2798
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
97,577!
2799
  if ((*ppTag) == NULL) {
97,637!
2800
    code = terrno;
×
2801
    goto _err;
×
2802
  }
2803
  (*ppTag)->flags = 0;
97,637✔
2804
  if (isJson) {
97,637✔
2805
    (*ppTag)->flags |= TD_TAG_JSON;
302✔
2806
  }
2807
  if (isLarge) {
97,637✔
2808
    (*ppTag)->flags |= TD_TAG_LARGE;
5,302✔
2809
  }
2810
  (*ppTag)->len = szTag;
97,637✔
2811
  (*ppTag)->nTag = nTag;
97,637✔
2812
  (*ppTag)->ver = version;
97,637✔
2813

2814
  if (isLarge) {
97,637✔
2815
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
5,302✔
2816
  } else {
2817
    p = (uint8_t *)&(*ppTag)->idx[nTag];
92,335✔
2818
  }
2819
  n = 0;
97,637✔
2820
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
309,010✔
2821
    if (isLarge) {
211,384✔
2822
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
64,032✔
2823
    } else {
2824
      (*ppTag)->idx[iTag] = n;
147,352✔
2825
    }
2826
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
211,384✔
2827
  }
2828
#ifdef TD_DEBUG_PRINT_TAG
2829
  debugPrintSTag(*ppTag, __func__, __LINE__);
2830
#endif
2831

2832
  return code;
97,626✔
2833

2834
_err:
×
2835
  return code;
×
2836
}
2837

2838
void tTagFree(STag *pTag) {
11,086✔
2839
  if (pTag) taosMemoryFree(pTag);
11,086!
2840
}
11,086✔
2841

2842
char *tTagValToData(const STagVal *value, bool isJson) {
21,530,627✔
2843
  if (!value) {
21,530,627!
2844
    return NULL;
×
2845
  }
2846

2847
  char  *data = NULL;
21,530,627✔
2848
  int8_t typeBytes = 0;
21,530,627✔
2849
  if (isJson) {
21,530,627✔
2850
    typeBytes = CHAR_BYTES;
8,389✔
2851
  }
2852

2853
  if (IS_VAR_DATA_TYPE(value->type)) {
21,530,627!
2854
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
4,910,771!
2855
    if (data == NULL) {
4,911,608!
2856
      return NULL;
×
2857
    }
2858

2859
    if (isJson) {
4,911,608✔
2860
      *data = value->type;
6,642✔
2861
    }
2862

2863
    varDataLen(data + typeBytes) = value->nData;
4,911,608✔
2864
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
4,911,608✔
2865
  } else {
2866
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
16,619,856✔
2867
  }
2868

2869
  return data;
21,531,464✔
2870
}
2871

2872
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
68,368,689✔
2873
  if (!pTag || !pTagVal) {
68,368,689!
2874
    return false;
×
2875
  }
2876

2877
  int16_t  lidx = 0;
68,570,884✔
2878
  int16_t  ridx = pTag->nTag - 1;
68,570,884✔
2879
  int16_t  midx;
2880
  uint8_t *p;
2881
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
68,570,884✔
2882
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
68,570,884✔
2883
  int16_t  offset;
2884
  STagVal  tv;
2885
  int      c;
2886

2887
  if (isLarge) {
68,570,884✔
2888
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
65,202,615✔
2889
  } else {
2890
    p = (uint8_t *)&pTag->idx[pTag->nTag];
3,368,269✔
2891
  }
2892

2893
  pTagVal->type = TSDB_DATA_TYPE_NULL;
68,570,884✔
2894
  pTagVal->pData = NULL;
68,570,884✔
2895
  pTagVal->nData = 0;
68,570,884✔
2896
  while (lidx <= ridx) {
236,117,786✔
2897
    midx = (lidx + ridx) / 2;
234,776,328✔
2898
    if (isLarge) {
234,776,328✔
2899
      offset = ((int16_t *)pTag->idx)[midx];
212,871,168✔
2900
    } else {
2901
      offset = pTag->idx[midx];
21,905,160✔
2902
    }
2903

2904
    int32_t nt = tGetTagVal(p + offset, &tv, isJson);
234,776,328✔
2905
    if (isJson) {
236,002,885✔
2906
      c = tTagValJsonCmprFn(pTagVal, &tv);
15,510✔
2907
    } else {
2908
      c = tTagValCmprFn(pTagVal, &tv);
235,987,375✔
2909
    }
2910

2911
    if (c < 0) {
239,105,985✔
2912
      ridx = midx - 1;
82,112,454✔
2913
    } else if (c > 0) {
156,993,531✔
2914
      lidx = midx + 1;
85,434,448✔
2915
    } else {
2916
      (void)memcpy(pTagVal, &tv, sizeof(tv));
71,559,083✔
2917
      return true;
71,559,083✔
2918
    }
2919
  }
2920
  return false;
1,341,458✔
2921
}
2922

2923
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
749,302✔
2924
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
1,498,604✔
2925
}
2926

2927
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
33,113,588✔
2928

2929
int32_t tTagToValArray(const STag *pTag, SArray **ppArray) {
1,432✔
2930
  int32_t  code = 0;
1,432✔
2931
  uint8_t *p = NULL;
1,432✔
2932
  STagVal  tv = {0};
1,432✔
2933
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
1,432✔
2934
  int16_t  offset = 0;
1,432✔
2935

2936
  if (isLarge) {
1,432✔
2937
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
11✔
2938
  } else {
2939
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,421✔
2940
  }
2941

2942
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
1,432✔
2943
  if (*ppArray == NULL) {
1,432!
2944
    code = terrno;
×
2945
    goto _err;
×
2946
  }
2947

2948
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
3,562✔
2949
    if (isLarge) {
2,130✔
2950
      offset = ((int16_t *)pTag->idx)[iTag];
17✔
2951
    } else {
2952
      offset = pTag->idx[iTag];
2,113✔
2953
    }
2954
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
2,130✔
2955
    if (taosArrayPush(*ppArray, &tv) == NULL) {
4,260!
2956
      code = terrno;
×
2957
      goto _err;
×
2958
    }
2959
  }
2960

2961
  return code;
1,432✔
2962

2963
_err:
×
2964
  return code;
×
2965
}
2966

2967
// STSchema ========================================
2968
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
24,011,957✔
2969
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
24,011,957!
2970
  if (pTSchema == NULL) {
24,080,702!
2971
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2972
    return NULL;
×
2973
  }
2974

2975
  pTSchema->numOfCols = numOfCols;
24,080,702✔
2976
  pTSchema->version = version;
24,080,702✔
2977

2978
  // timestamp column
2979
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
24,080,702!
2980
    terrno = TSDB_CODE_INVALID_PARA;
×
2981
    taosMemoryFree(pTSchema);
×
2982
    return NULL;
×
2983
  }
2984
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
24,080,702!
2985
    terrno = TSDB_CODE_INVALID_PARA;
×
2986
    taosMemoryFree(pTSchema);
×
2987
    return NULL;
×
2988
  }
2989
  pTSchema->columns[0].colId = aSchema[0].colId;
24,080,702✔
2990
  pTSchema->columns[0].type = aSchema[0].type;
24,080,702✔
2991
  pTSchema->columns[0].flags = aSchema[0].flags;
24,080,702✔
2992
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
24,080,702✔
2993
  pTSchema->columns[0].offset = -1;
24,080,702✔
2994

2995
  // other columns
2996
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
275,091,477✔
2997
    SSchema  *pSchema = &aSchema[iCol];
251,010,775✔
2998
    STColumn *pTColumn = &pTSchema->columns[iCol];
251,010,775✔
2999

3000
    pTColumn->colId = pSchema->colId;
251,010,775✔
3001
    pTColumn->type = pSchema->type;
251,010,775✔
3002
    pTColumn->flags = pSchema->flags;
251,010,775✔
3003
    pTColumn->offset = pTSchema->flen;
251,010,775✔
3004

3005
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
251,010,775!
3006
      pTColumn->bytes = pSchema->bytes;
31,921,902✔
3007
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
31,921,902✔
3008
    } else {
3009
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
219,088,873✔
3010
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
219,088,873✔
3011
    }
3012

3013
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
251,010,775✔
3014
  }
3015

3016
#if 1  // todo : remove this
3017
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
24,080,702✔
3018
#endif
3019

3020
  return pTSchema;
24,080,702✔
3021
}
3022

3023
static int32_t tTColumnCompare(const void *p1, const void *p2) {
18,592,697✔
3024
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
18,592,697✔
3025
    return -1;
2,757,566✔
3026
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
15,835,131✔
3027
    return 1;
11,105,392✔
3028
  }
3029

3030
  return 0;
4,729,739✔
3031
}
3032

3033
const STColumn *tTSchemaSearchColumn(const STSchema *pTSchema, int16_t cid) {
4,730,322✔
3034
  STColumn tcol = {
4,730,322✔
3035
      .colId = cid,
3036
  };
3037

3038
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
4,730,322✔
3039
}
3040

3041
// SColData ========================================
3042
void tColDataDestroy(void *ph) {
35,916,990✔
3043
  if (ph) {
35,916,990!
3044
    SColData *pColData = (SColData *)ph;
35,917,108✔
3045

3046
    tFree(pColData->pBitMap);
35,917,108!
3047
    tFree(pColData->aOffset);
35,917,126!
3048
    tFree(pColData->pData);
35,917,121!
3049
  }
3050
}
35,917,943✔
3051

3052
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
36,447,225✔
3053
  pColData->cid = cid;
36,447,225✔
3054
  pColData->type = type;
36,447,225✔
3055
  pColData->cflag = cflag;
36,447,225✔
3056
  tColDataClear(pColData);
36,447,225✔
3057
}
36,451,402✔
3058

3059
void tColDataClear(SColData *pColData) {
74,592,521✔
3060
  pColData->numOfNone = 0;
74,592,521✔
3061
  pColData->numOfNull = 0;
74,592,521✔
3062
  pColData->numOfValue = 0;
74,592,521✔
3063
  pColData->nVal = 0;
74,592,521✔
3064
  pColData->flag = 0;
74,592,521✔
3065
  pColData->nData = 0;
74,592,521✔
3066
}
74,592,521✔
3067

3068
void tColDataDeepClear(SColData *pColData) {
76,160✔
3069
  pColData->pBitMap = NULL;
76,160✔
3070
  pColData->aOffset = NULL;
76,160✔
3071
  pColData->pData = NULL;
76,160✔
3072

3073
  tColDataClear(pColData);
76,160✔
3074
}
76,171✔
3075

3076
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, uint8_t *pData, uint32_t nData) {
3077
  int32_t  code = 0;
2,147,483,647✔
3078
  uint32_t cvtNData = sizeof(uint64_t);
2,147,483,647✔
3079
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647!
3080
    // TODO
3081
    if (IS_STR_DATA_BLOB(pColData->type)) {
97,951,340!
3082
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
×
3083
      if (code) goto _exit;
×
3084
      pColData->aOffset[pColData->nVal] = pColData->nData;
×
3085

3086
      if (nData) {
×
3087
        code = tRealloc(&pColData->pData, pColData->nData + BSE_SEQUECE_SIZE);
×
3088
        if (code) goto _exit;
×
3089
        (void)memcpy(pColData->pData + pColData->nData, pData, BSE_SEQUECE_SIZE);
×
3090
        pColData->nData += BSE_SEQUECE_SIZE;
×
3091
      } else {
3092
        // uint64_t zero = 0;
3093
        // (void)memcpy(pColData->pData + pColData->nData, &zero, BSE_SEQUECE_SIZE);
3094
        // pColData->nData += BSE_SEQUECE_SIZE;
3095
      }
3096

3097
    } else {
3098
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
115,813,746!
3099
      if (code) goto _exit;
116,123,457!
3100
      pColData->aOffset[pColData->nVal] = pColData->nData;
116,123,457✔
3101

3102
      if (nData) {
116,123,457!
3103
        code = tRealloc(&pColData->pData, pColData->nData + nData);
112,260,608!
3104
        if (code) goto _exit;
112,863,347!
3105
        (void)memcpy(pColData->pData + pColData->nData, pData, nData);
112,863,347✔
3106
        pColData->nData += nData;
112,863,347✔
3107
      }
3108
    }
3109
  } else {
3110
    if (!(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal)) {
2,147,483,647!
3111
      return TSDB_CODE_INVALID_PARA;
×
3112
    }
3113
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
2,147,483,647!
3114
    if (code) goto _exit;
2,147,483,647!
3115
    if (pData) {
2,147,483,647!
3116
      (void)memcpy(pColData->pData + pColData->nData, pData, TYPE_BYTES[pColData->type]);
2,147,483,647✔
3117
    } else {
3118
      memset(pColData->pData + pColData->nData, 0, TYPE_BYTES[pColData->type]);
39,124,816✔
3119
    }
3120
    pColData->nData += tDataTypes[pColData->type].bytes;
2,147,483,647✔
3121
  }
3122
  pColData->nVal++;
2,147,483,647✔
3123

3124
_exit:
2,147,483,647✔
3125
  return code;
2,147,483,647✔
3126
}
3127
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,055,955✔
3128
  pColData->flag = HAS_VALUE;
3,056,110✔
3129
  pColData->numOfValue++;
3,055,955✔
3130
  return tColDataPutValue(pColData, pData, nData);
3,056,744✔
3131
}
3132
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
205,845✔
3133
  pColData->flag = HAS_NONE;
205,845✔
3134
  pColData->numOfNone++;
205,845✔
3135
  pColData->nVal++;
205,845✔
3136
  return 0;
205,845✔
3137
}
3138
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
16,307✔
3139
  pColData->flag = HAS_NULL;
16,350✔
3140
  pColData->numOfNull++;
16,350✔
3141
  pColData->nVal++;
16,350✔
3142
  return 0;
16,307✔
3143
}
3144
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,396✔
3145
  int32_t code = 0;
1,396✔
3146

3147
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
1,396✔
3148
  code = tRealloc(&pColData->pBitMap, nBit);
1,396!
3149
  if (code) return code;
1,396!
3150

3151
  memset(pColData->pBitMap, 0, nBit);
1,396✔
3152
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
1,396!
3153

3154
  pColData->flag |= HAS_VALUE;
1,396✔
3155
  pColData->numOfValue++;
1,396✔
3156

3157
  if (pColData->nVal) {
1,396!
3158
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,396!
3159
      if (IS_STR_DATA_BLOB(pColData->type)) {
67!
3160
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3161
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3162
        if (code) return code;
×
3163
        memset(pColData->aOffset, 0, nOffset);
×
3164

3165
      } else {
3166
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
67✔
3167
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
67!
3168
        if (code) return code;
67!
3169
        memset(pColData->aOffset, 0, nOffset);
67✔
3170
      }
3171
    } else {
3172
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
1,329✔
3173
      code = tRealloc(&pColData->pData, pColData->nData);
1,329!
3174
      if (code) return code;
1,329!
3175
      memset(pColData->pData, 0, pColData->nData);
1,329✔
3176
    }
3177
  }
3178

3179
  return tColDataPutValue(pColData, pData, nData);
1,396✔
3180
}
3181
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
17,860,976✔
3182
  pColData->nVal++;
17,860,976✔
3183
  pColData->numOfNone++;
17,860,976✔
3184
  return 0;
17,860,976✔
3185
}
3186
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
121✔
3187
  int32_t code = 0;
121✔
3188

3189
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
121✔
3190
  code = tRealloc(&pColData->pBitMap, nBit);
121!
3191
  if (code) return code;
121!
3192

3193
  memset(pColData->pBitMap, 0, nBit);
121✔
3194
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
121!
3195

3196
  pColData->flag |= HAS_NULL;
121✔
3197
  pColData->numOfNull++;
121✔
3198
  pColData->nVal++;
121✔
3199

3200
  return code;
121✔
3201
}
3202
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
11,843✔
3203
  int32_t code = 0;
11,843✔
3204

3205
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
11,843✔
3206
  code = tRealloc(&pColData->pBitMap, nBit);
11,843!
3207
  if (code) return code;
11,844!
3208

3209
  memset(pColData->pBitMap, 0, nBit);
11,844✔
3210
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
11,844!
3211

3212
  pColData->flag |= HAS_VALUE;
11,844✔
3213
  pColData->numOfValue++;
11,844✔
3214

3215
  if (pColData->nVal) {
11,844!
3216
    if (IS_VAR_DATA_TYPE(pColData->type)) {
12,217!
3217
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
373✔
3218
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
373!
3219
      if (code) return code;
373!
3220
      if (!IS_STR_DATA_BLOB(pColData->type)) {
373!
3221
        memset(pColData->aOffset, 0, nOffset);
373✔
3222
      }
3223
    } else {
3224
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
11,471✔
3225
      code = tRealloc(&pColData->pData, pColData->nData);
11,471!
3226
      if (code) return code;
11,471!
3227
      memset(pColData->pData, 0, pColData->nData);
11,471✔
3228
    }
3229
  }
3230

3231
  return tColDataPutValue(pColData, pData, nData);
11,844✔
3232
}
3233
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
20✔
3234
  int32_t code = 0;
20✔
3235

3236
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
20✔
3237
  code = tRealloc(&pColData->pBitMap, nBit);
20✔
3238
  if (code) return code;
20!
3239

3240
  memset(pColData->pBitMap, 255, nBit);
20✔
3241
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
20!
3242

3243
  pColData->flag |= HAS_NONE;
20✔
3244
  pColData->numOfNone++;
20✔
3245
  pColData->nVal++;
20✔
3246

3247
  return code;
20✔
3248
}
3249
static FORCE_INLINE int32_t tColDataAppendValue22(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,282,019✔
3250
  pColData->nVal++;
1,282,019✔
3251
  pColData->numOfNull++;
1,282,019✔
3252
  return 0;
1,282,019✔
3253
}
3254
static FORCE_INLINE int32_t tColDataAppendValue30(SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3255
  int32_t code = 0;
×
3256

3257
  pColData->flag |= HAS_VALUE;
×
3258
  pColData->numOfValue++;
×
3259

3260
  uint8_t *pBitMap = NULL;
×
3261
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3262
  if (code) return code;
×
3263

3264
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3265
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
3266
  }
3267
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
3268

3269
  tFree(pColData->pBitMap);
×
3270
  pColData->pBitMap = pBitMap;
×
3271

3272
  if (pColData->nVal) {
×
3273
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3274
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3275
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3276
      if (code) return code;
×
3277
      memset(pColData->aOffset, 0, nOffset);
×
3278
    } else {
3279
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
3280
      code = tRealloc(&pColData->pData, pColData->nData);
×
3281
      if (code) return code;
×
3282
      memset(pColData->pData, 0, pColData->nData);
×
3283
    }
3284
  }
3285

3286
  return tColDataPutValue(pColData, pData, nData);
×
3287
}
3288
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, uint8_t *pData, uint32_t nData) {
80✔
3289
  int32_t code = 0;
80✔
3290

3291
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
80!
3292
  if (code) return code;
80!
3293

3294
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
80!
3295
  pColData->numOfNone++;
80✔
3296
  pColData->nVal++;
80✔
3297

3298
  return code;
80✔
3299
}
3300
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,879✔
3301
  int32_t code = 0;
1,879✔
3302

3303
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,879!
3304
  if (code) return code;
1,878!
3305

3306
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
1,878!
3307
  pColData->numOfNull++;
1,878✔
3308
  pColData->nVal++;
1,878✔
3309

3310
  return code;
1,878✔
3311
}
3312
static FORCE_INLINE int32_t tColDataAppendValue40(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3313
  pColData->numOfValue++;
2,147,483,647✔
3314
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3315
}
3316
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, uint8_t *pData, uint32_t nData) {
18,386✔
3317
  int32_t code = 0;
18,386✔
3318

3319
  pColData->flag |= HAS_NONE;
18,386✔
3320
  pColData->numOfNone++;
18,386✔
3321

3322
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
18,386✔
3323
  code = tRealloc(&pColData->pBitMap, nBit);
18,386✔
3324
  if (code) return code;
18,404!
3325

3326
  memset(pColData->pBitMap, 255, nBit);
18,404✔
3327
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
18,404✔
3328

3329
  return tColDataPutValue(pColData, NULL, 0);
18,398✔
3330
}
3331
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
182,926✔
3332
  int32_t code = 0;
183,989✔
3333

3334
  pColData->flag |= HAS_NULL;
183,989✔
3335
  pColData->numOfNull++;
183,989✔
3336

3337
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
183,989✔
3338
  code = tRealloc(&pColData->pBitMap, nBit);
183,928✔
3339
  if (code) return code;
183,991!
3340

3341
  memset(pColData->pBitMap, 255, nBit);
183,991✔
3342
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
183,991!
3343

3344
  return tColDataPutValue(pColData, NULL, 0);
183,992✔
3345
}
3346
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,263,055✔
3347
  int32_t code = 0;
1,263,456✔
3348

3349
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,263,455!
3350
  if (code) return code;
1,263,343!
3351

3352
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
1,263,343!
3353
  pColData->numOfValue++;
1,263,343!
3354

3355
  return tColDataPutValue(pColData, pData, nData);
1,265,616✔
3356
}
3357
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,279,840✔
3358
  int32_t code = 0;
1,279,840✔
3359

3360
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,279,840!
3361
  if (code) return code;
1,279,917!
3362

3363
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,279,917✔
3364
  pColData->numOfNone++;
1,279,917✔
3365

3366
  return tColDataPutValue(pColData, NULL, 0);
1,280,336✔
3367
}
3368
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
4✔
3369
  int32_t code = 0;
4✔
3370

3371
  pColData->flag |= HAS_NULL;
4✔
3372
  pColData->numOfNull++;
4✔
3373

3374
  uint8_t *pBitMap = NULL;
4✔
3375
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
4!
3376
  if (code) return code;
4!
3377

3378
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
942!
3379
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
938!
3380
  }
3381
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
4!
3382

3383
  tFree(pColData->pBitMap);
4!
3384
  pColData->pBitMap = pBitMap;
4!
3385

3386
  return tColDataPutValue(pColData, NULL, 0);
4✔
3387
}
3388
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
553,106,818✔
3389
  int32_t code = 0;
553,107,467✔
3390

3391
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
553,107,387!
3392
  if (code) return code;
554,047,104!
3393
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
554,047,104!
3394
  pColData->numOfValue++;
554,047,104✔
3395

3396
  return tColDataPutValue(pColData, pData, nData);
555,118,110✔
3397
}
3398
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,358✔
3399
  int32_t code = 0;
3,358✔
3400

3401
  pColData->flag |= HAS_NONE;
3,358✔
3402
  pColData->numOfNone++;
3,358✔
3403

3404
  uint8_t *pBitMap = NULL;
3,358✔
3405
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
3,358!
3406
  if (code) return code;
3,358!
3407

3408
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
282,577✔
3409
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
279,219✔
3410
  }
3411
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
3,358✔
3412

3413
  tFree(pColData->pBitMap);
3,358!
3414
  pColData->pBitMap = pBitMap;
3,358✔
3415

3416
  return tColDataPutValue(pColData, NULL, 0);
3,359✔
3417
}
3418
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
32,090,389✔
3419
  int32_t code = 0;
32,093,272✔
3420

3421
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
32,092,488!
3422
  if (code) return code;
32,096,954!
3423
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
32,096,954✔
3424
  pColData->numOfNull++;
32,096,954✔
3425

3426
  return tColDataPutValue(pColData, NULL, 0);
32,105,493✔
3427
}
3428
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,068✔
3429
  int32_t code = 0;
3,068✔
3430

3431
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
3,068!
3432
  if (code) return code;
3,068!
3433
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
3,068!
3434
  pColData->numOfValue++;
3,068!
3435

3436
  return tColDataPutValue(pColData, pData, nData);
3,068✔
3437
}
3438
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
495,642✔
3439
  int32_t code = 0;
495,642✔
3440

3441
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
495,642!
3442
  if (code) return code;
495,642!
3443
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
495,642✔
3444
  pColData->numOfNone++;
495,642!
3445

3446
  return tColDataPutValue(pColData, NULL, 0);
495,642✔
3447
}
3448
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
13✔
3449
  int32_t code = 0;
13✔
3450

3451
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
13!
3452
  if (code) return code;
13!
3453
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
13!
3454
  pColData->numOfNull++;
13!
3455

3456
  return tColDataPutValue(pColData, NULL, 0);
13✔
3457
}
3458
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData) = {
3459
    {tColDataAppendValue00, tColDataAppendValue01, tColDataAppendValue02},  // 0
3460
    {tColDataAppendValue10, tColDataAppendValue11, tColDataAppendValue12},  // HAS_NONE
3461
    {tColDataAppendValue20, tColDataAppendValue21, tColDataAppendValue22},  // HAS_NULL
3462
    {tColDataAppendValue30, tColDataAppendValue31, tColDataAppendValue32},  // HAS_NULL|HAS_NONE
3463
    {tColDataAppendValue40, tColDataAppendValue41, tColDataAppendValue42},  // HAS_VALUE
3464
    {tColDataAppendValue50, tColDataAppendValue51, tColDataAppendValue52},  // HAS_VALUE|HAS_NONE
3465
    {tColDataAppendValue60, tColDataAppendValue61, tColDataAppendValue62},  // HAS_VALUE|HAS_NULL
3466
    {tColDataAppendValue70, tColDataAppendValue71, tColDataAppendValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
3467

3468
    //       VALUE                  NONE                     NULL
3469
};
3470

3471
static FORCE_INLINE int32_t tColDataPutValueBlob(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
3472
  int32_t  code = 0;
×
3473
  uint8_t  buf[sizeof(uint64_t) + 1] = {0};
×
3474
  uint64_t seq = 0;
×
3475

3476
  int32_t offset = 0;
×
3477
  if (IS_STR_DATA_BLOB(pColData->type)) {
×
3478
    code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
×
3479
    if (code) goto _exit;
×
3480
    pColData->aOffset[pColData->nVal] = pColData->nData;
×
3481
    if (nData) {
×
3482
      SBlobItem item = {.seqOffsetInRow = seq, .len = nData, .data = pData, .type = TSDB_DATA_BLOB_VALUE};
×
3483
      code = tBlobSetPush(pArg, &item, &seq, 0);
×
3484
      if (code != 0) return code;
×
3485

3486
      offset = tPutU64(buf, seq);
×
3487
      code = tRealloc(&pColData->pData, pColData->nData + offset);
×
3488
      if (code != 0) return code;
×
3489
      memcpy(pColData->pData + pColData->nData, buf, offset);
×
3490
      pColData->nData += offset;
×
3491
    } else {
3492
      int8_t type = pData ? TSDB_DATA_BLOB_EMPTY_VALUE : TSDB_DATA_BLOB_NULL_VALUE;
×
3493
      code = addEmptyItemToBlobSet(pArg, type, NULL);
×
3494
    }
3495
  }
3496
  pColData->nVal++;
×
3497

3498
_exit:
×
3499
  return code;
×
3500
}
3501

3502
static FORCE_INLINE int32_t tColDataAppendValueBlob00(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3503
  pColData->flag = HAS_VALUE;
×
3504
  pColData->numOfValue++;
×
3505
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3506
}
3507
static FORCE_INLINE int32_t tColDataAppendValueBlob01(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3508
  pColData->flag = HAS_NONE;
×
3509
  pColData->numOfNone++;
×
3510
  pColData->nVal++;
×
3511
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3512
}
3513
static FORCE_INLINE int32_t tColDataAppendValueBlob02(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3514
  pColData->flag = HAS_NULL;
×
3515
  pColData->numOfNull++;
×
3516
  pColData->nVal++;
×
3517
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3518
}
3519
static FORCE_INLINE int32_t tColDataAppendValueBlob10(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3520
  int32_t code = 0;
×
3521

3522
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3523
  code = tRealloc(&pColData->pBitMap, nBit);
×
3524
  if (code) return code;
×
3525

3526
  memset(pColData->pBitMap, 0, nBit);
×
3527
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3528

3529
  pColData->flag |= HAS_VALUE;
×
3530
  pColData->numOfValue++;
×
3531

3532
  if (pColData->nVal) {
×
3533
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3534
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3535
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3536
      if (code) return code;
×
3537
      memset(pColData->aOffset, 0, nOffset);
×
3538
    } else {
3539
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
3540
      code = tRealloc(&pColData->pData, pColData->nData);
×
3541
      if (code) return code;
×
3542
      memset(pColData->pData, 0, pColData->nData);
×
3543
    }
3544
  }
3545

3546
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3547
}
3548
static FORCE_INLINE int32_t tColDataAppendValueBlob11(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3549
  pColData->nVal++;
×
3550
  pColData->numOfNone++;
×
3551
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3552
}
3553
static FORCE_INLINE int32_t tColDataAppendValueBlob12(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3554
  int32_t code = 0;
×
3555

3556
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3557
  code = tRealloc(&pColData->pBitMap, nBit);
×
3558
  if (code) return code;
×
3559

3560
  memset(pColData->pBitMap, 0, nBit);
×
3561
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3562

3563
  pColData->flag |= HAS_NULL;
×
3564
  pColData->numOfNull++;
×
3565
  pColData->nVal++;
×
3566
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3567
}
3568
static FORCE_INLINE int32_t tColDataAppendValueBlob20(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3569
  int32_t code = 0;
×
3570

3571
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3572
  code = tRealloc(&pColData->pBitMap, nBit);
×
3573
  if (code) return code;
×
3574

3575
  memset(pColData->pBitMap, 0, nBit);
×
3576
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3577

3578
  pColData->flag |= HAS_VALUE;
×
3579
  pColData->numOfValue++;
×
3580

3581
  if (pColData->nVal) {
×
3582
    if (IS_STR_DATA_BLOB(pColData->type)) {
×
3583
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3584
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3585
      if (code) return code;
×
3586
      memset(pColData->aOffset, 0, nOffset);
×
3587
    } else {
3588
      return TSDB_CODE_INVALID_MSG;
×
3589
    }
3590
  }
3591

3592
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3593
}
3594
static FORCE_INLINE int32_t tColDataAppendValueBlob21(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3595
  int32_t code = 0;
×
3596

3597
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3598
  code = tRealloc(&pColData->pBitMap, nBit);
×
3599
  if (code) return code;
×
3600

3601
  memset(pColData->pBitMap, 255, nBit);
×
3602
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3603

3604
  pColData->flag |= HAS_NONE;
×
3605
  pColData->numOfNone++;
×
3606
  pColData->nVal++;
×
3607
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3608
}
3609
static FORCE_INLINE int32_t tColDataAppendValueBlob22(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3610
  pColData->nVal++;
×
3611
  pColData->numOfNull++;
×
3612
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3613
}
3614
static FORCE_INLINE int32_t tColDataAppendValueBlob30(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3615
  int32_t code = 0;
×
3616

3617
  pColData->flag |= HAS_VALUE;
×
3618
  pColData->numOfValue++;
×
3619

3620
  uint8_t *pBitMap = NULL;
×
3621
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3622
  if (code) return code;
×
3623

3624
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3625
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
3626
  }
3627
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
3628

3629
  tFree(pColData->pBitMap);
×
3630
  pColData->pBitMap = pBitMap;
×
3631

3632
  if (pColData->nVal) {
×
3633
    if (IS_STR_DATA_BLOB(pColData->type)) {
×
3634
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3635
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3636
      if (code) return code;
×
3637
      memset(pColData->aOffset, 0, nOffset);
×
3638
    } else {
3639
      return TSDB_CODE_INVALID_MSG;
×
3640
    }
3641
  }
3642

3643
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3644
}
3645
static FORCE_INLINE int32_t tColDataAppendValueBlob31(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3646
  int32_t code = 0;
×
3647

3648
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3649
  if (code) return code;
×
3650

3651
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3652
  pColData->numOfNone++;
×
3653
  pColData->nVal++;
×
3654

3655
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3656
}
3657
static FORCE_INLINE int32_t tColDataAppendValueBlob32(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3658
  int32_t code = 0;
×
3659

3660
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3661
  if (code) return code;
×
3662

3663
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3664
  pColData->numOfNull++;
×
3665
  pColData->nVal++;
×
3666
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3667
}
3668
static FORCE_INLINE int32_t tColDataAppendValueBlob40(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3669
  pColData->numOfValue++;
×
3670
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3671
}
3672
static FORCE_INLINE int32_t tColDataAppendValueBlob41(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3673
  int32_t code = 0;
×
3674

3675
  pColData->flag |= HAS_NONE;
×
3676
  pColData->numOfNone++;
×
3677

3678
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3679
  code = tRealloc(&pColData->pBitMap, nBit);
×
3680
  if (code) return code;
×
3681

3682
  memset(pColData->pBitMap, 255, nBit);
×
3683
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3684

3685
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3686
}
3687
static FORCE_INLINE int32_t tColDataAppendValueBlob42(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3688
  int32_t code = 0;
×
3689

3690
  pColData->flag |= HAS_NULL;
×
3691
  pColData->numOfNull++;
×
3692

3693
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3694
  code = tRealloc(&pColData->pBitMap, nBit);
×
3695
  if (code) return code;
×
3696

3697
  memset(pColData->pBitMap, 255, nBit);
×
3698
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3699

3700
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3701
}
3702
static FORCE_INLINE int32_t tColDataAppendValueBlob50(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3703
  int32_t code = 0;
×
3704

3705
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3706
  if (code) return code;
×
3707

3708
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3709
  pColData->numOfValue++;
×
3710

3711
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3712
}
3713
static FORCE_INLINE int32_t tColDataAppendValueBlob51(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3714
  int32_t code = 0;
×
3715

3716
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3717
  if (code) return code;
×
3718

3719
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3720
  pColData->numOfNone++;
×
3721

3722
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3723
}
3724
static FORCE_INLINE int32_t tColDataAppendValueBlob52(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3725
  int32_t code = 0;
×
3726

3727
  pColData->flag |= HAS_NULL;
×
3728
  pColData->numOfNull++;
×
3729

3730
  uint8_t *pBitMap = NULL;
×
3731
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3732
  if (code) return code;
×
3733

3734
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3735
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
×
3736
  }
3737
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
×
3738

3739
  tFree(pColData->pBitMap);
×
3740
  pColData->pBitMap = pBitMap;
×
3741

3742
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3743
}
3744
static FORCE_INLINE int32_t tColDataAppendValueBlob60(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3745
  int32_t code = 0;
×
3746

3747
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3748
  if (code) return code;
×
3749
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3750
  pColData->numOfValue++;
×
3751

3752
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3753
}
3754
static FORCE_INLINE int32_t tColDataAppendValueBlob61(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3755
  int32_t code = 0;
×
3756

3757
  pColData->flag |= HAS_NONE;
×
3758
  pColData->numOfNone++;
×
3759

3760
  uint8_t *pBitMap = NULL;
×
3761
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3762
  if (code) return code;
×
3763

3764
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3765
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
×
3766
  }
3767
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
×
3768

3769
  tFree(pColData->pBitMap);
×
3770
  pColData->pBitMap = pBitMap;
×
3771

3772
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3773
}
3774
static FORCE_INLINE int32_t tColDataAppendValueBlob62(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3775
  int32_t code = 0;
×
3776

3777
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3778
  if (code) return code;
×
3779
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3780
  pColData->numOfNull++;
×
3781

3782
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3783
}
3784
static FORCE_INLINE int32_t tColDataAppendValueBlob70(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3785
  int32_t code = 0;
×
3786

3787
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3788
  if (code) return code;
×
3789
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
×
3790
  pColData->numOfValue++;
×
3791

3792
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3793
}
3794
static FORCE_INLINE int32_t tColDataAppendValueBlob71(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3795
  int32_t code = 0;
×
3796

3797
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3798
  if (code) return code;
×
3799
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
×
3800
  pColData->numOfNone++;
×
3801

3802
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3803
}
3804
static FORCE_INLINE int32_t tColDataAppendValueBlob72(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3805
  int32_t code = 0;
×
3806

3807
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3808
  if (code) return code;
×
3809
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
×
3810
  pColData->numOfNull++;
×
3811

3812
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3813
}
3814

3815
static int32_t (*tColDataAppendValueBlobImpl[8][3])(void *pDst, SColData *pColData, uint8_t *pData, uint32_t nData) = {
3816
    {tColDataAppendValueBlob00, tColDataAppendValueBlob01, tColDataAppendValueBlob02},  // 0
3817
    {tColDataAppendValueBlob10, tColDataAppendValueBlob11, tColDataAppendValueBlob12},  // HAS_NONE
3818
    {tColDataAppendValueBlob20, tColDataAppendValueBlob21, tColDataAppendValueBlob22},  // HAS_NULL
3819
    {tColDataAppendValueBlob30, tColDataAppendValueBlob31, tColDataAppendValueBlob32},  // HAS_NULL|HAS_NONE
3820
    {tColDataAppendValueBlob40, tColDataAppendValueBlob41, tColDataAppendValueBlob42},  // HAS_VALUE
3821
    {tColDataAppendValueBlob50, tColDataAppendValueBlob51, tColDataAppendValueBlob52},  // HAS_VALUE|HAS_NONE
3822
    {tColDataAppendValueBlob60, tColDataAppendValueBlob61, tColDataAppendValueBlob62},  // HAS_VALUE|HAS_NULL
3823
    {tColDataAppendValueBlob70, tColDataAppendValueBlob71, tColDataAppendValueBlob72}   // HAS_VALUE|HAS_NULL|HAS_NONE
3824
    //       VALUE                  NONE                     NULL
3825
};
3826
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal) {
2,147,483,647✔
3827
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) {
2,147,483,647!
3828
    return TSDB_CODE_INVALID_PARA;
×
3829
  }
3830
  return tColDataAppendValueImpl[pColData->flag][pColVal->flag](
2,147,483,647✔
3831
      pColData, VALUE_GET_DATUM(&pColVal->value, pColData->type), pColVal->value.nData);
2,147,483,647!
3832
}
3833

3834
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
7✔
3835
  pColData->numOfNone--;
7✔
3836
  pColData->nVal--;
7✔
3837
  if (pColData->numOfNone) {
7!
3838
    return tColDataAppendValue10(pColData, pData, nData);
×
3839
  } else {
3840
    pColData->flag = 0;
7✔
3841
    return tColDataAppendValue00(pColData, pData, nData);
7✔
3842
  }
3843
}
3844
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
6✔
3845
  pColData->numOfNone--;
6✔
3846
  pColData->nVal--;
6✔
3847
  if (pColData->numOfNone) {
6!
3848
    return tColDataAppendValue12(pColData, pData, nData);
×
3849
  } else {
3850
    pColData->flag = 0;
6✔
3851
    return tColDataAppendValue02(pColData, pData, nData);
6✔
3852
  }
3853
  return 0;
3854
}
3855
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
148✔
3856
  if (forward) {
148!
3857
    pColData->numOfNull--;
148✔
3858
    pColData->nVal--;
148✔
3859
    if (pColData->numOfNull) {
148!
3860
      return tColDataAppendValue20(pColData, pData, nData);
×
3861
    } else {
3862
      pColData->flag = 0;
148✔
3863
      return tColDataAppendValue00(pColData, pData, nData);
148✔
3864
    }
3865
  }
3866
  return 0;
×
3867
}
3868
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3869
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3870
    pColData->numOfNone--;
×
3871
    pColData->nVal--;
×
3872
    if (pColData->numOfNone) {
×
3873
      return tColDataAppendValue30(pColData, pData, nData);
×
3874
    } else {
3875
      pColData->flag = HAS_NULL;
×
3876
      return tColDataAppendValue20(pColData, pData, nData);
×
3877
    }
3878
  } else if (forward) {  // NULL ==> VALUE
×
3879
    pColData->numOfNull--;
×
3880
    pColData->nVal--;
×
3881
    if (pColData->numOfNull) {
×
3882
      return tColDataAppendValue30(pColData, pData, nData);
×
3883
    } else {
3884
      pColData->flag = HAS_NONE;
×
3885
      return tColDataAppendValue10(pColData, pData, nData);
×
3886
    }
3887
  }
3888
  return 0;
×
3889
}
3890
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
20✔
3891
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
20!
3892
    pColData->numOfNone--;
20✔
3893
    pColData->numOfNull++;
20✔
3894
    if (pColData->numOfNone) {
20!
3895
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
×
3896
    } else {
3897
      pColData->flag = HAS_NULL;
20✔
3898
    }
3899
  }
3900
  return 0;
20✔
3901
}
3902
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
13,856,135✔
3903
  if (forward) {  // VALUE ==> VALUE
13,856,135!
3904
    pColData->nVal--;
13,856,752✔
3905
    if (IS_VAR_DATA_TYPE(pColData->type)) {
13,856,752!
3906
      pColData->nData = pColData->aOffset[pColData->nVal];
2,220,458✔
3907
    } else {
3908
      pColData->nData -= TYPE_BYTES[pColData->type];
11,636,294✔
3909
    }
3910
    return tColDataPutValue(pColData, pData, nData);
13,997,147✔
3911
  }
3912
  return 0;
×
3913
}
3914
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,039✔
3915
  if (forward) {  // VALUE ==> NULL
1,039!
3916
    pColData->numOfValue--;
1,039✔
3917
    pColData->nVal--;
1,039✔
3918
    if (pColData->numOfValue) {
1,039✔
3919
      if (IS_VAR_DATA_TYPE(pColData->type)) {
1,002!
3920
        pColData->nData = pColData->aOffset[pColData->nVal];
200✔
3921
      } else {
3922
        pColData->nData -= TYPE_BYTES[pColData->type];
802✔
3923
      }
3924
      return tColDataAppendValue42(pColData, pData, nData);
1,001✔
3925
    } else {
3926
      pColData->flag = 0;
37✔
3927
      pColData->nData = 0;
37✔
3928
      return tColDataAppendValue02(pColData, pData, nData);
37✔
3929
    }
3930
  }
3931
  return 0;
×
3932
}
3933
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,154✔
3934
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
5,154✔
3935
    pColData->numOfNone--;
1,120✔
3936
    pColData->nVal--;
1,120✔
3937
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
1,120!
3938
      pColData->nData -= TYPE_BYTES[pColData->type];
896✔
3939
    }
3940
    if (pColData->numOfNone) {
1,120✔
3941
      return tColDataAppendValue50(pColData, pData, nData);
400✔
3942
    } else {
3943
      pColData->flag = HAS_VALUE;
720✔
3944
      return tColDataAppendValue40(pColData, pData, nData);
720✔
3945
    }
3946
  } else if (forward) {  // VALUE ==> VALUE
4,034!
3947
    pColData->nVal--;
4,034✔
3948
    if (IS_VAR_DATA_TYPE(pColData->type)) {
4,034!
3949
      pColData->nData = pColData->aOffset[pColData->nVal];
576✔
3950
    } else {
3951
      pColData->nData -= TYPE_BYTES[pColData->type];
3,458✔
3952
    }
3953
    return tColDataPutValue(pColData, pData, nData);
4,034✔
3954
  }
3955
  return 0;
×
3956
}
3957
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
61✔
3958
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
61!
3959
    pColData->numOfNone--;
61✔
3960
    pColData->nVal--;
61✔
3961
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
61!
3962
      pColData->nData -= TYPE_BYTES[pColData->type];
49✔
3963
    }
3964
    if (pColData->numOfNone) {
61!
3965
      return tColDataAppendValue52(pColData, pData, nData);
×
3966
    } else {
3967
      pColData->flag = HAS_VALUE;
61!
3968
      return tColDataAppendValue42(pColData, pData, nData);
61✔
3969
    }
3970
  } else if (forward) {  // VALUE ==> NULL
×
3971
    pColData->numOfValue--;
×
3972
    pColData->nVal--;
×
3973
    if (pColData->numOfValue) {
×
3974
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3975
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3976
      } else {
3977
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3978
      }
3979
      return tColDataAppendValue52(pColData, pData, nData);
×
3980
    } else {
3981
      pColData->flag = HAS_NONE;
×
3982
      pColData->nData = 0;
×
3983
      return tColDataAppendValue12(pColData, pData, nData);
×
3984
    }
3985
  }
3986
  return 0;
×
3987
}
3988
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
8,003✔
3989
  if (forward) {
8,003!
3990
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
8,003✔
3991
      pColData->numOfNull--;
5,524✔
3992
      pColData->nVal--;
5,524✔
3993
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
5,524!
3994
        pColData->nData -= TYPE_BYTES[pColData->type];
4,439✔
3995
      }
3996
      if (pColData->numOfNull) {
5,524✔
3997
        return tColDataAppendValue60(pColData, pData, nData);
569✔
3998
      } else {
3999
        pColData->flag = HAS_VALUE;
4,955✔
4000
        return tColDataAppendValue40(pColData, pData, nData);
4,954✔
4001
      }
4002
    } else {  // VALUE ==> VALUE
4003
      pColData->nVal--;
2,479✔
4004
      if (IS_VAR_DATA_TYPE(pColData->type)) {
2,479!
4005
        pColData->nData = pColData->aOffset[pColData->nVal];
477✔
4006
      } else {
4007
        pColData->nData -= TYPE_BYTES[pColData->type];
2,002✔
4008
      }
4009
      return tColDataPutValue(pColData, pData, nData);
2,479✔
4010
    }
4011
  }
4012
  return 0;
×
4013
}
4014
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
5,253✔
4015
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
5,253!
4016
    pColData->numOfValue--;
2,099✔
4017
    pColData->nVal--;
2,099✔
4018
    if (pColData->numOfValue) {
2,099!
4019
      if (IS_VAR_DATA_TYPE(pColData->type)) {
2,099!
4020
        pColData->nData = pColData->aOffset[pColData->nVal];
441✔
4021
      } else {
4022
        pColData->nData -= TYPE_BYTES[pColData->type];
1,658✔
4023
      }
4024
      return tColDataAppendValue62(pColData, pData, nData);
2,099✔
4025
    } else {
4026
      pColData->flag = HAS_NULL;
×
4027
      pColData->nData = 0;
×
4028
      return tColDataAppendValue20(pColData, pData, nData);
×
4029
    }
4030
  }
4031
  return 0;
3,154✔
4032
}
4033
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
89✔
4034
  int32_t code = 0;
89✔
4035

4036
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
89✔
4037
  if (bv == 0) {  // NONE ==> VALUE
89✔
4038
    pColData->numOfNone--;
80✔
4039
    pColData->nVal--;
80✔
4040
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
80!
4041
      pColData->nData -= TYPE_BYTES[pColData->type];
64✔
4042
    }
4043
    if (pColData->numOfNone) {
80!
4044
      return tColDataAppendValue70(pColData, pData, nData);
×
4045
    } else {
4046
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
4,240✔
4047
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
4,160✔
4048
      }
4049
      pColData->flag = (HAS_VALUE | HAS_NULL);
80!
4050
      return tColDataAppendValue60(pColData, pData, nData);
80✔
4051
    }
4052
  } else if (bv == 1) {  // NULL ==> VALUE
9✔
4053
    if (forward) {
1!
4054
      pColData->numOfNull--;
1✔
4055
      pColData->nVal--;
1✔
4056
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
1!
4057
        pColData->nData -= TYPE_BYTES[pColData->type];
1✔
4058
      }
4059
      if (pColData->numOfNull) {
1!
4060
        return tColDataAppendValue70(pColData, pData, nData);
×
4061
      } else {
4062
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
3✔
4063
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
2✔
4064
        }
4065
        pColData->flag = (HAS_VALUE | HAS_NONE);
1!
4066
        return tColDataAppendValue50(pColData, pData, nData);
1✔
4067
      }
4068
    }
4069
  } else if (bv == 2) {  // VALUE ==> VALUE
8!
4070
    if (forward) {
8!
4071
      pColData->nVal--;
8✔
4072
      if (IS_VAR_DATA_TYPE(pColData->type)) {
8!
4073
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4074
      } else {
4075
        pColData->nData -= TYPE_BYTES[pColData->type];
8✔
4076
      }
4077
      return tColDataPutValue(pColData, pData, nData);
8✔
4078
    }
4079
  } else {
4080
    return TSDB_CODE_INVALID_PARA;
×
4081
  }
4082
  return 0;
×
4083
}
4084
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
784✔
4085
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
784✔
4086
  if (bv == 0) {  // NONE ==> NULL
784!
4087
    pColData->numOfNone--;
784✔
4088
    pColData->nVal--;
784✔
4089
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
784!
4090
      pColData->nData -= TYPE_BYTES[pColData->type];
543✔
4091
    }
4092
    if (pColData->numOfNone) {
784!
4093
      return tColDataAppendValue72(pColData, pData, nData);
×
4094
    } else {
4095
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
25,000✔
4096
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
24,216✔
4097
      }
4098
      pColData->flag = (HAS_VALUE | HAS_NULL);
784✔
4099
      return tColDataAppendValue62(pColData, pData, nData);
783✔
4100
    }
4101
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
×
4102
    pColData->numOfValue--;
×
4103
    pColData->nVal--;
×
4104
    if (pColData->numOfValue) {
×
4105
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
4106
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4107
      } else {
4108
        pColData->nData -= TYPE_BYTES[pColData->type];
×
4109
      }
4110
      return tColDataAppendValue72(pColData, pData, nData);
×
4111
    } else {
4112
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4113
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
×
4114
      }
4115
      pColData->flag = (HAS_NULL | HAS_NONE);
×
4116
      pColData->nData = 0;
×
4117
      return tColDataAppendValue32(pColData, pData, nData);
×
4118
    }
4119
  }
4120
  return 0;
×
4121
}
4122
static FORCE_INLINE int32_t tColDataUpdateNothing(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
4,794✔
4123
  return 0;
4,794✔
4124
}
4125
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
4126
    {NULL, NULL, NULL},                                                     // 0
4127
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
4128
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
4129
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
4130
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
4131
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
4132
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
4133
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
4134

4135
    //    VALUE             NONE        NULL
4136
};
4137
int32_t tColDataUpdateValue(SColData *pColData, SColVal *pColVal, bool forward) {
12,818,984✔
4138
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) return TSDB_CODE_INVALID_PARA;
12,818,984!
4139
  if (!(pColData->nVal > 0)) return TSDB_CODE_INVALID_PARA;
12,822,309!
4140

4141
  if (tColDataUpdateValueImpl[pColData->flag][pColVal->flag] == NULL) return 0;
12,822,309!
4142

4143
  return tColDataUpdateValueImpl[pColData->flag][pColVal->flag](
12,822,309✔
4144
      pColData, VALUE_GET_DATUM(&pColVal->value, pColData->type), pColVal->value.nData, forward);
12,822,309!
4145
}
4146

4147
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
163,690,133✔
4148
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
163,690,133✔
4149
}
163,690,133✔
4150
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
814,921✔
4151
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
814,921✔
4152
}
814,921✔
4153
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
2,200✔
4154
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4155
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,200!
4156
    case 0:
200✔
4157
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
200✔
4158
      break;
200✔
4159
    case 1:
2,000✔
4160
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
2,000✔
4161
      break;
2,000✔
4162
    default:
×
4163
      break;
×
4164
  }
4165
}
2,200✔
4166
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4167
  SValue value = {.type = pColData->type};
2,147,483,647✔
4168
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647!
4169
    if (iVal + 1 < pColData->nVal) {
344,732,975!
4170
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
370,241,706✔
4171
    } else {
4172
      value.nData = pColData->nData - pColData->aOffset[iVal];
×
4173
    }
4174
    value.pData = pColData->pData + pColData->aOffset[iVal];
344,732,975✔
4175
  } else {
4176
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4177
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4178
  }
4179
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4180
}
281,806,665✔
4181
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
1,055,162✔
4182
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4183
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
1,055,162!
4184
    case 0:
491,279✔
4185
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
491,279✔
4186
      break;
491,279✔
4187
    case 1:
577,624✔
4188
      tColDataGetValue4(pColData, iVal, pColVal);
4189
      break;
581,361✔
4190
    default:
×
4191
      break;
×
4192
  }
4193
}
1,058,899✔
4194
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
299,026,061✔
4195
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4196
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
299,026,061!
4197
    case 0:
19,534,966✔
4198
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
19,534,966✔
4199
      break;
19,534,966✔
4200
    case 1:
279,919,605✔
4201
      tColDataGetValue4(pColData, iVal, pColVal);
4202
      break;
281,220,454✔
4203
    default:
×
4204
      break;
×
4205
  }
4206
}
300,326,910✔
4207
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
5,458✔
4208
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4209
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
5,458!
4210
    case 0:
610✔
4211
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
610✔
4212
      break;
610✔
4213
    case 1:
29✔
4214
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
29✔
4215
      break;
29✔
4216
    case 2:
4,819!
4217
      tColDataGetValue4(pColData, iVal, pColVal);
4218
      break;
4,819✔
4219
    default:
×
4220
      break;
×
4221
  }
4222
}
5,458✔
4223
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4224
    NULL,               // 0
4225
    tColDataGetValue1,  // HAS_NONE
4226
    tColDataGetValue2,  // HAS_NULL
4227
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4228
    tColDataGetValue4,  // HAS_VALUE
4229
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4230
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4231
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4232
};
4233
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4234
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647!
4235
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647!
4236
    return TSDB_CODE_INVALID_PARA;
×
4237
  }
4238
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4239
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4240
}
4241

4242
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
735,298,118✔
4243
  switch (pColData->flag) {
735,298,118!
4244
    case HAS_NONE:
×
4245
      return 0;
×
4246
    case HAS_NULL:
×
4247
      return 1;
×
4248
    case (HAS_NULL | HAS_NONE):
2,200✔
4249
      return GET_BIT1(pColData->pBitMap, iVal);
2,200✔
4250
    case HAS_VALUE:
×
4251
      return 2;
×
4252
    case (HAS_VALUE | HAS_NONE):
6,933,895✔
4253
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
6,933,895✔
4254
    case (HAS_VALUE | HAS_NULL):
729,227,617✔
4255
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
729,227,617✔
4256
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
140,306✔
4257
      return GET_BIT2(pColData->pBitMap, iVal);
140,306✔
4258
    default:
×
4259
      return 0;
×
4260
  }
4261
}
4262

4263
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
6,136✔
4264
  int32_t code = 0;
6,136✔
4265

4266
  *pColData = *pColDataFrom;
6,136✔
4267

4268
  // bitmap
4269
  switch (pColData->flag) {
6,136!
4270
    case (HAS_NULL | HAS_NONE):
105✔
4271
    case (HAS_VALUE | HAS_NONE):
4272
    case (HAS_VALUE | HAS_NULL):
4273
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
105✔
4274
      if (pColData->pBitMap == NULL) {
105!
4275
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4276
        goto _exit;
×
4277
      }
4278
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
105✔
4279
      break;
105✔
4280
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
4281
      pColData->pBitMap = xMalloc(arg, BIT2_SIZE(pColData->nVal));
×
4282
      if (pColData->pBitMap == NULL) {
×
4283
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4284
        goto _exit;
×
4285
      }
4286
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT2_SIZE(pColData->nVal));
×
4287
      break;
×
4288
    default:
6,031✔
4289
      pColData->pBitMap = NULL;
6,031✔
4290
      break;
6,031✔
4291
  }
4292

4293
  // offset
4294
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
6,136!
4295
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
728✔
4296
    if (pColData->aOffset == NULL) {
728!
4297
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4298
      goto _exit;
×
4299
    }
4300
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
728✔
4301
  } else {
4302
    pColData->aOffset = NULL;
5,408✔
4303
  }
4304

4305
  // value
4306
  if (pColData->nData) {
6,136✔
4307
    pColData->pData = xMalloc(arg, pColData->nData);
5,972✔
4308
    if (pColData->pData == NULL) {
5,972!
4309
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4310
      goto _exit;
×
4311
    }
4312

4313
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
5,972✔
4314
  } else {
4315
    pColData->pData = NULL;
164✔
4316
  }
4317

4318
_exit:
6,136✔
4319
  return code;
6,136✔
4320
}
4321

4322
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
2,671,543✔
4323
  int32_t code;
4324
  SBuffer local;
4325

4326
  if (!(colData->nVal > 0)) {
2,671,543!
4327
    return TSDB_CODE_INVALID_PARA;
×
4328
  }
4329

4330
  (*info) = (SColDataCompressInfo){
2,671,543✔
4331
      .cmprAlg = info->cmprAlg,
2,671,543✔
4332
      .columnFlag = colData->cflag,
2,671,543✔
4333
      .flag = colData->flag,
2,671,543✔
4334
      .dataType = colData->type,
2,671,543✔
4335
      .columnId = colData->cid,
2,671,543✔
4336
      .numOfData = colData->nVal,
2,671,543✔
4337
  };
4338

4339
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
2,671,543!
4340
    return 0;
3,968✔
4341
  }
4342

4343
  tBufferInit(&local);
4344
  if (assist == NULL) {
2,667,575!
4345
    assist = &local;
×
4346
  }
4347

4348
  // bitmap
4349
  if (colData->flag != HAS_VALUE) {
2,667,575✔
4350
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
184,275✔
4351
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
2,498✔
4352
    } else {
4353
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
181,777✔
4354
    }
4355

4356
    SCompressInfo cinfo = {
184,275✔
4357
        .dataType = TSDB_DATA_TYPE_TINYINT,
4358
        .cmprAlg = info->cmprAlg,
184,275✔
4359
        .originalSize = info->bitmapOriginalSize,
184,275✔
4360
    };
4361

4362
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
184,275✔
4363
    if (code) {
184,284!
4364
      tBufferDestroy(&local);
4365
      return code;
×
4366
    }
4367

4368
    info->bitmapCompressedSize = cinfo.compressedSize;
184,284✔
4369
  }
4370

4371
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
2,667,584✔
4372
    tBufferDestroy(&local);
4373
    return 0;
121✔
4374
  }
4375

4376
  // offset
4377
  if (IS_VAR_DATA_TYPE(colData->type)) {
2,667,463!
4378
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
88,459✔
4379

4380
    SCompressInfo cinfo = {
88,459✔
4381
        .dataType = TSDB_DATA_TYPE_INT,
4382
        .cmprAlg = info->cmprAlg,
88,459✔
4383
        .originalSize = info->offsetOriginalSize,
88,459✔
4384
    };
4385

4386
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
88,459✔
4387
    if (code) {
88,416!
4388
      tBufferDestroy(&local);
4389
      return code;
×
4390
    }
4391

4392
    info->offsetCompressedSize = cinfo.compressedSize;
88,416✔
4393
  }
4394

4395
  // data
4396
  if (colData->nData > 0) {
2,667,420!
4397
    info->dataOriginalSize = colData->nData;
2,667,435✔
4398

4399
    SCompressInfo cinfo = {
2,667,435✔
4400
        .dataType = colData->type,
2,667,435✔
4401
        .cmprAlg = info->cmprAlg,
2,667,435✔
4402
        .originalSize = info->dataOriginalSize,
2,667,435✔
4403
    };
4404

4405
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
2,667,435✔
4406
    if (code) {
2,667,649!
4407
      tBufferDestroy(&local);
4408
      return code;
×
4409
    }
4410

4411
    info->dataCompressedSize = cinfo.compressedSize;
2,667,649✔
4412
  }
4413

4414
  tBufferDestroy(&local);
4415
  return 0;
2,667,634✔
4416
}
4417

4418
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
34,276,844✔
4419
  int32_t  code;
4420
  SBuffer  local;
4421
  uint8_t *data = (uint8_t *)input;
34,276,844✔
4422

4423
  tBufferInit(&local);
4424
  if (assist == NULL) {
34,276,844!
4425
    assist = &local;
×
4426
  }
4427

4428
  tColDataClear(colData);
34,276,844✔
4429
  colData->cid = info->columnId;
34,275,657✔
4430
  colData->type = info->dataType;
34,275,657✔
4431
  colData->cflag = info->columnFlag;
34,275,657✔
4432
  colData->nVal = info->numOfData;
34,275,657✔
4433
  colData->flag = info->flag;
34,275,657✔
4434

4435
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
34,275,657✔
4436
    goto _exit;
4,802,038✔
4437
  }
4438

4439
  // bitmap
4440
  if (info->bitmapOriginalSize > 0) {
29,473,619✔
4441
    SCompressInfo cinfo = {
301,069✔
4442
        .dataType = TSDB_DATA_TYPE_TINYINT,
4443
        .cmprAlg = info->cmprAlg,
301,069✔
4444
        .originalSize = info->bitmapOriginalSize,
301,069✔
4445
        .compressedSize = info->bitmapCompressedSize,
301,069✔
4446
    };
4447

4448
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
301,069!
4449
    if (code) {
301,023!
4450
      tBufferDestroy(&local);
4451
      return code;
×
4452
    }
4453

4454
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
301,023✔
4455
    if (code) {
301,097!
4456
      tBufferDestroy(&local);
4457
      return code;
×
4458
    }
4459

4460
    data += cinfo.compressedSize;
301,097✔
4461
  }
4462

4463
  if (info->flag == (HAS_NONE | HAS_NULL)) {
29,473,647✔
4464
    goto _exit;
120✔
4465
  }
4466

4467
  // offset
4468
  if (info->offsetOriginalSize > 0) {
29,473,527✔
4469
    SCompressInfo cinfo = {
4,884,136✔
4470
        .cmprAlg = info->cmprAlg,
4,884,136✔
4471
        .dataType = TSDB_DATA_TYPE_INT,
4472
        .originalSize = info->offsetOriginalSize,
4,884,136✔
4473
        .compressedSize = info->offsetCompressedSize,
4,884,136✔
4474
    };
4475

4476
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
4,884,136!
4477
    if (code) {
4,884,108!
4478
      tBufferDestroy(&local);
4479
      return code;
×
4480
    }
4481

4482
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
4,884,108✔
4483
    if (code) {
4,884,268!
4484
      tBufferDestroy(&local);
4485
      return code;
×
4486
    }
4487

4488
    data += cinfo.compressedSize;
4,884,268✔
4489
  }
4490

4491
  // data
4492
  if (info->dataOriginalSize > 0) {
29,473,659✔
4493
    colData->nData = info->dataOriginalSize;
29,472,566✔
4494

4495
    SCompressInfo cinfo = {
29,472,566✔
4496
        .cmprAlg = info->cmprAlg,
29,472,566✔
4497
        .dataType = colData->type,
29,472,566✔
4498
        .originalSize = info->dataOriginalSize,
29,472,566✔
4499
        .compressedSize = info->dataCompressedSize,
29,472,566✔
4500
    };
4501

4502
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
29,472,566!
4503
    if (code) {
29,471,006!
4504
      tBufferDestroy(&local);
4505
      return code;
×
4506
    }
4507

4508
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
29,471,006✔
4509
    if (code) {
29,472,037!
4510
      tBufferDestroy(&local);
4511
      return code;
×
4512
    }
4513

4514
    data += cinfo.compressedSize;
29,472,037✔
4515
  }
4516

4517
_exit:
1,093✔
4518
  switch (colData->flag) {
34,275,288✔
4519
    case HAS_NONE:
4,730,649✔
4520
      colData->numOfNone = colData->nVal;
4,730,649✔
4521
      break;
4,730,649✔
4522
    case HAS_NULL:
73,989✔
4523
      colData->numOfNull = colData->nVal;
73,989✔
4524
      break;
73,989✔
4525
    case HAS_VALUE:
29,170,341✔
4526
      colData->numOfValue = colData->nVal;
29,170,341✔
4527
      break;
29,170,341✔
4528
    default:
300,309✔
4529
      for (int32_t i = 0; i < colData->nVal; i++) {
405,226,816✔
4530
        uint8_t bitValue = tColDataGetBitValue(colData, i);
404,927,680✔
4531
        if (bitValue == 0) {
404,926,507✔
4532
          colData->numOfNone++;
586,515✔
4533
        } else if (bitValue == 1) {
404,339,992✔
4534
          colData->numOfNull++;
37,361,804✔
4535
        } else {
4536
          colData->numOfValue++;
366,978,188✔
4537
        }
4538
      }
4539
  }
4540
  tBufferDestroy(&local);
4541
  return 0;
34,274,115✔
4542
}
4543

4544
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
545✔
4545
                                    char *data) {
4546
  int32_t code = 0;
545✔
4547
  if (data == NULL) {
545✔
4548
    if (pColData->cflag & COL_IS_KEY) {
13!
4549
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4550
    } else {
4551
      for (int32_t i = 0; i < nRows; ++i) {
28✔
4552
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
15✔
4553
      }
4554
    }
4555
    goto _exit;
13✔
4556
  }
4557

4558
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
532!
4559
    if (!IS_STR_DATA_BLOB(type)) {
92!
4560
      for (int32_t i = 0; i < nRows; ++i) {
265✔
4561
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
173✔
4562
        if (offset == -1) {
173!
4563
          if (pColData->cflag & COL_IS_KEY) {
×
4564
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4565
            goto _exit;
×
4566
          }
4567
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4568
            goto _exit;
×
4569
          }
4570
        } else {
4571
          if (varDataTLen(data + offset) > bytes) {
173!
4572
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4573
                   bytes);
4574
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4575
            goto _exit;
×
4576
          }
4577
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
173✔
4578
                                                                        varDataLen(data + offset));
173✔
4579
        }
4580
      }
4581
    } else {
4582
      for (int32_t i = 0; i < nRows; ++i) {
×
4583
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4584
        if (offset == -1) {
×
4585
          if (pColData->cflag & COL_IS_KEY) {
×
4586
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4587
            goto _exit;
×
4588
          }
4589
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4590
            goto _exit;
×
4591
          }
4592
        } else {
4593
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4594
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4595
                   (int)blobDataTLen(data + offset), bytes);
4596
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4597
            goto _exit;
×
4598
          }
4599
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)blobDataVal(data + offset),
×
4600
                                                                        blobDataLen(data + offset));
×
4601
        }
4602
      }
4603
    }
4604
  } else {  // fixed-length data type
4605
    bool allValue = true;
440✔
4606
    bool allNull = true;
440✔
4607
    for (int32_t i = 0; i < nRows; ++i) {
1,254✔
4608
      if (!BMIsNull(lengthOrbitmap, i)) {
814✔
4609
        allNull = false;
601✔
4610
      } else {
4611
        allValue = false;
213✔
4612
      }
4613
    }
4614
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
440!
4615
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4616
      goto _exit;
×
4617
    }
4618

4619
    if (allValue) {
440✔
4620
      // optimize (todo)
4621
      for (int32_t i = 0; i < nRows; ++i) {
933✔
4622
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
587✔
4623
      }
4624
    } else if (allNull) {
94✔
4625
      // optimize (todo)
4626
      for (int32_t i = 0; i < nRows; ++i) {
249✔
4627
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
165✔
4628
        if (code) goto _exit;
165!
4629
      }
4630
    } else {
4631
      for (int32_t i = 0; i < nRows; ++i) {
72✔
4632
        if (BMIsNull(lengthOrbitmap, i)) {
62✔
4633
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
48✔
4634
          if (code) goto _exit;
48!
4635
        } else {
4636
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
14✔
4637
        }
4638
      }
4639
    }
4640
  }
4641

4642
_exit:
10✔
4643
  return code;
545✔
4644
}
4645
int32_t tColDataAddValueByDataBlockWithBlob(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows,
×
4646
                                            char *lengthOrbitmap, char *data, void *pBlobSet) {
4647
  int32_t code = 0;
×
4648
  if (data == NULL) {
×
4649
    if (pColData->cflag & COL_IS_KEY) {
×
4650
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4651
    } else {
4652
      for (int32_t i = 0; i < nRows; ++i) {
×
4653
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4654
      }
4655
    }
4656
    goto _exit;
×
4657
  }
4658

4659
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
×
4660
    if (!IS_STR_DATA_BLOB(type)) {
×
4661
      for (int32_t i = 0; i < nRows; ++i) {
×
4662
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4663
        if (offset == -1) {
×
4664
          if (pColData->cflag & COL_IS_KEY) {
×
4665
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4666
            goto _exit;
×
4667
          }
4668
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4669
            goto _exit;
×
4670
          }
4671
        } else {
4672
          if (varDataTLen(data + offset) > bytes) {
×
4673
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4674
                   bytes);
4675
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4676
            goto _exit;
×
4677
          }
4678
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
×
4679
                                                                        varDataLen(data + offset));
×
4680
        }
4681
      }
4682
    } else {
4683
      for (int32_t i = 0; i < nRows; ++i) {
×
4684
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4685
        if (offset == -1) {
×
4686
          if (pColData->cflag & COL_IS_KEY) {
×
4687
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4688
            goto _exit;
×
4689
          }
4690
          if ((code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NULL](pBlobSet, pColData, NULL, 0))) {
×
4691
            goto _exit;
×
4692
          }
4693
        } else {
4694
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4695
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4696
                   (int)blobDataTLen(data + offset), bytes);
4697
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4698
            goto _exit;
×
4699
          }
4700
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_VALUE](
×
4701
              pBlobSet, pColData, (uint8_t *)blobDataVal(data + offset), blobDataLen(data + offset));
×
4702
        }
4703
      }
4704
    }
4705
  } else {  // fixed-length data type
4706
    bool allValue = true;
×
4707
    bool allNull = true;
×
4708
    for (int32_t i = 0; i < nRows; ++i) {
×
4709
      if (!BMIsNull(lengthOrbitmap, i)) {
×
4710
        allNull = false;
×
4711
      } else {
4712
        allValue = false;
×
4713
      }
4714
    }
4715
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
×
4716
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4717
      goto _exit;
×
4718
    }
4719

4720
    if (allValue) {
×
4721
      // optimize (todo)
4722
      for (int32_t i = 0; i < nRows; ++i) {
×
4723
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4724
      }
4725
    } else if (allNull) {
×
4726
      // optimize (todo)
4727
      for (int32_t i = 0; i < nRows; ++i) {
×
4728
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4729
        if (code) goto _exit;
×
4730
      }
4731
    } else {
4732
      for (int32_t i = 0; i < nRows; ++i) {
×
4733
        if (BMIsNull(lengthOrbitmap, i)) {
×
4734
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4735
          if (code) goto _exit;
×
4736
        } else {
4737
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4738
        }
4739
      }
4740
    }
4741
  }
4742

4743
_exit:
×
4744
  return code;
×
4745
}
4746

4747
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32_t buffMaxLen, initGeosFn igeos,
1,811,826✔
4748
                               checkWKBGeometryFn cgeos) {
4749
  int32_t code = 0;
1,811,826✔
4750

4751
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
1,811,826!
4752
    if (!(pColData->type == pBind->buffer_type)) {
1,811,480!
4753
      return TSDB_CODE_INVALID_PARA;
×
4754
    }
4755
  }
4756

4757
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
1,811,826!
4758
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
48,054✔
4759
      code = igeos();
3✔
4760
      if (code) {
3!
4761
        return code;
×
4762
      }
4763
    }
4764
    for (int32_t i = 0; i < pBind->num; ++i) {
106,460✔
4765
      if (pBind->is_null && pBind->is_null[i]) {
58,406✔
4766
        if (pColData->cflag & COL_IS_KEY) {
284!
4767
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4768
          goto _exit;
×
4769
        }
4770
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
284✔
4771
        if (code) goto _exit;
284!
4772
      } else if (pBind->length[i] > buffMaxLen) {
58,122!
4773
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4774
      } else {
4775
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
58,122✔
4776
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
5✔
4777
          if (code) {
5✔
4778
            uError("stmt col[%d] bind geometry wrong format", i);
1!
4779
            goto _exit;
1✔
4780
          }
4781
        }
4782
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
58,121✔
4783
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
58,121✔
4784
      }
4785
    }
4786
  } else {  // fixed-length data type
4787
    bool allValue;
4788
    bool allNull;
4789
    if (pBind->is_null) {
1,763,772✔
4790
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
20,405✔
4791
      allNull = (same && pBind->is_null[0] != 0);
20,405!
4792
      allValue = (same && pBind->is_null[0] == 0);
20,405!
4793
    } else {
4794
      allNull = false;
1,743,367✔
4795
      allValue = true;
1,743,367✔
4796
    }
4797

4798
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
1,763,772!
4799
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4800
      goto _exit;
×
4801
    }
4802

4803
    if (allValue) {
1,763,772✔
4804
      // optimize (todo)
4805
      for (int32_t i = 0; i < pBind->num; ++i) {
24,052,487✔
4806
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
22,455,251✔
4807
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
22,455,251✔
4808
      }
4809
    } else if (allNull) {
37,330✔
4810
      // optimize (todo)
4811
      for (int32_t i = 0; i < pBind->num; ++i) {
124✔
4812
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
62✔
4813
        if (code) goto _exit;
62!
4814
      }
4815
    } else {
4816
      for (int32_t i = 0; i < pBind->num; ++i) {
37,268!
4817
        if (pBind->is_null[i]) {
×
4818
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4819
          if (code) goto _exit;
×
4820
        } else {
4821
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
×
4822
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
×
4823
        }
4824
      }
4825
    }
4826
  }
4827

4828
_exit:
37,268✔
4829
  return code;
1,682,621✔
4830
}
4831

4832
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen) {
343✔
4833
  int32_t code = 0;
343✔
4834

4835
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
343!
4836
    if (!(pColData->type == pBind->buffer_type)) {
343!
4837
      return TSDB_CODE_INVALID_PARA;
×
4838
    }
4839
  }
4840

4841
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
472!
4842
    uint8_t *buf = pBind->buffer;
129✔
4843
    for (int32_t i = 0; i < pBind->num; ++i) {
501✔
4844
      if (pBind->is_null && pBind->is_null[i]) {
372✔
4845
        if (pColData->cflag & COL_IS_KEY) {
1!
4846
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4847
          goto _exit;
×
4848
        }
4849
        if (pBind->is_null[i] == 1) {
1!
4850
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1✔
4851
          if (code) goto _exit;
1!
4852
        } else {
4853
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4854
          if (code) goto _exit;
×
4855
        }
4856
      } else if (pBind->length[i] > buffMaxLen) {
371!
4857
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4858
      } else {
4859
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
371✔
4860
        buf += pBind->length[i];
371✔
4861
      }
4862
    }
4863
  } else {  // fixed-length data type
4864
    bool allValue;
4865
    bool allNull;
4866
    bool allNone;
4867
    if (pBind->is_null) {
214✔
4868
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
1✔
4869
      allNull = (same && pBind->is_null[0] == 1);
1!
4870
      allNone = (same && pBind->is_null[0] > 1);
1!
4871
      allValue = (same && pBind->is_null[0] == 0);
1!
4872
    } else {
4873
      allNull = false;
213✔
4874
      allNone = false;
213✔
4875
      allValue = true;
213✔
4876
    }
4877

4878
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
214!
4879
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4880
      goto _exit;
×
4881
    }
4882

4883
    uint8_t *buf = pBind->buffer;
214✔
4884

4885
    if (allValue) {
214!
4886
      // optimize (todo)
4887
      for (int32_t i = 0; i < pBind->num; ++i) {
665✔
4888
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
451✔
4889
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
451!
4890
          *val = 1;
1✔
4891
        }
4892
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
451✔
4893
      }
4894
    } else if (allNull) {
×
4895
      // optimize (todo)
4896
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4897
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4898
        if (code) goto _exit;
×
4899
      }
4900
    } else if (allNone) {
×
4901
      // optimize (todo)
4902
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4903
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4904
        if (code) goto _exit;
×
4905
      }
4906
    } else {
4907
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4908
        if (pBind->is_null[i]) {
×
4909
          if (pBind->is_null[i] == 1) {
×
4910
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4911
            if (code) goto _exit;
×
4912
          } else {
4913
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4914
            if (code) goto _exit;
×
4915
          }
4916
        } else {
4917
          uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
×
4918
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
×
4919
            *val = 1;
×
4920
          }
4921

4922
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
×
4923
        }
4924
      }
4925
    }
4926
  }
4927

4928
_exit:
×
4929
  return code;
343✔
4930
}
4931

4932
int32_t tColDataAddValueByBind2WithGeos(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
15✔
4933
                                        initGeosFn igeos, checkWKBGeometryFn cgeos) {
4934
  int32_t code = 0;
15✔
4935

4936
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
15!
4937
    if (!(pColData->type == pBind->buffer_type)) {
15!
4938
      return TSDB_CODE_INVALID_PARA;
×
4939
    }
4940
  }
4941

4942
  if (pColData->type != TSDB_DATA_TYPE_GEOMETRY) {
15!
4943
    return TSDB_CODE_INVALID_OPTION;
×
4944
  }
4945

4946
  code = igeos();
15✔
4947
  if (code) {
15!
4948
    return code;
×
4949
  }
4950

4951
  uint8_t *buf = pBind->buffer;
15✔
4952
  for (int32_t i = 0; i < pBind->num; ++i) {
36✔
4953
    if (pBind->is_null && pBind->is_null[i]) {
21✔
4954
      if (pColData->cflag & COL_IS_KEY) {
2!
4955
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4956
        goto _exit;
×
4957
      }
4958
      if (pBind->is_null[i] == 1) {
2!
4959
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2✔
4960
        if (code) goto _exit;
2!
4961
      } else {
4962
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4963
        if (code) goto _exit;
×
4964
      }
4965
    } else if (pBind->length[i] > buffMaxLen) {
19!
4966
      return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4967
    } else {
4968
      code = cgeos(buf, pBind->length[i]);
19✔
4969
      if (code) {
19!
4970
        uError("stmt2 col[%d] bind geometry with wrong format", i);
×
4971
        goto _exit;
×
4972
      }
4973
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
19✔
4974
      buf += pBind->length[i];
19✔
4975
    }
4976
  }
4977

4978
_exit:
15✔
4979
  return code;
15✔
4980
}
4981

4982
int32_t tColDataAddValueByBind2WithBlob(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
×
4983
                                        SBlobSet *pBlobSet) {
4984
  int32_t code = 0;
×
4985

4986
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
×
4987
    if (!(pColData->type == pBind->buffer_type)) {
×
4988
      return TSDB_CODE_INVALID_PARA;
×
4989
    }
4990
  }
4991

4992
  if (IS_STR_DATA_BLOB(pColData->type)) {  // var-length data type
×
4993
    uint8_t *buf = pBind->buffer;
×
4994
    for (int32_t i = 0; i < pBind->num; ++i) {
×
4995
      if (pBind->is_null && pBind->is_null[i]) {
×
4996
        if (pColData->cflag & COL_IS_KEY) {
×
4997
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4998
          goto _exit;
×
4999
        }
5000
        if (pBind->is_null[i] == 1) {
×
5001
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NULL](pBlobSet, pColData, NULL, 0);
×
5002
          if (code) goto _exit;
×
5003
        } else {
5004
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NONE](pBlobSet, pColData, NULL, 0);
×
5005
          if (code) goto _exit;
×
5006
        }
5007
      } else if (pBind->length[i] > buffMaxLen) {
×
5008
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5009
      } else {
5010
        code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_VALUE](pBlobSet, pColData, buf, pBind->length[i]);
×
5011
        buf += pBind->length[i];
×
5012
      }
5013
    }
5014
  }
5015
_exit:
×
5016
  return code;
×
5017
}
5018

5019
int32_t tColDataAddValueByBind2WithDecimal(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
8✔
5020
                                           uint8_t precision, uint8_t scale) {
5021
  int32_t code = 0;
8✔
5022

5023
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
8!
5024
    if (!(pColData->type == pBind->buffer_type)) {
6!
5025
      return TSDB_CODE_INVALID_PARA;
×
5026
    }
5027
  }
5028

5029
  if (!IS_DECIMAL_TYPE(pColData->type)) {
8!
5030
    return TSDB_CODE_INVALID_OPTION;
×
5031
  }
5032

5033
  uint8_t *buf = pBind->buffer;
8✔
5034
  for (int32_t i = 0; i < pBind->num; ++i) {
22✔
5035
    if (pBind->is_null && pBind->is_null[i]) {
14!
5036
      if (pColData->cflag & COL_IS_KEY) {
2!
5037
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5038
        goto _exit;
×
5039
      }
5040
      if (pBind->is_null[i] == 1) {
2!
5041
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
5042
        if (code) goto _exit;
×
5043
      } else {
5044
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
2✔
5045
        if (code) goto _exit;
2!
5046
      }
5047
    } else {
5048
      if (pColData->type == TSDB_DATA_TYPE_DECIMAL64) {
12✔
5049
        Decimal64 dec = {0};
6✔
5050
        int32_t   code = decimal64FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
6✔
5051
        buf += pBind->length[i];
6✔
5052
        if (TSDB_CODE_SUCCESS != code) {
6!
5053
          return code;
×
5054
        }
5055
        int64_t tmp = DECIMAL64_GET_VALUE(&dec);
6✔
5056
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)&tmp,
6✔
5057
                                                                      TYPE_BYTES[pColData->type]);
6✔
5058
      } else if (pColData->type == TSDB_DATA_TYPE_DECIMAL) {
6!
5059
        Decimal128 dec = {0};
6✔
5060
        int32_t    code = decimal128FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
6✔
5061
        buf += pBind->length[i];
6✔
5062
        if (TSDB_CODE_SUCCESS != code) {
6!
5063
          return code;
×
5064
        }
5065
        uint8_t *pV = taosMemCalloc(1, sizeof(Decimal128));
6✔
5066
        if (!pV) return terrno;
6!
5067
        memcpy(pV, &dec, DECIMAL_WORD_NUM(Decimal128) * sizeof(DecimalWord));
6✔
5068
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pV, TYPE_BYTES[pColData->type]);
6✔
5069
      }
5070
    }
5071
  }
5072

5073
_exit:
8✔
5074
  return code;
8✔
5075
}
5076
/* build rows to `rowArray` from bind
5077
 * `infos` is the bind information array
5078
 * `numOfInfos` is the number of bind information
5079
 * `infoSorted` is whether the bind information is sorted by column id
5080
 * `pTSchema` is the schema of the table
5081
 * `rowArray` is the array to store the rows
5082
 * `pOrdered` is the pointer to store ordered
5083
 * `pDupTs` is the pointer to store duplicateTs
5084
 */
5085
int32_t tRowBuildFromBind2(SBindInfo2 *infos, int32_t numOfInfos, SSHashObj *parsedCols, bool infoSorted,
7,511✔
5086
                           const STSchema *pTSchema, const SSchemaExt *pSchemaExt, SArray *rowArray, bool *pOrdered,
5087
                           bool *pDupTs) {
5088
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
7,511!
5089
    return TSDB_CODE_INVALID_PARA;
×
5090
  }
5091
  int8_t hasBlob = schemaHasBlob(pTSchema);
7,518✔
5092
  if (!infoSorted) {
7,514✔
5093
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
7✔
5094
  }
5095

5096
  int32_t code = 0;
7,514✔
5097
  int32_t numOfRows = infos[0].bind->num;
7,514✔
5098
  SArray *colValArray, *bufArray;
5099
  SColVal colVal;
5100
  int32_t numOfFixedValue = 0;
7,514✔
5101
  int32_t lino = 0;
7,514✔
5102

5103
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
7,514!
5104
    return terrno;
×
5105
  }
5106
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
7,509!
5107
    taosArrayDestroy(colValArray);
×
5108
    return terrno;
×
5109
  }
5110
  for (int i = 0; i < numOfInfos; ++i) {
117,621✔
5111
    if (parsedCols) {
110,132✔
5112
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
132✔
5113
      if (pParsedVal) {
132✔
5114
        continue;
24✔
5115
      }
5116
    }
5117
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
220,190!
5118
      taosArrayDestroy(colValArray);
×
5119
      taosArrayDestroy(bufArray);
×
5120
      return terrno;
×
5121
    }
5122
  }
5123

5124
  SRowKey rowKey, lastRowKey;
5125
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
255,350✔
5126
    taosArrayClear(colValArray);
249,825✔
5127

5128
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
1,397,685✔
5129
      if (parsedCols) {
1,123,365✔
5130
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
132✔
5131
        if (pParsedVal) {
132!
5132
          numOfFixedValue++;
×
5133
          colVal = *pParsedVal;
×
5134

5135
          if (taosArrayPush(colValArray, &colVal) == NULL) {
24!
5136
            if (IS_VAR_DATA_TYPE(pParsedVal->value.type)) {
×
5137
              taosMemoryFree(colVal.value.pData);
×
5138
            }
5139
            code = terrno;
×
5140
            TAOS_CHECK_GOTO(code, &lino, _exit);
2!
5141
          }
5142
          continue;
24✔
5143
        }
5144
      }
5145

5146
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
1,128,030✔
5147
        if (infos[iInfo].bind->is_null[iRow] == 1) {
5✔
5148
          if (iInfo == 0) {
3✔
5149
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1✔
5150
            TAOS_CHECK_GOTO(code, &lino, _exit);
1!
5151
          }
5152
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
2✔
5153
        } else {
5154
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
2✔
5155
        }
5156
      } else {
5157
        SValue value = {
1,128,025✔
5158
            .type = infos[iInfo].type,
1,128,025✔
5159
        };
5160
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
1,128,025!
5161
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
85,894!
5162
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5163
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5164
            value.nData = length;
×
5165
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5166
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5167
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5168
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5169
              goto _exit;
1✔
5170
            }
5171
            value.pData = *data;
×
5172
            *data += length;
×
5173
          } else {
5174
            int32_t   length = infos[iInfo].bind->length[iRow];
85,894✔
5175
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
85,894✔
5176
            value.nData = length;
85,894✔
5177
            if (value.nData > infos[iInfo].bytes - VARSTR_HEADER_SIZE) {
85,894!
5178
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5179
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5180
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5181
              goto _exit;
×
5182
            }
5183
            value.pData = *data;
85,894✔
5184
            *data += length;
85,894✔
5185
          }
5186
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5187
        } else {
5188
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
1,042,131✔
5189
            if (!pSchemaExt) {
4!
5190
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5191
              code = TSDB_CODE_PAR_INTERNAL_ERROR;
×
5192
              goto _exit;
×
5193
            }
5194
            uint8_t precision = 0, scale = 0;
4✔
5195
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
4✔
5196
            Decimal128 dec = {0};
4✔
5197
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
4✔
5198
            int32_t    length = infos[iInfo].bind->length[iRow];
4✔
5199
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
4✔
5200
            *data += length;
4✔
5201
            TAOS_CHECK_GOTO(code, &lino, _exit);
4!
5202

5203
            // precision check
5204
            // scale auto fit
5205

5206
            code = decimal128ToDataVal(&dec, &value);
4✔
5207
            TAOS_CHECK_GOTO(code, &lino, _exit);
4!
5208

5209
          } else if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL64) {
1,042,127✔
5210
            if (!pSchemaExt) {
5!
5211
              uError("stmt2 decimal128 type without ext schema info, cannot parse decimal values");
×
5212
              code = TSDB_CODE_PAR_INTERNAL_ERROR;
×
5213
              goto _exit;
1✔
5214
            }
5215
            uint8_t precision = 0, scale = 0;
5✔
5216
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
5✔
5217
            Decimal64 dec = {0};
5✔
5218
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
5✔
5219
            int32_t   length = infos[iInfo].bind->length[iRow];
5✔
5220
            code = decimal64FromStr(*(char **)data, length, precision, scale, &dec);
5✔
5221
            *data += length;
5✔
5222
            TAOS_CHECK_GOTO(code, &lino, _exit);
5✔
5223

5224
            code = decimal64ToDataVal(&dec, &value);
4✔
5225
            TAOS_CHECK_GOTO(code, &lino, _exit);
4!
5226

5227
          } else {
5228
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
1,042,122✔
5229
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
1,042,122✔
5230
              *val = 1;
1✔
5231
            }
5232
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
1,042,122✔
5233
          }
5234
        }
5235
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
1,133,641✔
5236
      }
5237
      if (taosArrayPush(colValArray, &colVal) == NULL) {
1,147,912!
5238
        code = terrno;
×
5239
        goto _exit;
×
5240
      }
5241
    }
5242

5243
    SRow *row;
5244

5245
    if (hasBlob == 0) {
274,320✔
5246
      SRowBuildScanInfo sinfo = {0};
248,903✔
5247
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
248,903✔
5248
      TAOS_CHECK_GOTO(code, &lino, _exit);
248,272!
5249
    } else {
5250
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
25,417✔
5251
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
25,417✔
5252
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5253
    }
5254

5255
    if ((taosArrayPush(rowArray, &row)) == NULL) {
247,842!
5256
      code = terrno;
×
5257
      goto _exit;
×
5258
    }
5259

5260
    if (pOrdered && pDupTs) {
247,842!
5261
      tRowGetKey(row, &rowKey);
496,222✔
5262
      if (iRow == 0) {
248,130✔
5263
        *pOrdered = true;
7,512✔
5264
        *pDupTs = false;
7,512✔
5265
      } else {
5266
        if (*pOrdered) {
240,618!
5267
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
240,693✔
5268
          *pOrdered = (res >= 0);
240,693✔
5269
          if (!*pDupTs) {
240,693✔
5270
            *pDupTs = (res == 0);
240,419✔
5271
          }
5272
        }
5273
      }
5274
      lastRowKey = rowKey;
248,130✔
5275
    }
5276
  }
5277
_exit:
5,525✔
5278
  if (code != 0) {
5,527✔
5279
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
2!
5280
  }
5281
  taosArrayDestroy(colValArray);
5,527✔
5282
  taosArrayDestroy(bufArray);
7,514✔
5283
  return code;
7,524✔
5284
}
5285
/* build rows to `rowArray` from bind
5286
 * `infos` is the bind information array
5287
 * `numOfInfos` is the number of bind information
5288
 * `infoSorted` is whether the bind information is sorted by column id
5289
 * `pTSchema` is the schema of the table
5290
 * `rowArray` is the array to store the rows
5291
 * `pOrdered` is the pointer to store ordered
5292
 * `pDupTs` is the pointer to store duplicateTs
5293
 */
5294
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
×
5295
                                   SArray *rowArray, bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5296
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
×
5297
    return TSDB_CODE_INVALID_PARA;
×
5298
  }
5299
  int8_t hasBlob = schemaHasBlob(pTSchema);
×
5300
  if (!infoSorted) {
×
5301
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5302
  }
5303

5304
  int32_t code = 0;
×
5305
  int32_t numOfRows = infos[0].bind->num;
×
5306
  SArray *colValArray, *bufArray;
5307
  SColVal colVal;
5308

5309
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
×
5310
    return terrno;
×
5311
  }
5312
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
×
5313
    taosArrayDestroy(colValArray);
×
5314
    return terrno;
×
5315
  }
5316
  for (int i = 0; i < numOfInfos; ++i) {
×
5317
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
×
5318
      taosArrayDestroy(colValArray);
×
5319
      taosArrayDestroy(bufArray);
×
5320
      return terrno;
×
5321
    }
5322
  }
5323

5324
  SRowKey rowKey, lastRowKey;
5325
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
×
5326
    taosArrayClear(colValArray);
×
5327

5328
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
×
5329
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
×
5330
        if (infos[iInfo].bind->is_null[iRow] == 1) {
×
5331
          if (iInfo == 0) {
×
5332
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5333
            goto _exit;
×
5334
          }
5335
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
×
5336
        } else {
5337
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5338
        }
5339
      } else {
5340
        SValue value = {
×
5341
            .type = infos[iInfo].type,
×
5342
        };
5343
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
×
5344
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
×
5345
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5346
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
×
5347
            value.nData = length;
×
5348
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5349
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5350
              uError("stmt bind param[%d] length:%d  greater than type maximum lenght: %d", iInfo, value.nData,
×
5351
                     pTSchema->columns[infos[iInfo].columnId - 1].bytes);
5352
              goto _exit;
×
5353
            }
5354
            value.pData = *data;
×
5355
            *data += length;
×
5356
          } else {
5357
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5358
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
×
5359
            value.nData = length;
×
5360
            if (value.nData > pTSchema->columns[infos[iInfo].columnId - 1].bytes - VARSTR_HEADER_SIZE) {
×
5361
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5362
              uError("stmt bind param[%d] length:%d  greater than type maximum lenght: %d", iInfo, value.nData,
×
5363
                     pTSchema->columns[infos[iInfo].columnId - 1].bytes);
5364
              goto _exit;
×
5365
            }
5366
            value.pData = *data;
×
5367
            *data += length;
×
5368
          }
5369

5370
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5371
        } else {
5372
          uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
×
5373
          if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
×
5374
            *val = 1;
×
5375
          }
5376
          valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
×
5377
        }
5378
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
×
5379
      }
5380
      if (taosArrayPush(colValArray, &colVal) == NULL) {
×
5381
        code = terrno;
×
5382
        goto _exit;
×
5383
      }
5384
    }
5385

5386
    SRow *row;
5387

5388
    if (hasBlob == 0) {
×
5389
      SRowBuildScanInfo sinfo = {0};
×
5390
      if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
×
5391
        goto _exit;
×
5392
      }
5393
    } else {
5394
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
×
5395
      if ((code = tRowBuildWithBlob(colValArray, pTSchema, &row, pBlobSet, &sinfo))) {
×
5396
        goto _exit;
×
5397
      }
5398
    }
5399

5400
    if ((taosArrayPush(rowArray, &row)) == NULL) {
×
5401
      code = terrno;
×
5402
      goto _exit;
×
5403
    }
5404

5405
    if (pOrdered && pDupTs) {
×
5406
      tRowGetKey(row, &rowKey);
×
5407
      if (iRow == 0) {
×
5408
        *pOrdered = true;
×
5409
        *pDupTs = false;
×
5410
      } else {
5411
        if (*pOrdered) {
×
5412
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
×
5413
          *pOrdered = (res >= 0);
×
5414
          if (!*pDupTs) {
×
5415
            *pDupTs = (res == 0);
×
5416
          }
5417
        }
5418
      }
5419
      lastRowKey = rowKey;
×
5420
    }
5421
  }
5422
_exit:
×
5423
  taosArrayDestroy(colValArray);
×
5424
  taosArrayDestroy(bufArray);
×
5425
  return code;
×
5426
}
5427

5428
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
374✔
5429
  int32_t code = TSDB_CODE_SUCCESS;
374✔
5430

5431
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
374!
5432
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
146✔
5433
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
51✔
5434
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
73✔
5435
    if (iToRow == 0) {
73✔
5436
      pToColData->aOffset[iToRow] = 0;
14✔
5437
    }
5438

5439
    if (iToRow < pToColData->nVal - 1) {
73✔
5440
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
64✔
5441
    }
5442

5443
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
73✔
5444
                 nData);
5445
  } else {
5446
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
301✔
5447
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
301✔
5448
  }
5449
  return code;
374✔
5450
}
5451

5452
static int32_t tColDataCopyRowSingleCol(SColData *pFromColData, int32_t iFromRow, SColData *pToColData,
382✔
5453
                                        int32_t iToRow) {
5454
  int32_t code = TSDB_CODE_SUCCESS;
382✔
5455
  int     bit_val = 0;
382✔
5456

5457
  switch (pFromColData->flag) {
382!
5458
    case HAS_NONE: {
×
5459
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5460
    } break;
×
5461
    case HAS_NULL: {
8✔
5462
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
8!
5463
    } break;
8✔
5464
    case (HAS_NULL | HAS_NONE): {
×
5465
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5466
      if (0 == bit_val)
×
5467
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5468
      else
5469
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
×
5470
    } break;
×
5471
    case HAS_VALUE: {
328✔
5472
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
328!
5473
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
328!
5474
    } break;
328✔
5475
    case (HAS_VALUE | HAS_NONE): {
×
5476
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5477
      if (0 == bit_val)
×
5478
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5479
      else
5480
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
×
5481
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5482
    } break;
×
5483
    case (HAS_VALUE | HAS_NULL): {
46✔
5484
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
46✔
5485
      if (0 == bit_val)
46✔
5486
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
28!
5487
      else
5488
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
18!
5489
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
46!
5490
    } break;
46✔
5491
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
5492
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
5493
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5494
    } break;
×
5495
    default:
×
5496
      return -1;
×
5497
  }
5498

5499
  return code;
382✔
5500
}
5501

5502
static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
89✔
5503
                               int32_t nColData) {
5504
  int32_t code = TSDB_CODE_SUCCESS;
89✔
5505

5506
  for (int32_t i = 0; i < nColData; i++) {
471✔
5507
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
382✔
5508
    if (code != TSDB_CODE_SUCCESS) {
382!
5509
      return code;
×
5510
    }
5511
  }
5512

5513
  return code;
89✔
5514
}
5515

5516
static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
89✔
5517
  int32_t code = TSDB_CODE_SUCCESS;
89✔
5518

5519
  for (int32_t i = 0; i < nColData; i++) {
471✔
5520
    SColVal cv = {0};
382✔
5521
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
382✔
5522
    if (code != TSDB_CODE_SUCCESS) {
382!
5523
      return code;
×
5524
    }
5525
    code = tColDataAppendValue(&aToColData[i], &cv);
382✔
5526
    if (code != TSDB_CODE_SUCCESS) {
382!
5527
      return code;
×
5528
    }
5529
  }
5530

5531
  return code;
89✔
5532
}
5533

5534
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
10,198,180✔
5535
  SColVal cv;
5536

5537
  key->ts = ((TSKEY *)aColData[0].pData)[iRow];
10,198,180✔
5538
  key->numOfPKs = 0;
10,198,180✔
5539

5540
  for (int i = 1; i < nColData; i++) {
10,198,211!
5541
    if (aColData[i].cflag & COL_IS_KEY) {
10,208,746✔
5542
      tColDataGetValue4(&aColData[i], iRow, &cv);
31✔
5543
      key->pks[key->numOfPKs++] = cv.value;
31✔
5544
    } else {
5545
      break;
10,208,715✔
5546
    }
5547
  }
5548
}
10,198,180✔
5549

5550
static int32_t tColDataMergeSortMerge(SColData *aColData, int32_t start, int32_t mid, int32_t end, int32_t nColData) {
27✔
5551
  SColData *aDstColData = NULL;
27✔
5552
  int32_t   i = start, j = mid + 1, k = 0;
27✔
5553
  SRowKey   keyi, keyj;
5554

5555
  if (end > start) {
27!
5556
    aDstColData = taosMemoryCalloc(1, sizeof(SColData) * nColData);
27!
5557
    if (aDstColData == NULL) {
27!
5558
      return terrno;
×
5559
    }
5560
    for (int c = 0; c < nColData; ++c) {
141✔
5561
      tColDataInit(&aDstColData[c], aColData[c].cid, aColData[c].type, aColData[c].cflag);
114✔
5562
    }
5563
  }
5564

5565
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
27✔
5566
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
27✔
5567
  while (i <= mid && j <= end) {
76✔
5568
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
49✔
5569
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
37!
5570
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
37✔
5571
    } else {
5572
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
12!
5573
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
12✔
5574
    }
5575
  }
5576

5577
  while (i <= mid) {
40✔
5578
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
13!
5579
  }
5580

5581
  while (j <= end) {
54✔
5582
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
27!
5583
  }
5584

5585
  for (i = start, k = 0; i <= end; ++i, ++k) {
116✔
5586
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
89!
5587
  }
5588

5589
  if (aDstColData) {
27!
5590
    for (int32_t i = 0; i < nColData; i++) {
141✔
5591
      tColDataDestroy(&aDstColData[i]);
114✔
5592
    }
5593
    taosMemoryFree(aDstColData);
27!
5594
  }
5595

5596
  return TSDB_CODE_SUCCESS;
27✔
5597
}
5598

5599
static int32_t tColDataMergeSort(SColData *aColData, int32_t start, int32_t end, int32_t nColData) {
60✔
5600
  int32_t ret = TSDB_CODE_SUCCESS;
60✔
5601
  int32_t mid;
5602

5603
  if (start >= end) {
60✔
5604
    return TSDB_CODE_SUCCESS;
33✔
5605
  }
5606

5607
  mid = (start + end) / 2;
27✔
5608

5609
  ret = tColDataMergeSort(aColData, start, mid, nColData);
27✔
5610
  if (ret != TSDB_CODE_SUCCESS) {
27!
5611
    return ret;
×
5612
  }
5613

5614
  ret = tColDataMergeSort(aColData, mid + 1, end, nColData);
27✔
5615
  if (ret != TSDB_CODE_SUCCESS) {
27!
5616
    return ret;
×
5617
  }
5618

5619
  return tColDataMergeSortMerge(aColData, start, mid, end, nColData);
27✔
5620
}
5621

5622
static int32_t tColDataSort(SColData *aColData, int32_t nColData) {
6✔
5623
  int32_t nVal = aColData[0].nVal;
6✔
5624

5625
  if (nVal < 2) return TSDB_CODE_SUCCESS;
6!
5626

5627
  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
6✔
5628
}
5629

5630
static int32_t tColDataMerge(SArray **colArr) {
4✔
5631
  int32_t code = 0;
4✔
5632
  SArray *src = *colArr;
4✔
5633
  SArray *dst = NULL;
4✔
5634

5635
  dst = taosArrayInit(taosArrayGetSize(src), sizeof(SColData));
4✔
5636
  if (dst == NULL) {
4!
5637
    return terrno;
×
5638
  }
5639

5640
  for (int32_t i = 0; i < taosArrayGetSize(src); i++) {
15✔
5641
    SColData *srcCol = taosArrayGet(src, i);
11✔
5642

5643
    SColData *dstCol = taosArrayReserve(dst, 1);
11✔
5644
    if (dstCol == NULL) {
11!
5645
      code = terrno;
×
5646
      goto _exit;
×
5647
    }
5648
    tColDataInit(dstCol, srcCol->cid, srcCol->type, srcCol->cflag);
11✔
5649
  }
5650

5651
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
4✔
5652
  SRowKey lastKey;
5653
  for (int32_t i = 0; i < numRows; i++) {
16✔
5654
    SRowKey key;
5655
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
12✔
5656

5657
    if (i == 0 || tRowKeyCompare(&key, &lastKey) != 0) {  // append new row
20✔
5658
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
18✔
5659
        SColData *srcCol = taosArrayGet(src, j);
13✔
5660
        SColData *dstCol = taosArrayGet(dst, j);
13✔
5661

5662
        SColVal cv;
5663
        code = tColDataGetValue(srcCol, i, &cv);
13✔
5664
        if (code != TSDB_CODE_SUCCESS) {
13!
5665
          goto _exit;
×
5666
        }
5667
        code = tColDataAppendValue(dstCol, &cv);
13✔
5668
        if (code) {
13!
5669
          goto _exit;
×
5670
        }
5671
      }
5672
      lastKey = key;
5✔
5673
    } else {  // update existing row
5674
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
24✔
5675
        SColData *srcCol = taosArrayGet(src, j);
17✔
5676
        SColData *dstCol = taosArrayGet(dst, j);
17✔
5677

5678
        SColVal cv;
5679
        code = tColDataGetValue(srcCol, i, &cv);
17✔
5680
        if (code != TSDB_CODE_SUCCESS) {
17!
5681
          goto _exit;
×
5682
        }
5683
        code = tColDataUpdateValue(dstCol, &cv, true);
17✔
5684
        if (code) {
17!
5685
          goto _exit;
×
5686
        }
5687
      }
5688
    }
5689
  }
5690

5691
_exit:
4✔
5692
  if (code) {
4!
5693
    taosArrayDestroyEx(dst, tColDataDestroy);
×
5694
  } else {
5695
    taosArrayDestroyEx(src, tColDataDestroy);
4✔
5696
    *colArr = dst;
4✔
5697
  }
5698
  return code;
4✔
5699
}
5700

5701
int32_t tColDataSortMerge(SArray **arr) {
12,192✔
5702
  SArray   *colDataArr = *arr;
12,192✔
5703
  int32_t   nColData = TARRAY_SIZE(colDataArr);
12,192✔
5704
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
12,192✔
5705

5706
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
12,192!
5707
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5708
  }
5709
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
12,192!
5710
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5711
  }
5712
  if (!(aColData[0].flag == HAS_VALUE)) {
12,192!
5713
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5714
  }
5715

5716
  if (aColData[0].nVal <= 1) goto _exit;
12,192✔
5717

5718
  int8_t doSort = 0;
12,055✔
5719
  int8_t doMerge = 0;
12,055✔
5720
  // scan -------
5721
  SRowKey lastKey;
5722
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
12,055✔
5723
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
10,110,068!
5724
    SRowKey key;
5725
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
10,143,115✔
5726

5727
    int32_t c = tRowKeyCompare(&lastKey, &key);
10,098,000✔
5728
    if (c < 0) {
10,098,000!
5729
      lastKey = key;
10,098,043✔
5730
      continue;
10,098,043✔
5731
    } else if (c > 0) {
×
5732
      doSort = 1;
6✔
5733
      break;
6✔
5734
    } else {
5735
      doMerge = 1;
×
5736
    }
5737
  }
5738

5739
  // sort -------
5740
  if (doSort) {
×
5741
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
6!
5742
  }
5743

5744
  if ((doMerge != 1) && (doSort == 1)) {
12,085!
5745
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
6✔
5746
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
28✔
5747
      SRowKey key;
5748
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
23✔
5749

5750
      int32_t c = tRowKeyCompare(&lastKey, &key);
23✔
5751
      if (c == 0) {
23✔
5752
        doMerge = 1;
1✔
5753
        break;
1✔
5754
      }
5755
      lastKey = key;
22✔
5756
    }
5757
  }
5758

5759
  // merge -------
5760
  if (doMerge) {
12,085✔
5761
    int32_t code = tColDataMerge(arr);
4✔
5762
    if (code) return code;
4!
5763
  }
5764

5765
_exit:
12,085✔
5766
  return 0;
12,222✔
5767
}
5768

5769
int32_t tColDataSortMergeWithBlob(SArray **arr, SBlobSet *pBlob) {
×
5770
  SArray   *colDataArr = *arr;
×
5771
  int32_t   nColData = TARRAY_SIZE(colDataArr);
×
5772
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
×
5773

5774
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
×
5775
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5776
  }
5777
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
×
5778
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5779
  }
5780
  if (!(aColData[0].flag == HAS_VALUE)) {
×
5781
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5782
  }
5783

5784
  if (aColData[0].nVal <= 1) goto _exit;
×
5785

5786
  int8_t doSort = 0;
×
5787
  int8_t doMerge = 0;
×
5788
  // scan -------
5789
  SRowKey lastKey;
5790
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5791
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5792
    SRowKey key;
5793
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5794

5795
    int32_t c = tRowKeyCompare(&lastKey, &key);
×
5796
    if (c < 0) {
×
5797
      lastKey = key;
×
5798
      continue;
×
5799
    } else if (c > 0) {
×
5800
      doSort = 1;
×
5801
      break;
×
5802
    } else {
5803
      doMerge = 1;
×
5804
    }
5805
  }
5806
  if (doMerge || doSort) {
×
5807
    return TSDB_CODE_BLOB_NOT_SUPPORT;
×
5808
  }
5809
  // sort -------
5810
  if (doSort) {
×
5811
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
×
5812
  }
5813

5814
  if ((doMerge != 1) && (doSort == 1)) {
×
5815
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5816
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5817
      SRowKey key;
5818
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5819

5820
      int32_t c = tRowKeyCompare(&lastKey, &key);
×
5821
      if (c == 0) {
×
5822
        doMerge = 1;
×
5823
        break;
×
5824
      }
5825
      lastKey = key;
×
5826
    }
5827
  }
5828

5829
  // merge -------
5830
  if (doMerge) {
×
5831
    int32_t code = tColDataMerge(arr);
×
5832
    if (code) return code;
×
5833
  }
5834

5835
_exit:
×
5836
  return 0;
×
5837
}
5838

5839
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
89,293✔
5840
  int32_t code = 0;
89,293✔
5841

5842
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
178,586!
5843
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
178,586!
5844
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
178,586!
5845
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
178,586!
5846

5847
  // bitmap
5848
  switch (pColData->flag) {
89,293!
5849
    case (HAS_NULL | HAS_NONE):
116✔
5850
    case (HAS_VALUE | HAS_NONE):
5851
    case (HAS_VALUE | HAS_NULL):
5852
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
116✔
5853
      if (code) return code;
116!
5854
      break;
116✔
5855
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5856
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
5857
      if (code) return code;
×
5858
      break;
×
5859
    default:
89,177✔
5860
      break;
89,177✔
5861
  }
5862

5863
  // value
5864
  if (pColData->flag & HAS_VALUE) {
89,293✔
5865
    if (IS_VAR_DATA_TYPE(pColData->type)) {
88,986!
5866
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
1,822✔
5867
      if (code) return code;
1,822!
5868

5869
      code = tEncodeI32v(pEncoder, pColData->nData);
1,822✔
5870
      if (code) return code;
1,822!
5871

5872
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
1,822✔
5873
      if (code) return code;
1,822!
5874
    } else {
5875
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
87,164✔
5876
      if (code) return code;
87,164!
5877
    }
5878
  }
5879

5880
  return code;
89,293✔
5881
}
5882

5883
static int32_t tDecodeColDataVersion0(SDecoder *pDecoder, SColData *pColData) {
9,246✔
5884
  int32_t code = 0;
9,246✔
5885

5886
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
18,495!
5887
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
18,497!
5888
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
18,495!
5889
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
18,491!
5890

5891
  if (pColData->type <= 0 || pColData->type >= TSDB_DATA_TYPE_MAX || pColData->flag <= 0 || pColData->flag >= 8) {
9,244!
5892
    return TSDB_CODE_INVALID_PARA;
×
5893
  }
5894

5895
  // bitmap
5896
  switch (pColData->flag) {
9,245!
5897
    case (HAS_NULL | HAS_NONE):
129✔
5898
    case (HAS_VALUE | HAS_NONE):
5899
    case (HAS_VALUE | HAS_NULL):
5900
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
129!
5901
      if (code) return code;
129!
5902
      break;
129✔
5903
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5904
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
5905
      if (code) return code;
×
5906
      break;
×
5907
    default:
9,116✔
5908
      break;
9,116✔
5909
  }
5910

5911
  // value
5912
  if (pColData->flag & HAS_VALUE) {
9,245✔
5913
    if (IS_VAR_DATA_TYPE(pColData->type)) {
9,070!
5914
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
884!
5915
      if (code) return code;
884!
5916

5917
      code = tDecodeI32v(pDecoder, &pColData->nData);
884✔
5918
      if (code) return code;
885!
5919

5920
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
885!
5921
      if (code) return code;
885!
5922
    } else {
5923
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
8,186✔
5924
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
8,186!
5925
      if (code) return code;
8,186!
5926
    }
5927
  }
5928
  pColData->cflag = 0;
9,246✔
5929

5930
  return code;
9,246✔
5931
}
5932

5933
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
89,295✔
5934
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
89,295✔
5935
  if (code) return code;
89,286!
5936
  return tEncodeI8(pEncoder, pColData->cflag);
178,572✔
5937
}
5938

5939
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
9,246✔
5940
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
9,246✔
5941
  if (code) return code;
9,249!
5942

5943
  code = tDecodeI8(pDecoder, &pColData->cflag);
9,249✔
5944
  return code;
9,249✔
5945
}
5946

5947
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
89,272✔
5948
  if (version == 0) {
89,272!
5949
    return tEncodeColDataVersion0(pEncoder, pColData);
×
5950
  } else if (version == 1) {
89,272!
5951
    return tEncodeColDataVersion1(pEncoder, pColData);
×
5952
  } else if (version == 2) {
89,272!
5953
    int32_t posStart = pEncoder->pos;
89,307✔
5954
    pEncoder->pos += INT_BYTES;
89,307✔
5955
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
89,307✔
5956
    if (code) return code;
89,322!
5957
    int32_t posEnd = pEncoder->pos;
89,322✔
5958
    int32_t pos = posEnd - posStart;
89,322✔
5959
    pEncoder->pos = posStart;
89,322✔
5960
    code = tEncodeI32(pEncoder, pos);
89,322✔
5961
    pEncoder->pos = posEnd;
89,322✔
5962
    return code;
89,322✔
5963
  } else {
5964
    return TSDB_CODE_INVALID_PARA;
×
5965
  }
5966
}
5967

5968
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
15,383✔
5969
  if (version == 0) {
15,383!
5970
    return tDecodeColDataVersion0(pDecoder, pColData);
×
5971
  } else if (version == 1) {
15,383!
5972
    return tDecodeColDataVersion1(pDecoder, pColData);
×
5973
  } else if (version == 2) {
15,383✔
5974
    if (jump) {
15,382✔
5975
      int32_t len = 0;
6,138✔
5976
      int32_t code = tDecodeI32(pDecoder, &len);
6,139✔
5977
      if (code) return code;
6,139!
5978
      pDecoder->pos += (len - INT_BYTES);
6,139✔
5979
      return TSDB_CODE_SUCCESS;
6,139✔
5980
    } else {
5981
      pDecoder->pos += INT_BYTES;
9,244✔
5982
      return tDecodeColDataVersion1(pDecoder, pColData);
9,244✔
5983
    }    
5984
  } else {
5985
    return TSDB_CODE_INVALID_PARA;
1✔
5986
  }
5987
}
5988

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

5991
int32_t tDecodeRow(SDecoder *pDecoder, SRow **ppRow) {
1,466,551,616✔
5992
  if (ppRow == NULL) {
1,466,551,616!
5993
    return TSDB_CODE_INVALID_PARA;
×
5994
  }
5995

5996
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
1,466,551,616!
5997
    return TSDB_CODE_OUT_OF_RANGE;
×
5998
  }
5999

6000
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
1,466,551,616✔
6001
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
2,147,483,647!
6002
}
6003

6004
int32_t tEncodeBlobSet(SEncoder *pEncoder, SBlobSet *pRow) {
×
6005
  int32_t code = 0;
×
6006
  int32_t lino = 0;
×
6007
  uint8_t compressed = 0;
×
6008
  int32_t compressSize = 0;
×
6009
  char   *p = NULL;
×
6010

6011
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pRow->type));
×
6012
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pRow->len));
×
6013

6014
  int32_t nSeq = taosArrayGetSize(pRow->pSeqTable);
×
6015
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, nSeq));
×
6016

6017
  // if (pRow->type) {
6018
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
×
6019
  while (pIter) {
×
6020
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
×
6021
    int32_t  idx = *(int32_t *)pIter;
×
6022
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
×
6023
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
×
6024

6025
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
×
6026
  }
6027
  //}
6028
  for (int32_t i = 0; i < nSeq; i++) {
×
6029
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
×
6030
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
×
6031
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
×
6032
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
×
6033
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
×
6034
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
×
6035
  }
6036

6037
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
×
6038

6039
_exit:
×
6040
  return code;
×
6041
}
6042

6043
int32_t tDecodeBlobSet(SDecoder *pDecoder, SBlobSet **pBlobSet) {
×
6044
  int32_t    code = 0;
×
6045
  int32_t    lino = 0;
×
6046
  int32_t    nSeq = 0;
×
6047
  uint8_t    compressd = 0;
×
6048
  int32_t    compressSize = 0;
×
6049
  SBlobSet  *pBlob = taosMemCalloc(1, sizeof(SBlobSet));
×
6050
  if (pBlob == NULL) {
×
6051
    TAOS_CHECK_EXIT(terrno);
×
6052
  }
6053
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pBlob->type));
×
6054
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pBlob->len));
×
6055

6056
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
×
6057

6058
  // if (pBlob->type) {
6059
  pBlob->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
×
6060
  if (pBlob->pSeqToffset == NULL) {
×
6061
    TAOS_CHECK_EXIT(terrno);
×
6062
  }
6063
  for (int32_t i = 0; i < nSeq; i++) {
×
6064
    uint64_t seq;
6065
    int32_t  idx;
6066
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &seq));
×
6067
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
×
6068

6069
    code = taosHashPut(pBlob->pSeqToffset, &seq, sizeof(seq), &idx, sizeof(idx));
×
6070
    TAOS_CHECK_EXIT(code);
×
6071
  }
6072
  //}
6073

6074
  pBlob->pSeqTable = taosArrayInit(nSeq, sizeof(SBlobValue));
×
6075
  if (pBlob->pSeqTable == NULL) {
×
6076
    TAOS_CHECK_EXIT(terrno);
×
6077
  }
6078

6079
  for (int32_t i = 0; i < nSeq; i++) {
×
6080
    SBlobValue value;
6081
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
×
6082
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
×
6083
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
×
6084
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
×
6085
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
×
6086
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
×
6087
      TAOS_CHECK_EXIT(terrno);
×
6088
    }
6089
  }
6090

6091
  pBlob->data = taosMemCalloc(1, pBlob->len);
×
6092
  if (pBlob->data == NULL) {
×
6093
    TAOS_CHECK_EXIT(terrno);
×
6094
  }
6095

6096
  TAOS_CHECK_EXIT(tDecodeFixed(pDecoder, pBlob->data, pBlob->len));
×
6097
  *pBlobSet = pBlob;
×
6098

6099
  uTrace("decode blob len:%d", (int32_t)(pBlob->len));
×
6100

6101
_exit:
×
6102
  if (code != 0) {
×
6103
    if (pBlob != NULL) {
×
6104
      taosMemoryFree(pBlob->data);
×
6105
      taosArrayDestroy(pBlob->pSeqTable);
×
6106
      taosMemoryFree(pBlob);
×
6107
    }
6108
  }
6109
  return code;
×
6110
}
6111

6112
#define CALC_SUM_MAX_MIN(SUM, MAX, MIN, VAL) \
6113
  do {                                       \
6114
    (SUM) += (VAL);                          \
6115
    if ((MAX) < (VAL)) (MAX) = (VAL);        \
6116
    if ((MIN) > (VAL)) (MIN) = (VAL);        \
6117
  } while (0)
6118

6119
static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, SColumnDataAgg *pAggs) {
9,104✔
6120
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,104✔
6121
  int16_t *numOfNull = &pAggs->numOfNull;
9,104✔
6122
  *sum = 0;
9,104✔
6123
  *max = 0;
9,104✔
6124
  *min = 1;
9,104✔
6125
  *numOfNull = 0;
9,104✔
6126

6127
  int8_t val;
6128
  if (HAS_VALUE == pColData->flag) {
9,104✔
6129
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
13,128,880✔
6130
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
13,120,776✔
6131
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
13,120,776✔
6132
    }
6133
  } else {
6134
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,851,000✔
6135
      switch (tColDataGetBitValue(pColData, iVal)) {
2,850,000!
6136
        case 0:
142,836✔
6137
        case 1:
6138
          (*numOfNull)++;
142,836✔
6139
          break;
142,836✔
6140
        case 2:
2,707,164✔
6141
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
2,707,164✔
6142
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,707,164✔
6143
          break;
2,707,164✔
6144
        default:
×
6145
          break;
×
6146
      }
6147
    }
6148
  }
6149
}
9,104✔
6150

6151
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
9,143✔
6152
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,143✔
6153
  int16_t *numOfNull = &pAggs->numOfNull;
9,143✔
6154
  *sum = 0;
9,143✔
6155
  *max = INT8_MIN;
9,143✔
6156
  *min = INT8_MAX;
9,143✔
6157
  *numOfNull = 0;
9,143✔
6158

6159
  int8_t val;
6160
  if (HAS_VALUE == pColData->flag) {
9,143✔
6161
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
13,213,136✔
6162
      val = ((int8_t *)pColData->pData)[iVal];
13,204,996✔
6163
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
13,204,996✔
6164
    }
6165
  } else {
6166
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,854,003✔
6167
      switch (tColDataGetBitValue(pColData, iVal)) {
2,853,000!
6168
        case 0:
145,124✔
6169
        case 1:
6170
          (*numOfNull)++;
145,124✔
6171
          break;
145,124✔
6172
        case 2:
2,707,876✔
6173
          val = ((int8_t *)pColData->pData)[iVal];
2,707,876✔
6174
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,707,876✔
6175
          break;
2,707,876✔
6176
        default:
×
6177
          break;
×
6178
      }
6179
    }
6180
  }
6181
}
9,143✔
6182

6183
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
9,122✔
6184
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,122✔
6185
  int16_t *numOfNull = &pAggs->numOfNull;
9,122✔
6186
  *sum = 0;
9,122✔
6187
  *max = INT16_MIN;
9,122✔
6188
  *min = INT16_MAX;
9,122✔
6189
  *numOfNull = 0;
9,122✔
6190

6191
  int16_t val;
6192
  if (HAS_VALUE == pColData->flag) {
9,122✔
6193
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
13,187,524✔
6194
      val = ((int16_t *)pColData->pData)[iVal];
13,179,403✔
6195
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
13,179,403✔
6196
    }
6197
  } else {
6198
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,851,001✔
6199
      switch (tColDataGetBitValue(pColData, iVal)) {
2,850,000!
6200
        case 0:
142,848✔
6201
        case 1:
6202
          (*numOfNull)++;
142,848✔
6203
          break;
142,848✔
6204
        case 2:
2,707,152✔
6205
          val = ((int16_t *)pColData->pData)[iVal];
2,707,152✔
6206
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,707,152✔
6207
          break;
2,707,152✔
6208
        default:
×
6209
          break;
×
6210
      }
6211
    }
6212
  }
6213
}
9,122✔
6214

6215
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
506,134✔
6216
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
506,134✔
6217
  int16_t *numOfNull = &pAggs->numOfNull;
506,134✔
6218
  *sum = 0;
506,134✔
6219
  *max = INT32_MIN;
506,134✔
6220
  *min = INT32_MAX;
506,134✔
6221
  *numOfNull = 0;
506,134✔
6222

6223
  int32_t val;
6224
  if (HAS_VALUE == pColData->flag) {
506,134✔
6225
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,562,015,110✔
6226
      val = ((int32_t *)pColData->pData)[iVal];
1,561,572,237✔
6227
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
1,561,572,237✔
6228
    }
6229
  } else {
6230
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
169,517,043!
6231
      switch (tColDataGetBitValue(pColData, iVal)) {
169,526,178!
6232
        case 0:
9,365,816✔
6233
        case 1:
6234
          (*numOfNull)++;
9,365,816✔
6235
          break;
9,365,816✔
6236
        case 2:
160,234,110✔
6237
          val = ((int32_t *)pColData->pData)[iVal];
160,234,110✔
6238
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
160,234,110✔
6239
          break;
160,234,110✔
6240
        default:
×
6241
          break;
×
6242
      }
6243
    }
6244
  }
6245
}
433,738✔
6246

6247
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
269,768✔
6248
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
269,768✔
6249
  int16_t *numOfNull = &pAggs->numOfNull;
269,768✔
6250
  *sum = 0;
269,768✔
6251
  *max = INT64_MIN;
269,768✔
6252
  *min = INT64_MAX;
269,768✔
6253
  *numOfNull = 0;
269,768✔
6254

6255
  int64_t val;
6256
  if (HAS_VALUE == pColData->flag) {
269,768✔
6257
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
681,371,557✔
6258
      val = ((int64_t *)pColData->pData)[iVal];
681,106,886✔
6259
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
681,106,886✔
6260
    }
6261
  } else {
6262
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
18,749,545✔
6263
      switch (tColDataGetBitValue(pColData, iVal)) {
18,744,448!
6264
        case 0:
1,781,495✔
6265
        case 1:
6266
          (*numOfNull)++;
1,781,495✔
6267
          break;
1,781,495✔
6268
        case 2:
16,962,953✔
6269
          val = ((int64_t *)pColData->pData)[iVal];
16,962,953✔
6270
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
16,962,953✔
6271
          break;
16,962,953✔
6272
        default:
×
6273
          break;
×
6274
      }
6275
    }
6276
  }
6277
}
269,768✔
6278

6279
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
413,309✔
6280
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
413,309✔
6281
  int16_t *numOfNull = &pAggs->numOfNull;
413,309✔
6282
  *(double *)sum = 0;
413,309✔
6283
  *(double *)max = -FLT_MAX;
413,309✔
6284
  *(double *)min = FLT_MAX;
413,309✔
6285
  *numOfNull = 0;
413,309✔
6286

6287
  float val;
6288
  if (HAS_VALUE == pColData->flag) {
413,309✔
6289
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,667,735,838✔
6290
      val = ((float *)pColData->pData)[iVal];
1,667,323,551✔
6291
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
1,667,323,551✔
6292
    }
6293
  } else {
6294
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,866,399✔
6295
      switch (tColDataGetBitValue(pColData, iVal)) {
2,865,377!
6296
        case 0:
145,867✔
6297
        case 1:
6298
          (*numOfNull)++;
145,867✔
6299
          break;
145,867✔
6300
        case 2:
2,719,510✔
6301
          val = ((float *)pColData->pData)[iVal];
2,719,510✔
6302
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,719,510✔
6303
          break;
2,719,510✔
6304
        default:
×
6305
          break;
×
6306
      }
6307
    }
6308
  }
6309
}
413,309✔
6310

6311
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
10,113✔
6312
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
10,113✔
6313
  int16_t *numOfNull = &pAggs->numOfNull;
10,113✔
6314
  *(double *)sum = 0;
10,113✔
6315
  *(double *)max = -DBL_MAX;
10,113✔
6316
  *(double *)min = DBL_MAX;
10,113✔
6317
  *numOfNull = 0;
10,113✔
6318

6319
  double val;
6320
  if (HAS_VALUE == pColData->flag) {
10,113✔
6321
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
16,051,119✔
6322
      val = ((double *)pColData->pData)[iVal];
16,042,009✔
6323
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
16,042,009✔
6324
    }
6325
  } else {
6326
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,854,003✔
6327
      switch (tColDataGetBitValue(pColData, iVal)) {
2,853,000!
6328
        case 0:
144,897✔
6329
        case 1:
6330
          (*numOfNull)++;
144,897✔
6331
          break;
144,897✔
6332
        case 2:
2,708,103✔
6333
          val = ((double *)pColData->pData)[iVal];
2,708,103✔
6334
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,708,103✔
6335
          break;
2,708,103✔
6336
        default:
×
6337
          break;
×
6338
      }
6339
    }
6340
  }
6341
}
10,113✔
6342

6343
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,424✔
6344
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,424✔
6345
  int16_t *numOfNull = &pAggs->numOfNull;
8,424✔
6346
  *(uint64_t *)sum = 0;
8,424✔
6347
  *(uint64_t *)max = 0;
8,424✔
6348
  *(uint64_t *)min = UINT8_MAX;
8,424✔
6349
  *numOfNull = 0;
8,424✔
6350

6351
  uint8_t val;
6352
  if (HAS_VALUE == pColData->flag) {
8,424✔
6353
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
13,074,674✔
6354
      val = ((uint8_t *)pColData->pData)[iVal];
13,067,250✔
6355
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
13,067,250✔
6356
    }
6357
  } else {
6358
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,851,000✔
6359
      switch (tColDataGetBitValue(pColData, iVal)) {
2,850,000!
6360
        case 0:
142,613✔
6361
        case 1:
6362
          (*numOfNull)++;
142,613✔
6363
          break;
142,613✔
6364
        case 2:
2,707,387✔
6365
          val = ((uint8_t *)pColData->pData)[iVal];
2,707,387✔
6366
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,707,387✔
6367
          break;
2,707,387✔
6368
        default:
×
6369
          break;
×
6370
      }
6371
    }
6372
  }
6373
}
8,424✔
6374

6375
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,424✔
6376
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,424✔
6377
  int16_t *numOfNull = &pAggs->numOfNull;
8,424✔
6378
  *(uint64_t *)sum = 0;
8,424✔
6379
  *(uint64_t *)max = 0;
8,424✔
6380
  *(uint64_t *)min = UINT16_MAX;
8,424✔
6381
  *numOfNull = 0;
8,424✔
6382

6383
  uint16_t val;
6384
  if (HAS_VALUE == pColData->flag) {
8,424✔
6385
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
13,074,220✔
6386
      val = ((uint16_t *)pColData->pData)[iVal];
13,066,796✔
6387
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
13,066,796✔
6388
    }
6389
  } else {
6390
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,851,000✔
6391
      switch (tColDataGetBitValue(pColData, iVal)) {
2,850,000!
6392
        case 0:
142,740✔
6393
        case 1:
6394
          (*numOfNull)++;
142,740✔
6395
          break;
142,740✔
6396
        case 2:
2,707,260✔
6397
          val = ((uint16_t *)pColData->pData)[iVal];
2,707,260✔
6398
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,707,260✔
6399
          break;
2,707,260✔
6400
        default:
×
6401
          break;
×
6402
      }
6403
    }
6404
  }
6405
}
8,424✔
6406

6407
static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,424✔
6408
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,424✔
6409
  int16_t *numOfNull = &pAggs->numOfNull;
8,424✔
6410
  *(uint64_t *)sum = 0;
8,424✔
6411
  *(uint64_t *)max = 0;
8,424✔
6412
  *(uint64_t *)min = UINT32_MAX;
8,424✔
6413
  *numOfNull = 0;
8,424✔
6414

6415
  uint32_t val;
6416
  if (HAS_VALUE == pColData->flag) {
8,424✔
6417
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
13,074,260✔
6418
      val = ((uint32_t *)pColData->pData)[iVal];
13,066,836✔
6419
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
13,066,836✔
6420
    }
6421
  } else {
6422
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,851,000✔
6423
      switch (tColDataGetBitValue(pColData, iVal)) {
2,850,000!
6424
        case 0:
142,942✔
6425
        case 1:
6426
          (*numOfNull)++;
142,942✔
6427
          break;
142,942✔
6428
        case 2:
2,707,058✔
6429
          val = ((uint32_t *)pColData->pData)[iVal];
2,707,058✔
6430
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,707,058✔
6431
          break;
2,707,058✔
6432
        default:
×
6433
          break;
×
6434
      }
6435
    }
6436
  }
6437
}
8,424✔
6438

6439
static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,424✔
6440
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,424✔
6441
  int16_t *numOfNull = &pAggs->numOfNull;
8,424✔
6442
  *(uint64_t *)sum = 0;
8,424✔
6443
  *(uint64_t *)max = 0;
8,424✔
6444
  *(uint64_t *)min = UINT64_MAX;
8,424✔
6445
  *numOfNull = 0;
8,424✔
6446

6447
  uint64_t val;
6448
  if (HAS_VALUE == pColData->flag) {
8,424✔
6449
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
13,074,563✔
6450
      val = ((uint64_t *)pColData->pData)[iVal];
13,067,139✔
6451
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
13,067,139✔
6452
    }
6453
  } else {
6454
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,851,000✔
6455
      switch (tColDataGetBitValue(pColData, iVal)) {
2,850,000!
6456
        case 0:
142,797✔
6457
        case 1:
6458
          (*numOfNull)++;
142,797✔
6459
          break;
142,797✔
6460
        case 2:
2,707,203✔
6461
          val = ((uint64_t *)pColData->pData)[iVal];
2,707,203✔
6462
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,707,203✔
6463
          break;
2,707,203✔
6464
        default:
×
6465
          break;
×
6466
      }
6467
    }
6468
  }
6469
}
8,424✔
6470

6471
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
21,414✔
6472
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
21,414✔
6473
  int16_t *numOfNull = &pAggs->numOfNull;
21,414✔
6474
  *(uint64_t *)sum = 0;
21,414✔
6475
  *(uint64_t *)max = 0;
21,414✔
6476
  *(uint64_t *)min = 0;
21,414✔
6477
  *numOfNull = 0;
21,414✔
6478

6479
  switch (pColData->flag) {
21,414!
6480
    case HAS_NONE:
×
6481
    case HAS_NULL:
6482
    case (HAS_NONE | HAS_NULL):
6483
      *numOfNull = pColData->nVal;
×
6484
      break;
×
6485
    case HAS_VALUE:
19,411✔
6486
      *numOfNull = 0;
19,411✔
6487
      break;
19,411✔
6488
    case (HAS_VALUE | HAS_NULL):
2,003✔
6489
    case (HAS_VALUE | HAS_NONE):
6490
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
5,705,003✔
6491
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
5,703,000✔
6492
          (*numOfNull)++;
287,038✔
6493
        }
6494
      }
6495
      break;
2,003✔
6496
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6497
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6498
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6499
          (*numOfNull)++;
×
6500
        }
6501
      }
6502
      break;
×
6503
    default:
×
6504
      break;
×
6505
  }
6506
}
21,414✔
6507

6508
#define CALC_DECIMAL_SUM_MAX_MIN(TYPE, pSumOp, pCompOp, pColData, pSum, pMax, pMin)                   \
6509
  do {                                                                                                \
6510
    if (decimal128AddCheckOverflow((Decimal *)pSum, pVal, DECIMAL_WORD_NUM(TYPE))) *pOverflow = true; \
6511
    pSumOp->add(pSum, pVal, DECIMAL_WORD_NUM(TYPE));                                                  \
6512
    if (pCompOp->gt(pVal, pMax, DECIMAL_WORD_NUM(TYPE))) {                                            \
6513
      *(pMax) = *pVal;                                                                                \
6514
    }                                                                                                 \
6515
    if (pCompOp->lt(pVal, pMin, DECIMAL_WORD_NUM(TYPE))) {                                            \
6516
      *(pMin) = *pVal;                                                                                \
6517
    }                                                                                                 \
6518
  } while (0)
6519

6520
static FORCE_INLINE void tColDataCalcSMADecimal64Type(SColData *pColData, SColumnDataAgg *pAggs) {
747✔
6521
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum;
747✔
6522
  Decimal64  *pMax = (Decimal64 *)pAggs->decimal128Max, *pMin = (Decimal64 *)pAggs->decimal128Min;
747✔
6523
  uint8_t    *pOverflow = &pAggs->overflow;
747✔
6524
  *pSum = DECIMAL128_ZERO;
747✔
6525
  *pMax = DECIMAL64_MIN;
747✔
6526
  *pMin = DECIMAL64_MAX;
747✔
6527
  pAggs->numOfNull = 0;
747✔
6528
  pAggs->colId |= DECIMAL_AGG_FLAG;
747✔
6529

6530
  Decimal64         *pVal = NULL;
747✔
6531
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
747✔
6532
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
747✔
6533
  if (HAS_VALUE == pColData->flag) {
747✔
6534
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
2,289,667✔
6535
      pVal = ((Decimal64 *)pColData->pData) + iVal;
2,288,947✔
6536
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
2,288,947!
6537
    }
6538
  } else {
6539
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
20,727✔
6540
      switch (tColDataGetBitValue(pColData, iVal)) {
20,700!
6541
        case 0:
1,209✔
6542
        case 1:
6543
          pAggs->numOfNull++;
1,209✔
6544
          break;
1,209✔
6545
        case 2:
19,491✔
6546
          pVal = ((Decimal64 *)pColData->pData) + iVal;
19,491✔
6547
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
19,491!
6548
          break;
19,491✔
6549
        default:
×
6550
          break;
×
6551
      }
6552
    }
6553
  }
6554
}
747✔
6555

6556
static FORCE_INLINE void tColDataCalcSMADecimal128Type(SColData *pColData, SColumnDataAgg *pAggs) {
793✔
6557
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum, *pMax = (Decimal128 *)pAggs->decimal128Max,
793✔
6558
             *pMin = (Decimal128 *)pAggs->decimal128Min;
793✔
6559
  uint8_t *pOverflow = &pAggs->overflow;
793✔
6560
  *pSum = DECIMAL128_ZERO;
793✔
6561
  *pMax = DECIMAL128_MIN;
793✔
6562
  *pMin = DECIMAL128_MAX;
793✔
6563
  pAggs->numOfNull = 0;
793✔
6564
  pAggs->colId |= DECIMAL_AGG_FLAG;
793✔
6565

6566
  Decimal128        *pVal = NULL;
793✔
6567
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
793✔
6568
  if (HAS_VALUE == pColData->flag) {
793✔
6569
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
2,292,300✔
6570
      pVal = ((Decimal128 *)pColData->pData) + iVal;
2,291,816✔
6571
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
2,291,816✔
6572
    }
6573
  } else {
6574
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
66,773✔
6575
      switch (tColDataGetBitValue(pColData, iVal)) {
66,700!
6576
        case 0:
3,865✔
6577
        case 1:
6578
          pAggs->numOfNull++;
3,865✔
6579
          break;
3,865✔
6580
        case 2:
62,835✔
6581
          pVal = ((Decimal128 *)pColData->pData) + iVal;
62,835✔
6582
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
62,835✔
6583
          break;
62,835✔
6584
        default:
×
6585
          break;
×
6586
      }
6587
    }
6588
  }
6589
}
557✔
6590

6591
void (*tColDataCalcSMA[])(SColData *pColData, SColumnDataAgg *pAggs) = {
6592
    NULL,
6593
    tColDataCalcSMABool,            // TSDB_DATA_TYPE_BOOL
6594
    tColDataCalcSMATinyInt,         // TSDB_DATA_TYPE_TINYINT
6595
    tColDataCalcSMATinySmallInt,    // TSDB_DATA_TYPE_SMALLINT
6596
    tColDataCalcSMAInt,             // TSDB_DATA_TYPE_INT
6597
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_BIGINT
6598
    tColDataCalcSMAFloat,           // TSDB_DATA_TYPE_FLOAT
6599
    tColDataCalcSMADouble,          // TSDB_DATA_TYPE_DOUBLE
6600
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARCHAR
6601
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_TIMESTAMP
6602
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_NCHAR
6603
    tColDataCalcSMAUTinyInt,        // TSDB_DATA_TYPE_UTINYINT
6604
    tColDataCalcSMATinyUSmallInt,   // TSDB_DATA_TYPE_USMALLINT
6605
    tColDataCalcSMAUInt,            // TSDB_DATA_TYPE_UINT
6606
    tColDataCalcSMAUBigInt,         // TSDB_DATA_TYPE_UBIGINT
6607
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_JSON
6608
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARBINARY
6609
    tColDataCalcSMADecimal128Type,  // TSDB_DATA_TYPE_DECIMAL
6610
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_BLOB
6611
    NULL,                           // TSDB_DATA_TYPE_MEDIUMBLOB
6612
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_GEOMETRY
6613
    tColDataCalcSMADecimal64Type,   // TSDB_DATA_TYPE_DECIMAL64
6614
};
6615

6616
// SValueColumn ================================
6617
int32_t tValueColumnInit(SValueColumn *valCol) {
32,251,199✔
6618
  valCol->type = TSDB_DATA_TYPE_NULL;
32,251,199✔
6619
  valCol->numOfValues = 0;
32,251,199✔
6620
  tBufferInit(&valCol->data);
32,251,199✔
6621
  tBufferInit(&valCol->offsets);
32,251,199✔
6622
  return 0;
32,251,199✔
6623
}
6624

6625
void tValueColumnDestroy(SValueColumn *valCol) {
71,713,072✔
6626
  valCol->type = TSDB_DATA_TYPE_NULL;
71,713,072✔
6627
  valCol->numOfValues = 0;
71,713,072✔
6628
  tBufferDestroy(&valCol->data);
71,713,072✔
6629
  tBufferDestroy(&valCol->offsets);
71,713,077✔
6630
  return;
71,713,078✔
6631
}
6632

6633
void tValueColumnClear(SValueColumn *valCol) {
34,077,519✔
6634
  valCol->type = TSDB_DATA_TYPE_NULL;
34,077,519✔
6635
  valCol->numOfValues = 0;
34,077,519✔
6636
  tBufferClear(&valCol->data);
34,077,519✔
6637
  tBufferClear(&valCol->offsets);
34,077,519✔
6638
  return;
34,077,519✔
6639
}
6640

6641
int32_t tValueColumnAppend(SValueColumn *valCol, const SValue *value) {
1,784,042✔
6642
  int32_t code;
6643

6644
  if (valCol->numOfValues == 0) {
1,784,042✔
6645
    valCol->type = value->type;
10,441✔
6646
  }
6647

6648
  if (!(value->type == valCol->type)) {
1,784,042!
6649
    return TSDB_CODE_INVALID_PARA;
×
6650
  }
6651

6652
  if (IS_VAR_DATA_TYPE(value->type)) {
1,784,042!
6653
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
2,792!
6654
      return code;
×
6655
    }
6656
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
2,792!
6657
      return code;
×
6658
    }
6659
  } else {
6660
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
1,782,646!
6661
    if (code) return code;
1,782,646!
6662
  }
6663
  valCol->numOfValues++;
1,784,042✔
6664

6665
  return 0;
1,784,042✔
6666
}
6667

6668
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
465,941,132✔
6669
  int32_t code;
6670

6671
  if (idx < 0 || idx >= valCol->numOfValues) {
465,941,132!
6672
    return TSDB_CODE_OUT_OF_RANGE;
×
6673
  }
6674

6675
  if (IS_VAR_DATA_TYPE(valCol->type)) {
466,028,739!
6676
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
×
6677
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
×
6678
    int32_t  oldDataSize = nextOffset - offsets[idx];
×
6679
    int32_t  bytesAdded = value->nData - oldDataSize;
×
6680

6681
    if (bytesAdded != 0) {
×
6682
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
74!
6683
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
37✔
6684
              tBufferGetSize(&valCol->data) - nextOffset);
37✔
6685
      valCol->data.size += bytesAdded;
37✔
6686

6687
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
37!
6688
        offsets[i] += bytesAdded;
×
6689
      }
6690
    }
6691
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
×
6692
  } else {
6693
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
466,104,298!
6694
                        tDataTypes[valCol->type].bytes);
466,104,298✔
6695
  }
6696
  return 0;
6697
}
6698

6699
int32_t tValueColumnGet(SValueColumn *valCol, int32_t idx, SValue *value) {
938,709,431✔
6700
  if (idx < 0 || idx >= valCol->numOfValues) {
938,709,431!
6701
    return TSDB_CODE_OUT_OF_RANGE;
×
6702
  }
6703

6704
  value->type = valCol->type;
938,894,821✔
6705
  if (IS_VAR_DATA_TYPE(value->type)) {
938,894,821!
6706
    int32_t       offset, nextOffset;
6707
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
×
6708

6709
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
×
6710
    if (idx == valCol->numOfValues - 1) {
28,033✔
6711
      nextOffset = tBufferGetSize(&valCol->data);
19,453✔
6712
    } else {
6713
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
8,580✔
6714
    }
6715
    value->nData = nextOffset - offset;
28,027✔
6716
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
28,027✔
6717
  } else {
6718
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
939,163,688✔
6719
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
1,878,327,376!
6720
  }
6721
  return 0;
939,191,715✔
6722
}
6723

6724
int32_t tValueColumnCompress(SValueColumn *valCol, SValueColumnCompressInfo *info, SBuffer *output, SBuffer *assist) {
10,442✔
6725
  int32_t code;
6726

6727
  if (!(valCol->numOfValues > 0)) {
10,442!
6728
    return TSDB_CODE_INVALID_PARA;
×
6729
  }
6730

6731
  (*info) = (SValueColumnCompressInfo){
10,442✔
6732
      .cmprAlg = info->cmprAlg,
10,442✔
6733
      .type = valCol->type,
10,442✔
6734
  };
6735

6736
  // offset
6737
  if (IS_VAR_DATA_TYPE(valCol->type)) {
10,442!
6738
    SCompressInfo cinfo = {
664✔
6739
        .cmprAlg = info->cmprAlg,
664✔
6740
        .dataType = TSDB_DATA_TYPE_INT,
6741
        .originalSize = valCol->offsets.size,
664✔
6742
    };
6743

6744
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
664✔
6745
    if (code) return code;
664!
6746

6747
    info->offsetOriginalSize = cinfo.originalSize;
664✔
6748
    info->offsetCompressedSize = cinfo.compressedSize;
664✔
6749
  }
6750

6751
  // data
6752
  SCompressInfo cinfo = {
10,442✔
6753
      .cmprAlg = info->cmprAlg,
10,442✔
6754
      .dataType = valCol->type,
10,442✔
6755
      .originalSize = valCol->data.size,
10,442✔
6756
  };
6757

6758
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
10,442✔
6759
  if (code) return code;
10,442!
6760

6761
  info->dataOriginalSize = cinfo.originalSize;
10,442✔
6762
  info->dataCompressedSize = cinfo.compressedSize;
10,442✔
6763

6764
  return 0;
10,442✔
6765
}
6766

6767
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
115,627✔
6768
                               SBuffer *assist) {
6769
  int32_t code;
6770

6771
  tValueColumnClear(valCol);
115,627✔
6772
  valCol->type = info->type;
115,631✔
6773
  // offset
6774
  if (IS_VAR_DATA_TYPE(valCol->type)) {
129,372!
6775
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
13,734✔
6776

6777
    SCompressInfo cinfo = {
13,734✔
6778
        .dataType = TSDB_DATA_TYPE_INT,
6779
        .cmprAlg = info->cmprAlg,
13,734✔
6780
        .originalSize = info->offsetOriginalSize,
13,734✔
6781
        .compressedSize = info->offsetCompressedSize,
13,734✔
6782
    };
6783

6784
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
13,734✔
6785
    if (code) {
13,741!
6786
      return code;
×
6787
    }
6788
  } else {
6789
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
101,897✔
6790
  }
6791

6792
  // data
6793
  SCompressInfo cinfo = {
115,638✔
6794
      .dataType = valCol->type,
115,638✔
6795
      .cmprAlg = info->cmprAlg,
115,638✔
6796
      .originalSize = info->dataOriginalSize,
115,638✔
6797
      .compressedSize = info->dataCompressedSize,
115,638✔
6798
  };
6799

6800
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
115,638✔
6801
  if (code) {
115,643!
6802
    return code;
×
6803
  }
6804

6805
  return 0;
115,643✔
6806
}
6807

6808
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *info, SBuffer *buffer) {
10,442✔
6809
  int32_t code;
6810
  uint8_t fmtVer = 0;
10,442✔
6811

6812
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
20,884!
6813
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
20,884!
6814
  if ((code = tBufferPutI8(buffer, info->type))) return code;
20,884!
6815
  if (IS_VAR_DATA_TYPE(info->type)) {
10,442!
6816
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
1,328!
6817
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
1,328!
6818
  }
6819
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
20,884!
6820
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
20,884!
6821

6822
  return 0;
10,442✔
6823
}
6824

6825
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
115,627✔
6826
  int32_t code;
6827
  uint8_t fmtVer;
6828

6829
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
115,627!
6830
  if (fmtVer == 0) {
115,632!
6831
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
115,632!
6832
    if ((code = tBufferGetI8(reader, &info->type))) return code;
115,630!
6833
    if (IS_VAR_DATA_TYPE(info->type)) {
115,629!
6834
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
13,737!
6835
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
13,741!
6836
    } else {
6837
      info->offsetOriginalSize = 0;
101,892✔
6838
      info->offsetCompressedSize = 0;
101,892✔
6839
    }
6840
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
115,631!
6841
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
115,633!
6842
  } else {
6843
    return TSDB_CODE_INVALID_PARA;
×
6844
  }
6845

6846
  return 0;
115,634✔
6847
}
6848

6849
int32_t tCompressData(void          *input,       // input
6,344,850✔
6850
                      SCompressInfo *info,        // compress info
6851
                      void          *output,      // output
6852
                      int32_t        outputSize,  // output size
6853
                      SBuffer       *buffer       // assistant buffer provided by caller, can be NULL
6854
) {
6855
  int32_t extraSizeNeeded;
6856
  int32_t code;
6857

6858
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
6,344,850✔
6859
  if (!(outputSize >= extraSizeNeeded)) {
6,344,850!
6860
    return TSDB_CODE_INVALID_PARA;
×
6861
  }
6862

6863
  if (info->cmprAlg == NO_COMPRESSION) {
6,344,850✔
6864
    (void)memcpy(output, input, info->originalSize);
244✔
6865
    info->compressedSize = info->originalSize;
244✔
6866
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
7,908,577✔
6867
    SBuffer local;
6868

6869
    tBufferInit(&local);
6870
    if (buffer == NULL) {
1,563,809!
6871
      buffer = &local;
×
6872
    }
6873

6874
    if (info->cmprAlg == TWO_STAGE_COMP) {
1,563,809✔
6875
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
1,563,754✔
6876
      if (code) {
1,563,754!
6877
        tBufferDestroy(&local);
6878
        return code;
×
6879
      }
6880
    }
6881

6882
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
3,127,780✔
6883
        input,                                                   // input
6884
        info->originalSize,                                      // input size
6885
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
1,563,809✔
6886
        output,                                                  // output
6887
        outputSize,                                              // output size
6888
        info->cmprAlg,                                           // compression algorithm
1,563,809✔
6889
        buffer->data,                                            // buffer
6890
        buffer->capacity                                         // buffer size
1,563,809✔
6891
    );
6892
    if (info->compressedSize < 0) {
1,563,971!
6893
      tBufferDestroy(&local);
6894
      return TSDB_CODE_COMPRESS_ERROR;
×
6895
    }
6896

6897
    tBufferDestroy(&local);
6898
  } else {
6899
    DEFINE_VAR(info->cmprAlg)
4,780,797✔
6900
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
4,780,797!
6901
      (void)memcpy(output, input, info->originalSize);
12✔
6902
      info->compressedSize = info->originalSize;
12✔
6903
      return 0;
12✔
6904
    }
6905
    SBuffer local;
6906

6907
    tBufferInit(&local);
6908
    if (buffer == NULL) {
4,780,785!
6909
      buffer = &local;
×
6910
    }
6911
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
4,780,785✔
6912

6913
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
9,562,022✔
6914
        input,                                                      // input
6915
        info->originalSize,                                         // input size
6916
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
4,780,784✔
6917
        output,                                                     // output
6918
        outputSize,                                                 // output size
6919
        info->cmprAlg,                                              // compression algorithm
6920
        buffer->data,                                               // buffer
6921
        buffer->capacity                                            // buffer size
4,780,784✔
6922
    );
6923
    if (info->compressedSize < 0) {
4,781,238!
6924
      tBufferDestroy(&local);
6925
      return TSDB_CODE_COMPRESS_ERROR;
×
6926
    }
6927

6928
    tBufferDestroy(&local);
6929
    // new col compress
6930
  }
6931

6932
  return 0;
6,345,453✔
6933
}
6934

6935
int32_t tDecompressData(void                *input,       // input
112,622,846✔
6936
                        const SCompressInfo *info,        // compress info
6937
                        void                *output,      // output
6938
                        int32_t              outputSize,  // output size
6939
                        SBuffer             *buffer       // assistant buffer provided by caller, can be NULL
6940
) {
6941
  int32_t code;
6942

6943
  if (!(outputSize >= info->originalSize)) {
112,622,846!
6944
    return TSDB_CODE_INVALID_PARA;
×
6945
  }
6946

6947
  if (info->cmprAlg == NO_COMPRESSION) {
112,622,846✔
6948
    if (!(info->compressedSize == info->originalSize)) {
244!
6949
      return TSDB_CODE_INVALID_PARA;
×
6950
    }
6951
    (void)memcpy(output, input, info->compressedSize);
244✔
6952
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
156,513,122!
6953
    SBuffer local;
6954

6955
    tBufferInit(&local);
6956
    if (buffer == NULL) {
43,838,921!
6957
      buffer = &local;
×
6958
    }
6959

6960
    if (info->cmprAlg == TWO_STAGE_COMP) {
43,838,921!
6961
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
43,885,513✔
6962
      if (code) {
43,886,465!
6963
        tBufferDestroy(&local);
6964
        return code;
×
6965
      }
6966
    }
6967

6968
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
43,839,873✔
6969
        input,                                                  // input
6970
        info->compressedSize,                                   // inputSize
43,839,873✔
6971
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
43,839,873✔
6972
        output,                                                 // output
6973
        outputSize,                                             // output size
6974
        info->cmprAlg,                                          // compression algorithm
43,839,873✔
6975
        buffer->data,                                           // helper buffer
6976
        buffer->capacity                                        // extra buffer size
43,839,873✔
6977
    );
6978
    if (decompressedSize < 0) {
43,890,520!
6979
      tBufferDestroy(&local);
6980
      return TSDB_CODE_COMPRESS_ERROR;
×
6981
    }
6982

6983
    if (!(decompressedSize == info->originalSize)) {
43,890,520!
6984
      return TSDB_CODE_COMPRESS_ERROR;
×
6985
    }
6986
    tBufferDestroy(&local);
6987
  } else {
6988
    DEFINE_VAR(info->cmprAlg);
68,783,681✔
6989
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
68,783,681✔
6990
      (void)memcpy(output, input, info->compressedSize);
18✔
6991
      return 0;
18✔
6992
    }
6993
    SBuffer local;
6994

6995
    tBufferInit(&local);
6996
    if (buffer == NULL) {
68,783,663!
6997
      buffer = &local;
×
6998
    }
6999
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
68,783,663✔
7000
    if (code) {
68,783,439!
7001
      return code;
×
7002
    }
7003

7004
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
68,783,439✔
7005
        input,                                                  // input
7006
        info->compressedSize,                                   // inputSize
68,783,439✔
7007
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
68,783,439✔
7008
        output,                                                 // output
7009
        outputSize,                                             // output size
7010
        info->cmprAlg,                                          // compression algorithm
68,783,439✔
7011
        buffer->data,                                           // helper buffer
7012
        buffer->capacity                                        // extra buffer size
68,783,439✔
7013
    );
7014
    if (decompressedSize < 0) {
68,791,404!
7015
      tBufferDestroy(&local);
7016
      return TSDB_CODE_COMPRESS_ERROR;
×
7017
    }
7018

7019
    if (!(decompressedSize == info->originalSize)) {
68,791,404!
7020
      return TSDB_CODE_COMPRESS_ERROR;
×
7021
    }
7022
    tBufferDestroy(&local);
7023
  }
7024

7025
  return 0;
112,682,168✔
7026
}
7027

7028
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
6,344,893✔
7029
  int32_t code;
7030

7031
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
6,344,893✔
7032
  if (code) return code;
6,344,906!
7033

7034
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
6,344,906✔
7035
  if (code) return code;
6,345,268!
7036

7037
  output->size += info->compressedSize;
6,345,268✔
7038
  return 0;
6,345,268✔
7039
}
7040

7041
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
43,883,576✔
7042
  int32_t code;
7043

7044
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
43,883,576✔
7045
  if (code) return code;
43,877,461!
7046

7047
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
43,877,461✔
7048
  if (code) return code;
43,888,021!
7049

7050
  output->size += info->originalSize;
43,888,021✔
7051
  return 0;
43,888,021✔
7052
}
7053

7054
// handle all types, including var data
7055
void valueSetDatum(SValue *pVal, int8_t type, void *pDatum, uint32_t len) {
2,147,483,647✔
7056
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647!
7057
    pVal->pData = pDatum;
×
7058
    pVal->nData = len;
×
7059
  } else {
7060
    switch (len) {
2,147,483,647!
7061
      case sizeof(uint8_t):
2,147,483,647✔
7062
        pVal->val = *(uint8_t *)pDatum;
2,147,483,647✔
7063
        break;
2,147,483,647✔
7064
      case sizeof(uint16_t):
1,900,078,647✔
7065
        pVal->val = *(uint16_t *)pDatum;
1,900,078,647✔
7066
        break;
1,900,078,647✔
7067
      case sizeof(uint32_t):
2,147,483,647✔
7068
        pVal->val = *(uint32_t *)pDatum;
2,147,483,647✔
7069
        break;
2,147,483,647✔
7070
      case sizeof(uint64_t):
2,147,483,647✔
7071
        pVal->val = *(uint64_t *)pDatum;
2,147,483,647✔
7072
        break;
2,147,483,647✔
7073
      default:
×
7074
        break;
×
7075
    }
7076
  }
7077
}
2,147,483,647✔
7078

7079
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
152,523,733✔
7080
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
152,523,733!
7081
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
×
7082
    pDst->nData = pSrc->nData;
×
7083
  } else {
7084
    pDst->val = pSrc->val;
153,093,607✔
7085
  }
7086
}
152,523,733✔
7087
void valueClearDatum(SValue *pVal, int8_t type) {
×
7088
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
×
7089
    taosMemoryFreeClear(pVal->pData);
×
7090
    pVal->nData = 0;
×
7091
  } else {
7092
    pVal->val = 0;
×
7093
  }
7094
}
×
7095

7096
int8_t schemaHasBlob(const STSchema *pSchema) {
15,964,859✔
7097
  if (pSchema == NULL) {
15,964,859!
7098
    return 0;
×
7099
  }
7100
  for (int i = 0; i < pSchema->numOfCols; ++i) {
108,601,779✔
7101
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
92,636,920!
7102
      return 1;
×
7103
    }
7104
  }
7105
  return 0;
15,964,859✔
7106
}
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