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

taosdata / TDengine / #4938

23 Jan 2026 09:40AM UTC coverage: 66.8% (+0.006%) from 66.794%
#4938

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204187 of 305671 relevant lines covered (66.8%)

124015580.65 hits per line

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

63.46
/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) {
201,800,211✔
79
  int32_t n = 0;
201,800,211✔
80
  n += tPutI8(p ? p + n : p, index->type);
201,800,211✔
81
  n += tPutU32v(p ? p + n : p, index->offset);
201,803,159✔
82
  return n;
201,800,563✔
83
}
84

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

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

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

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

109
  if (isPK) {
2,147,483,647✔
110
    sinfo->tupleIndices[sinfo->numOfPKs].type = colVal->value.type;
67,268,585✔
111
    sinfo->tupleIndices[sinfo->numOfPKs].offset =
67,267,617✔
112
        IS_VAR_DATA_TYPE(pTColumn->type) ? sinfo->tupleVarSize + sinfo->tupleFixedSize : pTColumn->offset;
67,267,573✔
113
    sinfo->kvIndices[sinfo->numOfPKs].type = colVal->value.type;
67,268,453✔
114
    sinfo->kvIndices[sinfo->numOfPKs].offset = sinfo->kvPayloadSize;
67,267,661✔
115
    sinfo->numOfPKs++;
67,265,725✔
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
46,868,808✔
122
                             + BSE_SEQUECE_SIZE;                     // value
23,434,404✔
123
      sinfo->kvPayloadSize += tPutI16v(NULL, colVal->cid)            // colId
46,868,808✔
124
                              + tPutU32v(NULL, colVal->value.nData)  // size
23,434,404✔
125
                              + BSE_SEQUECE_SIZE;                    // seq offset
46,868,808✔
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;
94,344✔
164
        break;
47,172✔
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) {
847,420✔
184
        if ((code = tRowBuildScanAddNone(sinfo, schema->columns + i))) goto _exit;
328✔
185
        break;
164✔
186
      } else {  // skip useless value
187
        colValIndex++;
847,256✔
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,854,245,081✔
197
  }
198
  if (sinfo->numOfValue) {
2,147,483,647✔
199
    sinfo->flag |= HAS_VALUE;
2,147,483,647✔
200
  }
201

202
  // Tuple
203
  sinfo->tupleFlag = sinfo->flag;
2,147,483,647✔
204
  switch (sinfo->flag) {
2,147,483,647✔
205
    case HAS_NONE:
2,147,483,647✔
206
    case HAS_NULL:
207
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
208
      sinfo->tupleFixedSize = 0;
2,147,483,647✔
209
      break;
2,147,483,647✔
210
    case HAS_VALUE:
2,147,483,647✔
211
      sinfo->tupleBitmapSize = 0;
2,147,483,647✔
212
      sinfo->tupleFixedSize = schema->flen;
2,147,483,647✔
213
      break;
2,147,483,647✔
214
    case (HAS_NONE | HAS_NULL):
2,357,781✔
215
      sinfo->tupleBitmapSize = BIT1_SIZE(schema->numOfCols - 1);
2,357,781✔
216
      sinfo->tupleFixedSize = 0;
2,357,781✔
217
      break;
2,357,217✔
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,058,375,285✔
224
      sinfo->tupleBitmapSize = BIT2_SIZE(schema->numOfCols - 1);
1,058,375,285✔
225
      sinfo->tupleFixedSize = schema->flen;
1,058,374,157✔
226
      break;
1,051,327,799✔
227
  }
228
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
229
    sinfo->tupleIndices[i].offset += sinfo->tupleBitmapSize;
67,267,705✔
230
    sinfo->tuplePKSize += tPutPrimaryKeyIndex(NULL, sinfo->tupleIndices + i);
67,268,629✔
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) {
52,297,751✔
243
    sinfo->kvFlag = (KV_FLG_MID | sinfo->flag);
52,303,870✔
244
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint16_t);
52,309,222✔
245
  } else {
246
    sinfo->kvFlag = (KV_FLG_BIG | sinfo->flag);
6,064✔
247
    sinfo->kvIndexSize = sizeof(SKVIdx) + (sinfo->numOfNull + sinfo->numOfValue) * sizeof(uint32_t);
6,064✔
248
  }
249
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
250
    sinfo->kvIndices[i].offset += sinfo->kvIndexSize;
67,266,781✔
251
    sinfo->kvPKSize += tPutPrimaryKeyIndex(NULL, sinfo->kvIndices + i);
67,267,881✔
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);
61,789,649✔
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);
13,396✔
296
        break;
13,396✔
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
668,157,052✔
316
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NULL);
618,305,255✔
317
        } else if (COL_VAL_IS_NONE(&colValArray[colValIndex])) {  // NONE
49,895,737✔
318
          ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
49,894,613✔
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
164✔
324
        ROW_SET_BITMAP(bitmap, sinfo->tupleFlag, i - 1, BIT_FLG_NONE);
164✔
325
        break;
164✔
326
      } else {
327
        colValIndex++;
×
328
      }
329
    }
330
  }
331

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

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

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

360
  if (((*ppRow)->flag) == 0) {
488,250✔
361
    return TSDB_CODE_INVALID_PARA;
×
362
  }
363
  if (sinfo->tupleFlag == HAS_NONE || sinfo->tupleFlag == HAS_NULL) {
488,250✔
364
    code = addEmptyItemToBlobSet(pBlobSet, TSDB_DATA_BLOB_NULL_VALUE, NULL);
488,250✔
365
    return code;
488,250✔
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) {
597,334,031✔
569
    ((uint16_t *)indices->idx)[indices->nCol] = (uint16_t)offset;
597,334,031✔
570
  } else {
571
    ((uint32_t *)indices->idx)[indices->nCol] = (uint32_t)offset;
×
572
  }
573
  indices->nCol++;
2,147,483,647✔
574
}
2,147,483,647✔
575

576
static int32_t tRowBuildKVRow(SArray *aColVal, const SRowBuildScanInfo *sinfo, const STSchema *schema, SRow **ppRow) {
2,147,483,647✔
577
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
2,147,483,647✔
578

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

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

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

598
  // primary keys
599
  for (int32_t i = 0; i < sinfo->numOfPKs; i++) {
2,147,483,647✔
600
    primaryKeys += tPutPrimaryKeyIndex(primaryKeys, sinfo->kvIndices + i);
5,478,100✔
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;
22,136✔
609
      }
610

611
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
2,147,483,647✔
612
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
2,147,483,647✔
613
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
2,147,483,647✔
614
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
2,147,483,647✔
615
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
2,023,181,558✔
616
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
2,026,754,751✔
617
            if (colValArray[colValIndex].value.nData > 0) {
2,026,714,343✔
618
              (void)memcpy(payload + payloadSize, colValArray[colValIndex].value.pData,
2,013,989,786✔
619
                           colValArray[colValIndex].value.nData);
2,013,951,434✔
620
              payloadSize += colValArray[colValIndex].value.nData;
2,014,016,363✔
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,992,879,622✔
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,
23,434,404✔
647
                                      SRow **ppRow, SBlobSet *ppBlobSet) {
648
  SColVal *colValArray = (SColVal *)TARRAY_DATA(aColVal);
23,434,404✔
649

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

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

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

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

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

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

688
      if (colValArray[colValIndex].cid == schema->columns[i].colId) {
23,434,404✔
689
        if (COL_VAL_IS_VALUE(&colValArray[colValIndex])) {  // value
23,434,404✔
690
          tRowBuildKVRowSetIndex(sinfo->kvFlag, indices, payloadSize);
23,434,404✔
691
          if (IS_VAR_DATA_TYPE(schema->columns[i].type)) {
23,434,404✔
692
            if (IS_STR_DATA_BLOB(schema->columns[i].type)) {
23,434,404✔
693
              hasBlob = 1;
23,434,404✔
694
            }
695
            payloadSize += tPutI16v(payload + payloadSize, colValArray[colValIndex].cid);
23,434,404✔
696
            payloadSize += tPutU32v(payload + payloadSize, colValArray[colValIndex].value.nData);
23,434,404✔
697
            if (colValArray[colValIndex].value.nData > 0) {
23,434,404✔
698
              if (hasBlob == 0) {
21,519,624✔
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,519,624✔
704
                uint32_t  seqOffset = payloadSize + payload - (*ppRow)->data;
21,519,624✔
705
                SBlobItem item = {.seqOffsetInRow = seqOffset,
21,519,624✔
706
                                  .data = colValArray[colValIndex].value.pData,
21,519,624✔
707
                                  .len = colValArray[colValIndex].value.nData,
21,519,624✔
708
                                  .type = TSDB_DATA_BLOB_VALUE};
709
                int32_t   code = tBlobSetPush(ppBlobSet, &item, &seq, 0);
21,519,624✔
710
                if (code != 0) return code;
21,519,624✔
711
                payloadSize += tPutU64(payload + payloadSize, seq);
43,039,248✔
712
              }
713
            } else {
714
              if (hasBlob) {
1,914,780✔
715
                TAOS_CHECK_RETURN(addEmptyItemToBlobSet(ppBlobSet, TSDB_DATA_BLOB_EMPTY_VALUE, NULL));
1,914,780✔
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++;
23,434,404✔
732
        break;
23,434,404✔
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) {
23,434,404✔
742
    return TSDB_CODE_INVALID_PARA;
×
743
  }
744
  return 0;
23,434,404✔
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,
23,922,654✔
1074
                          SRowBuildScanInfo *sinfo) {
1075
  int32_t code;
1076

1077
  code = tRowBuildScan(aColVal, pTSchema, sinfo);
23,922,654✔
1078
  if (code) return code;
23,922,654✔
1079

1080
  if (sinfo->tupleRowSize <= sinfo->kvRowSize) {
23,922,654✔
1081
    code = tRowBuildTupleWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
488,250✔
1082
  } else {
1083
    code = tRowBuildKVRowWithBlob(aColVal, sinfo, pTSchema, ppRow, pBlobSet);
23,434,404✔
1084
  }
1085

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

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

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

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

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

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

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

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

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

1183
  offset = pBlobSet->len;
23,930,004✔
1184
  pBlobSet->len += len;
23,930,004✔
1185

1186
  pBlobSet->seq++;
23,930,004✔
1187
  *seq = pBlobSet->seq;
23,930,004✔
1188

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

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

1200
_exit:
23,930,004✔
1201
  return code;
23,930,004✔
1202
}
1203

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

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

1209
  memcpy(p1, p2, sizeof(SBlobSet));
735✔
1210
  memcpy(p2, &t, sizeof(SBlobSet));
735✔
1211
}
735✔
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,195,818,364✔
1261
  if (pBlobSet == NULL) return 0;
1,195,818,364✔
1262
  return taosArrayGetSize(pBlobSet->pSeqTable);
69,852✔
1263
}
1264

1265
void tBlobSetDestroy(SBlobSet *pBlobSet) {
13,112,741✔
1266
  if (pBlobSet == NULL) return;
13,112,741✔
1267
  uTrace("destroy blob row, seqTable size %p", pBlobSet);
40,741✔
1268
  taosMemoryFree(pBlobSet->data);
40,741✔
1269
  taosArrayDestroy(pBlobSet->pSeqTable);
43,641✔
1270
  taosHashCleanup(pBlobSet->pSeqToffset);
43,641✔
1271
  taosArrayDestroy(pBlobSet->pSet);
43,641✔
1272
  taosMemoryFree(pBlobSet);
43,641✔
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,
2,446,145✔
1304
                          SArray *rowArray, bool *pOrdered, bool *pDupTs) {
1305
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
2,446,145✔
1306
    return TSDB_CODE_INVALID_PARA;
×
1307
  }
1308

1309
  if (!infoSorted) {
2,450,902✔
1310
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo), NULL, tBindInfoCompare);
×
1311
  }
1312

1313
  int32_t code = 0;
2,450,902✔
1314
  int32_t numOfRows = infos[0].bind->num;
2,450,902✔
1315
  SArray *colValArray;
1316
  SColVal colVal;
2,437,043✔
1317

1318
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
2,450,624✔
1319
    return terrno;
×
1320
  }
1321

1322
  SRowKey rowKey, lastRowKey;
2,436,459✔
1323
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
117,312,586✔
1324
    taosArrayClear(colValArray);
115,004,062✔
1325

1326
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
570,007,033✔
1327
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
446,952,500✔
1328
        colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
1,344✔
1329
      } else {
1330
        SValue value = {
446,249,940✔
1331
            .type = infos[iInfo].type,
447,379,813✔
1332
        };
1333
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
447,216,833✔
1334
          value.nData = infos[iInfo].bind->length[iRow];
4,703,970✔
1335
          if (value.nData > pTSchema->columns[iInfo].bytes - VARSTR_HEADER_SIZE) {
4,750,076✔
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;
4,749,926✔
1340
        } else {
1341
          valueSetDatum(&value, infos[iInfo].type,
455,354,216✔
1342
                        (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow,
450,738,975✔
1343
                        infos[iInfo].bind->buffer_length);
449,142,294✔
1344
        }
1345
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
454,583,534✔
1346
      }
1347
      if (taosArrayPush(colValArray, &colVal) == NULL) {
455,305,707✔
1348
        code = terrno;
×
1349
        goto _exit;
×
1350
      }
1351
    }
1352

1353
    SRow             *row;
111,053,347✔
1354
    SRowBuildScanInfo sinfo = {0};
114,765,534✔
1355
    if ((code = tRowBuild(colValArray, pTSchema, &row, &sinfo))) {
114,812,641✔
1356
      goto _exit;
×
1357
    }
1358

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

1364
    if (pOrdered && pDupTs) {
113,257,228✔
1365
      tRowGetKey(row, &rowKey);
226,879,080✔
1366
      if (iRow == 0) {
113,533,377✔
1367
        *pOrdered = true;
2,449,450✔
1368
        *pDupTs = false;
2,450,140✔
1369
      } else {
1370
        if (*pOrdered) {
111,083,927✔
1371
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
111,353,002✔
1372
          *pOrdered = (res >= 0);
111,353,002✔
1373
          if (!*pDupTs) {
111,318,149✔
1374
            *pDupTs = (res == 0);
111,478,259✔
1375
          }
1376
        }
1377
      }
1378
      lastRowKey = rowKey;
114,814,941✔
1379
    }
1380
  }
1381

1382
_exit:
2,793,447✔
1383
  taosArrayDestroy(colValArray);
2,436,908✔
1384
  return code;
2,449,777✔
1385
}
1386

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

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

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

1401
  if (pRow->flag == HAS_NONE) {
2,147,483,647✔
1402
    *pColVal = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
20,526,333✔
1403
    return 0;
20,530,418✔
1404
  }
1405

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

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

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

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

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

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

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

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

1456
            pData += tGetU32v(pData, &pColVal->value.nData);
2,147,483,647✔
1457
            if (pColVal->value.nData > 0) {
2,147,483,647✔
1458
              pColVal->value.pData = pData;
2,147,483,647✔
1459
            } else {
1460
              pColVal->value.pData = NULL;
12,514,606✔
1461
            }
1462
            if (isBlob == 1) {
2,147,483,647✔
1463
              pData += BSE_SEQUECE_SIZE;  // skip seq
29,568,224✔
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++;
10,698,848✔
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,635,337✔
1504
      return 0;
1,635,337✔
1505
    } else if (bit == BIT_FLG_NULL) {
2,147,483,647✔
1506
      *pColVal = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
1,642,086,321✔
1507
      return 0;
1,642,087,625✔
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) {
414,082,595✔
1533
  SRowKey key1, key2;
414,067,562✔
1534
  tRowGetKey(*(SRow **)p1, &key1);
830,886,826✔
1535
  tRowGetKey(*(SRow **)p2, &key2);
830,159,986✔
1536
  return tRowKeyCompare(&key1, &key2);
415,492,103✔
1537
}
1538
static void    tRowPDestroy(SRow **ppRow) { tRowDestroy(*ppRow); }
37,714,831✔
1539

1540
static SColVal* tRowFindColumnValue(SRowIter *iter, int32_t targetCid) {
89,450,985✔
1541
  SColVal* pColVal = tRowIterNext(iter);
89,450,985✔
1542
  while (pColVal != NULL && pColVal->cid < targetCid) {
113,923,653✔
1543
    pColVal = tRowIterNext(iter);
24,473,100✔
1544
  }
1545
  return pColVal;
89,450,625✔
1546
}
1547

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

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

1562
  for (int32_t i = 0; i < nRow; i++) {
44,180,633✔
1563
    SRow *pRowT = taosArrayGetP(aRowP, iStart + i);
37,716,235✔
1564

1565
    code = tRowIterOpen(pRowT, pTSchema, &aIter[i]);
37,716,667✔
1566
    if (code) goto _exit;
37,716,667✔
1567
  }
1568

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

1576
  for (int32_t iCol = 0; iCol < pTSchema->numOfCols; iCol++) {
76,171,504✔
1577
    int32_t targetCid = pTSchema->columns[iCol].colId;
69,706,746✔
1578
    SColVal *pColVal = NULL;
69,707,214✔
1579

1580
    switch (strategy) {
69,707,214✔
1581
      case KEEP_CONSISTENCY:
3,788,770✔
1582
        if (nRow > 0)
3,788,770✔
1583
          pColVal = tRowFindColumnValue(aIter[nRow - 1], targetCid);
3,788,770✔
1584
        break;
3,788,770✔
1585

1586
      default:  // default using PREFER_NON_NULL strategy
65,918,444✔
1587
      case PREFER_NON_NULL:
1588
        for (int32_t iRow = nRow - 1; iRow >= 0; --iRow) {
102,673,566✔
1589
          SColVal *pColValT = tRowFindColumnValue(aIter[iRow], targetCid);
85,661,999✔
1590

1591
          if (COL_VAL_IS_VALUE(pColValT)) {
85,661,927✔
1592
            pColVal = pColValT;
48,906,841✔
1593
            break;
48,906,841✔
1594
          } else if (pColVal == NULL) {
36,755,122✔
1595
            pColVal = pColValT;
18,600,787✔
1596
          }
1597
        }
1598
        break;
65,918,408✔
1599
    }
1600

1601
    if (pColVal) {
69,707,178✔
1602
      if (taosArrayPush(aColVal, pColVal) == NULL) {
69,707,934✔
1603
        code = terrno;
×
1604
        goto _exit;
×
1605
      }
1606
    }
1607
  }
1608

1609
  // build
1610
  SRowBuildScanInfo sinfo = {0};
6,462,670✔
1611
  code = tRowBuild(aColVal, pTSchema, &pRow, &sinfo);
6,464,326✔
1612

1613
  if (code) goto _exit;
6,464,002✔
1614

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

1621
_exit:
6,462,670✔
1622
  if (aIter) {
6,464,146✔
1623
    for (int32_t i = 0; i < nRow; i++) {
44,180,741✔
1624
      tRowIterClose(&aIter[i]);
37,716,559✔
1625
    }
1626
    taosMemoryFree(aIter);
6,464,182✔
1627
  }
1628
  if (aColVal) taosArrayDestroy(aColVal);
6,464,146✔
1629
  if (code) tRowDestroy(pRow);
6,463,822✔
1630
  return code;
6,463,822✔
1631
}
1632
static int32_t tBlobSetTransferTo(SBlobSet *pSrc, SBlobSet *pDst, SColVal *pVal) {
7,350✔
1633
  int32_t code = 0;
7,350✔
1634
  int32_t lino = 0;
7,350✔
1635
  if (COL_VAL_IS_NULL(pVal) || pVal->value.pData == NULL) {
14,700✔
1636
    int8_t type = COL_VAL_IS_NULL(pVal) ? TSDB_DATA_BLOB_NULL_VALUE : TSDB_DATA_BLOB_EMPTY_VALUE;
7,350✔
1637
    code = addEmptyItemToBlobSet(pDst, type, NULL);
7,350✔
1638
    TAOS_CHECK_GOTO(code, &lino, _error);
7,350✔
1639
  } else {
1640
    uint64_t seq = 0;
×
1641
    if (tGetU64(pVal->value.pData, &seq) < 0) {
×
1642
      uError("tBlobSetTransferTo: invalid blob value, seq %p", pVal->value.pData);
×
1643
      return TSDB_CODE_INVALID_PARA;
×
1644
    }
1645

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

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

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

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

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

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

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

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

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

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

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

1710
  tBlobSetSwap(pBlob, pTempBlob);
735✔
1711

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

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

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

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

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

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

1755
    tRowGetKey(row1, &key1);
×
1756

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

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

1765
      iEnd++;
×
1766
    }
1767

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

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

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

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

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

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

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

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

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

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

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

1848
  // build
1849

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

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

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

1870
  return code;
×
1871
}
1872

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

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

1885
  int32_t iStart = 0;
1,572,021✔
1886
  while (iStart < aRowP->size) {
35,533,329✔
1887
    SRowKey key1;
33,953,453✔
1888
    SRow   *row1 = (SRow *)taosArrayGetP(aRowP, iStart);
33,961,272✔
1889

1890
    tRowGetKey(row1, &key1);
67,923,264✔
1891

1892
    int32_t iEnd = iStart + 1;
33,961,272✔
1893
    while (iEnd < aRowP->size) {
65,212,497✔
1894
      SRowKey key2;
63,633,888✔
1895
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
63,641,520✔
1896
      tRowGetKey(row2, &key2);
127,283,256✔
1897

1898
      if (tRowKeyCompare(&key1, &key2) != 0) break;
63,642,816✔
1899

1900
      iEnd++;
31,252,953✔
1901
    }
1902

1903
    if (iEnd - iStart > 1) {
33,961,776✔
1904
      code = tRowMergeImpl(aRowP, pTSchema, iStart, iEnd, strategy);
6,464,218✔
1905
      if (code) return code;
6,463,714✔
1906
    }
1907

1908
    // the array is also changing, so the iStart just ++ instead of iEnd
1909
    iStart++;
33,961,272✔
1910
  }
1911

1912
  return code;
1,572,021✔
1913
}
1914

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

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

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

1934
    tRowGetKey(row1, &key1);
14,700✔
1935

1936
    int32_t iEnd = iStart + 1;
7,350✔
1937
    while (iEnd < aRowP->size) {
7,350✔
1938
      SRowKey key2;
6,615✔
1939
      SRow   *row2 = (SRow *)taosArrayGetP(aRowP, iEnd);
6,615✔
1940
      tRowGetKey(row2, &key2);
13,230✔
1941

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

1944
      iEnd++;
×
1945
    }
1946

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

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

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

1966
    code = tRowMergeAndRebuildBlob(aRowP, pTSchema, pBlobSet);
×
1967
  }
1968
  return code;
735✔
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) {
42,312,034✔
1992
  if (!(pRow->sver == pTSchema->version)) return TSDB_CODE_INVALID_PARA;
42,312,034✔
1993

1994
  int32_t code = 0;
42,312,587✔
1995

1996
  SRowIter *pIter = taosMemoryCalloc(1, sizeof(*pIter));
42,312,587✔
1997
  if (pIter == NULL) {
42,310,327✔
1998
    code = terrno;
×
1999
    goto _exit;
×
2000
  }
2001

2002
  pIter->pRow = pRow;
42,310,327✔
2003
  pIter->pTSchema = pTSchema;
42,310,327✔
2004
  pIter->iTColumn = 0;
42,309,960✔
2005

2006
  if (pRow->flag == HAS_NONE || pRow->flag == HAS_NULL) goto _exit;
42,310,435✔
2007

2008
  uint8_t         *data = pRow->data;
41,971,962✔
2009
  SPrimaryKeyIndex index;
41,972,079✔
2010
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
44,508,330✔
2011
    data += tGetPrimaryKeyIndex(data, &index);
2,533,102✔
2012
  }
2013

2014
  if (pRow->flag >> 4) {
41,974,787✔
2015
    pIter->iCol = 0;
6,750,282✔
2016
    pIter->pIdx = (SKVIdx *)data;
6,750,282✔
2017
    if (pRow->flag & KV_FLG_LIT) {
6,750,282✔
2018
      pIter->pv = pIter->pIdx->idx + pIter->pIdx->nCol;
6,750,282✔
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) {
35,225,204✔
2026
      case (HAS_NULL | HAS_NONE):
1,822✔
2027
        pIter->pb = data;
1,822✔
2028
        break;
1,822✔
2029
      case HAS_VALUE:
32,579,672✔
2030
        pIter->pf = data;
32,579,672✔
2031
        pIter->pv = pIter->pf + pTSchema->flen;
32,579,924✔
2032
        break;
32,580,320✔
2033
      case (HAS_VALUE | HAS_NONE):
2,643,769✔
2034
      case (HAS_VALUE | HAS_NULL):
2035
        pIter->pb = data;
2,643,769✔
2036
        pIter->pf = data + BIT1_SIZE(pTSchema->numOfCols - 1);
2,644,172✔
2037
        pIter->pv = pIter->pf + pTSchema->flen;
2,643,769✔
2038
        break;
2,643,769✔
2039
      case (HAS_VALUE | HAS_NULL | HAS_NONE):
36✔
2040
        pIter->pb = data;
36✔
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:
42,313,224✔
2050
  if (code) {
42,313,044✔
2051
    *ppIter = NULL;
×
2052
  } else {
2053
    *ppIter = pIter;
42,313,044✔
2054
  }
2055
  return code;
42,312,972✔
2056
}
2057

2058
void tRowIterClose(SRowIter **ppIter) {
42,312,684✔
2059
  SRowIter *pIter = *ppIter;
42,312,684✔
2060
  if (pIter) {
42,312,792✔
2061
    taosMemoryFree(pIter);
42,312,792✔
2062
  }
2063
  *ppIter = NULL;
42,311,631✔
2064
}
42,311,667✔
2065

2066
SColVal *tRowIterNext(SRowIter *pIter) {
198,429,886✔
2067
  if (pIter->iTColumn >= pIter->pTSchema->numOfCols) {
198,429,886✔
2068
    return NULL;
4,124,731✔
2069
  }
2070

2071
  STColumn *pTColumn = pIter->pTSchema->columns + pIter->iTColumn;
194,305,272✔
2072

2073
  // timestamp
2074
  if (0 == pIter->iTColumn) {
194,305,968✔
2075
    pIter->cv.cid = pTColumn->colId;
14,361,394✔
2076
    pIter->cv.value.type = pTColumn->type;
14,362,392✔
2077
    pIter->cv.flag = CV_FLAG_VALUE;
14,362,471✔
2078
    VALUE_SET_TRIVIAL_DATUM(&pIter->cv.value, pIter->pRow->ts);
14,360,333✔
2079
    goto _exit;
14,362,718✔
2080
  }
2081

2082
  if (pIter->pRow->flag == HAS_NONE) {
179,946,419✔
2083
    pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
4,926,502✔
2084
    goto _exit;
4,926,502✔
2085
  }
2086

2087
  if (pIter->pRow->flag == HAS_NULL) {
175,018,162✔
2088
    pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
321,768✔
2089
    goto _exit;
321,365✔
2090
  }
2091

2092
  if (pIter->pRow->flag >> 4) {  // KV
174,696,248✔
2093
    if (pIter->iCol < pIter->pIdx->nCol) {
135,964,463✔
2094
      uint8_t *pData;
2095

2096
      if (pIter->pRow->flag & KV_FLG_LIT) {
61,398,329✔
2097
        pData = pIter->pv + ((uint8_t *)pIter->pIdx->idx)[pIter->iCol];
61,398,329✔
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;
61,398,329✔
2105
      pData += tGetI16v(pData, &cid);
61,398,329✔
2106

2107
      if (TABS(cid) == pTColumn->colId) {
61,398,329✔
2108
        if (cid < 0) {
60,182,795✔
2109
          pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
3,134,706✔
2110
        } else {
2111
          pIter->cv.cid = pTColumn->colId;
57,048,089✔
2112
          pIter->cv.value.type = pTColumn->type;
57,048,089✔
2113
          pIter->cv.flag = CV_FLAG_VALUE;
57,048,089✔
2114

2115
          if (IS_VAR_DATA_TYPE(pTColumn->type)) {
57,048,089✔
2116
            pData += tGetU32v(pData, &pIter->cv.value.nData);
22,126,738✔
2117
            if (pIter->cv.value.nData > 0) {
11,063,369✔
2118
              pIter->cv.value.pData = pData;
11,063,369✔
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);
45,984,720✔
2127
          }
2128
        }
2129

2130
        pIter->iCol++;
60,182,795✔
2131
        goto _exit;
60,182,795✔
2132
      } else if (TABS(cid) > pTColumn->colId) {
1,215,534✔
2133
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
1,215,534✔
2134
        goto _exit;
1,215,534✔
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);
74,566,134✔
2141
      goto _exit;
74,566,134✔
2142
    }
2143
  } else {  // Tuple
2144
    uint8_t bv = BIT_FLG_VALUE;
38,699,266✔
2145
    if (pIter->pb) {
38,699,266✔
2146
      switch (pIter->pRow->flag) {
14,005,814✔
2147
        case (HAS_NULL | HAS_NONE):
10,482✔
2148
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
10,482✔
2149
          break;
10,482✔
2150
        case (HAS_VALUE | HAS_NONE):
8,526,641✔
2151
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1);
8,526,641✔
2152
          if (bv) bv++;
8,526,641✔
2153
          break;
8,526,641✔
2154
        case (HAS_VALUE | HAS_NULL):
5,468,691✔
2155
          bv = GET_BIT1(pIter->pb, pIter->iTColumn - 1) + 1;
5,468,691✔
2156
          break;
5,469,094✔
2157
        case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2158
          bv = GET_BIT2(pIter->pb, pIter->iTColumn - 1);
×
2159
          break;
×
2160
        default:
×
2161
          break;
×
2162
      }
2163

2164
      if (bv == BIT_FLG_NONE) {
14,006,217✔
2165
        pIter->cv = COL_VAL_NONE(pTColumn->colId, pTColumn->type);
864,058✔
2166
        goto _exit;
864,058✔
2167
      } else if (bv == BIT_FLG_NULL) {
13,142,159✔
2168
        pIter->cv = COL_VAL_NULL(pTColumn->colId, pTColumn->type);
957,292✔
2169
        goto _exit;
956,889✔
2170
      }
2171
    }
2172

2173
    pIter->cv.cid = pTColumn->colId;
36,910,683✔
2174
    pIter->cv.value.type = pTColumn->type;
36,911,698✔
2175
    pIter->cv.flag = CV_FLAG_VALUE;
36,911,480✔
2176
    if (IS_VAR_DATA_TYPE(pTColumn->type)) {
43,953,635✔
2177
      uint8_t *pData = pIter->pv + *(int32_t *)(pIter->pf + pTColumn->offset);
7,044,707✔
2178
      pData += tGetU32v(pData, &pIter->cv.value.nData);
14,084,072✔
2179
      if (pIter->cv.value.nData > 0) {
7,042,371✔
2180
        pIter->cv.value.pData = pData;
6,994,347✔
2181
      } else {
2182
        pIter->cv.value.pData = NULL;
48,348✔
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]);
29,868,132✔
2189
    }
2190
    goto _exit;
36,908,273✔
2191
  }
2192

2193
_exit:
194,304,268✔
2194
  pIter->iTColumn++;
194,304,268✔
2195
  return &pIter->cv;
194,303,123✔
2196
}
2197

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

2201
  if (flag) return code;
700,228✔
2202

2203
  for (int32_t iColData = 0; iColData < nColData; iColData++) {
5,571,741✔
2204
    code = tColDataAppendValueImpl[aColData[iColData].flag][CV_FLAG_NONE](&aColData[iColData], NULL, 0);
5,115,071✔
2205
    if (code) return code;
5,114,657✔
2206
  }
2207

2208
  return code;
456,670✔
2209
}
2210
static int32_t tRowNullUpsertColData(SColData *aColData, int32_t nColData, STSchema *pSchema, int32_t flag) {
49,124,979✔
2211
  int32_t code = 0;
49,124,979✔
2212

2213
  int32_t   iColData = 0;
49,124,979✔
2214
  SColData *pColData = &aColData[iColData];
49,124,979✔
2215
  int32_t   iTColumn = 1;
49,130,269✔
2216
  STColumn *pTColumn = &pSchema->columns[iTColumn];
49,130,269✔
2217

2218
  while (pColData) {
323,968,322✔
2219
    if (pTColumn) {
274,840,469✔
2220
      if (pTColumn->colId == pColData->cid) {  // NULL
274,840,469✔
2221
        if (flag == 0) {
274,836,759✔
2222
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
274,836,759✔
2223
        } else {
2224
          code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
×
2225
        }
2226
        if (code) goto _exit;
274,836,861✔
2227

2228
        pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
274,836,861✔
2229
        pTColumn = (++iTColumn < pSchema->numOfCols) ? &pSchema->columns[iTColumn] : NULL;
274,822,660✔
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:
49,127,853✔
2243
  return code;
49,127,853✔
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);
54,955,524✔
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):
166,535✔
2267
      pb = data;
166,535✔
2268
      break;
166,535✔
2269
    case (HAS_VALUE | HAS_NONE):
368,722,824✔
2270
    case (HAS_VALUE | HAS_NULL):
2271
      pb = data;
368,722,824✔
2272
      pf = pb + BIT1_SIZE(pTSchema->numOfCols - 1);
368,722,824✔
2273
      pv = pf + pTSchema->flen;
368,715,908✔
2274
      break;
368,718,960✔
2275
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2276
      pb = data;
×
2277
      pf = pb + BIT2_SIZE(pTSchema->numOfCols - 1);
×
2278
      pv = pf + pTSchema->flen;
×
2279
      break;
×
2280
    default:
×
2281
      return TSDB_CODE_INVALID_DATA_FMT;
×
2282
  }
2283

2284
  while (pColData) {
2,147,483,647✔
2285
    if (pTColumn) {
2,147,483,647✔
2286
      if (pTColumn->colId == pColData->cid) {
2,147,483,647✔
2287
        if (!(pTColumn->type == pColData->type)) {
2,147,483,647✔
2288
          return TSDB_CODE_INVALID_PARA;
×
2289
        }
2290
        if (pb) {
2,147,483,647✔
2291
          uint8_t bv;
2292
          switch (pRow->flag) {
1,589,961,759✔
2293
            case (HAS_NULL | HAS_NONE):
3,288,449✔
2294
              bv = GET_BIT1(pb, iTColumn - 1);
3,288,449✔
2295
              break;
3,288,449✔
2296
            case (HAS_VALUE | HAS_NONE):
57,759,312✔
2297
              bv = GET_BIT1(pb, iTColumn - 1);
57,759,312✔
2298
              if (bv) bv++;
57,766,402✔
2299
              break;
57,766,402✔
2300
            case (HAS_VALUE | HAS_NULL):
1,529,242,005✔
2301
              bv = GET_BIT1(pb, iTColumn - 1) + 1;
1,529,242,005✔
2302
              break;
1,529,272,673✔
2303
            case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
2304
              bv = GET_BIT2(pb, iTColumn - 1);
×
2305
              break;
×
2306
            default:
×
2307
              return TSDB_CODE_INVALID_DATA_FMT;
×
2308
          }
2309

2310
          if (bv == BIT_FLG_NONE) {
1,590,327,524✔
2311
            if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0)))
7,297,512✔
2312
              goto _exit;
×
2313
            goto _continue;
7,298,578✔
2314
          } else if (bv == BIT_FLG_NULL) {
1,583,030,012✔
2315
            if (flag == 0) {
442,336,802✔
2316
              code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
441,965,693✔
2317
            } else {
2318
              code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
371,109✔
2319
            }
2320
            if (code) goto _exit;
442,364,978✔
2321
            goto _continue;
442,364,978✔
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);
55,450,645✔
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,
323,842,860✔
2341
                                                                          TYPE_BYTES[pColData->type], flag > 0);
162,049,712✔
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
61,543,461✔
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;
61,537,851✔
2354
      }
2355
    } else {
2356
      if (flag == 0 && (code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0))) goto _exit;
35,080,579✔
2357
      pColData = (++iColData < nColData) ? &aColData[iColData] : NULL;
35,080,579✔
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,584,998,887✔
2365
  int32_t code = 0;
1,584,998,887✔
2366

2367
  uint8_t  *pv = NULL;
1,584,998,887✔
2368
  int32_t   iColData = 0;
1,584,998,887✔
2369
  SColData *pColData = &aColData[iColData];
1,584,998,887✔
2370
  int32_t   iTColumn = 1;
1,585,349,916✔
2371
  STColumn *pTColumn = &pTSchema->columns[iTColumn];
1,585,349,916✔
2372
  int32_t   iCol = 0;
1,585,384,094✔
2373

2374
  // primary keys
2375
  uint8_t         *data = pRow->data;
1,585,384,094✔
2376
  SPrimaryKeyIndex index;
1,585,374,512✔
2377
  for (int32_t i = 0; i < pRow->numOfPKs; i++) {
1,590,519,065✔
2378
    data += tGetPrimaryKeyIndex(data, &index);
5,140,053✔
2379
  }
2380

2381
  SKVIdx *pKVIdx = (SKVIdx *)data;
1,585,378,047✔
2382
  if (pRow->flag & KV_FLG_LIT) {
1,585,378,047✔
2383
    pv = pKVIdx->idx + pKVIdx->nCol;
1,575,558,382✔
2384
  } else if (pRow->flag & KV_FLG_MID) {
9,799,579✔
2385
    pv = pKVIdx->idx + (pKVIdx->nCol << 1);
9,816,842✔
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) {
265,425,875✔
2400
            pData = pv + ((uint16_t *)pKVIdx->idx)[iCol];
265,531,993✔
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,924,771,743✔
2413
                code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
1,924,740,323✔
2414
              } else {
2415
                code = tColDataUpdateValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0, flag > 0);
31,420✔
2416
              }
2417
              if (code) goto _exit;
1,921,921,517✔
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);
774,857,567✔
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);
4,480,763✔
2429
              }
2430
              if (code) goto _exit;
2,147,483,647✔
2431
            }
2432
            iCol++;
2,147,483,647✔
2433
            goto _continue;
2,147,483,647✔
2434
          } else if (TABS(cid) > pTColumn->colId) {  // NONE
2,147,483,647✔
2435
            break;
2,147,483,647✔
2436
          } else {
2437
            iCol++;
4,110,000✔
2438
          }
2439
        }
2440

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

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

2458
_exit:
1,586,022,536✔
2459
  return code;
1,585,288,426✔
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);
700,642✔
2471
  } else if (pRow->flag == HAS_NULL) {
2,147,483,647✔
2472
    return tRowNullUpsertColData(aColData, nColData, pTSchema, flag);
49,129,432✔
2473
  } else if (pRow->flag >> 4) {  // KV row
2,147,483,647✔
2474
    return tRowKVUpsertColData(pRow, pTSchema, aColData, nColData, flag);
1,585,258,556✔
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) {
2,147,483,647✔
2481
  key->numOfPKs = row->numOfPKs;
2,147,483,647✔
2482

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

2487
  SPrimaryKeyIndex indices[TD_MAX_PK_COLS];
2,147,483,647✔
2488

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

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

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

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

2504
    if (IS_VAR_DATA_TYPE(indices[i].type)) {
2,147,483,647✔
2505
      key->pks[i].pData = tdata;
744,564,445✔
2506
      key->pks[i].pData += tGetU32v(key->pks[i].pData, &key->pks[i].nData);
1,489,118,452✔
2507
    } else {
2508
      valueSetDatum(key->pks + i, indices[i].type, tdata, tDataTypes[indices[i].type].bytes);
2,082,558,746✔
2509
    }
2510
  }
2511
}
2512

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

2524
int32_t tValueCompare(const SValue *tv1, const SValue *tv2) {
1,151,307,256✔
2525
  switch (tv1->type) {
1,151,307,256✔
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:
486,920,509✔
2532
      T_COMPARE_SCALAR_VALUE(int32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
486,920,509✔
2533
    case TSDB_DATA_TYPE_BIGINT:
141,373,133✔
2534
    case TSDB_DATA_TYPE_TIMESTAMP:
2535
      T_COMPARE_SCALAR_VALUE(int64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
141,373,133✔
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:
141,247,150✔
2545
      T_COMPARE_SCALAR_VALUE(uint32_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
141,247,150✔
2546
    case TSDB_DATA_TYPE_UBIGINT:
141,240,274✔
2547
      T_COMPARE_SCALAR_VALUE(uint64_t, &VALUE_GET_TRIVIAL_DATUM(tv1), &VALUE_GET_TRIVIAL_DATUM(tv2));
141,240,274✔
2548
    case TSDB_DATA_TYPE_GEOMETRY:
240,528,535✔
2549
    case TSDB_DATA_TYPE_BINARY: {
2550
      int32_t ret = strncmp((const char *)tv1->pData, (const char *)tv2->pData, TMIN(tv1->nData, tv2->nData));
240,528,535✔
2551
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
240,531,197✔
2552
    }
2553
    case TSDB_DATA_TYPE_NCHAR: {
×
2554
      int32_t ret = taosUcs4Compare((TdUcs4 *)tv1->pData, (TdUcs4 *)tv2->pData,
×
2555
                                    tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2556
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2557
    }
2558
    case TSDB_DATA_TYPE_VARBINARY: {
×
2559
      int32_t ret = memcmp(tv1->pData, tv2->pData, tv1->nData < tv2->nData ? tv1->nData : tv2->nData);
×
2560
      return ret ? ret : (tv1->nData < tv2->nData ? -1 : (tv1->nData > tv2->nData ? 1 : 0));
×
2561
    }
2562
    default:
×
2563
      break;
×
2564
  }
2565

2566
  return 0;
×
2567
}
2568

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

2579
  if (key1->numOfPKs == key2->numOfPKs) {
2,147,483,647✔
2580
    for (uint8_t iKey = 0; iKey < key1->numOfPKs; iKey++) {
2,147,483,647✔
2581
      int32_t ret = tValueCompare(&key1->pks[iKey], &key2->pks[iKey]);
1,151,397,772✔
2582
      if (ret) return ret;
1,151,167,914✔
2583
    }
2584
  } else if (key1->numOfPKs < key2->numOfPKs) {
5,922✔
2585
    return -1;
5,922✔
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) {
114,703,476✔
2599
      SValue *pVal = &pDst->pks[i];
57,352,976✔
2600
      pVal->type = pSrc->pks[i].type;
57,352,357✔
2601

2602
      valueCloneDatum(pVal, pSrc->pks + i, pVal->type);
57,351,119✔
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;
1,859,167,397✔
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,007,078,442✔
2616
}
2617
static int tTagValJsonCmprFn(const void *p1, const void *p2) {
3,914,921✔
2618
  return strcmp(((STagVal *)p1)[0].pKey, ((STagVal *)p2)[0].pKey);
3,914,921✔
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) {
319,160,306✔
2715
  int32_t n = 0;
319,160,306✔
2716

2717
  // key
2718
  if (isJson) {
319,160,306✔
2719
    n += tPutCStr(p ? p + n : p, pTagVal->pKey);
2,364,564✔
2720
  } else {
2721
    n += tPutI16v(p ? p + n : p, pTagVal->cid);
635,941,589✔
2722
  }
2723

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

2727
  // value
2728
  if (IS_VAR_DATA_TYPE(pTagVal->type)) {
319,166,627✔
2729
    n += tPutBinary(p ? p + n : p, pTagVal->pData, pTagVal->nData);
275,006,548✔
2730
  } else {
2731
    p = p ? p + n : p;
181,694,963✔
2732
    n += tDataTypes[pTagVal->type].bytes;
181,692,976✔
2733
    if (p) (void)memcpy(p, &(pTagVal->i64), tDataTypes[pTagVal->type].bytes);
181,667,614✔
2734
  }
2735

2736
  return n;
319,130,546✔
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);
8,907,687✔
2744
  } else {
2745
    n += tGetI16v(p + n, &pTagVal->cid);
2,147,483,647✔
2746
  }
2747

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

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

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

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

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

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

2779
  // sort
2780
  if (isJson) {
69,439,715✔
2781
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValJsonCmprFn);
381,964✔
2782
  } else {
2783
    taosSort(pArray->pData, nTag, sizeof(STagVal), tTagValCmprFn);
69,057,751✔
2784
  }
2785

2786
  // get size
2787
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
228,997,941✔
2788
    szTag += tPutTagVal(NULL, (STagVal *)taosArrayGet(pArray, iTag), isJson);
159,597,058✔
2789
  }
2790
  if (szTag <= INT8_MAX) {
69,400,883✔
2791
    szTag = szTag + sizeof(STag) + sizeof(int8_t) * nTag;
61,713,133✔
2792
  } else {
2793
    szTag = szTag + sizeof(STag) + sizeof(int16_t) * nTag;
7,687,750✔
2794
    isLarge = 1;
7,687,750✔
2795
  }
2796

2797
  // build tag
2798
  (*ppTag) = (STag *)taosMemoryCalloc(szTag, 1);
69,400,883✔
2799
  if ((*ppTag) == NULL) {
69,447,821✔
2800
    code = terrno;
×
2801
    goto _err;
×
2802
  }
2803
  (*ppTag)->flags = 0;
69,443,271✔
2804
  if (isJson) {
69,438,730✔
2805
    (*ppTag)->flags |= TD_TAG_JSON;
381,964✔
2806
  }
2807
  if (isLarge) {
69,438,730✔
2808
    (*ppTag)->flags |= TD_TAG_LARGE;
7,692,655✔
2809
  }
2810
  (*ppTag)->len = szTag;
69,438,730✔
2811
  (*ppTag)->nTag = nTag;
69,464,640✔
2812
  (*ppTag)->ver = version;
69,439,211✔
2813

2814
  if (isLarge) {
69,399,398✔
2815
    p = (uint8_t *)&((int16_t *)(*ppTag)->idx)[nTag];
7,692,655✔
2816
  } else {
2817
    p = (uint8_t *)&(*ppTag)->idx[nTag];
61,706,743✔
2818
  }
2819
  n = 0;
69,426,598✔
2820
  for (int16_t iTag = 0; iTag < nTag; iTag++) {
228,960,640✔
2821
    if (isLarge) {
159,517,646✔
2822
      ((int16_t *)(*ppTag)->idx)[iTag] = n;
44,461,398✔
2823
    } else {
2824
      (*ppTag)->idx[iTag] = n;
115,056,248✔
2825
    }
2826
    n += tPutTagVal(p + n, (STagVal *)taosArrayGet(pArray, iTag), isJson);
159,572,597✔
2827
  }
2828
#ifdef TD_DEBUG_PRINT_TAG
2829
  debugPrintSTag(*ppTag, __func__, __LINE__);
2830
#endif
2831

2832
  return code;
69,442,994✔
2833

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

2838
void tTagFree(STag *pTag) {
1,053,596,485✔
2839
  if (pTag) taosMemoryFree(pTag);
1,053,596,485✔
2840
}
1,053,596,485✔
2841

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

2847
  char  *data = NULL;
712,523,592✔
2848
  int8_t typeBytes = 0;
712,523,592✔
2849
  if (isJson) {
712,523,592✔
2850
    typeBytes = CHAR_BYTES;
2,024,115✔
2851
  }
2852

2853
  if (IS_VAR_DATA_TYPE(value->type)) {
712,523,592✔
2854
    data = taosMemoryCalloc(1, typeBytes + VARSTR_HEADER_SIZE + value->nData);
289,848,885✔
2855
    if (data == NULL) {
289,913,694✔
2856
      return NULL;
×
2857
    }
2858

2859
    if (isJson) {
289,913,694✔
2860
      *data = value->type;
1,562,092✔
2861
    }
2862

2863
    varDataLen(data + typeBytes) = value->nData;
289,913,694✔
2864
    (void)memcpy(varDataVal(data + typeBytes), value->pData, value->nData);
289,900,681✔
2865
  } else {
2866
    data = ((char *)&(value->i64)) - typeBytes;  // json with type
422,719,578✔
2867
  }
2868

2869
  return data;
712,664,639✔
2870
}
2871

2872
bool tTagGet(const STag *pTag, STagVal *pTagVal) {
2,062,437,339✔
2873
  if (!pTag || !pTagVal) {
2,062,437,339✔
2874
    return false;
36✔
2875
  }
2876

2877
  int16_t  lidx = 0;
2,062,668,750✔
2878
  int16_t  ridx = pTag->nTag - 1;
2,062,668,750✔
2879
  int16_t  midx;
2880
  uint8_t *p;
2881
  int8_t   isJson = pTag->flags & TD_TAG_JSON;
2,062,624,741✔
2882
  int8_t   isLarge = pTag->flags & TD_TAG_LARGE;
2,062,678,169✔
2883
  int16_t  offset;
2884
  STagVal  tv;
2,062,426,355✔
2885
  int      c;
2886

2887
  if (isLarge) {
2,062,584,090✔
2888
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
355,183,479✔
2889
  } else {
2890
    p = (uint8_t *)&pTag->idx[pTag->nTag];
1,707,400,611✔
2891
  }
2892

2893
  pTagVal->type = TSDB_DATA_TYPE_NULL;
2,062,651,335✔
2894
  pTagVal->pData = NULL;
2,062,663,312✔
2895
  pTagVal->nData = 0;
2,062,671,864✔
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];
1,222,891,887✔
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,417,463✔
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;
1,625,702,558✔
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,008,744,501✔
2917
      return true;
2,009,098,073✔
2918
    }
2919
  }
2920
  return false;
53,584,554✔
2921
}
2922

2923
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag) {
247,247,178✔
2924
  return tEncodeBinary(pEncoder, (const uint8_t *)pTag, pTag->len);
494,570,135✔
2925
}
2926

2927
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag) { return tDecodeBinary(pDecoder, (uint8_t **)ppTag, NULL); }
1,595,630,878✔
2928

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

2936
  if (isLarge) {
825,485✔
2937
    p = (uint8_t *)&((int16_t *)pTag->idx)[pTag->nTag];
3,797✔
2938
  } else {
2939
    p = (uint8_t *)&pTag->idx[pTag->nTag];
821,688✔
2940
  }
2941

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

2948
  for (int16_t iTag = 0; iTag < pTag->nTag; iTag++) {
2,104,969✔
2949
    if (isLarge) {
1,279,484✔
2950
      offset = ((int16_t *)pTag->idx)[iTag];
5,078✔
2951
    } else {
2952
      offset = pTag->idx[iTag];
1,274,406✔
2953
    }
2954
    int32_t nt = tGetTagVal(p + offset, &tv, pTag->flags & TD_TAG_JSON);
1,279,484✔
2955
    if (taosArrayPush(*ppArray, &tv) == NULL) {
2,558,968✔
2956
      code = terrno;
×
2957
      goto _err;
×
2958
    }
2959
  }
2960

2961
  return code;
825,485✔
2962

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

2967

2968
void destroyTagVal(void *pTag) {
42,816,160✔
2969
  STagVal* pTagVal = (STagVal*)pTag;
42,816,160✔
2970
  if (pTagVal && IS_VAR_DATA_TYPE(pTagVal->type)) {
42,816,160✔
2971
    taosMemoryFree(pTagVal->pData);
18,834,600✔
2972
  }
2973
}
42,816,160✔
2974

2975
// STSchema ========================================
2976
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version) {
1,007,335,687✔
2977
  STSchema *pTSchema = taosMemoryCalloc(1, sizeof(STSchema) + sizeof(STColumn) * numOfCols);
1,007,335,687✔
2978
  if (pTSchema == NULL) {
1,007,282,763✔
2979
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
2980
    return NULL;
×
2981
  }
2982

2983
  pTSchema->numOfCols = numOfCols;
1,007,282,763✔
2984
  pTSchema->version = version;
1,007,310,024✔
2985

2986
  // timestamp column
2987
  if (!(aSchema[0].type == TSDB_DATA_TYPE_TIMESTAMP)) {
1,007,499,683✔
2988
    terrno = TSDB_CODE_INVALID_PARA;
×
2989
    taosMemoryFree(pTSchema);
×
2990
    return NULL;
×
2991
  }
2992
  if (!(aSchema[0].colId == PRIMARYKEY_TIMESTAMP_COL_ID)) {
1,007,336,964✔
2993
    terrno = TSDB_CODE_INVALID_PARA;
×
2994
    taosMemoryFree(pTSchema);
×
2995
    return NULL;
×
2996
  }
2997
  pTSchema->columns[0].colId = aSchema[0].colId;
1,007,271,178✔
2998
  pTSchema->columns[0].type = aSchema[0].type;
1,007,640,287✔
2999
  pTSchema->columns[0].flags = aSchema[0].flags;
1,007,618,521✔
3000
  pTSchema->columns[0].bytes = TYPE_BYTES[aSchema[0].type];
1,007,718,955✔
3001
  pTSchema->columns[0].offset = -1;
1,007,648,100✔
3002

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

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

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

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

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

3028
  return pTSchema;
1,007,784,644✔
3029
}
3030

3031
static int32_t tTColumnCompare(const void *p1, const void *p2) {
13,643,114✔
3032
  if (((STColumn *)p1)->colId < ((STColumn *)p2)->colId) {
13,643,114✔
3033
    return -1;
2,272,443✔
3034
  } else if (((STColumn *)p1)->colId > ((STColumn *)p2)->colId) {
11,370,671✔
3035
    return 1;
7,748,329✔
3036
  }
3037

3038
  return 0;
3,623,470✔
3039
}
3040

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

3046
  return taosbsearch(&tcol, pTSchema->columns, pTSchema->numOfCols, sizeof(STColumn), tTColumnCompare, TD_EQ);
3,624,034✔
3047
}
3048

3049
// SColData ========================================
3050
void tColDataDestroy(void *ph) {
1,100,961,582✔
3051
  if (ph) {
1,100,961,582✔
3052
    SColData *pColData = (SColData *)ph;
1,101,077,695✔
3053

3054
    tFree(pColData->pBitMap);
1,101,077,695✔
3055
    tFree(pColData->aOffset);
1,101,176,559✔
3056
    tFree(pColData->pData);
1,101,116,284✔
3057
  }
3058
}
1,100,991,101✔
3059

3060
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag) {
1,168,104,683✔
3061
  pColData->cid = cid;
1,168,104,683✔
3062
  pColData->type = type;
1,168,274,769✔
3063
  pColData->cflag = cflag;
1,168,276,725✔
3064
  tColDataClear(pColData);
1,168,294,217✔
3065
}
1,168,195,372✔
3066

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

3076
void tColDataDeepClear(SColData *pColData) {
39,862,962✔
3077
  pColData->pBitMap = NULL;
39,862,962✔
3078
  pColData->aOffset = NULL;
39,871,952✔
3079
  pColData->pData = NULL;
39,869,724✔
3080

3081
  tColDataClear(pColData);
39,875,756✔
3082
}
39,880,303✔
3083

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

3094
      if (nData) {
28,396,202✔
3095
        code = tRealloc(&pColData->pData, pColData->nData + BSE_SEQUECE_SIZE);
25,560,140✔
3096
        if (code) goto _exit;
25,560,140✔
3097
        (void)memcpy(pColData->pData + pColData->nData, pData, BSE_SEQUECE_SIZE);
25,560,140✔
3098
        pColData->nData += BSE_SEQUECE_SIZE;
25,560,140✔
3099
      } else {
3100
        // uint64_t zero = 0;
3101
        // (void)memcpy(pColData->pData + pColData->nData, &zero, BSE_SEQUECE_SIZE);
3102
        // pColData->nData += BSE_SEQUECE_SIZE;
3103
      }
3104

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

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

3132
_exit:
2,147,483,647✔
3133
  return code;
2,147,483,647✔
3134
}
3135
static FORCE_INLINE int32_t tColDataAppendValue00(SColData *pColData, uint8_t *pData, uint32_t nData) {
508,203,171✔
3136
  pColData->flag = HAS_VALUE;
508,289,480✔
3137
  pColData->numOfValue++;
508,316,744✔
3138
  return tColDataPutValue(pColData, pData, nData);
508,327,323✔
3139
}
3140
static FORCE_INLINE int32_t tColDataAppendValue01(SColData *pColData, uint8_t *pData, uint32_t nData) {
15,985,118✔
3141
  pColData->flag = HAS_NONE;
15,985,118✔
3142
  pColData->numOfNone++;
15,985,532✔
3143
  pColData->nVal++;
15,986,137✔
3144
  return 0;
15,985,309✔
3145
}
3146
static FORCE_INLINE int32_t tColDataAppendValue02(SColData *pColData, uint8_t *pData, uint32_t nData) {
29,312,268✔
3147
  pColData->flag = HAS_NULL;
29,332,503✔
3148
  pColData->numOfNull++;
29,338,529✔
3149
  pColData->nVal++;
29,338,617✔
3150
  return 0;
29,340,694✔
3151
}
3152
static FORCE_INLINE int32_t tColDataAppendValue10(SColData *pColData, uint8_t *pData, uint32_t nData) {
877,168✔
3153
  int32_t code = 0;
877,168✔
3154

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

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

3162
  pColData->flag |= HAS_VALUE;
877,582✔
3163
  pColData->numOfValue++;
877,582✔
3164

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

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

3187
  return tColDataPutValue(pColData, pData, nData);
877,582✔
3188
}
3189
static FORCE_INLINE int32_t tColDataAppendValue11(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,147,483,647✔
3190
  pColData->nVal++;
2,147,483,647✔
3191
  pColData->numOfNone++;
2,147,483,647✔
3192
  return 0;
2,147,483,647✔
3193
}
3194
static FORCE_INLINE int32_t tColDataAppendValue12(SColData *pColData, uint8_t *pData, uint32_t nData) {
61,348✔
3195
  int32_t code = 0;
81,652✔
3196

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

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

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

3208
  return code;
81,652✔
3209
}
3210
static FORCE_INLINE int32_t tColDataAppendValue20(SColData *pColData, uint8_t *pData, uint32_t nData) {
4,216,155✔
3211
  int32_t code = 0;
4,216,536✔
3212

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

3217
  memset(pColData->pBitMap, 0, nBit);
4,216,536✔
3218
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
4,216,536✔
3219

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

3223
  if (pColData->nVal) {
4,216,536✔
3224
    if (IS_VAR_DATA_TYPE(pColData->type)) {
5,484,282✔
3225
      int32_t nOffset = sizeof(int32_t) * pColData->nVal;
1,267,746✔
3226
      code = tRealloc((uint8_t **)(&pColData->aOffset), nOffset);
1,267,746✔
3227
      if (code) return code;
1,267,746✔
3228
      if (!IS_STR_DATA_BLOB(pColData->type)) {
1,267,746✔
3229
        memset(pColData->aOffset, 0, nOffset);
1,262,293✔
3230
      }
3231
    } else {
3232
      pColData->nData = tDataTypes[pColData->type].bytes * pColData->nVal;
2,948,790✔
3233
      code = tRealloc(&pColData->pData, pColData->nData);
2,948,790✔
3234
      if (code) return code;
2,948,790✔
3235
      memset(pColData->pData, 0, pColData->nData);
2,948,790✔
3236
    }
3237
  }
3238

3239
  return tColDataPutValue(pColData, pData, nData);
4,216,536✔
3240
}
3241
static FORCE_INLINE int32_t tColDataAppendValue21(SColData *pColData, uint8_t *pData, uint32_t nData) {
16,868✔
3242
  int32_t code = 0;
16,868✔
3243

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

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

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

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

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

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

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

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

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

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

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

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

3306
  return code;
708,384✔
3307
}
3308
static FORCE_INLINE int32_t tColDataAppendValue32(SColData *pColData, uint8_t *pData, uint32_t nData) {
130,848✔
3309
  int32_t code = 0;
130,848✔
3310

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

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

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

3327
  pColData->flag |= HAS_NONE;
7,918,977✔
3328
  pColData->numOfNone++;
7,918,977✔
3329

3330
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
7,918,977✔
3331
  code = tRealloc(&pColData->pBitMap, nBit);
7,918,977✔
3332
  if (code) return code;
7,918,354✔
3333

3334
  memset(pColData->pBitMap, 255, nBit);
7,918,354✔
3335
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
7,918,977✔
3336

3337
  return tColDataPutValue(pColData, NULL, 0);
7,919,600✔
3338
}
3339
static FORCE_INLINE int32_t tColDataAppendValue42(SColData *pColData, uint8_t *pData, uint32_t nData) {
19,877,042✔
3340
  int32_t code = 0;
20,439,500✔
3341

3342
  pColData->flag |= HAS_NULL;
20,439,500✔
3343
  pColData->numOfNull++;
20,439,966✔
3344

3345
  int32_t nBit = BIT1_SIZE(pColData->nVal + 1);
20,439,966✔
3346
  code = tRealloc(&pColData->pBitMap, nBit);
20,439,966✔
3347
  if (code) return code;
20,439,572✔
3348

3349
  memset(pColData->pBitMap, 255, nBit);
20,439,572✔
3350
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
20,440,038✔
3351

3352
  return tColDataPutValue(pColData, NULL, 0);
20,440,420✔
3353
}
3354
static FORCE_INLINE int32_t tColDataAppendValue50(SColData *pColData, uint8_t *pData, uint32_t nData) {
300,937,232✔
3355
  int32_t code = 0;
300,937,810✔
3356

3357
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
300,937,810✔
3358
  if (code) return code;
300,941,303✔
3359

3360
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 1);
300,941,303✔
3361
  pColData->numOfValue++;
300,948,437✔
3362

3363
  return tColDataPutValue(pColData, pData, nData);
300,930,614✔
3364
}
3365
static FORCE_INLINE int32_t tColDataAppendValue51(SColData *pColData, uint8_t *pData, uint32_t nData) {
766,139,148✔
3366
  int32_t code = 0;
766,139,148✔
3367

3368
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
766,139,148✔
3369
  if (code) return code;
766,153,138✔
3370

3371
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
766,153,138✔
3372
  pColData->numOfNone++;
766,153,138✔
3373

3374
  return tColDataPutValue(pColData, NULL, 0);
766,156,014✔
3375
}
3376
static FORCE_INLINE int32_t tColDataAppendValue52(SColData *pColData, uint8_t *pData, uint32_t nData) {
90,740✔
3377
  int32_t code = 0;
90,740✔
3378

3379
  pColData->flag |= HAS_NULL;
90,740✔
3380
  pColData->numOfNull++;
90,740✔
3381

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

3386
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,213,016✔
3387
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 0);
2,122,276✔
3388
  }
3389
  SET_BIT2_EX(pBitMap, pColData->nVal, 1);
90,740✔
3390

3391
  tFree(pColData->pBitMap);
90,740✔
3392
  pColData->pBitMap = pBitMap;
90,740✔
3393

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

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

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

3409
  pColData->flag |= HAS_NONE;
1,820,558✔
3410
  pColData->numOfNone++;
1,820,558✔
3411

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

3416
  for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
156,123,454✔
3417
    SET_BIT2_EX(pBitMap, iVal, GET_BIT1(pColData->pBitMap, iVal) ? 2 : 1);
154,302,896✔
3418
  }
3419
  SET_BIT2_EX(pBitMap, pColData->nVal, 0);
1,820,558✔
3420

3421
  tFree(pColData->pBitMap);
1,820,558✔
3422
  pColData->pBitMap = pBitMap;
1,820,558✔
3423

3424
  return tColDataPutValue(pColData, NULL, 0);
1,820,558✔
3425
}
3426
static FORCE_INLINE int32_t tColDataAppendValue62(SColData *pColData, uint8_t *pData, uint32_t nData) {
1,136,619,387✔
3427
  int32_t code = 0;
1,137,803,868✔
3428

3429
  code = tRealloc(&pColData->pBitMap, BIT1_SIZE(pColData->nVal + 1));
1,137,803,868✔
3430
  if (code) return code;
1,137,892,514✔
3431
  SET_BIT1_EX(pColData->pBitMap, pColData->nVal, 0);
1,137,892,514✔
3432
  pColData->numOfNull++;
1,137,923,709✔
3433

3434
  return tColDataPutValue(pColData, NULL, 0);
1,137,915,591✔
3435
}
3436
static FORCE_INLINE int32_t tColDataAppendValue70(SColData *pColData, uint8_t *pData, uint32_t nData) {
2,907,000✔
3437
  int32_t code = 0;
3,010,776✔
3438

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

3444
  return tColDataPutValue(pColData, pData, nData);
3,010,776✔
3445
}
3446
static FORCE_INLINE int32_t tColDataAppendValue71(SColData *pColData, uint8_t *pData, uint32_t nData) {
283,046,264✔
3447
  int32_t code = 0;
283,046,264✔
3448

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

3454
  return tColDataPutValue(pColData, NULL, 0);
283,046,264✔
3455
}
3456
static FORCE_INLINE int32_t tColDataAppendValue72(SColData *pColData, uint8_t *pData, uint32_t nData) {
341,070✔
3457
  int32_t code = 0;
341,070✔
3458

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

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

3476
    //       VALUE                  NONE                     NULL
3477
};
3478

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

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

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

3506
_exit:
×
3507
  return code;
×
3508
}
3509

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3842
static FORCE_INLINE int32_t tColDataUpdateValue10(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,836✔
3843
  pColData->numOfNone--;
2,836✔
3844
  pColData->nVal--;
2,836✔
3845
  if (pColData->numOfNone) {
2,836✔
3846
    return tColDataAppendValue10(pColData, pData, nData);
×
3847
  } else {
3848
    pColData->flag = 0;
2,836✔
3849
    return tColDataAppendValue00(pColData, pData, nData);
2,836✔
3850
  }
3851
}
3852
static FORCE_INLINE int32_t tColDataUpdateValue12(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
23,334✔
3853
  pColData->numOfNone--;
23,334✔
3854
  pColData->nVal--;
23,334✔
3855
  if (pColData->numOfNone) {
23,334✔
3856
    return tColDataAppendValue12(pColData, pData, nData);
20,304✔
3857
  } else {
3858
    pColData->flag = 0;
3,030✔
3859
    return tColDataAppendValue02(pColData, pData, nData);
3,030✔
3860
  }
3861
  return 0;
3862
}
3863
static FORCE_INLINE int32_t tColDataUpdateValue20(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
83,854✔
3864
  if (forward) {
83,854✔
3865
    pColData->numOfNull--;
83,854✔
3866
    pColData->nVal--;
83,854✔
3867
    if (pColData->numOfNull) {
83,854✔
3868
      return tColDataAppendValue20(pColData, pData, nData);
381✔
3869
    } else {
3870
      pColData->flag = 0;
83,473✔
3871
      return tColDataAppendValue00(pColData, pData, nData);
83,473✔
3872
    }
3873
  }
3874
  return 0;
×
3875
}
3876
static FORCE_INLINE int32_t tColDataUpdateValue30(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
×
3877
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
×
3878
    pColData->numOfNone--;
×
3879
    pColData->nVal--;
×
3880
    if (pColData->numOfNone) {
×
3881
      return tColDataAppendValue30(pColData, pData, nData);
×
3882
    } else {
3883
      pColData->flag = HAS_NULL;
×
3884
      return tColDataAppendValue20(pColData, pData, nData);
×
3885
    }
3886
  } else if (forward) {  // NULL ==> VALUE
×
3887
    pColData->numOfNull--;
×
3888
    pColData->nVal--;
×
3889
    if (pColData->numOfNull) {
×
3890
      return tColDataAppendValue30(pColData, pData, nData);
×
3891
    } else {
3892
      pColData->flag = HAS_NONE;
×
3893
      return tColDataAppendValue10(pColData, pData, nData);
×
3894
    }
3895
  }
3896
  return 0;
×
3897
}
3898
static FORCE_INLINE int32_t tColDataUpdateValue32(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
294,356✔
3899
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
294,356✔
3900
    pColData->numOfNone--;
294,356✔
3901
    pColData->numOfNull++;
294,356✔
3902
    if (pColData->numOfNone) {
294,356✔
3903
      SET_BIT1(pColData->pBitMap, pColData->nVal - 1, 1);
284,256✔
3904
    } else {
3905
      pColData->flag = HAS_NULL;
10,100✔
3906
    }
3907
  }
3908
  return 0;
294,356✔
3909
}
3910
static FORCE_INLINE int32_t tColDataUpdateValue40(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,147,483,647✔
3911
  if (forward) {  // VALUE ==> VALUE
2,147,483,647✔
3912
    pColData->nVal--;
2,147,483,647✔
3913
    if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
3914
      pColData->nData = pColData->aOffset[pColData->nVal];
1,165,961,155✔
3915
    } else {
3916
      pColData->nData -= TYPE_BYTES[pColData->type];
2,147,483,647✔
3917
    }
3918
    return tColDataPutValue(pColData, pData, nData);
2,147,483,647✔
3919
  }
3920
  return 0;
×
3921
}
3922
static FORCE_INLINE int32_t tColDataUpdateValue42(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
558,854✔
3923
  if (forward) {  // VALUE ==> NULL
558,854✔
3924
    pColData->numOfValue--;
558,854✔
3925
    pColData->nVal--;
558,854✔
3926
    if (pColData->numOfValue) {
558,854✔
3927
      if (IS_VAR_DATA_TYPE(pColData->type)) {
541,649✔
3928
        pColData->nData = pColData->aOffset[pColData->nVal];
157,920✔
3929
      } else {
3930
        pColData->nData -= TYPE_BYTES[pColData->type];
383,729✔
3931
      }
3932
      return tColDataAppendValue42(pColData, pData, nData);
541,649✔
3933
    } else {
3934
      pColData->flag = 0;
17,205✔
3935
      pColData->nData = 0;
17,205✔
3936
      return tColDataAppendValue02(pColData, pData, nData);
17,205✔
3937
    }
3938
  }
3939
  return 0;
×
3940
}
3941
static FORCE_INLINE int32_t tColDataUpdateValue50(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,817,863✔
3942
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> VALUE
1,817,863✔
3943
    pColData->numOfNone--;
112,800✔
3944
    pColData->nVal--;
112,800✔
3945
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
112,800✔
3946
      pColData->nData -= TYPE_BYTES[pColData->type];
112,800✔
3947
    }
3948
    if (pColData->numOfNone) {
112,800✔
3949
      return tColDataAppendValue50(pColData, pData, nData);
×
3950
    } else {
3951
      pColData->flag = HAS_VALUE;
112,800✔
3952
      return tColDataAppendValue40(pColData, pData, nData);
112,800✔
3953
    }
3954
  } else if (forward) {  // VALUE ==> VALUE
1,705,063✔
3955
    pColData->nVal--;
1,705,063✔
3956
    if (IS_VAR_DATA_TYPE(pColData->type)) {
1,705,063✔
3957
      pColData->nData = pColData->aOffset[pColData->nVal];
243,468✔
3958
    } else {
3959
      pColData->nData -= TYPE_BYTES[pColData->type];
1,461,595✔
3960
    }
3961
    return tColDataPutValue(pColData, pData, nData);
1,705,063✔
3962
  }
3963
  return 0;
×
3964
}
3965
static FORCE_INLINE int32_t tColDataUpdateValue52(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
20,809✔
3966
  if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NONE ==> NULL
20,809✔
3967
    pColData->numOfNone--;
20,809✔
3968
    pColData->nVal--;
20,809✔
3969
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
20,809✔
3970
      pColData->nData -= TYPE_BYTES[pColData->type];
14,041✔
3971
    }
3972
    if (pColData->numOfNone) {
20,809✔
3973
      return tColDataAppendValue52(pColData, pData, nData);
×
3974
    } else {
3975
      pColData->flag = HAS_VALUE;
20,809✔
3976
      return tColDataAppendValue42(pColData, pData, nData);
20,809✔
3977
    }
3978
  } else if (forward) {  // VALUE ==> NULL
×
3979
    pColData->numOfValue--;
×
3980
    pColData->nVal--;
×
3981
    if (pColData->numOfValue) {
×
3982
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
3983
        pColData->nData = pColData->aOffset[pColData->nVal];
×
3984
      } else {
3985
        pColData->nData -= TYPE_BYTES[pColData->type];
×
3986
      }
3987
      return tColDataAppendValue52(pColData, pData, nData);
×
3988
    } else {
3989
      pColData->flag = HAS_NONE;
×
3990
      pColData->nData = 0;
×
3991
      return tColDataAppendValue12(pColData, pData, nData);
×
3992
    }
3993
  }
3994
  return 0;
×
3995
}
3996
static FORCE_INLINE int32_t tColDataUpdateValue60(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
3,124,754✔
3997
  if (forward) {
3,124,754✔
3998
    if (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 0) {  // NULL ==> VALUE
3,124,754✔
3999
      pColData->numOfNull--;
2,040,157✔
4000
      pColData->nVal--;
2,040,157✔
4001
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
2,040,157✔
4002
        pColData->nData -= TYPE_BYTES[pColData->type];
1,687,864✔
4003
      }
4004
      if (pColData->numOfNull) {
2,040,157✔
4005
        return tColDataAppendValue60(pColData, pData, nData);
2,331✔
4006
      } else {
4007
        pColData->flag = HAS_VALUE;
2,037,258✔
4008
        return tColDataAppendValue40(pColData, pData, nData);
2,037,826✔
4009
      }
4010
    } else {  // VALUE ==> VALUE
4011
      pColData->nVal--;
1,084,033✔
4012
      if (IS_VAR_DATA_TYPE(pColData->type)) {
1,084,597✔
4013
        pColData->nData = pColData->aOffset[pColData->nVal];
108,272✔
4014
      } else {
4015
        pColData->nData -= TYPE_BYTES[pColData->type];
976,325✔
4016
      }
4017
      return tColDataPutValue(pColData, pData, nData);
1,084,597✔
4018
    }
4019
  }
4020
  return 0;
×
4021
}
4022
static FORCE_INLINE int32_t tColDataUpdateValue62(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
1,408,874✔
4023
  if (forward && (GET_BIT1(pColData->pBitMap, pColData->nVal - 1) == 1)) {  // VALUE ==> NULL
1,408,874✔
4024
    pColData->numOfValue--;
849,405✔
4025
    pColData->nVal--;
849,405✔
4026
    if (pColData->numOfValue) {
849,405✔
4027
      if (IS_VAR_DATA_TYPE(pColData->type)) {
849,405✔
4028
        pColData->nData = pColData->aOffset[pColData->nVal];
90,903✔
4029
      } else {
4030
        pColData->nData -= TYPE_BYTES[pColData->type];
758,502✔
4031
      }
4032
      return tColDataAppendValue62(pColData, pData, nData);
849,405✔
4033
    } else {
4034
      pColData->flag = HAS_NULL;
×
4035
      pColData->nData = 0;
×
4036
      return tColDataAppendValue20(pColData, pData, nData);
×
4037
    }
4038
  }
4039
  return 0;
559,469✔
4040
}
4041
static FORCE_INLINE int32_t tColDataUpdateValue70(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
286,130✔
4042
  int32_t code = 0;
286,130✔
4043

4044
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
286,130✔
4045
  if (bv == 0) {  // NONE ==> VALUE
286,130✔
4046
    pColData->numOfNone--;
67,680✔
4047
    pColData->nVal--;
67,680✔
4048
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
67,680✔
4049
      pColData->nData -= TYPE_BYTES[pColData->type];
67,680✔
4050
    }
4051
    if (pColData->numOfNone) {
67,680✔
4052
      return tColDataAppendValue70(pColData, pData, nData);
67,680✔
4053
    } else {
4054
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4055
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
×
4056
      }
4057
      pColData->flag = (HAS_VALUE | HAS_NULL);
×
4058
      return tColDataAppendValue60(pColData, pData, nData);
×
4059
    }
4060
  } else if (bv == 1) {  // NULL ==> VALUE
218,450✔
4061
    if (forward) {
36,674✔
4062
      pColData->numOfNull--;
36,674✔
4063
      pColData->nVal--;
36,674✔
4064
      if (!IS_VAR_DATA_TYPE(pColData->type)) {
36,674✔
4065
        pColData->nData -= TYPE_BYTES[pColData->type];
36,674✔
4066
      }
4067
      if (pColData->numOfNull) {
36,674✔
4068
        return tColDataAppendValue70(pColData, pData, nData);
36,096✔
4069
      } else {
4070
        for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
1,734✔
4071
          SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) ? 1 : 0);
1,156✔
4072
        }
4073
        pColData->flag = (HAS_VALUE | HAS_NONE);
578✔
4074
        return tColDataAppendValue50(pColData, pData, nData);
578✔
4075
      }
4076
    }
4077
  } else if (bv == 2) {  // VALUE ==> VALUE
181,776✔
4078
    if (forward) {
181,776✔
4079
      pColData->nVal--;
181,776✔
4080
      if (IS_VAR_DATA_TYPE(pColData->type)) {
181,776✔
4081
        pColData->nData = pColData->aOffset[pColData->nVal];
45,120✔
4082
      } else {
4083
        pColData->nData -= TYPE_BYTES[pColData->type];
136,656✔
4084
      }
4085
      return tColDataPutValue(pColData, pData, nData);
181,776✔
4086
    }
4087
  } else {
4088
    return TSDB_CODE_INVALID_PARA;
×
4089
  }
4090
  return 0;
×
4091
}
4092
static int32_t tColDataUpdateValue72(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
335,076✔
4093
  uint8_t bv = GET_BIT2(pColData->pBitMap, pColData->nVal - 1);
335,076✔
4094
  if (bv == 0) {  // NONE ==> NULL
335,076✔
4095
    pColData->numOfNone--;
335,076✔
4096
    pColData->nVal--;
335,076✔
4097
    if (!IS_VAR_DATA_TYPE(pColData->type)) {
335,076✔
4098
      pColData->nData -= TYPE_BYTES[pColData->type];
208,179✔
4099
    }
4100
    if (pColData->numOfNone) {
335,076✔
4101
      return tColDataAppendValue72(pColData, pData, nData);
×
4102
    } else {
4103
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
10,533,416✔
4104
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal) - 1);
10,198,340✔
4105
      }
4106
      pColData->flag = (HAS_VALUE | HAS_NULL);
335,076✔
4107
      return tColDataAppendValue62(pColData, pData, nData);
335,076✔
4108
    }
4109
  } else if (bv == 2 && forward) {  // VALUE ==> NULL
×
4110
    pColData->numOfValue--;
×
4111
    pColData->nVal--;
×
4112
    if (pColData->numOfValue) {
×
4113
      if (IS_VAR_DATA_TYPE(pColData->type)) {
×
4114
        pColData->nData = pColData->aOffset[pColData->nVal];
×
4115
      } else {
4116
        pColData->nData -= TYPE_BYTES[pColData->type];
×
4117
      }
4118
      return tColDataAppendValue72(pColData, pData, nData);
×
4119
    } else {
4120
      for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
×
4121
        SET_BIT1(pColData->pBitMap, iVal, GET_BIT2(pColData->pBitMap, iVal));
×
4122
      }
4123
      pColData->flag = (HAS_NULL | HAS_NONE);
×
4124
      pColData->nData = 0;
×
4125
      return tColDataAppendValue32(pColData, pData, nData);
×
4126
    }
4127
  }
4128
  return 0;
×
4129
}
4130
static FORCE_INLINE int32_t tColDataUpdateNothing(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) {
2,592,884✔
4131
  return 0;
2,592,884✔
4132
}
4133
static int32_t (*tColDataUpdateValueImpl[8][3])(SColData *pColData, uint8_t *pData, uint32_t nData, bool forward) = {
4134
    {NULL, NULL, NULL},                                                     // 0
4135
    {tColDataUpdateValue10, tColDataUpdateNothing, tColDataUpdateValue12},  // HAS_NONE
4136
    {tColDataUpdateValue20, tColDataUpdateNothing, tColDataUpdateNothing},  // HAS_NULL
4137
    {tColDataUpdateValue30, tColDataUpdateNothing, tColDataUpdateValue32},  // HAS_NULL|HAS_NONE
4138
    {tColDataUpdateValue40, tColDataUpdateNothing, tColDataUpdateValue42},  // HAS_VALUE
4139
    {tColDataUpdateValue50, tColDataUpdateNothing, tColDataUpdateValue52},  // HAS_VALUE|HAS_NONE
4140
    {tColDataUpdateValue60, tColDataUpdateNothing, tColDataUpdateValue62},  // HAS_VALUE|HAS_NULL
4141
    {tColDataUpdateValue70, tColDataUpdateNothing, tColDataUpdateValue72},  // HAS_VALUE|HAS_NULL|HAS_NONE
4142

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

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

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

4155
static FORCE_INLINE void tColDataGetValue1(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NONE
434,161,811✔
4156
  *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
434,161,811✔
4157
}
434,205,861✔
4158
static FORCE_INLINE void tColDataGetValue2(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_NULL
1,454,016,369✔
4159
  *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
1,454,016,369✔
4160
}
1,454,025,731✔
4161
static FORCE_INLINE void tColDataGetValue3(SColData *pColData, int32_t iVal,
5,415,126✔
4162
                                           SColVal *pColVal) {  // HAS_NULL|HAS_NONE
4163
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
5,415,126✔
4164
    case 0:
2,644,113✔
4165
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
2,644,113✔
4166
      break;
2,642,985✔
4167
    case 1:
2,772,141✔
4168
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
2,772,141✔
4169
      break;
2,773,833✔
4170
    default:
×
4171
      break;
×
4172
  }
4173
}
5,416,818✔
4174
static FORCE_INLINE void tColDataGetValue4(SColData *pColData, int32_t iVal, SColVal *pColVal) {  // HAS_VALUE
2,147,483,647✔
4175
  SValue value = {.type = pColData->type};
2,147,483,647✔
4176
  if (IS_VAR_DATA_TYPE(pColData->type)) {
2,147,483,647✔
4177
    if (iVal + 1 < pColData->nVal) {
2,147,483,647✔
4178
      value.nData = pColData->aOffset[iVal + 1] - pColData->aOffset[iVal];
2,147,483,647✔
4179
    } else {
4180
      value.nData = pColData->nData - pColData->aOffset[iVal];
116,532,299✔
4181
    }
4182
    value.pData = pColData->pData + pColData->aOffset[iVal];
2,147,483,647✔
4183
  } else {
4184
    valueSetDatum(&value, pColData->type, pColData->pData + tDataTypes[pColData->type].bytes * iVal,
2,147,483,647✔
4185
                  tDataTypes[pColData->type].bytes);
2,147,483,647✔
4186
  }
4187
  *pColVal = COL_VAL_VALUE(pColData->cid, value);
2,147,483,647✔
4188
}
2,147,483,647✔
4189
static FORCE_INLINE void tColDataGetValue5(SColData *pColData, int32_t iVal,
106,005,325✔
4190
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NONE
4191
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
106,005,325✔
4192
    case 0:
30,713,903✔
4193
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
30,713,903✔
4194
      break;
30,716,424✔
4195
    case 1:
75,360,147✔
4196
      tColDataGetValue4(pColData, iVal, pColVal);
4197
      break;
75,352,297✔
4198
    default:
×
4199
      break;
×
4200
  }
4201
}
106,068,721✔
4202
static FORCE_INLINE void tColDataGetValue6(SColData *pColData, int32_t iVal,
2,147,483,647✔
4203
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL
4204
  switch (GET_BIT1(pColData->pBitMap, iVal)) {
2,147,483,647✔
4205
    case 0:
2,022,617,654✔
4206
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
2,022,617,654✔
4207
      break;
2,022,621,928✔
4208
    case 1:
2,147,483,647✔
4209
      tColDataGetValue4(pColData, iVal, pColVal);
4210
      break;
2,147,483,647✔
4211
    default:
×
4212
      break;
×
4213
  }
4214
}
2,147,483,647✔
4215
static FORCE_INLINE void tColDataGetValue7(SColData *pColData, int32_t iVal,
19,262,913✔
4216
                                           SColVal *pColVal) {  // HAS_VALUE|HAS_NULL|HAS_NONE
4217
  switch (GET_BIT2(pColData->pBitMap, iVal)) {
19,262,913✔
4218
    case 0:
3,460,827✔
4219
      *pColVal = COL_VAL_NONE(pColData->cid, pColData->type);
3,460,827✔
4220
      break;
3,460,827✔
4221
    case 1:
4,063,041✔
4222
      *pColVal = COL_VAL_NULL(pColData->cid, pColData->type);
4,063,041✔
4223
      break;
4,065,861✔
4224
    case 2:
11,747,505✔
4225
      tColDataGetValue4(pColData, iVal, pColVal);
4226
      break;
11,752,581✔
4227
    default:
×
4228
      break;
×
4229
  }
4230
}
19,279,269✔
4231
static void (*tColDataGetValueImpl[])(SColData *pColData, int32_t iVal, SColVal *pColVal) = {
4232
    NULL,               // 0
4233
    tColDataGetValue1,  // HAS_NONE
4234
    tColDataGetValue2,  // HAS_NULL
4235
    tColDataGetValue3,  // HAS_NULL | HAS_NONE
4236
    tColDataGetValue4,  // HAS_VALUE
4237
    tColDataGetValue5,  // HAS_VALUE | HAS_NONE
4238
    tColDataGetValue6,  // HAS_VALUE | HAS_NULL
4239
    tColDataGetValue7   // HAS_VALUE | HAS_NULL | HAS_NONE
4240
};
4241
int32_t tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal) {
2,147,483,647✔
4242
  if (iVal < 0 || iVal >= pColData->nVal ||
2,147,483,647✔
4243
      (pColData->flag <= 0 || pColData->flag >= sizeof(tColDataGetValueImpl) / POINTER_BYTES)) {
2,147,483,647✔
4244
    return TSDB_CODE_INVALID_PARA;
×
4245
  }
4246
  tColDataGetValueImpl[pColData->flag](pColData, iVal, pColVal);
2,147,483,647✔
4247
  return TSDB_CODE_SUCCESS;
2,147,483,647✔
4248
}
4249

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

4271
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg) {
13,422,024✔
4272
  int32_t code = 0;
13,422,024✔
4273

4274
  *pColData = *pColDataFrom;
13,422,024✔
4275

4276
  // bitmap
4277
  switch (pColData->flag) {
13,422,861✔
4278
    case (HAS_NULL | HAS_NONE):
39,419✔
4279
    case (HAS_VALUE | HAS_NONE):
4280
    case (HAS_VALUE | HAS_NULL):
4281
      pColData->pBitMap = xMalloc(arg, BIT1_SIZE(pColData->nVal));
39,419✔
4282
      if (pColData->pBitMap == NULL) {
39,419✔
4283
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4284
        goto _exit;
×
4285
      }
4286
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT1_SIZE(pColData->nVal));
39,419✔
4287
      break;
39,419✔
4288
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
4289
      pColData->pBitMap = xMalloc(arg, BIT2_SIZE(pColData->nVal));
×
4290
      if (pColData->pBitMap == NULL) {
×
4291
        code = TSDB_CODE_OUT_OF_MEMORY;
×
4292
        goto _exit;
×
4293
      }
4294
      (void)memcpy(pColData->pBitMap, pColDataFrom->pBitMap, BIT2_SIZE(pColData->nVal));
×
4295
      break;
×
4296
    default:
13,382,814✔
4297
      pColData->pBitMap = NULL;
13,382,814✔
4298
      break;
13,383,651✔
4299
  }
4300

4301
  // offset
4302
  if (IS_VAR_DATA_TYPE(pColData->type) && (pColData->flag & HAS_VALUE)) {
13,423,070✔
4303
    pColData->aOffset = xMalloc(arg, pColData->nVal << 2);
55,129✔
4304
    if (pColData->aOffset == NULL) {
55,129✔
4305
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4306
      goto _exit;
×
4307
    }
4308
    (void)memcpy(pColData->aOffset, pColDataFrom->aOffset, pColData->nVal << 2);
55,129✔
4309
  } else {
4310
    pColData->aOffset = NULL;
13,367,407✔
4311
  }
4312

4313
  // value
4314
  if (pColData->nData) {
13,423,373✔
4315
    pColData->pData = xMalloc(arg, pColData->nData);
13,388,793✔
4316
    if (pColData->pData == NULL) {
13,389,421✔
4317
      code = TSDB_CODE_OUT_OF_MEMORY;
×
4318
      goto _exit;
×
4319
    }
4320

4321
    (void)memcpy(pColData->pData, pColDataFrom->pData, pColData->nData);
13,389,118✔
4322
  } else {
4323
    pColData->pData = NULL;
33,429✔
4324
  }
4325

4326
_exit:
13,422,338✔
4327
  return code;
13,422,338✔
4328
}
4329

4330
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist) {
407,883,466✔
4331
  int32_t code;
4332
  SBuffer local;
407,870,823✔
4333

4334
  if (!(colData->nVal > 0)) {
407,928,383✔
4335
    return TSDB_CODE_INVALID_PARA;
×
4336
  }
4337

4338
  (*info) = (SColDataCompressInfo){
407,939,186✔
4339
      .cmprAlg = info->cmprAlg,
407,937,907✔
4340
      .columnFlag = colData->cflag,
407,929,680✔
4341
      .flag = colData->flag,
407,949,606✔
4342
      .dataType = colData->type,
407,949,075✔
4343
      .columnId = colData->cid,
407,949,185✔
4344
      .numOfData = colData->nVal,
407,901,915✔
4345
  };
4346

4347
  if (colData->flag == HAS_NONE || colData->flag == HAS_NULL) {
407,947,421✔
4348
    return 0;
16,746,975✔
4349
  }
4350

4351
  tBufferInit(&local);
4352
  if (assist == NULL) {
391,184,991✔
4353
    assist = &local;
×
4354
  }
4355

4356
  // bitmap
4357
  if (colData->flag != HAS_VALUE) {
391,184,991✔
4358
    if (colData->flag == (HAS_NONE | HAS_NULL | HAS_VALUE)) {
27,313,349✔
4359
      info->bitmapOriginalSize = BIT2_SIZE(colData->nVal);
1,553,084✔
4360
    } else {
4361
      info->bitmapOriginalSize = BIT1_SIZE(colData->nVal);
25,759,194✔
4362
    }
4363

4364
    SCompressInfo cinfo = {
27,310,902✔
4365
        .dataType = TSDB_DATA_TYPE_TINYINT,
4366
        .cmprAlg = info->cmprAlg,
27,311,973✔
4367
        .originalSize = info->bitmapOriginalSize,
27,313,331✔
4368
    };
4369

4370
    code = tCompressDataToBuffer(colData->pBitMap, &cinfo, output, assist);
27,312,189✔
4371
    if (code) {
27,311,185✔
4372
      tBufferDestroy(&local);
4373
      return code;
×
4374
    }
4375

4376
    info->bitmapCompressedSize = cinfo.compressedSize;
27,311,185✔
4377
  }
4378

4379
  if (colData->flag == (HAS_NONE | HAS_NULL)) {
391,187,675✔
4380
    tBufferDestroy(&local);
4381
    return 0;
88,339✔
4382
  }
4383

4384
  // offset
4385
  if (IS_VAR_DATA_TYPE(colData->type)) {
391,027,360✔
4386
    info->offsetOriginalSize = sizeof(int32_t) * info->numOfData;
52,797,341✔
4387

4388
    SCompressInfo cinfo = {
52,805,838✔
4389
        .dataType = TSDB_DATA_TYPE_INT,
4390
        .cmprAlg = info->cmprAlg,
52,808,879✔
4391
        .originalSize = info->offsetOriginalSize,
52,806,993✔
4392
    };
4393

4394
    code = tCompressDataToBuffer(colData->aOffset, &cinfo, output, assist);
52,807,099✔
4395
    if (code) {
52,800,267✔
4396
      tBufferDestroy(&local);
4397
      return code;
×
4398
    }
4399

4400
    info->offsetCompressedSize = cinfo.compressedSize;
52,800,267✔
4401
  }
4402

4403
  // data
4404
  if (colData->nData > 0) {
391,098,206✔
4405
    info->dataOriginalSize = colData->nData;
390,988,560✔
4406

4407
    SCompressInfo cinfo = {
391,000,581✔
4408
        .dataType = colData->type,
390,999,630✔
4409
        .cmprAlg = info->cmprAlg,
390,984,994✔
4410
        .originalSize = info->dataOriginalSize,
390,987,072✔
4411
    };
4412

4413
    code = tCompressDataToBuffer(colData->pData, &cinfo, output, assist);
390,995,758✔
4414
    if (code) {
390,890,862✔
4415
      tBufferDestroy(&local);
4416
      return code;
×
4417
    }
4418

4419
    info->dataCompressedSize = cinfo.compressedSize;
390,890,862✔
4420
  }
4421

4422
  tBufferDestroy(&local);
4423
  return 0;
391,066,600✔
4424
}
4425

4426
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist) {
528,120,812✔
4427
  int32_t  code;
4428
  SBuffer  local;
528,116,582✔
4429
  uint8_t *data = (uint8_t *)input;
528,221,569✔
4430

4431
  tBufferInit(&local);
4432
  if (assist == NULL) {
528,247,551✔
4433
    assist = &local;
×
4434
  }
4435

4436
  tColDataClear(colData);
528,247,551✔
4437
  colData->cid = info->columnId;
528,271,168✔
4438
  colData->type = info->dataType;
528,270,488✔
4439
  colData->cflag = info->columnFlag;
528,270,818✔
4440
  colData->nVal = info->numOfData;
528,249,356✔
4441
  colData->flag = info->flag;
528,282,729✔
4442

4443
  if (info->flag == HAS_NONE || info->flag == HAS_NULL) {
528,149,364✔
4444
    goto _exit;
36,892,859✔
4445
  }
4446

4447
  // bitmap
4448
  if (info->bitmapOriginalSize > 0) {
491,377,382✔
4449
    SCompressInfo cinfo = {
68,725,831✔
4450
        .dataType = TSDB_DATA_TYPE_TINYINT,
4451
        .cmprAlg = info->cmprAlg,
68,730,218✔
4452
        .originalSize = info->bitmapOriginalSize,
68,731,179✔
4453
        .compressedSize = info->bitmapCompressedSize,
68,724,423✔
4454
    };
4455

4456
    code = tRealloc(&colData->pBitMap, cinfo.originalSize);
68,720,612✔
4457
    if (code) {
68,731,068✔
4458
      tBufferDestroy(&local);
4459
      return code;
×
4460
    }
4461

4462
    code = tDecompressData(data, &cinfo, colData->pBitMap, cinfo.originalSize, assist);
68,731,068✔
4463
    if (code) {
68,719,969✔
4464
      tBufferDestroy(&local);
4465
      return code;
×
4466
    }
4467

4468
    data += cinfo.compressedSize;
68,719,969✔
4469
  }
4470

4471
  if (info->flag == (HAS_NONE | HAS_NULL)) {
491,397,088✔
4472
    goto _exit;
168,636✔
4473
  }
4474

4475
  // offset
4476
  if (info->offsetOriginalSize > 0) {
491,207,812✔
4477
    SCompressInfo cinfo = {
104,802,728✔
4478
        .cmprAlg = info->cmprAlg,
104,800,598✔
4479
        .dataType = TSDB_DATA_TYPE_INT,
4480
        .originalSize = info->offsetOriginalSize,
104,798,678✔
4481
        .compressedSize = info->offsetCompressedSize,
104,791,411✔
4482
    };
4483

4484
    code = tRealloc((uint8_t **)&colData->aOffset, cinfo.originalSize);
104,790,194✔
4485
    if (code) {
104,774,115✔
4486
      tBufferDestroy(&local);
4487
      return code;
×
4488
    }
4489

4490
    code = tDecompressData(data, &cinfo, colData->aOffset, cinfo.originalSize, assist);
104,774,115✔
4491
    if (code) {
104,764,138✔
4492
      tBufferDestroy(&local);
4493
      return code;
×
4494
    }
4495

4496
    data += cinfo.compressedSize;
104,764,138✔
4497
  }
4498

4499
  // data
4500
  if (info->dataOriginalSize > 0) {
491,164,429✔
4501
    colData->nData = info->dataOriginalSize;
490,891,149✔
4502

4503
    SCompressInfo cinfo = {
490,834,208✔
4504
        .cmprAlg = info->cmprAlg,
981,817,654✔
4505
        .dataType = colData->type,
490,817,321✔
4506
        .originalSize = info->dataOriginalSize,
490,905,133✔
4507
        .compressedSize = info->dataCompressedSize,
490,885,650✔
4508
    };
4509

4510
    code = tRealloc((uint8_t **)&colData->pData, cinfo.originalSize);
490,900,019✔
4511
    if (code) {
490,865,038✔
4512
      tBufferDestroy(&local);
4513
      return code;
×
4514
    }
4515

4516
    code = tDecompressData(data, &cinfo, colData->pData, cinfo.originalSize, assist);
490,865,038✔
4517
    if (code) {
490,885,382✔
4518
      tBufferDestroy(&local);
4519
      return code;
×
4520
    }
4521

4522
    data += cinfo.compressedSize;
490,885,382✔
4523
  }
4524

4525
_exit:
528,291,327✔
4526
  switch (colData->flag) {
528,277,019✔
4527
    case HAS_NONE:
3,624,598✔
4528
      colData->numOfNone = colData->nVal;
3,624,598✔
4529
      break;
3,624,598✔
4530
    case HAS_NULL:
33,237,996✔
4531
      colData->numOfNull = colData->nVal;
33,237,996✔
4532
      break;
33,256,442✔
4533
    case HAS_VALUE:
422,610,289✔
4534
      colData->numOfValue = colData->nVal;
422,610,289✔
4535
      break;
422,635,899✔
4536
    default:
68,729,363✔
4537
      for (int32_t i = 0; i < colData->nVal; i++) {
2,147,483,647✔
4538
        uint8_t bitValue = tColDataGetBitValue(colData, i);
2,147,483,647✔
4539
        if (bitValue == 0) {
2,147,483,647✔
4540
          colData->numOfNone++;
46,972,564✔
4541
        } else if (bitValue == 1) {
2,147,483,647✔
4542
          colData->numOfNull++;
2,147,483,647✔
4543
        } else {
4544
          colData->numOfValue++;
2,147,483,647✔
4545
        }
4546
      }
4547
  }
4548
  tBufferDestroy(&local);
4549
  return 0;
528,157,779✔
4550
}
4551

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

4566
  if (IS_VAR_DATA_TYPE(type)) {  // var-length data type
150,972✔
4567
    if (!IS_STR_DATA_BLOB(type)) {
25,422✔
4568
      for (int32_t i = 0; i < nRows; ++i) {
73,165✔
4569
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
47,743✔
4570
        if (offset == -1) {
47,743✔
4571
          if (pColData->cflag & COL_IS_KEY) {
×
4572
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4573
            goto _exit;
×
4574
          }
4575
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4576
            goto _exit;
×
4577
          }
4578
        } else {
4579
          if (varDataTLen(data + offset) > bytes || (type == TSDB_DATA_TYPE_NCHAR && varDataLen(data + offset) % TSDB_NCHAR_SIZE != 0)) {
47,743✔
4580
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d", (int)varDataTLen(data + offset),
×
4581
                   bytes);
4582
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4583
            goto _exit;
×
4584
          }
4585
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)varDataVal(data + offset),
95,486✔
4586
                                                                        varDataLen(data + offset));
47,743✔
4587
        }
4588
      }
4589
    } else {
4590
      for (int32_t i = 0; i < nRows; ++i) {
×
4591
        int32_t offset = *((int32_t *)lengthOrbitmap + i);
×
4592
        if (offset == -1) {
×
4593
          if (pColData->cflag & COL_IS_KEY) {
×
4594
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4595
            goto _exit;
×
4596
          }
4597
          if ((code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0))) {
×
4598
            goto _exit;
×
4599
          }
4600
        } else {
4601
          if (blobDataTLen(data + offset) > TSDB_MAX_BLOB_LEN) {
×
4602
            uError("var data length invalid, varDataTLen(data + offset):%d > bytes:%d",
×
4603
                   (int)blobDataTLen(data + offset), bytes);
4604
            code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4605
            goto _exit;
×
4606
          }
4607
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)blobDataVal(data + offset),
×
4608
                                                                        blobDataLen(data + offset));
×
4609
        }
4610
      }
4611
    }
4612
  } else {  // fixed-length data type
4613
    bool allValue = true;
125,550✔
4614
    bool allNull = true;
125,550✔
4615
    for (int32_t i = 0; i < nRows; ++i) {
358,838✔
4616
      if (!BMIsNull(lengthOrbitmap, i)) {
233,288✔
4617
        allNull = false;
165,593✔
4618
      } else {
4619
        allValue = false;
67,695✔
4620
      }
4621
    }
4622
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
125,550✔
4623
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
4624
      goto _exit;
×
4625
    }
4626

4627
    if (allValue) {
125,550✔
4628
      // optimize (todo)
4629
      for (int32_t i = 0; i < nRows; ++i) {
256,894✔
4630
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
161,743✔
4631
      }
4632
    } else if (allNull) {
30,399✔
4633
      // optimize (todo)
4634
      for (int32_t i = 0; i < nRows; ++i) {
82,104✔
4635
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
54,459✔
4636
        if (code) goto _exit;
54,459✔
4637
      }
4638
    } else {
4639
      for (int32_t i = 0; i < nRows; ++i) {
19,840✔
4640
        if (BMIsNull(lengthOrbitmap, i)) {
17,086✔
4641
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
13,236✔
4642
          if (code) goto _exit;
13,236✔
4643
        } else {
4644
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, (uint8_t *)data + bytes * i, bytes);
3,850✔
4645
        }
4646
      }
4647
    }
4648
  }
4649

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

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

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

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

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

4759
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
632,417,738✔
4760
    if (!(pColData->type == pBind->buffer_type)) {
625,601,432✔
4761
      return TSDB_CODE_INVALID_PARA;
×
4762
    }
4763
  }
4764

4765
  if (IS_VAR_DATA_TYPE(pColData->type)) {  // var-length data type
646,448,614✔
4766
    if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
63,137,351✔
4767
      code = igeos();
1,080,736✔
4768
      if (code) {
1,079,764✔
4769
        return code;
×
4770
      }
4771
    }
4772
    for (int32_t i = 0; i < pBind->num; ++i) {
142,115,140✔
4773
      if (pBind->is_null && pBind->is_null[i]) {
72,125,266✔
4774
        if (pColData->cflag & COL_IS_KEY) {
3,944,178✔
4775
          code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
1,238✔
4776
          goto _exit;
1,238✔
4777
        }
4778
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,945,784✔
4779
        if (code) goto _exit;
3,945,165✔
4780
      } else if (pBind->length[i] > buffMaxLen) {
68,210,089✔
4781
        return TSDB_CODE_PAR_VALUE_TOO_LONG;
×
4782
      } else {
4783
        if (pColData->type == TSDB_DATA_TYPE_GEOMETRY) {
68,189,528✔
4784
          code = cgeos((char *)pBind->buffer + pBind->buffer_length * i, (size_t)pBind->length[i]);
1,081,096✔
4785
          if (code) {
1,079,908✔
4786
            uError("stmt col[%d] bind geometry wrong format", i);
×
4787
            goto _exit;
×
4788
          }
4789
        }
4790
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
136,303,619✔
4791
            pColData, (uint8_t *)pBind->buffer + pBind->buffer_length * i, pBind->length[i]);
68,200,293✔
4792
      }
4793
    }
4794
  } else {  // fixed-length data type
4795
    bool allValue;
4796
    bool allNull;
4797
    if (pBind->is_null) {
582,558,561✔
4798
      bool same = (memcmp(pBind->is_null, pBind->is_null + 1, pBind->num - 1) == 0);
29,690,418✔
4799
      allNull = (same && pBind->is_null[0] != 0);
29,687,455✔
4800
      allValue = (same && pBind->is_null[0] == 0);
29,675,609✔
4801
    } else {
4802
      allNull = false;
547,958,255✔
4803
      allValue = true;
547,958,255✔
4804
    }
4805

4806
    if ((pColData->cflag & COL_IS_KEY) && !allValue) {
577,633,066✔
4807
      code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
2,476✔
4808
      goto _exit;
2,476✔
4809
    }
4810

4811
    if (allValue) {
580,480,593✔
4812
      // optimize (todo)
4813
      for (int32_t i = 0; i < pBind->num; ++i) {
2,147,483,647✔
4814
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
2,147,483,647✔
4815
            pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
2,147,483,647✔
4816
      }
4817
    } else if (allNull) {
17,356,661✔
4818
      // optimize (todo)
4819
      for (int32_t i = 0; i < pBind->num; ++i) {
33,344,192✔
4820
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
16,669,008✔
4821
        if (code) goto _exit;
16,673,906✔
4822
      }
4823
    } else {
4824
      for (int32_t i = 0; i < pBind->num; ++i) {
8,389,427✔
4825
        if (pBind->is_null[i]) {
7,703,052✔
4826
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
3,405,505✔
4827
          if (code) goto _exit;
3,405,505✔
4828
        } else {
4829
          code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](
8,595,094✔
4830
              pColData, (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i, pBind->buffer_length);
4,297,547✔
4831
        }
4832
      }
4833
    }
4834
  }
4835

4836
_exit:
78,874,553✔
4837
  return code;
643,737,530✔
4838
}
4839

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

4843
  if (!(pBind->num == 1 && pBind->is_null && *pBind->is_null)) {
856✔
4844
    if (!(pColData->type == pBind->buffer_type)) {
856✔
4845
      return TSDB_CODE_INVALID_PARA;
×
4846
    }
4847
  }
4848

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

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

4891
    uint8_t *buf = pBind->buffer;
856✔
4892

4893
    if (allValue) {
856✔
4894
      // optimize (todo)
4895
      for (int32_t i = 0; i < pBind->num; ++i) {
8,552✔
4896
        uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
7,696✔
4897
        if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
7,696✔
4898
          *val = 1;
×
4899
        }
4900
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_VALUE](pColData, val, TYPE_BYTES[pColData->type]);
7,696✔
4901
      }
4902
    } else if (allNull) {
×
4903
      // optimize (todo)
4904
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4905
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4906
        if (code) goto _exit;
×
4907
      }
4908
    } else if (allNone) {
×
4909
      // optimize (todo)
4910
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4911
        code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4912
        if (code) goto _exit;
×
4913
      }
4914
    } else {
4915
      for (int32_t i = 0; i < pBind->num; ++i) {
×
4916
        if (pBind->is_null[i]) {
×
4917
          if (pBind->is_null[i] == 1) {
×
4918
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NULL](pColData, NULL, 0);
×
4919
            if (code) goto _exit;
×
4920
          } else {
4921
            code = tColDataAppendValueImpl[pColData->flag][CV_FLAG_NONE](pColData, NULL, 0);
×
4922
            if (code) goto _exit;
×
4923
          }
4924
        } else {
4925
          uint8_t *val = (uint8_t *)pBind->buffer + TYPE_BYTES[pColData->type] * i;
×
4926
          if (TSDB_DATA_TYPE_BOOL == pColData->type && *val > 1) {
×
4927
            *val = 1;
×
4928
          }
4929

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

4936
_exit:
×
4937
  return code;
856✔
4938
}
4939

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

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

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

4954
  code = igeos();
×
4955
  if (code) {
×
4956
    return code;
×
4957
  }
4958

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

4986
_exit:
×
4987
  return code;
×
4988
}
4989

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

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

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

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

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

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

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

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

5105
  int32_t code = 0;
1,036,568✔
5106
  int32_t numOfRows = -1;
1,036,568✔
5107
  SArray *colValArray, *bufArray;
5108
  SColVal colVal;
333,422✔
5109
  int32_t numOfFixedValue = 0;
1,036,475✔
5110
  int32_t lino = 0;
1,036,475✔
5111
  bool    hasDecimal128 = false;
1,036,525✔
5112

5113
  if ((colValArray = taosArrayInit(numOfInfos, sizeof(SColVal))) == NULL) {
1,036,525✔
5114
    return terrno;
×
5115
  }
5116
  if ((bufArray = taosArrayInit(numOfInfos, sizeof(uint8_t *))) == NULL) {
1,036,649✔
5117
    taosArrayDestroy(colValArray);
×
5118
    return terrno;
×
5119
  }
5120
  for (int i = 0; i < numOfInfos; ++i) {
9,599,460✔
5121
    if (parsedCols) {
8,563,035✔
5122
      SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[i].columnId, sizeof(int16_t));
×
5123
      if (pParsedVal) {
×
5124
        continue;
×
5125
      }
5126
    }
5127
    if (numOfRows == -1) {
8,563,035✔
5128
      numOfRows = infos[i].bind->num;
1,036,312✔
5129
    }
5130

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

5138
  SRowKey rowKey, lastRowKey;
333,345✔
5139
  for (int32_t iRow = 0; iRow < numOfRows; iRow++) {
11,149,033✔
5140
    taosArrayClear(colValArray);
10,114,699✔
5141

5142
    for (int32_t iInfo = 0; iInfo < numOfInfos; iInfo++) {
58,150,367✔
5143
      if (parsedCols) {
47,980,050✔
5144
        SColVal *pParsedVal = tSimpleHashGet(parsedCols, &infos[iInfo].columnId, sizeof(int16_t));
×
5145
        if (pParsedVal) {
×
5146
          numOfFixedValue++;
×
5147
          colVal = *pParsedVal;
×
5148

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

5157
      if (infos[iInfo].bind->is_null && infos[iInfo].bind->is_null[iRow]) {
47,980,169✔
5158
        if (infos[iInfo].bind->is_null[iRow] == 1) {
×
5159
          if (iInfo == 0) {
×
5160
            code = TSDB_CODE_PAR_PRIMARY_KEY_IS_NULL;
×
5161
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5162
          }
5163
          colVal = COL_VAL_NULL(infos[iInfo].columnId, infos[iInfo].type);
×
5164
        } else {
5165
          colVal = COL_VAL_NONE(infos[iInfo].columnId, infos[iInfo].type);
×
5166
        }
5167
      } else {
5168
        SValue value = {
47,984,593✔
5169
            .type = infos[iInfo].type,
47,982,931✔
5170
        };
5171
        if (IS_VAR_DATA_TYPE(infos[iInfo].type)) {
47,985,456✔
5172
          if (IS_STR_DATA_BLOB(infos[iInfo].type)) {
3,908,105✔
5173
            int32_t   length = infos[iInfo].bind->length[iRow];
1,950✔
5174
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5175
            value.nData = length;
×
5176
            if (value.nData > (TSDB_MAX_BLOB_LEN - BLOBSTR_HEADER_SIZE)) {
×
5177
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5178
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5179
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5180
              goto _exit;
×
5181
            }
5182
            value.pData = *data;
×
5183
            *data += length;
×
5184
          } else {
5185
            int32_t   length = infos[iInfo].bind->length[iRow];
3,917,320✔
5186
            uint8_t **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
3,918,324✔
5187
            value.nData = length;
3,918,822✔
5188
            if (value.nData > infos[iInfo].bytes - VARSTR_HEADER_SIZE) {
3,918,822✔
5189
              code = TSDB_CODE_PAR_VALUE_TOO_LONG;
×
5190
              uError("stmt2 bind col:%d, row:%d length:%d  greater than type maximum lenght: %d", iInfo, iRow,
×
5191
                     value.nData + (uint32_t)(BLOBSTR_HEADER_SIZE), infos[iInfo].bytes);
5192
              goto _exit;
×
5193
            }
5194
            value.pData = *data;
3,919,268✔
5195
            *data += length;
3,918,220✔
5196
          }
5197
          // value.pData = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bind->buffer_length * iRow;
5198
        } else {
5199
          if (infos[iInfo].type == TSDB_DATA_TYPE_DECIMAL) {
44,103,669✔
5200
            if (!pSchemaExt) {
×
5201
              uError("stmt2 decimal64 type without ext schema info, cannot parse decimal values");
×
5202
              code = TSDB_CODE_DECIMAL_PARSE_ERROR;
×
5203
              goto _exit;
×
5204
            }
5205
            uint8_t precision = 0, scale = 0;
×
5206
            decimalFromTypeMod(pSchemaExt[iInfo].typeMod, &precision, &scale);
×
5207
            Decimal128 dec = {0};
×
5208
            uint8_t  **data = &((uint8_t **)TARRAY_DATA(bufArray))[iInfo - numOfFixedValue];
×
5209
            int32_t    length = infos[iInfo].bind->length[iRow];
×
5210
            code = decimal128FromStr(*(char **)data, length, precision, scale, &dec);
×
5211
            *data += length;
×
5212
            hasDecimal128 = true;
×
5213
            TAOS_CHECK_GOTO(code, &lino, _exit);
×
5214

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

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

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

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

5239
          } else {
5240
            uint8_t *val = (uint8_t *)infos[iInfo].bind->buffer + infos[iInfo].bytes * iRow;
44,102,277✔
5241
            if (TSDB_DATA_TYPE_BOOL == value.type && *val > 1) {
44,090,562✔
5242
              *val = 1;
×
5243
            }
5244
            valueSetDatum(&value, infos[iInfo].type, val, infos[iInfo].bytes);
44,090,562✔
5245
          }
5246
        }
5247
        colVal = COL_VAL_VALUE(infos[iInfo].columnId, value);
48,027,839✔
5248
      }
5249
      if (taosArrayPush(colValArray, &colVal) == NULL) {
48,036,427✔
5250
        code = terrno;
×
5251
        goto _exit;
×
5252
      }
5253
    }
5254

5255
    SRow *row;
2,497,944✔
5256

5257
    if (hasBlob == 0) {
10,137,209✔
5258
      SRowBuildScanInfo sinfo = {0};
10,115,482✔
5259
      code = tRowBuild(colValArray, pTSchema, &row, &sinfo);
10,115,438✔
5260
      TAOS_CHECK_GOTO(code, &lino, _exit);
10,115,150✔
5261
    } else {
5262
      SRowBuildScanInfo sinfo = {.hasBlob = 1, .scanType = ROW_BUILD_UPDATE};
21,820✔
5263
      code = tRowBuildWithBlob(colValArray, pTSchema, &row, NULL, &sinfo);
21,820✔
5264
      TAOS_CHECK_GOTO(code, &lino, _exit);
×
5265
    }
5266

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

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

5284
    if (pOrdered && pDupTs) {
10,111,834✔
5285
      tRowGetKey(row, &rowKey);
20,224,529✔
5286
      if (iRow == 0) {
10,112,973✔
5287
        *pOrdered = true;
1,035,930✔
5288
        *pDupTs = false;
1,035,887✔
5289
      } else {
5290
        if (*pOrdered) {
9,077,043✔
5291
          int32_t res = tRowKeyCompare(&rowKey, &lastRowKey);
9,077,183✔
5292
          *pOrdered = (res >= 0);
9,077,183✔
5293
          if (!*pDupTs) {
9,076,848✔
5294
            *pDupTs = (res == 0);
9,077,041✔
5295
          }
5296
        }
5297
      }
5298
      lastRowKey = rowKey;
10,112,708✔
5299
    }
5300
  }
5301
_exit:
1,033,876✔
5302
  if (code != 0) {
1,034,420✔
5303
    if (hasDecimal128) {
×
5304
      int32_t num = taosArrayGetSize(colValArray);
×
5305
      for (int32_t i = 0; i < num; ++i) {
×
5306
        SColVal *pCol = taosArrayGet(colValArray, i);
×
5307
        if (pCol->value.type == TSDB_DATA_TYPE_DECIMAL) {
×
5308
          taosMemoryFreeClear(pCol->value.pData);
×
5309
        }
5310
      }
5311
    }
5312
    uError("tRowBuildFromBind2 failed at line %d, ErrCode=0x%x", lino, code);
×
5313
  }
5314
  taosArrayDestroy(colValArray);
1,034,420✔
5315
  taosArrayDestroy(bufArray);
1,036,686✔
5316
  return code;
1,036,853✔
5317
}
5318
/* build rows to `rowArray` from bind
5319
 * `infos` is the bind information array
5320
 * `numOfInfos` is the number of bind information
5321
 * `infoSorted` is whether the bind information is sorted by column id
5322
 * `pTSchema` is the schema of the table
5323
 * `rowArray` is the array to store the rows
5324
 * `pOrdered` is the pointer to store ordered
5325
 * `pDupTs` is the pointer to store duplicateTs
5326
 */
5327
int32_t tRowBuildFromBind2WithBlob(SBindInfo2 *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
×
5328
                                   SArray *rowArray, bool *pOrdered, bool *pDupTs, SBlobSet *pBlobSet) {
5329
  if (infos == NULL || numOfInfos <= 0 || numOfInfos > pTSchema->numOfCols || pTSchema == NULL || rowArray == NULL) {
×
5330
    return TSDB_CODE_INVALID_PARA;
×
5331
  }
5332
  int8_t hasBlob = schemaHasBlob(pTSchema);
×
5333
  if (!infoSorted) {
×
5334
    taosqsort_r(infos, numOfInfos, sizeof(SBindInfo2), NULL, tBindInfoCompare);
×
5335
  }
5336

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

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

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

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

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

5419
    SRow *row;
×
5420

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

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

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

5461
static int32_t tColDataCopyRowCell(SColData *pFromColData, int32_t iFromRow, SColData *pToColData, int32_t iToRow) {
41,161,134✔
5462
  int32_t code = TSDB_CODE_SUCCESS;
41,161,134✔
5463

5464
  if (IS_VAR_DATA_TYPE(pToColData->type)) {
41,161,134✔
5465
    int32_t nData = (iFromRow < pFromColData->nVal - 1)
5,190,393✔
5466
                        ? pFromColData->aOffset[iFromRow + 1] - pFromColData->aOffset[iFromRow]
4,668,389✔
5467
                        : pFromColData->nData - pFromColData->aOffset[iFromRow];
9,858,782✔
5468
    if (iToRow == 0) {
5,190,393✔
5469
      pToColData->aOffset[iToRow] = 0;
14,304✔
5470
    }
5471

5472
    if (iToRow < pToColData->nVal - 1) {
5,190,393✔
5473
      pToColData->aOffset[iToRow + 1] = pToColData->aOffset[iToRow] + nData;
5,178,063✔
5474
    }
5475

5476
    (void)memcpy(pToColData->pData + pToColData->aOffset[iToRow], pFromColData->pData + pFromColData->aOffset[iFromRow],
5,190,393✔
5477
                 nData);
5478
  } else {
5479
    (void)memcpy(&pToColData->pData[TYPE_BYTES[pToColData->type] * iToRow],
35,970,741✔
5480
                 &pFromColData->pData[TYPE_BYTES[pToColData->type] * iFromRow], TYPE_BYTES[pToColData->type]);
35,970,741✔
5481
  }
5482
  return code;
41,161,134✔
5483
}
5484

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

5490
  switch (pFromColData->flag) {
42,640,502✔
5491
    case HAS_NONE: {
×
5492
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5493
    } break;
×
5494
    case HAS_NULL: {
1,479,368✔
5495
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
1,479,368✔
5496
    } break;
1,479,368✔
5497
    case (HAS_NULL | HAS_NONE): {
×
5498
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5499
      if (0 == bit_val)
×
5500
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5501
      else
5502
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
×
5503
    } break;
×
5504
    case HAS_VALUE: {
4,662,716✔
5505
      ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
4,662,716✔
5506
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
4,662,716✔
5507
    } break;
4,662,716✔
5508
    case (HAS_VALUE | HAS_NONE): {
×
5509
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
×
5510
      if (0 == bit_val)
×
5511
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NONE);
×
5512
      else
5513
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
×
5514
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5515
    } break;
×
5516
    case (HAS_VALUE | HAS_NULL): {
36,498,418✔
5517
      bit_val = GET_BIT1(pFromColData->pBitMap, iFromRow);
36,498,418✔
5518
      if (0 == bit_val)
36,498,418✔
5519
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_NULL);
16,478,070✔
5520
      else
5521
        ROW_SET_BITMAP(pToColData->pBitMap, pToColData->flag, iToRow, BIT_FLG_VALUE);
20,020,348✔
5522
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
36,498,418✔
5523
    } break;
36,498,418✔
5524
    case (HAS_VALUE | HAS_NULL | HAS_NONE): {
×
5525
      SET_BIT2(pToColData->pBitMap, iToRow, GET_BIT2(pFromColData->pBitMap, iFromRow));
×
5526
      TAOS_CHECK_RETURN(tColDataCopyRowCell(pFromColData, iFromRow, pToColData, iToRow));
×
5527
    } break;
×
5528
    default:
×
5529
      return -1;
×
5530
  }
5531

5532
  return code;
42,640,502✔
5533
}
5534

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

5539
  for (int32_t i = 0; i < nColData; i++) {
45,320,419✔
5540
    code = tColDataCopyRowSingleCol(&aFromColData[i], iFromRow, &aToColData[i], iToRow);
42,640,502✔
5541
    if (code != TSDB_CODE_SUCCESS) {
42,640,502✔
5542
      return code;
×
5543
    }
5544
  }
5545

5546
  return code;
2,679,917✔
5547
}
5548

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

5552
  for (int32_t i = 0; i < nColData; i++) {
45,320,419✔
5553
    SColVal cv = {0};
42,640,502✔
5554
    code = tColDataGetValue(&aFromColData[i], iFromRow, &cv);
42,640,502✔
5555
    if (code != TSDB_CODE_SUCCESS) {
42,640,502✔
5556
      return code;
×
5557
    }
5558
    code = tColDataAppendValue(&aToColData[i], &cv);
42,640,502✔
5559
    if (code != TSDB_CODE_SUCCESS) {
42,640,502✔
5560
      return code;
×
5561
    }
5562
  }
5563

5564
  return code;
2,679,917✔
5565
}
5566

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

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

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

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

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

5598
  tColDataArrGetRowKey(aColData, nColData, i, &keyi);
301,327✔
5599
  tColDataArrGetRowKey(aColData, nColData, j, &keyj);
301,327✔
5600
  while (i <= mid && j <= end) {
1,641,984✔
5601
    if (tRowKeyCompare(&keyi, &keyj) <= 0) {
1,340,657✔
5602
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
8,414✔
5603
      tColDataArrGetRowKey(aColData, nColData, i, &keyi);
8,414✔
5604
    } else {
5605
      TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
1,332,243✔
5606
      if (j <= end) tColDataArrGetRowKey(aColData, nColData, j, &keyj);
1,332,243✔
5607
    }
5608
  }
5609

5610
  while (i <= mid) {
1,634,132✔
5611
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, i++, aDstColData, nColData));
1,332,805✔
5612
  }
5613

5614
  while (j <= end) {
307,782✔
5615
    TAOS_CHECK_RETURN(tColDataCopyRowAppend(aColData, j++, aDstColData, nColData));
6,455✔
5616
  }
5617

5618
  for (i = start, k = 0; i <= end; ++i, ++k) {
2,981,244✔
5619
    TAOS_CHECK_RETURN(tColDataCopyRow(aDstColData, k, aColData, i, nColData));
2,679,917✔
5620
  }
5621

5622
  if (aDstColData) {
301,327✔
5623
    for (int32_t i = 0; i < nColData; i++) {
5,048,667✔
5624
      tColDataDestroy(&aDstColData[i]);
4,747,340✔
5625
    }
5626
    taosMemoryFree(aDstColData);
301,327✔
5627
  }
5628

5629
  return TSDB_CODE_SUCCESS;
301,327✔
5630
}
5631

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

5636
  if (start >= end) {
604,942✔
5637
    return TSDB_CODE_SUCCESS;
303,615✔
5638
  }
5639

5640
  mid = (start + end) / 2;
301,327✔
5641

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

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

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

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

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

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

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

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

5673
  for (int32_t i = 0; i < taosArrayGetSize(src); i++) {
3,340✔
5674
    SColData *srcCol = taosArrayGet(src, i);
2,505✔
5675

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

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

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

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

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

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

5734
int32_t tColDataSortMerge(SArray **arr) {
6,609,809✔
5735
  SArray   *colDataArr = *arr;
6,609,809✔
5736
  int32_t   nColData = TARRAY_SIZE(colDataArr);
6,612,058✔
5737
  SColData *aColData = (SColData *)TARRAY_DATA(colDataArr);
6,613,394✔
5738

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

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

5751
  int8_t doSort = 0;
6,581,272✔
5752
  int8_t doMerge = 0;
6,581,272✔
5753
  // scan -------
5754
  SRowKey lastKey;
6,570,562✔
5755
  tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
6,582,894✔
5756
  for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
2,147,483,647✔
5757
    SRowKey key;
2,147,483,647✔
5758
    tColDataArrGetRowKey(aColData, nColData, iVal, &key);
2,147,483,647✔
5759

5760
    int32_t c = tRowKeyCompare(&lastKey, &key);
2,147,483,647✔
5761
    if (c < 0) {
2,147,483,647✔
5762
      lastKey = key;
2,147,483,647✔
5763
      continue;
2,147,483,647✔
5764
    } else if (c > 0) {
3,605,506✔
5765
      doSort = 1;
2,288✔
5766
      break;
2,288✔
5767
    } else {
5768
      doMerge = 1;
3,603,218✔
5769
    }
5770
  }
5771

5772
  // sort -------
5773
  if (doSort) {
5,680,392✔
5774
    TAOS_CHECK_RETURN(tColDataSort(aColData, nColData));
2,288✔
5775
  }
5776

5777
  if ((doMerge != 1) && (doSort == 1)) {
5,691,086✔
5778
    tColDataArrGetRowKey(aColData, nColData, 0, &lastKey);
2,288✔
5779
    for (int32_t iVal = 1; iVal < aColData[0].nVal; ++iVal) {
303,615✔
5780
      SRowKey key;
301,327✔
5781
      tColDataArrGetRowKey(aColData, nColData, iVal, &key);
301,327✔
5782

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

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

5798
_exit:
6,614,674✔
5799
  return 0;
6,615,374✔
5800
}
5801

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

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

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

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

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

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

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

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

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

5872
static int32_t tEncodeColDataVersion0(SEncoder *pEncoder, SColData *pColData) {
40,819,768✔
5873
  int32_t code = 0;
40,819,768✔
5874

5875
  if ((code = tEncodeI16v(pEncoder, pColData->cid))) return code;
81,648,812✔
5876
  if ((code = tEncodeI8(pEncoder, pColData->type))) return code;
81,645,790✔
5877
  if ((code = tEncodeI32v(pEncoder, pColData->nVal))) return code;
81,626,308✔
5878
  if ((code = tEncodeI8(pEncoder, pColData->flag))) return code;
81,606,064✔
5879

5880
  // bitmap
5881
  switch (pColData->flag) {
40,796,502✔
5882
    case (HAS_NULL | HAS_NONE):
124,598✔
5883
    case (HAS_VALUE | HAS_NONE):
5884
    case (HAS_VALUE | HAS_NULL):
5885
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT1_SIZE(pColData->nVal));
124,598✔
5886
      if (code) return code;
124,598✔
5887
      break;
124,598✔
5888
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5889
      code = tEncodeFixed(pEncoder, pColData->pBitMap, BIT2_SIZE(pColData->nVal));
×
5890
      if (code) return code;
×
5891
      break;
×
5892
    default:
40,667,256✔
5893
      break;
40,667,256✔
5894
  }
5895

5896
  // value
5897
  if (pColData->flag & HAS_VALUE) {
40,791,854✔
5898
    if (IS_VAR_DATA_TYPE(pColData->type)) {
40,736,605✔
5899
      code = tEncodeFixed(pEncoder, pColData->aOffset, pColData->nVal << 2);
166,570✔
5900
      if (code) return code;
134,096✔
5901

5902
      code = tEncodeI32v(pEncoder, pColData->nData);
134,096✔
5903
      if (code) return code;
134,098✔
5904

5905
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
134,098✔
5906
      if (code) return code;
134,098✔
5907
    } else {
5908
      code = tEncodeFixed(pEncoder, pColData->pData, pColData->nData);
40,606,779✔
5909
      if (code) return code;
40,611,053✔
5910
    }
5911
  }
5912

5913
  return code;
40,808,732✔
5914
}
5915

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

5919
  if ((code = tDecodeI16v(pDecoder, &pColData->cid))) return code;
58,002,654✔
5920
  if ((code = tDecodeI8(pDecoder, &pColData->type))) return code;
58,005,088✔
5921
  if ((code = tDecodeI32v(pDecoder, &pColData->nVal))) return code;
58,003,645✔
5922
  if ((code = tDecodeI8(pDecoder, &pColData->flag))) return code;
58,003,725✔
5923

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

5928
  // bitmap
5929
  switch (pColData->flag) {
29,003,822✔
5930
    case (HAS_NULL | HAS_NONE):
46,059✔
5931
    case (HAS_VALUE | HAS_NONE):
5932
    case (HAS_VALUE | HAS_NULL):
5933
      code = tDecodeBinaryWithSize(pDecoder, BIT1_SIZE(pColData->nVal), &pColData->pBitMap);
46,059✔
5934
      if (code) return code;
46,059✔
5935
      break;
46,059✔
5936
    case (HAS_VALUE | HAS_NULL | HAS_NONE):
×
5937
      code = tDecodeBinaryWithSize(pDecoder, BIT2_SIZE(pColData->nVal), &pColData->pBitMap);
×
5938
      if (code) return code;
×
5939
      break;
×
5940
    default:
28,953,690✔
5941
      break;
28,953,690✔
5942
  }
5943

5944
  // value
5945
  if (pColData->flag & HAS_VALUE) {
28,999,749✔
5946
    if (IS_VAR_DATA_TYPE(pColData->type)) {
28,965,606✔
5947
      code = tDecodeBinaryWithSize(pDecoder, pColData->nVal << 2, (uint8_t **)&pColData->aOffset);
102,389✔
5948
      if (code) return code;
97,315✔
5949

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

5953
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
97,315✔
5954
      if (code) return code;
97,315✔
5955
    } else {
5956
      pColData->nData = TYPE_BYTES[pColData->type] * pColData->nVal;
28,866,448✔
5957
      code = tDecodeBinaryWithSize(pDecoder, pColData->nData, &pColData->pData);
28,868,808✔
5958
      if (code) return code;
28,869,888✔
5959
    }
5960
  }
5961
  pColData->cflag = 0;
29,003,346✔
5962

5963
  return code;
29,001,707✔
5964
}
5965

5966
static int32_t tEncodeColDataVersion1(SEncoder *pEncoder, SColData *pColData) {
40,823,324✔
5967
  int32_t code = tEncodeColDataVersion0(pEncoder, pColData);
40,823,324✔
5968
  if (code) return code;
40,798,801✔
5969
  return tEncodeI8(pEncoder, pColData->cflag);
81,632,492✔
5970
}
5971

5972
static int32_t tDecodeColDataVersion1(SDecoder *pDecoder, SColData *pColData) {
29,000,110✔
5973
  int32_t code = tDecodeColDataVersion0(pDecoder, pColData);
29,000,110✔
5974
  if (code) return code;
28,999,834✔
5975

5976
  code = tDecodeI8(pDecoder, &pColData->cflag);
28,999,834✔
5977
  return code;
29,003,764✔
5978
}
5979

5980
int32_t tEncodeColData(uint8_t version, SEncoder *pEncoder, SColData *pColData) {
40,783,706✔
5981
  if (version == 0) {
40,783,706✔
5982
    return tEncodeColDataVersion0(pEncoder, pColData);
×
5983
  } else if (version == 1) {
40,783,706✔
5984
    return tEncodeColDataVersion1(pEncoder, pColData);
×
5985
  } else if (version == 2) {
40,783,706✔
5986
    int32_t posStart = pEncoder->pos;
40,787,438✔
5987
    pEncoder->pos += INT_BYTES;
40,807,450✔
5988
    int32_t code = tEncodeColDataVersion1(pEncoder, pColData);
40,840,872✔
5989
    if (code) return code;
40,825,818✔
5990
    int32_t posEnd = pEncoder->pos;
40,825,818✔
5991
    int32_t pos = posEnd - posStart;
40,836,930✔
5992
    pEncoder->pos = posStart;
40,836,930✔
5993
    code = tEncodeI32(pEncoder, pos);
40,818,118✔
5994
    pEncoder->pos = posEnd;
40,818,118✔
5995
    return code;
40,823,860✔
5996
  } else {
5997
    return TSDB_CODE_INVALID_PARA;
70✔
5998
  }
5999
}
6000

6001
int32_t tDecodeColData(uint8_t version, SDecoder *pDecoder, SColData *pColData, bool jump) {
42,421,695✔
6002
  if (version == 0) {
42,421,695✔
6003
    return tDecodeColDataVersion0(pDecoder, pColData);
×
6004
  } else if (version == 1) {
42,421,695✔
6005
    return tDecodeColDataVersion1(pDecoder, pColData);
×
6006
  } else if (version == 2) {
42,421,695✔
6007
    if (jump) {
42,422,846✔
6008
      int32_t len = 0;
13,422,756✔
6009
      int32_t code = tDecodeI32(pDecoder, &len);
13,423,384✔
6010
      if (code) return code;
13,423,384✔
6011
      pDecoder->pos += (len - INT_BYTES);
13,423,384✔
6012
      return TSDB_CODE_SUCCESS;
13,423,070✔
6013
    } else {
6014
      pDecoder->pos += INT_BYTES;
29,000,090✔
6015
      return tDecodeColDataVersion1(pDecoder, pColData);
29,002,230✔
6016
    }    
6017
  } else {
6018
    return TSDB_CODE_INVALID_PARA;
×
6019
  }
6020
}
6021

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

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

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

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

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

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

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

6050
  // if (pRow->type) {
6051
  void *pIter = taosHashIterate(pRow->pSeqToffset, NULL);
42,906✔
6052
  while (pIter) {
47,888,214✔
6053
    uint64_t seq = *(uint64_t *)taosHashGetKey(pIter, NULL);
47,845,308✔
6054
    int32_t  idx = *(int32_t *)pIter;
47,845,308✔
6055
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, seq));
47,845,308✔
6056
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, idx));
47,845,308✔
6057

6058
    pIter = taosHashIterate(pRow->pSeqToffset, pIter);
47,845,308✔
6059
  }
6060
  //}
6061
  for (int32_t i = 0; i < nSeq; i++) {
47,888,214✔
6062
    SBlobValue *p = taosArrayGet(pRow->pSeqTable, i);
47,845,308✔
6063
    TAOS_CHECK_EXIT(tEncodeU64(pEncoder, p->offset));
95,690,616✔
6064
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->len));
95,690,616✔
6065
    TAOS_CHECK_EXIT(tEncodeU32(pEncoder, p->dataOffset));
95,690,616✔
6066
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->nextRow));
95,690,616✔
6067
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, p->type));
95,690,616✔
6068
  }
6069

6070
  TAOS_CHECK_EXIT(tEncodeFixed(pEncoder, pRow->data, pRow->len));
85,812✔
6071

6072
_exit:
42,906✔
6073
  return code;
42,906✔
6074
}
6075

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

6089
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nSeq));
21,453✔
6090

6091
  // if (pBlob->type) {
6092
  pBlob->pSeqToffset = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK);
21,453✔
6093
  if (pBlob->pSeqToffset == NULL) {
21,453✔
6094
    TAOS_CHECK_EXIT(terrno);
×
6095
  }
6096
  for (int32_t i = 0; i < nSeq; i++) {
23,944,107✔
6097
    uint64_t seq;
23,922,654✔
6098
    int32_t  idx;
23,922,654✔
6099
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &seq));
23,922,654✔
6100
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
23,922,654✔
6101

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

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

6112
  for (int32_t i = 0; i < nSeq; i++) {
23,944,107✔
6113
    SBlobValue value;
23,922,654✔
6114
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &value.offset));
23,922,654✔
6115
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.len));
23,922,654✔
6116
    TAOS_CHECK_EXIT(tDecodeU32(pDecoder, &value.dataOffset));
23,922,654✔
6117
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.nextRow));
23,922,654✔
6118
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &value.type));
23,922,654✔
6119
    if (taosArrayPush(pBlob->pSeqTable, &value) == NULL) {
47,845,308✔
6120
      TAOS_CHECK_EXIT(terrno);
×
6121
    }
6122
  }
6123

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

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

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

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

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

6152
static FORCE_INLINE void tColDataCalcSMABool(SColData *pColData, SColumnDataAgg *pAggs) {
2,642,904✔
6153
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,642,904✔
6154
  int16_t *numOfNull = &pAggs->numOfNull;
2,642,988✔
6155
  *sum = 0;
2,642,904✔
6156
  *max = 0;
2,642,988✔
6157
  *min = 1;
2,642,904✔
6158
  *numOfNull = 0;
2,642,904✔
6159

6160
  int8_t val;
6161
  if (HAS_VALUE == pColData->flag) {
2,642,904✔
6162
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6163
      val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
2,147,483,647✔
6164
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6165
    }
6166
  } else {
6167
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,800,450✔
6168
      switch (tColDataGetBitValue(pColData, iVal)) {
1,800,000✔
6169
        case 0:
89,619✔
6170
        case 1:
6171
          (*numOfNull)++;
89,619✔
6172
          break;
89,619✔
6173
        case 2:
1,710,381✔
6174
          val = ((int8_t *)pColData->pData)[iVal] ? 1 : 0;
1,710,381✔
6175
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
1,710,381✔
6176
          break;
1,710,381✔
6177
        default:
×
6178
          break;
×
6179
      }
6180
    }
6181
  }
6182
}
2,508,992✔
6183

6184
static FORCE_INLINE void tColDataCalcSMATinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,658,428✔
6185
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,658,428✔
6186
  int16_t *numOfNull = &pAggs->numOfNull;
2,658,428✔
6187
  *sum = 0;
2,658,428✔
6188
  *max = INT8_MIN;
2,658,428✔
6189
  *min = INT8_MAX;
2,658,428✔
6190
  *numOfNull = 0;
2,658,428✔
6191

6192
  int8_t val;
6193
  if (HAS_VALUE == pColData->flag) {
2,658,428✔
6194
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6195
      val = ((int8_t *)pColData->pData)[iVal];
2,147,483,647✔
6196
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6197
    }
6198
  } else {
6199
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,908,557✔
6200
      switch (tColDataGetBitValue(pColData, iVal)) {
2,907,000✔
6201
        case 0:
987,573✔
6202
        case 1:
6203
          (*numOfNull)++;
987,573✔
6204
          break;
987,573✔
6205
        case 2:
1,919,427✔
6206
          val = ((int8_t *)pColData->pData)[iVal];
1,919,427✔
6207
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
1,919,427✔
6208
          break;
1,919,427✔
6209
        default:
×
6210
          break;
×
6211
      }
6212
    }
6213
  }
6214
}
2,658,647✔
6215

6216
static FORCE_INLINE void tColDataCalcSMATinySmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,644,246✔
6217
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,644,246✔
6218
  int16_t *numOfNull = &pAggs->numOfNull;
2,644,246✔
6219
  *sum = 0;
2,644,246✔
6220
  *max = INT16_MIN;
2,644,246✔
6221
  *min = INT16_MAX;
2,644,246✔
6222
  *numOfNull = 0;
2,644,246✔
6223

6224
  int16_t val;
6225
  if (HAS_VALUE == pColData->flag) {
2,644,246✔
6226
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6227
      val = ((int16_t *)pColData->pData)[iVal];
2,147,483,647✔
6228
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6229
    }
6230
  } else {
6231
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,800,450✔
6232
      switch (tColDataGetBitValue(pColData, iVal)) {
1,800,000✔
6233
        case 0:
89,496✔
6234
        case 1:
6235
          (*numOfNull)++;
89,496✔
6236
          break;
89,496✔
6237
        case 2:
1,710,504✔
6238
          val = ((int16_t *)pColData->pData)[iVal];
1,710,504✔
6239
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
1,710,504✔
6240
          break;
1,710,504✔
6241
        default:
×
6242
          break;
×
6243
      }
6244
    }
6245
  }
6246
}
2,636,489✔
6247

6248
static FORCE_INLINE void tColDataCalcSMAInt(SColData *pColData, SColumnDataAgg *pAggs) {
19,501,238✔
6249
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
19,501,238✔
6250
  int16_t *numOfNull = &pAggs->numOfNull;
19,506,923✔
6251
  *sum = 0;
19,503,110✔
6252
  *max = INT32_MIN;
19,503,749✔
6253
  *min = INT32_MAX;
19,504,378✔
6254
  *numOfNull = 0;
19,505,656✔
6255

6256
  int32_t val;
6257
  if (HAS_VALUE == pColData->flag) {
19,503,122✔
6258
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6259
      val = ((int32_t *)pColData->pData)[iVal];
2,147,483,647✔
6260
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6261
    }
6262
  } else {
6263
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6264
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6265
        case 0:
227,360,363✔
6266
        case 1:
6267
          (*numOfNull)++;
227,360,363✔
6268
          break;
227,360,363✔
6269
        case 2:
2,044,860,928✔
6270
          val = ((int32_t *)pColData->pData)[iVal];
2,044,860,928✔
6271
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,044,860,928✔
6272
          break;
2,044,860,928✔
6273
        default:
×
6274
          break;
×
6275
      }
6276
    }
6277
  }
6278
}
15,319,170✔
6279

6280
static FORCE_INLINE void tColDataCalcSMABigInt(SColData *pColData, SColumnDataAgg *pAggs) {
57,832,585✔
6281
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
57,832,585✔
6282
  int16_t *numOfNull = &pAggs->numOfNull;
57,832,909✔
6283
  *sum = 0;
57,832,197✔
6284
  *max = INT64_MIN;
57,832,197✔
6285
  *min = INT64_MAX;
57,831,485✔
6286
  *numOfNull = 0;
57,832,359✔
6287

6288
  int64_t val;
6289
  if (HAS_VALUE == pColData->flag) {
57,831,723✔
6290
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6291
      val = ((int64_t *)pColData->pData)[iVal];
2,147,483,647✔
6292
      CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,147,483,647✔
6293
    }
6294
  } else {
6295
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6296
      switch (tColDataGetBitValue(pColData, iVal)) {
2,147,483,647✔
6297
        case 0:
253,183,841✔
6298
        case 1:
6299
          (*numOfNull)++;
253,183,841✔
6300
          break;
253,183,841✔
6301
        case 2:
2,050,179,241✔
6302
          val = ((int64_t *)pColData->pData)[iVal];
2,050,179,241✔
6303
          CALC_SUM_MAX_MIN(*sum, *max, *min, val);
2,050,179,241✔
6304
          break;
2,050,179,241✔
6305
        default:
×
6306
          break;
×
6307
      }
6308
    }
6309
  }
6310
}
84✔
6311

6312
static FORCE_INLINE void tColDataCalcSMAFloat(SColData *pColData, SColumnDataAgg *pAggs) {
20,950,493✔
6313
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
20,950,493✔
6314
  int16_t *numOfNull = &pAggs->numOfNull;
20,951,118✔
6315
  *(double *)sum = 0;
20,951,118✔
6316
  *(double *)max = -FLT_MAX;
20,951,118✔
6317
  *(double *)min = FLT_MAX;
20,951,034✔
6318
  *numOfNull = 0;
20,950,043✔
6319

6320
  float val;
6321
  if (HAS_VALUE == pColData->flag) {
20,950,584✔
6322
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6323
      val = ((float *)pColData->pData)[iVal];
2,147,483,647✔
6324
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6325
    }
6326
  } else {
6327
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
12,208,119✔
6328
      switch (tColDataGetBitValue(pColData, iVal)) {
12,192,033✔
6329
        case 0:
1,893,582✔
6330
        case 1:
6331
          (*numOfNull)++;
1,893,582✔
6332
          break;
1,893,582✔
6333
        case 2:
10,298,451✔
6334
          val = ((float *)pColData->pData)[iVal];
10,298,451✔
6335
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
10,298,451✔
6336
          break;
10,298,451✔
6337
        default:
×
6338
          break;
×
6339
      }
6340
    }
6341
  }
6342
}
9,389,664✔
6343

6344
static FORCE_INLINE void tColDataCalcSMADouble(SColData *pColData, SColumnDataAgg *pAggs) {
9,096,625✔
6345
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
9,096,625✔
6346
  int16_t *numOfNull = &pAggs->numOfNull;
9,105,830✔
6347
  *(double *)sum = 0;
9,105,819✔
6348
  *(double *)max = -DBL_MAX;
9,105,191✔
6349
  *(double *)min = DBL_MAX;
9,104,238✔
6350
  *numOfNull = 0;
9,104,877✔
6351

6352
  double val;
6353
  if (HAS_VALUE == pColData->flag) {
9,105,191✔
6354
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6355
      val = ((double *)pColData->pData)[iVal];
2,147,483,647✔
6356
      CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
2,147,483,647✔
6357
    }
6358
  } else {
6359
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,908,557✔
6360
      switch (tColDataGetBitValue(pColData, iVal)) {
2,907,000✔
6361
        case 0:
978,852✔
6362
        case 1:
6363
          (*numOfNull)++;
978,852✔
6364
          break;
978,852✔
6365
        case 2:
1,928,148✔
6366
          val = ((double *)pColData->pData)[iVal];
1,928,148✔
6367
          CALC_SUM_MAX_MIN(*(double *)sum, *(double *)max, *(double *)min, val);
1,928,148✔
6368
          break;
1,928,148✔
6369
        default:
×
6370
          break;
×
6371
      }
6372
    }
6373
  }
6374
}
8,530,980✔
6375

6376
static FORCE_INLINE void tColDataCalcSMAUTinyInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,331,224✔
6377
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,331,224✔
6378
  int16_t *numOfNull = &pAggs->numOfNull;
2,331,224✔
6379
  *(uint64_t *)sum = 0;
2,331,224✔
6380
  *(uint64_t *)max = 0;
2,331,224✔
6381
  *(uint64_t *)min = UINT8_MAX;
2,331,224✔
6382
  *numOfNull = 0;
2,331,224✔
6383

6384
  uint8_t val;
6385
  if (HAS_VALUE == pColData->flag) {
2,331,224✔
6386
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6387
      val = ((uint8_t *)pColData->pData)[iVal];
2,147,483,647✔
6388
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6389
    }
6390
  } else {
6391
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,800,450✔
6392
      switch (tColDataGetBitValue(pColData, iVal)) {
1,800,000✔
6393
        case 0:
89,781✔
6394
        case 1:
6395
          (*numOfNull)++;
89,781✔
6396
          break;
89,781✔
6397
        case 2:
1,710,219✔
6398
          val = ((uint8_t *)pColData->pData)[iVal];
1,710,219✔
6399
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
1,710,219✔
6400
          break;
1,710,219✔
6401
        default:
×
6402
          break;
×
6403
      }
6404
    }
6405
  }
6406
}
2,245,790✔
6407

6408
static FORCE_INLINE void tColDataCalcSMATinyUSmallInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,331,224✔
6409
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,331,224✔
6410
  int16_t *numOfNull = &pAggs->numOfNull;
2,331,224✔
6411
  *(uint64_t *)sum = 0;
2,331,224✔
6412
  *(uint64_t *)max = 0;
2,331,224✔
6413
  *(uint64_t *)min = UINT16_MAX;
2,331,224✔
6414
  *numOfNull = 0;
2,331,224✔
6415

6416
  uint16_t val;
6417
  if (HAS_VALUE == pColData->flag) {
2,331,224✔
6418
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6419
      val = ((uint16_t *)pColData->pData)[iVal];
2,147,483,647✔
6420
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6421
    }
6422
  } else {
6423
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,800,450✔
6424
      switch (tColDataGetBitValue(pColData, iVal)) {
1,800,000✔
6425
        case 0:
89,994✔
6426
        case 1:
6427
          (*numOfNull)++;
89,994✔
6428
          break;
89,994✔
6429
        case 2:
1,710,006✔
6430
          val = ((uint16_t *)pColData->pData)[iVal];
1,710,006✔
6431
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
1,710,006✔
6432
          break;
1,710,006✔
6433
        default:
×
6434
          break;
×
6435
      }
6436
    }
6437
  }
6438
}
2,278,211✔
6439

6440
static FORCE_INLINE void tColDataCalcSMAUInt(SColData *pColData, SColumnDataAgg *pAggs) {
2,331,224✔
6441
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
2,331,224✔
6442
  int16_t *numOfNull = &pAggs->numOfNull;
2,331,224✔
6443
  *(uint64_t *)sum = 0;
2,331,224✔
6444
  *(uint64_t *)max = 0;
2,331,224✔
6445
  *(uint64_t *)min = UINT32_MAX;
2,331,224✔
6446
  *numOfNull = 0;
2,331,224✔
6447

6448
  uint32_t val;
6449
  if (HAS_VALUE == pColData->flag) {
2,331,224✔
6450
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6451
      val = ((uint32_t *)pColData->pData)[iVal];
2,147,483,647✔
6452
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6453
    }
6454
  } else {
6455
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,800,450✔
6456
      switch (tColDataGetBitValue(pColData, iVal)) {
1,800,000✔
6457
        case 0:
89,445✔
6458
        case 1:
6459
          (*numOfNull)++;
89,445✔
6460
          break;
89,445✔
6461
        case 2:
1,710,555✔
6462
          val = ((uint32_t *)pColData->pData)[iVal];
1,710,555✔
6463
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
1,710,555✔
6464
          break;
1,710,555✔
6465
        default:
×
6466
          break;
×
6467
      }
6468
    }
6469
  }
6470
}
2,332,939✔
6471

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

6480
  uint64_t val;
6481
  if (HAS_VALUE == pColData->flag) {
2,331,224✔
6482
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
2,147,483,647✔
6483
      val = ((uint64_t *)pColData->pData)[iVal];
2,147,483,647✔
6484
      CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
2,147,483,647✔
6485
    }
6486
  } else {
6487
    for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
1,800,450✔
6488
      switch (tColDataGetBitValue(pColData, iVal)) {
1,800,000✔
6489
        case 0:
90,192✔
6490
        case 1:
6491
          (*numOfNull)++;
90,192✔
6492
          break;
90,192✔
6493
        case 2:
1,709,808✔
6494
          val = ((uint64_t *)pColData->pData)[iVal];
1,709,808✔
6495
          CALC_SUM_MAX_MIN(*(uint64_t *)sum, *(uint64_t *)max, *(uint64_t *)min, val);
1,709,808✔
6496
          break;
1,709,808✔
6497
        default:
×
6498
          break;
×
6499
      }
6500
    }
6501
  }
6502
}
2,320,904✔
6503

6504
static FORCE_INLINE void tColDataCalcSMAVarType(SColData *pColData, SColumnDataAgg *pAggs) {
7,435,905✔
6505
  int64_t *sum = &pAggs->sum, *max = &pAggs->max, *min = &pAggs->min;
7,435,905✔
6506
  int16_t *numOfNull = &pAggs->numOfNull;
7,435,393✔
6507
  *(uint64_t *)sum = 0;
7,435,905✔
6508
  *(uint64_t *)max = 0;
7,435,393✔
6509
  *(uint64_t *)min = 0;
7,434,881✔
6510
  *numOfNull = 0;
7,433,857✔
6511

6512
  switch (pColData->flag) {
7,434,369✔
6513
    case HAS_NONE:
×
6514
    case HAS_NULL:
6515
    case (HAS_NONE | HAS_NULL):
6516
      *numOfNull = pColData->nVal;
×
6517
      break;
×
6518
    case HAS_VALUE:
7,428,614✔
6519
      *numOfNull = 0;
7,428,614✔
6520
      break;
7,431,622✔
6521
    case (HAS_VALUE | HAS_NULL):
4,283✔
6522
    case (HAS_VALUE | HAS_NONE):
6523
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
11,539,283✔
6524
        if (GET_BIT1(pColData->pBitMap, iVal) == 0) {
11,535,000✔
6525
          (*numOfNull)++;
1,208,919✔
6526
        }
6527
      }
6528
      break;
4,283✔
6529
    case (HAS_VALUE | HAS_NONE | HAS_NULL):
×
6530
      for (int32_t iVal = 0; iVal < pColData->nVal; iVal++) {
×
6531
        if (GET_BIT2(pColData->pBitMap, iVal) != 2) {
×
6532
          (*numOfNull)++;
×
6533
        }
6534
      }
6535
      break;
×
6536
    default:
×
6537
      break;
×
6538
  }
6539
}
7,435,905✔
6540

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

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

6563
  Decimal64         *pVal = NULL;
18,072✔
6564
  const SDecimalOps *pSumOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
18,072✔
6565
  const SDecimalOps *pCompOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
18,072✔
6566
  if (HAS_VALUE == pColData->flag) {
18,072✔
6567
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
71,205✔
6568
      pVal = ((Decimal64 *)pColData->pData) + iVal;
70,500✔
6569
      CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
70,500✔
6570
    }
6571
  } else {
6572
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
13,577,367✔
6573
      switch (tColDataGetBitValue(pColData, iVal)) {
13,560,000✔
6574
        case 0:
553,125✔
6575
        case 1:
6576
          pAggs->numOfNull++;
553,125✔
6577
          break;
553,125✔
6578
        case 2:
13,006,875✔
6579
          pVal = ((Decimal64 *)pColData->pData) + iVal;
13,006,875✔
6580
          CALC_DECIMAL_SUM_MAX_MIN(Decimal64, pSumOps, pCompOps, pColData, pSum, pMax, pMin);
13,006,875✔
6581
          break;
13,006,875✔
6582
        default:
×
6583
          break;
×
6584
      }
6585
    }
6586
  }
6587
}
18,072✔
6588

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

6599
  Decimal128        *pVal = NULL;
48,606✔
6600
  const SDecimalOps *pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
48,606✔
6601
  if (HAS_VALUE == pColData->flag) {
48,606✔
6602
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
213,615✔
6603
      pVal = ((Decimal128 *)pColData->pData) + iVal;
211,500✔
6604
      CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
211,500✔
6605
    }
6606
  } else {
6607
    for (int32_t iVal = 0; iVal < pColData->nVal; ++iVal) {
43,999,491✔
6608
      switch (tColDataGetBitValue(pColData, iVal)) {
43,953,000✔
6609
        case 0:
1,783,839✔
6610
        case 1:
6611
          pAggs->numOfNull++;
1,783,839✔
6612
          break;
1,783,839✔
6613
        case 2:
42,169,161✔
6614
          pVal = ((Decimal128 *)pColData->pData) + iVal;
42,169,161✔
6615
          CALC_DECIMAL_SUM_MAX_MIN(Decimal128, pOps, pOps, pColData, pSum, pMax, pMin);
42,169,161✔
6616
          break;
42,169,161✔
6617
        default:
×
6618
          break;
×
6619
      }
6620
    }
6621
  }
6622
}
48,606✔
6623

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

6649
// SValueColumn ================================
6650
int32_t tValueColumnInit(SValueColumn *valCol) {
668,103,239✔
6651
  valCol->type = TSDB_DATA_TYPE_NULL;
668,103,239✔
6652
  valCol->numOfValues = 0;
668,194,639✔
6653
  tBufferInit(&valCol->data);
668,254,807✔
6654
  tBufferInit(&valCol->offsets);
668,223,597✔
6655
  return 0;
668,371,624✔
6656
}
6657

6658
void tValueColumnDestroy(SValueColumn *valCol) {
1,623,524,353✔
6659
  valCol->type = TSDB_DATA_TYPE_NULL;
1,623,524,353✔
6660
  valCol->numOfValues = 0;
1,623,732,079✔
6661
  tBufferDestroy(&valCol->data);
1,623,818,259✔
6662
  tBufferDestroy(&valCol->offsets);
1,623,587,119✔
6663
  return;
1,623,555,273✔
6664
}
6665

6666
void tValueColumnClear(SValueColumn *valCol) {
892,985,993✔
6667
  valCol->type = TSDB_DATA_TYPE_NULL;
892,985,993✔
6668
  valCol->numOfValues = 0;
893,108,136✔
6669
  tBufferClear(&valCol->data);
893,129,315✔
6670
  tBufferClear(&valCol->offsets);
893,111,750✔
6671
  return;
893,103,087✔
6672
}
6673

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

6677
  if (valCol->numOfValues == 0) {
1,582,126✔
6678
    valCol->type = value->type;
778,996✔
6679
  }
6680

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

6685
  if (IS_VAR_DATA_TYPE(value->type)) {
1,582,126✔
6686
    if ((code = tBufferPutI32(&valCol->offsets, tBufferGetSize(&valCol->data)))) {
1,267,712✔
6687
      return code;
×
6688
    }
6689
    if ((code = tBufferPut(&valCol->data, value->pData, value->nData))) {
1,267,712✔
6690
      return code;
×
6691
    }
6692
  } else {
6693
    code = tBufferPut(&valCol->data, VALUE_GET_DATUM(value, value->type), tDataTypes[value->type].bytes);
948,270✔
6694
    if (code) return code;
948,270✔
6695
  }
6696
  valCol->numOfValues++;
1,582,126✔
6697

6698
  return 0;
1,582,126✔
6699
}
6700

6701
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value) {
53,541,072✔
6702
  int32_t code;
6703

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

6708
  if (IS_VAR_DATA_TYPE(valCol->type)) {
53,541,912✔
6709
    int32_t *offsets = (int32_t *)tBufferGetData(&valCol->offsets);
1,661,332✔
6710
    int32_t  nextOffset = (idx == valCol->numOfValues - 1) ? tBufferGetSize(&valCol->data) : offsets[idx + 1];
1,661,619✔
6711
    int32_t  oldDataSize = nextOffset - offsets[idx];
1,661,619✔
6712
    int32_t  bytesAdded = value->nData - oldDataSize;
1,661,619✔
6713

6714
    if (bytesAdded != 0) {
1,661,619✔
6715
      if ((code = tBufferEnsureCapacity(&valCol->data, tBufferGetSize(&valCol->data) + bytesAdded))) return code;
36,050✔
6716
      memmove(tBufferGetDataAt(&valCol->data, nextOffset + bytesAdded), tBufferGetDataAt(&valCol->data, nextOffset),
18,025✔
6717
              tBufferGetSize(&valCol->data) - nextOffset);
18,025✔
6718
      valCol->data.size += bytesAdded;
18,025✔
6719

6720
      for (int32_t i = idx + 1; i < valCol->numOfValues; i++) {
18,025✔
6721
        offsets[i] += bytesAdded;
×
6722
      }
6723
    }
6724
    return tBufferPutAt(&valCol->data, offsets[idx], value->pData, value->nData);
1,661,619✔
6725
  } else {
6726
    return tBufferPutAt(&valCol->data, idx * tDataTypes[valCol->type].bytes, VALUE_GET_DATUM(value, valCol->type),
51,880,580✔
6727
                        tDataTypes[valCol->type].bytes);
51,881,504✔
6728
  }
6729
  return 0;
6730
}
6731

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

6737
  value->type = valCol->type;
130,444,305✔
6738
  if (IS_VAR_DATA_TYPE(value->type)) {
139,812,917✔
6739
    int32_t       offset, nextOffset;
9,363,328✔
6740
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * sizeof(offset), &valCol->offsets);
9,365,697✔
6741

6742
    TAOS_CHECK_RETURN(tBufferGetI32(&reader, &offset));
9,365,697✔
6743
    if (idx == valCol->numOfValues - 1) {
9,366,320✔
6744
      nextOffset = tBufferGetSize(&valCol->data);
7,044,977✔
6745
    } else {
6746
      TAOS_CHECK_RETURN(tBufferGetI32(&reader, &nextOffset));
2,321,962✔
6747
    }
6748
    value->nData = nextOffset - offset;
9,366,939✔
6749
    value->pData = (uint8_t *)tBufferGetDataAt(&valCol->data, offset);
9,366,939✔
6750
  } else {
6751
    SBufferReader reader = BUFFER_READER_INITIALIZER(idx * tDataTypes[value->type].bytes, &valCol->data);
121,080,835✔
6752
    TAOS_CHECK_RETURN(tBufferGet(&reader, tDataTypes[value->type].bytes, VALUE_GET_DATUM(value, value->type)));
242,159,763✔
6753
  }
6754
  return 0;
130,445,860✔
6755
}
6756

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

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

6764
  (*info) = (SValueColumnCompressInfo){
778,996✔
6765
      .cmprAlg = info->cmprAlg,
778,996✔
6766
      .type = valCol->type,
778,996✔
6767
  };
6768

6769
  // offset
6770
  if (IS_VAR_DATA_TYPE(valCol->type)) {
778,996✔
6771
    SCompressInfo cinfo = {
285,658✔
6772
        .cmprAlg = info->cmprAlg,
285,658✔
6773
        .dataType = TSDB_DATA_TYPE_INT,
6774
        .originalSize = valCol->offsets.size,
285,658✔
6775
    };
6776

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

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

6784
  // data
6785
  SCompressInfo cinfo = {
778,996✔
6786
      .cmprAlg = info->cmprAlg,
1,557,992✔
6787
      .dataType = valCol->type,
777,758✔
6788
      .originalSize = valCol->data.size,
778,996✔
6789
  };
6790

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

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

6797
  return 0;
778,996✔
6798
}
6799

6800
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *info, SValueColumn *valCol,
13,847,435✔
6801
                               SBuffer *assist) {
6802
  int32_t code;
6803

6804
  tValueColumnClear(valCol);
13,847,435✔
6805
  valCol->type = info->type;
13,849,292✔
6806
  // offset
6807
  if (IS_VAR_DATA_TYPE(valCol->type)) {
17,390,511✔
6808
    valCol->numOfValues = info->offsetOriginalSize / tDataTypes[TSDB_DATA_TYPE_INT].bytes;
3,541,219✔
6809

6810
    SCompressInfo cinfo = {
3,541,219✔
6811
        .dataType = TSDB_DATA_TYPE_INT,
6812
        .cmprAlg = info->cmprAlg,
3,540,600✔
6813
        .originalSize = info->offsetOriginalSize,
3,540,600✔
6814
        .compressedSize = info->offsetCompressedSize,
3,541,219✔
6815
    };
6816

6817
    code = tDecompressDataToBuffer(input, &cinfo, &valCol->offsets, assist);
3,540,600✔
6818
    if (code) {
3,541,838✔
6819
      return code;
×
6820
    }
6821
  } else {
6822
    valCol->numOfValues = info->dataOriginalSize / tDataTypes[valCol->type].bytes;
10,306,835✔
6823
  }
6824

6825
  // data
6826
  SCompressInfo cinfo = {
13,849,292✔
6827
      .dataType = valCol->type,
13,849,911✔
6828
      .cmprAlg = info->cmprAlg,
13,849,292✔
6829
      .originalSize = info->dataOriginalSize,
13,848,673✔
6830
      .compressedSize = info->dataCompressedSize,
13,848,054✔
6831
  };
6832

6833
  code = tDecompressDataToBuffer((char *)input + info->offsetCompressedSize, &cinfo, &valCol->data, assist);
13,849,292✔
6834
  if (code) {
13,850,530✔
6835
    return code;
×
6836
  }
6837

6838
  return 0;
13,850,530✔
6839
}
6840

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

6845
  if ((code = tBufferPutU8(buffer, fmtVer))) return code;
1,557,992✔
6846
  if ((code = tBufferPutI8(buffer, info->cmprAlg))) return code;
1,557,992✔
6847
  if ((code = tBufferPutI8(buffer, info->type))) return code;
1,557,992✔
6848
  if (IS_VAR_DATA_TYPE(info->type)) {
778,996✔
6849
    if ((code = tBufferPutI32v(buffer, info->offsetOriginalSize))) return code;
571,316✔
6850
    if ((code = tBufferPutI32v(buffer, info->offsetCompressedSize))) return code;
571,316✔
6851
  }
6852
  if ((code = tBufferPutI32v(buffer, info->dataOriginalSize))) return code;
1,557,992✔
6853
  if ((code = tBufferPutI32v(buffer, info->dataCompressedSize))) return code;
1,557,992✔
6854

6855
  return 0;
778,996✔
6856
}
6857

6858
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *info) {
13,848,673✔
6859
  int32_t code;
6860
  uint8_t fmtVer;
13,848,673✔
6861

6862
  if ((code = tBufferGetU8(reader, &fmtVer))) return code;
13,848,673✔
6863
  if (fmtVer == 0) {
13,849,292✔
6864
    if ((code = tBufferGetI8(reader, &info->cmprAlg))) return code;
13,849,292✔
6865
    if ((code = tBufferGetI8(reader, &info->type))) return code;
13,849,911✔
6866
    if (IS_VAR_DATA_TYPE(info->type)) {
13,850,530✔
6867
      if ((code = tBufferGetI32v(reader, &info->offsetOriginalSize))) return code;
3,543,076✔
6868
      if ((code = tBufferGetI32v(reader, &info->offsetCompressedSize))) return code;
3,540,600✔
6869
    } else {
6870
      info->offsetOriginalSize = 0;
10,306,835✔
6871
      info->offsetCompressedSize = 0;
10,308,073✔
6872
    }
6873
    if ((code = tBufferGetI32v(reader, &info->dataOriginalSize))) return code;
13,849,292✔
6874
    if ((code = tBufferGetI32v(reader, &info->dataCompressedSize))) return code;
13,848,054✔
6875
  } else {
6876
    return TSDB_CODE_INVALID_PARA;
×
6877
  }
6878

6879
  return 0;
13,849,292✔
6880
}
6881

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

6891
  extraSizeNeeded = (info->cmprAlg == NO_COMPRESSION) ? info->originalSize : info->originalSize + COMP_OVERFLOW_BYTES;
737,882,603✔
6892
  if (!(outputSize >= extraSizeNeeded)) {
737,910,935✔
6893
    return TSDB_CODE_INVALID_PARA;
×
6894
  }
6895

6896
  if (info->cmprAlg == NO_COMPRESSION) {
737,910,935✔
6897
    (void)memcpy(output, input, info->originalSize);
34,464✔
6898
    info->compressedSize = info->originalSize;
34,464✔
6899
  } else if (info->cmprAlg == ONE_STAGE_COMP || info->cmprAlg == TWO_STAGE_COMP) {
844,027,581✔
6900
    SBuffer local;
106,058,124✔
6901

6902
    tBufferInit(&local);
6903
    if (buffer == NULL) {
106,122,343✔
6904
      buffer = &local;
×
6905
    }
6906

6907
    if (info->cmprAlg == TWO_STAGE_COMP) {
106,122,343✔
6908
      code = tBufferEnsureCapacity(buffer, extraSizeNeeded);
106,019,456✔
6909
      if (code) {
106,011,322✔
6910
        tBufferDestroy(&local);
6911
        return code;
×
6912
      }
6913
    }
6914

6915
    info->compressedSize = tDataTypes[info->dataType].compFunc(  //
318,344,756✔
6916
        input,                                                   // input
6917
        info->originalSize,                                      // input size
6918
        info->originalSize / tDataTypes[info->dataType].bytes,   // number of elements
106,123,677✔
6919
        output,                                                  // output
6920
        outputSize,                                              // output size
6921
        info->cmprAlg,                                           // compression algorithm
106,124,035✔
6922
        buffer->data,                                            // buffer
6923
        buffer->capacity                                         // buffer size
106,115,837✔
6924
    );
6925
    if (info->compressedSize < 0) {
106,129,220✔
6926
      tBufferDestroy(&local);
6927
      return TSDB_CODE_COMPRESS_ERROR;
×
6928
    }
6929

6930
    tBufferDestroy(&local);
6931
  } else {
6932
    DEFINE_VAR(info->cmprAlg)
631,791,067✔
6933
    if ((l1 == L1_UNKNOWN && l2 == L2_UNKNOWN) || (l1 == L1_DISABLED && l2 == L2_DISABLED)) {
631,804,827✔
6934
      (void)memcpy(output, input, info->originalSize);
3,210✔
6935
      info->compressedSize = info->originalSize;
3,210✔
6936
      return 0;
3,210✔
6937
    }
6938
    SBuffer local;
631,772,711✔
6939

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

6946
    info->compressedSize = tDataCompress[info->dataType].compFunc(  //
1,895,163,840✔
6947
        input,                                                      // input
6948
        info->originalSize,                                         // input size
6949
        info->originalSize / tDataTypes[info->dataType].bytes,      // number of elements
631,757,740✔
6950
        output,                                                     // output
6951
        outputSize,                                                 // output size
6952
        info->cmprAlg,                                              // compression algorithm
6953
        buffer->data,                                               // buffer
6954
        buffer->capacity                                            // buffer size
631,730,085✔
6955
    );
6956
    if (info->compressedSize < 0) {
631,780,728✔
6957
      tBufferDestroy(&local);
6958
      return TSDB_CODE_COMPRESS_ERROR;
×
6959
    }
6960

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

6965
  return 0;
737,868,534✔
6966
}
6967

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

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

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

6988
    tBufferInit(&local);
6989
    if (buffer == NULL) {
1,147,006,895✔
6990
      buffer = &local;
×
6991
    }
6992

6993
    if (info->cmprAlg == TWO_STAGE_COMP) {
1,147,006,895✔
6994
      code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,146,457,317✔
6995
      if (code) {
1,146,470,310✔
6996
        tBufferDestroy(&local);
6997
        return code;
×
6998
      }
6999
    }
7000

7001
    int32_t decompressedSize = tDataTypes[info->dataType].decompFunc(
2,147,483,647✔
7002
        input,                                                  // input
7003
        info->compressedSize,                                   // inputSize
1,146,999,644✔
7004
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,147,006,877✔
7005
        output,                                                 // output
7006
        outputSize,                                             // output size
7007
        info->cmprAlg,                                          // compression algorithm
1,147,040,313✔
7008
        buffer->data,                                           // helper buffer
7009
        buffer->capacity                                        // extra buffer size
1,146,960,723✔
7010
    );
7011
    if (decompressedSize < 0) {
1,146,762,813✔
7012
      tBufferDestroy(&local);
7013
      return TSDB_CODE_COMPRESS_ERROR;
×
7014
    }
7015

7016
    if (!(decompressedSize == info->originalSize)) {
1,146,762,813✔
7017
      return TSDB_CODE_COMPRESS_ERROR;
×
7018
    }
7019
    tBufferDestroy(&local);
7020
  } else {
7021
    DEFINE_VAR(info->cmprAlg);
1,223,775,405✔
7022
    if (l1 == L1_DISABLED && l2 == L2_DISABLED) {
1,223,881,072✔
7023
      (void)memcpy(output, input, info->compressedSize);
×
7024
      return 0;
×
7025
    }
7026
    SBuffer local;
1,223,872,612✔
7027

7028
    tBufferInit(&local);
7029
    if (buffer == NULL) {
1,223,748,378✔
7030
      buffer = &local;
×
7031
    }
7032
    code = tBufferEnsureCapacity(buffer, info->originalSize + COMP_OVERFLOW_BYTES);
1,223,748,378✔
7033
    if (code) {
1,223,686,130✔
7034
      return code;
×
7035
    }
7036

7037
    int32_t decompressedSize = tDataCompress[info->dataType].decompFunc(
2,147,483,647✔
7038
        input,                                                  // input
7039
        info->compressedSize,                                   // inputSize
1,223,815,605✔
7040
        info->originalSize / tDataTypes[info->dataType].bytes,  // number of elements
1,223,772,189✔
7041
        output,                                                 // output
7042
        outputSize,                                             // output size
7043
        info->cmprAlg,                                          // compression algorithm
1,223,809,892✔
7044
        buffer->data,                                           // helper buffer
7045
        buffer->capacity                                        // extra buffer size
1,223,729,924✔
7046
    );
7047
    if (decompressedSize < 0) {
1,223,338,375✔
7048
      tBufferDestroy(&local);
7049
      return TSDB_CODE_COMPRESS_ERROR;
×
7050
    }
7051

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

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

7061
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
737,813,833✔
7062
  int32_t code;
7063

7064
  code = tBufferEnsureCapacity(output, output->size + info->originalSize + COMP_OVERFLOW_BYTES);
737,813,833✔
7065
  if (code) return code;
737,712,895✔
7066

7067
  code = tCompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
737,712,895✔
7068
  if (code) return code;
737,840,148✔
7069

7070
  output->size += info->compressedSize;
737,840,148✔
7071
  return 0;
737,889,742✔
7072
}
7073

7074
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist) {
1,146,697,837✔
7075
  int32_t code;
7076

7077
  code = tBufferEnsureCapacity(output, output->size + info->originalSize);
1,146,697,837✔
7078
  if (code) return code;
1,146,043,606✔
7079

7080
  code = tDecompressData(input, info, tBufferGetDataEnd(output), output->capacity - output->size, assist);
1,146,043,606✔
7081
  if (code) return code;
1,146,782,056✔
7082

7083
  output->size += info->originalSize;
1,146,782,056✔
7084
  return 0;
1,146,808,198✔
7085
}
7086

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

7112
void valueCloneDatum(SValue *pDst, const SValue *pSrc, int8_t type) {
160,817,514✔
7113
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
160,817,514✔
7114
    memcpy(pDst->pData, pSrc->pData, pSrc->nData);
5,774,809✔
7115
    pDst->nData = pSrc->nData;
5,773,938✔
7116
  } else {
7117
    pDst->val = pSrc->val;
155,042,705✔
7118
  }
7119
}
160,819,161✔
7120
void valueClearDatum(SValue *pVal, int8_t type) {
×
7121
  if (IS_VAR_DATA_TYPE(type) || type == TSDB_DATA_TYPE_DECIMAL) {
×
7122
    taosMemoryFreeClear(pVal->pData);
×
7123
    pVal->nData = 0;
×
7124
  } else {
7125
    pVal->val = 0;
×
7126
  }
7127
}
×
7128

7129
int8_t schemaHasBlob(const STSchema *pSchema) {
591,359,626✔
7130
  if (pSchema == NULL) {
591,359,626✔
7131
    return 0;
×
7132
  }
7133
  for (int i = 0; i < pSchema->numOfCols; ++i) {
2,147,483,647✔
7134
    if (IS_STR_DATA_BLOB(pSchema->columns[i].type)) {
2,147,483,647✔
7135
      return 1;
30,273✔
7136
    }
7137
  }
7138
  return 0;
591,334,846✔
7139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc