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

taosdata / TDengine / #5049

11 May 2026 06:30AM UTC coverage: 73.313% (+0.09%) from 73.222%
#5049

push

travis-ci

web-flow
feat: refactor taosdump code to improve backup speed and compression ratio (#35292)

6625 of 8435 new or added lines in 28 files covered. (78.54%)

2491 existing lines in 142 files now uncovered.

281233 of 383605 relevant lines covered (73.31%)

132489999.79 hits per line

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

69.26
/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) {
249,830,255✔
79
  int32_t n = 0;
249,830,255✔
80
  n += tPutI8(p ? p + n : p, index->type);
249,830,255✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
249,854,518✔
82
  return n;
249,873,024✔
83
}
84

85
static int32_t tGetPrimaryKeyIndex(uint8_t *p, SPrimaryKeyIndex *index) {
914,221,119✔
86
  int32_t n = 0;
914,221,119✔
87
  n += tGetI8(p + n, &index->type);
914,221,119✔
88
  n += tGetU32v(p + n, &index->offset);
914,295,923✔
89
  return n;
914,388,602✔
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;
83,286,999✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
83,288,785✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
83,280,406✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
83,288,215✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
83,276,435✔
115
    sinfo->numOfPKs++;
83,282,553✔
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
54,775,376✔
122
                             + BSE_SEQUECE_SIZE;                     // value
27,387,886✔
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
54,775,376✔
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
27,387,886✔
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
54,775,376✔
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;
225,648✔
164
        break;
112,824✔
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) {
38,904✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
90,576✔
185
        break;
45,288✔
186
      } else {  // skip useless value
UNCOV
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,147,483,647✔
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,822,006✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
2,822,006✔
216
      sinfo->tupleFixedSize = 0;
2,820,702✔
217
      break;
2,818,746✔
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,231,518,432✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,231,518,432✔
225
      sinfo->tupleFixedSize = schema->flen;
1,231,525,604✔
226
      break;
1,001,115,945✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
83,280,102✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
83,286,885✔
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) {
49,973,228✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
50,083,968✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
50,084,033✔
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;
83,274,991✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
83,277,499✔
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);
76,997,918✔
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,424✔
296
        break;
15,424✔
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
821,733,728✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
765,887,999✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
56,996,674✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
56,998,566✔
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
252,507✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
144✔
325
        break;
144✔
326
      } else {
327
        colValIndex++;
252,363✔
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,785,403✔
336
  SBlobItem item = {.type = type, .len = 0};
2,785,403✔
337
  uint64_t  seq = 0;
2,785,403✔
338
  int32_t   code = tBlobSetPush(pBlobSet, &item, &seq, 0);
2,785,403✔
339
  if (code != 0) return code;
2,785,403✔
340

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

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

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

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

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

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

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

438
        colValIndex++;
90,582✔
439
        break;
90,582✔
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,950✔
450
    return TSDB_CODE_INVALID_PARA;
×
451
  }
452

453
  return 0;
4,950✔
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) {
803,550,129✔
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
803,550,129✔
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,276,237✔
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;
41,192✔
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
45,144✔
637
        break;
45,144✔
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,
27,384,995✔
647
                                      SRow **ppRow, SBlobSet *ppBlobSet) {
648
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
27,384,995✔
649

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

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

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

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

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

678
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
27,384,995✔
679
  int32_t colValIndex = 1;
27,384,995✔
680
  int8_t  firstBlobCol = 1;
27,384,995✔
681
  for (int32_t i = 1; i < schema->numOfCols; i++) {
61,838,606✔
682
    for (;;) {
×
683
      if (colValIndex >= numOfColVals) {  // NONE
34,453,611✔
684
        break;
×
685
      }
686
      uint8_t hasBlob = 0;
34,453,611✔
687

688
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
34,453,611✔
689
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
34,453,611✔
690
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
30,928,014✔
691
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
30,928,014✔
692
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
28,101,341✔
693
              hasBlob = 1;
27,383,438✔
694
            }
695
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
28,101,341✔
696
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
28,101,341✔
697
            if (colValArray[colValIndex].value.nData > 0) {
28,101,341✔
698
              if (hasBlob == 0) {
25,892,233✔
699
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
717,903✔
700
                             colValArray[colValIndex].value.nData);
717,903✔
701
                payloadSize += colValArray[colValIndex].value.nData;
717,903✔
702
              } else {
703
                uint64_t  seq = 0;
25,174,330✔
704
                uint32_t  seqOffset = payloadSize + payload - (*ppRow)->data;
25,174,330✔
705
                SBlobItem item = {.seqOffsetInRow = seqOffset,
25,174,330✔
706
                                  .data = colValArray[colValIndex].value.pData,
25,174,330✔
707
                                  .len = colValArray[colValIndex].value.nData,
25,174,330✔
708
                                  .type = TSDB_DATA_BLOB_VALUE};
709
                int32_t   code = tBlobSetPush(ppBlobSet, &item, &seq, 0);
25,174,330✔
710
                if (code != 0) return code;
25,174,330✔
711
                payloadSize += tPutU64(payload + payloadSize, seq);
50,348,660✔
712
              }
713
            } else {
714
              if (hasBlob) {
2,209,108✔
715
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
2,209,108✔
716
              }
717
            }
718
          } else {
719
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,826,673✔
720
            (void)memcpy(payload + payloadSize,
2,827,465✔
721
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
2,826,673✔
722
                         tDataTypes[schema->columns[i].type].bytes);
2,826,673✔
723
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
2,826,673✔
724
          }
725
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
3,525,597✔
726
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
1,557✔
727
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
1,557✔
728
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
1,557✔
729
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
1,557✔
730
          }
731
        }
732
        colValIndex++;
34,453,611✔
733
        break;
34,453,611✔
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) {
27,384,995✔
743
    return TSDB_CODE_INVALID_PARA;
×
744
  }
745
  return 0;
27,384,995✔
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,955,373✔
1076
                          SRowBuildScanInfo *sinfo) {
1077
  int32_t code;
1078

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

1082
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
27,955,373✔
1083
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
570,378✔
1084
  } else {
1085
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
27,384,995✔
1086
  }
1087

1088
  return code;
27,955,373✔
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) {
51,535✔
1108
  int32_t    lino = 0;
51,535✔
1109
  int32_t    code = 0;
51,535✔
1110
  SBlobSet  *p = taosMemCalloc(1, sizeof(SBlobSet));
51,535✔
1111
  if (p == NULL) {
51,535✔
1112
    return terrno;
×
1113
  }
1114

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

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

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

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

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

1140
  *ppBlobSet = p;
51,535✔
1141
_exit:
51,535✔
1142
  if (code != 0) {
51,535✔
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);
51,535✔
1151
  return code;
51,535✔
1152
}
1153
int32_t tBlobSetPush(SBlobSet *pBlobSet, SBlobItem *pItem, uint64_t *seq, int8_t nextRow) {
27,964,333✔
1154
  if (pBlobSet == NULL || pItem == NULL || seq == NULL) {
27,964,333✔
1155
    return TSDB_CODE_INVALID_PARA;
×
1156
  }
1157
  uTrace("push blob %p, seqOffsetInRow %d, dataLen %d, nextRow %d", pBlobSet, pItem->seqOffsetInRow, pItem->len,
27,964,333✔
1158
         nextRow);
1159
  int32_t  lino = 0;
27,964,333✔
1160
  int32_t  code = 0;
27,964,333✔
1161
  uint64_t offset;
1162
  uint32_t len = pItem->len;
27,964,333✔
1163
  uint8_t *data = pItem->data;
27,964,333✔
1164
  int32_t  dataOffset = pItem->seqOffsetInRow;
27,964,333✔
1165

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

1174
    uint8_t *data = taosMemRealloc(pBlobSet->data, cap);
23,308✔
1175
    if (data == NULL) {
23,308✔
1176
      return terrno;
×
1177
    }
1178
    pBlobSet->data = data;
23,308✔
1179
    pBlobSet->cap = cap;
23,308✔
1180
  }
1181
  if (len > 0) {
27,964,333✔
1182
    (void)memcpy(pBlobSet->data + pBlobSet->len, data, len);
25,178,930✔
1183
  }
1184

1185
  offset = pBlobSet->len;
27,964,333✔
1186
  pBlobSet->len += len;
27,964,333✔
1187

1188
  pBlobSet->seq++;
27,964,333✔
1189
  *seq = pBlobSet->seq;
27,964,333✔
1190

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

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

1202
_exit:
27,964,333✔
1203
  return code;
27,964,333✔
1204
}
1205

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

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

1211
  memcpy(p1, p2, sizeof(SBlobSet));
852✔
1212
  memcpy(p2, &t, sizeof(SBlobSet));
852✔
1213
}
852✔
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,386,005,851✔
1263
  if (pBlobSet == NULL) return 0;
1,386,005,851✔
1264
  return taosArrayGetSize(pBlobSet->pSeqTable);
159,121✔
1265
}
1266

1267
void tBlobSetDestroy(SBlobSet *pBlobSet) {
15,378,566✔
1268
  if (pBlobSet == NULL) return;
15,378,566✔
1269
  uTrace("destroy blob row, seqTable size %p", pBlobSet);
97,796✔
1270
  taosMemoryFree(pBlobSet->data);
97,796✔
1271
  taosArrayDestroy(pBlobSet->pSeqTable);
101,970✔
1272
  taosHashCleanup(pBlobSet->pSeqToffset);
101,970✔
1273
  taosArrayDestroy(pBlobSet->pSet);
101,970✔
1274
  taosMemoryFree(pBlobSet);
101,970✔
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) {
7,326✔
1288
  if (((SBindInfo *)p1)->columnId < ((SBindInfo *)p2)->columnId) {
7,326✔
1289
    return -1;
1,386✔
1290
  } else if (((SBindInfo *)p1)->columnId > ((SBindInfo *)p2)->columnId) {
5,940✔
1291
    return 1;
5,940✔
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,
2,165,948✔
1306
                          SArray *rowArray, bool *pOrdered, bool *pDupTs) {
1307
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
2,165,948✔
1308
    return TSDB_CODE_INVALID_PARA;
10✔
1309
  }
1310

1311
  if (!infoSorted) {
2,172,174✔
1312
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1313
  }
1314

1315
  int32_t code = 0;
2,172,174✔
1316
  int32_t numOfRows = infos[0].bind->num;
2,172,174✔
1317
  SArray *colValArray;
1318
  SColVal colVal;
2,148,032✔
1319

1320
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
2,171,947✔
1321
    return terrno;
×
1322
  }
1323

1324
  SRowKey rowKey, lastRowKey;
2,147,892✔
1325
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
18,222,086✔
1326
    taosArrayClear(colValArray);
16,437,253✔
1327

1328
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
84,242,144✔
1329
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
67,919,981✔
1330
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
1,480✔
1331
      } else {
1332
        SValue value = {
67,926,586✔
1333
            .type = infos[iInfo].type,
67,922,904✔
1334
        };
1335
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
67,936,356✔
1336
          value.nData = infos[iInfo].bind->length[iRow];
4,829,258✔
1337
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
4,875,982✔
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,876,940✔
1342
        } else {
1343
          valueSetDatum(&value, infos[iInfo].type,
63,141,454✔
1344
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
63,155,431✔
1345
                        infos[iInfo].bind->buffer_length);
63,155,762✔
1346
        }
1347
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
68,694,179✔
1348
      }
1349
      if (taosArrayPush(colValArray, &colVal) == NULL) {
67,923,760✔
1350
        code = terrno;
×
1351
        goto _exit;
×
1352
      }
1353
    }
1354

1355
    SRow             *row;
4,409,698✔
1356
    SRowBuildScanInfo sinfo = {0};
16,292,150✔
1357
    if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
16,293,001✔
1358
      goto _exit;
×
1359
    }
1360

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

1366
    if (pOrdered && pDupTs) {
16,058,162✔
1367
      tRowGetKey(row, &rowKey);
32,240,063✔
1368
      if (iRow == 0) {
16,110,158✔
1369
        *pOrdered = true;
2,170,268✔
1370
        *pDupTs = false;
2,170,436✔
1371
      } else {
1372
        if (*pOrdered) {
13,939,890✔
1373
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
13,976,440✔
1374
          *pOrdered = (res >= 0);
13,976,440✔
1375
          if (!*pDupTs) {
13,978,554✔
1376
            *pDupTs = (res == 0);
13,919,415✔
1377
          }
1378
        }
1379
      }
1380
      lastRowKey = rowKey;
16,109,398✔
1381
    }
1382
  }
1383

1384
_exit:
2,153,475✔
1385
  taosArrayDestroy(colValArray);
2,152,863✔
1386
  return code;
2,171,523✔
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,564,258✔
1405
    return 0;
20,563,779✔
1406
  }
1407

1408
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
1409
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
579,476,543✔
1410
    return 0;
579,166,877✔
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);
43,732,043✔
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);
1✔
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;
11,423,654✔
1463
            }
1464
            if (isBlob == 1) {
2,147,483,647✔
1465
              pData += BSE_SEQUECE_SIZE;  // skip seq
34,125,656✔
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,986,460✔
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,965,671✔
1506
      return 0;
1,965,671✔
1507
    } else if (bit == BIT_FLG_NULL) {
2,147,483,647✔
1508
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
2,003,425,628✔
1509
      return 0;
2,003,442,863✔
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) {
470,027,598✔
1535
  SRowKey key1, key2;
469,999,946✔
1536
  tRowGetKey(*(SRow **)p1, &key1);
942,730,789✔
1537
  tRowGetKey(*(SRow **)p2, &key2);
945,317,956✔
1538
  return tRowKeyCompare(&key1, &key2);
473,873,406✔
1539
}
1540
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
45,533,785✔
1541

1542
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
94,817,323✔
1543
  SColVal* pColVal = tRowIterNext(iter);
94,817,323✔
1544
  while (pColVal != NULL && pColVal->cid < targetCid) {
123,143,953✔
1545
    pColVal = tRowIterNext(iter);
28,322,854✔
1546
  }
1547
  return pColVal;
94,821,231✔
1548
}
1549

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

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

1564
  for (int32_t i = 0; i < nRow; i++) {
51,322,531✔
1565
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
45,535,981✔
1566

1567
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
45,535,141✔
1568
    if (code) goto _exit;
45,536,137✔
1569
  }
1570

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

1578
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
77,767,848✔
1579
    int32_t targetCid = pTSchema->columns[iCol].colId;
71,974,850✔
1580
    SColVal *pColVal = NULL;
71,974,230✔
1581

1582
    switch (strategy) {
71,974,230✔
1583
      case KEEP_CONSISTENCY:
4,953,428✔
1584
        if (nRow > 0)
4,953,428✔
1585
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
4,953,428✔
1586
        break;
4,953,428✔
1587

1588
      default:  // default using PREFER_NON_NULL strategy
67,020,802✔
1589
      case PREFER_NON_NULL:
1590
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
109,531,948✔
1591
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
89,864,987✔
1592

1593
          if (COL_VAL_IS_VALUE(pColValT)) {
89,866,907✔
1594
            pColVal = pColValT;
47,356,089✔
1595
            break;
47,356,089✔
1596
          } else if (pColVal == NULL) {
42,511,146✔
1597
            pColVal = pColValT;
21,524,029✔
1598
          }
1599
        }
1600
        break;
67,023,050✔
1601
    }
1602

1603
    if (pColVal) {
71,976,478✔
1604
      if (taosArrayPush(aColVal, pColVal) == NULL) {
71,976,362✔
1605
        code = terrno;
×
1606
        goto _exit;
×
1607
      }
1608
    }
1609
  }
1610

1611
  // build
1612
  SRowBuildScanInfo sinfo = {0};
5,782,870✔
1613
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
5,786,318✔
1614

1615
  if (code) goto _exit;
5,785,454✔
1616

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

1623
_exit:
5,786,426✔
1624
  if (aIter) {
5,786,422✔
1625
    for (int32_t i = 0; i < nRow; i++) {
51,323,431✔
1626
      tRowIterClose(&aIter[i]);
45,536,949✔
1627
    }
1628
    taosMemoryFree(aIter);
5,786,482✔
1629
  }
1630
  if (aColVal) taosArrayDestroy(aColVal);
5,786,502✔
1631
  if (code) tRowDestroy(pRow);
5,786,374✔
1632
  return code;
5,786,374✔
1633
}
1634
static int32_t tBlobSetTransferTo(SBlobSet *pSrc, SBlobSet *pDst, SColVal *pVal) {
8,520✔
1635
  int32_t code = 0;
8,520✔
1636
  int32_t lino = 0;
8,520✔
1637
  if (COL_VAL_IS_NULL(pVal) || pVal->value.pData == NULL) {
17,040✔
1638
    int8_t type = COL_VAL_IS_NULL(pVal) ? TSDB_DATA_BLOB_NULL_VALUE : TSDB_DATA_BLOB_EMPTY_VALUE;
8,520✔
1639
    code = addEmptyItemToBlobSet(pDst, type, NULL);
8,520✔
1640
    TAOS_CHECK_GOTO(code, &lino, _error);
8,520✔
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,520✔
1661
  return code;
8,520✔
1662
}
1663

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

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

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

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

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

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

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

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

1712
  tBlobSetSwap(pBlob, pTempBlob);
852✔
1713

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

1719
  if (aIter) {
852✔
1720
    for (int32_t i = 0; i < nRow; i++) {
9,372✔
1721
      tRowIterClose(&aIter[i]);
8,520✔
1722
    }
1723
    taosMemoryFree(aIter);
852✔
1724
  }
1725
  if (aColVal) {
852✔
1726
    taosArrayDestroy(aColVal);
852✔
1727
  }
1728
  tBlobSetDestroy(pTempBlob);
852✔
1729
  return code;
852✔
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,688,097✔
1876
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
1,688,097✔
1877
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
1,681,575✔
1878
  if (code != TSDB_CODE_SUCCESS) {
1,681,575✔
1879
    uError("taosArrayMSort failed caused by %d", code);
×
1880
  }
1881
  return code;
1,681,575✔
1882
}
1883

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

1887
  int32_t iStart = 0;
1,841,117✔
1888
  while (iStart < aRowP->size) {
35,862,210✔
1889
    SRowKey key1;
34,003,155✔
1890
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
34,037,077✔
1891

1892
    tRowGetKey(row1, &key1);
68,027,003✔
1893

1894
    int32_t iEnd = iStart + 1;
34,018,604✔
1895
    while (iEnd < aRowP->size) {
73,764,152✔
1896
      SRowKey key2;
71,907,785✔
1897
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
71,918,820✔
1898
      tRowGetKey(row2, &key2);
143,822,357✔
1899

1900
      if (tRowKeyCompare(&key1, &key2) != 0) break;
71,929,648✔
1901

1902
      iEnd++;
39,754,088✔
1903
    }
1904

1905
    if (iEnd - iStart > 1) {
34,017,670✔
1906
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
5,786,226✔
1907
      if (code) return code;
5,786,194✔
1908
    }
1909

1910
    // the array is also changing, so the iStart just ++ instead of iEnd
1911
    iStart++;
34,017,638✔
1912
  }
1913

1914
  return code;
1,841,104✔
1915
}
1916

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

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

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

1936
    tRowGetKey(row1, &key1);
17,040✔
1937

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

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

1946
      iEnd++;
×
1947
    }
1948

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

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

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

1968
    code = tRowMergeAndRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1969
  }
1970
  return code;
852✔
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) {
51,361,767✔
1994
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
51,361,767✔
1995

1996
  int32_t code = 0;
51,362,955✔
1997

1998
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
51,362,955✔
1999
  if (pIter == NULL) {
51,363,595✔
2000
    code = terrno;
×
2001
    goto _exit;
×
2002
  }
2003

2004
  pIter->pRow = pRow;
51,363,595✔
2005
  pIter->pTSchema = pTSchema;
51,363,359✔
2006
  pIter->iTColumn = 0;
51,363,531✔
2007

2008
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
51,362,743✔
2009

2010
  uint8_t         *data = pRow->data;
50,964,292✔
2011
  SPrimaryKeyIndex index;
50,954,774✔
2012
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
53,905,749✔
2013
    data += tGetPrimaryKeyIndex(data, &index);
2,940,749✔
2014
  }
2015

2016
  if (pRow->flag >> 4) {
50,964,932✔
2017
    pIter->iCol = 0;
7,904,765✔
2018
    pIter->pIdx = (SKVIdx *)data;
7,904,765✔
2019
    if (pRow->flag & KV_FLG_LIT) {
7,904,765✔
2020
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
7,904,765✔
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) {
43,061,027✔
2028
      case (HAS_NULL | HAS_NONE):
3,148✔
2029
        pIter->pb = data;
3,148✔
2030
        break;
3,148✔
2031
      case HAS_VALUE:
40,003,409✔
2032
        pIter->pf = data;
40,003,409✔
2033
        pIter->pv = pIter->pf + pTSchema->flen;
40,003,489✔
2034
        break;
40,003,793✔
2035
      case (HAS_VALUE | HAS_NONE):
3,054,510✔
2036
      case (HAS_VALUE | HAS_NULL):
2037
        pIter->pb = data;
3,054,510✔
2038
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
3,054,510✔
2039
        pIter->pv = pIter->pf + pTSchema->flen;
3,054,510✔
2040
        break;
3,054,510✔
UNCOV
2041
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
UNCOV
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:
51,363,847✔
2052
  if (code) {
51,363,463✔
2053
    *ppIter = NULL;
×
2054
  } else {
2055
    *ppIter = pIter;
51,363,463✔
2056
  }
2057
  return code;
51,362,579✔
2058
}
2059

2060
void tRowIterClose(SRowIter **ppIter) {
51,362,079✔
2061
  SRowIter *pIter = *ppIter;
51,362,079✔
2062
  if (pIter) {
51,363,999✔
2063
    taosMemoryFree(pIter);
51,364,043✔
2064
  }
2065
  *ppIter = NULL;
51,364,247✔
2066
}
51,364,327✔
2067

2068
SColVal *tRowIterNext(SRowIter *pIter) {
224,432,816✔
2069
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
224,432,816✔
2070
    return NULL;
4,885,378✔
2071
  }
2072

2073
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
219,550,054✔
2074

2075
  // timestamp
2076
  if (0 == pIter->iTColumn) {
219,553,242✔
2077
    pIter->cv.cid = pTColumn->colId;
15,441,513✔
2078
    pIter->cv.value.type = pTColumn->type;
15,441,437✔
2079
    pIter->cv.flag = CV_FLAG_VALUE;
15,441,549✔
2080
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
15,441,337✔
2081
    goto _exit;
15,441,037✔
2082
  }
2083

2084
  if (pIter->pRow->flag == HAS_NONE) {
204,113,165✔
2085
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
5,827,230✔
2086
    goto _exit;
5,827,230✔
2087
  }
2088

2089
  if (pIter->pRow->flag == HAS_NULL) {
198,284,699✔
2090
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
376,148✔
2091
    goto _exit;
376,148✔
2092
  }
2093

2094
  if (pIter->pRow->flag >> 4) {  // KV
197,908,203✔
2095
    if (pIter->iCol < pIter->pIdx->nCol) {
159,148,106✔
2096
      uint8_t *pData;
2097

2098
      if (pIter->pRow->flag & KV_FLG_LIT) {
71,759,810✔
2099
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
71,759,810✔
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,759,810✔
2107
      pData += tGetI16v(pData, &cid);
71,759,810✔
2108

2109
      if (TABS(cid) == pTColumn->colId) {
71,759,810✔
2110
        if (cid < 0) {
70,339,530✔
2111
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
3,679,244✔
2112
        } else {
2113
          pIter->cv.cid = pTColumn->colId;
66,660,286✔
2114
          pIter->cv.value.type = pTColumn->type;
66,660,286✔
2115
          pIter->cv.flag = CV_FLAG_VALUE;
66,660,286✔
2116

2117
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
66,660,286✔
2118
            pData += tGetU32v(pData, &pIter->cv.value.nData);
25,828,850✔
2119
            if (pIter->cv.value.nData > 0) {
12,914,425✔
2120
              pIter->cv.value.pData = pData;
12,912,919✔
2121
            } else {
2122
              pIter->cv.value.pData = NULL;
1,506✔
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,745,861✔
2129
          }
2130
        }
2131

2132
        pIter->iCol++;
70,339,530✔
2133
        goto _exit;
70,339,530✔
2134
      } else if (TABS(cid) > pTColumn->colId) {
1,420,280✔
2135
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,420,280✔
2136
        goto _exit;
1,420,280✔
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);
87,388,296✔
2143
      goto _exit;
87,388,296✔
2144
    }
2145
  } else {  // Tuple
2146
    uint8_t bv = BIT_FLG_VALUE;
38,760,809✔
2147
    if (pIter->pb) {
38,760,809✔
2148
      switch (pIter->pRow->flag) {
16,162,679✔
2149
        case (HAS_NULL | HAS_NONE):
28,448✔
2150
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
28,448✔
2151
          break;
28,448✔
2152
        case (HAS_VALUE | HAS_NONE):
9,856,983✔
2153
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
9,856,983✔
2154
          if (bv) bv++;
9,856,983✔
2155
          break;
9,856,983✔
2156
        case (HAS_VALUE | HAS_NULL):
6,277,248✔
2157
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
6,277,248✔
2158
          break;
6,277,248✔
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) {
16,162,679✔
2167
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,014,150✔
2168
        goto _exit;
1,014,150✔
2169
      } else if (bv == BIT_FLG_NULL) {
15,148,529✔
2170
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,107,050✔
2171
        goto _exit;
1,107,050✔
2172
      }
2173
    }
2174

2175
    pIter->cv.cid = pTColumn->colId;
36,638,725✔
2176
    pIter->cv.value.type = pTColumn->type;
36,638,125✔
2177
    pIter->cv.flag = CV_FLAG_VALUE;
36,639,485✔
2178
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
41,217,005✔
2179
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
4,576,176✔
2180
      pData += tGetU32v(pData, &pIter->cv.value.nData);
9,155,890✔
2181
      if (pIter->cv.value.nData > 0) {
4,577,920✔
2182
        pIter->cv.value.pData = pData;
4,572,636✔
2183
      } else {
2184
        pIter->cv.value.pData = NULL;
5,440✔
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]);
32,061,257✔
2191
    }
2192
    goto _exit;
36,634,721✔
2193
  }
2194

2195
_exit:
219,548,442✔
2196
  pIter->iTColumn++;
219,548,442✔
2197
  return &pIter->cv;
219,548,994✔
2198
}
2199

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

2203
  if (flag) return code;
812,801✔
2204

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

2210
  return code;
534,215✔
2211
}
2212
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
64,079,520✔
2213
  int32_t code = 0;
64,079,520✔
2214

2215
  int32_t   iColData = 0;
64,079,520✔
2216
  SColData *pColData = &aColData[iColData];
64,079,520✔
2217
  int32_t   iTColumn = 1;
64,080,022✔
2218
  STColumn *pTColumn = &pSchema->columns[iTColumn];
64,080,022✔
2219

2220
  while (pColData) {
422,688,570✔
2221
    if (pTColumn) {
358,608,198✔
2222
      if (pTColumn->colId == pColData->cid) {  // NULL
358,608,198✔
2223
        if (flag == 0) {
358,614,222✔
2224
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
358,614,222✔
2225
        } else {
2226
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2227
        }
2228
        if (code) goto _exit;
358,592,134✔
2229

2230
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
358,592,134✔
2231
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
358,591,632✔
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:
64,080,372✔
2245
  return code;
64,080,372✔
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);
70,324,045✔
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):
196,668✔
2269
      pb = data;
196,668✔
2270
      break;
196,668✔
2271
    case (HAS_VALUE | HAS_NONE):
460,597,996✔
2272
    case (HAS_VALUE | HAS_NULL):
2273
      pb = data;
460,597,996✔
2274
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
460,597,996✔
2275
      pv = pf + pTSchema->flen;
460,576,751✔
2276
      break;
460,581,952✔
UNCOV
2277
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
UNCOV
2278
      pb = data;
×
UNCOV
2279
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2280
      pv = pf + pTSchema->flen;
×
2281
      break;
×
UNCOV
2282
    default:
×
UNCOV
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,952,161,109✔
2295
            case (HAS_NULL | HAS_NONE):
3,884,894✔
2296
              bv = GET_BIT1(pb, iTColumn - 1);
3,884,894✔
2297
              break;
3,884,894✔
2298
            case (HAS_VALUE | HAS_NONE):
68,023,212✔
2299
              bv = GET_BIT1(pb, iTColumn - 1);
68,023,212✔
2300
              if (bv) bv++;
68,023,805✔
2301
              break;
68,023,805✔
2302
            case (HAS_VALUE | HAS_NULL):
1,880,184,724✔
2303
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
1,880,184,724✔
2304
              break;
1,880,421,234✔
2305
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
113,402✔
2306
              bv = GET_BIT2(pb, iTColumn - 1);
113,402✔
2307
              break;
×
2308
            default:
×
2309
              return TSDB_CODE_INVALID_DATA_FMT;
×
2310
          }
2311

2312
          if (bv == BIT_FLG_NONE) {
1,952,329,933✔
2313
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
8,577,309✔
2314
              goto _exit;
×
2315
            goto _continue;
8,576,716✔
2316
          } else if (bv == BIT_FLG_NULL) {
1,943,752,624✔
2317
            if (flag == 0) {
552,143,303✔
2318
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
551,821,828✔
2319
            } else {
2320
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
321,475✔
2321
            }
2322
            if (code) goto _exit;
552,147,731✔
2323
            goto _continue;
552,147,731✔
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);
547,785,644✔
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,408,489,614✔
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
68,921,079✔
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;
68,918,370✔
2356
      }
2357
    } else {
2358
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
43,134,317✔
2359
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
43,134,317✔
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,941,575,862✔
2367
  int32_t code = 0;
1,941,575,862✔
2368

2369
  uint8_t  *pv = NULL;
1,941,575,862✔
2370
  int32_t   iColData = 0;
1,941,575,862✔
2371
  SColData *pColData = &aColData[iColData];
1,941,575,862✔
2372
  int32_t   iTColumn = 1;
1,941,680,348✔
2373
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,941,680,348✔
2374
  int32_t   iCol = 0;
1,941,697,876✔
2375

2376
  // primary keys
2377
  uint8_t         *data = pRow->data;
1,941,697,876✔
2378
  SPrimaryKeyIndex index;
1,941,716,483✔
2379
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,947,605,849✔
2380
    data += tGetPrimaryKeyIndex(data, &index);
5,892,124✔
2381
  }
2382

2383
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,941,688,065✔
2384
  if (pRow->flag & KV_FLG_LIT) {
1,941,688,065✔
2385
    pv = pKVIdx->idx + pKVIdx->nCol;
1,929,720,750✔
2386
  } else if (pRow->flag & KV_FLG_MID) {
11,972,746✔
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
11,972,402✔
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) {
325,746,852✔
2402
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
326,232,874✔
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);
39,700✔
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,049,554,239✔
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);
4,968,297✔
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,923,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,923,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,923,000✔
2453
      }
2454
    } else {
2455
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
8,780✔
2456
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
8,780✔
2457
    }
2458
  }
2459

2460
_exit:
1,942,204,263✔
2461
  return code;
1,941,628,155✔
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);
812,801✔
2473
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2474
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
64,080,424✔
2475
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2476
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,941,669,050✔
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) {
791,427,553✔
2483
  key->numOfPKs = row->numOfPKs;
791,427,553✔
2484

2485
  if (key->numOfPKs == 0) {
790,835,199✔
2486
    return;
×
2487
  }
2488

2489
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
785,119,051✔
2490

2491
  uint8_t *data = row->data;
791,489,171✔
2492

2493
  for (int32_t i = 0; i < row->numOfPKs; i++) {
1,582,996,954✔
2494
    data += tGetPrimaryKeyIndex(data, &indices[i]);
791,472,742✔
2495
  }
2496

2497
  // primary keys
2498
  for (int32_t i = 0; i < row->numOfPKs; i++) {
1,582,971,259✔
2499
    key->pks[i].type = indices[i].type;
791,501,523✔
2500

2501
    uint8_t *tdata = data + indices[i].offset;
791,501,898✔
2502
    if (row->flag >> 4) {
791,501,484✔
2503
      tdata += tGetI16v(tdata, NULL);
150,859,464✔
2504
    }
2505

2506
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
791,494,391✔
2507
      key->pks[i].pData = tdata;
141,433,325✔
2508
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
282,893,532✔
2509
    } else {
2510
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
650,062,430✔
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,265,085,726✔
2527
  switch (tv1->type) {
1,265,085,726✔
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:
555,371,679✔
2534
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
555,371,679✔
2535
    case TSDB_DATA_TYPE_BIGINT:
152,045,324✔
2536
    case TSDB_DATA_TYPE_TIMESTAMP:
2537
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
152,045,324✔
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:
151,540,006✔
2547
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
151,540,006✔
2548
    case TSDB_DATA_TYPE_UBIGINT:
149,968,539✔
2549
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
149,968,539✔
2550
    case TSDB_DATA_TYPE_GEOMETRY:
256,176,528✔
2551
    case TSDB_DATA_TYPE_BINARY: {
2552
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
256,176,528✔
2553
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
256,175,146✔
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,265,105,776✔
2584
      if (ret) return ret;
1,265,121,344✔
2585
    }
2586
  } else if (key1->numOfPKs < key2->numOfPKs) {
4,404✔
2587
    return -1;
4,380✔
2588
  } else {
2589
    return 1;
24✔
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) {
2,147,483,647✔
2601
      SValue *pVal = &pDst->pks[i];
1,446,363,164✔
2602
      pVal->type = pSrc->pks[i].type;
1,446,407,451✔
2603

2604
      valueCloneDatum(pVal, pSrc->pks + i, pVal->type);
1,446,406,760✔
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,799,874✔
2620
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
4,799,874✔
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) {
398,966,697✔
2717
  int32_t n = 0;
398,966,697✔
2718

2719
  // key
2720
  if (isJson) {
398,966,697✔
2721
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
2,794,044✔
2722
  } else {
2723
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
795,119,996✔
2724
  }
2725

2726
  // type
2727
  n += tPutI8(p ? p + n : p, pTagVal->type);
398,947,343✔
2728

2729
  // value
2730
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
398,986,642✔
2731
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
339,560,201✔
2732
  } else {
2733
    p = p ? p + n : p;
229,212,431✔
2734
    n += tDataTypes[pTagVal->type].bytes;
229,216,594✔
2735
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
229,211,026✔
2736
  }
2737

2738
  return n;
398,944,404✔
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,871,741✔
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,817,161✔
2765

2766
bool tTagIsJsonNull(void *data) {
1,469,522✔
2767
  STag  *pTag = (STag *)data;
1,469,522✔
2768
  int8_t isJson = tTagIsJson(pTag);
1,469,522✔
2769
  if (!isJson) return false;
1,467,339✔
2770
  return ((STag *)data)->nTag == 0;
830,186✔
2771
}
2772

2773
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag) {
82,724,512✔
2774
  int32_t  code = 0;
82,724,512✔
2775
  uint8_t *p = NULL;
82,724,512✔
2776
  int16_t  n = 0;
82,724,512✔
2777
  int16_t  nTag = taosArrayGetSize(pArray);
82,724,512✔
2778
  int32_t  szTag = 0;
82,721,381✔
2779
  int8_t   isLarge = 0;
82,721,381✔
2780

2781
  // sort
2782
  if (isJson) {
82,721,381✔
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
455,783✔
2784
  } else {
2785
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
82,265,598✔
2786
  }
2787

2788
  // get size
2789
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
282,132,416✔
2790
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
199,478,840✔
2791
  }
2792
  if (szTag <= INT8_MAX) {
82,653,576✔
2793
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
73,764,035✔
2794
  } else {
2795
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
8,889,585✔
2796
    isLarge = 1;
8,889,585✔
2797
  }
2798

2799
  // build tag
2800
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
82,653,576✔
2801
  if ((*ppTag) == NULL) {
82,710,463✔
2802
    code = terrno;
×
2803
    goto _err;
×
2804
  }
2805
  (*ppTag)->flags = 0;
82,684,924✔
2806
  if (isJson) {
82,711,999✔
2807
    (*ppTag)->flags |= TD_TAG_JSON;
455,783✔
2808
  }
2809
  if (isLarge) {
82,711,999✔
2810
    (*ppTag)->flags |= TD_TAG_LARGE;
8,898,114✔
2811
  }
2812
  (*ppTag)->len = szTag;
82,711,999✔
2813
  (*ppTag)->nTag = nTag;
82,683,831✔
2814
  (*ppTag)->ver = version;
82,671,253✔
2815

2816
  if (isLarge) {
82,669,638✔
2817
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
8,898,114✔
2818
  } else {
2819
    p = (uint8_t *)&(*ppTag)->idx[nTag];
73,771,524✔
2820
  }
2821
  n = 0;
82,697,472✔
2822
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
282,158,060✔
2823
    if (isLarge) {
199,433,820✔
2824
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
52,276,676✔
2825
    } else {
2826
      (*ppTag)->idx[iTag] = n;
147,157,144✔
2827
    }
2828
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
199,460,018✔
2829
  }
2830
#ifdef TD_DEBUG_PRINT_TAG
2831
  debugPrintSTag(*ppTag, __func__, __LINE__);
2832
#endif
2833

2834
  return code;
82,724,240✔
2835

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

2840
void tTagFree(STag *pTag) {
1,224,526,837✔
2841
  if (pTag) taosMemoryFree(pTag);
1,224,526,837✔
2842
}
1,224,526,837✔
2843

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

2849
  char  *data = NULL;
994,646,212✔
2850
  int8_t typeBytes = 0;
994,646,212✔
2851
  if (isJson) {
994,646,212✔
2852
    typeBytes = CHAR_BYTES;
2,445,033✔
2853
  }
2854

2855
  if (IS_VAR_DATA_TYPE(value->type)) {
994,646,212✔
2856
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
343,098,859✔
2857
    if (data == NULL) {
342,991,973✔
2858
      return NULL;
×
2859
    }
2860

2861
    if (isJson) {
342,991,973✔
2862
      *data = value->type;
1,853,001✔
2863
    }
2864

2865
    varDataLen(data + typeBytes) = value->nData;
342,991,973✔
2866
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
342,994,422✔
2867
  } else {
2868
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
651,752,497✔
2869
  }
2870

2871
  return data;
994,822,128✔
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];
815,448,696✔
2891
  } else {
2892
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,853,070,771✔
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,219,431✔
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;
62,337,180✔
2923
}
2924

2925
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
305,314,668✔
2926
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
610,671,403✔
2927
}
2928

2929
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
2,134,148,990✔
2930

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

2938
  if (isLarge) {
1,019,251✔
2939
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
4,439✔
2940
  } else {
2941
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,014,812✔
2942
  }
2943

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

2950
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
2,588,696✔
2951
    if (isLarge) {
1,569,445✔
2952
      offset = ((int16_t *)pTag->idx)[iTag];
5,945✔
2953
    } else {
2954
      offset = pTag->idx[iTag];
1,563,500✔
2955
    }
2956
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,569,445✔
2957
    if (taosArrayPush(*ppArray, &tv) == NULL) {
3,138,890✔
2958
      code = terrno;
×
2959
      goto _err;
×
2960
    }
2961
  }
2962

2963
  return code;
1,019,251✔
2964

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

2969

2970
void destroyTagVal(void *pTag) {
13,730,176✔
2971
  STagVal* pTagVal = (STagVal*)pTag;
13,730,176✔
2972
  if (pTagVal && IS_VAR_DATA_TYPE(pTagVal->type)) {
13,730,176✔
2973
    taosMemoryFree(pTagVal->pData);
5,990,880✔
2974
  }
2975
}
13,730,176✔
2976

2977
// STSchema ========================================
2978
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
1,116,894,462✔
2979
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
1,116,894,462✔
2980
  if (pTSchema == NULL) {
1,116,673,113✔
2981
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2982
    return NULL;
×
2983
  }
2984

2985
  pTSchema->numOfCols = numOfCols;
1,116,673,113✔
2986
  pTSchema->version = version;
1,116,705,857✔
2987

2988
  // timestamp column
2989
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
1,116,798,092✔
2990
    terrno = TSDB_CODE_INVALID_PARA;
×
2991
    taosMemoryFree(pTSchema);
×
2992
    return NULL;
×
2993
  }
2994
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
1,116,669,694✔
2995
    terrno = TSDB_CODE_INVALID_PARA;
×
2996
    taosMemoryFree(pTSchema);
×
2997
    return NULL;
×
2998
  }
2999
  pTSchema->columns[0].colId = aSchema[0].colId;
1,116,649,081✔
3000
  pTSchema->columns[0].type = aSchema[0].type;
1,116,949,638✔
3001
  pTSchema->columns[0].flags = aSchema[0].flags;
1,116,945,510✔
3002
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
1,116,901,340✔
3003
  pTSchema->columns[0].offset = -1;
1,116,936,480✔
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,117,257,727✔
3028
#endif
3029

3030
  return pTSchema;
1,116,974,941✔
3031
}
3032

3033
static int32_t tTColumnCompare(const void *p1, const void *p2) {
247,978,449✔
3034
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
247,978,449✔
3035
    return -1;
41,273,039✔
3036
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
206,712,972✔
3037
    return 1;
147,158,938✔
3038
  }
3039

3040
  return 0;
59,567,046✔
3041
}
3042

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

3048
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
59,568,389✔
3049
}
3050

3051
// SColData ========================================
3052
void tColDataDestroy(void *ph) {
1,669,796,387✔
3053
  if (ph) {
1,669,796,387✔
3054
    SColData *pColData = (SColData *)ph;
1,669,849,110✔
3055

3056
    tFree(pColData->pBitMap);
1,669,849,110✔
3057
    tFree(pColData->aOffset);
1,669,911,392✔
3058
    tFree(pColData->pData);
1,669,851,875✔
3059
  }
3060
}
1,669,847,091✔
3061

3062
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
1,752,297,914✔
3063
  pColData->cid = cid;
1,752,297,914✔
3064
  pColData->type = type;
1,752,373,400✔
3065
  pColData->cflag = cflag;
1,752,418,035✔
3066
  tColDataClear(pColData);
1,752,543,154✔
3067
}
1,752,456,911✔
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) {
46,609,541✔
3079
  pColData->pBitMap = NULL;
46,609,541✔
3080
  pColData->aOffset = NULL;
46,618,462✔
3081
  pColData->pData = NULL;
46,615,436✔
3082

3083
  tColDataClear(pColData);
46,617,678✔
3084
}
46,630,042✔
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);
48,266,012✔
3093
      if (code) goto _exit;
33,097,182✔
3094
      pColData->aOffset[pColData->nVal] = pColData->nData;
33,097,182✔
3095

3096
      if (nData) {
33,097,182✔
3097
        code = tRealloc(&pColData->pData, pColData->nData + BSE_SEQUECE_SIZE);
29,824,220✔
3098
        if (code) goto _exit;
29,824,220✔
3099
        (void)memcpy(pColData->pData + pColData->nData, pData, BSE_SEQUECE_SIZE);
29,824,220✔
3100
        pColData->nData += BSE_SEQUECE_SIZE;
29,824,220✔
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]);
72,040,287✔
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) {
706,905,098✔
3138
  pColData->flag = HAS_VALUE;
706,994,518✔
3139
  pColData->numOfValue++;
707,027,009✔
3140
  return tColDataPutValue(pColData, pData, nData);
707,041,059✔
3141
}
3142
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
17,396,422✔
3143
  pColData->flag = HAS_NONE;
17,396,422✔
3144
  pColData->numOfNone++;
17,397,804✔
3145
  pColData->nVal++;
17,397,804✔
3146
  return 0;
17,397,804✔
3147
}
3148
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
34,307,680✔
3149
  pColData->flag = HAS_NULL;
34,332,543✔
3150
  pColData->numOfNull++;
34,336,558✔
3151
  pColData->nVal++;
34,336,253✔
3152
  return 0;
34,335,715✔
3153
}
3154
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,017,505✔
3155
  int32_t code = 0;
1,017,505✔
3156

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

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

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

3167
  if (pColData->nVal) {
1,017,505✔
3168
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,017,505✔
3169
      if (IS_STR_DATA_BLOB(pColData->type)) {
38,434✔
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;
38,434✔
3177
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
38,434✔
3178
        if (code) return code;
38,434✔
3179
        memset(pColData->aOffset, 0, nOffset);
38,434✔
3180
      }
3181
    } else {
3182
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
979,071✔
3183
      code = tRealloc(&pColData->pData, pColData->nData);
979,071✔
3184
      if (code) return code;
979,071✔
3185
      memset(pColData->pData, 0, pColData->nData);
979,071✔
3186
    }
3187
  }
3188

3189
  return tColDataPutValue(pColData, pData, nData);
1,017,505✔
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) {
70,915✔
3197
  int32_t code = 0;
94,387✔
3198

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

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

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

3210
  return code;
94,387✔
3211
}
3212
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,898,994✔
3213
  int32_t code = 0;
4,899,444✔
3214

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

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

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

3225
  if (pColData->nVal) {
4,899,444✔
3226
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,373,862✔
3227
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,474,418✔
3228
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,474,418✔
3229
      if (code) return code;
1,474,418✔
3230
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,474,418✔
3231
        memset(pColData->aOffset, 0, nOffset);
1,468,122✔
3232
      }
3233
    } else {
3234
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
3,425,026✔
3235
      code = tRealloc(&pColData->pData, pColData->nData);
3,425,026✔
3236
      if (code) return code;
3,425,026✔
3237
      memset(pColData->pData, 0, pColData->nData);
3,425,026✔
3238
    }
3239
  }
3240

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

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

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

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

3257
  return code;
19,724✔
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) {
818,912✔
3299
  int32_t code = 0;
818,912✔
3300

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

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

3308
  return code;
818,912✔
3309
}
3310
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
151,264✔
3311
  int32_t code = 0;
151,264✔
3312

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

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

3320
  return code;
151,264✔
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) {
7,486,964✔
3327
  int32_t code = 0;
7,486,964✔
3328

3329
  pColData->flag |= HAS_NONE;
7,486,964✔
3330
  pColData->numOfNone++;
7,486,964✔
3331

3332
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
7,487,655✔
3333
  code = tRealloc(&pColData->pBitMap, nBit);
7,486,964✔
3334
  if (code) return code;
7,486,964✔
3335

3336
  memset(pColData->pBitMap, 255, nBit);
7,486,964✔
3337
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
7,486,964✔
3338

3339
  return tColDataPutValue(pColData, NULL, 0);
7,486,964✔
3340
}
3341
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
23,406,587✔
3342
  int32_t code = 0;
24,058,529✔
3343

3344
  pColData->flag |= HAS_NULL;
24,058,529✔
3345
  pColData->numOfNull++;
24,058,529✔
3346

3347
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
24,058,529✔
3348
  code = tRealloc(&pColData->pBitMap, nBit);
24,058,529✔
3349
  if (code) return code;
24,058,529✔
3350

3351
  memset(pColData->pBitMap, 255, nBit);
24,058,529✔
3352
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
24,058,075✔
3353

3354
  return tColDataPutValue(pColData, NULL, 0);
24,058,525✔
3355
}
3356
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
360,942,298✔
3357
  int32_t code = 0;
360,942,966✔
3358

3359
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
360,942,966✔
3360
  if (code) return code;
360,941,450✔
3361

3362
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
360,941,450✔
3363
  pColData->numOfValue++;
360,941,316✔
3364

3365
  return tColDataPutValue(pColData, pData, nData);
360,940,177✔
3366
}
3367
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
899,403,202✔
3368
  int32_t code = 0;
899,403,202✔
3369

3370
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
899,403,202✔
3371
  if (code) return code;
899,403,202✔
3372

3373
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
899,403,202✔
3374
  pColData->numOfNone++;
899,405,275✔
3375

3376
  return tColDataPutValue(pColData, NULL, 0);
899,403,202✔
3377
}
3378
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
104,855✔
3379
  int32_t code = 0;
104,855✔
3380

3381
  pColData->flag |= HAS_NULL;
104,855✔
3382
  pColData->numOfNull++;
104,855✔
3383

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

3388
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,544,903✔
3389
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
2,440,048✔
3390
  }
3391
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
104,855✔
3392

3393
  tFree(pColData->pBitMap);
104,855✔
3394
  pColData->pBitMap = pBitMap;
104,855✔
3395

3396
  return tColDataPutValue(pColData, NULL, 0);
104,855✔
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,188,168✔
3409
  int32_t code = 0;
2,188,168✔
3410

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

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

3418
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
188,671,948✔
3419
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
186,483,780✔
3420
  }
3421
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
2,188,168✔
3422

3423
  tFree(pColData->pBitMap);
2,188,168✔
3424
  pColData->pBitMap = pBitMap;
2,188,168✔
3425

3426
  return tColDataPutValue(pColData, NULL, 0);
2,188,168✔
3427
}
3428
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,430,667,992✔
3429
  int32_t code = 0;
1,432,042,587✔
3430

3431
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,432,042,587✔
3432
  if (code) return code;
1,432,545,461✔
3433
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,432,545,461✔
3434
  pColData->numOfNull++;
1,432,828,574✔
3435

3436
  return tColDataPutValue(pColData, NULL, 0);
1,432,811,170✔
3437
}
3438
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,330,676✔
3439
  int32_t code = 0;
3,450,644✔
3440

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

3446
  return tColDataPutValue(pColData, pData, nData);
3,450,644✔
3447
}
3448
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
343,095,492✔
3449
  int32_t code = 0;
343,095,492✔
3450

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

3456
  return tColDataPutValue(pColData, NULL, 0);
343,095,492✔
3457
}
3458
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
394,101✔
3459
  int32_t code = 0;
394,101✔
3460

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

3466
  return tColDataPutValue(pColData, NULL, 0);
394,101✔
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;
280✔
3483
  uint8_t  buf[sizeof(uint64_t) + 1] = {0};
×
3484
  uint64_t seq = 0;
280✔
3485

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

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

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

3512
static FORCE_INLINE int32_t tColDataAppendValueBlob00(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
168✔
3513
  pColData->flag = HAS_VALUE;
168✔
3514
  pColData->numOfValue++;
168✔
3515
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
168✔
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) {
160✔
3524
  pColData->flag = HAS_NULL;
160✔
3525
  pColData->numOfNull++;
160✔
3526
  pColData->nVal++;
160✔
3527
  return addEmptyItemToBlobSet(pArg, TSDB_DATA_BLOB_NULL_VALUE, NULL);
160✔
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) {
48✔
3679
  pColData->numOfValue++;
48✔
3680
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
48✔
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) {
64✔
3698
  int32_t code = 0;
64✔
3699

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

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

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

3710
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
64✔
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,366✔
3845
  pColData->numOfNone--;
3,366✔
3846
  pColData->nVal--;
3,366✔
3847
  if (pColData->numOfNone) {
3,366✔
3848
    return tColDataAppendValue10(pColData, pData, nData);
×
3849
  } else {
3850
    pColData->flag = 0;
3,366✔
3851
    return tColDataAppendValue00(pColData, pData, nData);
3,366✔
3852
  }
3853
}
3854
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
27,042✔
3855
  pColData->numOfNone--;
27,042✔
3856
  pColData->nVal--;
27,042✔
3857
  if (pColData->numOfNone) {
27,042✔
3858
    return tColDataAppendValue12(pColData, pData, nData);
23,472✔
3859
  } else {
3860
    pColData->flag = 0;
3,570✔
3861
    return tColDataAppendValue02(pColData, pData, nData);
3,570✔
3862
  }
3863
  return 0;
3864
}
3865
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
86,504✔
3866
  if (forward) {
86,504✔
3867
    pColData->numOfNull--;
86,504✔
3868
    pColData->nVal--;
86,504✔
3869
    if (pColData->numOfNull) {
86,504✔
3870
      return tColDataAppendValue20(pColData, pData, nData);
450✔
3871
    } else {
3872
      pColData->flag = 0;
86,054✔
3873
      return tColDataAppendValue00(pColData, pData, nData);
86,054✔
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) {
340,508✔
3901
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
340,508✔
3902
    pColData->numOfNone--;
340,508✔
3903
    pColData->numOfNull++;
340,508✔
3904
    if (pColData->numOfNone) {
340,508✔
3905
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
328,608✔
3906
    } else {
3907
      pColData->flag = HAS_NULL;
11,900✔
3908
    }
3909
  }
3910
  return 0;
340,508✔
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,809,045,791✔
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;
126✔
3923
}
3924
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
649,168✔
3925
  if (forward) {  // VALUE ==> NULL
649,168✔
3926
    pColData->numOfValue--;
649,168✔
3927
    pColData->nVal--;
649,168✔
3928
    if (pColData->numOfValue) {
649,168✔
3929
      if (IS_VAR_DATA_TYPE(pColData->type)) {
627,875✔
3930
        pColData->nData = pColData->aOffset[pColData->nVal];
182,560✔
3931
      } else {
3932
        pColData->nData -= TYPE_BYTES[pColData->type];
445,315✔
3933
      }
3934
      return tColDataAppendValue42(pColData, pData, nData);
627,875✔
3935
    } else {
3936
      pColData->flag = 0;
21,293✔
3937
      pColData->nData = 0;
21,293✔
3938
      return tColDataAppendValue02(pColData, pData, nData);
21,293✔
3939
    }
3940
  }
3941
  return 0;
×
3942
}
3943
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,799,996✔
3944
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
2,799,996✔
3945
    pColData->numOfNone--;
130,400✔
3946
    pColData->nVal--;
130,400✔
3947
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
130,400✔
3948
      pColData->nData -= TYPE_BYTES[pColData->type];
130,400✔
3949
    }
3950
    if (pColData->numOfNone) {
130,400✔
3951
      return tColDataAppendValue50(pColData, pData, nData);
×
3952
    } else {
3953
      pColData->flag = HAS_VALUE;
130,400✔
3954
      return tColDataAppendValue40(pColData, pData, nData);
130,400✔
3955
    }
3956
  } else if (forward) {  // VALUE ==> VALUE
2,669,596✔
3957
    pColData->nVal--;
2,669,596✔
3958
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,669,596✔
3959
      pColData->nData = pColData->aOffset[pColData->nVal];
381,240✔
3960
    } else {
3961
      pColData->nData -= TYPE_BYTES[pColData->type];
2,288,356✔
3962
    }
3963
    return tColDataPutValue(pColData, pData, nData);
2,669,596✔
3964
  }
3965
  return 0;
×
3966
}
3967
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
24,067✔
3968
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
24,067✔
3969
    pColData->numOfNone--;
24,067✔
3970
    pColData->nVal--;
24,067✔
3971
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
24,067✔
3972
      pColData->nData -= TYPE_BYTES[pColData->type];
16,243✔
3973
    }
3974
    if (pColData->numOfNone) {
24,067✔
3975
      return tColDataAppendValue52(pColData, pData, nData);
×
3976
    } else {
3977
      pColData->flag = HAS_VALUE;
24,067✔
3978
      return tColDataAppendValue42(pColData, pData, nData);
24,067✔
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,193,246✔
3999
  if (forward) {
3,193,246✔
4000
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,193,246✔
4001
      pColData->numOfNull--;
2,204,997✔
4002
      pColData->nVal--;
2,204,997✔
4003
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
2,204,997✔
4004
        pColData->nData -= TYPE_BYTES[pColData->type];
1,858,702✔
4005
      }
4006
      if (pColData->numOfNull) {
2,204,997✔
4007
        return tColDataAppendValue60(pColData, pData, nData);
2,434✔
4008
      } else {
4009
        pColData->flag = HAS_VALUE;
2,202,563✔
4010
        return tColDataAppendValue40(pColData, pData, nData);
2,202,563✔
4011
      }
4012
    } else {  // VALUE ==> VALUE
4013
      pColData->nVal--;
988,249✔
4014
      if (IS_VAR_DATA_TYPE(pColData->type)) {
988,249✔
4015
        pColData->nData = pColData->aOffset[pColData->nVal];
124,007✔
4016
      } else {
4017
        pColData->nData -= TYPE_BYTES[pColData->type];
864,242✔
4018
      }
4019
      return tColDataPutValue(pColData, pData, nData);
988,249✔
4020
    }
4021
  }
4022
  return 0;
×
4023
}
4024
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,571,633✔
4025
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
1,571,633✔
4026
    pColData->numOfValue--;
983,607✔
4027
    pColData->nVal--;
983,607✔
4028
    if (pColData->numOfValue) {
983,607✔
4029
      if (IS_VAR_DATA_TYPE(pColData->type)) {
983,607✔
4030
        pColData->nData = pColData->aOffset[pColData->nVal];
105,145✔
4031
      } else {
4032
        pColData->nData -= TYPE_BYTES[pColData->type];
878,462✔
4033
      }
4034
      return tColDataAppendValue62(pColData, pData, nData);
983,607✔
4035
    } else {
4036
      pColData->flag = HAS_NULL;
×
4037
      pColData->nData = 0;
×
4038
      return tColDataAppendValue20(pColData, pData, nData);
×
4039
    }
4040
  }
4041
  return 0;
588,026✔
4042
}
4043
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
330,660✔
4044
  int32_t code = 0;
330,660✔
4045

4046
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
330,660✔
4047
  if (bv == 0) {  // NONE ==> VALUE
330,660✔
4048
    pColData->numOfNone--;
78,240✔
4049
    pColData->nVal--;
78,240✔
4050
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
78,240✔
4051
      pColData->nData -= TYPE_BYTES[pColData->type];
78,240✔
4052
    }
4053
    if (pColData->numOfNone) {
78,240✔
4054
      return tColDataAppendValue70(pColData, pData, nData);
78,240✔
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
252,420✔
4063
    if (forward) {
42,396✔
4064
      pColData->numOfNull--;
42,396✔
4065
      pColData->nVal--;
42,396✔
4066
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
42,396✔
4067
        pColData->nData -= TYPE_BYTES[pColData->type];
42,396✔
4068
      }
4069
      if (pColData->numOfNull) {
42,396✔
4070
        return tColDataAppendValue70(pColData, pData, nData);
41,728✔
4071
      } else {
4072
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
2,004✔
4073
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
1,336✔
4074
        }
4075
        pColData->flag = (HAS_VALUE | HAS_NONE);
668✔
4076
        return tColDataAppendValue50(pColData, pData, nData);
668✔
4077
      }
4078
    }
4079
  } else if (bv == 2) {  // VALUE ==> VALUE
210,024✔
4080
    if (forward) {
210,024✔
4081
      pColData->nVal--;
210,024✔
4082
      if (IS_VAR_DATA_TYPE(pColData->type)) {
210,024✔
4083
        pColData->nData = pColData->aOffset[pColData->nVal];
52,160✔
4084
      } else {
4085
        pColData->nData -= TYPE_BYTES[pColData->type];
157,864✔
4086
      }
4087
      return tColDataPutValue(pColData, pData, nData);
210,024✔
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) {
390,988✔
4095
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
390,988✔
4096
  if (bv == 0) {  // NONE ==> NULL
390,988✔
4097
    pColData->numOfNone--;
390,988✔
4098
    pColData->nVal--;
390,988✔
4099
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
390,988✔
4100
      pColData->nData -= TYPE_BYTES[pColData->type];
242,577✔
4101
    }
4102
    if (pColData->numOfNone) {
390,988✔
4103
      return tColDataAppendValue72(pColData, pData, nData);
×
4104
    } else {
4105
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
12,300,728✔
4106
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
11,909,740✔
4107
      }
4108
      pColData->flag = (HAS_VALUE | HAS_NULL);
390,988✔
4109
      return tColDataAppendValue62(pColData, pData, nData);
390,988✔
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) {
3,009,480✔
4133
  return 0;
3,009,480✔
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
2,147,483,647✔
4158
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
2,147,483,647✔
4159
}
2,147,483,647✔
4160
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,730,797,867✔
4161
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,730,797,867✔
4162
}
1,730,882,774✔
4163
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
6,500,602✔
4164
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4165
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
6,500,602✔
4166
    case 0:
3,313,545✔
4167
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,313,545✔
4168
      break;
3,312,893✔
4169
    case 1:
3,186,405✔
4170
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
3,186,405✔
4171
      break;
3,187,057✔
4172
    default:
×
4173
      break;
×
4174
  }
4175
}
6,499,950✔
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];
229,551,844✔
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,
2,147,483,647✔
4192
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4193
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4194
    case 0:
1,157,975,751✔
4195
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,157,975,751✔
4196
      break;
1,157,966,975✔
4197
    case 1:
1,207,600,270✔
4198
      tColDataGetValue4(pColData, iVal, pColVal);
4199
      break;
1,207,360,311✔
4200
    default:
×
4201
      break;
×
4202
  }
4203
}
2,147,483,647✔
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,010,679✔
4218
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4219
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
22,010,679✔
4220
    case 0:
3,864,801✔
4221
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,864,801✔
4222
      break;
3,864,801✔
4223
    case 1:
4,688,915✔
4224
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
4,688,915✔
4225
      break;
4,687,611✔
4226
    case 2:
13,460,875✔
4227
      tColDataGetValue4(pColData, iVal, pColVal);
4228
      break;
13,466,743✔
4229
    default:
×
4230
      break;
×
4231
  }
4232
}
22,019,155✔
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;
612✔
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,467,840✔
4259
      return GET_BIT1(pColData->pBitMap, iVal);
6,467,840✔
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):
105,712,904✔
4267
      return GET_BIT2(pColData->pBitMap, iVal);
105,712,904✔
4268
    default:
×
4269
      return 0;
×
4270
  }
4271
}
4272

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

4276
  *pColData = *pColDataFrom;
15,707,658✔
4277

4278
  // bitmap
4279
  switch (pColData->flag) {
15,712,389✔
4280
    case (HAS_NULL | HAS_NONE):
51,221✔
4281
    case (HAS_VALUE | HAS_NONE):
4282
    case (HAS_VALUE | HAS_NULL):
4283
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
51,221✔
4284
      if (pColData->pBitMap == NULL) {
51,221✔
4285
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4286
        goto _exit;
×
4287
      }
4288
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
51,221✔
4289
      break;
51,221✔
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,661,162✔
4299
      pColData->pBitMap = NULL;
15,661,162✔
4300
      break;
15,661,168✔
4301
  }
4302

4303
  // offset
4304
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
15,712,389✔
4305
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
91,057✔
4306
    if (pColData->aOffset == NULL) {
91,057✔
4307
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4308
      goto _exit;
×
4309
    }
4310
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
91,057✔
4311
  } else {
4312
    pColData->aOffset = NULL;
15,621,370✔
4313
  }
4314

4315
  // value
4316
  if (pColData->nData) {
15,712,754✔
4317
    pColData->pData = xMalloc(arg, pColData->nData);
15,669,004✔
4318
    if (pColData->pData == NULL) {
15,669,325✔
4319
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4320
      goto _exit;
×
4321
    }
4322

4323
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
15,668,962✔
4324
  } else {
4325
    pColData->pData = NULL;
43,744✔
4326
  }
4327

4328
_exit:
15,712,387✔
4329
  return code;
15,712,387✔
4330
}
4331

4332
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
586,381,772✔
4333
  int32_t code;
4334
  SBuffer local;
586,377,113✔
4335

4336
  if (!(colData->nVal > 0)) {
586,402,338✔
4337
    return TSDB_CODE_INVALID_PARA;
×
4338
  }
4339

4340
  (*info) = (SColDataCompressInfo){
586,414,907✔
4341
      .cmprAlg = info->cmprAlg,
586,408,192✔
4342
      .columnFlag = colData->cflag,
586,420,112✔
4343
      .flag = colData->flag,
586,421,085✔
4344
      .dataType = colData->type,
586,416,188✔
4345
      .columnId = colData->cid,
586,423,715✔
4346
      .numOfData = colData->nVal,
586,408,013✔
4347
  };
4348

4349
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
586,411,106✔
4350
    return 0;
19,725,139✔
4351
  }
4352

4353
  tBufferInit(&local);
4354
  if (assist == NULL) {
566,689,601✔
4355
    assist = &local;
×
4356
  }
4357

4358
  // bitmap
4359
  if (colData->flag != HAS_VALUE) {
566,689,601✔
4360
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
30,531,660✔
4361
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,875,287✔
4362
    } else {
4363
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
28,655,345✔
4364
    }
4365

4366
    SCompressInfo cinfo = {
30,532,437✔
4367
        .dataType = TSDB_DATA_TYPE_TINYINT,
4368
        .cmprAlg = info->cmprAlg,
30,532,437✔
4369
        .originalSize = info->bitmapOriginalSize,
30,532,803✔
4370
    };
4371

4372
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
30,530,885✔
4373
    if (code) {
30,532,301✔
4374
      tBufferDestroy(&local);
4375
      return code;
×
4376
    }
4377

4378
    info->bitmapCompressedSize = cinfo.compressedSize;
30,532,301✔
4379
  }
4380

4381
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
566,667,505✔
4382
    tBufferDestroy(&local);
4383
    return 0;
102,130✔
4384
  }
4385

4386
  // offset
4387
  if (IS_VAR_DATA_TYPE(colData->type)) {
566,525,427✔
4388
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
90,666,806✔
4389

4390
    SCompressInfo cinfo = {
90,704,935✔
4391
        .dataType = TSDB_DATA_TYPE_INT,
4392
        .cmprAlg = info->cmprAlg,
90,704,180✔
4393
        .originalSize = info->offsetOriginalSize,
90,701,145✔
4394
    };
4395

4396
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
90,699,354✔
4397
    if (code) {
90,705,809✔
4398
      tBufferDestroy(&local);
4399
      return code;
×
4400
    }
4401

4402
    info->offsetCompressedSize = cinfo.compressedSize;
90,705,809✔
4403
  }
4404

4405
  // data
4406
  if (colData->nData > 0) {
566,592,040✔
4407
    info->dataOriginalSize = colData->nData;
566,567,489✔
4408

4409
    SCompressInfo cinfo = {
566,561,230✔
4410
        .dataType = colData->type,
566,558,933✔
4411
        .cmprAlg = info->cmprAlg,
566,562,413✔
4412
        .originalSize = info->dataOriginalSize,
566,572,839✔
4413
    };
4414

4415
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
566,548,927✔
4416
    if (code) {
566,563,225✔
4417
      tBufferDestroy(&local);
4418
      return code;
×
4419
    }
4420

4421
    info->dataCompressedSize = cinfo.compressedSize;
566,563,225✔
4422
  }
4423

4424
  tBufferDestroy(&local);
4425
  return 0;
566,551,914✔
4426
}
4427

4428
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
1,001,674,962✔
4429
  int32_t  code;
4430
  SBuffer  local;
1,001,671,166✔
4431
  uint8_t *data = (uint8_t *)input;
1,001,781,367✔
4432

4433
  tBufferInit(&local);
4434
  if (assist == NULL) {
1,001,798,697✔
4435
    assist = &local;
×
4436
  }
4437

4438
  tColDataClear(colData);
1,001,798,697✔
4439
  colData->cid = info->columnId;
1,001,806,994✔
4440
  colData->type = info->dataType;
1,001,787,072✔
4441
  colData->cflag = info->columnFlag;
1,001,774,959✔
4442
  colData->nVal = info->numOfData;
1,001,773,049✔
4443
  colData->flag = info->flag;
1,001,731,390✔
4444

4445
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
1,001,738,802✔
4446
    goto _exit;
97,797,869✔
4447
  }
4448

4449
  // bitmap
4450
  if (info->bitmapOriginalSize > 0) {
903,872,408✔
4451
    SCompressInfo cinfo = {
156,403,966✔
4452
        .dataType = TSDB_DATA_TYPE_TINYINT,
4453
        .cmprAlg = info->cmprAlg,
156,406,007✔
4454
        .originalSize = info->bitmapOriginalSize,
156,405,674✔
4455
        .compressedSize = info->bitmapCompressedSize,
156,396,739✔
4456
    };
4457

4458
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
156,399,146✔
4459
    if (code) {
156,392,781✔
4460
      tBufferDestroy(&local);
4461
      return code;
×
4462
    }
4463

4464
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
156,392,781✔
4465
    if (code) {
156,395,205✔
4466
      tBufferDestroy(&local);
4467
      return code;
×
4468
    }
4469

4470
    data += cinfo.compressedSize;
156,395,205✔
4471
  }
4472

4473
  if (info->flag == (HAS_NONE | HAS_NULL)) {
903,958,637✔
4474
    goto _exit;
198,860✔
4475
  }
4476

4477
  // offset
4478
  if (info->offsetOriginalSize > 0) {
903,763,306✔
4479
    SCompressInfo cinfo = {
192,988,233✔
4480
        .cmprAlg = info->cmprAlg,
192,987,681✔
4481
        .dataType = TSDB_DATA_TYPE_INT,
4482
        .originalSize = info->offsetOriginalSize,
192,989,049✔
4483
        .compressedSize = info->offsetCompressedSize,
192,978,435✔
4484
    };
4485

4486
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
192,980,552✔
4487
    if (code) {
192,981,566✔
4488
      tBufferDestroy(&local);
4489
      return code;
×
4490
    }
4491

4492
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
192,981,566✔
4493
    if (code) {
192,970,035✔
4494
      tBufferDestroy(&local);
4495
      return code;
×
4496
    }
4497

4498
    data += cinfo.compressedSize;
192,970,035✔
4499
  }
4500

4501
  // data
4502
  if (info->dataOriginalSize > 0) {
903,787,453✔
4503
    colData->nData = info->dataOriginalSize;
903,721,198✔
4504

4505
    SCompressInfo cinfo = {
903,729,307✔
4506
        .cmprAlg = info->cmprAlg,
1,807,427,629✔
4507
        .dataType = colData->type,
903,736,968✔
4508
        .originalSize = info->dataOriginalSize,
903,696,691✔
4509
        .compressedSize = info->dataCompressedSize,
903,710,619✔
4510
    };
4511

4512
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
903,708,514✔
4513
    if (code) {
903,615,361✔
4514
      tBufferDestroy(&local);
4515
      return code;
×
4516
    }
4517

4518
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
903,615,361✔
4519
    if (code) {
903,685,365✔
4520
      tBufferDestroy(&local);
4521
      return code;
×
4522
    }
4523

4524
    data += cinfo.compressedSize;
903,685,365✔
4525
  }
4526

4527
_exit:
1,001,796,267✔
4528
  switch (colData->flag) {
1,001,797,509✔
4529
    case HAS_NONE:
59,571,114✔
4530
      colData->numOfNone = colData->nVal;
59,571,114✔
4531
      break;
59,569,080✔
4532
    case HAS_NULL:
38,222,731✔
4533
      colData->numOfNull = colData->nVal;
38,222,731✔
4534
      break;
38,227,554✔
4535
    case HAS_VALUE:
747,558,020✔
4536
      colData->numOfValue = colData->nVal;
747,558,020✔
4537
      break;
747,555,848✔
4538
    default:
156,404,437✔
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++;
1,207,830,347✔
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;
1,001,696,699✔
4552
}
4553

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

4568
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
241,059✔
4569
    if (!IS_STR_DATA_BLOB(type)) {
46,351✔
4570
      for (int32_t i = 0; i < nRows; ++i) {
123,028✔
4571
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
76,677✔
4572
        if (offset == -1) {
76,677✔
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)) {
76,677✔
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),
153,354✔
4588
                                                                        varDataLen(data + offset));
76,677✔
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;
194,708✔
4616
    bool allNull = true;
194,708✔
4617
    for (int32_t i = 0; i < nRows; ++i) {
528,815✔
4618
      if (!BMIsNull(lengthOrbitmap, i)) {
334,107✔
4619
        allNull = false;
254,263✔
4620
      } else {
4621
        allValue = false;
79,844✔
4622
      }
4623
    }
4624
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
194,708✔
4625
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4626
      goto _exit;
×
4627
    }
4628

4629
    if (allValue) {
194,708✔
4630
      // optimize (todo)
4631
      for (int32_t i = 0; i < nRows; ++i) {
408,558✔
4632
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
249,503✔
4633
      }
4634
    } else if (allNull) {
35,653✔
4635
      // optimize (todo)
4636
      for (int32_t i = 0; i < nRows; ++i) {
95,727✔
4637
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
63,479✔
4638
        if (code) goto _exit;
63,479✔
4639
      }
4640
    } else {
4641
      for (int32_t i = 0; i < nRows; ++i) {
24,530✔
4642
        if (BMIsNull(lengthOrbitmap, i)) {
21,125✔
4643
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
16,365✔
4644
          if (code) goto _exit;
16,365✔
4645
        } else {
4646
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
4,760✔
4647
        }
4648
      }
4649
    }
4650
  }
4651

4652
_exit:
3,405✔
4653
  return code;
248,261✔
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,690,727✔
4758
                               checkWKBGeometryFn cgeos) {
4759
  int32_t code = 0;
22,690,727✔
4760

4761
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
22,690,727✔
4762
    if (!(pColData->type == pBind->buffer_type)) {
22,694,469✔
4763
      return TSDB_CODE_INVALID_PARA;
×
4764
    }
4765
  }
4766

4767
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
22,697,117✔
4768
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
28,771✔
4769
      code = igeos();
968✔
4770
      if (code) {
968✔
4771
        return code;
×
4772
      }
4773
    }
4774
    for (int32_t i = 0; i < pBind->num; ++i) {
1,467,849✔
4775
      if (pBind->is_null && pBind->is_null[i]) {
1,449,950✔
4776
        if (pColData->cflag & COL_IS_KEY) {
681,958✔
4777
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,422✔
4778
          goto _exit;
1,422✔
4779
        }
4780
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
680,536✔
4781
        if (code) goto _exit;
680,536✔
4782
      } else if (pBind->length[i] > buffMaxLen) {
767,992✔
4783
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4784
      } else {
4785
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
767,992✔
4786
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
13,190✔
4787
          if (code) {
13,190✔
4788
            uError("stmt col[%d] bind geometry wrong format", i);
198✔
4789
            goto _exit;
198✔
4790
          }
4791
        }
4792
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
1,522,907✔
4793
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
767,794✔
4794
      }
4795
    }
4796
  } else {  // fixed-length data type
4797
    bool allValue;
4798
    bool allNull;
4799
    if (pBind->is_null) {
22,659,321✔
4800
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
15,121,197✔
4801
      allNull = (same && pBind->is_null[0] != 0);
15,117,798✔
4802
      allValue = (same && pBind->is_null[0] == 0);
15,115,952✔
4803
    } else {
4804
      allNull = false;
7,540,300✔
4805
      allValue = true;
7,540,300✔
4806
    }
4807

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

4813
    if (allValue) {
22,656,452✔
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,547✔
4820
      // optimize (todo)
4821
      for (int32_t i = 0; i < pBind->num; ++i) {
1,322✔
4822
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
661✔
4823
        if (code) goto _exit;
661✔
4824
      }
4825
    } else {
4826
      for (int32_t i = 0; i < pBind->num; ++i) {
8,863,722✔
4827
        if (pBind->is_null[i]) {
8,837,836✔
4828
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,907,192✔
4829
          if (code) goto _exit;
3,907,192✔
4830
        } else {
4831
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
9,861,288✔
4832
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,930,644✔
4833
        }
4834
      }
4835
    }
4836
  }
4837

4838
_exit:
10,640✔
4839
  return code;
22,694,786✔
4840
}
4841

4842
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen) {
1,229,120✔
4843
  int32_t code = 0;
1,229,120✔
4844

4845
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
1,229,120✔
4846
    if (!(pColData->type == pBind->buffer_type)) {
1,223,328✔
4847
      return TSDB_CODE_INVALID_PARA;
×
4848
    }
4849
  }
4850

4851
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
1,306,224✔
4852
    uint8_t *buf = pBind->buffer;
76,756✔
4853
    for (int32_t i = 0; i < pBind->num; ++i) {
112,818,045✔
4854
      if (pBind->is_null && pBind->is_null[i]) {
112,678,430✔
4855
        if (pColData->cflag & COL_IS_KEY) {
7,232,841✔
4856
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4857
          goto _exit;
×
4858
        }
4859
        if (pBind->is_null[i] == 1) {
7,233,226✔
4860
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
7,233,446✔
4861
          if (code) goto _exit;
7,232,560✔
4862
        } else {
4863
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4864
          if (code) goto _exit;
505✔
4865
        }
4866
      } else if (pBind->length[i] > buffMaxLen) {
105,529,934✔
4867
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4868
      } else {
4869
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
105,920,841✔
4870
        buf += pBind->length[i];
105,598,916✔
4871
      }
4872
    }
4873
  } else {  // fixed-length data type
4874
    bool allValue;
4875
    bool allNull;
4876
    bool allNone;
4877
    if (pBind->is_null) {
1,152,572✔
4878
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
1,105,484✔
4879
      allNull = (same && pBind->is_null[0] == 1);
1,105,213✔
4880
      allNone = (same && pBind->is_null[0] > 1);
1,105,484✔
4881
      allValue = (same && pBind->is_null[0] == 0);
1,105,430✔
4882
    } else {
4883
      allNull = false;
47,088✔
4884
      allNone = false;
47,088✔
4885
      allValue = true;
47,088✔
4886
    }
4887

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

4893
    uint8_t *buf = pBind->buffer;
1,152,197✔
4894

4895
    if (allValue) {
1,152,444✔
4896
      // optimize (todo)
4897
      for (int32_t i = 0; i < pBind->num; ++i) {
688,776,898✔
4898
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
688,553,067✔
4899
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
687,772,692✔
4900
          *val = 1;
×
4901
        }
4902
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
688,719,937✔
4903
      }
4904
    } else if (allNull) {
45,320✔
4905
      // optimize (todo)
4906
      for (int32_t i = 0; i < pBind->num; ++i) {
8,887,443✔
4907
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
8,881,677✔
4908
        if (code) goto _exit;
8,881,677✔
4909
      }
4910
    } else if (allNone) {
39,554✔
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) {
368,636,540✔
4918
        if (pBind->is_null[i]) {
378,354,660✔
4919
          if (pBind->is_null[i] == 1) {
22,652,944✔
4920
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
22,631,431✔
4921
            if (code) goto _exit;
22,592,010✔
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;
359,023,757✔
4928
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
359,080,990✔
4929
            *val = 1;
×
4930
          }
4931

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

4938
_exit:
123,263✔
4939
  return code;
1,172,759✔
4940
}
4941

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

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

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

4956
  code = igeos();
5,221✔
4957
  if (code) {
5,221✔
4958
    return code;
×
4959
  }
4960

4961
  uint8_t *buf = pBind->buffer;
5,221✔
4962
  for (int32_t i = 0; i < pBind->num; ++i) {
1,856,558✔
4963
    if (pBind->is_null && pBind->is_null[i]) {
1,856,381✔
4964
      if (pColData->cflag & COL_IS_KEY) {
521✔
4965
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4966
        goto _exit;
×
4967
      }
4968
      if (pBind->is_null[i] == 1) {
521✔
4969
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
521✔
4970
        if (code) goto _exit;
521✔
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,856,314✔
4976
      return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4977
    } else {
4978
      code = cgeos(buf, pBind->length[i]);
1,858,616✔
4979
      if (code) {
1,833,357✔
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,833,357✔
4984
      buf += pBind->length[i];
1,852,263✔
4985
    }
4986
  }
4987

4988
_exit:
5,221✔
4989
  return code;
5,221✔
4990
}
4991

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

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

5002
  if (IS_STR_DATA_BLOB(pColData->type)) {  // var-length data type
328✔
5003
    uint8_t *buf = pBind->buffer;
328✔
5004
    for (int32_t i = 0; i < pBind->num; ++i) {
768✔
5005
      if (pBind->is_null && pBind->is_null[i]) {
440✔
5006
        if (pColData->cflag & COL_IS_KEY) {
224✔
5007
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5008
          goto _exit;
×
5009
        }
5010
        if (pBind->is_null[i] == 1) {
224✔
5011
          code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_NULL](pBlobSet, pColData, NULL, 0);
224✔
5012
          if (code) goto _exit;
224✔
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) {
216✔
5018
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5019
      } else {
5020
        code = tColDataAppendValueBlobImpl[pColData->flag][CV_FLAG_VALUE](pBlobSet, pColData, buf, pBind->length[i]);
216✔
5021
        buf += pBind->length[i];
216✔
5022
      }
5023
    }
5024
  }
5025
_exit:
328✔
5026
  return code;
328✔
5027
}
5028

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

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

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

5043
  uint8_t *buf = pBind->buffer;
4,795✔
5044
  for (int32_t i = 0; i < pBind->num; ++i) {
27,305,651✔
5045
    if (pBind->is_null && pBind->is_null[i]) {
27,293,582✔
5046
      if (pColData->cflag & COL_IS_KEY) {
1,379,806✔
5047
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5048
        goto _exit;
×
5049
      }
5050
      if (pBind->is_null[i] == 1) {
1,379,781✔
5051
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1,379,376✔
5052
        if (code) goto _exit;
1,379,349✔
5053
      } else {
5054
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
396✔
5055
        if (code) goto _exit;
396✔
5056
      }
5057
    } else {
5058
      if (pColData->type == TSDB_DATA_TYPE_DECIMAL64) {
25,939,524✔
5059
        Decimal64 dec = {0};
13,012,041✔
5060
        int32_t   code = decimal64FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
13,009,917✔
5061
        buf += pBind->length[i];
12,978,735✔
5062
        if (TSDB_CODE_SUCCESS != code) {
12,981,832✔
5063
          return code;
×
5064
        }
5065
        int64_t tmp = DECIMAL64_GET_VALUE(&dec);
12,981,832✔
5066
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)&tmp,
12,982,661✔
5067
                                                                      TYPE_BYTES[pColData->type]);
12,983,580✔
5068
      } else if (pColData->type == TSDB_DATA_TYPE_DECIMAL) {
13,020,525✔
5069
        Decimal128 dec = {0};
13,016,462✔
5070
        int32_t    code = decimal128FromStr((char *)buf, pBind->length[i], precision, scale, &dec);
13,017,980✔
5071
        buf += pBind->length[i];
12,980,533✔
5072
        if (TSDB_CODE_SUCCESS != code) {
12,982,585✔
5073
          return code;
×
5074
        }
5075
        uint8_t *pV = taosMemCalloc(1, sizeof(Decimal128));
12,982,585✔
5076
        if (!pV) return terrno;
13,020,225✔
5077
        memcpy(pV, &dec, DECIMAL_WORD_NUM(Decimal128) * sizeof(DecimalWord));
13,020,225✔
5078
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pV, TYPE_BYTES[pColData->type]);
13,022,783✔
5079
        taosMemoryFree(pV);
13,017,986✔
5080
      }
5081
    }
5082
  }
5083

5084
_exit:
4,844✔
5085
  return code;
4,844✔
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,039,767✔
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,039,767✔
5100
    return TSDB_CODE_INVALID_PARA;
×
5101
  }
5102
  int8_t hasBlob = schemaHasBlob(pTSchema);
1,041,105✔
5103
  if (!infoSorted) {
1,041,200✔
5104
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
1,386✔
5105
  }
5106

5107
  int32_t code = 0;
1,041,200✔
5108
  int32_t numOfRows = -1;
1,041,200✔
5109
  SArray *colValArray, *bufArray;
5110
  SColVal colVal;
320,765✔
5111
  int32_t numOfFixedValue = 0;
1,041,200✔
5112
  int32_t lino = 0;
1,041,200✔
5113
  bool    hasDecimal128 = false;
1,041,200✔
5114

5115
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
1,041,200✔
5116
    return terrno;
×
5117
  }
5118
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
1,041,021✔
5119
    taosArrayDestroy(colValArray);
58✔
5120
    return terrno;
×
5121
  }
5122
  for (int i = 0; i < numOfInfos; ++i) {
10,313,832✔
5123
    if (parsedCols) {
9,272,538✔
5124
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
28,512✔
5125
      if (pParsedVal) {
28,512✔
5126
        continue;
5,940✔
5127
      }
5128
    }
5129
    if (numOfRows == -1) {
9,266,598✔
5130
      numOfRows = infos[i].bind->num;
1,041,066✔
5131
    }
5132

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

5140
  SRowKey rowKey, lastRowKey;
320,751✔
5141
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
12,663,328✔
5142
    taosArrayClear(colValArray);
11,634,844✔
5143
    numOfFixedValue = 0;
11,633,791✔
5144

5145
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
67,103,953✔
5146
      if (parsedCols) {
55,276,572✔
5147
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
30,888✔
5148
        if (pParsedVal) {
30,888✔
5149
          numOfFixedValue++;
6,264✔
5150
          colVal = *pParsedVal;
6,264✔
5151

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

5160
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
55,300,848✔
5161
        if (infos[iInfo].bind->is_null[iRow] == 1) {
990✔
5162
          if (iInfo == 0) {
594✔
5163
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
198✔
5164
            TAOS_CHECK_GOTO(code, &lino, _exit);
198✔
5165
          }
5166
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
396✔
5167
        } else {
5168
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
396✔
5169
        }
5170
      } else {
5171
        SValue value = {
55,326,858✔
5172
            .type = infos[iInfo].type,
55,318,494✔
5173
        };
5174
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
55,334,500✔
5175
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
4,665,944✔
5176
            int32_t   length = infos[iInfo].bind->length[iRow];
2,268✔
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;
198✔
5184
            }
5185
            value.pData = *data;
×
5186
            *data += length;
×
5187
          } else {
5188
            int32_t   length = infos[iInfo].bind->length[iRow];
4,700,022✔
5189
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
4,701,639✔
5190
            value.nData = length;
4,701,752✔
5191
            if (value.nData + VARSTR_HEADER_SIZE > infos[iInfo].bytes) {
4,701,752✔
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;
4,700,202✔
5198
            *data += length;
4,698,994✔
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) {
50,716,356✔
5203
            if (!pSchemaExt) {
792✔
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;
792✔
5209
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
792✔
5210
            Decimal128 dec = {0};
792✔
5211
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
792✔
5212
            int32_t    length = infos[iInfo].bind->length[iRow];
792✔
5213
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
792✔
5214
            *data += length;
792✔
5215
            hasDecimal128 = true;
792✔
5216
            TAOS_CHECK_GOTO(code, &lino, _exit);
792✔
5217

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

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

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

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

5242
          } else {
5243
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
50,711,389✔
5244
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
50,713,167✔
5245
              *val = 1;
×
5246
            }
5247
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
50,713,167✔
5248
          }
5249
        }
5250
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
55,494,530✔
5251
      }
5252
      if (taosArrayPush(colValArray, &colVal) == NULL) {
55,463,034✔
5253
        code = terrno;
×
5254
        goto _exit;
×
5255
      }
5256
    }
5257

5258
    SRow *row;
2,366,811✔
5259

5260
    if (hasBlob == 0) {
11,754,325✔
5261
      SRowBuildScanInfo sinfo = {0};
11,627,674✔
5262
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
11,628,611✔
5263
      TAOS_CHECK_GOTO(code, &lino, _exit);
11,619,810✔
5264
    } else {
5265
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
126,651✔
5266
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
126,444✔
5267
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5268
    }
5269

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

5275
    // fix decimal memory leak
5276
    if (hasDecimal128) {
11,618,031✔
5277
      int32_t num = taosArrayGetSize(colValArray);
792✔
5278
      for (int32_t i = 0; i < num; ++i) {
3,168✔
5279
        SColVal *pCol = taosArrayGet(colValArray, i);
2,376✔
5280
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
2,376✔
5281
          taosMemoryFreeClear(pCol->value.pData);
792✔
5282
        }
5283
      }
5284
      hasDecimal128 = false;
792✔
5285
    }
5286

5287
    if (pOrdered && pDupTs) {
11,618,031✔
5288
      tRowGetKey(row, &rowKey);
23,238,564✔
5289
      if (iRow == 0) {
11,623,254✔
5290
        *pOrdered = true;
1,040,328✔
5291
        *pDupTs = false;
1,040,328✔
5292
      } else {
5293
        if (*pOrdered) {
10,582,926✔
5294
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
10,583,976✔
5295
          *pOrdered = (res >= 0);
10,583,976✔
5296
          if (!*pDupTs) {
10,583,295✔
5297
            *pDupTs = (res == 0);
10,579,795✔
5298
          }
5299
        }
5300
      }
5301
      lastRowKey = rowKey;
11,622,419✔
5302
    }
5303
  }
5304
_exit:
1,028,264✔
5305
  if (code != 0) {
1,028,901✔
5306
    if (hasDecimal128) {
396✔
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);
396✔
5316
  }
5317
  taosArrayDestroy(colValArray);
1,028,901✔
5318
  taosArrayDestroy(bufArray);
1,041,147✔
5319
  return code;
1,041,981✔
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,
198✔
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) {
198✔
5334
    return TSDB_CODE_INVALID_PARA;
×
5335
  }
5336
  int8_t hasBlob = schemaHasBlob(pTSchema);
198✔
5337
  if (!infoSorted) {
198✔
5338
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5339
  }
5340

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

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

5367
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
1,188✔
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++) {
792✔
5376
    taosArrayClear(colValArray);
594✔
5377
    numOfFixedValue = 0;
594✔
5378

5379
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
2,376✔
5380
      if (parsedCols) {
1,782✔
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]) {
1,782✔
5395
        if (infos[iInfo].bind->is_null[iRow] == 1) {
396✔
5396
          if (iInfo == 0) {
396✔
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);
396✔
5401
        } else {
5402
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5403
        }
5404
      } else {
5405
        SValue value = {
1,386✔
5406
            .type = infos[iInfo].type,
1,386✔
5407
        };
5408
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
1,386✔
5409
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
396✔
5410
            int32_t   length = infos[iInfo].bind->length[iRow];
396✔
5411
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
396✔
5412
            value.nData = length;
396✔
5413
            if (value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE) > TSDB_MAX_BLOB_LEN) {
396✔
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;
396✔
5420
            *data += length;
396✔
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) {
990✔
5436
            if (!pSchemaExt) {
396✔
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;
396✔
5442
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
396✔
5443
            Decimal128 dec = {0};
396✔
5444
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
396✔
5445
            int32_t    length = infos[iInfo].bind->length[iRow];
396✔
5446
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
396✔
5447
            *data += length;
396✔
5448
            hasDecimal128 = true;
396✔
5449
            TAOS_CHECK_GOTO(code, &lino, _exit);
396✔
5450

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

5454
          } else if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL64) {
594✔
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;
594✔
5474
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
594✔
5475
              *val = 1;
×
5476
            }
5477
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
594✔
5478
          }
5479
        }
5480
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
1,386✔
5481
      }
5482
      if (taosArrayPush(colValArray, &colVal) == NULL) {
1,782✔
5483
        code = terrno;
×
5484
        goto _exit;
×
5485
      }
5486
    }
5487

5488
    SRow *row;
×
5489

5490
    if (hasBlob == 0) {
594✔
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};
594✔
5496
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, pBlobSet, &sinfo);
594✔
5497
      TAOS_CHECK_GOTO(code, &lino, _exit);
594✔
5498
    }
5499

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

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

5517
    if (pOrdered && pDupTs) {
594✔
5518
      tRowGetKey(row, &rowKey);
1,188✔
5519
      if (iRow == 0) {
594✔
5520
        *pOrdered = true;
198✔
5521
        *pDupTs = false;
198✔
5522
      } else {
5523
        if (*pOrdered) {
396✔
5524
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
396✔
5525
          *pOrdered = (res >= 0);
396✔
5526
          if (!*pDupTs) {
396✔
5527
            *pDupTs = (res == 0);
396✔
5528
          }
5529
        }
5530
      }
5531
      lastRowKey = rowKey;
594✔
5532
    }
5533
  }
5534
_exit:
198✔
5535
  if (code != 0) {
198✔
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);
198✔
5548
  taosArrayDestroy(bufArray);
198✔
5549
  return code;
198✔
5550
}
5551

5552
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
47,239,707✔
5553
  int32_t code = TSDB_CODE_SUCCESS;
47,239,707✔
5554

5555
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
47,239,707✔
5556
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
5,956,850✔
5557
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
5,357,392✔
5558
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
11,314,242✔
5559
    if (iToRow == 0) {
5,956,850✔
5560
      pToColData->aOffset[iToRow] = 0;
16,766✔
5561
    }
5562

5563
    if (iToRow < pToColData->nVal - 1) {
5,956,850✔
5564
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
5,942,476✔
5565
    }
5566

5567
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
5,956,850✔
5568
                 nData);
5569
  } else {
5570
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
41,282,857✔
5571
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
41,282,857✔
5572
  }
5573
  return code;
47,239,707✔
5574
}
5575

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

5581
  switch (pFromColData->flag) {
48,937,207✔
5582
    case HAS_NONE: {
×
5583
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5584
    } break;
×
5585
    case HAS_NULL: {
1,697,500✔
5586
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
1,697,500✔
5587
    } break;
1,697,500✔
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,363,408✔
5596
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
5,363,408✔
5597
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
5,363,408✔
5598
    } break;
5,363,408✔
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,876,299✔
5608
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
41,876,299✔
5609
      if (0 == bit_val)
41,876,299✔
5610
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
18,906,223✔
5611
      else
5612
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
22,970,076✔
5613
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
41,876,299✔
5614
    } break;
41,876,299✔
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,937,207✔
5624
}
5625

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

5630
  for (int32_t i = 0; i < nColData; i++) {
52,016,931✔
5631
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
48,937,207✔
5632
    if (code != TSDB_CODE_SUCCESS) {
48,937,207✔
5633
      return code;
×
5634
    }
5635
  }
5636

5637
  return code;
3,079,724✔
5638
}
5639

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

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

5655
  return code;
3,079,724✔
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);
1,414,158✔
5667
      key->pks[key->numOfPKs++] = cv.value;
1,414,158✔
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) {
347,267✔
5675
  SColData *aDstColData = NULL;
347,267✔
5676
  int32_t   i = start, j = mid + 1, k = 0;
347,267✔
5677
  SRowKey   keyi, keyj;
346,277✔
5678

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

5689
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
347,267✔
5690
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
347,267✔
5691
  while (i <= mid && j <= end) {
1,888,390✔
5692
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
1,541,123✔
5693
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
11,796✔
5694
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
11,796✔
5695
    } else {
5696
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,529,327✔
5697
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
1,529,327✔
5698
    }
5699
  }
5700

5701
  while (i <= mid) {
1,877,091✔
5702
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,529,824✔
5703
  }
5704

5705
  while (j <= end) {
356,044✔
5706
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
8,777✔
5707
  }
5708

5709
  for (i = start, k = 0; i <= end; ++i, ++k) {
3,426,991✔
5710
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
3,079,724✔
5711
  }
5712

5713
  if (aDstColData) {
347,267✔
5714
    for (int32_t i = 0; i < nColData; i++) {
5,798,569✔
5715
      tColDataDestroy(&aDstColData[i]);
5,451,302✔
5716
    }
5717
    taosMemoryFree(aDstColData);
347,267✔
5718
  }
5719

5720
  return TSDB_CODE_SUCCESS;
347,267✔
5721
}
5722

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

5727
  if (start >= end) {
697,489✔
5728
    return TSDB_CODE_SUCCESS;
350,222✔
5729
  }
5730

5731
  mid = (start + end) / 2;
347,267✔
5732

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

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

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

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

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

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

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

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

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

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

5775
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
1,233✔
5776
  SRowKey lastKey;
1,035✔
5777
  for (int32_t i = 0; i < numRows; i++) {
4,491✔
5778
    SRowKey key;
2,070✔
5779
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
3,258✔
5780

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

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

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

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

5825
int32_t tColDataSortMerge(SArray **arr) {
7,771,105✔
5826
  SArray   *colDataArr = *arr;
7,771,105✔
5827
  int32_t   nColData = TARRAY_SIZE(colDataArr);
7,774,177✔
5828
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
7,775,310✔
5829

5830
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
7,772,714✔
5831
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5832
  }
5833
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
7,769,104✔
5834
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5835
  }
5836
  if (!(aColData[0].flag == HAS_VALUE)) {
7,769,152✔
5837
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5838
  }
5839

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

5842
  int8_t doSort = 0;
7,710,524✔
5843
  int8_t doMerge = 0;
7,710,524✔
5844
  // scan -------
5845
  SRowKey lastKey;
7,670,284✔
5846
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
7,709,773✔
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) {
1,129,949✔
5856
      doSort = 1;
2,955✔
5857
      break;
2,955✔
5858
    } else {
5859
      doMerge = 1;
1,126,994✔
5860
    }
5861
  }
5862

5863
  // sort -------
5864
  if (doSort) {
15,968,477✔
5865
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
2,955✔
5866
  }
5867

5868
  if ((doMerge != 1) && (doSort == 1)) {
15,968,895✔
5869
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
2,955✔
5870
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
349,232✔
5871
      SRowKey key;
346,277✔
5872
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
346,475✔
5873

5874
      int32_t c = tRowKeyCompare(&lastKey, &key);
346,475✔
5875
      if (c == 0) {
346,475✔
5876
        doMerge = 1;
198✔
5877
        break;
198✔
5878
      }
5879
      lastKey = key;
346,277✔
5880
    }
5881
  }
5882

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

5889
_exit:
7,774,773✔
5890
  return 0;
7,775,821✔
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) {
48,194,202✔
5964
  int32_t code = 0;
48,194,202✔
5965

5966
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
96,391,477✔
5967
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
96,380,360✔
5968
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
96,383,585✔
5969
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
96,399,565✔
5970

5971
  // bitmap
5972
  switch (pColData->flag) {
48,199,065✔
5973
    case (HAS_NULL | HAS_NONE):
175,651✔
5974
    case (HAS_VALUE | HAS_NONE):
5975
    case (HAS_VALUE | HAS_NULL):
5976
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
175,651✔
5977
      if (code) return code;
175,651✔
5978
      break;
175,651✔
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:
47,999,288✔
5984
      break;
47,999,288✔
5985
  }
5986

5987
  // value
5988
  if (pColData->flag & HAS_VALUE) {
48,174,939✔
5989
    if (IS_VAR_DATA_TYPE(pColData->type)) {
48,105,637✔
5990
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
283,615✔
5991
      if (code) return code;
272,618✔
5992

5993
      code = tEncodeI32v(pEncoder, pColData->nData);
272,618✔
5994
      if (code) return code;
272,530✔
5995

5996
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
272,530✔
5997
      if (code) return code;
272,594✔
5998
    } else {
5999
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
47,834,536✔
6000
      if (code) return code;
47,812,841✔
6001
    }
6002
  }
6003

6004
  return code;
48,174,630✔
6005
}
6006

6007
static int32_t tDecodeColDataVersion0(SDecoder *pDecoder, SColData *pColData) {
33,802,880✔
6008
  int32_t code = 0;
33,802,880✔
6009

6010
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
67,606,124✔
6011
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
67,607,519✔
6012
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
67,606,744✔
6013
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
67,607,142✔
6014

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

6019
  // bitmap
6020
  switch (pColData->flag) {
33,802,432✔
6021
    case (HAS_NULL | HAS_NONE):
59,281✔
6022
    case (HAS_VALUE | HAS_NONE):
6023
    case (HAS_VALUE | HAS_NULL):
6024
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
59,281✔
6025
      if (code) return code;
59,281✔
6026
      break;
59,281✔
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:
33,744,996✔
6032
      break;
33,744,996✔
6033
  }
6034

6035
  // value
6036
  if (pColData->flag & HAS_VALUE) {
33,804,277✔
6037
    if (IS_VAR_DATA_TYPE(pColData->type)) {
33,753,306✔
6038
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
146,380✔
6039
      if (code) return code;
145,666✔
6040

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

6044
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
145,666✔
6045
      if (code) return code;
145,666✔
6046
    } else {
6047
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
33,604,790✔
6048
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
33,606,226✔
6049
      if (code) return code;
33,605,554✔
6050
    }
6051
  }
6052
  pColData->cflag = 0;
33,802,555✔
6053

6054
  return code;
33,801,447✔
6055
}
6056

6057
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
48,188,143✔
6058
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
48,188,143✔
6059
  if (code) return code;
48,168,373✔
6060
  return tEncodeI8(pEncoder, pColData->cflag);
96,354,115✔
6061
}
6062

6063
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
33,804,464✔
6064
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
33,804,464✔
6065
  if (code) return code;
33,801,097✔
6066

6067
  code = tDecodeI8(pDecoder, &pColData->cflag);
33,801,097✔
6068
  return code;
33,804,744✔
6069
}
6070

6071
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
48,166,813✔
6072
  if (version == 0) {
48,166,813✔
6073
    return tEncodeColDataVersion0(pEncoder, pColData);
×
6074
  } else if (version == 1) {
48,166,813✔
6075
    return tEncodeColDataVersion1(pEncoder, pColData);
×
6076
  } else if (version == 2) {
48,166,813✔
6077
    int32_t posStart = pEncoder->pos;
48,166,577✔
6078
    pEncoder->pos += INT_BYTES;
48,175,913✔
6079
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
48,177,471✔
6080
    if (code) return code;
48,182,738✔
6081
    int32_t posEnd = pEncoder->pos;
48,182,738✔
6082
    int32_t pos = posEnd - posStart;
48,182,901✔
6083
    pEncoder->pos = posStart;
48,182,901✔
6084
    code = tEncodeI32(pEncoder, pos);
48,194,748✔
6085
    pEncoder->pos = posEnd;
48,194,748✔
6086
    return code;
48,197,136✔
6087
  } else {
6088
    return TSDB_CODE_INVALID_PARA;
640✔
6089
  }
6090
}
6091

6092
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
49,516,753✔
6093
  if (version == 0) {
49,516,753✔
6094
    return tDecodeColDataVersion0(pDecoder, pColData);
×
6095
  } else if (version == 1) {
49,516,753✔
6096
    return tDecodeColDataVersion1(pDecoder, pColData);
×
6097
  } else if (version == 2) {
49,516,753✔
6098
    if (jump) {
49,516,753✔
6099
      int32_t len = 0;
15,711,947✔
6100
      int32_t code = tDecodeI32(pDecoder, &len);
15,712,331✔
6101
      if (code) return code;
15,712,331✔
6102
      pDecoder->pos += (len - INT_BYTES);
15,712,331✔
6103
      return TSDB_CODE_SUCCESS;
15,711,587✔
6104
    } else {
6105
      pDecoder->pos += INT_BYTES;
33,804,806✔
6106
      return tDecodeColDataVersion1(pDecoder, pColData);
33,804,470✔
6107
    }    
6108
  } else {
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) {
101,366✔
6129
  int32_t code = 0;
101,366✔
6130
  int32_t lino = 0;
101,366✔
6131
  uint8_t compressed = 0;
101,366✔
6132
  int32_t compressSize = 0;
101,366✔
6133
  char   *p = NULL;
101,366✔
6134

6135
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pRow->type));
202,732✔
6136
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pRow->len));
202,732✔
6137

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

6141
  // if (pRow->type) {
6142
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
101,366✔
6143
  while (pIter) {
56,012,992✔
6144
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
55,911,626✔
6145
    int32_t  idx = *(int32_t *)pIter;
55,911,626✔
6146
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
55,911,626✔
6147
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
55,911,626✔
6148

6149
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
55,911,626✔
6150
  }
6151
  //}
6152
  for (int32_t i = 0; i < nSeq; i++) {
56,012,992✔
6153
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
55,911,626✔
6154
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
111,823,252✔
6155
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
111,823,252✔
6156
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
111,823,252✔
6157
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
111,823,252✔
6158
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
111,823,252✔
6159
  }
6160

6161
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
202,732✔
6162

6163
_exit:
101,366✔
6164
  return code;
101,366✔
6165
}
6166

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

6180
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
50,435✔
6181

6182
  // if (pBlob->type) {
6183
  pBlob->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
50,435✔
6184
  if (pBlob->pSeqToffset == NULL) {
50,435✔
6185
    TAOS_CHECK_EXIT(terrno);
×
6186
  }
6187
  for (int32_t i = 0; i < nSeq; i++) {
28,005,584✔
6188
    uint64_t seq;
27,955,149✔
6189
    int32_t  idx;
27,955,149✔
6190
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &seq));
27,955,149✔
6191
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
27,955,149✔
6192

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

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

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

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

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

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

6225
_exit:
50,435✔
6226
  if (code != 0) {
50,435✔
6227
    if (pBlob != NULL) {
×
6228
      taosMemoryFree(pBlob->data);
×
6229
      taosArrayDestroy(pBlob->pSeqTable);
×
6230
      taosMemoryFree(pBlob);
×
6231
    }
6232
  }
6233
  return code;
50,435✔
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) {
9,049,979✔
6244
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,049,979✔
6245
  int16_t *numOfNull = &pAggs->numOfNull;
9,049,979✔
6246
  *sum = 0;
9,048,788✔
6247
  *max = 0;
9,048,788✔
6248
  *min = 1;
9,048,788✔
6249
  *numOfNull = 0;
9,048,635✔
6250

6251
  int8_t val;
6252
  if (HAS_VALUE == pColData->flag) {
9,048,635✔
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,165,594✔
6274

6275
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
9,063,729✔
6276
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,063,729✔
6277
  int16_t *numOfNull = &pAggs->numOfNull;
9,064,401✔
6278
  *sum = 0;
9,062,385✔
6279
  *max = INT8_MIN;
9,062,385✔
6280
  *min = INT8_MAX;
9,063,729✔
6281
  *numOfNull = 0;
9,063,729✔
6282

6283
  int8_t val;
6284
  if (HAS_VALUE == pColData->flag) {
9,063,729✔
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,309,308✔
6291
      switch (tColDataGetBitValue(pColData, iVal)) {
1,308,000✔
6292
        case 0:
1,043,348✔
6293
        case 1:
6294
          (*numOfNull)++;
1,043,348✔
6295
          break;
1,043,348✔
6296
        case 2:
264,652✔
6297
          val = ((int8_t *)pColData->pData)[iVal];
264,652✔
6298
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
264,652✔
6299
          break;
264,652✔
6300
        default:
×
6301
          break;
×
6302
      }
6303
    }
6304
  }
6305
}
9,251,437✔
6306

6307
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
9,050,511✔
6308
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,050,511✔
6309
  int16_t *numOfNull = &pAggs->numOfNull;
9,050,511✔
6310
  *sum = 0;
9,050,511✔
6311
  *max = INT16_MIN;
9,050,511✔
6312
  *min = INT16_MAX;
9,050,511✔
6313
  *numOfNull = 0;
9,049,167✔
6314

6315
  int16_t val;
6316
  if (HAS_VALUE == pColData->flag) {
9,049,839✔
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
}
9,210,334✔
6338

6339
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
29,327,783✔
6340
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
29,327,783✔
6341
  int16_t *numOfNull = &pAggs->numOfNull;
29,330,700✔
6342
  *sum = 0;
29,330,612✔
6343
  *max = INT32_MIN;
29,330,320✔
6344
  *min = INT32_MAX;
29,329,029✔
6345
  *numOfNull = 0;
29,329,046✔
6346

6347
  int32_t val;
6348
  if (HAS_VALUE == pColData->flag) {
29,326,490✔
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:
293,289,388✔
6357
        case 1:
6358
          (*numOfNull)++;
293,289,388✔
6359
          break;
293,289,388✔
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
}
29,692,728✔
6370

6371
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
71,903,927✔
6372
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
71,903,927✔
6373
  int16_t *numOfNull = &pAggs->numOfNull;
71,904,918✔
6374
  *sum = 0;
71,904,918✔
6375
  *max = INT64_MIN;
71,904,918✔
6376
  *min = INT64_MAX;
71,904,918✔
6377
  *numOfNull = 0;
71,905,715✔
6378

6379
  int64_t val;
6380
  if (HAS_VALUE == pColData->flag) {
71,905,715✔
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:
324,003,405✔
6389
        case 1:
6390
          (*numOfNull)++;
324,003,405✔
6391
          break;
324,003,405✔
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
}
100,172,449✔
6402

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

6411
  float val;
6412
  if (HAS_VALUE == pColData->flag) {
31,414,138✔
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,878,770✔
6419
      switch (tColDataGetBitValue(pColData, iVal)) {
11,860,935✔
6420
        case 0:
2,081,194✔
6421
        case 1:
6422
          (*numOfNull)++;
2,081,194✔
6423
          break;
2,081,194✔
6424
        case 2:
9,779,741✔
6425
          val = ((float *)pColData->pData)[iVal];
9,779,741✔
6426
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
9,779,741✔
6427
          break;
9,779,741✔
6428
        default:
×
6429
          break;
×
6430
      }
6431
    }
6432
  }
6433
}
26,084,315✔
6434

6435
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
16,571,875✔
6436
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
16,571,875✔
6437
  int16_t *numOfNull = &pAggs->numOfNull;
16,573,652✔
6438
  *(double *)sum = 0;
16,572,909✔
6439
  *(double *)max = -DBL_MAX;
16,572,980✔
6440
  *(double *)min = DBL_MAX;
16,572,300✔
6441
  *numOfNull = 0;
16,571,168✔
6442

6443
  double val;
6444
  if (HAS_VALUE == pColData->flag) {
16,571,256✔
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,309,308✔
6451
      switch (tColDataGetBitValue(pColData, iVal)) {
1,308,000✔
6452
        case 0:
1,050,324✔
6453
        case 1:
6454
          (*numOfNull)++;
1,050,324✔
6455
          break;
1,050,324✔
6456
        case 2:
257,676✔
6457
          val = ((double *)pColData->pData)[iVal];
257,676✔
6458
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
257,676✔
6459
          break;
257,676✔
6460
        default:
×
6461
          break;
×
6462
      }
6463
    }
6464
  }
6465
}
16,944,667✔
6466

6467
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,686,484✔
6468
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,686,484✔
6469
  int16_t *numOfNull = &pAggs->numOfNull;
8,687,828✔
6470
  *(uint64_t *)sum = 0;
8,687,156✔
6471
  *(uint64_t *)max = 0;
8,687,156✔
6472
  *(uint64_t *)min = UINT8_MAX;
8,687,156✔
6473
  *numOfNull = 0;
8,687,828✔
6474

6475
  uint8_t val;
6476
  if (HAS_VALUE == pColData->flag) {
8,686,484✔
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,836,026✔
6498

6499
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,685,812✔
6500
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,685,812✔
6501
  int16_t *numOfNull = &pAggs->numOfNull;
8,686,484✔
6502
  *(uint64_t *)sum = 0;
8,685,812✔
6503
  *(uint64_t *)max = 0;
8,686,484✔
6504
  *(uint64_t *)min = UINT16_MAX;
8,687,156✔
6505
  *numOfNull = 0;
8,686,484✔
6506

6507
  uint16_t val;
6508
  if (HAS_VALUE == pColData->flag) {
8,685,812✔
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,729,492✔
6530

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

6539
  uint32_t val;
6540
  if (HAS_VALUE == pColData->flag) {
8,687,156✔
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,709,268✔
6562

6563
static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,686,546✔
6564
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,686,546✔
6565
  int16_t *numOfNull = &pAggs->numOfNull;
8,687,828✔
6566
  *(uint64_t *)sum = 0;
8,686,484✔
6567
  *(uint64_t *)max = 0;
8,687,156✔
6568
  *(uint64_t *)min = UINT64_MAX;
8,686,484✔
6569
  *numOfNull = 0;
8,686,484✔
6570

6571
  uint64_t val;
6572
  if (HAS_VALUE == pColData->flag) {
8,686,484✔
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,672,564✔
6594

6595
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
32,852,170✔
6596
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
32,852,170✔
6597
  int16_t *numOfNull = &pAggs->numOfNull;
32,854,034✔
6598
  *(uint64_t *)sum = 0;
32,853,515✔
6599
  *(uint64_t *)max = 0;
32,852,094✔
6600
  *(uint64_t *)min = 0;
32,852,842✔
6601
  *numOfNull = 0;
32,852,842✔
6602

6603
  switch (pColData->flag) {
32,850,826✔
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,846,894✔
6610
      *numOfNull = 0;
32,846,894✔
6611
      break;
32,845,703✔
6612
    case (HAS_VALUE | HAS_NULL):
3,932✔
6613
    case (HAS_VALUE | HAS_NONE):
6614
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
9,183,932✔
6615
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
9,180,000✔
6616
          (*numOfNull)++;
1,202,096✔
6617
        }
6618
      }
6619
      break;
3,932✔
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,849,635✔
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) {
19,243✔
6645
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum;
19,243✔
6646
  Decimal64  *pMax = (Decimal64 *)pAggs->decimal128Max, *pMin = (Decimal64 *)pAggs->decimal128Min;
19,243✔
6647
  uint8_t    *pOverflow = &pAggs->overflow;
19,243✔
6648
  *pSum = DECIMAL128_ZERO;
19,243✔
6649
  *pMax = DECIMAL64_MIN;
19,243✔
6650
  *pMin = DECIMAL64_MAX;
19,243✔
6651
  pAggs->numOfNull = 0;
19,243✔
6652
  pAggs->colId |= DECIMAL_AGG_FLAG;
19,243✔
6653

6654
  Decimal64         *pVal = NULL;
19,243✔
6655
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
19,243✔
6656
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
19,243✔
6657
  if (HAS_VALUE == pColData->flag) {
19,243✔
6658
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
330,068✔
6659
      pVal = ((Decimal64 *)pColData->pData) + iVal;
326,800✔
6660
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
326,800✔
6661
    }
6662
  } else {
6663
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
13,027,716✔
6664
      switch (tColDataGetBitValue(pColData, iVal)) {
13,011,741✔
6665
        case 0:
605,142✔
6666
        case 1:
6667
          pAggs->numOfNull++;
605,142✔
6668
          break;
601,874✔
6669
        case 2:
12,414,769✔
6670
          pVal = ((Decimal64 *)pColData->pData) + iVal;
12,414,769✔
6671
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
12,422,939✔
6672
          break;
12,409,867✔
6673
        default:
×
6674
          break;
×
6675
      }
6676
    }
6677
  }
6678
}
19,243✔
6679

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

6690
  Decimal128        *pVal = NULL;
47,899✔
6691
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
47,899✔
6692
  if (HAS_VALUE == pColData->flag) {
47,899✔
6693
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
165,034✔
6694
      pVal = ((Decimal128 *)pColData->pData) + iVal;
163,400✔
6695
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
163,400✔
6696
    }
6697
  } else {
6698
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
41,892,929✔
6699
      switch (tColDataGetBitValue(pColData, iVal)) {
41,847,481✔
6700
        case 0:
1,880,960✔
6701
        case 1:
6702
          pAggs->numOfNull++;
1,880,960✔
6703
          break;
1,878,509✔
6704
        case 2:
39,965,704✔
6705
          pVal = ((Decimal128 *)pColData->pData) + iVal;
39,965,704✔
6706
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
39,968,972✔
6707
          break;
39,968,155✔
6708
        default:
×
6709
          break;
×
6710
      }
6711
    }
6712
  }
6713
}
47,082✔
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,211,797,810✔
6742
  valCol->type = TSDB_DATA_TYPE_NULL;
1,211,797,810✔
6743
  valCol->numOfValues = 0;
1,211,842,125✔
6744
  tBufferInit(&valCol->data);
1,211,823,180✔
6745
  tBufferInit(&valCol->offsets);
1,211,852,446✔
6746
  return 0;
1,211,937,923✔
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,722,570,200✔
6758
  valCol->type = TSDB_DATA_TYPE_NULL;
1,722,570,200✔
6759
  valCol->numOfValues = 0;
1,722,660,379✔
6760
  tBufferClear(&valCol->data);
1,722,683,184✔
6761
  tBufferClear(&valCol->offsets);
1,722,672,727✔
6762
  return;
1,722,669,537✔
6763
}
6764

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

6768
  if (valCol->numOfValues == 0) {
1,927,280✔
6769
    valCol->type = value->type;
670,114✔
6770
  }
6771

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

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

6789
  return 0;
1,927,280✔
6790
}
6791

6792
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
62,922,113✔
6793
  int32_t code;
6794

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

6799
  if (IS_VAR_DATA_TYPE(valCol->type)) {
62,923,194✔
6800
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
1,888,236✔
6801
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
1,888,124✔
6802
    int32_t  oldDataSize = nextOffset - offsets[idx];
1,888,124✔
6803
    int32_t  bytesAdded = value->nData - oldDataSize;
1,888,124✔
6804

6805
    if (bytesAdded != 0) {
1,888,124✔
6806
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
44,416✔
6807
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
22,208✔
6808
              tBufferGetSize(&valCol->data) - nextOffset);
22,208✔
6809
      valCol->data.size += bytesAdded;
22,208✔
6810

6811
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
22,208✔
6812
        offsets[i] += bytesAdded;
×
6813
      }
6814
    }
6815
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
1,888,124✔
6816
  } else {
6817
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
61,034,703✔
6818
                        tDataTypes[valCol->type].bytes);
61,034,517✔
6819
  }
6820
  return 0;
6821
}
6822

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

6828
  value->type = valCol->type;
572,362,176✔
6829
  if (IS_VAR_DATA_TYPE(value->type)) {
759,383,619✔
6830
    int32_t       offset, nextOffset;
187,026,674✔
6831
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
187,026,306✔
6832

6833
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
187,026,306✔
6834
    if (idx == valCol->numOfValues - 1) {
187,020,087✔
6835
      nextOffset = tBufferGetSize(&valCol->data);
31,706,798✔
6836
    } else {
6837
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
155,314,671✔
6838
    }
6839
    value->nData = nextOffset - offset;
187,022,809✔
6840
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
187,018,014✔
6841
  } else {
6842
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
385,343,403✔
6843
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
770,690,577✔
6844
  }
6845
  return 0;
572,336,708✔
6846
}
6847

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

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

6855
  (*info) = (SValueColumnCompressInfo){
669,402✔
6856
      .cmprAlg = info->cmprAlg,
670,114✔
6857
      .type = valCol->type,
670,114✔
6858
  };
6859

6860
  // offset
6861
  if (IS_VAR_DATA_TYPE(valCol->type)) {
669,402✔
6862
    SCompressInfo cinfo = {
233,666✔
6863
        .cmprAlg = info->cmprAlg,
234,378✔
6864
        .dataType = TSDB_DATA_TYPE_INT,
6865
        .originalSize = valCol->offsets.size,
233,666✔
6866
    };
6867

6868
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
234,378✔
6869
    if (code) return code;
235,090✔
6870

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

6875
  // data
6876
  SCompressInfo cinfo = {
669,402✔
6877
      .cmprAlg = info->cmprAlg,
1,339,478✔
6878
      .dataType = valCol->type,
669,402✔
6879
      .originalSize = valCol->data.size,
670,114✔
6880
  };
6881

6882
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
669,402✔
6883
  if (code) return code;
670,114✔
6884

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

6888
  return 0;
670,114✔
6889
}
6890

6891
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
188,843,845✔
6892
                               SBuffer *assist) {
6893
  int32_t code;
6894

6895
  tValueColumnClear(valCol);
188,843,845✔
6896
  valCol->type = info->type;
188,850,755✔
6897
  // offset
6898
  if (IS_VAR_DATA_TYPE(valCol->type)) {
251,657,608✔
6899
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
62,802,728✔
6900

6901
    SCompressInfo cinfo = {
62,799,964✔
6902
        .dataType = TSDB_DATA_TYPE_INT,
6903
        .cmprAlg = info->cmprAlg,
62,800,634✔
6904
        .originalSize = info->offsetOriginalSize,
62,799,231✔
6905
        .compressedSize = info->offsetCompressedSize,
62,795,755✔
6906
    };
6907

6908
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
62,798,582✔
6909
    if (code) {
62,808,947✔
6910
      return code;
×
6911
    }
6912
  } else {
6913
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
126,044,572✔
6914
  }
6915

6916
  // data
6917
  SCompressInfo cinfo = {
188,857,665✔
6918
      .dataType = valCol->type,
188,850,043✔
6919
      .cmprAlg = info->cmprAlg,
188,859,738✔
6920
      .originalSize = info->dataOriginalSize,
188,852,786✔
6921
      .compressedSize = info->dataCompressedSize,
188,850,713✔
6922
  };
6923

6924
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
188,854,168✔
6925
  if (code) {
188,863,905✔
6926
    return code;
×
6927
  }
6928

6929
  return 0;
188,863,905✔
6930
}
6931

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

6936
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
1,338,804✔
6937
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
1,338,804✔
6938
  if ((code = tBufferPutI8(buffer, info->type))) return code;
1,338,804✔
6939
  if (IS_VAR_DATA_TYPE(info->type)) {
669,402✔
6940
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
468,756✔
6941
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
469,468✔
6942
  }
6943
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
1,339,516✔
6944
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
1,338,804✔
6945

6946
  return 0;
669,402✔
6947
}
6948

6949
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
188,845,248✔
6950
  int32_t code;
6951
  uint8_t fmtVer;
188,845,248✔
6952

6953
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
188,845,918✔
6954
  if (fmtVer == 0) {
188,852,849✔
6955
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
188,852,849✔
6956
    if ((code = tBufferGetI8(reader, &info->type))) return code;
188,851,425✔
6957
    if (IS_VAR_DATA_TYPE(info->type)) {
188,858,377✔
6958
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
62,806,204✔
6959
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
62,801,325✔
6960
    } else {
6961
      info->offsetOriginalSize = 0;
126,051,482✔
6962
      info->offsetCompressedSize = 0;
126,049,388✔
6963
    }
6964
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
188,860,429✔
6965
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
188,855,592✔
6966
  } else {
6967
    return TSDB_CODE_INVALID_PARA;
×
6968
  }
6969

6970
  return 0;
188,863,193✔
6971
}
6972

6973
int32_t tCompressData(void          *input,       // input
1,022,453,812✔
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,022,453,812✔
6983
  if (!(outputSize >= extraSizeNeeded)) {
1,022,541,575✔
6984
    return TSDB_CODE_INVALID_PARA;
×
6985
  }
6986

6987
  if (info->cmprAlg == NO_COMPRESSION) {
1,022,541,575✔
6988
    (void)memcpy(output, input, info->originalSize);
36,832✔
6989
    info->compressedSize = info->originalSize;
36,832✔
6990
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
1,148,097,202✔
6991
    SBuffer local;
125,620,842✔
6992

6993
    tBufferInit(&local);
6994
    if (buffer == NULL) {
125,630,509✔
6995
      buffer = &local;
×
6996
    }
6997

6998
    if (info->cmprAlg == TWO_STAGE_COMP) {
125,630,509✔
6999
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
125,507,233✔
7000
      if (code) {
125,501,961✔
7001
        tBufferDestroy(&local);
7002
        return code;
×
7003
      }
7004
    }
7005

7006
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
376,871,053✔
7007
        input,                                                   // input
7008
        info->originalSize,                                      // input size
7009
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
125,621,325✔
7010
        output,                                                  // output
7011
        outputSize,                                              // output size
7012
        info->cmprAlg,                                           // compression algorithm
125,615,443✔
7013
        buffer->data,                                            // buffer
7014
        buffer->capacity                                         // buffer size
125,619,658✔
7015
    );
7016
    if (info->compressedSize < 0) {
125,631,141✔
7017
      tBufferDestroy(&local);
7018
      return TSDB_CODE_COMPRESS_ERROR;
×
7019
    }
7020

7021
    tBufferDestroy(&local);
7022
  } else {
7023
    DEFINE_VAR(info->cmprAlg)
896,894,016✔
7024
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
896,884,215✔
7025
      (void)memcpy(output, input, info->originalSize);
7,608✔
7026
      info->compressedSize = info->originalSize;
7,608✔
7027
      return 0;
7,608✔
7028
    }
7029
    SBuffer local;
896,868,329✔
7030

7031
    tBufferInit(&local);
7032
    if (buffer == NULL) {
896,897,168✔
7033
      buffer = &local;
486,111✔
7034
    }
7035
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
896,897,168✔
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
896,902,535✔
7041
        output,                                                     // output
7042
        outputSize,                                                 // output size
7043
        info->cmprAlg,                                              // compression algorithm
7044
        buffer->data,                                               // buffer
7045
        buffer->capacity                                            // buffer size
896,850,752✔
7046
    );
7047
    if (info->compressedSize < 0) {
896,916,301✔
7048
      tBufferDestroy(&local);
7049
      return TSDB_CODE_COMPRESS_ERROR;
×
7050
    }
7051

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

7056
  return 0;
1,022,542,950✔
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)) {
6,672✔
7073
      return TSDB_CODE_INVALID_PARA;
×
7074
    }
7075
    (void)memcpy(output, input, info->compressedSize);
6,672✔
7076
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
7077
    SBuffer local;
2,147,483,647✔
7078

7079
    tBufferInit(&local);
7080
    if (buffer == NULL) {
2,147,483,647✔
7081
      buffer = &local;
×
7082
    }
7083

7084
    if (info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
7085
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
2,147,483,647✔
7086
      if (code) {
2,147,483,647✔
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
2,147,483,647✔
7095
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
2,147,483,647✔
7096
        output,                                                 // output
7097
        outputSize,                                             // output size
7098
        info->cmprAlg,                                          // compression algorithm
2,147,483,647✔
7099
        buffer->data,                                           // helper buffer
7100
        buffer->capacity                                        // extra buffer size
2,147,483,647✔
7101
    );
7102
    if (decompressedSize < 0) {
2,147,483,647✔
7103
      tBufferDestroy(&local);
7104
      return TSDB_CODE_COMPRESS_ERROR;
×
7105
    }
7106

7107
    if (!(decompressedSize == info->originalSize)) {
2,147,483,647✔
7108
      return TSDB_CODE_COMPRESS_ERROR;
×
7109
    }
7110
    tBufferDestroy(&local);
7111
  } else {
7112
    DEFINE_VAR(info->cmprAlg);
2,147,483,647✔
7113
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
2,147,483,647✔
7114
      (void)memcpy(output, input, info->compressedSize);
15,216✔
7115
      return 0;
15,216✔
7116
    }
7117
    SBuffer local;
2,147,483,647✔
7118

7119
    tBufferInit(&local);
7120
    if (buffer == NULL) {
2,147,483,647✔
7121
      buffer = &local;
442,900✔
7122
    }
7123
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
2,147,483,647✔
7124
    if (code) {
2,147,483,647✔
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,147,483,647✔
7131
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
2,147,483,647✔
7132
        output,                                                 // output
7133
        outputSize,                                             // output size
7134
        info->cmprAlg,                                          // compression algorithm
2,147,483,647✔
7135
        buffer->data,                                           // helper buffer
7136
        buffer->capacity                                        // extra buffer size
2,147,483,647✔
7137
    );
7138
    if (decompressedSize < 0) {
2,147,483,647✔
7139
      tBufferDestroy(&local);
7140
      return TSDB_CODE_COMPRESS_ERROR;
×
7141
    }
7142

7143
    if (!(decompressedSize == info->originalSize)) {
2,147,483,647✔
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,021,476,771✔
7153
  int32_t code;
7154

7155
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
1,021,476,771✔
7156
  if (code) return code;
1,021,477,518✔
7157

7158
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,021,477,518✔
7159
  if (code) return code;
1,021,573,751✔
7160

7161
  output->size += info->compressedSize;
1,021,573,751✔
7162
  return 0;
1,021,617,046✔
7163
}
7164

7165
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
2,147,483,647✔
7166
  int32_t code;
7167

7168
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
2,147,483,647✔
7169
  if (code) return code;
2,147,483,647✔
7170

7171
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
2,147,483,647✔
7172
  if (code) return code;
2,147,483,647✔
7173

7174
  output->size += info->originalSize;
2,147,483,647✔
7175
  return 0;
2,147,483,647✔
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;
208,972,715✔
7182
    pVal->nData = len;
1,189,994,522✔
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:
164,368✔
7198
        break;
164,368✔
7199
    }
7200
  }
7201
}
2,147,483,647✔
7202

7203
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
2,147,483,647✔
7204
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647✔
7205
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
416,468,209✔
7206
    pDst->nData = pSrc->nData;
416,907,725✔
7207
  } else {
7208
    pDst->val = pSrc->val;
2,147,483,647✔
7209
  }
7210
}
2,147,483,647✔
7211
void valueClearDatum(SValue *pVal, int8_t type) {
478,242✔
7212
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
478,242✔
7213
    taosMemoryFreeClear(pVal->pData);
25,504✔
7214
    pVal->nData = 0;
25,504✔
7215
  } else {
7216
    pVal->val = 0;
452,738✔
7217
  }
7218
}
478,242✔
7219

7220
int8_t schemaHasBlob(const STSchema *pSchema) {
683,237,072✔
7221
  if (pSchema == NULL) {
683,237,072✔
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;
61,097✔
7227
    }
7228
  }
7229
  return 0;
683,254,435✔
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