TDengine/include/common/tdataformat.h

388 lines
13 KiB
C
Raw Normal View History

2020-03-03 08:17:48 +00:00
/*
* 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/>.
*/
2022-02-28 09:39:07 +00:00
2021-10-04 12:42:53 +00:00
#ifndef _TD_COMMON_DATA_FORMAT_H_
#define _TD_COMMON_DATA_FORMAT_H_
2020-02-11 07:29:08 +00:00
2021-05-09 07:47:07 +00:00
#include "os.h"
2020-05-30 10:00:49 +00:00
#include "talgo.h"
2022-05-27 14:05:14 +00:00
#include "tarray.h"
2024-02-21 09:02:25 +00:00
#include "tbuffer.h"
2022-05-11 02:29:53 +00:00
#include "tencode.h"
2021-10-08 12:33:46 +00:00
#include "ttypes.h"
2020-04-28 02:11:22 +00:00
#include "tutil.h"
2020-03-10 06:39:11 +00:00
2020-03-03 08:17:48 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2020-03-10 06:39:11 +00:00
2024-03-15 10:22:57 +00:00
typedef struct SSchema SSchema;
typedef struct SSchema2 SSchema2;
2024-03-12 08:46:41 +00:00
typedef struct SSchemaExt SSchemaExt;
2024-03-15 10:22:57 +00:00
typedef struct STColumn STColumn;
typedef struct STSchema STSchema;
typedef struct SValue SValue;
typedef struct SColVal SColVal;
typedef struct SRow SRow;
typedef struct SRowIter SRowIter;
typedef struct STagVal STagVal;
typedef struct STag STag;
typedef struct SColData SColData;
2024-02-28 06:34:37 +00:00
2024-02-21 09:02:25 +00:00
typedef struct SRowKey SRowKey;
typedef struct SValueColumn SValueColumn;
2022-05-11 02:29:53 +00:00
2024-02-28 06:34:37 +00:00
#define HAS_NONE ((uint8_t)0x1)
#define HAS_NULL ((uint8_t)0x2)
#define HAS_VALUE ((uint8_t)0x4)
2022-09-13 06:21:43 +00:00
// bitmap ================================
2022-12-05 13:42:05 +00:00
const static uint8_t BIT1_MAP[8] = {0b11111110, 0b11111101, 0b11111011, 0b11110111,
0b11101111, 0b11011111, 0b10111111, 0b01111111};
const static uint8_t BIT2_MAP[4] = {0b11111100, 0b11110011, 0b11001111, 0b00111111};
2022-08-18 02:09:31 +00:00
2022-12-05 13:42:05 +00:00
#define ONE ((uint8_t)1)
#define THREE ((uint8_t)3)
#define DIV_8(i) ((i) >> 3)
2024-03-15 10:22:57 +00:00
#define MOD_8(i) ((i)&7)
2022-12-05 13:42:05 +00:00
#define DIV_4(i) ((i) >> 2)
2024-03-15 10:22:57 +00:00
#define MOD_4(i) ((i)&3)
2022-12-05 13:42:05 +00:00
#define MOD_4_TIME_2(i) (MOD_4(i) << 1)
#define BIT1_SIZE(n) (DIV_8((n)-1) + 1)
#define BIT2_SIZE(n) (DIV_4((n)-1) + 1)
#define SET_BIT1(p, i, v) ((p)[DIV_8(i)] = (p)[DIV_8(i)] & BIT1_MAP[MOD_8(i)] | ((v) << MOD_8(i)))
2022-12-21 05:47:36 +00:00
#define SET_BIT1_EX(p, i, v) \
do { \
if (MOD_8(i) == 0) { \
(p)[DIV_8(i)] = 0; \
} \
SET_BIT1(p, i, v); \
} while (0)
2022-12-05 13:42:05 +00:00
#define GET_BIT1(p, i) (((p)[DIV_8(i)] >> MOD_8(i)) & ONE)
#define SET_BIT2(p, i, v) ((p)[DIV_4(i)] = (p)[DIV_4(i)] & BIT2_MAP[MOD_4(i)] | ((v) << MOD_4_TIME_2(i)))
2022-12-21 05:47:36 +00:00
#define SET_BIT2_EX(p, i, v) \
do { \
if (MOD_4(i) == 0) { \
(p)[DIV_4(i)] = 0; \
} \
SET_BIT2(p, i, v); \
} while (0)
#define GET_BIT2(p, i) (((p)[DIV_4(i)] >> MOD_4_TIME_2(i)) & THREE)
2022-06-18 03:35:49 +00:00
2022-09-13 06:21:43 +00:00
// SColVal ================================
2022-09-23 10:55:27 +00:00
#define CV_FLAG_VALUE ((int8_t)0x0)
#define CV_FLAG_NONE ((int8_t)0x1)
#define CV_FLAG_NULL ((int8_t)0x2)
2024-02-21 06:29:10 +00:00
#define COL_VAL_NONE(CID, TYPE) ((SColVal){.cid = (CID), .flag = CV_FLAG_NONE, .value = {.type = (TYPE)}})
#define COL_VAL_NULL(CID, TYPE) ((SColVal){.cid = (CID), .flag = CV_FLAG_NULL, .value = {.type = (TYPE)}})
#define COL_VAL_VALUE(CID, V) ((SColVal){.cid = (CID), .flag = CV_FLAG_VALUE, .value = (V)})
2022-06-01 07:28:57 +00:00
2022-09-23 10:55:27 +00:00
#define COL_VAL_IS_NONE(CV) ((CV)->flag == CV_FLAG_NONE)
#define COL_VAL_IS_NULL(CV) ((CV)->flag == CV_FLAG_NULL)
#define COL_VAL_IS_VALUE(CV) ((CV)->flag == CV_FLAG_VALUE)
2024-05-15 02:54:24 +00:00
#define tRowGetKey(_pRow, _pKey) \
do { \
(_pKey)->ts = (_pRow)->ts; \
(_pKey)->numOfPKs = 0; \
if ((_pRow)->numOfPKs > 0) { \
tRowGetPrimaryKey((_pRow), (_pKey)); \
} \
2024-05-15 01:09:20 +00:00
} while (0)
2024-02-21 09:02:25 +00:00
// SValueColumn ================================
2024-02-23 06:44:46 +00:00
typedef struct {
2024-02-28 02:58:42 +00:00
int8_t cmprAlg; // filled by caller
int8_t type;
2024-02-28 08:49:44 +00:00
int32_t dataOriginalSize;
int32_t dataCompressedSize;
int32_t offsetOriginalSize;
int32_t offsetCompressedSize;
2024-02-23 06:44:46 +00:00
} SValueColumnCompressInfo;
2024-02-21 11:26:12 +00:00
int32_t tValueColumnInit(SValueColumn *valCol);
2024-02-21 09:02:25 +00:00
int32_t tValueColumnDestroy(SValueColumn *valCol);
int32_t tValueColumnClear(SValueColumn *valCol);
int32_t tValueColumnAppend(SValueColumn *valCol, const SValue *value);
2024-03-08 09:04:26 +00:00
int32_t tValueColumnUpdate(SValueColumn *valCol, int32_t idx, const SValue *value);
2024-02-21 09:02:25 +00:00
int32_t tValueColumnGet(SValueColumn *valCol, int32_t idx, SValue *value);
2024-02-28 02:58:42 +00:00
int32_t tValueColumnCompress(SValueColumn *valCol, SValueColumnCompressInfo *info, SBuffer *output, SBuffer *assist);
2024-03-06 08:23:02 +00:00
int32_t tValueColumnDecompress(void *input, const SValueColumnCompressInfo *compressInfo, SValueColumn *valCol,
SBuffer *buffer);
2024-03-03 08:01:12 +00:00
int32_t tValueColumnCompressInfoEncode(const SValueColumnCompressInfo *compressInfo, SBuffer *buffer);
2024-02-23 06:44:46 +00:00
int32_t tValueColumnCompressInfoDecode(SBufferReader *reader, SValueColumnCompressInfo *compressInfo);
2024-02-23 09:27:03 +00:00
int32_t tValueCompare(const SValue *tv1, const SValue *tv2);
2024-02-21 09:02:25 +00:00
2022-11-16 09:22:09 +00:00
// SRow ================================
int32_t tRowBuild(SArray *aColVal, const STSchema *pTSchema, SRow **ppRow);
2023-02-22 06:29:14 +00:00
int32_t tRowGet(SRow *pRow, STSchema *pTSchema, int32_t iCol, SColVal *pColVal);
2022-11-24 03:49:48 +00:00
void tRowDestroy(SRow *pRow);
2023-09-18 05:46:29 +00:00
int32_t tRowSort(SArray *aRowP);
2022-11-30 02:23:44 +00:00
int32_t tRowMerge(SArray *aRowP, STSchema *pTSchema, int8_t flag);
2022-12-30 08:55:49 +00:00
int32_t tRowUpsertColData(SRow *pRow, STSchema *pTSchema, SColData *aColData, int32_t nColData, int32_t flag);
2024-05-15 01:09:20 +00:00
void tRowGetPrimaryKey(SRow *pRow, SRowKey *key);
int32_t tRowKeyCompare(const SRowKey *key1, const SRowKey *key2);
2024-07-15 15:53:12 +00:00
void tRowKeyAssign(SRowKey *pDst, SRowKey *pSrc);
2022-11-16 09:22:09 +00:00
// SRowIter ================================
2022-11-21 07:16:50 +00:00
int32_t tRowIterOpen(SRow *pRow, STSchema *pTSchema, SRowIter **ppIter);
void tRowIterClose(SRowIter **ppIter);
SColVal *tRowIterNext(SRowIter *pIter);
2022-05-11 03:11:52 +00:00
2022-09-13 06:21:43 +00:00
// STag ================================
2022-05-27 14:18:31 +00:00
int32_t tTagNew(SArray *pArray, int32_t version, int8_t isJson, STag **ppTag);
2022-05-17 09:50:21 +00:00
void tTagFree(STag *pTag);
2022-06-22 08:32:10 +00:00
bool tTagIsJson(const void *pTag);
bool tTagIsJsonNull(void *tagVal);
2022-05-28 09:16:04 +00:00
bool tTagGet(const STag *pTag, STagVal *pTagVal);
2022-06-01 12:42:29 +00:00
char *tTagValToData(const STagVal *pTagVal, bool isJson);
2022-05-18 08:30:39 +00:00
int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag);
2022-05-24 06:50:47 +00:00
int32_t tDecodeTag(SDecoder *pDecoder, STag **ppTag);
2022-05-28 09:16:04 +00:00
int32_t tTagToValArray(const STag *pTag, SArray **ppArray);
2022-08-19 07:12:04 +00:00
void tTagSetCid(const STag *pTag, int16_t iTag, int16_t cid);
2022-06-02 06:04:31 +00:00
void debugPrintSTag(STag *pTag, const char *tag, int32_t ln); // TODO: remove
2022-07-11 11:43:16 +00:00
int32_t parseJsontoTagData(const char *json, SArray *pTagVals, STag **ppTag, void *pMsgBuf);
2022-05-17 09:50:21 +00:00
2022-09-13 06:21:43 +00:00
// SColData ================================
2024-02-27 07:10:42 +00:00
typedef struct {
2024-03-15 10:22:57 +00:00
uint32_t cmprAlg; // filled by caller
2024-03-29 13:48:58 +00:00
int8_t columnFlag;
int8_t flag;
int8_t dataType;
int16_t columnId;
int32_t numOfData;
int32_t bitmapOriginalSize;
int32_t bitmapCompressedSize;
int32_t offsetOriginalSize;
int32_t offsetCompressedSize;
int32_t dataOriginalSize;
int32_t dataCompressedSize;
2024-02-27 07:10:42 +00:00
} SColDataCompressInfo;
2022-12-01 08:19:21 +00:00
typedef void *(*xMallocFn)(void *, int32_t);
2024-02-28 06:34:37 +00:00
2024-03-06 07:37:57 +00:00
void tColDataDestroy(void *ph);
void tColDataInit(SColData *pColData, int16_t cid, int8_t type, int8_t cflag);
void tColDataClear(SColData *pColData);
void tColDataDeepClear(SColData *pColData);
int32_t tColDataAppendValue(SColData *pColData, SColVal *pColVal);
int32_t tColDataUpdateValue(SColData *pColData, SColVal *pColVal, bool forward);
void tColDataGetValue(SColData *pColData, int32_t iVal, SColVal *pColVal);
uint8_t tColDataGetBitValue(const SColData *pColData, int32_t iVal);
int32_t tColDataCopy(SColData *pColDataFrom, SColData *pColData, xMallocFn xMalloc, void *arg);
2024-04-10 07:06:43 +00:00
void tColDataArrGetRowKey(SColData *aColData, int32_t nColData, int32_t iRow, SRowKey *key);
2024-03-06 07:37:57 +00:00
2024-02-28 06:34:37 +00:00
extern void (*tColDataCalcSMA[])(SColData *pColData, int64_t *sum, int64_t *max, int64_t *min, int16_t *numOfNull);
2024-02-28 08:49:44 +00:00
int32_t tColDataCompress(SColData *colData, SColDataCompressInfo *info, SBuffer *output, SBuffer *assist);
2024-03-06 08:42:54 +00:00
int32_t tColDataDecompress(void *input, SColDataCompressInfo *info, SColData *colData, SBuffer *assist);
2022-09-13 06:21:43 +00:00
2022-11-25 09:27:04 +00:00
// for stmt bind
2023-05-31 01:16:47 +00:00
int32_t tColDataAddValueByBind(SColData *pColData, TAOS_MULTI_BIND *pBind, int32_t buffMaxLen);
2022-11-30 06:49:18 +00:00
void tColDataSortMerge(SArray *colDataArr);
2022-11-25 09:27:04 +00:00
2022-12-30 06:20:55 +00:00
// for raw block
int32_t tColDataAddValueByDataBlock(SColData *pColData, int8_t type, int32_t bytes, int32_t nRows, char *lengthOrbitmap,
char *data);
2022-11-29 14:06:08 +00:00
// for encode/decode
2024-04-10 07:06:43 +00:00
int32_t tPutColData(uint8_t version, uint8_t *pBuf, SColData *pColData);
int32_t tGetColData(uint8_t version, uint8_t *pBuf, SColData *pColData);
2022-11-25 09:27:04 +00:00
2022-09-13 06:21:43 +00:00
// STRUCT ================================
2022-05-11 02:29:53 +00:00
struct STColumn {
col_id_t colId;
int8_t type;
int8_t flags;
int32_t bytes;
int32_t offset;
};
struct STSchema {
2022-05-11 02:37:52 +00:00
int32_t numOfCols;
int32_t version;
int32_t flen;
int32_t tlen;
STColumn columns[];
2022-05-11 02:29:53 +00:00
};
2024-02-29 06:50:24 +00:00
/*
2024-01-26 09:14:00 +00:00
* 1. Tuple format:
2024-02-29 06:50:24 +00:00
* SRow + [(type, offset) * numOfPKs +] [bit map +] fix-length data + [var-length data]
2024-01-26 09:14:00 +00:00
*
* 2. K-V format:
2024-02-29 06:50:24 +00:00
* SRow + [(type, offset) * numOfPKs +] offset array + ([-]cid [+ data]) * numColsNotNone
2024-01-26 09:14:00 +00:00
*/
2022-11-16 09:22:09 +00:00
struct SRow {
uint8_t flag;
2024-02-29 06:50:24 +00:00
uint8_t numOfPKs;
2022-11-16 09:22:09 +00:00
uint16_t sver;
uint32_t len;
2022-11-21 04:13:27 +00:00
TSKEY ts;
2022-11-16 09:22:09 +00:00
uint8_t data[];
2022-05-11 02:29:53 +00:00
};
2024-02-29 06:50:24 +00:00
typedef struct {
int8_t type;
uint32_t offset;
} SPrimaryKeyIndex;
2022-05-31 02:48:45 +00:00
struct SValue {
2024-02-21 06:29:10 +00:00
int8_t type;
2022-05-31 02:48:45 +00:00
union {
2022-09-21 07:28:04 +00:00
int64_t val;
2022-05-31 02:48:45 +00:00
struct {
uint8_t *pData;
uint32_t nData;
2022-05-31 02:48:45 +00:00
};
};
};
2024-02-28 06:34:37 +00:00
#define TD_MAX_PK_COLS 2
2024-01-26 09:14:00 +00:00
struct SRowKey {
2024-02-21 07:21:46 +00:00
TSKEY ts;
2024-02-22 02:13:35 +00:00
uint8_t numOfPKs;
2024-02-28 06:34:37 +00:00
SValue pks[TD_MAX_PK_COLS];
2024-01-26 09:14:00 +00:00
};
2022-05-14 06:18:07 +00:00
struct SColVal {
2022-06-01 07:10:33 +00:00
int16_t cid;
2022-09-23 10:55:27 +00:00
int8_t flag;
2022-05-31 03:08:29 +00:00
SValue value;
2022-05-14 06:18:07 +00:00
};
2022-09-13 06:21:43 +00:00
struct SColData {
int16_t cid;
int8_t type;
2024-01-26 09:14:00 +00:00
int8_t cflag;
2023-01-03 03:34:27 +00:00
int32_t numOfNone; // # of none
int32_t numOfNull; // # of null
int32_t numOfValue; // # of vale
2022-09-13 06:21:43 +00:00
int32_t nVal;
2023-02-15 09:59:54 +00:00
int8_t flag;
2022-09-13 06:21:43 +00:00
uint8_t *pBitMap;
int32_t *aOffset;
int32_t nData;
uint8_t *pData;
};
2022-05-31 15:38:21 +00:00
#pragma pack(push, 1)
2022-05-17 09:50:21 +00:00
struct STagVal {
2022-08-18 02:09:31 +00:00
// char colName[TSDB_COL_NAME_LEN]; // only used for tmq_get_meta
2022-05-27 13:59:53 +00:00
union {
int16_t cid;
char *pKey;
};
2022-05-30 03:51:36 +00:00
int8_t type;
union {
2022-06-01 12:42:29 +00:00
int64_t i64;
2022-05-30 03:51:36 +00:00
struct {
uint32_t nData;
uint8_t *pData;
};
};
2022-05-17 09:50:21 +00:00
};
2022-06-01 12:42:29 +00:00
#define TD_TAG_JSON ((int8_t)0x40) // distinguish JSON string and JSON value with the highest bit
2022-05-31 13:05:43 +00:00
#define TD_TAG_LARGE ((int8_t)0x20)
2022-05-28 09:16:04 +00:00
struct STag {
2022-05-28 10:10:16 +00:00
int8_t flags;
2022-05-28 09:16:04 +00:00
int16_t len;
int16_t nTag;
int32_t ver;
2022-05-28 10:10:16 +00:00
int8_t idx[];
2022-05-28 09:16:04 +00:00
};
#pragma pack(pop)
2022-05-17 09:50:21 +00:00
#if 1 //================================================================================================================================================
2022-01-24 12:53:04 +00:00
// 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
2022-02-03 09:01:38 +00:00
2021-01-25 03:30:41 +00:00
#define STR_TO_VARSTR(x, str) \
do { \
VarDataLenT __len = (VarDataLenT)strlen(str); \
*(VarDataLenT *)(x) = __len; \
2024-07-25 00:51:19 +00:00
(void)memcpy(varDataVal(x), (str), __len); \
2020-06-01 01:57:55 +00:00
} while (0);
2021-01-24 02:24:44 +00:00
#define STR_WITH_MAXSIZE_TO_VARSTR(x, str, _maxs) \
do { \
2020-06-16 07:22:58 +00:00
char *_e = stpncpy(varDataVal(x), (str), (_maxs)-VARSTR_HEADER_SIZE); \
2021-01-24 02:24:44 +00:00
varDataSetLen(x, (_e - (x)-VARSTR_HEADER_SIZE)); \
2020-06-01 01:57:55 +00:00
} while (0)
2024-07-25 00:51:19 +00:00
#define STR_WITH_SIZE_TO_VARSTR(x, str, _size) \
do { \
*(VarDataLenT *)(x) = (VarDataLenT)(_size); \
(void)memcpy(varDataVal(x), (str), (_size)); \
2020-06-01 01:57:55 +00:00
} while (0);
2020-05-02 09:41:38 +00:00
2022-11-23 12:13:27 +00:00
// STSchema ================================
2022-11-23 02:45:58 +00:00
STSchema *tBuildTSchema(SSchema *aSchema, int32_t numOfCols, int32_t version);
2023-01-30 06:24:00 +00:00
#define tDestroyTSchema(pTSchema) \
do { \
if (pTSchema) { \
taosMemoryFree(pTSchema); \
pTSchema = NULL; \
} \
} while (0)
2024-03-07 05:14:56 +00:00
const STColumn *tTSchemaSearchColumn(const STSchema *pTSchema, int16_t cid);
2022-11-23 02:45:58 +00:00
2024-02-21 11:26:12 +00:00
struct SValueColumn {
int8_t type;
uint32_t numOfValues;
SBuffer data;
SBuffer offsets;
};
2024-02-23 06:44:46 +00:00
typedef struct {
2024-03-15 10:22:57 +00:00
int32_t dataType; // filled by caller
uint32_t cmprAlg; // filled by caller
int32_t originalSize; // filled by caller
int32_t compressedSize;
2024-02-23 06:44:46 +00:00
} SCompressInfo;
2024-02-28 02:58:42 +00:00
int32_t tCompressData(void *input, // input
SCompressInfo *info, // compress info
void *output, // output
int32_t outputSize, // output size
SBuffer *buffer // assistant buffer provided by caller, can be NULL
);
int32_t tDecompressData(void *input, // input
const SCompressInfo *info, // compress info
void *output, // output
int32_t outputSize, // output size
SBuffer *buffer // assistant buffer provided by caller, can be NULL
);
2024-03-06 07:37:57 +00:00
int32_t tCompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist);
int32_t tDecompressDataToBuffer(void *input, SCompressInfo *info, SBuffer *output, SBuffer *assist);
2024-02-23 06:44:46 +00:00
2024-06-05 09:06:36 +00:00
typedef struct {
int32_t columnId;
int32_t type;
TAOS_MULTI_BIND *bind;
} SBindInfo;
int32_t tRowBuildFromBind(SBindInfo *infos, int32_t numOfInfos, bool infoSorted, const STSchema *pTSchema,
SArray *rowArray);
2022-05-11 02:29:53 +00:00
#endif
2020-03-03 08:17:48 +00:00
#ifdef __cplusplus
}
#endif
2022-01-06 10:11:28 +00:00
#endif /*_TD_COMMON_DATA_FORMAT_H_*/