tudf.h 4.2 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
shenglian zhou 已提交
24

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

29 30
//======================================================================================
//begin API to taosd and qworker
S
shenglian zhou 已提交
31

32 33 34 35 36
enum {
  UDFC_CODE_STOPPING = -1,
  UDFC_CODE_RESTARTING = -2,
};

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/**
 * start udf dameon service
 * @return error code
 */
int32_t startUdfService();

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

typedef void *UdfHandle;

/**
 * setup udf
 * @param udf, in
 * @param handle, out
 * @return error code
 */
57
int32_t setupUdf(char udfName[], SEpSet *epSet, UdfHandle *handle);
58

S
shenglian zhou 已提交
59 60 61 62 63 64
typedef struct SUdfColumnMeta {
  int16_t type;
  int32_t bytes; // <0 var length, others fixed length bytes
  uint8_t precision;
  uint8_t scale;
} SUdfColumnMeta;
65

S
shenglian zhou 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
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;
89

90
typedef struct SUdfDataBlock {
S
shenglian zhou 已提交
91 92
  int32_t numOfRows;
  int32_t numOfCols;
S
shenglian zhou 已提交
93
  SUdfColumn **udfCols;
94
} SUdfDataBlock;
95

S
shenglian zhou 已提交
96 97 98 99 100
typedef struct SUdfInterBuf {
  int32_t bufLen;
  char* buf;
} SUdfInterBuf;

101
//TODO: translate these calls to callUdf
S
slzhou 已提交
102 103 104 105 106
// output: interBuf
int32_t callUdfAggInit(UdfHandle handle, SUdfInterBuf *interBuf);
// input: block, state
// output: newState
int32_t callUdfAggProcess(UdfHandle handle, SSDataBlock *block, SUdfInterBuf *state, SUdfInterBuf *newState);
S
shenglian zhou 已提交
107 108
// input: interBuf
// output: resultData
S
slzhou 已提交
109
int32_t callUdfAggFinalize(UdfHandle handle, SUdfInterBuf *interBuf, SUdfInterBuf *resultData);
S
slzhou 已提交
110 111
// input: interbuf1, interbuf2
// output: resultBuf
S
slzhou 已提交
112
int32_t callUdfAggMerge(UdfHandle handle, SUdfInterBuf *interBuf1, SUdfInterBuf *interBuf2, SUdfInterBuf *resultBuf);
S
shenglian zhou 已提交
113 114
// input: block
// output: resultData
S
slzhou 已提交
115
int32_t callUdfScalaProcess(UdfHandle handle, SSDataBlock *block, SSDataBlock *resultData);
116

117 118 119 120 121 122 123 124 125
/**
 * tearn down udf
 * @param handle
 * @return
 */
int32_t teardownUdf(UdfHandle handle);

// end API to taosd and qworker
//=============================================================================================================================
126
// begin API to UDF writer.
127

S
shenglian zhou 已提交
128
// dynamic lib init and destroy
S
shenglian zhou 已提交
129 130 131
typedef int32_t (*TUdfSetupFunc)();
typedef int32_t (*TUdfTeardownFunc)();

132
//TODO: add API to check function arguments type, number etc.
133 134 135 136 137
//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 已提交
138
typedef int32_t (*TUdfFreeUdfColumnDataFunc)(SUdfColumn* columnData);
139

S
slzhou 已提交
140
typedef int32_t (*TUdfScalarProcFunc)(SUdfDataBlock block, SUdfColumn *resultCol);
S
shenglian zhou 已提交
141 142
typedef int32_t (*TUdfAggInitFunc)(SUdfInterBuf *buf);
typedef int32_t (*TUdfAggProcessFunc)(SUdfDataBlock block, SUdfInterBuf *interBuf);
S
slzhou 已提交
143
typedef int32_t (*TUdfAggFinalizeFunc)(SUdfInterBuf buf, SUdfInterBuf *resultData);
S
shenglian zhou 已提交
144 145


146 147
// end API to UDF writer
//=======================================================================================================================
148

149 150 151 152
#ifdef __cplusplus
}
#endif

153
#endif  // TDENGINE_TUDF_H