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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 hits per line

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

68.59
/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) {
253,103,085✔
79
  int32_t n = 0;
253,103,085✔
80
  n += tPutI8(p ? p + n : p, index->type);
253,103,085✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
253,114,833✔
82
  return n;
253,110,389✔
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;
2,147,483,647✔
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;
2,147,483,647✔
100
  sinfo->numOfNull++;
2,147,483,647✔
101
  sinfo->kvMaxOffset = sinfo->kvPayloadSize;
2,147,483,647✔
102
  sinfo->kvPayloadSize += tPutI16v(NULL, -pTColumn->colId);
2,147,483,647✔
103
  return 0;
2,147,483,647✔
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;
84,373,415✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
84,373,063✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
84,373,767✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
84,373,415✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
84,371,963✔
115
    sinfo->numOfPKs++;
84,371,391✔
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)) {
2,147,483,647✔
121
      sinfo->tupleVarSize += tPutU32v(NULL, colVal->value.nData)     // size
53,949,516✔
122
                             + BSE_SEQUECE_SIZE;                     // value
26,974,859✔
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
53,949,516✔
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
26,974,859✔
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
53,949,516✔
126
    } else {
127
      sinfo->tupleVarSize += tPutU32v(NULL, colVal->value.nData)  // size
2,147,483,647✔
128
                             + colVal->value.nData;               // value
2,147,483,647✔
129

130
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
2,147,483,647✔
131
                              + tPutU32v(NULL, colVal->value.nData)  // size
2,147,483,647✔
132
                              + colVal->value.nData;                 // value
2,147,483,647✔
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;
176,968✔
164
        break;
88,484✔
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;
2,147,483,647✔
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) {
19,536✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
46,368✔
185
        break;
23,184✔
186
      } else {  // skip useless value
187
        colValIndex++;
×
188
      }
189
    }
190
  }
191

192
  if (sinfo->numOfNone) {
2,147,483,647✔
193
    sinfo->flag |= HAS_NONE;
2,147,483,647✔
194
  }
195
  if (sinfo->numOfNull) {
2,147,483,647✔
196
    sinfo->flag |= HAS_NULL;
2,134,231,643✔
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:
2,147,483,647✔
206
    case HAS_NULL:
207
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
208
      sinfo->tupleFixedSize = 0;
2,147,483,647✔
209
      break;
2,147,483,647✔
210
    case HAS_VALUE:
2,147,483,647✔
211
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
212
      sinfo->tupleFixedSize = schema->flen;
2,147,483,647✔
213
      break;
2,147,483,647✔
214
    case (HAS_NONE | HAS_NULL):
2,947,731✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
2,947,731✔
216
      sinfo->tupleFixedSize = 0;
2,947,731✔
217
      break;
2,947,089✔
218
    case (HAS_NONE | HAS_VALUE):
2,147,483,647✔
219
    case (HAS_NULL | HAS_VALUE):
220
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
2,147,483,647✔
221
      sinfo->tupleFixedSize = schema->flen;
2,147,483,647✔
222
      break;
2,147,483,647✔
223
    case (HAS_NONE | HAS_NULL | HAS_VALUE):
1,188,741,682✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,188,741,682✔
225
      sinfo->tupleFixedSize = schema->flen;
1,188,750,670✔
226
      break;
1,071,922,636✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
84,368,487✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
84,373,635✔
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) {
48,319,388✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
48,425,405✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
48,426,023✔
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;
84,369,983✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
84,372,711✔
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,
2,147,483,647✔
263
                                 SRow **ppRow) {
264
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
2,147,483,647✔
265
  *ppRow = (SRow *)taosMemoryCalloc(1, sinfo->tupleRowSize);
2,147,483,647✔
266
  if (*ppRow == NULL) {
2,147,483,647✔
267
    return terrno;
×
268
  }
269
  (*ppRow)->flag = sinfo->tupleFlag;
2,147,483,647✔
270
  (*ppRow)->numOfPKs = sinfo->numOfPKs;
2,147,483,647✔
271
  (*ppRow)->sver = schema->version;
2,147,483,647✔
272
  (*ppRow)->len = sinfo->tupleRowSize;
2,147,483,647✔
273
  (*ppRow)->ts = VALUE_GET_TRIVIAL_DATUM(&colValArray[0].value);
2,147,483,647✔
274

275
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
2,147,483,647✔
276
    return 0;
2,147,483,647✔
277
  }
278

279
  uint8_t *primaryKeys = (*ppRow)->data;
2,147,483,647✔
280
  uint8_t *bitmap = primaryKeys + sinfo->tuplePKSize;
2,147,483,647✔
281
  uint8_t *fixed = bitmap + sinfo->tupleBitmapSize;
2,147,483,647✔
282
  uint8_t *varlen = fixed + sinfo->tupleFixedSize;
2,147,483,647✔
283

284
  // primary keys
285
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
286
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->tupleIndices + i);
78,074,065✔
287
  }
288

289
  // bitmap + fixed + varlen
290
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
2,147,483,647✔
291
  int32_t colValIndex = 1;
2,147,483,647✔
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);
15,280✔
296
        break;
15,280✔
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;
2,147,483,647✔
305
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
2,147,483,647✔
306
            if (colValArray[colValIndex].value.nData) {
2,147,483,647✔
307
              (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
2,147,483,647✔
308
              varlen += colValArray[colValIndex].value.nData;
2,147,483,647✔
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
820,276,252✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
766,209,233✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
56,191,694✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
56,191,082✔
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
1,438,218✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
156✔
325
        break;
156✔
326
      } else {
327
        colValIndex++;
1,438,062✔
328
      }
329
    }
330
  }
331

332
  return 0;
2,147,483,647✔
333
}
334

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

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

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

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

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

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

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

395
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
78,192✔
396
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
21,616✔
397
              hasBlob = 1;
4,352✔
398
            }
399
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
21,616✔
400
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
43,232✔
401
            if (colValArray[colValIndex].value.nData) {
21,616✔
402
              if (hasBlob == 0) {
21,616✔
403
                (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
17,264✔
404
                varlen += colValArray[colValIndex].value.nData;
17,264✔
405
              } else {
406
                uint64_t  seq = 0;
4,352✔
407
                SBlobItem item = {.seqOffsetInRow = varlen - (*ppRow)->data,
4,352✔
408
                                  .data = colValArray[colValIndex].value.pData,
4,352✔
409
                                  .len = colValArray[colValIndex].value.nData,
4,352✔
410
                                  .type = TSDB_DATA_BLOB_VALUE};
411
                code = tBlobSetPush(pBlobSet, &item, &seq, firstBlobCol);
4,352✔
412
                if (firstBlobCol == 1) {
4,352✔
413
                  firstBlobCol = 0;
4,352✔
414
                }
415
                if (code != 0) return code;
4,352✔
416
                varlen += tPutU64(varlen, seq);
8,704✔
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,
56,576✔
423
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
56,576✔
424
                         tDataTypes[schema->columns[i].type].bytes);
56,576✔
425
          }
426
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
10,521✔
427
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
501✔
428
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
501✔
429
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
501✔
430
          }
431
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
10,020✔
432
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
10,020✔
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);
10,020✔
436
        }
437

438
        colValIndex++;
88,713✔
439
        break;
88,713✔
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) {
4,853✔
450
    return TSDB_CODE_INVALID_PARA;
×
451
  }
452

453
  return 0;
4,853✔
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) {
787,577,649✔
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
787,577,649✔
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) {
2,147,483,647✔
577
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
2,147,483,647✔
578

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

589
  if (!(sinfo->flag != HAS_NONE && sinfo->flag != HAS_NULL)) {
2,147,483,647✔
590
    return TSDB_CODE_INVALID_PARA;
×
591
  }
592

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

598
  // primary keys
599
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
600
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
6,298,074✔
601
  }
602

603
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
2,147,483,647✔
604
  int32_t colValIndex = 1;
2,147,483,647✔
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;
38,268✔
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);
2,147,483,647✔
616
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
2,147,483,647✔
617
            if (colValArray[colValIndex].value.nData > 0) {
2,147,483,647✔
618
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
2,147,483,647✔
619
                           colValArray[colValIndex].value.nData);
2,147,483,647✔
620
              payloadSize += colValArray[colValIndex].value.nData;
2,147,483,647✔
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);
2,147,483,647✔
631
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
2,147,483,647✔
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
22,929✔
637
        break;
23,028✔
638
      } else {
639
        colValIndex++;
×
640
      }
641
    }
642
  }
643
  return 0;
2,147,483,647✔
644
}
645

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

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

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

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

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

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

678
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
26,972,067✔
679
  int32_t colValIndex = 1;
26,972,067✔
680
  int8_t  firstBlobCol = 1;
26,972,067✔
681
  for (int32_t i = 1; i < schema->numOfCols; i++) {
60,998,516✔
682
    for (;;) {
×
683
      if (colValIndex >= numOfColVals) {  // NONE
34,026,449✔
684
        break;
×
685
      }
686
      uint8_t hasBlob = 0;
34,026,449✔
687

688
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
34,026,449✔
689
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
34,026,449✔
690
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
30,507,869✔
691
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
30,507,869✔
692
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
27,686,987✔
693
              hasBlob = 1;
26,970,507✔
694
            }
695
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
27,686,987✔
696
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
27,686,987✔
697
            if (colValArray[colValIndex].value.nData > 0) {
27,686,987✔
698
              if (hasBlob == 0) {
25,511,582✔
699
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
716,480✔
700
                             colValArray[colValIndex].value.nData);
716,480✔
701
                payloadSize += colValArray[colValIndex].value.nData;
716,480✔
702
              } else {
703
                uint64_t  seq = 0;
24,795,102✔
704
                uint32_t  seqOffset = payloadSize + payload - (*ppRow)->data;
24,795,102✔
705
                SBlobItem item = {.seqOffsetInRow = seqOffset,
24,795,102✔
706
                                  .data = colValArray[colValIndex].value.pData,
24,795,102✔
707
                                  .len = colValArray[colValIndex].value.nData,
24,795,102✔
708
                                  .type = TSDB_DATA_BLOB_VALUE};
709
                int32_t   code = tBlobSetPush(ppBlobSet, &item, &seq, 0);
24,795,102✔
710
                if (code != 0) return code;
24,795,102✔
711
                payloadSize += tPutU64(payload + payloadSize, seq);
49,590,204✔
712
              }
713
            } else {
714
              if (hasBlob) {
2,175,405✔
715
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
2,175,405✔
716
              }
717
            }
718
          } else {
719
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,820,882✔
720
            (void)memcpy(payload + payloadSize,
2,821,286✔
721
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,820,882✔
722
                         tDataTypes[schema->columns[i].type].bytes);
2,820,882✔
723
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
2,820,882✔
724
          }
725
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
3,518,580✔
726
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
1,560✔
727
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
1,560✔
728
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
1,560✔
729
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
1,560✔
730
          }
731
        }
732
        colValIndex++;
34,026,449✔
733
        break;
34,026,449✔
734
      } else if (colValArray[colValIndex].cid > schema->columns[i].colId) {  // NONE
×
735
        break;
×
736
      } else {
737
        colValIndex++;
×
738
      }
739
    }
740
  }
741

742
  if (((*ppRow)->flag) == 0) {
26,972,067✔
743
    return TSDB_CODE_INVALID_PARA;
×
744
  }
745
  return 0;
26,972,067✔
746
}
747

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

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

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

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

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

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

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

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

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

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

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

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

857
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
2,147,483,647✔
858
    code = tRowBuildTupleRow(aColVal, sinfo, pTSchema, ppRow);
2,147,483,647✔
859
  } else {
860
    code = tRowBuildKVRow(aColVal, sinfo, pTSchema, ppRow);
2,147,483,647✔
861
  }
862
  return code;
2,147,483,647✔
863
}
864

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

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

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

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

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

894
  p->cap = cap;
×
895
  p->len = 0;
×
896
  p->seq = 1;
×
897

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

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

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

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

943
  offset = pBlobRow->len;
×
944
  pBlobRow->len += len;
×
945

946
  pBlobRow->seq++;
×
947
  *seq = pBlobRow->seq;
×
948

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

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

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

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

983
  int32_t code = 0;
×
984
  int32_t len = 0;
×
985
  int32_t dataOffset = 0;
×
986
  int8_t  nextRow = 0;
×
987

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

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

998
  len = value->len;
×
999
  dataOffset = value->dataOffset;
×
1000
  nextRow = value->nextRow;
×
1001

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

1006
  return code;
×
1007
}
1008

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

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

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

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

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

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

1050
  return code;
×
1051
}
1052

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

1075
int32_t tRowBuildWithBlob(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow, SBlobSet *pBlobSet,
27,533,468✔
1076
                          SRowBuildScanInfo *sinfo) {
1077
  int32_t code;
1078

1079
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
27,533,468✔
1080
  if (code) return code;
27,533,468✔
1081

1082
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
27,533,468✔
1083
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
561,401✔
1084
  } else {
1085
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
26,972,067✔
1086
  }
1087

1088
  return code;
27,533,468✔
1089
}
1090

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

1095
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
×
1096
  if (code) return code;
×
1097

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

1104
  return code;
×
1105
}
1106

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

1115
  p->type = type;
50,555✔
1116
  p->pSeqTable = taosArrayInit(128, sizeof(SBlobValue));
50,555✔
1117
  if (p->pSeqTable == NULL) {
50,555✔
1118
    TAOS_CHECK_EXIT(terrno);
×
1119
  }
1120

1121
  p->data = taosMemCalloc(1024, cap * sizeof(uint8_t));
50,555✔
1122
  if (p->data == NULL) {
50,555✔
1123
    TAOS_CHECK_EXIT(terrno);
×
1124
  }
1125

1126
  p->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
50,555✔
1127
  if (p->pSeqToffset == NULL) {
50,555✔
1128
    TAOS_CHECK_EXIT(terrno);
×
1129
  }
1130

1131
  p->pSet = taosArrayInit(4, sizeof(int32_t));
50,555✔
1132
  if (p->pSet == NULL) {
50,555✔
1133
    TAOS_CHECK_EXIT(terrno);
×
1134
  }
1135

1136
  p->cap = cap;
50,555✔
1137
  p->len = 0;
50,555✔
1138
  p->seq = 1;
50,555✔
1139

1140
  *ppBlobSet = p;
50,555✔
1141
_exit:
50,555✔
1142
  if (code != 0) {
50,555✔
1143
    taosHashCleanup(p->pSeqToffset);
×
1144
    taosArrayDestroy(p->pSeqTable);
×
1145
    taosArrayDestroy(p->pSet);
×
1146

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

1166
  uint64_t tlen = pBlobSet->len + len;
27,541,986✔
1167
  if (tlen > pBlobSet->cap) {
27,541,986✔
1168
    int64_t cap = pBlobSet->cap;
22,966✔
1169
    // opt later
1170
    do {
1171
      cap = cap * 2;
22,966✔
1172
    } while (tlen > cap);
22,966✔
1173

1174
    uint8_t *data = taosMemRealloc(pBlobSet->data, cap);
22,966✔
1175
    if (data == NULL) {
22,966✔
1176
      return terrno;
×
1177
    }
1178
    pBlobSet->data = data;
22,966✔
1179
    pBlobSet->cap = cap;
22,966✔
1180
  }
1181
  if (len > 0) {
27,541,986✔
1182
    (void)memcpy(pBlobSet->data + pBlobSet->len, data, len);
24,799,582✔
1183
  }
1184

1185
  offset = pBlobSet->len;
27,541,986✔
1186
  pBlobSet->len += len;
27,541,986✔
1187

1188
  pBlobSet->seq++;
27,541,986✔
1189
  *seq = pBlobSet->seq;
27,541,986✔
1190

1191
  SBlobValue value = {.offset = offset, .len = len, .dataOffset = dataOffset, .nextRow = nextRow, .type = pItem->type};
27,541,986✔
1192
  if (taosArrayPush(pBlobSet->pSeqTable, &value) == NULL) {
55,083,972✔
1193
    TAOS_CHECK_EXIT(terrno);
×
1194
  }
1195

1196
  int32_t sz = taosArrayGetSize(pBlobSet->pSeqTable);
27,541,986✔
1197
  code = taosHashPut(pBlobSet->pSeqToffset, seq, sizeof(int64_t), &sz, sizeof(int32_t));
27,541,986✔
1198
  if (code != 0) {
27,541,986✔
1199
    TAOS_CHECK_EXIT(code);
×
1200
  }
1201

1202
_exit:
27,541,986✔
1203
  return code;
27,541,986✔
1204
}
1205

1206
void tBlobSetSwap(SBlobSet *p1, SBlobSet *p2) {
826✔
1207
  SBlobSet t = {0};
826✔
1208

1209
  memcpy(&t, p1, sizeof(SBlobSet));
826✔
1210

1211
  memcpy(p1, p2, sizeof(SBlobSet));
826✔
1212
  memcpy(p2, &t, sizeof(SBlobSet));
826✔
1213
}
826✔
1214

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

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

1235
  int32_t code = 0;
×
1236
  int32_t len = 0;
×
1237
  int32_t dataOffset = 0;
×
1238
  int8_t  nextRow = 0;
×
1239

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

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

1250
  len = value->len;
×
1251
  dataOffset = value->dataOffset;
×
1252
  nextRow = value->nextRow;
×
1253

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

1259
  return code;
×
1260
}
1261

1262
int32_t tBlobSetSize(SBlobSet *pBlobSet) {
1,605,079,361✔
1263
  if (pBlobSet == NULL) return 0;
1,605,079,361✔
1264
  return taosArrayGetSize(pBlobSet->pSeqTable);
117,890✔
1265
}
1266

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

1287
static int32_t tBindInfoCompare(const void *p1, const void *p2, const void *param) {
3,737✔
1288
  if (((SBindInfo *)p1)->columnId < ((SBindInfo *)p2)->columnId) {
3,737✔
1289
    return -1;
707✔
1290
  } else if (((SBindInfo *)p1)->columnId > ((SBindInfo *)p2)->columnId) {
3,030✔
1291
    return 1;
3,030✔
1292
  }
1293
  return 0;
×
1294
}
1295

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

1311
  if (!infoSorted) {
1,911,373✔
1312
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1313
  }
1314

1315
  int32_t code = 0;
1,911,373✔
1316
  int32_t numOfRows = infos[0].bind->num;
1,911,373✔
1317
  SArray *colValArray;
1318
  SColVal colVal;
1,885,855✔
1319

1320
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
1,911,863✔
1321
    return terrno;
×
1322
  }
1323

1324
  SRowKey rowKey, lastRowKey;
1,885,421✔
1325
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
19,671,654✔
1326
    taosArrayClear(colValArray);
18,011,793✔
1327

1328
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
93,362,693✔
1329
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
75,255,239✔
1330
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
1,296✔
1331
      } else {
1332
        SValue value = {
75,278,064✔
1333
            .type = infos[iInfo].type,
75,283,501✔
1334
        };
1335
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
75,302,691✔
1336
          value.nData = infos[iInfo].bind->length[iRow];
4,678,398✔
1337
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
4,731,990✔
1338
            code = TSDB_CODE_INVALID_PARA;
×
1339
            goto _exit;
×
1340
          }
1341
          value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
4,732,331✔
1342
        } else {
1343
          valueSetDatum(&value, infos[iInfo].type,
70,679,761✔
1344
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
70,658,864✔
1345
                        infos[iInfo].bind->buffer_length);
70,651,876✔
1346
        }
1347
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
76,579,655✔
1348
      }
1349
      if (taosArrayPush(colValArray, &colVal) == NULL) {
75,407,298✔
1350
        code = terrno;
×
1351
        goto _exit;
×
1352
      }
1353
    }
1354

1355
    SRow             *row;
3,824,319✔
1356
    SRowBuildScanInfo sinfo = {0};
18,098,231✔
1357
    if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
18,108,313✔
1358
      goto _exit;
×
1359
    }
1360

1361
    if ((taosArrayPush(rowArray, &row)) == NULL) {
17,750,240✔
1362
      code = terrno;
×
1363
      goto _exit;
×
1364
    }
1365

1366
    if (pOrdered && pDupTs) {
17,750,240✔
1367
      tRowGetKey(row, &rowKey);
35,695,252✔
1368
      if (iRow == 0) {
17,848,208✔
1369
        *pOrdered = true;
1,908,214✔
1370
        *pDupTs = false;
1,908,214✔
1371
      } else {
1372
        if (*pOrdered) {
15,939,994✔
1373
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
15,964,546✔
1374
          *pOrdered = (res >= 0);
15,964,546✔
1375
          if (!*pDupTs) {
15,959,847✔
1376
            *pDupTs = (res == 0);
15,926,995✔
1377
          }
1378
        }
1379
      }
1380
      lastRowKey = rowKey;
17,845,177✔
1381
    }
1382
  }
1383

1384
_exit:
1,888,796✔
1385
  taosArrayDestroy(colValArray);
1,888,801✔
1386
  return code;
1,910,844✔
1387
}
1388

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

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

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

1403
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
1404
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
20,382,695✔
1405
    return 0;
20,383,166✔
1406
  }
1407

1408
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
1409
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
563,031,674✔
1410
    return 0;
563,132,758✔
1411
  }
1412

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

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

1423
    if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
1424
      pv = pIdx->idx + pIdx->nCol;
2,147,483,647✔
1425
    } else if (pRow->flag & KV_FLG_MID) {
2,147,483,647✔
1426
      pv = pIdx->idx + (pIdx->nCol << 1);
2,147,483,647✔
1427
    } else {
1428
      pv = pIdx->idx + (pIdx->nCol << 2);
7,373✔
1429
    }
1430

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

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

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

1455
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
1456
            int8_t isBlob = IS_STR_DATA_BLOB(pTColumn->type) ? 1 : 0;
2,147,483,647✔
1457

1458
            pData += tGetU32v(pData, &pColVal->value.nData);
2,147,483,647✔
1459
            if (pColVal->value.nData > 0) {
2,147,483,647✔
1460
              pColVal->value.pData = pData;
2,147,483,647✔
1461
            } else {
1462
              pColVal->value.pData = NULL;
12,163,810✔
1463
            }
1464
            if (isBlob == 1) {
2,147,483,647✔
1465
              pData += BSE_SEQUECE_SIZE;  // skip seq
33,605,802✔
1466
            }
1467
          } else {
1468
            valueSetDatum(&pColVal->value, pTColumn->type, pData, pTColumn->bytes);
2,147,483,647✔
1469
          }
1470
        }
1471
        return 0;
2,147,483,647✔
1472
      } else if (TABS(cid) < pTColumn->colId) {
2,147,483,647✔
1473
        lidx = mid + 1;
2,147,483,647✔
1474
      } else {
1475
        ridx = mid - 1;
2,147,483,647✔
1476
      }
1477
    }
1478

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

1486
    if (pRow->flag == HAS_VALUE) {
2,147,483,647✔
1487
      fixed = bitmap;
2,147,483,647✔
1488
      bit = BIT_FLG_VALUE;
2,147,483,647✔
1489
    } else if (pRow->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
2,147,483,647✔
1490
      fixed = BIT2_SIZE(pTSchema->numOfCols - 1) + bitmap;
×
1491
      bit = GET_BIT2(bitmap, iCol - 1);
×
1492
    } else {
1493
      fixed = BIT1_SIZE(pTSchema->numOfCols - 1) + bitmap;
2,147,483,647✔
1494
      bit = GET_BIT1(bitmap, iCol - 1);
2,147,483,647✔
1495

1496
      if (pRow->flag == (HAS_NONE | HAS_VALUE)) {
2,147,483,647✔
1497
        if (bit) bit++;
11,508,822✔
1498
      } else if (pRow->flag == (HAS_NULL | HAS_VALUE)) {
2,147,483,647✔
1499
        bit++;
2,147,483,647✔
1500
      }
1501
    }
1502
    varlen = fixed + pTSchema->flen;
2,147,483,647✔
1503

1504
    if (bit == BIT_FLG_NONE) {
2,147,483,647✔
1505
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,779,250✔
1506
      return 0;
1,779,250✔
1507
    } else if (bit == BIT_FLG_NULL) {
2,147,483,647✔
1508
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,953,309,724✔
1509
      return 0;
1,953,314,100✔
1510
    }
1511

1512
    pColVal->cid = pTColumn->colId;
2,147,483,647✔
1513
    pColVal->value.type = pTColumn->type;
2,147,483,647✔
1514
    pColVal->flag = CV_FLAG_VALUE;
2,147,483,647✔
1515
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
1516
      int8_t isBlob = IS_STR_DATA_BLOB(pTColumn->type) ? 1 : 0;
2,147,483,647✔
1517
      pColVal->value.pData = varlen + *(int32_t *)(fixed + pTColumn->offset);
2,147,483,647✔
1518
      pColVal->value.pData += tGetU32v(pColVal->value.pData, &pColVal->value.nData);
2,147,483,647✔
1519
      // TODO(yhDeng): support tuple
1520
      //  if (isBlob) pColVal->value.pData += sizeof(uint64_t);  // skip seq
1521
      //  }
1522
    } else {
1523
      valueSetDatum(&pColVal->value, pTColumn->type, fixed + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
2,147,483,647✔
1524
    }
1525
  }
1526

1527
  return 0;
2,147,483,647✔
1528
}
1529

1530
void tRowDestroy(SRow *pRow) {
2,147,483,647✔
1531
  if (pRow) taosMemoryFree(pRow);
2,147,483,647✔
1532
}
2,147,483,647✔
1533

1534
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
351,750,702✔
1535
  SRowKey key1, key2;
351,727,935✔
1536
  tRowGetKey(*(SRow **)p1, &key1);
703,945,894✔
1537
  tRowGetKey(*(SRow **)p2, &key2);
704,308,946✔
1538
  return tRowKeyCompare(&key1, &key2);
352,877,550✔
1539
}
1540
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
42,593,104✔
1541

1542
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
92,983,170✔
1543
  SColVal* pColVal = tRowIterNext(iter);
92,983,170✔
1544
  while (pColVal != NULL && pColVal->cid < targetCid) {
120,904,837✔
1545
    pColVal = tRowIterNext(iter);
27,919,082✔
1546
  }
1547
  return pColVal;
92,985,833✔
1548
}
1549

1550
static int32_t tRowMergeImpl(SArray *aRowP, STSchema *pTSchema, int32_t iStart, int32_t iEnd, ERowMergeStrategy strategy) {
5,601,310✔
1551
  int32_t code = 0;
5,601,310✔
1552

1553
  int32_t    nRow = iEnd - iStart;
5,601,310✔
1554
  SRowIter **aIter = NULL;
5,601,310✔
1555
  SArray    *aColVal = NULL;
5,601,310✔
1556
  SRow      *pRow = NULL;
5,601,310✔
1557
  uint8_t    hasBlob = 0;
5,601,388✔
1558
  aIter = taosMemoryCalloc(nRow, sizeof(SRowIter *));
5,601,388✔
1559
  if (aIter == NULL) {
5,601,300✔
1560
    code = terrno;
×
1561
    goto _exit;
×
1562
  }
1563

1564
  for (int32_t i = 0; i < nRow; i++) {
48,195,290✔
1565
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
42,593,702✔
1566

1567
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
42,593,494✔
1568
    if (code) goto _exit;
42,593,990✔
1569
  }
1570

1571
  // merge
1572
  aColVal = taosArrayInit(pTSchema->numOfCols, sizeof(SColVal));
5,601,588✔
1573
  if (aColVal == NULL) {
5,600,978✔
1574
    code = terrno;
×
1575
    goto _exit;
×
1576
  }
1577

1578
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
76,070,966✔
1579
    int32_t targetCid = pTSchema->columns[iCol].colId;
70,468,676✔
1580
    SColVal *pColVal = NULL;
70,468,648✔
1581

1582
    switch (strategy) {
70,468,648✔
1583
      case KEEP_CONSISTENCY:
4,849,972✔
1584
        if (nRow > 0)
4,849,972✔
1585
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
4,849,972✔
1586
        break;
4,849,972✔
1587

1588
      default:  // default using PREFER_NON_NULL strategy
65,618,676✔
1589
      case PREFER_NON_NULL:
1590
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
107,498,382✔
1591
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
88,133,565✔
1592

1593
          if (COL_VAL_IS_VALUE(pColValT)) {
88,135,513✔
1594
            pColVal = pColValT;
46,255,879✔
1595
            break;
46,255,879✔
1596
          } else if (pColVal == NULL) {
41,879,706✔
1597
            pColVal = pColValT;
21,214,409✔
1598
          }
1599
        }
1600
        break;
65,620,696✔
1601
    }
1602

1603
    if (pColVal) {
70,470,668✔
1604
      if (taosArrayPush(aColVal, pColVal) == NULL) {
70,469,426✔
1605
        code = terrno;
×
1606
        goto _exit;
×
1607
      }
1608
    }
1609
  }
1610

1611
  // build
1612
  SRowBuildScanInfo sinfo = {0};
5,599,168✔
1613
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
5,601,310✔
1614

1615
  if (code) goto _exit;
5,601,038✔
1616

1617
  taosArrayRemoveBatch(aRowP, iStart, nRow, (FDelete)tRowPDestroy);
5,601,038✔
1618
  if (taosArrayInsert(aRowP, iStart, &pRow) == NULL) {
5,601,456✔
1619
    code = terrno;
×
1620
    goto _exit;
×
1621
  }
1622

1623
_exit:
5,601,446✔
1624
  if (aIter) {
5,601,456✔
1625
    for (int32_t i = 0; i < nRow; i++) {
48,195,786✔
1626
      tRowIterClose(&aIter[i]);
42,594,364✔
1627
    }
1628
    taosMemoryFree(aIter);
5,601,422✔
1629
  }
1630
  if (aColVal) taosArrayDestroy(aColVal);
5,601,390✔
1631
  if (code) tRowDestroy(pRow);
5,601,348✔
1632
  return code;
5,601,348✔
1633
}
1634
static int32_t tBlobSetTransferTo(SBlobSet *pSrc, SBlobSet *pDst, SColVal *pVal) {
8,260✔
1635
  int32_t code = 0;
8,260✔
1636
  int32_t lino = 0;
8,260✔
1637
  if (COL_VAL_IS_NULL(pVal) || pVal->value.pData == NULL) {
16,520✔
1638
    int8_t type = COL_VAL_IS_NULL(pVal) ? TSDB_DATA_BLOB_NULL_VALUE : TSDB_DATA_BLOB_EMPTY_VALUE;
8,260✔
1639
    code = addEmptyItemToBlobSet(pDst, type, NULL);
8,260✔
1640
    TAOS_CHECK_GOTO(code, &lino, _error);
8,260✔
1641
  } else {
1642
    uint64_t seq = 0;
×
1643
    if (tGetU64(pVal->value.pData, &seq) < 0) {
×
1644
      uError("tBlobSetTransferTo: invalid blob value, seq %p", pVal->value.pData);
×
1645
      return TSDB_CODE_INVALID_PARA;
×
1646
    }
1647

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

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

1660
_error:
8,260✔
1661
  return code;
8,260✔
1662
}
1663

1664
static int32_t tRowRebuildBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlob) {
826✔
1665
  int32_t code = 0;
826✔
1666
  int32_t   nRow = TARRAY_SIZE(aRowP);
826✔
1667
  int32_t   lino = 0;
826✔
1668
  SBlobSet *pTempBlob = NULL;
826✔
1669

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

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

1680
  for (int32_t i = 0; i < nRow; i++) {
9,086✔
1681
    SRow *pRowT = taosArrayGetP(aRowP, 0 + i);
8,260✔
1682
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
8,260✔
1683
    TAOS_CHECK_GOTO(code, &lino, _error);
8,260✔
1684
  }
1685

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

1701
  code = tBlobSetCreate(pBlob->cap, pBlob->type, &pTempBlob);
826✔
1702
  TAOS_CHECK_GOTO(code, &lino, _error);
826✔
1703

1704
  for (int32_t i = 0; i < taosArrayGetSize(aColVal); i++) {
9,086✔
1705
    uint64_t seq = 0;
8,260✔
1706
    SColVal *pVal = taosArrayGet(aColVal, i);
8,260✔
1707

1708
    code = tBlobSetTransferTo(pBlob, pTempBlob, pVal);
8,260✔
1709
    TAOS_CHECK_GOTO(code, &lino, _error);
8,260✔
1710
  }
1711

1712
  tBlobSetSwap(pBlob, pTempBlob);
826✔
1713

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

1719
  if (aIter) {
826✔
1720
    for (int32_t i = 0; i < nRow; i++) {
9,086✔
1721
      tRowIterClose(&aIter[i]);
8,260✔
1722
    }
1723
    taosMemoryFree(aIter);
826✔
1724
  }
1725
  if (aColVal) {
826✔
1726
    taosArrayDestroy(aColVal);
826✔
1727
  }
1728
  tBlobSetDestroy(pTempBlob);
826✔
1729
  return code;
826✔
1730
}
1731

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

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

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

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

1757
    tRowGetKey(row1, &key1);
×
1758

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

1765
      if (tRowKeyCompare(&key1, &key2) != 0) break;
×
1766

1767
      iEnd++;
×
1768
    }
1769

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

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

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

1790
  tBlobSetDestroy(pTempBlobSet);
×
1791
  return code;
×
1792
}
1793

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

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

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

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

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

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

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

1850
  // build
1851

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

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

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

1872
  return code;
×
1873
}
1874

1875
int32_t tRowSort(SArray *aRowP) {
1,683,402✔
1876
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
1,683,402✔
1877
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
1,677,016✔
1878
  if (code != TSDB_CODE_SUCCESS) {
1,677,016✔
1879
    uError("taosArrayMSort failed caused by %d", code);
×
1880
  }
1881
  return code;
1,677,016✔
1882
}
1883

1884
int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, ERowMergeStrategy strategy) {
1,833,429✔
1885
  int32_t code = 0;
1,833,429✔
1886

1887
  int32_t iStart = 0;
1,833,429✔
1888
  while (iStart < aRowP->size) {
24,464,606✔
1889
    SRowKey key1;
22,617,566✔
1890
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
22,631,121✔
1891

1892
    tRowGetKey(row1, &key1);
45,258,006✔
1893

1894
    int32_t iEnd = iStart + 1;
22,629,283✔
1895
    while (iEnd < aRowP->size) {
59,618,819✔
1896
      SRowKey key2;
57,773,924✔
1897
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
57,786,955✔
1898
      tRowGetKey(row2, &key2);
115,565,112✔
1899

1900
      if (tRowKeyCompare(&key1, &key2) != 0) break;
57,790,433✔
1901

1902
      iEnd++;
36,993,370✔
1903
    }
1904

1905
    if (iEnd - iStart > 1) {
22,631,391✔
1906
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
5,601,372✔
1907
      if (code) return code;
5,601,204✔
1908
    }
1909

1910
    // the array is also changing, so the iStart just ++ instead of iEnd
1911
    iStart++;
22,631,223✔
1912
  }
1913

1914
  return code;
1,833,724✔
1915
}
1916

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

1927
int8_t tRowNeedDoMerge(SArray *aRowP) {
826✔
1928
  if (aRowP == NULL || aRowP->size <= 1) return 0;
826✔
1929

1930
  int8_t  doMerge = 0;
826✔
1931
  int32_t iStart = 0;
826✔
1932
  while (iStart < aRowP->size) {
9,086✔
1933
    SRowKey key1;
8,260✔
1934
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
8,260✔
1935

1936
    tRowGetKey(row1, &key1);
16,520✔
1937

1938
    int32_t iEnd = iStart + 1;
8,260✔
1939
    while (iEnd < aRowP->size) {
8,260✔
1940
      SRowKey key2;
7,434✔
1941
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
7,434✔
1942
      tRowGetKey(row2, &key2);
14,868✔
1943

1944
      if (tRowKeyCompare(&key1, &key2) != 0) break;
7,434✔
1945

1946
      iEnd++;
×
1947
    }
1948

1949
    if (iEnd - iStart > 1) {
8,260✔
1950
      doMerge = 1;
×
1951
      break;
×
1952
    }
1953
    iStart++;
8,260✔
1954
  }
1955
  return doMerge;
826✔
1956
}
1957

1958
int32_t tRowMergeWithBlob(SArray *aRowP, STSchema *pTSchema, SBlobSet *pBlobSet, int8_t flag) {
826✔
1959
  int32_t code = 0;
826✔
1960

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

1968
    code = tRowMergeAndRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1969
  }
1970
  return code;
826✔
1971
}
1972

1973
// SRowIter ========================================
1974
struct SRowIter {
1975
  SRow     *pRow;
1976
  STSchema *pTSchema;
1977

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

1993
int32_t tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter) {
48,741,773✔
1994
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
48,741,773✔
1995

1996
  int32_t code = 0;
48,742,295✔
1997

1998
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
48,742,295✔
1999
  if (pIter == NULL) {
48,740,949✔
2000
    code = terrno;
×
2001
    goto _exit;
×
2002
  }
2003

2004
  pIter->pRow = pRow;
48,740,949✔
2005
  pIter->pTSchema = pTSchema;
48,740,877✔
2006
  pIter->iTColumn = 0;
48,741,063✔
2007

2008
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
48,741,131✔
2009

2010
  uint8_t         *data = pRow->data;
48,346,731✔
2011
  SPrimaryKeyIndex index;
48,339,631✔
2012
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
51,650,319✔
2013
    data += tGetPrimaryKeyIndex(data, &index);
3,303,590✔
2014
  }
2015

2016
  if (pRow->flag >> 4) {
48,346,893✔
2017
    pIter->iCol = 0;
8,190,902✔
2018
    pIter->pIdx = (SKVIdx *)data;
8,190,902✔
2019
    if (pRow->flag & KV_FLG_LIT) {
8,190,902✔
2020
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
8,190,902✔
2021
    } else if (pRow->flag & KV_FLG_MID) {
×
2022
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
×
2023
    } else {
2024
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
2025
    }
2026
  } else {
2027
    switch (pRow->flag) {
40,156,041✔
2028
      case (HAS_NULL | HAS_NONE):
3,122✔
2029
        pIter->pb = data;
3,122✔
2030
        break;
3,122✔
2031
      case HAS_VALUE:
37,141,489✔
2032
        pIter->pf = data;
37,141,489✔
2033
        pIter->pv = pIter->pf + pTSchema->flen;
37,141,559✔
2034
        break;
37,142,092✔
2035
      case (HAS_VALUE | HAS_NONE):
3,011,825✔
2036
      case (HAS_VALUE | HAS_NULL):
2037
        pIter->pb = data;
3,011,825✔
2038
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
3,011,825✔
2039
        pIter->pv = pIter->pf + pTSchema->flen;
3,011,825✔
2040
        break;
3,011,825✔
2041
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2042
        pIter->pb = data;
×
2043
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2044
        pIter->pv = pIter->pf + pTSchema->flen;
×
2045
        break;
×
2046
      default:
×
2047
        break;
×
2048
    }
2049
  }
2050

2051
_exit:
48,742,467✔
2052
  if (code) {
48,742,257✔
2053
    *ppIter = NULL;
×
2054
  } else {
2055
    *ppIter = pIter;
48,742,257✔
2056
  }
2057
  return code;
48,742,217✔
2058
}
2059

2060
void tRowIterClose(SRowIter **ppIter) {
48,742,639✔
2061
  SRowIter *pIter = *ppIter;
48,742,639✔
2062
  if (pIter) {
48,742,677✔
2063
    taosMemoryFree(pIter);
48,742,701✔
2064
  }
2065
  *ppIter = NULL;
48,742,753✔
2066
}
48,742,795✔
2067

2068
SColVal *tRowIterNext(SRowIter *pIter) {
222,124,562✔
2069
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
222,124,562✔
2070
    return NULL;
4,838,855✔
2071
  }
2072

2073
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
217,288,942✔
2074

2075
  // timestamp
2076
  if (0 == pIter->iTColumn) {
217,289,934✔
2077
    pIter->cv.cid = pTColumn->colId;
15,528,533✔
2078
    pIter->cv.value.type = pTColumn->type;
15,528,467✔
2079
    pIter->cv.flag = CV_FLAG_VALUE;
15,529,252✔
2080
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
15,529,140✔
2081
    goto _exit;
15,529,232✔
2082
  }
2083

2084
  if (pIter->pRow->flag == HAS_NONE) {
201,761,947✔
2085
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
5,785,538✔
2086
    goto _exit;
5,785,538✔
2087
  }
2088

2089
  if (pIter->pRow->flag == HAS_NULL) {
195,975,934✔
2090
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
374,676✔
2091
    goto _exit;
374,676✔
2092
  }
2093

2094
  if (pIter->pRow->flag >> 4) {  // KV
195,602,462✔
2095
    if (pIter->iCol < pIter->pIdx->nCol) {
157,771,441✔
2096
      uint8_t *pData;
2097

2098
      if (pIter->pRow->flag & KV_FLG_LIT) {
71,301,546✔
2099
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
71,301,546✔
2100
      } else if (pIter->pRow->flag & KV_FLG_MID) {
×
2101
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
×
2102
      } else {
2103
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
×
2104
      }
2105

2106
      int16_t cid;
71,300,835✔
2107
      pData += tGetI16v(pData, &cid);
71,301,546✔
2108

2109
      if (TABS(cid) == pTColumn->colId) {
71,301,546✔
2110
        if (cid < 0) {
69,902,079✔
2111
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
3,617,279✔
2112
        } else {
2113
          pIter->cv.cid = pTColumn->colId;
66,284,800✔
2114
          pIter->cv.value.type = pTColumn->type;
66,284,800✔
2115
          pIter->cv.flag = CV_FLAG_VALUE;
66,285,511✔
2116

2117
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
66,285,511✔
2118
            pData += tGetU32v(pData, &pIter->cv.value.nData);
25,722,139✔
2119
            if (pIter->cv.value.nData > 0) {
12,861,425✔
2120
              pIter->cv.value.pData = pData;
12,859,925✔
2121
            } else {
2122
              pIter->cv.value.pData = NULL;
1,500✔
2123
              // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2124
              //   pIter->cv.value.pData = pData;
2125
              // }
2126
            }
2127
          } else {
2128
            valueSetDatum(&pIter->cv.value, pTColumn->type, pData, pTColumn->bytes);
53,424,086✔
2129
          }
2130
        }
2131

2132
        pIter->iCol++;
69,902,079✔
2133
        goto _exit;
69,902,079✔
2134
      } else if (TABS(cid) > pTColumn->colId) {
1,398,756✔
2135
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,398,756✔
2136
        goto _exit;
1,398,756✔
2137
      } else {
2138
        uError("unexpected column id %d, %d", cid, pTColumn->colId);
×
2139
        goto _exit;
×
2140
      }
2141
    } else {
2142
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
86,470,606✔
2143
      goto _exit;
86,470,606✔
2144
    }
2145
  } else {  // Tuple
2146
    uint8_t bv = BIT_FLG_VALUE;
37,830,377✔
2147
    if (pIter->pb) {
37,830,377✔
2148
      switch (pIter->pRow->flag) {
15,948,254✔
2149
        case (HAS_NULL | HAS_NONE):
28,270✔
2150
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
28,270✔
2151
          break;
28,270✔
2152
        case (HAS_VALUE | HAS_NONE):
9,705,829✔
2153
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
9,705,829✔
2154
          if (bv) bv++;
9,705,829✔
2155
          break;
9,705,829✔
2156
        case (HAS_VALUE | HAS_NULL):
6,214,155✔
2157
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
6,214,155✔
2158
          break;
6,214,155✔
2159
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2160
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
2161
          break;
×
2162
        default:
×
2163
          break;
×
2164
      }
2165

2166
      if (bv == BIT_FLG_NONE) {
15,948,254✔
2167
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
998,832✔
2168
        goto _exit;
998,832✔
2169
      } else if (bv == BIT_FLG_NULL) {
14,949,422✔
2170
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,095,073✔
2171
        goto _exit;
1,095,073✔
2172
      }
2173
    }
2174

2175
    pIter->cv.cid = pTColumn->colId;
35,736,412✔
2176
    pIter->cv.value.type = pTColumn->type;
35,736,474✔
2177
    pIter->cv.flag = CV_FLAG_VALUE;
35,737,410✔
2178
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
40,057,311✔
2179
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
4,321,183✔
2180
      pData += tGetU32v(pData, &pIter->cv.value.nData);
8,639,922✔
2181
      if (pIter->cv.value.nData > 0) {
4,320,279✔
2182
        pIter->cv.value.pData = pData;
4,310,835✔
2183
      } else {
2184
        pIter->cv.value.pData = NULL;
9,534✔
2185
        // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2186
        //   pIter->cv.value.pData = pData;
2187
        // }
2188
      }
2189
    } else {
2190
      valueSetDatum(&pIter->cv.value, pTColumn->type, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
31,417,481✔
2191
    }
2192
    goto _exit;
35,736,316✔
2193
  }
2194

2195
_exit:
217,291,819✔
2196
  pIter->iTColumn++;
217,291,819✔
2197
  return &pIter->cv;
217,287,113✔
2198
}
2199

2200
static int32_t tRowNoneUpsertColData(SColData *aColData, int32_t nColData, int32_t flag) {
806,299✔
2201
  int32_t code = 0;
806,299✔
2202

2203
  if (flag) return code;
806,299✔
2204

2205
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
6,445,573✔
2206
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
5,919,048✔
2207
    if (code) return code;
5,919,048✔
2208
  }
2209

2210
  return code;
526,525✔
2211
}
2212
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
63,554,512✔
2213
  int32_t code = 0;
63,554,512✔
2214

2215
  int32_t   iColData = 0;
63,554,512✔
2216
  SColData *pColData = &aColData[iColData];
63,554,512✔
2217
  int32_t   iTColumn = 1;
63,554,938✔
2218
  STColumn *pTColumn = &pSchema->columns[iTColumn];
63,554,938✔
2219

2220
  while (pColData) {
419,147,468✔
2221
    if (pTColumn) {
355,610,605✔
2222
      if (pTColumn->colId == pColData->cid) {  // NULL
355,610,605✔
2223
        if (flag == 0) {
355,634,058✔
2224
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
355,634,058✔
2225
        } else {
2226
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2227
        }
2228
        if (code) goto _exit;
355,154,775✔
2229

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

2244
_exit:
63,536,863✔
2245
  return code;
63,536,863✔
2246
}
2247
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
2,147,483,647✔
2248
                                      int32_t flag) {
2249
  int32_t code = 0;
2,147,483,647✔
2250

2251
  int32_t   iColData = 0;
2,147,483,647✔
2252
  SColData *pColData = &aColData[iColData];
2,147,483,647✔
2253
  int32_t   iTColumn = 1;
2,147,483,647✔
2254
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
2,147,483,647✔
2255

2256
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
2,147,483,647✔
2257
  SPrimaryKeyIndex index;
2,147,483,647✔
2258
  uint8_t         *data = pRow->data;
2,147,483,647✔
2259
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
2,147,483,647✔
2260
    data += tGetPrimaryKeyIndex(data, &index);
68,977,375✔
2261
  }
2262

2263
  switch (pRow->flag) {
2,147,483,647✔
2264
    case HAS_VALUE:
2,147,483,647✔
2265
      pf = data;  // TODO: fix here
2,147,483,647✔
2266
      pv = pf + pTSchema->flen;
2,147,483,647✔
2267
      break;
2,147,483,647✔
2268
    case (HAS_NULL | HAS_NONE):
188,830✔
2269
      pb = data;
188,830✔
2270
      break;
188,830✔
2271
    case (HAS_VALUE | HAS_NONE):
456,963,283✔
2272
    case (HAS_VALUE | HAS_NULL):
2273
      pb = data;
456,963,283✔
2274
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
456,963,283✔
2275
      pv = pf + pTSchema->flen;
456,952,811✔
2276
      break;
456,961,125✔
2277
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2278
      pb = data;
×
2279
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2280
      pv = pf + pTSchema->flen;
×
2281
      break;
×
2282
    default:
×
2283
      return TSDB_CODE_INVALID_DATA_FMT;
×
2284
  }
2285

2286
  while (pColData) {
2,147,483,647✔
2287
    if (pTColumn) {
2,147,483,647✔
2288
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2289
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647✔
2290
          return TSDB_CODE_INVALID_PARA;
×
2291
        }
2292
        if (pb) {
2,147,483,647✔
2293
          uint8_t bv;
2294
          switch (pRow->flag) {
1,953,523,257✔
2295
            case (HAS_NULL | HAS_NONE):
3,729,050✔
2296
              bv = GET_BIT1(pb, iTColumn - 1);
3,729,050✔
2297
              break;
3,729,050✔
2298
            case (HAS_VALUE | HAS_NONE):
65,822,400✔
2299
              bv = GET_BIT1(pb, iTColumn - 1);
65,822,400✔
2300
              if (bv) bv++;
65,839,095✔
2301
              break;
65,839,095✔
2302
            case (HAS_VALUE | HAS_NULL):
1,883,994,640✔
2303
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
1,883,994,640✔
2304
              break;
1,884,128,868✔
2305
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
36,000✔
2306
              bv = GET_BIT2(pb, iTColumn - 1);
36,000✔
2307
              break;
×
2308
            default:
×
2309
              return TSDB_CODE_INVALID_DATA_FMT;
×
2310
          }
2311

2312
          if (bv == BIT_FLG_NONE) {
1,953,697,013✔
2313
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
8,294,358✔
2314
              goto _exit;
×
2315
            goto _continue;
8,292,503✔
2316
          } else if (bv == BIT_FLG_NULL) {
1,945,402,655✔
2317
            if (flag == 0) {
546,690,300✔
2318
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
546,389,724✔
2319
            } else {
2320
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
300,576✔
2321
            }
2322
            if (code) goto _exit;
546,741,685✔
2323
            goto _continue;
546,741,685✔
2324
          }
2325
        }
2326

2327
        if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
2328
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
2,147,483,647✔
2329
          uint32_t nData;
2,147,483,647✔
2330
          pData += tGetU32v(pData, &nData);
2,147,483,647✔
2331
          if (flag == 0) {
2,147,483,647✔
2332
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2333
          } else {
2334
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
513,428,606✔
2335
          }
2336
          if (code) goto _exit;
2,147,483,647✔
2337
        } else {
2338
          if (flag == 0) {
2,147,483,647✔
2339
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
2340
                                                                          TYPE_BYTES[pColData->type]);
2,147,483,647✔
2341
          } else {
2342
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
2343
                                                                          TYPE_BYTES[pColData->type], flag > 0);
1,382,573,735✔
2344
          }
2345
          if (code) goto _exit;
2,147,483,647✔
2346
        }
2347

2348
      _continue:
2,147,483,647✔
2349
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2350
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2351
      } else if (pTColumn->colId > pColData->cid) {  // NONE
133,810,620✔
2352
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2353
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2354
      } else {
2355
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
133,910,076✔
2356
      }
2357
    } else {
2358
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
103,239,370✔
2359
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
103,239,370✔
2360
    }
2361
  }
2362

2363
_exit:
2,147,483,647✔
2364
  return code;
2,147,483,647✔
2365
}
2366
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
1,888,636,457✔
2367
  int32_t code = 0;
1,888,636,457✔
2368

2369
  uint8_t  *pv = NULL;
1,888,636,457✔
2370
  int32_t   iColData = 0;
1,888,636,457✔
2371
  SColData *pColData = &aColData[iColData];
1,888,636,457✔
2372
  int32_t   iTColumn = 1;
1,888,778,176✔
2373
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,888,778,176✔
2374
  int32_t   iCol = 0;
1,888,842,580✔
2375

2376
  // primary keys
2377
  uint8_t         *data = pRow->data;
1,888,842,580✔
2378
  SPrimaryKeyIndex index;
1,888,829,021✔
2379
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,894,744,922✔
2380
    data += tGetPrimaryKeyIndex(data, &index);
5,915,090✔
2381
  }
2382

2383
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,888,774,926✔
2384
  if (pRow->flag & KV_FLG_LIT) {
1,888,774,926✔
2385
    pv = pKVIdx->idx + pKVIdx->nCol;
1,877,073,108✔
2386
  } else if (pRow->flag & KV_FLG_MID) {
11,717,927✔
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
11,720,348✔
2388
  } else if (pRow->flag & KV_FLG_BIG) {
×
2389
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
2390
  } else {
2391
    return TSDB_CODE_INVALID_PARA;
×
2392
  }
2393

2394
  while (pColData) {
2,147,483,647✔
2395
    if (pTColumn) {
2,147,483,647✔
2396
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2397
        while (iCol < pKVIdx->nCol) {
2,147,483,647✔
2398
          uint8_t *pData;
2399
          if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
2400
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
2,147,483,647✔
2401
          } else if (pRow->flag & KV_FLG_MID) {
317,890,644✔
2402
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
318,426,039✔
2403
          } else if (pRow->flag & KV_FLG_BIG) {
×
2404
            pData = pv + ((uint32_t *)pKVIdx->idx)[iCol];
×
2405
          } else {
2406
            return TSDB_CODE_INVALID_DATA_FMT;
×
2407
          }
2408

2409
          int16_t cid;
2,147,483,647✔
2410
          pData += tGetI16v(pData, &cid);
2,147,483,647✔
2411

2412
          if (TABS(cid) == pTColumn->colId) {
2,147,483,647✔
2413
            if (cid < 0) {
2,147,483,647✔
2414
              if (flag == 0) {
2,147,483,647✔
2415
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2,147,483,647✔
2416
              } else {
2417
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
19,422✔
2418
              }
2419
              if (code) goto _exit;
2,147,483,647✔
2420
            } else {
2421
              uint32_t nData;
2,147,483,647✔
2422
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
2423
                pData += tGetU32v(pData, &nData);
1,028,609,637✔
2424
              } else {
2425
                nData = 0;
2,147,483,647✔
2426
              }
2427
              if (flag == 0) {
2,147,483,647✔
2428
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2429
              } else {
2430
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
5,953,058✔
2431
              }
2432
              if (code) goto _exit;
2,147,483,647✔
2433
            }
2434
            iCol++;
2,147,483,647✔
2435
            goto _continue;
2,147,483,647✔
2436
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
2,147,483,647✔
2437
            break;
2,147,483,647✔
2438
          } else {
2439
            iCol++;
4,731,000✔
2440
          }
2441
        }
2442

2443
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
2,147,483,647✔
2444

2445
      _continue:
2,147,483,647✔
2446
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
2,147,483,647✔
2447
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
2,147,483,647✔
2448
      } else if (pTColumn->colId > pColData->cid) {
4,731,000✔
2449
        if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
×
2450
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
×
2451
      } else {
2452
        pTColumn = (++iTColumn < pTSchema->numOfCols) ? &pTSchema->columns[iTColumn] : NULL;
4,731,000✔
2453
      }
2454
    } else {
2455
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
8,634✔
2456
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
8,634✔
2457
    }
2458
  }
2459

2460
_exit:
1,891,541,647✔
2461
  return code;
1,888,598,381✔
2462
}
2463
/* flag > 0: forward update
2464
 * flag == 0: append
2465
 * flag < 0: backward update
2466
 */
2467
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
2,147,483,647✔
2468
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
2469
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
2470

2471
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
2472
    return tRowNoneUpsertColData(aColData, nColData, flag);
806,299✔
2473
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2474
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
63,554,938✔
2475
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2476
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,888,711,828✔
2477
  } else {  // TUPLE row
2478
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
2,147,483,647✔
2479
  }
2480
}
2481

2482
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
2,068,669,346✔
2483
  key->numOfPKs = row->numOfPKs;
2,068,669,346✔
2484

2485
  if (key->numOfPKs == 0) {
2,069,109,240✔
2486
    return;
×
2487
  }
2488

2489
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
2,063,402,752✔
2490

2491
  uint8_t *data = row->data;
2,069,454,702✔
2492

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

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

2501
    uint8_t *tdata = data + indices[i].offset;
2,069,599,533✔
2502
    if (row->flag >> 4) {
2,069,543,693✔
2503
      tdata += tGetI16v(tdata, NULL);
1,442,171,811✔
2504
    }
2505

2506
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
2,069,571,185✔
2507
      key->pks[i].pData = tdata;
519,771,002✔
2508
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
1,039,952,801✔
2509
    } else {
2510
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
1,549,895,783✔
2511
    }
2512
  }
2513
}
2514

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

2526
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
1,304,898,232✔
2527
  switch (tv1->type) {
1,304,898,232✔
2528
    case TSDB_DATA_TYPE_BOOL:
×
2529
    case TSDB_DATA_TYPE_TINYINT:
2530
      T_COMPARE_SCALAR_VALUE(int8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2531
    case TSDB_DATA_TYPE_SMALLINT:
×
2532
      T_COMPARE_SCALAR_VALUE(int16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2533
    case TSDB_DATA_TYPE_INT:
548,835,642✔
2534
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
548,835,642✔
2535
    case TSDB_DATA_TYPE_BIGINT:
160,126,569✔
2536
    case TSDB_DATA_TYPE_TIMESTAMP:
2537
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
160,126,569✔
2538
    case TSDB_DATA_TYPE_FLOAT:
×
2539
      T_COMPARE_SCALAR_VALUE(float, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2540
    case TSDB_DATA_TYPE_DOUBLE:
×
2541
      T_COMPARE_SCALAR_VALUE(double, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2542
    case TSDB_DATA_TYPE_UTINYINT:
×
2543
      T_COMPARE_SCALAR_VALUE(uint8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2544
    case TSDB_DATA_TYPE_USMALLINT:
×
2545
      T_COMPARE_SCALAR_VALUE(uint16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2546
    case TSDB_DATA_TYPE_UINT:
159,860,196✔
2547
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
159,860,196✔
2548
    case TSDB_DATA_TYPE_UBIGINT:
159,842,812✔
2549
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
159,842,812✔
2550
    case TSDB_DATA_TYPE_GEOMETRY:
276,270,832✔
2551
    case TSDB_DATA_TYPE_BINARY: {
2552
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
276,270,832✔
2553
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
276,261,546✔
2554
    }
2555
    case TSDB_DATA_TYPE_NCHAR: {
×
2556
      int32_t ret = taosUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
2557
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2558
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2559
    }
2560
    case TSDB_DATA_TYPE_VARBINARY: {
×
2561
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2562
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2563
    }
2564
    default:
×
2565
      break;
×
2566
  }
2567

2568
  return 0;
×
2569
}
2570

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

2581
  if (key1->numOfPKs == key2->numOfPKs) {
2,147,483,647✔
2582
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
2,147,483,647✔
2583
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
1,305,017,012✔
2584
      if (ret) return ret;
1,304,848,329✔
2585
    }
2586
  } else if (key1->numOfPKs < key2->numOfPKs) {
5,874✔
2587
    return -1;
5,874✔
2588
  } else {
2589
    return 1;
×
2590
  }
2591

2592
  return 0;
2,147,483,647✔
2593
}
2594

2595
void tRowKeyAssign(SRowKey *pDst, SRowKey *pSrc) {
2,147,483,647✔
2596
  pDst->ts = pSrc->ts;
2,147,483,647✔
2597
  pDst->numOfPKs = pSrc->numOfPKs;
2,147,483,647✔
2598

2599
  if (pSrc->numOfPKs > 0) {
2,147,483,647✔
2600
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
1,500,077,911✔
2601
      SValue *pVal = &pDst->pks[i];
750,088,457✔
2602
      pVal->type = pSrc->pks[i].type;
750,090,556✔
2603

2604
      valueCloneDatum(pVal, pSrc->pks + i, pVal->type);
750,075,091✔
2605
    }
2606
  }
2607
}
2,147,483,647✔
2608

2609
// STag ========================================
2610
static int tTagValCmprFn(const void *p1, const void *p2) {
2,147,483,647✔
2611
  if (((STagVal *)p1)->cid < ((STagVal *)p2)->cid) {
2,147,483,647✔
2612
    return -1;
2,147,483,647✔
2613
  } else if (((STagVal *)p1)->cid > ((STagVal *)p2)->cid) {
2,147,483,647✔
2614
    return 1;
2,147,483,647✔
2615
  }
2616

2617
  return 0;
2,147,483,647✔
2618
}
2619
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
4,680,491✔
2620
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
4,680,491✔
2621
}
2622

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

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

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

2716
static int32_t tPutTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
391,618,201✔
2717
  int32_t n = 0;
391,618,201✔
2718

2719
  // key
2720
  if (isJson) {
391,618,201✔
2721
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
2,731,980✔
2722
  } else {
2723
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
780,497,559✔
2724
  }
2725

2726
  // type
2727
  n += tPutI8(p ? p + n : p, pTagVal->type);
391,611,338✔
2728

2729
  // value
2730
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
391,648,699✔
2731
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
333,794,609✔
2732
  } else {
2733
    p = p ? p + n : p;
224,747,228✔
2734
    n += tDataTypes[pTagVal->type].bytes;
224,749,188✔
2735
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
224,711,679✔
2736
  }
2737

2738
  return n;
391,588,377✔
2739
}
2740
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
2,147,483,647✔
2741
  int32_t n = 0;
2,147,483,647✔
2742

2743
  // key
2744
  if (isJson) {
2,147,483,647✔
2745
    n += tGetCStr(p + n, &pTagVal->pKey);
10,614,424✔
2746
  } else {
2747
    n += tGetI16v(p + n, &pTagVal->cid);
2,147,483,647✔
2748
  }
2749

2750
  // type
2751
  n += tGetI8(p + n, &pTagVal->type);
2,147,483,647✔
2752

2753
  // value
2754
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
2,147,483,647✔
2755
    n += tGetBinary(p + n, &pTagVal->pData, &pTagVal->nData);
2,147,483,647✔
2756
  } else {
2757
    (void)memcpy(&(pTagVal->i64), p + n, tDataTypes[pTagVal->type].bytes);
2,147,483,647✔
2758
    n += tDataTypes[pTagVal->type].bytes;
2,147,483,647✔
2759
  }
2760

2761
  return n;
2,147,483,647✔
2762
}
2763

2764
bool tTagIsJson(const void *pTag) { return (((const STag *)pTag)->flags & TD_TAG_JSON); }
11,640,000✔
2765

2766
bool tTagIsJsonNull(void *data) {
1,430,381✔
2767
  STag  *pTag = (STag *)data;
1,430,381✔
2768
  int8_t isJson = tTagIsJson(pTag);
1,430,381✔
2769
  if (!isJson) return false;
1,429,952✔
2770
  return ((STag *)data)->nTag == 0;
821,867✔
2771
}
2772

2773
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
81,316,980✔
2774
  int32_t  code = 0;
81,316,980✔
2775
  uint8_t *p = NULL;
81,316,980✔
2776
  int16_t  n = 0;
81,316,980✔
2777
  int16_t  nTag = taosArrayGetSize(pArray);
81,316,980✔
2778
  int32_t  szTag = 0;
81,320,559✔
2779
  int8_t   isLarge = 0;
81,320,559✔
2780

2781
  // sort
2782
  if (isJson) {
81,320,559✔
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
445,840✔
2784
  } else {
2785
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
80,874,719✔
2786
  }
2787

2788
  // get size
2789
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
277,087,516✔
2790
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
195,816,516✔
2791
  }
2792
  if (szTag <= INT8_MAX) {
81,271,000✔
2793
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
72,391,890✔
2794
  } else {
2795
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
8,879,110✔
2796
    isLarge = 1;
8,879,110✔
2797
  }
2798

2799
  // build tag
2800
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
81,271,000✔
2801
  if ((*ppTag) == NULL) {
81,316,737✔
2802
    code = terrno;
×
2803
    goto _err;
×
2804
  }
2805
  (*ppTag)->flags = 0;
81,316,607✔
2806
  if (isJson) {
81,328,712✔
2807
    (*ppTag)->flags |= TD_TAG_JSON;
445,840✔
2808
  }
2809
  if (isLarge) {
81,328,712✔
2810
    (*ppTag)->flags |= TD_TAG_LARGE;
8,887,420✔
2811
  }
2812
  (*ppTag)->len = szTag;
81,328,712✔
2813
  (*ppTag)->nTag = nTag;
81,317,557✔
2814
  (*ppTag)->ver = version;
81,313,610✔
2815

2816
  if (isLarge) {
81,265,441✔
2817
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
8,887,420✔
2818
  } else {
2819
    p = (uint8_t *)&(*ppTag)->idx[nTag];
72,378,021✔
2820
  }
2821
  n = 0;
81,310,828✔
2822
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
277,113,612✔
2823
    if (isLarge) {
195,786,794✔
2824
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
51,722,181✔
2825
    } else {
2826
      (*ppTag)->idx[iTag] = n;
144,064,613✔
2827
    }
2828
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
195,768,966✔
2829
  }
2830
#ifdef TD_DEBUG_PRINT_TAG
2831
  debugPrintSTag(*ppTag, __func__, __LINE__);
2832
#endif
2833

2834
  return code;
81,326,818✔
2835

2836
_err:
×
2837
  return code;
×
2838
}
2839

2840
void tTagFree(STag *pTag) {
1,181,858,408✔
2841
  if (pTag) taosMemoryFree(pTag);
1,181,858,408✔
2842
}
1,181,858,408✔
2843

2844
char *tTagValToData(const STagVal *value, bool isJson) {
914,995,481✔
2845
  if (!value) {
914,995,481✔
2846
    return NULL;
×
2847
  }
2848

2849
  char  *data = NULL;
914,995,481✔
2850
  int8_t typeBytes = 0;
914,995,481✔
2851
  if (isJson) {
914,995,481✔
2852
    typeBytes = CHAR_BYTES;
2,407,903✔
2853
  }
2854

2855
  if (IS_VAR_DATA_TYPE(value->type)) {
914,995,481✔
2856
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
325,967,751✔
2857
    if (data == NULL) {
325,730,355✔
2858
      return NULL;
×
2859
    }
2860

2861
    if (isJson) {
325,730,355✔
2862
      *data = value->type;
1,841,193✔
2863
    }
2864

2865
    varDataLen(data + typeBytes) = value->nData;
325,730,355✔
2866
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
325,792,034✔
2867
  } else {
2868
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
589,251,079✔
2869
  }
2870

2871
  return data;
915,097,945✔
2872
}
2873

2874
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
2,147,483,647✔
2875
  if (!pTag || !pTagVal) {
2,147,483,647✔
2876
    return false;
×
2877
  }
2878

2879
  int16_t  lidx = 0;
2,147,483,647✔
2880
  int16_t  ridx = pTag->nTag - 1;
2,147,483,647✔
2881
  int16_t  midx;
2882
  uint8_t *p;
2883
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
2,147,483,647✔
2884
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
2,147,483,647✔
2885
  int16_t  offset;
2886
  STagVal  tv;
2,147,483,647✔
2887
  int      c;
2888

2889
  if (isLarge) {
2,147,483,647✔
2890
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
739,924,580✔
2891
  } else {
2892
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,808,150,153✔
2893
  }
2894

2895
  pTagVal->type = TSDB_DATA_TYPE_NULL;
2,147,483,647✔
2896
  pTagVal->pData = NULL;
2,147,483,647✔
2897
  pTagVal->nData = 0;
2,147,483,647✔
2898
  while (lidx <= ridx) {
2,147,483,647✔
2899
    midx = (lidx + ridx) / 2;
2,147,483,647✔
2900
    if (isLarge) {
2,147,483,647✔
2901
      offset = ((int16_t *)pTag->idx)[midx];
2,147,483,647✔
2902
    } else {
2903
      offset = pTag->idx[midx];
2,147,483,647✔
2904
    }
2905

2906
    int32_t nt = tGetTagVal(p + offset, &tv, isJson);
2,147,483,647✔
2907
    if (isJson) {
2,147,483,647✔
2908
      c = tTagValJsonCmprFn(pTagVal, &tv);
4,112,489✔
2909
    } else {
2910
      c = tTagValCmprFn(pTagVal, &tv);
2,147,483,647✔
2911
    }
2912

2913
    if (c < 0) {
2,147,483,647✔
2914
      ridx = midx - 1;
2,147,483,647✔
2915
    } else if (c > 0) {
2,147,483,647✔
2916
      lidx = midx + 1;
2,147,483,647✔
2917
    } else {
2918
      (void)memcpy(pTagVal, &tv, sizeof(tv));
2,147,483,647✔
2919
      return true;
2,147,483,647✔
2920
    }
2921
  }
2922
  return false;
61,077,331✔
2923
}
2924

2925
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
299,585,307✔
2926
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
599,209,345✔
2927
}
2928

2929
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
2,031,796,253✔
2930

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

2938
  if (isLarge) {
996,075✔
2939
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
4,422✔
2940
  } else {
2941
    p = (uint8_t *)&pTag->idx[pTag->nTag];
991,653✔
2942
  }
2943

2944
  (*ppArray) = taosArrayInit(pTag->nTag + 1, sizeof(STagVal));
996,075✔
2945
  if (*ppArray == NULL) {
996,075✔
2946
    code = terrno;
×
2947
    goto _err;
×
2948
  }
2949

2950
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
2,534,540✔
2951
    if (isLarge) {
1,538,465✔
2952
      offset = ((int16_t *)pTag->idx)[iTag];
5,925✔
2953
    } else {
2954
      offset = pTag->idx[iTag];
1,532,540✔
2955
    }
2956
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,538,465✔
2957
    if (taosArrayPush(*ppArray, &tv) == NULL) {
3,076,930✔
2958
      code = terrno;
×
2959
      goto _err;
×
2960
    }
2961
  }
2962

2963
  return code;
996,075✔
2964

2965
_err:
×
2966
  return code;
×
2967
}
2968

2969

2970
void destroyTagVal(void *pTag) {
13,442,560✔
2971
  STagVal* pTagVal = (STagVal*)pTag;
13,442,560✔
2972
  if (pTagVal && IS_VAR_DATA_TYPE(pTagVal->type)) {
13,442,560✔
2973
    taosMemoryFree(pTagVal->pData);
5,864,784✔
2974
  }
2975
}
13,442,560✔
2976

2977
// STSchema ========================================
2978
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
1,228,151,818✔
2979
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
1,228,151,818✔
2980
  if (pTSchema == NULL) {
1,228,097,325✔
2981
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2982
    return NULL;
×
2983
  }
2984

2985
  pTSchema->numOfCols = numOfCols;
1,228,097,325✔
2986
  pTSchema->version = version;
1,228,110,108✔
2987

2988
  // timestamp column
2989
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
1,228,204,559✔
2990
    terrno = TSDB_CODE_INVALID_PARA;
×
2991
    taosMemoryFree(pTSchema);
×
2992
    return NULL;
×
2993
  }
2994
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
1,228,185,371✔
2995
    terrno = TSDB_CODE_INVALID_PARA;
×
2996
    taosMemoryFree(pTSchema);
×
2997
    return NULL;
×
2998
  }
2999
  pTSchema->columns[0].colId = aSchema[0].colId;
1,227,991,887✔
3000
  pTSchema->columns[0].type = aSchema[0].type;
1,228,388,994✔
3001
  pTSchema->columns[0].flags = aSchema[0].flags;
1,228,501,045✔
3002
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
1,228,495,083✔
3003
  pTSchema->columns[0].offset = -1;
1,228,491,677✔
3004

3005
  // other columns
3006
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
2,147,483,647✔
3007
    SSchema  *pSchema = &aSchema[iCol];
2,147,483,647✔
3008
    STColumn *pTColumn = &pTSchema->columns[iCol];
2,147,483,647✔
3009

3010
    pTColumn->colId = pSchema->colId;
2,147,483,647✔
3011
    pTColumn->type = pSchema->type;
2,147,483,647✔
3012
    pTColumn->flags = pSchema->flags;
2,147,483,647✔
3013
    pTColumn->offset = pTSchema->flen;
2,147,483,647✔
3014

3015
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
2,147,483,647✔
3016
      pTColumn->bytes = pSchema->bytes;
2,147,483,647✔
3017
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
2,147,483,647✔
3018
    } else {
3019
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
2,147,483,647✔
3020
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
2,147,483,647✔
3021
    }
3022

3023
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
2,147,483,647✔
3024
  }
3025

3026
#if 1  // todo : remove this
3027
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
1,228,680,252✔
3028
#endif
3029

3030
  return pTSchema;
1,228,575,500✔
3031
}
3032

3033
static int32_t tTColumnCompare(const void *p1, const void *p2) {
140,991,888✔
3034
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
140,991,888✔
3035
    return -1;
23,439,876✔
3036
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
117,560,436✔
3037
    return 1;
83,470,370✔
3038
  }
3039

3040
  return 0;
34,097,086✔
3041
}
3042

3043
const STColumn *tTSchemaSearchColumn(const STSchema *pTSchema, int16_t cid) {
34,097,086✔
3044
  STColumn tcol = {
34,097,086✔
3045
      .colId = cid,
3046
  };
3047

3048
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
34,097,788✔
3049
}
3050

3051
// SColData ========================================
3052
void tColDataDestroy(void *ph) {
1,571,437,524✔
3053
  if (ph) {
1,571,437,524✔
3054
    SColData *pColData = (SColData *)ph;
1,571,503,334✔
3055

3056
    tFree(pColData->pBitMap);
1,571,503,334✔
3057
    tFree(pColData->aOffset);
1,571,568,162✔
3058
    tFree(pColData->pData);
1,571,529,756✔
3059
  }
3060
}
1,571,484,345✔
3061

3062
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
1,654,376,150✔
3063
  pColData->cid = cid;
1,654,376,150✔
3064
  pColData->type = type;
1,654,493,761✔
3065
  pColData->cflag = cflag;
1,654,623,317✔
3066
  tColDataClear(pColData);
1,654,723,720✔
3067
}
1,654,615,261✔
3068

3069
void tColDataClear(SColData *pColData) {
2,147,483,647✔
3070
  pColData->numOfNone = 0;
2,147,483,647✔
3071
  pColData->numOfNull = 0;
2,147,483,647✔
3072
  pColData->numOfValue = 0;
2,147,483,647✔
3073
  pColData->nVal = 0;
2,147,483,647✔
3074
  pColData->flag = 0;
2,147,483,647✔
3075
  pColData->nData = 0;
2,147,483,647✔
3076
}
2,147,483,647✔
3077

3078
void tColDataDeepClear(SColData *pColData) {
45,227,339✔
3079
  pColData->pBitMap = NULL;
45,227,339✔
3080
  pColData->aOffset = NULL;
45,230,655✔
3081
  pColData->pData = NULL;
45,232,122✔
3082

3083
  tColDataClear(pColData);
45,230,858✔
3084
}
45,232,524✔
3085

3086
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, uint8_t *pData, uint32_t nData) {
3087
  int32_t  code = 0;
2,147,483,647✔
3088
  uint32_t cvtNData = sizeof(uint64_t);
2,147,483,647✔
3089
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3090
    // TODO
3091
    if (IS_STR_DATA_BLOB(pColData->type)) {
2,147,483,647✔
3092
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
77,829,847✔
3093
      if (code) goto _exit;
32,596,306✔
3094
      pColData->aOffset[pColData->nVal] = pColData->nData;
32,596,306✔
3095

3096
      if (nData) {
32,596,306✔
3097
        code = tRealloc(&pColData->pData, pColData->nData + BSE_SEQUECE_SIZE);
29,373,322✔
3098
        if (code) goto _exit;
29,373,322✔
3099
        (void)memcpy(pColData->pData + pColData->nData, pData, BSE_SEQUECE_SIZE);
29,373,322✔
3100
        pColData->nData += BSE_SEQUECE_SIZE;
29,373,322✔
3101
      } else {
3102
        // uint64_t zero = 0;
3103
        // (void)memcpy(pColData->pData + pColData->nData, &zero, BSE_SEQUECE_SIZE);
3104
        // pColData->nData += BSE_SEQUECE_SIZE;
3105
      }
3106

3107
    } else {
3108
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
2,147,483,647✔
3109
      if (code) goto _exit;
2,147,483,647✔
3110
      pColData->aOffset[pColData->nVal] = pColData->nData;
2,147,483,647✔
3111

3112
      if (nData) {
2,147,483,647✔
3113
        code = tRealloc(&pColData->pData, pColData->nData + nData);
2,147,483,647✔
3114
        if (code) goto _exit;
2,147,483,647✔
3115
        (void)memcpy(pColData->pData + pColData->nData, pData, nData);
2,147,483,647✔
3116
        pColData->nData += nData;
2,147,483,647✔
3117
      }
3118
    }
3119
  } else {
3120
    if (!(pColData->nData == tDataTypes[pColData->type].bytes * pColData->nVal)) {
2,147,483,647✔
3121
      return TSDB_CODE_INVALID_PARA;
×
3122
    }
3123
    code = tRealloc(&pColData->pData, pColData->nData + tDataTypes[pColData->type].bytes);
2,147,483,647✔
3124
    if (code) goto _exit;
2,147,483,647✔
3125
    if (pData) {
2,147,483,647✔
3126
      (void)memcpy(pColData->pData + pColData->nData, pData, TYPE_BYTES[pColData->type]);
2,147,483,647✔
3127
    } else {
3128
      memset(pColData->pData + pColData->nData, 0, TYPE_BYTES[pColData->type]);
1,628,115,484✔
3129
    }
3130
    pColData->nData += tDataTypes[pColData->type].bytes;
2,147,483,647✔
3131
  }
3132
  pColData->nVal++;
2,147,483,647✔
3133

3134
_exit:
2,147,483,647✔
3135
  return code;
2,147,483,647✔
3136
}
3137
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
698,828,455✔
3138
  pColData->flag = HAS_VALUE;
698,926,668✔
3139
  pColData->numOfValue++;
699,024,624✔
3140
  return tColDataPutValue(pColData, pData, nData);
698,974,528✔
3141
}
3142
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
18,231,905✔
3143
  pColData->flag = HAS_NONE;
18,231,905✔
3144
  pColData->numOfNone++;
18,232,616✔
3145
  pColData->nVal++;
18,231,914✔
3146
  return 0;
18,231,203✔
3147
}
3148
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
33,965,667✔
3149
  pColData->flag = HAS_NULL;
33,990,011✔
3150
  pColData->numOfNull++;
33,991,627✔
3151
  pColData->nVal++;
33,992,418✔
3152
  return 0;
33,992,481✔
3153
}
3154
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,006,630✔
3155
  int32_t code = 0;
1,006,630✔
3156

3157
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
1,006,630✔
3158
  code = tRealloc(&pColData->pBitMap, nBit);
1,006,630✔
3159
  if (code) return code;
1,006,630✔
3160

3161
  memset(pColData->pBitMap, 0, nBit);
1,006,630✔
3162
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
1,006,630✔
3163

3164
  pColData->flag |= HAS_VALUE;
1,006,630✔
3165
  pColData->numOfValue++;
1,006,630✔
3166

3167
  if (pColData->nVal) {
1,006,630✔
3168
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,006,630✔
3169
      if (IS_STR_DATA_BLOB(pColData->type)) {
37,175✔
3170
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3171
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3172
        if (code) return code;
×
3173
        memset(pColData->aOffset, 0, nOffset);
×
3174

3175
      } else {
3176
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
37,175✔
3177
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
37,175✔
3178
        if (code) return code;
37,175✔
3179
        memset(pColData->aOffset, 0, nOffset);
37,175✔
3180
      }
3181
    } else {
3182
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
969,455✔
3183
      code = tRealloc(&pColData->pData, pColData->nData);
969,455✔
3184
      if (code) return code;
969,455✔
3185
      memset(pColData->pData, 0, pColData->nData);
969,455✔
3186
    }
3187
  }
3188

3189
  return tColDataPutValue(pColData, pData, nData);
1,006,630✔
3190
}
3191
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3192
  pColData->nVal++;
2,147,483,647✔
3193
  pColData->numOfNone++;
2,147,483,647✔
3194
  return 0;
2,147,483,647✔
3195
}
3196
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
69,835✔
3197
  int32_t code = 0;
92,947✔
3198

3199
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
92,947✔
3200
  code = tRealloc(&pColData->pBitMap, nBit);
92,947✔
3201
  if (code) return code;
92,947✔
3202

3203
  memset(pColData->pBitMap, 0, nBit);
92,947✔
3204
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
92,947✔
3205

3206
  pColData->flag |= HAS_NULL;
92,947✔
3207
  pColData->numOfNull++;
92,947✔
3208
  pColData->nVal++;
92,947✔
3209

3210
  return code;
92,947✔
3211
}
3212
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,872,684✔
3213
  int32_t code = 0;
4,873,128✔
3214

3215
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
4,873,128✔
3216
  code = tRealloc(&pColData->pBitMap, nBit);
4,873,138✔
3217
  if (code) return code;
4,873,138✔
3218

3219
  memset(pColData->pBitMap, 0, nBit);
4,873,138✔
3220
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
4,873,138✔
3221

3222
  pColData->flag |= HAS_VALUE;
4,873,138✔
3223
  pColData->numOfValue++;
4,873,138✔
3224

3225
  if (pColData->nVal) {
4,873,138✔
3226
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,332,038✔
3227
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,458,900✔
3228
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,458,900✔
3229
      if (code) return code;
1,458,900✔
3230
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,458,900✔
3231
        memset(pColData->aOffset, 0, nOffset);
1,452,726✔
3232
      }
3233
    } else {
3234
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
3,414,238✔
3235
      code = tRealloc(&pColData->pData, pColData->nData);
3,414,238✔
3236
      if (code) return code;
3,414,238✔
3237
      memset(pColData->pData, 0, pColData->nData);
3,414,238✔
3238
    }
3239
  }
3240

3241
  return tColDataPutValue(pColData, pData, nData);
4,873,138✔
3242
}
3243
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
19,124✔
3244
  int32_t code = 0;
19,124✔
3245

3246
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
19,124✔
3247
  code = tRealloc(&pColData->pBitMap, nBit);
19,124✔
3248
  if (code) return code;
19,124✔
3249

3250
  memset(pColData->pBitMap, 255, nBit);
19,124✔
3251
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
19,124✔
3252

3253
  pColData->flag |= HAS_NONE;
19,124✔
3254
  pColData->numOfNone++;
19,124✔
3255
  pColData->nVal++;
19,124✔
3256

3257
  return code;
19,124✔
3258
}
3259
static FORCE_INLINE int32_t tColDataAppendValue22(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3260
  pColData->nVal++;
2,147,483,647✔
3261
  pColData->numOfNull++;
2,147,483,647✔
3262
  return 0;
2,147,483,647✔
3263
}
3264
static FORCE_INLINE int32_t tColDataAppendValue30(SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3265
  int32_t code = 0;
×
3266

3267
  pColData->flag |= HAS_VALUE;
×
3268
  pColData->numOfValue++;
×
3269

3270
  uint8_t *pBitMap = NULL;
×
3271
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3272
  if (code) return code;
×
3273

3274
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3275
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
3276
  }
3277
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
3278

3279
  tFree(pColData->pBitMap);
×
3280
  pColData->pBitMap = pBitMap;
×
3281

3282
  if (pColData->nVal) {
×
3283
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3284
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3285
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3286
      if (code) return code;
×
3287
      memset(pColData->aOffset, 0, nOffset);
×
3288
    } else {
3289
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
3290
      code = tRealloc(&pColData->pData, pColData->nData);
×
3291
      if (code) return code;
×
3292
      memset(pColData->pData, 0, pColData->nData);
×
3293
    }
3294
  }
3295

3296
  return tColDataPutValue(pColData, pData, nData);
×
3297
}
3298
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, uint8_t *pData, uint32_t nData) {
806,352✔
3299
  int32_t code = 0;
806,352✔
3300

3301
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
806,352✔
3302
  if (code) return code;
806,352✔
3303

3304
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
806,352✔
3305
  pColData->numOfNone++;
806,352✔
3306
  pColData->nVal++;
806,352✔
3307

3308
  return code;
806,352✔
3309
}
3310
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
148,944✔
3311
  int32_t code = 0;
148,944✔
3312

3313
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
148,944✔
3314
  if (code) return code;
148,944✔
3315

3316
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
148,944✔
3317
  pColData->numOfNull++;
148,944✔
3318
  pColData->nVal++;
148,944✔
3319

3320
  return code;
148,944✔
3321
}
3322
static FORCE_INLINE int32_t tColDataAppendValue40(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3323
  pColData->numOfValue++;
2,147,483,647✔
3324
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3325
}
3326
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, uint8_t *pData, uint32_t nData) {
9,116,870✔
3327
  int32_t code = 0;
9,116,870✔
3328

3329
  pColData->flag |= HAS_NONE;
9,116,870✔
3330
  pColData->numOfNone++;
9,116,870✔
3331

3332
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
9,116,870✔
3333
  code = tRealloc(&pColData->pBitMap, nBit);
9,116,870✔
3334
  if (code) return code;
9,112,604✔
3335

3336
  memset(pColData->pBitMap, 255, nBit);
9,112,604✔
3337
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
9,114,737✔
3338

3339
  return tColDataPutValue(pColData, NULL, 0);
9,116,939✔
3340
}
3341
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
23,293,638✔
3342
  int32_t code = 0;
23,933,936✔
3343

3344
  pColData->flag |= HAS_NULL;
23,933,936✔
3345
  pColData->numOfNull++;
23,933,936✔
3346

3347
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
23,933,936✔
3348
  code = tRealloc(&pColData->pBitMap, nBit);
23,933,936✔
3349
  if (code) return code;
23,933,455✔
3350

3351
  memset(pColData->pBitMap, 255, nBit);
23,933,455✔
3352
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
23,933,455✔
3353

3354
  return tColDataPutValue(pColData, NULL, 0);
23,933,562✔
3355
}
3356
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
353,781,004✔
3357
  int32_t code = 0;
353,781,656✔
3358

3359
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
353,781,656✔
3360
  if (code) return code;
353,780,599✔
3361

3362
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
353,780,599✔
3363
  pColData->numOfValue++;
353,793,230✔
3364

3365
  return tColDataPutValue(pColData, pData, nData);
353,784,976✔
3366
}
3367
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
878,746,305✔
3368
  int32_t code = 0;
878,746,305✔
3369

3370
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
878,746,305✔
3371
  if (code) return code;
878,753,279✔
3372

3373
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
878,753,279✔
3374
  pColData->numOfNone++;
878,762,184✔
3375

3376
  return tColDataPutValue(pColData, NULL, 0);
878,760,078✔
3377
}
3378
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
103,210✔
3379
  int32_t code = 0;
103,210✔
3380

3381
  pColData->flag |= HAS_NULL;
103,210✔
3382
  pColData->numOfNull++;
103,210✔
3383

3384
  uint8_t *pBitMap = NULL;
103,210✔
3385
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
103,210✔
3386
  if (code) return code;
103,210✔
3387

3388
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,496,138✔
3389
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
2,392,928✔
3390
  }
3391
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
103,210✔
3392

3393
  tFree(pColData->pBitMap);
103,210✔
3394
  pColData->pBitMap = pBitMap;
103,210✔
3395

3396
  return tColDataPutValue(pColData, NULL, 0);
103,210✔
3397
}
3398
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3399
  int32_t code = 0;
2,147,483,647✔
3400

3401
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
2,147,483,647✔
3402
  if (code) return code;
2,147,483,647✔
3403
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
2,147,483,647✔
3404
  pColData->numOfValue++;
2,147,483,647✔
3405

3406
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3407
}
3408
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,128,826✔
3409
  int32_t code = 0;
2,128,826✔
3410

3411
  pColData->flag |= HAS_NONE;
2,128,826✔
3412
  pColData->numOfNone++;
2,128,826✔
3413

3414
  uint8_t *pBitMap = NULL;
2,128,826✔
3415
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
2,128,826✔
3416
  if (code) return code;
2,128,826✔
3417

3418
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
183,467,285✔
3419
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
181,337,888✔
3420
  }
3421
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
2,128,826✔
3422

3423
  tFree(pColData->pBitMap);
2,128,826✔
3424
  pColData->pBitMap = pBitMap;
2,128,826✔
3425

3426
  return tColDataPutValue(pColData, NULL, 0);
2,128,826✔
3427
}
3428
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,427,062,636✔
3429
  int32_t code = 0;
1,428,408,511✔
3430

3431
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,428,408,511✔
3432
  if (code) return code;
1,428,540,545✔
3433
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,428,540,545✔
3434
  pColData->numOfNull++;
1,428,670,981✔
3435

3436
  return tColDataPutValue(pColData, NULL, 0);
1,428,607,915✔
3437
}
3438
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,248,432✔
3439
  int32_t code = 0;
3,366,560✔
3440

3441
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
3,366,560✔
3442
  if (code) return code;
3,366,560✔
3443
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
3,366,560✔
3444
  pColData->numOfValue++;
3,365,918✔
3445

3446
  return tColDataPutValue(pColData, pData, nData);
3,366,560✔
3447
}
3448
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
333,668,742✔
3449
  int32_t code = 0;
333,668,742✔
3450

3451
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
333,668,742✔
3452
  if (code) return code;
333,668,742✔
3453
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
333,668,742✔
3454
  pColData->numOfNone++;
333,668,742✔
3455

3456
  return tColDataPutValue(pColData, NULL, 0);
333,668,742✔
3457
}
3458
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
387,922✔
3459
  int32_t code = 0;
387,922✔
3460

3461
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
387,922✔
3462
  if (code) return code;
387,922✔
3463
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
387,922✔
3464
  pColData->numOfNull++;
387,922✔
3465

3466
  return tColDataPutValue(pColData, NULL, 0);
387,922✔
3467
}
3468
static int32_t (*tColDataAppendValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData) = {
3469
    {tColDataAppendValue00, tColDataAppendValue01, tColDataAppendValue02},  // 0
3470
    {tColDataAppendValue10, tColDataAppendValue11, tColDataAppendValue12},  // HAS_NONE
3471
    {tColDataAppendValue20, tColDataAppendValue21, tColDataAppendValue22},  // HAS_NULL
3472
    {tColDataAppendValue30, tColDataAppendValue31, tColDataAppendValue32},  // HAS_NULL|HAS_NONE
3473
    {tColDataAppendValue40, tColDataAppendValue41, tColDataAppendValue42},  // HAS_VALUE
3474
    {tColDataAppendValue50, tColDataAppendValue51, tColDataAppendValue52},  // HAS_VALUE|HAS_NONE
3475
    {tColDataAppendValue60, tColDataAppendValue61, tColDataAppendValue62},  // HAS_VALUE|HAS_NULL
3476
    {tColDataAppendValue70, tColDataAppendValue71, tColDataAppendValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
3477

3478
    //       VALUE                  NONE                     NULL
3479
};
3480

3481
static FORCE_INLINE int32_t tColDataPutValueBlob(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
3482
  int32_t  code = 0;
128✔
3483
  uint8_t  buf[sizeof(uint64_t) + 1] = {0};
×
3484
  uint64_t seq = 0;
128✔
3485

3486
  int32_t offset = 0;
128✔
3487
  if (IS_STR_DATA_BLOB(pColData->type)) {
128✔
3488
    code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
128✔
3489
    if (code) goto _exit;
128✔
3490
    pColData->aOffset[pColData->nVal] = pColData->nData;
128✔
3491
    if (nData) {
128✔
3492
      SBlobItem item = {.seqOffsetInRow = seq, .len = nData, .data = pData, .type = TSDB_DATA_BLOB_VALUE};
128✔
3493
      code = tBlobSetPush(pArg, &item, &seq, 0);
128✔
3494
      if (code != 0) return code;
128✔
3495

3496
      offset = tPutU64(buf, seq);
128✔
3497
      code = tRealloc(&pColData->pData, pColData->nData + offset);
128✔
3498
      if (code != 0) return code;
128✔
3499
      memcpy(pColData->pData + pColData->nData, buf, offset);
128✔
3500
      pColData->nData += offset;
128✔
3501
    } else {
3502
      int8_t type = pData ? TSDB_DATA_BLOB_EMPTY_VALUE : TSDB_DATA_BLOB_NULL_VALUE;
×
3503
      code = addEmptyItemToBlobSet(pArg, type, NULL);
×
3504
    }
3505
  }
3506
  pColData->nVal++;
128✔
3507

3508
_exit:
128✔
3509
  return code;
128✔
3510
}
3511

3512
static FORCE_INLINE int32_t tColDataAppendValueBlob00(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
86✔
3513
  pColData->flag = HAS_VALUE;
86✔
3514
  pColData->numOfValue++;
86✔
3515
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
86✔
3516
}
3517
static FORCE_INLINE int32_t tColDataAppendValueBlob01(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3518
  pColData->flag = HAS_NONE;
×
3519
  pColData->numOfNone++;
×
3520
  pColData->nVal++;
×
3521
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3522
}
3523
static FORCE_INLINE int32_t tColDataAppendValueBlob02(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
130✔
3524
  pColData->flag = HAS_NULL;
130✔
3525
  pColData->numOfNull++;
130✔
3526
  pColData->nVal++;
130✔
3527
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
130✔
3528
}
3529
static FORCE_INLINE int32_t tColDataAppendValueBlob10(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3530
  int32_t code = 0;
×
3531

3532
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3533
  code = tRealloc(&pColData->pBitMap, nBit);
×
3534
  if (code) return code;
×
3535

3536
  memset(pColData->pBitMap, 0, nBit);
×
3537
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3538

3539
  pColData->flag |= HAS_VALUE;
×
3540
  pColData->numOfValue++;
×
3541

3542
  if (pColData->nVal) {
×
3543
    if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3544
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3545
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3546
      if (code) return code;
×
3547
      memset(pColData->aOffset, 0, nOffset);
×
3548
    } else {
3549
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
×
3550
      code = tRealloc(&pColData->pData, pColData->nData);
×
3551
      if (code) return code;
×
3552
      memset(pColData->pData, 0, pColData->nData);
×
3553
    }
3554
  }
3555

3556
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3557
}
3558
static FORCE_INLINE int32_t tColDataAppendValueBlob11(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3559
  pColData->nVal++;
×
3560
  pColData->numOfNone++;
×
3561
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3562
}
3563
static FORCE_INLINE int32_t tColDataAppendValueBlob12(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3564
  int32_t code = 0;
×
3565

3566
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3567
  code = tRealloc(&pColData->pBitMap, nBit);
×
3568
  if (code) return code;
×
3569

3570
  memset(pColData->pBitMap, 0, nBit);
×
3571
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3572

3573
  pColData->flag |= HAS_NULL;
×
3574
  pColData->numOfNull++;
×
3575
  pColData->nVal++;
×
3576
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3577
}
3578
static FORCE_INLINE int32_t tColDataAppendValueBlob20(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3579
  int32_t code = 0;
×
3580

3581
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3582
  code = tRealloc(&pColData->pBitMap, nBit);
×
3583
  if (code) return code;
×
3584

3585
  memset(pColData->pBitMap, 0, nBit);
×
3586
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3587

3588
  pColData->flag |= HAS_VALUE;
×
3589
  pColData->numOfValue++;
×
3590

3591
  if (pColData->nVal) {
×
3592
    if (IS_STR_DATA_BLOB(pColData->type)) {
×
3593
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3594
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3595
      if (code) return code;
×
3596
      memset(pColData->aOffset, 0, nOffset);
×
3597
    } else {
3598
      return TSDB_CODE_INVALID_MSG;
×
3599
    }
3600
  }
3601

3602
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3603
}
3604
static FORCE_INLINE int32_t tColDataAppendValueBlob21(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3605
  int32_t code = 0;
×
3606

3607
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3608
  code = tRealloc(&pColData->pBitMap, nBit);
×
3609
  if (code) return code;
×
3610

3611
  memset(pColData->pBitMap, 255, nBit);
×
3612
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3613

3614
  pColData->flag |= HAS_NONE;
×
3615
  pColData->numOfNone++;
×
3616
  pColData->nVal++;
×
3617
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3618
}
3619
static FORCE_INLINE int32_t tColDataAppendValueBlob22(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3620
  pColData->nVal++;
×
3621
  pColData->numOfNull++;
×
3622
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3623
}
3624
static FORCE_INLINE int32_t tColDataAppendValueBlob30(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3625
  int32_t code = 0;
×
3626

3627
  pColData->flag |= HAS_VALUE;
×
3628
  pColData->numOfValue++;
×
3629

3630
  uint8_t *pBitMap = NULL;
×
3631
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3632
  if (code) return code;
×
3633

3634
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3635
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal));
×
3636
  }
3637
  SET_BIT2_EX(pBitMap, pColData->nVal, 2);
×
3638

3639
  tFree(pColData->pBitMap);
×
3640
  pColData->pBitMap = pBitMap;
×
3641

3642
  if (pColData->nVal) {
×
3643
    if (IS_STR_DATA_BLOB(pColData->type)) {
×
3644
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3645
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3646
      if (code) return code;
×
3647
      memset(pColData->aOffset, 0, nOffset);
×
3648
    } else {
3649
      return TSDB_CODE_INVALID_MSG;
×
3650
    }
3651
  }
3652

3653
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3654
}
3655
static FORCE_INLINE int32_t tColDataAppendValueBlob31(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3656
  int32_t code = 0;
×
3657

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

3661
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3662
  pColData->numOfNone++;
×
3663
  pColData->nVal++;
×
3664

3665
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3666
}
3667
static FORCE_INLINE int32_t tColDataAppendValueBlob32(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3668
  int32_t code = 0;
×
3669

3670
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3671
  if (code) return code;
×
3672

3673
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3674
  pColData->numOfNull++;
×
3675
  pColData->nVal++;
×
3676
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
×
3677
}
3678
static FORCE_INLINE int32_t tColDataAppendValueBlob40(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
42✔
3679
  pColData->numOfValue++;
42✔
3680
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
42✔
3681
}
3682
static FORCE_INLINE int32_t tColDataAppendValueBlob41(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3683
  int32_t code = 0;
×
3684

3685
  pColData->flag |= HAS_NONE;
×
3686
  pColData->numOfNone++;
×
3687

3688
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
×
3689
  code = tRealloc(&pColData->pBitMap, nBit);
×
3690
  if (code) return code;
×
3691

3692
  memset(pColData->pBitMap, 255, nBit);
×
3693
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3694

3695
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3696
}
3697
static FORCE_INLINE int32_t tColDataAppendValueBlob42(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3698
  int32_t code = 0;
×
3699

3700
  pColData->flag |= HAS_NULL;
×
3701
  pColData->numOfNull++;
×
3702

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

3707
  memset(pColData->pBitMap, 255, nBit);
×
3708
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3709

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

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

3718
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3719
  pColData->numOfValue++;
×
3720

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

3726
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3727
  if (code) return code;
×
3728

3729
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3730
  pColData->numOfNone++;
×
3731

3732
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3733
}
3734
static FORCE_INLINE int32_t tColDataAppendValueBlob52(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3735
  int32_t code = 0;
×
3736

3737
  pColData->flag |= HAS_NULL;
×
3738
  pColData->numOfNull++;
×
3739

3740
  uint8_t *pBitMap = NULL;
×
3741
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3742
  if (code) return code;
×
3743

3744
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3745
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
×
3746
  }
3747
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
×
3748

3749
  tFree(pColData->pBitMap);
×
3750
  pColData->pBitMap = pBitMap;
×
3751

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

3757
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3758
  if (code) return code;
×
3759
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3760
  pColData->numOfValue++;
×
3761

3762
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3763
}
3764
static FORCE_INLINE int32_t tColDataAppendValueBlob61(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3765
  int32_t code = 0;
×
3766

3767
  pColData->flag |= HAS_NONE;
×
3768
  pColData->numOfNone++;
×
3769

3770
  uint8_t *pBitMap = NULL;
×
3771
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3772
  if (code) return code;
×
3773

3774
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
3775
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
×
3776
  }
3777
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
×
3778

3779
  tFree(pColData->pBitMap);
×
3780
  pColData->pBitMap = pBitMap;
×
3781

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

3787
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
×
3788
  if (code) return code;
×
3789
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3790
  pColData->numOfNull++;
×
3791

3792
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3793
}
3794
static FORCE_INLINE int32_t tColDataAppendValueBlob70(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, 2);
×
3800
  pColData->numOfValue++;
×
3801

3802
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3803
}
3804
static FORCE_INLINE int32_t tColDataAppendValueBlob71(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, 0);
×
3810
  pColData->numOfNone++;
×
3811

3812
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3813
}
3814
static FORCE_INLINE int32_t tColDataAppendValueBlob72(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3815
  int32_t code = 0;
×
3816

3817
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3818
  if (code) return code;
×
3819
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
×
3820
  pColData->numOfNull++;
×
3821

3822
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3823
}
3824

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

3844
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,274✔
3845
  pColData->numOfNone--;
3,274✔
3846
  pColData->nVal--;
3,274✔
3847
  if (pColData->numOfNone) {
3,274✔
3848
    return tColDataAppendValue10(pColData, pData, nData);
×
3849
  } else {
3850
    pColData->flag = 0;
3,274✔
3851
    return tColDataAppendValue00(pColData, pData, nData);
3,274✔
3852
  }
3853
}
3854
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
26,538✔
3855
  pColData->numOfNone--;
26,538✔
3856
  pColData->nVal--;
26,538✔
3857
  if (pColData->numOfNone) {
26,538✔
3858
    return tColDataAppendValue12(pColData, pData, nData);
23,112✔
3859
  } else {
3860
    pColData->flag = 0;
3,426✔
3861
    return tColDataAppendValue02(pColData, pData, nData);
3,426✔
3862
  }
3863
  return 0;
3864
}
3865
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
95,383✔
3866
  if (forward) {
95,383✔
3867
    pColData->numOfNull--;
95,383✔
3868
    pColData->nVal--;
95,383✔
3869
    if (pColData->numOfNull) {
95,383✔
3870
      return tColDataAppendValue20(pColData, pData, nData);
444✔
3871
    } else {
3872
      pColData->flag = 0;
94,939✔
3873
      return tColDataAppendValue00(pColData, pData, nData);
94,939✔
3874
    }
3875
  }
3876
  return 0;
×
3877
}
3878
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3879
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3880
    pColData->numOfNone--;
×
3881
    pColData->nVal--;
×
3882
    if (pColData->numOfNone) {
×
3883
      return tColDataAppendValue30(pColData, pData, nData);
×
3884
    } else {
3885
      pColData->flag = HAS_NULL;
×
3886
      return tColDataAppendValue20(pColData, pData, nData);
×
3887
    }
3888
  } else if (forward) {  // NULL ==> VALUE
×
3889
    pColData->numOfNull--;
×
3890
    pColData->nVal--;
×
3891
    if (pColData->numOfNull) {
×
3892
      return tColDataAppendValue30(pColData, pData, nData);
×
3893
    } else {
3894
      pColData->flag = HAS_NONE;
×
3895
      return tColDataAppendValue10(pColData, pData, nData);
×
3896
    }
3897
  }
3898
  return 0;
×
3899
}
3900
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
334,988✔
3901
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
334,988✔
3902
    pColData->numOfNone--;
334,988✔
3903
    pColData->numOfNull++;
334,988✔
3904
    if (pColData->numOfNone) {
334,988✔
3905
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
323,568✔
3906
    } else {
3907
      pColData->flag = HAS_NULL;
11,420✔
3908
    }
3909
  }
3910
  return 0;
334,988✔
3911
}
3912
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,147,483,647✔
3913
  if (forward) {  // VALUE ==> VALUE
2,147,483,647✔
3914
    pColData->nVal--;
2,147,483,647✔
3915
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3916
      pColData->nData = pColData->aOffset[pColData->nVal];
1,792,596,700✔
3917
    } else {
3918
      pColData->nData -= TYPE_BYTES[pColData->type];
2,147,483,647✔
3919
    }
3920
    return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3921
  }
3922
  return 0;
×
3923
}
3924
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
637,533✔
3925
  if (forward) {  // VALUE ==> NULL
637,533✔
3926
    pColData->numOfValue--;
637,533✔
3927
    pColData->nVal--;
637,533✔
3928
    if (pColData->numOfValue) {
637,533✔
3929
      if (IS_VAR_DATA_TYPE(pColData->type)) {
616,615✔
3930
        pColData->nData = pColData->aOffset[pColData->nVal];
179,760✔
3931
      } else {
3932
        pColData->nData -= TYPE_BYTES[pColData->type];
436,855✔
3933
      }
3934
      return tColDataAppendValue42(pColData, pData, nData);
616,615✔
3935
    } else {
3936
      pColData->flag = 0;
20,918✔
3937
      pColData->nData = 0;
20,918✔
3938
      return tColDataAppendValue02(pColData, pData, nData);
20,918✔
3939
    }
3940
  }
3941
  return 0;
×
3942
}
3943
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,261,218✔
3944
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
2,261,218✔
3945
    pColData->numOfNone--;
128,400✔
3946
    pColData->nVal--;
128,400✔
3947
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
128,400✔
3948
      pColData->nData -= TYPE_BYTES[pColData->type];
128,400✔
3949
    }
3950
    if (pColData->numOfNone) {
128,400✔
3951
      return tColDataAppendValue50(pColData, pData, nData);
×
3952
    } else {
3953
      pColData->flag = HAS_VALUE;
128,400✔
3954
      return tColDataAppendValue40(pColData, pData, nData);
128,400✔
3955
    }
3956
  } else if (forward) {  // VALUE ==> VALUE
2,132,818✔
3957
    pColData->nVal--;
2,132,818✔
3958
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,132,818✔
3959
      pColData->nData = pColData->aOffset[pColData->nVal];
304,560✔
3960
    } else {
3961
      pColData->nData -= TYPE_BYTES[pColData->type];
1,828,258✔
3962
    }
3963
    return tColDataPutValue(pColData, pData, nData);
2,132,818✔
3964
  }
3965
  return 0;
×
3966
}
3967
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
23,683✔
3968
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
23,683✔
3969
    pColData->numOfNone--;
23,683✔
3970
    pColData->nVal--;
23,683✔
3971
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
23,683✔
3972
      pColData->nData -= TYPE_BYTES[pColData->type];
15,979✔
3973
    }
3974
    if (pColData->numOfNone) {
23,683✔
3975
      return tColDataAppendValue52(pColData, pData, nData);
×
3976
    } else {
3977
      pColData->flag = HAS_VALUE;
23,683✔
3978
      return tColDataAppendValue42(pColData, pData, nData);
23,683✔
3979
    }
3980
  } else if (forward) {  // VALUE ==> NULL
×
3981
    pColData->numOfValue--;
×
3982
    pColData->nVal--;
×
3983
    if (pColData->numOfValue) {
×
3984
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3985
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3986
      } else {
3987
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3988
      }
3989
      return tColDataAppendValue52(pColData, pData, nData);
×
3990
    } else {
3991
      pColData->flag = HAS_NONE;
×
3992
      pColData->nData = 0;
×
3993
      return tColDataAppendValue12(pColData, pData, nData);
×
3994
    }
3995
  }
3996
  return 0;
×
3997
}
3998
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,309,990✔
3999
  if (forward) {
3,309,990✔
4000
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,309,990✔
4001
      pColData->numOfNull--;
2,319,646✔
4002
      pColData->nVal--;
2,320,288✔
4003
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
2,319,646✔
4004
        pColData->nData -= TYPE_BYTES[pColData->type];
1,918,801✔
4005
      }
4006
      if (pColData->numOfNull) {
2,318,362✔
4007
        return tColDataAppendValue60(pColData, pData, nData);
1,276✔
4008
      } else {
4009
        pColData->flag = HAS_VALUE;
2,319,012✔
4010
        return tColDataAppendValue40(pColData, pData, nData);
2,319,012✔
4011
      }
4012
    } else {  // VALUE ==> VALUE
4013
      pColData->nVal--;
989,702✔
4014
      if (IS_VAR_DATA_TYPE(pColData->type)) {
989,702✔
4015
        pColData->nData = pColData->aOffset[pColData->nVal];
121,537✔
4016
      } else {
4017
        pColData->nData -= TYPE_BYTES[pColData->type];
868,165✔
4018
      }
4019
      return tColDataPutValue(pColData, pData, nData);
989,702✔
4020
    }
4021
  }
4022
  return 0;
×
4023
}
4024
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,588,718✔
4025
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
1,588,718✔
4026
    pColData->numOfValue--;
965,703✔
4027
    pColData->nVal--;
965,703✔
4028
    if (pColData->numOfValue) {
965,703✔
4029
      if (IS_VAR_DATA_TYPE(pColData->type)) {
965,703✔
4030
        pColData->nData = pColData->aOffset[pColData->nVal];
103,347✔
4031
      } else {
4032
        pColData->nData -= TYPE_BYTES[pColData->type];
862,356✔
4033
      }
4034
      return tColDataAppendValue62(pColData, pData, nData);
965,703✔
4035
    } else {
4036
      pColData->flag = HAS_NULL;
×
4037
      pColData->nData = 0;
×
4038
      return tColDataAppendValue20(pColData, pData, nData);
×
4039
    }
4040
  }
4041
  return 0;
623,015✔
4042
}
4043
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
325,500✔
4044
  int32_t code = 0;
325,500✔
4045

4046
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
325,500✔
4047
  if (bv == 0) {  // NONE ==> VALUE
325,500✔
4048
    pColData->numOfNone--;
77,040✔
4049
    pColData->nVal--;
77,040✔
4050
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
77,040✔
4051
      pColData->nData -= TYPE_BYTES[pColData->type];
77,040✔
4052
    }
4053
    if (pColData->numOfNone) {
77,040✔
4054
      return tColDataAppendValue70(pColData, pData, nData);
77,040✔
4055
    } else {
4056
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4057
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
×
4058
      }
4059
      pColData->flag = (HAS_VALUE | HAS_NULL);
×
4060
      return tColDataAppendValue60(pColData, pData, nData);
×
4061
    }
4062
  } else if (bv == 1) {  // NULL ==> VALUE
248,460✔
4063
    if (forward) {
41,740✔
4064
      pColData->numOfNull--;
41,740✔
4065
      pColData->nVal--;
41,740✔
4066
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
41,740✔
4067
        pColData->nData -= TYPE_BYTES[pColData->type];
41,740✔
4068
      }
4069
      if (pColData->numOfNull) {
41,740✔
4070
        return tColDataAppendValue70(pColData, pData, nData);
41,088✔
4071
      } else {
4072
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
1,956✔
4073
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
1,304✔
4074
        }
4075
        pColData->flag = (HAS_VALUE | HAS_NONE);
652✔
4076
        return tColDataAppendValue50(pColData, pData, nData);
652✔
4077
      }
4078
    }
4079
  } else if (bv == 2) {  // VALUE ==> VALUE
206,720✔
4080
    if (forward) {
206,720✔
4081
      pColData->nVal--;
206,720✔
4082
      if (IS_VAR_DATA_TYPE(pColData->type)) {
206,720✔
4083
        pColData->nData = pColData->aOffset[pColData->nVal];
51,360✔
4084
      } else {
4085
        pColData->nData -= TYPE_BYTES[pColData->type];
155,360✔
4086
      }
4087
      return tColDataPutValue(pColData, pData, nData);
206,720✔
4088
    }
4089
  } else {
4090
    return TSDB_CODE_INVALID_PARA;
×
4091
  }
4092
  return 0;
×
4093
}
4094
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
380,172✔
4095
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
380,172✔
4096
  if (bv == 0) {  // NONE ==> NULL
380,172✔
4097
    pColData->numOfNone--;
380,172✔
4098
    pColData->nVal--;
380,172✔
4099
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
380,172✔
4100
      pColData->nData -= TYPE_BYTES[pColData->type];
236,313✔
4101
    }
4102
    if (pColData->numOfNone) {
380,172✔
4103
      return tColDataAppendValue72(pColData, pData, nData);
×
4104
    } else {
4105
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
11,947,736✔
4106
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
11,567,564✔
4107
      }
4108
      pColData->flag = (HAS_VALUE | HAS_NULL);
380,172✔
4109
      return tColDataAppendValue62(pColData, pData, nData);
380,172✔
4110
    }
4111
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
×
4112
    pColData->numOfValue--;
×
4113
    pColData->nVal--;
×
4114
    if (pColData->numOfValue) {
×
4115
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
4116
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4117
      } else {
4118
        pColData->nData -= TYPE_BYTES[pColData->type];
×
4119
      }
4120
      return tColDataAppendValue72(pColData, pData, nData);
×
4121
    } else {
4122
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4123
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
×
4124
      }
4125
      pColData->flag = (HAS_NULL | HAS_NONE);
×
4126
      pColData->nData = 0;
×
4127
      return tColDataAppendValue32(pColData, pData, nData);
×
4128
    }
4129
  }
4130
  return 0;
×
4131
}
4132
static FORCE_INLINE int32_t tColDataUpdateNothing(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,945,614✔
4133
  return 0;
2,945,614✔
4134
}
4135
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
4136
    {NULL, NULL, NULL},                                                     // 0
4137
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
4138
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
4139
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
4140
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
4141
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
4142
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
4143
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
4144

4145
    //    VALUE             NONE        NULL
4146
};
4147
int32_t tColDataUpdateValue(SColData *pColData, SColVal *pColVal, bool forward) {
2,147,483,647✔
4148
  if (!(pColData->cid == pColVal->cid && pColData->type == pColVal->value.type)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
4149
  if (!(pColData->nVal > 0)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
4150

4151
  if (tColDataUpdateValueImpl[pColData->flag][pColVal->flag] == NULL) return 0;
2,147,483,647✔
4152

4153
  return tColDataUpdateValueImpl[pColData->flag][pColVal->flag](
2,147,483,647✔
4154
      pColData, VALUE_GET_DATUM(&pColVal->value, pColData->type), pColVal->value.nData, forward);
2,147,483,647✔
4155
}
4156

4157
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
1,593,379,543✔
4158
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,593,379,543✔
4159
}
1,593,614,503✔
4160
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,671,362,639✔
4161
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,671,362,639✔
4162
}
1,671,359,397✔
4163
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
6,294,978✔
4164
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4165
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
6,294,978✔
4166
    case 0:
3,165,786✔
4167
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,165,786✔
4168
      break;
3,165,786✔
4169
    case 1:
3,129,192✔
4170
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
3,129,192✔
4171
      break;
3,129,192✔
4172
    default:
×
4173
      break;
×
4174
  }
4175
}
6,294,978✔
4176
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4177
  SValue value = {.type = pColData->type};
2,147,483,647✔
4178
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
4179
    if (iVal + 1 < pColData->nVal) {
2,147,483,647✔
4180
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
2,147,483,647✔
4181
    } else {
4182
      value.nData = pColData->nData - pColData->aOffset[iVal];
203,131,155✔
4183
    }
4184
    value.pData = pColData->pData + pColData->aOffset[iVal];
2,147,483,647✔
4185
  } else {
4186
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4187
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4188
  }
4189
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4190
}
2,147,483,647✔
4191
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
1,507,758,525✔
4192
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4193
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
1,507,758,525✔
4194
    case 0:
735,137,378✔
4195
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
735,137,378✔
4196
      break;
735,137,078✔
4197
    case 1:
772,874,917✔
4198
      tColDataGetValue4(pColData, iVal, pColVal);
4199
      break;
772,467,374✔
4200
    default:
×
4201
      break;
×
4202
  }
4203
}
1,507,604,452✔
4204
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
2,147,483,647✔
4205
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4206
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4207
    case 0:
2,147,483,647✔
4208
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
2,147,483,647✔
4209
      break;
2,147,483,647✔
4210
    case 1:
2,147,483,647✔
4211
      tColDataGetValue4(pColData, iVal, pColVal);
4212
      break;
2,147,483,647✔
4213
    default:
×
4214
      break;
×
4215
  }
4216
}
2,147,483,647✔
4217
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
22,286,806✔
4218
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4219
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
22,286,806✔
4220
    case 0:
3,986,396✔
4221
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,986,396✔
4222
      break;
3,986,396✔
4223
    case 1:
4,690,002✔
4224
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
4,690,002✔
4225
      break;
4,690,002✔
4226
    case 2:
13,614,260✔
4227
      tColDataGetValue4(pColData, iVal, pColVal);
4228
      break;
13,613,618✔
4229
    default:
×
4230
      break;
×
4231
  }
4232
}
22,290,016✔
4233
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4234
    NULL,               // 0
4235
    tColDataGetValue1,  // HAS_NONE
4236
    tColDataGetValue2,  // HAS_NULL
4237
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4238
    tColDataGetValue4,  // HAS_VALUE
4239
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4240
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4241
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4242
};
4243
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4244
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647✔
4245
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647✔
4246
    return TSDB_CODE_INVALID_PARA;
318✔
4247
  }
4248
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4249
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4250
}
4251

4252
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
2,147,483,647✔
4253
  switch (pColData->flag) {
2,147,483,647✔
4254
    case HAS_NONE:
×
4255
      return 0;
×
4256
    case HAS_NULL:
×
4257
      return 1;
×
4258
    case (HAS_NULL | HAS_NONE):
6,260,142✔
4259
      return GET_BIT1(pColData->pBitMap, iVal);
6,260,142✔
4260
    case HAS_VALUE:
×
4261
      return 2;
×
4262
    case (HAS_VALUE | HAS_NONE):
2,147,483,647✔
4263
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
2,147,483,647✔
4264
    case (HAS_VALUE | HAS_NULL):
2,147,483,647✔
4265
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
2,147,483,647✔
4266
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
97,159,586✔
4267
      return GET_BIT2(pColData->pBitMap, iVal);
97,159,586✔
4268
    default:
×
4269
      return 0;
×
4270
  }
4271
}
4272

4273
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
15,067,713✔
4274
  int32_t code = 0;
15,067,713✔
4275

4276
  *pColData = *pColDataFrom;
15,067,713✔
4277

4278
  // bitmap
4279
  switch (pColData->flag) {
15,068,415✔
4280
    case (HAS_NULL | HAS_NONE):
54,972✔
4281
    case (HAS_VALUE | HAS_NONE):
4282
    case (HAS_VALUE | HAS_NULL):
4283
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
54,972✔
4284
      if (pColData->pBitMap == NULL) {
54,972✔
4285
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4286
        goto _exit;
×
4287
      }
4288
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
54,972✔
4289
      break;
54,972✔
4290
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
4291
      pColData->pBitMap = xMalloc(arg, BIT2_SIZE(pColData->nVal));
×
4292
      if (pColData->pBitMap == NULL) {
×
4293
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4294
        goto _exit;
×
4295
      }
4296
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT2_SIZE(pColData->nVal));
×
4297
      break;
×
4298
    default:
15,013,443✔
4299
      pColData->pBitMap = NULL;
15,013,443✔
4300
      break;
15,013,443✔
4301
  }
4302

4303
  // offset
4304
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
15,068,415✔
4305
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
74,554✔
4306
    if (pColData->aOffset == NULL) {
74,554✔
4307
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4308
      goto _exit;
×
4309
    }
4310
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
74,554✔
4311
  } else {
4312
    pColData->aOffset = NULL;
14,993,824✔
4313
  }
4314

4315
  // value
4316
  if (pColData->nData) {
15,069,080✔
4317
    pColData->pData = xMalloc(arg, pColData->nData);
15,030,784✔
4318
    if (pColData->pData == NULL) {
15,030,820✔
4319
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4320
      goto _exit;
×
4321
    }
4322

4323
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
15,030,469✔
4324
  } else {
4325
    pColData->pData = NULL;
38,296✔
4326
  }
4327

4328
_exit:
15,068,766✔
4329
  return code;
15,068,766✔
4330
}
4331

4332
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
579,495,465✔
4333
  int32_t code;
4334
  SBuffer local;
579,489,549✔
4335

4336
  if (!(colData->nVal > 0)) {
579,637,615✔
4337
    return TSDB_CODE_INVALID_PARA;
×
4338
  }
4339

4340
  (*info) = (SColDataCompressInfo){
579,643,656✔
4341
      .cmprAlg = info->cmprAlg,
579,663,969✔
4342
      .columnFlag = colData->cflag,
579,679,857✔
4343
      .flag = colData->flag,
579,680,227✔
4344
      .dataType = colData->type,
579,660,044✔
4345
      .columnId = colData->cid,
579,627,712✔
4346
      .numOfData = colData->nVal,
579,637,309✔
4347
  };
4348

4349
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
579,670,939✔
4350
    return 0;
19,511,711✔
4351
  }
4352

4353
  tBufferInit(&local);
4354
  if (assist == NULL) {
560,160,221✔
4355
    assist = &local;
×
4356
  }
4357

4358
  // bitmap
4359
  if (colData->flag != HAS_VALUE) {
560,160,221✔
4360
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
31,959,220✔
4361
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,825,532✔
4362
    } else {
4363
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
30,133,113✔
4364
    }
4365

4366
    SCompressInfo cinfo = {
31,960,632✔
4367
        .dataType = TSDB_DATA_TYPE_TINYINT,
4368
        .cmprAlg = info->cmprAlg,
31,960,622✔
4369
        .originalSize = info->bitmapOriginalSize,
31,960,632✔
4370
    };
4371

4372
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
31,959,921✔
4373
    if (code) {
31,959,931✔
4374
      tBufferDestroy(&local);
4375
      return code;
×
4376
    }
4377

4378
    info->bitmapCompressedSize = cinfo.compressedSize;
31,959,931✔
4379
  }
4380

4381
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
559,998,495✔
4382
    tBufferDestroy(&local);
4383
    return 0;
100,567✔
4384
  }
4385

4386
  // offset
4387
  if (IS_VAR_DATA_TYPE(colData->type)) {
559,901,490✔
4388
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
89,709,296✔
4389

4390
    SCompressInfo cinfo = {
89,829,284✔
4391
        .dataType = TSDB_DATA_TYPE_INT,
4392
        .cmprAlg = info->cmprAlg,
89,836,229✔
4393
        .originalSize = info->offsetOriginalSize,
89,828,902✔
4394
    };
4395

4396
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
89,834,877✔
4397
    if (code) {
89,828,911✔
4398
      tBufferDestroy(&local);
4399
      return code;
×
4400
    }
4401

4402
    info->offsetCompressedSize = cinfo.compressedSize;
89,828,911✔
4403
  }
4404

4405
  // data
4406
  if (colData->nData > 0) {
560,035,463✔
4407
    info->dataOriginalSize = colData->nData;
560,049,030✔
4408

4409
    SCompressInfo cinfo = {
560,041,737✔
4410
        .dataType = colData->type,
560,052,240✔
4411
        .cmprAlg = info->cmprAlg,
560,039,965✔
4412
        .originalSize = info->dataOriginalSize,
560,044,738✔
4413
    };
4414

4415
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
559,862,110✔
4416
    if (code) {
559,946,542✔
4417
      tBufferDestroy(&local);
4418
      return code;
×
4419
    }
4420

4421
    info->dataCompressedSize = cinfo.compressedSize;
559,946,542✔
4422
  }
4423

4424
  tBufferDestroy(&local);
4425
  return 0;
559,855,746✔
4426
}
4427

4428
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
907,965,608✔
4429
  int32_t  code;
4430
  SBuffer  local;
907,960,678✔
4431
  uint8_t *data = (uint8_t *)input;
908,036,133✔
4432

4433
  tBufferInit(&local);
4434
  if (assist == NULL) {
907,996,898✔
4435
    assist = &local;
×
4436
  }
4437

4438
  tColDataClear(colData);
907,996,898✔
4439
  colData->cid = info->columnId;
908,006,445✔
4440
  colData->type = info->dataType;
908,042,518✔
4441
  colData->cflag = info->columnFlag;
908,033,478✔
4442
  colData->nVal = info->numOfData;
908,042,277✔
4443
  colData->flag = info->flag;
908,012,032✔
4444

4445
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
908,032,762✔
4446
    goto _exit;
71,880,114✔
4447
  }
4448

4449
  // bitmap
4450
  if (info->bitmapOriginalSize > 0) {
836,113,378✔
4451
    SCompressInfo cinfo = {
120,288,857✔
4452
        .dataType = TSDB_DATA_TYPE_TINYINT,
4453
        .cmprAlg = info->cmprAlg,
120,290,783✔
4454
        .originalSize = info->bitmapOriginalSize,
120,286,673✔
4455
        .compressedSize = info->bitmapCompressedSize,
120,282,455✔
4456
    };
4457

4458
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
120,286,945✔
4459
    if (code) {
120,279,551✔
4460
      tBufferDestroy(&local);
4461
      return code;
×
4462
    }
4463

4464
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
120,279,551✔
4465
    if (code) {
120,272,666✔
4466
      tBufferDestroy(&local);
4467
      return code;
×
4468
    }
4469

4470
    data += cinfo.compressedSize;
120,272,666✔
4471
  }
4472

4473
  if (info->flag == (HAS_NONE | HAS_NULL)) {
836,172,288✔
4474
    goto _exit;
193,242✔
4475
  }
4476

4477
  // offset
4478
  if (info->offsetOriginalSize > 0) {
835,949,795✔
4479
    SCompressInfo cinfo = {
177,689,149✔
4480
        .cmprAlg = info->cmprAlg,
177,673,048✔
4481
        .dataType = TSDB_DATA_TYPE_INT,
4482
        .originalSize = info->offsetOriginalSize,
177,703,598✔
4483
        .compressedSize = info->offsetCompressedSize,
177,697,098✔
4484
    };
4485

4486
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
177,688,619✔
4487
    if (code) {
177,692,232✔
4488
      tBufferDestroy(&local);
4489
      return code;
×
4490
    }
4491

4492
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
177,692,232✔
4493
    if (code) {
177,684,300✔
4494
      tBufferDestroy(&local);
4495
      return code;
×
4496
    }
4497

4498
    data += cinfo.compressedSize;
177,684,300✔
4499
  }
4500

4501
  // data
4502
  if (info->dataOriginalSize > 0) {
835,911,726✔
4503
    colData->nData = info->dataOriginalSize;
835,900,503✔
4504

4505
    SCompressInfo cinfo = {
835,933,818✔
4506
        .cmprAlg = info->cmprAlg,
1,671,835,470✔
4507
        .dataType = colData->type,
835,917,204✔
4508
        .originalSize = info->dataOriginalSize,
835,911,418✔
4509
        .compressedSize = info->dataCompressedSize,
835,945,377✔
4510
    };
4511

4512
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
835,940,991✔
4513
    if (code) {
835,858,307✔
4514
      tBufferDestroy(&local);
4515
      return code;
×
4516
    }
4517

4518
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
835,858,307✔
4519
    if (code) {
835,911,150✔
4520
      tBufferDestroy(&local);
4521
      return code;
×
4522
    }
4523

4524
    data += cinfo.compressedSize;
835,911,150✔
4525
  }
4526

4527
_exit:
908,070,876✔
4528
  switch (colData->flag) {
908,058,450✔
4529
    case HAS_NONE:
34,098,490✔
4530
      colData->numOfNone = colData->nVal;
34,098,490✔
4531
      break;
34,097,788✔
4532
    case HAS_NULL:
37,757,287✔
4533
      colData->numOfNull = colData->nVal;
37,757,287✔
4534
      break;
37,776,179✔
4535
    case HAS_VALUE:
715,829,835✔
4536
      colData->numOfValue = colData->nVal;
715,829,835✔
4537
      break;
715,884,223✔
4538
    default:
120,282,788✔
4539
      for (int32_t i = 0; i < colData->nVal; i++) {
2,147,483,647✔
4540
        uint8_t bitValue = tColDataGetBitValue(colData, i);
2,147,483,647✔
4541
        if (bitValue == 0) {
2,147,483,647✔
4542
          colData->numOfNone++;
770,238,020✔
4543
        } else if (bitValue == 1) {
2,147,483,647✔
4544
          colData->numOfNull++;
2,147,483,647✔
4545
        } else {
4546
          colData->numOfValue++;
2,147,483,647✔
4547
        }
4548
      }
4549
  }
4550
  tBufferDestroy(&local);
4551
  return 0;
907,994,948✔
4552
}
4553

4554
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
241,076✔
4555
                                    char *data) {
4556
  int32_t code = 0;
241,076✔
4557
  if (data == NULL) {
241,076✔
4558
    if (pColData->cflag & COL_IS_KEY) {
6,646✔
4559
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4560
    } else {
4561
      for (int32_t i = 0; i < nRows; ++i) {
14,616✔
4562
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
7,970✔
4563
      }
4564
    }
4565
    goto _exit;
6,646✔
4566
  }
4567

4568
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
234,430✔
4569
    if (!IS_STR_DATA_BLOB(type)) {
45,066✔
4570
      for (int32_t i = 0; i < nRows; ++i) {
119,517✔
4571
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
74,451✔
4572
        if (offset == -1) {
74,451✔
4573
          if (pColData->cflag & COL_IS_KEY) {
×
4574
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4575
            goto _exit;
×
4576
          }
4577
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4578
            goto _exit;
×
4579
          }
4580
        } else {
4581
          if (varDataTLen(data + offset) > bytes || (type == TSDB_DATA_TYPE_NCHAR && varDataLen(data + offset) % TSDB_NCHAR_SIZE != 0)) {
74,451✔
4582
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4583
                   bytes);
4584
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4585
            goto _exit;
×
4586
          }
4587
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
148,902✔
4588
                                                                        varDataLen(data + offset));
74,451✔
4589
        }
4590
      }
4591
    } else {
4592
      for (int32_t i = 0; i < nRows; ++i) {
×
4593
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4594
        if (offset == -1) {
×
4595
          if (pColData->cflag & COL_IS_KEY) {
×
4596
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4597
            goto _exit;
×
4598
          }
4599
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4600
            goto _exit;
×
4601
          }
4602
        } else {
4603
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4604
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4605
                   (int)blobDataTLen(data + offset), bytes);
4606
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4607
            goto _exit;
×
4608
          }
4609
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)blobDataVal(data + offset),
×
4610
                                                                        blobDataLen(data + offset));
×
4611
        }
4612
      }
4613
    }
4614
  } else {  // fixed-length data type
4615
    bool allValue = true;
189,364✔
4616
    bool allNull = true;
189,364✔
4617
    for (int32_t i = 0; i < nRows; ++i) {
514,122✔
4618
      if (!BMIsNull(lengthOrbitmap, i)) {
324,758✔
4619
        allNull = false;
246,763✔
4620
      } else {
4621
        allValue = false;
77,995✔
4622
      }
4623
    }
4624
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
189,364✔
4625
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4626
      goto _exit;
×
4627
    }
4628

4629
    if (allValue) {
189,364✔
4630
      // optimize (todo)
4631
      for (int32_t i = 0; i < nRows; ++i) {
396,652✔
4632
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
242,152✔
4633
      }
4634
    } else if (allNull) {
34,864✔
4635
      // optimize (todo)
4636
      for (int32_t i = 0; i < nRows; ++i) {
93,702✔
4637
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
62,137✔
4638
        if (code) goto _exit;
62,137✔
4639
      }
4640
    } else {
4641
      for (int32_t i = 0; i < nRows; ++i) {
23,768✔
4642
        if (BMIsNull(lengthOrbitmap, i)) {
20,469✔
4643
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
15,858✔
4644
          if (code) goto _exit;
15,858✔
4645
        } else {
4646
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
4,611✔
4647
        }
4648
      }
4649
    }
4650
  }
4651

4652
_exit:
3,299✔
4653
  return code;
241,076✔
4654
}
4655
int32_t tColDataAddValueByDataBlockWithBlob(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows,
×
4656
                                            char *lengthOrbitmap, char *data, void *pBlobSet) {
4657
  int32_t code = 0;
×
4658
  if (data == NULL) {
×
4659
    if (pColData->cflag & COL_IS_KEY) {
×
4660
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4661
    } else {
4662
      for (int32_t i = 0; i < nRows; ++i) {
×
4663
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4664
      }
4665
    }
4666
    goto _exit;
×
4667
  }
4668

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

4730
    if (allValue) {
×
4731
      // optimize (todo)
4732
      for (int32_t i = 0; i < nRows; ++i) {
×
4733
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4734
      }
4735
    } else if (allNull) {
×
4736
      // optimize (todo)
4737
      for (int32_t i = 0; i < nRows; ++i) {
×
4738
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4739
        if (code) goto _exit;
×
4740
      }
4741
    } else {
4742
      for (int32_t i = 0; i < nRows; ++i) {
×
4743
        if (BMIsNull(lengthOrbitmap, i)) {
×
4744
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4745
          if (code) goto _exit;
×
4746
        } else {
4747
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4748
        }
4749
      }
4750
    }
4751
  }
4752

4753
_exit:
×
4754
  return code;
×
4755
}
4756

4757
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32_t buffMaxLen, initGeosFn igeos,
22,101,618✔
4758
                               checkWKBGeometryFn cgeos) {
4759
  int32_t code = 0;
22,101,618✔
4760

4761
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
22,101,618✔
4762
    if (!(pColData->type == pBind->buffer_type)) {
22,107,646✔
4763
      return TSDB_CODE_INVALID_PARA;
×
4764
    }
4765
  }
4766

4767
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
22,113,218✔
4768
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
32,198✔
4769
      code = igeos();
1,412✔
4770
      if (code) {
1,412✔
4771
        return code;
×
4772
      }
4773
    }
4774
    for (int32_t i = 0; i < pBind->num; ++i) {
1,392,506✔
4775
      if (pBind->is_null && pBind->is_null[i]) {
1,372,324✔
4776
        if (pColData->cflag & COL_IS_KEY) {
669,618✔
4777
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,418✔
4778
          goto _exit;
1,418✔
4779
        }
4780
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
668,200✔
4781
        if (code) goto _exit;
668,200✔
4782
      } else if (pBind->length[i] > buffMaxLen) {
702,706✔
4783
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4784
      } else {
4785
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
702,706✔
4786
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
2,520✔
4787
          if (code) {
2,520✔
4788
            uError("stmt col[%d] bind geometry wrong format", i);
104✔
4789
            goto _exit;
104✔
4790
          }
4791
        }
4792
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
1,386,932✔
4793
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
702,602✔
4794
      }
4795
    }
4796
  } else {  // fixed-length data type
4797
    bool allValue;
4798
    bool allNull;
4799
    if (pBind->is_null) {
22,080,630✔
4800
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
14,743,826✔
4801
      allNull = (same && pBind->is_null[0] != 0);
14,743,094✔
4802
      allValue = (same && pBind->is_null[0] == 0);
14,742,346✔
4803
    } else {
4804
      allNull = false;
7,340,074✔
4805
      allValue = true;
7,340,074✔
4806
    }
4807

4808
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
22,083,526✔
4809
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
2,836✔
4810
      goto _exit;
2,836✔
4811
    }
4812

4813
    if (allValue) {
22,075,924✔
4814
      // optimize (todo)
4815
      for (int32_t i = 0; i < pBind->num; ++i) {
2,147,483,647✔
4816
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,147,483,647✔
4817
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
2,147,483,647✔
4818
      }
4819
    } else if (allNull) {
26,054✔
4820
      // optimize (todo)
4821
      for (int32_t i = 0; i < pBind->num; ++i) {
1,300✔
4822
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
650✔
4823
        if (code) goto _exit;
650✔
4824
      }
4825
    } else {
4826
      for (int32_t i = 0; i < pBind->num; ++i) {
8,703,012✔
4827
        if (pBind->is_null[i]) {
8,677,608✔
4828
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,836,354✔
4829
          if (code) goto _exit;
3,836,354✔
4830
        } else {
4831
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
9,682,508✔
4832
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,841,254✔
4833
        }
4834
      }
4835
    }
4836
  }
4837

4838
_exit:
10,488✔
4839
  return code;
22,116,848✔
4840
}
4841

4842
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen) {
994,001,609✔
4843
  int32_t code = 0;
994,001,609✔
4844

4845
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
994,001,609✔
4846
    if (!(pColData->type == pBind->buffer_type)) {
965,257,027✔
4847
      return TSDB_CODE_INVALID_PARA;
×
4848
    }
4849
  }
4850

4851
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
1,132,180,137✔
4852
    uint8_t *buf = pBind->buffer;
123,506,726✔
4853
    for (int32_t i = 0; i < pBind->num; ++i) {
244,242,699✔
4854
      if (pBind->is_null && pBind->is_null[i]) {
122,480,661✔
4855
        if (pColData->cflag & COL_IS_KEY) {
6,012,173✔
4856
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4857
          goto _exit;
×
4858
        }
4859
        if (pBind->is_null[i] == 1) {
6,012,787✔
4860
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
6,005,306✔
4861
          if (code) goto _exit;
6,011,086✔
4862
        } else {
4863
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4864
          if (code) goto _exit;
×
4865
        }
4866
      } else if (pBind->length[i] > buffMaxLen) {
116,278,415✔
4867
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4868
      } else {
4869
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
116,668,757✔
4870
        buf += pBind->length[i];
116,208,406✔
4871
      }
4872
    }
4873
  } else {  // fixed-length data type
4874
    bool allValue;
4875
    bool allNull;
4876
    bool allNone;
4877
    if (pBind->is_null) {
905,619,808✔
4878
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
30,179,016✔
4879
      allNull = (same && pBind->is_null[0] == 1);
30,212,603✔
4880
      allNone = (same && pBind->is_null[0] > 1);
30,187,964✔
4881
      allValue = (same && pBind->is_null[0] == 0);
30,205,395✔
4882
    } else {
4883
      allNull = false;
861,488,085✔
4884
      allNone = false;
861,488,085✔
4885
      allValue = true;
861,488,085✔
4886
    }
4887

4888
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
891,667,887✔
4889
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4890
      goto _exit;
×
4891
    }
4892

4893
    uint8_t *buf = pBind->buffer;
894,582,100✔
4894

4895
    if (allValue) {
899,467,921✔
4896
      // optimize (todo)
4897
      for (int32_t i = 0; i < pBind->num; ++i) {
1,722,480,868✔
4898
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
861,921,222✔
4899
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
874,890,931✔
4900
          *val = 1;
×
4901
        }
4902
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
867,589,409✔
4903
      }
4904
    } else if (allNull) {
31,825,337✔
4905
      // optimize (todo)
4906
      for (int32_t i = 0; i < pBind->num; ++i) {
60,385,540✔
4907
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
30,184,129✔
4908
        if (code) goto _exit;
30,203,360✔
4909
      }
4910
    } else if (allNone) {
1,643,157✔
4911
      // optimize (todo)
4912
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4913
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4914
        if (code) goto _exit;
×
4915
      }
4916
    } else {
4917
      for (int32_t i = 0; i < pBind->num; ++i) {
1,643,157✔
4918
        if (pBind->is_null[i]) {
×
4919
          if (pBind->is_null[i] == 1) {
×
4920
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4921
            if (code) goto _exit;
×
4922
          } else {
4923
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4924
            if (code) goto _exit;
×
4925
          }
4926
        } else {
4927
          uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
×
4928
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
×
4929
            *val = 1;
×
4930
          }
4931

4932
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
×
4933
        }
4934
      }
4935
    }
4936
  }
4937

4938
_exit:
35,914,776✔
4939
  return code;
998,357,934✔
4940
}
4941

4942
int32_t tColDataAddValueByBind2WithGeos(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
1,138,530✔
4943
                                        initGeosFn igeos, checkWKBGeometryFn cgeos) {
4944
  int32_t code = 0;
1,138,530✔
4945

4946
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
1,138,530✔
4947
    if (!(pColData->type == pBind->buffer_type)) {
1,139,625✔
4948
      return TSDB_CODE_INVALID_PARA;
×
4949
    }
4950
  }
4951

4952
  if (pColData->type != TSDB_DATA_TYPE_GEOMETRY) {
1,139,549✔
4953
    return TSDB_CODE_INVALID_OPTION;
×
4954
  }
4955

4956
  code = igeos();
1,139,119✔
4957
  if (code) {
1,138,326✔
4958
    return code;
×
4959
  }
4960

4961
  uint8_t *buf = pBind->buffer;
1,138,326✔
4962
  for (int32_t i = 0; i < pBind->num; ++i) {
2,276,962✔
4963
    if (pBind->is_null && pBind->is_null[i]) {
1,139,283✔
4964
      if (pColData->cflag & COL_IS_KEY) {
240✔
4965
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4966
        goto _exit;
×
4967
      }
4968
      if (pBind->is_null[i] == 1) {
240✔
4969
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
240✔
4970
        if (code) goto _exit;
240✔
4971
      } else {
4972
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4973
        if (code) goto _exit;
×
4974
      }
4975
    } else if (pBind->length[i] > buffMaxLen) {
1,139,315✔
4976
      return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4977
    } else {
4978
      code = cgeos(buf, pBind->length[i]);
1,139,728✔
4979
      if (code) {
1,132,366✔
4980
        uError("stmt2 col[%d] bind geometry with wrong format", i);
×
4981
        goto _exit;
×
4982
      }
4983
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
1,132,366✔
4984
      buf += pBind->length[i];
1,137,454✔
4985
    }
4986
  }
4987

4988
_exit:
1,138,363✔
4989
  return code;
1,138,363✔
4990
}
4991

4992
int32_t tColDataAddValueByBind2WithBlob(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
255✔
4993
                                        SBlobSet *pBlobSet) {
4994
  int32_t code = 0;
255✔
4995

4996
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
255✔
4997
    if (!(pColData->type == pBind->buffer_type)) {
128✔
4998
      return TSDB_CODE_INVALID_PARA;
×
4999
    }
5000
  }
5001

5002
  if (IS_STR_DATA_BLOB(pColData->type)) {  // var-length data type
258✔
5003
    uint8_t *buf = pBind->buffer;
258✔
5004
    for (int32_t i = 0; i < pBind->num; ++i) {
516✔
5005
      if (pBind->is_null && pBind->is_null[i]) {
258✔
5006
        if (pColData->cflag & COL_IS_KEY) {
130✔
5007
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5008
          goto _exit;
×
5009
        }
5010
        if (pBind->is_null[i] == 1) {
130✔
5011
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NULL](pBlobSet, pColData, NULL, 0);
130✔
5012
          if (code) goto _exit;
130✔
5013
        } else {
5014
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NONE](pBlobSet, pColData, NULL, 0);
×
5015
          if (code) goto _exit;
×
5016
        }
5017
      } else if (pBind->length[i] > buffMaxLen) {
128✔
5018
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5019
      } else {
5020
        code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_VALUE](pBlobSet, pColData, buf, pBind->length[i]);
128✔
5021
        buf += pBind->length[i];
128✔
5022
      }
5023
    }
5024
  }
5025
_exit:
258✔
5026
  return code;
258✔
5027
}
5028

5029
int32_t tColDataAddValueByBind2WithDecimal(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
1,105✔
5030
                                           uint8_t precision, uint8_t scale) {
5031
  int32_t code = 0;
1,105✔
5032

5033
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
1,105✔
5034
    if (!(pColData->type == pBind->buffer_type)) {
734✔
5035
      return TSDB_CODE_INVALID_PARA;
×
5036
    }
5037
  }
5038

5039
  if (!IS_DECIMAL_TYPE(pColData->type)) {
1,108✔
5040
    return TSDB_CODE_INVALID_OPTION;
×
5041
  }
5042

5043
  uint8_t *buf = pBind->buffer;
1,108✔
5044
  for (int32_t i = 0; i < pBind->num; ++i) {
2,822✔
5045
    if (pBind->is_null && pBind->is_null[i]) {
1,714✔
5046
      if (pColData->cflag & COL_IS_KEY) {
374✔
5047
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5048
        goto _exit;
×
5049
      }
5050
      if (pBind->is_null[i] == 1) {
374✔
5051
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
172✔
5052
        if (code) goto _exit;
172✔
5053
      } else {
5054
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
202✔
5055
        if (code) goto _exit;
202✔
5056
      }
5057
    } else {
5058
      if (pColData->type == TSDB_DATA_TYPE_DECIMAL64) {
1,340✔
5059
        Decimal64 dec = {0};
648✔
5060
        int32_t   code = decimal64FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
648✔
5061
        buf += pBind->length[i];
648✔
5062
        if (TSDB_CODE_SUCCESS != code) {
648✔
5063
          return code;
×
5064
        }
5065
        int64_t tmp = DECIMAL64_GET_VALUE(&dec);
648✔
5066
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)&tmp,
648✔
5067
                                                                      TYPE_BYTES[pColData->type]);
648✔
5068
      } else if (pColData->type == TSDB_DATA_TYPE_DECIMAL) {
692✔
5069
        Decimal128 dec = {0};
692✔
5070
        int32_t    code = decimal128FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
692✔
5071
        buf += pBind->length[i];
692✔
5072
        if (TSDB_CODE_SUCCESS != code) {
692✔
5073
          return code;
×
5074
        }
5075
        uint8_t *pV = taosMemCalloc(1, sizeof(Decimal128));
692✔
5076
        if (!pV) return terrno;
692✔
5077
        memcpy(pV, &dec, DECIMAL_WORD_NUM(Decimal128) * sizeof(DecimalWord));
692✔
5078
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pV, TYPE_BYTES[pColData->type]);
692✔
5079
        taosMemoryFree(pV);
692✔
5080
      }
5081
    }
5082
  }
5083

5084
_exit:
1,108✔
5085
  return code;
1,108✔
5086
}
5087
/* build rows to `rowArray` from bind
5088
 * `infos` is the bind information array
5089
 * `numOfInfos` is the number of bind information
5090
 * `infoSorted` is whether the bind information is sorted by column id
5091
 * `pTSchema` is the schema of the table
5092
 * `rowArray` is the array to store the rows
5093
 * `pOrdered` is the pointer to store ordered
5094
 * `pDupTs` is the pointer to store duplicateTs
5095
 */
5096
int32_t tRowBuildFromBind2(SBindInfo2 *infos, int32_t numOfInfos, SSHashObj *parsedCols, bool infoSorted,
1,099,410✔
5097
                           const STSchema *pTSchema, const SSchemaExt *pSchemaExt, SArray *rowArray, bool *pOrdered,
5098
                           bool *pDupTs) {
5099
  if (infos == NULL || numOfInfos <= 0 || pTSchema == NULL || numOfInfos > pTSchema->numOfCols || rowArray == NULL) {
1,099,410✔
5100
    return TSDB_CODE_INVALID_PARA;
×
5101
  }
5102
  int8_t hasBlob = schemaHasBlob(pTSchema);
1,100,396✔
5103
  if (!infoSorted) {
1,100,248✔
5104
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
707✔
5105
  }
5106

5107
  int32_t code = 0;
1,100,248✔
5108
  int32_t numOfRows = -1;
1,100,248✔
5109
  SArray *colValArray, *bufArray;
5110
  SColVal colVal;
317,278✔
5111
  int32_t numOfFixedValue = 0;
1,100,118✔
5112
  int32_t lino = 0;
1,100,118✔
5113
  bool    hasDecimal128 = false;
1,100,118✔
5114

5115
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
1,100,118✔
5116
    return terrno;
×
5117
  }
5118
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
1,100,130✔
5119
    taosArrayDestroy(colValArray);
×
5120
    return terrno;
×
5121
  }
5122
  for (int i = 0; i < numOfInfos; ++i) {
9,886,199✔
5123
    if (parsedCols) {
8,784,219✔
5124
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
14,544✔
5125
      if (pParsedVal) {
14,544✔
5126
        continue;
3,030✔
5127
      }
5128
    }
5129
    if (numOfRows == -1) {
8,781,189✔
5130
      numOfRows = infos[i].bind->num;
1,100,162✔
5131
    }
5132

5133
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
17,563,909✔
5134
      taosArrayDestroy(colValArray);
×
5135
      taosArrayDestroy(bufArray);
×
5136
      return terrno;
×
5137
    }
5138
  }
5139

5140
  SRowKey rowKey, lastRowKey;
318,715✔
5141
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
14,019,565✔
5142
    taosArrayClear(colValArray);
12,925,400✔
5143
    numOfFixedValue = 0;
12,923,586✔
5144

5145
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
72,248,518✔
5146
      if (parsedCols) {
59,180,326✔
5147
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
15,756✔
5148
        if (pParsedVal) {
15,756✔
5149
          numOfFixedValue++;
3,132✔
5150
          colVal = *pParsedVal;
3,132✔
5151

5152
          if (taosArrayPush(colValArray, &colVal) == NULL) {
3,636✔
5153
            code = terrno;
×
5154
            TAOS_CHECK_GOTO(code, &lino, _exit);
202✔
5155
          }
5156
          continue;
3,636✔
5157
        }
5158
      }
5159

5160
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
59,199,775✔
5161
        if (infos[iInfo].bind->is_null[iRow] == 1) {
505✔
5162
          if (iInfo == 0) {
303✔
5163
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
101✔
5164
            TAOS_CHECK_GOTO(code, &lino, _exit);
101✔
5165
          }
5166
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
202✔
5167
        } else {
5168
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
202✔
5169
        }
5170
      } else {
5171
        SValue value = {
59,225,736✔
5172
            .type = infos[iInfo].type,
59,235,224✔
5173
        };
5174
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
59,249,168✔
5175
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
3,859,159✔
UNCOV
5176
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5177
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5178
            value.nData = length;
×
5179
            if (value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE) > TSDB_MAX_BLOB_LEN) {
×
5180
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5181
              uError("stmt2 bind col:%d, row:%d length:%d  greater than blob maximum length: %d", iInfo, iRow,
×
5182
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), TSDB_MAX_BLOB_LEN);
5183
              goto _exit;
101✔
5184
            }
5185
            value.pData = *data;
×
5186
            *data += length;
×
5187
          } else {
5188
            int32_t   length = infos[iInfo].bind->length[iRow];
3,967,974✔
5189
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
3,968,864✔
5190
            value.nData = length;
3,970,812✔
5191
            if (value.nData + VARSTR_HEADER_SIZE > infos[iInfo].bytes) {
3,970,812✔
5192
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5193
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum length: %d", iInfo, iRow,
×
5194
                     value.nData + (uint32_t)(VARSTR_HEADER_SIZE), infos[iInfo].bytes);
5195
              goto _exit;
×
5196
            }
5197
            value.pData = *data;
3,968,600✔
5198
            *data += length;
3,968,318✔
5199
          }
5200
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5201
        } else {
5202
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
55,360,446✔
5203
            if (!pSchemaExt) {
404✔
5204
              uError("stmt2 decimal128 type without ext schema info, cannot parse decimal values");
×
5205
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5206
              goto _exit;
×
5207
            }
5208
            uint8_t precision = 0, scale = 0;
404✔
5209
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
404✔
5210
            Decimal128 dec = {0};
404✔
5211
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
404✔
5212
            int32_t    length = infos[iInfo].bind->length[iRow];
404✔
5213
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
404✔
5214
            *data += length;
404✔
5215
            hasDecimal128 = true;
404✔
5216
            TAOS_CHECK_GOTO(code, &lino, _exit);
404✔
5217

5218
            // precision check
5219
            // scale auto fit
5220

5221
            code = decimal128ToDataVal(&dec, &value);
404✔
5222
            TAOS_CHECK_GOTO(code, &lino, _exit);
404✔
5223

5224
          } else if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL64) {
55,362,879✔
5225
            if (!pSchemaExt) {
505✔
5226
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5227
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5228
              goto _exit;
101✔
5229
            }
5230
            uint8_t precision = 0, scale = 0;
505✔
5231
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
505✔
5232
            Decimal64 dec = {0};
505✔
5233
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
505✔
5234
            int32_t   length = infos[iInfo].bind->length[iRow];
505✔
5235
            code = decimal64FromStr(*(char **)data, length, precision, scale, &dec);
505✔
5236
            *data += length;
505✔
5237
            TAOS_CHECK_GOTO(code, &lino, _exit);
505✔
5238

5239
            code = decimal64ToDataVal(&dec, &value);
404✔
5240
            TAOS_CHECK_GOTO(code, &lino, _exit);
404✔
5241

5242
          } else {
5243
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
55,362,444✔
5244
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
55,363,857✔
5245
              *val = 1;
×
5246
            }
5247
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
55,363,857✔
5248
          }
5249
        }
5250
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
59,395,417✔
5251
      }
5252
      if (taosArrayPush(colValArray, &colVal) == NULL) {
59,321,296✔
5253
        code = terrno;
×
5254
        goto _exit;
×
5255
      }
5256
    }
5257

5258
    SRow *row;
2,238,692✔
5259

5260
    if (hasBlob == 0) {
13,025,417✔
5261
      SRowBuildScanInfo sinfo = {0};
12,923,174✔
5262
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
12,923,243✔
5263
      TAOS_CHECK_GOTO(code, &lino, _exit);
12,915,139✔
5264
    } else {
5265
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
103,269✔
5266
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
103,269✔
5267
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5268
    }
5269

5270
    if ((taosArrayPush(rowArray, &row)) == NULL) {
12,911,250✔
5271
      code = terrno;
×
5272
      goto _exit;
×
5273
    }
5274

5275
    // fix decimal memory leak
5276
    if (hasDecimal128) {
12,911,250✔
5277
      int32_t num = taosArrayGetSize(colValArray);
404✔
5278
      for (int32_t i = 0; i < num; ++i) {
1,616✔
5279
        SColVal *pCol = taosArrayGet(colValArray, i);
1,212✔
5280
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
1,212✔
5281
          taosMemoryFreeClear(pCol->value.pData);
404✔
5282
        }
5283
      }
5284
      hasDecimal128 = false;
404✔
5285
    }
5286

5287
    if (pOrdered && pDupTs) {
12,911,250✔
5288
      tRowGetKey(row, &rowKey);
25,832,266✔
5289
      if (iRow == 0) {
12,920,536✔
5290
        *pOrdered = true;
1,099,398✔
5291
        *pDupTs = false;
1,099,488✔
5292
      } else {
5293
        if (*pOrdered) {
11,821,138✔
5294
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
11,820,353✔
5295
          *pOrdered = (res >= 0);
11,820,353✔
5296
          if (!*pDupTs) {
11,820,279✔
5297
            *pDupTs = (res == 0);
11,819,994✔
5298
          }
5299
        }
5300
      }
5301
      lastRowKey = rowKey;
12,920,767✔
5302
    }
5303
  }
5304
_exit:
1,094,368✔
5305
  if (code != 0) {
1,094,287✔
5306
    if (hasDecimal128) {
202✔
5307
      int32_t num = taosArrayGetSize(colValArray);
×
5308
      for (int32_t i = 0; i < num; ++i) {
×
5309
        SColVal *pCol = taosArrayGet(colValArray, i);
×
5310
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
×
5311
          taosMemoryFreeClear(pCol->value.pData);
×
5312
        }
5313
      }
5314
    }
5315
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
202✔
5316
  }
5317
  taosArrayDestroy(colValArray);
1,094,287✔
5318
  taosArrayDestroy(bufArray);
1,099,918✔
5319
  return code;
1,100,833✔
5320
}
5321
/* build rows to `rowArray` from bind
5322
 * `infos` is the bind information array
5323
 * `numOfInfos` is the number of bind information
5324
 * `infoSorted` is whether the bind information is sorted by column id
5325
 * `pTSchema` is the schema of the table
5326
 * `rowArray` is the array to store the rows
5327
 * `pOrdered` is the pointer to store ordered
5328
 * `pDupTs` is the pointer to store duplicateTs
5329
 */
5330
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, SSHashObj *parsedCols, bool infoSorted,
101✔
5331
                                   const STSchema *pTSchema, const SSchemaExt *pSchemaExt, SArray *rowArray,
5332
                                   bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5333
  if (infos == NULL || numOfInfos <= 0 || pTSchema == NULL || numOfInfos > pTSchema->numOfCols || rowArray == NULL) {
101✔
5334
    return TSDB_CODE_INVALID_PARA;
×
5335
  }
5336
  int8_t hasBlob = schemaHasBlob(pTSchema);
101✔
5337
  if (!infoSorted) {
101✔
5338
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5339
  }
5340

5341
  int32_t code = 0;
101✔
5342
  int32_t numOfRows = -1;
101✔
5343
  SArray *colValArray, *bufArray;
5344
  SColVal colVal;
×
5345
  int32_t numOfFixedValue = 0;
101✔
5346
  int32_t lino = 0;
101✔
5347
  bool    hasDecimal128 = false;
101✔
5348

5349
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
101✔
5350
    return terrno;
×
5351
  }
5352
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
101✔
5353
    taosArrayDestroy(colValArray);
×
5354
    return terrno;
×
5355
  }
5356
  for (int i = 0; i < numOfInfos; ++i) {
404✔
5357
    if (parsedCols) {
303✔
5358
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
×
5359
      if (pParsedVal) {
×
5360
        continue;
×
5361
      }
5362
    }
5363
    if (numOfRows == -1) {
303✔
5364
      numOfRows = infos[i].bind->num;
101✔
5365
    }
5366

5367
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
606✔
5368
      taosArrayDestroy(colValArray);
×
5369
      taosArrayDestroy(bufArray);
×
5370
      return terrno;
×
5371
    }
5372
  }
5373

5374
  SRowKey rowKey, lastRowKey;
×
5375
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
404✔
5376
    taosArrayClear(colValArray);
303✔
5377
    numOfFixedValue = 0;
303✔
5378

5379
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
1,212✔
5380
      if (parsedCols) {
909✔
5381
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
×
5382
        if (pParsedVal) {
×
5383
          numOfFixedValue++;
×
5384
          colVal = *pParsedVal;
×
5385

5386
          if (taosArrayPush(colValArray, &colVal) == NULL) {
×
5387
            code = terrno;
×
5388
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5389
          }
5390
          continue;
×
5391
        }
5392
      }
5393

5394
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
909✔
5395
        if (infos[iInfo].bind->is_null[iRow] == 1) {
202✔
5396
          if (iInfo == 0) {
202✔
5397
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5398
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5399
          }
5400
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
202✔
5401
        } else {
5402
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5403
        }
5404
      } else {
5405
        SValue value = {
707✔
5406
            .type = infos[iInfo].type,
707✔
5407
        };
5408
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
707✔
5409
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
202✔
5410
            int32_t   length = infos[iInfo].bind->length[iRow];
202✔
5411
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
202✔
5412
            value.nData = length;
202✔
5413
            if (value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE) > TSDB_MAX_BLOB_LEN) {
202✔
5414
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5415
              uError("stmt2 bind col:%d, row:%d length:%d  greater than blob maximum length: %d", iInfo, iRow,
×
5416
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), TSDB_MAX_BLOB_LEN);
5417
              goto _exit;
×
5418
            }
5419
            value.pData = *data;
202✔
5420
            *data += length;
202✔
5421
          } else {
5422
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5423
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5424
            value.nData = length;
×
5425
            if (value.nData + VARSTR_HEADER_SIZE > infos[iInfo].bytes) {
×
5426
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5427
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum length: %d", iInfo, iRow,
×
5428
                     value.nData + (uint32_t)(VARSTR_HEADER_SIZE), infos[iInfo].bytes);
5429
              goto _exit;
×
5430
            }
5431
            value.pData = *data;
×
5432
            *data += length;
×
5433
          }
5434
        } else {
5435
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
505✔
5436
            if (!pSchemaExt) {
202✔
5437
              uError("stmt2 decimal128 type without ext schema info, cannot parse decimal values");
×
5438
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5439
              goto _exit;
×
5440
            }
5441
            uint8_t precision = 0, scale = 0;
202✔
5442
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
202✔
5443
            Decimal128 dec = {0};
202✔
5444
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
202✔
5445
            int32_t    length = infos[iInfo].bind->length[iRow];
202✔
5446
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
202✔
5447
            *data += length;
202✔
5448
            hasDecimal128 = true;
202✔
5449
            TAOS_CHECK_GOTO(code, &lino, _exit);
202✔
5450

5451
            code = decimal128ToDataVal(&dec, &value);
202✔
5452
            TAOS_CHECK_GOTO(code, &lino, _exit);
202✔
5453

5454
          } else if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL64) {
303✔
5455
            if (!pSchemaExt) {
×
5456
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5457
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5458
              goto _exit;
×
5459
            }
5460
            uint8_t precision = 0, scale = 0;
×
5461
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
×
5462
            Decimal64 dec = {0};
×
5463
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5464
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5465
            code = decimal64FromStr(*(char **)data, length, precision, scale, &dec);
×
5466
            *data += length;
×
5467
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5468

5469
            code = decimal64ToDataVal(&dec, &value);
×
5470
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5471

5472
          } else {
5473
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
303✔
5474
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
303✔
5475
              *val = 1;
×
5476
            }
5477
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
303✔
5478
          }
5479
        }
5480
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
707✔
5481
      }
5482
      if (taosArrayPush(colValArray, &colVal) == NULL) {
909✔
5483
        code = terrno;
×
5484
        goto _exit;
×
5485
      }
5486
    }
5487

5488
    SRow *row;
×
5489

5490
    if (hasBlob == 0) {
303✔
5491
      SRowBuildScanInfo sinfo = {0};
×
5492
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
×
5493
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5494
    } else {
5495
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
303✔
5496
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, pBlobSet, &sinfo);
303✔
5497
      TAOS_CHECK_GOTO(code, &lino, _exit);
303✔
5498
    }
5499

5500
    if ((taosArrayPush(rowArray, &row)) == NULL) {
303✔
5501
      code = terrno;
×
5502
      goto _exit;
×
5503
    }
5504

5505
    // fix decimal memory leak
5506
    if (hasDecimal128) {
303✔
5507
      int32_t num = taosArrayGetSize(colValArray);
202✔
5508
      for (int32_t i = 0; i < num; ++i) {
808✔
5509
        SColVal *pCol = taosArrayGet(colValArray, i);
606✔
5510
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
606✔
5511
          taosMemoryFreeClear(pCol->value.pData);
202✔
5512
        }
5513
      }
5514
      hasDecimal128 = false;
202✔
5515
    }
5516

5517
    if (pOrdered && pDupTs) {
303✔
5518
      tRowGetKey(row, &rowKey);
606✔
5519
      if (iRow == 0) {
303✔
5520
        *pOrdered = true;
101✔
5521
        *pDupTs = false;
101✔
5522
      } else {
5523
        if (*pOrdered) {
202✔
5524
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
202✔
5525
          *pOrdered = (res >= 0);
202✔
5526
          if (!*pDupTs) {
202✔
5527
            *pDupTs = (res == 0);
202✔
5528
          }
5529
        }
5530
      }
5531
      lastRowKey = rowKey;
303✔
5532
    }
5533
  }
5534
_exit:
101✔
5535
  if (code != 0) {
101✔
5536
    if (hasDecimal128) {
×
5537
      int32_t num = taosArrayGetSize(colValArray);
×
5538
      for (int32_t i = 0; i < num; ++i) {
×
5539
        SColVal *pCol = taosArrayGet(colValArray, i);
×
5540
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
×
5541
          taosMemoryFreeClear(pCol->value.pData);
×
5542
        }
5543
      }
5544
    }
5545
    uError("tRowBuildFromBind2WithBlob failed at line %d, ErrCode=0x%x", lino, code);
×
5546
  }
5547
  taosArrayDestroy(colValArray);
101✔
5548
  taosArrayDestroy(bufArray);
101✔
5549
  return code;
101✔
5550
}
5551

5552
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
46,379,075✔
5553
  int32_t code = TSDB_CODE_SUCCESS;
46,379,075✔
5554

5555
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
46,379,075✔
5556
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
5,848,590✔
5557
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
5,260,084✔
5558
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
11,108,674✔
5559
    if (iToRow == 0) {
5,848,590✔
5560
      pToColData->aOffset[iToRow] = 0;
16,406✔
5561
    }
5562

5563
    if (iToRow < pToColData->nVal - 1) {
5,848,590✔
5564
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
5,834,512✔
5565
    }
5566

5567
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
5,848,590✔
5568
                 nData);
5569
  } else {
5570
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
40,530,485✔
5571
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
40,530,485✔
5572
  }
5573
  return code;
46,379,075✔
5574
}
5575

5576
static int32_t tColDataCopyRowSingleCol(SColData *pFromColData, int32_t iFromRow, SColData *pToColData,
48,045,775✔
5577
                                        int32_t iToRow) {
5578
  int32_t code = TSDB_CODE_SUCCESS;
48,045,775✔
5579
  int     bit_val = 0;
48,045,775✔
5580

5581
  switch (pFromColData->flag) {
48,045,775✔
5582
    case HAS_NONE: {
×
5583
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5584
    } break;
×
5585
    case HAS_NULL: {
1,666,700✔
5586
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
1,666,700✔
5587
    } break;
1,666,700✔
5588
    case (HAS_NULL | HAS_NONE): {
×
5589
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5590
      if (0 == bit_val)
×
5591
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5592
      else
5593
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
×
5594
    } break;
×
5595
    case HAS_VALUE: {
5,262,046✔
5596
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
5,262,046✔
5597
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
5,262,046✔
5598
    } break;
5,262,046✔
5599
    case (HAS_VALUE | HAS_NONE): {
×
5600
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5601
      if (0 == bit_val)
×
5602
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5603
      else
5604
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
×
5605
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5606
    } break;
×
5607
    case (HAS_VALUE | HAS_NULL): {
41,117,029✔
5608
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
41,117,029✔
5609
      if (0 == bit_val)
41,117,029✔
5610
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
18,563,402✔
5611
      else
5612
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
22,553,627✔
5613
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
41,117,029✔
5614
    } break;
41,117,029✔
5615
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
5616
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
5617
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5618
    } break;
×
5619
    default:
×
5620
      return -1;
×
5621
  }
5622

5623
  return code;
48,045,775✔
5624
}
5625

5626
static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
3,022,110✔
5627
                               int32_t nColData) {
5628
  int32_t code = TSDB_CODE_SUCCESS;
3,022,110✔
5629

5630
  for (int32_t i = 0; i < nColData; i++) {
51,067,885✔
5631
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
48,045,775✔
5632
    if (code != TSDB_CODE_SUCCESS) {
48,045,775✔
5633
      return code;
×
5634
    }
5635
  }
5636

5637
  return code;
3,022,110✔
5638
}
5639

5640
static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
3,022,110✔
5641
  int32_t code = TSDB_CODE_SUCCESS;
3,022,110✔
5642

5643
  for (int32_t i = 0; i < nColData; i++) {
51,067,885✔
5644
    SColVal cv = {0};
48,045,775✔
5645
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
48,045,775✔
5646
    if (code != TSDB_CODE_SUCCESS) {
48,045,775✔
5647
      return code;
×
5648
    }
5649
    code = tColDataAppendValue(&aToColData[i], &cv);
48,045,775✔
5650
    if (code != TSDB_CODE_SUCCESS) {
48,045,775✔
5651
      return code;
×
5652
    }
5653
  }
5654

5655
  return code;
3,022,110✔
5656
}
5657

5658
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
2,147,483,647✔
5659
  SColVal cv;
2,147,483,647✔
5660

5661
  key->ts = ((TSKEY *)aColData[0].pData)[iRow];
2,147,483,647✔
5662
  key->numOfPKs = 0;
2,147,483,647✔
5663

5664
  for (int i = 1; i < nColData; i++) {
2,147,483,647✔
5665
    if (aColData[i].cflag & COL_IS_KEY) {
2,147,483,647✔
5666
      tColDataGetValue4(&aColData[i], iRow, &cv);
883,430✔
5667
      key->pks[key->numOfPKs++] = cv.value;
881,798✔
5668
    } else {
5669
      break;
2,147,483,647✔
5670
    }
5671
  }
5672
}
2,147,483,647✔
5673

5674
static int32_t tColDataMergeSortMerge(SColData *aColData, int32_t start, int32_t mid, int32_t end, int32_t nColData) {
340,413✔
5675
  SColData *aDstColData = NULL;
340,413✔
5676
  int32_t   i = start, j = mid + 1, k = 0;
340,413✔
5677
  SRowKey   keyi, keyj;
339,908✔
5678

5679
  if (end > start) {
340,413✔
5680
    aDstColData = taosMemoryCalloc(1, sizeof(SColData) * nColData);
340,413✔
5681
    if (aDstColData == NULL) {
340,413✔
5682
      return terrno;
×
5683
    }
5684
    for (int c = 0; c < nColData; ++c) {
5,691,551✔
5685
      tColDataInit(&aDstColData[c], aColData[c].cid, aColData[c].type, aColData[c].cflag);
5,351,138✔
5686
    }
5687
  }
5688

5689
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
340,413✔
5690
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
340,413✔
5691
  while (i <= mid && j <= end) {
1,852,509✔
5692
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
1,512,096✔
5693
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
10,813✔
5694
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
10,813✔
5695
    } else {
5696
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,501,283✔
5697
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
1,501,283✔
5698
    }
5699
  }
5700

5701
  while (i <= mid) {
1,842,270✔
5702
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,501,857✔
5703
  }
5704

5705
  while (j <= end) {
348,570✔
5706
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
8,157✔
5707
  }
5708

5709
  for (i = start, k = 0; i <= end; ++i, ++k) {
3,362,523✔
5710
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
3,022,110✔
5711
  }
5712

5713
  if (aDstColData) {
340,413✔
5714
    for (int32_t i = 0; i < nColData; i++) {
5,691,551✔
5715
      tColDataDestroy(&aDstColData[i]);
5,351,138✔
5716
    }
5717
    taosMemoryFree(aDstColData);
340,413✔
5718
  }
5719

5720
  return TSDB_CODE_SUCCESS;
340,413✔
5721
}
5722

5723
static int32_t tColDataMergeSort(SColData *aColData, int32_t start, int32_t end, int32_t nColData) {
683,607✔
5724
  int32_t ret = TSDB_CODE_SUCCESS;
683,607✔
5725
  int32_t mid;
5726

5727
  if (start >= end) {
683,607✔
5728
    return TSDB_CODE_SUCCESS;
343,194✔
5729
  }
5730

5731
  mid = (start + end) / 2;
340,413✔
5732

5733
  ret = tColDataMergeSort(aColData, start, mid, nColData);
340,413✔
5734
  if (ret != TSDB_CODE_SUCCESS) {
340,413✔
5735
    return ret;
×
5736
  }
5737

5738
  ret = tColDataMergeSort(aColData, mid + 1, end, nColData);
340,413✔
5739
  if (ret != TSDB_CODE_SUCCESS) {
340,413✔
5740
    return ret;
×
5741
  }
5742

5743
  return tColDataMergeSortMerge(aColData, start, mid, end, nColData);
340,413✔
5744
}
5745

5746
static int32_t tColDataSort(SColData *aColData, int32_t nColData) {
2,781✔
5747
  int32_t nVal = aColData[0].nVal;
2,781✔
5748

5749
  if (nVal < 2) return TSDB_CODE_SUCCESS;
2,781✔
5750

5751
  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
2,781✔
5752
}
5753

5754
static int32_t tColDataMerge(SArray **colArr) {
1,104✔
5755
  int32_t code = 0;
1,104✔
5756
  SArray *src = *colArr;
1,104✔
5757
  SArray *dst = NULL;
1,104✔
5758

5759
  dst = taosArrayInit(taosArrayGetSize(src), sizeof(SColData));
1,104✔
5760
  if (dst == NULL) {
1,104✔
5761
    return terrno;
×
5762
  }
5763

5764
  for (int32_t i = 0; i < taosArrayGetSize(src); i++) {
4,315✔
5765
    SColData *srcCol = taosArrayGet(src, i);
3,211✔
5766

5767
    SColData *dstCol = taosArrayReserve(dst, 1);
3,211✔
5768
    if (dstCol == NULL) {
3,211✔
5769
      code = terrno;
×
5770
      goto _exit;
×
5771
    }
5772
    tColDataInit(dstCol, srcCol->cid, srcCol->type, srcCol->cflag);
3,211✔
5773
  }
5774

5775
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
1,104✔
5776
  SRowKey lastKey;
1,003✔
5777
  for (int32_t i = 0; i < numRows; i++) {
3,716✔
5778
    SRowKey key;
2,006✔
5779
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
2,612✔
5780

5781
    if (i == 0 || tRowKeyCompare(&key, &lastKey) != 0) {  // append new row
4,120✔
5782
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
4,618✔
5783
        SColData *srcCol = taosArrayGet(src, j);
3,413✔
5784
        SColData *dstCol = taosArrayGet(dst, j);
3,413✔
5785

5786
        SColVal cv;
3,009✔
5787
        code = tColDataGetValue(srcCol, i, &cv);
3,413✔
5788
        if (code != TSDB_CODE_SUCCESS) {
3,413✔
5789
          goto _exit;
×
5790
        }
5791
        code = tColDataAppendValue(dstCol, &cv);
3,413✔
5792
        if (code) {
3,413✔
5793
          goto _exit;
×
5794
        }
5795
      }
5796
      lastKey = key;
1,205✔
5797
    } else {  // update existing row
5798
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
5,224✔
5799
        SColData *srcCol = taosArrayGet(src, j);
3,817✔
5800
        SColData *dstCol = taosArrayGet(dst, j);
3,817✔
5801

5802
        SColVal cv;
3,009✔
5803
        code = tColDataGetValue(srcCol, i, &cv);
3,817✔
5804
        if (code != TSDB_CODE_SUCCESS) {
3,817✔
5805
          goto _exit;
×
5806
        }
5807
        code = tColDataUpdateValue(dstCol, &cv, true);
3,817✔
5808
        if (code) {
3,817✔
5809
          goto _exit;
×
5810
        }
5811
      }
5812
    }
5813
  }
5814

5815
_exit:
1,104✔
5816
  if (code) {
1,104✔
5817
    taosArrayDestroyEx(dst, tColDataDestroy);
×
5818
  } else {
5819
    taosArrayDestroyEx(src, tColDataDestroy);
1,104✔
5820
    *colArr = dst;
1,104✔
5821
  }
5822
  return code;
1,104✔
5823
}
5824

5825
int32_t tColDataSortMerge(SArray **arr) {
7,529,446✔
5826
  SArray   *colDataArr = *arr;
7,529,446✔
5827
  int32_t   nColData = TARRAY_SIZE(colDataArr);
7,532,184✔
5828
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
7,532,916✔
5829

5830
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
7,531,468✔
5831
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5832
  }
5833
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
7,529,912✔
5834
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5835
  }
5836
  if (!(aColData[0].flag == HAS_VALUE)) {
7,530,736✔
5837
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5838
  }
5839

5840
  if (aColData[0].nVal <= 1) goto _exit;
7,529,896✔
5841

5842
  int8_t doSort = 0;
7,476,760✔
5843
  int8_t doMerge = 0;
7,476,760✔
5844
  // scan -------
5845
  SRowKey lastKey;
7,449,916✔
5846
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
7,478,690✔
5847
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
2,147,483,647✔
5848
    SRowKey key;
2,147,483,647✔
5849
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
2,147,483,647✔
5850

5851
    int32_t c = tRowKeyCompare(&lastKey, &key);
2,147,483,647✔
5852
    if (c < 0) {
2,147,483,647✔
5853
      lastKey = key;
2,147,483,647✔
5854
      continue;
2,147,483,647✔
5855
    } else if (c > 0) {
5,001,340✔
5856
      doSort = 1;
2,781✔
5857
      break;
2,781✔
5858
    } else {
5859
      doMerge = 1;
4,998,600✔
5860
    }
5861
  }
5862

5863
  // sort -------
5864
  if (doSort) {
10,749,059✔
5865
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
2,781✔
5866
  }
5867

5868
  if ((doMerge != 1) && (doSort == 1)) {
10,749,674✔
5869
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
2,781✔
5870
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
342,689✔
5871
      SRowKey key;
339,908✔
5872
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
340,009✔
5873

5874
      int32_t c = tRowKeyCompare(&lastKey, &key);
340,009✔
5875
      if (c == 0) {
340,009✔
5876
        doMerge = 1;
101✔
5877
        break;
101✔
5878
      }
5879
      lastKey = key;
339,908✔
5880
    }
5881
  }
5882

5883
  // merge -------
5884
  if (doMerge) {
7,480,170✔
5885
    int32_t code = tColDataMerge(arr);
1,104✔
5886
    if (code) return code;
1,104✔
5887
  }
5888

5889
_exit:
7,532,483✔
5890
  return 0;
7,533,238✔
5891
}
5892

5893
int32_t tColDataSortMergeWithBlob(SArray **arr, SBlobSet *pBlob) {
×
5894
  SArray   *colDataArr = *arr;
×
5895
  int32_t   nColData = TARRAY_SIZE(colDataArr);
×
5896
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
×
5897

5898
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
×
5899
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5900
  }
5901
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
×
5902
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5903
  }
5904
  if (!(aColData[0].flag == HAS_VALUE)) {
×
5905
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5906
  }
5907

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

5910
  int8_t doSort = 0;
×
5911
  int8_t doMerge = 0;
×
5912
  // scan -------
5913
  SRowKey lastKey;
×
5914
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5915
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5916
    SRowKey key;
×
5917
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5918

5919
    int32_t c = tRowKeyCompare(&lastKey, &key);
×
5920
    if (c < 0) {
×
5921
      lastKey = key;
×
5922
      continue;
×
5923
    } else if (c > 0) {
×
5924
      doSort = 1;
×
5925
      break;
×
5926
    } else {
5927
      doMerge = 1;
×
5928
    }
5929
  }
5930
  if (doMerge || doSort) {
×
5931
    return TSDB_CODE_BLOB_NOT_SUPPORT;
×
5932
  }
5933
  // sort -------
5934
  if (doSort) {
×
5935
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
×
5936
  }
5937

5938
  if ((doMerge != 1) && (doSort == 1)) {
×
5939
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5940
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5941
      SRowKey key;
×
5942
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5943

5944
      int32_t c = tRowKeyCompare(&lastKey, &key);
×
5945
      if (c == 0) {
×
5946
        doMerge = 1;
×
5947
        break;
×
5948
      }
5949
      lastKey = key;
×
5950
    }
5951
  }
5952

5953
  // merge -------
5954
  if (doMerge) {
×
5955
    int32_t code = tColDataMerge(arr);
×
5956
    if (code) return code;
×
5957
  }
5958

5959
_exit:
×
5960
  return 0;
×
5961
}
5962

5963
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
46,528,430✔
5964
  int32_t code = 0;
46,528,430✔
5965

5966
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
93,063,361✔
5967
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
93,064,787✔
5968
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
93,060,339✔
5969
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
93,057,899✔
5970

5971
  // bitmap
5972
  switch (pColData->flag) {
46,527,416✔
5973
    case (HAS_NULL | HAS_NONE):
178,372✔
5974
    case (HAS_VALUE | HAS_NONE):
5975
    case (HAS_VALUE | HAS_NULL):
5976
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
178,372✔
5977
      if (code) return code;
178,372✔
5978
      break;
178,372✔
5979
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5980
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
5981
      if (code) return code;
×
5982
      break;
×
5983
    default:
46,348,093✔
5984
      break;
46,348,093✔
5985
  }
5986

5987
  // value
5988
  if (pColData->flag & HAS_VALUE) {
46,526,465✔
5989
    if (IS_VAR_DATA_TYPE(pColData->type)) {
46,449,185✔
5990
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
213,831✔
5991
      if (code) return code;
210,170✔
5992

5993
      code = tEncodeI32v(pEncoder, pColData->nData);
210,170✔
5994
      if (code) return code;
210,170✔
5995

5996
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
210,170✔
5997
      if (code) return code;
210,126✔
5998
    } else {
5999
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
46,239,482✔
6000
      if (code) return code;
46,223,983✔
6001
    }
6002
  }
6003

6004
  return code;
46,518,925✔
6005
}
6006

6007
static int32_t tDecodeColDataVersion0(SDecoder *pDecoder, SColData *pColData) {
32,588,775✔
6008
  int32_t code = 0;
32,588,775✔
6009

6010
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
65,187,686✔
6011
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
65,199,254✔
6012
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
65,198,595✔
6013
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
65,199,023✔
6014

6015
  if (pColData->type <= 0 || pColData->type >= TSDB_DATA_TYPE_MAX || pColData->flag <= 0 || pColData->flag >= 8) {
32,600,771✔
6016
    return TSDB_CODE_INVALID_PARA;
×
6017
  }
6018

6019
  // bitmap
6020
  switch (pColData->flag) {
32,597,571✔
6021
    case (HAS_NULL | HAS_NONE):
62,736✔
6022
    case (HAS_VALUE | HAS_NONE):
6023
    case (HAS_VALUE | HAS_NULL):
6024
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
62,736✔
6025
      if (code) return code;
62,736✔
6026
      break;
62,736✔
6027
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
6028
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
6029
      if (code) return code;
×
6030
      break;
×
6031
    default:
32,533,446✔
6032
      break;
32,533,446✔
6033
  }
6034

6035
  // value
6036
  if (pColData->flag & HAS_VALUE) {
32,596,182✔
6037
    if (IS_VAR_DATA_TYPE(pColData->type)) {
32,553,498✔
6038
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
135,337✔
6039
      if (code) return code;
127,474✔
6040

6041
      code = tDecodeI32v(pDecoder, &pColData->nData);
127,474✔
6042
      if (code) return code;
127,474✔
6043

6044
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
127,474✔
6045
      if (code) return code;
127,438✔
6046
    } else {
6047
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
32,419,700✔
6048
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
32,425,656✔
6049
      if (code) return code;
32,427,741✔
6050
    }
6051
  }
6052
  pColData->cflag = 0;
32,601,384✔
6053

6054
  return code;
32,598,970✔
6055
}
6056

6057
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
46,525,160✔
6058
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
46,525,160✔
6059
  if (code) return code;
46,514,860✔
6060
  return tEncodeI8(pEncoder, pColData->cflag);
93,043,264✔
6061
}
6062

6063
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
32,597,514✔
6064
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
32,597,514✔
6065
  if (code) return code;
32,597,236✔
6066

6067
  code = tDecodeI8(pDecoder, &pColData->cflag);
32,597,236✔
6068
  return code;
32,602,174✔
6069
}
6070

6071
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
46,513,268✔
6072
  if (version == 0) {
46,513,268✔
6073
    return tEncodeColDataVersion0(pEncoder, pColData);
×
6074
  } else if (version == 1) {
46,513,268✔
6075
    return tEncodeColDataVersion1(pEncoder, pColData);
×
6076
  } else if (version == 2) {
46,513,268✔
6077
    int32_t posStart = pEncoder->pos;
46,512,338✔
6078
    pEncoder->pos += INT_BYTES;
46,516,200✔
6079
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
46,532,827✔
6080
    if (code) return code;
46,526,853✔
6081
    int32_t posEnd = pEncoder->pos;
46,526,853✔
6082
    int32_t pos = posEnd - posStart;
46,529,040✔
6083
    pEncoder->pos = posStart;
46,529,040✔
6084
    code = tEncodeI32(pEncoder, pos);
46,534,257✔
6085
    pEncoder->pos = posEnd;
46,534,257✔
6086
    return code;
46,527,687✔
6087
  } else {
6088
    return TSDB_CODE_INVALID_PARA;
956✔
6089
  }
6090
}
6091

6092
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
47,666,317✔
6093
  if (version == 0) {
47,666,317✔
6094
    return tDecodeColDataVersion0(pDecoder, pColData);
×
6095
  } else if (version == 1) {
47,666,317✔
6096
    return tDecodeColDataVersion1(pDecoder, pColData);
×
6097
  } else if (version == 2) {
47,666,317✔
6098
    if (jump) {
47,666,983✔
6099
      int32_t len = 0;
15,068,416✔
6100
      int32_t code = tDecodeI32(pDecoder, &len);
15,068,417✔
6101
      if (code) return code;
15,068,417✔
6102
      pDecoder->pos += (len - INT_BYTES);
15,068,417✔
6103
      return TSDB_CODE_SUCCESS;
15,068,416✔
6104
    } else {
6105
      pDecoder->pos += INT_BYTES;
32,598,567✔
6106
      return tDecodeColDataVersion1(pDecoder, pColData);
32,600,350✔
6107
    }    
6108
  } else {
UNCOV
6109
    return TSDB_CODE_INVALID_PARA;
×
6110
  }
6111
}
6112

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

6115
int32_t tDecodeRow(SDecoder *pDecoder, SRow **ppRow) {
2,147,483,647✔
6116
  if (ppRow == NULL) {
2,147,483,647✔
6117
    return TSDB_CODE_INVALID_PARA;
×
6118
  }
6119

6120
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
2,147,483,647✔
6121
    return TSDB_CODE_OUT_OF_RANGE;
×
6122
  }
6123

6124
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
2,147,483,647✔
6125
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
2,147,483,647✔
6126
}
6127

6128
int32_t tEncodeBlobSet(SEncoder *pEncoder, SBlobSet *pRow) {
99,455✔
6129
  int32_t code = 0;
99,455✔
6130
  int32_t lino = 0;
99,455✔
6131
  uint8_t compressed = 0;
99,455✔
6132
  int32_t compressSize = 0;
99,455✔
6133
  char   *p = NULL;
99,455✔
6134

6135
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pRow->type));
198,913✔
6136
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pRow->len));
198,916✔
6137

6138
  int32_t nSeq = taosArrayGetSize(pRow->pSeqTable);
99,458✔
6139
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, nSeq));
99,458✔
6140

6141
  // if (pRow->type) {
6142
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
99,458✔
6143
  while (pIter) {
55,166,910✔
6144
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
55,067,452✔
6145
    int32_t  idx = *(int32_t *)pIter;
55,067,452✔
6146
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
55,067,452✔
6147
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
55,067,452✔
6148

6149
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
55,067,452✔
6150
  }
6151
  //}
6152
  for (int32_t i = 0; i < nSeq; i++) {
55,166,910✔
6153
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
55,067,449✔
6154
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
110,134,898✔
6155
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
110,134,898✔
6156
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
110,134,901✔
6157
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
110,134,901✔
6158
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
110,134,901✔
6159
  }
6160

6161
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
198,919✔
6162

6163
_exit:
99,458✔
6164
  return code;
99,458✔
6165
}
6166

6167
int32_t tDecodeBlobSet(SDecoder *pDecoder, SBlobSet **pBlobSet) {
49,413✔
6168
  int32_t    code = 0;
49,413✔
6169
  int32_t    lino = 0;
49,413✔
6170
  int32_t    nSeq = 0;
49,413✔
6171
  uint8_t    compressd = 0;
49,413✔
6172
  int32_t    compressSize = 0;
49,413✔
6173
  SBlobSet  *pBlob = taosMemCalloc(1, sizeof(SBlobSet));
49,413✔
6174
  if (pBlob == NULL) {
49,413✔
6175
    TAOS_CHECK_EXIT(terrno);
×
6176
  }
6177
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pBlob->type));
98,826✔
6178
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pBlob->len));
98,826✔
6179

6180
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
49,413✔
6181

6182
  // if (pBlob->type) {
6183
  pBlob->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
49,413✔
6184
  if (pBlob->pSeqToffset == NULL) {
49,413✔
6185
    TAOS_CHECK_EXIT(terrno);
×
6186
  }
6187
  for (int32_t i = 0; i < nSeq; i++) {
27,581,884✔
6188
    uint64_t seq;
27,532,471✔
6189
    int32_t  idx;
27,532,471✔
6190
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &seq));
27,532,471✔
6191
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
27,532,471✔
6192

6193
    code = taosHashPut(pBlob->pSeqToffset, &seq, sizeof(seq), &idx, sizeof(idx));
27,532,471✔
6194
    TAOS_CHECK_EXIT(code);
27,532,471✔
6195
  }
6196
  //}
6197

6198
  pBlob->pSeqTable = taosArrayInit(nSeq, sizeof(SBlobValue));
49,413✔
6199
  if (pBlob->pSeqTable == NULL) {
49,413✔
6200
    TAOS_CHECK_EXIT(terrno);
×
6201
  }
6202

6203
  for (int32_t i = 0; i < nSeq; i++) {
27,581,884✔
6204
    SBlobValue value;
27,532,471✔
6205
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
27,532,471✔
6206
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
27,532,471✔
6207
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
27,532,471✔
6208
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
27,532,471✔
6209
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
27,532,471✔
6210
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
55,064,942✔
6211
      TAOS_CHECK_EXIT(terrno);
×
6212
    }
6213
  }
6214

6215
  pBlob->data = taosMemCalloc(1, pBlob->len);
49,413✔
6216
  if (pBlob->data == NULL) {
49,413✔
6217
    TAOS_CHECK_EXIT(terrno);
×
6218
  }
6219

6220
  TAOS_CHECK_EXIT(tDecodeFixed(pDecoder, pBlob->data, pBlob->len));
49,413✔
6221
  *pBlobSet = pBlob;
49,413✔
6222

6223
  uTrace("decode blob len:%d", (int32_t)(pBlob->len));
49,413✔
6224

6225
_exit:
49,413✔
6226
  if (code != 0) {
49,413✔
6227
    if (pBlob != NULL) {
×
6228
      taosMemoryFree(pBlob->data);
×
6229
      taosArrayDestroy(pBlob->pSeqTable);
×
6230
      taosMemoryFree(pBlob);
×
6231
    }
6232
  }
6233
  return code;
49,413✔
6234
}
6235

6236
#define CALC_SUM_MAX_MIN(SUM, MAX, MIN, VAL) \
6237
  do {                                       \
6238
    (SUM) += (VAL);                          \
6239
    if ((MAX) < (VAL)) (MAX) = (VAL);        \
6240
    if ((MIN) > (VAL)) (MIN) = (VAL);        \
6241
  } while (0)
6242

6243
static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, SColumnDataAgg *pAggs) {
8,934,139✔
6244
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,934,139✔
6245
  int16_t *numOfNull = &pAggs->numOfNull;
8,934,795✔
6246
  *sum = 0;
8,934,139✔
6247
  *max = 0;
8,934,795✔
6248
  *min = 1;
8,934,795✔
6249
  *numOfNull = 0;
8,934,139✔
6250

6251
  int8_t val;
6252
  if (HAS_VALUE == pColData->flag) {
8,934,139✔
6253
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6254
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
2,147,483,647✔
6255
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6256
    }
6257
  } else {
6258
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6259
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6260
        case 0:
×
6261
        case 1:
6262
          (*numOfNull)++;
×
6263
          break;
×
6264
        case 2:
×
6265
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
×
6266
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
×
6267
          break;
×
6268
        default:
×
6269
          break;
×
6270
      }
6271
    }
6272
  }
6273
}
9,002,031✔
6274

6275
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,952,711✔
6276
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,952,711✔
6277
  int16_t *numOfNull = &pAggs->numOfNull;
8,952,711✔
6278
  *sum = 0;
8,952,043✔
6279
  *max = INT8_MIN;
8,952,043✔
6280
  *min = INT8_MAX;
8,952,043✔
6281
  *numOfNull = 0;
8,952,043✔
6282

6283
  int8_t val;
6284
  if (HAS_VALUE == pColData->flag) {
8,952,043✔
6285
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6286
      val = ((int8_t *)pColData->pData)[iVal];
2,147,483,647✔
6287
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6288
    }
6289
  } else {
6290
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,306,305✔
6291
      switch (tColDataGetBitValue(pColData, iVal)) {
1,305,000✔
6292
        case 0:
1,057,920✔
6293
        case 1:
6294
          (*numOfNull)++;
1,057,920✔
6295
          break;
1,057,920✔
6296
        case 2:
247,080✔
6297
          val = ((int8_t *)pColData->pData)[iVal];
247,080✔
6298
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
247,080✔
6299
          break;
247,080✔
6300
        default:
×
6301
          break;
×
6302
      }
6303
    }
6304
  }
6305
}
9,008,743✔
6306

6307
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,936,607✔
6308
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,936,607✔
6309
  int16_t *numOfNull = &pAggs->numOfNull;
8,936,607✔
6310
  *sum = 0;
8,935,295✔
6311
  *max = INT16_MIN;
8,935,951✔
6312
  *min = INT16_MAX;
8,936,607✔
6313
  *numOfNull = 0;
8,935,951✔
6314

6315
  int16_t val;
6316
  if (HAS_VALUE == pColData->flag) {
8,935,951✔
6317
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6318
      val = ((int16_t *)pColData->pData)[iVal];
2,147,483,647✔
6319
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6320
    }
6321
  } else {
6322
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6323
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6324
        case 0:
×
6325
        case 1:
6326
          (*numOfNull)++;
×
6327
          break;
×
6328
        case 2:
×
6329
          val = ((int16_t *)pColData->pData)[iVal];
×
6330
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
×
6331
          break;
×
6332
        default:
×
6333
          break;
×
6334
      }
6335
    }
6336
  }
6337
}
8,983,880✔
6338

6339
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
29,041,599✔
6340
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
29,041,599✔
6341
  int16_t *numOfNull = &pAggs->numOfNull;
29,044,069✔
6342
  *sum = 0;
29,041,293✔
6343
  *max = INT32_MIN;
29,042,360✔
6344
  *min = INT32_MAX;
29,043,764✔
6345
  *numOfNull = 0;
29,043,749✔
6346

6347
  int32_t val;
6348
  if (HAS_VALUE == pColData->flag) {
29,041,718✔
6349
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6350
      val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6351
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6352
    }
6353
  } else {
6354
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6355
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6356
        case 0:
291,648,319✔
6357
        case 1:
6358
          (*numOfNull)++;
291,648,319✔
6359
          break;
291,648,319✔
6360
        case 2:
2,147,483,647✔
6361
          val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6362
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6363
          break;
2,147,483,647✔
6364
        default:
×
6365
          break;
×
6366
      }
6367
    }
6368
  }
6369
}
26,822,728✔
6370

6371
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
71,578,777✔
6372
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
71,578,777✔
6373
  int16_t *numOfNull = &pAggs->numOfNull;
71,578,777✔
6374
  *sum = 0;
71,577,453✔
6375
  *max = INT64_MIN;
71,578,314✔
6376
  *min = INT64_MAX;
71,578,314✔
6377
  *numOfNull = 0;
71,578,970✔
6378

6379
  int64_t val;
6380
  if (HAS_VALUE == pColData->flag) {
71,578,970✔
6381
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6382
      val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6383
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6384
    }
6385
  } else {
6386
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6387
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6388
        case 0:
321,749,415✔
6389
        case 1:
6390
          (*numOfNull)++;
321,749,415✔
6391
          break;
321,749,415✔
6392
        case 2:
2,147,483,647✔
6393
          val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6394
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6395
          break;
2,147,483,647✔
6396
        default:
×
6397
          break;
×
6398
      }
6399
    }
6400
  }
6401
}
70,328,553✔
6402

6403
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
31,415,253✔
6404
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
31,415,253✔
6405
  int16_t *numOfNull = &pAggs->numOfNull;
31,415,253✔
6406
  *(double *)sum = 0;
31,415,253✔
6407
  *(double *)max = -FLT_MAX;
31,415,253✔
6408
  *(double *)min = FLT_MAX;
31,415,253✔
6409
  *numOfNull = 0;
31,414,639✔
6410

6411
  float val;
6412
  if (HAS_VALUE == pColData->flag) {
31,413,983✔
6413
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6414
      val = ((float *)pColData->pData)[iVal];
2,147,483,647✔
6415
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6416
    }
6417
  } else {
6418
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
11,580,865✔
6419
      switch (tColDataGetBitValue(pColData, iVal)) {
11,563,496✔
6420
        case 0:
2,054,829✔
6421
        case 1:
6422
          (*numOfNull)++;
2,054,829✔
6423
          break;
2,054,829✔
6424
        case 2:
9,508,667✔
6425
          val = ((float *)pColData->pData)[iVal];
9,508,667✔
6426
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
9,508,667✔
6427
          break;
9,508,667✔
6428
        default:
×
6429
          break;
×
6430
      }
6431
    }
6432
  }
6433
}
31,614,513✔
6434

6435
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
16,222,470✔
6436
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
16,222,470✔
6437
  int16_t *numOfNull = &pAggs->numOfNull;
16,226,035✔
6438
  *(double *)sum = 0;
16,224,328✔
6439
  *(double *)max = -DBL_MAX;
16,224,663✔
6440
  *(double *)min = DBL_MAX;
16,225,731✔
6441
  *numOfNull = 0;
16,223,613✔
6442

6443
  double val;
6444
  if (HAS_VALUE == pColData->flag) {
16,224,314✔
6445
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6446
      val = ((double *)pColData->pData)[iVal];
2,147,483,647✔
6447
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6448
    }
6449
  } else {
6450
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,306,305✔
6451
      switch (tColDataGetBitValue(pColData, iVal)) {
1,305,000✔
6452
        case 0:
1,061,835✔
6453
        case 1:
6454
          (*numOfNull)++;
1,061,835✔
6455
          break;
1,061,835✔
6456
        case 2:
243,165✔
6457
          val = ((double *)pColData->pData)[iVal];
243,165✔
6458
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
243,165✔
6459
          break;
243,165✔
6460
        default:
×
6461
          break;
×
6462
      }
6463
    }
6464
  }
6465
}
16,652,501✔
6466

6467
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,574,837✔
6468
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,574,837✔
6469
  int16_t *numOfNull = &pAggs->numOfNull;
8,577,473✔
6470
  *(uint64_t *)sum = 0;
8,576,161✔
6471
  *(uint64_t *)max = 0;
8,576,817✔
6472
  *(uint64_t *)min = UINT8_MAX;
8,576,817✔
6473
  *numOfNull = 0;
8,576,817✔
6474

6475
  uint8_t val;
6476
  if (HAS_VALUE == pColData->flag) {
8,575,493✔
6477
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6478
      val = ((uint8_t *)pColData->pData)[iVal];
2,147,483,647✔
6479
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6480
    }
6481
  } else {
6482
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6483
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6484
        case 0:
×
6485
        case 1:
6486
          (*numOfNull)++;
×
6487
          break;
×
6488
        case 2:
×
6489
          val = ((uint8_t *)pColData->pData)[iVal];
×
6490
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
6491
          break;
×
6492
        default:
×
6493
          break;
×
6494
      }
6495
    }
6496
  }
6497
}
8,622,566✔
6498

6499
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,574,837✔
6500
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,574,837✔
6501
  int16_t *numOfNull = &pAggs->numOfNull;
8,577,473✔
6502
  *(uint64_t *)sum = 0;
8,574,837✔
6503
  *(uint64_t *)max = 0;
8,576,149✔
6504
  *(uint64_t *)min = UINT16_MAX;
8,576,149✔
6505
  *numOfNull = 0;
8,576,149✔
6506

6507
  uint16_t val;
6508
  if (HAS_VALUE == pColData->flag) {
8,574,837✔
6509
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6510
      val = ((uint16_t *)pColData->pData)[iVal];
2,147,483,647✔
6511
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6512
    }
6513
  } else {
6514
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6515
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6516
        case 0:
×
6517
        case 1:
6518
          (*numOfNull)++;
×
6519
          break;
×
6520
        case 2:
×
6521
          val = ((uint16_t *)pColData->pData)[iVal];
×
6522
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
6523
          break;
×
6524
        default:
×
6525
          break;
×
6526
      }
6527
    }
6528
  }
6529
}
8,624,247✔
6530

6531
static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,577,473✔
6532
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,577,473✔
6533
  int16_t *numOfNull = &pAggs->numOfNull;
8,577,473✔
6534
  *(uint64_t *)sum = 0;
8,576,817✔
6535
  *(uint64_t *)max = 0;
8,577,473✔
6536
  *(uint64_t *)min = UINT32_MAX;
8,577,473✔
6537
  *numOfNull = 0;
8,577,473✔
6538

6539
  uint32_t val;
6540
  if (HAS_VALUE == pColData->flag) {
8,577,473✔
6541
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6542
      val = ((uint32_t *)pColData->pData)[iVal];
2,147,483,647✔
6543
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6544
    }
6545
  } else {
6546
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6547
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6548
        case 0:
×
6549
        case 1:
6550
          (*numOfNull)++;
×
6551
          break;
×
6552
        case 2:
×
6553
          val = ((uint32_t *)pColData->pData)[iVal];
×
6554
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
6555
          break;
×
6556
        default:
×
6557
          break;
×
6558
      }
6559
    }
6560
  }
6561
}
8,624,706✔
6562

6563
static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,576,817✔
6564
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,576,817✔
6565
  int16_t *numOfNull = &pAggs->numOfNull;
8,577,473✔
6566
  *(uint64_t *)sum = 0;
8,576,137✔
6567
  *(uint64_t *)max = 0;
8,576,805✔
6568
  *(uint64_t *)min = UINT64_MAX;
8,576,805✔
6569
  *numOfNull = 0;
8,577,473✔
6570

6571
  uint64_t val;
6572
  if (HAS_VALUE == pColData->flag) {
8,577,473✔
6573
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6574
      val = ((uint64_t *)pColData->pData)[iVal];
2,147,483,647✔
6575
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6576
    }
6577
  } else {
6578
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6579
      switch (tColDataGetBitValue(pColData, iVal)) {
×
6580
        case 0:
×
6581
        case 1:
6582
          (*numOfNull)++;
×
6583
          break;
×
6584
        case 2:
×
6585
          val = ((uint64_t *)pColData->pData)[iVal];
×
6586
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
×
6587
          break;
×
6588
        default:
×
6589
          break;
×
6590
      }
6591
    }
6592
  }
6593
}
8,609,696✔
6594

6595
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
32,331,501✔
6596
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
32,331,501✔
6597
  int16_t *numOfNull = &pAggs->numOfNull;
32,332,157✔
6598
  *(uint64_t *)sum = 0;
32,332,157✔
6599
  *(uint64_t *)max = 0;
32,331,501✔
6600
  *(uint64_t *)min = 0;
32,330,845✔
6601
  *numOfNull = 0;
32,329,521✔
6602

6603
  switch (pColData->flag) {
32,329,521✔
6604
    case HAS_NONE:
×
6605
    case HAS_NULL:
6606
    case (HAS_NONE | HAS_NULL):
6607
      *numOfNull = pColData->nVal;
×
6608
      break;
×
6609
    case HAS_VALUE:
32,324,976✔
6610
      *numOfNull = 0;
32,324,976✔
6611
      break;
32,324,976✔
6612
    case (HAS_VALUE | HAS_NULL):
3,889✔
6613
    case (HAS_VALUE | HAS_NONE):
6614
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
9,060,889✔
6615
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
9,057,000✔
6616
          (*numOfNull)++;
1,190,340✔
6617
        }
6618
      }
6619
      break;
3,889✔
6620
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6621
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6622
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6623
          (*numOfNull)++;
×
6624
        }
6625
      }
6626
      break;
×
6627
    default:
×
6628
      break;
×
6629
  }
6630
}
32,328,865✔
6631

6632
#define CALC_DECIMAL_SUM_MAX_MIN(TYPE, pSumOp, pCompOp, pColData, pSum, pMax, pMin)                   \
6633
  do {                                                                                                \
6634
    if (decimal128AddCheckOverflow((Decimal *)pSum, pVal, DECIMAL_WORD_NUM(TYPE))) *pOverflow = true; \
6635
    pSumOp->add(pSum, pVal, DECIMAL_WORD_NUM(TYPE));                                                  \
6636
    if (pCompOp->gt(pVal, pMax, DECIMAL_WORD_NUM(TYPE))) {                                            \
6637
      *(pMax) = *pVal;                                                                                \
6638
    }                                                                                                 \
6639
    if (pCompOp->lt(pVal, pMin, DECIMAL_WORD_NUM(TYPE))) {                                            \
6640
      *(pMin) = *pVal;                                                                                \
6641
    }                                                                                                 \
6642
  } while (0)
6643

6644
static FORCE_INLINE void tColDataCalcSMADecimal64Type(SColData *pColData, SColumnDataAgg *pAggs) {
21,331✔
6645
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum;
21,331✔
6646
  Decimal64  *pMax = (Decimal64 *)pAggs->decimal128Max, *pMin = (Decimal64 *)pAggs->decimal128Min;
21,331✔
6647
  uint8_t    *pOverflow = &pAggs->overflow;
21,331✔
6648
  *pSum = DECIMAL128_ZERO;
21,331✔
6649
  *pMax = DECIMAL64_MIN;
21,331✔
6650
  *pMin = DECIMAL64_MAX;
21,331✔
6651
  pAggs->numOfNull = 0;
21,331✔
6652
  pAggs->colId |= DECIMAL_AGG_FLAG;
21,331✔
6653

6654
  Decimal64         *pVal = NULL;
21,331✔
6655
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
21,331✔
6656
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
21,331✔
6657
  if (HAS_VALUE == pColData->flag) {
21,331✔
6658
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
80,699✔
6659
      pVal = ((Decimal64 *)pColData->pData) + iVal;
79,900✔
6660
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
79,900✔
6661
    }
6662
  } else {
6663
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
15,482,877✔
6664
      switch (tColDataGetBitValue(pColData, iVal)) {
15,460,747✔
6665
        case 0:
659,345✔
6666
        case 1:
6667
          pAggs->numOfNull++;
659,345✔
6668
          break;
655,350✔
6669
        case 2:
14,812,588✔
6670
          pVal = ((Decimal64 *)pColData->pData) + iVal;
14,812,588✔
6671
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
14,822,975✔
6672
          break;
14,806,995✔
6673
        default:
×
6674
          break;
×
6675
      }
6676
    }
6677
  }
6678
}
22,929✔
6679

6680
static FORCE_INLINE void tColDataCalcSMADecimal128Type(SColData *pColData, SColumnDataAgg *pAggs) {
56,037✔
6681
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum, *pMax = (Decimal128 *)pAggs->decimal128Max,
56,037✔
6682
             *pMin = (Decimal128 *)pAggs->decimal128Min;
56,037✔
6683
  uint8_t *pOverflow = &pAggs->overflow;
56,037✔
6684
  *pSum = DECIMAL128_ZERO;
56,037✔
6685
  *pMax = DECIMAL128_MIN;
56,037✔
6686
  *pMin = DECIMAL128_MAX;
56,037✔
6687
  pAggs->numOfNull = 0;
56,037✔
6688
  pAggs->colId |= DECIMAL_AGG_FLAG;
56,037✔
6689

6690
  Decimal128        *pVal = NULL;
56,037✔
6691
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
56,037✔
6692
  if (HAS_VALUE == pColData->flag) {
56,037✔
6693
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
6694
      pVal = ((Decimal128 *)pColData->pData) + iVal;
×
6695
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
×
6696
    }
6697
  } else {
6698
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
50,312,272✔
6699
      switch (tColDataGetBitValue(pColData, iVal)) {
50,253,838✔
6700
        case 0:
2,018,703✔
6701
        case 1:
6702
          pAggs->numOfNull++;
2,018,703✔
6703
          break;
2,009,914✔
6704
        case 2:
48,236,733✔
6705
          pVal = ((Decimal128 *)pColData->pData) + iVal;
48,236,733✔
6706
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
48,247,120✔
6707
          break;
48,246,321✔
6708
        default:
×
6709
          break;
×
6710
      }
6711
    }
6712
  }
6713
}
56,037✔
6714

6715
void (*tColDataCalcSMA[])(SColData *pColData, SColumnDataAgg *pAggs) = {
6716
    NULL,
6717
    tColDataCalcSMABool,            // TSDB_DATA_TYPE_BOOL
6718
    tColDataCalcSMATinyInt,         // TSDB_DATA_TYPE_TINYINT
6719
    tColDataCalcSMATinySmallInt,    // TSDB_DATA_TYPE_SMALLINT
6720
    tColDataCalcSMAInt,             // TSDB_DATA_TYPE_INT
6721
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_BIGINT
6722
    tColDataCalcSMAFloat,           // TSDB_DATA_TYPE_FLOAT
6723
    tColDataCalcSMADouble,          // TSDB_DATA_TYPE_DOUBLE
6724
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARCHAR
6725
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_TIMESTAMP
6726
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_NCHAR
6727
    tColDataCalcSMAUTinyInt,        // TSDB_DATA_TYPE_UTINYINT
6728
    tColDataCalcSMATinyUSmallInt,   // TSDB_DATA_TYPE_USMALLINT
6729
    tColDataCalcSMAUInt,            // TSDB_DATA_TYPE_UINT
6730
    tColDataCalcSMAUBigInt,         // TSDB_DATA_TYPE_UBIGINT
6731
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_JSON
6732
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARBINARY
6733
    tColDataCalcSMADecimal128Type,  // TSDB_DATA_TYPE_DECIMAL
6734
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_BLOB
6735
    NULL,                           // TSDB_DATA_TYPE_MEDIUMBLOB
6736
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_GEOMETRY
6737
    tColDataCalcSMADecimal64Type,   // TSDB_DATA_TYPE_DECIMAL64
6738
};
6739

6740
// SValueColumn ================================
6741
int32_t tValueColumnInit(SValueColumn *valCol) {
1,047,559,433✔
6742
  valCol->type = TSDB_DATA_TYPE_NULL;
1,047,559,433✔
6743
  valCol->numOfValues = 0;
1,047,601,557✔
6744
  tBufferInit(&valCol->data);
1,047,586,541✔
6745
  tBufferInit(&valCol->offsets);
1,047,596,899✔
6746
  return 0;
1,047,679,814✔
6747
}
6748

6749
void tValueColumnDestroy(SValueColumn *valCol) {
2,147,483,647✔
6750
  valCol->type = TSDB_DATA_TYPE_NULL;
2,147,483,647✔
6751
  valCol->numOfValues = 0;
2,147,483,647✔
6752
  tBufferDestroy(&valCol->data);
2,147,483,647✔
6753
  tBufferDestroy(&valCol->offsets);
2,147,483,647✔
6754
  return;
2,147,483,647✔
6755
}
6756

6757
void tValueColumnClear(SValueColumn *valCol) {
1,446,119,733✔
6758
  valCol->type = TSDB_DATA_TYPE_NULL;
1,446,119,733✔
6759
  valCol->numOfValues = 0;
1,446,200,211✔
6760
  tBufferClear(&valCol->data);
1,446,226,406✔
6761
  tBufferClear(&valCol->offsets);
1,446,199,888✔
6762
  return;
1,446,167,264✔
6763
}
6764

6765
int32_t tValueColumnAppend(SValueColumn *valCol, const SValue *value) {
1,937,244✔
6766
  int32_t code;
6767

6768
  if (valCol->numOfValues == 0) {
1,937,244✔
6769
    valCol->type = value->type;
898,811✔
6770
  }
6771

6772
  if (!(value->type == valCol->type)) {
1,937,244✔
6773
    return TSDB_CODE_INVALID_PARA;
×
6774
  }
6775

6776
  if (IS_VAR_DATA_TYPE(value->type)) {
1,937,244✔
6777
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
1,686,716✔
6778
      return code;
×
6779
    }
6780
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
1,686,716✔
6781
      return code;
×
6782
    }
6783
  } else {
6784
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
1,093,177✔
6785
    if (code) return code;
1,093,886✔
6786
  }
6787
  valCol->numOfValues++;
1,937,244✔
6788

6789
  return 0;
1,937,244✔
6790
}
6791

6792
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
61,269,039✔
6793
  int32_t code;
6794

6795
  if (idx < 0 || idx >= valCol->numOfValues) {
61,269,039✔
6796
    return TSDB_CODE_OUT_OF_RANGE;
×
6797
  }
6798

6799
  if (IS_VAR_DATA_TYPE(valCol->type)) {
61,269,207✔
6800
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
1,911,683✔
6801
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
1,911,389✔
6802
    int32_t  oldDataSize = nextOffset - offsets[idx];
1,911,389✔
6803
    int32_t  bytesAdded = value->nData - oldDataSize;
1,911,389✔
6804

6805
    if (bytesAdded != 0) {
1,910,687✔
6806
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
43,708✔
6807
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
21,854✔
6808
              tBufferGetSize(&valCol->data) - nextOffset);
21,854✔
6809
      valCol->data.size += bytesAdded;
21,854✔
6810

6811
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
21,854✔
6812
        offsets[i] += bytesAdded;
×
6813
      }
6814
    }
6815
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
1,910,687✔
6816
  } else {
6817
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
59,357,902✔
6818
                        tDataTypes[valCol->type].bytes);
59,357,692✔
6819
  }
6820
  return 0;
6821
}
6822

6823
int32_t tValueColumnGet(SValueColumn *valCol, int32_t idx, SValue *value) {
343,771,060✔
6824
  if (idx < 0 || idx >= valCol->numOfValues) {
343,771,060✔
6825
    return TSDB_CODE_OUT_OF_RANGE;
×
6826
  }
6827

6828
  value->type = valCol->type;
343,791,828✔
6829
  if (IS_VAR_DATA_TYPE(value->type)) {
441,286,697✔
6830
    int32_t       offset, nextOffset;
97,478,471✔
6831
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
97,486,312✔
6832

6833
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
97,482,093✔
6834
    if (idx == valCol->numOfValues - 1) {
97,479,285✔
6835
      nextOffset = tBufferGetSize(&valCol->data);
20,312,751✔
6836
    } else {
6837
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
77,167,938✔
6838
    }
6839
    value->nData = nextOffset - offset;
97,486,298✔
6840
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
97,483,504✔
6841
  } else {
6842
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
246,316,634✔
6843
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
492,640,130✔
6844
  }
6845
  return 0;
343,812,984✔
6846
}
6847

6848
int32_t tValueColumnCompress(SValueColumn *valCol, SValueColumnCompressInfo *info, SBuffer *output, SBuffer *assist) {
899,522✔
6849
  int32_t code;
6850

6851
  if (!(valCol->numOfValues > 0)) {
899,522✔
6852
    return TSDB_CODE_INVALID_PARA;
×
6853
  }
6854

6855
  (*info) = (SValueColumnCompressInfo){
899,522✔
6856
      .cmprAlg = info->cmprAlg,
899,522✔
6857
      .type = valCol->type,
899,522✔
6858
  };
6859

6860
  // offset
6861
  if (IS_VAR_DATA_TYPE(valCol->type)) {
899,522✔
6862
    SCompressInfo cinfo = {
320,930✔
6863
        .cmprAlg = info->cmprAlg,
320,930✔
6864
        .dataType = TSDB_DATA_TYPE_INT,
6865
        .originalSize = valCol->offsets.size,
320,930✔
6866
    };
6867

6868
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
320,930✔
6869
    if (code) return code;
320,930✔
6870

6871
    info->offsetOriginalSize = cinfo.originalSize;
320,930✔
6872
    info->offsetCompressedSize = cinfo.compressedSize;
320,930✔
6873
  }
6874

6875
  // data
6876
  SCompressInfo cinfo = {
899,522✔
6877
      .cmprAlg = info->cmprAlg,
1,799,006✔
6878
      .dataType = valCol->type,
899,522✔
6879
      .originalSize = valCol->data.size,
899,522✔
6880
  };
6881

6882
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
899,522✔
6883
  if (code) return code;
899,522✔
6884

6885
  info->dataOriginalSize = cinfo.originalSize;
899,522✔
6886
  info->dataCompressedSize = cinfo.compressedSize;
899,522✔
6887

6888
  return 0;
899,522✔
6889
}
6890

6891
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
105,076,756✔
6892
                               SBuffer *assist) {
6893
  int32_t code;
6894

6895
  tValueColumnClear(valCol);
105,076,756✔
6896
  valCol->type = info->type;
105,090,831✔
6897
  // offset
6898
  if (IS_VAR_DATA_TYPE(valCol->type)) {
157,359,459✔
6899
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
52,275,620✔
6900

6901
    SCompressInfo cinfo = {
52,268,607✔
6902
        .dataType = TSDB_DATA_TYPE_INT,
6903
        .cmprAlg = info->cmprAlg,
52,267,919✔
6904
        .originalSize = info->offsetOriginalSize,
52,267,912✔
6905
        .compressedSize = info->offsetCompressedSize,
52,265,771✔
6906
    };
6907

6908
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
52,270,713✔
6909
    if (code) {
52,275,676✔
6910
      return code;
×
6911
    }
6912
  } else {
6913
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
52,811,680✔
6914
  }
6915

6916
  // data
6917
  SCompressInfo cinfo = {
105,090,852✔
6918
      .dataType = valCol->type,
105,088,753✔
6919
      .cmprAlg = info->cmprAlg,
105,092,263✔
6920
      .originalSize = info->dataOriginalSize,
105,091,554✔
6921
      .compressedSize = info->dataCompressedSize,
105,085,938✔
6922
  };
6923

6924
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
105,093,674✔
6925
  if (code) {
105,101,403✔
6926
    return code;
×
6927
  }
6928

6929
  return 0;
105,101,403✔
6930
}
6931

6932
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *info, SBuffer *buffer) {
899,522✔
6933
  int32_t code;
6934
  uint8_t fmtVer = 0;
899,522✔
6935

6936
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
1,799,044✔
6937
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
1,799,044✔
6938
  if ((code = tBufferPutI8(buffer, info->type))) return code;
1,799,044✔
6939
  if (IS_VAR_DATA_TYPE(info->type)) {
899,522✔
6940
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
641,860✔
6941
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
641,860✔
6942
  }
6943
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
1,799,044✔
6944
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
1,799,044✔
6945

6946
  return 0;
899,522✔
6947
}
6948

6949
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
105,083,095✔
6950
  int32_t code;
6951
  uint8_t fmtVer;
105,083,095✔
6952

6953
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
105,084,513✔
6954
  if (fmtVer == 0) {
105,088,739✔
6955
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
105,088,739✔
6956
    if ((code = tBufferGetI8(reader, &info->type))) return code;
105,089,413✔
6957
    if (IS_VAR_DATA_TYPE(info->type)) {
105,097,893✔
6958
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
52,281,994✔
6959
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
52,272,847✔
6960
    } else {
6961
      info->offsetOriginalSize = 0;
52,816,594✔
6962
      info->offsetCompressedSize = 0;
52,815,176✔
6963
    }
6964
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
105,088,732✔
6965
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
105,091,554✔
6966
  } else {
6967
    return TSDB_CODE_INVALID_PARA;
×
6968
  }
6969

6970
  return 0;
105,099,999✔
6971
}
6972

6973
int32_t tCompressData(void          *input,       // input
1,011,193,069✔
6974
                      SCompressInfo *info,        // compress info
6975
                      void          *output,      // output
6976
                      int32_t        outputSize,  // output size
6977
                      SBuffer       *buffer       // assistant buffer provided by caller, can be NULL
6978
) {
6979
  int32_t extraSizeNeeded;
6980
  int32_t code;
6981

6982
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
1,011,193,069✔
6983
  if (!(outputSize >= extraSizeNeeded)) {
1,011,367,619✔
6984
    return TSDB_CODE_INVALID_PARA;
×
6985
  }
6986

6987
  if (info->cmprAlg == NO_COMPRESSION) {
1,011,367,619✔
6988
    (void)memcpy(output, input, info->originalSize);
37,304✔
6989
    info->compressedSize = info->originalSize;
37,304✔
6990
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
1,135,992,728✔
6991
    SBuffer local;
124,787,990✔
6992

6993
    tBufferInit(&local);
6994
    if (buffer == NULL) {
124,675,214✔
6995
      buffer = &local;
×
6996
    }
6997

6998
    if (info->cmprAlg == TWO_STAGE_COMP) {
124,675,214✔
6999
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
124,502,867✔
7000
      if (code) {
124,499,454✔
7001
        tBufferDestroy(&local);
7002
        return code;
×
7003
      }
7004
    }
7005

7006
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
374,017,848✔
7007
        input,                                                   // input
7008
        info->originalSize,                                      // input size
7009
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
124,675,262✔
7010
        output,                                                  // output
7011
        outputSize,                                              // output size
7012
        info->cmprAlg,                                           // compression algorithm
124,674,046✔
7013
        buffer->data,                                            // buffer
7014
        buffer->capacity                                         // buffer size
124,671,275✔
7015
    );
7016
    if (info->compressedSize < 0) {
124,678,712✔
7017
      tBufferDestroy(&local);
7018
      return TSDB_CODE_COMPRESS_ERROR;
×
7019
    }
7020

7021
    tBufferDestroy(&local);
7022
  } else {
7023
    DEFINE_VAR(info->cmprAlg)
886,620,633✔
7024
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
886,779,791✔
7025
      (void)memcpy(output, input, info->originalSize);
7,380✔
7026
      info->compressedSize = info->originalSize;
7,380✔
7027
      return 0;
7,380✔
7028
    }
7029
    SBuffer local;
886,761,874✔
7030

7031
    tBufferInit(&local);
7032
    if (buffer == NULL) {
886,767,223✔
7033
      buffer = &local;
×
7034
    }
7035
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
886,767,223✔
7036

7037
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
2,147,483,647✔
7038
        input,                                                      // input
7039
        info->originalSize,                                         // input size
7040
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
886,769,049✔
7041
        output,                                                     // output
7042
        outputSize,                                                 // output size
7043
        info->cmprAlg,                                              // compression algorithm
7044
        buffer->data,                                               // buffer
7045
        buffer->capacity                                            // buffer size
886,748,333✔
7046
    );
7047
    if (info->compressedSize < 0) {
886,728,404✔
7048
      tBufferDestroy(&local);
7049
      return TSDB_CODE_COMPRESS_ERROR;
×
7050
    }
7051

7052
    tBufferDestroy(&local);
7053
    // new col compress
7054
  }
7055

7056
  return 0;
1,011,382,775✔
7057
}
7058

7059
int32_t tDecompressData(void                *input,       // input
2,147,483,647✔
7060
                        const SCompressInfo *info,        // compress info
7061
                        void                *output,      // output
7062
                        int32_t              outputSize,  // output size
7063
                        SBuffer             *buffer       // assistant buffer provided by caller, can be NULL
7064
) {
7065
  int32_t code;
7066

7067
  if (!(outputSize >= info->originalSize)) {
2,147,483,647✔
7068
    return TSDB_CODE_INVALID_PARA;
×
7069
  }
7070

7071
  if (info->cmprAlg == NO_COMPRESSION) {
2,147,483,647✔
7072
    if (!(info->compressedSize == info->originalSize)) {
20,430✔
7073
      return TSDB_CODE_INVALID_PARA;
×
7074
    }
7075
    (void)memcpy(output, input, info->compressedSize);
20,430✔
7076
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
7077
    SBuffer local;
1,893,320,483✔
7078

7079
    tBufferInit(&local);
7080
    if (buffer == NULL) {
1,893,409,996✔
7081
      buffer = &local;
×
7082
    }
7083

7084
    if (info->cmprAlg == TWO_STAGE_COMP) {
1,893,409,996✔
7085
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,892,271,332✔
7086
      if (code) {
1,892,168,366✔
7087
        tBufferDestroy(&local);
7088
        return code;
×
7089
      }
7090
    }
7091

7092
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
2,147,483,647✔
7093
        input,                                                  // input
7094
        info->compressedSize,                                   // inputSize
1,893,433,930✔
7095
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,893,407,367✔
7096
        output,                                                 // output
7097
        outputSize,                                             // output size
7098
        info->cmprAlg,                                          // compression algorithm
1,893,463,253✔
7099
        buffer->data,                                           // helper buffer
7100
        buffer->capacity                                        // extra buffer size
1,893,427,643✔
7101
    );
7102
    if (decompressedSize < 0) {
1,893,227,803✔
7103
      tBufferDestroy(&local);
7104
      return TSDB_CODE_COMPRESS_ERROR;
×
7105
    }
7106

7107
    if (!(decompressedSize == info->originalSize)) {
1,893,227,803✔
7108
      return TSDB_CODE_COMPRESS_ERROR;
×
7109
    }
7110
    tBufferDestroy(&local);
7111
  } else {
7112
    DEFINE_VAR(info->cmprAlg);
2,014,566,602✔
7113
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
2,014,611,275✔
7114
      (void)memcpy(output, input, info->compressedSize);
12,915✔
7115
      return 0;
12,915✔
7116
    }
7117
    SBuffer local;
2,014,588,500✔
7118

7119
    tBufferInit(&local);
7120
    if (buffer == NULL) {
2,014,628,918✔
7121
      buffer = &local;
×
7122
    }
7123
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
2,014,628,918✔
7124
    if (code) {
2,014,526,547✔
7125
      return code;
×
7126
    }
7127

7128
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
2,147,483,647✔
7129
        input,                                                  // input
7130
        info->compressedSize,                                   // inputSize
2,014,662,965✔
7131
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
2,014,692,112✔
7132
        output,                                                 // output
7133
        outputSize,                                             // output size
7134
        info->cmprAlg,                                          // compression algorithm
2,014,656,005✔
7135
        buffer->data,                                           // helper buffer
7136
        buffer->capacity                                        // extra buffer size
2,014,618,906✔
7137
    );
7138
    if (decompressedSize < 0) {
2,014,450,095✔
7139
      tBufferDestroy(&local);
7140
      return TSDB_CODE_COMPRESS_ERROR;
×
7141
    }
7142

7143
    if (!(decompressedSize == info->originalSize)) {
2,014,450,095✔
7144
      return TSDB_CODE_COMPRESS_ERROR;
×
7145
    }
7146
    tBufferDestroy(&local);
7147
  }
7148

7149
  return 0;
2,147,483,647✔
7150
}
7151

7152
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,011,229,102✔
7153
  int32_t code;
7154

7155
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
1,011,229,102✔
7156
  if (code) return code;
1,011,242,496✔
7157

7158
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,011,242,496✔
7159
  if (code) return code;
1,011,391,101✔
7160

7161
  output->size += info->compressedSize;
1,011,391,101✔
7162
  return 0;
1,011,471,493✔
7163
}
7164

7165
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,893,274,848✔
7166
  int32_t code;
7167

7168
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
1,893,274,848✔
7169
  if (code) return code;
1,892,895,315✔
7170

7171
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,892,895,315✔
7172
  if (code) return code;
1,893,201,223✔
7173

7174
  output->size += info->originalSize;
1,893,201,223✔
7175
  return 0;
1,893,303,814✔
7176
}
7177

7178
// handle all types, including var data
7179
void valueSetDatum(SValue *pVal, int8_t type, void *pDatum, uint32_t len) {
2,147,483,647✔
7180
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647✔
7181
    pVal->pData = pDatum;
62,399,086✔
7182
    pVal->nData = len;
437,755,781✔
7183
  } else {
7184
    switch (len) {
2,147,483,647✔
7185
      case sizeof(uint8_t):
2,147,483,647✔
7186
        pVal->val = *(uint8_t *)pDatum;
2,147,483,647✔
7187
        break;
2,147,483,647✔
7188
      case sizeof(uint16_t):
2,147,483,647✔
7189
        pVal->val = *(uint16_t *)pDatum;
2,147,483,647✔
7190
        break;
2,147,483,647✔
7191
      case sizeof(uint32_t):
2,147,483,647✔
7192
        pVal->val = *(uint32_t *)pDatum;
2,147,483,647✔
7193
        break;
2,147,483,647✔
7194
      case sizeof(uint64_t):
2,147,483,647✔
7195
        pVal->val = *(uint64_t *)pDatum;
2,147,483,647✔
7196
        break;
2,147,483,647✔
7197
      default:
1,035✔
7198
        break;
1,035✔
7199
    }
7200
  }
7201
}
2,147,483,647✔
7202

7203
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
1,813,207,583✔
7204
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
1,813,207,583✔
7205
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
216,836,775✔
7206
    pDst->nData = pSrc->nData;
217,109,937✔
7207
  } else {
7208
    pDst->val = pSrc->val;
1,596,370,808✔
7209
  }
7210
}
1,813,490,046✔
7211
void valueClearDatum(SValue *pVal, int8_t type) {
472,232✔
7212
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
472,232✔
7213
    taosMemoryFreeClear(pVal->pData);
25,080✔
7214
    pVal->nData = 0;
25,080✔
7215
  } else {
7216
    pVal->val = 0;
447,152✔
7217
  }
7218
}
472,232✔
7219

7220
int8_t schemaHasBlob(const STSchema *pSchema) {
793,137,554✔
7221
  if (pSchema == NULL) {
793,137,554✔
7222
    return 0;
×
7223
  }
7224
  for (int i = 0; i < pSchema->numOfCols; ++i) {
2,147,483,647✔
7225
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
2,147,483,647✔
7226
      return 1;
59,742✔
7227
    }
7228
  }
7229
  return 0;
793,268,219✔
7230
}
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