tudf.h 3.7 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

19 20 21 22
#ifdef __cplusplus
extern "C" {
#endif

23 24
//======================================================================================
//begin API to taosd and qworker
S
shenglian zhou 已提交
25 26
#define TSDB_UDF_MAX_COLUMNS 4

27 28 29 30 31
enum {
  UDFC_CODE_STOPPING = -1,
  UDFC_CODE_RESTARTING = -2,
};

32 33 34 35 36 37 38 39 40 41 42 43
/**
 * start udf dameon service
 * @return error code
 */
int32_t startUdfService();

/**
 * stop udf dameon service
 * @return error code
 */
int32_t stopUdfService();

44 45 46 47 48 49 50 51 52 53
enum {
  TSDB_UDF_TYPE_SCALAR = 0,
  TSDB_UDF_TYPE_AGGREGATE = 1
};

enum {
  TSDB_UDF_SCRIPT_BIN_LIB = 0,
  TSDB_UDF_SCRIPT_LUA = 1,
};

S
shenglian zhou 已提交
54 55 56 57 58 59 60
typedef struct SUdfColumnMeta {
  int16_t type;
  int32_t bytes; // <0 var length, others fixed length bytes
  uint8_t precision;
  uint8_t scale;
} SUdfColumnMeta;

61 62
typedef struct SUdfInfo {
  char   *udfName;        // function name
63 64
  int32_t udfType;    // scalar function or aggregate function
  int8_t    scriptType;
65 66
  char *path;

S
shenglian zhou 已提交
67 68 69
  // known info between qworker and udf
  //  struct SUdfColumnMeta resultMeta;
  //  int32_t bufSize;     //interbuf size
70 71 72 73 74 75 76 77 78 79 80 81 82

} SUdfInfo;

typedef void *UdfHandle;

/**
 * setup udf
 * @param udf, in
 * @param handle, out
 * @return error code
 */
int32_t setupUdf(SUdfInfo* udf, UdfHandle *handle);

83

S
shenglian zhou 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
typedef struct SUdfColumnData {
  int32_t numOfRows;
  bool varLengthColumn;
  union {
    int32_t nullBitmapLen;
    char* nullBitmap;
    int32_t dataLen;
    char* data;
  };

  union {
    int32_t varOffsetsLen;
    char* varOffsets;
    int32_t payloadLen;
    char* payload;
  };
} SUdfColumnData;


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

108
typedef struct SUdfDataBlock {
S
shenglian zhou 已提交
109 110 111
  int32_t numOfRows;
  int32_t numOfCols;
  SUdfColumn udfCols[TSDB_UDF_MAX_COLUMNS];
112
} SUdfDataBlock;
113

S
shenglian zhou 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127
typedef struct SUdfInterBuf {
  int32_t bufLen;
  char* buf;
} SUdfInterBuf;

// input: block, initFirst
// output: interbuf
int32_t callUdfAggProcess(SUdfDataBlock block, SUdfInterBuf *interBuf, bool initFirst);
// input: interBuf
// output: resultData
int32_t callUdfAggFinalize(SUdfInterBuf interBuf, SUdfColumnData* resultData);
// input: block
// output: resultData
int32_t callUdfScalaProcess(SUdfDataBlock block, SUdfColumnData* resultData);
128

129 130 131 132 133 134 135 136 137
/**
 * tearn down udf
 * @param handle
 * @return
 */
int32_t teardownUdf(UdfHandle handle);

// end API to taosd and qworker
//=============================================================================================================================
138
// begin API to UDF writer.
139

S
shenglian zhou 已提交
140
// dynamic lib init and destroy
141
typedef int32_t (*TUdfInitFunc)();
S
shenglian zhou 已提交
142
typedef int32_t (*TUdfDestroyFunc)();
143

S
shenglian zhou 已提交
144 145 146 147
typedef int32_t (*TUdfScalarProcFunc)(SUdfDataBlock block, SUdfColumnData *resultData);
typedef int32_t (*TUdfAggInit)(SUdfInterBuf *buf);
typedef int32_t (*TUdfAggProcess)(SUdfDataBlock block, SUdfInterBuf *interBuf);
typedef int32_t (*TUdfAggFinalize)(SUdfInterBuf buf, SUdfColumnData *resultData);
148 149
// end API to UDF writer
//=======================================================================================================================
150

151 152 153 154
#ifdef __cplusplus
}
#endif

155
#endif  // TDENGINE_TUDF_H