tudf.h 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#ifndef TDENGINE_TUDF_H
#define TDENGINE_TUDF_H

S
shenglian zhou 已提交
19 20 21 22

#include <stdint.h>
#include <stdbool.h>
#include "tmsg.h"
23
#include "tcommon.h"
S
slzhou 已提交
24
#include "function.h"
S
shenglian zhou 已提交
25

26 27 28 29
#ifdef __cplusplus
extern "C" {
#endif

S
slzhou 已提交
30
#define UDF_LISTEN_PIPE_NAME_LEN 32
S
slzhou 已提交
31
#define UDF_LISTEN_PIPE_NAME_PREFIX "udfd.sock."
S
slzhou 已提交
32

33 34
//======================================================================================
//begin API to taosd and qworker
S
shenglian zhou 已提交
35

36 37
enum {
  UDFC_CODE_STOPPING = -1,
S
shenglian zhou 已提交
38
  UDFC_CODE_PIPE_READ_ERR = -3,
39 40
};

41 42 43
typedef void *UdfcHandle;
typedef void *UdfcFuncHandle;

44 45
/**
 * create udfd proxy, called once in process that call setupUdf/callUdfxxx/teardownUdf
46 47
 * @return error code
 */
48
int32_t udfcOpen(int32_t dnodeId, UdfcHandle* proxyHandle);
49 50

/**
51
 * destroy udfd proxy
52 53
 * @return error code
 */
54
int32_t udfcClose(UdfcHandle proxyhandle);
55 56 57 58 59 60 61 62


/**
 * setup udf
 * @param udf, in
 * @param handle, out
 * @return error code
 */
63
int32_t setupUdf(UdfcHandle proxyHandle, char udfName[], SEpSet *epSet, UdfcFuncHandle *handle);
64

S
shenglian zhou 已提交
65 66 67 68 69 70
typedef struct SUdfColumnMeta {
  int16_t type;
  int32_t bytes; // <0 var length, others fixed length bytes
  uint8_t precision;
  uint8_t scale;
} SUdfColumnMeta;
71

S
shenglian zhou 已提交
72 73 74 75
typedef struct SUdfColumnData {
  int32_t numOfRows;
  bool varLengthColumn;
  union {
S
slzhou 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88
    struct {
      int32_t nullBitmapLen;
      char   *nullBitmap;
      int32_t dataLen;
      char   *data;
    } fixLenCol;

    struct {
      int32_t varOffsetsLen;
      char   *varOffsets;
      int32_t payloadLen;
      char   *payload;
    } varLenCol;
S
shenglian zhou 已提交
89 90 91 92 93 94 95 96
  };
} SUdfColumnData;


typedef struct SUdfColumn {
  SUdfColumnMeta colMeta;
  SUdfColumnData colData;
} SUdfColumn;
97

98
typedef struct SUdfDataBlock {
S
shenglian zhou 已提交
99 100
  int32_t numOfRows;
  int32_t numOfCols;
S
shenglian zhou 已提交
101
  SUdfColumn **udfCols;
102
} SUdfDataBlock;
103

S
shenglian zhou 已提交
104 105 106 107 108
typedef struct SUdfInterBuf {
  int32_t bufLen;
  char* buf;
} SUdfInterBuf;

S
slzhou 已提交
109
// output: interBuf
110
int32_t callUdfAggInit(UdfcFuncHandle handle, SUdfInterBuf *interBuf);
S
slzhou 已提交
111 112
// input: block, state
// output: newState
113
int32_t callUdfAggProcess(UdfcFuncHandle handle, SSDataBlock *block, SUdfInterBuf *state, SUdfInterBuf *newState);
S
shenglian zhou 已提交
114 115
// input: interBuf
// output: resultData
116
int32_t callUdfAggFinalize(UdfcFuncHandle handle, SUdfInterBuf *interBuf, SUdfInterBuf *resultData);
S
slzhou 已提交
117 118
// input: interbuf1, interbuf2
// output: resultBuf
119
int32_t callUdfAggMerge(UdfcFuncHandle handle, SUdfInterBuf *interBuf1, SUdfInterBuf *interBuf2, SUdfInterBuf *resultBuf);
S
shenglian zhou 已提交
120 121
// input: block
// output: resultData
S
slzhou 已提交
122
int32_t callUdfScalarFunc(UdfcFuncHandle handle, SScalarParam *input, int32_t numOfCols, SScalarParam *output);
123 124 125 126 127
/**
 * tearn down udf
 * @param handle
 * @return
 */
128
int32_t teardownUdf(UdfcFuncHandle handle);
129 130 131

// end API to taosd and qworker
//=============================================================================================================================
132
// begin API to UDF writer.
133

S
shenglian zhou 已提交
134
// dynamic lib init and destroy
S
shenglian zhou 已提交
135 136 137
typedef int32_t (*TUdfSetupFunc)();
typedef int32_t (*TUdfTeardownFunc)();

138
//TODO: add API to check function arguments type, number etc.
139 140 141 142 143
//TODO: another way to manage memory is provide api for UDF to add data to SUdfColumnData and UDF framework will allocate memory.
// then UDF framework will free the memory
//typedef int32_t addFixedLengthColumnData(SColumnData *columnData, int rowIndex, bool isNull, int32_t colBytes, char* data);
//typedef int32_t addVariableLengthColumnData(SColumnData *columnData, int rowIndex, bool isNull, int32_t dataLen, char * data);

S
slzhou 已提交
144
typedef int32_t (*TUdfFreeUdfColumnFunc)(SUdfColumn* column);
145

S
slzhou 已提交
146
typedef int32_t (*TUdfScalarProcFunc)(SUdfDataBlock block, SUdfColumn *resultCol);
S
shenglian zhou 已提交
147 148
typedef int32_t (*TUdfAggInitFunc)(SUdfInterBuf *buf);
typedef int32_t (*TUdfAggProcessFunc)(SUdfDataBlock block, SUdfInterBuf *interBuf);
S
slzhou 已提交
149
typedef int32_t (*TUdfAggFinalizeFunc)(SUdfInterBuf buf, SUdfInterBuf *resultData);
S
shenglian zhou 已提交
150 151


152 153
// end API to UDF writer
//=======================================================================================================================
154

155 156 157 158
#ifdef __cplusplus
}
#endif

159
#endif  // TDENGINE_TUDF_H