trow.h 12.7 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

X
Xiaoyu Wang 已提交
33
typedef struct STSRow {
H
refact  
Hongze Cheng 已提交
34 35 36 37 38 39 40
  TSKEY ts;
  union {
    uint32_t info;
    struct {
      uint16_t type : 2;
      uint16_t del : 1;
      uint16_t endian : 1;
41
      uint16_t statis : 1;  // 0 all normal, 1 has null or none
42
      uint16_t reserve : 11;
H
refact  
Hongze Cheng 已提交
43 44 45 46 47 48 49
      uint16_t sver;
    };
  };
  uint32_t len;
  char     data[];
} STSRow;

H
Haojun Liao 已提交
50 51 52 53 54 55 56 57 58
// 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 已提交
59 60
 * @brief value type
 *  - for data from client input and STSRow in memory, 3 types of value none/null/norm available
H
Haojun Liao 已提交
61
 */
C
Cary Xu 已提交
62
#define TD_VTYPE_NORM 0x00U  // normal val: not none, not null
H
Haojun Liao 已提交
63
#define TD_VTYPE_NULL 0x01U  // null val
C
Cary Xu 已提交
64 65
#define TD_VTYPE_NONE 0x02U  // none or unknown/undefined
#define TD_VTYPE_MAX  0x03U  //
H
Haojun Liao 已提交
66

C
Cary Xu 已提交
67 68 69 70 71 72
#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 已提交
73

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

H
Hongze Cheng 已提交
77
#define TD_COL_ROWS_NORM(c)          ((c)->bitmap == TD_ROWS_ALL_NORM)  // all rows of SDataCol/SBlockCol is NORM
H
Haojun Liao 已提交
78
#define TD_SET_COL_ROWS_BTIMAP(c, v) ((c)->bitmap = (v))
H
Hongze Cheng 已提交
79 80
#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 已提交
81

H
Hongze Cheng 已提交
82
#define KvConvertRatio            (0.9f)
H
Haojun Liao 已提交
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 116
#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 已提交
117
  void     *val;
H
Haojun Liao 已提交
118
} SCellVal;
H
Hongze Cheng 已提交
119

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

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

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

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

  // extended info
144 145 146 147 148 149 150 151 152
  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;
C
Cary Xu 已提交
153 154
  bool     hasNone;
  bool     hasNull;
H
Haojun Liao 已提交
155 156
} SRowBuilder;

C
Cary Xu 已提交
157
#define TD_ROW_HEAD_LEN  (sizeof(STSRow))
H
Haojun Liao 已提交
158 159
#define TD_ROW_NCOLS_LEN (sizeof(col_id_t))

H
Hongze Cheng 已提交
160 161 162 163 164 165 166 167 168
#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 已提交
169
// #define TD_ROW_VER(r)      ((r)->ver)
C
Cary Xu 已提交
170
#define TD_ROW_KEY_ADDR(r) (r)
H
Haojun Liao 已提交
171

X
Xiaoyu Wang 已提交
172
// N.B. If without STSchema, insGetExtendedRowSize() is used to get the rowMaxBytes and
S
Shengliang Guan 已提交
173
// (int32_t)ceil((double)nCols/TD_VTYPE_PARTS) should be added if TD_SUPPORT_BITMAP defined.
H
Hongze Cheng 已提交
174
#define TD_ROW_MAX_BYTES_FROM_SCHEMA(s) ((s)->tlen + TD_ROW_HEAD_LEN)
H
Haojun Liao 已提交
175

C
Cary Xu 已提交
176 177 178 179 180
#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 已提交
181 182 183
#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 已提交
184 185 186 187
#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 已提交
188

C
Cary Xu 已提交
189
#define TD_BOOL_STR(b)       ((b) ? "true" : "false")
H
Haojun Liao 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#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 已提交
207

C
Cary Xu 已提交
208 209 210 211 212 213 214 215
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 已提交
216

H
Haojun Liao 已提交
217 218 219 220
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 已提交
221
void                        tdMergeBitmap(uint8_t *srcBitmap, int32_t nBits, uint8_t *dstBitmap);
H
Haojun Liao 已提交
222
static FORCE_INLINE void    tdRowCopy(void *dst, STSRow *row) { memcpy(dst, row, TD_ROW_LEN(row)); }
C
Cary Xu 已提交
223 224 225
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 已提交
226 227 228
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);
H
Hongze Cheng 已提交
229 230 231 232
// int32_t tdAppendValToDataCol(SDataCol *pCol, TDRowValT valType, const void *val, int32_t numOfRows, int32_t
// maxPoints,
//                              int8_t bitmapMode, bool isMerge);
// int32_t tdAppendSTSRowToDataCol(STSRow *pRow, STSchema *pSchema, SDataCols *pCols, bool isMerge);
H
Hongze Cheng 已提交
233 234 235 236

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 已提交
237 238 239 240 241 242 243 244 245 246

/**
 * @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.
H
Hongze Cheng 已提交
247
  return POINTER_SHIFT(TD_ROW_DATA(pRow), flen - sizeof(TSKEY));
H
Haojun Liao 已提交
248 249 250 251 252
  // 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
C
Cary Xu 已提交
253
  return POINTER_SHIFT(TD_ROW_COL_IDX(pRow), (--nKvCols) * sizeof(SKvRowIdx));
H
Haojun Liao 已提交
254
}
H
Hongze Cheng 已提交
255 256 257
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);
C
Cary Xu 已提交
258
// bool    tdIsBitmapValTypeNorm(const void *pBitmap, int16_t idx, int8_t bitmapMode);
H
Hongze Cheng 已提交
259
int32_t tdGetBitmapValType(const void *pBitmap, int16_t colIdx, TDRowValT *pValType, int8_t bitmapMode);
C
Cary Xu 已提交
260

H
Haojun Liao 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
// ----------------- 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 已提交
288 289 290 291 292
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);
C
Cary Xu 已提交
293 294
static FORCE_INLINE void tdSRowEnd(SRowBuilder *pBuilder) {
  STSRow *pRow = (STSRow *)pBuilder->pBuf;
C
Cary Xu 已提交
295
  if (pBuilder->hasNull || pBuilder->hasNone) {
C
Cary Xu 已提交
296 297 298
    pRow->statis = 1;
  }
}
H
Hongze Cheng 已提交
299 300 301 302 303 304 305 306 307 308 309
int32_t tdSRowGetBuf(SRowBuilder *pBuilder, void *pBuf);
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 已提交
310
void    tTSRowGetVal(STSRow *pRow, STSchema *pTSchema, int16_t iCol, SColVal *pColVal);
H
Haojun Liao 已提交
311 312 313

typedef struct {
  STSchema *pSchema;
L
fix  
Liu Jicong 已提交
314 315
  STSRow   *pRow;
  void     *pBitmap;
H
Haojun Liao 已提交
316 317 318 319 320 321
  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;

C
Cary Xu 已提交
322 323 324 325 326
void tdSTSRowIterInit(STSRowIter *pIter, STSchema *pSchema);
void tdSTSRowIterReset(STSRowIter *pIter, STSRow *pRow);
bool tdSTSRowIterFetch(STSRowIter *pIter, col_id_t colId, col_type_t colType, SCellVal *pVal);
bool tdSTSRowIterNext(STSRowIter *pIter, SCellVal *pVal);

327
int32_t tdSTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow **ppRow);
C
Cary Xu 已提交
328
bool    tdSTSRowGetVal(STSRowIter *pIter, col_id_t colId, col_type_t colType, SCellVal *pVal);
H
Hongze Cheng 已提交
329
void    tdSRowPrint(STSRow *row, STSchema *pSchema, const char *tag);
H
refact  
Hongze Cheng 已提交
330

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

C
Cary Xu 已提交
335
#endif /*_TD_COMMON_ROW_H_*/