trow.h 13.2 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16 17
#ifndef _TD_COMMON_ROW_H_
#define _TD_COMMON_ROW_H_
H
refact  
Hongze Cheng 已提交
18

H
Hongze Cheng 已提交
19
#include "os.h"
H
Haojun Liao 已提交
20
#include "talgo.h"
S
Shengliang Guan 已提交
21
#include "taosdef.h"
H
Haojun Liao 已提交
22
#include "taoserror.h"
H
more  
Hongze Cheng 已提交
23
#include "tbuffer.h"
H
Haojun Liao 已提交
24
#include "tdataformat.h"
S
Shengliang Guan 已提交
25
#include "tdef.h"
H
Haojun Liao 已提交
26 27
#include "ttypes.h"
#include "tutil.h"
H
Hongze Cheng 已提交
28

H
refact  
Hongze Cheng 已提交
29 30 31 32
#ifdef __cplusplus
extern "C" {
#endif

H
refact  
Hongze Cheng 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
typedef struct {
  TSKEY ts;
  union {
    uint32_t info;
    struct {
      uint16_t type : 2;
      uint16_t del : 1;
      uint16_t endian : 1;
      uint16_t reserve : 12;
      uint16_t sver;
    };
  };
  uint32_t len;
  char     data[];
} STSRow;

H
Haojun Liao 已提交
49 50 51 52 53 54 55 56 57
// Target of tdataformat.h:
// 1. Row related definition in dataformat.h of 2.0 could be replaced with tdataformat.h of 3.0.
// 2. The basic definition in dataformat.h is shared with tdataformat.h of 3.0.

// row type
#define TD_ROW_TP 0x0U  // default
#define TD_ROW_KV 0x01U

/**
C
Cary Xu 已提交
58 59
 * @brief value type
 *  - for data from client input and STSRow in memory, 3 types of value none/null/norm available
H
Haojun Liao 已提交
60
 */
C
Cary Xu 已提交
61
#define TD_VTYPE_NORM 0x00U  // normal val: not none, not null
H
Haojun Liao 已提交
62
#define TD_VTYPE_NULL 0x01U  // null val
C
Cary Xu 已提交
63 64
#define TD_VTYPE_NONE 0x02U  // none or unknown/undefined
#define TD_VTYPE_MAX  0x03U  //
H
Haojun Liao 已提交
65

C
Cary Xu 已提交
66 67 68 69 70 71
#define TD_VTYPE_NORM_BYTE_I 0x0U
#define TD_VTYPE_NULL_BYTE_I 0xFFU

#define TD_VTYPE_NORM_BYTE_II 0x0U
#define TD_VTYPE_NULL_BYTE_II 0x55U
#define TD_VTYPE_NONE_BYTE_II 0xAAU
H
Haojun Liao 已提交
72

C
Cary Xu 已提交
73 74
#define TD_ROWS_ALL_NORM  0x00U
#define TD_ROWS_NULL_NORM 0x01U
H
Haojun Liao 已提交
75

H
Hongze Cheng 已提交
76
#define TD_COL_ROWS_NORM(c)          ((c)->bitmap == TD_ROWS_ALL_NORM)  // all rows of SDataCol/SBlockCol is NORM
H
Haojun Liao 已提交
77
#define TD_SET_COL_ROWS_BTIMAP(c, v) ((c)->bitmap = (v))
H
Hongze Cheng 已提交
78 79
#define TD_SET_COL_ROWS_NORM(c)      TD_SET_COL_ROWS_BTIMAP((c), TD_ROWS_ALL_NORM)
#define TD_SET_COL_ROWS_MISC(c)      TD_SET_COL_ROWS_BTIMAP((c), TD_ROWS_NULL_NORM)
H
Haojun Liao 已提交
80

H
Hongze Cheng 已提交
81
#define KvConvertRatio            (0.9f)
H
Haojun Liao 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#define isSelectKVRow(klen, tlen) ((klen) < ((tlen)*KvConvertRatio))

#ifdef TD_SUPPORT_BITMAP
static FORCE_INLINE bool tdValTypeIsNone(TDRowValT valType) { return (valType & 0x03U) == TD_VTYPE_NONE; }
static FORCE_INLINE bool tdValTypeIsNull(TDRowValT valType) { return (valType & 0x03U) == TD_VTYPE_NULL; }
static FORCE_INLINE bool tdValTypeIsNorm(TDRowValT valType) { return (valType & 0x03U) == TD_VTYPE_NORM; }
#endif

static FORCE_INLINE bool tdValIsNorm(TDRowValT valType, const void *val, int32_t colType) {
#ifdef TD_SUPPORT_BITMAP
  return tdValTypeIsNorm(valType);
#else
  return !isNull(val, colType);
#endif
}

static FORCE_INLINE bool tdValIsNone(TDRowValT valType) {
#ifdef TD_SUPPORT_BITMAP
  return tdValTypeIsNone(valType);
#else
  return false;
#endif
}

static FORCE_INLINE bool tdValIsNull(TDRowValT valType, const void *val, int32_t colType) {
#ifdef TD_SUPPORT_BITMAP
  return tdValTypeIsNull(valType);
#else
  return isNull(val, colType);
#endif
}

typedef struct {
  TDRowValT valType;
L
fix  
Liu Jicong 已提交
116
  void     *val;
H
Haojun Liao 已提交
117
} SCellVal;
H
Hongze Cheng 已提交
118

H
more  
Hongze Cheng 已提交
119
typedef struct {
H
more  
Hongze Cheng 已提交
120
  // TODO
C
Cary Xu 已提交
121 122
  int tmp;  // TODO: to avoid compile error
} STpRow;   // tuple
H
Hongze Cheng 已提交
123

H
Haojun Liao 已提交
124
#pragma pack(push, 1)
H
more  
Hongze Cheng 已提交
125
typedef struct {
H
Haojun Liao 已提交
126
  col_id_t colId;
H
more  
Hongze Cheng 已提交
127 128
  uint32_t offset;
} SKvRowIdx;
H
Haojun Liao 已提交
129
#pragma pack(pop)
H
Hongze Cheng 已提交
130

H
more  
Hongze Cheng 已提交
131 132 133 134 135
typedef struct {
  uint16_t  ncols;
  SKvRowIdx cidx[];
} SKvRow;

H
Haojun Liao 已提交
136 137
typedef struct {
  // basic info
138 139 140
  int8_t       rowType;
  schema_ver_t sver;
  STSRow      *pBuf;
H
Haojun Liao 已提交
141 142

  // extended info
143 144 145 146 147 148 149 150 151
  int32_t  flen;
  col_id_t nBoundCols;
  col_id_t nCols;
  col_id_t nBitmaps;
  col_id_t nBoundBitmaps;
  int32_t  offset;
  void    *pBitmap;
  void    *pOffset;
  int32_t  extendedRowSize;
H
Haojun Liao 已提交
152 153
} SRowBuilder;

C
Cary Xu 已提交
154
#define TD_ROW_HEAD_LEN  (sizeof(STSRow))
H
Haojun Liao 已提交
155 156
#define TD_ROW_NCOLS_LEN (sizeof(col_id_t))

H
Hongze Cheng 已提交
157 158 159 160 161 162 163 164 165
#define TD_ROW_INFO(r)   ((r)->info)
#define TD_ROW_TYPE(r)   ((r)->type)
#define TD_ROW_DELETE(r) ((r)->del)
#define TD_ROW_ENDIAN(r) ((r)->endian)
#define TD_ROW_SVER(r)   ((r)->sver)
#define TD_ROW_NCOLS(r)  ((r)->data)  // only valid for SKvRow
#define TD_ROW_DATA(r)   ((r)->data)
#define TD_ROW_LEN(r)    ((r)->len)
#define TD_ROW_KEY(r)    ((r)->ts)
C
Cary Xu 已提交
166
// #define TD_ROW_VER(r)      ((r)->ver)
C
Cary Xu 已提交
167
#define TD_ROW_KEY_ADDR(r) (r)
H
Haojun Liao 已提交
168 169

// N.B. If without STSchema, getExtendedRowSize() is used to get the rowMaxBytes and
S
Shengliang Guan 已提交
170
// (int32_t)ceil((double)nCols/TD_VTYPE_PARTS) should be added if TD_SUPPORT_BITMAP defined.
C
Cary Xu 已提交
171
#define TD_ROW_MAX_BYTES_FROM_SCHEMA(s) (schemaTLen(s) + TD_ROW_HEAD_LEN)
H
Haojun Liao 已提交
172

C
Cary Xu 已提交
173 174 175 176 177
#define TD_ROW_SET_INFO(r, i)  (TD_ROW_INFO(r) = (i))
#define TD_ROW_SET_TYPE(r, t)  (TD_ROW_TYPE(r) = (t))
#define TD_ROW_SET_DELETE(r)   (TD_ROW_DELETE(r) = 1)
#define TD_ROW_SET_SVER(r, v)  (TD_ROW_SVER(r) = (v))
#define TD_ROW_SET_LEN(r, l)   (TD_ROW_LEN(r) = (l))
H
Haojun Liao 已提交
178 179 180
#define TD_ROW_SET_NCOLS(r, n) (*(col_id_t *)TD_ROW_NCOLS(r) = (n))

#define TD_ROW_IS_DELETED(r) (TD_ROW_DELETE(r) == 1)
C
Cary Xu 已提交
181 182 183 184
#define TD_IS_TP_ROW(r)      (TD_ROW_TYPE(r) == TD_ROW_TP)
#define TD_IS_KV_ROW(r)      (TD_ROW_TYPE(r) == TD_ROW_KV)
#define TD_IS_TP_ROW_T(t)    ((t) == TD_ROW_TP)
#define TD_IS_KV_ROW_T(t)    ((t) == TD_ROW_KV)
H
Haojun Liao 已提交
185

C
Cary Xu 已提交
186
#define TD_BOOL_STR(b)       ((b) ? "true" : "false")
H
Haojun Liao 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
#define isUtilizeKVRow(k, d) ((k) < ((d)*KVRatioConvert))

#define TD_ROW_COL_IDX(r) POINTER_SHIFT(TD_ROW_DATA(r), sizeof(col_id_t))

static FORCE_INLINE void tdRowSetVal(SCellVal *pVal, uint8_t valType, void *val) {
  pVal->valType = valType;
  pVal->val = val;
}
static FORCE_INLINE col_id_t    tdRowGetNCols(STSRow *pRow) { return *(col_id_t *)TD_ROW_NCOLS(pRow); }
static FORCE_INLINE void        tdRowCpy(void *dst, const STSRow *pRow) { memcpy(dst, pRow, TD_ROW_LEN(pRow)); }
static FORCE_INLINE const char *tdRowEnd(STSRow *pRow) { return (const char *)POINTER_SHIFT(pRow, TD_ROW_LEN(pRow)); }

STSRow *tdRowDup(STSRow *row);

static FORCE_INLINE SKvRowIdx *tdKvRowColIdxAt(STSRow *pRow, col_id_t idx) {
  return (SKvRowIdx *)TD_ROW_COL_IDX(pRow) + idx;
}
D
stmt  
dapan1121 已提交
204

C
Cary Xu 已提交
205 206 207 208 209 210 211 212
static FORCE_INLINE int16_t tdKvRowColIdAt(STSRow *pRow, col_id_t idx) {
  ASSERT(idx >= 0);
  if (idx == 0) {
    return PRIMARYKEY_TIMESTAMP_COL_ID;
  }

  return ((SKvRowIdx *)TD_ROW_COL_IDX(pRow) + idx - 1)->colId;
}
D
stmt  
dapan1121 已提交
213

H
Haojun Liao 已提交
214 215 216 217
static FORCE_INLINE void *tdKVRowColVal(STSRow *pRow, SKvRowIdx *pIdx) { return POINTER_SHIFT(pRow, pIdx->offset); }

#define TD_ROW_OFFSET(p) ((p)->toffset);  // During ParseInsert when without STSchema, how to get the offset for STpRow?

C
Cary Xu 已提交
218
void                        tdMergeBitmap(uint8_t *srcBitmap, int32_t nBits, uint8_t *dstBitmap);
H
Haojun Liao 已提交
219
static FORCE_INLINE void    tdRowCopy(void *dst, STSRow *row) { memcpy(dst, row, TD_ROW_LEN(row)); }
C
Cary Xu 已提交
220 221 222
static FORCE_INLINE int32_t tdSetBitmapValTypeII(void *pBitmap, int16_t colIdx, TDRowValT valType);
static FORCE_INLINE int32_t tdSetBitmapValType(void *pBitmap, int16_t colIdx, TDRowValT valType, int8_t bitmapMode);
int32_t                     tdSetBitmapValTypeN(void *pBitmap, int16_t nEle, TDRowValT valType, int8_t bitmapMode);
C
Cary Xu 已提交
223 224 225
static FORCE_INLINE int32_t tdGetBitmapValType(const void *pBitmap, int16_t colIdx, TDRowValT *pValType,
                                               int8_t bitmapMode);
bool                        tdIsBitmapBlkNorm(const void *pBitmap, int32_t numOfBits, int8_t bitmapMode);
C
Cary Xu 已提交
226
int32_t tdAppendValToDataCol(SDataCol *pCol, TDRowValT valType, const void *val, int32_t numOfRows, int32_t maxPoints,
C
Cary Xu 已提交
227
                             int8_t bitmapMode, bool isMerge);
H
Hongze Cheng 已提交
228 229 230 231 232
int32_t tdAppendSTSRowToDataCol(STSRow *pRow, STSchema *pSchema, SDataCols *pCols, bool isMerge);

int32_t tdGetBitmapValTypeII(const void *pBitmap, int16_t colIdx, TDRowValT *pValType);
int32_t tdSetBitmapValTypeI(void *pBitmap, int16_t colIdx, TDRowValT valType);
int32_t tdGetBitmapValTypeI(const void *pBitmap, int16_t colIdx, TDRowValT *pValType);
H
Haojun Liao 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

/**
 * @brief
 *
 * @param pRow
 * @param flen flen in STSchema
 * @return FORCE_INLINE*
 */
static FORCE_INLINE void *tdGetBitmapAddrTp(STSRow *pRow, uint32_t flen) {
  // The primary TS key is stored separatedly.
  return POINTER_SHIFT(TD_ROW_DATA(pRow), flen - sizeof(TSKEY));
  // return POINTER_SHIFT(pRow->ts, flen);
}

static FORCE_INLINE void *tdGetBitmapAddrKv(STSRow *pRow, col_id_t nKvCols) {
  // The primary TS key is stored separatedly and is Norm value, thus should minus 1 firstly
  return POINTER_SHIFT(TD_ROW_COL_IDX(pRow), (--nKvCols) * sizeof(SKvRowIdx));
}
H
Hongze Cheng 已提交
251 252 253 254 255
void   *tdGetBitmapAddr(STSRow *pRow, uint8_t rowType, uint32_t flen, col_id_t nKvCols);
int32_t tdSetBitmapValType(void *pBitmap, int16_t colIdx, TDRowValT valType, int8_t bitmapMode);
int32_t tdSetBitmapValTypeII(void *pBitmap, int16_t colIdx, TDRowValT valType);
bool    tdIsBitmapValTypeNorm(const void *pBitmap, int16_t idx, int8_t bitmapMode);
int32_t tdGetBitmapValType(const void *pBitmap, int16_t colIdx, TDRowValT *pValType, int8_t bitmapMode);
C
Cary Xu 已提交
256

H
Haojun Liao 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
// ----------------- Tuple row structure(STpRow)
/*
 * |<----------------------------- tlen ---------------------------------->|
 * |<---------   flen  ------------->|<-- blen  -->|                       |
 * +---------------------------------+-------------+-----------------------+
 * |                                 |             |                       |
 * +---------------------------------+-------------------------------------+
 * |           first part            |   bitmap    |    second part        |
 * +---------------------------------+-------------+-----------------------+
 *
 */

// -----------------  K-V row structure(SKvRow)
/*
 * |<--------- colsIdxLen ---------->|<-- blen -->|                        |
 * +---------------------------------+------------+------------------------+
 * |                                 |            |                        |
 * +-----------------------------------------------------------------------+
 * |           cols index            |   bitmap   |     data part          |
 * +---------------------------------+------------+------------------------+
 *
 */

static FORCE_INLINE void tdSRowInit(SRowBuilder *pBuilder, int16_t sver) {
  pBuilder->rowType = TD_ROW_TP;  // default STpRow
  pBuilder->sver = sver;
}
H
Hongze Cheng 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
int32_t tdSRowSetInfo(SRowBuilder *pBuilder, int32_t nCols, int32_t nBoundCols, int32_t flen);
int32_t tdSRowSetTpInfo(SRowBuilder *pBuilder, int32_t nCols, int32_t flen);
int32_t tdSRowSetExtendedInfo(SRowBuilder *pBuilder, int32_t nCols, int32_t nBoundCols, int32_t flen,
                              int32_t allNullLen, int32_t boundNullLen);
int32_t tdSRowResetBuf(SRowBuilder *pBuilder, void *pBuf);
int32_t tdSRowGetBuf(SRowBuilder *pBuilder, void *pBuf);
int32_t tdSRowInitEx(SRowBuilder *pBuilder, void *pBuf, uint32_t allNullLen, uint32_t boundNullLen, int32_t nCols,
                     int32_t nBoundCols, int32_t flen);
void    tdSRowReset(SRowBuilder *pBuilder);
int32_t tdAppendColValToTpRow(SRowBuilder *pBuilder, TDRowValT valType, const void *val, bool isCopyVarData,
                              int8_t colType, int16_t colIdx, int32_t offset);
int32_t tdAppendColValToKvRow(SRowBuilder *pBuilder, TDRowValT valType, const void *val, bool isCopyVarData,
                              int8_t colType, int16_t colIdx, int32_t offset, col_id_t colId);
int32_t tdAppendColValToRow(SRowBuilder *pBuilder, col_id_t colId, int8_t colType, TDRowValT valType, const void *val,
                            bool isCopyVarData, int32_t offset, col_id_t colIdx);
int32_t tdGetTpRowValOfCol(SCellVal *output, STSRow *pRow, void *pBitmap, int8_t colType, int32_t offset,
                           int16_t colIdx);
int32_t tdGetKvRowValOfCol(SCellVal *output, STSRow *pRow, void *pBitmap, int32_t offset, int16_t colIdx);
H
Hongze Cheng 已提交
302
void    tTSRowGetVal(STSRow *pRow, STSchema *pTSchema, int16_t iCol, SColVal *pColVal);
H
Haojun Liao 已提交
303 304 305

typedef struct {
  STSchema *pSchema;
L
fix  
Liu Jicong 已提交
306 307
  STSRow   *pRow;
  void     *pBitmap;
H
Haojun Liao 已提交
308 309 310 311 312 313
  uint32_t  offset;
  col_id_t  maxColId;
  col_id_t  colIdx;  // [PRIMARYKEY_TIMESTAMP_COL_ID, nSchemaCols], PRIMARYKEY_TIMESTAMP_COL_ID equals 1
  col_id_t  kvIdx;   // [0, nKvCols)
} STSRowIter;

H
Hongze Cheng 已提交
314 315
void    tdSTSRowIterReset(STSRowIter *pIter, STSRow *pRow);
void    tdSTSRowIterInit(STSRowIter *pIter, STSchema *pSchema);
316
int32_t tdSTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow **ppRow);
H
Hongze Cheng 已提交
317 318 319 320
bool    tdSTSRowGetVal(STSRowIter *pIter, col_id_t colId, col_type_t colType, SCellVal *pVal);
bool    tdGetTpRowDataOfCol(STSRowIter *pIter, col_type_t colType, int32_t offset, SCellVal *pVal);
bool    tdGetKvRowValOfColEx(STSRowIter *pIter, col_id_t colId, col_type_t colType, col_id_t *nIdx, SCellVal *pVal);
bool    tdSTSRowIterNext(STSRowIter *pIter, col_id_t colId, col_type_t colType, SCellVal *pVal);
H
Haojun Liao 已提交
321
STSRow *mergeTwoRows(void *buffer, STSRow *row1, STSRow *row2, STSchema *pSchema1, STSchema *pSchema2);
H
Hongze Cheng 已提交
322 323 324
int32_t tdGetColDataOfRow(SCellVal *pVal, SDataCol *pCol, int32_t row, int8_t bitmapMode);
bool    tdSTpRowGetVal(STSRow *pRow, col_id_t colId, col_type_t colType, int32_t flen, uint32_t offset, col_id_t colIdx,
                       SCellVal *pVal);
C
Cary Xu 已提交
325
bool    tdSKvRowGetVal(STSRow *pRow, col_id_t colId, col_id_t colIdx, SCellVal *pVal);
H
Hongze Cheng 已提交
326 327 328
int32_t dataColGetNEleLen(SDataCol *pDataCol, int32_t rows, int8_t bitmapMode);
void    tdSCellValPrint(SCellVal *pVal, int8_t colType);
void    tdSRowPrint(STSRow *row, STSchema *pSchema, const char *tag);
H
refact  
Hongze Cheng 已提交
329

H
refact  
Hongze Cheng 已提交
330 331 332 333
#ifdef __cplusplus
}
#endif

334
#endif /*_TD_COMMON_ROW_H_*/