tdatablock.h 11.0 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#ifndef _TD_COMMON_EP_H_
#define _TD_COMMON_EP_H_

S
common  
Shengliang Guan 已提交
19
#include "tcommon.h"
L
Liu Jicong 已提交
20
#include "tcompression.h"
S
Shengliang Guan 已提交
21
#include "tmsg.h"
22

H
Haojun Liao 已提交
23 24 25 26
#ifdef __cplusplus
extern "C" {
#endif

H
Haojun Liao 已提交
27
typedef struct SBlockOrderInfo {
H
Haojun Liao 已提交
28
  bool             nullFirst;
H
Haojun Liao 已提交
29
  int32_t          order;
H
Haojun Liao 已提交
30
  int32_t          slotId;
S
Shengliang Guan 已提交
31
  SColumnInfoData* pColData;
H
Haojun Liao 已提交
32 33
} SBlockOrderInfo;

H
Haojun Liao 已提交
34
#define NBIT                     (3u)
H
Haojun Liao 已提交
35
#define BitPos(_n)               ((_n) & ((1 << NBIT) - 1))
H
Haojun Liao 已提交
36 37
#define BMCharPos(bm_, r_)       ((bm_)[(r_) >> NBIT])
#define colDataIsNull_f(bm_, r_) ((BMCharPos(bm_, r_) & (1u << (7u - BitPos(r_)))) == (1u << (7u - BitPos(r_))))
38

H
Haojun Liao 已提交
39 40 41 42
#define colDataSetNull_f(bm_, r_)                    \
  do {                                               \
    BMCharPos(bm_, r_) |= (1u << (7u - BitPos(r_))); \
  } while (0)
H
Haojun Liao 已提交
43

44 45 46 47 48 49
#define colDataSetNull_f_s(c_, r_)                                        \
  do {                                                                    \
    colDataSetNull_f((c_)->nullbitmap, r_);                               \
    memset(((char*)(c_)->pData) + (c_)->info.bytes * (r_), 0, (c_)->info.bytes); \
  } while (0)

50 51 52
#define colDataClearNull_f(bm_, r_)                             \
  do {                                                          \
    BMCharPos(bm_, r_) &= ((char)(~(1u << (7u - BitPos(r_))))); \
S
shenglian zhou 已提交
53 54
  } while (0)

L
Liu Jicong 已提交
55 56
#define colDataIsNull_var(pColumnInfoData, row)  (pColumnInfoData->varmeta.offset[row] == -1)
#define colDataSetNull_var(pColumnInfoData, row) (pColumnInfoData->varmeta.offset[row] = -1)
57

58 59 60 61 62
#define BitmapLen(_n) (((_n) + ((1 << NBIT) - 1)) >> NBIT)

#define colDataGetVarData(p1_, r_) ((p1_)->pData + (p1_)->varmeta.offset[(r_)])
#define colDataGetNumData(p1_, r_) ((p1_)->pData + ((r_) * (p1_)->info.bytes))
// SColumnInfoData, rowNumber
L
Liu Jicong 已提交
63 64
#define colDataGetData(p1_, r_) \
  ((IS_VAR_DATA_TYPE((p1_)->info.type)) ? colDataGetVarData(p1_, r_) : colDataGetNumData(p1_, r_))
65

L
Liu Jicong 已提交
66 67
#define IS_JSON_NULL(type, data) \
  ((type) == TSDB_DATA_TYPE_JSON && (*(data) == TSDB_DATA_TYPE_NULL || tTagIsJsonNull(data)))
68

wmmhello's avatar
wmmhello 已提交
69
static FORCE_INLINE bool colDataIsNull_s(const SColumnInfoData* pColumnInfoData, uint32_t row) {
L
Liu Jicong 已提交
70 71 72
  if (!pColumnInfoData->hasNull) {
    return false;
  }
73

wmmhello's avatar
wmmhello 已提交
74
  if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
75
    return colDataIsNull_var(pColumnInfoData, row);
L
Liu Jicong 已提交
76 77 78 79 80 81 82 83 84
  } else {
    if (pColumnInfoData->nullbitmap == NULL) {
      return false;
    }

    return colDataIsNull_f(pColumnInfoData->nullbitmap, row);
  }
}

S
Shengliang Guan 已提交
85 86
static FORCE_INLINE bool colDataIsNull(const SColumnInfoData* pColumnInfoData, uint32_t totalRows, uint32_t row,
                                       SColumnDataAgg* pColAgg) {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  if (!pColumnInfoData->hasNull) {
    return false;
  }

  if (pColAgg != NULL) {
    if (pColAgg->numOfNull == totalRows) {
      ASSERT(pColumnInfoData->nullbitmap == NULL);
      return true;
    } else if (pColAgg->numOfNull == 0) {
      ASSERT(pColumnInfoData->nullbitmap == NULL);
      return false;
    }
  }

  if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
102
    return colDataIsNull_var(pColumnInfoData, row);
103 104 105 106 107 108 109 110 111
  } else {
    if (pColumnInfoData->nullbitmap == NULL) {
      return false;
    }

    return colDataIsNull_f(pColumnInfoData->nullbitmap, row);
  }
}

112
static FORCE_INLINE void colDataSetNULL(SColumnInfoData* pColumnInfoData, uint32_t rowIndex) {
H
Haojun Liao 已提交
113 114
  // There is a placehold for each NULL value of binary or nchar type.
  if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
115
    colDataSetNull_var(pColumnInfoData, rowIndex);  // it is a null value of VAR type.
H
Haojun Liao 已提交
116
  } else {
117
    colDataSetNull_f_s(pColumnInfoData, rowIndex);
H
Haojun Liao 已提交
118 119 120 121 122
  }

  pColumnInfoData->hasNull = true;
}

123
static FORCE_INLINE void colDataSetNNULL(SColumnInfoData* pColumnInfoData, uint32_t start, size_t nRows) {
124
  if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) {
L
Liu Jicong 已提交
125
    for (int32_t i = start; i < start + nRows; ++i) {
L
Liu Jicong 已提交
126
      colDataSetNull_var(pColumnInfoData, i);  // it is a null value of VAR type.
127 128
    }
  } else {
L
Liu Jicong 已提交
129
    for (int32_t i = start; i < start + nRows; ++i) {
130 131
      colDataSetNull_f(pColumnInfoData->nullbitmap, i);
    }
132
    memset(pColumnInfoData->pData + start * pColumnInfoData->info.bytes, 0, pColumnInfoData->info.bytes * nRows);
133 134 135 136 137
  }

  pColumnInfoData->hasNull = true;
}

138
static FORCE_INLINE void colDataSetInt8(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, int8_t* v) {
H
Haojun Liao 已提交
139 140
  ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_TINYINT ||
         pColumnInfoData->info.type == TSDB_DATA_TYPE_UTINYINT || pColumnInfoData->info.type == TSDB_DATA_TYPE_BOOL);
141
  char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex;
H
Haojun Liao 已提交
142 143 144
  *(int8_t*)p = *(int8_t*)v;
}

145
static FORCE_INLINE void colDataSetInt16(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, int16_t* v) {
L
Liu Jicong 已提交
146 147
  ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_SMALLINT ||
         pColumnInfoData->info.type == TSDB_DATA_TYPE_USMALLINT);
148
  char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex;
H
Haojun Liao 已提交
149 150 151
  *(int16_t*)p = *(int16_t*)v;
}

152
static FORCE_INLINE void colDataSetInt32(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, int32_t* v) {
H
Haojun Liao 已提交
153
  ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_INT || pColumnInfoData->info.type == TSDB_DATA_TYPE_UINT);
154
  char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex;
H
Haojun Liao 已提交
155 156 157
  *(int32_t*)p = *(int32_t*)v;
}

158
static FORCE_INLINE void colDataSetInt64(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, int64_t* v) {
159 160
  int32_t type = pColumnInfoData->info.type;
  ASSERT(type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT || type == TSDB_DATA_TYPE_TIMESTAMP);
161
  char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex;
H
Haojun Liao 已提交
162 163 164
  *(int64_t*)p = *(int64_t*)v;
}

165
static FORCE_INLINE void colDataSetFloat(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, float* v) {
H
Haojun Liao 已提交
166
  ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_FLOAT);
167
  char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex;
H
Haojun Liao 已提交
168 169 170
  *(float*)p = *(float*)v;
}

171
static FORCE_INLINE void colDataSetDouble(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, double* v) {
H
Haojun Liao 已提交
172
  ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_DOUBLE);
173
  char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * rowIndex;
H
Haojun Liao 已提交
174 175 176
  *(double*)p = *(double*)v;
}

177
int32_t getJsonValueLen(const char* data);
wmmhello's avatar
wmmhello 已提交
178

179
int32_t colDataSetVal(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, const char* pData, bool isNull);
180
int32_t colDataAppend(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, const char* pData, bool isNull);
D
dapan1121 已提交
181
int32_t colDataReassignVal(SColumnInfoData* pColumnInfoData, uint32_t dstRowIdx, uint32_t srcRowIdx, const char* pData);
D
dapan1121 已提交
182
int32_t colDataSetNItems(SColumnInfoData* pColumnInfoData, uint32_t rowIndex, const char* pData, uint32_t numOfRows, bool trimValue);
H
Haojun Liao 已提交
183 184
int32_t colDataMergeCol(SColumnInfoData* pColumnInfoData, int32_t numOfRow1, int32_t* capacity,
                        const SColumnInfoData* pSource, int32_t numOfRow2);
L
Liu Jicong 已提交
185 186
int32_t colDataAssign(SColumnInfoData* pColumnInfoData, const SColumnInfoData* pSource, int32_t numOfRows,
                      const SDataBlockInfo* pBlockInfo);
187
int32_t blockDataUpdateTsWindow(SSDataBlock* pDataBlock, int32_t tsColumnIndex);
H
Haojun Liao 已提交
188

189
int32_t colDataGetLength(const SColumnInfoData* pColumnInfoData, int32_t numOfRows);
190
int32_t colDataGetRowLength(const SColumnInfoData* pColumnInfoData, int32_t rowIdx);
191
void    colDataTrim(SColumnInfoData* pColumnInfoData);
H
Haojun Liao 已提交
192

H
Haojun Liao 已提交
193 194
size_t blockDataGetNumOfCols(const SSDataBlock* pBlock);
size_t blockDataGetNumOfRows(const SSDataBlock* pBlock);
H
Haojun Liao 已提交
195

196
int32_t blockDataMerge(SSDataBlock* pDest, const SSDataBlock* pSrc);
L
Liu Jicong 已提交
197 198
int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startIndex, int32_t* stopIndex,
                           int32_t pageSize);
H
Haojun Liao 已提交
199
int32_t blockDataToBuf(char* buf, const SSDataBlock* pBlock);
200
int32_t blockDataFromBuf(SSDataBlock* pBlock, const char* buf);
H
Haojun Liao 已提交
201
int32_t blockDataFromBuf1(SSDataBlock* pBlock, const char* buf, size_t capacity);
H
Haojun Liao 已提交
202

203 204
SSDataBlock* blockDataExtractBlock(SSDataBlock* pBlock, int32_t startIndex, int32_t rowCount);

H
Haojun Liao 已提交
205
size_t blockDataGetSize(const SSDataBlock* pBlock);
206
size_t blockDataGetRowSize(SSDataBlock* pBlock);
H
Haojun Liao 已提交
207
double blockDataGetSerialRowSize(const SSDataBlock* pBlock);
208
size_t blockDataGetSerialMetaSize(uint32_t numOfCols);
H
Haojun Liao 已提交
209

H
Haojun Liao 已提交
210
int32_t blockDataSort(SSDataBlock* pDataBlock, SArray* pOrderInfo);
H
Haojun Liao 已提交
211
int32_t blockDataSort_rv(SSDataBlock* pDataBlock, SArray* pOrderInfo, bool nullFirst);
H
Haojun Liao 已提交
212

H
Haojun Liao 已提交
213
int32_t colInfoDataEnsureCapacity(SColumnInfoData* pColumn, uint32_t numOfRows, bool clearPayload);
214
int32_t blockDataEnsureCapacity(SSDataBlock* pDataBlock, uint32_t numOfRows);
215

L
Liu Jicong 已提交
216 217
void colInfoDataCleanup(SColumnInfoData* pColumn, uint32_t numOfRows);
void blockDataCleanup(SSDataBlock* pDataBlock);
H
Haojun Liao 已提交
218
void blockDataEmpty(SSDataBlock* pDataBlock);
219

G
Ganlin Zhao 已提交
220
size_t blockDataGetCapacityInRow(const SSDataBlock* pBlock, size_t pageSize, int32_t extraSize);
221

222
int32_t blockDataTrimFirstRows(SSDataBlock* pBlock, size_t n);
wmmhello's avatar
wmmhello 已提交
223
int32_t blockDataKeepFirstNRows(SSDataBlock* pBlock, size_t n);
H
Haojun Liao 已提交
224

L
Liu Jicong 已提交
225 226
int32_t assignOneDataBlock(SSDataBlock* dst, const SSDataBlock* src);
int32_t copyDataBlock(SSDataBlock* dst, const SSDataBlock* src);
227

228
SSDataBlock* createDataBlock();
229 230 231
void*        blockDataDestroy(SSDataBlock* pBlock);
void         blockDataFreeRes(SSDataBlock* pBlock);
SSDataBlock* createOneDataBlock(const SSDataBlock* pDataBlock, bool copyData);
L
Liu Jicong 已提交
232
SSDataBlock* createSpecialDataBlock(EStreamType type);
233

234 235
SSDataBlock* blockCopyOneRow(const SSDataBlock* pDataBlock, int32_t rowIdx);
int32_t      blockDataAppendColInfo(SSDataBlock* pBlock, SColumnInfoData* pColInfoData);
236 237

SColumnInfoData  createColumnInfoData(int16_t type, int32_t bytes, int16_t colId);
L
Liu Jicong 已提交
238
SColumnInfoData* bdGetColumnInfoData(const SSDataBlock* pBlock, int32_t index);
239

H
Haojun Liao 已提交
240
int32_t blockEncode(const SSDataBlock* pBlock, char* data, int32_t numOfCols);
241
const char* blockDecode(SSDataBlock* pBlock, const char* pData);
242

243
void blockDebugShowDataBlock(SSDataBlock* pBlock, const char* flag);
S
slzhou 已提交
244
void blockDebugShowDataBlocks(const SArray* dataBlocks, const char* flag);
5
54liuyao 已提交
245 246
// for debug
char* dumpBlockData(SSDataBlock* pDataBlock, const char* flag, char** dumpBuf);
L
Liu Jicong 已提交
247

248
int32_t buildSubmitReqFromDataBlock(SSubmitReq2** pReq, const SSDataBlock* pDataBlocks, const STSchema* pTSchema, int64_t uid, int32_t vgId,
249
                                    tb_uid_t suid);
C
Cary Xu 已提交
250

251
char* buildCtbNameByGroupId(const char* stbName, uint64_t groupId);
L
liuyao 已提交
252
int32_t buildCtbNameByGroupIdImpl(const char* stbName, uint64_t groupId, char* pBuf);
253

L
Liu Jicong 已提交
254
static FORCE_INLINE int32_t blockGetEncodeSize(const SSDataBlock* pBlock) {
255
  return blockDataGetSerialMetaSize(taosArrayGetSize(pBlock->pDataBlock)) + blockDataGetSize(pBlock);
L
Liu Jicong 已提交
256 257
}

H
Haojun Liao 已提交
258 259 260 261
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
262
#endif /*_TD_COMMON_EP_H_*/