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

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
#include "tdatablock.h"
S
shenglian zhou 已提交
26

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

S
slzhou 已提交
31
#define UDF_LISTEN_PIPE_NAME_LEN 32
S
slzhou 已提交
32
#define UDF_LISTEN_PIPE_NAME_PREFIX "udfd.sock."
33
#define UDF_DNODE_ID_ENV_NAME "DNODE_ID"
S
slzhou 已提交
34

35 36
//======================================================================================
//begin API to taosd and qworker
S
shenglian zhou 已提交
37

38 39
enum {
  UDFC_CODE_STOPPING = -1,
S
slzhou 已提交
40
  UDFC_CODE_PIPE_READ_ERR = -2,
41 42 43
  UDFC_CODE_CONNECT_PIPE_ERR = -3,
  UDFC_CODE_LOAD_UDF_FAILURE = -4,
  UDFC_CODE_INVALID_STATE = -5
44 45
};

46
typedef void *UdfcFuncHandle;
47 48 49 50 51 52 53

/**
 * setup udf
 * @param udf, in
 * @param handle, out
 * @return error code
 */
54
int32_t setupUdf(char udfName[], UdfcFuncHandle *handle);
55

S
shenglian zhou 已提交
56 57
typedef struct SUdfColumnMeta {
  int16_t type;
S
shenglian zhou 已提交
58
  int32_t bytes;
S
shenglian zhou 已提交
59 60 61
  uint8_t precision;
  uint8_t scale;
} SUdfColumnMeta;
62

S
shenglian zhou 已提交
63 64
typedef struct SUdfColumnData {
  int32_t numOfRows;
S
shenglian zhou 已提交
65
  int32_t rowsAlloc;
S
shenglian zhou 已提交
66
  union {
S
slzhou 已提交
67 68 69 70 71 72 73 74 75
    struct {
      int32_t nullBitmapLen;
      char   *nullBitmap;
      int32_t dataLen;
      char   *data;
    } fixLenCol;

    struct {
      int32_t varOffsetsLen;
S
shenglian zhou 已提交
76
      int32_t   *varOffsets;
S
slzhou 已提交
77 78
      int32_t payloadLen;
      char   *payload;
S
shenglian zhou 已提交
79
      int32_t payloadAllocLen;
S
slzhou 已提交
80
    } varLenCol;
S
shenglian zhou 已提交
81 82 83 84 85 86 87 88
  };
} 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
typedef struct SUdfInterBuf {
  int32_t bufLen;
  char* buf;
99
  int8_t numOfResult; //zero or one
S
shenglian zhou 已提交
100 101
} SUdfInterBuf;

S
slzhou 已提交
102
// output: interBuf
103
int32_t callUdfAggInit(UdfcFuncHandle handle, SUdfInterBuf *interBuf);
S
slzhou 已提交
104 105
// input: block, state
// output: newState
106
int32_t callUdfAggProcess(UdfcFuncHandle handle, SSDataBlock *block, SUdfInterBuf *state, SUdfInterBuf *newState);
S
shenglian zhou 已提交
107 108
// input: interBuf
// output: resultData
109
int32_t callUdfAggFinalize(UdfcFuncHandle handle, SUdfInterBuf *interBuf, SUdfInterBuf *resultData);
S
slzhou 已提交
110 111
// input: interbuf1, interbuf2
// output: resultBuf
112
int32_t callUdfAggMerge(UdfcFuncHandle handle, SUdfInterBuf *interBuf1, SUdfInterBuf *interBuf2, SUdfInterBuf *resultBuf);
S
shenglian zhou 已提交
113 114
// input: block
// output: resultData
S
slzhou 已提交
115
int32_t callUdfScalarFunc(UdfcFuncHandle handle, SScalarParam *input, int32_t numOfCols, SScalarParam *output);
116 117 118 119 120
/**
 * tearn down udf
 * @param handle
 * @return
 */
121
int32_t teardownUdf(UdfcFuncHandle handle);
122

S
shenglian zhou 已提交
123 124 125 126
bool udfAggGetEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv);
bool udfAggInit(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo* pResultCellInfo);
int32_t udfAggProcess(struct SqlFunctionCtx *pCtx);
int32_t udfAggFinalize(struct SqlFunctionCtx *pCtx, SSDataBlock* pBlock);
127 128
// end API to taosd and qworker
//=============================================================================================================================
129
// begin API to UDF writer.
130

S
shenglian zhou 已提交
131
// dynamic lib init and destroy
S
shenglian zhou 已提交
132 133 134
typedef int32_t (*TUdfSetupFunc)();
typedef int32_t (*TUdfTeardownFunc)();

135
//TODO: add API to check function arguments type, number etc.
S
shenglian zhou 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

#define UDF_MEMORY_EXP_GROWTH 1.5

static FORCE_INLINE int32_t udfColEnsureCapacity(SUdfColumn* pColumn, int32_t newCapacity) {
  SUdfColumnMeta *meta = &pColumn->colMeta;
  SUdfColumnData *data = &pColumn->colData;

  if (newCapacity== 0 || newCapacity <= data->rowsAlloc) {
    return TSDB_CODE_SUCCESS;
  }

  int allocCapacity = MAX(data->rowsAlloc, 8);
  while (allocCapacity < newCapacity) {
    allocCapacity *= UDF_MEMORY_EXP_GROWTH;
  }

  if (IS_VAR_DATA_TYPE(meta->type)) {
    char* tmp = taosMemoryRealloc(data->varLenCol.varOffsets, sizeof(int32_t) * allocCapacity);
    if (tmp == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    data->varLenCol.varOffsets = (int32_t*)tmp;
    data->varLenCol.varOffsetsLen = sizeof(int32_t) * allocCapacity;
    // for payload, add data in udfColDataAppend
  } else {
    char* tmp = taosMemoryRealloc(data->fixLenCol.nullBitmap, BitmapLen(allocCapacity));
    if (tmp == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    data->fixLenCol.nullBitmap = tmp;
    data->fixLenCol.nullBitmapLen = BitmapLen(allocCapacity);
    if (meta->type == TSDB_DATA_TYPE_NULL) {
      return TSDB_CODE_SUCCESS;
    }

    tmp = taosMemoryRealloc(data->fixLenCol.data, allocCapacity* meta->bytes);
    if (tmp == NULL) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }

    data->fixLenCol.data = tmp;
    data->fixLenCol.dataLen = allocCapacity* meta->bytes;
  }

  data->rowsAlloc = allocCapacity;

  return TSDB_CODE_SUCCESS;
}

static FORCE_INLINE int32_t udfColSetRow(SUdfColumn* pColumn, uint32_t currentRow, const char* pData, bool isNull) {
  SUdfColumnMeta *meta = &pColumn->colMeta;
  SUdfColumnData *data = &pColumn->colData;
  udfColEnsureCapacity(pColumn, currentRow+1);
  bool isVarCol = IS_VAR_DATA_TYPE(meta->type);
  if (isNull) {
    if (isVarCol) {
      data->varLenCol.varOffsets[currentRow] = -1;
    } else {
      colDataSetNull_f(data->fixLenCol.nullBitmap, currentRow);
    }
  } else {
    if (!isVarCol) {
      colDataSetNotNull_f(data->fixLenCol.nullBitmap, currentRow);
      memcpy(data->fixLenCol.data + meta->bytes * currentRow, pData, meta->bytes);
    } else {
      int32_t dataLen = varDataTLen(pData);
      if (meta->type == TSDB_DATA_TYPE_JSON) {
        if (*pData == TSDB_DATA_TYPE_NULL) {
          dataLen = 0;
        } else if (*pData == TSDB_DATA_TYPE_NCHAR) {
          dataLen = varDataTLen(pData + CHAR_BYTES);
        } else if (*pData == TSDB_DATA_TYPE_BIGINT || *pData == TSDB_DATA_TYPE_DOUBLE) {
          dataLen = LONG_BYTES;
        } else if (*pData == TSDB_DATA_TYPE_BOOL) {
          dataLen = CHAR_BYTES;
        }
        dataLen += CHAR_BYTES;
      }

      if (data->varLenCol.payloadAllocLen < data->varLenCol.payloadLen + dataLen) {
        uint32_t newSize = data->varLenCol.payloadAllocLen;
        if (newSize <= 1) {
          newSize = 8;
        }

        while (newSize < data->varLenCol.payloadLen + dataLen) {
          newSize = newSize * UDF_MEMORY_EXP_GROWTH;
        }

        char *buf = taosMemoryRealloc(data->varLenCol.payload, newSize);
        if (buf == NULL) {
          return TSDB_CODE_OUT_OF_MEMORY;
        }

        data->varLenCol.payload = buf;
        data->varLenCol.payloadAllocLen = newSize;
      }

      uint32_t len = data->varLenCol.payloadLen;
      data->varLenCol.varOffsets[currentRow] = len;

      memcpy(data->varLenCol.payload + len, pData, dataLen);
      data->varLenCol.payloadLen += dataLen;
    }
  }
  data->numOfRows = MAX(currentRow + 1, data->numOfRows);
  return 0;
}
244

S
slzhou 已提交
245
typedef int32_t (*TUdfFreeUdfColumnFunc)(SUdfColumn* column);
246
typedef int32_t (*TUdfScalarProcFunc)(SUdfDataBlock* block, SUdfColumn *resultCol);
S
slzhou 已提交
247

248
typedef int32_t (*TUdfAggStartFunc)(SUdfInterBuf *buf);
S
shenglian zhou 已提交
249
typedef int32_t (*TUdfAggProcessFunc)(SUdfDataBlock* block, SUdfInterBuf *interBuf, SUdfInterBuf *newInterBuf);
250
typedef int32_t (*TUdfAggFinishFunc)(SUdfInterBuf* buf, SUdfInterBuf *resultData);
S
shenglian zhou 已提交
251 252


253 254
// end API to UDF writer
//=======================================================================================================================
255

256 257 258 259
#ifdef __cplusplus
}
#endif

260
#endif  // TDENGINE_TUDF_H