提交 daab0724 编写于 作者: S shenglian zhou

cell/floor/round reimplemented with scalar expr

上级 9aa19d59
......@@ -1837,19 +1837,30 @@ static int32_t handleAggTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t
char aliasName[TSDB_COL_NAME_LEN] = {0};
getColumnName(pItem, aliasName, rawName, TSDB_COL_NAME_LEN);
tExprNode *pExpr = NULL;
uint64_t uid = 0;
int32_t ret = exprTreeFromSqlExpr(pCmd, &pExpr, pItem->pNode, pQueryInfo, NULL, &uid);
if (ret != TSDB_CODE_SUCCESS) {
tExprTreeDestroy(pExpr, NULL);
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "invalid expression in select clause");
}
ret = exprTreeValidateTree(pExpr);
if (ret != TSDB_CODE_SUCCESS) {
tExprTreeDestroy(pExpr, NULL);
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2);
}
// the expr associated with the result field will become exprList1 in SQueryInfo, then pExpr2 in SQueryAttr
insertResultField(pQueryInfo, exprIndex, columnList, sizeof(double), TSDB_DATA_TYPE_DOUBLE, aliasName, NULL);
insertResultField(pQueryInfo, exprIndex, columnList, pExpr->resultBytes, (int8_t)pExpr->resultType, aliasName, NULL);
int32_t slot = tscNumOfFields(pQueryInfo) - 1;
SInternalField* pInfo = tscFieldInfoGetInternalField(&pQueryInfo->fieldsInfo, slot);
assert(pInfo->pExpr == NULL);
SExprInfo* pExprInfo = calloc(1, sizeof(SExprInfo));
// arithmetic expression always return result in the format of double float
pExprInfo->base.resBytes = sizeof(double);
pExprInfo->base.resBytes = pExpr->resultBytes;
pExprInfo->base.interBytes = 0;
pExprInfo->base.resType = TSDB_DATA_TYPE_DOUBLE;
pExprInfo->base.resType = pExpr->resultType;
pExprInfo->base.functionId = TSDB_FUNC_SCALAR_EXPR;
pExprInfo->base.numOfParams = 1;
......@@ -1857,19 +1868,9 @@ static int32_t handleAggTypeExpr(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t
strncpy(pExprInfo->base.aliasName, aliasName, tListLen(pExprInfo->base.aliasName));
strncpy(pExprInfo->base.token, rawName, tListLen(pExprInfo->base.token));
int32_t ret = exprTreeFromSqlExpr(pCmd, &pExprInfo->pExpr, pItem->pNode, pQueryInfo, NULL, &(pExprInfo->base.uid));
if (ret != TSDB_CODE_SUCCESS) {
tExprTreeDestroy(pExprInfo->pExpr, NULL);
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "invalid expression in select clause");
}
pExprInfo->pExpr = pExpr;
pExprInfo->base.uid = uid;
pInfo->pExpr = pExprInfo;
ret = exprTreeValidateTree(pExprInfo->pExpr);
if (ret != TSDB_CODE_SUCCESS) {
tExprTreeDestroy(pExprInfo->pExpr, NULL);
pExprInfo->pExpr = NULL;
return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2);
}
SBufferWriter bw = tbufInitWriter(NULL, false);
......@@ -2608,9 +2609,6 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col
case TSDB_FUNC_DIFF:
case TSDB_FUNC_DERIVATIVE:
case TSDB_FUNC_CSUM:
case TSDB_FUNC_CEIL:
case TSDB_FUNC_FLOOR:
case TSDB_FUNC_ROUND:
case TSDB_FUNC_STDDEV:
case TSDB_FUNC_LEASTSQR: {
// 1. valid the number of parameters
......@@ -3685,10 +3683,6 @@ static bool functionCompatibleCheck(SQueryInfo* pQueryInfo, bool joinQuery, bool
++prjNum;
}
if (functionId == TSDB_FUNC_CEIL || functionId == TSDB_FUNC_FLOOR || functionId == TSDB_FUNC_ROUND) {
++scalarFuncNum;
}
if (TSDB_FUNC_IS_SCALAR(functionId)) {
++scalarFuncNum;
}
......@@ -6662,8 +6656,7 @@ int32_t validateFunctionsInIntervalOrGroupbyQuery(SSqlCmd* pCmd, SQueryInfo* pQu
int32_t f = pExpr->base.functionId;
if ((f == TSDB_FUNC_PRJ && pExpr->base.numOfParams == 0) ||
f == TSDB_FUNC_DIFF || f == TSDB_FUNC_SCALAR_EXPR || f == TSDB_FUNC_DERIVATIVE ||
f == TSDB_FUNC_CSUM || f == TSDB_FUNC_MAVG ||
f == TSDB_FUNC_CEIL || f == TSDB_FUNC_FLOOR || f == TSDB_FUNC_ROUND)
f == TSDB_FUNC_CSUM || f == TSDB_FUNC_MAVG)
{
isProjectionFunction = true;
break;
......
......@@ -274,9 +274,6 @@ bool tscIsProjectionQueryOnSTable(SQueryInfo* pQueryInfo, int32_t tableIndex) {
functionId != TSDB_FUNC_CSUM &&
functionId != TSDB_FUNC_TS_DUMMY &&
functionId != TSDB_FUNC_TID_TAG &&
functionId != TSDB_FUNC_CEIL &&
functionId != TSDB_FUNC_FLOOR &&
functionId != TSDB_FUNC_ROUND &&
!TSDB_FUNC_IS_SCALAR(functionId)) {
return false;
}
......
......@@ -63,6 +63,7 @@ struct SSchema;
#define TSDB_FUNC_SCALAR_CONCAT (TSDB_FUNC_FLAG_SCALAR | 0x000D)
#define TSDB_FUNC_SCALAR_LENGTH (TSDB_FUNC_FLAG_SCALAR | 0x000E)
#define TSDB_FUNC_SCALAR_MAX_NUM 15
#define TSDB_FUNC_SCALAR_NAME_MAX_LEN 16
typedef struct {
......
......@@ -1258,7 +1258,7 @@ void vectorMathFunc(int16_t functionId, tExprOperandInfo *pInputs, uint8_t numIn
SET_TYPED_DATA(outputData, pOutput->type, result);
} else if (pInputs[0].type == TSDB_DATA_TYPE_DOUBLE) {
double v = 0;
GET_TYPED_DATA(v, float, pInputs[0].type, inputData[0]);
GET_TYPED_DATA(v, double, pInputs[0].type, inputData[0]);
double result = ceil(v);
SET_TYPED_DATA(outputData, pOutput->type, result);
}
......@@ -1278,7 +1278,7 @@ void vectorMathFunc(int16_t functionId, tExprOperandInfo *pInputs, uint8_t numIn
SET_TYPED_DATA(outputData, pOutput->type, result);
} else if (pInputs[0].type == TSDB_DATA_TYPE_DOUBLE) {
double v = 0;
GET_TYPED_DATA(v, float, pInputs[0].type, inputData[0]);
GET_TYPED_DATA(v, double, pInputs[0].type, inputData[0]);
double result = floor(v);
SET_TYPED_DATA(outputData, pOutput->type, result);
}
......@@ -1299,7 +1299,7 @@ void vectorMathFunc(int16_t functionId, tExprOperandInfo *pInputs, uint8_t numIn
SET_TYPED_DATA(outputData, pOutput->type, result);
} else if (pInputs[0].type == TSDB_DATA_TYPE_DOUBLE) {
double v = 0;
GET_TYPED_DATA(v, float, pInputs[0].type, inputData[0]);
GET_TYPED_DATA(v, double, pInputs[0].type, inputData[0]);
double result = round(v);
SET_TYPED_DATA(outputData, pOutput->type, result);
}
......@@ -1310,9 +1310,9 @@ void vectorMathFunc(int16_t functionId, tExprOperandInfo *pInputs, uint8_t numIn
assert(false);
break;
}
}
}
}
} // end switch function(id)
} // end can produce value, all child has value
} // end for each row
free(inputData);
}
......@@ -1381,7 +1381,7 @@ tScalarFunctionInfo aScalarFunctions[] = {
},
{
TSDB_FUNC_SCALAR_FLOOR,
"flooor",
"floor",
vectorMathFunc,
},
{
......
......@@ -70,15 +70,11 @@ extern "C" {
#define TSDB_FUNC_TID_TAG 31
#define TSDB_FUNC_DERIVATIVE 32
#define TSDB_FUNC_CEIL 33
#define TSDB_FUNC_FLOOR 34
#define TSDB_FUNC_ROUND 35
#define TSDB_FUNC_CSUM 33
#define TSDB_FUNC_MAVG 34
#define TSDB_FUNC_SAMPLE 35
#define TSDB_FUNC_CSUM 36
#define TSDB_FUNC_MAVG 37
#define TSDB_FUNC_SAMPLE 38
#define TSDB_FUNC_BLKINFO 39
#define TSDB_FUNC_BLKINFO 36
///////////////////////////////////////////
// the following functions is not implemented.
......
......@@ -206,8 +206,7 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI
if (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TS_DUMMY || functionId == TSDB_FUNC_TAG_DUMMY ||
functionId == TSDB_FUNC_DIFF || functionId == TSDB_FUNC_PRJ || functionId == TSDB_FUNC_TAGPRJ ||
functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_INTERP || functionId == TSDB_FUNC_CEIL ||
functionId == TSDB_FUNC_FLOOR || functionId == TSDB_FUNC_ROUND)
functionId == TSDB_FUNC_TAG || functionId == TSDB_FUNC_INTERP)
{
*type = (int16_t)dataType;
*bytes = (int16_t)dataBytes;
......@@ -4502,233 +4501,6 @@ void blockinfo_func_finalizer(SQLFunctionCtx* pCtx) {
doFinalizer(pCtx);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define CFR_SET_VAL(type, data, pCtx, func, i, step, notNullElems) \
do { \
type *pData = (type *) data; \
type *pOutput = (type *) pCtx->pOutput; \
\
for (; i < pCtx->size && i >= 0; i += step) { \
if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { \
continue; \
} \
\
*pOutput++ = (type) func((double) pData[i]); \
\
notNullElems++; \
} \
} while (0)
#define CFR_SET_VAL_DOUBLE(data, pCtx, func, i, step, notNullElems) \
do { \
double *pData = (double *) data; \
double *pOutput = (double *) pCtx->pOutput; \
\
for (; i < pCtx->size && i >= 0; i += step) { \
if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { \
continue; \
} \
\
SET_DOUBLE_VAL(pOutput, func(pData[i])); \
pOutput++; \
\
notNullElems++; \
} \
} while (0)
static void ceil_function(SQLFunctionCtx *pCtx) {
void *data = GET_INPUT_DATA_LIST(pCtx);
int32_t notNullElems = 0;
int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order);
int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size - 1;
switch (pCtx->inputType) {
case TSDB_DATA_TYPE_INT: {
CFR_SET_VAL(int32_t, data, pCtx, ceil, i, step, notNullElems);
break;
};
case TSDB_DATA_TYPE_UINT: {
CFR_SET_VAL(uint32_t, data, pCtx, ceil, i, step, notNullElems);
break;
};
case TSDB_DATA_TYPE_BIGINT: {
CFR_SET_VAL(int64_t, data, pCtx, ceil, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_UBIGINT: {
CFR_SET_VAL(uint64_t, data, pCtx, ceil, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_DOUBLE: {
CFR_SET_VAL_DOUBLE(data, pCtx, ceil, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_FLOAT: {
CFR_SET_VAL(float, data, pCtx, ceil, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_SMALLINT: {
CFR_SET_VAL(int16_t, data, pCtx, ceil, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_USMALLINT: {
CFR_SET_VAL(uint16_t, data, pCtx, ceil, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_TINYINT: {
CFR_SET_VAL(int8_t, data, pCtx, ceil, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_UTINYINT: {
CFR_SET_VAL(uint8_t, data, pCtx, ceil, i, step, notNullElems);
break;
}
default:
qError("error input type");
}
if (notNullElems <= 0) {
/*
* current block may be null value
*/
assert(pCtx->hasNull);
} else {
GET_RES_INFO(pCtx)->numOfRes += notNullElems;
}
}
static void floor_function(SQLFunctionCtx *pCtx) {
void *data = GET_INPUT_DATA_LIST(pCtx);
int32_t notNullElems = 0;
int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order);
int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size - 1;
switch (pCtx->inputType) {
case TSDB_DATA_TYPE_INT: {
CFR_SET_VAL(int32_t, data, pCtx, floor, i, step, notNullElems);
break;
};
case TSDB_DATA_TYPE_UINT: {
CFR_SET_VAL(uint32_t, data, pCtx, floor, i, step, notNullElems);
break;
};
case TSDB_DATA_TYPE_BIGINT: {
CFR_SET_VAL(int64_t, data, pCtx, floor, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_UBIGINT: {
CFR_SET_VAL(uint64_t, data, pCtx, floor, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_DOUBLE: {
CFR_SET_VAL_DOUBLE(data, pCtx, floor, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_FLOAT: {
CFR_SET_VAL(float, data, pCtx, floor, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_SMALLINT: {
CFR_SET_VAL(int16_t, data, pCtx, floor, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_USMALLINT: {
CFR_SET_VAL(uint16_t, data, pCtx, floor, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_TINYINT: {
CFR_SET_VAL(int8_t, data, pCtx, floor, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_UTINYINT: {
CFR_SET_VAL(uint8_t, data, pCtx, floor, i, step, notNullElems);
break;
}
default:
qError("error input type");
}
if (notNullElems <= 0) {
/*
* current block may be null value
*/
assert(pCtx->hasNull);
} else {
GET_RES_INFO(pCtx)->numOfRes += notNullElems;
}
}
static void round_function(SQLFunctionCtx *pCtx) {
void *data = GET_INPUT_DATA_LIST(pCtx);
int32_t notNullElems = 0;
int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order);
int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size - 1;
switch (pCtx->inputType) {
case TSDB_DATA_TYPE_INT: {
CFR_SET_VAL(int32_t, data, pCtx, round, i, step, notNullElems);
break;
};
case TSDB_DATA_TYPE_UINT: {
CFR_SET_VAL(uint32_t, data, pCtx, round, i, step, notNullElems);
break;
};
case TSDB_DATA_TYPE_BIGINT: {
CFR_SET_VAL(int64_t, data, pCtx, round, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_UBIGINT: {
CFR_SET_VAL(uint64_t, data, pCtx, round, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_DOUBLE: {
CFR_SET_VAL_DOUBLE(data, pCtx, round, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_FLOAT: {
CFR_SET_VAL(float, data, pCtx, round, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_SMALLINT: {
CFR_SET_VAL(int16_t, data, pCtx, round, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_USMALLINT: {
CFR_SET_VAL(uint16_t, data, pCtx, round, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_TINYINT: {
CFR_SET_VAL(int8_t, data, pCtx, round, i, step, notNullElems);
break;
}
case TSDB_DATA_TYPE_UTINYINT: {
CFR_SET_VAL(uint8_t, data, pCtx, round, i, step, notNullElems);
break;
}
default:
qError("error input type");
}
if (notNullElems <= 0) {
/*
* current block may be null value
*/
assert(pCtx->hasNull);
} else {
GET_RES_INFO(pCtx)->numOfRes += notNullElems;
}
}
#undef CFR_SET_VAL
#undef CFR_SET_VAL_DOUBLE
//////////////////////////////////////////////////////////////////////////////////
//cumulative_sum function
......@@ -5074,8 +4846,8 @@ int32_t functionCompatList[] = {
4, -1, -1, 1, 1, 1, 1, 1, 1, -1,
// tag, colprj, tagprj, arithm, diff, first_dist, last_dist, stddev_dst, interp rate, irate
1, 1, 1, 1, -1, 1, 1, 1, 5, 1, 1,
// tid_tag, deriv, ceil, floor, round, csum, mavg, sample,
6, 8, 1, 1, 1, -1, -1, -1,
// tid_tag, deriv, csum, mavg, sample,
6, 8, -1, -1, -1,
// block_info
7
};
......@@ -5479,39 +5251,6 @@ SAggFunctionInfo aAggs[] = {{
noop1,
dataBlockRequired,
},
{// 33
"ceil",
TSDB_FUNC_CEIL,
TSDB_FUNC_CEIL,
TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
function_setup,
ceil_function,
doFinalizer,
noop1,
dataBlockRequired
},
{// 34
"floor",
TSDB_FUNC_FLOOR,
TSDB_FUNC_FLOOR,
TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
function_setup,
floor_function,
doFinalizer,
noop1,
dataBlockRequired
},
{// 35
"round",
TSDB_FUNC_ROUND,
TSDB_FUNC_ROUND,
TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS | TSDB_FUNCSTATE_SCALAR,
function_setup,
round_function,
doFinalizer,
noop1,
dataBlockRequired
},
{
// 36
"csum",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册