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

taosdata / TDengine / #4869

26 Nov 2025 05:46AM UTC coverage: 64.539% (-0.09%) from 64.629%
#4869

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

771 of 945 new or added lines in 33 files covered. (81.59%)

3214 existing lines in 124 files now uncovered.

158203 of 245129 relevant lines covered (64.54%)

113224023.06 hits per line

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

63.42
/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) {
211,449,048✔
79
  int32_t n = 0;
211,449,048✔
80
  n += tPutI8(p ? p + n : p, index->type);
211,449,048✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
211,478,679✔
82
  return n;
211,480,920✔
83
}
84

85
static int32_t tGetPrimaryKeyIndex(uint8_t *p, SPrimaryKeyIndex *index) {
1,036,431,618✔
86
  int32_t n = 0;
1,036,431,618✔
87
  n += tGetI8(p + n, &index->type);
1,036,431,618✔
88
  n += tGetU32v(p + n, &index->offset);
1,036,558,637✔
89
  return n;
1,036,890,272✔
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;
70,493,806✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
70,492,063✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
70,495,051✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
70,497,790✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
70,482,601✔
115
    sinfo->numOfPKs++;
70,474,882✔
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
56,088✔
122
                             + BSE_SEQUECE_SIZE;                     // value
28,044✔
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
56,088✔
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
28,044✔
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
56,088✔
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;
95,456✔
164
        break;
47,728✔
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) {
948✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
1,896✔
185
        break;
948✔
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,698,377,241✔
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):
4,532,744✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
4,532,744✔
216
      sinfo->tupleFixedSize = 0;
4,532,905✔
217
      break;
4,532,583✔
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,023,796,194✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,023,796,194✔
225
      sinfo->tupleFixedSize = schema->flen;
1,023,799,414✔
226
      break;
809,444,028✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
70,475,380✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
70,495,549✔
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) {
198,057,937✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
201,664,776✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
201,663,376✔
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;
70,477,123✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
70,497,292✔
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);
65,937,490✔
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,016✔
296
        break;
14,016✔
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
720,301,055✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
662,737,820✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
65,557,794✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
65,571,438✔
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
3,272,562✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
948✔
325
        break;
948✔
326
      } else {
327
        colValIndex++;
3,271,614✔
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) {
43,624✔
336
  SBlobItem item = {.type = type, .len = 0};
43,624✔
337
  uint64_t  seq = 0;
43,624✔
338
  int32_t   code = tBlobSetPush(pBlobSet, &item, &seq, 0);
43,624✔
339
  if (code != 0) return code;
43,624✔
340

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

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

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

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

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

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

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

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

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

453
  return 0;
×
454
}
455

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

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

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

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

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

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

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

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

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

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

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

563
  return 0;
×
564
}
565
static FORCE_INLINE void tRowBuildKVRowSetIndex(uint8_t flag, SKVIdx *indices, uint32_t offset) {
566
  if (flag & KV_FLG_LIT) {
2,147,483,647✔
567
    ((uint8_t *)indices->idx)[indices->nCol] = (uint8_t)offset;
2,147,483,647✔
568
  } else if (flag & KV_FLG_MID) {
357,894,800✔
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
357,894,800✔
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);
4,552,083✔
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;
33,712✔
609
      }
610

611
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
612
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
2,147,483,647✔
613
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
2,147,483,647✔
614
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647✔
615
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
1,075,060,511✔
616
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
1,073,608,624✔
617
            if (colValArray[colValIndex].value.nData > 0) {
1,073,618,353✔
618
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
1,049,236,174✔
619
                           colValArray[colValIndex].value.nData);
1,049,258,586✔
620
              payloadSize += colValArray[colValIndex].value.nData;
1,049,255,193✔
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);
1,770,874,027✔
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
×
637
        break;
×
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,
28,044✔
647
                                      SRow **ppRow, SBlobSet *ppBlobSet) {
648
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
28,044✔
649

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

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

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

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

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

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

688
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
28,044✔
689
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
28,044✔
690
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
28,044✔
691
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
28,044✔
692
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
28,044✔
693
              hasBlob = 1;
28,044✔
694
            }
695
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
28,044✔
696
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
28,044✔
697
            if (colValArray[colValIndex].value.nData > 0) {
28,044✔
698
              if (hasBlob == 0) {
21,812✔
699
                (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
×
700
                             colValArray[colValIndex].value.nData);
×
701
                payloadSize += colValArray[colValIndex].value.nData;
×
702
              } else {
703
                uint64_t  seq = 0;
21,812✔
704
                uint32_t  seqOffset = payloadSize + payload - (*ppRow)->data;
21,812✔
705
                SBlobItem item = {.seqOffsetInRow = seqOffset,
21,812✔
706
                                  .data = colValArray[colValIndex].value.pData,
21,812✔
707
                                  .len = colValArray[colValIndex].value.nData,
21,812✔
708
                                  .type = TSDB_DATA_BLOB_VALUE};
709
                int32_t   code = tBlobSetPush(ppBlobSet, &item, &seq, 0);
21,812✔
710
                if (code != 0) return code;
21,812✔
711
                payloadSize += tPutU64(payload + payloadSize, seq);
43,624✔
712
              }
713
            } else {
714
              if (hasBlob) {
6,232✔
715
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
6,232✔
716
              }
717
            }
718
          } else {
719
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
×
720
            (void)memcpy(payload + payloadSize, &colValArray[colValIndex].value.val,
×
721
                         tDataTypes[schema->columns[i].type].bytes);
×
722
            payloadSize += tDataTypes[schema->columns[i].type].bytes;
×
723
          }
724
        } else if (COL_VAL_IS_NULL(&colValArray[colValIndex])) {  // NULL
×
725
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
×
726
          payloadSize += tPutI16v(payload + payloadSize, -schema->columns[i].colId);
×
727
          if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
×
728
            TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL));
×
729
          }
730
        }
731
        colValIndex++;
28,044✔
732
        break;
28,044✔
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) {
28,044✔
742
    return TSDB_CODE_INVALID_PARA;
×
743
  }
744
  return 0;
28,044✔
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,
49,856✔
1074
                          SRowBuildScanInfo *sinfo) {
1075
  int32_t code;
1076

1077
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
49,856✔
1078
  if (code) return code;
49,856✔
1079

1080
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
49,856✔
1081
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
21,812✔
1082
  } else {
1083
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
28,044✔
1084
  }
1085

1086
  return code;
49,856✔
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) {
37,392✔
1106
  int32_t    lino = 0;
37,392✔
1107
  int32_t    code = 0;
37,392✔
1108
  SBlobSet  *p = taosMemCalloc(1, sizeof(SBlobSet));
37,392✔
1109
  if (p == NULL) {
37,392✔
1110
    return terrno;
×
1111
  }
1112

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

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

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

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

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

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

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

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

1183
  offset = pBlobSet->len;
65,436✔
1184
  pBlobSet->len += len;
65,436✔
1185

1186
  pBlobSet->seq++;
65,436✔
1187
  *seq = pBlobSet->seq;
65,436✔
1188

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

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

1200
_exit:
65,436✔
1201
  return code;
65,436✔
1202
}
1203

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

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

1209
  memcpy(p1, p2, sizeof(SBlobSet));
1,558✔
1210
  memcpy(p2, &t, sizeof(SBlobSet));
1,558✔
1211
}
1,558✔
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,061,635,377✔
1261
  if (pBlobSet == NULL) return 0;
1,061,635,377✔
1262
  return taosArrayGetSize(pBlobSet->pSeqTable);
135,278✔
1263
}
1264

1265
void tBlobSetDestroy(SBlobSet *pBlobSet) {
1,182,502✔
1266
  if (pBlobSet == NULL) return;
1,182,502✔
1267
  uTrace("destroy blob row, seqTable size %p", pBlobSet);
71,234✔
1268
  taosMemoryFree(pBlobSet->data);
71,234✔
1269
  taosArrayDestroy(pBlobSet->pSeqTable);
73,226✔
1270
  taosHashCleanup(pBlobSet->pSeqToffset);
73,226✔
1271
  taosArrayDestroy(pBlobSet->pSet);
73,226✔
1272
  taosMemoryFree(pBlobSet);
73,226✔
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) {
×
1286
  if (((SBindInfo *)p1)->columnId < ((SBindInfo *)p2)->columnId) {
×
1287
    return -1;
×
1288
  } else if (((SBindInfo *)p1)->columnId > ((SBindInfo *)p2)->columnId) {
×
1289
    return 1;
×
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,
12,119,261✔
1304
                          SArray *rowArray, bool *pOrdered, bool *pDupTs) {
1305
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
12,119,261✔
1306
    return TSDB_CODE_INVALID_PARA;
84✔
1307
  }
1308

1309
  if (!infoSorted) {
12,125,072✔
1310
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1311
  }
1312

1313
  int32_t code = 0;
12,125,072✔
1314
  int32_t numOfRows = infos[0].bind->num;
12,125,072✔
1315
  SArray *colValArray;
1316
  SColVal colVal;
12,083,521✔
1317

1318
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
12,124,064✔
1319
    return terrno;
×
1320
  }
1321

1322
  SRowKey rowKey, lastRowKey;
12,077,245✔
1323
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
2,147,483,647✔
1324
    taosArrayClear(colValArray);
2,147,483,647✔
1325

1326
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
2,147,483,647✔
1327
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
2,147,483,647✔
1328
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
7,952✔
1329
      } else {
1330
        SValue value = {
2,147,483,647✔
1331
            .type = infos[iInfo].type,
2,147,483,647✔
1332
        };
1333
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
2,147,483,647✔
1334
          value.nData = infos[iInfo].bind->length[iRow];
35,317,372✔
1335
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
45,217,593✔
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;
45,220,408✔
1340
        } else {
1341
          valueSetDatum(&value, infos[iInfo].type,
2,147,483,647✔
1342
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
2,147,483,647✔
1343
                        infos[iInfo].bind->buffer_length);
2,147,483,647✔
1344
        }
1345
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
2,147,483,647✔
1346
      }
1347
      if (taosArrayPush(colValArray, &colVal) == NULL) {
2,147,483,647✔
1348
        code = terrno;
×
1349
        goto _exit;
×
1350
      }
1351
    }
1352

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

1359
    if ((taosArrayPush(rowArray, &row)) == NULL) {
2,147,483,647✔
1360
      code = terrno;
×
1361
      goto _exit;
×
1362
    }
1363

1364
    if (pOrdered && pDupTs) {
2,147,483,647✔
1365
      tRowGetKey(row, &rowKey);
2,147,483,647✔
1366
      if (iRow == 0) {
2,147,483,647✔
1367
        *pOrdered = true;
12,121,965✔
1368
        *pDupTs = false;
12,123,270✔
1369
      } else {
1370
        if (*pOrdered) {
2,147,483,647✔
1371
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
2,147,483,647✔
1372
          *pOrdered = (res >= 0);
2,147,483,647✔
1373
          if (!*pDupTs) {
2,147,483,647✔
1374
            *pDupTs = (res == 0);
2,147,483,647✔
1375
          }
1376
        }
1377
      }
1378
      lastRowKey = rowKey;
2,147,483,647✔
1379
    }
1380
  }
1381

1382
_exit:
4,003,070✔
1383
  taosArrayDestroy(colValArray);
12,097,976✔
1384
  return code;
12,118,894✔
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,097,734,821✔
1395
    pColVal->value.type = pTColumn->type;
2,100,487,104✔
1396
    pColVal->flag = CV_FLAG_VALUE;
2,100,388,255✔
1397
    VALUE_SET_TRIVIAL_DATUM(&pColVal->value, pRow->ts);
2,100,533,098✔
1398
    return 0;
2,103,067,808✔
1399
  }
1400

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

1406
  if (pRow->flag == HAS_NULL) {
2,147,483,647✔
1407
    *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
260,526,055✔
1408
    return 0;
260,525,180✔
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);
464,730,439✔
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);
2,273✔
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);
1,420,706,512✔
1448
        } else {
1449
          pColVal->cid = pTColumn->colId;
2,147,483,647✔
1450
          pColVal->value.type = pTColumn->type;
2,147,483,647✔
1451
          pColVal->flag = CV_FLAG_VALUE;
2,147,483,647✔
1452

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

1456
            pData += tGetU32v(pData, &pColVal->value.nData);
2,147,483,647✔
1457
            if (pColVal->value.nData > 0) {
1,115,218,983✔
1458
              pColVal->value.pData = pData;
1,113,748,879✔
1459
            } else {
1460
              pColVal->value.pData = NULL;
1,624,643✔
1461
            }
1462
            if (isBlob == 1) {
1,115,365,252✔
1463
              pData += BSE_SEQUECE_SIZE;  // skip seq
28,044✔
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++;
9,352,163✔
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,239,232✔
1504
      return 0;
1,239,232✔
1505
    } else if (bit == BIT_FLG_NULL) {
2,147,483,647✔
1506
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,853,572,295✔
1507
      return 0;
1,853,576,976✔
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) {
1,313,654,304✔
1533
  SRowKey key1, key2;
1,310,059,900✔
1534
  tRowGetKey(*(SRow **)p1, &key1);
2,147,483,647✔
1535
  tRowGetKey(*(SRow **)p2, &key2);
2,147,483,647✔
1536
  return tRowKeyCompare(&key1, &key2);
1,316,633,364✔
1537
}
1538
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
67,220,168✔
1539

1540
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
20,941,111✔
1541
  SColVal* pColVal = tRowIterNext(iter);
20,941,111✔
1542
  while (pColVal != NULL && pColVal->cid < targetCid) {
23,776,421✔
1543
    pColVal = tRowIterNext(iter);
2,835,310✔
1544
  }
1545
  return pColVal;
20,941,111✔
1546
}
1547

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

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

1562
  for (int32_t i = 0; i < nRow; i++) {
70,147,173✔
1563
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
67,183,449✔
1564

1565
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
67,186,572✔
1566
    if (code) goto _exit;
67,183,449✔
1567
  }
1568

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

1576
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
22,000,625✔
1577
    int32_t targetCid = pTSchema->columns[iCol].colId;
19,036,901✔
1578
    SColVal *pColVal = NULL;
19,036,901✔
1579

1580
    switch (strategy) {
19,036,901✔
1581
      case KEEP_CONSISTENCY:
779,785✔
1582
        if (nRow > 0)
779,785✔
1583
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
779,785✔
1584
        break;
779,785✔
1585

1586
      default:  // default using PREFER_NON_NULL strategy
18,257,116✔
1587
      case PREFER_NON_NULL:
1588
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
20,179,980✔
1589
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
20,161,326✔
1590

1591
          if (COL_VAL_IS_VALUE(pColValT)) {
20,161,326✔
1592
            pColVal = pColValT;
18,238,462✔
1593
            break;
18,238,462✔
1594
          } else if (pColVal == NULL) {
1,922,864✔
1595
            pColVal = pColValT;
1,888,454✔
1596
          }
1597
        }
1598
        break;
18,257,116✔
1599
    }
1600

1601
    if (pColVal) {
19,036,901✔
1602
      if (taosArrayPush(aColVal, pColVal) == NULL) {
19,037,106✔
1603
        code = terrno;
×
1604
        goto _exit;
×
1605
      }
1606
    }
1607
  }
1608

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

1613
  if (code) goto _exit;
2,963,724✔
1614

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

1621
_exit:
2,963,724✔
1622
  if (aIter) {
2,963,724✔
1623
    for (int32_t i = 0; i < nRow; i++) {
70,173,340✔
1624
      tRowIterClose(&aIter[i]);
67,209,616✔
1625
    }
1626
    taosMemoryFree(aIter);
2,963,724✔
1627
  }
1628
  if (aColVal) taosArrayDestroy(aColVal);
2,963,724✔
1629
  if (code) tRowDestroy(pRow);
2,963,724✔
1630
  return code;
2,963,724✔
1631
}
1632
static int32_t tBlobSetTransferTo(SBlobSet *pSrc, SBlobSet *pDst, SColVal *pVal) {
15,580✔
1633
  int32_t code = 0;
15,580✔
1634
  int32_t lino = 0;
15,580✔
1635
  if (COL_VAL_IS_NULL(pVal) || pVal->value.pData == NULL) {
31,160✔
1636
    int8_t type = COL_VAL_IS_NULL(pVal) ? TSDB_DATA_BLOB_NULL_VALUE : TSDB_DATA_BLOB_EMPTY_VALUE;
15,580✔
1637
    code = addEmptyItemToBlobSet(pDst, type, NULL);
15,580✔
1638
    TAOS_CHECK_GOTO(code, &lino, _error);
15,580✔
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:
15,580✔
1659
  return code;
15,580✔
1660
}
1661

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

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

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

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

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

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

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

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

1710
  tBlobSetSwap(pBlob, pTempBlob);
1,558✔
1711

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

1717
  if (aIter) {
1,558✔
1718
    for (int32_t i = 0; i < nRow; i++) {
17,138✔
1719
      tRowIterClose(&aIter[i]);
15,580✔
1720
    }
1721
    taosMemoryFree(aIter);
1,558✔
1722
  }
1723
  if (aColVal) {
1,558✔
1724
    taosArrayDestroy(aColVal);
1,558✔
1725
  }
1726
  tBlobSetDestroy(pTempBlob);
1,558✔
1727
  return code;
1,558✔
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) {
977,162✔
1874
  if (TARRAY_SIZE(aRowP) <= 1) return 0;
977,162✔
1875
  int32_t code = taosArrayMSort(aRowP, tRowPCmprFn);
966,368✔
1876
  if (code != TSDB_CODE_SUCCESS) {
966,368✔
1877
    uError("taosArrayMSort failed caused by %d", code);
×
1878
  }
1879
  return code;
966,368✔
1880
}
1881

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

1885
  int32_t iStart = 0;
1,001,363✔
1886
  while (iStart < aRowP->size) {
110,192,763✔
1887
    SRowKey key1;
106,827,615✔
1888
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
109,191,447✔
1889

1890
    tRowGetKey(row1, &key1);
218,519,330✔
1891

1892
    int32_t iEnd = iStart + 1;
109,285,905✔
1893
    while (iEnd < aRowP->size) {
173,573,482✔
1894
      SRowKey key2;
170,588,615✔
1895
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
172,491,396✔
1896
      tRowGetKey(row2, &key2);
344,595,342✔
1897

1898
      if (tRowKeyCompare(&key1, &key2) != 0) break;
172,470,896✔
1899

1900
      iEnd++;
64,277,122✔
1901
    }
1902

1903
    if (iEnd - iStart > 1) {
109,181,970✔
1904
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
2,963,519✔
1905
      if (code) return code;
2,963,724✔
1906
    }
1907

1908
    // the array is also changing, so the iStart just ++ instead of iEnd
1909
    iStart++;
109,182,175✔
1910
  }
1911

1912
  return code;
1,001,521✔
1913
}
1914

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

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

1928
  int8_t  doMerge = 0;
1,558✔
1929
  int32_t iStart = 0;
1,558✔
1930
  while (iStart < aRowP->size) {
17,138✔
1931
    SRowKey key1;
15,580✔
1932
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
15,580✔
1933

1934
    tRowGetKey(row1, &key1);
31,160✔
1935

1936
    int32_t iEnd = iStart + 1;
15,580✔
1937
    while (iEnd < aRowP->size) {
15,580✔
1938
      SRowKey key2;
14,022✔
1939
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
14,022✔
1940
      tRowGetKey(row2, &key2);
28,044✔
1941

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

1944
      iEnd++;
×
1945
    }
1946

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

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

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

1966
    code = tRowMergeAndRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1967
  }
1968
  return code;
1,558✔
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) {
100,350,807✔
1992
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
100,350,807✔
1993

1994
  int32_t code = 0;
100,431,517✔
1995

1996
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
100,431,517✔
1997
  if (pIter == NULL) {
100,048,605✔
1998
    code = terrno;
×
1999
    goto _exit;
×
2000
  }
2001

2002
  pIter->pRow = pRow;
100,048,605✔
2003
  pIter->pTSchema = pTSchema;
100,048,657✔
2004
  pIter->iTColumn = 0;
100,053,104✔
2005

2006
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
100,058,177✔
2007

2008
  uint8_t         *data = pRow->data;
99,882,638✔
2009
  SPrimaryKeyIndex index;
99,852,720✔
2010
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
100,680,563✔
2011
    data += tGetPrimaryKeyIndex(data, &index);
808,553✔
2012
  }
2013

2014
  if (pRow->flag >> 4) {
99,881,109✔
2015
    pIter->iCol = 0;
31,985,652✔
2016
    pIter->pIdx = (SKVIdx *)data;
31,996,312✔
2017
    if (pRow->flag & KV_FLG_LIT) {
31,966,177✔
2018
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
32,047,972✔
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) {
67,900,267✔
2026
      case (HAS_NULL | HAS_NONE):
606✔
2027
        pIter->pb = data;
606✔
2028
        break;
606✔
2029
      case HAS_VALUE:
67,742,207✔
2030
        pIter->pf = data;
67,742,207✔
2031
        pIter->pv = pIter->pf + pTSchema->flen;
67,742,759✔
2032
        break;
67,767,601✔
2033
      case (HAS_VALUE | HAS_NONE):
170,118✔
2034
      case (HAS_VALUE | HAS_NULL):
2035
        pIter->pb = data;
170,118✔
2036
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
170,118✔
2037
        pIter->pv = pIter->pf + pTSchema->flen;
170,118✔
2038
        break;
170,118✔
2039
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2040
        pIter->pb = data;
×
2041
        pIter->pf = data + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2042
        pIter->pv = pIter->pf + pTSchema->flen;
×
2043
        break;
×
2044
      default:
×
2045
        break;
×
2046
    }
2047
  }
2048

2049
_exit:
100,203,456✔
2050
  if (code) {
100,131,501✔
2051
    *ppIter = NULL;
×
2052
  } else {
2053
    *ppIter = pIter;
100,131,501✔
2054
  }
2055
  return code;
100,150,566✔
2056
}
2057

2058
void tRowIterClose(SRowIter **ppIter) {
100,391,668✔
2059
  SRowIter *pIter = *ppIter;
100,391,668✔
2060
  if (pIter) {
100,532,093✔
2061
    taosMemoryFree(pIter);
100,539,205✔
2062
  }
2063
  *ppIter = NULL;
100,188,560✔
2064
}
100,210,842✔
2065

2066
SColVal *tRowIterNext(SRowIter *pIter) {
408,693,119✔
2067
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
408,693,119✔
2068
    return NULL;
32,890,434✔
2069
  }
2070

2071
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
386,188,805✔
2072

2073
  // timestamp
2074
  if (0 == pIter->iTColumn) {
387,240,351✔
2075
    pIter->cv.cid = pTColumn->colId;
37,387,637✔
2076
    pIter->cv.value.type = pTColumn->type;
37,286,950✔
2077
    pIter->cv.flag = CV_FLAG_VALUE;
37,379,899✔
2078
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
37,365,670✔
2079
    goto _exit;
37,415,433✔
2080
  }
2081

2082
  if (pIter->pRow->flag == HAS_NONE) {
347,377,475✔
2083
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,447,484✔
2084
    goto _exit;
1,447,484✔
2085
  }
2086

2087
  if (pIter->pRow->flag == HAS_NULL) {
348,501,153✔
2088
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
386,644✔
2089
    goto _exit;
386,644✔
2090
  }
2091

2092
  if (pIter->pRow->flag >> 4) {  // KV
349,142,379✔
2093
    if (pIter->iCol < pIter->pIdx->nCol) {
332,741,964✔
2094
      uint8_t *pData;
2095

2096
      if (pIter->pRow->flag & KV_FLG_LIT) {
320,735,089✔
2097
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
321,299,864✔
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;
320,609,424✔
2105
      pData += tGetI16v(pData, &cid);
320,624,389✔
2106

2107
      if (TABS(cid) == pTColumn->colId) {
321,125,409✔
2108
        if (cid < 0) {
321,472,680✔
2109
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
63,530,818✔
2110
        } else {
2111
          pIter->cv.cid = pTColumn->colId;
257,941,862✔
2112
          pIter->cv.value.type = pTColumn->type;
257,728,457✔
2113
          pIter->cv.flag = CV_FLAG_VALUE;
257,876,672✔
2114

2115
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
254,505,037✔
2116
            pData += tGetU32v(pData, &pIter->cv.value.nData);
127,938,101✔
2117
            if (pIter->cv.value.nData > 0) {
64,273,373✔
2118
              pIter->cv.value.pData = pData;
64,303,508✔
2119
            } else {
2120
              pIter->cv.value.pData = NULL;
×
2121
              // if (IS_STR_DATA_BLOB(pTColumn->type)) {
2122
              //   pIter->cv.value.pData = pData;
2123
              // }
2124
            }
2125
          } else {
2126
            valueSetDatum(&pIter->cv.value, pTColumn->type, pData, pTColumn->bytes);
194,037,079✔
2127
          }
2128
        }
2129

2130
        pIter->iCol++;
320,953,415✔
2131
        goto _exit;
319,447,485✔
2132
      } else if (TABS(cid) > pTColumn->colId) {
43,664✔
2133
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
43,664✔
2134
        goto _exit;
43,664✔
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);
11,663,090✔
2141
      goto _exit;
11,663,090✔
2142
    }
2143
  } else {  // Tuple
2144
    uint8_t bv = BIT_FLG_VALUE;
16,420,095✔
2145
    if (pIter->pb) {
16,420,095✔
2146
      switch (pIter->pRow->flag) {
676,497✔
2147
        case (HAS_NULL | HAS_NONE):
1,818✔
2148
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
1,818✔
2149
          break;
1,818✔
2150
        case (HAS_VALUE | HAS_NONE):
15,112✔
2151
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
15,112✔
2152
          if (bv) bv++;
15,112✔
2153
          break;
15,112✔
2154
        case (HAS_VALUE | HAS_NULL):
659,567✔
2155
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
659,567✔
2156
          break;
659,567✔
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) {
676,497✔
2165
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
7,614✔
2166
        goto _exit;
7,614✔
2167
      } else if (bv == BIT_FLG_NULL) {
668,883✔
2168
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
119,233✔
2169
        goto _exit;
119,233✔
2170
      }
2171
    }
2172

2173
    pIter->cv.cid = pTColumn->colId;
16,293,248✔
2174
    pIter->cv.value.type = pTColumn->type;
16,293,248✔
2175
    pIter->cv.flag = CV_FLAG_VALUE;
16,293,658✔
2176
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
20,324,015✔
2177
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
4,030,972✔
2178
      pData += tGetU32v(pData, &pIter->cv.value.nData);
8,058,029✔
2179
      if (pIter->cv.value.nData > 0) {
4,030,562✔
2180
        pIter->cv.value.pData = pData;
3,986,282✔
2181
      } else {
2182
        pIter->cv.value.pData = NULL;
44,485✔
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]);
12,262,481✔
2189
    }
2190
    goto _exit;
16,293,043✔
2191
  }
2192

2193
_exit:
381,835,835✔
2194
  pIter->iTColumn++;
381,835,835✔
2195
  return &pIter->cv;
385,941,933✔
2196
}
2197

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

2201
  if (flag) return code;
409,235✔
2202

2203
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
3,083,212✔
2204
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
2,769,341✔
2205
    if (code) return code;
2,770,007✔
2206
  }
2207

2208
  return code;
313,871✔
2209
}
2210
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
32,381,835✔
2211
  int32_t code = 0;
32,381,835✔
2212

2213
  int32_t   iColData = 0;
32,381,835✔
2214
  SColData *pColData = &aColData[iColData];
32,381,835✔
2215
  int32_t   iTColumn = 1;
32,383,569✔
2216
  STColumn *pTColumn = &pSchema->columns[iTColumn];
32,383,569✔
2217

2218
  while (pColData) {
297,148,511✔
2219
    if (pTColumn) {
264,742,945✔
2220
      if (pTColumn->colId == pColData->cid) {  // NULL
264,742,945✔
2221
        if (flag == 0) {
264,833,574✔
2222
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
264,833,574✔
2223
        } else {
2224
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2225
        }
2226
        if (code) goto _exit;
264,805,199✔
2227

2228
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
264,805,199✔
2229
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
264,809,459✔
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:
32,405,566✔
2243
  return code;
32,405,566✔
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);
41,156,871✔
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):
54,072✔
2267
      pb = data;
54,072✔
2268
      break;
54,072✔
2269
    case (HAS_VALUE | HAS_NONE):
242,828,042✔
2270
    case (HAS_VALUE | HAS_NULL):
2271
      pb = data;
242,828,042✔
2272
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
242,828,042✔
2273
      pv = pf + pTSchema->flen;
242,784,337✔
2274
      break;
242,852,523✔
2275
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
23✔
2276
      pb = data;
23✔
2277
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
23✔
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) {
2,147,483,647✔
2293
            case (HAS_NULL | HAS_NONE):
993,156✔
2294
              bv = GET_BIT1(pb, iTColumn - 1);
993,156✔
2295
              break;
993,156✔
2296
            case (HAS_VALUE | HAS_NONE):
53,569,136✔
2297
              bv = GET_BIT1(pb, iTColumn - 1);
53,569,136✔
2298
              if (bv) bv++;
53,569,458✔
2299
              break;
53,569,458✔
2300
            case (HAS_VALUE | HAS_NULL):
2,147,483,647✔
2301
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
2,147,483,647✔
2302
              break;
2,147,483,647✔
2303
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
7,046,258✔
2304
              bv = GET_BIT2(pb, iTColumn - 1);
7,046,258✔
2305
              break;
×
2306
            default:
×
2307
              return TSDB_CODE_INVALID_DATA_FMT;
×
2308
          }
2309

2310
          if (bv == BIT_FLG_NONE) {
2,147,483,647✔
2311
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
4,546,284✔
2312
              goto _exit;
×
2313
            goto _continue;
4,546,284✔
2314
          } else if (bv == BIT_FLG_NULL) {
2,147,483,647✔
2315
            if (flag == 0) {
350,783,104✔
2316
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
350,611,063✔
2317
            } else {
2318
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
172,041✔
2319
            }
2320
            if (code) goto _exit;
350,816,334✔
2321
            goto _continue;
350,816,334✔
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);
64,776,401✔
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,
224,521,728✔
2341
                                                                          TYPE_BYTES[pColData->type], flag > 0);
112,305,661✔
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
119,704,483✔
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;
119,705,800✔
2354
      }
2355
    } else {
2356
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
112,687,821✔
2357
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
112,688,436✔
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,278,507,886✔
2365
  int32_t code = 0;
1,278,507,886✔
2366

2367
  uint8_t  *pv = NULL;
1,278,507,886✔
2368
  int32_t   iColData = 0;
1,278,507,886✔
2369
  SColData *pColData = &aColData[iColData];
1,278,507,886✔
2370
  int32_t   iTColumn = 1;
1,279,228,147✔
2371
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,279,228,147✔
2372
  int32_t   iCol = 0;
1,279,375,335✔
2373

2374
  // primary keys
2375
  uint8_t         *data = pRow->data;
1,279,375,335✔
2376
  SPrimaryKeyIndex index;
1,279,418,556✔
2377
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,281,674,695✔
2378
    data += tGetPrimaryKeyIndex(data, &index);
2,241,200✔
2379
  }
2380

2381
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,279,335,716✔
2382
  if (pRow->flag & KV_FLG_LIT) {
1,279,335,716✔
2383
    pv = pKVIdx->idx + pKVIdx->nCol;
1,272,159,222✔
2384
  } else if (pRow->flag & KV_FLG_MID) {
7,210,929✔
2385
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
7,238,526✔
2386
  } else if (pRow->flag & KV_FLG_BIG) {
×
2387
    pv = pKVIdx->idx + (pKVIdx->nCol << 2);
×
2388
  } else {
2389
    return TSDB_CODE_INVALID_PARA;
×
2390
  }
2391

2392
  while (pColData) {
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) {
199,565,758✔
2400
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
200,909,418✔
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) {
1,560,167,509✔
2413
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1,560,671,586✔
2414
              } else {
2415
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
4,256✔
2416
              }
2417
              if (code) goto _exit;
1,557,517,034✔
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);
571,728,522✔
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);
6,660,324✔
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++;
21,533,285✔
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) {
21,532,260✔
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;
21,532,055✔
2451
      }
2452
    } else {
2453
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
20,507,388✔
2454
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
20,510,258✔
2455
    }
2456
  }
2457

2458
_exit:
1,279,525,924✔
2459
  return code;
1,279,100,036✔
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);
409,235✔
2471
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2472
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
32,381,865✔
2473
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2474
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,279,095,840✔
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) {
527,877,442✔
2481
  key->numOfPKs = row->numOfPKs;
527,877,442✔
2482

2483
  if (key->numOfPKs == 0) {
527,917,777✔
2484
    return;
×
2485
  }
2486

2487
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
496,051,634✔
2488

2489
  uint8_t *data = row->data;
527,763,713✔
2490

2491
  for (int32_t i = 0; i < row->numOfPKs; i++) {
1,055,918,349✔
2492
    data += tGetPrimaryKeyIndex(data, &indices[i]);
527,950,603✔
2493
  }
2494

2495
  // primary keys
2496
  for (int32_t i = 0; i < row->numOfPKs; i++) {
1,055,957,203✔
2497
    key->pks[i].type = indices[i].type;
527,962,526✔
2498

2499
    uint8_t *tdata = data + indices[i].offset;
527,971,902✔
2500
    if (row->flag >> 4) {
527,970,851✔
2501
      tdata += tGetI16v(tdata, NULL);
179,350,648✔
2502
    }
2503

2504
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
527,972,323✔
2505
      key->pks[i].pData = tdata;
124,627,652✔
2506
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
248,486,957✔
2507
    } else {
2508
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
403,740,974✔
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) {
220,103,156✔
2525
  switch (tv1->type) {
220,103,156✔
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:
156,346,947✔
2532
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
156,346,947✔
2533
    case TSDB_DATA_TYPE_BIGINT:
12,231,663✔
2534
    case TSDB_DATA_TYPE_TIMESTAMP:
2535
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
12,231,663✔
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:
12,103,084✔
2545
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
12,103,084✔
2546
    case TSDB_DATA_TYPE_UBIGINT:
12,040,560✔
2547
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
12,040,560✔
2548
    case TSDB_DATA_TYPE_GEOMETRY:
27,396,847✔
2549
    case TSDB_DATA_TYPE_BINARY: {
2550
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
27,396,847✔
2551
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
27,396,157✔
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: {
2,815✔
2559
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
2,815✔
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]);
220,146,740✔
2582
      if (ret) return ret;
220,159,574✔
2583
    }
2584
  } else if (key1->numOfPKs < key2->numOfPKs) {
5,710✔
2585
    return -1;
5,710✔
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) {
71,257,996✔
2599
      SValue *pVal = &pDst->pks[i];
35,629,634✔
2600
      pVal->type = pSrc->pks[i].type;
35,626,438✔
2601

2602
      valueCloneDatum(pVal, pSrc->pks + i, pVal->type);
35,627,255✔
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,515,189✔
2618
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
4,515,189✔
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) {
365,086,146✔
2715
  int32_t n = 0;
365,086,146✔
2716

2717
  // key
2718
  if (isJson) {
365,086,146✔
2719
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
2,940,592✔
2720
  } else {
2721
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
727,219,925✔
2722
  }
2723

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

2727
  // value
2728
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
365,096,547✔
2729
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
318,584,178✔
2730
  } else {
2731
    p = p ? p + n : p;
205,802,618✔
2732
    n += tDataTypes[pTagVal->type].bytes;
205,801,834✔
2733
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
205,766,636✔
2734
  }
2735

2736
  return n;
365,054,765✔
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);
10,144,654✔
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);
1,975,774,550✔
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,855,633✔
2763

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

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

2779
  // sort
2780
  if (isJson) {
79,292,710✔
2781
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
486,756✔
2782
  } else {
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
78,805,954✔
2784
  }
2785

2786
  // get size
2787
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
261,806,808✔
2788
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
182,540,420✔
2789
  }
2790
  if (szTag <= INT8_MAX) {
79,266,388✔
2791
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
74,856,452✔
2792
  } else {
2793
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
4,409,936✔
2794
    isLarge = 1;
4,409,936✔
2795
  }
2796

2797
  // build tag
2798
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
79,266,388✔
2799
  if ((*ppTag) == NULL) {
79,286,105✔
2800
    code = terrno;
×
2801
    goto _err;
×
2802
  }
2803
  (*ppTag)->flags = 0;
79,291,881✔
2804
  if (isJson) {
79,297,407✔
2805
    (*ppTag)->flags |= TD_TAG_JSON;
486,756✔
2806
  }
2807
  if (isLarge) {
79,297,407✔
2808
    (*ppTag)->flags |= TD_TAG_LARGE;
4,410,890✔
2809
  }
2810
  (*ppTag)->len = szTag;
79,297,407✔
2811
  (*ppTag)->nTag = nTag;
79,292,886✔
2812
  (*ppTag)->ver = version;
79,289,588✔
2813

2814
  if (isLarge) {
79,265,340✔
2815
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
4,410,788✔
2816
  } else {
2817
    p = (uint8_t *)&(*ppTag)->idx[nTag];
74,854,552✔
2818
  }
2819
  n = 0;
79,277,104✔
2820
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
261,845,080✔
2821
    if (isLarge) {
182,543,674✔
2822
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
48,873,020✔
2823
    } else {
2824
      (*ppTag)->idx[iTag] = n;
133,670,654✔
2825
    }
2826
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
182,537,332✔
2827
  }
2828
#ifdef TD_DEBUG_PRINT_TAG
2829
  debugPrintSTag(*ppTag, __func__, __LINE__);
2830
#endif
2831

2832
  return code;
79,301,406✔
2833

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

2838
void tTagFree(STag *pTag) {
1,032,040,960✔
2839
  if (pTag) taosMemoryFree(pTag);
1,032,040,960✔
2840
}
1,032,040,960✔
2841

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

2847
  char  *data = NULL;
450,650,285✔
2848
  int8_t typeBytes = 0;
450,650,285✔
2849
  if (isJson) {
450,650,285✔
2850
    typeBytes = CHAR_BYTES;
2,341,804✔
2851
  }
2852

2853
  if (IS_VAR_DATA_TYPE(value->type)) {
450,650,285✔
2854
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
183,507,135✔
2855
    if (data == NULL) {
183,521,162✔
2856
      return NULL;
×
2857
    }
2858

2859
    if (isJson) {
183,521,162✔
2860
      *data = value->type;
1,824,152✔
2861
    }
2862

2863
    varDataLen(data + typeBytes) = value->nData;
183,521,162✔
2864
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
183,523,900✔
2865
  } else {
2866
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
267,135,512✔
2867
  }
2868

2869
  return data;
450,655,341✔
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];
266,124,737✔
2889
  } else {
2890
    p = (uint8_t *)&pTag->idx[pTag->nTag];
2,147,483,647✔
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];
964,652,598✔
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,950,779✔
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,147,483,647✔
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;
59,452,788✔
2921
}
2922

2923
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
235,562,465✔
2924
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
471,149,457✔
2925
}
2926

2927
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
1,315,530,299✔
2928

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

2936
  if (isLarge) {
829,348✔
2937
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
4,346✔
2938
  } else {
2939
    p = (uint8_t *)&pTag->idx[pTag->nTag];
825,002✔
2940
  }
2941

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

2948
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
2,008,501✔
2949
    if (isLarge) {
1,179,153✔
2950
      offset = ((int16_t *)pTag->idx)[iTag];
5,849✔
2951
    } else {
2952
      offset = pTag->idx[iTag];
1,173,304✔
2953
    }
2954
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,179,153✔
2955
    if (taosArrayPush(*ppArray, &tv) == NULL) {
2,358,306✔
2956
      code = terrno;
×
2957
      goto _err;
×
2958
    }
2959
  }
2960

2961
  return code;
829,348✔
2962

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

2967
// STSchema ========================================
2968
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
727,468,394✔
2969
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
727,468,394✔
2970
  if (pTSchema == NULL) {
727,395,903✔
2971
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2972
    return NULL;
×
2973
  }
2974

2975
  pTSchema->numOfCols = numOfCols;
727,395,903✔
2976
  pTSchema->version = version;
727,397,231✔
2977

2978
  // timestamp column
2979
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
727,455,641✔
2980
    terrno = TSDB_CODE_INVALID_PARA;
×
2981
    taosMemoryFree(pTSchema);
×
2982
    return NULL;
×
2983
  }
2984
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
727,405,571✔
2985
    terrno = TSDB_CODE_INVALID_PARA;
×
2986
    taosMemoryFree(pTSchema);
×
2987
    return NULL;
×
2988
  }
2989
  pTSchema->columns[0].colId = aSchema[0].colId;
727,393,012✔
2990
  pTSchema->columns[0].type = aSchema[0].type;
727,494,480✔
2991
  pTSchema->columns[0].flags = aSchema[0].flags;
727,448,901✔
2992
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
727,529,342✔
2993
  pTSchema->columns[0].offset = -1;
727,506,024✔
2994

2995
  // other columns
2996
  for (int32_t iCol = 1; iCol < numOfCols; iCol++) {
2,147,483,647✔
2997
    SSchema  *pSchema = &aSchema[iCol];
2,147,483,647✔
2998
    STColumn *pTColumn = &pTSchema->columns[iCol];
2,147,483,647✔
2999

3000
    pTColumn->colId = pSchema->colId;
2,147,483,647✔
3001
    pTColumn->type = pSchema->type;
2,147,483,647✔
3002
    pTColumn->flags = pSchema->flags;
2,147,483,647✔
3003
    pTColumn->offset = pTSchema->flen;
2,147,483,647✔
3004

3005
    if (IS_VAR_DATA_TYPE(pSchema->type)) {
2,147,483,647✔
3006
      pTColumn->bytes = pSchema->bytes;
2,147,483,647✔
3007
      pTSchema->tlen += (TYPE_BYTES[pSchema->type] + pSchema->bytes);  // todo: remove
2,147,483,647✔
3008
    } else {
3009
      pTColumn->bytes = TYPE_BYTES[pSchema->type];
2,147,483,647✔
3010
      pTSchema->tlen += TYPE_BYTES[pSchema->type];  // todo: remove
2,147,483,647✔
3011
    }
3012

3013
    pTSchema->flen += TYPE_BYTES[pTColumn->type];
2,147,483,647✔
3014
  }
3015

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

3020
  return pTSchema;
727,546,482✔
3021
}
3022

3023
static int32_t tTColumnCompare(const void *p1, const void *p2) {
46,633,821✔
3024
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
46,633,821✔
3025
    return -1;
7,610,153✔
3026
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
39,024,823✔
3027
    return 1;
27,677,787✔
3028
  }
3029

3030
  return 0;
11,348,986✔
3031
}
3032

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

3038
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
11,349,151✔
3039
}
3040

3041
// SColData ========================================
3042
void tColDataDestroy(void *ph) {
853,758,666✔
3043
  if (ph) {
853,758,666✔
3044
    SColData *pColData = (SColData *)ph;
853,809,965✔
3045

3046
    tFree(pColData->pBitMap);
853,809,965✔
3047
    tFree(pColData->aOffset);
853,852,730✔
3048
    tFree(pColData->pData);
853,797,553✔
3049
  }
3050
}
853,736,091✔
3051

3052
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
934,290,604✔
3053
  pColData->cid = cid;
934,290,604✔
3054
  pColData->type = type;
934,337,537✔
3055
  pColData->cflag = cflag;
934,331,336✔
3056
  tColDataClear(pColData);
934,362,181✔
3057
}
934,222,426✔
3058

3059
void tColDataClear(SColData *pColData) {
1,913,740,214✔
3060
  pColData->numOfNone = 0;
1,913,740,214✔
3061
  pColData->numOfNull = 0;
1,913,773,186✔
3062
  pColData->numOfValue = 0;
1,913,802,810✔
3063
  pColData->nVal = 0;
1,913,826,109✔
3064
  pColData->flag = 0;
1,913,811,315✔
3065
  pColData->nData = 0;
1,913,872,918✔
3066
}
1,913,822,698✔
3067

3068
void tColDataDeepClear(SColData *pColData) {
5,925,321✔
3069
  pColData->pBitMap = NULL;
5,925,321✔
3070
  pColData->aOffset = NULL;
5,925,799✔
3071
  pColData->pData = NULL;
5,925,195✔
3072

3073
  tColDataClear(pColData);
5,925,767✔
3074
}
5,925,652✔
3075

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

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

3097
    } else {
3098
      code = tRealloc((uint8_t **)(&pColData->aOffset), ((int64_t)(pColData->nVal + 1)) << 2);
2,147,483,647✔
3099
      if (code) goto _exit;
2,147,483,647✔
3100
      pColData->aOffset[pColData->nVal] = pColData->nData;
2,147,483,647✔
3101

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

3124
_exit:
2,147,483,647✔
3125
  return code;
2,147,483,647✔
3126
}
3127
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
439,623,664✔
3128
  pColData->flag = HAS_VALUE;
439,715,314✔
3129
  pColData->numOfValue++;
439,733,082✔
3130
  return tColDataPutValue(pColData, pData, nData);
439,735,203✔
3131
}
3132
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
14,816,081✔
3133
  pColData->flag = HAS_NONE;
14,816,081✔
3134
  pColData->numOfNone++;
14,817,926✔
3135
  pColData->nVal++;
14,818,181✔
3136
  return 0;
14,818,181✔
3137
}
3138
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
20,268,664✔
3139
  pColData->flag = HAS_NULL;
20,286,895✔
3140
  pColData->numOfNull++;
20,292,561✔
3141
  pColData->nVal++;
20,290,625✔
3142
  return 0;
20,290,432✔
3143
}
3144
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
833,432✔
3145
  int32_t code = 0;
833,432✔
3146

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

3151
  memset(pColData->pBitMap, 0, nBit);
832,950✔
3152
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
833,432✔
3153

3154
  pColData->flag |= HAS_VALUE;
833,432✔
3155
  pColData->numOfValue++;
833,432✔
3156

3157
  if (pColData->nVal) {
833,432✔
3158
    if (IS_VAR_DATA_TYPE(pColData->type)) {
833,432✔
3159
      if (IS_STR_DATA_BLOB(pColData->type)) {
20,198✔
3160
        int32_t nOffset = sizeof(int32_t) * pColData->nVal;
×
3161
        code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
×
3162
        if (code) return code;
×
3163
        memset(pColData->aOffset, 0, nOffset);
×
3164

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

3179
  return tColDataPutValue(pColData, pData, nData);
832,950✔
3180
}
3181
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3182
  pColData->nVal++;
2,147,483,647✔
3183
  pColData->numOfNone++;
2,147,483,647✔
3184
  return 0;
2,147,483,647✔
3185
}
3186
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
19,876✔
3187
  int32_t code = 0;
19,876✔
3188

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

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

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

3200
  return code;
19,876✔
3201
}
3202
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,602,298✔
3203
  int32_t code = 0;
4,602,760✔
3204

3205
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
4,602,760✔
3206
  code = tRealloc(&pColData->pBitMap, nBit);
4,603,539✔
3207
  if (code) return code;
4,603,539✔
3208

3209
  memset(pColData->pBitMap, 0, nBit);
4,603,539✔
3210
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
4,603,539✔
3211

3212
  pColData->flag |= HAS_VALUE;
4,603,539✔
3213
  pColData->numOfValue++;
4,603,539✔
3214

3215
  if (pColData->nVal) {
4,603,539✔
3216
    if (IS_VAR_DATA_TYPE(pColData->type)) {
6,060,259✔
3217
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,455,941✔
3218
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,456,720✔
3219
      if (code) return code;
1,457,499✔
3220
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,457,499✔
3221
        memset(pColData->aOffset, 0, nOffset);
1,454,383✔
3222
      }
3223
    } else {
3224
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
3,146,040✔
3225
      code = tRealloc(&pColData->pData, pColData->nData);
3,146,040✔
3226
      if (code) return code;
3,146,040✔
3227
      memset(pColData->pData, 0, pColData->nData);
3,146,040✔
3228
    }
3229
  }
3230

3231
  return tColDataPutValue(pColData, pData, nData);
4,603,539✔
3232
}
3233
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,940✔
3234
  int32_t code = 0;
2,940✔
3235

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

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

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

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

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

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

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

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

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

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

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

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

3298
  return code;
12,880✔
3299
}
3300
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
302,680✔
3301
  int32_t code = 0;
302,680✔
3302

3303
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
302,680✔
3304
  if (code) return code;
302,680✔
3305

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

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

3319
  pColData->flag |= HAS_NONE;
5,293,055✔
3320
  pColData->numOfNone++;
5,293,055✔
3321

3322
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
5,293,210✔
3323
  code = tRealloc(&pColData->pBitMap, nBit);
5,293,055✔
3324
  if (code) return code;
5,292,425✔
3325

3326
  memset(pColData->pBitMap, 255, nBit);
5,292,425✔
3327
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
5,292,580✔
3328

3329
  return tColDataPutValue(pColData, NULL, 0);
5,292,900✔
3330
}
3331
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
17,862,659✔
3332
  int32_t code = 0;
18,033,846✔
3333

3334
  pColData->flag |= HAS_NULL;
18,033,846✔
3335
  pColData->numOfNull++;
18,034,007✔
3336

3337
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
18,034,171✔
3338
  code = tRealloc(&pColData->pBitMap, nBit);
18,034,332✔
3339
  if (code) return code;
18,034,171✔
3340

3341
  memset(pColData->pBitMap, 255, nBit);
18,034,171✔
3342
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
18,034,171✔
3343

3344
  return tColDataPutValue(pColData, NULL, 0);
18,033,780✔
3345
}
3346
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
488,307,175✔
3347
  int32_t code = 0;
488,372,198✔
3348

3349
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
488,372,198✔
3350
  if (code) return code;
488,360,129✔
3351

3352
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
488,360,129✔
3353
  pColData->numOfValue++;
488,375,928✔
3354

3355
  return tColDataPutValue(pColData, pData, nData);
488,375,706✔
3356
}
3357
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
779,489,069✔
3358
  int32_t code = 0;
779,489,069✔
3359

3360
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
779,489,069✔
3361
  if (code) return code;
779,494,806✔
3362

3363
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
779,494,806✔
3364
  pColData->numOfNone++;
779,494,943✔
3365

3366
  return tColDataPutValue(pColData, NULL, 0);
779,494,332✔
3367
}
3368
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,879✔
3369
  int32_t code = 0;
2,879✔
3370

3371
  pColData->flag |= HAS_NULL;
2,879✔
3372
  pColData->numOfNull++;
2,879✔
3373

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

3378
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
707,997✔
3379
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
705,118✔
3380
  }
3381
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
2,879✔
3382

3383
  tFree(pColData->pBitMap);
2,879✔
3384
  pColData->pBitMap = pBitMap;
2,879✔
3385

3386
  return tColDataPutValue(pColData, NULL, 0);
2,879✔
3387
}
3388
static FORCE_INLINE int32_t tColDataAppendValue60(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3389
  int32_t code = 0;
2,147,483,647✔
3390

3391
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
2,147,483,647✔
3392
  if (code) return code;
2,147,483,647✔
3393
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
2,147,483,647✔
3394
  pColData->numOfValue++;
2,147,483,647✔
3395

3396
  return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3397
}
3398
static FORCE_INLINE int32_t tColDataAppendValue61(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,634,236✔
3399
  int32_t code = 0;
1,634,236✔
3400

3401
  pColData->flag |= HAS_NONE;
1,634,236✔
3402
  pColData->numOfNone++;
1,634,236✔
3403

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

3408
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
157,033,890✔
3409
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
155,399,654✔
3410
  }
3411
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
1,634,236✔
3412

3413
  tFree(pColData->pBitMap);
1,634,236✔
3414
  pColData->pBitMap = pBitMap;
1,634,236✔
3415

3416
  return tColDataPutValue(pColData, NULL, 0);
1,634,236✔
3417
}
3418
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,088,215,925✔
3419
  int32_t code = 0;
1,088,667,058✔
3420

3421
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,088,667,058✔
3422
  if (code) return code;
1,089,070,255✔
3423
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,089,070,255✔
3424
  pColData->numOfNull++;
1,089,113,247✔
3425

3426
  return tColDataPutValue(pColData, NULL, 0);
1,089,193,263✔
3427
}
3428
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,261,836✔
3429
  int32_t code = 0;
2,261,836✔
3430

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

3436
  return tColDataPutValue(pColData, pData, nData);
2,261,836✔
3437
}
3438
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
297,900,711✔
3439
  int32_t code = 0;
297,900,711✔
3440

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

3446
  return tColDataPutValue(pColData, NULL, 0);
297,900,711✔
3447
}
3448
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
9,776✔
3449
  int32_t code = 0;
9,776✔
3450

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

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

3468
    //       VALUE                  NONE                     NULL
3469
};
3470

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3834
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,985✔
3835
  pColData->numOfNone--;
1,985✔
3836
  pColData->nVal--;
1,985✔
3837
  if (pColData->numOfNone) {
1,985✔
3838
    return tColDataAppendValue10(pColData, pData, nData);
×
3839
  } else {
3840
    pColData->flag = 0;
1,985✔
3841
    return tColDataAppendValue00(pColData, pData, nData);
1,985✔
3842
  }
3843
}
3844
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
882✔
3845
  pColData->numOfNone--;
882✔
3846
  pColData->nVal--;
882✔
3847
  if (pColData->numOfNone) {
882✔
3848
    return tColDataAppendValue12(pColData, pData, nData);
×
3849
  } else {
3850
    pColData->flag = 0;
882✔
3851
    return tColDataAppendValue02(pColData, pData, nData);
882✔
3852
  }
3853
  return 0;
3854
}
3855
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
90,127✔
3856
  if (forward) {
90,127✔
3857
    pColData->numOfNull--;
90,127✔
3858
    pColData->nVal--;
90,127✔
3859
    if (pColData->numOfNull) {
90,127✔
3860
      return tColDataAppendValue20(pColData, pData, nData);
462✔
3861
    } else {
3862
      pColData->flag = 0;
89,665✔
3863
      return tColDataAppendValue00(pColData, pData, nData);
89,665✔
3864
    }
3865
  }
3866
  return 0;
×
3867
}
3868
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3869
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3870
    pColData->numOfNone--;
×
3871
    pColData->nVal--;
×
3872
    if (pColData->numOfNone) {
×
3873
      return tColDataAppendValue30(pColData, pData, nData);
×
3874
    } else {
3875
      pColData->flag = HAS_NULL;
×
3876
      return tColDataAppendValue20(pColData, pData, nData);
×
3877
    }
3878
  } else if (forward) {  // NULL ==> VALUE
×
3879
    pColData->numOfNull--;
×
3880
    pColData->nVal--;
×
3881
    if (pColData->numOfNull) {
×
3882
      return tColDataAppendValue30(pColData, pData, nData);
×
3883
    } else {
3884
      pColData->flag = HAS_NONE;
×
3885
      return tColDataAppendValue10(pColData, pData, nData);
×
3886
    }
3887
  }
3888
  return 0;
×
3889
}
3890
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,940✔
3891
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
2,940✔
3892
    pColData->numOfNone--;
2,940✔
3893
    pColData->numOfNull++;
2,940✔
3894
    if (pColData->numOfNone) {
2,940✔
3895
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
×
3896
    } else {
3897
      pColData->flag = HAS_NULL;
2,940✔
3898
    }
3899
  }
3900
  return 0;
2,940✔
3901
}
3902
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,147,483,647✔
3903
  if (forward) {  // VALUE ==> VALUE
2,147,483,647✔
3904
    pColData->nVal--;
2,147,483,647✔
3905
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3906
      pColData->nData = pColData->aOffset[pColData->nVal];
1,185,965,461✔
3907
    } else {
3908
      pColData->nData -= TYPE_BYTES[pColData->type];
2,147,483,647✔
3909
    }
3910
    return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3911
  }
3912
  return 0;
×
3913
}
3914
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
178,729✔
3915
  if (forward) {  // VALUE ==> NULL
178,729✔
3916
    pColData->numOfValue--;
178,729✔
3917
    pColData->nVal--;
178,729✔
3918
    if (pColData->numOfValue) {
178,729✔
3919
      if (IS_VAR_DATA_TYPE(pColData->type)) {
161,380✔
3920
        pColData->nData = pColData->aOffset[pColData->nVal];
32,200✔
3921
      } else {
3922
        pColData->nData -= TYPE_BYTES[pColData->type];
129,180✔
3923
      }
3924
      return tColDataAppendValue42(pColData, pData, nData);
161,380✔
3925
    } else {
3926
      pColData->flag = 0;
17,349✔
3927
      pColData->nData = 0;
17,349✔
3928
      return tColDataAppendValue02(pColData, pData, nData);
17,349✔
3929
    }
3930
  }
3931
  return 0;
×
3932
}
3933
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,036,702✔
3934
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
1,036,702✔
3935
    pColData->numOfNone--;
180,320✔
3936
    pColData->nVal--;
180,320✔
3937
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
180,159✔
3938
      pColData->nData -= TYPE_BYTES[pColData->type];
144,256✔
3939
    }
3940
    if (pColData->numOfNone) {
180,159✔
3941
      return tColDataAppendValue50(pColData, pData, nData);
64,400✔
3942
    } else {
3943
      pColData->flag = HAS_VALUE;
115,759✔
3944
      return tColDataAppendValue40(pColData, pData, nData);
115,920✔
3945
    }
3946
  } else if (forward) {  // VALUE ==> VALUE
856,543✔
3947
    pColData->nVal--;
856,543✔
3948
    if (IS_VAR_DATA_TYPE(pColData->type)) {
856,543✔
3949
      pColData->nData = pColData->aOffset[pColData->nVal];
122,220✔
3950
    } else {
3951
      pColData->nData -= TYPE_BYTES[pColData->type];
734,323✔
3952
    }
3953
    return tColDataPutValue(pColData, pData, nData);
856,543✔
3954
  }
3955
  return 0;
×
3956
}
3957
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
9,807✔
3958
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
9,807✔
3959
    pColData->numOfNone--;
9,807✔
3960
    pColData->nVal--;
9,807✔
3961
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
9,807✔
3962
      pColData->nData -= TYPE_BYTES[pColData->type];
7,875✔
3963
    }
3964
    if (pColData->numOfNone) {
9,807✔
3965
      return tColDataAppendValue52(pColData, pData, nData);
×
3966
    } else {
3967
      pColData->flag = HAS_VALUE;
9,807✔
3968
      return tColDataAppendValue42(pColData, pData, nData);
9,807✔
3969
    }
3970
  } else if (forward) {  // VALUE ==> NULL
×
3971
    pColData->numOfValue--;
×
3972
    pColData->nVal--;
×
3973
    if (pColData->numOfValue) {
×
3974
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3975
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3976
      } else {
3977
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3978
      }
3979
      return tColDataAppendValue52(pColData, pData, nData);
×
3980
    } else {
3981
      pColData->flag = HAS_NONE;
×
3982
      pColData->nData = 0;
×
3983
      return tColDataAppendValue12(pColData, pData, nData);
×
3984
    }
3985
  }
3986
  return 0;
×
3987
}
3988
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,344,055✔
3989
  if (forward) {
2,344,055✔
3990
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
2,344,055✔
3991
      pColData->numOfNull--;
1,426,342✔
3992
      pColData->nVal--;
1,426,342✔
3993
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
1,426,342✔
3994
        pColData->nData -= TYPE_BYTES[pColData->type];
1,167,766✔
3995
      }
3996
      if (pColData->numOfNull) {
1,426,181✔
3997
        return tColDataAppendValue60(pColData, pData, nData);
94,498✔
3998
      } else {
3999
        pColData->flag = HAS_VALUE;
1,331,683✔
4000
        return tColDataAppendValue40(pColData, pData, nData);
1,331,683✔
4001
      }
4002
    } else {  // VALUE ==> VALUE
4003
      pColData->nVal--;
917,713✔
4004
      if (IS_VAR_DATA_TYPE(pColData->type)) {
917,713✔
4005
        pColData->nData = pColData->aOffset[pColData->nVal];
96,424✔
4006
      } else {
4007
        pColData->nData -= TYPE_BYTES[pColData->type];
821,289✔
4008
      }
4009
      return tColDataPutValue(pColData, pData, nData);
917,713✔
4010
    }
4011
  }
4012
  return 0;
×
4013
}
4014
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
936,634✔
4015
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
936,634✔
4016
    pColData->numOfValue--;
329,445✔
4017
    pColData->nVal--;
329,445✔
4018
    if (pColData->numOfValue) {
329,445✔
4019
      if (IS_VAR_DATA_TYPE(pColData->type)) {
329,445✔
4020
        pColData->nData = pColData->aOffset[pColData->nVal];
72,609✔
4021
      } else {
4022
        pColData->nData -= TYPE_BYTES[pColData->type];
256,836✔
4023
      }
4024
      return tColDataAppendValue62(pColData, pData, nData);
329,445✔
4025
    } else {
4026
      pColData->flag = HAS_NULL;
×
4027
      pColData->nData = 0;
×
4028
      return tColDataAppendValue20(pColData, pData, nData);
×
4029
    }
4030
  }
4031
  return 0;
607,189✔
4032
}
4033
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
19,519✔
4034
  int32_t code = 0;
19,519✔
4035

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

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

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

4143
  return tColDataUpdateValueImpl[pColData->flag][pColVal->flag](
2,147,483,647✔
4144
      pColData, VALUE_GET_DATUM(&pColVal->value, pColData->type), pColVal->value.nData, forward);
2,147,483,647✔
4145
}
4146

4147
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
471,125,331✔
4148
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
471,125,331✔
4149
}
471,132,562✔
4150
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,476,017,251✔
4151
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,476,017,251✔
4152
}
1,476,020,648✔
4153
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
460,988✔
4154
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4155
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
460,988✔
4156
    case 0:
42,124✔
4157
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
42,124✔
4158
      break;
42,124✔
4159
    case 1:
418,864✔
4160
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
418,864✔
4161
      break;
418,864✔
4162
    default:
×
4163
      break;
×
4164
  }
4165
}
460,988✔
4166
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4167
  SValue value = {.type = pColData->type};
2,147,483,647✔
4168
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
4169
    if (iVal + 1 < pColData->nVal) {
2,147,483,647✔
4170
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
2,147,483,647✔
4171
    } else {
4172
      value.nData = pColData->nData - pColData->aOffset[iVal];
87,892,861✔
4173
    }
4174
    value.pData = pColData->pData + pColData->aOffset[iVal];
2,147,483,647✔
4175
  } else {
4176
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4177
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4178
  }
4179
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4180
}
2,147,483,647✔
4181
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
373,353,981✔
4182
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4183
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
373,353,981✔
4184
    case 0:
171,363,088✔
4185
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
171,363,088✔
4186
      break;
171,360,816✔
4187
    case 1:
202,029,069✔
4188
      tColDataGetValue4(pColData, iVal, pColVal);
4189
      break;
202,023,286✔
4190
    default:
×
4191
      break;
×
4192
  }
4193
}
373,384,102✔
4194
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
2,147,483,647✔
4195
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4196
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4197
    case 0:
1,541,164,815✔
4198
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,541,164,815✔
4199
      break;
1,541,368,931✔
4200
    case 1:
2,147,483,647✔
4201
      tColDataGetValue4(pColData, iVal, pColVal);
4202
      break;
2,147,483,647✔
4203
    default:
×
4204
      break;
×
4205
  }
4206
}
2,147,483,647✔
4207
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
4,194,664✔
4208
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4209
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
4,194,664✔
4210
    case 0:
549,784✔
4211
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
549,784✔
4212
      break;
549,784✔
4213
    case 1:
21,128✔
4214
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
21,128✔
4215
      break;
21,128✔
4216
    case 2:
3,623,752✔
4217
      tColDataGetValue4(pColData, iVal, pColVal);
4218
      break;
3,623,752✔
4219
    default:
×
4220
      break;
×
4221
  }
4222
}
4,194,664✔
4223
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4224
    NULL,               // 0
4225
    tColDataGetValue1,  // HAS_NONE
4226
    tColDataGetValue2,  // HAS_NULL
4227
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4228
    tColDataGetValue4,  // HAS_VALUE
4229
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4230
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4231
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4232
};
4233
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4234
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647✔
4235
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647✔
4236
    return TSDB_CODE_INVALID_PARA;
×
4237
  }
4238
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4239
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4240
}
4241

4242
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal) {
2,147,483,647✔
4243
  switch (pColData->flag) {
2,147,483,647✔
4244
    case HAS_NONE:
×
4245
      return 0;
×
4246
    case HAS_NULL:
×
4247
      return 1;
×
4248
    case (HAS_NULL | HAS_NONE):
460,460✔
4249
      return GET_BIT1(pColData->pBitMap, iVal);
460,460✔
4250
    case HAS_VALUE:
×
4251
      return 2;
×
4252
    case (HAS_VALUE | HAS_NONE):
1,993,498,969✔
4253
      return (GET_BIT1(pColData->pBitMap, iVal)) ? 2 : 0;
1,993,498,969✔
4254
    case (HAS_VALUE | HAS_NULL):
2,147,483,647✔
4255
      return GET_BIT1(pColData->pBitMap, iVal) + 1;
2,147,483,647✔
4256
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
80,865,584✔
4257
      return GET_BIT2(pColData->pBitMap, iVal);
80,865,584✔
4258
    default:
×
4259
      return 0;
×
4260
  }
4261
}
4262

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

4266
  *pColData = *pColDataFrom;
2,511,239✔
4267

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

4293
  // offset
4294
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
2,511,645✔
4295
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
186,123✔
4296
    if (pColData->aOffset == NULL) {
186,123✔
4297
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4298
      goto _exit;
×
4299
    }
4300
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
186,123✔
4301
  } else {
4302
    pColData->aOffset = NULL;
2,325,522✔
4303
  }
4304

4305
  // value
4306
  if (pColData->nData) {
2,511,619✔
4307
    pColData->pData = xMalloc(arg, pColData->nData);
2,478,110✔
4308
    if (pColData->pData == NULL) {
2,478,032✔
4309
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4310
      goto _exit;
×
4311
    }
4312

4313
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
2,478,084✔
4314
  } else {
4315
    pColData->pData = NULL;
33,492✔
4316
  }
4317

4318
_exit:
2,511,645✔
4319
  return code;
2,511,645✔
4320
}
4321

4322
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
400,187,845✔
4323
  int32_t code;
4324
  SBuffer local;
400,127,108✔
4325

4326
  if (!(colData->nVal > 0)) {
400,216,318✔
4327
    return TSDB_CODE_INVALID_PARA;
×
4328
  }
4329

4330
  (*info) = (SColDataCompressInfo){
400,220,824✔
4331
      .cmprAlg = info->cmprAlg,
400,219,205✔
4332
      .columnFlag = colData->cflag,
400,216,453✔
4333
      .flag = colData->flag,
400,215,912✔
4334
      .dataType = colData->type,
400,222,107✔
4335
      .columnId = colData->cid,
400,221,738✔
4336
      .numOfData = colData->nVal,
400,195,719✔
4337
  };
4338

4339
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
400,216,009✔
4340
    return 0;
14,752,696✔
4341
  }
4342

4343
  tBufferInit(&local);
4344
  if (assist == NULL) {
385,454,132✔
4345
    assist = &local;
×
4346
  }
4347

4348
  // bitmap
4349
  if (colData->flag != HAS_VALUE) {
385,454,132✔
4350
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
23,053,770✔
4351
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,501,924✔
4352
    } else {
4353
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
21,551,485✔
4354
    }
4355

4356
    SCompressInfo cinfo = {
23,051,879✔
4357
        .dataType = TSDB_DATA_TYPE_TINYINT,
4358
        .cmprAlg = info->cmprAlg,
23,053,049✔
4359
        .originalSize = info->bitmapOriginalSize,
23,053,049✔
4360
    };
4361

4362
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
23,053,600✔
4363
    if (code) {
23,053,254✔
4364
      tBufferDestroy(&local);
4365
      return code;
×
4366
    }
4367

4368
    info->bitmapCompressedSize = cinfo.compressedSize;
23,053,254✔
4369
  }
4370

4371
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
385,445,907✔
4372
    tBufferDestroy(&local);
4373
    return 0;
19,612✔
4374
  }
4375

4376
  // offset
4377
  if (IS_VAR_DATA_TYPE(colData->type)) {
385,411,997✔
4378
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
45,682,979✔
4379

4380
    SCompressInfo cinfo = {
45,679,126✔
4381
        .dataType = TSDB_DATA_TYPE_INT,
4382
        .cmprAlg = info->cmprAlg,
45,682,167✔
4383
        .originalSize = info->offsetOriginalSize,
45,680,970✔
4384
    };
4385

4386
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
45,680,022✔
4387
    if (code) {
45,683,246✔
4388
      tBufferDestroy(&local);
4389
      return code;
×
4390
    }
4391

4392
    info->offsetCompressedSize = cinfo.compressedSize;
45,683,246✔
4393
  }
4394

4395
  // data
4396
  if (colData->nData > 0) {
385,447,831✔
4397
    info->dataOriginalSize = colData->nData;
385,434,178✔
4398

4399
    SCompressInfo cinfo = {
385,438,985✔
4400
        .dataType = colData->type,
385,440,446✔
4401
        .cmprAlg = info->cmprAlg,
385,434,293✔
4402
        .originalSize = info->dataOriginalSize,
385,434,560✔
4403
    };
4404

4405
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
385,436,122✔
4406
    if (code) {
385,419,965✔
4407
      tBufferDestroy(&local);
4408
      return code;
×
4409
    }
4410

4411
    info->dataCompressedSize = cinfo.compressedSize;
385,419,965✔
4412
  }
4413

4414
  tBufferDestroy(&local);
4415
  return 0;
385,430,980✔
4416
}
4417

4418
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
480,151,841✔
4419
  int32_t  code;
4420
  SBuffer  local;
480,116,824✔
4421
  uint8_t *data = (uint8_t *)input;
480,204,867✔
4422

4423
  tBufferInit(&local);
4424
  if (assist == NULL) {
480,196,067✔
4425
    assist = &local;
×
4426
  }
4427

4428
  tColDataClear(colData);
480,196,067✔
4429
  colData->cid = info->columnId;
480,208,060✔
4430
  colData->type = info->dataType;
480,219,103✔
4431
  colData->cflag = info->columnFlag;
480,222,574✔
4432
  colData->nVal = info->numOfData;
480,209,443✔
4433
  colData->flag = info->flag;
480,209,852✔
4434

4435
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
480,169,579✔
4436
    goto _exit;
62,872,738✔
4437
  }
4438

4439
  // bitmap
4440
  if (info->bitmapOriginalSize > 0) {
417,291,728✔
4441
    SCompressInfo cinfo = {
57,088,375✔
4442
        .dataType = TSDB_DATA_TYPE_TINYINT,
4443
        .cmprAlg = info->cmprAlg,
57,092,213✔
4444
        .originalSize = info->bitmapOriginalSize,
57,092,834✔
4445
        .compressedSize = info->bitmapCompressedSize,
57,090,931✔
4446
    };
4447

4448
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
57,086,521✔
4449
    if (code) {
57,087,527✔
4450
      tBufferDestroy(&local);
4451
      return code;
×
4452
    }
4453

4454
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
57,087,527✔
4455
    if (code) {
57,083,217✔
4456
      tBufferDestroy(&local);
4457
      return code;
×
4458
    }
4459

4460
    data += cinfo.compressedSize;
57,083,217✔
4461
  }
4462

4463
  if (info->flag == (HAS_NONE | HAS_NULL)) {
417,302,069✔
4464
    goto _exit;
21,252✔
4465
  }
4466

4467
  // offset
4468
  if (info->offsetOriginalSize > 0) {
417,278,227✔
4469
    SCompressInfo cinfo = {
82,179,376✔
4470
        .cmprAlg = info->cmprAlg,
82,179,560✔
4471
        .dataType = TSDB_DATA_TYPE_INT,
4472
        .originalSize = info->offsetOriginalSize,
82,173,701✔
4473
        .compressedSize = info->offsetCompressedSize,
82,174,487✔
4474
    };
4475

4476
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
82,175,831✔
4477
    if (code) {
82,168,271✔
4478
      tBufferDestroy(&local);
4479
      return code;
×
4480
    }
4481

4482
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
82,168,271✔
4483
    if (code) {
82,155,153✔
4484
      tBufferDestroy(&local);
4485
      return code;
×
4486
    }
4487

4488
    data += cinfo.compressedSize;
82,155,153✔
4489
  }
4490

4491
  // data
4492
  if (info->dataOriginalSize > 0) {
417,274,178✔
4493
    colData->nData = info->dataOriginalSize;
417,214,718✔
4494

4495
    SCompressInfo cinfo = {
417,226,113✔
4496
        .cmprAlg = info->cmprAlg,
834,445,929✔
4497
        .dataType = colData->type,
417,213,013✔
4498
        .originalSize = info->dataOriginalSize,
417,219,888✔
4499
        .compressedSize = info->dataCompressedSize,
417,216,789✔
4500
    };
4501

4502
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
417,205,592✔
4503
    if (code) {
417,219,544✔
4504
      tBufferDestroy(&local);
4505
      return code;
×
4506
    }
4507

4508
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
417,219,544✔
4509
    if (code) {
417,191,177✔
4510
      tBufferDestroy(&local);
4511
      return code;
×
4512
    }
4513

4514
    data += cinfo.compressedSize;
417,191,177✔
4515
  }
4516

4517
_exit:
480,176,766✔
4518
  switch (colData->flag) {
480,206,486✔
4519
    case HAS_NONE:
11,349,151✔
4520
      colData->numOfNone = colData->nVal;
11,349,151✔
4521
      break;
11,349,151✔
4522
    case HAS_NULL:
51,499,970✔
4523
      colData->numOfNull = colData->nVal;
51,499,970✔
4524
      break;
51,524,408✔
4525
    case HAS_VALUE:
360,169,661✔
4526
      colData->numOfValue = colData->nVal;
360,169,661✔
4527
      break;
360,201,655✔
4528
    default:
57,090,487✔
4529
      for (int32_t i = 0; i < colData->nVal; i++) {
2,147,483,647✔
4530
        uint8_t bitValue = tColDataGetBitValue(colData, i);
2,147,483,647✔
4531
        if (bitValue == 0) {
2,147,483,647✔
4532
          colData->numOfNone++;
197,970,140✔
4533
        } else if (bitValue == 1) {
2,147,483,647✔
4534
          colData->numOfNull++;
2,147,483,647✔
4535
        } else {
4536
          colData->numOfValue++;
2,147,483,647✔
4537
        }
4538
      }
4539
  }
4540
  tBufferDestroy(&local);
4541
  return 0;
480,134,213✔
4542
}
4543

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

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

4619
    if (allValue) {
15,332✔
4620
      // optimize (todo)
4621
      for (int32_t i = 0; i < nRows; ++i) {
28,295✔
4622
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
17,769✔
4623
      }
4624
    } else if (allNull) {
4,806✔
4625
      // optimize (todo)
4626
      for (int32_t i = 0; i < nRows; ++i) {
13,443✔
4627
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
8,933✔
4628
        if (code) goto _exit;
8,933✔
4629
      }
4630
    } else {
4631
      for (int32_t i = 0; i < nRows; ++i) {
2,146✔
4632
        if (BMIsNull(lengthOrbitmap, i)) {
1,850✔
4633
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1,443✔
4634
          if (code) goto _exit;
1,443✔
4635
        } else {
4636
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
407✔
4637
        }
4638
      }
4639
    }
4640
  }
4641

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

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

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

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

4747
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32_t buffMaxLen, initGeosFn igeos,
2,147,483,647✔
4748
                               checkWKBGeometryFn cgeos) {
4749
  int32_t code = 0;
2,147,483,647✔
4750

4751
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
2,147,483,647✔
4752
    if (!(pColData->type == pBind->buffer_type)) {
2,147,483,647✔
4753
      return TSDB_CODE_INVALID_PARA;
×
4754
    }
4755
  }
4756

4757
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
2,147,483,647✔
4758
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
480,614,386✔
4759
      code = igeos();
6,900,856✔
4760
      if (code) {
6,882,015✔
4761
        return code;
×
4762
      }
4763
    }
4764
    for (int32_t i = 0; i < pBind->num; ++i) {
953,669,089✔
4765
      if (pBind->is_null && pBind->is_null[i]) {
480,220,696✔
4766
        if (pColData->cflag & COL_IS_KEY) {
23,190,658✔
4767
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,294✔
4768
          goto _exit;
1,294✔
4769
        }
4770
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
23,190,036✔
4771
        if (code) goto _exit;
23,183,988✔
4772
      } else if (pBind->length[i] > buffMaxLen) {
457,218,239✔
4773
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4774
      } else {
4775
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
457,136,226✔
4776
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
6,921,045✔
4777
          if (code) {
6,852,264✔
4778
            uError("stmt col[%d] bind geometry wrong format", i);
×
4779
            goto _exit;
×
4780
          }
4781
        }
4782
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
912,822,988✔
4783
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
457,426,771✔
4784
      }
4785
    }
4786
  } else {  // fixed-length data type
4787
    bool allValue;
4788
    bool allNull;
4789
    if (pBind->is_null) {
2,147,483,647✔
4790
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
114,112,239✔
4791
      allNull = (same && pBind->is_null[0] != 0);
113,572,168✔
4792
      allValue = (same && pBind->is_null[0] == 0);
114,092,982✔
4793
    } else {
4794
      allNull = false;
2,147,483,647✔
4795
      allValue = true;
2,147,483,647✔
4796
    }
4797

4798
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
2,147,483,647✔
4799
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
2,588✔
4800
      goto _exit;
2,588✔
4801
    }
4802

4803
    if (allValue) {
2,147,483,647✔
4804
      // optimize (todo)
4805
      for (int32_t i = 0; i < pBind->num; ++i) {
2,147,483,647✔
4806
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,147,483,647✔
4807
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
2,147,483,647✔
4808
      }
4809
    } else if (allNull) {
121,639,073✔
4810
      // optimize (todo)
4811
      for (int32_t i = 0; i < pBind->num; ++i) {
225,720,111✔
4812
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
112,851,394✔
4813
        if (code) goto _exit;
112,905,154✔
4814
      }
4815
    } else {
4816
      for (int32_t i = 0; i < pBind->num; ++i) {
17,100,008✔
4817
        if (pBind->is_null[i]) {
8,275,892✔
4818
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,658,686✔
4819
          if (code) goto _exit;
3,658,686✔
4820
        } else {
4821
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
9,234,412✔
4822
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,617,206✔
4823
        }
4824
      }
4825
    }
4826
  }
4827

4828
_exit:
57,499,042✔
4829
  return code;
2,147,483,647✔
4830
}
4831

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

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

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

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

4883
    uint8_t *buf = pBind->buffer;
9,520✔
4884

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

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

4928
_exit:
×
4929
  return code;
9,520✔
4930
}
4931

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

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

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

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

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

4978
_exit:
×
4979
  return code;
×
4980
}
4981

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

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

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

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

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

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

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

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

5096
  int32_t code = 0;
15,039,416✔
5097
  int32_t numOfRows = -1;
15,039,416✔
5098
  SArray *colValArray, *bufArray;
5099
  SColVal colVal;
1,873,610✔
5100
  int32_t numOfFixedValue = 0;
15,033,065✔
5101
  int32_t lino = 0;
15,033,065✔
5102

5103
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
15,039,161✔
5104
    return terrno;
×
5105
  }
5106
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
15,040,413✔
5107
    taosArrayDestroy(colValArray);
×
5108
    return terrno;
×
5109
  }
5110
  for (int i = 0; i < numOfInfos; ++i) {
97,939,272✔
5111
    if (parsedCols) {
82,895,450✔
5112
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
×
5113
      if (pParsedVal) {
×
5114
        continue;
×
5115
      }
5116
    }
5117
    if (numOfRows == -1) {
82,895,450✔
5118
      numOfRows = infos[i].bind->num;
15,041,101✔
5119
    }
5120

5121
    if (!taosArrayPush(bufArray, &infos[i].bind->buffer)) {
165,792,640✔
5122
      taosArrayDestroy(colValArray);
×
5123
      taosArrayDestroy(bufArray);
×
5124
      return terrno;
×
5125
    }
5126
  }
5127

5128
  SRowKey rowKey, lastRowKey;
1,875,898✔
5129
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
167,938,845✔
5130
    taosArrayClear(colValArray);
152,941,283✔
5131

5132
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
803,642,299✔
5133
      if (parsedCols) {
649,939,250✔
5134
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
×
5135
        if (pParsedVal) {
×
5136
          numOfFixedValue++;
×
5137
          colVal = *pParsedVal;
×
5138

5139
          if (taosArrayPush(colValArray, &colVal) == NULL) {
×
5140
            if (IS_VAR_DATA_TYPE(pParsedVal->value.type)) {
×
5141
              taosMemoryFree(colVal.value.pData);
×
5142
            }
5143
            code = terrno;
×
5144
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5145
          }
5146
          continue;
×
5147
        }
5148
      }
5149

5150
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
650,000,191✔
5151
        if (infos[iInfo].bind->is_null[iRow] == 1) {
×
5152
          if (iInfo == 0) {
×
5153
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5154
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5155
          }
5156
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
×
5157
        } else {
5158
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5159
        }
5160
      } else {
5161
        SValue value = {
650,072,152✔
5162
            .type = infos[iInfo].type,
650,039,868✔
5163
        };
5164
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
650,080,061✔
5165
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
21,432,192✔
UNCOV
5166
            int32_t   length = infos[iInfo].bind->length[iRow];
×
5167
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5168
            value.nData = length;
×
5169
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5170
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5171
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5172
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5173
              goto _exit;
×
5174
            }
5175
            value.pData = *data;
×
5176
            *data += length;
×
5177
          } else {
5178
            int32_t   length = infos[iInfo].bind->length[iRow];
20,315,936✔
5179
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
20,315,936✔
5180
            value.nData = length;
20,316,190✔
5181
            if (value.nData > infos[iInfo].bytes - VARSTR_HEADER_SIZE) {
20,316,190✔
5182
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5183
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5184
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5185
              goto _exit;
×
5186
            }
5187
            value.pData = *data;
20,314,158✔
5188
            *data += length;
20,316,444✔
5189
          }
5190
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5191
        } else {
5192
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
629,971,445✔
5193
            if (!pSchemaExt) {
×
5194
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5195
              code = TSDB_CODE_PAR_INTERNAL_ERROR;
×
5196
              goto _exit;
×
5197
            }
5198
            uint8_t precision = 0, scale = 0;
×
5199
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
×
5200
            Decimal128 dec = {0};
×
5201
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5202
            int32_t    length = infos[iInfo].bind->length[iRow];
×
5203
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
×
5204
            *data += length;
×
5205
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5206

5207
            // precision check
5208
            // scale auto fit
5209

5210
            code = decimal128ToDataVal(&dec, &value);
×
5211
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5212

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

5228
            code = decimal64ToDataVal(&dec, &value);
×
5229
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5230

5231
          } else {
5232
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
629,969,404✔
5233
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
629,955,214✔
5234
              *val = 1;
×
5235
            }
5236
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
629,955,214✔
5237
          }
5238
        }
5239
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
650,897,396✔
5240
      }
5241
      if (taosArrayPush(colValArray, &colVal) == NULL) {
650,695,326✔
5242
        code = terrno;
×
5243
        goto _exit;
×
5244
      }
5245
    }
5246

5247
    SRow *row;
13,381,304✔
5248

5249
    if (hasBlob == 0) {
153,543,015✔
5250
      SRowBuildScanInfo sinfo = {0};
152,917,717✔
5251
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
152,926,178✔
5252
      TAOS_CHECK_GOTO(code, &lino, _exit);
152,909,611✔
5253
    } else {
5254
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
625,811✔
5255
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
625,811✔
5256
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5257
    }
5258

5259
    if ((taosArrayPush(rowArray, &row)) == NULL) {
152,854,370✔
5260
      code = terrno;
×
5261
      goto _exit;
×
5262
    }
5263

5264
    if (pOrdered && pDupTs) {
152,854,370✔
5265
      tRowGetKey(row, &rowKey);
305,741,716✔
5266
      if (iRow == 0) {
152,904,208✔
5267
        *pOrdered = true;
15,034,899✔
5268
        *pDupTs = false;
15,034,644✔
5269
      } else {
5270
        if (*pOrdered) {
137,869,309✔
5271
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
137,723,409✔
5272
          *pOrdered = (res >= 0);
137,723,409✔
5273
          if (!*pDupTs) {
137,872,645✔
5274
            *pDupTs = (res == 0);
137,872,023✔
5275
          }
5276
        }
5277
      }
5278
      lastRowKey = rowKey;
152,899,840✔
5279
    }
5280
  }
5281
_exit:
14,996,456✔
5282
  if (code != 0) {
14,997,307✔
5283
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
×
5284
  }
5285
  taosArrayDestroy(colValArray);
14,997,307✔
5286
  taosArrayDestroy(bufArray);
15,043,329✔
5287
  return code;
15,050,378✔
5288
}
5289
/* build rows to `rowArray` from bind
5290
 * `infos` is the bind information array
5291
 * `numOfInfos` is the number of bind information
5292
 * `infoSorted` is whether the bind information is sorted by column id
5293
 * `pTSchema` is the schema of the table
5294
 * `rowArray` is the array to store the rows
5295
 * `pOrdered` is the pointer to store ordered
5296
 * `pDupTs` is the pointer to store duplicateTs
5297
 */
5298
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
×
5299
                                   SArray *rowArray, bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5300
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
×
5301
    return TSDB_CODE_INVALID_PARA;
×
5302
  }
5303
  int8_t hasBlob = schemaHasBlob(pTSchema);
×
5304
  if (!infoSorted) {
×
5305
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5306
  }
5307

5308
  int32_t code = 0;
×
5309
  int32_t numOfRows = infos[0].bind->num;
×
5310
  SArray *colValArray, *bufArray;
5311
  SColVal colVal;
×
5312

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

5328
  SRowKey rowKey, lastRowKey;
×
5329
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
×
5330
    taosArrayClear(colValArray);
×
5331

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

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

5390
    SRow *row;
×
5391

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

5404
    if ((taosArrayPush(rowArray, &row)) == NULL) {
×
5405
      code = terrno;
×
5406
      goto _exit;
×
5407
    }
5408

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

5432
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
44,136,706✔
5433
  int32_t code = TSDB_CODE_SUCCESS;
44,136,706✔
5434

5435
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
44,136,706✔
5436
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
5,557,841✔
5437
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
5,002,771✔
5438
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
10,560,612✔
5439
    if (iToRow == 0) {
5,557,841✔
5440
      pToColData->aOffset[iToRow] = 0;
11,654✔
5441
    }
5442

5443
    if (iToRow < pToColData->nVal - 1) {
5,557,841✔
5444
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
5,546,980✔
5445
    }
5446

5447
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
5,557,841✔
5448
                 nData);
5449
  } else {
5450
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
38,578,865✔
5451
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
38,578,865✔
5452
  }
5453
  return code;
44,136,706✔
5454
}
5455

5456
static int32_t tColDataCopyRowSingleCol(SColData *pFromColData, int32_t iFromRow, SColData *pToColData,
45,724,202✔
5457
                                        int32_t iToRow) {
5458
  int32_t code = TSDB_CODE_SUCCESS;
45,724,202✔
5459
  int     bit_val = 0;
45,724,202✔
5460

5461
  switch (pFromColData->flag) {
45,724,202✔
5462
    case HAS_NONE: {
×
5463
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5464
    } break;
×
5465
    case HAS_NULL: {
1,587,496✔
5466
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
1,587,496✔
5467
    } break;
1,587,496✔
5468
    case (HAS_NULL | HAS_NONE): {
×
5469
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5470
      if (0 == bit_val)
×
5471
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5472
      else
5473
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
×
5474
    } break;
×
5475
    case HAS_VALUE: {
4,931,098✔
5476
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
4,931,098✔
5477
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
4,931,098✔
5478
    } break;
4,931,098✔
5479
    case (HAS_VALUE | HAS_NONE): {
×
5480
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5481
      if (0 == bit_val)
×
5482
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5483
      else
5484
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
×
5485
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5486
    } break;
×
5487
    case (HAS_VALUE | HAS_NULL): {
39,205,608✔
5488
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
39,205,608✔
5489
      if (0 == bit_val)
39,205,608✔
5490
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
17,698,349✔
5491
      else
5492
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
21,507,259✔
5493
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
39,205,608✔
5494
    } break;
39,205,608✔
5495
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
5496
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
5497
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5498
    } break;
×
5499
    default:
×
5500
      return -1;
×
5501
  }
5502

5503
  return code;
45,724,202✔
5504
}
5505

5506
static int32_t tColDataCopyRow(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t iToRow,
2,859,667✔
5507
                               int32_t nColData) {
5508
  int32_t code = TSDB_CODE_SUCCESS;
2,859,667✔
5509

5510
  for (int32_t i = 0; i < nColData; i++) {
48,583,869✔
5511
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
45,724,202✔
5512
    if (code != TSDB_CODE_SUCCESS) {
45,724,202✔
5513
      return code;
×
5514
    }
5515
  }
5516

5517
  return code;
2,859,667✔
5518
}
5519

5520
static int32_t tColDataCopyRowAppend(SColData *aFromColData, int32_t iFromRow, SColData *aToColData, int32_t nColData) {
2,859,667✔
5521
  int32_t code = TSDB_CODE_SUCCESS;
2,859,667✔
5522

5523
  for (int32_t i = 0; i < nColData; i++) {
48,583,869✔
5524
    SColVal cv = {0};
45,724,202✔
5525
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
45,724,202✔
5526
    if (code != TSDB_CODE_SUCCESS) {
45,724,202✔
5527
      return code;
×
5528
    }
5529
    code = tColDataAppendValue(&aToColData[i], &cv);
45,724,202✔
5530
    if (code != TSDB_CODE_SUCCESS) {
45,724,202✔
5531
      return code;
×
5532
    }
5533
  }
5534

5535
  return code;
2,859,667✔
5536
}
5537

5538
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key) {
1,132,920,356✔
5539
  SColVal cv;
1,086,804,879✔
5540

5541
  key->ts = ((TSKEY *)aColData[0].pData)[iRow];
1,133,898,007✔
5542
  key->numOfPKs = 0;
1,134,585,255✔
5543

5544
  for (int i = 1; i < nColData; i++) {
1,139,593,468✔
5545
    if (aColData[i].cflag & COL_IS_KEY) {
1,138,499,390✔
5546
      tColDataGetValue4(&aColData[i], iRow, &cv);
4,990,906✔
5547
      key->pks[key->numOfPKs++] = cv.value;
4,990,906✔
5548
    } else {
5549
      break;
1,133,550,111✔
5550
    }
5551
  }
5552
}
1,133,616,098✔
5553

5554
static int32_t tColDataMergeSortMerge(SColData *aColData, int32_t start, int32_t mid, int32_t end, int32_t nColData) {
317,643✔
5555
  SColData *aDstColData = NULL;
317,643✔
5556
  int32_t   i = start, j = mid + 1, k = 0;
317,643✔
5557
  SRowKey   keyi, keyj;
317,643✔
5558

5559
  if (end > start) {
317,643✔
5560
    aDstColData = taosMemoryCalloc(1, sizeof(SColData) * nColData);
317,643✔
5561
    if (aDstColData == NULL) {
317,643✔
5562
      return terrno;
×
5563
    }
5564
    for (int c = 0; c < nColData; ++c) {
5,390,581✔
5565
      tColDataInit(&aDstColData[c], aColData[c].cid, aColData[c].type, aColData[c].cflag);
5,072,938✔
5566
    }
5567
  }
5568

5569
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
317,643✔
5570
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
317,643✔
5571
  while (i <= mid && j <= end) {
1,747,563✔
5572
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
1,429,920✔
5573
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,086✔
5574
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
1,086✔
5575
    } else {
5576
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,428,834✔
5577
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
1,428,834✔
5578
    }
5579
  }
5580

5581
  while (i <= mid) {
1,746,551✔
5582
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,428,908✔
5583
  }
5584

5585
  while (j <= end) {
318,482✔
5586
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
839✔
5587
  }
5588

5589
  for (i = start, k = 0; i <= end; ++i, ++k) {
3,177,310✔
5590
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
2,859,667✔
5591
  }
5592

5593
  if (aDstColData) {
317,643✔
5594
    for (int32_t i = 0; i < nColData; i++) {
5,390,581✔
5595
      tColDataDestroy(&aDstColData[i]);
5,072,938✔
5596
    }
5597
    taosMemoryFree(aDstColData);
317,643✔
5598
  }
5599

5600
  return TSDB_CODE_SUCCESS;
317,643✔
5601
}
5602

5603
static int32_t tColDataMergeSort(SColData *aColData, int32_t start, int32_t end, int32_t nColData) {
636,112✔
5604
  int32_t ret = TSDB_CODE_SUCCESS;
636,112✔
5605
  int32_t mid;
5606

5607
  if (start >= end) {
636,112✔
5608
    return TSDB_CODE_SUCCESS;
318,469✔
5609
  }
5610

5611
  mid = (start + end) / 2;
317,643✔
5612

5613
  ret = tColDataMergeSort(aColData, start, mid, nColData);
317,643✔
5614
  if (ret != TSDB_CODE_SUCCESS) {
317,643✔
5615
    return ret;
×
5616
  }
5617

5618
  ret = tColDataMergeSort(aColData, mid + 1, end, nColData);
317,643✔
5619
  if (ret != TSDB_CODE_SUCCESS) {
317,643✔
5620
    return ret;
×
5621
  }
5622

5623
  return tColDataMergeSortMerge(aColData, start, mid, end, nColData);
317,643✔
5624
}
5625

5626
static int32_t tColDataSort(SColData *aColData, int32_t nColData) {
826✔
5627
  int32_t nVal = aColData[0].nVal;
826✔
5628

5629
  if (nVal < 2) return TSDB_CODE_SUCCESS;
826✔
5630

5631
  return tColDataMergeSort(aColData, 0, nVal - 1, nColData);
826✔
5632
}
5633

5634
static int32_t tColDataMerge(SArray **colArr) {
99✔
5635
  int32_t code = 0;
99✔
5636
  SArray *src = *colArr;
99✔
5637
  SArray *dst = NULL;
99✔
5638

5639
  dst = taosArrayInit(taosArrayGetSize(src), sizeof(SColData));
99✔
5640
  if (dst == NULL) {
99✔
5641
    return terrno;
×
5642
  }
5643

5644
  for (int32_t i = 0; i < taosArrayGetSize(src); i++) {
396✔
5645
    SColData *srcCol = taosArrayGet(src, i);
297✔
5646

5647
    SColData *dstCol = taosArrayReserve(dst, 1);
297✔
5648
    if (dstCol == NULL) {
297✔
5649
      code = terrno;
×
5650
      goto _exit;
×
5651
    }
5652
    tColDataInit(dstCol, srcCol->cid, srcCol->type, srcCol->cflag);
297✔
5653
  }
5654

5655
  int32_t numRows = ((SColData *)TARRAY_DATA(src))->nVal;
99✔
5656
  SRowKey lastKey;
99✔
5657
  for (int32_t i = 0; i < numRows; i++) {
297✔
5658
    SRowKey key;
198✔
5659
    tColDataArrGetRowKey((SColData *)TARRAY_DATA(src), taosArrayGetSize(src), i, &key);
198✔
5660

5661
    if (i == 0 || tRowKeyCompare(&key, &lastKey) != 0) {  // append new row
297✔
5662
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
396✔
5663
        SColData *srcCol = taosArrayGet(src, j);
297✔
5664
        SColData *dstCol = taosArrayGet(dst, j);
297✔
5665

5666
        SColVal cv;
297✔
5667
        code = tColDataGetValue(srcCol, i, &cv);
297✔
5668
        if (code != TSDB_CODE_SUCCESS) {
297✔
5669
          goto _exit;
×
5670
        }
5671
        code = tColDataAppendValue(dstCol, &cv);
297✔
5672
        if (code) {
297✔
5673
          goto _exit;
×
5674
        }
5675
      }
5676
      lastKey = key;
99✔
5677
    } else {  // update existing row
5678
      for (int32_t j = 0; j < taosArrayGetSize(src); j++) {
396✔
5679
        SColData *srcCol = taosArrayGet(src, j);
297✔
5680
        SColData *dstCol = taosArrayGet(dst, j);
297✔
5681

5682
        SColVal cv;
297✔
5683
        code = tColDataGetValue(srcCol, i, &cv);
297✔
5684
        if (code != TSDB_CODE_SUCCESS) {
297✔
5685
          goto _exit;
×
5686
        }
5687
        code = tColDataUpdateValue(dstCol, &cv, true);
297✔
5688
        if (code) {
297✔
5689
          goto _exit;
×
5690
        }
5691
      }
5692
    }
5693
  }
5694

5695
_exit:
99✔
5696
  if (code) {
99✔
5697
    taosArrayDestroyEx(dst, tColDataDestroy);
×
5698
  } else {
5699
    taosArrayDestroyEx(src, tColDataDestroy);
99✔
5700
    *colArr = dst;
99✔
5701
  }
5702
  return code;
99✔
5703
}
5704

5705
int32_t tColDataSortMerge(SArray **arr) {
718,385✔
5706
  SArray   *colDataArr = *arr;
718,385✔
5707
  int32_t   nColData = TARRAY_SIZE(colDataArr);
718,417✔
5708
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
718,449✔
5709

5710
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
718,449✔
5711
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5712
  }
5713
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
718,545✔
5714
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5715
  }
5716
  if (!(aColData[0].flag == HAS_VALUE)) {
718,545✔
5717
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5718
  }
5719

5720
  if (aColData[0].nVal <= 1) goto _exit;
718,417✔
5721

5722
  int8_t doSort = 0;
692,034✔
5723
  int8_t doMerge = 0;
692,034✔
5724
  // scan -------
5725
  SRowKey lastKey;
591,982✔
5726
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
692,066✔
5727
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
626,362,592✔
5728
    SRowKey key;
579,958,513✔
5729
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
625,231,857✔
5730

5731
    int32_t c = tRowKeyCompare(&lastKey, &key);
625,867,399✔
5732
    if (c < 0) {
625,867,399✔
5733
      lastKey = key;
625,486,101✔
5734
      continue;
625,486,101✔
5735
    } else if (c > 0) {
381,298✔
5736
      doSort = 1;
826✔
5737
      break;
826✔
5738
    } else {
5739
      doMerge = 1;
380,472✔
5740
    }
5741
  }
5742

5743
  // sort -------
5744
  if (doSort) {
335,191✔
5745
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
826✔
5746
  }
5747

5748
  if ((doMerge != 1) && (doSort == 1)) {
345,184✔
5749
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
826✔
5750
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
318,469✔
5751
      SRowKey key;
317,643✔
5752
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
317,643✔
5753

5754
      int32_t c = tRowKeyCompare(&lastKey, &key);
317,643✔
5755
      if (c == 0) {
317,643✔
5756
        doMerge = 1;
×
5757
        break;
×
5758
      }
5759
      lastKey = key;
317,643✔
5760
    }
5761
  }
5762

5763
  // merge -------
5764
  if (doMerge) {
692,452✔
5765
    int32_t code = tColDataMerge(arr);
99✔
5766
    if (code) return code;
99✔
5767
  }
5768

5769
_exit:
717,664✔
5770
  return 0;
718,899✔
5771
}
5772

5773
int32_t tColDataSortMergeWithBlob(SArray **arr, SBlobSet *pBlob) {
×
5774
  SArray   *colDataArr = *arr;
×
5775
  int32_t   nColData = TARRAY_SIZE(colDataArr);
×
5776
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
×
5777

5778
  if (!(aColData[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
×
5779
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5780
  }
5781
  if (!(aColData[0].cid == PRIMARYKEY_TIMESTAMP_COL_ID)) {
×
5782
    return TSDB_CODE_PAR_INVALID_FIRST_COLUMN;
×
5783
  }
5784
  if (!(aColData[0].flag == HAS_VALUE)) {
×
5785
    return TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5786
  }
5787

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

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

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

5818
  if ((doMerge != 1) && (doSort == 1)) {
×
5819
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
×
5820
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
×
5821
      SRowKey key;
×
5822
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
×
5823

5824
      int32_t c = tRowKeyCompare(&lastKey, &key);
×
5825
      if (c == 0) {
×
5826
        doMerge = 1;
×
5827
        break;
×
5828
      }
5829
      lastKey = key;
×
5830
    }
5831
  }
5832

5833
  // merge -------
5834
  if (doMerge) {
×
5835
    int32_t code = tColDataMerge(arr);
×
5836
    if (code) return code;
×
5837
  }
5838

5839
_exit:
×
5840
  return 0;
×
5841
}
5842

5843
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
7,604,339✔
5844
  int32_t code = 0;
7,604,339✔
5845

5846
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
15,210,148✔
5847
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
15,210,290✔
5848
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
15,209,810✔
5849
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
15,211,096✔
5850

5851
  // bitmap
5852
  switch (pColData->flag) {
7,605,767✔
5853
    case (HAS_NULL | HAS_NONE):
494,128✔
5854
    case (HAS_VALUE | HAS_NONE):
5855
    case (HAS_VALUE | HAS_NULL):
5856
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
494,128✔
5857
      if (code) return code;
494,128✔
5858
      break;
494,128✔
5859
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5860
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
5861
      if (code) return code;
×
5862
      break;
×
5863
    default:
7,111,122✔
5864
      break;
7,111,122✔
5865
  }
5866

5867
  // value
5868
  if (pColData->flag & HAS_VALUE) {
7,605,250✔
5869
    if (IS_VAR_DATA_TYPE(pColData->type)) {
7,547,449✔
5870
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
511,519✔
5871
      if (code) return code;
511,312✔
5872

5873
      code = tEncodeI32v(pEncoder, pColData->nData);
511,312✔
5874
      if (code) return code;
511,556✔
5875

5876
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
511,556✔
5877
      if (code) return code;
511,312✔
5878
    } else {
5879
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
7,036,634✔
5880
      if (code) return code;
7,034,928✔
5881
    }
5882
  }
5883

5884
  return code;
7,604,750✔
5885
}
5886

5887
static int32_t tDecodeColDataVersion0(SDecoder *pDecoder, SColData *pColData) {
3,665,670✔
5888
  int32_t code = 0;
3,665,670✔
5889

5890
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
7,331,357✔
5891
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
7,331,426✔
5892
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
7,331,504✔
5893
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
7,331,409✔
5894

5895
  if (pColData->type <= 0 || pColData->type >= TSDB_DATA_TYPE_MAX || pColData->flag <= 0 || pColData->flag >= 8) {
3,665,644✔
5896
    return TSDB_CODE_INVALID_PARA;
×
5897
  }
5898

5899
  // bitmap
5900
  switch (pColData->flag) {
3,665,713✔
5901
    case (HAS_NULL | HAS_NONE):
234,558✔
5902
    case (HAS_VALUE | HAS_NONE):
5903
    case (HAS_VALUE | HAS_NULL):
5904
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
234,558✔
5905
      if (code) return code;
234,558✔
5906
      break;
234,558✔
5907
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5908
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
5909
      if (code) return code;
×
5910
      break;
×
5911
    default:
3,431,051✔
5912
      break;
3,431,051✔
5913
  }
5914

5915
  // value
5916
  if (pColData->flag & HAS_VALUE) {
3,665,609✔
5917
    if (IS_VAR_DATA_TYPE(pColData->type)) {
3,634,193✔
5918
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
188,957✔
5919
      if (code) return code;
188,809✔
5920

5921
      code = tDecodeI32v(pDecoder, &pColData->nData);
188,809✔
5922
      if (code) return code;
188,809✔
5923

5924
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
188,809✔
5925
      if (code) return code;
188,809✔
5926
    } else {
5927
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
3,445,280✔
5928
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
3,445,306✔
5929
      if (code) return code;
3,445,332✔
5930
    }
5931
  }
5932
  pColData->cflag = 0;
3,665,609✔
5933

5934
  return code;
3,665,687✔
5935
}
5936

5937
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
7,605,530✔
5938
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
7,605,530✔
5939
  if (code) return code;
7,605,423✔
5940
  return tEncodeI8(pEncoder, pColData->cflag);
15,210,932✔
5941
}
5942

5943
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
3,665,722✔
5944
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
3,665,722✔
5945
  if (code) return code;
3,665,609✔
5946

5947
  code = tDecodeI8(pDecoder, &pColData->cflag);
3,665,609✔
5948
  return code;
3,665,739✔
5949
}
5950

5951
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
7,603,998✔
5952
  if (version == 0) {
7,603,998✔
5953
    return tEncodeColDataVersion0(pEncoder, pColData);
×
5954
  } else if (version == 1) {
7,603,998✔
5955
    return tEncodeColDataVersion1(pEncoder, pColData);
×
5956
  } else if (version == 2) {
7,603,998✔
5957
    int32_t posStart = pEncoder->pos;
7,604,791✔
5958
    pEncoder->pos += INT_BYTES;
7,605,207✔
5959
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
7,606,268✔
5960
    if (code) return code;
7,605,307✔
5961
    int32_t posEnd = pEncoder->pos;
7,605,307✔
5962
    int32_t pos = posEnd - posStart;
7,605,531✔
5963
    pEncoder->pos = posStart;
7,605,531✔
5964
    code = tEncodeI32(pEncoder, pos);
7,605,775✔
5965
    pEncoder->pos = posEnd;
7,605,775✔
5966
    return code;
7,605,470✔
5967
  } else {
5968
    return TSDB_CODE_INVALID_PARA;
×
5969
  }
5970
}
5971

5972
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
6,177,254✔
5973
  if (version == 0) {
6,177,254✔
5974
    return tDecodeColDataVersion0(pDecoder, pColData);
×
5975
  } else if (version == 1) {
6,177,254✔
5976
    return tDecodeColDataVersion1(pDecoder, pColData);
×
5977
  } else if (version == 2) {
6,177,254✔
5978
    if (jump) {
6,177,254✔
5979
      int32_t len = 0;
2,511,921✔
5980
      int32_t code = tDecodeI32(pDecoder, &len);
2,512,025✔
5981
      if (code) return code;
2,512,025✔
5982
      pDecoder->pos += (len - INT_BYTES);
2,512,025✔
5983
      return TSDB_CODE_SUCCESS;
2,512,025✔
5984
    } else {
5985
      pDecoder->pos += INT_BYTES;
3,665,333✔
5986
      return tDecodeColDataVersion1(pDecoder, pColData);
3,665,696✔
5987
    }    
5988
  } else {
UNCOV
5989
    return TSDB_CODE_INVALID_PARA;
×
5990
  }
5991
}
5992

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

5995
int32_t tDecodeRow(SDecoder *pDecoder, SRow **ppRow) {
2,147,483,647✔
5996
  if (ppRow == NULL) {
2,147,483,647✔
5997
    return TSDB_CODE_INVALID_PARA;
×
5998
  }
5999

6000
  if (pDecoder->pos + sizeof(SRow) > pDecoder->size) {
2,147,483,647✔
6001
    return TSDB_CODE_OUT_OF_RANGE;
×
6002
  }
6003

6004
  SRow *pRow = (SRow *)(pDecoder->data + pDecoder->pos);
2,147,483,647✔
6005
  return tDecodeBinaryWithSize(pDecoder, pRow->len, (uint8_t **)ppRow);
2,147,483,647✔
6006
}
6007

6008
int32_t tEncodeBlobSet(SEncoder *pEncoder, SBlobSet *pRow) {
71,668✔
6009
  int32_t code = 0;
71,668✔
6010
  int32_t lino = 0;
71,668✔
6011
  uint8_t compressed = 0;
71,668✔
6012
  int32_t compressSize = 0;
71,668✔
6013
  char   *p = NULL;
71,668✔
6014

6015
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pRow->type));
143,336✔
6016
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pRow->len));
143,336✔
6017

6018
  int32_t nSeq = taosArrayGetSize(pRow->pSeqTable);
71,668✔
6019
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, nSeq));
71,668✔
6020

6021
  // if (pRow->type) {
6022
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
71,668✔
6023
  while (pIter) {
171,380✔
6024
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
99,712✔
6025
    int32_t  idx = *(int32_t *)pIter;
99,712✔
6026
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
99,712✔
6027
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
99,712✔
6028

6029
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
99,712✔
6030
  }
6031
  //}
6032
  for (int32_t i = 0; i < nSeq; i++) {
171,380✔
6033
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
99,712✔
6034
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
199,424✔
6035
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
199,424✔
6036
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
199,424✔
6037
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
199,424✔
6038
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
199,424✔
6039
  }
6040

6041
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
143,336✔
6042

6043
_exit:
71,668✔
6044
  return code;
71,668✔
6045
}
6046

6047
int32_t tDecodeBlobSet(SDecoder *pDecoder, SBlobSet **pBlobSet) {
35,834✔
6048
  int32_t    code = 0;
35,834✔
6049
  int32_t    lino = 0;
35,834✔
6050
  int32_t    nSeq = 0;
35,834✔
6051
  uint8_t    compressd = 0;
35,834✔
6052
  int32_t    compressSize = 0;
35,834✔
6053
  SBlobSet  *pBlob = taosMemCalloc(1, sizeof(SBlobSet));
35,834✔
6054
  if (pBlob == NULL) {
35,834✔
6055
    TAOS_CHECK_EXIT(terrno);
×
6056
  }
6057
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pBlob->type));
71,668✔
6058
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pBlob->len));
71,668✔
6059

6060
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
35,834✔
6061

6062
  // if (pBlob->type) {
6063
  pBlob->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
35,834✔
6064
  if (pBlob->pSeqToffset == NULL) {
35,834✔
6065
    TAOS_CHECK_EXIT(terrno);
×
6066
  }
6067
  for (int32_t i = 0; i < nSeq; i++) {
85,690✔
6068
    uint64_t seq;
49,856✔
6069
    int32_t  idx;
49,856✔
6070
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &seq));
49,856✔
6071
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
49,856✔
6072

6073
    code = taosHashPut(pBlob->pSeqToffset, &seq, sizeof(seq), &idx, sizeof(idx));
49,856✔
6074
    TAOS_CHECK_EXIT(code);
49,856✔
6075
  }
6076
  //}
6077

6078
  pBlob->pSeqTable = taosArrayInit(nSeq, sizeof(SBlobValue));
35,834✔
6079
  if (pBlob->pSeqTable == NULL) {
35,834✔
6080
    TAOS_CHECK_EXIT(terrno);
×
6081
  }
6082

6083
  for (int32_t i = 0; i < nSeq; i++) {
85,690✔
6084
    SBlobValue value;
49,856✔
6085
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
49,856✔
6086
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
49,856✔
6087
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
49,856✔
6088
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
49,856✔
6089
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
49,856✔
6090
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
99,712✔
6091
      TAOS_CHECK_EXIT(terrno);
×
6092
    }
6093
  }
6094

6095
  pBlob->data = taosMemCalloc(1, pBlob->len);
35,834✔
6096
  if (pBlob->data == NULL) {
35,834✔
6097
    TAOS_CHECK_EXIT(terrno);
×
6098
  }
6099

6100
  TAOS_CHECK_EXIT(tDecodeFixed(pDecoder, pBlob->data, pBlob->len));
35,834✔
6101
  *pBlobSet = pBlob;
35,834✔
6102

6103
  uTrace("decode blob len:%d", (int32_t)(pBlob->len));
35,834✔
6104

6105
_exit:
35,834✔
6106
  if (code != 0) {
35,834✔
6107
    if (pBlob != NULL) {
×
6108
      taosMemoryFree(pBlob->data);
×
6109
      taosArrayDestroy(pBlob->pSeqTable);
×
6110
      taosMemoryFree(pBlob);
×
6111
    }
6112
  }
6113
  return code;
35,834✔
6114
}
6115

6116
#define CALC_SUM_MAX_MIN(SUM, MAX, MIN, VAL) \
6117
  do {                                       \
6118
    (SUM) += (VAL);                          \
6119
    if ((MAX) < (VAL)) (MAX) = (VAL);        \
6120
    if ((MIN) > (VAL)) (MIN) = (VAL);        \
6121
  } while (0)
6122

6123
static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, SColumnDataAgg *pAggs) {
2,925,702✔
6124
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,925,702✔
6125
  int16_t *numOfNull = &pAggs->numOfNull;
2,925,702✔
6126
  *sum = 0;
2,925,702✔
6127
  *max = 0;
2,925,702✔
6128
  *min = 1;
2,925,702✔
6129
  *numOfNull = 0;
2,925,702✔
6130

6131
  int8_t val;
6132
  if (HAS_VALUE == pColData->flag) {
2,925,702✔
6133
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6134
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
2,147,483,647✔
6135
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6136
    }
6137
  } else {
6138
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6139
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6140
        case 0:
6,249,518✔
6141
        case 1:
6142
          (*numOfNull)++;
6,249,518✔
6143
          break;
6,249,518✔
6144
        case 2:
119,150,482✔
6145
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
119,150,482✔
6146
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
119,150,482✔
6147
          break;
119,150,482✔
6148
        default:
×
6149
          break;
×
6150
      }
6151
    }
6152
  }
6153
}
2,917,993✔
6154

6155
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,954,443✔
6156
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,954,443✔
6157
  int16_t *numOfNull = &pAggs->numOfNull;
2,954,443✔
6158
  *sum = 0;
2,954,443✔
6159
  *max = INT8_MIN;
2,954,443✔
6160
  *min = INT8_MAX;
2,954,443✔
6161
  *numOfNull = 0;
2,954,443✔
6162

6163
  int8_t val;
6164
  if (HAS_VALUE == pColData->flag) {
2,954,443✔
6165
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6166
      val = ((int8_t *)pColData->pData)[iVal];
2,147,483,647✔
6167
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6168
    }
6169
  } else {
6170
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,872,791✔
6171
      switch (tColDataGetBitValue(pColData, iVal)) {
125,841,000✔
6172
        case 0:
6,655,202✔
6173
        case 1:
6174
          (*numOfNull)++;
6,655,202✔
6175
          break;
6,655,202✔
6176
        case 2:
119,185,798✔
6177
          val = ((int8_t *)pColData->pData)[iVal];
119,185,798✔
6178
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
119,185,798✔
6179
          break;
119,185,798✔
6180
        default:
×
6181
          break;
×
6182
      }
6183
    }
6184
  }
6185
}
2,954,443✔
6186

6187
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,930,622✔
6188
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,930,622✔
6189
  int16_t *numOfNull = &pAggs->numOfNull;
2,930,622✔
6190
  *sum = 0;
2,930,622✔
6191
  *max = INT16_MIN;
2,930,622✔
6192
  *min = INT16_MAX;
2,930,622✔
6193
  *numOfNull = 0;
2,930,622✔
6194

6195
  int16_t val;
6196
  if (HAS_VALUE == pColData->flag) {
2,930,622✔
6197
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6198
      val = ((int16_t *)pColData->pData)[iVal];
2,147,483,647✔
6199
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6200
    }
6201
  } else {
6202
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6203
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6204
        case 0:
6,209,181✔
6205
        case 1:
6206
          (*numOfNull)++;
6,209,181✔
6207
          break;
6,209,181✔
6208
        case 2:
119,190,819✔
6209
          val = ((int16_t *)pColData->pData)[iVal];
119,190,819✔
6210
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
119,190,819✔
6211
          break;
119,190,819✔
6212
        default:
×
6213
          break;
×
6214
      }
6215
    }
6216
  }
6217
}
2,930,622✔
6218

6219
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
18,172,962✔
6220
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
18,172,962✔
6221
  int16_t *numOfNull = &pAggs->numOfNull;
18,174,054✔
6222
  *sum = 0;
18,174,289✔
6223
  *max = INT32_MIN;
18,173,714✔
6224
  *min = INT32_MAX;
18,174,160✔
6225
  *numOfNull = 0;
18,174,005✔
6226

6227
  int32_t val;
6228
  if (HAS_VALUE == pColData->flag) {
18,173,408✔
6229
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6230
      val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6231
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6232
    }
6233
  } else {
6234
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
255,864,443✔
6235
      switch (tColDataGetBitValue(pColData, iVal)) {
255,759,493✔
6236
        case 0:
26,133,767✔
6237
        case 1:
6238
          (*numOfNull)++;
26,133,767✔
6239
          break;
26,133,767✔
6240
        case 2:
229,625,726✔
6241
          val = ((int32_t *)pColData->pData)[iVal];
229,625,726✔
6242
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
229,625,726✔
6243
          break;
229,625,726✔
6244
        default:
×
6245
          break;
×
6246
      }
6247
    }
6248
  }
6249
}
14,999,372✔
6250

6251
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
70,841,514✔
6252
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
70,841,514✔
6253
  int16_t *numOfNull = &pAggs->numOfNull;
70,842,172✔
6254
  *sum = 0;
70,842,172✔
6255
  *max = INT64_MIN;
70,842,172✔
6256
  *min = INT64_MAX;
70,842,501✔
6257
  *numOfNull = 0;
70,844,296✔
6258

6259
  int64_t val;
6260
  if (HAS_VALUE == pColData->flag) {
70,843,967✔
6261
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6262
      val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6263
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6264
    }
6265
  } else {
6266
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
413,250,807✔
6267
      switch (tColDataGetBitValue(pColData, iVal)) {
413,046,300✔
6268
        case 0:
60,072,392✔
6269
        case 1:
6270
          (*numOfNull)++;
60,072,392✔
6271
          break;
60,072,392✔
6272
        case 2:
352,973,908✔
6273
          val = ((int64_t *)pColData->pData)[iVal];
352,973,908✔
6274
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
352,973,908✔
6275
          break;
352,973,908✔
6276
        default:
×
6277
          break;
×
6278
      }
6279
    }
6280
  }
6281
}
204✔
6282

6283
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
31,969,996✔
6284
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
31,969,996✔
6285
  int16_t *numOfNull = &pAggs->numOfNull;
31,970,593✔
6286
  *(double *)sum = 0;
31,970,748✔
6287
  *(double *)max = -FLT_MAX;
31,970,593✔
6288
  *(double *)min = FLT_MAX;
31,970,593✔
6289
  *numOfNull = 0;
31,970,593✔
6290

6291
  float val;
6292
  if (HAS_VALUE == pColData->flag) {
31,970,593✔
6293
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6294
      val = ((float *)pColData->pData)[iVal];
2,147,483,647✔
6295
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6296
    }
6297
  } else {
6298
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
135,195,113✔
6299
      switch (tColDataGetBitValue(pColData, iVal)) {
135,149,029✔
6300
        case 0:
7,457,030✔
6301
        case 1:
6302
          (*numOfNull)++;
7,457,030✔
6303
          break;
7,457,030✔
6304
        case 2:
127,691,999✔
6305
          val = ((float *)pColData->pData)[iVal];
127,691,999✔
6306
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
127,691,999✔
6307
          break;
127,691,999✔
6308
        default:
×
6309
          break;
×
6310
      }
6311
    }
6312
  }
6313
}
26,191,147✔
6314

6315
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
3,252,020✔
6316
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
3,252,020✔
6317
  int16_t *numOfNull = &pAggs->numOfNull;
3,252,046✔
6318
  *(double *)sum = 0;
3,252,046✔
6319
  *(double *)max = -DBL_MAX;
3,252,046✔
6320
  *(double *)min = DBL_MAX;
3,252,020✔
6321
  *numOfNull = 0;
3,252,020✔
6322

6323
  double val;
6324
  if (HAS_VALUE == pColData->flag) {
3,252,020✔
6325
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6326
      val = ((double *)pColData->pData)[iVal];
2,147,483,647✔
6327
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6328
    }
6329
  } else {
6330
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,872,791✔
6331
      switch (tColDataGetBitValue(pColData, iVal)) {
125,841,000✔
6332
        case 0:
6,574,186✔
6333
        case 1:
6334
          (*numOfNull)++;
6,574,186✔
6335
          break;
6,574,186✔
6336
        case 2:
119,266,814✔
6337
          val = ((double *)pColData->pData)[iVal];
119,266,814✔
6338
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
119,266,814✔
6339
          break;
119,266,814✔
6340
        default:
×
6341
          break;
×
6342
      }
6343
    }
6344
  }
6345
}
3,215,353✔
6346

6347
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,863,457✔
6348
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,863,457✔
6349
  int16_t *numOfNull = &pAggs->numOfNull;
2,863,457✔
6350
  *(uint64_t *)sum = 0;
2,863,457✔
6351
  *(uint64_t *)max = 0;
2,863,457✔
6352
  *(uint64_t *)min = UINT8_MAX;
2,863,457✔
6353
  *numOfNull = 0;
2,863,457✔
6354

6355
  uint8_t val;
6356
  if (HAS_VALUE == pColData->flag) {
2,863,457✔
6357
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6358
      val = ((uint8_t *)pColData->pData)[iVal];
2,147,483,647✔
6359
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6360
    }
6361
  } else {
6362
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6363
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6364
        case 0:
6,311,173✔
6365
        case 1:
6366
          (*numOfNull)++;
6,311,173✔
6367
          break;
6,311,173✔
6368
        case 2:
119,088,827✔
6369
          val = ((uint8_t *)pColData->pData)[iVal];
119,088,827✔
6370
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
119,088,827✔
6371
          break;
119,088,827✔
6372
        default:
×
6373
          break;
×
6374
      }
6375
    }
6376
  }
6377
}
2,725,697✔
6378

6379
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,863,457✔
6380
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,863,457✔
6381
  int16_t *numOfNull = &pAggs->numOfNull;
2,863,457✔
6382
  *(uint64_t *)sum = 0;
2,863,457✔
6383
  *(uint64_t *)max = 0;
2,863,457✔
6384
  *(uint64_t *)min = UINT16_MAX;
2,863,457✔
6385
  *numOfNull = 0;
2,863,457✔
6386

6387
  uint16_t val;
6388
  if (HAS_VALUE == pColData->flag) {
2,863,457✔
6389
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6390
      val = ((uint16_t *)pColData->pData)[iVal];
2,147,483,647✔
6391
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6392
    }
6393
  } else {
6394
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6395
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6396
        case 0:
6,271,672✔
6397
        case 1:
6398
          (*numOfNull)++;
6,271,672✔
6399
          break;
6,271,672✔
6400
        case 2:
119,128,328✔
6401
          val = ((uint16_t *)pColData->pData)[iVal];
119,128,328✔
6402
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
119,128,328✔
6403
          break;
119,128,328✔
6404
        default:
×
6405
          break;
×
6406
      }
6407
    }
6408
  }
6409
}
2,774,487✔
6410

6411
static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,863,457✔
6412
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,863,457✔
6413
  int16_t *numOfNull = &pAggs->numOfNull;
2,863,457✔
6414
  *(uint64_t *)sum = 0;
2,863,457✔
6415
  *(uint64_t *)max = 0;
2,863,457✔
6416
  *(uint64_t *)min = UINT32_MAX;
2,863,457✔
6417
  *numOfNull = 0;
2,863,457✔
6418

6419
  uint32_t val;
6420
  if (HAS_VALUE == pColData->flag) {
2,863,457✔
6421
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6422
      val = ((uint32_t *)pColData->pData)[iVal];
2,147,483,647✔
6423
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6424
    }
6425
  } else {
6426
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6427
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6428
        case 0:
6,230,081✔
6429
        case 1:
6430
          (*numOfNull)++;
6,230,081✔
6431
          break;
6,230,081✔
6432
        case 2:
119,169,919✔
6433
          val = ((uint32_t *)pColData->pData)[iVal];
119,169,919✔
6434
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
119,169,919✔
6435
          break;
119,169,919✔
6436
        default:
×
6437
          break;
×
6438
      }
6439
    }
6440
  }
6441
}
2,843,920✔
6442

6443
static FORCE_INLINE void tColDataCalcSMAUBigInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,863,457✔
6444
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,863,457✔
6445
  int16_t *numOfNull = &pAggs->numOfNull;
2,863,457✔
6446
  *(uint64_t *)sum = 0;
2,863,457✔
6447
  *(uint64_t *)max = 0;
2,863,457✔
6448
  *(uint64_t *)min = UINT64_MAX;
2,863,457✔
6449
  *numOfNull = 0;
2,863,457✔
6450

6451
  uint64_t val;
6452
  if (HAS_VALUE == pColData->flag) {
2,863,457✔
6453
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6454
      val = ((uint64_t *)pColData->pData)[iVal];
2,147,483,647✔
6455
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6456
    }
6457
  } else {
6458
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
125,431,350✔
6459
      switch (tColDataGetBitValue(pColData, iVal)) {
125,400,000✔
6460
        case 0:
6,220,467✔
6461
        case 1:
6462
          (*numOfNull)++;
6,220,467✔
6463
          break;
6,220,467✔
6464
        case 2:
119,179,533✔
6465
          val = ((uint64_t *)pColData->pData)[iVal];
119,179,533✔
6466
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
119,179,533✔
6467
          break;
119,179,533✔
6468
        default:
×
6469
          break;
×
6470
      }
6471
    }
6472
  }
6473
}
2,855,565✔
6474

6475
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
7,368,663✔
6476
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
7,368,663✔
6477
  int16_t *numOfNull = &pAggs->numOfNull;
7,368,663✔
6478
  *(uint64_t *)sum = 0;
7,368,663✔
6479
  *(uint64_t *)max = 0;
7,368,663✔
6480
  *(uint64_t *)min = 0;
7,368,663✔
6481
  *numOfNull = 0;
7,368,161✔
6482

6483
  switch (pColData->flag) {
7,368,663✔
6484
    case HAS_NONE:
×
6485
    case HAS_NULL:
6486
    case (HAS_NONE | HAS_NULL):
6487
      *numOfNull = pColData->nVal;
×
6488
      break;
×
6489
    case HAS_VALUE:
7,305,020✔
6490
      *numOfNull = 0;
7,305,020✔
6491
      break;
7,305,020✔
6492
    case (HAS_VALUE | HAS_NULL):
63,141✔
6493
    case (HAS_VALUE | HAS_NONE):
6494
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
251,304,141✔
6495
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
251,241,000✔
6496
          (*numOfNull)++;
12,830,915✔
6497
        }
6498
      }
6499
      break;
63,141✔
6500
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6501
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6502
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6503
          (*numOfNull)++;
×
6504
        }
6505
      }
6506
      break;
×
6507
    default:
×
6508
      break;
×
6509
  }
6510
}
7,368,161✔
6511

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

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

6534
  Decimal64         *pVal = NULL;
33,584✔
6535
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
33,584✔
6536
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
33,584✔
6537
  if (HAS_VALUE == pColData->flag) {
33,584✔
6538
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
650,238✔
6539
      pVal = ((Decimal64 *)pColData->pData) + iVal;
643,800✔
6540
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
643,800✔
6541
    }
6542
  } else {
6543
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
21,378,946✔
6544
      switch (tColDataGetBitValue(pColData, iVal)) {
21,351,800✔
6545
        case 0:
494,139✔
6546
        case 1:
6547
          pAggs->numOfNull++;
494,139✔
6548
          break;
494,139✔
6549
        case 2:
20,857,661✔
6550
          pVal = ((Decimal64 *)pColData->pData) + iVal;
20,857,661✔
6551
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
20,857,661✔
6552
          break;
20,857,661✔
6553
        default:
×
6554
          break;
×
6555
      }
6556
    }
6557
  }
6558
}
33,584✔
6559

6560
static FORCE_INLINE void tColDataCalcSMADecimal128Type(SColData *pColData, SColumnDataAgg *pAggs) {
80,858✔
6561
  Decimal128 *pSum = (Decimal128 *)pAggs->decimal128Sum, *pMax = (Decimal128 *)pAggs->decimal128Max,
80,858✔
6562
             *pMin = (Decimal128 *)pAggs->decimal128Min;
80,858✔
6563
  uint8_t *pOverflow = &pAggs->overflow;
80,858✔
6564
  *pSum = DECIMAL128_ZERO;
80,858✔
6565
  *pMax = DECIMAL128_MIN;
80,858✔
6566
  *pMin = DECIMAL128_MAX;
80,858✔
6567
  pAggs->numOfNull = 0;
80,858✔
6568
  pAggs->colId |= DECIMAL_AGG_FLAG;
80,858✔
6569

6570
  Decimal128        *pVal = NULL;
80,858✔
6571
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
80,858✔
6572
  if (HAS_VALUE == pColData->flag) {
80,858✔
6573
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
219,372✔
6574
      pVal = ((Decimal128 *)pColData->pData) + iVal;
217,200✔
6575
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
217,200✔
6576
    }
6577
  } else {
6578
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
69,131,086✔
6579
      switch (tColDataGetBitValue(pColData, iVal)) {
69,052,400✔
6580
        case 0:
1,540,953✔
6581
        case 1:
6582
          pAggs->numOfNull++;
1,540,953✔
6583
          break;
1,540,953✔
6584
        case 2:
67,511,447✔
6585
          pVal = ((Decimal128 *)pColData->pData) + iVal;
67,511,447✔
6586
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
67,511,447✔
6587
          break;
67,511,447✔
6588
        default:
×
6589
          break;
×
6590
      }
6591
    }
6592
  }
6593
}
80,858✔
6594

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

6620
// SValueColumn ================================
6621
int32_t tValueColumnInit(SValueColumn *valCol) {
580,053,914✔
6622
  valCol->type = TSDB_DATA_TYPE_NULL;
580,053,914✔
6623
  valCol->numOfValues = 0;
580,090,618✔
6624
  tBufferInit(&valCol->data);
580,108,312✔
6625
  tBufferInit(&valCol->offsets);
580,111,450✔
6626
  return 0;
580,174,770✔
6627
}
6628

6629
void tValueColumnDestroy(SValueColumn *valCol) {
1,363,133,087✔
6630
  valCol->type = TSDB_DATA_TYPE_NULL;
1,363,133,087✔
6631
  valCol->numOfValues = 0;
1,363,213,863✔
6632
  tBufferDestroy(&valCol->data);
1,363,253,126✔
6633
  tBufferDestroy(&valCol->offsets);
1,363,114,255✔
6634
  return;
1,363,130,676✔
6635
}
6636

6637
void tValueColumnClear(SValueColumn *valCol) {
740,961,767✔
6638
  valCol->type = TSDB_DATA_TYPE_NULL;
740,961,767✔
6639
  valCol->numOfValues = 0;
741,010,804✔
6640
  tBufferClear(&valCol->data);
741,005,814✔
6641
  tBufferClear(&valCol->offsets);
740,964,646✔
6642
  return;
740,948,109✔
6643
}
6644

6645
int32_t tValueColumnAppend(SValueColumn *valCol, const SValue *value) {
1,022,299✔
6646
  int32_t code;
6647

6648
  if (valCol->numOfValues == 0) {
1,022,299✔
6649
    valCol->type = value->type;
511,445✔
6650
  }
6651

6652
  if (!(value->type == valCol->type)) {
1,021,657✔
6653
    return TSDB_CODE_INVALID_PARA;
×
6654
  }
6655

6656
  if (IS_VAR_DATA_TYPE(value->type)) {
1,021,015✔
6657
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
741,989✔
6658
      return code;
×
6659
    }
6660
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
741,192✔
6661
      return code;
×
6662
    }
6663
  } else {
6664
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
650,419✔
6665
    if (code) return code;
651,858✔
6666
  }
6667
  valCol->numOfValues++;
1,022,454✔
6668

6669
  return 0;
1,022,299✔
6670
}
6671

6672
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
38,829,633✔
6673
  int32_t code;
6674

6675
  if (idx < 0 || idx >= valCol->numOfValues) {
38,829,633✔
6676
    return TSDB_CODE_OUT_OF_RANGE;
×
6677
  }
6678

6679
  if (IS_VAR_DATA_TYPE(valCol->type)) {
38,833,668✔
6680
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
826,948✔
6681
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
823,173✔
6682
    int32_t  oldDataSize = nextOffset - offsets[idx];
823,338✔
6683
    int32_t  bytesAdded = value->nData - oldDataSize;
823,338✔
6684

6685
    if (bytesAdded != 0) {
823,338✔
6686
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
34,936✔
6687
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
17,468✔
6688
              tBufferGetSize(&valCol->data) - nextOffset);
17,468✔
6689
      valCol->data.size += bytesAdded;
17,468✔
6690

6691
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
17,468✔
6692
        offsets[i] += bytesAdded;
×
6693
      }
6694
    }
6695
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
823,338✔
6696
  } else {
6697
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
38,013,953✔
6698
                        tDataTypes[valCol->type].bytes);
38,005,308✔
6699
  }
6700
  return 0;
6701
}
6702

6703
int32_t tValueColumnGet(SValueColumn *valCol, int32_t idx, SValue *value) {
108,505,959✔
6704
  if (idx < 0 || idx >= valCol->numOfValues) {
108,505,959✔
6705
    return TSDB_CODE_OUT_OF_RANGE;
×
6706
  }
6707

6708
  value->type = valCol->type;
108,524,361✔
6709
  if (IS_VAR_DATA_TYPE(value->type)) {
119,447,481✔
6710
    int32_t       offset, nextOffset;
10,916,899✔
6711
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
10,916,010✔
6712

6713
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
10,915,680✔
6714
    if (idx == valCol->numOfValues - 1) {
10,910,536✔
6715
      nextOffset = tBufferGetSize(&valCol->data);
6,284,840✔
6716
    } else {
6717
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
4,628,586✔
6718
    }
6719
    value->nData = nextOffset - offset;
10,914,076✔
6720
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
10,914,718✔
6721
  } else {
6722
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
97,614,961✔
6723
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
195,233,607✔
6724
  }
6725
  return 0;
108,529,799✔
6726
}
6727

6728
int32_t tValueColumnCompress(SValueColumn *valCol, SValueColumnCompressInfo *info, SBuffer *output, SBuffer *assist) {
511,600✔
6729
  int32_t code;
6730

6731
  if (!(valCol->numOfValues > 0)) {
511,600✔
6732
    return TSDB_CODE_INVALID_PARA;
×
6733
  }
6734

6735
  (*info) = (SValueColumnCompressInfo){
511,600✔
6736
      .cmprAlg = info->cmprAlg,
511,380✔
6737
      .type = valCol->type,
511,380✔
6738
  };
6739

6740
  // offset
6741
  if (IS_VAR_DATA_TYPE(valCol->type)) {
511,600✔
6742
    SCompressInfo cinfo = {
177,924✔
6743
        .cmprAlg = info->cmprAlg,
177,924✔
6744
        .dataType = TSDB_DATA_TYPE_INT,
6745
        .originalSize = valCol->offsets.size,
177,924✔
6746
    };
6747

6748
    code = tCompressDataToBuffer(valCol->offsets.data, &cinfo, output, assist);
177,924✔
6749
    if (code) return code;
177,924✔
6750

6751
    info->offsetOriginalSize = cinfo.originalSize;
177,924✔
6752
    info->offsetCompressedSize = cinfo.compressedSize;
177,924✔
6753
  }
6754

6755
  // data
6756
  SCompressInfo cinfo = {
511,600✔
6757
      .cmprAlg = info->cmprAlg,
1,023,200✔
6758
      .dataType = valCol->type,
511,600✔
6759
      .originalSize = valCol->data.size,
511,600✔
6760
  };
6761

6762
  code = tCompressDataToBuffer(valCol->data.data, &cinfo, output, assist);
511,600✔
6763
  if (code) return code;
511,600✔
6764

6765
  info->dataOriginalSize = cinfo.originalSize;
511,600✔
6766
  info->dataCompressedSize = cinfo.compressedSize;
511,600✔
6767

6768
  return 0;
511,600✔
6769
}
6770

6771
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
25,951,921✔
6772
                               SBuffer *assist) {
6773
  int32_t code;
6774

6775
  tValueColumnClear(valCol);
25,951,921✔
6776
  valCol->type = info->type;
25,952,883✔
6777
  // offset
6778
  if (IS_VAR_DATA_TYPE(valCol->type)) {
32,998,748✔
6779
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
7,042,311✔
6780

6781
    SCompressInfo cinfo = {
7,043,110✔
6782
        .dataType = TSDB_DATA_TYPE_INT,
6783
        .cmprAlg = info->cmprAlg,
7,042,623✔
6784
        .originalSize = info->offsetOriginalSize,
7,043,587✔
6785
        .compressedSize = info->offsetCompressedSize,
7,042,293✔
6786
    };
6787

6788
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
7,041,816✔
6789
    if (code) {
7,046,640✔
6790
      return code;
×
6791
    }
6792
  } else {
6793
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
18,907,050✔
6794
  }
6795

6796
  // data
6797
  SCompressInfo cinfo = {
25,953,855✔
6798
      .dataType = valCol->type,
25,953,700✔
6799
      .cmprAlg = info->cmprAlg,
25,954,177✔
6800
      .originalSize = info->dataOriginalSize,
25,952,903✔
6801
      .compressedSize = info->dataCompressedSize,
25,954,809✔
6802
  };
6803

6804
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
25,954,177✔
6805
  if (code) {
25,955,791✔
6806
    return code;
×
6807
  }
6808

6809
  return 0;
25,955,791✔
6810
}
6811

6812
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *info, SBuffer *buffer) {
511,600✔
6813
  int32_t code;
6814
  uint8_t fmtVer = 0;
511,600✔
6815

6816
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
1,022,980✔
6817
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
1,022,980✔
6818
  if ((code = tBufferPutI8(buffer, info->type))) return code;
1,023,200✔
6819
  if (IS_VAR_DATA_TYPE(info->type)) {
511,600✔
6820
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
355,848✔
6821
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
355,848✔
6822
  }
6823
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
1,023,200✔
6824
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
1,022,980✔
6825

6826
  return 0;
511,380✔
6827
}
6828

6829
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
25,953,855✔
6830
  int32_t code;
6831
  uint8_t fmtVer;
25,953,855✔
6832

6833
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
25,955,139✔
6834
  if (fmtVer == 0) {
25,956,900✔
6835
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
25,956,900✔
6836
    if ((code = tBufferGetI8(reader, &info->type))) return code;
25,956,900✔
6837
    if (IS_VAR_DATA_TYPE(info->type)) {
25,955,781✔
6838
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
7,049,520✔
6839
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
7,043,430✔
6840
    } else {
6841
      info->offsetOriginalSize = 0;
18,908,022✔
6842
      info->offsetCompressedSize = 0;
18,909,306✔
6843
    }
6844
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
25,955,781✔
6845
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
25,954,332✔
6846
  } else {
6847
    return TSDB_CODE_INVALID_PARA;
×
6848
  }
6849

6850
  return 0;
25,956,268✔
6851
}
6852

6853
int32_t tCompressData(void          *input,       // input
717,733,154✔
6854
                      SCompressInfo *info,        // compress info
6855
                      void          *output,      // output
6856
                      int32_t        outputSize,  // output size
6857
                      SBuffer       *buffer       // assistant buffer provided by caller, can be NULL
6858
) {
6859
  int32_t extraSizeNeeded;
6860
  int32_t code;
6861

6862
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
717,733,154✔
6863
  if (!(outputSize >= extraSizeNeeded)) {
717,704,245✔
6864
    return TSDB_CODE_INVALID_PARA;
×
6865
  }
6866

6867
  if (info->cmprAlg == NO_COMPRESSION) {
717,704,245✔
6868
    (void)memcpy(output, input, info->originalSize);
90,424✔
6869
    info->compressedSize = info->originalSize;
90,424✔
6870
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
817,885,594✔
6871
    SBuffer local;
100,203,531✔
6872

6873
    tBufferInit(&local);
6874
    if (buffer == NULL) {
100,252,360✔
6875
      buffer = &local;
×
6876
    }
6877

6878
    if (info->cmprAlg == TWO_STAGE_COMP) {
100,252,360✔
6879
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
100,139,563✔
6880
      if (code) {
100,136,035✔
6881
        tBufferDestroy(&local);
6882
        return code;
×
6883
      }
6884
    }
6885

6886
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
300,731,032✔
6887
        input,                                                   // input
6888
        info->originalSize,                                      // input size
6889
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
100,238,072✔
6890
        output,                                                  // output
6891
        outputSize,                                              // output size
6892
        info->cmprAlg,                                           // compression algorithm
100,251,124✔
6893
        buffer->data,                                            // buffer
6894
        buffer->capacity                                         // buffer size
100,246,934✔
6895
    );
6896
    if (info->compressedSize < 0) {
100,254,314✔
6897
      tBufferDestroy(&local);
6898
      return TSDB_CODE_COMPRESS_ERROR;
×
6899
    }
6900

6901
    tBufferDestroy(&local);
6902
  } else {
6903
    DEFINE_VAR(info->cmprAlg)
617,428,606✔
6904
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
617,448,168✔
6905
      (void)memcpy(output, input, info->originalSize);
7,080✔
6906
      info->compressedSize = info->originalSize;
7,080✔
6907
      return 0;
7,080✔
6908
    }
6909
    SBuffer local;
617,334,300✔
6910

6911
    tBufferInit(&local);
6912
    if (buffer == NULL) {
617,428,222✔
6913
      buffer = &local;
×
6914
    }
6915
    code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
617,428,222✔
6916

6917
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
1,852,105,598✔
6918
        input,                                                      // input
6919
        info->originalSize,                                         // input size
6920
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
617,419,375✔
6921
        output,                                                     // output
6922
        outputSize,                                                 // output size
6923
        info->cmprAlg,                                              // compression algorithm
6924
        buffer->data,                                               // buffer
6925
        buffer->capacity                                            // buffer size
617,406,004✔
6926
    );
6927
    if (info->compressedSize < 0) {
617,430,738✔
6928
      tBufferDestroy(&local);
6929
      return TSDB_CODE_COMPRESS_ERROR;
×
6930
    }
6931

6932
    tBufferDestroy(&local);
6933
    // new col compress
6934
  }
6935

6936
  return 0;
717,729,317✔
6937
}
6938

6939
int32_t tDecompressData(void                *input,       // input
1,940,052,060✔
6940
                        const SCompressInfo *info,        // compress info
6941
                        void                *output,      // output
6942
                        int32_t              outputSize,  // output size
6943
                        SBuffer             *buffer       // assistant buffer provided by caller, can be NULL
6944
) {
6945
  int32_t code;
6946

6947
  if (!(outputSize >= info->originalSize)) {
1,940,052,060✔
6948
    return TSDB_CODE_INVALID_PARA;
×
6949
  }
6950

6951
  if (info->cmprAlg == NO_COMPRESSION) {
1,940,264,993✔
6952
    if (!(info->compressedSize == info->originalSize)) {
45,080✔
6953
      return TSDB_CODE_INVALID_PARA;
×
6954
    }
6955
    (void)memcpy(output, input, info->compressedSize);
45,080✔
6956
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
2,147,483,647✔
6957
    SBuffer local;
932,493,033✔
6958

6959
    tBufferInit(&local);
6960
    if (buffer == NULL) {
932,586,932✔
6961
      buffer = &local;
×
6962
    }
6963

6964
    if (info->cmprAlg == TWO_STAGE_COMP) {
932,586,932✔
6965
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
931,948,223✔
6966
      if (code) {
931,972,029✔
6967
        tBufferDestroy(&local);
6968
        return code;
×
6969
      }
6970
    }
6971

6972
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
1,865,222,612✔
6973
        input,                                                  // input
6974
        info->compressedSize,                                   // inputSize
932,630,093✔
6975
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
932,641,375✔
6976
        output,                                                 // output
6977
        outputSize,                                             // output size
6978
        info->cmprAlg,                                          // compression algorithm
932,630,205✔
6979
        buffer->data,                                           // helper buffer
6980
        buffer->capacity                                        // extra buffer size
932,592,776✔
6981
    );
6982
    if (decompressedSize < 0) {
932,479,648✔
6983
      tBufferDestroy(&local);
6984
      return TSDB_CODE_COMPRESS_ERROR;
×
6985
    }
6986

6987
    if (!(decompressedSize == info->originalSize)) {
932,479,648✔
6988
      return TSDB_CODE_COMPRESS_ERROR;
×
6989
    }
6990
    tBufferDestroy(&local);
6991
  } else {
6992
    DEFINE_VAR(info->cmprAlg);
1,007,687,249✔
6993
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
1,007,722,498✔
6994
      (void)memcpy(output, input, info->compressedSize);
15,930✔
6995
      return 0;
15,930✔
6996
    }
6997
    SBuffer local;
1,007,639,846✔
6998

6999
    tBufferInit(&local);
7000
    if (buffer == NULL) {
1,007,632,082✔
7001
      buffer = &local;
×
7002
    }
7003
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,007,632,082✔
7004
    if (code) {
1,007,640,793✔
7005
      return code;
×
7006
    }
7007

7008
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
2,015,266,610✔
7009
        input,                                                  // input
7010
        info->compressedSize,                                   // inputSize
1,007,690,987✔
7011
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,007,676,921✔
7012
        output,                                                 // output
7013
        outputSize,                                             // output size
7014
        info->cmprAlg,                                          // compression algorithm
1,007,689,892✔
7015
        buffer->data,                                           // helper buffer
7016
        buffer->capacity                                        // extra buffer size
1,007,630,958✔
7017
    );
7018
    if (decompressedSize < 0) {
1,007,383,935✔
7019
      tBufferDestroy(&local);
7020
      return TSDB_CODE_COMPRESS_ERROR;
×
7021
    }
7022

7023
    if (!(decompressedSize == info->originalSize)) {
1,007,383,935✔
7024
      return TSDB_CODE_COMPRESS_ERROR;
×
7025
    }
7026
    tBufferDestroy(&local);
7027
  }
7028

7029
  return 0;
1,940,097,571✔
7030
}
7031

7032
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
717,709,368✔
7033
  int32_t code;
7034

7035
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
717,709,368✔
7036
  if (code) return code;
717,680,301✔
7037

7038
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
717,680,301✔
7039
  if (code) return code;
717,703,955✔
7040

7041
  output->size += info->compressedSize;
717,703,955✔
7042
  return 0;
717,728,548✔
7043
}
7044

7045
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
932,472,267✔
7046
  int32_t code;
7047

7048
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
932,472,267✔
7049
  if (code) return code;
932,276,039✔
7050

7051
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
932,276,039✔
7052
  if (code) return code;
932,490,049✔
7053

7054
  output->size += info->originalSize;
932,490,049✔
7055
  return 0;
932,504,590✔
7056
}
7057

7058
// handle all types, including var data
7059
void valueSetDatum(SValue *pVal, int8_t type, void *pDatum, uint32_t len) {
2,147,483,647✔
7060
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
2,147,483,647✔
7061
    pVal->pData = pDatum;
83,954,177✔
7062
    pVal->nData = len;
665,799,869✔
7063
  } else {
7064
    switch (len) {
2,147,483,647✔
7065
      case sizeof(uint8_t):
2,147,483,647✔
7066
        pVal->val = *(uint8_t *)pDatum;
2,147,483,647✔
7067
        break;
2,147,483,647✔
7068
      case sizeof(uint16_t):
2,147,483,647✔
7069
        pVal->val = *(uint16_t *)pDatum;
2,147,483,647✔
7070
        break;
2,147,483,647✔
7071
      case sizeof(uint32_t):
2,147,483,647✔
7072
        pVal->val = *(uint32_t *)pDatum;
2,147,483,647✔
7073
        break;
2,147,483,647✔
7074
      case sizeof(uint64_t):
2,147,483,647✔
7075
        pVal->val = *(uint64_t *)pDatum;
2,147,483,647✔
7076
        break;
2,147,483,647✔
7077
      default:
346,209,501✔
7078
        break;
346,209,501✔
7079
    }
7080
  }
7081
}
2,147,483,647✔
7082

7083
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
88,771,385✔
7084
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
88,771,385✔
7085
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
13,914,566✔
7086
    pDst->nData = pSrc->nData;
13,915,234✔
7087
  } else {
7088
    pDst->val = pSrc->val;
74,856,819✔
7089
  }
7090
}
88,778,644✔
7091
void valueClearDatum(SValue *pVal, int8_t type) {
×
7092
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
×
7093
    taosMemoryFreeClear(pVal->pData);
×
7094
    pVal->nData = 0;
×
7095
  } else {
7096
    pVal->val = 0;
×
7097
  }
7098
}
×
7099

7100
int8_t schemaHasBlob(const STSchema *pSchema) {
514,003,646✔
7101
  if (pSchema == NULL) {
514,003,646✔
7102
    return 0;
×
7103
  }
7104
  for (int i = 0; i < pSchema->numOfCols; ++i) {
2,147,483,647✔
7105
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
2,147,483,647✔
7106
      return 1;
54,530✔
7107
    }
7108
  }
7109
  return 0;
513,958,057✔
7110
}
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