builtins.c 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 "builtins.h"
H
Haojun Liao 已提交
17
#include "builtinsimpl.h"
18
#include "taoserror.h"
H
Haojun Liao 已提交
19
#include "tdatablock.h"
20 21 22 23 24 25 26 27

int32_t stubCheckAndGetResultType(SFunctionNode* pFunc);

const SBuiltinFuncDefinition funcMgtBuiltins[] = {
  {
    .name = "count",
    .type = FUNCTION_TYPE_COUNT,
    .classification = FUNC_MGT_AGG_FUNC,
H
Haojun Liao 已提交
28 29 30 31 32
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getCountFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = countFunction,
    .finalizeFunc = functionFinalizer
33
  },
X
Xiaoyu Wang 已提交
34 35 36 37
  {
    .name = "sum",
    .type = FUNCTION_TYPE_SUM,
    .classification = FUNC_MGT_AGG_FUNC,
H
Haojun Liao 已提交
38 39 40 41 42
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getSumFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = sumFunction,
    .finalizeFunc = functionFinalizer
X
Xiaoyu Wang 已提交
43
  },
44 45 46
  {
    .name = "min",
    .type = FUNCTION_TYPE_MIN,
47
    .classification = FUNC_MGT_AGG_FUNC,
48 49 50 51 52 53 54 55 56
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getMinmaxFuncEnv,
    .initFunc     = minFunctionSetup,
    .processFunc  = minFunction,
    .finalizeFunc = functionFinalizer
  },
  {
    .name = "max",
    .type = FUNCTION_TYPE_MAX,
57
    .classification = FUNC_MGT_AGG_FUNC,
58 59 60 61 62 63
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getMinmaxFuncEnv,
    .initFunc     = maxFunctionSetup,
    .processFunc  = maxFunction,
    .finalizeFunc = functionFinalizer
  },
H
Haojun Liao 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  {
      .name = "stddev",
      .type = FUNCTION_TYPE_STDDEV,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
      .finalizeFunc = functionFinalizer
  },
  {
      .name = "percentile",
      .type = FUNCTION_TYPE_PERCENTILE,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
      .finalizeFunc = functionFinalizer
  },
  {
      .name = "apercentile",
      .type = FUNCTION_TYPE_APERCENTILE,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
      .finalizeFunc = functionFinalizer
  },
  {
      .name = "top",
      .type = FUNCTION_TYPE_TOP,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
      .finalizeFunc = functionFinalizer
  },
  {
      .name = "bottom",
      .type = FUNCTION_TYPE_BOTTOM,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
      .finalizeFunc = functionFinalizer
  },
  {
      .name = "spread",
      .type = FUNCTION_TYPE_SPREAD,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
      .finalizeFunc = functionFinalizer
  },
  {
      .name = "last_row",
      .type = FUNCTION_TYPE_LAST_ROW,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
      .finalizeFunc = functionFinalizer
  },
134 135 136 137
  {
    .name = "concat",
    .type = FUNCTION_TYPE_CONCAT,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
H
Haojun Liao 已提交
138 139 140
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
D
dapan1121 已提交
141
    .sprocessFunc = NULL,
142 143 144 145
    .finalizeFunc = NULL
  }
};

X
Xiaoyu Wang 已提交
146
const int32_t funcMgtBuiltinsNum = (sizeof(funcMgtBuiltins) / sizeof(SBuiltinFuncDefinition));
147 148

int32_t stubCheckAndGetResultType(SFunctionNode* pFunc) {
H
Haojun Liao 已提交
149
  switch(pFunc->funcType) {
X
Xiaoyu Wang 已提交
150 151 152
    case FUNCTION_TYPE_COUNT:
      pFunc->node.resType = (SDataType){.bytes = sizeof(int64_t), .type = TSDB_DATA_TYPE_BIGINT};
      break;
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    case FUNCTION_TYPE_SUM: {
      SColumnNode* pParam = nodesListGetNode(pFunc->pParameterList, 0);
      int32_t paraType = pParam->node.resType.type;

      int32_t resType  = 0;
      if (IS_SIGNED_NUMERIC_TYPE(paraType)) {
        resType = TSDB_DATA_TYPE_BIGINT;
      } else if (IS_UNSIGNED_NUMERIC_TYPE(paraType)) {
        resType = TSDB_DATA_TYPE_UBIGINT;
      } else if (IS_FLOAT_TYPE(paraType)) {
        resType = TSDB_DATA_TYPE_DOUBLE;
      } else {
        ASSERT(0);
      }

      pFunc->node.resType = (SDataType) { .bytes = tDataTypes[resType].bytes, .type = resType };
      break;
    }
    case FUNCTION_TYPE_MIN:
    case FUNCTION_TYPE_MAX: {
      SColumnNode* pParam = nodesListGetNode(pFunc->pParameterList, 0);
      int32_t paraType = pParam->node.resType.type;
      pFunc->node.resType = (SDataType) { .bytes = tDataTypes[paraType].bytes, .type = paraType };
H
Haojun Liao 已提交
176
      break;
177
    }
X
Xiaoyu Wang 已提交
178 179 180
    case FUNCTION_TYPE_CONCAT:
      // todo
      break;
181 182
    default:
      ASSERT(0); // to found the fault ASAP.
H
Haojun Liao 已提交
183 184
  }

185 186
  return TSDB_CODE_SUCCESS;
}