tdataformat.c 29.5 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
S
common  
Shengliang Guan 已提交
15 16

#define _DEFAULT_SOURCE
S
slguan 已提交
17
#include "tdataformat.h"
S
Shengliang Guan 已提交
18
#include "tcoding.h"
L
Liu Jicong 已提交
19
#include "tdatablock.h"
S
log  
Shengliang Guan 已提交
20
#include "tlog.h"
H
more  
hzcheng 已提交
21

22
static void dataColSetNEleNull(SDataCol *pCol, int nEle);
C
Cary Xu 已提交
23
#if 0
H
TD-1438  
Hongze Cheng 已提交
24
static void tdMergeTwoDataCols(SDataCols *target, SDataCols *src1, int *iter1, int limit1, SDataCols *src2, int *iter2,
25
                               int limit2, int tRows, bool forceSetNull);
C
Cary Xu 已提交
26
#endif
L
Liu Jicong 已提交
27
int tdAllocMemForCol(SDataCol *pCol, int maxPoints) {
L
Liu Jicong 已提交
28
  int spaceNeeded = pCol->bytes * maxPoints;
S
Shengliang Guan 已提交
29
  if (IS_VAR_DATA_TYPE(pCol->type)) {
L
Liu Jicong 已提交
30
    spaceNeeded += sizeof(VarDataOffsetT) * maxPoints;
L
Liu Jicong 已提交
31
  }
C
Cary Xu 已提交
32
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
33 34 35 36
  int32_t nBitmapBytes = (int32_t)TD_BITMAP_BYTES(maxPoints);
  spaceNeeded += (int)nBitmapBytes;
  // TODO: Currently, the compression of bitmap parts is affiliated to the column data parts, thus allocate 1 more
  // TYPE_BYTES as to comprise complete TYPE_BYTES. Otherwise, invalid read/write would be triggered.
37 38
  // spaceNeeded += TYPE_BYTES[pCol->type]; // the bitmap part is append as a single part since 2022.04.03, thus remove
  // the additional space
C
Cary Xu 已提交
39
#endif
C
Cary Xu 已提交
40

S
Shengliang Guan 已提交
41
  if (pCol->spaceSize < spaceNeeded) {
wafwerar's avatar
wafwerar 已提交
42
    void *ptr = taosMemoryRealloc(pCol->pData, spaceNeeded);
S
Shengliang Guan 已提交
43 44
    if (ptr == NULL) {
      uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)spaceNeeded, strerror(errno));
L
Liu Jicong 已提交
45
      return -1;
L
Liu Jicong 已提交
46 47 48
    } else {
      pCol->pData = ptr;
      pCol->spaceSize = spaceNeeded;
49 50
    }
  }
C
Cary Xu 已提交
51
#ifdef TD_SUPPORT_BITMAP
52

C
Cary Xu 已提交
53 54 55 56
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->pBitmap = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
    pCol->dataOff = POINTER_SHIFT(pCol->pBitmap, nBitmapBytes);
  } else {
C
Cary Xu 已提交
57
    pCol->pBitmap = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
L
Liu Jicong 已提交
58
  }
C
Cary Xu 已提交
59 60 61 62
#else
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->dataOff = POINTER_SHIFT(pCol->pData, pCol->bytes * maxPoints);
  }
C
Cary Xu 已提交
63
#endif
L
Liu Jicong 已提交
64
  return 0;
65 66
}

H
hzcheng 已提交
67 68 69
/**
 * Duplicate the schema and return a new object
 */
H
Hongze Cheng 已提交
70
STSchema *tdDupSchema(const STSchema *pSchema) {
S
Shengliang Guan 已提交
71
  int       tlen = sizeof(STSchema) + sizeof(STColumn) * schemaNCols(pSchema);
wafwerar's avatar
wafwerar 已提交
72
  STSchema *tSchema = (STSchema *)taosMemoryMalloc(tlen);
H
hzcheng 已提交
73 74
  if (tSchema == NULL) return NULL;

H
Hongze Cheng 已提交
75
  memcpy((void *)tSchema, (void *)pSchema, tlen);
H
hzcheng 已提交
76 77 78 79

  return tSchema;
}

H
TD-27  
hzcheng 已提交
80 81 82
/**
 * Encode a schema to dst, and return the next pointer
 */
H
TD-353  
Hongze Cheng 已提交
83 84 85 86
int tdEncodeSchema(void **buf, STSchema *pSchema) {
  int tlen = 0;
  tlen += taosEncodeFixedI32(buf, schemaVersion(pSchema));
  tlen += taosEncodeFixedI32(buf, schemaNCols(pSchema));
H
TD-166  
hzcheng 已提交
87

H
TD-27  
hzcheng 已提交
88 89
  for (int i = 0; i < schemaNCols(pSchema); i++) {
    STColumn *pCol = schemaColAt(pSchema, i);
H
TD-353  
Hongze Cheng 已提交
90
    tlen += taosEncodeFixedI8(buf, colType(pCol));
C
Cary Xu 已提交
91
    tlen += taosEncodeFixedI8(buf, colFlags(pCol));
H
TD-353  
Hongze Cheng 已提交
92
    tlen += taosEncodeFixedI16(buf, colColId(pCol));
93
    tlen += taosEncodeFixedI16(buf, colBytes(pCol));
H
TD-27  
hzcheng 已提交
94 95
  }

H
TD-353  
Hongze Cheng 已提交
96
  return tlen;
H
TD-27  
hzcheng 已提交
97 98 99 100 101
}

/**
 * Decode a schema from a binary.
 */
H
TD-353  
Hongze Cheng 已提交
102
void *tdDecodeSchema(void *buf, STSchema **pRSchema) {
S
Shengliang Guan 已提交
103 104
  int             version = 0;
  int             numOfCols = 0;
H
TD-353  
Hongze Cheng 已提交
105
  STSchemaBuilder schemaBuilder;
H
TD-27  
hzcheng 已提交
106

H
TD-353  
Hongze Cheng 已提交
107 108
  buf = taosDecodeFixedI32(buf, &version);
  buf = taosDecodeFixedI32(buf, &numOfCols);
H
TD-27  
hzcheng 已提交
109

H
Hongze Cheng 已提交
110 111
  if (tdInitTSchemaBuilder(&schemaBuilder, version) < 0) return NULL;

H
TD-353  
Hongze Cheng 已提交
112
  for (int i = 0; i < numOfCols; i++) {
113
    col_type_t  type = 0;
C
Cary Xu 已提交
114
    int8_t      flags = 0;
115 116
    col_id_t    colId = 0;
    col_bytes_t bytes = 0;
H
TD-353  
Hongze Cheng 已提交
117
    buf = taosDecodeFixedI8(buf, &type);
C
Cary Xu 已提交
118
    buf = taosDecodeFixedI8(buf, &flags);
H
TD-353  
Hongze Cheng 已提交
119
    buf = taosDecodeFixedI16(buf, &colId);
120
    buf = taosDecodeFixedI32(buf, &bytes);
C
Cary Xu 已提交
121
    if (tdAddColToSchema(&schemaBuilder, type, flags, colId, bytes) < 0) {
H
Hongze Cheng 已提交
122 123 124
      tdDestroyTSchemaBuilder(&schemaBuilder);
      return NULL;
    }
H
TD-27  
hzcheng 已提交
125 126
  }

H
TD-353  
Hongze Cheng 已提交
127
  *pRSchema = tdGetSchemaFromBuilder(&schemaBuilder);
H
Hongze Cheng 已提交
128
  tdDestroyTSchemaBuilder(&schemaBuilder);
H
TD-353  
Hongze Cheng 已提交
129
  return buf;
H
Hongze Cheng 已提交
130 131
}

L
Liu Jicong 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
#if 0
int32_t tEncodeSTColumn(SCoder *pEncoder, const STColumn *pCol) {
  if (tEncodeI16(pEncoder, pCol->colId) < 0) return -1;
  if (tEncodeI8(pEncoder, pCol->type) < 0) return -1;
  if (tEncodeI8(pEncoder, pCol->sma) < 0) return -1;
  if (tEncodeI32(pEncoder, pCol->bytes) < 0) return -1;
  if (tEncodeI32(pEncoder, pCol->offset) < 0) return -1;
  return pEncoder->pos;
}

int32_t tDecodeSTColumn(SCoder *pDecoder, STColumn *pCol) {
  if (tDecodeI16(pDecoder, &pCol->colId) < 0) return -1;
  if (tDecodeI8(pDecoder, &pCol->type) < 0) return -1;
  if (tDecodeI8(pDecoder, &pCol->sma) < 0) return -1;
  if (tDecodeI32(pDecoder, &pCol->bytes) < 0) return -1;
  if (tDecodeI32(pDecoder, &pCol->offset) < 0) return -1;
  return 0;
}

int32_t tEncodeSchema(SCoder *pEncoder, const STSchema *pSchema) {
  if (tEncodeI32(pEncoder, pSchema->numOfCols) < 0) return -1;
  if (tEncodeI16(pEncoder, pSchema->version) < 0) return -1;
  if (tEncodeU16(pEncoder, pSchema->flen) < 0) return -1;
  if (tEncodeI32(pEncoder, pSchema->vlen) < 0) return -1;
  if (tEncodeI32(pEncoder, pSchema->tlen) < 0) return -1;

  for (int32_t i = 0; i < schemaNCols(pSchema); i++) {
    const STColumn *pCol = schemaColAt(pSchema, i);
    if (tEncodeSTColumn(pEncoder, pCol) < 0) return -1;
  }
  return 0;
}

int32_t tDecodeSchema(SCoder *pDecoder, STSchema *pSchema) {
  if (tDecodeI32(pDecoder, &pSchema->numOfCols) < 0) return -1;
  if (tDecodeI16(pDecoder, &pSchema->version) < 0) return -1;
  if (tDecodeU16(pDecoder, &pSchema->flen) < 0) return -1;
  if (tDecodeI32(pDecoder, &pSchema->vlen) < 0) return -1;
  if (tDecodeI32(pDecoder, &pSchema->tlen) < 0) return -1;

  return 0;
}
#endif

C
Cary Xu 已提交
176
int tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
177 178 179
  if (pBuilder == NULL) return -1;

  pBuilder->tCols = 256;
wafwerar's avatar
wafwerar 已提交
180
  pBuilder->columns = (STColumn *)taosMemoryMalloc(sizeof(STColumn) * pBuilder->tCols);
H
Hongze Cheng 已提交
181 182 183 184 185 186 187 188
  if (pBuilder->columns == NULL) return -1;

  tdResetTSchemaBuilder(pBuilder, version);
  return 0;
}

void tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder) {
  if (pBuilder) {
wafwerar's avatar
wafwerar 已提交
189
    taosMemoryFreeClear(pBuilder->columns);
H
Hongze Cheng 已提交
190 191 192
  }
}

C
Cary Xu 已提交
193
void tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version) {
H
Hongze Cheng 已提交
194 195 196
  pBuilder->nCols = 0;
  pBuilder->tlen = 0;
  pBuilder->flen = 0;
T
Tao Liu 已提交
197
  pBuilder->vlen = 0;
H
Hongze Cheng 已提交
198 199 200
  pBuilder->version = version;
}

C
Cary Xu 已提交
201
int32_t tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t flags, col_id_t colId, col_bytes_t bytes) {
202
  if (!isValidDataType(type)) return -1;
H
Hongze Cheng 已提交
203 204 205

  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
wafwerar's avatar
wafwerar 已提交
206
    STColumn *columns = (STColumn *)taosMemoryRealloc(pBuilder->columns, sizeof(STColumn) * pBuilder->tCols);
T
tickduan 已提交
207 208
    if (columns == NULL) return -1;
    pBuilder->columns = columns;
H
Hongze Cheng 已提交
209 210 211 212 213
  }

  STColumn *pCol = &(pBuilder->columns[pBuilder->nCols]);
  colSetType(pCol, type);
  colSetColId(pCol, colId);
C
Cary Xu 已提交
214
  colSetFlags(pCol, flags);
H
Hongze Cheng 已提交
215 216 217
  if (pBuilder->nCols == 0) {
    colSetOffset(pCol, 0);
  } else {
S
Shengliang Guan 已提交
218
    STColumn *pTCol = &(pBuilder->columns[pBuilder->nCols - 1]);
H
Hongze Cheng 已提交
219 220 221 222 223
    colSetOffset(pCol, pTCol->offset + TYPE_BYTES[pTCol->type]);
  }

  if (IS_VAR_DATA_TYPE(type)) {
    colSetBytes(pCol, bytes);
T
Tao Liu 已提交
224 225
    pBuilder->tlen += (TYPE_BYTES[type] + bytes);
    pBuilder->vlen += bytes - sizeof(VarDataLenT);
H
Hongze Cheng 已提交
226 227 228
  } else {
    colSetBytes(pCol, TYPE_BYTES[type]);
    pBuilder->tlen += TYPE_BYTES[type];
T
Tao Liu 已提交
229
    pBuilder->vlen += TYPE_BYTES[type];
H
Hongze Cheng 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  }

  pBuilder->nCols++;
  pBuilder->flen += TYPE_BYTES[type];

  ASSERT(pCol->offset < pBuilder->flen);

  return 0;
}

STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder) {
  if (pBuilder->nCols <= 0) return NULL;

  int tlen = sizeof(STSchema) + sizeof(STColumn) * pBuilder->nCols;

wafwerar's avatar
wafwerar 已提交
245
  STSchema *pSchema = (STSchema *)taosMemoryMalloc(tlen);
H
Hongze Cheng 已提交
246 247 248 249 250 251
  if (pSchema == NULL) return NULL;

  schemaVersion(pSchema) = pBuilder->version;
  schemaNCols(pSchema) = pBuilder->nCols;
  schemaTLen(pSchema) = pBuilder->tlen;
  schemaFLen(pSchema) = pBuilder->flen;
T
Tao Liu 已提交
252
  schemaVLen(pSchema) = pBuilder->vlen;
H
Hongze Cheng 已提交
253

C
Cary Xu 已提交
254
#ifdef TD_SUPPORT_BITMAP
C
Cary Xu 已提交
255
  schemaTLen(pSchema) += (int)TD_BITMAP_BYTES(schemaNCols(pSchema));
C
Cary Xu 已提交
256 257
#endif

H
Hongze Cheng 已提交
258 259
  memcpy(schemaColAt(pSchema, 0), pBuilder->columns, sizeof(STColumn) * pBuilder->nCols);

H
TD-27  
hzcheng 已提交
260 261 262
  return pSchema;
}

C
Cary Xu 已提交
263
#if 0
H
hzcheng 已提交
264 265 266
/**
 * Initialize a data row
 */
H
TD-90  
Hongze Cheng 已提交
267 268 269 270
void tdInitDataRow(SDataRow row, STSchema *pSchema) {
  dataRowSetLen(row, TD_DATA_ROW_HEAD_SIZE + schemaFLen(pSchema));
  dataRowSetVersion(row, schemaVersion(pSchema));
}
H
hzcheng 已提交
271

C
Cary Xu 已提交
272 273
SDataRow tdNewDataRowFromSchema(STSchema *pSchema) {
  int32_t size = dataRowMaxBytesFromSchema(pSchema);
H
hzcheng 已提交
274

wafwerar's avatar
wafwerar 已提交
275
  SDataRow row = taosMemoryMalloc(size);
C
Cary Xu 已提交
276
  if (row == NULL) return NULL;
H
hzcheng 已提交
277

C
Cary Xu 已提交
278 279 280
  tdInitDataRow(row, pSchema);
  return row;
}
H
hzcheng 已提交
281

H
hzcheng 已提交
282 283 284
/**
 * Free the SDataRow object
 */
C
Cary Xu 已提交
285
void tdFreeDataRow(SDataRow row) {
wafwerar's avatar
wafwerar 已提交
286
  if (row) taosMemoryFree(row);
C
Cary Xu 已提交
287
}
C
Cary Xu 已提交
288

C
Cary Xu 已提交
289
SDataRow tdDataRowDup(SDataRow row) {
wafwerar's avatar
wafwerar 已提交
290
  SDataRow trow = taosMemoryMalloc(dataRowLen(row));
C
Cary Xu 已提交
291
  if (trow == NULL) return NULL;
H
hzcheng 已提交
292

C
Cary Xu 已提交
293 294 295
  dataRowCpy(trow, row);
  return trow;
}
C
Cary Xu 已提交
296 297

SMemRow tdMemRowDup(SMemRow row) {
wafwerar's avatar
wafwerar 已提交
298
  SMemRow trow = taosMemoryMalloc(memRowTLen(row));
H
hzcheng 已提交
299 300
  if (trow == NULL) return NULL;

C
Cary Xu 已提交
301
  memRowCpy(trow, row);
H
hzcheng 已提交
302
  return trow;
H
hzcheng 已提交
303
}
C
Cary Xu 已提交
304
#endif
C
Cary Xu 已提交
305

306
void dataColInit(SDataCol *pDataCol, STColumn *pCol, int maxPoints) {
H
TD-166  
hzcheng 已提交
307 308 309
  pDataCol->type = colType(pCol);
  pDataCol->colId = colColId(pCol);
  pDataCol->bytes = colBytes(pCol);
S
Shengliang Guan 已提交
310
  pDataCol->offset = colOffset(pCol) + 0;  // TD_DATA_ROW_HEAD_SIZE;
H
TD-166  
hzcheng 已提交
311 312 313

  pDataCol->len = 0;
}
C
Cary Xu 已提交
314 315

#if 0
H
Hongze Cheng 已提交
316
// value from timestamp should be TKEY here instead of TSKEY
L
Liu Jicong 已提交
317
int dataColAppendVal(SDataCol *pCol, const void *value, int numOfRows, int maxPoints) {
H
TD-166  
hzcheng 已提交
318 319
  ASSERT(pCol != NULL && value != NULL);

K
kailixu 已提交
320 321 322
  if (isAllRowsNull(pCol)) {
    if (isNull(value, pCol->type)) {
      // all null value yet, just return
L
Liu Jicong 已提交
323
      return 0;
K
kailixu 已提交
324 325
    }

S
Shengliang Guan 已提交
326
    if (tdAllocMemForCol(pCol, maxPoints) < 0) return -1;
K
kailixu 已提交
327 328
    if (numOfRows > 0) {
      // Find the first not null value, fill all previouse values as NULL
L
Liu Jicong 已提交
329
      dataColSetNEleNull(pCol, numOfRows);
K
kailixu 已提交
330 331 332
    }
  }

H
Hongze Cheng 已提交
333 334 335 336 337 338 339
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    // set offset
    pCol->dataOff[numOfRows] = pCol->len;
    // Copy data
    memcpy(POINTER_SHIFT(pCol->pData, pCol->len), value, varDataTLen(value));
    // Update the length
    pCol->len += varDataTLen(value);
H
TD-166  
hzcheng 已提交
340
  } else {
H
Haojun Liao 已提交
341
    ASSERT(pCol->len == TYPE_BYTES[pCol->type] * numOfRows);
H
Hongze Cheng 已提交
342 343
    memcpy(POINTER_SHIFT(pCol->pData, pCol->len), value, pCol->bytes);
    pCol->len += pCol->bytes;
H
TD-166  
hzcheng 已提交
344
  }
L
Liu Jicong 已提交
345 346
  return 0;
}
C
Cary Xu 已提交
347
#endif
L
Liu Jicong 已提交
348 349 350 351 352 353
static FORCE_INLINE const void *tdGetColDataOfRowUnsafe(SDataCol *pCol, int row) {
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    return POINTER_SHIFT(pCol->pData, pCol->dataOff[row]);
  } else {
    return POINTER_SHIFT(pCol->pData, TYPE_BYTES[pCol->type] * row);
  }
H
TD-166  
hzcheng 已提交
354 355
}

H
TD-166  
hzcheng 已提交
356
bool isNEleNull(SDataCol *pCol, int nEle) {
S
Shengliang Guan 已提交
357
  if (isAllRowsNull(pCol)) return true;
358
  for (int i = 0; i < nEle; ++i) {
L
Liu Jicong 已提交
359
    if (!isNull(tdGetColDataOfRowUnsafe(pCol, i), pCol->type)) return false;
H
TD-166  
hzcheng 已提交
360
  }
H
Hongze Cheng 已提交
361
  return true;
H
TD-166  
hzcheng 已提交
362 363
}

C
Cary Xu 已提交
364
#if 0
365
static FORCE_INLINE void dataColSetNullAt(SDataCol *pCol, int index) {
H
TD-90  
Hongze Cheng 已提交
366 367 368
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->dataOff[index] = pCol->len;
    char *ptr = POINTER_SHIFT(pCol->pData, pCol->len);
369
    setVardataNull(ptr, pCol->type);
H
TD-90  
Hongze Cheng 已提交
370 371 372 373 374 375 376
    pCol->len += varDataTLen(ptr);
  } else {
    setNull(POINTER_SHIFT(pCol->pData, TYPE_BYTES[pCol->type] * index), pCol->type, pCol->bytes);
    pCol->len += TYPE_BYTES[pCol->type];
  }
}

C
Cary Xu 已提交
377
static void dataColSetNEleNull(SDataCol *pCol, int nEle, int8_t bitmapMode) {
H
TD-90  
Hongze Cheng 已提交
378 379
  if (IS_VAR_DATA_TYPE(pCol->type)) {
    pCol->len = 0;
380
    for (int i = 0; i < nEle; ++i) {
H
TD-90  
Hongze Cheng 已提交
381 382 383 384 385
      dataColSetNullAt(pCol, i);
    }
  } else {
    setNullN(pCol->pData, pCol->type, pCol->bytes, nEle);
    pCol->len = TYPE_BYTES[pCol->type] * nEle;
H
TD-166  
hzcheng 已提交
386 387
  }
}
C
Cary Xu 已提交
388
#endif
C
Cary Xu 已提交
389
void *dataColSetOffset(SDataCol *pCol, int nEle) {
H
TD-166  
hzcheng 已提交
390 391
  ASSERT(((pCol->type == TSDB_DATA_TYPE_BINARY) || (pCol->type == TSDB_DATA_TYPE_NCHAR)));

H
Hongze Cheng 已提交
392
  void *tptr = pCol->pData;
H
TD-166  
hzcheng 已提交
393
  // char *tptr = (char *)(pCol->pData);
H
TD-166  
hzcheng 已提交
394

H
TD-166  
hzcheng 已提交
395
  VarDataOffsetT offset = 0;
396
  for (int i = 0; i < nEle; ++i) {
H
TD-166  
hzcheng 已提交
397
    pCol->dataOff[i] = offset;
H
TD-166  
hzcheng 已提交
398
    offset += varDataTLen(tptr);
H
hzcheng 已提交
399
    tptr = POINTER_SHIFT(tptr, varDataTLen(tptr));
H
TD-166  
hzcheng 已提交
400
  }
C
Cary Xu 已提交
401
  return POINTER_SHIFT(tptr, varDataTLen(tptr));
H
TD-166  
hzcheng 已提交
402 403
}

L
Liu Jicong 已提交
404
SDataCols *tdNewDataCols(int maxCols, int maxRows) {
wafwerar's avatar
wafwerar 已提交
405
  SDataCols *pCols = (SDataCols *)taosMemoryCalloc(1, sizeof(SDataCols));
H
Haojun Liao 已提交
406
  if (pCols == NULL) {
S
Shengliang Guan 已提交
407
    uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)sizeof(SDataCols), strerror(errno));
H
Haojun Liao 已提交
408 409
    return NULL;
  }
H
TD-34  
hzcheng 已提交
410

H
Hongze Cheng 已提交
411
  pCols->maxPoints = maxRows;
L
Liu Jicong 已提交
412 413 414
  pCols->maxCols = maxCols;
  pCols->numOfRows = 0;
  pCols->numOfCols = 0;
C
Cary Xu 已提交
415
  // pCols->bitmapMode = 0; // calloc already set 0
H
Hongze Cheng 已提交
416 417

  if (maxCols > 0) {
wafwerar's avatar
wafwerar 已提交
418
    pCols->cols = (SDataCol *)taosMemoryCalloc(maxCols, sizeof(SDataCol));
H
Hongze Cheng 已提交
419 420 421 422 423 424
    if (pCols->cols == NULL) {
      uDebug("malloc failure, size:%" PRId64 " failed, reason:%s", (int64_t)sizeof(SDataCol) * maxCols,
             strerror(errno));
      tdFreeDataCols(pCols);
      return NULL;
    }
425
#if 0  // no need as calloc used
L
Liu Jicong 已提交
426
    int i;
S
Shengliang Guan 已提交
427
    for (i = 0; i < maxCols; i++) {
L
Liu Jicong 已提交
428
      pCols->cols[i].spaceSize = 0;
L
Liu Jicong 已提交
429
      pCols->cols[i].len = 0;
L
Liu Jicong 已提交
430 431 432
      pCols->cols[i].pData = NULL;
      pCols->cols[i].dataOff = NULL;
    }
433
#endif
H
Hongze Cheng 已提交
434 435
  }

H
TD-34  
hzcheng 已提交
436 437 438
  return pCols;
}

H
Hongze Cheng 已提交
439
int tdInitDataCols(SDataCols *pCols, STSchema *pSchema) {
440 441
  int i;
  int oldMaxCols = pCols->maxCols;
L
Liu Jicong 已提交
442
  if (schemaNCols(pSchema) > oldMaxCols) {
H
Hongze Cheng 已提交
443
    pCols->maxCols = schemaNCols(pSchema);
wafwerar's avatar
wafwerar 已提交
444
    void *ptr = (SDataCol *)taosMemoryRealloc(pCols->cols, sizeof(SDataCol) * pCols->maxCols);
L
Liu Jicong 已提交
445 446
    if (ptr == NULL) return -1;
    pCols->cols = ptr;
447
    for (i = oldMaxCols; i < pCols->maxCols; ++i) {
448 449
      pCols->cols[i].pData = NULL;
      pCols->cols[i].dataOff = NULL;
450
      pCols->cols[i].pBitmap = NULL;
L
Liu Jicong 已提交
451
      pCols->cols[i].spaceSize = 0;
452
    }
L
Liu Jicong 已提交
453
  }
454 455 456
#if 0
  tdResetDataCols(pCols); // redundant loop to reset len/blen to 0, already reset in following dataColInit(...)
#endif
H
Hongze Cheng 已提交
457

458
  pCols->numOfRows = 0;
C
Cary Xu 已提交
459
  pCols->bitmapMode = 0;
H
TD-34  
hzcheng 已提交
460 461
  pCols->numOfCols = schemaNCols(pSchema);

462
  for (i = 0; i < schemaNCols(pSchema); ++i) {
463
    dataColInit(pCols->cols + i, schemaColAt(pSchema, i), pCols->maxPoints);
H
TD-34  
hzcheng 已提交
464
  }
S
Shengliang Guan 已提交
465

H
Hongze Cheng 已提交
466
  return 0;
H
TD-34  
hzcheng 已提交
467 468
}

H
Hongze Cheng 已提交
469
SDataCols *tdFreeDataCols(SDataCols *pCols) {
470
  int i;
H
TD-34  
hzcheng 已提交
471
  if (pCols) {
S
Shengliang Guan 已提交
472
    if (pCols->cols) {
473
      int maxCols = pCols->maxCols;
474
      for (i = 0; i < maxCols; ++i) {
475
        SDataCol *pCol = &pCols->cols[i];
wafwerar's avatar
wafwerar 已提交
476
        taosMemoryFreeClear(pCol->pData);
477
      }
wafwerar's avatar
wafwerar 已提交
478
      taosMemoryFree(pCols->cols);
479 480
      pCols->cols = NULL;
    }
wafwerar's avatar
wafwerar 已提交
481
    taosMemoryFree(pCols);
H
TD-34  
hzcheng 已提交
482
  }
H
Hongze Cheng 已提交
483
  return NULL;
H
TD-34  
hzcheng 已提交
484 485
}

C
Cary Xu 已提交
486
#if 0
H
TD-100  
hzcheng 已提交
487
SDataCols *tdDupDataCols(SDataCols *pDataCols, bool keepData) {
L
Liu Jicong 已提交
488
  SDataCols *pRet = tdNewDataCols(pDataCols->maxCols, pDataCols->maxPoints);
H
TD-100  
hzcheng 已提交
489 490 491 492
  if (pRet == NULL) return NULL;

  pRet->numOfCols = pDataCols->numOfCols;
  pRet->sversion = pDataCols->sversion;
H
Haojun Liao 已提交
493
  if (keepData) pRet->numOfRows = pDataCols->numOfRows;
H
TD-100  
hzcheng 已提交
494 495 496

  for (int i = 0; i < pDataCols->numOfCols; i++) {
    pRet->cols[i].type = pDataCols->cols[i].type;
C
Cary Xu 已提交
497
    pRet->cols[i].bitmap = pDataCols->cols[i].bitmap;
H
TD-100  
hzcheng 已提交
498 499 500
    pRet->cols[i].colId = pDataCols->cols[i].colId;
    pRet->cols[i].bytes = pDataCols->cols[i].bytes;
    pRet->cols[i].offset = pDataCols->cols[i].offset;
H
TD-166  
hzcheng 已提交
501 502

    if (keepData) {
L
Liu Jicong 已提交
503
      if (pDataCols->cols[i].len > 0) {
S
Shengliang Guan 已提交
504
        if (tdAllocMemForCol(&pRet->cols[i], pRet->maxPoints) < 0) {
L
Liu Jicong 已提交
505 506 507
          tdFreeDataCols(pRet);
          return NULL;
        }
L
Liu Jicong 已提交
508
        pRet->cols[i].len = pDataCols->cols[i].len;
H
Hongze Cheng 已提交
509
        memcpy(pRet->cols[i].pData, pDataCols->cols[i].pData, pDataCols->cols[i].len);
H
Hongze Cheng 已提交
510
        if (IS_VAR_DATA_TYPE(pRet->cols[i].type)) {
L
Liu Jicong 已提交
511 512
          int dataOffSize = sizeof(VarDataOffsetT) * pDataCols->maxPoints;
          memcpy(pRet->cols[i].dataOff, pDataCols->cols[i].dataOff, dataOffSize);
H
Hongze Cheng 已提交
513
        }
H
TD-166  
hzcheng 已提交
514 515
      }
    }
H
TD-100  
hzcheng 已提交
516 517 518 519
  }

  return pRet;
}
C
Cary Xu 已提交
520
#endif
H
TD-100  
hzcheng 已提交
521

H
TD-34  
hzcheng 已提交
522
void tdResetDataCols(SDataCols *pCols) {
B
Bomin Zhang 已提交
523 524
  if (pCols != NULL) {
    pCols->numOfRows = 0;
C
Cary Xu 已提交
525
    pCols->bitmapMode = 0;
526
    for (int i = 0; i < pCols->maxCols; ++i) {
B
Bomin Zhang 已提交
527 528
      dataColReset(pCols->cols + i);
    }
H
TD-34  
hzcheng 已提交
529 530
  }
}
C
Cary Xu 已提交
531
#if 0
532
static void tdAppendDataRowToDataCol(SDataRow row, STSchema *pSchema, SDataCols *pCols, bool forceSetNull) {
H
TD-1548  
Hongze Cheng 已提交
533
  ASSERT(pCols->numOfRows == 0 || dataColsKeyLast(pCols) < dataRowKey(row));
H
TD-166  
hzcheng 已提交
534

C
Cary Xu 已提交
535
  int rcol = 0;
H
TD-90  
Hongze Cheng 已提交
536 537
  int dcol = 0;

538
  while (dcol < pCols->numOfCols) {
539
    bool setCol = 0;
540 541 542 543 544
    SDataCol *pDataCol = &(pCols->cols[dcol]);
    if (rcol >= schemaNCols(pSchema)) {
      dataColAppendVal(pDataCol, getNullValue(pDataCol->type), pCols->numOfRows, pCols->maxPoints);
      dcol++;
      continue;
H
TD-90  
Hongze Cheng 已提交
545
    }
H
TD-166  
hzcheng 已提交
546

547 548 549
    STColumn *pRowCol = schemaColAt(pSchema, rcol);
    if (pRowCol->colId == pDataCol->colId) {
      void *value = tdGetRowDataOfCol(row, pRowCol->type, pRowCol->offset + TD_DATA_ROW_HEAD_SIZE);
550
      if(!isNull(value, pDataCol->type)) setCol = 1;
551 552 553 554 555 556
      dataColAppendVal(pDataCol, value, pCols->numOfRows, pCols->maxPoints);
      dcol++;
      rcol++;
    } else if (pRowCol->colId < pDataCol->colId) {
      rcol++;
    } else {
557
      if(forceSetNull || setCol) {
558
        dataColAppendVal(pDataCol, getNullValue(pDataCol->type), pCols->numOfRows, pCols->maxPoints);
C
Cary Xu 已提交
559
      }
560
      dcol++;
C
Cary Xu 已提交
561 562 563 564 565
    }
  }
  pCols->numOfRows++;
}

C
Cary Xu 已提交
566
static void tdAppendKVRowToDataCol(SKVRow row, STSchema *pSchema, SDataCols *pCols, bool forceSetNull) {
C
Cary Xu 已提交
567 568 569 570 571
  ASSERT(pCols->numOfRows == 0 || dataColsKeyLast(pCols) < kvRowKey(row));

  int rcol = 0;
  int dcol = 0;

572 573 574
  int nRowCols = kvRowNCols(row);

  while (dcol < pCols->numOfCols) {
575
    bool setCol = 0;
576 577 578 579 580
    SDataCol *pDataCol = &(pCols->cols[dcol]);
    if (rcol >= nRowCols || rcol >= schemaNCols(pSchema)) {
      dataColAppendVal(pDataCol, getNullValue(pDataCol->type), pCols->numOfRows, pCols->maxPoints);
      ++dcol;
      continue;
C
Cary Xu 已提交
581 582
    }

583 584 585 586
    SColIdx *colIdx = kvRowColIdxAt(row, rcol);

    if (colIdx->colId == pDataCol->colId) {
      void *value = tdGetKvRowDataOfCol(row, colIdx->offset);
587
      if(!isNull(value, pDataCol->type)) setCol = 1;
588 589 590 591 592 593
      dataColAppendVal(pDataCol, value, pCols->numOfRows, pCols->maxPoints);
      ++dcol;
      ++rcol;
    } else if (colIdx->colId < pDataCol->colId) {
      ++rcol;
    } else {
594
      if(forceSetNull || setCol) {
C
Cary Xu 已提交
595
        dataColAppendVal(pDataCol, getNullValue(pDataCol->type), pCols->numOfRows, pCols->maxPoints);
H
TD-1548  
Hongze Cheng 已提交
596
      }
597
      ++dcol;
H
TD-90  
Hongze Cheng 已提交
598
    }
H
TD-34  
hzcheng 已提交
599
  }
H
Haojun Liao 已提交
600
  pCols->numOfRows++;
H
TD-34  
hzcheng 已提交
601
}
H
TD-166  
hzcheng 已提交
602

603
void tdAppendMemRowToDataCol(SMemRow row, STSchema *pSchema, SDataCols *pCols, bool forceSetNull) {
C
Cary Xu 已提交
604
  if (isDataRow(row)) {
605
    tdAppendDataRowToDataCol(memRowDataBody(row), pSchema, pCols, forceSetNull);
C
Cary Xu 已提交
606
  } else if (isKvRow(row)) {
C
Cary Xu 已提交
607
    tdAppendKVRowToDataCol(memRowKvBody(row), pSchema, pCols, forceSetNull);
C
Cary Xu 已提交
608 609 610 611 612
  } else {
    ASSERT(0);
  }
}

613
int tdMergeDataCols(SDataCols *target, SDataCols *source, int rowsToMerge, int *pOffset, bool forceSetNull) {
H
Haojun Liao 已提交
614
  ASSERT(rowsToMerge > 0 && rowsToMerge <= source->numOfRows);
H
TD-166  
hzcheng 已提交
615
  ASSERT(target->numOfCols == source->numOfCols);
H
Hongze Cheng 已提交
616 617 618 619 620
  int offset = 0;

  if (pOffset == NULL) {
    pOffset = &offset;
  }
H
TD-100  
hzcheng 已提交
621

H
TD-166  
hzcheng 已提交
622
  SDataCols *pTarget = NULL;
H
TD-100  
hzcheng 已提交
623

624
  if ((target->numOfRows == 0) || (dataColsKeyLast(target) < dataColsKeyAtRow(source, *pOffset))) {  // No overlap
H
Hongze Cheng 已提交
625
    ASSERT(target->numOfRows + rowsToMerge <= target->maxPoints);
H
TD-166  
hzcheng 已提交
626 627
    for (int i = 0; i < rowsToMerge; i++) {
      for (int j = 0; j < source->numOfCols; j++) {
628
        if (source->cols[j].len > 0 || target->cols[j].len > 0) {
H
Hongze Cheng 已提交
629
          dataColAppendVal(target->cols + j, tdGetColDataOfRow(source->cols + j, i + (*pOffset)), target->numOfRows,
H
Hongze Cheng 已提交
630 631
                           target->maxPoints);
        }
H
TD-166  
hzcheng 已提交
632
      }
H
Haojun Liao 已提交
633
      target->numOfRows++;
H
TD-166  
hzcheng 已提交
634
    }
L
lichuang 已提交
635
    (*pOffset) += rowsToMerge;
H
TD-166  
hzcheng 已提交
636 637 638 639 640
  } else {
    pTarget = tdDupDataCols(target, true);
    if (pTarget == NULL) goto _err;

    int iter1 = 0;
H
Hongze Cheng 已提交
641
    tdMergeTwoDataCols(target, pTarget, &iter1, pTarget->numOfRows, source, pOffset, source->numOfRows,
642
                       pTarget->numOfRows + rowsToMerge, forceSetNull);
H
TD-166  
hzcheng 已提交
643
  }
H
TD-100  
hzcheng 已提交
644 645 646 647 648 649 650 651

  tdFreeDataCols(pTarget);
  return 0;

_err:
  tdFreeDataCols(pTarget);
  return -1;
}
H
TD-100  
hzcheng 已提交
652

H
TD-1438  
Hongze Cheng 已提交
653 654
// src2 data has more priority than src1
static void tdMergeTwoDataCols(SDataCols *target, SDataCols *src1, int *iter1, int limit1, SDataCols *src2, int *iter2,
655
                               int limit2, int tRows, bool forceSetNull) {
H
TD-100  
hzcheng 已提交
656
  tdResetDataCols(target);
H
TD-521  
Hongze Cheng 已提交
657
  ASSERT(limit1 <= src1->numOfRows && limit2 <= src2->numOfRows);
H
TD-100  
hzcheng 已提交
658

H
Haojun Liao 已提交
659
  while (target->numOfRows < tRows) {
H
TD-521  
Hongze Cheng 已提交
660
    if (*iter1 >= limit1 && *iter2 >= limit2) break;
H
TD-100  
hzcheng 已提交
661

H
TD-1548  
Hongze Cheng 已提交
662 663 664 665 666 667
    TSKEY key1 = (*iter1 >= limit1) ? INT64_MAX : dataColsKeyAt(src1, *iter1);
    TKEY  tkey1 = (*iter1 >= limit1) ? TKEY_NULL : dataColsTKeyAt(src1, *iter1);
    TSKEY key2 = (*iter2 >= limit2) ? INT64_MAX : dataColsKeyAt(src2, *iter2);
    TKEY  tkey2 = (*iter2 >= limit2) ? TKEY_NULL : dataColsTKeyAt(src2, *iter2);

    ASSERT(tkey1 == TKEY_NULL || (!TKEY_IS_DELETED(tkey1)));
H
TD-100  
hzcheng 已提交
668

H
TD-1438  
Hongze Cheng 已提交
669
    if (key1 < key2) {
H
TD-100  
hzcheng 已提交
670 671
      for (int i = 0; i < src1->numOfCols; i++) {
        ASSERT(target->cols[i].type == src1->cols[i].type);
672
        if (src1->cols[i].len > 0 || target->cols[i].len > 0) {
H
Hongze Cheng 已提交
673 674 675
          dataColAppendVal(&(target->cols[i]), tdGetColDataOfRow(src1->cols + i, *iter1), target->numOfRows,
                           target->maxPoints);
        }
H
TD-100  
hzcheng 已提交
676 677
      }

H
Haojun Liao 已提交
678
      target->numOfRows++;
H
TD-100  
hzcheng 已提交
679
      (*iter1)++;
H
TD-1548  
Hongze Cheng 已提交
680 681 682 683
    } else if (key1 >= key2) {
      if ((key1 > key2) || (key1 == key2 && !TKEY_IS_DELETED(tkey2))) {
        for (int i = 0; i < src2->numOfCols; i++) {
          ASSERT(target->cols[i].type == src2->cols[i].type);
L
Liu Jicong 已提交
684
          if (src2->cols[i].len > 0 && !isNull(src2->cols[i].pData, src2->cols[i].type)) {
H
TD-1548  
Hongze Cheng 已提交
685 686
            dataColAppendVal(&(target->cols[i]), tdGetColDataOfRow(src2->cols + i, *iter2), target->numOfRows,
                             target->maxPoints);
L
Liu Jicong 已提交
687 688 689
          } else if(!forceSetNull && key1 == key2 && src1->cols[i].len > 0) {
            dataColAppendVal(&(target->cols[i]), tdGetColDataOfRow(src1->cols + i, *iter1), target->numOfRows,
                             target->maxPoints);
690 691
          } else if(target->cols[i].len > 0) {
            dataColSetNullAt(&target->cols[i], target->numOfRows);
H
TD-1548  
Hongze Cheng 已提交
692
          }
H
Hongze Cheng 已提交
693
        }
H
Hongze Cheng 已提交
694
        target->numOfRows++;
H
TD-100  
hzcheng 已提交
695
      }
H
TD-100  
hzcheng 已提交
696

H
TD-100  
hzcheng 已提交
697
      (*iter2)++;
H
TD-1438  
Hongze Cheng 已提交
698
      if (key1 == key2) (*iter1)++;
H
TD-100  
hzcheng 已提交
699
    }
H
Hongze Cheng 已提交
700 701

    ASSERT(target->numOfRows <= target->maxPoints);
H
TD-100  
hzcheng 已提交
702
  }
H
Hongze Cheng 已提交
703
}
C
Cary Xu 已提交
704
#endif
H
Hongze Cheng 已提交
705

H
Hongze Cheng 已提交
706
SKVRow tdKVRowDup(SKVRow row) {
wafwerar's avatar
wafwerar 已提交
707
  SKVRow trow = taosMemoryMalloc(kvRowLen(row));
H
Hongze Cheng 已提交
708 709
  if (trow == NULL) return NULL;

H
Hongze Cheng 已提交
710
  kvRowCpy(trow, row);
H
Hongze Cheng 已提交
711 712 713
  return trow;
}

S
Shengliang Guan 已提交
714 715 716
static int compareColIdx(const void *a, const void *b) {
  const SColIdx *x = (const SColIdx *)a;
  const SColIdx *y = (const SColIdx *)b;
B
Bomin Zhang 已提交
717 718 719 720 721 722 723 724 725
  if (x->colId > y->colId) {
    return 1;
  }
  if (x->colId < y->colId) {
    return -1;
  }
  return 0;
}

S
Shengliang Guan 已提交
726
void tdSortKVRowByColIdx(SKVRow row) { qsort(kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), compareColIdx); }
B
Bomin Zhang 已提交
727

H
TD-90  
Hongze Cheng 已提交
728 729 730 731
int tdSetKVRowDataOfCol(SKVRow *orow, int16_t colId, int8_t type, void *value) {
  SColIdx *pColIdx = NULL;
  SKVRow   row = *orow;
  SKVRow   nrow = NULL;
S
Shengliang Guan 已提交
732
  void    *ptr = taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_GE);
H
TD-90  
Hongze Cheng 已提交
733

734
  if (ptr == NULL || ((SColIdx *)ptr)->colId > colId) {  // need to add a column value to the row
C
Cary Xu 已提交
735
    int diff = IS_VAR_DATA_TYPE(type) ? varDataTLen(value) : TYPE_BYTES[type];
736 737 738 739
    int nRowLen = kvRowLen(row) + sizeof(SColIdx) + diff;
    int oRowCols = kvRowNCols(row);

    ASSERT(diff > 0);
wafwerar's avatar
wafwerar 已提交
740
    nrow = taosMemoryMalloc(nRowLen);
H
TD-90  
Hongze Cheng 已提交
741 742
    if (nrow == NULL) return -1;

743 744
    kvRowSetLen(nrow, nRowLen);
    kvRowSetNCols(nrow, oRowCols + 1);
H
TD-90  
Hongze Cheng 已提交
745

746 747
    memcpy(kvRowColIdx(nrow), kvRowColIdx(row), sizeof(SColIdx) * oRowCols);
    memcpy(kvRowValues(nrow), kvRowValues(row), kvRowValLen(row));
H
TD-90  
Hongze Cheng 已提交
748

749 750 751
    pColIdx = kvRowColIdxAt(nrow, oRowCols);
    pColIdx->colId = colId;
    pColIdx->offset = kvRowValLen(row);
H
TD-90  
Hongze Cheng 已提交
752

753
    memcpy(kvRowColVal(nrow, pColIdx), value, diff);  // copy new value
H
TD-90  
Hongze Cheng 已提交
754

755
    tdSortKVRowByColIdx(nrow);
H
TD-90  
Hongze Cheng 已提交
756 757

    *orow = nrow;
wafwerar's avatar
wafwerar 已提交
758
    taosMemoryFree(row);
H
TD-90  
Hongze Cheng 已提交
759 760 761 762 763
  } else {
    ASSERT(((SColIdx *)ptr)->colId == colId);
    if (IS_VAR_DATA_TYPE(type)) {
      void *pOldVal = kvRowColVal(row, (SColIdx *)ptr);

S
Shengliang Guan 已提交
764
      if (varDataTLen(value) == varDataTLen(pOldVal)) {  // just update the column value in place
H
TD-90  
Hongze Cheng 已提交
765
        memcpy(pOldVal, value, varDataTLen(value));
766 767
      } else {  // need to reallocate the memory
        int16_t nlen = kvRowLen(row) + (varDataTLen(value) - varDataTLen(pOldVal));
H
TD-90  
Hongze Cheng 已提交
768
        ASSERT(nlen > 0);
wafwerar's avatar
wafwerar 已提交
769
        nrow = taosMemoryMalloc(nlen);
H
TD-90  
Hongze Cheng 已提交
770
        if (nrow == NULL) return -1;
H
TD-90  
Hongze Cheng 已提交
771 772 773 774

        kvRowSetLen(nrow, nlen);
        kvRowSetNCols(nrow, kvRowNCols(row));

775 776 777 778 779 780 781 782
        int zsize = sizeof(SColIdx) * kvRowNCols(row) + ((SColIdx *)ptr)->offset;
        memcpy(kvRowColIdx(nrow), kvRowColIdx(row), zsize);
        memcpy(kvRowColVal(nrow, ((SColIdx *)ptr)), value, varDataTLen(value));
        // Copy left value part
        int lsize = kvRowLen(row) - TD_KV_ROW_HEAD_SIZE - zsize - varDataTLen(pOldVal);
        if (lsize > 0) {
          memcpy(POINTER_SHIFT(nrow, TD_KV_ROW_HEAD_SIZE + zsize + varDataTLen(value)),
                 POINTER_SHIFT(row, TD_KV_ROW_HEAD_SIZE + zsize + varDataTLen(pOldVal)), lsize);
H
TD-90  
Hongze Cheng 已提交
783 784
        }

785 786 787 788 789
        for (int i = 0; i < kvRowNCols(nrow); i++) {
          pColIdx = kvRowColIdxAt(nrow, i);

          if (pColIdx->offset > ((SColIdx *)ptr)->offset) {
            pColIdx->offset = pColIdx->offset - varDataTLen(pOldVal) + varDataTLen(value);
H
TD-90  
Hongze Cheng 已提交
790 791 792 793
          }
        }

        *orow = nrow;
wafwerar's avatar
wafwerar 已提交
794
        taosMemoryFree(row);
H
TD-90  
Hongze Cheng 已提交
795 796 797 798 799 800 801
      }
    } else {
      memcpy(kvRowColVal(row, (SColIdx *)ptr), value, TYPE_BYTES[type]);
    }
  }

  return 0;
H
Hongze Cheng 已提交
802 803
}

H
TD-353  
Hongze Cheng 已提交
804
int tdEncodeKVRow(void **buf, SKVRow row) {
H
Hongze Cheng 已提交
805
  // May change the encode purpose
H
TD-353  
Hongze Cheng 已提交
806 807 808 809 810 811
  if (buf != NULL) {
    kvRowCpy(*buf, row);
    *buf = POINTER_SHIFT(*buf, kvRowLen(row));
  }

  return kvRowLen(row);
H
Hongze Cheng 已提交
812 813
}

H
Hongze Cheng 已提交
814 815
void *tdDecodeKVRow(void *buf, SKVRow *row) {
  *row = tdKVRowDup(buf);
H
TD-353  
Hongze Cheng 已提交
816
  if (*row == NULL) return NULL;
H
Hongze Cheng 已提交
817
  return POINTER_SHIFT(buf, kvRowLen(*row));
H
Hongze Cheng 已提交
818 819
}

H
Hongze Cheng 已提交
820
int tdInitKVRowBuilder(SKVRowBuilder *pBuilder) {
H
Hongze Cheng 已提交
821 822
  pBuilder->tCols = 128;
  pBuilder->nCols = 0;
wafwerar's avatar
wafwerar 已提交
823
  pBuilder->pColIdx = (SColIdx *)taosMemoryMalloc(sizeof(SColIdx) * pBuilder->tCols);
H
Hongze Cheng 已提交
824 825 826
  if (pBuilder->pColIdx == NULL) return -1;
  pBuilder->alloc = 1024;
  pBuilder->size = 0;
wafwerar's avatar
wafwerar 已提交
827
  pBuilder->buf = taosMemoryMalloc(pBuilder->alloc);
H
Hongze Cheng 已提交
828
  if (pBuilder->buf == NULL) {
wafwerar's avatar
wafwerar 已提交
829
    taosMemoryFree(pBuilder->pColIdx);
H
Hongze Cheng 已提交
830 831 832 833 834
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
835
void tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder) {
wafwerar's avatar
wafwerar 已提交
836 837
  taosMemoryFreeClear(pBuilder->pColIdx);
  taosMemoryFreeClear(pBuilder->buf);
H
Hongze Cheng 已提交
838 839
}

H
Hongze Cheng 已提交
840
void tdResetKVRowBuilder(SKVRowBuilder *pBuilder) {
H
Hongze Cheng 已提交
841 842 843 844
  pBuilder->nCols = 0;
  pBuilder->size = 0;
}

H
Hongze Cheng 已提交
845
SKVRow tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder) {
C
Cary Xu 已提交
846
  int tlen = sizeof(SColIdx) * pBuilder->nCols + pBuilder->size;
H
Hongze Cheng 已提交
847 848
  if (tlen == 0) return NULL;

H
Hongze Cheng 已提交
849 850
  tlen += TD_KV_ROW_HEAD_SIZE;

wafwerar's avatar
wafwerar 已提交
851
  SKVRow row = taosMemoryMalloc(tlen);
H
Hongze Cheng 已提交
852 853
  if (row == NULL) return NULL;

H
Hongze Cheng 已提交
854
  kvRowSetNCols(row, pBuilder->nCols);
H
Hongze Cheng 已提交
855
  kvRowSetLen(row, tlen);
H
Hongze Cheng 已提交
856

H
Hongze Cheng 已提交
857 858
  memcpy(kvRowColIdx(row), pBuilder->pColIdx, sizeof(SColIdx) * pBuilder->nCols);
  memcpy(kvRowValues(row), pBuilder->buf, pBuilder->size);
H
Hongze Cheng 已提交
859 860

  return row;
861
}
C
Cary Xu 已提交
862
#if 0
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
SMemRow mergeTwoMemRows(void *buffer, SMemRow row1, SMemRow row2, STSchema *pSchema1, STSchema *pSchema2) {
#if 0
  ASSERT(memRowKey(row1) == memRowKey(row2));
  ASSERT(schemaVersion(pSchema1) == memRowVersion(row1));
  ASSERT(schemaVersion(pSchema2) == memRowVersion(row2));
  ASSERT(schemaVersion(pSchema1) >= schemaVersion(pSchema2));
#endif

  SArray *stashRow = taosArrayInit(pSchema1->numOfCols, sizeof(SColInfo));
  if (stashRow == NULL) {
    return NULL;
  }

  SMemRow  pRow = buffer;
  SDataRow dataRow = memRowDataBody(pRow);
  memRowSetType(pRow, SMEM_ROW_DATA);
  dataRowSetVersion(dataRow, schemaVersion(pSchema1));  // use latest schema version
  dataRowSetLen(dataRow, (TDRowLenT)(TD_DATA_ROW_HEAD_SIZE + pSchema1->flen));

C
Cary Xu 已提交
882
  TDRowLenT dataLen = 0, kvLen = TD_MEM_ROW_KV_HEAD_SIZE;
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947

  int32_t  i = 0;  // row1
  int32_t  j = 0;  // row2
  int32_t  nCols1 = schemaNCols(pSchema1);
  int32_t  nCols2 = schemaNCols(pSchema2);
  SColInfo colInfo = {0};
  int32_t  kvIdx1 = 0, kvIdx2 = 0;

  while (i < nCols1) {
    STColumn *pCol = schemaColAt(pSchema1, i);
    void *    val1 = tdGetMemRowDataOfColEx(row1, pCol->colId, pCol->type, TD_DATA_ROW_HEAD_SIZE + pCol->offset, &kvIdx1);
    // if val1 != NULL, use val1;
    if (val1 != NULL && !isNull(val1, pCol->type)) {
      tdAppendColVal(dataRow, val1, pCol->type, pCol->offset);
      kvLen += tdGetColAppendLen(SMEM_ROW_KV, val1, pCol->type);
      setSColInfo(&colInfo, pCol->colId, pCol->type, val1);
      taosArrayPush(stashRow, &colInfo);
      ++i;  // next col
      continue;
    }

    void *val2 = NULL;
    while (j < nCols2) {
      STColumn *tCol = schemaColAt(pSchema2, j);
      if (tCol->colId < pCol->colId) {
        ++j;
        continue;
      }
      if (tCol->colId == pCol->colId) {
        val2 = tdGetMemRowDataOfColEx(row2, tCol->colId, tCol->type, TD_DATA_ROW_HEAD_SIZE + tCol->offset, &kvIdx2);
      } else if (tCol->colId > pCol->colId) {
        // set NULL
      }
      break;
    }  // end of while(j<nCols2)
    if (val2 == NULL) {
      val2 = (void *)getNullValue(pCol->type);
    }
    tdAppendColVal(dataRow, val2, pCol->type, pCol->offset);
    if (!isNull(val2, pCol->type)) {
      kvLen += tdGetColAppendLen(SMEM_ROW_KV, val2, pCol->type);
      setSColInfo(&colInfo, pCol->colId, pCol->type, val2);
      taosArrayPush(stashRow, &colInfo);
    }

    ++i;  // next col
  }

  dataLen = memRowTLen(pRow);

  if (kvLen < dataLen) {
    // scan stashRow and generate SKVRow
    memset(buffer, 0, sizeof(dataLen));
    SMemRow tRow = buffer;
    memRowSetType(tRow, SMEM_ROW_KV);
    SKVRow kvRow = (SKVRow)memRowKvBody(tRow);
    int16_t nKvNCols = (int16_t) taosArrayGetSize(stashRow);
    kvRowSetLen(kvRow, (TDRowLenT)(TD_KV_ROW_HEAD_SIZE + sizeof(SColIdx) * nKvNCols));
    kvRowSetNCols(kvRow, nKvNCols);
    memRowSetKvVersion(tRow, pSchema1->version);

    int32_t toffset = 0;
    int16_t k;
    for (k = 0; k < nKvNCols; ++k) {
      SColInfo *pColInfo = taosArrayGet(stashRow, k);
948 949
      tdAppendKvColVal(kvRow, pColInfo->colVal, true, pColInfo->colId, pColInfo->colType, toffset);
      toffset += sizeof(SColIdx);
950 951 952 953 954 955
    }
    ASSERT(kvLen == memRowTLen(tRow));
  }
  taosArrayDestroy(stashRow);
  return buffer;
}
L
Liu Jicong 已提交
956
#endif