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