builtins.c 6.4 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
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getCountFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = countFunction,
H
Haojun Liao 已提交
32
    .finalizeFunc = functionFinalize
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
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getSumFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = sumFunction,
H
Haojun Liao 已提交
42
    .finalizeFunc = functionFinalize
X
Xiaoyu Wang 已提交
43
  },
44 45 46
  {
    .name = "min",
    .type = FUNCTION_TYPE_MIN,
47
    .classification = FUNC_MGT_AGG_FUNC,
48 49 50 51
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getMinmaxFuncEnv,
    .initFunc     = minFunctionSetup,
    .processFunc  = minFunction,
H
Haojun Liao 已提交
52
    .finalizeFunc = functionFinalize
53 54 55 56
  },
  {
    .name = "max",
    .type = FUNCTION_TYPE_MAX,
57
    .classification = FUNC_MGT_AGG_FUNC,
58 59 60 61
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getMinmaxFuncEnv,
    .initFunc     = maxFunctionSetup,
    .processFunc  = maxFunction,
H
Haojun Liao 已提交
62
    .finalizeFunc = functionFinalize
63
  },
H
Haojun Liao 已提交
64 65 66 67 68
  {
      .name = "stddev",
      .type = FUNCTION_TYPE_STDDEV,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
H
Haojun Liao 已提交
69
      .getEnvFunc   = getStddevFuncEnv,
H
Haojun Liao 已提交
70 71
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
H
Haojun Liao 已提交
72
      .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
73 74 75 76 77 78 79 80 81
  },
  {
      .name = "percentile",
      .type = FUNCTION_TYPE_PERCENTILE,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
H
Haojun Liao 已提交
82
      .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
83 84 85 86 87 88 89 90 91
  },
  {
      .name = "apercentile",
      .type = FUNCTION_TYPE_APERCENTILE,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
H
Haojun Liao 已提交
92
      .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
93 94 95 96 97 98 99 100 101
  },
  {
      .name = "top",
      .type = FUNCTION_TYPE_TOP,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
H
Haojun Liao 已提交
102
      .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
103 104 105 106 107 108 109 110 111
  },
  {
      .name = "bottom",
      .type = FUNCTION_TYPE_BOTTOM,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
H
Haojun Liao 已提交
112
      .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
113 114 115 116 117 118 119 120 121
  },
  {
      .name = "spread",
      .type = FUNCTION_TYPE_SPREAD,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
H
Haojun Liao 已提交
122
      .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
123 124 125 126 127 128 129 130 131
  },
  {
      .name = "last_row",
      .type = FUNCTION_TYPE_LAST_ROW,
      .classification = FUNC_MGT_AGG_FUNC,
      .checkFunc    = stubCheckAndGetResultType,
      .getEnvFunc   = getMinmaxFuncEnv,
      .initFunc     = maxFunctionSetup,
      .processFunc  = maxFunction,
H
Haojun Liao 已提交
132
      .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
133
  },
H
Haojun Liao 已提交
134 135 136 137 138 139 140 141
  {
    .name = "first",
    .type = FUNCTION_TYPE_FIRST,
    .classification = FUNC_MGT_AGG_FUNC,
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = firstFunction,
H
Haojun Liao 已提交
142
    .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
143 144 145 146 147 148 149 150 151
  },
  {
    .name = "last",
    .type = FUNCTION_TYPE_LAST,
    .classification = FUNC_MGT_AGG_FUNC,
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunction,
H
Haojun Liao 已提交
152
    .finalizeFunc = functionFinalize
H
Haojun Liao 已提交
153
  },
154 155 156 157
  {
    .name = "concat",
    .type = FUNCTION_TYPE_CONCAT,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
H
Haojun Liao 已提交
158 159 160
    .checkFunc    = stubCheckAndGetResultType,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
D
dapan1121 已提交
161
    .sprocessFunc = NULL,
162 163 164 165
    .finalizeFunc = NULL
  }
};

X
Xiaoyu Wang 已提交
166
const int32_t funcMgtBuiltinsNum = (sizeof(funcMgtBuiltins) / sizeof(SBuiltinFuncDefinition));
167 168

int32_t stubCheckAndGetResultType(SFunctionNode* pFunc) {
H
Haojun Liao 已提交
169
  switch(pFunc->funcType) {
X
Xiaoyu Wang 已提交
170 171 172
    case FUNCTION_TYPE_COUNT:
      pFunc->node.resType = (SDataType){.bytes = sizeof(int64_t), .type = TSDB_DATA_TYPE_BIGINT};
      break;
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    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;
    }
H
Haojun Liao 已提交
191 192
    case FUNCTION_TYPE_FIRST:
    case FUNCTION_TYPE_LAST:
193 194 195 196 197
    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 已提交
198
      break;
199
    }
X
Xiaoyu Wang 已提交
200 201 202
    case FUNCTION_TYPE_CONCAT:
      // todo
      break;
203 204
    default:
      ASSERT(0); // to found the fault ASAP.
H
Haojun Liao 已提交
205 206
  }

207 208
  return TSDB_CODE_SUCCESS;
}