functionMgt.c 4.7 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

typedef struct SFuncMgtService {
  SHashObj* pFuncNameHashTable;
} SFuncMgtService;

static SFuncMgtService gFunMgtService;
wafwerar's avatar
wafwerar 已提交
29
static TdThreadOnce functionHashTableInit = PTHREAD_ONCE_INIT;
H
Haojun Liao 已提交
30
static int32_t initFunctionCode = 0;
31

H
Haojun Liao 已提交
32
static void doInitFunctionHashTable() {
33
  gFunMgtService.pFuncNameHashTable = taosHashInit(funcMgtBuiltinsNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
34
  if (NULL == gFunMgtService.pFuncNameHashTable) {
H
Haojun Liao 已提交
35 36
    initFunctionCode = TSDB_CODE_FAILED;
    return;
37
  }
H
Haojun Liao 已提交
38

39 40
  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))) {
H
Haojun Liao 已提交
41 42
      initFunctionCode = TSDB_CODE_FAILED;
      return;
43
    }
44
  }
H
Haojun Liao 已提交
45 46
}

47 48 49 50 51 52 53
static bool isSpecificClassifyFunc(int32_t funcId, uint64_t classification) {
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
    return false;
  }
  return FUNC_MGT_TEST_MASK(funcMgtBuiltins[funcId].classification, classification);
}

H
Haojun Liao 已提交
54
int32_t fmFuncMgtInit() {
wafwerar's avatar
wafwerar 已提交
55
  taosThreadOnce(&functionHashTableInit, doInitFunctionHashTable);
H
Haojun Liao 已提交
56
  return initFunctionCode;
57 58
}

X
Xiaoyu Wang 已提交
59 60
int32_t fmGetFuncInfo(const char* pFuncName, int32_t* pFuncId, int32_t* pFuncType) {
  void* pVal = taosHashGet(gFunMgtService.pFuncNameHashTable, pFuncName, strlen(pFuncName));
61
  if (NULL == pVal) {
62 63
    return TSDB_CODE_FAILED;
  }
64
  *pFuncId = *(int32_t*)pVal;
65 66 67 68 69
  if (*pFuncId < 0 || *pFuncId >= funcMgtBuiltinsNum) {
    return TSDB_CODE_FAILED;
  }
  *pFuncType = funcMgtBuiltins[*pFuncId].type;
  return TSDB_CODE_SUCCESS;
70
}
71

72 73 74 75 76
int32_t fmGetFuncResultType(SFunctionNode* pFunc) {
  if (pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
    return TSDB_CODE_FAILED;
  }
  return funcMgtBuiltins[pFunc->funcId].checkFunc(pFunc);
77 78
}

79 80 81 82 83 84 85 86 87 88
EFuncDataRequired fmFuncDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) {
  if (pFunc->funcId < 0 || pFunc->funcId >= funcMgtBuiltinsNum) {
    return FUNC_DATA_REQUIRED_ALL_NEEDED;
  }
  if (NULL == funcMgtBuiltins[pFunc->funcId].dataRequiredFunc) {
    return FUNC_DATA_REQUIRED_ALL_NEEDED;
  }
  return funcMgtBuiltins[pFunc->funcId].dataRequiredFunc(pFunc, pTimeWindow);
}

X
Xiaoyu Wang 已提交
89 90 91 92 93 94 95 96 97 98 99
int32_t fmGetFuncExecFuncs(int32_t funcId, SFuncExecFuncs* pFpSet) {
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
    return TSDB_CODE_FAILED;
  }
  pFpSet->getEnv = funcMgtBuiltins[funcId].getEnvFunc;
  pFpSet->init = funcMgtBuiltins[funcId].initFunc;
  pFpSet->process = funcMgtBuiltins[funcId].processFunc;
  pFpSet->finalize = funcMgtBuiltins[funcId].finalizeFunc;
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
100 101 102 103 104
int32_t fmGetScalarFuncExecFuncs(int32_t funcId, SScalarFuncExecFuncs* pFpSet) {
  if (funcId < 0 || funcId >= funcMgtBuiltinsNum) {
    return TSDB_CODE_FAILED;
  }
  pFpSet->process = funcMgtBuiltins[funcId].sprocessFunc;
105
  pFpSet->getEnv  = funcMgtBuiltins[funcId].getEnvFunc;
D
dapan1121 已提交
106 107 108
  return TSDB_CODE_SUCCESS;
}

109
bool fmIsAggFunc(int32_t funcId) {
110 111 112 113 114 115 116
  return isSpecificClassifyFunc(funcId, FUNC_MGT_AGG_FUNC);
}

bool fmIsScalarFunc(int32_t funcId) {
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SCALAR_FUNC);
}

X
Xiaoyu Wang 已提交
117 118 119 120
bool fmIsPseudoColumnFunc(int32_t funcId) {
  return isSpecificClassifyFunc(funcId, FUNC_MGT_PSEUDO_COLUMN_FUNC);
}

121 122 123 124 125 126
bool fmIsWindowPseudoColumnFunc(int32_t funcId) {
  return isSpecificClassifyFunc(funcId, FUNC_MGT_WINDOW_PC_FUNC);
}

bool fmIsWindowClauseFunc(int32_t funcId) {
  return fmIsAggFunc(funcId) || fmIsWindowPseudoColumnFunc(funcId);
127
}
X
Xiaoyu Wang 已提交
128

H
Haojun Liao 已提交
129 130 131 132
bool fmIsNonstandardSQLFunc(int32_t funcId) {
  return isSpecificClassifyFunc(funcId, FUNC_MGT_NONSTANDARD_SQL_FUNC);
}

133 134 135 136 137 138 139
bool fmIsSpecialDataRequiredFunc(int32_t funcId) {
  return isSpecificClassifyFunc(funcId, FUNC_MGT_SPECIAL_DATA_REQUIRED);
}

bool fmIsDynamicScanOptimizedFunc(int32_t funcId) {
  return isSpecificClassifyFunc(funcId, FUNC_MGT_DYNAMIC_SCAN_OPTIMIZED);
}
H
Haojun Liao 已提交
140

X
Xiaoyu Wang 已提交
141 142
void fmFuncMgtDestroy() {
  void* m = gFunMgtService.pFuncNameHashTable;
wafwerar's avatar
wafwerar 已提交
143
  if (m != NULL && atomic_val_compare_exchange_ptr((void**)&gFunMgtService.pFuncNameHashTable, m, 0) == m) {
X
Xiaoyu Wang 已提交
144 145 146
    taosHashCleanup(m);
  }
}