function.h 8.1 KB
Newer Older
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 TDENGINE_FUNCTION_H
#define TDENGINE_FUNCTION_H
18 19 20 21 22

#ifdef __cplusplus
extern "C" {
#endif

S
common  
Shengliang Guan 已提交
23
#include "tcommon.h"
24
#include "tsimplehash.h"
25
#include "tvariant.h"
H
Haojun Liao 已提交
26 27 28 29

struct SqlFunctionCtx;
struct SResultRowEntryInfo;

30 31
struct SFunctionNode;
typedef struct SScalarParam SScalarParam;
H
Haojun Liao 已提交
32 33 34 35 36

typedef struct SFuncExecEnv {
  int32_t calcMemSize;
} SFuncExecEnv;

37 38
typedef bool (*FExecGetEnv)(struct SFunctionNode *pFunc, SFuncExecEnv *pEnv);
typedef bool (*FExecInit)(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo);
H
Haojun Liao 已提交
39
typedef int32_t (*FExecProcess)(struct SqlFunctionCtx *pCtx);
40
typedef int32_t (*FExecFinalize)(struct SqlFunctionCtx *pCtx, SSDataBlock *pBlock);
41
typedef int32_t (*FScalarExecProcess)(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput);
5
54liuyao 已提交
42
typedef int32_t (*FExecCombine)(struct SqlFunctionCtx *pDestCtx, struct SqlFunctionCtx *pSourceCtx);
43 44

typedef struct SScalarFuncExecFuncs {
45
  FExecGetEnv        getEnv;
46 47
  FScalarExecProcess process;
} SScalarFuncExecFuncs;
H
Haojun Liao 已提交
48 49

typedef struct SFuncExecFuncs {
50 51 52
  FExecGetEnv   getEnv;
  FExecInit     init;
  FExecProcess  process;
H
Haojun Liao 已提交
53
  FExecFinalize finalize;
54
  FExecCombine  combine;
H
Haojun Liao 已提交
55
} SFuncExecFuncs;
56

H
Haojun Liao 已提交
57
#define MAX_INTERVAL_TIME_WINDOW 10000000  // maximum allowed time windows in final results
58

59
#define TOP_BOTTOM_QUERY_LIMIT    100
G
Ganlin Zhao 已提交
60
#define FUNCTIONS_NAME_MAX_LENGTH 32
61

H
Haojun Liao 已提交
62
typedef struct SResultRowEntryInfo {
63 64 65 66
  bool     initialized : 1;  // output buffer has been initialized
  bool     complete : 1;     // query has completed
  uint8_t  isNullRes : 6;    // the result is null
  uint16_t numOfRes;         // num of output result in current buffer. NOT NULL RESULT
H
Haojun Liao 已提交
67 68
} SResultRowEntryInfo;

69 70
// determine the real data need to calculated the result
enum {
71 72
  BLK_DATA_NOT_LOAD = 0x0,
  BLK_DATA_SMA_LOAD = 0x1,
73
  BLK_DATA_DATA_LOAD = 0x3,
74
  BLK_DATA_FILTEROUT = 0x4,  // discard current data block since it is not qualified for filter
75 76 77
};

enum {
78 79
  MAIN_SCAN = 0x0u,
  REVERSE_SCAN = 0x1u,  // todo remove it
80
  PRE_SCAN = 0x2u,      // pre-scan belongs to the main scan and occurs before main scan
81 82
};

83
typedef struct SPoint1 {
84 85 86 87 88
  int64_t key;
  union {
    double val;
    char  *ptr;
  };
89 90
} SPoint1;

H
Haojun Liao 已提交
91
struct SqlFunctionCtx;
92
struct SResultRowEntryInfo;
93

94
// for selectivity query, the corresponding tag value is assigned if the data is qualified
95
typedef struct SSubsidiaryResInfo {
96 97 98
  int16_t                 num;
  int32_t                 rowLen;
  char                   *buf;  // serialize data buffer
99 100
  struct SqlFunctionCtx **pCtx;
} SSubsidiaryResInfo;
101

H
Haojun Liao 已提交
102
typedef struct SResultDataInfo {
103 104 105 106 107
  int16_t  precision;
  int16_t  scale;
  int16_t  type;
  uint16_t bytes;
  int32_t  interBufSize;
H
Haojun Liao 已提交
108 109
} SResultDataInfo;

H
Haojun Liao 已提交
110
#define GET_RES_INFO(ctx)        ((ctx)->resultInfo)
111
#define GET_ROWCELL_INTERBUF(_c) ((void *)((char *)(_c) + sizeof(SResultRowEntryInfo)))
112

H
Haojun Liao 已提交
113
typedef struct SInputColumnInfoData {
114 115
  int32_t           totalRows;        // total rows in current columnar data
  int32_t           startRowIndex;    // handle started row index
116
  int64_t           numOfRows;        // the number of rows needs to be handled
117
  int32_t           numOfInputCols;   // PTS is not included
H
Haojun Liao 已提交
118
  bool              colDataSMAIsSet;  // if agg is set or not
119
  SColumnInfoData  *pPTS;             // primary timestamp column
H
Haojun Liao 已提交
120 121
  SColumnInfoData **pData;
  SColumnDataAgg  **pColumnDataAgg;
122
  uint64_t uid;  // table uid, used to set the tag value when building the final query result for selectivity functions.
H
Haojun Liao 已提交
123
} SInputColumnInfoData;
H
Haojun Liao 已提交
124

125
typedef struct SSerializeDataHandle {
126
  struct SDiskbasedBuf *pBuf;
127
  int32_t               currentPage;
128
  void                 *pState;
129 130
} SSerializeDataHandle;

131 132
// incremental state storage
typedef struct STdbState {
133 134 135 136 137 138 139 140 141 142 143
  void               *rocksdb;
  void              **pHandle;
  void               *writeOpts;
  void               *readOpts;
  void              **cfOpts;
  void               *dbOpt;
  struct SStreamTask *pOwner;
  void               *param;
  void               *env;
  SListNode          *pComparNode;
  void               *pBackend;
144
  char                idstr[64];
145 146 147 148 149 150 151 152 153 154 155
  void               *compactFactory;
  TdThreadRwlock      rwLock;

  void *db;
  void *pStateDb;
  void *pFuncStateDb;
  void *pFillStateDb;  // todo refactor
  void *pSessionStateDb;
  void *pParNameDb;
  void *pParTagDb;
  void *txn;
156 157 158
} STdbState;

typedef struct {
159 160 161 162 163 164 165
  STdbState               *pTdbState;
  struct SStreamFileState *pFileState;
  int32_t                  number;
  SSHashObj               *parNameMap;
  int64_t                  checkPointId;
  int32_t                  taskId;
  int64_t                  streamId;
dengyihao's avatar
dengyihao 已提交
166
  int64_t                  streamBackendRid;
167 168 169
} SStreamState;

typedef struct SFunctionStateStore {
170 171
  int32_t (*streamStateFuncPut)(SStreamState *pState, const SWinKey *key, const void *value, int32_t vLen);
  int32_t (*streamStateFuncGet)(SStreamState *pState, const SWinKey *key, void **ppVal, int32_t *pVLen);
172 173
} SFunctionStateStore;

174
// sql function runtime context
H
Haojun Liao 已提交
175
typedef struct SqlFunctionCtx {
176 177
  SInputColumnInfoData input;
  SResultDataInfo      resDataInfo;
178 179 180 181 182 183
  uint32_t             order;          // data block scanner order: asc|desc
  uint8_t              isPseudoFunc;   // denote current function is pseudo function or not [added for perf reason]
  uint8_t              isNotNullFunc;  // not return null value.
  uint8_t              scanFlag;       // record current running step, default: 0
  int16_t              functionId;     // function id
  char                *pOutput;        // final result output buffer, point to sdata->data
H
Haojun Liao 已提交
184
  // input parameter, e.g., top(k, 20), the number of results of top query is kept in param
185
  SFunctParam *param;
H
Haojun Liao 已提交
186 187
  // corresponding output buffer for timestamp of each result, e.g., diff/csum
  SColumnInfoData     *pTsOutput;
188
  int32_t              numOfParams;
H
Haojun Liao 已提交
189 190 191 192 193 194 195 196 197 198 199
  int32_t              offset;
  SResultRowEntryInfo *resultInfo;
  SSubsidiaryResInfo   subsidiaries;
  SPoint1              start;
  SPoint1              end;
  SFuncExecFuncs       fpSet;
  SScalarFuncExecFuncs sfp;
  struct SExprInfo    *pExpr;
  struct SSDataBlock  *pSrcBlock;
  struct SSDataBlock  *pDstBlock;  // used by indefinite rows function to set selectivity
  SSerializeDataHandle saveHandle;
L
Liu Jicong 已提交
200
  int32_t              exprIdx;
201
  char                *udfName;
202
  SFunctionStateStore *pStore;
H
Haojun Liao 已提交
203
} SqlFunctionCtx;
204

205
typedef struct tExprNode {
H
Haojun Liao 已提交
206
  int32_t nodeType;
207
  union {
208 209 210 211 212
    struct {                                                          // function node
      char                  functionName[FUNCTIONS_NAME_MAX_LENGTH];  // todo refactor
      int32_t               functionId;
      int32_t               num;
      struct SFunctionNode *pFunctNode;
S
shenglian zhou 已提交
213
      int32_t               functionType;
214
    } _function;
215 216

    struct {
217
      struct SNode *pRootNode;
218
    } _optrRoot;
219 220 221
  };
} tExprNode;

222
struct SScalarParam {
D
dapan1121 已提交
223
  bool             colAlloced;
224 225
  SColumnInfoData *columnData;
  SHashObj        *pHashFilter;
D
dapan1121 已提交
226
  int32_t          hashValueType;
H
Hongze Cheng 已提交
227
  void            *param;  // other parameter, such as meta handle from vnode, to extract table name/tag value
228
  int32_t          numOfRows;
H
Hongze Cheng 已提交
229
  int32_t          numOfQualified;  // number of qualified elements in the final results
230
};
231

L
Liu Jicong 已提交
232 233 234
void cleanupResultRowEntry(struct SResultRowEntryInfo *pCell);
bool isRowEntryCompleted(struct SResultRowEntryInfo *pEntry);
bool isRowEntryInitialized(struct SResultRowEntryInfo *pEntry);
235 236 237

typedef struct SPoint {
  int64_t key;
238
  void   *val;
239 240
} SPoint;

241 242
int32_t taosGetLinearInterpolationVal(SPoint *point, int32_t outputType, SPoint *point1, SPoint *point2,
                                      int32_t inputType);
243

244 245 246 247
#ifdef __cplusplus
}
#endif

248
#endif  // TDENGINE_FUNCTION_H