functionMgt.c 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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/>.
 */

#include "functionMgt.h"

#include "functionMgtInt.h"
#include "taos.h"
#include "taoserror.h"
#include "thash.h"
22
#include "builtins.h"
23 24 25 26 27 28 29 30

typedef struct SFuncMgtService {
  SHashObj* pFuncNameHashTable;
} SFuncMgtService;

static SFuncMgtService gFunMgtService;

int32_t fmFuncMgtInit() {
31
  gFunMgtService.pFuncNameHashTable = taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
32 33 34
  if (NULL == gFunMgtService.pFuncNameHashTable) {
    return TSDB_CODE_FAILED;
  }
35 36 37 38
  for (int32_t i = 0; i < funcMgtBuiltinsNum; ++i) {
    if (TSDB_CODE_SUCCESS != taosHashPut(gFunMgtService.pFuncNameHashTable, funcMgtBuiltins[i].name, strlen(funcMgtBuiltins[i].name), &i, sizeof(int32_t))) {
      return TSDB_CODE_FAILED;
    }
39
  }
40
  return TSDB_CODE_SUCCESS;
41 42
}

43 44 45
int32_t fmGetHandle(FuncMgtHandle* pHandle) {
  *pHandle = &gFunMgtService;
  return TSDB_CODE_SUCCESS;
46 47
}

48 49 50 51 52 53 54 55 56 57 58
int32_t fmGetFuncInfo(FuncMgtHandle handle, const char* pFuncName, int32_t* pFuncId, int32_t* pFuncType) {
  SFuncMgtService* pService = (SFuncMgtService*)handle;
  pFuncId = taosHashGet(pService->pFuncNameHashTable, pFuncName, strlen(pFuncName));
  if (NULL == pFuncId) {
    return TSDB_CODE_FAILED;
  }
  if (*pFuncId < 0 || *pFuncId >= funcMgtBuiltinsNum) {
    return TSDB_CODE_FAILED;
  }
  *pFuncType = funcMgtBuiltins[*pFuncId].type;
  return TSDB_CODE_SUCCESS;
59
}
60

61 62 63 64 65
int32_t fmGetFuncResultType(SFunctionNode* pFunc) {
  if (pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
    return TSDB_CODE_FAILED;
  }
  return funcMgtBuiltins[pFunc->funcId].checkFunc(pFunc);
66 67 68
}

bool fmIsAggFunc(int32_t funcId) {
69 70 71 72
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
    return false;
  }
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, FUNC_MGT_AGG_FUNC);
73
}