TDengine/source/libs/function/src/texpr.c

329 lines
9.1 KiB
C
Raw Normal View History

2019-07-11 08:36:16 +00:00
/*
* 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 "function.h"
2019-07-26 11:15:31 +00:00
#include "os.h"
2020-07-07 08:39:01 +00:00
2022-02-28 01:52:30 +00:00
#include "texception.h"
#include "taosdef.h"
2021-12-24 07:00:51 +00:00
#include "tmsg.h"
2020-07-07 08:39:01 +00:00
#include "tarray.h"
#include "tbuffer.h"
#include "tcompare.h"
2021-10-08 12:33:46 +00:00
#include "thash.h"
2021-01-23 06:49:06 +00:00
#include "texpr.h"
2021-10-08 12:33:46 +00:00
#include "tvariant.h"
2022-02-23 06:34:17 +00:00
#include "tdef.h"
2019-07-11 08:36:16 +00:00
2021-01-23 06:49:06 +00:00
static void doExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *));
void tExprTreeDestroy(tExprNode *pNode, void (*fp)(void *)) {
2021-01-03 14:25:35 +00:00
if (pNode == NULL) {
return;
}
if (pNode->nodeType == TEXPR_BINARYEXPR_NODE || pNode->nodeType == TEXPR_UNARYEXPR_NODE) {
2021-01-23 06:49:06 +00:00
doExprTreeDestroy(&pNode, fp);
} else if (pNode->nodeType == TEXPR_VALUE_NODE) {
2021-10-08 13:52:18 +00:00
taosVariantDestroy(pNode->pVal);
} else if (pNode->nodeType == TEXPR_COL_NODE) {
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear(pNode->pSchema);
2021-01-03 14:25:35 +00:00
}
2022-03-25 16:29:53 +00:00
taosMemoryFree(pNode);
2021-01-03 14:25:35 +00:00
}
2021-01-23 06:49:06 +00:00
static void doExprTreeDestroy(tExprNode **pExpr, void (*fp)(void *)) {
2021-01-03 14:25:35 +00:00
if (*pExpr == NULL) {
return;
}
int32_t type = (*pExpr)->nodeType;
if (type == TEXPR_VALUE_NODE) {
2021-10-08 13:52:18 +00:00
taosVariantDestroy((*pExpr)->pVal);
2022-03-25 16:29:53 +00:00
taosMemoryFree((*pExpr)->pVal);
} else if (type == TEXPR_COL_NODE) {
2022-03-25 16:29:53 +00:00
taosMemoryFree((*pExpr)->pSchema);
2021-01-03 14:25:35 +00:00
}
2022-03-25 16:29:53 +00:00
taosMemoryFree(*pExpr);
2021-01-03 14:25:35 +00:00
*pExpr = NULL;
}
2021-03-29 05:23:29 +00:00
bool exprTreeApplyFilter(tExprNode *pExpr, const void *pItem, SExprTraverseSupp *param) {
#if 0
2021-01-03 14:25:35 +00:00
//non-leaf nodes, recursively traverse the expression tree in the post-root order
if (pLeft->nodeType == TEXPR_BINARYEXPR_NODE && pRight->nodeType == TEXPR_BINARYEXPR_NODE) {
2022-02-23 06:34:17 +00:00
if (pExpr->_node.optr == LOGIC_COND_TYPE_OR) { // or
2021-03-29 05:23:29 +00:00
if (exprTreeApplyFilter(pLeft, pItem, param)) {
2021-01-03 14:25:35 +00:00
return true;
}
// left child does not satisfy the query condition, try right child
2021-03-29 05:23:29 +00:00
return exprTreeApplyFilter(pRight, pItem, param);
2021-01-03 14:25:35 +00:00
} else { // and
2021-03-29 05:23:29 +00:00
if (!exprTreeApplyFilter(pLeft, pItem, param)) {
2021-01-03 14:25:35 +00:00
return false;
}
2021-03-29 05:23:29 +00:00
return exprTreeApplyFilter(pRight, pItem, param);
2021-01-03 14:25:35 +00:00
}
}
// handle the leaf node
param->setupInfoFn(pExpr, param->pExtInfo);
return param->nodeFilterFn(pItem, pExpr->_node.info);
#endif
return 0;
2021-01-03 14:25:35 +00:00
}
2020-04-27 06:03:02 +00:00
// TODO: these three functions should be made global
static void* exception_calloc(size_t nmemb, size_t size) {
2022-03-25 16:29:53 +00:00
void* p = taosMemoryCalloc(nmemb, size);
2020-04-27 06:03:02 +00:00
if (p == NULL) {
2020-06-06 06:30:50 +00:00
THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
}
2020-04-27 06:03:02 +00:00
return p;
}
static void* exception_malloc(size_t size) {
2022-03-25 16:29:53 +00:00
void* p = taosMemoryMalloc(size);
2020-04-27 06:03:02 +00:00
if (p == NULL) {
2020-06-06 06:30:50 +00:00
THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
2020-04-27 06:03:02 +00:00
}
return p;
}
2020-05-23 09:34:15 +00:00
static UNUSED_FUNC char* exception_strdup(const char* str) {
2020-04-27 06:03:02 +00:00
char* p = strdup(str);
if (p == NULL) {
2020-06-06 06:30:50 +00:00
THROW(TSDB_CODE_QRY_OUT_OF_MEMORY);
2020-04-27 06:03:02 +00:00
}
return p;
}
2021-06-03 04:45:30 +00:00
void buildFilterSetFromBinary(void **q, const char *buf, int32_t len) {
SBufferReader br = tbufInitReader(buf, len, false);
uint32_t type = tbufReadUint32(&br);
SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(type), true, false);
2021-10-08 13:52:18 +00:00
// taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(type));
2021-06-03 04:45:30 +00:00
int dummy = -1;
int32_t sz = tbufReadInt32(&br);
for (int32_t i = 0; i < sz; i++) {
if (type == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(type)) {
int64_t val = tbufReadInt64(&br);
taosHashPut(pObj, (char *)&val, sizeof(val), &dummy, sizeof(dummy));
} else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
uint64_t val = tbufReadUint64(&br);
taosHashPut(pObj, (char *)&val, sizeof(val), &dummy, sizeof(dummy));
}
else if (type == TSDB_DATA_TYPE_TIMESTAMP) {
2021-06-03 04:45:30 +00:00
int64_t val = tbufReadInt64(&br);
taosHashPut(pObj, (char *)&val, sizeof(val), &dummy, sizeof(dummy));
} else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) {
double val = tbufReadDouble(&br);
taosHashPut(pObj, (char *)&val, sizeof(val), &dummy, sizeof(dummy));
2021-06-04 06:19:48 +00:00
} else if (type == TSDB_DATA_TYPE_BINARY) {
2021-06-03 04:45:30 +00:00
size_t t = 0;
const char *val = tbufReadBinary(&br, &t);
taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
2021-06-04 06:19:48 +00:00
} else if (type == TSDB_DATA_TYPE_NCHAR) {
2021-06-04 19:54:42 +00:00
size_t t = 0;
const char *val = tbufReadBinary(&br, &t);
taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy));
2021-06-03 04:45:30 +00:00
}
}
*q = (void *)pObj;
}
2021-06-21 10:23:36 +00:00
void convertFilterSetFromBinary(void **q, const char *buf, int32_t len, uint32_t tType) {
SBufferReader br = tbufInitReader(buf, len, false);
uint32_t sType = tbufReadUint32(&br);
SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(tType), true, false);
2021-10-08 13:52:18 +00:00
// taosHashSetEqualFp(pObj, taosGetDefaultEqualFunction(tType));
2021-06-21 10:23:36 +00:00
int dummy = -1;
2021-10-08 12:33:46 +00:00
SVariant tmpVar = {0};
2021-06-21 10:23:36 +00:00
size_t t = 0;
int32_t sz = tbufReadInt32(&br);
2021-07-27 02:55:44 +00:00
void *pvar = NULL;
int64_t val = 0;
2021-06-21 10:23:36 +00:00
int32_t bufLen = 0;
if (IS_NUMERIC_TYPE(sType)) {
bufLen = 60; // The maximum length of string that a number is converted to.
} else {
bufLen = 128;
}
2022-03-25 16:29:53 +00:00
char *tmp = taosMemoryCalloc(1, bufLen * TSDB_NCHAR_SIZE);
2021-06-21 10:23:36 +00:00
for (int32_t i = 0; i < sz; i++) {
switch (sType) {
case TSDB_DATA_TYPE_BOOL:
2021-06-22 02:01:24 +00:00
case TSDB_DATA_TYPE_UTINYINT:
2021-06-21 10:23:36 +00:00
case TSDB_DATA_TYPE_TINYINT: {
2021-07-27 02:55:44 +00:00
*(uint8_t *)&val = (uint8_t)tbufReadInt64(&br);
2021-06-21 10:23:36 +00:00
t = sizeof(val);
pvar = &val;
break;
}
2021-06-22 02:01:24 +00:00
case TSDB_DATA_TYPE_USMALLINT:
2021-06-21 10:23:36 +00:00
case TSDB_DATA_TYPE_SMALLINT: {
2021-07-27 02:55:44 +00:00
*(uint16_t *)&val = (uint16_t)tbufReadInt64(&br);
2021-06-21 10:23:36 +00:00
t = sizeof(val);
pvar = &val;
break;
}
2021-06-22 02:01:24 +00:00
case TSDB_DATA_TYPE_UINT:
2021-06-21 10:23:36 +00:00
case TSDB_DATA_TYPE_INT: {
2021-07-27 02:55:44 +00:00
*(uint32_t *)&val = (uint32_t)tbufReadInt64(&br);
2021-06-21 10:23:36 +00:00
t = sizeof(val);
pvar = &val;
break;
}
2021-06-23 00:55:46 +00:00
case TSDB_DATA_TYPE_TIMESTAMP:
2021-06-22 02:01:24 +00:00
case TSDB_DATA_TYPE_UBIGINT:
2021-06-21 10:23:36 +00:00
case TSDB_DATA_TYPE_BIGINT: {
2021-07-27 02:55:44 +00:00
*(uint64_t *)&val = (uint64_t)tbufReadInt64(&br);
2021-06-21 10:23:36 +00:00
t = sizeof(val);
pvar = &val;
break;
}
case TSDB_DATA_TYPE_DOUBLE: {
2021-07-27 02:55:44 +00:00
*(double *)&val = tbufReadDouble(&br);
2021-06-21 10:23:36 +00:00
t = sizeof(val);
pvar = &val;
break;
}
case TSDB_DATA_TYPE_FLOAT: {
2021-07-27 02:55:44 +00:00
*(float *)&val = (float)tbufReadDouble(&br);
2021-06-21 10:23:36 +00:00
t = sizeof(val);
pvar = &val;
break;
}
case TSDB_DATA_TYPE_BINARY: {
2021-07-27 02:55:44 +00:00
pvar = (char *)tbufReadBinary(&br, &t);
2021-06-21 10:23:36 +00:00
break;
}
case TSDB_DATA_TYPE_NCHAR: {
2021-07-27 02:55:44 +00:00
pvar = (char *)tbufReadBinary(&br, &t);
2021-06-21 10:23:36 +00:00
break;
}
default:
taosHashCleanup(pObj);
*q = NULL;
return;
}
2021-10-08 13:52:18 +00:00
taosVariantCreateFromBinary(&tmpVar, (char *)pvar, t, sType);
2021-06-21 10:23:36 +00:00
if (bufLen < t) {
2022-03-25 16:29:53 +00:00
tmp = taosMemoryRealloc(tmp, t * TSDB_NCHAR_SIZE);
2021-08-03 02:51:20 +00:00
bufLen = (int32_t)t;
2021-06-21 10:23:36 +00:00
}
switch (tType) {
case TSDB_DATA_TYPE_BOOL:
2021-06-22 02:01:24 +00:00
case TSDB_DATA_TYPE_UTINYINT:
2021-06-21 10:23:36 +00:00
case TSDB_DATA_TYPE_TINYINT: {
2021-10-08 12:33:46 +00:00
if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
2021-06-21 10:23:36 +00:00
goto err_ret;
}
pvar = &val;
t = sizeof(val);
break;
}
2021-06-22 02:01:24 +00:00
case TSDB_DATA_TYPE_USMALLINT:
2021-06-21 10:23:36 +00:00
case TSDB_DATA_TYPE_SMALLINT: {
2021-10-08 12:33:46 +00:00
if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
2021-06-21 10:23:36 +00:00
goto err_ret;
}
pvar = &val;
t = sizeof(val);
break;
}
2021-06-22 02:01:24 +00:00
case TSDB_DATA_TYPE_UINT:
2021-06-21 10:23:36 +00:00
case TSDB_DATA_TYPE_INT: {
2021-10-08 12:33:46 +00:00
if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
2021-06-21 10:23:36 +00:00
goto err_ret;
}
pvar = &val;
t = sizeof(val);
break;
}
2021-06-23 00:55:46 +00:00
case TSDB_DATA_TYPE_TIMESTAMP:
2021-06-22 02:01:24 +00:00
case TSDB_DATA_TYPE_UBIGINT:
2021-06-21 10:23:36 +00:00
case TSDB_DATA_TYPE_BIGINT: {
2021-10-08 12:33:46 +00:00
if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
2021-06-21 10:23:36 +00:00
goto err_ret;
}
pvar = &val;
t = sizeof(val);
break;
}
case TSDB_DATA_TYPE_DOUBLE: {
2021-10-08 12:33:46 +00:00
if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
2021-06-21 10:23:36 +00:00
goto err_ret;
}
pvar = &val;
t = sizeof(val);
break;
}
case TSDB_DATA_TYPE_FLOAT: {
2021-10-08 12:33:46 +00:00
if (taosVariantDump(&tmpVar, (char *)&val, tType, false)) {
2021-06-21 10:23:36 +00:00
goto err_ret;
}
pvar = &val;
t = sizeof(val);
break;
}
case TSDB_DATA_TYPE_BINARY: {
2021-10-08 12:33:46 +00:00
if (taosVariantDump(&tmpVar, tmp, tType, true)) {
2021-06-21 10:23:36 +00:00
goto err_ret;
}
t = varDataLen(tmp);
pvar = varDataVal(tmp);
break;
}
case TSDB_DATA_TYPE_NCHAR: {
2021-10-08 12:33:46 +00:00
if (taosVariantDump(&tmpVar, tmp, tType, true)) {
2021-06-21 10:23:36 +00:00
goto err_ret;
}
t = varDataLen(tmp);
pvar = varDataVal(tmp);
break;
}
default:
goto err_ret;
}
taosHashPut(pObj, (char *)pvar, t, &dummy, sizeof(dummy));
2021-10-08 13:52:18 +00:00
taosVariantDestroy(&tmpVar);
2021-07-27 02:55:44 +00:00
memset(&tmpVar, 0, sizeof(tmpVar));
2021-06-21 10:23:36 +00:00
}
*q = (void *)pObj;
pObj = NULL;
err_ret:
2021-10-08 13:52:18 +00:00
taosVariantDestroy(&tmpVar);
2021-06-21 10:23:36 +00:00
taosHashCleanup(pObj);
2022-03-25 16:29:53 +00:00
taosMemoryFreeClear(tmp);
2021-06-21 10:23:36 +00:00
}