tdataformat.h 30.8 KB
Newer Older
H
more  
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
Shengliang Guan 已提交
15

16 17
#ifndef _TD_COMMON_DATA_FORMAT_H_
#define _TD_COMMON_DATA_FORMAT_H_
H
more  
Hongze Cheng 已提交
18

S
TD-4088  
Shengliang Guan 已提交
19
#include "os.h"
H
Hongze Cheng 已提交
20
#include "talgo.h"
H
Haojun Liao 已提交
21
#include "ttypes.h"
H
TD-166  
hzcheng 已提交
22
#include "tutil.h"
H
hzcheng 已提交
23

H
more  
hzcheng 已提交
24 25 26
#ifdef __cplusplus
extern "C" {
#endif
H
hzcheng 已提交
27

C
Cary Xu 已提交
28 29
// Imported since 3.0 and use bitmap to demonstrate None/Null/Norm, while use Null/Norm below 3.0 without of bitmap.
#define TD_SUPPORT_BITMAP
C
Cary Xu 已提交
30
#define TD_SUPPORT_READ2
K
Kaili Xu 已提交
31 32 33
#define TD_SUPPORT_BACK2  // suppport back compatibility of 2.0

#define TASSERT(x) ASSERT(x)
C
Cary Xu 已提交
34

35 36 37 38 39
#define STR_TO_VARSTR(x, str)                     \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = __len;                  \
    memcpy(varDataVal(x), (str), __len);          \
H
Hongze Cheng 已提交
40 41
  } while (0);

H
Hongze Cheng 已提交
42 43 44 45 46 47
#define STR_TO_NET_VARSTR(x, str)                 \
  do {                                            \
    VarDataLenT __len = (VarDataLenT)strlen(str); \
    *(VarDataLenT *)(x) = htons(__len);           \
    memcpy(varDataVal(x), (str), __len);          \
  } while (0);
D
dapan1121 已提交
48

49 50
#define STR_WITH_MAXSIZE_TO_VARSTR(x, str, _maxs)                         \
  do {                                                                    \
H
Hui Li 已提交
51
    char *_e = stpncpy(varDataVal(x), (str), (_maxs)-VARSTR_HEADER_SIZE); \
52
    varDataSetLen(x, (_e - (x)-VARSTR_HEADER_SIZE));                      \
H
Hongze Cheng 已提交
53 54
  } while (0)

55 56 57 58
#define STR_WITH_SIZE_TO_VARSTR(x, str, _size)  \
  do {                                          \
    *(VarDataLenT *)(x) = (VarDataLenT)(_size); \
    memcpy(varDataVal(x), (str), (_size));      \
H
Hongze Cheng 已提交
59
  } while (0);
H
hjxilinx 已提交
60

61
// ----------------- TSDB COLUMN DEFINITION
62
#pragma pack(push, 1)
63
typedef struct {
64 65
  col_id_t colId;        // column ID(start from PRIMARYKEY_TIMESTAMP_COL_ID(1))
  int32_t  type : 8;     // column type
C
Cary Xu 已提交
66
  int32_t  bytes : 24;   // column bytes (0~16M)
67 68
  int32_t  sma : 8;      // block SMA: 0, no SMA, 1, sum/min/max, 2, ...
  int32_t  offset : 24;  // point offset in STpRow after the header part.
69
} STColumn;
70
#pragma pack(pop)
71

S
Shengliang Guan 已提交
72
#define colType(col)   ((col)->type)
C
Cary Xu 已提交
73
#define colSma(col)    ((col)->sma)
S
Shengliang Guan 已提交
74 75
#define colColId(col)  ((col)->colId)
#define colBytes(col)  ((col)->bytes)
76 77
#define colOffset(col) ((col)->offset)

S
Shengliang Guan 已提交
78
#define colSetType(col, t)   (colType(col) = (t))
C
Cary Xu 已提交
79
#define colSetSma(col, s)    (colSma(col) = (s))
80
#define colSetColId(col, id) (colColId(col) = (id))
S
Shengliang Guan 已提交
81
#define colSetBytes(col, b)  (colBytes(col) = (b))
82 83 84 85
#define colSetOffset(col, o) (colOffset(col) = (o))

// ----------------- TSDB SCHEMA DEFINITION
typedef struct {
C
Cary Xu 已提交
86 87 88 89 90 91
  int32_t      numOfCols;  // Number of columns appended
  schema_ver_t version;    // schema version
  uint16_t     flen;       // First part length in a STpRow after the header part
  int32_t      vlen;       // pure value part length, excluded the overhead (bytes only)
  int32_t      tlen;       // maximum length of a STpRow without the header part
                           // (sizeof(VarDataOffsetT) + sizeof(VarDataLenT) + (bytes))
92 93 94
  STColumn columns[];
} STSchema;

S
Shengliang Guan 已提交
95 96 97 98 99
#define schemaNCols(s)    ((s)->numOfCols)
#define schemaVersion(s)  ((s)->version)
#define schemaTLen(s)     ((s)->tlen)
#define schemaFLen(s)     ((s)->flen)
#define schemaVLen(s)     ((s)->vlen)
100
#define schemaColAt(s, i) ((s)->columns + i)
wafwerar's avatar
wafwerar 已提交
101
#define tdFreeSchema(s)   taosMemoryFreeClear((s))
102

H
Hongze Cheng 已提交
103
STSchema *tdDupSchema(const STSchema *pSchema);
S
Shengliang Guan 已提交
104 105
int32_t   tdEncodeSchema(void **buf, STSchema *pSchema);
void     *tdDecodeSchema(void *buf, STSchema **pRSchema);
106

S
Shengliang Guan 已提交
107
static FORCE_INLINE int32_t comparColId(const void *key1, const void *key2) {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  if (*(int16_t *)key1 > ((STColumn *)key2)->colId) {
    return 1;
  } else if (*(int16_t *)key1 < ((STColumn *)key2)->colId) {
    return -1;
  } else {
    return 0;
  }
}

static FORCE_INLINE STColumn *tdGetColOfID(STSchema *pSchema, int16_t colId) {
  void *ptr = bsearch(&colId, (void *)pSchema->columns, schemaNCols(pSchema), sizeof(STColumn), comparColId);
  if (ptr == NULL) return NULL;
  return (STColumn *)ptr;
}

// ----------------- SCHEMA BUILDER DEFINITION
typedef struct {
C
Cary Xu 已提交
125 126 127 128 129 130 131
  int32_t      tCols;
  int32_t      nCols;
  schema_ver_t version;
  uint16_t     flen;
  int32_t      vlen;
  int32_t      tlen;
  STColumn    *columns;
132 133
} STSchemaBuilder;

C
Cary Xu 已提交
134 135 136 137 138 139 140 141 142 143 144
// use 2 bits for bitmap(default: STSRow/sub block)
#define TD_VTYPE_BITS        2
#define TD_VTYPE_PARTS       4  // PARTITIONS: 1 byte / 2 bits
#define TD_VTYPE_OPTR        3  // OPERATOR: 4 - 1, utilize to get remainder
#define TD_BITMAP_BYTES(cnt) (((cnt) + TD_VTYPE_OPTR) >> 2)

// use 1 bit for bitmap(super block)
#define TD_VTYPE_BITS_I        1
#define TD_VTYPE_PARTS_I       8  // PARTITIONS: 1 byte / 1 bit
#define TD_VTYPE_OPTR_I        7  // OPERATOR: 8 - 1, utilize to get remainder
#define TD_BITMAP_BYTES_I(cnt) (((cnt) + TD_VTYPE_OPTR_I) >> 3)
C
Cary Xu 已提交
145

C
Cary Xu 已提交
146
int32_t   tdInitTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version);
147
void      tdDestroyTSchemaBuilder(STSchemaBuilder *pBuilder);
C
Cary Xu 已提交
148
void      tdResetTSchemaBuilder(STSchemaBuilder *pBuilder, schema_ver_t version);
C
Cary Xu 已提交
149
int32_t   tdAddColToSchema(STSchemaBuilder *pBuilder, int8_t type, int8_t sma, col_id_t colId, col_bytes_t bytes);
150
STSchema *tdGetSchemaFromBuilder(STSchemaBuilder *pBuilder);
H
Hongze Cheng 已提交
151

H
TD-1548  
Hongze Cheng 已提交
152
// ----------------- Semantic timestamp key definition
C
update  
Cary Xu 已提交
153
#ifdef TD_2_0
C
Cary Xu 已提交
154

H
TD-1548  
Hongze Cheng 已提交
155 156
typedef uint64_t TKEY;

S
Shengliang Guan 已提交
157 158
#define TKEY_INVALID       UINT64_MAX
#define TKEY_NULL          TKEY_INVALID
H
Hongze Cheng 已提交
159
#define TKEY_NEGATIVE_FLAG (((TKEY)1) << 63)
S
Shengliang Guan 已提交
160 161
#define TKEY_DELETE_FLAG   (((TKEY)1) << 62)
#define TKEY_VALUE_FILTER  (~(TKEY_NEGATIVE_FLAG | TKEY_DELETE_FLAG))
H
TD-1548  
Hongze Cheng 已提交
162 163

#define TKEY_IS_NEGATIVE(tkey) (((tkey)&TKEY_NEGATIVE_FLAG) != 0)
S
Shengliang Guan 已提交
164
#define TKEY_IS_DELETED(tkey)  (((tkey)&TKEY_DELETE_FLAG) != 0)
H
TD-1548  
Hongze Cheng 已提交
165
#define tdSetTKEYDeleted(tkey) ((tkey) | TKEY_DELETE_FLAG)
S
Shengliang Guan 已提交
166 167
#define tdGetTKEY(key)         (((TKEY)TABS(key)) | (TKEY_NEGATIVE_FLAG & (TKEY)(key)))
#define tdGetKey(tkey)         (((TSKEY)((tkey)&TKEY_VALUE_FILTER)) * (TKEY_IS_NEGATIVE(tkey) ? -1 : 1))
H
TD-1548  
Hongze Cheng 已提交
168

D
dapan1121 已提交
169 170 171 172 173
#define MIN_TS_KEY ((TSKEY)0x8000000000000001)
#define MAX_TS_KEY ((TSKEY)0x3fffffffffffffff)

#define TD_TO_TKEY(key) tdGetTKEY(((key) < MIN_TS_KEY) ? MIN_TS_KEY : (((key) > MAX_TS_KEY) ? MAX_TS_KEY : key))

C
Cary Xu 已提交
174 175
#else

C
Cary Xu 已提交
176 177
// typedef uint64_t TKEY;
#define TKEY TSKEY
C
Cary Xu 已提交
178

S
Shengliang Guan 已提交
179 180
#define TKEY_INVALID       UINT64_MAX
#define TKEY_NULL          TKEY_INVALID
C
Cary Xu 已提交
181
#define TKEY_NEGATIVE_FLAG (((TKEY)1) << 63)
S
Shengliang Guan 已提交
182
#define TKEY_VALUE_FILTER  (~(TKEY_NEGATIVE_FLAG))
C
Cary Xu 已提交
183 184

#define TKEY_IS_NEGATIVE(tkey) (((tkey)&TKEY_NEGATIVE_FLAG) != 0)
S
Shengliang Guan 已提交
185
#define TKEY_IS_DELETED(tkey)  (false)
C
Cary Xu 已提交
186

S
Shengliang Guan 已提交
187
#define tdGetTKEY(key)  (key)
C
Cary Xu 已提交
188
#define tdGetKey(tskey) (tskey)
C
Cary Xu 已提交
189 190 191 192 193 194 195 196

#define MIN_TS_KEY ((TSKEY)0x8000000000000001)
#define MAX_TS_KEY ((TSKEY)0x7fffffffffffffff)

#define TD_TO_TKEY(key) tdGetTKEY(((key) < MIN_TS_KEY) ? MIN_TS_KEY : (((key) > MAX_TS_KEY) ? MAX_TS_KEY : key))

#endif

D
dapan1121 已提交
197 198 199 200 201 202 203 204 205 206 207
static FORCE_INLINE TKEY keyToTkey(TSKEY key) {
  TSKEY lkey = key;
  if (key > MAX_TS_KEY) {
    lkey = MAX_TS_KEY;
  } else if (key < MIN_TS_KEY) {
    lkey = MIN_TS_KEY;
  }

  return tdGetTKEY(lkey);
}

S
Shengliang Guan 已提交
208
static FORCE_INLINE int32_t tkeyComparFn(const void *tkey1, const void *tkey2) {
H
TD-1548  
Hongze Cheng 已提交
209 210 211 212 213 214 215 216 217 218 219
  TSKEY key1 = tdGetKey(*(TKEY *)tkey1);
  TSKEY key2 = tdGetKey(*(TKEY *)tkey2);

  if (key1 < key2) {
    return -1;
  } else if (key1 > key2) {
    return 1;
  } else {
    return 0;
  }
}
H
hzcheng 已提交
220

C
Cary Xu 已提交
221
#if 0
C
Cary Xu 已提交
222 223 224
// ----------------- Data row structure

/* A data row, the format is like below:
K
kailixu 已提交
225
 * |<------------------------------------------------ len ---------------------------------->|
C
Cary Xu 已提交
226 227 228 229 230 231 232 233 234 235 236
 * |<--     Head      -->|<---------   flen -------------->|                                 |
 * +---------------------+---------------------------------+---------------------------------+
 * | uint16_t |  int16_t |                                 |                                 |
 * +----------+----------+---------------------------------+---------------------------------+
 * |   len    | sversion |           First part            |             Second part         |
 * +----------+----------+---------------------------------+---------------------------------+
 *
 * NOTE: timestamp in this row structure is TKEY instead of TSKEY
 */
typedef void *SDataRow;

B
Bomin Zhang 已提交
237
#define TD_DATA_ROW_HEAD_SIZE (sizeof(uint16_t) + sizeof(int16_t))
H
hzcheng 已提交
238

S
Shengliang Guan 已提交
239 240 241 242 243 244 245 246 247
#define dataRowLen(r)                (*(TDRowLenT *)(r))  // 0~65535
#define dataRowEnd(r)                POINTER_SHIFT(r, dataRowLen(r))
#define dataRowVersion(r)            (*(int16_t *)POINTER_SHIFT(r, sizeof(int16_t)))
#define dataRowTuple(r)              POINTER_SHIFT(r, TD_DATA_ROW_HEAD_SIZE)
#define dataRowTKey(r)               (*(TKEY *)(dataRowTuple(r)))
#define dataRowKey(r)                tdGetKey(dataRowTKey(r))
#define dataRowSetLen(r, l)          (dataRowLen(r) = (l))
#define dataRowSetVersion(r, v)      (dataRowVersion(r) = (v))
#define dataRowCpy(dst, r)           memcpy((dst), (r), dataRowLen(r))
H
TD-166  
hzcheng 已提交
248
#define dataRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_DATA_ROW_HEAD_SIZE)
S
Shengliang Guan 已提交
249
#define dataRowDeleted(r)            TKEY_IS_DELETED(dataRowTKey(r))
H
hzcheng 已提交
250

C
Cary Xu 已提交
251 252
SDataRow tdNewDataRowFromSchema(STSchema *pSchema);
void     tdFreeDataRow(SDataRow row);
H
TD-166  
hzcheng 已提交
253
void     tdInitDataRow(SDataRow row, STSchema *pSchema);
C
Cary Xu 已提交
254
SDataRow tdDataRowDup(SDataRow row);
H
more  
Hongze Cheng 已提交
255

256
// offset here not include dataRow header length
S
Shengliang Guan 已提交
257
static FORCE_INLINE int32_t tdAppendDataColVal(SDataRow row, const void *value, bool isCopyVarData, int8_t type,
258
                                           int32_t offset) {
259
  assert(value != NULL);
260 261
  int32_t toffset = offset + TD_DATA_ROW_HEAD_SIZE;

H
TD-1548  
Hongze Cheng 已提交
262 263
  if (IS_VAR_DATA_TYPE(type)) {
    *(VarDataOffsetT *)POINTER_SHIFT(row, toffset) = dataRowLen(row);
264 265 266
    if (isCopyVarData) {
      memcpy(POINTER_SHIFT(row, dataRowLen(row)), value, varDataTLen(value));
    }
H
TD-1548  
Hongze Cheng 已提交
267 268 269
    dataRowLen(row) += varDataTLen(value);
  } else {
    if (offset == 0) {
270
      assert(type == TSDB_DATA_TYPE_TIMESTAMP);
H
TD-1548  
Hongze Cheng 已提交
271
      TKEY tvalue = tdGetTKEY(*(TSKEY *)value);
C
Cary Xu 已提交
272
      memcpy(POINTER_SHIFT(row, toffset), (const void *)(&tvalue), TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
273
    } else {
274
      memcpy(POINTER_SHIFT(row, toffset), value, TYPE_BYTES[type]);
H
TD-1548  
Hongze Cheng 已提交
275
    }
276 277 278 279 280
  }

  return 0;
}

281
// offset here not include dataRow header length
S
Shengliang Guan 已提交
282
static FORCE_INLINE int32_t tdAppendColVal(SDataRow row, const void *value, int8_t type, int32_t offset) {
283 284 285
  return tdAppendDataColVal(row, value, true, type, offset);
}

C
Cary Xu 已提交
286
// NOTE: offset here including the header size
C
Cary Xu 已提交
287
static FORCE_INLINE void *tdGetRowDataOfCol(SDataRow row, int8_t type, int32_t offset) {
C
Cary Xu 已提交
288 289 290 291 292 293 294
  if (IS_VAR_DATA_TYPE(type)) {
    return POINTER_SHIFT(row, *(VarDataOffsetT *)POINTER_SHIFT(row, offset));
  } else {
    return POINTER_SHIFT(row, offset);
  }
}

S
Shengliang Guan 已提交
295
static FORCE_INLINE void *tdGetPtrToCol(SDataRow row, STSchema *pSchema, int32_t idx) {
296 297 298
  return POINTER_SHIFT(row, TD_DATA_ROW_HEAD_SIZE + pSchema->columns[idx].offset);
}

S
Shengliang Guan 已提交
299
static FORCE_INLINE void *tdGetColOfRowBySchema(SDataRow row, STSchema *pSchema, int32_t idx) {
300
  int16_t offset = TD_DATA_ROW_HEAD_SIZE + pSchema->columns[idx].offset;
H
Hongze Cheng 已提交
301
  int8_t  type = pSchema->columns[idx].type;
302 303 304 305

  return tdGetRowDataOfCol(row, type, offset);
}

S
Shengliang Guan 已提交
306
static FORCE_INLINE bool tdIsColOfRowNullBySchema(SDataRow row, STSchema *pSchema, int32_t idx) {
307
  int16_t offset = TD_DATA_ROW_HEAD_SIZE + pSchema->columns[idx].offset;
H
Hongze Cheng 已提交
308
  int8_t  type = pSchema->columns[idx].type;
309 310 311 312

  return isNull(tdGetRowDataOfCol(row, type, offset), type);
}

S
Shengliang Guan 已提交
313
static FORCE_INLINE void tdSetColOfRowNullBySchema(SDataRow row, STSchema *pSchema, int32_t idx) {
314
  int16_t offset = TD_DATA_ROW_HEAD_SIZE + pSchema->columns[idx].offset;
H
Hongze Cheng 已提交
315
  int8_t  type = pSchema->columns[idx].type;
316 317 318 319 320
  int16_t bytes = pSchema->columns[idx].bytes;

  setNull(tdGetRowDataOfCol(row, type, offset), type, bytes);
}

S
Shengliang Guan 已提交
321 322
static FORCE_INLINE void tdCopyColOfRowBySchema(SDataRow dst, STSchema *pDstSchema, int32_t dstIdx, SDataRow src,
                                                STSchema *pSrcSchema, int32_t srcIdx) {
323
  int8_t type = pDstSchema->columns[dstIdx].type;
324
  assert(type == pSrcSchema->columns[srcIdx].type);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
  void *pData = tdGetPtrToCol(dst, pDstSchema, dstIdx);
  void *value = tdGetPtrToCol(src, pSrcSchema, srcIdx);

  switch (type) {
    case TSDB_DATA_TYPE_BINARY:
    case TSDB_DATA_TYPE_NCHAR:
      *(VarDataOffsetT *)pData = *(VarDataOffsetT *)value;
      pData = POINTER_SHIFT(dst, *(VarDataOffsetT *)pData);
      value = POINTER_SHIFT(src, *(VarDataOffsetT *)value);
      memcpy(pData, value, varDataTLen(value));
      break;
    case TSDB_DATA_TYPE_NULL:
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_UTINYINT:
      *(uint8_t *)pData = *(uint8_t *)value;
      break;
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_USMALLINT:
      *(uint16_t *)pData = *(uint16_t *)value;
      break;
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_UINT:
      *(uint32_t *)pData = *(uint32_t *)value;
      break;
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_UBIGINT:
      *(uint64_t *)pData = *(uint64_t *)value;
      break;
    case TSDB_DATA_TYPE_FLOAT:
      SET_FLOAT_PTR(pData, value);
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      SET_DOUBLE_PTR(pData, value);
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
361
      if (pSrcSchema->columns[srcIdx].colId == PRIMARYKEY_TIMESTAMP_COL_ID) {
362 363 364 365 366 367 368 369 370
        *(TSKEY *)pData = tdGetKey(*(TKEY *)value);
      } else {
        *(TSKEY *)pData = *(TSKEY *)value;
      }
      break;
    default:
      memcpy(pData, value, pSrcSchema->columns[srcIdx].bytes);
  }
}
C
Cary Xu 已提交
371
#endif
H
TD-34  
hzcheng 已提交
372
// ----------------- Data column structure
373
// SDataCol arrangement: data => bitmap => dataOffset
H
TD-34  
hzcheng 已提交
374
typedef struct SDataCol {
C
Cary Xu 已提交
375 376 377 378
  int8_t          type;            // column type
  uint8_t         bitmap : 1;      // 0: no bitmap if all rows are NORM, 1: has bitmap if has NULL/NORM rows
  uint8_t         bitmapMode : 1;  // default is 0(2 bits), otherwise 1(1 bit)
  uint8_t         reserve : 6;
H
TD-166  
hzcheng 已提交
379
  int16_t         colId;      // column ID
S
Shengliang Guan 已提交
380 381 382 383
  int32_t         bytes;      // column data bytes defined
  int32_t         offset;     // data offset in a SDataRow (including the header size)
  int32_t         spaceSize;  // Total space size for this column
  int32_t         len;        // column data length
H
TD-166  
hzcheng 已提交
384
  VarDataOffsetT *dataOff;    // For binary and nchar data, the offset in the data column
S
Shengliang Guan 已提交
385 386
  void           *pData;      // Actual data pointer
  void           *pBitmap;    // Bitmap pointer
L
lichuang 已提交
387
  TSKEY           ts;         // only used in last NULL column
H
TD-34  
hzcheng 已提交
388 389
} SDataCol;

C
Cary Xu 已提交
390 391


K
kailixu 已提交
392
#define isAllRowsNull(pCol) ((pCol)->len == 0)
K
Kaili Xu 已提交
393
#define isAllRowsNone(pCol) ((pCol)->len == 0)
H
TD-166  
hzcheng 已提交
394 395
static FORCE_INLINE void dataColReset(SDataCol *pDataCol) { pDataCol->len = 0; }

S
Shengliang Guan 已提交
396
int32_t tdAllocMemForCol(SDataCol *pCol, int32_t maxPoints);
397

S
Shengliang Guan 已提交
398 399 400
void    dataColInit(SDataCol *pDataCol, STColumn *pCol, int32_t maxPoints);
int32_t dataColAppendVal(SDataCol *pCol, const void *value, int32_t numOfRows, int32_t maxPoints);
void   *dataColSetOffset(SDataCol *pCol, int32_t nEle);
H
TD-166  
hzcheng 已提交
401

S
Shengliang Guan 已提交
402
bool isNEleNull(SDataCol *pCol, int32_t nEle);
H
TD-166  
hzcheng 已提交
403

C
Cary Xu 已提交
404
#if 0
H
TD-166  
hzcheng 已提交
405
// Get the data pointer from a column-wised data
S
Shengliang Guan 已提交
406
static FORCE_INLINE const void *tdGetColDataOfRow(SDataCol *pCol, int32_t row) {
K
kailixu 已提交
407
  if (isAllRowsNull(pCol)) {
C
Cary Xu 已提交
408
    return getNullValue(pCol->type);
K
kailixu 已提交
409
  }
H
Hongze Cheng 已提交
410 411 412 413
  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 已提交
414 415 416
  }
}

S
Shengliang Guan 已提交
417
static FORCE_INLINE int32_t dataColGetNEleLen(SDataCol *pDataCol, int32_t rows) {
418
  assert(rows > 0);
H
TD-166  
hzcheng 已提交
419

H
Hongze Cheng 已提交
420 421 422 423
  if (IS_VAR_DATA_TYPE(pDataCol->type)) {
    return pDataCol->dataOff[rows - 1] + varDataTLen(tdGetColDataOfRow(pDataCol, rows - 1));
  } else {
    return TYPE_BYTES[pDataCol->type] * rows;
H
TD-166  
hzcheng 已提交
424 425
  }
}
C
Cary Xu 已提交
426
#endif
H
TD-34  
hzcheng 已提交
427
typedef struct {
C
update  
Cary Xu 已提交
428 429
  col_id_t  maxCols;    // max number of columns
  col_id_t  numOfCols;  // Total number of cols
S
Shengliang Guan 已提交
430 431
  int32_t   maxPoints;  // max number of points
  int32_t   numOfRows;
C
Cary Xu 已提交
432 433
  int32_t   bitmapMode : 1;  // default is 0(2 bits), otherwise 1(1 bit)
  int32_t   sversion : 31;   // TODO: set sversion(not used yet)
H
Hongze Cheng 已提交
434
  SDataCol *cols;
H
TD-34  
hzcheng 已提交
435 436
} SDataCols;

C
Cary Xu 已提交
437 438 439
static FORCE_INLINE bool tdDataColsIsBitmapI(SDataCols *pCols) { return pCols->bitmapMode != 0; }
static FORCE_INLINE void tdDataColsSetBitmapI(SDataCols *pCols) { pCols->bitmapMode = 1; }

S
Shengliang Guan 已提交
440
#define keyCol(pCols)              (&((pCols)->cols[0]))                    // Key column
C
Cary Xu 已提交
441
#define dataColsTKeyAt(pCols, idx) ((TKEY *)(keyCol(pCols)->pData))[(idx)]  // the idx row of column-wised data
S
Shengliang Guan 已提交
442
#define dataColsKeyAt(pCols, idx)  tdGetKey(dataColsTKeyAt(pCols, idx))
443 444 445 446 447 448 449 450
static FORCE_INLINE TKEY dataColsTKeyFirst(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsTKeyAt(pCols, 0);
  } else {
    return TKEY_INVALID;
  }
}

S
Shengliang Guan 已提交
451
static FORCE_INLINE TSKEY dataColsKeyAtRow(SDataCols *pCols, int32_t row) {
452
  assert(row < pCols->numOfRows);
453 454 455
  return dataColsKeyAt(pCols, row);
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
static FORCE_INLINE TSKEY dataColsKeyFirst(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsKeyAt(pCols, 0);
  } else {
    return TSDB_DATA_TIMESTAMP_NULL;
  }
}

static FORCE_INLINE TKEY dataColsTKeyLast(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsTKeyAt(pCols, pCols->numOfRows - 1);
  } else {
    return TKEY_INVALID;
  }
}

static FORCE_INLINE TSKEY dataColsKeyLast(SDataCols *pCols) {
  if (pCols->numOfRows) {
    return dataColsKeyAt(pCols, pCols->numOfRows - 1);
  } else {
    return TSDB_DATA_TIMESTAMP_NULL;
  }
}
H
TD-34  
hzcheng 已提交
479

S
Shengliang Guan 已提交
480
SDataCols *tdNewDataCols(int32_t maxCols, int32_t maxRows);
H
TD-34  
hzcheng 已提交
481
void       tdResetDataCols(SDataCols *pCols);
S
Shengliang Guan 已提交
482
int32_t    tdInitDataCols(SDataCols *pCols, STSchema *pSchema);
H
TD-100  
hzcheng 已提交
483
SDataCols *tdDupDataCols(SDataCols *pCols, bool keepData);
H
Hongze Cheng 已提交
484
SDataCols *tdFreeDataCols(SDataCols *pCols);
C
Cary Xu 已提交
485
int32_t tdMergeDataCols(SDataCols *target, SDataCols *source, int32_t rowsToMerge, int32_t *pOffset, bool forceSetNull, TDRowVerT maxVer);
H
more  
Hongze Cheng 已提交
486

H
Hongze Cheng 已提交
487
// ----------------- K-V data row structure
C
Cary Xu 已提交
488 489
/* |<-------------------------------------- len -------------------------------------------->|
 * |<----- header  ----->|<--------------------------- body -------------------------------->|
H
Hongze Cheng 已提交
490
 * +----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
491
 * | uint16_t |  int16_t |                                 |                                 |
H
Hongze Cheng 已提交
492 493 494 495
 * +----------+----------+---------------------------------+---------------------------------+
 * |    len   |   ncols  |           cols index            |             data part           |
 * +----------+----------+---------------------------------+---------------------------------+
 */
H
Hongze Cheng 已提交
496
typedef void *SKVRow;
H
Hongze Cheng 已提交
497 498

typedef struct {
C
Cary Xu 已提交
499 500
  int16_t  colId;
  uint16_t offset;
H
Hongze Cheng 已提交
501 502
} SColIdx;

C
Cary Xu 已提交
503
#define TD_KV_ROW_HEAD_SIZE (sizeof(uint16_t) + sizeof(int16_t))
H
Hongze Cheng 已提交
504

S
Shengliang Guan 已提交
505 506 507 508 509 510 511
#define kvRowLen(r)            (*(TDRowLenT *)(r))
#define kvRowNCols(r)          (*(int16_t *)POINTER_SHIFT(r, sizeof(uint16_t)))
#define kvRowSetLen(r, len)    kvRowLen(r) = (len)
#define kvRowSetNCols(r, n)    kvRowNCols(r) = (n)
#define kvRowColIdx(r)         (SColIdx *)POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE)
#define kvRowValues(r)         POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE + sizeof(SColIdx) * kvRowNCols(r))
#define kvRowCpy(dst, r)       memcpy((dst), (r), kvRowLen(r))
H
Hongze Cheng 已提交
512
#define kvRowColVal(r, colIdx) POINTER_SHIFT(kvRowValues(r), (colIdx)->offset)
S
Shengliang Guan 已提交
513
#define kvRowColIdxAt(r, i)    (kvRowColIdx(r) + (i))
wafwerar's avatar
wafwerar 已提交
514
#define kvRowFree(r)           taosMemoryFreeClear(r)
S
Shengliang Guan 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528
#define kvRowEnd(r)            POINTER_SHIFT(r, kvRowLen(r))
#define kvRowValLen(r)         (kvRowLen(r) - TD_KV_ROW_HEAD_SIZE - sizeof(SColIdx) * kvRowNCols(r))
#define kvRowTKey(r)           (*(TKEY *)(kvRowValues(r)))
#define kvRowKey(r)            tdGetKey(kvRowTKey(r))
#define kvRowKeys(r)           POINTER_SHIFT(r, *(uint16_t *)POINTER_SHIFT(r, TD_KV_ROW_HEAD_SIZE + sizeof(int16_t)))
#define kvRowDeleted(r)        TKEY_IS_DELETED(kvRowTKey(r))

SKVRow  tdKVRowDup(SKVRow row);
int32_t tdSetKVRowDataOfCol(SKVRow *orow, int16_t colId, int8_t type, void *value);
int32_t tdEncodeKVRow(void **buf, SKVRow row);
void   *tdDecodeKVRow(void *buf, SKVRow *row);
void    tdSortKVRowByColIdx(SKVRow row);

static FORCE_INLINE int32_t comparTagId(const void *key1, const void *key2) {
H
Hongze Cheng 已提交
529 530 531 532 533 534 535 536 537
  if (*(int16_t *)key1 > ((SColIdx *)key2)->colId) {
    return 1;
  } else if (*(int16_t *)key1 < ((SColIdx *)key2)->colId) {
    return -1;
  } else {
    return 0;
  }
}

H
Hongze Cheng 已提交
538
static FORCE_INLINE void *tdGetKVRowValOfCol(SKVRow row, int16_t colId) {
H
Hongze Cheng 已提交
539
  void *ret = taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_EQ);
H
Hongze Cheng 已提交
540
  if (ret == NULL) return NULL;
H
Hongze Cheng 已提交
541
  return kvRowColVal(row, (SColIdx *)ret);
H
Hongze Cheng 已提交
542 543
}

L
liuyq-617 已提交
544 545 546 547
static FORCE_INLINE void *tdGetKVRowIdxOfCol(SKVRow row, int16_t colId) {
  return taosbsearch(&colId, kvRowColIdx(row), kvRowNCols(row), sizeof(SColIdx), comparTagId, TD_EQ);
}

C
Cary Xu 已提交
548
#if 0
C
Cary Xu 已提交
549
// offset here not include kvRow header length
S
Shengliang Guan 已提交
550
static FORCE_INLINE int32_t tdAppendKvColVal(SKVRow row, const void *value, bool isCopyValData, int16_t colId, int8_t type,
551
                                         int32_t offset) {
552
  assert(value != NULL);
553
  int32_t  toffset = offset + TD_KV_ROW_HEAD_SIZE;
C
Cary Xu 已提交
554 555 556 557
  SColIdx *pColIdx = (SColIdx *)POINTER_SHIFT(row, toffset);
  char *   ptr = (char *)POINTER_SHIFT(row, kvRowLen(row));

  pColIdx->colId = colId;
C
Cary Xu 已提交
558
  pColIdx->offset = kvRowLen(row);  // offset of pColIdx including the TD_KV_ROW_HEAD_SIZE
C
Cary Xu 已提交
559 560

  if (IS_VAR_DATA_TYPE(type)) {
561 562 563
    if (isCopyValData) {
      memcpy(ptr, value, varDataTLen(value));
    }
C
Cary Xu 已提交
564 565
    kvRowLen(row) += varDataTLen(value);
  } else {
566
    if (offset == 0) {
567
      assert(type == TSDB_DATA_TYPE_TIMESTAMP);
C
Cary Xu 已提交
568 569 570 571 572 573 574 575 576 577
      TKEY tvalue = tdGetTKEY(*(TSKEY *)value);
      memcpy(ptr, (void *)(&tvalue), TYPE_BYTES[type]);
    } else {
      memcpy(ptr, value, TYPE_BYTES[type]);
    }
    kvRowLen(row) += TYPE_BYTES[type];
  }

  return 0;
}
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
// NOTE: offset here including the header size
static FORCE_INLINE void *tdGetKvRowDataOfCol(void *row, int32_t offset) { return POINTER_SHIFT(row, offset); }

static FORCE_INLINE void *tdGetKVRowValOfColEx(SKVRow row, int16_t colId, int32_t *nIdx) {
  while (*nIdx < kvRowNCols(row)) {
    SColIdx *pColIdx = kvRowColIdxAt(row, *nIdx);
    if (pColIdx->colId == colId) {
      ++(*nIdx);
      return tdGetKvRowDataOfCol(row, pColIdx->offset);
    } else if (pColIdx->colId > colId) {
      return NULL;
    } else {
      ++(*nIdx);
    }
  }
  return NULL;
}
C
Cary Xu 已提交
595
#endif
H
Hongze Cheng 已提交
596 597 598 599 600
// ----------------- K-V data row builder
typedef struct {
  int16_t  tCols;
  int16_t  nCols;
  SColIdx *pColIdx;
C
Cary Xu 已提交
601 602
  uint16_t alloc;
  uint16_t size;
S
Shengliang Guan 已提交
603
  void    *buf;
H
Hongze Cheng 已提交
604
} SKVRowBuilder;
H
Hongze Cheng 已提交
605

S
Shengliang Guan 已提交
606 607 608 609
int32_t tdInitKVRowBuilder(SKVRowBuilder *pBuilder);
void    tdDestroyKVRowBuilder(SKVRowBuilder *pBuilder);
void    tdResetKVRowBuilder(SKVRowBuilder *pBuilder);
SKVRow  tdGetKVRowFromBuilder(SKVRowBuilder *pBuilder);
H
Hongze Cheng 已提交
610

611
static FORCE_INLINE int32_t tdAddColToKVRow(SKVRowBuilder *pBuilder, col_id_t colId, int8_t type, const void *value) {
H
Hongze Cheng 已提交
612 613
  if (pBuilder->nCols >= pBuilder->tCols) {
    pBuilder->tCols *= 2;
wafwerar's avatar
wafwerar 已提交
614
    SColIdx *pColIdx = (SColIdx *)taosMemoryRealloc((void *)(pBuilder->pColIdx), sizeof(SColIdx) * pBuilder->tCols);
T
tickduan 已提交
615 616
    if (pColIdx == NULL) return -1;
    pBuilder->pColIdx = pColIdx;
H
Hongze Cheng 已提交
617 618 619 620 621 622 623
  }

  pBuilder->pColIdx[pBuilder->nCols].colId = colId;
  pBuilder->pColIdx[pBuilder->nCols].offset = pBuilder->size;

  pBuilder->nCols++;

S
Shengliang Guan 已提交
624
  int32_t tlen = IS_VAR_DATA_TYPE(type) ? varDataTLen(value) : TYPE_BYTES[type];
H
Hongze Cheng 已提交
625 626 627 628
  if (tlen > pBuilder->alloc - pBuilder->size) {
    while (tlen > pBuilder->alloc - pBuilder->size) {
      pBuilder->alloc *= 2;
    }
wafwerar's avatar
wafwerar 已提交
629
    void *buf = taosMemoryRealloc(pBuilder->buf, pBuilder->alloc);
T
tickduan 已提交
630 631
    if (buf == NULL) return -1;
    pBuilder->buf = buf;
H
Hongze Cheng 已提交
632 633 634 635 636 637 638
  }

  memcpy(POINTER_SHIFT(pBuilder->buf, pBuilder->size), value, tlen);
  pBuilder->size += tlen;

  return 0;
}
C
Cary Xu 已提交
639
#if 0
C
update  
Cary Xu 已提交
640
// ----------------- SMemRow appended with tuple row structure
C
Cary Xu 已提交
641
/*
C
Cary Xu 已提交
642
 * |---------|------------------------------------------------- len ---------------------------------->|
C
Cary Xu 已提交
643 644 645 646 647 648
 * |<--------     Head      ------>|<---------   flen -------------->|                                 |
 * |---------+---------------------+---------------------------------+---------------------------------+
 * | uint8_t | uint16_t |  int16_t |                                 |                                 |
 * |---------+----------+----------+---------------------------------+---------------------------------+
 * |  flag   |   len    | sversion |           First part            |             Second part         |
 * +---------+----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
649 650 651
 *
 * NOTE: timestamp in this row structure is TKEY instead of TSKEY
 */
C
Cary Xu 已提交
652

C
update  
Cary Xu 已提交
653
// ----------------- SMemRow appended with extended K-V data row structure
C
Cary Xu 已提交
654 655
/* |--------------------|------------------------------------------------  len ---------------------------------->|
 * |<-------------     Head      ------------>|<---------   flen -------------->|                                 |
C
Cary Xu 已提交
656 657 658 659 660
 * |--------------------+----------+--------------------------------------------+---------------------------------+
 * | uint8_t | int16_t  | uint16_t |  int16_t |                                 |                                 |
 * |---------+----------+----------+----------+---------------------------------+---------------------------------+
 * |   flag  | sversion |   len    |   ncols  |           cols index            |             data part           |
 * |---------+----------+----------+----------+---------------------------------+---------------------------------+
C
Cary Xu 已提交
661 662
 */

C
Cary Xu 已提交
663 664
typedef void *SMemRow;

S
Shengliang Guan 已提交
665 666
#define TD_MEM_ROW_TYPE_SIZE        sizeof(uint8_t)
#define TD_MEM_ROW_KV_VER_SIZE      sizeof(int16_t)
C
Cary Xu 已提交
667
#define TD_MEM_ROW_KV_TYPE_VER_SIZE (TD_MEM_ROW_TYPE_SIZE + TD_MEM_ROW_KV_VER_SIZE)
S
Shengliang Guan 已提交
668 669
#define TD_MEM_ROW_DATA_HEAD_SIZE   (TD_MEM_ROW_TYPE_SIZE + TD_DATA_ROW_HEAD_SIZE)
#define TD_MEM_ROW_KV_HEAD_SIZE     (TD_MEM_ROW_TYPE_SIZE + TD_MEM_ROW_KV_VER_SIZE + TD_KV_ROW_HEAD_SIZE)
C
Cary Xu 已提交
670

S
Shengliang Guan 已提交
671 672
#define SMEM_ROW_DATA 0x0U   // SDataRow
#define SMEM_ROW_KV   0x01U  // SKVRow
673 674 675

#define KVRatioConvert (0.9f)

C
Cary Xu 已提交
676
#define memRowType(r) ((*(uint8_t *)(r)) & 0x01)
C
Cary Xu 已提交
677

S
Shengliang Guan 已提交
678 679 680 681 682
#define memRowSetType(r, t)  ((*(uint8_t *)(r)) = (t))  // set the total byte in case of dirty memory
#define isDataRowT(t)        (SMEM_ROW_DATA == (((uint8_t)(t)) & 0x01))
#define isDataRow(r)         (SMEM_ROW_DATA == memRowType(r))
#define isKvRowT(t)          (SMEM_ROW_KV == (((uint8_t)(t)) & 0x01))
#define isKvRow(r)           (SMEM_ROW_KV == memRowType(r))
C
Cary Xu 已提交
683
#define isUtilizeKVRow(k, d) ((k) < ((d)*KVRatioConvert))
C
Cary Xu 已提交
684

C
Cary Xu 已提交
685 686
#define memRowDataBody(r) POINTER_SHIFT(r, TD_MEM_ROW_TYPE_SIZE)  // section after flag
#define memRowKvBody(r) \
C
Cary Xu 已提交
687 688 689
  POINTER_SHIFT(r, TD_MEM_ROW_KV_TYPE_VER_SIZE)  // section after flag + sversion as to reuse SKVRow

#define memRowDataLen(r) (*(TDRowLenT *)memRowDataBody(r))  //  0~65535
S
Shengliang Guan 已提交
690
#define memRowKvLen(r)   (*(TDRowLenT *)memRowKvBody(r))    //  0~65535
C
Cary Xu 已提交
691

K
kailixu 已提交
692
#define memRowDataTLen(r) \
C
Cary Xu 已提交
693
  ((TDRowLenT)(memRowDataLen(r) + TD_MEM_ROW_TYPE_SIZE))  // using uint32_t/int32_t to store the TLen
C
Cary Xu 已提交
694

C
Cary Xu 已提交
695
#define memRowKvTLen(r) ((TDRowLenT)(memRowKvLen(r) + TD_MEM_ROW_KV_TYPE_VER_SIZE))
C
Cary Xu 已提交
696

S
Shengliang Guan 已提交
697
#define memRowLen(r)  (isDataRow(r) ? memRowDataLen(r) : memRowKvLen(r))
C
Cary Xu 已提交
698
#define memRowTLen(r) (isDataRow(r) ? memRowDataTLen(r) : memRowKvTLen(r))  // using uint32_t/int32_t to store the TLen
C
Cary Xu 已提交
699

700 701 702 703 704 705 706 707
static FORCE_INLINE char *memRowEnd(SMemRow row) {
  if (isDataRow(row)) {
    return (char *)dataRowEnd(memRowDataBody(row));
  } else {
    return (char *)kvRowEnd(memRowKvBody(row));
  }
}

S
Shengliang Guan 已提交
708 709 710
#define memRowDataVersion(r)     dataRowVersion(memRowDataBody(r))
#define memRowKvVersion(r)       (*(int16_t *)POINTER_SHIFT(r, TD_MEM_ROW_TYPE_SIZE))
#define memRowVersion(r)         (isDataRow(r) ? memRowDataVersion(r) : memRowKvVersion(r))  // schema version
C
Cary Xu 已提交
711
#define memRowSetKvVersion(r, v) (memRowKvVersion(r) = (v))
S
Shengliang Guan 已提交
712
#define memRowTuple(r)           (isDataRow(r) ? dataRowTuple(memRowDataBody(r)) : kvRowValues(memRowKvBody(r)))
C
Cary Xu 已提交
713

C
Cary Xu 已提交
714
#define memRowTKey(r) (isDataRow(r) ? dataRowTKey(memRowDataBody(r)) : kvRowTKey(memRowKvBody(r)))
S
Shengliang Guan 已提交
715
#define memRowKey(r)  (isDataRow(r) ? dataRowKey(memRowDataBody(r)) : kvRowKey(memRowKvBody(r)))
H
Hongze Cheng 已提交
716
#define memRowKeys(r) (isDataRow(r) ? dataRowTuple(memRowDataBody(r)) : kvRowKeys(memRowKvBody(r)))
C
Cary Xu 已提交
717 718 719 720 721 722 723 724
#define memRowSetTKey(r, k)                 \
  do {                                      \
    if (isDataRow(r)) {                     \
      dataRowTKey(memRowDataBody(r)) = (k); \
    } else {                                \
      kvRowTKey(memRowKvBody(r)) = (k);     \
    }                                       \
  } while (0)
C
Cary Xu 已提交
725

S
Shengliang Guan 已提交
726 727 728
#define memRowSetLen(r, l)          (isDataRow(r) ? memRowDataLen(r) = (l) : memRowKvLen(r) = (l))
#define memRowSetVersion(r, v)      (isDataRow(r) ? dataRowSetVersion(memRowDataBody(r), v) : memRowSetKvVersion(r, v))
#define memRowCpy(dst, r)           memcpy((dst), (r), memRowTLen(r))
C
Cary Xu 已提交
729
#define memRowMaxBytesFromSchema(s) (schemaTLen(s) + TD_MEM_ROW_DATA_HEAD_SIZE)
S
Shengliang Guan 已提交
730
#define memRowDeleted(r)            TKEY_IS_DELETED(memRowTKey(r))
C
Cary Xu 已提交
731

C
Cary Xu 已提交
732
SMemRow tdMemRowDup(SMemRow row);
733 734
void    tdAppendMemRowToDataCol(SMemRow row, STSchema *pSchema, SDataCols *pCols, bool forceSetNull);

C
Cary Xu 已提交
735
// NOTE: offset here including the header size
736
static FORCE_INLINE void *tdGetMemRowDataOfCol(void *row, int16_t colId, int8_t colType, uint16_t offset) {
C
Cary Xu 已提交
737
  if (isDataRow(row)) {
738
    return tdGetRowDataOfCol(memRowDataBody(row), colType, offset);
C
Cary Xu 已提交
739
  } else {
740
    return tdGetKVRowValOfCol(memRowKvBody(row), colId);
C
Cary Xu 已提交
741 742 743
  }
}

744 745 746 747 748 749 750 751 752 753 754 755 756 757
/**
 * NOTE:
 *  1. Applicable to scan columns one by one
 *  2. offset here including the header size
 */
static FORCE_INLINE void *tdGetMemRowDataOfColEx(void *row, int16_t colId, int8_t colType, int32_t offset,
                                                 int32_t *kvNIdx) {
  if (isDataRow(row)) {
    return tdGetRowDataOfCol(memRowDataBody(row), colType, offset);
  } else {
    return tdGetKVRowValOfColEx(memRowKvBody(row), colId, kvNIdx);
  }
}

S
Shengliang Guan 已提交
758
static FORCE_INLINE int32_t tdAppendMemRowColVal(SMemRow row, const void *value, bool isCopyVarData, int16_t colId,
759
                                             int8_t type, int32_t offset) {
760
  if (isDataRow(row)) {
761
    tdAppendDataColVal(memRowDataBody(row), value, isCopyVarData, type, offset);
762
  } else {
763
    tdAppendKvColVal(memRowKvBody(row), value, isCopyVarData, colId, type, offset);
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
  }
  return 0;
}

// make sure schema->flen appended for SDataRow
static FORCE_INLINE int32_t tdGetColAppendLen(uint8_t rowType, const void *value, int8_t colType) {
  int32_t len = 0;
  if (IS_VAR_DATA_TYPE(colType)) {
    len += varDataTLen(value);
    if (rowType == SMEM_ROW_KV) {
      len += sizeof(SColIdx);
    }
  } else {
    if (rowType == SMEM_ROW_KV) {
      len += TYPE_BYTES[colType];
      len += sizeof(SColIdx);
    }
  }
  return len;
}

typedef struct {
  int16_t colId;
  uint8_t colType;
H
Hongze Cheng 已提交
788
  char *  colVal;
789 790
} SColInfo;

H
Hongze Cheng 已提交
791
static FORCE_INLINE void setSColInfo(SColInfo *colInfo, int16_t colId, uint8_t colType, char *colVal) {
792 793 794 795 796 797
  colInfo->colId = colId;
  colInfo->colType = colType;
  colInfo->colVal = colVal;
}

SMemRow mergeTwoMemRows(void *buffer, SMemRow row1, SMemRow row2, STSchema *pSchema1, STSchema *pSchema2);
C
Cary Xu 已提交
798
#endif
799

H
more  
hzcheng 已提交
800 801 802 803
#ifdef __cplusplus
}
#endif

H
Hongze Cheng 已提交
804
#endif /*_TD_COMMON_DATA_FORMAT_H_*/