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

taosdata / TDengine / #5010

29 Mar 2026 04:32AM UTC coverage: 72.292% (+0.03%) from 72.26%
#5010

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253774 of 351039 relevant lines covered (72.29%)

133420324.04 hits per line

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

68.74
/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) {
229,694,151✔
79
  int32_t n = 0;
229,694,151✔
80
  n += tPutI8(p ? p + n : p, index->type);
229,694,151✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
229,697,903✔
82
  return n;
229,696,895✔
83
}
84

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

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

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

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

109
  if (isPK) {
2,147,483,647✔
110
    sinfo->tupleIndices[sinfo->numOfPKs].type = colVal->value.type;
76,566,761✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
76,566,341✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
76,566,705✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
76,566,593✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
76,566,593✔
115
    sinfo->numOfPKs++;
76,565,389✔
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
49,173,650✔
122
                             + BSE_SEQUECE_SIZE;                     // value
24,586,825✔
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
49,173,650✔
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
24,586,825✔
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
49,173,650✔
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;
211,008✔
164
        break;
105,504✔
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,128✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
89,024✔
185
        break;
44,512✔
186
      } else {  // skip useless value
187
        colValIndex++;
×
188
      }
189
    }
190
  }
191

192
  if (sinfo->numOfNone) {
2,147,483,647✔
193
    sinfo->flag |= HAS_NONE;
2,147,483,647✔
194
  }
195
  if (sinfo->numOfNull) {
2,147,483,647✔
196
    sinfo->flag |= HAS_NULL;
1,959,627,423✔
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,410,768✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
2,410,768✔
216
      sinfo->tupleFixedSize = 0;
2,410,179✔
217
      break;
2,410,768✔
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,123,691,931✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,123,691,931✔
225
      sinfo->tupleFixedSize = schema->flen;
1,123,724,326✔
226
      break;
973,225,206✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
76,565,585✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
76,566,313✔
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) {
55,245,833✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
55,424,618✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
55,443,882✔
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;
76,566,845✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
76,567,125✔
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);
70,676,843✔
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);
14,144✔
296
        break;
14,144✔
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
736,721,561✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
686,969,967✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
51,267,737✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
51,281,872✔
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
526,792✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
52✔
325
        break;
52✔
326
      } else {
327
        colValIndex++;
526,740✔
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,501,314✔
336
  SBlobItem item = {.type = type, .len = 0};
2,501,314✔
337
  uint64_t  seq = 0;
2,501,314✔
338
  int32_t   code = tBlobSetPush(pBlobSet, &item, &seq, 0);
2,501,314✔
339
  if (code != 0) return code;
2,501,314✔
340

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

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

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

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

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

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

395
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
74,536✔
396
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
20,638✔
397
              hasBlob = 1;
4,146✔
398
            }
399
            *(int32_t *)(fixed + schema->columns[i].offset) = varlen - fixed - sinfo->tupleFixedSize;
20,638✔
400
            varlen += tPutU32v(varlen, colValArray[colValIndex].value.nData);
41,276✔
401
            if (colValArray[colValIndex].value.nData) {
20,638✔
402
              if (hasBlob == 0) {
20,638✔
403
                (void)memcpy(varlen, colValArray[colValIndex].value.pData, colValArray[colValIndex].value.nData);
16,492✔
404
                varlen += colValArray[colValIndex].value.nData;
16,492✔
405
              } else {
406
                uint64_t  seq = 0;
4,146✔
407
                SBlobItem item = {.seqOffsetInRow = varlen - (*ppRow)->data,
4,146✔
408
                                  .data = colValArray[colValIndex].value.pData,
4,146✔
409
                                  .len = colValArray[colValIndex].value.nData,
4,146✔
410
                                  .type = TSDB_DATA_BLOB_VALUE};
411
                code = tBlobSetPush(pBlobSet, &item, &seq, firstBlobCol);
4,146✔
412
                if (firstBlobCol == 1) {
4,146✔
413
                  firstBlobCol = 0;
4,146✔
414
                }
415
                if (code != 0) return code;
4,146✔
416
                varlen += tPutU64(varlen, seq);
8,292✔
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,
53,898✔
423
                         VALUE_GET_DATUM(&colValArray[colValIndex].value, schema->columns[i].type),
53,898✔
424
                         tDataTypes[schema->columns[i].type].bytes);
53,898✔
425
          }
426
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
9,345✔
427
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
445✔
428
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
445✔
429
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
445✔
430
          }
431
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
8,900✔
432
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
8,900✔
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);
8,900✔
436
        }
437

438
        colValIndex++;
83,881✔
439
        break;
83,881✔
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,591✔
450
    return TSDB_CODE_INVALID_PARA;
×
451
  }
452

453
  return 0;
4,591✔
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) {
733,079,242✔
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
733,079,242✔
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);
5,889,946✔
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;
37,240✔
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,070,457,900✔
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
44,395✔
637
        break;
44,460✔
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,
24,584,068✔
647
                                      SRow **ppRow, SBlobSet *ppBlobSet) {
648
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
24,584,068✔
649

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

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

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

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

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

678
  int32_t numOfColVals = TARRAY_SIZE(aColVal);
24,584,068✔
679
  int32_t colValIndex = 1;
24,584,068✔
680
  int8_t  firstBlobCol = 1;
24,584,068✔
681
  for (int32_t i = 1; i < schema->numOfCols; i++) {
55,433,890✔
682
    for (;;) {
×
683
      if (colValIndex >= numOfColVals) {  // NONE
30,849,822✔
684
        break;
×
685
      }
686
      uint8_t hasBlob = 0;
30,849,822✔
687

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1004
  return code;
×
1005
}
1006

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

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

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

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

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

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

1048
  return code;
×
1049
}
1050

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

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

1077
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
25,096,588✔
1078
  if (code) return code;
25,096,588✔
1079

1080
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
25,096,588✔
1081
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
512,520✔
1082
  } else {
1083
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
24,584,068✔
1084
  }
1085

1086
  return code;
25,096,588✔
1087
}
1088

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

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

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

1102
  return code;
×
1103
}
1104

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

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

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

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

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

1134
  p->cap = cap;
46,657✔
1135
  p->len = 0;
46,657✔
1136
  p->seq = 1;
46,657✔
1137

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

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

1164
  uint64_t tlen = pBlobSet->len + len;
25,104,638✔
1165
  if (tlen > pBlobSet->cap) {
25,104,638✔
1166
    int64_t cap = pBlobSet->cap;
20,916✔
1167
    // opt later
1168
    do {
1169
      cap = cap * 2;
20,916✔
1170
    } while (tlen > cap);
20,916✔
1171

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

1183
  offset = pBlobSet->len;
25,104,638✔
1184
  pBlobSet->len += len;
25,104,638✔
1185

1186
  pBlobSet->seq++;
25,104,638✔
1187
  *seq = pBlobSet->seq;
25,104,638✔
1188

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

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

1200
_exit:
25,104,638✔
1201
  return code;
25,104,638✔
1202
}
1203

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

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

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

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

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

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

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

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

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

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

1257
  return code;
×
1258
}
1259

1260
int32_t tBlobSetSize(SBlobSet *pBlobSet) {
1,031,062,370✔
1261
  if (pBlobSet == NULL) return 0;
1,031,062,370✔
1262
  return taosArrayGetSize(pBlobSet->pSeqTable);
164,940✔
1263
}
1264

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

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

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

1309
  if (!infoSorted) {
1,686,805✔
1310
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1311
  }
1312

1313
  int32_t code = 0;
1,686,805✔
1314
  int32_t numOfRows = infos[0].bind->num;
1,686,805✔
1315
  SArray *colValArray;
1316
  SColVal colVal;
1,663,701✔
1317

1318
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
1,686,891✔
1319
    return terrno;
×
1320
  }
1321

1322
  SRowKey rowKey, lastRowKey;
1,662,558✔
1323
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
15,970,994✔
1324
    taosArrayClear(colValArray);
14,601,466✔
1325

1326
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
74,212,547✔
1327
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
59,368,303✔
1328
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
928✔
1329
      } else {
1330
        SValue value = {
59,379,831✔
1331
            .type = infos[iInfo].type,
59,421,330✔
1332
        };
1333
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
59,418,110✔
1334
          value.nData = infos[iInfo].bind->length[iRow];
3,825,946✔
1335
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
3,885,481✔
1336
            code = TSDB_CODE_INVALID_PARA;
×
1337
            goto _exit;
×
1338
          }
1339
          value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
3,886,126✔
1340
        } else {
1341
          valueSetDatum(&value, infos[iInfo].type,
55,668,914✔
1342
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
55,691,285✔
1343
                        infos[iInfo].bind->buffer_length);
55,704,647✔
1344
        }
1345
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
60,298,803✔
1346
      }
1347
      if (taosArrayPush(colValArray, &colVal) == NULL) {
59,708,786✔
1348
        code = terrno;
×
1349
        goto _exit;
×
1350
      }
1351
    }
1352

1353
    SRow             *row;
3,518,302✔
1354
    SRowBuildScanInfo sinfo = {0};
14,705,711✔
1355
    if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
14,705,839✔
1356
      goto _exit;
×
1357
    }
1358

1359
    if ((taosArrayPush(rowArray, &row)) == NULL) {
14,307,183✔
1360
      code = terrno;
×
1361
      goto _exit;
×
1362
    }
1363

1364
    if (pOrdered && pDupTs) {
14,307,183✔
1365
      tRowGetKey(row, &rowKey);
28,704,684✔
1366
      if (iRow == 0) {
14,328,113✔
1367
        *pOrdered = true;
1,686,573✔
1368
        *pDupTs = false;
1,686,616✔
1369
      } else {
1370
        if (*pOrdered) {
12,641,540✔
1371
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
12,669,222✔
1372
          *pOrdered = (res >= 0);
12,669,222✔
1373
          if (!*pDupTs) {
12,665,374✔
1374
            *pDupTs = (res == 0);
12,656,771✔
1375
          }
1376
        }
1377
      }
1378
      lastRowKey = rowKey;
14,327,608✔
1379
    }
1380
  }
1381

1382
_exit:
1,669,937✔
1383
  taosArrayDestroy(colValArray);
1,668,713✔
1384
  return code;
1,685,779✔
1385
}
1386

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

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

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

1401
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
1402
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
22,059,029✔
1403
    return 0;
22,057,729✔
1404
  }
1405

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

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

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

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

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

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

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

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

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

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

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

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

1502
    if (bit == BIT_FLG_NONE) {
2,147,483,647✔
1503
      *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,721,901✔
1504
      return 0;
1,721,901✔
1505
    } else if (bit == BIT_FLG_NULL) {
2,147,483,647✔
1506
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,823,610,603✔
1507
      return 0;
1,823,617,035✔
1508
    }
1509

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

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

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

1532
static int32_t tRowPCmprFn(const void *p1, const void *p2) {
401,336,940✔
1533
  SRowKey key1, key2;
401,309,110✔
1534
  tRowGetKey(*(SRow **)p1, &key1);
814,071,242✔
1535
  tRowGetKey(*(SRow **)p2, &key2);
820,379,586✔
1536
  return tRowKeyCompare(&key1, &key2);
413,503,832✔
1537
}
1538
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
40,700,619✔
1539

1540
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
91,760,893✔
1541
  SColVal* pColVal = tRowIterNext(iter);
91,760,893✔
1542
  while (pColVal != NULL && pColVal->cid < targetCid) {
117,300,864✔
1543
    pColVal = tRowIterNext(iter);
25,542,257✔
1544
  }
1545
  return pColVal;
91,758,907✔
1546
}
1547

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

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

1562
  for (int32_t i = 0; i < nRow; i++) {
47,151,638✔
1563
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
40,691,723✔
1564

1565
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
40,679,803✔
1566
    if (code) goto _exit;
40,694,873✔
1567
  }
1568

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

1576
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
77,625,499✔
1577
    int32_t targetCid = pTSchema->columns[iCol].colId;
71,160,358✔
1578
    SColVal *pColVal = NULL;
71,161,258✔
1579

1580
    switch (strategy) {
71,161,258✔
1581
      case KEEP_CONSISTENCY:
4,319,048✔
1582
        if (nRow > 0)
4,319,048✔
1583
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
4,319,048✔
1584
        break;
4,318,832✔
1585

1586
      default:  // default using PREFER_NON_NULL strategy
66,842,210✔
1587
      case PREFER_NON_NULL:
1588
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
105,215,162✔
1589
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
87,445,565✔
1590

1591
          if (COL_VAL_IS_VALUE(pColValT)) {
87,438,755✔
1592
            pColVal = pColValT;
49,066,673✔
1593
            break;
49,066,673✔
1594
          } else if (pColVal == NULL) {
38,372,952✔
1595
            pColVal = pColValT;
19,414,511✔
1596
          }
1597
        }
1598
        break;
66,836,270✔
1599
    }
1600

1601
    if (pColVal) {
71,155,102✔
1602
      if (taosArrayPush(aColVal, pColVal) == NULL) {
71,164,288✔
1603
        code = terrno;
×
1604
        goto _exit;
×
1605
      }
1606
    }
1607
  }
1608

1609
  // build
1610
  SRowBuildScanInfo sinfo = {0};
6,448,545✔
1611
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
6,458,745✔
1612

1613
  if (code) goto _exit;
6,455,535✔
1614

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

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

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

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

1658
_error:
7,900✔
1659
  return code;
7,900✔
1660
}
1661

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

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

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

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

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

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

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

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

1710
  tBlobSetSwap(pBlob, pTempBlob);
790✔
1711

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

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

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

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

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

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

1755
    tRowGetKey(row1, &key1);
×
1756

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

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

1765
      iEnd++;
×
1766
    }
1767

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

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

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

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

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

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

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

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

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

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

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

1848
  // build
1849

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

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

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

1870
  return code;
×
1871
}
1872

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

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

1885
  int32_t iStart = 0;
1,685,547✔
1886
  while (iStart < aRowP->size) {
33,842,510✔
1887
    SRowKey key1;
32,139,487✔
1888
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
32,156,027✔
1889

1890
    tRowGetKey(row1, &key1);
64,246,714✔
1891

1892
    int32_t iEnd = iStart + 1;
32,143,761✔
1893
    while (iEnd < aRowP->size) {
66,241,953✔
1894
      SRowKey key2;
64,543,623✔
1895
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
64,531,774✔
1896
      tRowGetKey(row2, &key2);
129,016,572✔
1897

1898
      if (tRowKeyCompare(&key1, &key2) != 0) break;
64,586,738✔
1899

1900
      iEnd++;
34,117,932✔
1901
    }
1902

1903
    if (iEnd - iStart > 1) {
32,158,907✔
1904
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
6,458,019✔
1905
      if (code) return code;
6,457,875✔
1906
    }
1907

1908
    // the array is also changing, so the iStart just ++ instead of iEnd
1909
    iStart++;
32,158,763✔
1910
  }
1911

1912
  return code;
1,685,547✔
1913
}
1914

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

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

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

1934
    tRowGetKey(row1, &key1);
15,800✔
1935

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

1942
      if (tRowKeyCompare(&key1, &key2) != 0) break;
7,110✔
1943

1944
      iEnd++;
×
1945
    }
1946

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

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

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

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

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

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

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

1994
  int32_t code = 0;
49,462,069✔
1995

1996
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
49,462,069✔
1997
  if (pIter == NULL) {
49,468,689✔
1998
    code = terrno;
×
1999
    goto _exit;
×
2000
  }
2001

2002
  pIter->pRow = pRow;
49,468,689✔
2003
  pIter->pTSchema = pTSchema;
49,471,052✔
2004
  pIter->iTColumn = 0;
49,470,206✔
2005

2006
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
49,470,862✔
2007

2008
  uint8_t         *data = pRow->data;
48,888,204✔
2009
  SPrimaryKeyIndex index;
48,877,787✔
2010
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
54,701,756✔
2011
    data += tGetPrimaryKeyIndex(data, &index);
5,803,489✔
2012
  }
2013

2014
  if (pRow->flag >> 4) {
48,901,939✔
2015
    pIter->iCol = 0;
10,446,200✔
2016
    pIter->pIdx = (SKVIdx *)data;
10,446,630✔
2017
    if (pRow->flag & KV_FLG_LIT) {
10,442,616✔
2018
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
10,441,947✔
2019
    } else if (pRow->flag & KV_FLG_MID) {
×
2020
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 1);  // * sizeof(uint16_t)
×
2021
    } else {
2022
      pIter->pv = pIter->pIdx->idx + (pIter->pIdx->nCol << 2);  // * sizeof(uint32_t)
×
2023
    }
2024
  } else {
2025
    switch (pRow->flag) {
38,456,652✔
2026
      case (HAS_NULL | HAS_NONE):
2,721✔
2027
        pIter->pb = data;
2,721✔
2028
        break;
2,721✔
2029
      case HAS_VALUE:
35,692,384✔
2030
        pIter->pf = data;
35,692,384✔
2031
        pIter->pv = pIter->pf + pTSchema->flen;
35,694,827✔
2032
        break;
35,697,837✔
2033
      case (HAS_VALUE | HAS_NONE):
2,758,823✔
2034
      case (HAS_VALUE | HAS_NULL):
2035
        pIter->pb = data;
2,758,823✔
2036
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
2,758,823✔
2037
        pIter->pv = pIter->pf + pTSchema->flen;
2,758,823✔
2038
        break;
2,758,823✔
2039
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
1,109✔
2040
        pIter->pb = data;
1,109✔
2041
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2042
        pIter->pv = pIter->pf + pTSchema->flen;
×
2043
        break;
×
2044
      default:
×
2045
        break;
×
2046
    }
2047
  }
2048

2049
_exit:
49,480,356✔
2050
  if (code) {
49,475,700✔
2051
    *ppIter = NULL;
×
2052
  } else {
2053
    *ppIter = pIter;
49,475,700✔
2054
  }
2055
  return code;
49,477,089✔
2056
}
2057

2058
void tRowIterClose(SRowIter **ppIter) {
49,484,616✔
2059
  SRowIter *pIter = *ppIter;
49,484,616✔
2060
  if (pIter) {
49,487,785✔
2061
    taosMemoryFree(pIter);
49,489,746✔
2062
  }
2063
  *ppIter = NULL;
49,490,024✔
2064
}
49,492,315✔
2065

2066
SColVal *tRowIterNext(SRowIter *pIter) {
288,934,031✔
2067
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
288,934,031✔
2068
    return NULL;
7,533,616✔
2069
  }
2070

2071
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
283,125,369✔
2072

2073
  // timestamp
2074
  if (0 == pIter->iTColumn) {
284,062,247✔
2075
    pIter->cv.cid = pTColumn->colId;
18,688,575✔
2076
    pIter->cv.value.type = pTColumn->type;
18,687,848✔
2077
    pIter->cv.flag = CV_FLAG_VALUE;
18,686,577✔
2078
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
18,684,203✔
2079
    goto _exit;
18,685,681✔
2080
  }
2081

2082
  if (pIter->pRow->flag == HAS_NONE) {
265,501,328✔
2083
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
10,586,668✔
2084
    goto _exit;
10,568,605✔
2085
  }
2086

2087
  if (pIter->pRow->flag == HAS_NULL) {
254,907,286✔
2088
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
339,429✔
2089
    goto _exit;
339,429✔
2090
  }
2091

2092
  if (pIter->pRow->flag >> 4) {  // KV
254,404,166✔
2093
    if (pIter->iCol < pIter->pIdx->nCol) {
214,235,412✔
2094
      uint8_t *pData;
2095

2096
      if (pIter->pRow->flag & KV_FLG_LIT) {
89,297,477✔
2097
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
89,270,717✔
2098
      } else if (pIter->pRow->flag & KV_FLG_MID) {
×
2099
        pData = pIter->pv + ((uint16_t *)pIter->pIdx->idx)[pIter->iCol];
×
2100
      } else {
2101
        pData = pIter->pv + ((uint32_t *)pIter->pIdx->idx)[pIter->iCol];
×
2102
      }
2103

2104
      int16_t cid;
89,273,632✔
2105
      pData += tGetI16v(pData, &cid);
89,301,061✔
2106

2107
      if (TABS(cid) == pTColumn->colId) {
89,303,068✔
2108
        if (cid < 0) {
87,977,614✔
2109
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
3,236,712✔
2110
        } else {
2111
          pIter->cv.cid = pTColumn->colId;
84,740,902✔
2112
          pIter->cv.value.type = pTColumn->type;
84,739,564✔
2113
          pIter->cv.flag = CV_FLAG_VALUE;
84,724,846✔
2114

2115
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
84,714,142✔
2116
            pData += tGetU32v(pData, &pIter->cv.value.nData);
31,521,661✔
2117
            if (pIter->cv.value.nData > 0) {
15,803,312✔
2118
              pIter->cv.value.pData = pData;
15,802,646✔
2119
            } else {
2120
              pIter->cv.value.pData = NULL;
1,335✔
2121
              // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2122
              //   pIter->cv.value.pData = pData;
2123
              // }
2124
            }
2125
          } else {
2126
            valueSetDatum(&pIter->cv.value, pTColumn->type, pData, pTColumn->bytes);
68,993,308✔
2127
          }
2128
        }
2129

2130
        pIter->iCol++;
87,993,192✔
2131
        goto _exit;
87,868,758✔
2132
      } else if (TABS(cid) > pTColumn->colId) {
1,282,208✔
2133
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,282,208✔
2134
        goto _exit;
1,282,208✔
2135
      } else {
2136
        uError("unexpected column id %d, %d", cid, pTColumn->colId);
×
2137
        goto _exit;
×
2138
      }
2139
    } else {
2140
      pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
125,974,407✔
2141
      goto _exit;
126,017,892✔
2142
    }
2143
  } else {  // Tuple
2144
    uint8_t bv = BIT_FLG_VALUE;
39,990,524✔
2145
    if (pIter->pb) {
39,990,524✔
2146
      switch (pIter->pRow->flag) {
14,614,111✔
2147
        case (HAS_NULL | HAS_NONE):
24,701✔
2148
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
24,701✔
2149
          break;
24,701✔
2150
        case (HAS_VALUE | HAS_NONE):
8,904,641✔
2151
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
8,904,641✔
2152
          if (bv) bv++;
8,904,641✔
2153
          break;
8,904,641✔
2154
        case (HAS_VALUE | HAS_NULL):
5,684,769✔
2155
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
5,684,769✔
2156
          break;
5,684,769✔
2157
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2158
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
2159
          break;
×
2160
        default:
×
2161
          break;
×
2162
      }
2163

2164
      if (bv == BIT_FLG_NONE) {
14,614,111✔
2165
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
915,321✔
2166
        goto _exit;
915,321✔
2167
      } else if (bv == BIT_FLG_NULL) {
13,698,790✔
2168
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,000,618✔
2169
        goto _exit;
1,000,618✔
2170
      }
2171
    }
2172

2173
    pIter->cv.cid = pTColumn->colId;
38,076,789✔
2174
    pIter->cv.value.type = pTColumn->type;
38,078,263✔
2175
    pIter->cv.flag = CV_FLAG_VALUE;
38,077,281✔
2176
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
44,811,619✔
2177
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
6,732,569✔
2178
      pData += tGetU32v(pData, &pIter->cv.value.nData);
13,468,831✔
2179
      if (pIter->cv.value.nData > 0) {
6,734,224✔
2180
        pIter->cv.value.pData = pData;
6,689,555✔
2181
      } else {
2182
        pIter->cv.value.pData = NULL;
43,868✔
2183
        // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2184
        //   pIter->cv.value.pData = pData;
2185
        // }
2186
      }
2187
    } else {
2188
      valueSetDatum(&pIter->cv.value, pTColumn->type, pIter->pf + pTColumn->offset, TYPE_BYTES[pTColumn->type]);
31,346,275✔
2189
    }
2190
    goto _exit;
38,065,062✔
2191
  }
2192

2193
_exit:
284,730,863✔
2194
  pIter->iTColumn++;
284,730,863✔
2195
  return &pIter->cv;
283,475,923✔
2196
}
2197

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

2201
  if (flag) return code;
751,748✔
2202

2203
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
6,000,761✔
2204
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
5,511,165✔
2205
    if (code) return code;
5,511,165✔
2206
  }
2207

2208
  return code;
489,596✔
2209
}
2210
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
56,146,449✔
2211
  int32_t code = 0;
56,146,449✔
2212

2213
  int32_t   iColData = 0;
56,146,449✔
2214
  SColData *pColData = &aColData[iColData];
56,146,449✔
2215
  int32_t   iTColumn = 1;
56,152,051✔
2216
  STColumn *pTColumn = &pSchema->columns[iTColumn];
56,152,051✔
2217

2218
  while (pColData) {
372,919,367✔
2219
    if (pTColumn) {
316,785,297✔
2220
      if (pTColumn->colId == pColData->cid) {  // NULL
316,785,297✔
2221
        if (flag == 0) {
316,810,666✔
2222
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
316,810,666✔
2223
        } else {
2224
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2225
        }
2226
        if (code) goto _exit;
316,821,097✔
2227

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

2242
_exit:
56,134,070✔
2243
  return code;
56,134,070✔
2244
}
2245
static int32_t tRowTupleUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData,
2,147,483,647✔
2246
                                      int32_t flag) {
2247
  int32_t code = 0;
2,147,483,647✔
2248

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

2254
  uint8_t         *pb = NULL, *pf = NULL, *pv = NULL;
2,147,483,647✔
2255
  SPrimaryKeyIndex index;
2,147,483,647✔
2256
  uint8_t         *data = pRow->data;
2,147,483,647✔
2257
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
2,147,483,647✔
2258
    data += tGetPrimaryKeyIndex(data, &index);
60,748,933✔
2259
  }
2260

2261
  switch (pRow->flag) {
2,147,483,647✔
2262
    case HAS_VALUE:
2,147,483,647✔
2263
      pf = data;  // TODO: fix here
2,147,483,647✔
2264
      pv = pf + pTSchema->flen;
2,147,483,647✔
2265
      break;
2,147,483,647✔
2266
    case (HAS_NULL | HAS_NONE):
176,456✔
2267
      pb = data;
176,456✔
2268
      break;
176,456✔
2269
    case (HAS_VALUE | HAS_NONE):
405,037,274✔
2270
    case (HAS_VALUE | HAS_NULL):
2271
      pb = data;
405,037,274✔
2272
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
405,037,274✔
2273
      pv = pf + pTSchema->flen;
405,029,634✔
2274
      break;
405,027,351✔
2275
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
12,185✔
2276
      pb = data;
12,185✔
2277
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
12,185✔
2278
      pv = pf + pTSchema->flen;
×
2279
      break;
×
2280
    default:
×
2281
      return TSDB_CODE_INVALID_DATA_FMT;
×
2282
  }
2283

2284
  while (pColData) {
2,147,483,647✔
2285
    if (pTColumn) {
2,147,483,647✔
2286
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2287
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647✔
2288
          return TSDB_CODE_INVALID_PARA;
×
2289
        }
2290
        if (pb) {
2,147,483,647✔
2291
          uint8_t bv;
2292
          switch (pRow->flag) {
1,720,958,694✔
2293
            case (HAS_NULL | HAS_NONE):
3,485,992✔
2294
              bv = GET_BIT1(pb, iTColumn - 1);
3,485,992✔
2295
              break;
3,485,992✔
2296
            case (HAS_VALUE | HAS_NONE):
60,568,803✔
2297
              bv = GET_BIT1(pb, iTColumn - 1);
60,568,803✔
2298
              if (bv) bv++;
60,660,170✔
2299
              break;
60,660,170✔
2300
            case (HAS_VALUE | HAS_NULL):
1,656,999,656✔
2301
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
1,656,999,656✔
2302
              break;
1,657,075,367✔
2303
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2304
              bv = GET_BIT2(pb, iTColumn - 1);
×
2305
              break;
×
2306
            default:
×
2307
              return TSDB_CODE_INVALID_DATA_FMT;
×
2308
          }
2309

2310
          if (bv == BIT_FLG_NONE) {
1,721,221,529✔
2311
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
7,712,238✔
2312
              goto _exit;
×
2313
            goto _continue;
7,708,395✔
2314
          } else if (bv == BIT_FLG_NULL) {
1,713,509,291✔
2315
            if (flag == 0) {
488,233,633✔
2316
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
487,886,848✔
2317
            } else {
2318
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
346,785✔
2319
            }
2320
            if (code) goto _exit;
488,225,904✔
2321
            goto _continue;
488,225,904✔
2322
          }
2323
        }
2324

2325
        if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
2326
          uint8_t *pData = pv + *(int32_t *)(pf + pTColumn->offset);
2,147,483,647✔
2327
          uint32_t nData;
2,147,483,647✔
2328
          pData += tGetU32v(pData, &nData);
2,147,483,647✔
2329
          if (flag == 0) {
2,147,483,647✔
2330
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2331
          } else {
2332
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
480,492,696✔
2333
          }
2334
          if (code) goto _exit;
2,147,483,647✔
2335
        } else {
2336
          if (flag == 0) {
2,147,483,647✔
2337
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
2338
                                                                          TYPE_BYTES[pColData->type]);
2,147,483,647✔
2339
          } else {
2340
            code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pf + pTColumn->offset,
2,147,483,647✔
2341
                                                                          TYPE_BYTES[pColData->type], flag > 0);
1,270,549,885✔
2342
          }
2343
          if (code) goto _exit;
2,147,483,647✔
2344
        }
2345

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

2361
_exit:
2,147,483,647✔
2362
  return code;
2,147,483,647✔
2363
}
2364
static int32_t tRowKVUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
1,751,699,360✔
2365
  int32_t code = 0;
1,751,699,360✔
2366

2367
  uint8_t  *pv = NULL;
1,751,699,360✔
2368
  int32_t   iColData = 0;
1,751,699,360✔
2369
  SColData *pColData = &aColData[iColData];
1,751,699,360✔
2370
  int32_t   iTColumn = 1;
1,751,965,789✔
2371
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,751,965,789✔
2372
  int32_t   iCol = 0;
1,752,042,921✔
2373

2374
  // primary keys
2375
  uint8_t         *data = pRow->data;
1,752,042,921✔
2376
  SPrimaryKeyIndex index;
1,751,977,116✔
2377
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,757,508,895✔
2378
    data += tGetPrimaryKeyIndex(data, &index);
5,532,874✔
2379
  }
2380

2381
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,751,957,121✔
2382
  if (pRow->flag & KV_FLG_LIT) {
1,751,957,121✔
2383
    pv = pKVIdx->idx + pKVIdx->nCol;
1,740,940,391✔
2384
  } else if (pRow->flag & KV_FLG_MID) {
10,921,278✔
2385
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
10,938,134✔
2386
  } else if (pRow->flag & KV_FLG_BIG) {
1✔
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
2388
  } else {
2389
    return TSDB_CODE_INVALID_PARA;
×
2390
  }
2391

2392
  while (pColData) {
2,147,483,647✔
2393
    if (pTColumn) {
2,147,483,647✔
2394
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2395
        while (iCol < pKVIdx->nCol) {
2,147,483,647✔
2396
          uint8_t *pData;
2397
          if (pRow->flag & KV_FLG_LIT) {
2,147,483,647✔
2398
            pData = pv + ((uint8_t *)pKVIdx->idx)[iCol];
2,147,483,647✔
2399
          } else if (pRow->flag & KV_FLG_MID) {
296,003,176✔
2400
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
296,014,473✔
2401
          } else if (pRow->flag & KV_FLG_BIG) {
×
2402
            pData = pv + ((uint32_t *)pKVIdx->idx)[iCol];
×
2403
          } else {
2404
            return TSDB_CODE_INVALID_DATA_FMT;
×
2405
          }
2406

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

2410
          if (TABS(cid) == pTColumn->colId) {
2,147,483,647✔
2411
            if (cid < 0) {
2,147,483,647✔
2412
              if (flag == 0) {
2,147,483,647✔
2413
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
2,147,483,647✔
2414
              } else {
2415
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
26,673✔
2416
              }
2417
              if (code) goto _exit;
2,147,483,647✔
2418
            } else {
2419
              uint32_t nData;
2,147,483,647✔
2420
              if (IS_VAR_DATA_TYPE(pTColumn->type)) {
2,147,483,647✔
2421
                pData += tGetU32v(pData, &nData);
941,425,109✔
2422
              } else {
2423
                nData = 0;
2,147,483,647✔
2424
              }
2425
              if (flag == 0) {
2,147,483,647✔
2426
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData);
2,147,483,647✔
2427
              } else {
2428
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, pData, nData, flag > 0);
7,890,803✔
2429
              }
2430
              if (code) goto _exit;
2,147,483,647✔
2431
            }
2432
            iCol++;
2,147,483,647✔
2433
            goto _continue;
2,147,483,647✔
2434
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
2,147,483,647✔
2435
            break;
2,147,483,647✔
2436
          } else {
2437
            iCol++;
4,458,000✔
2438
          }
2439
        }
2440

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

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

2458
_exit:
1,753,026,364✔
2459
  return code;
1,751,898,706✔
2460
}
2461
/* flag > 0: forward update
2462
 * flag == 0: append
2463
 * flag < 0: backward update
2464
 */
2465
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag) {
2,147,483,647✔
2466
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
2467
  if (!(nColData > 0)) return TSDB_CODE_INVALID_PARA;
2,147,483,647✔
2468

2469
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
2470
    return tRowNoneUpsertColData(aColData, nColData, flag);
751,748✔
2471
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2472
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
56,149,226✔
2473
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2474
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,751,848,751✔
2475
  } else {  // TUPLE row
2476
    return tRowTupleUpsertColData(pRow, pTSchema, aColData, nColData, flag);
2,147,483,647✔
2477
  }
2478
}
2479

2480
void tRowGetPrimaryKey(SRow *row, SRowKey *key) {
1,929,981,006✔
2481
  key->numOfPKs = row->numOfPKs;
1,929,981,006✔
2482

2483
  if (key->numOfPKs == 0) {
1,930,728,888✔
2484
    return;
×
2485
  }
2486

2487
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
1,925,251,590✔
2488

2489
  uint8_t *data = row->data;
1,931,570,970✔
2490

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

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

2499
    uint8_t *tdata = data + indices[i].offset;
1,930,990,885✔
2500
    if (row->flag >> 4) {
1,931,408,893✔
2501
      tdata += tGetI16v(tdata, NULL);
1,356,728,332✔
2502
    }
2503

2504
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
1,931,931,880✔
2505
      key->pks[i].pData = tdata;
474,149,448✔
2506
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
950,054,576✔
2507
    } else {
2508
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
1,458,150,060✔
2509
    }
2510
  }
2511
}
2512

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

2524
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
1,217,263,546✔
2525
  switch (tv1->type) {
1,217,263,546✔
2526
    case TSDB_DATA_TYPE_BOOL:
×
2527
    case TSDB_DATA_TYPE_TINYINT:
2528
      T_COMPARE_SCALAR_VALUE(int8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2529
    case TSDB_DATA_TYPE_SMALLINT:
×
2530
      T_COMPARE_SCALAR_VALUE(int16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2531
    case TSDB_DATA_TYPE_INT:
510,583,450✔
2532
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
510,583,450✔
2533
    case TSDB_DATA_TYPE_BIGINT:
150,170,407✔
2534
    case TSDB_DATA_TYPE_TIMESTAMP:
2535
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
150,170,407✔
2536
    case TSDB_DATA_TYPE_FLOAT:
×
2537
      T_COMPARE_SCALAR_VALUE(float, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2538
    case TSDB_DATA_TYPE_DOUBLE:
×
2539
      T_COMPARE_SCALAR_VALUE(double, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2540
    case TSDB_DATA_TYPE_UTINYINT:
×
2541
      T_COMPARE_SCALAR_VALUE(uint8_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2542
    case TSDB_DATA_TYPE_USMALLINT:
×
2543
      T_COMPARE_SCALAR_VALUE(uint16_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
×
2544
    case TSDB_DATA_TYPE_UINT:
149,906,063✔
2545
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
149,906,063✔
2546
    case TSDB_DATA_TYPE_UBIGINT:
149,946,702✔
2547
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
149,946,702✔
2548
    case TSDB_DATA_TYPE_GEOMETRY:
256,627,944✔
2549
    case TSDB_DATA_TYPE_BINARY: {
2550
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
256,627,944✔
2551
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
256,686,153✔
2552
    }
2553
    case TSDB_DATA_TYPE_NCHAR: {
×
2554
      int32_t ret = taosUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
2555
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2556
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2557
    }
2558
    case TSDB_DATA_TYPE_VARBINARY: {
×
2559
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2560
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2561
    }
2562
    default:
×
2563
      break;
×
2564
  }
2565

2566
  return 0;
×
2567
}
2568

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

2579
  if (key1->numOfPKs == key2->numOfPKs) {
2,147,483,647✔
2580
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
2,147,483,647✔
2581
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
1,217,251,766✔
2582
      if (ret) return ret;
1,217,117,166✔
2583
    }
2584
  } else if (key1->numOfPKs < key2->numOfPKs) {
5,505✔
2585
    return -1;
5,505✔
2586
  } else {
2587
    return 1;
×
2588
  }
2589

2590
  return 0;
2,147,483,647✔
2591
}
2592

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

2597
  if (pSrc->numOfPKs > 0) {
2,147,483,647✔
2598
    for (int32_t i = 0; i < pSrc->numOfPKs; ++i) {
1,398,672,608✔
2599
      SValue *pVal = &pDst->pks[i];
699,342,894✔
2600
      pVal->type = pSrc->pks[i].type;
699,342,235✔
2601

2602
      valueCloneDatum(pVal, pSrc->pks + i, pVal->type);
699,338,281✔
2603
    }
2604
  }
2605
}
2,147,483,647✔
2606

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

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

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

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

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

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

2717
  // key
2718
  if (isJson) {
356,214,232✔
2719
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
2,454,376✔
2720
  } else {
2721
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
709,963,939✔
2722
  }
2723

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

2727
  // value
2728
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
356,220,390✔
2729
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
304,550,265✔
2730
  } else {
2731
    p = p ? p + n : p;
203,968,545✔
2732
    n += tDataTypes[pTagVal->type].bytes;
203,967,594✔
2733
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
203,956,874✔
2734
  }
2735

2736
  return n;
356,204,916✔
2737
}
2738
static int32_t tGetTagVal(uint8_t *p, STagVal *pTagVal, int8_t isJson) {
2,147,483,647✔
2739
  int32_t n = 0;
2,147,483,647✔
2740

2741
  // key
2742
  if (isJson) {
2,147,483,647✔
2743
    n += tGetCStr(p + n, &pTagVal->pKey);
9,601,300✔
2744
  } else {
2745
    n += tGetI16v(p + n, &pTagVal->cid);
2,147,483,647✔
2746
  }
2747

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

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

2759
  return n;
2,147,483,647✔
2760
}
2761

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

2764
bool tTagIsJsonNull(void *data) {
1,302,776✔
2765
  STag  *pTag = (STag *)data;
1,302,776✔
2766
  int8_t isJson = tTagIsJson(pTag);
1,302,776✔
2767
  if (!isJson) return false;
1,301,910✔
2768
  return ((STag *)data)->nTag == 0;
730,110✔
2769
}
2770

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

2779
  // sort
2780
  if (isJson) {
74,414,386✔
2781
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
397,746✔
2782
  } else {
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
74,016,640✔
2784
  }
2785

2786
  // get size
2787
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
252,446,089✔
2788
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
178,110,990✔
2789
  }
2790
  if (szTag <= INT8_MAX) {
74,335,099✔
2791
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
66,272,102✔
2792
  } else {
2793
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
8,063,092✔
2794
    isLarge = 1;
8,063,092✔
2795
  }
2796

2797
  // build tag
2798
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
74,335,099✔
2799
  if ((*ppTag) == NULL) {
74,399,876✔
2800
    code = terrno;
×
2801
    goto _err;
×
2802
  }
2803
  (*ppTag)->flags = 0;
74,401,913✔
2804
  if (isJson) {
74,405,123✔
2805
    (*ppTag)->flags |= TD_TAG_JSON;
397,746✔
2806
  }
2807
  if (isLarge) {
74,405,123✔
2808
    (*ppTag)->flags |= TD_TAG_LARGE;
8,084,712✔
2809
  }
2810
  (*ppTag)->len = szTag;
74,405,123✔
2811
  (*ppTag)->nTag = nTag;
74,411,055✔
2812
  (*ppTag)->ver = version;
74,380,786✔
2813

2814
  if (isLarge) {
74,331,318✔
2815
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
8,084,712✔
2816
  } else {
2817
    p = (uint8_t *)&(*ppTag)->idx[nTag];
66,246,606✔
2818
  }
2819
  n = 0;
74,386,881✔
2820
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
252,460,196✔
2821
    if (isLarge) {
178,040,780✔
2822
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
47,394,043✔
2823
    } else {
2824
      (*ppTag)->idx[iTag] = n;
130,646,737✔
2825
    }
2826
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
178,089,935✔
2827
  }
2828
#ifdef TD_DEBUG_PRINT_TAG
2829
  debugPrintSTag(*ppTag, __func__, __LINE__);
2830
#endif
2831

2832
  return code;
74,419,416✔
2833

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

2838
void tTagFree(STag *pTag) {
1,117,280,575✔
2839
  if (pTag) taosMemoryFree(pTag);
1,117,280,575✔
2840
}
1,117,280,575✔
2841

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

2847
  char  *data = NULL;
888,339,831✔
2848
  int8_t typeBytes = 0;
888,339,831✔
2849
  if (isJson) {
888,339,831✔
2850
    typeBytes = CHAR_BYTES;
2,178,305✔
2851
  }
2852

2853
  if (IS_VAR_DATA_TYPE(value->type)) {
888,339,831✔
2854
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
311,881,813✔
2855
    if (data == NULL) {
312,049,329✔
2856
      return NULL;
×
2857
    }
2858

2859
    if (isJson) {
312,049,329✔
2860
      *data = value->type;
1,647,070✔
2861
    }
2862

2863
    varDataLen(data + typeBytes) = value->nData;
312,049,329✔
2864
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
312,025,563✔
2865
  } else {
2866
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
576,425,484✔
2867
  }
2868

2869
  return data;
888,573,865✔
2870
}
2871

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

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

2887
  if (isLarge) {
2,147,483,647✔
2888
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
703,592,919✔
2889
  } else {
2890
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,655,823,808✔
2891
  }
2892

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

2904
    int32_t nt = tGetTagVal(p + offset, &tv, isJson);
2,147,483,647✔
2905
    if (isJson) {
2,147,483,647✔
2906
      c = tTagValJsonCmprFn(pTagVal, &tv);
3,765,231✔
2907
    } else {
2908
      c = tTagValCmprFn(pTagVal, &tv);
2,147,483,647✔
2909
    }
2910

2911
    if (c < 0) {
2,147,483,647✔
2912
      ridx = midx - 1;
2,067,519,508✔
2913
    } else if (c > 0) {
2,147,483,647✔
2914
      lidx = midx + 1;
2,147,483,647✔
2915
    } else {
2916
      (void)memcpy(pTagVal, &tv, sizeof(tv));
2,147,483,647✔
2917
      return true;
2,147,483,647✔
2918
    }
2919
  }
2920
  return false;
56,309,937✔
2921
}
2922

2923
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
274,243,936✔
2924
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
548,570,537✔
2925
}
2926

2927
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
1,896,674,283✔
2928

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

2936
  if (isLarge) {
782,166✔
2937
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
3,974✔
2938
  } else {
2939
    p = (uint8_t *)&pTag->idx[pTag->nTag];
778,192✔
2940
  }
2941

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

2948
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
1,905,037✔
2949
    if (isLarge) {
1,122,871✔
2950
      offset = ((int16_t *)pTag->idx)[iTag];
5,309✔
2951
    } else {
2952
      offset = pTag->idx[iTag];
1,117,562✔
2953
    }
2954
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,122,871✔
2955
    if (taosArrayPush(*ppArray, &tv) == NULL) {
2,245,742✔
2956
      code = terrno;
×
2957
      goto _err;
×
2958
    }
2959
  }
2960

2961
  return code;
782,166✔
2962

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

2967

2968
void destroyTagVal(void *pTag) {
62,801,364✔
2969
  STagVal* pTagVal = (STagVal*)pTag;
62,801,364✔
2970
  if (pTagVal && IS_VAR_DATA_TYPE(pTagVal->type)) {
62,801,364✔
2971
    taosMemoryFree(pTagVal->pData);
27,649,566✔
2972
  }
2973
}
62,801,364✔
2974

2975
// STSchema ========================================
2976
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
906,242,937✔
2977
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
906,242,937✔
2978
  if (pTSchema == NULL) {
905,692,494✔
2979
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2980
    return NULL;
×
2981
  }
2982

2983
  pTSchema->numOfCols = numOfCols;
905,692,494✔
2984
  pTSchema->version = version;
905,625,523✔
2985

2986
  // timestamp column
2987
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
905,750,556✔
2988
    terrno = TSDB_CODE_INVALID_PARA;
×
2989
    taosMemoryFree(pTSchema);
×
2990
    return NULL;
×
2991
  }
2992
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
905,588,620✔
2993
    terrno = TSDB_CODE_INVALID_PARA;
×
2994
    taosMemoryFree(pTSchema);
×
2995
    return NULL;
×
2996
  }
2997
  pTSchema->columns[0].colId = aSchema[0].colId;
905,583,508✔
2998
  pTSchema->columns[0].type = aSchema[0].type;
906,419,960✔
2999
  pTSchema->columns[0].flags = aSchema[0].flags;
906,066,425✔
3000
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
906,044,878✔
3001
  pTSchema->columns[0].offset = -1;
906,387,518✔
3002

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

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

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

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

3024
#if 1  // todo : remove this
3025
  pTSchema->tlen += (int32_t)TD_BITMAP_BYTES(numOfCols);
906,936,649✔
3026
#endif
3027

3028
  return pTSchema;
906,621,226✔
3029
}
3030

3031
static int32_t tTColumnCompare(const void *p1, const void *p2) {
92,731,275✔
3032
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
92,731,275✔
3033
    return -1;
15,403,655✔
3034
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
77,329,387✔
3035
    return 1;
54,791,266✔
3036
  }
3037

3038
  return 0;
22,539,888✔
3039
}
3040

3041
const STColumn *tTSchemaSearchColumn(const STSchema *pTSchema, int16_t cid) {
22,539,888✔
3042
  STColumn tcol = {
22,539,888✔
3043
      .colId = cid,
3044
  };
3045

3046
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
22,539,888✔
3047
}
3048

3049
// SColData ========================================
3050
void tColDataDestroy(void *ph) {
1,499,141,361✔
3051
  if (ph) {
1,499,141,361✔
3052
    SColData *pColData = (SColData *)ph;
1,499,320,976✔
3053

3054
    tFree(pColData->pBitMap);
1,499,320,976✔
3055
    tFree(pColData->aOffset);
1,499,549,255✔
3056
    tFree(pColData->pData);
1,499,535,095✔
3057
  }
3058
}
1,499,267,389✔
3059

3060
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
1,573,301,672✔
3061
  pColData->cid = cid;
1,573,301,672✔
3062
  pColData->type = type;
1,573,511,421✔
3063
  pColData->cflag = cflag;
1,573,562,203✔
3064
  tColDataClear(pColData);
1,573,682,801✔
3065
}
1,573,378,704✔
3066

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

3076
void tColDataDeepClear(SColData *pColData) {
41,898,677✔
3077
  pColData->pBitMap = NULL;
41,898,677✔
3078
  pColData->aOffset = NULL;
41,907,419✔
3079
  pColData->pData = NULL;
41,907,752✔
3080

3081
  tColDataClear(pColData);
41,911,104✔
3082
}
41,912,809✔
3083

3084
static FORCE_INLINE int32_t tColDataPutValue(SColData *pColData, uint8_t *pData, uint32_t nData) {
3085
  int32_t  code = 0;
2,147,483,647✔
3086
  uint32_t cvtNData = sizeof(uint64_t);
2,147,483,647✔
3087
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3088
    // TODO
3089
    if (IS_STR_DATA_BLOB(pColData->type)) {
2,147,483,647✔
3090
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
26,737,481✔
3091
      if (code) goto _exit;
29,713,027✔
3092
      pColData->aOffset[pColData->nVal] = pColData->nData;
29,713,027✔
3093

3094
      if (nData) {
29,713,027✔
3095
        code = tRealloc(&pColData->pData, pColData->nData + BSE_SEQUECE_SIZE);
26,774,312✔
3096
        if (code) goto _exit;
26,774,312✔
3097
        (void)memcpy(pColData->pData + pColData->nData, pData, BSE_SEQUECE_SIZE);
26,774,312✔
3098
        pColData->nData += BSE_SEQUECE_SIZE;
26,774,312✔
3099
      } else {
3100
        // uint64_t zero = 0;
3101
        // (void)memcpy(pColData->pData + pColData->nData, &zero, BSE_SEQUECE_SIZE);
3102
        // pColData->nData += BSE_SEQUECE_SIZE;
3103
      }
3104

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

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

3132
_exit:
2,147,483,647✔
3133
  return code;
2,147,483,647✔
3134
}
3135
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
626,318,529✔
3136
  pColData->flag = HAS_VALUE;
626,406,305✔
3137
  pColData->numOfValue++;
626,511,747✔
3138
  return tColDataPutValue(pColData, pData, nData);
626,500,308✔
3139
}
3140
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
15,500,511✔
3141
  pColData->flag = HAS_NONE;
15,500,511✔
3142
  pColData->numOfNone++;
15,500,511✔
3143
  pColData->nVal++;
15,500,511✔
3144
  return 0;
15,500,367✔
3145
}
3146
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
30,782,310✔
3147
  pColData->flag = HAS_NULL;
30,805,157✔
3148
  pColData->numOfNull++;
30,835,133✔
3149
  pColData->nVal++;
30,836,572✔
3150
  return 0;
30,838,461✔
3151
}
3152
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
926,613✔
3153
  int32_t code = 0;
926,613✔
3154

3155
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
926,613✔
3156
  code = tRealloc(&pColData->pBitMap, nBit);
926,613✔
3157
  if (code) return code;
926,613✔
3158

3159
  memset(pColData->pBitMap, 0, nBit);
926,613✔
3160
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
926,613✔
3161

3162
  pColData->flag |= HAS_VALUE;
926,613✔
3163
  pColData->numOfValue++;
926,613✔
3164

3165
  if (pColData->nVal) {
926,613✔
3166
    if (IS_VAR_DATA_TYPE(pColData->type)) {
926,613✔
3167
      if (IS_STR_DATA_BLOB(pColData->type)) {
33,640✔
3168
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3169
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3170
        if (code) return code;
×
3171
        memset(pColData->aOffset, 0, nOffset);
×
3172

3173
      } else {
3174
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
33,640✔
3175
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
33,640✔
3176
        if (code) return code;
33,640✔
3177
        memset(pColData->aOffset, 0, nOffset);
33,640✔
3178
      }
3179
    } else {
3180
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
892,973✔
3181
      code = tRealloc(&pColData->pData, pColData->nData);
892,973✔
3182
      if (code) return code;
892,973✔
3183
      memset(pColData->pData, 0, pColData->nData);
892,973✔
3184
    }
3185
  }
3186

3187
  return tColDataPutValue(pColData, pData, nData);
926,613✔
3188
}
3189
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3190
  pColData->nVal++;
2,147,483,647✔
3191
  pColData->numOfNone++;
2,147,483,647✔
3192
  return 0;
2,147,483,647✔
3193
}
3194
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
64,016✔
3195
  int32_t code = 0;
85,220✔
3196

3197
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
85,220✔
3198
  code = tRealloc(&pColData->pBitMap, nBit);
85,220✔
3199
  if (code) return code;
85,220✔
3200

3201
  memset(pColData->pBitMap, 0, nBit);
85,220✔
3202
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
85,220✔
3203

3204
  pColData->flag |= HAS_NULL;
85,220✔
3205
  pColData->numOfNull++;
85,220✔
3206
  pColData->nVal++;
85,220✔
3207

3208
  return code;
85,220✔
3209
}
3210
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,505,239✔
3211
  int32_t code = 0;
4,505,640✔
3212

3213
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
4,505,640✔
3214
  code = tRealloc(&pColData->pBitMap, nBit);
4,505,692✔
3215
  if (code) return code;
4,505,205✔
3216

3217
  memset(pColData->pBitMap, 0, nBit);
4,505,205✔
3218
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
4,505,692✔
3219

3220
  pColData->flag |= HAS_VALUE;
4,505,692✔
3221
  pColData->numOfValue++;
4,505,692✔
3222

3223
  if (pColData->nVal) {
4,505,692✔
3224
    if (IS_VAR_DATA_TYPE(pColData->type)) {
5,840,429✔
3225
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,334,763✔
3226
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,334,737✔
3227
      if (code) return code;
1,334,737✔
3228
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,334,737✔
3229
        memset(pColData->aOffset, 0, nOffset);
1,329,034✔
3230
      }
3231
    } else {
3232
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
3,170,929✔
3233
      code = tRealloc(&pColData->pData, pColData->nData);
3,170,712✔
3234
      if (code) return code;
3,170,929✔
3235
      memset(pColData->pData, 0, pColData->nData);
3,170,929✔
3236
    }
3237
  }
3238

3239
  return tColDataPutValue(pColData, pData, nData);
4,505,692✔
3240
}
3241
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
17,748✔
3242
  int32_t code = 0;
17,748✔
3243

3244
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
17,748✔
3245
  code = tRealloc(&pColData->pBitMap, nBit);
17,748✔
3246
  if (code) return code;
17,748✔
3247

3248
  memset(pColData->pBitMap, 255, nBit);
17,748✔
3249
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
17,748✔
3250

3251
  pColData->flag |= HAS_NONE;
17,748✔
3252
  pColData->numOfNone++;
17,748✔
3253
  pColData->nVal++;
17,748✔
3254

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

3265
  pColData->flag |= HAS_VALUE;
×
3266
  pColData->numOfValue++;
×
3267

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

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

3277
  tFree(pColData->pBitMap);
×
3278
  pColData->pBitMap = pBitMap;
×
3279

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

3294
  return tColDataPutValue(pColData, pData, nData);
×
3295
}
3296
static FORCE_INLINE int32_t tColDataAppendValue31(SColData *pColData, uint8_t *pData, uint32_t nData) {
739,784✔
3297
  int32_t code = 0;
739,784✔
3298

3299
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
739,784✔
3300
  if (code) return code;
739,784✔
3301

3302
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
739,784✔
3303
  pColData->numOfNone++;
739,784✔
3304
  pColData->nVal++;
739,784✔
3305

3306
  return code;
739,784✔
3307
}
3308
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
136,648✔
3309
  int32_t code = 0;
136,648✔
3310

3311
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
136,648✔
3312
  if (code) return code;
136,648✔
3313

3314
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
136,648✔
3315
  pColData->numOfNull++;
136,648✔
3316
  pColData->nVal++;
136,648✔
3317

3318
  return code;
136,648✔
3319
}
3320
static FORCE_INLINE int32_t tColDataAppendValue40(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3321
  pColData->numOfValue++;
2,147,483,647✔
3322
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3323
}
3324
static FORCE_INLINE int32_t tColDataAppendValue41(SColData *pColData, uint8_t *pData, uint32_t nData) {
6,345,231✔
3325
  int32_t code = 0;
6,345,231✔
3326

3327
  pColData->flag |= HAS_NONE;
6,345,231✔
3328
  pColData->numOfNone++;
6,345,820✔
3329

3330
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
6,345,231✔
3331
  code = tRealloc(&pColData->pBitMap, nBit);
6,345,231✔
3332
  if (code) return code;
6,344,642✔
3333

3334
  memset(pColData->pBitMap, 255, nBit);
6,344,642✔
3335
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
6,345,231✔
3336

3337
  return tColDataPutValue(pColData, NULL, 0);
6,345,820✔
3338
}
3339
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
20,738,175✔
3340
  int32_t code = 0;
21,325,597✔
3341

3342
  pColData->flag |= HAS_NULL;
21,325,597✔
3343
  pColData->numOfNull++;
21,329,221✔
3344

3345
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
21,328,733✔
3346
  code = tRealloc(&pColData->pBitMap, nBit);
21,328,646✔
3347
  if (code) return code;
21,325,825✔
3348

3349
  memset(pColData->pBitMap, 255, nBit);
21,325,825✔
3350
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
21,325,236✔
3351

3352
  return tColDataPutValue(pColData, NULL, 0);
21,326,498✔
3353
}
3354
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
313,470,621✔
3355
  int32_t code = 0;
313,471,218✔
3356

3357
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
313,471,218✔
3358
  if (code) return code;
313,450,990✔
3359

3360
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
313,450,990✔
3361
  pColData->numOfValue++;
313,463,970✔
3362

3363
  return tColDataPutValue(pColData, pData, nData);
313,445,709✔
3364
}
3365
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
826,026,944✔
3366
  int32_t code = 0;
826,026,944✔
3367

3368
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
826,026,944✔
3369
  if (code) return code;
826,023,599✔
3370

3371
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
826,023,599✔
3372
  pColData->numOfNone++;
826,015,491✔
3373

3374
  return tColDataPutValue(pColData, NULL, 0);
826,044,098✔
3375
}
3376
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
94,800✔
3377
  int32_t code = 0;
94,800✔
3378

3379
  pColData->flag |= HAS_NULL;
94,800✔
3380
  pColData->numOfNull++;
94,800✔
3381

3382
  uint8_t *pBitMap = NULL;
94,800✔
3383
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
94,800✔
3384
  if (code) return code;
94,800✔
3385

3386
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,325,006✔
3387
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
2,230,206✔
3388
  }
3389
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
94,800✔
3390

3391
  tFree(pColData->pBitMap);
94,800✔
3392
  pColData->pBitMap = pBitMap;
94,800✔
3393

3394
  return tColDataPutValue(pColData, NULL, 0);
94,800✔
3395
}
3396
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3397
  int32_t code = 0;
2,147,483,647✔
3398

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

3404
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3405
}
3406
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,012,437✔
3407
  int32_t code = 0;
2,012,437✔
3408

3409
  pColData->flag |= HAS_NONE;
2,012,437✔
3410
  pColData->numOfNone++;
2,012,437✔
3411

3412
  uint8_t *pBitMap = NULL;
2,012,437✔
3413
  code = tRealloc(&pBitMap, BIT2_SIZE(pColData->nVal + 1));
2,012,437✔
3414
  if (code) return code;
2,012,437✔
3415

3416
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
174,160,651✔
3417
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
172,148,214✔
3418
  }
3419
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
2,012,437✔
3420

3421
  tFree(pColData->pBitMap);
2,012,437✔
3422
  pColData->pBitMap = pBitMap;
2,012,437✔
3423

3424
  return tColDataPutValue(pColData, NULL, 0);
2,012,437✔
3425
}
3426
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,240,186,475✔
3427
  int32_t code = 0;
1,241,424,397✔
3428

3429
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,241,424,397✔
3430
  if (code) return code;
1,241,633,448✔
3431
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,241,633,448✔
3432
  pColData->numOfNull++;
1,241,654,163✔
3433

3434
  return tColDataPutValue(pColData, NULL, 0);
1,241,631,750✔
3435
}
3436
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
3,089,996✔
3437
  int32_t code = 0;
3,198,372✔
3438

3439
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
3,198,372✔
3440
  if (code) return code;
3,198,372✔
3441
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
3,198,372✔
3442
  pColData->numOfValue++;
3,198,372✔
3443

3444
  return tColDataPutValue(pColData, pData, nData);
3,198,372✔
3445
}
3446
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
317,265,080✔
3447
  int32_t code = 0;
317,265,080✔
3448

3449
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
317,265,080✔
3450
  if (code) return code;
317,265,080✔
3451
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
317,265,080✔
3452
  pColData->numOfNone++;
317,265,080✔
3453

3454
  return tColDataPutValue(pColData, NULL, 0);
317,265,080✔
3455
}
3456
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
356,381✔
3457
  int32_t code = 0;
356,381✔
3458

3459
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
356,381✔
3460
  if (code) return code;
356,381✔
3461
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 1);
356,381✔
3462
  pColData->numOfNull++;
356,381✔
3463

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

3476
    //       VALUE                  NONE                     NULL
3477
};
3478

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

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

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

3506
_exit:
70✔
3507
  return code;
70✔
3508
}
3509

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

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

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

3537
  pColData->flag |= HAS_VALUE;
×
3538
  pColData->numOfValue++;
×
3539

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

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

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

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

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

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

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

3586
  pColData->flag |= HAS_VALUE;
×
3587
  pColData->numOfValue++;
×
3588

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

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

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

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

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

3625
  pColData->flag |= HAS_VALUE;
×
3626
  pColData->numOfValue++;
×
3627

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

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

3637
  tFree(pColData->pBitMap);
×
3638
  pColData->pBitMap = pBitMap;
×
3639

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

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

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

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

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

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

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

3683
  pColData->flag |= HAS_NONE;
×
3684
  pColData->numOfNone++;
×
3685

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

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

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

3698
  pColData->flag |= HAS_NULL;
×
3699
  pColData->numOfNull++;
×
3700

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

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

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

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

3716
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
×
3717
  pColData->numOfValue++;
×
3718

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

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

3727
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
×
3728
  pColData->numOfNone++;
×
3729

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

3735
  pColData->flag |= HAS_NULL;
×
3736
  pColData->numOfNull++;
×
3737

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

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

3747
  tFree(pColData->pBitMap);
×
3748
  pColData->pBitMap = pBitMap;
×
3749

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

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

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

3765
  pColData->flag |= HAS_NONE;
×
3766
  pColData->numOfNone++;
×
3767

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

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

3777
  tFree(pColData->pBitMap);
×
3778
  pColData->pBitMap = pBitMap;
×
3779

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

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

3790
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3791
}
3792
static FORCE_INLINE int32_t tColDataAppendValueBlob70(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3793
  int32_t code = 0;
×
3794

3795
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3796
  if (code) return code;
×
3797
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 2);
×
3798
  pColData->numOfValue++;
×
3799

3800
  return tColDataPutValueBlob(pArg, pColData, pData, nData);
×
3801
}
3802
static FORCE_INLINE int32_t tColDataAppendValueBlob71(void *pArg, SColData *pColData, uint8_t *pData, uint32_t nData) {
×
3803
  int32_t code = 0;
×
3804

3805
  code = tRealloc(&pColData->pBitMap, BIT2_SIZE(pColData->nVal + 1));
×
3806
  if (code) return code;
×
3807
  SET_BIT2_EX(pColData->pBitMap, pColData->nVal, 0);
×
3808
  pColData->numOfNone++;
×
3809

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

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

3820
  return tColDataPutValueBlob(pArg, pColData, NULL, 0);
×
3821
}
3822

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

3842
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,791✔
3843
  pColData->numOfNone--;
1,791✔
3844
  pColData->nVal--;
1,791✔
3845
  if (pColData->numOfNone) {
1,791✔
3846
    return tColDataAppendValue10(pColData, pData, nData);
×
3847
  } else {
3848
    pColData->flag = 0;
1,791✔
3849
    return tColDataAppendValue00(pColData, pData, nData);
1,791✔
3850
  }
3851
}
3852
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
24,408✔
3853
  pColData->numOfNone--;
24,408✔
3854
  pColData->nVal--;
24,408✔
3855
  if (pColData->numOfNone) {
24,408✔
3856
    return tColDataAppendValue12(pColData, pData, nData);
21,204✔
3857
  } else {
3858
    pColData->flag = 0;
3,204✔
3859
    return tColDataAppendValue02(pColData, pData, nData);
3,204✔
3860
  }
3861
  return 0;
3862
}
3863
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
86,975✔
3864
  if (forward) {
86,975✔
3865
    pColData->numOfNull--;
86,975✔
3866
    pColData->nVal--;
86,975✔
3867
    if (pColData->numOfNull) {
86,975✔
3868
      return tColDataAppendValue20(pColData, pData, nData);
401✔
3869
    } else {
3870
      pColData->flag = 0;
86,574✔
3871
      return tColDataAppendValue00(pColData, pData, nData);
86,574✔
3872
    }
3873
  }
3874
  return 0;
×
3875
}
3876
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3877
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3878
    pColData->numOfNone--;
×
3879
    pColData->nVal--;
×
3880
    if (pColData->numOfNone) {
×
3881
      return tColDataAppendValue30(pColData, pData, nData);
×
3882
    } else {
3883
      pColData->flag = HAS_NULL;
×
3884
      return tColDataAppendValue20(pColData, pData, nData);
×
3885
    }
3886
  } else if (forward) {  // NULL ==> VALUE
×
3887
    pColData->numOfNull--;
×
3888
    pColData->nVal--;
×
3889
    if (pColData->numOfNull) {
×
3890
      return tColDataAppendValue30(pColData, pData, nData);
×
3891
    } else {
3892
      pColData->flag = HAS_NONE;
×
3893
      return tColDataAppendValue10(pColData, pData, nData);
×
3894
    }
3895
  }
3896
  return 0;
×
3897
}
3898
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
307,536✔
3899
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
307,536✔
3900
    pColData->numOfNone--;
307,536✔
3901
    pColData->numOfNull++;
307,536✔
3902
    if (pColData->numOfNone) {
307,536✔
3903
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
296,856✔
3904
    } else {
3905
      pColData->flag = HAS_NULL;
10,680✔
3906
    }
3907
  }
3908
  return 0;
307,536✔
3909
}
3910
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,147,483,647✔
3911
  if (forward) {  // VALUE ==> VALUE
2,147,483,647✔
3912
    pColData->nVal--;
2,147,483,647✔
3913
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3914
      pColData->nData = pColData->aOffset[pColData->nVal];
1,646,251,067✔
3915
    } else {
3916
      pColData->nData -= TYPE_BYTES[pColData->type];
2,147,483,647✔
3917
    }
3918
    return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3919
  }
3920
  return 0;
×
3921
}
3922
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
585,327✔
3923
  if (forward) {  // VALUE ==> NULL
585,327✔
3924
    pColData->numOfValue--;
585,327✔
3925
    pColData->nVal--;
585,327✔
3926
    if (pColData->numOfValue) {
585,327✔
3927
      if (IS_VAR_DATA_TYPE(pColData->type)) {
565,684✔
3928
        pColData->nData = pColData->aOffset[pColData->nVal];
164,920✔
3929
      } else {
3930
        pColData->nData -= TYPE_BYTES[pColData->type];
400,764✔
3931
      }
3932
      return tColDataAppendValue42(pColData, pData, nData);
565,684✔
3933
    } else {
3934
      pColData->flag = 0;
19,643✔
3935
      pColData->nData = 0;
19,643✔
3936
      return tColDataAppendValue02(pColData, pData, nData);
19,643✔
3937
    }
3938
  }
3939
  return 0;
×
3940
}
3941
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,952,848✔
3942
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
2,952,848✔
3943
    pColData->numOfNone--;
117,800✔
3944
    pColData->nVal--;
117,800✔
3945
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
117,800✔
3946
      pColData->nData -= TYPE_BYTES[pColData->type];
117,800✔
3947
    }
3948
    if (pColData->numOfNone) {
117,800✔
3949
      return tColDataAppendValue50(pColData, pData, nData);
×
3950
    } else {
3951
      pColData->flag = HAS_VALUE;
117,800✔
3952
      return tColDataAppendValue40(pColData, pData, nData);
117,800✔
3953
    }
3954
  } else if (forward) {  // VALUE ==> VALUE
2,835,048✔
3955
    pColData->nVal--;
2,835,048✔
3956
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,835,048✔
3957
      pColData->nData = pColData->aOffset[pColData->nVal];
404,892✔
3958
    } else {
3959
      pColData->nData -= TYPE_BYTES[pColData->type];
2,430,156✔
3960
    }
3961
    return tColDataPutValue(pColData, pData, nData);
2,835,048✔
3962
  }
3963
  return 0;
×
3964
}
3965
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
21,738✔
3966
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
21,738✔
3967
    pColData->numOfNone--;
21,738✔
3968
    pColData->nVal--;
21,738✔
3969
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
21,738✔
3970
      pColData->nData -= TYPE_BYTES[pColData->type];
14,670✔
3971
    }
3972
    if (pColData->numOfNone) {
21,738✔
3973
      return tColDataAppendValue52(pColData, pData, nData);
×
3974
    } else {
3975
      pColData->flag = HAS_VALUE;
21,738✔
3976
      return tColDataAppendValue42(pColData, pData, nData);
21,738✔
3977
    }
3978
  } else if (forward) {  // VALUE ==> NULL
×
3979
    pColData->numOfValue--;
×
3980
    pColData->nVal--;
×
3981
    if (pColData->numOfValue) {
×
3982
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3983
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3984
      } else {
3985
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3986
      }
3987
      return tColDataAppendValue52(pColData, pData, nData);
×
3988
    } else {
3989
      pColData->flag = HAS_NONE;
×
3990
      pColData->nData = 0;
×
3991
      return tColDataAppendValue12(pColData, pData, nData);
×
3992
    }
3993
  }
3994
  return 0;
×
3995
}
3996
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,046,851✔
3997
  if (forward) {
3,046,851✔
3998
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,046,851✔
3999
      pColData->numOfNull--;
2,125,615✔
4000
      pColData->nVal--;
2,125,615✔
4001
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
2,125,615✔
4002
        pColData->nData -= TYPE_BYTES[pColData->type];
1,757,931✔
4003
      }
4004
      if (pColData->numOfNull) {
2,125,026✔
4005
        return tColDataAppendValue60(pColData, pData, nData);
2,445✔
4006
      } else {
4007
        pColData->flag = HAS_VALUE;
2,122,581✔
4008
        return tColDataAppendValue40(pColData, pData, nData);
2,122,581✔
4009
      }
4010
    } else {  // VALUE ==> VALUE
4011
      pColData->nVal--;
921,236✔
4012
      if (IS_VAR_DATA_TYPE(pColData->type)) {
921,236✔
4013
        pColData->nData = pColData->aOffset[pColData->nVal];
110,708✔
4014
      } else {
4015
        pColData->nData -= TYPE_BYTES[pColData->type];
810,528✔
4016
      }
4017
      return tColDataPutValue(pColData, pData, nData);
921,236✔
4018
    }
4019
  }
4020
  return 0;
×
4021
}
4022
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,419,963✔
4023
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
1,419,963✔
4024
    pColData->numOfValue--;
885,850✔
4025
    pColData->nVal--;
885,850✔
4026
    if (pColData->numOfValue) {
885,850✔
4027
      if (IS_VAR_DATA_TYPE(pColData->type)) {
885,850✔
4028
        pColData->nData = pColData->aOffset[pColData->nVal];
94,855✔
4029
      } else {
4030
        pColData->nData -= TYPE_BYTES[pColData->type];
790,995✔
4031
      }
4032
      return tColDataAppendValue62(pColData, pData, nData);
885,850✔
4033
    } else {
4034
      pColData->flag = HAS_NULL;
×
4035
      pColData->nData = 0;
×
4036
      return tColDataAppendValue20(pColData, pData, nData);
×
4037
    }
4038
  }
4039
  return 0;
534,113✔
4040
}
4041
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
298,925✔
4042
  int32_t code = 0;
298,925✔
4043

4044
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
298,925✔
4045
  if (bv == 0) {  // NONE ==> VALUE
298,925✔
4046
    pColData->numOfNone--;
70,680✔
4047
    pColData->nVal--;
70,680✔
4048
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
70,680✔
4049
      pColData->nData -= TYPE_BYTES[pColData->type];
70,680✔
4050
    }
4051
    if (pColData->numOfNone) {
70,680✔
4052
      return tColDataAppendValue70(pColData, pData, nData);
70,680✔
4053
    } else {
4054
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4055
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
×
4056
      }
4057
      pColData->flag = (HAS_VALUE | HAS_NULL);
×
4058
      return tColDataAppendValue60(pColData, pData, nData);
×
4059
    }
4060
  } else if (bv == 1) {  // NULL ==> VALUE
228,245✔
4061
    if (forward) {
38,293✔
4062
      pColData->numOfNull--;
38,293✔
4063
      pColData->nVal--;
38,293✔
4064
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
38,293✔
4065
        pColData->nData -= TYPE_BYTES[pColData->type];
38,293✔
4066
      }
4067
      if (pColData->numOfNull) {
38,293✔
4068
        return tColDataAppendValue70(pColData, pData, nData);
37,696✔
4069
      } else {
4070
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
1,791✔
4071
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
1,194✔
4072
        }
4073
        pColData->flag = (HAS_VALUE | HAS_NONE);
597✔
4074
        return tColDataAppendValue50(pColData, pData, nData);
597✔
4075
      }
4076
    }
4077
  } else if (bv == 2) {  // VALUE ==> VALUE
189,952✔
4078
    if (forward) {
189,952✔
4079
      pColData->nVal--;
189,952✔
4080
      if (IS_VAR_DATA_TYPE(pColData->type)) {
189,952✔
4081
        pColData->nData = pColData->aOffset[pColData->nVal];
47,120✔
4082
      } else {
4083
        pColData->nData -= TYPE_BYTES[pColData->type];
142,832✔
4084
      }
4085
      return tColDataPutValue(pColData, pData, nData);
189,952✔
4086
    }
4087
  } else {
4088
    return TSDB_CODE_INVALID_PARA;
×
4089
  }
4090
  return 0;
×
4091
}
4092
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
352,072✔
4093
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
352,072✔
4094
  if (bv == 0) {  // NONE ==> NULL
352,072✔
4095
    pColData->numOfNone--;
352,072✔
4096
    pColData->nVal--;
352,072✔
4097
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
352,072✔
4098
      pColData->nData -= TYPE_BYTES[pColData->type];
218,538✔
4099
    }
4100
    if (pColData->numOfNone) {
352,072✔
4101
      return tColDataAppendValue72(pColData, pData, nData);
×
4102
    } else {
4103
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
11,073,408✔
4104
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
10,721,336✔
4105
      }
4106
      pColData->flag = (HAS_VALUE | HAS_NULL);
352,072✔
4107
      return tColDataAppendValue62(pColData, pData, nData);
352,072✔
4108
    }
4109
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
×
4110
    pColData->numOfValue--;
×
4111
    pColData->nVal--;
×
4112
    if (pColData->numOfValue) {
×
4113
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
4114
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4115
      } else {
4116
        pColData->nData -= TYPE_BYTES[pColData->type];
×
4117
      }
4118
      return tColDataAppendValue72(pColData, pData, nData);
×
4119
    } else {
4120
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4121
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
×
4122
      }
4123
      pColData->flag = (HAS_NULL | HAS_NONE);
×
4124
      pColData->nData = 0;
×
4125
      return tColDataAppendValue32(pColData, pData, nData);
×
4126
    }
4127
  }
4128
  return 0;
×
4129
}
4130
static FORCE_INLINE int32_t tColDataUpdateNothing(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,713,345✔
4131
  return 0;
2,713,345✔
4132
}
4133
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
4134
    {NULL, NULL, NULL},                                                     // 0
4135
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
4136
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
4137
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
4138
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
4139
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
4140
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
4141
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
4142

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

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

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

4155
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
1,475,413,195✔
4156
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
1,475,413,195✔
4157
}
1,475,591,732✔
4158
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,536,564,311✔
4159
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,536,564,311✔
4160
}
1,536,520,402✔
4161
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
5,996,717✔
4162
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4163
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
5,996,717✔
4164
    case 0:
3,003,365✔
4165
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,003,365✔
4166
      break;
3,002,776✔
4167
    case 1:
2,995,119✔
4168
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
2,995,119✔
4169
      break;
2,996,297✔
4170
    default:
×
4171
      break;
×
4172
  }
4173
}
5,999,073✔
4174
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4175
  SValue value = {.type = pColData->type};
2,147,483,647✔
4176
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
4177
    if (iVal + 1 < pColData->nVal) {
2,147,483,647✔
4178
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
2,147,483,647✔
4179
    } else {
4180
      value.nData = pColData->nData - pColData->aOffset[iVal];
195,721,539✔
4181
    }
4182
    value.pData = pColData->pData + pColData->aOffset[iVal];
2,147,483,647✔
4183
  } else {
4184
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4185
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4186
  }
4187
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4188
}
2,147,483,647✔
4189
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
1,422,617,771✔
4190
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4191
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
1,422,617,771✔
4192
    case 0:
685,709,394✔
4193
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
685,709,394✔
4194
      break;
685,705,184✔
4195
    case 1:
736,890,473✔
4196
      tColDataGetValue4(pColData, iVal, pColVal);
4197
      break;
736,896,437✔
4198
    default:
×
4199
      break;
×
4200
  }
4201
}
1,422,601,621✔
4202
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
2,147,483,647✔
4203
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4204
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4205
    case 0:
2,147,483,647✔
4206
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
2,147,483,647✔
4207
      break;
2,147,483,647✔
4208
    case 1:
2,147,483,647✔
4209
      tColDataGetValue4(pColData, iVal, pColVal);
4210
      break;
2,147,483,647✔
4211
    default:
×
4212
      break;
×
4213
  }
4214
}
2,147,483,647✔
4215
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
20,739,545✔
4216
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4217
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
20,739,545✔
4218
    case 0:
3,726,479✔
4219
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,726,479✔
4220
      break;
3,724,123✔
4221
    case 1:
4,288,238✔
4222
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
4,288,238✔
4223
      break;
4,290,594✔
4224
    case 2:
12,725,417✔
4225
      tColDataGetValue4(pColData, iVal, pColVal);
4226
      break;
12,727,773✔
4227
    default:
×
4228
      break;
×
4229
  }
4230
}
20,742,490✔
4231
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4232
    NULL,               // 0
4233
    tColDataGetValue1,  // HAS_NONE
4234
    tColDataGetValue2,  // HAS_NULL
4235
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4236
    tColDataGetValue4,  // HAS_VALUE
4237
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4238
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4239
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4240
};
4241
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4242
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647✔
4243
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647✔
4244
    return TSDB_CODE_INVALID_PARA;
603✔
4245
  }
4246
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4247
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4248
}
4249

4250
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
2,147,483,647✔
4251
  switch (pColData->flag) {
2,147,483,647✔
4252
    case HAS_NONE:
×
4253
      return 0;
×
4254
    case HAS_NULL:
×
4255
      return 1;
×
4256
    case (HAS_NULL | HAS_NONE):
5,915,916✔
4257
      return GET_BIT1(pColData->pBitMap, iVal);
5,915,916✔
4258
    case HAS_VALUE:
×
4259
      return 2;
×
4260
    case (HAS_VALUE | HAS_NONE):
2,147,483,647✔
4261
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
2,147,483,647✔
4262
    case (HAS_VALUE | HAS_NULL):
2,147,483,647✔
4263
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
2,147,483,647✔
4264
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
99,106,629✔
4265
      return GET_BIT2(pColData->pBitMap, iVal);
99,106,629✔
4266
    default:
×
4267
      return 0;
×
4268
  }
4269
}
4270

4271
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
14,073,751✔
4272
  int32_t code = 0;
14,073,751✔
4273

4274
  *pColData = *pColDataFrom;
14,073,751✔
4275

4276
  // bitmap
4277
  switch (pColData->flag) {
14,073,751✔
4278
    case (HAS_NULL | HAS_NONE):
32,191✔
4279
    case (HAS_VALUE | HAS_NONE):
4280
    case (HAS_VALUE | HAS_NULL):
4281
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
32,191✔
4282
      if (pColData->pBitMap == NULL) {
32,191✔
4283
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4284
        goto _exit;
×
4285
      }
4286
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
32,191✔
4287
      break;
32,191✔
4288
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
4289
      pColData->pBitMap = xMalloc(arg, BIT2_SIZE(pColData->nVal));
×
4290
      if (pColData->pBitMap == NULL) {
×
4291
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4292
        goto _exit;
×
4293
      }
4294
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT2_SIZE(pColData->nVal));
×
4295
      break;
×
4296
    default:
14,041,226✔
4297
      pColData->pBitMap = NULL;
14,041,226✔
4298
      break;
14,041,560✔
4299
  }
4300

4301
  // offset
4302
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
14,073,751✔
4303
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
30,620✔
4304
    if (pColData->aOffset == NULL) {
30,620✔
4305
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4306
      goto _exit;
×
4307
    }
4308
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
30,620✔
4309
  } else {
4310
    pColData->aOffset = NULL;
14,042,809✔
4311
  }
4312

4313
  // value
4314
  if (pColData->nData) {
14,073,101✔
4315
    pColData->pData = xMalloc(arg, pColData->nData);
14,041,709✔
4316
    if (pColData->pData == NULL) {
14,042,043✔
4317
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4318
      goto _exit;
×
4319
    }
4320

4321
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
14,042,043✔
4322
  } else {
4323
    pColData->pData = NULL;
31,392✔
4324
  }
4325

4326
_exit:
14,073,769✔
4327
  return code;
14,073,769✔
4328
}
4329

4330
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
516,795,367✔
4331
  int32_t code;
4332
  SBuffer local;
516,790,560✔
4333

4334
  if (!(colData->nVal > 0)) {
516,858,440✔
4335
    return TSDB_CODE_INVALID_PARA;
×
4336
  }
4337

4338
  (*info) = (SColDataCompressInfo){
516,894,863✔
4339
      .cmprAlg = info->cmprAlg,
516,897,048✔
4340
      .columnFlag = colData->cflag,
516,839,923✔
4341
      .flag = colData->flag,
516,927,675✔
4342
      .dataType = colData->type,
516,931,604✔
4343
      .columnId = colData->cid,
516,921,976✔
4344
      .numOfData = colData->nVal,
516,892,227✔
4345
  };
4346

4347
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
516,853,918✔
4348
    return 0;
17,501,528✔
4349
  }
4350

4351
  tBufferInit(&local);
4352
  if (assist == NULL) {
499,390,704✔
4353
    assist = &local;
×
4354
  }
4355

4356
  // bitmap
4357
  if (colData->flag != HAS_VALUE) {
499,390,704✔
4358
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
26,778,553✔
4359
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,731,008✔
4360
    } else {
4361
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
25,044,593✔
4362
    }
4363

4364
    SCompressInfo cinfo = {
26,778,269✔
4365
        .dataType = TSDB_DATA_TYPE_TINYINT,
4366
        .cmprAlg = info->cmprAlg,
26,778,269✔
4367
        .originalSize = info->bitmapOriginalSize,
26,777,154✔
4368
    };
4369

4370
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
26,777,680✔
4371
    if (code) {
26,778,446✔
4372
      tBufferDestroy(&local);
4373
      return code;
×
4374
    }
4375

4376
    info->bitmapCompressedSize = cinfo.compressedSize;
26,778,446✔
4377
  }
4378

4379
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
499,383,757✔
4380
    tBufferDestroy(&local);
4381
    return 0;
92,234✔
4382
  }
4383

4384
  // offset
4385
  if (IS_VAR_DATA_TYPE(colData->type)) {
499,167,274✔
4386
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
78,287,510✔
4387

4388
    SCompressInfo cinfo = {
78,378,840✔
4389
        .dataType = TSDB_DATA_TYPE_INT,
4390
        .cmprAlg = info->cmprAlg,
78,380,063✔
4391
        .originalSize = info->offsetOriginalSize,
78,382,530✔
4392
    };
4393

4394
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
78,384,923✔
4395
    if (code) {
78,390,412✔
4396
      tBufferDestroy(&local);
4397
      return code;
×
4398
    }
4399

4400
    info->offsetCompressedSize = cinfo.compressedSize;
78,390,412✔
4401
  }
4402

4403
  // data
4404
  if (colData->nData > 0) {
499,315,138✔
4405
    info->dataOriginalSize = colData->nData;
499,277,430✔
4406

4407
    SCompressInfo cinfo = {
499,290,197✔
4408
        .dataType = colData->type,
499,305,800✔
4409
        .cmprAlg = info->cmprAlg,
499,298,670✔
4410
        .originalSize = info->dataOriginalSize,
499,298,656✔
4411
    };
4412

4413
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
499,289,355✔
4414
    if (code) {
499,253,249✔
4415
      tBufferDestroy(&local);
4416
      return code;
×
4417
    }
4418

4419
    info->dataCompressedSize = cinfo.compressedSize;
499,253,249✔
4420
  }
4421

4422
  tBufferDestroy(&local);
4423
  return 0;
499,204,933✔
4424
}
4425

4426
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
895,788,862✔
4427
  int32_t  code;
4428
  SBuffer  local;
895,786,088✔
4429
  uint8_t *data = (uint8_t *)input;
896,032,732✔
4430

4431
  tBufferInit(&local);
4432
  if (assist == NULL) {
896,103,711✔
4433
    assist = &local;
×
4434
  }
4435

4436
  tColDataClear(colData);
896,103,711✔
4437
  colData->cid = info->columnId;
896,189,940✔
4438
  colData->type = info->dataType;
896,170,648✔
4439
  colData->cflag = info->columnFlag;
896,156,202✔
4440
  colData->nVal = info->numOfData;
895,768,632✔
4441
  colData->flag = info->flag;
895,879,344✔
4442

4443
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
895,904,441✔
4444
    goto _exit;
57,360,294✔
4445
  }
4446

4447
  // bitmap
4448
  if (info->bitmapOriginalSize > 0) {
838,638,703✔
4449
    SCompressInfo cinfo = {
97,763,677✔
4450
        .dataType = TSDB_DATA_TYPE_TINYINT,
4451
        .cmprAlg = info->cmprAlg,
97,778,252✔
4452
        .originalSize = info->bitmapOriginalSize,
97,794,868✔
4453
        .compressedSize = info->bitmapCompressedSize,
97,764,259✔
4454
    };
4455

4456
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
97,768,190✔
4457
    if (code) {
97,776,854✔
4458
      tBufferDestroy(&local);
4459
      return code;
×
4460
    }
4461

4462
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
97,776,854✔
4463
    if (code) {
97,755,056✔
4464
      tBufferDestroy(&local);
4465
      return code;
×
4466
    }
4467

4468
    data += cinfo.compressedSize;
97,755,056✔
4469
  }
4470

4471
  if (info->flag == (HAS_NONE | HAS_NULL)) {
838,678,859✔
4472
    goto _exit;
180,823✔
4473
  }
4474

4475
  // offset
4476
  if (info->offsetOriginalSize > 0) {
838,292,432✔
4477
    SCompressInfo cinfo = {
179,509,373✔
4478
        .cmprAlg = info->cmprAlg,
179,505,104✔
4479
        .dataType = TSDB_DATA_TYPE_INT,
4480
        .originalSize = info->offsetOriginalSize,
179,520,011✔
4481
        .compressedSize = info->offsetCompressedSize,
179,506,016✔
4482
    };
4483

4484
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
179,462,911✔
4485
    if (code) {
179,430,598✔
4486
      tBufferDestroy(&local);
4487
      return code;
×
4488
    }
4489

4490
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
179,430,598✔
4491
    if (code) {
179,398,133✔
4492
      tBufferDestroy(&local);
4493
      return code;
×
4494
    }
4495

4496
    data += cinfo.compressedSize;
179,398,133✔
4497
  }
4498

4499
  // data
4500
  if (info->dataOriginalSize > 0) {
838,300,488✔
4501
    colData->nData = info->dataOriginalSize;
838,241,118✔
4502

4503
    SCompressInfo cinfo = {
838,259,467✔
4504
        .cmprAlg = info->cmprAlg,
1,676,659,191✔
4505
        .dataType = colData->type,
838,157,785✔
4506
        .originalSize = info->dataOriginalSize,
838,361,897✔
4507
        .compressedSize = info->dataCompressedSize,
838,422,766✔
4508
    };
4509

4510
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
838,331,044✔
4511
    if (code) {
838,220,264✔
4512
      tBufferDestroy(&local);
4513
      return code;
×
4514
    }
4515

4516
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
838,220,264✔
4517
    if (code) {
838,244,264✔
4518
      tBufferDestroy(&local);
4519
      return code;
×
4520
    }
4521

4522
    data += cinfo.compressedSize;
838,244,264✔
4523
  }
4524

4525
_exit:
896,010,408✔
4526
  switch (colData->flag) {
896,125,101✔
4527
    case HAS_NONE:
22,539,888✔
4528
      colData->numOfNone = colData->nVal;
22,539,888✔
4529
      break;
22,539,888✔
4530
    case HAS_NULL:
34,698,047✔
4531
      colData->numOfNull = colData->nVal;
34,698,047✔
4532
      break;
34,790,039✔
4533
    case HAS_VALUE:
740,689,143✔
4534
      colData->numOfValue = colData->nVal;
740,689,143✔
4535
      break;
740,751,861✔
4536
    default:
97,762,547✔
4537
      for (int32_t i = 0; i < colData->nVal; i++) {
2,147,483,647✔
4538
        uint8_t bitValue = tColDataGetBitValue(colData, i);
2,147,483,647✔
4539
        if (bitValue == 0) {
2,147,483,647✔
4540
          colData->numOfNone++;
723,162,082✔
4541
        } else if (bitValue == 1) {
2,147,483,647✔
4542
          colData->numOfNull++;
2,147,483,647✔
4543
        } else {
4544
          colData->numOfValue++;
2,147,483,647✔
4545
        }
4546
      }
4547
  }
4548
  tBufferDestroy(&local);
4549
  return 0;
895,556,725✔
4550
}
4551

4552
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
54,058✔
4553
                                    char *data) {
4554
  int32_t code = 0;
54,058✔
4555
  if (data == NULL) {
54,058✔
4556
    if (pColData->cflag & COL_IS_KEY) {
1,188✔
4557
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4558
    } else {
4559
      for (int32_t i = 0; i < nRows; ++i) {
2,376✔
4560
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
1,188✔
4561
      }
4562
    }
4563
    goto _exit;
1,188✔
4564
  }
4565

4566
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
52,870✔
4567
    if (!IS_STR_DATA_BLOB(type)) {
6,731✔
4568
      for (int32_t i = 0; i < nRows; ++i) {
13,759✔
4569
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
7,028✔
4570
        if (offset == -1) {
7,028✔
4571
          if (pColData->cflag & COL_IS_KEY) {
×
4572
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4573
            goto _exit;
×
4574
          }
4575
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4576
            goto _exit;
×
4577
          }
4578
        } else {
4579
          if (varDataTLen(data + offset) > bytes || (type == TSDB_DATA_TYPE_NCHAR && varDataLen(data + offset) % TSDB_NCHAR_SIZE != 0)) {
7,028✔
4580
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4581
                   bytes);
4582
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4583
            goto _exit;
×
4584
          }
4585
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
14,056✔
4586
                                                                        varDataLen(data + offset));
7,028✔
4587
        }
4588
      }
4589
    } else {
4590
      for (int32_t i = 0; i < nRows; ++i) {
×
4591
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4592
        if (offset == -1) {
×
4593
          if (pColData->cflag & COL_IS_KEY) {
×
4594
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4595
            goto _exit;
×
4596
          }
4597
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4598
            goto _exit;
×
4599
          }
4600
        } else {
4601
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4602
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4603
                   (int)blobDataTLen(data + offset), bytes);
4604
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4605
            goto _exit;
×
4606
          }
4607
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)blobDataVal(data + offset),
×
4608
                                                                        blobDataLen(data + offset));
×
4609
        }
4610
      }
4611
    }
4612
  } else {  // fixed-length data type
4613
    bool allValue = true;
46,139✔
4614
    bool allNull = true;
46,139✔
4615
    for (int32_t i = 0; i < nRows; ++i) {
120,472✔
4616
      if (!BMIsNull(lengthOrbitmap, i)) {
74,333✔
4617
        allNull = false;
20,312✔
4618
      } else {
4619
        allValue = false;
54,021✔
4620
      }
4621
    }
4622
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
46,139✔
4623
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4624
      goto _exit;
×
4625
    }
4626

4627
    if (allValue) {
46,139✔
4628
      // optimize (todo)
4629
      for (int32_t i = 0; i < nRows; ++i) {
38,995✔
4630
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
20,312✔
4631
      }
4632
    } else if (allNull) {
27,456✔
4633
      // optimize (todo)
4634
      for (int32_t i = 0; i < nRows; ++i) {
81,477✔
4635
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
54,021✔
4636
        if (code) goto _exit;
54,021✔
4637
      }
4638
    } else {
4639
      for (int32_t i = 0; i < nRows; ++i) {
×
4640
        if (BMIsNull(lengthOrbitmap, i)) {
×
4641
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4642
          if (code) goto _exit;
×
4643
        } else {
4644
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
×
4645
        }
4646
      }
4647
    }
4648
  }
4649

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

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

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

4751
_exit:
×
4752
  return code;
×
4753
}
4754

4755
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32_t buffMaxLen, initGeosFn igeos,
20,569,524✔
4756
                               checkWKBGeometryFn cgeos) {
4757
  int32_t code = 0;
20,569,524✔
4758

4759
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
20,569,524✔
4760
    if (!(pColData->type == pBind->buffer_type)) {
20,579,200✔
4761
      return TSDB_CODE_INVALID_PARA;
×
4762
    }
4763
  }
4764

4765
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
20,588,520✔
4766
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
27,486✔
4767
      code = igeos();
1,325✔
4768
      if (code) {
1,325✔
4769
        return code;
×
4770
      }
4771
    }
4772
    for (int32_t i = 0; i < pBind->num; ++i) {
1,267,433✔
4773
      if (pBind->is_null && pBind->is_null[i]) {
1,249,624✔
4774
        if (pColData->cflag & COL_IS_KEY) {
610,924✔
4775
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,320✔
4776
          goto _exit;
1,320✔
4777
        }
4778
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
609,604✔
4779
        if (code) goto _exit;
609,604✔
4780
      } else if (pBind->length[i] > buffMaxLen) {
638,700✔
4781
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4782
      } else {
4783
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
638,700✔
4784
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
2,075✔
4785
          if (code) {
2,075✔
4786
            uError("stmt col[%d] bind geometry wrong format", i);
195✔
4787
            goto _exit;
195✔
4788
          }
4789
        }
4790
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
1,263,088✔
4791
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
638,505✔
4792
      }
4793
    }
4794
  } else {  // fixed-length data type
4795
    bool allValue;
4796
    bool allNull;
4797
    if (pBind->is_null) {
20,558,964✔
4798
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
13,712,252✔
4799
      allNull = (same && pBind->is_null[0] != 0);
13,710,545✔
4800
      allValue = (same && pBind->is_null[0] == 0);
13,710,545✔
4801
    } else {
4802
      allNull = false;
6,840,292✔
4803
      allValue = true;
6,840,292✔
4804
    }
4805

4806
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
20,551,173✔
4807
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
2,640✔
4808
      goto _exit;
2,640✔
4809
    }
4810

4811
    if (allValue) {
20,553,609✔
4812
      // optimize (todo)
4813
      for (int32_t i = 0; i < pBind->num; ++i) {
2,147,483,647✔
4814
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,147,483,647✔
4815
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
2,147,483,647✔
4816
      }
4817
    } else if (allNull) {
23,797✔
4818
      // optimize (todo)
4819
      for (int32_t i = 0; i < pBind->num; ++i) {
1,186✔
4820
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
593✔
4821
        if (code) goto _exit;
593✔
4822
      }
4823
    } else {
4824
      for (int32_t i = 0; i < pBind->num; ++i) {
7,939,908✔
4825
        if (pBind->is_null[i]) {
7,916,704✔
4826
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,499,963✔
4827
          if (code) goto _exit;
3,499,963✔
4828
        } else {
4829
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
8,833,482✔
4830
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,416,741✔
4831
        }
4832
      }
4833
    }
4834
  }
4835

4836
_exit:
98,503,709✔
4837
  return code;
20,589,506✔
4838
}
4839

4840
int32_t tColDataAddValueByBind2(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen) {
340,427,862✔
4841
  int32_t code = 0;
340,427,862✔
4842

4843
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
340,427,862✔
4844
    if (!(pColData->type == pBind->buffer_type)) {
335,737,023✔
4845
      return TSDB_CODE_INVALID_PARA;
×
4846
    }
4847
  }
4848

4849
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
376,398,357✔
4850
    uint8_t *buf = pBind->buffer;
27,034,425✔
4851
    for (int32_t i = 0; i < pBind->num; ++i) {
63,538,242✔
4852
      if (pBind->is_null && pBind->is_null[i]) {
31,998,780✔
4853
        if (pColData->cflag & COL_IS_KEY) {
1,526,896✔
4854
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4855
          goto _exit;
×
4856
        }
4857
        if (pBind->is_null[i] == 1) {
1,526,988✔
4858
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1,526,912✔
4859
          if (code) goto _exit;
1,526,606✔
4860
        } else {
4861
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4862
          if (code) goto _exit;
×
4863
        }
4864
      } else if (pBind->length[i] > buffMaxLen) {
30,403,204✔
4865
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4866
      } else {
4867
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
30,479,983✔
4868
        buf += pBind->length[i];
30,233,371✔
4869
      }
4870
    }
4871
  } else {  // fixed-length data type
4872
    bool allValue;
4873
    bool allNull;
4874
    bool allNone;
4875
    if (pBind->is_null) {
319,733,039✔
4876
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
7,620,713✔
4877
      allNull = (same && pBind->is_null[0] == 1);
7,621,396✔
4878
      allNone = (same && pBind->is_null[0] > 1);
7,622,119✔
4879
      allValue = (same && pBind->is_null[0] == 0);
7,629,003✔
4880
    } else {
4881
      allNull = false;
307,323,537✔
4882
      allNone = false;
307,323,537✔
4883
      allValue = true;
307,323,537✔
4884
    }
4885

4886
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
314,948,760✔
4887
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4888
      goto _exit;
×
4889
    }
4890

4891
    uint8_t *buf = pBind->buffer;
316,271,119✔
4892

4893
    if (allValue) {
316,801,444✔
4894
      // optimize (todo)
4895
      for (int32_t i = 0; i < pBind->num; ++i) {
615,061,762✔
4896
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
306,260,799✔
4897
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
307,850,116✔
4898
          *val = 1;
×
4899
        }
4900
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
309,134,469✔
4901
      }
4902
    } else if (allNull) {
8,391,909✔
4903
      // optimize (todo)
4904
      for (int32_t i = 0; i < pBind->num; ++i) {
15,235,839✔
4905
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
7,611,852✔
4906
        if (code) goto _exit;
7,622,152✔
4907
      }
4908
    } else if (allNone) {
778,222✔
4909
      // optimize (todo)
4910
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4911
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4912
        if (code) goto _exit;
×
4913
      }
4914
    } else {
4915
      for (int32_t i = 0; i < pBind->num; ++i) {
778,222✔
4916
        if (pBind->is_null[i]) {
×
4917
          if (pBind->is_null[i] == 1) {
×
4918
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4919
            if (code) goto _exit;
×
4920
          } else {
4921
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4922
            if (code) goto _exit;
×
4923
          }
4924
        } else {
4925
          uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
×
4926
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
×
4927
            *val = 1;
×
4928
          }
4929

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

4936
_exit:
15,885,334✔
4937
  return code;
342,713,359✔
4938
}
4939

4940
int32_t tColDataAddValueByBind2WithGeos(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
536,697✔
4941
                                        initGeosFn igeos, checkWKBGeometryFn cgeos) {
4942
  int32_t code = 0;
536,697✔
4943

4944
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
536,697✔
4945
    if (!(pColData->type == pBind->buffer_type)) {
539,631✔
4946
      return TSDB_CODE_INVALID_PARA;
×
4947
    }
4948
  }
4949

4950
  if (pColData->type != TSDB_DATA_TYPE_GEOMETRY) {
541,071✔
4951
    return TSDB_CODE_INVALID_OPTION;
×
4952
  }
4953

4954
  code = igeos();
541,341✔
4955
  if (code) {
535,869✔
4956
    return code;
×
4957
  }
4958

4959
  uint8_t *buf = pBind->buffer;
535,869✔
4960
  for (int32_t i = 0; i < pBind->num; ++i) {
1,076,184✔
4961
    if (pBind->is_null && pBind->is_null[i]) {
538,137✔
4962
      if (pColData->cflag & COL_IS_KEY) {
408✔
4963
        code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4964
        goto _exit;
×
4965
      }
4966
      if (pBind->is_null[i] == 1) {
408✔
4967
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
408✔
4968
        if (code) goto _exit;
408✔
4969
      } else {
4970
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4971
        if (code) goto _exit;
×
4972
      }
4973
    } else if (pBind->length[i] > buffMaxLen) {
537,909✔
4974
      return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4975
    } else {
4976
      code = cgeos(buf, pBind->length[i]);
539,331✔
4977
      if (code) {
522,645✔
4978
        uError("stmt2 col[%d] bind geometry with wrong format", i);
×
4979
        goto _exit;
×
4980
      }
4981
      code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, buf, pBind->length[i]);
522,645✔
4982
      buf += pBind->length[i];
538,503✔
4983
    }
4984
  }
4985

4986
_exit:
539,199✔
4987
  return code;
539,199✔
4988
}
4989

4990
int32_t tColDataAddValueByBind2WithBlob(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
150✔
4991
                                        SBlobSet *pBlobSet) {
4992
  int32_t code = 0;
150✔
4993

4994
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
150✔
4995
    if (!(pColData->type == pBind->buffer_type)) {
70✔
4996
      return TSDB_CODE_INVALID_PARA;
×
4997
    }
4998
  }
4999

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

5027
int32_t tColDataAddValueByBind2WithDecimal(SColData *pColData, TAOS_STMT2_BIND *pBind, int32_t buffMaxLen,
1,734✔
5028
                                           uint8_t precision, uint8_t scale) {
5029
  int32_t code = 0;
1,734✔
5030

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

5037
  if (!IS_DECIMAL_TYPE(pColData->type)) {
1,734✔
5038
    return TSDB_CODE_INVALID_OPTION;
×
5039
  }
5040

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

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

5105
  int32_t code = 0;
934,454✔
5106
  int32_t numOfRows = -1;
934,454✔
5107
  SArray *colValArray, *bufArray;
5108
  SColVal colVal;
270,288✔
5109
  int32_t numOfFixedValue = 0;
934,454✔
5110
  int32_t lino = 0;
934,454✔
5111
  bool    hasDecimal128 = false;
934,492✔
5112

5113
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
934,492✔
5114
    return terrno;
×
5115
  }
5116
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
934,432✔
5117
    taosArrayDestroy(colValArray);
×
5118
    return terrno;
×
5119
  }
5120
  for (int i = 0; i < numOfInfos; ++i) {
7,619,718✔
5121
    if (parsedCols) {
6,687,372✔
5122
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
28,080✔
5123
      if (pParsedVal) {
28,080✔
5124
        continue;
5,850✔
5125
      }
5126
    }
5127
    if (numOfRows == -1) {
6,681,522✔
5128
      numOfRows = infos[i].bind->num;
934,219✔
5129
    }
5130

5131
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
13,360,969✔
5132
      taosArrayDestroy(colValArray);
×
5133
      taosArrayDestroy(bufArray);
×
5134
      return terrno;
×
5135
    }
5136
  }
5137

5138
  SRowKey rowKey, lastRowKey;
267,894✔
5139
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
11,724,665✔
5140
    taosArrayClear(colValArray);
10,795,142✔
5141

5142
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
59,093,414✔
5143
      if (parsedCols) {
48,101,630✔
5144
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
30,420✔
5145
        if (pParsedVal) {
30,420✔
5146
          numOfFixedValue++;
6,228✔
5147
          colVal = *pParsedVal;
6,228✔
5148

5149
          if (taosArrayPush(colValArray, &colVal) == NULL) {
7,020✔
5150
            code = terrno;
×
5151
            TAOS_CHECK_GOTO(code, &lino, _exit);
390✔
5152
          }
5153
          continue;
7,020✔
5154
        }
5155
      }
5156

5157
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
48,111,638✔
5158
        if (infos[iInfo].bind->is_null[iRow] == 1) {
975✔
5159
          if (iInfo == 0) {
585✔
5160
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
195✔
5161
            TAOS_CHECK_GOTO(code, &lino, _exit);
195✔
5162
          }
5163
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
390✔
5164
        } else {
5165
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
390✔
5166
        }
5167
      } else {
5168
        SValue value = {
48,126,474✔
5169
            .type = infos[iInfo].type,
48,109,396✔
5170
        };
5171
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
48,161,183✔
5172
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
2,741,111✔
5173
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5174
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5175
            value.nData = length;
×
5176
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5177
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5178
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5179
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5180
              goto _exit;
195✔
5181
            }
5182
            value.pData = *data;
×
5183
            *data += length;
×
5184
          } else {
5185
            int32_t   length = infos[iInfo].bind->length[iRow];
2,825,229✔
5186
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
2,828,459✔
5187
            value.nData = length;
2,827,507✔
5188
            if (value.nData > infos[iInfo].bytes - VARSTR_HEADER_SIZE) {
2,827,507✔
5189
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5190
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5191
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5192
              goto _exit;
×
5193
            }
5194
            value.pData = *data;
2,829,071✔
5195
            *data += length;
2,827,609✔
5196
          }
5197
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5198
        } else {
5199
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
45,612,861✔
5200
            if (!pSchemaExt) {
780✔
5201
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5202
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5203
              goto _exit;
×
5204
            }
5205
            uint8_t precision = 0, scale = 0;
780✔
5206
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
780✔
5207
            Decimal128 dec = {0};
780✔
5208
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
780✔
5209
            int32_t    length = infos[iInfo].bind->length[iRow];
780✔
5210
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
780✔
5211
            *data += length;
780✔
5212
            hasDecimal128 = true;
780✔
5213
            TAOS_CHECK_GOTO(code, &lino, _exit);
780✔
5214

5215
            // precision check
5216
            // scale auto fit
5217

5218
            code = decimal128ToDataVal(&dec, &value);
780✔
5219
            TAOS_CHECK_GOTO(code, &lino, _exit);
780✔
5220

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

5236
            code = decimal64ToDataVal(&dec, &value);
780✔
5237
            TAOS_CHECK_GOTO(code, &lino, _exit);
780✔
5238

5239
          } else {
5240
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
45,599,996✔
5241
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
45,604,731✔
5242
              *val = 1;
×
5243
            }
5244
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
45,604,731✔
5245
          }
5246
        }
5247
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
48,248,620✔
5248
      }
5249
      if (taosArrayPush(colValArray, &colVal) == NULL) {
48,293,417✔
5250
        code = terrno;
×
5251
        goto _exit;
×
5252
      }
5253
    }
5254

5255
    SRow *row;
1,949,436✔
5256

5257
    if (hasBlob == 0) {
10,875,329✔
5258
      SRowBuildScanInfo sinfo = {0};
10,777,938✔
5259
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
10,777,596✔
5260
      TAOS_CHECK_GOTO(code, &lino, _exit);
10,772,255✔
5261
    } else {
5262
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
99,231✔
5263
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
99,231✔
5264
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5265
    }
5266

5267
    if ((taosArrayPush(rowArray, &row)) == NULL) {
10,777,403✔
5268
      code = terrno;
×
5269
      goto _exit;
×
5270
    }
5271

5272
    // fix decimal memory leak
5273
    if (hasDecimal128) {
10,777,403✔
5274
      int32_t num = taosArrayGetSize(colValArray);
780✔
5275
      for (int32_t i = 0; i < num; ++i) {
3,120✔
5276
        SColVal *pCol = taosArrayGet(colValArray, i);
2,340✔
5277
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
2,340✔
5278
          taosMemoryFreeClear(pCol->value.pData);
780✔
5279
        }
5280
      }
5281
      hasDecimal128 = false;
780✔
5282
    }
5283

5284
    if (pOrdered && pDupTs) {
10,777,403✔
5285
      tRowGetKey(row, &rowKey);
21,559,450✔
5286
      if (iRow == 0) {
10,784,680✔
5287
        *pOrdered = true;
933,742✔
5288
        *pDupTs = false;
933,780✔
5289
      } else {
5290
        if (*pOrdered) {
9,850,938✔
5291
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
9,849,892✔
5292
          *pOrdered = (res >= 0);
9,849,892✔
5293
          if (!*pDupTs) {
9,848,963✔
5294
            *pDupTs = (res == 0);
9,849,258✔
5295
          }
5296
        }
5297
      }
5298
      lastRowKey = rowKey;
10,788,814✔
5299
    }
5300
  }
5301
_exit:
927,173✔
5302
  if (code != 0) {
929,951✔
5303
    if (hasDecimal128) {
390✔
5304
      int32_t num = taosArrayGetSize(colValArray);
×
5305
      for (int32_t i = 0; i < num; ++i) {
×
5306
        SColVal *pCol = taosArrayGet(colValArray, i);
×
5307
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
×
5308
          taosMemoryFreeClear(pCol->value.pData);
×
5309
        }
5310
      }
5311
    }
5312
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
390✔
5313
  }
5314
  taosArrayDestroy(colValArray);
929,951✔
5315
  taosArrayDestroy(bufArray);
934,377✔
5316
  return code;
934,610✔
5317
}
5318
/* build rows to `rowArray` from bind
5319
 * `infos` is the bind information array
5320
 * `numOfInfos` is the number of bind information
5321
 * `infoSorted` is whether the bind information is sorted by column id
5322
 * `pTSchema` is the schema of the table
5323
 * `rowArray` is the array to store the rows
5324
 * `pOrdered` is the pointer to store ordered
5325
 * `pDupTs` is the pointer to store duplicateTs
5326
 */
5327
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
×
5328
                                   SArray *rowArray, bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5329
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
×
5330
    return TSDB_CODE_INVALID_PARA;
×
5331
  }
5332
  int8_t hasBlob = schemaHasBlob(pTSchema);
×
5333
  if (!infoSorted) {
×
5334
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5335
  }
5336

5337
  int32_t code = 0;
×
5338
  int32_t numOfRows = infos[0].bind->num;
×
5339
  SArray *colValArray, *bufArray;
5340
  SColVal colVal;
×
5341

5342
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
×
5343
    return terrno;
×
5344
  }
5345
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
×
5346
    taosArrayDestroy(colValArray);
×
5347
    return terrno;
×
5348
  }
5349
  for (int i = 0; i < numOfInfos; ++i) {
×
5350
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
×
5351
      taosArrayDestroy(colValArray);
×
5352
      taosArrayDestroy(bufArray);
×
5353
      return terrno;
×
5354
    }
5355
  }
5356

5357
  SRowKey rowKey, lastRowKey;
×
5358
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
×
5359
    taosArrayClear(colValArray);
×
5360

5361
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
×
5362
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
×
5363
        if (infos[iInfo].bind->is_null[iRow] == 1) {
×
5364
          if (iInfo == 0) {
×
5365
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5366
            goto _exit;
×
5367
          }
5368
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
×
5369
        } else {
5370
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5371
        }
5372
      } else {
5373
        SValue value = {
×
5374
            .type = infos[iInfo].type,
×
5375
        };
5376
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
×
5377
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
×
5378
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5379
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
×
5380
            value.nData = length;
×
5381
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5382
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5383
              uError("stmt bind param[%d] length:%d  greater than type maximum lenght: %d", iInfo, value.nData,
×
5384
                     pTSchema->columns[infos[iInfo].columnId - 1].bytes);
5385
              goto _exit;
×
5386
            }
5387
            value.pData = *data;
×
5388
            *data += length;
×
5389
          } else {
5390
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5391
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo];
×
5392
            value.nData = length;
×
5393
            if (value.nData > pTSchema->columns[infos[iInfo].columnId - 1].bytes - VARSTR_HEADER_SIZE) {
×
5394
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5395
              uError("stmt bind param[%d] length:%d  greater than type maximum lenght: %d", iInfo, value.nData,
×
5396
                     pTSchema->columns[infos[iInfo].columnId - 1].bytes);
5397
              goto _exit;
×
5398
            }
5399
            value.pData = *data;
×
5400
            *data += length;
×
5401
          }
5402

5403
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5404
        } else {
5405
          uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
×
5406
          if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
×
5407
            *val = 1;
×
5408
          }
5409
          valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
×
5410
        }
5411
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
×
5412
      }
5413
      if (taosArrayPush(colValArray, &colVal) == NULL) {
×
5414
        code = terrno;
×
5415
        goto _exit;
×
5416
      }
5417
    }
5418

5419
    SRow *row;
×
5420

5421
    if (hasBlob == 0) {
×
5422
      SRowBuildScanInfo sinfo = {0};
×
5423
      if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
×
5424
        goto _exit;
×
5425
      }
5426
    } else {
5427
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
×
5428
      if ((code = tRowBuildWithBlob(colValArray, pTSchema, &row, pBlobSet, &sinfo))) {
×
5429
        goto _exit;
×
5430
      }
5431
    }
5432

5433
    if ((taosArrayPush(rowArray, &row)) == NULL) {
×
5434
      code = terrno;
×
5435
      goto _exit;
×
5436
    }
5437

5438
    if (pOrdered && pDupTs) {
×
5439
      tRowGetKey(row, &rowKey);
×
5440
      if (iRow == 0) {
×
5441
        *pOrdered = true;
×
5442
        *pDupTs = false;
×
5443
      } else {
5444
        if (*pOrdered) {
×
5445
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
×
5446
          *pOrdered = (res >= 0);
×
5447
          if (!*pDupTs) {
×
5448
            *pDupTs = (res == 0);
×
5449
          }
5450
        }
5451
      }
5452
      lastRowKey = rowKey;
×
5453
    }
5454
  }
5455
_exit:
×
5456
  taosArrayDestroy(colValArray);
×
5457
  taosArrayDestroy(bufArray);
×
5458
  return code;
×
5459
}
5460

5461
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
42,210,204✔
5462
  int32_t code = TSDB_CODE_SUCCESS;
42,210,204✔
5463

5464
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
42,210,204✔
5465
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
5,313,280✔
5466
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
4,783,138✔
5467
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
10,096,418✔
5468
    if (iToRow == 0) {
5,313,280✔
5469
      pToColData->aOffset[iToRow] = 0;
10,674✔
5470
    }
5471

5472
    if (iToRow < pToColData->nVal - 1) {
5,313,280✔
5473
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
5,303,199✔
5474
    }
5475

5476
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
5,313,280✔
5477
                 nData);
5478
  } else {
5479
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
36,896,924✔
5480
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
36,896,924✔
5481
  }
5482
  return code;
42,210,204✔
5483
}
5484

5485
static int32_t tColDataCopyRowSingleCol(SColData *pFromColData, int32_t iFromRow, SColData *pToColData,
43,728,284✔
5486
                                        int32_t iToRow) {
5487
  int32_t code = TSDB_CODE_SUCCESS;
43,728,284✔
5488
  int     bit_val = 0;
43,728,284✔
5489

5490
  switch (pFromColData->flag) {
43,728,284✔
5491
    case HAS_NONE: {
×
5492
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5493
    } break;
×
5494
    case HAS_NULL: {
1,518,080✔
5495
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
1,518,080✔
5496
    } break;
1,518,080✔
5497
    case (HAS_NULL | HAS_NONE): {
×
5498
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5499
      if (0 == bit_val)
×
5500
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5501
      else
5502
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
×
5503
    } break;
×
5504
    case HAS_VALUE: {
4,712,958✔
5505
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
4,712,958✔
5506
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
4,712,958✔
5507
    } break;
4,712,958✔
5508
    case (HAS_VALUE | HAS_NONE): {
×
5509
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5510
      if (0 == bit_val)
×
5511
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5512
      else
5513
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
×
5514
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5515
    } break;
×
5516
    case (HAS_VALUE | HAS_NULL): {
37,497,246✔
5517
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
37,497,246✔
5518
      if (0 == bit_val)
37,497,246✔
5519
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
16,926,927✔
5520
      else
5521
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
20,570,319✔
5522
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
37,497,246✔
5523
    } break;
37,497,246✔
5524
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
5525
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
5526
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5527
    } break;
×
5528
    default:
×
5529
      return -1;
×
5530
  }
5531

5532
  return code;
43,728,284✔
5533
}
5534

5535
static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
2,736,334✔
5536
                               int32_t nColData) {
5537
  int32_t code = TSDB_CODE_SUCCESS;
2,736,334✔
5538

5539
  for (int32_t i = 0; i < nColData; i++) {
46,464,618✔
5540
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
43,728,284✔
5541
    if (code != TSDB_CODE_SUCCESS) {
43,728,284✔
5542
      return code;
×
5543
    }
5544
  }
5545

5546
  return code;
2,736,334✔
5547
}
5548

5549
static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
2,736,334✔
5550
  int32_t code = TSDB_CODE_SUCCESS;
2,736,334✔
5551

5552
  for (int32_t i = 0; i < nColData; i++) {
46,464,618✔
5553
    SColVal cv = {0};
43,728,284✔
5554
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
43,728,284✔
5555
    if (code != TSDB_CODE_SUCCESS) {
43,728,284✔
5556
      return code;
×
5557
    }
5558
    code = tColDataAppendValue(&aToColData[i], &cv);
43,728,284✔
5559
    if (code != TSDB_CODE_SUCCESS) {
43,728,284✔
5560
      return code;
×
5561
    }
5562
  }
5563

5564
  return code;
2,736,334✔
5565
}
5566

5567
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
2,147,483,647✔
5568
  SColVal cv;
2,147,483,647✔
5569

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

5573
  for (int i = 1; i < nColData; i++) {
2,147,483,647✔
5574
    if (aColData[i].cflag & COL_IS_KEY) {
2,147,483,647✔
5575
      tColDataGetValue4(&aColData[i], iRow, &cv);
561,650✔
5576
      key->pks[key->numOfPKs++] = cv.value;
561,650✔
5577
    } else {
5578
      break;
2,147,483,647✔
5579
    }
5580
  }
5581
}
2,147,483,647✔
5582

5583
static int32_t tColDataMergeSortMerge(SColData *aColData, int32_t start, int32_t mid, int32_t end, int32_t nColData) {
304,333✔
5584
  SColData *aDstColData = NULL;
304,333✔
5585
  int32_t   i = start, j = mid + 1, k = 0;
304,333✔
5586
  SRowKey   keyi, keyj;
303,358✔
5587

5588
  if (end > start) {
304,333✔
5589
    aDstColData = taosMemoryCalloc(1, sizeof(SColData) * nColData);
304,333✔
5590
    if (aDstColData == NULL) {
304,333✔
5591
      return terrno;
×
5592
    }
5593
    for (int c = 0; c < nColData; ++c) {
5,155,321✔
5594
      tColDataInit(&aDstColData[c], aColData[c].cid, aColData[c].type, aColData[c].cflag);
4,850,988✔
5595
    }
5596
  }
5597

5598
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
304,333✔
5599
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
304,333✔
5600
  while (i <= mid && j <= end) {
1,672,890✔
5601
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
1,368,557✔
5602
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,365✔
5603
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
1,365✔
5604
    } else {
5605
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,367,192✔
5606
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
1,367,192✔
5607
    }
5608
  }
5609

5610
  while (i <= mid) {
1,671,330✔
5611
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,366,997✔
5612
  }
5613

5614
  while (j <= end) {
305,113✔
5615
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
780✔
5616
  }
5617

5618
  for (i = start, k = 0; i <= end; ++i, ++k) {
3,040,667✔
5619
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
2,736,334✔
5620
  }
5621

5622
  if (aDstColData) {
304,333✔
5623
    for (int32_t i = 0; i < nColData; i++) {
5,155,321✔
5624
      tColDataDestroy(&aDstColData[i]);
4,850,988✔
5625
    }
5626
    taosMemoryFree(aDstColData);
304,333✔
5627
  }
5628

5629
  return TSDB_CODE_SUCCESS;
304,333✔
5630
}
5631

5632
static int32_t tColDataMergeSort(SColData *aColData, int32_t start, int32_t end, int32_t nColData) {
609,789✔
5633
  int32_t ret = TSDB_CODE_SUCCESS;
609,789✔
5634
  int32_t mid;
5635

5636
  if (start >= end) {
609,789✔
5637
    return TSDB_CODE_SUCCESS;
305,456✔
5638
  }
5639

5640
  mid = (start + end) / 2;
304,333✔
5641

5642
  ret = tColDataMergeSort(aColData, start, mid, nColData);
304,333✔
5643
  if (ret != TSDB_CODE_SUCCESS) {
304,333✔
5644
    return ret;
×
5645
  }
5646

5647
  ret = tColDataMergeSort(aColData, mid + 1, end, nColData);
304,333✔
5648
  if (ret != TSDB_CODE_SUCCESS) {
304,333✔
5649
    return ret;
×
5650
  }
5651

5652
  return tColDataMergeSortMerge(aColData, start, mid, end, nColData);
304,333✔
5653
}
5654

5655
static int32_t tColDataSort(SColData *aColData, int32_t nColData) {
1,123✔
5656
  int32_t nVal = aColData[0].nVal;
1,123✔
5657

5658
  if (nVal < 2) return TSDB_CODE_SUCCESS;
1,123✔
5659

5660
  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
1,123✔
5661
}
5662

5663
static int32_t tColDataMerge(SArray **colArr) {
195✔
5664
  int32_t code = 0;
195✔
5665
  SArray *src = *colArr;
195✔
5666
  SArray *dst = NULL;
195✔
5667

5668
  dst = taosArrayInit(taosArrayGetSize(src), sizeof(SColData));
195✔
5669
  if (dst == NULL) {
195✔
5670
    return terrno;
×
5671
  }
5672

5673
  for (int32_t i = 0; i < taosArrayGetSize(src); i++) {
585✔
5674
    SColData *srcCol = taosArrayGet(src, i);
390✔
5675

5676
    SColData *dstCol = taosArrayReserve(dst, 1);
390✔
5677
    if (dstCol == NULL) {
390✔
5678
      code = terrno;
×
5679
      goto _exit;
×
5680
    }
5681
    tColDataInit(dstCol, srcCol->cid, srcCol->type, srcCol->cflag);
390✔
5682
  }
5683

5684
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
195✔
5685
  SRowKey lastKey;
×
5686
  for (int32_t i = 0; i < numRows; i++) {
1,365✔
5687
    SRowKey key;
×
5688
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
1,170✔
5689

5690
    if (i == 0 || tRowKeyCompare(&key, &lastKey) != 0) {  // append new row
2,145✔
5691
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
1,170✔
5692
        SColData *srcCol = taosArrayGet(src, j);
780✔
5693
        SColData *dstCol = taosArrayGet(dst, j);
780✔
5694

5695
        SColVal cv;
×
5696
        code = tColDataGetValue(srcCol, i, &cv);
780✔
5697
        if (code != TSDB_CODE_SUCCESS) {
780✔
5698
          goto _exit;
×
5699
        }
5700
        code = tColDataAppendValue(dstCol, &cv);
780✔
5701
        if (code) {
780✔
5702
          goto _exit;
×
5703
        }
5704
      }
5705
      lastKey = key;
390✔
5706
    } else {  // update existing row
5707
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
2,340✔
5708
        SColData *srcCol = taosArrayGet(src, j);
1,560✔
5709
        SColData *dstCol = taosArrayGet(dst, j);
1,560✔
5710

5711
        SColVal cv;
×
5712
        code = tColDataGetValue(srcCol, i, &cv);
1,560✔
5713
        if (code != TSDB_CODE_SUCCESS) {
1,560✔
5714
          goto _exit;
×
5715
        }
5716
        code = tColDataUpdateValue(dstCol, &cv, true);
1,560✔
5717
        if (code) {
1,560✔
5718
          goto _exit;
×
5719
        }
5720
      }
5721
    }
5722
  }
5723

5724
_exit:
195✔
5725
  if (code) {
195✔
5726
    taosArrayDestroyEx(dst, tColDataDestroy);
×
5727
  } else {
5728
    taosArrayDestroyEx(src, tColDataDestroy);
195✔
5729
    *colArr = dst;
195✔
5730
  }
5731
  return code;
195✔
5732
}
5733

5734
int32_t tColDataSortMerge(SArray **arr) {
6,956,015✔
5735
  SArray   *colDataArr = *arr;
6,956,015✔
5736
  int32_t   nColData = TARRAY_SIZE(colDataArr);
6,960,436✔
5737
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
6,961,789✔
5738

5739
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
6,962,770✔
5740
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5741
  }
5742
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
6,962,425✔
5743
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5744
  }
5745
  if (!(aColData[0].flag == HAS_VALUE)) {
6,962,425✔
5746
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5747
  }
5748

5749
  if (aColData[0].nVal <= 1) goto _exit;
6,960,373✔
5750

5751
  int8_t doSort = 0;
6,940,863✔
5752
  int8_t doMerge = 0;
6,940,863✔
5753
  // scan -------
5754
  SRowKey lastKey;
6,902,206✔
5755
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
6,942,915✔
5756
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
2,147,483,647✔
5757
    SRowKey key;
2,147,483,647✔
5758
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
2,147,483,647✔
5759

5760
    int32_t c = tRowKeyCompare(&lastKey, &key);
2,147,483,647✔
5761
    if (c < 0) {
2,147,483,647✔
5762
      lastKey = key;
2,147,483,647✔
5763
      continue;
2,147,483,647✔
5764
    } else if (c > 0) {
8,335,478✔
5765
      doSort = 1;
1,123✔
5766
      break;
1,123✔
5767
    } else {
5768
      doMerge = 1;
8,334,355✔
5769
    }
5770
  }
5771

5772
  // sort -------
5773
  if (doSort) {
6,124,443✔
5774
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
1,123✔
5775
  }
5776

5777
  if ((doMerge != 1) && (doSort == 1)) {
6,125,317✔
5778
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
1,123✔
5779
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
304,481✔
5780
      SRowKey key;
303,358✔
5781
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
303,553✔
5782

5783
      int32_t c = tRowKeyCompare(&lastKey, &key);
303,553✔
5784
      if (c == 0) {
303,553✔
5785
        doMerge = 1;
195✔
5786
        break;
195✔
5787
      }
5788
      lastKey = key;
303,358✔
5789
    }
5790
  }
5791

5792
  // merge -------
5793
  if (doMerge) {
6,942,575✔
5794
    int32_t code = tColDataMerge(arr);
195✔
5795
    if (code) return code;
195✔
5796
  }
5797

5798
_exit:
6,962,037✔
5799
  return 0;
6,964,092✔
5800
}
5801

5802
int32_t tColDataSortMergeWithBlob(SArray **arr, SBlobSet *pBlob) {
×
5803
  SArray   *colDataArr = *arr;
×
5804
  int32_t   nColData = TARRAY_SIZE(colDataArr);
×
5805
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
×
5806

5807
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
×
5808
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5809
  }
5810
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
×
5811
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5812
  }
5813
  if (!(aColData[0].flag == HAS_VALUE)) {
×
5814
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5815
  }
5816

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

5819
  int8_t doSort = 0;
×
5820
  int8_t doMerge = 0;
×
5821
  // scan -------
5822
  SRowKey lastKey;
×
5823
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5824
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5825
    SRowKey key;
×
5826
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5827

5828
    int32_t c = tRowKeyCompare(&lastKey, &key);
×
5829
    if (c < 0) {
×
5830
      lastKey = key;
×
5831
      continue;
×
5832
    } else if (c > 0) {
×
5833
      doSort = 1;
×
5834
      break;
×
5835
    } else {
5836
      doMerge = 1;
×
5837
    }
5838
  }
5839
  if (doMerge || doSort) {
×
5840
    return TSDB_CODE_BLOB_NOT_SUPPORT;
×
5841
  }
5842
  // sort -------
5843
  if (doSort) {
×
5844
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
×
5845
  }
5846

5847
  if ((doMerge != 1) && (doSort == 1)) {
×
5848
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5849
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5850
      SRowKey key;
×
5851
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5852

5853
      int32_t c = tRowKeyCompare(&lastKey, &key);
×
5854
      if (c == 0) {
×
5855
        doMerge = 1;
×
5856
        break;
×
5857
      }
5858
      lastKey = key;
×
5859
    }
5860
  }
5861

5862
  // merge -------
5863
  if (doMerge) {
×
5864
    int32_t code = tColDataMerge(arr);
×
5865
    if (code) return code;
×
5866
  }
5867

5868
_exit:
×
5869
  return 0;
×
5870
}
5871

5872
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
42,710,274✔
5873
  int32_t code = 0;
42,710,274✔
5874

5875
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
85,440,895✔
5876
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
85,456,160✔
5877
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
85,439,897✔
5878
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
85,418,480✔
5879

5880
  // bitmap
5881
  switch (pColData->flag) {
42,704,122✔
5882
    case (HAS_NULL | HAS_NONE):
87,630✔
5883
    case (HAS_VALUE | HAS_NONE):
5884
    case (HAS_VALUE | HAS_NULL):
5885
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
87,630✔
5886
      if (code) return code;
87,630✔
5887
      break;
87,630✔
5888
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5889
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
5890
      if (code) return code;
×
5891
      break;
×
5892
    default:
42,603,763✔
5893
      break;
42,603,763✔
5894
  }
5895

5896
  // value
5897
  if (pColData->flag & HAS_VALUE) {
42,691,393✔
5898
    if (IS_VAR_DATA_TYPE(pColData->type)) {
42,649,631✔
5899
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
132,697✔
5900
      if (code) return code;
127,188✔
5901

5902
      code = tEncodeI32v(pEncoder, pColData->nData);
127,188✔
5903
      if (code) return code;
127,094✔
5904

5905
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
127,094✔
5906
      if (code) return code;
127,142✔
5907
    } else {
5908
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
42,522,420✔
5909
      if (code) return code;
42,505,198✔
5910
    }
5911
  }
5912

5913
  return code;
42,689,851✔
5914
}
5915

5916
static int32_t tDecodeColDataVersion0(SDecoder *pDecoder, SColData *pColData) {
29,946,469✔
5917
  int32_t code = 0;
29,946,469✔
5918

5919
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
59,893,035✔
5920
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
59,896,697✔
5921
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
59,897,930✔
5922
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
59,899,133✔
5923

5924
  if (pColData->type <= 0 || pColData->type >= TSDB_DATA_TYPE_MAX || pColData->flag <= 0 || pColData->flag >= 8) {
29,951,334✔
5925
    return TSDB_CODE_INVALID_PARA;
×
5926
  }
5927

5928
  // bitmap
5929
  switch (pColData->flag) {
29,949,664✔
5930
    case (HAS_NULL | HAS_NONE):
33,551✔
5931
    case (HAS_VALUE | HAS_NONE):
5932
    case (HAS_VALUE | HAS_NULL):
5933
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
33,551✔
5934
      if (code) return code;
33,551✔
5935
      break;
33,551✔
5936
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5937
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
5938
      if (code) return code;
×
5939
      break;
×
5940
    default:
29,907,550✔
5941
      break;
29,907,550✔
5942
  }
5943

5944
  // value
5945
  if (pColData->flag & HAS_VALUE) {
29,941,101✔
5946
    if (IS_VAR_DATA_TYPE(pColData->type)) {
29,913,853✔
5947
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
29,343✔
5948
      if (code) return code;
30,620✔
5949

5950
      code = tDecodeI32v(pDecoder, &pColData->nData);
30,620✔
5951
      if (code) return code;
30,620✔
5952

5953
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
30,620✔
5954
      if (code) return code;
30,620✔
5955
    } else {
5956
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
29,883,762✔
5957
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
29,886,221✔
5958
      if (code) return code;
29,886,440✔
5959
    }
5960
  }
5961
  pColData->cflag = 0;
29,947,654✔
5962

5963
  return code;
29,948,647✔
5964
}
5965

5966
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
42,716,915✔
5967
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
42,716,915✔
5968
  if (code) return code;
42,683,371✔
5969
  return tEncodeI8(pEncoder, pColData->cflag);
85,411,308✔
5970
}
5971

5972
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
29,947,793✔
5973
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
29,947,793✔
5974
  if (code) return code;
29,946,965✔
5975

5976
  code = tDecodeI8(pDecoder, &pColData->cflag);
29,946,965✔
5977
  return code;
29,948,662✔
5978
}
5979

5980
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
42,689,406✔
5981
  if (version == 0) {
42,689,406✔
5982
    return tEncodeColDataVersion0(pEncoder, pColData);
×
5983
  } else if (version == 1) {
42,689,406✔
5984
    return tEncodeColDataVersion1(pEncoder, pColData);
×
5985
  } else if (version == 2) {
42,689,406✔
5986
    int32_t posStart = pEncoder->pos;
42,696,776✔
5987
    pEncoder->pos += INT_BYTES;
42,714,110✔
5988
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
42,734,587✔
5989
    if (code) return code;
42,717,921✔
5990
    int32_t posEnd = pEncoder->pos;
42,717,921✔
5991
    int32_t pos = posEnd - posStart;
42,726,070✔
5992
    pEncoder->pos = posStart;
42,726,070✔
5993
    code = tEncodeI32(pEncoder, pos);
42,720,532✔
5994
    pEncoder->pos = posEnd;
42,720,532✔
5995
    return code;
42,721,725✔
5996
  } else {
5997
    return TSDB_CODE_INVALID_PARA;
×
5998
  }
5999
}
6000

6001
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
44,019,558✔
6002
  if (version == 0) {
44,019,558✔
6003
    return tDecodeColDataVersion0(pDecoder, pColData);
×
6004
  } else if (version == 1) {
44,019,558✔
6005
    return tDecodeColDataVersion1(pDecoder, pColData);
×
6006
  } else if (version == 2) {
44,019,558✔
6007
    if (jump) {
44,019,351✔
6008
      int32_t len = 0;
14,073,228✔
6009
      int32_t code = tDecodeI32(pDecoder, &len);
14,073,334✔
6010
      if (code) return code;
14,073,334✔
6011
      pDecoder->pos += (len - INT_BYTES);
14,073,334✔
6012
      return TSDB_CODE_SUCCESS;
14,073,355✔
6013
    } else {
6014
      pDecoder->pos += INT_BYTES;
29,946,123✔
6015
      return tDecodeColDataVersion1(pDecoder, pColData);
29,948,334✔
6016
    }    
6017
  } else {
6018
    return TSDB_CODE_INVALID_PARA;
207✔
6019
  }
6020
}
6021

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

6024
int32_t tDecodeRow(SDecoder *pDecoder, SRow **ppRow) {
2,147,483,647✔
6025
  if (ppRow == NULL) {
2,147,483,647✔
6026
    return TSDB_CODE_INVALID_PARA;
×
6027
  }
6028

6029
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
2,147,483,647✔
6030
    return TSDB_CODE_OUT_OF_RANGE;
×
6031
  }
6032

6033
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
2,147,483,647✔
6034
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
2,147,483,647✔
6035
}
6036

6037
int32_t tEncodeBlobSet(SEncoder *pEncoder, SBlobSet *pRow) {
91,734✔
6038
  int32_t code = 0;
91,734✔
6039
  int32_t lino = 0;
91,734✔
6040
  uint8_t compressed = 0;
91,734✔
6041
  int32_t compressSize = 0;
91,734✔
6042
  char   *p = NULL;
91,734✔
6043

6044
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pRow->type));
183,468✔
6045
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pRow->len));
183,468✔
6046

6047
  int32_t nSeq = taosArrayGetSize(pRow->pSeqTable);
91,734✔
6048
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, nSeq));
91,734✔
6049

6050
  // if (pRow->type) {
6051
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
91,734✔
6052
  while (pIter) {
50,285,210✔
6053
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
50,193,476✔
6054
    int32_t  idx = *(int32_t *)pIter;
50,193,476✔
6055
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
50,193,476✔
6056
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
50,193,476✔
6057

6058
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
50,193,476✔
6059
  }
6060
  //}
6061
  for (int32_t i = 0; i < nSeq; i++) {
50,285,210✔
6062
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
50,193,476✔
6063
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
100,386,952✔
6064
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
100,386,952✔
6065
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
100,386,952✔
6066
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
100,386,952✔
6067
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
100,386,952✔
6068
  }
6069

6070
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
183,468✔
6071

6072
_exit:
91,734✔
6073
  return code;
91,734✔
6074
}
6075

6076
int32_t tDecodeBlobSet(SDecoder *pDecoder, SBlobSet **pBlobSet) {
45,819✔
6077
  int32_t    code = 0;
45,819✔
6078
  int32_t    lino = 0;
45,819✔
6079
  int32_t    nSeq = 0;
45,819✔
6080
  uint8_t    compressd = 0;
45,819✔
6081
  int32_t    compressSize = 0;
45,819✔
6082
  SBlobSet  *pBlob = taosMemCalloc(1, sizeof(SBlobSet));
45,819✔
6083
  if (pBlob == NULL) {
45,819✔
6084
    TAOS_CHECK_EXIT(terrno);
×
6085
  }
6086
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pBlob->type));
91,638✔
6087
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pBlob->len));
91,638✔
6088

6089
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
45,819✔
6090

6091
  // if (pBlob->type) {
6092
  pBlob->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
45,819✔
6093
  if (pBlob->pSeqToffset == NULL) {
45,819✔
6094
    TAOS_CHECK_EXIT(terrno);
×
6095
  }
6096
  for (int32_t i = 0; i < nSeq; i++) {
25,142,502✔
6097
    uint64_t seq;
25,096,683✔
6098
    int32_t  idx;
25,096,683✔
6099
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &seq));
25,096,683✔
6100
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
25,096,683✔
6101

6102
    code = taosHashPut(pBlob->pSeqToffset, &seq, sizeof(seq), &idx, sizeof(idx));
25,096,683✔
6103
    TAOS_CHECK_EXIT(code);
25,096,683✔
6104
  }
6105
  //}
6106

6107
  pBlob->pSeqTable = taosArrayInit(nSeq, sizeof(SBlobValue));
45,819✔
6108
  if (pBlob->pSeqTable == NULL) {
45,819✔
6109
    TAOS_CHECK_EXIT(terrno);
×
6110
  }
6111

6112
  for (int32_t i = 0; i < nSeq; i++) {
25,142,502✔
6113
    SBlobValue value;
25,096,683✔
6114
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
25,096,683✔
6115
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
25,096,683✔
6116
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
25,096,683✔
6117
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
25,096,683✔
6118
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
25,096,683✔
6119
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
50,193,366✔
6120
      TAOS_CHECK_EXIT(terrno);
×
6121
    }
6122
  }
6123

6124
  pBlob->data = taosMemCalloc(1, pBlob->len);
45,819✔
6125
  if (pBlob->data == NULL) {
45,819✔
6126
    TAOS_CHECK_EXIT(terrno);
×
6127
  }
6128

6129
  TAOS_CHECK_EXIT(tDecodeFixed(pDecoder, pBlob->data, pBlob->len));
45,819✔
6130
  *pBlobSet = pBlob;
45,819✔
6131

6132
  uTrace("decode blob len:%d", (int32_t)(pBlob->len));
45,819✔
6133

6134
_exit:
45,819✔
6135
  if (code != 0) {
45,819✔
6136
    if (pBlob != NULL) {
×
6137
      taosMemoryFree(pBlob->data);
×
6138
      taosArrayDestroy(pBlob->pSeqTable);
×
6139
      taosMemoryFree(pBlob);
×
6140
    }
6141
  }
6142
  return code;
45,819✔
6143
}
6144

6145
#define CALC_SUM_MAX_MIN(SUM, MAX, MIN, VAL) \
6146
  do {                                       \
6147
    (SUM) += (VAL);                          \
6148
    if ((MAX) < (VAL)) (MAX) = (VAL);        \
6149
    if ((MIN) > (VAL)) (MIN) = (VAL);        \
6150
  } while (0)
6151

6152
static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, SColumnDataAgg *pAggs) {
8,145,848✔
6153
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,145,848✔
6154
  int16_t *numOfNull = &pAggs->numOfNull;
8,146,458✔
6155
  *sum = 0;
8,145,836✔
6156
  *max = 0;
8,145,226✔
6157
  *min = 1;
8,144,006✔
6158
  *numOfNull = 0;
8,143,396✔
6159

6160
  int8_t val;
6161
  if (HAS_VALUE == pColData->flag) {
8,141,542✔
6162
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6163
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
2,147,483,647✔
6164
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6165
    }
6166
  } else {
6167
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
600,150✔
6168
      switch (tColDataGetBitValue(pColData, iVal)) {
600,000✔
6169
        case 0:
29,980✔
6170
        case 1:
6171
          (*numOfNull)++;
29,980✔
6172
          break;
29,980✔
6173
        case 2:
570,020✔
6174
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
570,020✔
6175
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
570,020✔
6176
          break;
570,020✔
6177
        default:
×
6178
          break;
×
6179
      }
6180
    }
6181
  }
6182
}
8,084,828✔
6183

6184
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,156,595✔
6185
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,156,595✔
6186
  int16_t *numOfNull = &pAggs->numOfNull;
8,154,765✔
6187
  *sum = 0;
8,155,985✔
6188
  *max = INT8_MIN;
8,154,765✔
6189
  *min = INT8_MAX;
8,154,765✔
6190
  *numOfNull = 0;
8,154,155✔
6191

6192
  int8_t val;
6193
  if (HAS_VALUE == pColData->flag) {
8,155,375✔
6194
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6195
      val = ((int8_t *)pColData->pData)[iVal];
2,147,483,647✔
6196
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6197
    }
6198
  } else {
6199
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
600,150✔
6200
      switch (tColDataGetBitValue(pColData, iVal)) {
600,000✔
6201
        case 0:
29,940✔
6202
        case 1:
6203
          (*numOfNull)++;
29,940✔
6204
          break;
29,940✔
6205
        case 2:
570,060✔
6206
          val = ((int8_t *)pColData->pData)[iVal];
570,060✔
6207
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
570,060✔
6208
          break;
570,060✔
6209
        default:
×
6210
          break;
×
6211
      }
6212
    }
6213
  }
6214
}
8,098,448✔
6215

6216
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
8,147,282✔
6217
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
8,147,282✔
6218
  int16_t *numOfNull = &pAggs->numOfNull;
8,146,684✔
6219
  *sum = 0;
8,145,452✔
6220
  *max = INT16_MIN;
8,146,062✔
6221
  *min = INT16_MAX;
8,144,830✔
6222
  *numOfNull = 0;
8,143,610✔
6223

6224
  int16_t val;
6225
  if (HAS_VALUE == pColData->flag) {
8,142,390✔
6226
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6227
      val = ((int16_t *)pColData->pData)[iVal];
2,147,483,647✔
6228
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6229
    }
6230
  } else {
6231
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
600,150✔
6232
      switch (tColDataGetBitValue(pColData, iVal)) {
600,000✔
6233
        case 0:
29,951✔
6234
        case 1:
6235
          (*numOfNull)++;
29,951✔
6236
          break;
29,951✔
6237
        case 2:
570,049✔
6238
          val = ((int16_t *)pColData->pData)[iVal];
570,049✔
6239
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
570,049✔
6240
          break;
570,049✔
6241
        default:
×
6242
          break;
×
6243
      }
6244
    }
6245
  }
6246
}
8,017,442✔
6247

6248
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
26,830,143✔
6249
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
26,830,143✔
6250
  int16_t *numOfNull = &pAggs->numOfNull;
26,841,811✔
6251
  *sum = 0;
26,839,314✔
6252
  *max = INT32_MIN;
26,838,293✔
6253
  *min = INT32_MAX;
26,838,563✔
6254
  *numOfNull = 0;
26,837,549✔
6255

6256
  int32_t val;
6257
  if (HAS_VALUE == pColData->flag) {
26,827,440✔
6258
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6259
      val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6260
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6261
    }
6262
  } else {
6263
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6264
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6265
        case 0:
255,532,717✔
6266
        case 1:
6267
          (*numOfNull)++;
255,532,717✔
6268
          break;
255,475,648✔
6269
        case 2:
2,147,483,647✔
6270
          val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6271
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6272
          break;
2,147,483,647✔
6273
        default:
×
6274
          break;
×
6275
      }
6276
    }
6277
  }
6278
}
25,468,144✔
6279

6280
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
65,439,673✔
6281
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
65,439,673✔
6282
  int16_t *numOfNull = &pAggs->numOfNull;
65,439,945✔
6283
  *sum = 0;
65,439,323✔
6284
  *max = INT64_MIN;
65,439,323✔
6285
  *min = INT64_MAX;
65,438,103✔
6286
  *numOfNull = 0;
65,438,091✔
6287

6288
  int64_t val;
6289
  if (HAS_VALUE == pColData->flag) {
65,433,984✔
6290
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6291
      val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6292
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6293
    }
6294
  } else {
6295
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6296
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6297
        case 0:
282,914,763✔
6298
        case 1:
6299
          (*numOfNull)++;
282,914,763✔
6300
          break;
282,862,437✔
6301
        case 2:
2,147,483,647✔
6302
          val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6303
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6304
          break;
2,147,483,647✔
6305
        default:
×
6306
          break;
×
6307
      }
6308
    }
6309
  }
6310
}
391✔
6311

6312
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
28,971,931✔
6313
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
28,971,931✔
6314
  int16_t *numOfNull = &pAggs->numOfNull;
28,971,321✔
6315
  *(double *)sum = 0;
28,970,711✔
6316
  *(double *)max = -FLT_MAX;
28,970,101✔
6317
  *(double *)min = FLT_MAX;
28,970,101✔
6318
  *numOfNull = 0;
28,971,321✔
6319

6320
  float val;
6321
  if (HAS_VALUE == pColData->flag) {
28,971,333✔
6322
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6323
      val = ((float *)pColData->pData)[iVal];
2,147,483,647✔
6324
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6325
    }
6326
  } else {
6327
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
10,182,046✔
6328
      switch (tColDataGetBitValue(pColData, iVal)) {
10,167,211✔
6329
        case 0:
938,929✔
6330
        case 1:
6331
          (*numOfNull)++;
938,929✔
6332
          break;
938,929✔
6333
        case 2:
9,228,282✔
6334
          val = ((float *)pColData->pData)[iVal];
9,228,282✔
6335
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
9,228,282✔
6336
          break;
9,228,282✔
6337
        default:
×
6338
          break;
×
6339
      }
6340
    }
6341
  }
6342
}
25,845,092✔
6343

6344
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
15,014,584✔
6345
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
15,014,584✔
6346
  int16_t *numOfNull = &pAggs->numOfNull;
15,031,016✔
6347
  *(double *)sum = 0;
15,031,356✔
6348
  *(double *)max = -DBL_MAX;
15,029,508✔
6349
  *(double *)min = DBL_MAX;
15,029,514✔
6350
  *numOfNull = 0;
15,028,172✔
6351

6352
  double val;
6353
  if (HAS_VALUE == pColData->flag) {
15,025,162✔
6354
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6355
      val = ((double *)pColData->pData)[iVal];
2,147,483,647✔
6356
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6357
    }
6358
  } else {
6359
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
600,150✔
6360
      switch (tColDataGetBitValue(pColData, iVal)) {
600,000✔
6361
        case 0:
29,908✔
6362
        case 1:
6363
          (*numOfNull)++;
29,908✔
6364
          break;
29,908✔
6365
        case 2:
570,092✔
6366
          val = ((double *)pColData->pData)[iVal];
570,092✔
6367
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
570,092✔
6368
          break;
570,092✔
6369
        default:
×
6370
          break;
×
6371
      }
6372
    }
6373
  }
6374
}
13,709,972✔
6375

6376
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
7,814,172✔
6377
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
7,814,172✔
6378
  int16_t *numOfNull = &pAggs->numOfNull;
7,815,392✔
6379
  *(uint64_t *)sum = 0;
7,816,014✔
6380
  *(uint64_t *)max = 0;
7,816,014✔
6381
  *(uint64_t *)min = UINT8_MAX;
7,816,014✔
6382
  *numOfNull = 0;
7,814,782✔
6383

6384
  uint8_t val;
6385
  if (HAS_VALUE == pColData->flag) {
7,815,392✔
6386
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6387
      val = ((uint8_t *)pColData->pData)[iVal];
2,147,483,647✔
6388
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6389
    }
6390
  } else {
6391
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
600,150✔
6392
      switch (tColDataGetBitValue(pColData, iVal)) {
600,000✔
6393
        case 0:
30,027✔
6394
        case 1:
6395
          (*numOfNull)++;
30,027✔
6396
          break;
30,027✔
6397
        case 2:
569,973✔
6398
          val = ((uint8_t *)pColData->pData)[iVal];
569,973✔
6399
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
569,973✔
6400
          break;
569,973✔
6401
        default:
×
6402
          break;
×
6403
      }
6404
    }
6405
  }
6406
}
7,761,702✔
6407

6408
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
7,810,488✔
6409
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
7,810,488✔
6410
  int16_t *numOfNull = &pAggs->numOfNull;
7,812,330✔
6411
  *(uint64_t *)sum = 0;
7,808,658✔
6412
  *(uint64_t *)max = 0;
7,813,562✔
6413
  *(uint64_t *)min = UINT16_MAX;
7,811,732✔
6414
  *numOfNull = 0;
7,809,890✔
6415

6416
  uint16_t val;
6417
  if (HAS_VALUE == pColData->flag) {
7,805,620✔
6418
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6419
      val = ((uint16_t *)pColData->pData)[iVal];
2,147,483,647✔
6420
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6421
    }
6422
  } else {
6423
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
600,150✔
6424
      switch (tColDataGetBitValue(pColData, iVal)) {
600,000✔
6425
        case 0:
29,904✔
6426
        case 1:
6427
          (*numOfNull)++;
29,904✔
6428
          break;
29,904✔
6429
        case 2:
570,096✔
6430
          val = ((uint16_t *)pColData->pData)[iVal];
570,096✔
6431
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
570,096✔
6432
          break;
570,096✔
6433
        default:
×
6434
          break;
×
6435
      }
6436
    }
6437
  }
6438
}
7,721,786✔
6439

6440
static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, SColumnDataAgg *pAggs) {
7,816,624✔
6441
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
7,816,624✔
6442
  int16_t *numOfNull = &pAggs->numOfNull;
7,815,404✔
6443
  *(uint64_t *)sum = 0;
7,816,014✔
6444
  *(uint64_t *)max = 0;
7,816,002✔
6445
  *(uint64_t *)min = UINT32_MAX;
7,814,172✔
6446
  *numOfNull = 0;
7,814,172✔
6447

6448
  uint32_t val;
6449
  if (HAS_VALUE == pColData->flag) {
7,813,562✔
6450
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6451
      val = ((uint32_t *)pColData->pData)[iVal];
2,147,483,647✔
6452
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6453
    }
6454
  } else {
6455
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
600,150✔
6456
      switch (tColDataGetBitValue(pColData, iVal)) {
600,000✔
6457
        case 0:
30,125✔
6458
        case 1:
6459
          (*numOfNull)++;
30,125✔
6460
          break;
30,125✔
6461
        case 2:
569,875✔
6462
          val = ((uint32_t *)pColData->pData)[iVal];
569,875✔
6463
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
569,875✔
6464
          break;
569,875✔
6465
        default:
×
6466
          break;
×
6467
      }
6468
    }
6469
  }
6470
}
7,727,573✔
6471

6472
static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, SColumnDataAgg *pAggs) {
7,816,624✔
6473
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
7,816,624✔
6474
  int16_t *numOfNull = &pAggs->numOfNull;
7,816,624✔
6475
  *(uint64_t *)sum = 0;
7,816,014✔
6476
  *(uint64_t *)max = 0;
7,816,624✔
6477
  *(uint64_t *)min = UINT64_MAX;
7,816,624✔
6478
  *numOfNull = 0;
7,815,392✔
6479

6480
  uint64_t val;
6481
  if (HAS_VALUE == pColData->flag) {
7,814,782✔
6482
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6483
      val = ((uint64_t *)pColData->pData)[iVal];
2,147,483,647✔
6484
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6485
    }
6486
  } else {
6487
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
600,150✔
6488
      switch (tColDataGetBitValue(pColData, iVal)) {
600,000✔
6489
        case 0:
29,900✔
6490
        case 1:
6491
          (*numOfNull)++;
29,900✔
6492
          break;
29,900✔
6493
        case 2:
570,100✔
6494
          val = ((uint64_t *)pColData->pData)[iVal];
570,100✔
6495
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
570,100✔
6496
          break;
570,100✔
6497
        default:
×
6498
          break;
×
6499
      }
6500
    }
6501
  }
6502
}
7,683,733✔
6503

6504
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
29,679,176✔
6505
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
29,679,176✔
6506
  int16_t *numOfNull = &pAggs->numOfNull;
29,679,242✔
6507
  *(uint64_t *)sum = 0;
29,678,022✔
6508
  *(uint64_t *)max = 0;
29,677,478✔
6509
  *(uint64_t *)min = 0;
29,678,632✔
6510
  *numOfNull = 0;
29,678,022✔
6511

6512
  switch (pColData->flag) {
29,675,095✔
6513
    case HAS_NONE:
×
6514
    case HAS_NULL:
6515
    case (HAS_NONE | HAS_NULL):
6516
      *numOfNull = pColData->nVal;
×
6517
      break;
×
6518
    case HAS_VALUE:
29,668,166✔
6519
      *numOfNull = 0;
29,668,166✔
6520
      break;
29,668,698✔
6521
    case (HAS_VALUE | HAS_NULL):
2,656✔
6522
    case (HAS_VALUE | HAS_NONE):
6523
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
8,270,656✔
6524
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
8,268,000✔
6525
          (*numOfNull)++;
201,126✔
6526
        }
6527
      }
6528
      break;
2,656✔
6529
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6530
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6531
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6532
          (*numOfNull)++;
×
6533
        }
6534
      }
6535
      break;
×
6536
    default:
×
6537
      break;
×
6538
  }
6539
}
29,671,354✔
6540

6541
#define CALC_DECIMAL_SUM_MAX_MIN(TYPE, pSumOp, pCompOp, pColData, pSum, pMax, pMin)                   \
6542
  do {                                                                                                \
6543
    if (decimal128AddCheckOverflow((Decimal *)pSum, pVal, DECIMAL_WORD_NUM(TYPE))) *pOverflow = true; \
6544
    pSumOp->add(pSum, pVal, DECIMAL_WORD_NUM(TYPE));                                                  \
6545
    if (pCompOp->gt(pVal, pMax, DECIMAL_WORD_NUM(TYPE))) {                                            \
6546
      *(pMax) = *pVal;                                                                                \
6547
    }                                                                                                 \
6548
    if (pCompOp->lt(pVal, pMin, DECIMAL_WORD_NUM(TYPE))) {                                            \
6549
      *(pMin) = *pVal;                                                                                \
6550
    }                                                                                                 \
6551
  } while (0)
6552

6553
static FORCE_INLINE void tColDataCalcSMADecimal64Type(SColData *pColData, SColumnDataAgg *pAggs) {
15,185✔
6554
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum;
15,185✔
6555
  Decimal64  *pMax = (Decimal64 *)pAggs->decimal128Max, *pMin = (Decimal64 *)pAggs->decimal128Min;
15,185✔
6556
  uint8_t    *pOverflow = &pAggs->overflow;
15,185✔
6557
  *pSum = DECIMAL128_ZERO;
15,185✔
6558
  *pMax = DECIMAL64_MIN;
15,185✔
6559
  *pMin = DECIMAL64_MAX;
15,185✔
6560
  pAggs->numOfNull = 0;
15,185✔
6561
  pAggs->colId |= DECIMAL_AGG_FLAG;
15,185✔
6562

6563
  Decimal64         *pVal = NULL;
15,185✔
6564
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
15,185✔
6565
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
15,185✔
6566
  if (HAS_VALUE == pColData->flag) {
15,185✔
6567
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
153,520✔
6568
      pVal = ((Decimal64 *)pColData->pData) + iVal;
152,000✔
6569
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
152,000✔
6570
    }
6571
  } else {
6572
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
10,942,665✔
6573
      switch (tColDataGetBitValue(pColData, iVal)) {
10,929,000✔
6574
        case 0:
176,770✔
6575
        case 1:
6576
          pAggs->numOfNull++;
176,770✔
6577
          break;
176,770✔
6578
        case 2:
10,752,230✔
6579
          pVal = ((Decimal64 *)pColData->pData) + iVal;
10,752,230✔
6580
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
10,752,230✔
6581
          break;
10,752,230✔
6582
        default:
×
6583
          break;
×
6584
      }
6585
    }
6586
  }
6587
}
15,185✔
6588

6589
static FORCE_INLINE void tColDataCalcSMADecimal128Type(SColData *pColData, SColumnDataAgg *pAggs) {
39,465✔
6590
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum, *pMax = (Decimal128 *)pAggs->decimal128Max,
39,465✔
6591
             *pMin = (Decimal128 *)pAggs->decimal128Min;
39,465✔
6592
  uint8_t *pOverflow = &pAggs->overflow;
39,465✔
6593
  *pSum = DECIMAL128_ZERO;
39,465✔
6594
  *pMax = DECIMAL128_MIN;
39,465✔
6595
  *pMin = DECIMAL128_MAX;
39,465✔
6596
  pAggs->numOfNull = 0;
39,465✔
6597
  pAggs->colId |= DECIMAL_AGG_FLAG;
39,465✔
6598

6599
  Decimal128        *pVal = NULL;
39,465✔
6600
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
39,465✔
6601
  if (HAS_VALUE == pColData->flag) {
39,465✔
6602
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
307,040✔
6603
      pVal = ((Decimal128 *)pColData->pData) + iVal;
304,000✔
6604
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
304,000✔
6605
    }
6606
  } else {
6607
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
35,093,425✔
6608
      switch (tColDataGetBitValue(pColData, iVal)) {
35,057,000✔
6609
        case 0:
567,620✔
6610
        case 1:
6611
          pAggs->numOfNull++;
567,620✔
6612
          break;
567,620✔
6613
        case 2:
34,489,380✔
6614
          pVal = ((Decimal128 *)pColData->pData) + iVal;
34,489,380✔
6615
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
34,489,380✔
6616
          break;
34,489,380✔
6617
        default:
×
6618
          break;
×
6619
      }
6620
    }
6621
  }
6622
}
39,465✔
6623

6624
void (*tColDataCalcSMA[])(SColData *pColData, SColumnDataAgg *pAggs) = {
6625
    NULL,
6626
    tColDataCalcSMABool,            // TSDB_DATA_TYPE_BOOL
6627
    tColDataCalcSMATinyInt,         // TSDB_DATA_TYPE_TINYINT
6628
    tColDataCalcSMATinySmallInt,    // TSDB_DATA_TYPE_SMALLINT
6629
    tColDataCalcSMAInt,             // TSDB_DATA_TYPE_INT
6630
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_BIGINT
6631
    tColDataCalcSMAFloat,           // TSDB_DATA_TYPE_FLOAT
6632
    tColDataCalcSMADouble,          // TSDB_DATA_TYPE_DOUBLE
6633
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARCHAR
6634
    tColDataCalcSMABigInt,          // TSDB_DATA_TYPE_TIMESTAMP
6635
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_NCHAR
6636
    tColDataCalcSMAUTinyInt,        // TSDB_DATA_TYPE_UTINYINT
6637
    tColDataCalcSMATinyUSmallInt,   // TSDB_DATA_TYPE_USMALLINT
6638
    tColDataCalcSMAUInt,            // TSDB_DATA_TYPE_UINT
6639
    tColDataCalcSMAUBigInt,         // TSDB_DATA_TYPE_UBIGINT
6640
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_JSON
6641
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_VARBINARY
6642
    tColDataCalcSMADecimal128Type,  // TSDB_DATA_TYPE_DECIMAL
6643
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_BLOB
6644
    NULL,                           // TSDB_DATA_TYPE_MEDIUMBLOB
6645
    tColDataCalcSMAVarType,         // TSDB_DATA_TYPE_GEOMETRY
6646
    tColDataCalcSMADecimal64Type,   // TSDB_DATA_TYPE_DECIMAL64
6647
};
6648

6649
// SValueColumn ================================
6650
int32_t tValueColumnInit(SValueColumn *valCol) {
1,000,503,970✔
6651
  valCol->type = TSDB_DATA_TYPE_NULL;
1,000,503,970✔
6652
  valCol->numOfValues = 0;
1,000,731,592✔
6653
  tBufferInit(&valCol->data);
1,000,878,474✔
6654
  tBufferInit(&valCol->offsets);
1,000,985,227✔
6655
  return 0;
1,001,322,152✔
6656
}
6657

6658
void tValueColumnDestroy(SValueColumn *valCol) {
2,147,483,647✔
6659
  valCol->type = TSDB_DATA_TYPE_NULL;
2,147,483,647✔
6660
  valCol->numOfValues = 0;
2,147,483,647✔
6661
  tBufferDestroy(&valCol->data);
2,147,483,647✔
6662
  tBufferDestroy(&valCol->offsets);
2,147,483,647✔
6663
  return;
2,147,483,647✔
6664
}
6665

6666
void tValueColumnClear(SValueColumn *valCol) {
1,379,657,081✔
6667
  valCol->type = TSDB_DATA_TYPE_NULL;
1,379,657,081✔
6668
  valCol->numOfValues = 0;
1,379,865,212✔
6669
  tBufferClear(&valCol->data);
1,379,889,889✔
6670
  tBufferClear(&valCol->offsets);
1,379,659,444✔
6671
  return;
1,379,736,806✔
6672
}
6673

6674
int32_t tValueColumnAppend(SValueColumn *valCol, const SValue *value) {
1,759,491✔
6675
  int32_t code;
6676

6677
  if (valCol->numOfValues == 0) {
1,759,491✔
6678
    valCol->type = value->type;
549,576✔
6679
  }
6680

6681
  if (!(value->type == valCol->type)) {
1,758,858✔
6682
    return TSDB_CODE_INVALID_PARA;
×
6683
  }
6684

6685
  if (IS_VAR_DATA_TYPE(value->type)) {
1,760,202✔
6686
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
1,500,645✔
6687
      return code;
×
6688
    }
6689
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
1,502,648✔
6690
      return code;
×
6691
    }
6692
  } else {
6693
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
1,008,440✔
6694
    if (code) return code;
1,007,550✔
6695
  }
6696
  valCol->numOfValues++;
1,758,874✔
6697

6698
  return 0;
1,759,507✔
6699
}
6700

6701
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
55,542,083✔
6702
  int32_t code;
6703

6704
  if (idx < 0 || idx >= valCol->numOfValues) {
55,542,083✔
6705
    return TSDB_CODE_OUT_OF_RANGE;
×
6706
  }
6707

6708
  if (IS_VAR_DATA_TYPE(valCol->type)) {
55,542,135✔
6709
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
1,784,138✔
6710
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
1,786,405✔
6711
    int32_t  oldDataSize = nextOffset - offsets[idx];
1,787,074✔
6712
    int32_t  bytesAdded = value->nData - oldDataSize;
1,787,074✔
6713

6714
    if (bytesAdded != 0) {
1,787,074✔
6715
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
39,732✔
6716
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
19,866✔
6717
              tBufferGetSize(&valCol->data) - nextOffset);
19,866✔
6718
      valCol->data.size += bytesAdded;
19,866✔
6719

6720
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
19,866✔
6721
        offsets[i] += bytesAdded;
×
6722
      }
6723
    }
6724
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
1,787,074✔
6725
  } else {
6726
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
53,757,390✔
6727
                        tDataTypes[valCol->type].bytes);
53,758,231✔
6728
  }
6729
  return 0;
6730
}
6731

6732
int32_t tValueColumnGet(SValueColumn *valCol, int32_t idx, SValue *value) {
361,110,276✔
6733
  if (idx < 0 || idx >= valCol->numOfValues) {
361,110,276✔
6734
    return TSDB_CODE_OUT_OF_RANGE;
×
6735
  }
6736

6737
  value->type = valCol->type;
361,122,478✔
6738
  if (IS_VAR_DATA_TYPE(value->type)) {
468,620,654✔
6739
    int32_t       offset, nextOffset;
107,499,575✔
6740
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
107,497,524✔
6741

6742
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
107,496,865✔
6743
    if (idx == valCol->numOfValues - 1) {
107,493,570✔
6744
      nextOffset = tBufferGetSize(&valCol->data);
14,528,790✔
6745
    } else {
6746
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
92,967,416✔
6747
    }
6748
    value->nData = nextOffset - offset;
107,494,229✔
6749
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
107,494,229✔
6750
  } else {
6751
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
253,627,165✔
6752
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
507,256,171✔
6753
  }
6754
  return 0;
361,123,641✔
6755
}
6756

6757
int32_t tValueColumnCompress(SValueColumn *valCol, SValueColumnCompressInfo *info, SBuffer *output, SBuffer *assist) {
548,159✔
6758
  int32_t code;
6759

6760
  if (!(valCol->numOfValues > 0)) {
548,159✔
6761
    return TSDB_CODE_INVALID_PARA;
×
6762
  }
6763

6764
  (*info) = (SValueColumnCompressInfo){
549,210✔
6765
      .cmprAlg = info->cmprAlg,
548,551✔
6766
      .type = valCol->type,
548,551✔
6767
  };
6768

6769
  // offset
6770
  if (IS_VAR_DATA_TYPE(valCol->type)) {
549,602✔
6771
    SCompressInfo cinfo = {
192,025✔
6772
        .cmprAlg = info->cmprAlg,
191,758✔
6773
        .dataType = TSDB_DATA_TYPE_INT,
6774
        .originalSize = valCol->offsets.size,
191,758✔
6775
    };
6776

6777
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
191,758✔
6778
    if (code) return code;
191,758✔
6779

6780
    info->offsetOriginalSize = cinfo.originalSize;
191,758✔
6781
    info->offsetCompressedSize = cinfo.compressedSize;
191,758✔
6782
  }
6783

6784
  // data
6785
  SCompressInfo cinfo = {
549,602✔
6786
      .cmprAlg = info->cmprAlg,
1,098,384✔
6787
      .dataType = valCol->type,
549,602✔
6788
      .originalSize = valCol->data.size,
549,210✔
6789
  };
6790

6791
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
549,602✔
6792
  if (code) return code;
549,602✔
6793

6794
  info->dataOriginalSize = cinfo.originalSize;
549,602✔
6795
  info->dataCompressedSize = cinfo.compressedSize;
549,602✔
6796

6797
  return 0;
549,602✔
6798
}
6799

6800
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
104,223,359✔
6801
                               SBuffer *assist) {
6802
  int32_t code;
6803

6804
  tValueColumnClear(valCol);
104,223,359✔
6805
  valCol->type = info->type;
104,223,359✔
6806
  // offset
6807
  if (IS_VAR_DATA_TYPE(valCol->type)) {
155,977,260✔
6808
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
51,755,878✔
6809

6810
    SCompressInfo cinfo = {
51,747,970✔
6811
        .dataType = TSDB_DATA_TYPE_INT,
6812
        .cmprAlg = info->cmprAlg,
51,747,311✔
6813
        .originalSize = info->offsetOriginalSize,
51,746,652✔
6814
        .compressedSize = info->offsetCompressedSize,
51,745,334✔
6815
    };
6816

6817
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
51,746,652✔
6818
    if (code) {
51,753,242✔
6819
      return code;
×
6820
    }
6821
  } else {
6822
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
52,472,094✔
6823
  }
6824

6825
  // data
6826
  SCompressInfo cinfo = {
104,229,290✔
6827
      .dataType = valCol->type,
104,227,313✔
6828
      .cmprAlg = info->cmprAlg,
104,229,949✔
6829
      .originalSize = info->dataOriginalSize,
104,229,290✔
6830
      .compressedSize = info->dataCompressedSize,
104,227,972✔
6831
  };
6832

6833
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
104,225,336✔
6834
  if (code) {
104,234,562✔
6835
    return code;
×
6836
  }
6837

6838
  return 0;
104,234,562✔
6839
}
6840

6841
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *info, SBuffer *buffer) {
549,602✔
6842
  int32_t code;
6843
  uint8_t fmtVer = 0;
549,602✔
6844

6845
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
1,099,204✔
6846
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
1,099,204✔
6847
  if ((code = tBufferPutI8(buffer, info->type))) return code;
1,099,204✔
6848
  if (IS_VAR_DATA_TYPE(info->type)) {
549,602✔
6849
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
382,198✔
6850
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
383,516✔
6851
  }
6852
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
1,098,153✔
6853
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
1,097,494✔
6854

6855
  return 0;
548,943✔
6856
}
6857

6858
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
104,225,336✔
6859
  int32_t code;
6860
  uint8_t fmtVer;
104,225,336✔
6861

6862
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
104,225,995✔
6863
  if (fmtVer == 0) {
104,224,018✔
6864
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
104,224,018✔
6865
    if ((code = tBufferGetI8(reader, &info->type))) return code;
104,220,723✔
6866
    if (IS_VAR_DATA_TYPE(info->type)) {
104,228,631✔
6867
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
51,753,901✔
6868
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
51,744,016✔
6869
    } else {
6870
      info->offsetOriginalSize = 0;
52,472,094✔
6871
      info->offsetCompressedSize = 0;
52,476,048✔
6872
    }
6873
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
104,225,336✔
6874
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
104,227,313✔
6875
  } else {
6876
    return TSDB_CODE_INVALID_PARA;
×
6877
  }
6878

6879
  return 0;
104,230,608✔
6880
}
6881

6882
int32_t tCompressData(void          *input,       // input
901,246,485✔
6883
                      SCompressInfo *info,        // compress info
6884
                      void          *output,      // output
6885
                      int32_t        outputSize,  // output size
6886
                      SBuffer       *buffer       // assistant buffer provided by caller, can be NULL
6887
) {
6888
  int32_t extraSizeNeeded;
6889
  int32_t code;
6890

6891
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
901,246,485✔
6892
  if (!(outputSize >= extraSizeNeeded)) {
901,376,164✔
6893
    return TSDB_CODE_INVALID_PARA;
×
6894
  }
6895

6896
  if (info->cmprAlg == NO_COMPRESSION) {
901,376,164✔
6897
    (void)memcpy(output, input, info->originalSize);
29,604✔
6898
    info->compressedSize = info->originalSize;
29,604✔
6899
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
1,012,873,635✔
6900
    SBuffer local;
111,488,936✔
6901

6902
    tBufferInit(&local);
6903
    if (buffer == NULL) {
111,563,574✔
6904
      buffer = &local;
×
6905
    }
6906

6907
    if (info->cmprAlg == TWO_STAGE_COMP) {
111,563,574✔
6908
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
111,452,096✔
6909
      if (code) {
111,435,422✔
6910
        tBufferDestroy(&local);
6911
        return code;
×
6912
      }
6913
    }
6914

6915
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
334,662,686✔
6916
        input,                                                   // input
6917
        info->originalSize,                                      // input size
6918
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
111,558,465✔
6919
        output,                                                  // output
6920
        outputSize,                                              // output size
6921
        info->cmprAlg,                                           // compression algorithm
111,564,461✔
6922
        buffer->data,                                            // buffer
6923
        buffer->capacity                                         // buffer size
111,550,898✔
6924
    );
6925
    if (info->compressedSize < 0) {
111,575,200✔
6926
      tBufferDestroy(&local);
6927
      return TSDB_CODE_COMPRESS_ERROR;
×
6928
    }
6929

6930
    tBufferDestroy(&local);
6931
  } else {
6932
    DEFINE_VAR(info->cmprAlg)
789,822,753✔
6933
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
789,892,571✔
6934
      (void)memcpy(output, input, info->originalSize);
6,792✔
6935
      info->compressedSize = info->originalSize;
6,792✔
6936
      return 0;
6,792✔
6937
    }
6938
    SBuffer local;
789,877,231✔
6939

6940
    tBufferInit(&local);
6941
    if (buffer == NULL) {
789,832,754✔
6942
      buffer = &local;
×
6943
    }
6944
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
789,832,754✔
6945

6946
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
2,147,483,647✔
6947
        input,                                                      // input
6948
        info->originalSize,                                         // input size
6949
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
789,788,104✔
6950
        output,                                                     // output
6951
        outputSize,                                                 // output size
6952
        info->cmprAlg,                                              // compression algorithm
6953
        buffer->data,                                               // buffer
6954
        buffer->capacity                                            // buffer size
789,740,962✔
6955
    );
6956
    if (info->compressedSize < 0) {
789,871,717✔
6957
      tBufferDestroy(&local);
6958
      return TSDB_CODE_COMPRESS_ERROR;
×
6959
    }
6960

6961
    tBufferDestroy(&local);
6962
    // new col compress
6963
  }
6964

6965
  return 0;
901,313,631✔
6966
}
6967

6968
int32_t tDecompressData(void                *input,       // input
2,147,483,647✔
6969
                        const SCompressInfo *info,        // compress info
6970
                        void                *output,      // output
6971
                        int32_t              outputSize,  // output size
6972
                        SBuffer             *buffer       // assistant buffer provided by caller, can be NULL
6973
) {
6974
  int32_t code;
6975

6976
  if (!(outputSize >= info->originalSize)) {
2,147,483,647✔
6977
    return TSDB_CODE_INVALID_PARA;
×
6978
  }
6979

6980
  if (info->cmprAlg == NO_COMPRESSION) {
2,147,483,647✔
6981
    if (!(info->compressedSize == info->originalSize)) {
19,620✔
6982
      return TSDB_CODE_INVALID_PARA;
×
6983
    }
6984
    (void)memcpy(output, input, info->compressedSize);
19,620✔
6985
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
6986
    SBuffer local;
1,807,039,341✔
6987

6988
    tBufferInit(&local);
6989
    if (buffer == NULL) {
1,807,368,785✔
6990
      buffer = &local;
×
6991
    }
6992

6993
    if (info->cmprAlg == TWO_STAGE_COMP) {
1,807,368,785✔
6994
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,806,831,756✔
6995
      if (code) {
1,806,655,329✔
6996
        tBufferDestroy(&local);
6997
        return code;
×
6998
      }
6999
    }
7000

7001
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
2,147,483,647✔
7002
        input,                                                  // input
7003
        info->compressedSize,                                   // inputSize
1,807,448,562✔
7004
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,807,679,746✔
7005
        output,                                                 // output
7006
        outputSize,                                             // output size
7007
        info->cmprAlg,                                          // compression algorithm
1,807,720,685✔
7008
        buffer->data,                                           // helper buffer
7009
        buffer->capacity                                        // extra buffer size
1,807,376,290✔
7010
    );
7011
    if (decompressedSize < 0) {
1,806,523,947✔
7012
      tBufferDestroy(&local);
7013
      return TSDB_CODE_COMPRESS_ERROR;
×
7014
    }
7015

7016
    if (!(decompressedSize == info->originalSize)) {
1,806,523,947✔
7017
      return TSDB_CODE_COMPRESS_ERROR;
×
7018
    }
7019
    tBufferDestroy(&local);
7020
  } else {
7021
    DEFINE_VAR(info->cmprAlg);
1,932,263,032✔
7022
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
1,932,206,949✔
7023
      (void)memcpy(output, input, info->compressedSize);
13,584✔
7024
      return 0;
13,584✔
7025
    }
7026
    SBuffer local;
1,932,187,779✔
7027

7028
    tBufferInit(&local);
7029
    if (buffer == NULL) {
1,932,045,140✔
7030
      buffer = &local;
×
7031
    }
7032
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,932,045,140✔
7033
    if (code) {
1,931,745,054✔
7034
      return code;
×
7035
    }
7036

7037
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
2,147,483,647✔
7038
        input,                                                  // input
7039
        info->compressedSize,                                   // inputSize
1,932,492,432✔
7040
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,932,273,560✔
7041
        output,                                                 // output
7042
        outputSize,                                             // output size
7043
        info->cmprAlg,                                          // compression algorithm
1,932,237,896✔
7044
        buffer->data,                                           // helper buffer
7045
        buffer->capacity                                        // extra buffer size
1,932,189,514✔
7046
    );
7047
    if (decompressedSize < 0) {
1,931,269,115✔
7048
      tBufferDestroy(&local);
7049
      return TSDB_CODE_COMPRESS_ERROR;
×
7050
    }
7051

7052
    if (!(decompressedSize == info->originalSize)) {
1,931,269,115✔
7053
      return TSDB_CODE_COMPRESS_ERROR;
×
7054
    }
7055
    tBufferDestroy(&local);
7056
  }
7057

7058
  return 0;
2,147,483,647✔
7059
}
7060

7061
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
901,186,004✔
7062
  int32_t code;
7063

7064
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
901,186,004✔
7065
  if (code) return code;
901,170,643✔
7066

7067
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
901,170,643✔
7068
  if (code) return code;
901,236,208✔
7069

7070
  output->size += info->compressedSize;
901,236,208✔
7071
  return 0;
901,305,569✔
7072
}
7073

7074
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,806,756,605✔
7075
  int32_t code;
7076

7077
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
1,806,756,605✔
7078
  if (code) return code;
1,805,458,291✔
7079

7080
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,805,458,291✔
7081
  if (code) return code;
1,806,653,432✔
7082

7083
  output->size += info->originalSize;
1,806,653,432✔
7084
  return 0;
1,806,630,743✔
7085
}
7086

7087
// handle all types, including var data
7088
void valueSetDatum(SValue *pVal, int8_t type, void *pDatum, uint32_t len) {
2,147,483,647✔
7089
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647✔
7090
    pVal->pData = pDatum;
210,152,460✔
7091
    pVal->nData = len;
1,085,827,407✔
7092
  } else {
7093
    switch (len) {
2,147,483,647✔
7094
      case sizeof(uint8_t):
2,147,483,647✔
7095
        pVal->val = *(uint8_t *)pDatum;
2,147,483,647✔
7096
        break;
2,147,483,647✔
7097
      case sizeof(uint16_t):
2,147,483,647✔
7098
        pVal->val = *(uint16_t *)pDatum;
2,147,483,647✔
7099
        break;
2,147,483,647✔
7100
      case sizeof(uint32_t):
2,147,483,647✔
7101
        pVal->val = *(uint32_t *)pDatum;
2,147,483,647✔
7102
        break;
2,147,483,647✔
7103
      case sizeof(uint64_t):
2,147,483,647✔
7104
        pVal->val = *(uint64_t *)pDatum;
2,147,483,647✔
7105
        break;
2,147,483,647✔
7106
      default:
2,155✔
7107
        break;
2,155✔
7108
    }
7109
  }
7110
}
2,147,483,647✔
7111

7112
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
1,693,007,810✔
7113
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
1,693,007,810✔
7114
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
202,078,136✔
7115
    pDst->nData = pSrc->nData;
202,079,749✔
7116
  } else {
7117
    pDst->val = pSrc->val;
1,490,929,674✔
7118
  }
7119
}
1,693,016,258✔
7120
void valueClearDatum(SValue *pVal, int8_t type) {
×
7121
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
×
7122
    taosMemoryFreeClear(pVal->pData);
×
7123
    pVal->nData = 0;
×
7124
  } else {
7125
    pVal->val = 0;
×
7126
  }
7127
}
×
7128

7129
int8_t schemaHasBlob(const STSchema *pSchema) {
508,591,810✔
7130
  if (pSchema == NULL) {
508,591,810✔
7131
    return 0;
×
7132
  }
7133
  for (int i = 0; i < pSchema->numOfCols; ++i) {
2,147,483,647✔
7134
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
2,147,483,647✔
7135
      return 1;
55,347✔
7136
    }
7137
  }
7138
  return 0;
508,556,749✔
7139
}
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