/src/tdengine/include/libs/function/function.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com> |
3 | | * |
4 | | * This program is free software: you can use, redistribute, and/or modify |
5 | | * it under the terms of the GNU Affero General Public License, version 3 |
6 | | * or later ("AGPL"), as published by the Free Software Foundation. |
7 | | * |
8 | | * This program is distributed in the hope that it will be useful, but WITHOUT |
9 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
10 | | * FITNESS FOR A PARTICULAR PURPOSE. |
11 | | * |
12 | | * You should have received a copy of the GNU Affero General Public License |
13 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
14 | | */ |
15 | | |
16 | | #ifndef TDENGINE_FUNCTION_H |
17 | | #define TDENGINE_FUNCTION_H |
18 | | |
19 | | #ifdef __cplusplus |
20 | | extern "C" { |
21 | | #endif |
22 | | |
23 | | #include "functionResInfo.h" |
24 | | #include "tcommon.h" |
25 | | #include "tsimplehash.h" |
26 | | #include "tvariant.h" |
27 | | #include "nodes.h" |
28 | | |
29 | | struct SqlFunctionCtx; |
30 | | struct SResultRowEntryInfo; |
31 | | |
32 | | struct SFunctionNode; |
33 | | struct SExprSupp; |
34 | | typedef struct SScalarParam SScalarParam; |
35 | | typedef struct SStreamState SStreamState; |
36 | | |
37 | | typedef struct SFuncExecEnv { |
38 | | int32_t calcMemSize; |
39 | | } SFuncExecEnv; |
40 | | |
41 | | |
42 | | |
43 | | typedef struct SExprBasicInfo { |
44 | | SResSchema resSchema; |
45 | | int16_t numOfParams; // argument value of each function |
46 | | SFunctParam* pParam; |
47 | | SNodeList* pParamList; // no need to free |
48 | | } SExprBasicInfo; |
49 | | |
50 | | typedef struct SExprInfo { |
51 | | struct SExprBasicInfo base; |
52 | | struct tExprNode* pExpr; |
53 | | } SExprInfo; |
54 | | |
55 | | int32_t setLagLeadDefaultValueToCol(SColumnInfoData *pOutput, int32_t pos, SFunctParam *pDefaultParam); |
56 | | |
57 | | typedef bool (*FExecGetEnv)(struct SFunctionNode *pFunc, SFuncExecEnv *pEnv); |
58 | | typedef void (*FExecCleanUp)(struct SqlFunctionCtx *pCtx); |
59 | | typedef int32_t (*FExecInit)(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo *pResultCellInfo); |
60 | | typedef int32_t (*FExecProcess)(struct SqlFunctionCtx *pCtx); |
61 | | typedef int32_t (*FExecFinalize)(struct SqlFunctionCtx *pCtx, SSDataBlock *pBlock); |
62 | | typedef int32_t (*FScalarExecProcess)(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput); |
63 | | typedef int32_t (*FExecCombine)(struct SqlFunctionCtx *pDestCtx, struct SqlFunctionCtx *pSourceCtx); |
64 | | typedef int32_t (*FExecDecode)(struct SqlFunctionCtx *pCtx, const char *buf, |
65 | | struct SResultRowEntryInfo *pResultCellInfo, int32_t version); |
66 | | typedef int32_t (*processFuncByRow)(SArray *pCtx); // array of SqlFunctionCtx |
67 | | |
68 | | typedef struct SScalarFuncExecFuncs { |
69 | | FExecGetEnv getEnv; |
70 | | FScalarExecProcess process; |
71 | | } SScalarFuncExecFuncs; |
72 | | |
73 | | typedef struct SFuncExecFuncs { |
74 | | FExecGetEnv getEnv; |
75 | | FExecInit init; |
76 | | FExecProcess process; |
77 | | FExecFinalize finalize; |
78 | | FExecCombine combine; |
79 | | FExecCleanUp cleanup; |
80 | | FExecDecode decode; |
81 | | processFuncByRow processFuncByRow; |
82 | | } SFuncExecFuncs; |
83 | | |
84 | 0 | #define MAX_INTERVAL_TIME_WINDOW 10000000 // maximum allowed time windows in final results |
85 | | |
86 | | #define TOP_BOTTOM_QUERY_LIMIT 100 |
87 | | #define FUNCTIONS_NAME_MAX_LENGTH 32 |
88 | | |
89 | | #define FUNCTION_RESULT_INFO_VERSION 1 |
90 | | |
91 | | typedef struct SResultRowEntryInfo { |
92 | | bool initialized : 1; // output buffer has been initialized |
93 | | bool complete : 1; // query has completed |
94 | | uint8_t isNullRes : 6; // the result is null |
95 | | uint64_t numOfRes; // num of output result in current buffer. NOT NULL RESULT |
96 | | } SResultRowEntryInfo; |
97 | | |
98 | | // determine the real data need to calculated the result |
99 | | enum { |
100 | | BLK_DATA_NOT_LOAD = 0x0, |
101 | | BLK_DATA_SMA_LOAD = 0x1, |
102 | | BLK_DATA_DATA_LOAD = 0x3, |
103 | | BLK_DATA_FILTEROUT = 0x4, // discard current data block since it is not qualified for filter |
104 | | }; |
105 | | |
106 | | enum { |
107 | | MAIN_SCAN = 0x0u, |
108 | | REVERSE_SCAN = 0x1u, // todo remove it |
109 | | PRE_SCAN = 0x2u, // pre-scan belongs to the main scan and occurs before main scan |
110 | | }; |
111 | | |
112 | | struct SPoint1; |
113 | | struct SqlFunctionCtx; |
114 | | struct SResultRowEntryInfo; |
115 | | |
116 | | // for selectivity query, the corresponding tag value is assigned if the data is qualified |
117 | | typedef struct SSubsidiaryResInfo { |
118 | | int16_t num; |
119 | | int32_t rowLen; |
120 | | char *buf; // serialize data buffer |
121 | | struct SqlFunctionCtx **pCtx; |
122 | | } SSubsidiaryResInfo; |
123 | | |
124 | | typedef struct SResultDataInfo { |
125 | | int16_t precision; |
126 | | int16_t scale; |
127 | | int16_t type; |
128 | | uint16_t bytes; |
129 | | int32_t interBufSize; |
130 | | } SResultDataInfo; |
131 | | |
132 | 0 | #define GET_RES_INFO(ctx) ((ctx)->resultInfo) |
133 | 0 | #define GET_ROWCELL_INTERBUF(_c) ((void *)((char *)(_c) + sizeof(SResultRowEntryInfo))) |
134 | | |
135 | | typedef struct SInputColumnInfoData { |
136 | | int32_t totalRows; // total rows in current columnar data |
137 | | int32_t startRowIndex; // handle started row index |
138 | | int64_t numOfRows; // the number of rows needs to be handled |
139 | | bool blankFill; // fill blank data to block for empty table |
140 | | bool colDataSMAIsSet; // if agg is set or not |
141 | | int32_t numOfInputCols; // PTS is not included |
142 | | SColumnInfoData *pPTS; // primary timestamp column |
143 | | SColumnInfoData *pPrimaryKey; // primary key column |
144 | | SColumnInfoData **pData; |
145 | | SColumnDataAgg **pColumnDataAgg; |
146 | | uint64_t uid; // table uid, used to set the tag value when building the final query result for selectivity functions. |
147 | | } SInputColumnInfoData; |
148 | | |
149 | | typedef struct SSerializeDataHandle { |
150 | | struct SDiskbasedBuf *pBuf; |
151 | | int32_t currentPage; |
152 | | SStreamState *pState; |
153 | | } SSerializeDataHandle; |
154 | | |
155 | | // incremental state storage |
156 | | |
157 | | typedef struct SBackendCfWrapper { |
158 | | void *rocksdb; |
159 | | void **pHandle; |
160 | | void *writeOpts; |
161 | | void *readOpts; |
162 | | void **cfOpts; |
163 | | void *dbOpt; |
164 | | void *param; |
165 | | void *env; |
166 | | SListNode *pComparNode; |
167 | | void *pBackend; |
168 | | void *compactFactory; |
169 | | TdThreadRwlock rwLock; |
170 | | bool remove; |
171 | | int64_t backendId; |
172 | | char idstr[64]; |
173 | | } SBackendCfWrapper; |
174 | | |
175 | | typedef struct STdbState { |
176 | | SBackendCfWrapper *pBackendCfWrapper; |
177 | | int64_t backendCfWrapperId; |
178 | | char idstr[64]; |
179 | | |
180 | | struct SStreamTask *pOwner; |
181 | | void *db; |
182 | | void *pStateDb; |
183 | | void *pFuncStateDb; |
184 | | void *pFillStateDb; // todo refactor |
185 | | void *pSessionStateDb; |
186 | | void *pParNameDb; |
187 | | void *pParTagDb; |
188 | | void *txn; |
189 | | int8_t recalc; |
190 | | } STdbState; |
191 | | |
192 | | typedef struct SResultRowStore { |
193 | | int32_t (*resultRowPut)(struct SExprSupp *pSup, const char *inBuf, size_t inBufSize, char **outBuf, |
194 | | size_t *outBufSize); |
195 | | int32_t (*resultRowGet)(struct SExprSupp *pSup, const char *inBuf, size_t inBufSize, char **outBuf, |
196 | | size_t *outBufSize); |
197 | | } SResultRowStore; |
198 | | |
199 | | struct SStreamState { |
200 | | STdbState *pTdbState; |
201 | | struct SStreamFileState *pFileState; |
202 | | int32_t number; |
203 | | SSHashObj *parNameMap; |
204 | | int32_t taskId; |
205 | | int64_t streamId; |
206 | | int64_t streamBackendRid; |
207 | | int8_t dump; |
208 | | int32_t tsIndex; |
209 | | SResultRowStore pResultRowStore; |
210 | | struct SExprSupp *pExprSupp; |
211 | | char pTaskIdStr[65]; |
212 | | }; |
213 | | |
214 | | typedef struct SFunctionStateStore { |
215 | | int32_t (*streamStateFuncPut)(SStreamState *pState, const SWinKey *key, const void *value, int32_t vLen); |
216 | | int32_t (*streamStateFuncGet)(SStreamState *pState, const SWinKey *key, void **ppVal, int32_t *pVLen); |
217 | | } SFunctionStateStore; |
218 | | |
219 | | typedef struct SFuncInputRow { |
220 | | TSKEY ts; |
221 | | bool isDataNull; |
222 | | char *pData; |
223 | | char *pPk; |
224 | | |
225 | | SSDataBlock *block; // prev row block or src block |
226 | | int32_t rowIndex; // prev row block ? 0 : rowIndex in srcBlock |
227 | | |
228 | | // TODO: |
229 | | // int32_t startOffset; // for diff, derivative |
230 | | // SPoint1 startPoint; // for twa |
231 | | } SFuncInputRow; |
232 | | |
233 | | typedef struct SFuncInputRowIter { |
234 | | bool hasPrev; |
235 | | |
236 | | SInputColumnInfoData *pInput; |
237 | | SColumnInfoData *pDataCol; |
238 | | SColumnInfoData *pPkCol; |
239 | | TSKEY *tsList; |
240 | | int32_t rowIndex; |
241 | | int32_t inputEndIndex; |
242 | | SSDataBlock *pSrcBlock; |
243 | | |
244 | | TSKEY prevBlockTsEnd; |
245 | | bool prevIsDataNull; |
246 | | char *pPrevData; |
247 | | char *pPrevPk; |
248 | | SSDataBlock *pPrevRowBlock; // pre one row block |
249 | | |
250 | | uint64_t groupId; |
251 | | bool hasGroupId; |
252 | | |
253 | | bool finalRow; |
254 | | } SFuncInputRowIter; |
255 | | |
256 | | // sql function runtime context |
257 | | typedef struct SqlFunctionCtx { |
258 | | SInputColumnInfoData input; |
259 | | SResultDataInfo resDataInfo; |
260 | | uint32_t order; // data block scanner order: asc|desc |
261 | | uint8_t isPseudoFunc; // denote current function is pseudo function or not [added for perf reason] |
262 | | uint8_t isNotNullFunc; // not return null value. |
263 | | uint8_t scanFlag; // record current running step, default: 0 |
264 | | int16_t functionId; // function id |
265 | | char *pOutput; // final result output buffer, point to sdata->data |
266 | | // input parameter, e.g., top(k, 20), the number of results of top query is kept in param |
267 | | SFunctParam *param; |
268 | | // corresponding output buffer for timestamp of each result, e.g., diff/csum |
269 | | SColumnInfoData *pTsOutput; |
270 | | int32_t numOfParams; |
271 | | int32_t offset; |
272 | | SResultRowEntryInfo *resultInfo; |
273 | | SSubsidiaryResInfo subsidiaries; |
274 | | SPoint1 start; |
275 | | SPoint1 end; |
276 | | SFuncExecFuncs fpSet; |
277 | | SScalarFuncExecFuncs sfp; |
278 | | struct SExprInfo *pExpr; |
279 | | struct SSDataBlock *pSrcBlock; |
280 | | struct SSDataBlock *pDstBlock; // used by indefinite rows function to set selectivity |
281 | | SSerializeDataHandle saveHandle; |
282 | | int32_t exprIdx; |
283 | | char *udfName; |
284 | | SFunctionStateStore *pStore; |
285 | | bool hasPrimaryKey; |
286 | | SFuncInputRowIter rowIter; |
287 | | bool bInputFinished; |
288 | | bool hasWindowOrGroup; // denote that the function is used with time window or group |
289 | | bool hasWindow; // denote that the function is used with time window |
290 | | bool needCleanup; // denote that the function need to be cleaned up |
291 | | int32_t inputType; // save the fuction input type funcs like finalize |
292 | | bool skipDynDataCheck; |
293 | | } SqlFunctionCtx; |
294 | | |
295 | | typedef struct tExprNode { |
296 | | int32_t nodeType; |
297 | | union { |
298 | | struct { // function node |
299 | | char functionName[FUNCTIONS_NAME_MAX_LENGTH]; // todo refactor |
300 | | int32_t functionId; |
301 | | int32_t num; |
302 | | struct SFunctionNode *pFunctNode; |
303 | | int32_t functionType; |
304 | | int32_t bindExprID; |
305 | | } _function; |
306 | | |
307 | | struct { |
308 | | struct SNode *pRootNode; |
309 | | } _optrRoot; |
310 | | }; |
311 | | int32_t relatedTo; |
312 | | } tExprNode; |
313 | | |
314 | | typedef struct SHashParam { |
315 | | bool hasHashParam; |
316 | | bool hasValue; |
317 | | bool hasNull; |
318 | | bool hasNotNull; |
319 | | bool isNegativeOp; |
320 | | SHashObj *pHashFilter; |
321 | | SHashObj *pHashFilterOthers; |
322 | | int32_t filterValueType; |
323 | | STypeMod filterValueTypeMod; |
324 | | } SHashParam; |
325 | | |
326 | | typedef struct SRemoteParam { |
327 | | bool hasRemoteParam; |
328 | | bool hasValue; |
329 | | bool hasNull; |
330 | | bool isMinVal; |
331 | | } SRemoteParam; |
332 | | |
333 | | struct SScalarParam { |
334 | | bool colAlloced; |
335 | | SColumnInfoData *columnData; |
336 | | SHashParam hashParam; |
337 | | SRemoteParam remoteParam; |
338 | | void *param; // other parameter, such as meta handle from vnode, to extract table name/tag value |
339 | | int32_t numOfRows; |
340 | | int32_t numOfQualified; // number of qualified elements in the final results |
341 | | timezone_t tz; |
342 | | char tzName[TD_TIMEZONE_LEN]; // IANA timezone name (for server-side timezone() function) |
343 | | void *charsetCxt; |
344 | | int8_t firstDayOfWeek; /* 0-6, from connection/global config */ |
345 | | SArray *pFilterArr; // for types that can't filter with hash |
346 | | }; |
347 | | |
348 | | typedef struct SSclCompareCtx { |
349 | | SScalarParam *pLeft; |
350 | | SScalarParam *pLeftVar; |
351 | | SScalarParam *pRight; |
352 | | SScalarParam *pOut; |
353 | | int32_t startIndex; |
354 | | int32_t endIndex; |
355 | | __compar_fn_t fp; |
356 | | int32_t optr; |
357 | | int32_t *qualifiedNum; |
358 | | bool isAny; |
359 | | } SSclCompareCtx; |
360 | | |
361 | 0 | static inline void setTzCharset(SScalarParam *param, timezone_t tz, void *charsetCxt) { |
362 | 0 | if (param == NULL) return; |
363 | 0 | param->tz = tz; |
364 | 0 | param->charsetCxt = charsetCxt; |
365 | 0 | } Unexecuted instantiation: parTranslater.c:setTzCharset Unexecuted instantiation: parAstParser.c:setTzCharset Unexecuted instantiation: parCalcConst.c:setTzCharset Unexecuted instantiation: parInsertSql.c:setTzCharset Unexecuted instantiation: taos_lemon_sql.tab.c:setTzCharset Unexecuted instantiation: planLogicCreater.c:setTzCharset Unexecuted instantiation: planOptimizer.c:setTzCharset Unexecuted instantiation: planPhysiCreater.c:setTzCharset Unexecuted instantiation: planSpliter.c:setTzCharset Unexecuted instantiation: planUtil.c:setTzCharset Unexecuted instantiation: planValidator.c:setTzCharset Unexecuted instantiation: functionMgt.c:setTzCharset Unexecuted instantiation: tudf.c:setTzCharset Unexecuted instantiation: builtins.c:setTzCharset Unexecuted instantiation: builtinsimpl.c:setTzCharset Unexecuted instantiation: tavgfunction.c:setTzCharset Unexecuted instantiation: tminmax.c:setTzCharset Unexecuted instantiation: tminmaxavx.c:setTzCharset Unexecuted instantiation: filter.c:setTzCharset Unexecuted instantiation: scalar.c:setTzCharset Unexecuted instantiation: sclfunc.c:setTzCharset Unexecuted instantiation: sclvector.c:setTzCharset Unexecuted instantiation: nodesEqualFuncs.c:setTzCharset Unexecuted instantiation: nodesUtilFuncs.c:setTzCharset Unexecuted instantiation: geomFunc.c:setTzCharset |
366 | | |
367 | | #define cleanupResultRowEntry(p) p->initialized = false |
368 | | #define isRowEntryCompleted(p) (p->complete) |
369 | | #define isRowEntryInitialized(p) (p->initialized) |
370 | | |
371 | | typedef struct SPoint { |
372 | | int64_t key; |
373 | | void *val; |
374 | | } SPoint; |
375 | | |
376 | | void taosGetLinearInterpolationVal(SPoint *point, int32_t outputType, SPoint *point1, SPoint *point2, |
377 | | int32_t inputType, STypeMod inputTypeMod); |
378 | | |
379 | 0 | #define LEASTSQUARES_DOUBLE_ITEM_LENGTH 25 |
380 | 0 | #define LEASTSQUARES_BUFF_LENGTH 128 |
381 | 0 | #define DOUBLE_PRECISION_DIGITS "16e" |
382 | | |
383 | | #ifdef __cplusplus |
384 | | } |
385 | | #endif |
386 | | |
387 | | #endif // TDENGINE_FUNCTION_H |