function.h 7.0 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

H
Haojun Liao 已提交
23
#include "tbuffer.h"
S
common  
Shengliang Guan 已提交
24
#include "tcommon.h"
H
Haojun Liao 已提交
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
typedef bool (*FExecGetEnv)(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv);
H
Haojun Liao 已提交
38
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 50 51 52 53

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

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

59
#define TOP_BOTTOM_QUERY_LIMIT    100
60 61
#define FUNCTIONS_NAME_MAX_LENGTH 16

H
Haojun Liao 已提交
62
typedef struct SResultRowEntryInfo {
H
Haojun Liao 已提交
63 64
  bool     initialized:1;     // output buffer has been initialized
  bool     complete:1;        // query has completed
H
Haojun Liao 已提交
65
  uint8_t  isNullRes:6;       // the result is null
66
  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 73 74
  BLK_DATA_NOT_LOAD  = 0x0,
  BLK_DATA_SMA_LOAD  = 0x1,
  BLK_DATA_DATA_LOAD = 0x3,
  BLK_DATA_FILTEROUT = 0x4,   // discard current data block since it is not qualified for filter
75 76 77
};

enum {
H
Haojun Liao 已提交
78
  MAIN_SCAN     = 0x0u,
79
  REVERSE_SCAN  = 0x1u,  // todo remove it
80 81 82 83
  REPEAT_SCAN   = 0x2u,  //repeat scan belongs to the master scan
  MERGE_STAGE   = 0x20u,
};

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

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

//for selectivity query, the corresponding tag value is assigned if the data is qualified
93
typedef struct SSubsidiaryResInfo {
94
  int16_t num;
95 96
  struct SqlFunctionCtx **pCtx;
} SSubsidiaryResInfo;
97

H
Haojun Liao 已提交
98
typedef struct SResultDataInfo {
H
Haojun Liao 已提交
99 100
  int16_t precision;
  int16_t scale;
H
Haojun Liao 已提交
101 102
  int16_t type;
  int16_t bytes;
H
Haojun Liao 已提交
103
  int32_t interBufSize;
H
Haojun Liao 已提交
104 105
} SResultDataInfo;

H
Haojun Liao 已提交
106 107
#define GET_RES_INFO(ctx)        ((ctx)->resultInfo)
#define GET_ROWCELL_INTERBUF(_c) ((void*) ((char*)(_c) + sizeof(SResultRowEntryInfo)))
108

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

121
// sql function runtime context
H
Haojun Liao 已提交
122
typedef struct SqlFunctionCtx {
123 124 125 126 127 128 129 130 131 132
  SInputColumnInfoData   input;
  SResultDataInfo        resDataInfo;
  uint32_t               order;  // data block scanner order: asc|desc
  uint8_t                scanFlag;  // record current running step, default: 0
  int16_t                functionId;    // function id
  char                  *pOutput;       // final result output buffer, point to sdata->data
  int32_t                numOfParams;
  SFunctParam           *param;         // input parameter, e.g., top(k, 20), the number of results for top query is kept in param
  SColumnInfoData       *pTsOutput;     // corresponding output buffer for timestamp of each result, e.g., top/bottom*/
  int32_t                offset;
133
  struct  SResultRowEntryInfo *resultInfo;
134 135 136 137 138
  SSubsidiaryResInfo     subsidiaries;
  SPoint1                start;
  SPoint1                end;
  SFuncExecFuncs         fpSet;
  SScalarFuncExecFuncs   sfp;
139
  struct SExprInfo      *pExpr;
140 141
  struct SDiskbasedBuf  *pBuf;
  struct SSDataBlock    *pSrcBlock;
142
  struct SSDataBlock    *pDstBlock; // used by indifinite rows function to set selectivity
143
  int32_t                curBufPage;
144
  bool                   increase;
H
Haojun Liao 已提交
145
  bool                   isStream;
S
shenglian zhou 已提交
146

147
  char                   udfName[TSDB_FUNC_NAME_LEN];
H
Haojun Liao 已提交
148
} SqlFunctionCtx;
149

150 151 152 153 154 155
enum {
  TEXPR_BINARYEXPR_NODE= 0x1,
  TEXPR_UNARYEXPR_NODE = 0x2,
};

typedef struct tExprNode {
H
Haojun Liao 已提交
156
  int32_t nodeType;
157
  union {
158
    struct {// function node
159 160 161 162
      char     functionName[FUNCTIONS_NAME_MAX_LENGTH];  // todo refactor
      int32_t  functionId;
      int32_t  num;
      struct SFunctionNode  *pFunctNode;
163
    } _function;
164 165 166 167

    struct {
      struct SNode* pRootNode;
    } _optrRoot;
168 169 170
  };
} tExprNode;

171
struct SScalarParam {
D
dapan1121 已提交
172
  bool             colAlloced;
173 174
  SColumnInfoData *columnData;
  SHashObj        *pHashFilter;
D
dapan1121 已提交
175
  int32_t          hashValueType;
176 177
  void            *param;  // other parameter, such as meta handle from vnode, to extract table name/tag value
  int32_t          numOfRows;
178
};
179

180
void    cleanupResultRowEntry(struct SResultRowEntryInfo* pCell);
H
Haojun Liao 已提交
181
int32_t getNumOfResult(SqlFunctionCtx* pCtx, int32_t num, SSDataBlock* pResBlock);
182 183
bool    isRowEntryCompleted(struct SResultRowEntryInfo* pEntry);
bool    isRowEntryInitialized(struct SResultRowEntryInfo* pEntry);
184 185 186 187 188 189 190 191

typedef struct SPoint {
  int64_t key;
  void *  val;
} SPoint;

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

H
Haojun Liao 已提交
192 193 194 195
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// udf api
struct SUdfInfo;

S
slzhou 已提交
196
/**
197
 * create udfd proxy, called once in process that call doSetupUdf/callUdfxxx/doTeardownUdf
S
slzhou 已提交
198 199 200 201 202 203 204 205 206 207
 * @return error code
 */
int32_t udfcOpen();

/**
 * destroy udfd proxy
 * @return error code
 */
int32_t udfcClose();

208 209 210 211 212 213 214 215 216 217 218
/**
 * start udfd that serves udf function invocation under dnode startDnodeId
 * @param startDnodeId
 * @return
 */
int32_t udfStartUdfd(int32_t startDnodeId);
/**
 * stop udfd
 * @return
 */
int32_t udfStopUdfd();
219 220 221 222
#ifdef __cplusplus
}
#endif

223
#endif  // TDENGINE_FUNCTION_H